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