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