blob: b885e726918da6ffa8f910eef9094769d3cea0ad [file] [log] [blame]
Chad Brubaker2075a3e2015-11-19 12:51:03 -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.net.config;
18
19import android.content.Context;
20import android.content.pm.ApplicationInfo;
Chad Brubaker2075a3e2015-11-19 12:51:03 -080021import android.util.Log;
22import android.util.Pair;
Chad Brubaker5ac2ea12017-10-18 10:35:04 -070023
Chad Brubaker2075a3e2015-11-19 12:51:03 -080024import java.util.Set;
25
26/** @hide */
27public class ManifestConfigSource implements ConfigSource {
Chad Brubaker2075a3e2015-11-19 12:51:03 -080028 private static final boolean DBG = true;
29 private static final String LOG_TAG = "NetworkSecurityConfig";
30
31 private final Object mLock = new Object();
32 private final Context mContext;
Chad Brubaker5ac2ea12017-10-18 10:35:04 -070033 private final ApplicationInfo mApplicationInfo;
Chad Brubaker2075a3e2015-11-19 12:51:03 -080034
35 private ConfigSource mConfigSource;
36
Chad Brubaker276ee962016-06-08 12:57:46 -070037 public ManifestConfigSource(Context context) {
Chad Brubaker2075a3e2015-11-19 12:51:03 -080038 mContext = context;
Chad Brubaker5ac2ea12017-10-18 10:35:04 -070039 // Cache the info because ApplicationInfo is mutable and apps do modify it :(
40 mApplicationInfo = new ApplicationInfo(context.getApplicationInfo());
Chad Brubaker2075a3e2015-11-19 12:51:03 -080041 }
42
43 @Override
44 public Set<Pair<Domain, NetworkSecurityConfig>> getPerDomainConfigs() {
45 return getConfigSource().getPerDomainConfigs();
46 }
47
48 @Override
49 public NetworkSecurityConfig getDefaultConfig() {
50 return getConfigSource().getDefaultConfig();
51 }
52
53 private ConfigSource getConfigSource() {
54 synchronized (mLock) {
55 if (mConfigSource != null) {
56 return mConfigSource;
57 }
Chad Brubaker5ac2ea12017-10-18 10:35:04 -070058 int configResource = mApplicationInfo.networkSecurityConfigRes;
Chad Brubaker2075a3e2015-11-19 12:51:03 -080059 ConfigSource source;
Chad Brubaker5ac2ea12017-10-18 10:35:04 -070060 if (configResource != 0) {
61 boolean debugBuild =
62 (mApplicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
Chad Brubaker2075a3e2015-11-19 12:51:03 -080063 if (DBG) {
64 Log.d(LOG_TAG, "Using Network Security Config from resource "
Chad Brubaker5ac2ea12017-10-18 10:35:04 -070065 + mContext.getResources()
66 .getResourceEntryName(configResource)
Chad Brubaker2075a3e2015-11-19 12:51:03 -080067 + " debugBuild: " + debugBuild);
68 }
Chad Brubaker5ac2ea12017-10-18 10:35:04 -070069 source = new XmlConfigSource(mContext, configResource, mApplicationInfo);
Chad Brubaker2075a3e2015-11-19 12:51:03 -080070 } else {
71 if (DBG) {
72 Log.d(LOG_TAG, "No Network Security Config specified, using platform default");
73 }
Chad Brubakerb8feba12016-12-06 10:26:29 -080074 // the legacy FLAG_USES_CLEARTEXT_TRAFFIC is not supported for Ephemeral apps, they
75 // should use the network security config.
Chad Brubaker8d28e4f2015-12-11 12:35:11 -080076 boolean usesCleartextTraffic =
Chad Brubaker5ac2ea12017-10-18 10:35:04 -070077 (mApplicationInfo.flags & ApplicationInfo.FLAG_USES_CLEARTEXT_TRAFFIC) != 0
Chad Brubaker11ecd582018-08-02 15:01:34 -070078 && !mApplicationInfo.isInstantApp();
Chad Brubaker5ac2ea12017-10-18 10:35:04 -070079 source = new DefaultConfigSource(usesCleartextTraffic, mApplicationInfo);
Chad Brubaker2075a3e2015-11-19 12:51:03 -080080 }
81 mConfigSource = source;
82 return mConfigSource;
83 }
84 }
85
86 private static final class DefaultConfigSource implements ConfigSource {
Chad Brubaker8d28e4f2015-12-11 12:35:11 -080087
88 private final NetworkSecurityConfig mDefaultConfig;
89
Chad Brubaker5ac2ea12017-10-18 10:35:04 -070090 DefaultConfigSource(boolean usesCleartextTraffic, ApplicationInfo info) {
91 mDefaultConfig = NetworkSecurityConfig.getDefaultBuilder(info)
Chad Brubaker8d28e4f2015-12-11 12:35:11 -080092 .setCleartextTrafficPermitted(usesCleartextTraffic)
93 .build();
Chad Brubaker32d2a102016-02-23 16:01:55 -080094 }
Chad Brubaker8d28e4f2015-12-11 12:35:11 -080095
Chad Brubaker2075a3e2015-11-19 12:51:03 -080096 @Override
97 public NetworkSecurityConfig getDefaultConfig() {
Chad Brubaker8d28e4f2015-12-11 12:35:11 -080098 return mDefaultConfig;
Chad Brubaker2075a3e2015-11-19 12:51:03 -080099 }
100
101 @Override
102 public Set<Pair<Domain, NetworkSecurityConfig>> getPerDomainConfigs() {
103 return null;
104 }
105 }
106}