tls_misc.c
Go to the documentation of this file.
1 /**
2  * @file tls_misc.c
3  * @brief TLS helper functions
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.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_cipher_suites.h"
37 #include "tls_common.h"
38 #include "tls_ffdhe.h"
39 #include "tls_misc.h"
40 #include "tls13_key_material.h"
41 #include "encoding/oid.h"
42 #include "debug.h"
43 
44 //Check TLS library configuration
45 #if (TLS_SUPPORT == ENABLED)
46 
47 
48 /**
49  * @brief Update TLS state
50  * @param[in] context Pointer to the TLS context
51  * @param[in] newState New state to switch to
52  **/
53 
54 void tlsChangeState(TlsContext *context, TlsState newState)
55 {
56  //Switch to the new state
57  context->state = newState;
58 
59  //Any registered callback?
60  if(context->stateChangeCallback != NULL)
61  {
62  //Invoke user callback function
63  context->stateChangeCallback(context, newState);
64  }
65 }
66 
67 
68 /**
69  * @brief Translate an error code to an alert message
70  * @param[in] context Pointer to the TLS context
71  * @param[in] errorCode Internal error code
72  **/
73 
75 {
76  //Check current state
77  if(context->state != TLS_STATE_INIT &&
78  context->state != TLS_STATE_CLOSED)
79  {
80  //Check status code
81  switch(errorCode)
82  {
83  //The timeout interval has elapsed
84  case ERROR_TIMEOUT:
85  break;
86 
87  //The read/write operation would have blocked
88  case ERROR_WOULD_BLOCK:
89  break;
90 
91  //Failed to allocate memory
93  break;
94 
95  //The read/write operation has failed
96  case ERROR_WRITE_FAILED:
97  case ERROR_READ_FAILED:
99  break;
100 
101  //An inappropriate message was received
104  break;
105 
106  //A record is received with an incorrect MAC
109  break;
110 
111  //Invalid record length
114  break;
115 
116  //Unable to negotiate an acceptable set of security parameters
119  break;
120 
121  //A certificate was corrupt
124  break;
125 
126  //A certificate was of an unsupported type
129  break;
130 
131  //A certificate has expired or is not currently valid
134  break;
135  //Some other issue arose in processing the certificate, rendering it unacceptable
138  break;
139 
140  //A field in the handshake was out of range or inconsistent with other fields
143  break;
144 
145  //The certificate could not be matched with a known, trusted CA
146  case ERROR_UNKNOWN_CA:
148  break;
149 
150  //A message could not be decoded because some field was incorrect
153  break;
154 
155  //A handshake cryptographic operation failed
159  break;
160 
161  //The protocol version the client has attempted to negotiate is not supported
164  break;
165 
166  //Inappropriate fallback detected by the server
169  break;
170 
171  //Handshake message not containing an extension that is mandatory
174  break;
175 
176  //The ServerHello contains an extension not present in the ClientHello
179  break;
180 
181  //A client certificate is desired but none was provided by the client
184  break;
185 
186  //No application protocol supported by the server
189  break;
190 
191  //Internal error
192  default:
194  break;
195  }
196  }
197 }
198 
199 
200 /**
201  * @brief Generate client or server random value
202  * @param[in] context Pointer to the TLS context
203  * @param[out] random Pointer to the random value
204  * @return Error code
205  **/
206 
208 {
209  error_t error;
210 
211  //Verify that the pseudorandom number generator is properly configured
212  if(context->prngAlgo != NULL && context->prngContext != NULL)
213  {
214  //Generate a 32-byte random value using a cryptographically-safe
215  //pseudorandom number generator
216  error = context->prngAlgo->generate(context->prngContext, random, 32);
217  }
218  else
219  {
220  //Report an error
221  error = ERROR_NOT_CONFIGURED;
222  }
223 
224 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
225  //Check status code
226  if(!error)
227  {
228  //TLS 1.3 has a downgrade protection mechanism embedded in the server's
229  //random value
230  if(context->entity == TLS_CONNECTION_END_SERVER)
231  {
232  //Check negotiated version
233  if(context->version <= TLS_VERSION_1_1 &&
234  context->versionMax >= TLS_VERSION_1_2)
235  {
236  //If negotiating TLS 1.1 or below, TLS 1.3 servers must, and TLS 1.2
237  //servers should, set the last eight bytes of their random value to
238  //the bytes 44 4F 57 4E 47 52 44 00
240  }
241  else if(context->version == TLS_VERSION_1_2 &&
242  context->versionMax >= TLS_VERSION_1_3)
243  {
244  //If negotiating TLS 1.2, TLS 1.3 servers must set the last eight
245  //bytes of their random value to the bytes 44 4F 57 4E 47 52 44 01
247  }
248  else
249  {
250  //No downgrade protection mechanism
251  }
252  }
253  }
254 #endif
255 
256  //Return status code
257  return error;
258 }
259 
260 
261 /**
262  * @brief Generate a random session identifier
263  * @param[in] context Pointer to the TLS context
264  * @param[out] length Desired length of the session ID
265  * @return Error code
266  **/
267 
269 {
270  error_t error;
271 
272  //Verify that the pseudorandom number generator is properly configured
273  if(context->prngAlgo != NULL && context->prngContext != NULL)
274  {
275  //Generate a random value using a cryptographically-safe pseudorandom
276  //number generator
277  error = context->prngAlgo->generate(context->prngContext,
278  context->sessionId, length);
279 
280  //Check status code
281  if(!error)
282  {
283  //Save the length of the session identifier
284  context->sessionIdLen = length;
285  }
286  }
287  else
288  {
289  //Report an error
290  error = ERROR_NOT_CONFIGURED;
291  }
292 
293  //Return status code
294  return error;
295 }
296 
297 
298 /**
299  * @brief Set the TLS version to be used
300  * @param[in] context Pointer to the TLS context
301  * @param[in] version TLS version
302  * @return Error code
303  **/
304 
306 {
307  error_t error;
308 
309  //Initialize status code
311 
312  //Check TLS version
313  if(version >= context->versionMin && version <= context->versionMax)
314  {
315  //Save the TLS protocol version to be used
316  context->version = version;
317  //The specified TLS version is acceptable
318  error = NO_ERROR;
319  }
320 
321  //Return status code
322  return error;
323 }
324 
325 
326 /**
327  * @brief Set cipher suite
328  * @param[in] context Pointer to the TLS context
329  * @param[in] identifier Cipher suite identifier
330  * @return Error code
331  **/
332 
334 {
335  error_t error;
336  uint_t i;
337  uint_t n;
339 
340  //Initialize status code
341  error = ERROR_HANDSHAKE_FAILED;
342 
343  //Determine the number of supported cipher suites
345 
346  //Loop through the list of supported cipher suites
347  for(cipherSuite = NULL, i = 0; i < n; i++)
348  {
349  //Compare cipher suite identifiers
351  {
352  //The cipher suite is supported
354  break;
355  }
356  }
357 
358  //Restrict the use of certain cipher suites
359  if(context->numCipherSuites > 0)
360  {
361  //Loop through the list of allowed cipher suites
362  for(i = 0; i < context->numCipherSuites; i++)
363  {
364  //Compare cipher suite identifiers
365  if(context->cipherSuites[i] == identifier)
366  break;
367  }
368 
369  //Check whether the use of the cipher suite is restricted
370  if(i >= context->numCipherSuites)
371  {
372  cipherSuite = NULL;
373  }
374  }
375 
376  //Acceptable cipher suite?
377  if(cipherSuite != NULL)
378  {
379  //Check whether the cipher suite can be negotiated with the negotiated
380  //protocol version
381  if(!tlsIsCipherSuiteAcceptable(cipherSuite, context->version,
382  context->version, context->transportProtocol))
383  {
384  cipherSuite = NULL;
385  }
386  }
387 
388  //Ensure that the selected cipher suite matches all the criteria
389  if(cipherSuite != NULL)
390  {
391  //Save the negotiated cipher suite
392  context->cipherSuite = *cipherSuite;
393  //Set the key exchange method to be used
394  context->keyExchMethod = cipherSuite->keyExchMethod;
395 
396  //PRF with the SHA-256 is used for all cipher suites published prior
397  //than TLS 1.2 when TLS 1.2 is negotiated
398  if(context->cipherSuite.prfHashAlgo == NULL)
399  {
400  context->cipherSuite.prfHashAlgo = SHA256_HASH_ALGO;
401  }
402 
403  //The length of the verify data depends on the TLS version currently used
404  if(context->version <= TLS_VERSION_1_1)
405  {
406  //Verify data is always 12-byte long for TLS 1.0 and 1.1
407  context->cipherSuite.verifyDataLen = 12;
408  }
409  else
410  {
411  //The length of the verify data depends on the cipher suite for TLS 1.2
412  }
413 
414  //The specified cipher suite is acceptable
415  error = NO_ERROR;
416  }
417 
418  //Return status code
419  return error;
420 }
421 
422 
423 /**
424  * @brief Save session ID
425  * @param[in] context Pointer to the TLS context
426  * @param[out] session Pointer to the session state
427  * @return Error code
428  **/
429 
431  TlsSessionState *session)
432 {
433 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
434  //Check TLS version
435  if(context->version < TLS_VERSION_1_0 || context->version > TLS_VERSION_1_2)
436  return ERROR_INVALID_VERSION;
437 
438  //Invalid session identifier?
439  if(context->sessionIdLen == 0)
440  return ERROR_INVALID_TICKET;
441 
442  //Invalid session parameters?
443  if(context->cipherSuite.identifier == 0)
444  return ERROR_INVALID_SESSION;
445 
446  //Save current time
447  session->timestamp = osGetSystemTime();
448 
449  //Save session parameters
450  session->version = context->version;
451  session->cipherSuite = context->cipherSuite.identifier;
452 
453  //Copy session identifier
454  osMemcpy(session->sessionId, context->sessionId, context->sessionIdLen);
455  session->sessionIdLen = context->sessionIdLen;
456 
457  //Save master secret
458  osMemcpy(session->secret, context->masterSecret, TLS_MASTER_SECRET_SIZE);
459 
460 #if (TLS_EXT_MASTER_SECRET_SUPPORT == ENABLED)
461  //Extended master secret computation
462  session->extendedMasterSecret = context->emsExtReceived;
463 #endif
464 
465 #if (TLS_SNI_SUPPORT == ENABLED)
466  //Any ServerName extension received by the server?
467  if(context->entity == TLS_CONNECTION_END_SERVER &&
468  context->serverName != NULL)
469  {
470  size_t n;
471 
472  //Retrieve the length of the server name
473  n = osStrlen(context->serverName);
474 
475  //Allocate a memory block to hold the server name
476  session->serverName = tlsAllocMem(n + 1);
477  //Failed to allocate memory?
478  if(session->serverName == NULL)
479  return ERROR_OUT_OF_MEMORY;
480 
481  //Copy the server name
482  osStrcpy(session->serverName, context->serverName);
483  }
484 #endif
485 
486  //Successful processing
487  return NO_ERROR;
488 #else
489  //Not implemented
490  return ERROR_NOT_IMPLEMENTED;
491 #endif
492 }
493 
494 
495 /**
496  * @brief Save session ticket
497  * @param[in] context Pointer to the TLS context
498  * @param[out] session Pointer to the session state
499  * @return Error code
500  **/
501 
503  TlsSessionState *session)
504 {
505 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
506  //Check TLS version
507  if(context->version < TLS_VERSION_1_0 || context->version > TLS_VERSION_1_2)
508  return ERROR_INVALID_VERSION;
509 
510  //Invalid session ticket?
511  if(context->ticket == NULL || context->ticketLen == 0)
512  return ERROR_INVALID_TICKET;
513 
514  //Invalid session parameters?
515  if(context->cipherSuite.identifier == 0)
516  return ERROR_INVALID_SESSION;
517 
518  //Save session parameters
519  session->version = context->version;
520  session->cipherSuite = context->cipherSuite.identifier;
521 
522  //Allocate a memory block to hold the ticket
523  session->ticket = tlsAllocMem(context->ticketLen);
524  //Failed to allocate memory?
525  if(session->ticket == NULL)
526  return ERROR_OUT_OF_MEMORY;
527 
528  //Copy session ticket
529  osMemcpy(session->ticket, context->ticket, context->ticketLen);
530  session->ticketLen = context->ticketLen;
531 
532  //Save master secret
533  osMemcpy(session->secret, context->masterSecret, TLS_MASTER_SECRET_SIZE);
534 
535 #if (TLS_EXT_MASTER_SECRET_SUPPORT == ENABLED)
536  //Extended master secret computation
537  session->extendedMasterSecret = context->emsExtReceived;
538 #endif
539 
540  //Successful processing
541  return NO_ERROR;
542 #else
543  //Not implemented
544  return ERROR_NOT_IMPLEMENTED;
545 #endif
546 }
547 
548 
549 /**
550  * @brief Restore a TLS session using session ID
551  * @param[in] context Pointer to the TLS context
552  * @param[in] session Pointer to the session state
553  * @return Error code
554  **/
555 
557  const TlsSessionState *session)
558 {
559 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
560  //Check TLS version
561  if(session->version < TLS_VERSION_1_0 || session->version > TLS_VERSION_1_2)
562  return ERROR_INVALID_VERSION;
563 
564  //Invalid session identifier?
565  if(session->sessionIdLen == 0)
566  return ERROR_INVALID_SESSION;
567 
568  //Invalid session parameters?
569  if(session->cipherSuite == 0)
570  return ERROR_INVALID_SESSION;
571 
572  //Restore session parameters
573  context->version = session->version;
574  context->cipherSuite.identifier = session->cipherSuite;
575  context->sessionIdLen = 0;
576 
577  //Copy session identifier
578  osMemcpy(context->sessionId, session->sessionId, session->sessionIdLen);
579  context->sessionIdLen = session->sessionIdLen;
580 
581  //Restore master secret
582  osMemcpy(context->masterSecret, session->secret, TLS_MASTER_SECRET_SIZE);
583 
584 #if (TLS_EXT_MASTER_SECRET_SUPPORT == ENABLED)
585  //Extended master secret computation
586  context->emsExtReceived = session->extendedMasterSecret;
587 #endif
588 
589  //Successful processing
590  return NO_ERROR;
591 #else
592  //Not implemented
593  return ERROR_NOT_IMPLEMENTED;
594 #endif
595 }
596 
597 
598 /**
599  * @brief Restore a TLS session using session ticket
600  * @param[in] context Pointer to the TLS context
601  * @param[in] session Pointer to the session state
602  * @return Error code
603  **/
604 
606  const TlsSessionState *session)
607 {
608 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
609  //Check TLS version
610  if(session->version < TLS_VERSION_1_0 || session->version > TLS_VERSION_1_2)
611  return ERROR_INVALID_VERSION;
612 
613  //Invalid session ticket?
614  if(session->ticket == NULL || session->ticketLen == 0)
615  return ERROR_INVALID_TICKET;
616 
617  //Invalid session parameters?
618  if(session->cipherSuite == 0)
619  return ERROR_INVALID_SESSION;
620 
621  //Restore session parameters
622  context->version = session->version;
623  context->cipherSuite.identifier = session->cipherSuite;
624  context->sessionIdLen = 0;
625 
626  //Release existing session ticket, if any
627  if(context->ticket != NULL)
628  {
629  osMemset(context->ticket, 0, context->ticketLen);
630  tlsFreeMem(context->ticket);
631  context->ticket = NULL;
632  context->ticketLen = 0;
633  }
634 
635  //Allocate a memory block to hold the ticket
636  context->ticket = tlsAllocMem(session->ticketLen);
637  //Failed to allocate memory?
638  if(context->ticket == NULL)
639  return ERROR_OUT_OF_MEMORY;
640 
641  //Copy session ticket
642  osMemcpy(context->ticket, session->ticket, session->ticketLen);
643  context->ticketLen = session->ticketLen;
644 
645  //Restore master secret
646  osMemcpy(context->masterSecret, session->secret, TLS_MASTER_SECRET_SIZE);
647 
648 #if (TLS_EXT_MASTER_SECRET_SUPPORT == ENABLED)
649  //Extended master secret computation
650  context->emsExtReceived = session->extendedMasterSecret;
651 #endif
652 
653  //Successful processing
654  return NO_ERROR;
655 #else
656  //Not implemented
657  return ERROR_NOT_IMPLEMENTED;
658 #endif
659 }
660 
661 
662 /**
663  * @brief Initialize encryption engine
664  * @param[in] context Pointer to the TLS context
665  * @param[in] encryptionEngine Pointer to the encryption/decryption engine to
666  * be initialized
667  * @param[in] entity Specifies whether client or server write keys shall be used
668  * @param[in] level Encryption level
669  * @param[in] secret Pointer to the secret value
670  * @return Error code
671  **/
672 
674  TlsEncryptionEngine *encryptionEngine, TlsConnectionEnd entity,
675  TlsEncryptionLevel level, const uint8_t *secret)
676 {
677  error_t error;
678  const CipherAlgo *cipherAlgo;
680 
681  //Point to the negotiated cipher suite
682  cipherSuite = &context->cipherSuite;
683  //Point to the cipher algorithm
684  cipherAlgo = cipherSuite->cipherAlgo;
685 
686  //Save the negotiated TLS version
687  encryptionEngine->version = context->version;
688 
689  //The sequence number is set to zero at the beginning of a connection
690  //and whenever the key is changed
691  osMemset(&encryptionEngine->seqNum, 0, sizeof(TlsSequenceNumber));
692 
693 #if (DTLS_SUPPORT == ENABLED)
694  //DTLS 1.3 protocol?
695  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM &&
696  context->version == TLS_VERSION_1_3)
697  {
698  //This version of DTLS assigns dedicated epoch values to messages in the
699  //protocol exchange to allow identification of the correct cipher state
700  //(refer to RFC 9147, section 6.1)
701  if(encryptionEngine->epoch == 0 &&
703  {
704  //Epoch value 1 is used for messages protected using keys derived from
705  //client_early_traffic_secret
706  encryptionEngine->epoch = 1;
707  }
708  else if(encryptionEngine->epoch == 0 &&
710  {
711  //Epoch value 2 is used for messages protected using keys derived from
712  //client_handshake_traffic_secret or server_handshake_traffic_secret
713  encryptionEngine->epoch = 2;
714  }
715  else
716  {
717  //The epoch number is incremented each time keying material changes
718  encryptionEngine->epoch++;
719  }
720  }
721  else
722  {
723  //The epoch number is initially zero and is incremented each time a
724  //ChangeCipherSpec message is sent
725  encryptionEngine->epoch++;
726  }
727 
728  //Implementations must not allow the epoch to wrap (refer to RFC 6347,
729  //section 4.1)
730  if(encryptionEngine->epoch == 0)
731  return ERROR_FAILURE;
732 
733  //Sequence numbers are maintained separately for each epoch, with each
734  //sequence number initially being 0 for each epoch
735  osMemset(&encryptionEngine->dtlsSeqNum, 0, sizeof(DtlsSequenceNumber));
736 #endif
737 
738 #if (TLS_QUIC_SUPPORT == ENABLED)
739  //TLS encryption level determines the QUIC packet type and keys that are
740  //used for protecting data (refer to RFC 9001, section 4.1.3)
741  encryptionEngine->level = level;
742 #endif
743 
744 #if (TLS_RECORD_SIZE_LIMIT_SUPPORT == ENABLED)
745  //The value of RecordSizeLimit is used to limit the size of records
746  //that are created when encoding application data and the protected
747  //handshake message into records (refer to RFC 8449, section 4)
748  if(entity == context->entity)
749  {
750  encryptionEngine->recordSizeLimit = context->recordSizeLimit;
751  }
752  else
753  {
754  encryptionEngine->recordSizeLimit = MIN(context->rxBufferMaxLen,
756  }
757 #endif
758 
759 #if (TLS_ENCRYPT_THEN_MAC_SUPPORT == ENABLED)
760  //If an upgrade from MAC-then-encrypt to encrypt-then-MAC is negotiated,
761  //then the change will take place in the first message that follows the
762  //ChangeCipherSpec message (refer to RFC 7366, section 3.1)
763  encryptionEngine->encryptThenMac = context->etmExtReceived;
764 #endif
765 
766  //Set appropriate length for MAC key, encryption key, authentication
767  //tag and IV
768  encryptionEngine->macKeyLen = cipherSuite->macKeyLen;
769  encryptionEngine->encKeyLen = cipherSuite->encKeyLen;
770  encryptionEngine->fixedIvLen = cipherSuite->fixedIvLen;
771  encryptionEngine->recordIvLen = cipherSuite->recordIvLen;
772  encryptionEngine->authTagLen = cipherSuite->authTagLen;
773 
774  //Set cipher and hash algorithms
775  encryptionEngine->cipherAlgo = cipherSuite->cipherAlgo;
776  encryptionEngine->cipherMode = cipherSuite->cipherMode;
777  encryptionEngine->hashAlgo = cipherSuite->hashAlgo;
778 
779  //Initialize cipher context
780  encryptionEngine->cipherContext = NULL;
781 
782 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
783  //Initialize HMAC context
784  encryptionEngine->hmacContext = &context->hmacContext;
785 #endif
786 
787 #if (TLS_GCM_CIPHER_SUPPORT == ENABLED)
788  //Initialize GCM context
789  encryptionEngine->gcmContext = NULL;
790 #endif
791 
792 #if (DTLS_SUPPORT == ENABLED && TLS_MAX_VERSION >= TLS_VERSION_1_3)
793  //Initialize sequence number encryption context
794  encryptionEngine->snCipherContext = NULL;
795 #endif
796 
797 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
798  //TLS 1.0, TLS 1.1 or TLS 1.2 currently selected?
799  if(context->version <= TLS_VERSION_1_2)
800  {
801  const uint8_t *p;
802 
803  //Check whether client or server write keys shall be used
804  if(entity == TLS_CONNECTION_END_CLIENT)
805  {
806  //Point to the key material
807  p = context->keyBlock;
808  //Save MAC key
809  osMemcpy(encryptionEngine->macKey, p, cipherSuite->macKeyLen);
810 
811  //Advance current position in the key block
812  p += 2 * cipherSuite->macKeyLen;
813  //Save encryption key
814  osMemcpy(encryptionEngine->encKey, p, cipherSuite->encKeyLen);
815 
816  //Advance current position in the key block
817  p += 2 * cipherSuite->encKeyLen;
818  //Save initialization vector
819  osMemcpy(encryptionEngine->iv, p, cipherSuite->fixedIvLen);
820  }
821  else
822  {
823  //Point to the key material
824  p = context->keyBlock + cipherSuite->macKeyLen;
825  //Save MAC key
826  osMemcpy(encryptionEngine->macKey, p, cipherSuite->macKeyLen);
827 
828  //Advance current position in the key block
829  p += cipherSuite->macKeyLen + cipherSuite->encKeyLen;
830  //Save encryption key
831  osMemcpy(encryptionEngine->encKey, p, cipherSuite->encKeyLen);
832 
833  //Advance current position in the key block
834  p += cipherSuite->encKeyLen + cipherSuite->fixedIvLen;
835  //Save initialization vector
836  osMemcpy(encryptionEngine->iv, p, cipherSuite->fixedIvLen);
837  }
838 
839  //Successful processing
840  error = NO_ERROR;
841  }
842  else
843 #endif
844 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
845  //TLS 1.3 currently selected?
846  if(context->version == TLS_VERSION_1_3)
847  {
848  const HashAlgo *hashAlgo;
849 
850  //The hash function used by HKDF is the cipher suite hash algorithm
851  hashAlgo = cipherSuite->prfHashAlgo;
852 
853  //Make sure the hash algorithm is valid
854  if(hashAlgo != NULL)
855  {
856  //Calculate the write key
857  error = tls13HkdfExpandLabel(context->transportProtocol, hashAlgo,
858  secret, hashAlgo->digestSize, "key", NULL, 0,
859  encryptionEngine->encKey, cipherSuite->encKeyLen);
860 
861  //Check status code
862  if(!error)
863  {
864  //Debug message
865  TRACE_DEBUG("Write Key:\r\n");
866  TRACE_DEBUG_ARRAY(" ", encryptionEngine->encKey, cipherSuite->encKeyLen);
867 
868  //Calculate the write IV
869  error = tls13HkdfExpandLabel(context->transportProtocol, hashAlgo,
870  secret, hashAlgo->digestSize, "iv", NULL, 0,
871  encryptionEngine->iv, cipherSuite->fixedIvLen);
872  }
873 
874  //Check status code
875  if(!error)
876  {
877  //Debug message
878  TRACE_DEBUG("Write IV:\r\n");
879  TRACE_DEBUG_ARRAY(" ", encryptionEngine->iv, cipherSuite->fixedIvLen);
880  }
881  }
882  else
883  {
884  //Invalid HKDF hash algorithm
885  error = ERROR_FAILURE;
886  }
887  }
888  else
889 #endif
890  //Invalid TLS version?
891  {
892  //Report an error
893  error = ERROR_INVALID_VERSION;
894  }
895 
896  //Check status code
897  if(!error)
898  {
899  //Check cipher mode of operation
900  if(encryptionEngine->cipherMode == CIPHER_MODE_STREAM ||
901  encryptionEngine->cipherMode == CIPHER_MODE_CBC ||
902  encryptionEngine->cipherMode == CIPHER_MODE_CCM ||
903  encryptionEngine->cipherMode == CIPHER_MODE_GCM)
904  {
905  //Allocate encryption context
906  encryptionEngine->cipherContext = tlsAllocMem(cipherAlgo->contextSize);
907 
908  //Successful memory allocation?
909  if(encryptionEngine->cipherContext != NULL)
910  {
911  //Configure the encryption engine with the write key
912  error = cipherAlgo->init(encryptionEngine->cipherContext,
913  encryptionEngine->encKey, cipherSuite->encKeyLen);
914  }
915  else
916  {
917  //Failed to allocate memory
918  error = ERROR_OUT_OF_MEMORY;
919  }
920  }
921  else if(encryptionEngine->cipherMode == CIPHER_MODE_NULL ||
922  encryptionEngine->cipherMode == CIPHER_MODE_CHACHA20_POLY1305)
923  {
924  //No need to allocate an encryption context
925  error = NO_ERROR;
926  }
927  else
928  {
929  //Unsupported mode of operation
930  error = ERROR_FAILURE;
931  }
932  }
933 
934 #if (TLS_GCM_CIPHER_SUPPORT == ENABLED)
935  //Check status code
936  if(!error)
937  {
938  //GCM cipher mode?
939  if(encryptionEngine->cipherMode == CIPHER_MODE_GCM)
940  {
941  //Allocate a memory buffer to hold the GCM context
942  encryptionEngine->gcmContext = tlsAllocMem(sizeof(GcmContext));
943 
944  //Successful memory allocation?
945  if(encryptionEngine->gcmContext != NULL)
946  {
947  //Initialize GCM context
948  error = gcmInit(encryptionEngine->gcmContext, cipherAlgo,
949  encryptionEngine->cipherContext);
950  }
951  else
952  {
953  //Failed to allocate memory
954  error = ERROR_OUT_OF_MEMORY;
955  }
956  }
957  }
958 #endif
959 
960 #if (DTLS_SUPPORT == ENABLED && TLS_MAX_VERSION >= TLS_VERSION_1_3)
961  //DTLS 1.3 protocol?
962  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM &&
963  context->version == TLS_VERSION_1_3)
964  {
965  //Check status code
966  if(!error)
967  {
968  const HashAlgo *hashAlgo;
969 
970  //The hash function used by HKDF is the cipher suite hash algorithm
971  hashAlgo = cipherSuite->prfHashAlgo;
972 
973  //Make sure the hash algorithm is valid
974  if(hashAlgo != NULL)
975  {
976  //Calculate the sequence number encryption key (refer to RFC 9147,
977  //section 4.2.3)
978  error = tls13HkdfExpandLabel(context->transportProtocol, hashAlgo,
979  secret, hashAlgo->digestSize, "sn", NULL, 0,
980  encryptionEngine->snKey, encryptionEngine->encKeyLen);
981 
982  //Check status code
983  if(!error)
984  {
985  //Debug message
986  TRACE_DEBUG("SN Key:\r\n");
987  TRACE_DEBUG_ARRAY(" ", encryptionEngine->snKey, encryptionEngine->encKeyLen);
988  }
989  }
990  else
991  {
992  //Invalid HKDF hash algorithm
993  error = ERROR_FAILURE;
994  }
995  }
996 
997  //Check status code
998  if(!error)
999  {
1000  //Check cipher mode of operation
1001  if(encryptionEngine->cipherMode == CIPHER_MODE_CCM ||
1002  encryptionEngine->cipherMode == CIPHER_MODE_GCM)
1003  {
1004  //Allocate encryption context
1005  encryptionEngine->snCipherContext = tlsAllocMem(
1006  cipherAlgo->contextSize);
1007 
1008  //Successful memory allocation?
1009  if(encryptionEngine->snCipherContext != NULL)
1010  {
1011  //Configure the encryption engine with the sequence number
1012  //encryption key
1013  error = cipherAlgo->init(encryptionEngine->snCipherContext,
1014  encryptionEngine->snKey, cipherSuite->encKeyLen);
1015  }
1016  else
1017  {
1018  //Failed to allocate memory
1019  error = ERROR_OUT_OF_MEMORY;
1020  }
1021  }
1022  else if(encryptionEngine->cipherMode == CIPHER_MODE_NULL ||
1023  encryptionEngine->cipherMode == CIPHER_MODE_CHACHA20_POLY1305)
1024  {
1025  //No need to allocate an encryption context
1026  error = NO_ERROR;
1027  }
1028  else
1029  {
1030  //Unsupported mode of operation
1031  error = ERROR_FAILURE;
1032  }
1033  }
1034  }
1035 #endif
1036 
1037  //Return status code
1038  return error;
1039 }
1040 
1041 
1042 /**
1043  * @brief Release encryption engine
1044  * @param[in] encryptionEngine Pointer to the encryption/decryption engine
1045  **/
1046 
1048 {
1049  //Valid cipher context?
1050  if(encryptionEngine->cipherContext != NULL)
1051  {
1052  //Erase cipher context
1053  encryptionEngine->cipherAlgo->deinit(encryptionEngine->cipherContext);
1054 
1055  //Release memory
1056  tlsFreeMem(encryptionEngine->cipherContext);
1057  encryptionEngine->cipherContext = NULL;
1058  }
1059 
1060 #if (TLS_GCM_CIPHER_SUPPORT == ENABLED)
1061  //Valid GCM context?
1062  if(encryptionEngine->gcmContext != NULL)
1063  {
1064  //Erase GCM context
1065  osMemset(encryptionEngine->gcmContext, 0, sizeof(GcmContext));
1066 
1067  //Release memory
1068  tlsFreeMem(encryptionEngine->gcmContext);
1069  encryptionEngine->gcmContext = NULL;
1070  }
1071 #endif
1072 
1073 #if (DTLS_SUPPORT == ENABLED && TLS_MAX_VERSION >= TLS_VERSION_1_3)
1074  //Valid sequence number encryption context?
1075  if(encryptionEngine->snCipherContext != NULL)
1076  {
1077  //Erase cipher context
1078  encryptionEngine->cipherAlgo->deinit(encryptionEngine->snCipherContext);
1079 
1080  //Release memory
1081  tlsFreeMem(encryptionEngine->snCipherContext);
1082  encryptionEngine->snCipherContext = NULL;
1083  }
1084 #endif
1085 
1086  //Reset encryption parameters
1087  encryptionEngine->cipherAlgo = NULL;
1088  encryptionEngine->cipherMode = CIPHER_MODE_NULL;
1089  encryptionEngine->hashAlgo = NULL;
1090 }
1091 
1092 
1093 /**
1094  * @brief Encode a multiple precision integer to an opaque vector
1095  * @param[in] a Pointer to a multiple precision integer
1096  * @param[out] data Buffer where to store the opaque vector
1097  * @param[out] length Total number of bytes that have been written
1098  * @return Error code
1099  **/
1100 
1101 error_t tlsWriteMpi(const Mpi *a, uint8_t *data, size_t *length)
1102 {
1103  error_t error;
1104  size_t n;
1105 
1106  //Retrieve the actual size of the integer
1107  n = mpiGetByteLength(a);
1108 
1109  //The data is preceded by a 2-byte length field
1110  STORE16BE(n, data);
1111 
1112  //Convert the integer to an octet string
1113  error = mpiExport(a, data + 2, n, MPI_FORMAT_BIG_ENDIAN);
1114  //Conversion failed?
1115  if(error)
1116  return error;
1117 
1118  //Return the total number of bytes that have been written
1119  *length = n + 2;
1120 
1121  //Successful processing
1122  return NO_ERROR;
1123 }
1124 
1125 
1126 /**
1127  * @brief Read a multiple precision integer from an opaque vector
1128  * @param[out] a Resulting multiple precision integer
1129  * @param[in] data Buffer where to read the opaque vector
1130  * @param[in] size Total number of bytes available in the buffer
1131  * @param[out] length Total number of bytes that have been read
1132  * @return Error code
1133  **/
1134 
1135 error_t tlsReadMpi(Mpi *a, const uint8_t *data, size_t size, size_t *length)
1136 {
1137  error_t error;
1138  size_t n;
1139 
1140  //Buffer underrun?
1141  if(size < 2)
1142  return ERROR_DECODING_FAILED;
1143 
1144  //Decode the length field
1145  n = LOAD16BE(data);
1146 
1147  //Invalid length?
1148  if(n < 1)
1149  return ERROR_DECODING_FAILED;
1150 
1151  //Buffer underrun?
1152  if(size < (n + 2))
1153  return ERROR_DECODING_FAILED;
1154 
1155  //Convert the octet string to a multiple precision integer
1156  error = mpiImport(a, data + 2, n, MPI_FORMAT_BIG_ENDIAN);
1157  //Any error to report?
1158  if(error)
1159  return error;
1160 
1161  //Return the total number of bytes that have been read
1162  *length = n + 2;
1163 
1164  //Successful processing
1165  return NO_ERROR;
1166 }
1167 
1168 
1169 /**
1170  * @brief Encode an EC point to an opaque vector
1171  * @param[in] publicKey EC public key to be encoded
1172  * @param[out] data Buffer where to store the opaque vector
1173  * @param[out] length Total number of bytes that have been written
1174  * @return Error code
1175  **/
1176 
1177 error_t tlsWriteEcPoint(const EcPublicKey *publicKey, uint8_t *data,
1178  size_t *length)
1179 {
1180 #if (TLS_ECDH_ANON_KE_SUPPORT == ENABLED || TLS_ECDHE_RSA_KE_SUPPORT == ENABLED || \
1181  TLS_ECDHE_ECDSA_KE_SUPPORT == ENABLED || TLS_ECDHE_PSK_KE_SUPPORT == ENABLED)
1182  error_t error;
1183 
1184  //Convert the EC point to an octet string
1185  error = ecExportPublicKey(publicKey, data + 1, length,
1187  //Any error to report?
1188  if(error)
1189  return error;
1190 
1191  //Set the length of the opaque vector
1192  data[0] = (uint8_t) (*length);
1193 
1194  //Return the total number of bytes that have been written
1195  *length += 1;
1196 
1197  //Successful processing
1198  return NO_ERROR;
1199 #else
1200  //Not implemented
1201  return ERROR_NOT_IMPLEMENTED;
1202 #endif
1203 }
1204 
1205 
1206 /**
1207  * @brief Read an EC point from an opaque vector
1208  * @param[out] publicKey Resulting EC public key
1209  * @param[in] curve Elliptic curve parameters
1210  * @param[in] data Buffer where to read the opaque vector
1211  * @param[in] size Total number of bytes available in the buffer
1212  * @param[out] length Total number of bytes that have been read
1213  * @return Error code
1214  **/
1215 
1216 error_t tlsReadEcPoint(EcPublicKey *publicKey, const EcCurve *curve,
1217  const uint8_t *data, size_t size, size_t *length)
1218 {
1219 #if (TLS_ECDH_ANON_KE_SUPPORT == ENABLED || TLS_ECDHE_RSA_KE_SUPPORT == ENABLED || \
1220  TLS_ECDHE_ECDSA_KE_SUPPORT == ENABLED || TLS_ECDHE_PSK_KE_SUPPORT == ENABLED)
1221  error_t error;
1222  size_t n;
1223 
1224  //Buffer underrun?
1225  if(size < 1)
1226  return ERROR_DECODING_FAILED;
1227 
1228  //The EC point representation is preceded by a length field
1229  n = data[0];
1230 
1231  //Invalid EC point representation?
1232  if(n == 0)
1233  return ERROR_DECODING_FAILED;
1234 
1235  //Buffer underrun?
1236  if(size < (n + 1))
1237  return ERROR_DECODING_FAILED;
1238 
1239  //Convert the octet string to an EC point
1240  error = ecImportPublicKey(publicKey, curve, data + 1, n,
1242  //Any error to report?
1243  if(error)
1244  return error;
1245 
1246  //Return the total number of bytes that have been read
1247  *length = n + 1;
1248 
1249  //Successful processing
1250  return NO_ERROR;
1251 #else
1252  //Not implemented
1253  return ERROR_NOT_IMPLEMENTED;
1254 #endif
1255 }
1256 
1257 
1258 /**
1259  * @brief Convert TLS version to string representation
1260  * @param[in] version Version number
1261  * @return String representation
1262  **/
1263 
1265 {
1266  const char_t *s;
1267 
1268  //TLS versions
1269  static const char_t *const label[] =
1270  {
1271  "SSL 3.0",
1272  "TLS 1.0",
1273  "TLS 1.1",
1274  "TLS 1.2",
1275  "TLS 1.3",
1276  "DTLS 1.0",
1277  "DTLS 1.2",
1278  "DTLS 1.3",
1279  "Unknown"
1280  };
1281 
1282  //Check current version
1283  switch(version)
1284  {
1285  case SSL_VERSION_3_0:
1286  s = label[0];
1287  break;
1288  case TLS_VERSION_1_0:
1289  s = label[1];
1290  break;
1291  case TLS_VERSION_1_1:
1292  s = label[2];
1293  break;
1294  case TLS_VERSION_1_2:
1295  s = label[3];
1296  break;
1297  case TLS_VERSION_1_3:
1298  s = label[4];
1299  break;
1300  case DTLS_VERSION_1_0:
1301  s = label[5];
1302  break;
1303  case DTLS_VERSION_1_2:
1304  s = label[6];
1305  break;
1306  case DTLS_VERSION_1_3:
1307  s = label[7];
1308  break;
1309  default:
1310  s = label[8];
1311  break;
1312  }
1313 
1314  //Return the string representation
1315  return s;
1316 }
1317 
1318 
1319 /**
1320  * @brief Get the hash algorithm that matches the specified identifier
1321  * @param[in] hashAlgoId Hash algorithm identifier
1322  * @return Pointer to the hash algorithm
1323  **/
1324 
1326 {
1327  const HashAlgo *hashAlgo;
1328 
1329  //Check hash algorithm identifier
1330  switch(hashAlgoId)
1331  {
1332 #if (TLS_MD5_SUPPORT == ENABLED)
1333  //MD5 hash identifier?
1334  case TLS_HASH_ALGO_MD5:
1335  hashAlgo = MD5_HASH_ALGO;
1336  break;
1337 #endif
1338 #if (TLS_SHA1_SUPPORT == ENABLED)
1339  //SHA-1 hash identifier?
1340  case TLS_HASH_ALGO_SHA1:
1341  hashAlgo = SHA1_HASH_ALGO;
1342  break;
1343 #endif
1344 #if (TLS_SHA224_SUPPORT == ENABLED)
1345  //SHA-224 hash identifier?
1346  case TLS_HASH_ALGO_SHA224:
1347  hashAlgo = SHA224_HASH_ALGO;
1348  break;
1349 #endif
1350 #if (TLS_SHA256_SUPPORT == ENABLED)
1351  //SHA-256 hash identifier?
1352  case TLS_HASH_ALGO_SHA256:
1353  hashAlgo = SHA256_HASH_ALGO;
1354  break;
1355 #endif
1356 #if (TLS_SHA384_SUPPORT == ENABLED)
1357  //SHA-384 hash identifier?
1358  case TLS_HASH_ALGO_SHA384:
1359  hashAlgo = SHA384_HASH_ALGO;
1360  break;
1361 #endif
1362 #if (TLS_SHA512_SUPPORT == ENABLED)
1363  //SHA-512 hash identifier?
1364  case TLS_HASH_ALGO_SHA512:
1365  hashAlgo = SHA512_HASH_ALGO;
1366  break;
1367 #endif
1368 #if (TLS_SM3_SUPPORT == ENABLED)
1369  //SM3 hash identifier?
1370  case TLS_HASH_ALGO_SM3:
1371  hashAlgo = SM3_HASH_ALGO;
1372  break;
1373 #endif
1374  //Unknown hash identifier?
1375  default:
1376  hashAlgo = NULL;
1377  break;
1378  }
1379 
1380  //Return a pointer to the corresponding hash algorithm
1381  return hashAlgo;
1382 }
1383 
1384 
1385 /**
1386  * @brief Get the EC domain parameters that match the specified named curve
1387  * @param[in] context Pointer to the TLS context
1388  * @param[in] namedCurve Elliptic curve identifier
1389  * @return Elliptic curve parameters
1390  **/
1391 
1392 const EcCurve *tlsGetCurve(TlsContext *context, uint16_t namedCurve)
1393 {
1394  uint_t i;
1395  const EcCurve *curve;
1396 
1397  //Default elliptic curve parameters
1398  curve = NULL;
1399 
1400 #if (TLS_ECDH_SUPPORT == ENABLED)
1401  //Check named curve
1402  switch(namedCurve)
1403  {
1404 #if (TLS_SECP160K1_SUPPORT == ENABLED)
1405  //secp160k1 elliptic curve?
1406  case TLS_GROUP_SECP160K1:
1407  curve = ecGetCurve(SECP160K1_OID, sizeof(SECP160K1_OID));
1408  break;
1409 #endif
1410 #if (TLS_SECP160R1_SUPPORT == ENABLED)
1411  //secp160r1 elliptic curve?
1412  case TLS_GROUP_SECP160R1:
1413  curve = ecGetCurve(SECP160R1_OID, sizeof(SECP160R1_OID));
1414  break;
1415 #endif
1416 #if (TLS_SECP160R2_SUPPORT == ENABLED)
1417  //secp160r2 elliptic curve?
1418  case TLS_GROUP_SECP160R2:
1419  curve = ecGetCurve(SECP160R2_OID, sizeof(SECP160R2_OID));
1420  break;
1421 #endif
1422 #if (TLS_SECP192K1_SUPPORT == ENABLED)
1423  //secp192k1 elliptic curve?
1424  case TLS_GROUP_SECP192K1:
1425  curve = ecGetCurve(SECP192K1_OID, sizeof(SECP192K1_OID));
1426  break;
1427 #endif
1428 #if (TLS_SECP192R1_SUPPORT == ENABLED)
1429  //secp192r1 elliptic curve?
1430  case TLS_GROUP_SECP192R1:
1431  curve = ecGetCurve(SECP192R1_OID, sizeof(SECP192R1_OID));
1432  break;
1433 #endif
1434 #if (TLS_SECP224K1_SUPPORT == ENABLED)
1435  //secp224k1 elliptic curve?
1436  case TLS_GROUP_SECP224K1:
1437  curve = ecGetCurve(SECP224K1_OID, sizeof(SECP224K1_OID));
1438  break;
1439 #endif
1440 #if (TLS_SECP224R1_SUPPORT == ENABLED)
1441  //secp224r1 elliptic curve?
1442  case TLS_GROUP_SECP224R1:
1443  curve = ecGetCurve(SECP224R1_OID, sizeof(SECP224R1_OID));
1444  break;
1445 #endif
1446 #if (TLS_SECP256K1_SUPPORT == ENABLED)
1447  //secp256k1 elliptic curve?
1448  case TLS_GROUP_SECP256K1:
1449  curve = ecGetCurve(SECP256K1_OID, sizeof(SECP256K1_OID));
1450  break;
1451 #endif
1452 #if (TLS_SECP256R1_SUPPORT == ENABLED)
1453  //secp256r1 elliptic curve?
1454  case TLS_GROUP_SECP256R1:
1455  curve = ecGetCurve(SECP256R1_OID, sizeof(SECP256R1_OID));
1456  break;
1457 #endif
1458 #if (TLS_SECP384R1_SUPPORT == ENABLED)
1459  //secp384r1 elliptic curve?
1460  case TLS_GROUP_SECP384R1:
1461  curve = ecGetCurve(SECP384R1_OID, sizeof(SECP384R1_OID));
1462  break;
1463 #endif
1464 #if (TLS_SECP521R1_SUPPORT == ENABLED)
1465  //secp521r1 elliptic curve?
1466  case TLS_GROUP_SECP521R1:
1467  curve = ecGetCurve(SECP521R1_OID, sizeof(SECP521R1_OID));
1468  break;
1469 #endif
1470 #if (TLS_BRAINPOOLP256R1_SUPPORT == ENABLED)
1471  //brainpoolP256r1 elliptic curve?
1475  break;
1476 #endif
1477 #if (TLS_BRAINPOOLP384R1_SUPPORT == ENABLED)
1478  //brainpoolP384r1 elliptic curve?
1482  break;
1483 #endif
1484 #if (TLS_BRAINPOOLP512R1_SUPPORT == ENABLED)
1485  //brainpoolP512r1 elliptic curve?
1489  break;
1490 #endif
1491 #if (TLS_SM2_SUPPORT == ENABLED)
1492  //SM2 elliptic curve?
1493  case TLS_GROUP_CURVE_SM2:
1494  curve = ecGetCurve(SM2_OID, sizeof(SM2_OID));
1495  break;
1496 #endif
1497 #if (TLS_X25519_SUPPORT == ENABLED)
1498  //Curve25519 elliptic curve?
1499  case TLS_GROUP_X25519:
1500  curve = ecGetCurve(X25519_OID, sizeof(X25519_OID));
1501  break;
1502 #endif
1503 #if (TLS_X448_SUPPORT == ENABLED)
1504  //Curve448 elliptic curve?
1505  case TLS_GROUP_X448:
1506  curve = ecGetCurve(X448_OID, sizeof(X448_OID));
1507  break;
1508 #endif
1509  //Unknown elliptic curve identifier?
1510  default:
1511  curve = NULL;
1512  break;
1513  }
1514 #endif
1515 
1516  //Restrict the use of certain elliptic curves
1517  if(context->numSupportedGroups > 0)
1518  {
1519  //Loop through the list of allowed named groups
1520  for(i = 0; i < context->numSupportedGroups; i++)
1521  {
1522  //Compare named groups
1523  if(context->supportedGroups[i] == namedCurve)
1524  break;
1525  }
1526 
1527  //Check whether the use of the elliptic curve is restricted
1528  if(i >= context->numSupportedGroups)
1529  {
1530  curve = NULL;
1531  }
1532  }
1533 
1534  //Return the elliptic curve parameters, if any
1535  return curve;
1536 }
1537 
1538 
1539 /**
1540  * @brief Get the named curve that matches the specified OID
1541  * @param[in] oid Object identifier
1542  * @param[in] length OID length
1543  * @return Named curve
1544  **/
1545 
1546 TlsNamedGroup tlsGetNamedCurve(const uint8_t *oid, size_t length)
1547 {
1548  TlsNamedGroup namedCurve;
1549 
1550  //Default named curve
1551  namedCurve = TLS_GROUP_NONE;
1552 
1553 #if (TLS_ECDSA_SIGN_SUPPORT == ENABLED)
1554  //Invalid parameters?
1555  if(oid == NULL || length == 0)
1556  {
1557  namedCurve = TLS_GROUP_NONE;
1558  }
1559 #if (TLS_SECP160K1_SUPPORT == ENABLED)
1560  //secp160k1 elliptic curve?
1561  else if(OID_COMP(oid, length, SECP160K1_OID) == 0)
1562  {
1563  namedCurve = TLS_GROUP_SECP160K1;
1564  }
1565 #endif
1566 #if (TLS_SECP160R1_SUPPORT == ENABLED)
1567  //secp160r1 elliptic curve?
1568  else if(OID_COMP(oid, length, SECP160R1_OID) == 0)
1569  {
1570  namedCurve = TLS_GROUP_SECP160R1;
1571  }
1572 #endif
1573 #if (TLS_SECP160R2_SUPPORT == ENABLED)
1574  //secp160r2 elliptic curve?
1575  else if(OID_COMP(oid, length, SECP160R2_OID) == 0)
1576  {
1577  namedCurve = TLS_GROUP_SECP160R2;
1578  }
1579 #endif
1580 #if (TLS_SECP192K1_SUPPORT == ENABLED)
1581  //secp192k1 elliptic curve?
1582  else if(OID_COMP(oid, length, SECP192K1_OID) == 0)
1583  {
1584  namedCurve = TLS_GROUP_SECP192K1;
1585  }
1586 #endif
1587 #if (TLS_SECP192R1_SUPPORT == ENABLED)
1588  //secp192r1 elliptic curve?
1589  else if(OID_COMP(oid, length, SECP192R1_OID) == 0)
1590  {
1591  namedCurve = TLS_GROUP_SECP192R1;
1592  }
1593 #endif
1594 #if (TLS_SECP224K1_SUPPORT == ENABLED)
1595  //secp224k1 elliptic curve?
1596  else if(OID_COMP(oid, length, SECP224K1_OID) == 0)
1597  {
1598  namedCurve = TLS_GROUP_SECP224K1;
1599  }
1600 #endif
1601 #if (TLS_SECP224R1_SUPPORT == ENABLED)
1602  //secp224r1 elliptic curve?
1603  else if(OID_COMP(oid, length, SECP224R1_OID) == 0)
1604  {
1605  namedCurve = TLS_GROUP_SECP224R1;
1606  }
1607 #endif
1608 #if (TLS_SECP256K1_SUPPORT == ENABLED)
1609  //secp256k1 elliptic curve?
1610  else if(OID_COMP(oid, length, SECP256K1_OID) == 0)
1611  {
1612  namedCurve = TLS_GROUP_SECP256K1;
1613  }
1614 #endif
1615 #if (TLS_SECP256R1_SUPPORT == ENABLED)
1616  //secp256r1 elliptic curve?
1617  else if(OID_COMP(oid, length, SECP256R1_OID) == 0)
1618  {
1619  namedCurve = TLS_GROUP_SECP256R1;
1620  }
1621 #endif
1622 #if (TLS_SECP384R1_SUPPORT == ENABLED)
1623  //secp384r1 elliptic curve?
1624  else if(OID_COMP(oid, length, SECP384R1_OID) == 0)
1625  {
1626  namedCurve = TLS_GROUP_SECP384R1;
1627  }
1628 #endif
1629 #if (TLS_SECP521R1_SUPPORT == ENABLED)
1630  //secp521r1 elliptic curve?
1631  else if(OID_COMP(oid, length, SECP521R1_OID) == 0)
1632  {
1633  namedCurve = TLS_GROUP_SECP521R1;
1634  }
1635 #endif
1636 #if (TLS_BRAINPOOLP256R1_SUPPORT == ENABLED)
1637  //brainpoolP256r1 elliptic curve?
1638  else if(OID_COMP(oid, length, BRAINPOOLP256R1_OID) == 0)
1639  {
1640  namedCurve = TLS_GROUP_BRAINPOOLP256R1;
1641  }
1642 #endif
1643 #if (TLS_BRAINPOOLP384R1_SUPPORT == ENABLED)
1644  //brainpoolP384r1 elliptic curve?
1645  else if(OID_COMP(oid, length, BRAINPOOLP384R1_OID) == 0)
1646  {
1647  namedCurve = TLS_GROUP_BRAINPOOLP384R1;
1648  }
1649 #endif
1650 #if (TLS_BRAINPOOLP512R1_SUPPORT == ENABLED)
1651  //brainpoolP512r1 elliptic curve?
1652  else if(OID_COMP(oid, length, BRAINPOOLP512R1_OID) == 0)
1653  {
1654  namedCurve = TLS_GROUP_BRAINPOOLP512R1;
1655  }
1656 #endif
1657 #if (TLS_SM2_SUPPORT == ENABLED)
1658  //SM2 elliptic curve?
1659  else if(OID_COMP(oid, length, SM2_OID) == 0)
1660  {
1661  namedCurve = TLS_GROUP_CURVE_SM2;
1662  }
1663 #endif
1664  //Unknown identifier?
1665  else
1666  {
1667  namedCurve = TLS_GROUP_NONE;
1668  }
1669 #endif
1670 
1671  //Return the corresponding named curve
1672  return namedCurve;
1673 }
1674 
1675 
1676 /**
1677  * @brief Compute overhead caused by encryption
1678  * @param[in] encryptionEngine Pointer to the encryption engine
1679  * @param[in] payloadLen Length of the payload, in bytes
1680  * @return Overhead, in bytes, caused by encryption
1681  **/
1682 
1684  size_t payloadLen)
1685 {
1686  size_t n;
1687 
1688  //Initialize variable
1689  n = 0;
1690 
1691  //Message authentication?
1692  if(encryptionEngine->hashAlgo != NULL)
1693  n += encryptionEngine->hashAlgo->digestSize;
1694 
1695  //Check cipher mode of operation
1696  if(encryptionEngine->cipherMode == CIPHER_MODE_CBC)
1697  {
1698  //TLS 1.1 and 1.2 use an explicit IV
1699  if(encryptionEngine->version >= TLS_VERSION_1_1)
1700  {
1701  n += encryptionEngine->recordIvLen;
1702  }
1703 
1704  //Padding is added to force the length of the plaintext to be an integral
1705  //multiple of the cipher's block length
1706  n += encryptionEngine->cipherAlgo->blockSize -
1707  ((payloadLen + n) % encryptionEngine->cipherAlgo->blockSize);
1708  }
1709  else if(encryptionEngine->cipherMode == CIPHER_MODE_CCM ||
1710  encryptionEngine->cipherMode == CIPHER_MODE_GCM)
1711  {
1712  //Consider the explicit nonce and the authentication tag
1713  n += encryptionEngine->recordIvLen + encryptionEngine->authTagLen;
1714  }
1715  else if(encryptionEngine->cipherMode == CIPHER_MODE_CHACHA20_POLY1305)
1716  {
1717  //Consider the authentication tag only
1718  n += encryptionEngine->authTagLen;
1719  }
1720  else
1721  {
1722  //Stream ciphers do not cause any overhead
1723  }
1724 
1725  //Return the total overhead caused by encryption
1726  return n;
1727 }
1728 
1729 
1730 /**
1731  * @brief DNS hostname verification
1732  * @param[in] name Pointer to the hostname
1733  * @param[in] length Length of the hostname
1734  * @return The function returns TRUE is the name is a valid DNS hostname
1735  **/
1736 
1738 {
1739  size_t i;
1740  bool_t valid;
1741 
1742  //Initialize flag
1743  valid = TRUE;
1744 
1745  //Loop through the hostname
1746  for(i = 0; i < length && valid; i++)
1747  {
1748  //DNS hostnames must start with a letter, end with a letter or
1749  //digit, and have as interior characters only letters, digits,
1750  //and hyphen (refer to RFC 1034, section 3.5)
1751  if(name[i] == '-' || name[i] == '.')
1752  {
1753  //Valid character
1754  }
1755  else if(name[i] >= '0' && name[i] <= '9')
1756  {
1757  //Valid character
1758  }
1759  else if(name[i] >= 'A' && name[i] <= 'Z')
1760  {
1761  //Valid character
1762  }
1763  else if(name[i] >= 'a' && name[i] <= 'z')
1764  {
1765  //Valid character
1766  }
1767  else if(name[i] == '_')
1768  {
1769  //In practice, DNS allows underscores to be used in hostnames
1770  }
1771  else
1772  {
1773  //Invalid character
1774  valid = FALSE;
1775  }
1776  }
1777 
1778  //Return TRUE is the name is a valid DNS hostname
1779  return valid;
1780 }
1781 
1782 #endif
@ TLS_GROUP_BRAINPOOLP512R1_TLS13
Definition: tls.h:1484
#define TLS_MAX_RECORD_LENGTH
Definition: tls.h:977
#define tlsAllocMem(size)
Definition: tls.h:888
size_t ticketLen
Length of the session ticket.
Definition: tls.h:2208
TLS helper functions.
@ TLS_ALERT_DECODE_ERROR
Definition: tls.h:1144
const uint8_t tls11DowngradeRandom[8]
Definition: tls13_misc.c:53
@ TLS_ALERT_UNEXPECTED_MESSAGE
Definition: tls.h:1129
@ TLS_GROUP_BRAINPOOLP256R1_TLS13
Definition: tls.h:1482
#define SHA256_HASH_ALGO
Definition: sha256.h:49
int bool_t
Definition: compiler_port.h:63
@ TLS_GROUP_SECP160R2
Definition: tls.h:1468
TLS cipher suites.
uint16_t cipherSuite
Cipher suite identifier.
Definition: tls.h:2005
error_t tlsSaveSessionTicket(const TlsContext *context, TlsSessionState *session)
Save session ticket.
Definition: tls_misc.c:502
@ TLS_ALERT_CERTIFICATE_REQUIRED
Definition: tls.h:1161
#define SHA1_HASH_ALGO
Definition: sha1.h:49
const HashAlgo * tlsGetHashAlgo(TlsHashAlgo hashAlgoId)
Get the hash algorithm that matches the specified identifier.
Definition: tls_misc.c:1325
@ CIPHER_MODE_CBC
Definition: crypto.h:1063
@ ERROR_WOULD_BLOCK
Definition: error.h:96
uint8_t a
Definition: ndp.h:411
#define SHA512_HASH_ALGO
Definition: sha512.h:49
const uint8_t X25519_OID[3]
Definition: ec_curves.c:108
Arbitrary precision integer.
Definition: mpi.h:102
error_t tlsGenerateSessionId(TlsContext *context, size_t length)
Generate a random session identifier.
Definition: tls_misc.c:268
@ TLS_ALERT_ILLEGAL_PARAMETER
Definition: tls.h:1141
@ ERROR_VERSION_NOT_SUPPORTED
Definition: error.h:67
uint8_t * ticket
Session ticket.
Definition: tls.h:2207
@ ERROR_UNKNOWN_CERTIFICATE
Definition: error.h:238
@ ERROR_NOT_IMPLEMENTED
Definition: error.h:66
@ ERROR_ILLEGAL_PARAMETER
Definition: error.h:244
const EcCurve * tlsGetCurve(TlsContext *context, uint16_t namedCurve)
Get the EC domain parameters that match the specified named curve.
Definition: tls_misc.c:1392
@ ERROR_DECRYPTION_FAILED
Definition: error.h:243
uint8_t secret[TLS_MASTER_SECRET_SIZE]
Master secret (TLS 1.2) or ticket PSK (TLS 1.3)
Definition: tls.h:2201
@ TLS_ALERT_UNSUPPORTED_EXTENSION
Definition: tls.h:1155
@ ERROR_UNEXPECTED_MESSAGE
Definition: error.h:195
TlsState
TLS FSM states.
Definition: tls.h:1540
OID (Object Identifier)
uint8_t p
Definition: ndp.h:300
@ CIPHER_MODE_GCM
Definition: crypto.h:1068
@ TLS_ALERT_RECORD_OVERFLOW
Definition: tls.h:1132
error_t tlsSelectVersion(TlsContext *context, uint16_t version)
Set the TLS version to be used.
Definition: tls_misc.c:305
TlsConnectionEnd
TLS connection end.
Definition: tls.h:1011
systime_t timestamp
Time stamp to manage entry lifetime.
Definition: tls.h:2200
@ TLS_GROUP_SECP256K1
Definition: tls.h:1473
#define TRUE
Definition: os_port.h:50
uint8_t data[]
Definition: ethernet.h:224
@ TLS_GROUP_SECP256R1
Definition: tls.h:1474
@ TLS_GROUP_CURVE_SM2
Definition: tls.h:1492
size_t digestSize
Definition: crypto.h:1157
@ TLS_GROUP_SECP224K1
Definition: tls.h:1471
const uint8_t SECP224R1_OID[5]
Definition: ec_curves.c:66
@ TLS_TRANSPORT_PROTOCOL_DATAGRAM
Definition: tls.h:1000
error_t tlsSaveSessionId(const TlsContext *context, TlsSessionState *session)
Save session ID.
Definition: tls_misc.c:430
error_t ecImportPublicKey(EcPublicKey *key, const EcCurve *curve, const uint8_t *input, size_t length, EcPublicKeyFormat format)
Import an EC public key.
Definition: ec.c:263
const uint8_t BRAINPOOLP512R1_OID[9]
Definition: ec_curves.c:100
Structure describing a cipher suite.
Definition: tls.h:2175
const uint8_t SECP160K1_OID[5]
Definition: ec_curves.c:54
@ TLS_HASH_ALGO_SHA1
Definition: tls.h:1262
size_t sessionIdLen
Length of the session identifier.
Definition: tls.h:2204
const uint8_t SECP256K1_OID[5]
Definition: ec_curves.c:68
error_t tlsWriteMpi(const Mpi *a, uint8_t *data, size_t *length)
Encode a multiple precision integer to an opaque vector.
Definition: tls_misc.c:1101
TlsHashAlgo
Hash algorithms.
Definition: tls.h:1259
@ ERROR_HANDSHAKE_FAILED
Definition: error.h:234
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
#define SM3_HASH_ALGO
Definition: sm3.h:49
const uint8_t BRAINPOOLP384R1_OID[9]
Definition: ec_curves.c:96
const uint8_t tls12DowngradeRandom[8]
Definition: tls13_misc.c:59
char_t name[]
@ EC_PUBLIC_KEY_FORMAT_X963
Definition: ec.h:386
error_t ecExportPublicKey(const EcPublicKey *key, uint8_t *output, size_t *written, EcPublicKeyFormat format)
Export an EC public key.
Definition: ec.c:378
@ ERROR_BAD_RECORD_MAC
Definition: error.h:232
@ ERROR_NOT_CONFIGURED
Definition: error.h:218
__weak_func error_t gcmInit(GcmContext *context, const CipherAlgo *cipherAlgo, void *cipherContext)
Initialize GCM context.
Definition: gcm.c:99
@ ERROR_UNSUPPORTED_CERTIFICATE
Definition: error.h:237
#define osStrlen(s)
Definition: os_port.h:168
uint8_t version
Definition: coap_common.h:177
uint16_t errorCode
Definition: tftp_common.h:138
@ TLS_ALERT_DECRYPT_ERROR
Definition: tls.h:1145
@ ERROR_INVALID_VERSION
Definition: error.h:118
@ TLS_ENCRYPTION_LEVEL_EARLY_DATA
Definition: tls.h:1585
@ TLS_GROUP_BRAINPOOLP256R1
Definition: tls.h:1477
const uint8_t SECP256R1_OID[8]
Definition: ec_curves.c:70
@ TLS_GROUP_X448
Definition: tls.h:1481
@ TLS_ENCRYPTION_LEVEL_HANDSHAKE
Definition: tls.h:1586
uint8_t oid[]
Definition: lldp_tlv.h:300
const uint8_t SECP224K1_OID[5]
Definition: ec_curves.c:64
@ TLS_HASH_ALGO_SHA224
Definition: tls.h:1263
CipherAlgoInit init
Definition: crypto.h:1196
@ TLS_ALERT_UNKNOWN_CA
Definition: tls.h:1142
const uint8_t SECP521R1_OID[5]
Definition: ec_curves.c:74
TlsEncryptionLevel
Encryption level.
Definition: tls.h:1583
@ TLS_HASH_ALGO_SHA512
Definition: tls.h:1266
uint16_t cipherSuite
Cipher suite identifier.
Definition: tls.h:2199
#define DTLS_VERSION_1_0
Definition: dtls_misc.h:35
#define FALSE
Definition: os_port.h:46
error_t tlsSendAlert(TlsContext *context, uint8_t level, uint8_t description)
Send Alert message.
Definition: tls_common.c:524
#define osMemcpy(dest, src, length)
Definition: os_port.h:144
@ ERROR_UNSUPPORTED_EXTENSION
Definition: error.h:246
@ TLS_HASH_ALGO_SM3
Definition: tls.h:1268
#define TlsContext
Definition: tls.h:36
error_t
Error codes.
Definition: error.h:43
@ TLS_ALERT_BAD_RECORD_MAC
Definition: tls.h:1130
@ TLS_CONNECTION_END_SERVER
Definition: tls.h:1013
void tlsFreeEncryptionEngine(TlsEncryptionEngine *encryptionEngine)
Release encryption engine.
Definition: tls_misc.c:1047
#define TLS_VERSION_1_2
Definition: tls.h:96
@ TLS_GROUP_NONE
Definition: tls.h:1451
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
error_t tlsSelectCipherSuite(TlsContext *context, uint16_t identifier)
Set cipher suite.
Definition: tls_misc.c:333
#define STORE16BE(a, p)
Definition: cpu_endian.h:262
#define MD5_HASH_ALGO
Definition: md5.h:49
error_t mpiImport(Mpi *r, const uint8_t *input, size_t length, MpiFormat format)
Octet string to integer conversion.
Definition: mpi.c:714
@ TLS_ALERT_UNSUPPORTED_CERTIFICATE
Definition: tls.h:1137
bool_t tlsCheckDnsHostname(const char_t *name, size_t length)
DNS hostname verification.
Definition: tls_misc.c:1737
@ ERROR_MISSING_EXTENSION
Definition: error.h:245
#define TLS_VERSION_1_3
Definition: tls.h:97
Handshake message processing (TLS client and server)
@ TLS_HASH_ALGO_SHA384
Definition: tls.h:1265
@ TLS_GROUP_SECP384R1
Definition: tls.h:1475
@ TLS_GROUP_SECP192K1
Definition: tls.h:1469
@ ERROR_BAD_CERTIFICATE
Definition: error.h:236
@ TLS_ALERT_MISSING_EXTENSION
Definition: tls.h:1154
@ TLS_HASH_ALGO_SHA256
Definition: tls.h:1264
#define SSL_VERSION_3_0
Definition: tls.h:93
const uint8_t SECP160R1_OID[5]
Definition: ec_curves.c:56
error_t mpiExport(const Mpi *a, uint8_t *output, size_t length, MpiFormat format)
Integer to octet string conversion.
Definition: mpi.c:811
size_t tlsComputeEncryptionOverhead(TlsEncryptionEngine *encryptionEngine, size_t payloadLen)
Compute overhead caused by encryption.
Definition: tls_misc.c:1683
TlsNamedGroup tlsGetNamedCurve(const uint8_t *oid, size_t length)
Get the named curve that matches the specified OID.
Definition: tls_misc.c:1546
uint8_t length
Definition: tcp.h:375
@ CIPHER_MODE_STREAM
Definition: crypto.h:1061
@ TLS_GROUP_BRAINPOOLP512R1
Definition: tls.h:1479
@ TLS_GROUP_SECP160K1
Definition: tls.h:1466
#define MIN(a, b)
Definition: os_port.h:63
@ TLS_GROUP_SECP521R1
Definition: tls.h:1476
@ TLS_GROUP_SECP192R1
Definition: tls.h:1470
@ TLS_ALERT_PROTOCOL_VERSION
Definition: tls.h:1148
@ TLS_GROUP_SECP160R1
Definition: tls.h:1467
const uint8_t SECP192R1_OID[8]
Definition: ec_curves.c:62
#define TLS_MASTER_SECRET_SIZE
Definition: tls.h:836
@ TLS_GROUP_BRAINPOOLP384R1_TLS13
Definition: tls.h:1483
@ TLS_HASH_ALGO_MD5
Definition: tls.h:1261
const uint8_t SECP384R1_OID[5]
Definition: ec_curves.c:72
uint8_t secret[TLS_MASTER_SECRET_SIZE]
Master secret.
Definition: tls.h:2006
uint8_t random[32]
Definition: tls.h:1893
const uint8_t X448_OID[3]
Definition: ec_curves.c:110
bool_t tlsIsCipherSuiteAcceptable(const TlsCipherSuiteInfo *cipherSuite, uint16_t minVersion, uint16_t maxVersion, TlsTransportProtocol transportProtocol)
Check whether a cipher suite can be used with a given protocol version.
error_t tlsRestoreSessionId(TlsContext *context, const TlsSessionState *session)
Restore a TLS session using session ID.
Definition: tls_misc.c:556
char_t * serverName
ServerName extension.
Definition: tls.h:2218
error_t tlsWriteEcPoint(const EcPublicKey *publicKey, uint8_t *data, size_t *length)
Encode an EC point to an opaque vector.
Definition: tls_misc.c:1177
EC public key.
Definition: ec.h:421
#define SHA384_HASH_ALGO
Definition: sha384.h:45
#define TRACE_DEBUG(...)
Definition: debug.h:119
const uint8_t SECP192K1_OID[5]
Definition: ec_curves.c:60
@ ERROR_TIMEOUT
Definition: error.h:95
char char_t
Definition: compiler_port.h:55
@ TLS_GROUP_SECP224R1
Definition: tls.h:1472
GCM context.
Definition: gcm.h:64
#define TLS_VERSION_1_1
Definition: tls.h:95
@ CIPHER_MODE_CCM
Definition: crypto.h:1067
error_t tlsReadMpi(Mpi *a, const uint8_t *data, size_t size, size_t *length)
Read a multiple precision integer from an opaque vector.
Definition: tls_misc.c:1135
@ TLS_ALERT_BAD_CERTIFICATE
Definition: tls.h:1136
size_t contextSize
Definition: crypto.h:1193
@ TLS_ALERT_INAPPROPRIATE_FALLBACK
Definition: tls.h:1151
#define TRACE_DEBUG_ARRAY(p, a, n)
Definition: debug.h:120
@ ERROR_NO_APPLICATION_PROTOCOL
Definition: error.h:248
#define OID_COMP(oid1, oidLen1, oid2)
Definition: oid.h:42
@ TLS_ALERT_LEVEL_FATAL
Definition: tls.h:1118
TLS session state.
Definition: tls.h:2197
uint8_t n
#define DTLS_VERSION_1_3
Definition: dtls_misc.h:37
@ ERROR_READ_FAILED
Definition: error.h:224
@ ERROR_WRITE_FAILED
Definition: error.h:223
uint8_t sessionId[32]
Session identifier.
Definition: tls.h:2203
@ ERROR_INAPPROPRIATE_FALLBACK
Definition: error.h:247
#define TLS_VERSION_1_0
Definition: tls.h:94
@ TLS_ALERT_CERTIFICATE_EXPIRED
Definition: tls.h:1139
const TlsCipherSuiteInfo tlsSupportedCipherSuites[]
@ TLS_STATE_INIT
Definition: tls.h:1541
@ ERROR_CERTIFICATE_EXPIRED
Definition: error.h:239
@ TLS_ALERT_NO_APPLICATION_PROTOCOL
Definition: tls.h:1162
@ TLS_CONNECTION_END_CLIENT
Definition: tls.h:1012
Common interface for encryption algorithms.
Definition: crypto.h:1191
DtlsSequenceNumber
Definition: dtls_misc.h:143
@ MPI_FORMAT_BIG_ENDIAN
Definition: mpi.h:93
TLS (Transport Layer Security)
uint8_t identifier[]
error_t tlsReadEcPoint(EcPublicKey *publicKey, const EcCurve *curve, const uint8_t *data, size_t size, size_t *length)
Read an EC point from an opaque vector.
Definition: tls_misc.c:1216
uint16_t version
TLS protocol version.
Definition: tls.h:2198
uint8_t s
Definition: igmp_common.h:234
const EcCurve * ecGetCurve(const uint8_t *oid, size_t length)
Get the elliptic curve that matches the specified OID.
Definition: ec_curves.c:5888
@ TLS_GROUP_X25519
Definition: tls.h:1480
const uint8_t SECP160R2_OID[5]
Definition: ec_curves.c:58
@ ERROR_UNKNOWN_CA
Definition: error.h:241
TLS 1.3 key schedule.
#define SHA224_HASH_ALGO
Definition: sha224.h:45
Common interface for hash algorithms.
Definition: crypto.h:1151
FFDHE key exchange.
#define EcCurve
Definition: ec.h:346
@ CIPHER_MODE_NULL
Definition: crypto.h:1060
@ CIPHER_MODE_CHACHA20_POLY1305
Definition: crypto.h:1070
void tlsProcessError(TlsContext *context, error_t errorCode)
Translate an error code to an alert message.
Definition: tls_misc.c:74
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.
uint16_t payloadLen
Definition: ipv6.h:301
const char_t * tlsGetVersionName(uint16_t version)
Convert TLS version to string representation.
Definition: tls_misc.c:1264
uint_t tlsGetNumSupportedCipherSuites(void)
Determine the number of cipher suites supported.
void tlsChangeState(TlsContext *context, TlsState newState)
Update TLS state.
Definition: tls_misc.c:54
TlsNamedGroup
Named groups.
Definition: tls.h:1450
@ ERROR_RECORD_OVERFLOW
Definition: error.h:233
@ ERROR_DECODING_FAILED
Definition: error.h:242
unsigned int uint_t
Definition: compiler_port.h:57
#define LOAD16BE(p)
Definition: cpu_endian.h:186
#define osMemset(p, value, length)
Definition: os_port.h:138
#define tlsFreeMem(p)
Definition: tls.h:893
@ TLS_ALERT_HANDSHAKE_FAILURE
Definition: tls.h:1134
@ ERROR_CERTIFICATE_REQUIRED
Definition: error.h:136
error_t tlsGenerateRandomValue(TlsContext *context, uint8_t *random)
Generate client or server random value.
Definition: tls_misc.c:207
@ TLS_ALERT_INTERNAL_ERROR
Definition: tls.h:1150
@ ERROR_INVALID_SIGNATURE
Definition: error.h:228
#define osStrcpy(s1, s2)
Definition: os_port.h:210
#define DTLS_VERSION_1_2
Definition: dtls_misc.h:36
#define TlsEncryptionEngine
Definition: tls.h:40
bool_t extendedMasterSecret
Extended master secret computation.
Definition: tls.h:2205
@ ERROR_INVALID_SESSION
Definition: error.h:287
@ ERROR_INVALID_TICKET
Definition: error.h:229
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
TlsSequenceNumber
Definition: tls.h:1606
@ TLS_GROUP_BRAINPOOLP384R1
Definition: tls.h:1478
__weak_func error_t tlsInitEncryptionEngine(TlsContext *context, TlsEncryptionEngine *encryptionEngine, TlsConnectionEnd entity, TlsEncryptionLevel level, const uint8_t *secret)
Initialize encryption engine.
Definition: tls_misc.c:673
@ TLS_ALERT_CERTIFICATE_UNKNOWN
Definition: tls.h:1140
const uint8_t BRAINPOOLP256R1_OID[9]
Definition: ec_curves.c:88
const uint8_t SM2_OID[8]
Definition: ec_curves.c:106
uint_t mpiGetByteLength(const Mpi *a)
Get the actual length in bytes.
Definition: mpi.c:216
systime_t osGetSystemTime(void)
Retrieve system time.
error_t tlsRestoreSessionTicket(TlsContext *context, const TlsSessionState *session)
Restore a TLS session using session ticket.
Definition: tls_misc.c:605
@ TLS_STATE_CLOSED
Definition: tls.h:1574