Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c136cb0a authored by Chad Brubaker's avatar Chad Brubaker
Browse files

Add ConfigNetworkSecurityPolicy

ConfigNetworkSecurityPolicy is a NetworkSecurityPolicy based on an
ApplicationConfig.

Change-Id: I623854090f9eaa1c2bd3561dce6ce8268850c819
parent 2091ab94
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
@@ -120,6 +120,32 @@ public final class ApplicationConfig {
        return mTrustManager;
    }

    /**
     * Returns {@code true} if cleartext traffic is permitted for this application, which is the
     * case only if all configurations permit cleartext traffic. For finer-grained policy use
     * {@link #isCleartextTrafficPermitted(String)}.
     */
    public boolean isCleartextTrafficPermitted() {
        ensureInitialized();
        if (mConfigs != null) {
            for (Pair<Domain, NetworkSecurityConfig> entry : mConfigs) {
                if (!entry.second.isCleartextTrafficPermitted()) {
                    return false;
                }
            }
        }

        return mDefaultConfig.isCleartextTrafficPermitted();
    }

    /**
     * Returns {@code true} if cleartext traffic is permitted for this application when connecting
     * to {@code hostname}.
     */
    public boolean isCleartextTrafficPermitted(String hostname) {
        return getConfigForHostname(hostname).isCleartextTrafficPermitted();
    }

    private void ensureInitialized() {
        synchronized(mLock) {
            if (mInitialized) {
+40 −0
Original line number Diff line number Diff line
/**
 * Copyright (c) 2015, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.security.net.config;

/**
 * {@link libcore.net.NetworkSecurityPolicy} based on an {@link ApplicationConfig}.
 *
 * @hide
 */
public class ConfigNetworkSecurityPolicy extends libcore.net.NetworkSecurityPolicy {
    private final ApplicationConfig mConfig;

    public ConfigNetworkSecurityPolicy(ApplicationConfig config) {
        mConfig = config;
    }

    @Override
    public boolean isCleartextTrafficPermitted() {
        return mConfig.isCleartextTrafficPermitted();
    }

    @Override
    public boolean isCleartextTrafficPermitted(String hostname) {
        return mConfig.isCleartextTrafficPermitted(hostname);
    }
}
+1 −0
Original line number Diff line number Diff line
@@ -40,5 +40,6 @@ public final class NetworkSecurityConfigProvider extends Provider {
            throw new RuntimeException("Failed to install provider as highest priority provider."
                    + " Provider was installed at position " + pos);
        }
        libcore.net.NetworkSecurityPolicy.setInstance(new ConfigNetworkSecurityPolicy(config));
    }
}