1 /* $OpenBSD: req.c,v 1.4 2014/10/16 10:43:54 jsing Exp $ */ 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * 9 * This library is free for commercial and non-commercial use as long as 10 * the following conditions are aheared to. The following conditions 11 * apply to all code found in this distribution, be it the RC4, RSA, 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 * included with this distribution is covered by the same copyright terms 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15 * 16 * Copyright remains Eric Young's, and as such any Copyright notices in 17 * the code are not to be removed. 18 * If this package is used in a product, Eric Young should be given attribution 19 * as the author of the parts of the library used. 20 * This can be in the form of a textual message at program startup or 21 * in documentation (online or textual) provided with the package. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * "This product includes cryptographic software written by 34 * Eric Young (eay@cryptsoft.com)" 35 * The word 'cryptographic' can be left out if the rouines from the library 36 * being used are not cryptographic related :-). 37 * 4. If you include any Windows specific code (or a derivative thereof) from 38 * the apps directory (application code) you must include an acknowledgement: 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40 * 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.] 57 */ 58 59 /* Until the key-gen callbacks are modified to use newer prototypes, we allow 60 * deprecated functions for openssl-internal code */ 61 #ifdef OPENSSL_NO_DEPRECATED 62 #undef OPENSSL_NO_DEPRECATED 63 #endif 64 65 #include <stdio.h> 66 #include <stdlib.h> 67 #include <limits.h> 68 #include <string.h> 69 #include <time.h> 70 71 #include "apps.h" 72 73 #include <openssl/asn1.h> 74 #include <openssl/bio.h> 75 #include <openssl/bn.h> 76 #include <openssl/conf.h> 77 #include <openssl/err.h> 78 #include <openssl/evp.h> 79 #include <openssl/objects.h> 80 #include <openssl/pem.h> 81 #include <openssl/x509.h> 82 #include <openssl/x509v3.h> 83 84 #include <openssl/dsa.h> 85 86 #include <openssl/rsa.h> 87 88 #define SECTION "req" 89 90 #define BITS "default_bits" 91 #define KEYFILE "default_keyfile" 92 #define PROMPT "prompt" 93 #define DISTINGUISHED_NAME "distinguished_name" 94 #define ATTRIBUTES "attributes" 95 #define V3_EXTENSIONS "x509_extensions" 96 #define REQ_EXTENSIONS "req_extensions" 97 #define STRING_MASK "string_mask" 98 #define UTF8_IN "utf8" 99 100 #define DEFAULT_KEY_LENGTH 2048 101 #define MIN_KEY_LENGTH 384 102 103 104 /* -inform arg - input format - default PEM (DER or PEM) 105 * -outform arg - output format - default PEM 106 * -in arg - input file - default stdin 107 * -out arg - output file - default stdout 108 * -verify - check request signature 109 * -noout - don't print stuff out. 110 * -text - print out human readable text. 111 * -nodes - no des encryption 112 * -config file - Load configuration file. 113 * -key file - make a request using key in file (or use it for verification). 114 * -keyform arg - key file format. 115 * -newkey - make a key and a request. 116 * -modulus - print RSA modulus. 117 * -pubkey - output Public Key. 118 * -x509 - output a self signed X509 structure instead. 119 * -asn1-kludge - output new certificate request in a format that some CA's 120 * require. This format is wrong 121 */ 122 123 static int make_REQ(X509_REQ * req, EVP_PKEY * pkey, char *dn, int mutlirdn, 124 int attribs, unsigned long chtype); 125 static int build_subject(X509_REQ * req, char *subj, unsigned long chtype, 126 int multirdn); 127 static int prompt_info(X509_REQ * req, 128 STACK_OF(CONF_VALUE) * dn_sk, char *dn_sect, 129 STACK_OF(CONF_VALUE) * attr_sk, char *attr_sect, int attribs, 130 unsigned long chtype); 131 static int auto_info(X509_REQ * req, STACK_OF(CONF_VALUE) * sk, 132 STACK_OF(CONF_VALUE) * attr, int attribs, 133 unsigned long chtype); 134 static int add_attribute_object(X509_REQ * req, char *text, const char *def, 135 char *value, int nid, int n_min, 136 int n_max, unsigned long chtype); 137 static int add_DN_object(X509_NAME * n, char *text, const char *def, char *value, 138 int nid, int n_min, int n_max, unsigned long chtype, int mval); 139 static int genpkey_cb(EVP_PKEY_CTX * ctx); 140 static int req_check_len(int len, int n_min, int n_max); 141 static int check_end(const char *str, const char *end); 142 static EVP_PKEY_CTX *set_keygen_ctx(BIO * err, const char *gstr, int *pkey_type, 143 long *pkeylen, char **palgnam, 144 ENGINE * keygen_engine); 145 static CONF *req_conf = NULL; 146 static int batch = 0; 147 148 int req_main(int, char **); 149 150 int 151 req_main(int argc, char **argv) 152 { 153 ENGINE *e = NULL, *gen_eng = NULL; 154 unsigned long nmflag = 0, reqflag = 0; 155 int ex = 1, x509 = 0, days = 30; 156 X509 *x509ss = NULL; 157 X509_REQ *req = NULL; 158 EVP_PKEY_CTX *genctx = NULL; 159 const char *keyalg = NULL; 160 char *keyalgstr = NULL; 161 STACK_OF(OPENSSL_STRING) * pkeyopts = NULL, *sigopts = NULL; 162 EVP_PKEY *pkey = NULL; 163 int i = 0, badops = 0, newreq = 0, verbose = 0, pkey_type = -1; 164 long newkey = -1; 165 BIO *in = NULL, *out = NULL; 166 int informat, outformat, verify = 0, noout = 0, text = 0, keyform = FORMAT_PEM; 167 int nodes = 0, kludge = 0, newhdr = 0, subject = 0, pubkey = 0; 168 char *infile, *outfile, *prog, *keyfile = NULL, *template = NULL, 169 *keyout = NULL; 170 #ifndef OPENSSL_NO_ENGINE 171 char *engine = NULL; 172 #endif 173 char *extensions = NULL; 174 char *req_exts = NULL; 175 const EVP_CIPHER *cipher = NULL; 176 ASN1_INTEGER *serial = NULL; 177 int modulus = 0; 178 char *passargin = NULL, *passargout = NULL; 179 char *passin = NULL, *passout = NULL; 180 char *p; 181 char *subj = NULL; 182 int multirdn = 0; 183 const EVP_MD *md_alg = NULL, *digest = NULL; 184 unsigned long chtype = MBSTRING_ASC; 185 186 req_conf = NULL; 187 cipher = EVP_aes_256_cbc(); 188 digest = EVP_sha256(); 189 190 infile = NULL; 191 outfile = NULL; 192 informat = FORMAT_PEM; 193 outformat = FORMAT_PEM; 194 195 prog = argv[0]; 196 argc--; 197 argv++; 198 while (argc >= 1) { 199 if (strcmp(*argv, "-inform") == 0) { 200 if (--argc < 1) 201 goto bad; 202 informat = str2fmt(*(++argv)); 203 } else if (strcmp(*argv, "-outform") == 0) { 204 if (--argc < 1) 205 goto bad; 206 outformat = str2fmt(*(++argv)); 207 } 208 #ifndef OPENSSL_NO_ENGINE 209 else if (strcmp(*argv, "-engine") == 0) { 210 if (--argc < 1) 211 goto bad; 212 engine = *(++argv); 213 } else if (strcmp(*argv, "-keygen_engine") == 0) { 214 if (--argc < 1) 215 goto bad; 216 gen_eng = ENGINE_by_id(*(++argv)); 217 if (gen_eng == NULL) { 218 BIO_printf(bio_err, "Can't find keygen engine %s\n", *argv); 219 goto end; 220 } 221 } 222 #endif 223 else if (strcmp(*argv, "-key") == 0) { 224 if (--argc < 1) 225 goto bad; 226 keyfile = *(++argv); 227 } else if (strcmp(*argv, "-pubkey") == 0) { 228 pubkey = 1; 229 } else if (strcmp(*argv, "-new") == 0) { 230 newreq = 1; 231 } else if (strcmp(*argv, "-config") == 0) { 232 if (--argc < 1) 233 goto bad; 234 template = *(++argv); 235 } else if (strcmp(*argv, "-keyform") == 0) { 236 if (--argc < 1) 237 goto bad; 238 keyform = str2fmt(*(++argv)); 239 } else if (strcmp(*argv, "-in") == 0) { 240 if (--argc < 1) 241 goto bad; 242 infile = *(++argv); 243 } else if (strcmp(*argv, "-out") == 0) { 244 if (--argc < 1) 245 goto bad; 246 outfile = *(++argv); 247 } else if (strcmp(*argv, "-keyout") == 0) { 248 if (--argc < 1) 249 goto bad; 250 keyout = *(++argv); 251 } else if (strcmp(*argv, "-passin") == 0) { 252 if (--argc < 1) 253 goto bad; 254 passargin = *(++argv); 255 } else if (strcmp(*argv, "-passout") == 0) { 256 if (--argc < 1) 257 goto bad; 258 passargout = *(++argv); 259 } else if (strcmp(*argv, "-newkey") == 0) { 260 if (--argc < 1) 261 goto bad; 262 keyalg = *(++argv); 263 newreq = 1; 264 } else if (strcmp(*argv, "-pkeyopt") == 0) { 265 if (--argc < 1) 266 goto bad; 267 if (!pkeyopts) 268 pkeyopts = sk_OPENSSL_STRING_new_null(); 269 if (!pkeyopts || !sk_OPENSSL_STRING_push(pkeyopts, *(++argv))) 270 goto bad; 271 } else if (strcmp(*argv, "-sigopt") == 0) { 272 if (--argc < 1) 273 goto bad; 274 if (!sigopts) 275 sigopts = sk_OPENSSL_STRING_new_null(); 276 if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, *(++argv))) 277 goto bad; 278 } else if (strcmp(*argv, "-batch") == 0) 279 batch = 1; 280 else if (strcmp(*argv, "-newhdr") == 0) 281 newhdr = 1; 282 else if (strcmp(*argv, "-modulus") == 0) 283 modulus = 1; 284 else if (strcmp(*argv, "-verify") == 0) 285 verify = 1; 286 else if (strcmp(*argv, "-nodes") == 0) 287 nodes = 1; 288 else if (strcmp(*argv, "-noout") == 0) 289 noout = 1; 290 else if (strcmp(*argv, "-verbose") == 0) 291 verbose = 1; 292 else if (strcmp(*argv, "-utf8") == 0) 293 chtype = MBSTRING_UTF8; 294 else if (strcmp(*argv, "-nameopt") == 0) { 295 if (--argc < 1) 296 goto bad; 297 if (!set_name_ex(&nmflag, *(++argv))) 298 goto bad; 299 } else if (strcmp(*argv, "-reqopt") == 0) { 300 if (--argc < 1) 301 goto bad; 302 if (!set_cert_ex(&reqflag, *(++argv))) 303 goto bad; 304 } else if (strcmp(*argv, "-subject") == 0) 305 subject = 1; 306 else if (strcmp(*argv, "-text") == 0) 307 text = 1; 308 else if (strcmp(*argv, "-x509") == 0) 309 x509 = 1; 310 else if (strcmp(*argv, "-asn1-kludge") == 0) 311 kludge = 1; 312 else if (strcmp(*argv, "-no-asn1-kludge") == 0) 313 kludge = 0; 314 else if (strcmp(*argv, "-subj") == 0) { 315 if (--argc < 1) 316 goto bad; 317 subj = *(++argv); 318 } else if (strcmp(*argv, "-multivalue-rdn") == 0) 319 multirdn = 1; 320 else if (strcmp(*argv, "-days") == 0) { 321 const char *errstr; 322 323 if (--argc < 1) 324 goto bad; 325 days = strtonum(*(++argv), 1, INT_MAX, &errstr); 326 if (errstr) { 327 BIO_printf(bio_err, "bad -days %s, using 0: %s\n", 328 *argv, errstr); 329 days = 30; 330 } 331 } else if (strcmp(*argv, "-set_serial") == 0) { 332 if (--argc < 1) 333 goto bad; 334 serial = s2i_ASN1_INTEGER(NULL, *(++argv)); 335 if (!serial) 336 goto bad; 337 } else if (strcmp(*argv, "-extensions") == 0) { 338 if (--argc < 1) 339 goto bad; 340 extensions = *(++argv); 341 } else if (strcmp(*argv, "-reqexts") == 0) { 342 if (--argc < 1) 343 goto bad; 344 req_exts = *(++argv); 345 } else if ((md_alg = EVP_get_digestbyname(&((*argv)[1]))) != NULL) { 346 /* ok */ 347 digest = md_alg; 348 } else { 349 BIO_printf(bio_err, "unknown option %s\n", *argv); 350 badops = 1; 351 break; 352 } 353 argc--; 354 argv++; 355 } 356 357 if (badops) { 358 bad: 359 BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog); 360 BIO_printf(bio_err, "where options are\n"); 361 BIO_printf(bio_err, " -inform arg input format - DER or PEM\n"); 362 BIO_printf(bio_err, " -outform arg output format - DER or PEM\n"); 363 BIO_printf(bio_err, " -in arg input file\n"); 364 BIO_printf(bio_err, " -out arg output file\n"); 365 BIO_printf(bio_err, " -text text form of request\n"); 366 BIO_printf(bio_err, " -pubkey output public key\n"); 367 BIO_printf(bio_err, " -noout do not output REQ\n"); 368 BIO_printf(bio_err, " -verify verify signature on REQ\n"); 369 BIO_printf(bio_err, " -modulus RSA modulus\n"); 370 BIO_printf(bio_err, " -nodes don't encrypt the output key\n"); 371 #ifndef OPENSSL_NO_ENGINE 372 BIO_printf(bio_err, " -engine e use engine e, possibly a hardware device\n"); 373 #endif 374 BIO_printf(bio_err, " -subject output the request's subject\n"); 375 BIO_printf(bio_err, " -passin private key password source\n"); 376 BIO_printf(bio_err, " -key file use the private key contained in file\n"); 377 BIO_printf(bio_err, " -keyform arg key file format\n"); 378 BIO_printf(bio_err, " -keyout arg file to send the key to\n"); 379 BIO_printf(bio_err, " -newkey rsa:bits generate a new RSA key of 'bits' in size\n"); 380 BIO_printf(bio_err, " -newkey dsa:file generate a new DSA key, parameters taken from CA in 'file'\n"); 381 BIO_printf(bio_err, " -newkey ec:file generate a new EC key, parameters taken from CA in 'file'\n"); 382 BIO_printf(bio_err, " -[digest] Digest to sign with (md5, sha1, md2, mdc2, md4)\n"); 383 BIO_printf(bio_err, " -config file request template file.\n"); 384 BIO_printf(bio_err, " -subj arg set or modify request subject\n"); 385 BIO_printf(bio_err, " -multivalue-rdn enable support for multivalued RDNs\n"); 386 BIO_printf(bio_err, " -new new request.\n"); 387 BIO_printf(bio_err, " -batch do not ask anything during request generation\n"); 388 BIO_printf(bio_err, " -x509 output a x509 structure instead of a cert. req.\n"); 389 BIO_printf(bio_err, " -days number of days a certificate generated by -x509 is valid for.\n"); 390 BIO_printf(bio_err, " -set_serial serial number to use for a certificate generated by -x509.\n"); 391 BIO_printf(bio_err, " -newhdr output \"NEW\" in the header lines\n"); 392 BIO_printf(bio_err, " -asn1-kludge Output the 'request' in a format that is wrong but some CA's\n"); 393 BIO_printf(bio_err, " have been reported as requiring\n"); 394 BIO_printf(bio_err, " -extensions .. specify certificate extension section (override value in config file)\n"); 395 BIO_printf(bio_err, " -reqexts .. specify request extension section (override value in config file)\n"); 396 BIO_printf(bio_err, " -utf8 input characters are UTF8 (default ASCII)\n"); 397 BIO_printf(bio_err, " -nameopt arg - various certificate name options\n"); 398 BIO_printf(bio_err, " -reqopt arg - various request text options\n\n"); 399 goto end; 400 } 401 402 if (!app_passwd(bio_err, passargin, passargout, &passin, &passout)) { 403 BIO_printf(bio_err, "Error getting passwords\n"); 404 goto end; 405 } 406 if (template != NULL) { 407 long errline = -1; 408 409 if (verbose) 410 BIO_printf(bio_err, "Using configuration from %s\n", template); 411 req_conf = NCONF_new(NULL); 412 i = NCONF_load(req_conf, template, &errline); 413 if (i == 0) { 414 BIO_printf(bio_err, "error on line %ld of %s\n", errline, template); 415 goto end; 416 } 417 } else { 418 req_conf = config; 419 420 if (req_conf == NULL) { 421 BIO_printf(bio_err, "Unable to load config info from %s\n", default_config_file); 422 if (newreq) 423 goto end; 424 } else if (verbose) 425 BIO_printf(bio_err, "Using configuration from %s\n", 426 default_config_file); 427 } 428 429 if (req_conf != NULL) { 430 if (!load_config(bio_err, req_conf)) 431 goto end; 432 p = NCONF_get_string(req_conf, NULL, "oid_file"); 433 if (p == NULL) 434 ERR_clear_error(); 435 if (p != NULL) { 436 BIO *oid_bio; 437 438 oid_bio = BIO_new_file(p, "r"); 439 if (oid_bio == NULL) { 440 /* 441 BIO_printf(bio_err,"problems opening %s for extra oid's\n",p); 442 ERR_print_errors(bio_err); 443 */ 444 } else { 445 OBJ_create_objects(oid_bio); 446 BIO_free(oid_bio); 447 } 448 } 449 } 450 if (!add_oid_section(bio_err, req_conf)) 451 goto end; 452 453 if (md_alg == NULL) { 454 p = NCONF_get_string(req_conf, SECTION, "default_md"); 455 if (p == NULL) 456 ERR_clear_error(); 457 if (p != NULL) { 458 if ((md_alg = EVP_get_digestbyname(p)) != NULL) 459 digest = md_alg; 460 } 461 } 462 if (!extensions) { 463 extensions = NCONF_get_string(req_conf, SECTION, V3_EXTENSIONS); 464 if (!extensions) 465 ERR_clear_error(); 466 } 467 if (extensions) { 468 /* Check syntax of file */ 469 X509V3_CTX ctx; 470 X509V3_set_ctx_test(&ctx); 471 X509V3_set_nconf(&ctx, req_conf); 472 if (!X509V3_EXT_add_nconf(req_conf, &ctx, extensions, NULL)) { 473 BIO_printf(bio_err, 474 "Error Loading extension section %s\n", extensions); 475 goto end; 476 } 477 } 478 if (!passin) { 479 passin = NCONF_get_string(req_conf, SECTION, "input_password"); 480 if (!passin) 481 ERR_clear_error(); 482 } 483 if (!passout) { 484 passout = NCONF_get_string(req_conf, SECTION, "output_password"); 485 if (!passout) 486 ERR_clear_error(); 487 } 488 p = NCONF_get_string(req_conf, SECTION, STRING_MASK); 489 if (!p) 490 ERR_clear_error(); 491 492 if (p && !ASN1_STRING_set_default_mask_asc(p)) { 493 BIO_printf(bio_err, "Invalid global string mask setting %s\n", p); 494 goto end; 495 } 496 if (chtype != MBSTRING_UTF8) { 497 p = NCONF_get_string(req_conf, SECTION, UTF8_IN); 498 if (!p) 499 ERR_clear_error(); 500 else if (!strcmp(p, "yes")) 501 chtype = MBSTRING_UTF8; 502 } 503 if (!req_exts) { 504 req_exts = NCONF_get_string(req_conf, SECTION, REQ_EXTENSIONS); 505 if (!req_exts) 506 ERR_clear_error(); 507 } 508 if (req_exts) { 509 /* Check syntax of file */ 510 X509V3_CTX ctx; 511 X509V3_set_ctx_test(&ctx); 512 X509V3_set_nconf(&ctx, req_conf); 513 if (!X509V3_EXT_add_nconf(req_conf, &ctx, req_exts, NULL)) { 514 BIO_printf(bio_err, 515 "Error Loading request extension section %s\n", 516 req_exts); 517 goto end; 518 } 519 } 520 in = BIO_new(BIO_s_file()); 521 out = BIO_new(BIO_s_file()); 522 if ((in == NULL) || (out == NULL)) 523 goto end; 524 525 #ifndef OPENSSL_NO_ENGINE 526 e = setup_engine(bio_err, engine, 0); 527 #endif 528 529 if (keyfile != NULL) { 530 pkey = load_key(bio_err, keyfile, keyform, 0, passin, e, 531 "Private Key"); 532 if (!pkey) { 533 /* 534 * load_key() has already printed an appropriate 535 * message 536 */ 537 goto end; 538 } 539 } 540 if (newreq && (pkey == NULL)) { 541 if (!NCONF_get_number(req_conf, SECTION, BITS, &newkey)) { 542 newkey = DEFAULT_KEY_LENGTH; 543 } 544 if (keyalg) { 545 genctx = set_keygen_ctx(bio_err, keyalg, &pkey_type, &newkey, 546 &keyalgstr, gen_eng); 547 if (!genctx) 548 goto end; 549 } 550 if (newkey < MIN_KEY_LENGTH && (pkey_type == EVP_PKEY_RSA || pkey_type == EVP_PKEY_DSA)) { 551 BIO_printf(bio_err, "private key length is too short,\n"); 552 BIO_printf(bio_err, "it needs to be at least %d bits, not %ld\n", MIN_KEY_LENGTH, newkey); 553 goto end; 554 } 555 if (!genctx) { 556 genctx = set_keygen_ctx(bio_err, NULL, &pkey_type, &newkey, 557 &keyalgstr, gen_eng); 558 if (!genctx) 559 goto end; 560 } 561 if (pkeyopts) { 562 char *genopt; 563 for (i = 0; i < sk_OPENSSL_STRING_num(pkeyopts); i++) { 564 genopt = sk_OPENSSL_STRING_value(pkeyopts, i); 565 if (pkey_ctrl_string(genctx, genopt) <= 0) { 566 BIO_printf(bio_err, 567 "parameter error \"%s\"\n", 568 genopt); 569 ERR_print_errors(bio_err); 570 goto end; 571 } 572 } 573 } 574 BIO_printf(bio_err, "Generating a %ld bit %s private key\n", 575 newkey, keyalgstr); 576 577 EVP_PKEY_CTX_set_cb(genctx, genpkey_cb); 578 EVP_PKEY_CTX_set_app_data(genctx, bio_err); 579 580 if (EVP_PKEY_keygen(genctx, &pkey) <= 0) { 581 BIO_puts(bio_err, "Error Generating Key\n"); 582 goto end; 583 } 584 EVP_PKEY_CTX_free(genctx); 585 genctx = NULL; 586 587 if (keyout == NULL) { 588 keyout = NCONF_get_string(req_conf, SECTION, KEYFILE); 589 if (keyout == NULL) 590 ERR_clear_error(); 591 } 592 if (keyout == NULL) { 593 BIO_printf(bio_err, "writing new private key to stdout\n"); 594 BIO_set_fp(out, stdout, BIO_NOCLOSE); 595 } else { 596 BIO_printf(bio_err, "writing new private key to '%s'\n", keyout); 597 if (BIO_write_filename(out, keyout) <= 0) { 598 perror(keyout); 599 goto end; 600 } 601 } 602 603 p = NCONF_get_string(req_conf, SECTION, "encrypt_rsa_key"); 604 if (p == NULL) { 605 ERR_clear_error(); 606 p = NCONF_get_string(req_conf, SECTION, "encrypt_key"); 607 if (p == NULL) 608 ERR_clear_error(); 609 } 610 if ((p != NULL) && (strcmp(p, "no") == 0)) 611 cipher = NULL; 612 if (nodes) 613 cipher = NULL; 614 615 i = 0; 616 loop: 617 if (!PEM_write_bio_PrivateKey(out, pkey, cipher, 618 NULL, 0, NULL, passout)) { 619 if ((ERR_GET_REASON(ERR_peek_error()) == 620 PEM_R_PROBLEMS_GETTING_PASSWORD) && (i < 3)) { 621 ERR_clear_error(); 622 i++; 623 goto loop; 624 } 625 goto end; 626 } 627 BIO_printf(bio_err, "-----\n"); 628 } 629 if (!newreq) { 630 /* 631 * Since we are using a pre-existing certificate request, the 632 * kludge 'format' info should not be changed. 633 */ 634 kludge = -1; 635 if (infile == NULL) 636 BIO_set_fp(in, stdin, BIO_NOCLOSE); 637 else { 638 if (BIO_read_filename(in, infile) <= 0) { 639 perror(infile); 640 goto end; 641 } 642 } 643 644 if (informat == FORMAT_ASN1) 645 req = d2i_X509_REQ_bio(in, NULL); 646 else if (informat == FORMAT_PEM) 647 req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL); 648 else { 649 BIO_printf(bio_err, "bad input format specified for X509 request\n"); 650 goto end; 651 } 652 if (req == NULL) { 653 BIO_printf(bio_err, "unable to load X509 request\n"); 654 goto end; 655 } 656 } 657 if (newreq || x509) { 658 if (pkey == NULL) { 659 BIO_printf(bio_err, "you need to specify a private key\n"); 660 goto end; 661 } 662 if (req == NULL) { 663 req = X509_REQ_new(); 664 if (req == NULL) { 665 goto end; 666 } 667 i = make_REQ(req, pkey, subj, multirdn, !x509, chtype); 668 subj = NULL; /* done processing '-subj' option */ 669 if ((kludge > 0) && !sk_X509_ATTRIBUTE_num(req->req_info->attributes)) { 670 sk_X509_ATTRIBUTE_free(req->req_info->attributes); 671 req->req_info->attributes = NULL; 672 } 673 if (!i) { 674 BIO_printf(bio_err, "problems making Certificate Request\n"); 675 goto end; 676 } 677 } 678 if (x509) { 679 EVP_PKEY *tmppkey; 680 X509V3_CTX ext_ctx; 681 if ((x509ss = X509_new()) == NULL) 682 goto end; 683 684 /* Set version to V3 */ 685 if (extensions && !X509_set_version(x509ss, 2)) 686 goto end; 687 if (serial) { 688 if (!X509_set_serialNumber(x509ss, serial)) 689 goto end; 690 } else { 691 if (!rand_serial(NULL, 692 X509_get_serialNumber(x509ss))) 693 goto end; 694 } 695 696 if (!X509_set_issuer_name(x509ss, X509_REQ_get_subject_name(req))) 697 goto end; 698 if (!X509_gmtime_adj(X509_get_notBefore(x509ss), 0)) 699 goto end; 700 if (!X509_time_adj_ex(X509_get_notAfter(x509ss), days, 0, NULL)) 701 goto end; 702 if (!X509_set_subject_name(x509ss, X509_REQ_get_subject_name(req))) 703 goto end; 704 tmppkey = X509_REQ_get_pubkey(req); 705 if (!tmppkey || !X509_set_pubkey(x509ss, tmppkey)) 706 goto end; 707 EVP_PKEY_free(tmppkey); 708 709 /* Set up V3 context struct */ 710 711 X509V3_set_ctx(&ext_ctx, x509ss, x509ss, NULL, NULL, 0); 712 X509V3_set_nconf(&ext_ctx, req_conf); 713 714 /* Add extensions */ 715 if (extensions && !X509V3_EXT_add_nconf(req_conf, 716 &ext_ctx, extensions, x509ss)) { 717 BIO_printf(bio_err, 718 "Error Loading extension section %s\n", 719 extensions); 720 goto end; 721 } 722 i = do_X509_sign(bio_err, x509ss, pkey, digest, sigopts); 723 if (!i) { 724 ERR_print_errors(bio_err); 725 goto end; 726 } 727 } else { 728 X509V3_CTX ext_ctx; 729 730 /* Set up V3 context struct */ 731 732 X509V3_set_ctx(&ext_ctx, NULL, NULL, req, NULL, 0); 733 X509V3_set_nconf(&ext_ctx, req_conf); 734 735 /* Add extensions */ 736 if (req_exts && !X509V3_EXT_REQ_add_nconf(req_conf, 737 &ext_ctx, req_exts, req)) { 738 BIO_printf(bio_err, 739 "Error Loading extension section %s\n", 740 req_exts); 741 goto end; 742 } 743 i = do_X509_REQ_sign(bio_err, req, pkey, digest, sigopts); 744 if (!i) { 745 ERR_print_errors(bio_err); 746 goto end; 747 } 748 } 749 } 750 if (subj && x509) { 751 BIO_printf(bio_err, "Cannot modifiy certificate subject\n"); 752 goto end; 753 } 754 if (subj && !x509) { 755 if (verbose) { 756 BIO_printf(bio_err, "Modifying Request's Subject\n"); 757 print_name(bio_err, "old subject=", X509_REQ_get_subject_name(req), nmflag); 758 } 759 if (build_subject(req, subj, chtype, multirdn) == 0) { 760 BIO_printf(bio_err, "ERROR: cannot modify subject\n"); 761 ex = 1; 762 goto end; 763 } 764 req->req_info->enc.modified = 1; 765 766 if (verbose) { 767 print_name(bio_err, "new subject=", X509_REQ_get_subject_name(req), nmflag); 768 } 769 } 770 if (verify && !x509) { 771 int tmp = 0; 772 773 if (pkey == NULL) { 774 pkey = X509_REQ_get_pubkey(req); 775 tmp = 1; 776 if (pkey == NULL) 777 goto end; 778 } 779 i = X509_REQ_verify(req, pkey); 780 if (tmp) { 781 EVP_PKEY_free(pkey); 782 pkey = NULL; 783 } 784 if (i < 0) { 785 goto end; 786 } else if (i == 0) { 787 BIO_printf(bio_err, "verify failure\n"); 788 ERR_print_errors(bio_err); 789 } else /* if (i > 0) */ 790 BIO_printf(bio_err, "verify OK\n"); 791 } 792 if (noout && !text && !modulus && !subject && !pubkey) { 793 ex = 0; 794 goto end; 795 } 796 if (outfile == NULL) { 797 BIO_set_fp(out, stdout, BIO_NOCLOSE); 798 } else { 799 if ((keyout != NULL) && (strcmp(outfile, keyout) == 0)) 800 i = (int) BIO_append_filename(out, outfile); 801 else 802 i = (int) BIO_write_filename(out, outfile); 803 if (!i) { 804 perror(outfile); 805 goto end; 806 } 807 } 808 809 if (pubkey) { 810 EVP_PKEY *tpubkey; 811 tpubkey = X509_REQ_get_pubkey(req); 812 if (tpubkey == NULL) { 813 BIO_printf(bio_err, "Error getting public key\n"); 814 ERR_print_errors(bio_err); 815 goto end; 816 } 817 PEM_write_bio_PUBKEY(out, tpubkey); 818 EVP_PKEY_free(tpubkey); 819 } 820 if (text) { 821 if (x509) 822 X509_print_ex(out, x509ss, nmflag, reqflag); 823 else 824 X509_REQ_print_ex(out, req, nmflag, reqflag); 825 } 826 if (subject) { 827 if (x509) 828 print_name(out, "subject=", X509_get_subject_name(x509ss), nmflag); 829 else 830 print_name(out, "subject=", X509_REQ_get_subject_name(req), nmflag); 831 } 832 if (modulus) { 833 EVP_PKEY *tpubkey; 834 835 if (x509) 836 tpubkey = X509_get_pubkey(x509ss); 837 else 838 tpubkey = X509_REQ_get_pubkey(req); 839 if (tpubkey == NULL) { 840 fprintf(stdout, "Modulus=unavailable\n"); 841 goto end; 842 } 843 fprintf(stdout, "Modulus="); 844 if (EVP_PKEY_base_id(tpubkey) == EVP_PKEY_RSA) 845 BN_print(out, tpubkey->pkey.rsa->n); 846 else 847 fprintf(stdout, "Wrong Algorithm type"); 848 EVP_PKEY_free(tpubkey); 849 fprintf(stdout, "\n"); 850 } 851 if (!noout && !x509) { 852 if (outformat == FORMAT_ASN1) 853 i = i2d_X509_REQ_bio(out, req); 854 else if (outformat == FORMAT_PEM) { 855 if (newhdr) 856 i = PEM_write_bio_X509_REQ_NEW(out, req); 857 else 858 i = PEM_write_bio_X509_REQ(out, req); 859 } else { 860 BIO_printf(bio_err, "bad output format specified for outfile\n"); 861 goto end; 862 } 863 if (!i) { 864 BIO_printf(bio_err, "unable to write X509 request\n"); 865 goto end; 866 } 867 } 868 if (!noout && x509 && (x509ss != NULL)) { 869 if (outformat == FORMAT_ASN1) 870 i = i2d_X509_bio(out, x509ss); 871 else if (outformat == FORMAT_PEM) 872 i = PEM_write_bio_X509(out, x509ss); 873 else { 874 BIO_printf(bio_err, "bad output format specified for outfile\n"); 875 goto end; 876 } 877 if (!i) { 878 BIO_printf(bio_err, "unable to write X509 certificate\n"); 879 goto end; 880 } 881 } 882 ex = 0; 883 end: 884 if (ex) { 885 ERR_print_errors(bio_err); 886 } 887 if ((req_conf != NULL) && (req_conf != config)) 888 NCONF_free(req_conf); 889 BIO_free(in); 890 BIO_free_all(out); 891 EVP_PKEY_free(pkey); 892 if (genctx) 893 EVP_PKEY_CTX_free(genctx); 894 if (pkeyopts) 895 sk_OPENSSL_STRING_free(pkeyopts); 896 if (sigopts) 897 sk_OPENSSL_STRING_free(sigopts); 898 #ifndef OPENSSL_NO_ENGINE 899 if (gen_eng) 900 ENGINE_free(gen_eng); 901 #endif 902 free(keyalgstr); 903 X509_REQ_free(req); 904 X509_free(x509ss); 905 ASN1_INTEGER_free(serial); 906 if (passargin && passin) 907 free(passin); 908 if (passargout && passout) 909 free(passout); 910 OBJ_cleanup(); 911 912 return (ex); 913 } 914 915 static int 916 make_REQ(X509_REQ * req, EVP_PKEY * pkey, char *subj, int multirdn, 917 int attribs, unsigned long chtype) 918 { 919 int ret = 0, i; 920 char no_prompt = 0; 921 STACK_OF(CONF_VALUE) * dn_sk, *attr_sk = NULL; 922 char *tmp, *dn_sect, *attr_sect; 923 924 tmp = NCONF_get_string(req_conf, SECTION, PROMPT); 925 if (tmp == NULL) 926 ERR_clear_error(); 927 if ((tmp != NULL) && !strcmp(tmp, "no")) 928 no_prompt = 1; 929 930 dn_sect = NCONF_get_string(req_conf, SECTION, DISTINGUISHED_NAME); 931 if (dn_sect == NULL) { 932 BIO_printf(bio_err, "unable to find '%s' in config\n", 933 DISTINGUISHED_NAME); 934 goto err; 935 } 936 dn_sk = NCONF_get_section(req_conf, dn_sect); 937 if (dn_sk == NULL) { 938 BIO_printf(bio_err, "unable to get '%s' section\n", dn_sect); 939 goto err; 940 } 941 attr_sect = NCONF_get_string(req_conf, SECTION, ATTRIBUTES); 942 if (attr_sect == NULL) { 943 ERR_clear_error(); 944 attr_sk = NULL; 945 } else { 946 attr_sk = NCONF_get_section(req_conf, attr_sect); 947 if (attr_sk == NULL) { 948 BIO_printf(bio_err, "unable to get '%s' section\n", attr_sect); 949 goto err; 950 } 951 } 952 953 /* setup version number */ 954 if (!X509_REQ_set_version(req, 0L)) 955 goto err; /* version 1 */ 956 957 if (no_prompt) 958 i = auto_info(req, dn_sk, attr_sk, attribs, chtype); 959 else { 960 if (subj) 961 i = build_subject(req, subj, chtype, multirdn); 962 else 963 i = prompt_info(req, dn_sk, dn_sect, attr_sk, attr_sect, attribs, chtype); 964 } 965 if (!i) 966 goto err; 967 968 if (!X509_REQ_set_pubkey(req, pkey)) 969 goto err; 970 971 ret = 1; 972 err: 973 return (ret); 974 } 975 976 /* 977 * subject is expected to be in the format /type0=value0/type1=value1/type2=... 978 * where characters may be escaped by \ 979 */ 980 static int 981 build_subject(X509_REQ * req, char *subject, unsigned long chtype, int multirdn) 982 { 983 X509_NAME *n; 984 985 if (!(n = parse_name(subject, chtype, multirdn))) 986 return 0; 987 988 if (!X509_REQ_set_subject_name(req, n)) { 989 X509_NAME_free(n); 990 return 0; 991 } 992 X509_NAME_free(n); 993 return 1; 994 } 995 996 997 static int 998 prompt_info(X509_REQ * req, 999 STACK_OF(CONF_VALUE) * dn_sk, char *dn_sect, 1000 STACK_OF(CONF_VALUE) * attr_sk, char *attr_sect, int attribs, 1001 unsigned long chtype) 1002 { 1003 int i; 1004 char *p, *q; 1005 char buf[100]; 1006 int nid, mval; 1007 long n_min, n_max; 1008 char *type, *value; 1009 const char *def; 1010 CONF_VALUE *v; 1011 X509_NAME *subj; 1012 subj = X509_REQ_get_subject_name(req); 1013 1014 if (!batch) { 1015 BIO_printf(bio_err, "You are about to be asked to enter information that will be incorporated\n"); 1016 BIO_printf(bio_err, "into your certificate request.\n"); 1017 BIO_printf(bio_err, "What you are about to enter is what is called a Distinguished Name or a DN.\n"); 1018 BIO_printf(bio_err, "There are quite a few fields but you can leave some blank\n"); 1019 BIO_printf(bio_err, "For some fields there will be a default value,\n"); 1020 BIO_printf(bio_err, "If you enter '.', the field will be left blank.\n"); 1021 BIO_printf(bio_err, "-----\n"); 1022 } 1023 if (sk_CONF_VALUE_num(dn_sk)) { 1024 i = -1; 1025 start: for (;;) { 1026 int ret; 1027 i++; 1028 if (sk_CONF_VALUE_num(dn_sk) <= i) 1029 break; 1030 1031 v = sk_CONF_VALUE_value(dn_sk, i); 1032 p = q = NULL; 1033 type = v->name; 1034 if (!check_end(type, "_min") || !check_end(type, "_max") || 1035 !check_end(type, "_default") || 1036 !check_end(type, "_value")) 1037 continue; 1038 /* 1039 * Skip past any leading X. X: X, etc to allow for 1040 * multiple instances 1041 */ 1042 for (p = v->name; *p; p++) 1043 if ((*p == ':') || (*p == ',') || 1044 (*p == '.')) { 1045 p++; 1046 if (*p) 1047 type = p; 1048 break; 1049 } 1050 if (*type == '+') { 1051 mval = -1; 1052 type++; 1053 } else 1054 mval = 0; 1055 /* If OBJ not recognised ignore it */ 1056 if ((nid = OBJ_txt2nid(type)) == NID_undef) 1057 goto start; 1058 ret = snprintf(buf, sizeof buf, "%s_default", v->name); 1059 if (ret == -1 || ret >= sizeof(buf)) { 1060 BIO_printf(bio_err, "Name '%s' too long for default\n", 1061 v->name); 1062 return 0; 1063 } 1064 if ((def = NCONF_get_string(req_conf, dn_sect, buf)) == NULL) { 1065 ERR_clear_error(); 1066 def = ""; 1067 } 1068 ret = snprintf(buf, sizeof buf, "%s_value", v->name); 1069 if (ret == -1 || ret >= sizeof(buf)) { 1070 BIO_printf(bio_err, "Name '%s' too long for value\n", 1071 v->name); 1072 return 0; 1073 } 1074 if ((value = NCONF_get_string(req_conf, dn_sect, buf)) == NULL) { 1075 ERR_clear_error(); 1076 value = NULL; 1077 } 1078 ret = snprintf(buf, sizeof buf, "%s_min", v->name); 1079 if (ret == -1 || ret >= sizeof(buf)) { 1080 BIO_printf(bio_err, "Name '%s' too long for min\n", 1081 v->name); 1082 return 0; 1083 } 1084 if (!NCONF_get_number(req_conf, dn_sect, buf, &n_min)) { 1085 ERR_clear_error(); 1086 n_min = -1; 1087 } 1088 ret = snprintf(buf, sizeof buf, "%s_max", v->name); 1089 if (ret == -1 || ret >= sizeof(buf)) { 1090 BIO_printf(bio_err, "Name '%s' too long for max\n", 1091 v->name); 1092 return 0; 1093 } 1094 if (!NCONF_get_number(req_conf, dn_sect, buf, &n_max)) { 1095 ERR_clear_error(); 1096 n_max = -1; 1097 } 1098 if (!add_DN_object(subj, v->value, def, value, nid, 1099 n_min, n_max, chtype, mval)) 1100 return 0; 1101 } 1102 if (X509_NAME_entry_count(subj) == 0) { 1103 BIO_printf(bio_err, "error, no objects specified in config file\n"); 1104 return 0; 1105 } 1106 if (attribs) { 1107 if ((attr_sk != NULL) && (sk_CONF_VALUE_num(attr_sk) > 0) && 1108 (!batch)) { 1109 BIO_printf(bio_err, 1110 "\nPlease enter the following 'extra' attributes\n"); 1111 BIO_printf(bio_err, 1112 "to be sent with your certificate request\n"); 1113 } 1114 i = -1; 1115 start2: for (;;) { 1116 int ret; 1117 i++; 1118 if ((attr_sk == NULL) || 1119 (sk_CONF_VALUE_num(attr_sk) <= i)) 1120 break; 1121 1122 v = sk_CONF_VALUE_value(attr_sk, i); 1123 type = v->name; 1124 if ((nid = OBJ_txt2nid(type)) == NID_undef) 1125 goto start2; 1126 ret = snprintf(buf, sizeof buf, "%s_default", type); 1127 if (ret == -1 || ret >= sizeof(buf)) { 1128 BIO_printf(bio_err, "Name '%s' too long for default\n", 1129 v->name); 1130 return 0; 1131 } 1132 if ((def = NCONF_get_string(req_conf, attr_sect, buf)) 1133 == NULL) { 1134 ERR_clear_error(); 1135 def = ""; 1136 } 1137 ret = snprintf(buf, sizeof buf, "%s_value", type); 1138 if (ret == -1 || ret >= sizeof(buf)) { 1139 BIO_printf(bio_err, "Name '%s' too long for value\n", 1140 v->name); 1141 return 0; 1142 } 1143 if ((value = NCONF_get_string(req_conf, attr_sect, buf)) 1144 == NULL) { 1145 ERR_clear_error(); 1146 value = NULL; 1147 } 1148 ret = snprintf(buf, sizeof buf, "%s_min", type); 1149 if (ret == -1 || ret >= sizeof(buf)) { 1150 BIO_printf(bio_err, "Name '%s' too long for min\n", 1151 v->name); 1152 return 0; 1153 } 1154 if (!NCONF_get_number(req_conf, attr_sect, buf, &n_min)) { 1155 ERR_clear_error(); 1156 n_min = -1; 1157 } 1158 ret = snprintf(buf, sizeof buf, "%s_max", type); 1159 if (ret == -1 || ret >= sizeof(buf)) { 1160 BIO_printf(bio_err, "Name '%s' too long for max\n", 1161 v->name); 1162 return 0; 1163 } 1164 if (!NCONF_get_number(req_conf, attr_sect, buf, &n_max)) { 1165 ERR_clear_error(); 1166 n_max = -1; 1167 } 1168 if (!add_attribute_object(req, 1169 v->value, def, value, nid, n_min, n_max, chtype)) 1170 return 0; 1171 } 1172 } 1173 } else { 1174 BIO_printf(bio_err, "No template, please set one up.\n"); 1175 return 0; 1176 } 1177 1178 return 1; 1179 1180 } 1181 1182 static int 1183 auto_info(X509_REQ * req, STACK_OF(CONF_VALUE) * dn_sk, 1184 STACK_OF(CONF_VALUE) * attr_sk, int attribs, unsigned long chtype) 1185 { 1186 int i; 1187 char *p, *q; 1188 char *type; 1189 CONF_VALUE *v; 1190 X509_NAME *subj; 1191 1192 subj = X509_REQ_get_subject_name(req); 1193 1194 for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) { 1195 int mval; 1196 v = sk_CONF_VALUE_value(dn_sk, i); 1197 p = q = NULL; 1198 type = v->name; 1199 /* 1200 * Skip past any leading X. X: X, etc to allow for multiple 1201 * instances 1202 */ 1203 for (p = v->name; *p; p++) 1204 if ((*p == ':') || (*p == ',') || (*p == '.')) { 1205 p++; 1206 if (*p) 1207 type = p; 1208 break; 1209 } 1210 if (*p == '+') { 1211 p++; 1212 mval = -1; 1213 } else 1214 mval = 0; 1215 if (!X509_NAME_add_entry_by_txt(subj, type, chtype, 1216 (unsigned char *) v->value, -1, -1, mval)) 1217 return 0; 1218 1219 } 1220 1221 if (!X509_NAME_entry_count(subj)) { 1222 BIO_printf(bio_err, "error, no objects specified in config file\n"); 1223 return 0; 1224 } 1225 if (attribs) { 1226 for (i = 0; i < sk_CONF_VALUE_num(attr_sk); i++) { 1227 v = sk_CONF_VALUE_value(attr_sk, i); 1228 if (!X509_REQ_add1_attr_by_txt(req, v->name, chtype, 1229 (unsigned char *) v->value, -1)) 1230 return 0; 1231 } 1232 } 1233 return 1; 1234 } 1235 1236 1237 static int 1238 add_DN_object(X509_NAME * n, char *text, const char *def, char *value, 1239 int nid, int n_min, int n_max, unsigned long chtype, int mval) 1240 { 1241 int i, ret = 0; 1242 char buf[1024]; 1243 start: 1244 if (!batch) 1245 BIO_printf(bio_err, "%s [%s]:", text, def); 1246 (void) BIO_flush(bio_err); 1247 if (value != NULL) { 1248 strlcpy(buf, value, sizeof buf); 1249 strlcat(buf, "\n", sizeof buf); 1250 BIO_printf(bio_err, "%s\n", value); 1251 } else { 1252 buf[0] = '\0'; 1253 if (!batch) { 1254 if (!fgets(buf, sizeof buf, stdin)) 1255 return 0; 1256 } else { 1257 buf[0] = '\n'; 1258 buf[1] = '\0'; 1259 } 1260 } 1261 1262 if (buf[0] == '\0') 1263 return (0); 1264 else if (buf[0] == '\n') { 1265 if ((def == NULL) || (def[0] == '\0')) 1266 return (1); 1267 strlcpy(buf, def, sizeof buf); 1268 strlcat(buf, "\n", sizeof buf); 1269 } else if ((buf[0] == '.') && (buf[1] == '\n')) 1270 return (1); 1271 1272 i = strlen(buf); 1273 if (buf[i - 1] != '\n') { 1274 BIO_printf(bio_err, "weird input :-(\n"); 1275 return (0); 1276 } 1277 buf[--i] = '\0'; 1278 if (!req_check_len(i, n_min, n_max)) 1279 goto start; 1280 if (!X509_NAME_add_entry_by_NID(n, nid, chtype, 1281 (unsigned char *) buf, -1, -1, mval)) 1282 goto err; 1283 ret = 1; 1284 err: 1285 return (ret); 1286 } 1287 1288 static int 1289 add_attribute_object(X509_REQ * req, char *text, const char *def, 1290 char *value, int nid, int n_min, 1291 int n_max, unsigned long chtype) 1292 { 1293 int i; 1294 static char buf[1024]; 1295 1296 start: 1297 if (!batch) 1298 BIO_printf(bio_err, "%s [%s]:", text, def); 1299 (void) BIO_flush(bio_err); 1300 if (value != NULL) { 1301 strlcpy(buf, value, sizeof buf); 1302 strlcat(buf, "\n", sizeof buf); 1303 BIO_printf(bio_err, "%s\n", value); 1304 } else { 1305 buf[0] = '\0'; 1306 if (!batch) { 1307 if (!fgets(buf, sizeof buf, stdin)) 1308 return 0; 1309 } else { 1310 buf[0] = '\n'; 1311 buf[1] = '\0'; 1312 } 1313 } 1314 1315 if (buf[0] == '\0') 1316 return (0); 1317 else if (buf[0] == '\n') { 1318 if ((def == NULL) || (def[0] == '\0')) 1319 return (1); 1320 strlcpy(buf, def, sizeof buf); 1321 strlcat(buf, "\n", sizeof buf); 1322 } else if ((buf[0] == '.') && (buf[1] == '\n')) 1323 return (1); 1324 1325 i = strlen(buf); 1326 if (buf[i - 1] != '\n') { 1327 BIO_printf(bio_err, "weird input :-(\n"); 1328 return (0); 1329 } 1330 buf[--i] = '\0'; 1331 if (!req_check_len(i, n_min, n_max)) 1332 goto start; 1333 1334 if (!X509_REQ_add1_attr_by_NID(req, nid, chtype, 1335 (unsigned char *) buf, -1)) { 1336 BIO_printf(bio_err, "Error adding attribute\n"); 1337 ERR_print_errors(bio_err); 1338 goto err; 1339 } 1340 return (1); 1341 err: 1342 return (0); 1343 } 1344 1345 static int 1346 req_check_len(int len, int n_min, int n_max) 1347 { 1348 if ((n_min > 0) && (len < n_min)) { 1349 BIO_printf(bio_err, "string is too short, it needs to be at least %d bytes long\n", n_min); 1350 return (0); 1351 } 1352 if ((n_max >= 0) && (len > n_max)) { 1353 BIO_printf(bio_err, "string is too long, it needs to be less than %d bytes long\n", n_max); 1354 return (0); 1355 } 1356 return (1); 1357 } 1358 1359 /* Check if the end of a string matches 'end' */ 1360 static int 1361 check_end(const char *str, const char *end) 1362 { 1363 int elen, slen; 1364 const char *tmp; 1365 elen = strlen(end); 1366 slen = strlen(str); 1367 if (elen > slen) 1368 return 1; 1369 tmp = str + slen - elen; 1370 return strcmp(tmp, end); 1371 } 1372 1373 static EVP_PKEY_CTX * 1374 set_keygen_ctx(BIO * err, const char *gstr, int *pkey_type, 1375 long *pkeylen, char **palgnam, 1376 ENGINE * keygen_engine) 1377 { 1378 EVP_PKEY_CTX *gctx = NULL; 1379 EVP_PKEY *param = NULL; 1380 long keylen = -1; 1381 BIO *pbio = NULL; 1382 const char *paramfile = NULL; 1383 const char *errstr; 1384 1385 if (gstr == NULL) { 1386 *pkey_type = EVP_PKEY_RSA; 1387 keylen = *pkeylen; 1388 } else if (gstr[0] >= '0' && gstr[0] <= '9') { 1389 *pkey_type = EVP_PKEY_RSA; 1390 keylen = strtonum(gstr, 0, LONG_MAX, &errstr); 1391 if (errstr) { 1392 BIO_printf(err, "bad algorithm %s: %s\n", gstr, errstr); 1393 return NULL; 1394 } 1395 *pkeylen = keylen; 1396 } else if (!strncmp(gstr, "param:", 6)) 1397 paramfile = gstr + 6; 1398 else { 1399 const char *p = strchr(gstr, ':'); 1400 int len; 1401 ENGINE *tmpeng; 1402 const EVP_PKEY_ASN1_METHOD *ameth; 1403 1404 if (p) 1405 len = p - gstr; 1406 else 1407 len = strlen(gstr); 1408 /* 1409 * The lookup of a the string will cover all engines so keep 1410 * a note of the implementation. 1411 */ 1412 1413 ameth = EVP_PKEY_asn1_find_str(&tmpeng, gstr, len); 1414 1415 if (!ameth) { 1416 BIO_printf(err, "Unknown algorithm %.*s\n", len, gstr); 1417 return NULL; 1418 } 1419 EVP_PKEY_asn1_get0_info(NULL, pkey_type, NULL, NULL, NULL, 1420 ameth); 1421 #ifndef OPENSSL_NO_ENGINE 1422 if (tmpeng) 1423 ENGINE_finish(tmpeng); 1424 #endif 1425 if (*pkey_type == EVP_PKEY_RSA) { 1426 if (p) { 1427 keylen = strtonum(p + 1, 0, LONG_MAX, &errstr); 1428 if (errstr) { 1429 BIO_printf(err, "bad algorithm %s: %s\n", 1430 p + 1, errstr); 1431 return NULL; 1432 } 1433 *pkeylen = keylen; 1434 } else 1435 keylen = *pkeylen; 1436 } else if (p) 1437 paramfile = p + 1; 1438 } 1439 1440 if (paramfile) { 1441 pbio = BIO_new_file(paramfile, "r"); 1442 if (!pbio) { 1443 BIO_printf(err, "Can't open parameter file %s\n", 1444 paramfile); 1445 return NULL; 1446 } 1447 param = PEM_read_bio_Parameters(pbio, NULL); 1448 1449 if (!param) { 1450 X509 *x; 1451 (void) BIO_reset(pbio); 1452 x = PEM_read_bio_X509(pbio, NULL, NULL, NULL); 1453 if (x) { 1454 param = X509_get_pubkey(x); 1455 X509_free(x); 1456 } 1457 } 1458 BIO_free(pbio); 1459 1460 if (!param) { 1461 BIO_printf(err, "Error reading parameter file %s\n", 1462 paramfile); 1463 return NULL; 1464 } 1465 if (*pkey_type == -1) 1466 *pkey_type = EVP_PKEY_id(param); 1467 else if (*pkey_type != EVP_PKEY_base_id(param)) { 1468 BIO_printf(err, "Key Type does not match parameters\n"); 1469 EVP_PKEY_free(param); 1470 return NULL; 1471 } 1472 } 1473 if (palgnam) { 1474 const EVP_PKEY_ASN1_METHOD *ameth; 1475 ENGINE *tmpeng; 1476 const char *anam; 1477 ameth = EVP_PKEY_asn1_find(&tmpeng, *pkey_type); 1478 if (!ameth) { 1479 BIO_puts(err, "Internal error: can't find key algorithm\n"); 1480 return NULL; 1481 } 1482 EVP_PKEY_asn1_get0_info(NULL, NULL, NULL, NULL, &anam, ameth); 1483 *palgnam = strdup(anam); 1484 #ifndef OPENSSL_NO_ENGINE 1485 if (tmpeng) 1486 ENGINE_finish(tmpeng); 1487 #endif 1488 } 1489 if (param) { 1490 gctx = EVP_PKEY_CTX_new(param, keygen_engine); 1491 *pkeylen = EVP_PKEY_bits(param); 1492 EVP_PKEY_free(param); 1493 } else 1494 gctx = EVP_PKEY_CTX_new_id(*pkey_type, keygen_engine); 1495 1496 if (!gctx) { 1497 BIO_puts(err, "Error allocating keygen context\n"); 1498 ERR_print_errors(err); 1499 return NULL; 1500 } 1501 if (EVP_PKEY_keygen_init(gctx) <= 0) { 1502 BIO_puts(err, "Error initializing keygen context\n"); 1503 ERR_print_errors(err); 1504 return NULL; 1505 } 1506 if ((*pkey_type == EVP_PKEY_RSA) && (keylen != -1)) { 1507 if (EVP_PKEY_CTX_set_rsa_keygen_bits(gctx, keylen) <= 0) { 1508 BIO_puts(err, "Error setting RSA keysize\n"); 1509 ERR_print_errors(err); 1510 EVP_PKEY_CTX_free(gctx); 1511 return NULL; 1512 } 1513 } 1514 1515 return gctx; 1516 } 1517 1518 static int 1519 genpkey_cb(EVP_PKEY_CTX * ctx) 1520 { 1521 char c = '*'; 1522 BIO *b = EVP_PKEY_CTX_get_app_data(ctx); 1523 int p; 1524 p = EVP_PKEY_CTX_get_keygen_info(ctx, 0); 1525 if (p == 0) 1526 c = '.'; 1527 if (p == 1) 1528 c = '+'; 1529 if (p == 2) 1530 c = '*'; 1531 if (p == 3) 1532 c = '\n'; 1533 BIO_write(b, &c, 1); 1534 (void) BIO_flush(b); 1535 return 1; 1536 } 1537 1538 static int 1539 do_sign_init(BIO * err, EVP_MD_CTX * ctx, EVP_PKEY * pkey, 1540 const EVP_MD * md, STACK_OF(OPENSSL_STRING) * sigopts) 1541 { 1542 EVP_PKEY_CTX *pkctx = NULL; 1543 int i; 1544 EVP_MD_CTX_init(ctx); 1545 if (!EVP_DigestSignInit(ctx, &pkctx, md, NULL, pkey)) 1546 return 0; 1547 for (i = 0; i < sk_OPENSSL_STRING_num(sigopts); i++) { 1548 char *sigopt = sk_OPENSSL_STRING_value(sigopts, i); 1549 if (pkey_ctrl_string(pkctx, sigopt) <= 0) { 1550 BIO_printf(err, "parameter error \"%s\"\n", sigopt); 1551 ERR_print_errors(bio_err); 1552 return 0; 1553 } 1554 } 1555 return 1; 1556 } 1557 1558 int 1559 do_X509_sign(BIO * err, X509 * x, EVP_PKEY * pkey, const EVP_MD * md, 1560 STACK_OF(OPENSSL_STRING) * sigopts) 1561 { 1562 int rv; 1563 EVP_MD_CTX mctx; 1564 EVP_MD_CTX_init(&mctx); 1565 rv = do_sign_init(err, &mctx, pkey, md, sigopts); 1566 if (rv > 0) 1567 rv = X509_sign_ctx(x, &mctx); 1568 EVP_MD_CTX_cleanup(&mctx); 1569 return rv > 0 ? 1 : 0; 1570 } 1571 1572 1573 int 1574 do_X509_REQ_sign(BIO * err, X509_REQ * x, EVP_PKEY * pkey, const EVP_MD * md, 1575 STACK_OF(OPENSSL_STRING) * sigopts) 1576 { 1577 int rv; 1578 EVP_MD_CTX mctx; 1579 EVP_MD_CTX_init(&mctx); 1580 rv = do_sign_init(err, &mctx, pkey, md, sigopts); 1581 if (rv > 0) 1582 rv = X509_REQ_sign_ctx(x, &mctx); 1583 EVP_MD_CTX_cleanup(&mctx); 1584 return rv > 0 ? 1 : 0; 1585 } 1586 1587 1588 1589 int 1590 do_X509_CRL_sign(BIO * err, X509_CRL * x, EVP_PKEY * pkey, const EVP_MD * md, 1591 STACK_OF(OPENSSL_STRING) * sigopts) 1592 { 1593 int rv; 1594 EVP_MD_CTX mctx; 1595 EVP_MD_CTX_init(&mctx); 1596 rv = do_sign_init(err, &mctx, pkey, md, sigopts); 1597 if (rv > 0) 1598 rv = X509_CRL_sign_ctx(x, &mctx); 1599 EVP_MD_CTX_cleanup(&mctx); 1600 return rv > 0 ? 1 : 0; 1601 } 1602