| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <stdio.h> |
| 16 | #include <string.h> |
| 17 |
| 18 | #include <openssl/conf.h> |
| 19 | #include <openssl/err.h> |
| 20 | #include <openssl/mem.h> |
| 21 | #include <openssl/obj.h> |
| 22 | #include <openssl/x509.h> |
| 23 |
| 24 | #include "ext_dat.h" |
| 25 | #include "internal.h" |
| 26 |
| 27 |
| 28 | static void *v2i_subject_alt(const X509V3_EXT_METHOD *method, |
| 29 | const X509V3_CTX *ctx, |
| 30 | const STACK_OF(CONF_VALUE) *nval); |
| 31 | static void *v2i_issuer_alt(const X509V3_EXT_METHOD *method, |
| 32 | const X509V3_CTX *ctx, |
| 33 | const STACK_OF(CONF_VALUE) *nval); |
| 34 | static int copy_email(const X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p); |
| 35 | static int copy_issuer(const X509V3_CTX *ctx, GENERAL_NAMES *gens); |
| 36 | static int do_othername(GENERAL_NAME *gen, const char *value, |
| 37 | const X509V3_CTX *ctx); |
| 38 | static int do_dirname(GENERAL_NAME *gen, const char *value, |
| 39 | const X509V3_CTX *ctx); |
| 40 |
| 41 | static STACK_OF(CONF_VALUE) *i2v_GENERAL_NAMES_cb( |
| 42 | const X509V3_EXT_METHOD *method, void *ext, STACK_OF(CONF_VALUE) *ret) { |
| 43 | return i2v_GENERAL_NAMES(method, reinterpret_cast<GENERAL_NAMES *>(ext), ret); |
| 44 | } |
| 45 |
| 46 | const X509V3_EXT_METHOD v3_alt[] = { |
| 47 | {NID_subject_alt_name, 0, ASN1_ITEM_ref(GENERAL_NAMES), 0, 0, 0, 0, 0, 0, |
| 48 | i2v_GENERAL_NAMES_cb, v2i_subject_alt, NULL, NULL, NULL}, |
| 49 |
| 50 | {NID_issuer_alt_name, 0, ASN1_ITEM_ref(GENERAL_NAMES), 0, 0, 0, 0, 0, 0, |
| 51 | i2v_GENERAL_NAMES_cb, v2i_issuer_alt, NULL, NULL, NULL}, |
| 52 |
| 53 | {NID_certificate_issuer, 0, ASN1_ITEM_ref(GENERAL_NAMES), 0, 0, 0, 0, 0, 0, |
| 54 | i2v_GENERAL_NAMES_cb, NULL, NULL, NULL, NULL}, |
| 55 | }; |
| 56 |
| 57 | STACK_OF(CONF_VALUE) *i2v_GENERAL_NAMES(const X509V3_EXT_METHOD *method, |
| 58 | const GENERAL_NAMES *gens, |
| 59 | STACK_OF(CONF_VALUE) *ret) { |
| 60 | int ret_was_null = ret == NULL; |
| 61 | for (size_t i = 0; i < sk_GENERAL_NAME_num(gens); i++) { |
| 62 | const GENERAL_NAME *gen = sk_GENERAL_NAME_value(gens, i); |
| 63 | STACK_OF(CONF_VALUE) *tmp = i2v_GENERAL_NAME(method, gen, ret); |
| 64 | if (tmp == NULL) { |
| 65 | if (ret_was_null) { |
| 66 | sk_CONF_VALUE_pop_free(ret, X509V3_conf_free); |
| 67 | } |
| 68 | return NULL; |
| 69 | } |
| 70 | ret = tmp; |
| 71 | } |
| 72 | if (!ret) { |
| 73 | return sk_CONF_VALUE_new_null(); |
| 74 | } |
| 75 | return ret; |
| 76 | } |
| 77 |
| 78 | STACK_OF(CONF_VALUE) *i2v_GENERAL_NAME(const X509V3_EXT_METHOD *method, |
| 79 | const GENERAL_NAME *gen, |
| 80 | STACK_OF(CONF_VALUE) *ret) { |
| 81 | // Note the error-handling for this function relies on there being at most |
| 82 | // one |X509V3_add_value| call. If there were two and the second failed, we |
| 83 | // would need to sometimes free the first call's result. |
| 84 | unsigned char *p; |
| 85 | char oline[256], htmp[5]; |
| 86 | int i; |
| 87 | switch (gen->type) { |
| 88 | case GEN_OTHERNAME: |
| 89 | if (!X509V3_add_value("othername", "<unsupported>", &ret)) { |
| 90 | return NULL; |
| 91 | } |
| 92 | break; |
| 93 |
| 94 | case GEN_X400: |
| 95 | if (!X509V3_add_value("X400Name", "<unsupported>", &ret)) { |
| 96 | return NULL; |
| 97 | } |
| 98 | break; |
| 99 |
| 100 | case GEN_EDIPARTY: |
| 101 | if (!X509V3_add_value("EdiPartyName", "<unsupported>", &ret)) { |
| 102 | return NULL; |
| 103 | } |
| 104 | break; |
| 105 |
| 106 | case GEN_EMAIL: |
| 107 | if (!x509V3_add_value_asn1_string("email", gen->d.ia5, &ret)) { |
| 108 | return NULL; |
| 109 | } |
| 110 | break; |
| 111 |
| 112 | case GEN_DNS: |
| 113 | if (!x509V3_add_value_asn1_string("DNS", gen->d.ia5, &ret)) { |
| 114 | return NULL; |
| 115 | } |
| 116 | break; |
| 117 |
| 118 | case GEN_URI: |
| 119 | if (!x509V3_add_value_asn1_string("URI", gen->d.ia5, &ret)) { |
| 120 | return NULL; |
| 121 | } |
| 122 | break; |
| 123 |
| 124 | case GEN_DIRNAME: |
| 125 | if (X509_NAME_oneline(gen->d.dirn, oline, 256) == NULL || |
| 126 | !X509V3_add_value("DirName", oline, &ret)) { |
| 127 | return NULL; |
| 128 | } |
| 129 | break; |
| 130 |
| 131 | case GEN_IPADD: |
| 132 | p = gen->d.ip->data; |
| 133 | if (gen->d.ip->length == 4) { |
| 134 | snprintf(oline, sizeof(oline), "%d.%d.%d.%d", p[0], p[1], p[2], p[3]); |
| 135 | } else if (gen->d.ip->length == 16) { |
| 136 | oline[0] = 0; |
| 137 | for (i = 0; i < 8; i++) { |
| 138 | uint16_t v = ((uint16_t)p[0] << 8) | p[1]; |
| 139 | snprintf(htmp, sizeof(htmp), "%X", v); |
| 140 | p += 2; |
| 141 | OPENSSL_strlcat(oline, htmp, sizeof(oline)); |
| 142 | if (i != 7) { |
| 143 | OPENSSL_strlcat(oline, ":", sizeof(oline)); |
| 144 | } |
| 145 | } |
| 146 | } else { |
| 147 | if (!X509V3_add_value("IP Address", "<invalid>", &ret)) { |
| 148 | return NULL; |
| 149 | } |
| 150 | break; |
| 151 | } |
| 152 | if (!X509V3_add_value("IP Address", oline, &ret)) { |
| 153 | return NULL; |
| 154 | } |
| 155 | break; |
| 156 |
| 157 | case GEN_RID: |
| 158 | i2t_ASN1_OBJECT(oline, 256, gen->d.rid); |
| 159 | if (!X509V3_add_value("Registered ID", oline, &ret)) { |
| 160 | return NULL; |
| 161 | } |
| 162 | break; |
| 163 | } |
| 164 | return ret; |
| 165 | } |
| 166 |
| 167 | int GENERAL_NAME_print(BIO *out, const GENERAL_NAME *gen) { |
| 168 | switch (gen->type) { |
| 169 | case GEN_OTHERNAME: |
| 170 | BIO_printf(out, "othername:<unsupported>"); |
| 171 | break; |
| 172 |
| 173 | case GEN_X400: |
| 174 | BIO_printf(out, "X400Name:<unsupported>"); |
| 175 | break; |
| 176 |
| 177 | case GEN_EDIPARTY: |
| 178 | // Maybe fix this: it is supported now |
| 179 | BIO_printf(out, "EdiPartyName:<unsupported>"); |
| 180 | break; |
| 181 |
| 182 | case GEN_EMAIL: |
| 183 | BIO_printf(out, "email:"); |
| 184 | ASN1_STRING_print(out, gen->d.ia5); |
| 185 | break; |
| 186 |
| 187 | case GEN_DNS: |
| 188 | BIO_printf(out, "DNS:"); |
| 189 | ASN1_STRING_print(out, gen->d.ia5); |
| 190 | break; |
| 191 |
| 192 | case GEN_URI: |
| 193 | BIO_printf(out, "URI:"); |
| 194 | ASN1_STRING_print(out, gen->d.ia5); |
| 195 | break; |
| 196 |
| 197 | case GEN_DIRNAME: |
| 198 | BIO_printf(out, "DirName: "); |
| 199 | X509_NAME_print_ex(out, gen->d.dirn, 0, XN_FLAG_ONELINE); |
| 200 | break; |
| 201 |
| 202 | case GEN_IPADD: { |
| 203 | const unsigned char *p = gen->d.ip->data; |
| 204 | if (gen->d.ip->length == 4) { |
| 205 | BIO_printf(out, "IP Address:%d.%d.%d.%d", p[0], p[1], p[2], p[3]); |
| 206 | } else if (gen->d.ip->length == 16) { |
| 207 | BIO_printf(out, "IP Address"); |
| 208 | for (int i = 0; i < 8; i++) { |
| 209 | uint16_t v = ((uint16_t)p[0] << 8) | p[1]; |
| 210 | BIO_printf(out, ":%X", v); |
| 211 | p += 2; |
| 212 | } |
| 213 | BIO_puts(out, "\n"); |
| 214 | } else { |
| 215 | BIO_printf(out, "IP Address:<invalid>"); |
| 216 | break; |
| 217 | } |
| 218 | break; |
| 219 | } |
| 220 |
| 221 | case GEN_RID: |
| 222 | BIO_printf(out, "Registered ID"); |
| 223 | i2a_ASN1_OBJECT(out, gen->d.rid); |
| 224 | break; |
| 225 | } |
| 226 | return 1; |
| 227 | } |
| 228 |
| 229 | static void *v2i_issuer_alt(const X509V3_EXT_METHOD *method, |
| 230 | const X509V3_CTX *ctx, |
| 231 | const STACK_OF(CONF_VALUE) *nval) { |
| 232 | GENERAL_NAMES *gens = sk_GENERAL_NAME_new_null(); |
| 233 | if (gens == NULL) { |
| 234 | return NULL; |
| 235 | } |
| 236 | for (size_t i = 0; i < sk_CONF_VALUE_num(nval); i++) { |
| 237 | const CONF_VALUE *cnf = sk_CONF_VALUE_value(nval, i); |
| 238 | if (x509v3_conf_name_matches(cnf->name, "issuer") && cnf->value && |
| 239 | !strcmp(cnf->value, "copy")) { |
| 240 | if (!copy_issuer(ctx, gens)) { |
| 241 | goto err; |
| 242 | } |
| 243 | } else { |
| 244 | GENERAL_NAME *gen = v2i_GENERAL_NAME(method, ctx, cnf); |
| 245 | if (gen == NULL || !sk_GENERAL_NAME_push(gens, gen)) { |
| 246 | GENERAL_NAME_free(gen); |
| 247 | goto err; |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | return gens; |
| 252 | err: |
| 253 | sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); |
| 254 | return NULL; |
| 255 | } |
| 256 |
| 257 | // Append subject altname of issuer to issuer alt name of subject |
| 258 |
| 259 | static int copy_issuer(const X509V3_CTX *ctx, GENERAL_NAMES *gens) { |
| 260 | if (ctx && (ctx->flags == X509V3_CTX_TEST)) { |
| 261 | return 1; |
| 262 | } |
| 263 | if (!ctx || !ctx->issuer_cert) { |
| 264 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_NO_ISSUER_DETAILS); |
| 265 | return 0; |
| 266 | } |
| 267 | int i = X509_get_ext_by_NID(ctx->issuer_cert, NID_subject_alt_name, -1); |
| 268 | if (i < 0) { |
| 269 | return 1; |
| 270 | } |
| 271 |
| 272 | int ret = 0; |
| 273 | GENERAL_NAMES *ialt = NULL; |
| 274 | X509_EXTENSION *ext; |
| 275 | if (!(ext = X509_get_ext(ctx->issuer_cert, i)) || |
| 276 | !(ialt = reinterpret_cast<GENERAL_NAMES *>(X509V3_EXT_d2i(ext)))) { |
| 277 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_ISSUER_DECODE_ERROR); |
| 278 | goto err; |
| 279 | } |
| 280 |
| 281 | for (size_t j = 0; j < sk_GENERAL_NAME_num(ialt); j++) { |
| 282 | GENERAL_NAME *gen = sk_GENERAL_NAME_value(ialt, j); |
| 283 | if (!sk_GENERAL_NAME_push(gens, gen)) { |
| 284 | goto err; |
| 285 | } |
| 286 | // Ownership of |gen| has moved from |ialt| to |gens|. |
| 287 | sk_GENERAL_NAME_set(ialt, j, NULL); |
| 288 | } |
| 289 |
| 290 | ret = 1; |
| 291 |
| 292 | err: |
| 293 | GENERAL_NAMES_free(ialt); |
| 294 | return ret; |
| 295 | } |
| 296 |
| 297 | static void *v2i_subject_alt(const X509V3_EXT_METHOD *method, |
| 298 | const X509V3_CTX *ctx, |
| 299 | const STACK_OF(CONF_VALUE) *nval) { |
| 300 | GENERAL_NAMES *gens = sk_GENERAL_NAME_new_null(); |
| 301 | if (gens == NULL) { |
| 302 | return NULL; |
| 303 | } |
| 304 | for (size_t i = 0; i < sk_CONF_VALUE_num(nval); i++) { |
| 305 | const CONF_VALUE *cnf = sk_CONF_VALUE_value(nval, i); |
| 306 | if (x509v3_conf_name_matches(cnf->name, "email") && cnf->value && |
| 307 | !strcmp(cnf->value, "copy")) { |
| 308 | if (!copy_email(ctx, gens, 0)) { |
| 309 | goto err; |
| 310 | } |
| 311 | } else if (x509v3_conf_name_matches(cnf->name, "email") && cnf->value && |
| 312 | !strcmp(cnf->value, "move")) { |
| 313 | if (!copy_email(ctx, gens, 1)) { |
| 314 | goto err; |
| 315 | } |
| 316 | } else { |
| 317 | GENERAL_NAME *gen = v2i_GENERAL_NAME(method, ctx, cnf); |
| 318 | if (gen == NULL || !sk_GENERAL_NAME_push(gens, gen)) { |
| 319 | GENERAL_NAME_free(gen); |
| 320 | goto err; |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | return gens; |
| 325 | err: |
| 326 | sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); |
| 327 | return NULL; |
| 328 | } |
| 329 |
| 330 | // Copy any email addresses in a certificate or request to GENERAL_NAMES |
| 331 |
| 332 | static int copy_email(const X509V3_CTX *ctx, GENERAL_NAMES *gens, int move_p) { |
| 333 | X509_NAME *nm; |
| 334 | ASN1_IA5STRING *email = NULL; |
| 335 | X509_NAME_ENTRY *ne; |
| 336 | GENERAL_NAME *gen = NULL; |
| 337 | int i; |
| 338 | if (ctx != NULL && ctx->flags == X509V3_CTX_TEST) { |
| 339 | return 1; |
| 340 | } |
| 341 | if (!ctx || (!ctx->subject_cert && !ctx->subject_req)) { |
| 342 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_NO_SUBJECT_DETAILS); |
| 343 | goto err; |
| 344 | } |
| 345 | // Find the subject name |
| 346 | if (ctx->subject_cert) { |
| 347 | nm = X509_get_subject_name(ctx->subject_cert); |
| 348 | } else { |
| 349 | nm = X509_REQ_get_subject_name(ctx->subject_req); |
| 350 | } |
| 351 |
| 352 | // Now add any email address(es) to STACK |
| 353 | i = -1; |
| 354 | while ((i = X509_NAME_get_index_by_NID(nm, NID_pkcs9_emailAddress, i)) >= 0) { |
| 355 | ne = X509_NAME_get_entry(nm, i); |
| 356 | email = ASN1_STRING_dup(X509_NAME_ENTRY_get_data(ne)); |
| 357 | if (move_p) { |
| 358 | X509_NAME_delete_entry(nm, i); |
| 359 | X509_NAME_ENTRY_free(ne); |
| 360 | i--; |
| 361 | } |
| 362 | if (!email || !(gen = GENERAL_NAME_new())) { |
| 363 | goto err; |
| 364 | } |
| 365 | gen->d.ia5 = email; |
| 366 | email = NULL; |
| 367 | gen->type = GEN_EMAIL; |
| 368 | if (!sk_GENERAL_NAME_push(gens, gen)) { |
| 369 | goto err; |
| 370 | } |
| 371 | gen = NULL; |
| 372 | } |
| 373 |
| 374 | return 1; |
| 375 |
| 376 | err: |
| 377 | GENERAL_NAME_free(gen); |
| 378 | ASN1_IA5STRING_free(email); |
| 379 | return 0; |
| 380 | } |
| 381 |
| 382 | GENERAL_NAMES *v2i_GENERAL_NAMES(const X509V3_EXT_METHOD *method, |
| 383 | const X509V3_CTX *ctx, |
| 384 | const STACK_OF(CONF_VALUE) *nval) { |
| 385 | GENERAL_NAMES *gens = sk_GENERAL_NAME_new_null(); |
| 386 | if (gens == NULL) { |
| 387 | return NULL; |
| 388 | } |
| 389 | for (size_t i = 0; i < sk_CONF_VALUE_num(nval); i++) { |
| 390 | const CONF_VALUE *cnf = sk_CONF_VALUE_value(nval, i); |
| 391 | GENERAL_NAME *gen = v2i_GENERAL_NAME(method, ctx, cnf); |
| 392 | if (gen == NULL || !sk_GENERAL_NAME_push(gens, gen)) { |
| 393 | GENERAL_NAME_free(gen); |
| 394 | goto err; |
| 395 | } |
| 396 | } |
| 397 | return gens; |
| 398 | err: |
| 399 | sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); |
| 400 | return NULL; |
| 401 | } |
| 402 |
| 403 | GENERAL_NAME *v2i_GENERAL_NAME(const X509V3_EXT_METHOD *method, |
| 404 | const X509V3_CTX *ctx, const CONF_VALUE *cnf) { |
| 405 | return v2i_GENERAL_NAME_ex(NULL, method, ctx, cnf, 0); |
| 406 | } |
| 407 |
| 408 | static GENERAL_NAME *a2i_GENERAL_NAME(GENERAL_NAME *out, |
| 409 | const X509V3_EXT_METHOD *method, |
| 410 | const X509V3_CTX *ctx, int gen_type, |
| 411 | const char *value, int is_nc) { |
| 412 | if (!value) { |
| 413 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_MISSING_VALUE); |
| 414 | return NULL; |
| 415 | } |
| 416 |
| 417 | GENERAL_NAME *gen = NULL; |
| 418 | if (out) { |
| 419 | gen = out; |
| 420 | } else { |
| 421 | gen = GENERAL_NAME_new(); |
| 422 | if (gen == NULL) { |
| 423 | return NULL; |
| 424 | } |
| 425 | } |
| 426 |
| 427 | switch (gen_type) { |
| 428 | case GEN_URI: |
| 429 | case GEN_EMAIL: |
| 430 | case GEN_DNS: { |
| 431 | ASN1_IA5STRING *str = ASN1_IA5STRING_new(); |
| 432 | if (str == NULL || !ASN1_STRING_set(str, value, strlen(value))) { |
| 433 | ASN1_STRING_free(str); |
| 434 | goto err; |
| 435 | } |
| 436 | gen->type = gen_type; |
| 437 | gen->d.ia5 = str; |
| 438 | break; |
| 439 | } |
| 440 |
| 441 | case GEN_RID: { |
| 442 | ASN1_OBJECT *obj; |
| 443 | if (!(obj = OBJ_txt2obj(value, 0))) { |
| 444 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_BAD_OBJECT); |
| 445 | ERR_add_error_data(2, "value=", value); |
| 446 | goto err; |
| 447 | } |
| 448 | gen->type = GEN_RID; |
| 449 | gen->d.rid = obj; |
| 450 | break; |
| 451 | } |
| 452 |
| 453 | case GEN_IPADD: |
| 454 | gen->type = GEN_IPADD; |
| 455 | if (is_nc) { |
| 456 | gen->d.ip = a2i_IPADDRESS_NC(value); |
| 457 | } else { |
| 458 | gen->d.ip = a2i_IPADDRESS(value); |
| 459 | } |
| 460 | if (gen->d.ip == NULL) { |
| 461 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_BAD_IP_ADDRESS); |
| 462 | ERR_add_error_data(2, "value=", value); |
| 463 | goto err; |
| 464 | } |
| 465 | break; |
| 466 |
| 467 | case GEN_DIRNAME: |
| 468 | if (!do_dirname(gen, value, ctx)) { |
| 469 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_DIRNAME_ERROR); |
| 470 | goto err; |
| 471 | } |
| 472 | break; |
| 473 |
| 474 | case GEN_OTHERNAME: |
| 475 | if (!do_othername(gen, value, ctx)) { |
| 476 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_OTHERNAME_ERROR); |
| 477 | goto err; |
| 478 | } |
| 479 | break; |
| 480 | default: |
| 481 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_UNSUPPORTED_TYPE); |
| 482 | goto err; |
| 483 | } |
| 484 |
| 485 | return gen; |
| 486 |
| 487 | err: |
| 488 | if (!out) { |
| 489 | GENERAL_NAME_free(gen); |
| 490 | } |
| 491 | return NULL; |
| 492 | } |
| 493 |
| 494 | GENERAL_NAME *v2i_GENERAL_NAME_ex(GENERAL_NAME *out, |
| 495 | const X509V3_EXT_METHOD *method, |
| 496 | const X509V3_CTX *ctx, const CONF_VALUE *cnf, |
| 497 | int is_nc) { |
| 498 | const char *name = cnf->name; |
| 499 | const char *value = cnf->value; |
| 500 | if (!value) { |
| 501 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_MISSING_VALUE); |
| 502 | return NULL; |
| 503 | } |
| 504 |
| 505 | int type; |
| 506 | if (x509v3_conf_name_matches(name, "email")) { |
| 507 | type = GEN_EMAIL; |
| 508 | } else if (x509v3_conf_name_matches(name, "URI")) { |
| 509 | type = GEN_URI; |
| 510 | } else if (x509v3_conf_name_matches(name, "DNS")) { |
| 511 | type = GEN_DNS; |
| 512 | } else if (x509v3_conf_name_matches(name, "RID")) { |
| 513 | type = GEN_RID; |
| 514 | } else if (x509v3_conf_name_matches(name, "IP")) { |
| 515 | type = GEN_IPADD; |
| 516 | } else if (x509v3_conf_name_matches(name, "dirName")) { |
| 517 | type = GEN_DIRNAME; |
| 518 | } else if (x509v3_conf_name_matches(name, "otherName")) { |
| 519 | type = GEN_OTHERNAME; |
| 520 | } else { |
| 521 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_UNSUPPORTED_OPTION); |
| 522 | ERR_add_error_data(2, "name=", name); |
| 523 | return NULL; |
| 524 | } |
| 525 |
| 526 | return a2i_GENERAL_NAME(out, method, ctx, type, value, is_nc); |
| 527 | } |
| 528 |
| 529 | static int do_othername(GENERAL_NAME *gen, const char *value, |
| 530 | const X509V3_CTX *ctx) { |
| 531 | const char *semicolon = strchr(value, ';'); |
| 532 | if (semicolon == NULL) { |
| 533 | return 0; |
| 534 | } |
| 535 |
| 536 | OTHERNAME *name = OTHERNAME_new(); |
| 537 | if (name == NULL) { |
| 538 | return 0; |
| 539 | } |
| 540 |
| 541 | char *objtmp = OPENSSL_strndup(value, semicolon - value); |
| 542 | if (objtmp == NULL) { |
| 543 | goto err; |
| 544 | } |
| 545 | ASN1_OBJECT_free(name->type_id); |
| 546 | name->type_id = OBJ_txt2obj(objtmp, /*dont_search_names=*/0); |
| 547 | OPENSSL_free(objtmp); |
| 548 | if (name->type_id == NULL) { |
| 549 | goto err; |
| 550 | } |
| 551 |
| 552 | ASN1_TYPE_free(name->value); |
| 553 | name->value = ASN1_generate_v3(semicolon + 1, ctx); |
| 554 | if (name->value == NULL) { |
| 555 | goto err; |
| 556 | } |
| 557 |
| 558 | gen->type = GEN_OTHERNAME; |
| 559 | gen->d.otherName = name; |
| 560 | return 1; |
| 561 |
| 562 | err: |
| 563 | OTHERNAME_free(name); |
| 564 | return 0; |
| 565 | } |
| 566 |
| 567 | static int do_dirname(GENERAL_NAME *gen, const char *value, |
| 568 | const X509V3_CTX *ctx) { |
| 569 | int ret = 0; |
| 570 | const STACK_OF(CONF_VALUE) *sk = X509V3_get_section(ctx, value); |
| 571 | X509_NAME *nm = X509_NAME_new(); |
| 572 | if (nm == NULL) { |
| 573 | goto err; |
| 574 | } |
| 575 | if (sk == NULL) { |
| 576 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_SECTION_NOT_FOUND); |
| 577 | ERR_add_error_data(2, "section=", value); |
| 578 | goto err; |
| 579 | } |
| 580 | // FIXME: should allow other character types... |
| 581 | if (!X509V3_NAME_from_section(nm, sk, MBSTRING_ASC)) { |
| 582 | goto err; |
| 583 | } |
| 584 | gen->type = GEN_DIRNAME; |
| 585 | gen->d.dirn = nm; |
| 586 | ret = 1; |
| 587 |
| 588 | err: |
| 589 | if (!ret) { |
| 590 | X509_NAME_free(nm); |
| 591 | } |
| 592 | return ret; |
| 593 | } |
| 594 |