author | xuelei |
Tue, 05 Jun 2018 11:49:25 -0700 | |
branch | JDK-8145252-TLS13-branch |
changeset 56672 | ec5537b61038 |
parent 56660 | 66c803c3ce32 |
child 56702 | 75527e40bdfd |
permissions | -rw-r--r-- |
56542 | 1 |
/* |
2 |
* Copyright (c) 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 |
package sun.security.ssl; |
|
26 |
||
27 |
import java.io.IOException; |
|
28 |
import java.math.BigInteger; |
|
29 |
import java.nio.ByteBuffer; |
|
30 |
import java.security.GeneralSecurityException; |
|
31 |
import java.security.ProviderException; |
|
32 |
import java.security.SecureRandom; |
|
33 |
import java.text.MessageFormat; |
|
34 |
import java.util.Locale; |
|
35 |
import java.util.Optional; |
|
36 |
import javax.crypto.SecretKey; |
|
37 |
import javax.net.ssl.SSLHandshakeException; |
|
38 |
||
39 |
import sun.security.ssl.SSLHandshake.HandshakeMessage; |
|
40 |
||
41 |
/** |
|
42 |
* Pack of the NewSessionTicket handshake message. |
|
43 |
*/ |
|
44 |
final class NewSessionTicket { |
|
45 |
||
46 |
private static final int SEVEN_DAYS_IN_SECONDS = 604800; |
|
47 |
||
48 |
static final SSLConsumer handshakeConsumer = |
|
49 |
new NewSessionTicketConsumer(); |
|
50 |
static final SSLProducer kickstartProducer = |
|
51 |
new NewSessionTicketKickstartProducer(); |
|
52 |
static final HandshakeProducer handshakeProducer = |
|
53 |
new NewSessionTicketProducer(); |
|
54 |
||
55 |
/** |
|
56 |
* The NewSessionTicketMessage handshake message. |
|
57 |
*/ |
|
58 |
static final class NewSessionTicketMessage extends HandshakeMessage { |
|
59 |
final int ticketLifetime; |
|
60 |
final int ticketAgeAdd; |
|
61 |
final byte[] ticketNonce; |
|
62 |
final byte[] ticket; |
|
63 |
final SSLExtensions extensions; |
|
64 |
||
65 |
NewSessionTicketMessage(HandshakeContext context, |
|
66 |
int ticketLifetime, SecureRandom generator, |
|
67 |
byte[] ticketNonce, byte[] ticket) { |
|
68 |
super(context); |
|
69 |
||
70 |
this.ticketLifetime = ticketLifetime; |
|
71 |
this.ticketAgeAdd = generator.nextInt(); |
|
72 |
this.ticketNonce = ticketNonce; |
|
73 |
this.ticket = ticket; |
|
74 |
this.extensions = new SSLExtensions(this); |
|
75 |
} |
|
76 |
||
77 |
NewSessionTicketMessage(HandshakeContext context, |
|
78 |
ByteBuffer m) throws IOException { |
|
79 |
super(context); |
|
80 |
||
81 |
this.ticketLifetime = Record.getInt32(m); |
|
82 |
this.ticketAgeAdd = Record.getInt32(m); |
|
83 |
this.ticketNonce = Record.getBytes8(m); |
|
84 |
this.ticket = Record.getBytes16(m); |
|
85 |
if (this.ticket.length == 0) { |
|
86 |
context.conContext.fatal(Alert.ILLEGAL_PARAMETER, |
|
56672
ec5537b61038
cleanup on HelloCookieManager, RSAClientKeyExchange and NewSessionTicket
xuelei
parents:
56660
diff
changeset
|
87 |
"No ticket in the NewSessionTicket handshake message"); |
56542 | 88 |
} |
89 |
||
90 |
SSLExtension[] supportedExtensions = |
|
56672
ec5537b61038
cleanup on HelloCookieManager, RSAClientKeyExchange and NewSessionTicket
xuelei
parents:
56660
diff
changeset
|
91 |
context.sslConfig.getEnabledExtensions( |
ec5537b61038
cleanup on HelloCookieManager, RSAClientKeyExchange and NewSessionTicket
xuelei
parents:
56660
diff
changeset
|
92 |
SSLHandshake.NEW_SESSION_TICKET); |
56542 | 93 |
if (m.hasRemaining()) { |
94 |
this.extensions = |
|
95 |
new SSLExtensions(this, m, supportedExtensions); |
|
96 |
} else { |
|
97 |
this.extensions = new SSLExtensions(this); |
|
98 |
} |
|
99 |
} |
|
100 |
||
101 |
@Override |
|
102 |
public SSLHandshake handshakeType() { |
|
103 |
return SSLHandshake.NEW_SESSION_TICKET; |
|
104 |
} |
|
105 |
||
106 |
@Override |
|
107 |
public int messageLength() { |
|
56568
7fe8ce3c78b6
TLS 1.3, incorrect encode of NewSessionTicket for empty extension
xuelei
parents:
56561
diff
changeset
|
108 |
int extLen = extensions.length(); |
7fe8ce3c78b6
TLS 1.3, incorrect encode of NewSessionTicket for empty extension
xuelei
parents:
56561
diff
changeset
|
109 |
if (extLen == 0) { |
7fe8ce3c78b6
TLS 1.3, incorrect encode of NewSessionTicket for empty extension
xuelei
parents:
56561
diff
changeset
|
110 |
extLen = 2; // empty extensions |
7fe8ce3c78b6
TLS 1.3, incorrect encode of NewSessionTicket for empty extension
xuelei
parents:
56561
diff
changeset
|
111 |
} |
7fe8ce3c78b6
TLS 1.3, incorrect encode of NewSessionTicket for empty extension
xuelei
parents:
56561
diff
changeset
|
112 |
|
7fe8ce3c78b6
TLS 1.3, incorrect encode of NewSessionTicket for empty extension
xuelei
parents:
56561
diff
changeset
|
113 |
return 8 + ticketNonce.length + 1 + |
7fe8ce3c78b6
TLS 1.3, incorrect encode of NewSessionTicket for empty extension
xuelei
parents:
56561
diff
changeset
|
114 |
ticket.length + 2 + extLen; |
56542 | 115 |
} |
116 |
||
117 |
@Override |
|
118 |
public void send(HandshakeOutStream hos) throws IOException { |
|
119 |
hos.putInt32(ticketLifetime); |
|
120 |
hos.putInt32(ticketAgeAdd); |
|
121 |
hos.putBytes8(ticketNonce); |
|
122 |
hos.putBytes16(ticket); |
|
56568
7fe8ce3c78b6
TLS 1.3, incorrect encode of NewSessionTicket for empty extension
xuelei
parents:
56561
diff
changeset
|
123 |
|
7fe8ce3c78b6
TLS 1.3, incorrect encode of NewSessionTicket for empty extension
xuelei
parents:
56561
diff
changeset
|
124 |
// Is it an empty extensions? |
7fe8ce3c78b6
TLS 1.3, incorrect encode of NewSessionTicket for empty extension
xuelei
parents:
56561
diff
changeset
|
125 |
if (extensions.length() == 0) { |
7fe8ce3c78b6
TLS 1.3, incorrect encode of NewSessionTicket for empty extension
xuelei
parents:
56561
diff
changeset
|
126 |
hos.putInt16(0); |
7fe8ce3c78b6
TLS 1.3, incorrect encode of NewSessionTicket for empty extension
xuelei
parents:
56561
diff
changeset
|
127 |
} else { |
7fe8ce3c78b6
TLS 1.3, incorrect encode of NewSessionTicket for empty extension
xuelei
parents:
56561
diff
changeset
|
128 |
extensions.send(hos); |
7fe8ce3c78b6
TLS 1.3, incorrect encode of NewSessionTicket for empty extension
xuelei
parents:
56561
diff
changeset
|
129 |
} |
56542 | 130 |
} |
131 |
||
132 |
@Override |
|
133 |
public String toString() { |
|
134 |
MessageFormat messageFormat = new MessageFormat( |
|
135 |
"\"NewSessionTicket\": '{'\n" + |
|
136 |
" \"ticket_lifetime\" : \"{0}\",\n" + |
|
137 |
" \"ticket_age_add\" : \"{1}\",\n" + |
|
138 |
" \"ticket_nonce\" : \"{2}\",\n" + |
|
139 |
" \"ticket\" : \"{3}\",\n" + |
|
140 |
" \"extensions\" : [\n" + |
|
141 |
"{5}\n" + |
|
142 |
" ]\n" + |
|
143 |
"'}'", |
|
144 |
Locale.ENGLISH); |
|
145 |
||
146 |
Object[] messageFields = { |
|
147 |
ticketLifetime, |
|
148 |
"omitted", //ticketAgeAdd should not be logged |
|
149 |
Utilities.toHexString(ticketNonce), |
|
150 |
Utilities.toHexString(ticket), |
|
151 |
Utilities.indent(extensions.toString(), " ") |
|
152 |
}; |
|
153 |
||
154 |
return messageFormat.format(messageFields); |
|
155 |
} |
|
156 |
} |
|
157 |
||
158 |
private static SecretKey derivePreSharedKey(CipherSuite.HashAlg hashAlg, |
|
56672
ec5537b61038
cleanup on HelloCookieManager, RSAClientKeyExchange and NewSessionTicket
xuelei
parents:
56660
diff
changeset
|
159 |
SecretKey resumptionMasterSecret, byte[] nonce) throws IOException { |
56542 | 160 |
try { |
161 |
HKDF hkdf = new HKDF(hashAlg.name); |
|
162 |
byte[] hkdfInfo = SSLSecretDerivation.createHkdfInfo( |
|
56672
ec5537b61038
cleanup on HelloCookieManager, RSAClientKeyExchange and NewSessionTicket
xuelei
parents:
56660
diff
changeset
|
163 |
"tls13 resumption".getBytes(), nonce, hashAlg.hashLength); |
56542 | 164 |
return hkdf.expand(resumptionMasterSecret, hkdfInfo, |
56672
ec5537b61038
cleanup on HelloCookieManager, RSAClientKeyExchange and NewSessionTicket
xuelei
parents:
56660
diff
changeset
|
165 |
hashAlg.hashLength, "TlsPreSharedKey"); |
56542 | 166 |
} catch (GeneralSecurityException gse) { |
167 |
throw (SSLHandshakeException) new SSLHandshakeException( |
|
56672
ec5537b61038
cleanup on HelloCookieManager, RSAClientKeyExchange and NewSessionTicket
xuelei
parents:
56660
diff
changeset
|
168 |
"Could not derive PSK").initCause(gse); |
56542 | 169 |
} |
170 |
} |
|
171 |
||
172 |
private static final |
|
56672
ec5537b61038
cleanup on HelloCookieManager, RSAClientKeyExchange and NewSessionTicket
xuelei
parents:
56660
diff
changeset
|
173 |
class NewSessionTicketKickstartProducer implements SSLProducer { |
56542 | 174 |
@Override |
175 |
public byte[] produce(ConnectionContext context) throws IOException { |
|
176 |
// The producing happens in server side only. |
|
177 |
ServerHandshakeContext shc = (ServerHandshakeContext)context; |
|
178 |
||
179 |
if (shc.pskKeyExchangeModes.isEmpty()) { |
|
180 |
// client doesn't support PSK |
|
181 |
return null; |
|
182 |
} |
|
56672
ec5537b61038
cleanup on HelloCookieManager, RSAClientKeyExchange and NewSessionTicket
xuelei
parents:
56660
diff
changeset
|
183 |
|
56542 | 184 |
if (!shc.handshakeSession.isRejoinable()) { |
185 |
return null; |
|
186 |
} |
|
187 |
||
188 |
// get a new session ID |
|
189 |
SSLSessionContextImpl sessionCache = (SSLSessionContextImpl) |
|
190 |
shc.sslContext.engineGetServerSessionContext(); |
|
191 |
SessionId newId = new SessionId(true, |
|
192 |
shc.sslContext.getSecureRandom()); |
|
193 |
||
194 |
Optional<SecretKey> resumptionMasterSecret = |
|
195 |
shc.handshakeSession.getResumptionMasterSecret(); |
|
196 |
if (!resumptionMasterSecret.isPresent()) { |
|
197 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
198 |
SSLLogger.fine( |
|
56672
ec5537b61038
cleanup on HelloCookieManager, RSAClientKeyExchange and NewSessionTicket
xuelei
parents:
56660
diff
changeset
|
199 |
"Session has no resumption secret. No ticket sent."); |
56542 | 200 |
} |
201 |
return null; |
|
202 |
} |
|
203 |
||
204 |
// construct the PSK and handshake message |
|
205 |
BigInteger nonce = shc.handshakeSession.incrTicketNonceCounter(); |
|
206 |
byte[] nonceArr = nonce.toByteArray(); |
|
56672
ec5537b61038
cleanup on HelloCookieManager, RSAClientKeyExchange and NewSessionTicket
xuelei
parents:
56660
diff
changeset
|
207 |
SecretKey psk = derivePreSharedKey( |
ec5537b61038
cleanup on HelloCookieManager, RSAClientKeyExchange and NewSessionTicket
xuelei
parents:
56660
diff
changeset
|
208 |
shc.negotiatedCipherSuite.hashAlg, |
ec5537b61038
cleanup on HelloCookieManager, RSAClientKeyExchange and NewSessionTicket
xuelei
parents:
56660
diff
changeset
|
209 |
resumptionMasterSecret.get(), nonceArr); |
56542 | 210 |
|
211 |
int sessionTimeoutSeconds = sessionCache.getSessionTimeout(); |
|
212 |
if (sessionTimeoutSeconds > SEVEN_DAYS_IN_SECONDS) { |
|
213 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
214 |
SSLLogger.fine( |
|
56672
ec5537b61038
cleanup on HelloCookieManager, RSAClientKeyExchange and NewSessionTicket
xuelei
parents:
56660
diff
changeset
|
215 |
"Session timeout is too long. No ticket sent."); |
56542 | 216 |
} |
217 |
return null; |
|
218 |
} |
|
219 |
NewSessionTicketMessage nstm = new NewSessionTicketMessage(shc, |
|
220 |
sessionTimeoutSeconds, shc.sslContext.getSecureRandom(), |
|
221 |
nonceArr, newId.getId()); |
|
222 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
223 |
SSLLogger.fine( |
|
224 |
"Produced NewSessionTicket handshake message", nstm); |
|
225 |
} |
|
226 |
||
56558
4a3deb6759b1
minor session management improvements to try to address some bugs
apetcher
parents:
56542
diff
changeset
|
227 |
// create and cache the new session |
4a3deb6759b1
minor session management improvements to try to address some bugs
apetcher
parents:
56542
diff
changeset
|
228 |
// The new session must be a child of the existing session so |
4a3deb6759b1
minor session management improvements to try to address some bugs
apetcher
parents:
56542
diff
changeset
|
229 |
// they will be invalidated together, etc. |
56542 | 230 |
SSLSessionImpl sessionCopy = new SSLSessionImpl(shc, |
231 |
shc.handshakeSession.getSuite(), newId, |
|
232 |
shc.handshakeSession.getCreationTime()); |
|
56558
4a3deb6759b1
minor session management improvements to try to address some bugs
apetcher
parents:
56542
diff
changeset
|
233 |
shc.handshakeSession.addChild(sessionCopy); |
56542 | 234 |
sessionCopy.setPreSharedKey(psk); |
235 |
sessionCopy.setPskIdentity(newId.getId()); |
|
236 |
sessionCopy.setTicketAgeAdd(nstm.ticketAgeAdd); |
|
237 |
sessionCache.put(sessionCopy); |
|
238 |
||
239 |
// Output the handshake message. |
|
240 |
nstm.write(shc.handshakeOutput); |
|
241 |
shc.handshakeOutput.flush(); |
|
242 |
||
243 |
// The message has been delivered. |
|
244 |
return null; |
|
245 |
} |
|
246 |
} |
|
247 |
||
248 |
/** |
|
249 |
* The "NewSessionTicket" handshake message producer. |
|
250 |
*/ |
|
251 |
private static final class NewSessionTicketProducer |
|
252 |
implements HandshakeProducer { |
|
253 |
||
254 |
// Prevent instantiation of this class. |
|
255 |
private NewSessionTicketProducer() { |
|
256 |
// blank |
|
257 |
} |
|
258 |
||
259 |
@Override |
|
260 |
public byte[] produce(ConnectionContext context, |
|
261 |
HandshakeMessage message) throws IOException { |
|
262 |
||
263 |
// NSTM may be sent in response to handshake messages. |
|
264 |
// For example: key update |
|
265 |
||
266 |
throw new ProviderException( |
|
267 |
"NewSessionTicket handshake producer not implemented"); |
|
268 |
} |
|
269 |
} |
|
270 |
||
271 |
||
272 |
||
273 |
private static final |
|
274 |
class NewSessionTicketConsumer implements SSLConsumer { |
|
275 |
// Prevent instantiation of this class. |
|
276 |
private NewSessionTicketConsumer() { |
|
277 |
// blank |
|
278 |
} |
|
279 |
||
280 |
@Override |
|
281 |
public void consume(ConnectionContext context, |
|
282 |
ByteBuffer message) throws IOException { |
|
283 |
// The consuming happens in client side only. |
|
56561
5f23e0400f27
Use PostHandshakeContext for New Session Ticket when approriate
ascarpino
parents:
56558
diff
changeset
|
284 |
PostHandshakeContext hc = (PostHandshakeContext) context; |
5f23e0400f27
Use PostHandshakeContext for New Session Ticket when approriate
ascarpino
parents:
56558
diff
changeset
|
285 |
NewSessionTicketMessage nstm = new NewSessionTicketMessage(hc, message); |
56542 | 286 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
287 |
SSLLogger.fine( |
|
288 |
"Consuming NewSessionTicket message", nstm); |
|
289 |
} |
|
290 |
||
291 |
// discard tickets with timeout 0 |
|
292 |
if (nstm.ticketLifetime <= 0 || |
|
293 |
nstm.ticketLifetime > SEVEN_DAYS_IN_SECONDS) { |
|
294 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
295 |
SSLLogger.fine( |
|
296 |
"Discarding NewSessionTicket with lifetime " |
|
297 |
+ nstm.ticketLifetime, nstm); |
|
298 |
} |
|
299 |
return; |
|
300 |
} |
|
301 |
||
302 |
SSLSessionContextImpl sessionCache = (SSLSessionContextImpl) |
|
56561
5f23e0400f27
Use PostHandshakeContext for New Session Ticket when approriate
ascarpino
parents:
56558
diff
changeset
|
303 |
hc.sslContext.engineGetClientSessionContext(); |
56542 | 304 |
|
305 |
if (sessionCache.getSessionTimeout() > SEVEN_DAYS_IN_SECONDS) { |
|
306 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
307 |
SSLLogger.fine( |
|
308 |
"Session cache lifetime is too long. Discarding ticket."); |
|
309 |
} |
|
310 |
return; |
|
311 |
} |
|
312 |
||
56561
5f23e0400f27
Use PostHandshakeContext for New Session Ticket when approriate
ascarpino
parents:
56558
diff
changeset
|
313 |
SSLSessionImpl sessionToSave = hc.conContext.conSession; |
56542 | 314 |
|
315 |
Optional<SecretKey> resumptionMasterSecret = |
|
316 |
sessionToSave.getResumptionMasterSecret(); |
|
317 |
if (!resumptionMasterSecret.isPresent()) { |
|
318 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
319 |
SSLLogger.fine( |
|
320 |
"Session has no resumption master secret. Ignoring ticket."); |
|
321 |
} |
|
322 |
return; |
|
323 |
} |
|
324 |
||
325 |
// derive the PSK |
|
326 |
SecretKey psk = derivePreSharedKey( |
|
327 |
sessionToSave.getSuite().hashAlg, resumptionMasterSecret.get(), |
|
328 |
nstm.ticketNonce); |
|
329 |
||
56558
4a3deb6759b1
minor session management improvements to try to address some bugs
apetcher
parents:
56542
diff
changeset
|
330 |
// create and cache the new session |
4a3deb6759b1
minor session management improvements to try to address some bugs
apetcher
parents:
56542
diff
changeset
|
331 |
// The new session must be a child of the existing session so |
4a3deb6759b1
minor session management improvements to try to address some bugs
apetcher
parents:
56542
diff
changeset
|
332 |
// they will be invalidated together, etc. |
56542 | 333 |
SessionId newId = |
56561
5f23e0400f27
Use PostHandshakeContext for New Session Ticket when approriate
ascarpino
parents:
56558
diff
changeset
|
334 |
new SessionId(true, hc.sslContext.getSecureRandom()); |
56542 | 335 |
SSLSessionImpl sessionCopy = |
56561
5f23e0400f27
Use PostHandshakeContext for New Session Ticket when approriate
ascarpino
parents:
56558
diff
changeset
|
336 |
new SSLSessionImpl(hc, sessionToSave.getSuite(), newId, |
56542 | 337 |
sessionToSave.getCreationTime()); |
56558
4a3deb6759b1
minor session management improvements to try to address some bugs
apetcher
parents:
56542
diff
changeset
|
338 |
sessionToSave.addChild(sessionCopy); |
56542 | 339 |
sessionCopy.setPreSharedKey(psk); |
340 |
sessionCopy.setTicketAgeAdd(nstm.ticketAgeAdd); |
|
341 |
sessionCopy.setPskIdentity(nstm.ticket); |
|
342 |
sessionCache.put(sessionCopy); |
|
343 |
||
56660
66c803c3ce32
key and iv implementation update, and two tests correction
xuelei
parents:
56568
diff
changeset
|
344 |
// clean handshake context |
66c803c3ce32
key and iv implementation update, and two tests correction
xuelei
parents:
56568
diff
changeset
|
345 |
hc.conContext.finishPostHandshake(); |
56542 | 346 |
} |
347 |
} |
|
348 |
} |
|
349 |