/src/openssl34/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 | 68.2k | { |
55 | 68.2k | int i; |
56 | 68.2k | GENERAL_NAME *gen; |
57 | 68.2k | STACK_OF(CONF_VALUE) *tmpret = NULL, *origret = ret; |
58 | | |
59 | 212k | for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { |
60 | 166k | 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 | 166k | tmpret = i2v_GENERAL_NAME(method, gen, ret); |
67 | 166k | if (tmpret == NULL) { |
68 | 23.0k | if (origret == NULL) |
69 | 21.9k | sk_CONF_VALUE_pop_free(ret, X509V3_conf_free); |
70 | 23.0k | return NULL; |
71 | 23.0k | } |
72 | 143k | ret = tmpret; |
73 | 143k | } |
74 | 45.1k | if (ret == NULL) |
75 | 9.22k | return sk_CONF_VALUE_new_null(); |
76 | 35.8k | return ret; |
77 | 45.1k | } |
78 | | |
79 | | STACK_OF(CONF_VALUE) *i2v_GENERAL_NAME(X509V3_EXT_METHOD *method, |
80 | | GENERAL_NAME *gen, |
81 | | STACK_OF(CONF_VALUE) *ret) |
82 | 170k | { |
83 | 170k | char othername[300]; |
84 | 170k | char oline[256], *tmp; |
85 | | |
86 | 170k | switch (gen->type) { |
87 | 32.8k | case GEN_OTHERNAME: |
88 | 32.8k | switch (OBJ_obj2nid(gen->d.otherName->type_id)) { |
89 | 858 | case NID_id_on_SmtpUTF8Mailbox: |
90 | 858 | if (gen->d.otherName->value->type != V_ASN1_UTF8STRING |
91 | 641 | || !x509v3_add_len_value_uchar("othername: SmtpUTF8Mailbox", |
92 | 641 | gen->d.otherName->value->value.utf8string->data, |
93 | 641 | gen->d.otherName->value->value.utf8string->length, |
94 | 641 | &ret)) |
95 | 682 | return NULL; |
96 | 176 | break; |
97 | 1.02k | case NID_XmppAddr: |
98 | 1.02k | if (gen->d.otherName->value->type != V_ASN1_UTF8STRING |
99 | 491 | || !x509v3_add_len_value_uchar("othername: XmppAddr", |
100 | 491 | gen->d.otherName->value->value.utf8string->data, |
101 | 491 | gen->d.otherName->value->value.utf8string->length, |
102 | 491 | &ret)) |
103 | 862 | return NULL; |
104 | 158 | break; |
105 | 551 | case NID_SRVName: |
106 | 551 | if (gen->d.otherName->value->type != V_ASN1_IA5STRING |
107 | 281 | || !x509v3_add_len_value_uchar("othername: SRVName", |
108 | 281 | gen->d.otherName->value->value.ia5string->data, |
109 | 281 | gen->d.otherName->value->value.ia5string->length, |
110 | 281 | &ret)) |
111 | 468 | return NULL; |
112 | 83 | break; |
113 | 566 | case NID_ms_upn: |
114 | 566 | if (gen->d.otherName->value->type != V_ASN1_UTF8STRING |
115 | 374 | || !x509v3_add_len_value_uchar("othername: UPN", |
116 | 374 | gen->d.otherName->value->value.utf8string->data, |
117 | 374 | gen->d.otherName->value->value.utf8string->length, |
118 | 374 | &ret)) |
119 | 448 | return NULL; |
120 | 118 | break; |
121 | 6.10k | case NID_NAIRealm: |
122 | 6.10k | if (gen->d.otherName->value->type != V_ASN1_UTF8STRING |
123 | 3.92k | || !x509v3_add_len_value_uchar("othername: NAIRealm", |
124 | 3.92k | gen->d.otherName->value->value.utf8string->data, |
125 | 3.92k | gen->d.otherName->value->value.utf8string->length, |
126 | 3.92k | &ret)) |
127 | 2.54k | return NULL; |
128 | 3.55k | break; |
129 | 23.7k | default: |
130 | 23.7k | if (OBJ_obj2txt(oline, sizeof(oline), gen->d.otherName->type_id, 0) > 0) |
131 | 23.7k | BIO_snprintf(othername, sizeof(othername), "othername: %s", |
132 | 23.7k | oline); |
133 | 0 | else |
134 | 0 | OPENSSL_strlcpy(othername, "othername", sizeof(othername)); |
135 | | |
136 | | /* check if the value is something printable */ |
137 | 23.7k | if (gen->d.otherName->value->type == V_ASN1_IA5STRING) { |
138 | 7.14k | if (x509v3_add_len_value_uchar(othername, |
139 | 7.14k | gen->d.otherName->value->value.ia5string->data, |
140 | 7.14k | gen->d.otherName->value->value.ia5string->length, |
141 | 7.14k | &ret)) |
142 | 4.76k | return ret; |
143 | 7.14k | } |
144 | 18.9k | if (gen->d.otherName->value->type == V_ASN1_UTF8STRING) { |
145 | 6.55k | if (x509v3_add_len_value_uchar(othername, |
146 | 6.55k | gen->d.otherName->value->value.utf8string->data, |
147 | 6.55k | gen->d.otherName->value->value.utf8string->length, |
148 | 6.55k | &ret)) |
149 | 3.00k | return ret; |
150 | 6.55k | } |
151 | 15.9k | if (!X509V3_add_value(othername, "<unsupported>", &ret)) |
152 | 0 | return NULL; |
153 | 15.9k | break; |
154 | 32.8k | } |
155 | 20.0k | break; |
156 | | |
157 | 20.0k | case GEN_X400: |
158 | 7.33k | if (!X509V3_add_value("X400Name", "<unsupported>", &ret)) |
159 | 0 | return NULL; |
160 | 7.33k | break; |
161 | | |
162 | 7.33k | case GEN_EDIPARTY: |
163 | 1.04k | if (!X509V3_add_value("EdiPartyName", "<unsupported>", &ret)) |
164 | 0 | return NULL; |
165 | 1.04k | break; |
166 | | |
167 | 22.8k | case GEN_EMAIL: |
168 | 22.8k | if (!x509v3_add_len_value_uchar("email", gen->d.ia5->data, |
169 | 22.8k | gen->d.ia5->length, &ret)) |
170 | 10.7k | return NULL; |
171 | 12.0k | break; |
172 | | |
173 | 34.1k | case GEN_DNS: |
174 | 34.1k | if (!x509v3_add_len_value_uchar("DNS", gen->d.ia5->data, |
175 | 34.1k | gen->d.ia5->length, &ret)) |
176 | 3.25k | return NULL; |
177 | 30.8k | break; |
178 | | |
179 | 36.5k | case GEN_URI: |
180 | 36.5k | if (!x509v3_add_len_value_uchar("URI", gen->d.ia5->data, |
181 | 36.5k | gen->d.ia5->length, &ret)) |
182 | 4.87k | return NULL; |
183 | 31.7k | break; |
184 | | |
185 | 31.7k | case GEN_DIRNAME: |
186 | 6.10k | if (X509_NAME_oneline(gen->d.dirn, oline, sizeof(oline)) == NULL |
187 | 6.10k | || !X509V3_add_value("DirName", oline, &ret)) |
188 | 0 | return NULL; |
189 | 6.10k | break; |
190 | | |
191 | 25.1k | case GEN_IPADD: |
192 | 25.1k | tmp = ossl_ipaddr_to_asc(gen->d.ip->data, gen->d.ip->length); |
193 | 25.1k | if (tmp == NULL || !X509V3_add_value("IP Address", tmp, &ret)) |
194 | 0 | ret = NULL; |
195 | 25.1k | OPENSSL_free(tmp); |
196 | 25.1k | break; |
197 | | |
198 | 4.40k | case GEN_RID: |
199 | 4.40k | i2t_ASN1_OBJECT(oline, 256, gen->d.rid); |
200 | 4.40k | if (!X509V3_add_value("Registered ID", oline, &ret)) |
201 | 0 | return NULL; |
202 | 4.40k | break; |
203 | 170k | } |
204 | 138k | return ret; |
205 | 170k | } |
206 | | |
207 | | int GENERAL_NAME_print(BIO *out, GENERAL_NAME *gen) |
208 | 82.0k | { |
209 | 82.0k | char *tmp; |
210 | 82.0k | int nid; |
211 | | |
212 | 82.0k | switch (gen->type) { |
213 | 1.05k | case GEN_OTHERNAME: |
214 | 1.05k | nid = OBJ_obj2nid(gen->d.otherName->type_id); |
215 | | /* Validate the types are as we expect before we use them */ |
216 | 1.05k | if ((nid == NID_SRVName |
217 | 26 | && gen->d.otherName->value->type != V_ASN1_IA5STRING) |
218 | 1.04k | || (nid != NID_SRVName |
219 | 1.03k | && gen->d.otherName->value->type != V_ASN1_UTF8STRING)) { |
220 | 660 | BIO_printf(out, "othername:<unsupported>"); |
221 | 660 | break; |
222 | 660 | } |
223 | | |
224 | 397 | switch (nid) { |
225 | 12 | case NID_id_on_SmtpUTF8Mailbox: |
226 | 12 | BIO_printf(out, "othername:SmtpUTF8Mailbox:%.*s", |
227 | 12 | gen->d.otherName->value->value.utf8string->length, |
228 | 12 | gen->d.otherName->value->value.utf8string->data); |
229 | 12 | break; |
230 | 12 | case NID_XmppAddr: |
231 | 12 | BIO_printf(out, "othername:XmppAddr:%.*s", |
232 | 12 | gen->d.otherName->value->value.utf8string->length, |
233 | 12 | gen->d.otherName->value->value.utf8string->data); |
234 | 12 | break; |
235 | 12 | case NID_SRVName: |
236 | 12 | BIO_printf(out, "othername:SRVName:%.*s", |
237 | 12 | gen->d.otherName->value->value.ia5string->length, |
238 | 12 | gen->d.otherName->value->value.ia5string->data); |
239 | 12 | break; |
240 | 46 | case NID_ms_upn: |
241 | 46 | BIO_printf(out, "othername:UPN:%.*s", |
242 | 46 | gen->d.otherName->value->value.utf8string->length, |
243 | 46 | gen->d.otherName->value->value.utf8string->data); |
244 | 46 | break; |
245 | 12 | case NID_NAIRealm: |
246 | 12 | BIO_printf(out, "othername:NAIRealm:%.*s", |
247 | 12 | gen->d.otherName->value->value.utf8string->length, |
248 | 12 | gen->d.otherName->value->value.utf8string->data); |
249 | 12 | break; |
250 | 303 | default: |
251 | 303 | BIO_printf(out, "othername:<unsupported>"); |
252 | 303 | break; |
253 | 397 | } |
254 | 397 | break; |
255 | | |
256 | 5.34k | case GEN_X400: |
257 | 5.34k | BIO_printf(out, "X400Name:<unsupported>"); |
258 | 5.34k | break; |
259 | | |
260 | 81 | case GEN_EDIPARTY: |
261 | | /* Maybe fix this: it is supported now */ |
262 | 81 | BIO_printf(out, "EdiPartyName:<unsupported>"); |
263 | 81 | break; |
264 | | |
265 | 11.3k | case GEN_EMAIL: |
266 | 11.3k | BIO_printf(out, "email:"); |
267 | 11.3k | ASN1_STRING_print(out, gen->d.ia5); |
268 | 11.3k | break; |
269 | | |
270 | 5.05k | case GEN_DNS: |
271 | 5.05k | BIO_printf(out, "DNS:"); |
272 | 5.05k | ASN1_STRING_print(out, gen->d.ia5); |
273 | 5.05k | break; |
274 | | |
275 | 8.17k | case GEN_URI: |
276 | 8.17k | BIO_printf(out, "URI:"); |
277 | 8.17k | ASN1_STRING_print(out, gen->d.ia5); |
278 | 8.17k | break; |
279 | | |
280 | 25.3k | case GEN_DIRNAME: |
281 | 25.3k | BIO_printf(out, "DirName:"); |
282 | 25.3k | X509_NAME_print_ex(out, gen->d.dirn, 0, XN_FLAG_ONELINE); |
283 | 25.3k | break; |
284 | | |
285 | 19.4k | case GEN_IPADD: |
286 | 19.4k | tmp = ossl_ipaddr_to_asc(gen->d.ip->data, gen->d.ip->length); |
287 | 19.4k | if (tmp == NULL) |
288 | 0 | return 0; |
289 | 19.4k | BIO_printf(out, "IP Address:%s", tmp); |
290 | 19.4k | OPENSSL_free(tmp); |
291 | 19.4k | break; |
292 | | |
293 | 6.15k | case GEN_RID: |
294 | 6.15k | BIO_printf(out, "Registered ID:"); |
295 | 6.15k | i2a_ASN1_OBJECT(out, gen->d.rid); |
296 | 6.15k | break; |
297 | 82.0k | } |
298 | 82.0k | return 1; |
299 | 82.0k | } |
300 | | |
301 | | static GENERAL_NAMES *v2i_issuer_alt(X509V3_EXT_METHOD *method, |
302 | | X509V3_CTX *ctx, |
303 | | STACK_OF(CONF_VALUE) *nval) |
304 | 3.12k | { |
305 | 3.12k | const int num = sk_CONF_VALUE_num(nval); |
306 | 3.12k | GENERAL_NAMES *gens = sk_GENERAL_NAME_new_reserve(NULL, num); |
307 | 3.12k | int i; |
308 | | |
309 | 3.12k | if (gens == NULL) { |
310 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB); |
311 | 0 | sk_GENERAL_NAME_free(gens); |
312 | 0 | return NULL; |
313 | 0 | } |
314 | 92.1k | for (i = 0; i < num; i++) { |
315 | 90.9k | CONF_VALUE *cnf = sk_CONF_VALUE_value(nval, i); |
316 | | |
317 | 90.9k | if (!ossl_v3_name_cmp(cnf->name, "issuer") |
318 | 20.5k | && cnf->value && strcmp(cnf->value, "copy") == 0) { |
319 | 20.4k | if (!copy_issuer(ctx, gens)) |
320 | 1.19k | goto err; |
321 | 70.5k | } else { |
322 | 70.5k | GENERAL_NAME *gen = v2i_GENERAL_NAME(method, ctx, cnf); |
323 | | |
324 | 70.5k | if (gen == NULL) |
325 | 771 | goto err; |
326 | 69.7k | sk_GENERAL_NAME_push(gens, gen); /* no failure as it was reserved */ |
327 | 69.7k | } |
328 | 90.9k | } |
329 | 1.15k | return gens; |
330 | 1.96k | err: |
331 | 1.96k | sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); |
332 | 1.96k | return NULL; |
333 | 3.12k | } |
334 | | |
335 | | /* Append subject altname of issuer to issuer alt name of subject */ |
336 | | |
337 | | static int copy_issuer(X509V3_CTX *ctx, GENERAL_NAMES *gens) |
338 | 20.4k | { |
339 | 20.4k | GENERAL_NAMES *ialt = NULL; |
340 | 20.4k | GENERAL_NAME *gen; |
341 | 20.4k | X509_EXTENSION *ext; |
342 | 20.4k | int i, num; |
343 | | |
344 | 20.4k | if (ctx != NULL && (ctx->flags & X509V3_CTX_TEST) != 0) |
345 | 0 | return 1; |
346 | 20.4k | if (!ctx || !ctx->issuer_cert) { |
347 | 1.10k | ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_ISSUER_DETAILS); |
348 | 1.10k | goto err; |
349 | 1.10k | } |
350 | 19.3k | i = X509_get_ext_by_NID(ctx->issuer_cert, NID_subject_alt_name, -1); |
351 | 19.3k | if (i < 0) |
352 | 3.33k | return 1; |
353 | 15.9k | if ((ext = X509_get_ext(ctx->issuer_cert, i)) == NULL |
354 | 15.9k | || (ialt = X509V3_EXT_d2i(ext)) == NULL) { |
355 | 96 | ERR_raise(ERR_LIB_X509V3, X509V3_R_ISSUER_DECODE_ERROR); |
356 | 96 | goto err; |
357 | 96 | } |
358 | | |
359 | 15.8k | num = sk_GENERAL_NAME_num(ialt); |
360 | 15.8k | if (!sk_GENERAL_NAME_reserve(gens, num)) { |
361 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB); |
362 | 0 | goto err; |
363 | 0 | } |
364 | | |
365 | 1.12M | for (i = 0; i < num; i++) { |
366 | 1.10M | gen = sk_GENERAL_NAME_value(ialt, i); |
367 | 1.10M | sk_GENERAL_NAME_push(gens, gen); /* no failure as it was reserved */ |
368 | 1.10M | } |
369 | 15.8k | sk_GENERAL_NAME_free(ialt); |
370 | | |
371 | 15.8k | return 1; |
372 | | |
373 | 1.19k | err: |
374 | 1.19k | sk_GENERAL_NAME_free(ialt); |
375 | 1.19k | return 0; |
376 | 15.8k | } |
377 | | |
378 | | static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method, |
379 | | X509V3_CTX *ctx, |
380 | | STACK_OF(CONF_VALUE) *nval) |
381 | 1.84k | { |
382 | 1.84k | GENERAL_NAMES *gens; |
383 | 1.84k | CONF_VALUE *cnf; |
384 | 1.84k | const int num = sk_CONF_VALUE_num(nval); |
385 | 1.84k | int i; |
386 | | |
387 | 1.84k | gens = sk_GENERAL_NAME_new_reserve(NULL, num); |
388 | 1.84k | if (gens == NULL) { |
389 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB); |
390 | 0 | sk_GENERAL_NAME_free(gens); |
391 | 0 | return NULL; |
392 | 0 | } |
393 | | |
394 | 190k | for (i = 0; i < num; i++) { |
395 | 188k | cnf = sk_CONF_VALUE_value(nval, i); |
396 | 188k | if (ossl_v3_name_cmp(cnf->name, "email") == 0 |
397 | 5.71k | && cnf->value && strcmp(cnf->value, "copy") == 0) { |
398 | 585 | if (!copy_email(ctx, gens, 0)) |
399 | 71 | goto err; |
400 | 188k | } else if (ossl_v3_name_cmp(cnf->name, "email") == 0 |
401 | 5.12k | && cnf->value && strcmp(cnf->value, "move") == 0) { |
402 | 625 | if (!copy_email(ctx, gens, 1)) |
403 | 54 | goto err; |
404 | 187k | } else { |
405 | 187k | GENERAL_NAME *gen; |
406 | 187k | if ((gen = v2i_GENERAL_NAME(method, ctx, cnf)) == NULL) |
407 | 529 | goto err; |
408 | 187k | sk_GENERAL_NAME_push(gens, gen); /* no failure as it was reserved */ |
409 | 187k | } |
410 | 188k | } |
411 | 1.18k | return gens; |
412 | 654 | err: |
413 | 654 | sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); |
414 | 654 | return NULL; |
415 | 1.84k | } |
416 | | |
417 | | /* |
418 | | * Copy any email addresses in a certificate or request to GENERAL_NAMES |
419 | | */ |
420 | | |
421 | | static int copy_email(X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p) |
422 | | { |
423 | | X509_NAME *nm; |
424 | | ASN1_IA5STRING *email = NULL; |
425 | | X509_NAME_ENTRY *ne; |
426 | | GENERAL_NAME *gen = NULL; |
427 | | int i = -1; |
428 | | |
429 | | if (ctx != NULL && (ctx->flags & X509V3_CTX_TEST) != 0) |
430 | | return 1; |
431 | | if (ctx == NULL |
432 | | || (ctx->subject_cert == NULL && ctx->subject_req == NULL)) { |
433 | | ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_SUBJECT_DETAILS); |
434 | | return 0; |
435 | | } |
436 | | /* Find the subject name */ |
437 | | nm = ctx->subject_cert != NULL ? X509_get_subject_name(ctx->subject_cert) : X509_REQ_get_subject_name(ctx->subject_req); |
438 | | |
439 | | /* Now add any email address(es) to STACK */ |
440 | | while ((i = X509_NAME_get_index_by_NID(nm, |
441 | | NID_pkcs9_emailAddress, i)) |
442 | | >= 0) { |
443 | | ne = X509_NAME_get_entry(nm, i); |
444 | | email = ASN1_STRING_dup(X509_NAME_ENTRY_get_data(ne)); |
445 | | if (move_p) { |
446 | | X509_NAME_delete_entry(nm, i); |
447 | | X509_NAME_ENTRY_free(ne); |
448 | | i--; |
449 | | } |
450 | | if (email == NULL || (gen = GENERAL_NAME_new()) == NULL) { |
451 | | ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB); |
452 | | goto err; |
453 | | } |
454 | | gen->d.ia5 = email; |
455 | | email = NULL; |
456 | | gen->type = GEN_EMAIL; |
457 | | if (!sk_GENERAL_NAME_push(gens, gen)) { |
458 | | ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB); |
459 | | goto err; |
460 | | } |
461 | | gen = NULL; |
462 | | } |
463 | | |
464 | | return 1; |
465 | | |
466 | | err: |
467 | | GENERAL_NAME_free(gen); |
468 | | ASN1_IA5STRING_free(email); |
469 | | return 0; |
470 | | } |
471 | | |
472 | | GENERAL_NAMES *v2i_GENERAL_NAMES(const X509V3_EXT_METHOD *method, |
473 | | X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval) |
474 | 10.4k | { |
475 | 10.4k | GENERAL_NAME *gen; |
476 | 10.4k | GENERAL_NAMES *gens; |
477 | 10.4k | CONF_VALUE *cnf; |
478 | 10.4k | const int num = sk_CONF_VALUE_num(nval); |
479 | 10.4k | int i; |
480 | | |
481 | 10.4k | gens = sk_GENERAL_NAME_new_reserve(NULL, num); |
482 | 10.4k | if (gens == NULL) { |
483 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB); |
484 | 0 | sk_GENERAL_NAME_free(gens); |
485 | 0 | return NULL; |
486 | 0 | } |
487 | | |
488 | 31.4k | for (i = 0; i < num; i++) { |
489 | 21.1k | cnf = sk_CONF_VALUE_value(nval, i); |
490 | 21.1k | if ((gen = v2i_GENERAL_NAME(method, ctx, cnf)) == NULL) |
491 | 42 | goto err; |
492 | 21.0k | sk_GENERAL_NAME_push(gens, gen); /* no failure as it was reserved */ |
493 | 21.0k | } |
494 | 10.3k | return gens; |
495 | 42 | err: |
496 | 42 | sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); |
497 | 42 | return NULL; |
498 | 10.4k | } |
499 | | |
500 | | GENERAL_NAME *v2i_GENERAL_NAME(const X509V3_EXT_METHOD *method, |
501 | | X509V3_CTX *ctx, CONF_VALUE *cnf) |
502 | 355k | { |
503 | 355k | return v2i_GENERAL_NAME_ex(NULL, method, ctx, cnf, 0); |
504 | 355k | } |
505 | | |
506 | | GENERAL_NAME *a2i_GENERAL_NAME(GENERAL_NAME *out, |
507 | | const X509V3_EXT_METHOD *method, |
508 | | X509V3_CTX *ctx, int gen_type, const char *value, |
509 | | int is_nc) |
510 | 378k | { |
511 | 378k | char is_string = 0; |
512 | 378k | GENERAL_NAME *gen = NULL; |
513 | | |
514 | 378k | if (!value) { |
515 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_MISSING_VALUE); |
516 | 0 | return NULL; |
517 | 0 | } |
518 | | |
519 | 378k | if (out) |
520 | 23.9k | gen = out; |
521 | 354k | else { |
522 | 354k | gen = GENERAL_NAME_new(); |
523 | 354k | if (gen == NULL) { |
524 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB); |
525 | 0 | return NULL; |
526 | 0 | } |
527 | 354k | } |
528 | | |
529 | 378k | switch (gen_type) { |
530 | 263k | case GEN_URI: |
531 | 290k | case GEN_EMAIL: |
532 | 292k | case GEN_DNS: |
533 | 292k | is_string = 1; |
534 | 292k | break; |
535 | | |
536 | 53.8k | case GEN_RID: { |
537 | 53.8k | ASN1_OBJECT *obj; |
538 | 53.8k | if ((obj = OBJ_txt2obj(value, 0)) == NULL) { |
539 | 159 | ERR_raise_data(ERR_LIB_X509V3, X509V3_R_BAD_OBJECT, |
540 | 159 | "value=%s", value); |
541 | 159 | goto err; |
542 | 159 | } |
543 | 53.6k | gen->d.rid = obj; |
544 | 53.6k | } break; |
545 | | |
546 | 3.49k | case GEN_IPADD: |
547 | 3.49k | if (is_nc) |
548 | 1.33k | gen->d.ip = a2i_IPADDRESS_NC(value); |
549 | 2.16k | else |
550 | 2.16k | gen->d.ip = a2i_IPADDRESS(value); |
551 | 3.49k | if (gen->d.ip == NULL) { |
552 | 544 | ERR_raise_data(ERR_LIB_X509V3, X509V3_R_BAD_IP_ADDRESS, |
553 | 544 | "value=%s", value); |
554 | 544 | goto err; |
555 | 544 | } |
556 | 2.95k | break; |
557 | | |
558 | 28.8k | case GEN_DIRNAME: |
559 | 28.8k | if (!do_dirname(gen, value, ctx)) { |
560 | 162 | ERR_raise(ERR_LIB_X509V3, X509V3_R_DIRNAME_ERROR); |
561 | 162 | goto err; |
562 | 162 | } |
563 | 28.7k | break; |
564 | | |
565 | 28.7k | case GEN_OTHERNAME: |
566 | 342 | if (!do_othername(gen, value, ctx)) { |
567 | 86 | ERR_raise(ERR_LIB_X509V3, X509V3_R_OTHERNAME_ERROR); |
568 | 86 | goto err; |
569 | 86 | } |
570 | 256 | break; |
571 | 256 | default: |
572 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_UNSUPPORTED_TYPE); |
573 | 0 | goto err; |
574 | 378k | } |
575 | | |
576 | 377k | if (is_string) { |
577 | 292k | if ((gen->d.ia5 = ASN1_IA5STRING_new()) == NULL || !ASN1_STRING_set(gen->d.ia5, (unsigned char *)value, strlen(value))) { |
578 | 0 | ASN1_IA5STRING_free(gen->d.ia5); |
579 | 0 | gen->d.ia5 = NULL; |
580 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB); |
581 | 0 | goto err; |
582 | 0 | } |
583 | 292k | } |
584 | | |
585 | 377k | gen->type = gen_type; |
586 | | |
587 | 377k | return gen; |
588 | | |
589 | 951 | err: |
590 | 951 | if (!out) |
591 | 809 | GENERAL_NAME_free(gen); |
592 | 951 | return NULL; |
593 | 377k | } |
594 | | |
595 | | GENERAL_NAME *v2i_GENERAL_NAME_ex(GENERAL_NAME *out, |
596 | | const X509V3_EXT_METHOD *method, |
597 | | X509V3_CTX *ctx, CONF_VALUE *cnf, int is_nc) |
598 | 379k | { |
599 | 379k | int type; |
600 | | |
601 | 379k | char *name, *value; |
602 | | |
603 | 379k | name = cnf->name; |
604 | 379k | value = cnf->value; |
605 | | |
606 | 379k | if (!value) { |
607 | 433 | ERR_raise(ERR_LIB_X509V3, X509V3_R_MISSING_VALUE); |
608 | 433 | return NULL; |
609 | 433 | } |
610 | | |
611 | 379k | if (!ossl_v3_name_cmp(name, "email")) |
612 | 27.1k | type = GEN_EMAIL; |
613 | 352k | else if (!ossl_v3_name_cmp(name, "URI")) |
614 | 263k | type = GEN_URI; |
615 | 88.5k | else if (!ossl_v3_name_cmp(name, "DNS")) |
616 | 1.71k | type = GEN_DNS; |
617 | 86.8k | else if (!ossl_v3_name_cmp(name, "RID")) |
618 | 53.8k | type = GEN_RID; |
619 | 33.0k | else if (!ossl_v3_name_cmp(name, "IP")) |
620 | 3.49k | type = GEN_IPADD; |
621 | 29.5k | else if (!ossl_v3_name_cmp(name, "dirName")) |
622 | 28.8k | type = GEN_DIRNAME; |
623 | 657 | else if (!ossl_v3_name_cmp(name, "otherName")) |
624 | 342 | type = GEN_OTHERNAME; |
625 | 315 | else { |
626 | 315 | ERR_raise_data(ERR_LIB_X509V3, X509V3_R_UNSUPPORTED_OPTION, |
627 | 315 | "name=%s", name); |
628 | 315 | return NULL; |
629 | 315 | } |
630 | | |
631 | 378k | return a2i_GENERAL_NAME(out, method, ctx, type, value, is_nc); |
632 | 379k | } |
633 | | |
634 | | static int do_othername(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx) |
635 | 342 | { |
636 | 342 | char *objtmp = NULL; |
637 | 342 | const char *p; |
638 | 342 | int objlen; |
639 | | |
640 | 342 | if ((p = strchr(value, ';')) == NULL) |
641 | 6 | return 0; |
642 | 336 | if ((gen->d.otherName = OTHERNAME_new()) == NULL) |
643 | 0 | return 0; |
644 | | /* |
645 | | * Free this up because we will overwrite it. no need to free type_id |
646 | | * because it is static |
647 | | */ |
648 | 336 | ASN1_TYPE_free(gen->d.otherName->value); |
649 | 336 | if ((gen->d.otherName->value = ASN1_generate_v3(p + 1, ctx)) == NULL) |
650 | 27 | goto err; |
651 | 309 | objlen = p - value; |
652 | 309 | objtmp = OPENSSL_strndup(value, objlen); |
653 | 309 | if (objtmp == NULL) |
654 | 0 | goto err; |
655 | 309 | gen->d.otherName->type_id = OBJ_txt2obj(objtmp, 0); |
656 | 309 | OPENSSL_free(objtmp); |
657 | 309 | if (!gen->d.otherName->type_id) |
658 | 53 | goto err; |
659 | 256 | return 1; |
660 | | |
661 | 80 | err: |
662 | 80 | OTHERNAME_free(gen->d.otherName); |
663 | 80 | gen->d.otherName = NULL; |
664 | 80 | return 0; |
665 | 309 | } |
666 | | |
667 | | static int do_dirname(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx) |
668 | 28.8k | { |
669 | 28.8k | int ret = 0; |
670 | 28.8k | STACK_OF(CONF_VALUE) *sk = NULL; |
671 | 28.8k | X509_NAME *nm; |
672 | | |
673 | 28.8k | if ((nm = X509_NAME_new()) == NULL) |
674 | 0 | goto err; |
675 | 28.8k | sk = X509V3_get_section(ctx, value); |
676 | 28.8k | if (!sk) { |
677 | 14 | ERR_raise_data(ERR_LIB_X509V3, X509V3_R_SECTION_NOT_FOUND, |
678 | 14 | "section=%s", value); |
679 | 14 | goto err; |
680 | 14 | } |
681 | | /* FIXME: should allow other character types... */ |
682 | 28.8k | ret = X509V3_NAME_from_section(nm, sk, MBSTRING_ASC); |
683 | 28.8k | if (!ret) |
684 | 148 | goto err; |
685 | 28.7k | gen->d.dirn = nm; |
686 | | |
687 | 28.8k | err: |
688 | 28.8k | if (ret == 0) |
689 | 162 | X509_NAME_free(nm); |
690 | 28.8k | X509V3_section_free(ctx, sk); |
691 | 28.8k | return ret; |
692 | 28.7k | } |