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