/src/openssl41/crypto/x509/v3_san.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1999-2026 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include <stdio.h> |
11 | | #include "internal/cryptlib.h" |
12 | | #include "crypto/x509.h" |
13 | | #include <openssl/conf.h> |
14 | | #include <openssl/x509v3.h> |
15 | | #include <openssl/bio.h> |
16 | | #include "ext_dat.h" |
17 | | |
18 | | static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method, |
19 | | X509V3_CTX *ctx, |
20 | | STACK_OF(CONF_VALUE) *nval); |
21 | | static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method, |
22 | | X509V3_CTX *ctx, |
23 | | STACK_OF(CONF_VALUE) *nval); |
24 | | static int copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p); |
25 | | static int copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens); |
26 | | static int do_othername(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx); |
27 | | static int do_dirname(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx); |
28 | | |
29 | | const X509V3_EXT_METHOD ossl_v3_alt[3] = { |
30 | | { NID_subject_alt_name, 0, ASN1_ITEM_ref(GENERAL_NAMES), |
31 | | 0, 0, 0, 0, |
32 | | 0, 0, |
33 | | (X509V3_EXT_I2V)i2v_GENERAL_NAMES, |
34 | | (X509V3_EXT_V2I)v2i_subject_alt, |
35 | | NULL, NULL, NULL }, |
36 | | |
37 | | { NID_issuer_alt_name, 0, ASN1_ITEM_ref(GENERAL_NAMES), |
38 | | 0, 0, 0, 0, |
39 | | 0, 0, |
40 | | (X509V3_EXT_I2V)i2v_GENERAL_NAMES, |
41 | | (X509V3_EXT_V2I)v2i_issuer_alt, |
42 | | NULL, NULL, NULL }, |
43 | | |
44 | | { NID_certificate_issuer, 0, ASN1_ITEM_ref(GENERAL_NAMES), |
45 | | 0, 0, 0, 0, |
46 | | 0, 0, |
47 | | (X509V3_EXT_I2V)i2v_GENERAL_NAMES, |
48 | | NULL, NULL, NULL, NULL }, |
49 | | }; |
50 | | |
51 | | STACK_OF(CONF_VALUE) *i2v_GENERAL_NAMES(X509V3_EXT_METHOD *method, |
52 | | GENERAL_NAMES *gens, |
53 | | STACK_OF(CONF_VALUE) *ret) |
54 | 61.8k | { |
55 | 61.8k | int i; |
56 | 61.8k | GENERAL_NAME *gen; |
57 | 61.8k | STACK_OF(CONF_VALUE) *tmpret = NULL, *origret = ret; |
58 | | |
59 | 193k | for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { |
60 | 151k | gen = sk_GENERAL_NAME_value(gens, i); |
61 | | /* |
62 | | * i2v_GENERAL_NAME allocates ret if it is NULL. If something goes |
63 | | * wrong we need to free the stack - but only if it was empty when we |
64 | | * originally entered this function. |
65 | | */ |
66 | 151k | tmpret = i2v_GENERAL_NAME(method, gen, ret); |
67 | 151k | if (tmpret == NULL) { |
68 | 19.9k | if (origret == NULL) |
69 | 18.8k | sk_CONF_VALUE_pop_free(ret, X509V3_conf_free); |
70 | 19.9k | return NULL; |
71 | 19.9k | } |
72 | 131k | ret = tmpret; |
73 | 131k | } |
74 | 41.8k | if (ret == NULL) |
75 | 7.14k | return sk_CONF_VALUE_new_null(); |
76 | 34.7k | return ret; |
77 | 41.8k | } |
78 | | |
79 | | STACK_OF(CONF_VALUE) *i2v_GENERAL_NAME(X509V3_EXT_METHOD *method, |
80 | | GENERAL_NAME *gen, |
81 | | STACK_OF(CONF_VALUE) *ret) |
82 | 154k | { |
83 | 154k | char othername[300]; |
84 | 154k | char oline[256], *tmp; |
85 | | |
86 | 154k | switch (gen->type) { |
87 | 30.2k | case GEN_OTHERNAME: |
88 | 30.2k | switch (OBJ_obj2nid(gen->d.otherName->type_id)) { |
89 | 794 | case NID_id_on_SmtpUTF8Mailbox: |
90 | 794 | if (gen->d.otherName->value->type != V_ASN1_UTF8STRING |
91 | 561 | || !x509v3_add_len_value_uchar("othername: SmtpUTF8Mailbox", |
92 | 561 | gen->d.otherName->value->value.utf8string->data, |
93 | 561 | gen->d.otherName->value->value.utf8string->length, |
94 | 561 | &ret)) |
95 | 644 | return NULL; |
96 | 150 | break; |
97 | 935 | case NID_XmppAddr: |
98 | 935 | if (gen->d.otherName->value->type != V_ASN1_UTF8STRING |
99 | 428 | || !x509v3_add_len_value_uchar("othername: XmppAddr", |
100 | 428 | gen->d.otherName->value->value.utf8string->data, |
101 | 428 | gen->d.otherName->value->value.utf8string->length, |
102 | 428 | &ret)) |
103 | 782 | return NULL; |
104 | 153 | break; |
105 | 496 | case NID_SRVName: |
106 | 496 | if (gen->d.otherName->value->type != V_ASN1_IA5STRING |
107 | 240 | || !x509v3_add_len_value_uchar("othername: SRVName", |
108 | 240 | gen->d.otherName->value->value.ia5string->data, |
109 | 240 | gen->d.otherName->value->value.ia5string->length, |
110 | 240 | &ret)) |
111 | 442 | return NULL; |
112 | 54 | break; |
113 | 480 | case NID_ms_upn: |
114 | 480 | if (gen->d.otherName->value->type != V_ASN1_UTF8STRING |
115 | 317 | || !x509v3_add_len_value_uchar("othername: UPN", |
116 | 317 | gen->d.otherName->value->value.utf8string->data, |
117 | 317 | gen->d.otherName->value->value.utf8string->length, |
118 | 317 | &ret)) |
119 | 370 | return NULL; |
120 | 110 | break; |
121 | 6.11k | case NID_NAIRealm: |
122 | 6.11k | if (gen->d.otherName->value->type != V_ASN1_UTF8STRING |
123 | 4.07k | || !x509v3_add_len_value_uchar("othername: NAIRealm", |
124 | 4.07k | gen->d.otherName->value->value.utf8string->data, |
125 | 4.07k | gen->d.otherName->value->value.utf8string->length, |
126 | 4.07k | &ret)) |
127 | 2.39k | return NULL; |
128 | 3.72k | break; |
129 | 21.4k | default: |
130 | 21.4k | if (OBJ_obj2txt(oline, sizeof(oline), gen->d.otherName->type_id, 0) > 0) |
131 | 21.4k | snprintf(othername, sizeof(othername), "othername: %s", |
132 | 21.4k | oline); |
133 | 0 | else |
134 | 0 | OPENSSL_strlcpy(othername, "othername", sizeof(othername)); |
135 | | |
136 | | /* check if the value is something printable */ |
137 | 21.4k | if (gen->d.otherName->value->type == V_ASN1_IA5STRING) { |
138 | 4.18k | if (x509v3_add_len_value_uchar(othername, |
139 | 4.18k | gen->d.otherName->value->value.ia5string->data, |
140 | 4.18k | gen->d.otherName->value->value.ia5string->length, |
141 | 4.18k | &ret)) |
142 | 3.21k | return ret; |
143 | 4.18k | } |
144 | 18.2k | if (gen->d.otherName->value->type == V_ASN1_UTF8STRING) { |
145 | 6.63k | if (x509v3_add_len_value_uchar(othername, |
146 | 6.63k | gen->d.otherName->value->value.utf8string->data, |
147 | 6.63k | gen->d.otherName->value->value.utf8string->length, |
148 | 6.63k | &ret)) |
149 | 3.06k | return ret; |
150 | 6.63k | } |
151 | 15.1k | if (!X509V3_add_value(othername, "<unsupported>", &ret)) |
152 | 0 | return NULL; |
153 | 15.1k | break; |
154 | 30.2k | } |
155 | 19.3k | break; |
156 | | |
157 | 19.3k | case GEN_X400: |
158 | 6.61k | if (!X509V3_add_value("X400Name", "<unsupported>", &ret)) |
159 | 0 | return NULL; |
160 | 6.61k | break; |
161 | | |
162 | 6.61k | case GEN_EDIPARTY: |
163 | 1.01k | if (!X509V3_add_value("EdiPartyName", "<unsupported>", &ret)) |
164 | 0 | return NULL; |
165 | 1.01k | break; |
166 | | |
167 | 18.8k | case GEN_EMAIL: |
168 | 18.8k | if (!x509v3_add_len_value_uchar("email", gen->d.ia5->data, |
169 | 18.8k | gen->d.ia5->length, &ret)) |
170 | 8.21k | return NULL; |
171 | 10.6k | break; |
172 | | |
173 | 30.2k | case GEN_DNS: |
174 | 30.2k | if (!x509v3_add_len_value_uchar("DNS", gen->d.ia5->data, |
175 | 30.2k | gen->d.ia5->length, &ret)) |
176 | 3.01k | return NULL; |
177 | 27.2k | break; |
178 | | |
179 | 36.4k | case GEN_URI: |
180 | 36.4k | if (!x509v3_add_len_value_uchar("URI", gen->d.ia5->data, |
181 | 36.4k | gen->d.ia5->length, &ret)) |
182 | 4.91k | return NULL; |
183 | 31.5k | break; |
184 | | |
185 | 31.5k | case GEN_DIRNAME: |
186 | 6.41k | if (X509_NAME_oneline(gen->d.dirn, oline, sizeof(oline)) == NULL |
187 | 6.41k | || !X509V3_add_value("DirName", oline, &ret)) |
188 | 0 | return NULL; |
189 | 6.41k | break; |
190 | | |
191 | 21.1k | case GEN_IPADD: |
192 | 21.1k | tmp = ossl_ipaddr_to_asc(gen->d.ip->data, gen->d.ip->length); |
193 | 21.1k | if (tmp == NULL || !X509V3_add_value("IP Address", tmp, &ret)) |
194 | 0 | ret = NULL; |
195 | 21.1k | OPENSSL_free(tmp); |
196 | 21.1k | break; |
197 | | |
198 | 3.72k | case GEN_RID: |
199 | 3.72k | i2t_ASN1_OBJECT(oline, 256, gen->d.rid); |
200 | 3.72k | if (!X509V3_add_value("Registered ID", oline, &ret)) |
201 | 0 | return NULL; |
202 | 3.72k | break; |
203 | 154k | } |
204 | 127k | return ret; |
205 | 154k | } |
206 | | |
207 | | int GENERAL_NAME_print(BIO *out, GENERAL_NAME *gen) |
208 | 31.1k | { |
209 | 31.1k | char *tmp; |
210 | 31.1k | int nid; |
211 | | |
212 | 31.1k | switch (gen->type) { |
213 | 135 | case GEN_OTHERNAME: |
214 | 135 | nid = OBJ_obj2nid(gen->d.otherName->type_id); |
215 | | /* Validate the types are as we expect before we use them */ |
216 | 135 | if ((nid == NID_SRVName |
217 | 6 | && gen->d.otherName->value->type != V_ASN1_IA5STRING) |
218 | 132 | || (nid != NID_SRVName |
219 | 129 | && gen->d.otherName->value->type != V_ASN1_UTF8STRING)) { |
220 | 58 | BIO_printf(out, "othername:<unsupported>"); |
221 | 58 | break; |
222 | 58 | } |
223 | | |
224 | 77 | switch (nid) { |
225 | 3 | case NID_id_on_SmtpUTF8Mailbox: |
226 | 3 | BIO_printf(out, "othername:SmtpUTF8Mailbox:%.*s", |
227 | 3 | gen->d.otherName->value->value.utf8string->length, |
228 | 3 | gen->d.otherName->value->value.utf8string->length |
229 | 3 | ? gen->d.otherName->value->value.utf8string->data |
230 | 3 | : (const unsigned char *)""); |
231 | 3 | break; |
232 | 3 | case NID_XmppAddr: |
233 | 3 | BIO_printf(out, "othername:XmppAddr:%.*s", |
234 | 3 | gen->d.otherName->value->value.utf8string->length, |
235 | 3 | gen->d.otherName->value->value.utf8string->length |
236 | 3 | ? gen->d.otherName->value->value.utf8string->data |
237 | 3 | : (const unsigned char *)""); |
238 | 3 | break; |
239 | 3 | case NID_SRVName: |
240 | 3 | BIO_printf(out, "othername:SRVName:%.*s", |
241 | 3 | gen->d.otherName->value->value.ia5string->length, |
242 | 3 | gen->d.otherName->value->value.ia5string->length |
243 | 3 | ? gen->d.otherName->value->value.ia5string->data |
244 | 3 | : (const unsigned char *)""); |
245 | 3 | break; |
246 | 10 | case NID_ms_upn: |
247 | 10 | BIO_printf(out, "othername:UPN:%.*s", |
248 | 10 | gen->d.otherName->value->value.utf8string->length, |
249 | 10 | gen->d.otherName->value->value.utf8string->length |
250 | 10 | ? gen->d.otherName->value->value.utf8string->data |
251 | 10 | : (const unsigned char *)""); |
252 | 10 | break; |
253 | 4 | case NID_NAIRealm: |
254 | 4 | BIO_printf(out, "othername:NAIRealm:%.*s", |
255 | 4 | gen->d.otherName->value->value.utf8string->length, |
256 | 4 | gen->d.otherName->value->value.utf8string->length |
257 | 4 | ? gen->d.otherName->value->value.utf8string->data |
258 | 4 | : (const unsigned char *)""); |
259 | 4 | break; |
260 | 54 | default: |
261 | 54 | BIO_printf(out, "othername:<unsupported>"); |
262 | 54 | break; |
263 | 77 | } |
264 | 77 | break; |
265 | | |
266 | 2.48k | case GEN_X400: |
267 | 2.48k | BIO_printf(out, "X400Name:<unsupported>"); |
268 | 2.48k | break; |
269 | | |
270 | 9 | case GEN_EDIPARTY: |
271 | | /* Maybe fix this: it is supported now */ |
272 | 9 | BIO_printf(out, "EdiPartyName:<unsupported>"); |
273 | 9 | break; |
274 | | |
275 | 2.52k | case GEN_EMAIL: |
276 | 2.52k | BIO_printf(out, "email:"); |
277 | 2.52k | ASN1_STRING_print(out, gen->d.ia5); |
278 | 2.52k | break; |
279 | | |
280 | 2.99k | case GEN_DNS: |
281 | 2.99k | BIO_printf(out, "DNS:"); |
282 | 2.99k | ASN1_STRING_print(out, gen->d.ia5); |
283 | 2.99k | break; |
284 | | |
285 | 3.45k | case GEN_URI: |
286 | 3.45k | BIO_printf(out, "URI:"); |
287 | 3.45k | ASN1_STRING_print(out, gen->d.ia5); |
288 | 3.45k | break; |
289 | | |
290 | 16.5k | case GEN_DIRNAME: |
291 | 16.5k | BIO_printf(out, "DirName:"); |
292 | 16.5k | X509_NAME_print_ex(out, gen->d.dirn, 0, XN_FLAG_ONELINE); |
293 | 16.5k | break; |
294 | | |
295 | 1.61k | case GEN_IPADD: |
296 | 1.61k | tmp = ossl_ipaddr_to_asc(gen->d.ip->data, gen->d.ip->length); |
297 | 1.61k | if (tmp == NULL) |
298 | 0 | return 0; |
299 | 1.61k | BIO_printf(out, "IP Address:%s", tmp); |
300 | 1.61k | OPENSSL_free(tmp); |
301 | 1.61k | break; |
302 | | |
303 | 1.37k | case GEN_RID: |
304 | 1.37k | BIO_printf(out, "Registered ID:"); |
305 | 1.37k | i2a_ASN1_OBJECT(out, gen->d.rid); |
306 | 1.37k | break; |
307 | 31.1k | } |
308 | 31.1k | return 1; |
309 | 31.1k | } |
310 | | |
311 | | static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method, |
312 | | X509V3_CTX *ctx, |
313 | | STACK_OF(CONF_VALUE) *nval) |
314 | 851 | { |
315 | 851 | const int num = sk_CONF_VALUE_num(nval); |
316 | 851 | GENERAL_NAMES *gens = sk_GENERAL_NAME_new_reserve(NULL, num); |
317 | 851 | int i; |
318 | | |
319 | 851 | if (gens == NULL) { |
320 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB); |
321 | 0 | return NULL; |
322 | 0 | } |
323 | 1.97k | for (i = 0; i < num; i++) { |
324 | 1.45k | CONF_VALUE *cnf = sk_CONF_VALUE_value(nval, i); |
325 | | |
326 | 1.45k | if (!ossl_v3_name_cmp(cnf->name, "issuer") |
327 | 479 | && cnf->value && strcmp(cnf->value, "copy") == 0) { |
328 | 444 | if (!copy_issuer(ctx, gens)) |
329 | 159 | goto err; |
330 | 1.00k | } else { |
331 | 1.00k | GENERAL_NAME *gen = v2i_GENERAL_NAME(method, ctx, cnf); |
332 | | |
333 | 1.00k | if (gen == NULL) |
334 | 169 | goto err; |
335 | 838 | sk_GENERAL_NAME_push(gens, gen); /* no failure as it was reserved */ |
336 | 838 | } |
337 | 1.45k | } |
338 | 523 | return gens; |
339 | 328 | err: |
340 | 328 | sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); |
341 | 328 | return NULL; |
342 | 851 | } |
343 | | |
344 | | /* Append subject altname of issuer to issuer alt name of subject */ |
345 | | |
346 | | static int copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens) |
347 | 444 | { |
348 | 444 | GENERAL_NAMES *ialt = NULL; |
349 | 444 | GENERAL_NAME *gen; |
350 | 444 | const X509_EXTENSION *ext; |
351 | 444 | int i, num; |
352 | | |
353 | 444 | if (ctx != NULL && (ctx->flags & X509V3_CTX_TEST) != 0) |
354 | 0 | return 1; |
355 | 444 | if (!ctx || !ctx->issuer_cert) { |
356 | 158 | ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_ISSUER_DETAILS); |
357 | 158 | goto err; |
358 | 158 | } |
359 | 286 | i = X509_get_ext_by_NID(ctx->issuer_cert, NID_subject_alt_name, -1); |
360 | 286 | if (i < 0) |
361 | 162 | return 1; |
362 | 124 | if ((ext = X509_get_ext(ctx->issuer_cert, i)) == NULL |
363 | 124 | || (ialt = X509V3_EXT_d2i(ext)) == NULL) { |
364 | 1 | ERR_raise(ERR_LIB_X509V3, X509V3_R_ISSUER_DECODE_ERROR); |
365 | 1 | goto err; |
366 | 1 | } |
367 | | |
368 | 123 | num = sk_GENERAL_NAME_num(ialt); |
369 | 123 | if (!sk_GENERAL_NAME_reserve(gens, num)) { |
370 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB); |
371 | 0 | goto err; |
372 | 0 | } |
373 | | |
374 | 32.9k | for (i = 0; i < num; i++) { |
375 | 32.8k | gen = sk_GENERAL_NAME_value(ialt, i); |
376 | 32.8k | sk_GENERAL_NAME_push(gens, gen); /* no failure as it was reserved */ |
377 | 32.8k | } |
378 | 123 | sk_GENERAL_NAME_free(ialt); |
379 | | |
380 | 123 | return 1; |
381 | | |
382 | 159 | err: |
383 | 159 | sk_GENERAL_NAME_free(ialt); |
384 | 159 | return 0; |
385 | 123 | } |
386 | | |
387 | | static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method, |
388 | | X509V3_CTX *ctx, |
389 | | STACK_OF(CONF_VALUE) *nval) |
390 | 857 | { |
391 | 857 | GENERAL_NAMES *gens; |
392 | 857 | CONF_VALUE *cnf; |
393 | 857 | const int num = sk_CONF_VALUE_num(nval); |
394 | 857 | int i; |
395 | | |
396 | 857 | gens = sk_GENERAL_NAME_new_reserve(NULL, num); |
397 | 857 | if (gens == NULL) { |
398 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB); |
399 | 0 | return NULL; |
400 | 0 | } |
401 | | |
402 | 94.3k | for (i = 0; i < num; i++) { |
403 | 93.7k | cnf = sk_CONF_VALUE_value(nval, i); |
404 | 93.7k | if (ossl_v3_name_cmp(cnf->name, "email") == 0 |
405 | 2.60k | && cnf->value && strcmp(cnf->value, "copy") == 0) { |
406 | 7 | if (!copy_email(ctx, gens, 0)) |
407 | 3 | goto err; |
408 | 93.7k | } else if (ossl_v3_name_cmp(cnf->name, "email") == 0 |
409 | 2.59k | && cnf->value && strcmp(cnf->value, "move") == 0) { |
410 | 9 | if (!copy_email(ctx, gens, 1)) |
411 | 4 | goto err; |
412 | 93.7k | } else { |
413 | 93.7k | GENERAL_NAME *gen; |
414 | 93.7k | if ((gen = v2i_GENERAL_NAME(method, ctx, cnf)) == NULL) |
415 | 267 | goto err; |
416 | 93.5k | sk_GENERAL_NAME_push(gens, gen); /* no failure as it was reserved */ |
417 | 93.5k | } |
418 | 93.7k | } |
419 | 583 | return gens; |
420 | 274 | err: |
421 | 274 | sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); |
422 | 274 | return NULL; |
423 | 857 | } |
424 | | |
425 | | /* |
426 | | * Copy any email addresses in a certificate or request to GENERAL_NAMES |
427 | | */ |
428 | | |
429 | | static int copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p) |
430 | 16 | { |
431 | 16 | const X509_NAME *nm; |
432 | 16 | ASN1_IA5STRING *email = NULL; |
433 | 16 | X509_NAME_ENTRY *ne; |
434 | 16 | GENERAL_NAME *gen = NULL; |
435 | 16 | int i = -1; |
436 | | |
437 | 16 | if (ctx != NULL && (ctx->flags & X509V3_CTX_TEST) != 0) |
438 | 0 | return 1; |
439 | 16 | if (ctx == NULL |
440 | 16 | || (ctx->subject_cert == NULL && ctx->subject_req == NULL)) { |
441 | 7 | ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_SUBJECT_DETAILS); |
442 | 7 | return 0; |
443 | 7 | } |
444 | | /* Find the subject name */ |
445 | 9 | nm = ctx->subject_cert != NULL ? X509_get_subject_name(ctx->subject_cert) |
446 | 9 | : X509_REQ_get_subject_name(ctx->subject_req); |
447 | | |
448 | | /* Now add any email address(es) to STACK */ |
449 | 9 | while ((i = X509_NAME_get_index_by_NID(nm, |
450 | 9 | NID_pkcs9_emailAddress, i)) |
451 | 9 | >= 0) { |
452 | 0 | if (move_p) { |
453 | | /* We should really not support deleting things in a const object |
454 | | * to rip the pointer out of it. If we truly want a new object |
455 | | * without this in it, we should just construct one without it. |
456 | | */ |
457 | 0 | return 0; |
458 | 0 | } |
459 | | /* XXX Casts away const */ |
460 | 0 | ne = (X509_NAME_ENTRY *)X509_NAME_get_entry(nm, i); |
461 | 0 | email = ASN1_STRING_dup(X509_NAME_ENTRY_get_data(ne)); |
462 | 0 | if (email == NULL || (gen = GENERAL_NAME_new()) == NULL) { |
463 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB); |
464 | 0 | goto err; |
465 | 0 | } |
466 | 0 | gen->d.ia5 = email; |
467 | 0 | email = NULL; |
468 | 0 | gen->type = GEN_EMAIL; |
469 | 0 | if (!sk_GENERAL_NAME_push(gens, gen)) { |
470 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB); |
471 | 0 | goto err; |
472 | 0 | } |
473 | 0 | gen = NULL; |
474 | 0 | } |
475 | | |
476 | 9 | return 1; |
477 | | |
478 | 0 | err: |
479 | 0 | GENERAL_NAME_free(gen); |
480 | 0 | ASN1_IA5STRING_free(email); |
481 | 0 | return 0; |
482 | 9 | } |
483 | | |
484 | | GENERAL_NAMES *v2i_GENERAL_NAMES(const X509V3_EXT_METHOD *method, |
485 | | X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval) |
486 | 60 | { |
487 | 60 | GENERAL_NAME *gen; |
488 | 60 | GENERAL_NAMES *gens; |
489 | 60 | CONF_VALUE *cnf; |
490 | 60 | const int num = sk_CONF_VALUE_num(nval); |
491 | 60 | int i; |
492 | | |
493 | 60 | gens = sk_GENERAL_NAME_new_reserve(NULL, num); |
494 | 60 | if (gens == NULL) { |
495 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB); |
496 | 0 | return NULL; |
497 | 0 | } |
498 | | |
499 | 317 | for (i = 0; i < num; i++) { |
500 | 279 | cnf = sk_CONF_VALUE_value(nval, i); |
501 | 279 | if ((gen = v2i_GENERAL_NAME(method, ctx, cnf)) == NULL) |
502 | 22 | goto err; |
503 | 257 | sk_GENERAL_NAME_push(gens, gen); /* no failure as it was reserved */ |
504 | 257 | } |
505 | 38 | return gens; |
506 | 22 | err: |
507 | 22 | sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); |
508 | 22 | return NULL; |
509 | 60 | } |
510 | | |
511 | | GENERAL_NAME *v2i_GENERAL_NAME(const X509V3_EXT_METHOD *method, |
512 | | X509V3_CTX *ctx, CONF_VALUE *cnf) |
513 | 129k | { |
514 | 129k | return v2i_GENERAL_NAME_ex(NULL, method, ctx, cnf, 0); |
515 | 129k | } |
516 | | |
517 | | GENERAL_NAME *a2i_GENERAL_NAME(GENERAL_NAME *out, |
518 | | const X509V3_EXT_METHOD *method, |
519 | | X509V3_CTX *ctx, int gen_type, const char *value, |
520 | | int is_nc) |
521 | 132k | { |
522 | 132k | char is_string = 0; |
523 | 132k | GENERAL_NAME *gen = NULL; |
524 | | |
525 | 132k | if (!value) { |
526 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_MISSING_VALUE); |
527 | 0 | return NULL; |
528 | 0 | } |
529 | | |
530 | 132k | if (out) |
531 | 3.00k | gen = out; |
532 | 129k | else { |
533 | 129k | gen = GENERAL_NAME_new(); |
534 | 129k | if (gen == NULL) { |
535 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB); |
536 | 0 | return NULL; |
537 | 0 | } |
538 | 129k | } |
539 | | |
540 | 132k | switch (gen_type) { |
541 | 89.5k | case GEN_URI: |
542 | 94.7k | case GEN_EMAIL: |
543 | 95.5k | case GEN_DNS: |
544 | 95.5k | is_string = 1; |
545 | 95.5k | break; |
546 | | |
547 | 34.6k | case GEN_RID: { |
548 | 34.6k | ASN1_OBJECT *obj; |
549 | 34.6k | if ((obj = OBJ_txt2obj(value, 0)) == NULL) { |
550 | 93 | ERR_raise_data(ERR_LIB_X509V3, X509V3_R_BAD_OBJECT, |
551 | 93 | "value=%s", value); |
552 | 93 | goto err; |
553 | 93 | } |
554 | 34.6k | gen->d.rid = obj; |
555 | 34.6k | } break; |
556 | | |
557 | 1.07k | case GEN_IPADD: |
558 | 1.07k | if (is_nc) |
559 | 711 | gen->d.ip = a2i_IPADDRESS_NC(value); |
560 | 365 | else |
561 | 365 | gen->d.ip = a2i_IPADDRESS(value); |
562 | 1.07k | if (gen->d.ip == NULL) { |
563 | 245 | ERR_raise_data(ERR_LIB_X509V3, X509V3_R_BAD_IP_ADDRESS, |
564 | 245 | "value=%s", value); |
565 | 245 | goto err; |
566 | 245 | } |
567 | 831 | break; |
568 | | |
569 | 831 | case GEN_DIRNAME: |
570 | 758 | if (!do_dirname(gen, value, ctx)) { |
571 | 82 | ERR_raise(ERR_LIB_X509V3, X509V3_R_DIRNAME_ERROR); |
572 | 82 | goto err; |
573 | 82 | } |
574 | 676 | break; |
575 | | |
576 | 676 | case GEN_OTHERNAME: |
577 | 232 | if (!do_othername(gen, value, ctx)) { |
578 | 81 | ERR_raise(ERR_LIB_X509V3, X509V3_R_OTHERNAME_ERROR); |
579 | 81 | goto err; |
580 | 81 | } |
581 | 151 | break; |
582 | 151 | default: |
583 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_UNSUPPORTED_TYPE); |
584 | 0 | goto err; |
585 | 132k | } |
586 | | |
587 | 131k | if (is_string) { |
588 | 95.5k | if ((gen->d.ia5 = ASN1_IA5STRING_new()) == NULL |
589 | 95.5k | || !ASN1_STRING_set1_string(gen->d.ia5, value)) { |
590 | 0 | ASN1_IA5STRING_free(gen->d.ia5); |
591 | 0 | gen->d.ia5 = NULL; |
592 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB); |
593 | 0 | goto err; |
594 | 0 | } |
595 | 95.5k | } |
596 | | |
597 | 131k | gen->type = gen_type; |
598 | | |
599 | 131k | return gen; |
600 | | |
601 | 501 | err: |
602 | 501 | if (!out) |
603 | 315 | GENERAL_NAME_free(gen); |
604 | 501 | return NULL; |
605 | 131k | } |
606 | | |
607 | | GENERAL_NAME *v2i_GENERAL_NAME_ex(GENERAL_NAME *out, |
608 | | const X509V3_EXT_METHOD *method, |
609 | | X509V3_CTX *ctx, CONF_VALUE *cnf, int is_nc) |
610 | 132k | { |
611 | 132k | int type; |
612 | | |
613 | 132k | char *name, *value; |
614 | | |
615 | 132k | name = cnf->name; |
616 | 132k | value = cnf->value; |
617 | | |
618 | 132k | if (!value) { |
619 | 137 | ERR_raise(ERR_LIB_X509V3, X509V3_R_MISSING_VALUE); |
620 | 137 | return NULL; |
621 | 137 | } |
622 | | |
623 | 132k | if (!ossl_v3_name_cmp(name, "email")) |
624 | 5.23k | type = GEN_EMAIL; |
625 | 127k | else if (!ossl_v3_name_cmp(name, "URI")) |
626 | 89.5k | type = GEN_URI; |
627 | 37.7k | else if (!ossl_v3_name_cmp(name, "DNS")) |
628 | 802 | type = GEN_DNS; |
629 | 36.9k | else if (!ossl_v3_name_cmp(name, "RID")) |
630 | 34.6k | type = GEN_RID; |
631 | 2.28k | else if (!ossl_v3_name_cmp(name, "IP")) |
632 | 1.07k | type = GEN_IPADD; |
633 | 1.20k | else if (!ossl_v3_name_cmp(name, "dirName")) |
634 | 758 | type = GEN_DIRNAME; |
635 | 450 | else if (!ossl_v3_name_cmp(name, "otherName")) |
636 | 232 | type = GEN_OTHERNAME; |
637 | 218 | else { |
638 | 218 | ERR_raise_data(ERR_LIB_X509V3, X509V3_R_UNSUPPORTED_OPTION, |
639 | 218 | "name=%s", name); |
640 | 218 | return NULL; |
641 | 218 | } |
642 | | |
643 | 132k | return a2i_GENERAL_NAME(out, method, ctx, type, value, is_nc); |
644 | 132k | } |
645 | | |
646 | | static int do_othername(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx) |
647 | 232 | { |
648 | 232 | char *objtmp = NULL; |
649 | 232 | const char *p; |
650 | 232 | size_t objlen; |
651 | | |
652 | 232 | if ((p = strchr(value, ';')) == NULL) |
653 | 3 | return 0; |
654 | 229 | if ((gen->d.otherName = OTHERNAME_new()) == NULL) |
655 | 0 | return 0; |
656 | | /* |
657 | | * Free this up because we will overwrite it. no need to free type_id |
658 | | * because it is static |
659 | | */ |
660 | 229 | ASN1_TYPE_free(gen->d.otherName->value); |
661 | 229 | if ((gen->d.otherName->value = ASN1_generate_v3(p + 1, ctx)) == NULL) |
662 | 24 | goto err; |
663 | 205 | objlen = p - value; |
664 | 205 | objtmp = OPENSSL_strndup(value, objlen); |
665 | 205 | if (objtmp == NULL) |
666 | 0 | goto err; |
667 | 205 | gen->d.otherName->type_id = OBJ_txt2obj(objtmp, 0); |
668 | 205 | OPENSSL_free(objtmp); |
669 | 205 | if (!gen->d.otherName->type_id) |
670 | 54 | goto err; |
671 | 151 | return 1; |
672 | | |
673 | 78 | err: |
674 | 78 | OTHERNAME_free(gen->d.otherName); |
675 | 78 | gen->d.otherName = NULL; |
676 | 78 | return 0; |
677 | 205 | } |
678 | | |
679 | | static int do_dirname(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx) |
680 | 758 | { |
681 | 758 | int ret = 0; |
682 | 758 | STACK_OF(CONF_VALUE) *sk = NULL; |
683 | 758 | X509_NAME *nm; |
684 | | |
685 | 758 | if ((nm = X509_NAME_new()) == NULL) |
686 | 0 | goto err; |
687 | 758 | sk = X509V3_get_section(ctx, value); |
688 | 758 | if (!sk) { |
689 | 18 | ERR_raise_data(ERR_LIB_X509V3, X509V3_R_SECTION_NOT_FOUND, |
690 | 18 | "section=%s", value); |
691 | 18 | goto err; |
692 | 18 | } |
693 | | /* FIXME: should allow other character types... */ |
694 | 740 | ret = X509V3_NAME_from_section(nm, sk, MBSTRING_ASC); |
695 | 740 | if (!ret) |
696 | 64 | goto err; |
697 | 676 | gen->d.dirn = nm; |
698 | | |
699 | 758 | err: |
700 | 758 | if (ret == 0) |
701 | 82 | X509_NAME_free(nm); |
702 | 758 | X509V3_section_free(ctx, sk); |
703 | 758 | return ret; |
704 | 676 | } |