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