]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/pkeyutl.c
Check non-option arguments
[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,
b4250010 27 OSSL_LIB_CTX *libctx, const char *propq);
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;
924ec89a
DSH
120 const char *kdfalg = NULL;
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();
ae89578b 128 const char *propq = NULL;
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:
248 if (!opt_md(opt_arg(), &md))
249 goto end;
250 break;
0f113f3e 251 }
0f113f3e 252 }
021410ea
RS
253
254 /* No extra arguments. */
7e1b7485 255 argc = opt_num_rest();
03358517
KR
256 if (argc != 0)
257 goto opthelp;
0f113f3e 258
a7cef52f
PY
259 if (rawin && pkey_op != EVP_PKEY_OP_SIGN && pkey_op != EVP_PKEY_OP_VERIFY) {
260 BIO_printf(bio_err,
261 "%s: -rawin can only be used with -sign or -verify\n",
262 prog);
263 goto opthelp;
264 }
265
266 if (md != NULL && !rawin) {
267 BIO_printf(bio_err,
268 "%s: -digest can only be used with -rawin\n",
269 prog);
270 goto opthelp;
271 }
272
273 if (rawin && rev) {
274 BIO_printf(bio_err, "%s: -rev cannot be used with raw input\n",
275 prog);
276 goto opthelp;
277 }
278
924ec89a 279 if (kdfalg != NULL) {
f6add6ac
JB
280 if (kdflen == 0) {
281 BIO_printf(bio_err,
282 "%s: no KDF length given (-kdflen parameter).\n", prog);
924ec89a 283 goto opthelp;
f6add6ac
JB
284 }
285 } else if (inkey == NULL) {
286 BIO_printf(bio_err,
287 "%s: no private key given (-inkey parameter).\n", prog);
288 goto opthelp;
289 } else if (peerkey != NULL && pkey_op != EVP_PKEY_OP_DERIVE) {
290 BIO_printf(bio_err,
291 "%s: no peer key given (-peerkey parameter).\n", prog);
7e1b7485 292 goto opthelp;
924ec89a
DSH
293 }
294 ctx = init_ctx(kdfalg, &keysize, inkey, keyform, key_type,
ae89578b
SL
295 passinarg, pkey_op, e, engine_impl, rawin, &pkey,
296 libctx, propq);
0c20802c
VD
297 if (ctx == NULL) {
298 BIO_printf(bio_err, "%s: Error initializing context\n", prog);
299 ERR_print_errors(bio_err);
300 goto end;
301 }
302 if (peerkey != NULL && !setup_peer(ctx, peerform, peerkey, e)) {
303 BIO_printf(bio_err, "%s: Error setting up peer key\n", prog);
304 ERR_print_errors(bio_err);
305 goto end;
306 }
307 if (pkeyopts != NULL) {
308 int num = sk_OPENSSL_STRING_num(pkeyopts);
309 int i;
310
311 for (i = 0; i < num; ++i) {
312 const char *opt = sk_OPENSSL_STRING_value(pkeyopts, i);
313
314 if (pkey_ctrl_string(ctx, opt) <= 0) {
f6add6ac
JB
315 BIO_printf(bio_err, "%s: Can't set parameter \"%s\":\n",
316 prog, opt);
0c20802c
VD
317 ERR_print_errors(bio_err);
318 goto end;
319 }
320 }
321 }
9d1bf5f7
JB
322 if (pkeyopts_passin != NULL) {
323 int num = sk_OPENSSL_STRING_num(pkeyopts_passin);
324 int i;
325
326 for (i = 0; i < num; i++) {
327 char *opt = sk_OPENSSL_STRING_value(pkeyopts_passin, i);
328 char *passin = strchr(opt, ':');
329 char *passwd;
330
331 if (passin == NULL) {
332 /* Get password interactively */
333 char passwd_buf[4096];
ffcdb24b
P
334 int r;
335
9d1bf5f7 336 BIO_snprintf(passwd_buf, sizeof(passwd_buf), "Enter %s: ", opt);
ffcdb24b
P
337 r = EVP_read_pw_string(passwd_buf, sizeof(passwd_buf) - 1,
338 passwd_buf, 0);
339 if (r < 0) {
340 if (r == -2)
341 BIO_puts(bio_err, "user abort\n");
342 else
343 BIO_puts(bio_err, "entry failed\n");
344 goto end;
345 }
9d1bf5f7
JB
346 passwd = OPENSSL_strdup(passwd_buf);
347 if (passwd == NULL) {
348 BIO_puts(bio_err, "out of memory\n");
349 goto end;
350 }
351 } else {
352 /* Get password as a passin argument: First split option name
353 * and passphrase argument into two strings */
354 *passin = 0;
355 passin++;
356 if (app_passwd(passin, NULL, &passwd, NULL) == 0) {
357 BIO_printf(bio_err, "failed to get '%s'\n", opt);
358 goto end;
359 }
360 }
361
362 if (EVP_PKEY_CTX_ctrl_str(ctx, opt, passwd) <= 0) {
363 BIO_printf(bio_err, "%s: Can't set parameter \"%s\":\n",
364 prog, opt);
365 goto end;
366 }
367 OPENSSL_free(passwd);
368 }
369 }
0c20802c 370
2234212c 371 if (sigfile != NULL && (pkey_op != EVP_PKEY_OP_VERIFY)) {
7e1b7485
RS
372 BIO_printf(bio_err,
373 "%s: Signature file specified for non verify\n", prog);
0f113f3e
MC
374 goto end;
375 }
376
2234212c 377 if (sigfile == NULL && (pkey_op == EVP_PKEY_OP_VERIFY)) {
7e1b7485
RS
378 BIO_printf(bio_err,
379 "%s: No signature file specified for verify\n", prog);
0f113f3e
MC
380 goto end;
381 }
a9164153 382
0f113f3e 383 if (pkey_op != EVP_PKEY_OP_DERIVE) {
bdd58d98 384 in = bio_open_default(infile, 'r', FORMAT_BINARY);
93b1e74c 385 if (infile != NULL) {
ee633ace
MC
386 struct stat st;
387
388 if (stat(infile, &st) == 0 && st.st_size <= INT_MAX)
389 filesize = (int)st.st_size;
390 }
7e1b7485 391 if (in == NULL)
0f113f3e 392 goto end;
0f113f3e 393 }
bdd58d98 394 out = bio_open_default(outfile, 'w', FORMAT_BINARY);
7e1b7485
RS
395 if (out == NULL)
396 goto end;
0f113f3e 397
2234212c 398 if (sigfile != NULL) {
0f113f3e 399 BIO *sigbio = BIO_new_file(sigfile, "rb");
2234212c
PY
400
401 if (sigbio == NULL) {
0f113f3e
MC
402 BIO_printf(bio_err, "Can't open signature file %s\n", sigfile);
403 goto end;
404 }
405 siglen = bio_to_mem(&sig, keysize * 10, sigbio);
406 BIO_free(sigbio);
0c20802c 407 if (siglen < 0) {
0f113f3e
MC
408 BIO_printf(bio_err, "Error reading signature data\n");
409 goto end;
410 }
411 }
412
a7cef52f
PY
413 /* Raw input data is handled elsewhere */
414 if (in != NULL && !rawin) {
0f113f3e
MC
415 /* Read the input data */
416 buf_inlen = bio_to_mem(&buf_in, keysize * 10, in);
0c20802c 417 if (buf_inlen < 0) {
0f113f3e 418 BIO_printf(bio_err, "Error reading input Data\n");
a0abb6a1 419 goto end;
0f113f3e
MC
420 }
421 if (rev) {
422 size_t i;
423 unsigned char ctmp;
424 size_t l = (size_t)buf_inlen;
425 for (i = 0; i < l / 2; i++) {
426 ctmp = buf_in[i];
427 buf_in[i] = buf_in[l - 1 - i];
428 buf_in[l - 1 - i] = ctmp;
429 }
430 }
431 }
432
a7cef52f
PY
433 /* Sanity check the input if the input is not raw */
434 if (!rawin
435 && buf_inlen > EVP_MAX_MD_SIZE
a0abb6a1 436 && (pkey_op == EVP_PKEY_OP_SIGN
5ffc3324 437 || pkey_op == EVP_PKEY_OP_VERIFY)) {
a0abb6a1
MC
438 BIO_printf(bio_err,
439 "Error: The input data looks too long to be a hash\n");
440 goto end;
441 }
442
0f113f3e 443 if (pkey_op == EVP_PKEY_OP_VERIFY) {
a7cef52f 444 if (rawin) {
ee633ace 445 rv = do_raw_keyop(pkey_op, ctx, md, pkey, in, filesize, sig, siglen,
a7cef52f
PY
446 NULL, 0);
447 } else {
448 rv = EVP_PKEY_verify(ctx, sig, (size_t)siglen,
449 buf_in, (size_t)buf_inlen);
450 }
7e1b7485 451 if (rv == 1) {
0f113f3e 452 BIO_puts(out, "Signature Verified Successfully\n");
7e1b7485 453 ret = 0;
2234212c 454 } else {
7e1b7485 455 BIO_puts(out, "Signature Verification Failure\n");
2234212c 456 }
7e1b7485
RS
457 goto end;
458 }
924ec89a
DSH
459 if (kdflen != 0) {
460 buf_outlen = kdflen;
461 rv = 1;
462 } else {
a7cef52f
PY
463 if (rawin) {
464 /* rawin allocates the buffer in do_raw_keyop() */
ee633ace 465 rv = do_raw_keyop(pkey_op, ctx, md, pkey, in, filesize, NULL, 0,
a7cef52f
PY
466 &buf_out, (size_t *)&buf_outlen);
467 } else {
468 rv = do_keyop(ctx, pkey_op, NULL, (size_t *)&buf_outlen,
469 buf_in, (size_t)buf_inlen);
470 if (rv > 0 && buf_outlen != 0) {
471 buf_out = app_malloc(buf_outlen, "buffer output");
472 rv = do_keyop(ctx, pkey_op,
473 buf_out, (size_t *)&buf_outlen,
474 buf_in, (size_t)buf_inlen);
475 }
476 }
0f113f3e 477 }
78524149 478 if (rv <= 0) {
f6add6ac
JB
479 if (pkey_op != EVP_PKEY_OP_DERIVE) {
480 BIO_puts(bio_err, "Public Key operation error\n");
481 } else {
482 BIO_puts(bio_err, "Key derivation failed\n");
483 }
0f113f3e
MC
484 ERR_print_errors(bio_err);
485 goto end;
486 }
487 ret = 0;
7e1b7485 488
0f113f3e
MC
489 if (asn1parse) {
490 if (!ASN1_parse_dump(out, buf_out, buf_outlen, 1, -1))
491 ERR_print_errors(bio_err);
2234212c 492 } else if (hexdump) {
0f113f3e 493 BIO_dump(out, (char *)buf_out, buf_outlen);
2234212c 494 } else {
0f113f3e 495 BIO_write(out, buf_out, buf_outlen);
2234212c 496 }
0f113f3e
MC
497
498 end:
c5ba2d99 499 EVP_PKEY_CTX_free(ctx);
dd1abd44 500 release_engine(e);
0f113f3e
MC
501 BIO_free(in);
502 BIO_free_all(out);
b548a1f1
RS
503 OPENSSL_free(buf_in);
504 OPENSSL_free(buf_out);
505 OPENSSL_free(sig);
0c20802c 506 sk_OPENSSL_STRING_free(pkeyopts);
9d1bf5f7 507 sk_OPENSSL_STRING_free(pkeyopts_passin);
ae89578b 508 NCONF_free(conf);
0f113f3e 509 return ret;
9e4d0f0b
DSH
510}
511
924ec89a 512static EVP_PKEY_CTX *init_ctx(const char *kdfalg, int *pkeysize,
0c20802c 513 const char *keyfile, int keyform, int key_type,
9880236e 514 char *passinarg, int pkey_op, ENGINE *e,
ee633ace 515 const int engine_impl, int rawin,
ae89578b 516 EVP_PKEY **ppkey,
b4250010 517 OSSL_LIB_CTX *libctx, const char *propq)
0f113f3e
MC
518{
519 EVP_PKEY *pkey = NULL;
520 EVP_PKEY_CTX *ctx = NULL;
9880236e 521 ENGINE *impl = NULL;
0f113f3e
MC
522 char *passin = NULL;
523 int rv = -1;
524 X509 *x;
525 if (((pkey_op == EVP_PKEY_OP_SIGN) || (pkey_op == EVP_PKEY_OP_DECRYPT)
526 || (pkey_op == EVP_PKEY_OP_DERIVE))
924ec89a 527 && (key_type != KEY_PRIVKEY && kdfalg == NULL)) {
0f113f3e
MC
528 BIO_printf(bio_err, "A private key is needed for this operation\n");
529 goto end;
530 }
7e1b7485 531 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
0f113f3e
MC
532 BIO_printf(bio_err, "Error getting password\n");
533 goto end;
534 }
535 switch (key_type) {
536 case KEY_PRIVKEY:
50eb2a50 537 pkey = load_key(keyfile, keyform, 0, passin, e, "private key");
0f113f3e
MC
538 break;
539
540 case KEY_PUBKEY:
50eb2a50 541 pkey = load_pubkey(keyfile, keyform, 0, NULL, e, "public key");
0f113f3e
MC
542 break;
543
544 case KEY_CERT:
22dddfb9 545 x = load_cert(keyfile, "Certificate");
0f113f3e
MC
546 if (x) {
547 pkey = X509_get_pubkey(x);
548 X509_free(x);
549 }
550 break;
551
924ec89a
DSH
552 case KEY_NONE:
553 break;
0f113f3e 554
924ec89a 555 }
0f113f3e 556
9880236e
M
557#ifndef OPENSSL_NO_ENGINE
558 if (engine_impl)
559 impl = e;
560#endif
0f113f3e 561
2234212c 562 if (kdfalg != NULL) {
924ec89a 563 int kdfnid = OBJ_sn2nid(kdfalg);
b15d5ab6
DSH
564
565 if (kdfnid == NID_undef) {
566 kdfnid = OBJ_ln2nid(kdfalg);
f6add6ac
JB
567 if (kdfnid == NID_undef) {
568 BIO_printf(bio_err, "The given KDF \"%s\" is unknown.\n",
569 kdfalg);
b15d5ab6 570 goto end;
f6add6ac 571 }
b15d5ab6 572 }
ae89578b
SL
573 if (impl != NULL)
574 ctx = EVP_PKEY_CTX_new_id(kdfnid, impl);
575 else
576 ctx = EVP_PKEY_CTX_new_from_name(libctx, kdfalg, propq);
924ec89a
DSH
577 } else {
578 if (pkey == NULL)
579 goto end;
ed86f884 580
924ec89a 581 *pkeysize = EVP_PKEY_size(pkey);
ae89578b
SL
582 if (impl != NULL)
583 ctx = EVP_PKEY_CTX_new(pkey, impl);
584 else
585 ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
a7cef52f
PY
586 if (ppkey != NULL)
587 *ppkey = pkey;
924ec89a
DSH
588 EVP_PKEY_free(pkey);
589 }
0f113f3e 590
96487cdd 591 if (ctx == NULL)
0f113f3e
MC
592 goto end;
593
ee633ace
MC
594 /*
595 * If rawin then we don't need to actually initialise the EVP_PKEY_CTX
596 * itself. That will get initialised during EVP_DigestSignInit or
597 * EVP_DigestVerifyInit.
598 */
599 if (rawin) {
600 rv = 1;
601 } else {
602 switch (pkey_op) {
603 case EVP_PKEY_OP_SIGN:
604 rv = EVP_PKEY_sign_init(ctx);
605 break;
0f113f3e 606
ee633ace
MC
607 case EVP_PKEY_OP_VERIFY:
608 rv = EVP_PKEY_verify_init(ctx);
609 break;
0f113f3e 610
ee633ace
MC
611 case EVP_PKEY_OP_VERIFYRECOVER:
612 rv = EVP_PKEY_verify_recover_init(ctx);
613 break;
0f113f3e 614
ee633ace
MC
615 case EVP_PKEY_OP_ENCRYPT:
616 rv = EVP_PKEY_encrypt_init(ctx);
617 break;
0f113f3e 618
ee633ace
MC
619 case EVP_PKEY_OP_DECRYPT:
620 rv = EVP_PKEY_decrypt_init(ctx);
621 break;
0f113f3e 622
ee633ace
MC
623 case EVP_PKEY_OP_DERIVE:
624 rv = EVP_PKEY_derive_init(ctx);
625 break;
626 }
0f113f3e
MC
627 }
628
629 if (rv <= 0) {
630 EVP_PKEY_CTX_free(ctx);
631 ctx = NULL;
632 }
633
634 end:
b548a1f1 635 OPENSSL_free(passin);
0f113f3e
MC
636 return ctx;
637
638}
9e4d0f0b 639
0c20802c 640static int setup_peer(EVP_PKEY_CTX *ctx, int peerform, const char *file,
f6add6ac 641 ENGINE *e)
0f113f3e
MC
642{
643 EVP_PKEY *peer = NULL;
f6add6ac 644 ENGINE *engine = NULL;
0f113f3e 645 int ret;
0f113f3e 646
0c20802c
VD
647 if (peerform == FORMAT_ENGINE)
648 engine = e;
50eb2a50 649 peer = load_pubkey(file, peerform, 0, NULL, engine, "peer key");
2234212c 650 if (peer == NULL) {
0f113f3e 651 BIO_printf(bio_err, "Error reading peer key %s\n", file);
7e1b7485 652 ERR_print_errors(bio_err);
0f113f3e
MC
653 return 0;
654 }
655
656 ret = EVP_PKEY_derive_set_peer(ctx, peer);
657
658 EVP_PKEY_free(peer);
659 if (ret <= 0)
7e1b7485 660 ERR_print_errors(bio_err);
0f113f3e
MC
661 return ret;
662}
b010b7c4
DSH
663
664static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
0f113f3e 665 unsigned char *out, size_t *poutlen,
cc696296 666 const unsigned char *in, size_t inlen)
0f113f3e
MC
667{
668 int rv = 0;
669 switch (pkey_op) {
670 case EVP_PKEY_OP_VERIFYRECOVER:
671 rv = EVP_PKEY_verify_recover(ctx, out, poutlen, in, inlen);
672 break;
673
674 case EVP_PKEY_OP_SIGN:
675 rv = EVP_PKEY_sign(ctx, out, poutlen, in, inlen);
676 break;
677
678 case EVP_PKEY_OP_ENCRYPT:
679 rv = EVP_PKEY_encrypt(ctx, out, poutlen, in, inlen);
680 break;
681
682 case EVP_PKEY_OP_DECRYPT:
683 rv = EVP_PKEY_decrypt(ctx, out, poutlen, in, inlen);
684 break;
685
686 case EVP_PKEY_OP_DERIVE:
687 rv = EVP_PKEY_derive(ctx, out, poutlen);
688 break;
689
690 }
691 return rv;
692}
a7cef52f
PY
693
694#define TBUF_MAXSIZE 2048
695
696static int do_raw_keyop(int pkey_op, EVP_PKEY_CTX *ctx,
697 const EVP_MD *md, EVP_PKEY *pkey, BIO *in,
ee633ace 698 int filesize, unsigned char *sig, int siglen,
a7cef52f
PY
699 unsigned char **out, size_t *poutlen)
700{
701 int rv = 0;
702 EVP_MD_CTX *mctx = NULL;
703 unsigned char tbuf[TBUF_MAXSIZE];
ee633ace
MC
704 unsigned char *mbuf = NULL;
705 int buf_len = 0;
a7cef52f
PY
706
707 if ((mctx = EVP_MD_CTX_new()) == NULL) {
708 BIO_printf(bio_err, "Error: out of memory\n");
709 return rv;
710 }
711 EVP_MD_CTX_set_pkey_ctx(mctx, ctx);
712
ee633ace
MC
713 /* Some algorithms only support oneshot digests */
714 if (EVP_PKEY_id(pkey) == EVP_PKEY_ED25519
715 || EVP_PKEY_id(pkey) == EVP_PKEY_ED448) {
716 if (filesize < 0) {
717 BIO_printf(bio_err,
718 "Error: unable to determine file size for oneshot operation\n");
df09b6b5 719 goto end;
ee633ace
MC
720 }
721 mbuf = app_malloc(filesize, "oneshot sign/verify buffer");
722 switch(pkey_op) {
723 case EVP_PKEY_OP_VERIFY:
724 if (EVP_DigestVerifyInit(mctx, NULL, md, NULL, pkey) != 1)
725 goto end;
726 buf_len = BIO_read(in, mbuf, filesize);
727 if (buf_len != filesize) {
728 BIO_printf(bio_err, "Error reading raw input data\n");
729 goto end;
730 }
731 rv = EVP_DigestVerify(mctx, sig, (size_t)siglen, mbuf, buf_len);
732 break;
733 case EVP_PKEY_OP_SIGN:
734 if (EVP_DigestSignInit(mctx, NULL, md, NULL, pkey) != 1)
735 goto end;
736 buf_len = BIO_read(in, mbuf, filesize);
737 if (buf_len != filesize) {
738 BIO_printf(bio_err, "Error reading raw input data\n");
739 goto end;
740 }
741 rv = EVP_DigestSign(mctx, NULL, poutlen, mbuf, buf_len);
742 if (rv == 1 && out != NULL) {
743 *out = app_malloc(*poutlen, "buffer output");
744 rv = EVP_DigestSign(mctx, *out, poutlen, mbuf, buf_len);
745 }
746 break;
747 }
ee633ace
MC
748 goto end;
749 }
750
a7cef52f
PY
751 switch(pkey_op) {
752 case EVP_PKEY_OP_VERIFY:
753 if (EVP_DigestVerifyInit(mctx, NULL, md, NULL, pkey) != 1)
754 goto end;
755 for (;;) {
ee633ace
MC
756 buf_len = BIO_read(in, tbuf, TBUF_MAXSIZE);
757 if (buf_len == 0)
a7cef52f 758 break;
ee633ace 759 if (buf_len < 0) {
a7cef52f
PY
760 BIO_printf(bio_err, "Error reading raw input data\n");
761 goto end;
762 }
ee633ace 763 rv = EVP_DigestVerifyUpdate(mctx, tbuf, (size_t)buf_len);
a7cef52f
PY
764 if (rv != 1) {
765 BIO_printf(bio_err, "Error verifying raw input data\n");
766 goto end;
767 }
768 }
769 rv = EVP_DigestVerifyFinal(mctx, sig, (size_t)siglen);
770 break;
771 case EVP_PKEY_OP_SIGN:
772 if (EVP_DigestSignInit(mctx, NULL, md, NULL, pkey) != 1)
773 goto end;
774 for (;;) {
ee633ace
MC
775 buf_len = BIO_read(in, tbuf, TBUF_MAXSIZE);
776 if (buf_len == 0)
a7cef52f 777 break;
ee633ace 778 if (buf_len < 0) {
a7cef52f
PY
779 BIO_printf(bio_err, "Error reading raw input data\n");
780 goto end;
781 }
ee633ace 782 rv = EVP_DigestSignUpdate(mctx, tbuf, (size_t)buf_len);
a7cef52f
PY
783 if (rv != 1) {
784 BIO_printf(bio_err, "Error signing raw input data\n");
785 goto end;
786 }
787 }
788 rv = EVP_DigestSignFinal(mctx, NULL, poutlen);
789 if (rv == 1 && out != NULL) {
790 *out = app_malloc(*poutlen, "buffer output");
791 rv = EVP_DigestSignFinal(mctx, *out, poutlen);
792 }
793 break;
794 }
795
796 end:
df09b6b5 797 OPENSSL_free(mbuf);
a7cef52f
PY
798 EVP_MD_CTX_free(mctx);
799 return rv;
800}