1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package libcore.net; 18 19 import static android.annotation.SystemApi.Client.MODULE_LIBRARIES; 20 21 import android.annotation.SystemApi; 22 import android.compat.annotation.UnsupportedAppUsage; 23 24 /** 25 * Network security policy for this process/application. 26 * 27 * <p>Network stacks/components are expected to honor this policy. Components which can use the 28 * Android framework API should be accessing this policy via the framework's 29 * {@code android.security.NetworkSecurityPolicy} instead of via this class. 30 * 31 * <p>The policy can be determined by the {@link #isCleartextTrafficPermitted()}, 32 * {@link #isCleartextTrafficPermitted(String)} and 33 * {@link #isCertificateTransparencyVerificationRequired(String)} methods. 34 * 35 * @hide 36 */ 37 @SystemApi(client = MODULE_LIBRARIES) 38 @libcore.api.IntraCoreApi 39 public abstract class NetworkSecurityPolicy { 40 41 private static volatile NetworkSecurityPolicy instance = new DefaultNetworkSecurityPolicy(); 42 43 /** 44 * Constructs a default {@code NetworkSecurityPolicy}. 45 * 46 * @see {@link #DefaultNetworkSecurityPolicy}. 47 * 48 * @hide 49 */ 50 @SystemApi(client = MODULE_LIBRARIES) NetworkSecurityPolicy()51 public NetworkSecurityPolicy() { 52 } 53 54 /** 55 * Gets current singleton {@code NetworkSecurityPolicy} instance. 56 * 57 * @return the current {@code NetworkSecurityPolicy}. 58 * 59 * @hide 60 */ 61 @SystemApi(client = MODULE_LIBRARIES) 62 @libcore.api.IntraCoreApi getInstance()63 public static NetworkSecurityPolicy getInstance() { 64 return instance; 65 } 66 67 /** 68 * Sets current singleton instance 69 * 70 * @param policy new {@code NetworlSecurityPolicy} instance. 71 * 72 * @hide 73 */ 74 @SystemApi(client = MODULE_LIBRARIES) setInstance(NetworkSecurityPolicy policy)75 public static void setInstance(NetworkSecurityPolicy policy) { 76 if (policy == null) { 77 throw new NullPointerException("policy == null"); 78 } 79 instance = policy; 80 } 81 82 /** 83 * Returns {@code true} if cleartext network traffic (e.g. HTTP, FTP, XMPP, IMAP, SMTP -- 84 * without TLS or STARTTLS) is permitted for all network communications of this process. 85 * 86 * <p>{@link #isCleartextTrafficPermitted(String)} should be used to determine if cleartext 87 * traffic is permitted for a specific host. 88 * 89 * <p>When cleartext network traffic is not permitted, the platform's components (e.g. HTTP 90 * stacks, {@code WebView}, {@code MediaPlayer}) will refuse this process's requests to use 91 * cleartext traffic. Third-party libraries are encouraged to do the same. 92 * 93 * <p>This flag is honored on a best effort basis because it's impossible to prevent all 94 * cleartext traffic from an application given the level of access provided to applications on 95 * Android. For example, there's no expectation that {@link java.net.Socket} API will honor this 96 * flag. Luckily, most network traffic from apps is handled by higher-level network stacks which 97 * can be made to honor this flag. Platform-provided network stacks (e.g. HTTP and FTP) honor 98 * this flag from day one, and well-established third-party network stacks will eventually 99 * honor it. 100 * 101 * @return {@code true} if cleartext traffic is permitted and {@code false} otherwise. 102 * 103 * @hide 104 */ 105 @UnsupportedAppUsage 106 @SystemApi(client = MODULE_LIBRARIES) isCleartextTrafficPermitted()107 public abstract boolean isCleartextTrafficPermitted(); 108 109 /** 110 * Returns {@code true} if cleartext network traffic (e.g. HTTP, FTP, XMPP, IMAP, SMTP -- 111 * without TLS or STARTTLS) is permitted for communicating with {@code hostname} for this 112 * process. 113 * 114 * <p>See {@link #isCleartextTrafficPermitted} for more details. 115 * 116 * @param hostname hostname to check if cleartext traffic is permitted for 117 * @return {@code true} if cleartext traffic is permitted and {@code false} otherwise 118 * 119 * @hide 120 */ 121 @SystemApi(client = MODULE_LIBRARIES) isCleartextTrafficPermitted(String hostname)122 public abstract boolean isCleartextTrafficPermitted(String hostname); 123 124 /** 125 * Returns {@code true} if Certificate Transparency information is required to be presented by 126 * the server and verified by the client in TLS connections to {@code hostname}. 127 * 128 * <p>See RFC6962 section 3.3 for more details. 129 * 130 * @param hostname hostname to check whether certificate transparency verification 131 * is required 132 * @return {@code true} if certificate transparency verification is required and 133 * {@code false} otherwise 134 * 135 * @hide 136 */ 137 @SystemApi(client = MODULE_LIBRARIES) 138 @libcore.api.IntraCoreApi isCertificateTransparencyVerificationRequired(String hostname)139 public abstract boolean isCertificateTransparencyVerificationRequired(String hostname); 140 141 /** 142 * Default network security policy that allows cleartext traffic and does not require 143 * certificate transparency verification. 144 * 145 * @hide 146 */ 147 public static final class DefaultNetworkSecurityPolicy extends NetworkSecurityPolicy { 148 @Override isCleartextTrafficPermitted()149 public boolean isCleartextTrafficPermitted() { 150 return true; 151 } 152 153 @Override isCleartextTrafficPermitted(String hostname)154 public boolean isCleartextTrafficPermitted(String hostname) { 155 return isCleartextTrafficPermitted(); 156 } 157 158 @Override isCertificateTransparencyVerificationRequired(String hostname)159 public boolean isCertificateTransparencyVerificationRequired(String hostname) { 160 return false; 161 } 162 } 163 } 164