Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/x509/v3_crld.c

raw
1 // Written by Dr Stephen N Henson ([email protected]) for the OpenSSL project 1999.
2 // Copyright (c) 1999-2008 The OpenSSL Project. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
4
5 #include <stdio.h>
6 #include <string.h>
7
8 #include <openssl/asn1.h>
9 #include <openssl/asn1t.h>
10 #include <openssl/conf.h>
11 #include <openssl/err.h>
12 #include <openssl/mem.h>
13 #include <openssl/obj.h>
14 #include <openssl/x509.h>
15
16 #include "internal.h"
17
18
19 static void *v2i_crld(const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx,
20 const STACK_OF(CONF_VALUE) *nval);
21 static int i2r_crldp(const X509V3_EXT_METHOD *method, void *pcrldp, BIO *out,
22 int indent);
23
24 const X509V3_EXT_METHOD v3_crld = {
25 NID_crl_distribution_points,
26 0,
27 ASN1_ITEM_ref(CRL_DIST_POINTS),
28 0,
29 0,
30 0,
31 0,
32 0,
33 0,
34 0,
35 v2i_crld,
36 i2r_crldp,
37 0,
38 NULL,
39 };
40
41 const X509V3_EXT_METHOD v3_freshest_crl = {
42 NID_freshest_crl,
43 0,
44 ASN1_ITEM_ref(CRL_DIST_POINTS),
45 0,
46 0,
47 0,
48 0,
49 0,
50 0,
51 0,
52 v2i_crld,
53 i2r_crldp,
54 0,
55 NULL,
56 };
57
58 static STACK_OF(GENERAL_NAME) *gnames_from_sectname(const X509V3_CTX *ctx,
59 char *sect) {
60 const STACK_OF(CONF_VALUE) *gnsect;
61 STACK_OF(CONF_VALUE) *gnsect_owned = NULL;
62 if (*sect == '@') {
63 gnsect = X509V3_get_section(ctx, sect + 1);
64 } else {
65 gnsect_owned = X509V3_parse_list(sect);
66 gnsect = gnsect_owned;
67 }
68 if (!gnsect) {
69 OPENSSL_PUT_ERROR(X509V3, X509V3_R_SECTION_NOT_FOUND);
70 return NULL;
71 }
72 STACK_OF(GENERAL_NAME) *gens = v2i_GENERAL_NAMES(NULL, ctx, gnsect);
73 sk_CONF_VALUE_pop_free(gnsect_owned, X509V3_conf_free);
74 return gens;
75 }
76
77 // set_dist_point_name decodes a DistributionPointName from |cnf| and writes the
78 // result in |*pdp|. It returns 1 on success, -1 on error, and 0 if |cnf| used
79 // an unrecognized input type. The zero return can be used by callers to support
80 // additional syntax.
81 static int set_dist_point_name(DIST_POINT_NAME **pdp, const X509V3_CTX *ctx,
82 const CONF_VALUE *cnf) {
83 STACK_OF(GENERAL_NAME) *fnm = NULL;
84 STACK_OF(X509_NAME_ENTRY) *rnm = NULL;
85 if (!strncmp(cnf->name, "fullname", 9)) {
86 // If |cnf| comes from |X509V3_parse_list|, which is possible for a v2i
87 // function, |cnf->value| may be NULL.
88 if (cnf->value == NULL) {
89 OPENSSL_PUT_ERROR(X509V3, X509V3_R_MISSING_VALUE);
90 return -1;
91 }
92 fnm = gnames_from_sectname(ctx, cnf->value);
93 if (!fnm) {
94 goto err;
95 }
96 } else if (!strcmp(cnf->name, "relativename")) {
97 // If |cnf| comes from |X509V3_parse_list|, which is possible for a v2i
98 // function, |cnf->value| may be NULL.
99 if (cnf->value == NULL) {
100 OPENSSL_PUT_ERROR(X509V3, X509V3_R_MISSING_VALUE);
101 return -1;
102 }
103 const STACK_OF(CONF_VALUE) *dnsect = X509V3_get_section(ctx, cnf->value);
104 if (!dnsect) {
105 OPENSSL_PUT_ERROR(X509V3, X509V3_R_SECTION_NOT_FOUND);
106 return -1;
107 }
108 X509_NAME *nm = X509_NAME_new();
109 if (!nm) {
110 return -1;
111 }
112 int ret = X509V3_NAME_from_section(nm, dnsect, MBSTRING_ASC);
113 rnm = nm->entries;
114 nm->entries = NULL;
115 X509_NAME_free(nm);
116 if (!ret || sk_X509_NAME_ENTRY_num(rnm) <= 0) {
117 goto err;
118 }
119 // There can only be one RDN in nameRelativeToCRLIssuer.
120 if (sk_X509_NAME_ENTRY_value(rnm, sk_X509_NAME_ENTRY_num(rnm) - 1)->set) {
121 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_MULTIPLE_RDNS);
122 goto err;
123 }
124 } else {
125 return 0;
126 }
127
128 if (*pdp) {
129 OPENSSL_PUT_ERROR(X509V3, X509V3_R_DISTPOINT_ALREADY_SET);
130 goto err;
131 }
132
133 *pdp = DIST_POINT_NAME_new();
134 if (!*pdp) {
135 goto err;
136 }
137 if (fnm) {
138 (*pdp)->type = 0;
139 (*pdp)->name.fullname = fnm;
140 } else {
141 (*pdp)->type = 1;
142 (*pdp)->name.relativename = rnm;
143 }
144
145 return 1;
146
147 err:
148 sk_GENERAL_NAME_pop_free(fnm, GENERAL_NAME_free);
149 sk_X509_NAME_ENTRY_pop_free(rnm, X509_NAME_ENTRY_free);
150 return -1;
151 }
152
153 static const BIT_STRING_BITNAME reason_flags[] = {
154 {0, "Unused", "unused"},
155 {1, "Key Compromise", "keyCompromise"},
156 {2, "CA Compromise", "CACompromise"},
157 {3, "Affiliation Changed", "affiliationChanged"},
158 {4, "Superseded", "superseded"},
159 {5, "Cessation Of Operation", "cessationOfOperation"},
160 {6, "Certificate Hold", "certificateHold"},
161 {7, "Privilege Withdrawn", "privilegeWithdrawn"},
162 {8, "AA Compromise", "AACompromise"},
163 {-1, NULL, NULL}};
164
165 static int set_reasons(ASN1_BIT_STRING **preas, const char *value) {
166 if (*preas) {
167 // Duplicate "reasons" or "onlysomereasons" key.
168 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_VALUE);
169 return 0;
170 }
171 int ret = 0;
172 STACK_OF(CONF_VALUE) *rsk = X509V3_parse_list(value);
173 if (!rsk) {
174 return 0;
175 }
176 for (size_t i = 0; i < sk_CONF_VALUE_num(rsk); i++) {
177 const char *bnam = sk_CONF_VALUE_value(rsk, i)->name;
178 if (!*preas) {
179 *preas = ASN1_BIT_STRING_new();
180 if (!*preas) {
181 goto err;
182 }
183 }
184 const BIT_STRING_BITNAME *pbn;
185 for (pbn = reason_flags; pbn->lname; pbn++) {
186 if (!strcmp(pbn->sname, bnam)) {
187 if (!ASN1_BIT_STRING_set_bit(*preas, pbn->bitnum, 1)) {
188 goto err;
189 }
190 break;
191 }
192 }
193 if (!pbn->lname) {
194 goto err;
195 }
196 }
197 ret = 1;
198
199 err:
200 sk_CONF_VALUE_pop_free(rsk, X509V3_conf_free);
201 return ret;
202 }
203
204 static int print_reasons(BIO *out, const char *rname, ASN1_BIT_STRING *rflags,
205 int indent) {
206 int first = 1;
207 const BIT_STRING_BITNAME *pbn;
208 BIO_printf(out, "%*s%s:\n%*s", indent, "", rname, indent + 2, "");
209 for (pbn = reason_flags; pbn->lname; pbn++) {
210 if (ASN1_BIT_STRING_get_bit(rflags, pbn->bitnum)) {
211 if (first) {
212 first = 0;
213 } else {
214 BIO_puts(out, ", ");
215 }
216 BIO_puts(out, pbn->lname);
217 }
218 }
219 if (first) {
220 BIO_puts(out, "<EMPTY>\n");
221 } else {
222 BIO_puts(out, "\n");
223 }
224 return 1;
225 }
226
227 static DIST_POINT *crldp_from_section(const X509V3_CTX *ctx,
228 const STACK_OF(CONF_VALUE) *nval) {
229 DIST_POINT *point = NULL;
230 point = DIST_POINT_new();
231 if (!point) {
232 goto err;
233 }
234 for (size_t i = 0; i < sk_CONF_VALUE_num(nval); i++) {
235 const CONF_VALUE *cnf = sk_CONF_VALUE_value(nval, i);
236 int ret = set_dist_point_name(&point->distpoint, ctx, cnf);
237 if (ret > 0) {
238 continue;
239 }
240 if (ret < 0) {
241 goto err;
242 }
243 if (!strcmp(cnf->name, "reasons")) {
244 if (!set_reasons(&point->reasons, cnf->value)) {
245 goto err;
246 }
247 } else if (!strcmp(cnf->name, "CRLissuer")) {
248 GENERAL_NAMES_free(point->CRLissuer);
249 point->CRLissuer = gnames_from_sectname(ctx, cnf->value);
250 if (!point->CRLissuer) {
251 goto err;
252 }
253 }
254 }
255
256 return point;
257
258 err:
259 DIST_POINT_free(point);
260 return NULL;
261 }
262
263 static void *v2i_crld(const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx,
264 const STACK_OF(CONF_VALUE) *nval) {
265 STACK_OF(DIST_POINT) *crld = NULL;
266 GENERAL_NAMES *gens = NULL;
267 GENERAL_NAME *gen = NULL;
268 if (!(crld = sk_DIST_POINT_new_null())) {
269 goto err;
270 }
271 for (size_t i = 0; i < sk_CONF_VALUE_num(nval); i++) {
272 DIST_POINT *point;
273 const CONF_VALUE *cnf = sk_CONF_VALUE_value(nval, i);
274 if (!cnf->value) {
275 const STACK_OF(CONF_VALUE) *dpsect = X509V3_get_section(ctx, cnf->name);
276 if (!dpsect) {
277 goto err;
278 }
279 point = crldp_from_section(ctx, dpsect);
280 if (!point) {
281 goto err;
282 }
283 if (!sk_DIST_POINT_push(crld, point)) {
284 DIST_POINT_free(point);
285 goto err;
286 }
287 } else {
288 if (!(gen = v2i_GENERAL_NAME(method, ctx, cnf))) {
289 goto err;
290 }
291 if (!(gens = GENERAL_NAMES_new())) {
292 goto err;
293 }
294 if (!sk_GENERAL_NAME_push(gens, gen)) {
295 goto err;
296 }
297 gen = NULL;
298 if (!(point = DIST_POINT_new())) {
299 goto err;
300 }
301 if (!sk_DIST_POINT_push(crld, point)) {
302 DIST_POINT_free(point);
303 goto err;
304 }
305 if (!(point->distpoint = DIST_POINT_NAME_new())) {
306 goto err;
307 }
308 point->distpoint->name.fullname = gens;
309 point->distpoint->type = 0;
310 gens = NULL;
311 }
312 }
313 return crld;
314
315 err:
316 GENERAL_NAME_free(gen);
317 GENERAL_NAMES_free(gens);
318 sk_DIST_POINT_pop_free(crld, DIST_POINT_free);
319 return NULL;
320 }
321
322 static int dpn_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
323 void *exarg) {
324 DIST_POINT_NAME *dpn = (DIST_POINT_NAME *)*pval;
325
326 switch (operation) {
327 case ASN1_OP_NEW_POST:
328 dpn->dpname = NULL;
329 break;
330
331 case ASN1_OP_FREE_POST:
332 X509_NAME_free(dpn->dpname);
333 break;
334 }
335 return 1;
336 }
337
338
339 ASN1_CHOICE_cb(DIST_POINT_NAME, dpn_cb) = {
340 ASN1_IMP_SEQUENCE_OF(DIST_POINT_NAME, name.fullname, GENERAL_NAME, 0),
341 ASN1_IMP_SET_OF(DIST_POINT_NAME, name.relativename, X509_NAME_ENTRY, 1),
342 } ASN1_CHOICE_END_cb(DIST_POINT_NAME, DIST_POINT_NAME, type)
343
344 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(DIST_POINT_NAME)
345
346 ASN1_SEQUENCE(DIST_POINT) = {
347 ASN1_EXP_OPT(DIST_POINT, distpoint, DIST_POINT_NAME, 0),
348 ASN1_IMP_OPT(DIST_POINT, reasons, ASN1_BIT_STRING, 1),
349 ASN1_IMP_SEQUENCE_OF_OPT(DIST_POINT, CRLissuer, GENERAL_NAME, 2),
350 } ASN1_SEQUENCE_END(DIST_POINT)
351
352 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(DIST_POINT)
353
354 ASN1_ITEM_TEMPLATE(CRL_DIST_POINTS) = ASN1_EX_TEMPLATE_TYPE(
355 ASN1_TFLG_SEQUENCE_OF, 0, CRLDistributionPoints, DIST_POINT)
356 ASN1_ITEM_TEMPLATE_END(CRL_DIST_POINTS)
357
358 IMPLEMENT_ASN1_FUNCTIONS(CRL_DIST_POINTS)
359
360 ASN1_SEQUENCE(ISSUING_DIST_POINT) = {
361 ASN1_EXP_OPT(ISSUING_DIST_POINT, distpoint, DIST_POINT_NAME, 0),
362 ASN1_IMP_OPT(ISSUING_DIST_POINT, onlyuser, ASN1_FBOOLEAN, 1),
363 ASN1_IMP_OPT(ISSUING_DIST_POINT, onlyCA, ASN1_FBOOLEAN, 2),
364 ASN1_IMP_OPT(ISSUING_DIST_POINT, onlysomereasons, ASN1_BIT_STRING, 3),
365 ASN1_IMP_OPT(ISSUING_DIST_POINT, indirectCRL, ASN1_FBOOLEAN, 4),
366 ASN1_IMP_OPT(ISSUING_DIST_POINT, onlyattr, ASN1_FBOOLEAN, 5),
367 } ASN1_SEQUENCE_END(ISSUING_DIST_POINT)
368
369 IMPLEMENT_ASN1_FUNCTIONS(ISSUING_DIST_POINT)
370
371 static int i2r_idp(const X509V3_EXT_METHOD *method, void *pidp, BIO *out,
372 int indent);
373 static void *v2i_idp(const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx,
374 const STACK_OF(CONF_VALUE) *nval);
375
376 const X509V3_EXT_METHOD v3_idp = {
377 NID_issuing_distribution_point,
378 X509V3_EXT_MULTILINE,
379 ASN1_ITEM_ref(ISSUING_DIST_POINT),
380 0,
381 0,
382 0,
383 0,
384 0,
385 0,
386 0,
387 v2i_idp,
388 i2r_idp,
389 0,
390 NULL,
391 };
392
393 static void *v2i_idp(const X509V3_EXT_METHOD *method, const X509V3_CTX *ctx,
394 const STACK_OF(CONF_VALUE) *nval) {
395 ISSUING_DIST_POINT *idp = ISSUING_DIST_POINT_new();
396 if (!idp) {
397 goto err;
398 }
399 for (size_t i = 0; i < sk_CONF_VALUE_num(nval); i++) {
400 const CONF_VALUE *cnf = sk_CONF_VALUE_value(nval, i);
401 const char *name = cnf->name;
402 const char *val = cnf->value;
403 int ret = set_dist_point_name(&idp->distpoint, ctx, cnf);
404 if (ret > 0) {
405 continue;
406 }
407 if (ret < 0) {
408 goto err;
409 }
410 if (!strcmp(name, "onlyuser")) {
411 if (!X509V3_get_value_bool(cnf, &idp->onlyuser)) {
412 goto err;
413 }
414 } else if (!strcmp(name, "onlyCA")) {
415 if (!X509V3_get_value_bool(cnf, &idp->onlyCA)) {
416 goto err;
417 }
418 } else if (!strcmp(name, "onlyAA")) {
419 if (!X509V3_get_value_bool(cnf, &idp->onlyattr)) {
420 goto err;
421 }
422 } else if (!strcmp(name, "indirectCRL")) {
423 if (!X509V3_get_value_bool(cnf, &idp->indirectCRL)) {
424 goto err;
425 }
426 } else if (!strcmp(name, "onlysomereasons")) {
427 if (!set_reasons(&idp->onlysomereasons, val)) {
428 goto err;
429 }
430 } else {
431 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NAME);
432 X509V3_conf_err(cnf);
433 goto err;
434 }
435 }
436 return idp;
437
438 err:
439 ISSUING_DIST_POINT_free(idp);
440 return NULL;
441 }
442
443 static int print_gens(BIO *out, STACK_OF(GENERAL_NAME) *gens, int indent) {
444 size_t i;
445 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
446 BIO_printf(out, "%*s", indent + 2, "");
447 GENERAL_NAME_print(out, sk_GENERAL_NAME_value(gens, i));
448 BIO_puts(out, "\n");
449 }
450 return 1;
451 }
452
453 static int print_distpoint(BIO *out, DIST_POINT_NAME *dpn, int indent) {
454 if (dpn->type == 0) {
455 BIO_printf(out, "%*sFull Name:\n", indent, "");
456 print_gens(out, dpn->name.fullname, indent);
457 } else {
458 X509_NAME ntmp;
459 ntmp.entries = dpn->name.relativename;
460 BIO_printf(out, "%*sRelative Name:\n%*s", indent, "", indent + 2, "");
461 X509_NAME_print_ex(out, &ntmp, 0, XN_FLAG_ONELINE);
462 BIO_puts(out, "\n");
463 }
464 return 1;
465 }
466
467 static int i2r_idp(const X509V3_EXT_METHOD *method, void *pidp, BIO *out,
468 int indent) {
469 ISSUING_DIST_POINT *idp = pidp;
470 if (idp->distpoint) {
471 print_distpoint(out, idp->distpoint, indent);
472 }
473 if (idp->onlyuser > 0) {
474 BIO_printf(out, "%*sOnly User Certificates\n", indent, "");
475 }
476 if (idp->onlyCA > 0) {
477 BIO_printf(out, "%*sOnly CA Certificates\n", indent, "");
478 }
479 if (idp->indirectCRL > 0) {
480 BIO_printf(out, "%*sIndirect CRL\n", indent, "");
481 }
482 if (idp->onlysomereasons) {
483 print_reasons(out, "Only Some Reasons", idp->onlysomereasons, indent);
484 }
485 if (idp->onlyattr > 0) {
486 BIO_printf(out, "%*sOnly Attribute Certificates\n", indent, "");
487 }
488 if (!idp->distpoint && (idp->onlyuser <= 0) && (idp->onlyCA <= 0) &&
489 (idp->indirectCRL <= 0) && !idp->onlysomereasons &&
490 (idp->onlyattr <= 0)) {
491 BIO_printf(out, "%*s<EMPTY>\n", indent, "");
492 }
493
494 return 1;
495 }
496
497 static int i2r_crldp(const X509V3_EXT_METHOD *method, void *pcrldp, BIO *out,
498 int indent) {
499 STACK_OF(DIST_POINT) *crld = pcrldp;
500 DIST_POINT *point;
501 size_t i;
502 for (i = 0; i < sk_DIST_POINT_num(crld); i++) {
503 BIO_puts(out, "\n");
504 point = sk_DIST_POINT_value(crld, i);
505 if (point->distpoint) {
506 print_distpoint(out, point->distpoint, indent);
507 }
508 if (point->reasons) {
509 print_reasons(out, "Reasons", point->reasons, indent);
510 }
511 if (point->CRLissuer) {
512 BIO_printf(out, "%*sCRL Issuer:\n", indent, "");
513 print_gens(out, point->CRLissuer, indent);
514 }
515 }
516 return 1;
517 }
518
519 int DIST_POINT_set_dpname(DIST_POINT_NAME *dpn, X509_NAME *iname) {
520 size_t i;
521 STACK_OF(X509_NAME_ENTRY) *frag;
522 X509_NAME_ENTRY *ne;
523 if (!dpn || (dpn->type != 1)) {
524 return 1;
525 }
526 frag = dpn->name.relativename;
527 dpn->dpname = X509_NAME_dup(iname);
528 if (!dpn->dpname) {
529 return 0;
530 }
531 for (i = 0; i < sk_X509_NAME_ENTRY_num(frag); i++) {
532 ne = sk_X509_NAME_ENTRY_value(frag, i);
533 if (!X509_NAME_add_entry(dpn->dpname, ne, -1, i ? 0 : 1)) {
534 X509_NAME_free(dpn->dpname);
535 dpn->dpname = NULL;
536 return 0;
537 }
538 }
539 // generate cached encoding of name
540 if (i2d_X509_NAME(dpn->dpname, NULL) < 0) {
541 X509_NAME_free(dpn->dpname);
542 dpn->dpname = NULL;
543 return 0;
544 }
545 return 1;
546 }