forked from Oryx-Embedded/CycloneSSL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtls_server_extensions.c
1362 lines (1145 loc) · 40.4 KB
/
tls_server_extensions.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file tls_server_extensions.c
* @brief Formatting and parsing of extensions (TLS server)
*
* @section License
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* Copyright (C) 2010-2022 Oryx Embedded SARL. All rights reserved.
*
* This file is part of CycloneSSL Open.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @author Oryx Embedded SARL (www.oryx-embedded.com)
* @version 2.1.4
**/
//Switch to the appropriate trace level
#define TRACE_LEVEL TLS_TRACE_LEVEL
//Dependencies
#include <string.h>
#include "tls.h"
#include "tls_cipher_suites.h"
#include "tls_server_extensions.h"
#include "tls_extensions.h"
#include "tls_misc.h"
#include "debug.h"
//Check TLS library configuration
#if (TLS_SUPPORT == ENABLED && TLS_SERVER_SUPPORT == ENABLED)
/**
* @brief Format SNI extension
* @param[in] context Pointer to the TLS context
* @param[in] p Output stream where to write the ServerName extension
* @param[out] written Total number of bytes that have been written
* @return Error code
**/
error_t tlsFormatServerSniExtension(TlsContext *context,
uint8_t *p, size_t *written)
{
size_t n = 0;
#if (TLS_SNI_SUPPORT == ENABLED)
//A server that receives a ClientHello containing the SNI extension may use
//the information contained in the extension to guide its selection of an
//appropriate certificate to return to the client. In this event, the server
//shall include an extension of type SNI in the ServerHello
if(context->serverName != NULL)
{
//Full handshake?
if(!context->resume)
{
TlsExtension *extension;
//Add SNI (Server Name Indication) extension
extension = (TlsExtension *) p;
//Type of the extension
extension->type = HTONS(TLS_EXT_SERVER_NAME);
//The extension data field of this extension shall be empty (refer to
//RFC 6066, section 3)
extension->length = HTONS(0);
//Compute the length, in bytes, of the ServerName extension
n = sizeof(TlsExtension);
}
else
{
//When resuming a session, the server must not include a ServerName
//extension in the ServerHello (refer to RFC 6066, section 3)
n = 0;
}
}
#endif
//Total number of bytes that have been written
*written = n;
//Successful processing
return NO_ERROR;
}
/**
* @brief Format MaxFragmentLength extension
* @param[in] context Pointer to the TLS context
* @param[in] p Output stream where to write the MaxFragmentLength extension
* @param[out] written Total number of bytes that have been written
* @return Error code
**/
error_t tlsFormatServerMaxFragLenExtension(TlsContext *context,
uint8_t *p, size_t *written)
{
size_t n = 0;
#if (TLS_MAX_FRAG_LEN_SUPPORT == ENABLED)
//An extension type must not appear in the ServerHello unless the same
//extension type appeared in the corresponding ClientHello
if(context->maxFragLenExtReceived)
{
//Servers that receive an ClientHello containing a MaxFragmentLength
//extension may accept the requested maximum fragment length by including
//an extension of type MaxFragmentLength in the ServerHello
if(context->maxFragLen == 512 || context->maxFragLen == 1024 ||
context->maxFragLen == 2048 || context->maxFragLen == 4096)
{
TlsExtension *extension;
//Add the MaxFragmentLength extension
extension = (TlsExtension *) p;
//Type of the extension
extension->type = HTONS(TLS_EXT_MAX_FRAGMENT_LENGTH);
//The data field of this extension shall contain a MaxFragmentLength
//whose value is the same as the requested maximum fragment length
switch(context->maxFragLen)
{
case 512:
extension->value[0] = TLS_MAX_FRAGMENT_LENGTH_512;
break;
case 1024:
extension->value[0] = TLS_MAX_FRAGMENT_LENGTH_1024;
break;
case 2048:
extension->value[0] = TLS_MAX_FRAGMENT_LENGTH_2048;
break;
default:
extension->value[0] = TLS_MAX_FRAGMENT_LENGTH_4096;
break;
}
//The extension data field contains a single byte
n = sizeof(uint8_t);
//Fix the length of the extension
extension->length = htons(n);
//Compute the length, in bytes, of the MaxFragmentLength extension
n += sizeof(TlsExtension);
}
}
#endif
//Total number of bytes that have been written
*written = n;
//Successful processing
return NO_ERROR;
}
/**
* @brief Format RecordSizeLimit extension
* @param[in] context Pointer to the TLS context
* @param[in] p Output stream where to write the RecordSizeLimit extension
* @param[out] written Total number of bytes that have been written
* @return Error code
**/
error_t tlsFormatServerRecordSizeLimitExtension(TlsContext *context,
uint8_t *p, size_t *written)
{
size_t n = 0;
#if (TLS_RECORD_SIZE_LIMIT_SUPPORT == ENABLED)
//An extension type must not appear in the ServerHello unless the same
//extension type appeared in the corresponding ClientHello
if(context->recordSizeLimitExtReceived)
{
size_t recordSizeLimit;
TlsExtension *extension;
//Add the RecordSizeLimit extension
extension = (TlsExtension *) p;
//Type of the extension
extension->type = HTONS(TLS_EXT_RECORD_SIZE_LIMIT);
//An endpoint must not send a value higher than the protocol-defined
//maximum record size (refer to RFC 8449, section 4)
recordSizeLimit = MIN(context->rxBufferMaxLen, TLS_MAX_RECORD_LENGTH);
//TLS 1.3 currently selected?
if(context->version == TLS_VERSION_1_3)
{
//The value includes the content type and padding added in TLS 1.3
recordSizeLimit++;
}
//The value of RecordSizeLimit is the maximum size of record in octets
//that the endpoint is willing to receive
STORE16BE(recordSizeLimit, extension->value);
//The extension data field contains a 16-bit unsigned integer
n = sizeof(uint16_t);
//Fix the length of the extension
extension->length = htons(n);
//Compute the length, in bytes, of the RecordSizeLimit extension
n += sizeof(TlsExtension);
}
#endif
//Total number of bytes that have been written
*written = n;
//Successful processing
return NO_ERROR;
}
/**
* @brief Format EcPointFormats extension
* @param[in] context Pointer to the TLS context
* @param[in] p Output stream where to write the EcPointFormats extension
* @param[out] written Total number of bytes that have been written
* @return Error code
**/
error_t tlsFormatServerEcPointFormatsExtension(TlsContext *context,
uint8_t *p, size_t *written)
{
size_t n = 0;
#if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
//TLS 1.3 has removed point format negotiation in favor of a single point
//format for each curve (refer to RFC 8446, section 1.2)
if(context->version <= TLS_VERSION_1_2)
{
#if (TLS_ECDH_ANON_KE_SUPPORT == ENABLED || TLS_ECDHE_RSA_KE_SUPPORT == ENABLED || \
TLS_ECDHE_ECDSA_KE_SUPPORT == ENABLED || TLS_ECDHE_PSK_KE_SUPPORT == ENABLED)
//An extension type must not appear in the ServerHello unless the same
//extension type appeared in the corresponding ClientHello
if(context->ecPointFormatsExtReceived)
{
uint16_t identifier;
//Retrieve the selected cipher suite
identifier = context->cipherSuite.identifier;
//A server that selects an ECC cipher suite in response to a ClientHello
//message including an EcPointFormats extension appends this extension
//to its ServerHello message
if((tlsGetCipherSuiteType(identifier) & TLS_CIPHER_SUITE_TYPE_ECC) != 0)
{
TlsExtension *extension;
TlsEcPointFormatList *ecPointFormatList;
//Add the EcPointFormats extension
extension = (TlsExtension *) p;
//Type of the extension
extension->type = HTONS(TLS_EXT_EC_POINT_FORMATS);
//Point to the list of supported EC point formats
ecPointFormatList = (TlsEcPointFormatList *) extension->value;
//Items in the list are ordered according to server's preferences
n = 0;
//The server can parse only the uncompressed point format...
ecPointFormatList->value[n++] = TLS_EC_POINT_FORMAT_UNCOMPRESSED;
//Fix the length of the list
ecPointFormatList->length = (uint8_t) n;
//Consider the length field that precedes the list
n += sizeof(TlsEcPointFormatList);
//Fix the length of the extension
extension->length = htons(n);
//Compute the length, in bytes, of the EcPointFormats extension
n += sizeof(TlsExtension);
}
}
#endif
}
#endif
//Total number of bytes that have been written
*written = n;
//Successful processing
return NO_ERROR;
}
/**
* @brief Format ALPN extension
* @param[in] context Pointer to the TLS context
* @param[in] p Output stream where to write the ALPN extension
* @param[out] written Total number of bytes that have been written
* @return Error code
**/
error_t tlsFormatServerAlpnExtension(TlsContext *context,
uint8_t *p, size_t *written)
{
size_t n = 0;
#if (TLS_ALPN_SUPPORT == ENABLED)
//The ALPN extension may be returned to the client within the extended
//ServerHello message
if(context->selectedProtocol != NULL)
{
//Empty strings must not be included
if(context->selectedProtocol[0] != '\0')
{
TlsExtension *extension;
TlsProtocolName *protocolName;
TlsProtocolNameList *protocolNameList;
//Add ALPN (Application-Layer Protocol Negotiation) extension
extension = (TlsExtension *) p;
//Type of the extension
extension->type = HTONS(TLS_EXT_ALPN);
//Point to the list of protocol names
protocolNameList = (TlsProtocolNameList *) extension->value;
//The list must contain exactly one protocol name
protocolName = (TlsProtocolName *) protocolNameList->value;
//Retrieve the length of the protocol name
n = osStrlen(context->selectedProtocol);
//Fill in the length field
protocolName->length = (uint8_t) n;
//Copy protocol name
osMemcpy(protocolName->value, context->selectedProtocol, n);
//Adjust the length of the list
n += sizeof(TlsProtocolName);
//Fix the length of the list
protocolNameList->length = htons(n);
//Consider the 2-byte length field that precedes the list
n += sizeof(TlsProtocolNameList);
//Fix the length of the extension
extension->length = htons(n);
//Compute the length, in bytes, of the ALPN extension
n += sizeof(TlsExtension);
}
}
#endif
//Total number of bytes that have been written
*written = n;
//Successful processing
return NO_ERROR;
}
/**
* @brief Format ClientCertType extension
* @param[in] context Pointer to the TLS context
* @param[in] p Output stream where to write the ClientCertType extension
* @param[out] written Total number of bytes that have been written
* @return Error code
**/
error_t tlsFormatClientCertTypeExtension(TlsContext *context,
uint8_t *p, size_t *written)
{
size_t n = 0;
#if (TLS_RAW_PUBLIC_KEY_SUPPORT == ENABLED)
//An extension type must not appear in the ServerHello unless the same
//extension type appeared in the corresponding ClientHello
if(context->clientCertTypeExtReceived)
{
TlsExtension *extension;
//Add the ClientCertType extension
extension = (TlsExtension *) p;
//Type of the extension
extension->type = HTONS(TLS_EXT_CLIENT_CERT_TYPE);
//The ClientCertType extension in the ServerHello indicates the type
//of certificates the client is requested to provide in a subsequent
//certificate payload
extension->value[0] = context->peerCertFormat;
//The extension data field contains a single byte
n = sizeof(uint8_t);
//Fix the length of the extension
extension->length = htons(n);
//Compute the length, in bytes, of the ClientCertType extension
n += sizeof(TlsExtension);
}
#endif
//Total number of bytes that have been written
*written = n;
//Successful processing
return NO_ERROR;
}
/**
* @brief Format ServerCertType extension
* @param[in] context Pointer to the TLS context
* @param[in] p Output stream where to write the ServerCertType extension
* @param[out] written Total number of bytes that have been written
* @return Error code
**/
error_t tlsFormatServerCertTypeExtension(TlsContext *context,
uint8_t *p, size_t *written)
{
size_t n = 0;
#if (TLS_RAW_PUBLIC_KEY_SUPPORT == ENABLED)
//An extension type must not appear in the ServerHello unless the same
//extension type appeared in the corresponding ClientHello
if(context->serverCertTypeExtReceived)
{
TlsExtension *extension;
//Add the ServerCertType extension
extension = (TlsExtension *) p;
//Type of the extension
extension->type = HTONS(TLS_EXT_SERVER_CERT_TYPE);
//With the ServerCertType extension in the ServerHello, the TLS server
//indicates the certificate type carried in the certificate payload
extension->value[0] = context->certFormat;
//The extension data field contains a single byte
n = sizeof(uint8_t);
//Fix the length of the extension
extension->length = htons(n);
//Compute the length, in bytes, of the ServerCertType extension
n += sizeof(TlsExtension);
}
#endif
//Total number of bytes that have been written
*written = n;
//Successful processing
return NO_ERROR;
}
/**
* @brief Format ExtendedMasterSecret extension
* @param[in] context Pointer to the TLS context
* @param[in] p Output stream where to write the ExtendedMasterSecret extension
* @param[out] written Total number of bytes that have been written
* @return Error code
**/
error_t tlsFormatServerEmsExtension(TlsContext *context,
uint8_t *p, size_t *written)
{
size_t n = 0;
#if (TLS_EXT_MASTER_SECRET_SUPPORT == ENABLED)
//If the server receives a ClientHello without the ExtendedMasterSecret
//extension, then it must not include the extension in the ServerHello
if(context->emsExtReceived)
{
TlsExtension *extension;
//Add the ExtendedMasterSecret extension
extension = (TlsExtension *) p;
//Type of the extension
extension->type = HTONS(TLS_EXT_EXTENDED_MASTER_SECRET);
//The extension data field of this extension is empty
extension->length = HTONS(0);
//Compute the length, in bytes, of the ExtendedMasterSecret extension
n = sizeof(TlsExtension);
}
#endif
//Total number of bytes that have been written
*written = n;
//Successful processing
return NO_ERROR;
}
/**
* @brief Format SessionTicket extension
* @param[in] context Pointer to the TLS context
* @param[in] p Output stream where to write the SessionTicket extension
* @param[out] written Total number of bytes that have been written
* @return Error code
**/
error_t tlsFormatServerSessionTicketExtension(TlsContext *context,
uint8_t *p, size_t *written)
{
size_t n = 0;
#if (TLS_TICKET_SUPPORT == ENABLED)
//The server must not send this extension if it does not receive one in the
//ClientHello (refer to RFC 5077, section 3.2)
if(context->sessionTicketExtSent)
{
TlsExtension *extension;
//Add the SessionTicket extension
extension = (TlsExtension *) p;
//Type of the extension
extension->type = HTONS(TLS_EXT_SESSION_TICKET);
//The server uses a zero-length SessionTicket extension to indicate to the
//client that it will send a new session ticket using the NewSessionTicket
//handshake message
n = 0;
//Set the length of the extension
extension->length = htons(n);
//Compute the length, in bytes, of the SessionTicket extension
n += sizeof(TlsExtension);
}
#endif
//Total number of bytes that have been written
*written = n;
//Successful processing
return NO_ERROR;
}
/**
* @brief Format RenegotiationInfo extension
* @param[in] context Pointer to the TLS context
* @param[in] p Output stream where to write the RenegotiationInfo extension
* @param[out] written Total number of bytes that have been written
* @return Error code
**/
error_t tlsFormatServerRenegoInfoExtension(TlsContext *context,
uint8_t *p, size_t *written)
{
size_t n = 0;
#if (TLS_SECURE_RENEGOTIATION_SUPPORT == ENABLED)
//Check whether secure renegotiation is enabled
if(context->secureRenegoEnabled)
{
//During secure renegotiation, the server must include a renegotiation_info
//extension containing the saved client_verify_data and server_verify_data
if(context->secureRenegoFlag)
{
TlsExtension *extension;
TlsRenegoInfo *renegoInfo;
//Determine the length of the renegotiated_connection field
n = context->clientVerifyDataLen + context->serverVerifyDataLen;
//Add the RenegotiationInfo extension
extension = (TlsExtension *) p;
//Type of the extension
extension->type = HTONS(TLS_EXT_RENEGOTIATION_INFO);
//Point to the renegotiated_connection field
renegoInfo = (TlsRenegoInfo *) extension->value;
//Set the length of the verify data
renegoInfo->length = (uint8_t) n;
//Copy the saved client_verify_data
osMemcpy(renegoInfo->value, context->clientVerifyData,
context->clientVerifyDataLen);
//Copy the saved client_verify_data
osMemcpy(renegoInfo->value + context->clientVerifyDataLen,
context->serverVerifyData, context->serverVerifyDataLen);
//Consider the length field that precedes the renegotiated_connection
//field
n += sizeof(TlsRenegoInfo);
//Fix the length of the extension
extension->length = htons(n);
//Compute the length, in bytes, of the RenegotiationInfo extension
n += sizeof(TlsExtension);
}
}
#endif
//Total number of bytes that have been written
*written = n;
//Successful processing
return NO_ERROR;
}
/**
* @brief Parse SupportedVersions extension
* @param[in] context Pointer to the TLS context
* @param[in] supportedVersionList Pointer to the SupportedVersions extension
* @return Error code
**/
error_t tlsParseClientSupportedVersionsExtension(TlsContext *context,
const TlsSupportedVersionList *supportedVersionList)
{
error_t error;
uint_t i;
uint_t j;
uint_t n;
//Supported TLS versions
const uint16_t supportedVersions[] =
{
TLS_VERSION_1_3,
TLS_VERSION_1_2,
TLS_VERSION_1_1,
TLS_VERSION_1_0
};
//Initialize status code
error = ERROR_VERSION_NOT_SUPPORTED;
//Retrieve the number of items in the list
n = supportedVersionList->length / sizeof(uint16_t);
//Loop through the list of TLS versions supported by the server
for(i = 0; i < arraysize(supportedVersions) && error; i++)
{
//The extension contains a list of TLS versions supported by the client
for(j = 0; j < n && error; j++)
{
//Servers must only select a version of TLS present in that extension
//and must ignore any unknown versions
if(ntohs(supportedVersionList->value[j]) == supportedVersions[i])
{
//Set the TLS version to be used
error = tlsSelectVersion(context, supportedVersions[i]);
}
}
}
//Return status code
return error;
}
/**
* @brief Parse SNI extension
* @param[in] context Pointer to the TLS context
* @param[in] serverNameList Pointer to the SNI extension
* @return Error code
**/
error_t tlsParseClientSniExtension(TlsContext *context,
const TlsServerNameList *serverNameList)
{
#if (TLS_SNI_SUPPORT == ENABLED)
//SNI extension found?
if(serverNameList != NULL)
{
size_t i;
size_t n;
size_t length;
const TlsServerName *serverName;
//In order to provide the server name, clients may include ServerName
//extension
if(context->serverName != NULL)
{
//Release memory
tlsFreeMem(context->serverName);
context->serverName = NULL;
}
//Retrieve the length of the list
length = ntohs(serverNameList->length);
//Loop through the list of server names advertised by the client
for(i = 0; i < length; i += sizeof(TlsServerName) + n)
{
//Point to the current server name
serverName = (TlsServerName *) (serverNameList->value + i);
//Malformed extension?
if(length < (i + sizeof(TlsServerName)))
return ERROR_DECODING_FAILED;
if(length < (i + sizeof(TlsServerName) + ntohs(serverName->length)))
return ERROR_DECODING_FAILED;
//Retrieve the length of the server name
n = ntohs(serverName->length);
//Empty strings must not be included in the list
if(n == 0)
return ERROR_DECODING_FAILED;
//Currently, the only server names supported are DNS hostnames
if(serverName->type == TLS_NAME_TYPE_HOSTNAME)
{
//The server name must be a valid DNS hostname
if(!tlsCheckDnsHostname(serverName->hostname, n))
return ERROR_ILLEGAL_PARAMETER;
//The ServerNameList must not contain more than one name of the
//same type (refer to RFC 6066, section 3)
if(context->serverName != NULL)
return ERROR_ILLEGAL_PARAMETER;
//Check the length of the name
if(n <= TLS_MAX_SERVER_NAME_LEN)
{
//Allocate a memory block to hold the server name
context->serverName = tlsAllocMem(n + 1);
//Failed to allocate memory?
if(context->serverName == NULL)
return ERROR_OUT_OF_MEMORY;
//Save server name
osMemcpy(context->serverName, serverName->hostname, n);
//Properly terminate the string with a NULL character
context->serverName[n] = '\0';
}
}
}
}
#endif
//Successful processing
return NO_ERROR;
}
/**
* @brief Parse MaxFragmentLength extension
* @param[in] context Pointer to the TLS context
* @param[in] maxFragLen Pointer to the MaxFragmentLength extension
* @return Error code
**/
error_t tlsParseClientMaxFragLenExtension(TlsContext *context,
const TlsExtension *maxFragLen)
{
error_t error;
//Initialize status code
error = NO_ERROR;
#if (TLS_MAX_FRAG_LEN_SUPPORT == ENABLED)
//MaxFragmentLength extension found?
if(maxFragLen != NULL)
{
size_t n;
//Retrieve the value advertised by the client
switch(maxFragLen->value[0])
{
case TLS_MAX_FRAGMENT_LENGTH_512:
n = 512;
break;
case TLS_MAX_FRAGMENT_LENGTH_1024:
n = 1024;
break;
case TLS_MAX_FRAGMENT_LENGTH_2048:
n = 2048;
break;
case TLS_MAX_FRAGMENT_LENGTH_4096:
n = 4096;
break;
default:
n = 0;
break;
}
//Acceptable value?
if(n > 0)
{
//Once a maximum fragment length has been successfully negotiated,
//the server must immediately begin fragmenting messages (including
//handshake messages) to ensure that no fragment larger than the
//negotiated length is sent
context->maxFragLen = n;
}
else
{
//If a server receives a maximum fragment length negotiation request
//for a value other than the allowed values, it must abort the handshake
//with an illegal_parameter alert
error = ERROR_ILLEGAL_PARAMETER;
}
//The ClientHello includes a MaxFragmentLength extension
context->maxFragLenExtReceived = TRUE;
}
else
{
//The ClientHello does not contain any MaxFragmentLength extension
context->maxFragLenExtReceived = FALSE;
}
#endif
//Return status code
return error;
}
/**
* @brief Parse RecordSizeLimit extension
* @param[in] context Pointer to the TLS context
* @param[in] recordSizeLimit Pointer to the RecordSizeLimit extension
* @return Error code
**/
error_t tlsParseClientRecordSizeLimitExtension(TlsContext *context,
const TlsExtension *recordSizeLimit)
{
#if (TLS_RECORD_SIZE_LIMIT_SUPPORT == ENABLED)
//RecordSizeLimit extension found?
if(recordSizeLimit != NULL)
{
uint16_t n;
//The value of RecordSizeLimit is the maximum size of record in octets
//that the peer is willing to receive
n = LOAD16BE(recordSizeLimit->value);
//Endpoints must not send a RecordSizeLimit extension with a value
//smaller than 64
if(n < 64)
{
//An endpoint must treat receipt of a smaller value as a fatal error
//and generate an illegal_parameter alert
return ERROR_ILLEGAL_PARAMETER;
}
//TLS 1.3 currently selected?
if(context->version == TLS_VERSION_1_3)
{
//The value includes the content type and padding added in TLS 1.3
n--;
}
//Initial or updated ClientHello?
if(context->state == TLS_STATE_CLIENT_HELLO_2)
{
//When responding to a HelloRetryRequest, the client must send the
//same ClientHello without modification
if(!context->recordSizeLimitExtReceived ||
context->recordSizeLimit != n)
{
return ERROR_ILLEGAL_PARAMETER;
}
}
//The peer can include any limit up to the protocol-defined limit for
//maximum record size. Even if a larger value is provided by a peer, an
//endpoint must not send records larger than the protocol-defined limit
context->recordSizeLimit = MIN(n, TLS_MAX_RECORD_LENGTH);
//The ClientHello includes a RecordSizeLimit extension
context->recordSizeLimitExtReceived = TRUE;
}
else
{
//Initial or updated ClientHello?
if(context->state == TLS_STATE_CLIENT_HELLO_2)
{
//When responding to a HelloRetryRequest, the client must send the
//same ClientHello without modification
if(context->recordSizeLimitExtReceived)
return ERROR_ILLEGAL_PARAMETER;
}
//If this extension is not negotiated, endpoints can send records of any
//size permitted by the protocol or other negotiated extensions
context->recordSizeLimit = TLS_MAX_RECORD_LENGTH;
//The RecordSizeLimit extension is not supported by the client
context->recordSizeLimitExtReceived = FALSE;
}
#endif
//Successful processing
return NO_ERROR;
}
/**
* @brief Parse EcPointFormats extension
* @param[in] context Pointer to the TLS context
* @param[in] ecPointFormatList Pointer to the EcPointFormats extension
* @return Error code
**/
error_t tlsParseClientEcPointFormatsExtension(TlsContext *context,
const TlsEcPointFormatList *ecPointFormatList)
{
error_t error;
#if (TLS_MAX_VERSION >= TLS_VERSION_1_0 && TLS_MIN_VERSION <= TLS_VERSION_1_2)
//Initialize status code
error = NO_ERROR;
#if (TLS_ECDH_ANON_KE_SUPPORT == ENABLED || TLS_ECDHE_RSA_KE_SUPPORT == ENABLED || \
TLS_ECDHE_ECDSA_KE_SUPPORT == ENABLED || TLS_ECDHE_PSK_KE_SUPPORT == ENABLED)
//EcPointFormats extension found?
if(ecPointFormatList != NULL)
{
uint_t i;
//The ClientHello includes a EcPointFormats extension
context->ecPointFormatsExtReceived = TRUE;
//Loop through the list of supported EC point formats
for(i = 0; i < ecPointFormatList->length; i++)
{
//Uncompressed point format?
if(ecPointFormatList->value[i] == TLS_EC_POINT_FORMAT_UNCOMPRESSED)
{
break;
}
}
//The uncompressed point format must be supported by any TLS application
//that supports this extension (refer to RFC 4492, section 5.1)
if(i >= ecPointFormatList->length)
{
//Report an error
error = ERROR_ILLEGAL_PARAMETER;
}
}
else
{
//If no SupportedPointsFormat extension is sent, the uncompressed format
//has to be used
context->ecPointFormatsExtReceived = FALSE;
}
#endif
#else
//Not implemented
error = ERROR_NOT_IMPLEMENTED;
#endif
//Return status code
return error;
}
/**
* @brief Parse ALPN extension
* @param[in] context Pointer to the TLS context
* @param[in] protocolNameList Pointer to the ALPN extension
* @return Error code
**/
error_t tlsParseClientAlpnExtension(TlsContext *context,
const TlsProtocolNameList *protocolNameList)
{
#if (TLS_ALPN_SUPPORT == ENABLED)
//The protocol identified in the ALPN extension type in the ServerHello
//shall be definitive for the connection, until renegotiated (refer to
//RFC 7301, section 3.2)
if(context->selectedProtocol != NULL)
{
//Release memory
tlsFreeMem(context->selectedProtocol);
context->selectedProtocol = NULL;
}
//ALPN extension found?
if(protocolNameList != NULL)
{
size_t i;
size_t n;
size_t length;
const TlsProtocolName *protocolName;
//Retrieve the length of the list
length = ntohs(protocolNameList->length);
//The list must not be be empty
if(length == 0)