Home | History | Annotate | Download | only in jsse
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 package org.apache.harmony.xnet.provider.jsse;
     19 
     20 import java.io.BufferedInputStream;
     21 import java.io.File;
     22 import java.io.FileInputStream;
     23 import java.io.FileNotFoundException;
     24 import java.io.IOException;
     25 import java.security.AccessController;
     26 import java.security.InvalidAlgorithmParameterException;
     27 import java.security.KeyStore;
     28 import java.security.KeyStoreException;
     29 import java.security.NoSuchAlgorithmException;
     30 import java.security.cert.CertificateException;
     31 import javax.net.ssl.ManagerFactoryParameters;
     32 import javax.net.ssl.TrustManager;
     33 import javax.net.ssl.TrustManagerFactorySpi;
     34 
     35 /**
     36  *
     37  * TrustManagerFactory service provider interface implementation.
     38  *
     39  * @see javax.net.ssl.TrustManagerFactorySpi
     40  */
     41 public class TrustManagerFactoryImpl extends TrustManagerFactorySpi {
     42 
     43     private KeyStore keyStore;
     44 
     45     /**
     46      * @see javax.net.ssl.TrustManagerFactorySpi#engineInit(KeyStore)
     47      */
     48     @Override
     49     public void engineInit(KeyStore ks) throws KeyStoreException {
     50         if (ks != null) {
     51             keyStore = ks;
     52         } else {
     53             // BEGIN android-added
     54             if (System.getProperty("javax.net.ssl.trustStore") == null) {
     55                 String file = System.getProperty("java.home")
     56                     + java.io.File.separator + "etc" + java.io.File.separator
     57                     + "security" + java.io.File.separator
     58                     + "cacerts.bks";
     59 
     60                 System.setProperty("javax.net.ssl.trustStore", file);
     61             }
     62             // END android-added
     63             keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
     64             String keyStoreName = AccessController
     65                     .doPrivileged(new java.security.PrivilegedAction<String>() {
     66                         public String run() {
     67                             return System
     68                                     .getProperty("javax.net.ssl.trustStore");
     69                         }
     70                     });
     71             String keyStorePwd = null;
     72             if (keyStoreName == null || keyStoreName.equalsIgnoreCase("NONE")
     73                     || keyStoreName.length() == 0) {
     74                 try {
     75                     keyStore.load(null, null);
     76                 } catch (IOException e) {
     77                     throw new KeyStoreException(e);
     78                 } catch (CertificateException e) {
     79                     throw new KeyStoreException(e);
     80                 } catch (NoSuchAlgorithmException e) {
     81                     throw new KeyStoreException(e);
     82                 }
     83             } else {
     84                 keyStorePwd = AccessController
     85                         .doPrivileged(new java.security.PrivilegedAction<String>() {
     86                             public String run() {
     87                                 return System
     88                                         .getProperty("javax.net.ssl.trustStorePassword");
     89                             }
     90                         });
     91                 char[] pwd;
     92                 if (keyStorePwd == null) {
     93                     pwd = new char[0];
     94                 } else {
     95                     pwd = keyStorePwd.toCharArray();
     96                 }
     97                 try {
     98                     keyStore.load(new BufferedInputStream(new FileInputStream(keyStoreName)), pwd);
     99                 } catch (FileNotFoundException e) {
    100                     throw new KeyStoreException(e);
    101                 } catch (IOException e) {
    102                     throw new KeyStoreException(e);
    103                 } catch (CertificateException e) {
    104                     throw new KeyStoreException(e);
    105                 } catch (NoSuchAlgorithmException e) {
    106                     throw new KeyStoreException(e);
    107                 }
    108             }
    109         }
    110 
    111     }
    112 
    113     /**
    114      * @see javax.net.ssl#engineInit(ManagerFactoryParameters)
    115      */
    116     @Override
    117     public void engineInit(ManagerFactoryParameters spec)
    118             throws InvalidAlgorithmParameterException {
    119         throw new InvalidAlgorithmParameterException(
    120                 "ManagerFactoryParameters not supported");
    121     }
    122 
    123     /**
    124      * @see javax.net.ssl#engineGetTrustManagers()
    125      */
    126     @Override
    127     public TrustManager[] engineGetTrustManagers() {
    128         if (keyStore == null) {
    129             throw new IllegalStateException(
    130                     "TrustManagerFactory is not initialized");
    131         }
    132         return new TrustManager[] { new TrustManagerImpl(keyStore) };
    133     }
    134 }
    135