Build resource files
[openssl.git] / apps / genpkey.c
1 /*
2  * Copyright 2006-2021 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, *p;
66     const char *ciphername = NULL, *paramfile = NULL, *algname = NULL;
67     const EVP_CIPHER *cipher = NULL;
68     OPTION_CHOICE o;
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;
73
74     prog = opt_init(argc, argv, genpkey_options);
75     keyopt = sk_OPENSSL_STRING_new_null();
76     if (keyopt == NULL)
77         goto end;
78     while ((o = opt_next()) != OPT_EOF) {
79         switch (o) {
80         case OPT_EOF:
81         case OPT_ERR:
82  opthelp:
83             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
84             goto end;
85         case OPT_HELP:
86             ret = 0;
87             opt_help(genpkey_options);
88             goto end;
89         case OPT_OUTFORM:
90             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
91                 goto opthelp;
92             break;
93         case OPT_OUT:
94             outfile = opt_arg();
95             break;
96         case OPT_PASS:
97             passarg = opt_arg();
98             break;
99         case OPT_ENGINE:
100             e = setup_engine(opt_arg(), 0);
101             break;
102         case OPT_PARAMFILE:
103             if (do_param == 1)
104                 goto opthelp;
105             paramfile = opt_arg();
106             break;
107         case OPT_ALGORITHM:
108             algname = opt_arg();
109             break;
110         case OPT_PKEYOPT:
111             if (!sk_OPENSSL_STRING_push(keyopt, opt_arg()))
112                 goto end;
113             break;
114         case OPT_GENPARAM:
115             do_param = 1;
116             break;
117         case OPT_TEXT:
118             text = 1;
119             break;
120         case OPT_CIPHER:
121             ciphername = opt_unknown();
122             break;
123         case OPT_CONFIG:
124             conf = app_load_config_modules(opt_arg());
125             if (conf == NULL)
126                 goto end;
127             break;
128         case OPT_PROV_CASES:
129             if (!opt_provider(o))
130                 goto end;
131             break;
132         }
133     }
134
135     /* No extra arguments. */
136     argc = opt_num_rest();
137     if (argc != 0)
138         goto opthelp;
139
140     /* Fetch cipher, etc. */
141     if (paramfile != NULL) {
142         if (!init_keygen_file(&ctx, paramfile, e, libctx, app_get0_propq()))
143             goto end;
144     }
145     if (algname != NULL) {
146         if (!init_gen_str(&ctx, algname, e, do_param, libctx, app_get0_propq()))
147             goto end;
148     }
149     if (ctx == NULL)
150         goto opthelp;
151
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);
157             goto end;
158         }
159     }
160     if (ciphername != NULL) {
161         if (!opt_cipher(ciphername, &cipher) || do_param == 1)
162             goto opthelp;
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);
167             goto end;
168         }
169     }
170
171     private = do_param ? 0 : 1;
172
173     if (!app_passwd(passarg, NULL, &pass, NULL)) {
174         BIO_puts(bio_err, "Error getting password\n");
175         goto end;
176     }
177
178     out = bio_open_owner(outfile, outformat, private);
179     if (out == NULL)
180         goto end;
181
182     EVP_PKEY_CTX_set_cb(ctx, genpkey_cb);
183     EVP_PKEY_CTX_set_app_data(ctx, bio_err);
184
185     if (do_param) {
186         if (EVP_PKEY_paramgen(ctx, &pkey) <= 0) {
187             BIO_puts(bio_err, "Error generating parameters\n");
188             ERR_print_errors(bio_err);
189             goto end;
190         }
191     } else {
192         if (EVP_PKEY_keygen(ctx, &pkey) <= 0) {
193             BIO_puts(bio_err, "Error generating key\n");
194             ERR_print_errors(bio_err);
195             goto end;
196         }
197     }
198
199     if (do_param) {
200         rv = PEM_write_bio_Parameters(out, pkey);
201     } else if (outformat == FORMAT_PEM) {
202         assert(private);
203         rv = PEM_write_bio_PrivateKey(out, pkey, cipher, NULL, 0, NULL, pass);
204     } else if (outformat == FORMAT_ASN1) {
205         assert(private);
206         rv = i2d_PrivateKey_bio(out, pkey);
207     } else {
208         BIO_printf(bio_err, "Bad format specified for key\n");
209         goto end;
210     }
211
212     ret = 0;
213
214     if (rv <= 0) {
215         BIO_puts(bio_err, "Error writing key\n");
216         ERR_print_errors(bio_err);
217         ret = 1;
218     }
219
220     if (text) {
221         if (do_param)
222             rv = EVP_PKEY_print_params(out, pkey, 0, NULL);
223         else
224             rv = EVP_PKEY_print_private(out, pkey, 0, NULL);
225
226         if (rv <= 0) {
227             BIO_puts(bio_err, "Error printing key\n");
228             ERR_print_errors(bio_err);
229             ret = 1;
230         }
231     }
232
233  end:
234     sk_OPENSSL_STRING_free(keyopt);
235     EVP_PKEY_free(pkey);
236     EVP_PKEY_CTX_free(ctx);
237     BIO_free_all(out);
238     BIO_free(in);
239     release_engine(e);
240     OPENSSL_free(pass);
241     NCONF_free(conf);
242     return ret;
243 }
244
245 static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e,
246                             OSSL_LIB_CTX *libctx, const char *propq)
247 {
248     BIO *pbio;
249     EVP_PKEY *pkey = NULL;
250     EVP_PKEY_CTX *ctx = NULL;
251     if (*pctx) {
252         BIO_puts(bio_err, "Parameters already set!\n");
253         return 0;
254     }
255
256     pbio = BIO_new_file(file, "r");
257     if (pbio == NULL) {
258         BIO_printf(bio_err, "Can't open parameter file %s\n", file);
259         return 0;
260     }
261
262     pkey = PEM_read_bio_Parameters_ex(pbio, NULL, libctx, propq);
263     BIO_free(pbio);
264
265     if (pkey == NULL) {
266         BIO_printf(bio_err, "Error reading parameter file %s\n", file);
267         return 0;
268     }
269
270     if (e != NULL)
271         ctx = EVP_PKEY_CTX_new(pkey, e);
272     else
273         ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
274     if (ctx == NULL)
275         goto err;
276     if (EVP_PKEY_keygen_init(ctx) <= 0)
277         goto err;
278     EVP_PKEY_free(pkey);
279     *pctx = ctx;
280     return 1;
281
282  err:
283     BIO_puts(bio_err, "Error initializing context\n");
284     ERR_print_errors(bio_err);
285     EVP_PKEY_CTX_free(ctx);
286     EVP_PKEY_free(pkey);
287     return 0;
288
289 }
290
291 int init_gen_str(EVP_PKEY_CTX **pctx,
292                  const char *algname, ENGINE *e, int do_param,
293                  OSSL_LIB_CTX *libctx, const char *propq)
294 {
295     EVP_PKEY_CTX *ctx = NULL;
296     int pkey_id;
297
298     if (*pctx) {
299         BIO_puts(bio_err, "Algorithm already set!\n");
300         return 0;
301     }
302
303     pkey_id = get_legacy_pkey_id(libctx, algname, e);
304     if (pkey_id != NID_undef)
305         ctx = EVP_PKEY_CTX_new_id(pkey_id, e);
306     else
307         ctx = EVP_PKEY_CTX_new_from_name(libctx, algname, propq);
308
309     if (ctx == NULL)
310         goto err;
311     if (do_param) {
312         if (EVP_PKEY_paramgen_init(ctx) <= 0)
313             goto err;
314     } else {
315         if (EVP_PKEY_keygen_init(ctx) <= 0)
316             goto err;
317     }
318
319     *pctx = ctx;
320     return 1;
321
322  err:
323     BIO_printf(bio_err, "Error initializing %s context\n", algname);
324     ERR_print_errors(bio_err);
325     EVP_PKEY_CTX_free(ctx);
326     return 0;
327
328 }
329
330 static int genpkey_cb(EVP_PKEY_CTX *ctx)
331 {
332     char c = '*';
333     BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
334     int p;
335     p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
336     if (p == 0)
337         c = '.';
338     if (p == 1)
339         c = '+';
340     if (p == 2)
341         c = '*';
342     if (p == 3)
343         c = '\n';
344     BIO_write(b, &c, 1);
345     (void)BIO_flush(b);
346     return 1;
347 }