]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
846e33c7 | 2 | * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved. |
f5cda4cb | 3 | * |
846e33c7 RS |
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 | |
f5cda4cb | 8 | */ |
846e33c7 | 9 | |
f5cda4cb DSH |
10 | #include <stdio.h> |
11 | #include <string.h> | |
12 | #include "apps.h" | |
13 | #include <openssl/pem.h> | |
14 | #include <openssl/err.h> | |
15 | #include <openssl/evp.h> | |
01b8b3c7 | 16 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e | 17 | # include <openssl/engine.h> |
01b8b3c7 | 18 | #endif |
f5cda4cb | 19 | |
7e1b7485 | 20 | static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e); |
f5cda4cb DSH |
21 | static int genpkey_cb(EVP_PKEY_CTX *ctx); |
22 | ||
7e1b7485 RS |
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 | |
27 | } OPTION_CHOICE; | |
28 | ||
44c83ebd | 29 | const OPTIONS genpkey_options[] = { |
7e1b7485 RS |
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"}, | |
43 | #endif | |
9c3bcfa0 | 44 | /* This is deliberately last. */ |
7e1b7485 RS |
45 | {OPT_HELP_STR, 1, 1, |
46 | "Order of options may be important! See the documentation.\n"}, | |
47 | {NULL} | |
48 | }; | |
f5cda4cb | 49 | |
7e1b7485 | 50 | int genpkey_main(int argc, char **argv) |
0f113f3e | 51 | { |
0f113f3e | 52 | BIO *in = NULL, *out = NULL; |
7e1b7485 | 53 | ENGINE *e = NULL; |
0f113f3e MC |
54 | EVP_PKEY *pkey = NULL; |
55 | EVP_PKEY_CTX *ctx = NULL; | |
7e1b7485 RS |
56 | char *outfile = NULL, *passarg = NULL, *pass = NULL, *prog; |
57 | const EVP_CIPHER *cipher = NULL; | |
58 | OPTION_CHOICE o; | |
59 | int outformat = FORMAT_PEM, text = 0, ret = 1, rv, do_param = 0; | |
3b061a00 | 60 | int private = 0; |
7e1b7485 RS |
61 | |
62 | prog = opt_init(argc, argv, genpkey_options); | |
63 | while ((o = opt_next()) != OPT_EOF) { | |
64 | switch (o) { | |
65 | case OPT_EOF: | |
66 | case OPT_ERR: | |
67 | opthelp: | |
68 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
69 | goto end; | |
70 | case OPT_HELP: | |
71 | ret = 0; | |
72 | opt_help(genpkey_options); | |
73 | goto end; | |
74 | case OPT_OUTFORM: | |
75 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat)) | |
76 | goto opthelp; | |
77 | break; | |
78 | case OPT_OUT: | |
79 | outfile = opt_arg(); | |
80 | break; | |
7e1b7485 RS |
81 | case OPT_PASS: |
82 | passarg = opt_arg(); | |
83 | break; | |
7e1b7485 RS |
84 | case OPT_ENGINE: |
85 | e = setup_engine(opt_arg(), 0); | |
86 | break; | |
7e1b7485 | 87 | case OPT_PARAMFILE: |
0f113f3e | 88 | if (do_param == 1) |
7e1b7485 RS |
89 | goto opthelp; |
90 | if (!init_keygen_file(&ctx, opt_arg(), e)) | |
0f113f3e | 91 | goto end; |
7e1b7485 RS |
92 | break; |
93 | case OPT_ALGORITHM: | |
94 | if (!init_gen_str(&ctx, opt_arg(), e, do_param)) | |
0f113f3e | 95 | goto end; |
7e1b7485 RS |
96 | break; |
97 | case OPT_PKEYOPT: | |
98 | if (ctx == NULL) { | |
99 | BIO_printf(bio_err, "%s: No keytype specified.\n", prog); | |
100 | goto opthelp; | |
101 | } | |
102 | if (pkey_ctrl_string(ctx, opt_arg()) <= 0) { | |
103 | BIO_printf(bio_err, | |
104 | "%s: Error setting %s parameter:\n", | |
105 | prog, opt_arg()); | |
0f113f3e MC |
106 | ERR_print_errors(bio_err); |
107 | goto end; | |
108 | } | |
7e1b7485 RS |
109 | break; |
110 | case OPT_GENPARAM: | |
111 | if (ctx != NULL) | |
112 | goto opthelp; | |
0f113f3e | 113 | do_param = 1; |
7e1b7485 RS |
114 | break; |
115 | case OPT_TEXT: | |
0f113f3e | 116 | text = 1; |
7e1b7485 RS |
117 | break; |
118 | case OPT_CIPHER: | |
119 | if (!opt_cipher(opt_unknown(), &cipher) | |
120 | || do_param == 1) | |
121 | goto opthelp; | |
0f113f3e | 122 | } |
0f113f3e | 123 | } |
7e1b7485 | 124 | argc = opt_num_rest(); |
03358517 KR |
125 | if (argc != 0) |
126 | goto opthelp; | |
127 | ||
3b061a00 | 128 | private = do_param ? 0 : 1; |
0f113f3e | 129 | |
7e1b7485 RS |
130 | if (ctx == NULL) |
131 | goto opthelp; | |
0f113f3e | 132 | |
7e1b7485 | 133 | if (!app_passwd(passarg, NULL, &pass, NULL)) { |
0f113f3e MC |
134 | BIO_puts(bio_err, "Error getting password\n"); |
135 | goto end; | |
136 | } | |
137 | ||
bdd58d98 | 138 | out = bio_open_owner(outfile, outformat, private); |
7e1b7485 RS |
139 | if (out == NULL) |
140 | goto end; | |
0f113f3e MC |
141 | |
142 | EVP_PKEY_CTX_set_cb(ctx, genpkey_cb); | |
143 | EVP_PKEY_CTX_set_app_data(ctx, bio_err); | |
144 | ||
145 | if (do_param) { | |
146 | if (EVP_PKEY_paramgen(ctx, &pkey) <= 0) { | |
147 | BIO_puts(bio_err, "Error generating parameters\n"); | |
148 | ERR_print_errors(bio_err); | |
149 | goto end; | |
150 | } | |
151 | } else { | |
152 | if (EVP_PKEY_keygen(ctx, &pkey) <= 0) { | |
153 | BIO_puts(bio_err, "Error generating key\n"); | |
154 | ERR_print_errors(bio_err); | |
155 | goto end; | |
156 | } | |
157 | } | |
158 | ||
159 | if (do_param) | |
160 | rv = PEM_write_bio_Parameters(out, pkey); | |
3b061a00 RS |
161 | else if (outformat == FORMAT_PEM) { |
162 | assert(private); | |
0f113f3e | 163 | rv = PEM_write_bio_PrivateKey(out, pkey, cipher, NULL, 0, NULL, pass); |
3b061a00 RS |
164 | } else if (outformat == FORMAT_ASN1) { |
165 | assert(private); | |
0f113f3e | 166 | rv = i2d_PrivateKey_bio(out, pkey); |
3b061a00 | 167 | } else { |
0f113f3e MC |
168 | BIO_printf(bio_err, "Bad format specified for key\n"); |
169 | goto end; | |
170 | } | |
171 | ||
172 | if (rv <= 0) { | |
173 | BIO_puts(bio_err, "Error writing key\n"); | |
174 | ERR_print_errors(bio_err); | |
175 | } | |
176 | ||
177 | if (text) { | |
178 | if (do_param) | |
179 | rv = EVP_PKEY_print_params(out, pkey, 0, NULL); | |
180 | else | |
181 | rv = EVP_PKEY_print_private(out, pkey, 0, NULL); | |
182 | ||
183 | if (rv <= 0) { | |
184 | BIO_puts(bio_err, "Error printing key\n"); | |
185 | ERR_print_errors(bio_err); | |
186 | } | |
187 | } | |
188 | ||
189 | ret = 0; | |
190 | ||
191 | end: | |
c5ba2d99 RS |
192 | EVP_PKEY_free(pkey); |
193 | EVP_PKEY_CTX_free(ctx); | |
ca3a82c3 | 194 | BIO_free_all(out); |
0f113f3e | 195 | BIO_free(in); |
b548a1f1 | 196 | OPENSSL_free(pass); |
0f113f3e MC |
197 | |
198 | return ret; | |
199 | } | |
f5cda4cb | 200 | |
7e1b7485 | 201 | static int init_keygen_file(EVP_PKEY_CTX **pctx, const char *file, ENGINE *e) |
0f113f3e MC |
202 | { |
203 | BIO *pbio; | |
204 | EVP_PKEY *pkey = NULL; | |
205 | EVP_PKEY_CTX *ctx = NULL; | |
206 | if (*pctx) { | |
7e1b7485 | 207 | BIO_puts(bio_err, "Parameters already set!\n"); |
0f113f3e MC |
208 | return 0; |
209 | } | |
210 | ||
211 | pbio = BIO_new_file(file, "r"); | |
212 | if (!pbio) { | |
7e1b7485 | 213 | BIO_printf(bio_err, "Can't open parameter file %s\n", file); |
0f113f3e MC |
214 | return 0; |
215 | } | |
216 | ||
217 | pkey = PEM_read_bio_Parameters(pbio, NULL); | |
218 | BIO_free(pbio); | |
219 | ||
220 | if (!pkey) { | |
221 | BIO_printf(bio_err, "Error reading parameter file %s\n", file); | |
222 | return 0; | |
223 | } | |
224 | ||
225 | ctx = EVP_PKEY_CTX_new(pkey, e); | |
96487cdd | 226 | if (ctx == NULL) |
0f113f3e MC |
227 | goto err; |
228 | if (EVP_PKEY_keygen_init(ctx) <= 0) | |
229 | goto err; | |
230 | EVP_PKEY_free(pkey); | |
231 | *pctx = ctx; | |
232 | return 1; | |
233 | ||
234 | err: | |
7e1b7485 RS |
235 | BIO_puts(bio_err, "Error initializing context\n"); |
236 | ERR_print_errors(bio_err); | |
c5ba2d99 RS |
237 | EVP_PKEY_CTX_free(ctx); |
238 | EVP_PKEY_free(pkey); | |
0f113f3e MC |
239 | return 0; |
240 | ||
241 | } | |
f5cda4cb | 242 | |
7e1b7485 | 243 | int init_gen_str(EVP_PKEY_CTX **pctx, |
0f113f3e MC |
244 | const char *algname, ENGINE *e, int do_param) |
245 | { | |
246 | EVP_PKEY_CTX *ctx = NULL; | |
247 | const EVP_PKEY_ASN1_METHOD *ameth; | |
248 | ENGINE *tmpeng = NULL; | |
249 | int pkey_id; | |
01b8b3c7 | 250 | |
0f113f3e | 251 | if (*pctx) { |
7e1b7485 | 252 | BIO_puts(bio_err, "Algorithm already set!\n"); |
0f113f3e MC |
253 | return 0; |
254 | } | |
b3c6a331 | 255 | |
0f113f3e | 256 | ameth = EVP_PKEY_asn1_find_str(&tmpeng, algname, -1); |
f5cda4cb | 257 | |
70531c14 | 258 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e MC |
259 | if (!ameth && e) |
260 | ameth = ENGINE_get_pkey_asn1_meth_str(e, algname, -1); | |
70531c14 | 261 | #endif |
b3c6a331 | 262 | |
0f113f3e MC |
263 | if (!ameth) { |
264 | BIO_printf(bio_err, "Algorithm %s not found\n", algname); | |
265 | return 0; | |
266 | } | |
f5cda4cb | 267 | |
0f113f3e | 268 | ERR_clear_error(); |
b3c6a331 | 269 | |
0f113f3e | 270 | EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL, ameth); |
01b8b3c7 | 271 | #ifndef OPENSSL_NO_ENGINE |
7c96dbcd | 272 | ENGINE_finish(tmpeng); |
01b8b3c7 | 273 | #endif |
0f113f3e MC |
274 | ctx = EVP_PKEY_CTX_new_id(pkey_id, e); |
275 | ||
276 | if (!ctx) | |
277 | goto err; | |
278 | if (do_param) { | |
279 | if (EVP_PKEY_paramgen_init(ctx) <= 0) | |
280 | goto err; | |
281 | } else { | |
282 | if (EVP_PKEY_keygen_init(ctx) <= 0) | |
283 | goto err; | |
284 | } | |
285 | ||
286 | *pctx = ctx; | |
287 | return 1; | |
288 | ||
289 | err: | |
7e1b7485 RS |
290 | BIO_printf(bio_err, "Error initializing %s context\n", algname); |
291 | ERR_print_errors(bio_err); | |
c5ba2d99 | 292 | EVP_PKEY_CTX_free(ctx); |
0f113f3e MC |
293 | return 0; |
294 | ||
295 | } | |
f5cda4cb DSH |
296 | |
297 | static int genpkey_cb(EVP_PKEY_CTX *ctx) | |
0f113f3e MC |
298 | { |
299 | char c = '*'; | |
300 | BIO *b = EVP_PKEY_CTX_get_app_data(ctx); | |
301 | int p; | |
302 | p = EVP_PKEY_CTX_get_keygen_info(ctx, 0); | |
303 | if (p == 0) | |
304 | c = '.'; | |
305 | if (p == 1) | |
306 | c = '+'; | |
307 | if (p == 2) | |
308 | c = '*'; | |
309 | if (p == 3) | |
310 | c = '\n'; | |
311 | BIO_write(b, &c, 1); | |
312 | (void)BIO_flush(b); | |
313 | return 1; | |
314 | } |