]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
a28d06f3 | 2 | * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved. |
9e4d0f0b | 3 | * |
dffa7520 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
846e33c7 RS |
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 | |
9e4d0f0b DSH |
8 | */ |
9 | ||
9e4d0f0b | 10 | #include "apps.h" |
dab2cd68 | 11 | #include "progs.h" |
9e4d0f0b DSH |
12 | #include <string.h> |
13 | #include <openssl/err.h> | |
14 | #include <openssl/pem.h> | |
15 | #include <openssl/evp.h> | |
93b1e74c | 16 | #include <sys/stat.h> |
9e4d0f0b | 17 | |
924ec89a | 18 | #define KEY_NONE 0 |
0f113f3e MC |
19 | #define KEY_PRIVKEY 1 |
20 | #define KEY_PUBKEY 2 | |
21 | #define KEY_CERT 3 | |
9e4d0f0b | 22 | |
924ec89a | 23 | static EVP_PKEY_CTX *init_ctx(const char *kdfalg, int *pkeysize, |
0c20802c | 24 | const char *keyfile, int keyform, int key_type, |
9880236e | 25 | char *passinarg, int pkey_op, ENGINE *e, |
ae89578b | 26 | const int impl, int rawin, EVP_PKEY **ppkey, |
6ceaf672 PG |
27 | EVP_MD_CTX *mctx, const char *digestname, |
28 | OSSL_LIB_CTX *libctx, const char *propq); | |
ffb1ac67 | 29 | |
0c20802c VD |
30 | static int setup_peer(EVP_PKEY_CTX *ctx, int peerform, const char *file, |
31 | ENGINE *e); | |
9e4d0f0b | 32 | |
b010b7c4 | 33 | static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op, |
0f113f3e | 34 | unsigned char *out, size_t *poutlen, |
cc696296 | 35 | const unsigned char *in, size_t inlen); |
b010b7c4 | 36 | |
6ceaf672 PG |
37 | static int do_raw_keyop(int pkey_op, EVP_MD_CTX *mctx, |
38 | EVP_PKEY *pkey, BIO *in, | |
ee633ace | 39 | int filesize, unsigned char *sig, int siglen, |
a7cef52f PY |
40 | unsigned char **out, size_t *poutlen); |
41 | ||
7e1b7485 | 42 | typedef enum OPTION_choice { |
b0f96018 | 43 | OPT_COMMON, |
9880236e | 44 | OPT_ENGINE, OPT_ENGINE_IMPL, OPT_IN, OPT_OUT, |
7e1b7485 RS |
45 | OPT_PUBIN, OPT_CERTIN, OPT_ASN1PARSE, OPT_HEXDUMP, OPT_SIGN, |
46 | OPT_VERIFY, OPT_VERIFYRECOVER, OPT_REV, OPT_ENCRYPT, OPT_DECRYPT, | |
47 | OPT_DERIVE, OPT_SIGFILE, OPT_INKEY, OPT_PEERKEY, OPT_PASSIN, | |
9d1bf5f7 | 48 | OPT_PEERFORM, OPT_KEYFORM, OPT_PKEYOPT, OPT_PKEYOPT_PASSIN, OPT_KDF, |
6bd4e3f2 | 49 | OPT_KDFLEN, OPT_R_ENUM, OPT_PROV_ENUM, |
ae89578b | 50 | OPT_CONFIG, |
a7cef52f | 51 | OPT_RAWIN, OPT_DIGEST |
7e1b7485 RS |
52 | } OPTION_CHOICE; |
53 | ||
44c83ebd | 54 | const OPTIONS pkeyutl_options[] = { |
5388f986 | 55 | OPT_SECTION("General"), |
7e1b7485 | 56 | {"help", OPT_HELP, '-', "Display this summary"}, |
5388f986 RS |
57 | #ifndef OPENSSL_NO_ENGINE |
58 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, | |
59 | {"engine_impl", OPT_ENGINE_IMPL, '-', | |
60 | "Also use engine given by -engine for crypto operations"}, | |
61 | #endif | |
a173a7ee | 62 | {"sign", OPT_SIGN, '-', "Sign input data with private key"}, |
7e1b7485 | 63 | {"verify", OPT_VERIFY, '-', "Verify with public key"}, |
a173a7ee RS |
64 | {"encrypt", OPT_ENCRYPT, '-', "Encrypt input data with public key"}, |
65 | {"decrypt", OPT_DECRYPT, '-', "Decrypt input data with private key"}, | |
7e1b7485 | 66 | {"derive", OPT_DERIVE, '-', "Derive shared secret"}, |
ae89578b | 67 | OPT_CONFIG_OPTION, |
5388f986 RS |
68 | |
69 | OPT_SECTION("Input"), | |
70 | {"in", OPT_IN, '<', "Input file - default stdin"}, | |
71 | {"rawin", OPT_RAWIN, '-', "Indicate the input data is in raw form"}, | |
72 | {"pubin", OPT_PUBIN, '-', "Input is a public key"}, | |
a173a7ee | 73 | {"inkey", OPT_INKEY, 's', "Input private key file"}, |
16e1b281 | 74 | {"passin", OPT_PASSIN, 's', "Input file pass phrase source"}, |
5388f986 | 75 | {"peerkey", OPT_PEERKEY, 's', "Peer key file used in key derivation"}, |
6d382c74 | 76 | {"peerform", OPT_PEERFORM, 'E', "Peer key format (DER/PEM/P12/ENGINE)"}, |
5388f986 RS |
77 | {"certin", OPT_CERTIN, '-', "Input is a cert with a public key"}, |
78 | {"rev", OPT_REV, '-', "Reverse the order of the input buffer"}, | |
79 | {"sigfile", OPT_SIGFILE, '<', "Signature file (verify operation only)"}, | |
6d382c74 | 80 | {"keyform", OPT_KEYFORM, 'E', "Private key format (ENGINE, other values ignored)"}, |
5388f986 RS |
81 | |
82 | OPT_SECTION("Output"), | |
83 | {"out", OPT_OUT, '>', "Output file - default stdout"}, | |
84 | {"asn1parse", OPT_ASN1PARSE, '-', "asn1parse the output data"}, | |
85 | {"hexdump", OPT_HEXDUMP, '-', "Hex dump output"}, | |
86 | {"verifyrecover", OPT_VERIFYRECOVER, '-', | |
87 | "Verify with public key, recover original data"}, | |
88 | ||
89 | OPT_SECTION("Signing/Derivation"), | |
90 | {"digest", OPT_DIGEST, 's', | |
91 | "Specify the digest algorithm when signing the raw input data"}, | |
7e1b7485 | 92 | {"pkeyopt", OPT_PKEYOPT, 's', "Public key options as opt:value"}, |
9d1bf5f7 JB |
93 | {"pkeyopt_passin", OPT_PKEYOPT_PASSIN, 's', |
94 | "Public key option that is read as a passphrase argument opt:passphrase"}, | |
5388f986 RS |
95 | {"kdf", OPT_KDF, 's', "Use KDF algorithm"}, |
96 | {"kdflen", OPT_KDFLEN, 'p', "KDF algorithm output length"}, | |
97 | ||
3ee1eac2 | 98 | OPT_R_OPTIONS, |
6bd4e3f2 | 99 | OPT_PROV_OPTIONS, |
7e1b7485 RS |
100 | {NULL} |
101 | }; | |
9e4d0f0b | 102 | |
7e1b7485 | 103 | int pkeyutl_main(int argc, char **argv) |
9e4d0f0b | 104 | { |
ae89578b | 105 | CONF *conf = NULL; |
0f113f3e | 106 | BIO *in = NULL, *out = NULL; |
0f113f3e | 107 | ENGINE *e = NULL; |
0f113f3e | 108 | EVP_PKEY_CTX *ctx = NULL; |
a7cef52f | 109 | EVP_PKEY *pkey = NULL; |
7e1b7485 RS |
110 | char *infile = NULL, *outfile = NULL, *sigfile = NULL, *passinarg = NULL; |
111 | char hexdump = 0, asn1parse = 0, rev = 0, *prog; | |
0f113f3e | 112 | unsigned char *buf_in = NULL, *buf_out = NULL, *sig = NULL; |
7e1b7485 | 113 | OPTION_CHOICE o; |
d382e796 TM |
114 | int buf_inlen = 0, siglen = -1; |
115 | int keyform = FORMAT_UNDEF, peerform = FORMAT_UNDEF; | |
7e1b7485 | 116 | int keysize = -1, pkey_op = EVP_PKEY_OP_SIGN, key_type = KEY_PRIVKEY; |
9880236e | 117 | int engine_impl = 0; |
0f113f3e | 118 | int ret = 1, rv = -1; |
7e1b7485 | 119 | size_t buf_outlen; |
0c20802c VD |
120 | const char *inkey = NULL; |
121 | const char *peerkey = NULL; | |
d0190e11 | 122 | const char *kdfalg = NULL, *digestname = NULL; |
924ec89a | 123 | int kdflen = 0; |
0c20802c | 124 | STACK_OF(OPENSSL_STRING) *pkeyopts = NULL; |
9d1bf5f7 | 125 | STACK_OF(OPENSSL_STRING) *pkeyopts_passin = NULL; |
a7cef52f | 126 | int rawin = 0; |
6ceaf672 | 127 | EVP_MD_CTX *mctx = NULL; |
606a417f | 128 | EVP_MD *md = NULL; |
ee633ace | 129 | int filesize = -1; |
b4250010 | 130 | OSSL_LIB_CTX *libctx = app_get0_libctx(); |
0f113f3e | 131 | |
7e1b7485 RS |
132 | prog = opt_init(argc, argv, pkeyutl_options); |
133 | while ((o = opt_next()) != OPT_EOF) { | |
134 | switch (o) { | |
135 | case OPT_EOF: | |
136 | case OPT_ERR: | |
137 | opthelp: | |
138 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
139 | goto end; | |
140 | case OPT_HELP: | |
141 | opt_help(pkeyutl_options); | |
142 | ret = 0; | |
143 | goto end; | |
144 | case OPT_IN: | |
145 | infile = opt_arg(); | |
146 | break; | |
147 | case OPT_OUT: | |
148 | outfile = opt_arg(); | |
149 | break; | |
150 | case OPT_SIGFILE: | |
151 | sigfile = opt_arg(); | |
152 | break; | |
9880236e M |
153 | case OPT_ENGINE_IMPL: |
154 | engine_impl = 1; | |
155 | break; | |
7e1b7485 | 156 | case OPT_INKEY: |
0c20802c | 157 | inkey = opt_arg(); |
7e1b7485 RS |
158 | break; |
159 | case OPT_PEERKEY: | |
0c20802c | 160 | peerkey = opt_arg(); |
7e1b7485 RS |
161 | break; |
162 | case OPT_PASSIN: | |
163 | passinarg = opt_arg(); | |
164 | break; | |
165 | case OPT_PEERFORM: | |
6d382c74 | 166 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &peerform)) |
7e1b7485 RS |
167 | goto opthelp; |
168 | break; | |
169 | case OPT_KEYFORM: | |
6d382c74 | 170 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform)) |
7e1b7485 RS |
171 | goto opthelp; |
172 | break; | |
3ee1eac2 RS |
173 | case OPT_R_CASES: |
174 | if (!opt_rand(o)) | |
175 | goto end; | |
176 | break; | |
ae89578b SL |
177 | case OPT_CONFIG: |
178 | conf = app_load_config_modules(opt_arg()); | |
179 | if (conf == NULL) | |
180 | goto end; | |
181 | break; | |
6bd4e3f2 P |
182 | case OPT_PROV_CASES: |
183 | if (!opt_provider(o)) | |
184 | goto end; | |
185 | break; | |
7e1b7485 RS |
186 | case OPT_ENGINE: |
187 | e = setup_engine(opt_arg(), 0); | |
188 | break; | |
7e1b7485 | 189 | case OPT_PUBIN: |
0f113f3e | 190 | key_type = KEY_PUBKEY; |
7e1b7485 RS |
191 | break; |
192 | case OPT_CERTIN: | |
0f113f3e | 193 | key_type = KEY_CERT; |
7e1b7485 RS |
194 | break; |
195 | case OPT_ASN1PARSE: | |
0f113f3e | 196 | asn1parse = 1; |
7e1b7485 RS |
197 | break; |
198 | case OPT_HEXDUMP: | |
0f113f3e | 199 | hexdump = 1; |
7e1b7485 RS |
200 | break; |
201 | case OPT_SIGN: | |
0f113f3e | 202 | pkey_op = EVP_PKEY_OP_SIGN; |
7e1b7485 RS |
203 | break; |
204 | case OPT_VERIFY: | |
0f113f3e | 205 | pkey_op = EVP_PKEY_OP_VERIFY; |
7e1b7485 RS |
206 | break; |
207 | case OPT_VERIFYRECOVER: | |
0f113f3e | 208 | pkey_op = EVP_PKEY_OP_VERIFYRECOVER; |
7e1b7485 | 209 | break; |
7e1b7485 | 210 | case OPT_ENCRYPT: |
0f113f3e | 211 | pkey_op = EVP_PKEY_OP_ENCRYPT; |
7e1b7485 RS |
212 | break; |
213 | case OPT_DECRYPT: | |
0f113f3e | 214 | pkey_op = EVP_PKEY_OP_DECRYPT; |
7e1b7485 RS |
215 | break; |
216 | case OPT_DERIVE: | |
0f113f3e | 217 | pkey_op = EVP_PKEY_OP_DERIVE; |
7e1b7485 | 218 | break; |
924ec89a DSH |
219 | case OPT_KDF: |
220 | pkey_op = EVP_PKEY_OP_DERIVE; | |
221 | key_type = KEY_NONE; | |
222 | kdfalg = opt_arg(); | |
223 | break; | |
224 | case OPT_KDFLEN: | |
225 | kdflen = atoi(opt_arg()); | |
226 | break; | |
0c20802c VD |
227 | case OPT_REV: |
228 | rev = 1; | |
229 | break; | |
7e1b7485 | 230 | case OPT_PKEYOPT: |
0c20802c VD |
231 | if ((pkeyopts == NULL && |
232 | (pkeyopts = sk_OPENSSL_STRING_new_null()) == NULL) || | |
78524149 | 233 | sk_OPENSSL_STRING_push(pkeyopts, opt_arg()) == 0) { |
0c20802c | 234 | BIO_puts(bio_err, "out of memory\n"); |
0f113f3e MC |
235 | goto end; |
236 | } | |
7e1b7485 | 237 | break; |
9d1bf5f7 JB |
238 | case OPT_PKEYOPT_PASSIN: |
239 | if ((pkeyopts_passin == NULL && | |
240 | (pkeyopts_passin = sk_OPENSSL_STRING_new_null()) == NULL) || | |
241 | sk_OPENSSL_STRING_push(pkeyopts_passin, opt_arg()) == 0) { | |
242 | BIO_puts(bio_err, "out of memory\n"); | |
243 | goto end; | |
244 | } | |
245 | break; | |
a7cef52f PY |
246 | case OPT_RAWIN: |
247 | rawin = 1; | |
248 | break; | |
249 | case OPT_DIGEST: | |
d0190e11 | 250 | digestname = opt_arg(); |
a7cef52f | 251 | break; |
0f113f3e | 252 | } |
0f113f3e | 253 | } |
021410ea RS |
254 | |
255 | /* No extra arguments. */ | |
7e1b7485 | 256 | argc = opt_num_rest(); |
03358517 KR |
257 | if (argc != 0) |
258 | goto opthelp; | |
0f113f3e | 259 | |
3ad60309 DDO |
260 | if (!app_RAND_load()) |
261 | goto end; | |
51e5df0e | 262 | |
a7cef52f PY |
263 | if (rawin && pkey_op != EVP_PKEY_OP_SIGN && pkey_op != EVP_PKEY_OP_VERIFY) { |
264 | BIO_printf(bio_err, | |
265 | "%s: -rawin can only be used with -sign or -verify\n", | |
266 | prog); | |
267 | goto opthelp; | |
268 | } | |
269 | ||
6ceaf672 | 270 | if (digestname != NULL && !rawin) { |
a7cef52f PY |
271 | BIO_printf(bio_err, |
272 | "%s: -digest can only be used with -rawin\n", | |
273 | prog); | |
274 | goto opthelp; | |
275 | } | |
276 | ||
277 | if (rawin && rev) { | |
278 | BIO_printf(bio_err, "%s: -rev cannot be used with raw input\n", | |
279 | prog); | |
280 | goto opthelp; | |
281 | } | |
282 | ||
924ec89a | 283 | if (kdfalg != NULL) { |
f6add6ac JB |
284 | if (kdflen == 0) { |
285 | BIO_printf(bio_err, | |
286 | "%s: no KDF length given (-kdflen parameter).\n", prog); | |
924ec89a | 287 | goto opthelp; |
f6add6ac JB |
288 | } |
289 | } else if (inkey == NULL) { | |
290 | BIO_printf(bio_err, | |
291 | "%s: no private key given (-inkey parameter).\n", prog); | |
292 | goto opthelp; | |
293 | } else if (peerkey != NULL && pkey_op != EVP_PKEY_OP_DERIVE) { | |
294 | BIO_printf(bio_err, | |
295 | "%s: no peer key given (-peerkey parameter).\n", prog); | |
7e1b7485 | 296 | goto opthelp; |
924ec89a | 297 | } |
6ceaf672 PG |
298 | |
299 | if (rawin) { | |
300 | if ((mctx = EVP_MD_CTX_new()) == NULL) { | |
301 | BIO_printf(bio_err, "Error: out of memory\n"); | |
302 | goto end; | |
303 | } | |
304 | } | |
924ec89a | 305 | ctx = init_ctx(kdfalg, &keysize, inkey, keyform, key_type, |
ae89578b | 306 | passinarg, pkey_op, e, engine_impl, rawin, &pkey, |
6ceaf672 | 307 | mctx, digestname, libctx, app_get0_propq()); |
0c20802c VD |
308 | if (ctx == NULL) { |
309 | BIO_printf(bio_err, "%s: Error initializing context\n", prog); | |
310 | ERR_print_errors(bio_err); | |
311 | goto end; | |
312 | } | |
313 | if (peerkey != NULL && !setup_peer(ctx, peerform, peerkey, e)) { | |
314 | BIO_printf(bio_err, "%s: Error setting up peer key\n", prog); | |
315 | ERR_print_errors(bio_err); | |
316 | goto end; | |
317 | } | |
318 | if (pkeyopts != NULL) { | |
319 | int num = sk_OPENSSL_STRING_num(pkeyopts); | |
320 | int i; | |
321 | ||
322 | for (i = 0; i < num; ++i) { | |
323 | const char *opt = sk_OPENSSL_STRING_value(pkeyopts, i); | |
324 | ||
325 | if (pkey_ctrl_string(ctx, opt) <= 0) { | |
f6add6ac JB |
326 | BIO_printf(bio_err, "%s: Can't set parameter \"%s\":\n", |
327 | prog, opt); | |
0c20802c VD |
328 | ERR_print_errors(bio_err); |
329 | goto end; | |
330 | } | |
331 | } | |
332 | } | |
9d1bf5f7 JB |
333 | if (pkeyopts_passin != NULL) { |
334 | int num = sk_OPENSSL_STRING_num(pkeyopts_passin); | |
335 | int i; | |
336 | ||
337 | for (i = 0; i < num; i++) { | |
338 | char *opt = sk_OPENSSL_STRING_value(pkeyopts_passin, i); | |
339 | char *passin = strchr(opt, ':'); | |
340 | char *passwd; | |
341 | ||
342 | if (passin == NULL) { | |
343 | /* Get password interactively */ | |
344 | char passwd_buf[4096]; | |
ffcdb24b P |
345 | int r; |
346 | ||
9d1bf5f7 | 347 | BIO_snprintf(passwd_buf, sizeof(passwd_buf), "Enter %s: ", opt); |
ffcdb24b P |
348 | r = EVP_read_pw_string(passwd_buf, sizeof(passwd_buf) - 1, |
349 | passwd_buf, 0); | |
350 | if (r < 0) { | |
351 | if (r == -2) | |
352 | BIO_puts(bio_err, "user abort\n"); | |
353 | else | |
354 | BIO_puts(bio_err, "entry failed\n"); | |
355 | goto end; | |
356 | } | |
9d1bf5f7 JB |
357 | passwd = OPENSSL_strdup(passwd_buf); |
358 | if (passwd == NULL) { | |
359 | BIO_puts(bio_err, "out of memory\n"); | |
360 | goto end; | |
361 | } | |
362 | } else { | |
363 | /* Get password as a passin argument: First split option name | |
364 | * and passphrase argument into two strings */ | |
365 | *passin = 0; | |
366 | passin++; | |
367 | if (app_passwd(passin, NULL, &passwd, NULL) == 0) { | |
368 | BIO_printf(bio_err, "failed to get '%s'\n", opt); | |
369 | goto end; | |
370 | } | |
371 | } | |
372 | ||
373 | if (EVP_PKEY_CTX_ctrl_str(ctx, opt, passwd) <= 0) { | |
374 | BIO_printf(bio_err, "%s: Can't set parameter \"%s\":\n", | |
375 | prog, opt); | |
376 | goto end; | |
377 | } | |
378 | OPENSSL_free(passwd); | |
379 | } | |
380 | } | |
0c20802c | 381 | |
2234212c | 382 | if (sigfile != NULL && (pkey_op != EVP_PKEY_OP_VERIFY)) { |
7e1b7485 RS |
383 | BIO_printf(bio_err, |
384 | "%s: Signature file specified for non verify\n", prog); | |
0f113f3e MC |
385 | goto end; |
386 | } | |
387 | ||
2234212c | 388 | if (sigfile == NULL && (pkey_op == EVP_PKEY_OP_VERIFY)) { |
7e1b7485 RS |
389 | BIO_printf(bio_err, |
390 | "%s: No signature file specified for verify\n", prog); | |
0f113f3e MC |
391 | goto end; |
392 | } | |
a9164153 | 393 | |
0f113f3e | 394 | if (pkey_op != EVP_PKEY_OP_DERIVE) { |
bdd58d98 | 395 | in = bio_open_default(infile, 'r', FORMAT_BINARY); |
93b1e74c | 396 | if (infile != NULL) { |
ee633ace MC |
397 | struct stat st; |
398 | ||
399 | if (stat(infile, &st) == 0 && st.st_size <= INT_MAX) | |
400 | filesize = (int)st.st_size; | |
401 | } | |
7e1b7485 | 402 | if (in == NULL) |
0f113f3e | 403 | goto end; |
0f113f3e | 404 | } |
bdd58d98 | 405 | out = bio_open_default(outfile, 'w', FORMAT_BINARY); |
7e1b7485 RS |
406 | if (out == NULL) |
407 | goto end; | |
0f113f3e | 408 | |
2234212c | 409 | if (sigfile != NULL) { |
0f113f3e | 410 | BIO *sigbio = BIO_new_file(sigfile, "rb"); |
2234212c PY |
411 | |
412 | if (sigbio == NULL) { | |
0f113f3e MC |
413 | BIO_printf(bio_err, "Can't open signature file %s\n", sigfile); |
414 | goto end; | |
415 | } | |
416 | siglen = bio_to_mem(&sig, keysize * 10, sigbio); | |
417 | BIO_free(sigbio); | |
0c20802c | 418 | if (siglen < 0) { |
0f113f3e MC |
419 | BIO_printf(bio_err, "Error reading signature data\n"); |
420 | goto end; | |
421 | } | |
422 | } | |
423 | ||
a7cef52f PY |
424 | /* Raw input data is handled elsewhere */ |
425 | if (in != NULL && !rawin) { | |
0f113f3e MC |
426 | /* Read the input data */ |
427 | buf_inlen = bio_to_mem(&buf_in, keysize * 10, in); | |
0c20802c | 428 | if (buf_inlen < 0) { |
0f113f3e | 429 | BIO_printf(bio_err, "Error reading input Data\n"); |
a0abb6a1 | 430 | goto end; |
0f113f3e MC |
431 | } |
432 | if (rev) { | |
433 | size_t i; | |
434 | unsigned char ctmp; | |
435 | size_t l = (size_t)buf_inlen; | |
436 | for (i = 0; i < l / 2; i++) { | |
437 | ctmp = buf_in[i]; | |
438 | buf_in[i] = buf_in[l - 1 - i]; | |
439 | buf_in[l - 1 - i] = ctmp; | |
440 | } | |
441 | } | |
442 | } | |
443 | ||
a7cef52f PY |
444 | /* Sanity check the input if the input is not raw */ |
445 | if (!rawin | |
446 | && buf_inlen > EVP_MAX_MD_SIZE | |
a0abb6a1 | 447 | && (pkey_op == EVP_PKEY_OP_SIGN |
5ffc3324 | 448 | || pkey_op == EVP_PKEY_OP_VERIFY)) { |
a0abb6a1 MC |
449 | BIO_printf(bio_err, |
450 | "Error: The input data looks too long to be a hash\n"); | |
451 | goto end; | |
452 | } | |
453 | ||
0f113f3e | 454 | if (pkey_op == EVP_PKEY_OP_VERIFY) { |
a7cef52f | 455 | if (rawin) { |
6ceaf672 | 456 | rv = do_raw_keyop(pkey_op, mctx, pkey, in, filesize, sig, siglen, |
a7cef52f PY |
457 | NULL, 0); |
458 | } else { | |
459 | rv = EVP_PKEY_verify(ctx, sig, (size_t)siglen, | |
460 | buf_in, (size_t)buf_inlen); | |
461 | } | |
7e1b7485 | 462 | if (rv == 1) { |
0f113f3e | 463 | BIO_puts(out, "Signature Verified Successfully\n"); |
7e1b7485 | 464 | ret = 0; |
2234212c | 465 | } else { |
7e1b7485 | 466 | BIO_puts(out, "Signature Verification Failure\n"); |
2234212c | 467 | } |
7e1b7485 RS |
468 | goto end; |
469 | } | |
924ec89a DSH |
470 | if (kdflen != 0) { |
471 | buf_outlen = kdflen; | |
472 | rv = 1; | |
473 | } else { | |
a7cef52f PY |
474 | if (rawin) { |
475 | /* rawin allocates the buffer in do_raw_keyop() */ | |
6ceaf672 | 476 | rv = do_raw_keyop(pkey_op, mctx, pkey, in, filesize, NULL, 0, |
a7cef52f PY |
477 | &buf_out, (size_t *)&buf_outlen); |
478 | } else { | |
479 | rv = do_keyop(ctx, pkey_op, NULL, (size_t *)&buf_outlen, | |
480 | buf_in, (size_t)buf_inlen); | |
481 | if (rv > 0 && buf_outlen != 0) { | |
482 | buf_out = app_malloc(buf_outlen, "buffer output"); | |
483 | rv = do_keyop(ctx, pkey_op, | |
484 | buf_out, (size_t *)&buf_outlen, | |
485 | buf_in, (size_t)buf_inlen); | |
486 | } | |
487 | } | |
0f113f3e | 488 | } |
78524149 | 489 | if (rv <= 0) { |
f6add6ac JB |
490 | if (pkey_op != EVP_PKEY_OP_DERIVE) { |
491 | BIO_puts(bio_err, "Public Key operation error\n"); | |
492 | } else { | |
493 | BIO_puts(bio_err, "Key derivation failed\n"); | |
494 | } | |
0f113f3e MC |
495 | ERR_print_errors(bio_err); |
496 | goto end; | |
497 | } | |
498 | ret = 0; | |
7e1b7485 | 499 | |
0f113f3e MC |
500 | if (asn1parse) { |
501 | if (!ASN1_parse_dump(out, buf_out, buf_outlen, 1, -1)) | |
502 | ERR_print_errors(bio_err); | |
2234212c | 503 | } else if (hexdump) { |
0f113f3e | 504 | BIO_dump(out, (char *)buf_out, buf_outlen); |
2234212c | 505 | } else { |
0f113f3e | 506 | BIO_write(out, buf_out, buf_outlen); |
2234212c | 507 | } |
0f113f3e MC |
508 | |
509 | end: | |
6ceaf672 | 510 | EVP_MD_CTX_free(mctx); |
c5ba2d99 | 511 | EVP_PKEY_CTX_free(ctx); |
606a417f | 512 | EVP_MD_free(md); |
dd1abd44 | 513 | release_engine(e); |
0f113f3e MC |
514 | BIO_free(in); |
515 | BIO_free_all(out); | |
b548a1f1 RS |
516 | OPENSSL_free(buf_in); |
517 | OPENSSL_free(buf_out); | |
518 | OPENSSL_free(sig); | |
0c20802c | 519 | sk_OPENSSL_STRING_free(pkeyopts); |
9d1bf5f7 | 520 | sk_OPENSSL_STRING_free(pkeyopts_passin); |
ae89578b | 521 | NCONF_free(conf); |
0f113f3e | 522 | return ret; |
9e4d0f0b DSH |
523 | } |
524 | ||
924ec89a | 525 | static EVP_PKEY_CTX *init_ctx(const char *kdfalg, int *pkeysize, |
0c20802c | 526 | const char *keyfile, int keyform, int key_type, |
9880236e | 527 | char *passinarg, int pkey_op, ENGINE *e, |
ee633ace | 528 | const int engine_impl, int rawin, |
6ceaf672 PG |
529 | EVP_PKEY **ppkey, EVP_MD_CTX *mctx, const char *digestname, |
530 | OSSL_LIB_CTX *libctx, const char *propq) | |
0f113f3e MC |
531 | { |
532 | EVP_PKEY *pkey = NULL; | |
533 | EVP_PKEY_CTX *ctx = NULL; | |
9880236e | 534 | ENGINE *impl = NULL; |
0f113f3e MC |
535 | char *passin = NULL; |
536 | int rv = -1; | |
537 | X509 *x; | |
7dc67708 | 538 | |
0f113f3e MC |
539 | if (((pkey_op == EVP_PKEY_OP_SIGN) || (pkey_op == EVP_PKEY_OP_DECRYPT) |
540 | || (pkey_op == EVP_PKEY_OP_DERIVE)) | |
924ec89a | 541 | && (key_type != KEY_PRIVKEY && kdfalg == NULL)) { |
0f113f3e MC |
542 | BIO_printf(bio_err, "A private key is needed for this operation\n"); |
543 | goto end; | |
544 | } | |
7e1b7485 | 545 | if (!app_passwd(passinarg, NULL, &passin, NULL)) { |
0f113f3e MC |
546 | BIO_printf(bio_err, "Error getting password\n"); |
547 | goto end; | |
548 | } | |
549 | switch (key_type) { | |
550 | case KEY_PRIVKEY: | |
50eb2a50 | 551 | pkey = load_key(keyfile, keyform, 0, passin, e, "private key"); |
0f113f3e MC |
552 | break; |
553 | ||
554 | case KEY_PUBKEY: | |
50eb2a50 | 555 | pkey = load_pubkey(keyfile, keyform, 0, NULL, e, "public key"); |
0f113f3e MC |
556 | break; |
557 | ||
558 | case KEY_CERT: | |
d382e796 | 559 | x = load_cert(keyfile, keyform, "Certificate"); |
0f113f3e MC |
560 | if (x) { |
561 | pkey = X509_get_pubkey(x); | |
562 | X509_free(x); | |
563 | } | |
564 | break; | |
565 | ||
924ec89a DSH |
566 | case KEY_NONE: |
567 | break; | |
0f113f3e | 568 | |
924ec89a | 569 | } |
0f113f3e | 570 | |
9880236e M |
571 | #ifndef OPENSSL_NO_ENGINE |
572 | if (engine_impl) | |
573 | impl = e; | |
574 | #endif | |
0f113f3e | 575 | |
2234212c | 576 | if (kdfalg != NULL) { |
924ec89a | 577 | int kdfnid = OBJ_sn2nid(kdfalg); |
b15d5ab6 DSH |
578 | |
579 | if (kdfnid == NID_undef) { | |
580 | kdfnid = OBJ_ln2nid(kdfalg); | |
f6add6ac JB |
581 | if (kdfnid == NID_undef) { |
582 | BIO_printf(bio_err, "The given KDF \"%s\" is unknown.\n", | |
583 | kdfalg); | |
b15d5ab6 | 584 | goto end; |
f6add6ac | 585 | } |
b15d5ab6 | 586 | } |
ae89578b SL |
587 | if (impl != NULL) |
588 | ctx = EVP_PKEY_CTX_new_id(kdfnid, impl); | |
589 | else | |
6ceaf672 | 590 | ctx = EVP_PKEY_CTX_new_from_name(libctx, kdfalg, propq); |
924ec89a DSH |
591 | } else { |
592 | if (pkey == NULL) | |
593 | goto end; | |
ed86f884 | 594 | |
ed576acd | 595 | *pkeysize = EVP_PKEY_get_size(pkey); |
ae89578b SL |
596 | if (impl != NULL) |
597 | ctx = EVP_PKEY_CTX_new(pkey, impl); | |
598 | else | |
6ceaf672 | 599 | ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq); |
a7cef52f PY |
600 | if (ppkey != NULL) |
601 | *ppkey = pkey; | |
924ec89a DSH |
602 | EVP_PKEY_free(pkey); |
603 | } | |
0f113f3e | 604 | |
96487cdd | 605 | if (ctx == NULL) |
0f113f3e MC |
606 | goto end; |
607 | ||
ee633ace | 608 | if (rawin) { |
6ceaf672 PG |
609 | EVP_MD_CTX_set_pkey_ctx(mctx, ctx); |
610 | ||
611 | switch (pkey_op) { | |
612 | case EVP_PKEY_OP_SIGN: | |
ebbf3563 P |
613 | rv = EVP_DigestSignInit_ex(mctx, NULL, digestname, libctx, propq, |
614 | pkey, NULL); | |
6ceaf672 PG |
615 | break; |
616 | ||
617 | case EVP_PKEY_OP_VERIFY: | |
ebbf3563 P |
618 | rv = EVP_DigestVerifyInit_ex(mctx, NULL, digestname, libctx, propq, |
619 | pkey, NULL); | |
6ceaf672 PG |
620 | break; |
621 | } | |
622 | ||
ee633ace MC |
623 | } else { |
624 | switch (pkey_op) { | |
625 | case EVP_PKEY_OP_SIGN: | |
626 | rv = EVP_PKEY_sign_init(ctx); | |
627 | break; | |
0f113f3e | 628 | |
ee633ace MC |
629 | case EVP_PKEY_OP_VERIFY: |
630 | rv = EVP_PKEY_verify_init(ctx); | |
631 | break; | |
0f113f3e | 632 | |
ee633ace MC |
633 | case EVP_PKEY_OP_VERIFYRECOVER: |
634 | rv = EVP_PKEY_verify_recover_init(ctx); | |
635 | break; | |
0f113f3e | 636 | |
ee633ace MC |
637 | case EVP_PKEY_OP_ENCRYPT: |
638 | rv = EVP_PKEY_encrypt_init(ctx); | |
639 | break; | |
0f113f3e | 640 | |
ee633ace MC |
641 | case EVP_PKEY_OP_DECRYPT: |
642 | rv = EVP_PKEY_decrypt_init(ctx); | |
643 | break; | |
0f113f3e | 644 | |
ee633ace MC |
645 | case EVP_PKEY_OP_DERIVE: |
646 | rv = EVP_PKEY_derive_init(ctx); | |
647 | break; | |
648 | } | |
0f113f3e MC |
649 | } |
650 | ||
651 | if (rv <= 0) { | |
652 | EVP_PKEY_CTX_free(ctx); | |
653 | ctx = NULL; | |
654 | } | |
655 | ||
656 | end: | |
b548a1f1 | 657 | OPENSSL_free(passin); |
0f113f3e MC |
658 | return ctx; |
659 | ||
660 | } | |
9e4d0f0b | 661 | |
0c20802c | 662 | static int setup_peer(EVP_PKEY_CTX *ctx, int peerform, const char *file, |
f6add6ac | 663 | ENGINE *e) |
0f113f3e MC |
664 | { |
665 | EVP_PKEY *peer = NULL; | |
f6add6ac | 666 | ENGINE *engine = NULL; |
0f113f3e | 667 | int ret; |
0f113f3e | 668 | |
0c20802c VD |
669 | if (peerform == FORMAT_ENGINE) |
670 | engine = e; | |
50eb2a50 | 671 | peer = load_pubkey(file, peerform, 0, NULL, engine, "peer key"); |
2234212c | 672 | if (peer == NULL) { |
0f113f3e | 673 | BIO_printf(bio_err, "Error reading peer key %s\n", file); |
7e1b7485 | 674 | ERR_print_errors(bio_err); |
0f113f3e MC |
675 | return 0; |
676 | } | |
677 | ||
678 | ret = EVP_PKEY_derive_set_peer(ctx, peer); | |
679 | ||
680 | EVP_PKEY_free(peer); | |
681 | if (ret <= 0) | |
7e1b7485 | 682 | ERR_print_errors(bio_err); |
0f113f3e MC |
683 | return ret; |
684 | } | |
b010b7c4 DSH |
685 | |
686 | static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op, | |
0f113f3e | 687 | unsigned char *out, size_t *poutlen, |
cc696296 | 688 | const unsigned char *in, size_t inlen) |
0f113f3e MC |
689 | { |
690 | int rv = 0; | |
691 | switch (pkey_op) { | |
692 | case EVP_PKEY_OP_VERIFYRECOVER: | |
693 | rv = EVP_PKEY_verify_recover(ctx, out, poutlen, in, inlen); | |
694 | break; | |
695 | ||
696 | case EVP_PKEY_OP_SIGN: | |
697 | rv = EVP_PKEY_sign(ctx, out, poutlen, in, inlen); | |
698 | break; | |
699 | ||
700 | case EVP_PKEY_OP_ENCRYPT: | |
701 | rv = EVP_PKEY_encrypt(ctx, out, poutlen, in, inlen); | |
702 | break; | |
703 | ||
704 | case EVP_PKEY_OP_DECRYPT: | |
705 | rv = EVP_PKEY_decrypt(ctx, out, poutlen, in, inlen); | |
706 | break; | |
707 | ||
708 | case EVP_PKEY_OP_DERIVE: | |
709 | rv = EVP_PKEY_derive(ctx, out, poutlen); | |
710 | break; | |
711 | ||
712 | } | |
713 | return rv; | |
714 | } | |
a7cef52f PY |
715 | |
716 | #define TBUF_MAXSIZE 2048 | |
717 | ||
6ceaf672 PG |
718 | static int do_raw_keyop(int pkey_op, EVP_MD_CTX *mctx, |
719 | EVP_PKEY *pkey, BIO *in, | |
ee633ace | 720 | int filesize, unsigned char *sig, int siglen, |
a7cef52f PY |
721 | unsigned char **out, size_t *poutlen) |
722 | { | |
723 | int rv = 0; | |
a7cef52f | 724 | unsigned char tbuf[TBUF_MAXSIZE]; |
ee633ace MC |
725 | unsigned char *mbuf = NULL; |
726 | int buf_len = 0; | |
a7cef52f | 727 | |
ee633ace | 728 | /* Some algorithms only support oneshot digests */ |
ed576acd TM |
729 | if (EVP_PKEY_get_id(pkey) == EVP_PKEY_ED25519 |
730 | || EVP_PKEY_get_id(pkey) == EVP_PKEY_ED448) { | |
ee633ace MC |
731 | if (filesize < 0) { |
732 | BIO_printf(bio_err, | |
733 | "Error: unable to determine file size for oneshot operation\n"); | |
df09b6b5 | 734 | goto end; |
ee633ace MC |
735 | } |
736 | mbuf = app_malloc(filesize, "oneshot sign/verify buffer"); | |
737 | switch(pkey_op) { | |
738 | case EVP_PKEY_OP_VERIFY: | |
ee633ace MC |
739 | buf_len = BIO_read(in, mbuf, filesize); |
740 | if (buf_len != filesize) { | |
741 | BIO_printf(bio_err, "Error reading raw input data\n"); | |
742 | goto end; | |
743 | } | |
744 | rv = EVP_DigestVerify(mctx, sig, (size_t)siglen, mbuf, buf_len); | |
745 | break; | |
746 | case EVP_PKEY_OP_SIGN: | |
ee633ace MC |
747 | buf_len = BIO_read(in, mbuf, filesize); |
748 | if (buf_len != filesize) { | |
749 | BIO_printf(bio_err, "Error reading raw input data\n"); | |
750 | goto end; | |
751 | } | |
752 | rv = EVP_DigestSign(mctx, NULL, poutlen, mbuf, buf_len); | |
753 | if (rv == 1 && out != NULL) { | |
754 | *out = app_malloc(*poutlen, "buffer output"); | |
755 | rv = EVP_DigestSign(mctx, *out, poutlen, mbuf, buf_len); | |
756 | } | |
757 | break; | |
758 | } | |
ee633ace MC |
759 | goto end; |
760 | } | |
761 | ||
a7cef52f PY |
762 | switch(pkey_op) { |
763 | case EVP_PKEY_OP_VERIFY: | |
a7cef52f | 764 | for (;;) { |
ee633ace MC |
765 | buf_len = BIO_read(in, tbuf, TBUF_MAXSIZE); |
766 | if (buf_len == 0) | |
a7cef52f | 767 | break; |
ee633ace | 768 | if (buf_len < 0) { |
a7cef52f PY |
769 | BIO_printf(bio_err, "Error reading raw input data\n"); |
770 | goto end; | |
771 | } | |
ee633ace | 772 | rv = EVP_DigestVerifyUpdate(mctx, tbuf, (size_t)buf_len); |
a7cef52f PY |
773 | if (rv != 1) { |
774 | BIO_printf(bio_err, "Error verifying raw input data\n"); | |
775 | goto end; | |
776 | } | |
777 | } | |
778 | rv = EVP_DigestVerifyFinal(mctx, sig, (size_t)siglen); | |
779 | break; | |
780 | case EVP_PKEY_OP_SIGN: | |
a7cef52f | 781 | for (;;) { |
ee633ace MC |
782 | buf_len = BIO_read(in, tbuf, TBUF_MAXSIZE); |
783 | if (buf_len == 0) | |
a7cef52f | 784 | break; |
ee633ace | 785 | if (buf_len < 0) { |
a7cef52f PY |
786 | BIO_printf(bio_err, "Error reading raw input data\n"); |
787 | goto end; | |
788 | } | |
ee633ace | 789 | rv = EVP_DigestSignUpdate(mctx, tbuf, (size_t)buf_len); |
a7cef52f PY |
790 | if (rv != 1) { |
791 | BIO_printf(bio_err, "Error signing raw input data\n"); | |
792 | goto end; | |
793 | } | |
794 | } | |
795 | rv = EVP_DigestSignFinal(mctx, NULL, poutlen); | |
796 | if (rv == 1 && out != NULL) { | |
797 | *out = app_malloc(*poutlen, "buffer output"); | |
798 | rv = EVP_DigestSignFinal(mctx, *out, poutlen); | |
799 | } | |
800 | break; | |
801 | } | |
802 | ||
803 | end: | |
df09b6b5 | 804 | OPENSSL_free(mbuf); |
a7cef52f PY |
805 | return rv; |
806 | } |