author | xuelei |
Sat, 24 Nov 2012 04:09:19 -0800 | |
changeset 14664 | e71aa0962e70 |
parent 10125 | c70d99150c40 |
child 23010 | 6dadb192ad81 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
7990
diff
changeset
|
2 |
* Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.ssl; |
|
27 |
||
28 |
import java.util.*; |
|
29 |
import java.io.*; |
|
30 |
import java.security.*; |
|
31 |
import java.security.cert.*; |
|
32 |
import javax.net.ssl.*; |
|
33 |
||
34 |
import sun.security.validator.Validator; |
|
35 |
||
36 |
abstract class TrustManagerFactoryImpl extends TrustManagerFactorySpi { |
|
37 |
||
38 |
private static final Debug debug = Debug.getInstance("ssl"); |
|
39 |
private X509TrustManager trustManager = null; |
|
40 |
private boolean isInitialized = false; |
|
41 |
||
42 |
TrustManagerFactoryImpl() { |
|
43 |
// empty |
|
44 |
} |
|
45 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
10125
diff
changeset
|
46 |
@Override |
2 | 47 |
protected void engineInit(KeyStore ks) throws KeyStoreException { |
48 |
if (ks == null) { |
|
49 |
try { |
|
50 |
ks = getCacertsKeyStore("trustmanager"); |
|
51 |
} catch (SecurityException se) { |
|
52 |
// eat security exceptions but report other throwables |
|
53 |
if (debug != null && Debug.isOn("trustmanager")) { |
|
54 |
System.out.println( |
|
55 |
"SunX509: skip default keystore: " + se); |
|
56 |
} |
|
57 |
} catch (Error err) { |
|
58 |
if (debug != null && Debug.isOn("trustmanager")) { |
|
59 |
System.out.println( |
|
60 |
"SunX509: skip default keystore: " + err); |
|
61 |
} |
|
62 |
throw err; |
|
63 |
} catch (RuntimeException re) { |
|
64 |
if (debug != null && Debug.isOn("trustmanager")) { |
|
65 |
System.out.println( |
|
66 |
"SunX509: skip default keystore: " + re); |
|
67 |
} |
|
68 |
throw re; |
|
69 |
} catch (Exception e) { |
|
70 |
if (debug != null && Debug.isOn("trustmanager")) { |
|
71 |
System.out.println( |
|
72 |
"SunX509: skip default keystore: " + e); |
|
73 |
} |
|
74 |
throw new KeyStoreException( |
|
75 |
"problem accessing trust store" + e); |
|
76 |
} |
|
77 |
} |
|
78 |
trustManager = getInstance(ks); |
|
79 |
isInitialized = true; |
|
80 |
} |
|
81 |
||
82 |
abstract X509TrustManager getInstance(KeyStore ks) throws KeyStoreException; |
|
83 |
||
84 |
abstract X509TrustManager getInstance(ManagerFactoryParameters spec) |
|
85 |
throws InvalidAlgorithmParameterException; |
|
86 |
||
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
10125
diff
changeset
|
87 |
@Override |
2 | 88 |
protected void engineInit(ManagerFactoryParameters spec) throws |
89 |
InvalidAlgorithmParameterException { |
|
90 |
trustManager = getInstance(spec); |
|
91 |
isInitialized = true; |
|
92 |
} |
|
93 |
||
94 |
/** |
|
95 |
* Returns one trust manager for each type of trust material. |
|
96 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
10125
diff
changeset
|
97 |
@Override |
2 | 98 |
protected TrustManager[] engineGetTrustManagers() { |
99 |
if (!isInitialized) { |
|
100 |
throw new IllegalStateException( |
|
101 |
"TrustManagerFactoryImpl is not initialized"); |
|
102 |
} |
|
103 |
return new TrustManager[] { trustManager }; |
|
104 |
} |
|
105 |
||
106 |
/* |
|
107 |
* Try to get an InputStream based on the file we pass in. |
|
108 |
*/ |
|
109 |
private static FileInputStream getFileInputStream(final File file) |
|
110 |
throws Exception { |
|
111 |
return AccessController.doPrivileged( |
|
112 |
new PrivilegedExceptionAction<FileInputStream>() { |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
10125
diff
changeset
|
113 |
@Override |
2 | 114 |
public FileInputStream run() throws Exception { |
115 |
try { |
|
116 |
if (file.exists()) { |
|
117 |
return new FileInputStream(file); |
|
118 |
} else { |
|
119 |
return null; |
|
120 |
} |
|
121 |
} catch (FileNotFoundException e) { |
|
122 |
// couldn't find it, oh well. |
|
123 |
return null; |
|
124 |
} |
|
125 |
} |
|
126 |
}); |
|
127 |
} |
|
128 |
||
129 |
/** |
|
130 |
* Returns the keystore with the configured CA certificates. |
|
131 |
*/ |
|
132 |
static KeyStore getCacertsKeyStore(String dbgname) throws Exception |
|
133 |
{ |
|
134 |
String storeFileName = null; |
|
135 |
File storeFile = null; |
|
136 |
FileInputStream fis = null; |
|
137 |
String defaultTrustStoreType; |
|
138 |
String defaultTrustStoreProvider; |
|
7990 | 139 |
final HashMap<String,String> props = new HashMap<>(); |
2 | 140 |
final String sep = File.separator; |
141 |
KeyStore ks = null; |
|
142 |
||
143 |
AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() { |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
10125
diff
changeset
|
144 |
@Override |
2 | 145 |
public Void run() throws Exception { |
146 |
props.put("trustStore", System.getProperty( |
|
147 |
"javax.net.ssl.trustStore")); |
|
148 |
props.put("javaHome", System.getProperty( |
|
149 |
"java.home")); |
|
150 |
props.put("trustStoreType", System.getProperty( |
|
151 |
"javax.net.ssl.trustStoreType", |
|
152 |
KeyStore.getDefaultType())); |
|
153 |
props.put("trustStoreProvider", System.getProperty( |
|
154 |
"javax.net.ssl.trustStoreProvider", "")); |
|
155 |
props.put("trustStorePasswd", System.getProperty( |
|
156 |
"javax.net.ssl.trustStorePassword", "")); |
|
157 |
return null; |
|
158 |
} |
|
159 |
}); |
|
160 |
||
161 |
/* |
|
162 |
* Try: |
|
163 |
* javax.net.ssl.trustStore (if this variable exists, stop) |
|
164 |
* jssecacerts |
|
165 |
* cacerts |
|
166 |
* |
|
167 |
* If none exists, we use an empty keystore. |
|
168 |
*/ |
|
169 |
||
10125 | 170 |
try { |
171 |
storeFileName = props.get("trustStore"); |
|
172 |
if (!"NONE".equals(storeFileName)) { |
|
173 |
if (storeFileName != null) { |
|
174 |
storeFile = new File(storeFileName); |
|
175 |
fis = getFileInputStream(storeFile); |
|
176 |
} else { |
|
177 |
String javaHome = props.get("javaHome"); |
|
2 | 178 |
storeFile = new File(javaHome + sep + "lib" + sep |
10125 | 179 |
+ "security" + sep + |
180 |
"jssecacerts"); |
|
181 |
if ((fis = getFileInputStream(storeFile)) == null) { |
|
182 |
storeFile = new File(javaHome + sep + "lib" + sep |
|
183 |
+ "security" + sep + |
|
184 |
"cacerts"); |
|
185 |
fis = getFileInputStream(storeFile); |
|
186 |
} |
|
187 |
} |
|
188 |
||
189 |
if (fis != null) { |
|
190 |
storeFileName = storeFile.getPath(); |
|
191 |
} else { |
|
192 |
storeFileName = "No File Available, using empty keystore."; |
|
2 | 193 |
} |
194 |
} |
|
195 |
||
10125 | 196 |
defaultTrustStoreType = props.get("trustStoreType"); |
197 |
defaultTrustStoreProvider = props.get("trustStoreProvider"); |
|
198 |
if (debug != null && Debug.isOn(dbgname)) { |
|
199 |
System.out.println("trustStore is: " + storeFileName); |
|
200 |
System.out.println("trustStore type is : " + |
|
201 |
defaultTrustStoreType); |
|
202 |
System.out.println("trustStore provider is : " + |
|
203 |
defaultTrustStoreProvider); |
|
2 | 204 |
} |
205 |
||
10125 | 206 |
/* |
207 |
* Try to initialize trust store. |
|
208 |
*/ |
|
209 |
if (defaultTrustStoreType.length() != 0) { |
|
210 |
if (debug != null && Debug.isOn(dbgname)) { |
|
211 |
System.out.println("init truststore"); |
|
212 |
} |
|
213 |
if (defaultTrustStoreProvider.length() == 0) { |
|
214 |
ks = KeyStore.getInstance(defaultTrustStoreType); |
|
215 |
} else { |
|
216 |
ks = KeyStore.getInstance(defaultTrustStoreType, |
|
217 |
defaultTrustStoreProvider); |
|
218 |
} |
|
219 |
char[] passwd = null; |
|
220 |
String defaultTrustStorePassword = |
|
221 |
props.get("trustStorePasswd"); |
|
222 |
if (defaultTrustStorePassword.length() != 0) |
|
223 |
passwd = defaultTrustStorePassword.toCharArray(); |
|
2 | 224 |
|
10125 | 225 |
// if trustStore is NONE, fis will be null |
226 |
ks.load(fis, passwd); |
|
2 | 227 |
|
10125 | 228 |
// Zero out the temporary password storage |
229 |
if (passwd != null) { |
|
230 |
for (int i = 0; i < passwd.length; i++) { |
|
231 |
passwd[i] = (char)0; |
|
232 |
} |
|
2 | 233 |
} |
234 |
} |
|
10125 | 235 |
} finally { |
236 |
if (fis != null) { |
|
237 |
fis.close(); |
|
238 |
} |
|
2 | 239 |
} |
240 |
||
241 |
return ks; |
|
242 |
} |
|
243 |
||
244 |
public static final class SimpleFactory extends TrustManagerFactoryImpl { |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
10125
diff
changeset
|
245 |
@Override |
2 | 246 |
X509TrustManager getInstance(KeyStore ks) throws KeyStoreException { |
247 |
return new X509TrustManagerImpl(Validator.TYPE_SIMPLE, ks); |
|
248 |
} |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
10125
diff
changeset
|
249 |
@Override |
2 | 250 |
X509TrustManager getInstance(ManagerFactoryParameters spec) |
251 |
throws InvalidAlgorithmParameterException { |
|
252 |
throw new InvalidAlgorithmParameterException |
|
253 |
("SunX509 TrustManagerFactory does not use " |
|
254 |
+ "ManagerFactoryParameters"); |
|
255 |
} |
|
256 |
} |
|
257 |
||
258 |
public static final class PKIXFactory extends TrustManagerFactoryImpl { |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
10125
diff
changeset
|
259 |
@Override |
2 | 260 |
X509TrustManager getInstance(KeyStore ks) throws KeyStoreException { |
261 |
return new X509TrustManagerImpl(Validator.TYPE_PKIX, ks); |
|
262 |
} |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
10125
diff
changeset
|
263 |
@Override |
2 | 264 |
X509TrustManager getInstance(ManagerFactoryParameters spec) |
265 |
throws InvalidAlgorithmParameterException { |
|
266 |
if (spec instanceof CertPathTrustManagerParameters == false) { |
|
267 |
throw new InvalidAlgorithmParameterException |
|
268 |
("Parameters must be CertPathTrustManagerParameters"); |
|
269 |
} |
|
270 |
CertPathParameters params = |
|
271 |
((CertPathTrustManagerParameters)spec).getParameters(); |
|
272 |
if (params instanceof PKIXBuilderParameters == false) { |
|
273 |
throw new InvalidAlgorithmParameterException |
|
274 |
("Encapsulated parameters must be PKIXBuilderParameters"); |
|
275 |
} |
|
276 |
PKIXBuilderParameters pkixParams = (PKIXBuilderParameters)params; |
|
277 |
return new X509TrustManagerImpl(Validator.TYPE_PKIX, pkixParams); |
|
278 |
} |
|
279 |
} |
|
280 |
} |