Chad Brubaker | 2075a3e | 2015-11-19 12:51:03 -0800 | [diff] [blame] | 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 android.security.net.config; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.pm.ApplicationInfo; |
Chad Brubaker | 2075a3e | 2015-11-19 12:51:03 -0800 | [diff] [blame] | 21 | import android.util.Log; |
| 22 | import android.util.Pair; |
Chad Brubaker | 5ac2ea1 | 2017-10-18 10:35:04 -0700 | [diff] [blame] | 23 | |
Chad Brubaker | 2075a3e | 2015-11-19 12:51:03 -0800 | [diff] [blame] | 24 | import java.util.Set; |
| 25 | |
| 26 | /** @hide */ |
| 27 | public class ManifestConfigSource implements ConfigSource { |
Chad Brubaker | 2075a3e | 2015-11-19 12:51:03 -0800 | [diff] [blame] | 28 | 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 Brubaker | 5ac2ea1 | 2017-10-18 10:35:04 -0700 | [diff] [blame] | 33 | private final ApplicationInfo mApplicationInfo; |
Chad Brubaker | 2075a3e | 2015-11-19 12:51:03 -0800 | [diff] [blame] | 34 | |
| 35 | private ConfigSource mConfigSource; |
| 36 | |
Chad Brubaker | 276ee96 | 2016-06-08 12:57:46 -0700 | [diff] [blame] | 37 | public ManifestConfigSource(Context context) { |
Chad Brubaker | 2075a3e | 2015-11-19 12:51:03 -0800 | [diff] [blame] | 38 | mContext = context; |
Chad Brubaker | 5ac2ea1 | 2017-10-18 10:35:04 -0700 | [diff] [blame] | 39 | // Cache the info because ApplicationInfo is mutable and apps do modify it :( |
| 40 | mApplicationInfo = new ApplicationInfo(context.getApplicationInfo()); |
Chad Brubaker | 2075a3e | 2015-11-19 12:51:03 -0800 | [diff] [blame] | 41 | } |
| 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 Brubaker | 5ac2ea1 | 2017-10-18 10:35:04 -0700 | [diff] [blame] | 58 | int configResource = mApplicationInfo.networkSecurityConfigRes; |
Chad Brubaker | 2075a3e | 2015-11-19 12:51:03 -0800 | [diff] [blame] | 59 | ConfigSource source; |
Chad Brubaker | 5ac2ea1 | 2017-10-18 10:35:04 -0700 | [diff] [blame] | 60 | if (configResource != 0) { |
| 61 | boolean debugBuild = |
| 62 | (mApplicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0; |
Chad Brubaker | 2075a3e | 2015-11-19 12:51:03 -0800 | [diff] [blame] | 63 | if (DBG) { |
| 64 | Log.d(LOG_TAG, "Using Network Security Config from resource " |
Chad Brubaker | 5ac2ea1 | 2017-10-18 10:35:04 -0700 | [diff] [blame] | 65 | + mContext.getResources() |
| 66 | .getResourceEntryName(configResource) |
Chad Brubaker | 2075a3e | 2015-11-19 12:51:03 -0800 | [diff] [blame] | 67 | + " debugBuild: " + debugBuild); |
| 68 | } |
Chad Brubaker | 5ac2ea1 | 2017-10-18 10:35:04 -0700 | [diff] [blame] | 69 | source = new XmlConfigSource(mContext, configResource, mApplicationInfo); |
Chad Brubaker | 2075a3e | 2015-11-19 12:51:03 -0800 | [diff] [blame] | 70 | } else { |
| 71 | if (DBG) { |
| 72 | Log.d(LOG_TAG, "No Network Security Config specified, using platform default"); |
| 73 | } |
Chad Brubaker | b8feba10 | 2016-12-06 10:26:29 -0800 | [diff] [blame] | 74 | // the legacy FLAG_USES_CLEARTEXT_TRAFFIC is not supported for Ephemeral apps, they |
| 75 | // should use the network security config. |
Chad Brubaker | 8d28e4f | 2015-12-11 12:35:11 -0800 | [diff] [blame] | 76 | boolean usesCleartextTraffic = |
Chad Brubaker | 5ac2ea1 | 2017-10-18 10:35:04 -0700 | [diff] [blame] | 77 | (mApplicationInfo.flags & ApplicationInfo.FLAG_USES_CLEARTEXT_TRAFFIC) != 0 |
Chad Brubaker | 11ecd58 | 2018-08-02 15:01:34 -0700 | [diff] [blame^] | 78 | && !mApplicationInfo.isInstantApp(); |
Chad Brubaker | 5ac2ea1 | 2017-10-18 10:35:04 -0700 | [diff] [blame] | 79 | source = new DefaultConfigSource(usesCleartextTraffic, mApplicationInfo); |
Chad Brubaker | 2075a3e | 2015-11-19 12:51:03 -0800 | [diff] [blame] | 80 | } |
| 81 | mConfigSource = source; |
| 82 | return mConfigSource; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | private static final class DefaultConfigSource implements ConfigSource { |
Chad Brubaker | 8d28e4f | 2015-12-11 12:35:11 -0800 | [diff] [blame] | 87 | |
| 88 | private final NetworkSecurityConfig mDefaultConfig; |
| 89 | |
Chad Brubaker | 5ac2ea1 | 2017-10-18 10:35:04 -0700 | [diff] [blame] | 90 | DefaultConfigSource(boolean usesCleartextTraffic, ApplicationInfo info) { |
| 91 | mDefaultConfig = NetworkSecurityConfig.getDefaultBuilder(info) |
Chad Brubaker | 8d28e4f | 2015-12-11 12:35:11 -0800 | [diff] [blame] | 92 | .setCleartextTrafficPermitted(usesCleartextTraffic) |
| 93 | .build(); |
Chad Brubaker | 32d2a10 | 2016-02-23 16:01:55 -0800 | [diff] [blame] | 94 | } |
Chad Brubaker | 8d28e4f | 2015-12-11 12:35:11 -0800 | [diff] [blame] | 95 | |
Chad Brubaker | 2075a3e | 2015-11-19 12:51:03 -0800 | [diff] [blame] | 96 | @Override |
| 97 | public NetworkSecurityConfig getDefaultConfig() { |
Chad Brubaker | 8d28e4f | 2015-12-11 12:35:11 -0800 | [diff] [blame] | 98 | return mDefaultConfig; |
Chad Brubaker | 2075a3e | 2015-11-19 12:51:03 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public Set<Pair<Domain, NetworkSecurityConfig>> getPerDomainConfigs() { |
| 103 | return null; |
| 104 | } |
| 105 | } |
| 106 | } |