2 * Copyright 1999-2016 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 "internal/x509_int.h"
20 static void *v2i_crld(const X509V3_EXT_METHOD
*method
,
21 X509V3_CTX
*ctx
, STACK_OF(CONF_VALUE
) *nval
);
22 static int i2r_crldp(const X509V3_EXT_METHOD
*method
, void *pcrldp
, BIO
*out
,
25 const X509V3_EXT_METHOD v3_crld
= {
26 NID_crl_distribution_points
, 0, ASN1_ITEM_ref(CRL_DIST_POINTS
),
35 const X509V3_EXT_METHOD v3_freshest_crl
= {
36 NID_freshest_crl
, 0, ASN1_ITEM_ref(CRL_DIST_POINTS
),
45 static STACK_OF(GENERAL_NAME
) *gnames_from_sectname(X509V3_CTX
*ctx
,
48 STACK_OF(CONF_VALUE
) *gnsect
;
49 STACK_OF(GENERAL_NAME
) *gens
;
51 gnsect
= X509V3_get_section(ctx
, sect
+ 1);
53 gnsect
= X509V3_parse_list(sect
);
55 X509V3err(X509V3_F_GNAMES_FROM_SECTNAME
, X509V3_R_SECTION_NOT_FOUND
);
58 gens
= v2i_GENERAL_NAMES(NULL
, ctx
, gnsect
);
60 X509V3_section_free(ctx
, gnsect
);
62 sk_CONF_VALUE_pop_free(gnsect
, X509V3_conf_free
);
66 static int set_dist_point_name(DIST_POINT_NAME
**pdp
, X509V3_CTX
*ctx
,
69 STACK_OF(GENERAL_NAME
) *fnm
= NULL
;
70 STACK_OF(X509_NAME_ENTRY
) *rnm
= NULL
;
72 if (strncmp(cnf
->name
, "fullname", 9) == 0) {
73 fnm
= gnames_from_sectname(ctx
, cnf
->value
);
76 } else if (strcmp(cnf
->name
, "relativename") == 0) {
78 STACK_OF(CONF_VALUE
) *dnsect
;
83 dnsect
= X509V3_get_section(ctx
, cnf
->value
);
85 X509V3err(X509V3_F_SET_DIST_POINT_NAME
,
86 X509V3_R_SECTION_NOT_FOUND
);
89 ret
= X509V3_NAME_from_section(nm
, dnsect
, MBSTRING_ASC
);
90 X509V3_section_free(ctx
, dnsect
);
94 if (!ret
|| sk_X509_NAME_ENTRY_num(rnm
) <= 0)
97 * Since its a name fragment can't have more than one RDNSequence
99 if (sk_X509_NAME_ENTRY_value(rnm
,
100 sk_X509_NAME_ENTRY_num(rnm
) - 1)->set
) {
101 X509V3err(X509V3_F_SET_DIST_POINT_NAME
,
102 X509V3_R_INVALID_MULTIPLE_RDNS
);
109 X509V3err(X509V3_F_SET_DIST_POINT_NAME
,
110 X509V3_R_DISTPOINT_ALREADY_SET
);
114 *pdp
= DIST_POINT_NAME_new();
119 (*pdp
)->name
.fullname
= fnm
;
122 (*pdp
)->name
.relativename
= rnm
;
128 sk_GENERAL_NAME_pop_free(fnm
, GENERAL_NAME_free
);
129 sk_X509_NAME_ENTRY_pop_free(rnm
, X509_NAME_ENTRY_free
);
133 static const BIT_STRING_BITNAME reason_flags
[] = {
134 {0, "Unused", "unused"},
135 {1, "Key Compromise", "keyCompromise"},
136 {2, "CA Compromise", "CACompromise"},
137 {3, "Affiliation Changed", "affiliationChanged"},
138 {4, "Superseded", "superseded"},
139 {5, "Cessation Of Operation", "cessationOfOperation"},
140 {6, "Certificate Hold", "certificateHold"},
141 {7, "Privilege Withdrawn", "privilegeWithdrawn"},
142 {8, "AA Compromise", "AACompromise"},
146 static int set_reasons(ASN1_BIT_STRING
**preas
, char *value
)
148 STACK_OF(CONF_VALUE
) *rsk
= NULL
;
149 const BIT_STRING_BITNAME
*pbn
;
152 rsk
= X509V3_parse_list(value
);
157 for (i
= 0; i
< sk_CONF_VALUE_num(rsk
); i
++) {
158 bnam
= sk_CONF_VALUE_value(rsk
, i
)->name
;
159 if (*preas
== NULL
) {
160 *preas
= ASN1_BIT_STRING_new();
164 for (pbn
= reason_flags
; pbn
->lname
; pbn
++) {
165 if (strcmp(pbn
->sname
, bnam
) == 0) {
166 if (!ASN1_BIT_STRING_set_bit(*preas
, pbn
->bitnum
, 1))
177 sk_CONF_VALUE_pop_free(rsk
, X509V3_conf_free
);
181 static int print_reasons(BIO
*out
, const char *rname
,
182 ASN1_BIT_STRING
*rflags
, int indent
)
185 const BIT_STRING_BITNAME
*pbn
;
186 BIO_printf(out
, "%*s%s:\n%*s", indent
, "", rname
, indent
+ 2, "");
187 for (pbn
= reason_flags
; pbn
->lname
; pbn
++) {
188 if (ASN1_BIT_STRING_get_bit(rflags
, pbn
->bitnum
)) {
193 BIO_puts(out
, pbn
->lname
);
197 BIO_puts(out
, "<EMPTY>\n");
203 static DIST_POINT
*crldp_from_section(X509V3_CTX
*ctx
,
204 STACK_OF(CONF_VALUE
) *nval
)
208 DIST_POINT
*point
= DIST_POINT_new();
212 for (i
= 0; i
< sk_CONF_VALUE_num(nval
); i
++) {
214 cnf
= sk_CONF_VALUE_value(nval
, i
);
215 ret
= set_dist_point_name(&point
->distpoint
, ctx
, cnf
);
220 if (strcmp(cnf
->name
, "reasons") == 0) {
221 if (!set_reasons(&point
->reasons
, cnf
->value
))
223 } else if (strcmp(cnf
->name
, "CRLissuer") == 0) {
224 point
->CRLissuer
= gnames_from_sectname(ctx
, cnf
->value
);
225 if (!point
->CRLissuer
)
233 DIST_POINT_free(point
);
237 static void *v2i_crld(const X509V3_EXT_METHOD
*method
,
238 X509V3_CTX
*ctx
, STACK_OF(CONF_VALUE
) *nval
)
240 STACK_OF(DIST_POINT
) *crld
;
241 GENERAL_NAMES
*gens
= NULL
;
242 GENERAL_NAME
*gen
= NULL
;
244 const int num
= sk_CONF_VALUE_num(nval
);
247 crld
= sk_DIST_POINT_new_reserve(NULL
, num
);
250 for (i
= 0; i
< num
; i
++) {
253 cnf
= sk_CONF_VALUE_value(nval
, i
);
255 STACK_OF(CONF_VALUE
) *dpsect
;
256 dpsect
= X509V3_get_section(ctx
, cnf
->name
);
259 point
= crldp_from_section(ctx
, dpsect
);
260 X509V3_section_free(ctx
, dpsect
);
263 sk_DIST_POINT_push(crld
, point
); /* no failure as it was reserved */
265 if ((gen
= v2i_GENERAL_NAME(method
, ctx
, cnf
)) == NULL
)
267 if ((gens
= GENERAL_NAMES_new()) == NULL
)
269 if (!sk_GENERAL_NAME_push(gens
, gen
))
272 if ((point
= DIST_POINT_new()) == NULL
)
274 sk_DIST_POINT_push(crld
, point
); /* no failure as it was reserved */
275 if ((point
->distpoint
= DIST_POINT_NAME_new()) == NULL
)
277 point
->distpoint
->name
.fullname
= gens
;
278 point
->distpoint
->type
= 0;
285 X509V3err(X509V3_F_V2I_CRLD
, ERR_R_MALLOC_FAILURE
);
287 GENERAL_NAME_free(gen
);
288 GENERAL_NAMES_free(gens
);
289 sk_DIST_POINT_pop_free(crld
, DIST_POINT_free
);
293 static int dpn_cb(int operation
, ASN1_VALUE
**pval
, const ASN1_ITEM
*it
,
296 DIST_POINT_NAME
*dpn
= (DIST_POINT_NAME
*)*pval
;
299 case ASN1_OP_NEW_POST
:
303 case ASN1_OP_FREE_POST
:
304 X509_NAME_free(dpn
->dpname
);
311 ASN1_CHOICE_cb(DIST_POINT_NAME
, dpn_cb
) = {
312 ASN1_IMP_SEQUENCE_OF(DIST_POINT_NAME
, name
.fullname
, GENERAL_NAME
, 0),
313 ASN1_IMP_SET_OF(DIST_POINT_NAME
, name
.relativename
, X509_NAME_ENTRY
, 1)
314 } ASN1_CHOICE_END_cb(DIST_POINT_NAME
, DIST_POINT_NAME
, type
)
317 IMPLEMENT_ASN1_FUNCTIONS(DIST_POINT_NAME
)
319 ASN1_SEQUENCE(DIST_POINT
) = {
320 ASN1_EXP_OPT(DIST_POINT
, distpoint
, DIST_POINT_NAME
, 0),
321 ASN1_IMP_OPT(DIST_POINT
, reasons
, ASN1_BIT_STRING
, 1),
322 ASN1_IMP_SEQUENCE_OF_OPT(DIST_POINT
, CRLissuer
, GENERAL_NAME
, 2)
323 } ASN1_SEQUENCE_END(DIST_POINT
)
325 IMPLEMENT_ASN1_FUNCTIONS(DIST_POINT
)
327 ASN1_ITEM_TEMPLATE(CRL_DIST_POINTS
) =
328 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF
, 0, CRLDistributionPoints
, DIST_POINT
)
329 ASN1_ITEM_TEMPLATE_END(CRL_DIST_POINTS
)
331 IMPLEMENT_ASN1_FUNCTIONS(CRL_DIST_POINTS
)
333 ASN1_SEQUENCE(ISSUING_DIST_POINT
) = {
334 ASN1_EXP_OPT(ISSUING_DIST_POINT
, distpoint
, DIST_POINT_NAME
, 0),
335 ASN1_IMP_OPT(ISSUING_DIST_POINT
, onlyuser
, ASN1_FBOOLEAN
, 1),
336 ASN1_IMP_OPT(ISSUING_DIST_POINT
, onlyCA
, ASN1_FBOOLEAN
, 2),
337 ASN1_IMP_OPT(ISSUING_DIST_POINT
, onlysomereasons
, ASN1_BIT_STRING
, 3),
338 ASN1_IMP_OPT(ISSUING_DIST_POINT
, indirectCRL
, ASN1_FBOOLEAN
, 4),
339 ASN1_IMP_OPT(ISSUING_DIST_POINT
, onlyattr
, ASN1_FBOOLEAN
, 5)
340 } ASN1_SEQUENCE_END(ISSUING_DIST_POINT
)
342 IMPLEMENT_ASN1_FUNCTIONS(ISSUING_DIST_POINT
)
344 static int i2r_idp(const X509V3_EXT_METHOD
*method
, void *pidp
, BIO
*out
,
346 static void *v2i_idp(const X509V3_EXT_METHOD
*method
, X509V3_CTX
*ctx
,
347 STACK_OF(CONF_VALUE
) *nval
);
349 const X509V3_EXT_METHOD v3_idp
= {
350 NID_issuing_distribution_point
, X509V3_EXT_MULTILINE
,
351 ASN1_ITEM_ref(ISSUING_DIST_POINT
),
360 static void *v2i_idp(const X509V3_EXT_METHOD
*method
, X509V3_CTX
*ctx
,
361 STACK_OF(CONF_VALUE
) *nval
)
363 ISSUING_DIST_POINT
*idp
= NULL
;
367 idp
= ISSUING_DIST_POINT_new();
370 for (i
= 0; i
< sk_CONF_VALUE_num(nval
); i
++) {
371 cnf
= sk_CONF_VALUE_value(nval
, i
);
374 ret
= set_dist_point_name(&idp
->distpoint
, ctx
, cnf
);
379 if (strcmp(name
, "onlyuser") == 0) {
380 if (!X509V3_get_value_bool(cnf
, &idp
->onlyuser
))
382 } else if (strcmp(name
, "onlyCA") == 0) {
383 if (!X509V3_get_value_bool(cnf
, &idp
->onlyCA
))
385 } else if (strcmp(name
, "onlyAA") == 0) {
386 if (!X509V3_get_value_bool(cnf
, &idp
->onlyattr
))
388 } else if (strcmp(name
, "indirectCRL") == 0) {
389 if (!X509V3_get_value_bool(cnf
, &idp
->indirectCRL
))
391 } else if (strcmp(name
, "onlysomereasons") == 0) {
392 if (!set_reasons(&idp
->onlysomereasons
, val
))
395 X509V3err(X509V3_F_V2I_IDP
, X509V3_R_INVALID_NAME
);
396 X509V3_conf_err(cnf
);
403 X509V3err(X509V3_F_V2I_IDP
, ERR_R_MALLOC_FAILURE
);
405 ISSUING_DIST_POINT_free(idp
);
409 static int print_gens(BIO
*out
, STACK_OF(GENERAL_NAME
) *gens
, int indent
)
412 for (i
= 0; i
< sk_GENERAL_NAME_num(gens
); i
++) {
415 BIO_printf(out
, "%*s", indent
+ 2, "");
416 GENERAL_NAME_print(out
, sk_GENERAL_NAME_value(gens
, i
));
421 static int print_distpoint(BIO
*out
, DIST_POINT_NAME
*dpn
, int indent
)
423 if (dpn
->type
== 0) {
424 BIO_printf(out
, "%*sFull Name:\n", indent
, "");
425 print_gens(out
, dpn
->name
.fullname
, indent
);
428 ntmp
.entries
= dpn
->name
.relativename
;
429 BIO_printf(out
, "%*sRelative Name:\n%*s", indent
, "", indent
+ 2, "");
430 X509_NAME_print_ex(out
, &ntmp
, 0, XN_FLAG_ONELINE
);
436 static int i2r_idp(const X509V3_EXT_METHOD
*method
, void *pidp
, BIO
*out
,
439 ISSUING_DIST_POINT
*idp
= pidp
;
441 print_distpoint(out
, idp
->distpoint
, indent
);
442 if (idp
->onlyuser
> 0)
443 BIO_printf(out
, "%*sOnly User Certificates\n", indent
, "");
445 BIO_printf(out
, "%*sOnly CA Certificates\n", indent
, "");
446 if (idp
->indirectCRL
> 0)
447 BIO_printf(out
, "%*sIndirect CRL\n", indent
, "");
448 if (idp
->onlysomereasons
)
449 print_reasons(out
, "Only Some Reasons", idp
->onlysomereasons
, indent
);
450 if (idp
->onlyattr
> 0)
451 BIO_printf(out
, "%*sOnly Attribute Certificates\n", indent
, "");
452 if (!idp
->distpoint
&& (idp
->onlyuser
<= 0) && (idp
->onlyCA
<= 0)
453 && (idp
->indirectCRL
<= 0) && !idp
->onlysomereasons
454 && (idp
->onlyattr
<= 0))
455 BIO_printf(out
, "%*s<EMPTY>\n", indent
, "");
460 static int i2r_crldp(const X509V3_EXT_METHOD
*method
, void *pcrldp
, BIO
*out
,
463 STACK_OF(DIST_POINT
) *crld
= pcrldp
;
466 for (i
= 0; i
< sk_DIST_POINT_num(crld
); i
++) {
469 point
= sk_DIST_POINT_value(crld
, i
);
470 if (point
->distpoint
)
471 print_distpoint(out
, point
->distpoint
, indent
);
473 print_reasons(out
, "Reasons", point
->reasons
, indent
);
474 if (point
->CRLissuer
) {
475 BIO_printf(out
, "%*sCRL Issuer:\n", indent
, "");
476 print_gens(out
, point
->CRLissuer
, indent
);
482 int DIST_POINT_set_dpname(DIST_POINT_NAME
*dpn
, X509_NAME
*iname
)
485 STACK_OF(X509_NAME_ENTRY
) *frag
;
487 if (!dpn
|| (dpn
->type
!= 1))
489 frag
= dpn
->name
.relativename
;
490 dpn
->dpname
= X509_NAME_dup(iname
);
493 for (i
= 0; i
< sk_X509_NAME_ENTRY_num(frag
); i
++) {
494 ne
= sk_X509_NAME_ENTRY_value(frag
, i
);
495 if (!X509_NAME_add_entry(dpn
->dpname
, ne
, -1, i
? 0 : 1)) {
496 X509_NAME_free(dpn
->dpname
);
501 /* generate cached encoding of name */
502 if (i2d_X509_NAME(dpn
->dpname
, NULL
) < 0) {
503 X509_NAME_free(dpn
->dpname
);