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