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
10 /* We need to use some engine deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
17 #include <openssl/pem.h>
18 #include <openssl/err.h>
19 #include <openssl/evp.h>
20 #ifndef OPENSSL_NO_ENGINE
21 # include <openssl/engine.h>
24 static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e,
25 OPENSSL_CTX *libctx, const char *propq);
26 static int genpkey_cb(EVP_PKEY_CTX *ctx);
28 typedef enum OPTION_choice {
29 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
30 OPT_ENGINE, OPT_OUTFORM, OPT_OUT, OPT_PASS, OPT_PARAMFILE,
31 OPT_ALGORITHM, OPT_PKEYOPT, OPT_GENPARAM, OPT_TEXT, OPT_CIPHER,
36 const OPTIONS genpkey_options[] = {
37 OPT_SECTION("General"),
38 {"help", OPT_HELP, '-', "Display this summary"},
39 #ifndef OPENSSL_NO_ENGINE
40 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
42 {"paramfile", OPT_PARAMFILE, '<', "Parameters file"},
43 {"algorithm", OPT_ALGORITHM, 's', "The public key algorithm"},
44 {"pkeyopt", OPT_PKEYOPT, 's',
45 "Set the public key algorithm option as opt:value"},
48 OPT_SECTION("Output"),
49 {"out", OPT_OUT, '>', "Output file"},
50 {"outform", OPT_OUTFORM, 'F', "output format (DER or PEM)"},
51 {"pass", OPT_PASS, 's', "Output file pass phrase source"},
52 {"genparam", OPT_GENPARAM, '-', "Generate parameters, not key"},
53 {"text", OPT_TEXT, '-', "Print the in text"},
54 {"", OPT_CIPHER, '-', "Cipher to use to encrypt the key"},
58 /* This is deliberately last. */
60 "Order of options may be important! See the documentation.\n"},
64 int genpkey_main(int argc, char **argv)
67 BIO *in = NULL, *out = NULL;
69 EVP_PKEY *pkey = NULL;
70 EVP_PKEY_CTX *ctx = NULL;
71 char *outfile = NULL, *passarg = NULL, *pass = NULL, *prog;
72 const EVP_CIPHER *cipher = NULL;
74 int outformat = FORMAT_PEM, text = 0, ret = 1, rv, do_param = 0;
76 OPENSSL_CTX *libctx = app_get0_libctx();
77 const char *propq = app_get0_propq();
79 prog = opt_init(argc, argv, genpkey_options);
80 while ((o = opt_next()) != OPT_EOF) {
85 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
89 opt_help(genpkey_options);
92 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
102 e = setup_engine(opt_arg(), 0);
107 if (!init_keygen_file(&ctx, opt_arg(), e, libctx, propq))
111 if (!init_gen_str(&ctx, opt_arg(), e, do_param, libctx, propq))
116 BIO_printf(bio_err, "%s: No keytype specified.\n", prog);
119 if (pkey_ctrl_string(ctx, opt_arg()) <= 0) {
121 "%s: Error setting %s parameter:\n",
123 ERR_print_errors(bio_err);
136 if (!opt_cipher(opt_unknown(), &cipher)
139 if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE ||
140 EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE ||
141 EVP_CIPHER_mode(cipher) == EVP_CIPH_XTS_MODE ||
142 EVP_CIPHER_mode(cipher) == EVP_CIPH_OCB_MODE) {
143 BIO_printf(bio_err, "%s: cipher mode not supported\n", prog);
148 conf = app_load_config_modules(opt_arg());
153 if (!opt_provider(o))
158 argc = opt_num_rest();
162 private = do_param ? 0 : 1;
167 if (!app_passwd(passarg, NULL, &pass, NULL)) {
168 BIO_puts(bio_err, "Error getting password\n");
172 out = bio_open_owner(outfile, outformat, private);
176 EVP_PKEY_CTX_set_cb(ctx, genpkey_cb);
177 EVP_PKEY_CTX_set_app_data(ctx, bio_err);
180 if (EVP_PKEY_paramgen(ctx, &pkey) <= 0) {
181 BIO_puts(bio_err, "Error generating parameters\n");
182 ERR_print_errors(bio_err);
186 if (EVP_PKEY_keygen(ctx, &pkey) <= 0) {
187 BIO_puts(bio_err, "Error generating key\n");
188 ERR_print_errors(bio_err);
194 rv = PEM_write_bio_Parameters(out, pkey);
195 } else if (outformat == FORMAT_PEM) {
197 rv = PEM_write_bio_PrivateKey(out, pkey, cipher, NULL, 0, NULL, pass);
198 } else if (outformat == FORMAT_ASN1) {
200 rv = i2d_PrivateKey_bio(out, pkey);
202 BIO_printf(bio_err, "Bad format specified for key\n");
209 BIO_puts(bio_err, "Error writing key\n");
210 ERR_print_errors(bio_err);
216 rv = EVP_PKEY_print_params(out, pkey, 0, NULL);
218 rv = EVP_PKEY_print_private(out, pkey, 0, NULL);
221 BIO_puts(bio_err, "Error printing key\n");
222 ERR_print_errors(bio_err);
229 EVP_PKEY_CTX_free(ctx);
238 static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e,
239 OPENSSL_CTX *libctx, const char *propq)
242 EVP_PKEY *pkey = NULL;
243 EVP_PKEY_CTX *ctx = NULL;
245 BIO_puts(bio_err, "Parameters already set!\n");
249 pbio = BIO_new_file(file, "r");
251 BIO_printf(bio_err, "Can't open parameter file %s\n", file);
255 pkey = PEM_read_bio_Parameters(pbio, NULL);
259 BIO_printf(bio_err, "Error reading parameter file %s\n", file);
264 ctx = EVP_PKEY_CTX_new(pkey, e);
266 ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
269 if (EVP_PKEY_keygen_init(ctx) <= 0)
276 BIO_puts(bio_err, "Error initializing context\n");
277 ERR_print_errors(bio_err);
278 EVP_PKEY_CTX_free(ctx);
284 int init_gen_str(EVP_PKEY_CTX **pctx,
285 const char *algname, ENGINE *e, int do_param,
286 OPENSSL_CTX *libctx, const char *propq)
288 EVP_PKEY_CTX *ctx = NULL;
289 const EVP_PKEY_ASN1_METHOD *ameth;
290 ENGINE *tmpeng = NULL;
294 BIO_puts(bio_err, "Algorithm already set!\n");
298 if (libctx == NULL || e != NULL) {
299 ameth = EVP_PKEY_asn1_find_str(&tmpeng, algname, -1);
301 #if !defined(OPENSSL_NO_ENGINE) && !defined(OPENSSL_NO_DEPRECATED_3_0)
302 if (ameth == NULL && e != NULL)
303 ameth = ENGINE_get_pkey_asn1_meth_str(e, algname, -1);
306 BIO_printf(bio_err, "Algorithm %s not found\n", algname);
311 EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL, ameth);
312 #if !defined(OPENSSL_NO_ENGINE) && !defined(OPENSSL_NO_DEPRECATED_3_0)
313 ENGINE_finish(tmpeng);
315 ctx = EVP_PKEY_CTX_new_id(pkey_id, e);
317 ctx = EVP_PKEY_CTX_new_from_name(libctx, algname, propq);
323 if (EVP_PKEY_paramgen_init(ctx) <= 0)
326 if (EVP_PKEY_keygen_init(ctx) <= 0)
334 BIO_printf(bio_err, "Error initializing %s context\n", algname);
335 ERR_print_errors(bio_err);
336 EVP_PKEY_CTX_free(ctx);
341 static int genpkey_cb(EVP_PKEY_CTX *ctx)
344 BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
346 p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);