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