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