• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**
19  * @author Boris Kuznetsov
20  * @version $Revision$
21  */
22 
23 package org.apache.harmony.xnet.provider.jsse;
24 
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.security.AccessController;
30 import java.security.InvalidAlgorithmParameterException;
31 import java.security.KeyStore;
32 import java.security.KeyStoreException;
33 import java.security.NoSuchAlgorithmException;
34 import java.security.cert.CertificateException;
35 
36 import javax.net.ssl.ManagerFactoryParameters;
37 import javax.net.ssl.TrustManager;
38 import javax.net.ssl.TrustManagerFactorySpi;
39 
40 /**
41  *
42  * TrustManagerFactory service provider interface implementation.
43  *
44  * @see javax.net.ssl.TrustManagerFactorySpi
45  */
46 public class TrustManagerFactoryImpl extends TrustManagerFactorySpi {
47 
48     private KeyStore keyStore;
49 
50     /**
51      * @see javax.net.ssl.TrustManagerFactorySpi#engineInit(KeyStore)
52      */
engineInit(KeyStore ks)53     public void engineInit(KeyStore ks) throws KeyStoreException {
54         if (ks != null) {
55             keyStore = ks;
56         } else {
57             // BEGIN android-added
58             if (System.getProperty("javax.net.ssl.trustStore") == null) {
59                 String file = System.getProperty("java.home")
60                     + java.io.File.separator + "etc" + java.io.File.separator
61                     + "security" + java.io.File.separator
62                     + "cacerts.bks";
63 
64                 System.setProperty("javax.net.ssl.trustStore", file);
65             }
66             // END android-added
67             keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
68             String keyStoreName = AccessController
69                     .doPrivileged(new java.security.PrivilegedAction<String>() {
70                         public String run() {
71                             return System
72                                     .getProperty("javax.net.ssl.trustStore");
73                         }
74                     });
75             String keyStorePwd = null;
76             if (keyStoreName == null || keyStoreName.equalsIgnoreCase("NONE")
77                     || keyStoreName.length() == 0) {
78                 try {
79                     keyStore.load(null, null);
80                 } catch (IOException e) {
81                     throw new KeyStoreException(e);
82                 } catch (CertificateException e) {
83                     throw new KeyStoreException(e);
84                 } catch (NoSuchAlgorithmException e) {
85                     throw new KeyStoreException(e);
86                 }
87             } else {
88                 keyStorePwd = AccessController
89                         .doPrivileged(new java.security.PrivilegedAction<String>() {
90                             public String run() {
91                                 return System
92                                         .getProperty("javax.net.ssl.trustStorePassword");
93                             }
94                         });
95                 char[] pwd;
96                 if (keyStorePwd == null) {
97                     pwd = new char[0];
98                 } else {
99                     pwd = keyStorePwd.toCharArray();
100                 }
101                 try {
102                     keyStore.load(new FileInputStream(new File(keyStoreName)), pwd);
103                 } catch (FileNotFoundException e) {
104                     throw new KeyStoreException(e);
105                 } catch (IOException e) {
106                     throw new KeyStoreException(e);
107                 } catch (CertificateException e) {
108                     throw new KeyStoreException(e);
109                 } catch (NoSuchAlgorithmException e) {
110                     throw new KeyStoreException(e);
111                 }
112             }
113         }
114 
115     }
116 
117     /**
118      * @see javax.net.ssl#engineInit(ManagerFactoryParameters)
119      */
engineInit(ManagerFactoryParameters spec)120     public void engineInit(ManagerFactoryParameters spec)
121             throws InvalidAlgorithmParameterException {
122         throw new InvalidAlgorithmParameterException(
123                 "ManagerFactoryParameters not supported");
124     }
125 
126     /**
127      * @see javax.net.ssl#engineGetTrustManagers()
128      */
engineGetTrustManagers()129     public TrustManager[] engineGetTrustManagers() {
130         if (keyStore == null) {
131             throw new IllegalStateException(
132                     "TrustManagerFactory is not initialized");
133         }
134         return new TrustManager[] { new TrustManagerImpl(keyStore) };
135     }
136 }
137