J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005-2007 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.security.ssl; |
| 27 | |
| 28 | import java.io.*; |
| 29 | import java.util.*; |
| 30 | |
| 31 | import java.security.*; |
| 32 | |
| 33 | import javax.net.ssl.*; |
| 34 | |
| 35 | /** |
| 36 | * "Default" SSLContext as returned by SSLContext.getDefault(). It comes |
| 37 | * initialized with default KeyManagers and TrustManagers created using |
| 38 | * various system properties. |
| 39 | * |
| 40 | * @since 1.6 |
| 41 | */ |
| 42 | public final class DefaultSSLContextImpl extends SSLContextImpl { |
| 43 | |
| 44 | private static final String NONE = "NONE"; |
| 45 | private static final String P11KEYSTORE = "PKCS11"; |
| 46 | private static final Debug debug = Debug.getInstance("ssl"); |
| 47 | |
| 48 | private static volatile SSLContextImpl defaultImpl; |
| 49 | |
| 50 | private static TrustManager[] defaultTrustManagers; |
| 51 | |
| 52 | private static KeyManager[] defaultKeyManagers; |
| 53 | |
| 54 | public DefaultSSLContextImpl() throws Exception { |
| 55 | super(defaultImpl); |
| 56 | try { |
| 57 | super.engineInit(getDefaultKeyManager(), getDefaultTrustManager(), null); |
| 58 | } catch (Exception e) { |
| 59 | if (debug != null && Debug.isOn("defaultctx")) { |
| 60 | System.out.println("default context init failed: " + e); |
| 61 | } |
| 62 | throw e; |
| 63 | } |
| 64 | if (defaultImpl == null) { |
| 65 | defaultImpl = this; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | protected void engineInit(KeyManager[] km, TrustManager[] tm, |
| 70 | SecureRandom sr) throws KeyManagementException { |
| 71 | throw new KeyManagementException |
| 72 | ("Default SSLContext is initialized automatically"); |
| 73 | } |
| 74 | |
| 75 | static synchronized SSLContextImpl getDefaultImpl() throws Exception { |
| 76 | if (defaultImpl == null) { |
| 77 | new DefaultSSLContextImpl(); |
| 78 | } |
| 79 | return defaultImpl; |
| 80 | } |
| 81 | |
| 82 | private static synchronized TrustManager[] getDefaultTrustManager() throws Exception { |
| 83 | if (defaultTrustManagers != null) { |
| 84 | return defaultTrustManagers; |
| 85 | } |
| 86 | |
| 87 | KeyStore ks = TrustManagerFactoryImpl.getCacertsKeyStore("defaultctx"); |
| 88 | |
| 89 | TrustManagerFactory tmf = TrustManagerFactory.getInstance( |
| 90 | TrustManagerFactory.getDefaultAlgorithm()); |
| 91 | tmf.init(ks); |
| 92 | defaultTrustManagers = tmf.getTrustManagers(); |
| 93 | return defaultTrustManagers; |
| 94 | } |
| 95 | |
| 96 | private static synchronized KeyManager[] getDefaultKeyManager() throws Exception { |
| 97 | if (defaultKeyManagers != null) { |
| 98 | return defaultKeyManagers; |
| 99 | } |
| 100 | |
| 101 | final Map<String,String> props = new HashMap<String,String>(); |
| 102 | AccessController.doPrivileged( |
| 103 | new PrivilegedExceptionAction<Object>() { |
| 104 | public Object run() throws Exception { |
| 105 | props.put("keyStore", System.getProperty( |
| 106 | "javax.net.ssl.keyStore", "")); |
| 107 | props.put("keyStoreType", System.getProperty( |
| 108 | "javax.net.ssl.keyStoreType", |
| 109 | KeyStore.getDefaultType())); |
| 110 | props.put("keyStoreProvider", System.getProperty( |
| 111 | "javax.net.ssl.keyStoreProvider", "")); |
| 112 | props.put("keyStorePasswd", System.getProperty( |
| 113 | "javax.net.ssl.keyStorePassword", "")); |
| 114 | return null; |
| 115 | } |
| 116 | }); |
| 117 | |
| 118 | final String defaultKeyStore = props.get("keyStore"); |
| 119 | String defaultKeyStoreType = props.get("keyStoreType"); |
| 120 | String defaultKeyStoreProvider = props.get("keyStoreProvider"); |
| 121 | if (debug != null && Debug.isOn("defaultctx")) { |
| 122 | System.out.println("keyStore is : " + defaultKeyStore); |
| 123 | System.out.println("keyStore type is : " + |
| 124 | defaultKeyStoreType); |
| 125 | System.out.println("keyStore provider is : " + |
| 126 | defaultKeyStoreProvider); |
| 127 | } |
| 128 | |
| 129 | if (P11KEYSTORE.equals(defaultKeyStoreType) && |
| 130 | !NONE.equals(defaultKeyStore)) { |
| 131 | throw new IllegalArgumentException("if keyStoreType is " |
| 132 | + P11KEYSTORE + ", then keyStore must be " + NONE); |
| 133 | } |
| 134 | |
| 135 | FileInputStream fs = null; |
| 136 | if (defaultKeyStore.length() != 0 && !NONE.equals(defaultKeyStore)) { |
| 137 | fs = AccessController.doPrivileged( |
| 138 | new PrivilegedExceptionAction<FileInputStream>() { |
| 139 | public FileInputStream run() throws Exception { |
| 140 | return new FileInputStream(defaultKeyStore); |
| 141 | } |
| 142 | }); |
| 143 | } |
| 144 | |
| 145 | String defaultKeyStorePassword = props.get("keyStorePasswd"); |
| 146 | char[] passwd = null; |
| 147 | if (defaultKeyStorePassword.length() != 0) { |
| 148 | passwd = defaultKeyStorePassword.toCharArray(); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Try to initialize key store. |
| 153 | */ |
| 154 | KeyStore ks = null; |
| 155 | if ((defaultKeyStoreType.length()) != 0) { |
| 156 | if (debug != null && Debug.isOn("defaultctx")) { |
| 157 | System.out.println("init keystore"); |
| 158 | } |
| 159 | if (defaultKeyStoreProvider.length() == 0) { |
| 160 | ks = KeyStore.getInstance(defaultKeyStoreType); |
| 161 | } else { |
| 162 | ks = KeyStore.getInstance(defaultKeyStoreType, |
| 163 | defaultKeyStoreProvider); |
| 164 | } |
| 165 | |
| 166 | // if defaultKeyStore is NONE, fs will be null |
| 167 | ks.load(fs, passwd); |
| 168 | } |
| 169 | if (fs != null) { |
| 170 | fs.close(); |
| 171 | fs = null; |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * Try to initialize key manager. |
| 176 | */ |
| 177 | if (debug != null && Debug.isOn("defaultctx")) { |
| 178 | System.out.println("init keymanager of type " + |
| 179 | KeyManagerFactory.getDefaultAlgorithm()); |
| 180 | } |
| 181 | KeyManagerFactory kmf = KeyManagerFactory.getInstance( |
| 182 | KeyManagerFactory.getDefaultAlgorithm()); |
| 183 | |
| 184 | if (P11KEYSTORE.equals(defaultKeyStoreType)) { |
| 185 | kmf.init(ks, null); // do not pass key passwd if using token |
| 186 | } else { |
| 187 | kmf.init(ks, passwd); |
| 188 | } |
| 189 | |
| 190 | defaultKeyManagers = kmf.getKeyManagers(); |
| 191 | return defaultKeyManagers; |
| 192 | } |
| 193 | } |