author | apetcher |
Tue, 15 May 2018 14:54:04 -0400 | |
branch | JDK-8145252-TLS13-branch |
changeset 56558 | 4a3deb6759b1 |
parent 56542 | 56aaa6cb3693 |
child 56561 | 5f23e0400f27 |
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, |
|
87 |
"Ticket has length 0"); |
|
88 |
} |
|
89 |
||
90 |
SSLExtension[] supportedExtensions = |
|
91 |
context.sslConfig.getEnabledExtensions( |
|
92 |
SSLHandshake.NEW_SESSION_TICKET); |
|
93 |
||
94 |
if (m.hasRemaining()) { |
|
95 |
this.extensions = |
|
96 |
new SSLExtensions(this, m, supportedExtensions); |
|
97 |
} else { |
|
98 |
this.extensions = new SSLExtensions(this); |
|
99 |
} |
|
100 |
} |
|
101 |
||
102 |
@Override |
|
103 |
public SSLHandshake handshakeType() { |
|
104 |
return SSLHandshake.NEW_SESSION_TICKET; |
|
105 |
} |
|
106 |
||
107 |
@Override |
|
108 |
public int messageLength() { |
|
109 |
return 8 + ticketNonce.length + 1 + ticket.length |
|
110 |
+ 2 + extensions.length(); |
|
111 |
} |
|
112 |
||
113 |
@Override |
|
114 |
public void send(HandshakeOutStream hos) throws IOException { |
|
115 |
hos.putInt32(ticketLifetime); |
|
116 |
hos.putInt32(ticketAgeAdd); |
|
117 |
hos.putBytes8(ticketNonce); |
|
118 |
hos.putBytes16(ticket); |
|
119 |
extensions.send(hos); |
|
120 |
} |
|
121 |
||
122 |
@Override |
|
123 |
public String toString() { |
|
124 |
MessageFormat messageFormat = new MessageFormat( |
|
125 |
"\"NewSessionTicket\": '{'\n" + |
|
126 |
" \"ticket_lifetime\" : \"{0}\",\n" + |
|
127 |
" \"ticket_age_add\" : \"{1}\",\n" + |
|
128 |
" \"ticket_nonce\" : \"{2}\",\n" + |
|
129 |
" \"ticket\" : \"{3}\",\n" + |
|
130 |
" \"extensions\" : [\n" + |
|
131 |
"{5}\n" + |
|
132 |
" ]\n" + |
|
133 |
"'}'", |
|
134 |
Locale.ENGLISH); |
|
135 |
||
136 |
Object[] messageFields = { |
|
137 |
ticketLifetime, |
|
138 |
"omitted", //ticketAgeAdd should not be logged |
|
139 |
Utilities.toHexString(ticketNonce), |
|
140 |
Utilities.toHexString(ticket), |
|
141 |
Utilities.indent(extensions.toString(), " ") |
|
142 |
}; |
|
143 |
||
144 |
return messageFormat.format(messageFields); |
|
145 |
} |
|
146 |
} |
|
147 |
||
148 |
private static SecretKey derivePreSharedKey(CipherSuite.HashAlg hashAlg, |
|
149 |
SecretKey resumptionMasterSecret, |
|
150 |
byte[] nonce) throws IOException { |
|
151 |
||
152 |
try { |
|
153 |
HKDF hkdf = new HKDF(hashAlg.name); |
|
154 |
byte[] hkdfInfo = SSLSecretDerivation.createHkdfInfo( |
|
155 |
"tls13 resumption".getBytes(), nonce, hashAlg.hashLength); |
|
156 |
return hkdf.expand(resumptionMasterSecret, hkdfInfo, |
|
157 |
hashAlg.hashLength, "TlsPreSharedKey"); |
|
158 |
||
159 |
} catch (GeneralSecurityException gse) { |
|
160 |
throw (SSLHandshakeException) new SSLHandshakeException( |
|
161 |
"Could not derive PSK").initCause(gse); |
|
162 |
} |
|
163 |
} |
|
164 |
||
165 |
private static final |
|
166 |
class NewSessionTicketKickstartProducer implements SSLProducer { |
|
167 |
||
168 |
@Override |
|
169 |
public byte[] produce(ConnectionContext context) throws IOException { |
|
170 |
// The producing happens in server side only. |
|
171 |
ServerHandshakeContext shc = (ServerHandshakeContext)context; |
|
172 |
||
173 |
if (shc.pskKeyExchangeModes.isEmpty()) { |
|
174 |
// client doesn't support PSK |
|
175 |
return null; |
|
176 |
} |
|
177 |
if (!shc.handshakeSession.isRejoinable()) { |
|
178 |
return null; |
|
179 |
} |
|
180 |
||
181 |
// get a new session ID |
|
182 |
SSLSessionContextImpl sessionCache = (SSLSessionContextImpl) |
|
183 |
shc.sslContext.engineGetServerSessionContext(); |
|
184 |
SessionId newId = new SessionId(true, |
|
185 |
shc.sslContext.getSecureRandom()); |
|
186 |
||
187 |
Optional<SecretKey> resumptionMasterSecret = |
|
188 |
shc.handshakeSession.getResumptionMasterSecret(); |
|
189 |
if (!resumptionMasterSecret.isPresent()) { |
|
190 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
191 |
SSLLogger.fine( |
|
192 |
"Session has no resumption secret. No ticket sent."); |
|
193 |
} |
|
194 |
return null; |
|
195 |
} |
|
196 |
||
197 |
// construct the PSK and handshake message |
|
198 |
BigInteger nonce = shc.handshakeSession.incrTicketNonceCounter(); |
|
199 |
byte[] nonceArr = nonce.toByteArray(); |
|
200 |
SecretKey psk = derivePreSharedKey(shc.negotiatedCipherSuite.hashAlg, |
|
201 |
resumptionMasterSecret.get(), nonceArr); |
|
202 |
||
203 |
int sessionTimeoutSeconds = sessionCache.getSessionTimeout(); |
|
204 |
if (sessionTimeoutSeconds > SEVEN_DAYS_IN_SECONDS) { |
|
205 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
206 |
SSLLogger.fine( |
|
207 |
"Session timeout is too long. No NewSessionTicket sent."); |
|
208 |
} |
|
209 |
return null; |
|
210 |
} |
|
211 |
NewSessionTicketMessage nstm = new NewSessionTicketMessage(shc, |
|
212 |
sessionTimeoutSeconds, shc.sslContext.getSecureRandom(), |
|
213 |
nonceArr, newId.getId()); |
|
214 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
215 |
SSLLogger.fine( |
|
216 |
"Produced NewSessionTicket handshake message", nstm); |
|
217 |
} |
|
218 |
||
56558
4a3deb6759b1
minor session management improvements to try to address some bugs
apetcher
parents:
56542
diff
changeset
|
219 |
// create and cache the new session |
4a3deb6759b1
minor session management improvements to try to address some bugs
apetcher
parents:
56542
diff
changeset
|
220 |
// 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
|
221 |
// they will be invalidated together, etc. |
56542 | 222 |
SSLSessionImpl sessionCopy = new SSLSessionImpl(shc, |
223 |
shc.handshakeSession.getSuite(), newId, |
|
224 |
shc.handshakeSession.getCreationTime()); |
|
56558
4a3deb6759b1
minor session management improvements to try to address some bugs
apetcher
parents:
56542
diff
changeset
|
225 |
shc.handshakeSession.addChild(sessionCopy); |
56542 | 226 |
sessionCopy.setPreSharedKey(psk); |
227 |
sessionCopy.setPskIdentity(newId.getId()); |
|
228 |
sessionCopy.setTicketAgeAdd(nstm.ticketAgeAdd); |
|
229 |
sessionCache.put(sessionCopy); |
|
230 |
||
231 |
// Output the handshake message. |
|
232 |
nstm.write(shc.handshakeOutput); |
|
233 |
shc.handshakeOutput.flush(); |
|
234 |
||
235 |
// The message has been delivered. |
|
236 |
return null; |
|
237 |
} |
|
238 |
} |
|
239 |
||
240 |
/** |
|
241 |
* The "NewSessionTicket" handshake message producer. |
|
242 |
*/ |
|
243 |
private static final class NewSessionTicketProducer |
|
244 |
implements HandshakeProducer { |
|
245 |
||
246 |
// Prevent instantiation of this class. |
|
247 |
private NewSessionTicketProducer() { |
|
248 |
// blank |
|
249 |
} |
|
250 |
||
251 |
@Override |
|
252 |
public byte[] produce(ConnectionContext context, |
|
253 |
HandshakeMessage message) throws IOException { |
|
254 |
||
255 |
// NSTM may be sent in response to handshake messages. |
|
256 |
// For example: key update |
|
257 |
||
258 |
throw new ProviderException( |
|
259 |
"NewSessionTicket handshake producer not implemented"); |
|
260 |
} |
|
261 |
} |
|
262 |
||
263 |
||
264 |
||
265 |
private static final |
|
266 |
class NewSessionTicketConsumer implements SSLConsumer { |
|
267 |
// Prevent instantiation of this class. |
|
268 |
private NewSessionTicketConsumer() { |
|
269 |
// blank |
|
270 |
} |
|
271 |
||
272 |
@Override |
|
273 |
public void consume(ConnectionContext context, |
|
274 |
ByteBuffer message) throws IOException { |
|
275 |
// The consuming happens in client side only. |
|
276 |
ClientHandshakeContext chc = (ClientHandshakeContext)context; |
|
277 |
NewSessionTicketMessage nstm = new NewSessionTicketMessage(chc, message); |
|
278 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
279 |
SSLLogger.fine( |
|
280 |
"Consuming NewSessionTicket message", nstm); |
|
281 |
} |
|
282 |
||
283 |
// discard tickets with timeout 0 |
|
284 |
if (nstm.ticketLifetime <= 0 || |
|
285 |
nstm.ticketLifetime > SEVEN_DAYS_IN_SECONDS) { |
|
286 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
287 |
SSLLogger.fine( |
|
288 |
"Discarding NewSessionTicket with lifetime " |
|
289 |
+ nstm.ticketLifetime, nstm); |
|
290 |
} |
|
291 |
return; |
|
292 |
} |
|
293 |
||
294 |
SSLSessionContextImpl sessionCache = (SSLSessionContextImpl) |
|
295 |
chc.sslContext.engineGetClientSessionContext(); |
|
296 |
||
297 |
if (sessionCache.getSessionTimeout() > SEVEN_DAYS_IN_SECONDS) { |
|
298 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
299 |
SSLLogger.fine( |
|
300 |
"Session cache lifetime is too long. Discarding ticket."); |
|
301 |
} |
|
302 |
return; |
|
303 |
} |
|
304 |
||
305 |
SSLSessionImpl sessionToSave = chc.conContext.conSession; |
|
306 |
||
307 |
Optional<SecretKey> resumptionMasterSecret = |
|
308 |
sessionToSave.getResumptionMasterSecret(); |
|
309 |
if (!resumptionMasterSecret.isPresent()) { |
|
310 |
if (SSLLogger.isOn && SSLLogger.isOn("ssl,handshake")) { |
|
311 |
SSLLogger.fine( |
|
312 |
"Session has no resumption master secret. Ignoring ticket."); |
|
313 |
} |
|
314 |
return; |
|
315 |
} |
|
316 |
||
317 |
// derive the PSK |
|
318 |
SecretKey psk = derivePreSharedKey( |
|
319 |
sessionToSave.getSuite().hashAlg, resumptionMasterSecret.get(), |
|
320 |
nstm.ticketNonce); |
|
321 |
||
56558
4a3deb6759b1
minor session management improvements to try to address some bugs
apetcher
parents:
56542
diff
changeset
|
322 |
// create and cache the new session |
4a3deb6759b1
minor session management improvements to try to address some bugs
apetcher
parents:
56542
diff
changeset
|
323 |
// 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
|
324 |
// they will be invalidated together, etc. |
56542 | 325 |
chc.negotiatedProtocol = chc.conContext.protocolVersion; |
326 |
SessionId newId = |
|
327 |
new SessionId(true, chc.sslContext.getSecureRandom()); |
|
328 |
SSLSessionImpl sessionCopy = |
|
329 |
new SSLSessionImpl(chc, sessionToSave.getSuite(), newId, |
|
330 |
sessionToSave.getCreationTime()); |
|
56558
4a3deb6759b1
minor session management improvements to try to address some bugs
apetcher
parents:
56542
diff
changeset
|
331 |
sessionToSave.addChild(sessionCopy); |
56542 | 332 |
sessionCopy.setPreSharedKey(psk); |
333 |
sessionCopy.setTicketAgeAdd(nstm.ticketAgeAdd); |
|
334 |
sessionCopy.setPskIdentity(nstm.ticket); |
|
335 |
sessionCache.put(sessionCopy); |
|
336 |
||
337 |
// The handshakeContext is no longer needed |
|
338 |
chc.conContext.handshakeContext = null; |
|
339 |
} |
|
340 |
} |
|
341 |
||
342 |
} |
|
343 |