author | xuelei |
Thu, 24 May 2018 09:21:01 -0700 | |
branch | JDK-8145252-TLS13-branch |
changeset 56603 | f103e0c2be1e |
parent 56584 | a0f3377c58c7 |
child 56704 | c3ee22c3a0f6 |
permissions | -rw-r--r-- |
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.GeneralSecurityException; |
|
31 |
import java.security.InvalidKeyException; |
|
32 |
import java.security.MessageDigest; |
|
33 |
import java.security.NoSuchAlgorithmException; |
|
34 |
import java.security.ProviderException; |
|
35 |
import java.security.spec.AlgorithmParameterSpec; |
|
36 |
import java.text.MessageFormat; |
|
37 |
import java.util.Locale; |
|
38 |
import javax.crypto.KeyGenerator; |
|
39 |
import javax.crypto.Mac; |
|
40 |
import javax.crypto.SecretKey; |
|
41 |
import javax.crypto.spec.IvParameterSpec; |
|
42 |
import javax.crypto.spec.SecretKeySpec; |
|
43 |
import sun.security.internal.spec.TlsPrfParameterSpec; |
|
44 |
import sun.security.ssl.CipherSuite.HashAlg; |
|
45 |
import static sun.security.ssl.CipherSuite.HashAlg.H_NONE; |
|
46 |
import sun.security.ssl.SSLBasicKeyDerivation.SecretSizeSpec; |
|
47 |
import sun.security.ssl.SSLCipher.SSLReadCipher; |
|
48 |
import sun.security.ssl.SSLCipher.SSLWriteCipher; |
|
49 |
import sun.security.ssl.SSLHandshake.HandshakeMessage; |
|
50 |
import sun.security.util.HexDumpEncoder; |
|
51 |
||
52 |
/** |
|
53 |
* Pack of the Finished handshake message. |
|
54 |
*/ |
|
55 |
final class Finished { |
|
56 |
static final SSLConsumer t12HandshakeConsumer = |
|
57 |
new T12FinishedConsumer(); |
|
58 |
static final HandshakeProducer t12HandshakeProducer = |
|
59 |
new T12FinishedProducer(); |
|
60 |
||
61 |
static final SSLConsumer t13HandshakeConsumer = |
|
62 |
new T13FinishedConsumer(); |
|
63 |
static final HandshakeProducer t13HandshakeProducer = |
|
64 |
new T13FinishedProducer(); |
|
65 |
||
66 |
/** |
|
67 |
* The Finished handshake message. |
|
68 |
*/ |
|
69 |
private static final class FinishedMessage extends HandshakeMessage { |
|
70 |
private final byte[] verifyData; |
|
71 |
||
72 |
FinishedMessage(HandshakeContext context) throws IOException { |
|
73 |
super(context); |
|
74 |
||
75 |
VerifyDataScheme vds = |
|
76 |
VerifyDataScheme.valueOf(context.negotiatedProtocol); |
|
77 |
||
78 |
byte[] vd = null; |
|
79 |
try { |
|
80 |
vd = vds.createVerifyData(context, false); |
|
81 |
} catch (IOException ioe) { |
|
82 |
context.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
|
83 |
"Failed to generate verify_data", ioe); |
|
84 |
} |
|
85 |
||
86 |
this.verifyData = vd; |
|
87 |
} |
|
88 |
||
89 |
FinishedMessage(HandshakeContext context, |
|
90 |
ByteBuffer m) throws IOException { |
|
91 |
super(context); |
|
92 |
int verifyDataLen = 12; |
|
93 |
if (context.negotiatedProtocol == ProtocolVersion.SSL30) { |
|
94 |
verifyDataLen = 36; |
|
95 |
} else if (context.negotiatedProtocol.useTLS13PlusSpec()) { |
|
96 |
verifyDataLen = |
|
97 |
context.negotiatedCipherSuite.hashAlg.hashLength; |
|
98 |
} |
|
99 |
||
100 |
if (m.remaining() != verifyDataLen) { |
|
101 |
context.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
|
102 |
"Inappropriate finished message: need " + verifyDataLen + |
|
103 |
" but remine " + m.remaining() + " bytes verify_data"); |
|
104 |
} |
|
105 |
||
106 |
this.verifyData = new byte[verifyDataLen]; |
|
107 |
m.get(verifyData); |
|
108 |
||
109 |
VerifyDataScheme vd = |
|
110 |
VerifyDataScheme.valueOf(context.negotiatedProtocol); |
|
111 |
byte[] myVerifyData; |
|
112 |
try { |
|
113 |
myVerifyData = vd.createVerifyData(context, true); |
|
114 |
} catch (IOException ioe) { |
|
115 |
context.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
|
116 |
"Failed to generate verify_data", ioe); |
|
117 |
return; // make the compiler happy |
|
118 |
} |
|
119 |
if (!MessageDigest.isEqual(myVerifyData, verifyData)) { |
|
120 |
context.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
|
121 |
"The Finished message cannot be verified."); |
|
122 |
} |
|
123 |
} |
|
124 |
||
125 |
@Override |
|
126 |
public SSLHandshake handshakeType() { |
|
127 |
return SSLHandshake.FINISHED; |
|
128 |
} |
|
129 |
||
130 |
@Override |
|
131 |
public int messageLength() { |
|
132 |
return verifyData.length; |
|
133 |
} |
|
134 |
||
135 |
@Override |
|
136 |
public void send(HandshakeOutStream hos) throws IOException { |
|
137 |
hos.write(verifyData); |
|
138 |
} |
|
139 |
||
140 |
@Override |
|
141 |
public String toString() { |
|
142 |
MessageFormat messageFormat = new MessageFormat( |
|
143 |
"\"Finished\": '{'\n" + |
|
144 |
" \"verify data\": '{'\n" + |
|
145 |
"{0}\n" + |
|
146 |
" '}'" + |
|
147 |
"'}'", |
|
148 |
Locale.ENGLISH); |
|
149 |
||
150 |
HexDumpEncoder hexEncoder = new HexDumpEncoder(); |
|
151 |
Object[] messageFields = { |
|
152 |
Utilities.indent(hexEncoder.encode(verifyData), " "), |
|
153 |
}; |
|
154 |
return messageFormat.format(messageFields); |
|
155 |
} |
|
156 |
} |
|
157 |
||
158 |
interface VerifyDataGenerator { |
|
159 |
byte[] createVerifyData(HandshakeContext context, |
|
160 |
boolean isValidation) throws IOException; |
|
161 |
} |
|
162 |
||
163 |
enum VerifyDataScheme { |
|
164 |
SSL30 ("kdf_ssl30", new S30VerifyDataGenerator()), |
|
165 |
TLS10 ("kdf_tls10", new T10VerifyDataGenerator()), |
|
166 |
TLS12 ("kdf_tls12", new T12VerifyDataGenerator()), |
|
167 |
TLS13 ("kdf_tls13", new T13VerifyDataGenerator()); |
|
168 |
||
169 |
final String name; |
|
170 |
final VerifyDataGenerator generator; |
|
171 |
||
172 |
VerifyDataScheme(String name, VerifyDataGenerator verifyDataGenerator) { |
|
173 |
this.name = name; |
|
174 |
this.generator = verifyDataGenerator; |
|
175 |
} |
|
176 |
||
177 |
static VerifyDataScheme valueOf(ProtocolVersion protocolVersion) { |
|
178 |
switch (protocolVersion) { |
|
179 |
case SSL30: |
|
180 |
return VerifyDataScheme.SSL30; |
|
181 |
case TLS10: |
|
182 |
case TLS11: |
|
183 |
case DTLS10: |
|
184 |
return VerifyDataScheme.TLS10; |
|
185 |
case TLS12: |
|
186 |
case DTLS12: |
|
187 |
return VerifyDataScheme.TLS12; |
|
188 |
case TLS13: |
|
189 |
case DTLS13: |
|
190 |
return VerifyDataScheme.TLS13; |
|
191 |
default: |
|
192 |
return null; |
|
193 |
} |
|
194 |
} |
|
195 |
||
196 |
public byte[] createVerifyData(HandshakeContext context, |
|
197 |
boolean isValidation) throws IOException { |
|
198 |
if (generator != null) { |
|
199 |
return generator.createVerifyData(context, isValidation); |
|
200 |
} |
|
201 |
||
202 |
throw new UnsupportedOperationException("Not supported yet."); |
|
203 |
} |
|
204 |
} |
|
205 |
||
206 |
// SSL 3.0 |
|
207 |
private static final |
|
208 |
class S30VerifyDataGenerator implements VerifyDataGenerator { |
|
209 |
@Override |
|
210 |
public byte[] createVerifyData(HandshakeContext context, |
|
211 |
boolean isValidation) throws IOException { |
|
212 |
HandshakeHash handshakeHash = context.handshakeHash; |
|
213 |
SecretKey masterSecretKey = |
|
214 |
context.handshakeSession.getMasterSecret(); |
|
215 |
||
216 |
boolean useClientLabel = |
|
217 |
(context.sslConfig.isClientMode && !isValidation) || |
|
218 |
(!context.sslConfig.isClientMode && isValidation); |
|
219 |
return handshakeHash.digest(useClientLabel, masterSecretKey); |
|
220 |
} |
|
221 |
} |
|
222 |
||
223 |
// TLS 1.0, TLS 1.1, DTLS 1.0 |
|
224 |
private static final |
|
225 |
class T10VerifyDataGenerator implements VerifyDataGenerator { |
|
226 |
@Override |
|
227 |
public byte[] createVerifyData(HandshakeContext context, |
|
228 |
boolean isValidation) throws IOException { |
|
229 |
HandshakeHash handshakeHash = context.handshakeHash; |
|
230 |
SecretKey masterSecretKey = |
|
231 |
context.handshakeSession.getMasterSecret(); |
|
232 |
||
233 |
boolean useClientLabel = |
|
234 |
(context.sslConfig.isClientMode && !isValidation) || |
|
235 |
(!context.sslConfig.isClientMode && isValidation); |
|
236 |
String tlsLabel; |
|
237 |
if (useClientLabel) { |
|
238 |
tlsLabel = "client finished"; |
|
239 |
} else { |
|
240 |
tlsLabel = "server finished"; |
|
241 |
} |
|
242 |
||
243 |
try { |
|
244 |
byte[] seed = handshakeHash.digest(); |
|
245 |
String prfAlg = "SunTlsPrf"; |
|
246 |
HashAlg hashAlg = H_NONE; |
|
247 |
||
248 |
/* |
|
249 |
* RFC 5246/7.4.9 says that finished messages can |
|
250 |
* be ciphersuite-specific in both length/PRF hash |
|
251 |
* algorithm. If we ever run across a different |
|
252 |
* length, this call will need to be updated. |
|
253 |
*/ |
|
254 |
@SuppressWarnings("deprecation") |
|
255 |
TlsPrfParameterSpec spec = new TlsPrfParameterSpec( |
|
256 |
masterSecretKey, tlsLabel, seed, 12, |
|
257 |
hashAlg.name, hashAlg.hashLength, hashAlg.blockSize); |
|
258 |
KeyGenerator kg = JsseJce.getKeyGenerator(prfAlg); |
|
259 |
kg.init(spec); |
|
260 |
SecretKey prfKey = kg.generateKey(); |
|
261 |
if (!"RAW".equals(prfKey.getFormat())) { |
|
262 |
throw new ProviderException( |
|
263 |
"Invalid PRF output, format must be RAW. " + |
|
264 |
"Format received: " + prfKey.getFormat()); |
|
265 |
} |
|
266 |
byte[] finished = prfKey.getEncoded(); |
|
267 |
return finished; |
|
268 |
} catch (GeneralSecurityException e) { |
|
269 |
throw new RuntimeException("PRF failed", e); |
|
270 |
} |
|
271 |
} |
|
272 |
} |
|
273 |
||
274 |
// TLS 1.2 |
|
275 |
private static final |
|
276 |
class T12VerifyDataGenerator implements VerifyDataGenerator { |
|
277 |
@Override |
|
278 |
public byte[] createVerifyData(HandshakeContext context, |
|
279 |
boolean isValidation) throws IOException { |
|
280 |
CipherSuite cipherSuite = context.negotiatedCipherSuite; |
|
281 |
HandshakeHash handshakeHash = context.handshakeHash; |
|
282 |
SecretKey masterSecretKey = |
|
283 |
context.handshakeSession.getMasterSecret(); |
|
284 |
||
285 |
boolean useClientLabel = |
|
286 |
(context.sslConfig.isClientMode && !isValidation) || |
|
287 |
(!context.sslConfig.isClientMode && isValidation); |
|
288 |
String tlsLabel; |
|
289 |
if (useClientLabel) { |
|
290 |
tlsLabel = "client finished"; |
|
291 |
} else { |
|
292 |
tlsLabel = "server finished"; |
|
293 |
} |
|
294 |
||
295 |
try { |
|
296 |
byte[] seed = handshakeHash.digest(); |
|
297 |
String prfAlg = "SunTls12Prf"; |
|
298 |
HashAlg hashAlg = cipherSuite.hashAlg; |
|
299 |
||
300 |
/* |
|
301 |
* RFC 5246/7.4.9 says that finished messages can |
|
302 |
* be ciphersuite-specific in both length/PRF hash |
|
303 |
* algorithm. If we ever run across a different |
|
304 |
* length, this call will need to be updated. |
|
305 |
*/ |
|
306 |
@SuppressWarnings("deprecation") |
|
307 |
TlsPrfParameterSpec spec = new TlsPrfParameterSpec( |
|
308 |
masterSecretKey, tlsLabel, seed, 12, |
|
309 |
hashAlg.name, hashAlg.hashLength, hashAlg.blockSize); |
|
310 |
KeyGenerator kg = JsseJce.getKeyGenerator(prfAlg); |
|
311 |
kg.init(spec); |
|
312 |
SecretKey prfKey = kg.generateKey(); |
|
313 |
if (!"RAW".equals(prfKey.getFormat())) { |
|
314 |
throw new ProviderException( |
|
315 |
"Invalid PRF output, format must be RAW. " + |
|
316 |
"Format received: " + prfKey.getFormat()); |
|
317 |
} |
|
318 |
byte[] finished = prfKey.getEncoded(); |
|
319 |
return finished; |
|
320 |
} catch (GeneralSecurityException e) { |
|
321 |
throw new RuntimeException("PRF failed", e); |
|
322 |
} |
|
323 |
} |
|
324 |
} |
|
325 |
||
326 |
// TLS 1.2 |
|
327 |
private static final |
|
328 |
class T13VerifyDataGenerator implements VerifyDataGenerator { |
|
329 |
private static final byte[] hkdfLabel = "tls13 finished".getBytes(); |
|
330 |
private static final byte[] hkdfContext = new byte[0]; |
|
331 |
||
332 |
@Override |
|
333 |
public byte[] createVerifyData(HandshakeContext context, |
|
334 |
boolean isValidation) throws IOException { |
|
335 |
// create finished secret key |
|
336 |
HashAlg hashAlg = |
|
337 |
context.negotiatedCipherSuite.hashAlg; |
|
338 |
SecretKey secret = isValidation ? |
|
339 |
context.baseReadSecret : context.baseWriteSecret; |
|
340 |
SSLBasicKeyDerivation kdf = new SSLBasicKeyDerivation( |
|
341 |
secret, hashAlg.name, |
|
342 |
hkdfLabel, hkdfContext, hashAlg.hashLength); |
|
343 |
AlgorithmParameterSpec keySpec = |
|
344 |
new SecretSizeSpec(hashAlg.hashLength); |
|
345 |
SecretKey finishedSecret = |
|
346 |
kdf.deriveKey("TlsFinishedSecret", keySpec); |
|
347 |
||
348 |
String hmacAlg = |
|
349 |
"Hmac" + hashAlg.name.replace("-", ""); |
|
350 |
try { |
|
351 |
Mac hmac = JsseJce.getMac(hmacAlg); |
|
352 |
hmac.init(finishedSecret); |
|
353 |
return hmac.doFinal(context.handshakeHash.digest()); |
|
354 |
} catch (NoSuchAlgorithmException |InvalidKeyException ex) { |
|
355 |
throw new ProviderException( |
|
356 |
"Failed to generate verify_data", ex); |
|
357 |
} |
|
358 |
} |
|
359 |
} |
|
360 |
||
361 |
/** |
|
362 |
* The "Finished" handshake message producer. |
|
363 |
*/ |
|
364 |
private static final |
|
365 |
class T12FinishedProducer implements HandshakeProducer { |
|
366 |
// Prevent instantiation of this class. |
|
367 |
private T12FinishedProducer() { |
|
368 |
// blank |
|
369 |
} |
|
370 |
||
371 |
@Override |
|
372 |
public byte[] produce(ConnectionContext context, |
|
373 |
HandshakeMessage message) throws IOException { |
|
374 |
// The consuming happens in handshake context only. |
|
375 |
HandshakeContext hc = (HandshakeContext)context; |
|
376 |
if (hc.sslConfig.isClientMode) { |
|
377 |
return onProduceFinished( |
|
378 |
(ClientHandshakeContext)context, message); |
|
379 |
} else { |
|
380 |
return onProduceFinished( |
|
381 |
(ServerHandshakeContext)context, message); |
|
382 |
} |
|
383 |
} |
|
384 |
||
385 |
private byte[] onProduceFinished(ClientHandshakeContext chc, |
|
386 |
HandshakeMessage message) throws IOException { |
|
387 |
// Refresh handshake hash |
|
388 |
chc.handshakeHash.update(); |
|
389 |
||
390 |
FinishedMessage fm = new FinishedMessage(chc); |
|
391 |
||
392 |
// Change write cipher and delivery ChangeCipherSpec message. |
|
393 |
ChangeCipherSpec.t10Producer.produce(chc, message); |
|
394 |
||
395 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
396 |
SSLLogger.fine( |
|
397 |
"Produced client Finished handshake message", fm); |
|
398 |
} |
|
399 |
||
400 |
// Output the handshake message. |
|
401 |
fm.write(chc.handshakeOutput); |
|
402 |
chc.handshakeOutput.flush(); |
|
403 |
||
404 |
/* |
|
405 |
* save server verify data for secure renegotiation |
|
406 |
*/ |
|
407 |
if (chc.conContext.secureRenegotiation) { |
|
408 |
chc.conContext.clientVerifyData = fm.verifyData; |
|
409 |
} |
|
410 |
||
411 |
// update the consumers and producers |
|
412 |
if (!chc.isResumption) { |
|
413 |
chc.conContext.consumers.put(ContentType.CHANGE_CIPHER_SPEC.id, |
|
414 |
ChangeCipherSpec.t10Consumer); |
|
415 |
chc.handshakeConsumers.put( |
|
416 |
SSLHandshake.FINISHED.id, SSLHandshake.FINISHED); |
|
417 |
chc.conContext.inputRecord.expectingFinishFlight(); |
|
418 |
} else { |
|
419 |
if (chc.handshakeSession.isRejoinable()) { |
|
420 |
((SSLSessionContextImpl)chc.sslContext. |
|
421 |
engineGetClientSessionContext()).put( |
|
422 |
chc.handshakeSession); |
|
423 |
} |
|
56584 | 424 |
chc.conContext.conSession = chc.handshakeSession.finish(); |
56542 | 425 |
chc.conContext.protocolVersion = chc.negotiatedProtocol; |
426 |
||
427 |
// handshake context cleanup. |
|
428 |
chc.handshakeFinished = true; |
|
429 |
||
430 |
// May need to retransmit the last flight for DTLS. |
|
431 |
if (!chc.sslContext.isDTLS()) { |
|
432 |
chc.conContext.finishHandshake(); |
|
433 |
} |
|
434 |
} |
|
435 |
||
436 |
// The handshake message has been delivered. |
|
437 |
return null; |
|
438 |
} |
|
439 |
||
440 |
private byte[] onProduceFinished(ServerHandshakeContext shc, |
|
441 |
HandshakeMessage message) throws IOException { |
|
442 |
// Refresh handshake hash |
|
443 |
shc.handshakeHash.update(); |
|
444 |
||
445 |
FinishedMessage fm = new FinishedMessage(shc); |
|
446 |
||
447 |
// Change write cipher and delivery ChangeCipherSpec message. |
|
448 |
ChangeCipherSpec.t10Producer.produce(shc, message); |
|
449 |
||
450 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
451 |
SSLLogger.fine( |
|
452 |
"Produced server Finished handshake message", fm); |
|
453 |
} |
|
454 |
||
455 |
// Output the handshake message. |
|
456 |
fm.write(shc.handshakeOutput); |
|
457 |
shc.handshakeOutput.flush(); |
|
458 |
||
459 |
/* |
|
460 |
* save client verify data for secure renegotiation |
|
461 |
*/ |
|
462 |
if (shc.conContext.secureRenegotiation) { |
|
463 |
shc.conContext.serverVerifyData = fm.verifyData; |
|
464 |
} |
|
465 |
||
466 |
// update the consumers and producers |
|
467 |
if (shc.isResumption) { |
|
468 |
shc.conContext.consumers.put(ContentType.CHANGE_CIPHER_SPEC.id, |
|
469 |
ChangeCipherSpec.t10Consumer); |
|
470 |
shc.handshakeConsumers.put( |
|
471 |
SSLHandshake.FINISHED.id, SSLHandshake.FINISHED); |
|
472 |
shc.conContext.inputRecord.expectingFinishFlight(); |
|
473 |
} else { |
|
474 |
if (shc.handshakeSession.isRejoinable()) { |
|
475 |
((SSLSessionContextImpl)shc.sslContext. |
|
476 |
engineGetServerSessionContext()).put( |
|
477 |
shc.handshakeSession); |
|
478 |
} |
|
56584 | 479 |
shc.conContext.conSession = shc.handshakeSession.finish(); |
56542 | 480 |
shc.conContext.protocolVersion = shc.negotiatedProtocol; |
481 |
||
482 |
// handshake context cleanup. |
|
483 |
shc.handshakeFinished = true; |
|
484 |
||
485 |
// May need to retransmit the last flight for DTLS. |
|
486 |
if (!shc.sslContext.isDTLS()) { |
|
487 |
shc.conContext.finishHandshake(); |
|
488 |
} |
|
489 |
} |
|
490 |
||
491 |
// The handshake message has been delivered. |
|
492 |
return null; |
|
493 |
} |
|
494 |
} |
|
495 |
||
496 |
/** |
|
497 |
* The "Finished" handshake message consumer. |
|
498 |
*/ |
|
499 |
private static final class T12FinishedConsumer implements SSLConsumer { |
|
500 |
// Prevent instantiation of this class. |
|
501 |
private T12FinishedConsumer() { |
|
502 |
// blank |
|
503 |
} |
|
504 |
||
505 |
@Override |
|
506 |
public void consume(ConnectionContext context, |
|
507 |
ByteBuffer message) throws IOException { |
|
508 |
// The consuming happens in handshake context only. |
|
509 |
HandshakeContext hc = (HandshakeContext)context; |
|
510 |
||
511 |
// This comsumer can be used only once. |
|
512 |
hc.handshakeConsumers.remove(SSLHandshake.FINISHED.id); |
|
513 |
||
514 |
// We should not be processing finished messages unless |
|
515 |
// we have received ChangeCipherSpec |
|
516 |
if (hc.conContext.consumers.containsKey( |
|
517 |
ContentType.CHANGE_CIPHER_SPEC.id)) { |
|
518 |
hc.conContext.fatal(Alert.UNEXPECTED_MESSAGE, |
|
519 |
"Missing ChangeCipherSpec message"); |
|
520 |
} |
|
521 |
||
522 |
if (hc.sslConfig.isClientMode) { |
|
523 |
onConsumeFinished((ClientHandshakeContext)context, message); |
|
524 |
} else { |
|
525 |
onConsumeFinished((ServerHandshakeContext)context, message); |
|
526 |
} |
|
527 |
} |
|
528 |
||
529 |
private void onConsumeFinished(ClientHandshakeContext chc, |
|
530 |
ByteBuffer message) throws IOException { |
|
531 |
FinishedMessage fm = new FinishedMessage(chc, message); |
|
532 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
533 |
SSLLogger.fine( |
|
534 |
"Consuming server Finished handshake message", fm); |
|
535 |
} |
|
536 |
||
537 |
if (chc.conContext.secureRenegotiation) { |
|
538 |
chc.conContext.serverVerifyData = fm.verifyData; |
|
539 |
} |
|
540 |
||
541 |
if (!chc.isResumption) { |
|
542 |
if (chc.handshakeSession.isRejoinable()) { |
|
543 |
((SSLSessionContextImpl)chc.sslContext. |
|
544 |
engineGetClientSessionContext()).put( |
|
545 |
chc.handshakeSession); |
|
546 |
} |
|
56584 | 547 |
chc.conContext.conSession = chc.handshakeSession.finish(); |
56542 | 548 |
chc.conContext.protocolVersion = chc.negotiatedProtocol; |
549 |
||
550 |
// handshake context cleanup. |
|
551 |
chc.handshakeFinished = true; |
|
552 |
||
553 |
// May need to retransmit the last flight for DTLS. |
|
554 |
if (!chc.sslContext.isDTLS()) { |
|
555 |
chc.conContext.finishHandshake(); |
|
556 |
} |
|
557 |
} else { |
|
558 |
chc.handshakeProducers.put(SSLHandshake.FINISHED.id, |
|
559 |
SSLHandshake.FINISHED); |
|
560 |
} |
|
561 |
||
562 |
// |
|
563 |
// produce |
|
564 |
// |
|
565 |
SSLHandshake[] probableHandshakeMessages = new SSLHandshake[] { |
|
566 |
SSLHandshake.FINISHED |
|
567 |
}; |
|
568 |
||
569 |
for (SSLHandshake hs : probableHandshakeMessages) { |
|
570 |
HandshakeProducer handshakeProducer = |
|
571 |
chc.handshakeProducers.remove(hs.id); |
|
572 |
if (handshakeProducer != null) { |
|
573 |
handshakeProducer.produce(chc, fm); |
|
574 |
} |
|
575 |
} |
|
576 |
} |
|
577 |
||
578 |
private void onConsumeFinished(ServerHandshakeContext shc, |
|
579 |
ByteBuffer message) throws IOException { |
|
580 |
FinishedMessage fm = new FinishedMessage(shc, message); |
|
581 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
582 |
SSLLogger.fine( |
|
583 |
"Consuming client Finished handshake message", fm); |
|
584 |
} |
|
585 |
||
586 |
if (shc.conContext.secureRenegotiation) { |
|
587 |
shc.conContext.clientVerifyData = fm.verifyData; |
|
588 |
} |
|
589 |
||
590 |
if (shc.isResumption) { |
|
591 |
if (shc.handshakeSession.isRejoinable()) { |
|
592 |
((SSLSessionContextImpl)shc.sslContext. |
|
593 |
engineGetServerSessionContext()).put( |
|
594 |
shc.handshakeSession); |
|
595 |
} |
|
56584 | 596 |
shc.conContext.conSession = shc.handshakeSession.finish(); |
56542 | 597 |
shc.conContext.protocolVersion = shc.negotiatedProtocol; |
598 |
||
599 |
// handshake context cleanup. |
|
600 |
shc.handshakeFinished = true; |
|
601 |
||
602 |
// May need to retransmit the last flight for DTLS. |
|
603 |
if (!shc.sslContext.isDTLS()) { |
|
604 |
shc.conContext.finishHandshake(); |
|
605 |
} |
|
606 |
} else { |
|
607 |
shc.handshakeProducers.put(SSLHandshake.FINISHED.id, |
|
608 |
SSLHandshake.FINISHED); |
|
609 |
} |
|
610 |
||
611 |
// |
|
612 |
// produce |
|
613 |
// |
|
614 |
SSLHandshake[] probableHandshakeMessages = new SSLHandshake[] { |
|
615 |
SSLHandshake.FINISHED |
|
616 |
}; |
|
617 |
||
618 |
for (SSLHandshake hs : probableHandshakeMessages) { |
|
619 |
HandshakeProducer handshakeProducer = |
|
620 |
shc.handshakeProducers.remove(hs.id); |
|
621 |
if (handshakeProducer != null) { |
|
622 |
handshakeProducer.produce(shc, fm); |
|
623 |
} |
|
624 |
} |
|
625 |
} |
|
626 |
} |
|
627 |
||
628 |
/** |
|
629 |
* The "Finished" handshake message producer. |
|
630 |
*/ |
|
631 |
private static final |
|
632 |
class T13FinishedProducer implements HandshakeProducer { |
|
633 |
// Prevent instantiation of this class. |
|
634 |
private T13FinishedProducer() { |
|
635 |
// blank |
|
636 |
} |
|
637 |
||
638 |
@Override |
|
639 |
public byte[] produce(ConnectionContext context, |
|
640 |
HandshakeMessage message) throws IOException { |
|
641 |
// The consuming happens in handshake context only. |
|
642 |
HandshakeContext hc = (HandshakeContext)context; |
|
643 |
if (hc.sslConfig.isClientMode) { |
|
644 |
return onProduceFinished( |
|
645 |
(ClientHandshakeContext)context, message); |
|
646 |
} else { |
|
647 |
return onProduceFinished( |
|
648 |
(ServerHandshakeContext)context, message); |
|
649 |
} |
|
650 |
} |
|
651 |
||
652 |
private byte[] onProduceFinished(ClientHandshakeContext chc, |
|
653 |
HandshakeMessage message) throws IOException { |
|
654 |
// Refresh handshake hash |
|
655 |
chc.handshakeHash.update(); |
|
656 |
||
657 |
FinishedMessage fm = new FinishedMessage(chc); |
|
658 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
659 |
SSLLogger.fine( |
|
660 |
"Produced client Finished handshake message", fm); |
|
661 |
} |
|
662 |
||
663 |
// Output the handshake message. |
|
664 |
fm.write(chc.handshakeOutput); |
|
665 |
chc.handshakeOutput.flush(); |
|
666 |
||
667 |
// save server verify data for secure renegotiation |
|
668 |
if (chc.conContext.secureRenegotiation) { |
|
669 |
chc.conContext.clientVerifyData = fm.verifyData; |
|
670 |
} |
|
671 |
||
672 |
// update the context |
|
673 |
// Change client/server application traffic secrets. |
|
674 |
SSLKeyDerivation kd = chc.handshakeKeyDerivation; |
|
675 |
if (kd == null) { |
|
676 |
// unlikely |
|
677 |
chc.conContext.fatal(Alert.INTERNAL_ERROR, |
|
678 |
"no key derivation"); |
|
679 |
return null; // make the compiler happy |
|
680 |
} |
|
681 |
||
682 |
SSLTrafficKeyDerivation kdg = |
|
683 |
SSLTrafficKeyDerivation.valueOf(chc.negotiatedProtocol); |
|
684 |
if (kdg == null) { |
|
685 |
// unlikely |
|
686 |
chc.conContext.fatal(Alert.INTERNAL_ERROR, |
|
687 |
"Not supported key derivation: " + |
|
688 |
chc.negotiatedProtocol); |
|
689 |
return null; // make the compiler happy |
|
690 |
} |
|
691 |
||
692 |
try { |
|
693 |
// update the application traffic read keys. |
|
694 |
SecretKey writeSecret = kd.deriveKey( |
|
695 |
"TlsClientAppTrafficSecret", null); |
|
696 |
||
697 |
SSLKeyDerivation writeKD = |
|
698 |
kdg.createKeyDerivation(chc, writeSecret); |
|
699 |
SecretKey writeKey = writeKD.deriveKey( |
|
700 |
"TlsKey", null); |
|
701 |
SecretKey writeIvSecret = writeKD.deriveKey( |
|
702 |
"TlsIv", null); |
|
703 |
IvParameterSpec writeIv = |
|
704 |
new IvParameterSpec(writeIvSecret.getEncoded()); |
|
705 |
SSLWriteCipher writeCipher = |
|
706 |
chc.negotiatedCipherSuite.bulkCipher.createWriteCipher( |
|
707 |
Authenticator.valueOf(chc.negotiatedProtocol), |
|
708 |
chc.negotiatedProtocol, writeKey, writeIv, |
|
709 |
chc.sslContext.getSecureRandom()); |
|
710 |
||
711 |
chc.baseWriteSecret = writeSecret; |
|
712 |
chc.conContext.outputRecord.changeWriteCiphers( |
|
713 |
writeCipher, false); |
|
714 |
||
715 |
} catch (GeneralSecurityException gse) { |
|
716 |
chc.conContext.fatal(Alert.INTERNAL_ERROR, |
|
717 |
"Failure to derive application secrets", gse); |
|
718 |
return null; // make the compiler happy |
|
719 |
} |
|
720 |
||
721 |
// The resumption master secret is stored in the session so |
|
722 |
// it can be used after the handshake is completed. |
|
723 |
SSLSecretDerivation sd = ((SSLSecretDerivation) kd).forContext(chc); |
|
724 |
SecretKey resumptionMasterSecret = sd.deriveKey( |
|
725 |
"TlsResumptionMasterSecret", null); |
|
726 |
chc.handshakeSession.setResumptionMasterSecret(resumptionMasterSecret); |
|
727 |
||
56584 | 728 |
chc.conContext.conSession = chc.handshakeSession.finish(); |
56542 | 729 |
chc.conContext.protocolVersion = chc.negotiatedProtocol; |
730 |
||
731 |
// handshake context cleanup. |
|
732 |
chc.handshakeFinished = true; |
|
733 |
chc.conContext.finishHandshake(); |
|
734 |
||
735 |
// The handshake message has been delivered. |
|
736 |
return null; |
|
737 |
} |
|
738 |
||
739 |
private byte[] onProduceFinished(ServerHandshakeContext shc, |
|
740 |
HandshakeMessage message) throws IOException { |
|
741 |
// Refresh handshake hash |
|
742 |
shc.handshakeHash.update(); |
|
743 |
||
744 |
FinishedMessage fm = new FinishedMessage(shc); |
|
745 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
746 |
SSLLogger.fine( |
|
747 |
"Produced server Finished handshake message", fm); |
|
748 |
} |
|
749 |
||
750 |
// Output the handshake message. |
|
751 |
fm.write(shc.handshakeOutput); |
|
752 |
shc.handshakeOutput.flush(); |
|
753 |
||
754 |
// Change client/server application traffic secrets. |
|
755 |
SSLKeyDerivation kd = shc.handshakeKeyDerivation; |
|
756 |
if (kd == null) { |
|
757 |
// unlikely |
|
758 |
shc.conContext.fatal(Alert.INTERNAL_ERROR, |
|
759 |
"no key derivation"); |
|
760 |
return null; // make the compiler happy |
|
761 |
} |
|
762 |
||
763 |
SSLTrafficKeyDerivation kdg = |
|
764 |
SSLTrafficKeyDerivation.valueOf(shc.negotiatedProtocol); |
|
765 |
if (kdg == null) { |
|
766 |
// unlikely |
|
767 |
shc.conContext.fatal(Alert.INTERNAL_ERROR, |
|
768 |
"Not supported key derivation: " + |
|
769 |
shc.negotiatedProtocol); |
|
770 |
return null; // make the compiler happy |
|
771 |
} |
|
772 |
||
773 |
// derive salt secret |
|
774 |
try { |
|
775 |
SecretKey saltSecret = kd.deriveKey("TlsSaltSecret", null); |
|
776 |
||
777 |
// derive application secrets |
|
778 |
HashAlg hashAlg = shc.negotiatedCipherSuite.hashAlg; |
|
779 |
HKDF hkdf = new HKDF(hashAlg.name); |
|
780 |
byte[] zeros = new byte[hashAlg.hashLength]; |
|
781 |
SecretKeySpec sharedSecret = |
|
782 |
new SecretKeySpec(zeros, "TlsZeroSecret"); |
|
783 |
SecretKey masterSecret = |
|
784 |
hkdf.extract(saltSecret, sharedSecret, "TlsMasterSecret"); |
|
785 |
||
786 |
SSLKeyDerivation secretKD = |
|
787 |
new SSLSecretDerivation(shc, masterSecret); |
|
788 |
||
789 |
// update the handshake traffic write keys. |
|
790 |
SecretKey writeSecret = secretKD.deriveKey( |
|
791 |
"TlsServerAppTrafficSecret", null); |
|
792 |
SSLKeyDerivation writeKD = |
|
793 |
kdg.createKeyDerivation(shc, writeSecret); |
|
794 |
SecretKey writeKey = writeKD.deriveKey( |
|
795 |
"TlsKey", null); |
|
796 |
SecretKey writeIvSecret = writeKD.deriveKey( |
|
797 |
"TlsIv", null); |
|
798 |
IvParameterSpec writeIv = |
|
799 |
new IvParameterSpec(writeIvSecret.getEncoded()); |
|
800 |
SSLWriteCipher writeCipher = |
|
801 |
shc.negotiatedCipherSuite.bulkCipher.createWriteCipher( |
|
802 |
Authenticator.valueOf(shc.negotiatedProtocol), |
|
803 |
shc.negotiatedProtocol, writeKey, writeIv, |
|
804 |
shc.sslContext.getSecureRandom()); |
|
805 |
||
806 |
shc.baseWriteSecret = writeSecret; |
|
807 |
shc.conContext.outputRecord.changeWriteCiphers( |
|
808 |
writeCipher, false); |
|
809 |
||
810 |
// update the context for the following key derivation |
|
811 |
shc.handshakeKeyDerivation = secretKD; |
|
812 |
} catch (GeneralSecurityException gse) { |
|
813 |
shc.conContext.fatal(Alert.INTERNAL_ERROR, |
|
814 |
"Failure to derive application secrets", gse); |
|
815 |
return null; // make the compiler happy |
|
816 |
} |
|
817 |
||
818 |
/* |
|
819 |
* save client verify data for secure renegotiation |
|
820 |
*/ |
|
821 |
if (shc.conContext.secureRenegotiation) { |
|
822 |
shc.conContext.serverVerifyData = fm.verifyData; |
|
823 |
} |
|
824 |
||
825 |
// update the context |
|
826 |
shc.handshakeConsumers.put( |
|
827 |
SSLHandshake.FINISHED.id, SSLHandshake.FINISHED); |
|
828 |
||
829 |
// The handshake message has been delivered. |
|
830 |
return null; |
|
831 |
} |
|
832 |
} |
|
833 |
||
834 |
/** |
|
835 |
* The "Finished" handshake message consumer. |
|
836 |
*/ |
|
837 |
private static final class T13FinishedConsumer implements SSLConsumer { |
|
838 |
// Prevent instantiation of this class. |
|
839 |
private T13FinishedConsumer() { |
|
840 |
// blank |
|
841 |
} |
|
842 |
||
843 |
@Override |
|
844 |
public void consume(ConnectionContext context, |
|
845 |
ByteBuffer message) throws IOException { |
|
846 |
// The consuming happens in handshake context only. |
|
847 |
HandshakeContext hc = (HandshakeContext)context; |
|
848 |
if (hc.sslConfig.isClientMode) { |
|
849 |
onConsumeFinished( |
|
850 |
(ClientHandshakeContext)context, message); |
|
851 |
} else { |
|
852 |
onConsumeFinished( |
|
853 |
(ServerHandshakeContext)context, message); |
|
854 |
} |
|
855 |
} |
|
856 |
||
857 |
private void onConsumeFinished(ClientHandshakeContext chc, |
|
858 |
ByteBuffer message) throws IOException { |
|
859 |
FinishedMessage fm = new FinishedMessage(chc, message); |
|
860 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
861 |
SSLLogger.fine( |
|
862 |
"Consuming server Finished handshake message", fm); |
|
863 |
} |
|
864 |
||
865 |
// Save client verify data for secure renegotiation. |
|
866 |
if (chc.conContext.secureRenegotiation) { |
|
867 |
chc.conContext.serverVerifyData = fm.verifyData; |
|
868 |
} |
|
869 |
||
870 |
// |
|
871 |
// validate |
|
872 |
// |
|
873 |
// blank |
|
874 |
||
875 |
// |
|
876 |
// update |
|
877 |
// |
|
878 |
// A change_cipher_spec record received after the peer's Finished |
|
879 |
// message MUST be treated as an unexpected record type. |
|
880 |
chc.conContext.consumers.remove(ContentType.CHANGE_CIPHER_SPEC.id); |
|
881 |
||
882 |
// Change client/server application traffic secrets. |
|
883 |
// Refresh handshake hash |
|
884 |
chc.handshakeHash.update(); |
|
885 |
SSLKeyDerivation kd = chc.handshakeKeyDerivation; |
|
886 |
if (kd == null) { |
|
887 |
// unlikely |
|
888 |
chc.conContext.fatal(Alert.INTERNAL_ERROR, |
|
889 |
"no key derivation"); |
|
890 |
return; // make the compiler happy |
|
891 |
} |
|
892 |
||
893 |
SSLTrafficKeyDerivation kdg = |
|
894 |
SSLTrafficKeyDerivation.valueOf(chc.negotiatedProtocol); |
|
895 |
if (kdg == null) { |
|
896 |
// unlikely |
|
897 |
chc.conContext.fatal(Alert.INTERNAL_ERROR, |
|
898 |
"Not supported key derivation: " + |
|
899 |
chc.negotiatedProtocol); |
|
900 |
return; // make the compiler happy |
|
901 |
} |
|
902 |
||
56558
4a3deb6759b1
minor session management improvements to try to address some bugs
apetcher
parents:
56542
diff
changeset
|
903 |
// save the session |
4a3deb6759b1
minor session management improvements to try to address some bugs
apetcher
parents:
56542
diff
changeset
|
904 |
if (!chc.isResumption && chc.handshakeSession.isRejoinable()) { |
4a3deb6759b1
minor session management improvements to try to address some bugs
apetcher
parents:
56542
diff
changeset
|
905 |
SSLSessionContextImpl sessionContext = (SSLSessionContextImpl) |
4a3deb6759b1
minor session management improvements to try to address some bugs
apetcher
parents:
56542
diff
changeset
|
906 |
chc.sslContext.engineGetClientSessionContext(); |
4a3deb6759b1
minor session management improvements to try to address some bugs
apetcher
parents:
56542
diff
changeset
|
907 |
sessionContext.put(chc.handshakeSession); |
4a3deb6759b1
minor session management improvements to try to address some bugs
apetcher
parents:
56542
diff
changeset
|
908 |
} |
4a3deb6759b1
minor session management improvements to try to address some bugs
apetcher
parents:
56542
diff
changeset
|
909 |
|
56542 | 910 |
// derive salt secret |
911 |
try { |
|
912 |
SecretKey saltSecret = kd.deriveKey("TlsSaltSecret", null); |
|
913 |
||
914 |
// derive application secrets |
|
915 |
HashAlg hashAlg = chc.negotiatedCipherSuite.hashAlg; |
|
916 |
HKDF hkdf = new HKDF(hashAlg.name); |
|
917 |
byte[] zeros = new byte[hashAlg.hashLength]; |
|
918 |
SecretKeySpec sharedSecret = |
|
919 |
new SecretKeySpec(zeros, "TlsZeroSecret"); |
|
920 |
SecretKey masterSecret = |
|
921 |
hkdf.extract(saltSecret, sharedSecret, "TlsMasterSecret"); |
|
922 |
||
923 |
SSLKeyDerivation secretKD = |
|
924 |
new SSLSecretDerivation(chc, masterSecret); |
|
925 |
||
926 |
// update the handshake traffic read keys. |
|
927 |
SecretKey readSecret = secretKD.deriveKey( |
|
928 |
"TlsServerAppTrafficSecret", null); |
|
929 |
SSLKeyDerivation writeKD = |
|
930 |
kdg.createKeyDerivation(chc, readSecret); |
|
931 |
SecretKey readKey = writeKD.deriveKey( |
|
932 |
"TlsKey", null); |
|
933 |
SecretKey readIvSecret = writeKD.deriveKey( |
|
934 |
"TlsIv", null); |
|
935 |
IvParameterSpec readIv = |
|
936 |
new IvParameterSpec(readIvSecret.getEncoded()); |
|
937 |
SSLReadCipher readCipher = |
|
938 |
chc.negotiatedCipherSuite.bulkCipher.createReadCipher( |
|
939 |
Authenticator.valueOf(chc.negotiatedProtocol), |
|
940 |
chc.negotiatedProtocol, readKey, readIv, |
|
941 |
chc.sslContext.getSecureRandom()); |
|
942 |
||
943 |
chc.baseReadSecret = readSecret; |
|
944 |
chc.conContext.inputRecord.changeReadCiphers(readCipher); |
|
945 |
||
946 |
// update the context for the following key derivation |
|
947 |
chc.handshakeKeyDerivation = secretKD; |
|
948 |
} catch (GeneralSecurityException gse) { |
|
949 |
chc.conContext.fatal(Alert.INTERNAL_ERROR, |
|
950 |
"Failure to derive application secrets", gse); |
|
951 |
return; // make the compiler happy |
|
952 |
} |
|
953 |
||
954 |
// |
|
955 |
// produce |
|
956 |
// |
|
957 |
chc.handshakeProducers.put(SSLHandshake.FINISHED.id, |
|
958 |
SSLHandshake.FINISHED); |
|
959 |
SSLHandshake[] probableHandshakeMessages = new SSLHandshake[] { |
|
960 |
// full handshake messages |
|
961 |
SSLHandshake.CERTIFICATE, |
|
962 |
SSLHandshake.CERTIFICATE_VERIFY, |
|
963 |
SSLHandshake.FINISHED |
|
964 |
}; |
|
965 |
||
966 |
for (SSLHandshake hs : probableHandshakeMessages) { |
|
967 |
HandshakeProducer handshakeProducer = |
|
968 |
chc.handshakeProducers.remove(hs.id); |
|
969 |
if (handshakeProducer != null) { |
|
970 |
handshakeProducer.produce(chc, null); |
|
971 |
} |
|
972 |
} |
|
973 |
} |
|
974 |
||
975 |
private void onConsumeFinished(ServerHandshakeContext shc, |
|
976 |
ByteBuffer message) throws IOException { |
|
977 |
FinishedMessage fm = new FinishedMessage(shc, message); |
|
978 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
979 |
SSLLogger.fine( |
|
980 |
"Consuming client Finished handshake message", fm); |
|
981 |
} |
|
982 |
||
983 |
if (shc.conContext.secureRenegotiation) { |
|
984 |
shc.conContext.clientVerifyData = fm.verifyData; |
|
985 |
} |
|
986 |
||
987 |
// |
|
988 |
// validate |
|
989 |
// |
|
990 |
// blank |
|
991 |
||
992 |
// |
|
993 |
// update |
|
994 |
// |
|
995 |
// Change client/server application traffic secrets. |
|
996 |
SSLKeyDerivation kd = shc.handshakeKeyDerivation; |
|
997 |
if (kd == null) { |
|
998 |
// unlikely |
|
999 |
shc.conContext.fatal(Alert.INTERNAL_ERROR, |
|
1000 |
"no key derivation"); |
|
1001 |
return; // make the compiler happy |
|
1002 |
} |
|
1003 |
||
1004 |
SSLTrafficKeyDerivation kdg = |
|
1005 |
SSLTrafficKeyDerivation.valueOf(shc.negotiatedProtocol); |
|
1006 |
if (kdg == null) { |
|
1007 |
// unlikely |
|
1008 |
shc.conContext.fatal(Alert.INTERNAL_ERROR, |
|
1009 |
"Not supported key derivation: " + |
|
1010 |
shc.negotiatedProtocol); |
|
1011 |
return; // make the compiler happy |
|
1012 |
} |
|
1013 |
||
56558
4a3deb6759b1
minor session management improvements to try to address some bugs
apetcher
parents:
56542
diff
changeset
|
1014 |
// save the session |
4a3deb6759b1
minor session management improvements to try to address some bugs
apetcher
parents:
56542
diff
changeset
|
1015 |
if (!shc.isResumption && shc.handshakeSession.isRejoinable()) { |
4a3deb6759b1
minor session management improvements to try to address some bugs
apetcher
parents:
56542
diff
changeset
|
1016 |
SSLSessionContextImpl sessionContext = (SSLSessionContextImpl) |
4a3deb6759b1
minor session management improvements to try to address some bugs
apetcher
parents:
56542
diff
changeset
|
1017 |
shc.sslContext.engineGetServerSessionContext(); |
4a3deb6759b1
minor session management improvements to try to address some bugs
apetcher
parents:
56542
diff
changeset
|
1018 |
sessionContext.put(shc.handshakeSession); |
4a3deb6759b1
minor session management improvements to try to address some bugs
apetcher
parents:
56542
diff
changeset
|
1019 |
} |
4a3deb6759b1
minor session management improvements to try to address some bugs
apetcher
parents:
56542
diff
changeset
|
1020 |
|
56542 | 1021 |
try { |
1022 |
// update the application traffic read keys. |
|
1023 |
SecretKey readSecret = kd.deriveKey( |
|
1024 |
"TlsClientAppTrafficSecret", null); |
|
1025 |
||
1026 |
SSLKeyDerivation readKD = |
|
1027 |
kdg.createKeyDerivation(shc, readSecret); |
|
1028 |
SecretKey readKey = readKD.deriveKey( |
|
1029 |
"TlsKey", null); |
|
1030 |
SecretKey readIvSecret = readKD.deriveKey( |
|
1031 |
"TlsIv", null); |
|
1032 |
IvParameterSpec readIv = |
|
1033 |
new IvParameterSpec(readIvSecret.getEncoded()); |
|
1034 |
SSLReadCipher readCipher = |
|
1035 |
shc.negotiatedCipherSuite.bulkCipher.createReadCipher( |
|
1036 |
Authenticator.valueOf(shc.negotiatedProtocol), |
|
1037 |
shc.negotiatedProtocol, readKey, readIv, |
|
1038 |
shc.sslContext.getSecureRandom()); |
|
1039 |
||
1040 |
shc.baseReadSecret = readSecret; |
|
1041 |
shc.conContext.inputRecord.changeReadCiphers(readCipher); |
|
1042 |
||
1043 |
// The resumption master secret is stored in the session so |
|
1044 |
// it can be used after the handshake is completed. |
|
1045 |
shc.handshakeHash.update(); |
|
1046 |
SSLSecretDerivation sd = ((SSLSecretDerivation)kd).forContext(shc); |
|
1047 |
SecretKey resumptionMasterSecret = sd.deriveKey( |
|
1048 |
"TlsResumptionMasterSecret", null); |
|
1049 |
shc.handshakeSession.setResumptionMasterSecret(resumptionMasterSecret); |
|
1050 |
} catch (GeneralSecurityException gse) { |
|
1051 |
shc.conContext.fatal(Alert.INTERNAL_ERROR, |
|
1052 |
"Failure to derive application secrets", gse); |
|
1053 |
return; // make the compiler happy |
|
1054 |
} |
|
1055 |
||
1056 |
// update connection context |
|
56584 | 1057 |
shc.conContext.conSession = shc.handshakeSession.finish(); |
56542 | 1058 |
shc.conContext.protocolVersion = shc.negotiatedProtocol; |
1059 |
||
1060 |
// handshake context cleanup. |
|
1061 |
shc.handshakeFinished = true; |
|
1062 |
||
1063 |
// May need to retransmit the last flight for DTLS. |
|
1064 |
if (!shc.sslContext.isDTLS()) { |
|
1065 |
shc.conContext.finishHandshake(); |
|
1066 |
} |
|
1067 |
||
1068 |
// |
|
1069 |
// produce |
|
1070 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
1071 |
SSLLogger.fine( |
|
1072 |
"Sending new session ticket"); |
|
1073 |
} |
|
1074 |
NewSessionTicket.kickstartProducer.produce(shc); |
|
1075 |
||
1076 |
} |
|
1077 |
} |
|
1078 |
} |