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