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.4
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL TLS_TRACE_LEVEL
33 
34 //Dependencies
35 #include "tls.h"
36 #include "tls_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_QUIC ||
358  context->transportProtocol == TLS_TRANSPORT_PROTOCOL_EAP))
359  {
360  //The value includes the content type and padding added in TLS 1.3
361  recordSizeLimit++;
362  }
363 
364  //The value of RecordSizeLimit is the maximum size of record in octets
365  //that the endpoint is willing to receive
366  STORE16BE(recordSizeLimit, extension->value);
367 
368  //The extension data field contains a 16-bit unsigned integer
369  n = sizeof(uint16_t);
370  //Fix the length of the extension
371  extension->length = htons(n);
372 
373  //Compute the length, in bytes, of the RecordSizeLimit extension
374  n += sizeof(TlsExtension);
375 #endif
376 
377  //Total number of bytes that have been written
378  *written = n;
379 
380  //Successful processing
381  return NO_ERROR;
382 }
383 
384 
385 /**
386  * @brief Format TrustedCaKeys extension
387  * @param[in] context Pointer to the TLS context
388  * @param[in] p Output stream where to write the TrustedCaKeys extension
389  * @param[out] written Total number of bytes that have been written
390  * @return Error code
391  **/
392 
394  size_t *written)
395 {
396  error_t error;
397  size_t n;
398 
399  //Initialize status code
400  error = NO_ERROR;
401  //Initialize length field
402  n = 0;
403 
404 #if (TLS_TRUSTED_CA_KEYS_SUPPORT == ENABLED)
405  //The TrustedCaKeys extension is optional
406  if(context->trustedCaKeysEnabled && context->versionMin <= TLS_VERSION_1_2)
407  {
408  TlsExtension *extension;
409 
410  //Add the TrustedCaKeys extension
411  extension = (TlsExtension *) p;
412  //Type of the extension
413  extension->type = HTONS(TLS_EXT_TRUSTED_CA_KEYS);
414 
415  //"TrustedAuthorities" provides a list of CA root key identifiers that
416  //the client possesses (refer to RFC 6066, section 6)
417  error = tlsFormatTrustedAuthorities(context, extension->value, &n);
418 
419  //Check status code
420  if(!error)
421  {
422  //The list must contains at least one CA root key identifier
423  if(n > sizeof(TlsTrustedAuthorities))
424  {
425  //Fix the length of the extension
426  extension->length = htons(n);
427 
428  //Compute the length, in bytes, of the TrustedCaKeys extension
429  n += sizeof(TlsExtension);
430  }
431  else
432  {
433  //The list of distinguished names is empty
434  n = 0;
435  }
436  }
437  }
438 #endif
439 
440  //Total number of bytes that have been written
441  *written = n;
442 
443  //Return status code
444  return error;
445 }
446 
447 
448 /**
449  * @brief Format SupportedGroups extension
450  * @param[in] context Pointer to the TLS context
451  * @param[in] p Output stream where to write the SupportedGroups extension
452  * @param[out] written Total number of bytes that have been written
453  * @return Error code
454  **/
455 
457  size_t *written)
458 {
459  size_t n = 0;
460 
461 #if (TLS_ECDH_SUPPORT == ENABLED || TLS_FFDHE_SUPPORT == ENABLED || \
462  TLS_MLKEM_SUPPORT == ENABLED || TLS_HYBRID_SUPPORT == ENABLED)
463  uint_t i;
464  uint_t numSupportedGroups;
465  const uint16_t *supportedGroups;
466  TlsExtension *extension;
467  TlsSupportedGroupList *supportedGroupList;
468 
469  //Add the SupportedGroups extension
470  extension = (TlsExtension *) p;
471  //Type of the extension
472  extension->type = HTONS(TLS_EXT_SUPPORTED_GROUPS);
473 
474  //Point to the list of supported groups
475  supportedGroupList = (TlsSupportedGroupList *) extension->value;
476 
477  //Any preferred ECDHE or FFDHE groups?
478  if(context->numSupportedGroups > 0)
479  {
480  //Point to the list of preferred named groups
481  supportedGroups = context->supportedGroups;
482  numSupportedGroups = context->numSupportedGroups;
483  }
484  else
485  {
486  //Point to the list of default named groups
487  supportedGroups = tlsSupportedGroups;
488  numSupportedGroups = arraysize(tlsSupportedGroups);
489  }
490 
491  //The groups are ordered according to client's preferences
492  n = 0;
493 
494  //Loop through the list of named groups
495  for(i = 0; i < numSupportedGroups; i++)
496  {
497 #if (TLS_FFDHE_SUPPORT == ENABLED)
498  //Finite field group?
499  if(tlsGetFfdheGroup(context, supportedGroups[i]) != NULL)
500  {
501  //Any Diffie-Hellman cipher suite proposed by the client?
502  if((context->cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_DH) != 0 ||
503  (context->cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_TLS13) != 0)
504  {
505  //Add the current named group to the list
506  supportedGroupList->value[n++] = htons(supportedGroups[i]);
507  }
508  }
509  else
510 #endif
511 #if (TLS_ECDH_SUPPORT == ENABLED)
512  //Elliptic curve group?
513  if(tlsGetCurve(context, supportedGroups[i]) != NULL)
514  {
515  //SM2 elliptic curve?
516  if(supportedGroups[i] == TLS_GROUP_CURVE_SM2)
517  {
518  //Any ShangMi cipher suite proposed by the client?
519  if((context->cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_SM) != 0)
520  {
521  //Add the current named group to the list
522  supportedGroupList->value[n++] = htons(supportedGroups[i]);
523  }
524  }
525  else
526  {
527  //Any ECDH cipher suite proposed by the client?
528  if((context->cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_ECDH) != 0 ||
529  (context->cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_TLS13) != 0)
530  {
531  //Add the current named group to the list
532  supportedGroupList->value[n++] = htons(supportedGroups[i]);
533  }
534  }
535  }
536  else
537 #endif
538 #if (TLS_MLKEM_SUPPORT == ENABLED)
539  //ML-KEM key exchange method?
540  if(tls13GetMlkemAlgo(context, supportedGroups[i]) != NULL)
541  {
542  //Any TLS 1.3 cipher suite proposed by the client?
543  if((context->cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_TLS13) != 0)
544  {
545  //Add the current named group to the list
546  supportedGroupList->value[n++] = htons(supportedGroups[i]);
547  }
548  }
549  else
550 #endif
551 #if (TLS_HYBRID_SUPPORT == ENABLED)
552  //Hybrid key exchange method?
553  if(tls13GetTraditionalAlgo(context, supportedGroups[i]) != NULL &&
554  tls13GetNextGenAlgo(context, supportedGroups[i]) != NULL)
555  {
556  //Any TLS 1.3 cipher suite proposed by the client?
557  if((context->cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_TLS13) != 0)
558  {
559  //Add the current named group to the list
560  supportedGroupList->value[n++] = htons(supportedGroups[i]);
561  }
562  }
563  else
564 #endif
565  //Unknown group?
566  {
567  //Discard current named group
568  }
569  }
570 
571  //If the client supports and wants ECDHE and FFDHE key exchanges, it must
572  //use a single SupportedGroups extension to include all supported groups
573  //(both ECDHE and FFDHE groups)
574  if(n > 0)
575  {
576  //Compute the length, in bytes, of the list
577  n *= 2;
578  //Fix the length of the list
579  supportedGroupList->length = htons(n);
580 
581  //Consider the 2-byte length field that precedes the list
582  n += sizeof(TlsSupportedGroupList);
583  //Fix the length of the extension
584  extension->length = htons(n);
585 
586  //Compute the length, in bytes, of the SupportedGroups extension
587  n += sizeof(TlsExtension);
588  }
589 #endif
590 
591  //Total number of bytes that have been written
592  *written = n;
593 
594  //Successful processing
595  return NO_ERROR;
596 }
597 
598 
599 /**
600  * @brief Format EcPointFormats extension
601  * @param[in] context Pointer to the TLS context
602  * @param[in] p Output stream where to write the EcPointFormats extension
603  * @param[out] written Total number of bytes that have been written
604  * @return Error code
605  **/
606 
608  uint8_t *p, size_t *written)
609 {
610  size_t n = 0;
611 
612 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
613  //TLS 1.3 has removed point format negotiation in favor of a single point
614  //format for each curve (refer to RFC 8446, section 1.2)
615  if(context->versionMin <= TLS_VERSION_1_2)
616  {
617 #if (TLS_ECDH_ANON_KE_SUPPORT == ENABLED || TLS_ECDHE_RSA_KE_SUPPORT == ENABLED || \
618  TLS_ECDHE_ECDSA_KE_SUPPORT == ENABLED || TLS_ECDHE_PSK_KE_SUPPORT == ENABLED)
619  //A client that proposes ECC cipher suites in its ClientHello message
620  //should send the EcPointFormats extension
621  if((context->cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_ECDH) != 0)
622  {
623  TlsExtension *extension;
624  TlsEcPointFormatList *ecPointFormatList;
625 
626  //Add the EcPointFormats extension
627  extension = (TlsExtension *) p;
628  //Type of the extension
629  extension->type = HTONS(TLS_EXT_EC_POINT_FORMATS);
630 
631  //Point to the list of supported EC point formats
632  ecPointFormatList = (TlsEcPointFormatList *) extension->value;
633  //Items in the list are ordered according to client's preferences
634  n = 0;
635 
636  //The client can parse only the uncompressed point format...
637  ecPointFormatList->value[n++] = TLS_EC_POINT_FORMAT_UNCOMPRESSED;
638  //Fix the length of the list
639  ecPointFormatList->length = (uint8_t) n;
640 
641  //Consider the length field that precedes the list
642  n += sizeof(TlsEcPointFormatList);
643  //Fix the length of the extension
644  extension->length = htons(n);
645 
646  //Compute the length, in bytes, of the EcPointFormats extension
647  n += sizeof(TlsExtension);
648  }
649 #endif
650  }
651 #endif
652 
653  //Total number of bytes that have been written
654  *written = n;
655 
656  //Successful processing
657  return NO_ERROR;
658 }
659 
660 
661 /**
662  * @brief Format ALPN extension
663  * @param[in] context Pointer to the TLS context
664  * @param[in] p Output stream where to write the ALPN extension
665  * @param[out] written Total number of bytes that have been written
666  * @return Error code
667  **/
668 
670  uint8_t *p, size_t *written)
671 {
672  size_t n = 0;
673 
674 #if (TLS_ALPN_SUPPORT == ENABLED)
675  //The ALPN extension contains the list of protocols advertised by the
676  //client, in descending order of preference
677  if(context->protocolList != NULL)
678  {
679  uint_t i;
680  uint_t j;
681  TlsExtension *extension;
682  TlsProtocolName *protocolName;
683  TlsProtocolNameList *protocolNameList;
684 
685  //Add ALPN (Application-Layer Protocol Negotiation) extension
686  extension = (TlsExtension *) p;
687  //Type of the extension
688  extension->type = HTONS(TLS_EXT_ALPN);
689 
690  //Point to the list of protocol names
691  protocolNameList = (TlsProtocolNameList *) extension->value;
692 
693  //Move back to the beginning of the list
694  i = 0;
695  j = 0;
696  n = 0;
697 
698  //Parse the list of supported protocols
699  do
700  {
701  //Delimiter character found?
702  if(context->protocolList[i] == ',' || context->protocolList[i] == '\0')
703  {
704  //Discard empty tokens
705  if((i - j) > 0)
706  {
707  //Point to the protocol name
708  protocolName = (TlsProtocolName *) (protocolNameList->value + n);
709 
710  //Fill in the length field
711  protocolName->length = i - j;
712  //Copy protocol name
713  osMemcpy(protocolName->value, context->protocolList + j, i - j);
714 
715  //Adjust the length of the list
716  n += sizeof(TlsProtocolName) + i - j;
717  }
718 
719  //Move to the next token
720  j = i + 1;
721  }
722 
723  //Loop until the NULL character is reached
724  } while(context->protocolList[i++] != '\0');
725 
726  //Fix the length of the list
727  protocolNameList->length = htons(n);
728 
729  //Consider the 2-byte length field that precedes the list
730  n += sizeof(TlsProtocolNameList);
731  //Fix the length of the extension
732  extension->length = htons(n);
733 
734  //Compute the length, in bytes, of the ALPN extension
735  n += sizeof(TlsExtension);
736  }
737 #endif
738 
739  //Total number of bytes that have been written
740  *written = n;
741 
742  //Successful processing
743  return NO_ERROR;
744 }
745 
746 
747 /**
748  * @brief Format ClientCertType extension
749  * @param[in] context Pointer to the TLS context
750  * @param[in] p Output stream where to write the ClientCertType extension
751  * @param[out] written Total number of bytes that have been written
752  * @return Error code
753  **/
754 
756  uint8_t *p, size_t *written)
757 {
758  size_t n = 0;
759 
760 #if (TLS_RAW_PUBLIC_KEY_SUPPORT == ENABLED)
761  TlsExtension *extension;
762  TlsCertTypeList *clientCertTypeList;
763 
764  //Add the ClientCertType extension
765  extension = (TlsExtension *) p;
766  //Type of the extension
767  extension->type = HTONS(TLS_EXT_CLIENT_CERT_TYPE);
768 
769  //The ClientCertType extension in the ClientHello indicates the certificate
770  //types the client is able to provide to the server, when requested using a
771  //CertificateRequest message
772  clientCertTypeList = (TlsCertTypeList *) extension->value;
773 
774  //The ClientCertType extension carries a list of supported certificate
775  //types, sorted by client preference
776  n = 0;
777 
778  //Raw public key type
779  clientCertTypeList->value[n++] = TLS_CERT_FORMAT_RAW_PUBLIC_KEY;
780  //X.509 certificate type
781  clientCertTypeList->value[n++] = TLS_CERT_FORMAT_X509;
782 
783  //Fix the length of the list
784  clientCertTypeList->length = (uint8_t) n;
785 
786  //Consider the length field that precedes the list
787  n += sizeof(TlsCertTypeList);
788  //Fix the length of the extension
789  extension->length = htons(n);
790 
791  //Compute the length, in bytes, of the ClientCertType extension
792  n += sizeof(TlsExtension);
793 #endif
794 
795  //Total number of bytes that have been written
796  *written = n;
797 
798  //Successful processing
799  return NO_ERROR;
800 }
801 
802 
803 /**
804  * @brief Format ServerCertType extension
805  * @param[in] context Pointer to the TLS context
806  * @param[in] p Output stream where to write the ServerCertType extension
807  * @param[out] written Total number of bytes that have been written
808  * @return Error code
809  **/
810 
812  uint8_t *p, size_t *written)
813 {
814  size_t n = 0;
815 
816 #if (TLS_RAW_PUBLIC_KEY_SUPPORT == ENABLED)
817  //Ensure the client is able to process raw public keys
818  if(context->rpkVerifyCallback != NULL)
819  {
820  TlsExtension *extension;
821  TlsCertTypeList *serverCertTypeList;
822 
823  //Add the ServerCertType extension
824  extension = (TlsExtension *) p;
825  //Type of the extension
826  extension->type = HTONS(TLS_EXT_SERVER_CERT_TYPE);
827 
828  //The ServerCertType extension in the ClientHello indicates the types of
829  //certificates the client is able to process when provided by the server
830  //in a subsequent certificate payload
831  serverCertTypeList = (TlsCertTypeList *) extension->value;
832 
833  //The ServerCertType extension carries a list of supported certificate
834  //types, sorted by client preference
835  n = 0;
836 
837  //Raw public key type
838  serverCertTypeList->value[n++] = TLS_CERT_FORMAT_RAW_PUBLIC_KEY;
839  //X.509 certificate type
840  serverCertTypeList->value[n++] = TLS_CERT_FORMAT_X509;
841 
842  //Fix the length of the list
843  serverCertTypeList->length = (uint8_t) n;
844 
845  //Consider the length field that precedes the list
846  n += sizeof(TlsCertTypeList);
847  //Fix the length of the extension
848  extension->length = htons(n);
849 
850  //Compute the length, in bytes, of the ServerCertType extension
851  n += sizeof(TlsExtension);
852  }
853 #endif
854 
855  //Total number of bytes that have been written
856  *written = n;
857 
858  //Successful processing
859  return NO_ERROR;
860 }
861 
862 
863 /**
864  * @brief Format EncryptThenMac extension
865  * @param[in] context Pointer to the TLS context
866  * @param[in] p Output stream where to write the EncryptThenMac extension
867  * @param[out] written Total number of bytes that have been written
868  * @return Error code
869  **/
870 
872  uint8_t *p, size_t *written)
873 {
874  size_t n = 0;
875 
876 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
877  //In versions of TLS prior to TLS 1.3, this extension is used to negotiate
878  //encrypt-then-MAC construction rather than the default MAC-then-encrypt
879  if(context->versionMax >= TLS_VERSION_1_0 &&
880  context->versionMin <= TLS_VERSION_1_2)
881  {
882 #if (TLS_ENCRYPT_THEN_MAC_SUPPORT == ENABLED)
883  TlsExtension *extension;
884 
885  //Add the EncryptThenMac extension
886  extension = (TlsExtension *) p;
887  //Type of the extension
888  extension->type = HTONS(TLS_EXT_ENCRYPT_THEN_MAC);
889 
890  //The extension data field of this extension shall be empty (refer to
891  //RFC 7366, section 2)
892  extension->length = HTONS(0);
893 
894  //Compute the length, in bytes, of the EncryptThenMac extension
895  n = sizeof(TlsExtension);
896 #endif
897  }
898 #endif
899 
900  //Total number of bytes that have been written
901  *written = n;
902 
903  //Successful processing
904  return NO_ERROR;
905 }
906 
907 
908 /**
909  * @brief Format ExtendedMasterSecret extension
910  * @param[in] context Pointer to the TLS context
911  * @param[in] p Output stream where to write the ExtendedMasterSecret extension
912  * @param[out] written Total number of bytes that have been written
913  * @return Error code
914  **/
915 
917  uint8_t *p, size_t *written)
918 {
919  size_t n = 0;
920 
921 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
922  //Implementations which support both TLS 1.3 and earlier versions should
923  //indicate the use of the ExtendedMasterSecret extension whenever TLS 1.3
924  //is used (refer to RFC 8446, appendix D)
925  if(context->versionMax >= TLS_VERSION_1_0 &&
926  context->versionMin <= TLS_VERSION_1_2)
927  {
928 #if (TLS_EXT_MASTER_SECRET_SUPPORT == ENABLED)
929  TlsExtension *extension;
930 
931  //Add the ExtendedMasterSecret extension
932  extension = (TlsExtension *) p;
933  //Type of the extension
934  extension->type = HTONS(TLS_EXT_EXTENDED_MASTER_SECRET);
935 
936  //The extension data field of this extension is empty
937  extension->length = HTONS(0);
938 
939  //Compute the length, in bytes, of the ExtendedMasterSecret extension
940  n = sizeof(TlsExtension);
941 #endif
942  }
943 #endif
944 
945  //Total number of bytes that have been written
946  *written = n;
947 
948  //Successful processing
949  return NO_ERROR;
950 }
951 
952 
953 /**
954  * @brief Format SessionTicket extension
955  * @param[in] context Pointer to the TLS context
956  * @param[in] p Output stream where to write the SessionTicket extension
957  * @param[out] written Total number of bytes that have been written
958  * @return Error code
959  **/
960 
962  uint8_t *p, size_t *written)
963 {
964  size_t n = 0;
965 
966 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
967  //In versions of TLS prior to TLS 1.3, the SessionTicket extension is used
968  //to resume a TLS session without requiring session-specific state at the
969  //TLS server
970  if(context->versionMin <= TLS_VERSION_1_2)
971  {
972 #if (TLS_TICKET_SUPPORT == ENABLED)
973  //Check whether session ticket mechanism is enabled
974  if(context->sessionTicketEnabled)
975  {
976  TlsExtension *extension;
977 
978  //Add the SessionTicket extension
979  extension = (TlsExtension *) p;
980  //Type of the extension
981  extension->type = HTONS(TLS_EXT_SESSION_TICKET);
982 
983  //Valid ticket?
984  if(tlsIsTicketValid(context))
985  {
986  //If the client possesses a ticket that it wants to use to resume
987  //a session, then it includes the ticket in the SessionTicket
988  //extension in the ClientHello
989  osMemcpy(extension->value, context->ticket, context->ticketLen);
990 
991  //The extension_data field of SessionTicket extension contains the
992  //ticket
993  n = context->ticketLen;
994  }
995  else
996  {
997  //If the client does not have a ticket and is prepared to receive
998  //one in the NewSessionTicket handshake message, then it must
999  //include a zero-length ticket in the SessionTicket extension
1000  n = 0;
1001  }
1002 
1003  //Set the length of the extension
1004  extension->length = htons(n);
1005 
1006  //Compute the length, in bytes, of the SessionTicket extension
1007  n += sizeof(TlsExtension);
1008  }
1009 #endif
1010  }
1011 #endif
1012 
1013  //Total number of bytes that have been written
1014  *written = n;
1015 
1016  //Successful processing
1017  return NO_ERROR;
1018 }
1019 
1020 
1021 /**
1022  * @brief Format RenegotiationInfo extension
1023  * @param[in] context Pointer to the TLS context
1024  * @param[in] p Output stream where to write the RenegotiationInfo extension
1025  * @param[out] written Total number of bytes that have been written
1026  * @return Error code
1027  **/
1028 
1030  uint8_t *p, size_t *written)
1031 {
1032  size_t n = 0;
1033 
1034 #if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
1035  //TLS 1.3 forbids renegotiation
1036  if(context->versionMin <= TLS_VERSION_1_2)
1037  {
1038 #if (TLS_SECURE_RENEGOTIATION_SUPPORT == ENABLED)
1039  //Check whether secure renegotiation is enabled
1040  if(context->secureRenegoEnabled)
1041  {
1042  //During secure renegotiation, the client must include the
1043  //RenegotiationInfo extension containing the saved client_verify_data
1044  if(context->secureRenegoFlag)
1045  {
1046  TlsExtension *extension;
1047  TlsRenegoInfo *renegoInfo;
1048 
1049  //Determine the length of the verify data
1050  n = context->clientVerifyDataLen;
1051 
1052  //Add the RenegotiationInfo extension
1053  extension = (TlsExtension *) p;
1054  //Type of the extension
1055  extension->type = HTONS(TLS_EXT_RENEGOTIATION_INFO);
1056 
1057  //Point to the renegotiated_connection field
1058  renegoInfo = (TlsRenegoInfo *) extension->value;
1059  //Set the length of the verify data
1060  renegoInfo->length = (uint8_t) n;
1061 
1062  //Copy the verify data from the Finished message sent by the client
1063  //on the immediately previous handshake
1064  osMemcpy(renegoInfo->value, context->clientVerifyData, n);
1065 
1066  //Consider the length field that precedes the renegotiated_connection
1067  //field
1068  n += sizeof(TlsRenegoInfo);
1069  //Fix the length of the extension
1070  extension->length = htons(n);
1071 
1072  //Compute the length, in bytes, of the RenegotiationInfo extension
1073  n += sizeof(TlsExtension);
1074  }
1075  }
1076 #endif
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 Format ClientHello Padding extension
1090  * @param[in] context Pointer to the TLS context
1091  * @param[in] clientHelloLen Actual length of the ClientHello message
1092  * @param[in] p Output stream where to write the ClientHello Padding extension
1093  * @param[out] written Total number of bytes that have been written
1094  * @return Error code
1095  **/
1096 
1098  size_t clientHelloLen, uint8_t *p, size_t *written)
1099 {
1100  size_t n = 0;
1101 
1102 #if (TLS_CLIENT_HELLO_PADDING_SUPPORT == ENABLED)
1103  //TLS protocol?
1104  if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_STREAM)
1105  {
1106  TlsExtension *extension;
1107 
1108  //After building a ClientHello as normal, the client can add four bytes
1109  //to the length and test whether the resulting length falls into the
1110  //range 256 to 511 (refer to RFC 7685, section 4)
1111  clientHelloLen += sizeof(TlsHandshake);
1112 
1113  //Check the resulting length
1114  if(clientHelloLen >= 256 && clientHelloLen < 512)
1115  {
1116  //The ClientHello Padding extension will be added in order to push
1117  //the length to (at least) 512 bytes
1118  extension = (TlsExtension *) p;
1119 
1120  //Type of the extension
1121  extension->type = HTONS(TLS_EXT_PADDING);
1122 
1123  //Calculate the length of the padding
1124  if((clientHelloLen + sizeof(TlsExtension)) < 512)
1125  {
1126  n = 512 - sizeof(TlsExtension) - clientHelloLen;
1127  }
1128  else
1129  {
1130  n = 0;
1131  }
1132 
1133  //The padding string consists of an arbitrary number of zero bytes
1134  osMemset(extension->value, 0, n);
1135  //Set the length of the extension
1136  extension->length = htons(n);
1137 
1138  //Compute the length, in bytes, of the padding extension
1139  n += sizeof(TlsExtension);
1140  }
1141  }
1142 #endif
1143 
1144  //Total number of bytes that have been written
1145  *written = n;
1146 
1147  //Successful processing
1148  return NO_ERROR;
1149 }
1150 
1151 
1152 /**
1153  * @brief Parse SNI extension
1154  * @param[in] context Pointer to the TLS context
1155  * @param[in] serverNameList Pointer to the ServerName extension
1156  * @return Error code
1157  **/
1158 
1160  const TlsServerNameList *serverNameList)
1161 {
1162 #if (TLS_SNI_SUPPORT == ENABLED)
1163  //SNI extension found?
1164  if(serverNameList != NULL)
1165  {
1166  //If a client receives an extension type in the ServerHello that it did
1167  //not request in the associated ClientHello, it must abort the handshake
1168  //with an unsupported_extension fatal alert
1169  if(context->serverName == NULL)
1171  }
1172 #endif
1173 
1174  //Successful processing
1175  return NO_ERROR;
1176 }
1177 
1178 
1179 /**
1180  * @brief Parse MaxFragmentLength extension
1181  * @param[in] context Pointer to the TLS context
1182  * @param[in] maxFragLen Pointer to the MaxFragmentLength extension
1183  * @return Error code
1184  **/
1185 
1187  const TlsExtension *maxFragLen)
1188 {
1189 #if (TLS_MAX_FRAG_LEN_SUPPORT == ENABLED)
1190  //MaxFragmentLength extension found?
1191  if(maxFragLen != NULL)
1192  {
1193  size_t n;
1194 
1195  //Retrieve the value advertised by the server
1196  switch(maxFragLen->value[0])
1197  {
1199  n = 512;
1200  break;
1201 
1203  n = 1024;
1204  break;
1205 
1207  n = 2048;
1208  break;
1209 
1211  n = 4096;
1212  break;
1213 
1214  default:
1215  n = 0;
1216  break;
1217  }
1218 
1219  //If a client receives a maximum fragment length negotiation response
1220  //that differs from the length it requested, it must also abort the
1221  //handshake with an illegal_parameter alert
1222  if(n != context->maxFragLen)
1223  return ERROR_ILLEGAL_PARAMETER;
1224  }
1225 #endif
1226 
1227  //Successful processing
1228  return NO_ERROR;
1229 }
1230 
1231 
1232 /**
1233  * @brief Parse RecordSizeLimit extension
1234  * @param[in] context Pointer to the TLS context
1235  * @param[in] recordSizeLimit Pointer to the RecordSizeLimit extension
1236  * @return Error code
1237  **/
1238 
1240  const TlsExtension *recordSizeLimit)
1241 {
1242 #if (TLS_RECORD_SIZE_LIMIT_SUPPORT == ENABLED)
1243  //RecordSizeLimit extension found?
1244  if(recordSizeLimit != NULL)
1245  {
1246  uint16_t n;
1247 
1248  //The value of RecordSizeLimit is the maximum size of record in octets
1249  //that the peer is willing to receive
1250  n = LOAD16BE(recordSizeLimit->value);
1251 
1252  //Endpoints must not send a RecordSizeLimit extension with a value
1253  //smaller than 64
1254  if(n < 64)
1255  {
1256  //An endpoint must treat receipt of a smaller value as a fatal error
1257  //and generate an illegal_parameter alert
1258  return ERROR_ILLEGAL_PARAMETER;
1259  }
1260 
1261  //TLS 1.3 currently selected?
1262  if(context->version == TLS_VERSION_1_3)
1263  {
1264  //The value includes the content type and padding added in TLS 1.3
1265  n--;
1266  }
1267 
1268  //The peer can include any limit up to the protocol-defined limit for
1269  //maximum record size. Even if a larger value is provided by a peer, an
1270  //endpoint must not send records larger than the protocol-defined limit
1271  context->recordSizeLimit = MIN(n, TLS_MAX_RECORD_LENGTH);
1272 
1273  //The RecordSizeLimit extension has been successfully negotiated
1274  context->recordSizeLimitExtReceived = TRUE;
1275  }
1276  else
1277  {
1278  //If this extension is not negotiated, endpoints can send records of any
1279  //size permitted by the protocol or other negotiated extensions
1280  context->recordSizeLimit = TLS_MAX_RECORD_LENGTH;
1281 
1282  //The RecordSizeLimit extension is not supported by the server
1283  context->recordSizeLimitExtReceived = FALSE;
1284  }
1285 #endif
1286 
1287  //Successful processing
1288  return NO_ERROR;
1289 }
1290 
1291 
1292 /**
1293  * @brief Parse EcPointFormats extension
1294  * @param[in] context Pointer to the TLS context
1295  * @param[in] ecPointFormatList Pointer to the EcPointFormats extension
1296  * @return Error code
1297  **/
1298 
1300  const TlsEcPointFormatList *ecPointFormatList)
1301 {
1302  error_t error;
1303 
1304  //Initialize status code
1305  error = NO_ERROR;
1306 
1307 #if (TLS_ECDH_ANON_KE_SUPPORT == ENABLED || TLS_ECDHE_RSA_KE_SUPPORT == ENABLED || \
1308  TLS_ECDHE_ECDSA_KE_SUPPORT == ENABLED || TLS_ECDHE_PSK_KE_SUPPORT == ENABLED)
1309  //EcPointFormats extension found?
1310  if(ecPointFormatList != NULL)
1311  {
1312  uint_t i;
1313 
1314  //Loop through the list of supported EC point formats
1315  for(i = 0; i < ecPointFormatList->length; i++)
1316  {
1317  //Uncompressed point format?
1318  if(ecPointFormatList->value[i] == TLS_EC_POINT_FORMAT_UNCOMPRESSED)
1319  {
1320  break;
1321  }
1322  }
1323 
1324  //If the EcPointFormats extension is sent, it must contain the value 0
1325  //as one of the items in the list of point formats (refer to RFC 4492,
1326  //section 5.2)
1327  if(i >= ecPointFormatList->length)
1328  {
1329  //Report an error
1330  error = ERROR_ILLEGAL_PARAMETER;
1331  }
1332  }
1333 #endif
1334 
1335  //Return status code
1336  return error;
1337 }
1338 
1339 
1340 /**
1341  * @brief Parse ALPN extension
1342  * @param[in] context Pointer to the TLS context
1343  * @param[in] protocolNameList Pointer to the ALPN extension
1344  * @return Error code
1345  **/
1346 
1348  const TlsProtocolNameList *protocolNameList)
1349 {
1350 #if (TLS_ALPN_SUPPORT == ENABLED)
1351  //ALPN extension found?
1352  if(protocolNameList != NULL)
1353  {
1354  size_t length;
1355  const TlsProtocolName *protocolName;
1356 
1357  //If a client receives an extension type in the ServerHello that it
1358  //did not request in the associated ClientHello, it must abort the
1359  //handshake with an unsupported_extension fatal alert
1360  if(context->protocolList == NULL)
1362 
1363  //Retrieve the length of the list
1364  length = ntohs(protocolNameList->length);
1365 
1366  //The list must not be be empty
1367  if(length == 0)
1368  return ERROR_DECODING_FAILED;
1369 
1370  //Point to the selected protocol
1371  protocolName = (TlsProtocolName *) protocolNameList->value;
1372 
1373  //The list must contain exactly one protocol name
1374  if(length < sizeof(TlsProtocolName))
1375  return ERROR_DECODING_FAILED;
1376  if(length != (sizeof(TlsProtocolName) + protocolName->length))
1377  return ERROR_DECODING_FAILED;
1378 
1379  //Retrieve the length of the protocol name
1380  length -= sizeof(TlsProtocolName);
1381 
1382  //Empty strings must not be included in the list
1383  if(length == 0)
1384  return ERROR_DECODING_FAILED;
1385 
1386  //Check whether the protocol is supported by the client
1387  if(!tlsIsAlpnProtocolSupported(context, protocolName->value, length))
1388  {
1389  //Report an error if unknown ALPN protocols are disallowed
1390  if(!context->unknownProtocolsAllowed)
1391  return ERROR_ILLEGAL_PARAMETER;
1392  }
1393 
1394  //Sanity check
1395  if(context->selectedProtocol != NULL)
1396  {
1397  //Release memory
1398  tlsFreeMem(context->selectedProtocol);
1399  context->selectedProtocol = NULL;
1400  }
1401 
1402  //Allocate a memory block to hold the protocol name
1403  context->selectedProtocol = tlsAllocMem(length + 1);
1404  //Failed to allocate memory?
1405  if(context->selectedProtocol == NULL)
1406  return ERROR_OUT_OF_MEMORY;
1407 
1408  //Save protocol name
1409  osMemcpy(context->selectedProtocol, protocolName->value, length);
1410  //Properly terminate the string with a NULL character
1411  context->selectedProtocol[length] = '\0';
1412  }
1413 #endif
1414 
1415  //Successful processing
1416  return NO_ERROR;
1417 }
1418 
1419 
1420 /**
1421  * @brief Parse ClientCertType extension
1422  * @param[in] context Pointer to the TLS context
1423  * @param[in] clientCertType Pointer to the ClientCertType extension
1424  * @return Error code
1425  **/
1426 
1428  const TlsExtension *clientCertType)
1429 {
1430 #if (TLS_RAW_PUBLIC_KEY_SUPPORT == ENABLED)
1431  //ClientCertType extension found?
1432  if(clientCertType != NULL)
1433  {
1434  //The value conveyed in the extension must be selected from one of the
1435  //values provided in the ClientCertType extension sent in the ClientHello
1436  if(clientCertType->value[0] != TLS_CERT_FORMAT_X509 &&
1437  clientCertType->value[0] != TLS_CERT_FORMAT_RAW_PUBLIC_KEY)
1438  {
1439  return ERROR_ILLEGAL_PARAMETER;
1440  }
1441 
1442  //The ClientCertType extension in the ServerHello indicates the type
1443  //of certificates the client is requested to provide in a subsequent
1444  //certificate payload
1445  context->certFormat = (TlsCertificateFormat) clientCertType->value[0];
1446  }
1447 #endif
1448 
1449  //Successful processing
1450  return NO_ERROR;
1451 }
1452 
1453 
1454 /**
1455  * @brief Parse ServerCertType extension
1456  * @param[in] context Pointer to the TLS context
1457  * @param[in] serverCertType Pointer to the ServerCertType extension
1458  * @return Error code
1459  **/
1460 
1462  const TlsExtension *serverCertType)
1463 {
1464 #if (TLS_RAW_PUBLIC_KEY_SUPPORT == ENABLED)
1465  //ServerCertType extension found?
1466  if(serverCertType != NULL)
1467  {
1468  //If a client receives an extension type in the ServerHello that it did
1469  //not request in the associated ClientHello, it must abort the handshake
1470  //with an unsupported_extension fatal alert
1471  if(context->rpkVerifyCallback == NULL &&
1472  serverCertType->value[0] != TLS_CERT_FORMAT_X509)
1473  {
1475  }
1476 
1477  //The value conveyed in the extension must be selected from one of the
1478  //values provided in the ServerCertType extension sent in the ClientHello
1479  if(serverCertType->value[0] != TLS_CERT_FORMAT_X509 &&
1480  serverCertType->value[0] != TLS_CERT_FORMAT_RAW_PUBLIC_KEY)
1481  {
1482  return ERROR_ILLEGAL_PARAMETER;
1483  }
1484 
1485  //With the ServerCertType extension in the ServerHello, the TLS server
1486  //indicates the certificate type carried in the certificate payload
1487  context->peerCertFormat = (TlsCertificateFormat) serverCertType->value[0];
1488  }
1489 #endif
1490 
1491  //Successful processing
1492  return NO_ERROR;
1493 }
1494 
1495 
1496 /**
1497  * @brief Parse EncryptThenMac extension
1498  * @param[in] context Pointer to the TLS context
1499  * @param[in] encryptThenMac Pointer to the EncryptThenMac extension
1500  * @return Error code
1501  **/
1502 
1504  const TlsExtension *encryptThenMac)
1505 {
1506 #if (TLS_ENCRYPT_THEN_MAC_SUPPORT == ENABLED)
1507  //EncryptThenMac extension found?
1508  if(encryptThenMac != NULL)
1509  {
1510  //If a server receives an encrypt-then-MAC request extension from a
1511  //client and then selects a stream or AEAD ciphersuite, it must not send
1512  //an encrypt-then-MAC response extension back to the client (refer to
1513  //RFC 7366, section 3)
1514  if(context->cipherSuite.cipherMode != CIPHER_MODE_CBC)
1515  return ERROR_HANDSHAKE_FAILED;
1516 
1517  //Use encrypt-then-MAC construction rather than the default
1518  //MAC-then-encrypt
1519  context->etmExtReceived = TRUE;
1520  }
1521  else
1522  {
1523  //Use default MAC-then-encrypt construction
1524  context->etmExtReceived = FALSE;
1525  }
1526 #endif
1527 
1528  //Successful processing
1529  return NO_ERROR;
1530 }
1531 
1532 
1533 /**
1534  * @brief Parse ExtendedMasterSecret extension
1535  * @param[in] context Pointer to the TLS context
1536  * @param[in] extendedMasterSecret Pointer to the ExtendedMasterSecret extension
1537  * @return Error code
1538  **/
1539 
1542 {
1543 #if (TLS_EXT_MASTER_SECRET_SUPPORT == ENABLED)
1544  //ExtendedMasterSecret extension found?
1545  if(extendedMasterSecret != NULL)
1546  {
1547  //Abbreviated handshake?
1548  if(context->resume)
1549  {
1550  //If the original session did not use the ExtendedMasterSecret
1551  //extension but the new ServerHello contains the extension, the
1552  //client must abort the handshake
1553  if(!context->emsExtReceived)
1554  return ERROR_HANDSHAKE_FAILED;
1555  }
1556 
1557  //A valid ExtendedMasterSecret extension has been received
1558  context->emsExtReceived = TRUE;
1559  }
1560  else
1561  {
1562  //Abbreviated handshake?
1563  if(context->resume)
1564  {
1565  //If the original session used the ExtendedMasterSecret extension
1566  //but the new ServerHello does not contain the extension, the client
1567  //must abort the handshake
1568  if(context->emsExtReceived)
1569  return ERROR_HANDSHAKE_FAILED;
1570  }
1571 
1572  //The ServerHello does not contain any ExtendedMasterSecret extension
1573  context->emsExtReceived = FALSE;
1574  }
1575 #endif
1576 
1577  //Successful processing
1578  return NO_ERROR;
1579 }
1580 
1581 
1582 /**
1583  * @brief Parse SessionTicket extension
1584  * @param[in] context Pointer to the TLS context
1585  * @param[in] sessionTicket Pointer to the SessionTicket extension
1586  * @return Error code
1587  **/
1588 
1590  const TlsExtension *sessionTicket)
1591 {
1592 #if (TLS_TICKET_SUPPORT == ENABLED)
1593  //SessionTicket extension found?
1594  if(sessionTicket != NULL)
1595  {
1596  //If a client receives an extension type in the ServerHello that it did
1597  //not request in the associated ClientHello, it must abort the handshake
1598  //with an unsupported_extension fatal alert
1599  if(!context->sessionTicketEnabled)
1601 
1602  //The server uses the SessionTicket extension to indicate to the client
1603  //that it will send a new session ticket using the NewSessionTicket
1604  //handshake message
1605  context->sessionTicketExtReceived = TRUE;
1606  }
1607  else
1608  {
1609  //The ServerHello does not contain any SessionTicket extension
1610  context->sessionTicketExtReceived = FALSE;
1611  }
1612 #endif
1613 
1614  //Successful processing
1615  return NO_ERROR;
1616 }
1617 
1618 
1619 /**
1620  * @brief Parse RenegotiationInfo extension
1621  * @param[in] context Pointer to the TLS context
1622  * @param[in] extensions ServerHello extensions offered by the server
1623  * @return Error code
1624  **/
1625 
1628 {
1629 #if (TLS_SECURE_RENEGOTIATION_SUPPORT == ENABLED)
1630  //Initial handshake?
1631  if(context->clientVerifyDataLen == 0)
1632  {
1633  //RenegotiationInfo extension found?
1634  if(extensions->renegoInfo != NULL)
1635  {
1636  //If the extension is present, set the secure_renegotiation flag to TRUE
1637  context->secureRenegoFlag = TRUE;
1638 
1639  //Verify that the length of the renegotiated_connection field is zero
1640  if(extensions->renegoInfo->length != 0)
1641  {
1642  //If it is not, the client must abort the handshake by sending a
1643  //fatal handshake failure alert
1644  return ERROR_HANDSHAKE_FAILED;
1645  }
1646  }
1647  else
1648  {
1649  //If the extension is not present, the server does not support secure
1650  //renegotiation
1651  context->secureRenegoFlag = FALSE;
1652  }
1653  }
1654  //Secure renegotiation?
1655  else
1656  {
1657  //RenegotiationInfo extension found?
1658  if(extensions->renegoInfo != NULL)
1659  {
1660  //Check the length of the renegotiated_connection field
1661  if(extensions->renegoInfo->length != (context->clientVerifyDataLen +
1662  context->serverVerifyDataLen))
1663  {
1664  //The client must abort the handshake
1665  return ERROR_HANDSHAKE_FAILED;
1666  }
1667 
1668  //The client must verify that the first half of the field is equal to
1669  //the saved client_verify_data value
1670  if(osMemcmp(extensions->renegoInfo->value, context->clientVerifyData,
1671  context->clientVerifyDataLen))
1672  {
1673  //If it is not, the client must abort the handshake
1674  return ERROR_HANDSHAKE_FAILED;
1675  }
1676 
1677  //The client must verify that the second half of the field is equal to
1678  //the saved server_verify_data value
1679  if(osMemcmp(extensions->renegoInfo->value + context->clientVerifyDataLen,
1680  context->serverVerifyData, context->serverVerifyDataLen))
1681  {
1682  //If it is not, the client must abort the handshake
1683  return ERROR_HANDSHAKE_FAILED;
1684  }
1685 
1686 #if (TLS_EXT_MASTER_SECRET_SUPPORT == ENABLED)
1687  //ExtendedMasterSecret extension found?
1688  if(extensions->extendedMasterSecret != NULL)
1689  {
1690  //If the initial handshake did not use the ExtendedMasterSecret
1691  //extension but the new ServerHello contains the extension, the
1692  //client must abort the handshake
1693  if(!context->emsExtReceived)
1694  return ERROR_HANDSHAKE_FAILED;
1695  }
1696  else
1697  {
1698  //If the initial handshake used the ExtendedMasterSecret extension
1699  //but the new ServerHello does not contain the extension, the
1700  //client must abort the handshake
1701  if(context->emsExtReceived)
1702  return ERROR_HANDSHAKE_FAILED;
1703  }
1704 #endif
1705  }
1706  else
1707  {
1708  //If the RenegotiationInfo extension is not present, the client
1709  //must abort the handshake
1710  return ERROR_HANDSHAKE_FAILED;
1711  }
1712  }
1713 #endif
1714 
1715  //Successful processing
1716  return NO_ERROR;
1717 }
1718 
1719 #endif
@ TLS_GROUP_X25519_MLKEM768
Definition: tls.h:1500
@ TLS_GROUP_BRAINPOOLP512R1_TLS13
Definition: tls.h:1481
#define TLS_MAX_RECORD_LENGTH
Definition: tls.h:977
#define tlsAllocMem(size)
Definition: tls.h:888
@ TLS_EXT_MAX_FRAGMENT_LENGTH
Definition: tls.h:1351
#define htons(value)
Definition: cpu_endian.h:413
Parsing and checking of TLS extensions.
@ TLS_CERT_FORMAT_RAW_PUBLIC_KEY
Definition: tls.h:1223
TLS helper functions.
TlsServerName
Definition: tls.h:1725
uint8_t extensions[]
Definition: ntp_common.h:213
@ TLS_GROUP_BRAINPOOLP256R1_TLS13
Definition: tls.h:1479
@ TLS_TRANSPORT_PROTOCOL_QUIC
Definition: tls.h:1001
@ TLS_GROUP_SECP160R2
Definition: tls.h:1465
TLS cipher suites.
@ CIPHER_MODE_CBC
Definition: crypto.h:1036
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:1260
@ TLS_EXT_SUPPORTED_VERSIONS
Definition: tls.h:1387
uint8_t p
Definition: ndp.h:300
Helper functions for TLS client.
@ TLS_GROUP_SECP256K1
Definition: tls.h:1470
#define TRUE
Definition: os_port.h:50
@ TLS_GROUP_SECP256R1
Definition: tls.h:1471
bool_t tlsIsAlpnProtocolSupported(TlsContext *context, const char_t *protocol, size_t length)
Check whether the specified ALPN protocol is supported.
TlsEcPointFormatList
Definition: tls.h:1780
@ TLS_GROUP_CURVE_SM2
Definition: tls.h:1489
@ TLS_GROUP_SECP224K1
Definition: tls.h:1468
error_t tlsParseServerRecordSizeLimitExtension(TlsContext *context, const TlsExtension *recordSizeLimit)
Parse RecordSizeLimit extension.
@ TLS_TRANSPORT_PROTOCOL_DATAGRAM
Definition: tls.h:1000
TlsRenegoInfo
Definition: tls.h:1802
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.
error_t tlsFormatTrustedAuthorities(TlsContext *context, uint8_t *p, size_t *written)
Format the list of trusted authorities.
#define osStrlen(s)
Definition: os_port.h:168
TlsProtocolNameList
Definition: tls.h:1758
TlsExtension
Definition: tls.h:1691
@ TLS_GROUP_BRAINPOOLP256R1
Definition: tls.h:1474
@ TLS_EXT_SESSION_TICKET
Definition: tls.h:1383
@ TLS_GROUP_X448
Definition: tls.h:1478
@ TLS_CIPHER_SUITE_TYPE_TLS13
@ TLS_GROUP_FFDHE6144
Definition: tls.h:1493
@ TLS_MAX_FRAGMENT_LENGTH_4096
Definition: tls.h:1425
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:2006
@ TLS_CIPHER_SUITE_TYPE_SM
#define DTLS_SUPPORT
Definition: dtls_misc.h:41
@ TLS_EXT_SERVER_NAME
Definition: tls.h:1350
error_t tlsParseServerEtmExtension(TlsContext *context, const TlsExtension *encryptThenMac)
Parse EncryptThenMac extension.
error_t tlsFormatTrustedCaKeysExtension(TlsContext *context, uint8_t *p, size_t *written)
Format TrustedCaKeys extension.
@ TLS_EXT_TRUSTED_CA_KEYS
Definition: tls.h:1353
#define DTLS_VERSION_1_0
Definition: dtls_misc.h:35
TlsHandshake
Definition: tls.h:1872
#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:1220
@ TLS_EXT_CLIENT_CERT_TYPE
Definition: tls.h:1369
#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:1373
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:1499
@ TLS_EXT_SUPPORTED_GROUPS
Definition: tls.h:1360
#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:1176
@ TLS_EXT_RENEGOTIATION_INFO
Definition: tls.h:1402
const KemAlgo * tls13GetNextGenAlgo(TlsContext *context, uint16_t namedGroup)
Get the next-gen algorithm used by the hybrid key exchange method.
Definition: tls13_misc.c:1319
#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:1372
@ TLS_GROUP_FFDHE4096
Definition: tls.h:1492
@ TLS_CIPHER_SUITE_TYPE_ECDH
bool_t tlsCheckDnsHostname(const char_t *name, size_t length)
DNS hostname verification.
Definition: tls_misc.c:1605
#define TLS_VERSION_1_3
Definition: tls.h:97
TlsServerNameList
Definition: tls.h:1736
@ TLS_MAX_FRAGMENT_LENGTH_2048
Definition: tls.h:1424
@ TLS_GROUP_SECP384R1
Definition: tls.h:1472
@ TLS_GROUP_SECP192K1
Definition: tls.h:1466
error_t tlsFormatClientAlpnExtension(TlsContext *context, uint8_t *p, size_t *written)
Format ALPN extension.
@ TLS_TRANSPORT_PROTOCOL_EAP
Definition: tls.h:1002
TlsProtocolName
Definition: tls.h:1747
error_t tlsParseServerEcPointFormatsExtension(TlsContext *context, const TlsEcPointFormatList *ecPointFormatList)
Parse EcPointFormats extension.
@ TLS_EC_POINT_FORMAT_UNCOMPRESSED
Definition: tls.h:1514
uint8_t length
Definition: tcp.h:375
@ TLS_GROUP_BRAINPOOLP512R1
Definition: tls.h:1476
@ TLS_GROUP_FFDHE2048
Definition: tls.h:1490
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:1496
#define ENABLED
Definition: os_port.h:37
@ TLS_EXT_EC_POINT_FORMATS
Definition: tls.h:1361
@ TLS_GROUP_SECP521R1
Definition: tls.h:1473
@ TLS_GROUP_SECP192R1
Definition: tls.h:1467
@ TLS_EXT_ALPN
Definition: tls.h:1366
@ TLS_GROUP_FFDHE3072
Definition: tls.h:1491
Hello extensions.
Definition: tls.h:2253
@ TLS_GROUP_SECP160R1
Definition: tls.h:1464
@ TLS_GROUP_BRAINPOOLP384R1_TLS13
Definition: tls.h:1480
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:1469
#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:1371
#define HTONS(value)
Definition: cpu_endian.h:410
@ TLS_GROUP_MLKEM1024
Definition: tls.h:1498
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:1370
@ TLS_NAME_TYPE_HOSTNAME
Definition: tls.h:1412
@ TLS_MAX_FRAGMENT_LENGTH_1024
Definition: tls.h:1423
const uint16_t tlsSupportedGroups[]
error_t tlsFormatClientCertTypeListExtension(TlsContext *context, uint8_t *p, size_t *written)
Format ClientCertType extension.
TLS (Transport Layer Security)
TlsTrustedAuthorities
Definition: tls.h:1679
@ TLS_GROUP_X25519
Definition: tls.h:1477
@ TLS_TRANSPORT_PROTOCOL_STREAM
Definition: tls.h:999
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:1244
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:1497
TlsSupportedGroupList
Definition: tls.h:1769
#define tlsFreeMem(p)
Definition: tls.h:893
TlsSupportedVersionList
Definition: tls.h:1713
#define DTLS_VERSION_1_2
Definition: dtls_misc.h:36
@ TLS_GROUP_FFDHE8192
Definition: tls.h:1494
TlsCertTypeList
Definition: tls.h:1791
@ NO_ERROR
Success.
Definition: error.h:44
@ TLS_EXT_RECORD_SIZE_LIMIT
Definition: tls.h:1377
Debugging facilities.
@ TLS_CERT_FORMAT_X509
Definition: tls.h:1221
@ TLS_GROUP_BRAINPOOLP384R1
Definition: tls.h:1475
@ TLS_MAX_FRAGMENT_LENGTH_512
Definition: tls.h:1422
#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:1501
error_t tlsFormatClientMaxFragLenExtension(TlsContext *context, uint8_t *p, size_t *written)
Format MaxFragmentLength extension.