Various changes to make this stuff compile under Win32 and VC++ with and
[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 "cryptlib.h"
61 #include "conf.h"
62 #include "x509v3.h"
63
64 #ifndef NOPROTO
65 static STACK *v2i_subject_alt(X509V3_EXT_METHOD *method, X509V3_CTX *ctx, STACK *nval);
66 static STACK *v2i_issuer_alt(X509V3_EXT_METHOD *method, X509V3_CTX *ctx, STACK *nval);
67 static int copy_email(X509V3_CTX *ctx, STACK *gens);
68 static int copy_issuer(X509V3_CTX *ctx, STACK *gens);
69 #else
70 static STACK *v2i_issuer_alt();
71 static STACK *v2i_subject_alt();
72 static int copy_email();
73 static int copy_issuer();
74 #endif
75
76 X509V3_EXT_METHOD v3_alt[] = {
77 { NID_subject_alt_name, 0,
78 (X509V3_EXT_NEW)GENERAL_NAMES_new,
79 GENERAL_NAMES_free,
80 (X509V3_EXT_D2I)d2i_GENERAL_NAMES,
81 i2d_GENERAL_NAMES,
82 NULL, NULL,
83 (X509V3_EXT_I2V)i2v_GENERAL_NAMES,
84 (X509V3_EXT_V2I)v2i_subject_alt,
85 NULL, NULL},
86 { NID_issuer_alt_name, 0,
87 (X509V3_EXT_NEW)GENERAL_NAMES_new,
88 GENERAL_NAMES_free,
89 (X509V3_EXT_D2I)d2i_GENERAL_NAMES,
90 i2d_GENERAL_NAMES,
91 NULL, NULL,
92 (X509V3_EXT_I2V)i2v_GENERAL_NAMES,
93 (X509V3_EXT_V2I)v2i_issuer_alt,
94 NULL, NULL},
95 EXT_END
96 };
97
98 STACK *i2v_GENERAL_NAMES(method, gens, ret)
99 X509V3_EXT_METHOD *method;
100 STACK *gens;
101 STACK *ret;
102 {
103         int i;
104         GENERAL_NAME *gen;
105         for(i = 0; i < sk_num(gens); i++) {
106                 gen = (GENERAL_NAME *)sk_value(gens, i);
107                 ret = i2v_GENERAL_NAME(method, gen, ret);
108         }
109         return ret;
110 }
111
112 STACK *i2v_GENERAL_NAME(method, gen, ret)
113 X509V3_EXT_METHOD *method;
114 GENERAL_NAME *gen;
115 STACK *ret;
116 {
117         char oline[256];
118         unsigned char *p;
119         switch (gen->type)
120         {
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("email",gen->d.ia5->data, &ret);
135                 break;
136
137                 case GEN_DNS:
138                 X509V3_add_value("DNS",gen->d.ia5->data, &ret);
139                 break;
140
141                 case GEN_URI:
142                 X509V3_add_value("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                 /* BUG: doesn't support IPV6 */
153                 if(gen->d.ip->length != 4) {
154                         X509V3_add_value("IP Address","<invalid>", &ret);
155                         break;
156                 }
157                 sprintf(oline, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
158                 X509V3_add_value("IP Address",oline, &ret);
159                 break;
160
161                 case GEN_RID:
162                 i2t_ASN1_OBJECT(oline, 256, gen->d.rid);
163                 X509V3_add_value("Registered ID",oline, &ret);
164                 break;
165         }
166         return ret;
167 }
168
169 static STACK *v2i_issuer_alt(method, ctx, nval)
170 X509V3_EXT_METHOD *method;
171 X509V3_CTX *ctx;
172 STACK *nval;
173 {
174         STACK *gens = NULL;
175         CONF_VALUE *cnf;
176         int i;
177         if(!(gens = sk_new(NULL))) {
178                 X509V3err(X509V3_F_V2I_GENERAL_NAMES,ERR_R_MALLOC_FAILURE);
179                 return NULL;
180         }
181         for(i = 0; i < sk_num(nval); i++) {
182                 cnf = (CONF_VALUE *)sk_value(nval, i);
183                 if(!name_cmp(cnf->name, "issuer") && cnf->value &&
184                                                 !strcmp(cnf->value, "copy")) {
185                         if(!copy_issuer(ctx, gens)) goto err;
186                 } else {
187                         GENERAL_NAME *gen;
188                         if(!(gen = v2i_GENERAL_NAME(method, ctx, cnf)))
189                                                                  goto err; 
190                         sk_push(gens, (char *)gen);
191                 }
192         }
193         return gens;
194         err:
195         sk_pop_free(gens, GENERAL_NAME_free);
196         return NULL;
197 }
198
199 /* Append subject altname of issuer to issuer alt name of subject */
200
201 static int copy_issuer(ctx, gens)
202 X509V3_CTX *ctx;
203 STACK *gens;
204 {
205         STACK *ialt;
206         char *gen;
207         X509_EXTENSION *ext;
208         int i;
209         if(ctx && (ctx->flags == CTX_TEST)) return 1;
210         if(!ctx || !ctx->issuer_cert) {
211                 X509V3err(X509V3_F_COPY_ISSUER,X509V3_R_NO_ISSUER_DETAILS);
212                 goto err;
213         }
214         i = X509_get_ext_by_NID(ctx->issuer_cert, NID_subject_alt_name, -1);
215         if(i < 0) return 1;
216         if(!(ext = X509_get_ext(ctx->issuer_cert, i)) ||
217                         !(ialt = (STACK *) X509V3_EXT_d2i(ext)) ) {
218                 X509V3err(X509V3_F_COPY_ISSUER,X509V3_R_ISSUER_DECODE_ERROR);
219                 goto err;
220         }
221
222         for(i = 0; i < sk_num(ialt); i++) {
223                 gen = sk_value(ialt, i);
224                 if(!sk_push(gens, gen)) {
225                         X509V3err(X509V3_F_COPY_ISSUER,ERR_R_MALLOC_FAILURE);
226                         goto err;
227                 }
228         }
229         sk_free(ialt);
230
231         return 1;
232                 
233         err:
234         return 0;
235         
236 }
237
238 static STACK *v2i_subject_alt(method, ctx, nval)
239 X509V3_EXT_METHOD *method;
240 X509V3_CTX *ctx;
241 STACK *nval;
242 {
243         STACK *gens = NULL;
244         CONF_VALUE *cnf;
245         int i;
246         if(!(gens = sk_new(NULL))) {
247                 X509V3err(X509V3_F_V2I_GENERAL_NAMES,ERR_R_MALLOC_FAILURE);
248                 return NULL;
249         }
250         for(i = 0; i < sk_num(nval); i++) {
251                 cnf = (CONF_VALUE *)sk_value(nval, i);
252                 if(!name_cmp(cnf->name, "email") && cnf->value &&
253                                                 !strcmp(cnf->value, "copy")) {
254                         if(!copy_email(ctx, gens)) goto err;
255                 } else {
256                         GENERAL_NAME *gen;
257                         if(!(gen = v2i_GENERAL_NAME(method, ctx, cnf)))
258                                                                  goto err; 
259                         sk_push(gens, (char *)gen);
260                 }
261         }
262         return gens;
263         err:
264         sk_pop_free(gens, GENERAL_NAME_free);
265         return NULL;
266 }
267
268 /* Copy any email addresses in a certificate or request to 
269  * GENERAL_NAMES
270  */
271
272 static int copy_email(ctx, gens)
273 X509V3_CTX *ctx;
274 STACK *gens;
275 {
276         X509_NAME *nm;
277         ASN1_IA5STRING *email = NULL;
278         X509_NAME_ENTRY *ne;
279         GENERAL_NAME *gen = NULL;
280         int i;
281         if(ctx->flags == CTX_TEST) return 1;
282         if(!ctx || (!ctx->subject_cert && !ctx->subject_req)) {
283                 X509V3err(X509V3_F_COPY_EMAIL,X509V3_R_NO_SUBJECT_DETAILS);
284                 goto err;
285         }
286         /* Find the subject name */
287         if(ctx->subject_cert) nm = X509_get_subject_name(ctx->subject_cert);
288         else nm = X509_REQ_get_subject_name(ctx->subject_req);
289
290         /* Now add any email address(es) to STACK */
291         i = -1;
292         while((i = X509_NAME_get_index_by_NID(nm,
293                                          NID_pkcs9_emailAddress, i)) > 0) {
294                 ne = X509_NAME_get_entry(nm, i);
295                 email = ASN1_IA5STRING_dup(X509_NAME_ENTRY_get_data(ne));
296                 if(!email || !(gen = GENERAL_NAME_new())) {
297                         X509V3err(X509V3_F_COPY_EMAIL,ERR_R_MALLOC_FAILURE);
298                         goto err;
299                 }
300                 gen->d.ia5 = email;
301                 email = NULL;
302                 gen->type = GEN_EMAIL;
303                 if(!sk_push(gens, (char *)gen)) {
304                         X509V3err(X509V3_F_COPY_EMAIL,ERR_R_MALLOC_FAILURE);
305                         goto err;
306                 }
307                 gen = NULL;
308         }
309
310         
311         return 1;
312                 
313         err:
314         GENERAL_NAME_free(gen);
315         ASN1_IA5STRING_free(email);
316         return 0;
317         
318 }
319
320 STACK *v2i_GENERAL_NAMES(method, ctx, nval)
321 X509V3_EXT_METHOD *method;
322 X509V3_CTX *ctx;
323 STACK *nval;
324 {
325         GENERAL_NAME *gen;
326         STACK *gens = NULL;
327         CONF_VALUE *cnf;
328         int i;
329         if(!(gens = sk_new(NULL))) {
330                 X509V3err(X509V3_F_V2I_GENERAL_NAMES,ERR_R_MALLOC_FAILURE);
331                 return NULL;
332         }
333         for(i = 0; i < sk_num(nval); i++) {
334                 cnf = (CONF_VALUE *)sk_value(nval, i);
335                 if(!(gen = v2i_GENERAL_NAME(method, ctx, cnf))) goto err; 
336                 sk_push(gens, (char *)gen);
337         }
338         return gens;
339         err:
340         sk_pop_free(gens, GENERAL_NAME_free);
341         return NULL;
342 }
343
344 GENERAL_NAME *v2i_GENERAL_NAME(method, ctx, cnf)
345 X509V3_EXT_METHOD *method;
346 X509V3_CTX *ctx;
347 CONF_VALUE *cnf;
348 {
349 char is_string = 0;
350 int type;
351 GENERAL_NAME *gen = NULL;
352
353 char *name, *value;
354
355 name = cnf->name;
356 value = cnf->value;
357
358 if(!value) {
359         X509V3err(X509V3_F_V2I_GENERAL_NAME,X509V3_R_MISSING_VALUE);
360         return NULL;
361 }
362
363 if(!(gen = GENERAL_NAME_new())) {
364         X509V3err(X509V3_F_V2I_GENERAL_NAME,ERR_R_MALLOC_FAILURE);
365         return NULL;
366 }
367
368 if(!name_cmp(name, "email")) {
369         is_string = 1;
370         type = GEN_EMAIL;
371 } else if(!name_cmp(name, "URI")) {
372         is_string = 1;
373         type = GEN_URI;
374 } else if(!name_cmp(name, "DNS")) {
375         is_string = 1;
376         type = GEN_DNS;
377 } else if(!name_cmp(name, "RID")) {
378         ASN1_OBJECT *obj;
379         if(!(obj = OBJ_txt2obj(value,0))) {
380                 X509V3err(X509V3_F_V2I_GENERAL_NAME,X509V3_R_BAD_OBJECT);
381                 ERR_add_error_data(2, "value=", value);
382                 goto err;
383         }
384         gen->d.rid = obj;
385         type = GEN_RID;
386 } else if(!name_cmp(name, "IP")) {
387         int i1,i2,i3,i4;
388         unsigned char ip[4];
389         if((sscanf(value, "%d.%d.%d.%d",&i1,&i2,&i3,&i4) != 4) ||
390             (i1 < 0) || (i1 > 255) || (i2 < 0) || (i2 > 255) ||
391             (i3 < 0) || (i3 > 255) || (i4 < 0) || (i4 > 255) ) {
392                 X509V3err(X509V3_F_V2I_GENERAL_NAME,X509V3_R_BAD_IP_ADDRESS);
393                 ERR_add_error_data(2, "value=", value);
394                 goto err;
395         }
396         ip[0] = i1; ip[1] = i2 ; ip[2] = i3 ; ip[3] = i4;
397         if(!(gen->d.ip = ASN1_OCTET_STRING_new()) ||
398                 !ASN1_STRING_set(gen->d.ip, ip, 4)) {
399                         X509V3err(X509V3_F_V2I_GENERAL_NAME,ERR_R_MALLOC_FAILURE);
400                         goto err;
401         }
402         type = GEN_IPADD;
403 } else {
404         X509V3err(X509V3_F_V2I_GENERAL_NAME,X509V3_R_UNSUPPORTED_OPTION);
405         ERR_add_error_data(2, "name=", name);
406         goto err;
407 }
408
409 if(is_string) {
410         if(!(gen->d.ia5 = ASN1_IA5STRING_new()) ||
411                       !ASN1_STRING_set(gen->d.ia5, value, strlen(value))) {
412                 X509V3err(X509V3_F_V2I_GENERAL_NAME,ERR_R_MALLOC_FAILURE);
413                 goto err;
414         }
415 }
416
417 gen->type = type;
418
419 return gen;
420
421 err:
422 GENERAL_NAME_free(gen);
423 return NULL;
424 }