1 /* $OpenBSD: x509_alt.c,v 1.1 2020/06/04 15:19:31 jsing Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1999-2003 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
62 #include <openssl/conf.h>
63 #include <openssl/err.h>
64 #include <openssl/x509v3.h>
66 static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method,
67 X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval);
68 static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method,
69 X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval);
70 static int copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p);
71 static int copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens);
72 static int do_othername(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx);
73 static int do_dirname(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx);
75 const X509V3_EXT_METHOD v3_alt[] = {
77 .ext_nid = NID_subject_alt_name,
79 .it = &GENERAL_NAMES_it,
86 .i2v = (X509V3_EXT_I2V)i2v_GENERAL_NAMES,
87 .v2i = (X509V3_EXT_V2I)v2i_subject_alt,
93 .ext_nid = NID_issuer_alt_name,
95 .it = &GENERAL_NAMES_it,
102 .i2v = (X509V3_EXT_I2V)i2v_GENERAL_NAMES,
103 .v2i = (X509V3_EXT_V2I)v2i_issuer_alt,
109 .ext_nid = NID_certificate_issuer,
111 .it = &GENERAL_NAMES_it,
118 .i2v = (X509V3_EXT_I2V)i2v_GENERAL_NAMES,
126 STACK_OF(CONF_VALUE) *
127 i2v_GENERAL_NAMES(X509V3_EXT_METHOD *method, GENERAL_NAMES *gens,
128 STACK_OF(CONF_VALUE) *ret)
130 STACK_OF(CONF_VALUE) *free_ret = NULL;
135 if ((free_ret = ret = sk_CONF_VALUE_new_null()) == NULL)
139 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
140 if ((gen = sk_GENERAL_NAME_value(gens, i)) == NULL)
142 if ((ret = i2v_GENERAL_NAME(method, gen, ret)) == NULL)
149 sk_CONF_VALUE_pop_free(free_ret, X509V3_conf_free);
154 STACK_OF(CONF_VALUE) *
155 i2v_GENERAL_NAME(X509V3_EXT_METHOD *method, GENERAL_NAME *gen,
156 STACK_OF(CONF_VALUE) *ret)
158 STACK_OF(CONF_VALUE) *free_ret = NULL;
160 char oline[256], htmp[5];
164 if ((free_ret = ret = sk_CONF_VALUE_new_null()) == NULL)
170 if (!X509V3_add_value("othername", "<unsupported>", &ret))
175 if (!X509V3_add_value("X400Name", "<unsupported>", &ret))
180 if (!X509V3_add_value("EdiPartyName", "<unsupported>", &ret))
185 if (!X509V3_add_value_uchar("email", gen->d.ia5->data, &ret))
190 if (!X509V3_add_value_uchar("DNS", gen->d.ia5->data, &ret))
195 if (!X509V3_add_value_uchar("URI", gen->d.ia5->data, &ret))
200 if (X509_NAME_oneline(gen->d.dirn, oline, 256) == NULL)
202 if (!X509V3_add_value("DirName", oline, &ret))
206 case GEN_IPADD: /* XXX */
208 if (gen->d.ip->length == 4)
209 (void) snprintf(oline, sizeof oline,
210 "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
211 else if (gen->d.ip->length == 16) {
213 for (i = 0; i < 8; i++) {
214 (void) snprintf(htmp, sizeof htmp,
215 "%X", p[0] << 8 | p[1]);
217 strlcat(oline, htmp, sizeof(oline));
219 strlcat(oline, ":", sizeof(oline));
222 if (!X509V3_add_value("IP Address", "<invalid>", &ret))
226 if (!X509V3_add_value("IP Address", oline, &ret))
231 if (!i2t_ASN1_OBJECT(oline, 256, gen->d.rid))
233 if (!X509V3_add_value("Registered ID", oline, &ret))
241 sk_CONF_VALUE_pop_free(free_ret, X509V3_conf_free);
247 GENERAL_NAME_print(BIO *out, GENERAL_NAME *gen)
254 BIO_printf(out, "othername:<unsupported>");
258 BIO_printf(out, "X400Name:<unsupported>");
262 /* Maybe fix this: it is supported now */
263 BIO_printf(out, "EdiPartyName:<unsupported>");
267 BIO_printf(out, "email:%s", gen->d.ia5->data);
271 BIO_printf(out, "DNS:%s", gen->d.ia5->data);
275 BIO_printf(out, "URI:%s", gen->d.ia5->data);
279 BIO_printf(out, "DirName: ");
280 X509_NAME_print_ex(out, gen->d.dirn, 0, XN_FLAG_ONELINE);
285 if (gen->d.ip->length == 4)
286 BIO_printf(out, "IP Address:%d.%d.%d.%d",
287 p[0], p[1], p[2], p[3]);
288 else if (gen->d.ip->length == 16) {
289 BIO_printf(out, "IP Address");
290 for (i = 0; i < 8; i++) {
291 BIO_printf(out, ":%X", p[0] << 8 | p[1]);
296 BIO_printf(out, "IP Address:<invalid>");
302 BIO_printf(out, "Registered ID");
303 i2a_ASN1_OBJECT(out, gen->d.rid);
309 static GENERAL_NAMES *
310 v2i_issuer_alt(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
311 STACK_OF(CONF_VALUE) *nval)
313 GENERAL_NAMES *gens = NULL;
317 if ((gens = sk_GENERAL_NAME_new_null()) == NULL) {
318 X509V3error(ERR_R_MALLOC_FAILURE);
321 for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
322 cnf = sk_CONF_VALUE_value(nval, i);
323 if (name_cmp(cnf->name, "issuer") == 0 && cnf->value != NULL &&
324 strcmp(cnf->value, "copy") == 0) {
325 if (!copy_issuer(ctx, gens))
329 if ((gen = v2i_GENERAL_NAME(method, ctx, cnf)) == NULL)
331 if (sk_GENERAL_NAME_push(gens, gen) == 0) {
332 GENERAL_NAME_free(gen);
340 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
344 /* Append subject altname of issuer to issuer alt name of subject */
347 copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens)
354 if (ctx && (ctx->flags == CTX_TEST))
356 if (!ctx || !ctx->issuer_cert) {
357 X509V3error(X509V3_R_NO_ISSUER_DETAILS);
360 i = X509_get_ext_by_NID(ctx->issuer_cert, NID_subject_alt_name, -1);
363 if (!(ext = X509_get_ext(ctx->issuer_cert, i)) ||
364 !(ialt = X509V3_EXT_d2i(ext))) {
365 X509V3error(X509V3_R_ISSUER_DECODE_ERROR);
369 for (i = 0; i < sk_GENERAL_NAME_num(ialt); i++) {
370 gen = sk_GENERAL_NAME_value(ialt, i);
371 if (!sk_GENERAL_NAME_push(gens, gen)) {
372 X509V3error(ERR_R_MALLOC_FAILURE);
376 sk_GENERAL_NAME_free(ialt);
385 static GENERAL_NAMES *
386 v2i_subject_alt(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
387 STACK_OF(CONF_VALUE) *nval)
389 GENERAL_NAMES *gens = NULL;
393 if (!(gens = sk_GENERAL_NAME_new_null())) {
394 X509V3error(ERR_R_MALLOC_FAILURE);
397 for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
398 cnf = sk_CONF_VALUE_value(nval, i);
399 if (!name_cmp(cnf->name, "email") && cnf->value &&
400 !strcmp(cnf->value, "copy")) {
401 if (!copy_email(ctx, gens, 0))
403 } else if (!name_cmp(cnf->name, "email") && cnf->value &&
404 !strcmp(cnf->value, "move")) {
405 if (!copy_email(ctx, gens, 1))
409 if (!(gen = v2i_GENERAL_NAME(method, ctx, cnf)))
411 if (sk_GENERAL_NAME_push(gens, gen) == 0) {
412 GENERAL_NAME_free(gen);
420 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
424 /* Copy any email addresses in a certificate or request to
429 copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p)
432 ASN1_IA5STRING *email = NULL;
434 GENERAL_NAME *gen = NULL;
437 if (ctx != NULL && ctx->flags == CTX_TEST)
439 if (!ctx || (!ctx->subject_cert && !ctx->subject_req)) {
440 X509V3error(X509V3_R_NO_SUBJECT_DETAILS);
443 /* Find the subject name */
444 if (ctx->subject_cert)
445 nm = X509_get_subject_name(ctx->subject_cert);
447 nm = X509_REQ_get_subject_name(ctx->subject_req);
449 /* Now add any email address(es) to STACK */
451 while ((i = X509_NAME_get_index_by_NID(nm,
452 NID_pkcs9_emailAddress, i)) >= 0) {
453 ne = X509_NAME_get_entry(nm, i);
454 email = ASN1_STRING_dup(X509_NAME_ENTRY_get_data(ne));
456 X509_NAME_delete_entry(nm, i);
457 X509_NAME_ENTRY_free(ne);
460 if (!email || !(gen = GENERAL_NAME_new())) {
461 X509V3error(ERR_R_MALLOC_FAILURE);
466 gen->type = GEN_EMAIL;
467 if (!sk_GENERAL_NAME_push(gens, gen)) {
468 X509V3error(ERR_R_MALLOC_FAILURE);
477 GENERAL_NAME_free(gen);
478 ASN1_IA5STRING_free(email);
483 v2i_GENERAL_NAMES(const X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
484 STACK_OF(CONF_VALUE) *nval)
487 GENERAL_NAMES *gens = NULL;
491 if (!(gens = sk_GENERAL_NAME_new_null())) {
492 X509V3error(ERR_R_MALLOC_FAILURE);
495 for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
496 cnf = sk_CONF_VALUE_value(nval, i);
497 if (!(gen = v2i_GENERAL_NAME(method, ctx, cnf)))
499 if (sk_GENERAL_NAME_push(gens, gen) == 0) {
500 GENERAL_NAME_free(gen);
507 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
512 v2i_GENERAL_NAME(const X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
515 return v2i_GENERAL_NAME_ex(NULL, method, ctx, cnf, 0);
519 a2i_GENERAL_NAME(GENERAL_NAME *out, const X509V3_EXT_METHOD *method,
520 X509V3_CTX *ctx, int gen_type, const char *value, int is_nc)
523 GENERAL_NAME *gen = NULL;
526 X509V3error(X509V3_R_MISSING_VALUE);
533 gen = GENERAL_NAME_new();
535 X509V3error(ERR_R_MALLOC_FAILURE);
550 if (!(obj = OBJ_txt2obj(value, 0))) {
551 X509V3error(X509V3_R_BAD_OBJECT);
552 ERR_asprintf_error_data("value=%s", value);
561 gen->d.ip = a2i_IPADDRESS_NC(value);
563 gen->d.ip = a2i_IPADDRESS(value);
564 if (gen->d.ip == NULL) {
565 X509V3error(X509V3_R_BAD_IP_ADDRESS);
566 ERR_asprintf_error_data("value=%s", value);
572 if (!do_dirname(gen, value, ctx)) {
573 X509V3error(X509V3_R_DIRNAME_ERROR);
579 if (!do_othername(gen, value, ctx)) {
580 X509V3error(X509V3_R_OTHERNAME_ERROR);
586 X509V3error(X509V3_R_UNSUPPORTED_TYPE);
591 if (!(gen->d.ia5 = ASN1_IA5STRING_new()) ||
592 !ASN1_STRING_set(gen->d.ia5, value, strlen(value))) {
593 X509V3error(ERR_R_MALLOC_FAILURE);
598 gen->type = gen_type;
604 GENERAL_NAME_free(gen);
609 v2i_GENERAL_NAME_ex(GENERAL_NAME *out, const X509V3_EXT_METHOD *method,
610 X509V3_CTX *ctx, CONF_VALUE *cnf, int is_nc)
619 X509V3error(X509V3_R_MISSING_VALUE);
623 if (!name_cmp(name, "email"))
625 else if (!name_cmp(name, "URI"))
627 else if (!name_cmp(name, "DNS"))
629 else if (!name_cmp(name, "RID"))
631 else if (!name_cmp(name, "IP"))
633 else if (!name_cmp(name, "dirName"))
635 else if (!name_cmp(name, "otherName"))
636 type = GEN_OTHERNAME;
638 X509V3error(X509V3_R_UNSUPPORTED_OPTION);
639 ERR_asprintf_error_data("name=%s", name);
643 return a2i_GENERAL_NAME(out, method, ctx, type, value, is_nc);
647 do_othername(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx)
649 char *objtmp = NULL, *p;
652 if (!(p = strchr(value, ';')))
654 if (!(gen->d.otherName = OTHERNAME_new()))
656 /* Free this up because we will overwrite it.
657 * no need to free type_id because it is static
659 ASN1_TYPE_free(gen->d.otherName->value);
660 if (!(gen->d.otherName->value = ASN1_generate_v3(p + 1, ctx)))
663 objtmp = malloc(objlen + 1);
665 strlcpy(objtmp, value, objlen + 1);
666 gen->d.otherName->type_id = OBJ_txt2obj(objtmp, 0);
669 gen->d.otherName->type_id = NULL;
670 if (!gen->d.otherName->type_id)
676 do_dirname(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx)
679 STACK_OF(CONF_VALUE) *sk;
682 if (!(nm = X509_NAME_new()))
684 sk = X509V3_get_section(ctx, value);
686 X509V3error(X509V3_R_SECTION_NOT_FOUND);
687 ERR_asprintf_error_data("section=%s", value);
691 /* FIXME: should allow other character types... */
692 ret = X509V3_NAME_from_section(nm, sk, MBSTRING_ASC);
696 X509V3_section_free(ctx, sk);