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