/src/libressl/crypto/x509/x509_alt.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD: x509_alt.c,v 1.12 2022/03/26 16:34:21 tb Exp $ */ |
2 | | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL |
3 | | * project. |
4 | | */ |
5 | | /* ==================================================================== |
6 | | * Copyright (c) 1999-2003 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 <string.h> |
61 | | |
62 | | #include <openssl/conf.h> |
63 | | #include <openssl/err.h> |
64 | | #include <openssl/x509v3.h> |
65 | | |
66 | | #include "x509_internal.h" |
67 | | |
68 | | static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method, |
69 | | X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval); |
70 | | static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method, |
71 | | X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval); |
72 | | static int copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p); |
73 | | static int copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens); |
74 | | static int do_othername(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx); |
75 | | static int do_dirname(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx); |
76 | | |
77 | | const X509V3_EXT_METHOD v3_alt[] = { |
78 | | { |
79 | | .ext_nid = NID_subject_alt_name, |
80 | | .ext_flags = 0, |
81 | | .it = &GENERAL_NAMES_it, |
82 | | .ext_new = NULL, |
83 | | .ext_free = NULL, |
84 | | .d2i = NULL, |
85 | | .i2d = NULL, |
86 | | .i2s = NULL, |
87 | | .s2i = NULL, |
88 | | .i2v = (X509V3_EXT_I2V)i2v_GENERAL_NAMES, |
89 | | .v2i = (X509V3_EXT_V2I)v2i_subject_alt, |
90 | | .i2r = NULL, |
91 | | .r2i = NULL, |
92 | | .usr_data = NULL, |
93 | | }, |
94 | | { |
95 | | .ext_nid = NID_issuer_alt_name, |
96 | | .ext_flags = 0, |
97 | | .it = &GENERAL_NAMES_it, |
98 | | .ext_new = NULL, |
99 | | .ext_free = NULL, |
100 | | .d2i = NULL, |
101 | | .i2d = NULL, |
102 | | .i2s = NULL, |
103 | | .s2i = NULL, |
104 | | .i2v = (X509V3_EXT_I2V)i2v_GENERAL_NAMES, |
105 | | .v2i = (X509V3_EXT_V2I)v2i_issuer_alt, |
106 | | .i2r = NULL, |
107 | | .r2i = NULL, |
108 | | .usr_data = NULL, |
109 | | }, |
110 | | { |
111 | | .ext_nid = NID_certificate_issuer, |
112 | | .ext_flags = 0, |
113 | | .it = &GENERAL_NAMES_it, |
114 | | .ext_new = NULL, |
115 | | .ext_free = NULL, |
116 | | .d2i = NULL, |
117 | | .i2d = NULL, |
118 | | .i2s = NULL, |
119 | | .s2i = NULL, |
120 | | .i2v = (X509V3_EXT_I2V)i2v_GENERAL_NAMES, |
121 | | .v2i = NULL, |
122 | | .i2r = NULL, |
123 | | .r2i = NULL, |
124 | | .usr_data = NULL, |
125 | | }, |
126 | | }; |
127 | | |
128 | | STACK_OF(CONF_VALUE) * |
129 | | i2v_GENERAL_NAMES(X509V3_EXT_METHOD *method, GENERAL_NAMES *gens, |
130 | | STACK_OF(CONF_VALUE) *ret) |
131 | 0 | { |
132 | 0 | STACK_OF(CONF_VALUE) *free_ret = NULL; |
133 | 0 | GENERAL_NAME *gen; |
134 | 0 | int i; |
135 | |
|
136 | 0 | if (ret == NULL) { |
137 | 0 | if ((free_ret = ret = sk_CONF_VALUE_new_null()) == NULL) |
138 | 0 | return NULL; |
139 | 0 | } |
140 | | |
141 | 0 | for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { |
142 | 0 | if ((gen = sk_GENERAL_NAME_value(gens, i)) == NULL) |
143 | 0 | goto err; |
144 | 0 | if ((ret = i2v_GENERAL_NAME(method, gen, ret)) == NULL) |
145 | 0 | goto err; |
146 | 0 | } |
147 | | |
148 | 0 | return ret; |
149 | | |
150 | 0 | err: |
151 | 0 | sk_CONF_VALUE_pop_free(free_ret, X509V3_conf_free); |
152 | |
|
153 | 0 | return NULL; |
154 | 0 | } |
155 | | |
156 | | STACK_OF(CONF_VALUE) * |
157 | | i2v_GENERAL_NAME(X509V3_EXT_METHOD *method, GENERAL_NAME *gen, |
158 | | STACK_OF(CONF_VALUE) *ret) |
159 | 0 | { |
160 | 0 | STACK_OF(CONF_VALUE) *free_ret = NULL; |
161 | 0 | unsigned char *p; |
162 | 0 | char oline[256], htmp[5]; |
163 | 0 | int i; |
164 | |
|
165 | 0 | if (ret == NULL) { |
166 | 0 | if ((free_ret = ret = sk_CONF_VALUE_new_null()) == NULL) |
167 | 0 | return NULL; |
168 | 0 | } |
169 | | |
170 | 0 | switch (gen->type) { |
171 | 0 | case GEN_OTHERNAME: |
172 | 0 | if (!X509V3_add_value("othername", "<unsupported>", &ret)) |
173 | 0 | goto err; |
174 | 0 | break; |
175 | | |
176 | 0 | case GEN_X400: |
177 | 0 | if (!X509V3_add_value("X400Name", "<unsupported>", &ret)) |
178 | 0 | goto err; |
179 | 0 | break; |
180 | | |
181 | 0 | case GEN_EDIPARTY: |
182 | 0 | if (!X509V3_add_value("EdiPartyName", "<unsupported>", &ret)) |
183 | 0 | goto err; |
184 | 0 | break; |
185 | | |
186 | 0 | case GEN_EMAIL: |
187 | 0 | if (!X509V3_add_value_uchar("email", gen->d.ia5->data, &ret)) |
188 | 0 | goto err; |
189 | 0 | break; |
190 | | |
191 | 0 | case GEN_DNS: |
192 | 0 | if (!X509V3_add_value_uchar("DNS", gen->d.ia5->data, &ret)) |
193 | 0 | goto err; |
194 | 0 | break; |
195 | | |
196 | 0 | case GEN_URI: |
197 | 0 | if (!X509V3_add_value_uchar("URI", gen->d.ia5->data, &ret)) |
198 | 0 | goto err; |
199 | 0 | break; |
200 | | |
201 | 0 | case GEN_DIRNAME: |
202 | 0 | if (X509_NAME_oneline(gen->d.dirn, oline, 256) == NULL) |
203 | 0 | goto err; |
204 | 0 | if (!X509V3_add_value("DirName", oline, &ret)) |
205 | 0 | goto err; |
206 | 0 | break; |
207 | | |
208 | 0 | case GEN_IPADD: /* XXX */ |
209 | 0 | p = gen->d.ip->data; |
210 | 0 | if (gen->d.ip->length == 4) |
211 | 0 | (void) snprintf(oline, sizeof oline, |
212 | 0 | "%d.%d.%d.%d", p[0], p[1], p[2], p[3]); |
213 | 0 | else if (gen->d.ip->length == 16) { |
214 | 0 | oline[0] = 0; |
215 | 0 | for (i = 0; i < 8; i++) { |
216 | 0 | (void) snprintf(htmp, sizeof htmp, |
217 | 0 | "%X", p[0] << 8 | p[1]); |
218 | 0 | p += 2; |
219 | 0 | strlcat(oline, htmp, sizeof(oline)); |
220 | 0 | if (i != 7) |
221 | 0 | strlcat(oline, ":", sizeof(oline)); |
222 | 0 | } |
223 | 0 | } else { |
224 | 0 | if (!X509V3_add_value("IP Address", "<invalid>", &ret)) |
225 | 0 | goto err; |
226 | 0 | break; |
227 | 0 | } |
228 | 0 | if (!X509V3_add_value("IP Address", oline, &ret)) |
229 | 0 | goto err; |
230 | 0 | break; |
231 | | |
232 | 0 | case GEN_RID: |
233 | 0 | if (!i2t_ASN1_OBJECT(oline, 256, gen->d.rid)) |
234 | 0 | goto err; |
235 | 0 | if (!X509V3_add_value("Registered ID", oline, &ret)) |
236 | 0 | goto err; |
237 | 0 | break; |
238 | 0 | } |
239 | | |
240 | 0 | return ret; |
241 | | |
242 | 0 | err: |
243 | 0 | sk_CONF_VALUE_pop_free(free_ret, X509V3_conf_free); |
244 | |
|
245 | 0 | return NULL; |
246 | 0 | } |
247 | | |
248 | | int |
249 | | GENERAL_NAME_print(BIO *out, GENERAL_NAME *gen) |
250 | 0 | { |
251 | 0 | unsigned char *p; |
252 | 0 | int i; |
253 | |
|
254 | 0 | switch (gen->type) { |
255 | 0 | case GEN_OTHERNAME: |
256 | 0 | BIO_printf(out, "othername:<unsupported>"); |
257 | 0 | break; |
258 | | |
259 | 0 | case GEN_X400: |
260 | 0 | BIO_printf(out, "X400Name:<unsupported>"); |
261 | 0 | break; |
262 | | |
263 | 0 | case GEN_EDIPARTY: |
264 | | /* Maybe fix this: it is supported now */ |
265 | 0 | BIO_printf(out, "EdiPartyName:<unsupported>"); |
266 | 0 | break; |
267 | | |
268 | 0 | case GEN_EMAIL: |
269 | 0 | BIO_printf(out, "email:%.*s", gen->d.ia5->length, |
270 | 0 | gen->d.ia5->data); |
271 | 0 | break; |
272 | | |
273 | 0 | case GEN_DNS: |
274 | 0 | BIO_printf(out, "DNS:%.*s", gen->d.ia5->length, |
275 | 0 | gen->d.ia5->data); |
276 | 0 | break; |
277 | | |
278 | 0 | case GEN_URI: |
279 | 0 | BIO_printf(out, "URI:%.*s", gen->d.ia5->length, |
280 | 0 | gen->d.ia5->data); |
281 | 0 | break; |
282 | | |
283 | 0 | case GEN_DIRNAME: |
284 | 0 | BIO_printf(out, "DirName: "); |
285 | 0 | X509_NAME_print_ex(out, gen->d.dirn, 0, XN_FLAG_ONELINE); |
286 | 0 | break; |
287 | | |
288 | 0 | case GEN_IPADD: |
289 | 0 | p = gen->d.ip->data; |
290 | 0 | if (gen->d.ip->length == 4) |
291 | 0 | BIO_printf(out, "IP Address:%d.%d.%d.%d", |
292 | 0 | p[0], p[1], p[2], p[3]); |
293 | 0 | else if (gen->d.ip->length == 16) { |
294 | 0 | BIO_printf(out, "IP Address"); |
295 | 0 | for (i = 0; i < 8; i++) { |
296 | 0 | BIO_printf(out, ":%X", p[0] << 8 | p[1]); |
297 | 0 | p += 2; |
298 | 0 | } |
299 | 0 | BIO_puts(out, "\n"); |
300 | 0 | } else { |
301 | 0 | BIO_printf(out, "IP Address:<invalid>"); |
302 | 0 | break; |
303 | 0 | } |
304 | 0 | break; |
305 | | |
306 | 0 | case GEN_RID: |
307 | 0 | BIO_printf(out, "Registered ID"); |
308 | 0 | i2a_ASN1_OBJECT(out, gen->d.rid); |
309 | 0 | break; |
310 | 0 | } |
311 | 0 | return 1; |
312 | 0 | } |
313 | | |
314 | | static GENERAL_NAMES * |
315 | | v2i_issuer_alt(X509V3_EXT_METHOD *method, X509V3_CTX *ctx, |
316 | | STACK_OF(CONF_VALUE) *nval) |
317 | 0 | { |
318 | 0 | GENERAL_NAMES *gens = NULL; |
319 | 0 | CONF_VALUE *cnf; |
320 | 0 | int i; |
321 | |
|
322 | 0 | if ((gens = sk_GENERAL_NAME_new_null()) == NULL) { |
323 | 0 | X509V3error(ERR_R_MALLOC_FAILURE); |
324 | 0 | return NULL; |
325 | 0 | } |
326 | 0 | for (i = 0; i < sk_CONF_VALUE_num(nval); i++) { |
327 | 0 | cnf = sk_CONF_VALUE_value(nval, i); |
328 | 0 | if (name_cmp(cnf->name, "issuer") == 0 && cnf->value != NULL && |
329 | 0 | strcmp(cnf->value, "copy") == 0) { |
330 | 0 | if (!copy_issuer(ctx, gens)) |
331 | 0 | goto err; |
332 | 0 | } else { |
333 | 0 | GENERAL_NAME *gen; |
334 | 0 | if ((gen = v2i_GENERAL_NAME(method, ctx, cnf)) == NULL) |
335 | 0 | goto err; |
336 | 0 | if (sk_GENERAL_NAME_push(gens, gen) == 0) { |
337 | 0 | GENERAL_NAME_free(gen); |
338 | 0 | goto err; |
339 | 0 | } |
340 | 0 | } |
341 | 0 | } |
342 | 0 | return gens; |
343 | | |
344 | 0 | err: |
345 | 0 | sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); |
346 | 0 | return NULL; |
347 | 0 | } |
348 | | |
349 | | /* Append subject altname of issuer to issuer alt name of subject */ |
350 | | |
351 | | static int |
352 | | copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens) |
353 | 0 | { |
354 | 0 | GENERAL_NAMES *ialt; |
355 | 0 | GENERAL_NAME *gen; |
356 | 0 | X509_EXTENSION *ext; |
357 | 0 | int i; |
358 | |
|
359 | 0 | if (ctx && (ctx->flags == CTX_TEST)) |
360 | 0 | return 1; |
361 | 0 | if (!ctx || !ctx->issuer_cert) { |
362 | 0 | X509V3error(X509V3_R_NO_ISSUER_DETAILS); |
363 | 0 | goto err; |
364 | 0 | } |
365 | 0 | i = X509_get_ext_by_NID(ctx->issuer_cert, NID_subject_alt_name, -1); |
366 | 0 | if (i < 0) |
367 | 0 | return 1; |
368 | 0 | if (!(ext = X509_get_ext(ctx->issuer_cert, i)) || |
369 | 0 | !(ialt = X509V3_EXT_d2i(ext))) { |
370 | 0 | X509V3error(X509V3_R_ISSUER_DECODE_ERROR); |
371 | 0 | goto err; |
372 | 0 | } |
373 | | |
374 | 0 | for (i = 0; i < sk_GENERAL_NAME_num(ialt); i++) { |
375 | 0 | gen = sk_GENERAL_NAME_value(ialt, i); |
376 | 0 | if (!sk_GENERAL_NAME_push(gens, gen)) { |
377 | 0 | X509V3error(ERR_R_MALLOC_FAILURE); |
378 | 0 | goto err; |
379 | 0 | } |
380 | 0 | } |
381 | 0 | sk_GENERAL_NAME_free(ialt); |
382 | |
|
383 | 0 | return 1; |
384 | | |
385 | 0 | err: |
386 | 0 | return 0; |
387 | |
|
388 | 0 | } |
389 | | |
390 | | static GENERAL_NAMES * |
391 | | v2i_subject_alt(X509V3_EXT_METHOD *method, X509V3_CTX *ctx, |
392 | | STACK_OF(CONF_VALUE) *nval) |
393 | 0 | { |
394 | 0 | GENERAL_NAMES *gens = NULL; |
395 | 0 | CONF_VALUE *cnf; |
396 | 0 | int i; |
397 | |
|
398 | 0 | if (!(gens = sk_GENERAL_NAME_new_null())) { |
399 | 0 | X509V3error(ERR_R_MALLOC_FAILURE); |
400 | 0 | return NULL; |
401 | 0 | } |
402 | 0 | for (i = 0; i < sk_CONF_VALUE_num(nval); i++) { |
403 | 0 | cnf = sk_CONF_VALUE_value(nval, i); |
404 | 0 | if (!name_cmp(cnf->name, "email") && cnf->value && |
405 | 0 | !strcmp(cnf->value, "copy")) { |
406 | 0 | if (!copy_email(ctx, gens, 0)) |
407 | 0 | goto err; |
408 | 0 | } else if (!name_cmp(cnf->name, "email") && cnf->value && |
409 | 0 | !strcmp(cnf->value, "move")) { |
410 | 0 | if (!copy_email(ctx, gens, 1)) |
411 | 0 | goto err; |
412 | 0 | } else { |
413 | 0 | GENERAL_NAME *gen; |
414 | 0 | if (!(gen = v2i_GENERAL_NAME(method, ctx, cnf))) |
415 | 0 | goto err; |
416 | 0 | if (sk_GENERAL_NAME_push(gens, gen) == 0) { |
417 | 0 | GENERAL_NAME_free(gen); |
418 | 0 | goto err; |
419 | 0 | } |
420 | 0 | } |
421 | 0 | } |
422 | 0 | return gens; |
423 | | |
424 | 0 | err: |
425 | 0 | sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); |
426 | 0 | return NULL; |
427 | 0 | } |
428 | | |
429 | | /* Copy any email addresses in a certificate or request to |
430 | | * GENERAL_NAMES |
431 | | */ |
432 | | |
433 | | static int |
434 | | copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p) |
435 | 0 | { |
436 | 0 | X509_NAME *nm; |
437 | 0 | ASN1_IA5STRING *email = NULL; |
438 | 0 | X509_NAME_ENTRY *ne; |
439 | 0 | GENERAL_NAME *gen = NULL; |
440 | 0 | int i; |
441 | |
|
442 | 0 | if (ctx != NULL && ctx->flags == CTX_TEST) |
443 | 0 | return 1; |
444 | 0 | if (!ctx || (!ctx->subject_cert && !ctx->subject_req)) { |
445 | 0 | X509V3error(X509V3_R_NO_SUBJECT_DETAILS); |
446 | 0 | goto err; |
447 | 0 | } |
448 | | /* Find the subject name */ |
449 | 0 | if (ctx->subject_cert) |
450 | 0 | nm = X509_get_subject_name(ctx->subject_cert); |
451 | 0 | else |
452 | 0 | nm = X509_REQ_get_subject_name(ctx->subject_req); |
453 | | |
454 | | /* Now add any email address(es) to STACK */ |
455 | 0 | i = -1; |
456 | 0 | while ((i = X509_NAME_get_index_by_NID(nm, |
457 | 0 | NID_pkcs9_emailAddress, i)) >= 0) { |
458 | 0 | ne = X509_NAME_get_entry(nm, i); |
459 | 0 | email = ASN1_STRING_dup(X509_NAME_ENTRY_get_data(ne)); |
460 | 0 | if (move_p) { |
461 | 0 | X509_NAME_delete_entry(nm, i); |
462 | 0 | X509_NAME_ENTRY_free(ne); |
463 | 0 | i--; |
464 | 0 | } |
465 | 0 | if (!email || !(gen = GENERAL_NAME_new())) { |
466 | 0 | X509V3error(ERR_R_MALLOC_FAILURE); |
467 | 0 | goto err; |
468 | 0 | } |
469 | 0 | gen->d.ia5 = email; |
470 | 0 | email = NULL; |
471 | 0 | gen->type = GEN_EMAIL; |
472 | 0 | if (!sk_GENERAL_NAME_push(gens, gen)) { |
473 | 0 | X509V3error(ERR_R_MALLOC_FAILURE); |
474 | 0 | goto err; |
475 | 0 | } |
476 | 0 | gen = NULL; |
477 | 0 | } |
478 | | |
479 | 0 | return 1; |
480 | | |
481 | 0 | err: |
482 | 0 | GENERAL_NAME_free(gen); |
483 | 0 | ASN1_IA5STRING_free(email); |
484 | 0 | return 0; |
485 | 0 | } |
486 | | |
487 | | GENERAL_NAMES * |
488 | | v2i_GENERAL_NAMES(const X509V3_EXT_METHOD *method, X509V3_CTX *ctx, |
489 | | STACK_OF(CONF_VALUE) *nval) |
490 | 0 | { |
491 | 0 | GENERAL_NAME *gen; |
492 | 0 | GENERAL_NAMES *gens = NULL; |
493 | 0 | CONF_VALUE *cnf; |
494 | 0 | int i; |
495 | |
|
496 | 0 | if (!(gens = sk_GENERAL_NAME_new_null())) { |
497 | 0 | X509V3error(ERR_R_MALLOC_FAILURE); |
498 | 0 | return NULL; |
499 | 0 | } |
500 | 0 | for (i = 0; i < sk_CONF_VALUE_num(nval); i++) { |
501 | 0 | cnf = sk_CONF_VALUE_value(nval, i); |
502 | 0 | if (!(gen = v2i_GENERAL_NAME(method, ctx, cnf))) |
503 | 0 | goto err; |
504 | 0 | if (sk_GENERAL_NAME_push(gens, gen) == 0) { |
505 | 0 | GENERAL_NAME_free(gen); |
506 | 0 | goto err; |
507 | 0 | } |
508 | 0 | } |
509 | 0 | return gens; |
510 | | |
511 | 0 | err: |
512 | 0 | sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); |
513 | 0 | return NULL; |
514 | 0 | } |
515 | | |
516 | | GENERAL_NAME * |
517 | | v2i_GENERAL_NAME(const X509V3_EXT_METHOD *method, X509V3_CTX *ctx, |
518 | | CONF_VALUE *cnf) |
519 | 0 | { |
520 | 0 | return v2i_GENERAL_NAME_ex(NULL, method, ctx, cnf, 0); |
521 | 0 | } |
522 | | |
523 | | GENERAL_NAME * |
524 | | a2i_GENERAL_NAME(GENERAL_NAME *out, const X509V3_EXT_METHOD *method, |
525 | | X509V3_CTX *ctx, int gen_type, const char *value, int is_nc) |
526 | 0 | { |
527 | 0 | char is_string = 0; |
528 | 0 | GENERAL_NAME *gen = NULL; |
529 | |
|
530 | 0 | if (!value) { |
531 | 0 | X509V3error(X509V3_R_MISSING_VALUE); |
532 | 0 | return NULL; |
533 | 0 | } |
534 | | |
535 | 0 | if (out) |
536 | 0 | gen = out; |
537 | 0 | else { |
538 | 0 | gen = GENERAL_NAME_new(); |
539 | 0 | if (gen == NULL) { |
540 | 0 | X509V3error(ERR_R_MALLOC_FAILURE); |
541 | 0 | return NULL; |
542 | 0 | } |
543 | 0 | } |
544 | | |
545 | 0 | switch (gen_type) { |
546 | 0 | case GEN_URI: |
547 | 0 | case GEN_EMAIL: |
548 | 0 | case GEN_DNS: |
549 | 0 | is_string = 1; |
550 | 0 | break; |
551 | | |
552 | 0 | case GEN_RID: |
553 | 0 | { |
554 | 0 | ASN1_OBJECT *obj; |
555 | 0 | if (!(obj = OBJ_txt2obj(value, 0))) { |
556 | 0 | X509V3error(X509V3_R_BAD_OBJECT); |
557 | 0 | ERR_asprintf_error_data("value=%s", value); |
558 | 0 | goto err; |
559 | 0 | } |
560 | 0 | gen->d.rid = obj; |
561 | 0 | } |
562 | 0 | break; |
563 | | |
564 | 0 | case GEN_IPADD: |
565 | 0 | if (is_nc) |
566 | 0 | gen->d.ip = a2i_IPADDRESS_NC(value); |
567 | 0 | else |
568 | 0 | gen->d.ip = a2i_IPADDRESS(value); |
569 | 0 | if (gen->d.ip == NULL) { |
570 | 0 | X509V3error(X509V3_R_BAD_IP_ADDRESS); |
571 | 0 | ERR_asprintf_error_data("value=%s", value); |
572 | 0 | goto err; |
573 | 0 | } |
574 | 0 | break; |
575 | | |
576 | 0 | case GEN_DIRNAME: |
577 | 0 | if (!do_dirname(gen, value, ctx)) { |
578 | 0 | X509V3error(X509V3_R_DIRNAME_ERROR); |
579 | 0 | goto err; |
580 | 0 | } |
581 | 0 | break; |
582 | | |
583 | 0 | case GEN_OTHERNAME: |
584 | 0 | if (!do_othername(gen, value, ctx)) { |
585 | 0 | X509V3error(X509V3_R_OTHERNAME_ERROR); |
586 | 0 | goto err; |
587 | 0 | } |
588 | 0 | break; |
589 | | |
590 | 0 | default: |
591 | 0 | X509V3error(X509V3_R_UNSUPPORTED_TYPE); |
592 | 0 | goto err; |
593 | 0 | } |
594 | | |
595 | 0 | if (is_string) { |
596 | 0 | if (!(gen->d.ia5 = ASN1_IA5STRING_new()) || |
597 | 0 | !ASN1_STRING_set(gen->d.ia5, value, strlen(value))) { |
598 | 0 | X509V3error(ERR_R_MALLOC_FAILURE); |
599 | 0 | goto err; |
600 | 0 | } |
601 | 0 | } |
602 | | |
603 | 0 | gen->type = gen_type; |
604 | |
|
605 | 0 | return gen; |
606 | | |
607 | 0 | err: |
608 | 0 | if (out == NULL) |
609 | 0 | GENERAL_NAME_free(gen); |
610 | 0 | return NULL; |
611 | 0 | } |
612 | | |
613 | | GENERAL_NAME * |
614 | | v2i_GENERAL_NAME_ex(GENERAL_NAME *out, const X509V3_EXT_METHOD *method, |
615 | | X509V3_CTX *ctx, CONF_VALUE *cnf, int is_nc) |
616 | 0 | { |
617 | 0 | uint8_t *bytes = NULL; |
618 | 0 | char *name, *value; |
619 | 0 | GENERAL_NAME *ret; |
620 | 0 | size_t len = 0; |
621 | 0 | int type; |
622 | |
|
623 | 0 | name = cnf->name; |
624 | 0 | value = cnf->value; |
625 | |
|
626 | 0 | if (!value) { |
627 | 0 | X509V3error(X509V3_R_MISSING_VALUE); |
628 | 0 | return NULL; |
629 | 0 | } |
630 | | |
631 | 0 | if (!name_cmp(name, "email")) |
632 | 0 | type = GEN_EMAIL; |
633 | 0 | else if (!name_cmp(name, "URI")) |
634 | 0 | type = GEN_URI; |
635 | 0 | else if (!name_cmp(name, "DNS")) |
636 | 0 | type = GEN_DNS; |
637 | 0 | else if (!name_cmp(name, "RID")) |
638 | 0 | type = GEN_RID; |
639 | 0 | else if (!name_cmp(name, "IP")) |
640 | 0 | type = GEN_IPADD; |
641 | 0 | else if (!name_cmp(name, "dirName")) |
642 | 0 | type = GEN_DIRNAME; |
643 | 0 | else if (!name_cmp(name, "otherName")) |
644 | 0 | type = GEN_OTHERNAME; |
645 | 0 | else { |
646 | 0 | X509V3error(X509V3_R_UNSUPPORTED_OPTION); |
647 | 0 | ERR_asprintf_error_data("name=%s", name); |
648 | 0 | return NULL; |
649 | 0 | } |
650 | | |
651 | 0 | ret = a2i_GENERAL_NAME(out, method, ctx, type, value, is_nc); |
652 | 0 | if (ret == NULL) |
653 | 0 | return NULL; |
654 | | |
655 | | /* |
656 | | * Validate what we have for sanity. |
657 | | */ |
658 | | |
659 | 0 | if (is_nc) { |
660 | 0 | struct x509_constraints_name *constraints_name = NULL; |
661 | |
|
662 | 0 | if (!x509_constraints_validate(ret, &constraints_name, NULL)) { |
663 | 0 | X509V3error(X509V3_R_BAD_OBJECT); |
664 | 0 | ERR_asprintf_error_data("name=%s", name); |
665 | 0 | goto err; |
666 | 0 | } |
667 | 0 | x509_constraints_name_free(constraints_name); |
668 | 0 | return ret; |
669 | 0 | } |
670 | | |
671 | 0 | type = x509_constraints_general_to_bytes(ret, &bytes, &len); |
672 | 0 | switch (type) { |
673 | 0 | case GEN_DNS: |
674 | 0 | if (!x509_constraints_valid_sandns(bytes, len)) { |
675 | 0 | X509V3error(X509V3_R_BAD_OBJECT); |
676 | 0 | ERR_asprintf_error_data("name=%s value='%.*s'", name, |
677 | 0 | (int)len, bytes); |
678 | 0 | goto err; |
679 | 0 | } |
680 | 0 | break; |
681 | 0 | case GEN_URI: |
682 | 0 | if (!x509_constraints_uri_host(bytes, len, NULL)) { |
683 | 0 | X509V3error(X509V3_R_BAD_OBJECT); |
684 | 0 | ERR_asprintf_error_data("name=%s value='%.*s'", name, |
685 | 0 | (int)len, bytes); |
686 | 0 | goto err; |
687 | 0 | } |
688 | 0 | break; |
689 | 0 | case GEN_EMAIL: |
690 | 0 | if (!x509_constraints_parse_mailbox(bytes, len, NULL)) { |
691 | 0 | X509V3error(X509V3_R_BAD_OBJECT); |
692 | 0 | ERR_asprintf_error_data("name=%s value='%.*s'", name, |
693 | 0 | (int)len, bytes); |
694 | 0 | goto err; |
695 | 0 | } |
696 | 0 | break; |
697 | 0 | case GEN_IPADD: |
698 | 0 | if (len != 4 && len != 16) { |
699 | 0 | X509V3error(X509V3_R_BAD_IP_ADDRESS); |
700 | 0 | ERR_asprintf_error_data("name=%s len=%zu", name, len); |
701 | 0 | goto err; |
702 | 0 | } |
703 | 0 | break; |
704 | 0 | default: |
705 | 0 | break; |
706 | 0 | } |
707 | 0 | return ret; |
708 | 0 | err: |
709 | 0 | if (out == NULL) |
710 | 0 | GENERAL_NAME_free(ret); |
711 | 0 | return NULL; |
712 | 0 | } |
713 | | |
714 | | static int |
715 | | do_othername(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx) |
716 | 0 | { |
717 | 0 | char *objtmp = NULL, *p; |
718 | 0 | int objlen; |
719 | |
|
720 | 0 | if (!(p = strchr(value, ';'))) |
721 | 0 | return 0; |
722 | 0 | if (!(gen->d.otherName = OTHERNAME_new())) |
723 | 0 | return 0; |
724 | | /* Free this up because we will overwrite it. |
725 | | * no need to free type_id because it is static |
726 | | */ |
727 | 0 | ASN1_TYPE_free(gen->d.otherName->value); |
728 | 0 | if (!(gen->d.otherName->value = ASN1_generate_v3(p + 1, ctx))) |
729 | 0 | return 0; |
730 | 0 | objlen = p - value; |
731 | 0 | objtmp = malloc(objlen + 1); |
732 | 0 | if (objtmp) { |
733 | 0 | strlcpy(objtmp, value, objlen + 1); |
734 | 0 | gen->d.otherName->type_id = OBJ_txt2obj(objtmp, 0); |
735 | 0 | free(objtmp); |
736 | 0 | } else |
737 | 0 | gen->d.otherName->type_id = NULL; |
738 | 0 | if (!gen->d.otherName->type_id) |
739 | 0 | return 0; |
740 | 0 | return 1; |
741 | 0 | } |
742 | | |
743 | | static int |
744 | | do_dirname(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx) |
745 | 0 | { |
746 | 0 | int ret; |
747 | 0 | STACK_OF(CONF_VALUE) *sk; |
748 | 0 | X509_NAME *nm; |
749 | |
|
750 | 0 | if (!(nm = X509_NAME_new())) |
751 | 0 | return 0; |
752 | 0 | sk = X509V3_get_section(ctx, value); |
753 | 0 | if (!sk) { |
754 | 0 | X509V3error(X509V3_R_SECTION_NOT_FOUND); |
755 | 0 | ERR_asprintf_error_data("section=%s", value); |
756 | 0 | X509_NAME_free(nm); |
757 | 0 | return 0; |
758 | 0 | } |
759 | | /* FIXME: should allow other character types... */ |
760 | 0 | ret = X509V3_NAME_from_section(nm, sk, MBSTRING_ASC); |
761 | 0 | if (!ret) |
762 | 0 | X509_NAME_free(nm); |
763 | 0 | gen->d.dirn = nm; |
764 | 0 | X509V3_section_free(ctx, sk); |
765 | |
|
766 | 0 | return ret; |
767 | 0 | } |