author | xuelei |
Sat, 09 Jul 2016 05:56:18 +0000 | |
changeset 39563 | 1449ed425710 |
parent 35298 | 9f93cbce8c44 |
child 42706 | 796cf076d69b |
permissions | -rw-r--r-- |
2 | 1 |
/* |
39563
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
2 |
* Copyright (c) 1996, 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 |
||
29 |
import java.io.*; |
|
30 |
import java.util.*; |
|
31 |
import java.security.*; |
|
30904 | 32 |
import java.nio.ByteBuffer; |
2 | 33 |
import java.security.NoSuchAlgorithmException; |
34 |
import java.security.AccessController; |
|
7043 | 35 |
import java.security.AlgorithmConstraints; |
2 | 36 |
import java.security.AccessControlContext; |
37 |
import java.security.PrivilegedExceptionAction; |
|
38 |
import java.security.PrivilegedActionException; |
|
39 |
||
40 |
import javax.crypto.*; |
|
41 |
import javax.crypto.spec.*; |
|
42 |
||
43 |
import javax.net.ssl.*; |
|
34687
d302ed125dc9
8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents:
34380
diff
changeset
|
44 |
import sun.security.util.HexDumpEncoder; |
2 | 45 |
|
46 |
import sun.security.internal.spec.*; |
|
47 |
import sun.security.internal.interfaces.TlsMasterSecret; |
|
48 |
||
49 |
import sun.security.ssl.HandshakeMessage.*; |
|
50 |
import sun.security.ssl.CipherSuite.*; |
|
51 |
||
7043 | 52 |
import static sun.security.ssl.CipherSuite.PRF.*; |
16913 | 53 |
import static sun.security.ssl.CipherSuite.CipherType.*; |
7043 | 54 |
|
2 | 55 |
/** |
56 |
* Handshaker ... processes handshake records from an SSL V3.0 |
|
57 |
* data stream, handling all the details of the handshake protocol. |
|
58 |
* |
|
59 |
* Note that the real protocol work is done in two subclasses, the base |
|
60 |
* class just provides the control flow and key generation framework. |
|
61 |
* |
|
62 |
* @author David Brownell |
|
63 |
*/ |
|
64 |
abstract class Handshaker { |
|
65 |
||
5182 | 66 |
// protocol version being established using this Handshaker |
2 | 67 |
ProtocolVersion protocolVersion; |
68 |
||
5182 | 69 |
// the currently active protocol version during a renegotiation |
70 |
ProtocolVersion activeProtocolVersion; |
|
71 |
||
6856 | 72 |
// security parameters for secure renegotiation. |
73 |
boolean secureRenegotiation; |
|
74 |
byte[] clientVerifyData; |
|
75 |
byte[] serverVerifyData; |
|
76 |
||
7039 | 77 |
// Is it an initial negotiation or a renegotiation? |
6856 | 78 |
boolean isInitialHandshake; |
79 |
||
7039 | 80 |
// List of enabled protocols |
81 |
private ProtocolList enabledProtocols; |
|
82 |
||
83 |
// List of enabled CipherSuites |
|
84 |
private CipherSuiteList enabledCipherSuites; |
|
85 |
||
7043 | 86 |
// The endpoint identification protocol |
30904 | 87 |
String identificationProtocol; |
7043 | 88 |
|
89 |
// The cryptographic algorithm constraints |
|
31712 | 90 |
AlgorithmConstraints algorithmConstraints = null; |
7043 | 91 |
|
92 |
// Local supported signature and algorithms |
|
35298 | 93 |
private Collection<SignatureAndHashAlgorithm> localSupportedSignAlgs; |
7043 | 94 |
|
95 |
// Peer supported signature and algorithms |
|
96 |
Collection<SignatureAndHashAlgorithm> peerSupportedSignAlgs; |
|
97 |
||
98 |
/* |
|
7039 | 99 |
* List of active protocols |
100 |
* |
|
101 |
* Active protocols is a subset of enabled protocols, and will |
|
102 |
* contain only those protocols that have vaild cipher suites |
|
103 |
* enabled. |
|
104 |
*/ |
|
105 |
private ProtocolList activeProtocols; |
|
106 |
||
107 |
/* |
|
108 |
* List of active cipher suites |
|
109 |
* |
|
110 |
* Active cipher suites is a subset of enabled cipher suites, and will |
|
111 |
* contain only those cipher suites available for the active protocols. |
|
112 |
*/ |
|
30904 | 113 |
private CipherSuiteList activeCipherSuites; |
2 | 114 |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
115 |
// The server name indication and matchers |
28543 | 116 |
List<SNIServerName> serverNames = Collections.<SNIServerName>emptyList(); |
117 |
Collection<SNIMatcher> sniMatchers = Collections.<SNIMatcher>emptyList(); |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
118 |
|
34380
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32649
diff
changeset
|
119 |
// List of local ApplicationProtocols |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32649
diff
changeset
|
120 |
String[] localApl = null; |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32649
diff
changeset
|
121 |
|
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32649
diff
changeset
|
122 |
// Negotiated ALPN value |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32649
diff
changeset
|
123 |
String applicationProtocol = null; |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32649
diff
changeset
|
124 |
|
30904 | 125 |
// The maximum expected network packet size for SSL/TLS/DTLS records. |
126 |
int maximumPacketSize = 0; |
|
127 |
||
2 | 128 |
private boolean isClient; |
7043 | 129 |
private boolean needCertVerify; |
2 | 130 |
|
131 |
SSLSocketImpl conn = null; |
|
132 |
SSLEngineImpl engine = null; |
|
133 |
||
134 |
HandshakeHash handshakeHash; |
|
135 |
HandshakeInStream input; |
|
136 |
HandshakeOutStream output; |
|
137 |
SSLContextImpl sslContext; |
|
138 |
RandomCookie clnt_random, svr_random; |
|
139 |
SSLSessionImpl session; |
|
140 |
||
30904 | 141 |
HandshakeStateManager handshakeState; |
142 |
boolean clientHelloDelivered; |
|
143 |
boolean serverHelloRequested; |
|
144 |
boolean handshakeActivated; |
|
145 |
boolean handshakeFinished; |
|
146 |
||
2 | 147 |
// current CipherSuite. Never null, initially SSL_NULL_WITH_NULL_NULL |
148 |
CipherSuite cipherSuite; |
|
149 |
||
150 |
// current key exchange. Never null, initially K_NULL |
|
151 |
KeyExchange keyExchange; |
|
152 |
||
28543 | 153 |
// True if this session is being resumed (fast handshake) |
2 | 154 |
boolean resumingSession; |
155 |
||
28543 | 156 |
// True if it's OK to start a new SSL session |
2 | 157 |
boolean enableNewSession; |
158 |
||
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18554
diff
changeset
|
159 |
// Whether local cipher suites preference should be honored during |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18554
diff
changeset
|
160 |
// handshaking? |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18554
diff
changeset
|
161 |
// |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18554
diff
changeset
|
162 |
// Note that in this provider, this option only applies to server side. |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18554
diff
changeset
|
163 |
// Local cipher suites preference is always honored in client side in |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18554
diff
changeset
|
164 |
// this provider. |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18554
diff
changeset
|
165 |
boolean preferLocalCipherSuites = false; |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18554
diff
changeset
|
166 |
|
2 | 167 |
// Temporary storage for the individual keys. Set by |
168 |
// calculateConnectionKeys() and cleared once the ciphers are |
|
169 |
// activated. |
|
170 |
private SecretKey clntWriteKey, svrWriteKey; |
|
171 |
private IvParameterSpec clntWriteIV, svrWriteIV; |
|
172 |
private SecretKey clntMacSecret, svrMacSecret; |
|
173 |
||
174 |
/* |
|
175 |
* Delegated task subsystem data structures. |
|
176 |
* |
|
177 |
* If thrown is set, we need to propagate this back immediately |
|
178 |
* on entry into processMessage(). |
|
179 |
* |
|
180 |
* Data is protected by the SSLEngine.this lock. |
|
181 |
*/ |
|
182 |
private volatile boolean taskDelegated = false; |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
183 |
private volatile DelegatedTask<?> delegatedTask = null; |
2 | 184 |
private volatile Exception thrown = null; |
185 |
||
186 |
// Could probably use a java.util.concurrent.atomic.AtomicReference |
|
187 |
// here instead of using this lock. Consider changing. |
|
188 |
private Object thrownLock = new Object(); |
|
189 |
||
190 |
/* Class and subclass dynamic debugging support */ |
|
191 |
static final Debug debug = Debug.getInstance("ssl"); |
|
192 |
||
5182 | 193 |
// By default, disable the unsafe legacy session renegotiation |
6856 | 194 |
static final boolean allowUnsafeRenegotiation = Debug.getBooleanProperty( |
5182 | 195 |
"sun.security.ssl.allowUnsafeRenegotiation", false); |
196 |
||
6856 | 197 |
// For maximum interoperability and backward compatibility, RFC 5746 |
198 |
// allows server (or client) to accept ClientHello (or ServerHello) |
|
199 |
// message without the secure renegotiation_info extension or SCSV. |
|
200 |
// |
|
201 |
// For maximum security, RFC 5746 also allows server (or client) to |
|
202 |
// reject such message with a fatal "handshake_failure" alert. |
|
203 |
// |
|
204 |
// By default, allow such legacy hello messages. |
|
205 |
static final boolean allowLegacyHelloMessages = Debug.getBooleanProperty( |
|
206 |
"sun.security.ssl.allowLegacyHelloMessages", true); |
|
207 |
||
18283
f842a42076b9
7188658: Add possibility to disable client initiated renegotiation
xuelei
parents:
16913
diff
changeset
|
208 |
// To prevent the TLS renegotiation issues, by setting system property |
18554
d2f655022d2d
8017049: rename property jdk.tls.rejectClientInitializedRenego
xuelei
parents:
18283
diff
changeset
|
209 |
// "jdk.tls.rejectClientInitiatedRenegotiation" to true, applications in |
d2f655022d2d
8017049: rename property jdk.tls.rejectClientInitializedRenego
xuelei
parents:
18283
diff
changeset
|
210 |
// server side can disable all client initiated SSL renegotiations |
d2f655022d2d
8017049: rename property jdk.tls.rejectClientInitializedRenego
xuelei
parents:
18283
diff
changeset
|
211 |
// regardless of the support of TLS protocols. |
18283
f842a42076b9
7188658: Add possibility to disable client initiated renegotiation
xuelei
parents:
16913
diff
changeset
|
212 |
// |
f842a42076b9
7188658: Add possibility to disable client initiated renegotiation
xuelei
parents:
16913
diff
changeset
|
213 |
// By default, allow client initiated renegotiations. |
f842a42076b9
7188658: Add possibility to disable client initiated renegotiation
xuelei
parents:
16913
diff
changeset
|
214 |
static final boolean rejectClientInitiatedRenego = |
f842a42076b9
7188658: Add possibility to disable client initiated renegotiation
xuelei
parents:
16913
diff
changeset
|
215 |
Debug.getBooleanProperty( |
18554
d2f655022d2d
8017049: rename property jdk.tls.rejectClientInitializedRenego
xuelei
parents:
18283
diff
changeset
|
216 |
"jdk.tls.rejectClientInitiatedRenegotiation", false); |
18283
f842a42076b9
7188658: Add possibility to disable client initiated renegotiation
xuelei
parents:
16913
diff
changeset
|
217 |
|
5182 | 218 |
// need to dispose the object when it is invalidated |
219 |
boolean invalidated; |
|
220 |
||
30904 | 221 |
/* |
222 |
* Is this an instance for Datagram Transport Layer Security (DTLS)? |
|
223 |
*/ |
|
224 |
final boolean isDTLS; |
|
225 |
||
2 | 226 |
Handshaker(SSLSocketImpl c, SSLContextImpl context, |
227 |
ProtocolList enabledProtocols, boolean needCertVerify, |
|
6856 | 228 |
boolean isClient, ProtocolVersion activeProtocolVersion, |
229 |
boolean isInitialHandshake, boolean secureRenegotiation, |
|
230 |
byte[] clientVerifyData, byte[] serverVerifyData) { |
|
2 | 231 |
this.conn = c; |
30904 | 232 |
this.isDTLS = false; |
6856 | 233 |
init(context, enabledProtocols, needCertVerify, isClient, |
234 |
activeProtocolVersion, isInitialHandshake, secureRenegotiation, |
|
235 |
clientVerifyData, serverVerifyData); |
|
2 | 236 |
} |
237 |
||
238 |
Handshaker(SSLEngineImpl engine, SSLContextImpl context, |
|
239 |
ProtocolList enabledProtocols, boolean needCertVerify, |
|
6856 | 240 |
boolean isClient, ProtocolVersion activeProtocolVersion, |
241 |
boolean isInitialHandshake, boolean secureRenegotiation, |
|
30904 | 242 |
byte[] clientVerifyData, byte[] serverVerifyData, |
243 |
boolean isDTLS) { |
|
2 | 244 |
this.engine = engine; |
30904 | 245 |
this.isDTLS = isDTLS; |
6856 | 246 |
init(context, enabledProtocols, needCertVerify, isClient, |
247 |
activeProtocolVersion, isInitialHandshake, secureRenegotiation, |
|
248 |
clientVerifyData, serverVerifyData); |
|
2 | 249 |
} |
250 |
||
251 |
private void init(SSLContextImpl context, ProtocolList enabledProtocols, |
|
6856 | 252 |
boolean needCertVerify, boolean isClient, |
253 |
ProtocolVersion activeProtocolVersion, |
|
254 |
boolean isInitialHandshake, boolean secureRenegotiation, |
|
255 |
byte[] clientVerifyData, byte[] serverVerifyData) { |
|
256 |
||
257 |
if (debug != null && Debug.isOn("handshake")) { |
|
258 |
System.out.println( |
|
259 |
"Allow unsafe renegotiation: " + allowUnsafeRenegotiation + |
|
260 |
"\nAllow legacy hello messages: " + allowLegacyHelloMessages + |
|
261 |
"\nIs initial handshake: " + isInitialHandshake + |
|
262 |
"\nIs secure renegotiation: " + secureRenegotiation); |
|
263 |
} |
|
2 | 264 |
|
265 |
this.sslContext = context; |
|
266 |
this.isClient = isClient; |
|
7043 | 267 |
this.needCertVerify = needCertVerify; |
6856 | 268 |
this.activeProtocolVersion = activeProtocolVersion; |
269 |
this.isInitialHandshake = isInitialHandshake; |
|
270 |
this.secureRenegotiation = secureRenegotiation; |
|
271 |
this.clientVerifyData = clientVerifyData; |
|
272 |
this.serverVerifyData = serverVerifyData; |
|
30904 | 273 |
this.enableNewSession = true; |
274 |
this.invalidated = false; |
|
275 |
this.handshakeState = new HandshakeStateManager(isDTLS); |
|
276 |
this.clientHelloDelivered = false; |
|
277 |
this.serverHelloRequested = false; |
|
278 |
this.handshakeActivated = false; |
|
279 |
this.handshakeFinished = false; |
|
2 | 280 |
|
281 |
setCipherSuite(CipherSuite.C_NULL); |
|
282 |
setEnabledProtocols(enabledProtocols); |
|
283 |
||
284 |
if (conn != null) { |
|
7043 | 285 |
algorithmConstraints = new SSLAlgorithmConstraints(conn, true); |
2 | 286 |
} else { // engine != null |
7043 | 287 |
algorithmConstraints = new SSLAlgorithmConstraints(engine, true); |
2 | 288 |
} |
289 |
} |
|
290 |
||
291 |
/* |
|
292 |
* Reroutes calls to the SSLSocket or SSLEngine (*SE). |
|
293 |
* |
|
294 |
* We could have also done it by extra classes |
|
295 |
* and letting them override, but this seemed much |
|
296 |
* less involved. |
|
297 |
*/ |
|
298 |
void fatalSE(byte b, String diagnostic) throws IOException { |
|
299 |
fatalSE(b, diagnostic, null); |
|
300 |
} |
|
301 |
||
302 |
void fatalSE(byte b, Throwable cause) throws IOException { |
|
303 |
fatalSE(b, null, cause); |
|
304 |
} |
|
305 |
||
306 |
void fatalSE(byte b, String diagnostic, Throwable cause) |
|
307 |
throws IOException { |
|
308 |
if (conn != null) { |
|
309 |
conn.fatal(b, diagnostic, cause); |
|
310 |
} else { |
|
311 |
engine.fatal(b, diagnostic, cause); |
|
312 |
} |
|
313 |
} |
|
314 |
||
315 |
void warningSE(byte b) { |
|
316 |
if (conn != null) { |
|
317 |
conn.warning(b); |
|
318 |
} else { |
|
319 |
engine.warning(b); |
|
320 |
} |
|
321 |
} |
|
322 |
||
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
323 |
// ONLY used by ClientHandshaker to setup the peer host in SSLSession. |
2 | 324 |
String getHostSE() { |
325 |
if (conn != null) { |
|
326 |
return conn.getHost(); |
|
327 |
} else { |
|
328 |
return engine.getPeerHost(); |
|
329 |
} |
|
330 |
} |
|
331 |
||
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
332 |
// ONLY used by ServerHandshaker to setup the peer host in SSLSession. |
2 | 333 |
String getHostAddressSE() { |
334 |
if (conn != null) { |
|
335 |
return conn.getInetAddress().getHostAddress(); |
|
336 |
} else { |
|
337 |
/* |
|
338 |
* This is for caching only, doesn't matter that's is really |
|
339 |
* a hostname. The main thing is that it doesn't do |
|
340 |
* a reverse DNS lookup, potentially slowing things down. |
|
341 |
*/ |
|
342 |
return engine.getPeerHost(); |
|
343 |
} |
|
344 |
} |
|
345 |
||
346 |
int getPortSE() { |
|
347 |
if (conn != null) { |
|
348 |
return conn.getPort(); |
|
349 |
} else { |
|
350 |
return engine.getPeerPort(); |
|
351 |
} |
|
352 |
} |
|
353 |
||
354 |
int getLocalPortSE() { |
|
355 |
if (conn != null) { |
|
356 |
return conn.getLocalPort(); |
|
357 |
} else { |
|
358 |
return -1; |
|
359 |
} |
|
360 |
} |
|
361 |
||
362 |
AccessControlContext getAccSE() { |
|
363 |
if (conn != null) { |
|
364 |
return conn.getAcc(); |
|
365 |
} else { |
|
366 |
return engine.getAcc(); |
|
367 |
} |
|
368 |
} |
|
369 |
||
27068 | 370 |
String getEndpointIdentificationAlgorithmSE() { |
371 |
SSLParameters paras; |
|
372 |
if (conn != null) { |
|
373 |
paras = conn.getSSLParameters(); |
|
374 |
} else { |
|
375 |
paras = engine.getSSLParameters(); |
|
376 |
} |
|
377 |
||
378 |
return paras.getEndpointIdentificationAlgorithm(); |
|
379 |
} |
|
380 |
||
2 | 381 |
private void setVersionSE(ProtocolVersion protocolVersion) { |
382 |
if (conn != null) { |
|
383 |
conn.setVersion(protocolVersion); |
|
384 |
} else { |
|
385 |
engine.setVersion(protocolVersion); |
|
386 |
} |
|
387 |
} |
|
388 |
||
389 |
/** |
|
390 |
* Set the active protocol version and propagate it to the SSLSocket |
|
391 |
* and our handshake streams. Called from ClientHandshaker |
|
392 |
* and ServerHandshaker with the negotiated protocol version. |
|
393 |
*/ |
|
394 |
void setVersion(ProtocolVersion protocolVersion) { |
|
395 |
this.protocolVersion = protocolVersion; |
|
396 |
setVersionSE(protocolVersion); |
|
397 |
} |
|
398 |
||
399 |
/** |
|
400 |
* Set the enabled protocols. Called from the constructor or |
|
7039 | 401 |
* SSLSocketImpl/SSLEngineImpl.setEnabledProtocols() (if the |
402 |
* handshake is not yet in progress). |
|
2 | 403 |
*/ |
404 |
void setEnabledProtocols(ProtocolList enabledProtocols) { |
|
7039 | 405 |
activeCipherSuites = null; |
406 |
activeProtocols = null; |
|
407 |
||
2 | 408 |
this.enabledProtocols = enabledProtocols; |
7039 | 409 |
} |
410 |
||
411 |
/** |
|
412 |
* Set the enabled cipher suites. Called from |
|
413 |
* SSLSocketImpl/SSLEngineImpl.setEnabledCipherSuites() (if the |
|
414 |
* handshake is not yet in progress). |
|
415 |
*/ |
|
416 |
void setEnabledCipherSuites(CipherSuiteList enabledCipherSuites) { |
|
417 |
activeCipherSuites = null; |
|
418 |
activeProtocols = null; |
|
419 |
this.enabledCipherSuites = enabledCipherSuites; |
|
420 |
} |
|
421 |
||
7043 | 422 |
/** |
423 |
* Set the algorithm constraints. Called from the constructor or |
|
424 |
* SSLSocketImpl/SSLEngineImpl.setAlgorithmConstraints() (if the |
|
425 |
* handshake is not yet in progress). |
|
426 |
*/ |
|
427 |
void setAlgorithmConstraints(AlgorithmConstraints algorithmConstraints) { |
|
428 |
activeCipherSuites = null; |
|
429 |
activeProtocols = null; |
|
430 |
||
431 |
this.algorithmConstraints = |
|
432 |
new SSLAlgorithmConstraints(algorithmConstraints); |
|
433 |
this.localSupportedSignAlgs = null; |
|
434 |
} |
|
435 |
||
436 |
Collection<SignatureAndHashAlgorithm> getLocalSupportedSignAlgs() { |
|
437 |
if (localSupportedSignAlgs == null) { |
|
438 |
localSupportedSignAlgs = |
|
439 |
SignatureAndHashAlgorithm.getSupportedAlgorithms( |
|
440 |
algorithmConstraints); |
|
441 |
} |
|
442 |
||
443 |
return localSupportedSignAlgs; |
|
444 |
} |
|
445 |
||
446 |
void setPeerSupportedSignAlgs( |
|
447 |
Collection<SignatureAndHashAlgorithm> algorithms) { |
|
448 |
peerSupportedSignAlgs = |
|
449 |
new ArrayList<SignatureAndHashAlgorithm>(algorithms); |
|
450 |
} |
|
451 |
||
452 |
Collection<SignatureAndHashAlgorithm> getPeerSupportedSignAlgs() { |
|
453 |
return peerSupportedSignAlgs; |
|
454 |
} |
|
455 |
||
456 |
||
457 |
/** |
|
458 |
* Set the identification protocol. Called from the constructor or |
|
459 |
* SSLSocketImpl/SSLEngineImpl.setIdentificationProtocol() (if the |
|
460 |
* handshake is not yet in progress). |
|
461 |
*/ |
|
462 |
void setIdentificationProtocol(String protocol) { |
|
463 |
this.identificationProtocol = protocol; |
|
464 |
} |
|
7039 | 465 |
|
466 |
/** |
|
14194
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
467 |
* Sets the server name indication of the handshake. |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
468 |
*/ |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
469 |
void setSNIServerNames(List<SNIServerName> serverNames) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
470 |
// The serverNames parameter is unmodifiable. |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
471 |
this.serverNames = serverNames; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
472 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
473 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
474 |
/** |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
475 |
* Sets the server name matchers of the handshaking. |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
476 |
*/ |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
477 |
void setSNIMatchers(Collection<SNIMatcher> sniMatchers) { |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
478 |
// The sniMatchers parameter is unmodifiable. |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
479 |
this.sniMatchers = sniMatchers; |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
480 |
} |
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
481 |
|
971f46db533d
7068321: Support TLS Server Name Indication (SNI) Extension in JSSE Server
xuelei
parents:
10336
diff
changeset
|
482 |
/** |
30904 | 483 |
* Sets the maximum packet size of the handshaking. |
484 |
*/ |
|
485 |
void setMaximumPacketSize(int maximumPacketSize) { |
|
486 |
this.maximumPacketSize = maximumPacketSize; |
|
487 |
} |
|
488 |
||
489 |
/** |
|
34380
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32649
diff
changeset
|
490 |
* Sets the Application Protocol list. |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32649
diff
changeset
|
491 |
*/ |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32649
diff
changeset
|
492 |
void setApplicationProtocols(String[] apl) { |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32649
diff
changeset
|
493 |
this.localApl = apl; |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32649
diff
changeset
|
494 |
} |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32649
diff
changeset
|
495 |
|
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32649
diff
changeset
|
496 |
/** |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32649
diff
changeset
|
497 |
* Gets the "negotiated" ALPN value. |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32649
diff
changeset
|
498 |
*/ |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32649
diff
changeset
|
499 |
String getHandshakeApplicationProtocol() { |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32649
diff
changeset
|
500 |
return applicationProtocol; |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32649
diff
changeset
|
501 |
} |
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32649
diff
changeset
|
502 |
|
2b2609379881
8144093: JEP 244/8051498 - TLS Application-Layer Protocol Negotiation Extension
vinnie
parents:
32649
diff
changeset
|
503 |
/** |
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18554
diff
changeset
|
504 |
* Sets the cipher suites preference. |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18554
diff
changeset
|
505 |
*/ |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18554
diff
changeset
|
506 |
void setUseCipherSuitesOrder(boolean on) { |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18554
diff
changeset
|
507 |
this.preferLocalCipherSuites = on; |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18554
diff
changeset
|
508 |
} |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18554
diff
changeset
|
509 |
|
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18554
diff
changeset
|
510 |
/** |
7039 | 511 |
* Prior to handshaking, activate the handshake and initialize the version, |
512 |
* input stream and output stream. |
|
513 |
*/ |
|
514 |
void activate(ProtocolVersion helloVersion) throws IOException { |
|
515 |
if (activeProtocols == null) { |
|
516 |
activeProtocols = getActiveProtocols(); |
|
517 |
} |
|
518 |
||
519 |
if (activeProtocols.collection().isEmpty() || |
|
520 |
activeProtocols.max.v == ProtocolVersion.NONE.v) { |
|
28555 | 521 |
throw new SSLHandshakeException( |
522 |
"No appropriate protocol (protocol is disabled or " + |
|
523 |
"cipher suites are inappropriate)"); |
|
7039 | 524 |
} |
525 |
||
526 |
if (activeCipherSuites == null) { |
|
527 |
activeCipherSuites = getActiveCipherSuites(); |
|
528 |
} |
|
529 |
||
530 |
if (activeCipherSuites.collection().isEmpty()) { |
|
531 |
throw new SSLHandshakeException("No appropriate cipher suite"); |
|
532 |
} |
|
2 | 533 |
|
534 |
// temporary protocol version until the actual protocol version |
|
535 |
// is negotiated in the Hello exchange. This affects the record |
|
7039 | 536 |
// version we sent with the ClientHello. |
537 |
if (!isInitialHandshake) { |
|
538 |
protocolVersion = activeProtocolVersion; |
|
539 |
} else { |
|
540 |
protocolVersion = activeProtocols.max; |
|
541 |
} |
|
2 | 542 |
|
7039 | 543 |
if (helloVersion == null || helloVersion.v == ProtocolVersion.NONE.v) { |
544 |
helloVersion = activeProtocols.helloVersion; |
|
545 |
} |
|
2 | 546 |
|
7043 | 547 |
// We accumulate digests of the handshake messages so that |
548 |
// we can read/write CertificateVerify and Finished messages, |
|
549 |
// getting assurance against some particular active attacks. |
|
14675
17224d0282f1
8004019: Removes unused method HandshakeHash.setCertificateVerifyAlg()
xuelei
parents:
14664
diff
changeset
|
550 |
handshakeHash = new HandshakeHash(needCertVerify); |
7043 | 551 |
|
552 |
// Generate handshake input/output stream. |
|
2 | 553 |
if (conn != null) { |
30904 | 554 |
input = new HandshakeInStream(); |
555 |
output = new HandshakeOutStream(conn.outputRecord); |
|
556 |
||
557 |
conn.inputRecord.setHandshakeHash(handshakeHash); |
|
558 |
conn.inputRecord.setHelloVersion(helloVersion); |
|
559 |
||
560 |
conn.outputRecord.setHandshakeHash(handshakeHash); |
|
561 |
conn.outputRecord.setHelloVersion(helloVersion); |
|
562 |
conn.outputRecord.setVersion(protocolVersion); |
|
563 |
} else if (engine != null) { |
|
564 |
input = new HandshakeInStream(); |
|
565 |
output = new HandshakeOutStream(engine.outputRecord); |
|
566 |
||
7043 | 567 |
engine.inputRecord.setHandshakeHash(handshakeHash); |
7039 | 568 |
engine.inputRecord.setHelloVersion(helloVersion); |
30904 | 569 |
|
570 |
engine.outputRecord.setHandshakeHash(handshakeHash); |
|
2 | 571 |
engine.outputRecord.setHelloVersion(helloVersion); |
30904 | 572 |
engine.outputRecord.setVersion(protocolVersion); |
2 | 573 |
} |
574 |
||
30904 | 575 |
handshakeActivated = true; |
2 | 576 |
} |
577 |
||
578 |
/** |
|
579 |
* Set cipherSuite and keyExchange to the given CipherSuite. |
|
580 |
* Does not perform any verification that this is a valid selection, |
|
581 |
* this must be done before calling this method. |
|
582 |
*/ |
|
583 |
void setCipherSuite(CipherSuite s) { |
|
584 |
this.cipherSuite = s; |
|
585 |
this.keyExchange = s.keyExchange; |
|
586 |
} |
|
587 |
||
588 |
/** |
|
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18554
diff
changeset
|
589 |
* Check if the given ciphersuite is enabled and available within the |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18554
diff
changeset
|
590 |
* current active cipher suites. |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18554
diff
changeset
|
591 |
* |
2 | 592 |
* Does not check if the required server certificates are available. |
593 |
*/ |
|
6856 | 594 |
boolean isNegotiable(CipherSuite s) { |
7039 | 595 |
if (activeCipherSuites == null) { |
596 |
activeCipherSuites = getActiveCipherSuites(); |
|
597 |
} |
|
598 |
||
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18554
diff
changeset
|
599 |
return isNegotiable(activeCipherSuites, s); |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18554
diff
changeset
|
600 |
} |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18554
diff
changeset
|
601 |
|
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18554
diff
changeset
|
602 |
/** |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18554
diff
changeset
|
603 |
* Check if the given ciphersuite is enabled and available within the |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18554
diff
changeset
|
604 |
* proposed cipher suite list. |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18554
diff
changeset
|
605 |
* |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18554
diff
changeset
|
606 |
* Does not check if the required server certificates are available. |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18554
diff
changeset
|
607 |
*/ |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
31712
diff
changeset
|
608 |
static final boolean isNegotiable(CipherSuiteList proposed, CipherSuite s) { |
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18554
diff
changeset
|
609 |
return proposed.contains(s) && s.isNegotiable(); |
7039 | 610 |
} |
611 |
||
612 |
/** |
|
613 |
* Check if the given protocol version is enabled and available. |
|
614 |
*/ |
|
615 |
boolean isNegotiable(ProtocolVersion protocolVersion) { |
|
616 |
if (activeProtocols == null) { |
|
617 |
activeProtocols = getActiveProtocols(); |
|
618 |
} |
|
619 |
||
620 |
return activeProtocols.contains(protocolVersion); |
|
621 |
} |
|
622 |
||
623 |
/** |
|
624 |
* Select a protocol version from the list. Called from |
|
625 |
* ServerHandshaker to negotiate protocol version. |
|
626 |
* |
|
627 |
* Return the lower of the protocol version suggested in the |
|
628 |
* clien hello and the highest supported by the server. |
|
629 |
*/ |
|
630 |
ProtocolVersion selectProtocolVersion(ProtocolVersion protocolVersion) { |
|
631 |
if (activeProtocols == null) { |
|
632 |
activeProtocols = getActiveProtocols(); |
|
633 |
} |
|
634 |
||
635 |
return activeProtocols.selectProtocolVersion(protocolVersion); |
|
2 | 636 |
} |
637 |
||
638 |
/** |
|
7039 | 639 |
* Get the active cipher suites. |
640 |
* |
|
641 |
* In TLS 1.1, many weak or vulnerable cipher suites were obsoleted, |
|
642 |
* such as TLS_RSA_EXPORT_WITH_RC4_40_MD5. The implementation MUST NOT |
|
643 |
* negotiate these cipher suites in TLS 1.1 or later mode. |
|
644 |
* |
|
645 |
* Therefore, when the active protocols only include TLS 1.1 or later, |
|
646 |
* the client cannot request to negotiate those obsoleted cipher |
|
7043 | 647 |
* suites. That is, the obsoleted suites should not be included in the |
7039 | 648 |
* client hello. So we need to create a subset of the enabled cipher |
649 |
* suites, the active cipher suites, which does not contain obsoleted |
|
650 |
* cipher suites of the minimum active protocol. |
|
651 |
* |
|
652 |
* Return empty list instead of null if no active cipher suites. |
|
653 |
*/ |
|
654 |
CipherSuiteList getActiveCipherSuites() { |
|
655 |
if (activeCipherSuites == null) { |
|
656 |
if (activeProtocols == null) { |
|
657 |
activeProtocols = getActiveProtocols(); |
|
658 |
} |
|
659 |
||
7990 | 660 |
ArrayList<CipherSuite> suites = new ArrayList<>(); |
7039 | 661 |
if (!(activeProtocols.collection().isEmpty()) && |
662 |
activeProtocols.min.v != ProtocolVersion.NONE.v) { |
|
39563
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
663 |
boolean checkedCurves = false; |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
664 |
boolean hasCurves = false; |
7039 | 665 |
for (CipherSuite suite : enabledCipherSuites.collection()) { |
30904 | 666 |
if (!activeProtocols.min.obsoletes(suite) && |
667 |
activeProtocols.max.supports(suite)) { |
|
7043 | 668 |
if (algorithmConstraints.permits( |
669 |
EnumSet.of(CryptoPrimitive.KEY_AGREEMENT), |
|
670 |
suite.name, null)) { |
|
39563
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
671 |
|
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
672 |
boolean available = true; |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
673 |
if (suite.keyExchange.isEC) { |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
674 |
if (!checkedCurves) { |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
675 |
hasCurves = EllipticCurvesExtension |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
676 |
.hasActiveCurves(algorithmConstraints); |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
677 |
checkedCurves = true; |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
678 |
|
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
679 |
if (!hasCurves && debug != null && |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
680 |
Debug.isOn("verbose")) { |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
681 |
System.out.println( |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
682 |
"No available elliptic curves"); |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
683 |
} |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
684 |
} |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
685 |
|
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
686 |
available = hasCurves; |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
687 |
|
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
688 |
if (!available && debug != null && |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
689 |
Debug.isOn("verbose")) { |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
690 |
System.out.println( |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
691 |
"No active elliptic curves, ignore " + |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
692 |
suite); |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
693 |
} |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
694 |
} |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
695 |
|
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
696 |
if (available) { |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
697 |
suites.add(suite); |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
698 |
} |
7043 | 699 |
} |
700 |
} else if (debug != null && Debug.isOn("verbose")) { |
|
30904 | 701 |
if (activeProtocols.min.obsoletes(suite)) { |
7043 | 702 |
System.out.println( |
703 |
"Ignoring obsoleted cipher suite: " + suite); |
|
704 |
} else { |
|
705 |
System.out.println( |
|
706 |
"Ignoring unsupported cipher suite: " + suite); |
|
707 |
} |
|
7039 | 708 |
} |
709 |
} |
|
710 |
} |
|
711 |
activeCipherSuites = new CipherSuiteList(suites); |
|
712 |
} |
|
713 |
||
714 |
return activeCipherSuites; |
|
715 |
} |
|
716 |
||
717 |
/* |
|
718 |
* Get the active protocol versions. |
|
719 |
* |
|
720 |
* In TLS 1.1, many weak or vulnerable cipher suites were obsoleted, |
|
721 |
* such as TLS_RSA_EXPORT_WITH_RC4_40_MD5. The implementation MUST NOT |
|
722 |
* negotiate these cipher suites in TLS 1.1 or later mode. |
|
723 |
* |
|
724 |
* For example, if "TLS_RSA_EXPORT_WITH_RC4_40_MD5" is the |
|
725 |
* only enabled cipher suite, the client cannot request TLS 1.1 or |
|
726 |
* later, even though TLS 1.1 or later is enabled. We need to create a |
|
727 |
* subset of the enabled protocols, called the active protocols, which |
|
728 |
* contains protocols appropriate to the list of enabled Ciphersuites. |
|
729 |
* |
|
730 |
* Return empty list instead of null if no active protocol versions. |
|
731 |
*/ |
|
732 |
ProtocolList getActiveProtocols() { |
|
733 |
if (activeProtocols == null) { |
|
25801
da76619a8c19
8052406: SSLv2Hello protocol may be filter out unexpectedly
xuelei
parents:
22336
diff
changeset
|
734 |
boolean enabledSSL20Hello = false; |
39563
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
735 |
boolean checkedCurves = false; |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
736 |
boolean hasCurves = false; |
7990 | 737 |
ArrayList<ProtocolVersion> protocols = new ArrayList<>(4); |
7039 | 738 |
for (ProtocolVersion protocol : enabledProtocols.collection()) { |
25801
da76619a8c19
8052406: SSLv2Hello protocol may be filter out unexpectedly
xuelei
parents:
22336
diff
changeset
|
739 |
// Need not to check the SSL20Hello protocol. |
da76619a8c19
8052406: SSLv2Hello protocol may be filter out unexpectedly
xuelei
parents:
22336
diff
changeset
|
740 |
if (protocol.v == ProtocolVersion.SSL20Hello.v) { |
da76619a8c19
8052406: SSLv2Hello protocol may be filter out unexpectedly
xuelei
parents:
22336
diff
changeset
|
741 |
enabledSSL20Hello = true; |
da76619a8c19
8052406: SSLv2Hello protocol may be filter out unexpectedly
xuelei
parents:
22336
diff
changeset
|
742 |
continue; |
da76619a8c19
8052406: SSLv2Hello protocol may be filter out unexpectedly
xuelei
parents:
22336
diff
changeset
|
743 |
} |
da76619a8c19
8052406: SSLv2Hello protocol may be filter out unexpectedly
xuelei
parents:
22336
diff
changeset
|
744 |
|
28555 | 745 |
if (!algorithmConstraints.permits( |
746 |
EnumSet.of(CryptoPrimitive.KEY_AGREEMENT), |
|
747 |
protocol.name, null)) { |
|
748 |
if (debug != null && Debug.isOn("verbose")) { |
|
749 |
System.out.println( |
|
750 |
"Ignoring disabled protocol: " + protocol); |
|
751 |
} |
|
752 |
||
753 |
continue; |
|
754 |
} |
|
755 |
||
7039 | 756 |
boolean found = false; |
757 |
for (CipherSuite suite : enabledCipherSuites.collection()) { |
|
30904 | 758 |
if (suite.isAvailable() && (!protocol.obsoletes(suite)) && |
759 |
protocol.supports(suite)) { |
|
7043 | 760 |
if (algorithmConstraints.permits( |
761 |
EnumSet.of(CryptoPrimitive.KEY_AGREEMENT), |
|
762 |
suite.name, null)) { |
|
39563
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
763 |
|
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
764 |
boolean available = true; |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
765 |
if (suite.keyExchange.isEC) { |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
766 |
if (!checkedCurves) { |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
767 |
hasCurves = EllipticCurvesExtension |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
768 |
.hasActiveCurves(algorithmConstraints); |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
769 |
checkedCurves = true; |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
770 |
|
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
771 |
if (!hasCurves && debug != null && |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
772 |
Debug.isOn("verbose")) { |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
773 |
System.out.println( |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
774 |
"No activated elliptic curves"); |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
775 |
} |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
776 |
} |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
777 |
|
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
778 |
available = hasCurves; |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
779 |
|
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
780 |
if (!available && debug != null && |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
781 |
Debug.isOn("verbose")) { |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
782 |
System.out.println( |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
783 |
"No active elliptic curves, ignore " + |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
784 |
suite + " for " + protocol); |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
785 |
} |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
786 |
} |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
787 |
|
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
788 |
if (available) { |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
789 |
protocols.add(protocol); |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
790 |
found = true; |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
791 |
break; |
1449ed425710
8148516: Improve the default strength of EC in JDK
xuelei
parents:
35298
diff
changeset
|
792 |
} |
7043 | 793 |
} else if (debug != null && Debug.isOn("verbose")) { |
794 |
System.out.println( |
|
795 |
"Ignoring disabled cipher suite: " + suite + |
|
796 |
" for " + protocol); |
|
797 |
} |
|
798 |
} else if (debug != null && Debug.isOn("verbose")) { |
|
799 |
System.out.println( |
|
800 |
"Ignoring unsupported cipher suite: " + suite + |
|
801 |
" for " + protocol); |
|
7039 | 802 |
} |
803 |
} |
|
804 |
if (!found && (debug != null) && Debug.isOn("handshake")) { |
|
805 |
System.out.println( |
|
806 |
"No available cipher suite for " + protocol); |
|
807 |
} |
|
808 |
} |
|
25801
da76619a8c19
8052406: SSLv2Hello protocol may be filter out unexpectedly
xuelei
parents:
22336
diff
changeset
|
809 |
|
da76619a8c19
8052406: SSLv2Hello protocol may be filter out unexpectedly
xuelei
parents:
22336
diff
changeset
|
810 |
if (!protocols.isEmpty() && enabledSSL20Hello) { |
da76619a8c19
8052406: SSLv2Hello protocol may be filter out unexpectedly
xuelei
parents:
22336
diff
changeset
|
811 |
protocols.add(ProtocolVersion.SSL20Hello); |
da76619a8c19
8052406: SSLv2Hello protocol may be filter out unexpectedly
xuelei
parents:
22336
diff
changeset
|
812 |
} |
da76619a8c19
8052406: SSLv2Hello protocol may be filter out unexpectedly
xuelei
parents:
22336
diff
changeset
|
813 |
|
7039 | 814 |
activeProtocols = new ProtocolList(protocols); |
815 |
} |
|
816 |
||
817 |
return activeProtocols; |
|
818 |
} |
|
819 |
||
820 |
/** |
|
821 |
* As long as handshaking has not activated, we can |
|
2 | 822 |
* change whether session creations are allowed. |
823 |
* |
|
824 |
* Callers should do their own checking if handshaking |
|
7039 | 825 |
* has activated. |
2 | 826 |
*/ |
827 |
void setEnableSessionCreation(boolean newSessions) { |
|
828 |
enableNewSession = newSessions; |
|
829 |
} |
|
830 |
||
831 |
/** |
|
832 |
* Create a new read cipher and return it to caller. |
|
833 |
*/ |
|
834 |
CipherBox newReadCipher() throws NoSuchAlgorithmException { |
|
835 |
BulkCipher cipher = cipherSuite.cipher; |
|
836 |
CipherBox box; |
|
837 |
if (isClient) { |
|
838 |
box = cipher.newCipher(protocolVersion, svrWriteKey, svrWriteIV, |
|
7039 | 839 |
sslContext.getSecureRandom(), false); |
2 | 840 |
svrWriteKey = null; |
841 |
svrWriteIV = null; |
|
842 |
} else { |
|
843 |
box = cipher.newCipher(protocolVersion, clntWriteKey, clntWriteIV, |
|
7039 | 844 |
sslContext.getSecureRandom(), false); |
2 | 845 |
clntWriteKey = null; |
846 |
clntWriteIV = null; |
|
847 |
} |
|
848 |
return box; |
|
849 |
} |
|
850 |
||
851 |
/** |
|
852 |
* Create a new write cipher and return it to caller. |
|
853 |
*/ |
|
854 |
CipherBox newWriteCipher() throws NoSuchAlgorithmException { |
|
855 |
BulkCipher cipher = cipherSuite.cipher; |
|
856 |
CipherBox box; |
|
857 |
if (isClient) { |
|
858 |
box = cipher.newCipher(protocolVersion, clntWriteKey, clntWriteIV, |
|
7039 | 859 |
sslContext.getSecureRandom(), true); |
2 | 860 |
clntWriteKey = null; |
861 |
clntWriteIV = null; |
|
862 |
} else { |
|
863 |
box = cipher.newCipher(protocolVersion, svrWriteKey, svrWriteIV, |
|
7039 | 864 |
sslContext.getSecureRandom(), true); |
2 | 865 |
svrWriteKey = null; |
866 |
svrWriteIV = null; |
|
867 |
} |
|
868 |
return box; |
|
869 |
} |
|
870 |
||
871 |
/** |
|
872 |
* Create a new read MAC and return it to caller. |
|
873 |
*/ |
|
16913 | 874 |
Authenticator newReadAuthenticator() |
875 |
throws NoSuchAlgorithmException, InvalidKeyException { |
|
876 |
||
877 |
Authenticator authenticator = null; |
|
878 |
if (cipherSuite.cipher.cipherType == AEAD_CIPHER) { |
|
879 |
authenticator = new Authenticator(protocolVersion); |
|
2 | 880 |
} else { |
16913 | 881 |
MacAlg macAlg = cipherSuite.macAlg; |
882 |
if (isClient) { |
|
883 |
authenticator = macAlg.newMac(protocolVersion, svrMacSecret); |
|
884 |
svrMacSecret = null; |
|
885 |
} else { |
|
886 |
authenticator = macAlg.newMac(protocolVersion, clntMacSecret); |
|
887 |
clntMacSecret = null; |
|
888 |
} |
|
2 | 889 |
} |
16913 | 890 |
|
891 |
return authenticator; |
|
2 | 892 |
} |
893 |
||
894 |
/** |
|
895 |
* Create a new write MAC and return it to caller. |
|
896 |
*/ |
|
16913 | 897 |
Authenticator newWriteAuthenticator() |
898 |
throws NoSuchAlgorithmException, InvalidKeyException { |
|
899 |
||
900 |
Authenticator authenticator = null; |
|
901 |
if (cipherSuite.cipher.cipherType == AEAD_CIPHER) { |
|
902 |
authenticator = new Authenticator(protocolVersion); |
|
2 | 903 |
} else { |
16913 | 904 |
MacAlg macAlg = cipherSuite.macAlg; |
905 |
if (isClient) { |
|
906 |
authenticator = macAlg.newMac(protocolVersion, clntMacSecret); |
|
907 |
clntMacSecret = null; |
|
908 |
} else { |
|
909 |
authenticator = macAlg.newMac(protocolVersion, svrMacSecret); |
|
910 |
svrMacSecret = null; |
|
911 |
} |
|
2 | 912 |
} |
16913 | 913 |
|
914 |
return authenticator; |
|
2 | 915 |
} |
916 |
||
917 |
/* |
|
918 |
* Returns true iff the handshake sequence is done, so that |
|
919 |
* this freshly created session can become the current one. |
|
920 |
*/ |
|
921 |
boolean isDone() { |
|
30904 | 922 |
return started() && handshakeState.isEmpty() && handshakeFinished; |
2 | 923 |
} |
924 |
||
925 |
||
926 |
/* |
|
927 |
* Returns the session which was created through this |
|
928 |
* handshake sequence ... should be called after isDone() |
|
929 |
* returns true. |
|
930 |
*/ |
|
931 |
SSLSessionImpl getSession() { |
|
932 |
return session; |
|
933 |
} |
|
934 |
||
935 |
/* |
|
7043 | 936 |
* Set the handshake session |
937 |
*/ |
|
938 |
void setHandshakeSessionSE(SSLSessionImpl handshakeSession) { |
|
939 |
if (conn != null) { |
|
940 |
conn.setHandshakeSession(handshakeSession); |
|
941 |
} else { |
|
942 |
engine.setHandshakeSession(handshakeSession); |
|
943 |
} |
|
944 |
} |
|
945 |
||
30904 | 946 |
void expectingFinishFlightSE() { |
947 |
if (conn != null) { |
|
948 |
conn.expectingFinishFlight(); |
|
949 |
} else { |
|
950 |
engine.expectingFinishFlight(); |
|
951 |
} |
|
952 |
} |
|
953 |
||
7043 | 954 |
/* |
6856 | 955 |
* Returns true if renegotiation is in use for this connection. |
956 |
*/ |
|
957 |
boolean isSecureRenegotiation() { |
|
958 |
return secureRenegotiation; |
|
959 |
} |
|
960 |
||
961 |
/* |
|
962 |
* Returns the verify_data from the Finished message sent by the client. |
|
963 |
*/ |
|
964 |
byte[] getClientVerifyData() { |
|
965 |
return clientVerifyData; |
|
966 |
} |
|
967 |
||
968 |
/* |
|
969 |
* Returns the verify_data from the Finished message sent by the server. |
|
970 |
*/ |
|
971 |
byte[] getServerVerifyData() { |
|
972 |
return serverVerifyData; |
|
973 |
} |
|
974 |
||
975 |
/* |
|
2 | 976 |
* This routine is fed SSL handshake records when they become available, |
977 |
* and processes messages found therein. |
|
978 |
*/ |
|
30904 | 979 |
void processRecord(ByteBuffer record, |
980 |
boolean expectingFinished) throws IOException { |
|
2 | 981 |
|
982 |
checkThrown(); |
|
983 |
||
984 |
/* |
|
985 |
* Store the incoming handshake data, then see if we can |
|
986 |
* now process any completed handshake messages |
|
987 |
*/ |
|
30904 | 988 |
input.incomingRecord(record); |
2 | 989 |
|
990 |
/* |
|
991 |
* We don't need to create a separate delegatable task |
|
992 |
* for finished messages. |
|
993 |
*/ |
|
994 |
if ((conn != null) || expectingFinished) { |
|
995 |
processLoop(); |
|
996 |
} else { |
|
997 |
delegateTask(new PrivilegedExceptionAction<Void>() { |
|
14664
e71aa0962e70
8003950: Adds missing Override annotations and removes unnecessary imports in sun.security.ssl
xuelei
parents:
14229
diff
changeset
|
998 |
@Override |
2 | 999 |
public Void run() throws Exception { |
1000 |
processLoop(); |
|
1001 |
return null; |
|
1002 |
} |
|
1003 |
}); |
|
1004 |
} |
|
1005 |
} |
|
1006 |
||
1007 |
/* |
|
1008 |
* On input, we hash messages one at a time since servers may need |
|
1009 |
* to access an intermediate hash to validate a CertificateVerify |
|
1010 |
* message. |
|
1011 |
* |
|
1012 |
* Note that many handshake messages can come in one record (and often |
|
1013 |
* do, to reduce network resource utilization), and one message can also |
|
1014 |
* require multiple records (e.g. very large Certificate messages). |
|
1015 |
*/ |
|
1016 |
void processLoop() throws IOException { |
|
1017 |
||
5182 | 1018 |
// need to read off 4 bytes at least to get the handshake |
1019 |
// message type and length. |
|
1020 |
while (input.available() >= 4) { |
|
2 | 1021 |
byte messageType; |
1022 |
int messageLen; |
|
1023 |
||
1024 |
/* |
|
1025 |
* See if we can read the handshake message header, and |
|
1026 |
* then the entire handshake message. If not, wait till |
|
1027 |
* we can read and process an entire message. |
|
1028 |
*/ |
|
1029 |
input.mark(4); |
|
1030 |
||
1031 |
messageType = (byte)input.getInt8(); |
|
1032 |
messageLen = input.getInt24(); |
|
1033 |
||
1034 |
if (input.available() < messageLen) { |
|
1035 |
input.reset(); |
|
1036 |
return; |
|
1037 |
} |
|
1038 |
||
30904 | 1039 |
// Set the flags in the message receiving side. |
1040 |
if (messageType == HandshakeMessage.ht_client_hello) { |
|
1041 |
clientHelloDelivered = true; |
|
1042 |
} else if (messageType == HandshakeMessage.ht_hello_request) { |
|
1043 |
serverHelloRequested = true; |
|
1044 |
} |
|
1045 |
||
2 | 1046 |
/* |
21278 | 1047 |
* Process the message. We require |
2 | 1048 |
* that processMessage() consumes the entire message. In |
1049 |
* lieu of explicit error checks (how?!) we assume that the |
|
1050 |
* data will look like garbage on encoding/processing errors, |
|
1051 |
* and that other protocol code will detect such errors. |
|
1052 |
* |
|
1053 |
* Note that digesting is normally deferred till after the |
|
1054 |
* message has been processed, though to process at least the |
|
1055 |
* client's Finished message (i.e. send the server's) we need |
|
1056 |
* to acccelerate that digesting. |
|
1057 |
* |
|
1058 |
* Also, note that hello request messages are never hashed; |
|
1059 |
* that includes the hello request header, too. |
|
1060 |
*/ |
|
30904 | 1061 |
processMessage(messageType, messageLen); |
1062 |
||
1063 |
// Reload if this message has been reserved. |
|
1064 |
// |
|
1065 |
// Note: in the implementation, only certificate_verify and |
|
1066 |
// finished messages are reserved. |
|
1067 |
if ((messageType == HandshakeMessage.ht_finished) || |
|
1068 |
(messageType == HandshakeMessage.ht_certificate_verify)) { |
|
1069 |
||
1070 |
handshakeHash.reload(); |
|
2 | 1071 |
} |
1072 |
} |
|
1073 |
} |
|
1074 |
||
1075 |
||
1076 |
/** |
|
7039 | 1077 |
* Returns true iff the handshaker has been activated. |
1078 |
* |
|
1079 |
* In activated state, the handshaker may not send any messages out. |
|
1080 |
*/ |
|
1081 |
boolean activated() { |
|
30904 | 1082 |
return handshakeActivated; |
7039 | 1083 |
} |
1084 |
||
1085 |
/** |
|
2 | 1086 |
* Returns true iff the handshaker has sent any messages. |
1087 |
*/ |
|
1088 |
boolean started() { |
|
30904 | 1089 |
return (serverHelloRequested || clientHelloDelivered); |
2 | 1090 |
} |
1091 |
||
1092 |
/* |
|
1093 |
* Used to kickstart the negotiation ... either writing a |
|
1094 |
* ClientHello or a HelloRequest as appropriate, whichever |
|
1095 |
* the subclass returns. NOP if handshaking's already started. |
|
1096 |
*/ |
|
1097 |
void kickstart() throws IOException { |
|
30904 | 1098 |
if ((isClient && clientHelloDelivered) || |
1099 |
(!isClient && serverHelloRequested)) { |
|
2 | 1100 |
return; |
1101 |
} |
|
7039 | 1102 |
|
2 | 1103 |
HandshakeMessage m = getKickstartMessage(); |
30904 | 1104 |
handshakeState.update(m, resumingSession); |
2 | 1105 |
|
1106 |
if (debug != null && Debug.isOn("handshake")) { |
|
1107 |
m.print(System.out); |
|
1108 |
} |
|
1109 |
m.write(output); |
|
1110 |
output.flush(); |
|
1111 |
||
30904 | 1112 |
// Set the flags in the message delivering side. |
1113 |
int handshakeType = m.messageType(); |
|
1114 |
if (handshakeType == HandshakeMessage.ht_hello_request) { |
|
1115 |
serverHelloRequested = true; |
|
1116 |
} else { // HandshakeMessage.ht_client_hello |
|
1117 |
clientHelloDelivered = true; |
|
1118 |
} |
|
2 | 1119 |
} |
1120 |
||
1121 |
/** |
|
1122 |
* Both client and server modes can start handshaking; but the |
|
1123 |
* message they send to do so is different. |
|
1124 |
*/ |
|
1125 |
abstract HandshakeMessage getKickstartMessage() throws SSLException; |
|
1126 |
||
1127 |
/* |
|
1128 |
* Client and Server side protocols are each driven though this |
|
1129 |
* call, which processes a single message and drives the appropriate |
|
1130 |
* side of the protocol state machine (depending on the subclass). |
|
1131 |
*/ |
|
1132 |
abstract void processMessage(byte messageType, int messageLen) |
|
1133 |
throws IOException; |
|
1134 |
||
1135 |
/* |
|
1136 |
* Most alerts in the protocol relate to handshaking problems. |
|
1137 |
* Alerts are detected as the connection reads data. |
|
1138 |
*/ |
|
1139 |
abstract void handshakeAlert(byte description) throws SSLProtocolException; |
|
1140 |
||
1141 |
/* |
|
1142 |
* Sends a change cipher spec message and updates the write side |
|
1143 |
* cipher state so that future messages use the just-negotiated spec. |
|
1144 |
*/ |
|
1145 |
void sendChangeCipherSpec(Finished mesg, boolean lastMessage) |
|
1146 |
throws IOException { |
|
1147 |
||
1148 |
output.flush(); // i.e. handshake data |
|
1149 |
||
1150 |
/* |
|
1151 |
* The write cipher state is protected by the connection write lock |
|
1152 |
* so we must grab it while making the change. We also |
|
1153 |
* make sure no writes occur between sending the ChangeCipherSpec |
|
1154 |
* message, installing the new cipher state, and sending the |
|
1155 |
* Finished message. |
|
1156 |
* |
|
1157 |
* We already hold SSLEngine/SSLSocket "this" by virtue |
|
1158 |
* of this being called from the readRecord code. |
|
1159 |
*/ |
|
1160 |
if (conn != null) { |
|
100
01ef29ca378f
6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
2
diff
changeset
|
1161 |
conn.writeLock.lock(); |
01ef29ca378f
6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
2
diff
changeset
|
1162 |
try { |
30904 | 1163 |
handshakeState.changeCipherSpec(false, isClient); |
2 | 1164 |
conn.changeWriteCiphers(); |
1165 |
if (debug != null && Debug.isOn("handshake")) { |
|
1166 |
mesg.print(System.out); |
|
1167 |
} |
|
30904 | 1168 |
|
1169 |
handshakeState.update(mesg, resumingSession); |
|
2 | 1170 |
mesg.write(output); |
1171 |
output.flush(); |
|
100
01ef29ca378f
6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
2
diff
changeset
|
1172 |
} finally { |
01ef29ca378f
6447412: Issue with socket.close() for ssl sockets when poweroff on other system
xuelei
parents:
2
diff
changeset
|
1173 |
conn.writeLock.unlock(); |
2 | 1174 |
} |
1175 |
} else { |
|
1176 |
synchronized (engine.writeLock) { |
|
30904 | 1177 |
handshakeState.changeCipherSpec(false, isClient); |
2 | 1178 |
engine.changeWriteCiphers(); |
1179 |
if (debug != null && Debug.isOn("handshake")) { |
|
1180 |
mesg.print(System.out); |
|
1181 |
} |
|
30904 | 1182 |
|
1183 |
handshakeState.update(mesg, resumingSession); |
|
2 | 1184 |
mesg.write(output); |
1185 |
output.flush(); |
|
1186 |
} |
|
1187 |
} |
|
30904 | 1188 |
|
1189 |
if (lastMessage) { |
|
1190 |
handshakeFinished = true; |
|
1191 |
} |
|
1192 |
} |
|
1193 |
||
1194 |
void receiveChangeCipherSpec() throws IOException { |
|
1195 |
handshakeState.changeCipherSpec(true, isClient); |
|
2 | 1196 |
} |
1197 |
||
1198 |
/* |
|
1199 |
* Single access point to key calculation logic. Given the |
|
1200 |
* pre-master secret and the nonces from client and server, |
|
1201 |
* produce all the keying material to be used. |
|
1202 |
*/ |
|
1203 |
void calculateKeys(SecretKey preMasterSecret, ProtocolVersion version) { |
|
1204 |
SecretKey master = calculateMasterSecret(preMasterSecret, version); |
|
1205 |
session.setMasterSecret(master); |
|
1206 |
calculateConnectionKeys(master); |
|
1207 |
} |
|
1208 |
||
1209 |
/* |
|
1210 |
* Calculate the master secret from its various components. This is |
|
1211 |
* used for key exchange by all cipher suites. |
|
1212 |
* |
|
1213 |
* The master secret is the catenation of three MD5 hashes, each |
|
1214 |
* consisting of the pre-master secret and a SHA1 hash. Those three |
|
1215 |
* SHA1 hashes are of (different) constant strings, the pre-master |
|
1216 |
* secret, and the nonces provided by the client and the server. |
|
1217 |
*/ |
|
1218 |
private SecretKey calculateMasterSecret(SecretKey preMasterSecret, |
|
1219 |
ProtocolVersion requestedVersion) { |
|
7039 | 1220 |
|
2 | 1221 |
if (debug != null && Debug.isOn("keygen")) { |
1222 |
HexDumpEncoder dump = new HexDumpEncoder(); |
|
1223 |
||
1224 |
System.out.println("SESSION KEYGEN:"); |
|
1225 |
||
1226 |
System.out.println("PreMaster Secret:"); |
|
1227 |
printHex(dump, preMasterSecret.getEncoded()); |
|
1228 |
||
1229 |
// Nonces are dumped with connection keygen, no |
|
1230 |
// benefit to doing it twice |
|
1231 |
} |
|
1232 |
||
7043 | 1233 |
// What algs/params do we need to use? |
1234 |
String masterAlg; |
|
1235 |
PRF prf; |
|
1236 |
||
30904 | 1237 |
byte majorVersion = protocolVersion.major; |
1238 |
byte minorVersion = protocolVersion.minor; |
|
1239 |
if (protocolVersion.isDTLSProtocol()) { |
|
1240 |
// Use TLS version number for DTLS key calculation |
|
1241 |
if (protocolVersion.v == ProtocolVersion.DTLS10.v) { |
|
1242 |
majorVersion = ProtocolVersion.TLS11.major; |
|
1243 |
minorVersion = ProtocolVersion.TLS11.minor; |
|
1244 |
||
1245 |
masterAlg = "SunTlsMasterSecret"; |
|
1246 |
prf = P_NONE; |
|
1247 |
} else { // DTLS 1.2 |
|
1248 |
majorVersion = ProtocolVersion.TLS12.major; |
|
1249 |
minorVersion = ProtocolVersion.TLS12.minor; |
|
1250 |
||
1251 |
masterAlg = "SunTls12MasterSecret"; |
|
1252 |
prf = cipherSuite.prfAlg; |
|
1253 |
} |
|
7043 | 1254 |
} else { |
30904 | 1255 |
if (protocolVersion.v >= ProtocolVersion.TLS12.v) { |
1256 |
masterAlg = "SunTls12MasterSecret"; |
|
1257 |
prf = cipherSuite.prfAlg; |
|
1258 |
} else { |
|
1259 |
masterAlg = "SunTlsMasterSecret"; |
|
1260 |
prf = P_NONE; |
|
1261 |
} |
|
7043 | 1262 |
} |
1263 |
||
1264 |
String prfHashAlg = prf.getPRFHashAlg(); |
|
1265 |
int prfHashLength = prf.getPRFHashLength(); |
|
1266 |
int prfBlockSize = prf.getPRFBlockSize(); |
|
1267 |
||
27804
4659e70271c4
8066617: Suppress deprecation warnings in java.base module
darcy
parents:
27068
diff
changeset
|
1268 |
@SuppressWarnings("deprecation") |
7043 | 1269 |
TlsMasterSecretParameterSpec spec = new TlsMasterSecretParameterSpec( |
30904 | 1270 |
preMasterSecret, (majorVersion & 0xFF), (minorVersion & 0xFF), |
7043 | 1271 |
clnt_random.random_bytes, svr_random.random_bytes, |
1272 |
prfHashAlg, prfHashLength, prfBlockSize); |
|
1273 |
||
2 | 1274 |
try { |
7043 | 1275 |
KeyGenerator kg = JsseJce.getKeyGenerator(masterAlg); |
2 | 1276 |
kg.init(spec); |
22309 | 1277 |
return kg.generateKey(); |
1278 |
} catch (InvalidAlgorithmParameterException | |
|
1279 |
NoSuchAlgorithmException iae) { |
|
1280 |
// unlikely to happen, otherwise, must be a provider exception |
|
1281 |
// |
|
2 | 1282 |
// For RSA premaster secrets, do not signal a protocol error |
1283 |
// due to the Bleichenbacher attack. See comments further down. |
|
1284 |
if (debug != null && Debug.isOn("handshake")) { |
|
1285 |
System.out.println("RSA master secret generation error:"); |
|
22309 | 1286 |
iae.printStackTrace(System.out); |
7039 | 1287 |
} |
22309 | 1288 |
throw new ProviderException(iae); |
2 | 1289 |
|
7039 | 1290 |
} |
2 | 1291 |
} |
1292 |
||
1293 |
/* |
|
1294 |
* Calculate the keys needed for this connection, once the session's |
|
1295 |
* master secret has been calculated. Uses the master key and nonces; |
|
1296 |
* the amount of keying material generated is a function of the cipher |
|
1297 |
* suite that's been negotiated. |
|
1298 |
* |
|
1299 |
* This gets called both on the "full handshake" (where we exchanged |
|
1300 |
* a premaster secret and started a new session) as well as on the |
|
1301 |
* "fast handshake" (where we just resumed a pre-existing session). |
|
1302 |
*/ |
|
27804
4659e70271c4
8066617: Suppress deprecation warnings in java.base module
darcy
parents:
27068
diff
changeset
|
1303 |
@SuppressWarnings("deprecation") |
2 | 1304 |
void calculateConnectionKeys(SecretKey masterKey) { |
1305 |
/* |
|
1306 |
* For both the read and write sides of the protocol, we use the |
|
1307 |
* master to generate MAC secrets and cipher keying material. Block |
|
1308 |
* ciphers need initialization vectors, which we also generate. |
|
1309 |
* |
|
1310 |
* First we figure out how much keying material is needed. |
|
1311 |
*/ |
|
1312 |
int hashSize = cipherSuite.macAlg.size; |
|
1313 |
boolean is_exportable = cipherSuite.exportable; |
|
1314 |
BulkCipher cipher = cipherSuite.cipher; |
|
1315 |
int expandedKeySize = is_exportable ? cipher.expandedKeySize : 0; |
|
1316 |
||
7043 | 1317 |
// Which algs/params do we need to use? |
1318 |
String keyMaterialAlg; |
|
1319 |
PRF prf; |
|
1320 |
||
30904 | 1321 |
byte majorVersion = protocolVersion.major; |
1322 |
byte minorVersion = protocolVersion.minor; |
|
1323 |
if (protocolVersion.isDTLSProtocol()) { |
|
1324 |
// Use TLS version number for DTLS key calculation |
|
1325 |
if (protocolVersion.v == ProtocolVersion.DTLS10.v) { |
|
1326 |
majorVersion = ProtocolVersion.TLS11.major; |
|
1327 |
minorVersion = ProtocolVersion.TLS11.minor; |
|
1328 |
||
1329 |
keyMaterialAlg = "SunTlsKeyMaterial"; |
|
1330 |
prf = P_NONE; |
|
1331 |
} else { // DTLS 1.2+ |
|
1332 |
majorVersion = ProtocolVersion.TLS12.major; |
|
1333 |
minorVersion = ProtocolVersion.TLS12.minor; |
|
1334 |
||
1335 |
keyMaterialAlg = "SunTls12KeyMaterial"; |
|
1336 |
prf = cipherSuite.prfAlg; |
|
1337 |
} |
|
7043 | 1338 |
} else { |
30904 | 1339 |
if (protocolVersion.v >= ProtocolVersion.TLS12.v) { |
1340 |
keyMaterialAlg = "SunTls12KeyMaterial"; |
|
1341 |
prf = cipherSuite.prfAlg; |
|
1342 |
} else { |
|
1343 |
keyMaterialAlg = "SunTlsKeyMaterial"; |
|
1344 |
prf = P_NONE; |
|
1345 |
} |
|
7043 | 1346 |
} |
1347 |
||
1348 |
String prfHashAlg = prf.getPRFHashAlg(); |
|
1349 |
int prfHashLength = prf.getPRFHashLength(); |
|
1350 |
int prfBlockSize = prf.getPRFBlockSize(); |
|
1351 |
||
30904 | 1352 |
// TLS v1.1+ and DTLS use an explicit IV in CBC cipher suites to |
16913 | 1353 |
// protect against the CBC attacks. AEAD/GCM cipher suites in TLS |
1354 |
// v1.2 or later use a fixed IV as the implicit part of the partially |
|
1355 |
// implicit nonce technique described in RFC 5116. |
|
1356 |
int ivSize = cipher.ivSize; |
|
1357 |
if (cipher.cipherType == AEAD_CIPHER) { |
|
1358 |
ivSize = cipher.fixedIvSize; |
|
30904 | 1359 |
} else if ((cipher.cipherType == BLOCK_CIPHER) && |
1360 |
protocolVersion.useTLS11PlusSpec()) { |
|
16913 | 1361 |
ivSize = 0; |
1362 |
} |
|
1363 |
||
7043 | 1364 |
TlsKeyMaterialParameterSpec spec = new TlsKeyMaterialParameterSpec( |
30904 | 1365 |
masterKey, (majorVersion & 0xFF), (minorVersion & 0xFF), |
1366 |
clnt_random.random_bytes, svr_random.random_bytes, |
|
1367 |
cipher.algorithm, cipher.keySize, expandedKeySize, |
|
1368 |
ivSize, hashSize, |
|
1369 |
prfHashAlg, prfHashLength, prfBlockSize); |
|
2 | 1370 |
|
1371 |
try { |
|
7043 | 1372 |
KeyGenerator kg = JsseJce.getKeyGenerator(keyMaterialAlg); |
2 | 1373 |
kg.init(spec); |
1374 |
TlsKeyMaterialSpec keySpec = (TlsKeyMaterialSpec)kg.generateKey(); |
|
1375 |
||
16913 | 1376 |
// Return null if cipher keys are not supposed to be generated. |
2 | 1377 |
clntWriteKey = keySpec.getClientCipherKey(); |
1378 |
svrWriteKey = keySpec.getServerCipherKey(); |
|
1379 |
||
7039 | 1380 |
// Return null if IVs are not supposed to be generated. |
2 | 1381 |
clntWriteIV = keySpec.getClientIv(); |
1382 |
svrWriteIV = keySpec.getServerIv(); |
|
1383 |
||
16913 | 1384 |
// Return null if MAC keys are not supposed to be generated. |
2 | 1385 |
clntMacSecret = keySpec.getClientMacKey(); |
1386 |
svrMacSecret = keySpec.getServerMacKey(); |
|
1387 |
} catch (GeneralSecurityException e) { |
|
1388 |
throw new ProviderException(e); |
|
1389 |
} |
|
1390 |
||
1391 |
// |
|
1392 |
// Dump the connection keys as they're generated. |
|
1393 |
// |
|
1394 |
if (debug != null && Debug.isOn("keygen")) { |
|
1395 |
synchronized (System.out) { |
|
1396 |
HexDumpEncoder dump = new HexDumpEncoder(); |
|
1397 |
||
1398 |
System.out.println("CONNECTION KEYGEN:"); |
|
1399 |
||
1400 |
// Inputs: |
|
1401 |
System.out.println("Client Nonce:"); |
|
1402 |
printHex(dump, clnt_random.random_bytes); |
|
1403 |
System.out.println("Server Nonce:"); |
|
1404 |
printHex(dump, svr_random.random_bytes); |
|
1405 |
System.out.println("Master Secret:"); |
|
1406 |
printHex(dump, masterKey.getEncoded()); |
|
1407 |
||
1408 |
// Outputs: |
|
16913 | 1409 |
if (clntMacSecret != null) { |
1410 |
System.out.println("Client MAC write Secret:"); |
|
1411 |
printHex(dump, clntMacSecret.getEncoded()); |
|
1412 |
System.out.println("Server MAC write Secret:"); |
|
1413 |
printHex(dump, svrMacSecret.getEncoded()); |
|
1414 |
} else { |
|
1415 |
System.out.println("... no MAC keys used for this cipher"); |
|
1416 |
} |
|
2 | 1417 |
|
1418 |
if (clntWriteKey != null) { |
|
1419 |
System.out.println("Client write key:"); |
|
1420 |
printHex(dump, clntWriteKey.getEncoded()); |
|
1421 |
System.out.println("Server write key:"); |
|
1422 |
printHex(dump, svrWriteKey.getEncoded()); |
|
1423 |
} else { |
|
1424 |
System.out.println("... no encryption keys used"); |
|
1425 |
} |
|
1426 |
||
1427 |
if (clntWriteIV != null) { |
|
1428 |
System.out.println("Client write IV:"); |
|
1429 |
printHex(dump, clntWriteIV.getIV()); |
|
1430 |
System.out.println("Server write IV:"); |
|
1431 |
printHex(dump, svrWriteIV.getIV()); |
|
1432 |
} else { |
|
30904 | 1433 |
if (protocolVersion.useTLS11PlusSpec()) { |
7039 | 1434 |
System.out.println( |
1435 |
"... no IV derived for this protocol"); |
|
1436 |
} else { |
|
1437 |
System.out.println("... no IV used for this cipher"); |
|
1438 |
} |
|
2 | 1439 |
} |
1440 |
System.out.flush(); |
|
1441 |
} |
|
1442 |
} |
|
1443 |
} |
|
1444 |
||
1445 |
private static void printHex(HexDumpEncoder dump, byte[] bytes) { |
|
1446 |
if (bytes == null) { |
|
1447 |
System.out.println("(key bytes not available)"); |
|
1448 |
} else { |
|
1449 |
try { |
|
1450 |
dump.encodeBuffer(bytes, System.out); |
|
1451 |
} catch (IOException e) { |
|
1452 |
// just for debugging, ignore this |
|
1453 |
} |
|
1454 |
} |
|
1455 |
} |
|
1456 |
||
1457 |
/* |
|
1458 |
* Implement a simple task delegator. |
|
1459 |
* |
|
1460 |
* We are currently implementing this as a single delegator, may |
|
1461 |
* try for parallel tasks later. Client Authentication could |
|
1462 |
* benefit from this, where ClientKeyExchange/CertificateVerify |
|
1463 |
* could be carried out in parallel. |
|
1464 |
*/ |
|
1465 |
class DelegatedTask<E> implements Runnable { |
|
1466 |
||
1467 |
private PrivilegedExceptionAction<E> pea; |
|
1468 |
||
1469 |
DelegatedTask(PrivilegedExceptionAction<E> pea) { |
|
1470 |
this.pea = pea; |
|
1471 |
} |
|
1472 |
||
1473 |
public void run() { |
|
1474 |
synchronized (engine) { |
|
1475 |
try { |
|
1476 |
AccessController.doPrivileged(pea, engine.getAcc()); |
|
1477 |
} catch (PrivilegedActionException pae) { |
|
1478 |
thrown = pae.getException(); |
|
1479 |
} catch (RuntimeException rte) { |
|
1480 |
thrown = rte; |
|
1481 |
} |
|
1482 |
delegatedTask = null; |
|
1483 |
taskDelegated = false; |
|
1484 |
} |
|
1485 |
} |
|
1486 |
} |
|
1487 |
||
1488 |
private <T> void delegateTask(PrivilegedExceptionAction<T> pea) { |
|
1489 |
delegatedTask = new DelegatedTask<T>(pea); |
|
1490 |
taskDelegated = false; |
|
1491 |
thrown = null; |
|
1492 |
} |
|
1493 |
||
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
1494 |
DelegatedTask<?> getTask() { |
2 | 1495 |
if (!taskDelegated) { |
1496 |
taskDelegated = true; |
|
1497 |
return delegatedTask; |
|
1498 |
} else { |
|
1499 |
return null; |
|
1500 |
} |
|
1501 |
} |
|
1502 |
||
1503 |
/* |
|
1504 |
* See if there are any tasks which need to be delegated |
|
1505 |
* |
|
1506 |
* Locked by SSLEngine.this. |
|
1507 |
*/ |
|
1508 |
boolean taskOutstanding() { |
|
1509 |
return (delegatedTask != null); |
|
1510 |
} |
|
1511 |
||
1512 |
/* |
|
1513 |
* The previous caller failed for some reason, report back the |
|
1514 |
* Exception. We won't worry about Error's. |
|
1515 |
* |
|
1516 |
* Locked by SSLEngine.this. |
|
1517 |
*/ |
|
1518 |
void checkThrown() throws SSLException { |
|
1519 |
synchronized (thrownLock) { |
|
1520 |
if (thrown != null) { |
|
1521 |
||
1522 |
String msg = thrown.getMessage(); |
|
1523 |
||
1524 |
if (msg == null) { |
|
1525 |
msg = "Delegated task threw Exception/Error"; |
|
1526 |
} |
|
1527 |
||
1528 |
/* |
|
1529 |
* See what the underlying type of exception is. We should |
|
1530 |
* throw the same thing. Chain thrown to the new exception. |
|
1531 |
*/ |
|
1532 |
Exception e = thrown; |
|
1533 |
thrown = null; |
|
1534 |
||
1535 |
if (e instanceof RuntimeException) { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
1536 |
throw new RuntimeException(msg, e); |
2 | 1537 |
} else if (e instanceof SSLHandshakeException) { |
1538 |
throw (SSLHandshakeException) |
|
1539 |
new SSLHandshakeException(msg).initCause(e); |
|
1540 |
} else if (e instanceof SSLKeyException) { |
|
1541 |
throw (SSLKeyException) |
|
1542 |
new SSLKeyException(msg).initCause(e); |
|
1543 |
} else if (e instanceof SSLPeerUnverifiedException) { |
|
1544 |
throw (SSLPeerUnverifiedException) |
|
1545 |
new SSLPeerUnverifiedException(msg).initCause(e); |
|
1546 |
} else if (e instanceof SSLProtocolException) { |
|
1547 |
throw (SSLProtocolException) |
|
1548 |
new SSLProtocolException(msg).initCause(e); |
|
1549 |
} else { |
|
1550 |
/* |
|
1551 |
* If it's SSLException or any other Exception, |
|
1552 |
* we'll wrap it in an SSLException. |
|
1553 |
*/ |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
9035
diff
changeset
|
1554 |
throw new SSLException(msg, e); |
2 | 1555 |
} |
1556 |
} |
|
1557 |
} |
|
1558 |
} |
|
1559 |
} |