/src/xmlsec/src/openssl/x509vfy.c
Line | Count | Source |
1 | | /** |
2 | | * XML Security Library (http://www.aleksey.com/xmlsec). |
3 | | * |
4 | | * This is free software; see the Copyright file in the source distribution for precise wording. |
5 | | * |
6 | | * Copyright (C) 2002-2026 Aleksey Sanin <aleksey@aleksey.com>. All Rights Reserved. |
7 | | */ |
8 | | /** |
9 | | * @addtogroup xmlsec_openssl_x509 |
10 | | * @brief X509 certificates verification support functions for OpenSSL. |
11 | | */ |
12 | | #include "globals.h" |
13 | | |
14 | | #include <stdlib.h> |
15 | | #include <stdio.h> |
16 | | #include <string.h> |
17 | | #include <ctype.h> |
18 | | #include <errno.h> |
19 | | |
20 | | #include <xmlsec/xmlsec.h> |
21 | | #include <xmlsec/keys.h> |
22 | | #include <xmlsec/keyinfo.h> |
23 | | #include <xmlsec/keysmngr.h> |
24 | | #include <xmlsec/base64.h> |
25 | | #include <xmlsec/errors.h> |
26 | | #include <xmlsec/private.h> |
27 | | #include <xmlsec/xmltree.h> |
28 | | |
29 | | #include <xmlsec/openssl/crypto.h> |
30 | | #include <xmlsec/openssl/evp.h> |
31 | | #include <xmlsec/openssl/x509.h> |
32 | | |
33 | | #include <openssl/evp.h> |
34 | | #include <openssl/x509.h> |
35 | | #include <openssl/x509_vfy.h> |
36 | | #include <openssl/x509v3.h> |
37 | | |
38 | | #include "../cast_helpers.h" |
39 | | #include "../x509_helpers.h" |
40 | | #include "openssl_compat.h" |
41 | | #include "private.h" |
42 | | |
43 | | #ifndef XMLSEC_NO_X509 |
44 | | |
45 | | /****************************************************************************** |
46 | | * |
47 | | * Internal OpenSSL X509 store CTX |
48 | | * |
49 | | *****************************************************************************/ |
50 | | typedef struct _xmlSecOpenSSLX509StoreCtx xmlSecOpenSSLX509StoreCtx, |
51 | | *xmlSecOpenSSLX509StoreCtxPtr; |
52 | | struct _xmlSecOpenSSLX509StoreCtx { |
53 | | X509_STORE* xst; |
54 | | STACK_OF(X509)* untrusted; |
55 | | STACK_OF(X509_CRL)* crls; |
56 | | X509_VERIFY_PARAM * vpm; |
57 | | }; |
58 | | |
59 | | /****************************************************************************** |
60 | | * |
61 | | * xmlSecOpenSSLKeyDataStoreX509Id: |
62 | | * |
63 | | *****************************************************************************/ |
64 | 0 | XMLSEC_KEY_DATA_STORE_DECLARE(OpenSSLX509Store, xmlSecOpenSSLX509StoreCtx) |
65 | 0 | #define xmlSecOpenSSLX509StoreSize XMLSEC_KEY_DATA_STORE_SIZE(OpenSSLX509Store) |
66 | 0 |
|
67 | 0 | static int xmlSecOpenSSLX509StoreInitialize (xmlSecKeyDataStorePtr store); |
68 | 0 | static void xmlSecOpenSSLX509StoreFinalize (xmlSecKeyDataStorePtr store); |
69 | 0 |
|
70 | 0 | static xmlSecKeyDataStoreKlass xmlSecOpenSSLX509StoreKlass = { |
71 | 0 | sizeof(xmlSecKeyDataStoreKlass), |
72 | 0 | xmlSecOpenSSLX509StoreSize, |
73 | 0 |
|
74 | 0 | /* data */ |
75 | 0 | xmlSecNameX509Store, /* const xmlChar* name; */ |
76 | 0 |
|
77 | 0 | /* constructors/destructor */ |
78 | 0 | xmlSecOpenSSLX509StoreInitialize, /* xmlSecKeyDataStoreInitializeMethod initialize; */ |
79 | 0 | xmlSecOpenSSLX509StoreFinalize, /* xmlSecKeyDataStoreFinalizeMethod finalize; */ |
80 | 0 |
|
81 | 0 | /* reserved for the future */ |
82 | 0 | NULL, /* void* reserved0; */ |
83 | 0 | NULL, /* void* reserved1; */ |
84 | 0 | }; |
85 | 0 |
|
86 | 0 | static int xmlSecOpenSSLX509VerifyCRLTimeValidity (X509_CRL *crl, |
87 | 0 | xmlSecKeyInfoCtx* keyInfoCtx); |
88 | 0 | static int xmlSecOpenSSLX509VerifyCRL (X509_STORE* xst, |
89 | 0 | X509_STORE_CTX* xsc, |
90 | 0 | STACK_OF(X509)* untrusted, |
91 | 0 | X509_CRL *crl, |
92 | 0 | xmlSecKeyInfoCtx* keyInfoCtx); |
93 | 0 | static X509* xmlSecOpenSSLX509FindChildCert (STACK_OF(X509) *chain, |
94 | 0 | X509 *cert); |
95 | 0 | static X509_NAME* xmlSecOpenSSLX509NameRead (const xmlChar *str); |
96 | 0 |
|
97 | 0 | static int xmlSecOpenSSLX509NamesCompare (XMLSEC_OPENSSL400_CONST X509_NAME *a, |
98 | 0 | XMLSEC_OPENSSL400_CONST X509_NAME *b); |
99 | 0 | static STACK_OF(X509_NAME_ENTRY)* xmlSecOpenSSLX509_NAME_ENTRIES_copy (XMLSEC_OPENSSL400_CONST X509_NAME *a); |
100 | 0 | static int xmlSecOpenSSLX509_NAME_ENTRIES_cmp (STACK_OF(X509_NAME_ENTRY) * a, |
101 | 0 | STACK_OF(X509_NAME_ENTRY) * b); |
102 | 0 | static int xmlSecOpenSSLX509_NAME_ENTRY_cmp (const X509_NAME_ENTRY * const *a, |
103 | 0 | const X509_NAME_ENTRY * const *b); |
104 | 0 |
|
105 | 0 | static STACK_OF(X509)* xmlSecOpenSSLX509StoreCombineCerts (STACK_OF(X509)* certs1, |
106 | 0 | STACK_OF(X509)* certs2); |
107 | 0 | /** |
108 | 0 | * @brief The OpenSSL X509 certificates store klass. |
109 | 0 | * @details The OpenSSL X509 certificates key data store klass. |
110 | 0 | * @return pointer to OpenSSL X509 certificates key data store klass. |
111 | 0 | */ |
112 | 0 | xmlSecKeyDataStoreId |
113 | 0 | xmlSecOpenSSLX509StoreGetKlass(void) { |
114 | 0 | return(&xmlSecOpenSSLX509StoreKlass); |
115 | 0 | } |
116 | | |
117 | | /** |
118 | | * @brief Deprecated. Searches @p store for a certificate that matches given criteria. |
119 | | * @param store the pointer to X509 key data store klass. |
120 | | * @param subjectName the desired certificate name. |
121 | | * @param issuerName the desired certificate issuer name. |
122 | | * @param issuerSerial the desired certificate issuer serial number. |
123 | | * @param ski the desired certificate SKI. |
124 | | * @param keyInfoCtx the pointer to <dsig:KeyInfo/> element processing context. |
125 | | * |
126 | | * |
127 | | * @return pointer to found certificate or NULL if certificate is not found |
128 | | * or an error occurs. |
129 | | */ |
130 | | X509* |
131 | | xmlSecOpenSSLX509StoreFindCert(xmlSecKeyDataStorePtr store, xmlChar *subjectName, |
132 | | xmlChar *issuerName, xmlChar *issuerSerial, |
133 | | xmlChar *ski, xmlSecKeyInfoCtx* keyInfoCtx |
134 | 0 | ) { |
135 | 0 | if(ski != NULL) { |
136 | 0 | xmlSecSize skiDecodedSize = 0; |
137 | 0 | int ret; |
138 | | |
139 | | /* our usual trick with base64 decode */ |
140 | 0 | ret = xmlSecBase64DecodeInPlace(ski, &skiDecodedSize); |
141 | 0 | if(ret < 0) { |
142 | 0 | xmlSecInternalError2("xmlSecBase64DecodeInPlace", NULL, |
143 | 0 | "ski=%s", xmlSecErrorsSafeString(ski)); |
144 | 0 | return(NULL); |
145 | 0 | } |
146 | | |
147 | 0 | return(xmlSecOpenSSLX509StoreFindCert_ex(store, subjectName, issuerName, issuerSerial, |
148 | 0 | (xmlSecByte*)ski, skiDecodedSize, keyInfoCtx)); |
149 | 0 | } else { |
150 | 0 | return(xmlSecOpenSSLX509StoreFindCert_ex(store, subjectName, issuerName, issuerSerial, |
151 | 0 | NULL, 0, keyInfoCtx)); |
152 | 0 | } |
153 | 0 | } |
154 | | |
155 | | /** |
156 | | * @brief Deprecated. Searches @p store for a certificate that matches given criteria. |
157 | | * @param store the pointer to X509 key data store klass. |
158 | | * @param subjectName the desired certificate name. |
159 | | * @param issuerName the desired certificate issuer name. |
160 | | * @param issuerSerial the desired certificate issuer serial number. |
161 | | * @param ski the desired certificate SKI. |
162 | | * @param skiSize the desired certificate SKI size. |
163 | | * @param keyInfoCtx the pointer to <dsig:KeyInfo/> element processing context. |
164 | | * |
165 | | * |
166 | | * @return pointer to found certificate or NULL if certificate is not found |
167 | | * or an error occurs. |
168 | | */ |
169 | | X509* |
170 | | xmlSecOpenSSLX509StoreFindCert_ex(xmlSecKeyDataStorePtr store, |
171 | | xmlChar *subjectName, |
172 | | xmlChar *issuerName, xmlChar *issuerSerial, |
173 | | xmlSecByte * ski, xmlSecSize skiSize, |
174 | | xmlSecKeyInfoCtx* keyInfoCtx XMLSEC_ATTRIBUTE_UNUSED |
175 | 0 | ) { |
176 | 0 | xmlSecOpenSSLX509StoreCtxPtr ctx; |
177 | 0 | xmlSecOpenSSLX509FindCertCtx findCertCtx; |
178 | 0 | xmlSecOpenSSLSizeT ii; |
179 | 0 | int ret; |
180 | 0 | X509* res = NULL; |
181 | |
|
182 | 0 | xmlSecAssert2(xmlSecKeyDataStoreCheckId(store, xmlSecOpenSSLX509StoreId), NULL); |
183 | 0 | UNREFERENCED_PARAMETER(keyInfoCtx); |
184 | |
|
185 | 0 | ctx = xmlSecOpenSSLX509StoreGetCtx(store); |
186 | 0 | xmlSecAssert2(ctx != NULL, NULL); |
187 | | |
188 | | /* do we have any certs at all? */ |
189 | 0 | if(ctx->untrusted == NULL) { |
190 | 0 | return(NULL); |
191 | 0 | } |
192 | 0 | ret = xmlSecOpenSSLX509FindCertCtxInitialize(&findCertCtx, |
193 | 0 | subjectName, |
194 | 0 | issuerName, issuerSerial, |
195 | 0 | ski, skiSize); |
196 | 0 | if(ret < 0) { |
197 | 0 | xmlSecInternalError("xmlSecOpenSSLX509FindCertCtxInitialize", NULL); |
198 | 0 | xmlSecOpenSSLX509FindCertCtxFinalize(&findCertCtx); |
199 | 0 | return(NULL); |
200 | 0 | } |
201 | 0 | for(ii = 0; ii < sk_X509_num(ctx->untrusted); ++ii) { |
202 | 0 | X509 * cert = sk_X509_value(ctx->untrusted, ii); |
203 | 0 | if(cert == NULL) { |
204 | 0 | continue; |
205 | 0 | } |
206 | | |
207 | 0 | ret = xmlSecOpenSSLX509FindCertCtxMatch(&findCertCtx, cert); |
208 | 0 | if(ret < 0) { |
209 | 0 | xmlSecInternalError("xmlSecOpenSSLX509FindCertCtxMatch", NULL); |
210 | 0 | xmlSecOpenSSLX509FindCertCtxFinalize(&findCertCtx); |
211 | 0 | return(NULL); |
212 | 0 | } else if(ret == 1) { |
213 | 0 | res = cert; |
214 | 0 | break; |
215 | 0 | } |
216 | 0 | } |
217 | | |
218 | | /* done */ |
219 | 0 | xmlSecOpenSSLX509FindCertCtxFinalize(&findCertCtx); |
220 | 0 | return(res); |
221 | 0 | } |
222 | | |
223 | | X509* |
224 | 0 | xmlSecOpenSSLX509StoreFindCertByValue(xmlSecKeyDataStorePtr store, xmlSecKeyX509DataValuePtr x509Value) { |
225 | 0 | xmlSecOpenSSLX509StoreCtxPtr ctx; |
226 | 0 | xmlSecOpenSSLX509FindCertCtx findCertCtx; |
227 | 0 | xmlSecOpenSSLSizeT ii; |
228 | 0 | int ret; |
229 | 0 | X509* res = NULL; |
230 | |
|
231 | 0 | xmlSecAssert2(store != NULL, NULL); |
232 | 0 | xmlSecAssert2(xmlSecKeyDataStoreCheckId(store, xmlSecOpenSSLX509StoreId), NULL); |
233 | 0 | xmlSecAssert2(x509Value != NULL, NULL); |
234 | |
|
235 | 0 | ctx = xmlSecOpenSSLX509StoreGetCtx(store); |
236 | 0 | xmlSecAssert2(ctx != NULL, NULL); |
237 | | |
238 | | /* do we have any certs at all? */ |
239 | 0 | if(ctx->untrusted == NULL) { |
240 | 0 | return(NULL); |
241 | 0 | } |
242 | 0 | ret = xmlSecOpenSSLX509FindCertCtxInitializeFromValue(&findCertCtx, x509Value); |
243 | 0 | if(ret < 0) { |
244 | 0 | xmlSecInternalError("xmlSecOpenSSLX509FindCertCtxInitializeFromValue", NULL); |
245 | 0 | xmlSecOpenSSLX509FindCertCtxFinalize(&findCertCtx); |
246 | 0 | return(NULL); |
247 | 0 | } |
248 | 0 | for(ii = 0; ii < sk_X509_num(ctx->untrusted); ++ii) { |
249 | 0 | X509 * cert = sk_X509_value(ctx->untrusted, ii); |
250 | 0 | if(cert == NULL) { |
251 | 0 | continue; |
252 | 0 | } |
253 | | |
254 | 0 | ret = xmlSecOpenSSLX509FindCertCtxMatch(&findCertCtx, cert); |
255 | 0 | if(ret < 0) { |
256 | 0 | xmlSecInternalError("xmlSecOpenSSLX509FindCertCtxMatch", NULL); |
257 | 0 | xmlSecOpenSSLX509FindCertCtxFinalize(&findCertCtx); |
258 | 0 | return(NULL); |
259 | 0 | } else if(ret == 1) { |
260 | 0 | res = cert; |
261 | 0 | break; |
262 | 0 | } |
263 | 0 | } |
264 | | |
265 | | /* done */ |
266 | 0 | xmlSecOpenSSLX509FindCertCtxFinalize(&findCertCtx); |
267 | 0 | return(res); |
268 | 0 | } |
269 | | |
270 | | xmlSecKeyPtr |
271 | 0 | xmlSecOpenSSLX509FindKeyByValue(xmlSecPtrListPtr keysList, xmlSecKeyX509DataValuePtr x509Value) { |
272 | 0 | xmlSecOpenSSLX509FindCertCtx findCertCtx; |
273 | 0 | xmlSecSize keysListSize, ii; |
274 | 0 | xmlSecKeyPtr res = NULL; |
275 | 0 | int ret; |
276 | |
|
277 | 0 | xmlSecAssert2(keysList != NULL, NULL); |
278 | 0 | xmlSecAssert2(x509Value != NULL, NULL); |
279 | |
|
280 | 0 | ret = xmlSecOpenSSLX509FindCertCtxInitializeFromValue(&findCertCtx, x509Value); |
281 | 0 | if(ret < 0) { |
282 | 0 | xmlSecInternalError("xmlSecOpenSSLX509FindCertCtxInitializeFromValue", NULL); |
283 | 0 | xmlSecOpenSSLX509FindCertCtxFinalize(&findCertCtx); |
284 | 0 | return(NULL); |
285 | 0 | } |
286 | | |
287 | 0 | keysListSize = xmlSecPtrListGetSize(keysList); |
288 | 0 | for(ii = 0; ii < keysListSize; ++ii) { |
289 | 0 | xmlSecKeyPtr key; |
290 | 0 | xmlSecKeyDataPtr keyData; |
291 | 0 | X509* keyCert; |
292 | | |
293 | | /* get key's cert from x509 key data */ |
294 | 0 | key = (xmlSecKeyPtr)xmlSecPtrListGetItem(keysList, ii); |
295 | 0 | if(key == NULL) { |
296 | 0 | continue; |
297 | 0 | } |
298 | 0 | keyData = xmlSecKeyGetData(key, xmlSecOpenSSLKeyDataX509Id); |
299 | 0 | if(keyData == NULL) { |
300 | 0 | continue; |
301 | 0 | } |
302 | 0 | keyCert = xmlSecOpenSSLKeyDataX509GetKeyCert(keyData); |
303 | 0 | if(keyCert == NULL) { |
304 | 0 | continue; |
305 | 0 | } |
306 | | |
307 | | /* does it match? */ |
308 | 0 | ret = xmlSecOpenSSLX509FindCertCtxMatch(&findCertCtx, keyCert); |
309 | 0 | if(ret < 0) { |
310 | 0 | xmlSecInternalError("xmlSecOpenSSLX509FindCertCtxMatch", NULL); |
311 | 0 | xmlSecOpenSSLX509FindCertCtxFinalize(&findCertCtx); |
312 | 0 | return(NULL); |
313 | 0 | } else if(ret == 1) { |
314 | 0 | res = key; |
315 | 0 | break; |
316 | 0 | } |
317 | 0 | } |
318 | | |
319 | | /* done */ |
320 | 0 | xmlSecOpenSSLX509FindCertCtxFinalize(&findCertCtx); |
321 | 0 | return(res); |
322 | 0 | } |
323 | | |
324 | | |
325 | | static int |
326 | | xmlSecOpenSSLX509StoreVerifyAndCopyCrls(X509_STORE* xst, X509_STORE_CTX* xsc, STACK_OF(X509)* untrusted, STACK_OF(X509_CRL)* crls, |
327 | | xmlSecKeyInfoCtx* keyInfoCtx, STACK_OF(X509_CRL)** out_crls |
328 | 0 | ) { |
329 | 0 | STACK_OF(X509_CRL)* verified_crls = NULL; |
330 | 0 | xmlSecOpenSSLSizeT ii, num, num2; |
331 | 0 | int ret; |
332 | |
|
333 | 0 | xmlSecAssert2(xst != NULL, -1); |
334 | 0 | xmlSecAssert2(xsc != NULL, -1); |
335 | 0 | xmlSecAssert2(keyInfoCtx != NULL, -1); |
336 | 0 | xmlSecAssert2(out_crls != NULL, -1); |
337 | |
|
338 | 0 | (*out_crls) = NULL; |
339 | | |
340 | | /* check if we have anything to copy */ |
341 | 0 | if(crls == NULL) { |
342 | 0 | return(0); |
343 | 0 | } |
344 | 0 | num = sk_X509_CRL_num(crls); |
345 | 0 | if(num <= 0) { |
346 | 0 | return(0); |
347 | 0 | } |
348 | | |
349 | | /* create output crls list */ |
350 | 0 | verified_crls = sk_X509_CRL_new_null(); |
351 | 0 | if(verified_crls == NULL) { |
352 | 0 | xmlSecOpenSSLError("sk_X509_CRL_new_null", NULL); |
353 | 0 | return(-1); |
354 | 0 | } |
355 | 0 | ret = sk_X509_CRL_reserve(verified_crls, num); |
356 | 0 | if(ret != 1) { |
357 | 0 | xmlSecOpenSSLError("sk_X509_CRL_reserve", NULL); |
358 | 0 | sk_X509_CRL_free(verified_crls); |
359 | 0 | return(-1); |
360 | 0 | } |
361 | | |
362 | | /* verify and dup crls */ |
363 | 0 | for(ii = 0; ii < num; ++ii) { |
364 | 0 | X509_CRL* crl = sk_X509_CRL_value(crls, ii); |
365 | 0 | if(crl == NULL) { |
366 | 0 | continue; |
367 | 0 | } |
368 | | |
369 | 0 | ret = xmlSecOpenSSLX509VerifyCRL(xst, xsc, untrusted, crl, keyInfoCtx); |
370 | 0 | if(ret < 0) { |
371 | 0 | xmlSecInternalError("xmlSecOpenSSLX509VerifyCRL", NULL); |
372 | 0 | sk_X509_CRL_free(verified_crls); |
373 | 0 | return(-1); |
374 | 0 | } else if (ret != 1) { |
375 | | /* crl failed verification */ |
376 | 0 | continue; |
377 | 0 | } |
378 | | /* dont duplicate or up_ref the crl since we own |
379 | | * pointer to it */ |
380 | 0 | num2 = sk_X509_CRL_push(verified_crls, crl); |
381 | 0 | if(num2 <= 0) { |
382 | 0 | xmlSecOpenSSLError("sk_X509_CRL_push", NULL); |
383 | 0 | sk_X509_CRL_free(verified_crls); |
384 | 0 | return(-1); |
385 | 0 | } |
386 | 0 | } |
387 | | |
388 | | /* done! */ |
389 | 0 | (*out_crls) = verified_crls; |
390 | 0 | return(0); |
391 | 0 | } |
392 | | |
393 | | |
394 | | /* X509_cmp_time is deprecated in OpenSSL 4.0.0 */ |
395 | | #if defined(XMLSEC_OPENSSL_API_400) |
396 | | /* ASN1_TIME_cmp_time_t() and ASN1_UTCTIME_cmp_time_t() return -1 if s is before t, |
397 | | 0 if s equals t, or 1 if s is after t. -2 is returned on error */ |
398 | | #define xmlSecOpenSSLAsn1TimeCmp(a, b) ASN1_TIME_cmp_time_t((a), *(b)) |
399 | | #else /* defined(XMLSEC_OPENSSL_API_400) */ |
400 | | /* X509_cmp_time() and X509_cmp_current_time() return -1 if asn1_time is earlier than, |
401 | | or equal to, in_tm (resp. current time), and 1 otherwise. These methods return 0 |
402 | | on error. */ |
403 | 0 | #define xmlSecOpenSSLAsn1TimeCmp(a, b) X509_cmp_time((a), (b)) |
404 | | #endif /* defined(XMLSEC_OPENSSL_API_400) */ |
405 | | |
406 | | static int |
407 | 0 | xmlSecOpenSSLX509StoreVerifyCertAgainstRevoked(X509 * cert, STACK_OF(X509_REVOKED) *revoked_certs, xmlSecKeyInfoCtx* keyInfoCtx) { |
408 | 0 | X509_REVOKED * revoked_cert; |
409 | 0 | const ASN1_INTEGER * revoked_cert_serial; |
410 | 0 | const ASN1_INTEGER * cert_serial; |
411 | 0 | xmlSecOpenSSLSizeT ii, num; |
412 | 0 | int ret; |
413 | |
|
414 | 0 | xmlSecAssert2(cert != NULL, -1); |
415 | 0 | xmlSecAssert2(revoked_certs != NULL, -1); |
416 | 0 | xmlSecAssert2(keyInfoCtx != NULL, -1); |
417 | |
|
418 | 0 | cert_serial = X509_get_serialNumber(cert); |
419 | 0 | if(cert_serial == NULL) { |
420 | 0 | xmlSecOpenSSLError("X509_get_serialNumber(cert)", NULL); |
421 | 0 | return(-1); |
422 | 0 | } |
423 | | |
424 | 0 | num = sk_X509_REVOKED_num(revoked_certs); |
425 | 0 | for(ii = 0; ii < num; ++ii) { |
426 | 0 | revoked_cert = sk_X509_REVOKED_value(revoked_certs, ii); |
427 | 0 | if(revoked_cert == NULL) { |
428 | 0 | continue; |
429 | 0 | } |
430 | | |
431 | 0 | revoked_cert_serial = X509_REVOKED_get0_serialNumber(revoked_cert); |
432 | 0 | if(revoked_cert_serial == NULL) { |
433 | 0 | xmlSecOpenSSLError("X509_REVOKED_get0_serialNumber(revoked_cert)", NULL); |
434 | 0 | return(-1); |
435 | 0 | } |
436 | | |
437 | 0 | if (ASN1_INTEGER_cmp(cert_serial, revoked_cert_serial) != 0) { |
438 | 0 | continue; |
439 | 0 | } |
440 | | |
441 | | /* don't bother checking the revocation date if we are checking against |
442 | | * current time. In this case we assume that CRL didn't come from the future */ |
443 | 0 | if(keyInfoCtx->certsVerificationTime > 0) { |
444 | 0 | const ASN1_TIME * revocationDate; |
445 | 0 | time_t tt = keyInfoCtx->certsVerificationTime; |
446 | |
|
447 | 0 | revocationDate = X509_REVOKED_get0_revocationDate(revoked_cert); |
448 | 0 | if(revocationDate == NULL) { |
449 | 0 | xmlSecOpenSSLError("X509_REVOKED_get0_revocationDate(revoked_cert)", NULL); |
450 | 0 | return(-1); |
451 | 0 | } |
452 | 0 | ret = xmlSecOpenSSLAsn1TimeCmp(revocationDate, &tt); |
453 | 0 | if (ret == 0) { |
454 | 0 | xmlSecOpenSSLError("X509_cmp_time(revocationDate)", NULL); |
455 | 0 | return(-1); |
456 | 0 | } |
457 | | /* ret = 1: asn1_time is later than time */ |
458 | 0 | if (ret > 0) { |
459 | 0 | XMLSEC_OPENSSL400_CONST X509_NAME *issuer; |
460 | 0 | char issuer_name[256]; |
461 | 0 | time_t ts; |
462 | | |
463 | | /* revocationDate > certsVerificationTime, we are good */ |
464 | 0 | ret = xmlSecOpenSSLX509Asn1TimeToTime(revocationDate, &ts); |
465 | 0 | if (ret < 0) { |
466 | 0 | xmlSecInternalError("xmlSecOpenSSLX509Asn1TimeToTime", NULL); |
467 | 0 | return(-1); |
468 | 0 | } |
469 | 0 | issuer = X509_get_issuer_name(cert); |
470 | 0 | if(issuer != NULL) { |
471 | 0 | xmlSecOpenSSLX509NameToString(issuer, issuer_name, sizeof(issuer_name)); |
472 | 0 | xmlSecOtherError3(XMLSEC_ERRORS_R_CRL_NOT_YET_VALID, NULL, |
473 | 0 | "issuer=%s; revocationDate=%lf", issuer_name, (double)ts); |
474 | 0 | } else { |
475 | 0 | xmlSecOtherError2(XMLSEC_ERRORS_R_CRL_NOT_YET_VALID, NULL, |
476 | 0 | "revocationDates=%lf", (double)ts); |
477 | 0 | } |
478 | 0 | continue; |
479 | 0 | } |
480 | 0 | } |
481 | | |
482 | | /* cert matches revoked */ |
483 | 0 | return(0); |
484 | 0 | } |
485 | | |
486 | | /* success: nomatch */ |
487 | 0 | return(1); |
488 | 0 | } |
489 | | |
490 | | /* tries to find the best CRL, returns 1 on success, 0 if crl is not found, or a negative value on error */ |
491 | | static int |
492 | 0 | xmlSecOpenSSLX509StoreFindBestCrl(XMLSEC_OPENSSL400_CONST X509_NAME *cert_issuer, STACK_OF(X509_CRL) *crls, X509_CRL **res) { |
493 | 0 | X509_CRL *crl = NULL; |
494 | 0 | XMLSEC_OPENSSL400_CONST X509_NAME *crl_issuer; |
495 | 0 | const ASN1_TIME * lastUpdate; |
496 | 0 | time_t resLastUpdateTime = 0; |
497 | 0 | xmlSecOpenSSLSizeT ii, num; |
498 | 0 | int ret; |
499 | |
|
500 | 0 | xmlSecAssert2(cert_issuer != NULL, -1); |
501 | 0 | xmlSecAssert2(crls != NULL, -1); |
502 | 0 | xmlSecAssert2(res != NULL, -1); |
503 | 0 | xmlSecAssert2((*res) == NULL, -1); |
504 | | |
505 | |
|
506 | 0 | num = sk_X509_CRL_num(crls); |
507 | 0 | for(ii = 0; ii < num; ++ii) { |
508 | 0 | crl = sk_X509_CRL_value(crls, ii); |
509 | 0 | if(crl == NULL) { |
510 | 0 | continue; |
511 | 0 | } |
512 | 0 | crl_issuer = X509_CRL_get_issuer(crl); |
513 | 0 | if(crl_issuer == NULL) { |
514 | 0 | continue; |
515 | 0 | } |
516 | | |
517 | | /* is this CRL from same issuer? */ |
518 | 0 | if(xmlSecOpenSSLX509NamesCompare(crl_issuer, cert_issuer) != 0) { |
519 | 0 | continue; |
520 | 0 | } |
521 | | |
522 | | /* use the latest CRL we have */ |
523 | 0 | lastUpdate = X509_CRL_get0_lastUpdate(crl); |
524 | 0 | if(lastUpdate == NULL) { |
525 | 0 | xmlSecOpenSSLError("X509_CRL_get0_lastUpdate", NULL); |
526 | 0 | return(-1); |
527 | 0 | } |
528 | | |
529 | 0 | if((*res) == NULL) { |
530 | 0 | (*res) = crl; |
531 | |
|
532 | 0 | ret = xmlSecOpenSSLX509Asn1TimeToTime(lastUpdate, &resLastUpdateTime); |
533 | 0 | if(ret < 0) { |
534 | 0 | xmlSecInternalError("xmlSecOpenSSLX509Asn1TimeToTime", NULL); |
535 | 0 | return(-1); |
536 | 0 | } |
537 | 0 | continue; |
538 | 0 | } |
539 | | |
540 | | /* return -1 if asn1_time is earlier than, or equal to, ts |
541 | | * and 1 otherwise. These methods return 0 on error.*/ |
542 | 0 | ret = xmlSecOpenSSLAsn1TimeCmp(lastUpdate, &resLastUpdateTime); |
543 | 0 | if(ret == 0) { |
544 | 0 | xmlSecOpenSSLError("X509_cmp_time(lastUpdate)", NULL); |
545 | 0 | return(-1); |
546 | 0 | } |
547 | 0 | if(ret > 0) { |
548 | | /* asn1_time is greater than ts (i.e. crl is newer than crl in res)*/ |
549 | 0 | (*res) = crl; |
550 | |
|
551 | 0 | ret = xmlSecOpenSSLX509Asn1TimeToTime(lastUpdate, &resLastUpdateTime); |
552 | 0 | if(ret < 0) { |
553 | 0 | xmlSecInternalError("xmlSecOpenSSLX509Asn1TimeToTime", NULL); |
554 | 0 | return(-1); |
555 | 0 | } |
556 | 0 | continue; |
557 | 0 | } |
558 | 0 | } |
559 | | |
560 | | /* did we find anything? */ |
561 | 0 | return((*res) != NULL ? 1 : 0); |
562 | 0 | } |
563 | | |
564 | | static int |
565 | 0 | xmlSecOpenSSLX509StoreVerifyCertAgainstCrls(STACK_OF(X509_CRL) *crls, X509* cert, xmlSecKeyInfoCtx* keyInfoCtx) { |
566 | 0 | XMLSEC_OPENSSL400_CONST X509_NAME *cert_issuer; |
567 | 0 | X509_CRL *crl = NULL; |
568 | 0 | STACK_OF(X509_REVOKED) * revoked_certs; |
569 | 0 | int ret; |
570 | |
|
571 | 0 | xmlSecAssert2(crls != NULL, -1); |
572 | 0 | xmlSecAssert2(cert != NULL, -1); |
573 | 0 | xmlSecAssert2(keyInfoCtx != NULL, -1); |
574 | | |
575 | | /* |
576 | | * Try to retrieve a CRL corresponding to the issuer of |
577 | | * the current certificate |
578 | | */ |
579 | 0 | cert_issuer = X509_get_issuer_name(cert); |
580 | 0 | if(cert_issuer == NULL) { |
581 | 0 | xmlSecOpenSSLError("X509_get_issuer_name", NULL); |
582 | 0 | return(-1); |
583 | 0 | } |
584 | | |
585 | 0 | ret = xmlSecOpenSSLX509StoreFindBestCrl(cert_issuer, crls, &crl); |
586 | 0 | if(ret < 0) { |
587 | 0 | xmlSecInternalError("xmlSecOpenSSLX509StoreFindBestCrl", NULL); |
588 | 0 | return(-1); |
589 | 0 | } |
590 | | |
591 | | /* verify against revoked certs */ |
592 | 0 | if(crl == NULL) { |
593 | | /* success: verified! */ |
594 | 0 | return(1); |
595 | 0 | } |
596 | | |
597 | 0 | revoked_certs = X509_CRL_get_REVOKED(crl); |
598 | 0 | if(revoked_certs == NULL) { |
599 | 0 | xmlSecOpenSSLError("X509_CRL_get_REVOKED", NULL); |
600 | 0 | return(-1); |
601 | 0 | } |
602 | | |
603 | 0 | ret = xmlSecOpenSSLX509StoreVerifyCertAgainstRevoked(cert, revoked_certs, keyInfoCtx); |
604 | 0 | if(ret < 0) { |
605 | 0 | xmlSecInternalError("xmlSecOpenSSLX509StoreVerifyCertAgainstRevoked", NULL); |
606 | 0 | return(-1); |
607 | 0 | } else if(ret != 1) { |
608 | 0 | char subject[256], issuer[256]; |
609 | | |
610 | | /* cert is revoked, fail */ |
611 | 0 | xmlSecOpenSSLX509NameToString(X509_get_subject_name(cert), subject, sizeof(subject)); |
612 | 0 | xmlSecOpenSSLX509NameToString(X509_get_issuer_name(cert), issuer, sizeof(issuer)); |
613 | 0 | xmlSecOtherError3(XMLSEC_ERRORS_R_CERT_REVOKED, NULL, "subject=%s; issuer=%s", subject, issuer); |
614 | 0 | return(0); |
615 | 0 | } |
616 | | |
617 | | /* success: verified! */ |
618 | 0 | return(1); |
619 | 0 | } |
620 | | |
621 | | |
622 | | static int |
623 | 0 | xmlSecOpenSSLX509StoreVerifyCertsAgainstCrls(STACK_OF(X509)* chain, STACK_OF(X509_CRL)* crls, xmlSecKeyInfoCtx* keyInfoCtx) { |
624 | 0 | X509 * cert; |
625 | 0 | xmlSecOpenSSLSizeT ii, num_certs; |
626 | 0 | int ret; |
627 | |
|
628 | 0 | xmlSecAssert2(chain != NULL, -1); |
629 | 0 | xmlSecAssert2(crls != NULL, -1); |
630 | 0 | xmlSecAssert2(keyInfoCtx != NULL, -1); |
631 | | |
632 | | /* find all CRLs that apply to each cert */ |
633 | 0 | num_certs = sk_X509_num(chain); |
634 | 0 | for(ii = 0; ii < num_certs; ++ii) { |
635 | 0 | cert = sk_X509_value(chain, ii); |
636 | 0 | if(cert == NULL) { |
637 | 0 | continue; |
638 | 0 | } |
639 | 0 | ret = xmlSecOpenSSLX509StoreVerifyCertAgainstCrls(crls, cert, keyInfoCtx); |
640 | 0 | if(ret < 0) { |
641 | 0 | xmlSecInternalError("xmlSecOpenSSLX509StoreVerifyCertAgainstCrls", NULL); |
642 | 0 | return(-1); |
643 | 0 | } else if(ret != 1) { |
644 | | /* cert was revoked */ |
645 | 0 | return(0); |
646 | 0 | } |
647 | 0 | } |
648 | | |
649 | | /* success! */ |
650 | 0 | return(1); |
651 | 0 | } |
652 | | |
653 | | static int |
654 | 0 | xmlSecOpenSSLX509StoreSetCtx(X509_STORE_CTX* xsc, xmlSecKeyInfoCtx* keyInfoCtx) { |
655 | 0 | X509_VERIFY_PARAM * vpm = NULL; |
656 | 0 | unsigned long vpm_flags = 0; |
657 | |
|
658 | 0 | xmlSecAssert2(xsc != NULL, -1); |
659 | 0 | xmlSecAssert2(keyInfoCtx != NULL, -1); |
660 | |
|
661 | 0 | if(keyInfoCtx->certsVerificationTime > 0) { |
662 | 0 | X509_STORE_CTX_set_time(xsc, 0, keyInfoCtx->certsVerificationTime); |
663 | 0 | } |
664 | | |
665 | | /* set verification params: we verify CRLs manually because OpenSSL fails cert verification if there is no CRL */ |
666 | 0 | vpm = X509_VERIFY_PARAM_new(); |
667 | 0 | if(vpm == NULL) { |
668 | 0 | xmlSecOpenSSLError("X509_VERIFY_PARAM_new", NULL); |
669 | 0 | return(-1); |
670 | 0 | } |
671 | 0 | vpm_flags = X509_VERIFY_PARAM_get_flags(vpm); |
672 | 0 | vpm_flags &= (~((unsigned long)X509_V_FLAG_CRL_CHECK)); |
673 | 0 | if(keyInfoCtx->certsVerificationTime > 0) { |
674 | 0 | vpm_flags |= X509_V_FLAG_USE_CHECK_TIME; |
675 | 0 | X509_VERIFY_PARAM_set_time(vpm, keyInfoCtx->certsVerificationTime); |
676 | 0 | } |
677 | 0 | if((keyInfoCtx->flags & XMLSEC_KEYINFO_FLAGS_X509DATA_SKIP_TIME_CHECKS) != 0) { |
678 | 0 | vpm_flags |= X509_V_FLAG_NO_CHECK_TIME; |
679 | 0 | } |
680 | |
|
681 | 0 | X509_VERIFY_PARAM_set_flags(vpm, vpm_flags); |
682 | 0 | X509_VERIFY_PARAM_set_depth(vpm, keyInfoCtx->certsVerificationDepth); |
683 | |
|
684 | 0 | X509_STORE_CTX_set0_param(xsc, vpm); |
685 | 0 | vpm = NULL; /* owned by xsc now */ |
686 | | |
687 | | /* done */ |
688 | 0 | return(0); |
689 | 0 | } |
690 | | |
691 | | static int |
692 | | xmlSecOpenSSLX509StoreVerifyCert(X509_STORE* xst, X509_STORE_CTX* xsc, X509* cert, |
693 | | STACK_OF(X509)* untrusted, STACK_OF(X509_CRL)* crls, STACK_OF(X509_CRL)* crls2, |
694 | | xmlSecKeyInfoCtx* keyInfoCtx |
695 | 0 | ) { |
696 | 0 | STACK_OF(X509)* chain; |
697 | 0 | int ret; |
698 | 0 | int res = -1; |
699 | |
|
700 | 0 | xmlSecAssert2(xst != NULL, -1); |
701 | 0 | xmlSecAssert2(xsc != NULL, -1); |
702 | 0 | xmlSecAssert2(cert != NULL, -1); |
703 | 0 | xmlSecAssert2(keyInfoCtx != NULL, -1); |
704 | | |
705 | | /* init contenxt and set verification params from keyinfo ctx*/ |
706 | 0 | ret = X509_STORE_CTX_init(xsc, xst, cert, untrusted); |
707 | 0 | if(ret != 1) { |
708 | 0 | xmlSecOpenSSLError("X509_STORE_CTX_init", NULL); |
709 | 0 | goto done; |
710 | 0 | } |
711 | 0 | ret = xmlSecOpenSSLX509StoreSetCtx(xsc, keyInfoCtx); |
712 | 0 | if(ret < 0) { |
713 | 0 | xmlSecInternalError("xmlSecOpenSSLX509StoreSetCtx", NULL); |
714 | 0 | goto done; |
715 | 0 | } |
716 | | |
717 | | /* verify */ |
718 | 0 | ret = X509_verify_cert(xsc); |
719 | 0 | if(ret < 0) { |
720 | 0 | xmlSecOpenSSLError("X509_verify_cert", NULL); |
721 | 0 | goto done; |
722 | 0 | } else if(ret != 1) { |
723 | 0 | X509 * err_cert = NULL; |
724 | 0 | int err = 0; |
725 | | |
726 | | /* not verified: get error */ |
727 | 0 | err_cert = X509_STORE_CTX_get_current_cert(xsc); |
728 | 0 | err = X509_STORE_CTX_get_error(xsc); |
729 | 0 | if((err != 0) && (err_cert != NULL)) { |
730 | 0 | const char* err_msg; |
731 | 0 | char subject[256], issuer[256]; |
732 | |
|
733 | 0 | xmlSecOpenSSLX509NameToString(X509_get_subject_name(err_cert), subject, sizeof(subject)); |
734 | 0 | xmlSecOpenSSLX509NameToString(X509_get_issuer_name(err_cert), issuer, sizeof(issuer)); |
735 | 0 | err_msg = X509_verify_cert_error_string(err); |
736 | |
|
737 | 0 | switch (err) { |
738 | 0 | case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT: |
739 | 0 | xmlSecOtherError5(XMLSEC_ERRORS_R_CERT_ISSUER_FAILED, NULL, |
740 | 0 | "subject=%s; issuer=%s; err=%d; msg=%s", |
741 | 0 | subject, issuer, err, xmlSecErrorsSafeString(err_msg)); |
742 | 0 | break; |
743 | 0 | case X509_V_ERR_CERT_NOT_YET_VALID: |
744 | 0 | case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD: |
745 | 0 | xmlSecOtherError5(XMLSEC_ERRORS_R_CERT_NOT_YET_VALID, NULL, |
746 | 0 | "subject=%s; issuer=%s; err=%d; msg=%s", |
747 | 0 | subject, issuer, err, xmlSecErrorsSafeString(err_msg)); |
748 | 0 | break; |
749 | 0 | case X509_V_ERR_CERT_HAS_EXPIRED: |
750 | 0 | case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD: |
751 | 0 | xmlSecOtherError5(XMLSEC_ERRORS_R_CERT_HAS_EXPIRED, NULL, |
752 | 0 | "subject=%s; issuer=%s; err=%d; msg=%s", |
753 | 0 | subject, issuer, err, xmlSecErrorsSafeString(err_msg)); |
754 | 0 | break; |
755 | 0 | default: |
756 | 0 | xmlSecOtherError5(XMLSEC_ERRORS_R_CERT_VERIFY_FAILED, NULL, |
757 | 0 | "subject=%s; issuer=%s; err=%d; msg=%s", |
758 | 0 | subject, issuer, err, xmlSecErrorsSafeString(err_msg)); |
759 | 0 | break; |
760 | 0 | } |
761 | 0 | } else if(err != 0) { |
762 | 0 | const char* err_msg; |
763 | |
|
764 | 0 | err_msg = X509_verify_cert_error_string(err); |
765 | 0 | xmlSecOtherError3(XMLSEC_ERRORS_R_CERT_VERIFY_FAILED, NULL, |
766 | 0 | "err=%d; msg=%s", err, xmlSecErrorsSafeString(err_msg)); |
767 | 0 | } else { |
768 | 0 | xmlSecOtherError(XMLSEC_ERRORS_R_CERT_VERIFY_FAILED, NULL, "cert verification failed"); |
769 | 0 | } |
770 | | |
771 | | /* not verified */ |
772 | 0 | res = 0; |
773 | 0 | goto done; |
774 | 0 | } |
775 | | |
776 | 0 | chain = X509_STORE_CTX_get0_chain(xsc); |
777 | 0 | if(chain == NULL) { |
778 | 0 | xmlSecOpenSSLError("X509_STORE_CTX_get0_chain(crls)", NULL); |
779 | 0 | goto done; |
780 | 0 | } |
781 | | |
782 | | /* now check against crls */ |
783 | 0 | if(crls != NULL) { |
784 | 0 | ret = xmlSecOpenSSLX509StoreVerifyCertsAgainstCrls(chain, crls, keyInfoCtx); |
785 | 0 | if(ret < 0) { |
786 | 0 | xmlSecInternalError("xmlSecOpenSSLX509StoreVerifyCertsAgainstCrls(crls)", NULL); |
787 | 0 | goto done; |
788 | 0 | } else if(ret != 1) { |
789 | | /* not verified */ |
790 | 0 | res = 0; |
791 | 0 | goto done; |
792 | 0 | } |
793 | 0 | } |
794 | 0 | if(crls2 != NULL) { |
795 | 0 | ret = xmlSecOpenSSLX509StoreVerifyCertsAgainstCrls(chain, crls2, keyInfoCtx); |
796 | 0 | if(ret < 0) { |
797 | 0 | xmlSecInternalError("xmlSecOpenSSLX509StoreVerifyCertsAgainstCrls(crls2)", NULL); |
798 | 0 | goto done; |
799 | 0 | } else if(ret != 1) { |
800 | | /* not verified */ |
801 | 0 | res = 0; |
802 | 0 | goto done; |
803 | 0 | } |
804 | 0 | } |
805 | | |
806 | | /* success: verified */ |
807 | 0 | res = 1; |
808 | |
|
809 | 0 | done: |
810 | 0 | X509_STORE_CTX_cleanup(xsc); |
811 | 0 | return(res); |
812 | 0 | } |
813 | | |
814 | | /* Filters a CRL stack by time validity, returning a new stack that contains |
815 | | * only CRLs that are currently valid (thisUpdate <= verification_time <= nextUpdate). |
816 | | * Does NOT re-verify CRL signatures — store CRLs are already trusted. |
817 | | * Returns NULL if the input stack is NULL/empty or on allocation failure. |
818 | | * The returned stack does not own the CRL pointers. |
819 | | */ |
820 | | static int |
821 | 0 | xmlSecOpenSSLX509FilterCrlsByTime(STACK_OF(X509_CRL)* crls, xmlSecKeyInfoCtx* keyInfoCtx, STACK_OF(X509_CRL)** out_crls) { |
822 | 0 | STACK_OF(X509_CRL)* res = NULL; |
823 | 0 | xmlSecOpenSSLSizeT ii, num; |
824 | 0 | int ret; |
825 | |
|
826 | 0 | xmlSecAssert2(keyInfoCtx != NULL, -1); |
827 | 0 | xmlSecAssert2(out_crls != NULL, -1); |
828 | |
|
829 | 0 | (*out_crls) = NULL; |
830 | |
|
831 | 0 | if(crls == NULL) { |
832 | 0 | return(0); |
833 | 0 | } |
834 | 0 | num = sk_X509_CRL_num(crls); |
835 | 0 | if(num <= 0) { |
836 | 0 | return(0); |
837 | 0 | } |
838 | | |
839 | 0 | res = sk_X509_CRL_new_null(); |
840 | 0 | if(res == NULL) { |
841 | 0 | xmlSecOpenSSLError("sk_X509_CRL_new_null", NULL); |
842 | 0 | return(-1); |
843 | 0 | } |
844 | | |
845 | 0 | for(ii = 0; ii < num; ++ii) { |
846 | 0 | X509_CRL* crl = sk_X509_CRL_value(crls, ii); |
847 | 0 | if(crl == NULL) { |
848 | 0 | continue; |
849 | 0 | } |
850 | | |
851 | 0 | ret = xmlSecOpenSSLX509VerifyCRLTimeValidity(crl, keyInfoCtx); |
852 | 0 | if(ret < 0) { |
853 | 0 | xmlSecInternalError("xmlSecOpenSSLX509VerifyCRLTimeValidity", NULL); |
854 | 0 | sk_X509_CRL_free(res); |
855 | 0 | return(-1); |
856 | 0 | } else if(ret != 1) { |
857 | | /* CRL is not yet valid or has expired — skip it */ |
858 | 0 | continue; |
859 | 0 | } |
860 | | |
861 | 0 | if(sk_X509_CRL_push(res, crl) <= 0) { |
862 | 0 | xmlSecOpenSSLError("sk_X509_CRL_push", NULL); |
863 | 0 | sk_X509_CRL_free(res); |
864 | 0 | return(-1); |
865 | 0 | } |
866 | 0 | } |
867 | | |
868 | 0 | (*out_crls) = res; |
869 | 0 | return(0); |
870 | 0 | } |
871 | | |
872 | | /** |
873 | | * @brief Verifies @p certs list. |
874 | | * @param store the pointer to X509 key data store klass. |
875 | | * @param certs the untrusted certificates stack. |
876 | | * @param crls the crls stack. |
877 | | * @param keyInfoCtx the pointer to <dsig:KeyInfo/> element processing context. |
878 | | * @return pointer to the first verified certificate from @p certs. |
879 | | */ |
880 | | X509* |
881 | 0 | xmlSecOpenSSLX509StoreVerify(xmlSecKeyDataStorePtr store, XMLSEC_STACK_OF_X509* certs, XMLSEC_STACK_OF_X509_CRL* crls, xmlSecKeyInfoCtx* keyInfoCtx) { |
882 | 0 | xmlSecOpenSSLX509StoreCtxPtr ctx; |
883 | 0 | STACK_OF(X509)* all_untrusted_certs = NULL; |
884 | 0 | STACK_OF(X509_CRL)* verified_crls = NULL; |
885 | 0 | STACK_OF(X509_CRL)* time_filtered_crls = NULL; |
886 | 0 | X509 * res = NULL; |
887 | 0 | X509 * cert; |
888 | 0 | X509_STORE_CTX *xsc = NULL; |
889 | 0 | xmlSecOpenSSLSizeT ii, num; |
890 | 0 | int ret; |
891 | |
|
892 | 0 | xmlSecAssert2(xmlSecKeyDataStoreCheckId(store, xmlSecOpenSSLX509StoreId), NULL); |
893 | 0 | xmlSecAssert2(certs != NULL, NULL); |
894 | 0 | xmlSecAssert2(keyInfoCtx != NULL, NULL); |
895 | |
|
896 | 0 | ctx = xmlSecOpenSSLX509StoreGetCtx(store); |
897 | 0 | xmlSecAssert2(ctx != NULL, NULL); |
898 | 0 | xmlSecAssert2(ctx->xst != NULL, NULL); |
899 | | |
900 | | /* reuse xsc for both crls and certs verification */ |
901 | 0 | xsc = X509_STORE_CTX_new_ex(xmlSecOpenSSLGetLibCtx(), NULL); |
902 | 0 | if(xsc == NULL) { |
903 | 0 | xmlSecOpenSSLError("X509_STORE_CTX_new", xmlSecKeyDataStoreGetName(store)); |
904 | 0 | goto done; |
905 | 0 | } |
906 | | |
907 | | /* create a combined list of all untrusted certs (new list doesn't OWN certs)*/ |
908 | 0 | all_untrusted_certs = xmlSecOpenSSLX509StoreCombineCerts(certs, ctx->untrusted); |
909 | 0 | if(all_untrusted_certs == NULL) { |
910 | 0 | xmlSecInternalError("xmlSecOpenSSLX509StoreCombineCerts", NULL); |
911 | 0 | goto done; |
912 | 0 | } |
913 | | |
914 | | /* if cert verification is disabled, return the first leaf cert without |
915 | | * touching CRLs or other verification-only state. |
916 | | */ |
917 | 0 | if((keyInfoCtx->flags & XMLSEC_KEYINFO_FLAGS_X509DATA_DONT_VERIFY_CERTS) != 0) { |
918 | 0 | num = sk_X509_num(certs); |
919 | 0 | for(ii = 0; ii < num; ++ii) { |
920 | 0 | cert = sk_X509_value(certs, ii); |
921 | 0 | if(cert == NULL) { |
922 | 0 | continue; |
923 | 0 | } |
924 | | |
925 | 0 | if((all_untrusted_certs != NULL) && (xmlSecOpenSSLX509FindChildCert(all_untrusted_certs, cert) != NULL)) { |
926 | 0 | continue; |
927 | 0 | } |
928 | | |
929 | 0 | res = cert; |
930 | 0 | goto done; |
931 | 0 | } |
932 | | |
933 | 0 | goto done; |
934 | 0 | } |
935 | | |
936 | | /* copy crls list but remove all non-verified (we assume that CRLs in the store are already verified) */ |
937 | 0 | ret = xmlSecOpenSSLX509StoreVerifyAndCopyCrls(ctx->xst, xsc, all_untrusted_certs, crls, keyInfoCtx, &verified_crls); |
938 | 0 | if(ret < 0) { |
939 | 0 | xmlSecInternalError("xmlSecOpenSSLX509StoreVerifyAndCopyCrls", xmlSecKeyDataStoreGetName(store)); |
940 | 0 | goto done; |
941 | 0 | } |
942 | | |
943 | | /* filter store crls by time validity (signatures already trusted) */ |
944 | 0 | ret = xmlSecOpenSSLX509FilterCrlsByTime(ctx->crls, keyInfoCtx, &time_filtered_crls); |
945 | 0 | if(ret < 0) { |
946 | 0 | xmlSecInternalError("xmlSecOpenSSLX509FilterCrlsByTime", xmlSecKeyDataStoreGetName(store)); |
947 | 0 | goto done; |
948 | 0 | } |
949 | | |
950 | | /* get one cert after another and try to verify */ |
951 | 0 | num = sk_X509_num(certs); |
952 | 0 | for(ii = 0; ii < num; ++ii) { |
953 | 0 | cert = sk_X509_value(certs, ii); |
954 | 0 | if(cert == NULL) { |
955 | 0 | continue; |
956 | 0 | } |
957 | | |
958 | | /* we only attempt to verify "leaf" certs without children */ |
959 | 0 | if((all_untrusted_certs != NULL) && (xmlSecOpenSSLX509FindChildCert(all_untrusted_certs, cert) != NULL)) { |
960 | 0 | continue; |
961 | 0 | } |
962 | | |
963 | 0 | ret = xmlSecOpenSSLX509StoreVerifyCert(ctx->xst, xsc, cert, all_untrusted_certs, verified_crls, time_filtered_crls, keyInfoCtx); |
964 | 0 | if(ret < 0) { |
965 | 0 | xmlSecInternalError("xmlSecOpenSSLX509StoreVerifyCert", xmlSecKeyDataStoreGetName(store)); |
966 | 0 | goto done; |
967 | 0 | } else if(ret != 1) { |
968 | 0 | continue; |
969 | 0 | } |
970 | | |
971 | | /* success! */ |
972 | 0 | res = cert; |
973 | 0 | break; |
974 | 0 | } |
975 | | |
976 | 0 | done: |
977 | | /* only free sk_* structures, not the certs or crls because caller owns pointers |
978 | | * or the store does and we didn't up_ref / dup certs when creating the sk_*'s. |
979 | | */ |
980 | 0 | if(all_untrusted_certs != NULL) { |
981 | 0 | sk_X509_free(all_untrusted_certs); |
982 | 0 | } |
983 | 0 | if(verified_crls != NULL) { |
984 | 0 | sk_X509_CRL_free(verified_crls); |
985 | 0 | } |
986 | 0 | if(time_filtered_crls != NULL) { |
987 | 0 | sk_X509_CRL_free(time_filtered_crls); |
988 | 0 | } |
989 | 0 | if(xsc != NULL) { |
990 | 0 | X509_STORE_CTX_free(xsc); |
991 | 0 | } |
992 | 0 | return(res); |
993 | 0 | } |
994 | | |
995 | | /** |
996 | | * @brief Verifies @p key with the keys manager @p mngr created with #xmlSecCryptoAppDefaultKeysMngrInit |
997 | | * @param store the pointer to X509 key data store klass. |
998 | | * @param key the pointer to key. |
999 | | * @param keyInfoCtx the key info context for verification. |
1000 | | * |
1001 | | * function: |
1002 | | * - Checks that key certificate is present |
1003 | | * - Checks that key certificate is valid |
1004 | | * |
1005 | | * Adds @p key to the keys manager @p mngr created with #xmlSecCryptoAppDefaultKeysMngrInit |
1006 | | * function. |
1007 | | * |
1008 | | * @return 1 if key is verified, 0 otherwise, or a negative value if an error occurs. |
1009 | | */ |
1010 | | int |
1011 | 0 | xmlSecOpenSSLX509StoreVerifyKey(xmlSecKeyDataStorePtr store, xmlSecKeyPtr key, xmlSecKeyInfoCtxPtr keyInfoCtx) { |
1012 | 0 | xmlSecOpenSSLX509StoreCtxPtr ctx; |
1013 | 0 | xmlSecKeyDataPtr x509Data; |
1014 | 0 | X509* keyCert; |
1015 | 0 | STACK_OF(X509)* certs; |
1016 | 0 | STACK_OF(X509_CRL)* crls; |
1017 | 0 | X509_STORE_CTX *xsc = NULL; |
1018 | 0 | STACK_OF(X509)* all_untrusted_certs = NULL; |
1019 | 0 | STACK_OF(X509_CRL)* verified_crls = NULL; |
1020 | 0 | STACK_OF(X509_CRL)* time_filtered_crls = NULL; |
1021 | 0 | int ret; |
1022 | 0 | int res = -1; |
1023 | |
|
1024 | 0 | xmlSecAssert2(xmlSecKeyDataStoreCheckId(store, xmlSecOpenSSLX509StoreId), -1); |
1025 | 0 | xmlSecAssert2(key != NULL, -1); |
1026 | 0 | xmlSecAssert2(keyInfoCtx != NULL, -1); |
1027 | | |
1028 | |
|
1029 | 0 | ctx = xmlSecOpenSSLX509StoreGetCtx(store); |
1030 | 0 | xmlSecAssert2(ctx != NULL, -1); |
1031 | 0 | xmlSecAssert2(ctx->xst != NULL, -1); |
1032 | | |
1033 | | /* retrieve X509 data and get key cert */ |
1034 | 0 | x509Data = xmlSecKeyGetData(key, xmlSecOpenSSLKeyDataX509Id); |
1035 | 0 | if(x509Data == NULL) { |
1036 | 0 | xmlSecInternalError("xmlSecKeyGetData(xmlSecOpenSSLKeyDataX509Id)", xmlSecKeyDataStoreGetName(store)); |
1037 | 0 | return(-1); |
1038 | 0 | } |
1039 | 0 | keyCert = xmlSecOpenSSLKeyDataX509GetKeyCert(x509Data); |
1040 | 0 | if(keyCert == NULL) { |
1041 | 0 | xmlSecInternalError("key certificate is required", xmlSecKeyDataStoreGetName(store)); |
1042 | 0 | res = 0; /* verification failed */ |
1043 | 0 | goto done; |
1044 | 0 | } |
1045 | | |
1046 | | /* do we even need to verify the key cert? */ |
1047 | 0 | if((keyInfoCtx->flags & XMLSEC_KEYINFO_FLAGS_X509DATA_DONT_VERIFY_CERTS) != 0) { |
1048 | 0 | res = 1; |
1049 | 0 | goto done; |
1050 | 0 | } |
1051 | | |
1052 | 0 | certs = xmlSecOpenSSLKeyDataX509GetCerts(x509Data); |
1053 | 0 | crls = xmlSecOpenSSLKeyDataX509GetCrls(x509Data); |
1054 | | |
1055 | | /* reuse xsc for both crls and certs verification */ |
1056 | 0 | xsc = X509_STORE_CTX_new_ex(xmlSecOpenSSLGetLibCtx(), NULL); |
1057 | 0 | if(xsc == NULL) { |
1058 | 0 | xmlSecOpenSSLError("X509_STORE_CTX_new", xmlSecKeyDataStoreGetName(store)); |
1059 | 0 | goto done; |
1060 | 0 | } |
1061 | | |
1062 | | /* create a combined list of all untrusted certs (new list doesn't OWN certs) */ |
1063 | 0 | all_untrusted_certs = xmlSecOpenSSLX509StoreCombineCerts(certs, ctx->untrusted); |
1064 | 0 | if(all_untrusted_certs == NULL) { |
1065 | 0 | xmlSecInternalError("xmlSecOpenSSLX509StoreCombineCerts", xmlSecKeyDataStoreGetName(store)); |
1066 | 0 | goto done; |
1067 | 0 | } |
1068 | | |
1069 | | /* copy crls list but remove all non-verified (we assume that CRLs in the store are already verified) */ |
1070 | 0 | ret = xmlSecOpenSSLX509StoreVerifyAndCopyCrls(ctx->xst, xsc, all_untrusted_certs, crls, keyInfoCtx, &verified_crls); |
1071 | 0 | if(ret < 0) { |
1072 | 0 | xmlSecInternalError("xmlSecOpenSSLX509StoreVerifyAndCopyCrls", xmlSecKeyDataStoreGetName(store)); |
1073 | 0 | goto done; |
1074 | 0 | } |
1075 | | |
1076 | | /* filter store crls by time validity (signatures already trusted) */ |
1077 | 0 | ret = xmlSecOpenSSLX509FilterCrlsByTime(ctx->crls, keyInfoCtx, &time_filtered_crls); |
1078 | 0 | if(ret < 0) { |
1079 | 0 | xmlSecInternalError("xmlSecOpenSSLX509FilterCrlsByTime", xmlSecKeyDataStoreGetName(store)); |
1080 | 0 | goto done; |
1081 | 0 | } |
1082 | | |
1083 | | /* verify */ |
1084 | 0 | ret = xmlSecOpenSSLX509StoreVerifyCert(ctx->xst, xsc, keyCert, all_untrusted_certs, verified_crls, time_filtered_crls, keyInfoCtx); |
1085 | 0 | if(ret < 0) { |
1086 | 0 | xmlSecInternalError("xmlSecOpenSSLX509StoreVerifyCert", xmlSecKeyDataStoreGetName(store)); |
1087 | 0 | goto done; |
1088 | 0 | } else if(ret != 1) { |
1089 | 0 | res = 0; /* verification failed */ |
1090 | 0 | goto done; |
1091 | 0 | } |
1092 | | |
1093 | | /* success! */ |
1094 | 0 | res = 1; |
1095 | |
|
1096 | 0 | done: |
1097 | | /* only free sk_* structures, not the certs or crls because caller owns pointers |
1098 | | * or the store does and we didn't up_ref / dup certs when creating the sk_*'s. |
1099 | | */ |
1100 | 0 | if(all_untrusted_certs != NULL) { |
1101 | 0 | sk_X509_free(all_untrusted_certs); |
1102 | 0 | } |
1103 | 0 | if(verified_crls != NULL) { |
1104 | 0 | sk_X509_CRL_free(verified_crls); |
1105 | 0 | } |
1106 | 0 | if(time_filtered_crls != NULL) { |
1107 | 0 | sk_X509_CRL_free(time_filtered_crls); |
1108 | 0 | } |
1109 | 0 | if(xsc != NULL) { |
1110 | 0 | X509_STORE_CTX_free(xsc); |
1111 | 0 | } |
1112 | 0 | return(res); |
1113 | 0 | } |
1114 | | |
1115 | | /** |
1116 | | * @brief Verifies @p crl by checking: |
1117 | | * @param store the pointer to X509 key data store klass. |
1118 | | * @param crl the CRL to verify. |
1119 | | * @param keyInfoCtx the key info context for verification parameters. |
1120 | | * |
1121 | | * 1. Signature is valid (signed by issuer cert in store) |
1122 | | * 2. thisUpdate <= verification_time <= nextUpdate |
1123 | | * |
1124 | | * @return 1 if verified, 0 if not verified, or a negative value on error. |
1125 | | */ |
1126 | | int |
1127 | | xmlSecOpenSSLX509StoreVerifyCrl(xmlSecKeyDataStorePtr store, X509_CRL* crl, |
1128 | | xmlSecKeyInfoCtxPtr keyInfoCtx |
1129 | 0 | ) { |
1130 | 0 | xmlSecOpenSSLX509StoreCtxPtr ctx; |
1131 | 0 | X509_STORE_CTX *xsc = NULL; |
1132 | 0 | int ret; |
1133 | 0 | int res = -1; |
1134 | |
|
1135 | 0 | xmlSecAssert2(xmlSecKeyDataStoreCheckId(store, xmlSecOpenSSLX509StoreId), -1); |
1136 | 0 | xmlSecAssert2(crl != NULL, -1); |
1137 | 0 | xmlSecAssert2(keyInfoCtx != NULL, -1); |
1138 | | |
1139 | | /* do we even need to verify the CRL? */ |
1140 | 0 | if((keyInfoCtx->flags & XMLSEC_KEYINFO_FLAGS_X509DATA_DONT_VERIFY_CERTS) != 0) { |
1141 | 0 | return(1); |
1142 | 0 | } |
1143 | | |
1144 | 0 | ctx = xmlSecOpenSSLX509StoreGetCtx(store); |
1145 | 0 | xmlSecAssert2(ctx != NULL, -1); |
1146 | 0 | xmlSecAssert2(ctx->xst != NULL, -1); |
1147 | | |
1148 | | /* Create store context */ |
1149 | 0 | xsc = X509_STORE_CTX_new_ex(xmlSecOpenSSLGetLibCtx(), NULL); |
1150 | 0 | if(xsc == NULL) { |
1151 | 0 | xmlSecOpenSSLError("X509_STORE_CTX_new", xmlSecKeyDataStoreGetName(store)); |
1152 | 0 | goto done; |
1153 | 0 | } |
1154 | | |
1155 | | /* Verify CRL signature, issuer, and time validity */ |
1156 | 0 | ret = xmlSecOpenSSLX509VerifyCRL(ctx->xst, xsc, ctx->untrusted, crl, keyInfoCtx); |
1157 | 0 | if(ret < 0) { |
1158 | 0 | xmlSecInternalError("xmlSecOpenSSLX509VerifyCRL", xmlSecKeyDataStoreGetName(store)); |
1159 | 0 | goto done; |
1160 | 0 | } else if(ret != 1) { |
1161 | | /* Verification failed */ |
1162 | 0 | res = 0; |
1163 | 0 | goto done; |
1164 | 0 | } |
1165 | | |
1166 | | /* Success */ |
1167 | 0 | res = 1; |
1168 | |
|
1169 | 0 | done: |
1170 | 0 | if(xsc != NULL) { |
1171 | 0 | X509_STORE_CTX_free(xsc); |
1172 | 0 | } |
1173 | 0 | return(res); |
1174 | 0 | } |
1175 | | |
1176 | | /** |
1177 | | * @brief Adds cert to the trusted or untrusted store. |
1178 | | * @details Adds trusted (root) or untrusted certificate to the store. |
1179 | | * @param store the pointer to X509 key data store klass. |
1180 | | * @param cert the pointer to OpenSSL X509 certificate. |
1181 | | * @param type the certificate type (trusted/untrusted). |
1182 | | * @return 0 on success or a negative value if an error occurs. |
1183 | | */ |
1184 | | int |
1185 | 0 | xmlSecOpenSSLX509StoreAdoptCert(xmlSecKeyDataStorePtr store, X509* cert, xmlSecKeyDataType type) { |
1186 | 0 | xmlSecOpenSSLX509StoreCtxPtr ctx; |
1187 | |
|
1188 | 0 | xmlSecAssert2(xmlSecKeyDataStoreCheckId(store, xmlSecOpenSSLX509StoreId), -1); |
1189 | 0 | xmlSecAssert2(cert != NULL, -1); |
1190 | |
|
1191 | 0 | ctx = xmlSecOpenSSLX509StoreGetCtx(store); |
1192 | 0 | xmlSecAssert2(ctx != NULL, -1); |
1193 | |
|
1194 | 0 | if((type & xmlSecKeyDataTypeTrusted) != 0) { |
1195 | 0 | int ret; |
1196 | |
|
1197 | 0 | xmlSecAssert2(ctx->xst != NULL, -1); |
1198 | |
|
1199 | 0 | ret = X509_STORE_add_cert(ctx->xst, cert); |
1200 | 0 | if(ret != 1) { |
1201 | 0 | xmlSecOpenSSLError("X509_STORE_add_cert", |
1202 | 0 | xmlSecKeyDataStoreGetName(store)); |
1203 | 0 | return(-1); |
1204 | 0 | } |
1205 | | /* add cert increments the reference */ |
1206 | 0 | X509_free(cert); |
1207 | 0 | } else { |
1208 | 0 | xmlSecOpenSSLSizeT ret; |
1209 | |
|
1210 | 0 | xmlSecAssert2(ctx->untrusted != NULL, -1); |
1211 | |
|
1212 | 0 | ret = sk_X509_push(ctx->untrusted, cert); |
1213 | 0 | if(ret <= 0) { |
1214 | 0 | xmlSecOpenSSLError("sk_X509_push", xmlSecKeyDataStoreGetName(store)); |
1215 | 0 | return(-1); |
1216 | 0 | } |
1217 | 0 | } |
1218 | 0 | return(0); |
1219 | 0 | } |
1220 | | |
1221 | | /** |
1222 | | * @brief Adds X509 CRL to the store. |
1223 | | * @param store the pointer to X509 key data store klass. |
1224 | | * @param crl the pointer to OpenSSL X509_CRL. |
1225 | | * @return 0 on success or a negative value if an error occurs. |
1226 | | */ |
1227 | | int |
1228 | 0 | xmlSecOpenSSLX509StoreAdoptCrl(xmlSecKeyDataStorePtr store, X509_CRL* crl) { |
1229 | 0 | xmlSecOpenSSLX509StoreCtxPtr ctx; |
1230 | 0 | xmlSecOpenSSLSizeT ret; |
1231 | |
|
1232 | 0 | xmlSecAssert2(xmlSecKeyDataStoreCheckId(store, xmlSecOpenSSLX509StoreId), -1); |
1233 | 0 | xmlSecAssert2(crl != NULL, -1); |
1234 | |
|
1235 | 0 | ctx = xmlSecOpenSSLX509StoreGetCtx(store); |
1236 | 0 | xmlSecAssert2(ctx != NULL, -1); |
1237 | 0 | xmlSecAssert2(ctx->crls != NULL, -1); |
1238 | |
|
1239 | 0 | ret = sk_X509_CRL_push(ctx->crls, crl); |
1240 | 0 | if(ret <= 0) { |
1241 | 0 | xmlSecOpenSSLError("sk_X509_CRL_push", xmlSecKeyDataStoreGetName(store)); |
1242 | 0 | return(-1); |
1243 | 0 | } |
1244 | | |
1245 | 0 | return (0); |
1246 | 0 | } |
1247 | | |
1248 | | /** |
1249 | | * @brief Adds all certs in the @p path to the list of trusted certs |
1250 | | * @param store the pointer to OpenSSL x509 store. |
1251 | | * @param path the path to the certs dir. |
1252 | | * |
1253 | | * in @p store. |
1254 | | * |
1255 | | * @return 0 on success or a negative value otherwise. |
1256 | | */ |
1257 | | int |
1258 | 0 | xmlSecOpenSSLX509StoreAddCertsPath(xmlSecKeyDataStorePtr store, const char *path) { |
1259 | 0 | xmlSecOpenSSLX509StoreCtxPtr ctx; |
1260 | 0 | X509_LOOKUP *lookup = NULL; |
1261 | |
|
1262 | 0 | xmlSecAssert2(xmlSecKeyDataStoreCheckId(store, xmlSecOpenSSLX509StoreId), -1); |
1263 | 0 | xmlSecAssert2(path != NULL, -1); |
1264 | |
|
1265 | 0 | ctx = xmlSecOpenSSLX509StoreGetCtx(store); |
1266 | 0 | xmlSecAssert2(ctx != NULL, -1); |
1267 | 0 | xmlSecAssert2(ctx->xst != NULL, -1); |
1268 | |
|
1269 | 0 | lookup = X509_STORE_add_lookup(ctx->xst, X509_LOOKUP_hash_dir()); |
1270 | 0 | if(lookup == NULL) { |
1271 | 0 | xmlSecOpenSSLError("X509_STORE_add_lookup", |
1272 | 0 | xmlSecKeyDataStoreGetName(store)); |
1273 | 0 | return(-1); |
1274 | 0 | } |
1275 | 0 | if(!X509_LOOKUP_add_dir(lookup, path, X509_FILETYPE_PEM)) { |
1276 | 0 | xmlSecOpenSSLError2("X509_LOOKUP_add_dir", |
1277 | 0 | xmlSecKeyDataStoreGetName(store), |
1278 | 0 | "path='%s'", |
1279 | 0 | xmlSecErrorsSafeString(path)); |
1280 | 0 | return(-1); |
1281 | 0 | } |
1282 | 0 | return(0); |
1283 | 0 | } |
1284 | | |
1285 | | /** |
1286 | | * @brief Adds all certs in the file to the list of trusted certs |
1287 | | * @param store the pointer to OpenSSL x509 store. |
1288 | | * @param filename the certs file. |
1289 | | * |
1290 | | * in @p store. It is possible for the file to contain multiple certs. |
1291 | | * |
1292 | | * @return 0 on success or a negative value otherwise. |
1293 | | */ |
1294 | | int |
1295 | 0 | xmlSecOpenSSLX509StoreAddCertsFile(xmlSecKeyDataStorePtr store, const char *filename) { |
1296 | 0 | xmlSecOpenSSLX509StoreCtxPtr ctx; |
1297 | 0 | X509_LOOKUP *lookup = NULL; |
1298 | |
|
1299 | 0 | xmlSecAssert2(xmlSecKeyDataStoreCheckId(store, xmlSecOpenSSLX509StoreId), -1); |
1300 | 0 | xmlSecAssert2(filename != NULL, -1); |
1301 | |
|
1302 | 0 | ctx = xmlSecOpenSSLX509StoreGetCtx(store); |
1303 | 0 | xmlSecAssert2(ctx != NULL, -1); |
1304 | 0 | xmlSecAssert2(ctx->xst != NULL, -1); |
1305 | |
|
1306 | 0 | lookup = X509_STORE_add_lookup(ctx->xst, X509_LOOKUP_file()); |
1307 | 0 | if(lookup == NULL) { |
1308 | 0 | xmlSecOpenSSLError("X509_STORE_add_lookup", |
1309 | 0 | xmlSecKeyDataStoreGetName(store)); |
1310 | 0 | return(-1); |
1311 | 0 | } |
1312 | 0 | if(!X509_LOOKUP_load_file(lookup, filename, X509_FILETYPE_PEM)) { |
1313 | 0 | xmlSecOpenSSLError2("X509_LOOKUP_load_file", |
1314 | 0 | xmlSecKeyDataStoreGetName(store), |
1315 | 0 | "filename='%s'", |
1316 | 0 | xmlSecErrorsSafeString(filename)); |
1317 | 0 | return(-1); |
1318 | 0 | } |
1319 | 0 | return(0); |
1320 | 0 | } |
1321 | | |
1322 | | static int |
1323 | 0 | xmlSecOpenSSLX509StoreInitialize(xmlSecKeyDataStorePtr store) { |
1324 | 0 | const xmlChar* path; |
1325 | 0 | X509_LOOKUP *lookup = NULL; |
1326 | 0 | int ret; |
1327 | |
|
1328 | 0 | xmlSecOpenSSLX509StoreCtxPtr ctx; |
1329 | 0 | xmlSecAssert2(xmlSecKeyDataStoreCheckId(store, xmlSecOpenSSLX509StoreId), -1); |
1330 | |
|
1331 | 0 | ctx = xmlSecOpenSSLX509StoreGetCtx(store); |
1332 | 0 | xmlSecAssert2(ctx != NULL, -1); |
1333 | |
|
1334 | 0 | memset(ctx, 0, sizeof(xmlSecOpenSSLX509StoreCtx)); |
1335 | |
|
1336 | 0 | ctx->xst = X509_STORE_new(); |
1337 | 0 | if(ctx->xst == NULL) { |
1338 | 0 | xmlSecOpenSSLError("X509_STORE_new", |
1339 | 0 | xmlSecKeyDataStoreGetName(store)); |
1340 | 0 | return(-1); |
1341 | 0 | } |
1342 | | |
1343 | 0 | ret = X509_STORE_set_default_paths_ex(ctx->xst, xmlSecOpenSSLGetLibCtx(), NULL); |
1344 | 0 | if(ret != 1) { |
1345 | 0 | xmlSecOpenSSLError("X509_STORE_set_default_paths", |
1346 | 0 | xmlSecKeyDataStoreGetName(store)); |
1347 | 0 | return(-1); |
1348 | 0 | } |
1349 | | |
1350 | | |
1351 | 0 | lookup = X509_STORE_add_lookup(ctx->xst, X509_LOOKUP_hash_dir()); |
1352 | 0 | if(lookup == NULL) { |
1353 | 0 | xmlSecOpenSSLError("X509_STORE_add_lookup", |
1354 | 0 | xmlSecKeyDataStoreGetName(store)); |
1355 | 0 | return(-1); |
1356 | 0 | } |
1357 | | |
1358 | 0 | path = xmlSecOpenSSLGetDefaultTrustedCertsFolder(); |
1359 | 0 | if(path != NULL) { |
1360 | 0 | if(!X509_LOOKUP_add_dir(lookup, (char*)path, X509_FILETYPE_PEM)) { |
1361 | 0 | xmlSecOpenSSLError2("X509_LOOKUP_add_dir", |
1362 | 0 | xmlSecKeyDataStoreGetName(store), |
1363 | 0 | "path='%s'", |
1364 | 0 | xmlSecErrorsSafeString(path)); |
1365 | 0 | return(-1); |
1366 | 0 | } |
1367 | 0 | } else { |
1368 | 0 | if(!X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT)) { |
1369 | 0 | xmlSecOpenSSLError("X509_LOOKUP_add_dir", |
1370 | 0 | xmlSecKeyDataStoreGetName(store)); |
1371 | 0 | return(-1); |
1372 | 0 | } |
1373 | 0 | } |
1374 | | |
1375 | 0 | ctx->untrusted = sk_X509_new_null(); |
1376 | 0 | if(ctx->untrusted == NULL) { |
1377 | 0 | xmlSecOpenSSLError("sk_X509_new_null", |
1378 | 0 | xmlSecKeyDataStoreGetName(store)); |
1379 | 0 | return(-1); |
1380 | 0 | } |
1381 | | |
1382 | 0 | ctx->crls = sk_X509_CRL_new_null(); |
1383 | 0 | if(ctx->crls == NULL) { |
1384 | 0 | xmlSecOpenSSLError("sk_X509_CRL_new_null", |
1385 | 0 | xmlSecKeyDataStoreGetName(store)); |
1386 | 0 | return(-1); |
1387 | 0 | } |
1388 | | |
1389 | 0 | ctx->vpm = X509_VERIFY_PARAM_new(); |
1390 | 0 | if(ctx->vpm == NULL) { |
1391 | 0 | xmlSecOpenSSLError("X509_VERIFY_PARAM_new", |
1392 | 0 | xmlSecKeyDataStoreGetName(store)); |
1393 | 0 | return(-1); |
1394 | 0 | } |
1395 | 0 | X509_VERIFY_PARAM_set_depth(ctx->vpm, 9); /* the default cert verification path in openssl */ |
1396 | 0 | X509_STORE_set1_param(ctx->xst, ctx->vpm); |
1397 | | |
1398 | |
|
1399 | 0 | return(0); |
1400 | 0 | } |
1401 | | |
1402 | | static void |
1403 | 0 | xmlSecOpenSSLX509StoreFinalize(xmlSecKeyDataStorePtr store) { |
1404 | 0 | xmlSecOpenSSLX509StoreCtxPtr ctx; |
1405 | 0 | xmlSecAssert(xmlSecKeyDataStoreCheckId(store, xmlSecOpenSSLX509StoreId)); |
1406 | |
|
1407 | 0 | ctx = xmlSecOpenSSLX509StoreGetCtx(store); |
1408 | 0 | xmlSecAssert(ctx != NULL); |
1409 | | |
1410 | |
|
1411 | 0 | if(ctx->xst != NULL) { |
1412 | 0 | X509_STORE_free(ctx->xst); |
1413 | 0 | } |
1414 | 0 | if(ctx->untrusted != NULL) { |
1415 | 0 | sk_X509_pop_free(ctx->untrusted, X509_free); |
1416 | 0 | } |
1417 | 0 | if(ctx->crls != NULL) { |
1418 | 0 | sk_X509_CRL_pop_free(ctx->crls, X509_CRL_free); |
1419 | 0 | } |
1420 | 0 | if(ctx->vpm != NULL) { |
1421 | 0 | X509_VERIFY_PARAM_free(ctx->vpm); |
1422 | 0 | } |
1423 | |
|
1424 | 0 | memset(ctx, 0, sizeof(xmlSecOpenSSLX509StoreCtx)); |
1425 | 0 | } |
1426 | | |
1427 | | |
1428 | | /****************************************************************************** |
1429 | | * |
1430 | | * Low-level x509 functions |
1431 | | * |
1432 | | *****************************************************************************/ |
1433 | | static X509* |
1434 | 0 | xmlSecOpenSSLX509FindTrustedIssuer(X509_STORE* xst, XMLSEC_OPENSSL400_CONST X509_NAME* issuer) { |
1435 | 0 | STACK_OF(X509_OBJECT)* objects = NULL; |
1436 | 0 | xmlSecOpenSSLSizeT ii, num; |
1437 | 0 | X509* issuer_cert = NULL; |
1438 | |
|
1439 | 0 | xmlSecAssert2(xst != NULL, NULL); |
1440 | 0 | xmlSecAssert2(issuer != NULL, NULL); |
1441 | | |
1442 | | /* Get all objects from the trusted store */ |
1443 | | #if defined(XMLSEC_OPENSSL_API_350) |
1444 | | objects = X509_STORE_get1_objects(xst); |
1445 | | if(objects == NULL) { |
1446 | | return(NULL); |
1447 | | } |
1448 | | #else /* defined(XMLSEC_OPENSSL_API_350) */ |
1449 | 0 | objects = X509_STORE_get0_objects(xst); |
1450 | 0 | if(objects == NULL) { |
1451 | 0 | return(NULL); |
1452 | 0 | } |
1453 | 0 | #endif /* defined(XMLSEC_OPENSSL_API_350) */ |
1454 | | |
1455 | | /* Search for a certificate with matching subject */ |
1456 | 0 | num = sk_X509_OBJECT_num(objects); |
1457 | 0 | for(ii = 0; ii < num; ++ii) { |
1458 | 0 | X509_OBJECT* obj = sk_X509_OBJECT_value(objects, ii); |
1459 | 0 | X509* cert; |
1460 | 0 | XMLSEC_OPENSSL400_CONST X509_NAME* cert_subject; |
1461 | |
|
1462 | 0 | if(obj == NULL) { |
1463 | 0 | continue; |
1464 | 0 | } |
1465 | | |
1466 | | /* Check if this object is a certificate */ |
1467 | 0 | if(X509_OBJECT_get_type(obj) != X509_LU_X509) { |
1468 | 0 | continue; |
1469 | 0 | } |
1470 | | |
1471 | 0 | cert = X509_OBJECT_get0_X509(obj); |
1472 | 0 | if(cert == NULL) { |
1473 | 0 | continue; |
1474 | 0 | } |
1475 | | |
1476 | 0 | cert_subject = X509_get_subject_name(cert); |
1477 | 0 | if(cert_subject == NULL) { |
1478 | 0 | continue; |
1479 | 0 | } |
1480 | | |
1481 | | /* Check if subject matches the issuer we're looking for */ |
1482 | 0 | if(X509_NAME_cmp(cert_subject, issuer) == 0) { |
1483 | | /* Found a match, duplicate and return */ |
1484 | 0 | issuer_cert = X509_dup(cert); |
1485 | 0 | if(issuer_cert == NULL) { |
1486 | 0 | xmlSecOpenSSLError("X509_dup", NULL); |
1487 | 0 | } |
1488 | 0 | break; |
1489 | 0 | } |
1490 | 0 | } |
1491 | | |
1492 | | /* list returned by X509_STORE_get1_objects needs to be freed */ |
1493 | | #if defined(XMLSEC_OPENSSL_API_350) |
1494 | | sk_X509_OBJECT_pop_free(objects, X509_OBJECT_free); |
1495 | | #endif /* defined(XMLSEC_OPENSSL_API_350) */ |
1496 | | |
1497 | | /* done */ |
1498 | 0 | return(issuer_cert); |
1499 | 0 | } |
1500 | | |
1501 | | static X509* |
1502 | 0 | xmlSecOpenSSLX509FindUntrustedIssuer(XMLSEC_OPENSSL400_CONST X509_NAME* issuer, X509_STORE* xst, X509_STORE_CTX* xsc, STACK_OF(X509)* untrusted, xmlSecKeyInfoCtx* keyInfoCtx) { |
1503 | 0 | X509* issuer_cert = NULL; |
1504 | 0 | xmlSecOpenSSLSizeT ii, num; |
1505 | 0 | int ret; |
1506 | 0 | int ctx_initialized = 0; |
1507 | |
|
1508 | 0 | xmlSecAssert2(xst != NULL, NULL); |
1509 | 0 | xmlSecAssert2(xsc != NULL, NULL); |
1510 | 0 | xmlSecAssert2(issuer != NULL, NULL); |
1511 | 0 | xmlSecAssert2(keyInfoCtx != NULL, NULL); |
1512 | |
|
1513 | 0 | if(untrusted == NULL) { |
1514 | 0 | return(NULL); |
1515 | 0 | } |
1516 | | |
1517 | 0 | num = sk_X509_num(untrusted); |
1518 | 0 | for(ii = 0; ii < num; ++ii) { |
1519 | 0 | X509* cert = sk_X509_value(untrusted, ii); |
1520 | 0 | XMLSEC_OPENSSL400_CONST X509_NAME* cert_subject; |
1521 | |
|
1522 | 0 | if(cert == NULL) { |
1523 | 0 | continue; |
1524 | 0 | } |
1525 | | |
1526 | 0 | cert_subject = X509_get_subject_name(cert); |
1527 | 0 | if(cert_subject == NULL) { |
1528 | 0 | continue; |
1529 | 0 | } |
1530 | | |
1531 | | /* Check if subject matches the issuer we're looking for */ |
1532 | 0 | if(X509_NAME_cmp(cert_subject, issuer) != 0) { |
1533 | 0 | continue; |
1534 | 0 | } |
1535 | | |
1536 | | /* Found a candidate, verify the chain to a trusted root using passed xsc */ |
1537 | 0 | ret = X509_STORE_CTX_init(xsc, xst, cert, untrusted); |
1538 | 0 | if(ret != 1) { |
1539 | 0 | xmlSecOpenSSLError("X509_STORE_CTX_init", NULL); |
1540 | 0 | goto done; |
1541 | 0 | } |
1542 | 0 | ctx_initialized = 1; |
1543 | |
|
1544 | 0 | ret = xmlSecOpenSSLX509StoreSetCtx(xsc, keyInfoCtx); |
1545 | 0 | if(ret < 0) { |
1546 | 0 | xmlSecInternalError("xmlSecOpenSSLX509StoreSetCtx", NULL); |
1547 | 0 | goto done; |
1548 | 0 | } |
1549 | | |
1550 | 0 | ret = X509_verify_cert(xsc); |
1551 | 0 | if(ret == 1) { |
1552 | | /* Chain verified successfully, return a copy */ |
1553 | 0 | issuer_cert = X509_dup(cert); |
1554 | 0 | if(issuer_cert == NULL) { |
1555 | 0 | xmlSecOpenSSLError("X509_dup", NULL); |
1556 | 0 | } |
1557 | 0 | goto done; |
1558 | 0 | } |
1559 | | |
1560 | | /* Chain verification failed, try next candidate */ |
1561 | 0 | X509_STORE_CTX_cleanup(xsc); |
1562 | 0 | ctx_initialized = 0; |
1563 | 0 | } |
1564 | | |
1565 | 0 | done: |
1566 | 0 | if(ctx_initialized != 0) { |
1567 | 0 | X509_STORE_CTX_cleanup(xsc); |
1568 | 0 | } |
1569 | 0 | return(issuer_cert); |
1570 | 0 | } |
1571 | | |
1572 | | static X509* |
1573 | 0 | xmlSecOpenSSLX509FindIssuer(XMLSEC_OPENSSL400_CONST X509_NAME* issuer, X509_STORE* xst, X509_STORE_CTX* xsc, STACK_OF(X509)* untrusted, xmlSecKeyInfoCtx* keyInfoCtx) { |
1574 | 0 | X509* issuer_cert = NULL; |
1575 | |
|
1576 | 0 | xmlSecAssert2(xst != NULL, NULL); |
1577 | 0 | xmlSecAssert2(xsc != NULL, NULL); |
1578 | 0 | xmlSecAssert2(issuer != NULL, NULL); |
1579 | 0 | xmlSecAssert2(keyInfoCtx != NULL, NULL); |
1580 | | |
1581 | | /* First, search in the untrusted certificates */ |
1582 | 0 | issuer_cert = xmlSecOpenSSLX509FindUntrustedIssuer(issuer, xst, xsc, untrusted, keyInfoCtx); |
1583 | 0 | if(issuer_cert != NULL) { |
1584 | 0 | return(issuer_cert); |
1585 | 0 | } |
1586 | | |
1587 | | /* Not found in untrusted certs, search in trusted store */ |
1588 | 0 | issuer_cert = xmlSecOpenSSLX509FindTrustedIssuer(xst, issuer); |
1589 | 0 | if(issuer_cert != NULL) { |
1590 | 0 | return(issuer_cert); |
1591 | 0 | } |
1592 | | |
1593 | | /* no luck */ |
1594 | 0 | return(NULL); |
1595 | 0 | } |
1596 | | |
1597 | | static int |
1598 | 0 | xmlSecOpenSSLX509VerifyCRLTimeValidity(X509_CRL *crl, xmlSecKeyInfoCtx* keyInfoCtx) { |
1599 | 0 | const ASN1_TIME *thisUpdate, *nextUpdate; |
1600 | 0 | time_t verification_time; |
1601 | 0 | int ret; |
1602 | |
|
1603 | 0 | xmlSecAssert2(crl != NULL, -1); |
1604 | 0 | xmlSecAssert2(keyInfoCtx != NULL, -1); |
1605 | | |
1606 | | /* Get verification time */ |
1607 | 0 | verification_time = (keyInfoCtx->certsVerificationTime > 0) ? |
1608 | 0 | keyInfoCtx->certsVerificationTime : time(NULL); |
1609 | |
|
1610 | 0 | thisUpdate = X509_CRL_get0_lastUpdate(crl); |
1611 | 0 | nextUpdate = X509_CRL_get0_nextUpdate(crl); |
1612 | | |
1613 | | /* Verify thisUpdate */ |
1614 | 0 | if(thisUpdate != NULL) { |
1615 | 0 | ret = xmlSecOpenSSLAsn1TimeCmp(thisUpdate, &verification_time); |
1616 | 0 | if(ret == 0) { |
1617 | 0 | xmlSecOpenSSLError("X509_cmp_time(thisUpdate)", NULL); |
1618 | 0 | return(-1); |
1619 | 0 | } |
1620 | 0 | if(ret > 0) { |
1621 | | /* thisUpdate > verification_time: CRL not yet valid */ |
1622 | 0 | char issuer[256]; |
1623 | 0 | xmlSecOpenSSLX509NameToString(X509_CRL_get_issuer(crl), issuer, sizeof(issuer)); |
1624 | 0 | xmlSecOtherError2(XMLSEC_ERRORS_R_CRL_NOT_YET_VALID, NULL, |
1625 | 0 | "issuer=%s", issuer); |
1626 | 0 | return(0); |
1627 | 0 | } |
1628 | 0 | } |
1629 | | |
1630 | | /* Verify nextUpdate */ |
1631 | 0 | if(nextUpdate != NULL) { |
1632 | 0 | ret = xmlSecOpenSSLAsn1TimeCmp(nextUpdate, &verification_time); |
1633 | 0 | if(ret <= 0) { |
1634 | | /* nextUpdate <= verification_time: CRL expired */ |
1635 | 0 | char issuer[256]; |
1636 | 0 | xmlSecOpenSSLX509NameToString(X509_CRL_get_issuer(crl), issuer, sizeof(issuer)); |
1637 | 0 | xmlSecOtherError2(XMLSEC_ERRORS_R_CRL_HAS_EXPIRED, NULL, "issuer=%s", issuer); |
1638 | 0 | return(0); |
1639 | 0 | } |
1640 | 0 | } |
1641 | | |
1642 | | /* Success */ |
1643 | 0 | return(1); |
1644 | 0 | } |
1645 | | |
1646 | | static int |
1647 | 0 | xmlSecOpenSSLX509VerifyCRLSignature(X509_STORE* xst, X509_STORE_CTX* xsc, STACK_OF(X509)* untrusted, X509_CRL *crl, xmlSecKeyInfoCtx* keyInfoCtx) { |
1648 | 0 | X509 *issuer_cert = NULL; |
1649 | 0 | EVP_PKEY *pKey = NULL; |
1650 | 0 | int ret; |
1651 | 0 | int res = -1; |
1652 | |
|
1653 | 0 | xmlSecAssert2(xst != NULL, -1); |
1654 | 0 | xmlSecAssert2(xsc != NULL, -1); |
1655 | 0 | xmlSecAssert2(crl != NULL, -1); |
1656 | 0 | xmlSecAssert2(keyInfoCtx != NULL, -1); |
1657 | | |
1658 | | /* Find the CRL issuer certificate (searches untrusted first, then trusted) */ |
1659 | 0 | issuer_cert = xmlSecOpenSSLX509FindIssuer(X509_CRL_get_issuer(crl), xst, xsc, untrusted, keyInfoCtx); |
1660 | 0 | if(issuer_cert == NULL) { |
1661 | 0 | char issuer[256]; |
1662 | 0 | xmlSecOpenSSLX509NameToString(X509_CRL_get_issuer(crl), issuer, sizeof(issuer)); |
1663 | 0 | xmlSecOtherError2(XMLSEC_ERRORS_R_CERT_NOT_FOUND, NULL, "issuer=%s", issuer); |
1664 | 0 | goto done; |
1665 | 0 | } |
1666 | | |
1667 | 0 | pKey = X509_get_pubkey(issuer_cert); |
1668 | 0 | if(pKey == NULL) { |
1669 | 0 | xmlSecOpenSSLError("X509_get_pubkey", NULL); |
1670 | 0 | goto done; |
1671 | 0 | } |
1672 | | |
1673 | 0 | ret = X509_CRL_verify(crl, pKey); |
1674 | 0 | if(ret < 0) { |
1675 | 0 | xmlSecOpenSSLError("X509_CRL_verify", NULL); |
1676 | 0 | goto done; |
1677 | 0 | } else if(ret == 0) { |
1678 | 0 | char issuer[256]; |
1679 | | |
1680 | | /* cert was not verified */ |
1681 | 0 | xmlSecOpenSSLX509NameToString(X509_CRL_get_issuer(crl), issuer, sizeof(issuer)); |
1682 | 0 | xmlSecOtherError2(XMLSEC_ERRORS_R_CRL_VERIFY_FAILED, NULL, "issuer=%s", issuer); |
1683 | | |
1684 | | /* not verified */ |
1685 | 0 | res = 0; |
1686 | 0 | goto done; |
1687 | 0 | } |
1688 | | |
1689 | | /* success: verified */ |
1690 | 0 | res = 1; |
1691 | |
|
1692 | 0 | done: |
1693 | 0 | if(pKey != NULL) { |
1694 | 0 | EVP_PKEY_free(pKey); |
1695 | 0 | } |
1696 | 0 | if(issuer_cert != NULL) { |
1697 | 0 | X509_free(issuer_cert); |
1698 | 0 | } |
1699 | 0 | return(res); |
1700 | 0 | } |
1701 | | |
1702 | | static int |
1703 | 0 | xmlSecOpenSSLX509VerifyCRL(X509_STORE* xst, X509_STORE_CTX* xsc, STACK_OF(X509)* untrusted, X509_CRL *crl, xmlSecKeyInfoCtx* keyInfoCtx) { |
1704 | 0 | int ret; |
1705 | |
|
1706 | 0 | xmlSecAssert2(xst != NULL, -1); |
1707 | 0 | xmlSecAssert2(xsc != NULL, -1); |
1708 | 0 | xmlSecAssert2(crl != NULL, -1); |
1709 | 0 | xmlSecAssert2(keyInfoCtx != NULL, -1); |
1710 | | |
1711 | | /* Verify time validity first (fast check) */ |
1712 | 0 | ret = xmlSecOpenSSLX509VerifyCRLTimeValidity(crl, keyInfoCtx); |
1713 | 0 | if(ret < 0) { |
1714 | 0 | xmlSecInternalError("xmlSecOpenSSLX509VerifyCRLTimeValidity", NULL); |
1715 | 0 | return(-1); |
1716 | 0 | } else if(ret != 1) { |
1717 | | /* Time validity check failed */ |
1718 | 0 | return(0); |
1719 | 0 | } |
1720 | | |
1721 | | /* Verify CRL signature (slower check) */ |
1722 | 0 | ret = xmlSecOpenSSLX509VerifyCRLSignature(xst, xsc, untrusted, crl, keyInfoCtx); |
1723 | 0 | if(ret < 0) { |
1724 | 0 | xmlSecInternalError("xmlSecOpenSSLX509VerifyCRLSignature", NULL); |
1725 | 0 | return(-1); |
1726 | 0 | } else if(ret != 1) { |
1727 | | /* Signature verification failed */ |
1728 | 0 | return(0); |
1729 | 0 | } |
1730 | | |
1731 | | /* success: verified */ |
1732 | 0 | return(1); |
1733 | 0 | } |
1734 | | |
1735 | | int |
1736 | | xmlSecOpenSSLX509FindCertCtxInitialize(xmlSecOpenSSLX509FindCertCtxPtr ctx, |
1737 | | const xmlChar *subjectName, |
1738 | | const xmlChar *issuerName, const xmlChar *issuerSerial, |
1739 | | const xmlSecByte * ski, xmlSecSize skiSize |
1740 | 0 | ) { |
1741 | 0 | int skiLen; |
1742 | 0 | int ret; |
1743 | |
|
1744 | 0 | xmlSecAssert2(ctx != NULL, -1); |
1745 | |
|
1746 | 0 | memset(ctx, 0, sizeof(*ctx)); |
1747 | | |
1748 | | /* cast first */ |
1749 | 0 | XMLSEC_SAFE_CAST_SIZE_TO_INT(skiSize, skiLen, return(-1), NULL); |
1750 | | |
1751 | | /* Subject name */ |
1752 | 0 | if(subjectName != NULL) { |
1753 | 0 | ctx->subjectName = xmlSecOpenSSLX509NameRead(subjectName); |
1754 | 0 | if(ctx->subjectName == NULL) { |
1755 | 0 | xmlSecInternalError2("xmlSecOpenSSLX509NameRead", NULL, |
1756 | 0 | "subject=%s", xmlSecErrorsSafeString(subjectName)); |
1757 | 0 | xmlSecOpenSSLX509FindCertCtxFinalize(ctx); |
1758 | 0 | return(-1); |
1759 | 0 | } |
1760 | 0 | } |
1761 | | |
1762 | | /* Issuer name / serial */ |
1763 | 0 | if((issuerName != NULL) && (issuerSerial != NULL)) { |
1764 | 0 | BIGNUM *bn = NULL; |
1765 | |
|
1766 | 0 | ctx->issuerName = xmlSecOpenSSLX509NameRead(issuerName); |
1767 | 0 | if(ctx->issuerName == NULL) { |
1768 | 0 | xmlSecInternalError2("xmlSecOpenSSLX509NameRead", NULL, |
1769 | 0 | "issuer=%s", xmlSecErrorsSafeString(issuerName)); |
1770 | 0 | xmlSecOpenSSLX509FindCertCtxFinalize(ctx); |
1771 | 0 | return(-1); |
1772 | 0 | } |
1773 | | |
1774 | 0 | bn = BN_new(); |
1775 | 0 | if(bn == NULL) { |
1776 | 0 | xmlSecOpenSSLError("BN_new", NULL); |
1777 | 0 | xmlSecOpenSSLX509FindCertCtxFinalize(ctx); |
1778 | 0 | return(-1); |
1779 | 0 | } |
1780 | 0 | if(BN_dec2bn(&bn, (char*)issuerSerial) == 0) { |
1781 | 0 | xmlSecOpenSSLError("BN_dec2bn", NULL); |
1782 | 0 | BN_clear_free(bn); |
1783 | 0 | xmlSecOpenSSLX509FindCertCtxFinalize(ctx); |
1784 | 0 | return(-1); |
1785 | 0 | } |
1786 | 0 | ctx->issuerSerial = BN_to_ASN1_INTEGER(bn, NULL); |
1787 | 0 | if(ctx->issuerSerial == NULL) { |
1788 | 0 | xmlSecOpenSSLError("BN_to_ASN1_INTEGER", NULL); |
1789 | 0 | BN_clear_free(bn); |
1790 | 0 | xmlSecOpenSSLX509FindCertCtxFinalize(ctx); |
1791 | 0 | return(-1); |
1792 | 0 | } |
1793 | 0 | BN_clear_free(bn); |
1794 | 0 | } |
1795 | | |
1796 | | /* SKI */ |
1797 | 0 | if((ski != NULL) && (skiLen > 0)) { |
1798 | 0 | ctx->ski = ASN1_OCTET_STRING_new(); |
1799 | 0 | if(ctx->ski == NULL) { |
1800 | 0 | xmlSecOpenSSLError("ASN1_OCTET_STRING_new", NULL); |
1801 | 0 | xmlSecOpenSSLX509FindCertCtxFinalize(ctx); |
1802 | 0 | return(-1); |
1803 | 0 | } |
1804 | 0 | ret = ASN1_OCTET_STRING_set(ctx->ski, ski, skiLen); |
1805 | 0 | if(ret != 1) { |
1806 | 0 | xmlSecOpenSSLError("ASN1_OCTET_STRING_set", NULL); |
1807 | 0 | xmlSecOpenSSLX509FindCertCtxFinalize(ctx); |
1808 | 0 | return(-1); |
1809 | 0 | } |
1810 | 0 | } |
1811 | | |
1812 | | |
1813 | | /* done! */ |
1814 | 0 | return(0); |
1815 | 0 | } |
1816 | | |
1817 | | int |
1818 | 0 | xmlSecOpenSSLX509FindCertCtxInitializeFromValue(xmlSecOpenSSLX509FindCertCtxPtr ctx, xmlSecKeyX509DataValuePtr x509Value) { |
1819 | 0 | int ret; |
1820 | |
|
1821 | 0 | xmlSecAssert2(ctx != NULL, -1); |
1822 | 0 | xmlSecAssert2(x509Value != NULL, -1); |
1823 | |
|
1824 | 0 | ret = xmlSecOpenSSLX509FindCertCtxInitialize(ctx, |
1825 | 0 | x509Value->subject, |
1826 | 0 | x509Value->issuerName, x509Value->issuerSerial, |
1827 | 0 | xmlSecBufferGetData(&(x509Value->ski)), xmlSecBufferGetSize(&(x509Value->ski)) |
1828 | 0 | ); |
1829 | 0 | if(ret < 0) { |
1830 | 0 | xmlSecInternalError("xmlSecOpenSSLX509FindCertCtxInitialize", NULL); |
1831 | 0 | xmlSecOpenSSLX509FindCertCtxFinalize(ctx); |
1832 | 0 | return(-1); |
1833 | 0 | } |
1834 | | |
1835 | 0 | if((!xmlSecBufferIsEmpty(&(x509Value->digest))) && (x509Value->digestAlgorithm != NULL)) { |
1836 | 0 | xmlSecSize digestSize; |
1837 | |
|
1838 | 0 | ctx->digestValue = xmlSecBufferGetData(&(x509Value->digest)); |
1839 | 0 | digestSize = xmlSecBufferGetSize(&(x509Value->digest)); |
1840 | 0 | XMLSEC_SAFE_CAST_SIZE_TO_UINT(digestSize, ctx->digestLen, return(-1), NULL); |
1841 | |
|
1842 | 0 | ctx->digestMd = xmlSecOpenSSLX509GetDigestFromAlgorithm(x509Value->digestAlgorithm); |
1843 | 0 | if(ctx->digestMd == NULL) { |
1844 | 0 | xmlSecInternalError("xmlSecOpenSSLX509GetDigestFromAlgorithm", NULL); |
1845 | 0 | xmlSecOpenSSLX509FindCertCtxFinalize(ctx); |
1846 | 0 | return(-1); |
1847 | 0 | } |
1848 | 0 | } |
1849 | | |
1850 | 0 | return(0); |
1851 | 0 | } |
1852 | | |
1853 | 0 | void xmlSecOpenSSLX509FindCertCtxFinalize(xmlSecOpenSSLX509FindCertCtxPtr ctx) { |
1854 | 0 | xmlSecAssert(ctx != NULL); |
1855 | |
|
1856 | 0 | if(ctx->subjectName != NULL) { |
1857 | 0 | X509_NAME_free(ctx->subjectName); |
1858 | 0 | } |
1859 | 0 | if(ctx->issuerName != NULL) { |
1860 | 0 | X509_NAME_free(ctx->issuerName); |
1861 | 0 | } |
1862 | 0 | if(ctx->issuerSerial != NULL) { |
1863 | 0 | ASN1_INTEGER_free(ctx->issuerSerial); |
1864 | 0 | } |
1865 | 0 | if(ctx->ski != NULL) { |
1866 | 0 | ASN1_OCTET_STRING_free(ctx->ski); |
1867 | 0 | } |
1868 | 0 | memset(ctx, 0, sizeof(*ctx)); |
1869 | 0 | } |
1870 | | |
1871 | | |
1872 | | static int |
1873 | 0 | xmlSecOpenSSLX509MatchBySubjectName(X509* cert, XMLSEC_OPENSSL400_CONST X509_NAME* subjectName) { |
1874 | 0 | XMLSEC_OPENSSL400_CONST X509_NAME * certSubjectName; |
1875 | 0 | int ret; |
1876 | |
|
1877 | 0 | xmlSecAssert2(cert != NULL, -1); |
1878 | |
|
1879 | 0 | if(subjectName == NULL) { |
1880 | 0 | return(0); |
1881 | 0 | } |
1882 | | |
1883 | 0 | certSubjectName = X509_get_subject_name(cert); |
1884 | 0 | if(certSubjectName == NULL) { |
1885 | 0 | return(0); |
1886 | 0 | } |
1887 | | |
1888 | | /* returns 0 if equal */ |
1889 | 0 | ret = xmlSecOpenSSLX509NamesCompare(subjectName, certSubjectName); |
1890 | 0 | if(ret != 0) { |
1891 | 0 | return(0); |
1892 | 0 | } |
1893 | | |
1894 | | /* success */ |
1895 | 0 | return(1); |
1896 | 0 | } |
1897 | | |
1898 | | static int |
1899 | 0 | xmlSecOpenSSLX509MatchByIssuer(X509* cert, XMLSEC_OPENSSL400_CONST X509_NAME* issuerName, ASN1_INTEGER* issuerSerial) { |
1900 | 0 | ASN1_INTEGER* certSerial; |
1901 | 0 | XMLSEC_OPENSSL400_CONST X509_NAME* certName; |
1902 | |
|
1903 | 0 | xmlSecAssert2(cert != NULL, -1); |
1904 | |
|
1905 | 0 | if((issuerName == NULL) || (issuerSerial == NULL)) { |
1906 | 0 | return(0); |
1907 | 0 | } |
1908 | | |
1909 | 0 | certSerial = X509_get_serialNumber(cert); |
1910 | 0 | if((certSerial == NULL) || (ASN1_INTEGER_cmp(certSerial, issuerSerial) != 0)) { |
1911 | 0 | return(0); |
1912 | 0 | } |
1913 | 0 | certName = X509_get_issuer_name(cert); |
1914 | 0 | if((certName == NULL) || (xmlSecOpenSSLX509NamesCompare(certName, issuerName) != 0)) { |
1915 | 0 | return(0); |
1916 | 0 | } |
1917 | | |
1918 | | /* success */ |
1919 | 0 | return(1); |
1920 | 0 | } |
1921 | | |
1922 | | static int |
1923 | 0 | xmlSecOpenSSLX509MatchBySki(X509* cert, ASN1_OCTET_STRING* ski) { |
1924 | 0 | XMLSEC_OPENSSL400_CONST X509_EXTENSION* ext; |
1925 | 0 | ASN1_OCTET_STRING* keyId; |
1926 | 0 | int ret; |
1927 | 0 | int index; |
1928 | |
|
1929 | 0 | xmlSecAssert2(cert != NULL, -1); |
1930 | |
|
1931 | 0 | if(ski == NULL){ |
1932 | 0 | return(0); |
1933 | 0 | } |
1934 | | |
1935 | 0 | index = X509_get_ext_by_NID(cert, NID_subject_key_identifier, -1); |
1936 | 0 | if(index < 0) { |
1937 | 0 | return(0); |
1938 | 0 | } |
1939 | 0 | ext = X509_get_ext(cert, index); |
1940 | 0 | if(ext == NULL) { |
1941 | 0 | return(0); |
1942 | 0 | } |
1943 | 0 | keyId = (ASN1_OCTET_STRING *)X509V3_EXT_d2i(ext); |
1944 | 0 | if(keyId == NULL) { |
1945 | 0 | return(0); |
1946 | 0 | } |
1947 | | |
1948 | 0 | ret = ASN1_OCTET_STRING_cmp(keyId, ski); |
1949 | 0 | if(ret != 0) { |
1950 | 0 | ASN1_OCTET_STRING_free(keyId); |
1951 | 0 | return(0); |
1952 | 0 | } |
1953 | 0 | ASN1_OCTET_STRING_free(keyId); |
1954 | | |
1955 | | /* success */ |
1956 | 0 | return(1); |
1957 | 0 | } |
1958 | | |
1959 | | static int |
1960 | 0 | xmlSecOpenSSLX509MatchByDigest(X509* cert, const xmlSecByte * digestValue, unsigned int digestLen, const EVP_MD* digest) { |
1961 | 0 | xmlSecByte md[EVP_MAX_MD_SIZE]; |
1962 | 0 | unsigned int len = 0; |
1963 | 0 | int ret; |
1964 | |
|
1965 | 0 | xmlSecAssert2(cert != NULL, -1); |
1966 | |
|
1967 | 0 | if((digestValue == NULL) || (digestLen <= 0) || (digest == NULL)) { |
1968 | 0 | return(0); |
1969 | 0 | } |
1970 | | |
1971 | 0 | ret = X509_digest(cert, digest, md, &len); |
1972 | 0 | if((ret != 1) || (len <= 0)) { |
1973 | 0 | xmlSecOpenSSLError("X509_digest", NULL); |
1974 | 0 | return(-1); |
1975 | 0 | } |
1976 | | |
1977 | 0 | if((len != digestLen) || (memcmp(md, digestValue, digestLen) != 0)) { |
1978 | 0 | return(0); |
1979 | 0 | } |
1980 | | |
1981 | | /* success */ |
1982 | 0 | return(1); |
1983 | 0 | } |
1984 | | |
1985 | | /* returns 1 for match, 0 for no match, and a negative value if an error occurs */ |
1986 | | int |
1987 | 0 | xmlSecOpenSSLX509FindCertCtxMatch(xmlSecOpenSSLX509FindCertCtxPtr ctx, X509* cert) { |
1988 | 0 | int ret; |
1989 | |
|
1990 | 0 | xmlSecAssert2(ctx != NULL, -1); |
1991 | 0 | xmlSecAssert2(cert != NULL, -1); |
1992 | |
|
1993 | 0 | ret = xmlSecOpenSSLX509MatchBySubjectName(cert, ctx->subjectName); |
1994 | 0 | if(ret < 0) { |
1995 | 0 | xmlSecInternalError("xmlSecOpenSSLX509MatchBySubjectName", NULL); |
1996 | 0 | return(-1); |
1997 | 0 | } else if(ret == 1) { |
1998 | | /* success! */ |
1999 | 0 | return(1); |
2000 | 0 | } |
2001 | | |
2002 | 0 | ret = xmlSecOpenSSLX509MatchByIssuer(cert, ctx->issuerName, ctx->issuerSerial); |
2003 | 0 | if(ret < 0) { |
2004 | 0 | xmlSecInternalError("xmlSecOpenSSLX509MatchByIssuer", NULL); |
2005 | 0 | return(-1); |
2006 | 0 | } else if(ret == 1) { |
2007 | | /* success! */ |
2008 | 0 | return(1); |
2009 | 0 | } |
2010 | | |
2011 | 0 | ret = xmlSecOpenSSLX509MatchBySki(cert, ctx->ski); |
2012 | 0 | if(ret < 0) { |
2013 | 0 | xmlSecInternalError("xmlSecOpenSSLX509MatchBySki", NULL); |
2014 | 0 | return(-1); |
2015 | 0 | } else if(ret == 1) { |
2016 | | /* success! */ |
2017 | 0 | return(1); |
2018 | 0 | } |
2019 | | |
2020 | 0 | ret = xmlSecOpenSSLX509MatchByDigest(cert, ctx->digestValue, ctx->digestLen, ctx->digestMd); |
2021 | 0 | if(ret < 0) { |
2022 | 0 | xmlSecInternalError("xmlSecOpenSSLX509MatchByDigest", NULL); |
2023 | 0 | return(-1); |
2024 | 0 | } else if(ret == 1) { |
2025 | | /* success! */ |
2026 | 0 | return(1); |
2027 | 0 | } |
2028 | | |
2029 | | /* not found */ |
2030 | 0 | return(0); |
2031 | 0 | } |
2032 | | |
2033 | | static unsigned long |
2034 | 0 | xmlSecOpenSSLX509GetSubjectHash(X509* x) { |
2035 | 0 | XMLSEC_OPENSSL400_CONST X509_NAME* name; |
2036 | 0 | unsigned long res; |
2037 | |
|
2038 | 0 | xmlSecAssert2(x != NULL, 0); |
2039 | |
|
2040 | 0 | name = X509_get_subject_name(x); |
2041 | 0 | if(name == NULL) { |
2042 | 0 | xmlSecOpenSSLError("X509_get_subject_name", NULL); |
2043 | 0 | return(0); |
2044 | 0 | } |
2045 | | |
2046 | 0 | res = X509_NAME_hash_ex(name, xmlSecOpenSSLGetLibCtx(), NULL, NULL); |
2047 | 0 | if(res == 0) { |
2048 | 0 | xmlSecOpenSSLError("X509_NAME_hash_ex", NULL); |
2049 | 0 | return(0); |
2050 | 0 | } |
2051 | | |
2052 | 0 | return(res); |
2053 | 0 | } |
2054 | | |
2055 | | static unsigned long |
2056 | 0 | xmlSecOpenSSLX509GetIssuerHash(X509* x) { |
2057 | 0 | XMLSEC_OPENSSL400_CONST X509_NAME* name; |
2058 | 0 | unsigned long res; |
2059 | |
|
2060 | 0 | xmlSecAssert2(x != NULL, 0); |
2061 | |
|
2062 | 0 | name = X509_get_issuer_name(x); |
2063 | 0 | if(name == NULL) { |
2064 | 0 | xmlSecOpenSSLError("X509_get_issuer_name", NULL); |
2065 | 0 | return(0); |
2066 | 0 | } |
2067 | | |
2068 | 0 | res = X509_NAME_hash_ex(name, xmlSecOpenSSLGetLibCtx(), NULL, NULL); |
2069 | 0 | if(res == 0) { |
2070 | 0 | xmlSecOpenSSLError("X509_NAME_hash_ex", NULL); |
2071 | 0 | return(0); |
2072 | 0 | } |
2073 | | |
2074 | 0 | return(res); |
2075 | 0 | } |
2076 | | |
2077 | | /* new list doesn't OWN certs */ |
2078 | | static STACK_OF(X509)* |
2079 | 0 | xmlSecOpenSSLX509StoreCombineCerts(STACK_OF(X509)* certs1, STACK_OF(X509)* certs2) { |
2080 | 0 | #if defined(XMLSEC_OPENSSL_API_300) |
2081 | 0 | STACK_OF(X509)* res = NULL; |
2082 | 0 | int ret; |
2083 | |
|
2084 | 0 | res = sk_X509_new_null(); |
2085 | 0 | if (res == NULL) { |
2086 | 0 | xmlSecOpenSSLError("sk_X509_new_null()", NULL); |
2087 | 0 | return(NULL); |
2088 | 0 | } |
2089 | | |
2090 | | /* certs 1 */ |
2091 | 0 | ret = X509_add_certs(res, certs1, 0); |
2092 | 0 | if (ret != 1) { |
2093 | 0 | xmlSecOpenSSLError("X509_add_certs(certs1)", NULL); |
2094 | 0 | sk_X509_free(res); |
2095 | 0 | return(NULL); |
2096 | 0 | } |
2097 | | |
2098 | | |
2099 | | /* certs 2 */ |
2100 | 0 | ret = X509_add_certs(res, certs2, 0); |
2101 | 0 | if (ret != 1) { |
2102 | 0 | xmlSecOpenSSLError("X509_add_certs(certs2)", NULL); |
2103 | 0 | sk_X509_free(res); |
2104 | 0 | return(NULL); |
2105 | 0 | } |
2106 | | |
2107 | | /* done |
2108 | | */ |
2109 | 0 | return (res); |
2110 | |
|
2111 | | #else /* defined(XMLSEC_OPENSSL_API_300) */ |
2112 | | STACK_OF(X509)* res = NULL; |
2113 | | |
2114 | | /* certs1 */ |
2115 | | if((res == NULL) && (certs1 != NULL)) { |
2116 | | res = sk_X509_dup(certs1); |
2117 | | if(res == NULL) { |
2118 | | xmlSecOpenSSLError("sk_X509_dup(certs1)", NULL); |
2119 | | return(NULL); |
2120 | | } |
2121 | | } |
2122 | | |
2123 | | /* certs2 */ |
2124 | | if((res == NULL) && (certs2 != NULL)) { |
2125 | | res = sk_X509_dup(certs2); |
2126 | | if(res == NULL) { |
2127 | | xmlSecOpenSSLError("sk_X509_dup(certs2)", NULL); |
2128 | | return(NULL); |
2129 | | } |
2130 | | } else if(certs2 != NULL) { |
2131 | | X509 * cert; |
2132 | | xmlSecOpenSSLSizeT ii, num; |
2133 | | xmlSecOpenSSLSizeT ret; |
2134 | | |
2135 | | /* append certs2 to result */ |
2136 | | num = sk_X509_num(certs2); |
2137 | | ret = sk_X509_reserve(res, num + sk_X509_num(res)); |
2138 | | if(ret != 1) { |
2139 | | xmlSecOpenSSLError2("sk_X509_reserve(res)", NULL, |
2140 | | "size=%d", (int)(num + sk_X509_num(res))); |
2141 | | sk_X509_free(res); |
2142 | | return(NULL); |
2143 | | } |
2144 | | |
2145 | | for(ii = 0; ii < num; ++ii) { |
2146 | | cert = sk_X509_value(certs2, ii); |
2147 | | if(cert == NULL) { |
2148 | | continue; |
2149 | | } |
2150 | | ret = sk_X509_push(res, cert); |
2151 | | if(ret <= 0) { |
2152 | | xmlSecInternalError("sk_X509_push(res)", NULL); |
2153 | | sk_X509_free(res); |
2154 | | return(NULL); |
2155 | | } |
2156 | | } |
2157 | | } |
2158 | | |
2159 | | /* done */ |
2160 | | return(res); |
2161 | | #endif /* defined(XMLSEC_OPENSSL_API_300) */ |
2162 | 0 | } |
2163 | | |
2164 | | |
2165 | | /* Try to find child for the cert (i.e. cert with an issuer matching cert subject) */ |
2166 | | static X509* |
2167 | 0 | xmlSecOpenSSLX509FindChildCert(STACK_OF(X509) *chain, X509 *cert) { |
2168 | 0 | unsigned long certNameHash; |
2169 | 0 | unsigned long certNameHash2; |
2170 | 0 | xmlSecOpenSSLSizeT ii; |
2171 | |
|
2172 | 0 | xmlSecAssert2(chain != NULL, NULL); |
2173 | 0 | xmlSecAssert2(cert != NULL, NULL); |
2174 | |
|
2175 | 0 | certNameHash = xmlSecOpenSSLX509GetSubjectHash(cert); |
2176 | 0 | if(certNameHash == 0) { |
2177 | 0 | xmlSecInternalError("xmlSecOpenSSLX509GetSubjectHash", NULL); |
2178 | 0 | return(NULL); |
2179 | 0 | } |
2180 | 0 | for(ii = 0; ii < sk_X509_num(chain); ++ii) { |
2181 | 0 | X509* cert_ii = sk_X509_value(chain, ii); |
2182 | 0 | xmlSecAssert2(cert_ii != NULL, NULL); |
2183 | |
|
2184 | 0 | if(cert == cert_ii) { |
2185 | | /* same cert, skip for self-signed certs */ |
2186 | 0 | continue; |
2187 | 0 | } |
2188 | | |
2189 | 0 | certNameHash2 = xmlSecOpenSSLX509GetSubjectHash(cert_ii); |
2190 | 0 | if(certNameHash2 == 0) { |
2191 | 0 | xmlSecInternalError("xmlSecOpenSSLX509GetSubjectHash", NULL); |
2192 | 0 | return(NULL); |
2193 | 0 | } |
2194 | 0 | if(certNameHash == certNameHash2) { |
2195 | | /* same cert but different copy, skip for self-signed certs */ |
2196 | 0 | continue; |
2197 | 0 | } |
2198 | | |
2199 | 0 | certNameHash2 = xmlSecOpenSSLX509GetIssuerHash(cert_ii); |
2200 | 0 | if(certNameHash2 == 0) { |
2201 | 0 | xmlSecInternalError("xmlSecOpenSSLX509GetIssuerHash", NULL); |
2202 | 0 | return(NULL); |
2203 | 0 | } |
2204 | 0 | if(certNameHash != certNameHash2) { |
2205 | | /* issuer doesn't match */ |
2206 | 0 | continue; |
2207 | 0 | } |
2208 | | |
2209 | | /* found it! cert_ii issuer matches cert */ |
2210 | 0 | return(cert_ii); |
2211 | 0 | } |
2212 | 0 | return(NULL); |
2213 | 0 | } |
2214 | | |
2215 | | static int |
2216 | | xmlSecOpenSSLX509NameReadCallback( |
2217 | | const xmlChar * name, |
2218 | | const xmlChar * value, |
2219 | | xmlSecSize valueSize, |
2220 | | int type, |
2221 | | void * context |
2222 | 0 | ) { |
2223 | 0 | X509_NAME *nm = NULL; |
2224 | 0 | int valueLen; |
2225 | 0 | int valueType; |
2226 | 0 | int ret; |
2227 | |
|
2228 | 0 | xmlSecAssert2(name != NULL, -1); |
2229 | 0 | xmlSecAssert2(value != NULL, -1); |
2230 | 0 | xmlSecAssert2(context != NULL, -1); |
2231 | |
|
2232 | 0 | nm = (X509_NAME *)context; |
2233 | 0 | xmlSecAssert2(nm != NULL, -1); |
2234 | |
|
2235 | 0 | switch(type) { |
2236 | 0 | case XMLSEC_X509_VALUE_TYPE_UF8_STRING: |
2237 | 0 | valueType = MBSTRING_UTF8 ; |
2238 | 0 | break; |
2239 | 0 | case XMLSEC_X509_VALUE_TYPE_OCTET_STRING: |
2240 | 0 | valueType = B_ASN1_OCTET_STRING; |
2241 | 0 | break; |
2242 | 0 | default: |
2243 | 0 | xmlSecInvalidIntegerDataError("type", type, "should be either utf8 or octet string", NULL); |
2244 | 0 | return(-1); |
2245 | 0 | } |
2246 | | |
2247 | | /* add to X509_NAME */ |
2248 | 0 | XMLSEC_SAFE_CAST_SIZE_TO_INT(valueSize, valueLen, return(-1), NULL); |
2249 | 0 | ret = X509_NAME_add_entry_by_txt(nm, (char*)name, valueType, value, valueLen, -1, 0); |
2250 | 0 | if(ret != 1) { |
2251 | 0 | xmlSecOpenSSLError3("X509_NAME_add_entry_by_txt", NULL, "name=%s; type=%d", xmlSecErrorsSafeString(name), type); |
2252 | 0 | return(-1); |
2253 | 0 | } |
2254 | | |
2255 | | /* success */ |
2256 | 0 | return(0); |
2257 | 0 | } |
2258 | | |
2259 | | |
2260 | | /* OpenSSL doesn't accept "E" so we need to replace it */ |
2261 | | static xmlSecx509NameReplacements xmlSecOpenSSLX509NameReplacements[] = { |
2262 | | { BAD_CAST "E", BAD_CAST "emailAddress"}, |
2263 | | { BAD_CAST "SERIALNUMBER", BAD_CAST "serialNumber"}, |
2264 | | { NULL, NULL } |
2265 | | }; |
2266 | | |
2267 | | static X509_NAME * |
2268 | 0 | xmlSecOpenSSLX509NameRead(const xmlChar *str) { |
2269 | 0 | X509_NAME *nm = NULL; |
2270 | 0 | int ret; |
2271 | |
|
2272 | 0 | xmlSecAssert2(str != NULL, NULL); |
2273 | |
|
2274 | 0 | nm = X509_NAME_new(); |
2275 | 0 | if(nm == NULL) { |
2276 | 0 | xmlSecOpenSSLError("X509_NAME_new", NULL); |
2277 | 0 | return(NULL); |
2278 | 0 | } |
2279 | | |
2280 | 0 | ret = xmlSecX509NameRead(str, xmlSecOpenSSLX509NameReplacements, xmlSecOpenSSLX509NameReadCallback, (void*)nm); |
2281 | 0 | if(ret < 0) { |
2282 | 0 | xmlSecInternalError("xmlSecX509NameRead", NULL); |
2283 | 0 | X509_NAME_free(nm); |
2284 | 0 | return(NULL); |
2285 | 0 | } |
2286 | | |
2287 | | /* succcess */ |
2288 | 0 | return(nm); |
2289 | 0 | } |
2290 | | |
2291 | | /* |
2292 | | * This function CREATES duplicates for X509_NAME_ENTRY objects! |
2293 | | */ |
2294 | | static STACK_OF(X509_NAME_ENTRY)* |
2295 | 0 | xmlSecOpenSSLX509_NAME_ENTRIES_copy(XMLSEC_OPENSSL400_CONST X509_NAME * a) { |
2296 | 0 | STACK_OF(X509_NAME_ENTRY) * res = NULL; |
2297 | 0 | int ii; |
2298 | 0 | xmlSecOpenSSLSizeT ret; |
2299 | |
|
2300 | 0 | res = sk_X509_NAME_ENTRY_new(xmlSecOpenSSLX509_NAME_ENTRY_cmp); |
2301 | 0 | if(res == NULL) { |
2302 | 0 | xmlSecOpenSSLError("sk_X509_NAME_ENTRY_new", NULL); |
2303 | 0 | return(NULL); |
2304 | 0 | } |
2305 | | |
2306 | 0 | for (ii = X509_NAME_entry_count(a) - 1; ii >= 0; --ii) { |
2307 | 0 | XMLSEC_OPENSSL400_CONST X509_NAME_ENTRY* entry = X509_NAME_get_entry(a, ii); |
2308 | 0 | X509_NAME_ENTRY* entry_dup = X509_NAME_ENTRY_dup(entry); |
2309 | 0 | if(entry_dup == NULL) { |
2310 | 0 | xmlSecOpenSSLError("X509_NAME_ENTRY_dup", NULL); |
2311 | 0 | sk_X509_NAME_ENTRY_pop_free(res, X509_NAME_ENTRY_free); |
2312 | 0 | return(NULL); |
2313 | 0 | } |
2314 | | |
2315 | 0 | ret = sk_X509_NAME_ENTRY_push(res, entry_dup); |
2316 | 0 | if(ret <= 0) { |
2317 | 0 | xmlSecOpenSSLError("sk_X509_NAME_ENTRY_push", NULL); |
2318 | 0 | sk_X509_NAME_ENTRY_pop_free(res, X509_NAME_ENTRY_free); |
2319 | 0 | return(NULL); |
2320 | 0 | } |
2321 | 0 | } |
2322 | | |
2323 | 0 | return (res); |
2324 | 0 | } |
2325 | | |
2326 | | /* returns 0 if equal */ |
2327 | | static |
2328 | 0 | int xmlSecOpenSSLX509_NAME_ENTRIES_cmp(STACK_OF(X509_NAME_ENTRY)* a, STACK_OF(X509_NAME_ENTRY)* b) { |
2329 | 0 | const X509_NAME_ENTRY *na; |
2330 | 0 | const X509_NAME_ENTRY *nb; |
2331 | 0 | xmlSecOpenSSLSizeT ii; |
2332 | 0 | xmlSecOpenSSLSizeT num_a, num_b; |
2333 | 0 | int ret; |
2334 | |
|
2335 | 0 | xmlSecAssert2(a != NULL, -1); |
2336 | 0 | xmlSecAssert2(b != NULL, 1); |
2337 | |
|
2338 | 0 | num_a = sk_X509_NAME_ENTRY_num(a); |
2339 | 0 | num_b = sk_X509_NAME_ENTRY_num(b); |
2340 | 0 | if (num_a > num_b) { |
2341 | 0 | return(1); |
2342 | 0 | } else if (num_a < num_b) { |
2343 | 0 | return(-1); |
2344 | 0 | } |
2345 | | |
2346 | | /* num_a == num_b */ |
2347 | 0 | for (ii = 0; ii < num_a; ++ii) { |
2348 | 0 | na = sk_X509_NAME_ENTRY_value(a, ii); |
2349 | 0 | nb = sk_X509_NAME_ENTRY_value(b, ii); |
2350 | |
|
2351 | 0 | ret = xmlSecOpenSSLX509_NAME_ENTRY_cmp(&na, &nb); |
2352 | 0 | if(ret != 0) { |
2353 | 0 | return(ret); |
2354 | 0 | } |
2355 | 0 | } |
2356 | | |
2357 | | /* same */ |
2358 | 0 | return(0); |
2359 | 0 | } |
2360 | | |
2361 | | |
2362 | | /** |
2363 | | * @brief We have to sort X509_NAME entries to get correct results. |
2364 | | * This is ugly but OpenSSL does not support it |
2365 | | * |
2366 | | * Returns 0 if equal |
2367 | | */ |
2368 | | static int |
2369 | 0 | xmlSecOpenSSLX509NamesCompare(XMLSEC_OPENSSL400_CONST X509_NAME *a, XMLSEC_OPENSSL400_CONST X509_NAME *b) { |
2370 | 0 | STACK_OF(X509_NAME_ENTRY) *a1 = NULL; |
2371 | 0 | STACK_OF(X509_NAME_ENTRY) *b1 = NULL; |
2372 | 0 | int ret; |
2373 | |
|
2374 | 0 | xmlSecAssert2(a != NULL, -1); |
2375 | 0 | xmlSecAssert2(b != NULL, 1); |
2376 | |
|
2377 | 0 | a1 = xmlSecOpenSSLX509_NAME_ENTRIES_copy(a); |
2378 | 0 | if(a1 == NULL) { |
2379 | 0 | xmlSecInternalError("xmlSecOpenSSLX509_NAME_ENTRIES_copy", NULL); |
2380 | 0 | return(-1); |
2381 | 0 | } |
2382 | 0 | b1 = xmlSecOpenSSLX509_NAME_ENTRIES_copy(b); |
2383 | 0 | if(b1 == NULL) { |
2384 | 0 | xmlSecInternalError("xmlSecOpenSSLX509_NAME_ENTRIES_copy", NULL); |
2385 | 0 | sk_X509_NAME_ENTRY_pop_free(a1, X509_NAME_ENTRY_free); |
2386 | 0 | return(1); |
2387 | 0 | } |
2388 | | |
2389 | | /* sort both */ |
2390 | 0 | (void)sk_X509_NAME_ENTRY_set_cmp_func(a1, xmlSecOpenSSLX509_NAME_ENTRY_cmp); |
2391 | 0 | sk_X509_NAME_ENTRY_sort(a1); |
2392 | 0 | (void)sk_X509_NAME_ENTRY_set_cmp_func(b1, xmlSecOpenSSLX509_NAME_ENTRY_cmp); |
2393 | 0 | sk_X509_NAME_ENTRY_sort(b1); |
2394 | | |
2395 | | /* actually compare, returns 0 if equal */ |
2396 | 0 | ret = xmlSecOpenSSLX509_NAME_ENTRIES_cmp(a1, b1); |
2397 | | |
2398 | | /* cleanup */ |
2399 | 0 | sk_X509_NAME_ENTRY_pop_free(a1, X509_NAME_ENTRY_free); |
2400 | 0 | sk_X509_NAME_ENTRY_pop_free(b1, X509_NAME_ENTRY_free); |
2401 | 0 | return(ret); |
2402 | 0 | } |
2403 | | |
2404 | | /* returns 0 if equal */ |
2405 | | static int |
2406 | 0 | xmlSecOpenSSLX509_NAME_ENTRY_cmp(const X509_NAME_ENTRY * const *a, const X509_NAME_ENTRY * const *b) { |
2407 | 0 | XMLSEC_OPENSSL400_CONST ASN1_STRING *a_value, *b_value; |
2408 | 0 | XMLSEC_OPENSSL400_CONST ASN1_OBJECT *a_name, *b_name; |
2409 | 0 | int a_len, b_len; |
2410 | 0 | int ret; |
2411 | |
|
2412 | 0 | xmlSecAssert2(a != NULL, -1); |
2413 | 0 | xmlSecAssert2(b != NULL, 1); |
2414 | 0 | xmlSecAssert2((*a) != NULL, -1); |
2415 | 0 | xmlSecAssert2((*b) != NULL, 1); |
2416 | | |
2417 | | |
2418 | | /* first compare values */ |
2419 | 0 | a_value = X509_NAME_ENTRY_get_data((X509_NAME_ENTRY*)(*a)); |
2420 | 0 | b_value = X509_NAME_ENTRY_get_data((X509_NAME_ENTRY*)(*b)); |
2421 | |
|
2422 | 0 | if((a_value == NULL) && (b_value != NULL)) { |
2423 | 0 | return(-1); |
2424 | 0 | } else if((a_value != NULL) && (b_value == NULL)) { |
2425 | 0 | return(1); |
2426 | 0 | } else if((a_value == NULL) && (b_value == NULL)) { |
2427 | 0 | return(0); |
2428 | 0 | } |
2429 | | |
2430 | 0 | a_len = ASN1_STRING_length(a_value); |
2431 | 0 | b_len = ASN1_STRING_length(b_value); |
2432 | 0 | ret = a_len - b_len; |
2433 | 0 | if(ret != 0) { |
2434 | 0 | return(ret); |
2435 | 0 | } |
2436 | | |
2437 | 0 | if(a_len > 0) { |
2438 | 0 | xmlSecSize a_size; |
2439 | 0 | XMLSEC_SAFE_CAST_INT_TO_SIZE(a_len, a_size, return(-1), NULL); |
2440 | 0 | ret = memcmp(ASN1_STRING_get0_data(a_value), ASN1_STRING_get0_data(b_value), a_size); |
2441 | 0 | if(ret != 0) { |
2442 | 0 | return(ret); |
2443 | 0 | } |
2444 | 0 | } |
2445 | | |
2446 | | /* next compare names */ |
2447 | 0 | a_name = X509_NAME_ENTRY_get_object((X509_NAME_ENTRY*)(*a)); |
2448 | 0 | b_name = X509_NAME_ENTRY_get_object((X509_NAME_ENTRY*)(*b)); |
2449 | |
|
2450 | 0 | if((a_name == NULL) && (b_name != NULL)) { |
2451 | 0 | return(-1); |
2452 | 0 | } else if((a_name != NULL) && (b_name == NULL)) { |
2453 | 0 | return(1); |
2454 | 0 | } else if((a_name == NULL) && (b_name == NULL)) { |
2455 | 0 | return(0); |
2456 | 0 | } |
2457 | | |
2458 | 0 | return(OBJ_cmp(a_name, b_name)); |
2459 | 0 | } |
2460 | | |
2461 | | #endif /* XMLSEC_NO_X509 */ |