-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathssl_api_ext.c
More file actions
2469 lines (2162 loc) · 74.2 KB
/
Copy pathssl_api_ext.c
File metadata and controls
2469 lines (2162 loc) · 74.2 KB
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
/* ssl_api_ext.c
*
* Copyright (C) 2006-2026 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL 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 3 of the License, or
* (at your option) any later version.
*
* wolfSSL 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-1335, USA
*/
#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
#if !defined(WOLFSSL_SSL_API_EXT_INCLUDED)
#ifndef WOLFSSL_IGNORE_FILE_WARN
#warning ssl_api_ext.c does not need to be compiled separately from ssl.c
#endif
#else
#ifndef WOLFCRYPT_ONLY
#ifndef NO_TLS
#ifdef HAVE_SNI
/* Set the Server Name Indication extension data on the object.
*
* @param [in] ssl SSL/TLS object.
* @param [in] type SNI type, e.g. WOLFSSL_SNI_HOST_NAME.
* @param [in] data SNI data.
* @param [in] size Length of SNI data in bytes.
* @return WOLFSSL_SUCCESS on success.
* @return BAD_FUNC_ARG when ssl is NULL.
* @return Negative value on error.
*/
WOLFSSL_ABI
int wolfSSL_UseSNI(WOLFSSL* ssl, byte type, const void* data, word16 size)
{
if (ssl == NULL)
return BAD_FUNC_ARG;
return TLSX_UseSNI(&ssl->extensions, type, data, size, ssl->heap);
}
/* Set the Server Name Indication extension data on the context.
*
* @param [in] ctx SSL/TLS context object.
* @param [in] type SNI type, e.g. WOLFSSL_SNI_HOST_NAME.
* @param [in] data SNI data.
* @param [in] size Length of SNI data in bytes.
* @return WOLFSSL_SUCCESS on success.
* @return BAD_FUNC_ARG when ctx is NULL.
* @return Negative value on error.
*/
WOLFSSL_ABI
int wolfSSL_CTX_UseSNI(WOLFSSL_CTX* ctx, byte type, const void* data,
word16 size)
{
if (ctx == NULL)
return BAD_FUNC_ARG;
return TLSX_UseSNI(&ctx->extensions, type, data, size, ctx->heap);
}
#ifndef NO_WOLFSSL_SERVER
/* Set options for the Server Name Indication extension on the object.
*
* @param [in] ssl SSL/TLS object.
* @param [in] type SNI type.
* @param [in] options Bitmask of SNI options.
*/
void wolfSSL_SNI_SetOptions(WOLFSSL* ssl, byte type, byte options)
{
if ((ssl != NULL) && (ssl->extensions != NULL)) {
TLSX_SNI_SetOptions(ssl->extensions, type, options);
}
}
/* Set options for the Server Name Indication extension on the context.
*
* @param [in] ctx SSL/TLS context object.
* @param [in] type SNI type.
* @param [in] options Bitmask of SNI options.
*/
void wolfSSL_CTX_SNI_SetOptions(WOLFSSL_CTX* ctx, byte type, byte options)
{
if ((ctx != NULL) && (ctx->extensions != NULL)) {
TLSX_SNI_SetOptions(ctx->extensions, type, options);
}
}
/* Get the status of the Server Name Indication extension on the object.
*
* @param [in] ssl SSL/TLS object.
* @param [in] type SNI type.
* @return SNI status for the type.
*/
byte wolfSSL_SNI_Status(WOLFSSL* ssl, byte type)
{
return TLSX_SNI_Status((ssl != NULL) ? ssl->extensions : NULL, type);
}
/* Get the Server Name Indication request data received from the peer.
*
* @param [in] ssl SSL/TLS object.
* @param [in] type SNI type.
* @param [out] data Pointer to the SNI request data. May be NULL.
* @return Length of the SNI request data in bytes, or 0 when none.
*/
word16 wolfSSL_SNI_GetRequest(WOLFSSL* ssl, byte type, void** data)
{
if (data)
*data = NULL;
if (ssl && ssl->extensions)
return TLSX_SNI_GetRequest(ssl->extensions, type, data, 0);
return 0;
}
/* Get the Server Name Indication data from a raw ClientHello buffer.
*
* @param [in] clientHello ClientHello message buffer.
* @param [in] helloSz Length of the ClientHello in bytes.
* @param [in] type SNI type.
* @param [out] sni Buffer to hold the SNI data.
* @param [in, out] inOutSz In: size of buffer. Out: length of SNI data.
* @return WOLFSSL_SUCCESS on success.
* @return BAD_FUNC_ARG when an argument is NULL or a size is zero.
*/
int wolfSSL_SNI_GetFromBuffer(const byte* clientHello, word32 helloSz,
byte type, byte* sni, word32* inOutSz)
{
if (clientHello && helloSz > 0 && sni && inOutSz && *inOutSz > 0)
return TLSX_SNI_GetFromBuffer(clientHello, helloSz, type, sni, inOutSz);
return BAD_FUNC_ARG;
}
#endif /* !NO_WOLFSSL_SERVER */
#endif /* HAVE_SNI */
#ifdef HAVE_TRUSTED_CA
/* Set the Trusted CA Indication extension on the object.
*
* @param [in] ssl SSL/TLS object.
* @param [in] type Trusted CA identifier type.
* @param [in] certId Certificate identifier data.
* @param [in] certIdSz Length of certificate identifier in bytes.
* @return WOLFSSL_SUCCESS on success.
* @return BAD_FUNC_ARG when ssl is NULL or arguments are inconsistent with
* the type.
*/
int wolfSSL_UseTrustedCA(WOLFSSL* ssl, byte type,
const byte* certId, word32 certIdSz)
{
if (ssl == NULL)
return BAD_FUNC_ARG;
if (type == WOLFSSL_TRUSTED_CA_PRE_AGREED) {
if (certId != NULL || certIdSz != 0)
return BAD_FUNC_ARG;
}
else if (type == WOLFSSL_TRUSTED_CA_X509_NAME) {
if (certId == NULL || certIdSz == 0)
return BAD_FUNC_ARG;
}
#ifndef NO_SHA
else if (type == WOLFSSL_TRUSTED_CA_KEY_SHA1 ||
type == WOLFSSL_TRUSTED_CA_CERT_SHA1) {
if (certId == NULL || certIdSz != WC_SHA_DIGEST_SIZE)
return BAD_FUNC_ARG;
}
#endif
else
return BAD_FUNC_ARG;
return TLSX_UseTrustedCA(&ssl->extensions,
type, certId, certIdSz, ssl->heap);
}
#endif /* HAVE_TRUSTED_CA */
#ifdef HAVE_MAX_FRAGMENT
#ifndef NO_WOLFSSL_CLIENT
/* Set the Maximum Fragment Length extension on the object.
*
* @param [in] ssl SSL/TLS object.
* @param [in] mfl Maximum fragment length code, e.g. WOLFSSL_MFL_2_9.
* @return WOLFSSL_SUCCESS on success.
* @return BAD_FUNC_ARG when ssl is NULL.
* @return Negative value on error.
*/
int wolfSSL_UseMaxFragment(WOLFSSL* ssl, byte mfl)
{
if (ssl == NULL)
return BAD_FUNC_ARG;
#ifdef WOLFSSL_ALLOW_MAX_FRAGMENT_ADJUST
/* The following is a non-standard way to reconfigure the max packet size
post-handshake for wolfSSL_write/wolfSSL_read */
if (ssl->options.handShakeState == HANDSHAKE_DONE) {
switch (mfl) {
case WOLFSSL_MFL_2_8 : ssl->max_fragment = 256; break;
case WOLFSSL_MFL_2_9 : ssl->max_fragment = 512; break;
case WOLFSSL_MFL_2_10: ssl->max_fragment = 1024; break;
case WOLFSSL_MFL_2_11: ssl->max_fragment = 2048; break;
case WOLFSSL_MFL_2_12: ssl->max_fragment = 4096; break;
case WOLFSSL_MFL_2_13: ssl->max_fragment = 8192; break;
default: ssl->max_fragment = MAX_RECORD_SIZE; break;
}
return WOLFSSL_SUCCESS;
}
#endif /* WOLFSSL_MAX_FRAGMENT_ADJUST */
/* This call sets the max fragment TLS extension, which gets sent to server.
The server_hello response is what sets the `ssl->max_fragment` in
TLSX_MFL_Parse */
return TLSX_UseMaxFragment(&ssl->extensions, mfl, ssl->heap);
}
/* Set the Maximum Fragment Length extension on the context.
*
* @param [in] ctx SSL/TLS context object.
* @param [in] mfl Maximum fragment length code, e.g. WOLFSSL_MFL_2_9.
* @return WOLFSSL_SUCCESS on success.
* @return BAD_FUNC_ARG when ctx is NULL.
* @return Negative value on error.
*/
int wolfSSL_CTX_UseMaxFragment(WOLFSSL_CTX* ctx, byte mfl)
{
if (ctx == NULL)
return BAD_FUNC_ARG;
return TLSX_UseMaxFragment(&ctx->extensions, mfl, ctx->heap);
}
#endif /* NO_WOLFSSL_CLIENT */
#endif /* HAVE_MAX_FRAGMENT */
#ifdef HAVE_TRUNCATED_HMAC
#ifndef NO_WOLFSSL_CLIENT
/* Set the Truncated HMAC extension on the object.
*
* @param [in] ssl SSL/TLS object.
* @return WOLFSSL_SUCCESS on success.
* @return BAD_FUNC_ARG when ssl is NULL.
* @return Negative value on error.
*/
int wolfSSL_UseTruncatedHMAC(WOLFSSL* ssl)
{
if (ssl == NULL)
return BAD_FUNC_ARG;
return TLSX_UseTruncatedHMAC(&ssl->extensions, ssl->heap);
}
/* Set the Truncated HMAC extension on the context.
*
* @param [in] ctx SSL/TLS context object.
* @return WOLFSSL_SUCCESS on success.
* @return BAD_FUNC_ARG when ctx is NULL.
* @return Negative value on error.
*/
int wolfSSL_CTX_UseTruncatedHMAC(WOLFSSL_CTX* ctx)
{
if (ctx == NULL)
return BAD_FUNC_ARG;
return TLSX_UseTruncatedHMAC(&ctx->extensions, ctx->heap);
}
#endif /* NO_WOLFSSL_CLIENT */
#endif /* HAVE_TRUNCATED_HMAC */
/* Elliptic Curves */
#if defined(HAVE_SUPPORTED_CURVES)
/* Determine whether a named group is a supported curve or FFDHE group.
*
* @param [in] name Named group identifier.
* @return 1 when the named group is valid.
* @return 0 otherwise.
*/
static int isValidCurveGroup(word16 name)
{
switch (name) {
case WOLFSSL_ECC_SECP160K1:
case WOLFSSL_ECC_SECP160R1:
case WOLFSSL_ECC_SECP160R2:
case WOLFSSL_ECC_SECP192K1:
case WOLFSSL_ECC_SECP192R1:
case WOLFSSL_ECC_SECP224K1:
case WOLFSSL_ECC_SECP224R1:
case WOLFSSL_ECC_SECP256K1:
case WOLFSSL_ECC_SECP256R1:
case WOLFSSL_ECC_SECP384R1:
case WOLFSSL_ECC_SECP521R1:
case WOLFSSL_ECC_BRAINPOOLP256R1:
case WOLFSSL_ECC_BRAINPOOLP384R1:
case WOLFSSL_ECC_BRAINPOOLP512R1:
case WOLFSSL_ECC_SM2P256V1:
case WOLFSSL_ECC_X25519:
case WOLFSSL_ECC_X448:
case WOLFSSL_ECC_BRAINPOOLP256R1TLS13:
case WOLFSSL_ECC_BRAINPOOLP384R1TLS13:
case WOLFSSL_ECC_BRAINPOOLP512R1TLS13:
case WOLFSSL_FFDHE_2048:
case WOLFSSL_FFDHE_3072:
case WOLFSSL_FFDHE_4096:
case WOLFSSL_FFDHE_6144:
case WOLFSSL_FFDHE_8192:
#ifdef WOLFSSL_HAVE_MLKEM
#ifndef WOLFSSL_NO_ML_KEM
#ifndef WOLFSSL_TLS_NO_MLKEM_STANDALONE
case WOLFSSL_ML_KEM_512:
case WOLFSSL_ML_KEM_768:
case WOLFSSL_ML_KEM_1024:
#endif /* !WOLFSSL_TLS_NO_MLKEM_STANDALONE */
#ifdef WOLFSSL_PQC_HYBRIDS
case WOLFSSL_SECP384R1MLKEM1024:
case WOLFSSL_X25519MLKEM768:
case WOLFSSL_SECP256R1MLKEM768:
#endif /* WOLFSSL_PQC_HYBRIDS */
#ifdef WOLFSSL_EXTRA_PQC_HYBRIDS
case WOLFSSL_SECP256R1MLKEM512:
case WOLFSSL_SECP384R1MLKEM768:
case WOLFSSL_SECP521R1MLKEM1024:
case WOLFSSL_X25519MLKEM512:
case WOLFSSL_X448MLKEM768:
#endif /* WOLFSSL_EXTRA_PQC_HYBRIDS */
#endif /* !WOLFSSL_NO_ML_KEM */
#ifdef WOLFSSL_MLKEM_KYBER
case WOLFSSL_KYBER_LEVEL1:
case WOLFSSL_KYBER_LEVEL3:
case WOLFSSL_KYBER_LEVEL5:
case WOLFSSL_P256_KYBER_LEVEL1:
case WOLFSSL_P384_KYBER_LEVEL3:
case WOLFSSL_P521_KYBER_LEVEL5:
case WOLFSSL_X25519_KYBER_LEVEL1:
case WOLFSSL_X448_KYBER_LEVEL3:
case WOLFSSL_X25519_KYBER_LEVEL3:
case WOLFSSL_P256_KYBER_LEVEL3:
#endif /* WOLFSSL_MLKEM_KYBER */
#endif
return 1;
default:
return 0;
}
}
/* Set a named group in the Supported Groups extension on the object.
*
* @param [in] ssl SSL/TLS object.
* @param [in] name Named group identifier.
* @return WOLFSSL_SUCCESS on success.
* @return BAD_FUNC_ARG when ssl is NULL or the group is invalid.
* @return WOLFSSL_FAILURE when TLS is not compiled in.
*/
int wolfSSL_UseSupportedCurve(WOLFSSL* ssl, word16 name)
{
if (ssl == NULL || !isValidCurveGroup(name))
return BAD_FUNC_ARG;
ssl->options.userCurves = 1;
#if defined(NO_TLS)
return WOLFSSL_FAILURE;
#else
return TLSX_UseSupportedCurve(&ssl->extensions, name, ssl->heap,
ssl->options.side);
#endif /* NO_TLS */
}
/* Set a named group in the Supported Groups extension on the context.
*
* @param [in] ctx SSL/TLS context object.
* @param [in] name Named group identifier.
* @return WOLFSSL_SUCCESS on success.
* @return BAD_FUNC_ARG when ctx is NULL or the group is invalid.
* @return WOLFSSL_FAILURE when TLS is not compiled in.
*/
int wolfSSL_CTX_UseSupportedCurve(WOLFSSL_CTX* ctx, word16 name)
{
if (ctx == NULL || !isValidCurveGroup(name))
return BAD_FUNC_ARG;
ctx->userCurves = 1;
#if defined(NO_TLS)
return WOLFSSL_FAILURE;
#else
return TLSX_UseSupportedCurve(&ctx->extensions, name, ctx->heap,
ctx->method->side);
#endif /* NO_TLS */
}
#if defined(OPENSSL_EXTRA)
/* Validate a list of group identifiers and translate them into named groups.
*
* Group values may be wolfSSL named groups or curve NIDs (when ECC is
* available).
*
* @param [in] groups Array of group identifiers.
* @param [in] count Number of groups in the array.
* @param [out] outGroups Array to hold the named groups. Must have at least
* count entries.
* @return WOLFSSL_SUCCESS on success.
* @return WOLFSSL_FAILURE when a group is not recognized.
*/
static int wolfssl_validate_groups(const int* groups, int count, int* outGroups)
{
int i;
int ret = WOLFSSL_SUCCESS;
for (i = 0; i < count; i++) {
if (isValidCurveGroup((word16)groups[i])) {
outGroups[i] = groups[i];
}
#ifdef HAVE_ECC
else {
/* Groups may be populated with curve NIDs. */
int oid = (int)nid2oid(groups[i], oidCurveType);
int name = (int)GetCurveByOID(oid);
if (name == 0) {
WOLFSSL_MSG("Invalid group name");
ret = WOLFSSL_FAILURE;
break;
}
outGroups[i] = name;
}
#else
else {
WOLFSSL_MSG("Invalid group name");
ret = WOLFSSL_FAILURE;
break;
}
#endif
}
return ret;
}
/* Set the list of supported groups on the context.
*
* Group values may be wolfSSL named groups or curve NIDs.
*
* @param [in] ctx SSL/TLS context object.
* @param [in] groups Array of group identifiers.
* @param [in] count Number of groups in the array.
* @return WOLFSSL_SUCCESS on success.
* @return WOLFSSL_FAILURE when count is invalid or a group is not recognized.
*/
int wolfSSL_CTX_set1_groups(WOLFSSL_CTX* ctx, int* groups, int count)
{
int _groups[WOLFSSL_MAX_GROUP_COUNT];
int ret = WOLFSSL_SUCCESS;
WOLFSSL_ENTER("wolfSSL_CTX_set1_groups");
if (groups == NULL || count <= 0) {
WOLFSSL_MSG("Groups NULL or count not positive");
ret = WOLFSSL_FAILURE;
}
else if (count > WOLFSSL_MAX_GROUP_COUNT) {
WOLFSSL_MSG("Group count exceeds maximum");
ret = WOLFSSL_FAILURE;
}
else {
/* Translate the input list into named groups, then apply it. */
ret = wolfssl_validate_groups(groups, count, _groups);
if (ret == WOLFSSL_SUCCESS) {
ret = wolfSSL_CTX_set_groups(ctx, _groups, count);
/* Normalize any non-success result to WOLFSSL_FAILURE. */
if (ret != WOLFSSL_SUCCESS) {
ret = WOLFSSL_FAILURE;
}
}
}
return ret;
}
/* Set the list of supported groups on the object.
*
* Group values may be wolfSSL named groups or curve NIDs.
*
* @param [in] ssl SSL/TLS object.
* @param [in] groups Array of group identifiers.
* @param [in] count Number of groups in the array.
* @return WOLFSSL_SUCCESS on success.
* @return WOLFSSL_FAILURE when count is invalid or a group is not recognized.
*/
int wolfSSL_set1_groups(WOLFSSL* ssl, int* groups, int count)
{
int _groups[WOLFSSL_MAX_GROUP_COUNT];
int ret = WOLFSSL_SUCCESS;
WOLFSSL_ENTER("wolfSSL_set1_groups");
if (groups == NULL || count <= 0) {
WOLFSSL_MSG("Groups NULL or count not positive");
ret = WOLFSSL_FAILURE;
}
else if (count > WOLFSSL_MAX_GROUP_COUNT) {
WOLFSSL_MSG("Group count exceeds maximum");
ret = WOLFSSL_FAILURE;
}
else {
/* Translate the input list into named groups, then apply it. */
ret = wolfssl_validate_groups(groups, count, _groups);
if (ret == WOLFSSL_SUCCESS) {
ret = wolfSSL_set_groups(ssl, _groups, count);
/* Normalize any non-success result to WOLFSSL_FAILURE. */
if (ret != WOLFSSL_SUCCESS) {
ret = WOLFSSL_FAILURE;
}
}
}
return ret;
}
#endif /* OPENSSL_EXTRA */
#endif /* HAVE_SUPPORTED_CURVES */
/* Application-Layer Protocol Negotiation */
#ifdef HAVE_ALPN
/* Set the Application-Layer Protocol Negotiation extension on the object.
*
* @param [in] ssl SSL/TLS object.
* @param [in] protocol_name_list Comma-separated list of protocol names.
* @param [in] protocol_name_listSz Length of the list in bytes.
* @param [in] options Bitmask of ALPN options.
* @return WOLFSSL_SUCCESS on success.
* @return BAD_FUNC_ARG when an argument is NULL, the list is too long or
* options are unsupported.
* @return MEMORY_ERROR on allocation failure.
*/
WOLFSSL_ABI
int wolfSSL_UseALPN(WOLFSSL* ssl, char *protocol_name_list,
word32 protocol_name_listSz, byte options)
{
char* list = NULL;
char* ptr = NULL;
char** token = NULL;
word16 len;
int idx = 0;
int ret = WOLFSSL_SUCCESS;
WOLFSSL_ENTER("wolfSSL_UseALPN");
if ((ssl == NULL) || (protocol_name_list == NULL)) {
return BAD_FUNC_ARG;
}
else if (protocol_name_listSz > (WOLFSSL_MAX_ALPN_NUMBER *
WOLFSSL_MAX_ALPN_PROTO_NAME_LEN + WOLFSSL_MAX_ALPN_NUMBER)) {
WOLFSSL_MSG("Invalid arguments, protocol name list too long");
return BAD_FUNC_ARG;
}
else if ((!(options & WOLFSSL_ALPN_CONTINUE_ON_MISMATCH)) &&
(!(options & WOLFSSL_ALPN_FAILED_ON_MISMATCH))) {
WOLFSSL_MSG("Invalid arguments, options not supported");
return BAD_FUNC_ARG;
}
list = (char *)XMALLOC(protocol_name_listSz + 1, ssl->heap,
DYNAMIC_TYPE_ALPN);
token = (char **)XMALLOC(sizeof(char*) * (WOLFSSL_MAX_ALPN_NUMBER + 1),
ssl->heap, DYNAMIC_TYPE_ALPN);
if ((list == NULL) || (token == NULL)) {
WOLFSSL_MSG("Memory failure");
ret = MEMORY_ERROR;
}
if (ret == WOLFSSL_SUCCESS) {
XMEMSET(token, 0, sizeof(char *) * (WOLFSSL_MAX_ALPN_NUMBER+1));
XSTRNCPY(list, protocol_name_list, protocol_name_listSz);
list[protocol_name_listSz] = '\0';
/* Read all protocol names from the list. */
token[idx] = XSTRTOK(list, ",", &ptr);
while ((idx < WOLFSSL_MAX_ALPN_NUMBER) && (token[idx] != NULL)) {
token[++idx] = XSTRTOK(NULL, ",", &ptr);
}
/* Add the protocol name list to the TLS extension in reverse order. */
while ((idx--) > 0) {
len = (word16)XSTRLEN(token[idx]);
ret = TLSX_UseALPN(&ssl->extensions, token[idx], len, options,
ssl->heap);
if (ret != WOLFSSL_SUCCESS) {
WOLFSSL_MSG("TLSX_UseALPN failure");
break;
}
}
}
XFREE(token, ssl->heap, DYNAMIC_TYPE_ALPN);
XFREE(list, ssl->heap, DYNAMIC_TYPE_ALPN);
return ret;
}
/* Get the ALPN protocol negotiated for the object.
*
* @param [in] ssl SSL/TLS object.
* @param [out] protocol_name Negotiated protocol name.
* @param [out] size Length of the protocol name in bytes.
* @return WOLFSSL_SUCCESS on success.
* @return Negative value on error.
*/
int wolfSSL_ALPN_GetProtocol(WOLFSSL* ssl, char **protocol_name, word16 *size)
{
return TLSX_ALPN_GetRequest((ssl != NULL) ? ssl->extensions : NULL,
(void **)protocol_name, size);
}
/* Get the ALPN protocol list offered by the peer as a comma-separated string.
*
* The returned list must be freed with wolfSSL_ALPN_FreePeerProtocol().
*
* @param [in] ssl SSL/TLS object.
* @param [out] list Newly allocated comma-separated protocol list.
* @param [out] listSz Length of the list string.
* @return WOLFSSL_SUCCESS on success.
* @return BAD_FUNC_ARG when an argument is NULL.
* @return BUFFER_ERROR when the peer offered no protocols.
* @return MEMORY_ERROR on allocation failure.
*/
int wolfSSL_ALPN_GetPeerProtocol(WOLFSSL* ssl, char **list, word16 *listSz)
{
int i, len;
char *p;
byte *s;
if (ssl == NULL || list == NULL || listSz == NULL)
return BAD_FUNC_ARG;
if (ssl->alpn_peer_requested == NULL
|| ssl->alpn_peer_requested_length == 0)
return BUFFER_ERROR;
/* ssl->alpn_peer_requested are the original bytes sent in a ClientHello,
* formatted as (len-byte chars+)+. To turn n protocols into a
* comma-separated C string, one needs (n-1) commas and a final 0 byte
* which has the same length as the original.
* The returned length is the strlen() of the C string, so -1 of that. */
*listSz = ssl->alpn_peer_requested_length-1;
*list = p = (char *)XMALLOC(ssl->alpn_peer_requested_length, ssl->heap,
DYNAMIC_TYPE_TLSX);
if (p == NULL)
return MEMORY_ERROR;
for (i = 0, s = ssl->alpn_peer_requested;
i < ssl->alpn_peer_requested_length;
p += len, i += len)
{
if (i)
*p++ = ',';
len = s[i++];
/* guard against bad length bytes. */
if (i + len > ssl->alpn_peer_requested_length) {
XFREE(*list, ssl->heap, DYNAMIC_TYPE_TLSX);
*list = NULL;
return WOLFSSL_FAILURE;
}
XMEMCPY(p, s + i, (size_t)len);
}
*p = 0;
return WOLFSSL_SUCCESS;
}
/* Free a peer protocol list returned by wolfSSL_ALPN_GetPeerProtocol().
*
* @param [in] ssl SSL/TLS object.
* @param [in, out] list Protocol list to free; set to NULL on return.
* @return WOLFSSL_SUCCESS on success.
* @return BAD_FUNC_ARG when ssl is NULL.
*/
int wolfSSL_ALPN_FreePeerProtocol(WOLFSSL* ssl, char **list)
{
if (ssl == NULL) {
return BAD_FUNC_ARG;
}
XFREE(*list, ssl->heap, DYNAMIC_TYPE_TLSX);
*list = NULL;
return WOLFSSL_SUCCESS;
}
#endif /* HAVE_ALPN */
/* Secure Renegotiation */
#ifdef HAVE_SERVER_RENEGOTIATION_INFO
/* Enable the Secure Renegotiation extension on the object.
*
* Use of secure renegotiation is discouraged.
*
* @param [in] ssl SSL/TLS object.
* @return WOLFSSL_SUCCESS on success.
* @return BAD_FUNC_ARG when ssl is NULL.
* @return Negative value on error.
*/
int wolfSSL_UseSecureRenegotiation(WOLFSSL* ssl)
{
int ret = WC_NO_ERR_TRACE(BAD_FUNC_ARG);
#if defined(NO_TLS)
(void)ssl;
#else
if (ssl != NULL) {
ret = TLSX_UseSecureRenegotiation(&ssl->extensions, ssl->heap);
}
else {
ret = BAD_FUNC_ARG;
}
if (ret == WOLFSSL_SUCCESS) {
TLSX* extension = TLSX_Find(ssl->extensions, TLSX_RENEGOTIATION_INFO);
if (extension != NULL) {
ssl->secure_renegotiation = (SecureRenegotiation*)extension->data;
}
}
#endif /* !NO_TLS */
return ret;
}
/* Enable the Secure Renegotiation extension on the context.
*
* Use of secure renegotiation is discouraged.
*
* @param [in] ctx SSL/TLS context object.
* @return WOLFSSL_SUCCESS on success.
* @return BAD_FUNC_ARG when ctx is NULL.
*/
int wolfSSL_CTX_UseSecureRenegotiation(WOLFSSL_CTX* ctx)
{
if (ctx == NULL)
return BAD_FUNC_ARG;
ctx->useSecureReneg = 1;
return WOLFSSL_SUCCESS;
}
#ifdef HAVE_SECURE_RENEGOTIATION
/* Perform a secure renegotiation handshake on the object.
*
* User forced; use of secure renegotiation is discouraged.
*
* @param [in] ssl SSL/TLS object.
* @return WOLFSSL_SUCCESS on success.
* @return BAD_FUNC_ARG when ssl is NULL.
* @return SECURE_RENEGOTIATION_E when renegotiation is not allowed.
* @return WOLFSSL_FATAL_ERROR on error.
*/
static int _Rehandshake(WOLFSSL* ssl)
{
int ret;
if (ssl == NULL)
return BAD_FUNC_ARG;
if (IsAtLeastTLSv1_3(ssl->version)) {
WOLFSSL_MSG("Secure Renegotiation not supported in TLS 1.3");
return SECURE_RENEGOTIATION_E;
}
if (ssl->secure_renegotiation == NULL) {
WOLFSSL_MSG("Secure Renegotiation not forced on by user");
return SECURE_RENEGOTIATION_E;
}
if (ssl->secure_renegotiation->enabled == 0) {
WOLFSSL_MSG("Secure Renegotiation not enabled at extension level");
return SECURE_RENEGOTIATION_E;
}
if (ssl->secure_renegotiation->advertiseOnly) {
/* Extension was advertised only for the RFC 5746 check; the
* application did not call wolfSSL_UseSecureRenegotiation(). */
WOLFSSL_MSG("Secure Renegotiation not forced on by user");
return SECURE_RENEGOTIATION_E;
}
#ifdef WOLFSSL_DTLS
if (ssl->options.dtls && ssl->keys.dtls_epoch == 0xFFFF) {
WOLFSSL_MSG("Secure Renegotiation not allowed. Epoch would wrap");
return SECURE_RENEGOTIATION_E;
}
#endif
/* If the client started the renegotiation, the server will already
* have processed the client's hello. */
if (ssl->options.side != WOLFSSL_SERVER_END ||
ssl->options.acceptState != ACCEPT_FIRST_REPLY_DONE) {
if (ssl->options.handShakeState != HANDSHAKE_DONE) {
if (!ssl->options.handShakeDone) {
WOLFSSL_MSG("Can't renegotiate until initial "
"handshake complete");
return SECURE_RENEGOTIATION_E;
}
else {
WOLFSSL_MSG("Renegotiation already started. "
"Moving it forward.");
ret = wolfSSL_negotiate(ssl);
if (ret == WOLFSSL_SUCCESS)
ssl->secure_rene_count++;
return ret;
}
}
/* reset handshake states */
ssl->options.sendVerify = 0;
ssl->options.serverState = NULL_STATE;
ssl->options.clientState = NULL_STATE;
ssl->options.connectState = CONNECT_BEGIN;
ssl->options.acceptState = ACCEPT_BEGIN_RENEG;
ssl->options.handShakeState = NULL_STATE;
ssl->options.processReply = 0; /* TODO, move states in internal.h */
XMEMSET(&ssl->msgsReceived, 0, sizeof(ssl->msgsReceived));
ssl->secure_renegotiation->cache_status = SCR_CACHE_NEEDED;
#if !defined(NO_WOLFSSL_SERVER) && !defined(WOLFSSL_NO_TLS12)
if (ssl->options.side == WOLFSSL_SERVER_END) {
ret = SendHelloRequest(ssl);
if (ret != 0) {
ssl->error = ret;
return WOLFSSL_FATAL_ERROR;
}
}
#endif /* !NO_WOLFSSL_SERVER && !WOLFSSL_NO_TLS12 */
ret = InitHandshakeHashes(ssl);
if (ret != 0) {
ssl->error = ret;
return WOLFSSL_FATAL_ERROR;
}
}
ret = wolfSSL_negotiate(ssl);
if (ret == WOLFSSL_SUCCESS)
ssl->secure_rene_count++;
return ret;
}
/* Perform a secure renegotiation handshake on the object.
*
* User forced; use of secure renegotiation is discouraged.
*
* @param [in] ssl SSL/TLS object.
* @return WOLFSSL_SUCCESS on success.
* @return WOLFSSL_FAILURE when ssl is NULL.
* @return Negative value on error.
*/
int wolfSSL_Rehandshake(WOLFSSL* ssl)
{
int ret;
WOLFSSL_ENTER("wolfSSL_Rehandshake");
if (ssl == NULL)
return WOLFSSL_FAILURE;
#ifdef HAVE_SESSION_TICKET
ret = WOLFSSL_SUCCESS;
#endif
if (ssl->options.side == WOLFSSL_SERVER_END) {
/* Reset option to send certificate verify. */
ssl->options.sendVerify = 0;
/* Reset resuming flag to do full secure handshake. */
ssl->options.resuming = 0;
}
else {
/* Reset resuming flag to do full secure handshake. */
ssl->options.resuming = 0;
#if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_CLIENT)
/* Clearing the ticket. */
ret = wolfSSL_UseSessionTicket(ssl);
#endif
}
/* CLIENT/SERVER: Reset peer authentication for full secure handshake. */
ssl->options.peerAuthGood = 0;
#ifdef HAVE_SESSION_TICKET
if (ret == WOLFSSL_SUCCESS)
#endif
ret = _Rehandshake(ssl);
return ret;
}
#ifndef NO_WOLFSSL_CLIENT
/* Perform a secure resumption handshake on the object.
*
* Client side only. User forced; use of secure renegotiation is discouraged.
*
* @param [in] ssl SSL/TLS object.
* @return WOLFSSL_SUCCESS on success.
* @return BAD_FUNC_ARG when ssl is NULL.
* @return WOLFSSL_FATAL_ERROR when called on a server.
*/
int wolfSSL_SecureResume(WOLFSSL* ssl)
{
WOLFSSL_ENTER("wolfSSL_SecureResume");
if (ssl == NULL)
return BAD_FUNC_ARG;
if (ssl->options.side == WOLFSSL_SERVER_END) {
ssl->error = SIDE_ERROR;
return WOLFSSL_FATAL_ERROR;
}
return _Rehandshake(ssl);
}
#endif /* NO_WOLFSSL_CLIENT */
#endif /* HAVE_SECURE_RENEGOTIATION */
/* Get whether secure renegotiation is enabled for the object.
*
* @param [in] ssl SSL/TLS object.
* @return 1 when secure renegotiation is enabled.
* @return 0 when ssl is NULL or it is not enabled.
*/
long wolfSSL_SSL_get_secure_renegotiation_support(WOLFSSL* ssl)
{
WOLFSSL_ENTER("wolfSSL_SSL_get_secure_renegotiation_support");
return (ssl != NULL) && (ssl->secure_renegotiation != NULL) &&
ssl->secure_renegotiation->enabled;
}
#endif /* HAVE_SECURE_RENEGOTIATION_INFO */
#if !defined(NO_WOLFSSL_CLIENT) && !defined(WOLFSSL_NO_TLS12) && \
defined(HAVE_SERVER_RENEGOTIATION_INFO) && \
!defined(WOLFSSL_HARDEN_TLS_NO_SCR_CHECK)
/* Get whether the secure renegotiation check is enabled for the object.
*
* @param [in] ssl SSL/TLS object.
* @return Non-zero when the check is enabled, 0 otherwise.
* @return BAD_FUNC_ARG when ssl is NULL.
*/
WOLFSSL_API int wolfSSL_get_scr_check_enabled(const WOLFSSL* ssl)
{
WOLFSSL_ENTER("wolfSSL_get_scr_check_enabled");
if (ssl == NULL)
return BAD_FUNC_ARG;
return ssl->scr_check_enabled;
}
/* Set whether the secure renegotiation check is enabled for the object.
*
* @param [in] ssl SSL/TLS object.
* @param [in] enabled Non-zero to enable the check, 0 to disable it.
* @return WOLFSSL_SUCCESS on success.
* @return BAD_FUNC_ARG when ssl is NULL.
*/
WOLFSSL_API int wolfSSL_set_scr_check_enabled(WOLFSSL* ssl, byte enabled)
{
WOLFSSL_ENTER("wolfSSL_set_scr_check_enabled");