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/x509v3.h>
14 #include <openssl/bio.h>
17 DEFINE_STACK_OF(CONF_VALUE
)
18 DEFINE_STACK_OF(GENERAL_NAME
)
20 static GENERAL_NAMES
*v2i_subject_alt(X509V3_EXT_METHOD
*method
,
22 STACK_OF(CONF_VALUE
) *nval
);
23 static GENERAL_NAMES
*v2i_issuer_alt(X509V3_EXT_METHOD
*method
,
25 STACK_OF(CONF_VALUE
) *nval
);
26 static int copy_email(X509V3_CTX
*ctx
, GENERAL_NAMES
*gens
, int move_p
);
27 static int copy_issuer(X509V3_CTX
*ctx
, GENERAL_NAMES
*gens
);
28 static int do_othername(GENERAL_NAME
*gen
, const char *value
, X509V3_CTX
*ctx
);
29 static int do_dirname(GENERAL_NAME
*gen
, const char *value
, X509V3_CTX
*ctx
);
31 const X509V3_EXT_METHOD v3_alt
[3] = {
32 {NID_subject_alt_name
, 0, ASN1_ITEM_ref(GENERAL_NAMES
),
35 (X509V3_EXT_I2V
) i2v_GENERAL_NAMES
,
36 (X509V3_EXT_V2I
)v2i_subject_alt
,
39 {NID_issuer_alt_name
, 0, ASN1_ITEM_ref(GENERAL_NAMES
),
42 (X509V3_EXT_I2V
) i2v_GENERAL_NAMES
,
43 (X509V3_EXT_V2I
)v2i_issuer_alt
,
46 {NID_certificate_issuer
, 0, ASN1_ITEM_ref(GENERAL_NAMES
),
49 (X509V3_EXT_I2V
) i2v_GENERAL_NAMES
,
50 NULL
, NULL
, NULL
, NULL
},
53 STACK_OF(CONF_VALUE
) *i2v_GENERAL_NAMES(X509V3_EXT_METHOD
*method
,
55 STACK_OF(CONF_VALUE
) *ret
)
59 STACK_OF(CONF_VALUE
) *tmpret
= NULL
, *origret
= ret
;
61 for (i
= 0; i
< sk_GENERAL_NAME_num(gens
); i
++) {
62 gen
= sk_GENERAL_NAME_value(gens
, i
);
64 * i2v_GENERAL_NAME allocates ret if it is NULL. If something goes
65 * wrong we need to free the stack - but only if it was empty when we
66 * originally entered this function.
68 tmpret
= i2v_GENERAL_NAME(method
, gen
, ret
);
71 sk_CONF_VALUE_pop_free(ret
, X509V3_conf_free
);
77 return sk_CONF_VALUE_new_null();
81 STACK_OF(CONF_VALUE
) *i2v_GENERAL_NAME(X509V3_EXT_METHOD
*method
,
83 STACK_OF(CONF_VALUE
) *ret
)
86 char oline
[256], *tmp
;
90 switch (OBJ_obj2nid(gen
->d
.otherName
->type_id
)) {
91 case NID_id_on_SmtpUTF8Mailbox
:
92 if (gen
->d
.otherName
->value
->type
!= V_ASN1_UTF8STRING
93 || !X509V3_add_value_uchar("othername: SmtpUTF8Mailbox:",
94 gen
->d
.otherName
->value
->value
.utf8string
->data
,
99 if (gen
->d
.otherName
->value
->type
!= V_ASN1_UTF8STRING
100 || !X509V3_add_value_uchar("othername: XmppAddr:",
101 gen
->d
.otherName
->value
->value
.utf8string
->data
,
106 if (gen
->d
.otherName
->value
->type
!= V_ASN1_IA5STRING
107 || !X509V3_add_value_uchar("othername: SRVName:",
108 gen
->d
.otherName
->value
->value
.ia5string
->data
,
113 if (gen
->d
.otherName
->value
->type
!= V_ASN1_UTF8STRING
114 || !X509V3_add_value_uchar("othername: UPN:",
115 gen
->d
.otherName
->value
->value
.utf8string
->data
,
120 if (gen
->d
.otherName
->value
->type
!= V_ASN1_UTF8STRING
121 || !X509V3_add_value_uchar("othername: NAIRealm:",
122 gen
->d
.otherName
->value
->value
.utf8string
->data
,
127 if (OBJ_obj2txt(oline
, sizeof(oline
), gen
->d
.otherName
->type_id
, 0) > 0)
128 BIO_snprintf(othername
, sizeof(othername
), "othername: %s:",
131 strncpy(othername
, "othername:", sizeof(othername
));
133 /* check if the value is something printable */
134 if (gen
->d
.otherName
->value
->type
== V_ASN1_IA5STRING
) {
135 if (X509V3_add_value_uchar(othername
,
136 gen
->d
.otherName
->value
->value
.ia5string
->data
,
140 if (gen
->d
.otherName
->value
->type
== V_ASN1_UTF8STRING
) {
141 if (X509V3_add_value_uchar(othername
,
142 gen
->d
.otherName
->value
->value
.utf8string
->data
,
146 if (!X509V3_add_value(othername
, "<unsupported>", &ret
))
153 if (!X509V3_add_value("X400Name", "<unsupported>", &ret
))
158 if (!X509V3_add_value("EdiPartyName", "<unsupported>", &ret
))
163 if (!X509V3_add_value_uchar("email", gen
->d
.ia5
->data
, &ret
))
168 if (!X509V3_add_value_uchar("DNS", gen
->d
.ia5
->data
, &ret
))
173 if (!X509V3_add_value_uchar("URI", gen
->d
.ia5
->data
, &ret
))
178 if (X509_NAME_oneline(gen
->d
.dirn
, oline
, sizeof(oline
)) == NULL
179 || !X509V3_add_value("DirName", oline
, &ret
))
184 tmp
= ipaddr_to_asc(gen
->d
.ip
->data
, gen
->d
.ip
->length
);
185 if (tmp
== NULL
|| !X509V3_add_value("IP Address", tmp
, &ret
))
191 i2t_ASN1_OBJECT(oline
, 256, gen
->d
.rid
);
192 if (!X509V3_add_value("Registered ID", oline
, &ret
))
199 int GENERAL_NAME_print(BIO
*out
, GENERAL_NAME
*gen
)
206 nid
= OBJ_obj2nid(gen
->d
.otherName
->type_id
);
207 /* Validate the types are as we expect before we use them */
208 if ((nid
== NID_SRVName
209 && gen
->d
.otherName
->value
->type
!= V_ASN1_IA5STRING
)
210 || (nid
!= NID_SRVName
211 && gen
->d
.otherName
->value
->type
!= V_ASN1_UTF8STRING
)) {
212 BIO_printf(out
, "othername:<unsupported>");
217 case NID_id_on_SmtpUTF8Mailbox
:
218 BIO_printf(out
, "othername:SmtpUTF8Mailbox:%s",
219 gen
->d
.otherName
->value
->value
.utf8string
->data
);
222 BIO_printf(out
, "othername:XmppAddr:%s",
223 gen
->d
.otherName
->value
->value
.utf8string
->data
);
226 BIO_printf(out
, "othername:SRVName:%s",
227 gen
->d
.otherName
->value
->value
.ia5string
->data
);
230 BIO_printf(out
, "othername:UPN:%s",
231 gen
->d
.otherName
->value
->value
.utf8string
->data
);
234 BIO_printf(out
, "othername:NAIRealm:%s",
235 gen
->d
.otherName
->value
->value
.utf8string
->data
);
238 BIO_printf(out
, "othername:<unsupported>");
244 BIO_printf(out
, "X400Name:<unsupported>");
248 /* Maybe fix this: it is supported now */
249 BIO_printf(out
, "EdiPartyName:<unsupported>");
253 BIO_printf(out
, "email:");
254 ASN1_STRING_print(out
, gen
->d
.ia5
);
258 BIO_printf(out
, "DNS:");
259 ASN1_STRING_print(out
, gen
->d
.ia5
);
263 BIO_printf(out
, "URI:");
264 ASN1_STRING_print(out
, gen
->d
.ia5
);
268 BIO_printf(out
, "DirName:");
269 X509_NAME_print_ex(out
, gen
->d
.dirn
, 0, XN_FLAG_ONELINE
);
273 tmp
= ipaddr_to_asc(gen
->d
.ip
->data
, gen
->d
.ip
->length
);
276 BIO_printf(out
, "IP Address:%s", tmp
);
281 BIO_printf(out
, "Registered ID:");
282 i2a_ASN1_OBJECT(out
, gen
->d
.rid
);
288 static GENERAL_NAMES
*v2i_issuer_alt(X509V3_EXT_METHOD
*method
,
290 STACK_OF(CONF_VALUE
) *nval
)
292 const int num
= sk_CONF_VALUE_num(nval
);
293 GENERAL_NAMES
*gens
= sk_GENERAL_NAME_new_reserve(NULL
, num
);
297 X509V3err(X509V3_F_V2I_ISSUER_ALT
, ERR_R_MALLOC_FAILURE
);
298 sk_GENERAL_NAME_free(gens
);
301 for (i
= 0; i
< num
; i
++) {
302 CONF_VALUE
*cnf
= sk_CONF_VALUE_value(nval
, i
);
304 if (!v3_name_cmp(cnf
->name
, "issuer")
305 && cnf
->value
&& strcmp(cnf
->value
, "copy") == 0) {
306 if (!copy_issuer(ctx
, gens
))
309 GENERAL_NAME
*gen
= v2i_GENERAL_NAME(method
, ctx
, cnf
);
313 sk_GENERAL_NAME_push(gens
, gen
); /* no failure as it was reserved */
318 sk_GENERAL_NAME_pop_free(gens
, GENERAL_NAME_free
);
322 /* Append subject altname of issuer to issuer alt name of subject */
324 static int copy_issuer(X509V3_CTX
*ctx
, GENERAL_NAMES
*gens
)
331 if (ctx
&& (ctx
->flags
== CTX_TEST
))
333 if (!ctx
|| !ctx
->issuer_cert
) {
334 X509V3err(X509V3_F_COPY_ISSUER
, X509V3_R_NO_ISSUER_DETAILS
);
337 i
= X509_get_ext_by_NID(ctx
->issuer_cert
, NID_subject_alt_name
, -1);
340 if ((ext
= X509_get_ext(ctx
->issuer_cert
, i
)) == NULL
341 || (ialt
= X509V3_EXT_d2i(ext
)) == NULL
) {
342 X509V3err(X509V3_F_COPY_ISSUER
, X509V3_R_ISSUER_DECODE_ERROR
);
346 num
= sk_GENERAL_NAME_num(ialt
);
347 if (!sk_GENERAL_NAME_reserve(gens
, num
)) {
348 X509V3err(X509V3_F_COPY_ISSUER
, ERR_R_MALLOC_FAILURE
);
352 for (i
= 0; i
< num
; i
++) {
353 gen
= sk_GENERAL_NAME_value(ialt
, i
);
354 sk_GENERAL_NAME_push(gens
, gen
); /* no failure as it was reserved */
356 sk_GENERAL_NAME_free(ialt
);
365 static GENERAL_NAMES
*v2i_subject_alt(X509V3_EXT_METHOD
*method
,
367 STACK_OF(CONF_VALUE
) *nval
)
371 const int num
= sk_CONF_VALUE_num(nval
);
374 gens
= sk_GENERAL_NAME_new_reserve(NULL
, num
);
376 X509V3err(X509V3_F_V2I_SUBJECT_ALT
, ERR_R_MALLOC_FAILURE
);
377 sk_GENERAL_NAME_free(gens
);
381 for (i
= 0; i
< num
; i
++) {
382 cnf
= sk_CONF_VALUE_value(nval
, i
);
383 if (!v3_name_cmp(cnf
->name
, "email")
384 && cnf
->value
&& strcmp(cnf
->value
, "copy") == 0) {
385 if (!copy_email(ctx
, gens
, 0))
387 } else if (!v3_name_cmp(cnf
->name
, "email")
388 && cnf
->value
&& strcmp(cnf
->value
, "move") == 0) {
389 if (!copy_email(ctx
, gens
, 1))
393 if ((gen
= v2i_GENERAL_NAME(method
, ctx
, cnf
)) == NULL
)
395 sk_GENERAL_NAME_push(gens
, gen
); /* no failure as it was reserved */
400 sk_GENERAL_NAME_pop_free(gens
, GENERAL_NAME_free
);
405 * Copy any email addresses in a certificate or request to GENERAL_NAMES
408 static int copy_email(X509V3_CTX
*ctx
, GENERAL_NAMES
*gens
, int move_p
)
411 ASN1_IA5STRING
*email
= NULL
;
413 GENERAL_NAME
*gen
= NULL
;
416 if (ctx
!= NULL
&& ctx
->flags
== CTX_TEST
)
419 || (ctx
->subject_cert
== NULL
&& ctx
->subject_req
== NULL
)) {
420 X509V3err(X509V3_F_COPY_EMAIL
, X509V3_R_NO_SUBJECT_DETAILS
);
423 /* Find the subject name */
424 if (ctx
->subject_cert
)
425 nm
= X509_get_subject_name(ctx
->subject_cert
);
427 nm
= X509_REQ_get_subject_name(ctx
->subject_req
);
429 /* Now add any email address(es) to STACK */
430 while ((i
= X509_NAME_get_index_by_NID(nm
,
431 NID_pkcs9_emailAddress
, i
)) >= 0) {
432 ne
= X509_NAME_get_entry(nm
, i
);
433 email
= ASN1_STRING_dup(X509_NAME_ENTRY_get_data(ne
));
435 X509_NAME_delete_entry(nm
, i
);
436 X509_NAME_ENTRY_free(ne
);
439 if (email
== NULL
|| (gen
= GENERAL_NAME_new()) == NULL
) {
440 X509V3err(X509V3_F_COPY_EMAIL
, ERR_R_MALLOC_FAILURE
);
445 gen
->type
= GEN_EMAIL
;
446 if (!sk_GENERAL_NAME_push(gens
, gen
)) {
447 X509V3err(X509V3_F_COPY_EMAIL
, ERR_R_MALLOC_FAILURE
);
456 GENERAL_NAME_free(gen
);
457 ASN1_IA5STRING_free(email
);
462 GENERAL_NAMES
*v2i_GENERAL_NAMES(const X509V3_EXT_METHOD
*method
,
463 X509V3_CTX
*ctx
, STACK_OF(CONF_VALUE
) *nval
)
468 const int num
= sk_CONF_VALUE_num(nval
);
471 gens
= sk_GENERAL_NAME_new_reserve(NULL
, num
);
473 X509V3err(X509V3_F_V2I_GENERAL_NAMES
, ERR_R_MALLOC_FAILURE
);
474 sk_GENERAL_NAME_free(gens
);
478 for (i
= 0; i
< num
; i
++) {
479 cnf
= sk_CONF_VALUE_value(nval
, i
);
480 if ((gen
= v2i_GENERAL_NAME(method
, ctx
, cnf
)) == NULL
)
482 sk_GENERAL_NAME_push(gens
, gen
); /* no failure as it was reserved */
486 sk_GENERAL_NAME_pop_free(gens
, GENERAL_NAME_free
);
490 GENERAL_NAME
*v2i_GENERAL_NAME(const X509V3_EXT_METHOD
*method
,
491 X509V3_CTX
*ctx
, CONF_VALUE
*cnf
)
493 return v2i_GENERAL_NAME_ex(NULL
, method
, ctx
, cnf
, 0);
496 GENERAL_NAME
*a2i_GENERAL_NAME(GENERAL_NAME
*out
,
497 const X509V3_EXT_METHOD
*method
,
498 X509V3_CTX
*ctx
, int gen_type
, const char *value
,
502 GENERAL_NAME
*gen
= NULL
;
505 X509V3err(X509V3_F_A2I_GENERAL_NAME
, X509V3_R_MISSING_VALUE
);
512 gen
= GENERAL_NAME_new();
514 X509V3err(X509V3_F_A2I_GENERAL_NAME
, ERR_R_MALLOC_FAILURE
);
529 if ((obj
= OBJ_txt2obj(value
, 0)) == NULL
) {
530 X509V3err(X509V3_F_A2I_GENERAL_NAME
, X509V3_R_BAD_OBJECT
);
531 ERR_add_error_data(2, "value=", value
);
540 gen
->d
.ip
= a2i_IPADDRESS_NC(value
);
542 gen
->d
.ip
= a2i_IPADDRESS(value
);
543 if (gen
->d
.ip
== NULL
) {
544 X509V3err(X509V3_F_A2I_GENERAL_NAME
, X509V3_R_BAD_IP_ADDRESS
);
545 ERR_add_error_data(2, "value=", value
);
551 if (!do_dirname(gen
, value
, ctx
)) {
552 X509V3err(X509V3_F_A2I_GENERAL_NAME
, X509V3_R_DIRNAME_ERROR
);
558 if (!do_othername(gen
, value
, ctx
)) {
559 X509V3err(X509V3_F_A2I_GENERAL_NAME
, X509V3_R_OTHERNAME_ERROR
);
564 X509V3err(X509V3_F_A2I_GENERAL_NAME
, X509V3_R_UNSUPPORTED_TYPE
);
569 if ((gen
->d
.ia5
= ASN1_IA5STRING_new()) == NULL
||
570 !ASN1_STRING_set(gen
->d
.ia5
, (unsigned char *)value
,
572 X509V3err(X509V3_F_A2I_GENERAL_NAME
, ERR_R_MALLOC_FAILURE
);
577 gen
->type
= gen_type
;
583 GENERAL_NAME_free(gen
);
587 GENERAL_NAME
*v2i_GENERAL_NAME_ex(GENERAL_NAME
*out
,
588 const X509V3_EXT_METHOD
*method
,
589 X509V3_CTX
*ctx
, CONF_VALUE
*cnf
, int is_nc
)
599 X509V3err(X509V3_F_V2I_GENERAL_NAME_EX
, X509V3_R_MISSING_VALUE
);
603 if (!v3_name_cmp(name
, "email"))
605 else if (!v3_name_cmp(name
, "URI"))
607 else if (!v3_name_cmp(name
, "DNS"))
609 else if (!v3_name_cmp(name
, "RID"))
611 else if (!v3_name_cmp(name
, "IP"))
613 else if (!v3_name_cmp(name
, "dirName"))
615 else if (!v3_name_cmp(name
, "otherName"))
616 type
= GEN_OTHERNAME
;
618 X509V3err(X509V3_F_V2I_GENERAL_NAME_EX
, X509V3_R_UNSUPPORTED_OPTION
);
619 ERR_add_error_data(2, "name=", name
);
623 return a2i_GENERAL_NAME(out
, method
, ctx
, type
, value
, is_nc
);
627 static int do_othername(GENERAL_NAME
*gen
, const char *value
, X509V3_CTX
*ctx
)
629 char *objtmp
= NULL
, *p
;
632 if ((p
= strchr(value
, ';')) == NULL
)
634 if ((gen
->d
.otherName
= OTHERNAME_new()) == NULL
)
637 * Free this up because we will overwrite it. no need to free type_id
638 * because it is static
640 ASN1_TYPE_free(gen
->d
.otherName
->value
);
641 if ((gen
->d
.otherName
->value
= ASN1_generate_v3(p
+ 1, ctx
)) == NULL
)
644 objtmp
= OPENSSL_strndup(value
, objlen
);
647 gen
->d
.otherName
->type_id
= OBJ_txt2obj(objtmp
, 0);
648 OPENSSL_free(objtmp
);
649 if (!gen
->d
.otherName
->type_id
)
654 static int do_dirname(GENERAL_NAME
*gen
, const char *value
, X509V3_CTX
*ctx
)
657 STACK_OF(CONF_VALUE
) *sk
= NULL
;
660 if ((nm
= X509_NAME_new()) == NULL
)
662 sk
= X509V3_get_section(ctx
, value
);
664 X509V3err(X509V3_F_DO_DIRNAME
, X509V3_R_SECTION_NOT_FOUND
);
665 ERR_add_error_data(2, "section=", value
);
668 /* FIXME: should allow other character types... */
669 ret
= X509V3_NAME_from_section(nm
, sk
, MBSTRING_ASC
);
677 X509V3_section_free(ctx
, sk
);