2 * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
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
13 #include <openssl/err.h>
14 #include <openssl/pem.h>
15 #include <openssl/evp.h>
18 DEFINE_STACK_OF_STRING()
25 static EVP_PKEY_CTX
*init_ctx(const char *kdfalg
, int *pkeysize
,
26 const char *keyfile
, int keyform
, int key_type
,
27 char *passinarg
, int pkey_op
, ENGINE
*e
,
28 const int impl
, int rawin
, EVP_PKEY
**ppkey
,
29 OPENSSL_CTX
*libctx
, const char *propq
);
31 static int setup_peer(EVP_PKEY_CTX
*ctx
, int peerform
, const char *file
,
34 static int do_keyop(EVP_PKEY_CTX
*ctx
, int pkey_op
,
35 unsigned char *out
, size_t *poutlen
,
36 const unsigned char *in
, size_t inlen
);
38 static int do_raw_keyop(int pkey_op
, EVP_PKEY_CTX
*ctx
,
39 const EVP_MD
*md
, EVP_PKEY
*pkey
, BIO
*in
,
40 int filesize
, unsigned char *sig
, int siglen
,
41 unsigned char **out
, size_t *poutlen
);
43 typedef enum OPTION_choice
{
44 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
45 OPT_ENGINE
, OPT_ENGINE_IMPL
, OPT_IN
, OPT_OUT
,
46 OPT_PUBIN
, OPT_CERTIN
, OPT_ASN1PARSE
, OPT_HEXDUMP
, OPT_SIGN
,
47 OPT_VERIFY
, OPT_VERIFYRECOVER
, OPT_REV
, OPT_ENCRYPT
, OPT_DECRYPT
,
48 OPT_DERIVE
, OPT_SIGFILE
, OPT_INKEY
, OPT_PEERKEY
, OPT_PASSIN
,
49 OPT_PEERFORM
, OPT_KEYFORM
, OPT_PKEYOPT
, OPT_PKEYOPT_PASSIN
, OPT_KDF
,
50 OPT_KDFLEN
, OPT_R_ENUM
, OPT_PROV_ENUM
,
55 const OPTIONS pkeyutl_options
[] = {
56 OPT_SECTION("General"),
57 {"help", OPT_HELP
, '-', "Display this summary"},
58 #ifndef OPENSSL_NO_ENGINE
59 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
60 {"engine_impl", OPT_ENGINE_IMPL
, '-',
61 "Also use engine given by -engine for crypto operations"},
63 {"sign", OPT_SIGN
, '-', "Sign input data with private key"},
64 {"verify", OPT_VERIFY
, '-', "Verify with public key"},
65 {"encrypt", OPT_ENCRYPT
, '-', "Encrypt input data with public key"},
66 {"decrypt", OPT_DECRYPT
, '-', "Decrypt input data with private key"},
67 {"derive", OPT_DERIVE
, '-', "Derive shared secret"},
71 {"in", OPT_IN
, '<', "Input file - default stdin"},
72 {"rawin", OPT_RAWIN
, '-', "Indicate the input data is in raw form"},
73 {"pubin", OPT_PUBIN
, '-', "Input is a public key"},
74 {"inkey", OPT_INKEY
, 's', "Input private key file"},
75 {"passin", OPT_PASSIN
, 's', "Input file pass phrase source"},
76 {"peerkey", OPT_PEERKEY
, 's', "Peer key file used in key derivation"},
77 {"peerform", OPT_PEERFORM
, 'E', "Peer key format (DER/PEM/P12/ENGINE)"},
78 {"certin", OPT_CERTIN
, '-', "Input is a cert with a public key"},
79 {"rev", OPT_REV
, '-', "Reverse the order of the input buffer"},
80 {"sigfile", OPT_SIGFILE
, '<', "Signature file (verify operation only)"},
81 {"keyform", OPT_KEYFORM
, 'E', "Private key format (ENGINE, other values ignored)"},
83 OPT_SECTION("Output"),
84 {"out", OPT_OUT
, '>', "Output file - default stdout"},
85 {"asn1parse", OPT_ASN1PARSE
, '-', "asn1parse the output data"},
86 {"hexdump", OPT_HEXDUMP
, '-', "Hex dump output"},
87 {"verifyrecover", OPT_VERIFYRECOVER
, '-',
88 "Verify with public key, recover original data"},
90 OPT_SECTION("Signing/Derivation"),
91 {"digest", OPT_DIGEST
, 's',
92 "Specify the digest algorithm when signing the raw input data"},
93 {"pkeyopt", OPT_PKEYOPT
, 's', "Public key options as opt:value"},
94 {"pkeyopt_passin", OPT_PKEYOPT_PASSIN
, 's',
95 "Public key option that is read as a passphrase argument opt:passphrase"},
96 {"kdf", OPT_KDF
, 's', "Use KDF algorithm"},
97 {"kdflen", OPT_KDFLEN
, 'p', "KDF algorithm output length"},
104 int pkeyutl_main(int argc
, char **argv
)
107 BIO
*in
= NULL
, *out
= NULL
;
109 EVP_PKEY_CTX
*ctx
= NULL
;
110 EVP_PKEY
*pkey
= NULL
;
111 char *infile
= NULL
, *outfile
= NULL
, *sigfile
= NULL
, *passinarg
= NULL
;
112 char hexdump
= 0, asn1parse
= 0, rev
= 0, *prog
;
113 unsigned char *buf_in
= NULL
, *buf_out
= NULL
, *sig
= NULL
;
115 int buf_inlen
= 0, siglen
= -1, keyform
= FORMAT_PEM
, peerform
= FORMAT_PEM
;
116 int keysize
= -1, pkey_op
= EVP_PKEY_OP_SIGN
, key_type
= KEY_PRIVKEY
;
118 int ret
= 1, rv
= -1;
120 const char *inkey
= NULL
;
121 const char *peerkey
= NULL
;
122 const char *kdfalg
= NULL
;
124 STACK_OF(OPENSSL_STRING
) *pkeyopts
= NULL
;
125 STACK_OF(OPENSSL_STRING
) *pkeyopts_passin
= NULL
;
127 const EVP_MD
*md
= NULL
;
129 OPENSSL_CTX
*libctx
= app_get0_libctx();
130 const char *propq
= NULL
;
132 prog
= opt_init(argc
, argv
, pkeyutl_options
);
133 while ((o
= opt_next()) != OPT_EOF
) {
138 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
141 opt_help(pkeyutl_options
);
153 case OPT_ENGINE_IMPL
:
163 passinarg
= opt_arg();
166 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &peerform
))
170 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &keyform
))
178 conf
= app_load_config_modules(opt_arg());
183 if (!opt_provider(o
))
187 e
= setup_engine(opt_arg(), 0);
190 key_type
= KEY_PUBKEY
;
202 pkey_op
= EVP_PKEY_OP_SIGN
;
205 pkey_op
= EVP_PKEY_OP_VERIFY
;
207 case OPT_VERIFYRECOVER
:
208 pkey_op
= EVP_PKEY_OP_VERIFYRECOVER
;
211 pkey_op
= EVP_PKEY_OP_ENCRYPT
;
214 pkey_op
= EVP_PKEY_OP_DECRYPT
;
217 pkey_op
= EVP_PKEY_OP_DERIVE
;
220 pkey_op
= EVP_PKEY_OP_DERIVE
;
225 kdflen
= atoi(opt_arg());
231 if ((pkeyopts
== NULL
&&
232 (pkeyopts
= sk_OPENSSL_STRING_new_null()) == NULL
) ||
233 sk_OPENSSL_STRING_push(pkeyopts
, opt_arg()) == 0) {
234 BIO_puts(bio_err
, "out of memory\n");
238 case OPT_PKEYOPT_PASSIN
:
239 if ((pkeyopts_passin
== NULL
&&
240 (pkeyopts_passin
= sk_OPENSSL_STRING_new_null()) == NULL
) ||
241 sk_OPENSSL_STRING_push(pkeyopts_passin
, opt_arg()) == 0) {
242 BIO_puts(bio_err
, "out of memory\n");
250 if (!opt_md(opt_arg(), &md
))
255 argc
= opt_num_rest();
259 if (rawin
&& pkey_op
!= EVP_PKEY_OP_SIGN
&& pkey_op
!= EVP_PKEY_OP_VERIFY
) {
261 "%s: -rawin can only be used with -sign or -verify\n",
266 if (md
!= NULL
&& !rawin
) {
268 "%s: -digest can only be used with -rawin\n",
274 BIO_printf(bio_err
, "%s: -rev cannot be used with raw input\n",
279 if (kdfalg
!= NULL
) {
282 "%s: no KDF length given (-kdflen parameter).\n", prog
);
285 } else if (inkey
== NULL
) {
287 "%s: no private key given (-inkey parameter).\n", prog
);
289 } else if (peerkey
!= NULL
&& pkey_op
!= EVP_PKEY_OP_DERIVE
) {
291 "%s: no peer key given (-peerkey parameter).\n", prog
);
294 ctx
= init_ctx(kdfalg
, &keysize
, inkey
, keyform
, key_type
,
295 passinarg
, pkey_op
, e
, engine_impl
, rawin
, &pkey
,
298 BIO_printf(bio_err
, "%s: Error initializing context\n", prog
);
299 ERR_print_errors(bio_err
);
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
);
307 if (pkeyopts
!= NULL
) {
308 int num
= sk_OPENSSL_STRING_num(pkeyopts
);
311 for (i
= 0; i
< num
; ++i
) {
312 const char *opt
= sk_OPENSSL_STRING_value(pkeyopts
, i
);
314 if (pkey_ctrl_string(ctx
, opt
) <= 0) {
315 BIO_printf(bio_err
, "%s: Can't set parameter \"%s\":\n",
317 ERR_print_errors(bio_err
);
322 if (pkeyopts_passin
!= NULL
) {
323 int num
= sk_OPENSSL_STRING_num(pkeyopts_passin
);
326 for (i
= 0; i
< num
; i
++) {
327 char *opt
= sk_OPENSSL_STRING_value(pkeyopts_passin
, i
);
328 char *passin
= strchr(opt
, ':');
331 if (passin
== NULL
) {
332 /* Get password interactively */
333 char passwd_buf
[4096];
336 BIO_snprintf(passwd_buf
, sizeof(passwd_buf
), "Enter %s: ", opt
);
337 r
= EVP_read_pw_string(passwd_buf
, sizeof(passwd_buf
) - 1,
341 BIO_puts(bio_err
, "user abort\n");
343 BIO_puts(bio_err
, "entry failed\n");
346 passwd
= OPENSSL_strdup(passwd_buf
);
347 if (passwd
== NULL
) {
348 BIO_puts(bio_err
, "out of memory\n");
352 /* Get password as a passin argument: First split option name
353 * and passphrase argument into two strings */
356 if (app_passwd(passin
, NULL
, &passwd
, NULL
) == 0) {
357 BIO_printf(bio_err
, "failed to get '%s'\n", opt
);
362 if (EVP_PKEY_CTX_ctrl_str(ctx
, opt
, passwd
) <= 0) {
363 BIO_printf(bio_err
, "%s: Can't set parameter \"%s\":\n",
367 OPENSSL_free(passwd
);
371 if (sigfile
!= NULL
&& (pkey_op
!= EVP_PKEY_OP_VERIFY
)) {
373 "%s: Signature file specified for non verify\n", prog
);
377 if (sigfile
== NULL
&& (pkey_op
== EVP_PKEY_OP_VERIFY
)) {
379 "%s: No signature file specified for verify\n", prog
);
383 if (pkey_op
!= EVP_PKEY_OP_DERIVE
) {
384 in
= bio_open_default(infile
, 'r', FORMAT_BINARY
);
385 if (infile
!= NULL
) {
388 if (stat(infile
, &st
) == 0 && st
.st_size
<= INT_MAX
)
389 filesize
= (int)st
.st_size
;
394 out
= bio_open_default(outfile
, 'w', FORMAT_BINARY
);
398 if (sigfile
!= NULL
) {
399 BIO
*sigbio
= BIO_new_file(sigfile
, "rb");
401 if (sigbio
== NULL
) {
402 BIO_printf(bio_err
, "Can't open signature file %s\n", sigfile
);
405 siglen
= bio_to_mem(&sig
, keysize
* 10, sigbio
);
408 BIO_printf(bio_err
, "Error reading signature data\n");
413 /* Raw input data is handled elsewhere */
414 if (in
!= NULL
&& !rawin
) {
415 /* Read the input data */
416 buf_inlen
= bio_to_mem(&buf_in
, keysize
* 10, in
);
418 BIO_printf(bio_err
, "Error reading input Data\n");
424 size_t l
= (size_t)buf_inlen
;
425 for (i
= 0; i
< l
/ 2; i
++) {
427 buf_in
[i
] = buf_in
[l
- 1 - i
];
428 buf_in
[l
- 1 - i
] = ctmp
;
433 /* Sanity check the input if the input is not raw */
435 && buf_inlen
> EVP_MAX_MD_SIZE
436 && (pkey_op
== EVP_PKEY_OP_SIGN
437 || pkey_op
== EVP_PKEY_OP_VERIFY
)) {
439 "Error: The input data looks too long to be a hash\n");
443 if (pkey_op
== EVP_PKEY_OP_VERIFY
) {
445 rv
= do_raw_keyop(pkey_op
, ctx
, md
, pkey
, in
, filesize
, sig
, siglen
,
448 rv
= EVP_PKEY_verify(ctx
, sig
, (size_t)siglen
,
449 buf_in
, (size_t)buf_inlen
);
452 BIO_puts(out
, "Signature Verified Successfully\n");
455 BIO_puts(out
, "Signature Verification Failure\n");
464 /* rawin allocates the buffer in do_raw_keyop() */
465 rv
= do_raw_keyop(pkey_op
, ctx
, md
, pkey
, in
, filesize
, NULL
, 0,
466 &buf_out
, (size_t *)&buf_outlen
);
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
);
479 if (pkey_op
!= EVP_PKEY_OP_DERIVE
) {
480 BIO_puts(bio_err
, "Public Key operation error\n");
482 BIO_puts(bio_err
, "Key derivation failed\n");
484 ERR_print_errors(bio_err
);
490 if (!ASN1_parse_dump(out
, buf_out
, buf_outlen
, 1, -1))
491 ERR_print_errors(bio_err
);
492 } else if (hexdump
) {
493 BIO_dump(out
, (char *)buf_out
, buf_outlen
);
495 BIO_write(out
, buf_out
, buf_outlen
);
499 EVP_PKEY_CTX_free(ctx
);
503 OPENSSL_free(buf_in
);
504 OPENSSL_free(buf_out
);
506 sk_OPENSSL_STRING_free(pkeyopts
);
507 sk_OPENSSL_STRING_free(pkeyopts_passin
);
512 static EVP_PKEY_CTX
*init_ctx(const char *kdfalg
, int *pkeysize
,
513 const char *keyfile
, int keyform
, int key_type
,
514 char *passinarg
, int pkey_op
, ENGINE
*e
,
515 const int engine_impl
, int rawin
,
517 OPENSSL_CTX
*libctx
, const char *propq
)
519 EVP_PKEY
*pkey
= NULL
;
520 EVP_PKEY_CTX
*ctx
= NULL
;
525 if (((pkey_op
== EVP_PKEY_OP_SIGN
) || (pkey_op
== EVP_PKEY_OP_DECRYPT
)
526 || (pkey_op
== EVP_PKEY_OP_DERIVE
))
527 && (key_type
!= KEY_PRIVKEY
&& kdfalg
== NULL
)) {
528 BIO_printf(bio_err
, "A private key is needed for this operation\n");
531 if (!app_passwd(passinarg
, NULL
, &passin
, NULL
)) {
532 BIO_printf(bio_err
, "Error getting password\n");
537 pkey
= load_key(keyfile
, keyform
, 0, passin
, e
, "Private Key");
541 pkey
= load_pubkey(keyfile
, keyform
, 0, NULL
, e
, "Public Key");
545 x
= load_cert(keyfile
, FORMAT_UNDEF
, "Certificate");
547 pkey
= X509_get_pubkey(x
);
557 #ifndef OPENSSL_NO_ENGINE
562 if (kdfalg
!= NULL
) {
563 int kdfnid
= OBJ_sn2nid(kdfalg
);
565 if (kdfnid
== NID_undef
) {
566 kdfnid
= OBJ_ln2nid(kdfalg
);
567 if (kdfnid
== NID_undef
) {
568 BIO_printf(bio_err
, "The given KDF \"%s\" is unknown.\n",
574 ctx
= EVP_PKEY_CTX_new_id(kdfnid
, impl
);
576 ctx
= EVP_PKEY_CTX_new_from_name(libctx
, kdfalg
, propq
);
581 *pkeysize
= EVP_PKEY_size(pkey
);
583 ctx
= EVP_PKEY_CTX_new(pkey
, impl
);
585 ctx
= EVP_PKEY_CTX_new_from_pkey(libctx
, pkey
, propq
);
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.
603 case EVP_PKEY_OP_SIGN
:
604 rv
= EVP_PKEY_sign_init(ctx
);
607 case EVP_PKEY_OP_VERIFY
:
608 rv
= EVP_PKEY_verify_init(ctx
);
611 case EVP_PKEY_OP_VERIFYRECOVER
:
612 rv
= EVP_PKEY_verify_recover_init(ctx
);
615 case EVP_PKEY_OP_ENCRYPT
:
616 rv
= EVP_PKEY_encrypt_init(ctx
);
619 case EVP_PKEY_OP_DECRYPT
:
620 rv
= EVP_PKEY_decrypt_init(ctx
);
623 case EVP_PKEY_OP_DERIVE
:
624 rv
= EVP_PKEY_derive_init(ctx
);
630 EVP_PKEY_CTX_free(ctx
);
635 OPENSSL_free(passin
);
640 static int setup_peer(EVP_PKEY_CTX
*ctx
, int peerform
, const char *file
,
643 EVP_PKEY
*peer
= NULL
;
644 ENGINE
*engine
= NULL
;
647 if (peerform
== FORMAT_ENGINE
)
649 peer
= load_pubkey(file
, peerform
, 0, NULL
, engine
, "Peer Key");
651 BIO_printf(bio_err
, "Error reading peer key %s\n", file
);
652 ERR_print_errors(bio_err
);
656 ret
= EVP_PKEY_derive_set_peer(ctx
, peer
);
660 ERR_print_errors(bio_err
);
664 static int do_keyop(EVP_PKEY_CTX
*ctx
, int pkey_op
,
665 unsigned char *out
, size_t *poutlen
,
666 const unsigned char *in
, size_t inlen
)
670 case EVP_PKEY_OP_VERIFYRECOVER
:
671 rv
= EVP_PKEY_verify_recover(ctx
, out
, poutlen
, in
, inlen
);
674 case EVP_PKEY_OP_SIGN
:
675 rv
= EVP_PKEY_sign(ctx
, out
, poutlen
, in
, inlen
);
678 case EVP_PKEY_OP_ENCRYPT
:
679 rv
= EVP_PKEY_encrypt(ctx
, out
, poutlen
, in
, inlen
);
682 case EVP_PKEY_OP_DECRYPT
:
683 rv
= EVP_PKEY_decrypt(ctx
, out
, poutlen
, in
, inlen
);
686 case EVP_PKEY_OP_DERIVE
:
687 rv
= EVP_PKEY_derive(ctx
, out
, poutlen
);
694 #define TBUF_MAXSIZE 2048
696 static int do_raw_keyop(int pkey_op
, EVP_PKEY_CTX
*ctx
,
697 const EVP_MD
*md
, EVP_PKEY
*pkey
, BIO
*in
,
698 int filesize
, unsigned char *sig
, int siglen
,
699 unsigned char **out
, size_t *poutlen
)
702 EVP_MD_CTX
*mctx
= NULL
;
703 unsigned char tbuf
[TBUF_MAXSIZE
];
704 unsigned char *mbuf
= NULL
;
707 if ((mctx
= EVP_MD_CTX_new()) == NULL
) {
708 BIO_printf(bio_err
, "Error: out of memory\n");
711 EVP_MD_CTX_set_pkey_ctx(mctx
, ctx
);
713 /* Some algorithms only support oneshot digests */
714 if (EVP_PKEY_id(pkey
) == EVP_PKEY_ED25519
715 || EVP_PKEY_id(pkey
) == EVP_PKEY_ED448
) {
718 "Error: unable to determine file size for oneshot operation\n");
721 mbuf
= app_malloc(filesize
, "oneshot sign/verify buffer");
723 case EVP_PKEY_OP_VERIFY
:
724 if (EVP_DigestVerifyInit(mctx
, NULL
, md
, NULL
, pkey
) != 1)
726 buf_len
= BIO_read(in
, mbuf
, filesize
);
727 if (buf_len
!= filesize
) {
728 BIO_printf(bio_err
, "Error reading raw input data\n");
731 rv
= EVP_DigestVerify(mctx
, sig
, (size_t)siglen
, mbuf
, buf_len
);
733 case EVP_PKEY_OP_SIGN
:
734 if (EVP_DigestSignInit(mctx
, NULL
, md
, NULL
, pkey
) != 1)
736 buf_len
= BIO_read(in
, mbuf
, filesize
);
737 if (buf_len
!= filesize
) {
738 BIO_printf(bio_err
, "Error reading raw input data\n");
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
);
752 case EVP_PKEY_OP_VERIFY
:
753 if (EVP_DigestVerifyInit(mctx
, NULL
, md
, NULL
, pkey
) != 1)
756 buf_len
= BIO_read(in
, tbuf
, TBUF_MAXSIZE
);
760 BIO_printf(bio_err
, "Error reading raw input data\n");
763 rv
= EVP_DigestVerifyUpdate(mctx
, tbuf
, (size_t)buf_len
);
765 BIO_printf(bio_err
, "Error verifying raw input data\n");
769 rv
= EVP_DigestVerifyFinal(mctx
, sig
, (size_t)siglen
);
771 case EVP_PKEY_OP_SIGN
:
772 if (EVP_DigestSignInit(mctx
, NULL
, md
, NULL
, pkey
) != 1)
775 buf_len
= BIO_read(in
, tbuf
, TBUF_MAXSIZE
);
779 BIO_printf(bio_err
, "Error reading raw input data\n");
782 rv
= EVP_DigestSignUpdate(mctx
, tbuf
, (size_t)buf_len
);
784 BIO_printf(bio_err
, "Error signing raw input data\n");
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
);
798 EVP_MD_CTX_free(mctx
);