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