1 /* $OpenBSD: x509_conf.c,v 1.5 2023/02/16 08:38:17 tb Exp $ */ 2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3 * project 1999. 4 */ 5 /* ==================================================================== 6 * Copyright (c) 1999-2002 The OpenSSL Project. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * 3. All advertising materials mentioning features or use of this 21 * software must display the following acknowledgment: 22 * "This product includes software developed by the OpenSSL Project 23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 24 * 25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26 * endorse or promote products derived from this software without 27 * prior written permission. For written permission, please contact 28 * licensing@OpenSSL.org. 29 * 30 * 5. Products derived from this software may not be called "OpenSSL" 31 * nor may "OpenSSL" appear in their names without prior written 32 * permission of the OpenSSL Project. 33 * 34 * 6. Redistributions of any form whatsoever must retain the following 35 * acknowledgment: 36 * "This product includes software developed by the OpenSSL Project 37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 38 * 39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50 * OF THE POSSIBILITY OF SUCH DAMAGE. 51 * ==================================================================== 52 * 53 * This product includes cryptographic software written by Eric Young 54 * (eay@cryptsoft.com). This product includes software written by Tim 55 * Hudson (tjh@cryptsoft.com). 56 * 57 */ 58 /* extension creation utilities */ 59 60 #include <ctype.h> 61 #include <stdio.h> 62 #include <string.h> 63 64 #include <openssl/conf.h> 65 #include <openssl/err.h> 66 #include <openssl/x509.h> 67 #include <openssl/x509v3.h> 68 69 #include "x509_local.h" 70 71 static int v3_check_critical(const char **value); 72 static int v3_check_generic(const char **value); 73 static X509_EXTENSION *do_ext_nconf(CONF *conf, X509V3_CTX *ctx, int ext_nid, 74 int crit, const char *value); 75 static X509_EXTENSION *v3_generic_extension(const char *ext, const char *value, 76 int crit, int type, X509V3_CTX *ctx); 77 static char *conf_lhash_get_string(void *db, const char *section, 78 const char *value); 79 static STACK_OF(CONF_VALUE) *conf_lhash_get_section(void *db, 80 const char *section); 81 static X509_EXTENSION *do_ext_i2d(const X509V3_EXT_METHOD *method, int ext_nid, 82 int crit, void *ext_struc); 83 static unsigned char *generic_asn1(const char *value, X509V3_CTX *ctx, 84 long *ext_len); 85 86 /* CONF *conf: Config file */ 87 /* char *name: Name */ 88 /* char *value: Value */ 89 X509_EXTENSION * 90 X509V3_EXT_nconf(CONF *conf, X509V3_CTX *ctx, const char *name, 91 const char *value) 92 { 93 int crit; 94 int ext_type; 95 X509_EXTENSION *ret; 96 97 crit = v3_check_critical(&value); 98 if ((ext_type = v3_check_generic(&value))) 99 return v3_generic_extension(name, value, crit, ext_type, ctx); 100 ret = do_ext_nconf(conf, ctx, OBJ_sn2nid(name), crit, value); 101 if (!ret) { 102 X509V3error(X509V3_R_ERROR_IN_EXTENSION); 103 ERR_asprintf_error_data("name=%s, value=%s", name, value); 104 } 105 return ret; 106 } 107 LCRYPTO_ALIAS(X509V3_EXT_nconf); 108 109 /* CONF *conf: Config file */ 110 /* char *value: Value */ 111 X509_EXTENSION * 112 X509V3_EXT_nconf_nid(CONF *conf, X509V3_CTX *ctx, int ext_nid, 113 const char *value) 114 { 115 int crit; 116 int ext_type; 117 118 crit = v3_check_critical(&value); 119 if ((ext_type = v3_check_generic(&value))) 120 return v3_generic_extension(OBJ_nid2sn(ext_nid), 121 value, crit, ext_type, ctx); 122 return do_ext_nconf(conf, ctx, ext_nid, crit, value); 123 } 124 LCRYPTO_ALIAS(X509V3_EXT_nconf_nid); 125 126 /* CONF *conf: Config file */ 127 /* char *value: Value */ 128 static X509_EXTENSION * 129 do_ext_nconf(CONF *conf, X509V3_CTX *ctx, int ext_nid, int crit, 130 const char *value) 131 { 132 const X509V3_EXT_METHOD *method; 133 X509_EXTENSION *ext; 134 void *ext_struc; 135 136 if (ext_nid == NID_undef) { 137 X509V3error(X509V3_R_UNKNOWN_EXTENSION_NAME); 138 return NULL; 139 } 140 if (!(method = X509V3_EXT_get_nid(ext_nid))) { 141 X509V3error(X509V3_R_UNKNOWN_EXTENSION); 142 return NULL; 143 } 144 /* Now get internal extension representation based on type */ 145 if (method->v2i) { 146 STACK_OF(CONF_VALUE) *nval; 147 148 if (*value == '@') 149 nval = NCONF_get_section(conf, value + 1); 150 else 151 nval = X509V3_parse_list(value); 152 if (sk_CONF_VALUE_num(nval) <= 0) { 153 X509V3error(X509V3_R_INVALID_EXTENSION_STRING); 154 ERR_asprintf_error_data("name=%s,section=%s", 155 OBJ_nid2sn(ext_nid), value); 156 if (*value != '@') 157 sk_CONF_VALUE_pop_free(nval, X509V3_conf_free); 158 return NULL; 159 } 160 ext_struc = method->v2i(method, ctx, nval); 161 if (*value != '@') 162 sk_CONF_VALUE_pop_free(nval, X509V3_conf_free); 163 } else if (method->s2i) { 164 ext_struc = method->s2i(method, ctx, value); 165 } else if (method->r2i) { 166 if (!ctx->db || !ctx->db_meth) { 167 X509V3error(X509V3_R_NO_CONFIG_DATABASE); 168 return NULL; 169 } 170 ext_struc = method->r2i(method, ctx, value); 171 } else { 172 X509V3error(X509V3_R_EXTENSION_SETTING_NOT_SUPPORTED); 173 ERR_asprintf_error_data("name=%s", OBJ_nid2sn(ext_nid)); 174 return NULL; 175 } 176 if (ext_struc == NULL) 177 return NULL; 178 179 ext = do_ext_i2d(method, ext_nid, crit, ext_struc); 180 if (method->it) 181 ASN1_item_free(ext_struc, method->it); 182 else 183 method->ext_free(ext_struc); 184 return ext; 185 } 186 187 static X509_EXTENSION * 188 do_ext_i2d(const X509V3_EXT_METHOD *method, int ext_nid, int crit, 189 void *ext_struc) 190 { 191 unsigned char *ext_der; 192 int ext_len; 193 ASN1_OCTET_STRING *ext_oct = NULL; 194 X509_EXTENSION *ext; 195 196 /* Convert internal representation to DER */ 197 if (method->it) { 198 ext_der = NULL; 199 ext_len = ASN1_item_i2d(ext_struc, &ext_der, 200 method->it); 201 if (ext_len < 0) 202 goto merr; 203 } else { 204 unsigned char *p; 205 ext_len = method->i2d(ext_struc, NULL); 206 if (!(ext_der = malloc(ext_len))) 207 goto merr; 208 p = ext_der; 209 method->i2d(ext_struc, &p); 210 } 211 if (!(ext_oct = ASN1_OCTET_STRING_new())) 212 goto merr; 213 ext_oct->data = ext_der; 214 ext_oct->length = ext_len; 215 216 ext = X509_EXTENSION_create_by_NID(NULL, ext_nid, crit, ext_oct); 217 if (!ext) 218 goto merr; 219 ASN1_OCTET_STRING_free(ext_oct); 220 221 return ext; 222 223 merr: 224 ASN1_OCTET_STRING_free(ext_oct); 225 X509V3error(ERR_R_MALLOC_FAILURE); 226 return NULL; 227 228 } 229 230 /* Given an internal structure, nid and critical flag create an extension */ 231 232 X509_EXTENSION * 233 X509V3_EXT_i2d(int ext_nid, int crit, void *ext_struc) 234 { 235 const X509V3_EXT_METHOD *method; 236 237 if (!(method = X509V3_EXT_get_nid(ext_nid))) { 238 X509V3error(X509V3_R_UNKNOWN_EXTENSION); 239 return NULL; 240 } 241 return do_ext_i2d(method, ext_nid, crit, ext_struc); 242 } 243 LCRYPTO_ALIAS(X509V3_EXT_i2d); 244 245 /* Check the extension string for critical flag */ 246 static int 247 v3_check_critical(const char **value) 248 { 249 const char *p = *value; 250 251 if ((strlen(p) < 9) || strncmp(p, "critical,", 9)) 252 return 0; 253 p += 9; 254 while (isspace((unsigned char)*p)) p++; 255 *value = p; 256 return 1; 257 } 258 259 /* Check extension string for generic extension and return the type */ 260 static int 261 v3_check_generic(const char **value) 262 { 263 int gen_type = 0; 264 const char *p = *value; 265 266 if ((strlen(p) >= 4) && !strncmp(p, "DER:", 4)) { 267 p += 4; 268 gen_type = 1; 269 } else if ((strlen(p) >= 5) && !strncmp(p, "ASN1:", 5)) { 270 p += 5; 271 gen_type = 2; 272 } else 273 return 0; 274 275 while (isspace((unsigned char)*p)) 276 p++; 277 *value = p; 278 return gen_type; 279 } 280 281 /* Create a generic extension: for now just handle DER type */ 282 static X509_EXTENSION * 283 v3_generic_extension(const char *ext, const char *value, int crit, int gen_type, 284 X509V3_CTX *ctx) 285 { 286 unsigned char *ext_der = NULL; 287 long ext_len = 0; 288 ASN1_OBJECT *obj = NULL; 289 ASN1_OCTET_STRING *oct = NULL; 290 X509_EXTENSION *extension = NULL; 291 292 if (!(obj = OBJ_txt2obj(ext, 0))) { 293 X509V3error(X509V3_R_EXTENSION_NAME_ERROR); 294 ERR_asprintf_error_data("name=%s", ext); 295 goto err; 296 } 297 298 if (gen_type == 1) 299 ext_der = string_to_hex(value, &ext_len); 300 else if (gen_type == 2) 301 ext_der = generic_asn1(value, ctx, &ext_len); 302 else { 303 ERR_asprintf_error_data("Unexpected generic extension type %d", gen_type); 304 goto err; 305 } 306 307 if (ext_der == NULL) { 308 X509V3error(X509V3_R_EXTENSION_VALUE_ERROR); 309 ERR_asprintf_error_data("value=%s", value); 310 goto err; 311 } 312 313 if (!(oct = ASN1_OCTET_STRING_new())) { 314 X509V3error(ERR_R_MALLOC_FAILURE); 315 goto err; 316 } 317 318 oct->data = ext_der; 319 oct->length = ext_len; 320 ext_der = NULL; 321 322 extension = X509_EXTENSION_create_by_OBJ(NULL, obj, crit, oct); 323 324 err: 325 ASN1_OBJECT_free(obj); 326 ASN1_OCTET_STRING_free(oct); 327 free(ext_der); 328 return extension; 329 } 330 331 static unsigned char * 332 generic_asn1(const char *value, X509V3_CTX *ctx, long *ext_len) 333 { 334 ASN1_TYPE *typ; 335 unsigned char *ext_der = NULL; 336 337 typ = ASN1_generate_v3(value, ctx); 338 if (typ == NULL) 339 return NULL; 340 *ext_len = i2d_ASN1_TYPE(typ, &ext_der); 341 ASN1_TYPE_free(typ); 342 return ext_der; 343 } 344 345 /* This is the main function: add a bunch of extensions based on a config file 346 * section to an extension STACK. 347 */ 348 349 int 350 X509V3_EXT_add_nconf_sk(CONF *conf, X509V3_CTX *ctx, const char *section, 351 STACK_OF(X509_EXTENSION) **sk) 352 { 353 X509_EXTENSION *ext; 354 STACK_OF(CONF_VALUE) *nval; 355 CONF_VALUE *val; 356 int i; 357 358 if (!(nval = NCONF_get_section(conf, section))) 359 return 0; 360 for (i = 0; i < sk_CONF_VALUE_num(nval); i++) { 361 val = sk_CONF_VALUE_value(nval, i); 362 if (!(ext = X509V3_EXT_nconf(conf, ctx, val->name, val->value))) 363 return 0; 364 if (sk) 365 X509v3_add_ext(sk, ext, -1); 366 X509_EXTENSION_free(ext); 367 } 368 return 1; 369 } 370 LCRYPTO_ALIAS(X509V3_EXT_add_nconf_sk); 371 372 /* Convenience functions to add extensions to a certificate, CRL and request */ 373 374 int 375 X509V3_EXT_add_nconf(CONF *conf, X509V3_CTX *ctx, const char *section, 376 X509 *cert) 377 { 378 STACK_OF(X509_EXTENSION) **sk = NULL; 379 380 if (cert) 381 sk = &cert->cert_info->extensions; 382 return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk); 383 } 384 LCRYPTO_ALIAS(X509V3_EXT_add_nconf); 385 386 /* Same as above but for a CRL */ 387 388 int 389 X509V3_EXT_CRL_add_nconf(CONF *conf, X509V3_CTX *ctx, const char *section, 390 X509_CRL *crl) 391 { 392 STACK_OF(X509_EXTENSION) **sk = NULL; 393 394 if (crl) 395 sk = &crl->crl->extensions; 396 return X509V3_EXT_add_nconf_sk(conf, ctx, section, sk); 397 } 398 LCRYPTO_ALIAS(X509V3_EXT_CRL_add_nconf); 399 400 /* Add extensions to certificate request */ 401 402 int 403 X509V3_EXT_REQ_add_nconf(CONF *conf, X509V3_CTX *ctx, const char *section, 404 X509_REQ *req) 405 { 406 STACK_OF(X509_EXTENSION) *extlist = NULL, **sk = NULL; 407 int i; 408 409 if (req) 410 sk = &extlist; 411 i = X509V3_EXT_add_nconf_sk(conf, ctx, section, sk); 412 if (!i || !sk) 413 return i; 414 i = X509_REQ_add_extensions(req, extlist); 415 sk_X509_EXTENSION_pop_free(extlist, X509_EXTENSION_free); 416 return i; 417 } 418 LCRYPTO_ALIAS(X509V3_EXT_REQ_add_nconf); 419 420 /* Config database functions */ 421 422 char * 423 X509V3_get_string(X509V3_CTX *ctx, const char *name, const char *section) 424 { 425 if (!ctx->db || !ctx->db_meth || !ctx->db_meth->get_string) { 426 X509V3error(X509V3_R_OPERATION_NOT_DEFINED); 427 return NULL; 428 } 429 return ctx->db_meth->get_string(ctx->db, name, section); 430 } 431 LCRYPTO_ALIAS(X509V3_get_string); 432 433 STACK_OF(CONF_VALUE) * 434 X509V3_get_section(X509V3_CTX *ctx, const char *section) 435 { 436 if (!ctx->db || !ctx->db_meth || !ctx->db_meth->get_section) { 437 X509V3error(X509V3_R_OPERATION_NOT_DEFINED); 438 return NULL; 439 } 440 return ctx->db_meth->get_section(ctx->db, section); 441 } 442 LCRYPTO_ALIAS(X509V3_get_section); 443 444 void 445 X509V3_string_free(X509V3_CTX *ctx, char *str) 446 { 447 if (!str) 448 return; 449 if (ctx->db_meth->free_string) 450 ctx->db_meth->free_string(ctx->db, str); 451 } 452 LCRYPTO_ALIAS(X509V3_string_free); 453 454 void 455 X509V3_section_free(X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *section) 456 { 457 if (!section) 458 return; 459 if (ctx->db_meth->free_section) 460 ctx->db_meth->free_section(ctx->db, section); 461 } 462 LCRYPTO_ALIAS(X509V3_section_free); 463 464 static char * 465 nconf_get_string(void *db, const char *section, const char *value) 466 { 467 return NCONF_get_string(db, section, value); 468 } 469 470 static STACK_OF(CONF_VALUE) * 471 nconf_get_section(void *db, const char *section) 472 { 473 return NCONF_get_section(db, section); 474 } 475 476 static X509V3_CONF_METHOD nconf_method = { 477 nconf_get_string, 478 nconf_get_section, 479 NULL, 480 NULL 481 }; 482 483 void 484 X509V3_set_nconf(X509V3_CTX *ctx, CONF *conf) 485 { 486 ctx->db_meth = &nconf_method; 487 ctx->db = conf; 488 } 489 LCRYPTO_ALIAS(X509V3_set_nconf); 490 491 void 492 X509V3_set_ctx(X509V3_CTX *ctx, X509 *issuer, X509 *subj, X509_REQ *req, 493 X509_CRL *crl, int flags) 494 { 495 ctx->issuer_cert = issuer; 496 ctx->subject_cert = subj; 497 ctx->crl = crl; 498 ctx->subject_req = req; 499 ctx->flags = flags; 500 } 501 LCRYPTO_ALIAS(X509V3_set_ctx); 502 503 /* Old conf compatibility functions */ 504 505 X509_EXTENSION * 506 X509V3_EXT_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx, const char *name, 507 const char *value) 508 { 509 CONF ctmp; 510 511 CONF_set_nconf(&ctmp, conf); 512 return X509V3_EXT_nconf(&ctmp, ctx, name, value); 513 } 514 LCRYPTO_ALIAS(X509V3_EXT_conf); 515 516 /* LHASH *conf: Config file */ 517 /* char *value: Value */ 518 X509_EXTENSION * 519 X509V3_EXT_conf_nid(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx, int ext_nid, 520 const char *value) 521 { 522 CONF ctmp; 523 524 CONF_set_nconf(&ctmp, conf); 525 return X509V3_EXT_nconf_nid(&ctmp, ctx, ext_nid, value); 526 } 527 LCRYPTO_ALIAS(X509V3_EXT_conf_nid); 528 529 static char * 530 conf_lhash_get_string(void *db, const char *section, const char *value) 531 { 532 return CONF_get_string(db, section, value); 533 } 534 535 static STACK_OF(CONF_VALUE) * 536 conf_lhash_get_section(void *db, const char *section) 537 { 538 return CONF_get_section(db, section); 539 } 540 541 static X509V3_CONF_METHOD conf_lhash_method = { 542 conf_lhash_get_string, 543 conf_lhash_get_section, 544 NULL, 545 NULL 546 }; 547 548 void 549 X509V3_set_conf_lhash(X509V3_CTX *ctx, LHASH_OF(CONF_VALUE) *lhash) 550 { 551 ctx->db_meth = &conf_lhash_method; 552 ctx->db = lhash; 553 } 554 LCRYPTO_ALIAS(X509V3_set_conf_lhash); 555 556 int 557 X509V3_EXT_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx, 558 const char *section, X509 *cert) 559 { 560 CONF ctmp; 561 562 CONF_set_nconf(&ctmp, conf); 563 return X509V3_EXT_add_nconf(&ctmp, ctx, section, cert); 564 } 565 LCRYPTO_ALIAS(X509V3_EXT_add_conf); 566 567 /* Same as above but for a CRL */ 568 569 int 570 X509V3_EXT_CRL_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx, 571 const char *section, X509_CRL *crl) 572 { 573 CONF ctmp; 574 575 CONF_set_nconf(&ctmp, conf); 576 return X509V3_EXT_CRL_add_nconf(&ctmp, ctx, section, crl); 577 } 578 LCRYPTO_ALIAS(X509V3_EXT_CRL_add_conf); 579 580 /* Add extensions to certificate request */ 581 582 int 583 X509V3_EXT_REQ_add_conf(LHASH_OF(CONF_VALUE) *conf, X509V3_CTX *ctx, 584 const char *section, X509_REQ *req) 585 { 586 CONF ctmp; 587 588 CONF_set_nconf(&ctmp, conf); 589 return X509V3_EXT_REQ_add_nconf(&ctmp, ctx, section, req); 590 } 591 LCRYPTO_ALIAS(X509V3_EXT_REQ_add_conf); 592