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