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