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