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