author | xuelei |
Wed, 04 Mar 2015 08:10:23 +0000 | |
changeset 29266 | 5705356edc61 |
parent 29264 | 5172066a2da6 |
child 29390 | 9927a5ff3ded |
permissions | -rw-r--r-- |
2 | 1 |
/* |
29264
5172066a2da6
8054037: Improve tracing for java.security.debug=certpath
juh
parents:
28550
diff
changeset
|
2 |
* Copyright (c) 1996, 2015, 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.io.*; |
|
29 |
import java.math.BigInteger; |
|
30 |
import java.security.*; |
|
31 |
import java.util.*; |
|
32 |
||
33 |
import java.security.interfaces.ECPublicKey; |
|
703 | 34 |
import java.security.interfaces.RSAPublicKey; |
2 | 35 |
import java.security.spec.ECParameterSpec; |
36 |
||
37 |
import java.security.cert.X509Certificate; |
|
38 |
import java.security.cert.CertificateException; |
|
27068 | 39 |
import java.security.cert.CertificateParsingException; |
40 |
import javax.security.auth.x500.X500Principal; |
|
2 | 41 |
|
42 |
import javax.crypto.SecretKey; |
|
43 |
import javax.crypto.spec.SecretKeySpec; |
|
44 |
||
45 |
import javax.net.ssl.*; |
|
46 |
||
47 |
import javax.security.auth.Subject; |
|
48 |
||
49 |
import sun.security.ssl.HandshakeMessage.*; |
|
50 |
import static sun.security.ssl.CipherSuite.KeyExchange.*; |
|
51 |
||
52 |
/** |
|
53 |
* ClientHandshaker does the protocol handshaking from the point |
|
54 |
* of view of a client. It is driven asychronously by handshake messages |
|
55 |
* as delivered by the parent Handshaker class, and also uses |
|
56 |
* common functionality (e.g. key generation) that is provided there. |
|
57 |
* |
|
58 |
* @author David Brownell |
|
59 |
*/ |
|
60 |
final class ClientHandshaker extends Handshaker { |
|
61 |
||
29266 | 62 |
// constants for subject alt names of type DNS and IP |
63 |
private final static int ALTNAME_DNS = 2; |
|
64 |
private final static int ALTNAME_IP = 7; |
|
65 |
||
2 | 66 |
// the server's public key from its certificate. |
67 |
private PublicKey serverKey; |
|
68 |
||
69 |
// the server's ephemeral public key from the server key exchange message |
|
70 |
// for ECDHE/ECDH_anon and RSA_EXPORT. |
|
71 |
private PublicKey ephemeralServerKey; |
|
72 |
||
73 |
// server's ephemeral public value for DHE/DH_anon key exchanges |
|
74 |
private BigInteger serverDH; |
|
75 |
||
76 |
private DHCrypt dh; |
|
77 |
||
78 |
private ECDHCrypt ecdh; |
|
79 |
||
80 |
private CertificateRequest certRequest; |
|
81 |
||
82 |
private boolean serverKeyExchangeReceived; |
|
83 |
||
84 |
/* |
|
85 |
* The RSA PreMasterSecret needs to know the version of |
|
86 |
* ClientHello that was used on this handshake. This represents |
|
87 |
* the "max version" this client is supporting. In the |
|
88 |
* case of an initial handshake, it's the max version enabled, |
|
89 |
* but in the case of a resumption attempt, it's the version |
|
90 |
* of the session we're trying to resume. |
|
91 |
*/ |
|
92 |
private ProtocolVersion maxProtocolVersion; |
|
93 |
||
7043 | 94 |
// To switch off the SNI extension. |
95 |
private final static boolean enableSNIExtension = |
|
96 |
Debug.getBooleanProperty("jsse.enableSNIExtension", true); |
|
97 |
||
27068 | 98 |
/* |
99 |
* Allow unsafe server certificate change? |
|
100 |
* |
|
101 |
* Server certificate change during SSL/TLS renegotiation may be considered |
|
102 |
* unsafe, as described in the Triple Handshake attacks: |
|
103 |
* |
|
104 |
* https://secure-resumption.com/tlsauth.pdf |
|
105 |
* |
|
106 |
* Endpoint identification (See |
|
107 |
* SSLParameters.getEndpointIdentificationAlgorithm()) is a pretty nice |
|
108 |
* guarantee that the server certificate change in renegotiation is legal. |
|
109 |
* However, endpoing identification is only enabled for HTTPS and LDAP |
|
110 |
* over SSL/TLS by default. It is not enough to protect SSL/TLS |
|
111 |
* connections other than HTTPS and LDAP. |
|
112 |
* |
|
113 |
* The renegotiation indication extension (See RFC 5764) is a pretty |
|
114 |
* strong guarantee that the endpoints on both client and server sides |
|
115 |
* are identical on the same connection. However, the Triple Handshake |
|
116 |
* attacks can bypass this guarantee if there is a session-resumption |
|
117 |
* handshake between the initial full handshake and the renegotiation |
|
118 |
* full handshake. |
|
119 |
* |
|
120 |
* Server certificate change may be unsafe and should be restricted if |
|
121 |
* endpoint identification is not enabled and the previous handshake is |
|
122 |
* a session-resumption abbreviated initial handshake, unless the |
|
123 |
* identities represented by both certificates can be regraded as the |
|
124 |
* same (See isIdentityEquivalent()). |
|
125 |
* |
|
126 |
* Considering the compatibility impact and the actual requirements to |
|
127 |
* support server certificate change in practice, the system property, |
|
128 |
* jdk.tls.allowUnsafeServerCertChange, is used to define whether unsafe |
|
129 |
* server certificate change in renegotiation is allowed or not. The |
|
130 |
* default value of the system property is "false". To mitigate the |
|
131 |
* compactibility impact, applications may want to set the system |
|
132 |
* property to "true" at their own risk. |
|
133 |
* |
|
134 |
* If the value of the system property is "false", server certificate |
|
135 |
* change in renegotiation after a session-resumption abbreviated initial |
|
136 |
* handshake is restricted (See isIdentityEquivalent()). |
|
137 |
* |
|
138 |
* If the system property is set to "true" explicitly, the restriction on |
|
139 |
* server certificate change in renegotiation is disabled. |
|
140 |
*/ |
|
141 |
private final static boolean allowUnsafeServerCertChange = |
|
142 |
Debug.getBooleanProperty("jdk.tls.allowUnsafeServerCertChange", false); |
|
143 |
||
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
11521
diff
changeset
|
144 |
private List<SNIServerName> requestedServerNames = |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
11521
diff
changeset
|
145 |
Collections.<SNIServerName>emptyList(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
11521
diff
changeset
|
146 |
|
20499 | 147 |
private boolean serverNamesAccepted = false; |
148 |
||
2 | 149 |
/* |
27068 | 150 |
* the reserved server certificate chain in previous handshaking |
151 |
* |
|
152 |
* The server certificate chain is only reserved if the previous |
|
153 |
* handshake is a session-resumption abbreviated initial handshake. |
|
154 |
*/ |
|
155 |
private X509Certificate[] reservedServerCerts = null; |
|
156 |
||
157 |
/* |
|
2 | 158 |
* Constructors |
159 |
*/ |
|
160 |
ClientHandshaker(SSLSocketImpl socket, SSLContextImpl context, |
|
5182 | 161 |
ProtocolList enabledProtocols, |
6856 | 162 |
ProtocolVersion activeProtocolVersion, |
163 |
boolean isInitialHandshake, boolean secureRenegotiation, |
|
164 |
byte[] clientVerifyData, byte[] serverVerifyData) { |
|
165 |
||
166 |
super(socket, context, enabledProtocols, true, true, |
|
167 |
activeProtocolVersion, isInitialHandshake, secureRenegotiation, |
|
168 |
clientVerifyData, serverVerifyData); |
|
2 | 169 |
} |
170 |
||
171 |
ClientHandshaker(SSLEngineImpl engine, SSLContextImpl context, |
|
5182 | 172 |
ProtocolList enabledProtocols, |
6856 | 173 |
ProtocolVersion activeProtocolVersion, |
174 |
boolean isInitialHandshake, boolean secureRenegotiation, |
|
175 |
byte[] clientVerifyData, byte[] serverVerifyData) { |
|
176 |
||
177 |
super(engine, context, enabledProtocols, true, true, |
|
178 |
activeProtocolVersion, isInitialHandshake, secureRenegotiation, |
|
179 |
clientVerifyData, serverVerifyData); |
|
2 | 180 |
} |
181 |
||
182 |
/* |
|
183 |
* This routine handles all the client side handshake messages, one at |
|
184 |
* a time. Given the message type (and in some cases the pending cipher |
|
185 |
* spec) it parses the type-specific message. Then it calls a function |
|
186 |
* that handles that specific message. |
|
187 |
* |
|
188 |
* It updates the state machine (need to verify it) as each message |
|
189 |
* is processed, and writes responses as needed using the connection |
|
190 |
* in the constructor. |
|
191 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
192 |
@Override |
2 | 193 |
void processMessage(byte type, int messageLen) throws IOException { |
16071 | 194 |
if (state >= type |
195 |
&& (type != HandshakeMessage.ht_hello_request)) { |
|
2 | 196 |
throw new SSLProtocolException( |
197 |
"Handshake message sequence violation, " + type); |
|
198 |
} |
|
199 |
||
200 |
switch (type) { |
|
201 |
case HandshakeMessage.ht_hello_request: |
|
202 |
this.serverHelloRequest(new HelloRequest(input)); |
|
203 |
break; |
|
204 |
||
205 |
case HandshakeMessage.ht_server_hello: |
|
206 |
this.serverHello(new ServerHello(input, messageLen)); |
|
207 |
break; |
|
208 |
||
209 |
case HandshakeMessage.ht_certificate: |
|
210 |
if (keyExchange == K_DH_ANON || keyExchange == K_ECDH_ANON |
|
211 |
|| keyExchange == K_KRB5 || keyExchange == K_KRB5_EXPORT) { |
|
212 |
fatalSE(Alerts.alert_unexpected_message, |
|
213 |
"unexpected server cert chain"); |
|
214 |
// NOTREACHED |
|
215 |
} |
|
216 |
this.serverCertificate(new CertificateMsg(input)); |
|
217 |
serverKey = |
|
218 |
session.getPeerCertificates()[0].getPublicKey(); |
|
219 |
break; |
|
220 |
||
221 |
case HandshakeMessage.ht_server_key_exchange: |
|
222 |
serverKeyExchangeReceived = true; |
|
223 |
switch (keyExchange) { |
|
224 |
case K_RSA_EXPORT: |
|
703 | 225 |
/** |
226 |
* The server key exchange message is sent by the server only |
|
227 |
* when the server certificate message does not contain the |
|
228 |
* proper amount of data to allow the client to exchange a |
|
229 |
* premaster secret, such as when RSA_EXPORT is used and the |
|
230 |
* public key in the server certificate is longer than 512 bits. |
|
231 |
*/ |
|
232 |
if (serverKey == null) { |
|
233 |
throw new SSLProtocolException |
|
234 |
("Server did not send certificate message"); |
|
235 |
} |
|
236 |
||
237 |
if (!(serverKey instanceof RSAPublicKey)) { |
|
238 |
throw new SSLProtocolException("Protocol violation:" + |
|
239 |
" the certificate type must be appropriate for the" + |
|
240 |
" selected cipher suite's key exchange algorithm"); |
|
241 |
} |
|
242 |
||
243 |
if (JsseJce.getRSAKeyLength(serverKey) <= 512) { |
|
244 |
throw new SSLProtocolException("Protocol violation:" + |
|
245 |
" server sent a server key exchange message for" + |
|
246 |
" key exchange " + keyExchange + |
|
247 |
" when the public key in the server certificate" + |
|
248 |
" is less than or equal to 512 bits in length"); |
|
249 |
} |
|
250 |
||
2 | 251 |
try { |
252 |
this.serverKeyExchange(new RSA_ServerKeyExchange(input)); |
|
253 |
} catch (GeneralSecurityException e) { |
|
254 |
throwSSLException("Server key", e); |
|
255 |
} |
|
256 |
break; |
|
257 |
case K_DH_ANON: |
|
16080 | 258 |
try { |
259 |
this.serverKeyExchange(new DH_ServerKeyExchange( |
|
7043 | 260 |
input, protocolVersion)); |
16080 | 261 |
} catch (GeneralSecurityException e) { |
262 |
throwSSLException("Server key", e); |
|
263 |
} |
|
2 | 264 |
break; |
265 |
case K_DHE_DSS: |
|
266 |
case K_DHE_RSA: |
|
267 |
try { |
|
268 |
this.serverKeyExchange(new DH_ServerKeyExchange( |
|
269 |
input, serverKey, |
|
270 |
clnt_random.random_bytes, svr_random.random_bytes, |
|
7043 | 271 |
messageLen, |
272 |
localSupportedSignAlgs, protocolVersion)); |
|
2 | 273 |
} catch (GeneralSecurityException e) { |
274 |
throwSSLException("Server key", e); |
|
275 |
} |
|
276 |
break; |
|
277 |
case K_ECDHE_ECDSA: |
|
278 |
case K_ECDHE_RSA: |
|
279 |
case K_ECDH_ANON: |
|
280 |
try { |
|
281 |
this.serverKeyExchange(new ECDH_ServerKeyExchange |
|
282 |
(input, serverKey, clnt_random.random_bytes, |
|
7043 | 283 |
svr_random.random_bytes, |
284 |
localSupportedSignAlgs, protocolVersion)); |
|
2 | 285 |
} catch (GeneralSecurityException e) { |
286 |
throwSSLException("Server key", e); |
|
287 |
} |
|
288 |
break; |
|
703 | 289 |
case K_RSA: |
290 |
case K_DH_RSA: |
|
291 |
case K_DH_DSS: |
|
2 | 292 |
case K_ECDH_ECDSA: |
293 |
case K_ECDH_RSA: |
|
7043 | 294 |
throw new SSLProtocolException( |
295 |
"Protocol violation: server sent a server key exchange" |
|
296 |
+ "message for key exchange " + keyExchange); |
|
2 | 297 |
case K_KRB5: |
298 |
case K_KRB5_EXPORT: |
|
299 |
throw new SSLProtocolException( |
|
300 |
"unexpected receipt of server key exchange algorithm"); |
|
301 |
default: |
|
302 |
throw new SSLProtocolException( |
|
303 |
"unsupported key exchange algorithm = " |
|
304 |
+ keyExchange); |
|
305 |
} |
|
306 |
break; |
|
307 |
||
308 |
case HandshakeMessage.ht_certificate_request: |
|
309 |
// save for later, it's handled by serverHelloDone |
|
310 |
if ((keyExchange == K_DH_ANON) || (keyExchange == K_ECDH_ANON)) { |
|
311 |
throw new SSLHandshakeException( |
|
312 |
"Client authentication requested for "+ |
|
313 |
"anonymous cipher suite."); |
|
314 |
} else if (keyExchange == K_KRB5 || keyExchange == K_KRB5_EXPORT) { |
|
315 |
throw new SSLHandshakeException( |
|
316 |
"Client certificate requested for "+ |
|
317 |
"kerberos cipher suite."); |
|
318 |
} |
|
7043 | 319 |
certRequest = new CertificateRequest(input, protocolVersion); |
2 | 320 |
if (debug != null && Debug.isOn("handshake")) { |
321 |
certRequest.print(System.out); |
|
322 |
} |
|
7043 | 323 |
|
324 |
if (protocolVersion.v >= ProtocolVersion.TLS12.v) { |
|
325 |
Collection<SignatureAndHashAlgorithm> peerSignAlgs = |
|
326 |
certRequest.getSignAlgorithms(); |
|
327 |
if (peerSignAlgs == null || peerSignAlgs.isEmpty()) { |
|
328 |
throw new SSLHandshakeException( |
|
329 |
"No peer supported signature algorithms"); |
|
330 |
} |
|
331 |
||
332 |
Collection<SignatureAndHashAlgorithm> supportedPeerSignAlgs = |
|
333 |
SignatureAndHashAlgorithm.getSupportedAlgorithms( |
|
334 |
peerSignAlgs); |
|
335 |
if (supportedPeerSignAlgs.isEmpty()) { |
|
336 |
throw new SSLHandshakeException( |
|
337 |
"No supported signature and hash algorithm in common"); |
|
338 |
} |
|
339 |
||
340 |
setPeerSupportedSignAlgs(supportedPeerSignAlgs); |
|
341 |
session.setPeerSupportedSignatureAlgorithms( |
|
342 |
supportedPeerSignAlgs); |
|
343 |
} |
|
344 |
||
2 | 345 |
break; |
346 |
||
347 |
case HandshakeMessage.ht_server_hello_done: |
|
348 |
this.serverHelloDone(new ServerHelloDone(input)); |
|
349 |
break; |
|
350 |
||
351 |
case HandshakeMessage.ht_finished: |
|
28550 | 352 |
// A ChangeCipherSpec record must have been received prior to |
353 |
// reception of the Finished message (RFC 5246, 7.4.9). |
|
354 |
if (!receivedChangeCipherSpec()) { |
|
355 |
fatalSE(Alerts.alert_handshake_failure, |
|
356 |
"Received Finished message before ChangeCipherSpec"); |
|
357 |
} |
|
358 |
||
7043 | 359 |
this.serverFinished( |
360 |
new Finished(protocolVersion, input, cipherSuite)); |
|
2 | 361 |
break; |
362 |
||
363 |
default: |
|
364 |
throw new SSLProtocolException( |
|
365 |
"Illegal client handshake msg, " + type); |
|
366 |
} |
|
367 |
||
368 |
// |
|
369 |
// Move state machine forward if the message handling |
|
370 |
// code didn't already do so |
|
371 |
// |
|
372 |
if (state < type) { |
|
373 |
state = type; |
|
374 |
} |
|
375 |
} |
|
376 |
||
377 |
/* |
|
378 |
* Used by the server to kickstart negotiations -- this requests a |
|
379 |
* "client hello" to renegotiate current cipher specs (e.g. maybe lots |
|
380 |
* of data has been encrypted with the same keys, or the server needs |
|
381 |
* the client to present a certificate). |
|
382 |
*/ |
|
383 |
private void serverHelloRequest(HelloRequest mesg) throws IOException { |
|
384 |
if (debug != null && Debug.isOn("handshake")) { |
|
385 |
mesg.print(System.out); |
|
386 |
} |
|
387 |
||
388 |
// |
|
389 |
// Could be (e.g. at connection setup) that we already |
|
390 |
// sent the "client hello" but the server's not seen it. |
|
391 |
// |
|
392 |
if (state < HandshakeMessage.ht_client_hello) { |
|
6856 | 393 |
if (!secureRenegotiation && !allowUnsafeRenegotiation) { |
394 |
// renegotiation is not allowed. |
|
5182 | 395 |
if (activeProtocolVersion.v >= ProtocolVersion.TLS10.v) { |
6856 | 396 |
// response with a no_renegotiation warning, |
397 |
warningSE(Alerts.alert_no_renegotiation); |
|
5182 | 398 |
|
399 |
// invalidate the handshake so that the caller can |
|
400 |
// dispose this object. |
|
401 |
invalidated = true; |
|
402 |
||
403 |
// If there is still unread block in the handshake |
|
404 |
// input stream, it would be truncated with the disposal |
|
405 |
// and the next handshake message will become incomplete. |
|
406 |
// |
|
407 |
// However, according to SSL/TLS specifications, no more |
|
6856 | 408 |
// handshake message should immediately follow ClientHello |
409 |
// or HelloRequest. So just let it be. |
|
5182 | 410 |
} else { |
411 |
// For SSLv3, send the handshake_failure fatal error. |
|
6856 | 412 |
// Note that SSLv3 does not define a no_renegotiation |
413 |
// alert like TLSv1. However we cannot ignore the message |
|
5182 | 414 |
// simply, otherwise the other side was waiting for a |
415 |
// response that would never come. |
|
416 |
fatalSE(Alerts.alert_handshake_failure, |
|
6856 | 417 |
"Renegotiation is not allowed"); |
5182 | 418 |
} |
419 |
} else { |
|
6856 | 420 |
if (!secureRenegotiation) { |
421 |
if (debug != null && Debug.isOn("handshake")) { |
|
422 |
System.out.println( |
|
423 |
"Warning: continue with insecure renegotiation"); |
|
424 |
} |
|
425 |
} |
|
5182 | 426 |
kickstart(); |
427 |
} |
|
2 | 428 |
} |
429 |
} |
|
430 |
||
431 |
||
432 |
/* |
|
433 |
* Server chooses session parameters given options created by the |
|
434 |
* client -- basically, cipher options, session id, and someday a |
|
435 |
* set of compression options. |
|
436 |
* |
|
437 |
* There are two branches of the state machine, decided by the |
|
438 |
* details of this message. One is the "fast" handshake, where we |
|
439 |
* can resume the pre-existing session we asked resume. The other |
|
440 |
* is a more expensive "full" handshake, with key exchange and |
|
441 |
* probably authentication getting done. |
|
442 |
*/ |
|
443 |
private void serverHello(ServerHello mesg) throws IOException { |
|
444 |
serverKeyExchangeReceived = false; |
|
445 |
if (debug != null && Debug.isOn("handshake")) { |
|
446 |
mesg.print(System.out); |
|
447 |
} |
|
448 |
||
449 |
// check if the server selected protocol version is OK for us |
|
450 |
ProtocolVersion mesgVersion = mesg.protocolVersion; |
|
7039 | 451 |
if (!isNegotiable(mesgVersion)) { |
452 |
throw new SSLHandshakeException( |
|
8782
1ff0b643b793
7009794: misleading text in SSLHandshakeException exception message
xuelei
parents:
7990
diff
changeset
|
453 |
"Server chose " + mesgVersion + |
8791
f5106bbf577d
7022855: Export "PKIX" as the standard algorithm name of KeyManagerFactory
xuelei
parents:
8782
diff
changeset
|
454 |
", but that protocol version is not enabled or not supported " + |
f5106bbf577d
7022855: Export "PKIX" as the standard algorithm name of KeyManagerFactory
xuelei
parents:
8782
diff
changeset
|
455 |
"by the client."); |
2 | 456 |
} |
457 |
||
7804 | 458 |
handshakeHash.protocolDetermined(mesgVersion); |
7043 | 459 |
|
2 | 460 |
// Set protocolVersion and propagate to SSLSocket and the |
461 |
// Handshake streams |
|
462 |
setVersion(mesgVersion); |
|
463 |
||
6856 | 464 |
// check the "renegotiation_info" extension |
465 |
RenegotiationInfoExtension serverHelloRI = (RenegotiationInfoExtension) |
|
466 |
mesg.extensions.get(ExtensionType.EXT_RENEGOTIATION_INFO); |
|
467 |
if (serverHelloRI != null) { |
|
468 |
if (isInitialHandshake) { |
|
469 |
// verify the length of the "renegotiated_connection" field |
|
470 |
if (!serverHelloRI.isEmpty()) { |
|
471 |
// abort the handshake with a fatal handshake_failure alert |
|
472 |
fatalSE(Alerts.alert_handshake_failure, |
|
473 |
"The renegotiation_info field is not empty"); |
|
474 |
} |
|
475 |
||
476 |
secureRenegotiation = true; |
|
477 |
} else { |
|
478 |
// For a legacy renegotiation, the client MUST verify that |
|
479 |
// it does not contain the "renegotiation_info" extension. |
|
480 |
if (!secureRenegotiation) { |
|
481 |
fatalSE(Alerts.alert_handshake_failure, |
|
482 |
"Unexpected renegotiation indication extension"); |
|
483 |
} |
|
484 |
||
485 |
// verify the client_verify_data and server_verify_data values |
|
486 |
byte[] verifyData = |
|
487 |
new byte[clientVerifyData.length + serverVerifyData.length]; |
|
488 |
System.arraycopy(clientVerifyData, 0, verifyData, |
|
489 |
0, clientVerifyData.length); |
|
490 |
System.arraycopy(serverVerifyData, 0, verifyData, |
|
491 |
clientVerifyData.length, serverVerifyData.length); |
|
492 |
if (!Arrays.equals(verifyData, |
|
493 |
serverHelloRI.getRenegotiatedConnection())) { |
|
494 |
fatalSE(Alerts.alert_handshake_failure, |
|
495 |
"Incorrect verify data in ServerHello " + |
|
496 |
"renegotiation_info message"); |
|
497 |
} |
|
498 |
} |
|
499 |
} else { |
|
500 |
// no renegotiation indication extension |
|
501 |
if (isInitialHandshake) { |
|
502 |
if (!allowLegacyHelloMessages) { |
|
503 |
// abort the handshake with a fatal handshake_failure alert |
|
504 |
fatalSE(Alerts.alert_handshake_failure, |
|
505 |
"Failed to negotiate the use of secure renegotiation"); |
|
506 |
} |
|
507 |
||
508 |
secureRenegotiation = false; |
|
509 |
if (debug != null && Debug.isOn("handshake")) { |
|
510 |
System.out.println("Warning: No renegotiation " + |
|
511 |
"indication extension in ServerHello"); |
|
512 |
} |
|
513 |
} else { |
|
514 |
// For a secure renegotiation, the client must abort the |
|
515 |
// handshake if no "renegotiation_info" extension is present. |
|
516 |
if (secureRenegotiation) { |
|
517 |
fatalSE(Alerts.alert_handshake_failure, |
|
518 |
"No renegotiation indication extension"); |
|
519 |
} |
|
520 |
||
521 |
// we have already allowed unsafe renegotation before request |
|
522 |
// the renegotiation. |
|
523 |
} |
|
524 |
} |
|
525 |
||
2 | 526 |
// |
527 |
// Save server nonce, we always use it to compute connection |
|
528 |
// keys and it's also used to create the master secret if we're |
|
529 |
// creating a new session (i.e. in the full handshake). |
|
530 |
// |
|
531 |
svr_random = mesg.svr_random; |
|
532 |
||
6856 | 533 |
if (isNegotiable(mesg.cipherSuite) == false) { |
2 | 534 |
fatalSE(Alerts.alert_illegal_parameter, |
7043 | 535 |
"Server selected improper ciphersuite " + mesg.cipherSuite); |
2 | 536 |
} |
6856 | 537 |
|
2 | 538 |
setCipherSuite(mesg.cipherSuite); |
7043 | 539 |
if (protocolVersion.v >= ProtocolVersion.TLS12.v) { |
540 |
handshakeHash.setFinishedAlg(cipherSuite.prfAlg.getPRFHashAlg()); |
|
541 |
} |
|
2 | 542 |
|
543 |
if (mesg.compression_method != 0) { |
|
544 |
fatalSE(Alerts.alert_illegal_parameter, |
|
545 |
"compression type not supported, " |
|
546 |
+ mesg.compression_method); |
|
547 |
// NOTREACHED |
|
548 |
} |
|
549 |
||
550 |
// so far so good, let's look at the session |
|
551 |
if (session != null) { |
|
552 |
// we tried to resume, let's see what the server decided |
|
553 |
if (session.getSessionId().equals(mesg.sessionId)) { |
|
554 |
// server resumed the session, let's make sure everything |
|
555 |
// checks out |
|
556 |
||
557 |
// Verify that the session ciphers are unchanged. |
|
558 |
CipherSuite sessionSuite = session.getSuite(); |
|
559 |
if (cipherSuite != sessionSuite) { |
|
560 |
throw new SSLProtocolException |
|
561 |
("Server returned wrong cipher suite for session"); |
|
562 |
} |
|
563 |
||
564 |
// verify protocol version match |
|
565 |
ProtocolVersion sessionVersion = session.getProtocolVersion(); |
|
566 |
if (protocolVersion != sessionVersion) { |
|
567 |
throw new SSLProtocolException |
|
568 |
("Server resumed session with wrong protocol version"); |
|
569 |
} |
|
570 |
||
571 |
// validate subject identity |
|
572 |
if (sessionSuite.keyExchange == K_KRB5 || |
|
573 |
sessionSuite.keyExchange == K_KRB5_EXPORT) { |
|
574 |
Principal localPrincipal = session.getLocalPrincipal(); |
|
575 |
||
576 |
Subject subject = null; |
|
577 |
try { |
|
578 |
subject = AccessController.doPrivileged( |
|
579 |
new PrivilegedExceptionAction<Subject>() { |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
580 |
@Override |
2 | 581 |
public Subject run() throws Exception { |
4236 | 582 |
return Krb5Helper.getClientSubject(getAccSE()); |
2 | 583 |
}}); |
584 |
} catch (PrivilegedActionException e) { |
|
585 |
subject = null; |
|
586 |
if (debug != null && Debug.isOn("session")) { |
|
587 |
System.out.println("Attempt to obtain" + |
|
588 |
" subject failed!"); |
|
589 |
} |
|
590 |
} |
|
591 |
||
592 |
if (subject != null) { |
|
4236 | 593 |
// Eliminate dependency on KerberosPrincipal |
594 |
Set<Principal> principals = |
|
595 |
subject.getPrincipals(Principal.class); |
|
2 | 596 |
if (!principals.contains(localPrincipal)) { |
597 |
throw new SSLProtocolException("Server resumed" + |
|
598 |
" session with wrong subject identity"); |
|
599 |
} else { |
|
600 |
if (debug != null && Debug.isOn("session")) |
|
601 |
System.out.println("Subject identity is same"); |
|
602 |
} |
|
603 |
} else { |
|
604 |
if (debug != null && Debug.isOn("session")) |
|
605 |
System.out.println("Kerberos credentials are not" + |
|
606 |
" present in the current Subject; check if " + |
|
607 |
" javax.security.auth.useSubjectAsCreds" + |
|
608 |
" system property has been set to false"); |
|
609 |
throw new SSLProtocolException |
|
610 |
("Server resumed session with no subject"); |
|
611 |
} |
|
612 |
} |
|
613 |
||
614 |
// looks fine; resume it, and update the state machine. |
|
615 |
resumingSession = true; |
|
616 |
state = HandshakeMessage.ht_finished - 1; |
|
617 |
calculateConnectionKeys(session.getMasterSecret()); |
|
618 |
if (debug != null && Debug.isOn("session")) { |
|
619 |
System.out.println("%% Server resumed " + session); |
|
620 |
} |
|
621 |
} else { |
|
622 |
// we wanted to resume, but the server refused |
|
623 |
session = null; |
|
624 |
if (!enableNewSession) { |
|
27068 | 625 |
throw new SSLException("New session creation is disabled"); |
2 | 626 |
} |
627 |
} |
|
628 |
} |
|
629 |
||
7043 | 630 |
if (resumingSession && session != null) { |
631 |
setHandshakeSessionSE(session); |
|
27068 | 632 |
// Reserve the handshake state if this is a session-resumption |
633 |
// abbreviated initial handshake. |
|
634 |
if (isInitialHandshake) { |
|
635 |
session.setAsSessionResumption(true); |
|
636 |
} |
|
637 |
||
7043 | 638 |
return; |
639 |
} |
|
640 |
||
2 | 641 |
// check extensions |
642 |
for (HelloExtension ext : mesg.extensions.list()) { |
|
643 |
ExtensionType type = ext.type; |
|
20499 | 644 |
if (type == ExtensionType.EXT_SERVER_NAME) { |
645 |
serverNamesAccepted = true; |
|
646 |
} else if ((type != ExtensionType.EXT_ELLIPTIC_CURVES) |
|
6856 | 647 |
&& (type != ExtensionType.EXT_EC_POINT_FORMATS) |
7043 | 648 |
&& (type != ExtensionType.EXT_SERVER_NAME) |
6856 | 649 |
&& (type != ExtensionType.EXT_RENEGOTIATION_INFO)) { |
2 | 650 |
fatalSE(Alerts.alert_unsupported_extension, |
651 |
"Server sent an unsupported extension: " + type); |
|
652 |
} |
|
653 |
} |
|
654 |
||
655 |
// Create a new session, we need to do the full handshake |
|
656 |
session = new SSLSessionImpl(protocolVersion, cipherSuite, |
|
7043 | 657 |
getLocalSupportedSignAlgs(), |
2 | 658 |
mesg.sessionId, getHostSE(), getPortSE()); |
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
11521
diff
changeset
|
659 |
session.setRequestedServerNames(requestedServerNames); |
7043 | 660 |
setHandshakeSessionSE(session); |
2 | 661 |
if (debug != null && Debug.isOn("handshake")) { |
662 |
System.out.println("** " + cipherSuite); |
|
663 |
} |
|
664 |
} |
|
665 |
||
666 |
/* |
|
667 |
* Server's own key was either a signing-only key, or was too |
|
668 |
* large for export rules ... this message holds an ephemeral |
|
669 |
* RSA key to use for key exchange. |
|
670 |
*/ |
|
671 |
private void serverKeyExchange(RSA_ServerKeyExchange mesg) |
|
672 |
throws IOException, GeneralSecurityException { |
|
673 |
if (debug != null && Debug.isOn("handshake")) { |
|
674 |
mesg.print(System.out); |
|
675 |
} |
|
676 |
if (!mesg.verify(serverKey, clnt_random, svr_random)) { |
|
677 |
fatalSE(Alerts.alert_handshake_failure, |
|
678 |
"server key exchange invalid"); |
|
679 |
// NOTREACHED |
|
680 |
} |
|
681 |
ephemeralServerKey = mesg.getPublicKey(); |
|
682 |
} |
|
683 |
||
684 |
||
685 |
/* |
|
686 |
* Diffie-Hellman key exchange. We save the server public key and |
|
687 |
* our own D-H algorithm object so we can defer key calculations |
|
688 |
* until after we've sent the client key exchange message (which |
|
689 |
* gives client and server some useful parallelism). |
|
690 |
*/ |
|
691 |
private void serverKeyExchange(DH_ServerKeyExchange mesg) |
|
692 |
throws IOException { |
|
693 |
if (debug != null && Debug.isOn("handshake")) { |
|
694 |
mesg.print(System.out); |
|
695 |
} |
|
7043 | 696 |
dh = new DHCrypt(mesg.getModulus(), mesg.getBase(), |
697 |
sslContext.getSecureRandom()); |
|
2 | 698 |
serverDH = mesg.getServerPublicKey(); |
699 |
} |
|
700 |
||
7043 | 701 |
private void serverKeyExchange(ECDH_ServerKeyExchange mesg) |
702 |
throws IOException { |
|
2 | 703 |
if (debug != null && Debug.isOn("handshake")) { |
704 |
mesg.print(System.out); |
|
705 |
} |
|
706 |
ECPublicKey key = mesg.getPublicKey(); |
|
707 |
ecdh = new ECDHCrypt(key.getParams(), sslContext.getSecureRandom()); |
|
708 |
ephemeralServerKey = key; |
|
709 |
} |
|
710 |
||
711 |
/* |
|
712 |
* The server's "Hello Done" message is the client's sign that |
|
713 |
* it's time to do all the hard work. |
|
714 |
*/ |
|
715 |
private void serverHelloDone(ServerHelloDone mesg) throws IOException { |
|
716 |
if (debug != null && Debug.isOn("handshake")) { |
|
717 |
mesg.print(System.out); |
|
718 |
} |
|
719 |
/* |
|
720 |
* Always make sure the input has been digested before we |
|
721 |
* start emitting data, to ensure the hashes are correctly |
|
722 |
* computed for the Finished and CertificateVerify messages |
|
723 |
* which we send (here). |
|
724 |
*/ |
|
725 |
input.digestNow(); |
|
726 |
||
727 |
/* |
|
728 |
* FIRST ... if requested, send an appropriate Certificate chain |
|
729 |
* to authenticate the client, and remember the associated private |
|
730 |
* key to sign the CertificateVerify message. |
|
731 |
*/ |
|
732 |
PrivateKey signingKey = null; |
|
733 |
||
734 |
if (certRequest != null) { |
|
735 |
X509ExtendedKeyManager km = sslContext.getX509KeyManager(); |
|
736 |
||
7990 | 737 |
ArrayList<String> keytypesTmp = new ArrayList<>(4); |
2 | 738 |
|
739 |
for (int i = 0; i < certRequest.types.length; i++) { |
|
740 |
String typeName; |
|
741 |
||
742 |
switch (certRequest.types[i]) { |
|
743 |
case CertificateRequest.cct_rsa_sign: |
|
744 |
typeName = "RSA"; |
|
745 |
break; |
|
746 |
||
747 |
case CertificateRequest.cct_dss_sign: |
|
748 |
typeName = "DSA"; |
|
749 |
break; |
|
750 |
||
751 |
case CertificateRequest.cct_ecdsa_sign: |
|
752 |
// ignore if we do not have EC crypto available |
|
753 |
typeName = JsseJce.isEcAvailable() ? "EC" : null; |
|
754 |
break; |
|
755 |
||
756 |
// Fixed DH/ECDH client authentication not supported |
|
757 |
case CertificateRequest.cct_rsa_fixed_dh: |
|
758 |
case CertificateRequest.cct_dss_fixed_dh: |
|
759 |
case CertificateRequest.cct_rsa_fixed_ecdh: |
|
760 |
case CertificateRequest.cct_ecdsa_fixed_ecdh: |
|
761 |
// Any other values (currently not used in TLS) |
|
762 |
case CertificateRequest.cct_rsa_ephemeral_dh: |
|
763 |
case CertificateRequest.cct_dss_ephemeral_dh: |
|
764 |
default: |
|
765 |
typeName = null; |
|
766 |
break; |
|
767 |
} |
|
768 |
||
769 |
if ((typeName != null) && (!keytypesTmp.contains(typeName))) { |
|
770 |
keytypesTmp.add(typeName); |
|
771 |
} |
|
772 |
} |
|
773 |
||
774 |
String alias = null; |
|
775 |
int keytypesTmpSize = keytypesTmp.size(); |
|
776 |
if (keytypesTmpSize != 0) { |
|
777 |
String keytypes[] = |
|
778 |
keytypesTmp.toArray(new String[keytypesTmpSize]); |
|
779 |
||
780 |
if (conn != null) { |
|
781 |
alias = km.chooseClientAlias(keytypes, |
|
782 |
certRequest.getAuthorities(), conn); |
|
783 |
} else { |
|
784 |
alias = km.chooseEngineClientAlias(keytypes, |
|
785 |
certRequest.getAuthorities(), engine); |
|
786 |
} |
|
787 |
} |
|
788 |
||
789 |
CertificateMsg m1 = null; |
|
790 |
if (alias != null) { |
|
791 |
X509Certificate[] certs = km.getCertificateChain(alias); |
|
792 |
if ((certs != null) && (certs.length != 0)) { |
|
793 |
PublicKey publicKey = certs[0].getPublicKey(); |
|
794 |
// for EC, make sure we use a supported named curve |
|
795 |
if (publicKey instanceof ECPublicKey) { |
|
7043 | 796 |
ECParameterSpec params = |
797 |
((ECPublicKey)publicKey).getParams(); |
|
798 |
int index = |
|
799 |
SupportedEllipticCurvesExtension.getCurveIndex( |
|
800 |
params); |
|
801 |
if (!SupportedEllipticCurvesExtension.isSupported( |
|
802 |
index)) { |
|
2 | 803 |
publicKey = null; |
804 |
} |
|
805 |
} |
|
806 |
if (publicKey != null) { |
|
807 |
m1 = new CertificateMsg(certs); |
|
808 |
signingKey = km.getPrivateKey(alias); |
|
809 |
session.setLocalPrivateKey(signingKey); |
|
810 |
session.setLocalCertificates(certs); |
|
811 |
} |
|
812 |
} |
|
813 |
} |
|
814 |
if (m1 == null) { |
|
815 |
// |
|
816 |
// No appropriate cert was found ... report this to the |
|
817 |
// server. For SSLv3, send the no_certificate alert; |
|
818 |
// TLS uses an empty cert chain instead. |
|
819 |
// |
|
820 |
if (protocolVersion.v >= ProtocolVersion.TLS10.v) { |
|
821 |
m1 = new CertificateMsg(new X509Certificate [0]); |
|
822 |
} else { |
|
823 |
warningSE(Alerts.alert_no_certificate); |
|
824 |
} |
|
29264
5172066a2da6
8054037: Improve tracing for java.security.debug=certpath
juh
parents:
28550
diff
changeset
|
825 |
if (debug != null && Debug.isOn("handshake")) { |
5172066a2da6
8054037: Improve tracing for java.security.debug=certpath
juh
parents:
28550
diff
changeset
|
826 |
System.out.println( |
5172066a2da6
8054037: Improve tracing for java.security.debug=certpath
juh
parents:
28550
diff
changeset
|
827 |
"Warning: no suitable certificate found - " + |
5172066a2da6
8054037: Improve tracing for java.security.debug=certpath
juh
parents:
28550
diff
changeset
|
828 |
"continuing without client authentication"); |
5172066a2da6
8054037: Improve tracing for java.security.debug=certpath
juh
parents:
28550
diff
changeset
|
829 |
} |
2 | 830 |
} |
831 |
||
832 |
// |
|
833 |
// At last ... send any client certificate chain. |
|
834 |
// |
|
835 |
if (m1 != null) { |
|
836 |
if (debug != null && Debug.isOn("handshake")) { |
|
837 |
m1.print(System.out); |
|
838 |
} |
|
839 |
m1.write(output); |
|
840 |
} |
|
841 |
} |
|
842 |
||
843 |
/* |
|
844 |
* SECOND ... send the client key exchange message. The |
|
845 |
* procedure used is a function of the cipher suite selected; |
|
846 |
* one is always needed. |
|
847 |
*/ |
|
848 |
HandshakeMessage m2; |
|
849 |
||
850 |
switch (keyExchange) { |
|
851 |
||
852 |
case K_RSA: |
|
853 |
case K_RSA_EXPORT: |
|
703 | 854 |
if (serverKey == null) { |
855 |
throw new SSLProtocolException |
|
856 |
("Server did not send certificate message"); |
|
857 |
} |
|
858 |
||
859 |
if (!(serverKey instanceof RSAPublicKey)) { |
|
860 |
throw new SSLProtocolException |
|
861 |
("Server certificate does not include an RSA key"); |
|
862 |
} |
|
863 |
||
2 | 864 |
/* |
865 |
* For RSA key exchange, we randomly generate a new |
|
866 |
* pre-master secret and encrypt it with the server's |
|
867 |
* public key. Then we save that pre-master secret |
|
868 |
* so that we can calculate the keying data later; |
|
869 |
* it's a performance speedup not to do that until |
|
870 |
* the client's waiting for the server response, but |
|
871 |
* more of a speedup for the D-H case. |
|
703 | 872 |
* |
873 |
* If the RSA_EXPORT scheme is active, when the public |
|
874 |
* key in the server certificate is less than or equal |
|
875 |
* to 512 bits in length, use the cert's public key, |
|
876 |
* otherwise, the ephemeral one. |
|
2 | 877 |
*/ |
703 | 878 |
PublicKey key; |
879 |
if (keyExchange == K_RSA) { |
|
880 |
key = serverKey; |
|
881 |
} else { // K_RSA_EXPORT |
|
882 |
if (JsseJce.getRSAKeyLength(serverKey) <= 512) { |
|
883 |
// extraneous ephemeralServerKey check done |
|
884 |
// above in processMessage() |
|
885 |
key = serverKey; |
|
886 |
} else { |
|
887 |
if (ephemeralServerKey == null) { |
|
888 |
throw new SSLProtocolException("Server did not send" + |
|
889 |
" a RSA_EXPORT Server Key Exchange message"); |
|
890 |
} |
|
891 |
key = ephemeralServerKey; |
|
892 |
} |
|
893 |
} |
|
894 |
||
2 | 895 |
m2 = new RSAClientKeyExchange(protocolVersion, maxProtocolVersion, |
896 |
sslContext.getSecureRandom(), key); |
|
897 |
break; |
|
898 |
case K_DH_RSA: |
|
899 |
case K_DH_DSS: |
|
900 |
/* |
|
901 |
* For DH Key exchange, we only need to make sure the server |
|
902 |
* knows our public key, so we calculate the same pre-master |
|
903 |
* secret. |
|
904 |
* |
|
905 |
* For certs that had DH keys in them, we send an empty |
|
906 |
* handshake message (no key) ... we flag this case by |
|
907 |
* passing a null "dhPublic" value. |
|
908 |
* |
|
909 |
* Otherwise we send ephemeral DH keys, unsigned. |
|
910 |
*/ |
|
911 |
// if (useDH_RSA || useDH_DSS) |
|
912 |
m2 = new DHClientKeyExchange(); |
|
913 |
break; |
|
914 |
case K_DHE_RSA: |
|
915 |
case K_DHE_DSS: |
|
916 |
case K_DH_ANON: |
|
917 |
if (dh == null) { |
|
918 |
throw new SSLProtocolException |
|
919 |
("Server did not send a DH Server Key Exchange message"); |
|
920 |
} |
|
921 |
m2 = new DHClientKeyExchange(dh.getPublicKey()); |
|
922 |
break; |
|
923 |
case K_ECDHE_RSA: |
|
924 |
case K_ECDHE_ECDSA: |
|
925 |
case K_ECDH_ANON: |
|
926 |
if (ecdh == null) { |
|
927 |
throw new SSLProtocolException |
|
928 |
("Server did not send a ECDH Server Key Exchange message"); |
|
929 |
} |
|
930 |
m2 = new ECDHClientKeyExchange(ecdh.getPublicKey()); |
|
931 |
break; |
|
932 |
case K_ECDH_RSA: |
|
933 |
case K_ECDH_ECDSA: |
|
934 |
if (serverKey == null) { |
|
935 |
throw new SSLProtocolException |
|
936 |
("Server did not send certificate message"); |
|
937 |
} |
|
938 |
if (serverKey instanceof ECPublicKey == false) { |
|
939 |
throw new SSLProtocolException |
|
940 |
("Server certificate does not include an EC key"); |
|
941 |
} |
|
942 |
ECParameterSpec params = ((ECPublicKey)serverKey).getParams(); |
|
943 |
ecdh = new ECDHCrypt(params, sslContext.getSecureRandom()); |
|
944 |
m2 = new ECDHClientKeyExchange(ecdh.getPublicKey()); |
|
945 |
break; |
|
946 |
case K_KRB5: |
|
947 |
case K_KRB5_EXPORT: |
|
20499 | 948 |
String sniHostname = null; |
949 |
for (SNIServerName serverName : requestedServerNames) { |
|
950 |
if (serverName instanceof SNIHostName) { |
|
951 |
sniHostname = ((SNIHostName) serverName).getAsciiName(); |
|
952 |
break; |
|
953 |
} |
|
2 | 954 |
} |
20499 | 955 |
|
956 |
KerberosClientKeyExchange kerberosMsg = null; |
|
957 |
if (sniHostname != null) { |
|
958 |
// use first requested SNI hostname |
|
959 |
try { |
|
960 |
kerberosMsg = new KerberosClientKeyExchange( |
|
961 |
sniHostname, getAccSE(), protocolVersion, |
|
962 |
sslContext.getSecureRandom()); |
|
963 |
} catch(IOException e) { |
|
964 |
if (serverNamesAccepted) { |
|
965 |
// server accepted requested SNI hostname, |
|
966 |
// so it must be used |
|
967 |
throw e; |
|
968 |
} |
|
969 |
// fallback to using hostname |
|
970 |
if (debug != null && Debug.isOn("handshake")) { |
|
971 |
System.out.println( |
|
972 |
"Warning, cannot use Server Name Indication: " |
|
973 |
+ e.getMessage()); |
|
974 |
} |
|
975 |
} |
|
976 |
} |
|
977 |
||
978 |
if (kerberosMsg == null) { |
|
979 |
String hostname = getHostSE(); |
|
980 |
if (hostname == null) { |
|
981 |
throw new IOException("Hostname is required" + |
|
982 |
" to use Kerberos cipher suites"); |
|
983 |
} |
|
984 |
kerberosMsg = new KerberosClientKeyExchange( |
|
985 |
hostname, getAccSE(), protocolVersion, |
|
986 |
sslContext.getSecureRandom()); |
|
987 |
} |
|
988 |
||
2 | 989 |
// Record the principals involved in exchange |
990 |
session.setPeerPrincipal(kerberosMsg.getPeerPrincipal()); |
|
991 |
session.setLocalPrincipal(kerberosMsg.getLocalPrincipal()); |
|
992 |
m2 = kerberosMsg; |
|
993 |
break; |
|
994 |
default: |
|
995 |
// somethings very wrong |
|
996 |
throw new RuntimeException |
|
997 |
("Unsupported key exchange: " + keyExchange); |
|
998 |
} |
|
999 |
if (debug != null && Debug.isOn("handshake")) { |
|
1000 |
m2.print(System.out); |
|
1001 |
} |
|
1002 |
m2.write(output); |
|
1003 |
||
1004 |
||
1005 |
/* |
|
1006 |
* THIRD, send a "change_cipher_spec" record followed by the |
|
1007 |
* "Finished" message. We flush the messages we've queued up, to |
|
1008 |
* get concurrency between client and server. The concurrency is |
|
1009 |
* useful as we calculate the master secret, which is needed both |
|
1010 |
* to compute the "Finished" message, and to compute the keys used |
|
1011 |
* to protect all records following the change_cipher_spec. |
|
1012 |
*/ |
|
1013 |
||
1014 |
output.doHashes(); |
|
1015 |
output.flush(); |
|
1016 |
||
1017 |
/* |
|
1018 |
* We deferred calculating the master secret and this connection's |
|
1019 |
* keying data; we do it now. Deferring this calculation is good |
|
1020 |
* from a performance point of view, since it lets us do it during |
|
1021 |
* some time that network delays and the server's own calculations |
|
1022 |
* would otherwise cause to be "dead" in the critical path. |
|
1023 |
*/ |
|
1024 |
SecretKey preMasterSecret; |
|
1025 |
switch (keyExchange) { |
|
1026 |
case K_RSA: |
|
1027 |
case K_RSA_EXPORT: |
|
1028 |
preMasterSecret = ((RSAClientKeyExchange)m2).preMaster; |
|
1029 |
break; |
|
1030 |
case K_KRB5: |
|
1031 |
case K_KRB5_EXPORT: |
|
1032 |
byte[] secretBytes = |
|
4236 | 1033 |
((KerberosClientKeyExchange)m2).getUnencryptedPreMasterSecret(); |
7043 | 1034 |
preMasterSecret = new SecretKeySpec(secretBytes, |
1035 |
"TlsPremasterSecret"); |
|
2 | 1036 |
break; |
1037 |
case K_DHE_RSA: |
|
1038 |
case K_DHE_DSS: |
|
1039 |
case K_DH_ANON: |
|
16080 | 1040 |
preMasterSecret = dh.getAgreedSecret(serverDH, true); |
2 | 1041 |
break; |
1042 |
case K_ECDHE_RSA: |
|
1043 |
case K_ECDHE_ECDSA: |
|
1044 |
case K_ECDH_ANON: |
|
1045 |
preMasterSecret = ecdh.getAgreedSecret(ephemeralServerKey); |
|
1046 |
break; |
|
1047 |
case K_ECDH_RSA: |
|
1048 |
case K_ECDH_ECDSA: |
|
1049 |
preMasterSecret = ecdh.getAgreedSecret(serverKey); |
|
1050 |
break; |
|
1051 |
default: |
|
7043 | 1052 |
throw new IOException("Internal error: unknown key exchange " |
1053 |
+ keyExchange); |
|
2 | 1054 |
} |
1055 |
||
1056 |
calculateKeys(preMasterSecret, null); |
|
1057 |
||
1058 |
/* |
|
1059 |
* FOURTH, if we sent a Certificate, we need to send a signed |
|
1060 |
* CertificateVerify (unless the key in the client's certificate |
|
1061 |
* was a Diffie-Hellman key).). |
|
1062 |
* |
|
1063 |
* This uses a hash of the previous handshake messages ... either |
|
1064 |
* a nonfinal one (if the particular implementation supports it) |
|
1065 |
* or else using the third element in the arrays of hashes being |
|
1066 |
* computed. |
|
1067 |
*/ |
|
1068 |
if (signingKey != null) { |
|
1069 |
CertificateVerify m3; |
|
1070 |
try { |
|
7043 | 1071 |
SignatureAndHashAlgorithm preferableSignatureAlgorithm = null; |
1072 |
if (protocolVersion.v >= ProtocolVersion.TLS12.v) { |
|
1073 |
preferableSignatureAlgorithm = |
|
1074 |
SignatureAndHashAlgorithm.getPreferableAlgorithm( |
|
11521
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
8791
diff
changeset
|
1075 |
peerSupportedSignAlgs, signingKey.getAlgorithm(), |
d7698e6c5f51
7106773: 512 bits RSA key cannot work with SHA384 and SHA512
xuelei
parents:
8791
diff
changeset
|
1076 |
signingKey); |
7043 | 1077 |
|
1078 |
if (preferableSignatureAlgorithm == null) { |
|
1079 |
throw new SSLHandshakeException( |
|
1080 |
"No supported signature algorithm"); |
|
1081 |
} |
|
1082 |
||
1083 |
String hashAlg = |
|
1084 |
SignatureAndHashAlgorithm.getHashAlgorithmName( |
|
1085 |
preferableSignatureAlgorithm); |
|
1086 |
if (hashAlg == null || hashAlg.length() == 0) { |
|
1087 |
throw new SSLHandshakeException( |
|
1088 |
"No supported hash algorithm"); |
|
1089 |
} |
|
1090 |
} |
|
1091 |
||
2 | 1092 |
m3 = new CertificateVerify(protocolVersion, handshakeHash, |
1093 |
signingKey, session.getMasterSecret(), |
|
7043 | 1094 |
sslContext.getSecureRandom(), |
1095 |
preferableSignatureAlgorithm); |
|
2 | 1096 |
} catch (GeneralSecurityException e) { |
1097 |
fatalSE(Alerts.alert_handshake_failure, |
|
1098 |
"Error signing certificate verify", e); |
|
1099 |
// NOTREACHED, make compiler happy |
|
1100 |
m3 = null; |
|
1101 |
} |
|
1102 |
if (debug != null && Debug.isOn("handshake")) { |
|
1103 |
m3.print(System.out); |
|
1104 |
} |
|
1105 |
m3.write(output); |
|
1106 |
output.doHashes(); |
|
1107 |
} |
|
1108 |
||
1109 |
/* |
|
1110 |
* OK, that's that! |
|
1111 |
*/ |
|
1112 |
sendChangeCipherAndFinish(false); |
|
1113 |
} |
|
1114 |
||
1115 |
||
1116 |
/* |
|
1117 |
* "Finished" is the last handshake message sent. If we got this |
|
1118 |
* far, the MAC has been validated post-decryption. We validate |
|
1119 |
* the two hashes here as an additional sanity check, protecting |
|
1120 |
* the handshake against various active attacks. |
|
1121 |
*/ |
|
1122 |
private void serverFinished(Finished mesg) throws IOException { |
|
1123 |
if (debug != null && Debug.isOn("handshake")) { |
|
1124 |
mesg.print(System.out); |
|
1125 |
} |
|
1126 |
||
7043 | 1127 |
boolean verified = mesg.verify(handshakeHash, Finished.SERVER, |
1128 |
session.getMasterSecret()); |
|
2 | 1129 |
|
1130 |
if (!verified) { |
|
1131 |
fatalSE(Alerts.alert_illegal_parameter, |
|
1132 |
"server 'finished' message doesn't verify"); |
|
1133 |
// NOTREACHED |
|
1134 |
} |
|
1135 |
||
1136 |
/* |
|
6856 | 1137 |
* save server verify data for secure renegotiation |
1138 |
*/ |
|
1139 |
if (secureRenegotiation) { |
|
1140 |
serverVerifyData = mesg.getVerifyData(); |
|
1141 |
} |
|
1142 |
||
1143 |
/* |
|
27068 | 1144 |
* Reset the handshake state if this is not an initial handshake. |
1145 |
*/ |
|
1146 |
if (!isInitialHandshake) { |
|
1147 |
session.setAsSessionResumption(false); |
|
1148 |
} |
|
1149 |
||
1150 |
/* |
|
2 | 1151 |
* OK, it verified. If we're doing the fast handshake, add that |
1152 |
* "Finished" message to the hash of handshake messages, then send |
|
1153 |
* our own change_cipher_spec and Finished message for the server |
|
1154 |
* to verify in turn. These are the last handshake messages. |
|
1155 |
* |
|
1156 |
* In any case, update the session cache. We're done handshaking, |
|
1157 |
* so there are no threats any more associated with partially |
|
1158 |
* completed handshakes. |
|
1159 |
*/ |
|
1160 |
if (resumingSession) { |
|
1161 |
input.digestNow(); |
|
1162 |
sendChangeCipherAndFinish(true); |
|
1163 |
} |
|
1164 |
session.setLastAccessedTime(System.currentTimeMillis()); |
|
1165 |
||
1166 |
if (!resumingSession) { |
|
1167 |
if (session.isRejoinable()) { |
|
1168 |
((SSLSessionContextImpl) sslContext |
|
1169 |
.engineGetClientSessionContext()) |
|
1170 |
.put(session); |
|
1171 |
if (debug != null && Debug.isOn("session")) { |
|
1172 |
System.out.println("%% Cached client session: " + session); |
|
1173 |
} |
|
1174 |
} else if (debug != null && Debug.isOn("session")) { |
|
1175 |
System.out.println( |
|
1176 |
"%% Didn't cache non-resumable client session: " |
|
1177 |
+ session); |
|
1178 |
} |
|
1179 |
} |
|
1180 |
} |
|
1181 |
||
1182 |
||
1183 |
/* |
|
1184 |
* Send my change-cipher-spec and Finished message ... done as the |
|
1185 |
* last handshake act in either the short or long sequences. In |
|
1186 |
* the short one, we've already seen the server's Finished; in the |
|
1187 |
* long one, we wait for it now. |
|
1188 |
*/ |
|
1189 |
private void sendChangeCipherAndFinish(boolean finishedTag) |
|
1190 |
throws IOException { |
|
1191 |
Finished mesg = new Finished(protocolVersion, handshakeHash, |
|
7043 | 1192 |
Finished.CLIENT, session.getMasterSecret(), cipherSuite); |
2 | 1193 |
|
1194 |
/* |
|
1195 |
* Send the change_cipher_spec message, then the Finished message |
|
1196 |
* which we just calculated (and protected using the keys we just |
|
1197 |
* calculated). Server responds with its Finished message, except |
|
1198 |
* in the "fast handshake" (resume session) case. |
|
1199 |
*/ |
|
1200 |
sendChangeCipherSpec(mesg, finishedTag); |
|
1201 |
||
1202 |
/* |
|
6856 | 1203 |
* save client verify data for secure renegotiation |
1204 |
*/ |
|
1205 |
if (secureRenegotiation) { |
|
1206 |
clientVerifyData = mesg.getVerifyData(); |
|
1207 |
} |
|
1208 |
||
1209 |
/* |
|
2 | 1210 |
* Update state machine so server MUST send 'finished' next. |
1211 |
* (In "long" handshake case; in short case, we're responding |
|
1212 |
* to its message.) |
|
1213 |
*/ |
|
1214 |
state = HandshakeMessage.ht_finished - 1; |
|
1215 |
} |
|
1216 |
||
1217 |
||
1218 |
/* |
|
1219 |
* Returns a ClientHello message to kickstart renegotiations |
|
1220 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1221 |
@Override |
2 | 1222 |
HandshakeMessage getKickstartMessage() throws SSLException { |
6856 | 1223 |
// session ID of the ClientHello message |
1224 |
SessionId sessionId = SSLSessionImpl.nullSession.getSessionId(); |
|
1225 |
||
1226 |
// a list of cipher suites sent by the client |
|
7039 | 1227 |
CipherSuiteList cipherSuites = getActiveCipherSuites(); |
6856 | 1228 |
|
1229 |
// set the max protocol version this client is supporting. |
|
2 | 1230 |
maxProtocolVersion = protocolVersion; |
1231 |
||
1232 |
// |
|
1233 |
// Try to resume an existing session. This might be mandatory, |
|
1234 |
// given certain API options. |
|
1235 |
// |
|
1236 |
session = ((SSLSessionContextImpl)sslContext |
|
1237 |
.engineGetClientSessionContext()) |
|
1238 |
.get(getHostSE(), getPortSE()); |
|
1239 |
if (debug != null && Debug.isOn("session")) { |
|
1240 |
if (session != null) { |
|
1241 |
System.out.println("%% Client cached " |
|
1242 |
+ session |
|
1243 |
+ (session.isRejoinable() ? "" : " (not rejoinable)")); |
|
1244 |
} else { |
|
1245 |
System.out.println("%% No cached client session"); |
|
1246 |
} |
|
1247 |
} |
|
27068 | 1248 |
if (session != null) { |
1249 |
// If unsafe server certificate change is not allowed, reserve |
|
1250 |
// current server certificates if the previous handshake is a |
|
1251 |
// session-resumption abbreviated initial handshake. |
|
1252 |
if (!allowUnsafeServerCertChange && session.isSessionResumption()) { |
|
1253 |
try { |
|
1254 |
// If existing, peer certificate chain cannot be null. |
|
1255 |
reservedServerCerts = |
|
1256 |
(X509Certificate[])session.getPeerCertificates(); |
|
1257 |
} catch (SSLPeerUnverifiedException puve) { |
|
1258 |
// Maybe not certificate-based, ignore the exception. |
|
1259 |
} |
|
1260 |
} |
|
1261 |
||
1262 |
if (!session.isRejoinable()) { |
|
1263 |
session = null; |
|
1264 |
} |
|
2 | 1265 |
} |
1266 |
||
1267 |
if (session != null) { |
|
1268 |
CipherSuite sessionSuite = session.getSuite(); |
|
1269 |
ProtocolVersion sessionVersion = session.getProtocolVersion(); |
|
6856 | 1270 |
if (isNegotiable(sessionSuite) == false) { |
2 | 1271 |
if (debug != null && Debug.isOn("session")) { |
6856 | 1272 |
System.out.println("%% can't resume, unavailable cipher"); |
2 | 1273 |
} |
1274 |
session = null; |
|
1275 |
} |
|
1276 |
||
7039 | 1277 |
if ((session != null) && !isNegotiable(sessionVersion)) { |
2 | 1278 |
if (debug != null && Debug.isOn("session")) { |
1279 |
System.out.println("%% can't resume, protocol disabled"); |
|
1280 |
} |
|
1281 |
session = null; |
|
1282 |
} |
|
1283 |
||
1284 |
if (session != null) { |
|
1285 |
if (debug != null) { |
|
1286 |
if (Debug.isOn("handshake") || Debug.isOn("session")) { |
|
1287 |
System.out.println("%% Try resuming " + session |
|
1288 |
+ " from port " + getLocalPortSE()); |
|
1289 |
} |
|
1290 |
} |
|
1291 |
||
6856 | 1292 |
sessionId = session.getSessionId(); |
2 | 1293 |
maxProtocolVersion = sessionVersion; |
1294 |
||
1295 |
// Update SSL version number in underlying SSL socket and |
|
1296 |
// handshake output stream, so that the output records (at the |
|
1297 |
// record layer) have the correct version |
|
1298 |
setVersion(sessionVersion); |
|
1299 |
} |
|
1300 |
||
6856 | 1301 |
/* |
1302 |
* Force use of the previous session ciphersuite, and |
|
1303 |
* add the SCSV if enabled. |
|
1304 |
*/ |
|
2 | 1305 |
if (!enableNewSession) { |
1306 |
if (session == null) { |
|
7039 | 1307 |
throw new SSLHandshakeException( |
2 | 1308 |
"Can't reuse existing SSL client session"); |
1309 |
} |
|
6856 | 1310 |
|
7990 | 1311 |
Collection<CipherSuite> cipherList = new ArrayList<>(2); |
6856 | 1312 |
cipherList.add(sessionSuite); |
1313 |
if (!secureRenegotiation && |
|
1314 |
cipherSuites.contains(CipherSuite.C_SCSV)) { |
|
1315 |
cipherList.add(CipherSuite.C_SCSV); |
|
1316 |
} // otherwise, renegotiation_info extension will be used |
|
1317 |
||
1318 |
cipherSuites = new CipherSuiteList(cipherList); |
|
2 | 1319 |
} |
1320 |
} |
|
1321 |
||
6856 | 1322 |
if (session == null && !enableNewSession) { |
7039 | 1323 |
throw new SSLHandshakeException("No existing session to resume"); |
6856 | 1324 |
} |
1325 |
||
1326 |
// exclude SCSV for secure renegotiation |
|
1327 |
if (secureRenegotiation && cipherSuites.contains(CipherSuite.C_SCSV)) { |
|
1328 |
Collection<CipherSuite> cipherList = |
|
7990 | 1329 |
new ArrayList<>(cipherSuites.size() - 1); |
6856 | 1330 |
for (CipherSuite suite : cipherSuites.collection()) { |
1331 |
if (suite != CipherSuite.C_SCSV) { |
|
1332 |
cipherList.add(suite); |
|
1333 |
} |
|
1334 |
} |
|
1335 |
||
1336 |
cipherSuites = new CipherSuiteList(cipherList); |
|
1337 |
} |
|
2 | 1338 |
|
6856 | 1339 |
// make sure there is a negotiable cipher suite. |
1340 |
boolean negotiable = false; |
|
1341 |
for (CipherSuite suite : cipherSuites.collection()) { |
|
1342 |
if (isNegotiable(suite)) { |
|
1343 |
negotiable = true; |
|
1344 |
break; |
|
1345 |
} |
|
1346 |
} |
|
1347 |
||
1348 |
if (!negotiable) { |
|
7039 | 1349 |
throw new SSLHandshakeException("No negotiable cipher suite"); |
6856 | 1350 |
} |
1351 |
||
7043 | 1352 |
// Not a TLS1.2+ handshake |
1353 |
// For SSLv2Hello, HandshakeHash.reset() will be called, so we |
|
1354 |
// cannot call HandshakeHash.protocolDetermined() here. As it does |
|
1355 |
// not follow the spec that HandshakeHash.reset() can be only be |
|
1356 |
// called before protocolDetermined. |
|
1357 |
// if (maxProtocolVersion.v < ProtocolVersion.TLS12.v) { |
|
7804 | 1358 |
// handshakeHash.protocolDetermined(maxProtocolVersion); |
7043 | 1359 |
// } |
1360 |
||
6856 | 1361 |
// create the ClientHello message |
1362 |
ClientHello clientHelloMessage = new ClientHello( |
|
1363 |
sslContext.getSecureRandom(), maxProtocolVersion, |
|
1364 |
sessionId, cipherSuites); |
|
1365 |
||
7043 | 1366 |
// add signature_algorithm extension |
1367 |
if (maxProtocolVersion.v >= ProtocolVersion.TLS12.v) { |
|
1368 |
// we will always send the signature_algorithm extension |
|
1369 |
Collection<SignatureAndHashAlgorithm> localSignAlgs = |
|
1370 |
getLocalSupportedSignAlgs(); |
|
1371 |
if (localSignAlgs.isEmpty()) { |
|
1372 |
throw new SSLHandshakeException( |
|
1373 |
"No supported signature algorithm"); |
|
1374 |
} |
|
1375 |
||
1376 |
clientHelloMessage.addSignatureAlgorithmsExtension(localSignAlgs); |
|
1377 |
} |
|
1378 |
||
1379 |
// add server_name extension |
|
1380 |
if (enableSNIExtension) { |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
11521
diff
changeset
|
1381 |
if (session != null) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
11521
diff
changeset
|
1382 |
requestedServerNames = session.getRequestedServerNames(); |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
11521
diff
changeset
|
1383 |
} else { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
11521
diff
changeset
|
1384 |
requestedServerNames = serverNames; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
11521
diff
changeset
|
1385 |
} |
7043 | 1386 |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
11521
diff
changeset
|
1387 |
if (!requestedServerNames.isEmpty()) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
11521
diff
changeset
|
1388 |
clientHelloMessage.addSNIExtension(requestedServerNames); |
7043 | 1389 |
} |
1390 |
} |
|
1391 |
||
6856 | 1392 |
// reset the client random cookie |
1393 |
clnt_random = clientHelloMessage.clnt_random; |
|
1394 |
||
1395 |
/* |
|
1396 |
* need to set the renegotiation_info extension for: |
|
1397 |
* 1: secure renegotiation |
|
1398 |
* 2: initial handshake and no SCSV in the ClientHello |
|
1399 |
* 3: insecure renegotiation and no SCSV in the ClientHello |
|
1400 |
*/ |
|
1401 |
if (secureRenegotiation || |
|
1402 |
!cipherSuites.contains(CipherSuite.C_SCSV)) { |
|
1403 |
clientHelloMessage.addRenegotiationInfoExtension(clientVerifyData); |
|
1404 |
} |
|
1405 |
||
1406 |
return clientHelloMessage; |
|
2 | 1407 |
} |
1408 |
||
1409 |
/* |
|
1410 |
* Fault detected during handshake. |
|
1411 |
*/ |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14194
diff
changeset
|
1412 |
@Override |
2 | 1413 |
void handshakeAlert(byte description) throws SSLProtocolException { |
1414 |
String message = Alerts.alertDescription(description); |
|
1415 |
||
1416 |
if (debug != null && Debug.isOn("handshake")) { |
|
1417 |
System.out.println("SSL - handshake alert: " + message); |
|
1418 |
} |
|
1419 |
throw new SSLProtocolException("handshake alert: " + message); |
|
1420 |
} |
|
1421 |
||
1422 |
/* |
|
1423 |
* Unless we are using an anonymous ciphersuite, the server always |
|
1424 |
* sends a certificate message (for the CipherSuites we currently |
|
1425 |
* support). The trust manager verifies the chain for us. |
|
1426 |
*/ |
|
1427 |
private void serverCertificate(CertificateMsg mesg) throws IOException { |
|
1428 |
if (debug != null && Debug.isOn("handshake")) { |
|
1429 |
mesg.print(System.out); |
|
1430 |
} |
|
1431 |
X509Certificate[] peerCerts = mesg.getCertificateChain(); |
|
1432 |
if (peerCerts.length == 0) { |
|
27068 | 1433 |
fatalSE(Alerts.alert_bad_certificate, "empty certificate chain"); |
2 | 1434 |
} |
27068 | 1435 |
|
1436 |
// Allow server certificate change in client side during renegotiation |
|
1437 |
// after a session-resumption abbreviated initial handshake? |
|
1438 |
// |
|
1439 |
// DO NOT need to check allowUnsafeServerCertChange here. We only |
|
1440 |
// reserve server certificates when allowUnsafeServerCertChange is |
|
1441 |
// flase. |
|
1442 |
if (reservedServerCerts != null) { |
|
1443 |
// It is not necessary to check the certificate update if endpoint |
|
1444 |
// identification is enabled. |
|
1445 |
String identityAlg = getEndpointIdentificationAlgorithmSE(); |
|
1446 |
if ((identityAlg == null || identityAlg.length() == 0) && |
|
1447 |
!isIdentityEquivalent(peerCerts[0], reservedServerCerts[0])) { |
|
1448 |
||
1449 |
fatalSE(Alerts.alert_bad_certificate, |
|
1450 |
"server certificate change is restricted " + |
|
1451 |
"during renegotiation"); |
|
1452 |
} |
|
1453 |
} |
|
1454 |
||
2 | 1455 |
// ask the trust manager to verify the chain |
1456 |
X509TrustManager tm = sslContext.getX509TrustManager(); |
|
1457 |
try { |
|
1458 |
// find out the key exchange algorithm used |
|
1459 |
// use "RSA" for non-ephemeral "RSA_EXPORT" |
|
1460 |
String keyExchangeString; |
|
1461 |
if (keyExchange == K_RSA_EXPORT && !serverKeyExchangeReceived) { |
|
1462 |
keyExchangeString = K_RSA.name; |
|
1463 |
} else { |
|
1464 |
keyExchangeString = keyExchange.name; |
|
1465 |
} |
|
1466 |
||
1467 |
if (tm instanceof X509ExtendedTrustManager) { |
|
7043 | 1468 |
if (conn != null) { |
1469 |
((X509ExtendedTrustManager)tm).checkServerTrusted( |
|
1470 |
peerCerts.clone(), |
|
2 | 1471 |
keyExchangeString, |
7043 | 1472 |
conn); |
1473 |
} else { |
|
1474 |
((X509ExtendedTrustManager)tm).checkServerTrusted( |
|
1475 |
peerCerts.clone(), |
|
1476 |
keyExchangeString, |
|
1477 |
engine); |
|
1478 |
} |
|
2 | 1479 |
} else { |
7043 | 1480 |
// Unlikely to happen, because we have wrapped the old |
1481 |
// X509TrustManager with the new X509ExtendedTrustManager. |
|
1482 |
throw new CertificateException( |
|
1483 |
"Improper X509TrustManager implementation"); |
|
2 | 1484 |
} |
1485 |
} catch (CertificateException e) { |
|
1486 |
// This will throw an exception, so include the original error. |
|
1487 |
fatalSE(Alerts.alert_certificate_unknown, e); |
|
1488 |
} |
|
1489 |
session.setPeerCertificates(peerCerts); |
|
1490 |
} |
|
27068 | 1491 |
|
1492 |
/* |
|
1493 |
* Whether the certificates can represent the same identity? |
|
1494 |
* |
|
1495 |
* The certificates can be used to represent the same identity: |
|
1496 |
* 1. If the subject alternative names of IP address are present in |
|
1497 |
* both certificates, they should be identical; otherwise, |
|
1498 |
* 2. if the subject alternative names of DNS name are present in |
|
1499 |
* both certificates, they should be identical; otherwise, |
|
1500 |
* 3. if the subject fields are present in both certificates, the |
|
1501 |
* certificate subjects and issuers should be identical. |
|
1502 |
*/ |
|
1503 |
private static boolean isIdentityEquivalent(X509Certificate thisCert, |
|
1504 |
X509Certificate prevCert) { |
|
1505 |
if (thisCert.equals(prevCert)) { |
|
1506 |
return true; |
|
1507 |
} |
|
1508 |
||
29266 | 1509 |
// check subject alternative names |
1510 |
Collection<List<?>> thisSubjectAltNames = null; |
|
1511 |
try { |
|
1512 |
thisSubjectAltNames = thisCert.getSubjectAlternativeNames(); |
|
1513 |
} catch (CertificateParsingException cpe) { |
|
1514 |
if (debug != null && Debug.isOn("handshake")) { |
|
1515 |
System.out.println( |
|
1516 |
"Attempt to obtain subjectAltNames extension failed!"); |
|
1517 |
} |
|
1518 |
} |
|
1519 |
||
1520 |
Collection<List<?>> prevSubjectAltNames = null; |
|
1521 |
try { |
|
1522 |
prevSubjectAltNames = prevCert.getSubjectAlternativeNames(); |
|
1523 |
} catch (CertificateParsingException cpe) { |
|
1524 |
if (debug != null && Debug.isOn("handshake")) { |
|
1525 |
System.out.println( |
|
1526 |
"Attempt to obtain subjectAltNames extension failed!"); |
|
1527 |
} |
|
27068 | 1528 |
} |
1529 |
||
29266 | 1530 |
if ((thisSubjectAltNames != null) && (prevSubjectAltNames != null)) { |
1531 |
// check the iPAddress field in subjectAltName extension |
|
1532 |
Collection<String> thisSubAltIPAddrs = |
|
1533 |
getSubjectAltNames(thisSubjectAltNames, ALTNAME_IP); |
|
1534 |
Collection<String> prevSubAltIPAddrs = |
|
1535 |
getSubjectAltNames(prevSubjectAltNames, ALTNAME_IP); |
|
1536 |
if ((thisSubAltIPAddrs != null) && (prevSubAltIPAddrs != null) && |
|
1537 |
(isEquivalent(thisSubAltIPAddrs, prevSubAltIPAddrs))) { |
|
1538 |
||
1539 |
return true; |
|
1540 |
} |
|
1541 |
||
1542 |
// check the dNSName field in subjectAltName extension |
|
1543 |
Collection<String> thisSubAltDnsNames = |
|
1544 |
getSubjectAltNames(thisSubjectAltNames, ALTNAME_DNS); |
|
1545 |
Collection<String> prevSubAltDnsNames = |
|
1546 |
getSubjectAltNames(prevSubjectAltNames, ALTNAME_DNS); |
|
1547 |
if ((thisSubAltDnsNames != null) && (prevSubAltDnsNames != null) && |
|
1548 |
(isEquivalent(thisSubAltDnsNames, prevSubAltDnsNames))) { |
|
1549 |
||
1550 |
return true; |
|
1551 |
} |
|
27068 | 1552 |
} |
1553 |
||
1554 |
// check the certificate subject and issuer |
|
1555 |
X500Principal thisSubject = thisCert.getSubjectX500Principal(); |
|
1556 |
X500Principal prevSubject = prevCert.getSubjectX500Principal(); |
|
1557 |
X500Principal thisIssuer = thisCert.getIssuerX500Principal(); |
|
1558 |
X500Principal prevIssuer = prevCert.getIssuerX500Principal(); |
|
1559 |
if (!thisSubject.getName().isEmpty() && |
|
1560 |
!prevSubject.getName().isEmpty() && |
|
1561 |
thisSubject.equals(prevSubject) && |
|
1562 |
thisIssuer.equals(prevIssuer)) { |
|
1563 |
return true; |
|
1564 |
} |
|
1565 |
||
1566 |
return false; |
|
1567 |
} |
|
1568 |
||
1569 |
/* |
|
1570 |
* Returns the subject alternative name of the specified type in the |
|
1571 |
* subjectAltNames extension of a certificate. |
|
1572 |
*/ |
|
29266 | 1573 |
private static Collection<String> getSubjectAltNames( |
1574 |
Collection<List<?>> subjectAltNames, int type) { |
|
27068 | 1575 |
|
29266 | 1576 |
HashSet<String> subAltDnsNames = null; |
1577 |
for (List<?> subjectAltName : subjectAltNames) { |
|
1578 |
int subjectAltNameType = (Integer)subjectAltName.get(0); |
|
1579 |
if (subjectAltNameType == type) { |
|
1580 |
String subAltDnsName = (String)subjectAltName.get(1); |
|
1581 |
if ((subAltDnsName != null) && !subAltDnsName.isEmpty()) { |
|
1582 |
if (subAltDnsNames == null) { |
|
1583 |
subAltDnsNames = |
|
1584 |
new HashSet<>(subjectAltNames.size()); |
|
1585 |
} |
|
1586 |
subAltDnsNames.add(subAltDnsName); |
|
27068 | 1587 |
} |
1588 |
} |
|
1589 |
} |
|
1590 |
||
29266 | 1591 |
return subAltDnsNames; |
1592 |
} |
|
1593 |
||
1594 |
private static boolean isEquivalent(Collection<String> thisSubAltNames, |
|
1595 |
Collection<String> prevSubAltNames) { |
|
1596 |
||
1597 |
for (String thisSubAltName : thisSubAltNames) { |
|
1598 |
for (String prevSubAltName : prevSubAltNames) { |
|
1599 |
// Only allow the exactly match. Check no wildcard character. |
|
1600 |
if (thisSubAltName.equalsIgnoreCase(prevSubAltName)) { |
|
1601 |
return true; |
|
1602 |
} |
|
1603 |
} |
|
1604 |
} |
|
1605 |
||
1606 |
return false; |
|
27068 | 1607 |
} |
2 | 1608 |
} |