• 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 package org.conscrypt;
18 
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.security.InvalidAlgorithmParameterException;
24 import java.security.KeyStore;
25 import java.security.KeyStoreException;
26 import java.security.NoSuchAlgorithmException;
27 import java.security.UnrecoverableKeyException;
28 import java.security.cert.CertificateException;
29 import javax.net.ssl.KeyManager;
30 import javax.net.ssl.KeyManagerFactorySpi;
31 import javax.net.ssl.ManagerFactoryParameters;
32 
33 /**
34  * KeyManagerFactory implementation.
35  * @see KeyManagerFactorySpi
36  * @hide
37  */
38 @Internal
39 public class KeyManagerFactoryImpl extends KeyManagerFactorySpi {
40 
41     // source of key material
42     private KeyStore keyStore;
43 
44     //password
45     private char[] pwd;
46 
47     /**
48      * @see KeyManagerFactorySpi#engineInit(KeyStore ks, char[] password)
49      */
50     @Override
engineInit(KeyStore ks, char[] password)51     protected void engineInit(KeyStore ks, char[] password)
52             throws KeyStoreException, NoSuchAlgorithmException,
53             UnrecoverableKeyException {
54         if (ks != null) {
55             keyStore = ks;
56             if (password != null) {
57                 pwd = password.clone();
58             } else {
59                 pwd = EmptyArray.CHAR;
60             }
61         } else {
62             keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
63             String keyStoreName = System.getProperty("javax.net.ssl.keyStore");
64             String keyStorePwd = null;
65             if (keyStoreName == null || keyStoreName.equalsIgnoreCase("NONE") || keyStoreName.isEmpty()) {
66                 try {
67                     keyStore.load(null, null);
68                 } catch (IOException e) {
69                     throw new KeyStoreException(e);
70                 } catch (CertificateException e) {
71                     throw new KeyStoreException(e);
72                 }
73             } else {
74                 keyStorePwd = System.getProperty("javax.net.ssl.keyStorePassword");
75                 if (keyStorePwd == null) {
76                     pwd = EmptyArray.CHAR;
77                 } else {
78                     pwd = keyStorePwd.toCharArray();
79                 }
80                 try {
81                     keyStore.load(new FileInputStream(new File(keyStoreName)), pwd);
82                 } catch (FileNotFoundException e) {
83                     throw new KeyStoreException(e);
84                 } catch (IOException e) {
85                     throw new KeyStoreException(e);
86                 } catch (CertificateException e) {
87                     throw new KeyStoreException(e);
88                 }
89             }
90 
91         }
92 
93     }
94 
95     /**
96      * @see KeyManagerFactorySpi#engineInit(ManagerFactoryParameters spec)
97      */
98     @Override
engineInit(ManagerFactoryParameters spec)99     protected void engineInit(ManagerFactoryParameters spec)
100             throws InvalidAlgorithmParameterException {
101         throw new InvalidAlgorithmParameterException(
102                 "ManagerFactoryParameters not supported");
103 
104     }
105 
106     /**
107      * @see KeyManagerFactorySpi#engineGetKeyManagers()
108      */
109     @Override
engineGetKeyManagers()110     protected KeyManager[] engineGetKeyManagers() {
111         if (keyStore == null) {
112             throw new IllegalStateException("KeyManagerFactory is not initialized");
113         }
114         return new KeyManager[] { new KeyManagerImpl(keyStore, pwd) };
115     }
116 }
117