author | xuelei |
Sat, 02 Jun 2018 21:23:02 -0700 | |
branch | JDK-8145252-TLS13-branch |
changeset 56660 | 66c803c3ce32 |
parent 56561 | 5f23e0400f27 |
child 56702 | 75527e40bdfd |
permissions | -rw-r--r-- |
2 | 1 |
/* |
56542 | 2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.ssl; |
|
27 |
||
56542 | 28 |
import java.io.IOException; |
30904 | 29 |
import java.nio.ByteBuffer; |
56542 | 30 |
import java.security.AlgorithmConstraints; |
31 |
import java.security.CryptoPrimitive; |
|
32 |
import java.util.AbstractMap.SimpleImmutableEntry; |
|
33 |
import java.util.ArrayList; |
|
34 |
import java.util.Collections; |
|
35 |
import java.util.EnumMap; |
|
36 |
import java.util.EnumSet; |
|
37 |
import java.util.HashMap; |
|
38 |
import java.util.LinkedHashMap; |
|
39 |
import java.util.LinkedList; |
|
40 |
import java.util.List; |
|
41 |
import java.util.Map; |
|
42 |
import java.util.Queue; |
|
43 |
import javax.crypto.SecretKey; |
|
44 |
import javax.net.ssl.SNIServerName; |
|
45 |
import javax.net.ssl.SSLHandshakeException; |
|
46 |
import sun.security.ssl.SupportedGroupsExtension.NamedGroup; |
|
47 |
import sun.security.ssl.SupportedGroupsExtension.NamedGroupType; |
|
48 |
import static sun.security.ssl.SupportedGroupsExtension.NamedGroupType.*; |
|
49 |
import sun.security.ssl.SupportedGroupsExtension.SupportedGroups; |
|
50 |
import sun.security.ssl.PskKeyExchangeModesExtension.PskKeyExchangeMode; |
|
2 | 51 |
|
56542 | 52 |
abstract class HandshakeContext implements ConnectionContext { |
53 |
// System properties |
|
2 | 54 |
|
56542 | 55 |
// By default, disable the unsafe legacy session renegotiation. |
56 |
static final boolean allowUnsafeRenegotiation = |
|
57 |
Utilities.getBooleanProperty( |
|
5182 | 58 |
"sun.security.ssl.allowUnsafeRenegotiation", false); |
59 |
||
6856 | 60 |
// For maximum interoperability and backward compatibility, RFC 5746 |
61 |
// allows server (or client) to accept ClientHello (or ServerHello) |
|
62 |
// message without the secure renegotiation_info extension or SCSV. |
|
63 |
// |
|
64 |
// For maximum security, RFC 5746 also allows server (or client) to |
|
65 |
// reject such message with a fatal "handshake_failure" alert. |
|
66 |
// |
|
67 |
// By default, allow such legacy hello messages. |
|
56542 | 68 |
static final boolean allowLegacyHelloMessages = |
69 |
Utilities.getBooleanProperty( |
|
6856 | 70 |
"sun.security.ssl.allowLegacyHelloMessages", true); |
71 |
||
56542 | 72 |
// registered handshake message actors |
56544
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
73 |
LinkedHashMap<Byte, SSLConsumer> handshakeConsumers; |
56542 | 74 |
final HashMap<Byte, HandshakeProducer> handshakeProducers; |
75 |
||
76 |
// context |
|
77 |
final SSLContextImpl sslContext; |
|
78 |
final TransportContext conContext; |
|
79 |
final SSLConfiguration sslConfig; |
|
80 |
||
81 |
// consolidated parameters |
|
82 |
final List<ProtocolVersion> activeProtocols; |
|
83 |
final List<CipherSuite> activeCipherSuites; |
|
84 |
final AlgorithmConstraints algorithmConstraints; |
|
85 |
final ProtocolVersion maximumActiveProtocol; |
|
86 |
||
87 |
// output stream |
|
88 |
final HandshakeOutStream handshakeOutput; |
|
89 |
||
90 |
// handshake transcript hash |
|
91 |
final HandshakeHash handshakeHash; |
|
92 |
||
93 |
// negotiated security parameters |
|
94 |
SSLSessionImpl handshakeSession; |
|
95 |
boolean handshakeFinished; |
|
96 |
// boolean isInvalidated; |
|
97 |
||
98 |
boolean kickstartMessageDelivered; |
|
99 |
||
100 |
// Resumption |
|
101 |
boolean isResumption; |
|
102 |
SSLSessionImpl resumingSession; |
|
103 |
||
104 |
final Queue<Map.Entry<Byte, ByteBuffer>> delegatedActions; |
|
105 |
volatile boolean taskDelegated = false; |
|
106 |
volatile Exception delegatedThrown = null; |
|
18283
f842a42076b9
7188658: Add possibility to disable client initiated renegotiation
xuelei
parents:
16913
diff
changeset
|
107 |
|
56542 | 108 |
ProtocolVersion negotiatedProtocol; |
109 |
CipherSuite negotiatedCipherSuite; |
|
110 |
final List<SSLPossession> handshakePossessions; |
|
111 |
final List<SSLCredentials> handshakeCredentials; |
|
112 |
SSLKeyDerivation handshakeKeyDerivation; |
|
113 |
SSLKeyExchange handshakeKeyExchange; |
|
114 |
SecretKey baseReadSecret; |
|
115 |
SecretKey baseWriteSecret; |
|
116 |
||
117 |
// protocol version being established |
|
118 |
int clientHelloVersion; |
|
119 |
String applicationProtocol; |
|
120 |
||
121 |
// the preferable signature algorithm used by ServerKeyExchange message |
|
122 |
SignatureScheme preferableSignatureAlgorithm; |
|
123 |
||
124 |
RandomCookie clientHelloRandom; |
|
125 |
RandomCookie serverHelloRandom; |
|
126 |
byte[] certRequestContext; |
|
48225
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
127 |
|
56542 | 128 |
//////////////////// |
129 |
// Extensions |
|
130 |
||
131 |
// the extensions used in the handshake |
|
132 |
final Map<SSLExtension, SSLExtension.SSLExtensionSpec> |
|
133 |
handshakeExtensions; |
|
134 |
||
135 |
// MaxFragmentLength |
|
136 |
int maxFragmentLength; |
|
137 |
||
138 |
// SignatureScheme |
|
139 |
List<SignatureScheme> localSupportedSignAlgs; |
|
140 |
List<SignatureScheme> peerRequestedSignatureSchemes; |
|
141 |
List<SignatureScheme> peerRequestedCertSignSchemes; |
|
142 |
||
143 |
// SupportedGroups |
|
144 |
List<NamedGroup> clientRequestedNamedGroups; |
|
145 |
||
146 |
// HelloRetryRequest |
|
147 |
NamedGroup serverSelectedNamedGroup; |
|
48225
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
148 |
|
56542 | 149 |
// if server name indicator is negotiated |
150 |
// |
|
151 |
// May need a public API for the indication in the future. |
|
152 |
List<SNIServerName> requestedServerNames; |
|
153 |
SNIServerName negotiatedServerName; |
|
154 |
||
155 |
List<PskKeyExchangeMode> pskKeyExchangeModes = new ArrayList<>(); |
|
156 |
||
157 |
// OCSP Stapling info |
|
158 |
boolean staplingActive = false; |
|
159 |
||
160 |
protected HandshakeContext(SSLContextImpl sslContext, |
|
161 |
TransportContext conContext) throws IOException { |
|
162 |
this.sslContext = sslContext; |
|
163 |
this.conContext = conContext; |
|
164 |
this.sslConfig = (SSLConfiguration)conContext.sslConfig.clone(); |
|
48225
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
165 |
|
56542 | 166 |
this.activeProtocols = getActiveProtocols(sslConfig.enabledProtocols, |
167 |
sslConfig.enabledCipherSuites, sslConfig.algorithmConstraints); |
|
168 |
if (activeProtocols.isEmpty()) { |
|
169 |
throw new SSLHandshakeException( |
|
170 |
"No appropriate protocol (protocol is disabled or " + |
|
171 |
"cipher suites are inappropriate)"); |
|
172 |
} |
|
48225
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
173 |
|
56542 | 174 |
ProtocolVersion maximumVersion = ProtocolVersion.NONE; |
175 |
for (ProtocolVersion pv : this.activeProtocols) { |
|
176 |
if (maximumVersion == ProtocolVersion.NONE || |
|
177 |
pv.compare(maximumVersion) > 0) { |
|
178 |
maximumVersion = pv; |
|
179 |
} |
|
180 |
} |
|
181 |
this.maximumActiveProtocol = maximumVersion; |
|
182 |
this.activeCipherSuites = getActiveCipherSuites(this.activeProtocols, |
|
183 |
sslConfig.enabledCipherSuites, sslConfig.algorithmConstraints); |
|
184 |
if (activeCipherSuites.isEmpty()) { |
|
185 |
throw new SSLHandshakeException("No appropriate cipher suite"); |
|
186 |
} |
|
187 |
this.algorithmConstraints = |
|
188 |
new SSLAlgorithmConstraints(sslConfig.algorithmConstraints); |
|
5182 | 189 |
|
56542 | 190 |
this.handshakeConsumers = new LinkedHashMap<>(); |
191 |
this.handshakeProducers = new HashMap<>(); |
|
192 |
this.handshakeHash = conContext.inputRecord.handshakeHash; |
|
193 |
this.handshakeOutput = new HandshakeOutStream(conContext.outputRecord); |
|
194 |
||
195 |
this.handshakeFinished = false; |
|
196 |
this.kickstartMessageDelivered = false; |
|
197 |
||
198 |
this.delegatedActions = new LinkedList<>(); |
|
199 |
this.handshakeExtensions = new HashMap<>(); |
|
200 |
this.handshakePossessions = new LinkedList<>(); |
|
201 |
this.handshakeCredentials = new LinkedList<>(); |
|
56561
5f23e0400f27
Use PostHandshakeContext for New Session Ticket when approriate
ascarpino
parents:
56544
diff
changeset
|
202 |
this.requestedServerNames = null; |
56542 | 203 |
this.negotiatedServerName = null; |
204 |
this.negotiatedCipherSuite = conContext.cipherSuite; |
|
205 |
initialize(); |
|
206 |
} |
|
30904 | 207 |
|
56544
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
208 |
/** |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
209 |
* Constructor for PostHandshakeContext |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
210 |
*/ |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
211 |
HandshakeContext(TransportContext conContext) { |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
212 |
this.sslContext = conContext.sslContext; |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
213 |
this.conContext = conContext; |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
214 |
this.sslConfig = conContext.sslConfig; |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
215 |
|
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
216 |
this.negotiatedProtocol = conContext.protocolVersion; |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
217 |
this.negotiatedCipherSuite = conContext.cipherSuite; |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
218 |
this.handshakeOutput = new HandshakeOutStream(conContext.outputRecord); |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
219 |
this.delegatedActions = new LinkedList<>(); |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
220 |
|
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
221 |
this.handshakeProducers = null; |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
222 |
this.handshakeHash = null; |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
223 |
this.activeProtocols = null; |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
224 |
this.activeCipherSuites = null; |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
225 |
this.algorithmConstraints = null; |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
226 |
this.maximumActiveProtocol = null; |
56561
5f23e0400f27
Use PostHandshakeContext for New Session Ticket when approriate
ascarpino
parents:
56544
diff
changeset
|
227 |
this.handshakeExtensions = Collections.emptyMap(); // Not in TLS13 |
56544
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
228 |
this.handshakePossessions = null; |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
229 |
this.handshakeCredentials = null; |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
230 |
} |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
231 |
|
56542 | 232 |
// Initialize the non-final class variables. |
233 |
private void initialize() throws IOException { |
|
234 |
ProtocolVersion inputHelloVersion; |
|
235 |
ProtocolVersion outputHelloVersion; |
|
236 |
if (conContext.isNegotiated) { |
|
237 |
inputHelloVersion = conContext.protocolVersion; |
|
238 |
outputHelloVersion = conContext.protocolVersion; |
|
239 |
} else { |
|
240 |
if (activeProtocols.contains(ProtocolVersion.SSL20Hello)) { |
|
241 |
inputHelloVersion = ProtocolVersion.SSL20Hello; |
|
242 |
||
243 |
// Per TLS 1.3 protocol, implementation MUST NOT send an SSL |
|
244 |
// version 2.0 compatible CLIENT-HELLO. |
|
245 |
if (maximumActiveProtocol.useTLS13PlusSpec()) { |
|
246 |
outputHelloVersion = maximumActiveProtocol; |
|
247 |
} else { |
|
248 |
outputHelloVersion = ProtocolVersion.SSL20Hello; |
|
249 |
} |
|
250 |
} else { |
|
251 |
inputHelloVersion = maximumActiveProtocol; |
|
252 |
outputHelloVersion = maximumActiveProtocol; |
|
253 |
} |
|
48225
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
254 |
} |
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
255 |
|
56542 | 256 |
conContext.inputRecord.setHelloVersion(inputHelloVersion); |
257 |
conContext.outputRecord.setHelloVersion(outputHelloVersion); |
|
258 |
||
259 |
if (!conContext.isNegotiated) { |
|
260 |
conContext.protocolVersion = maximumActiveProtocol; |
|
261 |
} |
|
262 |
conContext.outputRecord.setVersion(conContext.protocolVersion); |
|
263 |
} |
|
264 |
||
265 |
private static List<ProtocolVersion> getActiveProtocols( |
|
266 |
List<ProtocolVersion> enabledProtocols, |
|
267 |
List<CipherSuite> enabledCipherSuites, |
|
268 |
AlgorithmConstraints algorithmConstraints) { |
|
269 |
boolean enabledSSL20Hello = false; |
|
270 |
ArrayList<ProtocolVersion> protocols = new ArrayList<>(4); |
|
271 |
for (ProtocolVersion protocol : enabledProtocols) { |
|
272 |
if (!enabledSSL20Hello && protocol == ProtocolVersion.SSL20Hello) { |
|
273 |
enabledSSL20Hello = true; |
|
274 |
continue; |
|
275 |
} |
|
276 |
||
277 |
if (!algorithmConstraints.permits( |
|
278 |
EnumSet.of(CryptoPrimitive.KEY_AGREEMENT), |
|
279 |
protocol.name, null)) { |
|
280 |
// Ignore disabled protocol. |
|
281 |
continue; |
|
282 |
} |
|
283 |
||
284 |
boolean found = false; |
|
285 |
Map<NamedGroupType, Boolean> cachedStatus = |
|
286 |
new EnumMap<>(NamedGroupType.class); |
|
287 |
for (CipherSuite suite : enabledCipherSuites) { |
|
288 |
if (suite.isAvailable() && suite.supports(protocol)) { |
|
289 |
if (isActivatable(suite, |
|
290 |
algorithmConstraints, cachedStatus)) { |
|
291 |
protocols.add(protocol); |
|
292 |
found = true; |
|
293 |
break; |
|
294 |
} |
|
295 |
} else if (SSLLogger.isOn && SSLLogger.isOn("verbose")) { |
|
296 |
SSLLogger.fine( |
|
297 |
"Ignore unsupported cipher suite: " + suite + |
|
298 |
" for " + protocol); |
|
299 |
} |
|
300 |
} |
|
301 |
||
302 |
if (!found && (SSLLogger.isOn) && SSLLogger.isOn("handshake")) { |
|
303 |
SSLLogger.fine( |
|
304 |
"No available cipher suite for " + protocol); |
|
305 |
} |
|
306 |
} |
|
307 |
||
308 |
if (!protocols.isEmpty()) { |
|
309 |
if (enabledSSL20Hello) { |
|
310 |
protocols.add(ProtocolVersion.SSL20Hello); |
|
311 |
} |
|
312 |
Collections.sort(protocols); |
|
313 |
} |
|
314 |
||
315 |
return Collections.unmodifiableList(protocols); |
|
316 |
} |
|
317 |
||
318 |
private static List<CipherSuite> getActiveCipherSuites( |
|
319 |
List<ProtocolVersion> enabledProtocols, |
|
320 |
List<CipherSuite> enabledCipherSuites, |
|
321 |
AlgorithmConstraints algorithmConstraints) { |
|
322 |
||
323 |
List<CipherSuite> suites = new LinkedList<>(); |
|
324 |
if (enabledProtocols != null && !enabledProtocols.isEmpty()) { |
|
325 |
Map<NamedGroupType, Boolean> cachedStatus = |
|
326 |
new EnumMap<>(NamedGroupType.class); |
|
327 |
for (CipherSuite suite : enabledCipherSuites) { |
|
328 |
if (!suite.isAvailable()) { |
|
329 |
continue; |
|
330 |
} |
|
331 |
||
332 |
boolean isSupported = false; |
|
333 |
for (ProtocolVersion protocol : enabledProtocols) { |
|
334 |
if (!suite.supports(protocol)) { |
|
335 |
continue; |
|
336 |
} |
|
337 |
if (isActivatable(suite, |
|
338 |
algorithmConstraints, cachedStatus)) { |
|
339 |
suites.add(suite); |
|
340 |
isSupported = true; |
|
341 |
break; |
|
342 |
} |
|
343 |
} |
|
344 |
||
345 |
if (!isSupported && |
|
346 |
SSLLogger.isOn && SSLLogger.isOn("verbose")) { |
|
347 |
SSLLogger.finest( |
|
348 |
"Ignore unsupported cipher suite: " + suite); |
|
349 |
} |
|
350 |
} |
|
351 |
} |
|
352 |
||
353 |
return Collections.unmodifiableList(suites); |
|
354 |
} |
|
355 |
||
56544
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
356 |
/** |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
357 |
* Parse the handshake record and return the contentType |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
358 |
*/ |
56660
66c803c3ce32
key and iv implementation update, and two tests correction
xuelei
parents:
56561
diff
changeset
|
359 |
static byte getHandshakeType(TransportContext conContext, |
66c803c3ce32
key and iv implementation update, and two tests correction
xuelei
parents:
56561
diff
changeset
|
360 |
Plaintext plaintext) throws IOException { |
56542 | 361 |
// struct { |
362 |
// HandshakeType msg_type; /* handshake type */ |
|
363 |
// uint24 length; /* bytes in message */ |
|
364 |
// select (HandshakeType) { |
|
365 |
// ... |
|
366 |
// } body; |
|
367 |
// } Handshake; |
|
56544
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
368 |
|
56542 | 369 |
if (plaintext.contentType != ContentType.HANDSHAKE.id) { |
370 |
conContext.fatal(Alert.INTERNAL_ERROR, |
|
371 |
"Unexpected operation for record: " + plaintext.contentType); |
|
372 |
||
56544
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
373 |
return 0; |
56542 | 374 |
} |
375 |
||
376 |
if (plaintext.fragment == null || plaintext.fragment.remaining() < 4) { |
|
377 |
conContext.fatal(Alert.UNEXPECTED_MESSAGE, |
|
378 |
"Invalid handshake message: insufficient data"); |
|
379 |
||
56544
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
380 |
return 0; |
56542 | 381 |
} |
382 |
||
383 |
byte handshakeType = (byte)Record.getInt8(plaintext.fragment); |
|
384 |
int handshakeLen = Record.getInt24(plaintext.fragment); |
|
385 |
if (handshakeLen != plaintext.fragment.remaining()) { |
|
386 |
conContext.fatal(Alert.UNEXPECTED_MESSAGE, |
|
387 |
"Invalid handshake message: insufficient handshake body"); |
|
388 |
||
56544
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
389 |
return 0; |
56542 | 390 |
} |
391 |
||
56544
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
392 |
return handshakeType; |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
393 |
} |
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
394 |
|
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
395 |
void dispatch(byte handshakeType, Plaintext plaintext) throws IOException { |
56542 | 396 |
if (conContext.transport.useDelegatedTask()) { |
397 |
boolean hasDelegated = !delegatedActions.isEmpty(); |
|
56660
66c803c3ce32
key and iv implementation update, and two tests correction
xuelei
parents:
56561
diff
changeset
|
398 |
if (hasDelegated || |
66c803c3ce32
key and iv implementation update, and two tests correction
xuelei
parents:
56561
diff
changeset
|
399 |
(handshakeType != SSLHandshake.FINISHED.id && |
66c803c3ce32
key and iv implementation update, and two tests correction
xuelei
parents:
56561
diff
changeset
|
400 |
handshakeType != SSLHandshake.KEY_UPDATE.id && |
66c803c3ce32
key and iv implementation update, and two tests correction
xuelei
parents:
56561
diff
changeset
|
401 |
handshakeType != SSLHandshake.NEW_SESSION_TICKET.id)) { |
56542 | 402 |
if (!hasDelegated) { |
403 |
taskDelegated = false; |
|
404 |
delegatedThrown = null; |
|
405 |
} |
|
406 |
||
407 |
// Clone the fragment for delegated actions. |
|
408 |
// |
|
409 |
// The plaintext may share the application buffers. It is |
|
410 |
// fine to use shared buffers if no delegated actions. |
|
411 |
// However, for delegated actions, the shared buffers may be |
|
412 |
// polluted in application layer before the delegated actions |
|
413 |
// executed. |
|
414 |
ByteBuffer fragment = ByteBuffer.wrap( |
|
415 |
new byte[plaintext.fragment.remaining()]); |
|
416 |
fragment.put(plaintext.fragment); |
|
417 |
fragment = fragment.rewind(); |
|
418 |
||
419 |
delegatedActions.add(new SimpleImmutableEntry<>( |
|
420 |
handshakeType, |
|
421 |
fragment |
|
422 |
)); |
|
423 |
} else { |
|
424 |
dispatch(handshakeType, plaintext.fragment); |
|
425 |
} |
|
48225
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
426 |
} else { |
56542 | 427 |
dispatch(handshakeType, plaintext.fragment); |
48225
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
428 |
} |
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
429 |
} |
718669e6b375
8148421: Transport Layer Security (TLS) Session Hash and Extended Master Secret Extension
xuelei
parents:
47216
diff
changeset
|
430 |
|
56542 | 431 |
void dispatch(byte handshakeType, |
432 |
ByteBuffer fragment) throws IOException { |
|
433 |
SSLConsumer consumer; |
|
434 |
if (handshakeType == SSLHandshake.HELLO_REQUEST.id) { |
|
435 |
// For TLS 1.2 and prior versions, the HelloRequest message MAY |
|
436 |
// be sent by the server at any time. |
|
437 |
consumer = SSLHandshake.HELLO_REQUEST; |
|
438 |
} else { |
|
439 |
consumer = handshakeConsumers.get(handshakeType); |
|
6856 | 440 |
} |
2 | 441 |
|
56542 | 442 |
if (consumer == null) { |
443 |
conContext.fatal(Alert.UNEXPECTED_MESSAGE, |
|
444 |
"Unexpected handshake message: " + |
|
445 |
SSLHandshake.nameOf(handshakeType)); |
|
56544
ad120e0dfcfb
start/beginHandshake and more post-handshake changes
ascarpino
parents:
56542
diff
changeset
|
446 |
return; |
56542 | 447 |
} |
2 | 448 |
|
56542 | 449 |
try { |
450 |
consumer.consume(this, fragment); |
|
451 |
} catch (UnsupportedOperationException unsoe) { |
|
452 |
conContext.fatal(Alert.UNEXPECTED_MESSAGE, |
|
453 |
"Unsupported handshake message: " + |
|
454 |
SSLHandshake.nameOf(handshakeType), unsoe); |
|
2 | 455 |
} |
456 |
||
56542 | 457 |
// update handshake hash after handshake message consumption. |
458 |
handshakeHash.consume(); |
|
2 | 459 |
} |
460 |
||
56542 | 461 |
abstract void kickstart() throws IOException; |
2 | 462 |
|
56542 | 463 |
/** |
464 |
* Check if the given cipher suite is enabled and available within |
|
465 |
* the current active cipher suites. |
|
466 |
* |
|
467 |
* Does not check if the required server certificates are available. |
|
468 |
*/ |
|
469 |
boolean isNegotiable(CipherSuite cs) { |
|
470 |
return isNegotiable(activeCipherSuites, cs); |
|
2 | 471 |
} |
472 |
||
56542 | 473 |
/** |
474 |
* Check if the given cipher suite is enabled and available within |
|
475 |
* the proposed cipher suite list. |
|
476 |
* |
|
477 |
* Does not check if the required server certificates are available. |
|
478 |
*/ |
|
479 |
static final boolean isNegotiable( |
|
480 |
List<CipherSuite> proposed, CipherSuite cs) { |
|
481 |
return proposed.contains(cs) && cs.isNegotiable(); |
|
2 | 482 |
} |
483 |
||
56542 | 484 |
/** |
485 |
* Check if the given cipher suite is enabled and available within |
|
486 |
* the proposed cipher suite list and specific protocol version. |
|
487 |
* |
|
488 |
* Does not check if the required server certificates are available. |
|
489 |
*/ |
|
490 |
static final boolean isNegotiable(List<CipherSuite> proposed, |
|
491 |
ProtocolVersion protocolVersion, CipherSuite cs) { |
|
492 |
return proposed.contains(cs) && |
|
493 |
cs.isNegotiable() && cs.supports(protocolVersion); |
|
2 | 494 |
} |
495 |
||
56542 | 496 |
/** |
497 |
* Check if the given protocol version is enabled and available. |
|
498 |
*/ |
|
499 |
boolean isNegotiable(ProtocolVersion protocolVersion) { |
|
500 |
return activeProtocols.contains(protocolVersion); |
|
2 | 501 |
} |
502 |
||
56542 | 503 |
boolean isNegotiable(byte majorVersion, byte minorVersion) { |
504 |
ProtocolVersion pv = |
|
505 |
ProtocolVersion.valueOf(majorVersion, minorVersion); |
|
506 |
if (pv == null) { |
|
507 |
// unsupported protocol version |
|
508 |
return false; |
|
27068 | 509 |
} |
510 |
||
56542 | 511 |
return activeProtocols.contains(pv); |
2 | 512 |
} |
513 |
||
514 |
/** |
|
515 |
* Set the active protocol version and propagate it to the SSLSocket |
|
516 |
* and our handshake streams. Called from ClientHandshaker |
|
517 |
* and ServerHandshaker with the negotiated protocol version. |
|
518 |
*/ |
|
519 |
void setVersion(ProtocolVersion protocolVersion) { |
|
56542 | 520 |
this.conContext.protocolVersion = protocolVersion; |
19823
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18554
diff
changeset
|
521 |
} |
f0fd09e20528
7188657: There should be a way to reorder the JSSE ciphers
xuelei
parents:
18554
diff
changeset
|
522 |
|
56542 | 523 |
private static boolean isActivatable(CipherSuite suite, |
524 |
AlgorithmConstraints algorithmConstraints, |
|
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
42706
diff
changeset
|
525 |
Map<NamedGroupType, Boolean> cachedStatus) { |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
42706
diff
changeset
|
526 |
|
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
42706
diff
changeset
|
527 |
if (algorithmConstraints.permits( |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
42706
diff
changeset
|
528 |
EnumSet.of(CryptoPrimitive.KEY_AGREEMENT), suite.name, null)) { |
56542 | 529 |
if (suite.keyExchange == null) { |
530 |
// TLS 1.3, no definition of key exchange in cipher suite. |
|
531 |
return true; |
|
532 |
} |
|
533 |
||
534 |
boolean available; |
|
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
42706
diff
changeset
|
535 |
NamedGroupType groupType = suite.keyExchange.groupType; |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
42706
diff
changeset
|
536 |
if (groupType != NAMED_GROUP_NONE) { |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
42706
diff
changeset
|
537 |
Boolean checkedStatus = cachedStatus.get(groupType); |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
42706
diff
changeset
|
538 |
if (checkedStatus == null) { |
56542 | 539 |
available = SupportedGroups.isActivatable( |
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
42706
diff
changeset
|
540 |
algorithmConstraints, groupType); |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
42706
diff
changeset
|
541 |
cachedStatus.put(groupType, available); |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
42706
diff
changeset
|
542 |
|
56542 | 543 |
if (!available && |
544 |
SSLLogger.isOn && SSLLogger.isOn("verbose")) { |
|
545 |
SSLLogger.fine("No activated named group"); |
|
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
42706
diff
changeset
|
546 |
} |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
42706
diff
changeset
|
547 |
} else { |
56542 | 548 |
available = checkedStatus; |
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
42706
diff
changeset
|
549 |
} |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
42706
diff
changeset
|
550 |
|
56542 | 551 |
if (!available && SSLLogger.isOn && SSLLogger.isOn("verbose")) { |
552 |
SSLLogger.fine( |
|
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
42706
diff
changeset
|
553 |
"No active named group, ignore " + suite); |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
42706
diff
changeset
|
554 |
} |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
42706
diff
changeset
|
555 |
return available; |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
42706
diff
changeset
|
556 |
} else { |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
42706
diff
changeset
|
557 |
return true; |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
42706
diff
changeset
|
558 |
} |
56542 | 559 |
} else if (SSLLogger.isOn && SSLLogger.isOn("verbose")) { |
560 |
SSLLogger.fine("Ignore disabled cipher suite: " + suite); |
|
45064
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
42706
diff
changeset
|
561 |
} |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
42706
diff
changeset
|
562 |
|
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
42706
diff
changeset
|
563 |
return false; |
b1b45177051b
8140436: Negotiated Finite Field Diffie-Hellman Ephemeral Parameters for TLS
xuelei
parents:
42706
diff
changeset
|
564 |
} |
56561
5f23e0400f27
Use PostHandshakeContext for New Session Ticket when approriate
ascarpino
parents:
56544
diff
changeset
|
565 |
|
5f23e0400f27
Use PostHandshakeContext for New Session Ticket when approriate
ascarpino
parents:
56544
diff
changeset
|
566 |
List<SNIServerName> getRequestedServerNames() { |
5f23e0400f27
Use PostHandshakeContext for New Session Ticket when approriate
ascarpino
parents:
56544
diff
changeset
|
567 |
if (requestedServerNames == null) { |
5f23e0400f27
Use PostHandshakeContext for New Session Ticket when approriate
ascarpino
parents:
56544
diff
changeset
|
568 |
return Collections.<SNIServerName>emptyList(); |
5f23e0400f27
Use PostHandshakeContext for New Session Ticket when approriate
ascarpino
parents:
56544
diff
changeset
|
569 |
} |
5f23e0400f27
Use PostHandshakeContext for New Session Ticket when approriate
ascarpino
parents:
56544
diff
changeset
|
570 |
return requestedServerNames; |
5f23e0400f27
Use PostHandshakeContext for New Session Ticket when approriate
ascarpino
parents:
56544
diff
changeset
|
571 |
} |
56542 | 572 |
} |
2 | 573 |