tls13_key_material.c
Go to the documentation of this file.
1 /**
2  * @file tls13_key_material.c
3  * @brief TLS 1.3 key schedule
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 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.6.2
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL TLS_TRACE_LEVEL
33 
34 //Dependencies
35 #include "tls/tls.h"
36 #include "tls/tls_key_material.h"
38 #include "tls/tls_misc.h"
40 #include "tls13/tls13_ticket.h"
41 #include "quic/tls_quic_misc.h"
42 #include "kdf/hkdf.h"
43 #include "debug.h"
44 
45 //Check TLS library configuration
46 #if (TLS_SUPPORT == ENABLED && TLS_MAX_VERSION >= TLS_VERSION_1_3)
47 
48 
49 /**
50  * @brief HKDF-Expand-Label function
51  * @param[in] transportProtocol Transport protocol (TLS or DTLS)
52  * @param[in] hash Hash function used by HKDF
53  * @param[in] secret Pointer to the secret
54  * @param[in] secretLen Length of the secret
55  * @param[in] label Identifying label (NULL-terminated string)
56  * @param[in] context Pointer to the upper-layer context
57  * @param[in] contextLen Length of the upper-layer context
58  * @param[out] output Pointer to the output
59  * @param[in] outputLen Desired output length
60  * @return Error code
61  **/
62 
64  const HashAlgo *hash, const uint8_t *secret, size_t secretLen,
65  const char_t *label, const uint8_t *context, size_t contextLen,
66  uint8_t *output, size_t outputLen)
67 {
68  error_t error;
69  size_t n;
70  size_t labelLen;
71  uint8_t *hkdfLabel;
72  const char_t *prefix;
73 
74  //Check parameters
75  if(label == NULL)
77  if(context == NULL && contextLen != 0)
79 
80  //Retrieve the length of the label
81  labelLen = osStrlen(label);
82 
83  //Check parameters
84  if(labelLen > (255 - 6) || contextLen > 255)
85  return ERROR_INVALID_LENGTH;
86 
87  //Compute the length of the HkdfLabel structure
88  n = labelLen + contextLen + 10;
89  //Allocate a memory buffer to hold the HkdfLabel structure
90  hkdfLabel = tlsAllocMem(n);
91 
92  //Successful memory allocation?
93  if(hkdfLabel != NULL)
94  {
95 #if (DTLS_SUPPORT == ENABLED)
96  //DTLS protocol?
97  if(transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM)
98  {
99  //For DTLS 1.3, the label prefix shall be "dtls13". This ensures key
100  //separation between DTLS 1.3 and TLS 1.3. Note that there is no
101  //trailing space (refer to RFC 9147, section 5.9)
102  prefix = "dtls13";
103  }
104  else
105 #endif
106  //TLS protocol?
107  {
108  //For TLS 1.3, the label prefix shall be "tls13 " (refer to RFC 8446,
109  //section 7.1)
110  prefix = "tls13 ";
111  }
112 
113  //Format the HkdfLabel structure
114  hkdfLabel[0] = MSB(outputLen);
115  hkdfLabel[1] = LSB(outputLen);
116  hkdfLabel[2] = (uint8_t) (labelLen + 6);
117  osMemcpy(hkdfLabel + 3, prefix, 6);
118  osMemcpy(hkdfLabel + 9, label, labelLen);
119  hkdfLabel[labelLen + 9] = (uint8_t) contextLen;
120  osMemcpy(hkdfLabel + labelLen + 10, context, contextLen);
121 
122  //Debug message
123  TRACE_DEBUG("HkdfLabel (%" PRIuSIZE " bytes):\r\n", n);
124  TRACE_DEBUG_ARRAY(" ", hkdfLabel, n);
125 
126  //Compute HKDF-Expand(Secret, HkdfLabel, Length)
127  error = hkdfExpand(hash, secret, secretLen, hkdfLabel, n, output,
128  outputLen);
129 
130  //Release previously allocated memory
131  tlsFreeMem(hkdfLabel);
132  }
133  else
134  {
135  //Failed to allocate memory
136  error = ERROR_OUT_OF_MEMORY;
137  }
138 
139  //Return status code
140  return error;
141 }
142 
143 
144 /**
145  * @brief Derive-Secret function
146  * @param[in] context Pointer to the TLS context
147  * @param[in] secret Pointer to the secret
148  * @param[in] secretLen Length of the secret
149  * @param[in] label Identifying label (NULL-terminated string)
150  * @param[in] message Concatenation of the indicated handshake messages
151  * @param[in] messageLen Length of the indicated handshake messages
152  * @param[out] output Pointer to the output
153  * @param[in] outputLen Desired output length
154  * @return Error code
155  **/
156 
157 error_t tls13DeriveSecret(TlsContext *context, const uint8_t *secret,
158  size_t secretLen, const char_t *label, const char_t *message,
159  size_t messageLen, uint8_t *output, size_t outputLen)
160 {
161  error_t error;
162  const HashAlgo *hash;
163  uint8_t digest[TLS_MAX_HKDF_DIGEST_SIZE];
164 
165  //The hash function used by HKDF is the cipher suite hash algorithm
166  hash = context->cipherSuite.prfHashAlgo;
167 
168  //Make sure the hash algorithm is valid
169  if(hash != NULL)
170  {
171  //Any handshake messages specified?
172  if(message != NULL)
173  {
174  //Compute Transcript-Hash(Messages);
175  error = hash->compute(message, messageLen, digest);
176  }
177  else
178  {
179  //Implementations can implement the transcript by keeping a running
180  //transcript hash value based on the negotiated hash
181  error = tlsFinalizeTranscriptHash(context, hash,
182  context->transcriptHashContext, digest);
183  }
184 
185  //Debug message
186  TRACE_DEBUG("Transcript hash (%" PRIuSIZE " bytes):\r\n", hash->digestSize);
187  TRACE_DEBUG_ARRAY(" ", digest, hash->digestSize);
188 
189  //Check status code
190  if(!error)
191  {
192  //Compute HKDF-Expand-Label(Secret, Label, Transcript-Hash, Hash.length)
193  error = tls13HkdfExpandLabel(context->transportProtocol, hash, secret,
194  secretLen, label, digest, hash->digestSize, output, outputLen);
195  }
196  }
197  else
198  {
199  //Invalid HKDF hash algorithm
200  error = ERROR_FAILURE;
201  }
202 
203  //Return status code
204  return error;
205 }
206 
207 
208 /**
209  * @brief Compute early traffic keys
210  * @param[in] context Pointer to the TLS context
211  * @return Error code
212  **/
213 
215 {
216  error_t error;
217  size_t ikmLen;
218  const uint8_t *ikm;
219  const HashAlgo *hash;
220 
221  //The hash function used by HKDF is the cipher suite hash algorithm
222  hash = context->cipherSuite.prfHashAlgo;
223  //Make sure the hash algorithm is valid
224  if(hash == NULL)
225  return ERROR_FAILURE;
226 
227  //Although PSKs can be established out of band, PSKs can also be established
228  //in a previous connection
229  if(tls13IsPskValid(context))
230  {
231  //IKM is a pre-shared key established externally
232  ikm = context->psk;
233  ikmLen = context->pskLen;
234  }
235  else if(tls13IsTicketValid(context))
236  {
237  //IKM is a pre-shared key derived from the resumption master secret from
238  //a previous connection
239  ikm = context->ticketPsk;
240  ikmLen = context->ticketPskLen;
241  }
242  else
243  {
244  //The pre-shared key is not valid
245  return ERROR_FAILURE;
246  }
247 
248  //Calculate early secret
249  error = hkdfExtract(hash, ikm, ikmLen, NULL, 0, context->secret);
250  //Any error to report?
251  if(error)
252  return error;
253 
254  //Debug message
255  TRACE_DEBUG("Early secret:\r\n");
256  TRACE_DEBUG_ARRAY(" ", context->secret, hash->digestSize);
257 
258  //Calculate client early traffic secret
259  error = tls13DeriveSecret(context, context->secret, hash->digestSize,
260  "c e traffic", NULL, 0, context->clientEarlyTrafficSecret,
261  hash->digestSize);
262  //Any error to report?
263  if(error)
264  return error;
265 
266  //Debug message
267  TRACE_DEBUG("Client early secret:\r\n");
268  TRACE_DEBUG_ARRAY(" ", context->clientEarlyTrafficSecret, hash->digestSize);
269 
270  //The traffic keying material is generated from the traffic secret value
271  if(context->entity == TLS_CONNECTION_END_CLIENT)
272  {
273  //Calculate client early traffic keys
274  error = tlsUpdateEncryptionEngine(context, context->encryptionEngine,
276  context->clientEarlyTrafficSecret);
277  }
278  else
279  {
280  //The implementation must verify that its receive buffer is empty
281  if(context->rxBufferLen == 0)
282  {
283  //Calculate client early traffic keys
284  error = tlsUpdateEncryptionEngine(context, context->decryptionEngine,
286  context->clientEarlyTrafficSecret);
287  }
288  else
289  {
290  //The receive buffer is not empty
291  error = ERROR_UNEXPECTED_MESSAGE;
292  }
293  }
294 
295  //Failed to generate traffic keying material?
296  if(error)
297  return error;
298 
299  //Calculate early exporter master secret
300  error = tls13DeriveSecret(context, context->secret, hash->digestSize,
301  "e exp master", NULL, 0, context->exporterMasterSecret, hash->digestSize);
302  //Any error to report?
303  if(error)
304  return error;
305 
306  //Debug message
307  TRACE_DEBUG("Early exporter master secret:\r\n");
308  TRACE_DEBUG_ARRAY(" ", context->exporterMasterSecret, hash->digestSize);
309 
310 #if (TLS_QUIC_SUPPORT == ENABLED)
311  //After providing a QUIC client with the first handshake bytes, the TLS stack
312  //might signal the change to 0-RTT keys (refer to RFC 9001, section 4.1.4)
314  context->clientEarlyTrafficSecret, NULL, hash->digestSize);
315  //Any error to report?
316  if(error)
317  return error;
318 #endif
319 
320 #if (TLS_KEY_LOG_SUPPORT == ENABLED)
321  //Log client early traffic secret
322  tlsDumpSecret(context, "CLIENT_EARLY_TRAFFIC_SECRET",
323  context->clientEarlyTrafficSecret, hash->digestSize);
324 
325  //Log early exporter master secret
326  tlsDumpSecret(context, "EARLY_EXPORTER_SECRET",
327  context->exporterMasterSecret, hash->digestSize);
328 #endif
329 
330  //When a PSK is used and early data is allowed for that PSK, the client can
331  //send application data in its first flight of messages
333 
334  //Successful processing
335  return NO_ERROR;
336 }
337 
338 
339 /**
340  * @brief Compute handshake traffic keys
341  * @param[in] context Pointer to the TLS context
342  * @return Error code
343  **/
344 
346 {
347  error_t error;
348  size_t ikmLen;
349  const uint8_t *ikm;
350  const HashAlgo *hash;
351 
352  //The hash function used by HKDF is the cipher suite hash algorithm
353  hash = context->cipherSuite.prfHashAlgo;
354  //Make sure the hash algorithm is valid
355  if(hash == NULL)
356  return ERROR_FAILURE;
357 
358 #if (TLS13_DHE_KE_SUPPORT == ENABLED || TLS13_ECDHE_KE_SUPPORT == ENABLED || \
359  TLS13_MLKEM_KE_SUPPORT == ENABLED || TLS13_HYBRID_KE_SUPPORT == ENABLED)
360  //(EC)DHE key exchange method?
361  if(context->keyExchMethod == TLS13_KEY_EXCH_DHE ||
362  context->keyExchMethod == TLS13_KEY_EXCH_ECDHE ||
363  context->keyExchMethod == TLS13_KEY_EXCH_MLKEM ||
364  context->keyExchMethod == TLS13_KEY_EXCH_HYBRID)
365  {
366  //If PSK is not in use, IKM is a string of Hash-lengths bytes set to 0
367  osMemset(context->secret, 0, hash->digestSize);
368 
369  //Point to the IKM argument
370  ikm = context->secret;
371  ikmLen = hash->digestSize;
372  }
373  else
374 #endif
375 #if (TLS13_PSK_KE_SUPPORT == ENABLED || TLS13_PSK_DHE_KE_SUPPORT == ENABLED || \
376  TLS13_PSK_ECDHE_KE_SUPPORT == ENABLED || TLS13_PSK_MLKEM_KE_SUPPORT == ENABLED || \
377  TLS13_PSK_HYBRID_KE_SUPPORT == ENABLED)
378  //PSK-only or PSK with (EC)DHE key exchange method?
379  if(context->keyExchMethod == TLS13_KEY_EXCH_PSK ||
380  context->keyExchMethod == TLS13_KEY_EXCH_PSK_DHE ||
381  context->keyExchMethod == TLS13_KEY_EXCH_PSK_ECDHE ||
382  context->keyExchMethod == TLS13_KEY_EXCH_PSK_MLKEM ||
383  context->keyExchMethod == TLS13_KEY_EXCH_PSK_HYBRID)
384  {
385  //Although PSKs can be established out of band, PSKs can also be
386  //established in a previous connection
387  if(tls13IsPskValid(context))
388  {
389  //IKM is a pre-shared key established externally
390  ikm = context->psk;
391  ikmLen = context->pskLen;
392  }
393  else if(tls13IsTicketValid(context))
394  {
395  //IKM is a pre-shared key derived from the resumption master secret
396  //from a previous connection
397  ikm = context->ticketPsk;
398  ikmLen = context->ticketPskLen;
399  }
400  else
401  {
402  //The pre-shared key is not valid
403  return ERROR_FAILURE;
404  }
405  }
406  else
407 #endif
408  //Invalid key exchange method?
409  {
410  //Report an error
411  return ERROR_FAILURE;
412  }
413 
414  //Calculate early secret
415  error = hkdfExtract(hash, ikm, ikmLen, NULL, 0, context->secret);
416  //Any error to report?
417  if(error)
418  return error;
419 
420  //Debug message
421  TRACE_DEBUG("Early secret:\r\n");
422  TRACE_DEBUG_ARRAY(" ", context->secret, hash->digestSize);
423 
424  //Derive early secret
425  error = tls13DeriveSecret(context, context->secret, hash->digestSize,
426  "derived", "", 0, context->secret, hash->digestSize);
427  //Any error to report?
428  if(error)
429  return error;
430 
431  //Debug message
432  TRACE_DEBUG("Derived secret:\r\n");
433  TRACE_DEBUG_ARRAY(" ", context->secret, hash->digestSize);
434 
435  //PSK-only key exchange method?
436  if(context->keyExchMethod == TLS13_KEY_EXCH_PSK)
437  {
438  //If the (EC)DHE shared secret is not available, then the 0-value
439  //consisting of a string of Hash.length bytes set to zeros is used
440  osMemset(context->premasterSecret, 0, hash->digestSize);
441  context->premasterSecretLen = hash->digestSize;
442  }
443 
444  //Calculate handshake secret
445  error = hkdfExtract(hash, context->premasterSecret,
446  context->premasterSecretLen, context->secret, hash->digestSize,
447  context->secret);
448  //Any error to report?
449  if(error)
450  return error;
451 
452  //Debug message
453  TRACE_DEBUG("Handshake secret:\r\n");
454  TRACE_DEBUG_ARRAY(" ", context->secret, hash->digestSize);
455 
456  //Calculate client handshake traffic secret
457  error = tls13DeriveSecret(context, context->secret, hash->digestSize,
458  "c hs traffic", NULL, 0, context->clientHsTrafficSecret,
459  hash->digestSize);
460  //Any error to report?
461  if(error)
462  return error;
463 
464  //Debug message
465  TRACE_DEBUG("Client handshake secret:\r\n");
466  TRACE_DEBUG_ARRAY(" ", context->clientHsTrafficSecret, hash->digestSize);
467 
468  //Calculate server handshake traffic secret
469  error = tls13DeriveSecret(context, context->secret, hash->digestSize,
470  "s hs traffic", NULL, 0, context->serverHsTrafficSecret,
471  hash->digestSize);
472  //Any error to report?
473  if(error)
474  return error;
475 
476  //Debug message
477  TRACE_DEBUG("Server handshake secret:\r\n");
478  TRACE_DEBUG_ARRAY(" ", context->serverHsTrafficSecret, hash->digestSize);
479 
480  //The implementation must verify that its receive buffer is empty before
481  //switching to encrypted handshake
482  if(context->rxBufferLen != 0)
483  return ERROR_HANDSHAKE_FAILED;
484 
485  //The traffic keying material is generated from the traffic secret value
486  if(context->entity == TLS_CONNECTION_END_CLIENT)
487  {
488  //Calculate client handshake traffic keys
489  error = tlsUpdateEncryptionEngine(context, context->encryptionEngine,
491  context->clientHsTrafficSecret);
492 
493  //Check status code
494  if(!error)
495  {
496  //Calculate server handshake traffic keys
497  error = tlsUpdateEncryptionEngine(context, context->decryptionEngine,
499  context->serverHsTrafficSecret);
500  }
501  }
502  else
503  {
504  //Calculate client handshake traffic keys
505  error = tlsUpdateEncryptionEngine(context, context->decryptionEngine,
507  context->clientHsTrafficSecret);
508 
509  //Check status code
510  if(!error)
511  {
512  //Calculate server handshake traffic keys
513  error = tlsUpdateEncryptionEngine(context, context->encryptionEngine,
515  context->serverHsTrafficSecret);
516  }
517  }
518 
519  //Failed to generate traffic keying material?
520  if(error)
521  return error;
522 
523 #if (TLS_QUIC_SUPPORT == ENABLED)
524  //As keys at a given encryption level become available to TLS, TLS indicates
525  //to QUIC that reading or writing keys at that encryption level are available
526  //(refer to RFC 9001, section 4.1.4)
528  context->clientHsTrafficSecret, context->serverHsTrafficSecret,
529  hash->digestSize);
530  //Any error to report?
531  if(error)
532  return error;
533 #endif
534 
535 #if (TLS_KEY_LOG_SUPPORT == ENABLED)
536  //Log client handshake traffic secret
537  tlsDumpSecret(context, "CLIENT_HANDSHAKE_TRAFFIC_SECRET",
538  context->clientHsTrafficSecret, hash->digestSize);
539 
540  //Log server handshake traffic secret
541  tlsDumpSecret(context, "SERVER_HANDSHAKE_TRAFFIC_SECRET",
542  context->serverHsTrafficSecret, hash->digestSize);
543 #endif
544 
545  //In all handshakes, the server must send the EncryptedExtensions message
546  //immediately after the ServerHello message
548 
549  //Successful processing
550  return NO_ERROR;
551 }
552 
553 
554 /**
555  * @brief Compute server application traffic keys
556  * @param[in] context Pointer to the TLS context
557  * @return Error code
558  **/
559 
561 {
562  error_t error;
563  const HashAlgo *hash;
564  uint8_t ikm[TLS_MAX_HKDF_DIGEST_SIZE];
565 
566  //The hash function used by HKDF is the cipher suite hash algorithm
567  hash = context->cipherSuite.prfHashAlgo;
568  //Make sure the hash algorithm is valid
569  if(hash == NULL)
570  return ERROR_FAILURE;
571 
572  //Derive handshake secret
573  error = tls13DeriveSecret(context, context->secret, hash->digestSize,
574  "derived", "", 0, context->secret, hash->digestSize);
575  //Any error to report?
576  if(error)
577  return error;
578 
579  //Debug message
580  TRACE_DEBUG("Derived secret:\r\n");
581  TRACE_DEBUG_ARRAY(" ", context->secret, hash->digestSize);
582 
583  //IKM is a string of Hash-lengths bytes set to 0
584  osMemset(ikm, 0, hash->digestSize);
585 
586  //Calculate master secret
587  error = hkdfExtract(hash, ikm, hash->digestSize, context->secret,
588  hash->digestSize, context->secret);
589  //Any error to report?
590  if(error)
591  return error;
592 
593  //Debug message
594  TRACE_DEBUG("Master secret:\r\n");
595  TRACE_DEBUG_ARRAY(" ", context->secret, hash->digestSize);
596 
597  //Calculate client application traffic secret
598  error = tls13DeriveSecret(context, context->secret, hash->digestSize,
599  "c ap traffic", NULL, 0, context->clientAppTrafficSecret,
600  hash->digestSize);
601  //Any error to report?
602  if(error)
603  return error;
604 
605  //Debug message
606  TRACE_DEBUG("Client application secret:\r\n");
607  TRACE_DEBUG_ARRAY(" ", context->clientAppTrafficSecret, hash->digestSize);
608 
609  //Calculate server application traffic secret
610  error = tls13DeriveSecret(context, context->secret, hash->digestSize,
611  "s ap traffic", NULL, 0, context->serverAppTrafficSecret,
612  hash->digestSize);
613  //Any error to report?
614  if(error)
615  return error;
616 
617  //Debug message
618  TRACE_DEBUG("Server application secret:\r\n");
619  TRACE_DEBUG_ARRAY(" ", context->serverAppTrafficSecret, hash->digestSize);
620 
621  //All the traffic keying material is recomputed when changing from the
622  //handshake to application data keys
623  if(context->entity == TLS_CONNECTION_END_CLIENT)
624  {
625  //The implementation must verify that its receive buffer is empty before
626  //rekeying
627  if(context->rxBufferLen == 0)
628  {
629  //Inform the record layer that subsequent records will be protected
630  //under the new traffic keys
631  error = tlsUpdateEncryptionEngine(context, context->decryptionEngine,
633  context->serverAppTrafficSecret);
634  }
635  else
636  {
637  //The receive buffer is not empty
638  error = ERROR_UNEXPECTED_MESSAGE;
639  }
640  }
641  else
642  {
643  //Inform the record layer that subsequent records will be protected
644  //under the new traffic keys
645  error = tlsUpdateEncryptionEngine(context, context->encryptionEngine,
647  context->serverAppTrafficSecret);
648  }
649 
650  //Failed to generate traffic keying material?
651  if(error)
652  return error;
653 
654  //Calculate exporter master secret
655  error = tls13DeriveSecret(context, context->secret, hash->digestSize,
656  "exp master", NULL, 0, context->exporterMasterSecret, hash->digestSize);
657  //Any error to report?
658  if(error)
659  return error;
660 
661  //Debug message
662  TRACE_DEBUG("Exporter master secret:\r\n");
663  TRACE_DEBUG_ARRAY(" ", context->exporterMasterSecret, hash->digestSize);
664 
665 #if (TLS_QUIC_SUPPORT == ENABLED)
666  //As keys at a given encryption level become available to TLS, TLS indicates
667  //to QUIC that reading or writing keys at that encryption level are available
668  //(refer to RFC 9001, section 4.1.4)
670  context->clientAppTrafficSecret, context->serverAppTrafficSecret,
671  hash->digestSize);
672  //Any error to report?
673  if(error)
674  return error;
675 #endif
676 
677 #if (TLS_KEY_LOG_SUPPORT == ENABLED)
678  //Log client application traffic secret
679  tlsDumpSecret(context, "CLIENT_TRAFFIC_SECRET_0",
680  context->clientAppTrafficSecret, hash->digestSize);
681 
682  //Log server application traffic secret
683  tlsDumpSecret(context, "SERVER_TRAFFIC_SECRET_0",
684  context->serverAppTrafficSecret, hash->digestSize);
685 
686  //Log exporter master secret
687  tlsDumpSecret(context, "EXPORTER_SECRET",
688  context->exporterMasterSecret, hash->digestSize);
689 #endif
690 
691  //Check whether TLS operates as a client or a server
692  if(context->entity == TLS_CONNECTION_END_CLIENT)
693  {
694  //In DTLS 1.3, the EndOfEarlyData message is omitted both from the wire
695  //and the handshake transcript. Because DTLS records have epochs,
696  //EndOfEarlyData is not necessary to determine when the early data is
697  //complete (refer to RFC 9147, section 5.6)
698  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_STREAM &&
699  context->earlyDataEnabled && context->earlyDataExtReceived)
700  {
701  //If the server sent an EarlyData extension, the client must send an
702  //EndOfEarlyData message after receiving the server Finished
704  }
705  else
706  {
707  //PSK key exchange method?
708  if(context->keyExchMethod == TLS13_KEY_EXCH_PSK ||
709  context->keyExchMethod == TLS13_KEY_EXCH_PSK_DHE ||
710  context->keyExchMethod == TLS13_KEY_EXCH_PSK_ECDHE ||
711  context->keyExchMethod == TLS13_KEY_EXCH_PSK_HYBRID)
712  {
713  //Send a Finished message to the server
715  }
716  else
717  {
718  //Send a Certificate message if the server requests it
720  }
721  }
722  }
723  else
724  {
725  //PSK key exchange method?
726  if(context->keyExchMethod == TLS13_KEY_EXCH_PSK ||
727  context->keyExchMethod == TLS13_KEY_EXCH_PSK_DHE ||
728  context->keyExchMethod == TLS13_KEY_EXCH_PSK_ECDHE ||
729  context->keyExchMethod == TLS13_KEY_EXCH_PSK_HYBRID)
730  {
731  //Wait for a Finished message from the client
733  }
734  else
735  {
736  //The client must send a Certificate message if the server requests it
737  if(context->clientAuthMode != TLS_CLIENT_AUTH_NONE)
738  {
740  }
741  else
742  {
744  }
745  }
746  }
747 
748  //Successful processing
749  return NO_ERROR;
750 }
751 
752 
753 /**
754  * @brief Compute client application traffic keys
755  * @param[in] context Pointer to the TLS context
756  * @return Error code
757  **/
758 
760 {
761  error_t error;
762  const HashAlgo *hash;
763 
764  //The hash function used by HKDF is the cipher suite hash algorithm
765  hash = context->cipherSuite.prfHashAlgo;
766  //Make sure the hash algorithm is valid
767  if(hash == NULL)
768  return ERROR_FAILURE;
769 
770  //At this point, the handshake is complete, and the client and server
771  //derive the keying material required by the record layer to exchange
772  //application-layer data protected through authenticated encryption
773  if(context->entity == TLS_CONNECTION_END_CLIENT)
774  {
775  //Inform the record layer that subsequent records will be protected
776  //under the new traffic keys
777  error = tlsUpdateEncryptionEngine(context, context->encryptionEngine,
779  context->clientAppTrafficSecret);
780  }
781  else
782  {
783  //The implementation must verify that its receive buffer is empty before
784  //rekeying
785  if(context->rxBufferLen == 0)
786  {
787  //Inform the record layer that subsequent records will be protected
788  //under the new traffic keys
789  error = tlsUpdateEncryptionEngine(context, context->decryptionEngine,
791  context->clientAppTrafficSecret);
792  }
793  else
794  {
795  //The receive buffer is not empty
796  error = ERROR_UNEXPECTED_MESSAGE;
797  }
798  }
799 
800  //Failed to generate traffic keying material?
801  if(error)
802  return error;
803 
804  //Calculate resumption master secret
805  error = tls13DeriveSecret(context, context->secret, hash->digestSize,
806  "res master", NULL, 0, context->resumptionMasterSecret, hash->digestSize);
807  //Any error to report?
808  if(error)
809  return error;
810 
811  //Debug message
812  TRACE_DEBUG("Resumption master secret:\r\n");
813  TRACE_DEBUG_ARRAY(" ", context->resumptionMasterSecret, hash->digestSize);
814 
815  //Once all the values which are to be derived from a given secret have been
816  //computed, that secret should be erased
817  osMemset(context->secret, 0, TLS13_MAX_HKDF_DIGEST_SIZE);
818  osMemset(context->clientEarlyTrafficSecret, 0, TLS13_MAX_HKDF_DIGEST_SIZE);
819  osMemset(context->clientHsTrafficSecret, 0, TLS13_MAX_HKDF_DIGEST_SIZE);
820  osMemset(context->serverHsTrafficSecret, 0, TLS13_MAX_HKDF_DIGEST_SIZE);
821 
822  //DTLS protocol?
823  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM)
824  {
825  //When a handshake flight is sent without any expected response, as is
826  //the case with the client's final flight, the flight must be
827  //acknowledged with an ACK message (refer to RFC 9147, section 5.7)
828  if(context->entity == TLS_CONNECTION_END_CLIENT)
829  {
831  }
832  else
833  {
835  }
836  }
837  else
838  {
839 #if (TLS_TICKET_SUPPORT == ENABLED)
840  //Check whether session ticket mechanism is enabled at server side
841  if(context->entity == TLS_CONNECTION_END_SERVER &&
842  context->ticketEncryptCallback != NULL &&
843  context->pskKeModeSupported)
844  {
845  //At any time after the server has received the client Finished
846  //message, it may send a NewSessionTicket message
848  }
849  else
850 #endif
851  {
852  //The client and server can now exchange application-layer data
854  }
855  }
856 
857  //Successful processing
858  return NO_ERROR;
859 }
860 
861 #endif
@ TLS13_KEY_EXCH_PSK
Definition: tls.h:1224
#define tlsAllocMem(size)
Definition: tls.h:889
error_t tls13GenerateEarlyTrafficKeys(TlsContext *context)
Compute early traffic keys.
TLS helper functions.
error_t tlsUpdateEncryptionEngine(TlsContext *context, TlsEncryptionEngine *encryptionEngine, TlsConnectionEnd entity, TlsEncryptionLevel level, const uint8_t *secret)
Update encryption engine.
Definition: tls_misc.c:1051
#define TLS13_MAX_HKDF_DIGEST_SIZE
Definition: tls13_misc.h:148
error_t tls13DeriveSecret(TlsContext *context, const uint8_t *secret, size_t secretLen, const char_t *label, const char_t *message, size_t messageLen, uint8_t *output, size_t outputLen)
Derive-Secret function.
@ TLS13_KEY_EXCH_MLKEM
Definition: tls.h:1222
error_t tls13GenerateHandshakeTrafficKeys(TlsContext *context)
Compute handshake traffic keys.
@ TLS13_KEY_EXCH_PSK_DHE
Definition: tls.h:1225
Key material generation.
@ ERROR_UNEXPECTED_MESSAGE
Definition: error.h:195
QUIC helper functions.
uint8_t message[]
Definition: chap.h:154
size_t digestSize
Definition: crypto.h:1157
TLS 1.3 session tickets.
@ TLS_TRANSPORT_PROTOCOL_DATAGRAM
Definition: tls.h:1017
void tlsDumpSecret(TlsContext *context, const char_t *label, const uint8_t *secret, size_t secretLen)
Dump secret key (for debugging purpose only)
@ TLS13_KEY_EXCH_PSK_MLKEM
Definition: tls.h:1227
@ TLS_STATE_APPLICATION_DATA
Definition: tls.h:1589
@ ERROR_HANDSHAKE_FAILED
Definition: error.h:234
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
@ TLS13_KEY_EXCH_ECDHE
Definition: tls.h:1221
#define osStrlen(s)
Definition: os_port.h:168
error_t tlsFinalizeTranscriptHash(TlsContext *context, const HashAlgo *hash, const void *hashContext, uint8_t *output)
Finalize hash calculation from previous handshake messages.
Ipv6Addr prefix
@ TLS_ENCRYPTION_LEVEL_EARLY_DATA
Definition: tls.h:1606
error_t tls13GenerateClientAppTrafficKeys(TlsContext *context)
Compute client application traffic keys.
@ TLS_ENCRYPTION_LEVEL_HANDSHAKE
Definition: tls.h:1607
@ TLS13_KEY_EXCH_PSK_HYBRID
Definition: tls.h:1228
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
#define osMemcpy(dest, src, length)
Definition: os_port.h:144
#define TlsContext
Definition: tls.h:36
error_t
Error codes.
Definition: error.h:43
HashAlgoCompute compute
Definition: crypto.h:1160
@ TLS_CONNECTION_END_SERVER
Definition: tls.h:1030
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
@ TLS13_KEY_EXCH_DHE
Definition: tls.h:1220
@ TLS_STATE_EARLY_DATA
Definition: tls.h:1561
@ ERROR_INVALID_LENGTH
Definition: error.h:111
error_t tlsSetQuicEncryptionKeys(TlsContext *context, TlsEncryptionLevel level, const uint8_t *clientKey, const uint8_t *serverKey, size_t keyLen)
Set encryption keys.
bool_t tls13IsTicketValid(TlsContext *context)
Check whether a session ticket is valid.
Definition: tls13_ticket.c:51
error_t tls13GenerateServerAppTrafficKeys(TlsContext *context)
Compute server application traffic keys.
#define MSB(x)
Definition: os_port.h:59
@ TLS_STATE_END_OF_EARLY_DATA
Definition: tls.h:1586
#define LSB(x)
Definition: os_port.h:55
error_t hkdfExpand(const HashAlgo *hash, const uint8_t *prk, size_t prkLen, const uint8_t *info, size_t infoLen, uint8_t *okm, size_t okmLen)
HKDF expand step.
Definition: hkdf.c:158
@ TLS_CLIENT_AUTH_NONE
Definition: tls.h:1040
@ TLS_STATE_NEW_SESSION_TICKET_2
Definition: tls.h:1582
Transcript hash calculation.
uint8_t secret[TLS_MASTER_SECRET_SIZE]
Master secret.
Definition: tls.h:2027
#define TRACE_DEBUG(...)
Definition: debug.h:119
char char_t
Definition: compiler_port.h:55
@ TLS13_KEY_EXCH_PSK_ECDHE
Definition: tls.h:1226
@ TLS_ENCRYPTION_LEVEL_APPLICATION
Definition: tls.h:1608
#define TRACE_DEBUG_ARRAY(p, a, n)
Definition: debug.h:120
bool_t tls13IsPskValid(TlsContext *context)
Check whether an externally established PSK is valid.
Definition: tls13_misc.c:909
uint8_t n
TlsTransportProtocol
TLS transport protocols.
Definition: tls.h:1015
HKDF (HMAC-based Key Derivation Function)
@ TLS_STATE_CLIENT_CERTIFICATE
Definition: tls.h:1574
@ TLS13_KEY_EXCH_HYBRID
Definition: tls.h:1223
@ TLS_STATE_ENCRYPTED_EXTENSIONS
Definition: tls.h:1568
@ TLS_STATE_CLIENT_FINISHED_ACK
Definition: tls.h:1590
#define TLS_MAX_HKDF_DIGEST_SIZE
Definition: tls.h:965
@ TLS_CONNECTION_END_CLIENT
Definition: tls.h:1029
TLS (Transport Layer Security)
@ TLS_STATE_FINAL_ACK
Definition: tls.h:1588
@ TLS_TRANSPORT_PROTOCOL_STREAM
Definition: tls.h:1016
TLS 1.3 key schedule.
Common interface for hash algorithms.
Definition: crypto.h:1151
error_t hkdfExtract(const HashAlgo *hash, const uint8_t *ikm, size_t ikmLen, const uint8_t *salt, size_t saltLen, uint8_t *prk)
HKDF extract step.
Definition: hkdf.c:97
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.
void tlsChangeState(TlsContext *context, TlsState newState)
Update TLS state.
Definition: tls_misc.c:54
#define PRIuSIZE
#define osMemset(p, value, length)
Definition: os_port.h:138
#define tlsFreeMem(p)
Definition: tls.h:894
@ TLS_STATE_CLIENT_FINISHED
Definition: tls.h:1579
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.