blob: 0c4eedab2fc09e9631308013f12d9c88b266e267 [file] [log] [blame]
Alex Klyubinf9034cc2015-02-12 11:43:09 -08001/**
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
17package android.security;
18
Chad Brubaker9f779ab2016-06-20 12:40:20 -070019import android.content.Context;
20import android.content.pm.PackageManager;
Chad Brubaker056e8b52016-01-22 21:48:07 -080021import android.security.net.config.ApplicationConfig;
Chad Brubaker9f779ab2016-06-20 12:40:20 -070022import android.security.net.config.ManifestConfigSource;
Chad Brubaker056e8b52016-01-22 21:48:07 -080023
Alex Klyubinf9034cc2015-02-12 11:43:09 -080024/**
25 * Network security policy.
26 *
Alex Klyubin84750f32015-03-23 10:51:20 -070027 * <p>Network stacks/components should honor this policy to make it possible to centrally control
28 * the relevant aspects of network security behavior.
29 *
30 * <p>The policy currently consists of a single flag: whether cleartext network traffic is
31 * permitted. See {@link #isCleartextTrafficPermitted()}.
Alex Klyubinf9034cc2015-02-12 11:43:09 -080032 */
33public class NetworkSecurityPolicy {
34
Alex Klyubin84750f32015-03-23 10:51:20 -070035 private static final NetworkSecurityPolicy INSTANCE = new NetworkSecurityPolicy();
Alex Klyubinf9034cc2015-02-12 11:43:09 -080036
Alex Klyubin84750f32015-03-23 10:51:20 -070037 private NetworkSecurityPolicy() {}
Alex Klyubinf9034cc2015-02-12 11:43:09 -080038
Alex Klyubin84750f32015-03-23 10:51:20 -070039 /**
40 * Gets the policy for this process.
41 *
42 * <p>It's fine to cache this reference. Any changes to the policy will be immediately visible
43 * through the reference.
44 */
45 public static NetworkSecurityPolicy getInstance() {
46 return INSTANCE;
Alex Klyubinf9034cc2015-02-12 11:43:09 -080047 }
Alex Klyubinf9034cc2015-02-12 11:43:09 -080048
Alex Klyubin84750f32015-03-23 10:51:20 -070049 /**
50 * Returns whether cleartext network traffic (e.g. HTTP, FTP, WebSockets, XMPP, IMAP, SMTP --
Chad Brubaker2091ab92015-12-09 14:58:01 -080051 * without TLS or STARTTLS) is permitted for all network communication from this process.
Alex Klyubin84750f32015-03-23 10:51:20 -070052 *
53 * <p>When cleartext network traffic is not permitted, the platform's components (e.g. HTTP and
Alex Klyubinfbf45992015-04-21 13:44:29 -070054 * FTP stacks, {@link android.app.DownloadManager}, {@link android.media.MediaPlayer}) will
55 * refuse this process's requests to use cleartext traffic. Third-party libraries are strongly
56 * encouraged to honor this setting as well.
Alex Klyubin84750f32015-03-23 10:51:20 -070057 *
58 * <p>This flag is honored on a best effort basis because it's impossible to prevent all
59 * cleartext traffic from Android applications given the level of access provided to them. For
60 * example, there's no expectation that the {@link java.net.Socket} API will honor this flag
61 * because it cannot determine whether its traffic is in cleartext. However, most network
62 * traffic from applications is handled by higher-level network stacks/components which can
63 * honor this aspect of the policy.
Alex Klyubinfbf45992015-04-21 13:44:29 -070064 *
Nate Fischer6a2a5412017-10-23 18:02:41 -070065 * <p>NOTE: {@link android.webkit.WebView} honors this flag for applications targeting API level
66 * 26 and up.
Alex Klyubin84750f32015-03-23 10:51:20 -070067 */
68 public boolean isCleartextTrafficPermitted() {
Chad Brubaker6568cf12015-12-08 13:37:28 -080069 return libcore.net.NetworkSecurityPolicy.getInstance().isCleartextTrafficPermitted();
Alex Klyubinf9034cc2015-02-12 11:43:09 -080070 }
Alex Klyubin84750f32015-03-23 10:51:20 -070071
72 /**
Chad Brubaker2091ab92015-12-09 14:58:01 -080073 * Returns whether cleartext network traffic (e.g. HTTP, FTP, XMPP, IMAP, SMTP -- without
74 * TLS or STARTTLS) is permitted for communicating with {@code hostname} for this process.
75 *
76 * @see #isCleartextTrafficPermitted()
Chad Brubaker2091ab92015-12-09 14:58:01 -080077 */
78 public boolean isCleartextTrafficPermitted(String hostname) {
79 return libcore.net.NetworkSecurityPolicy.getInstance()
80 .isCleartextTrafficPermitted(hostname);
81 }
82
83 /**
Alex Klyubin84750f32015-03-23 10:51:20 -070084 * Sets whether cleartext network traffic is permitted for this process.
85 *
86 * <p>This method is used by the platform early on in the application's initialization to set
87 * the policy.
88 *
89 * @hide
90 */
91 public void setCleartextTrafficPermitted(boolean permitted) {
Chad Brubaker6568cf12015-12-08 13:37:28 -080092 FrameworkNetworkSecurityPolicy policy = new FrameworkNetworkSecurityPolicy(permitted);
93 libcore.net.NetworkSecurityPolicy.setInstance(policy);
Alex Klyubin84750f32015-03-23 10:51:20 -070094 }
Chad Brubaker056e8b52016-01-22 21:48:07 -080095
Chad Brubakerbf9a82a2016-03-25 10:12:19 -070096 /**
97 * Handle an update to the system or user certificate stores.
98 * @hide
99 */
Chad Brubakerbf9a82a2016-03-25 10:12:19 -0700100 public void handleTrustStorageUpdate() {
Chad Brubakerc72875b2016-04-27 16:35:11 -0700101 ApplicationConfig config = ApplicationConfig.getDefaultInstance();
102 if (config != null) {
103 config.handleTrustStorageUpdate();
104 }
Chad Brubakerbf9a82a2016-03-25 10:12:19 -0700105 }
Chad Brubaker9f779ab2016-06-20 12:40:20 -0700106
107 /**
108 * Returns an {@link ApplicationConfig} based on the configuration for {@code packageName}.
109 *
110 * @hide
111 */
112 public static ApplicationConfig getApplicationConfigForPackage(Context context,
113 String packageName) throws PackageManager.NameNotFoundException {
114 Context appContext = context.createPackageContext(packageName, 0);
115 ManifestConfigSource source = new ManifestConfigSource(appContext);
116 return new ApplicationConfig(source);
117 }
Alex Klyubinf9034cc2015-02-12 11:43:09 -0800118}