-
Notifications
You must be signed in to change notification settings - Fork 974
Expand file tree
/
Copy pathssl_certman.c
More file actions
3382 lines (2957 loc) · 101 KB
/
ssl_certman.c
File metadata and controls
3382 lines (2957 loc) · 101 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_certman.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>
#include <wolfssl/internal.h>
#if !defined(WOLFSSL_SSL_CERTMAN_INCLUDED)
#ifndef WOLFSSL_IGNORE_FILE_WARN
#warning ssl_certman.c not to be compiled separately from ssl.c
#endif
#else
#ifndef NO_CERTS
/* Pick an available TLS method.
*
* Used when creating temporary WOLFSSL_CTX.
*
* @return A TLS method on success.
* @return NULL when no TLS method built into wolfSSL.
*/
static WC_INLINE WOLFSSL_METHOD* cm_pick_method(void* heap)
{
(void)heap;
#ifndef NO_WOLFSSL_CLIENT
#if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3)
return wolfSSLv3_client_method_ex(heap);
#elif !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
return wolfTLSv1_client_method_ex(heap);
#elif !defined(NO_OLD_TLS)
return wolfTLSv1_1_client_method_ex(heap);
#elif !defined(WOLFSSL_NO_TLS12)
return wolfTLSv1_2_client_method_ex(heap);
#elif defined(WOLFSSL_TLS13)
return wolfTLSv1_3_client_method_ex(heap);
#else
return NULL;
#endif
#elif !defined(NO_WOLFSSL_SERVER)
#if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3)
return wolfSSLv3_server_method_ex(heap);
#elif !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
return wolfTLSv1_server_method_ex(heap);
#elif !defined(NO_OLD_TLS)
return wolfTLSv1_1_server_method_ex(heap);
#elif !defined(WOLFSSL_NO_TLS12)
return wolfTLSv1_2_server_method_ex(heap);
#elif defined(WOLFSSL_TLS13)
return wolfTLSv1_3_server_method_ex(heap);
#else
return NULL;
#endif
#else
return NULL;
#endif
}
static void DoCertManagerFree(WOLFSSL_CERT_MANAGER* cm);
/* Create a new certificate manager with a heap hint.
*
* @param [in] heap Heap hint.
* @return Certificate manager object on success.
* @return NULL on failure.
*/
WOLFSSL_CERT_MANAGER* wolfSSL_CertManagerNew_ex(void* heap)
{
int err = 0;
WOLFSSL_CERT_MANAGER* cm;
WOLFSSL_ENTER("wolfSSL_CertManagerNew");
if (heap == NULL) {
WOLFSSL_MSG("heap param is null");
}
else {
/* Some systems may have heap in unexpected segments. (IRAM vs DRAM) */
WOLFSSL_MSG_EX("heap param = %p", heap);
}
WOLFSSL_MSG_EX("DYNAMIC_TYPE_CERT_MANAGER Allocating = %d bytes",
(word32)sizeof(WOLFSSL_CERT_MANAGER));
/* Allocate memory for certificate manager. */
cm = (WOLFSSL_CERT_MANAGER*)XMALLOC(sizeof(WOLFSSL_CERT_MANAGER), heap,
DYNAMIC_TYPE_CERT_MANAGER);
if (cm == NULL) {
WOLFSSL_MSG_EX("XMALLOC failed to allocate WOLFSSL_CERT_MANAGER %d "
"bytes.", (int)sizeof(WOLFSSL_CERT_MANAGER));
err = 1;
}
if (!err) {
/* Reset all fields. */
XMEMSET(cm, 0, sizeof(WOLFSSL_CERT_MANAGER));
/* Set heap hint early so cleanup can use it. */
cm->heap = heap;
/* Create a mutex for use when modify table of stored CAs. */
if (wc_InitMutex(&cm->caLock) != 0) {
WOLFSSL_MSG("Bad mutex init");
err = 1;
}
else {
cm->caLockInit = 1;
}
}
if (!err) {
/* Initialize reference count. */
wolfSSL_RefInit(&cm->ref, &err);
#ifdef WOLFSSL_REFCNT_ERROR_RETURN
if (err != 0) {
WOLFSSL_MSG("Bad reference count init");
}
else {
cm->refInit = 1;
}
#else
cm->refInit = 1;
#endif
}
#ifdef WOLFSSL_TRUST_PEER_CERT
if (!err) {
/* Create a mutex for use when modify table of trusted peers. */
if (wc_InitMutex(&cm->tpLock) != 0) {
WOLFSSL_MSG("Bad mutex init");
err = 1;
}
else {
cm->tpLockInit = 1;
}
}
#endif
if (!err) {
/* Set default minimum key sizes allowed. */
#ifndef NO_RSA
cm->minRsaKeySz = MIN_RSAKEY_SZ;
#endif
#ifdef HAVE_ECC
cm->minEccKeySz = MIN_ECCKEY_SZ;
#endif
#ifdef HAVE_FALCON
cm->minFalconKeySz = MIN_FALCONKEY_SZ;
#endif /* HAVE_FALCON */
#ifdef HAVE_DILITHIUM
cm->minDilithiumKeySz = MIN_DILITHIUMKEY_SZ;
#endif /* HAVE_DILITHIUM */
}
/* Dispose of certificate manager on error. The reference count may not
* have been initialized, so bypass the ref check and free directly. */
if (err && (cm != NULL)) {
DoCertManagerFree(cm);
cm = NULL;
}
return cm;
}
/* Create a new certificate manager.
*
* @return Certificate manager object on success.
* @return NULL on failure.
*/
WOLFSSL_CERT_MANAGER* wolfSSL_CertManagerNew(void)
{
/* No heap hint. */
return wolfSSL_CertManagerNew_ex(NULL);
}
/* Unconditionally dispose of all resources owned by the certificate manager
* and free cm itself, bypassing any reference count check. Only frees the
* sub-resources that are marked as initialized in the cm bitfield, so it is
* safe to call on a cm that was only partially initialized by
* wolfSSL_CertManagerNew_ex.
*
* @param [in, out] cm Certificate manager (must be non-NULL).
*/
static void DoCertManagerFree(WOLFSSL_CERT_MANAGER* cm)
{
#ifdef HAVE_CRL
/* Dispose of CRL handler. */
if (cm->crl != NULL) {
/* Dispose of CRL object - indicating dynamically allocated. */
FreeCRL(cm->crl, 1);
}
#endif
#ifdef HAVE_OCSP
/* Dispose of OCSP handler. */
if (cm->ocsp != NULL) {
FreeOCSP(cm->ocsp, 1);
}
/* Dispose of URL. */
XFREE(cm->ocspOverrideURL, cm->heap, DYNAMIC_TYPE_URL);
#if !defined(NO_WOLFSSL_SERVER) && \
(defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2))
/* Dispose of OCSP stapling handler. */
if (cm->ocsp_stapling) {
FreeOCSP(cm->ocsp_stapling, 1);
}
#endif
#endif /* HAVE_OCSP */
/* Dispose of CA table and mutex. */
FreeSignerTable(cm->caTable, CA_TABLE_SIZE, cm->heap);
if (cm->caLockInit) {
wc_FreeMutex(&cm->caLock);
}
#ifdef WOLFSSL_TRUST_PEER_CERT
/* Dispose of trusted peer table and mutex. */
FreeTrustedPeerTable(cm->tpTable, TP_TABLE_SIZE, cm->heap);
if (cm->tpLockInit) {
wc_FreeMutex(&cm->tpLock);
}
#endif
/* Dispose of reference count. */
if (cm->refInit) {
wolfSSL_RefFree(&cm->ref);
}
/* Dispose of certificate manager memory. */
XFREE(cm, cm->heap, DYNAMIC_TYPE_CERT_MANAGER);
}
/* Dispose of certificate manager.
*
* @param [in, out] cm Certificate manager.
*/
void wolfSSL_CertManagerFree(WOLFSSL_CERT_MANAGER* cm)
{
WOLFSSL_ENTER("wolfSSL_CertManagerFree");
/* Validate parameter. */
if (cm != NULL) {
int doFree = 0;
int ret;
/* Decrement reference count and check if value is 0. */
wolfSSL_RefDec(&cm->ref, &doFree, &ret);
#ifdef WOLFSSL_REFCNT_ERROR_RETURN
if (ret != 0) {
WOLFSSL_MSG("Couldn't lock cm mutex");
}
#else
(void)ret;
#endif
if (doFree) {
DoCertManagerFree(cm);
}
}
}
/* Increase reference count on certificate manager.
*
* @param [in, out] cm Certificate manager.
* @return WOLFSSL_SUCCESS on success.
* @return 0 when cm is NULL or locking mutex fails.
*/
int wolfSSL_CertManager_up_ref(WOLFSSL_CERT_MANAGER* cm)
{
int ret = WOLFSSL_SUCCESS;
/* Validate parameter. */
if (cm == NULL) {
ret = 0;
}
if (ret == WOLFSSL_SUCCESS) {
int err;
/* Increment reference. */
wolfSSL_RefInc(&cm->ref, &err);
#ifdef WOLFSSL_REFCNT_ERROR_RETURN
if (err) {
WOLFSSL_MSG("Failed to lock cm mutex");
ret = 0;
}
#else
(void)err;
#endif
}
return ret;
}
#if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
#if defined(WOLFSSL_SIGNER_DER_CERT)
static WC_INLINE int wolfssl_cm_get_certs_der(WOLFSSL_CERT_MANAGER* cm,
DerBuffer*** buffers, int* cnt)
{
int err = 0;
Signer* signers = NULL;
DerBuffer** certBuffers = NULL;
int i = 0;
word32 row = 0;
int numCerts = 0;
/* Iterate once to get the number of certs, for memory allocation
* purposes. */
for (row = 0; row < CA_TABLE_SIZE; row++) {
/* Get signer information of CAs in a row. */
signers = cm->caTable[row];
/* Count each signer in row that has a DER certificate buffer. */
while ((signers != NULL) && (signers->derCert != NULL) &&
(signers->derCert->buffer != NULL)) {
++numCerts;
signers = signers->next;
}
}
/* Check we found certificates. */
if (numCerts == 0) {
err = 1;
}
if (!err) {
/* Allocate memory for pointers to each DER buffer. */
certBuffers = (DerBuffer**)XMALLOC(
sizeof(DerBuffer*) * (size_t)numCerts, cm->heap,
DYNAMIC_TYPE_TMP_BUFFER);
if (certBuffers == NULL) {
err = 1;
}
}
if (!err) {
/* Reset pointers. */
XMEMSET(certBuffers, 0, sizeof(DerBuffer*) * (size_t)numCerts);
}
/* Copy the certs locally so that we can release the caLock. If the lock
* is held when wolfSSL_d2i_X509 is called, GetCA will also try to get
* the lock, leading to deadlock. */
for (row = 0; (!err) && (row < CA_TABLE_SIZE); row++) {
/* Get signer information of CAs in a row. */
signers = cm->caTable[row];
/* Copy each DER certificate buffer of signers in a row. */
while ((signers != NULL) && (signers->derCert != NULL) &&
(signers->derCert->buffer != NULL)) {
/* Allocate memory to hold DER certificate buffer. */
int ret = AllocDer(&certBuffers[i], signers->derCert->length,
CA_TYPE, cm->heap);
if (ret < 0) {
err = 1;
break;
}
/* Copy buffer into array element. */
XMEMCPY(certBuffers[i]->buffer, signers->derCert->buffer,
signers->derCert->length);
certBuffers[i]->length = signers->derCert->length;
/* Store in next index. */
++i;
/* Move on to next signer in row. */
signers = signers->next;
}
}
*buffers = certBuffers;
*cnt = numCerts;
return err;
}
/* Retrieve stack of X509 certificates in a certificate manager (CM).
*
* @param [in] cm Certificate manager.
*
* @return Stack of X509 certs on success
* @return NULL on failure.
*/
WOLFSSL_STACK* wolfSSL_CertManagerGetCerts(WOLFSSL_CERT_MANAGER* cm)
{
WOLFSSL_STACK* sk = NULL;
int numCerts = 0;
DerBuffer** certBuffers = NULL;
int i = 0;
int err = 0;
WOLFSSL_ENTER("wolfSSL_CertManagerGetCerts");
/* Validate parameter. */
if (cm == NULL) {
err = 1;
}
if (!err) {
/* Create an empty certificate stack to return. */
sk = wolfSSL_sk_X509_new_null();
if (sk == NULL) {
err = 1;
}
}
/* Lock CA table. */
if ((!err) && (wc_LockMutex(&cm->caLock) != 0)) {
err = 1;
}
if (!err) {
err = wolfssl_cm_get_certs_der(cm, &certBuffers, &numCerts);
/* Release CA lock. */
wc_UnLockMutex(&cm->caLock);
}
/* Put each DER certificate buffer into a stack of WOLFSSL_X509 */
for (i = 0; (!err) && (i < numCerts); ++i) {
const byte* derBuffer = NULL;
WOLFSSL_X509* x509 = NULL;
/* Get pointer to DER encoding of certificate. */
derBuffer = certBuffers[i]->buffer;
/* Decode certificate. */
wolfSSL_d2i_X509(&x509, &derBuffer, (int)certBuffers[i]->length);
if (x509 == NULL) {
err = 1;
}
/* Decode certificate. */
if ((!err) && (wolfSSL_sk_X509_push(sk, x509) <= 0)) {
wolfSSL_X509_free(x509);
x509 = NULL;
err = 1;
}
}
if (certBuffers != NULL) {
/* Dispose of temporary cert storage (for access outside of lock). */
for (i = 0; i < numCerts && certBuffers[i] != NULL; ++i) {
FreeDer(&certBuffers[i]);
}
XFREE(certBuffers, cm->heap, DYNAMIC_TYPE_TMP_BUFFER);
}
/* Dispose of stack of certificates on error. */
if (err && (sk != NULL)) {
wolfSSL_sk_X509_pop_free(sk, NULL);
sk = NULL;
}
return sk;
}
#endif /* WOLFSSL_SIGNER_DER_CERT */
#endif /* OPENSSL_EXTRA && !NO_FILESYSTEM */
/* Unload the CA signer table.
*
* @param [in] cm Certificate manager.
* @return WOLFSSL_SUCCESS on success.
* @return BAD_FUNC_ARG when cm is NULL.
* @return BAD_MUTEX_E when locking fails.
*/
int wolfSSL_CertManagerUnloadCAs(WOLFSSL_CERT_MANAGER* cm)
{
int ret = WOLFSSL_SUCCESS;
WOLFSSL_ENTER("wolfSSL_CertManagerUnloadCAs");
/* Validate parameter. */
if (cm == NULL) {
ret = BAD_FUNC_ARG;
}
/* Lock CA table. */
if ((ret == WOLFSSL_SUCCESS) && (wc_LockMutex(&cm->caLock) != 0)) {
ret = BAD_MUTEX_E;
}
if (ret == WOLFSSL_SUCCESS) {
/* Dispose of CA table. */
FreeSignerTable(cm->caTable, CA_TABLE_SIZE, cm->heap);
/* Unlock CA table. */
wc_UnLockMutex(&cm->caLock);
}
return ret;
}
int wolfSSL_CertManagerUnloadTypeCerts(
WOLFSSL_CERT_MANAGER* cm, byte type)
{
int ret = WOLFSSL_SUCCESS;
WOLFSSL_ENTER("wolfSSL_CertManagerUnloadTypeCerts");
/* Validate parameter. */
if (cm == NULL) {
ret = BAD_FUNC_ARG;
}
/* Lock CA table. */
if ((ret == WOLFSSL_SUCCESS) && (wc_LockMutex(&cm->caLock) != 0)) {
ret = BAD_MUTEX_E;
}
if (ret == WOLFSSL_SUCCESS) {
/* Dispose of CA table. */
FreeSignerTableType(cm->caTable, CA_TABLE_SIZE, type,
cm->heap);
/* Unlock CA table. */
wc_UnLockMutex(&cm->caLock);
}
return ret;
}
#if defined(OPENSSL_EXTRA)
static int wolfSSL_CertManagerUnloadTempIntermediateCerts(
WOLFSSL_CERT_MANAGER* cm)
{
WOLFSSL_ENTER("wolfSSL_CertManagerUnloadTempIntermediateCerts");
return wolfSSL_CertManagerUnloadTypeCerts(cm, WOLFSSL_TEMP_CA);
}
#endif
int wolfSSL_CertManagerUnloadIntermediateCerts(
WOLFSSL_CERT_MANAGER* cm)
{
WOLFSSL_ENTER("wolfSSL_CertManagerUnloadIntermediateCerts");
return wolfSSL_CertManagerUnloadTypeCerts(cm, WOLFSSL_CHAIN_CA);
}
#ifdef WOLFSSL_TRUST_PEER_CERT
/* Unload the trusted peers table.
*
* @param [in] cm Certificate manager.
* @return WOLFSSL_SUCCESS on success.
* @return BAD_FUNC_ARG when cm is NULL.
* @return BAD_MUTEX_E when locking fails.
*/
int wolfSSL_CertManagerUnload_trust_peers(WOLFSSL_CERT_MANAGER* cm)
{
int ret = WOLFSSL_SUCCESS;
WOLFSSL_ENTER("wolfSSL_CertManagerUnload_trust_peers");
/* Validate parameter. */
if (cm == NULL) {
ret = BAD_FUNC_ARG;
}
/* Lock trusted peers table. */
if ((ret == WOLFSSL_SUCCESS) && (wc_LockMutex(&cm->tpLock) != 0)) {
ret = BAD_MUTEX_E;
}
if (ret == WOLFSSL_SUCCESS) {
/* Dispose of trusted peers table. */
FreeTrustedPeerTable(cm->tpTable, TP_TABLE_SIZE, cm->heap);
/* Unlock trusted peers table. */
wc_UnLockMutex(&cm->tpLock);
}
return ret;
}
#endif /* WOLFSSL_TRUST_PEER_CERT */
/* Load certificate/s from buffer with flags and type.
*
* @param [in] cm Certificate manager.
* @param [in] buff Buffer holding encoding of certificate.
* @param [in] sz Length in bytes of data in buffer.
* @param [in] format Format of encoding. Valid values:
* WOLFSSL_FILETYPE_ASN1, WOLFSSL_FILETYPE_PEM.
* @param [in] userChain Indicates buffer holds chain of certificates.
* @param [in] flags Flags to modify behaviour of loading. Valid flags:
* WOLFSSL_LOAD_FLAG_IGNORE_ERR,
* WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY,
* WOLFSSL_LOAD_FLAG_PEM_CA_ONLY,
* WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR, and
* WOLFSSL_LOAD_FLAG_IGNORE_ZEROFILE.
* @param [in] type The CA cert's type, used in the internal CA
table. Defaults to WOLFSSL_USER_CA, passing
in WOLFSSL_USER_CA = noop. Recommended to
set to WOLFSSL_USER_INTER when loading
intermediate certs to allow unloading via
wolfSSL_CertManagerUnloadTypeCerts.
* @return WOLFSSL_SUCCESS on success.
* @return WOLFSSL_FATAL_ERROR when cm is NULL or failed create WOLFSSL_CTX.
* @return Other values on loading failure.
*/
int wolfSSL_CertManagerLoadCABufferType(WOLFSSL_CERT_MANAGER* cm,
const unsigned char* buff, long sz, int format, int userChain,
word32 flags, int type)
{
int ret = WOLFSSL_SUCCESS;
WOLFSSL_CTX* tmp = NULL;
DecodedCert* dCert = NULL;
DerBuffer* der = NULL;
WOLFSSL_ENTER("wolfSSL_CertManagerLoadCABufferType");
/* Validate parameters. */
if (cm == NULL) {
WOLFSSL_MSG("No CertManager error");
ret = WOLFSSL_FATAL_ERROR;
}
/* Allocate a temporary WOLFSSL_CTX to load with. */
if ((ret == WOLFSSL_SUCCESS) && ((tmp =
wolfSSL_CTX_new_ex(cm_pick_method(cm->heap), cm->heap)) == NULL)) {
WOLFSSL_MSG("CTX new failed");
ret = WOLFSSL_FATAL_ERROR;
}
if (ret == WOLFSSL_SUCCESS) {
/* Some configurations like OPENSSL_COMPATIBLE_DEFAULTS may turn off
* verification by default. Let's restore our desired defaults. */
wolfSSL_CTX_set_verify(tmp, WOLFSSL_VERIFY_DEFAULT, NULL);
/* Replace certificate manager with one to load certificate/s into. */
wolfSSL_CertManagerFree(tmp->cm);
tmp->cm = cm;
/* Load certificate buffer. */
ret = wolfSSL_CTX_load_verify_buffer_ex(tmp, buff, sz, format,
userChain, flags);
/* Clear certificate manager in WOLFSSL_CTX so it won't be freed. */
tmp->cm = NULL;
}
if (ret == WOLFSSL_SUCCESS && type != WOLFSSL_USER_CA) {
dCert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), cm->heap,
DYNAMIC_TYPE_DCERT);
if (dCert == NULL) {
ret = WOLFSSL_FATAL_ERROR;
} else {
XMEMSET(dCert, 0, sizeof(DecodedCert));
if (format == WOLFSSL_FILETYPE_PEM) {
#ifndef WOLFSSL_PEM_TO_DER
ret = NOT_COMPILED_IN;
#else
ret = PemToDer(buff, sz, CERT_TYPE, &der, cm->heap, NULL, NULL);
if (!ret) {
/* Replace buffer pointer and size with DER buffer. */
buff = der->buffer;
sz = (long)der->length;
ret = WOLFSSL_SUCCESS;
} else {
WOLFSSL_ERROR(ret);
ret = WOLFSSL_FATAL_ERROR;
}
#endif
}
if (ret == WOLFSSL_SUCCESS) {
wc_InitDecodedCert(dCert, buff,
(word32)sz, cm->heap);
ret = wc_ParseCert(dCert, CERT_TYPE, NO_VERIFY, NULL);
if (ret) {
ret = WOLFSSL_FATAL_ERROR;
} else {
ret = SetCAType(cm, dCert->extSubjKeyId, type);
}
}
if (dCert) {
wc_FreeDecodedCert(dCert);
XFREE(dCert, cm->heap, DYNAMIC_TYPE_DCERT);
}
if (der) {
FreeDer(&der);
}
}
}
/* Dispose of temporary WOLFSSL_CTX. */
wolfSSL_CTX_free(tmp);
return ret;
}
/* Load certificate/s from buffer with flags.
*
* @param [in] cm Certificate manager.
* @param [in] buff Buffer holding encoding of certificate.
* @param [in] sz Length in bytes of data in buffer.
* @param [in] format Format of encoding. Valid values:
* WOLFSSL_FILETYPE_ASN1, WOLFSSL_FILETYPE_PEM.
* @param [in] userChain Indicates buffer holds chain of certificates.
* @param [in] flags Flags to modify behaviour of loading. Valid flags:
* WOLFSSL_LOAD_FLAG_IGNORE_ERR,
* WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY,
* WOLFSSL_LOAD_FLAG_PEM_CA_ONLY,
* WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR, and
* WOLFSSL_LOAD_FLAG_IGNORE_ZEROFILE.
* @return WOLFSSL_SUCCESS on success.
* @return WOLFSSL_FATAL_ERROR when cm is NULL or failed create WOLFSSL_CTX.
* @return Other values on loading failure.
*/
int wolfSSL_CertManagerLoadCABuffer_ex(WOLFSSL_CERT_MANAGER* cm,
const unsigned char* buff, long sz, int format, int userChain, word32 flags)
{
return wolfSSL_CertManagerLoadCABufferType(cm, buff, sz, format, userChain,
flags, WOLFSSL_USER_CA);
}
/* Load certificate/s from buffer into table.
*
* Uses default load verification flags and is not a user chain.
*
* @param [in] cm Certificate manager.
* @param [in] buff Buffer holding encoding of certificate.
* @param [in] sz Length in bytes of data in buffer.
* @param [in] format Format of encoding. Valid values:
* WOLFSSL_FILETYPE_ASN1, WOLFSSL_FILETYPE_PEM.
* @return WOLFSSL_SUCCESS on success.
* @return WOLFSSL_FATAL_ERROR when cm is NULL or failed create WOLFSSL_CTX.
* @return Other values on loading failure.
*/
int wolfSSL_CertManagerLoadCABuffer(WOLFSSL_CERT_MANAGER* cm,
const unsigned char* buff, long sz, int format)
{
return wolfSSL_CertManagerLoadCABuffer_ex(cm, buff, sz, format, 0,
WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS);
}
#ifndef NO_WOLFSSL_CM_VERIFY
/* Set the verification callback into certificate manager.
*
* @param [in] cm Certificate manager.
* @param [in] vc Verification callback.
*/
void wolfSSL_CertManagerSetVerify(WOLFSSL_CERT_MANAGER* cm, VerifyCallback vc)
{
WOLFSSL_ENTER("wolfSSL_CertManagerSetVerify");
if (cm != NULL) {
cm->verifyCallback = vc;
}
}
#endif /* !NO_WOLFSSL_CM_VERIFY */
#ifdef WC_ASN_UNKNOWN_EXT_CB
void wolfSSL_CertManagerSetUnknownExtCallback(WOLFSSL_CERT_MANAGER* cm,
wc_UnknownExtCallback cb)
{
WOLFSSL_ENTER("wolfSSL_CertManagerSetUnknownExtCallback");
if (cm != NULL) {
cm->unknownExtCallback = cb;
}
}
#endif /* WC_ASN_UNKNOWN_EXT_CB */
#if (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)) || \
defined(OPENSSL_EXTRA)
/* Verify the certificate.
*
* Uses the verification callback if available.
*
* @param [in] cm Certificate manager.
* @param [in] buff Buffer holding encoded certificate.
* @param [in] sz Size in bytes of data in buffer.
* @param [in] format Format of encoding. Valid values:
* WOLFSSL_FILETYPE_ASN1, WOLFSSL_FILETYPE_PEM.
* @param [in] prev_err Previous error. Passed to callback.
* @return WOLFSSL_SUCCESS on success.
* @return MEMORY_E when dynamic memory allocation fails.
* @return NOT_COMPILED_IN when converting from PEM to DER is not a feature of
* the wolfSSL build.
*/
int CM_VerifyBuffer_ex(WOLFSSL_CERT_MANAGER* cm, const unsigned char* buff,
long sz, int format, int prev_err)
{
int ret = 0;
int fatal = 0;
DerBuffer* der = NULL;
WC_DECLARE_VAR(cert, DecodedCert, 1, 0);
WOLFSSL_ENTER("CM_VerifyBuffer_ex");
(void)prev_err;
/* Allocate memory for decoded certificate. */
WC_ALLOC_VAR_EX(cert, DecodedCert, 1, cm->heap, DYNAMIC_TYPE_DCERT,
{
ret=MEMORY_E;
fatal=1;
});
if (WC_VAR_OK(cert))
{
/* Reset fields of decoded certificate. */
XMEMSET(cert, 0, sizeof(DecodedCert));
if (format == WOLFSSL_FILETYPE_PEM) {
#ifndef WOLFSSL_PEM_TO_DER
ret = NOT_COMPILED_IN;
fatal = 1;
#else
/* Convert to DER from PEM. */
ret = PemToDer(buff, sz, CERT_TYPE, &der, cm->heap, NULL, NULL);
if (ret != 0) {
fatal = 1;
}
else {
/* Replace buffer pointer and size with DER buffer. */
buff = der->buffer;
sz = (long)der->length;
}
#endif
}
}
if (ret == 0) {
/* Create a decoded certificate with DER buffer. */
InitDecodedCert(cert, buff, (word32)sz, cm->heap);
#ifdef WC_ASN_UNKNOWN_EXT_CB
if (cm->unknownExtCallback != NULL)
wc_SetUnknownExtCallback(cert, cm->unknownExtCallback);
#endif
/* Parse DER into decoded certificate fields and verify signature
* against a known CA. */
ret = ParseCertRelative(cert, CERT_TYPE, VERIFY, cm, NULL);
}
#ifdef HAVE_CRL
if ((ret == 0) && cm->crlEnabled) {
/* Check for a CRL for the CA and check validity of certificate. */
ret = CheckCertCRL(cm->crl, cert);
}
#endif
(void)fatal;
#if !defined(NO_WOLFSSL_CM_VERIFY) && \
(!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
/* Use callback to perform verification too if available. */
if ((!fatal) && cm->verifyCallback) {
WC_DECLARE_VAR(args, ProcPeerCertArgs, 1, 0);
buffer certBuf;
#ifdef WOLFSSL_SMALL_STACK
/* Allocate memory for object to hold arguments for callback. */
args = (ProcPeerCertArgs*)XMALLOC(sizeof(ProcPeerCertArgs), cm->heap,
DYNAMIC_TYPE_TMP_BUFFER);
if (args == NULL) {
ret = MEMORY_E;
fatal = 1;
}
if (!fatal)
#endif
{
XMEMSET(args, 0, sizeof(ProcPeerCertArgs));
/* DER encoding. */
certBuf.buffer = (byte*)buff;
certBuf.length = (unsigned int)sz;
/* One certificate available. */
args->totalCerts = 1;
args->certs = &certBuf;
args->dCert = cert;
args->dCertInit = 1;
/* Replace value in ret with an error value passed in. */
if (prev_err != 0) {
ret = prev_err;
}
/* Use callback to verify certificate. */
ret = DoVerifyCallback(cm, NULL, ret, args);
}
WC_FREE_VAR_EX(args, cm->heap, DYNAMIC_TYPE_TMP_BUFFER);
}
#endif
/* Dispose of allocated memory. */
FreeDecodedCert(cert);
FreeDer(&der);
WC_FREE_VAR_EX(cert, cm->heap, DYNAMIC_TYPE_DCERT);
/* Convert the ret value to a return value. */
return (ret == 0) ? WOLFSSL_SUCCESS : ret;
}
/* Verify the certificate.
*
* Uses the verification callback if available.
*
* @param [in] cm Certificate manager.
* @param [in] buff Buffer holding encoded certificate.
* @param [in] sz Size in bytes of data in buffer.
* @param [in] format Format of encoding. Valid values:
* WOLFSSL_FILETYPE_ASN1, WOLFSSL_FILETYPE_PEM.
* @param [in] prev_err Previous error. Passed to callback.
* @return WOLFSSL_SUCCESS on success.
* @return BAD_FUNC_ARG when cm or buff is NULL or sz is negative or zero.
* @return WOLFSSL_BAD_FILETYPE when format is invalid.
* @return MEMORY_E when dynamic memory allocation fails.
* @return NOT_COMPILED_IN when converting from PEM to DER is not a feature of
* the wolfSSL build.
*/
int wolfSSL_CertManagerVerifyBuffer(WOLFSSL_CERT_MANAGER* cm,
const unsigned char* buff, long sz, int format)
{
int ret;
WOLFSSL_ENTER("wolfSSL_CertManagerVerifyBuffer");
/* Validate parameters. */
if ((cm == NULL) || (buff == NULL) || (sz <= 0)) {
ret = BAD_FUNC_ARG;
}
else if ((format != WOLFSSL_FILETYPE_ASN1) &&
(format != WOLFSSL_FILETYPE_PEM)) {
ret = WOLFSSL_BAD_FILETYPE;
}
else {
/* No previous error. */
ret = CM_VerifyBuffer_ex(cm, buff, sz, format, 0);
}
return ret;
}
#endif /* (!NO_WOLFSSL_CLIENT || !WOLFSSL_NO_CLIENT_AUTH) || OPENSSL_EXTRA */
#ifndef NO_FILESYSTEM
#if (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)) || \
defined(OPENSSL_EXTRA)
/* Verify the certificate loaded from a file.
*
* Uses the verification callback if available.
*
* @param [in] cm Certificate manager.
* @param [in] format Format of encoding. Valid values:
* WOLFSSL_FILETYPE_ASN1, WOLFSSL_FILETYPE_PEM.
* @param [in] prev_err Previous error. Passed to callback.
* @return WOLFSSL_SUCCESS on success.
* @return BAD_FUNC_ARG when cm or buff is NULL or sz is negative.
* @return WOLFSSL_BAD_FILETYPE when format is invalid.
* @return WOLFSSL_BAD_FILE when reading the certificate file fails.
* @return MEMORY_E when dynamic memory allocation fails.
* @return NOT_COMPILED_IN when converting from PEM to DER is not a feature of
* the wolfSSL build.
*/
int wolfSSL_CertManagerVerify(WOLFSSL_CERT_MANAGER* cm, const char* fname,
int format)
{
int ret = WOLFSSL_SUCCESS;
#ifndef WOLFSSL_SMALL_STACK
byte staticBuffer[FILE_BUFFER_SIZE];
#endif
byte* buff = NULL;
long sz = 0;
XFILE file = XBADFILE;
WOLFSSL_ENTER("wolfSSL_CertManagerVerify");
#ifndef WOLFSSL_SMALL_STACK
buff = staticBuffer;
#endif
/* Validate parameters. cm and format validated in:
* wolfSSL_CertManagerVerifyBuffer */
if ((cm == NULL) || (fname == NULL)) {
ret = BAD_FUNC_ARG;
}
/* Open the file containing a certificate. */
if ((ret == WOLFSSL_SUCCESS) &&
((file = XFOPEN(fname, "rb")) == XBADFILE)) {
ret = WOLFSSL_BAD_FILE;
}
/* Get the length of the file. */
if (ret == WOLFSSL_SUCCESS) {
ret = wolfssl_file_len(file, &sz);
if (ret == 0) {
ret = WOLFSSL_SUCCESS;
}
}
/* Allocate dynamic memory for file contents if no static buffer or too
* small. */
#ifndef WOLFSSL_SMALL_STACK
if ((ret == WOLFSSL_SUCCESS) && (sz > (long)sizeof(staticBuffer)))
#else
if (ret == WOLFSSL_SUCCESS)
#endif
{
WOLFSSL_MSG("Getting dynamic buffer");
buff = (byte*)XMALLOC((size_t)sz, cm->heap, DYNAMIC_TYPE_FILE);
if (buff == NULL) {
ret = WOLFSSL_BAD_FILE;
}
}
/* Read all the file into buffer. */
if ((ret == WOLFSSL_SUCCESS) && (XFREAD(buff, 1, (size_t)sz, file) !=
(size_t)sz)) {
ret = WOLFSSL_BAD_FILE;
}
/* Close file if opened. */