Raise an error on syscall failure in tls_retry_write_records
[openssl.git] / apps / genpkey.c
1 /*
2  * Copyright 2006-2024 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 verbose = 1;
19
20 static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e,
21                             OSSL_LIB_CTX *libctx, const char *propq);
22 typedef enum OPTION_choice {
23     OPT_COMMON,
24     OPT_ENGINE, OPT_OUTFORM, OPT_OUT, OPT_PASS, OPT_PARAMFILE,
25     OPT_ALGORITHM, OPT_PKEYOPT, OPT_GENPARAM, OPT_TEXT, OPT_CIPHER,
26     OPT_VERBOSE, OPT_QUIET, OPT_CONFIG, OPT_OUTPUBKEY,
27     OPT_PROV_ENUM, OPT_R_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     {"verbose", OPT_VERBOSE, '-', "Output status while generating keys"},
39     {"quiet", OPT_QUIET, '-', "Do not output status while generating keys"},
40     {"pkeyopt", OPT_PKEYOPT, 's',
41      "Set the public key algorithm option as opt:value"},
42      OPT_CONFIG_OPTION,
43
44     OPT_SECTION("Output"),
45     {"out", OPT_OUT, '>', "Output (private key) file"},
46     {"outpubkey", OPT_OUTPUBKEY, '>', "Output public key file"},
47     {"outform", OPT_OUTFORM, 'F', "output format (DER or PEM)"},
48     {"pass", OPT_PASS, 's', "Output file pass phrase source"},
49     {"genparam", OPT_GENPARAM, '-', "Generate parameters, not key"},
50     {"text", OPT_TEXT, '-', "Print the private key in text"},
51     {"", OPT_CIPHER, '-', "Cipher to use to encrypt the key"},
52
53     OPT_PROV_OPTIONS,
54     OPT_R_OPTIONS,
55
56     /* This is deliberately last. */
57     {OPT_HELP_STR, 1, 1,
58      "Order of options may be important!  See the documentation.\n"},
59     {NULL}
60 };
61
62 static const char *param_datatype_2name(unsigned int type, int *ishex)
63 {
64     *ishex = 0;
65
66     switch (type) {
67     case OSSL_PARAM_INTEGER: return "int";
68     case OSSL_PARAM_UNSIGNED_INTEGER: return "uint";
69     case OSSL_PARAM_REAL: return "float";
70     case OSSL_PARAM_OCTET_STRING: *ishex = 1; return "string";
71     case OSSL_PARAM_UTF8_STRING: return "string";
72     default:
73         return NULL;
74     }
75 }
76
77 static void show_gen_pkeyopt(const char *algname, OSSL_LIB_CTX *libctx, const char *propq)
78 {
79     EVP_PKEY_CTX *ctx = NULL;
80     const OSSL_PARAM *params;
81     int i, ishex = 0;
82
83     if (algname == NULL)
84         return;
85     ctx = EVP_PKEY_CTX_new_from_name(libctx, algname, propq);
86     if (ctx == NULL)
87         return;
88
89     if (EVP_PKEY_keygen_init(ctx) <= 0)
90         goto cleanup;
91     params = EVP_PKEY_CTX_settable_params(ctx);
92     if (params == NULL)
93         goto cleanup;
94
95     BIO_printf(bio_err, "\nThe possible -pkeyopt arguments are:\n");
96     for (i = 0; params[i].key != NULL; ++i) {
97         const char *name = param_datatype_2name(params[i].data_type, &ishex);
98
99         if (name != NULL)
100             BIO_printf(bio_err, "    %s%s:%s\n", ishex ? "hex" : "", params[i].key, name);
101     }
102 cleanup:
103     EVP_PKEY_CTX_free(ctx);
104 }
105
106 int genpkey_main(int argc, char **argv)
107 {
108     CONF *conf = NULL;
109     BIO *in = NULL, *out = NULL, *outpubkey = NULL;
110     ENGINE *e = NULL;
111     EVP_PKEY *pkey = NULL;
112     EVP_PKEY_CTX *ctx = NULL;
113     char *outfile = NULL, *passarg = NULL, *pass = NULL, *prog, *p;
114     char *outpubkeyfile = NULL;
115     const char *ciphername = NULL, *paramfile = NULL, *algname = NULL;
116     EVP_CIPHER *cipher = NULL;
117     OPTION_CHOICE o;
118     int outformat = FORMAT_PEM, text = 0, ret = 1, rv, do_param = 0;
119     int private = 0, i;
120     OSSL_LIB_CTX *libctx = app_get0_libctx();
121     STACK_OF(OPENSSL_STRING) *keyopt = NULL;
122
123     opt_set_unknown_name("cipher");
124     prog = opt_init(argc, argv, genpkey_options);
125     keyopt = sk_OPENSSL_STRING_new_null();
126     if (keyopt == NULL)
127         goto end;
128     while ((o = opt_next()) != OPT_EOF) {
129         switch (o) {
130         case OPT_EOF:
131         case OPT_ERR:
132  opthelp:
133             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
134             goto end;
135         case OPT_HELP:
136             ret = 0;
137             opt_help(genpkey_options);
138             show_gen_pkeyopt(algname, libctx, app_get0_propq());
139             goto end;
140         case OPT_OUTFORM:
141             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
142                 goto opthelp;
143             break;
144         case OPT_OUT:
145             outfile = opt_arg();
146             break;
147         case OPT_OUTPUBKEY:
148             outpubkeyfile = opt_arg();
149             break;
150         case OPT_PASS:
151             passarg = opt_arg();
152             break;
153         case OPT_ENGINE:
154             e = setup_engine(opt_arg(), 0);
155             break;
156         case OPT_PARAMFILE:
157             if (do_param == 1)
158                 goto opthelp;
159             paramfile = opt_arg();
160             break;
161         case OPT_ALGORITHM:
162             algname = opt_arg();
163             break;
164         case OPT_PKEYOPT:
165             if (!sk_OPENSSL_STRING_push(keyopt, opt_arg()))
166                 goto end;
167             break;
168         case OPT_QUIET:
169             verbose = 0;
170             break;
171         case OPT_VERBOSE:
172             verbose = 1;
173             break;
174         case OPT_GENPARAM:
175             do_param = 1;
176             break;
177         case OPT_TEXT:
178             text = 1;
179             break;
180         case OPT_CIPHER:
181             ciphername = opt_unknown();
182             break;
183         case OPT_CONFIG:
184             conf = app_load_config_modules(opt_arg());
185             if (conf == NULL)
186                 goto end;
187             break;
188         case OPT_PROV_CASES:
189             if (!opt_provider(o))
190                 goto end;
191             break;
192         case OPT_R_CASES:
193             if (!opt_rand(o))
194                 goto end;
195             break;
196         }
197     }
198
199     /* No extra arguments. */
200     if (!opt_check_rest_arg(NULL))
201         goto opthelp;
202
203     if (!app_RAND_load())
204         goto end;
205
206     /* Fetch cipher, etc. */
207     if (paramfile != NULL) {
208         if (!init_keygen_file(&ctx, paramfile, e, libctx, app_get0_propq()))
209             goto end;
210     }
211     if (algname != NULL) {
212         if (!init_gen_str(&ctx, algname, e, do_param, libctx, app_get0_propq()))
213             goto end;
214     }
215     if (ctx == NULL)
216         goto opthelp;
217
218     for (i = 0; i < sk_OPENSSL_STRING_num(keyopt); i++) {
219         p = sk_OPENSSL_STRING_value(keyopt, i);
220         if (pkey_ctrl_string(ctx, p) <= 0) {
221             BIO_printf(bio_err, "%s: Error setting %s parameter:\n", prog, p);
222             ERR_print_errors(bio_err);
223             goto end;
224         }
225     }
226     if (!opt_cipher(ciphername, &cipher))
227         goto opthelp;
228     if (ciphername != NULL && do_param == 1) {
229         BIO_printf(bio_err, "Cannot use cipher with -genparam option\n");
230         goto opthelp;
231     }
232
233     private = do_param ? 0 : 1;
234
235     if (!app_passwd(passarg, NULL, &pass, NULL)) {
236         BIO_puts(bio_err, "Error getting password\n");
237         goto end;
238     }
239
240     out = bio_open_owner(outfile, outformat, private);
241     if (out == NULL)
242         goto end;
243
244     if (outpubkeyfile != NULL) {
245         outpubkey = bio_open_owner(outpubkeyfile, outformat, private);
246         if (outpubkey == NULL)
247             goto end;
248     }
249
250     if (verbose)
251         EVP_PKEY_CTX_set_cb(ctx, progress_cb);
252     EVP_PKEY_CTX_set_app_data(ctx, bio_err);
253
254     pkey = do_param ? app_paramgen(ctx, algname)
255                     : app_keygen(ctx, algname, 0, 0 /* not verbose */);
256     if (pkey == NULL)
257         goto end;
258
259     if (do_param) {
260         rv = PEM_write_bio_Parameters(out, pkey);
261     } else if (outformat == FORMAT_PEM) {
262         assert(private);
263         rv = PEM_write_bio_PrivateKey(out, pkey, cipher, NULL, 0, NULL, pass);
264         if (rv > 0 && outpubkey != NULL)
265            rv = PEM_write_bio_PUBKEY(outpubkey, pkey);
266     } else if (outformat == FORMAT_ASN1) {
267         assert(private);
268         rv = i2d_PrivateKey_bio(out, pkey);
269         if (rv > 0 && outpubkey != NULL)
270            rv = i2d_PUBKEY_bio(outpubkey, pkey);
271     } else {
272         BIO_printf(bio_err, "Bad format specified for key\n");
273         goto end;
274     }
275
276     ret = 0;
277
278     if (rv <= 0) {
279         BIO_puts(bio_err, "Error writing key(s)\n");
280         ret = 1;
281     }
282
283     if (text) {
284         if (do_param)
285             rv = EVP_PKEY_print_params(out, pkey, 0, NULL);
286         else
287             rv = EVP_PKEY_print_private(out, pkey, 0, NULL);
288
289         if (rv <= 0) {
290             BIO_puts(bio_err, "Error printing key\n");
291             ret = 1;
292         }
293     }
294
295  end:
296     sk_OPENSSL_STRING_free(keyopt);
297     if (ret != 0)
298         ERR_print_errors(bio_err);
299     EVP_PKEY_free(pkey);
300     EVP_PKEY_CTX_free(ctx);
301     EVP_CIPHER_free(cipher);
302     BIO_free_all(out);
303     BIO_free_all(outpubkey);
304     BIO_free(in);
305     release_engine(e);
306     OPENSSL_free(pass);
307     NCONF_free(conf);
308     return ret;
309 }
310
311 static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e,
312                             OSSL_LIB_CTX *libctx, const char *propq)
313 {
314     BIO *pbio;
315     EVP_PKEY *pkey = NULL;
316     EVP_PKEY_CTX *ctx = NULL;
317     if (*pctx) {
318         BIO_puts(bio_err, "Parameters already set!\n");
319         return 0;
320     }
321
322     pbio = BIO_new_file(file, "r");
323     if (pbio == NULL) {
324         BIO_printf(bio_err, "Can't open parameter file %s\n", file);
325         return 0;
326     }
327
328     pkey = PEM_read_bio_Parameters_ex(pbio, NULL, libctx, propq);
329     BIO_free(pbio);
330
331     if (pkey == NULL) {
332         BIO_printf(bio_err, "Error reading parameter file %s\n", file);
333         return 0;
334     }
335
336     if (e != NULL)
337         ctx = EVP_PKEY_CTX_new(pkey, e);
338     else
339         ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
340     if (ctx == NULL)
341         goto err;
342     if (EVP_PKEY_keygen_init(ctx) <= 0)
343         goto err;
344     EVP_PKEY_free(pkey);
345     *pctx = ctx;
346     return 1;
347
348  err:
349     BIO_puts(bio_err, "Error initializing context\n");
350     ERR_print_errors(bio_err);
351     EVP_PKEY_CTX_free(ctx);
352     EVP_PKEY_free(pkey);
353     return 0;
354
355 }
356
357 int init_gen_str(EVP_PKEY_CTX **pctx,
358                  const char *algname, ENGINE *e, int do_param,
359                  OSSL_LIB_CTX *libctx, const char *propq)
360 {
361     EVP_PKEY_CTX *ctx = NULL;
362     int pkey_id;
363
364     if (*pctx) {
365         BIO_puts(bio_err, "Algorithm already set!\n");
366         return 0;
367     }
368
369     pkey_id = get_legacy_pkey_id(libctx, algname, e);
370     if (pkey_id != NID_undef)
371         ctx = EVP_PKEY_CTX_new_id(pkey_id, e);
372     else
373         ctx = EVP_PKEY_CTX_new_from_name(libctx, algname, propq);
374
375     if (ctx == NULL)
376         goto err;
377     if (do_param) {
378         if (EVP_PKEY_paramgen_init(ctx) <= 0)
379             goto err;
380     } else {
381         if (EVP_PKEY_keygen_init(ctx) <= 0)
382             goto err;
383     }
384
385     *pctx = ctx;
386     return 1;
387
388  err:
389     BIO_printf(bio_err, "Error initializing %s context\n", algname);
390     ERR_print_errors(bio_err);
391     EVP_PKEY_CTX_free(ctx);
392     return 0;
393
394 }
395