]>
git.ipfire.org Git - thirdparty/openssl.git/blob - apps/genpkey.c
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>
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
{
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
, *p
;
66 const char *ciphername
= NULL
, *paramfile
= NULL
, *algname
= NULL
;
67 EVP_CIPHER
*cipher
= NULL
;
69 int outformat
= FORMAT_PEM
, text
= 0, ret
= 1, rv
, do_param
= 0;
70 int private = 0, i
, m
;
71 OSSL_LIB_CTX
*libctx
= app_get0_libctx();
72 STACK_OF(OPENSSL_STRING
) *keyopt
= NULL
;
74 prog
= opt_init(argc
, argv
, genpkey_options
);
75 keyopt
= sk_OPENSSL_STRING_new_null();
78 while ((o
= opt_next()) != OPT_EOF
) {
83 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
87 opt_help(genpkey_options
);
90 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &outformat
))
100 e
= setup_engine(opt_arg(), 0);
105 paramfile
= opt_arg();
111 if (!sk_OPENSSL_STRING_push(keyopt
, opt_arg()))
121 ciphername
= opt_unknown();
124 conf
= app_load_config_modules(opt_arg());
129 if (!opt_provider(o
))
135 /* No extra arguments. */
136 argc
= opt_num_rest();
140 /* Fetch cipher, etc. */
141 if (paramfile
!= NULL
) {
142 if (!init_keygen_file(&ctx
, paramfile
, e
, libctx
, app_get0_propq()))
145 if (algname
!= NULL
) {
146 if (!init_gen_str(&ctx
, algname
, e
, do_param
, libctx
, app_get0_propq()))
152 for (i
= 0; i
< sk_OPENSSL_STRING_num(keyopt
); i
++) {
153 p
= sk_OPENSSL_STRING_value(keyopt
, i
);
154 if (pkey_ctrl_string(ctx
, p
) <= 0) {
155 BIO_printf(bio_err
, "%s: Error setting %s parameter:\n", prog
, p
);
156 ERR_print_errors(bio_err
);
160 if (ciphername
!= NULL
) {
161 if (!opt_cipher(ciphername
, &cipher
) || do_param
== 1)
163 m
= EVP_CIPHER_mode(cipher
);
164 if (m
== EVP_CIPH_GCM_MODE
|| m
== EVP_CIPH_CCM_MODE
165 || m
== EVP_CIPH_XTS_MODE
|| m
== EVP_CIPH_OCB_MODE
) {
166 BIO_printf(bio_err
, "%s: cipher mode not supported\n", prog
);
171 private = do_param
? 0 : 1;
173 if (!app_passwd(passarg
, NULL
, &pass
, NULL
)) {
174 BIO_puts(bio_err
, "Error getting password\n");
178 out
= bio_open_owner(outfile
, outformat
, private);
182 EVP_PKEY_CTX_set_cb(ctx
, genpkey_cb
);
183 EVP_PKEY_CTX_set_app_data(ctx
, bio_err
);
186 if (EVP_PKEY_paramgen(ctx
, &pkey
) <= 0) {
187 BIO_puts(bio_err
, "Error generating parameters\n");
188 ERR_print_errors(bio_err
);
192 if (EVP_PKEY_keygen(ctx
, &pkey
) <= 0) {
193 BIO_puts(bio_err
, "Error generating key\n");
194 ERR_print_errors(bio_err
);
200 rv
= PEM_write_bio_Parameters(out
, pkey
);
201 } else if (outformat
== FORMAT_PEM
) {
203 rv
= PEM_write_bio_PrivateKey(out
, pkey
, cipher
, NULL
, 0, NULL
, pass
);
204 } else if (outformat
== FORMAT_ASN1
) {
206 rv
= i2d_PrivateKey_bio(out
, pkey
);
208 BIO_printf(bio_err
, "Bad format specified for key\n");
215 BIO_puts(bio_err
, "Error writing key\n");
216 ERR_print_errors(bio_err
);
222 rv
= EVP_PKEY_print_params(out
, pkey
, 0, NULL
);
224 rv
= EVP_PKEY_print_private(out
, pkey
, 0, NULL
);
227 BIO_puts(bio_err
, "Error printing key\n");
228 ERR_print_errors(bio_err
);
234 sk_OPENSSL_STRING_free(keyopt
);
236 EVP_PKEY_CTX_free(ctx
);
237 EVP_CIPHER_free(cipher
);
246 static int init_keygen_file(EVP_PKEY_CTX
**pctx
, const char *file
, ENGINE
*e
,
247 OSSL_LIB_CTX
*libctx
, const char *propq
)
250 EVP_PKEY
*pkey
= NULL
;
251 EVP_PKEY_CTX
*ctx
= NULL
;
253 BIO_puts(bio_err
, "Parameters already set!\n");
257 pbio
= BIO_new_file(file
, "r");
259 BIO_printf(bio_err
, "Can't open parameter file %s\n", file
);
263 pkey
= PEM_read_bio_Parameters_ex(pbio
, NULL
, libctx
, propq
);
267 BIO_printf(bio_err
, "Error reading parameter file %s\n", file
);
272 ctx
= EVP_PKEY_CTX_new(pkey
, e
);
274 ctx
= EVP_PKEY_CTX_new_from_pkey(libctx
, pkey
, propq
);
277 if (EVP_PKEY_keygen_init(ctx
) <= 0)
284 BIO_puts(bio_err
, "Error initializing context\n");
285 ERR_print_errors(bio_err
);
286 EVP_PKEY_CTX_free(ctx
);
292 int init_gen_str(EVP_PKEY_CTX
**pctx
,
293 const char *algname
, ENGINE
*e
, int do_param
,
294 OSSL_LIB_CTX
*libctx
, const char *propq
)
296 EVP_PKEY_CTX
*ctx
= NULL
;
300 BIO_puts(bio_err
, "Algorithm already set!\n");
304 pkey_id
= get_legacy_pkey_id(libctx
, algname
, e
);
305 if (pkey_id
!= NID_undef
)
306 ctx
= EVP_PKEY_CTX_new_id(pkey_id
, e
);
308 ctx
= EVP_PKEY_CTX_new_from_name(libctx
, algname
, propq
);
313 if (EVP_PKEY_paramgen_init(ctx
) <= 0)
316 if (EVP_PKEY_keygen_init(ctx
) <= 0)
324 BIO_printf(bio_err
, "Error initializing %s context\n", algname
);
325 ERR_print_errors(bio_err
);
326 EVP_PKEY_CTX_free(ctx
);
331 static int genpkey_cb(EVP_PKEY_CTX
*ctx
)
334 BIO
*b
= EVP_PKEY_CTX_get_app_data(ctx
);
336 p
= EVP_PKEY_CTX_get_keygen_info(ctx
, 0);