2 * Copyright 2006-2024 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 verbose
= 1;
20 static int init_keygen_file(EVP_PKEY_CTX
**pctx
, const char *file
, ENGINE
*e
,
21 OSSL_LIB_CTX
*libctx
, const char *propq
);
22 typedef enum OPTION_choice
{
24 OPT_ENGINE
, OPT_OUTFORM
, OPT_OUT
, OPT_PASS
, OPT_PARAMFILE
,
25 OPT_ALGORITHM
, OPT_PKEYOPT
, OPT_GENPARAM
, OPT_TEXT
, OPT_CIPHER
,
26 OPT_VERBOSE
, OPT_QUIET
, OPT_CONFIG
, OPT_OUTPUBKEY
,
27 OPT_PROV_ENUM
, OPT_R_ENUM
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 {"verbose", OPT_VERBOSE
, '-', "Output status while generating keys"},
39 {"quiet", OPT_QUIET
, '-', "Do not output status while generating keys"},
40 {"pkeyopt", OPT_PKEYOPT
, 's',
41 "Set the public key algorithm option as opt:value"},
44 OPT_SECTION("Output"),
45 {"out", OPT_OUT
, '>', "Output (private key) file"},
46 {"outpubkey", OPT_OUTPUBKEY
, '>', "Output public key 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 private key in text"},
51 {"", OPT_CIPHER
, '-', "Cipher to use to encrypt the key"},
56 /* This is deliberately last. */
58 "Order of options may be important! See the documentation.\n"},
62 static const char *param_datatype_2name(unsigned int type
, int *ishex
)
67 case OSSL_PARAM_INTEGER
: return "int";
68 case OSSL_PARAM_UNSIGNED_INTEGER
: return "uint";
69 case OSSL_PARAM_REAL
: return "float";
70 case OSSL_PARAM_OCTET_STRING
: *ishex
= 1; return "string";
71 case OSSL_PARAM_UTF8_STRING
: return "string";
77 static void show_gen_pkeyopt(const char *algname
, OSSL_LIB_CTX
*libctx
, const char *propq
)
79 EVP_PKEY_CTX
*ctx
= NULL
;
80 const OSSL_PARAM
*params
;
85 ctx
= EVP_PKEY_CTX_new_from_name(libctx
, algname
, propq
);
89 if (EVP_PKEY_keygen_init(ctx
) <= 0)
91 params
= EVP_PKEY_CTX_settable_params(ctx
);
95 BIO_printf(bio_err
, "\nThe possible -pkeyopt arguments are:\n");
96 for (i
= 0; params
[i
].key
!= NULL
; ++i
) {
97 const char *name
= param_datatype_2name(params
[i
].data_type
, &ishex
);
100 BIO_printf(bio_err
, " %s%s:%s\n", ishex
? "hex" : "", params
[i
].key
, name
);
103 EVP_PKEY_CTX_free(ctx
);
106 int genpkey_main(int argc
, char **argv
)
109 BIO
*in
= NULL
, *out
= NULL
, *outpubkey
= NULL
;
111 EVP_PKEY
*pkey
= NULL
;
112 EVP_PKEY_CTX
*ctx
= NULL
;
113 char *outfile
= NULL
, *passarg
= NULL
, *pass
= NULL
, *prog
, *p
;
114 char *outpubkeyfile
= NULL
;
115 const char *ciphername
= NULL
, *paramfile
= NULL
, *algname
= NULL
;
116 EVP_CIPHER
*cipher
= NULL
;
118 int outformat
= FORMAT_PEM
, text
= 0, ret
= 1, rv
, do_param
= 0;
120 OSSL_LIB_CTX
*libctx
= app_get0_libctx();
121 STACK_OF(OPENSSL_STRING
) *keyopt
= NULL
;
123 opt_set_unknown_name("cipher");
124 prog
= opt_init(argc
, argv
, genpkey_options
);
125 keyopt
= sk_OPENSSL_STRING_new_null();
128 while ((o
= opt_next()) != OPT_EOF
) {
133 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
137 opt_help(genpkey_options
);
138 show_gen_pkeyopt(algname
, libctx
, app_get0_propq());
141 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &outformat
))
148 outpubkeyfile
= opt_arg();
154 e
= setup_engine(opt_arg(), 0);
159 paramfile
= opt_arg();
165 if (!sk_OPENSSL_STRING_push(keyopt
, opt_arg()))
181 ciphername
= opt_unknown();
184 conf
= app_load_config_modules(opt_arg());
189 if (!opt_provider(o
))
199 /* No extra arguments. */
200 if (!opt_check_rest_arg(NULL
))
203 if (!app_RAND_load())
206 /* Fetch cipher, etc. */
207 if (paramfile
!= NULL
) {
208 if (!init_keygen_file(&ctx
, paramfile
, e
, libctx
, app_get0_propq()))
211 if (algname
!= NULL
) {
212 if (!init_gen_str(&ctx
, algname
, e
, do_param
, libctx
, app_get0_propq()))
218 for (i
= 0; i
< sk_OPENSSL_STRING_num(keyopt
); i
++) {
219 p
= sk_OPENSSL_STRING_value(keyopt
, i
);
220 if (pkey_ctrl_string(ctx
, p
) <= 0) {
221 BIO_printf(bio_err
, "%s: Error setting %s parameter:\n", prog
, p
);
222 ERR_print_errors(bio_err
);
226 if (!opt_cipher(ciphername
, &cipher
))
228 if (ciphername
!= NULL
&& do_param
== 1) {
229 BIO_printf(bio_err
, "Cannot use cipher with -genparam option\n");
233 private = do_param
? 0 : 1;
235 if (!app_passwd(passarg
, NULL
, &pass
, NULL
)) {
236 BIO_puts(bio_err
, "Error getting password\n");
240 out
= bio_open_owner(outfile
, outformat
, private);
244 if (outpubkeyfile
!= NULL
) {
245 outpubkey
= bio_open_owner(outpubkeyfile
, outformat
, private);
246 if (outpubkey
== NULL
)
251 EVP_PKEY_CTX_set_cb(ctx
, progress_cb
);
252 EVP_PKEY_CTX_set_app_data(ctx
, bio_err
);
254 pkey
= do_param
? app_paramgen(ctx
, algname
)
255 : app_keygen(ctx
, algname
, 0, 0 /* not verbose */);
260 rv
= PEM_write_bio_Parameters(out
, pkey
);
261 } else if (outformat
== FORMAT_PEM
) {
263 rv
= PEM_write_bio_PrivateKey(out
, pkey
, cipher
, NULL
, 0, NULL
, pass
);
264 if (rv
> 0 && outpubkey
!= NULL
)
265 rv
= PEM_write_bio_PUBKEY(outpubkey
, pkey
);
266 } else if (outformat
== FORMAT_ASN1
) {
268 rv
= i2d_PrivateKey_bio(out
, pkey
);
269 if (rv
> 0 && outpubkey
!= NULL
)
270 rv
= i2d_PUBKEY_bio(outpubkey
, pkey
);
272 BIO_printf(bio_err
, "Bad format specified for key\n");
279 BIO_puts(bio_err
, "Error writing key(s)\n");
285 rv
= EVP_PKEY_print_params(out
, pkey
, 0, NULL
);
287 rv
= EVP_PKEY_print_private(out
, pkey
, 0, NULL
);
290 BIO_puts(bio_err
, "Error printing key\n");
296 sk_OPENSSL_STRING_free(keyopt
);
298 ERR_print_errors(bio_err
);
300 EVP_PKEY_CTX_free(ctx
);
301 EVP_CIPHER_free(cipher
);
303 BIO_free_all(outpubkey
);
311 static int init_keygen_file(EVP_PKEY_CTX
**pctx
, const char *file
, ENGINE
*e
,
312 OSSL_LIB_CTX
*libctx
, const char *propq
)
315 EVP_PKEY
*pkey
= NULL
;
316 EVP_PKEY_CTX
*ctx
= NULL
;
318 BIO_puts(bio_err
, "Parameters already set!\n");
322 pbio
= BIO_new_file(file
, "r");
324 BIO_printf(bio_err
, "Can't open parameter file %s\n", file
);
328 pkey
= PEM_read_bio_Parameters_ex(pbio
, NULL
, libctx
, propq
);
332 BIO_printf(bio_err
, "Error reading parameter file %s\n", file
);
337 ctx
= EVP_PKEY_CTX_new(pkey
, e
);
339 ctx
= EVP_PKEY_CTX_new_from_pkey(libctx
, pkey
, propq
);
342 if (EVP_PKEY_keygen_init(ctx
) <= 0)
349 BIO_puts(bio_err
, "Error initializing context\n");
350 ERR_print_errors(bio_err
);
351 EVP_PKEY_CTX_free(ctx
);
357 int init_gen_str(EVP_PKEY_CTX
**pctx
,
358 const char *algname
, ENGINE
*e
, int do_param
,
359 OSSL_LIB_CTX
*libctx
, const char *propq
)
361 EVP_PKEY_CTX
*ctx
= NULL
;
365 BIO_puts(bio_err
, "Algorithm already set!\n");
369 pkey_id
= get_legacy_pkey_id(libctx
, algname
, e
);
370 if (pkey_id
!= NID_undef
)
371 ctx
= EVP_PKEY_CTX_new_id(pkey_id
, e
);
373 ctx
= EVP_PKEY_CTX_new_from_name(libctx
, algname
, propq
);
378 if (EVP_PKEY_paramgen_init(ctx
) <= 0)
381 if (EVP_PKEY_keygen_init(ctx
) <= 0)
389 BIO_printf(bio_err
, "Error initializing %s context\n", algname
);
390 ERR_print_errors(bio_err
);
391 EVP_PKEY_CTX_free(ctx
);