KeyManagerFactoryImpl.java revision 6186821cb13f4ac7ff50950c813394367e021eae
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 */
17package org.apache.harmony.xnet.provider.jsse;
18
19import java.io.File;
20import java.io.FileInputStream;
21import java.io.FileNotFoundException;
22import java.io.IOException;
23import java.security.AccessController;
24import java.security.InvalidAlgorithmParameterException;
25import java.security.KeyStore;
26import java.security.KeyStoreException;
27import java.security.NoSuchAlgorithmException;
28import java.security.UnrecoverableKeyException;
29import java.security.cert.CertificateException;
30import javax.net.ssl.KeyManager;
31import javax.net.ssl.KeyManagerFactorySpi;
32import javax.net.ssl.ManagerFactoryParameters;
33import libcore.util.EmptyArray;
34
35/**
36 * KeyManagerFactory implementation.
37 * @see javax.net.ssl.KeyManagerFactorySpi
38 */
39public 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 javax.net.ssl.KeyManagerFactorySpi#engineInit(KeyStore ks, char[]
49     *      password)
50     */
51    @Override
52    public void engineInit(KeyStore ks, char[] password)
53            throws KeyStoreException, NoSuchAlgorithmException,
54            UnrecoverableKeyException {
55        if (ks != null) {
56            keyStore = ks;
57            if (password != null) {
58                pwd = password.clone();
59            } else {
60                pwd = EmptyArray.CHAR;
61            }
62        } else {
63            keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
64            String keyStoreName = AccessController
65                    .doPrivileged(new java.security.PrivilegedAction<String>() {
66                        public String run() {
67                            return System.getProperty("javax.net.ssl.keyStore");
68                        }
69                    });
70            String keyStorePwd = null;
71            if (keyStoreName == null || keyStoreName.equalsIgnoreCase("NONE")
72                    || keyStoreName.length() == 0) {
73                try {
74                    keyStore.load(null, null);
75                } catch (IOException e) {
76                    throw new KeyStoreException(e);
77                } catch (CertificateException e) {
78                    throw new KeyStoreException(e);
79                }
80            } else {
81                keyStorePwd = AccessController
82                        .doPrivileged(new java.security.PrivilegedAction<String>() {
83                            public String run() {
84                                return System
85                                        .getProperty("javax.net.ssl.keyStorePassword");
86                            }
87                        });
88                if (keyStorePwd == null) {
89                    pwd = EmptyArray.CHAR;
90                } else {
91                    pwd = keyStorePwd.toCharArray();
92                }
93                try {
94                    keyStore.load(new FileInputStream(new File(keyStoreName)),
95                            pwd);
96
97                } catch (FileNotFoundException e) {
98                    throw new KeyStoreException(e);
99                } catch (IOException e) {
100                    throw new KeyStoreException(e);
101                } catch (CertificateException e) {
102                    throw new KeyStoreException(e);
103                }
104            }
105
106        }
107
108    }
109
110    /**
111     * @see javax.net.ssl.KeyManagerFactorySpi#engineInit(ManagerFactoryParameters
112     *      spec)
113     */
114    @Override
115    public void engineInit(ManagerFactoryParameters spec)
116            throws InvalidAlgorithmParameterException {
117        throw new InvalidAlgorithmParameterException(
118                "ManagerFactoryParameters not supported");
119
120    }
121
122    /**
123     * @see javax.net.ssl.KeyManagerFactorySpi#engineGetKeyManagers()
124     */
125    @Override
126    public KeyManager[] engineGetKeyManagers() {
127        if (keyStore == null) {
128            throw new IllegalStateException("KeyManagerFactory is not initialized");
129        }
130        return new KeyManager[] { new KeyManagerImpl(keyStore, pwd) };
131    }
132
133}
134