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.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_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  //(EC)DHE key exchange method?
347  if(context->keyExchMethod == TLS13_KEY_EXCH_DHE ||
348  context->keyExchMethod == TLS13_KEY_EXCH_ECDHE)
349  {
350  //If PSK is not in use, IKM is a string of Hash-lengths bytes set to 0
351  osMemset(context->secret, 0, hash->digestSize);
352 
353  //Point to the IKM argument
354  ikm = context->secret;
355  ikmLen = hash->digestSize;
356  }
357  else
358 #endif
359 #if (TLS13_PSK_KE_SUPPORT == ENABLED || TLS13_PSK_DHE_KE_SUPPORT == ENABLED || \
360  TLS13_PSK_ECDHE_KE_SUPPORT == ENABLED)
361  //PSK-only or PSK with (EC)DHE key exchange method?
362  if(context->keyExchMethod == TLS13_KEY_EXCH_PSK ||
363  context->keyExchMethod == TLS13_KEY_EXCH_PSK_DHE ||
364  context->keyExchMethod == TLS13_KEY_EXCH_PSK_ECDHE)
365  {
366  //Although PSKs can be established out of band, PSKs can also be
367  //established in a previous connection
368  if(tls13IsPskValid(context))
369  {
370  //IKM is a pre-shared key established externally
371  ikm = context->psk;
372  ikmLen = context->pskLen;
373  }
374  else if(tls13IsTicketValid(context))
375  {
376  //IKM is a pre-shared key derived from the resumption master secret
377  //from a previous connection
378  ikm = context->ticketPsk;
379  ikmLen = context->ticketPskLen;
380  }
381  else
382  {
383  //The pre-shared key is not valid
384  return ERROR_FAILURE;
385  }
386  }
387  else
388 #endif
389  //Invalid key exchange method?
390  {
391  //Report an error
392  return ERROR_FAILURE;
393  }
394 
395  //Calculate early secret
396  error = hkdfExtract(hash, ikm, ikmLen, NULL, 0, context->secret);
397  //Any error to report?
398  if(error)
399  return error;
400 
401  //Debug message
402  TRACE_DEBUG("Early secret:\r\n");
403  TRACE_DEBUG_ARRAY(" ", context->secret, hash->digestSize);
404 
405  //Derive early secret
406  error = tls13DeriveSecret(context, context->secret, hash->digestSize,
407  "derived", "", 0, context->secret, hash->digestSize);
408  //Any error to report?
409  if(error)
410  return error;
411 
412  //Debug message
413  TRACE_DEBUG("Derived secret:\r\n");
414  TRACE_DEBUG_ARRAY(" ", context->secret, hash->digestSize);
415 
416  //PSK-only key exchange method?
417  if(context->keyExchMethod == TLS13_KEY_EXCH_PSK)
418  {
419  //If the (EC)DHE shared secret is not available, then the 0-value
420  //consisting of a string of Hash.length bytes set to zeros is used
421  osMemset(context->premasterSecret, 0, hash->digestSize);
422  context->premasterSecretLen = hash->digestSize;
423  }
424 
425  //Calculate handshake secret
426  error = hkdfExtract(hash, context->premasterSecret,
427  context->premasterSecretLen, context->secret, hash->digestSize,
428  context->secret);
429  //Any error to report?
430  if(error)
431  return error;
432 
433  //Debug message
434  TRACE_DEBUG("Handshake secret:\r\n");
435  TRACE_DEBUG_ARRAY(" ", context->secret, hash->digestSize);
436 
437  //Calculate client handshake traffic secret
438  error = tls13DeriveSecret(context, context->secret, hash->digestSize,
439  "c hs traffic", NULL, 0, context->clientHsTrafficSecret,
440  hash->digestSize);
441  //Any error to report?
442  if(error)
443  return error;
444 
445  //Debug message
446  TRACE_DEBUG("Client handshake secret:\r\n");
447  TRACE_DEBUG_ARRAY(" ", context->clientHsTrafficSecret, hash->digestSize);
448 
449  //Calculate server handshake traffic secret
450  error = tls13DeriveSecret(context, context->secret, hash->digestSize,
451  "s hs traffic", NULL, 0, context->serverHsTrafficSecret,
452  hash->digestSize);
453  //Any error to report?
454  if(error)
455  return error;
456 
457  //Debug message
458  TRACE_DEBUG("Server handshake secret:\r\n");
459  TRACE_DEBUG_ARRAY(" ", context->serverHsTrafficSecret, hash->digestSize);
460 
461  //The implementation must verify that its receive buffer is empty before
462  //switching to encrypted handshake
463  if(context->rxBufferLen != 0)
464  return ERROR_HANDSHAKE_FAILED;
465 
466  //The traffic keying material is generated from the traffic secret value
467  if(context->entity == TLS_CONNECTION_END_CLIENT)
468  {
469  //Release encryption engine
470  tlsFreeEncryptionEngine(&context->encryptionEngine);
471 
472  //Calculate client handshake traffic keys
473  error = tlsInitEncryptionEngine(context, &context->encryptionEngine,
474  TLS_CONNECTION_END_CLIENT, context->clientHsTrafficSecret);
475 
476  //Check status code
477  if(!error)
478  {
479  //Calculate server handshake traffic keys
480  error = tlsInitEncryptionEngine(context, &context->decryptionEngine,
481  TLS_CONNECTION_END_SERVER, context->serverHsTrafficSecret);
482  }
483  }
484  else
485  {
486  //Release decryption engine
487  tlsFreeEncryptionEngine(&context->decryptionEngine);
488 
489  //Calculate client handshake traffic keys
490  error = tlsInitEncryptionEngine(context, &context->decryptionEngine,
491  TLS_CONNECTION_END_CLIENT, context->clientHsTrafficSecret);
492 
493  //Check status code
494  if(!error)
495  {
496  //Calculate server handshake traffic keys
497  error = tlsInitEncryptionEngine(context, &context->encryptionEngine,
498  TLS_CONNECTION_END_SERVER, context->serverHsTrafficSecret);
499  }
500  }
501 
502  //Failed to generate traffic keying material?
503  if(error)
504  return error;
505 
506 #if (TLS_KEY_LOG_SUPPORT == ENABLED)
507  //Log client handshake traffic secret
508  tlsDumpSecret(context, "CLIENT_HANDSHAKE_TRAFFIC_SECRET",
509  context->clientHsTrafficSecret, hash->digestSize);
510 
511  //Log server handshake traffic secret
512  tlsDumpSecret(context, "SERVER_HANDSHAKE_TRAFFIC_SECRET",
513  context->serverHsTrafficSecret, hash->digestSize);
514 #endif
515 
516 #if (DTLS_SUPPORT == ENABLED)
517  //DTLS protocol?
518  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM)
519  {
520  //Because each epoch resets the sequence number space, a separate sliding
521  //window is needed for each epoch (refer to RFC 9147, section 4.5.1)
522  dtlsInitReplayWindow(context);
523  }
524 #endif
525 
526  //In all handshakes, the server must send the EncryptedExtensions message
527  //immediately after the ServerHello message
529 
530  //Successful processing
531  return NO_ERROR;
532 }
533 
534 
535 /**
536  * @brief Compute server application traffic keys
537  * @param[in] context Pointer to the TLS context
538  * @return Error code
539  **/
540 
542 {
543  error_t error;
544  const HashAlgo *hash;
545  uint8_t ikm[TLS_MAX_HKDF_DIGEST_SIZE];
546 
547  //The hash function used by HKDF is the cipher suite hash algorithm
548  hash = context->cipherSuite.prfHashAlgo;
549  //Make sure the hash algorithm is valid
550  if(hash == NULL)
551  return ERROR_FAILURE;
552 
553  //Derive handshake secret
554  error = tls13DeriveSecret(context, context->secret, hash->digestSize,
555  "derived", "", 0, context->secret, hash->digestSize);
556  //Any error to report?
557  if(error)
558  return error;
559 
560  //Debug message
561  TRACE_DEBUG("Derived secret:\r\n");
562  TRACE_DEBUG_ARRAY(" ", context->secret, hash->digestSize);
563 
564  //IKM is a string of Hash-lengths bytes set to 0
565  osMemset(ikm, 0, hash->digestSize);
566 
567  //Calculate master secret
568  error = hkdfExtract(hash, ikm, hash->digestSize, context->secret,
569  hash->digestSize, context->secret);
570  //Any error to report?
571  if(error)
572  return error;
573 
574  //Debug message
575  TRACE_DEBUG("Master secret:\r\n");
576  TRACE_DEBUG_ARRAY(" ", context->secret, hash->digestSize);
577 
578  //Calculate client application traffic secret
579  error = tls13DeriveSecret(context, context->secret, hash->digestSize,
580  "c ap traffic", NULL, 0, context->clientAppTrafficSecret,
581  hash->digestSize);
582  //Any error to report?
583  if(error)
584  return error;
585 
586  //Debug message
587  TRACE_DEBUG("Client application secret:\r\n");
588  TRACE_DEBUG_ARRAY(" ", context->clientAppTrafficSecret, hash->digestSize);
589 
590  //Calculate server application traffic secret
591  error = tls13DeriveSecret(context, context->secret, hash->digestSize,
592  "s ap traffic", NULL, 0, context->serverAppTrafficSecret,
593  hash->digestSize);
594  //Any error to report?
595  if(error)
596  return error;
597 
598  //Debug message
599  TRACE_DEBUG("Server application secret:\r\n");
600  TRACE_DEBUG_ARRAY(" ", context->serverAppTrafficSecret, hash->digestSize);
601 
602  //All the traffic keying material is recomputed when changing from the
603  //handshake to application data keys
604  if(context->entity == TLS_CONNECTION_END_CLIENT)
605  {
606  //The implementation must verify that its receive buffer is empty before
607  //rekeying
608  if(context->rxBufferLen == 0)
609  {
610  //Release decryption engine
611  tlsFreeEncryptionEngine(&context->decryptionEngine);
612 
613  //Inform the record layer that subsequent records will be protected
614  //under the new traffic keys
615  error = tlsInitEncryptionEngine(context, &context->decryptionEngine,
616  TLS_CONNECTION_END_SERVER, context->serverAppTrafficSecret);
617  }
618  else
619  {
620  //The receive buffer is not empty
621  error = ERROR_UNEXPECTED_MESSAGE;
622  }
623  }
624  else
625  {
626  //Release encryption engine
627  tlsFreeEncryptionEngine(&context->encryptionEngine);
628 
629  //Inform the record layer that subsequent records will be protected
630  //under the new traffic keys
631  error = tlsInitEncryptionEngine(context, &context->encryptionEngine,
632  TLS_CONNECTION_END_SERVER, context->serverAppTrafficSecret);
633  }
634 
635  //Failed to generate traffic keying material?
636  if(error)
637  return error;
638 
639  //Calculate exporter master secret
640  error = tls13DeriveSecret(context, context->secret, hash->digestSize,
641  "exp master", NULL, 0, context->exporterMasterSecret, hash->digestSize);
642  //Any error to report?
643  if(error)
644  return error;
645 
646  //Debug message
647  TRACE_DEBUG("Exporter master secret:\r\n");
648  TRACE_DEBUG_ARRAY(" ", context->exporterMasterSecret, hash->digestSize);
649 
650 #if (TLS_KEY_LOG_SUPPORT == ENABLED)
651  //Log client application traffic secret
652  tlsDumpSecret(context, "CLIENT_TRAFFIC_SECRET_0",
653  context->clientAppTrafficSecret, hash->digestSize);
654 
655  //Log server application traffic secret
656  tlsDumpSecret(context, "SERVER_TRAFFIC_SECRET_0",
657  context->serverAppTrafficSecret, hash->digestSize);
658 
659  //Log exporter master secret
660  tlsDumpSecret(context, "EXPORTER_SECRET",
661  context->exporterMasterSecret, hash->digestSize);
662 #endif
663 
664 #if (DTLS_SUPPORT == ENABLED)
665  //DTLS protocol?
666  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM &&
667  context->entity == TLS_CONNECTION_END_CLIENT)
668  {
669  //Because each epoch resets the sequence number space, a separate sliding
670  //window is needed for each epoch (refer to RFC 9147, section 4.5.1)
671  dtlsInitReplayWindow(context);
672  }
673 #endif
674 
675  //Check whether TLS operates as a client or a server
676  if(context->entity == TLS_CONNECTION_END_CLIENT)
677  {
678  //In DTLS 1.3, the EndOfEarlyData message is omitted both from the wire
679  //and the handshake transcript. Because DTLS records have epochs,
680  //EndOfEarlyData is not necessary to determine when the early data is
681  //complete (refer to RFC 9147, section 5.6)
682  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_STREAM &&
683  context->earlyDataEnabled && context->earlyDataExtReceived)
684  {
685  //If the server sent an EarlyData extension, the client must send an
686  //EndOfEarlyData message after receiving the server Finished
688  }
689  else
690  {
691  //PSK key exchange method?
692  if(context->keyExchMethod == TLS13_KEY_EXCH_PSK ||
693  context->keyExchMethod == TLS13_KEY_EXCH_PSK_DHE ||
694  context->keyExchMethod == TLS13_KEY_EXCH_PSK_ECDHE)
695  {
696  //Send a Finished message to the server
698  }
699  else
700  {
701  //Send a Certificate message if the server requests it
703  }
704  }
705  }
706  else
707  {
708  //PSK key exchange method?
709  if(context->keyExchMethod == TLS13_KEY_EXCH_PSK ||
710  context->keyExchMethod == TLS13_KEY_EXCH_PSK_DHE ||
711  context->keyExchMethod == TLS13_KEY_EXCH_PSK_ECDHE)
712  {
713  //Wait for a Finished message from the client
715  }
716  else
717  {
718  //The client must send a Certificate message if the server requests it
719  if(context->clientAuthMode != TLS_CLIENT_AUTH_NONE)
720  {
722  }
723  else
724  {
726  }
727  }
728  }
729 
730  //Successful processing
731  return NO_ERROR;
732 }
733 
734 
735 /**
736  * @brief Compute client application traffic keys
737  * @param[in] context Pointer to the TLS context
738  * @return Error code
739  **/
740 
742 {
743  error_t error;
744  const HashAlgo *hash;
745 
746  //The hash function used by HKDF is the cipher suite hash algorithm
747  hash = context->cipherSuite.prfHashAlgo;
748  //Make sure the hash algorithm is valid
749  if(hash == NULL)
750  return ERROR_FAILURE;
751 
752  //At this point, the handshake is complete, and the client and server
753  //derive the keying material required by the record layer to exchange
754  //application-layer data protected through authenticated encryption
755  if(context->entity == TLS_CONNECTION_END_CLIENT)
756  {
757  //Release encryption engine
758  tlsFreeEncryptionEngine(&context->encryptionEngine);
759 
760  //Inform the record layer that subsequent records will be protected
761  //under the new traffic keys
762  error = tlsInitEncryptionEngine(context, &context->encryptionEngine,
763  TLS_CONNECTION_END_CLIENT, context->clientAppTrafficSecret);
764  }
765  else
766  {
767  //The implementation must verify that its receive buffer is empty before
768  //rekeying
769  if(context->rxBufferLen == 0)
770  {
771  //Release decryption engine
772  tlsFreeEncryptionEngine(&context->decryptionEngine);
773 
774  //Inform the record layer that subsequent records will be protected
775  //under the new traffic keys
776  error = tlsInitEncryptionEngine(context, &context->decryptionEngine,
777  TLS_CONNECTION_END_CLIENT, context->clientAppTrafficSecret);
778  }
779  else
780  {
781  //The receive buffer is not empty
782  error = ERROR_UNEXPECTED_MESSAGE;
783  }
784  }
785 
786  //Failed to generate traffic keying material?
787  if(error)
788  return error;
789 
790  //Calculate resumption master secret
791  error = tls13DeriveSecret(context, context->secret, hash->digestSize,
792  "res master", NULL, 0, context->resumptionMasterSecret, hash->digestSize);
793  //Any error to report?
794  if(error)
795  return error;
796 
797  //Debug message
798  TRACE_DEBUG("Resumption master secret:\r\n");
799  TRACE_DEBUG_ARRAY(" ", context->resumptionMasterSecret, hash->digestSize);
800 
801  //Once all the values which are to be derived from a given secret have been
802  //computed, that secret should be erased
803  osMemset(context->secret, 0, TLS13_MAX_HKDF_DIGEST_SIZE);
804  osMemset(context->clientEarlyTrafficSecret, 0, TLS13_MAX_HKDF_DIGEST_SIZE);
805  osMemset(context->clientHsTrafficSecret, 0, TLS13_MAX_HKDF_DIGEST_SIZE);
806  osMemset(context->serverHsTrafficSecret, 0, TLS13_MAX_HKDF_DIGEST_SIZE);
807 
808 #if (DTLS_SUPPORT == ENABLED)
809  //DTLS protocol?
810  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM &&
811  context->entity == TLS_CONNECTION_END_SERVER)
812  {
813  //Because each epoch resets the sequence number space, a separate sliding
814  //window is needed for each epoch (refer to RFC 9147, section 4.5.1)
815  dtlsInitReplayWindow(context);
816  }
817 #endif
818 
819 #if (TLS_TICKET_SUPPORT == ENABLED)
820  //Check whether session ticket mechanism is enabled
821  if(context->entity == TLS_CONNECTION_END_SERVER &&
822  context->ticketEncryptCallback != NULL &&
823  context->pskKeModeSupported)
824  {
825  //At any time after the server has received the client Finished message,
826  //it may send a NewSessionTicket message
828  }
829  else
830 #endif
831  {
832  //At this point, the handshake is complete, and the client and server
833  //can exchange application-layer data
835  }
836 
837  //Successful processing
838  return NO_ERROR;
839 }
840 
841 #endif
uint8_t message[]
Definition: chap.h:154
#define PRIuSIZE
char char_t
Definition: compiler_port.h:48
Debugging facilities.
#define TRACE_DEBUG_ARRAY(p, a, n)
Definition: debug.h:108
#define TRACE_DEBUG(...)
Definition: debug.h:107
uint8_t n
Ipv6Addr prefix
void dtlsInitReplayWindow(TlsContext *context)
Initialize sliding window.
Definition: dtls_misc.c:448
error_t
Error codes.
Definition: error.h:43
@ ERROR_HANDSHAKE_FAILED
Definition: error.h:232
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
@ ERROR_INVALID_LENGTH
Definition: error.h:111
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
@ ERROR_UNEXPECTED_MESSAGE
Definition: error.h:194
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 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
HKDF (HMAC-based Key Derivation Function)
#define osMemset(p, value, length)
Definition: os_port.h:135
#define osMemcpy(dest, src, length)
Definition: os_port.h:141
#define LSB(x)
Definition: os_port.h:55
#define osStrlen(s)
Definition: os_port.h:165
#define MSB(x)
Definition: os_port.h:59
Common interface for hash algorithms.
Definition: crypto.h:1014
HashAlgoCompute compute
Definition: crypto.h:1023
size_t digestSize
Definition: crypto.h:1020
error_t tls13GenerateClientAppTrafficKeys(TlsContext *context)
Compute client application traffic keys.
error_t tls13GenerateServerAppTrafficKeys(TlsContext *context)
Compute server application traffic keys.
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.
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.
error_t tls13GenerateEarlyTrafficKeys(TlsContext *context)
Compute early traffic keys.
TLS 1.3 key schedule.
bool_t tls13IsPskValid(TlsContext *context)
Check whether an externally established PSK is valid.
Definition: tls13_misc.c:538
#define TLS13_MAX_HKDF_DIGEST_SIZE
Definition: tls13_misc.h:120
bool_t tls13IsTicketValid(TlsContext *context)
Check whether a session ticket is valid.
Definition: tls13_ticket.c:51
TLS 1.3 session tickets.
TLS (Transport Layer Security)
#define tlsAllocMem(size)
Definition: tls.h:846
@ TLS_STATE_CLIENT_FINISHED
Definition: tls.h:1459
@ TLS_STATE_EARLY_DATA
Definition: tls.h:1441
@ TLS_STATE_APPLICATION_DATA
Definition: tls.h:1468
@ TLS_STATE_NEW_SESSION_TICKET
Definition: tls.h:1466
@ TLS_STATE_END_OF_EARLY_DATA
Definition: tls.h:1464
@ TLS_STATE_CLIENT_CERTIFICATE
Definition: tls.h:1454
@ TLS_STATE_ENCRYPTED_EXTENSIONS
Definition: tls.h:1448
@ TLS13_KEY_EXCH_PSK_DHE
Definition: tls.h:1146
@ TLS13_KEY_EXCH_ECDHE
Definition: tls.h:1144
@ TLS13_KEY_EXCH_PSK
Definition: tls.h:1145
@ TLS13_KEY_EXCH_DHE
Definition: tls.h:1143
@ TLS13_KEY_EXCH_PSK_ECDHE
Definition: tls.h:1147
TlsTransportProtocol
TLS transport protocols.
Definition: tls.h:940
@ TLS_TRANSPORT_PROTOCOL_STREAM
Definition: tls.h:941
@ TLS_TRANSPORT_PROTOCOL_DATAGRAM
Definition: tls.h:942
uint8_t secret[TLS_MASTER_SECRET_SIZE]
Master secret.
Definition: tls.h:1867
#define tlsFreeMem(p)
Definition: tls.h:851
@ TLS_CLIENT_AUTH_NONE
Definition: tls.h:964
#define TLS_MAX_HKDF_DIGEST_SIZE
Definition: tls.h:906
#define TlsContext
Definition: tls.h:36
@ TLS_CONNECTION_END_SERVER
Definition: tls.h:954
@ TLS_CONNECTION_END_CLIENT
Definition: tls.h:953
void tlsDumpSecret(TlsContext *context, const char_t *label, const uint8_t *secret, size_t secretLen)
Dump secret key (for debugging purpose only)
Key material generation.
void tlsFreeEncryptionEngine(TlsEncryptionEngine *encryptionEngine)
Release encryption engine.
Definition: tls_misc.c:917
void tlsChangeState(TlsContext *context, TlsState newState)
Update TLS state.
Definition: tls_misc.c:54
__weak_func error_t tlsInitEncryptionEngine(TlsContext *context, TlsEncryptionEngine *encryptionEngine, TlsConnectionEnd entity, const uint8_t *secret)
Initialize encryption engine.
Definition: tls_misc.c:670
TLS helper functions.
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.
Transcript hash calculation.