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