xref: /openbsd/lib/libcrypto/asn1/x_crl.c (revision a6445c1d)
1 /* $OpenBSD: x_crl.c,v 1.18 2014/07/11 13:54:41 miod 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 
70 static int X509_REVOKED_cmp(const X509_REVOKED * const *a,
71     const X509_REVOKED * const *b);
72 static void setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp);
73 
74 ASN1_SEQUENCE(X509_REVOKED) = {
75 	ASN1_SIMPLE(X509_REVOKED, serialNumber, ASN1_INTEGER),
76 	ASN1_SIMPLE(X509_REVOKED, revocationDate, ASN1_TIME),
77 	ASN1_SEQUENCE_OF_OPT(X509_REVOKED, extensions, X509_EXTENSION)
78 } ASN1_SEQUENCE_END(X509_REVOKED)
79 
80 static int def_crl_verify(X509_CRL *crl, EVP_PKEY *r);
81 static int def_crl_lookup(X509_CRL *crl, X509_REVOKED **ret,
82     ASN1_INTEGER *serial, X509_NAME *issuer);
83 
84 static X509_CRL_METHOD int_crl_meth = {
85 	.crl_lookup = def_crl_lookup,
86 	.crl_verify = def_crl_verify
87 };
88 
89 static const X509_CRL_METHOD *default_crl_method = &int_crl_meth;
90 
91 /* The X509_CRL_INFO structure needs a bit of customisation.
92  * Since we cache the original encoding the signature wont be affected by
93  * reordering of the revoked field.
94  */
95 static int
96 crl_inf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg)
97 {
98 	X509_CRL_INFO *a = (X509_CRL_INFO *)*pval;
99 
100 	if (!a || !a->revoked)
101 		return 1;
102 	switch (operation) {
103 		/* Just set cmp function here. We don't sort because that
104 		 * would affect the output of X509_CRL_print().
105 		 */
106 	case ASN1_OP_D2I_POST:
107 		(void)sk_X509_REVOKED_set_cmp_func(a->revoked, X509_REVOKED_cmp);
108 		break;
109 	}
110 	return 1;
111 }
112 
113 
114 ASN1_SEQUENCE_enc(X509_CRL_INFO, enc, crl_inf_cb) = {
115 	ASN1_OPT(X509_CRL_INFO, version, ASN1_INTEGER),
116 	ASN1_SIMPLE(X509_CRL_INFO, sig_alg, X509_ALGOR),
117 	ASN1_SIMPLE(X509_CRL_INFO, issuer, X509_NAME),
118 	ASN1_SIMPLE(X509_CRL_INFO, lastUpdate, ASN1_TIME),
119 	ASN1_OPT(X509_CRL_INFO, nextUpdate, ASN1_TIME),
120 	ASN1_SEQUENCE_OF_OPT(X509_CRL_INFO, revoked, X509_REVOKED),
121 	ASN1_EXP_SEQUENCE_OF_OPT(X509_CRL_INFO, extensions, X509_EXTENSION, 0)
122 } ASN1_SEQUENCE_END_enc(X509_CRL_INFO, X509_CRL_INFO)
123 
124 /* Set CRL entry issuer according to CRL certificate issuer extension.
125  * Check for unhandled critical CRL entry extensions.
126  */
127 
128 static int
129 crl_set_issuers(X509_CRL *crl)
130 {
131 	int i, j;
132 	GENERAL_NAMES *gens, *gtmp;
133 	STACK_OF(X509_REVOKED) *revoked;
134 
135 	revoked = X509_CRL_get_REVOKED(crl);
136 
137 	gens = NULL;
138 	for (i = 0; i < sk_X509_REVOKED_num(revoked); i++) {
139 		X509_REVOKED *rev = sk_X509_REVOKED_value(revoked, i);
140 		STACK_OF(X509_EXTENSION) *exts;
141 		ASN1_ENUMERATED *reason;
142 		X509_EXTENSION *ext;
143 		gtmp = X509_REVOKED_get_ext_d2i(rev, NID_certificate_issuer,
144 		    &j, NULL);
145 		if (!gtmp && (j != -1)) {
146 			crl->flags |= EXFLAG_INVALID;
147 			return 1;
148 		}
149 
150 		if (gtmp) {
151 			gens = gtmp;
152 			if (!crl->issuers) {
153 				crl->issuers = sk_GENERAL_NAMES_new_null();
154 				if (!crl->issuers)
155 					return 0;
156 			}
157 			if (!sk_GENERAL_NAMES_push(crl->issuers, gtmp))
158 				return 0;
159 		}
160 		rev->issuer = gens;
161 
162 		reason = X509_REVOKED_get_ext_d2i(rev, NID_crl_reason,
163 		    &j, NULL);
164 		if (!reason && (j != -1)) {
165 			crl->flags |= EXFLAG_INVALID;
166 			return 1;
167 		}
168 
169 		if (reason) {
170 			rev->reason = ASN1_ENUMERATED_get(reason);
171 			ASN1_ENUMERATED_free(reason);
172 		} else
173 			rev->reason = CRL_REASON_NONE;
174 
175 		/* Check for critical CRL entry extensions */
176 
177 		exts = rev->extensions;
178 
179 		for (j = 0; j < sk_X509_EXTENSION_num(exts); j++) {
180 			ext = sk_X509_EXTENSION_value(exts, j);
181 			if (ext->critical > 0) {
182 				if (OBJ_obj2nid(ext->object) ==
183 				    NID_certificate_issuer)
184 					continue;
185 				crl->flags |= EXFLAG_CRITICAL;
186 				break;
187 			}
188 		}
189 	}
190 
191 	return 1;
192 }
193 
194 /* The X509_CRL structure needs a bit of customisation. Cache some extensions
195  * and hash of the whole CRL.
196  */
197 static int
198 crl_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg)
199 {
200 	X509_CRL *crl = (X509_CRL *)*pval;
201 	STACK_OF(X509_EXTENSION) *exts;
202 	X509_EXTENSION *ext;
203 	int idx;
204 
205 	switch (operation) {
206 	case ASN1_OP_NEW_POST:
207 		crl->idp = NULL;
208 		crl->akid = NULL;
209 		crl->flags = 0;
210 		crl->idp_flags = 0;
211 		crl->idp_reasons = CRLDP_ALL_REASONS;
212 		crl->meth = default_crl_method;
213 		crl->meth_data = NULL;
214 		crl->issuers = NULL;
215 		crl->crl_number = NULL;
216 		crl->base_crl_number = NULL;
217 		break;
218 
219 	case ASN1_OP_D2I_POST:
220 #ifndef OPENSSL_NO_SHA
221 		X509_CRL_digest(crl, EVP_sha1(), crl->sha1_hash, NULL);
222 #endif
223 		crl->idp = X509_CRL_get_ext_d2i(crl,
224 		    NID_issuing_distribution_point, NULL, NULL);
225 		if (crl->idp)
226 			setup_idp(crl, crl->idp);
227 
228 		crl->akid = X509_CRL_get_ext_d2i(crl,
229 		    NID_authority_key_identifier, NULL, NULL);
230 
231 		crl->crl_number = X509_CRL_get_ext_d2i(crl,
232 		    NID_crl_number, NULL, NULL);
233 
234 		crl->base_crl_number = X509_CRL_get_ext_d2i(crl,
235 		    NID_delta_crl, NULL, NULL);
236 		/* Delta CRLs must have CRL number */
237 		if (crl->base_crl_number && !crl->crl_number)
238 			crl->flags |= EXFLAG_INVALID;
239 
240 		/* See if we have any unhandled critical CRL extensions and
241 		 * indicate this in a flag. We only currently handle IDP,
242 		 * AKID and deltas, so anything else critical sets the flag.
243 		 *
244 		 * This code accesses the X509_CRL structure directly:
245 		 * applications shouldn't do this.
246 		 */
247 
248 		exts = crl->crl->extensions;
249 
250 		for (idx = 0; idx < sk_X509_EXTENSION_num(exts); idx++) {
251 			int nid;
252 			ext = sk_X509_EXTENSION_value(exts, idx);
253 			nid = OBJ_obj2nid(ext->object);
254 			if (nid == NID_freshest_crl)
255 				crl->flags |= EXFLAG_FRESHEST;
256 			if (ext->critical > 0) {
257 				/* We handle IDP, AKID and deltas */
258 				if (nid == NID_issuing_distribution_point ||
259 				    nid == NID_authority_key_identifier ||
260 				    nid == NID_delta_crl)
261 					break;
262 				crl->flags |= EXFLAG_CRITICAL;
263 				break;
264 			}
265 		}
266 
267 		if (!crl_set_issuers(crl))
268 			return 0;
269 
270 		if (crl->meth->crl_init) {
271 			if (crl->meth->crl_init(crl) == 0)
272 				return 0;
273 		}
274 		break;
275 
276 	case ASN1_OP_FREE_POST:
277 		if (crl->meth->crl_free) {
278 			if (!crl->meth->crl_free(crl))
279 				return 0;
280 		}
281 		if (crl->akid)
282 			AUTHORITY_KEYID_free(crl->akid);
283 		if (crl->idp)
284 			ISSUING_DIST_POINT_free(crl->idp);
285 		ASN1_INTEGER_free(crl->crl_number);
286 		ASN1_INTEGER_free(crl->base_crl_number);
287 		sk_GENERAL_NAMES_pop_free(crl->issuers, GENERAL_NAMES_free);
288 		break;
289 	}
290 	return 1;
291 }
292 
293 /* Convert IDP into a more convenient form */
294 
295 static void
296 setup_idp(X509_CRL *crl, ISSUING_DIST_POINT *idp)
297 {
298 	int idp_only = 0;
299 
300 	/* Set various flags according to IDP */
301 	crl->idp_flags |= IDP_PRESENT;
302 	if (idp->onlyuser > 0) {
303 		idp_only++;
304 		crl->idp_flags |= IDP_ONLYUSER;
305 	}
306 	if (idp->onlyCA > 0) {
307 		idp_only++;
308 		crl->idp_flags |= IDP_ONLYCA;
309 	}
310 	if (idp->onlyattr > 0) {
311 		idp_only++;
312 		crl->idp_flags |= IDP_ONLYATTR;
313 	}
314 
315 	if (idp_only > 1)
316 		crl->idp_flags |= IDP_INVALID;
317 
318 	if (idp->indirectCRL > 0)
319 		crl->idp_flags |= IDP_INDIRECT;
320 
321 	if (idp->onlysomereasons) {
322 		crl->idp_flags |= IDP_REASONS;
323 		if (idp->onlysomereasons->length > 0)
324 			crl->idp_reasons = idp->onlysomereasons->data[0];
325 		if (idp->onlysomereasons->length > 1)
326 			crl->idp_reasons |=
327 			    (idp->onlysomereasons->data[1] << 8);
328 		crl->idp_reasons &= CRLDP_ALL_REASONS;
329 	}
330 
331 	DIST_POINT_set_dpname(idp->distpoint, X509_CRL_get_issuer(crl));
332 }
333 
334 ASN1_SEQUENCE_ref(X509_CRL, crl_cb, CRYPTO_LOCK_X509_CRL) = {
335 	ASN1_SIMPLE(X509_CRL, crl, X509_CRL_INFO),
336 	ASN1_SIMPLE(X509_CRL, sig_alg, X509_ALGOR),
337 	ASN1_SIMPLE(X509_CRL, signature, ASN1_BIT_STRING)
338 } ASN1_SEQUENCE_END_ref(X509_CRL, X509_CRL)
339 
340 IMPLEMENT_ASN1_FUNCTIONS(X509_REVOKED)
341 IMPLEMENT_ASN1_FUNCTIONS(X509_CRL_INFO)
342 IMPLEMENT_ASN1_FUNCTIONS(X509_CRL)
343 IMPLEMENT_ASN1_DUP_FUNCTION(X509_CRL)
344 
345 static int
346 X509_REVOKED_cmp(const X509_REVOKED * const *a, const X509_REVOKED * const *b)
347 {
348 	return(ASN1_STRING_cmp(
349 	    (ASN1_STRING *)(*a)->serialNumber,
350 	    (ASN1_STRING *)(*b)->serialNumber));
351 }
352 
353 int
354 X509_CRL_add0_revoked(X509_CRL *crl, X509_REVOKED *rev)
355 {
356 	X509_CRL_INFO *inf;
357 
358 	inf = crl->crl;
359 	if (!inf->revoked)
360 		inf->revoked = sk_X509_REVOKED_new(X509_REVOKED_cmp);
361 	if (!inf->revoked || !sk_X509_REVOKED_push(inf->revoked, rev)) {
362 		ASN1err(ASN1_F_X509_CRL_ADD0_REVOKED, ERR_R_MALLOC_FAILURE);
363 		return 0;
364 	}
365 	inf->enc.modified = 1;
366 	return 1;
367 }
368 
369 int
370 X509_CRL_verify(X509_CRL *crl, EVP_PKEY *r)
371 {
372 	if (crl->meth->crl_verify)
373 		return crl->meth->crl_verify(crl, r);
374 	return 0;
375 }
376 
377 int
378 X509_CRL_get0_by_serial(X509_CRL *crl, X509_REVOKED **ret,
379     ASN1_INTEGER *serial)
380 {
381 	if (crl->meth->crl_lookup)
382 		return crl->meth->crl_lookup(crl, ret, serial, NULL);
383 	return 0;
384 }
385 
386 int
387 X509_CRL_get0_by_cert(X509_CRL *crl, X509_REVOKED **ret, X509 *x)
388 {
389 	if (crl->meth->crl_lookup)
390 		return crl->meth->crl_lookup(crl, ret,
391 		    X509_get_serialNumber(x), X509_get_issuer_name(x));
392 	return 0;
393 }
394 
395 static int
396 def_crl_verify(X509_CRL *crl, EVP_PKEY *r)
397 {
398 	return(ASN1_item_verify(ASN1_ITEM_rptr(X509_CRL_INFO),
399 	    crl->sig_alg, crl->signature, crl->crl, r));
400 }
401 
402 static int
403 crl_revoked_issuer_match(X509_CRL *crl, X509_NAME *nm, X509_REVOKED *rev)
404 {
405 	int i;
406 
407 	if (!rev->issuer) {
408 		if (!nm)
409 			return 1;
410 		if (!X509_NAME_cmp(nm, X509_CRL_get_issuer(crl)))
411 			return 1;
412 		return 0;
413 	}
414 
415 	if (!nm)
416 		nm = X509_CRL_get_issuer(crl);
417 
418 	for (i = 0; i < sk_GENERAL_NAME_num(rev->issuer); i++) {
419 		GENERAL_NAME *gen = sk_GENERAL_NAME_value(rev->issuer, i);
420 		if (gen->type != GEN_DIRNAME)
421 			continue;
422 		if (!X509_NAME_cmp(nm, gen->d.directoryName))
423 			return 1;
424 	}
425 	return 0;
426 
427 }
428 
429 static int
430 def_crl_lookup(X509_CRL *crl, X509_REVOKED **ret, ASN1_INTEGER *serial,
431     X509_NAME *issuer)
432 {
433 	X509_REVOKED rtmp, *rev;
434 	int idx;
435 
436 	rtmp.serialNumber = serial;
437 	/* Sort revoked into serial number order if not already sorted.
438 	 * Do this under a lock to avoid race condition.
439  	 */
440 	if (!sk_X509_REVOKED_is_sorted(crl->crl->revoked)) {
441 		CRYPTO_w_lock(CRYPTO_LOCK_X509_CRL);
442 		sk_X509_REVOKED_sort(crl->crl->revoked);
443 		CRYPTO_w_unlock(CRYPTO_LOCK_X509_CRL);
444 	}
445 	idx = sk_X509_REVOKED_find(crl->crl->revoked, &rtmp);
446 	if (idx < 0)
447 		return 0;
448 	/* Need to look for matching name */
449 	for (; idx < sk_X509_REVOKED_num(crl->crl->revoked); idx++) {
450 		rev = sk_X509_REVOKED_value(crl->crl->revoked, idx);
451 		if (ASN1_INTEGER_cmp(rev->serialNumber, serial))
452 			return 0;
453 		if (crl_revoked_issuer_match(crl, issuer, rev)) {
454 			if (ret)
455 				*ret = rev;
456 			if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
457 				return 2;
458 			return 1;
459 		}
460 	}
461 	return 0;
462 }
463 
464 void
465 X509_CRL_set_default_method(const X509_CRL_METHOD *meth)
466 {
467 	if (meth == NULL)
468 		default_crl_method = &int_crl_meth;
469 	else
470 		default_crl_method = meth;
471 }
472 
473 X509_CRL_METHOD *
474 X509_CRL_METHOD_new(int (*crl_init)(X509_CRL *crl),
475     int (*crl_free)(X509_CRL *crl),
476     int (*crl_lookup)(X509_CRL *crl, X509_REVOKED **ret,
477     ASN1_INTEGER *ser, X509_NAME *issuer),
478     int (*crl_verify)(X509_CRL *crl, EVP_PKEY *pk))
479 {
480 	X509_CRL_METHOD *m;
481 
482 	m = malloc(sizeof(X509_CRL_METHOD));
483 	if (!m)
484 		return NULL;
485 	m->crl_init = crl_init;
486 	m->crl_free = crl_free;
487 	m->crl_lookup = crl_lookup;
488 	m->crl_verify = crl_verify;
489 	m->flags = X509_CRL_METHOD_DYNAMIC;
490 	return m;
491 }
492 
493 void
494 X509_CRL_METHOD_free(X509_CRL_METHOD *m)
495 {
496 	if (!(m->flags & X509_CRL_METHOD_DYNAMIC))
497 		return;
498 	free(m);
499 }
500 
501 void
502 X509_CRL_set_meth_data(X509_CRL *crl, void *dat)
503 {
504 	crl->meth_data = dat;
505 }
506 
507 void *
508 X509_CRL_get_meth_data(X509_CRL *crl)
509 {
510 	return crl->meth_data;
511 }
512 
513 IMPLEMENT_STACK_OF(X509_REVOKED)
514 IMPLEMENT_ASN1_SET_OF(X509_REVOKED)
515 IMPLEMENT_STACK_OF(X509_CRL)
516 IMPLEMENT_ASN1_SET_OF(X509_CRL)
517