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