2 * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "internal/cryptlib.h"
12 #include <openssl/conf.h>
13 #include <openssl/asn1.h>
14 #include <openssl/asn1t.h>
15 #include <openssl/x509v3.h>
17 #include "crypto/x509.h"
20 DEFINE_STACK_OF(CONF_VALUE)
21 DEFINE_STACK_OF(GENERAL_NAME)
22 DEFINE_STACK_OF(DIST_POINT)
23 DEFINE_STACK_OF(X509_NAME_ENTRY)
25 static void *v2i_crld(const X509V3_EXT_METHOD *method,
26 X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval);
27 static int i2r_crldp(const X509V3_EXT_METHOD *method, void *pcrldp, BIO *out,
30 const X509V3_EXT_METHOD v3_crld = {
31 NID_crl_distribution_points, 0, ASN1_ITEM_ref(CRL_DIST_POINTS),
40 const X509V3_EXT_METHOD v3_freshest_crl = {
41 NID_freshest_crl, 0, ASN1_ITEM_ref(CRL_DIST_POINTS),
50 static STACK_OF(GENERAL_NAME) *gnames_from_sectname(X509V3_CTX *ctx,
53 STACK_OF(CONF_VALUE) *gnsect;
54 STACK_OF(GENERAL_NAME) *gens;
56 gnsect = X509V3_get_section(ctx, sect + 1);
58 gnsect = X509V3_parse_list(sect);
60 X509V3err(X509V3_F_GNAMES_FROM_SECTNAME, X509V3_R_SECTION_NOT_FOUND);
63 gens = v2i_GENERAL_NAMES(NULL, ctx, gnsect);
65 X509V3_section_free(ctx, gnsect);
67 sk_CONF_VALUE_pop_free(gnsect, X509V3_conf_free);
71 static int set_dist_point_name(DIST_POINT_NAME **pdp, X509V3_CTX *ctx,
74 STACK_OF(GENERAL_NAME) *fnm = NULL;
75 STACK_OF(X509_NAME_ENTRY) *rnm = NULL;
77 if (strncmp(cnf->name, "fullname", 9) == 0) {
78 fnm = gnames_from_sectname(ctx, cnf->value);
81 } else if (strcmp(cnf->name, "relativename") == 0) {
83 STACK_OF(CONF_VALUE) *dnsect;
88 dnsect = X509V3_get_section(ctx, cnf->value);
90 X509V3err(X509V3_F_SET_DIST_POINT_NAME,
91 X509V3_R_SECTION_NOT_FOUND);
94 ret = X509V3_NAME_from_section(nm, dnsect, MBSTRING_ASC);
95 X509V3_section_free(ctx, dnsect);
99 if (!ret || sk_X509_NAME_ENTRY_num(rnm) <= 0)
102 * Since its a name fragment can't have more than one RDNSequence
104 if (sk_X509_NAME_ENTRY_value(rnm,
105 sk_X509_NAME_ENTRY_num(rnm) - 1)->set) {
106 X509V3err(X509V3_F_SET_DIST_POINT_NAME,
107 X509V3_R_INVALID_MULTIPLE_RDNS);
114 X509V3err(X509V3_F_SET_DIST_POINT_NAME,
115 X509V3_R_DISTPOINT_ALREADY_SET);
119 *pdp = DIST_POINT_NAME_new();
124 (*pdp)->name.fullname = fnm;
127 (*pdp)->name.relativename = rnm;
133 sk_GENERAL_NAME_pop_free(fnm, GENERAL_NAME_free);
134 sk_X509_NAME_ENTRY_pop_free(rnm, X509_NAME_ENTRY_free);
138 static const BIT_STRING_BITNAME reason_flags[] = {
139 {0, "Unused", "unused"},
140 {1, "Key Compromise", "keyCompromise"},
141 {2, "CA Compromise", "CACompromise"},
142 {3, "Affiliation Changed", "affiliationChanged"},
143 {4, "Superseded", "superseded"},
144 {5, "Cessation Of Operation", "cessationOfOperation"},
145 {6, "Certificate Hold", "certificateHold"},
146 {7, "Privilege Withdrawn", "privilegeWithdrawn"},
147 {8, "AA Compromise", "AACompromise"},
151 static int set_reasons(ASN1_BIT_STRING **preas, char *value)
153 STACK_OF(CONF_VALUE) *rsk = NULL;
154 const BIT_STRING_BITNAME *pbn;
157 rsk = X509V3_parse_list(value);
162 for (i = 0; i < sk_CONF_VALUE_num(rsk); i++) {
163 bnam = sk_CONF_VALUE_value(rsk, i)->name;
164 if (*preas == NULL) {
165 *preas = ASN1_BIT_STRING_new();
169 for (pbn = reason_flags; pbn->lname; pbn++) {
170 if (strcmp(pbn->sname, bnam) == 0) {
171 if (!ASN1_BIT_STRING_set_bit(*preas, pbn->bitnum, 1))
176 if (pbn->lname == NULL)
182 sk_CONF_VALUE_pop_free(rsk, X509V3_conf_free);
186 static int print_reasons(BIO *out, const char *rname,
187 ASN1_BIT_STRING *rflags, int indent)
190 const BIT_STRING_BITNAME *pbn;
191 BIO_printf(out, "%*s%s:\n%*s", indent, "", rname, indent + 2, "");
192 for (pbn = reason_flags; pbn->lname; pbn++) {
193 if (ASN1_BIT_STRING_get_bit(rflags, pbn->bitnum)) {
198 BIO_puts(out, pbn->lname);
202 BIO_puts(out, "<EMPTY>\n");
208 static DIST_POINT *crldp_from_section(X509V3_CTX *ctx,
209 STACK_OF(CONF_VALUE) *nval)
213 DIST_POINT *point = DIST_POINT_new();
217 for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
219 cnf = sk_CONF_VALUE_value(nval, i);
220 ret = set_dist_point_name(&point->distpoint, ctx, cnf);
225 if (strcmp(cnf->name, "reasons") == 0) {
226 if (!set_reasons(&point->reasons, cnf->value))
228 } else if (strcmp(cnf->name, "CRLissuer") == 0) {
229 point->CRLissuer = gnames_from_sectname(ctx, cnf->value);
230 if (point->CRLissuer == NULL)
238 DIST_POINT_free(point);
242 static void *v2i_crld(const X509V3_EXT_METHOD *method,
243 X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
245 STACK_OF(DIST_POINT) *crld;
246 GENERAL_NAMES *gens = NULL;
247 GENERAL_NAME *gen = NULL;
249 const int num = sk_CONF_VALUE_num(nval);
252 crld = sk_DIST_POINT_new_reserve(NULL, num);
255 for (i = 0; i < num; i++) {
258 cnf = sk_CONF_VALUE_value(nval, i);
260 STACK_OF(CONF_VALUE) *dpsect;
261 dpsect = X509V3_get_section(ctx, cnf->name);
264 point = crldp_from_section(ctx, dpsect);
265 X509V3_section_free(ctx, dpsect);
268 sk_DIST_POINT_push(crld, point); /* no failure as it was reserved */
270 if ((gen = v2i_GENERAL_NAME(method, ctx, cnf)) == NULL)
272 if ((gens = GENERAL_NAMES_new()) == NULL)
274 if (!sk_GENERAL_NAME_push(gens, gen))
277 if ((point = DIST_POINT_new()) == NULL)
279 sk_DIST_POINT_push(crld, point); /* no failure as it was reserved */
280 if ((point->distpoint = DIST_POINT_NAME_new()) == NULL)
282 point->distpoint->name.fullname = gens;
283 point->distpoint->type = 0;
290 X509V3err(X509V3_F_V2I_CRLD, ERR_R_MALLOC_FAILURE);
292 GENERAL_NAME_free(gen);
293 GENERAL_NAMES_free(gens);
294 sk_DIST_POINT_pop_free(crld, DIST_POINT_free);
298 static int dpn_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
301 DIST_POINT_NAME *dpn = (DIST_POINT_NAME *)*pval;
304 case ASN1_OP_NEW_POST:
308 case ASN1_OP_FREE_POST:
309 X509_NAME_free(dpn->dpname);
316 ASN1_CHOICE_cb(DIST_POINT_NAME, dpn_cb) = {
317 ASN1_IMP_SEQUENCE_OF(DIST_POINT_NAME, name.fullname, GENERAL_NAME, 0),
318 ASN1_IMP_SET_OF(DIST_POINT_NAME, name.relativename, X509_NAME_ENTRY, 1)
319 } ASN1_CHOICE_END_cb(DIST_POINT_NAME, DIST_POINT_NAME, type)
322 IMPLEMENT_ASN1_FUNCTIONS(DIST_POINT_NAME)
324 ASN1_SEQUENCE(DIST_POINT) = {
325 ASN1_EXP_OPT(DIST_POINT, distpoint, DIST_POINT_NAME, 0),
326 ASN1_IMP_OPT(DIST_POINT, reasons, ASN1_BIT_STRING, 1),
327 ASN1_IMP_SEQUENCE_OF_OPT(DIST_POINT, CRLissuer, GENERAL_NAME, 2)
328 } ASN1_SEQUENCE_END(DIST_POINT)
330 IMPLEMENT_ASN1_FUNCTIONS(DIST_POINT)
332 ASN1_ITEM_TEMPLATE(CRL_DIST_POINTS) =
333 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, CRLDistributionPoints, DIST_POINT)
334 ASN1_ITEM_TEMPLATE_END(CRL_DIST_POINTS)
336 IMPLEMENT_ASN1_FUNCTIONS(CRL_DIST_POINTS)
338 ASN1_SEQUENCE(ISSUING_DIST_POINT) = {
339 ASN1_EXP_OPT(ISSUING_DIST_POINT, distpoint, DIST_POINT_NAME, 0),
340 ASN1_IMP_OPT(ISSUING_DIST_POINT, onlyuser, ASN1_FBOOLEAN, 1),
341 ASN1_IMP_OPT(ISSUING_DIST_POINT, onlyCA, ASN1_FBOOLEAN, 2),
342 ASN1_IMP_OPT(ISSUING_DIST_POINT, onlysomereasons, ASN1_BIT_STRING, 3),
343 ASN1_IMP_OPT(ISSUING_DIST_POINT, indirectCRL, ASN1_FBOOLEAN, 4),
344 ASN1_IMP_OPT(ISSUING_DIST_POINT, onlyattr, ASN1_FBOOLEAN, 5)
345 } ASN1_SEQUENCE_END(ISSUING_DIST_POINT)
347 IMPLEMENT_ASN1_FUNCTIONS(ISSUING_DIST_POINT)
349 static int i2r_idp(const X509V3_EXT_METHOD *method, void *pidp, BIO *out,
351 static void *v2i_idp(const X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
352 STACK_OF(CONF_VALUE) *nval);
354 const X509V3_EXT_METHOD v3_idp = {
355 NID_issuing_distribution_point, X509V3_EXT_MULTILINE,
356 ASN1_ITEM_ref(ISSUING_DIST_POINT),
365 static void *v2i_idp(const X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
366 STACK_OF(CONF_VALUE) *nval)
368 ISSUING_DIST_POINT *idp = NULL;
372 idp = ISSUING_DIST_POINT_new();
375 for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
376 cnf = sk_CONF_VALUE_value(nval, i);
379 ret = set_dist_point_name(&idp->distpoint, ctx, cnf);
384 if (strcmp(name, "onlyuser") == 0) {
385 if (!X509V3_get_value_bool(cnf, &idp->onlyuser))
387 } else if (strcmp(name, "onlyCA") == 0) {
388 if (!X509V3_get_value_bool(cnf, &idp->onlyCA))
390 } else if (strcmp(name, "onlyAA") == 0) {
391 if (!X509V3_get_value_bool(cnf, &idp->onlyattr))
393 } else if (strcmp(name, "indirectCRL") == 0) {
394 if (!X509V3_get_value_bool(cnf, &idp->indirectCRL))
396 } else if (strcmp(name, "onlysomereasons") == 0) {
397 if (!set_reasons(&idp->onlysomereasons, val))
400 X509V3err(X509V3_F_V2I_IDP, X509V3_R_INVALID_NAME);
401 X509V3_conf_err(cnf);
408 X509V3err(X509V3_F_V2I_IDP, ERR_R_MALLOC_FAILURE);
410 ISSUING_DIST_POINT_free(idp);
414 static int print_gens(BIO *out, STACK_OF(GENERAL_NAME) *gens, int indent)
417 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
420 BIO_printf(out, "%*s", indent + 2, "");
421 GENERAL_NAME_print(out, sk_GENERAL_NAME_value(gens, i));
426 static int print_distpoint(BIO *out, DIST_POINT_NAME *dpn, int indent)
428 if (dpn->type == 0) {
429 BIO_printf(out, "%*sFull Name:\n", indent, "");
430 print_gens(out, dpn->name.fullname, indent);
433 ntmp.entries = dpn->name.relativename;
434 BIO_printf(out, "%*sRelative Name:\n%*s", indent, "", indent + 2, "");
435 X509_NAME_print_ex(out, &ntmp, 0, XN_FLAG_ONELINE);
441 static int i2r_idp(const X509V3_EXT_METHOD *method, void *pidp, BIO *out,
444 ISSUING_DIST_POINT *idp = pidp;
446 print_distpoint(out, idp->distpoint, indent);
447 if (idp->onlyuser > 0)
448 BIO_printf(out, "%*sOnly User Certificates\n", indent, "");
450 BIO_printf(out, "%*sOnly CA Certificates\n", indent, "");
451 if (idp->indirectCRL > 0)
452 BIO_printf(out, "%*sIndirect CRL\n", indent, "");
453 if (idp->onlysomereasons)
454 print_reasons(out, "Only Some Reasons", idp->onlysomereasons, indent);
455 if (idp->onlyattr > 0)
456 BIO_printf(out, "%*sOnly Attribute Certificates\n", indent, "");
457 if (!idp->distpoint && (idp->onlyuser <= 0) && (idp->onlyCA <= 0)
458 && (idp->indirectCRL <= 0) && !idp->onlysomereasons
459 && (idp->onlyattr <= 0))
460 BIO_printf(out, "%*s<EMPTY>\n", indent, "");
465 static int i2r_crldp(const X509V3_EXT_METHOD *method, void *pcrldp, BIO *out,
468 STACK_OF(DIST_POINT) *crld = pcrldp;
471 for (i = 0; i < sk_DIST_POINT_num(crld); i++) {
474 point = sk_DIST_POINT_value(crld, i);
475 if (point->distpoint)
476 print_distpoint(out, point->distpoint, indent);
478 print_reasons(out, "Reasons", point->reasons, indent);
479 if (point->CRLissuer) {
480 BIO_printf(out, "%*sCRL Issuer:\n", indent, "");
481 print_gens(out, point->CRLissuer, indent);
487 int DIST_POINT_set_dpname(DIST_POINT_NAME *dpn, const X509_NAME *iname)
490 STACK_OF(X509_NAME_ENTRY) *frag;
492 if (!dpn || (dpn->type != 1))
494 frag = dpn->name.relativename;
495 dpn->dpname = X509_NAME_dup(iname);
498 for (i = 0; i < sk_X509_NAME_ENTRY_num(frag); i++) {
499 ne = sk_X509_NAME_ENTRY_value(frag, i);
500 if (!X509_NAME_add_entry(dpn->dpname, ne, -1, i ? 0 : 1)) {
501 X509_NAME_free(dpn->dpname);
506 /* generate cached encoding of name */
507 if (i2d_X509_NAME(dpn->dpname, NULL) < 0) {
508 X509_NAME_free(dpn->dpname);