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