2 * Copyright 2006-2020 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>
18 static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e,
19 OSSL_LIB_CTX *libctx, const char *propq);
20 static int genpkey_cb(EVP_PKEY_CTX *ctx);
22 typedef enum OPTION_choice {
23 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
24 OPT_ENGINE, OPT_OUTFORM, OPT_OUT, OPT_PASS, OPT_PARAMFILE,
25 OPT_ALGORITHM, OPT_PKEYOPT, OPT_GENPARAM, OPT_TEXT, OPT_CIPHER,
30 const OPTIONS genpkey_options[] = {
31 OPT_SECTION("General"),
32 {"help", OPT_HELP, '-', "Display this summary"},
33 #ifndef OPENSSL_NO_ENGINE
34 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
36 {"paramfile", OPT_PARAMFILE, '<', "Parameters file"},
37 {"algorithm", OPT_ALGORITHM, 's', "The public key algorithm"},
38 {"pkeyopt", OPT_PKEYOPT, 's',
39 "Set the public key algorithm option as opt:value"},
42 OPT_SECTION("Output"),
43 {"out", OPT_OUT, '>', "Output file"},
44 {"outform", OPT_OUTFORM, 'F', "output format (DER or PEM)"},
45 {"pass", OPT_PASS, 's', "Output file pass phrase source"},
46 {"genparam", OPT_GENPARAM, '-', "Generate parameters, not key"},
47 {"text", OPT_TEXT, '-', "Print the in text"},
48 {"", OPT_CIPHER, '-', "Cipher to use to encrypt the key"},
52 /* This is deliberately last. */
54 "Order of options may be important! See the documentation.\n"},
58 int genpkey_main(int argc, char **argv)
61 BIO *in = NULL, *out = NULL;
63 EVP_PKEY *pkey = NULL;
64 EVP_PKEY_CTX *ctx = NULL;
65 char *outfile = NULL, *passarg = NULL, *pass = NULL, *prog;
66 const EVP_CIPHER *cipher = NULL;
68 int outformat = FORMAT_PEM, text = 0, ret = 1, rv, do_param = 0;
70 OSSL_LIB_CTX *libctx = app_get0_libctx();
71 const char *propq = app_get0_propq();
73 prog = opt_init(argc, argv, genpkey_options);
74 while ((o = opt_next()) != OPT_EOF) {
79 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
83 opt_help(genpkey_options);
86 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
96 e = setup_engine(opt_arg(), 0);
101 if (!init_keygen_file(&ctx, opt_arg(), e, libctx, propq))
105 if (!init_gen_str(&ctx, opt_arg(), e, do_param, libctx, propq))
110 BIO_printf(bio_err, "%s: No keytype specified.\n", prog);
113 if (pkey_ctrl_string(ctx, opt_arg()) <= 0) {
115 "%s: Error setting %s parameter:\n",
117 ERR_print_errors(bio_err);
124 "%s: '-genparam' option must be set before"
125 " the '-algorithm' option.\n", prog);
134 if (!opt_cipher(opt_unknown(), &cipher)
137 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE ||
138 EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE ||
139 EVP_CIPHER_mode(cipher) == EVP_CIPH_XTS_MODE ||
140 EVP_CIPHER_mode(cipher) == EVP_CIPH_OCB_MODE) {
141 BIO_printf(bio_err, "%s: cipher mode not supported\n", prog);
146 conf = app_load_config_modules(opt_arg());
151 if (!opt_provider(o))
156 argc = opt_num_rest();
160 private = do_param ? 0 : 1;
165 if (!app_passwd(passarg, NULL, &pass, NULL)) {
166 BIO_puts(bio_err, "Error getting password\n");
170 out = bio_open_owner(outfile, outformat, private);
174 EVP_PKEY_CTX_set_cb(ctx, genpkey_cb);
175 EVP_PKEY_CTX_set_app_data(ctx, bio_err);
178 if (EVP_PKEY_paramgen(ctx, &pkey) <= 0) {
179 BIO_puts(bio_err, "Error generating parameters\n");
180 ERR_print_errors(bio_err);
184 if (EVP_PKEY_keygen(ctx, &pkey) <= 0) {
185 BIO_puts(bio_err, "Error generating key\n");
186 ERR_print_errors(bio_err);
192 rv = PEM_write_bio_Parameters(out, pkey);
193 } else if (outformat == FORMAT_PEM) {
195 rv = PEM_write_bio_PrivateKey(out, pkey, cipher, NULL, 0, NULL, pass);
196 } else if (outformat == FORMAT_ASN1) {
198 rv = i2d_PrivateKey_bio(out, pkey);
200 BIO_printf(bio_err, "Bad format specified for key\n");
207 BIO_puts(bio_err, "Error writing key\n");
208 ERR_print_errors(bio_err);
214 rv = EVP_PKEY_print_params(out, pkey, 0, NULL);
216 rv = EVP_PKEY_print_private(out, pkey, 0, NULL);
219 BIO_puts(bio_err, "Error printing key\n");
220 ERR_print_errors(bio_err);
227 EVP_PKEY_CTX_free(ctx);
236 static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e,
237 OSSL_LIB_CTX *libctx, const char *propq)
240 EVP_PKEY *pkey = NULL;
241 EVP_PKEY_CTX *ctx = NULL;
243 BIO_puts(bio_err, "Parameters already set!\n");
247 pbio = BIO_new_file(file, "r");
249 BIO_printf(bio_err, "Can't open parameter file %s\n", file);
253 pkey = PEM_read_bio_Parameters(pbio, NULL);
257 BIO_printf(bio_err, "Error reading parameter file %s\n", file);
262 ctx = EVP_PKEY_CTX_new(pkey, e);
264 ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
267 if (EVP_PKEY_keygen_init(ctx) <= 0)
274 BIO_puts(bio_err, "Error initializing context\n");
275 ERR_print_errors(bio_err);
276 EVP_PKEY_CTX_free(ctx);
282 int init_gen_str(EVP_PKEY_CTX **pctx,
283 const char *algname, ENGINE *e, int do_param,
284 OSSL_LIB_CTX *libctx, const char *propq)
286 EVP_PKEY_CTX *ctx = NULL;
290 BIO_puts(bio_err, "Algorithm already set!\n");
294 pkey_id = get_legacy_pkey_id(libctx, algname, e);
295 if (pkey_id != NID_undef)
296 ctx = EVP_PKEY_CTX_new_id(pkey_id, e);
298 ctx = EVP_PKEY_CTX_new_from_name(libctx, algname, propq);
303 if (EVP_PKEY_paramgen_init(ctx) <= 0)
306 if (EVP_PKEY_keygen_init(ctx) <= 0)
314 BIO_printf(bio_err, "Error initializing %s context\n", algname);
315 ERR_print_errors(bio_err);
316 EVP_PKEY_CTX_free(ctx);
321 static int genpkey_cb(EVP_PKEY_CTX *ctx)
324 BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
326 p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);