- if (template != NULL)
- {
- long errline;
-
- BIO_printf(bio_err,"Using configuration from %s\n",template);
- req_conf=CONF_load(NULL,template,&errline);
- if (req_conf == NULL)
- {
- BIO_printf(bio_err,"error on line %ld of %s\n",errline,template);
- goto end;
- }
- }
- else
- {
- req_conf=config;
- BIO_printf(bio_err,"Using configuration from %s\n",
- default_config_file);
- if (req_conf == NULL)
- {
- BIO_printf(bio_err,"Unable to load config info\n");
- }
- }
-
- if (req_conf != NULL)
- {
- p=CONF_get_string(req_conf,NULL,"oid_file");
- if (p != NULL)
- {
- BIO *oid_bio;
-
- oid_bio=BIO_new_file(p,"r");
- if (oid_bio == NULL)
- {
- /*
- BIO_printf(bio_err,"problems opening %s for extra oid's\n",p);
- ERR_print_errors(bio_err);
- */
- }
- else
- {
- OBJ_create_objects(oid_bio);
- BIO_free(oid_bio);
- }
- }
- }
- if(!add_oid_section(bio_err, req_conf)) goto end;
-
- if ((md_alg == NULL) &&
- ((p=CONF_get_string(req_conf,SECTION,"default_md")) != NULL))
- {
- if ((md_alg=EVP_get_digestbyname(p)) != NULL)
- digest=md_alg;
- }
-
- if(!extensions)
- extensions = CONF_get_string(req_conf, SECTION, V3_EXTENSIONS);
- if(extensions) {
- /* Check syntax of file */
- X509V3_CTX ctx;
- X509V3_set_ctx_test(&ctx);
- X509V3_set_conf_lhash(&ctx, req_conf);
- if(!X509V3_EXT_add_conf(req_conf, &ctx, extensions, NULL)) {
- BIO_printf(bio_err,
- "Error Loading extension section %s\n", extensions);
- goto end;
- }
- }
-
- if(!passin)
- passin = CONF_get_string(req_conf, SECTION, "input_password");
-
- if(!passout)
- passout = CONF_get_string(req_conf, SECTION, "output_password");
-
- p = CONF_get_string(req_conf, SECTION, STRING_MASK);
-
- if(p && !ASN1_STRING_set_default_mask_asc(p)) {
- BIO_printf(bio_err, "Invalid global string mask setting %s\n", p);
- goto end;
- }
-
- if(!req_exts)
- req_exts = CONF_get_string(req_conf, SECTION, REQ_EXTENSIONS);
- if(req_exts) {
- /* Check syntax of file */
- X509V3_CTX ctx;
- X509V3_set_ctx_test(&ctx);
- X509V3_set_conf_lhash(&ctx, req_conf);
- if(!X509V3_EXT_add_conf(req_conf, &ctx, req_exts, NULL)) {
- BIO_printf(bio_err,
- "Error Loading request extension section %s\n",
- req_exts);
- goto end;
- }
- }
-
- in=BIO_new(BIO_s_file());
- out=BIO_new(BIO_s_file());
- if ((in == NULL) || (out == NULL))
- goto end;
-
- if (keyfile != NULL)
- {
- if (BIO_read_filename(in,keyfile) <= 0)
- {
- perror(keyfile);
- goto end;
- }
-
- if (keyform == FORMAT_ASN1)
- pkey=d2i_PrivateKey_bio(in,NULL);
- else if (keyform == FORMAT_PEM)
- {
- pkey=PEM_read_bio_PrivateKey(in,NULL,NULL,passin);
- }
- else
- {
- BIO_printf(bio_err,"bad input format specified for X509 request\n");
- goto end;
- }
-
- if (pkey == NULL)
- {
- BIO_printf(bio_err,"unable to load Private key\n");
- goto end;
- }
- if (EVP_PKEY_type(pkey->type) == EVP_PKEY_DSA)
- {
- char *randfile = CONF_get_string(req_conf,SECTION,"RANDFILE");
- app_RAND_load_file(randfile, bio_err, 0);
- }
- }
-
- if (newreq && (pkey == NULL))
- {
- char *randfile = CONF_get_string(req_conf,SECTION,"RANDFILE");
- app_RAND_load_file(randfile, bio_err, 0);
- if (inrand)
- app_RAND_load_files(inrand);
-
- if (newkey <= 0)
- {
- newkey=(int)CONF_get_number(req_conf,SECTION,BITS);
- if (newkey <= 0)
- newkey=DEFAULT_KEY_LENGTH;
- }
-
- if (newkey < MIN_KEY_LENGTH)
- {
- BIO_printf(bio_err,"private key length is too short,\n");
- BIO_printf(bio_err,"it needs to be at least %d bits, not %d\n",MIN_KEY_LENGTH,newkey);
- goto end;
- }
- BIO_printf(bio_err,"Generating a %d bit %s private key\n",
- newkey,(pkey_type == TYPE_RSA)?"RSA":"DSA");
-
- if ((pkey=EVP_PKEY_new()) == NULL) goto end;
-
-#ifndef NO_RSA
- if (pkey_type == TYPE_RSA)
- {
- if (!EVP_PKEY_assign_RSA(pkey,
- RSA_generate_key(newkey,0x10001,
- req_cb,bio_err)))
- goto end;
- }
- else
+ if (template != NULL) {
+ long errline = -1;
+
+ if (verbose)
+ BIO_printf(bio_err, "Using configuration from %s\n", template);
+ req_conf = NCONF_new(NULL);
+ i = NCONF_load(req_conf, template, &errline);
+ if (i == 0) {
+ BIO_printf(bio_err, "error on line %ld of %s\n", errline,
+ template);
+ goto end;
+ }
+ } else {
+ req_conf = config;
+
+ if (req_conf == NULL) {
+ BIO_printf(bio_err, "Unable to load config info from %s\n",
+ default_config_file);
+ if (newreq)
+ goto end;
+ } else if (verbose)
+ BIO_printf(bio_err, "Using configuration from %s\n",
+ default_config_file);
+ }
+
+ if (req_conf != NULL) {
+ if (!load_config(bio_err, req_conf))
+ goto end;
+ p = NCONF_get_string(req_conf, NULL, "oid_file");
+ if (p == NULL)
+ ERR_clear_error();
+ if (p != NULL) {
+ BIO *oid_bio;
+
+ oid_bio = BIO_new_file(p, "r");
+ if (oid_bio == NULL) {
+ /*-
+ BIO_printf(bio_err,"problems opening %s for extra oid's\n",p);
+ ERR_print_errors(bio_err);
+ */
+ } else {
+ OBJ_create_objects(oid_bio);
+ BIO_free(oid_bio);
+ }
+ }
+ }
+ if (!add_oid_section(bio_err, req_conf))
+ goto end;
+
+ if (md_alg == NULL) {
+ p = NCONF_get_string(req_conf, SECTION, "default_md");
+ if (p == NULL)
+ ERR_clear_error();
+ if (p != NULL) {
+ if ((md_alg = EVP_get_digestbyname(p)) != NULL)
+ digest = md_alg;
+ }
+ }
+
+ if (!extensions) {
+ extensions = NCONF_get_string(req_conf, SECTION, V3_EXTENSIONS);
+ if (!extensions)
+ ERR_clear_error();
+ }
+ if (extensions) {
+ /* Check syntax of file */
+ X509V3_CTX ctx;
+ X509V3_set_ctx_test(&ctx);
+ X509V3_set_nconf(&ctx, req_conf);
+ if (!X509V3_EXT_add_nconf(req_conf, &ctx, extensions, NULL)) {
+ BIO_printf(bio_err,
+ "Error Loading extension section %s\n", extensions);
+ goto end;
+ }
+ }
+
+ if (!passin) {
+ passin = NCONF_get_string(req_conf, SECTION, "input_password");
+ if (!passin)
+ ERR_clear_error();
+ }
+
+ if (!passout) {
+ passout = NCONF_get_string(req_conf, SECTION, "output_password");
+ if (!passout)
+ ERR_clear_error();
+ }
+
+ p = NCONF_get_string(req_conf, SECTION, STRING_MASK);
+ if (!p)
+ ERR_clear_error();
+
+ if (p && !ASN1_STRING_set_default_mask_asc(p)) {
+ BIO_printf(bio_err, "Invalid global string mask setting %s\n", p);
+ goto end;
+ }
+
+ if (chtype != MBSTRING_UTF8) {
+ p = NCONF_get_string(req_conf, SECTION, UTF8_IN);
+ if (!p)
+ ERR_clear_error();
+ else if (!strcmp(p, "yes"))
+ chtype = MBSTRING_UTF8;
+ }
+
+ if (!req_exts) {
+ req_exts = NCONF_get_string(req_conf, SECTION, REQ_EXTENSIONS);
+ if (!req_exts)
+ ERR_clear_error();
+ }
+ if (req_exts) {
+ /* Check syntax of file */
+ X509V3_CTX ctx;
+ X509V3_set_ctx_test(&ctx);
+ X509V3_set_nconf(&ctx, req_conf);
+ if (!X509V3_EXT_add_nconf(req_conf, &ctx, req_exts, NULL)) {
+ BIO_printf(bio_err,
+ "Error Loading request extension section %s\n",
+ req_exts);
+ goto end;
+ }
+ }
+
+ in = BIO_new(BIO_s_file());
+ out = BIO_new(BIO_s_file());
+ if ((in == NULL) || (out == NULL))
+ goto end;
+
+ e = setup_engine(bio_err, engine, 0);
+
+ if (keyfile != NULL) {
+ pkey = load_key(bio_err, keyfile, keyform, 0, passin, e,
+ "Private Key");
+ if (!pkey) {
+ /*
+ * load_key() has already printed an appropriate message
+ */
+ goto end;
+ } else {
+ char *randfile = NCONF_get_string(req_conf, SECTION, "RANDFILE");
+ if (randfile == NULL)
+ ERR_clear_error();
+ app_RAND_load_file(randfile, bio_err, 0);
+ }
+ }
+
+ if (newreq && (pkey == NULL)) {
+ char *randfile = NCONF_get_string(req_conf, SECTION, "RANDFILE");
+ if (randfile == NULL)
+ ERR_clear_error();
+ app_RAND_load_file(randfile, bio_err, 0);
+ if (inrand)
+ app_RAND_load_files(inrand);
+
+ if (!NCONF_get_number(req_conf, SECTION, BITS, &newkey)) {
+ newkey = DEFAULT_KEY_LENGTH;
+ }
+
+ if (keyalg) {
+ genctx = set_keygen_ctx(bio_err, keyalg, &pkey_type, &newkey,
+ &keyalgstr, gen_eng);
+ if (!genctx)
+ goto end;
+ }
+
+ if (newkey < MIN_KEY_LENGTH
+ && (pkey_type == EVP_PKEY_RSA || pkey_type == EVP_PKEY_DSA)) {
+ BIO_printf(bio_err, "private key length is too short,\n");
+ BIO_printf(bio_err, "it needs to be at least %d bits, not %ld\n",
+ MIN_KEY_LENGTH, newkey);
+ goto end;
+ }
+
+ if (!genctx) {
+ genctx = set_keygen_ctx(bio_err, NULL, &pkey_type, &newkey,
+ &keyalgstr, gen_eng);
+ if (!genctx)
+ goto end;
+ }
+
+ if (pkeyopts) {
+ char *genopt;
+ for (i = 0; i < sk_OPENSSL_STRING_num(pkeyopts); i++) {
+ genopt = sk_OPENSSL_STRING_value(pkeyopts, i);
+ if (pkey_ctrl_string(genctx, genopt) <= 0) {
+ BIO_printf(bio_err, "parameter error \"%s\"\n", genopt);
+ ERR_print_errors(bio_err);
+ goto end;
+ }
+ }
+ }
+
+ BIO_printf(bio_err, "Generating a %ld bit %s private key\n",
+ newkey, keyalgstr);
+
+ EVP_PKEY_CTX_set_cb(genctx, genpkey_cb);
+ EVP_PKEY_CTX_set_app_data(genctx, bio_err);
+
+ if (EVP_PKEY_keygen(genctx, &pkey) <= 0) {
+ BIO_puts(bio_err, "Error Generating Key\n");
+ goto end;
+ }
+
+ EVP_PKEY_CTX_free(genctx);
+ genctx = NULL;
+
+ app_RAND_write_file(randfile, bio_err);
+
+ if (keyout == NULL) {
+ keyout = NCONF_get_string(req_conf, SECTION, KEYFILE);
+ if (keyout == NULL)
+ ERR_clear_error();
+ }
+
+ if (keyout == NULL) {
+ BIO_printf(bio_err, "writing new private key to stdout\n");
+ BIO_set_fp(out, stdout, BIO_NOCLOSE);
+#ifdef OPENSSL_SYS_VMS
+ {
+ BIO *tmpbio = BIO_new(BIO_f_linebuffer());
+ out = BIO_push(tmpbio, out);
+ }