tls_client_extensions.c
Go to the documentation of this file.
1 /**
2  * @file tls_client_extensions.c
3  * @brief Formatting and parsing of extensions (TLS client)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2025 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.5.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_client_extensions.h"
38 #include "tls_client_misc.h"
39 #include "tls_extensions.h"
40 #include "tls_ffdhe.h"
41 #include "tls_misc.h"
42 #include "debug.h"
43 
44 //Check TLS library configuration
45 #if (TLS_SUPPORT == ENABLED && TLS_CLIENT_SUPPORT == ENABLED)
46 
47 //List of supported ECDHE or FFDHE groups
48 const uint16_t tlsSupportedGroups[] =
49 {
80 };
81 
82 
83 /**
84  * @brief Format SupportedVersions extension
85  * @param[in] context Pointer to the TLS context
86  * @param[in] p Output stream where to write the SupportedVersions extension
87  * @param[out] written Total number of bytes that have been written
88  * @return Error code
89  **/
90 
92  uint8_t *p, size_t *written)
93 {
94  size_t n = 0;
95 
96 #if (TLS_MAX_VERSION >= TLS_VERSION_1_2 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
97  //In TLS 1.2, the client can indicate its version preferences in the
98  //SupportedVersions extension, in preference to the legacy_version field
99  //of the ClientHello
100  if(context->versionMax >= TLS_VERSION_1_2)
101  {
102  TlsExtension *extension;
103  TlsSupportedVersionList *supportedVersionList;
104 
105  //Add the SupportedVersions extension
106  extension = (TlsExtension *) p;
107  //Type of the extension
108  extension->type = HTONS(TLS_EXT_SUPPORTED_VERSIONS);
109 
110  //Point to the extension data field
111  supportedVersionList = (TlsSupportedVersionList *) extension->value;
112 
113  //The extension contains a list of supported versions in preference
114  //order, with the most preferred version first
115  n = 0;
116 
117 #if (DTLS_SUPPORT == ENABLED)
118  //DTLS protocol?
119  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM)
120  {
121  //Check whether DTLS 1.2 is supported
122  if(context->versionMax >= TLS_VERSION_1_2 &&
123  context->versionMin <= TLS_VERSION_1_2)
124  {
125  supportedVersionList->value[n++] = HTONS(DTLS_VERSION_1_2);
126  }
127 
128  //Check whether DTLS 1.0 is supported
129  if(context->versionMax >= TLS_VERSION_1_1 &&
130  context->versionMin <= TLS_VERSION_1_1)
131  {
132  supportedVersionList->value[n++] = HTONS(DTLS_VERSION_1_0);
133  }
134  }
135  else
136 #endif
137  //TLS protocol?
138  {
139  //Check whether TLS 1.3 is supported
140  if(context->versionMax >= TLS_VERSION_1_3 &&
141  context->versionMin <= TLS_VERSION_1_3)
142  {
143  supportedVersionList->value[n++] = HTONS(TLS_VERSION_1_3);
144  }
145 
146  //Check whether TLS 1.2 is supported
147  if(context->versionMax >= TLS_VERSION_1_2 &&
148  context->versionMin <= TLS_VERSION_1_2)
149  {
150  supportedVersionList->value[n++] = HTONS(TLS_VERSION_1_2);
151  }
152 
153  //Check whether TLS 1.1 is supported
154  if(context->versionMax >= TLS_VERSION_1_1 &&
155  context->versionMin <= TLS_VERSION_1_1)
156  {
157  supportedVersionList->value[n++] = HTONS(TLS_VERSION_1_1);
158  }
159 
160  //Check whether TLS 1.0 is supported
161  if(context->versionMax >= TLS_VERSION_1_0 &&
162  context->versionMin <= TLS_VERSION_1_0)
163  {
164  supportedVersionList->value[n++] = HTONS(TLS_VERSION_1_0);
165  }
166  }
167 
168  //Compute the length, in bytes, of the list
169  n *= sizeof(uint16_t);
170  //Fix the length of the list
171  supportedVersionList->length = (uint8_t) n;
172 
173  //Consider the length field that precedes the list
174  n += sizeof(TlsSupportedVersionList);
175  //Fix the length of the extension
176  extension->length = htons(n);
177 
178  //Compute the length, in bytes, of the SupportedVersions extension
179  n += sizeof(TlsExtension);
180  }
181 #endif
182 
183  //Total number of bytes that have been written
184  *written = n;
185 
186  //Successful processing
187  return NO_ERROR;
188 }
189 
190 
191 /**
192  * @brief Format SNI extension
193  * @param[in] context Pointer to the TLS context
194  * @param[in] p Output stream where to write the ServerName extension
195  * @param[out] written Total number of bytes that have been written
196  * @return Error code
197  **/
198 
200  uint8_t *p, size_t *written)
201 {
202  size_t n = 0;
203 
204 #if (TLS_SNI_SUPPORT == ENABLED)
205  //In order to provide the server name, clients may include a ServerName
206  //extension
207  if(context->serverName != NULL)
208  {
209  //Determine the length of the server name
210  n = osStrlen(context->serverName);
211 
212  //The server name must be a valid DNS hostname
213  if(tlsCheckDnsHostname(context->serverName, n))
214  {
215  TlsExtension *extension;
216  TlsServerNameList *serverNameList;
217  TlsServerName *serverName;
218 
219  //Add SNI (Server Name Indication) extension
220  extension = (TlsExtension *) p;
221  //Type of the extension
222  extension->type = HTONS(TLS_EXT_SERVER_NAME);
223 
224  //Point to the list of server names
225  serverNameList = (TlsServerNameList *) extension->value;
226  //In practice, current client implementations only send one name
227  serverName = (TlsServerName *) serverNameList->value;
228 
229  //Fill in the type and the length fields
230  serverName->type = TLS_NAME_TYPE_HOSTNAME;
231  serverName->length = htons(n);
232  //Copy server name
233  osMemcpy(serverName->hostname, context->serverName, n);
234 
235  //Compute the length, in byte, of the structure
236  n += sizeof(TlsServerName);
237  //Fix the length of the list
238  serverNameList->length = htons(n);
239 
240  //Consider the 2-byte length field that precedes the list
241  n += sizeof(TlsServerNameList);
242  //Fix the length of the extension
243  extension->length = htons(n);
244 
245  //Compute the length, in bytes, of the ServerName extension
246  n += sizeof(TlsExtension);
247  }
248  else
249  {
250  //The server name is not a valid DNS hostname
251  n = 0;
252  }
253  }
254 #endif
255 
256  //Total number of bytes that have been written
257  *written = n;
258 
259  //Successful processing
260  return NO_ERROR;
261 }
262 
263 
264 /**
265  * @brief Format MaxFragmentLength extension
266  * @param[in] context Pointer to the TLS context
267  * @param[in] p Output stream where to write the MaxFragmentLength extension
268  * @param[out] written Total number of bytes that have been written
269  * @return Error code
270  **/
271 
273  uint8_t *p, size_t *written)
274 {
275  size_t n = 0;
276 
277 #if (TLS_MAX_FRAG_LEN_SUPPORT == ENABLED)
278  //In order to negotiate smaller maximum fragment lengths, clients may
279  //include a MaxFragmentLength extension
280  if(context->maxFragLen == 512 || context->maxFragLen == 1024 ||
281  context->maxFragLen == 2048 || context->maxFragLen == 4096)
282  {
283  TlsExtension *extension;
284 
285  //Add the MaxFragmentLength extension
286  extension = (TlsExtension *) p;
287  //Type of the extension
288  extension->type = HTONS(TLS_EXT_MAX_FRAGMENT_LENGTH);
289 
290  //Set the maximum fragment length
291  switch(context->maxFragLen)
292  {
293  case 512:
294  extension->value[0] = TLS_MAX_FRAGMENT_LENGTH_512;
295  break;
296 
297  case 1024:
298  extension->value[0] = TLS_MAX_FRAGMENT_LENGTH_1024;
299  break;
300 
301  case 2048:
302  extension->value[0] = TLS_MAX_FRAGMENT_LENGTH_2048;
303  break;
304 
305  default:
306  extension->value[0] = TLS_MAX_FRAGMENT_LENGTH_4096;
307  break;
308  }
309 
310  //The extension data field contains a single byte
311  n = sizeof(uint8_t);
312  //Fix the length of the extension
313  extension->length = htons(n);
314 
315  //Compute the length, in bytes, of the MaxFragmentLength extension
316  n += sizeof(TlsExtension);
317  }
318 #endif
319 
320  //Total number of bytes that have been written
321  *written = n;
322 
323  //Successful processing
324  return NO_ERROR;
325 }
326 
327 
328 /**
329  * @brief Format RecordSizeLimit extension
330  * @param[in] context Pointer to the TLS context
331  * @param[in] p Output stream where to write the RecordSizeLimit extension
332  * @param[out] written Total number of bytes that have been written
333  * @return Error code
334  **/
335 
337  uint8_t *p, size_t *written)
338 {
339  size_t n = 0;
340 
341 #if (TLS_RECORD_SIZE_LIMIT_SUPPORT == ENABLED)
342  size_t recordSizeLimit;
343  TlsExtension *extension;
344 
345  //Add the RecordSizeLimit extension
346  extension = (TlsExtension *) p;
347  //Type of the extension
348  extension->type = HTONS(TLS_EXT_RECORD_SIZE_LIMIT);
349 
350  //An endpoint must not send a value higher than the protocol-defined
351  //maximum record size (refer to RFC 8449, section 4)
352  recordSizeLimit = MIN(context->rxBufferMaxLen, TLS_MAX_RECORD_LENGTH);
353 
354  //Check whether TLS 1.3 is supported
355  if(context->versionMax >= TLS_VERSION_1_3 &&
356  (context->transportProtocol == TLS_TRANSPORT_PROTOCOL_STREAM ||
357  context->transportProtocol == TLS_TRANSPORT_PROTOCOL_EAP))
358  {
359  //The value includes the content type and padding added in TLS 1.3
360  recordSizeLimit++;
361  }
362 
363  //The value of RecordSizeLimit is the maximum size of record in octets
364  //that the endpoint is willing to receive
365  STORE16BE(recordSizeLimit, extension->value);
366 
367  //The extension data field contains a 16-bit unsigned integer
368  n = sizeof(uint16_t);
369  //Fix the length of the extension
370  extension->length = htons(n);
371 
372  //Compute the length, in bytes, of the RecordSizeLimit extension
373  n += sizeof(TlsExtension);
374 #endif
375 
376  //Total number of bytes that have been written
377  *written = n;
378 
379  //Successful processing
380  return NO_ERROR;
381 }
382 
383 
384 /**
385  * @brief Format SupportedGroups extension
386  * @param[in] context Pointer to the TLS context
387  * @param[in] p Output stream where to write the SupportedGroups extension
388  * @param[out] written Total number of bytes that have been written
389  * @return Error code
390  **/
391 
393  size_t *written)
394 {
395  size_t n = 0;
396 
397 #if (TLS_ECDH_SUPPORT == ENABLED || TLS_FFDHE_SUPPORT == ENABLED || \
398  TLS_MLKEM_SUPPORT == ENABLED || TLS_HYBRID_SUPPORT == ENABLED)
399  uint_t i;
400  uint_t numSupportedGroups;
401  const uint16_t *supportedGroups;
402  TlsExtension *extension;
403  TlsSupportedGroupList *supportedGroupList;
404 
405  //Add the SupportedGroups extension
406  extension = (TlsExtension *) p;
407  //Type of the extension
408  extension->type = HTONS(TLS_EXT_SUPPORTED_GROUPS);
409 
410  //Point to the list of supported groups
411  supportedGroupList = (TlsSupportedGroupList *) extension->value;
412 
413  //Any preferred ECDHE or FFDHE groups?
414  if(context->numSupportedGroups > 0)
415  {
416  //Point to the list of preferred named groups
417  supportedGroups = context->supportedGroups;
418  numSupportedGroups = context->numSupportedGroups;
419  }
420  else
421  {
422  //Point to the list of default named groups
423  supportedGroups = tlsSupportedGroups;
424  numSupportedGroups = arraysize(tlsSupportedGroups);
425  }
426 
427  //The groups are ordered according to client's preferences
428  n = 0;
429 
430  //Loop through the list of named groups
431  for(i = 0; i < numSupportedGroups; i++)
432  {
433 #if (TLS_FFDHE_SUPPORT == ENABLED)
434  //Finite field group?
435  if(tlsGetFfdheGroup(context, supportedGroups[i]) != NULL)
436  {
437  //Any Diffie-Hellman cipher suite proposed by the client?
438  if((context->cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_DH) != 0 ||
439  (context->cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_TLS13) != 0)
440  {
441  //Add the current named group to the list
442  supportedGroupList->value[n++] = htons(supportedGroups[i]);
443  }
444  }
445  else
446 #endif
447 #if (TLS_ECDH_SUPPORT == ENABLED)
448  //Elliptic curve group?
449  if(tlsGetCurve(context, supportedGroups[i]) != NULL)
450  {
451  //SM2 elliptic curve?
452  if(supportedGroups[i] == TLS_GROUP_CURVE_SM2)
453  {
454  //Any ShangMi cipher suite proposed by the client?
455  if((context->cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_SM) != 0)
456  {
457  //Add the current named group to the list
458  supportedGroupList->value[n++] = htons(supportedGroups[i]);
459  }
460  }
461  else
462  {
463  //Any ECDH cipher suite proposed by the client?
464  if((context->cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_ECDH) != 0 ||
465  (context->cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_TLS13) != 0)
466  {
467  //Add the current named group to the list
468  supportedGroupList->value[n++] = htons(supportedGroups[i]);
469  }
470  }
471  }
472  else
473 #endif
474 #if (TLS_MLKEM_SUPPORT == ENABLED)
475  //ML-KEM key exchange method?
476  if(tls13GetMlkemAlgo(context, supportedGroups[i]) != NULL)
477  {
478  //Any TLS 1.3 cipher suite proposed by the client?
479  if((context->cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_TLS13) != 0)
480  {
481  //Add the current named group to the list
482  supportedGroupList->value[n++] = htons(supportedGroups[i]);
483  }
484  }
485  else
486 #endif
487 #if (TLS_HYBRID_SUPPORT == ENABLED)
488  //Hybrid key exchange method?
489  if(tls13GetTraditionalAlgo(context, supportedGroups[i]) != NULL &&
490  tls13GetNextGenAlgo(context, supportedGroups[i]) != NULL)
491  {
492  //Any TLS 1.3 cipher suite proposed by the client?
493  if((context->cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_TLS13) != 0)
494  {
495  //Add the current named group to the list
496  supportedGroupList->value[n++] = htons(supportedGroups[i]);
497  }
498  }
499  else
500 #endif
501  //Unknown group?
502  {
503  //Discard current named group
504  }
505  }
506 
507  //If the client supports and wants ECDHE and FFDHE key exchanges, it must
508  //use a single SupportedGroups extension to include all supported groups
509  //(both ECDHE and FFDHE groups)
510  if(n > 0)
511  {
512  //Compute the length, in bytes, of the list
513  n *= 2;
514  //Fix the length of the list
515  supportedGroupList->length = htons(n);
516 
517  //Consider the 2-byte length field that precedes the list
518  n += sizeof(TlsSupportedGroupList);
519  //Fix the length of the extension
520  extension->length = htons(n);
521 
522  //Compute the length, in bytes, of the SupportedGroups extension
523  n += sizeof(TlsExtension);
524  }
525 #endif
526 
527  //Total number of bytes that have been written
528  *written = n;
529 
530  //Successful processing
531  return NO_ERROR;
532 }
533 
534 
535 /**
536  * @brief Format EcPointFormats extension
537  * @param[in] context Pointer to the TLS context
538  * @param[in] p Output stream where to write the EcPointFormats extension
539  * @param[out] written Total number of bytes that have been written
540  * @return Error code
541  **/
542 
544  uint8_t *p, size_t *written)
545 {
546  size_t n = 0;
547 
548 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
549  //TLS 1.3 has removed point format negotiation in favor of a single point
550  //format for each curve (refer to RFC 8446, section 1.2)
551  if(context->versionMin <= TLS_VERSION_1_2)
552  {
553 #if (TLS_ECDH_ANON_KE_SUPPORT == ENABLED || TLS_ECDHE_RSA_KE_SUPPORT == ENABLED || \
554  TLS_ECDHE_ECDSA_KE_SUPPORT == ENABLED || TLS_ECDHE_PSK_KE_SUPPORT == ENABLED)
555  //A client that proposes ECC cipher suites in its ClientHello message
556  //should send the EcPointFormats extension
557  if((context->cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_ECDH) != 0)
558  {
559  TlsExtension *extension;
560  TlsEcPointFormatList *ecPointFormatList;
561 
562  //Add the EcPointFormats extension
563  extension = (TlsExtension *) p;
564  //Type of the extension
565  extension->type = HTONS(TLS_EXT_EC_POINT_FORMATS);
566 
567  //Point to the list of supported EC point formats
568  ecPointFormatList = (TlsEcPointFormatList *) extension->value;
569  //Items in the list are ordered according to client's preferences
570  n = 0;
571 
572  //The client can parse only the uncompressed point format...
573  ecPointFormatList->value[n++] = TLS_EC_POINT_FORMAT_UNCOMPRESSED;
574  //Fix the length of the list
575  ecPointFormatList->length = (uint8_t) n;
576 
577  //Consider the length field that precedes the list
578  n += sizeof(TlsEcPointFormatList);
579  //Fix the length of the extension
580  extension->length = htons(n);
581 
582  //Compute the length, in bytes, of the EcPointFormats extension
583  n += sizeof(TlsExtension);
584  }
585 #endif
586  }
587 #endif
588 
589  //Total number of bytes that have been written
590  *written = n;
591 
592  //Successful processing
593  return NO_ERROR;
594 }
595 
596 
597 /**
598  * @brief Format ALPN extension
599  * @param[in] context Pointer to the TLS context
600  * @param[in] p Output stream where to write the ALPN extension
601  * @param[out] written Total number of bytes that have been written
602  * @return Error code
603  **/
604 
606  uint8_t *p, size_t *written)
607 {
608  size_t n = 0;
609 
610 #if (TLS_ALPN_SUPPORT == ENABLED)
611  //The ALPN extension contains the list of protocols advertised by the
612  //client, in descending order of preference
613  if(context->protocolList != NULL)
614  {
615  uint_t i;
616  uint_t j;
617  TlsExtension *extension;
618  TlsProtocolName *protocolName;
619  TlsProtocolNameList *protocolNameList;
620 
621  //Add ALPN (Application-Layer Protocol Negotiation) extension
622  extension = (TlsExtension *) p;
623  //Type of the extension
624  extension->type = HTONS(TLS_EXT_ALPN);
625 
626  //Point to the list of protocol names
627  protocolNameList = (TlsProtocolNameList *) extension->value;
628 
629  //Move back to the beginning of the list
630  i = 0;
631  j = 0;
632  n = 0;
633 
634  //Parse the list of supported protocols
635  do
636  {
637  //Delimiter character found?
638  if(context->protocolList[i] == ',' || context->protocolList[i] == '\0')
639  {
640  //Discard empty tokens
641  if((i - j) > 0)
642  {
643  //Point to the protocol name
644  protocolName = (TlsProtocolName *) (protocolNameList->value + n);
645 
646  //Fill in the length field
647  protocolName->length = i - j;
648  //Copy protocol name
649  osMemcpy(protocolName->value, context->protocolList + j, i - j);
650 
651  //Adjust the length of the list
652  n += sizeof(TlsProtocolName) + i - j;
653  }
654 
655  //Move to the next token
656  j = i + 1;
657  }
658 
659  //Loop until the NULL character is reached
660  } while(context->protocolList[i++] != '\0');
661 
662  //Fix the length of the list
663  protocolNameList->length = htons(n);
664 
665  //Consider the 2-byte length field that precedes the list
666  n += sizeof(TlsProtocolNameList);
667  //Fix the length of the extension
668  extension->length = htons(n);
669 
670  //Compute the length, in bytes, of the ALPN extension
671  n += sizeof(TlsExtension);
672  }
673 #endif
674 
675  //Total number of bytes that have been written
676  *written = n;
677 
678  //Successful processing
679  return NO_ERROR;
680 }
681 
682 
683 /**
684  * @brief Format ClientCertType extension
685  * @param[in] context Pointer to the TLS context
686  * @param[in] p Output stream where to write the ClientCertType extension
687  * @param[out] written Total number of bytes that have been written
688  * @return Error code
689  **/
690 
692  uint8_t *p, size_t *written)
693 {
694  size_t n = 0;
695 
696 #if (TLS_RAW_PUBLIC_KEY_SUPPORT == ENABLED)
697  TlsExtension *extension;
698  TlsCertTypeList *clientCertTypeList;
699 
700  //Add the ClientCertType extension
701  extension = (TlsExtension *) p;
702  //Type of the extension
703  extension->type = HTONS(TLS_EXT_CLIENT_CERT_TYPE);
704 
705  //The ClientCertType extension in the ClientHello indicates the certificate
706  //types the client is able to provide to the server, when requested using a
707  //CertificateRequest message
708  clientCertTypeList = (TlsCertTypeList *) extension->value;
709 
710  //The ClientCertType extension carries a list of supported certificate
711  //types, sorted by client preference
712  n = 0;
713 
714  //Raw public key type
715  clientCertTypeList->value[n++] = TLS_CERT_FORMAT_RAW_PUBLIC_KEY;
716  //X.509 certificate type
717  clientCertTypeList->value[n++] = TLS_CERT_FORMAT_X509;
718 
719  //Fix the length of the list
720  clientCertTypeList->length = (uint8_t) n;
721 
722  //Consider the length field that precedes the list
723  n += sizeof(TlsCertTypeList);
724  //Fix the length of the extension
725  extension->length = htons(n);
726 
727  //Compute the length, in bytes, of the ClientCertType extension
728  n += sizeof(TlsExtension);
729 #endif
730 
731  //Total number of bytes that have been written
732  *written = n;
733 
734  //Successful processing
735  return NO_ERROR;
736 }
737 
738 
739 /**
740  * @brief Format ServerCertType extension
741  * @param[in] context Pointer to the TLS context
742  * @param[in] p Output stream where to write the ServerCertType extension
743  * @param[out] written Total number of bytes that have been written
744  * @return Error code
745  **/
746 
748  uint8_t *p, size_t *written)
749 {
750  size_t n = 0;
751 
752 #if (TLS_RAW_PUBLIC_KEY_SUPPORT == ENABLED)
753  //Ensure the client is able to process raw public keys
754  if(context->rpkVerifyCallback != NULL)
755  {
756  TlsExtension *extension;
757  TlsCertTypeList *serverCertTypeList;
758 
759  //Add the ServerCertType extension
760  extension = (TlsExtension *) p;
761  //Type of the extension
762  extension->type = HTONS(TLS_EXT_SERVER_CERT_TYPE);
763 
764  //The ServerCertType extension in the ClientHello indicates the types of
765  //certificates the client is able to process when provided by the server
766  //in a subsequent certificate payload
767  serverCertTypeList = (TlsCertTypeList *) extension->value;
768 
769  //The ServerCertType extension carries a list of supported certificate
770  //types, sorted by client preference
771  n = 0;
772 
773  //Raw public key type
774  serverCertTypeList->value[n++] = TLS_CERT_FORMAT_RAW_PUBLIC_KEY;
775  //X.509 certificate type
776  serverCertTypeList->value[n++] = TLS_CERT_FORMAT_X509;
777 
778  //Fix the length of the list
779  serverCertTypeList->length = (uint8_t) n;
780 
781  //Consider the length field that precedes the list
782  n += sizeof(TlsCertTypeList);
783  //Fix the length of the extension
784  extension->length = htons(n);
785 
786  //Compute the length, in bytes, of the ServerCertType extension
787  n += sizeof(TlsExtension);
788  }
789 #endif
790 
791  //Total number of bytes that have been written
792  *written = n;
793 
794  //Successful processing
795  return NO_ERROR;
796 }
797 
798 
799 /**
800  * @brief Format EncryptThenMac extension
801  * @param[in] context Pointer to the TLS context
802  * @param[in] p Output stream where to write the EncryptThenMac extension
803  * @param[out] written Total number of bytes that have been written
804  * @return Error code
805  **/
806 
808  uint8_t *p, size_t *written)
809 {
810  size_t n = 0;
811 
812 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
813  //In versions of TLS prior to TLS 1.3, this extension is used to negotiate
814  //encrypt-then-MAC construction rather than the default MAC-then-encrypt
815  if(context->versionMax >= TLS_VERSION_1_0 &&
816  context->versionMin <= TLS_VERSION_1_2)
817  {
818 #if (TLS_ENCRYPT_THEN_MAC_SUPPORT == ENABLED)
819  TlsExtension *extension;
820 
821  //Add the EncryptThenMac extension
822  extension = (TlsExtension *) p;
823  //Type of the extension
824  extension->type = HTONS(TLS_EXT_ENCRYPT_THEN_MAC);
825 
826  //The extension data field of this extension shall be empty (refer to
827  //RFC 7366, section 2)
828  extension->length = HTONS(0);
829 
830  //Compute the length, in bytes, of the EncryptThenMac extension
831  n = sizeof(TlsExtension);
832 #endif
833  }
834 #endif
835 
836  //Total number of bytes that have been written
837  *written = n;
838 
839  //Successful processing
840  return NO_ERROR;
841 }
842 
843 
844 /**
845  * @brief Format ExtendedMasterSecret extension
846  * @param[in] context Pointer to the TLS context
847  * @param[in] p Output stream where to write the ExtendedMasterSecret extension
848  * @param[out] written Total number of bytes that have been written
849  * @return Error code
850  **/
851 
853  uint8_t *p, size_t *written)
854 {
855  size_t n = 0;
856 
857 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
858  //Implementations which support both TLS 1.3 and earlier versions should
859  //indicate the use of the ExtendedMasterSecret extension whenever TLS 1.3
860  //is used (refer to RFC 8446, appendix D)
861  if(context->versionMax >= TLS_VERSION_1_0 &&
862  context->versionMin <= TLS_VERSION_1_2)
863  {
864 #if (TLS_EXT_MASTER_SECRET_SUPPORT == ENABLED)
865  TlsExtension *extension;
866 
867  //Add the ExtendedMasterSecret extension
868  extension = (TlsExtension *) p;
869  //Type of the extension
870  extension->type = HTONS(TLS_EXT_EXTENDED_MASTER_SECRET);
871 
872  //The extension data field of this extension is empty
873  extension->length = HTONS(0);
874 
875  //Compute the length, in bytes, of the ExtendedMasterSecret extension
876  n = sizeof(TlsExtension);
877 #endif
878  }
879 #endif
880 
881  //Total number of bytes that have been written
882  *written = n;
883 
884  //Successful processing
885  return NO_ERROR;
886 }
887 
888 
889 /**
890  * @brief Format SessionTicket extension
891  * @param[in] context Pointer to the TLS context
892  * @param[in] p Output stream where to write the SessionTicket extension
893  * @param[out] written Total number of bytes that have been written
894  * @return Error code
895  **/
896 
898  uint8_t *p, size_t *written)
899 {
900  size_t n = 0;
901 
902 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
903  //In versions of TLS prior to TLS 1.3, the SessionTicket extension is used
904  //to resume a TLS session without requiring session-specific state at the
905  //TLS server
906  if(context->versionMin <= TLS_VERSION_1_2)
907  {
908 #if (TLS_TICKET_SUPPORT == ENABLED)
909  //Check whether session ticket mechanism is enabled
910  if(context->sessionTicketEnabled)
911  {
912  TlsExtension *extension;
913 
914  //Add the SessionTicket extension
915  extension = (TlsExtension *) p;
916  //Type of the extension
917  extension->type = HTONS(TLS_EXT_SESSION_TICKET);
918 
919  //Valid ticket?
920  if(tlsIsTicketValid(context))
921  {
922  //If the client possesses a ticket that it wants to use to resume
923  //a session, then it includes the ticket in the SessionTicket
924  //extension in the ClientHello
925  osMemcpy(extension->value, context->ticket, context->ticketLen);
926 
927  //The extension_data field of SessionTicket extension contains the
928  //ticket
929  n = context->ticketLen;
930  }
931  else
932  {
933  //If the client does not have a ticket and is prepared to receive
934  //one in the NewSessionTicket handshake message, then it must
935  //include a zero-length ticket in the SessionTicket extension
936  n = 0;
937  }
938 
939  //Set the length of the extension
940  extension->length = htons(n);
941 
942  //Compute the length, in bytes, of the SessionTicket extension
943  n += sizeof(TlsExtension);
944  }
945 #endif
946  }
947 #endif
948 
949  //Total number of bytes that have been written
950  *written = n;
951 
952  //Successful processing
953  return NO_ERROR;
954 }
955 
956 
957 /**
958  * @brief Format RenegotiationInfo extension
959  * @param[in] context Pointer to the TLS context
960  * @param[in] p Output stream where to write the RenegotiationInfo extension
961  * @param[out] written Total number of bytes that have been written
962  * @return Error code
963  **/
964 
966  uint8_t *p, size_t *written)
967 {
968  size_t n = 0;
969 
970 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
971  //TLS 1.3 forbids renegotiation
972  if(context->versionMin <= TLS_VERSION_1_2)
973  {
974 #if (TLS_SECURE_RENEGOTIATION_SUPPORT == ENABLED)
975  //Check whether secure renegotiation is enabled
976  if(context->secureRenegoEnabled)
977  {
978  //During secure renegotiation, the client must include the
979  //RenegotiationInfo extension containing the saved client_verify_data
980  if(context->secureRenegoFlag)
981  {
982  TlsExtension *extension;
983  TlsRenegoInfo *renegoInfo;
984 
985  //Determine the length of the verify data
986  n = context->clientVerifyDataLen;
987 
988  //Add the RenegotiationInfo extension
989  extension = (TlsExtension *) p;
990  //Type of the extension
991  extension->type = HTONS(TLS_EXT_RENEGOTIATION_INFO);
992 
993  //Point to the renegotiated_connection field
994  renegoInfo = (TlsRenegoInfo *) extension->value;
995  //Set the length of the verify data
996  renegoInfo->length = (uint8_t) n;
997 
998  //Copy the verify data from the Finished message sent by the client
999  //on the immediately previous handshake
1000  osMemcpy(renegoInfo->value, context->clientVerifyData, n);
1001 
1002  //Consider the length field that precedes the renegotiated_connection
1003  //field
1004  n += sizeof(TlsRenegoInfo);
1005  //Fix the length of the extension
1006  extension->length = htons(n);
1007 
1008  //Compute the length, in bytes, of the RenegotiationInfo extension
1009  n += sizeof(TlsExtension);
1010  }
1011  }
1012 #endif
1013  }
1014 #endif
1015 
1016  //Total number of bytes that have been written
1017  *written = n;
1018 
1019  //Successful processing
1020  return NO_ERROR;
1021 }
1022 
1023 
1024 /**
1025  * @brief Format ClientHello Padding extension
1026  * @param[in] context Pointer to the TLS context
1027  * @param[in] clientHelloLen Actual length of the ClientHello message
1028  * @param[in] p Output stream where to write the ClientHello Padding extension
1029  * @param[out] written Total number of bytes that have been written
1030  * @return Error code
1031  **/
1032 
1034  size_t clientHelloLen, uint8_t *p, size_t *written)
1035 {
1036  size_t n = 0;
1037 
1038 #if (TLS_CLIENT_HELLO_PADDING_SUPPORT == ENABLED)
1039  //TLS protocol?
1040  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_STREAM)
1041  {
1042  TlsExtension *extension;
1043 
1044  //After building a ClientHello as normal, the client can add four bytes
1045  //to the length and test whether the resulting length falls into the
1046  //range 256 to 511 (refer to RFC 7685, section 4)
1047  clientHelloLen += sizeof(TlsHandshake);
1048 
1049  //Check the resulting length
1050  if(clientHelloLen >= 256 && clientHelloLen < 512)
1051  {
1052  //The ClientHello Padding extension will be added in order to push
1053  //the length to (at least) 512 bytes
1054  extension = (TlsExtension *) p;
1055 
1056  //Type of the extension
1057  extension->type = HTONS(TLS_EXT_PADDING);
1058 
1059  //Calculate the length of the padding
1060  if((clientHelloLen + sizeof(TlsExtension)) < 512)
1061  {
1062  n = 512 - sizeof(TlsExtension) - clientHelloLen;
1063  }
1064  else
1065  {
1066  n = 0;
1067  }
1068 
1069  //The padding string consists of an arbitrary number of zero bytes
1070  osMemset(extension->value, 0, n);
1071  //Set the length of the extension
1072  extension->length = htons(n);
1073 
1074  //Compute the length, in bytes, of the padding extension
1075  n += sizeof(TlsExtension);
1076  }
1077  }
1078 #endif
1079 
1080  //Total number of bytes that have been written
1081  *written = n;
1082 
1083  //Successful processing
1084  return NO_ERROR;
1085 }
1086 
1087 
1088 /**
1089  * @brief Parse SNI extension
1090  * @param[in] context Pointer to the TLS context
1091  * @param[in] serverNameList Pointer to the ServerName extension
1092  * @return Error code
1093  **/
1094 
1096  const TlsServerNameList *serverNameList)
1097 {
1098 #if (TLS_SNI_SUPPORT == ENABLED)
1099  //SNI extension found?
1100  if(serverNameList != NULL)
1101  {
1102  //If a client receives an extension type in the ServerHello that it did
1103  //not request in the associated ClientHello, it must abort the handshake
1104  //with an unsupported_extension fatal alert
1105  if(context->serverName == NULL)
1107  }
1108 #endif
1109 
1110  //Successful processing
1111  return NO_ERROR;
1112 }
1113 
1114 
1115 /**
1116  * @brief Parse MaxFragmentLength extension
1117  * @param[in] context Pointer to the TLS context
1118  * @param[in] maxFragLen Pointer to the MaxFragmentLength extension
1119  * @return Error code
1120  **/
1121 
1123  const TlsExtension *maxFragLen)
1124 {
1125 #if (TLS_MAX_FRAG_LEN_SUPPORT == ENABLED)
1126  //MaxFragmentLength extension found?
1127  if(maxFragLen != NULL)
1128  {
1129  size_t n;
1130 
1131  //Retrieve the value advertised by the server
1132  switch(maxFragLen->value[0])
1133  {
1135  n = 512;
1136  break;
1137 
1139  n = 1024;
1140  break;
1141 
1143  n = 2048;
1144  break;
1145 
1147  n = 4096;
1148  break;
1149 
1150  default:
1151  n = 0;
1152  break;
1153  }
1154 
1155  //If a client receives a maximum fragment length negotiation response
1156  //that differs from the length it requested, it must also abort the
1157  //handshake with an illegal_parameter alert
1158  if(n != context->maxFragLen)
1159  return ERROR_ILLEGAL_PARAMETER;
1160  }
1161 #endif
1162 
1163  //Successful processing
1164  return NO_ERROR;
1165 }
1166 
1167 
1168 /**
1169  * @brief Parse RecordSizeLimit extension
1170  * @param[in] context Pointer to the TLS context
1171  * @param[in] recordSizeLimit Pointer to the RecordSizeLimit extension
1172  * @return Error code
1173  **/
1174 
1176  const TlsExtension *recordSizeLimit)
1177 {
1178 #if (TLS_RECORD_SIZE_LIMIT_SUPPORT == ENABLED)
1179  //RecordSizeLimit extension found?
1180  if(recordSizeLimit != NULL)
1181  {
1182  uint16_t n;
1183 
1184  //The value of RecordSizeLimit is the maximum size of record in octets
1185  //that the peer is willing to receive
1186  n = LOAD16BE(recordSizeLimit->value);
1187 
1188  //Endpoints must not send a RecordSizeLimit extension with a value
1189  //smaller than 64
1190  if(n < 64)
1191  {
1192  //An endpoint must treat receipt of a smaller value as a fatal error
1193  //and generate an illegal_parameter alert
1194  return ERROR_ILLEGAL_PARAMETER;
1195  }
1196 
1197  //TLS 1.3 currently selected?
1198  if(context->version == TLS_VERSION_1_3)
1199  {
1200  //The value includes the content type and padding added in TLS 1.3
1201  n--;
1202  }
1203 
1204  //The peer can include any limit up to the protocol-defined limit for
1205  //maximum record size. Even if a larger value is provided by a peer, an
1206  //endpoint must not send records larger than the protocol-defined limit
1207  context->recordSizeLimit = MIN(n, TLS_MAX_RECORD_LENGTH);
1208 
1209  //The RecordSizeLimit extension has been successfully negotiated
1210  context->recordSizeLimitExtReceived = TRUE;
1211  }
1212  else
1213  {
1214  //If this extension is not negotiated, endpoints can send records of any
1215  //size permitted by the protocol or other negotiated extensions
1216  context->recordSizeLimit = TLS_MAX_RECORD_LENGTH;
1217 
1218  //The RecordSizeLimit extension is not supported by the server
1219  context->recordSizeLimitExtReceived = FALSE;
1220  }
1221 #endif
1222 
1223  //Successful processing
1224  return NO_ERROR;
1225 }
1226 
1227 
1228 /**
1229  * @brief Parse EcPointFormats extension
1230  * @param[in] context Pointer to the TLS context
1231  * @param[in] ecPointFormatList Pointer to the EcPointFormats extension
1232  * @return Error code
1233  **/
1234 
1236  const TlsEcPointFormatList *ecPointFormatList)
1237 {
1238  error_t error;
1239 
1240  //Initialize status code
1241  error = NO_ERROR;
1242 
1243 #if (TLS_ECDH_ANON_KE_SUPPORT == ENABLED || TLS_ECDHE_RSA_KE_SUPPORT == ENABLED || \
1244  TLS_ECDHE_ECDSA_KE_SUPPORT == ENABLED || TLS_ECDHE_PSK_KE_SUPPORT == ENABLED)
1245  //EcPointFormats extension found?
1246  if(ecPointFormatList != NULL)
1247  {
1248  uint_t i;
1249 
1250  //Loop through the list of supported EC point formats
1251  for(i = 0; i < ecPointFormatList->length; i++)
1252  {
1253  //Uncompressed point format?
1254  if(ecPointFormatList->value[i] == TLS_EC_POINT_FORMAT_UNCOMPRESSED)
1255  {
1256  break;
1257  }
1258  }
1259 
1260  //If the EcPointFormats extension is sent, it must contain the value 0
1261  //as one of the items in the list of point formats (refer to RFC 4492,
1262  //section 5.2)
1263  if(i >= ecPointFormatList->length)
1264  {
1265  //Report an error
1266  error = ERROR_ILLEGAL_PARAMETER;
1267  }
1268  }
1269 #endif
1270 
1271  //Return status code
1272  return error;
1273 }
1274 
1275 
1276 /**
1277  * @brief Parse ALPN extension
1278  * @param[in] context Pointer to the TLS context
1279  * @param[in] protocolNameList Pointer to the ALPN extension
1280  * @return Error code
1281  **/
1282 
1284  const TlsProtocolNameList *protocolNameList)
1285 {
1286 #if (TLS_ALPN_SUPPORT == ENABLED)
1287  //ALPN extension found?
1288  if(protocolNameList != NULL)
1289  {
1290  size_t length;
1291  const TlsProtocolName *protocolName;
1292 
1293  //If a client receives an extension type in the ServerHello that it
1294  //did not request in the associated ClientHello, it must abort the
1295  //handshake with an unsupported_extension fatal alert
1296  if(context->protocolList == NULL)
1298 
1299  //Retrieve the length of the list
1300  length = ntohs(protocolNameList->length);
1301 
1302  //The list must not be be empty
1303  if(length == 0)
1304  return ERROR_DECODING_FAILED;
1305 
1306  //Point to the selected protocol
1307  protocolName = (TlsProtocolName *) protocolNameList->value;
1308 
1309  //The list must contain exactly one protocol name
1310  if(length < sizeof(TlsProtocolName))
1311  return ERROR_DECODING_FAILED;
1312  if(length != (sizeof(TlsProtocolName) + protocolName->length))
1313  return ERROR_DECODING_FAILED;
1314 
1315  //Retrieve the length of the protocol name
1316  length -= sizeof(TlsProtocolName);
1317 
1318  //Empty strings must not be included in the list
1319  if(length == 0)
1320  return ERROR_DECODING_FAILED;
1321 
1322  //Check whether the protocol is supported by the client
1323  if(!tlsIsAlpnProtocolSupported(context, protocolName->value, length))
1324  {
1325  //Report an error if unknown ALPN protocols are disallowed
1326  if(!context->unknownProtocolsAllowed)
1327  return ERROR_ILLEGAL_PARAMETER;
1328  }
1329 
1330  //Sanity check
1331  if(context->selectedProtocol != NULL)
1332  {
1333  //Release memory
1334  tlsFreeMem(context->selectedProtocol);
1335  context->selectedProtocol = NULL;
1336  }
1337 
1338  //Allocate a memory block to hold the protocol name
1339  context->selectedProtocol = tlsAllocMem(length + 1);
1340  //Failed to allocate memory?
1341  if(context->selectedProtocol == NULL)
1342  return ERROR_OUT_OF_MEMORY;
1343 
1344  //Save protocol name
1345  osMemcpy(context->selectedProtocol, protocolName->value, length);
1346  //Properly terminate the string with a NULL character
1347  context->selectedProtocol[length] = '\0';
1348  }
1349 #endif
1350 
1351  //Successful processing
1352  return NO_ERROR;
1353 }
1354 
1355 
1356 /**
1357  * @brief Parse ClientCertType extension
1358  * @param[in] context Pointer to the TLS context
1359  * @param[in] clientCertType Pointer to the ClientCertType extension
1360  * @return Error code
1361  **/
1362 
1364  const TlsExtension *clientCertType)
1365 {
1366 #if (TLS_RAW_PUBLIC_KEY_SUPPORT == ENABLED)
1367  //ClientCertType extension found?
1368  if(clientCertType != NULL)
1369  {
1370  //The value conveyed in the extension must be selected from one of the
1371  //values provided in the ClientCertType extension sent in the ClientHello
1372  if(clientCertType->value[0] != TLS_CERT_FORMAT_X509 &&
1373  clientCertType->value[0] != TLS_CERT_FORMAT_RAW_PUBLIC_KEY)
1374  {
1375  return ERROR_ILLEGAL_PARAMETER;
1376  }
1377 
1378  //The ClientCertType extension in the ServerHello indicates the type
1379  //of certificates the client is requested to provide in a subsequent
1380  //certificate payload
1381  context->certFormat = (TlsCertificateFormat) clientCertType->value[0];
1382  }
1383 #endif
1384 
1385  //Successful processing
1386  return NO_ERROR;
1387 }
1388 
1389 
1390 /**
1391  * @brief Parse ServerCertType extension
1392  * @param[in] context Pointer to the TLS context
1393  * @param[in] serverCertType Pointer to the ServerCertType extension
1394  * @return Error code
1395  **/
1396 
1398  const TlsExtension *serverCertType)
1399 {
1400 #if (TLS_RAW_PUBLIC_KEY_SUPPORT == ENABLED)
1401  //ServerCertType extension found?
1402  if(serverCertType != NULL)
1403  {
1404  //If a client receives an extension type in the ServerHello that it did
1405  //not request in the associated ClientHello, it must abort the handshake
1406  //with an unsupported_extension fatal alert
1407  if(context->rpkVerifyCallback == NULL &&
1408  serverCertType->value[0] != TLS_CERT_FORMAT_X509)
1409  {
1411  }
1412 
1413  //The value conveyed in the extension must be selected from one of the
1414  //values provided in the ServerCertType extension sent in the ClientHello
1415  if(serverCertType->value[0] != TLS_CERT_FORMAT_X509 &&
1416  serverCertType->value[0] != TLS_CERT_FORMAT_RAW_PUBLIC_KEY)
1417  {
1418  return ERROR_ILLEGAL_PARAMETER;
1419  }
1420 
1421  //With the ServerCertType extension in the ServerHello, the TLS server
1422  //indicates the certificate type carried in the certificate payload
1423  context->peerCertFormat = (TlsCertificateFormat) serverCertType->value[0];
1424  }
1425 #endif
1426 
1427  //Successful processing
1428  return NO_ERROR;
1429 }
1430 
1431 
1432 /**
1433  * @brief Parse EncryptThenMac extension
1434  * @param[in] context Pointer to the TLS context
1435  * @param[in] encryptThenMac Pointer to the EncryptThenMac extension
1436  * @return Error code
1437  **/
1438 
1440  const TlsExtension *encryptThenMac)
1441 {
1442 #if (TLS_ENCRYPT_THEN_MAC_SUPPORT == ENABLED)
1443  //EncryptThenMac extension found?
1444  if(encryptThenMac != NULL)
1445  {
1446  //If a server receives an encrypt-then-MAC request extension from a
1447  //client and then selects a stream or AEAD ciphersuite, it must not send
1448  //an encrypt-then-MAC response extension back to the client (refer to
1449  //RFC 7366, section 3)
1450  if(context->cipherSuite.cipherMode != CIPHER_MODE_CBC)
1451  return ERROR_HANDSHAKE_FAILED;
1452 
1453  //Use encrypt-then-MAC construction rather than the default
1454  //MAC-then-encrypt
1455  context->etmExtReceived = TRUE;
1456  }
1457  else
1458  {
1459  //Use default MAC-then-encrypt construction
1460  context->etmExtReceived = FALSE;
1461  }
1462 #endif
1463 
1464  //Successful processing
1465  return NO_ERROR;
1466 }
1467 
1468 
1469 /**
1470  * @brief Parse ExtendedMasterSecret extension
1471  * @param[in] context Pointer to the TLS context
1472  * @param[in] extendedMasterSecret Pointer to the ExtendedMasterSecret extension
1473  * @return Error code
1474  **/
1475 
1478 {
1479 #if (TLS_EXT_MASTER_SECRET_SUPPORT == ENABLED)
1480  //ExtendedMasterSecret extension found?
1481  if(extendedMasterSecret != NULL)
1482  {
1483  //Abbreviated handshake?
1484  if(context->resume)
1485  {
1486  //If the original session did not use the ExtendedMasterSecret
1487  //extension but the new ServerHello contains the extension, the
1488  //client must abort the handshake
1489  if(!context->emsExtReceived)
1490  return ERROR_HANDSHAKE_FAILED;
1491  }
1492 
1493  //A valid ExtendedMasterSecret extension has been received
1494  context->emsExtReceived = TRUE;
1495  }
1496  else
1497  {
1498  //Abbreviated handshake?
1499  if(context->resume)
1500  {
1501  //If the original session used the ExtendedMasterSecret extension
1502  //but the new ServerHello does not contain the extension, the client
1503  //must abort the handshake
1504  if(context->emsExtReceived)
1505  return ERROR_HANDSHAKE_FAILED;
1506  }
1507 
1508  //The ServerHello does not contain any ExtendedMasterSecret extension
1509  context->emsExtReceived = FALSE;
1510  }
1511 #endif
1512 
1513  //Successful processing
1514  return NO_ERROR;
1515 }
1516 
1517 
1518 /**
1519  * @brief Parse SessionTicket extension
1520  * @param[in] context Pointer to the TLS context
1521  * @param[in] sessionTicket Pointer to the SessionTicket extension
1522  * @return Error code
1523  **/
1524 
1526  const TlsExtension *sessionTicket)
1527 {
1528 #if (TLS_TICKET_SUPPORT == ENABLED)
1529  //SessionTicket extension found?
1530  if(sessionTicket != NULL)
1531  {
1532  //If a client receives an extension type in the ServerHello that it did
1533  //not request in the associated ClientHello, it must abort the handshake
1534  //with an unsupported_extension fatal alert
1535  if(!context->sessionTicketEnabled)
1537 
1538  //The server uses the SessionTicket extension to indicate to the client
1539  //that it will send a new session ticket using the NewSessionTicket
1540  //handshake message
1541  context->sessionTicketExtReceived = TRUE;
1542  }
1543  else
1544  {
1545  //The ServerHello does not contain any SessionTicket extension
1546  context->sessionTicketExtReceived = FALSE;
1547  }
1548 #endif
1549 
1550  //Successful processing
1551  return NO_ERROR;
1552 }
1553 
1554 
1555 /**
1556  * @brief Parse RenegotiationInfo extension
1557  * @param[in] context Pointer to the TLS context
1558  * @param[in] extensions ServerHello extensions offered by the server
1559  * @return Error code
1560  **/
1561 
1564 {
1565 #if (TLS_SECURE_RENEGOTIATION_SUPPORT == ENABLED)
1566  //Initial handshake?
1567  if(context->clientVerifyDataLen == 0)
1568  {
1569  //RenegotiationInfo extension found?
1570  if(extensions->renegoInfo != NULL)
1571  {
1572  //If the extension is present, set the secure_renegotiation flag to TRUE
1573  context->secureRenegoFlag = TRUE;
1574 
1575  //Verify that the length of the renegotiated_connection field is zero
1576  if(extensions->renegoInfo->length != 0)
1577  {
1578  //If it is not, the client must abort the handshake by sending a
1579  //fatal handshake failure alert
1580  return ERROR_HANDSHAKE_FAILED;
1581  }
1582  }
1583  else
1584  {
1585  //If the extension is not present, the server does not support secure
1586  //renegotiation
1587  context->secureRenegoFlag = FALSE;
1588  }
1589  }
1590  //Secure renegotiation?
1591  else
1592  {
1593  //RenegotiationInfo extension found?
1594  if(extensions->renegoInfo != NULL)
1595  {
1596  //Check the length of the renegotiated_connection field
1597  if(extensions->renegoInfo->length != (context->clientVerifyDataLen +
1598  context->serverVerifyDataLen))
1599  {
1600  //The client must abort the handshake
1601  return ERROR_HANDSHAKE_FAILED;
1602  }
1603 
1604  //The client must verify that the first half of the field is equal to
1605  //the saved client_verify_data value
1606  if(osMemcmp(extensions->renegoInfo->value, context->clientVerifyData,
1607  context->clientVerifyDataLen))
1608  {
1609  //If it is not, the client must abort the handshake
1610  return ERROR_HANDSHAKE_FAILED;
1611  }
1612 
1613  //The client must verify that the second half of the field is equal to
1614  //the saved server_verify_data value
1615  if(osMemcmp(extensions->renegoInfo->value + context->clientVerifyDataLen,
1616  context->serverVerifyData, context->serverVerifyDataLen))
1617  {
1618  //If it is not, the client must abort the handshake
1619  return ERROR_HANDSHAKE_FAILED;
1620  }
1621 
1622 #if (TLS_EXT_MASTER_SECRET_SUPPORT == ENABLED)
1623  //ExtendedMasterSecret extension found?
1624  if(extensions->extendedMasterSecret != NULL)
1625  {
1626  //If the initial handshake did not use the ExtendedMasterSecret
1627  //extension but the new ServerHello contains the extension, the
1628  //client must abort the handshake
1629  if(!context->emsExtReceived)
1630  return ERROR_HANDSHAKE_FAILED;
1631  }
1632  else
1633  {
1634  //If the initial handshake used the ExtendedMasterSecret extension
1635  //but the new ServerHello does not contain the extension, the
1636  //client must abort the handshake
1637  if(context->emsExtReceived)
1638  return ERROR_HANDSHAKE_FAILED;
1639  }
1640 #endif
1641  }
1642  else
1643  {
1644  //If the RenegotiationInfo extension is not present, the client
1645  //must abort the handshake
1646  return ERROR_HANDSHAKE_FAILED;
1647  }
1648  }
1649 #endif
1650 
1651  //Successful processing
1652  return NO_ERROR;
1653 }
1654 
1655 #endif
@ TLS_GROUP_X25519_MLKEM768
Definition: tls.h:1456
@ TLS_GROUP_BRAINPOOLP512R1_TLS13
Definition: tls.h:1437
#define TLS_MAX_RECORD_LENGTH
Definition: tls.h:956
#define tlsAllocMem(size)
Definition: tls.h:867
@ TLS_EXT_MAX_FRAGMENT_LENGTH
Definition: tls.h:1320
#define htons(value)
Definition: cpu_endian.h:413
Parsing and checking of TLS extensions.
@ TLS_CERT_FORMAT_RAW_PUBLIC_KEY
Definition: tls.h:1200
TLS helper functions.
TlsServerName
Definition: tls.h:1645
uint8_t extensions[]
Definition: ntp_common.h:207
@ TLS_GROUP_BRAINPOOLP256R1_TLS13
Definition: tls.h:1435
@ TLS_GROUP_SECP160R2
Definition: tls.h:1421
TLS cipher suites.
@ CIPHER_MODE_CBC
Definition: crypto.h:1001
error_t tlsFormatClientSupportedVersionsExtension(TlsContext *context, uint8_t *p, size_t *written)
Format SupportedVersions extension.
error_t tlsFormatClientEcPointFormatsExtension(TlsContext *context, uint8_t *p, size_t *written)
Format EcPointFormats extension.
@ 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:1251
@ TLS_EXT_SUPPORTED_VERSIONS
Definition: tls.h:1356
uint8_t p
Definition: ndp.h:300
Helper functions for TLS client.
@ TLS_GROUP_SECP256K1
Definition: tls.h:1426
#define TRUE
Definition: os_port.h:50
@ TLS_GROUP_SECP256R1
Definition: tls.h:1427
bool_t tlsIsAlpnProtocolSupported(TlsContext *context, const char_t *protocol, size_t length)
Check whether the specified ALPN protocol is supported.
TlsEcPointFormatList
Definition: tls.h:1700
@ TLS_GROUP_CURVE_SM2
Definition: tls.h:1445
@ TLS_GROUP_SECP224K1
Definition: tls.h:1424
error_t tlsParseServerRecordSizeLimitExtension(TlsContext *context, const TlsExtension *recordSizeLimit)
Parse RecordSizeLimit extension.
@ TLS_TRANSPORT_PROTOCOL_DATAGRAM
Definition: tls.h:979
TlsRenegoInfo
Definition: tls.h:1722
error_t tlsFormatClientSessionTicketExtension(TlsContext *context, uint8_t *p, size_t *written)
Format SessionTicket extension.
#define osMemcmp(p1, p2, length)
Definition: os_port.h:156
@ ERROR_HANDSHAKE_FAILED
Definition: error.h:234
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
error_t tlsParseServerSniExtension(TlsContext *context, const TlsServerNameList *serverNameList)
Parse SNI extension.
error_t tlsParseClientCertTypeExtension(TlsContext *context, const TlsExtension *clientCertType)
Parse ClientCertType extension.
#define osStrlen(s)
Definition: os_port.h:168
TlsProtocolNameList
Definition: tls.h:1678
TlsExtension
Definition: tls.h:1611
@ TLS_GROUP_BRAINPOOLP256R1
Definition: tls.h:1430
@ TLS_EXT_SESSION_TICKET
Definition: tls.h:1352
@ TLS_GROUP_X448
Definition: tls.h:1434
@ TLS_CIPHER_SUITE_TYPE_TLS13
@ TLS_GROUP_FFDHE6144
Definition: tls.h:1449
@ TLS_MAX_FRAGMENT_LENGTH_4096
Definition: tls.h:1394
error_t tlsFormatSupportedGroupsExtension(TlsContext *context, uint8_t *p, size_t *written)
Format SupportedGroups extension.
error_t tlsFormatClientSniExtension(TlsContext *context, uint8_t *p, size_t *written)
Format SNI extension.
bool_t extendedMasterSecret
Extended master secret computation.
Definition: tls.h:1926
@ TLS_CIPHER_SUITE_TYPE_SM
#define DTLS_SUPPORT
Definition: dtls_misc.h:41
@ TLS_EXT_SERVER_NAME
Definition: tls.h:1319
error_t tlsParseServerEtmExtension(TlsContext *context, const TlsExtension *encryptThenMac)
Parse EncryptThenMac extension.
#define DTLS_VERSION_1_0
Definition: dtls_misc.h:35
TlsHandshake
Definition: tls.h:1792
#define FALSE
Definition: os_port.h:46
error_t tlsParseServerMaxFragLenExtension(TlsContext *context, const TlsExtension *maxFragLen)
Parse MaxFragmentLength extension.
#define osMemcpy(dest, src, length)
Definition: os_port.h:144
@ ERROR_UNSUPPORTED_EXTENSION
Definition: error.h:246
TlsCertificateFormat
Certificate formats.
Definition: tls.h:1197
@ TLS_EXT_CLIENT_CERT_TYPE
Definition: tls.h:1338
#define TlsContext
Definition: tls.h:36
error_t
Error codes.
Definition: error.h:43
error_t tlsParseServerCertTypeExtension(TlsContext *context, const TlsExtension *serverCertType)
Parse ServerCertType extension.
@ TLS_EXT_EXTENDED_MASTER_SECRET
Definition: tls.h:1342
error_t tlsFormatClientRenegoInfoExtension(TlsContext *context, uint8_t *p, size_t *written)
Format RenegotiationInfo extension.
error_t tlsFormatClientEtmExtension(TlsContext *context, uint8_t *p, size_t *written)
Format EncryptThenMac extension.
@ TLS_GROUP_SECP256R1_MLKEM768
Definition: tls.h:1455
@ TLS_EXT_SUPPORTED_GROUPS
Definition: tls.h:1329
#define TLS_VERSION_1_2
Definition: tls.h:96
const KemAlgo * tls13GetMlkemAlgo(TlsContext *context, uint16_t namedGroup)
Get the ML-KEM algorithm that matches the specified named group.
Definition: tls13_misc.c:1193
@ TLS_EXT_RENEGOTIATION_INFO
Definition: tls.h:1371
const KemAlgo * tls13GetNextGenAlgo(TlsContext *context, uint16_t namedGroup)
Get the next-gen algorithm used by the hybrid key exchange method.
Definition: tls13_misc.c:1330
#define STORE16BE(a, p)
Definition: cpu_endian.h:262
error_t tlsParseServerSessionTicketExtension(TlsContext *context, const TlsExtension *sessionTicket)
Parse SessionTicket extension.
@ TLS_EXT_ENCRYPT_THEN_MAC
Definition: tls.h:1341
@ TLS_GROUP_FFDHE4096
Definition: tls.h:1448
@ TLS_CIPHER_SUITE_TYPE_ECDH
bool_t tlsCheckDnsHostname(const char_t *name, size_t length)
DNS hostname verification.
Definition: tls_misc.c:1596
#define TLS_VERSION_1_3
Definition: tls.h:97
TlsServerNameList
Definition: tls.h:1656
@ TLS_MAX_FRAGMENT_LENGTH_2048
Definition: tls.h:1393
@ TLS_GROUP_SECP384R1
Definition: tls.h:1428
@ TLS_GROUP_SECP192K1
Definition: tls.h:1422
error_t tlsFormatClientAlpnExtension(TlsContext *context, uint8_t *p, size_t *written)
Format ALPN extension.
@ TLS_TRANSPORT_PROTOCOL_EAP
Definition: tls.h:980
TlsProtocolName
Definition: tls.h:1667
error_t tlsParseServerEcPointFormatsExtension(TlsContext *context, const TlsEcPointFormatList *ecPointFormatList)
Parse EcPointFormats extension.
@ TLS_EC_POINT_FORMAT_UNCOMPRESSED
Definition: tls.h:1469
uint8_t length
Definition: tcp.h:375
@ TLS_GROUP_BRAINPOOLP512R1
Definition: tls.h:1432
@ TLS_GROUP_FFDHE2048
Definition: tls.h:1446
const TlsFfdheGroup * tlsGetFfdheGroup(TlsContext *context, uint16_t namedGroup)
Get the FFDHE parameters that match the specified named group.
Definition: tls_ffdhe.c:314
#define MIN(a, b)
Definition: os_port.h:63
@ TLS_GROUP_MLKEM512
Definition: tls.h:1452
#define ENABLED
Definition: os_port.h:37
@ TLS_EXT_EC_POINT_FORMATS
Definition: tls.h:1330
@ TLS_GROUP_SECP521R1
Definition: tls.h:1429
@ TLS_GROUP_SECP192R1
Definition: tls.h:1423
@ TLS_EXT_ALPN
Definition: tls.h:1335
@ TLS_GROUP_FFDHE3072
Definition: tls.h:1447
Hello extensions.
Definition: tls.h:2136
@ TLS_GROUP_SECP160R1
Definition: tls.h:1420
@ TLS_GROUP_BRAINPOOLP384R1_TLS13
Definition: tls.h:1436
Formatting and parsing of extensions (TLS client)
#define ntohs(value)
Definition: cpu_endian.h:421
error_t tlsFormatClientHelloPaddingExtension(TlsContext *context, size_t clientHelloLen, uint8_t *p, size_t *written)
Format ClientHello Padding extension.
@ TLS_GROUP_SECP224R1
Definition: tls.h:1425
#define TLS_VERSION_1_1
Definition: tls.h:95
error_t tlsParseServerEmsExtension(TlsContext *context, const TlsExtension *extendedMasterSecret)
Parse ExtendedMasterSecret extension.
@ TLS_EXT_PADDING
Definition: tls.h:1340
#define HTONS(value)
Definition: cpu_endian.h:410
@ TLS_GROUP_MLKEM1024
Definition: tls.h:1454
uint8_t n
@ TLS_CIPHER_SUITE_TYPE_DH
#define TLS_VERSION_1_0
Definition: tls.h:94
error_t tlsFormatClientEmsExtension(TlsContext *context, uint8_t *p, size_t *written)
Format ExtendedMasterSecret extension.
@ TLS_EXT_SERVER_CERT_TYPE
Definition: tls.h:1339
@ TLS_NAME_TYPE_HOSTNAME
Definition: tls.h:1381
@ TLS_MAX_FRAGMENT_LENGTH_1024
Definition: tls.h:1392
const uint16_t tlsSupportedGroups[]
error_t tlsFormatClientCertTypeListExtension(TlsContext *context, uint8_t *p, size_t *written)
Format ClientCertType extension.
TLS (Transport Layer Security)
@ TLS_GROUP_X25519
Definition: tls.h:1433
@ TLS_TRANSPORT_PROTOCOL_STREAM
Definition: tls.h:978
error_t tlsParseServerRenegoInfoExtension(TlsContext *context, const TlsHelloExtensions *extensions)
Parse RenegotiationInfo extension.
error_t tlsFormatClientRecordSizeLimitExtension(TlsContext *context, uint8_t *p, size_t *written)
Format RecordSizeLimit extension.
const EcCurve * tls13GetTraditionalAlgo(TlsContext *context, uint16_t namedGroup)
Get the traditional algorithm used by the hybrid key exchange method.
Definition: tls13_misc.c:1261
FFDHE key exchange.
error_t tlsParseServerAlpnExtension(TlsContext *context, const TlsProtocolNameList *protocolNameList)
Parse ALPN extension.
error_t tlsFormatServerCertTypeListExtension(TlsContext *context, uint8_t *p, size_t *written)
Format ServerCertType extension.
@ 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
@ TLS_GROUP_MLKEM768
Definition: tls.h:1453
TlsSupportedGroupList
Definition: tls.h:1689
#define tlsFreeMem(p)
Definition: tls.h:872
TlsSupportedVersionList
Definition: tls.h:1633
#define DTLS_VERSION_1_2
Definition: dtls_misc.h:36
@ TLS_GROUP_FFDHE8192
Definition: tls.h:1450
TlsCertTypeList
Definition: tls.h:1711
@ NO_ERROR
Success.
Definition: error.h:44
@ TLS_EXT_RECORD_SIZE_LIMIT
Definition: tls.h:1346
Debugging facilities.
@ TLS_CERT_FORMAT_X509
Definition: tls.h:1198
@ TLS_GROUP_BRAINPOOLP384R1
Definition: tls.h:1431
@ TLS_MAX_FRAGMENT_LENGTH_512
Definition: tls.h:1391
#define arraysize(a)
Definition: os_port.h:71
bool_t tlsIsTicketValid(TlsContext *context)
Check whether a session ticket is valid.
@ TLS_GROUP_SECP384R1_MLKEM1024
Definition: tls.h:1457
error_t tlsFormatClientMaxFragLenExtension(TlsContext *context, uint8_t *p, size_t *written)
Format MaxFragmentLength extension.