-
Notifications
You must be signed in to change notification settings - Fork 100
/
x509vfy.c
2120 lines (1819 loc) · 65.7 KB
/
x509vfy.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
/*
* XML Security Library (http://www.aleksey.com/xmlsec).
*
* X509 certificates verification support functions for OpenSSL.
*
* This is free software; see Copyright file in the source
* distribution for preciese wording.
*
* Copyright (C) 2002-2024 Aleksey Sanin <[email protected]>. All Rights Reserved.
*/
/**
* SECTION:x509
*/
#include "globals.h"
#ifndef XMLSEC_NO_X509
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <xmlsec/xmlsec.h>
#include <xmlsec/keys.h>
#include <xmlsec/keyinfo.h>
#include <xmlsec/keysmngr.h>
#include <xmlsec/base64.h>
#include <xmlsec/errors.h>
#include <xmlsec/private.h>
#include <xmlsec/xmltree.h>
#include <xmlsec/openssl/crypto.h>
#include <xmlsec/openssl/evp.h>
#include <xmlsec/openssl/x509.h>
#include "openssl_compat.h"
#include <openssl/evp.h>
#include <openssl/x509.h>
#include <openssl/x509_vfy.h>
#include <openssl/x509v3.h>
#include "../cast_helpers.h"
#include "openssl_compat.h"
#include "private.h"
#ifdef OPENSSL_IS_BORINGSSL
typedef size_t x509_size_t;
#else /* OPENSSL_IS_BORINGSSL */
typedef int x509_size_t;
#endif /* OPENSSL_IS_BORINGSSL */
/**************************************************************************
*
* Internal OpenSSL X509 store CTX
*
*************************************************************************/
typedef struct _xmlSecOpenSSLX509StoreCtx xmlSecOpenSSLX509StoreCtx,
*xmlSecOpenSSLX509StoreCtxPtr;
struct _xmlSecOpenSSLX509StoreCtx {
X509_STORE* xst;
STACK_OF(X509)* untrusted;
STACK_OF(X509_CRL)* crls;
X509_VERIFY_PARAM * vpm;
};
/****************************************************************************
*
* xmlSecOpenSSLKeyDataStoreX509Id:
*
***************************************************************************/
XMLSEC_KEY_DATA_STORE_DECLARE(OpenSSLX509Store, xmlSecOpenSSLX509StoreCtx)
#define xmlSecOpenSSLX509StoreSize XMLSEC_KEY_DATA_STORE_SIZE(OpenSSLX509Store)
static int xmlSecOpenSSLX509StoreInitialize (xmlSecKeyDataStorePtr store);
static void xmlSecOpenSSLX509StoreFinalize (xmlSecKeyDataStorePtr store);
static xmlSecKeyDataStoreKlass xmlSecOpenSSLX509StoreKlass = {
sizeof(xmlSecKeyDataStoreKlass),
xmlSecOpenSSLX509StoreSize,
/* data */
xmlSecNameX509Store, /* const xmlChar* name; */
/* constructors/destructor */
xmlSecOpenSSLX509StoreInitialize, /* xmlSecKeyDataStoreInitializeMethod initialize; */
xmlSecOpenSSLX509StoreFinalize, /* xmlSecKeyDataStoreFinalizeMethod finalize; */
/* reserved for the future */
NULL, /* void* reserved0; */
NULL, /* void* reserved1; */
};
static int xmlSecOpenSSLX509VerifyCRL (X509_STORE* xst,
X509_STORE_CTX* xsc,
STACK_OF(X509)* untrusted,
X509_CRL *crl,
xmlSecKeyInfoCtx* keyInfoCtx);
static X509* xmlSecOpenSSLX509FindChildCert (STACK_OF(X509) *chain,
X509 *cert);
static X509_NAME* xmlSecOpenSSLX509NameRead (const xmlChar *str);
static int xmlSecOpenSSLX509NameStringRead (const xmlChar **in,
xmlSecSize *inSize,
xmlSecByte *out,
xmlSecSize outSize,
xmlSecSize *outWritten,
xmlSecByte delim,
int ingoreTrailingSpaces);
static int xmlSecOpenSSLX509NamesCompare (X509_NAME *a,
X509_NAME *b);
static STACK_OF(X509_NAME_ENTRY)* xmlSecOpenSSLX509_NAME_ENTRIES_copy (X509_NAME *a);
static int xmlSecOpenSSLX509_NAME_ENTRIES_cmp (STACK_OF(X509_NAME_ENTRY) * a,
STACK_OF(X509_NAME_ENTRY) * b);
static int xmlSecOpenSSLX509_NAME_ENTRY_cmp (const X509_NAME_ENTRY * const *a,
const X509_NAME_ENTRY * const *b);
static STACK_OF(X509)* xmlSecOpenSSLX509StoreCombineCerts (STACK_OF(X509)* certs1,
STACK_OF(X509)* certs2);
/**
* xmlSecOpenSSLX509StoreGetKlass:
*
* The OpenSSL X509 certificates key data store klass.
*
* Returns: pointer to OpenSSL X509 certificates key data store klass.
*/
xmlSecKeyDataStoreId
xmlSecOpenSSLX509StoreGetKlass(void) {
return(&xmlSecOpenSSLX509StoreKlass);
}
/**
* xmlSecOpenSSLX509StoreFindCert:
* @store: the pointer to X509 key data store klass.
* @subjectName: the desired certificate name.
* @issuerName: the desired certificate issuer name.
* @issuerSerial: the desired certificate issuer serial number.
* @ski: the desired certificate SKI.
* @keyInfoCtx: the pointer to <dsig:KeyInfo/> element processing context.
*
* Deprecated. Searches @store for a certificate that matches given criteria.
*
* Returns: pointer to found certificate or NULL if certificate is not found
* or an error occurs.
*/
X509*
xmlSecOpenSSLX509StoreFindCert(xmlSecKeyDataStorePtr store, xmlChar *subjectName,
xmlChar *issuerName, xmlChar *issuerSerial,
xmlChar *ski, xmlSecKeyInfoCtx* keyInfoCtx
) {
if(ski != NULL) {
xmlSecSize skiDecodedSize = 0;
int ret;
/* our usual trick with base64 decode */
ret = xmlSecBase64DecodeInPlace(ski, &skiDecodedSize);
if(ret < 0) {
xmlSecInternalError2("xmlSecBase64DecodeInPlace", NULL,
"ski=%s", xmlSecErrorsSafeString(ski));
return(NULL);
}
return(xmlSecOpenSSLX509StoreFindCert_ex(store, subjectName, issuerName, issuerSerial,
(xmlSecByte*)ski, skiDecodedSize, keyInfoCtx));
} else {
return(xmlSecOpenSSLX509StoreFindCert_ex(store, subjectName, issuerName, issuerSerial,
NULL, 0, keyInfoCtx));
}
}
/**
* xmlSecOpenSSLX509StoreFindCert_ex:
* @store: the pointer to X509 key data store klass.
* @subjectName: the desired certificate name.
* @issuerName: the desired certificate issuer name.
* @issuerSerial: the desired certificate issuer serial number.
* @ski: the desired certificate SKI.
* @skiSize: the desired certificate SKI size.
* @keyInfoCtx: the pointer to <dsig:KeyInfo/> element processing context.
*
* Deprecated. Searches @store for a certificate that matches given criteria.
*
* Returns: pointer to found certificate or NULL if certificate is not found
* or an error occurs.
*/
X509*
xmlSecOpenSSLX509StoreFindCert_ex(xmlSecKeyDataStorePtr store,
xmlChar *subjectName,
xmlChar *issuerName, xmlChar *issuerSerial,
xmlSecByte * ski, xmlSecSize skiSize,
xmlSecKeyInfoCtx* keyInfoCtx ATTRIBUTE_UNUSED
) {
xmlSecOpenSSLX509StoreCtxPtr ctx;
xmlSecOpenSSLX509FindCertCtx findCertCtx;
x509_size_t ii;
int ret;
X509* res = NULL;
xmlSecAssert2(xmlSecKeyDataStoreCheckId(store, xmlSecOpenSSLX509StoreId), NULL);
UNREFERENCED_PARAMETER(keyInfoCtx);
ctx = xmlSecOpenSSLX509StoreGetCtx(store);
xmlSecAssert2(ctx != NULL, NULL);
/* do we have any certs at all? */
if(ctx->untrusted == NULL) {
return(NULL);
}
ret = xmlSecOpenSSLX509FindCertCtxInitialize(&findCertCtx,
subjectName,
issuerName, issuerSerial,
ski, skiSize);
if(ret < 0) {
xmlSecInternalError("xmlSecOpenSSLX509FindCertCtxInitialize", NULL);
xmlSecOpenSSLX509FindCertCtxFinalize(&findCertCtx);
return(NULL);
}
for(ii = 0; ii < sk_X509_num(ctx->untrusted); ++ii) {
X509 * cert = sk_X509_value(ctx->untrusted, ii);
if(cert == NULL) {
continue;
}
ret = xmlSecOpenSSLX509FindCertCtxMatch(&findCertCtx, cert);
if(ret < 0) {
xmlSecInternalError("xmlSecOpenSSLX509FindCertCtxMatch", NULL);
xmlSecOpenSSLX509FindCertCtxFinalize(&findCertCtx);
return(NULL);
} else if(ret == 1) {
res = cert;
break;
}
}
/* done */
xmlSecOpenSSLX509FindCertCtxFinalize(&findCertCtx);
return(res);
}
X509*
xmlSecOpenSSLX509StoreFindCertByValue(xmlSecKeyDataStorePtr store, xmlSecKeyX509DataValuePtr x509Value) {
xmlSecOpenSSLX509StoreCtxPtr ctx;
xmlSecOpenSSLX509FindCertCtx findCertCtx;
x509_size_t ii;
int ret;
X509* res = NULL;
xmlSecAssert2(store != NULL, NULL);
xmlSecAssert2(xmlSecKeyDataStoreCheckId(store, xmlSecOpenSSLX509StoreId), NULL);
xmlSecAssert2(x509Value != NULL, NULL);
ctx = xmlSecOpenSSLX509StoreGetCtx(store);
xmlSecAssert2(ctx != NULL, NULL);
/* do we have any certs at all? */
if(ctx->untrusted == NULL) {
return(NULL);
}
ret = xmlSecOpenSSLX509FindCertCtxInitializeFromValue(&findCertCtx, x509Value);
if(ret < 0) {
xmlSecInternalError("xmlSecOpenSSLX509FindCertCtxInitializeFromValue", NULL);
xmlSecOpenSSLX509FindCertCtxFinalize(&findCertCtx);
return(NULL);
}
for(ii = 0; ii < sk_X509_num(ctx->untrusted); ++ii) {
X509 * cert = sk_X509_value(ctx->untrusted, ii);
if(cert == NULL) {
continue;
}
ret = xmlSecOpenSSLX509FindCertCtxMatch(&findCertCtx, cert);
if(ret < 0) {
xmlSecInternalError("xmlSecOpenSSLX509FindCertCtxMatch", NULL);
xmlSecOpenSSLX509FindCertCtxFinalize(&findCertCtx);
return(NULL);
} else if(ret == 1) {
res = cert;
break;
}
}
/* done */
xmlSecOpenSSLX509FindCertCtxFinalize(&findCertCtx);
return(res);
}
xmlSecKeyPtr
xmlSecOpenSSLX509FindKeyByValue(xmlSecPtrListPtr keysList, xmlSecKeyX509DataValuePtr x509Value) {
xmlSecOpenSSLX509FindCertCtx findCertCtx;
xmlSecSize keysListSize, ii;
xmlSecKeyPtr res = NULL;
int ret;
xmlSecAssert2(keysList != NULL, NULL);
xmlSecAssert2(x509Value != NULL, NULL);
ret = xmlSecOpenSSLX509FindCertCtxInitializeFromValue(&findCertCtx, x509Value);
if(ret < 0) {
xmlSecInternalError("xmlSecOpenSSLX509FindCertCtxInitializeFromValue", NULL);
xmlSecOpenSSLX509FindCertCtxFinalize(&findCertCtx);
return(NULL);
}
keysListSize = xmlSecPtrListGetSize(keysList);
for(ii = 0; ii < keysListSize; ++ii) {
xmlSecKeyPtr key;
xmlSecKeyDataPtr keyData;
X509* keyCert;
/* get key's cert from x509 key data */
key = (xmlSecKeyPtr)xmlSecPtrListGetItem(keysList, ii);
if(key == NULL) {
continue;
}
keyData = xmlSecKeyGetData(key, xmlSecOpenSSLKeyDataX509Id);
if(keyData == NULL) {
continue;
}
keyCert = xmlSecOpenSSLKeyDataX509GetKeyCert(keyData);
if(keyCert == NULL) {
continue;
}
/* does it match? */
ret = xmlSecOpenSSLX509FindCertCtxMatch(&findCertCtx, keyCert);
if(ret < 0) {
xmlSecInternalError("xmlSecOpenSSLX509FindCertCtxMatch", NULL);
xmlSecOpenSSLX509FindCertCtxFinalize(&findCertCtx);
return(NULL);
} else if(ret == 1) {
res = key;
break;
}
}
/* done */
xmlSecOpenSSLX509FindCertCtxFinalize(&findCertCtx);
return(res);
}
static STACK_OF(X509_CRL)*
xmlSecOpenSSLX509StoreVerifyAndCopyCrls(X509_STORE* xst, X509_STORE_CTX* xsc, STACK_OF(X509)* untrusted, STACK_OF(X509_CRL)* crls,
xmlSecKeyInfoCtx* keyInfoCtx
) {
STACK_OF(X509_CRL)* verified_crls = NULL;
x509_size_t ii, num;
int ret;
xmlSecAssert2(xst != NULL, NULL);
xmlSecAssert2(xsc != NULL, NULL);
xmlSecAssert2(keyInfoCtx != NULL, NULL);
/* check if we have anything to copy */
if(crls == NULL) {
return(NULL);
}
num = sk_X509_CRL_num(crls);
if(num <= 0) {
return(NULL);
}
/* create output crls list */
verified_crls = sk_X509_CRL_new_null();
if(verified_crls == NULL) {
xmlSecOpenSSLError("sk_X509_CRL_new_null", NULL);
return(NULL);
}
ret = sk_X509_CRL_reserve(verified_crls, num);
if(ret != 1) {
xmlSecOpenSSLError("sk_X509_CRL_reserve", NULL);
sk_X509_CRL_free(verified_crls);
return(NULL);
}
/* verify and dup crls */
for(ii = 0; ii < num; ++ii) {
X509_CRL* crl = sk_X509_CRL_value(crls, ii);
if(crl == NULL) {
continue;
}
ret = xmlSecOpenSSLX509VerifyCRL(xst, xsc, untrusted, crl, keyInfoCtx);
if(ret < 0) {
xmlSecInternalError("xmlSecOpenSSLX509VerifyCRL", NULL);
sk_X509_CRL_free(verified_crls);
return(NULL);
} else if (ret != 1) {
/* crl failed verification */
continue;
}
/* dont duplicate or up_ref the crl since we own
* pointer to it */
ret = sk_X509_CRL_push(verified_crls, crl);
if(ret <= 0) {
xmlSecOpenSSLError("sk_X509_CRL_push", NULL);
sk_X509_CRL_free(verified_crls);
return(NULL);
}
}
/* done! */
return(verified_crls);
}
static int
xmlSecOpenSSLX509StoreVerifyCertAgainstRevoked(X509 * cert, STACK_OF(X509_REVOKED) *revoked_certs, xmlSecKeyInfoCtx* keyInfoCtx) {
X509_REVOKED * revoked_cert;
const ASN1_INTEGER * revoked_cert_serial;
const ASN1_INTEGER * cert_serial;
x509_size_t ii, num;
int ret;
xmlSecAssert2(cert != NULL, -1);
xmlSecAssert2(revoked_certs != NULL, -1);
xmlSecAssert2(keyInfoCtx != NULL, -1);
cert_serial = X509_get_serialNumber(cert);
if(cert_serial == NULL) {
xmlSecOpenSSLError("X509_get_serialNumber(cert)", NULL);
return(-1);
}
num = sk_X509_REVOKED_num(revoked_certs);
for(ii = 0; ii < num; ++ii) {
revoked_cert = sk_X509_REVOKED_value(revoked_certs, ii);
if(revoked_cert == NULL) {
continue;
}
revoked_cert_serial = X509_REVOKED_get0_serialNumber(revoked_cert);
if(revoked_cert_serial == NULL) {
xmlSecOpenSSLError("X509_REVOKED_get0_serialNumber(revoked_cert)", NULL);
return(-1);
}
if (ASN1_INTEGER_cmp(cert_serial, revoked_cert_serial) != 0) {
continue;
}
/* don't bother checking the revocation date if we are checking against
* current time. In this case we assume that CRL didn't come from the future */
if(keyInfoCtx->certsVerificationTime > 0) {
const ASN1_TIME * revocationDate;
time_t tt = keyInfoCtx->certsVerificationTime;
revocationDate = X509_REVOKED_get0_revocationDate(revoked_cert);
if(revocationDate == NULL) {
xmlSecOpenSSLError("X509_REVOKED_get0_revocationDate(revoked_cert)", NULL);
return(-1);
}
ret = X509_cmp_time(revocationDate, &tt);
if (ret == 0) {
xmlSecOpenSSLError("X509_cmp_time(revocationDate)", NULL);
return(-1);
}
/* ret = 1: asn1_time is later than time */
if (ret > 0) {
X509_NAME *issuer;
char issuer_name[256];
time_t ts;
/* revocationDate > certsVerificationTime, we are good */
ret = xmlSecOpenSSLX509Asn1TimeToTime(revocationDate, &ts);
if (ret < 0) {
xmlSecInternalError("xmlSecOpenSSLX509Asn1TimeToTime", NULL);
return(-1);
}
issuer = X509_get_issuer_name(cert);
if(issuer != NULL) {
X509_NAME_oneline(issuer, issuer_name, sizeof(issuer_name));
xmlSecOtherError3(XMLSEC_ERRORS_R_CRL_NOT_YET_VALID, NULL,
"issuer=%s; revocationDate=%lf", issuer_name, (double)ts);
} else {
xmlSecOtherError2(XMLSEC_ERRORS_R_CRL_NOT_YET_VALID, NULL,
"revocationDates=%lf", (double)ts);
}
continue;
}
}
/* cert matches revoked */
return(0);
}
/* success: nomatch */
return(1);
}
/* tries to find the best CRL, returns 1 on success, 0 if crl is not found, or a negative value on error */
static int
xmlSecOpenSSLX509StoreFindBestCrl(X509_NAME *cert_issuer, STACK_OF(X509_CRL) *crls, X509_CRL **res) {
X509_CRL *crl = NULL;
X509_NAME *crl_issuer;
const ASN1_TIME * lastUpdate;
time_t resLastUpdateTime = 0;
x509_size_t ii, num;
int ret;
xmlSecAssert2(cert_issuer != NULL, -1);
xmlSecAssert2(crls != NULL, -1);
xmlSecAssert2(res != NULL, -1);
xmlSecAssert2((*res) == NULL, -1);
num = sk_X509_CRL_num(crls);
for(ii = 0; ii < num; ++ii) {
crl = sk_X509_CRL_value(crls, ii);
if(crl == NULL) {
continue;
}
crl_issuer = X509_CRL_get_issuer(crl);
if(crl_issuer == NULL) {
continue;
}
/* is this CRL from same issuer? */
if(xmlSecOpenSSLX509NamesCompare(crl_issuer, cert_issuer) != 0) {
continue;
}
/* use the latest CRL we have */
lastUpdate = X509_CRL_get0_lastUpdate(crl);
if(lastUpdate == NULL) {
xmlSecOpenSSLError("X509_CRL_get0_lastUpdate", NULL);
return(-1);
}
if((*res) == NULL) {
(*res) = crl;
ret = xmlSecOpenSSLX509Asn1TimeToTime(lastUpdate, &resLastUpdateTime);
if(ret < 0) {
xmlSecInternalError("xmlSecOpenSSLX509Asn1TimeToTime", NULL);
return(-1);
}
continue;
}
/* return -1 if asn1_time is earlier than, or equal to, ts
* and 1 otherwise. These methods return 0 on error.*/
ret = X509_cmp_time(lastUpdate, &resLastUpdateTime);
if(ret == 0) {
xmlSecOpenSSLError("X509_cmp_time(lastUpdate)", NULL);
return(-1);
}
if(ret > 0) {
/* asn1_time is greater than ts (i.e. crl is newer than crl in res)*/
(*res) = crl;
ret = xmlSecOpenSSLX509Asn1TimeToTime(lastUpdate, &resLastUpdateTime);
if(ret < 0) {
xmlSecInternalError("xmlSecOpenSSLX509Asn1TimeToTime", NULL);
return(-1);
}
continue;
}
}
/* did we find anything? */
return((*res) != NULL ? 1 : 0);
}
static int
xmlSecOpenSSLX509StoreVerifyCertAgainstCrls(STACK_OF(X509_CRL) *crls, X509* cert, xmlSecKeyInfoCtx* keyInfoCtx) {
X509_NAME *cert_issuer;
X509_CRL *crl = NULL;
STACK_OF(X509_REVOKED) * revoked_certs;
int ret;
xmlSecAssert2(crls != NULL, -1);
xmlSecAssert2(cert != NULL, -1);
xmlSecAssert2(keyInfoCtx != NULL, -1);
/*
* Try to retrieve a CRL corresponding to the issuer of
* the current certificate
*/
cert_issuer = X509_get_issuer_name(cert);
if(cert_issuer == NULL) {
xmlSecOpenSSLError("X509_get_issuer_name", NULL);
return(-1);
}
ret = xmlSecOpenSSLX509StoreFindBestCrl(cert_issuer, crls, &crl);
if(ret < 0) {
xmlSecInternalError("xmlSecOpenSSLX509StoreFindBestCrl", NULL);
return(-1);
}
/* verify against revoked certs */
if(crl == NULL) {
/* success: verified! */
return(1);
}
revoked_certs = X509_CRL_get_REVOKED(crl);
if(revoked_certs == NULL) {
xmlSecOpenSSLError("X509_CRL_get_REVOKED", NULL);
return(-1);
}
ret = xmlSecOpenSSLX509StoreVerifyCertAgainstRevoked(cert, revoked_certs, keyInfoCtx);
if(ret < 0) {
xmlSecInternalError("xmlSecOpenSSLX509StoreVerifyCertAgainstRevoked", NULL);
return(-1);
} else if(ret != 1) {
char subject[256], issuer[256];
/* cert is revoked, fail */
X509_NAME_oneline(X509_get_subject_name(cert), subject, sizeof(subject));
X509_NAME_oneline(X509_get_issuer_name(cert), issuer, sizeof(issuer));
xmlSecOtherError3(XMLSEC_ERRORS_R_CERT_REVOKED, NULL, "subject=%s; issuer=%s", subject, issuer);
return(0);
}
/* success: verified! */
return(1);
}
static int
xmlSecOpenSSLX509StoreVerifyCertsAgainstCrls(STACK_OF(X509)* chain, STACK_OF(X509_CRL)* crls, xmlSecKeyInfoCtx* keyInfoCtx) {
X509 * cert;
x509_size_t ii, num_certs;
int ret;
xmlSecAssert2(chain != NULL, -1);
xmlSecAssert2(crls != NULL, -1);
xmlSecAssert2(keyInfoCtx != NULL, -1);
/* find all CRLs that apply to each cert */
num_certs = sk_X509_num(chain);
for(ii = 0; ii < num_certs; ++ii) {
cert = sk_X509_value(chain, ii);
if(cert == NULL) {
continue;
}
ret = xmlSecOpenSSLX509StoreVerifyCertAgainstCrls(crls, cert, keyInfoCtx);
if(ret < 0) {
xmlSecInternalError("xmlSecOpenSSLX509StoreVerifyCertAgainstCrls", NULL);
return(-1);
} else if(ret != 1) {
/* cert was revoked */
return(0);
}
}
/* success! */
return(1);
}
static int
xmlSecOpenSSLX509StoreSetCtx(X509_STORE_CTX* xsc, xmlSecKeyInfoCtx* keyInfoCtx) {
X509_VERIFY_PARAM * vpm = NULL;
unsigned long vpm_flags = 0;
xmlSecAssert2(xsc != NULL, -1);
xmlSecAssert2(keyInfoCtx != NULL, -1);
if(keyInfoCtx->certsVerificationTime > 0) {
X509_STORE_CTX_set_time(xsc, 0, keyInfoCtx->certsVerificationTime);
}
/* set verification params: we verify CRLs manually because OpenSSL fails cert verification if there is no CRL */
vpm = X509_VERIFY_PARAM_new();
if(vpm == NULL) {
xmlSecOpenSSLError("X509_VERIFY_PARAM_new", NULL);
return(-1);
}
vpm_flags = X509_VERIFY_PARAM_get_flags(vpm);
vpm_flags &= (~((unsigned long)X509_V_FLAG_CRL_CHECK));
if(keyInfoCtx->certsVerificationTime > 0) {
vpm_flags |= X509_V_FLAG_USE_CHECK_TIME;
X509_VERIFY_PARAM_set_time(vpm, keyInfoCtx->certsVerificationTime);
}
if((keyInfoCtx->flags & XMLSEC_KEYINFO_FLAGS_X509DATA_SKIP_TIME_CHECKS) != 0) {
vpm_flags |= X509_V_FLAG_NO_CHECK_TIME;
}
X509_VERIFY_PARAM_set_flags(vpm, vpm_flags);
X509_VERIFY_PARAM_set_depth(vpm, keyInfoCtx->certsVerificationDepth);
X509_STORE_CTX_set0_param(xsc, vpm);
vpm = NULL; /* owned by xsc now */
/* done */
return(0);
}
static int
xmlSecOpenSSLX509StoreVerifyCert(X509_STORE* xst, X509_STORE_CTX* xsc, X509* cert,
STACK_OF(X509)* untrusted, STACK_OF(X509_CRL)* crls, STACK_OF(X509_CRL)* crls2,
xmlSecKeyInfoCtx* keyInfoCtx
) {
STACK_OF(X509)* chain;
int ret;
int res = -1;
xmlSecAssert2(xst != NULL, -1);
xmlSecAssert2(xsc != NULL, -1);
xmlSecAssert2(cert != NULL, -1);
xmlSecAssert2(keyInfoCtx != NULL, -1);
/* init contenxt and set verification params from keyinfo ctx*/
ret = X509_STORE_CTX_init(xsc, xst, cert, untrusted);
if(ret != 1) {
xmlSecOpenSSLError("X509_STORE_CTX_init", NULL);
goto done;
}
ret = xmlSecOpenSSLX509StoreSetCtx(xsc, keyInfoCtx);
if(ret < 0) {
xmlSecInternalError("xmlSecOpenSSLX509StoreSetCtx", NULL);
goto done;
}
/* verify */
ret = X509_verify_cert(xsc);
if(ret < 0) {
xmlSecOpenSSLError("X509_verify_cert", NULL);
goto done;
} else if(ret != 1) {
X509 * err_cert = NULL;
int err = 0;
/* not verified: get error */
err_cert = X509_STORE_CTX_get_current_cert(xsc);
err = X509_STORE_CTX_get_error(xsc);
if((err != 0) && (err_cert != NULL)) {
const char* err_msg;
char subject[256], issuer[256];
X509_NAME_oneline(X509_get_subject_name(err_cert), subject, sizeof(subject));
X509_NAME_oneline(X509_get_issuer_name(err_cert), issuer, sizeof(issuer));
err_msg = X509_verify_cert_error_string(err);
switch (err) {
case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
xmlSecOtherError5(XMLSEC_ERRORS_R_CERT_ISSUER_FAILED, NULL,
"subject=%s; issuer=%s; err=%d; msg=%s",
subject, issuer, err, xmlSecErrorsSafeString(err_msg));
break;
case X509_V_ERR_CERT_NOT_YET_VALID:
case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
xmlSecOtherError5(XMLSEC_ERRORS_R_CERT_NOT_YET_VALID, NULL,
"subject=%s; issuer=%s; err=%d; msg=%s",
subject, issuer, err, xmlSecErrorsSafeString(err_msg));
break;
case X509_V_ERR_CERT_HAS_EXPIRED:
case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
xmlSecOtherError5(XMLSEC_ERRORS_R_CERT_HAS_EXPIRED, NULL,
"subject=%s; issuer=%s; err=%d; msg=%s",
subject, issuer, err, xmlSecErrorsSafeString(err_msg));
break;
default:
xmlSecOtherError5(XMLSEC_ERRORS_R_CERT_VERIFY_FAILED, NULL,
"subject=%s; issuer=%s; err=%d; msg=%s",
subject, issuer, err, xmlSecErrorsSafeString(err_msg));
break;
}
} else if(err != 0) {
const char* err_msg;
err_msg = X509_verify_cert_error_string(err);
xmlSecOtherError3(XMLSEC_ERRORS_R_CERT_VERIFY_FAILED, NULL,
"err=%d; msg=%s", err, xmlSecErrorsSafeString(err_msg));
} else {
xmlSecOtherError(XMLSEC_ERRORS_R_CERT_VERIFY_FAILED, NULL, "cert verification failed");
}
/* not verified */
res = 0;
goto done;
}
chain = X509_STORE_CTX_get0_chain(xsc);
if(chain == NULL) {
xmlSecOpenSSLError("X509_STORE_CTX_get0_chain(crls)", NULL);
goto done;
}
/* now check against crls */
if(crls != NULL) {
ret = xmlSecOpenSSLX509StoreVerifyCertsAgainstCrls(chain, crls, keyInfoCtx);
if(ret < 0) {
xmlSecInternalError("xmlSecOpenSSLX509StoreVerifyCertsAgainstCrls(crls)", NULL);
goto done;
} else if(ret != 1) {
/* not verified */
res = 0;
goto done;
}
}
if(crls2 != NULL) {
ret = xmlSecOpenSSLX509StoreVerifyCertsAgainstCrls(chain, crls2, keyInfoCtx);
if(ret < 0) {
xmlSecInternalError("xmlSecOpenSSLX509StoreVerifyCertsAgainstCrls(crls2)", NULL);
goto done;
} else if(ret != 1) {
/* not verified */
res = 0;
goto done;
}
}
/* success: verified */
res = 1;
done:
X509_STORE_CTX_cleanup(xsc);
return(res);
}
/**
* xmlSecOpenSSLX509StoreVerify:
* @store: the pointer to X509 key data store klass.
* @certs: the untrusted certificates stack.
* @crls: the crls stack.
* @keyInfoCtx: the pointer to <dsig:KeyInfo/> element processing context.
*
* Verifies @certs list.
*
* Returns: pointer to the first verified certificate from @certs.
*/
X509*
xmlSecOpenSSLX509StoreVerify(xmlSecKeyDataStorePtr store, XMLSEC_STACK_OF_X509* certs, XMLSEC_STACK_OF_X509_CRL* crls, xmlSecKeyInfoCtx* keyInfoCtx) {
xmlSecOpenSSLX509StoreCtxPtr ctx;
STACK_OF(X509)* all_untrusted_certs = NULL;
STACK_OF(X509_CRL)* verified_crls = NULL;
X509 * res = NULL;
X509 * cert;
X509_STORE_CTX *xsc = NULL;
x509_size_t ii, num;
int ret;
xmlSecAssert2(xmlSecKeyDataStoreCheckId(store, xmlSecOpenSSLX509StoreId), NULL);
xmlSecAssert2(certs != NULL, NULL);
xmlSecAssert2(keyInfoCtx != NULL, NULL);
ctx = xmlSecOpenSSLX509StoreGetCtx(store);
xmlSecAssert2(ctx != NULL, NULL);
xmlSecAssert2(ctx->xst != NULL, NULL);
/* reuse xsc for both crls and certs verification */
xsc = X509_STORE_CTX_new_ex(xmlSecOpenSSLGetLibCtx(), NULL);
if(xsc == NULL) {
xmlSecOpenSSLError("X509_STORE_CTX_new", xmlSecKeyDataStoreGetName(store));
goto done;
}
/* create a combined list of all untrusted certs*/
all_untrusted_certs = xmlSecOpenSSLX509StoreCombineCerts(certs, ctx->untrusted);
if(all_untrusted_certs == NULL) {
xmlSecInternalError("xmlSecOpenSSLX509StoreCombineCerts", NULL);
goto done;
}
/* copy crls list but remove all non-verified (we assume that CRLs in the store are already verified) */
verified_crls = xmlSecOpenSSLX509StoreVerifyAndCopyCrls(ctx->xst, xsc, all_untrusted_certs, crls, keyInfoCtx);
/* get one cert after another and try to verify */
num = sk_X509_num(certs);
for(ii = 0; ii < num; ++ii) {
cert = sk_X509_value(certs, ii);
if(cert == NULL) {
continue;
}
/* we only attempt to verify "leaf" certs without children */
if((all_untrusted_certs != NULL) && (xmlSecOpenSSLX509FindChildCert(all_untrusted_certs, cert) != NULL)) {
continue;
}
/* do we even need to verify the leaf cert? */
if((keyInfoCtx->flags & XMLSEC_KEYINFO_FLAGS_X509DATA_DONT_VERIFY_CERTS) != 0) {
res = cert;
goto done;
}
ret = xmlSecOpenSSLX509StoreVerifyCert(ctx->xst, xsc, cert, all_untrusted_certs, verified_crls, ctx->crls, keyInfoCtx);
if(ret < 0) {
xmlSecInternalError("xmlSecOpenSSLX509StoreVerifyCert", xmlSecKeyDataStoreGetName(store));
goto done;
} else if(ret != 1) {
continue;
}
/* success! */
res = cert;
break;
}
done:
/* only free sk_* structures, not the certs or crls because caller owns pointers
* or the store does and we didn't up_ref / dup certs when creating the sk_*'s.
*/
if(all_untrusted_certs != NULL) {
sk_X509_free(all_untrusted_certs);
}
if(verified_crls != NULL) {
sk_X509_CRL_free(verified_crls);
}
if(xsc != NULL) {
X509_STORE_CTX_free(xsc);
}
return(res);
}
/**
* xmlSecOpenSSLX509StoreVerifyKey:
* @store: the pointer to X509 key data store klass.
* @key: the pointer to key.
* @keyInfoCtx: the key info context for verification.
*
* Verifies @key with the keys manager @mngr created with #xmlSecCryptoAppDefaultKeysMngrInit
* function:
* - Checks that key certificate is present
* - Checks that key certificate is valid
*
* Adds @key to the keys manager @mngr created with #xmlSecCryptoAppDefaultKeysMngrInit
* function.
*
* Returns: 1 if key is verified, 0 otherwise, or a negative value if an error occurs.
*/
int
xmlSecOpenSSLX509StoreVerifyKey(xmlSecKeyDataStorePtr store, xmlSecKeyPtr key, xmlSecKeyInfoCtxPtr keyInfoCtx) {
xmlSecOpenSSLX509StoreCtxPtr ctx;
xmlSecKeyDataPtr x509Data;
X509* keyCert;
STACK_OF(X509)* certs;
STACK_OF(X509_CRL)* crls;
X509_STORE_CTX *xsc = NULL;
STACK_OF(X509)* all_untrusted_certs = NULL;
STACK_OF(X509_CRL)* verified_crls = NULL;
int ret;
int res = -1;
xmlSecAssert2(xmlSecKeyDataStoreCheckId(store, xmlSecOpenSSLX509StoreId), -1);
xmlSecAssert2(key != NULL, -1);
xmlSecAssert2(keyInfoCtx != NULL, -1);
ctx = xmlSecOpenSSLX509StoreGetCtx(store);
xmlSecAssert2(ctx != NULL, -1);
xmlSecAssert2(ctx->xst != NULL, -1);
/* retrieve X509 data and get key cert */
x509Data = xmlSecKeyGetData(key, xmlSecOpenSSLKeyDataX509Id);
if(x509Data == NULL) {
xmlSecInternalError("xmlSecKeyGetData(xmlSecOpenSSLKeyDataX509Id)", xmlSecKeyDataStoreGetName(store));
return(-1);
}
keyCert = xmlSecOpenSSLKeyDataX509GetKeyCert(x509Data);
if(keyCert == NULL) {
xmlSecInternalError("key certificate is required", xmlSecKeyDataStoreGetName(store));
res = 0; /* verification failed */
goto done;
}
/* do we even need to verify the key cert? */
if((keyInfoCtx->flags & XMLSEC_KEYINFO_FLAGS_X509DATA_DONT_VERIFY_CERTS) != 0) {
res = 1;
goto done;
}
certs = xmlSecOpenSSLKeyDataX509GetCerts(x509Data);
crls = xmlSecOpenSSLKeyDataX509GetCrls(x509Data);
/* reuse xsc for both crls and certs verification */
xsc = X509_STORE_CTX_new_ex(xmlSecOpenSSLGetLibCtx(), NULL);
if(xsc == NULL) {
xmlSecOpenSSLError("X509_STORE_CTX_new", xmlSecKeyDataStoreGetName(store));
goto done;
}
/* create a combined list of all untrusted certs*/
all_untrusted_certs = xmlSecOpenSSLX509StoreCombineCerts(certs, ctx->untrusted);
if(all_untrusted_certs == NULL) {
xmlSecInternalError("xmlSecOpenSSLX509StoreCombineCerts", xmlSecKeyDataStoreGetName(store));
goto done;
}
/* copy crls list but remove all non-verified (we assume that CRLs in the store are already verified) */
verified_crls = xmlSecOpenSSLX509StoreVerifyAndCopyCrls(ctx->xst, xsc, all_untrusted_certs, crls, keyInfoCtx);
/* verify */
ret = xmlSecOpenSSLX509StoreVerifyCert(ctx->xst, xsc, keyCert, all_untrusted_certs, verified_crls, ctx->crls, keyInfoCtx);
if(ret < 0) {
xmlSecInternalError("xmlSecOpenSSLX509StoreVerifyCert", xmlSecKeyDataStoreGetName(store));
goto done;
} else if(ret != 1) {
res = 0; /* verification failed */
goto done;
}
/* success! */
res = 1;
done:
/* only free sk_* structures, not the certs or crls because caller owns pointers