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