b2914ee6df19300fc6b2e30b015b5ccd9c72754a
[openssl.git] / crypto / x509v3 / v3_alt.c
1 /*
2  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999-2003 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
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
18  *    distribution.
19  *
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/)"
24  *
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.
29  *
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.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
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  * ====================================================================
52  *
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).
56  *
57  */
58
59 #include <stdio.h>
60 #include "internal/cryptlib.h"
61 #include <openssl/conf.h>
62 #include <openssl/x509v3.h>
63 #include "ext_dat.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     {NID_certificate_issuer, 0, ASN1_ITEM_ref(GENERAL_NAMES),
92      0, 0, 0, 0,
93      0, 0,
94      (X509V3_EXT_I2V) i2v_GENERAL_NAMES,
95      NULL, NULL, NULL, NULL},
96 };
97
98 STACK_OF(CONF_VALUE) *i2v_GENERAL_NAMES(X509V3_EXT_METHOD *method,
99                                         GENERAL_NAMES *gens,
100                                         STACK_OF(CONF_VALUE) *ret)
101 {
102     int i;
103     GENERAL_NAME *gen;
104     for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
105         gen = sk_GENERAL_NAME_value(gens, i);
106         ret = i2v_GENERAL_NAME(method, gen, ret);
107     }
108     if (!ret)
109         return sk_CONF_VALUE_new_null();
110     return ret;
111 }
112
113 STACK_OF(CONF_VALUE) *i2v_GENERAL_NAME(X509V3_EXT_METHOD *method,
114                                        GENERAL_NAME *gen,
115                                        STACK_OF(CONF_VALUE) *ret)
116 {
117     unsigned char *p;
118     char oline[256], htmp[5];
119     int i;
120     switch (gen->type) {
121     case GEN_OTHERNAME:
122         X509V3_add_value("othername", "<unsupported>", &ret);
123         break;
124
125     case GEN_X400:
126         X509V3_add_value("X400Name", "<unsupported>", &ret);
127         break;
128
129     case GEN_EDIPARTY:
130         X509V3_add_value("EdiPartyName", "<unsupported>", &ret);
131         break;
132
133     case GEN_EMAIL:
134         X509V3_add_value_uchar("email", gen->d.ia5->data, &ret);
135         break;
136
137     case GEN_DNS:
138         X509V3_add_value_uchar("DNS", gen->d.ia5->data, &ret);
139         break;
140
141     case GEN_URI:
142         X509V3_add_value_uchar("URI", gen->d.ia5->data, &ret);
143         break;
144
145     case GEN_DIRNAME:
146         X509_NAME_oneline(gen->d.dirn, oline, 256);
147         X509V3_add_value("DirName", oline, &ret);
148         break;
149
150     case GEN_IPADD:
151         p = gen->d.ip->data;
152         if (gen->d.ip->length == 4)
153             BIO_snprintf(oline, sizeof oline,
154                          "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
155         else if (gen->d.ip->length == 16) {
156             oline[0] = 0;
157             for (i = 0; i < 8; i++) {
158                 BIO_snprintf(htmp, sizeof htmp, "%X", p[0] << 8 | p[1]);
159                 p += 2;
160                 strcat(oline, htmp);
161                 if (i != 7)
162                     strcat(oline, ":");
163             }
164         } else {
165             X509V3_add_value("IP Address", "<invalid>", &ret);
166             break;
167         }
168         X509V3_add_value("IP Address", oline, &ret);
169         break;
170
171     case GEN_RID:
172         i2t_ASN1_OBJECT(oline, 256, gen->d.rid);
173         X509V3_add_value("Registered ID", oline, &ret);
174         break;
175     }
176     return ret;
177 }
178
179 int GENERAL_NAME_print(BIO *out, GENERAL_NAME *gen)
180 {
181     unsigned char *p;
182     int i;
183     switch (gen->type) {
184     case GEN_OTHERNAME:
185         BIO_printf(out, "othername:<unsupported>");
186         break;
187
188     case GEN_X400:
189         BIO_printf(out, "X400Name:<unsupported>");
190         break;
191
192     case GEN_EDIPARTY:
193         /* Maybe fix this: it is supported now */
194         BIO_printf(out, "EdiPartyName:<unsupported>");
195         break;
196
197     case GEN_EMAIL:
198         BIO_printf(out, "email:%s", gen->d.ia5->data);
199         break;
200
201     case GEN_DNS:
202         BIO_printf(out, "DNS:%s", gen->d.ia5->data);
203         break;
204
205     case GEN_URI:
206         BIO_printf(out, "URI:%s", gen->d.ia5->data);
207         break;
208
209     case GEN_DIRNAME:
210         BIO_printf(out, "DirName: ");
211         X509_NAME_print_ex(out, gen->d.dirn, 0, XN_FLAG_ONELINE);
212         break;
213
214     case GEN_IPADD:
215         p = gen->d.ip->data;
216         if (gen->d.ip->length == 4)
217             BIO_printf(out, "IP Address:%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
218         else if (gen->d.ip->length == 16) {
219             BIO_printf(out, "IP Address");
220             for (i = 0; i < 8; i++) {
221                 BIO_printf(out, ":%X", p[0] << 8 | p[1]);
222                 p += 2;
223             }
224             BIO_puts(out, "\n");
225         } else {
226             BIO_printf(out, "IP Address:<invalid>");
227             break;
228         }
229         break;
230
231     case GEN_RID:
232         BIO_printf(out, "Registered ID");
233         i2a_ASN1_OBJECT(out, gen->d.rid);
234         break;
235     }
236     return 1;
237 }
238
239 static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method,
240                                      X509V3_CTX *ctx,
241                                      STACK_OF(CONF_VALUE) *nval)
242 {
243     GENERAL_NAMES *gens = NULL;
244     CONF_VALUE *cnf;
245     int i;
246
247     if ((gens = sk_GENERAL_NAME_new_null()) == NULL) {
248         X509V3err(X509V3_F_V2I_ISSUER_ALT, ERR_R_MALLOC_FAILURE);
249         return NULL;
250     }
251     for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
252         cnf = sk_CONF_VALUE_value(nval, i);
253         if (!name_cmp(cnf->name, "issuer")
254             && cnf->value && strcmp(cnf->value, "copy") == 0) {
255             if (!copy_issuer(ctx, gens))
256                 goto err;
257         } else {
258             GENERAL_NAME *gen;
259             if ((gen = v2i_GENERAL_NAME(method, ctx, cnf)) == NULL)
260                 goto err;
261             sk_GENERAL_NAME_push(gens, gen);
262         }
263     }
264     return gens;
265  err:
266     sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
267     return NULL;
268 }
269
270 /* Append subject altname of issuer to issuer alt name of subject */
271
272 static int copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens)
273 {
274     GENERAL_NAMES *ialt;
275     GENERAL_NAME *gen;
276     X509_EXTENSION *ext;
277     int i;
278
279     if (ctx && (ctx->flags == CTX_TEST))
280         return 1;
281     if (!ctx || !ctx->issuer_cert) {
282         X509V3err(X509V3_F_COPY_ISSUER, X509V3_R_NO_ISSUER_DETAILS);
283         goto err;
284     }
285     i = X509_get_ext_by_NID(ctx->issuer_cert, NID_subject_alt_name, -1);
286     if (i < 0)
287         return 1;
288     if ((ext = X509_get_ext(ctx->issuer_cert, i)) == NULL
289         || (ialt = X509V3_EXT_d2i(ext)) == NULL) {
290         X509V3err(X509V3_F_COPY_ISSUER, X509V3_R_ISSUER_DECODE_ERROR);
291         goto err;
292     }
293
294     for (i = 0; i < sk_GENERAL_NAME_num(ialt); i++) {
295         gen = sk_GENERAL_NAME_value(ialt, i);
296         if (!sk_GENERAL_NAME_push(gens, gen)) {
297             X509V3err(X509V3_F_COPY_ISSUER, ERR_R_MALLOC_FAILURE);
298             goto err;
299         }
300     }
301     sk_GENERAL_NAME_free(ialt);
302
303     return 1;
304
305  err:
306     return 0;
307
308 }
309
310 static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method,
311                                       X509V3_CTX *ctx,
312                                       STACK_OF(CONF_VALUE) *nval)
313 {
314     GENERAL_NAMES *gens = NULL;
315     CONF_VALUE *cnf;
316     int i;
317
318     if ((gens = sk_GENERAL_NAME_new_null()) == NULL) {
319         X509V3err(X509V3_F_V2I_SUBJECT_ALT, ERR_R_MALLOC_FAILURE);
320         return NULL;
321     }
322     for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
323         cnf = sk_CONF_VALUE_value(nval, i);
324         if (!name_cmp(cnf->name, "email")
325             && cnf->value && strcmp(cnf->value, "copy") == 0) {
326             if (!copy_email(ctx, gens, 0))
327                 goto err;
328         } else if (!name_cmp(cnf->name, "email")
329                    && cnf->value && strcmp(cnf->value, "move") == 0) {
330             if (!copy_email(ctx, gens, 1))
331                 goto err;
332         } else {
333             GENERAL_NAME *gen;
334             if ((gen = v2i_GENERAL_NAME(method, ctx, cnf)) == NULL)
335                 goto err;
336             sk_GENERAL_NAME_push(gens, gen);
337         }
338     }
339     return gens;
340  err:
341     sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
342     return NULL;
343 }
344
345 /*
346  * Copy any email addresses in a certificate or request to GENERAL_NAMES
347  */
348
349 static int copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p)
350 {
351     X509_NAME *nm;
352     ASN1_IA5STRING *email = NULL;
353     X509_NAME_ENTRY *ne;
354     GENERAL_NAME *gen = NULL;
355     int i;
356     if (ctx != NULL && ctx->flags == CTX_TEST)
357         return 1;
358     if (!ctx || (!ctx->subject_cert && !ctx->subject_req)) {
359         X509V3err(X509V3_F_COPY_EMAIL, X509V3_R_NO_SUBJECT_DETAILS);
360         goto err;
361     }
362     /* Find the subject name */
363     if (ctx->subject_cert)
364         nm = X509_get_subject_name(ctx->subject_cert);
365     else
366         nm = X509_REQ_get_subject_name(ctx->subject_req);
367
368     /* Now add any email address(es) to STACK */
369     i = -1;
370     while ((i = X509_NAME_get_index_by_NID(nm,
371                                            NID_pkcs9_emailAddress, i)) >= 0) {
372         ne = X509_NAME_get_entry(nm, i);
373         email = ASN1_STRING_dup(X509_NAME_ENTRY_get_data(ne));
374         if (move_p) {
375             X509_NAME_delete_entry(nm, i);
376             X509_NAME_ENTRY_free(ne);
377             i--;
378         }
379         if (email == NULL || (gen = GENERAL_NAME_new()) == NULL) {
380             X509V3err(X509V3_F_COPY_EMAIL, ERR_R_MALLOC_FAILURE);
381             goto err;
382         }
383         gen->d.ia5 = email;
384         email = NULL;
385         gen->type = GEN_EMAIL;
386         if (!sk_GENERAL_NAME_push(gens, gen)) {
387             X509V3err(X509V3_F_COPY_EMAIL, ERR_R_MALLOC_FAILURE);
388             goto err;
389         }
390         gen = NULL;
391     }
392
393     return 1;
394
395  err:
396     GENERAL_NAME_free(gen);
397     ASN1_IA5STRING_free(email);
398     return 0;
399
400 }
401
402 GENERAL_NAMES *v2i_GENERAL_NAMES(const X509V3_EXT_METHOD *method,
403                                  X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
404 {
405     GENERAL_NAME *gen;
406     GENERAL_NAMES *gens = NULL;
407     CONF_VALUE *cnf;
408     int i;
409
410     if ((gens = sk_GENERAL_NAME_new_null()) == NULL) {
411         X509V3err(X509V3_F_V2I_GENERAL_NAMES, ERR_R_MALLOC_FAILURE);
412         return NULL;
413     }
414     for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
415         cnf = sk_CONF_VALUE_value(nval, i);
416         if ((gen = v2i_GENERAL_NAME(method, ctx, cnf)) == NULL)
417             goto err;
418         sk_GENERAL_NAME_push(gens, gen);
419     }
420     return gens;
421  err:
422     sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
423     return NULL;
424 }
425
426 GENERAL_NAME *v2i_GENERAL_NAME(const X509V3_EXT_METHOD *method,
427                                X509V3_CTX *ctx, CONF_VALUE *cnf)
428 {
429     return v2i_GENERAL_NAME_ex(NULL, method, ctx, cnf, 0);
430 }
431
432 GENERAL_NAME *a2i_GENERAL_NAME(GENERAL_NAME *out,
433                                const X509V3_EXT_METHOD *method,
434                                X509V3_CTX *ctx, int gen_type, char *value,
435                                int is_nc)
436 {
437     char is_string = 0;
438     GENERAL_NAME *gen = NULL;
439
440     if (!value) {
441         X509V3err(X509V3_F_A2I_GENERAL_NAME, X509V3_R_MISSING_VALUE);
442         return NULL;
443     }
444
445     if (out)
446         gen = out;
447     else {
448         gen = GENERAL_NAME_new();
449         if (gen == NULL) {
450             X509V3err(X509V3_F_A2I_GENERAL_NAME, ERR_R_MALLOC_FAILURE);
451             return NULL;
452         }
453     }
454
455     switch (gen_type) {
456     case GEN_URI:
457     case GEN_EMAIL:
458     case GEN_DNS:
459         is_string = 1;
460         break;
461
462     case GEN_RID:
463         {
464             ASN1_OBJECT *obj;
465             if ((obj = OBJ_txt2obj(value, 0)) == NULL) {
466                 X509V3err(X509V3_F_A2I_GENERAL_NAME, X509V3_R_BAD_OBJECT);
467                 ERR_add_error_data(2, "value=", value);
468                 goto err;
469             }
470             gen->d.rid = obj;
471         }
472         break;
473
474     case GEN_IPADD:
475         if (is_nc)
476             gen->d.ip = a2i_IPADDRESS_NC(value);
477         else
478             gen->d.ip = a2i_IPADDRESS(value);
479         if (gen->d.ip == NULL) {
480             X509V3err(X509V3_F_A2I_GENERAL_NAME, X509V3_R_BAD_IP_ADDRESS);
481             ERR_add_error_data(2, "value=", value);
482             goto err;
483         }
484         break;
485
486     case GEN_DIRNAME:
487         if (!do_dirname(gen, value, ctx)) {
488             X509V3err(X509V3_F_A2I_GENERAL_NAME, X509V3_R_DIRNAME_ERROR);
489             goto err;
490         }
491         break;
492
493     case GEN_OTHERNAME:
494         if (!do_othername(gen, value, ctx)) {
495             X509V3err(X509V3_F_A2I_GENERAL_NAME, X509V3_R_OTHERNAME_ERROR);
496             goto err;
497         }
498         break;
499     default:
500         X509V3err(X509V3_F_A2I_GENERAL_NAME, X509V3_R_UNSUPPORTED_TYPE);
501         goto err;
502     }
503
504     if (is_string) {
505         if ((gen->d.ia5 = ASN1_IA5STRING_new()) == NULL ||
506             !ASN1_STRING_set(gen->d.ia5, (unsigned char *)value,
507                              strlen(value))) {
508             X509V3err(X509V3_F_A2I_GENERAL_NAME, ERR_R_MALLOC_FAILURE);
509             goto err;
510         }
511     }
512
513     gen->type = gen_type;
514
515     return gen;
516
517  err:
518     if (!out)
519         GENERAL_NAME_free(gen);
520     return NULL;
521 }
522
523 GENERAL_NAME *v2i_GENERAL_NAME_ex(GENERAL_NAME *out,
524                                   const X509V3_EXT_METHOD *method,
525                                   X509V3_CTX *ctx, CONF_VALUE *cnf, int is_nc)
526 {
527     int type;
528
529     char *name, *value;
530
531     name = cnf->name;
532     value = cnf->value;
533
534     if (!value) {
535         X509V3err(X509V3_F_V2I_GENERAL_NAME_EX, X509V3_R_MISSING_VALUE);
536         return NULL;
537     }
538
539     if (!name_cmp(name, "email"))
540         type = GEN_EMAIL;
541     else if (!name_cmp(name, "URI"))
542         type = GEN_URI;
543     else if (!name_cmp(name, "DNS"))
544         type = GEN_DNS;
545     else if (!name_cmp(name, "RID"))
546         type = GEN_RID;
547     else if (!name_cmp(name, "IP"))
548         type = GEN_IPADD;
549     else if (!name_cmp(name, "dirName"))
550         type = GEN_DIRNAME;
551     else if (!name_cmp(name, "otherName"))
552         type = GEN_OTHERNAME;
553     else {
554         X509V3err(X509V3_F_V2I_GENERAL_NAME_EX, X509V3_R_UNSUPPORTED_OPTION);
555         ERR_add_error_data(2, "name=", name);
556         return NULL;
557     }
558
559     return a2i_GENERAL_NAME(out, method, ctx, type, value, is_nc);
560
561 }
562
563 static int do_othername(GENERAL_NAME *gen, char *value, X509V3_CTX *ctx)
564 {
565     char *objtmp = NULL, *p;
566     int objlen;
567
568     if ((p = strchr(value, ';')) == NULL)
569         return 0;
570     if ((gen->d.otherName = OTHERNAME_new()) == NULL)
571         return 0;
572     /*
573      * Free this up because we will overwrite it. no need to free type_id
574      * because it is static
575      */
576     ASN1_TYPE_free(gen->d.otherName->value);
577     if ((gen->d.otherName->value = ASN1_generate_v3(p + 1, ctx)) == NULL)
578         return 0;
579     objlen = p - value;
580     objtmp = OPENSSL_malloc(objlen + 1);
581     if (objtmp == NULL)
582         return 0;
583     strncpy(objtmp, value, objlen);
584     objtmp[objlen] = 0;
585     gen->d.otherName->type_id = OBJ_txt2obj(objtmp, 0);
586     OPENSSL_free(objtmp);
587     if (!gen->d.otherName->type_id)
588         return 0;
589     return 1;
590 }
591
592 static int do_dirname(GENERAL_NAME *gen, char *value, X509V3_CTX *ctx)
593 {
594     int ret = 0;
595     STACK_OF(CONF_VALUE) *sk = NULL;
596     X509_NAME *nm;
597
598     if ((nm = X509_NAME_new()) == NULL)
599         goto err;
600     sk = X509V3_get_section(ctx, value);
601     if (!sk) {
602         X509V3err(X509V3_F_DO_DIRNAME, X509V3_R_SECTION_NOT_FOUND);
603         ERR_add_error_data(2, "section=", value);
604         goto err;
605     }
606     /* FIXME: should allow other character types... */
607     ret = X509V3_NAME_from_section(nm, sk, MBSTRING_ASC);
608     if (!ret)
609         goto err;
610     gen->d.dirn = nm;
611
612 err:
613     if (ret == 0)
614         X509_NAME_free(nm);
615     X509V3_section_free(ctx, sk);
616     return ret;
617 }