author | mullan |
Mon, 20 Mar 2017 07:38:52 -0400 | |
changeset 44760 | 61b03b960583 |
parent 43009 | 5af9f7aa93e5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
44760 | 2 |
* Copyright (c) 1997, 2017, 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 |
||
27 |
package sun.security.ssl; |
|
28 |
||
7043 | 29 |
import java.net.Socket; |
30 |
import javax.net.ssl.SSLSession; |
|
31 |
||
2 | 32 |
import java.util.*; |
33 |
import java.security.*; |
|
34 |
import java.security.cert.*; |
|
35 |
import javax.net.ssl.*; |
|
36 |
||
37 |
import sun.security.validator.*; |
|
44760 | 38 |
import sun.security.util.AnchorCertificates; |
2 | 39 |
import sun.security.util.HostnameChecker; |
40 |
||
41 |
/** |
|
42 |
* This class implements the SunJSSE X.509 trust manager using the internal |
|
43 |
* validator API in J2SE core. The logic in this class is minimal.<p> |
|
7043 | 44 |
* <p> |
2 | 45 |
* This class supports both the Simple validation algorithm from previous |
46 |
* JSSE versions and PKIX validation. Currently, it is not possible for the |
|
47 |
* application to specify PKIX parameters other than trust anchors. This will |
|
48 |
* be fixed in a future release using new APIs. When that happens, it may also |
|
49 |
* make sense to separate the Simple and PKIX trust managers into separate |
|
50 |
* classes. |
|
51 |
* |
|
52 |
* @author Andreas Sterbenz |
|
53 |
*/ |
|
54 |
final class X509TrustManagerImpl extends X509ExtendedTrustManager |
|
55 |
implements X509TrustManager { |
|
56 |
||
57 |
private final String validatorType; |
|
58 |
||
59 |
/** |
|
60 |
* The Set of trusted X509Certificates. |
|
61 |
*/ |
|
62 |
private final Collection<X509Certificate> trustedCerts; |
|
63 |
||
64 |
private final PKIXBuilderParameters pkixParams; |
|
65 |
||
66 |
// note that we need separate validator for client and server due to |
|
67 |
// the different extension checks. They are initialized lazily on demand. |
|
68 |
private volatile Validator clientValidator, serverValidator; |
|
69 |
||
70 |
private static final Debug debug = Debug.getInstance("ssl"); |
|
71 |
||
43009
5af9f7aa93e5
8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
32032
diff
changeset
|
72 |
X509TrustManagerImpl(String validatorType, |
5af9f7aa93e5
8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
32032
diff
changeset
|
73 |
Collection<X509Certificate> trustedCerts) { |
5af9f7aa93e5
8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
32032
diff
changeset
|
74 |
|
2 | 75 |
this.validatorType = validatorType; |
76 |
this.pkixParams = null; |
|
43009
5af9f7aa93e5
8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
32032
diff
changeset
|
77 |
|
5af9f7aa93e5
8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
32032
diff
changeset
|
78 |
if (trustedCerts == null) { |
2 | 79 |
trustedCerts = Collections.<X509Certificate>emptySet(); |
80 |
} |
|
43009
5af9f7aa93e5
8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
32032
diff
changeset
|
81 |
|
5af9f7aa93e5
8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
32032
diff
changeset
|
82 |
this.trustedCerts = trustedCerts; |
5af9f7aa93e5
8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
32032
diff
changeset
|
83 |
|
5af9f7aa93e5
8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
32032
diff
changeset
|
84 |
if (debug != null && Debug.isOn("trustmanager")) { |
5af9f7aa93e5
8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
32032
diff
changeset
|
85 |
showTrustedCerts(); |
5af9f7aa93e5
8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
32032
diff
changeset
|
86 |
} |
2 | 87 |
} |
88 |
||
89 |
X509TrustManagerImpl(String validatorType, PKIXBuilderParameters params) { |
|
90 |
this.validatorType = validatorType; |
|
91 |
this.pkixParams = params; |
|
92 |
// create server validator eagerly so that we can conveniently |
|
93 |
// get the trusted certificates |
|
94 |
// clients need it anyway eventually, and servers will not mind |
|
95 |
// the little extra footprint |
|
96 |
Validator v = getValidator(Validator.VAR_TLS_SERVER); |
|
97 |
trustedCerts = v.getTrustedCertificates(); |
|
98 |
serverValidator = v; |
|
43009
5af9f7aa93e5
8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
32032
diff
changeset
|
99 |
|
5af9f7aa93e5
8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
32032
diff
changeset
|
100 |
if (debug != null && Debug.isOn("trustmanager")) { |
5af9f7aa93e5
8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
32032
diff
changeset
|
101 |
showTrustedCerts(); |
5af9f7aa93e5
8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
32032
diff
changeset
|
102 |
} |
2 | 103 |
} |
104 |
||
7043 | 105 |
@Override |
31538
0981099a3e54
8130022: Use Java-style array declarations consistently
igerasim
parents:
30904
diff
changeset
|
106 |
public void checkClientTrusted(X509Certificate[] chain, String authType) |
7043 | 107 |
throws CertificateException { |
108 |
checkTrusted(chain, authType, (Socket)null, true); |
|
109 |
} |
|
110 |
||
111 |
@Override |
|
31538
0981099a3e54
8130022: Use Java-style array declarations consistently
igerasim
parents:
30904
diff
changeset
|
112 |
public void checkServerTrusted(X509Certificate[] chain, String authType) |
7043 | 113 |
throws CertificateException { |
114 |
checkTrusted(chain, authType, (Socket)null, false); |
|
115 |
} |
|
116 |
||
117 |
@Override |
|
118 |
public X509Certificate[] getAcceptedIssuers() { |
|
119 |
X509Certificate[] certsArray = new X509Certificate[trustedCerts.size()]; |
|
120 |
trustedCerts.toArray(certsArray); |
|
121 |
return certsArray; |
|
122 |
} |
|
123 |
||
124 |
@Override |
|
125 |
public void checkClientTrusted(X509Certificate[] chain, String authType, |
|
126 |
Socket socket) throws CertificateException { |
|
127 |
checkTrusted(chain, authType, socket, true); |
|
128 |
} |
|
129 |
||
130 |
@Override |
|
131 |
public void checkServerTrusted(X509Certificate[] chain, String authType, |
|
132 |
Socket socket) throws CertificateException { |
|
133 |
checkTrusted(chain, authType, socket, false); |
|
134 |
} |
|
135 |
||
136 |
@Override |
|
137 |
public void checkClientTrusted(X509Certificate[] chain, String authType, |
|
138 |
SSLEngine engine) throws CertificateException { |
|
139 |
checkTrusted(chain, authType, engine, true); |
|
140 |
} |
|
141 |
||
142 |
@Override |
|
143 |
public void checkServerTrusted(X509Certificate[] chain, String authType, |
|
144 |
SSLEngine engine) throws CertificateException { |
|
145 |
checkTrusted(chain, authType, engine, false); |
|
146 |
} |
|
147 |
||
148 |
private Validator checkTrustedInit(X509Certificate[] chain, |
|
149 |
String authType, boolean isClient) { |
|
150 |
if (chain == null || chain.length == 0) { |
|
151 |
throw new IllegalArgumentException( |
|
152 |
"null or zero-length certificate chain"); |
|
153 |
} |
|
154 |
||
155 |
if (authType == null || authType.length() == 0) { |
|
156 |
throw new IllegalArgumentException( |
|
157 |
"null or zero-length authentication type"); |
|
158 |
} |
|
159 |
||
160 |
Validator v = null; |
|
161 |
if (isClient) { |
|
162 |
v = clientValidator; |
|
163 |
if (v == null) { |
|
164 |
synchronized (this) { |
|
165 |
v = clientValidator; |
|
166 |
if (v == null) { |
|
167 |
v = getValidator(Validator.VAR_TLS_CLIENT); |
|
168 |
clientValidator = v; |
|
169 |
} |
|
170 |
} |
|
171 |
} |
|
172 |
} else { |
|
173 |
// assume double checked locking with a volatile flag works |
|
174 |
// (guaranteed under the new Tiger memory model) |
|
175 |
v = serverValidator; |
|
176 |
if (v == null) { |
|
177 |
synchronized (this) { |
|
178 |
v = serverValidator; |
|
179 |
if (v == null) { |
|
180 |
v = getValidator(Validator.VAR_TLS_SERVER); |
|
181 |
serverValidator = v; |
|
182 |
} |
|
183 |
} |
|
184 |
} |
|
185 |
} |
|
186 |
||
187 |
return v; |
|
188 |
} |
|
189 |
||
190 |
private void checkTrusted(X509Certificate[] chain, String authType, |
|
191 |
Socket socket, boolean isClient) throws CertificateException { |
|
192 |
Validator v = checkTrustedInit(chain, authType, isClient); |
|
193 |
||
44760 | 194 |
X509Certificate[] trustedChain = null; |
7043 | 195 |
if ((socket != null) && socket.isConnected() && |
196 |
(socket instanceof SSLSocket)) { |
|
197 |
||
198 |
SSLSocket sslSocket = (SSLSocket)socket; |
|
199 |
SSLSession session = sslSocket.getHandshakeSession(); |
|
200 |
if (session == null) { |
|
201 |
throw new CertificateException("No handshake session"); |
|
202 |
} |
|
203 |
||
204 |
// create the algorithm constraints |
|
205 |
ProtocolVersion protocolVersion = |
|
206 |
ProtocolVersion.valueOf(session.getProtocol()); |
|
44760 | 207 |
boolean isExtSession = (session instanceof ExtendedSSLSession); |
208 |
AlgorithmConstraints constraints = null; |
|
209 |
if (protocolVersion.v >= ProtocolVersion.TLS12.v && isExtSession) { |
|
210 |
ExtendedSSLSession extSession = (ExtendedSSLSession)session; |
|
211 |
String[] localSupportedSignAlgs = |
|
212 |
extSession.getLocalSupportedSignatureAlgorithms(); |
|
7043 | 213 |
|
44760 | 214 |
constraints = new SSLAlgorithmConstraints( |
215 |
sslSocket, localSupportedSignAlgs, false); |
|
7043 | 216 |
} else { |
217 |
constraints = new SSLAlgorithmConstraints(sslSocket, false); |
|
218 |
} |
|
32032 | 219 |
|
220 |
// Grab any stapled OCSP responses for use in validation |
|
44760 | 221 |
List<byte[]> responseList = Collections.emptyList(); |
222 |
if (!isClient && isExtSession) { |
|
32032 | 223 |
responseList = |
224 |
((ExtendedSSLSession)session).getStatusResponses(); |
|
225 |
} |
|
44760 | 226 |
trustedChain = validate(v, chain, responseList, |
227 |
constraints, isClient ? null : authType); |
|
228 |
||
229 |
// check if EE certificate chains to a public root CA (as |
|
230 |
// pre-installed in cacerts) |
|
231 |
boolean chainsToPublicCA = |
|
232 |
AnchorCertificates.contains(trustedChain[trustedChain.length-1]); |
|
7043 | 233 |
|
44760 | 234 |
// check endpoint identity |
235 |
String identityAlg = sslSocket.getSSLParameters(). |
|
236 |
getEndpointIdentificationAlgorithm(); |
|
237 |
if (identityAlg != null && identityAlg.length() != 0) { |
|
238 |
checkIdentity(session, trustedChain[0], identityAlg, isClient, |
|
239 |
getRequestedServerNames(socket), chainsToPublicCA); |
|
240 |
} |
|
241 |
} else { |
|
32032 | 242 |
trustedChain = validate(v, chain, Collections.emptyList(), |
44760 | 243 |
null, isClient ? null : authType); |
7043 | 244 |
} |
245 |
if (debug != null && Debug.isOn("trustmanager")) { |
|
246 |
System.out.println("Found trusted certificate:"); |
|
247 |
System.out.println(trustedChain[trustedChain.length - 1]); |
|
248 |
} |
|
249 |
} |
|
250 |
||
251 |
private void checkTrusted(X509Certificate[] chain, String authType, |
|
252 |
SSLEngine engine, boolean isClient) throws CertificateException { |
|
253 |
Validator v = checkTrustedInit(chain, authType, isClient); |
|
254 |
||
44760 | 255 |
X509Certificate[] trustedChain = null; |
7043 | 256 |
if (engine != null) { |
257 |
SSLSession session = engine.getHandshakeSession(); |
|
258 |
if (session == null) { |
|
259 |
throw new CertificateException("No handshake session"); |
|
260 |
} |
|
261 |
||
262 |
// create the algorithm constraints |
|
263 |
ProtocolVersion protocolVersion = |
|
264 |
ProtocolVersion.valueOf(session.getProtocol()); |
|
44760 | 265 |
boolean isExtSession = (session instanceof ExtendedSSLSession); |
266 |
AlgorithmConstraints constraints = null; |
|
267 |
if (protocolVersion.v >= ProtocolVersion.TLS12.v && isExtSession) { |
|
268 |
ExtendedSSLSession extSession = (ExtendedSSLSession)session; |
|
269 |
String[] localSupportedSignAlgs = |
|
270 |
extSession.getLocalSupportedSignatureAlgorithms(); |
|
7043 | 271 |
|
44760 | 272 |
constraints = new SSLAlgorithmConstraints( |
273 |
engine, localSupportedSignAlgs, false); |
|
7043 | 274 |
} else { |
275 |
constraints = new SSLAlgorithmConstraints(engine, false); |
|
276 |
} |
|
32032 | 277 |
|
278 |
// Grab any stapled OCSP responses for use in validation |
|
44760 | 279 |
List<byte[]> responseList = Collections.emptyList(); |
280 |
if (!isClient && isExtSession) { |
|
32032 | 281 |
responseList = |
282 |
((ExtendedSSLSession)session).getStatusResponses(); |
|
283 |
} |
|
44760 | 284 |
trustedChain = validate(v, chain, responseList, |
285 |
constraints, isClient ? null : authType); |
|
286 |
||
287 |
// check if EE certificate chains to a public root CA (as |
|
288 |
// pre-installed in cacerts) |
|
289 |
boolean chainsToPublicCA = |
|
290 |
AnchorCertificates.contains(trustedChain[trustedChain.length-1]); |
|
7043 | 291 |
|
44760 | 292 |
// check endpoint identity |
293 |
String identityAlg = engine.getSSLParameters(). |
|
294 |
getEndpointIdentificationAlgorithm(); |
|
295 |
if (identityAlg != null && identityAlg.length() != 0) { |
|
296 |
checkIdentity(session, trustedChain[0], identityAlg, isClient, |
|
297 |
getRequestedServerNames(engine), chainsToPublicCA); |
|
298 |
} |
|
299 |
} else { |
|
32032 | 300 |
trustedChain = validate(v, chain, Collections.emptyList(), |
44760 | 301 |
null, isClient ? null : authType); |
7043 | 302 |
} |
303 |
if (debug != null && Debug.isOn("trustmanager")) { |
|
304 |
System.out.println("Found trusted certificate:"); |
|
305 |
System.out.println(trustedChain[trustedChain.length - 1]); |
|
306 |
} |
|
307 |
} |
|
308 |
||
2 | 309 |
private void showTrustedCerts() { |
43009
5af9f7aa93e5
8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
32032
diff
changeset
|
310 |
for (X509Certificate cert : trustedCerts) { |
5af9f7aa93e5
8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
32032
diff
changeset
|
311 |
System.out.println("adding as trusted cert:"); |
5af9f7aa93e5
8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
32032
diff
changeset
|
312 |
System.out.println(" Subject: " |
5af9f7aa93e5
8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
32032
diff
changeset
|
313 |
+ cert.getSubjectX500Principal()); |
5af9f7aa93e5
8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
32032
diff
changeset
|
314 |
System.out.println(" Issuer: " |
5af9f7aa93e5
8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
32032
diff
changeset
|
315 |
+ cert.getIssuerX500Principal()); |
5af9f7aa93e5
8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
32032
diff
changeset
|
316 |
System.out.println(" Algorithm: " |
5af9f7aa93e5
8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
32032
diff
changeset
|
317 |
+ cert.getPublicKey().getAlgorithm() |
5af9f7aa93e5
8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
32032
diff
changeset
|
318 |
+ "; Serial number: 0x" |
5af9f7aa93e5
8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
32032
diff
changeset
|
319 |
+ cert.getSerialNumber().toString(16)); |
5af9f7aa93e5
8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
32032
diff
changeset
|
320 |
System.out.println(" Valid from " |
5af9f7aa93e5
8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
32032
diff
changeset
|
321 |
+ cert.getNotBefore() + " until " |
5af9f7aa93e5
8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
32032
diff
changeset
|
322 |
+ cert.getNotAfter()); |
5af9f7aa93e5
8129988: JSSE should create a single instance of the cacerts KeyStore
xuelei
parents:
32032
diff
changeset
|
323 |
System.out.println(); |
2 | 324 |
} |
325 |
} |
|
326 |
||
327 |
private Validator getValidator(String variant) { |
|
328 |
Validator v; |
|
329 |
if (pkixParams == null) { |
|
330 |
v = Validator.getInstance(validatorType, variant, trustedCerts); |
|
331 |
} else { |
|
332 |
v = Validator.getInstance(validatorType, variant, pkixParams); |
|
333 |
} |
|
334 |
return v; |
|
335 |
} |
|
336 |
||
337 |
private static X509Certificate[] validate(Validator v, |
|
32032 | 338 |
X509Certificate[] chain, List<byte[]> responseList, |
339 |
AlgorithmConstraints constraints, String authType) |
|
340 |
throws CertificateException { |
|
2 | 341 |
Object o = JsseJce.beginFipsProvider(); |
342 |
try { |
|
32032 | 343 |
return v.validate(chain, null, responseList, constraints, authType); |
2 | 344 |
} finally { |
345 |
JsseJce.endFipsProvider(o); |
|
346 |
} |
|
347 |
} |
|
348 |
||
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
349 |
// Get string representation of HostName from a list of server names. |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
350 |
// |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
351 |
// We are only accepting host_name name type in the list. |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
352 |
private static String getHostNameInSNI(List<SNIServerName> sniNames) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
353 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
354 |
SNIHostName hostname = null; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
355 |
for (SNIServerName sniName : sniNames) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
356 |
if (sniName.getType() != StandardConstants.SNI_HOST_NAME) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
357 |
continue; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
358 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
359 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
360 |
if (sniName instanceof SNIHostName) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
361 |
hostname = (SNIHostName)sniName; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
362 |
} else { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
363 |
try { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
364 |
hostname = new SNIHostName(sniName.getEncoded()); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
365 |
} catch (IllegalArgumentException iae) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
366 |
// unlikely to happen, just in case ... |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
367 |
if ((debug != null) && Debug.isOn("trustmanager")) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
368 |
System.out.println("Illegal server name: " + sniName); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
369 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
370 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
371 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
372 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
373 |
// no more than server name of the same name type |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
374 |
break; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
375 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
376 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
377 |
if (hostname != null) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
378 |
return hostname.getAsciiName(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
379 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
380 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
381 |
return null; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
382 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
383 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
384 |
// Also used by X509KeyManagerImpl |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
385 |
static List<SNIServerName> getRequestedServerNames(Socket socket) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
386 |
if (socket != null && socket.isConnected() && |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
387 |
socket instanceof SSLSocket) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
388 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
389 |
SSLSocket sslSocket = (SSLSocket)socket; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
390 |
SSLSession session = sslSocket.getHandshakeSession(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
391 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
392 |
if (session != null && (session instanceof ExtendedSSLSession)) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
393 |
ExtendedSSLSession extSession = (ExtendedSSLSession)session; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
394 |
return extSession.getRequestedServerNames(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
395 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
396 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
397 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
398 |
return Collections.<SNIServerName>emptyList(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
399 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
400 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
401 |
// Also used by X509KeyManagerImpl |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
402 |
static List<SNIServerName> getRequestedServerNames(SSLEngine engine) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
403 |
if (engine != null) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
404 |
SSLSession session = engine.getHandshakeSession(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
405 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
406 |
if (session != null && (session instanceof ExtendedSSLSession)) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
407 |
ExtendedSSLSession extSession = (ExtendedSSLSession)session; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
408 |
return extSession.getRequestedServerNames(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
409 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
410 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
411 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
412 |
return Collections.<SNIServerName>emptyList(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
413 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
414 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
415 |
/* |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
416 |
* Per RFC 6066, if an application negotiates a server name using an |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
417 |
* application protocol and then upgrades to TLS, and if a server_name |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
418 |
* extension is sent, then the extension SHOULD contain the same name |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
419 |
* that was negotiated in the application protocol. If the server_name |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
420 |
* is established in the TLS session handshake, the client SHOULD NOT |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
421 |
* attempt to request a different server name at the application layer. |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
422 |
* |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
423 |
* According to the above spec, we only need to check either the identity |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
424 |
* in server_name extension or the peer host of the connection. Peer host |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
425 |
* is not always a reliable fully qualified domain name. The HostName in |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
426 |
* server_name extension is more reliable than peer host. So we prefer |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
427 |
* the identity checking aginst the server_name extension if present, and |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
428 |
* may failove to peer host checking. |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
429 |
*/ |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
430 |
private static void checkIdentity(SSLSession session, |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
431 |
X509Certificate cert, |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
432 |
String algorithm, |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
433 |
boolean isClient, |
44760 | 434 |
List<SNIServerName> sniNames, |
435 |
boolean chainsToPublicCA) throws CertificateException { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
436 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
437 |
boolean identifiable = false; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
438 |
String peerHost = session.getPeerHost(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
439 |
if (isClient) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
440 |
String hostname = getHostNameInSNI(sniNames); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
441 |
if (hostname != null) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
442 |
try { |
44760 | 443 |
checkIdentity(hostname, cert, algorithm, chainsToPublicCA); |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
444 |
identifiable = true; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
445 |
} catch (CertificateException ce) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
446 |
if (hostname.equalsIgnoreCase(peerHost)) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
447 |
throw ce; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
448 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
449 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
450 |
// otherwisw, failover to check peer host |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
451 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
452 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
453 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
454 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
455 |
if (!identifiable) { |
44760 | 456 |
checkIdentity(peerHost, cert, algorithm, chainsToPublicCA); |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
457 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
458 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
7043
diff
changeset
|
459 |
|
7043 | 460 |
/* |
461 |
* Identify the peer by its certificate and hostname. |
|
2 | 462 |
* |
7043 | 463 |
* Lifted from sun.net.www.protocol.https.HttpsClient. |
2 | 464 |
*/ |
7043 | 465 |
static void checkIdentity(String hostname, X509Certificate cert, |
466 |
String algorithm) throws CertificateException { |
|
44760 | 467 |
checkIdentity(hostname, cert, algorithm, false); |
468 |
} |
|
469 |
||
470 |
private static void checkIdentity(String hostname, X509Certificate cert, |
|
471 |
String algorithm, boolean chainsToPublicCA) |
|
472 |
throws CertificateException { |
|
2 | 473 |
if (algorithm != null && algorithm.length() != 0) { |
474 |
// if IPv6 strip off the "[]" |
|
7043 | 475 |
if ((hostname != null) && hostname.startsWith("[") && |
476 |
hostname.endsWith("]")) { |
|
477 |
hostname = hostname.substring(1, hostname.length() - 1); |
|
2 | 478 |
} |
479 |
||
480 |
if (algorithm.equalsIgnoreCase("HTTPS")) { |
|
481 |
HostnameChecker.getInstance(HostnameChecker.TYPE_TLS).match( |
|
44760 | 482 |
hostname, cert, chainsToPublicCA); |
7043 | 483 |
} else if (algorithm.equalsIgnoreCase("LDAP") || |
484 |
algorithm.equalsIgnoreCase("LDAPS")) { |
|
2 | 485 |
HostnameChecker.getInstance(HostnameChecker.TYPE_LDAP).match( |
44760 | 486 |
hostname, cert, chainsToPublicCA); |
2 | 487 |
} else { |
488 |
throw new CertificateException( |
|
489 |
"Unknown identification algorithm: " + algorithm); |
|
490 |
} |
|
491 |
} |
|
492 |
} |
|
493 |
} |