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-2024 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.4.0
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 if(hashAlgo == tlsGetHashAlgo(TLS_HASH_ALGO_SM3))
144  {
146  }
147  else
148  {
150  }
151 
152  //Copy ticket PSK
153  osMemcpy(session->secret, context->ticketPsk, hashAlgo->digestSize);
154 
155 #if (TLS_ALPN_SUPPORT == ENABLED)
156  //Valid ALPN protocol?
157  if(context->selectedProtocol != NULL)
158  {
159  size_t n;
160 
161  //Retrieve the length of the ALPN protocol
162  n = osStrlen(context->selectedProtocol);
163 
164  //Allocate a memory block to hold the ALPN protocol
165  session->ticketAlpn = tlsAllocMem(n + 1);
166  //Failed to allocate memory?
167  if(session->ticketAlpn == NULL)
168  return ERROR_OUT_OF_MEMORY;
169 
170  //Copy the ALPN protocol associated with the ticket
171  osStrcpy(session->ticketAlpn, context->selectedProtocol);
172  }
173 #endif
174 
175  //Successful processing
176  return NO_ERROR;
177 }
178 
179 
180 /**
181  * @brief Restore a TLS session using session ticket
182  * @param[in] context Pointer to the TLS context
183  * @param[in] session Pointer to the session state
184  * @return Error code
185  **/
186 
188  const TlsSessionState *session)
189 {
190  systime_t serverTicketAge;
191 
192  //Check TLS version
193  if(session->version != TLS_VERSION_1_3)
194  return ERROR_INVALID_VERSION;
195 
196  //Invalid session ticket?
197  if(session->ticket == NULL || session->ticketLen == 0)
198  return ERROR_INVALID_TICKET;
199 
200  //Invalid session parameters?
201  if(session->cipherSuite == 0 ||
203  {
204  return ERROR_INVALID_SESSION;
205  }
206 
207  //Compute the time since the ticket was issued
208  serverTicketAge = osGetSystemTime() - session->ticketTimestamp;
209 
210  //Verify ticket's validity
211  if(serverTicketAge >= (session->ticketLifetime * 1000))
212  return ERROR_TICKET_EXPIRED;
213 
214  //Restore session parameters
215  context->version = session->version;
216  context->ticketCipherSuite = session->cipherSuite;
217  context->ticketHashAlgo = session->ticketHashAlgo;
218  context->ticketTimestamp = session->ticketTimestamp;
219  context->ticketLifetime = session->ticketLifetime;
220  context->ticketAgeAdd = session->ticketAgeAdd;
221  context->maxEarlyDataSize = session->maxEarlyDataSize;
222  context->sessionIdLen = 0;
223 
224  //Release existing session ticket, if any
225  if(context->ticket != NULL)
226  {
227  osMemset(context->ticket, 0, context->ticketLen);
228  tlsFreeMem(context->ticket);
229  context->ticket = NULL;
230  context->ticketLen = 0;
231  }
232 
233  //Allocate a memory block to hold the ticket
234  context->ticket = tlsAllocMem(session->ticketLen);
235  //Failed to allocate memory?
236  if(context->ticket == NULL)
237  return ERROR_OUT_OF_MEMORY;
238 
239  //Copy session ticket
240  osMemcpy(context->ticket, session->ticket, session->ticketLen);
241  context->ticketLen = session->ticketLen;
242 
243  //Each PSK established via the ticket mechanism is associated with a single
244  //hash algorithm
245  if(session->ticketHashAlgo == TLS_HASH_ALGO_SHA256)
246  {
247  context->ticketPskLen = 32;
248  }
249  else if(session->ticketHashAlgo == TLS_HASH_ALGO_SHA384)
250  {
251  context->ticketPskLen = 48;
252  }
253  else if(session->ticketHashAlgo == TLS_HASH_ALGO_SM3)
254  {
255  context->ticketPskLen = 32;
256  }
257  else
258  {
259  context->ticketPskLen = 0;
260  }
261 
262  //Copy ticket PSK
263  osMemcpy(context->ticketPsk, session->secret, context->ticketPskLen);
264 
265 #if (TLS_ALPN_SUPPORT == ENABLED)
266  //Release ALPN protocol, if any
267  if(context->ticketAlpn != NULL)
268  {
269  tlsFreeMem(context->ticketAlpn);
270  context->ticketAlpn = NULL;
271  }
272 
273  //Valid ALPN protocol?
274  if(session->ticketAlpn != NULL)
275  {
276  size_t n;
277 
278  //Retrieve the length of the ALPN protocol
279  n = osStrlen(session->ticketAlpn);
280 
281  //Allocate a memory block to hold the ALPN protocol
282  context->ticketAlpn = tlsAllocMem(n + 1);
283  //Failed to allocate memory?
284  if(context->ticketAlpn == NULL)
285  return ERROR_OUT_OF_MEMORY;
286 
287  //Copy the ALPN protocol associated with the ticket
288  osStrcpy(context->ticketAlpn, session->ticketAlpn);
289  }
290 #endif
291 
292  //Successful processing
293  return NO_ERROR;
294 }
295 
296 
297 /**
298  * @brief Session ticket generation
299  * @param[in] context Pointer to the TLS context
300  * @param[in] message Pointer to the NewSessionTicket message
301  * @param[out] ticket Output stream where to write the session ticket
302  * @param[out] length Length of the session ticket, in bytes
303  * @return Error code
304  **/
305 
307  const Tls13NewSessionTicket *message, uint8_t *ticket, size_t *length)
308 {
309 #if (TLS_TICKET_SUPPORT == ENABLED)
310  error_t error;
311  size_t n;
313  const HashAlgo *hashAlgo;
314 
315  //Point to the session state information
317 
318  //Save session state
319  state->version = context->version;
320  state->cipherSuite = context->cipherSuite.identifier;
321  state->ticketTimestamp = osGetSystemTime();
322  state->ticketLifetime = ntohl(message->ticketLifetime);
323  state->ticketAgeAdd = ntohl(message->ticketAgeAdd);
324  osMemcpy(state->ticketNonce, message->ticketNonce, message->ticketNonceLen);
325  osMemset(state->ticketPsk, 0, TLS_MAX_HKDF_DIGEST_SIZE);
326 
327  //The hash function used by HKDF is the cipher suite hash algorithm
328  hashAlgo = context->cipherSuite.prfHashAlgo;
329  //Make sure the hash algorithm is valid
330  if(hashAlgo == NULL)
331  return ERROR_FAILURE;
332 
333  //Compute the PSK associated with the ticket
334  error = tls13HkdfExpandLabel(context->transportProtocol, hashAlgo,
335  context->resumptionMasterSecret, hashAlgo->digestSize, "resumption",
336  message->ticketNonce, message->ticketNonceLen, state->ticketPsk,
337  hashAlgo->digestSize);
338  //Any error to report?
339  if(error)
340  return error;
341 
342  //Save the length of the ticket PSK
343  state->ticketPskLen = hashAlgo->digestSize;
344 
345  //Compute the length of the session state
346  n = sizeof(Tls13PlaintextSessionState);
347 
348  //Make sure a valid callback has been registered
349  if(context->ticketEncryptCallback == NULL)
350  return ERROR_FAILURE;
351 
352  //Encrypt the state information
353  error = context->ticketEncryptCallback(context, (uint8_t *) state, n,
354  ticket, length, context->ticketParam);
355  //Any error to report?
356  if(error)
357  return error;
358 
359  //Successful processing
360  return NO_ERROR;
361 #else
362  //Session ticket mechanism is not implemented
363  return ERROR_NOT_IMPLEMENTED;
364 #endif
365 }
366 
367 
368 /**
369  * @brief Session ticket verification
370  * @param[in] context Pointer to the TLS context
371  * @param[in] ticket Pointer to the encrypted ticket
372  * @param[in] length Length of the encrypted ticket, in bytes
373  * @param[in] obfuscatedTicketAge Obfuscated version of the ticket age
374  * @return Error code
375  **/
376 
377 error_t tls13VerifyTicket(TlsContext *context, const uint8_t *ticket,
378  size_t length, uint32_t obfuscatedTicketAge)
379 {
380 #if (TLS_TICKET_SUPPORT == ENABLED)
381  error_t error;
382  systime_t serverTicketAge;
384  const HashAlgo *hashAlgo;
385 #if (TLS13_EARLY_DATA_SUPPORT == ENABLED)
387  systime_t clientTicketAge;
388 #endif
389 
390  //Make sure a valid callback has been registered
391  if(context->ticketDecryptCallback == NULL)
393 
394  //Check the length of the ticket
395  if(length == 0 || length > TLS13_MAX_TICKET_SIZE)
397 
398  //Allocate a buffer to store the decrypted state information
399  state = tlsAllocMem(length);
400  //Failed to allocate memory?
401  if(state == NULL)
402  return ERROR_OUT_OF_MEMORY;
403 
404  //Start of exception handling block
405  do
406  {
407  //Decrypt the received ticket
408  error = context->ticketDecryptCallback(context, ticket, length,
409  (uint8_t *) state, &length, context->ticketParam);
410  //Any error to report?
411  if(error)
412  break;
413 
414  //Check the length of the decrypted ticket
415  if(length != sizeof(Tls13PlaintextSessionState))
416  {
417  //The ticket is malformed
418  error = ERROR_INVALID_TICKET;
419  break;
420  }
421 
422  //Check TLS version
423  if(state->version != TLS_VERSION_1_3)
424  {
425  //The ticket is not valid
426  error = ERROR_INVALID_TICKET;
427  break;
428  }
429 
430  //Compute the time since the ticket was issued
431  serverTicketAge = osGetSystemTime() - state->ticketTimestamp;
432 
433  //Verify ticket's validity
434  if(serverTicketAge >= (state->ticketLifetime * 1000))
435  {
436  //The ticket is not valid
437  error = ERROR_INVALID_TICKET;
438  break;
439  }
440 
441 #if (TLS13_EARLY_DATA_SUPPORT == ENABLED)
442  //Compute the ticket age for the selected PSK identity by subtracting
443  //ticket_age_add from obfuscated_ticket_age modulo 2^32
444  clientTicketAge = obfuscatedTicketAge - state->ticketAgeAdd;
445 
446  //Calculate the difference between the client's view and the server's
447  //view of the age of the ticket
448  if(clientTicketAge < serverTicketAge)
449  {
450  delta = serverTicketAge - clientTicketAge;
451  }
452  else
453  {
454  delta = clientTicketAge - serverTicketAge;
455  }
456 
457  //For PSKs provisioned via NewSessionTicket, the server must validate
458  //that the ticket age for the selected PSK identity is within a small
459  //tolerance of the time since the ticket was issued
461  {
462  //If it is not, the server should proceed with the handshake but
463  //reject 0-RTT, and should not take any other action that assumes
464  //that this ClientHello is fresh (refer to RFC 8446, 4.2.10)
465  context->earlyDataRejected = TRUE;
466  }
467 #endif
468 
469  //Any ticket must only be resumed with a cipher suite that has the same
470  //KDF hash algorithm as that used to establish the original connection
471  error = tlsSelectCipherSuite(context, state->cipherSuite);
472  //Any error to report?
473  if(error)
474  break;
475 
476  //Point to the cipher suite hash algorithm
477  hashAlgo = context->cipherSuite.prfHashAlgo;
478  //Make sure the hash algorithm is valid
479  if(hashAlgo == NULL)
480  {
481  //The ticket is malformed
482  error = ERROR_INVALID_TICKET;
483  break;
484  }
485 
486  //The server must ensure that it selects a compatible PSK and cipher suite
487  if(state->ticketPskLen != hashAlgo->digestSize)
488  {
489  //The ticket is malformed
490  error = ERROR_INVALID_TICKET;
491  break;
492  }
493 
494  //Restore ticket PSK
495  osMemcpy(context->ticketPsk, state->ticketPsk, state->ticketPskLen);
496  context->ticketPskLen = state->ticketPskLen;
497 
498  //Retrieve the hash algorithm associated with the ticket
499  if(hashAlgo == tlsGetHashAlgo(TLS_HASH_ALGO_SHA256))
500  {
501  context->ticketHashAlgo = TLS_HASH_ALGO_SHA256;
502  }
503  else if(hashAlgo == tlsGetHashAlgo(TLS_HASH_ALGO_SHA384))
504  {
505  context->ticketHashAlgo = TLS_HASH_ALGO_SHA384;
506  }
507  else if(hashAlgo == tlsGetHashAlgo(TLS_HASH_ALGO_SM3))
508  {
509  context->ticketHashAlgo = TLS_HASH_ALGO_SM3;
510  }
511  else
512  {
513  context->ticketHashAlgo = TLS_HASH_ALGO_NONE;
514  }
515 
516  //End of exception handling block
517  } while(0);
518 
519  //Release state information
520  osMemset(state, 0, length);
521  tlsFreeMem(state);
522 
523  //Return status code
524  return error;
525 #else
526  //Session ticket mechanism is not implemented
528 #endif
529 }
530 
531 #endif
uint8_t message[]
Definition: chap.h:154
uint8_t delta
Definition: coap_common.h:196
int bool_t
Definition: compiler_port.h:53
#define ntohl(value)
Definition: cpu_endian.h:422
Debugging facilities.
uint8_t n
error_t
Error codes.
Definition: error.h:43
@ ERROR_DECRYPTION_FAILED
Definition: error.h:241
@ ERROR_TICKET_EXPIRED
Definition: error.h:286
@ ERROR_INVALID_SESSION
Definition: error.h:285
@ ERROR_INVALID_TICKET
Definition: error.h:227
@ ERROR_NOT_IMPLEMENTED
Definition: error.h:66
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
@ ERROR_INVALID_VERSION
Definition: error.h:118
#define osMemset(p, value, length)
Definition: os_port.h:135
#define osMemcpy(dest, src, length)
Definition: os_port.h:141
#define osStrlen(s)
Definition: os_port.h:165
#define TRUE
Definition: os_port.h:50
#define FALSE
Definition: os_port.h:46
#define osStrcpy(s1, s2)
Definition: os_port.h:207
systime_t osGetSystemTime(void)
Retrieve system time.
uint32_t systime_t
System time.
Common interface for hash algorithms.
Definition: crypto.h:1014
size_t digestSize
Definition: crypto.h:1020
TLS session state.
Definition: tls.h:2021
systime_t ticketTimestamp
Timestamp to manage ticket lifetime.
Definition: tls.h:2034
systime_t timestamp
Time stamp to manage entry lifetime.
Definition: tls.h:2024
uint8_t secret[TLS_MASTER_SECRET_SIZE]
Master secret (TLS 1.2) or ticket PSK (TLS 1.3)
Definition: tls.h:2025
size_t ticketLen
Length of the session ticket.
Definition: tls.h:2032
uint32_t maxEarlyDataSize
Maximum amount of 0-RTT data that the client is allowed to send.
Definition: tls.h:2039
uint16_t version
TLS protocol version.
Definition: tls.h:2022
uint8_t * ticket
Session ticket.
Definition: tls.h:2031
uint16_t cipherSuite
Cipher suite identifier.
Definition: tls.h:2023
uint32_t ticketAgeAdd
Random value used to obscure the age of the ticket.
Definition: tls.h:2036
char_t * ticketAlpn
ALPN protocol associated with the ticket.
Definition: tls.h:2038
TlsHashAlgo ticketHashAlgo
Hash algorithm associated with the ticket.
Definition: tls.h:2037
uint32_t ticketLifetime
Lifetime of the ticket.
Definition: tls.h:2035
uint8_t length
Definition: tcp.h:368
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.
TLS 1.3 key schedule.
Tls13NewSessionTicket
Definition: tls13_misc.h:314
#define TLS13_MAX_TICKET_SIZE
Definition: tls13_misc.h:92
Tls13PlaintextSessionState
Definition: tls13_misc.h:352
#define TLS13_TICKET_AGE_TOLERANCE
Definition: tls13_misc.h:106
error_t tls13SaveSessionTicket(const TlsContext *context, TlsSessionState *session)
Save session ticket.
Definition: tls13_ticket.c:89
error_t tls13RestoreSessionTicket(TlsContext *context, const TlsSessionState *session)
Restore a TLS session using session ticket.
Definition: tls13_ticket.c:187
bool_t tls13IsTicketValid(TlsContext *context)
Check whether a session ticket is valid.
Definition: tls13_ticket.c:51
error_t tls13VerifyTicket(TlsContext *context, const uint8_t *ticket, size_t length, uint32_t obfuscatedTicketAge)
Session ticket verification.
Definition: tls13_ticket.c:377
error_t tls13GenerateTicket(TlsContext *context, const Tls13NewSessionTicket *message, uint8_t *ticket, size_t *length)
Session ticket generation.
Definition: tls13_ticket.c:306
TLS 1.3 session tickets.
TLS (Transport Layer Security)
#define tlsAllocMem(size)
Definition: tls.h:846
#define tlsFreeMem(p)
Definition: tls.h:851
uint8_t ticket[]
Definition: tls.h:1827
#define TLS_MAX_HKDF_DIGEST_SIZE
Definition: tls.h:906
@ TLS_HASH_ALGO_SHA384
Definition: tls.h:1201
@ TLS_HASH_ALGO_NONE
Definition: tls.h:1196
@ TLS_HASH_ALGO_SHA256
Definition: tls.h:1200
@ TLS_HASH_ALGO_SM3
Definition: tls.h:1204
#define TLS_VERSION_1_3
Definition: tls.h:97
#define TlsContext
Definition: tls.h:36
@ TLS_CONNECTION_END_CLIENT
Definition: tls.h:953
error_t tlsSelectCipherSuite(TlsContext *context, uint16_t identifier)
Set cipher suite.
Definition: tls_misc.c:333
const HashAlgo * tlsGetHashAlgo(TlsHashAlgo hashAlgoId)
Get the hash algorithm that matches the specified identifier.
Definition: tls_misc.c:1173
TLS helper functions.