2 * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
14 #include <openssl/pem.h>
15 #include <openssl/err.h>
16 #include <openssl/evp.h>
20 static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e,
21 OSSL_LIB_CTX *libctx, const char *propq);
22 static int genpkey_cb(EVP_PKEY_CTX *ctx);
24 typedef enum OPTION_choice {
26 OPT_ENGINE, OPT_OUTFORM, OPT_OUT, OPT_PASS, OPT_PARAMFILE,
27 OPT_ALGORITHM, OPT_PKEYOPT, OPT_GENPARAM, OPT_TEXT, OPT_CIPHER,
28 OPT_QUIET, OPT_CONFIG,
32 const OPTIONS genpkey_options[] = {
33 OPT_SECTION("General"),
34 {"help", OPT_HELP, '-', "Display this summary"},
35 #ifndef OPENSSL_NO_ENGINE
36 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
38 {"paramfile", OPT_PARAMFILE, '<', "Parameters file"},
39 {"algorithm", OPT_ALGORITHM, 's', "The public key algorithm"},
40 {"quiet", OPT_QUIET, '-', "Do not output status while generating keys"},
41 {"pkeyopt", OPT_PKEYOPT, 's',
42 "Set the public key algorithm option as opt:value"},
45 OPT_SECTION("Output"),
46 {"out", OPT_OUT, '>', "Output file"},
47 {"outform", OPT_OUTFORM, 'F', "output format (DER or PEM)"},
48 {"pass", OPT_PASS, 's', "Output file pass phrase source"},
49 {"genparam", OPT_GENPARAM, '-', "Generate parameters, not key"},
50 {"text", OPT_TEXT, '-', "Print the in text"},
51 {"", OPT_CIPHER, '-', "Cipher to use to encrypt the key"},
55 /* This is deliberately last. */
57 "Order of options may be important! See the documentation.\n"},
61 int genpkey_main(int argc, char **argv)
64 BIO *in = NULL, *out = NULL;
66 EVP_PKEY *pkey = NULL;
67 EVP_PKEY_CTX *ctx = NULL;
68 char *outfile = NULL, *passarg = NULL, *pass = NULL, *prog, *p;
69 const char *ciphername = NULL, *paramfile = NULL, *algname = NULL;
70 EVP_CIPHER *cipher = NULL;
72 int outformat = FORMAT_PEM, text = 0, ret = 1, rv, do_param = 0;
74 OSSL_LIB_CTX *libctx = app_get0_libctx();
75 STACK_OF(OPENSSL_STRING) *keyopt = NULL;
77 opt_set_unknown_name("cipher");
78 prog = opt_init(argc, argv, genpkey_options);
79 keyopt = sk_OPENSSL_STRING_new_null();
82 while ((o = opt_next()) != OPT_EOF) {
87 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
91 opt_help(genpkey_options);
94 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
104 e = setup_engine(opt_arg(), 0);
109 paramfile = opt_arg();
115 if (!sk_OPENSSL_STRING_push(keyopt, opt_arg()))
128 ciphername = opt_unknown();
131 conf = app_load_config_modules(opt_arg());
136 if (!opt_provider(o))
142 /* No extra arguments. */
143 if (!opt_check_rest_arg(NULL))
146 /* Fetch cipher, etc. */
147 if (paramfile != NULL) {
148 if (!init_keygen_file(&ctx, paramfile, e, libctx, app_get0_propq()))
151 if (algname != NULL) {
152 if (!init_gen_str(&ctx, algname, e, do_param, libctx, app_get0_propq()))
158 for (i = 0; i < sk_OPENSSL_STRING_num(keyopt); i++) {
159 p = sk_OPENSSL_STRING_value(keyopt, i);
160 if (pkey_ctrl_string(ctx, p) <= 0) {
161 BIO_printf(bio_err, "%s: Error setting %s parameter:\n", prog, p);
162 ERR_print_errors(bio_err);
166 if (!opt_cipher(ciphername, &cipher))
168 if (ciphername != NULL && do_param == 1) {
169 BIO_printf(bio_err, "Cannot use cipher with -genparam option\n");
173 private = do_param ? 0 : 1;
175 if (!app_passwd(passarg, NULL, &pass, NULL)) {
176 BIO_puts(bio_err, "Error getting password\n");
180 out = bio_open_owner(outfile, outformat, private);
184 EVP_PKEY_CTX_set_cb(ctx, genpkey_cb);
185 EVP_PKEY_CTX_set_app_data(ctx, bio_err);
187 pkey = do_param ? app_paramgen(ctx, algname)
188 : app_keygen(ctx, algname, 0, 0 /* not verbose */);
191 rv = PEM_write_bio_Parameters(out, pkey);
192 } else if (outformat == FORMAT_PEM) {
194 rv = PEM_write_bio_PrivateKey(out, pkey, cipher, NULL, 0, NULL, pass);
195 } else if (outformat == FORMAT_ASN1) {
197 rv = i2d_PrivateKey_bio(out, pkey);
199 BIO_printf(bio_err, "Bad format specified for key\n");
206 BIO_puts(bio_err, "Error writing key\n");
212 rv = EVP_PKEY_print_params(out, pkey, 0, NULL);
214 rv = EVP_PKEY_print_private(out, pkey, 0, NULL);
217 BIO_puts(bio_err, "Error printing key\n");
223 sk_OPENSSL_STRING_free(keyopt);
225 ERR_print_errors(bio_err);
227 EVP_PKEY_CTX_free(ctx);
228 EVP_CIPHER_free(cipher);
237 static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e,
238 OSSL_LIB_CTX *libctx, const char *propq)
241 EVP_PKEY *pkey = NULL;
242 EVP_PKEY_CTX *ctx = NULL;
244 BIO_puts(bio_err, "Parameters already set!\n");
248 pbio = BIO_new_file(file, "r");
250 BIO_printf(bio_err, "Can't open parameter file %s\n", file);
254 pkey = PEM_read_bio_Parameters_ex(pbio, NULL, libctx, propq);
258 BIO_printf(bio_err, "Error reading parameter file %s\n", file);
263 ctx = EVP_PKEY_CTX_new(pkey, e);
265 ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
268 if (EVP_PKEY_keygen_init(ctx) <= 0)
275 BIO_puts(bio_err, "Error initializing context\n");
276 ERR_print_errors(bio_err);
277 EVP_PKEY_CTX_free(ctx);
283 int init_gen_str(EVP_PKEY_CTX **pctx,
284 const char *algname, ENGINE *e, int do_param,
285 OSSL_LIB_CTX *libctx, const char *propq)
287 EVP_PKEY_CTX *ctx = NULL;
291 BIO_puts(bio_err, "Algorithm already set!\n");
295 pkey_id = get_legacy_pkey_id(libctx, algname, e);
296 if (pkey_id != NID_undef)
297 ctx = EVP_PKEY_CTX_new_id(pkey_id, e);
299 ctx = EVP_PKEY_CTX_new_from_name(libctx, algname, propq);
304 if (EVP_PKEY_paramgen_init(ctx) <= 0)
307 if (EVP_PKEY_keygen_init(ctx) <= 0)
315 BIO_printf(bio_err, "Error initializing %s context\n", algname);
316 ERR_print_errors(bio_err);
317 EVP_PKEY_CTX_free(ctx);
322 static int genpkey_cb(EVP_PKEY_CTX *ctx)
325 BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
330 switch (EVP_PKEY_CTX_get_keygen_info(ctx, 0)) {