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