tls_extensions.c
Go to the documentation of this file.
1 /**
2  * @file tls_extensions.c
3  * @brief Parsing and checking of TLS extensions
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneSSL Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.6.8
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL TLS_TRACE_LEVEL
33 
34 //Dependencies
35 #include "tls/tls.h"
36 #include "tls/tls_cipher_suites.h"
37 #include "tls/tls_extensions.h"
39 #include "tls/tls_record.h"
40 #include "dtls/dtls_record.h"
41 #include "debug.h"
42 
43 //Check TLS library configuration
44 #if (TLS_SUPPORT == ENABLED)
45 
46 
47 /**
48  * @brief Parse Hello extensions
49  * @param[in] msgType Handshake message type
50  * @param[in] p Input stream where to read the list of extensions
51  * @param[in] length Number of bytes available in the input stream
52  * @param[out] extensions List of Hello extensions resulting from the parsing process
53  * @return Error code
54  **/
55 
58 {
59  error_t error;
60  uint_t i;
61  size_t n;
62  uint16_t type;
63  const TlsExtension *extension;
64  const TlsExtensionList *extensionList;
65 
66  //Initialize TLS extensions
68 
69  //Check message type
71  {
72  //The implementation must accept messages both with and without the
73  //extensions field
74  if(length == 0)
75  {
76  //The extensions field is not present
77  return NO_ERROR;
78  }
79  }
80 
81  //Point to the list of extensions
82  extensionList = (TlsExtensionList *) p;
83 
84  //Malformed message?
85  if(length < sizeof(TlsExtensionList))
86  {
87  return ERROR_DECODING_FAILED;
88  }
89 
90  //If the amount of data in the message does not precisely match the format
91  //of the message, then send a fatal alert
92  if(length != (sizeof(TlsExtensionList) + ntohs(extensionList->length)))
93  {
94  return ERROR_DECODING_FAILED;
95  }
96 
97  //Point to the first extension of the list
98  p += sizeof(TlsExtensionList);
99  //Retrieve the length of the list
100  length -= sizeof(TlsExtensionList);
101 
102  //Parse the list of extensions offered by the peer
103  for(i = 0; length > 0; i++)
104  {
105  //Limit the number of extensions (the duplicate check below takes a time
106  //that is quadratic in the number of extensions)
107  if(i >= TLS_MAX_EXTENSIONS)
108  {
109  return ERROR_DECODING_FAILED;
110  }
111 
112  //Point to the current extension
113  extension = (TlsExtension *) p;
114 
115  //Check the length of the extension
116  if(length < sizeof(TlsExtension))
117  {
118  return ERROR_DECODING_FAILED;
119  }
120 
121  if(length < (sizeof(TlsExtension) + ntohs(extension->length)))
122  {
123  return ERROR_DECODING_FAILED;
124  }
125 
126  //Get extension type
127  type = ntohs(extension->type);
128  //Retrieve the length of the extension
129  n = ntohs(extension->length);
130 
131  //Jump to the next extension
132  p += sizeof(TlsExtension) + n;
133  //Number of bytes left to process
134  length -= sizeof(TlsExtension) + n;
135 
136  //Test if the current extension is a duplicate
138  //Duplicate extension found?
139  if(error)
140  {
141  return error;
142  }
143 
144  //When multiple extensions of different types are present in the ClientHello
145  //or ServerHello messages, the extensions may appear in any order
147  {
148  //Check message type
150  {
151  const TlsSupportedVersionList *supportedVersionList;
152 
153  //Point to the SupportedVersions extension
154  supportedVersionList = (TlsSupportedVersionList *) extension->value;
155 
156  //Malformed extension?
157  if(n < sizeof(TlsSupportedVersionList))
158  {
159  return ERROR_DECODING_FAILED;
160  }
161 
162  if(n != (sizeof(TlsSupportedVersionList) + supportedVersionList->length))
163  {
164  return ERROR_DECODING_FAILED;
165  }
166 
167  //Check the length of the list
168  if(supportedVersionList->length == 0)
169  {
170  return ERROR_DECODING_FAILED;
171  }
172 
173  if((supportedVersionList->length % 2) != 0)
174  {
175  return ERROR_DECODING_FAILED;
176  }
177 
178  //The SupportedVersions extension is valid
179  extensions->supportedVersionList = supportedVersionList;
180  }
181  else if(msgType == TLS_TYPE_SERVER_HELLO ||
183  {
184  //The extension contains the selected version value
185  if(n != sizeof(uint16_t))
186  {
187  return ERROR_DECODING_FAILED;
188  }
189 
190  //The SupportedVersions extension is valid
191  extensions->selectedVersion = extension;
192  }
193  else
194  {
195  //The extension is not specified for the message in which it appears
197  }
198  }
199  else if(type == TLS_EXT_SERVER_NAME)
200  {
201  const TlsServerNameList *serverNameList;
202 
203  //Point to the ServerName extension
204  serverNameList = (TlsServerNameList *) extension->value;
205 
206  //Empty extension?
207  if(n == 0)
208  {
209  //When the server includes a ServerName extension, the data field
210  //of this extension may be empty
212  {
213  return ERROR_DECODING_FAILED;
214  }
215  }
216  else
217  {
218  //Malformed extension?
219  if(n < sizeof(TlsServerNameList))
220  {
221  return ERROR_DECODING_FAILED;
222  }
223 
224  if(n != (sizeof(TlsServerNameList) + ntohs(serverNameList->length)))
225  {
226  return ERROR_DECODING_FAILED;
227  }
228 
229  //Check the length of the list
230  if(ntohs(serverNameList->length) == 0)
231  {
232  return ERROR_DECODING_FAILED;
233  }
234  }
235 
236  //The ServerName extension is valid
237  extensions->serverNameList = serverNameList;
238  }
239  else if(type == TLS_EXT_SUPPORTED_GROUPS)
240  {
241  const TlsSupportedGroupList *supportedGroupList;
242 
243  //Point to the SupportedGroups extension
244  supportedGroupList = (TlsSupportedGroupList *) extension->value;
245 
246  //Malformed extension?
247  if(n < sizeof(TlsSupportedGroupList))
248  {
249  return ERROR_DECODING_FAILED;
250  }
251 
252  if(n != (sizeof(TlsSupportedGroupList) + ntohs(supportedGroupList->length)))
253  {
254  return ERROR_DECODING_FAILED;
255  }
256 
257  //Check the length of the list
258  if(ntohs(supportedGroupList->length) == 0)
259  {
260  return ERROR_DECODING_FAILED;
261  }
262 
263  if((ntohs(supportedGroupList->length) % 2) != 0)
264  {
265  return ERROR_DECODING_FAILED;
266  }
267 
268  //The SupportedGroups extension is valid
269  extensions->supportedGroupList = supportedGroupList;
270  }
271  else if(type == TLS_EXT_EC_POINT_FORMATS)
272  {
273  const TlsEcPointFormatList *ecPointFormatList;
274 
275  //Point to the EcPointFormats extension
276  ecPointFormatList = (TlsEcPointFormatList *) extension->value;
277 
278  //Malformed extension?
279  if(n < sizeof(TlsEcPointFormatList))
280  {
281  return ERROR_DECODING_FAILED;
282  }
283 
284  if(n != (sizeof(TlsEcPointFormatList) + ecPointFormatList->length))
285  {
286  return ERROR_DECODING_FAILED;
287  }
288 
289  //Check the length of the list
290  if(ecPointFormatList->length == 0)
291  {
292  return ERROR_DECODING_FAILED;
293  }
294 
295  //The EcPointFormats extension is valid
296  extensions->ecPointFormatList = ecPointFormatList;
297  }
299  {
300  const TlsSignSchemeList *signAlgoList;
301 
302  //Point to the SignatureAlgorithms extension
303  signAlgoList = (TlsSignSchemeList *) extension->value;
304 
305  //Malformed extension?
306  if(n < sizeof(TlsSignSchemeList))
307  {
308  return ERROR_DECODING_FAILED;
309  }
310 
311  if(n != (sizeof(TlsSignSchemeList) + ntohs(signAlgoList->length)))
312  {
313  return ERROR_DECODING_FAILED;
314  }
315 
316  //Check the length of the list
317  if(ntohs(signAlgoList->length) == 0)
318  {
319  return ERROR_DECODING_FAILED;
320  }
321 
322  if((ntohs(signAlgoList->length) % 2) != 0)
323  {
324  return ERROR_DECODING_FAILED;
325  }
326 
327  //The SignatureAlgorithms extension is valid
328  extensions->signAlgoList = signAlgoList;
329  }
331  {
332  const TlsSignSchemeList *certSignAlgoList;
333 
334  //Point to the SignatureAlgorithmsCert extension
335  certSignAlgoList = (TlsSignSchemeList *) extension->value;
336 
337  //Malformed extension?
338  if(n < sizeof(TlsSignSchemeList))
339  {
340  return ERROR_DECODING_FAILED;
341  }
342 
343  if(n != (sizeof(TlsSignSchemeList) + ntohs(certSignAlgoList->length)))
344  {
345  return ERROR_DECODING_FAILED;
346  }
347 
348  //Check the length of the list
349  if(ntohs(certSignAlgoList->length) == 0)
350  {
351  return ERROR_DECODING_FAILED;
352  }
353 
354  if((ntohs(certSignAlgoList->length) % 2) != 0)
355  {
356  return ERROR_DECODING_FAILED;
357  }
358 
359  //The SignatureAlgorithmsCert extension is valid
360  extensions->certSignAlgoList = certSignAlgoList;
361  }
362 #if (TLS_MAX_FRAG_LEN_SUPPORT == ENABLED)
364  {
365  //Malformed extension?
366  if(n != sizeof(uint8_t))
367  {
368  return ERROR_DECODING_FAILED;
369  }
370 
371  //The MaxFragmentLength extension is valid
372  extensions->maxFragLen = extension;
373  }
374 #endif
375 #if (TLS_RECORD_SIZE_LIMIT_SUPPORT == ENABLED)
376  else if(type == TLS_EXT_RECORD_SIZE_LIMIT)
377  {
378  //Malformed extension?
379  if(n != sizeof(uint16_t))
380  {
381  return ERROR_DECODING_FAILED;
382  }
383 
384  //The RecordSizeLimit extension is valid
385  extensions->recordSizeLimit = extension;
386  }
387 #endif
388 #if (TLS_ALPN_SUPPORT == ENABLED)
389  else if(type == TLS_EXT_ALPN)
390  {
391  const TlsProtocolNameList *protocolNameList;
392 
393  //Point to the ALPN extension
394  protocolNameList = (TlsProtocolNameList *) extension->value;
395 
396  //Malformed extension?
397  if(n < sizeof(TlsProtocolNameList))
398  {
399  return ERROR_DECODING_FAILED;
400  }
401 
402  if(n != (sizeof(TlsProtocolNameList) + ntohs(protocolNameList->length)))
403  {
404  return ERROR_DECODING_FAILED;
405  }
406 
407  //The ALPN extension is valid
408  extensions->protocolNameList = protocolNameList;
409  }
410 #endif
411 #if (TLS_RAW_PUBLIC_KEY_SUPPORT == ENABLED)
412  else if(type == TLS_EXT_CLIENT_CERT_TYPE)
413  {
414  //Check message type
416  {
417  const TlsCertTypeList *clientCertTypeList;
418 
419  //Point to the ClientCertType extension
420  clientCertTypeList = (TlsCertTypeList *) extension->value;
421 
422  //Malformed extension?
423  if(n < sizeof(TlsCertTypeList))
424  {
425  return ERROR_DECODING_FAILED;
426  }
427 
428  if(n != (sizeof(TlsCertTypeList) + clientCertTypeList->length))
429  {
430  return ERROR_DECODING_FAILED;
431  }
432 
433  //The ClientCertType extension is valid
434  extensions->clientCertTypeList = clientCertTypeList;
435  }
436  else
437  {
438  //Only a single value is permitted in the ClientCertType extension
439  //when carried in the ServerHello
440  if(n != sizeof(uint8_t))
441  {
442  return ERROR_DECODING_FAILED;
443  }
444 
445  //The ClientCertType extension is valid
446  extensions->clientCertType = extension;
447  }
448  }
449  else if(type == TLS_EXT_SERVER_CERT_TYPE)
450  {
451  //Check message type
453  {
454  const TlsCertTypeList *serverCertTypeList;
455 
456  //Point to the ServerCertType extension
457  serverCertTypeList = (TlsCertTypeList *) extension->value;
458 
459  //Malformed extension?
460  if(n < sizeof(TlsCertTypeList))
461  {
462  return ERROR_DECODING_FAILED;
463  }
464 
465  if(n != (sizeof(TlsCertTypeList) + serverCertTypeList->length))
466  {
467  return ERROR_DECODING_FAILED;
468  }
469 
470  //The ServerCertType extension is valid
471  extensions->serverCertTypeList = serverCertTypeList;
472  }
473  else
474  {
475  //Only a single value is permitted in the ServerCertType extension
476  //when carried in the ServerHello
477  if(n != sizeof(uint8_t))
478  {
479  return ERROR_DECODING_FAILED;
480  }
481 
482  //The ServerCertType extension is valid
483  extensions->serverCertType = extension;
484  }
485  }
486 #endif
487 #if (TLS_ENCRYPT_THEN_MAC_SUPPORT == ENABLED)
488  else if(type == TLS_EXT_ENCRYPT_THEN_MAC)
489  {
490  //The extension data field of this extension shall be empty (refer to
491  //RFC 7366, section 2)
492  if(n != 0)
493  {
494  return ERROR_DECODING_FAILED;
495  }
496 
497  //The EncryptThenMac extension is valid
498  extensions->encryptThenMac = extension;
499  }
500 #endif
501 #if (TLS_EXT_MASTER_SECRET_SUPPORT == ENABLED)
503  {
504  //Malformed extension?
505  if(n != 0)
506  {
507  return ERROR_DECODING_FAILED;
508  }
509 
510  //The ExtendedMasterSecret extension is valid
511  extensions->extendedMasterSecret = extension;
512  }
513 #endif
514 #if (TLS_TICKET_SUPPORT == ENABLED)
515  else if(type == TLS_EXT_SESSION_TICKET)
516  {
517  //Check message type
519  {
520  //The server uses a zero-length SessionTicket extension to indicate
521  //to the client that it will send a new session ticket
522  if(n != 0)
523  {
524  return ERROR_DECODING_FAILED;
525  }
526  }
527 
528  //The SessionTicket extension is valid
529  extensions->sessionTicket = extension;
530  }
531 #endif
532 #if (TLS_SECURE_RENEGOTIATION_SUPPORT == ENABLED)
533  else if(type == TLS_EXT_RENEGOTIATION_INFO)
534  {
535  const TlsRenegoInfo *renegoInfo;
536 
537  //Point to the RenegotiationInfo extension
538  renegoInfo = (TlsRenegoInfo *) extension->value;
539 
540  //Malformed extension?
541  if(n < sizeof(TlsRenegoInfo))
542  {
543  return ERROR_DECODING_FAILED;
544  }
545 
546  if(n != (sizeof(TlsRenegoInfo) + renegoInfo->length))
547  {
548  return ERROR_DECODING_FAILED;
549  }
550 
551  //The RenegotiationInfo extension is valid
552  extensions->renegoInfo = renegoInfo;
553  }
554 #endif
555 #if (TLS_QUIC_SUPPORT == ENABLED)
557  {
558  //The QuicTransportParameters extension is present
559  extensions->quicTransportParams = extension;
560  }
561 #endif
562 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
563  else if(type == TLS_EXT_COOKIE)
564  {
565  const Tls13Cookie *cookie;
566 
567  //Point to the Cookie extension
568  cookie = (Tls13Cookie *) extension->value;
569 
570  //Malformed extension?
571  if(n < sizeof(Tls13Cookie))
572  {
573  return ERROR_DECODING_FAILED;
574  }
575 
576  if(n != (sizeof(Tls13Cookie) + ntohs(cookie->length)))
577  {
578  return ERROR_DECODING_FAILED;
579  }
580 
581  //Check the length of the cookie
582  if(ntohs(cookie->length) == 0)
583  {
584  return ERROR_DECODING_FAILED;
585  }
586 
587  //The Cookie extension is valid
588  extensions->cookie = cookie;
589  }
591  {
592  const TlsCertAuthorities *certAuthorities;
593 
594  //Point to the CertificateAuthorities extension
595  certAuthorities = (TlsCertAuthorities *) extension->value;
596 
597  //Malformed extension?
598  if(n < sizeof(TlsCertAuthorities))
599  {
600  return ERROR_DECODING_FAILED;
601  }
602 
603  if(n != (sizeof(TlsCertAuthorities) + ntohs(certAuthorities->length)))
604  {
605  return ERROR_DECODING_FAILED;
606  }
607 
608  //Check the length of the list
609  if(ntohs(certAuthorities->length) < 3)
610  {
611  return ERROR_DECODING_FAILED;
612  }
613 
614  //The CertificateAuthorities extension is valid
615  extensions->certAuthorities = certAuthorities;
616  }
617  else if(type == TLS_EXT_KEY_SHARE)
618  {
619  //Check message type
621  {
622  uint_t j;
623  size_t k;
624  size_t m;
625  const Tls13KeyShareList *keyShareList;
626  const Tls13KeyShareEntry *keyShareEntry;
627 
628  //The extension contains a list of offered KeyShareEntry values
629  //in descending order of client preference
630  keyShareList = (Tls13KeyShareList *) extension->value;
631 
632  //Malformed extension?
633  if(n < sizeof(Tls13KeyShareList))
634  {
635  return ERROR_DECODING_FAILED;
636  }
637 
638  if(n != (sizeof(Tls13KeyShareList) + ntohs(keyShareList->length)))
639  {
640  return ERROR_DECODING_FAILED;
641  }
642 
643  //Point to the first KeyShareEntry of the list
644  p = keyShareList->value;
645  //Retrieve the length of the list
646  m = ntohs(keyShareList->length);
647 
648  //Parse the list of key share entries offered by the peer
649  for(j = 0; m > 0; j++)
650  {
651  //Limit the number of key share entries (the duplicate check
652  //below takes a time that is quadratic in the number of entries)
654  {
655  return ERROR_DECODING_FAILED;
656  }
657 
658  //Malformed extension?
659  if(m < sizeof(Tls13KeyShareEntry))
660  {
661  return ERROR_DECODING_FAILED;
662  }
663 
664  //Point to the current key share entry
665  keyShareEntry = (Tls13KeyShareEntry *) p;
666  //Retrieve the length of the key_exchange field
667  k = ntohs(keyShareEntry->length);
668 
669  //Malformed extension?
670  if(m < (sizeof(Tls13KeyShareEntry) + k))
671  {
672  return ERROR_DECODING_FAILED;
673  }
674 
675  //Point to the next entry
676  p += sizeof(Tls13KeyShareEntry) + k;
677  //Remaining bytes to process
678  m -= sizeof(Tls13KeyShareEntry) + k;
679 
680  //Clients must not offer multiple KeyShareEntry values for the
681  //same group. Servers may check for violations of this rule and
682  //abort the handshake with an illegal_parameter alert
683  error = tls13CheckDuplicateKeyShare(ntohs(keyShareEntry->group),
684  p, m);
685  //Any error to report?
686  if(error)
687  {
689  }
690  }
691 
692  //The KeyShare extension is valid
693  extensions->keyShareList = keyShareList;
694  }
696  {
697  //The extension contains the mutually supported group the server
698  //intends to negotiate
699  if(n != sizeof(uint16_t))
700  {
701  return ERROR_DECODING_FAILED;
702  }
703 
704  //The KeyShare extension is valid
705  extensions->selectedGroup = extension;
706  }
707  else if(msgType == TLS_TYPE_SERVER_HELLO)
708  {
709  const Tls13KeyShareEntry *serverShare;
710 
711  //The extension contains a single KeyShareEntry value that is in
712  //the same group as one of the client's shares
713  serverShare = (Tls13KeyShareEntry *) extension->value;
714 
715  //Malformed extension?
716  if(n < sizeof(Tls13KeyShareEntry))
717  {
718  return ERROR_DECODING_FAILED;
719  }
720 
721  if(n != (sizeof(Tls13KeyShareEntry) + ntohs(serverShare->length)))
722  {
723  return ERROR_DECODING_FAILED;
724  }
725 
726  //The KeyShare extension is valid
727  extensions->serverShare = serverShare;
728  }
729  else
730  {
731  //The extension is not specified for the message in which it appears
733  }
734  }
736  {
737  const Tls13PskKeModeList *pskKeModeList;
738 
739  //The extension contains the list of PSK key exchange modes that
740  //are supported by the client
741  pskKeModeList = (Tls13PskKeModeList *) extension->value;
742 
743  //Malformed extension?
744  if(n < sizeof(Tls13PskKeModeList))
745  {
746  return ERROR_DECODING_FAILED;
747  }
748 
749  if(n != (sizeof(Tls13PskKeModeList) + pskKeModeList->length))
750  {
751  return ERROR_DECODING_FAILED;
752  }
753 
754  //The PskKeyExchangeModes extension is valid
755  extensions->pskKeModeList = pskKeModeList;
756  }
757  else if(type == TLS_EXT_PRE_SHARED_KEY)
758  {
759  //Check message type
761  {
762  const Tls13PskIdentityList *identityList;
763  const Tls13PskBinderList *binderList;
764 
765  //The PreSharedKey extension must be the last extension in the
766  //ClientHello. Servers must check that it is the last extension and
767  //otherwise fail the handshake with an illegal_parameter alert
768  if(length != 0)
769  {
771  }
772 
773  //The extension contains a list of the identities that the client
774  //is willing to negotiate with the server
775  identityList = (Tls13PskIdentityList *) extension->value;
776 
777  //Malformed extension?
778  if(n < sizeof(Tls13PskIdentityList))
779  {
780  return ERROR_DECODING_FAILED;
781  }
782 
783  if(n < (sizeof(Tls13PskIdentityList) + ntohs(identityList->length)))
784  {
785  return ERROR_DECODING_FAILED;
786  }
787 
788  //Remaining bytes to process
789  n -= sizeof(Tls13PskIdentityList) + ntohs(identityList->length);
790 
791  //The extension also contains a series of HMAC values, one for each
792  //PSK offered in the PreSharedKey extension and in the same order
793  binderList = (Tls13PskBinderList *) (extension->value +
794  sizeof(Tls13PskIdentityList) + ntohs(identityList->length));
795 
796  //Malformed extension?
797  if(n < sizeof(Tls13PskBinderList))
798  {
799  return ERROR_DECODING_FAILED;
800  }
801 
802  if(n != (sizeof(Tls13PskBinderList) + ntohs(binderList->length)))
803  {
804  return ERROR_DECODING_FAILED;
805  }
806 
807  //The PreSharedKey extension is valid
808  extensions->identityList = identityList;
809  extensions->binderList = binderList;
810  }
811  else if(msgType == TLS_TYPE_SERVER_HELLO)
812  {
813  //The extension contains the chosen identity expressed as a 0-based
814  //index into the identities in the client's list
815  if(n != sizeof(uint16_t))
816  {
817  return ERROR_DECODING_FAILED;
818  }
819 
820  //The PreSharedKey extension is valid
821  extensions->selectedIdentity = extension;
822  }
823  else
824  {
825  //The extension is not specified for the message in which it appears
827  }
828  }
829  else if(type == TLS_EXT_EARLY_DATA)
830  {
831  //Check message type
834  {
835  //The extension data field is empty
836  if(n != 0)
837  {
838  return ERROR_DECODING_FAILED;
839  }
840  }
842  {
843  //The extension data field contains an unsigned 32-bit integer
844  if(n != sizeof(uint32_t))
845  {
846  return ERROR_DECODING_FAILED;
847  }
848  }
849  else
850  {
851  //The extension is not specified for the message in which it appears
853  }
854 
855  //The EarlyData extension is valid
856  extensions->earlyDataIndication = extension;
857  }
858 #endif
859  else
860  {
861  //If a client receives an extension type in the ServerHello that it
862  //did not request in the associated ClientHello, it must abort the
863  //handshake with an unsupported_extension fatal alert
867  {
869  }
870  }
871  }
872 
873  //Successful processing
874  return NO_ERROR;
875 }
876 
877 
878 /**
879  * @brief Check Hello extensions
880  * @param[in] msgType Handshake message type
881  * @param[in] version TLS version
882  * @param[in] extensions List of Hello extensions offered by the peer
883  * @return Error code
884  **/
885 
888 {
889  error_t error;
890 
891  //Initialize status code
892  error = NO_ERROR;
893 
894 #if (TLS_MAX_FRAG_LEN_SUPPORT == ENABLED && TLS_RECORD_SIZE_LIMIT_SUPPORT == ENABLED)
895  //A client must treat receipt of both MaxFragmentLength and RecordSizeLimit
896  //extensions as a fatal error, and it should generate an illegal_parameter
897  //alert (refer to RFC 8449, section 5)
898  if(extensions->maxFragLen != NULL && extensions->recordSizeLimit != NULL)
899  {
900  //ServerHello or EncryptedExtensions message?
903  {
904  error = ERROR_ILLEGAL_PARAMETER;
905  }
906  }
907 #endif
908 
909 #if (TLS_MAX_VERSION >= TLS_VERSION_1_3 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
910  //If an implementation receives an extension which it recognizes and which
911  //is not specified for the message in which it appears it must abort the
912  //handshake with an illegal_parameter alert (refer to RFC 8446, section 4.2)
913  if(version == TLS_VERSION_1_3)
914  {
915  //SupportedVersions extension found?
916  if(extensions->supportedVersionList != NULL ||
917  extensions->selectedVersion != NULL)
918  {
919  //The extension can only appear in CH, SH and HRR messages
923  {
924  error = ERROR_ILLEGAL_PARAMETER;
925  }
926  }
927 
928  //ServerName extension found?
929  if(extensions->serverNameList != NULL)
930  {
931  //The extension can only appear in CH and EE messages
934  {
935  error = ERROR_ILLEGAL_PARAMETER;
936  }
937  }
938 
939  //SupportedGroups extension found?
940  if(extensions->supportedGroupList != NULL)
941  {
942  //The extension can only appear in CH and EE messages
945  {
946  error = ERROR_ILLEGAL_PARAMETER;
947  }
948  }
949 
950  //EcPointFormats extension found?
951  if(extensions->ecPointFormatList != NULL)
952  {
953  //The extension can only appear in CH
955  {
956  error = ERROR_ILLEGAL_PARAMETER;
957  }
958  }
959 
960  //SignatureAlgorithms extension found?
961  if(extensions->signAlgoList != NULL)
962  {
963  //The extension can only appear in CH and CR messages
966  {
967  error = ERROR_ILLEGAL_PARAMETER;
968  }
969  }
970 
971  //SignatureAlgorithmsCert extension found?
972  if(extensions->certSignAlgoList != NULL)
973  {
974  //The extension can only appear in CH and CR messages
977  {
978  error = ERROR_ILLEGAL_PARAMETER;
979  }
980  }
981 
982 #if (TLS_MAX_FRAG_LEN_SUPPORT == ENABLED)
983  //MaxFragmentLength extension found?
984  if(extensions->maxFragLen != NULL)
985  {
986  //The extension can only appear in CH and EE messages
989  {
990  error = ERROR_ILLEGAL_PARAMETER;
991  }
992  }
993 #endif
994 
995 #if (TLS_RECORD_SIZE_LIMIT_SUPPORT == ENABLED)
996  //RecordSizeLimit extension found?
997  if(extensions->recordSizeLimit != NULL)
998  {
999  //The extension can only appear in CH and EE messages
1002  {
1003  error = ERROR_ILLEGAL_PARAMETER;
1004  }
1005  }
1006 #endif
1007 
1008 #if (TLS_ALPN_SUPPORT == ENABLED)
1009  //ALPN extension found?
1010  if(extensions->protocolNameList != NULL)
1011  {
1012  //The extension can only appear in CH and EE messages
1015  {
1016  error = ERROR_ILLEGAL_PARAMETER;
1017  }
1018  }
1019 #endif
1020 
1021 #if (TLS_RAW_PUBLIC_KEY_SUPPORT == ENABLED)
1022  //ClientCertType extension found?
1023  if(extensions->clientCertTypeList != NULL ||
1024  extensions->clientCertType != NULL)
1025  {
1026  //The extension can only appear in CH and EE messages
1029  {
1030  error = ERROR_ILLEGAL_PARAMETER;
1031  }
1032  }
1033 
1034  //ServerCertType extension found?
1035  if(extensions->serverCertTypeList != NULL ||
1036  extensions->serverCertType != NULL)
1037  {
1038  //The extension can only appear in CH and EE messages
1041  {
1042  error = ERROR_ILLEGAL_PARAMETER;
1043  }
1044  }
1045 #endif
1046 
1047 #if (TLS_ENCRYPT_THEN_MAC_SUPPORT == ENABLED)
1048  //EncryptThenMac extension found?
1049  if(extensions->encryptThenMac != NULL)
1050  {
1051  //The extension can only appear in CH
1053  {
1054  error = ERROR_ILLEGAL_PARAMETER;
1055  }
1056  }
1057 #endif
1058 
1059 #if (TLS_EXT_MASTER_SECRET_SUPPORT == ENABLED)
1060  //ExtendedMasterSecret extension found?
1061  if(extensions->extendedMasterSecret != NULL)
1062  {
1063  //The extension can only appear in CH
1065  {
1066  error = ERROR_ILLEGAL_PARAMETER;
1067  }
1068  }
1069 #endif
1070 
1071 #if (TLS_TICKET_SUPPORT == ENABLED)
1072  //SessionTicket extension found?
1073  if(extensions->sessionTicket != NULL)
1074  {
1075  //The extension can only appear in CH
1077  {
1078  error = ERROR_ILLEGAL_PARAMETER;
1079  }
1080  }
1081 #endif
1082 
1083 #if (TLS_SECURE_RENEGOTIATION_SUPPORT == ENABLED)
1084  //RenegotiationInfo extension found?
1085  if(extensions->renegoInfo != NULL)
1086  {
1087  //The extension can only appear in CH
1089  {
1090  error = ERROR_ILLEGAL_PARAMETER;
1091  }
1092  }
1093 #endif
1094 
1095 #if (TLS_QUIC_SUPPORT == ENABLED)
1096  //QuicTransportParameters extension found?
1097  if(extensions->quicTransportParams != NULL)
1098  {
1099  //The extension can only appear in CH and EE messages
1102  {
1103  error = ERROR_ILLEGAL_PARAMETER;
1104  }
1105  }
1106 #endif
1107 
1108  //Cookie extension found?
1109  if(extensions->cookie != NULL)
1110  {
1111  //The extension can only appear in CH and HRR messages
1114  {
1115  error = ERROR_ILLEGAL_PARAMETER;
1116  }
1117  }
1118 
1119  //CertificateAuthorities extension found?
1120  if(extensions->certAuthorities != NULL)
1121  {
1122  //The extension can only appear in CH and CR messages
1125  {
1126  error = ERROR_ILLEGAL_PARAMETER;
1127  }
1128  }
1129 
1130  //KeyShare extension found?
1131  if(extensions->keyShareList != NULL ||
1132  extensions->serverShare != NULL ||
1133  extensions->selectedGroup != NULL)
1134  {
1135  //The extension can only appear in CH, SH and HRR messages
1139  {
1140  error = ERROR_ILLEGAL_PARAMETER;
1141  }
1142  }
1143 
1144  //PskKeyExchangeModes extension found?
1145  if(extensions->pskKeModeList != NULL)
1146  {
1147  //The extension can only appear in CH message
1149  {
1150  error = ERROR_ILLEGAL_PARAMETER;
1151  }
1152  }
1153 
1154  //PreSharedKey extension found?
1155  if(extensions->identityList != NULL ||
1156  extensions->binderList != NULL ||
1157  extensions->selectedIdentity != NULL)
1158  {
1159  //The extension can only appear in CH and SH messages
1162  {
1163  error = ERROR_ILLEGAL_PARAMETER;
1164  }
1165  }
1166 
1167  //EarlyData extension found?
1168  if(extensions->earlyDataIndication != NULL)
1169  {
1170  //The extension can only appear in CH, EE and NST messages
1174  {
1175  error = ERROR_ILLEGAL_PARAMETER;
1176  }
1177  }
1178  }
1179 
1180  //Check mandatory-to-implement extensions
1182  {
1183  //A client must provide a PskKeyExchangeModes extension if it offers a
1184  //PreSharedKey extension (refer to RFC 8446, section 4.2.9)
1185  if(extensions->identityList != NULL || extensions->binderList != NULL)
1186  {
1187  //If a client offers PreSharedKey without a PskKeyExchangeModes
1188  //extension, the servers must abort the handshake
1189  if(extensions->pskKeModeList == NULL)
1190  {
1191  error = ERROR_MISSING_EXTENSION;
1192  }
1193  }
1194 
1195  //If the ClientHello does not contain a PreSharedKey extension, it must
1196  //contain both a SignatureAlgorithms extension and a SupportedGroups
1197  //extension (refer to RFC 8446, section 9.2)
1198  if(extensions->identityList == NULL || extensions->binderList == NULL)
1199  {
1200  //Servers receiving a ClientHello which does not conform to these
1201  //requirements must abort the handshake with a missing_extension
1202  //alert
1203  if(extensions->signAlgoList == NULL ||
1204  extensions->supportedGroupList == NULL)
1205  {
1206  error = ERROR_MISSING_EXTENSION;
1207  }
1208  }
1209 
1210  //If the ClientHello contains a SupportedGroups extension, it must also
1211  //contain a KeyShare extension, and vice versa
1212  if(extensions->supportedGroupList != NULL &&
1213  extensions->keyShareList == NULL)
1214  {
1215  error = ERROR_MISSING_EXTENSION;
1216  }
1217  else if(extensions->keyShareList != NULL &&
1218  extensions->supportedGroupList == NULL)
1219  {
1220  error = ERROR_MISSING_EXTENSION;
1221  }
1222  }
1223 #endif
1224 
1225  //Return status code
1226  return error;
1227 }
1228 
1229 
1230 /**
1231  * @brief Check whether the specified extension type is a duplicate
1232  * @param[in] type Extension type
1233  * @param[in] p Input stream where to read the list of extensions
1234  * @param[in] length Number of bytes available in the input stream
1235  * @return Error code
1236  **/
1237 
1238 error_t tlsCheckDuplicateExtension(uint16_t type, const uint8_t *p,
1239  size_t length)
1240 {
1241  size_t n;
1242  const TlsExtension *extension;
1243 
1244  //Parse the list of extensions offered by the peer
1245  while(length > 0)
1246  {
1247  //Point to the current extension
1248  extension = (TlsExtension *) p;
1249 
1250  //Check the length of the extension
1251  if(length < sizeof(TlsExtension))
1252  {
1253  return ERROR_DECODING_FAILED;
1254  }
1255 
1256  if(length < (sizeof(TlsExtension) + ntohs(extension->length)))
1257  {
1258  return ERROR_DECODING_FAILED;
1259  }
1260 
1261  //There must not be more than one extension of the same type (refer to
1262  //RFC 5246, section 7.4.1.4)
1263  if(ntohs(extension->type) == type)
1264  {
1266  {
1267  return ERROR_DECODING_FAILED;
1268  }
1269  else
1270  {
1271  return ERROR_ILLEGAL_PARAMETER;
1272  }
1273  }
1274 
1275  //Retrieve the length of the extension
1276  n = ntohs(extension->length);
1277 
1278  //Jump to the next extension
1279  p += sizeof(TlsExtension) + n;
1280  //Number of bytes left to process
1281  length -= sizeof(TlsExtension) + n;
1282  }
1283 
1284  //Successful verification
1285  return NO_ERROR;
1286 }
1287 
1288 
1289 /**
1290  * @brief Check whether the specified ALPN protocol is supported
1291  * @param[in] context Pointer to the TLS context
1292  * @param[in] protocol Pointer to the protocol name
1293  * @param[in] length Length of the protocol name, in bytes
1294  * @return TRUE if the specified protocol is supported, else FALSE
1295  **/
1296 
1298  const char_t *protocol, size_t length)
1299 {
1300  bool_t supported;
1301 
1302  //Initialize flag
1303  supported = FALSE;
1304 
1305 #if (TLS_ALPN_SUPPORT == ENABLED)
1306  //Sanity check
1307  if(context->protocolList != NULL)
1308  {
1309  size_t i;
1310  size_t j;
1311 
1312  //Move back to the beginning of the list
1313  i = 0;
1314  j = 0;
1315 
1316  //Parse the list of supported protocols
1317  do
1318  {
1319  //Delimiter character found?
1320  if(context->protocolList[i] == ',' || context->protocolList[i] == '\0')
1321  {
1322  //Check the length of the protocol name
1323  if(length == (i - j))
1324  {
1325  //Compare protocol names
1326  if(osMemcmp(protocol, context->protocolList + j, i - j) == 0)
1327  {
1328  //The specified protocol is supported
1329  supported = TRUE;
1330  //We are done
1331  break;
1332  }
1333  }
1334 
1335  //Move to the next token
1336  j = i + 1;
1337  }
1338 
1339  //Loop until the NULL character is reached
1340  } while(context->protocolList[i++] != '\0');
1341  }
1342 #endif
1343 
1344  //Return TRUE if the specified protocol is supported
1345  return supported;
1346 }
1347 
1348 #endif
@ TLS_EXT_PSK_KEY_EXCHANGE_MODES
Definition: tls.h:1457
@ TLS_EXT_MAX_FRAGMENT_LENGTH
Definition: tls.h:1419
Parsing and checking of TLS extensions.
uint8_t extensions[]
Definition: ntp_common.h:213
Tls13PskBinderList
Definition: tls13_misc.h:282
int bool_t
Definition: compiler_port.h:63
TLS cipher suites.
uint8_t protocol
Definition: ipv4.h:357
Tls13Cookie
Definition: tls13_misc.h:204
@ ERROR_ILLEGAL_PARAMETER
Definition: error.h:245
@ TLS_EXT_SUPPORTED_VERSIONS
Definition: tls.h:1455
uint8_t p
Definition: ndp.h:300
error_t tlsCheckDuplicateExtension(uint16_t type, const uint8_t *p, size_t length)
Check whether the specified extension type is a duplicate.
#define TRUE
Definition: os_port.h:50
bool_t tlsIsAlpnProtocolSupported(TlsContext *context, const char_t *protocol, size_t length)
Check whether the specified ALPN protocol is supported.
TlsEcPointFormatList
Definition: tls.h:1858
TlsRenegoInfo
Definition: tls.h:1880
TlsMessageType
Handshake message type.
Definition: tls.h:1147
uint8_t type
Definition: coap_common.h:176
#define osMemcmp(p1, p2, length)
Definition: os_port.h:159
uint8_t version
Definition: coap_common.h:177
TlsProtocolNameList
Definition: tls.h:1836
Tls13KeyShareEntry
Definition: tls13_misc.h:216
TlsExtension
Definition: tls.h:1769
@ TLS_EXT_EARLY_DATA
Definition: tls.h:1454
@ TLS_EXT_SESSION_TICKET
Definition: tls.h:1451
@ TLS_EXT_QUIC_TRANSPORT_PARAMETERS
Definition: tls.h:1467
@ TLS_EXT_SERVER_NAME
Definition: tls.h:1418
@ TLS_EXT_SIGNATURE_ALGORITHMS_CERT
Definition: tls.h:1461
TlsExtensionList
Definition: tls.h:1780
#define FALSE
Definition: os_port.h:46
TlsCertAuthorities
Definition: tls.h:1735
@ ERROR_UNSUPPORTED_EXTENSION
Definition: error.h:247
@ TLS_EXT_CLIENT_CERT_TYPE
Definition: tls.h:1437
#define TlsContext
Definition: tls.h:36
error_t
Error codes.
Definition: error.h:43
DTLS record layer.
@ TLS_EXT_EXTENDED_MASTER_SECRET
Definition: tls.h:1441
@ TLS_EXT_SUPPORTED_GROUPS
Definition: tls.h:1428
@ TLS_EXT_RENEGOTIATION_INFO
Definition: tls.h:1473
error_t tlsParseHelloExtensions(TlsMessageType msgType, const uint8_t *p, size_t length, TlsHelloExtensions *extensions)
Parse Hello extensions.
@ TLS_EXT_ENCRYPT_THEN_MAC
Definition: tls.h:1440
@ TLS_TYPE_CLIENT_HELLO
Definition: tls.h:1149
@ TLS_EXT_KEY_SHARE
Definition: tls.h:1462
@ ERROR_MISSING_EXTENSION
Definition: error.h:246
#define TLS_VERSION_1_3
Definition: tls.h:100
@ TLS_TYPE_SERVER_HELLO
Definition: tls.h:1150
TlsServerNameList
Definition: tls.h:1814
@ TLS_TYPE_ENCRYPTED_EXTENSIONS
Definition: tls.h:1155
TLS record protocol.
@ TLS_EXT_COOKIE
Definition: tls.h:1456
TlsSignSchemeList
Definition: tls.h:1713
@ TLS_EXT_CERTIFICATE_AUTHORITIES
Definition: tls.h:1458
uint8_t length
Definition: tcp.h:375
@ TLS_EXT_EC_POINT_FORMATS
Definition: tls.h:1429
@ TLS_EXT_ALPN
Definition: tls.h:1434
Hello extensions.
Definition: tls.h:2331
Tls13KeyShareList
Definition: tls13_misc.h:227
Transcript hash calculation.
#define TLS13_MAX_KEY_SHARE_ENTRIES
Definition: tls13_misc.h:148
#define ntohs(value)
Definition: cpu_endian.h:421
char char_t
Definition: compiler_port.h:55
uint8_t msgType
uint8_t m
Definition: ndp.h:304
uint8_t n
#define TLS_MAX_EXTENSIONS
Definition: tls.h:888
@ TLS_EXT_SERVER_CERT_TYPE
Definition: tls.h:1438
Tls13PskKeModeList
Definition: tls13_misc.h:238
@ TLS_TYPE_CERTIFICATE_REQUEST
Definition: tls.h:1160
@ TLS_EXT_PRE_SHARED_KEY
Definition: tls.h:1453
TLS (Transport Layer Security)
uint8_t cookie[]
Definition: dtls_misc.h:211
error_t tlsCheckHelloExtensions(TlsMessageType msgType, uint16_t version, TlsHelloExtensions *extensions)
Check Hello extensions.
@ TLS_EXT_SIGNATURE_ALGORITHMS
Definition: tls.h:1431
@ TLS_TYPE_NEW_SESSION_TICKET
Definition: tls.h:1152
@ ERROR_DECODING_FAILED
Definition: error.h:243
Tls13PskIdentityList
Definition: tls13_misc.h:260
@ TLS_TYPE_HELLO_RETRY_REQUEST
Definition: tls.h:1154
unsigned int uint_t
Definition: compiler_port.h:57
#define osMemset(p, value, length)
Definition: os_port.h:141
TlsSupportedGroupList
Definition: tls.h:1847
error_t tls13CheckDuplicateKeyShare(uint16_t namedGroup, const uint8_t *p, size_t length)
Check whether the specified key share group is a duplicate.
Definition: tls13_misc.c:1430
TlsSupportedVersionList
Definition: tls.h:1791
TlsCertTypeList
Definition: tls.h:1869
@ NO_ERROR
Success.
Definition: error.h:44
@ TLS_EXT_RECORD_SIZE_LIMIT
Definition: tls.h:1445
Debugging facilities.