Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1 | /* |
Matt Caswell | fecb3aa | 2022-05-03 11:52:38 +0100 | [diff] [blame] | 2 | * Copyright 2006-2022 The OpenSSL Project Authors. All Rights Reserved. |
Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 3 | * |
Richard Levitte | dffa752 | 2018-12-06 13:00:26 +0100 | [diff] [blame] | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 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 |
Dr. Stephen Henson | f5cda4c | 2006-04-11 13:28:52 +0000 | [diff] [blame] | 8 | */ |
Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 9 | |
Dr. Stephen Henson | f5cda4c | 2006-04-11 13:28:52 +0000 | [diff] [blame] | 10 | #include <stdio.h> |
| 11 | #include <string.h> |
| 12 | #include "apps.h" |
Richard Levitte | dab2cd6 | 2018-01-31 11:13:10 +0100 | [diff] [blame] | 13 | #include "progs.h" |
Dr. Stephen Henson | f5cda4c | 2006-04-11 13:28:52 +0000 | [diff] [blame] | 14 | #include <openssl/pem.h> |
| 15 | #include <openssl/err.h> |
| 16 | #include <openssl/evp.h> |
| 17 | |
Philip Prindeville | a414fd6 | 2021-12-21 20:44:07 -0700 | [diff] [blame] | 18 | static int verbose = 1; |
Rich Salz | 7d72dc7 | 2021-05-12 11:45:37 -0400 | [diff] [blame] | 19 | |
Shane Lontis | 7c9a7cf | 2020-06-16 13:04:57 +1000 | [diff] [blame] | 20 | static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e, |
Dr. Matthias St. Pierre | b425001 | 2020-10-15 12:55:50 +0300 | [diff] [blame] | 21 | OSSL_LIB_CTX *libctx, const char *propq); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 22 | typedef enum OPTION_choice { |
Dr. David von Oheimb | b0f9601 | 2021-05-01 15:29:00 +0200 | [diff] [blame] | 23 | OPT_COMMON, |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 24 | OPT_ENGINE, OPT_OUTFORM, OPT_OUT, OPT_PASS, OPT_PARAMFILE, |
Pauli | 6bd4e3f | 2020-02-25 14:29:30 +1000 | [diff] [blame] | 25 | OPT_ALGORITHM, OPT_PKEYOPT, OPT_GENPARAM, OPT_TEXT, OPT_CIPHER, |
Philip Prindeville | a414fd6 | 2021-12-21 20:44:07 -0700 | [diff] [blame] | 26 | OPT_VERBOSE, OPT_QUIET, OPT_CONFIG, |
Pauli | 6bd4e3f | 2020-02-25 14:29:30 +1000 | [diff] [blame] | 27 | OPT_PROV_ENUM |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 28 | } OPTION_CHOICE; |
Dr. Stephen Henson | f5cda4c | 2006-04-11 13:28:52 +0000 | [diff] [blame] | 29 | |
FdaSilvaYY | 44c83eb | 2016-03-13 14:07:50 +0100 | [diff] [blame] | 30 | const OPTIONS genpkey_options[] = { |
Rich Salz | 5388f98 | 2019-11-08 06:08:30 +1000 | [diff] [blame] | 31 | OPT_SECTION("General"), |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 32 | {"help", OPT_HELP, '-', "Display this summary"}, |
Rich Salz | 5388f98 | 2019-11-08 06:08:30 +1000 | [diff] [blame] | 33 | #ifndef OPENSSL_NO_ENGINE |
| 34 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, |
| 35 | #endif |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 36 | {"paramfile", OPT_PARAMFILE, '<', "Parameters file"}, |
| 37 | {"algorithm", OPT_ALGORITHM, 's', "The public key algorithm"}, |
Philip Prindeville | a414fd6 | 2021-12-21 20:44:07 -0700 | [diff] [blame] | 38 | {"verbose", OPT_VERBOSE, '-', "Output status while generating keys"}, |
Pauli | dbd0244 | 2021-08-06 10:01:15 +1000 | [diff] [blame] | 39 | {"quiet", OPT_QUIET, '-', "Do not output status while generating keys"}, |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 40 | {"pkeyopt", OPT_PKEYOPT, 's', |
| 41 | "Set the public key algorithm option as opt:value"}, |
Shane Lontis | 7c9a7cf | 2020-06-16 13:04:57 +1000 | [diff] [blame] | 42 | OPT_CONFIG_OPTION, |
Rich Salz | 5388f98 | 2019-11-08 06:08:30 +1000 | [diff] [blame] | 43 | |
| 44 | OPT_SECTION("Output"), |
| 45 | {"out", OPT_OUT, '>', "Output file"}, |
| 46 | {"outform", OPT_OUTFORM, 'F', "output format (DER or PEM)"}, |
| 47 | {"pass", OPT_PASS, 's', "Output file pass phrase source"}, |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 48 | {"genparam", OPT_GENPARAM, '-', "Generate parameters, not key"}, |
| 49 | {"text", OPT_TEXT, '-', "Print the in text"}, |
| 50 | {"", OPT_CIPHER, '-', "Cipher to use to encrypt the key"}, |
Rich Salz | 5388f98 | 2019-11-08 06:08:30 +1000 | [diff] [blame] | 51 | |
Pauli | 6bd4e3f | 2020-02-25 14:29:30 +1000 | [diff] [blame] | 52 | OPT_PROV_OPTIONS, |
| 53 | |
Rich Salz | 9c3bcfa | 2015-05-15 13:50:38 -0400 | [diff] [blame] | 54 | /* This is deliberately last. */ |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 55 | {OPT_HELP_STR, 1, 1, |
| 56 | "Order of options may be important! See the documentation.\n"}, |
| 57 | {NULL} |
| 58 | }; |
Dr. Stephen Henson | f5cda4c | 2006-04-11 13:28:52 +0000 | [diff] [blame] | 59 | |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 60 | int genpkey_main(int argc, char **argv) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 61 | { |
Shane Lontis | 7c9a7cf | 2020-06-16 13:04:57 +1000 | [diff] [blame] | 62 | CONF *conf = NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 63 | BIO *in = NULL, *out = NULL; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 64 | ENGINE *e = NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 65 | EVP_PKEY *pkey = NULL; |
| 66 | EVP_PKEY_CTX *ctx = NULL; |
Rich Salz | 182717b | 2021-02-07 10:42:23 -0500 | [diff] [blame] | 67 | char *outfile = NULL, *passarg = NULL, *pass = NULL, *prog, *p; |
| 68 | const char *ciphername = NULL, *paramfile = NULL, *algname = NULL; |
Rich Salz | 606a417 | 2021-02-17 16:15:27 -0500 | [diff] [blame] | 69 | EVP_CIPHER *cipher = NULL; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 70 | OPTION_CHOICE o; |
| 71 | int outformat = FORMAT_PEM, text = 0, ret = 1, rv, do_param = 0; |
Pauli | f7d2427 | 2021-06-15 14:07:51 +1000 | [diff] [blame] | 72 | int private = 0, i; |
Dr. Matthias St. Pierre | b425001 | 2020-10-15 12:55:50 +0300 | [diff] [blame] | 73 | OSSL_LIB_CTX *libctx = app_get0_libctx(); |
Rich Salz | 182717b | 2021-02-07 10:42:23 -0500 | [diff] [blame] | 74 | STACK_OF(OPENSSL_STRING) *keyopt = NULL; |
Dr. Stephen Henson | f5cda4c | 2006-04-11 13:28:52 +0000 | [diff] [blame] | 75 | |
Dr. David von Oheimb | 2c27244 | 2021-08-24 12:03:12 +0200 | [diff] [blame] | 76 | opt_set_unknown_name("cipher"); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 77 | prog = opt_init(argc, argv, genpkey_options); |
Rich Salz | 182717b | 2021-02-07 10:42:23 -0500 | [diff] [blame] | 78 | keyopt = sk_OPENSSL_STRING_new_null(); |
| 79 | if (keyopt == NULL) |
| 80 | goto end; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 81 | while ((o = opt_next()) != OPT_EOF) { |
| 82 | switch (o) { |
| 83 | case OPT_EOF: |
| 84 | case OPT_ERR: |
| 85 | opthelp: |
| 86 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); |
| 87 | goto end; |
| 88 | case OPT_HELP: |
| 89 | ret = 0; |
| 90 | opt_help(genpkey_options); |
| 91 | goto end; |
| 92 | case OPT_OUTFORM: |
| 93 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat)) |
| 94 | goto opthelp; |
| 95 | break; |
| 96 | case OPT_OUT: |
| 97 | outfile = opt_arg(); |
| 98 | break; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 99 | case OPT_PASS: |
| 100 | passarg = opt_arg(); |
| 101 | break; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 102 | case OPT_ENGINE: |
| 103 | e = setup_engine(opt_arg(), 0); |
| 104 | break; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 105 | case OPT_PARAMFILE: |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 106 | if (do_param == 1) |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 107 | goto opthelp; |
Rich Salz | 182717b | 2021-02-07 10:42:23 -0500 | [diff] [blame] | 108 | paramfile = opt_arg(); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 109 | break; |
| 110 | case OPT_ALGORITHM: |
Rich Salz | 182717b | 2021-02-07 10:42:23 -0500 | [diff] [blame] | 111 | algname = opt_arg(); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 112 | break; |
| 113 | case OPT_PKEYOPT: |
Rich Salz | 182717b | 2021-02-07 10:42:23 -0500 | [diff] [blame] | 114 | if (!sk_OPENSSL_STRING_push(keyopt, opt_arg())) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 115 | goto end; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 116 | break; |
Rich Salz | 7d72dc7 | 2021-05-12 11:45:37 -0400 | [diff] [blame] | 117 | case OPT_QUIET: |
Philip Prindeville | a414fd6 | 2021-12-21 20:44:07 -0700 | [diff] [blame] | 118 | verbose = 0; |
| 119 | break; |
| 120 | case OPT_VERBOSE: |
| 121 | verbose = 1; |
Rich Salz | 7d72dc7 | 2021-05-12 11:45:37 -0400 | [diff] [blame] | 122 | break; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 123 | case OPT_GENPARAM: |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 124 | do_param = 1; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 125 | break; |
| 126 | case OPT_TEXT: |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 127 | text = 1; |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 128 | break; |
| 129 | case OPT_CIPHER: |
Rich Salz | 182717b | 2021-02-07 10:42:23 -0500 | [diff] [blame] | 130 | ciphername = opt_unknown(); |
Pauli | 6bd4e3f | 2020-02-25 14:29:30 +1000 | [diff] [blame] | 131 | break; |
Shane Lontis | 7c9a7cf | 2020-06-16 13:04:57 +1000 | [diff] [blame] | 132 | case OPT_CONFIG: |
| 133 | conf = app_load_config_modules(opt_arg()); |
| 134 | if (conf == NULL) |
| 135 | goto end; |
| 136 | break; |
Pauli | 6bd4e3f | 2020-02-25 14:29:30 +1000 | [diff] [blame] | 137 | case OPT_PROV_CASES: |
| 138 | if (!opt_provider(o)) |
| 139 | goto end; |
| 140 | break; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 141 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 142 | } |
Rich Salz | 021410e | 2020-11-28 16:12:58 -0500 | [diff] [blame] | 143 | |
| 144 | /* No extra arguments. */ |
Dr. David von Oheimb | d9f0735 | 2021-08-27 15:33:18 +0200 | [diff] [blame] | 145 | if (!opt_check_rest_arg(NULL)) |
Kurt Roeckx | 0335851 | 2016-02-14 20:45:02 +0100 | [diff] [blame] | 146 | goto opthelp; |
| 147 | |
Rich Salz | 182717b | 2021-02-07 10:42:23 -0500 | [diff] [blame] | 148 | /* Fetch cipher, etc. */ |
| 149 | if (paramfile != NULL) { |
| 150 | if (!init_keygen_file(&ctx, paramfile, e, libctx, app_get0_propq())) |
| 151 | goto end; |
| 152 | } |
| 153 | if (algname != NULL) { |
| 154 | if (!init_gen_str(&ctx, algname, e, do_param, libctx, app_get0_propq())) |
| 155 | goto end; |
| 156 | } |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 157 | if (ctx == NULL) |
| 158 | goto opthelp; |
Dr. Stephen Henson | f5cda4c | 2006-04-11 13:28:52 +0000 | [diff] [blame] | 159 | |
Rich Salz | 182717b | 2021-02-07 10:42:23 -0500 | [diff] [blame] | 160 | for (i = 0; i < sk_OPENSSL_STRING_num(keyopt); i++) { |
| 161 | p = sk_OPENSSL_STRING_value(keyopt, i); |
| 162 | if (pkey_ctrl_string(ctx, p) <= 0) { |
| 163 | BIO_printf(bio_err, "%s: Error setting %s parameter:\n", prog, p); |
| 164 | ERR_print_errors(bio_err); |
| 165 | goto end; |
| 166 | } |
| 167 | } |
Dr. David von Oheimb | d9f0735 | 2021-08-27 15:33:18 +0200 | [diff] [blame] | 168 | if (!opt_cipher(ciphername, &cipher)) |
| 169 | goto opthelp; |
| 170 | if (ciphername != NULL && do_param == 1) { |
| 171 | BIO_printf(bio_err, "Cannot use cipher with -genparam option\n"); |
| 172 | goto opthelp; |
| 173 | } |
Rich Salz | 182717b | 2021-02-07 10:42:23 -0500 | [diff] [blame] | 174 | |
| 175 | private = do_param ? 0 : 1; |
| 176 | |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 177 | if (!app_passwd(passarg, NULL, &pass, NULL)) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 178 | BIO_puts(bio_err, "Error getting password\n"); |
| 179 | goto end; |
| 180 | } |
Dr. Stephen Henson | f5cda4c | 2006-04-11 13:28:52 +0000 | [diff] [blame] | 181 | |
Richard Levitte | bdd58d9 | 2015-09-04 12:49:06 +0200 | [diff] [blame] | 182 | out = bio_open_owner(outfile, outformat, private); |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 183 | if (out == NULL) |
| 184 | goto end; |
Dr. Stephen Henson | f5cda4c | 2006-04-11 13:28:52 +0000 | [diff] [blame] | 185 | |
Philip Prindeville | a414fd6 | 2021-12-21 20:44:07 -0700 | [diff] [blame] | 186 | if (verbose) |
Philip Prindeville | e1cd94f | 2021-12-21 22:00:38 -0700 | [diff] [blame] | 187 | EVP_PKEY_CTX_set_cb(ctx, progress_cb); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 188 | EVP_PKEY_CTX_set_app_data(ctx, bio_err); |
Dr. Stephen Henson | f5cda4c | 2006-04-11 13:28:52 +0000 | [diff] [blame] | 189 | |
Dr. David von Oheimb | a7e4ca5 | 2020-06-09 10:21:58 +0200 | [diff] [blame] | 190 | pkey = do_param ? app_paramgen(ctx, algname) |
| 191 | : app_keygen(ctx, algname, 0, 0 /* not verbose */); |
Dr. Stephen Henson | f5cda4c | 2006-04-11 13:28:52 +0000 | [diff] [blame] | 192 | |
Paul Yang | 2234212 | 2017-06-13 01:24:02 +0800 | [diff] [blame] | 193 | if (do_param) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 194 | rv = PEM_write_bio_Parameters(out, pkey); |
Paul Yang | 2234212 | 2017-06-13 01:24:02 +0800 | [diff] [blame] | 195 | } else if (outformat == FORMAT_PEM) { |
Rich Salz | 3b061a0 | 2015-05-02 10:01:33 -0400 | [diff] [blame] | 196 | assert(private); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 197 | rv = PEM_write_bio_PrivateKey(out, pkey, cipher, NULL, 0, NULL, pass); |
Rich Salz | 3b061a0 | 2015-05-02 10:01:33 -0400 | [diff] [blame] | 198 | } else if (outformat == FORMAT_ASN1) { |
| 199 | assert(private); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 200 | rv = i2d_PrivateKey_bio(out, pkey); |
Rich Salz | 3b061a0 | 2015-05-02 10:01:33 -0400 | [diff] [blame] | 201 | } else { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 202 | BIO_printf(bio_err, "Bad format specified for key\n"); |
| 203 | goto end; |
| 204 | } |
Dr. Stephen Henson | f5cda4c | 2006-04-11 13:28:52 +0000 | [diff] [blame] | 205 | |
Nicola Tuveri | 466d30c | 2020-06-27 01:42:49 +0300 | [diff] [blame] | 206 | ret = 0; |
| 207 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 208 | if (rv <= 0) { |
| 209 | BIO_puts(bio_err, "Error writing key\n"); |
Nicola Tuveri | 466d30c | 2020-06-27 01:42:49 +0300 | [diff] [blame] | 210 | ret = 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 211 | } |
Dr. Stephen Henson | f5cda4c | 2006-04-11 13:28:52 +0000 | [diff] [blame] | 212 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 213 | if (text) { |
| 214 | if (do_param) |
| 215 | rv = EVP_PKEY_print_params(out, pkey, 0, NULL); |
| 216 | else |
| 217 | rv = EVP_PKEY_print_private(out, pkey, 0, NULL); |
Dr. Stephen Henson | 01b8b3c | 2006-06-05 11:52:46 +0000 | [diff] [blame] | 218 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 219 | if (rv <= 0) { |
| 220 | BIO_puts(bio_err, "Error printing key\n"); |
Nicola Tuveri | 466d30c | 2020-06-27 01:42:49 +0300 | [diff] [blame] | 221 | ret = 1; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
Dr. Stephen Henson | f5cda4c | 2006-04-11 13:28:52 +0000 | [diff] [blame] | 224 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 225 | end: |
Rich Salz | 182717b | 2021-02-07 10:42:23 -0500 | [diff] [blame] | 226 | sk_OPENSSL_STRING_free(keyopt); |
Dr. David von Oheimb | a7e4ca5 | 2020-06-09 10:21:58 +0200 | [diff] [blame] | 227 | if (ret != 0) |
| 228 | ERR_print_errors(bio_err); |
Rich Salz | c5ba2d9 | 2015-03-28 10:54:15 -0400 | [diff] [blame] | 229 | EVP_PKEY_free(pkey); |
| 230 | EVP_PKEY_CTX_free(ctx); |
Rich Salz | 606a417 | 2021-02-17 16:15:27 -0500 | [diff] [blame] | 231 | EVP_CIPHER_free(cipher); |
Rich Salz | ca3a82c | 2015-03-25 11:31:18 -0400 | [diff] [blame] | 232 | BIO_free_all(out); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 233 | BIO_free(in); |
Richard Levitte | dd1abd4 | 2016-09-28 23:39:18 +0200 | [diff] [blame] | 234 | release_engine(e); |
Rich Salz | b548a1f | 2015-05-01 10:02:07 -0400 | [diff] [blame] | 235 | OPENSSL_free(pass); |
Shane Lontis | 7c9a7cf | 2020-06-16 13:04:57 +1000 | [diff] [blame] | 236 | NCONF_free(conf); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 237 | return ret; |
| 238 | } |
Dr. Stephen Henson | f5cda4c | 2006-04-11 13:28:52 +0000 | [diff] [blame] | 239 | |
Shane Lontis | 7c9a7cf | 2020-06-16 13:04:57 +1000 | [diff] [blame] | 240 | static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e, |
Dr. Matthias St. Pierre | b425001 | 2020-10-15 12:55:50 +0300 | [diff] [blame] | 241 | OSSL_LIB_CTX *libctx, const char *propq) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 242 | { |
| 243 | BIO *pbio; |
| 244 | EVP_PKEY *pkey = NULL; |
| 245 | EVP_PKEY_CTX *ctx = NULL; |
| 246 | if (*pctx) { |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 247 | BIO_puts(bio_err, "Parameters already set!\n"); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 248 | return 0; |
| 249 | } |
Dr. Stephen Henson | f5cda4c | 2006-04-11 13:28:52 +0000 | [diff] [blame] | 250 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 251 | pbio = BIO_new_file(file, "r"); |
Rich Salz | 12a765a | 2019-09-16 15:28:57 -0400 | [diff] [blame] | 252 | if (pbio == NULL) { |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 253 | BIO_printf(bio_err, "Can't open parameter file %s\n", file); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 254 | return 0; |
| 255 | } |
Dr. Stephen Henson | f5cda4c | 2006-04-11 13:28:52 +0000 | [diff] [blame] | 256 | |
Jon Spillett | 3d63348 | 2021-01-19 13:43:35 +1000 | [diff] [blame] | 257 | pkey = PEM_read_bio_Parameters_ex(pbio, NULL, libctx, propq); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 258 | BIO_free(pbio); |
Dr. Stephen Henson | f5cda4c | 2006-04-11 13:28:52 +0000 | [diff] [blame] | 259 | |
Rich Salz | 12a765a | 2019-09-16 15:28:57 -0400 | [diff] [blame] | 260 | if (pkey == NULL) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 261 | BIO_printf(bio_err, "Error reading parameter file %s\n", file); |
| 262 | return 0; |
| 263 | } |
Dr. Stephen Henson | f5cda4c | 2006-04-11 13:28:52 +0000 | [diff] [blame] | 264 | |
Shane Lontis | 7c9a7cf | 2020-06-16 13:04:57 +1000 | [diff] [blame] | 265 | if (e != NULL) |
| 266 | ctx = EVP_PKEY_CTX_new(pkey, e); |
| 267 | else |
| 268 | ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq); |
Matt Caswell | 96487cd | 2015-10-30 11:18:04 +0000 | [diff] [blame] | 269 | if (ctx == NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 270 | goto err; |
| 271 | if (EVP_PKEY_keygen_init(ctx) <= 0) |
| 272 | goto err; |
| 273 | EVP_PKEY_free(pkey); |
| 274 | *pctx = ctx; |
| 275 | return 1; |
Dr. Stephen Henson | f5cda4c | 2006-04-11 13:28:52 +0000 | [diff] [blame] | 276 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 277 | err: |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 278 | BIO_puts(bio_err, "Error initializing context\n"); |
| 279 | ERR_print_errors(bio_err); |
Rich Salz | c5ba2d9 | 2015-03-28 10:54:15 -0400 | [diff] [blame] | 280 | EVP_PKEY_CTX_free(ctx); |
| 281 | EVP_PKEY_free(pkey); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 282 | return 0; |
Dr. Stephen Henson | f5cda4c | 2006-04-11 13:28:52 +0000 | [diff] [blame] | 283 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 284 | } |
Dr. Stephen Henson | f5cda4c | 2006-04-11 13:28:52 +0000 | [diff] [blame] | 285 | |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 286 | int init_gen_str(EVP_PKEY_CTX **pctx, |
Shane Lontis | 7c9a7cf | 2020-06-16 13:04:57 +1000 | [diff] [blame] | 287 | const char *algname, ENGINE *e, int do_param, |
Dr. Matthias St. Pierre | b425001 | 2020-10-15 12:55:50 +0300 | [diff] [blame] | 288 | OSSL_LIB_CTX *libctx, const char *propq) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 289 | { |
| 290 | EVP_PKEY_CTX *ctx = NULL; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 291 | int pkey_id; |
Dr. Stephen Henson | 01b8b3c | 2006-06-05 11:52:46 +0000 | [diff] [blame] | 292 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 293 | if (*pctx) { |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 294 | BIO_puts(bio_err, "Algorithm already set!\n"); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 295 | return 0; |
| 296 | } |
Dr. Stephen Henson | b3c6a33 | 2006-07-12 18:00:20 +0000 | [diff] [blame] | 297 | |
Matt Caswell | 0f386f2 | 2020-11-02 11:04:06 +0000 | [diff] [blame] | 298 | pkey_id = get_legacy_pkey_id(libctx, algname, e); |
| 299 | if (pkey_id != NID_undef) |
Shane Lontis | 7c9a7cf | 2020-06-16 13:04:57 +1000 | [diff] [blame] | 300 | ctx = EVP_PKEY_CTX_new_id(pkey_id, e); |
Matt Caswell | 0f386f2 | 2020-11-02 11:04:06 +0000 | [diff] [blame] | 301 | else |
Shane Lontis | 7c9a7cf | 2020-06-16 13:04:57 +1000 | [diff] [blame] | 302 | ctx = EVP_PKEY_CTX_new_from_name(libctx, algname, propq); |
Dr. Stephen Henson | f5cda4c | 2006-04-11 13:28:52 +0000 | [diff] [blame] | 303 | |
Matt Caswell | 0f386f2 | 2020-11-02 11:04:06 +0000 | [diff] [blame] | 304 | if (ctx == NULL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 305 | goto err; |
| 306 | if (do_param) { |
| 307 | if (EVP_PKEY_paramgen_init(ctx) <= 0) |
| 308 | goto err; |
| 309 | } else { |
| 310 | if (EVP_PKEY_keygen_init(ctx) <= 0) |
| 311 | goto err; |
| 312 | } |
Dr. Stephen Henson | f5cda4c | 2006-04-11 13:28:52 +0000 | [diff] [blame] | 313 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 314 | *pctx = ctx; |
| 315 | return 1; |
Dr. Stephen Henson | f5cda4c | 2006-04-11 13:28:52 +0000 | [diff] [blame] | 316 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 317 | err: |
Rich Salz | 7e1b748 | 2015-04-24 15:26:15 -0400 | [diff] [blame] | 318 | BIO_printf(bio_err, "Error initializing %s context\n", algname); |
| 319 | ERR_print_errors(bio_err); |
Rich Salz | c5ba2d9 | 2015-03-28 10:54:15 -0400 | [diff] [blame] | 320 | EVP_PKEY_CTX_free(ctx); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 321 | return 0; |
Dr. Stephen Henson | f5cda4c | 2006-04-11 13:28:52 +0000 | [diff] [blame] | 322 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 323 | } |
Dr. Stephen Henson | f5cda4c | 2006-04-11 13:28:52 +0000 | [diff] [blame] | 324 | |