tls13_ticket.c
Go to the documentation of this file.
1 /**
2  * @file tls13_ticket.c
3  * @brief TLS 1.3 session tickets
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2023 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneSSL Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.2.4
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL TLS_TRACE_LEVEL
33 
34 //Dependencies
35 #include "tls.h"
36 #include "tls_misc.h"
37 #include "tls13_key_material.h"
38 #include "tls13_ticket.h"
39 #include "debug.h"
40 
41 //Check TLS library configuration
42 #if (TLS_SUPPORT == ENABLED && TLS_MAX_VERSION >= TLS_VERSION_1_3)
43 
44 
45 /**
46  * @brief Check whether a session ticket is valid
47  * @param[in] context Pointer to the TLS context
48  * @return TRUE is the session ticket is valid, else FALSE
49  **/
50 
52 {
53  bool_t valid = FALSE;
54 
55  //Make sure the hash algorithm associated with the ticket is valid
56  if(tlsGetHashAlgo(context->ticketHashAlgo) != NULL)
57  {
58  //Valid ticket PSK?
59  if(context->ticketPskLen > 0)
60  {
61  //Check whether TLS operates as a client or a server
62  if(context->entity == TLS_CONNECTION_END_CLIENT)
63  {
64  //Valid ticket?
65  if(context->ticket != NULL && context->ticketLen > 0)
66  {
67  valid = TRUE;
68  }
69  }
70  else
71  {
72  valid = TRUE;
73  }
74  }
75  }
76 
77  //Return TRUE is the ticket is valid, else FALSE
78  return valid;
79 }
80 
81 
82 /**
83  * @brief Save session ticket
84  * @param[in] context Pointer to the TLS context
85  * @param[out] session Pointer to the session state
86  * @return Error code
87  **/
88 
90  TlsSessionState *session)
91 {
92  const HashAlgo *hashAlgo;
93 
94  //Check TLS version
95  if(context->version != TLS_VERSION_1_3)
96  return ERROR_INVALID_VERSION;
97 
98  //Invalid session ticket?
99  if(context->ticket == NULL || context->ticketLen == 0)
100  return ERROR_INVALID_TICKET;
101 
102  //Invalid session parameters?
103  if(context->cipherSuite.identifier == 0 ||
104  context->cipherSuite.prfHashAlgo == NULL)
105  {
106  return ERROR_INVALID_SESSION;
107  }
108 
109  //Point to the cipher suite hash algorithm
110  hashAlgo = context->cipherSuite.prfHashAlgo;
111 
112  //Allocate a memory block to hold the ticket
113  session->ticket = tlsAllocMem(context->ticketLen);
114  //Failed to allocate memory?
115  if(session->ticket == NULL)
116  return ERROR_OUT_OF_MEMORY;
117 
118  //Get current time
119  session->timestamp = osGetSystemTime();
120 
121  //Save session parameters
122  session->version = context->version;
123  session->cipherSuite = context->cipherSuite.identifier;
124  session->ticketTimestamp = context->ticketTimestamp;
125  session->ticketLifetime = context->ticketLifetime;
126  session->ticketAgeAdd = context->ticketAgeAdd;
127  session->maxEarlyDataSize = context->maxEarlyDataSize;
128 
129  //Copy session ticket
130  osMemcpy(session->ticket, context->ticket, context->ticketLen);
131  session->ticketLen = context->ticketLen;
132 
133  //Each PSK established via the ticket mechanism is associated with a single
134  //hash algorithm
135  if(hashAlgo == tlsGetHashAlgo(TLS_HASH_ALGO_SHA256))
136  {
138  }
139  else if(hashAlgo == tlsGetHashAlgo(TLS_HASH_ALGO_SHA384))
140  {
142  }
143  else
144  {
146  }
147 
148  //Copy ticket PSK
149  osMemcpy(session->secret, context->ticketPsk, hashAlgo->digestSize);
150 
151 #if (TLS_ALPN_SUPPORT == ENABLED)
152  //Valid ALPN protocol?
153  if(context->selectedProtocol != NULL)
154  {
155  size_t n;
156 
157  //Retrieve the length of the ALPN protocol
158  n = osStrlen(context->selectedProtocol);
159 
160  //Allocate a memory block to hold the ALPN protocol
161  session->ticketAlpn = tlsAllocMem(n + 1);
162  //Failed to allocate memory?
163  if(session->ticketAlpn == NULL)
164  return ERROR_OUT_OF_MEMORY;
165 
166  //Copy the ALPN protocol associated with the ticket
167  osStrcpy(session->ticketAlpn, context->selectedProtocol);
168  }
169 #endif
170 
171  //Successful processing
172  return NO_ERROR;
173 }
174 
175 
176 /**
177  * @brief Restore a TLS session using session ticket
178  * @param[in] context Pointer to the TLS context
179  * @param[in] session Pointer to the session state
180  * @return Error code
181  **/
182 
184  const TlsSessionState *session)
185 {
186  systime_t serverTicketAge;
187 
188  //Check TLS version
189  if(session->version != TLS_VERSION_1_3)
190  return ERROR_INVALID_VERSION;
191 
192  //Invalid session ticket?
193  if(session->ticket == NULL || session->ticketLen == 0)
194  return ERROR_INVALID_TICKET;
195 
196  //Invalid session parameters?
197  if(session->cipherSuite == 0 ||
199  {
200  return ERROR_INVALID_SESSION;
201  }
202 
203  //Compute the time since the ticket was issued
204  serverTicketAge = osGetSystemTime() - session->ticketTimestamp;
205 
206  //Verify ticket's validity
207  if(serverTicketAge >= (session->ticketLifetime * 1000))
208  return ERROR_TICKET_EXPIRED;
209 
210  //Restore session parameters
211  context->version = session->version;
212  context->ticketCipherSuite = session->cipherSuite;
213  context->ticketHashAlgo = session->ticketHashAlgo;
214  context->ticketTimestamp = session->ticketTimestamp;
215  context->ticketLifetime = session->ticketLifetime;
216  context->ticketAgeAdd = session->ticketAgeAdd;
217  context->maxEarlyDataSize = session->maxEarlyDataSize;
218  context->sessionIdLen = 0;
219 
220  //Release existing session ticket, if any
221  if(context->ticket != NULL)
222  {
223  osMemset(context->ticket, 0, context->ticketLen);
224  tlsFreeMem(context->ticket);
225  context->ticket = NULL;
226  context->ticketLen = 0;
227  }
228 
229  //Allocate a memory block to hold the ticket
230  context->ticket = tlsAllocMem(session->ticketLen);
231  //Failed to allocate memory?
232  if(context->ticket == NULL)
233  return ERROR_OUT_OF_MEMORY;
234 
235  //Copy session ticket
236  osMemcpy(context->ticket, session->ticket, session->ticketLen);
237  context->ticketLen = session->ticketLen;
238 
239  //Each PSK established via the ticket mechanism is associated with a single
240  //hash algorithm
241  if(session->ticketHashAlgo == TLS_HASH_ALGO_SHA256)
242  {
243  context->ticketPskLen = SHA256_DIGEST_SIZE;
244  }
245  else if(session->ticketHashAlgo == TLS_HASH_ALGO_SHA384)
246  {
247  context->ticketPskLen = SHA384_DIGEST_SIZE;
248  }
249  else
250  {
251  context->ticketPskLen = 0;
252  }
253 
254  //Copy ticket PSK
255  osMemcpy(context->ticketPsk, session->secret, context->ticketPskLen);
256 
257 #if (TLS_ALPN_SUPPORT == ENABLED)
258  //Release ALPN protocol, if any
259  if(context->ticketAlpn != NULL)
260  {
261  tlsFreeMem(context->ticketAlpn);
262  context->ticketAlpn = NULL;
263  }
264 
265  //Valid ALPN protocol?
266  if(session->ticketAlpn != NULL)
267  {
268  size_t n;
269 
270  //Retrieve the length of the ALPN protocol
271  n = osStrlen(session->ticketAlpn);
272 
273  //Allocate a memory block to hold the ALPN protocol
274  context->ticketAlpn = tlsAllocMem(n + 1);
275  //Failed to allocate memory?
276  if(context->ticketAlpn == NULL)
277  return ERROR_OUT_OF_MEMORY;
278 
279  //Copy the ALPN protocol associated with the ticket
280  osStrcpy(context->ticketAlpn, session->ticketAlpn);
281  }
282 #endif
283 
284  //Successful processing
285  return NO_ERROR;
286 }
287 
288 
289 /**
290  * @brief Session ticket generation
291  * @param[in] context Pointer to the TLS context
292  * @param[in] message Pointer to the NewSessionTicket message
293  * @param[out] ticket Output stream where to write the session ticket
294  * @param[out] length Length of the session ticket, in bytes
295  * @return Error code
296  **/
297 
299  const Tls13NewSessionTicket *message, uint8_t *ticket, size_t *length)
300 {
301 #if (TLS_TICKET_SUPPORT == ENABLED)
302  error_t error;
303  size_t n;
305  const HashAlgo *hashAlgo;
306 
307  //Point to the session state information
309 
310  //Save session state
311  state->version = context->version;
312  state->cipherSuite = context->cipherSuite.identifier;
313  state->ticketTimestamp = osGetSystemTime();
314  state->ticketLifetime = ntohl(message->ticketLifetime);
315  state->ticketAgeAdd = ntohl(message->ticketAgeAdd);
316  osMemcpy(state->ticketNonce, message->ticketNonce, message->ticketNonceLen);
317  osMemset(state->ticketPsk, 0, TLS_MAX_HKDF_DIGEST_SIZE);
318 
319  //The hash function used by HKDF is the cipher suite hash algorithm
320  hashAlgo = context->cipherSuite.prfHashAlgo;
321  //Make sure the hash algorithm is valid
322  if(hashAlgo == NULL)
323  return ERROR_FAILURE;
324 
325  //Compute the PSK associated with the ticket
326  error = tls13HkdfExpandLabel(context->transportProtocol, hashAlgo,
327  context->resumptionMasterSecret, hashAlgo->digestSize, "resumption",
328  message->ticketNonce, message->ticketNonceLen, state->ticketPsk,
329  hashAlgo->digestSize);
330  //Any error to report?
331  if(error)
332  return error;
333 
334  //Save the length of the ticket PSK
335  state->ticketPskLen = hashAlgo->digestSize;
336 
337  //Compute the length of the session state
338  n = sizeof(Tls13PlaintextSessionState);
339 
340  //Make sure a valid callback has been registered
341  if(context->ticketEncryptCallback == NULL)
342  return ERROR_FAILURE;
343 
344  //Encrypt the state information
345  error = context->ticketEncryptCallback(context, (uint8_t *) state, n,
346  ticket, length, context->ticketParam);
347  //Any error to report?
348  if(error)
349  return error;
350 
351  //Successful processing
352  return NO_ERROR;
353 #else
354  //Session ticket mechanism is not implemented
355  return ERROR_NOT_IMPLEMENTED;
356 #endif
357 }
358 
359 
360 /**
361  * @brief Session ticket verification
362  * @param[in] context Pointer to the TLS context
363  * @param[in] ticket Pointer to the encrypted ticket
364  * @param[in] length Length of the encrypted ticket, in bytes
365  * @param[in] obfuscatedTicketAge Obfuscated version of the ticket age
366  * @return Error code
367  **/
368 
369 error_t tls13VerifyTicket(TlsContext *context, const uint8_t *ticket,
370  size_t length, uint32_t obfuscatedTicketAge)
371 {
372 #if (TLS_TICKET_SUPPORT == ENABLED)
373  error_t error;
374  systime_t serverTicketAge;
376  const HashAlgo *hashAlgo;
377 #if (TLS13_EARLY_DATA_SUPPORT == ENABLED)
379  systime_t clientTicketAge;
380 #endif
381 
382  //Make sure a valid callback has been registered
383  if(context->ticketDecryptCallback == NULL)
385 
386  //Check the length of the ticket
387  if(length == 0 || length > TLS13_MAX_TICKET_SIZE)
389 
390  //Allocate a buffer to store the decrypted state information
391  state = tlsAllocMem(length);
392  //Failed to allocate memory?
393  if(state == NULL)
394  return ERROR_OUT_OF_MEMORY;
395 
396  //Start of exception handling block
397  do
398  {
399  //Decrypt the received ticket
400  error = context->ticketDecryptCallback(context, ticket, length,
401  (uint8_t *) state, &length, context->ticketParam);
402  //Any error to report?
403  if(error)
404  break;
405 
406  //Check the length of the decrypted ticket
407  if(length != sizeof(Tls13PlaintextSessionState))
408  {
409  //The ticket is malformed
410  error = ERROR_INVALID_TICKET;
411  break;
412  }
413 
414  //Check TLS version
415  if(state->version != TLS_VERSION_1_3)
416  {
417  //The ticket is not valid
418  error = ERROR_INVALID_TICKET;
419  break;
420  }
421 
422  //Compute the time since the ticket was issued
423  serverTicketAge = osGetSystemTime() - state->ticketTimestamp;
424 
425  //Verify ticket's validity
426  if(serverTicketAge >= (state->ticketLifetime * 1000))
427  {
428  //The ticket is not valid
429  error = ERROR_INVALID_TICKET;
430  break;
431  }
432 
433 #if (TLS13_EARLY_DATA_SUPPORT == ENABLED)
434  //Compute the ticket age for the selected PSK identity by subtracting
435  //ticket_age_add from obfuscated_ticket_age modulo 2^32
436  clientTicketAge = obfuscatedTicketAge - state->ticketAgeAdd;
437 
438  //Calculate the difference between the client's view and the server's
439  //view of the age of the ticket
440  if(clientTicketAge < serverTicketAge)
441  {
442  delta = serverTicketAge - clientTicketAge;
443  }
444  else
445  {
446  delta = clientTicketAge - serverTicketAge;
447  }
448 
449  //For PSKs provisioned via NewSessionTicket, the server must validate
450  //that the ticket age for the selected PSK identity is within a small
451  //tolerance of the time since the ticket was issued
453  {
454  //If it is not, the server should proceed with the handshake but
455  //reject 0-RTT, and should not take any other action that assumes
456  //that this ClientHello is fresh (refer to RFC 8446, 4.2.10)
457  context->earlyDataRejected = TRUE;
458  }
459 #endif
460 
461  //Any ticket must only be resumed with a cipher suite that has the same
462  //KDF hash algorithm as that used to establish the original connection
463  error = tlsSelectCipherSuite(context, state->cipherSuite);
464  //Any error to report?
465  if(error)
466  break;
467 
468  //Point to the cipher suite hash algorithm
469  hashAlgo = context->cipherSuite.prfHashAlgo;
470  //Make sure the hash algorithm is valid
471  if(hashAlgo == NULL)
472  {
473  //The ticket is malformed
474  error = ERROR_INVALID_TICKET;
475  break;
476  }
477 
478  //The server must ensure that it selects a compatible PSK and cipher suite
479  if(state->ticketPskLen != hashAlgo->digestSize)
480  {
481  //The ticket is malformed
482  error = ERROR_INVALID_TICKET;
483  break;
484  }
485 
486  //Restore ticket PSK
487  osMemcpy(context->ticketPsk, state->ticketPsk, state->ticketPskLen);
488  context->ticketPskLen = state->ticketPskLen;
489 
490  //Retrieve the hash algorithm associated with the ticket
491  if(hashAlgo == tlsGetHashAlgo(TLS_HASH_ALGO_SHA256))
492  {
493  context->ticketHashAlgo = TLS_HASH_ALGO_SHA256;
494  }
495  else if(hashAlgo == tlsGetHashAlgo(TLS_HASH_ALGO_SHA384))
496  {
497  context->ticketHashAlgo = TLS_HASH_ALGO_SHA384;
498  }
499  else
500  {
501  context->ticketHashAlgo = TLS_HASH_ALGO_NONE;
502  }
503 
504  //End of exception handling block
505  } while(0);
506 
507  //Release state information
508  osMemset(state, 0, length);
509  tlsFreeMem(state);
510 
511  //Return status code
512  return error;
513 #else
514  //Session ticket mechanism is not implemented
516 #endif
517 }
518 
519 #endif
#define tlsAllocMem(size)
Definition: tls.h:820
size_t ticketLen
Length of the session ticket.
Definition: tls.h:1976
uint8_t length
Definition: coap_common.h:193
TLS helper functions.
int bool_t
Definition: compiler_port.h:53
error_t tls13VerifyTicket(TlsContext *context, const uint8_t *ticket, size_t length, uint32_t obfuscatedTicketAge)
Session ticket verification.
Definition: tls13_ticket.c:369
systime_t ticketTimestamp
Timestamp to manage ticket lifetime.
Definition: tls.h:1978
uint8_t * ticket
Session ticket.
Definition: tls.h:1975
@ ERROR_NOT_IMPLEMENTED
Definition: error.h:66
uint8_t delta
Definition: coap_common.h:194
@ ERROR_DECRYPTION_FAILED
Definition: error.h:241
uint8_t secret[TLS_MASTER_SECRET_SIZE]
Master secret (TLS 1.2) or ticket PSK (TLS 1.3)
Definition: tls.h:1969
systime_t timestamp
Time stamp to manage entry lifetime.
Definition: tls.h:1968
#define TRUE
Definition: os_port.h:52
size_t digestSize
Definition: crypto.h:964
#define TLS13_TICKET_AGE_TOLERANCE
Definition: tls13_misc.h:106
TLS 1.3 session tickets.
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
#define osStrlen(s)
Definition: os_port.h:164
@ ERROR_INVALID_VERSION
Definition: error.h:118
@ TLS_HASH_ALGO_NONE
Definition: tls.h:1164
uint16_t cipherSuite
Cipher suite identifier.
Definition: tls.h:1967
error_t tls13SaveSessionTicket(const TlsContext *context, TlsSessionState *session)
Save session ticket.
Definition: tls13_ticket.c:89
#define FALSE
Definition: os_port.h:48
#define osMemcpy(dest, src, length)
Definition: os_port.h:140
#define TlsContext
Definition: tls.h:36
error_t
Error codes.
Definition: error.h:43
const HashAlgo * tlsGetHashAlgo(uint8_t hashAlgoId)
Get the hash algorithm that matches the specified identifier.
Definition: tls_misc.c:1167
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
error_t tlsSelectCipherSuite(TlsContext *context, uint16_t identifier)
Set cipher suite.
Definition: tls_misc.c:334
#define TLS_VERSION_1_3
Definition: tls.h:99
@ TLS_HASH_ALGO_SHA384
Definition: tls.h:1169
bool_t tls13IsTicketValid(TlsContext *context)
Check whether a session ticket is valid.
Definition: tls13_ticket.c:51
@ TLS_HASH_ALGO_SHA256
Definition: tls.h:1168
uint32_t ticketAgeAdd
Random value used to obscure the age of the ticket.
Definition: tls.h:1980
#define SHA384_DIGEST_SIZE
Definition: sha384.h:41
uint32_t systime_t
System time.
#define TLS13_MAX_TICKET_SIZE
Definition: tls13_misc.h:92
uint32_t maxEarlyDataSize
Maximum amount of 0-RTT data that the client is allowed to send.
Definition: tls.h:1983
uint8_t ticket[]
Definition: tls.h:1773
TLS session state.
Definition: tls.h:1965
uint8_t n
TlsHashAlgo ticketHashAlgo
Hash algorithm associated with the ticket.
Definition: tls.h:1981
#define TLS_MAX_HKDF_DIGEST_SIZE
Definition: tls.h:880
@ TLS_CONNECTION_END_CLIENT
Definition: tls.h:922
uint8_t message[]
Definition: chap.h:152
TLS (Transport Layer Security)
__start_packed struct @15 Tls13PlaintextSessionState
Session state information.
uint16_t version
TLS protocol version.
Definition: tls.h:1966
error_t tls13GenerateTicket(TlsContext *context, const Tls13NewSessionTicket *message, uint8_t *ticket, size_t *length)
Session ticket generation.
Definition: tls13_ticket.c:298
TLS 1.3 key schedule.
uint32_t ticketLifetime
Lifetime of the ticket.
Definition: tls.h:1979
Common interface for hash algorithms.
Definition: crypto.h:958
error_t tls13HkdfExpandLabel(TlsTransportProtocol transportProtocol, const HashAlgo *hash, const uint8_t *secret, size_t secretLen, const char_t *label, const uint8_t *context, size_t contextLen, uint8_t *output, size_t outputLen)
HKDF-Expand-Label function.
#define osMemset(p, value, length)
Definition: os_port.h:134
#define tlsFreeMem(p)
Definition: tls.h:825
#define SHA256_DIGEST_SIZE
Definition: sha256.h:45
__start_packed struct @12 Tls13NewSessionTicket
NewSessionTicket message (TLS 1.3)
#define osStrcpy(s1, s2)
Definition: os_port.h:206
@ ERROR_TICKET_EXPIRED
Definition: error.h:286
@ ERROR_INVALID_SESSION
Definition: error.h:285
error_t tls13RestoreSessionTicket(TlsContext *context, const TlsSessionState *session)
Restore a TLS session using session ticket.
Definition: tls13_ticket.c:183
#define ntohl(value)
Definition: cpu_endian.h:422
@ ERROR_INVALID_TICKET
Definition: error.h:227
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
char_t * ticketAlpn
ALPN protocol associated with the ticket.
Definition: tls.h:1982
systime_t osGetSystemTime(void)
Retrieve system time.