1 /* v3_alt.c */
2 /*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4 * project.
5 */
6 /* ====================================================================
7 * Copyright (c) 1999-2003 The OpenSSL Project. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 *
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
30 *
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
34 *
35 * 6. Redistributions of any form whatsoever must retain the following
36 * acknowledgment:
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
53 *
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
57 *
58 */
59
60 #include <stdio.h>
61 #include "cryptlib.h"
62 #include <openssl/conf.h>
63 #include <openssl/x509v3.h>
64
65 static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method,
66 X509V3_CTX *ctx,
67 STACK_OF(CONF_VALUE) *nval);
68 static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method,
69 X509V3_CTX *ctx,
70 STACK_OF(CONF_VALUE) *nval);
71 static int copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p);
72 static int copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens);
73 static int do_othername(GENERAL_NAME *gen, char *value, X509V3_CTX *ctx);
74 static int do_dirname(GENERAL_NAME *gen, char *value, X509V3_CTX *ctx);
75
76 const X509V3_EXT_METHOD v3_alt[] = {
77 {NID_subject_alt_name, 0, ASN1_ITEM_ref(GENERAL_NAMES),
78 0, 0, 0, 0,
79 0, 0,
80 (X509V3_EXT_I2V) i2v_GENERAL_NAMES,
81 (X509V3_EXT_V2I)v2i_subject_alt,
82 NULL, NULL, NULL},
83
84 {NID_issuer_alt_name, 0, ASN1_ITEM_ref(GENERAL_NAMES),
85 0, 0, 0, 0,
86 0, 0,
87 (X509V3_EXT_I2V) i2v_GENERAL_NAMES,
88 (X509V3_EXT_V2I)v2i_issuer_alt,
89 NULL, NULL, NULL},
90 };
91
STACK_OF(CONF_VALUE)92 STACK_OF(CONF_VALUE) *i2v_GENERAL_NAMES(X509V3_EXT_METHOD *method,
93 GENERAL_NAMES *gens,
94 STACK_OF(CONF_VALUE) *ret)
95 {
96 int i;
97 GENERAL_NAME *gen;
98 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
99 gen = sk_GENERAL_NAME_value(gens, i);
100 ret = i2v_GENERAL_NAME(method, gen, ret);
101 }
102 if (!ret)
103 return sk_CONF_VALUE_new_null();
104 return ret;
105 }
106
STACK_OF(CONF_VALUE)107 STACK_OF(CONF_VALUE) *i2v_GENERAL_NAME(X509V3_EXT_METHOD *method,
108 GENERAL_NAME *gen,
109 STACK_OF(CONF_VALUE) *ret)
110 {
111 unsigned char *p;
112 char oline[256], htmp[5];
113 int i;
114 switch (gen->type) {
115 case GEN_OTHERNAME:
116 X509V3_add_value("othername", "<unsupported>", &ret);
117 break;
118
119 case GEN_X400:
120 X509V3_add_value("X400Name", "<unsupported>", &ret);
121 break;
122
123 case GEN_EDIPARTY:
124 X509V3_add_value("EdiPartyName", "<unsupported>", &ret);
125 break;
126
127 case GEN_EMAIL:
128 X509V3_add_value_uchar("email", gen->d.ia5->data, &ret);
129 break;
130
131 case GEN_DNS:
132 X509V3_add_value_uchar("DNS", gen->d.ia5->data, &ret);
133 break;
134
135 case GEN_URI:
136 X509V3_add_value_uchar("URI", gen->d.ia5->data, &ret);
137 break;
138
139 case GEN_DIRNAME:
140 X509_NAME_oneline(gen->d.dirn, oline, 256);
141 X509V3_add_value("DirName", oline, &ret);
142 break;
143
144 case GEN_IPADD:
145 p = gen->d.ip->data;
146 if (gen->d.ip->length == 4)
147 BIO_snprintf(oline, sizeof oline,
148 "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
149 else if (gen->d.ip->length == 16) {
150 oline[0] = 0;
151 for (i = 0; i < 8; i++) {
152 BIO_snprintf(htmp, sizeof htmp, "%X", p[0] << 8 | p[1]);
153 p += 2;
154 strcat(oline, htmp);
155 if (i != 7)
156 strcat(oline, ":");
157 }
158 } else {
159 X509V3_add_value("IP Address", "<invalid>", &ret);
160 break;
161 }
162 X509V3_add_value("IP Address", oline, &ret);
163 break;
164
165 case GEN_RID:
166 i2t_ASN1_OBJECT(oline, 256, gen->d.rid);
167 X509V3_add_value("Registered ID", oline, &ret);
168 break;
169 }
170 return ret;
171 }
172
GENERAL_NAME_print(BIO * out,GENERAL_NAME * gen)173 int GENERAL_NAME_print(BIO *out, GENERAL_NAME *gen)
174 {
175 unsigned char *p;
176 int i;
177 switch (gen->type) {
178 case GEN_OTHERNAME:
179 BIO_printf(out, "othername:<unsupported>");
180 break;
181
182 case GEN_X400:
183 BIO_printf(out, "X400Name:<unsupported>");
184 break;
185
186 case GEN_EDIPARTY:
187 /* Maybe fix this: it is supported now */
188 BIO_printf(out, "EdiPartyName:<unsupported>");
189 break;
190
191 case GEN_EMAIL:
192 BIO_printf(out, "email:%s", gen->d.ia5->data);
193 break;
194
195 case GEN_DNS:
196 BIO_printf(out, "DNS:%s", gen->d.ia5->data);
197 break;
198
199 case GEN_URI:
200 BIO_printf(out, "URI:%s", gen->d.ia5->data);
201 break;
202
203 case GEN_DIRNAME:
204 BIO_printf(out, "DirName: ");
205 X509_NAME_print_ex(out, gen->d.dirn, 0, XN_FLAG_ONELINE);
206 break;
207
208 case GEN_IPADD:
209 p = gen->d.ip->data;
210 if (gen->d.ip->length == 4)
211 BIO_printf(out, "IP Address:%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
212 else if (gen->d.ip->length == 16) {
213 BIO_printf(out, "IP Address");
214 for (i = 0; i < 8; i++) {
215 BIO_printf(out, ":%X", p[0] << 8 | p[1]);
216 p += 2;
217 }
218 BIO_puts(out, "\n");
219 } else {
220 BIO_printf(out, "IP Address:<invalid>");
221 break;
222 }
223 break;
224
225 case GEN_RID:
226 BIO_printf(out, "Registered ID");
227 i2a_ASN1_OBJECT(out, gen->d.rid);
228 break;
229 }
230 return 1;
231 }
232
v2i_issuer_alt(X509V3_EXT_METHOD * method,X509V3_CTX * ctx,STACK_OF (CONF_VALUE)* nval)233 static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method,
234 X509V3_CTX *ctx,
235 STACK_OF(CONF_VALUE) *nval)
236 {
237 GENERAL_NAMES *gens = NULL;
238 CONF_VALUE *cnf;
239 int i;
240 if (!(gens = sk_GENERAL_NAME_new_null())) {
241 X509V3err(X509V3_F_V2I_ISSUER_ALT, ERR_R_MALLOC_FAILURE);
242 return NULL;
243 }
244 for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
245 cnf = sk_CONF_VALUE_value(nval, i);
246 if (!name_cmp(cnf->name, "issuer") && cnf->value &&
247 !strcmp(cnf->value, "copy")) {
248 if (!copy_issuer(ctx, gens))
249 goto err;
250 } else {
251 GENERAL_NAME *gen;
252 if (!(gen = v2i_GENERAL_NAME(method, ctx, cnf)))
253 goto err;
254 sk_GENERAL_NAME_push(gens, gen);
255 }
256 }
257 return gens;
258 err:
259 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
260 return NULL;
261 }
262
263 /* Append subject altname of issuer to issuer alt name of subject */
264
copy_issuer(X509V3_CTX * ctx,GENERAL_NAMES * gens)265 static int copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens)
266 {
267 GENERAL_NAMES *ialt;
268 GENERAL_NAME *gen;
269 X509_EXTENSION *ext;
270 int i;
271 if (ctx && (ctx->flags == CTX_TEST))
272 return 1;
273 if (!ctx || !ctx->issuer_cert) {
274 X509V3err(X509V3_F_COPY_ISSUER, X509V3_R_NO_ISSUER_DETAILS);
275 goto err;
276 }
277 i = X509_get_ext_by_NID(ctx->issuer_cert, NID_subject_alt_name, -1);
278 if (i < 0)
279 return 1;
280 if (!(ext = X509_get_ext(ctx->issuer_cert, i)) ||
281 !(ialt = X509V3_EXT_d2i(ext))) {
282 X509V3err(X509V3_F_COPY_ISSUER, X509V3_R_ISSUER_DECODE_ERROR);
283 goto err;
284 }
285
286 for (i = 0; i < sk_GENERAL_NAME_num(ialt); i++) {
287 gen = sk_GENERAL_NAME_value(ialt, i);
288 if (!sk_GENERAL_NAME_push(gens, gen)) {
289 X509V3err(X509V3_F_COPY_ISSUER, ERR_R_MALLOC_FAILURE);
290 goto err;
291 }
292 }
293 sk_GENERAL_NAME_free(ialt);
294
295 return 1;
296
297 err:
298 return 0;
299
300 }
301
v2i_subject_alt(X509V3_EXT_METHOD * method,X509V3_CTX * ctx,STACK_OF (CONF_VALUE)* nval)302 static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method,
303 X509V3_CTX *ctx,
304 STACK_OF(CONF_VALUE) *nval)
305 {
306 GENERAL_NAMES *gens = NULL;
307 CONF_VALUE *cnf;
308 int i;
309 if (!(gens = sk_GENERAL_NAME_new_null())) {
310 X509V3err(X509V3_F_V2I_SUBJECT_ALT, ERR_R_MALLOC_FAILURE);
311 return NULL;
312 }
313 for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
314 cnf = sk_CONF_VALUE_value(nval, i);
315 if (!name_cmp(cnf->name, "email") && cnf->value &&
316 !strcmp(cnf->value, "copy")) {
317 if (!copy_email(ctx, gens, 0))
318 goto err;
319 } else if (!name_cmp(cnf->name, "email") && cnf->value &&
320 !strcmp(cnf->value, "move")) {
321 if (!copy_email(ctx, gens, 1))
322 goto err;
323 } else {
324 GENERAL_NAME *gen;
325 if (!(gen = v2i_GENERAL_NAME(method, ctx, cnf)))
326 goto err;
327 sk_GENERAL_NAME_push(gens, gen);
328 }
329 }
330 return gens;
331 err:
332 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
333 return NULL;
334 }
335
336 /*
337 * Copy any email addresses in a certificate or request to GENERAL_NAMES
338 */
339
copy_email(X509V3_CTX * ctx,GENERAL_NAMES * gens,int move_p)340 static int copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p)
341 {
342 X509_NAME *nm;
343 ASN1_IA5STRING *email = NULL;
344 X509_NAME_ENTRY *ne;
345 GENERAL_NAME *gen = NULL;
346 int i;
347 if (ctx != NULL && ctx->flags == CTX_TEST)
348 return 1;
349 if (!ctx || (!ctx->subject_cert && !ctx->subject_req)) {
350 X509V3err(X509V3_F_COPY_EMAIL, X509V3_R_NO_SUBJECT_DETAILS);
351 goto err;
352 }
353 /* Find the subject name */
354 if (ctx->subject_cert)
355 nm = X509_get_subject_name(ctx->subject_cert);
356 else
357 nm = X509_REQ_get_subject_name(ctx->subject_req);
358
359 /* Now add any email address(es) to STACK */
360 i = -1;
361 while ((i = X509_NAME_get_index_by_NID(nm,
362 NID_pkcs9_emailAddress, i)) >= 0) {
363 ne = X509_NAME_get_entry(nm, i);
364 email = M_ASN1_IA5STRING_dup(X509_NAME_ENTRY_get_data(ne));
365 if (move_p) {
366 X509_NAME_delete_entry(nm, i);
367 X509_NAME_ENTRY_free(ne);
368 i--;
369 }
370 if (!email || !(gen = GENERAL_NAME_new())) {
371 X509V3err(X509V3_F_COPY_EMAIL, ERR_R_MALLOC_FAILURE);
372 goto err;
373 }
374 gen->d.ia5 = email;
375 email = NULL;
376 gen->type = GEN_EMAIL;
377 if (!sk_GENERAL_NAME_push(gens, gen)) {
378 X509V3err(X509V3_F_COPY_EMAIL, ERR_R_MALLOC_FAILURE);
379 goto err;
380 }
381 gen = NULL;
382 }
383
384 return 1;
385
386 err:
387 GENERAL_NAME_free(gen);
388 M_ASN1_IA5STRING_free(email);
389 return 0;
390
391 }
392
v2i_GENERAL_NAMES(X509V3_EXT_METHOD * method,X509V3_CTX * ctx,STACK_OF (CONF_VALUE)* nval)393 GENERAL_NAMES *v2i_GENERAL_NAMES(X509V3_EXT_METHOD *method,
394 X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
395 {
396 GENERAL_NAME *gen;
397 GENERAL_NAMES *gens = NULL;
398 CONF_VALUE *cnf;
399 int i;
400 if (!(gens = sk_GENERAL_NAME_new_null())) {
401 X509V3err(X509V3_F_V2I_GENERAL_NAMES, ERR_R_MALLOC_FAILURE);
402 return NULL;
403 }
404 for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
405 cnf = sk_CONF_VALUE_value(nval, i);
406 if (!(gen = v2i_GENERAL_NAME(method, ctx, cnf)))
407 goto err;
408 sk_GENERAL_NAME_push(gens, gen);
409 }
410 return gens;
411 err:
412 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
413 return NULL;
414 }
415
v2i_GENERAL_NAME(X509V3_EXT_METHOD * method,X509V3_CTX * ctx,CONF_VALUE * cnf)416 GENERAL_NAME *v2i_GENERAL_NAME(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
417 CONF_VALUE *cnf)
418 {
419 return v2i_GENERAL_NAME_ex(NULL, method, ctx, cnf, 0);
420 }
421
v2i_GENERAL_NAME_ex(GENERAL_NAME * out,X509V3_EXT_METHOD * method,X509V3_CTX * ctx,CONF_VALUE * cnf,int is_nc)422 GENERAL_NAME *v2i_GENERAL_NAME_ex(GENERAL_NAME *out,
423 X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
424 CONF_VALUE *cnf, int is_nc)
425 {
426 char is_string = 0;
427 int type;
428 GENERAL_NAME *gen = NULL;
429
430 char *name, *value;
431
432 name = cnf->name;
433 value = cnf->value;
434
435 if (!value) {
436 X509V3err(X509V3_F_V2I_GENERAL_NAME_EX, X509V3_R_MISSING_VALUE);
437 return NULL;
438 }
439
440 if (out)
441 gen = out;
442 else {
443 gen = GENERAL_NAME_new();
444 if (gen == NULL) {
445 X509V3err(X509V3_F_V2I_GENERAL_NAME_EX, ERR_R_MALLOC_FAILURE);
446 return NULL;
447 }
448 }
449
450 if (!name_cmp(name, "email")) {
451 is_string = 1;
452 type = GEN_EMAIL;
453 } else if (!name_cmp(name, "URI")) {
454 is_string = 1;
455 type = GEN_URI;
456 } else if (!name_cmp(name, "DNS")) {
457 is_string = 1;
458 type = GEN_DNS;
459 } else if (!name_cmp(name, "RID")) {
460 ASN1_OBJECT *obj;
461 if (!(obj = OBJ_txt2obj(value, 0))) {
462 X509V3err(X509V3_F_V2I_GENERAL_NAME_EX, X509V3_R_BAD_OBJECT);
463 ERR_add_error_data(2, "value=", value);
464 goto err;
465 }
466 gen->d.rid = obj;
467 type = GEN_RID;
468 } else if (!name_cmp(name, "IP")) {
469 if (is_nc)
470 gen->d.ip = a2i_IPADDRESS_NC(value);
471 else
472 gen->d.ip = a2i_IPADDRESS(value);
473 if (gen->d.ip == NULL) {
474 X509V3err(X509V3_F_V2I_GENERAL_NAME_EX, X509V3_R_BAD_IP_ADDRESS);
475 ERR_add_error_data(2, "value=", value);
476 goto err;
477 }
478 type = GEN_IPADD;
479 } else if (!name_cmp(name, "dirName")) {
480 type = GEN_DIRNAME;
481 if (!do_dirname(gen, value, ctx)) {
482 X509V3err(X509V3_F_V2I_GENERAL_NAME_EX, X509V3_R_DIRNAME_ERROR);
483 goto err;
484 }
485 } else if (!name_cmp(name, "otherName")) {
486 if (!do_othername(gen, value, ctx)) {
487 X509V3err(X509V3_F_V2I_GENERAL_NAME_EX, X509V3_R_OTHERNAME_ERROR);
488 goto err;
489 }
490 type = GEN_OTHERNAME;
491 } else {
492 X509V3err(X509V3_F_V2I_GENERAL_NAME_EX, X509V3_R_UNSUPPORTED_OPTION);
493 ERR_add_error_data(2, "name=", name);
494 goto err;
495 }
496
497 if (is_string) {
498 if (!(gen->d.ia5 = M_ASN1_IA5STRING_new()) ||
499 !ASN1_STRING_set(gen->d.ia5, (unsigned char *)value,
500 strlen(value))) {
501 X509V3err(X509V3_F_V2I_GENERAL_NAME_EX, ERR_R_MALLOC_FAILURE);
502 goto err;
503 }
504 }
505
506 gen->type = type;
507
508 return gen;
509
510 err:
511 if (!out)
512 GENERAL_NAME_free(gen);
513 return NULL;
514 }
515
do_othername(GENERAL_NAME * gen,char * value,X509V3_CTX * ctx)516 static int do_othername(GENERAL_NAME *gen, char *value, X509V3_CTX *ctx)
517 {
518 char *objtmp = NULL, *p;
519 int objlen;
520 if (!(p = strchr(value, ';')))
521 return 0;
522 if (!(gen->d.otherName = OTHERNAME_new()))
523 return 0;
524 /*
525 * Free this up because we will overwrite it. no need to free type_id
526 * because it is static
527 */
528 ASN1_TYPE_free(gen->d.otherName->value);
529 if (!(gen->d.otherName->value = ASN1_generate_v3(p + 1, ctx)))
530 return 0;
531 objlen = p - value;
532 objtmp = OPENSSL_malloc(objlen + 1);
533 strncpy(objtmp, value, objlen);
534 objtmp[objlen] = 0;
535 gen->d.otherName->type_id = OBJ_txt2obj(objtmp, 0);
536 OPENSSL_free(objtmp);
537 if (!gen->d.otherName->type_id)
538 return 0;
539 return 1;
540 }
541
do_dirname(GENERAL_NAME * gen,char * value,X509V3_CTX * ctx)542 static int do_dirname(GENERAL_NAME *gen, char *value, X509V3_CTX *ctx)
543 {
544 int ret;
545 STACK_OF(CONF_VALUE) *sk;
546 X509_NAME *nm;
547 if (!(nm = X509_NAME_new()))
548 return 0;
549 sk = X509V3_get_section(ctx, value);
550 if (!sk) {
551 X509V3err(X509V3_F_DO_DIRNAME, X509V3_R_SECTION_NOT_FOUND);
552 ERR_add_error_data(2, "section=", value);
553 X509_NAME_free(nm);
554 return 0;
555 }
556 /* FIXME: should allow other character types... */
557 ret = X509V3_NAME_from_section(nm, sk, MBSTRING_ASC);
558 if (!ret)
559 X509_NAME_free(nm);
560 gen->d.dirn = nm;
561
562 X509V3_section_free(ctx, sk);
563
564 return ret;
565 }
566