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