-
Notifications
You must be signed in to change notification settings - Fork 16
/
tls_client_extensions.c
1638 lines (1375 loc) · 49 KB
/
tls_client_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_client_extensions.c
* @brief Formatting and parsing of extensions (TLS client)
*
* @section License
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* Copyright (C) 2010-2024 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.4.4
**/
//Switch to the appropriate trace level
#define TRACE_LEVEL TLS_TRACE_LEVEL
//Dependencies
#include "tls.h"
#include "tls_cipher_suites.h"
#include "tls_client_extensions.h"
#include "tls_client_misc.h"
#include "tls_extensions.h"
#include "tls_ffdhe.h"
#include "tls_misc.h"
#include "debug.h"
//Check TLS library configuration
#if (TLS_SUPPORT == ENABLED && TLS_CLIENT_SUPPORT == ENABLED)
//List of supported ECDHE or FFDHE groups
const uint16_t tlsSupportedGroups[] =
{
TLS_GROUP_X25519_KYBER768_DRAFT00,
TLS_GROUP_SECP256R1_KYBER768_DRAFT00,
TLS_GROUP_X25519,
TLS_GROUP_X448,
TLS_GROUP_SECP160R1,
TLS_GROUP_SECP160R2,
TLS_GROUP_SECP192K1,
TLS_GROUP_SECP192R1,
TLS_GROUP_SECP224K1,
TLS_GROUP_SECP224R1,
TLS_GROUP_SECP256K1,
TLS_GROUP_SECP256R1,
TLS_GROUP_SECP384R1,
TLS_GROUP_SECP521R1,
TLS_GROUP_BRAINPOOLP256R1_TLS13,
TLS_GROUP_BRAINPOOLP384R1_TLS13,
TLS_GROUP_BRAINPOOLP512R1_TLS13,
TLS_GROUP_BRAINPOOLP256R1,
TLS_GROUP_BRAINPOOLP384R1,
TLS_GROUP_BRAINPOOLP512R1,
TLS_GROUP_CURVE_SM2,
TLS_GROUP_FFDHE2048,
TLS_GROUP_FFDHE3072,
TLS_GROUP_FFDHE4096,
TLS_GROUP_FFDHE6144,
TLS_GROUP_FFDHE8192
};
/**
* @brief Format SupportedVersions extension
* @param[in] context Pointer to the TLS context
* @param[in] p Output stream where to write the SupportedVersions extension
* @param[out] written Total number of bytes that have been written
* @return Error code
**/
error_t tlsFormatClientSupportedVersionsExtension(TlsContext *context,
uint8_t *p, size_t *written)
{
size_t n = 0;
#if (TLS_MAX_VERSION >= TLS_VERSION_1_2 && TLS_MIN_VERSION <= TLS_VERSION_1_3)
//In TLS 1.2, the client can indicate its version preferences in the
//SupportedVersions extension, in preference to the legacy_version field
//of the ClientHello
if(context->versionMax >= TLS_VERSION_1_2)
{
TlsExtension *extension;
TlsSupportedVersionList *supportedVersionList;
//Add the SupportedVersions extension
extension = (TlsExtension *) p;
//Type of the extension
extension->type = HTONS(TLS_EXT_SUPPORTED_VERSIONS);
//Point to the extension data field
supportedVersionList = (TlsSupportedVersionList *) extension->value;
//The extension contains a list of supported versions in preference
//order, with the most preferred version first
n = 0;
#if (DTLS_SUPPORT == ENABLED)
//DTLS protocol?
if(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_DATAGRAM)
{
//Check whether DTLS 1.2 is supported
if(context->versionMax >= TLS_VERSION_1_2 &&
context->versionMin <= TLS_VERSION_1_2)
{
supportedVersionList->value[n++] = HTONS(DTLS_VERSION_1_2);
}
//Check whether DTLS 1.0 is supported
if(context->versionMax >= TLS_VERSION_1_1 &&
context->versionMin <= TLS_VERSION_1_1)
{
supportedVersionList->value[n++] = HTONS(DTLS_VERSION_1_0);
}
}
else
#endif
//TLS protocol?
{
//Check whether TLS 1.3 is supported
if(context->versionMax >= TLS_VERSION_1_3 &&
context->versionMin <= TLS_VERSION_1_3)
{
supportedVersionList->value[n++] = HTONS(TLS_VERSION_1_3);
}
//Check whether TLS 1.2 is supported
if(context->versionMax >= TLS_VERSION_1_2 &&
context->versionMin <= TLS_VERSION_1_2)
{
supportedVersionList->value[n++] = HTONS(TLS_VERSION_1_2);
}
//Check whether TLS 1.1 is supported
if(context->versionMax >= TLS_VERSION_1_1 &&
context->versionMin <= TLS_VERSION_1_1)
{
supportedVersionList->value[n++] = HTONS(TLS_VERSION_1_1);
}
//Check whether TLS 1.0 is supported
if(context->versionMax >= TLS_VERSION_1_0 &&
context->versionMin <= TLS_VERSION_1_0)
{
supportedVersionList->value[n++] = HTONS(TLS_VERSION_1_0);
}
}
//Compute the length, in bytes, of the list
n *= sizeof(uint16_t);
//Fix the length of the list
supportedVersionList->length = (uint8_t) n;
//Consider the length field that precedes the list
n += sizeof(TlsSupportedVersionList);
//Fix the length of the extension
extension->length = htons(n);
//Compute the length, in bytes, of the SupportedVersions extension
n += sizeof(TlsExtension);
}
#endif
//Total number of bytes that have been written
*written = n;
//Successful processing
return NO_ERROR;
}
/**
* @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 tlsFormatClientSniExtension(TlsContext *context,
uint8_t *p, size_t *written)
{
size_t n = 0;
#if (TLS_SNI_SUPPORT == ENABLED)
//In order to provide the server name, clients may include a ServerName
//extension
if(context->serverName != NULL)
{
//Determine the length of the server name
n = osStrlen(context->serverName);
//The server name must be a valid DNS hostname
if(tlsCheckDnsHostname(context->serverName, n))
{
TlsExtension *extension;
TlsServerNameList *serverNameList;
TlsServerName *serverName;
//Add SNI (Server Name Indication) extension
extension = (TlsExtension *) p;
//Type of the extension
extension->type = HTONS(TLS_EXT_SERVER_NAME);
//Point to the list of server names
serverNameList = (TlsServerNameList *) extension->value;
//In practice, current client implementations only send one name
serverName = (TlsServerName *) serverNameList->value;
//Fill in the type and the length fields
serverName->type = TLS_NAME_TYPE_HOSTNAME;
serverName->length = htons(n);
//Copy server name
osMemcpy(serverName->hostname, context->serverName, n);
//Compute the length, in byte, of the structure
n += sizeof(TlsServerName);
//Fix the length of the list
serverNameList->length = htons(n);
//Consider the 2-byte length field that precedes the list
n += sizeof(TlsServerNameList);
//Fix the length of the extension
extension->length = htons(n);
//Compute the length, in bytes, of the ServerName extension
n += sizeof(TlsExtension);
}
else
{
//The server name is not a valid DNS hostname
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 tlsFormatClientMaxFragLenExtension(TlsContext *context,
uint8_t *p, size_t *written)
{
size_t n = 0;
#if (TLS_MAX_FRAG_LEN_SUPPORT == ENABLED)
//In order to negotiate smaller maximum fragment lengths, clients may
//include a MaxFragmentLength extension
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);
//Set the 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 tlsFormatClientRecordSizeLimitExtension(TlsContext *context,
uint8_t *p, size_t *written)
{
size_t n = 0;
#if (TLS_RECORD_SIZE_LIMIT_SUPPORT == ENABLED)
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);
//Check whether TLS 1.3 is supported
if(context->versionMax >= TLS_VERSION_1_3 &&
(context->transportProtocol == TLS_TRANSPORT_PROTOCOL_STREAM ||
context->transportProtocol == TLS_TRANSPORT_PROTOCOL_EAP))
{
//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 SupportedGroups extension
* @param[in] context Pointer to the TLS context
* @param[in] p Output stream where to write the SupportedGroups extension
* @param[out] written Total number of bytes that have been written
* @return Error code
**/
error_t tlsFormatSupportedGroupsExtension(TlsContext *context, uint8_t *p,
size_t *written)
{
size_t n = 0;
#if (TLS_ECDH_SUPPORT == ENABLED || TLS_FFDHE_SUPPORT == ENABLED || \
TLS_HYBRID_SUPPORT == ENABLED)
uint_t i;
uint_t numSupportedGroups;
const uint16_t *supportedGroups;
TlsExtension *extension;
TlsSupportedGroupList *supportedGroupList;
//Add the SupportedGroups extension
extension = (TlsExtension *) p;
//Type of the extension
extension->type = HTONS(TLS_EXT_SUPPORTED_GROUPS);
//Point to the list of supported groups
supportedGroupList = (TlsSupportedGroupList *) extension->value;
//Any preferred ECDHE or FFDHE groups?
if(context->numSupportedGroups > 0)
{
//Point to the list of preferred named groups
supportedGroups = context->supportedGroups;
numSupportedGroups = context->numSupportedGroups;
}
else
{
//Point to the list of default named groups
supportedGroups = tlsSupportedGroups;
numSupportedGroups = arraysize(tlsSupportedGroups);
}
//The groups are ordered according to client's preferences
n = 0;
//Loop through the list of named groups
for(i = 0; i < numSupportedGroups; i++)
{
#if (TLS_FFDHE_SUPPORT == ENABLED)
//Finite field group?
if(tlsGetFfdheGroup(context, supportedGroups[i]) != NULL)
{
//Any Diffie-Hellman cipher suite proposed by the client?
if((context->cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_DH) != 0 ||
(context->cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_TLS13) != 0)
{
//Add the current named group to the list
supportedGroupList->value[n++] = htons(supportedGroups[i]);
}
}
else
#endif
#if (TLS_ECDH_SUPPORT == ENABLED)
//Elliptic curve group?
if(tlsGetCurveInfo(context, supportedGroups[i]) != NULL)
{
//SM2 elliptic curve?
if(supportedGroups[i] == TLS_GROUP_CURVE_SM2)
{
//Any ShangMi cipher suite proposed by the client?
if((context->cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_SM) != 0)
{
//Add the current named group to the list
supportedGroupList->value[n++] = htons(supportedGroups[i]);
}
}
else
{
//Any ECDH cipher suite proposed by the client?
if((context->cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_ECDH) != 0 ||
(context->cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_TLS13) != 0)
{
//Add the current named group to the list
supportedGroupList->value[n++] = htons(supportedGroups[i]);
}
}
}
else
#endif
#if (TLS_HYBRID_SUPPORT == ENABLED)
//Hybrid key exchange method?
if(tls13GetTraditionalAlgo(context, supportedGroups[i]) != NULL &&
tls13GetNextGenAlgo(context, supportedGroups[i]) != NULL)
{
//Any TLS 1.3 cipher suite proposed by the client?
if((context->cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_TLS13) != 0)
{
//Add the current named group to the list
supportedGroupList->value[n++] = htons(supportedGroups[i]);
}
}
else
#endif
//Unknown group?
{
//Discard current named group
}
}
//If the client supports and wants ECDHE and FFDHE key exchanges, it must
//use a single SupportedGroups extension to include all supported groups
//(both ECDHE and FFDHE groups)
if(n > 0)
{
//Compute the length, in bytes, of the list
n *= 2;
//Fix the length of the list
supportedGroupList->length = htons(n);
//Consider the 2-byte length field that precedes the list
n += sizeof(TlsSupportedGroupList);
//Fix the length of the extension
extension->length = htons(n);
//Compute the length, in bytes, of the SupportedGroups 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 tlsFormatClientEcPointFormatsExtension(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->versionMin <= 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)
//A client that proposes ECC cipher suites in its ClientHello message
//should send the EcPointFormats extension
if((context->cipherSuiteTypes & TLS_CIPHER_SUITE_TYPE_ECDH) != 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 client's preferences
n = 0;
//The client 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 tlsFormatClientAlpnExtension(TlsContext *context,
uint8_t *p, size_t *written)
{
size_t n = 0;
#if (TLS_ALPN_SUPPORT == ENABLED)
//The ALPN extension contains the list of protocols advertised by the
//client, in descending order of preference
if(context->protocolList != NULL)
{
uint_t i;
uint_t j;
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;
//Move back to the beginning of the list
i = 0;
j = 0;
n = 0;
//Parse the list of supported protocols
do
{
//Delimiter character found?
if(context->protocolList[i] == ',' || context->protocolList[i] == '\0')
{
//Discard empty tokens
if((i - j) > 0)
{
//Point to the protocol name
protocolName = (TlsProtocolName *) (protocolNameList->value + n);
//Fill in the length field
protocolName->length = i - j;
//Copy protocol name
osMemcpy(protocolName->value, context->protocolList + j, i - j);
//Adjust the length of the list
n += sizeof(TlsProtocolName) + i - j;
}
//Move to the next token
j = i + 1;
}
//Loop until the NULL character is reached
} while(context->protocolList[i++] != '\0');
//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 tlsFormatClientCertTypeListExtension(TlsContext *context,
uint8_t *p, size_t *written)
{
size_t n = 0;
#if (TLS_RAW_PUBLIC_KEY_SUPPORT == ENABLED)
TlsExtension *extension;
TlsCertTypeList *clientCertTypeList;
//Add the ClientCertType extension
extension = (TlsExtension *) p;
//Type of the extension
extension->type = HTONS(TLS_EXT_CLIENT_CERT_TYPE);
//The ClientCertType extension in the ClientHello indicates the certificate
//types the client is able to provide to the server, when requested using a
//CertificateRequest message
clientCertTypeList = (TlsCertTypeList *) extension->value;
//The ClientCertType extension carries a list of supported certificate
//types, sorted by client preference
n = 0;
//Raw public key type
clientCertTypeList->value[n++] = TLS_CERT_FORMAT_RAW_PUBLIC_KEY;
//X.509 certificate type
clientCertTypeList->value[n++] = TLS_CERT_FORMAT_X509;
//Fix the length of the list
clientCertTypeList->length = (uint8_t) n;
//Consider the length field that precedes the list
n += sizeof(TlsCertTypeList);
//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 tlsFormatServerCertTypeListExtension(TlsContext *context,
uint8_t *p, size_t *written)
{
size_t n = 0;
#if (TLS_RAW_PUBLIC_KEY_SUPPORT == ENABLED)
//Ensure the client is able to process raw public keys
if(context->rpkVerifyCallback != NULL)
{
TlsExtension *extension;
TlsCertTypeList *serverCertTypeList;
//Add the ServerCertType extension
extension = (TlsExtension *) p;
//Type of the extension
extension->type = HTONS(TLS_EXT_SERVER_CERT_TYPE);
//The ServerCertType extension in the ClientHello indicates the types of
//certificates the client is able to process when provided by the server
//in a subsequent certificate payload
serverCertTypeList = (TlsCertTypeList *) extension->value;
//The ServerCertType extension carries a list of supported certificate
//types, sorted by client preference
n = 0;
//Raw public key type
serverCertTypeList->value[n++] = TLS_CERT_FORMAT_RAW_PUBLIC_KEY;
//X.509 certificate type
serverCertTypeList->value[n++] = TLS_CERT_FORMAT_X509;
//Fix the length of the list
serverCertTypeList->length = (uint8_t) n;
//Consider the length field that precedes the list
n += sizeof(TlsCertTypeList);
//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 EncryptThenMac extension
* @param[in] context Pointer to the TLS context
* @param[in] p Output stream where to write the EncryptThenMac extension
* @param[out] written Total number of bytes that have been written
* @return Error code
**/
error_t tlsFormatClientEtmExtension(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)
//In versions of TLS prior to TLS 1.3, this extension is used to negotiate
//encrypt-then-MAC construction rather than the default MAC-then-encrypt
if(context->versionMax >= TLS_VERSION_1_0 &&
context->versionMin <= TLS_VERSION_1_2)
{
#if (TLS_ENCRYPT_THEN_MAC_SUPPORT == ENABLED)
TlsExtension *extension;
//Add the EncryptThenMac extension
extension = (TlsExtension *) p;
//Type of the extension
extension->type = HTONS(TLS_EXT_ENCRYPT_THEN_MAC);
//The extension data field of this extension shall be empty (refer to
//RFC 7366, section 2)
extension->length = HTONS(0);
//Compute the length, in bytes, of the EncryptThenMac extension
n = sizeof(TlsExtension);
#endif
}
#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 tlsFormatClientEmsExtension(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)
//Implementations which support both TLS 1.3 and earlier versions should
//indicate the use of the ExtendedMasterSecret extension whenever TLS 1.3
//is used (refer to RFC 8446, appendix D)
if(context->versionMax >= TLS_VERSION_1_0 &&
context->versionMin <= TLS_VERSION_1_2)
{
#if (TLS_EXT_MASTER_SECRET_SUPPORT == ENABLED)
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
}
#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 tlsFormatClientSessionTicketExtension(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)
//In versions of TLS prior to TLS 1.3, the SessionTicket extension is used
//to resume a TLS session without requiring session-specific state at the
//TLS server
if(context->versionMin <= TLS_VERSION_1_2)
{
#if (TLS_TICKET_SUPPORT == ENABLED)
//Check whether session ticket mechanism is enabled
if(context->sessionTicketEnabled)
{
TlsExtension *extension;
//Add the SessionTicket extension
extension = (TlsExtension *) p;
//Type of the extension
extension->type = HTONS(TLS_EXT_SESSION_TICKET);
//Valid ticket?
if(tlsIsTicketValid(context))
{
//If the client possesses a ticket that it wants to use to resume
//a session, then it includes the ticket in the SessionTicket
//extension in the ClientHello
osMemcpy(extension->value, context->ticket, context->ticketLen);
//The extension_data field of SessionTicket extension contains the
//ticket
n = context->ticketLen;
}
else
{
//If the client does not have a ticket and is prepared to receive
//one in the NewSessionTicket handshake message, then it must
//include a zero-length ticket in the SessionTicket extension
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
}
#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 tlsFormatClientRenegoInfoExtension(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 forbids renegotiation
if(context->versionMin <= TLS_VERSION_1_2)
{
#if (TLS_SECURE_RENEGOTIATION_SUPPORT == ENABLED)
//Check whether secure renegotiation is enabled
if(context->secureRenegoEnabled)
{
//During secure renegotiation, the client must include the
//RenegotiationInfo extension containing the saved client_verify_data
if(context->secureRenegoFlag)
{
TlsExtension *extension;
TlsRenegoInfo *renegoInfo;
//Determine the length of the verify data
n = context->clientVerifyDataLen;
//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 verify data from the Finished message sent by the client
//on the immediately previous handshake
osMemcpy(renegoInfo->value, context->clientVerifyData, n);
//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
}
#endif
//Total number of bytes that have been written
*written = n;