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