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 /** 20 * Network security policy for this process/application. 21 * 22 * <p>Network stacks/components are expected to honor this policy. Components which can use the 23 * Android framework API should be accessing this policy via the framework's 24 * {@code android.security.NetworkSecurityPolicy} instead of via this class. 25 * 26 * <p>The policy currently consists of a single flag: whether cleartext network traffic is 27 * permitted. See {@link #isCleartextTrafficPermitted()}. 28 */ 29 public abstract class NetworkSecurityPolicy { 30 private static volatile NetworkSecurityPolicy instance; 31 getInstance()32 public static NetworkSecurityPolicy getInstance() { 33 return instance; 34 } 35 setInstance(NetworkSecurityPolicy policy)36 public static void setInstance(NetworkSecurityPolicy policy) { 37 if (policy == null) { 38 throw new NullPointerException("policy == null"); 39 } 40 instance = policy; 41 } 42 43 /** 44 * Returns {@code true} if cleartext network traffic (e.g. HTTP, FTP, XMPP, IMAP, SMTP -- 45 * without TLS or STARTTLS) is permitted for all network communications of this process. 46 * 47 * <p>{@link #isCleartextTrafficPermitted(String)} should be used to determine if cleartext 48 * traffic is permitted for a specific host. 49 * 50 * <p>When cleartext network traffic is not permitted, the platform's components (e.g. HTTP 51 * stacks, {@code WebView}, {@code MediaPlayer}) will refuse this process's requests to use 52 * cleartext traffic. Third-party libraries are encouraged to do the same. 53 * 54 * <p>This flag is honored on a best effort basis because it's impossible to prevent all 55 * cleartext traffic from an application given the level of access provided to applications on 56 * Android. For example, there's no expectation that {@link java.net.Socket} API will honor this 57 * flag. Luckily, most network traffic from apps is handled by higher-level network stacks which 58 * can be made to honor this flag. Platform-provided network stacks (e.g. HTTP and FTP) honor 59 * this flag from day one, and well-established third-party network stacks will eventually 60 * honor it. 61 */ isCleartextTrafficPermitted()62 public abstract boolean isCleartextTrafficPermitted(); 63 64 /** 65 * Returns {@code true} if cleartext network traffic (e.g. HTTP, FTP, XMPP, IMAP, SMTP -- 66 * without TLS or STARTTLS) is permitted for communicating with {@code hostname} for this 67 * process. 68 * 69 * <p>See {@link #isCleartextTrafficPermitted} for more details. 70 */ isCleartextTrafficPermitted(String hostname)71 public abstract boolean isCleartextTrafficPermitted(String hostname); 72 73 /** 74 * Returns {@code true} if Certificate Transparency information is required to be presented by 75 * the server and verified by the client in TLS connections to {@code hostname}. 76 * 77 * <p>See RFC6962 section 3.3 for more details. 78 */ isCertificateTransparencyVerificationRequired(String hostname)79 public abstract boolean isCertificateTransparencyVerificationRequired(String hostname); 80 81 public static final class DefaultNetworkSecurityPolicy extends NetworkSecurityPolicy { 82 @Override isCleartextTrafficPermitted()83 public boolean isCleartextTrafficPermitted() { 84 return true; 85 } 86 87 @Override isCleartextTrafficPermitted(String hostname)88 public boolean isCleartextTrafficPermitted(String hostname) { 89 return isCleartextTrafficPermitted(); 90 } 91 92 @Override isCertificateTransparencyVerificationRequired(String hostname)93 public boolean isCertificateTransparencyVerificationRequired(String hostname) { 94 return false; 95 } 96 } 97 } 98