Home | History | Annotate | Download | only in conscrypt
      1 /*
      2  * Copyright (C) 2016 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 // License from Apache Harmony:
     18 /*
     19  *  Licensed to the Apache Software Foundation (ASF) under one or more
     20  *  contributor license agreements.  See the NOTICE file distributed with
     21  *  this work for additional information regarding copyright ownership.
     22  *  The ASF licenses this file to You under the Apache License, Version 2.0
     23  *  (the "License"); you may not use this file except in compliance with
     24  *  the License.  You may obtain a copy of the License at
     25  *
     26  *     http://www.apache.org/licenses/LICENSE-2.0
     27  *
     28  *  Unless required by applicable law or agreed to in writing, software
     29  *  distributed under the License is distributed on an "AS IS" BASIS,
     30  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     31  *  See the License for the specific language governing permissions and
     32  *  limitations under the License.
     33  */
     34 
     35 package org.conscrypt;
     36 
     37 import java.io.IOException;
     38 import java.security.InvalidAlgorithmParameterException;
     39 import java.security.KeyStore;
     40 import java.security.KeyStoreException;
     41 import java.security.NoSuchAlgorithmException;
     42 import java.security.cert.CertificateException;
     43 import javax.net.ssl.ManagerFactoryParameters;
     44 import javax.net.ssl.TrustManager;
     45 import javax.net.ssl.TrustManagerFactorySpi;
     46 
     47 /**
     48  *
     49  * TrustManagerFactory service provider interface implementation.
     50  *
     51  * @see javax.net.ssl.TrustManagerFactorySpi
     52  * @hide
     53  */
     54 @Internal
     55 public class TrustManagerFactoryImpl extends TrustManagerFactorySpi {
     56 
     57     private KeyStore keyStore;
     58 
     59     /**
     60      * @see javax.net.ssl.TrustManagerFactorySpi#engineInit(KeyStore)
     61      */
     62     @Override
     63     public void engineInit(KeyStore ks) throws KeyStoreException {
     64         if (ks != null) {
     65             keyStore = ks;
     66         } else {
     67             keyStore = KeyStore.getInstance("AndroidCAStore");
     68             try {
     69                 keyStore.load(null, null);
     70             } catch (IOException e) {
     71                 throw new KeyStoreException(e);
     72             } catch (CertificateException e) {
     73                 throw new KeyStoreException(e);
     74             } catch (NoSuchAlgorithmException e) {
     75                 throw new KeyStoreException(e);
     76             }
     77         }
     78     }
     79 
     80     /**
     81      * @see javax.net.ssl#engineInit(ManagerFactoryParameters)
     82      */
     83     @Override
     84     public void engineInit(ManagerFactoryParameters spec)
     85             throws InvalidAlgorithmParameterException {
     86         throw new InvalidAlgorithmParameterException(
     87                 "ManagerFactoryParameters not supported");
     88     }
     89 
     90     /**
     91      * @see javax.net.ssl#engineGetTrustManagers()
     92      */
     93     @Override
     94     public TrustManager[] engineGetTrustManagers() {
     95         if (keyStore == null) {
     96             throw new IllegalStateException(
     97                     "TrustManagerFactory is not initialized");
     98         }
     99         return new TrustManager[] { new TrustManagerImpl(keyStore) };
    100     }
    101 }
    102