xref: /dragonfly/crypto/libressl/crypto/asn1/x_crl.c (revision 6f5ec8b5)
1 /* $OpenBSD: x_crl.c,v 1.37 2022/02/24 22:05:06 beck Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 
59 #include <stdio.h>
60 
61 #include <openssl/opensslconf.h>
62 
63 #include <openssl/asn1t.h>
64 #include <openssl/err.h>
65 #include <openssl/x509.h>
66 #include <openssl/x509v3.h>
67 
68 #include "asn1_locl.h"
69 #include "x509_lcl.h"
70 
71 static int X509_REVOKED_cmp(const X509_REVOKED * const *a,
72     const X509_REVOKED * const *b);
73 static void setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp);
74 
75 static const ASN1_TEMPLATE X509_REVOKED_seq_tt[] = {
76 	{
77 		.offset = offsetof(X509_REVOKED, serialNumber),
78 		.field_name = "serialNumber",
79 		.item = &ASN1_INTEGER_it,
80 	},
81 	{
82 		.offset = offsetof(X509_REVOKED, revocationDate),
83 		.field_name = "revocationDate",
84 		.item = &ASN1_TIME_it,
85 	},
86 	{
87 		.flags = ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_OPTIONAL,
88 		.offset = offsetof(X509_REVOKED, extensions),
89 		.field_name = "extensions",
90 		.item = &X509_EXTENSION_it,
91 	},
92 };
93 
94 const ASN1_ITEM X509_REVOKED_it = {
95 	.itype = ASN1_ITYPE_SEQUENCE,
96 	.utype = V_ASN1_SEQUENCE,
97 	.templates = X509_REVOKED_seq_tt,
98 	.tcount = sizeof(X509_REVOKED_seq_tt) / sizeof(ASN1_TEMPLATE),
99 	.size = sizeof(X509_REVOKED),
100 	.sname = "X509_REVOKED",
101 };
102 
103 static int def_crl_verify(X509_CRL *crl, EVP_PKEY *r);
104 static int def_crl_lookup(X509_CRL *crl, X509_REVOKED **ret,
105     ASN1_INTEGER *serial, X509_NAME *issuer);
106 
107 static X509_CRL_METHOD int_crl_meth = {
108 	.crl_lookup = def_crl_lookup,
109 	.crl_verify = 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  */
118 static int
119 crl_inf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg)
120 {
121 	X509_CRL_INFO *a = (X509_CRL_INFO *)*pval;
122 
123 	if (!a || !a->revoked)
124 		return 1;
125 	switch (operation) {
126 		/* Just set cmp function here. We don't sort because that
127 		 * would affect the output of X509_CRL_print().
128 		 */
129 	case ASN1_OP_D2I_POST:
130 		(void)sk_X509_REVOKED_set_cmp_func(a->revoked, X509_REVOKED_cmp);
131 		break;
132 	}
133 	return 1;
134 }
135 
136 
137 static const ASN1_AUX X509_CRL_INFO_aux = {
138 	.flags = ASN1_AFLG_ENCODING,
139 	.asn1_cb = crl_inf_cb,
140 	.enc_offset = offsetof(X509_CRL_INFO, enc),
141 };
142 static const ASN1_TEMPLATE X509_CRL_INFO_seq_tt[] = {
143 	{
144 		.flags = ASN1_TFLG_OPTIONAL,
145 		.offset = offsetof(X509_CRL_INFO, version),
146 		.field_name = "version",
147 		.item = &ASN1_INTEGER_it,
148 	},
149 	{
150 		.offset = offsetof(X509_CRL_INFO, sig_alg),
151 		.field_name = "sig_alg",
152 		.item = &X509_ALGOR_it,
153 	},
154 	{
155 		.offset = offsetof(X509_CRL_INFO, issuer),
156 		.field_name = "issuer",
157 		.item = &X509_NAME_it,
158 	},
159 	{
160 		.offset = offsetof(X509_CRL_INFO, lastUpdate),
161 		.field_name = "lastUpdate",
162 		.item = &ASN1_TIME_it,
163 	},
164 	{
165 		.flags = ASN1_TFLG_OPTIONAL,
166 		.offset = offsetof(X509_CRL_INFO, nextUpdate),
167 		.field_name = "nextUpdate",
168 		.item = &ASN1_TIME_it,
169 	},
170 	{
171 		.flags = ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_OPTIONAL,
172 		.offset = offsetof(X509_CRL_INFO, revoked),
173 		.field_name = "revoked",
174 		.item = &X509_REVOKED_it,
175 	},
176 	{
177 		.flags = ASN1_TFLG_EXPLICIT | ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_OPTIONAL,
178 		.offset = offsetof(X509_CRL_INFO, extensions),
179 		.field_name = "extensions",
180 		.item = &X509_EXTENSION_it,
181 	},
182 };
183 
184 const ASN1_ITEM X509_CRL_INFO_it = {
185 	.itype = ASN1_ITYPE_SEQUENCE,
186 	.utype = V_ASN1_SEQUENCE,
187 	.templates = X509_CRL_INFO_seq_tt,
188 	.tcount = sizeof(X509_CRL_INFO_seq_tt) / sizeof(ASN1_TEMPLATE),
189 	.funcs = &X509_CRL_INFO_aux,
190 	.size = sizeof(X509_CRL_INFO),
191 	.sname = "X509_CRL_INFO",
192 };
193 
194 /* Set CRL entry issuer according to CRL certificate issuer extension.
195  * Check for unhandled critical CRL entry extensions.
196  */
197 
198 static int
199 crl_set_issuers(X509_CRL *crl)
200 {
201 	int i, j;
202 	GENERAL_NAMES *gens, *gtmp;
203 	STACK_OF(X509_REVOKED) *revoked;
204 
205 	revoked = X509_CRL_get_REVOKED(crl);
206 
207 	gens = NULL;
208 	for (i = 0; i < sk_X509_REVOKED_num(revoked); i++) {
209 		X509_REVOKED *rev = sk_X509_REVOKED_value(revoked, i);
210 		STACK_OF(X509_EXTENSION) *exts;
211 		ASN1_ENUMERATED *reason;
212 		X509_EXTENSION *ext;
213 		gtmp = X509_REVOKED_get_ext_d2i(rev, NID_certificate_issuer,
214 		    &j, NULL);
215 		if (!gtmp && (j != -1)) {
216 			crl->flags |= EXFLAG_INVALID;
217 			return 1;
218 		}
219 
220 		if (gtmp) {
221 			gens = gtmp;
222 			if (!crl->issuers) {
223 				crl->issuers = sk_GENERAL_NAMES_new_null();
224 				if (!crl->issuers)
225 					return 0;
226 			}
227 			if (!sk_GENERAL_NAMES_push(crl->issuers, gtmp))
228 				return 0;
229 		}
230 		rev->issuer = gens;
231 
232 		reason = X509_REVOKED_get_ext_d2i(rev, NID_crl_reason,
233 		    &j, NULL);
234 		if (!reason && (j != -1)) {
235 			crl->flags |= EXFLAG_INVALID;
236 			return 1;
237 		}
238 
239 		if (reason) {
240 			rev->reason = ASN1_ENUMERATED_get(reason);
241 			ASN1_ENUMERATED_free(reason);
242 		} else
243 			rev->reason = CRL_REASON_NONE;
244 
245 		/* Check for critical CRL entry extensions */
246 
247 		exts = rev->extensions;
248 
249 		for (j = 0; j < sk_X509_EXTENSION_num(exts); j++) {
250 			ext = sk_X509_EXTENSION_value(exts, j);
251 			if (ext->critical > 0) {
252 				if (OBJ_obj2nid(ext->object) ==
253 				    NID_certificate_issuer)
254 					continue;
255 				crl->flags |= EXFLAG_CRITICAL;
256 				break;
257 			}
258 		}
259 	}
260 
261 	return 1;
262 }
263 
264 /* The X509_CRL structure needs a bit of customisation. Cache some extensions
265  * and hash of the whole CRL.
266  */
267 static int
268 crl_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg)
269 {
270 	X509_CRL *crl = (X509_CRL *)*pval;
271 	STACK_OF(X509_EXTENSION) *exts;
272 	X509_EXTENSION *ext;
273 	int idx;
274 	int rc = 1;
275 
276 	switch (operation) {
277 	case ASN1_OP_NEW_POST:
278 		crl->idp = NULL;
279 		crl->akid = NULL;
280 		crl->flags = 0;
281 		crl->idp_flags = 0;
282 		crl->idp_reasons = CRLDP_ALL_REASONS;
283 		crl->meth = default_crl_method;
284 		crl->meth_data = NULL;
285 		crl->issuers = NULL;
286 		crl->crl_number = NULL;
287 		crl->base_crl_number = NULL;
288 		break;
289 
290 	case ASN1_OP_D2I_POST:
291 		X509_CRL_digest(crl, X509_CRL_HASH_EVP, crl->hash, NULL);
292 		crl->idp = X509_CRL_get_ext_d2i(crl,
293 		    NID_issuing_distribution_point, NULL, NULL);
294 		if (crl->idp)
295 			setup_idp(crl, crl->idp);
296 
297 		crl->akid = X509_CRL_get_ext_d2i(crl,
298 		    NID_authority_key_identifier, NULL, NULL);
299 
300 		crl->crl_number = X509_CRL_get_ext_d2i(crl,
301 		    NID_crl_number, NULL, NULL);
302 
303 		crl->base_crl_number = X509_CRL_get_ext_d2i(crl,
304 		    NID_delta_crl, NULL, NULL);
305 		/* Delta CRLs must have CRL number */
306 		if (crl->base_crl_number && !crl->crl_number)
307 			crl->flags |= EXFLAG_INVALID;
308 
309 		/* See if we have any unhandled critical CRL extensions and
310 		 * indicate this in a flag. We only currently handle IDP,
311 		 * AKID and deltas, so anything else critical sets the flag.
312 		 *
313 		 * This code accesses the X509_CRL structure directly:
314 		 * applications shouldn't do this.
315 		 */
316 
317 		exts = crl->crl->extensions;
318 
319 		for (idx = 0; idx < sk_X509_EXTENSION_num(exts); idx++) {
320 			int nid;
321 			ext = sk_X509_EXTENSION_value(exts, idx);
322 			nid = OBJ_obj2nid(ext->object);
323 			if (nid == NID_freshest_crl)
324 				crl->flags |= EXFLAG_FRESHEST;
325 			if (ext->critical > 0) {
326 				/* We handle IDP, AKID and deltas */
327 				if (nid == NID_issuing_distribution_point ||
328 				    nid == NID_authority_key_identifier ||
329 				    nid == NID_delta_crl)
330 					break;
331 				crl->flags |= EXFLAG_CRITICAL;
332 				break;
333 			}
334 		}
335 
336 		if (!crl_set_issuers(crl))
337 			return 0;
338 
339 		if (crl->meth->crl_init) {
340 			if (crl->meth->crl_init(crl) == 0)
341 				return 0;
342 		}
343 		break;
344 
345 	case ASN1_OP_FREE_POST:
346 		if (crl->meth->crl_free) {
347 			if (!crl->meth->crl_free(crl))
348 				rc = 0;
349 		}
350 		if (crl->akid)
351 			AUTHORITY_KEYID_free(crl->akid);
352 		if (crl->idp)
353 			ISSUING_DIST_POINT_free(crl->idp);
354 		ASN1_INTEGER_free(crl->crl_number);
355 		ASN1_INTEGER_free(crl->base_crl_number);
356 		sk_GENERAL_NAMES_pop_free(crl->issuers, GENERAL_NAMES_free);
357 		break;
358 	}
359 	return rc;
360 }
361 
362 /* Convert IDP into a more convenient form */
363 
364 static void
365 setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp)
366 {
367 	int idp_only = 0;
368 
369 	/* Set various flags according to IDP */
370 	crl->idp_flags |= IDP_PRESENT;
371 	if (idp->onlyuser > 0) {
372 		idp_only++;
373 		crl->idp_flags |= IDP_ONLYUSER;
374 	}
375 	if (idp->onlyCA > 0) {
376 		idp_only++;
377 		crl->idp_flags |= IDP_ONLYCA;
378 	}
379 	if (idp->onlyattr > 0) {
380 		idp_only++;
381 		crl->idp_flags |= IDP_ONLYATTR;
382 	}
383 
384 	if (idp_only > 1)
385 		crl->idp_flags |= IDP_INVALID;
386 
387 	if (idp->indirectCRL > 0)
388 		crl->idp_flags |= IDP_INDIRECT;
389 
390 	if (idp->onlysomereasons) {
391 		crl->idp_flags |= IDP_REASONS;
392 		if (idp->onlysomereasons->length > 0)
393 			crl->idp_reasons = idp->onlysomereasons->data[0];
394 		if (idp->onlysomereasons->length > 1)
395 			crl->idp_reasons |=
396 			    (idp->onlysomereasons->data[1] << 8);
397 		crl->idp_reasons &= CRLDP_ALL_REASONS;
398 	}
399 
400 	DIST_POINT_set_dpname(idp->distpoint, X509_CRL_get_issuer(crl));
401 }
402 
403 static const ASN1_AUX X509_CRL_aux = {
404 	.app_data = NULL,
405 	.flags = ASN1_AFLG_REFCOUNT,
406 	.ref_offset = offsetof(X509_CRL, references),
407 	.ref_lock = CRYPTO_LOCK_X509_CRL,
408 	.asn1_cb = crl_cb,
409 };
410 static const ASN1_TEMPLATE X509_CRL_seq_tt[] = {
411 	{
412 		.offset = offsetof(X509_CRL, crl),
413 		.field_name = "crl",
414 		.item = &X509_CRL_INFO_it,
415 	},
416 	{
417 		.offset = offsetof(X509_CRL, sig_alg),
418 		.field_name = "sig_alg",
419 		.item = &X509_ALGOR_it,
420 	},
421 	{
422 		.offset = offsetof(X509_CRL, signature),
423 		.field_name = "signature",
424 		.item = &ASN1_BIT_STRING_it,
425 	},
426 };
427 
428 const ASN1_ITEM X509_CRL_it = {
429 	.itype = ASN1_ITYPE_SEQUENCE,
430 	.utype = V_ASN1_SEQUENCE,
431 	.templates = X509_CRL_seq_tt,
432 	.tcount = sizeof(X509_CRL_seq_tt) / sizeof(ASN1_TEMPLATE),
433 	.funcs = &X509_CRL_aux,
434 	.size = sizeof(X509_CRL),
435 	.sname = "X509_CRL",
436 };
437 
438 
439 X509_REVOKED *
440 d2i_X509_REVOKED(X509_REVOKED **a, const unsigned char **in, long len)
441 {
442 	return (X509_REVOKED *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
443 	    &X509_REVOKED_it);
444 }
445 
446 int
447 i2d_X509_REVOKED(X509_REVOKED *a, unsigned char **out)
448 {
449 	return ASN1_item_i2d((ASN1_VALUE *)a, out, &X509_REVOKED_it);
450 }
451 
452 X509_REVOKED *
453 X509_REVOKED_new(void)
454 {
455 	return (X509_REVOKED *)ASN1_item_new(&X509_REVOKED_it);
456 }
457 
458 void
459 X509_REVOKED_free(X509_REVOKED *a)
460 {
461 	ASN1_item_free((ASN1_VALUE *)a, &X509_REVOKED_it);
462 }
463 
464 X509_REVOKED *
465 X509_REVOKED_dup(X509_REVOKED *a)
466 {
467 	return ASN1_item_dup(&X509_REVOKED_it, a);
468 }
469 
470 X509_CRL_INFO *
471 d2i_X509_CRL_INFO(X509_CRL_INFO **a, const unsigned char **in, long len)
472 {
473 	return (X509_CRL_INFO *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
474 	    &X509_CRL_INFO_it);
475 }
476 
477 int
478 i2d_X509_CRL_INFO(X509_CRL_INFO *a, unsigned char **out)
479 {
480 	return ASN1_item_i2d((ASN1_VALUE *)a, out, &X509_CRL_INFO_it);
481 }
482 
483 X509_CRL_INFO *
484 X509_CRL_INFO_new(void)
485 {
486 	return (X509_CRL_INFO *)ASN1_item_new(&X509_CRL_INFO_it);
487 }
488 
489 void
490 X509_CRL_INFO_free(X509_CRL_INFO *a)
491 {
492 	ASN1_item_free((ASN1_VALUE *)a, &X509_CRL_INFO_it);
493 }
494 
495 X509_CRL *
496 d2i_X509_CRL(X509_CRL **a, const unsigned char **in, long len)
497 {
498 	return (X509_CRL *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
499 	    &X509_CRL_it);
500 }
501 
502 int
503 i2d_X509_CRL(X509_CRL *a, unsigned char **out)
504 {
505 	return ASN1_item_i2d((ASN1_VALUE *)a, out, &X509_CRL_it);
506 }
507 
508 X509_CRL *
509 X509_CRL_new(void)
510 {
511 	return (X509_CRL *)ASN1_item_new(&X509_CRL_it);
512 }
513 
514 void
515 X509_CRL_free(X509_CRL *a)
516 {
517 	ASN1_item_free((ASN1_VALUE *)a, &X509_CRL_it);
518 }
519 
520 X509_CRL *
521 X509_CRL_dup(X509_CRL *x)
522 {
523 	return ASN1_item_dup(&X509_CRL_it, x);
524 }
525 
526 static int
527 X509_REVOKED_cmp(const X509_REVOKED * const *a, const X509_REVOKED * const *b)
528 {
529 	return(ASN1_INTEGER_cmp((*a)->serialNumber, (*b)->serialNumber));
530 }
531 
532 int
533 X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev)
534 {
535 	X509_CRL_INFO *inf;
536 
537 	inf = crl->crl;
538 	if (!inf->revoked)
539 		inf->revoked = sk_X509_REVOKED_new(X509_REVOKED_cmp);
540 	if (!inf->revoked || !sk_X509_REVOKED_push(inf->revoked, rev)) {
541 		ASN1error(ERR_R_MALLOC_FAILURE);
542 		return 0;
543 	}
544 	inf->enc.modified = 1;
545 	return 1;
546 }
547 
548 int
549 X509_CRL_verify(X509_CRL *crl, EVP_PKEY *r)
550 {
551 	if (crl->meth->crl_verify)
552 		return crl->meth->crl_verify(crl, r);
553 	return 0;
554 }
555 
556 int
557 X509_CRL_get0_by_serial(X509_CRL *crl, X509_REVOKED **ret,
558     ASN1_INTEGER *serial)
559 {
560 	if (crl->meth->crl_lookup)
561 		return crl->meth->crl_lookup(crl, ret, serial, NULL);
562 	return 0;
563 }
564 
565 int
566 X509_CRL_get0_by_cert(X509_CRL *crl, X509_REVOKED **ret, X509 *x)
567 {
568 	if (crl->meth->crl_lookup)
569 		return crl->meth->crl_lookup(crl, ret,
570 		    X509_get_serialNumber(x), X509_get_issuer_name(x));
571 	return 0;
572 }
573 
574 static int
575 def_crl_verify(X509_CRL *crl, EVP_PKEY *r)
576 {
577 	return(ASN1_item_verify(&X509_CRL_INFO_it,
578 	    crl->sig_alg, crl->signature, crl->crl, r));
579 }
580 
581 static int
582 crl_revoked_issuer_match(X509_CRL *crl, X509_NAME *nm, X509_REVOKED *rev)
583 {
584 	int i;
585 
586 	if (!rev->issuer) {
587 		if (!nm)
588 			return 1;
589 		if (!X509_NAME_cmp(nm, X509_CRL_get_issuer(crl)))
590 			return 1;
591 		return 0;
592 	}
593 
594 	if (!nm)
595 		nm = X509_CRL_get_issuer(crl);
596 
597 	for (i = 0; i < sk_GENERAL_NAME_num(rev->issuer); i++) {
598 		GENERAL_NAME *gen = sk_GENERAL_NAME_value(rev->issuer, i);
599 		if (gen->type != GEN_DIRNAME)
600 			continue;
601 		if (!X509_NAME_cmp(nm, gen->d.directoryName))
602 			return 1;
603 	}
604 	return 0;
605 
606 }
607 
608 static int
609 def_crl_lookup(X509_CRL *crl, X509_REVOKED **ret, ASN1_INTEGER *serial,
610     X509_NAME *issuer)
611 {
612 	X509_REVOKED rtmp, *rev;
613 	int idx;
614 
615 	rtmp.serialNumber = serial;
616 	/* Sort revoked into serial number order if not already sorted.
617 	 * Do this under a lock to avoid race condition.
618  	 */
619 	if (!sk_X509_REVOKED_is_sorted(crl->crl->revoked)) {
620 		CRYPTO_w_lock(CRYPTO_LOCK_X509_CRL);
621 		sk_X509_REVOKED_sort(crl->crl->revoked);
622 		CRYPTO_w_unlock(CRYPTO_LOCK_X509_CRL);
623 	}
624 	idx = sk_X509_REVOKED_find(crl->crl->revoked, &rtmp);
625 	if (idx < 0)
626 		return 0;
627 	/* Need to look for matching name */
628 	for (; idx < sk_X509_REVOKED_num(crl->crl->revoked); idx++) {
629 		rev = sk_X509_REVOKED_value(crl->crl->revoked, idx);
630 		if (ASN1_INTEGER_cmp(rev->serialNumber, serial))
631 			return 0;
632 		if (crl_revoked_issuer_match(crl, issuer, rev)) {
633 			if (ret)
634 				*ret = rev;
635 			if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
636 				return 2;
637 			return 1;
638 		}
639 	}
640 	return 0;
641 }
642 
643 void
644 X509_CRL_set_default_method(const X509_CRL_METHOD *meth)
645 {
646 	if (meth == NULL)
647 		default_crl_method = &int_crl_meth;
648 	else
649 		default_crl_method = meth;
650 }
651 
652 X509_CRL_METHOD *
653 X509_CRL_METHOD_new(int (*crl_init)(X509_CRL *crl),
654     int (*crl_free)(X509_CRL *crl),
655     int (*crl_lookup)(X509_CRL *crl, X509_REVOKED **ret,
656     ASN1_INTEGER *ser, X509_NAME *issuer),
657     int (*crl_verify)(X509_CRL *crl, EVP_PKEY *pk))
658 {
659 	X509_CRL_METHOD *m;
660 
661 	if ((m = calloc(1, sizeof(X509_CRL_METHOD))) == NULL)
662 		return NULL;
663 
664 	m->crl_init = crl_init;
665 	m->crl_free = crl_free;
666 	m->crl_lookup = crl_lookup;
667 	m->crl_verify = crl_verify;
668 	m->flags = X509_CRL_METHOD_DYNAMIC;
669 
670 	return m;
671 }
672 
673 void
674 X509_CRL_METHOD_free(X509_CRL_METHOD *m)
675 {
676 	if (m == NULL)
677 		return;
678 	if (!(m->flags & X509_CRL_METHOD_DYNAMIC))
679 		return;
680 	free(m);
681 }
682 
683 void
684 X509_CRL_set_meth_data(X509_CRL *crl, void *dat)
685 {
686 	crl->meth_data = dat;
687 }
688 
689 void *
690 X509_CRL_get_meth_data(X509_CRL *crl)
691 {
692 	return crl->meth_data;
693 }
694 
695 int
696 X509_CRL_get_signature_nid(const X509_CRL *crl)
697 {
698 	return OBJ_obj2nid(crl->sig_alg->algorithm);
699 }
700 
701 const STACK_OF(X509_EXTENSION) *
702 X509_CRL_get0_extensions(const X509_CRL *crl)
703 {
704 	return crl->crl->extensions;
705 }
706 
707 long
708 X509_CRL_get_version(const X509_CRL *crl)
709 {
710 	return ASN1_INTEGER_get(crl->crl->version);
711 }
712 
713 const ASN1_TIME *
714 X509_CRL_get0_lastUpdate(const X509_CRL *crl)
715 {
716 	return crl->crl->lastUpdate;
717 }
718 
719 ASN1_TIME *
720 X509_CRL_get_lastUpdate(X509_CRL *crl)
721 {
722 	return crl->crl->lastUpdate;
723 }
724 
725 const ASN1_TIME *
726 X509_CRL_get0_nextUpdate(const X509_CRL *crl)
727 {
728 	return crl->crl->nextUpdate;
729 }
730 
731 ASN1_TIME *
732 X509_CRL_get_nextUpdate(X509_CRL *crl)
733 {
734 	return crl->crl->nextUpdate;
735 }
736 
737 X509_NAME *
738 X509_CRL_get_issuer(const X509_CRL *crl)
739 {
740 	return crl->crl->issuer;
741 }
742 
743 STACK_OF(X509_REVOKED) *
744 X509_CRL_get_REVOKED(X509_CRL *crl)
745 {
746 	return crl->crl->revoked;
747 }
748 
749 void
750 X509_CRL_get0_signature(const X509_CRL *crl, const ASN1_BIT_STRING **psig,
751     const X509_ALGOR **palg)
752 {
753 	if (psig != NULL)
754 		*psig = crl->signature;
755 	if (palg != NULL)
756 		*palg = crl->sig_alg;
757 }
758