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.util.Pair; 20 import java.security.InvalidAlgorithmParameterException; 21 import java.security.InvalidParameterException; 22 import java.security.KeyStore; 23 import java.security.KeyStoreException; 24 import java.security.NoSuchAlgorithmException; 25 import java.security.Provider; 26 import java.security.Security; 27 import java.util.Set; 28 import javax.net.ssl.ManagerFactoryParameters; 29 import javax.net.ssl.TrustManager; 30 import javax.net.ssl.TrustManagerFactory; 31 import javax.net.ssl.TrustManagerFactorySpi; 32 33 import com.android.internal.annotations.VisibleForTesting; 34 35 /** @hide */ 36 public class RootTrustManagerFactorySpi extends TrustManagerFactorySpi { 37 private ApplicationConfig mApplicationConfig; 38 private NetworkSecurityConfig mConfig; 39 40 @Override engineInit(ManagerFactoryParameters spec)41 public void engineInit(ManagerFactoryParameters spec) 42 throws InvalidAlgorithmParameterException { 43 if (!(spec instanceof ApplicationConfigParameters)) { 44 throw new InvalidAlgorithmParameterException("Unsupported spec: " + spec + ". Only " 45 + ApplicationConfigParameters.class.getName() + " supported"); 46 47 } 48 mApplicationConfig = ((ApplicationConfigParameters) spec).config; 49 } 50 51 @Override engineInit(KeyStore ks)52 public void engineInit(KeyStore ks) throws KeyStoreException { 53 if (ks != null) { 54 mApplicationConfig = new ApplicationConfig(new KeyStoreConfigSource(ks)); 55 } else { 56 mApplicationConfig = ApplicationConfig.getDefaultInstance(); 57 } 58 } 59 60 @Override engineGetTrustManagers()61 public TrustManager[] engineGetTrustManagers() { 62 if (mApplicationConfig == null) { 63 throw new IllegalStateException("TrustManagerFactory not initialized"); 64 } 65 return new TrustManager[] { mApplicationConfig.getTrustManager() }; 66 } 67 68 @VisibleForTesting 69 public static final class ApplicationConfigParameters implements ManagerFactoryParameters { 70 public final ApplicationConfig config; ApplicationConfigParameters(ApplicationConfig config)71 public ApplicationConfigParameters(ApplicationConfig config) { 72 this.config = config; 73 } 74 } 75 } 76