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