-
Notifications
You must be signed in to change notification settings - Fork 975
Expand file tree
/
Copy pathssl_api_cert.c
More file actions
1769 lines (1561 loc) · 49.8 KB
/
ssl_api_cert.c
File metadata and controls
1769 lines (1561 loc) · 49.8 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_cert.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_CERT_INCLUDED)
#ifndef WOLFSSL_IGNORE_FILE_WARN
#warning ssl_api_cert.c is not compiled separately from ssl.c
#endif
#else
#ifndef NO_CERTS
/* Set whether mutual authentication is required for connections.
* Server side only.
*
* @param [in] ctx The SSL/TLS CTX object.
* @param [in] req 1 to indicate required and 0 when not.
* @return 0 on success.
* @return BAD_FUNC_ARG when ctx is NULL.
* @return SIDE_ERROR when not a server.
*/
int wolfSSL_CTX_mutual_auth(WOLFSSL_CTX* ctx, int req)
{
if (ctx == NULL)
return BAD_FUNC_ARG;
if (ctx->method->side != WOLFSSL_SERVER_END)
return SIDE_ERROR;
ctx->mutualAuth = (byte)req;
return 0;
}
/* Set whether mutual authentication is required for the connection.
* Server side only.
*
* @param [in] ssl The SSL/TLS object.
* @param [in] req 1 to indicate required and 0 when not.
* @return 0 on success.
* @return BAD_FUNC_ARG when ssl is NULL.
* @return SIDE_ERROR when not a server
*/
int wolfSSL_mutual_auth(WOLFSSL* ssl, int req)
{
if (ssl == NULL)
return BAD_FUNC_ARG;
if (ssl->options.side != WOLFSSL_SERVER_END)
return SIDE_ERROR;
ssl->options.mutualAuth = (word16)req;
return 0;
}
/* Get the certificate manager from the WOLFSSL_CTX.
*
* @param [in] ctx SSL/TLS CTX object.
* @return Certificate manager object on success.
* @return NULL when ctx is NULL.
*/
WOLFSSL_CERT_MANAGER* wolfSSL_CTX_GetCertManager(WOLFSSL_CTX* ctx)
{
WOLFSSL_CERT_MANAGER* cm = NULL;
if (ctx)
cm = ctx->cm;
return cm;
}
/* Sets the max chain depth when verifying a certificate chain.
*
* Default depth is set to MAX_CHAIN_DEPTH.
*
* @param [in] ctx WOLFSSL_CTX structure to set depth in
* @param [in] depth max depth
*/
void wolfSSL_CTX_set_verify_depth(WOLFSSL_CTX *ctx, int depth)
{
WOLFSSL_ENTER("wolfSSL_CTX_set_verify_depth");
if ((ctx == NULL) || (depth < 0) || (depth > MAX_CHAIN_DEPTH)) {
WOLFSSL_MSG("Bad depth argument, too large or less than 0");
}
else {
ctx->verifyDepth = (byte)depth;
}
}
/* Get certificate chaining depth of SSL/TLS context object
*
* @param [in] ctx SSL/TLS context object.
* @return Verification depth on success.
* @return BAD_FUNC_ARG when ctx is NULL.
*/
long wolfSSL_CTX_get_verify_depth(WOLFSSL_CTX* ctx)
{
long ret;
if (ctx == NULL) {
ret = BAD_FUNC_ARG;
}
else {
#ifndef OPENSSL_EXTRA
ret = MAX_CHAIN_DEPTH;
#else
ret = ctx->verifyDepth;
#endif
}
return ret;
}
/* Get certificate chaining depth of SSL/TLS object
*
* @param [in] ssl SSL/TLS object.
* @return Verification depth on success.
* @return BAD_FUNC_ARG when ssl is NULL.
*/
long wolfSSL_get_verify_depth(WOLFSSL* ssl)
{
long ret;
if (ssl == NULL) {
ret = BAD_FUNC_ARG;
}
else {
#ifndef OPENSSL_EXTRA
ret = MAX_CHAIN_DEPTH;
#else
ret = ssl->options.verifyDepth;
#endif
}
return ret;
}
#if defined(HAVE_RPK)
/* TODO: Change this to use a bitfield. */
/* Confirm that all the byte data in the buffer is unique.
*
* @param [in] buf Buffer to check.
* @param [in] len Length of buffer in bytes.
* @return 1 if all the byte data in the buffer is unique.
* @return 0 otherwise.
*/
static int isArrayUnique(const char* buf, size_t len)
{
size_t i;
/* check the array is unique */
for (i = 0; i < len - 1; ++i) {
size_t j;
for (j = i + 1; j < len; ++j) {
if (buf[i] == buf[j]) {
return 0;
}
}
}
return 1;
}
/* Set user preference for the {client,server}_cert_type extension.
*
* Takes byte array containing cert types the caller can provide to its peer.
* Cert types are in preferred order in the array.
*
* @param [in] cfg Raw Public Key configuration.
* @param [in] client Indicates whether this is the client side.
* @param [in] buf List of certificate types.
* @param [in] len Length of certificate types.
* @return 1 on success.
* @return BAD_FUNC_ARG when cfg is NULL.
* @return BAD_FUNC_ARG when len is too long.
* @return BAD_FUNC_ARG when buffer values are not unique.
* @return BAD_FUNC_ARG when buffer contains unrecognized certificate type.
*/
static int set_cert_type(RpkConfig* cfg, int client, const char* buf,
int len)
{
int i;
byte* certTypeCnt;
byte* certTypes;
/* Validate parameters. */
if ((cfg == NULL) || (len > (client ? MAX_CLIENT_CERT_TYPE_CNT :
MAX_SERVER_CERT_TYPE_CNT))) {
return BAD_FUNC_ARG;
}
/* Get preferred certificate types for side. */
if (client) {
certTypeCnt = &cfg->preferred_ClientCertTypeCnt;
certTypes = cfg->preferred_ClientCertTypes;
}
else {
certTypeCnt = &cfg->preferred_ServerCertTypeCnt;
certTypes = cfg->preferred_ServerCertTypes;
}
/* If no buffer or empty buffer passed in, set the defaults. */
if ((buf == NULL) || (len == 0)) {
*certTypeCnt = 1;
for (i = 0; i < 2; i++) {
certTypes[i] = WOLFSSL_CERT_TYPE_X509;
}
return 1;
}
/* Check that the certificate types set are unique. */
if (!isArrayUnique(buf, (size_t)len))
return BAD_FUNC_ARG;
/* Check that the certificate types being set are known and then set. */
for (i = 0; i < len; i++) {
if ((buf[i] != WOLFSSL_CERT_TYPE_RPK) &&
(buf[i] != WOLFSSL_CERT_TYPE_X509)) {
return BAD_FUNC_ARG;
}
certTypes[i] = (byte)buf[i];
}
*certTypeCnt = len;
return 1;
}
/* Set the client certificate types against the SSL/TLS context.
*
* @param [in] ctx SSL/TLS context object.
* @param [in] buf List of certificate types.
* @param [in] len Length of certificate types.
* @return 1 on success.
* @return BAD_FUNC_ARG when ctx is NULL.
* @return BAD_FUNC_ARG when len is too long.
* @return BAD_FUNC_ARG when buffer values are not unique.
* @return BAD_FUNC_ARG when buffer contains unrecognized certificate type.
*/
int wolfSSL_CTX_set_client_cert_type(WOLFSSL_CTX* ctx, const char* buf, int len)
{
int ret;
if (ctx == NULL) {
ret = BAD_FUNC_ARG;
}
else {
ret = set_cert_type(&ctx->rpkConfig, 1, buf, len);
}
return ret;
}
/* Set the server certificate types against the SSL/TLS context.
*
* @param [in] ctx SSL/TLS context object.
* @param [in] buf List of certificate types.
* @param [in] len Length of certificate types.
* @return 1 on success.
* @return BAD_FUNC_ARG when ctx is NULL.
* @return BAD_FUNC_ARG when len is too long.
* @return BAD_FUNC_ARG when buffer values are not unique.
* @return BAD_FUNC_ARG when buffer contains unrecognized certificate type.
*/
int wolfSSL_CTX_set_server_cert_type(WOLFSSL_CTX* ctx, const char* buf, int len)
{
int ret;
if (ctx == NULL) {
ret = BAD_FUNC_ARG;
}
else {
ret = set_cert_type(&ctx->rpkConfig, 0, buf, len);
}
return ret;
}
/* Set the client certificate types against the SSL/TLS object.
*
* @param [in] ssl SSL/TLS object.
* @param [in] buf List of certificate types.
* @param [in] len Length of certificate types.
* @return 1 on success.
* @return BAD_FUNC_ARG when ssl is NULL.
* @return BAD_FUNC_ARG when len is too long.
* @return BAD_FUNC_ARG when buffer values are not unique.
* @return BAD_FUNC_ARG when buffer contains unrecognized certificate type.
*/
int wolfSSL_set_client_cert_type(WOLFSSL* ssl, const char* buf, int len)
{
int ret;
if (ssl == NULL) {
ret = BAD_FUNC_ARG;
}
else {
ret = set_cert_type(&ssl->options.rpkConfig, 1, buf, len);
}
return ret;
}
/* Set the server certificate types against the SSL/TLS object.
*
* @param [in] ssl SSL/TLS object.
* @param [in] buf List of certificate types.
* @param [in] len Length of certificate types.
* @return 1 on success.
* @return BAD_FUNC_ARG when ssl is NULL.
* @return BAD_FUNC_ARG when len is too long.
* @return BAD_FUNC_ARG when buffer values are not unique.
* @return BAD_FUNC_ARG when buffer contains unrecognized certificate type.
*/
int wolfSSL_set_server_cert_type(WOLFSSL* ssl, const char* buf, int len)
{
int ret;
if (ssl == NULL) {
ret = BAD_FUNC_ARG;
}
else {
ret = set_cert_type(&ssl->options.rpkConfig, 0, buf, len);
}
return ret;
}
/* Get negotiated client certificate type value.
*
* WOLFSSL_CERT_TYPE_UNKNOWN returned when no negotiation has been performed.
*
* @param [in] ssl SSL/TLS object.
* @param [out] tp Certificate type. One of:
* -1: WOLFSSL_CERT_TYPE_UNKNOWN
* 0: WOLFSSL_CERT_TYPE_X509
* 2: WOLFSSL_CERT_TYPE_RPK
* @return 1 on success.
* @return BAD_FUNC_ARG when ssl or tp is NULL.
*/
int wolfSSL_get_negotiated_client_cert_type(WOLFSSL* ssl, int* tp)
{
int ret = 1;
/* Validate parameters. */
if ((ssl == NULL) || (tp == NULL)) {
ret = BAD_FUNC_ARG;
}
/* Check side. */
else if (ssl->options.side == WOLFSSL_CLIENT_END) {
/* Check certificate type negotiated. */
if (ssl->options.rpkState.received_ClientCertTypeCnt == 1) {
*tp = ssl->options.rpkState.received_ClientCertTypes[0];
}
else {
*tp = WOLFSSL_CERT_TYPE_UNKNOWN;
}
}
/* Check certificate type negotiated. */
else if (ssl->options.rpkState.sending_ClientCertTypeCnt == 1) {
*tp = ssl->options.rpkState.sending_ClientCertTypes[0];
}
else {
*tp = WOLFSSL_CERT_TYPE_UNKNOWN;
}
return ret;
}
/* Get negotiated server certificate type value.
*
* WOLFSSL_CERT_TYPE_UNKNOWN returned when no negotiation has been performed.
*
* @param [in] ssl SSL/TLS object.
* @param [out] tp Certificate type. One of:
* -1: WOLFSSL_CERT_TYPE_UNKNOWN
* 0: WOLFSSL_CERT_TYPE_X509
* 2: WOLFSSL_CERT_TYPE_RPK
* @return 1 on success.
* @return BAD_FUNC_ARG when ssl or tp is NULL.
*/
int wolfSSL_get_negotiated_server_cert_type(WOLFSSL* ssl, int* tp)
{
int ret = 1;
/* Validate parameters. */
if ((ssl == NULL) || (tp == NULL)) {
ret = BAD_FUNC_ARG;
}
/* Check side. */
else if (ssl->options.side == WOLFSSL_CLIENT_END) {
/* Check certificate type negotiated. */
if (ssl->options.rpkState.received_ServerCertTypeCnt == 1) {
*tp = ssl->options.rpkState.received_ServerCertTypes[0];
}
else {
*tp = WOLFSSL_CERT_TYPE_UNKNOWN;
}
}
/* Check certificate type negotiated. */
else if (ssl->options.rpkState.sending_ServerCertTypeCnt == 1) {
*tp = ssl->options.rpkState.sending_ServerCertTypes[0];
}
else {
*tp = WOLFSSL_CERT_TYPE_UNKNOWN;
}
return ret;
}
#endif /* HAVE_RPK */
/* Certificate verification options. */
typedef struct {
/* Verify the peer certificate. */
byte verifyPeer:1;
/* No peer certificate verification. */
byte verifyNone:1;
/* Fail when no peer certificate seen. */
byte failNoCert:1;
/* Fail when no peer certificate except when PSK handshake performed. */
byte failNoCertxPSK:1;
#if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
/* Verify peer certificate post handshake. */
byte verifyPostHandshake:1;
#endif
} SetVerifyOptions;
/* Convert the mode flags into certificate verification options.
*
* @param [in] mode Certificate verification mode flags.
* @return Certificate verification options.
*/
static SetVerifyOptions ModeToVerifyOptions(int mode)
{
SetVerifyOptions opts;
/* Set the options to the default - none set. */
XMEMSET(&opts, 0, sizeof(SetVerifyOptions));
/* When the mode is not default - set the options. */
if (mode != WOLFSSL_VERIFY_DEFAULT) {
opts.verifyNone = (mode == WOLFSSL_VERIFY_NONE);
/* When not no verification, set the chosen options. */
if (!opts.verifyNone) {
opts.verifyPeer =
(mode & WOLFSSL_VERIFY_PEER) != 0;
opts.failNoCertxPSK =
(mode & WOLFSSL_VERIFY_FAIL_EXCEPT_PSK) != 0;
opts.failNoCert =
(mode & WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT) != 0;
#if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
opts.verifyPostHandshake =
(mode & WOLFSSL_VERIFY_POST_HANDSHAKE) != 0;
#endif
}
}
return opts;
}
/* Set the verification options against the SSL/TLS context.
*
* @param [in] ctx SSL/TLS context object.
* @param [in] mode Verification mode options.
* @param [in] verify_callback Verification callback.
*/
WOLFSSL_ABI void wolfSSL_CTX_set_verify(WOLFSSL_CTX* ctx, int mode,
VerifyCallback verify_callback)
{
WOLFSSL_ENTER("wolfSSL_CTX_set_verify");
/* Ensure we have an SSL/TLS context to work with. */
if (ctx != NULL) {
SetVerifyOptions opts = ModeToVerifyOptions(mode);
/* Set the bitfield options. */
ctx->verifyNone = opts.verifyNone;
ctx->verifyPeer = opts.verifyPeer;
ctx->failNoCert = opts.failNoCert;
ctx->failNoCertxPSK = opts.failNoCertxPSK;
#if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
ctx->verifyPostHandshake = opts.verifyPostHandshake;
#endif
/* Store the user verification callback against the context. */
ctx->verifyCallback = verify_callback;
}
}
#ifdef OPENSSL_ALL
/* Set certificate verification callback and context against SSL/TLS context.
*
* @param [in] ctx SSL/TLS context object.
* @param [in] cb Certificate verification callback.
* @param [in] arg Context for certification verification callback.
*/
void wolfSSL_CTX_set_cert_verify_callback(WOLFSSL_CTX* ctx,
CertVerifyCallback cb, void* arg)
{
WOLFSSL_ENTER("wolfSSL_CTX_set_cert_verify_callback");
/* Ensure we have an SSL/TLS context to work with. */
if (ctx != NULL) {
ctx->verifyCertCb = cb;
ctx->verifyCertCbArg = arg;
}
}
#endif
/* Set the verification options against the SSL/TLS object.
*
* @param [in] ssl SSL/TLS object.
* @param [in] mode Verification mode options.
* @param [in] verify_callback Verification callback.
*/
void wolfSSL_set_verify(WOLFSSL* ssl, int mode, VerifyCallback verify_callback)
{
WOLFSSL_ENTER("wolfSSL_set_verify");
/* Ensure we have an SSL/TLS object to work with. */
if (ssl != NULL) {
SetVerifyOptions opts = ModeToVerifyOptions(mode);
/* Set the bitfield options. */
ssl->options.verifyNone = opts.verifyNone;
ssl->options.verifyPeer = opts.verifyPeer;
ssl->options.failNoCert = opts.failNoCert;
ssl->options.failNoCertxPSK = opts.failNoCertxPSK;
#if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
ssl->options.verifyPostHandshake = opts.verifyPostHandshake;
#endif
/* Store the user verification callback against the object. */
ssl->verifyCallback = verify_callback;
}
}
/* Set the certificate verification result for the SSL/TLS object.
*
* @param [in] ssl SSL/TLS object.
* @param [in] v Verification result.
*/
void wolfSSL_set_verify_result(WOLFSSL *ssl, long v)
{
WOLFSSL_ENTER("wolfSSL_set_verify_result");
/* Ensure we have an SSL/TLS object to work with. */
if (ssl != NULL) {
#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
ssl->peerVerifyRet = (unsigned long)v;
#else
WOLFSSL_STUB("wolfSSL_set_verify_result");
(void)v;
#endif
}
}
/* Store user ctx for verify callback into SSL/TLS context.
*
* @param [in] ctx SSL/TLS context.
* @param [in] userCtx User context for verify callback.
*/
void wolfSSL_CTX_SetCertCbCtx(WOLFSSL_CTX* ctx, void* userCtx)
{
WOLFSSL_ENTER("wolfSSL_CTX_SetCertCbCtx");
/* Validate parameters. */
if (ctx != NULL) {
ctx->verifyCbCtx = userCtx;
}
}
/* Store user ctx for verify callback into SSL/TLS object.
*
* @param [in] ssl SSL/TLS object.
* @param [in] ctx User context for verify callback.
*/
void wolfSSL_SetCertCbCtx(WOLFSSL* ssl, void* ctx)
{
WOLFSSL_ENTER("wolfSSL_SetCertCbCtx");
/* Validate parameters. */
if (ssl != NULL) {
ssl->verifyCbCtx = ctx;
}
}
/* Store context CA Cache addition callback into SSL/TLS context.
*
* @param [in] ctx SSL/TLS context.
* @param [in] userCtx User context for verify callback.
*/
void wolfSSL_CTX_SetCACb(WOLFSSL_CTX* ctx, CallbackCACache cb)
{
/* Validate parameters. */
if ((ctx != NULL) && (ctx->cm != NULL)) {
ctx->cm->caCacheCallback = cb;
}
}
#if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && \
defined(WOLFSSL_POST_HANDSHAKE_AUTH)
/* For TLS v1.3, send authentication messages after handshake completes.
*
* @return 1 on success.
* @return UNSUPPORTED_PROTO_VERSION when not a TLSv1.3 handshake.
* @return 0 on other failure.
*/
int wolfSSL_verify_client_post_handshake(WOLFSSL* ssl)
{
int ret;
/* Do request of certificate. */
ret = wolfSSL_request_certificate(ssl);
if (ret != 1) {
/* Special logging for wrong protocol version. */
if ((ssl != NULL) && !IsAtLeastTLSv1_3(ssl->version)) {
WOLFSSL_ERROR(UNSUPPORTED_PROTO_VERSION);
}
else {
/* Other errors - return 0. */
WOLFSSL_ERROR(ret);
}
ret = 0;
}
return ret;
}
/* Set whether handshakes from this SSL/TLS context allow auth post handshake.
*
* @param [in] ctx SSL/TLS context.
* @param [in] val Whether to allow post handshake authentication.
* @return 1 on success.
* @return 0 on failure.
*/
int wolfSSL_CTX_set_post_handshake_auth(WOLFSSL_CTX* ctx, int val)
{
int ret;
/* Try to allow - really just checking conditions. */
if (wolfSSL_CTX_allow_post_handshake_auth(ctx) == 0) {
/* Set value as a bit. */
ctx->postHandshakeAuth = (val != 0);
ret = 1;
}
else {
ret = 0;
}
return ret;
}
/* Set whether handshakes with this SSL/TLS object allow auth post handshake.
*
* @param [in] ctx SSL/TLS context.
* @param [in] val Whether to allow post handshake authentication.
* @return 1 on success.
* @return 0 on failure.
*/
int wolfSSL_set_post_handshake_auth(WOLFSSL* ssl, int val)
{
int ret;
/* Try to allow - really just checking conditions. */
if (wolfSSL_allow_post_handshake_auth(ssl) == 0) {
/* Set value as a bit. */
ssl->options.postHandshakeAuth = (val != 0);
ret = 1;
}
else {
ret = 0;
}
return ret;
}
#endif /* OPENSSL_EXTRA && WOLFSSL_TLS13 && WOLFSSL_POST_HANDSHAKE_AUTH */
#if defined(PERSIST_CERT_CACHE)
#if !defined(NO_FILESYSTEM)
/* Persist certificate cache in SSL/TLS context to file.
*
* @param [in] ctx SSL/TLS context.
* @param [in] fname Filename so store certificate cache to.
* @return 1 on success.
* @return BAD_FUNC_ARG when ctx or fname is NULL.
* @return Other values on failure.
*/
int wolfSSL_CTX_save_cert_cache(WOLFSSL_CTX* ctx, const char* fname)
{
int ret;
WOLFSSL_ENTER("wolfSSL_CTX_save_cert_cache");
/* Validate parameters. */
if ((ctx == NULL) || (fname == NULL)) {
ret = BAD_FUNC_ARG;
}
else {
/* Save certificate cache. */
ret = CM_SaveCertCache(ctx->cm, fname);
}
return ret;
}
/* Load certificate cache into SSL/TLS context from file.
*
* @param [in] ctx SSL/TLS context.
* @param [in] fname Filename so store certificate cache to.
* @return 1 on success.
* @return BAD_FUNC_ARG when ctx or fname is NULL.
* @return Other values on failure.
*/
int wolfSSL_CTX_restore_cert_cache(WOLFSSL_CTX* ctx, const char* fname)
{
int ret;
WOLFSSL_ENTER("wolfSSL_CTX_restore_cert_cache");
/* Validate parameters. */
if ((ctx == NULL) || (fname == NULL)) {
ret = BAD_FUNC_ARG;
}
else {
/* Restore certificate cache. */
ret = CM_RestoreCertCache(ctx->cm, fname);
}
return ret;
}
#endif /* NO_FILESYSTEM */
/* Persist certificate cache in SSL/TLS context to memory.
*
* @param [in] ctx SSL/TLS context.
* @param [in] mem Memory to fill with certificate cache.
* @param [in] sz Size of memory to fill in bytes.
* @param [out] used The number of bytes of memory used.
* @return 1 on success.
* @return BAD_FUNC_ARG when ctx, mem or used is NULL.
* @return BAD_FUNC_ARG when sz is less than or equal to zero.
* @return Other values on failure.
*/
int wolfSSL_CTX_memsave_cert_cache(WOLFSSL_CTX* ctx, void* mem,
int sz, int* used)
{
int ret;
WOLFSSL_ENTER("wolfSSL_CTX_memsave_cert_cache");
/* Validate parameters. */
if ((ctx == NULL) || (mem == NULL) || (used == NULL) || (sz <= 0)) {
ret = BAD_FUNC_ARG;
}
else {
/* Persist certificate change to memory. */
ret = CM_MemSaveCertCache(ctx->cm, mem, sz, used);
}
return ret;
}
/* Load certificate cache into SSL/TLS context from memory.
*
* @param [in] ctx SSL/TLS context.
* @param [in] mem Memory with certificate cache.
* @param [in] sz Size of certificate cache in bytes
* @return 1 on success.
* @return BAD_FUNC_ARG when ctx or mem is NULL.
* @return BAD_FUNC_ARG when sz is less than or equal to zero.
* @return Other values on failure.
*/
int wolfSSL_CTX_memrestore_cert_cache(WOLFSSL_CTX* ctx, const void* mem, int sz)
{
int ret;
WOLFSSL_ENTER("wolfSSL_CTX_memrestore_cert_cache");
/* Validate parameters. */
if ((ctx == NULL) || (mem == NULL) || (sz <= 0)) {
ret = BAD_FUNC_ARG;
}
else {
/* Restore certificate cache. */
ret = CM_MemRestoreCertCache(ctx->cm, mem, sz);
}
return ret;
}
/* Get size of certificate cache when persisted.
*
* @param [in] ctx SSL/TLS context.
* @return Size of certificate cache when pesisted in bytes.
* @return BAD_FUNC_ARG when ctx is NULL.
*/
int wolfSSL_CTX_get_cert_cache_memsize(WOLFSSL_CTX* ctx)
{
int ret;
WOLFSSL_ENTER("wolfSSL_CTX_get_cert_cache_memsize");
/* Validate parameter. */
if (ctx == NULL) {
ret = BAD_FUNC_ARG;
}
else {
/* Get size. */
ret = CM_GetCertCacheMemSize(ctx->cm);
}
return ret;
}
#endif /* PERSIST_CERT_CACHE */
/* Unload certificates and keys that the SSL/TLS object owns.
*
* The WOLFSSL_CTX referenced is untouched.
*
* @param [in] ssl SSL/TLS object.
* @return 1 on success.
* @return BAD_FUNC_ARG when ssl is NULL.
*/
int wolfSSL_UnloadCertsKeys(WOLFSSL* ssl)
{
int ret = 1;
/* Validate parameter. */
if (ssl == NULL) {
WOLFSSL_MSG("Null function arg");
ret = BAD_FUNC_ARG;
}
else {
if (ssl->buffers.weOwnCert && !ssl->keepCert) {
WOLFSSL_MSG("Unloading cert");
FreeDer(&ssl->buffers.certificate);
#ifdef KEEP_OUR_CERT
wolfSSL_X509_free(ssl->ourCert);
ssl->ourCert = NULL;
#endif
ssl->buffers.weOwnCert = 0;
}
if (ssl->buffers.weOwnCertChain) {
WOLFSSL_MSG("Unloading cert chain");
FreeDer(&ssl->buffers.certChain);
ssl->buffers.weOwnCertChain = 0;
}
if (ssl->buffers.weOwnKey) {
WOLFSSL_MSG("Unloading key");
if (ssl->buffers.key != NULL && ssl->buffers.key->buffer != NULL)
ForceZero(ssl->buffers.key->buffer, ssl->buffers.key->length);
FreeDer(&ssl->buffers.key);
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
FreeDer(&ssl->buffers.keyMask);
#endif
ssl->buffers.weOwnKey = 0;
}
#ifdef WOLFSSL_DUAL_ALG_CERTS
if (ssl->buffers.weOwnAltKey) {
WOLFSSL_MSG("Unloading alt key");
if (ssl->buffers.altKey != NULL &&
ssl->buffers.altKey->buffer != NULL) {
ForceZero(ssl->buffers.altKey->buffer,
ssl->buffers.altKey->length);
}
FreeDer(&ssl->buffers.altKey);
#ifdef WOLFSSL_BLIND_PRIVATE_KEY
FreeDer(&ssl->buffers.altKeyMask);
#endif
ssl->buffers.weOwnAltKey = 0;
}
#endif /* WOLFSSL_DUAL_ALG_CERTS */
}
return ret;
}
/* Unload CAs from the certificate manager of the SSL/TLS context.
*
* @param [in] ctx SSL/TLS context.
* @return 1 on success.
* @return BAD_FUNC_ARG when ctx or ctx->cm is NULL.
* @return BAD_MUTEX_E when locking fails.
*/
int wolfSSL_CTX_UnloadCAs(WOLFSSL_CTX* ctx)
{
int ret;
WOLFSSL_ENTER("wolfSSL_CTX_UnloadCAs");
/* Validate parameter. */
if (ctx == NULL) {
ret = BAD_FUNC_ARG;
}
else {
ret = wolfSSL_CertManagerUnloadCAs(ctx->cm);
}
return ret;
}
/* Unload Intermediate CAs from the certificate manager of the SSL/TLS context.
*
* @param [in] ctx SSL/TLS context.
* @return 1 on success.
* @return BAD_FUNC_ARG when ctx or ctx->cm is NULL.
* @return BAD_MUTEX_E when locking fails.
*/
int wolfSSL_CTX_UnloadIntermediateCerts(WOLFSSL_CTX* ctx)
{
int ret;
WOLFSSL_ENTER("wolfSSL_CTX_UnloadIntermediateCerts");
/* Validate parameter. */
if (ctx == NULL) {
ret = BAD_FUNC_ARG;
}
/* Lock reference count. */
else if ((ret = wolfSSL_RefWithMutexLock(&ctx->ref)) == 0) {
/* Must not have another reference for this operation to be done. */
if (ctx->ref.count > 1) {
WOLFSSL_MSG("ctx object must have a ref count of 1 before "
"unloading intermediate certs");
ret = BAD_STATE_E;
}
else {
ret = wolfSSL_CertManagerUnloadIntermediateCerts(ctx->cm);
}
/* Unlock reference count. */
if (wolfSSL_RefWithMutexUnlock(&ctx->ref) != 0) {
WOLFSSL_MSG("Failed to unlock mutex!");
}
}
return ret;
}
#ifdef WOLFSSL_TRUST_PEER_CERT
/* Unload trusted peers from the certificate manager of the SSL/TLS context.
*
* @param [in] ctx SSL/TLS context.
* @return 1 on success.
* @return BAD_FUNC_ARG when ctx or ctx->cm is NULL.
* @return BAD_MUTEX_E when locking fails.
*/
int wolfSSL_CTX_Unload_trust_peers(WOLFSSL_CTX* ctx)
{
int ret;
WOLFSSL_ENTER("wolfSSL_CTX_Unload_trust_peers");
/* Validate parameter. */
if (ctx == NULL) {
ret = BAD_FUNC_ARG;
}
else {
ret = wolfSSL_CertManagerUnload_trust_peers(ctx->cm);
}
return ret;
}
#ifdef WOLFSSL_LOCAL_X509_STORE
/* Unload trusted peers from the certificate manager of the SSL/TLS object.
*
* @param [in] ctx SSL/TLS context.
* @return 1 on success.
* @return BAD_FUNC_ARG when ssl is NULL.
* @return BAD_MUTEX_E when locking fails.
*/
int wolfSSL_Unload_trust_peers(WOLFSSL* ssl)
{
int ret;