]>
git.ipfire.org Git - thirdparty/openssl.git/blob - apps/genpkey.c
2 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
13 #include <openssl/pem.h>
14 #include <openssl/err.h>
15 #include <openssl/evp.h>
16 #ifndef OPENSSL_NO_ENGINE
17 # include <openssl/engine.h>
20 static int init_keygen_file(EVP_PKEY_CTX
**pctx
, const char *file
, ENGINE
*e
);
21 static int genpkey_cb(EVP_PKEY_CTX
*ctx
);
23 typedef enum OPTION_choice
{
24 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
25 OPT_ENGINE
, OPT_OUTFORM
, OPT_OUT
, OPT_PASS
, OPT_PARAMFILE
,
26 OPT_ALGORITHM
, OPT_PKEYOPT
, OPT_GENPARAM
, OPT_TEXT
, OPT_CIPHER
29 const OPTIONS genpkey_options
[] = {
30 {"help", OPT_HELP
, '-', "Display this summary"},
31 {"out", OPT_OUT
, '>', "Output file"},
32 {"outform", OPT_OUTFORM
, 'F', "output format (DER or PEM)"},
33 {"pass", OPT_PASS
, 's', "Output file pass phrase source"},
34 {"paramfile", OPT_PARAMFILE
, '<', "Parameters file"},
35 {"algorithm", OPT_ALGORITHM
, 's', "The public key algorithm"},
36 {"pkeyopt", OPT_PKEYOPT
, 's',
37 "Set the public key algorithm option as opt:value"},
38 {"genparam", OPT_GENPARAM
, '-', "Generate parameters, not key"},
39 {"text", OPT_TEXT
, '-', "Print the in text"},
40 {"", OPT_CIPHER
, '-', "Cipher to use to encrypt the key"},
41 #ifndef OPENSSL_NO_ENGINE
42 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
44 /* This is deliberately last. */
46 "Order of options may be important! See the documentation.\n"},
50 int genpkey_main(int argc
, char **argv
)
52 BIO
*in
= NULL
, *out
= NULL
;
54 EVP_PKEY
*pkey
= NULL
;
55 EVP_PKEY_CTX
*ctx
= NULL
;
56 char *outfile
= NULL
, *passarg
= NULL
, *pass
= NULL
, *prog
;
57 const EVP_CIPHER
*cipher
= NULL
;
59 int outformat
= FORMAT_PEM
, text
= 0, ret
= 1, rv
, do_param
= 0;
62 prog
= opt_init(argc
, argv
, genpkey_options
);
63 while ((o
= opt_next()) != OPT_EOF
) {
68 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
72 opt_help(genpkey_options
);
75 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &outformat
))
85 e
= setup_engine(opt_arg(), 0);
90 if (!init_keygen_file(&ctx
, opt_arg(), e
))
94 if (!init_gen_str(&ctx
, opt_arg(), e
, do_param
))
99 BIO_printf(bio_err
, "%s: No keytype specified.\n", prog
);
102 if (pkey_ctrl_string(ctx
, opt_arg()) <= 0) {
104 "%s: Error setting %s parameter:\n",
106 ERR_print_errors(bio_err
);
119 if (!opt_cipher(opt_unknown(), &cipher
)
124 argc
= opt_num_rest();
128 private = do_param
? 0 : 1;
133 if (!app_passwd(passarg
, NULL
, &pass
, NULL
)) {
134 BIO_puts(bio_err
, "Error getting password\n");
138 out
= bio_open_owner(outfile
, outformat
, private);
142 EVP_PKEY_CTX_set_cb(ctx
, genpkey_cb
);
143 EVP_PKEY_CTX_set_app_data(ctx
, bio_err
);
146 if (EVP_PKEY_paramgen(ctx
, &pkey
) <= 0) {
147 BIO_puts(bio_err
, "Error generating parameters\n");
148 ERR_print_errors(bio_err
);
152 if (EVP_PKEY_keygen(ctx
, &pkey
) <= 0) {
153 BIO_puts(bio_err
, "Error generating key\n");
154 ERR_print_errors(bio_err
);
160 rv
= PEM_write_bio_Parameters(out
, pkey
);
161 else if (outformat
== FORMAT_PEM
) {
163 rv
= PEM_write_bio_PrivateKey(out
, pkey
, cipher
, NULL
, 0, NULL
, pass
);
164 } else if (outformat
== FORMAT_ASN1
) {
166 rv
= i2d_PrivateKey_bio(out
, pkey
);
168 BIO_printf(bio_err
, "Bad format specified for key\n");
173 BIO_puts(bio_err
, "Error writing key\n");
174 ERR_print_errors(bio_err
);
179 rv
= EVP_PKEY_print_params(out
, pkey
, 0, NULL
);
181 rv
= EVP_PKEY_print_private(out
, pkey
, 0, NULL
);
184 BIO_puts(bio_err
, "Error printing key\n");
185 ERR_print_errors(bio_err
);
193 EVP_PKEY_CTX_free(ctx
);
201 static int init_keygen_file(EVP_PKEY_CTX
**pctx
, const char *file
, ENGINE
*e
)
204 EVP_PKEY
*pkey
= NULL
;
205 EVP_PKEY_CTX
*ctx
= NULL
;
207 BIO_puts(bio_err
, "Parameters already set!\n");
211 pbio
= BIO_new_file(file
, "r");
213 BIO_printf(bio_err
, "Can't open parameter file %s\n", file
);
217 pkey
= PEM_read_bio_Parameters(pbio
, NULL
);
221 BIO_printf(bio_err
, "Error reading parameter file %s\n", file
);
225 ctx
= EVP_PKEY_CTX_new(pkey
, e
);
228 if (EVP_PKEY_keygen_init(ctx
) <= 0)
235 BIO_puts(bio_err
, "Error initializing context\n");
236 ERR_print_errors(bio_err
);
237 EVP_PKEY_CTX_free(ctx
);
243 int init_gen_str(EVP_PKEY_CTX
**pctx
,
244 const char *algname
, ENGINE
*e
, int do_param
)
246 EVP_PKEY_CTX
*ctx
= NULL
;
247 const EVP_PKEY_ASN1_METHOD
*ameth
;
248 ENGINE
*tmpeng
= NULL
;
252 BIO_puts(bio_err
, "Algorithm already set!\n");
256 ameth
= EVP_PKEY_asn1_find_str(&tmpeng
, algname
, -1);
258 #ifndef OPENSSL_NO_ENGINE
260 ameth
= ENGINE_get_pkey_asn1_meth_str(e
, algname
, -1);
264 BIO_printf(bio_err
, "Algorithm %s not found\n", algname
);
270 EVP_PKEY_asn1_get0_info(&pkey_id
, NULL
, NULL
, NULL
, NULL
, ameth
);
271 #ifndef OPENSSL_NO_ENGINE
272 ENGINE_finish(tmpeng
);
274 ctx
= EVP_PKEY_CTX_new_id(pkey_id
, e
);
279 if (EVP_PKEY_paramgen_init(ctx
) <= 0)
282 if (EVP_PKEY_keygen_init(ctx
) <= 0)
290 BIO_printf(bio_err
, "Error initializing %s context\n", algname
);
291 ERR_print_errors(bio_err
);
292 EVP_PKEY_CTX_free(ctx
);
297 static int genpkey_cb(EVP_PKEY_CTX
*ctx
)
300 BIO
*b
= EVP_PKEY_CTX_get_app_data(ctx
);
302 p
= EVP_PKEY_CTX_get_keygen_info(ctx
, 0);