author | wetmore |
Tue, 21 Aug 2018 11:30:48 -0700 | |
changeset 51574 | ed52ea83f830 |
parent 50768 | 68fa3d4026ea |
child 53055 | c36464ea1f04 |
permissions | -rw-r--r-- |
50768 | 1 |
/* |
2 |
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
|
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 |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
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 |
* |
|
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. |
|
24 |
*/ |
|
25 |
||
26 |
package sun.security.ssl; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.nio.ByteBuffer; |
|
30 |
import java.security.AlgorithmConstraints; |
|
31 |
import java.security.GeneralSecurityException; |
|
32 |
import java.text.MessageFormat; |
|
33 |
import java.util.Arrays; |
|
34 |
import java.util.LinkedList; |
|
35 |
import java.util.List; |
|
36 |
import java.util.Locale; |
|
37 |
import java.util.Map; |
|
38 |
import java.util.Optional; |
|
39 |
import javax.crypto.SecretKey; |
|
40 |
import javax.crypto.spec.IvParameterSpec; |
|
41 |
import javax.net.ssl.SSLException; |
|
42 |
import javax.net.ssl.SSLHandshakeException; |
|
51574
ed52ea83f830
8207317: SSLEngine negotiation fail exception behavior changed from fail-fast to fail-lazy
wetmore
parents:
50768
diff
changeset
|
43 |
import javax.net.ssl.SSLProtocolException; |
50768 | 44 |
import sun.security.ssl.CipherSuite.KeyExchange; |
45 |
import sun.security.ssl.ClientHello.ClientHelloMessage; |
|
46 |
import sun.security.ssl.SSLCipher.SSLReadCipher; |
|
47 |
import sun.security.ssl.SSLCipher.SSLWriteCipher; |
|
48 |
import sun.security.ssl.SSLHandshake.HandshakeMessage; |
|
49 |
import sun.security.ssl.SupportedVersionsExtension.SHSupportedVersionsSpec; |
|
50 |
||
51 |
/** |
|
52 |
* Pack of the ServerHello/HelloRetryRequest handshake message. |
|
53 |
*/ |
|
54 |
final class ServerHello { |
|
55 |
static final SSLConsumer handshakeConsumer = |
|
56 |
new ServerHelloConsumer(); |
|
57 |
static final HandshakeProducer t12HandshakeProducer = |
|
58 |
new T12ServerHelloProducer(); |
|
59 |
static final HandshakeProducer t13HandshakeProducer = |
|
60 |
new T13ServerHelloProducer(); |
|
61 |
static final HandshakeProducer hrrHandshakeProducer = |
|
62 |
new T13HelloRetryRequestProducer(); |
|
63 |
||
64 |
static final HandshakeProducer hrrReproducer = |
|
65 |
new T13HelloRetryRequestReproducer(); |
|
66 |
||
67 |
private static final HandshakeConsumer t12HandshakeConsumer = |
|
68 |
new T12ServerHelloConsumer(); |
|
69 |
private static final HandshakeConsumer t13HandshakeConsumer = |
|
70 |
new T13ServerHelloConsumer(); |
|
71 |
||
72 |
private static final HandshakeConsumer d12HandshakeConsumer = |
|
73 |
new T12ServerHelloConsumer(); |
|
74 |
private static final HandshakeConsumer d13HandshakeConsumer = |
|
75 |
new T13ServerHelloConsumer(); |
|
76 |
||
77 |
private static final HandshakeConsumer t13HrrHandshakeConsumer = |
|
78 |
new T13HelloRetryRequestConsumer(); |
|
79 |
private static final HandshakeConsumer d13HrrHandshakeConsumer = |
|
80 |
new T13HelloRetryRequestConsumer(); |
|
81 |
||
82 |
/** |
|
83 |
* The ServerHello handshake message. |
|
84 |
*/ |
|
85 |
static final class ServerHelloMessage extends HandshakeMessage { |
|
86 |
final ProtocolVersion serverVersion; // TLS 1.3 legacy |
|
87 |
final RandomCookie serverRandom; |
|
88 |
final SessionId sessionId; // TLS 1.3 legacy |
|
89 |
final CipherSuite cipherSuite; |
|
90 |
final byte compressionMethod; // TLS 1.3 legacy |
|
91 |
final SSLExtensions extensions; |
|
92 |
||
93 |
// The HelloRetryRequest producer needs to use the ClientHello message |
|
94 |
// for cookie generation. Please don't use this field for other |
|
95 |
// purpose unless it is really necessary. |
|
96 |
final ClientHelloMessage clientHello; |
|
97 |
||
98 |
// Reserved for HelloRetryRequest consumer. Please don't use this |
|
99 |
// field for other purpose unless it is really necessary. |
|
100 |
final ByteBuffer handshakeRecord; |
|
101 |
||
102 |
ServerHelloMessage(HandshakeContext context, |
|
103 |
ProtocolVersion serverVersion, SessionId sessionId, |
|
104 |
CipherSuite cipherSuite, RandomCookie serverRandom, |
|
105 |
ClientHelloMessage clientHello) { |
|
106 |
super(context); |
|
107 |
||
108 |
this.serverVersion = serverVersion; |
|
109 |
this.serverRandom = serverRandom; |
|
110 |
this.sessionId = sessionId; |
|
111 |
this.cipherSuite = cipherSuite; |
|
112 |
this.compressionMethod = 0x00; // Don't support compression. |
|
113 |
this.extensions = new SSLExtensions(this); |
|
114 |
||
115 |
// Reserve the ClientHello message for cookie generation. |
|
116 |
this.clientHello = clientHello; |
|
117 |
||
118 |
// The handshakeRecord field is used for HelloRetryRequest consumer |
|
119 |
// only. It's fine to set it to null for generating side of the |
|
120 |
// ServerHello/HelloRetryRequest message. |
|
121 |
this.handshakeRecord = null; |
|
122 |
} |
|
123 |
||
124 |
ServerHelloMessage(HandshakeContext context, |
|
125 |
ByteBuffer m) throws IOException { |
|
126 |
super(context); |
|
127 |
||
128 |
// Reserve for HelloRetryRequest consumer if needed. |
|
129 |
this.handshakeRecord = m.duplicate(); |
|
130 |
||
131 |
byte major = m.get(); |
|
132 |
byte minor = m.get(); |
|
133 |
this.serverVersion = ProtocolVersion.valueOf(major, minor); |
|
134 |
if (this.serverVersion == null) { |
|
135 |
// The client should only request for known protocol versions. |
|
136 |
context.conContext.fatal(Alert.PROTOCOL_VERSION, |
|
137 |
"Unsupported protocol version: " + |
|
138 |
ProtocolVersion.nameOf(major, minor)); |
|
139 |
} |
|
140 |
||
141 |
this.serverRandom = new RandomCookie(m); |
|
142 |
this.sessionId = new SessionId(Record.getBytes8(m)); |
|
51574
ed52ea83f830
8207317: SSLEngine negotiation fail exception behavior changed from fail-fast to fail-lazy
wetmore
parents:
50768
diff
changeset
|
143 |
try { |
ed52ea83f830
8207317: SSLEngine negotiation fail exception behavior changed from fail-fast to fail-lazy
wetmore
parents:
50768
diff
changeset
|
144 |
sessionId.checkLength(serverVersion.id); |
ed52ea83f830
8207317: SSLEngine negotiation fail exception behavior changed from fail-fast to fail-lazy
wetmore
parents:
50768
diff
changeset
|
145 |
} catch (SSLProtocolException ex) { |
ed52ea83f830
8207317: SSLEngine negotiation fail exception behavior changed from fail-fast to fail-lazy
wetmore
parents:
50768
diff
changeset
|
146 |
handshakeContext.conContext.fatal(Alert.ILLEGAL_PARAMETER, ex); |
ed52ea83f830
8207317: SSLEngine negotiation fail exception behavior changed from fail-fast to fail-lazy
wetmore
parents:
50768
diff
changeset
|
147 |
} |
50768 | 148 |
|
149 |
int cipherSuiteId = Record.getInt16(m); |
|
150 |
this.cipherSuite = CipherSuite.valueOf(cipherSuiteId); |
|
151 |
if (cipherSuite == null || !context.isNegotiable(cipherSuite)) { |
|
152 |
context.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
|
153 |
"Server selected improper ciphersuite " + |
|
154 |
CipherSuite.nameOf(cipherSuiteId)); |
|
155 |
} |
|
156 |
||
157 |
this.compressionMethod = m.get(); |
|
158 |
if (compressionMethod != 0) { |
|
159 |
context.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
|
160 |
"compression type not supported, " + compressionMethod); |
|
161 |
} |
|
162 |
||
163 |
SSLExtension[] supportedExtensions; |
|
164 |
if (serverRandom.isHelloRetryRequest()) { |
|
165 |
supportedExtensions = context.sslConfig.getEnabledExtensions( |
|
166 |
SSLHandshake.HELLO_RETRY_REQUEST); |
|
167 |
} else { |
|
168 |
supportedExtensions = context.sslConfig.getEnabledExtensions( |
|
169 |
SSLHandshake.SERVER_HELLO); |
|
170 |
} |
|
171 |
||
172 |
if (m.hasRemaining()) { |
|
173 |
this.extensions = |
|
174 |
new SSLExtensions(this, m, supportedExtensions); |
|
175 |
} else { |
|
176 |
this.extensions = new SSLExtensions(this); |
|
177 |
} |
|
178 |
||
179 |
// The clientHello field is used for HelloRetryRequest producer |
|
180 |
// only. It's fine to set it to null for receiving side of |
|
181 |
// ServerHello/HelloRetryRequest message. |
|
182 |
this.clientHello = null; // not used, let it be null; |
|
183 |
} |
|
184 |
||
185 |
@Override |
|
186 |
public SSLHandshake handshakeType() { |
|
187 |
return serverRandom.isHelloRetryRequest() ? |
|
188 |
SSLHandshake.HELLO_RETRY_REQUEST : SSLHandshake.SERVER_HELLO; |
|
189 |
} |
|
190 |
||
191 |
@Override |
|
192 |
public int messageLength() { |
|
193 |
// almost fixed header size, except session ID and extensions: |
|
194 |
// major + minor = 2 |
|
195 |
// random = 32 |
|
196 |
// session ID len field = 1 |
|
197 |
// cipher suite = 2 |
|
198 |
// compression = 1 |
|
199 |
// extensions: if present, 2 + length of extensions |
|
200 |
// In TLS 1.3, use of certain extensions is mandatory. |
|
201 |
return 38 + sessionId.length() + extensions.length(); |
|
202 |
} |
|
203 |
||
204 |
@Override |
|
205 |
public void send(HandshakeOutStream hos) throws IOException { |
|
206 |
hos.putInt8(serverVersion.major); |
|
207 |
hos.putInt8(serverVersion.minor); |
|
208 |
hos.write(serverRandom.randomBytes); |
|
209 |
hos.putBytes8(sessionId.getId()); |
|
210 |
hos.putInt8((cipherSuite.id >> 8) & 0xFF); |
|
211 |
hos.putInt8(cipherSuite.id & 0xff); |
|
212 |
hos.putInt8(compressionMethod); |
|
213 |
||
214 |
extensions.send(hos); // In TLS 1.3, use of certain |
|
215 |
// extensions is mandatory. |
|
216 |
} |
|
217 |
||
218 |
@Override |
|
219 |
public String toString() { |
|
220 |
MessageFormat messageFormat = new MessageFormat( |
|
221 |
"\"{0}\": '{'\n" + |
|
222 |
" \"server version\" : \"{1}\",\n" + |
|
223 |
" \"random\" : \"{2}\",\n" + |
|
224 |
" \"session id\" : \"{3}\",\n" + |
|
225 |
" \"cipher suite\" : \"{4}\",\n" + |
|
226 |
" \"compression methods\" : \"{5}\",\n" + |
|
227 |
" \"extensions\" : [\n" + |
|
228 |
"{6}\n" + |
|
229 |
" ]\n" + |
|
230 |
"'}'", |
|
231 |
Locale.ENGLISH); |
|
232 |
Object[] messageFields = { |
|
233 |
serverRandom.isHelloRetryRequest() ? |
|
234 |
"HelloRetryRequest" : "ServerHello", |
|
235 |
serverVersion.name, |
|
236 |
Utilities.toHexString(serverRandom.randomBytes), |
|
237 |
sessionId.toString(), |
|
238 |
cipherSuite.name + "(" + |
|
239 |
Utilities.byte16HexString(cipherSuite.id) + ")", |
|
240 |
Utilities.toHexString(compressionMethod), |
|
241 |
Utilities.indent(extensions.toString(), " ") |
|
242 |
}; |
|
243 |
||
244 |
return messageFormat.format(messageFields); |
|
245 |
} |
|
246 |
} |
|
247 |
||
248 |
/** |
|
249 |
* The "ServerHello" handshake message producer. |
|
250 |
*/ |
|
251 |
private static final class T12ServerHelloProducer |
|
252 |
implements HandshakeProducer { |
|
253 |
||
254 |
// Prevent instantiation of this class. |
|
255 |
private T12ServerHelloProducer() { |
|
256 |
// blank |
|
257 |
} |
|
258 |
||
259 |
@Override |
|
260 |
public byte[] produce(ConnectionContext context, |
|
261 |
HandshakeMessage message) throws IOException { |
|
262 |
// The producing happens in server side only. |
|
263 |
ServerHandshakeContext shc = (ServerHandshakeContext)context; |
|
264 |
ClientHelloMessage clientHello = (ClientHelloMessage)message; |
|
265 |
||
266 |
// If client hasn't specified a session we can resume, start a |
|
267 |
// new one and choose its cipher suite and compression options, |
|
268 |
// unless new session creation is disabled for this connection! |
|
269 |
if (!shc.isResumption || shc.resumingSession == null) { |
|
270 |
if (!shc.sslConfig.enableSessionCreation) { |
|
271 |
throw new SSLException( |
|
272 |
"Not resumption, and no new session is allowed"); |
|
273 |
} |
|
274 |
||
275 |
if (shc.localSupportedSignAlgs == null) { |
|
276 |
shc.localSupportedSignAlgs = |
|
277 |
SignatureScheme.getSupportedAlgorithms( |
|
278 |
shc.algorithmConstraints, shc.activeProtocols); |
|
279 |
} |
|
280 |
||
281 |
SSLSessionImpl session = |
|
282 |
new SSLSessionImpl(shc, CipherSuite.C_NULL); |
|
283 |
session.setMaximumPacketSize(shc.sslConfig.maximumPacketSize); |
|
284 |
shc.handshakeSession = session; |
|
285 |
||
286 |
// consider the handshake extension impact |
|
287 |
SSLExtension[] enabledExtensions = |
|
288 |
shc.sslConfig.getEnabledExtensions( |
|
289 |
SSLHandshake.CLIENT_HELLO, shc.negotiatedProtocol); |
|
290 |
clientHello.extensions.consumeOnTrade(shc, enabledExtensions); |
|
291 |
||
292 |
// negotiate the cipher suite. |
|
293 |
KeyExchangeProperties credentials = |
|
294 |
chooseCipherSuite(shc, clientHello); |
|
295 |
if (credentials == null) { |
|
296 |
shc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
|
297 |
"no cipher suites in common"); |
|
298 |
||
299 |
return null; // make the compiler happy |
|
300 |
} |
|
301 |
shc.negotiatedCipherSuite = credentials.cipherSuite; |
|
302 |
shc.handshakeKeyExchange = credentials.keyExchange; |
|
303 |
shc.handshakeSession.setSuite(credentials.cipherSuite); |
|
304 |
shc.handshakePossessions.addAll( |
|
305 |
Arrays.asList(credentials.possessions)); |
|
306 |
shc.handshakeHash.determine( |
|
307 |
shc.negotiatedProtocol, shc.negotiatedCipherSuite); |
|
308 |
||
309 |
// Check the incoming OCSP stapling extensions and attempt |
|
310 |
// to get responses. If the resulting stapleParams is non |
|
311 |
// null, it implies that stapling is enabled on the server side. |
|
312 |
shc.stapleParams = StatusResponseManager.processStapling(shc); |
|
313 |
shc.staplingActive = (shc.stapleParams != null); |
|
314 |
||
315 |
// update the responders |
|
316 |
SSLKeyExchange ke = credentials.keyExchange; |
|
317 |
if (ke != null) { |
|
318 |
for (Map.Entry<Byte, HandshakeProducer> me : |
|
319 |
ke.getHandshakeProducers(shc)) { |
|
320 |
shc.handshakeProducers.put( |
|
321 |
me.getKey(), me.getValue()); |
|
322 |
} |
|
323 |
} |
|
324 |
||
325 |
if ((ke != null) && |
|
326 |
(shc.sslConfig.clientAuthType != |
|
327 |
ClientAuthType.CLIENT_AUTH_NONE) && |
|
328 |
!shc.negotiatedCipherSuite.isAnonymous()) { |
|
329 |
for (SSLHandshake hs : |
|
330 |
ke.getRelatedHandshakers(shc)) { |
|
331 |
if (hs == SSLHandshake.CERTIFICATE) { |
|
332 |
shc.handshakeProducers.put( |
|
333 |
SSLHandshake.CERTIFICATE_REQUEST.id, |
|
334 |
SSLHandshake.CERTIFICATE_REQUEST); |
|
335 |
break; |
|
336 |
} |
|
337 |
} |
|
338 |
} |
|
339 |
shc.handshakeProducers.put(SSLHandshake.SERVER_HELLO_DONE.id, |
|
340 |
SSLHandshake.SERVER_HELLO_DONE); |
|
341 |
} else { |
|
342 |
shc.handshakeSession = shc.resumingSession; |
|
343 |
shc.negotiatedProtocol = |
|
344 |
shc.resumingSession.getProtocolVersion(); |
|
345 |
shc.negotiatedCipherSuite = shc.resumingSession.getSuite(); |
|
346 |
shc.handshakeHash.determine( |
|
347 |
shc.negotiatedProtocol, shc.negotiatedCipherSuite); |
|
348 |
} |
|
349 |
||
350 |
// Generate the ServerHello handshake message. |
|
351 |
ServerHelloMessage shm = new ServerHelloMessage(shc, |
|
352 |
shc.negotiatedProtocol, |
|
353 |
shc.handshakeSession.getSessionId(), |
|
354 |
shc.negotiatedCipherSuite, |
|
355 |
new RandomCookie(shc), |
|
356 |
clientHello); |
|
357 |
shc.serverHelloRandom = shm.serverRandom; |
|
358 |
||
359 |
// Produce extensions for ServerHello handshake message. |
|
360 |
SSLExtension[] serverHelloExtensions = |
|
361 |
shc.sslConfig.getEnabledExtensions( |
|
362 |
SSLHandshake.SERVER_HELLO, shc.negotiatedProtocol); |
|
363 |
shm.extensions.produce(shc, serverHelloExtensions); |
|
364 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
365 |
SSLLogger.fine("Produced ServerHello handshake message", shm); |
|
366 |
} |
|
367 |
||
368 |
// Output the handshake message. |
|
369 |
shm.write(shc.handshakeOutput); |
|
370 |
shc.handshakeOutput.flush(); |
|
371 |
||
372 |
if (shc.isResumption && shc.resumingSession != null) { |
|
373 |
SSLTrafficKeyDerivation kdg = |
|
374 |
SSLTrafficKeyDerivation.valueOf(shc.negotiatedProtocol); |
|
375 |
if (kdg == null) { |
|
376 |
// unlikely |
|
377 |
shc.conContext.fatal(Alert.INTERNAL_ERROR, |
|
378 |
"Not supported key derivation: " + |
|
379 |
shc.negotiatedProtocol); |
|
380 |
} else { |
|
381 |
shc.handshakeKeyDerivation = kdg.createKeyDerivation( |
|
382 |
shc, shc.resumingSession.getMasterSecret()); |
|
383 |
} |
|
384 |
||
385 |
// update the responders |
|
386 |
shc.handshakeProducers.put(SSLHandshake.FINISHED.id, |
|
387 |
SSLHandshake.FINISHED); |
|
388 |
} |
|
389 |
||
390 |
// The handshake message has been delivered. |
|
391 |
return null; |
|
392 |
} |
|
393 |
||
394 |
private static KeyExchangeProperties chooseCipherSuite( |
|
395 |
ServerHandshakeContext shc, |
|
396 |
ClientHelloMessage clientHello) throws IOException { |
|
397 |
List<CipherSuite> preferred; |
|
398 |
List<CipherSuite> proposed; |
|
399 |
if (shc.sslConfig.preferLocalCipherSuites) { |
|
400 |
preferred = shc.activeCipherSuites; |
|
401 |
proposed = clientHello.cipherSuites; |
|
402 |
} else { |
|
403 |
preferred = clientHello.cipherSuites; |
|
404 |
proposed = shc.activeCipherSuites; |
|
405 |
} |
|
406 |
||
407 |
List<CipherSuite> legacySuites = new LinkedList<>(); |
|
408 |
for (CipherSuite cs : preferred) { |
|
409 |
if (!HandshakeContext.isNegotiable( |
|
410 |
proposed, shc.negotiatedProtocol, cs)) { |
|
411 |
continue; |
|
412 |
} |
|
413 |
||
414 |
if (shc.sslConfig.clientAuthType == |
|
415 |
ClientAuthType.CLIENT_AUTH_REQUIRED) { |
|
416 |
if ((cs.keyExchange == KeyExchange.K_DH_ANON) || |
|
417 |
(cs.keyExchange == KeyExchange.K_ECDH_ANON)) { |
|
418 |
continue; |
|
419 |
} |
|
420 |
} |
|
421 |
||
422 |
SSLKeyExchange ke = SSLKeyExchange.valueOf( |
|
423 |
cs.keyExchange, shc.negotiatedProtocol); |
|
424 |
if (ke == null) { |
|
425 |
continue; |
|
426 |
} |
|
427 |
if (!ServerHandshakeContext.legacyAlgorithmConstraints.permits( |
|
428 |
null, cs.name, null)) { |
|
429 |
legacySuites.add(cs); |
|
430 |
continue; |
|
431 |
} |
|
432 |
||
433 |
SSLPossession[] hcds = ke.createPossessions(shc); |
|
434 |
if ((hcds == null) || (hcds.length == 0)) { |
|
435 |
continue; |
|
436 |
} |
|
437 |
||
438 |
// The cipher suite has been negotiated. |
|
439 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
440 |
SSLLogger.fine("use cipher suite " + cs.name); |
|
441 |
} |
|
442 |
||
443 |
return new KeyExchangeProperties(cs, ke, hcds); |
|
444 |
} |
|
445 |
||
446 |
for (CipherSuite cs : legacySuites) { |
|
447 |
SSLKeyExchange ke = SSLKeyExchange.valueOf( |
|
448 |
cs.keyExchange, shc.negotiatedProtocol); |
|
449 |
if (ke != null) { |
|
450 |
SSLPossession[] hcds = ke.createPossessions(shc); |
|
451 |
if ((hcds != null) && (hcds.length != 0)) { |
|
452 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
453 |
SSLLogger.warning( |
|
454 |
"use legacy cipher suite " + cs.name); |
|
455 |
} |
|
456 |
return new KeyExchangeProperties(cs, ke, hcds); |
|
457 |
} |
|
458 |
} |
|
459 |
} |
|
460 |
||
461 |
shc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
|
462 |
"no cipher suites in common"); |
|
463 |
||
464 |
return null; // make the compiler happy. |
|
465 |
} |
|
466 |
||
467 |
private static final class KeyExchangeProperties { |
|
468 |
final CipherSuite cipherSuite; |
|
469 |
final SSLKeyExchange keyExchange; |
|
470 |
final SSLPossession[] possessions; |
|
471 |
||
472 |
private KeyExchangeProperties(CipherSuite cipherSuite, |
|
473 |
SSLKeyExchange keyExchange, SSLPossession[] possessions) { |
|
474 |
this.cipherSuite = cipherSuite; |
|
475 |
this.keyExchange = keyExchange; |
|
476 |
this.possessions = possessions; |
|
477 |
} |
|
478 |
} |
|
479 |
} |
|
480 |
||
481 |
/** |
|
482 |
* The "ServerHello" handshake message producer. |
|
483 |
*/ |
|
484 |
private static final |
|
485 |
class T13ServerHelloProducer implements HandshakeProducer { |
|
486 |
// Prevent instantiation of this class. |
|
487 |
private T13ServerHelloProducer() { |
|
488 |
// blank |
|
489 |
} |
|
490 |
||
491 |
@Override |
|
492 |
public byte[] produce(ConnectionContext context, |
|
493 |
HandshakeMessage message) throws IOException { |
|
494 |
// The producing happens in server side only. |
|
495 |
ServerHandshakeContext shc = (ServerHandshakeContext)context; |
|
496 |
ClientHelloMessage clientHello = (ClientHelloMessage)message; |
|
497 |
||
498 |
// If client hasn't specified a session we can resume, start a |
|
499 |
// new one and choose its cipher suite and compression options, |
|
500 |
// unless new session creation is disabled for this connection! |
|
501 |
if (!shc.isResumption || shc.resumingSession == null) { |
|
502 |
if (!shc.sslConfig.enableSessionCreation) { |
|
503 |
throw new SSLException( |
|
504 |
"Not resumption, and no new session is allowed"); |
|
505 |
} |
|
506 |
||
507 |
if (shc.localSupportedSignAlgs == null) { |
|
508 |
shc.localSupportedSignAlgs = |
|
509 |
SignatureScheme.getSupportedAlgorithms( |
|
510 |
shc.algorithmConstraints, shc.activeProtocols); |
|
511 |
} |
|
512 |
||
513 |
SSLSessionImpl session = |
|
514 |
new SSLSessionImpl(shc, CipherSuite.C_NULL); |
|
515 |
session.setMaximumPacketSize(shc.sslConfig.maximumPacketSize); |
|
516 |
shc.handshakeSession = session; |
|
517 |
||
518 |
// consider the handshake extension impact |
|
519 |
SSLExtension[] enabledExtensions = |
|
520 |
shc.sslConfig.getEnabledExtensions( |
|
521 |
SSLHandshake.CLIENT_HELLO, shc.negotiatedProtocol); |
|
522 |
clientHello.extensions.consumeOnTrade(shc, enabledExtensions); |
|
523 |
||
524 |
// negotiate the cipher suite. |
|
525 |
CipherSuite cipherSuite = chooseCipherSuite(shc, clientHello); |
|
526 |
if (cipherSuite == null) { |
|
527 |
shc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
|
528 |
"no cipher suites in common"); |
|
529 |
return null; // make the compiler happy |
|
530 |
} |
|
531 |
shc.negotiatedCipherSuite = cipherSuite; |
|
532 |
shc.handshakeSession.setSuite(cipherSuite); |
|
533 |
shc.handshakeHash.determine( |
|
534 |
shc.negotiatedProtocol, shc.negotiatedCipherSuite); |
|
535 |
} else { |
|
536 |
shc.handshakeSession = shc.resumingSession; |
|
537 |
||
538 |
// consider the handshake extension impact |
|
539 |
SSLExtension[] enabledExtensions = |
|
540 |
shc.sslConfig.getEnabledExtensions( |
|
541 |
SSLHandshake.CLIENT_HELLO, shc.negotiatedProtocol); |
|
542 |
clientHello.extensions.consumeOnTrade(shc, enabledExtensions); |
|
543 |
||
544 |
shc.negotiatedProtocol = |
|
545 |
shc.resumingSession.getProtocolVersion(); |
|
546 |
shc.negotiatedCipherSuite = shc.resumingSession.getSuite(); |
|
547 |
shc.handshakeHash.determine( |
|
548 |
shc.negotiatedProtocol, shc.negotiatedCipherSuite); |
|
549 |
||
550 |
setUpPskKD(shc, |
|
551 |
shc.resumingSession.consumePreSharedKey().get()); |
|
552 |
||
553 |
// The session can't be resumed again---remove it from cache |
|
554 |
SSLSessionContextImpl sessionCache = (SSLSessionContextImpl) |
|
555 |
shc.sslContext.engineGetServerSessionContext(); |
|
556 |
sessionCache.remove(shc.resumingSession.getSessionId()); |
|
557 |
} |
|
558 |
||
559 |
// update the responders |
|
560 |
shc.handshakeProducers.put(SSLHandshake.ENCRYPTED_EXTENSIONS.id, |
|
561 |
SSLHandshake.ENCRYPTED_EXTENSIONS); |
|
562 |
shc.handshakeProducers.put(SSLHandshake.FINISHED.id, |
|
563 |
SSLHandshake.FINISHED); |
|
564 |
||
565 |
// Generate the ServerHello handshake message. |
|
566 |
ServerHelloMessage shm = new ServerHelloMessage(shc, |
|
567 |
ProtocolVersion.TLS12, // use legacy version |
|
568 |
clientHello.sessionId, // echo back |
|
569 |
shc.negotiatedCipherSuite, |
|
570 |
new RandomCookie(shc), |
|
571 |
clientHello); |
|
572 |
shc.serverHelloRandom = shm.serverRandom; |
|
573 |
||
574 |
// Produce extensions for ServerHello handshake message. |
|
575 |
SSLExtension[] serverHelloExtensions = |
|
576 |
shc.sslConfig.getEnabledExtensions( |
|
577 |
SSLHandshake.SERVER_HELLO, shc.negotiatedProtocol); |
|
578 |
shm.extensions.produce(shc, serverHelloExtensions); |
|
579 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
580 |
SSLLogger.fine("Produced ServerHello handshake message", shm); |
|
581 |
} |
|
582 |
||
583 |
// Output the handshake message. |
|
584 |
shm.write(shc.handshakeOutput); |
|
585 |
shc.handshakeOutput.flush(); |
|
586 |
||
587 |
// Change client/server handshake traffic secrets. |
|
588 |
// Refresh handshake hash |
|
589 |
shc.handshakeHash.update(); |
|
590 |
||
591 |
// Change client/server handshake traffic secrets. |
|
592 |
SSLKeyExchange ke = shc.handshakeKeyExchange; |
|
593 |
if (ke == null) { |
|
594 |
// unlikely |
|
595 |
shc.conContext.fatal(Alert.INTERNAL_ERROR, |
|
596 |
"Not negotiated key shares"); |
|
597 |
return null; // make the compiler happy |
|
598 |
} |
|
599 |
||
600 |
SSLKeyDerivation handshakeKD = ke.createKeyDerivation(shc); |
|
601 |
SecretKey handshakeSecret = handshakeKD.deriveKey( |
|
602 |
"TlsHandshakeSecret", null); |
|
603 |
||
604 |
SSLTrafficKeyDerivation kdg = |
|
605 |
SSLTrafficKeyDerivation.valueOf(shc.negotiatedProtocol); |
|
606 |
if (kdg == null) { |
|
607 |
// unlikely |
|
608 |
shc.conContext.fatal(Alert.INTERNAL_ERROR, |
|
609 |
"Not supported key derivation: " + |
|
610 |
shc.negotiatedProtocol); |
|
611 |
return null; // make the compiler happy |
|
612 |
} |
|
613 |
||
614 |
SSLKeyDerivation kd = |
|
615 |
new SSLSecretDerivation(shc, handshakeSecret); |
|
616 |
||
617 |
// update the handshake traffic read keys. |
|
618 |
SecretKey readSecret = kd.deriveKey( |
|
619 |
"TlsClientHandshakeTrafficSecret", null); |
|
620 |
SSLKeyDerivation readKD = |
|
621 |
kdg.createKeyDerivation(shc, readSecret); |
|
622 |
SecretKey readKey = readKD.deriveKey( |
|
623 |
"TlsKey", null); |
|
624 |
SecretKey readIvSecret = readKD.deriveKey( |
|
625 |
"TlsIv", null); |
|
626 |
IvParameterSpec readIv = |
|
627 |
new IvParameterSpec(readIvSecret.getEncoded()); |
|
628 |
SSLReadCipher readCipher; |
|
629 |
try { |
|
630 |
readCipher = |
|
631 |
shc.negotiatedCipherSuite.bulkCipher.createReadCipher( |
|
632 |
Authenticator.valueOf(shc.negotiatedProtocol), |
|
633 |
shc.negotiatedProtocol, readKey, readIv, |
|
634 |
shc.sslContext.getSecureRandom()); |
|
635 |
} catch (GeneralSecurityException gse) { |
|
636 |
// unlikely |
|
637 |
shc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
|
638 |
"Missing cipher algorithm", gse); |
|
639 |
return null; // make the compiler happy |
|
640 |
} |
|
641 |
||
642 |
shc.baseReadSecret = readSecret; |
|
643 |
shc.conContext.inputRecord.changeReadCiphers(readCipher); |
|
644 |
||
645 |
// update the handshake traffic write secret. |
|
646 |
SecretKey writeSecret = kd.deriveKey( |
|
647 |
"TlsServerHandshakeTrafficSecret", null); |
|
648 |
SSLKeyDerivation writeKD = |
|
649 |
kdg.createKeyDerivation(shc, writeSecret); |
|
650 |
SecretKey writeKey = writeKD.deriveKey( |
|
651 |
"TlsKey", null); |
|
652 |
SecretKey writeIvSecret = writeKD.deriveKey( |
|
653 |
"TlsIv", null); |
|
654 |
IvParameterSpec writeIv = |
|
655 |
new IvParameterSpec(writeIvSecret.getEncoded()); |
|
656 |
SSLWriteCipher writeCipher; |
|
657 |
try { |
|
658 |
writeCipher = |
|
659 |
shc.negotiatedCipherSuite.bulkCipher.createWriteCipher( |
|
660 |
Authenticator.valueOf(shc.negotiatedProtocol), |
|
661 |
shc.negotiatedProtocol, writeKey, writeIv, |
|
662 |
shc.sslContext.getSecureRandom()); |
|
663 |
} catch (GeneralSecurityException gse) { |
|
664 |
// unlikely |
|
665 |
shc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
|
666 |
"Missing cipher algorithm", gse); |
|
667 |
return null; // make the compiler happy |
|
668 |
} |
|
669 |
||
670 |
shc.baseWriteSecret = writeSecret; |
|
671 |
shc.conContext.outputRecord.changeWriteCiphers( |
|
672 |
writeCipher, (clientHello.sessionId.length() != 0)); |
|
673 |
||
674 |
// Update the context for master key derivation. |
|
675 |
shc.handshakeKeyDerivation = kd; |
|
676 |
||
677 |
// The handshake message has been delivered. |
|
678 |
return null; |
|
679 |
} |
|
680 |
||
681 |
private static CipherSuite chooseCipherSuite( |
|
682 |
ServerHandshakeContext shc, |
|
683 |
ClientHelloMessage clientHello) throws IOException { |
|
684 |
List<CipherSuite> preferred; |
|
685 |
List<CipherSuite> proposed; |
|
686 |
if (shc.sslConfig.preferLocalCipherSuites) { |
|
687 |
preferred = shc.activeCipherSuites; |
|
688 |
proposed = clientHello.cipherSuites; |
|
689 |
} else { |
|
690 |
preferred = clientHello.cipherSuites; |
|
691 |
proposed = shc.activeCipherSuites; |
|
692 |
} |
|
693 |
||
694 |
CipherSuite legacySuite = null; |
|
695 |
AlgorithmConstraints legacyConstraints = |
|
696 |
ServerHandshakeContext.legacyAlgorithmConstraints; |
|
697 |
for (CipherSuite cs : preferred) { |
|
698 |
if (!HandshakeContext.isNegotiable( |
|
699 |
proposed, shc.negotiatedProtocol, cs)) { |
|
700 |
continue; |
|
701 |
} |
|
702 |
||
703 |
if ((legacySuite == null) && |
|
704 |
!legacyConstraints.permits(null, cs.name, null)) { |
|
705 |
legacySuite = cs; |
|
706 |
continue; |
|
707 |
} |
|
708 |
||
709 |
// The cipher suite has been negotiated. |
|
710 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
711 |
SSLLogger.fine("use cipher suite " + cs.name); |
|
712 |
} |
|
713 |
return cs; |
|
714 |
} |
|
715 |
||
716 |
if (legacySuite != null) { |
|
717 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
718 |
SSLLogger.warning( |
|
719 |
"use legacy cipher suite " + legacySuite.name); |
|
720 |
} |
|
721 |
return legacySuite; |
|
722 |
} |
|
723 |
||
724 |
// no cipher suites in common |
|
725 |
return null; |
|
726 |
} |
|
727 |
} |
|
728 |
||
729 |
/** |
|
730 |
* The "HelloRetryRequest" handshake message producer. |
|
731 |
*/ |
|
732 |
private static final |
|
733 |
class T13HelloRetryRequestProducer implements HandshakeProducer { |
|
734 |
// Prevent instantiation of this class. |
|
735 |
private T13HelloRetryRequestProducer() { |
|
736 |
// blank |
|
737 |
} |
|
738 |
||
739 |
@Override |
|
740 |
public byte[] produce(ConnectionContext context, |
|
741 |
HandshakeMessage message) throws IOException { |
|
742 |
ServerHandshakeContext shc = (ServerHandshakeContext) context; |
|
743 |
ClientHelloMessage clientHello = (ClientHelloMessage) message; |
|
744 |
||
745 |
// negotiate the cipher suite. |
|
746 |
CipherSuite cipherSuite = |
|
747 |
T13ServerHelloProducer.chooseCipherSuite(shc, clientHello); |
|
748 |
if (cipherSuite == null) { |
|
749 |
shc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
|
750 |
"no cipher suites in common for hello retry request"); |
|
751 |
return null; // make the compiler happy |
|
752 |
} |
|
753 |
||
754 |
ServerHelloMessage hhrm = new ServerHelloMessage(shc, |
|
755 |
ProtocolVersion.TLS12, // use legacy version |
|
756 |
clientHello.sessionId, // echo back |
|
757 |
cipherSuite, |
|
758 |
RandomCookie.hrrRandom, |
|
759 |
clientHello |
|
760 |
); |
|
761 |
||
762 |
shc.negotiatedCipherSuite = cipherSuite; |
|
763 |
shc.handshakeHash.determine( |
|
764 |
shc.negotiatedProtocol, shc.negotiatedCipherSuite); |
|
765 |
||
766 |
// Produce extensions for HelloRetryRequest handshake message. |
|
767 |
SSLExtension[] serverHelloExtensions = |
|
768 |
shc.sslConfig.getEnabledExtensions( |
|
769 |
SSLHandshake.HELLO_RETRY_REQUEST, shc.negotiatedProtocol); |
|
770 |
hhrm.extensions.produce(shc, serverHelloExtensions); |
|
771 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
772 |
SSLLogger.fine( |
|
773 |
"Produced HelloRetryRequest handshake message", hhrm); |
|
774 |
} |
|
775 |
||
776 |
// Output the handshake message. |
|
777 |
hhrm.write(shc.handshakeOutput); |
|
778 |
shc.handshakeOutput.flush(); |
|
779 |
||
780 |
// Stateless, shall we clean up the handshake context as well? |
|
781 |
shc.handshakeHash.finish(); // forgot about the handshake hash |
|
782 |
shc.handshakeExtensions.clear(); |
|
783 |
||
784 |
// What's the expected response? |
|
785 |
shc.handshakeConsumers.put( |
|
786 |
SSLHandshake.CLIENT_HELLO.id, SSLHandshake.CLIENT_HELLO); |
|
787 |
||
788 |
// The handshake message has been delivered. |
|
789 |
return null; |
|
790 |
} |
|
791 |
} |
|
792 |
||
793 |
/** |
|
794 |
* The "HelloRetryRequest" handshake message reproducer. |
|
795 |
*/ |
|
796 |
private static final |
|
797 |
class T13HelloRetryRequestReproducer implements HandshakeProducer { |
|
798 |
// Prevent instantiation of this class. |
|
799 |
private T13HelloRetryRequestReproducer() { |
|
800 |
// blank |
|
801 |
} |
|
802 |
||
803 |
@Override |
|
804 |
public byte[] produce(ConnectionContext context, |
|
805 |
HandshakeMessage message) throws IOException { |
|
806 |
ServerHandshakeContext shc = (ServerHandshakeContext) context; |
|
807 |
ClientHelloMessage clientHello = (ClientHelloMessage) message; |
|
808 |
||
809 |
// negotiate the cipher suite. |
|
810 |
CipherSuite cipherSuite = shc.negotiatedCipherSuite; |
|
811 |
ServerHelloMessage hhrm = new ServerHelloMessage(shc, |
|
812 |
ProtocolVersion.TLS12, // use legacy version |
|
813 |
clientHello.sessionId, // echo back |
|
814 |
cipherSuite, |
|
815 |
RandomCookie.hrrRandom, |
|
816 |
clientHello |
|
817 |
); |
|
818 |
||
819 |
// Produce extensions for HelloRetryRequest handshake message. |
|
820 |
SSLExtension[] serverHelloExtensions = |
|
821 |
shc.sslConfig.getEnabledExtensions( |
|
822 |
SSLHandshake.MESSAGE_HASH, shc.negotiatedProtocol); |
|
823 |
hhrm.extensions.produce(shc, serverHelloExtensions); |
|
824 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
825 |
SSLLogger.fine( |
|
826 |
"Reproduced HelloRetryRequest handshake message", hhrm); |
|
827 |
} |
|
828 |
||
829 |
HandshakeOutStream hos = new HandshakeOutStream(null); |
|
830 |
hhrm.write(hos); |
|
831 |
||
832 |
return hos.toByteArray(); |
|
833 |
} |
|
834 |
} |
|
835 |
||
836 |
/** |
|
837 |
* The "ServerHello" handshake message consumer. |
|
838 |
*/ |
|
839 |
private static final |
|
840 |
class ServerHelloConsumer implements SSLConsumer { |
|
841 |
// Prevent instantiation of this class. |
|
842 |
private ServerHelloConsumer() { |
|
843 |
// blank |
|
844 |
} |
|
845 |
||
846 |
@Override |
|
847 |
public void consume(ConnectionContext context, |
|
848 |
ByteBuffer message) throws IOException { |
|
849 |
// The consuming happens in client side only. |
|
850 |
ClientHandshakeContext chc = (ClientHandshakeContext)context; |
|
851 |
||
852 |
// clean up this consumer |
|
853 |
chc.handshakeConsumers.remove(SSLHandshake.SERVER_HELLO.id); |
|
854 |
if (!chc.handshakeConsumers.isEmpty()) { |
|
855 |
// DTLS 1.0/1.2 |
|
856 |
chc.handshakeConsumers.remove( |
|
857 |
SSLHandshake.HELLO_VERIFY_REQUEST.id); |
|
858 |
} |
|
859 |
if (!chc.handshakeConsumers.isEmpty()) { |
|
860 |
chc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, |
|
861 |
"No more message expected before ServerHello is processed"); |
|
862 |
} |
|
863 |
||
864 |
ServerHelloMessage shm = new ServerHelloMessage(chc, message); |
|
865 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
866 |
SSLLogger.fine("Consuming ServerHello handshake message", shm); |
|
867 |
} |
|
868 |
||
869 |
if (shm.serverRandom.isHelloRetryRequest()) { |
|
870 |
onHelloRetryRequest(chc, shm); |
|
871 |
} else { |
|
872 |
onServerHello(chc, shm); |
|
873 |
} |
|
874 |
} |
|
875 |
||
876 |
private void onHelloRetryRequest(ClientHandshakeContext chc, |
|
877 |
ServerHelloMessage helloRetryRequest) throws IOException { |
|
878 |
// Negotiate protocol version. |
|
879 |
// |
|
880 |
// Check and launch SupportedVersions. |
|
881 |
SSLExtension[] extTypes = new SSLExtension[] { |
|
882 |
SSLExtension.HRR_SUPPORTED_VERSIONS |
|
883 |
}; |
|
884 |
helloRetryRequest.extensions.consumeOnLoad(chc, extTypes); |
|
885 |
||
886 |
ProtocolVersion serverVersion; |
|
887 |
SHSupportedVersionsSpec svs = |
|
888 |
(SHSupportedVersionsSpec)chc.handshakeExtensions.get( |
|
889 |
SSLExtension.HRR_SUPPORTED_VERSIONS); |
|
890 |
if (svs != null) { |
|
891 |
serverVersion = // could be null |
|
892 |
ProtocolVersion.valueOf(svs.selectedVersion); |
|
893 |
} else { |
|
894 |
serverVersion = helloRetryRequest.serverVersion; |
|
895 |
} |
|
896 |
||
897 |
if (!chc.activeProtocols.contains(serverVersion)) { |
|
898 |
chc.conContext.fatal(Alert.PROTOCOL_VERSION, |
|
899 |
"The server selected protocol version " + serverVersion + |
|
900 |
" is not accepted by client preferences " + |
|
901 |
chc.activeProtocols); |
|
902 |
} |
|
903 |
||
904 |
if (!serverVersion.useTLS13PlusSpec()) { |
|
905 |
chc.conContext.fatal(Alert.PROTOCOL_VERSION, |
|
906 |
"Unexpected HelloRetryRequest for " + serverVersion.name); |
|
907 |
} |
|
908 |
||
909 |
chc.negotiatedProtocol = serverVersion; |
|
910 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
911 |
SSLLogger.fine( |
|
912 |
"Negotiated protocol version: " + serverVersion.name); |
|
913 |
} |
|
914 |
||
915 |
// TLS 1.3 key share extension may have produced client |
|
916 |
// possessions for TLS 1.3 key exchanges. |
|
917 |
// |
|
918 |
// Clean up before producing new client key share possessions. |
|
919 |
chc.handshakePossessions.clear(); |
|
920 |
||
921 |
if (serverVersion.isDTLS) { |
|
922 |
d13HrrHandshakeConsumer.consume(chc, helloRetryRequest); |
|
923 |
} else { |
|
924 |
t13HrrHandshakeConsumer.consume(chc, helloRetryRequest); |
|
925 |
} |
|
926 |
} |
|
927 |
||
928 |
private void onServerHello(ClientHandshakeContext chc, |
|
929 |
ServerHelloMessage serverHello) throws IOException { |
|
930 |
// Negotiate protocol version. |
|
931 |
// |
|
932 |
// Check and launch SupportedVersions. |
|
933 |
SSLExtension[] extTypes = new SSLExtension[] { |
|
934 |
SSLExtension.SH_SUPPORTED_VERSIONS |
|
935 |
}; |
|
936 |
serverHello.extensions.consumeOnLoad(chc, extTypes); |
|
937 |
||
938 |
ProtocolVersion serverVersion; |
|
939 |
SHSupportedVersionsSpec svs = |
|
940 |
(SHSupportedVersionsSpec)chc.handshakeExtensions.get( |
|
941 |
SSLExtension.SH_SUPPORTED_VERSIONS); |
|
942 |
if (svs != null) { |
|
943 |
serverVersion = // could be null |
|
944 |
ProtocolVersion.valueOf(svs.selectedVersion); |
|
945 |
} else { |
|
946 |
serverVersion = serverHello.serverVersion; |
|
947 |
} |
|
948 |
||
949 |
if (!chc.activeProtocols.contains(serverVersion)) { |
|
950 |
chc.conContext.fatal(Alert.PROTOCOL_VERSION, |
|
951 |
"The server selected protocol version " + serverVersion + |
|
952 |
" is not accepted by client preferences " + |
|
953 |
chc.activeProtocols); |
|
954 |
} |
|
955 |
||
956 |
chc.negotiatedProtocol = serverVersion; |
|
957 |
if (!chc.conContext.isNegotiated) { |
|
958 |
chc.conContext.protocolVersion = chc.negotiatedProtocol; |
|
959 |
chc.conContext.outputRecord.setVersion(chc.negotiatedProtocol); |
|
960 |
} |
|
961 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
962 |
SSLLogger.fine( |
|
963 |
"Negotiated protocol version: " + serverVersion.name); |
|
964 |
} |
|
965 |
||
966 |
if (serverHello.serverRandom.isVersionDowngrade(chc)) { |
|
967 |
chc.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
|
968 |
"A potential protocol version downgrade attack"); |
|
969 |
} |
|
970 |
||
971 |
// Consume the handshake message for the specific protocol version. |
|
972 |
if (serverVersion.isDTLS) { |
|
973 |
if (serverVersion.useTLS13PlusSpec()) { |
|
974 |
d13HandshakeConsumer.consume(chc, serverHello); |
|
975 |
} else { |
|
976 |
// TLS 1.3 key share extension may have produced client |
|
977 |
// possessions for TLS 1.3 key exchanges. |
|
978 |
chc.handshakePossessions.clear(); |
|
979 |
||
980 |
d12HandshakeConsumer.consume(chc, serverHello); |
|
981 |
} |
|
982 |
} else { |
|
983 |
if (serverVersion.useTLS13PlusSpec()) { |
|
984 |
t13HandshakeConsumer.consume(chc, serverHello); |
|
985 |
} else { |
|
986 |
// TLS 1.3 key share extension may have produced client |
|
987 |
// possessions for TLS 1.3 key exchanges. |
|
988 |
chc.handshakePossessions.clear(); |
|
989 |
||
990 |
t12HandshakeConsumer.consume(chc, serverHello); |
|
991 |
} |
|
992 |
} |
|
993 |
} |
|
994 |
} |
|
995 |
||
996 |
private static final |
|
997 |
class T12ServerHelloConsumer implements HandshakeConsumer { |
|
998 |
// Prevent instantiation of this class. |
|
999 |
private T12ServerHelloConsumer() { |
|
1000 |
// blank |
|
1001 |
} |
|
1002 |
||
1003 |
@Override |
|
1004 |
public void consume(ConnectionContext context, |
|
1005 |
HandshakeMessage message) throws IOException { |
|
1006 |
// The consuming happens in client side only. |
|
1007 |
ClientHandshakeContext chc = (ClientHandshakeContext)context; |
|
1008 |
ServerHelloMessage serverHello = (ServerHelloMessage)message; |
|
1009 |
if (!chc.isNegotiable(serverHello.serverVersion)) { |
|
1010 |
chc.conContext.fatal(Alert.PROTOCOL_VERSION, |
|
1011 |
"Server chose " + serverHello.serverVersion + |
|
1012 |
", but that protocol version is not enabled or " + |
|
1013 |
"not supported by the client."); |
|
1014 |
} |
|
1015 |
||
1016 |
// chc.negotiatedProtocol = serverHello.serverVersion; |
|
1017 |
chc.negotiatedCipherSuite = serverHello.cipherSuite; |
|
1018 |
chc.handshakeHash.determine( |
|
1019 |
chc.negotiatedProtocol, chc.negotiatedCipherSuite); |
|
1020 |
chc.serverHelloRandom = serverHello.serverRandom; |
|
1021 |
if (chc.negotiatedCipherSuite.keyExchange == null) { |
|
1022 |
chc.conContext.fatal(Alert.PROTOCOL_VERSION, |
|
1023 |
"TLS 1.2 or prior version does not support the " + |
|
1024 |
"server cipher suite: " + chc.negotiatedCipherSuite.name); |
|
1025 |
} |
|
1026 |
||
1027 |
// |
|
1028 |
// validate |
|
1029 |
// |
|
1030 |
||
1031 |
// Check and launch the "renegotiation_info" extension. |
|
1032 |
SSLExtension[] extTypes = new SSLExtension[] { |
|
1033 |
SSLExtension.SH_RENEGOTIATION_INFO |
|
1034 |
}; |
|
1035 |
serverHello.extensions.consumeOnLoad(chc, extTypes); |
|
1036 |
||
1037 |
// Is it session resuming? |
|
1038 |
if (chc.resumingSession != null) { |
|
1039 |
// we tried to resume, let's see what the server decided |
|
1040 |
if (serverHello.sessionId.equals( |
|
1041 |
chc.resumingSession.getSessionId())) { |
|
1042 |
// server resumed the session, let's make sure everything |
|
1043 |
// checks out |
|
1044 |
||
1045 |
// Verify that the session ciphers are unchanged. |
|
1046 |
CipherSuite sessionSuite = chc.resumingSession.getSuite(); |
|
1047 |
if (chc.negotiatedCipherSuite != sessionSuite) { |
|
1048 |
chc.conContext.fatal(Alert.PROTOCOL_VERSION, |
|
1049 |
"Server returned wrong cipher suite for session"); |
|
1050 |
} |
|
1051 |
||
1052 |
// verify protocol version match |
|
1053 |
ProtocolVersion sessionVersion = |
|
1054 |
chc.resumingSession.getProtocolVersion(); |
|
1055 |
if (chc.negotiatedProtocol != sessionVersion) { |
|
1056 |
chc.conContext.fatal(Alert.PROTOCOL_VERSION, |
|
1057 |
"Server resumed with wrong protocol version"); |
|
1058 |
} |
|
1059 |
||
1060 |
// looks fine; resume it. |
|
1061 |
chc.isResumption = true; |
|
1062 |
chc.resumingSession.setAsSessionResumption(true); |
|
1063 |
chc.handshakeSession = chc.resumingSession; |
|
1064 |
} else { |
|
1065 |
// we wanted to resume, but the server refused |
|
1066 |
// |
|
1067 |
// Invalidate the session for initial handshake in case |
|
1068 |
// of reusing next time. |
|
1069 |
if (chc.resumingSession != null) { |
|
1070 |
chc.resumingSession.invalidate(); |
|
1071 |
chc.resumingSession = null; |
|
1072 |
} |
|
1073 |
chc.isResumption = false; |
|
1074 |
if (!chc.sslConfig.enableSessionCreation) { |
|
1075 |
chc.conContext.fatal(Alert.PROTOCOL_VERSION, |
|
1076 |
"New session creation is disabled"); |
|
1077 |
} |
|
1078 |
} |
|
1079 |
} |
|
1080 |
||
1081 |
// Check and launch ClientHello extensions. |
|
1082 |
extTypes = chc.sslConfig.getEnabledExtensions( |
|
1083 |
SSLHandshake.SERVER_HELLO); |
|
1084 |
serverHello.extensions.consumeOnLoad(chc, extTypes); |
|
1085 |
||
1086 |
if (!chc.isResumption) { |
|
1087 |
if (chc.resumingSession != null) { |
|
1088 |
// in case the resumption happens next time. |
|
1089 |
chc.resumingSession.invalidate(); |
|
1090 |
chc.resumingSession = null; |
|
1091 |
} |
|
1092 |
||
1093 |
if (!chc.sslConfig.enableSessionCreation) { |
|
1094 |
chc.conContext.fatal(Alert.PROTOCOL_VERSION, |
|
1095 |
"New session creation is disabled"); |
|
1096 |
} |
|
1097 |
chc.handshakeSession = new SSLSessionImpl(chc, |
|
1098 |
chc.negotiatedCipherSuite, |
|
1099 |
serverHello.sessionId); |
|
1100 |
chc.handshakeSession.setMaximumPacketSize( |
|
1101 |
chc.sslConfig.maximumPacketSize); |
|
1102 |
} |
|
1103 |
||
1104 |
// |
|
1105 |
// update |
|
1106 |
// |
|
1107 |
serverHello.extensions.consumeOnTrade(chc, extTypes); |
|
1108 |
||
1109 |
// update the consumers and producers |
|
1110 |
if (chc.isResumption) { |
|
1111 |
SSLTrafficKeyDerivation kdg = |
|
1112 |
SSLTrafficKeyDerivation.valueOf(chc.negotiatedProtocol); |
|
1113 |
if (kdg == null) { |
|
1114 |
// unlikely |
|
1115 |
chc.conContext.fatal(Alert.INTERNAL_ERROR, |
|
1116 |
"Not supported key derivation: " + |
|
1117 |
chc.negotiatedProtocol); |
|
1118 |
} else { |
|
1119 |
chc.handshakeKeyDerivation = kdg.createKeyDerivation( |
|
1120 |
chc, chc.resumingSession.getMasterSecret()); |
|
1121 |
} |
|
1122 |
||
1123 |
chc.conContext.consumers.putIfAbsent( |
|
1124 |
ContentType.CHANGE_CIPHER_SPEC.id, |
|
1125 |
ChangeCipherSpec.t10Consumer); |
|
1126 |
chc.handshakeConsumers.put( |
|
1127 |
SSLHandshake.FINISHED.id, |
|
1128 |
SSLHandshake.FINISHED); |
|
1129 |
} else { |
|
1130 |
SSLKeyExchange ke = SSLKeyExchange.valueOf( |
|
1131 |
chc.negotiatedCipherSuite.keyExchange, |
|
1132 |
chc.negotiatedProtocol); |
|
1133 |
chc.handshakeKeyExchange = ke; |
|
1134 |
if (ke != null) { |
|
1135 |
for (SSLHandshake handshake : |
|
1136 |
ke.getRelatedHandshakers(chc)) { |
|
1137 |
chc.handshakeConsumers.put(handshake.id, handshake); |
|
1138 |
} |
|
1139 |
} |
|
1140 |
||
1141 |
chc.handshakeConsumers.put(SSLHandshake.SERVER_HELLO_DONE.id, |
|
1142 |
SSLHandshake.SERVER_HELLO_DONE); |
|
1143 |
} |
|
1144 |
||
1145 |
// |
|
1146 |
// produce |
|
1147 |
// |
|
1148 |
// Need no new handshake message producers here. |
|
1149 |
} |
|
1150 |
} |
|
1151 |
||
1152 |
private static void setUpPskKD(HandshakeContext hc, |
|
1153 |
SecretKey psk) throws SSLHandshakeException { |
|
1154 |
||
1155 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
1156 |
SSLLogger.fine("Using PSK to derive early secret"); |
|
1157 |
} |
|
1158 |
||
1159 |
try { |
|
1160 |
CipherSuite.HashAlg hashAlg = hc.negotiatedCipherSuite.hashAlg; |
|
1161 |
HKDF hkdf = new HKDF(hashAlg.name); |
|
1162 |
byte[] zeros = new byte[hashAlg.hashLength]; |
|
1163 |
SecretKey earlySecret = hkdf.extract(zeros, psk, "TlsEarlySecret"); |
|
1164 |
hc.handshakeKeyDerivation = |
|
1165 |
new SSLSecretDerivation(hc, earlySecret); |
|
1166 |
} catch (GeneralSecurityException gse) { |
|
1167 |
throw (SSLHandshakeException) new SSLHandshakeException( |
|
1168 |
"Could not generate secret").initCause(gse); |
|
1169 |
} |
|
1170 |
} |
|
1171 |
||
1172 |
private static final |
|
1173 |
class T13ServerHelloConsumer implements HandshakeConsumer { |
|
1174 |
// Prevent instantiation of this class. |
|
1175 |
private T13ServerHelloConsumer() { |
|
1176 |
// blank |
|
1177 |
} |
|
1178 |
||
1179 |
@Override |
|
1180 |
public void consume(ConnectionContext context, |
|
1181 |
HandshakeMessage message) throws IOException { |
|
1182 |
// The consuming happens in client side only. |
|
1183 |
ClientHandshakeContext chc = (ClientHandshakeContext)context; |
|
1184 |
ServerHelloMessage serverHello = (ServerHelloMessage)message; |
|
1185 |
if (serverHello.serverVersion != ProtocolVersion.TLS12) { |
|
1186 |
chc.conContext.fatal(Alert.PROTOCOL_VERSION, |
|
1187 |
"The ServerHello.legacy_version field is not TLS 1.2"); |
|
1188 |
} |
|
1189 |
||
1190 |
chc.negotiatedCipherSuite = serverHello.cipherSuite; |
|
1191 |
chc.handshakeHash.determine( |
|
1192 |
chc.negotiatedProtocol, chc.negotiatedCipherSuite); |
|
1193 |
chc.serverHelloRandom = serverHello.serverRandom; |
|
1194 |
||
1195 |
// |
|
1196 |
// validate |
|
1197 |
// |
|
1198 |
||
1199 |
// Check and launch ServerHello extensions. |
|
1200 |
SSLExtension[] extTypes = chc.sslConfig.getEnabledExtensions( |
|
1201 |
SSLHandshake.SERVER_HELLO); |
|
1202 |
serverHello.extensions.consumeOnLoad(chc, extTypes); |
|
1203 |
if (!chc.isResumption) { |
|
1204 |
if (chc.resumingSession != null) { |
|
1205 |
// in case the resumption happens next time. |
|
1206 |
chc.resumingSession.invalidate(); |
|
1207 |
chc.resumingSession = null; |
|
1208 |
} |
|
1209 |
||
1210 |
if (!chc.sslConfig.enableSessionCreation) { |
|
1211 |
chc.conContext.fatal(Alert.PROTOCOL_VERSION, |
|
1212 |
"New session creation is disabled"); |
|
1213 |
} |
|
1214 |
chc.handshakeSession = new SSLSessionImpl(chc, |
|
1215 |
chc.negotiatedCipherSuite, |
|
1216 |
serverHello.sessionId); |
|
1217 |
chc.handshakeSession.setMaximumPacketSize( |
|
1218 |
chc.sslConfig.maximumPacketSize); |
|
1219 |
} else { |
|
1220 |
// The PSK is consumed to allow it to be deleted |
|
1221 |
Optional<SecretKey> psk = |
|
1222 |
chc.resumingSession.consumePreSharedKey(); |
|
1223 |
if(!psk.isPresent()) { |
|
1224 |
chc.conContext.fatal(Alert.INTERNAL_ERROR, |
|
1225 |
"No PSK available. Unable to resume."); |
|
1226 |
} |
|
1227 |
||
1228 |
chc.handshakeSession = chc.resumingSession; |
|
1229 |
||
1230 |
setUpPskKD(chc, psk.get()); |
|
1231 |
} |
|
1232 |
||
1233 |
// |
|
1234 |
// update |
|
1235 |
// |
|
1236 |
serverHello.extensions.consumeOnTrade(chc, extTypes); |
|
1237 |
||
1238 |
// Change client/server handshake traffic secrets. |
|
1239 |
// Refresh handshake hash |
|
1240 |
chc.handshakeHash.update(); |
|
1241 |
||
1242 |
SSLKeyExchange ke = chc.handshakeKeyExchange; |
|
1243 |
if (ke == null) { |
|
1244 |
// unlikely |
|
1245 |
chc.conContext.fatal(Alert.INTERNAL_ERROR, |
|
1246 |
"Not negotiated key shares"); |
|
1247 |
return; // make the compiler happy |
|
1248 |
} |
|
1249 |
||
1250 |
SSLKeyDerivation handshakeKD = ke.createKeyDerivation(chc); |
|
1251 |
SecretKey handshakeSecret = handshakeKD.deriveKey( |
|
1252 |
"TlsHandshakeSecret", null); |
|
1253 |
SSLTrafficKeyDerivation kdg = |
|
1254 |
SSLTrafficKeyDerivation.valueOf(chc.negotiatedProtocol); |
|
1255 |
if (kdg == null) { |
|
1256 |
// unlikely |
|
1257 |
chc.conContext.fatal(Alert.INTERNAL_ERROR, |
|
1258 |
"Not supported key derivation: " + |
|
1259 |
chc.negotiatedProtocol); |
|
1260 |
return; // make the compiler happy |
|
1261 |
} |
|
1262 |
||
1263 |
SSLKeyDerivation secretKD = |
|
1264 |
new SSLSecretDerivation(chc, handshakeSecret); |
|
1265 |
||
1266 |
// update the handshake traffic read keys. |
|
1267 |
SecretKey readSecret = secretKD.deriveKey( |
|
1268 |
"TlsServerHandshakeTrafficSecret", null); |
|
1269 |
||
1270 |
SSLKeyDerivation readKD = |
|
1271 |
kdg.createKeyDerivation(chc, readSecret); |
|
1272 |
SecretKey readKey = readKD.deriveKey( |
|
1273 |
"TlsKey", null); |
|
1274 |
SecretKey readIvSecret = readKD.deriveKey( |
|
1275 |
"TlsIv", null); |
|
1276 |
IvParameterSpec readIv = |
|
1277 |
new IvParameterSpec(readIvSecret.getEncoded()); |
|
1278 |
SSLReadCipher readCipher; |
|
1279 |
try { |
|
1280 |
readCipher = |
|
1281 |
chc.negotiatedCipherSuite.bulkCipher.createReadCipher( |
|
1282 |
Authenticator.valueOf(chc.negotiatedProtocol), |
|
1283 |
chc.negotiatedProtocol, readKey, readIv, |
|
1284 |
chc.sslContext.getSecureRandom()); |
|
1285 |
} catch (GeneralSecurityException gse) { |
|
1286 |
// unlikely |
|
1287 |
chc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
|
1288 |
"Missing cipher algorithm", gse); |
|
1289 |
return; // make the compiler happy |
|
1290 |
} |
|
1291 |
||
1292 |
chc.baseReadSecret = readSecret; |
|
1293 |
chc.conContext.inputRecord.changeReadCiphers(readCipher); |
|
1294 |
||
1295 |
// update the handshake traffic write keys. |
|
1296 |
SecretKey writeSecret = secretKD.deriveKey( |
|
1297 |
"TlsClientHandshakeTrafficSecret", null); |
|
1298 |
SSLKeyDerivation writeKD = |
|
1299 |
kdg.createKeyDerivation(chc, writeSecret); |
|
1300 |
SecretKey writeKey = writeKD.deriveKey( |
|
1301 |
"TlsKey", null); |
|
1302 |
SecretKey writeIvSecret = writeKD.deriveKey( |
|
1303 |
"TlsIv", null); |
|
1304 |
IvParameterSpec writeIv = |
|
1305 |
new IvParameterSpec(writeIvSecret.getEncoded()); |
|
1306 |
SSLWriteCipher writeCipher; |
|
1307 |
try { |
|
1308 |
writeCipher = |
|
1309 |
chc.negotiatedCipherSuite.bulkCipher.createWriteCipher( |
|
1310 |
Authenticator.valueOf(chc.negotiatedProtocol), |
|
1311 |
chc.negotiatedProtocol, writeKey, writeIv, |
|
1312 |
chc.sslContext.getSecureRandom()); |
|
1313 |
} catch (GeneralSecurityException gse) { |
|
1314 |
// unlikely |
|
1315 |
chc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
|
1316 |
"Missing cipher algorithm", gse); |
|
1317 |
return; // make the compiler happy |
|
1318 |
} |
|
1319 |
||
1320 |
chc.baseWriteSecret = writeSecret; |
|
1321 |
chc.conContext.outputRecord.changeWriteCiphers( |
|
1322 |
writeCipher, (serverHello.sessionId.length() != 0)); |
|
1323 |
||
1324 |
// Should use resumption_master_secret for TLS 1.3. |
|
1325 |
// chc.handshakeSession.setMasterSecret(masterSecret); |
|
1326 |
||
1327 |
// Update the context for master key derivation. |
|
1328 |
chc.handshakeKeyDerivation = secretKD; |
|
1329 |
||
1330 |
// update the consumers and producers |
|
1331 |
// |
|
1332 |
// The server sends a dummy change_cipher_spec record immediately |
|
1333 |
// after its first handshake message. This may either be after a |
|
1334 |
// ServerHello or a HelloRetryRequest. |
|
1335 |
chc.conContext.consumers.putIfAbsent( |
|
1336 |
ContentType.CHANGE_CIPHER_SPEC.id, |
|
1337 |
ChangeCipherSpec.t13Consumer); |
|
1338 |
||
1339 |
chc.handshakeConsumers.put( |
|
1340 |
SSLHandshake.ENCRYPTED_EXTENSIONS.id, |
|
1341 |
SSLHandshake.ENCRYPTED_EXTENSIONS); |
|
1342 |
||
1343 |
// Support cert authentication only, when not PSK. |
|
1344 |
chc.handshakeConsumers.put( |
|
1345 |
SSLHandshake.CERTIFICATE_REQUEST.id, |
|
1346 |
SSLHandshake.CERTIFICATE_REQUEST); |
|
1347 |
chc.handshakeConsumers.put( |
|
1348 |
SSLHandshake.CERTIFICATE.id, |
|
1349 |
SSLHandshake.CERTIFICATE); |
|
1350 |
chc.handshakeConsumers.put( |
|
1351 |
SSLHandshake.CERTIFICATE_VERIFY.id, |
|
1352 |
SSLHandshake.CERTIFICATE_VERIFY); |
|
1353 |
||
1354 |
chc.handshakeConsumers.put( |
|
1355 |
SSLHandshake.FINISHED.id, |
|
1356 |
SSLHandshake.FINISHED); |
|
1357 |
||
1358 |
// |
|
1359 |
// produce |
|
1360 |
// |
|
1361 |
// Need no new handshake message producers here. |
|
1362 |
} |
|
1363 |
} |
|
1364 |
||
1365 |
private static final |
|
1366 |
class T13HelloRetryRequestConsumer implements HandshakeConsumer { |
|
1367 |
// Prevent instantiation of this class. |
|
1368 |
private T13HelloRetryRequestConsumer() { |
|
1369 |
// blank |
|
1370 |
} |
|
1371 |
||
1372 |
@Override |
|
1373 |
public void consume(ConnectionContext context, |
|
1374 |
HandshakeMessage message) throws IOException { |
|
1375 |
// The consuming happens in client side only. |
|
1376 |
ClientHandshakeContext chc = (ClientHandshakeContext)context; |
|
1377 |
ServerHelloMessage helloRetryRequest = (ServerHelloMessage)message; |
|
1378 |
if (helloRetryRequest.serverVersion != ProtocolVersion.TLS12) { |
|
1379 |
chc.conContext.fatal(Alert.PROTOCOL_VERSION, |
|
1380 |
"The HelloRetryRequest.legacy_version is not TLS 1.2"); |
|
1381 |
} |
|
1382 |
||
1383 |
chc.negotiatedCipherSuite = helloRetryRequest.cipherSuite; |
|
1384 |
||
1385 |
// |
|
1386 |
// validate |
|
1387 |
// |
|
1388 |
||
1389 |
// Check and launch ClientHello extensions. |
|
1390 |
SSLExtension[] extTypes = chc.sslConfig.getEnabledExtensions( |
|
1391 |
SSLHandshake.HELLO_RETRY_REQUEST); |
|
1392 |
helloRetryRequest.extensions.consumeOnLoad(chc, extTypes); |
|
1393 |
||
1394 |
// |
|
1395 |
// update |
|
1396 |
// |
|
1397 |
helloRetryRequest.extensions.consumeOnTrade(chc, extTypes); |
|
1398 |
||
1399 |
// Change client/server handshake traffic secrets. |
|
1400 |
// Refresh handshake hash |
|
1401 |
chc.handshakeHash.finish(); // reset the handshake hash |
|
1402 |
||
1403 |
// calculate the transcript hash of the 1st ClientHello message |
|
1404 |
HandshakeOutStream hos = new HandshakeOutStream(null); |
|
1405 |
try { |
|
1406 |
chc.initialClientHelloMsg.write(hos); |
|
1407 |
} catch (IOException ioe) { |
|
1408 |
// unlikely |
|
1409 |
chc.conContext.fatal(Alert.HANDSHAKE_FAILURE, |
|
1410 |
"Failed to construct message hash", ioe); |
|
1411 |
} |
|
1412 |
chc.handshakeHash.deliver(hos.toByteArray()); |
|
1413 |
chc.handshakeHash.determine( |
|
1414 |
chc.negotiatedProtocol, chc.negotiatedCipherSuite); |
|
1415 |
byte[] clientHelloHash = chc.handshakeHash.digest(); |
|
1416 |
||
1417 |
// calculate the message_hash |
|
1418 |
// |
|
1419 |
// Transcript-Hash(ClientHello1, HelloRetryRequest, ... Mn) = |
|
1420 |
// Hash(message_hash || /* Handshake type */ |
|
1421 |
// 00 00 Hash.length || /* Handshake message length (bytes) */ |
|
1422 |
// Hash(ClientHello1) || /* Hash of ClientHello1 */ |
|
1423 |
// HelloRetryRequest || ... || Mn) |
|
1424 |
int hashLen = chc.negotiatedCipherSuite.hashAlg.hashLength; |
|
1425 |
byte[] hashedClientHello = new byte[4 + hashLen]; |
|
1426 |
hashedClientHello[0] = SSLHandshake.MESSAGE_HASH.id; |
|
1427 |
hashedClientHello[1] = (byte)0x00; |
|
1428 |
hashedClientHello[2] = (byte)0x00; |
|
1429 |
hashedClientHello[3] = (byte)(hashLen & 0xFF); |
|
1430 |
System.arraycopy(clientHelloHash, 0, |
|
1431 |
hashedClientHello, 4, hashLen); |
|
1432 |
||
1433 |
chc.handshakeHash.finish(); // reset the handshake hash |
|
1434 |
chc.handshakeHash.deliver(hashedClientHello); |
|
1435 |
||
1436 |
int hrrBodyLen = helloRetryRequest.handshakeRecord.remaining(); |
|
1437 |
byte[] hrrMessage = new byte[4 + hrrBodyLen]; |
|
1438 |
hrrMessage[0] = SSLHandshake.HELLO_RETRY_REQUEST.id; |
|
1439 |
hrrMessage[1] = (byte)((hrrBodyLen >> 16) & 0xFF); |
|
1440 |
hrrMessage[2] = (byte)((hrrBodyLen >> 8) & 0xFF); |
|
1441 |
hrrMessage[3] = (byte)(hrrBodyLen & 0xFF); |
|
1442 |
||
1443 |
ByteBuffer hrrBody = helloRetryRequest.handshakeRecord.duplicate(); |
|
1444 |
hrrBody.get(hrrMessage, 4, hrrBodyLen); |
|
1445 |
||
1446 |
chc.handshakeHash.receive(hrrMessage); |
|
1447 |
||
1448 |
// Update the initial ClientHello handshake message. |
|
1449 |
chc.initialClientHelloMsg.extensions.reproduce(chc, |
|
1450 |
new SSLExtension[] { |
|
1451 |
SSLExtension.CH_COOKIE, |
|
1452 |
SSLExtension.CH_KEY_SHARE, |
|
1453 |
SSLExtension.CH_PRE_SHARED_KEY |
|
1454 |
}); |
|
1455 |
||
1456 |
// |
|
1457 |
// produce response handshake message |
|
1458 |
// |
|
1459 |
SSLHandshake.CLIENT_HELLO.produce(context, helloRetryRequest); |
|
1460 |
} |
|
1461 |
} |
|
1462 |
} |