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