]>
git.ipfire.org Git - thirdparty/openssl.git/blob - apps/genpkey.c
2 * Copyright 2006-2018 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>
17 #ifndef OPENSSL_NO_ENGINE
18 # include <openssl/engine.h>
21 static int init_keygen_file(EVP_PKEY_CTX
**pctx
, const char *file
, ENGINE
*e
);
22 static int genpkey_cb(EVP_PKEY_CTX
*ctx
);
24 typedef enum OPTION_choice
{
25 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
26 OPT_ENGINE
, OPT_OUTFORM
, OPT_OUT
, OPT_PASS
, OPT_PARAMFILE
,
27 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"},
41 OPT_SECTION("Output"),
42 {"out", OPT_OUT
, '>', "Output file"},
43 {"outform", OPT_OUTFORM
, 'F', "output format (DER or PEM)"},
44 {"pass", OPT_PASS
, 's', "Output file pass phrase source"},
45 {"genparam", OPT_GENPARAM
, '-', "Generate parameters, not key"},
46 {"text", OPT_TEXT
, '-', "Print the in text"},
47 {"", OPT_CIPHER
, '-', "Cipher to use to encrypt the key"},
49 /* This is deliberately last. */
51 "Order of options may be important! See the documentation.\n"},
55 int genpkey_main(int argc
, char **argv
)
57 BIO
*in
= NULL
, *out
= NULL
;
59 EVP_PKEY
*pkey
= NULL
;
60 EVP_PKEY_CTX
*ctx
= NULL
;
61 char *outfile
= NULL
, *passarg
= NULL
, *pass
= NULL
, *prog
;
62 const EVP_CIPHER
*cipher
= NULL
;
64 int outformat
= FORMAT_PEM
, text
= 0, ret
= 1, rv
, do_param
= 0;
67 prog
= opt_init(argc
, argv
, genpkey_options
);
68 while ((o
= opt_next()) != OPT_EOF
) {
73 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
77 opt_help(genpkey_options
);
80 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &outformat
))
90 e
= setup_engine(opt_arg(), 0);
95 if (!init_keygen_file(&ctx
, opt_arg(), e
))
99 if (!init_gen_str(&ctx
, opt_arg(), e
, do_param
))
104 BIO_printf(bio_err
, "%s: No keytype specified.\n", prog
);
107 if (pkey_ctrl_string(ctx
, opt_arg()) <= 0) {
109 "%s: Error setting %s parameter:\n",
111 ERR_print_errors(bio_err
);
124 if (!opt_cipher(opt_unknown(), &cipher
)
127 if (EVP_CIPHER_mode(cipher
) == EVP_CIPH_GCM_MODE
||
128 EVP_CIPHER_mode(cipher
) == EVP_CIPH_CCM_MODE
||
129 EVP_CIPHER_mode(cipher
) == EVP_CIPH_XTS_MODE
||
130 EVP_CIPHER_mode(cipher
) == EVP_CIPH_OCB_MODE
) {
131 BIO_printf(bio_err
, "%s: cipher mode not supported\n", prog
);
136 argc
= opt_num_rest();
140 private = do_param
? 0 : 1;
145 if (!app_passwd(passarg
, NULL
, &pass
, NULL
)) {
146 BIO_puts(bio_err
, "Error getting password\n");
150 out
= bio_open_owner(outfile
, outformat
, private);
154 EVP_PKEY_CTX_set_cb(ctx
, genpkey_cb
);
155 EVP_PKEY_CTX_set_app_data(ctx
, bio_err
);
158 if (EVP_PKEY_paramgen(ctx
, &pkey
) <= 0) {
159 BIO_puts(bio_err
, "Error generating parameters\n");
160 ERR_print_errors(bio_err
);
164 if (EVP_PKEY_keygen(ctx
, &pkey
) <= 0) {
165 BIO_puts(bio_err
, "Error generating key\n");
166 ERR_print_errors(bio_err
);
172 rv
= PEM_write_bio_Parameters(out
, pkey
);
173 } else if (outformat
== FORMAT_PEM
) {
175 rv
= PEM_write_bio_PrivateKey(out
, pkey
, cipher
, NULL
, 0, NULL
, pass
);
176 } else if (outformat
== FORMAT_ASN1
) {
178 rv
= i2d_PrivateKey_bio(out
, pkey
);
180 BIO_printf(bio_err
, "Bad format specified for key\n");
185 BIO_puts(bio_err
, "Error writing key\n");
186 ERR_print_errors(bio_err
);
191 rv
= EVP_PKEY_print_params(out
, pkey
, 0, NULL
);
193 rv
= EVP_PKEY_print_private(out
, pkey
, 0, NULL
);
196 BIO_puts(bio_err
, "Error printing key\n");
197 ERR_print_errors(bio_err
);
205 EVP_PKEY_CTX_free(ctx
);
213 static int init_keygen_file(EVP_PKEY_CTX
**pctx
, const char *file
, ENGINE
*e
)
216 EVP_PKEY
*pkey
= NULL
;
217 EVP_PKEY_CTX
*ctx
= NULL
;
219 BIO_puts(bio_err
, "Parameters already set!\n");
223 pbio
= BIO_new_file(file
, "r");
225 BIO_printf(bio_err
, "Can't open parameter file %s\n", file
);
229 pkey
= PEM_read_bio_Parameters(pbio
, NULL
);
233 BIO_printf(bio_err
, "Error reading parameter file %s\n", file
);
237 ctx
= EVP_PKEY_CTX_new(pkey
, e
);
240 if (EVP_PKEY_keygen_init(ctx
) <= 0)
247 BIO_puts(bio_err
, "Error initializing context\n");
248 ERR_print_errors(bio_err
);
249 EVP_PKEY_CTX_free(ctx
);
255 int init_gen_str(EVP_PKEY_CTX
**pctx
,
256 const char *algname
, ENGINE
*e
, int do_param
)
258 EVP_PKEY_CTX
*ctx
= NULL
;
259 const EVP_PKEY_ASN1_METHOD
*ameth
;
260 ENGINE
*tmpeng
= NULL
;
264 BIO_puts(bio_err
, "Algorithm already set!\n");
268 ameth
= EVP_PKEY_asn1_find_str(&tmpeng
, algname
, -1);
270 #ifndef OPENSSL_NO_ENGINE
272 ameth
= ENGINE_get_pkey_asn1_meth_str(e
, algname
, -1);
276 BIO_printf(bio_err
, "Algorithm %s not found\n", algname
);
282 EVP_PKEY_asn1_get0_info(&pkey_id
, NULL
, NULL
, NULL
, NULL
, ameth
);
283 #ifndef OPENSSL_NO_ENGINE
284 ENGINE_finish(tmpeng
);
286 ctx
= EVP_PKEY_CTX_new_id(pkey_id
, e
);
291 if (EVP_PKEY_paramgen_init(ctx
) <= 0)
294 if (EVP_PKEY_keygen_init(ctx
) <= 0)
302 BIO_printf(bio_err
, "Error initializing %s context\n", algname
);
303 ERR_print_errors(bio_err
);
304 EVP_PKEY_CTX_free(ctx
);
309 static int genpkey_cb(EVP_PKEY_CTX
*ctx
)
312 BIO
*b
= EVP_PKEY_CTX_get_app_data(ctx
);
314 p
= EVP_PKEY_CTX_get_keygen_info(ctx
, 0);