1 /* $OpenBSD: x509_crld.c,v 1.1 2020/06/04 15:19:31 jsing Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1999-2008 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
62 #include <openssl/asn1.h>
63 #include <openssl/asn1t.h>
64 #include <openssl/conf.h>
65 #include <openssl/err.h>
66 #include <openssl/x509v3.h>
68 static void *v2i_crld(const X509V3_EXT_METHOD *method,
69 X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval);
70 static int i2r_crldp(const X509V3_EXT_METHOD *method, void *pcrldp, BIO *out,
73 const X509V3_EXT_METHOD v3_crld = {
74 .ext_nid = NID_crl_distribution_points,
76 .it = &CRL_DIST_POINTS_it,
90 const X509V3_EXT_METHOD v3_freshest_crl = {
91 .ext_nid = NID_freshest_crl,
93 .it = &CRL_DIST_POINTS_it,
107 static STACK_OF(GENERAL_NAME) *
108 gnames_from_sectname(X509V3_CTX *ctx, char *sect)
110 STACK_OF(CONF_VALUE) *gnsect;
111 STACK_OF(GENERAL_NAME) *gens;
114 gnsect = X509V3_get_section(ctx, sect + 1);
116 gnsect = X509V3_parse_list(sect);
118 X509V3error(X509V3_R_SECTION_NOT_FOUND);
121 gens = v2i_GENERAL_NAMES(NULL, ctx, gnsect);
123 X509V3_section_free(ctx, gnsect);
125 sk_CONF_VALUE_pop_free(gnsect, X509V3_conf_free);
130 set_dist_point_name(DIST_POINT_NAME **pdp, X509V3_CTX *ctx, CONF_VALUE *cnf)
132 STACK_OF(GENERAL_NAME) *fnm = NULL;
133 STACK_OF(X509_NAME_ENTRY) *rnm = NULL;
135 if (!strncmp(cnf->name, "fullname", 9)) {
136 fnm = gnames_from_sectname(ctx, cnf->value);
139 } else if (!strcmp(cnf->name, "relativename")) {
141 STACK_OF(CONF_VALUE) *dnsect;
143 nm = X509_NAME_new();
146 dnsect = X509V3_get_section(ctx, cnf->value);
148 X509V3error(X509V3_R_SECTION_NOT_FOUND);
152 ret = X509V3_NAME_from_section(nm, dnsect, MBSTRING_ASC);
153 X509V3_section_free(ctx, dnsect);
157 if (!ret || sk_X509_NAME_ENTRY_num(rnm) <= 0)
159 /* Since its a name fragment can't have more than one
162 if (sk_X509_NAME_ENTRY_value(rnm,
163 sk_X509_NAME_ENTRY_num(rnm) - 1)->set) {
164 X509V3error(X509V3_R_INVALID_MULTIPLE_RDNS);
171 X509V3error(X509V3_R_DISTPOINT_ALREADY_SET);
175 *pdp = DIST_POINT_NAME_new();
180 (*pdp)->name.fullname = fnm;
183 (*pdp)->name.relativename = rnm;
189 sk_GENERAL_NAME_pop_free(fnm, GENERAL_NAME_free);
190 sk_X509_NAME_ENTRY_pop_free(rnm, X509_NAME_ENTRY_free);
194 static const BIT_STRING_BITNAME reason_flags[] = {
195 {0, "Unused", "unused"},
196 {1, "Key Compromise", "keyCompromise"},
197 {2, "CA Compromise", "CACompromise"},
198 {3, "Affiliation Changed", "affiliationChanged"},
199 {4, "Superseded", "superseded"},
200 {5, "Cessation Of Operation", "cessationOfOperation"},
201 {6, "Certificate Hold", "certificateHold"},
202 {7, "Privilege Withdrawn", "privilegeWithdrawn"},
203 {8, "AA Compromise", "AACompromise"},
208 set_reasons(ASN1_BIT_STRING **preas, char *value)
210 STACK_OF(CONF_VALUE) *rsk = NULL;
211 const BIT_STRING_BITNAME *pbn;
217 rsk = X509V3_parse_list(value);
220 for (i = 0; i < sk_CONF_VALUE_num(rsk); i++) {
221 bnam = sk_CONF_VALUE_value(rsk, i)->name;
223 *preas = ASN1_BIT_STRING_new();
227 for (pbn = reason_flags; pbn->lname; pbn++) {
228 if (!strcmp(pbn->sname, bnam)) {
229 if (!ASN1_BIT_STRING_set_bit(*preas,
241 sk_CONF_VALUE_pop_free(rsk, X509V3_conf_free);
246 print_reasons(BIO *out, const char *rname, ASN1_BIT_STRING *rflags, int indent)
249 const BIT_STRING_BITNAME *pbn;
251 BIO_printf(out, "%*s%s:\n%*s", indent, "", rname, indent + 2, "");
252 for (pbn = reason_flags; pbn->lname; pbn++) {
253 if (ASN1_BIT_STRING_get_bit(rflags, pbn->bitnum)) {
258 BIO_puts(out, pbn->lname);
262 BIO_puts(out, "<EMPTY>\n");
269 crldp_from_section(X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
273 DIST_POINT *point = NULL;
275 point = DIST_POINT_new();
278 for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
280 cnf = sk_CONF_VALUE_value(nval, i);
281 ret = set_dist_point_name(&point->distpoint, ctx, cnf);
286 if (!strcmp(cnf->name, "reasons")) {
287 if (!set_reasons(&point->reasons, cnf->value))
290 else if (!strcmp(cnf->name, "CRLissuer")) {
292 gnames_from_sectname(ctx, cnf->value);
293 if (!point->CRLissuer)
301 DIST_POINT_free(point);
306 v2i_crld(const X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
307 STACK_OF(CONF_VALUE) *nval)
309 STACK_OF(DIST_POINT) *crld = NULL;
310 GENERAL_NAMES *gens = NULL;
311 GENERAL_NAME *gen = NULL;
315 if (!(crld = sk_DIST_POINT_new_null()))
317 for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
319 cnf = sk_CONF_VALUE_value(nval, i);
321 STACK_OF(CONF_VALUE) *dpsect;
322 dpsect = X509V3_get_section(ctx, cnf->name);
325 point = crldp_from_section(ctx, dpsect);
326 X509V3_section_free(ctx, dpsect);
329 if (!sk_DIST_POINT_push(crld, point)) {
330 DIST_POINT_free(point);
334 if (!(gen = v2i_GENERAL_NAME(method, ctx, cnf)))
336 if (!(gens = GENERAL_NAMES_new()))
338 if (!sk_GENERAL_NAME_push(gens, gen))
341 if (!(point = DIST_POINT_new()))
343 if (!sk_DIST_POINT_push(crld, point)) {
344 DIST_POINT_free(point);
347 if (!(point->distpoint = DIST_POINT_NAME_new()))
349 point->distpoint->name.fullname = gens;
350 point->distpoint->type = 0;
357 X509V3error(ERR_R_MALLOC_FAILURE);
359 GENERAL_NAME_free(gen);
360 GENERAL_NAMES_free(gens);
361 sk_DIST_POINT_pop_free(crld, DIST_POINT_free);
366 dpn_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg)
368 DIST_POINT_NAME *dpn = (DIST_POINT_NAME *)*pval;
371 case ASN1_OP_NEW_POST:
375 case ASN1_OP_FREE_POST:
377 X509_NAME_free(dpn->dpname);
384 static const ASN1_AUX DIST_POINT_NAME_aux = {
392 static const ASN1_TEMPLATE DIST_POINT_NAME_ch_tt[] = {
394 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_SEQUENCE_OF,
396 .offset = offsetof(DIST_POINT_NAME, name.fullname),
397 .field_name = "name.fullname",
398 .item = &GENERAL_NAME_it,
401 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_SET_OF,
403 .offset = offsetof(DIST_POINT_NAME, name.relativename),
404 .field_name = "name.relativename",
405 .item = &X509_NAME_ENTRY_it,
409 const ASN1_ITEM DIST_POINT_NAME_it = {
410 .itype = ASN1_ITYPE_CHOICE,
411 .utype = offsetof(DIST_POINT_NAME, type),
412 .templates = DIST_POINT_NAME_ch_tt,
413 .tcount = sizeof(DIST_POINT_NAME_ch_tt) / sizeof(ASN1_TEMPLATE),
414 .funcs = &DIST_POINT_NAME_aux,
415 .size = sizeof(DIST_POINT_NAME),
416 .sname = "DIST_POINT_NAME",
422 d2i_DIST_POINT_NAME(DIST_POINT_NAME **a, const unsigned char **in, long len)
424 return (DIST_POINT_NAME *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
425 &DIST_POINT_NAME_it);
429 i2d_DIST_POINT_NAME(DIST_POINT_NAME *a, unsigned char **out)
431 return ASN1_item_i2d((ASN1_VALUE *)a, out, &DIST_POINT_NAME_it);
435 DIST_POINT_NAME_new(void)
437 return (DIST_POINT_NAME *)ASN1_item_new(&DIST_POINT_NAME_it);
441 DIST_POINT_NAME_free(DIST_POINT_NAME *a)
443 ASN1_item_free((ASN1_VALUE *)a, &DIST_POINT_NAME_it);
446 static const ASN1_TEMPLATE DIST_POINT_seq_tt[] = {
448 .flags = ASN1_TFLG_EXPLICIT | ASN1_TFLG_OPTIONAL,
450 .offset = offsetof(DIST_POINT, distpoint),
451 .field_name = "distpoint",
452 .item = &DIST_POINT_NAME_it,
455 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_OPTIONAL,
457 .offset = offsetof(DIST_POINT, reasons),
458 .field_name = "reasons",
459 .item = &ASN1_BIT_STRING_it,
462 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_OPTIONAL,
464 .offset = offsetof(DIST_POINT, CRLissuer),
465 .field_name = "CRLissuer",
466 .item = &GENERAL_NAME_it,
470 const ASN1_ITEM DIST_POINT_it = {
471 .itype = ASN1_ITYPE_SEQUENCE,
472 .utype = V_ASN1_SEQUENCE,
473 .templates = DIST_POINT_seq_tt,
474 .tcount = sizeof(DIST_POINT_seq_tt) / sizeof(ASN1_TEMPLATE),
476 .size = sizeof(DIST_POINT),
477 .sname = "DIST_POINT",
482 d2i_DIST_POINT(DIST_POINT **a, const unsigned char **in, long len)
484 return (DIST_POINT *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
489 i2d_DIST_POINT(DIST_POINT *a, unsigned char **out)
491 return ASN1_item_i2d((ASN1_VALUE *)a, out, &DIST_POINT_it);
497 return (DIST_POINT *)ASN1_item_new(&DIST_POINT_it);
501 DIST_POINT_free(DIST_POINT *a)
503 ASN1_item_free((ASN1_VALUE *)a, &DIST_POINT_it);
506 static const ASN1_TEMPLATE CRL_DIST_POINTS_item_tt = {
507 .flags = ASN1_TFLG_SEQUENCE_OF,
510 .field_name = "CRLDistributionPoints",
511 .item = &DIST_POINT_it,
514 const ASN1_ITEM CRL_DIST_POINTS_it = {
515 .itype = ASN1_ITYPE_PRIMITIVE,
517 .templates = &CRL_DIST_POINTS_item_tt,
521 .sname = "CRL_DIST_POINTS",
526 d2i_CRL_DIST_POINTS(CRL_DIST_POINTS **a, const unsigned char **in, long len)
528 return (CRL_DIST_POINTS *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
529 &CRL_DIST_POINTS_it);
533 i2d_CRL_DIST_POINTS(CRL_DIST_POINTS *a, unsigned char **out)
535 return ASN1_item_i2d((ASN1_VALUE *)a, out, &CRL_DIST_POINTS_it);
539 CRL_DIST_POINTS_new(void)
541 return (CRL_DIST_POINTS *)ASN1_item_new(&CRL_DIST_POINTS_it);
545 CRL_DIST_POINTS_free(CRL_DIST_POINTS *a)
547 ASN1_item_free((ASN1_VALUE *)a, &CRL_DIST_POINTS_it);
550 static const ASN1_TEMPLATE ISSUING_DIST_POINT_seq_tt[] = {
552 .flags = ASN1_TFLG_EXPLICIT | ASN1_TFLG_OPTIONAL,
554 .offset = offsetof(ISSUING_DIST_POINT, distpoint),
555 .field_name = "distpoint",
556 .item = &DIST_POINT_NAME_it,
559 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_OPTIONAL,
561 .offset = offsetof(ISSUING_DIST_POINT, onlyuser),
562 .field_name = "onlyuser",
563 .item = &ASN1_FBOOLEAN_it,
566 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_OPTIONAL,
568 .offset = offsetof(ISSUING_DIST_POINT, onlyCA),
569 .field_name = "onlyCA",
570 .item = &ASN1_FBOOLEAN_it,
573 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_OPTIONAL,
575 .offset = offsetof(ISSUING_DIST_POINT, onlysomereasons),
576 .field_name = "onlysomereasons",
577 .item = &ASN1_BIT_STRING_it,
580 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_OPTIONAL,
582 .offset = offsetof(ISSUING_DIST_POINT, indirectCRL),
583 .field_name = "indirectCRL",
584 .item = &ASN1_FBOOLEAN_it,
587 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_OPTIONAL,
589 .offset = offsetof(ISSUING_DIST_POINT, onlyattr),
590 .field_name = "onlyattr",
591 .item = &ASN1_FBOOLEAN_it,
595 const ASN1_ITEM ISSUING_DIST_POINT_it = {
596 .itype = ASN1_ITYPE_SEQUENCE,
597 .utype = V_ASN1_SEQUENCE,
598 .templates = ISSUING_DIST_POINT_seq_tt,
599 .tcount = sizeof(ISSUING_DIST_POINT_seq_tt) / sizeof(ASN1_TEMPLATE),
601 .size = sizeof(ISSUING_DIST_POINT),
602 .sname = "ISSUING_DIST_POINT",
607 d2i_ISSUING_DIST_POINT(ISSUING_DIST_POINT **a, const unsigned char **in, long len)
609 return (ISSUING_DIST_POINT *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
610 &ISSUING_DIST_POINT_it);
614 i2d_ISSUING_DIST_POINT(ISSUING_DIST_POINT *a, unsigned char **out)
616 return ASN1_item_i2d((ASN1_VALUE *)a, out, &ISSUING_DIST_POINT_it);
620 ISSUING_DIST_POINT_new(void)
622 return (ISSUING_DIST_POINT *)ASN1_item_new(&ISSUING_DIST_POINT_it);
626 ISSUING_DIST_POINT_free(ISSUING_DIST_POINT *a)
628 ASN1_item_free((ASN1_VALUE *)a, &ISSUING_DIST_POINT_it);
631 static int i2r_idp(const X509V3_EXT_METHOD *method, void *pidp, BIO *out,
633 static void *v2i_idp(const X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
634 STACK_OF(CONF_VALUE) *nval);
636 const X509V3_EXT_METHOD v3_idp = {
637 NID_issuing_distribution_point, X509V3_EXT_MULTILINE,
638 &ISSUING_DIST_POINT_it,
648 v2i_idp(const X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
649 STACK_OF(CONF_VALUE) *nval)
651 ISSUING_DIST_POINT *idp = NULL;
656 idp = ISSUING_DIST_POINT_new();
659 for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
660 cnf = sk_CONF_VALUE_value(nval, i);
663 ret = set_dist_point_name(&idp->distpoint, ctx, cnf);
668 if (!strcmp(name, "onlyuser")) {
669 if (!X509V3_get_value_bool(cnf, &idp->onlyuser))
672 else if (!strcmp(name, "onlyCA")) {
673 if (!X509V3_get_value_bool(cnf, &idp->onlyCA))
676 else if (!strcmp(name, "onlyAA")) {
677 if (!X509V3_get_value_bool(cnf, &idp->onlyattr))
680 else if (!strcmp(name, "indirectCRL")) {
681 if (!X509V3_get_value_bool(cnf, &idp->indirectCRL))
684 else if (!strcmp(name, "onlysomereasons")) {
685 if (!set_reasons(&idp->onlysomereasons, val))
688 X509V3error(X509V3_R_INVALID_NAME);
689 X509V3_conf_err(cnf);
696 X509V3error(ERR_R_MALLOC_FAILURE);
698 ISSUING_DIST_POINT_free(idp);
703 print_gens(BIO *out, STACK_OF(GENERAL_NAME) *gens, int indent)
707 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
708 BIO_printf(out, "%*s", indent + 2, "");
709 GENERAL_NAME_print(out, sk_GENERAL_NAME_value(gens, i));
716 print_distpoint(BIO *out, DIST_POINT_NAME *dpn, int indent)
718 if (dpn->type == 0) {
719 BIO_printf(out, "%*sFull Name:\n", indent, "");
720 print_gens(out, dpn->name.fullname, indent);
723 ntmp.entries = dpn->name.relativename;
724 BIO_printf(out, "%*sRelative Name:\n%*s",
725 indent, "", indent + 2, "");
726 X509_NAME_print_ex(out, &ntmp, 0, XN_FLAG_ONELINE);
733 i2r_idp(const X509V3_EXT_METHOD *method, void *pidp, BIO *out, int indent)
735 ISSUING_DIST_POINT *idp = pidp;
738 print_distpoint(out, idp->distpoint, indent);
739 if (idp->onlyuser > 0)
740 BIO_printf(out, "%*sOnly User Certificates\n", indent, "");
742 BIO_printf(out, "%*sOnly CA Certificates\n", indent, "");
743 if (idp->indirectCRL > 0)
744 BIO_printf(out, "%*sIndirect CRL\n", indent, "");
745 if (idp->onlysomereasons)
746 print_reasons(out, "Only Some Reasons",
747 idp->onlysomereasons, indent);
748 if (idp->onlyattr > 0)
749 BIO_printf(out, "%*sOnly Attribute Certificates\n", indent, "");
750 if (!idp->distpoint && (idp->onlyuser <= 0) && (idp->onlyCA <= 0) &&
751 (idp->indirectCRL <= 0) && !idp->onlysomereasons &&
752 (idp->onlyattr <= 0))
753 BIO_printf(out, "%*s<EMPTY>\n", indent, "");
759 i2r_crldp(const X509V3_EXT_METHOD *method, void *pcrldp, BIO *out, int indent)
761 STACK_OF(DIST_POINT) *crld = pcrldp;
765 for (i = 0; i < sk_DIST_POINT_num(crld); i++) {
767 point = sk_DIST_POINT_value(crld, i);
768 if (point->distpoint)
769 print_distpoint(out, point->distpoint, indent);
771 print_reasons(out, "Reasons", point->reasons,
773 if (point->CRLissuer) {
774 BIO_printf(out, "%*sCRL Issuer:\n", indent, "");
775 print_gens(out, point->CRLissuer, indent);
782 DIST_POINT_set_dpname(DIST_POINT_NAME *dpn, X509_NAME *iname)
785 STACK_OF(X509_NAME_ENTRY) *frag;
788 if (!dpn || (dpn->type != 1))
790 frag = dpn->name.relativename;
791 dpn->dpname = X509_NAME_dup(iname);
794 for (i = 0; i < sk_X509_NAME_ENTRY_num(frag); i++) {
795 ne = sk_X509_NAME_ENTRY_value(frag, i);
796 if (!X509_NAME_add_entry(dpn->dpname, ne, -1, i ? 0 : 1)) {
797 X509_NAME_free(dpn->dpname);
802 /* generate cached encoding of name */
803 if (i2d_X509_NAME(dpn->dpname, NULL) < 0) {
804 X509_NAME_free(dpn->dpname);