Change functions to ANSI C.
[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, 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, NULL},
95 EXT_END
96 };
97
98 STACK *i2v_GENERAL_NAMES(X509V3_EXT_METHOD *method, STACK *gens, STACK *ret)
99 {
100         int i;
101         GENERAL_NAME *gen;
102         for(i = 0; i < sk_num(gens); i++) {
103                 gen = (GENERAL_NAME *)sk_value(gens, i);
104                 ret = i2v_GENERAL_NAME(method, gen, ret);
105         }
106         return ret;
107 }
108
109 STACK *i2v_GENERAL_NAME(X509V3_EXT_METHOD *method, GENERAL_NAME *gen,
110              STACK *ret)
111 {
112         char oline[256];
113         unsigned char *p;
114         switch (gen->type)
115         {
116                 case GEN_OTHERNAME:
117                 X509V3_add_value("othername","<unsupported>", &ret);
118                 break;
119
120                 case GEN_X400:
121                 X509V3_add_value("X400Name","<unsupported>", &ret);
122                 break;
123
124                 case GEN_EDIPARTY:
125                 X509V3_add_value("EdiPartyName","<unsupported>", &ret);
126                 break;
127
128                 case GEN_EMAIL:
129                 X509V3_add_value("email",gen->d.ia5->data, &ret);
130                 break;
131
132                 case GEN_DNS:
133                 X509V3_add_value("DNS",gen->d.ia5->data, &ret);
134                 break;
135
136                 case GEN_URI:
137                 X509V3_add_value("URI",gen->d.ia5->data, &ret);
138                 break;
139
140                 case GEN_DIRNAME:
141                 X509_NAME_oneline(gen->d.dirn, oline, 256);
142                 X509V3_add_value("DirName",oline, &ret);
143                 break;
144
145                 case GEN_IPADD:
146                 p = gen->d.ip->data;
147                 /* BUG: doesn't support IPV6 */
148                 if(gen->d.ip->length != 4) {
149                         X509V3_add_value("IP Address","<invalid>", &ret);
150                         break;
151                 }
152                 sprintf(oline, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
153                 X509V3_add_value("IP Address",oline, &ret);
154                 break;
155
156                 case GEN_RID:
157                 i2t_ASN1_OBJECT(oline, 256, gen->d.rid);
158                 X509V3_add_value("Registered ID",oline, &ret);
159                 break;
160         }
161         return ret;
162 }
163
164 static STACK *v2i_issuer_alt(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
165              STACK *nval)
166 {
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(!name_cmp(cnf->name, "issuer") && cnf->value &&
177                                                 !strcmp(cnf->value, "copy")) {
178                         if(!copy_issuer(ctx, gens)) goto err;
179                 } else {
180                         GENERAL_NAME *gen;
181                         if(!(gen = v2i_GENERAL_NAME(method, ctx, cnf)))
182                                                                  goto err; 
183                         sk_push(gens, (char *)gen);
184                 }
185         }
186         return gens;
187         err:
188         sk_pop_free(gens, GENERAL_NAME_free);
189         return NULL;
190 }
191
192 /* Append subject altname of issuer to issuer alt name of subject */
193
194 static int copy_issuer(X509V3_CTX *ctx, STACK *gens)
195 {
196         STACK *ialt;
197         char *gen;
198         X509_EXTENSION *ext;
199         int i;
200         if(ctx && (ctx->flags == CTX_TEST)) return 1;
201         if(!ctx || !ctx->issuer_cert) {
202                 X509V3err(X509V3_F_COPY_ISSUER,X509V3_R_NO_ISSUER_DETAILS);
203                 goto err;
204         }
205         i = X509_get_ext_by_NID(ctx->issuer_cert, NID_subject_alt_name, -1);
206         if(i < 0) return 1;
207         if(!(ext = X509_get_ext(ctx->issuer_cert, i)) ||
208                         !(ialt = (STACK *) X509V3_EXT_d2i(ext)) ) {
209                 X509V3err(X509V3_F_COPY_ISSUER,X509V3_R_ISSUER_DECODE_ERROR);
210                 goto err;
211         }
212
213         for(i = 0; i < sk_num(ialt); i++) {
214                 gen = sk_value(ialt, i);
215                 if(!sk_push(gens, gen)) {
216                         X509V3err(X509V3_F_COPY_ISSUER,ERR_R_MALLOC_FAILURE);
217                         goto err;
218                 }
219         }
220         sk_free(ialt);
221
222         return 1;
223                 
224         err:
225         return 0;
226         
227 }
228
229 static STACK *v2i_subject_alt(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
230              STACK *nval)
231 {
232         STACK *gens = NULL;
233         CONF_VALUE *cnf;
234         int i;
235         if(!(gens = sk_new(NULL))) {
236                 X509V3err(X509V3_F_V2I_GENERAL_NAMES,ERR_R_MALLOC_FAILURE);
237                 return NULL;
238         }
239         for(i = 0; i < sk_num(nval); i++) {
240                 cnf = (CONF_VALUE *)sk_value(nval, i);
241                 if(!name_cmp(cnf->name, "email") && cnf->value &&
242                                                 !strcmp(cnf->value, "copy")) {
243                         if(!copy_email(ctx, gens)) goto err;
244                 } else {
245                         GENERAL_NAME *gen;
246                         if(!(gen = v2i_GENERAL_NAME(method, ctx, cnf)))
247                                                                  goto err; 
248                         sk_push(gens, (char *)gen);
249                 }
250         }
251         return gens;
252         err:
253         sk_pop_free(gens, GENERAL_NAME_free);
254         return NULL;
255 }
256
257 /* Copy any email addresses in a certificate or request to 
258  * GENERAL_NAMES
259  */
260
261 static int copy_email(X509V3_CTX *ctx, STACK *gens)
262 {
263         X509_NAME *nm;
264         ASN1_IA5STRING *email = NULL;
265         X509_NAME_ENTRY *ne;
266         GENERAL_NAME *gen = NULL;
267         int i;
268         if(ctx->flags == CTX_TEST) return 1;
269         if(!ctx || (!ctx->subject_cert && !ctx->subject_req)) {
270                 X509V3err(X509V3_F_COPY_EMAIL,X509V3_R_NO_SUBJECT_DETAILS);
271                 goto err;
272         }
273         /* Find the subject name */
274         if(ctx->subject_cert) nm = X509_get_subject_name(ctx->subject_cert);
275         else nm = X509_REQ_get_subject_name(ctx->subject_req);
276
277         /* Now add any email address(es) to STACK */
278         i = -1;
279         while((i = X509_NAME_get_index_by_NID(nm,
280                                          NID_pkcs9_emailAddress, i)) > 0) {
281                 ne = X509_NAME_get_entry(nm, i);
282                 email = ASN1_IA5STRING_dup(X509_NAME_ENTRY_get_data(ne));
283                 if(!email || !(gen = GENERAL_NAME_new())) {
284                         X509V3err(X509V3_F_COPY_EMAIL,ERR_R_MALLOC_FAILURE);
285                         goto err;
286                 }
287                 gen->d.ia5 = email;
288                 email = NULL;
289                 gen->type = GEN_EMAIL;
290                 if(!sk_push(gens, (char *)gen)) {
291                         X509V3err(X509V3_F_COPY_EMAIL,ERR_R_MALLOC_FAILURE);
292                         goto err;
293                 }
294                 gen = NULL;
295         }
296
297         
298         return 1;
299                 
300         err:
301         GENERAL_NAME_free(gen);
302         ASN1_IA5STRING_free(email);
303         return 0;
304         
305 }
306
307 STACK *v2i_GENERAL_NAMES(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
308              STACK *nval)
309 {
310         GENERAL_NAME *gen;
311         STACK *gens = NULL;
312         CONF_VALUE *cnf;
313         int i;
314         if(!(gens = sk_new(NULL))) {
315                 X509V3err(X509V3_F_V2I_GENERAL_NAMES,ERR_R_MALLOC_FAILURE);
316                 return NULL;
317         }
318         for(i = 0; i < sk_num(nval); i++) {
319                 cnf = (CONF_VALUE *)sk_value(nval, i);
320                 if(!(gen = v2i_GENERAL_NAME(method, ctx, cnf))) goto err; 
321                 sk_push(gens, (char *)gen);
322         }
323         return gens;
324         err:
325         sk_pop_free(gens, GENERAL_NAME_free);
326         return NULL;
327 }
328
329 GENERAL_NAME *v2i_GENERAL_NAME(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
330              CONF_VALUE *cnf)
331 {
332 char is_string = 0;
333 int type;
334 GENERAL_NAME *gen = NULL;
335
336 char *name, *value;
337
338 name = cnf->name;
339 value = cnf->value;
340
341 if(!value) {
342         X509V3err(X509V3_F_V2I_GENERAL_NAME,X509V3_R_MISSING_VALUE);
343         return NULL;
344 }
345
346 if(!(gen = GENERAL_NAME_new())) {
347         X509V3err(X509V3_F_V2I_GENERAL_NAME,ERR_R_MALLOC_FAILURE);
348         return NULL;
349 }
350
351 if(!name_cmp(name, "email")) {
352         is_string = 1;
353         type = GEN_EMAIL;
354 } else if(!name_cmp(name, "URI")) {
355         is_string = 1;
356         type = GEN_URI;
357 } else if(!name_cmp(name, "DNS")) {
358         is_string = 1;
359         type = GEN_DNS;
360 } else if(!name_cmp(name, "RID")) {
361         ASN1_OBJECT *obj;
362         if(!(obj = OBJ_txt2obj(value,0))) {
363                 X509V3err(X509V3_F_V2I_GENERAL_NAME,X509V3_R_BAD_OBJECT);
364                 ERR_add_error_data(2, "value=", value);
365                 goto err;
366         }
367         gen->d.rid = obj;
368         type = GEN_RID;
369 } else if(!name_cmp(name, "IP")) {
370         int i1,i2,i3,i4;
371         unsigned char ip[4];
372         if((sscanf(value, "%d.%d.%d.%d",&i1,&i2,&i3,&i4) != 4) ||
373             (i1 < 0) || (i1 > 255) || (i2 < 0) || (i2 > 255) ||
374             (i3 < 0) || (i3 > 255) || (i4 < 0) || (i4 > 255) ) {
375                 X509V3err(X509V3_F_V2I_GENERAL_NAME,X509V3_R_BAD_IP_ADDRESS);
376                 ERR_add_error_data(2, "value=", value);
377                 goto err;
378         }
379         ip[0] = i1; ip[1] = i2 ; ip[2] = i3 ; ip[3] = i4;
380         if(!(gen->d.ip = ASN1_OCTET_STRING_new()) ||
381                 !ASN1_STRING_set(gen->d.ip, ip, 4)) {
382                         X509V3err(X509V3_F_V2I_GENERAL_NAME,ERR_R_MALLOC_FAILURE);
383                         goto err;
384         }
385         type = GEN_IPADD;
386 } else {
387         X509V3err(X509V3_F_V2I_GENERAL_NAME,X509V3_R_UNSUPPORTED_OPTION);
388         ERR_add_error_data(2, "name=", name);
389         goto err;
390 }
391
392 if(is_string) {
393         if(!(gen->d.ia5 = ASN1_IA5STRING_new()) ||
394                       !ASN1_STRING_set(gen->d.ia5, value, strlen(value))) {
395                 X509V3err(X509V3_F_V2I_GENERAL_NAME,ERR_R_MALLOC_FAILURE);
396                 goto err;
397         }
398 }
399
400 gen->type = type;
401
402 return gen;
403
404 err:
405 GENERAL_NAME_free(gen);
406 return NULL;
407 }