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 prog = opt_init(argc, argv, genpkey_options);
78 keyopt = sk_OPENSSL_STRING_new_null();
81 while ((o = opt_next()) != OPT_EOF) {
86 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
90 opt_help(genpkey_options);
93 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
103 e = setup_engine(opt_arg(), 0);
108 paramfile = opt_arg();
114 if (!sk_OPENSSL_STRING_push(keyopt, opt_arg()))
127 ciphername = opt_unknown();
130 conf = app_load_config_modules(opt_arg());
135 if (!opt_provider(o))
141 /* No extra arguments. */
142 argc = opt_num_rest();
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 (ciphername != NULL)
167 if (!opt_cipher(ciphername, &cipher) || do_param == 1)
170 private = do_param ? 0 : 1;
172 if (!app_passwd(passarg, NULL, &pass, NULL)) {
173 BIO_puts(bio_err, "Error getting password\n");
177 out = bio_open_owner(outfile, outformat, private);
181 EVP_PKEY_CTX_set_cb(ctx, genpkey_cb);
182 EVP_PKEY_CTX_set_app_data(ctx, bio_err);
184 pkey = do_param ? app_paramgen(ctx, algname)
185 : app_keygen(ctx, algname, 0, 0 /* not verbose */);
188 rv = PEM_write_bio_Parameters(out, pkey);
189 } else if (outformat == FORMAT_PEM) {
191 rv = PEM_write_bio_PrivateKey(out, pkey, cipher, NULL, 0, NULL, pass);
192 } else if (outformat == FORMAT_ASN1) {
194 rv = i2d_PrivateKey_bio(out, pkey);
196 BIO_printf(bio_err, "Bad format specified for key\n");
203 BIO_puts(bio_err, "Error writing key\n");
209 rv = EVP_PKEY_print_params(out, pkey, 0, NULL);
211 rv = EVP_PKEY_print_private(out, pkey, 0, NULL);
214 BIO_puts(bio_err, "Error printing key\n");
220 sk_OPENSSL_STRING_free(keyopt);
222 ERR_print_errors(bio_err);
224 EVP_PKEY_CTX_free(ctx);
225 EVP_CIPHER_free(cipher);
234 static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e,
235 OSSL_LIB_CTX *libctx, const char *propq)
238 EVP_PKEY *pkey = NULL;
239 EVP_PKEY_CTX *ctx = NULL;
241 BIO_puts(bio_err, "Parameters already set!\n");
245 pbio = BIO_new_file(file, "r");
247 BIO_printf(bio_err, "Can't open parameter file %s\n", file);
251 pkey = PEM_read_bio_Parameters_ex(pbio, NULL, libctx, propq);
255 BIO_printf(bio_err, "Error reading parameter file %s\n", file);
260 ctx = EVP_PKEY_CTX_new(pkey, e);
262 ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
265 if (EVP_PKEY_keygen_init(ctx) <= 0)
272 BIO_puts(bio_err, "Error initializing context\n");
273 ERR_print_errors(bio_err);
274 EVP_PKEY_CTX_free(ctx);
280 int init_gen_str(EVP_PKEY_CTX **pctx,
281 const char *algname, ENGINE *e, int do_param,
282 OSSL_LIB_CTX *libctx, const char *propq)
284 EVP_PKEY_CTX *ctx = NULL;
288 BIO_puts(bio_err, "Algorithm already set!\n");
292 pkey_id = get_legacy_pkey_id(libctx, algname, e);
293 if (pkey_id != NID_undef)
294 ctx = EVP_PKEY_CTX_new_id(pkey_id, e);
296 ctx = EVP_PKEY_CTX_new_from_name(libctx, algname, propq);
301 if (EVP_PKEY_paramgen_init(ctx) <= 0)
304 if (EVP_PKEY_keygen_init(ctx) <= 0)
312 BIO_printf(bio_err, "Error initializing %s context\n", algname);
313 ERR_print_errors(bio_err);
314 EVP_PKEY_CTX_free(ctx);
319 static int genpkey_cb(EVP_PKEY_CTX *ctx)
322 BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
327 switch (EVP_PKEY_CTX_get_keygen_info(ctx, 0)) {