1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
57 #include <openssl/asn1.h>
58 #include <openssl/asn1t.h>
59 #include <openssl/digest.h>
60 #include <openssl/err.h>
61 #include <openssl/mem.h>
62 #include <openssl/obj.h>
63 #include <openssl/stack.h>
64 #include <openssl/thread.h>
65 #include <openssl/x509.h>
66 #include <openssl/x509v3.h>
67
68 #include "../internal.h"
69
70
71 /* Method to handle CRL access.
72 * In general a CRL could be very large (several Mb) and can consume large
73 * amounts of resources if stored in memory by multiple processes.
74 * This method allows general CRL operations to be redirected to more
75 * efficient callbacks: for example a CRL entry database.
76 */
77
78 #define X509_CRL_METHOD_DYNAMIC 1
79
80 struct x509_crl_method_st
81 {
82 int flags;
83 int (*crl_init)(X509_CRL *crl);
84 int (*crl_free)(X509_CRL *crl);
85 int (*crl_lookup)(X509_CRL *crl, X509_REVOKED **ret,
86 ASN1_INTEGER *ser, X509_NAME *issuer);
87 int (*crl_verify)(X509_CRL *crl, EVP_PKEY *pk);
88 };
89
90 static int X509_REVOKED_cmp(const X509_REVOKED **a,
91 const X509_REVOKED **b);
92 static void setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp);
93
94 ASN1_SEQUENCE(X509_REVOKED) = {
95 ASN1_SIMPLE(X509_REVOKED,serialNumber, ASN1_INTEGER),
96 ASN1_SIMPLE(X509_REVOKED,revocationDate, ASN1_TIME),
97 ASN1_SEQUENCE_OF_OPT(X509_REVOKED,extensions, X509_EXTENSION)
98 } ASN1_SEQUENCE_END(X509_REVOKED)
99
100 static int def_crl_verify(X509_CRL *crl, EVP_PKEY *r);
101 static int def_crl_lookup(X509_CRL *crl,
102 X509_REVOKED **ret, ASN1_INTEGER *serial, X509_NAME *issuer);
103
104 static const X509_CRL_METHOD int_crl_meth =
105 {
106 0,
107 0,0,
108 def_crl_lookup,
109 def_crl_verify
110 };
111
112 static const X509_CRL_METHOD *default_crl_method = &int_crl_meth;
113
114 /* The X509_CRL_INFO structure needs a bit of customisation.
115 * Since we cache the original encoding the signature wont be affected by
116 * reordering of the revoked field.
117 */
crl_inf_cb(int operation,ASN1_VALUE ** pval,const ASN1_ITEM * it,void * exarg)118 static int crl_inf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
119 void *exarg)
120 {
121 X509_CRL_INFO *a = (X509_CRL_INFO *)*pval;
122
123 if(!a || !a->revoked) return 1;
124 switch(operation) {
125 /* Just set cmp function here. We don't sort because that
126 * would affect the output of X509_CRL_print().
127 */
128 case ASN1_OP_D2I_POST:
129 (void)sk_X509_REVOKED_set_cmp_func(a->revoked,X509_REVOKED_cmp);
130 break;
131 }
132 return 1;
133 }
134
135
136 ASN1_SEQUENCE_enc(X509_CRL_INFO, enc, crl_inf_cb) = {
137 ASN1_OPT(X509_CRL_INFO, version, ASN1_INTEGER),
138 ASN1_SIMPLE(X509_CRL_INFO, sig_alg, X509_ALGOR),
139 ASN1_SIMPLE(X509_CRL_INFO, issuer, X509_NAME),
140 ASN1_SIMPLE(X509_CRL_INFO, lastUpdate, ASN1_TIME),
141 ASN1_OPT(X509_CRL_INFO, nextUpdate, ASN1_TIME),
142 ASN1_SEQUENCE_OF_OPT(X509_CRL_INFO, revoked, X509_REVOKED),
143 ASN1_EXP_SEQUENCE_OF_OPT(X509_CRL_INFO, extensions, X509_EXTENSION, 0)
144 } ASN1_SEQUENCE_END_enc(X509_CRL_INFO, X509_CRL_INFO)
145
146 /* Set CRL entry issuer according to CRL certificate issuer extension.
147 * Check for unhandled critical CRL entry extensions.
148 */
149
150 static int crl_set_issuers(X509_CRL *crl)
151 {
152
153 size_t i, k;
154 int j;
155 GENERAL_NAMES *gens, *gtmp;
156 STACK_OF(X509_REVOKED) *revoked;
157
158 revoked = X509_CRL_get_REVOKED(crl);
159
160 gens = NULL;
161 for (i = 0; i < sk_X509_REVOKED_num(revoked); i++)
162 {
163 X509_REVOKED *rev = sk_X509_REVOKED_value(revoked, i);
164 STACK_OF(X509_EXTENSION) *exts;
165 ASN1_ENUMERATED *reason;
166 X509_EXTENSION *ext;
167 gtmp = X509_REVOKED_get_ext_d2i(rev,
168 NID_certificate_issuer,
169 &j, NULL);
170 if (!gtmp && (j != -1))
171 {
172 crl->flags |= EXFLAG_INVALID;
173 return 1;
174 }
175
176 if (gtmp)
177 {
178 gens = gtmp;
179 if (!crl->issuers)
180 {
181 crl->issuers = sk_GENERAL_NAMES_new_null();
182 if (!crl->issuers)
183 return 0;
184 }
185 if (!sk_GENERAL_NAMES_push(crl->issuers, gtmp))
186 return 0;
187 }
188 rev->issuer = gens;
189
190 reason = X509_REVOKED_get_ext_d2i(rev, NID_crl_reason,
191 &j, NULL);
192 if (!reason && (j != -1))
193 {
194 crl->flags |= EXFLAG_INVALID;
195 return 1;
196 }
197
198 if (reason)
199 {
200 rev->reason = ASN1_ENUMERATED_get(reason);
201 ASN1_ENUMERATED_free(reason);
202 }
203 else
204 rev->reason = CRL_REASON_NONE;
205
206 /* Check for critical CRL entry extensions */
207
208 exts = rev->extensions;
209
210 for (k = 0; k < sk_X509_EXTENSION_num(exts); k++)
211 {
212 ext = sk_X509_EXTENSION_value(exts, k);
213 if (ext->critical > 0)
214 {
215 if (OBJ_obj2nid(ext->object) ==
216 NID_certificate_issuer)
217 continue;
218 crl->flags |= EXFLAG_CRITICAL;
219 break;
220 }
221 }
222
223
224 }
225
226 return 1;
227
228 }
229
230 /* The X509_CRL structure needs a bit of customisation. Cache some extensions
231 * and hash of the whole CRL.
232 */
crl_cb(int operation,ASN1_VALUE ** pval,const ASN1_ITEM * it,void * exarg)233 static int crl_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
234 void *exarg)
235 {
236 X509_CRL *crl = (X509_CRL *)*pval;
237 STACK_OF(X509_EXTENSION) *exts;
238 X509_EXTENSION *ext;
239 size_t idx;
240
241 switch(operation)
242 {
243 case ASN1_OP_NEW_POST:
244 crl->idp = NULL;
245 crl->akid = NULL;
246 crl->flags = 0;
247 crl->idp_flags = 0;
248 crl->idp_reasons = CRLDP_ALL_REASONS;
249 crl->meth = default_crl_method;
250 crl->meth_data = NULL;
251 crl->issuers = NULL;
252 crl->crl_number = NULL;
253 crl->base_crl_number = NULL;
254 break;
255
256 case ASN1_OP_D2I_POST:
257 X509_CRL_digest(crl, EVP_sha1(), crl->sha1_hash, NULL);
258 crl->idp = X509_CRL_get_ext_d2i(crl,
259 NID_issuing_distribution_point, NULL, NULL);
260 if (crl->idp)
261 setup_idp(crl, crl->idp);
262
263 crl->akid = X509_CRL_get_ext_d2i(crl,
264 NID_authority_key_identifier, NULL, NULL);
265
266 crl->crl_number = X509_CRL_get_ext_d2i(crl,
267 NID_crl_number, NULL, NULL);
268
269 crl->base_crl_number = X509_CRL_get_ext_d2i(crl,
270 NID_delta_crl, NULL, NULL);
271 /* Delta CRLs must have CRL number */
272 if (crl->base_crl_number && !crl->crl_number)
273 crl->flags |= EXFLAG_INVALID;
274
275 /* See if we have any unhandled critical CRL extensions and
276 * indicate this in a flag. We only currently handle IDP so
277 * anything else critical sets the flag.
278 *
279 * This code accesses the X509_CRL structure directly:
280 * applications shouldn't do this.
281 */
282
283 exts = crl->crl->extensions;
284
285 for (idx = 0; idx < sk_X509_EXTENSION_num(exts); idx++)
286 {
287 int nid;
288 ext = sk_X509_EXTENSION_value(exts, idx);
289 nid = OBJ_obj2nid(ext->object);
290 if (nid == NID_freshest_crl)
291 crl->flags |= EXFLAG_FRESHEST;
292 if (ext->critical > 0)
293 {
294 /* We handle IDP and deltas */
295 if ((nid == NID_issuing_distribution_point)
296 || (nid == NID_authority_key_identifier)
297 || (nid == NID_delta_crl))
298 break;;
299 crl->flags |= EXFLAG_CRITICAL;
300 break;
301 }
302 }
303
304
305 if (!crl_set_issuers(crl))
306 return 0;
307
308 if (crl->meth->crl_init)
309 {
310 if (crl->meth->crl_init(crl) == 0)
311 return 0;
312 }
313 break;
314
315 case ASN1_OP_FREE_POST:
316 if (crl->meth->crl_free)
317 {
318 if (!crl->meth->crl_free(crl))
319 return 0;
320 }
321 if (crl->akid)
322 AUTHORITY_KEYID_free(crl->akid);
323 if (crl->idp)
324 ISSUING_DIST_POINT_free(crl->idp);
325 ASN1_INTEGER_free(crl->crl_number);
326 ASN1_INTEGER_free(crl->base_crl_number);
327 sk_GENERAL_NAMES_pop_free(crl->issuers, GENERAL_NAMES_free);
328 break;
329 }
330 return 1;
331 }
332
333 /* Convert IDP into a more convenient form */
334
setup_idp(X509_CRL * crl,ISSUING_DIST_POINT * idp)335 static void setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp)
336 {
337 int idp_only = 0;
338 /* Set various flags according to IDP */
339 crl->idp_flags |= IDP_PRESENT;
340 if (idp->onlyuser > 0)
341 {
342 idp_only++;
343 crl->idp_flags |= IDP_ONLYUSER;
344 }
345 if (idp->onlyCA > 0)
346 {
347 idp_only++;
348 crl->idp_flags |= IDP_ONLYCA;
349 }
350 if (idp->onlyattr > 0)
351 {
352 idp_only++;
353 crl->idp_flags |= IDP_ONLYATTR;
354 }
355
356 if (idp_only > 1)
357 crl->idp_flags |= IDP_INVALID;
358
359 if (idp->indirectCRL > 0)
360 crl->idp_flags |= IDP_INDIRECT;
361
362 if (idp->onlysomereasons)
363 {
364 crl->idp_flags |= IDP_REASONS;
365 if (idp->onlysomereasons->length > 0)
366 crl->idp_reasons = idp->onlysomereasons->data[0];
367 if (idp->onlysomereasons->length > 1)
368 crl->idp_reasons |=
369 (idp->onlysomereasons->data[1] << 8);
370 crl->idp_reasons &= CRLDP_ALL_REASONS;
371 }
372
373 DIST_POINT_set_dpname(idp->distpoint, X509_CRL_get_issuer(crl));
374 }
375
376 ASN1_SEQUENCE_ref(X509_CRL, crl_cb) = {
377 ASN1_SIMPLE(X509_CRL, crl, X509_CRL_INFO),
378 ASN1_SIMPLE(X509_CRL, sig_alg, X509_ALGOR),
379 ASN1_SIMPLE(X509_CRL, signature, ASN1_BIT_STRING)
380 } ASN1_SEQUENCE_END_ref(X509_CRL, X509_CRL)
381
382 IMPLEMENT_ASN1_FUNCTIONS(X509_REVOKED)
383 IMPLEMENT_ASN1_DUP_FUNCTION(X509_REVOKED)
384 IMPLEMENT_ASN1_FUNCTIONS(X509_CRL_INFO)
385 IMPLEMENT_ASN1_FUNCTIONS(X509_CRL)
386 IMPLEMENT_ASN1_DUP_FUNCTION(X509_CRL)
387
388 static int X509_REVOKED_cmp(const X509_REVOKED **a,
389 const X509_REVOKED **b)
390 {
391 return(ASN1_STRING_cmp(
392 (ASN1_STRING *)(*a)->serialNumber,
393 (ASN1_STRING *)(*b)->serialNumber));
394 }
395
X509_CRL_add0_revoked(X509_CRL * crl,X509_REVOKED * rev)396 int X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev)
397 {
398 X509_CRL_INFO *inf;
399 inf = crl->crl;
400 if(!inf->revoked)
401 inf->revoked = sk_X509_REVOKED_new(X509_REVOKED_cmp);
402 if(!inf->revoked || !sk_X509_REVOKED_push(inf->revoked, rev)) {
403 OPENSSL_PUT_ERROR(X509, X509_CRL_add0_revoked, ERR_R_MALLOC_FAILURE);
404 return 0;
405 }
406 inf->enc.modified = 1;
407 return 1;
408 }
409
X509_CRL_verify(X509_CRL * crl,EVP_PKEY * r)410 int X509_CRL_verify(X509_CRL *crl, EVP_PKEY *r)
411 {
412 if (crl->meth->crl_verify)
413 return crl->meth->crl_verify(crl, r);
414 return 0;
415 }
416
X509_CRL_get0_by_serial(X509_CRL * crl,X509_REVOKED ** ret,ASN1_INTEGER * serial)417 int X509_CRL_get0_by_serial(X509_CRL *crl,
418 X509_REVOKED **ret, ASN1_INTEGER *serial)
419 {
420 if (crl->meth->crl_lookup)
421 return crl->meth->crl_lookup(crl, ret, serial, NULL);
422 return 0;
423 }
424
X509_CRL_get0_by_cert(X509_CRL * crl,X509_REVOKED ** ret,X509 * x)425 int X509_CRL_get0_by_cert(X509_CRL *crl, X509_REVOKED **ret, X509 *x)
426 {
427 if (crl->meth->crl_lookup)
428 return crl->meth->crl_lookup(crl, ret,
429 X509_get_serialNumber(x),
430 X509_get_issuer_name(x));
431 return 0;
432 }
433
def_crl_verify(X509_CRL * crl,EVP_PKEY * r)434 static int def_crl_verify(X509_CRL *crl, EVP_PKEY *r)
435 {
436 return(ASN1_item_verify(ASN1_ITEM_rptr(X509_CRL_INFO),
437 crl->sig_alg, crl->signature,crl->crl,r));
438 }
439
crl_revoked_issuer_match(X509_CRL * crl,X509_NAME * nm,X509_REVOKED * rev)440 static int crl_revoked_issuer_match(X509_CRL *crl, X509_NAME *nm,
441 X509_REVOKED *rev)
442 {
443 size_t i;
444
445 if (!rev->issuer)
446 {
447 if (!nm)
448 return 1;
449 if (!X509_NAME_cmp(nm, X509_CRL_get_issuer(crl)))
450 return 1;
451 return 0;
452 }
453
454 if (!nm)
455 nm = X509_CRL_get_issuer(crl);
456
457 for (i = 0; i < sk_GENERAL_NAME_num(rev->issuer); i++)
458 {
459 GENERAL_NAME *gen = sk_GENERAL_NAME_value(rev->issuer, i);
460 if (gen->type != GEN_DIRNAME)
461 continue;
462 if (!X509_NAME_cmp(nm, gen->d.directoryName))
463 return 1;
464 }
465 return 0;
466
467 }
468
469 static struct CRYPTO_STATIC_MUTEX g_crl_sort_lock = CRYPTO_STATIC_MUTEX_INIT;
470
def_crl_lookup(X509_CRL * crl,X509_REVOKED ** ret,ASN1_INTEGER * serial,X509_NAME * issuer)471 static int def_crl_lookup(X509_CRL *crl,
472 X509_REVOKED **ret, ASN1_INTEGER *serial, X509_NAME *issuer)
473 {
474 X509_REVOKED rtmp, *rev;
475 size_t idx;
476 rtmp.serialNumber = serial;
477 /* Sort revoked into serial number order if not already sorted.
478 * Do this under a lock to avoid race condition.
479 */
480
481 CRYPTO_STATIC_MUTEX_lock_read(&g_crl_sort_lock);
482 const int is_sorted = sk_X509_REVOKED_is_sorted(crl->crl->revoked);
483 CRYPTO_STATIC_MUTEX_unlock(&g_crl_sort_lock);
484
485 if (!is_sorted)
486 {
487 CRYPTO_STATIC_MUTEX_lock_write(&g_crl_sort_lock);
488 if (!sk_X509_REVOKED_is_sorted(crl->crl->revoked))
489 {
490 sk_X509_REVOKED_sort(crl->crl->revoked);
491 }
492 CRYPTO_STATIC_MUTEX_unlock(&g_crl_sort_lock);
493 }
494
495 if (!sk_X509_REVOKED_find(crl->crl->revoked, &idx, &rtmp))
496 return 0;
497 /* Need to look for matching name */
498 for(;idx < sk_X509_REVOKED_num(crl->crl->revoked); idx++)
499 {
500 rev = sk_X509_REVOKED_value(crl->crl->revoked, idx);
501 if (ASN1_INTEGER_cmp(rev->serialNumber, serial))
502 return 0;
503 if (crl_revoked_issuer_match(crl, issuer, rev))
504 {
505 if (ret)
506 *ret = rev;
507 if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
508 return 2;
509 return 1;
510 }
511 }
512 return 0;
513 }
514
X509_CRL_set_default_method(const X509_CRL_METHOD * meth)515 void X509_CRL_set_default_method(const X509_CRL_METHOD *meth)
516 {
517 if (meth == NULL)
518 default_crl_method = &int_crl_meth;
519 else
520 default_crl_method = meth;
521 }
522
X509_CRL_METHOD_new(int (* crl_init)(X509_CRL * crl),int (* crl_free)(X509_CRL * crl),int (* crl_lookup)(X509_CRL * crl,X509_REVOKED ** ret,ASN1_INTEGER * ser,X509_NAME * issuer),int (* crl_verify)(X509_CRL * crl,EVP_PKEY * pk))523 X509_CRL_METHOD *X509_CRL_METHOD_new(
524 int (*crl_init)(X509_CRL *crl),
525 int (*crl_free)(X509_CRL *crl),
526 int (*crl_lookup)(X509_CRL *crl, X509_REVOKED **ret,
527 ASN1_INTEGER *ser, X509_NAME *issuer),
528 int (*crl_verify)(X509_CRL *crl, EVP_PKEY *pk))
529 {
530 X509_CRL_METHOD *m;
531 m = OPENSSL_malloc(sizeof(X509_CRL_METHOD));
532 if (!m)
533 return NULL;
534 m->crl_init = crl_init;
535 m->crl_free = crl_free;
536 m->crl_lookup = crl_lookup;
537 m->crl_verify = crl_verify;
538 m->flags = X509_CRL_METHOD_DYNAMIC;
539 return m;
540 }
541
X509_CRL_METHOD_free(X509_CRL_METHOD * m)542 void X509_CRL_METHOD_free(X509_CRL_METHOD *m)
543 {
544 if (!(m->flags & X509_CRL_METHOD_DYNAMIC))
545 return;
546 OPENSSL_free(m);
547 }
548
X509_CRL_set_meth_data(X509_CRL * crl,void * dat)549 void X509_CRL_set_meth_data(X509_CRL *crl, void *dat)
550 {
551 crl->meth_data = dat;
552 }
553
X509_CRL_get_meth_data(X509_CRL * crl)554 void *X509_CRL_get_meth_data(X509_CRL *crl)
555 {
556 return crl->meth_data;
557 }
558
559 IMPLEMENT_ASN1_SET_OF(X509_REVOKED)
560 IMPLEMENT_ASN1_SET_OF(X509_CRL)
561