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