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