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