/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 | 8.30k | { |
55 | 8.30k | int i; |
56 | 8.30k | GENERAL_NAME *gen; |
57 | 8.30k | STACK_OF(CONF_VALUE) *tmpret = NULL, *origret = ret; |
58 | | |
59 | 16.0k | for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { |
60 | 8.86k | 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 | 8.86k | tmpret = i2v_GENERAL_NAME(method, gen, ret); |
67 | 8.86k | if (tmpret == NULL) { |
68 | 1.14k | if (origret == NULL) |
69 | 950 | sk_CONF_VALUE_pop_free(ret, X509V3_conf_free); |
70 | 1.14k | return NULL; |
71 | 1.14k | } |
72 | 7.72k | ret = tmpret; |
73 | 7.72k | } |
74 | 7.16k | if (ret == NULL) |
75 | 2.58k | return sk_CONF_VALUE_new_null(); |
76 | 4.58k | return ret; |
77 | 7.16k | } |
78 | | |
79 | | STACK_OF(CONF_VALUE) *i2v_GENERAL_NAME(X509V3_EXT_METHOD *method, |
80 | | GENERAL_NAME *gen, |
81 | | STACK_OF(CONF_VALUE) *ret) |
82 | 9.94k | { |
83 | 9.94k | char othername[300]; |
84 | 9.94k | char oline[256], *tmp; |
85 | | |
86 | 9.94k | switch (gen->type) { |
87 | 1.65k | case GEN_OTHERNAME: |
88 | 1.65k | switch (OBJ_obj2nid(gen->d.otherName->type_id)) { |
89 | 0 | case NID_id_on_SmtpUTF8Mailbox: |
90 | 0 | if (gen->d.otherName->value->type != V_ASN1_UTF8STRING |
91 | 0 | || !x509v3_add_len_value_uchar("othername: SmtpUTF8Mailbox:", |
92 | 0 | gen->d.otherName->value->value.utf8string->data, |
93 | 0 | gen->d.otherName->value->value.utf8string->length, |
94 | 0 | &ret)) |
95 | 0 | return NULL; |
96 | 0 | break; |
97 | 0 | case NID_XmppAddr: |
98 | 0 | if (gen->d.otherName->value->type != V_ASN1_UTF8STRING |
99 | 0 | || !x509v3_add_len_value_uchar("othername: XmppAddr:", |
100 | 0 | gen->d.otherName->value->value.utf8string->data, |
101 | 0 | gen->d.otherName->value->value.utf8string->length, |
102 | 0 | &ret)) |
103 | 0 | return NULL; |
104 | 0 | break; |
105 | 0 | case NID_SRVName: |
106 | 0 | if (gen->d.otherName->value->type != V_ASN1_IA5STRING |
107 | 0 | || !x509v3_add_len_value_uchar("othername: SRVName:", |
108 | 0 | gen->d.otherName->value->value.ia5string->data, |
109 | 0 | gen->d.otherName->value->value.ia5string->length, |
110 | 0 | &ret)) |
111 | 0 | return NULL; |
112 | 0 | break; |
113 | 0 | case NID_ms_upn: |
114 | 0 | if (gen->d.otherName->value->type != V_ASN1_UTF8STRING |
115 | 0 | || !x509v3_add_len_value_uchar("othername: UPN:", |
116 | 0 | gen->d.otherName->value->value.utf8string->data, |
117 | 0 | gen->d.otherName->value->value.utf8string->length, |
118 | 0 | &ret)) |
119 | 0 | return NULL; |
120 | 0 | break; |
121 | 0 | case NID_NAIRealm: |
122 | 0 | if (gen->d.otherName->value->type != V_ASN1_UTF8STRING |
123 | 0 | || !x509v3_add_len_value_uchar("othername: NAIRealm:", |
124 | 0 | gen->d.otherName->value->value.utf8string->data, |
125 | 0 | gen->d.otherName->value->value.utf8string->length, |
126 | 0 | &ret)) |
127 | 0 | return NULL; |
128 | 0 | break; |
129 | 1.65k | default: |
130 | 1.65k | if (OBJ_obj2txt(oline, sizeof(oline), gen->d.otherName->type_id, 0) > 0) |
131 | 1.65k | BIO_snprintf(othername, sizeof(othername), "othername: %s:", |
132 | 1.65k | oline); |
133 | 0 | else |
134 | 0 | OPENSSL_strlcpy(othername, "othername:", sizeof(othername)); |
135 | | |
136 | | /* check if the value is something printable */ |
137 | 1.65k | if (gen->d.otherName->value->type == V_ASN1_IA5STRING) { |
138 | 327 | if (x509v3_add_len_value_uchar(othername, |
139 | 327 | gen->d.otherName->value->value.ia5string->data, |
140 | 327 | gen->d.otherName->value->value.ia5string->length, |
141 | 327 | &ret)) |
142 | 293 | return ret; |
143 | 327 | } |
144 | 1.36k | if (gen->d.otherName->value->type == V_ASN1_UTF8STRING) { |
145 | 588 | if (x509v3_add_len_value_uchar(othername, |
146 | 588 | gen->d.otherName->value->value.utf8string->data, |
147 | 588 | gen->d.otherName->value->value.utf8string->length, |
148 | 588 | &ret)) |
149 | 553 | return ret; |
150 | 588 | } |
151 | 813 | if (!X509V3_add_value(othername, "<unsupported>", &ret)) |
152 | 0 | return NULL; |
153 | 813 | break; |
154 | 1.65k | } |
155 | 813 | break; |
156 | | |
157 | 1.25k | case GEN_X400: |
158 | 1.25k | if (!X509V3_add_value("X400Name", "<unsupported>", &ret)) |
159 | 0 | return NULL; |
160 | 1.25k | break; |
161 | | |
162 | 1.25k | case GEN_EDIPARTY: |
163 | 227 | if (!X509V3_add_value("EdiPartyName", "<unsupported>", &ret)) |
164 | 0 | return NULL; |
165 | 227 | break; |
166 | | |
167 | 2.34k | case GEN_EMAIL: |
168 | 2.34k | if (!x509v3_add_len_value_uchar("email", gen->d.ia5->data, |
169 | 2.34k | gen->d.ia5->length, &ret)) |
170 | 304 | return NULL; |
171 | 2.04k | break; |
172 | | |
173 | 2.04k | case GEN_DNS: |
174 | 692 | if (!x509v3_add_len_value_uchar("DNS", gen->d.ia5->data, |
175 | 692 | gen->d.ia5->length, &ret)) |
176 | 334 | return NULL; |
177 | 358 | break; |
178 | | |
179 | 1.54k | case GEN_URI: |
180 | 1.54k | if (!x509v3_add_len_value_uchar("URI", gen->d.ia5->data, |
181 | 1.54k | gen->d.ia5->length, &ret)) |
182 | 534 | return NULL; |
183 | 1.01k | break; |
184 | | |
185 | 1.01k | case GEN_DIRNAME: |
186 | 504 | if (X509_NAME_oneline(gen->d.dirn, oline, sizeof(oline)) == NULL |
187 | 504 | || !X509V3_add_value("DirName", oline, &ret)) |
188 | 0 | return NULL; |
189 | 504 | break; |
190 | | |
191 | 1.20k | case GEN_IPADD: |
192 | 1.20k | tmp = ossl_ipaddr_to_asc(gen->d.ip->data, gen->d.ip->length); |
193 | 1.20k | if (tmp == NULL || !X509V3_add_value("IP Address", tmp, &ret)) |
194 | 0 | ret = NULL; |
195 | 1.20k | OPENSSL_free(tmp); |
196 | 1.20k | break; |
197 | | |
198 | 504 | case GEN_RID: |
199 | 504 | i2t_ASN1_OBJECT(oline, 256, gen->d.rid); |
200 | 504 | if (!X509V3_add_value("Registered ID", oline, &ret)) |
201 | 0 | return NULL; |
202 | 504 | break; |
203 | 9.94k | } |
204 | 7.92k | return ret; |
205 | 9.94k | } |
206 | | |
207 | | int GENERAL_NAME_print(BIO *out, GENERAL_NAME *gen) |
208 | 5.01k | { |
209 | 5.01k | char *tmp; |
210 | 5.01k | int nid; |
211 | | |
212 | 5.01k | switch (gen->type) { |
213 | 1.62k | case GEN_OTHERNAME: |
214 | 1.62k | nid = OBJ_obj2nid(gen->d.otherName->type_id); |
215 | | /* Validate the types are as we expect before we use them */ |
216 | 1.62k | if ((nid == NID_SRVName |
217 | 1.62k | && gen->d.otherName->value->type != V_ASN1_IA5STRING) |
218 | 1.62k | || (nid != NID_SRVName |
219 | 1.62k | && gen->d.otherName->value->type != V_ASN1_UTF8STRING)) { |
220 | 1.52k | BIO_printf(out, "othername:<unsupported>"); |
221 | 1.52k | break; |
222 | 1.52k | } |
223 | | |
224 | 101 | switch (nid) { |
225 | 0 | case NID_id_on_SmtpUTF8Mailbox: |
226 | 0 | BIO_printf(out, "othername:SmtpUTF8Mailbox:%.*s", |
227 | 0 | gen->d.otherName->value->value.utf8string->length, |
228 | 0 | gen->d.otherName->value->value.utf8string->data); |
229 | 0 | break; |
230 | 0 | case NID_XmppAddr: |
231 | 0 | BIO_printf(out, "othername:XmppAddr:%.*s", |
232 | 0 | gen->d.otherName->value->value.utf8string->length, |
233 | 0 | gen->d.otherName->value->value.utf8string->data); |
234 | 0 | break; |
235 | 0 | case NID_SRVName: |
236 | 0 | BIO_printf(out, "othername:SRVName:%.*s", |
237 | 0 | gen->d.otherName->value->value.ia5string->length, |
238 | 0 | gen->d.otherName->value->value.ia5string->data); |
239 | 0 | break; |
240 | 0 | case NID_ms_upn: |
241 | 0 | BIO_printf(out, "othername:UPN:%.*s", |
242 | 0 | gen->d.otherName->value->value.utf8string->length, |
243 | 0 | gen->d.otherName->value->value.utf8string->data); |
244 | 0 | break; |
245 | 0 | case NID_NAIRealm: |
246 | 0 | BIO_printf(out, "othername:NAIRealm:%.*s", |
247 | 0 | gen->d.otherName->value->value.utf8string->length, |
248 | 0 | gen->d.otherName->value->value.utf8string->data); |
249 | 0 | break; |
250 | 101 | default: |
251 | 101 | BIO_printf(out, "othername:<unsupported>"); |
252 | 101 | break; |
253 | 101 | } |
254 | 101 | break; |
255 | | |
256 | 347 | case GEN_X400: |
257 | 347 | BIO_printf(out, "X400Name:<unsupported>"); |
258 | 347 | break; |
259 | | |
260 | 34 | case GEN_EDIPARTY: |
261 | | /* Maybe fix this: it is supported now */ |
262 | 34 | BIO_printf(out, "EdiPartyName:<unsupported>"); |
263 | 34 | break; |
264 | | |
265 | 492 | case GEN_EMAIL: |
266 | 492 | BIO_printf(out, "email:"); |
267 | 492 | ASN1_STRING_print(out, gen->d.ia5); |
268 | 492 | break; |
269 | | |
270 | 288 | case GEN_DNS: |
271 | 288 | BIO_printf(out, "DNS:"); |
272 | 288 | ASN1_STRING_print(out, gen->d.ia5); |
273 | 288 | break; |
274 | | |
275 | 539 | case GEN_URI: |
276 | 539 | BIO_printf(out, "URI:"); |
277 | 539 | ASN1_STRING_print(out, gen->d.ia5); |
278 | 539 | break; |
279 | | |
280 | 959 | case GEN_DIRNAME: |
281 | 959 | BIO_printf(out, "DirName:"); |
282 | 959 | X509_NAME_print_ex(out, gen->d.dirn, 0, XN_FLAG_ONELINE); |
283 | 959 | break; |
284 | | |
285 | 505 | case GEN_IPADD: |
286 | 505 | tmp = ossl_ipaddr_to_asc(gen->d.ip->data, gen->d.ip->length); |
287 | 505 | if (tmp == NULL) |
288 | 0 | return 0; |
289 | 505 | BIO_printf(out, "IP Address:%s", tmp); |
290 | 505 | OPENSSL_free(tmp); |
291 | 505 | break; |
292 | | |
293 | 223 | case GEN_RID: |
294 | 223 | BIO_printf(out, "Registered ID:"); |
295 | 223 | i2a_ASN1_OBJECT(out, gen->d.rid); |
296 | 223 | break; |
297 | 5.01k | } |
298 | 5.01k | return 1; |
299 | 5.01k | } |
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; |
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 | return 0; |
375 | |
|
376 | 0 | } |
377 | | |
378 | | static GENERAL_NAMES *v2i_subject_alt(X509V3_EXT_METHOD *method, |
379 | | X509V3_CTX *ctx, |
380 | | STACK_OF(CONF_VALUE) *nval) |
381 | 0 | { |
382 | 0 | GENERAL_NAMES *gens; |
383 | 0 | CONF_VALUE *cnf; |
384 | 0 | const int num = sk_CONF_VALUE_num(nval); |
385 | 0 | int i; |
386 | |
|
387 | 0 | gens = sk_GENERAL_NAME_new_reserve(NULL, num); |
388 | 0 | if (gens == NULL) { |
389 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE); |
390 | 0 | sk_GENERAL_NAME_free(gens); |
391 | 0 | return NULL; |
392 | 0 | } |
393 | | |
394 | 0 | for (i = 0; i < num; i++) { |
395 | 0 | cnf = sk_CONF_VALUE_value(nval, i); |
396 | 0 | if (ossl_v3_name_cmp(cnf->name, "email") == 0 |
397 | 0 | && cnf->value && strcmp(cnf->value, "copy") == 0) { |
398 | 0 | if (!copy_email(ctx, gens, 0)) |
399 | 0 | goto err; |
400 | 0 | } else if (ossl_v3_name_cmp(cnf->name, "email") == 0 |
401 | 0 | && cnf->value && strcmp(cnf->value, "move") == 0) { |
402 | 0 | if (!copy_email(ctx, gens, 1)) |
403 | 0 | goto err; |
404 | 0 | } else { |
405 | 0 | GENERAL_NAME *gen; |
406 | 0 | if ((gen = v2i_GENERAL_NAME(method, ctx, cnf)) == NULL) |
407 | 0 | goto err; |
408 | 0 | sk_GENERAL_NAME_push(gens, gen); /* no failure as it was reserved */ |
409 | 0 | } |
410 | 0 | } |
411 | 0 | return gens; |
412 | 0 | err: |
413 | 0 | sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); |
414 | 0 | return NULL; |
415 | 0 | } |
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 | 0 | { |
423 | 0 | X509_NAME *nm; |
424 | 0 | ASN1_IA5STRING *email = NULL; |
425 | 0 | X509_NAME_ENTRY *ne; |
426 | 0 | GENERAL_NAME *gen = NULL; |
427 | 0 | int i = -1; |
428 | |
|
429 | 0 | if (ctx != NULL && (ctx->flags & X509V3_CTX_TEST) != 0) |
430 | 0 | return 1; |
431 | 0 | if (ctx == NULL |
432 | 0 | || (ctx->subject_cert == NULL && ctx->subject_req == NULL)) { |
433 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_NO_SUBJECT_DETAILS); |
434 | 0 | return 0; |
435 | 0 | } |
436 | | /* Find the subject name */ |
437 | 0 | nm = ctx->subject_cert != NULL ? |
438 | 0 | X509_get_subject_name(ctx->subject_cert) : |
439 | 0 | X509_REQ_get_subject_name(ctx->subject_req); |
440 | | |
441 | | /* Now add any email address(es) to STACK */ |
442 | 0 | while ((i = X509_NAME_get_index_by_NID(nm, |
443 | 0 | NID_pkcs9_emailAddress, i)) >= 0) { |
444 | 0 | ne = X509_NAME_get_entry(nm, i); |
445 | 0 | email = ASN1_STRING_dup(X509_NAME_ENTRY_get_data(ne)); |
446 | 0 | if (move_p) { |
447 | 0 | X509_NAME_delete_entry(nm, i); |
448 | 0 | X509_NAME_ENTRY_free(ne); |
449 | 0 | i--; |
450 | 0 | } |
451 | 0 | if (email == NULL || (gen = GENERAL_NAME_new()) == NULL) { |
452 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE); |
453 | 0 | goto err; |
454 | 0 | } |
455 | 0 | gen->d.ia5 = email; |
456 | 0 | email = NULL; |
457 | 0 | gen->type = GEN_EMAIL; |
458 | 0 | if (!sk_GENERAL_NAME_push(gens, gen)) { |
459 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE); |
460 | 0 | goto err; |
461 | 0 | } |
462 | 0 | gen = NULL; |
463 | 0 | } |
464 | | |
465 | 0 | return 1; |
466 | | |
467 | 0 | err: |
468 | 0 | GENERAL_NAME_free(gen); |
469 | 0 | ASN1_IA5STRING_free(email); |
470 | 0 | return 0; |
471 | |
|
472 | 0 | } |
473 | | |
474 | | GENERAL_NAMES *v2i_GENERAL_NAMES(const X509V3_EXT_METHOD *method, |
475 | | X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval) |
476 | 0 | { |
477 | 0 | GENERAL_NAME *gen; |
478 | 0 | GENERAL_NAMES *gens; |
479 | 0 | CONF_VALUE *cnf; |
480 | 0 | const int num = sk_CONF_VALUE_num(nval); |
481 | 0 | int i; |
482 | |
|
483 | 0 | gens = sk_GENERAL_NAME_new_reserve(NULL, num); |
484 | 0 | if (gens == NULL) { |
485 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE); |
486 | 0 | sk_GENERAL_NAME_free(gens); |
487 | 0 | return NULL; |
488 | 0 | } |
489 | | |
490 | 0 | for (i = 0; i < num; i++) { |
491 | 0 | cnf = sk_CONF_VALUE_value(nval, i); |
492 | 0 | if ((gen = v2i_GENERAL_NAME(method, ctx, cnf)) == NULL) |
493 | 0 | goto err; |
494 | 0 | sk_GENERAL_NAME_push(gens, gen); /* no failure as it was reserved */ |
495 | 0 | } |
496 | 0 | return gens; |
497 | 0 | err: |
498 | 0 | sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); |
499 | 0 | return NULL; |
500 | 0 | } |
501 | | |
502 | | GENERAL_NAME *v2i_GENERAL_NAME(const X509V3_EXT_METHOD *method, |
503 | | X509V3_CTX *ctx, CONF_VALUE *cnf) |
504 | 0 | { |
505 | 0 | return v2i_GENERAL_NAME_ex(NULL, method, ctx, cnf, 0); |
506 | 0 | } |
507 | | |
508 | | GENERAL_NAME *a2i_GENERAL_NAME(GENERAL_NAME *out, |
509 | | const X509V3_EXT_METHOD *method, |
510 | | X509V3_CTX *ctx, int gen_type, const char *value, |
511 | | int is_nc) |
512 | 0 | { |
513 | 0 | char is_string = 0; |
514 | 0 | GENERAL_NAME *gen = NULL; |
515 | |
|
516 | 0 | if (!value) { |
517 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_MISSING_VALUE); |
518 | 0 | return NULL; |
519 | 0 | } |
520 | | |
521 | 0 | if (out) |
522 | 0 | gen = out; |
523 | 0 | else { |
524 | 0 | gen = GENERAL_NAME_new(); |
525 | 0 | if (gen == NULL) { |
526 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE); |
527 | 0 | return NULL; |
528 | 0 | } |
529 | 0 | } |
530 | | |
531 | 0 | switch (gen_type) { |
532 | 0 | case GEN_URI: |
533 | 0 | case GEN_EMAIL: |
534 | 0 | case GEN_DNS: |
535 | 0 | is_string = 1; |
536 | 0 | break; |
537 | | |
538 | 0 | case GEN_RID: |
539 | 0 | { |
540 | 0 | ASN1_OBJECT *obj; |
541 | 0 | if ((obj = OBJ_txt2obj(value, 0)) == NULL) { |
542 | 0 | ERR_raise_data(ERR_LIB_X509V3, X509V3_R_BAD_OBJECT, |
543 | 0 | "value=%s", value); |
544 | 0 | goto err; |
545 | 0 | } |
546 | 0 | gen->d.rid = obj; |
547 | 0 | } |
548 | 0 | break; |
549 | | |
550 | 0 | case GEN_IPADD: |
551 | 0 | if (is_nc) |
552 | 0 | gen->d.ip = a2i_IPADDRESS_NC(value); |
553 | 0 | else |
554 | 0 | gen->d.ip = a2i_IPADDRESS(value); |
555 | 0 | if (gen->d.ip == NULL) { |
556 | 0 | ERR_raise_data(ERR_LIB_X509V3, X509V3_R_BAD_IP_ADDRESS, |
557 | 0 | "value=%s", value); |
558 | 0 | goto err; |
559 | 0 | } |
560 | 0 | break; |
561 | | |
562 | 0 | case GEN_DIRNAME: |
563 | 0 | if (!do_dirname(gen, value, ctx)) { |
564 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_DIRNAME_ERROR); |
565 | 0 | goto err; |
566 | 0 | } |
567 | 0 | break; |
568 | | |
569 | 0 | case GEN_OTHERNAME: |
570 | 0 | if (!do_othername(gen, value, ctx)) { |
571 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_OTHERNAME_ERROR); |
572 | 0 | goto err; |
573 | 0 | } |
574 | 0 | break; |
575 | 0 | default: |
576 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_UNSUPPORTED_TYPE); |
577 | 0 | goto err; |
578 | 0 | } |
579 | | |
580 | 0 | if (is_string) { |
581 | 0 | if ((gen->d.ia5 = ASN1_IA5STRING_new()) == NULL || |
582 | 0 | !ASN1_STRING_set(gen->d.ia5, (unsigned char *)value, |
583 | 0 | strlen(value))) { |
584 | 0 | ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE); |
585 | 0 | goto err; |
586 | 0 | } |
587 | 0 | } |
588 | | |
589 | 0 | gen->type = gen_type; |
590 | |
|
591 | 0 | return gen; |
592 | | |
593 | 0 | err: |
594 | 0 | if (!out) |
595 | 0 | GENERAL_NAME_free(gen); |
596 | 0 | return NULL; |
597 | 0 | } |
598 | | |
599 | | GENERAL_NAME *v2i_GENERAL_NAME_ex(GENERAL_NAME *out, |
600 | | const X509V3_EXT_METHOD *method, |
601 | | X509V3_CTX *ctx, CONF_VALUE *cnf, int is_nc) |
602 | 0 | { |
603 | 0 | int type; |
604 | |
|
605 | 0 | char *name, *value; |
606 | |
|
607 | 0 | name = cnf->name; |
608 | 0 | value = cnf->value; |
609 | |
|
610 | 0 | if (!value) { |
611 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_MISSING_VALUE); |
612 | 0 | return NULL; |
613 | 0 | } |
614 | | |
615 | 0 | if (!ossl_v3_name_cmp(name, "email")) |
616 | 0 | type = GEN_EMAIL; |
617 | 0 | else if (!ossl_v3_name_cmp(name, "URI")) |
618 | 0 | type = GEN_URI; |
619 | 0 | else if (!ossl_v3_name_cmp(name, "DNS")) |
620 | 0 | type = GEN_DNS; |
621 | 0 | else if (!ossl_v3_name_cmp(name, "RID")) |
622 | 0 | type = GEN_RID; |
623 | 0 | else if (!ossl_v3_name_cmp(name, "IP")) |
624 | 0 | type = GEN_IPADD; |
625 | 0 | else if (!ossl_v3_name_cmp(name, "dirName")) |
626 | 0 | type = GEN_DIRNAME; |
627 | 0 | else if (!ossl_v3_name_cmp(name, "otherName")) |
628 | 0 | type = GEN_OTHERNAME; |
629 | 0 | else { |
630 | 0 | ERR_raise_data(ERR_LIB_X509V3, X509V3_R_UNSUPPORTED_OPTION, |
631 | 0 | "name=%s", name); |
632 | 0 | return NULL; |
633 | 0 | } |
634 | | |
635 | 0 | return a2i_GENERAL_NAME(out, method, ctx, type, value, is_nc); |
636 | |
|
637 | 0 | } |
638 | | |
639 | | static int do_othername(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx) |
640 | 0 | { |
641 | 0 | char *objtmp = NULL, *p; |
642 | 0 | int objlen; |
643 | |
|
644 | 0 | if ((p = strchr(value, ';')) == NULL) |
645 | 0 | return 0; |
646 | 0 | if ((gen->d.otherName = OTHERNAME_new()) == NULL) |
647 | 0 | return 0; |
648 | | /* |
649 | | * Free this up because we will overwrite it. no need to free type_id |
650 | | * because it is static |
651 | | */ |
652 | 0 | ASN1_TYPE_free(gen->d.otherName->value); |
653 | 0 | if ((gen->d.otherName->value = ASN1_generate_v3(p + 1, ctx)) == NULL) |
654 | 0 | return 0; |
655 | 0 | objlen = p - value; |
656 | 0 | objtmp = OPENSSL_strndup(value, objlen); |
657 | 0 | if (objtmp == NULL) |
658 | 0 | return 0; |
659 | 0 | gen->d.otherName->type_id = OBJ_txt2obj(objtmp, 0); |
660 | 0 | OPENSSL_free(objtmp); |
661 | 0 | if (!gen->d.otherName->type_id) |
662 | 0 | return 0; |
663 | 0 | return 1; |
664 | 0 | } |
665 | | |
666 | | static int do_dirname(GENERAL_NAME *gen, const char *value, X509V3_CTX *ctx) |
667 | 0 | { |
668 | 0 | int ret = 0; |
669 | 0 | STACK_OF(CONF_VALUE) *sk = NULL; |
670 | 0 | X509_NAME *nm; |
671 | |
|
672 | 0 | if ((nm = X509_NAME_new()) == NULL) |
673 | 0 | goto err; |
674 | 0 | sk = X509V3_get_section(ctx, value); |
675 | 0 | if (!sk) { |
676 | 0 | ERR_raise_data(ERR_LIB_X509V3, X509V3_R_SECTION_NOT_FOUND, |
677 | 0 | "section=%s", value); |
678 | 0 | goto err; |
679 | 0 | } |
680 | | /* FIXME: should allow other character types... */ |
681 | 0 | ret = X509V3_NAME_from_section(nm, sk, MBSTRING_ASC); |
682 | 0 | if (!ret) |
683 | 0 | goto err; |
684 | 0 | gen->d.dirn = nm; |
685 | |
|
686 | 0 | err: |
687 | 0 | if (ret == 0) |
688 | 0 | X509_NAME_free(nm); |
689 | 0 | X509V3_section_free(ctx, sk); |
690 | 0 | return ret; |
691 | 0 | } |