]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/x509/v3_crld.c
80-test_cmp_http: Make server diagnostics more verbose to aid debugging
[thirdparty/openssl.git] / crypto / x509 / v3_crld.c
CommitLineData
0f113f3e 1/*
3c2bdd7d 2 * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
d943e372 3 *
4286ca47 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
d2e9e320
RS
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
d943e372
DSH
8 */
9
10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
ec577822
BM
12#include <openssl/conf.h>
13#include <openssl/asn1.h>
9d6b1ce6 14#include <openssl/asn1t.h>
ec577822 15#include <openssl/x509v3.h>
d943e372 16
25f2138b 17#include "crypto/x509.h"
df2ee0e2 18#include "ext_dat.h"
c90c4693 19#include "x509_local.h"
2743e38c 20
babb3798 21static void *v2i_crld(const X509V3_EXT_METHOD *method,
0f113f3e 22 X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval);
babb3798 23static int i2r_crldp(const X509V3_EXT_METHOD *method, void *pcrldp, BIO *out,
0f113f3e
MC
24 int indent);
25
47864aea 26const X509V3_EXT_METHOD ossl_v3_crld = {
0f113f3e
MC
27 NID_crl_distribution_points, 0, ASN1_ITEM_ref(CRL_DIST_POINTS),
28 0, 0, 0, 0,
29 0, 0,
30 0,
31 v2i_crld,
32 i2r_crldp, 0,
33 NULL
34};
35
47864aea 36const X509V3_EXT_METHOD ossl_v3_freshest_crl = {
0f113f3e
MC
37 NID_freshest_crl, 0, ASN1_ITEM_ref(CRL_DIST_POINTS),
38 0, 0, 0, 0,
39 0, 0,
40 0,
41 v2i_crld,
42 i2r_crldp, 0,
43 NULL
44};
45
46static STACK_OF(GENERAL_NAME) *gnames_from_sectname(X509V3_CTX *ctx,
47 char *sect)
48{
49 STACK_OF(CONF_VALUE) *gnsect;
50 STACK_OF(GENERAL_NAME) *gens;
51 if (*sect == '@')
52 gnsect = X509V3_get_section(ctx, sect + 1);
53 else
54 gnsect = X509V3_parse_list(sect);
55 if (!gnsect) {
9311d0c4 56 ERR_raise(ERR_LIB_X509V3, X509V3_R_SECTION_NOT_FOUND);
0f113f3e
MC
57 return NULL;
58 }
59 gens = v2i_GENERAL_NAMES(NULL, ctx, gnsect);
60 if (*sect == '@')
61 X509V3_section_free(ctx, gnsect);
62 else
63 sk_CONF_VALUE_pop_free(gnsect, X509V3_conf_free);
64 return gens;
65}
0745d089 66
0537f968 67static int set_dist_point_name(DIST_POINT_NAME **pdp, X509V3_CTX *ctx,
0f113f3e
MC
68 CONF_VALUE *cnf)
69{
70 STACK_OF(GENERAL_NAME) *fnm = NULL;
71 STACK_OF(X509_NAME_ENTRY) *rnm = NULL;
86885c28
RS
72
73 if (strncmp(cnf->name, "fullname", 9) == 0) {
0f113f3e
MC
74 fnm = gnames_from_sectname(ctx, cnf->value);
75 if (!fnm)
76 goto err;
86885c28 77 } else if (strcmp(cnf->name, "relativename") == 0) {
0f113f3e
MC
78 int ret;
79 STACK_OF(CONF_VALUE) *dnsect;
80 X509_NAME *nm;
81 nm = X509_NAME_new();
90945fa3 82 if (nm == NULL)
0f113f3e
MC
83 return -1;
84 dnsect = X509V3_get_section(ctx, cnf->value);
85 if (!dnsect) {
9311d0c4 86 ERR_raise(ERR_LIB_X509V3, X509V3_R_SECTION_NOT_FOUND);
0f113f3e
MC
87 return -1;
88 }
89 ret = X509V3_NAME_from_section(nm, dnsect, MBSTRING_ASC);
90 X509V3_section_free(ctx, dnsect);
91 rnm = nm->entries;
92 nm->entries = NULL;
93 X509_NAME_free(nm);
94 if (!ret || sk_X509_NAME_ENTRY_num(rnm) <= 0)
95 goto err;
96 /*
97 * Since its a name fragment can't have more than one RDNSequence
98 */
99 if (sk_X509_NAME_ENTRY_value(rnm,
100 sk_X509_NAME_ENTRY_num(rnm) - 1)->set) {
9311d0c4 101 ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_MULTIPLE_RDNS);
0f113f3e
MC
102 goto err;
103 }
104 } else
105 return 0;
106
107 if (*pdp) {
9311d0c4 108 ERR_raise(ERR_LIB_X509V3, X509V3_R_DISTPOINT_ALREADY_SET);
0f113f3e
MC
109 goto err;
110 }
111
112 *pdp = DIST_POINT_NAME_new();
90945fa3 113 if (*pdp == NULL)
0f113f3e
MC
114 goto err;
115 if (fnm) {
116 (*pdp)->type = 0;
117 (*pdp)->name.fullname = fnm;
118 } else {
119 (*pdp)->type = 1;
120 (*pdp)->name.relativename = rnm;
121 }
122
123 return 1;
124
125 err:
25aaa98a 126 sk_GENERAL_NAME_pop_free(fnm, GENERAL_NAME_free);
222561fe 127 sk_X509_NAME_ENTRY_pop_free(rnm, X509_NAME_ENTRY_free);
0f113f3e
MC
128 return -1;
129}
0745d089 130
0745d089 131static const BIT_STRING_BITNAME reason_flags[] = {
0f113f3e
MC
132 {0, "Unused", "unused"},
133 {1, "Key Compromise", "keyCompromise"},
134 {2, "CA Compromise", "CACompromise"},
135 {3, "Affiliation Changed", "affiliationChanged"},
136 {4, "Superseded", "superseded"},
137 {5, "Cessation Of Operation", "cessationOfOperation"},
138 {6, "Certificate Hold", "certificateHold"},
139 {7, "Privilege Withdrawn", "privilegeWithdrawn"},
140 {8, "AA Compromise", "AACompromise"},
141 {-1, NULL, NULL}
d943e372
DSH
142};
143
0745d089 144static int set_reasons(ASN1_BIT_STRING **preas, char *value)
0f113f3e
MC
145{
146 STACK_OF(CONF_VALUE) *rsk = NULL;
147 const BIT_STRING_BITNAME *pbn;
148 const char *bnam;
149 int i, ret = 0;
150 rsk = X509V3_parse_list(value);
723412d4 151 if (rsk == NULL)
0f113f3e 152 return 0;
723412d4
MC
153 if (*preas != NULL)
154 goto err;
0f113f3e
MC
155 for (i = 0; i < sk_CONF_VALUE_num(rsk); i++) {
156 bnam = sk_CONF_VALUE_value(rsk, i)->name;
90945fa3 157 if (*preas == NULL) {
0f113f3e 158 *preas = ASN1_BIT_STRING_new();
90945fa3 159 if (*preas == NULL)
0f113f3e
MC
160 goto err;
161 }
162 for (pbn = reason_flags; pbn->lname; pbn++) {
86885c28 163 if (strcmp(pbn->sname, bnam) == 0) {
0f113f3e
MC
164 if (!ASN1_BIT_STRING_set_bit(*preas, pbn->bitnum, 1))
165 goto err;
166 break;
167 }
168 }
12a765a5 169 if (pbn->lname == NULL)
0f113f3e
MC
170 goto err;
171 }
172 ret = 1;
173
174 err:
175 sk_CONF_VALUE_pop_free(rsk, X509V3_conf_free);
176 return ret;
177}
0745d089
DSH
178
179static int print_reasons(BIO *out, const char *rname,
0f113f3e
MC
180 ASN1_BIT_STRING *rflags, int indent)
181{
182 int first = 1;
183 const BIT_STRING_BITNAME *pbn;
184 BIO_printf(out, "%*s%s:\n%*s", indent, "", rname, indent + 2, "");
185 for (pbn = reason_flags; pbn->lname; pbn++) {
186 if (ASN1_BIT_STRING_get_bit(rflags, pbn->bitnum)) {
187 if (first)
188 first = 0;
189 else
190 BIO_puts(out, ", ");
191 BIO_puts(out, pbn->lname);
192 }
193 }
194 if (first)
195 BIO_puts(out, "<EMPTY>\n");
196 else
197 BIO_puts(out, "\n");
198 return 1;
199}
0745d089
DSH
200
201static DIST_POINT *crldp_from_section(X509V3_CTX *ctx,
0f113f3e
MC
202 STACK_OF(CONF_VALUE) *nval)
203{
204 int i;
205 CONF_VALUE *cnf;
270a4bba
F
206 DIST_POINT *point = DIST_POINT_new();
207
90945fa3 208 if (point == NULL)
0f113f3e
MC
209 goto err;
210 for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
211 int ret;
212 cnf = sk_CONF_VALUE_value(nval, i);
213 ret = set_dist_point_name(&point->distpoint, ctx, cnf);
214 if (ret > 0)
215 continue;
216 if (ret < 0)
217 goto err;
86885c28 218 if (strcmp(cnf->name, "reasons") == 0) {
0f113f3e
MC
219 if (!set_reasons(&point->reasons, cnf->value))
220 goto err;
86885c28 221 } else if (strcmp(cnf->name, "CRLissuer") == 0) {
0f113f3e 222 point->CRLissuer = gnames_from_sectname(ctx, cnf->value);
12a765a5 223 if (point->CRLissuer == NULL)
0f113f3e
MC
224 goto err;
225 }
226 }
227
228 return point;
229
230 err:
25aaa98a 231 DIST_POINT_free(point);
0f113f3e
MC
232 return NULL;
233}
0745d089 234
babb3798 235static void *v2i_crld(const X509V3_EXT_METHOD *method,
0f113f3e
MC
236 X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
237{
270a4bba 238 STACK_OF(DIST_POINT) *crld;
0f113f3e
MC
239 GENERAL_NAMES *gens = NULL;
240 GENERAL_NAME *gen = NULL;
241 CONF_VALUE *cnf;
270a4bba 242 const int num = sk_CONF_VALUE_num(nval);
0f113f3e 243 int i;
75ebbd9a 244
7a908204
PY
245 crld = sk_DIST_POINT_new_reserve(NULL, num);
246 if (crld == NULL)
0f113f3e 247 goto merr;
270a4bba 248 for (i = 0; i < num; i++) {
0f113f3e 249 DIST_POINT *point;
270a4bba 250
0f113f3e 251 cnf = sk_CONF_VALUE_value(nval, i);
c90c4693 252 if (cnf->value == NULL) {
0f113f3e
MC
253 STACK_OF(CONF_VALUE) *dpsect;
254 dpsect = X509V3_get_section(ctx, cnf->name);
255 if (!dpsect)
256 goto err;
257 point = crldp_from_section(ctx, dpsect);
258 X509V3_section_free(ctx, dpsect);
12a765a5 259 if (point == NULL)
0f113f3e 260 goto err;
270a4bba 261 sk_DIST_POINT_push(crld, point); /* no failure as it was reserved */
0f113f3e 262 } else {
75ebbd9a 263 if ((gen = v2i_GENERAL_NAME(method, ctx, cnf)) == NULL)
0f113f3e 264 goto err;
75ebbd9a 265 if ((gens = GENERAL_NAMES_new()) == NULL)
0f113f3e
MC
266 goto merr;
267 if (!sk_GENERAL_NAME_push(gens, gen))
268 goto merr;
269 gen = NULL;
75ebbd9a 270 if ((point = DIST_POINT_new()) == NULL)
0f113f3e 271 goto merr;
270a4bba 272 sk_DIST_POINT_push(crld, point); /* no failure as it was reserved */
75ebbd9a 273 if ((point->distpoint = DIST_POINT_NAME_new()) == NULL)
0f113f3e
MC
274 goto merr;
275 point->distpoint->name.fullname = gens;
276 point->distpoint->type = 0;
277 gens = NULL;
278 }
279 }
280 return crld;
281
282 merr:
9311d0c4 283 ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
284 err:
285 GENERAL_NAME_free(gen);
286 GENERAL_NAMES_free(gens);
287 sk_DIST_POINT_pop_free(crld, DIST_POINT_free);
288 return NULL;
d943e372
DSH
289}
290
3e727a3b 291static int dpn_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
0f113f3e
MC
292 void *exarg)
293{
294 DIST_POINT_NAME *dpn = (DIST_POINT_NAME *)*pval;
295
296 switch (operation) {
297 case ASN1_OP_NEW_POST:
298 dpn->dpname = NULL;
299 break;
300
301 case ASN1_OP_FREE_POST:
222561fe 302 X509_NAME_free(dpn->dpname);
0f113f3e
MC
303 break;
304 }
305 return 1;
306}
d943e372 307
3e727a3b
DSH
308
309ASN1_CHOICE_cb(DIST_POINT_NAME, dpn_cb) = {
0f113f3e
MC
310 ASN1_IMP_SEQUENCE_OF(DIST_POINT_NAME, name.fullname, GENERAL_NAME, 0),
311 ASN1_IMP_SET_OF(DIST_POINT_NAME, name.relativename, X509_NAME_ENTRY, 1)
3e727a3b
DSH
312} ASN1_CHOICE_END_cb(DIST_POINT_NAME, DIST_POINT_NAME, type)
313
d943e372 314
9d6b1ce6 315IMPLEMENT_ASN1_FUNCTIONS(DIST_POINT_NAME)
d943e372 316
9d6b1ce6 317ASN1_SEQUENCE(DIST_POINT) = {
0f113f3e
MC
318 ASN1_EXP_OPT(DIST_POINT, distpoint, DIST_POINT_NAME, 0),
319 ASN1_IMP_OPT(DIST_POINT, reasons, ASN1_BIT_STRING, 1),
320 ASN1_IMP_SEQUENCE_OF_OPT(DIST_POINT, CRLissuer, GENERAL_NAME, 2)
d339187b 321} ASN1_SEQUENCE_END(DIST_POINT)
d943e372 322
9d6b1ce6 323IMPLEMENT_ASN1_FUNCTIONS(DIST_POINT)
d943e372 324
0f113f3e
MC
325ASN1_ITEM_TEMPLATE(CRL_DIST_POINTS) =
326 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, CRLDistributionPoints, DIST_POINT)
d339187b 327ASN1_ITEM_TEMPLATE_END(CRL_DIST_POINTS)
d943e372 328
9d6b1ce6 329IMPLEMENT_ASN1_FUNCTIONS(CRL_DIST_POINTS)
231493c9
DSH
330
331ASN1_SEQUENCE(ISSUING_DIST_POINT) = {
0f113f3e
MC
332 ASN1_EXP_OPT(ISSUING_DIST_POINT, distpoint, DIST_POINT_NAME, 0),
333 ASN1_IMP_OPT(ISSUING_DIST_POINT, onlyuser, ASN1_FBOOLEAN, 1),
334 ASN1_IMP_OPT(ISSUING_DIST_POINT, onlyCA, ASN1_FBOOLEAN, 2),
335 ASN1_IMP_OPT(ISSUING_DIST_POINT, onlysomereasons, ASN1_BIT_STRING, 3),
336 ASN1_IMP_OPT(ISSUING_DIST_POINT, indirectCRL, ASN1_FBOOLEAN, 4),
337 ASN1_IMP_OPT(ISSUING_DIST_POINT, onlyattr, ASN1_FBOOLEAN, 5)
231493c9
DSH
338} ASN1_SEQUENCE_END(ISSUING_DIST_POINT)
339
0537f968
DSH
340IMPLEMENT_ASN1_FUNCTIONS(ISSUING_DIST_POINT)
341
babb3798 342static int i2r_idp(const X509V3_EXT_METHOD *method, void *pidp, BIO *out,
0f113f3e 343 int indent);
babb3798 344static void *v2i_idp(const X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
0f113f3e
MC
345 STACK_OF(CONF_VALUE) *nval);
346
47864aea 347const X509V3_EXT_METHOD ossl_v3_idp = {
0f113f3e
MC
348 NID_issuing_distribution_point, X509V3_EXT_MULTILINE,
349 ASN1_ITEM_ref(ISSUING_DIST_POINT),
350 0, 0, 0, 0,
351 0, 0,
352 0,
353 v2i_idp,
354 i2r_idp, 0,
355 NULL
356};
231493c9 357
babb3798 358static void *v2i_idp(const X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
0f113f3e
MC
359 STACK_OF(CONF_VALUE) *nval)
360{
361 ISSUING_DIST_POINT *idp = NULL;
362 CONF_VALUE *cnf;
363 char *name, *val;
364 int i, ret;
365 idp = ISSUING_DIST_POINT_new();
90945fa3 366 if (idp == NULL)
0f113f3e
MC
367 goto merr;
368 for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
369 cnf = sk_CONF_VALUE_value(nval, i);
370 name = cnf->name;
371 val = cnf->value;
372 ret = set_dist_point_name(&idp->distpoint, ctx, cnf);
373 if (ret > 0)
374 continue;
375 if (ret < 0)
376 goto err;
86885c28 377 if (strcmp(name, "onlyuser") == 0) {
0f113f3e
MC
378 if (!X509V3_get_value_bool(cnf, &idp->onlyuser))
379 goto err;
86885c28 380 } else if (strcmp(name, "onlyCA") == 0) {
0f113f3e
MC
381 if (!X509V3_get_value_bool(cnf, &idp->onlyCA))
382 goto err;
86885c28 383 } else if (strcmp(name, "onlyAA") == 0) {
0f113f3e
MC
384 if (!X509V3_get_value_bool(cnf, &idp->onlyattr))
385 goto err;
86885c28 386 } else if (strcmp(name, "indirectCRL") == 0) {
0f113f3e
MC
387 if (!X509V3_get_value_bool(cnf, &idp->indirectCRL))
388 goto err;
86885c28 389 } else if (strcmp(name, "onlysomereasons") == 0) {
0f113f3e
MC
390 if (!set_reasons(&idp->onlysomereasons, val))
391 goto err;
392 } else {
9311d0c4 393 ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_NAME);
c90c4693 394 X509V3_conf_add_error_name_value(cnf);
0f113f3e
MC
395 goto err;
396 }
397 }
398 return idp;
399
400 merr:
9311d0c4 401 ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
402 err:
403 ISSUING_DIST_POINT_free(idp);
404 return NULL;
405}
0537f968 406
9aa9d70d 407static int print_gens(BIO *out, STACK_OF(GENERAL_NAME) *gens, int indent)
0f113f3e
MC
408{
409 int i;
410 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
a4c467c9
DO
411 if (i > 0)
412 BIO_puts(out, "\n");
0f113f3e
MC
413 BIO_printf(out, "%*s", indent + 2, "");
414 GENERAL_NAME_print(out, sk_GENERAL_NAME_value(gens, i));
0f113f3e
MC
415 }
416 return 1;
417}
9aa9d70d
DSH
418
419static int print_distpoint(BIO *out, DIST_POINT_NAME *dpn, int indent)
0f113f3e
MC
420{
421 if (dpn->type == 0) {
422 BIO_printf(out, "%*sFull Name:\n", indent, "");
423 print_gens(out, dpn->name.fullname, indent);
424 } else {
425 X509_NAME ntmp;
426 ntmp.entries = dpn->name.relativename;
427 BIO_printf(out, "%*sRelative Name:\n%*s", indent, "", indent + 2, "");
428 X509_NAME_print_ex(out, &ntmp, 0, XN_FLAG_ONELINE);
429 BIO_puts(out, "\n");
430 }
431 return 1;
432}
231493c9 433
babb3798 434static int i2r_idp(const X509V3_EXT_METHOD *method, void *pidp, BIO *out,
0f113f3e
MC
435 int indent)
436{
437 ISSUING_DIST_POINT *idp = pidp;
438 if (idp->distpoint)
439 print_distpoint(out, idp->distpoint, indent);
440 if (idp->onlyuser > 0)
441 BIO_printf(out, "%*sOnly User Certificates\n", indent, "");
442 if (idp->onlyCA > 0)
443 BIO_printf(out, "%*sOnly CA Certificates\n", indent, "");
444 if (idp->indirectCRL > 0)
445 BIO_printf(out, "%*sIndirect CRL\n", indent, "");
446 if (idp->onlysomereasons)
447 print_reasons(out, "Only Some Reasons", idp->onlysomereasons, indent);
448 if (idp->onlyattr > 0)
449 BIO_printf(out, "%*sOnly Attribute Certificates\n", indent, "");
450 if (!idp->distpoint && (idp->onlyuser <= 0) && (idp->onlyCA <= 0)
451 && (idp->indirectCRL <= 0) && !idp->onlysomereasons
452 && (idp->onlyattr <= 0))
453 BIO_printf(out, "%*s<EMPTY>\n", indent, "");
454
455 return 1;
456}
9aa9d70d 457
babb3798 458static int i2r_crldp(const X509V3_EXT_METHOD *method, void *pcrldp, BIO *out,
0f113f3e
MC
459 int indent)
460{
461 STACK_OF(DIST_POINT) *crld = pcrldp;
462 DIST_POINT *point;
463 int i;
464 for (i = 0; i < sk_DIST_POINT_num(crld); i++) {
a4c467c9
DO
465 if (i > 0)
466 BIO_puts(out, "\n");
0f113f3e
MC
467 point = sk_DIST_POINT_value(crld, i);
468 if (point->distpoint)
469 print_distpoint(out, point->distpoint, indent);
470 if (point->reasons)
471 print_reasons(out, "Reasons", point->reasons, indent);
472 if (point->CRLissuer) {
473 BIO_printf(out, "%*sCRL Issuer:\n", indent, "");
474 print_gens(out, point->CRLissuer, indent);
475 }
476 }
477 return 1;
478}
3e727a3b 479
1e41dadf 480/* Append any nameRelativeToCRLIssuer in dpn to iname, set in dpn->dpname */
8cc86b81 481int DIST_POINT_set_dpname(DIST_POINT_NAME *dpn, const X509_NAME *iname)
0f113f3e
MC
482{
483 int i;
484 STACK_OF(X509_NAME_ENTRY) *frag;
485 X509_NAME_ENTRY *ne;
1e41dadf
DDO
486
487 if (dpn == NULL || dpn->type != 1)
0f113f3e
MC
488 return 1;
489 frag = dpn->name.relativename;
1e41dadf 490 X509_NAME_free(dpn->dpname); /* just in case it was already set */
0f113f3e 491 dpn->dpname = X509_NAME_dup(iname);
1e41dadf 492 if (dpn->dpname == NULL)
0f113f3e
MC
493 return 0;
494 for (i = 0; i < sk_X509_NAME_ENTRY_num(frag); i++) {
495 ne = sk_X509_NAME_ENTRY_value(frag, i);
1e41dadf
DDO
496 if (!X509_NAME_add_entry(dpn->dpname, ne, -1, i ? 0 : 1))
497 goto err;
0f113f3e
MC
498 }
499 /* generate cached encoding of name */
1e41dadf
DDO
500 if (i2d_X509_NAME(dpn->dpname, NULL) >= 0)
501 return 1;
502
503 err:
504 X509_NAME_free(dpn->dpname);
505 dpn->dpname = NULL;
506 return 0;
0f113f3e 507}