]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/x509/v3_alt.c
crypto/x509/v3_alt.c: make 'othername' a bit bigger
[thirdparty/openssl.git] / crypto / x509 / v3_alt.c
CommitLineData
0f113f3e 1/*
a2371fa9 2 * Copyright 1999-2017 The OpenSSL Project Authors. All Rights Reserved.
142fcca8 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
142fcca8
DSH
8 */
9
10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
ec577822
BM
12#include <openssl/conf.h>
13#include <openssl/x509v3.h>
df2ee0e2 14#include "ext_dat.h"
142fcca8 15
852c2ed2
RS
16DEFINE_STACK_OF(CONF_VALUE)
17DEFINE_STACK_OF(GENERAL_NAME)
18
0f113f3e
MC
19static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method,
20 X509V3_CTX *ctx,
21 STACK_OF(CONF_VALUE) *nval);
22static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method,
23 X509V3_CTX *ctx,
24 STACK_OF(CONF_VALUE) *nval);
c9fd77e9 25static int copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p);
9d6b1ce6 26static int copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens);
c8f717fe
F
27static int do_othername(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx);
28static int do_dirname(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx);
9ea1b878 29
4b68cb41 30const X509V3_EXT_METHOD v3_alt[3] = {
0f113f3e
MC
31 {NID_subject_alt_name, 0, ASN1_ITEM_ref(GENERAL_NAMES),
32 0, 0, 0, 0,
33 0, 0,
34 (X509V3_EXT_I2V) i2v_GENERAL_NAMES,
35 (X509V3_EXT_V2I)v2i_subject_alt,
36 NULL, NULL, NULL},
37
38 {NID_issuer_alt_name, 0, ASN1_ITEM_ref(GENERAL_NAMES),
39 0, 0, 0, 0,
40 0, 0,
41 (X509V3_EXT_I2V) i2v_GENERAL_NAMES,
42 (X509V3_EXT_V2I)v2i_issuer_alt,
43 NULL, NULL, NULL},
44
45 {NID_certificate_issuer, 0, ASN1_ITEM_ref(GENERAL_NAMES),
46 0, 0, 0, 0,
47 0, 0,
48 (X509V3_EXT_I2V) i2v_GENERAL_NAMES,
49 NULL, NULL, NULL, NULL},
142fcca8
DSH
50};
51
ba404b5e 52STACK_OF(CONF_VALUE) *i2v_GENERAL_NAMES(X509V3_EXT_METHOD *method,
0f113f3e
MC
53 GENERAL_NAMES *gens,
54 STACK_OF(CONF_VALUE) *ret)
142fcca8 55{
0f113f3e
MC
56 int i;
57 GENERAL_NAME *gen;
45b24462
MC
58 STACK_OF(CONF_VALUE) *tmpret = NULL, *origret = ret;
59
0f113f3e
MC
60 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
61 gen = sk_GENERAL_NAME_value(gens, i);
45b24462
MC
62 /*
63 * i2v_GENERAL_NAME allocates ret if it is NULL. If something goes
64 * wrong we need to free the stack - but only if it was empty when we
65 * originally entered this function.
66 */
67 tmpret = i2v_GENERAL_NAME(method, gen, ret);
68 if (tmpret == NULL) {
69 if (origret == NULL)
70 sk_CONF_VALUE_pop_free(ret, X509V3_conf_free);
71 return NULL;
72 }
73 ret = tmpret;
0f113f3e 74 }
45b24462 75 if (ret == NULL)
0f113f3e
MC
76 return sk_CONF_VALUE_new_null();
77 return ret;
d08d8da4
DSH
78}
79
ba404b5e 80STACK_OF(CONF_VALUE) *i2v_GENERAL_NAME(X509V3_EXT_METHOD *method,
0f113f3e
MC
81 GENERAL_NAME *gen,
82 STACK_OF(CONF_VALUE) *ret)
d08d8da4 83{
0f113f3e 84 unsigned char *p;
d0bcad73 85 char othername[300];
0f113f3e
MC
86 char oline[256], htmp[5];
87 int i;
1a78a33a 88
0f113f3e
MC
89 switch (gen->type) {
90 case GEN_OTHERNAME:
4baee2d7
DB
91 switch (OBJ_obj2nid(gen->d.otherName->type_id)) {
92 case NID_id_on_SmtpUTF8Mailbox:
aec9667b
MC
93 if (gen->d.otherName->value->type != V_ASN1_UTF8STRING
94 || !X509V3_add_value_uchar("othername: SmtpUTF8Mailbox:",
95 gen->d.otherName->value->value.utf8string->data,
96 &ret))
4baee2d7
DB
97 return NULL;
98 break;
99 case NID_XmppAddr:
aec9667b
MC
100 if (gen->d.otherName->value->type != V_ASN1_UTF8STRING
101 || !X509V3_add_value_uchar("othername: XmppAddr:",
102 gen->d.otherName->value->value.utf8string->data,
103 &ret))
4baee2d7
DB
104 return NULL;
105 break;
106 case NID_SRVName:
aec9667b
MC
107 if (gen->d.otherName->value->type != V_ASN1_IA5STRING
108 || !X509V3_add_value_uchar("othername: SRVName:",
109 gen->d.otherName->value->value.ia5string->data,
110 &ret))
4baee2d7
DB
111 return NULL;
112 break;
113 case NID_ms_upn:
aec9667b
MC
114 if (gen->d.otherName->value->type != V_ASN1_UTF8STRING
115 || !X509V3_add_value_uchar("othername: UPN:",
116 gen->d.otherName->value->value.utf8string->data,
117 &ret))
4baee2d7
DB
118 return NULL;
119 break;
f5e77bb0
JFR
120 case NID_NAIRealm:
121 if (gen->d.otherName->value->type != V_ASN1_UTF8STRING
122 || !X509V3_add_value_uchar("othername: NAIRealm:",
123 gen->d.otherName->value->value.utf8string->data,
124 &ret))
125 return NULL;
126 break;
4baee2d7 127 default:
47f387e9
DWG
128 if (OBJ_obj2txt(oline, sizeof(oline), gen->d.otherName->type_id, 0) > 0)
129 snprintf(othername, sizeof(othername), "othername: %s:", oline);
130 else
131 strncpy(othername, "othername:", sizeof(othername));
132
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,
137 &ret))
138 return ret;
139 }
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,
143 &ret))
144 return ret;
145 }
146 if (!X509V3_add_value(othername, "<unsupported>", &ret))
4baee2d7
DB
147 return NULL;
148 break;
149 }
0f113f3e
MC
150 break;
151
152 case GEN_X400:
75a3e392
MC
153 if (!X509V3_add_value("X400Name", "<unsupported>", &ret))
154 return NULL;
0f113f3e
MC
155 break;
156
157 case GEN_EDIPARTY:
75a3e392
MC
158 if (!X509V3_add_value("EdiPartyName", "<unsupported>", &ret))
159 return NULL;
0f113f3e
MC
160 break;
161
162 case GEN_EMAIL:
75a3e392
MC
163 if (!X509V3_add_value_uchar("email", gen->d.ia5->data, &ret))
164 return NULL;
0f113f3e
MC
165 break;
166
167 case GEN_DNS:
75a3e392
MC
168 if (!X509V3_add_value_uchar("DNS", gen->d.ia5->data, &ret))
169 return NULL;
0f113f3e
MC
170 break;
171
172 case GEN_URI:
75a3e392
MC
173 if (!X509V3_add_value_uchar("URI", gen->d.ia5->data, &ret))
174 return NULL;
0f113f3e
MC
175 break;
176
177 case GEN_DIRNAME:
1a78a33a 178 if (X509_NAME_oneline(gen->d.dirn, oline, sizeof(oline)) == NULL
75a3e392
MC
179 || !X509V3_add_value("DirName", oline, &ret))
180 return NULL;
0f113f3e
MC
181 break;
182
183 case GEN_IPADD:
184 p = gen->d.ip->data;
185 if (gen->d.ip->length == 4)
a2371fa9
P
186 BIO_snprintf(oline, sizeof(oline), "%d.%d.%d.%d",
187 p[0], p[1], p[2], p[3]);
0f113f3e
MC
188 else if (gen->d.ip->length == 16) {
189 oline[0] = 0;
190 for (i = 0; i < 8; i++) {
a2371fa9 191 BIO_snprintf(htmp, sizeof(htmp), "%X", p[0] << 8 | p[1]);
0f113f3e
MC
192 p += 2;
193 strcat(oline, htmp);
194 if (i != 7)
195 strcat(oline, ":");
196 }
197 } else {
75a3e392
MC
198 if (!X509V3_add_value("IP Address", "<invalid>", &ret))
199 return NULL;
0f113f3e
MC
200 break;
201 }
75a3e392
MC
202 if (!X509V3_add_value("IP Address", oline, &ret))
203 return NULL;
0f113f3e
MC
204 break;
205
206 case GEN_RID:
207 i2t_ASN1_OBJECT(oline, 256, gen->d.rid);
75a3e392
MC
208 if (!X509V3_add_value("Registered ID", oline, &ret))
209 return NULL;
0f113f3e
MC
210 break;
211 }
212 return ret;
2c15d426
DSH
213}
214
215int GENERAL_NAME_print(BIO *out, GENERAL_NAME *gen)
216{
0f113f3e 217 unsigned char *p;
aec9667b
MC
218 int i, nid;
219
0f113f3e
MC
220 switch (gen->type) {
221 case GEN_OTHERNAME:
aec9667b
MC
222 nid = OBJ_obj2nid(gen->d.otherName->type_id);
223 /* Validate the types are as we expect before we use them */
224 if ((nid == NID_SRVName
225 && gen->d.otherName->value->type != V_ASN1_IA5STRING)
226 || (nid != NID_SRVName
227 && gen->d.otherName->value->type != V_ASN1_UTF8STRING)) {
228 BIO_printf(out, "othername:<unsupported>");
229 break;
230 }
231
232 switch (nid) {
4baee2d7 233 case NID_id_on_SmtpUTF8Mailbox:
aec9667b
MC
234 BIO_printf(out, "othername:SmtpUTF8Mailbox:%s",
235 gen->d.otherName->value->value.utf8string->data);
4baee2d7
DB
236 break;
237 case NID_XmppAddr:
aec9667b
MC
238 BIO_printf(out, "othername:XmppAddr:%s",
239 gen->d.otherName->value->value.utf8string->data);
4baee2d7
DB
240 break;
241 case NID_SRVName:
aec9667b
MC
242 BIO_printf(out, "othername:SRVName:%s",
243 gen->d.otherName->value->value.ia5string->data);
4baee2d7
DB
244 break;
245 case NID_ms_upn:
aec9667b
MC
246 BIO_printf(out, "othername:UPN:%s",
247 gen->d.otherName->value->value.utf8string->data);
4baee2d7 248 break;
f5e77bb0
JFR
249 case NID_NAIRealm:
250 BIO_printf(out, "othername:NAIRealm:%s",
251 gen->d.otherName->value->value.utf8string->data);
252 break;
4baee2d7
DB
253 default:
254 BIO_printf(out, "othername:<unsupported>");
255 break;
256 }
0f113f3e
MC
257 break;
258
259 case GEN_X400:
260 BIO_printf(out, "X400Name:<unsupported>");
261 break;
262
263 case GEN_EDIPARTY:
264 /* Maybe fix this: it is supported now */
265 BIO_printf(out, "EdiPartyName:<unsupported>");
266 break;
267
268 case GEN_EMAIL:
bab60461
AB
269 BIO_printf(out, "email:");
270 ASN1_STRING_print(out, gen->d.ia5);
0f113f3e
MC
271 break;
272
273 case GEN_DNS:
bab60461
AB
274 BIO_printf(out, "DNS:");
275 ASN1_STRING_print(out, gen->d.ia5);
0f113f3e
MC
276 break;
277
278 case GEN_URI:
bab60461
AB
279 BIO_printf(out, "URI:");
280 ASN1_STRING_print(out, gen->d.ia5);
0f113f3e
MC
281 break;
282
283 case GEN_DIRNAME:
ca1cb0d4 284 BIO_printf(out, "DirName:");
0f113f3e
MC
285 X509_NAME_print_ex(out, gen->d.dirn, 0, XN_FLAG_ONELINE);
286 break;
287
288 case GEN_IPADD:
289 p = gen->d.ip->data;
290 if (gen->d.ip->length == 4)
291 BIO_printf(out, "IP Address:%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
292 else if (gen->d.ip->length == 16) {
293 BIO_printf(out, "IP Address");
294 for (i = 0; i < 8; i++) {
295 BIO_printf(out, ":%X", p[0] << 8 | p[1]);
296 p += 2;
297 }
0f113f3e
MC
298 } else {
299 BIO_printf(out, "IP Address:<invalid>");
300 break;
301 }
302 break;
303
304 case GEN_RID:
ca1cb0d4 305 BIO_printf(out, "Registered ID:");
0f113f3e
MC
306 i2a_ASN1_OBJECT(out, gen->d.rid);
307 break;
308 }
309 return 1;
142fcca8
DSH
310}
311
9d6b1ce6 312static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method,
0f113f3e
MC
313 X509V3_CTX *ctx,
314 STACK_OF(CONF_VALUE) *nval)
aa066b9e 315{
270a4bba 316 const int num = sk_CONF_VALUE_num(nval);
7a908204 317 GENERAL_NAMES *gens = sk_GENERAL_NAME_new_reserve(NULL, num);
0f113f3e 318 int i;
75ebbd9a 319
7a908204 320 if (gens == NULL) {
0f113f3e 321 X509V3err(X509V3_F_V2I_ISSUER_ALT, ERR_R_MALLOC_FAILURE);
270a4bba 322 sk_GENERAL_NAME_free(gens);
0f113f3e
MC
323 return NULL;
324 }
270a4bba
F
325 for (i = 0; i < num; i++) {
326 CONF_VALUE *cnf = sk_CONF_VALUE_value(nval, i);
327
5f5edd7d 328 if (!v3_name_cmp(cnf->name, "issuer")
86885c28 329 && cnf->value && strcmp(cnf->value, "copy") == 0) {
0f113f3e
MC
330 if (!copy_issuer(ctx, gens))
331 goto err;
332 } else {
270a4bba
F
333 GENERAL_NAME *gen = v2i_GENERAL_NAME(method, ctx, cnf);
334
335 if (gen == NULL)
0f113f3e 336 goto err;
270a4bba 337 sk_GENERAL_NAME_push(gens, gen); /* no failure as it was reserved */
0f113f3e
MC
338 }
339 }
340 return gens;
341 err:
342 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
343 return NULL;
aa066b9e
DSH
344}
345
346/* Append subject altname of issuer to issuer alt name of subject */
347
9d6b1ce6 348static int copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens)
aa066b9e 349{
0f113f3e
MC
350 GENERAL_NAMES *ialt;
351 GENERAL_NAME *gen;
352 X509_EXTENSION *ext;
270a4bba 353 int i, num;
75ebbd9a 354
0f113f3e
MC
355 if (ctx && (ctx->flags == CTX_TEST))
356 return 1;
357 if (!ctx || !ctx->issuer_cert) {
358 X509V3err(X509V3_F_COPY_ISSUER, X509V3_R_NO_ISSUER_DETAILS);
359 goto err;
360 }
361 i = X509_get_ext_by_NID(ctx->issuer_cert, NID_subject_alt_name, -1);
362 if (i < 0)
363 return 1;
75ebbd9a
RS
364 if ((ext = X509_get_ext(ctx->issuer_cert, i)) == NULL
365 || (ialt = X509V3_EXT_d2i(ext)) == NULL) {
0f113f3e
MC
366 X509V3err(X509V3_F_COPY_ISSUER, X509V3_R_ISSUER_DECODE_ERROR);
367 goto err;
368 }
369
270a4bba
F
370 num = sk_GENERAL_NAME_num(ialt);
371 if (!sk_GENERAL_NAME_reserve(gens, num)) {
372 X509V3err(X509V3_F_COPY_ISSUER, ERR_R_MALLOC_FAILURE);
373 goto err;
374 }
375
376 for (i = 0; i < num; i++) {
0f113f3e 377 gen = sk_GENERAL_NAME_value(ialt, i);
270a4bba 378 sk_GENERAL_NAME_push(gens, gen); /* no failure as it was reserved */
0f113f3e
MC
379 }
380 sk_GENERAL_NAME_free(ialt);
381
382 return 1;
383
384 err:
385 return 0;
386
aa066b9e
DSH
387}
388
9d6b1ce6 389static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method,
0f113f3e
MC
390 X509V3_CTX *ctx,
391 STACK_OF(CONF_VALUE) *nval)
aa066b9e 392{
270a4bba 393 GENERAL_NAMES *gens;
0f113f3e 394 CONF_VALUE *cnf;
270a4bba 395 const int num = sk_CONF_VALUE_num(nval);
0f113f3e 396 int i;
75ebbd9a 397
7a908204
PY
398 gens = sk_GENERAL_NAME_new_reserve(NULL, num);
399 if (gens == NULL) {
0f113f3e 400 X509V3err(X509V3_F_V2I_SUBJECT_ALT, ERR_R_MALLOC_FAILURE);
270a4bba 401 sk_GENERAL_NAME_free(gens);
0f113f3e
MC
402 return NULL;
403 }
270a4bba
F
404
405 for (i = 0; i < num; i++) {
0f113f3e 406 cnf = sk_CONF_VALUE_value(nval, i);
5f5edd7d 407 if (!v3_name_cmp(cnf->name, "email")
86885c28 408 && cnf->value && strcmp(cnf->value, "copy") == 0) {
0f113f3e
MC
409 if (!copy_email(ctx, gens, 0))
410 goto err;
5f5edd7d 411 } else if (!v3_name_cmp(cnf->name, "email")
86885c28 412 && cnf->value && strcmp(cnf->value, "move") == 0) {
0f113f3e
MC
413 if (!copy_email(ctx, gens, 1))
414 goto err;
415 } else {
416 GENERAL_NAME *gen;
75ebbd9a 417 if ((gen = v2i_GENERAL_NAME(method, ctx, cnf)) == NULL)
0f113f3e 418 goto err;
270a4bba 419 sk_GENERAL_NAME_push(gens, gen); /* no failure as it was reserved */
0f113f3e
MC
420 }
421 }
422 return gens;
423 err:
424 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
425 return NULL;
aa066b9e
DSH
426}
427
0f113f3e
MC
428/*
429 * Copy any email addresses in a certificate or request to GENERAL_NAMES
aa066b9e
DSH
430 */
431
c9fd77e9 432static int copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p)
aa066b9e 433{
0f113f3e
MC
434 X509_NAME *nm;
435 ASN1_IA5STRING *email = NULL;
436 X509_NAME_ENTRY *ne;
437 GENERAL_NAME *gen = NULL;
d2a56999
F
438 int i = -1;
439
0f113f3e
MC
440 if (ctx != NULL && ctx->flags == CTX_TEST)
441 return 1;
bd91e3c8 442 if (ctx == NULL
d2a56999 443 || (ctx->subject_cert == NULL && ctx->subject_req == NULL)) {
0f113f3e
MC
444 X509V3err(X509V3_F_COPY_EMAIL, X509V3_R_NO_SUBJECT_DETAILS);
445 goto err;
446 }
447 /* Find the subject name */
448 if (ctx->subject_cert)
449 nm = X509_get_subject_name(ctx->subject_cert);
450 else
451 nm = X509_REQ_get_subject_name(ctx->subject_req);
452
453 /* Now add any email address(es) to STACK */
0f113f3e
MC
454 while ((i = X509_NAME_get_index_by_NID(nm,
455 NID_pkcs9_emailAddress, i)) >= 0) {
456 ne = X509_NAME_get_entry(nm, i);
f422a514 457 email = ASN1_STRING_dup(X509_NAME_ENTRY_get_data(ne));
0f113f3e
MC
458 if (move_p) {
459 X509_NAME_delete_entry(nm, i);
460 X509_NAME_ENTRY_free(ne);
461 i--;
462 }
75ebbd9a 463 if (email == NULL || (gen = GENERAL_NAME_new()) == NULL) {
0f113f3e
MC
464 X509V3err(X509V3_F_COPY_EMAIL, ERR_R_MALLOC_FAILURE);
465 goto err;
466 }
467 gen->d.ia5 = email;
468 email = NULL;
469 gen->type = GEN_EMAIL;
470 if (!sk_GENERAL_NAME_push(gens, gen)) {
471 X509V3err(X509V3_F_COPY_EMAIL, ERR_R_MALLOC_FAILURE);
472 goto err;
473 }
474 gen = NULL;
475 }
476
477 return 1;
478
479 err:
480 GENERAL_NAME_free(gen);
f422a514 481 ASN1_IA5STRING_free(email);
0f113f3e
MC
482 return 0;
483
aa066b9e
DSH
484}
485
babb3798 486GENERAL_NAMES *v2i_GENERAL_NAMES(const X509V3_EXT_METHOD *method,
0f113f3e 487 X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
d08d8da4 488{
0f113f3e 489 GENERAL_NAME *gen;
270a4bba 490 GENERAL_NAMES *gens;
0f113f3e 491 CONF_VALUE *cnf;
270a4bba 492 const int num = sk_CONF_VALUE_num(nval);
0f113f3e 493 int i;
75ebbd9a 494
7a908204
PY
495 gens = sk_GENERAL_NAME_new_reserve(NULL, num);
496 if (gens == NULL) {
0f113f3e 497 X509V3err(X509V3_F_V2I_GENERAL_NAMES, ERR_R_MALLOC_FAILURE);
270a4bba 498 sk_GENERAL_NAME_free(gens);
0f113f3e
MC
499 return NULL;
500 }
270a4bba
F
501
502 for (i = 0; i < num; i++) {
0f113f3e 503 cnf = sk_CONF_VALUE_value(nval, i);
75ebbd9a 504 if ((gen = v2i_GENERAL_NAME(method, ctx, cnf)) == NULL)
0f113f3e 505 goto err;
270a4bba 506 sk_GENERAL_NAME_push(gens, gen); /* no failure as it was reserved */
0f113f3e
MC
507 }
508 return gens;
509 err:
510 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
511 return NULL;
d08d8da4
DSH
512}
513
0f113f3e
MC
514GENERAL_NAME *v2i_GENERAL_NAME(const X509V3_EXT_METHOD *method,
515 X509V3_CTX *ctx, CONF_VALUE *cnf)
516{
517 return v2i_GENERAL_NAME_ex(NULL, method, ctx, cnf, 0);
518}
d08d8da4 519
be86c7fc 520GENERAL_NAME *a2i_GENERAL_NAME(GENERAL_NAME *out,
0f113f3e 521 const X509V3_EXT_METHOD *method,
c8f717fe 522 X509V3_CTX *ctx, int gen_type, const char *value,
0f113f3e
MC
523 int is_nc)
524{
525 char is_string = 0;
526 GENERAL_NAME *gen = NULL;
527
528 if (!value) {
529 X509V3err(X509V3_F_A2I_GENERAL_NAME, X509V3_R_MISSING_VALUE);
530 return NULL;
531 }
532
533 if (out)
534 gen = out;
535 else {
536 gen = GENERAL_NAME_new();
537 if (gen == NULL) {
538 X509V3err(X509V3_F_A2I_GENERAL_NAME, ERR_R_MALLOC_FAILURE);
539 return NULL;
540 }
541 }
542
543 switch (gen_type) {
544 case GEN_URI:
545 case GEN_EMAIL:
546 case GEN_DNS:
547 is_string = 1;
548 break;
549
550 case GEN_RID:
551 {
552 ASN1_OBJECT *obj;
75ebbd9a 553 if ((obj = OBJ_txt2obj(value, 0)) == NULL) {
0f113f3e
MC
554 X509V3err(X509V3_F_A2I_GENERAL_NAME, X509V3_R_BAD_OBJECT);
555 ERR_add_error_data(2, "value=", value);
556 goto err;
557 }
558 gen->d.rid = obj;
559 }
560 break;
561
562 case GEN_IPADD:
563 if (is_nc)
564 gen->d.ip = a2i_IPADDRESS_NC(value);
565 else
566 gen->d.ip = a2i_IPADDRESS(value);
567 if (gen->d.ip == NULL) {
568 X509V3err(X509V3_F_A2I_GENERAL_NAME, X509V3_R_BAD_IP_ADDRESS);
569 ERR_add_error_data(2, "value=", value);
570 goto err;
571 }
572 break;
573
574 case GEN_DIRNAME:
575 if (!do_dirname(gen, value, ctx)) {
576 X509V3err(X509V3_F_A2I_GENERAL_NAME, X509V3_R_DIRNAME_ERROR);
577 goto err;
578 }
579 break;
580
581 case GEN_OTHERNAME:
582 if (!do_othername(gen, value, ctx)) {
583 X509V3err(X509V3_F_A2I_GENERAL_NAME, X509V3_R_OTHERNAME_ERROR);
584 goto err;
585 }
586 break;
587 default:
588 X509V3err(X509V3_F_A2I_GENERAL_NAME, X509V3_R_UNSUPPORTED_TYPE);
589 goto err;
590 }
591
592 if (is_string) {
75ebbd9a 593 if ((gen->d.ia5 = ASN1_IA5STRING_new()) == NULL ||
0f113f3e
MC
594 !ASN1_STRING_set(gen->d.ia5, (unsigned char *)value,
595 strlen(value))) {
596 X509V3err(X509V3_F_A2I_GENERAL_NAME, ERR_R_MALLOC_FAILURE);
597 goto err;
598 }
599 }
600
601 gen->type = gen_type;
602
603 return gen;
604
605 err:
606 if (!out)
607 GENERAL_NAME_free(gen);
608 return NULL;
609}
9ea1b878 610
be86c7fc 611GENERAL_NAME *v2i_GENERAL_NAME_ex(GENERAL_NAME *out,
0f113f3e
MC
612 const X509V3_EXT_METHOD *method,
613 X509V3_CTX *ctx, CONF_VALUE *cnf, int is_nc)
614{
615 int type;
616
617 char *name, *value;
618
619 name = cnf->name;
620 value = cnf->value;
621
622 if (!value) {
623 X509V3err(X509V3_F_V2I_GENERAL_NAME_EX, X509V3_R_MISSING_VALUE);
624 return NULL;
625 }
626
5f5edd7d 627 if (!v3_name_cmp(name, "email"))
0f113f3e 628 type = GEN_EMAIL;
5f5edd7d 629 else if (!v3_name_cmp(name, "URI"))
0f113f3e 630 type = GEN_URI;
5f5edd7d 631 else if (!v3_name_cmp(name, "DNS"))
0f113f3e 632 type = GEN_DNS;
5f5edd7d 633 else if (!v3_name_cmp(name, "RID"))
0f113f3e 634 type = GEN_RID;
5f5edd7d 635 else if (!v3_name_cmp(name, "IP"))
0f113f3e 636 type = GEN_IPADD;
5f5edd7d 637 else if (!v3_name_cmp(name, "dirName"))
0f113f3e 638 type = GEN_DIRNAME;
5f5edd7d 639 else if (!v3_name_cmp(name, "otherName"))
0f113f3e
MC
640 type = GEN_OTHERNAME;
641 else {
642 X509V3err(X509V3_F_V2I_GENERAL_NAME_EX, X509V3_R_UNSUPPORTED_OPTION);
643 ERR_add_error_data(2, "name=", name);
644 return NULL;
645 }
646
647 return a2i_GENERAL_NAME(out, method, ctx, type, value, is_nc);
648
649}
be86c7fc 650
c8f717fe 651static int do_othername(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx)
0f113f3e
MC
652{
653 char *objtmp = NULL, *p;
654 int objlen;
75ebbd9a
RS
655
656 if ((p = strchr(value, ';')) == NULL)
0f113f3e 657 return 0;
75ebbd9a 658 if ((gen->d.otherName = OTHERNAME_new()) == NULL)
0f113f3e
MC
659 return 0;
660 /*
661 * Free this up because we will overwrite it. no need to free type_id
662 * because it is static
663 */
664 ASN1_TYPE_free(gen->d.otherName->value);
75ebbd9a 665 if ((gen->d.otherName->value = ASN1_generate_v3(p + 1, ctx)) == NULL)
0f113f3e
MC
666 return 0;
667 objlen = p - value;
5e04cfde 668 objtmp = OPENSSL_strndup(value, objlen);
0f113f3e
MC
669 if (objtmp == NULL)
670 return 0;
0f113f3e
MC
671 gen->d.otherName->type_id = OBJ_txt2obj(objtmp, 0);
672 OPENSSL_free(objtmp);
673 if (!gen->d.otherName->type_id)
674 return 0;
675 return 1;
676}
f0dc08e6 677
c8f717fe 678static int do_dirname(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx)
0f113f3e 679{
8ec5c5dd
KR
680 int ret = 0;
681 STACK_OF(CONF_VALUE) *sk = NULL;
75ebbd9a
RS
682 X509_NAME *nm;
683
684 if ((nm = X509_NAME_new()) == NULL)
8ec5c5dd 685 goto err;
0f113f3e
MC
686 sk = X509V3_get_section(ctx, value);
687 if (!sk) {
688 X509V3err(X509V3_F_DO_DIRNAME, X509V3_R_SECTION_NOT_FOUND);
689 ERR_add_error_data(2, "section=", value);
8ec5c5dd 690 goto err;
0f113f3e
MC
691 }
692 /* FIXME: should allow other character types... */
693 ret = X509V3_NAME_from_section(nm, sk, MBSTRING_ASC);
694 if (!ret)
8ec5c5dd 695 goto err;
0f113f3e 696 gen->d.dirn = nm;
0f113f3e 697
8ec5c5dd
KR
698err:
699 if (ret == 0)
700 X509_NAME_free(nm);
701 X509V3_section_free(ctx, sk);
0f113f3e
MC
702 return ret;
703}