]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/x509v3/v3_alt.c
Initial support for ASN1_ITEM_FUNCTION option to
[thirdparty/openssl.git] / crypto / x509v3 / v3_alt.c
CommitLineData
643aaa19 1/* v3_alt.c */
142fcca8
DSH
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>
e527ba09 60#include "cryptlib.h"
ec577822
BM
61#include <openssl/conf.h>
62#include <openssl/x509v3.h>
142fcca8 63
9d6b1ce6
DSH
64static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method, X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval);
65static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method, X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval);
66static int copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens);
67static int copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens);
142fcca8 68X509V3_EXT_METHOD v3_alt[] = {
bb5ea36b 69{ NID_subject_alt_name, 0, ASN1_ITEM_ref(GENERAL_NAMES),
2aff7727
DSH
700,0,0,0,
710,0,
142fcca8 72(X509V3_EXT_I2V)i2v_GENERAL_NAMES,
aa066b9e 73(X509V3_EXT_V2I)v2i_subject_alt,
79a474e8 74NULL, NULL, NULL},
2aff7727 75
bb5ea36b 76{ NID_issuer_alt_name, 0, ASN1_ITEM_ref(GENERAL_NAMES),
2aff7727
DSH
770,0,0,0,
780,0,
142fcca8 79(X509V3_EXT_I2V)i2v_GENERAL_NAMES,
aa066b9e 80(X509V3_EXT_V2I)v2i_issuer_alt,
79a474e8 81NULL, NULL, NULL},
142fcca8
DSH
82};
83
ba404b5e 84STACK_OF(CONF_VALUE) *i2v_GENERAL_NAMES(X509V3_EXT_METHOD *method,
9d6b1ce6 85 GENERAL_NAMES *gens, STACK_OF(CONF_VALUE) *ret)
142fcca8
DSH
86{
87 int i;
142fcca8 88 GENERAL_NAME *gen;
f5fedc04
DSH
89 for(i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
90 gen = sk_GENERAL_NAME_value(gens, i);
d08d8da4
DSH
91 ret = i2v_GENERAL_NAME(method, gen, ret);
92 }
ba404b5e 93 if(!ret) return sk_CONF_VALUE_new_null();
d08d8da4
DSH
94 return ret;
95}
96
ba404b5e
DSH
97STACK_OF(CONF_VALUE) *i2v_GENERAL_NAME(X509V3_EXT_METHOD *method,
98 GENERAL_NAME *gen, STACK_OF(CONF_VALUE) *ret)
d08d8da4 99{
d08d8da4 100 unsigned char *p;
6546fdfa 101 char oline[256];
d08d8da4
DSH
102 switch (gen->type)
103 {
104 case GEN_OTHERNAME:
142fcca8
DSH
105 X509V3_add_value("othername","<unsupported>", &ret);
106 break;
107
d08d8da4 108 case GEN_X400:
142fcca8
DSH
109 X509V3_add_value("X400Name","<unsupported>", &ret);
110 break;
111
d08d8da4 112 case GEN_EDIPARTY:
142fcca8 113 X509V3_add_value("EdiPartyName","<unsupported>", &ret);
d08d8da4 114 break;
142fcca8 115
d08d8da4 116 case GEN_EMAIL:
61f5b6f3 117 X509V3_add_value_uchar("email",gen->d.ia5->data, &ret);
142fcca8
DSH
118 break;
119
d08d8da4 120 case GEN_DNS:
61f5b6f3 121 X509V3_add_value_uchar("DNS",gen->d.ia5->data, &ret);
142fcca8
DSH
122 break;
123
d08d8da4 124 case GEN_URI:
61f5b6f3 125 X509V3_add_value_uchar("URI",gen->d.ia5->data, &ret);
d08d8da4 126 break;
142fcca8 127
d08d8da4 128 case GEN_DIRNAME:
175b0942
DSH
129 X509_NAME_oneline(gen->d.dirn, oline, 256);
130 X509V3_add_value("DirName",oline, &ret);
d08d8da4 131 break;
142fcca8 132
d08d8da4
DSH
133 case GEN_IPADD:
134 p = gen->d.ip->data;
135 /* BUG: doesn't support IPV6 */
136 if(gen->d.ip->length != 4) {
137 X509V3_add_value("IP Address","<invalid>", &ret);
138 break;
142fcca8 139 }
d08d8da4
DSH
140 sprintf(oline, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
141 X509V3_add_value("IP Address",oline, &ret);
142 break;
143
144 case GEN_RID:
145 i2t_ASN1_OBJECT(oline, 256, gen->d.rid);
146 X509V3_add_value("Registered ID",oline, &ret);
147 break;
142fcca8
DSH
148 }
149 return ret;
2c15d426
DSH
150}
151
152int GENERAL_NAME_print(BIO *out, GENERAL_NAME *gen)
153{
2c15d426
DSH
154 unsigned char *p;
155 switch (gen->type)
156 {
157 case GEN_OTHERNAME:
158 BIO_printf(out, "othername:<unsupported>");
159 break;
160
161 case GEN_X400:
162 BIO_printf(out, "X400Name:<unsupported>");
163 break;
164
165 case GEN_EDIPARTY:
166 /* Maybe fix this: it is supported now */
167 BIO_printf(out, "EdiPartyName:<unsupported>");
168 break;
169
170 case GEN_EMAIL:
171 BIO_printf(out, "email:%s",gen->d.ia5->data);
172 break;
173
174 case GEN_DNS:
175 BIO_printf(out, "DNS:%s",gen->d.ia5->data);
176 break;
177
178 case GEN_URI:
179 BIO_printf(out, "URI:%s",gen->d.ia5->data);
180 break;
181
182 case GEN_DIRNAME:
6546fdfa
DSH
183 BIO_printf(out, "DirName: ");
184 X509_NAME_print_ex(out, gen->d.dirn, 0, XN_FLAG_ONELINE);
2c15d426
DSH
185 break;
186
187 case GEN_IPADD:
188 p = gen->d.ip->data;
189 /* BUG: doesn't support IPV6 */
190 if(gen->d.ip->length != 4) {
191 BIO_printf(out,"IP Address:<invalid>");
192 break;
193 }
194 BIO_printf(out, "IP Address:%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
195 break;
196
197 case GEN_RID:
198 BIO_printf(out, "Registered ID");
199 i2a_ASN1_OBJECT(out, gen->d.rid);
200 break;
201 }
202 return 1;
142fcca8
DSH
203}
204
9d6b1ce6 205static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method,
66ab08b1 206 X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
aa066b9e 207{
9d6b1ce6 208 GENERAL_NAMES *gens = NULL;
aa066b9e
DSH
209 CONF_VALUE *cnf;
210 int i;
62324627 211 if(!(gens = sk_GENERAL_NAME_new_null())) {
aa066b9e
DSH
212 X509V3err(X509V3_F_V2I_GENERAL_NAMES,ERR_R_MALLOC_FAILURE);
213 return NULL;
214 }
66ab08b1
DSH
215 for(i = 0; i < sk_CONF_VALUE_num(nval); i++) {
216 cnf = sk_CONF_VALUE_value(nval, i);
aa066b9e
DSH
217 if(!name_cmp(cnf->name, "issuer") && cnf->value &&
218 !strcmp(cnf->value, "copy")) {
219 if(!copy_issuer(ctx, gens)) goto err;
220 } else {
221 GENERAL_NAME *gen;
222 if(!(gen = v2i_GENERAL_NAME(method, ctx, cnf)))
223 goto err;
f5fedc04 224 sk_GENERAL_NAME_push(gens, gen);
aa066b9e
DSH
225 }
226 }
227 return gens;
228 err:
f5fedc04 229 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
aa066b9e
DSH
230 return NULL;
231}
232
233/* Append subject altname of issuer to issuer alt name of subject */
234
9d6b1ce6 235static int copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens)
aa066b9e 236{
9d6b1ce6 237 GENERAL_NAMES *ialt;
f5fedc04 238 GENERAL_NAME *gen;
aa066b9e
DSH
239 X509_EXTENSION *ext;
240 int i;
241 if(ctx && (ctx->flags == CTX_TEST)) return 1;
242 if(!ctx || !ctx->issuer_cert) {
243 X509V3err(X509V3_F_COPY_ISSUER,X509V3_R_NO_ISSUER_DETAILS);
244 goto err;
245 }
246 i = X509_get_ext_by_NID(ctx->issuer_cert, NID_subject_alt_name, -1);
247 if(i < 0) return 1;
248 if(!(ext = X509_get_ext(ctx->issuer_cert, i)) ||
f5fedc04 249 !(ialt = X509V3_EXT_d2i(ext)) ) {
aa066b9e
DSH
250 X509V3err(X509V3_F_COPY_ISSUER,X509V3_R_ISSUER_DECODE_ERROR);
251 goto err;
252 }
253
f5fedc04
DSH
254 for(i = 0; i < sk_GENERAL_NAME_num(ialt); i++) {
255 gen = sk_GENERAL_NAME_value(ialt, i);
256 if(!sk_GENERAL_NAME_push(gens, gen)) {
aa066b9e
DSH
257 X509V3err(X509V3_F_COPY_ISSUER,ERR_R_MALLOC_FAILURE);
258 goto err;
259 }
260 }
f5fedc04 261 sk_GENERAL_NAME_free(ialt);
aa066b9e
DSH
262
263 return 1;
264
265 err:
266 return 0;
267
268}
269
9d6b1ce6 270static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method,
66ab08b1 271 X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
aa066b9e 272{
9d6b1ce6 273 GENERAL_NAMES *gens = NULL;
aa066b9e
DSH
274 CONF_VALUE *cnf;
275 int i;
62324627 276 if(!(gens = sk_GENERAL_NAME_new_null())) {
aa066b9e
DSH
277 X509V3err(X509V3_F_V2I_GENERAL_NAMES,ERR_R_MALLOC_FAILURE);
278 return NULL;
279 }
66ab08b1
DSH
280 for(i = 0; i < sk_CONF_VALUE_num(nval); i++) {
281 cnf = sk_CONF_VALUE_value(nval, i);
aa066b9e
DSH
282 if(!name_cmp(cnf->name, "email") && cnf->value &&
283 !strcmp(cnf->value, "copy")) {
284 if(!copy_email(ctx, gens)) goto err;
285 } else {
286 GENERAL_NAME *gen;
287 if(!(gen = v2i_GENERAL_NAME(method, ctx, cnf)))
288 goto err;
f5fedc04 289 sk_GENERAL_NAME_push(gens, gen);
aa066b9e
DSH
290 }
291 }
292 return gens;
293 err:
f5fedc04 294 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
aa066b9e
DSH
295 return NULL;
296}
297
298/* Copy any email addresses in a certificate or request to
299 * GENERAL_NAMES
300 */
301
9d6b1ce6 302static int copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens)
aa066b9e
DSH
303{
304 X509_NAME *nm;
305 ASN1_IA5STRING *email = NULL;
306 X509_NAME_ENTRY *ne;
307 GENERAL_NAME *gen = NULL;
308 int i;
309 if(ctx->flags == CTX_TEST) return 1;
310 if(!ctx || (!ctx->subject_cert && !ctx->subject_req)) {
311 X509V3err(X509V3_F_COPY_EMAIL,X509V3_R_NO_SUBJECT_DETAILS);
312 goto err;
313 }
314 /* Find the subject name */
315 if(ctx->subject_cert) nm = X509_get_subject_name(ctx->subject_cert);
316 else nm = X509_REQ_get_subject_name(ctx->subject_req);
317
318 /* Now add any email address(es) to STACK */
319 i = -1;
320 while((i = X509_NAME_get_index_by_NID(nm,
321 NID_pkcs9_emailAddress, i)) > 0) {
322 ne = X509_NAME_get_entry(nm, i);
08e9c1af 323 email = M_ASN1_IA5STRING_dup(X509_NAME_ENTRY_get_data(ne));
aa066b9e
DSH
324 if(!email || !(gen = GENERAL_NAME_new())) {
325 X509V3err(X509V3_F_COPY_EMAIL,ERR_R_MALLOC_FAILURE);
326 goto err;
327 }
328 gen->d.ia5 = email;
329 email = NULL;
330 gen->type = GEN_EMAIL;
f5fedc04 331 if(!sk_GENERAL_NAME_push(gens, gen)) {
aa066b9e
DSH
332 X509V3err(X509V3_F_COPY_EMAIL,ERR_R_MALLOC_FAILURE);
333 goto err;
334 }
335 gen = NULL;
336 }
337
338
339 return 1;
340
341 err:
342 GENERAL_NAME_free(gen);
08e9c1af 343 M_ASN1_IA5STRING_free(email);
aa066b9e
DSH
344 return 0;
345
346}
347
9d6b1ce6 348GENERAL_NAMES *v2i_GENERAL_NAMES(X509V3_EXT_METHOD *method,
ba404b5e 349 X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
d08d8da4
DSH
350{
351 GENERAL_NAME *gen;
9d6b1ce6 352 GENERAL_NAMES *gens = NULL;
d08d8da4
DSH
353 CONF_VALUE *cnf;
354 int i;
62324627 355 if(!(gens = sk_GENERAL_NAME_new_null())) {
d08d8da4
DSH
356 X509V3err(X509V3_F_V2I_GENERAL_NAMES,ERR_R_MALLOC_FAILURE);
357 return NULL;
358 }
ba404b5e
DSH
359 for(i = 0; i < sk_CONF_VALUE_num(nval); i++) {
360 cnf = sk_CONF_VALUE_value(nval, i);
d08d8da4 361 if(!(gen = v2i_GENERAL_NAME(method, ctx, cnf))) goto err;
f5fedc04 362 sk_GENERAL_NAME_push(gens, gen);
d08d8da4
DSH
363 }
364 return gens;
365 err:
f5fedc04 366 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
d08d8da4
DSH
367 return NULL;
368}
369
6b691a5c 370GENERAL_NAME *v2i_GENERAL_NAME(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
f5fedc04 371 CONF_VALUE *cnf)
d08d8da4
DSH
372{
373char is_string = 0;
374int type;
375GENERAL_NAME *gen = NULL;
376
377char *name, *value;
378
379name = cnf->name;
380value = cnf->value;
381
aa066b9e
DSH
382if(!value) {
383 X509V3err(X509V3_F_V2I_GENERAL_NAME,X509V3_R_MISSING_VALUE);
384 return NULL;
385}
386
d08d8da4
DSH
387if(!(gen = GENERAL_NAME_new())) {
388 X509V3err(X509V3_F_V2I_GENERAL_NAME,ERR_R_MALLOC_FAILURE);
389 return NULL;
390}
391
392if(!name_cmp(name, "email")) {
393 is_string = 1;
394 type = GEN_EMAIL;
395} else if(!name_cmp(name, "URI")) {
396 is_string = 1;
397 type = GEN_URI;
398} else if(!name_cmp(name, "DNS")) {
399 is_string = 1;
400 type = GEN_DNS;
401} else if(!name_cmp(name, "RID")) {
402 ASN1_OBJECT *obj;
403 if(!(obj = OBJ_txt2obj(value,0))) {
404 X509V3err(X509V3_F_V2I_GENERAL_NAME,X509V3_R_BAD_OBJECT);
405 ERR_add_error_data(2, "value=", value);
406 goto err;
407 }
408 gen->d.rid = obj;
409 type = GEN_RID;
410} else if(!name_cmp(name, "IP")) {
411 int i1,i2,i3,i4;
412 unsigned char ip[4];
413 if((sscanf(value, "%d.%d.%d.%d",&i1,&i2,&i3,&i4) != 4) ||
414 (i1 < 0) || (i1 > 255) || (i2 < 0) || (i2 > 255) ||
415 (i3 < 0) || (i3 > 255) || (i4 < 0) || (i4 > 255) ) {
416 X509V3err(X509V3_F_V2I_GENERAL_NAME,X509V3_R_BAD_IP_ADDRESS);
417 ERR_add_error_data(2, "value=", value);
418 goto err;
419 }
420 ip[0] = i1; ip[1] = i2 ; ip[2] = i3 ; ip[3] = i4;
08e9c1af 421 if(!(gen->d.ip = M_ASN1_OCTET_STRING_new()) ||
d08d8da4
DSH
422 !ASN1_STRING_set(gen->d.ip, ip, 4)) {
423 X509V3err(X509V3_F_V2I_GENERAL_NAME,ERR_R_MALLOC_FAILURE);
424 goto err;
425 }
426 type = GEN_IPADD;
427} else {
428 X509V3err(X509V3_F_V2I_GENERAL_NAME,X509V3_R_UNSUPPORTED_OPTION);
429 ERR_add_error_data(2, "name=", name);
430 goto err;
431}
432
433if(is_string) {
08e9c1af 434 if(!(gen->d.ia5 = M_ASN1_IA5STRING_new()) ||
95dc05bc
UM
435 !ASN1_STRING_set(gen->d.ia5, (unsigned char*)value,
436 strlen(value))) {
d08d8da4
DSH
437 X509V3err(X509V3_F_V2I_GENERAL_NAME,ERR_R_MALLOC_FAILURE);
438 goto err;
439 }
440}
441
442gen->type = type;
443
444return gen;
445
446err:
447GENERAL_NAME_free(gen);
448return NULL;
449}