0e8211b0075abcf013ffd8975fc08b66a401e049
[openssl.git] / crypto / x509v3 / v3_alt.c
1 /* v3_alt.c */
2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3  * project 1999.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999 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 <stdlib.h>
61 #include <pem.h>
62 #include <asn1_mac.h>
63 #include <err.h>
64 #include <objects.h>
65 #include <conf.h>
66 #include "x509v3.h"
67
68 X509V3_EXT_METHOD v3_alt[] = {
69 { NID_subject_alt_name, 0,
70 (X509V3_EXT_NEW)GENERAL_NAMES_new,
71 GENERAL_NAMES_free,
72 (X509V3_EXT_D2I)d2i_GENERAL_NAMES,
73 i2d_GENERAL_NAMES,
74 NULL, NULL,
75 (X509V3_EXT_I2V)i2v_GENERAL_NAMES,
76 (X509V3_EXT_V2I)v2i_GENERAL_NAMES,
77 NULL, NULL},
78 { NID_issuer_alt_name, 0,
79 (X509V3_EXT_NEW)GENERAL_NAMES_new,
80 GENERAL_NAMES_free,
81 (X509V3_EXT_D2I)d2i_GENERAL_NAMES,
82 i2d_GENERAL_NAMES,
83 NULL, NULL,
84 (X509V3_EXT_I2V)i2v_GENERAL_NAMES,
85 (X509V3_EXT_V2I)v2i_GENERAL_NAMES,
86 NULL, NULL},
87 EXT_END
88 };
89
90 STACK *i2v_GENERAL_NAMES(method, gens, ret)
91 X509V3_EXT_METHOD *method;
92 STACK *gens;
93 STACK *ret;
94 {
95         int i;
96         GENERAL_NAME *gen;
97         for(i = 0; i < sk_num(gens); i++) {
98                 gen = (GENERAL_NAME *)sk_value(gens, i);
99                 ret = i2v_GENERAL_NAME(method, gen, ret);
100         }
101         return ret;
102 }
103
104 STACK *i2v_GENERAL_NAME(method, gen, ret)
105 X509V3_EXT_METHOD *method;
106 GENERAL_NAME *gen;
107 STACK *ret;
108 {
109         char oline[256];
110         unsigned char *p;
111         switch (gen->type)
112         {
113                 case GEN_OTHERNAME:
114                 X509V3_add_value("othername","<unsupported>", &ret);
115                 break;
116
117                 case GEN_X400:
118                 X509V3_add_value("X400Name","<unsupported>", &ret);
119                 break;
120
121                 case GEN_EDIPARTY:
122                 X509V3_add_value("EdiPartyName","<unsupported>", &ret);
123                 break;
124
125                 case GEN_EMAIL:
126                 X509V3_add_value("email",gen->d.ia5->data, &ret);
127                 break;
128
129                 case GEN_DNS:
130                 X509V3_add_value("DNS",gen->d.ia5->data, &ret);
131                 break;
132
133                 case GEN_URI:
134                 X509V3_add_value("URI",gen->d.ia5->data, &ret);
135                 break;
136
137                 case GEN_DIRNAME:
138                 X509_NAME_oneline(gen->d.dirn, oline, 256);
139                 X509V3_add_value("DirName",oline, &ret);
140                 break;
141
142                 case GEN_IPADD:
143                 p = gen->d.ip->data;
144                 /* BUG: doesn't support IPV6 */
145                 if(gen->d.ip->length != 4) {
146                         X509V3_add_value("IP Address","<invalid>", &ret);
147                         break;
148                 }
149                 sprintf(oline, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
150                 X509V3_add_value("IP Address",oline, &ret);
151                 break;
152
153                 case GEN_RID:
154                 i2t_ASN1_OBJECT(oline, 256, gen->d.rid);
155                 X509V3_add_value("Registered ID",oline, &ret);
156                 break;
157         }
158         return ret;
159 }
160
161 STACK *v2i_GENERAL_NAMES(method, ctx, nval)
162 X509V3_EXT_METHOD *method;
163 X509V3_CTX *ctx;
164 STACK *nval;
165 {
166         GENERAL_NAME *gen;
167         STACK *gens = NULL;
168         CONF_VALUE *cnf;
169         int i;
170         if(!(gens = sk_new(NULL))) {
171                 X509V3err(X509V3_F_V2I_GENERAL_NAMES,ERR_R_MALLOC_FAILURE);
172                 return NULL;
173         }
174         for(i = 0; i < sk_num(nval); i++) {
175                 cnf = (CONF_VALUE *)sk_value(nval, i);
176                 if(!(gen = v2i_GENERAL_NAME(method, ctx, cnf))) goto err; 
177                 sk_push(gens, (char *)gen);
178         }
179         return gens;
180         err:
181         sk_pop_free(gens, GENERAL_NAME_free);
182         return NULL;
183 }
184
185 GENERAL_NAME *v2i_GENERAL_NAME(method, ctx, cnf)
186 X509V3_EXT_METHOD *method;
187 X509V3_CTX *ctx;
188 CONF_VALUE *cnf;
189 {
190 char is_string = 0;
191 int type;
192 GENERAL_NAME *gen = NULL;
193
194 char *name, *value;
195
196 name = cnf->name;
197 value = cnf->value;
198
199 if(!(gen = GENERAL_NAME_new())) {
200         X509V3err(X509V3_F_V2I_GENERAL_NAME,ERR_R_MALLOC_FAILURE);
201         return NULL;
202 }
203
204 if(!name_cmp(name, "email")) {
205         is_string = 1;
206         type = GEN_EMAIL;
207 } else if(!name_cmp(name, "URI")) {
208         is_string = 1;
209         type = GEN_URI;
210 } else if(!name_cmp(name, "DNS")) {
211         is_string = 1;
212         type = GEN_DNS;
213 } else if(!name_cmp(name, "RID")) {
214         ASN1_OBJECT *obj;
215         if(!(obj = OBJ_txt2obj(value,0))) {
216                 X509V3err(X509V3_F_V2I_GENERAL_NAME,X509V3_R_BAD_OBJECT);
217                 ERR_add_error_data(2, "value=", value);
218                 goto err;
219         }
220         gen->d.rid = obj;
221         type = GEN_RID;
222 } else if(!name_cmp(name, "IP")) {
223         int i1,i2,i3,i4;
224         unsigned char ip[4];
225         if((sscanf(value, "%d.%d.%d.%d",&i1,&i2,&i3,&i4) != 4) ||
226             (i1 < 0) || (i1 > 255) || (i2 < 0) || (i2 > 255) ||
227             (i3 < 0) || (i3 > 255) || (i4 < 0) || (i4 > 255) ) {
228                 X509V3err(X509V3_F_V2I_GENERAL_NAME,X509V3_R_BAD_IP_ADDRESS);
229                 ERR_add_error_data(2, "value=", value);
230                 goto err;
231         }
232         ip[0] = i1; ip[1] = i2 ; ip[2] = i3 ; ip[3] = i4;
233         if(!(gen->d.ip = ASN1_OCTET_STRING_new()) ||
234                 !ASN1_STRING_set(gen->d.ip, ip, 4)) {
235                         X509V3err(X509V3_F_V2I_GENERAL_NAME,ERR_R_MALLOC_FAILURE);
236                         goto err;
237         }
238         type = GEN_IPADD;
239 } else {
240         X509V3err(X509V3_F_V2I_GENERAL_NAME,X509V3_R_UNSUPPORTED_OPTION);
241         ERR_add_error_data(2, "name=", name);
242         goto err;
243 }
244
245 if(is_string) {
246         if(!(gen->d.ia5 = ASN1_IA5STRING_new()) ||
247                       !ASN1_STRING_set(gen->d.ia5, value, strlen(value))) {
248                 X509V3err(X509V3_F_V2I_GENERAL_NAME,ERR_R_MALLOC_FAILURE);
249                 goto err;
250         }
251 }
252
253 gen->type = type;
254
255 return gen;
256
257 err:
258 GENERAL_NAME_free(gen);
259 return NULL;
260 }