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