68dbbf87eb4d73a73d67e62d5452ad1469fd933b
[openssl.git] / apps / genpkey.c
1 /*
2  * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include "apps.h"
13 #include "progs.h"
14 #include <openssl/pem.h>
15 #include <openssl/err.h>
16 #include <openssl/evp.h>
17
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);
21
22 typedef enum OPTION_choice {
23     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
24     OPT_ENGINE, OPT_OUTFORM, OPT_OUT, OPT_PASS, OPT_PARAMFILE,
25     OPT_ALGORITHM, OPT_PKEYOPT, OPT_GENPARAM, OPT_TEXT, OPT_CIPHER,
26     OPT_CONFIG,
27     OPT_PROV_ENUM
28 } OPTION_CHOICE;
29
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"},
35 #endif
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"},
40      OPT_CONFIG_OPTION,
41
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"},
49
50     OPT_PROV_OPTIONS,
51
52     /* This is deliberately last. */
53     {OPT_HELP_STR, 1, 1,
54      "Order of options may be important!  See the documentation.\n"},
55     {NULL}
56 };
57
58 int genpkey_main(int argc, char **argv)
59 {
60     CONF *conf = NULL;
61     BIO *in = NULL, *out = NULL;
62     ENGINE *e = NULL;
63     EVP_PKEY *pkey = NULL;
64     EVP_PKEY_CTX *ctx = NULL;
65     char *outfile = NULL, *passarg = NULL, *pass = NULL, *prog;
66     const EVP_CIPHER *cipher = NULL;
67     OPTION_CHOICE o;
68     int outformat = FORMAT_PEM, text = 0, ret = 1, rv, do_param = 0;
69     int private = 0;
70     OSSL_LIB_CTX *libctx = app_get0_libctx();
71     const char *propq = app_get0_propq();
72
73     prog = opt_init(argc, argv, genpkey_options);
74     while ((o = opt_next()) != OPT_EOF) {
75         switch (o) {
76         case OPT_EOF:
77         case OPT_ERR:
78  opthelp:
79             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
80             goto end;
81         case OPT_HELP:
82             ret = 0;
83             opt_help(genpkey_options);
84             goto end;
85         case OPT_OUTFORM:
86             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
87                 goto opthelp;
88             break;
89         case OPT_OUT:
90             outfile = opt_arg();
91             break;
92         case OPT_PASS:
93             passarg = opt_arg();
94             break;
95         case OPT_ENGINE:
96             e = setup_engine(opt_arg(), 0);
97             break;
98         case OPT_PARAMFILE:
99             if (do_param == 1)
100                 goto opthelp;
101             if (!init_keygen_file(&ctx, opt_arg(), e, libctx, propq))
102                 goto end;
103             break;
104         case OPT_ALGORITHM:
105             if (!init_gen_str(&ctx, opt_arg(), e, do_param, libctx, propq))
106                 goto end;
107             break;
108         case OPT_PKEYOPT:
109             if (ctx == NULL) {
110                 BIO_printf(bio_err, "%s: No keytype specified.\n", prog);
111                 goto opthelp;
112             }
113             if (pkey_ctrl_string(ctx, opt_arg()) <= 0) {
114                 BIO_printf(bio_err,
115                            "%s: Error setting %s parameter:\n",
116                            prog, opt_arg());
117                 ERR_print_errors(bio_err);
118                 goto end;
119             }
120             break;
121         case OPT_GENPARAM:
122             if (ctx != NULL) {
123                 BIO_printf(bio_err,
124                            "%s: '-genparam' option must be set before"
125                             " the '-algorithm' option.\n", prog);
126                 goto opthelp;
127             }
128             do_param = 1;
129             break;
130         case OPT_TEXT:
131             text = 1;
132             break;
133         case OPT_CIPHER:
134             if (!opt_cipher(opt_unknown(), &cipher)
135                 || do_param == 1)
136                 goto opthelp;
137             if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE ||
138                 EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE ||
139                 EVP_CIPHER_mode(cipher) == EVP_CIPH_XTS_MODE ||
140                 EVP_CIPHER_mode(cipher) == EVP_CIPH_OCB_MODE) {
141                 BIO_printf(bio_err, "%s: cipher mode not supported\n", prog);
142                 goto end;
143             }
144             break;
145         case OPT_CONFIG:
146             conf = app_load_config_modules(opt_arg());
147             if (conf == NULL)
148                 goto end;
149             break;
150         case OPT_PROV_CASES:
151             if (!opt_provider(o))
152                 goto end;
153             break;
154         }
155     }
156
157     /* No extra arguments. */
158     argc = opt_num_rest();
159     if (argc != 0)
160         goto opthelp;
161
162     private = do_param ? 0 : 1;
163
164     if (ctx == NULL)
165         goto opthelp;
166
167     if (!app_passwd(passarg, NULL, &pass, NULL)) {
168         BIO_puts(bio_err, "Error getting password\n");
169         goto end;
170     }
171
172     out = bio_open_owner(outfile, outformat, private);
173     if (out == NULL)
174         goto end;
175
176     EVP_PKEY_CTX_set_cb(ctx, genpkey_cb);
177     EVP_PKEY_CTX_set_app_data(ctx, bio_err);
178
179     if (do_param) {
180         if (EVP_PKEY_paramgen(ctx, &pkey) <= 0) {
181             BIO_puts(bio_err, "Error generating parameters\n");
182             ERR_print_errors(bio_err);
183             goto end;
184         }
185     } else {
186         if (EVP_PKEY_keygen(ctx, &pkey) <= 0) {
187             BIO_puts(bio_err, "Error generating key\n");
188             ERR_print_errors(bio_err);
189             goto end;
190         }
191     }
192
193     if (do_param) {
194         rv = PEM_write_bio_Parameters(out, pkey);
195     } else if (outformat == FORMAT_PEM) {
196         assert(private);
197         rv = PEM_write_bio_PrivateKey(out, pkey, cipher, NULL, 0, NULL, pass);
198     } else if (outformat == FORMAT_ASN1) {
199         assert(private);
200         rv = i2d_PrivateKey_bio(out, pkey);
201     } else {
202         BIO_printf(bio_err, "Bad format specified for key\n");
203         goto end;
204     }
205
206     ret = 0;
207
208     if (rv <= 0) {
209         BIO_puts(bio_err, "Error writing key\n");
210         ERR_print_errors(bio_err);
211         ret = 1;
212     }
213
214     if (text) {
215         if (do_param)
216             rv = EVP_PKEY_print_params(out, pkey, 0, NULL);
217         else
218             rv = EVP_PKEY_print_private(out, pkey, 0, NULL);
219
220         if (rv <= 0) {
221             BIO_puts(bio_err, "Error printing key\n");
222             ERR_print_errors(bio_err);
223             ret = 1;
224         }
225     }
226
227  end:
228     EVP_PKEY_free(pkey);
229     EVP_PKEY_CTX_free(ctx);
230     BIO_free_all(out);
231     BIO_free(in);
232     release_engine(e);
233     OPENSSL_free(pass);
234     NCONF_free(conf);
235     return ret;
236 }
237
238 static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e,
239                             OSSL_LIB_CTX *libctx, const char *propq)
240 {
241     BIO *pbio;
242     EVP_PKEY *pkey = NULL;
243     EVP_PKEY_CTX *ctx = NULL;
244     if (*pctx) {
245         BIO_puts(bio_err, "Parameters already set!\n");
246         return 0;
247     }
248
249     pbio = BIO_new_file(file, "r");
250     if (pbio == NULL) {
251         BIO_printf(bio_err, "Can't open parameter file %s\n", file);
252         return 0;
253     }
254
255     pkey = PEM_read_bio_Parameters_ex(pbio, NULL, libctx, propq);
256     BIO_free(pbio);
257
258     if (pkey == NULL) {
259         BIO_printf(bio_err, "Error reading parameter file %s\n", file);
260         return 0;
261     }
262
263     if (e != NULL)
264         ctx = EVP_PKEY_CTX_new(pkey, e);
265     else
266         ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
267     if (ctx == NULL)
268         goto err;
269     if (EVP_PKEY_keygen_init(ctx) <= 0)
270         goto err;
271     EVP_PKEY_free(pkey);
272     *pctx = ctx;
273     return 1;
274
275  err:
276     BIO_puts(bio_err, "Error initializing context\n");
277     ERR_print_errors(bio_err);
278     EVP_PKEY_CTX_free(ctx);
279     EVP_PKEY_free(pkey);
280     return 0;
281
282 }
283
284 int init_gen_str(EVP_PKEY_CTX **pctx,
285                  const char *algname, ENGINE *e, int do_param,
286                  OSSL_LIB_CTX *libctx, const char *propq)
287 {
288     EVP_PKEY_CTX *ctx = NULL;
289     int pkey_id;
290
291     if (*pctx) {
292         BIO_puts(bio_err, "Algorithm already set!\n");
293         return 0;
294     }
295
296     pkey_id = get_legacy_pkey_id(libctx, algname, e);
297     if (pkey_id != NID_undef)
298         ctx = EVP_PKEY_CTX_new_id(pkey_id, e);
299     else
300         ctx = EVP_PKEY_CTX_new_from_name(libctx, algname, propq);
301
302     if (ctx == NULL)
303         goto err;
304     if (do_param) {
305         if (EVP_PKEY_paramgen_init(ctx) <= 0)
306             goto err;
307     } else {
308         if (EVP_PKEY_keygen_init(ctx) <= 0)
309             goto err;
310     }
311
312     *pctx = ctx;
313     return 1;
314
315  err:
316     BIO_printf(bio_err, "Error initializing %s context\n", algname);
317     ERR_print_errors(bio_err);
318     EVP_PKEY_CTX_free(ctx);
319     return 0;
320
321 }
322
323 static int genpkey_cb(EVP_PKEY_CTX *ctx)
324 {
325     char c = '*';
326     BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
327     int p;
328     p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
329     if (p == 0)
330         c = '.';
331     if (p == 1)
332         c = '+';
333     if (p == 2)
334         c = '*';
335     if (p == 3)
336         c = '\n';
337     BIO_write(b, &c, 1);
338     (void)BIO_flush(b);
339     return 1;
340 }