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>
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 OPENSSL_CTX
*libctx
, const char *propq
);
29 static int setup_peer(EVP_PKEY_CTX
*ctx
, int peerform
, const char *file
,
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
);
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
);
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
,
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"},
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"},
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)"},
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"},
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"},
102 int pkeyutl_main(int argc
, char **argv
)
105 BIO
*in
= NULL
, *out
= 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
;
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
;
116 int ret
= 1, rv
= -1;
118 const char *inkey
= NULL
;
119 const char *peerkey
= NULL
;
120 const char *kdfalg
= NULL
;
122 STACK_OF(OPENSSL_STRING
) *pkeyopts
= NULL
;
123 STACK_OF(OPENSSL_STRING
) *pkeyopts_passin
= NULL
;
125 const EVP_MD
*md
= NULL
;
127 OPENSSL_CTX
*libctx
= app_get0_libctx();
128 const char *propq
= NULL
;
130 prog
= opt_init(argc
, argv
, pkeyutl_options
);
131 while ((o
= opt_next()) != OPT_EOF
) {
136 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
139 opt_help(pkeyutl_options
);
151 case OPT_ENGINE_IMPL
:
161 passinarg
= opt_arg();
164 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &peerform
))
168 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &keyform
))
176 conf
= app_load_config_modules(opt_arg());
181 if (!opt_provider(o
))
185 e
= setup_engine(opt_arg(), 0);
188 key_type
= KEY_PUBKEY
;
200 pkey_op
= EVP_PKEY_OP_SIGN
;
203 pkey_op
= EVP_PKEY_OP_VERIFY
;
205 case OPT_VERIFYRECOVER
:
206 pkey_op
= EVP_PKEY_OP_VERIFYRECOVER
;
209 pkey_op
= EVP_PKEY_OP_ENCRYPT
;
212 pkey_op
= EVP_PKEY_OP_DECRYPT
;
215 pkey_op
= EVP_PKEY_OP_DERIVE
;
218 pkey_op
= EVP_PKEY_OP_DERIVE
;
223 kdflen
= atoi(opt_arg());
229 if ((pkeyopts
== NULL
&&
230 (pkeyopts
= sk_OPENSSL_STRING_new_null()) == NULL
) ||
231 sk_OPENSSL_STRING_push(pkeyopts
, opt_arg()) == 0) {
232 BIO_puts(bio_err
, "out of memory\n");
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");
248 if (!opt_md(opt_arg(), &md
))
253 argc
= opt_num_rest();
257 if (rawin
&& pkey_op
!= EVP_PKEY_OP_SIGN
&& pkey_op
!= EVP_PKEY_OP_VERIFY
) {
259 "%s: -rawin can only be used with -sign or -verify\n",
264 if (md
!= NULL
&& !rawin
) {
266 "%s: -digest can only be used with -rawin\n",
272 BIO_printf(bio_err
, "%s: -rev cannot be used with raw input\n",
277 if (kdfalg
!= NULL
) {
280 "%s: no KDF length given (-kdflen parameter).\n", prog
);
283 } else if (inkey
== NULL
) {
285 "%s: no private key given (-inkey parameter).\n", prog
);
287 } else if (peerkey
!= NULL
&& pkey_op
!= EVP_PKEY_OP_DERIVE
) {
289 "%s: no peer key given (-peerkey parameter).\n", prog
);
292 ctx
= init_ctx(kdfalg
, &keysize
, inkey
, keyform
, key_type
,
293 passinarg
, pkey_op
, e
, engine_impl
, rawin
, &pkey
,
296 BIO_printf(bio_err
, "%s: Error initializing context\n", prog
);
297 ERR_print_errors(bio_err
);
300 if (peerkey
!= NULL
&& !setup_peer(ctx
, peerform
, peerkey
, e
)) {
301 BIO_printf(bio_err
, "%s: Error setting up peer key\n", prog
);
302 ERR_print_errors(bio_err
);
305 if (pkeyopts
!= NULL
) {
306 int num
= sk_OPENSSL_STRING_num(pkeyopts
);
309 for (i
= 0; i
< num
; ++i
) {
310 const char *opt
= sk_OPENSSL_STRING_value(pkeyopts
, i
);
312 if (pkey_ctrl_string(ctx
, opt
) <= 0) {
313 BIO_printf(bio_err
, "%s: Can't set parameter \"%s\":\n",
315 ERR_print_errors(bio_err
);
320 if (pkeyopts_passin
!= NULL
) {
321 int num
= sk_OPENSSL_STRING_num(pkeyopts_passin
);
324 for (i
= 0; i
< num
; i
++) {
325 char *opt
= sk_OPENSSL_STRING_value(pkeyopts_passin
, i
);
326 char *passin
= strchr(opt
, ':');
329 if (passin
== NULL
) {
330 /* Get password interactively */
331 char passwd_buf
[4096];
334 BIO_snprintf(passwd_buf
, sizeof(passwd_buf
), "Enter %s: ", opt
);
335 r
= EVP_read_pw_string(passwd_buf
, sizeof(passwd_buf
) - 1,
339 BIO_puts(bio_err
, "user abort\n");
341 BIO_puts(bio_err
, "entry failed\n");
344 passwd
= OPENSSL_strdup(passwd_buf
);
345 if (passwd
== NULL
) {
346 BIO_puts(bio_err
, "out of memory\n");
350 /* Get password as a passin argument: First split option name
351 * and passphrase argument into two strings */
354 if (app_passwd(passin
, NULL
, &passwd
, NULL
) == 0) {
355 BIO_printf(bio_err
, "failed to get '%s'\n", opt
);
360 if (EVP_PKEY_CTX_ctrl_str(ctx
, opt
, passwd
) <= 0) {
361 BIO_printf(bio_err
, "%s: Can't set parameter \"%s\":\n",
365 OPENSSL_free(passwd
);
369 if (sigfile
!= NULL
&& (pkey_op
!= EVP_PKEY_OP_VERIFY
)) {
371 "%s: Signature file specified for non verify\n", prog
);
375 if (sigfile
== NULL
&& (pkey_op
== EVP_PKEY_OP_VERIFY
)) {
377 "%s: No signature file specified for verify\n", prog
);
381 if (pkey_op
!= EVP_PKEY_OP_DERIVE
) {
382 in
= bio_open_default(infile
, 'r', FORMAT_BINARY
);
383 if (infile
!= NULL
) {
386 if (stat(infile
, &st
) == 0 && st
.st_size
<= INT_MAX
)
387 filesize
= (int)st
.st_size
;
392 out
= bio_open_default(outfile
, 'w', FORMAT_BINARY
);
396 if (sigfile
!= NULL
) {
397 BIO
*sigbio
= BIO_new_file(sigfile
, "rb");
399 if (sigbio
== NULL
) {
400 BIO_printf(bio_err
, "Can't open signature file %s\n", sigfile
);
403 siglen
= bio_to_mem(&sig
, keysize
* 10, sigbio
);
406 BIO_printf(bio_err
, "Error reading signature data\n");
411 /* Raw input data is handled elsewhere */
412 if (in
!= NULL
&& !rawin
) {
413 /* Read the input data */
414 buf_inlen
= bio_to_mem(&buf_in
, keysize
* 10, in
);
416 BIO_printf(bio_err
, "Error reading input Data\n");
422 size_t l
= (size_t)buf_inlen
;
423 for (i
= 0; i
< l
/ 2; i
++) {
425 buf_in
[i
] = buf_in
[l
- 1 - i
];
426 buf_in
[l
- 1 - i
] = ctmp
;
431 /* Sanity check the input if the input is not raw */
433 && buf_inlen
> EVP_MAX_MD_SIZE
434 && (pkey_op
== EVP_PKEY_OP_SIGN
435 || pkey_op
== EVP_PKEY_OP_VERIFY
)) {
437 "Error: The input data looks too long to be a hash\n");
441 if (pkey_op
== EVP_PKEY_OP_VERIFY
) {
443 rv
= do_raw_keyop(pkey_op
, ctx
, md
, pkey
, in
, filesize
, sig
, siglen
,
446 rv
= EVP_PKEY_verify(ctx
, sig
, (size_t)siglen
,
447 buf_in
, (size_t)buf_inlen
);
450 BIO_puts(out
, "Signature Verified Successfully\n");
453 BIO_puts(out
, "Signature Verification Failure\n");
462 /* rawin allocates the buffer in do_raw_keyop() */
463 rv
= do_raw_keyop(pkey_op
, ctx
, md
, pkey
, in
, filesize
, NULL
, 0,
464 &buf_out
, (size_t *)&buf_outlen
);
466 rv
= do_keyop(ctx
, pkey_op
, NULL
, (size_t *)&buf_outlen
,
467 buf_in
, (size_t)buf_inlen
);
468 if (rv
> 0 && buf_outlen
!= 0) {
469 buf_out
= app_malloc(buf_outlen
, "buffer output");
470 rv
= do_keyop(ctx
, pkey_op
,
471 buf_out
, (size_t *)&buf_outlen
,
472 buf_in
, (size_t)buf_inlen
);
477 if (pkey_op
!= EVP_PKEY_OP_DERIVE
) {
478 BIO_puts(bio_err
, "Public Key operation error\n");
480 BIO_puts(bio_err
, "Key derivation failed\n");
482 ERR_print_errors(bio_err
);
488 if (!ASN1_parse_dump(out
, buf_out
, buf_outlen
, 1, -1))
489 ERR_print_errors(bio_err
);
490 } else if (hexdump
) {
491 BIO_dump(out
, (char *)buf_out
, buf_outlen
);
493 BIO_write(out
, buf_out
, buf_outlen
);
497 EVP_PKEY_CTX_free(ctx
);
501 OPENSSL_free(buf_in
);
502 OPENSSL_free(buf_out
);
504 sk_OPENSSL_STRING_free(pkeyopts
);
505 sk_OPENSSL_STRING_free(pkeyopts_passin
);
510 static EVP_PKEY_CTX
*init_ctx(const char *kdfalg
, int *pkeysize
,
511 const char *keyfile
, int keyform
, int key_type
,
512 char *passinarg
, int pkey_op
, ENGINE
*e
,
513 const int engine_impl
, int rawin
,
515 OPENSSL_CTX
*libctx
, const char *propq
)
517 EVP_PKEY
*pkey
= NULL
;
518 EVP_PKEY_CTX
*ctx
= NULL
;
523 if (((pkey_op
== EVP_PKEY_OP_SIGN
) || (pkey_op
== EVP_PKEY_OP_DECRYPT
)
524 || (pkey_op
== EVP_PKEY_OP_DERIVE
))
525 && (key_type
!= KEY_PRIVKEY
&& kdfalg
== NULL
)) {
526 BIO_printf(bio_err
, "A private key is needed for this operation\n");
529 if (!app_passwd(passinarg
, NULL
, &passin
, NULL
)) {
530 BIO_printf(bio_err
, "Error getting password\n");
535 pkey
= load_key(keyfile
, keyform
, 0, passin
, e
, "private key");
539 pkey
= load_pubkey(keyfile
, keyform
, 0, NULL
, e
, "public key");
543 x
= load_cert(keyfile
, FORMAT_UNDEF
, "Certificate");
545 pkey
= X509_get_pubkey(x
);
555 #ifndef OPENSSL_NO_ENGINE
560 if (kdfalg
!= NULL
) {
561 int kdfnid
= OBJ_sn2nid(kdfalg
);
563 if (kdfnid
== NID_undef
) {
564 kdfnid
= OBJ_ln2nid(kdfalg
);
565 if (kdfnid
== NID_undef
) {
566 BIO_printf(bio_err
, "The given KDF \"%s\" is unknown.\n",
572 ctx
= EVP_PKEY_CTX_new_id(kdfnid
, impl
);
574 ctx
= EVP_PKEY_CTX_new_from_name(libctx
, kdfalg
, propq
);
579 *pkeysize
= EVP_PKEY_size(pkey
);
581 ctx
= EVP_PKEY_CTX_new(pkey
, impl
);
583 ctx
= EVP_PKEY_CTX_new_from_pkey(libctx
, pkey
, propq
);
593 * If rawin then we don't need to actually initialise the EVP_PKEY_CTX
594 * itself. That will get initialised during EVP_DigestSignInit or
595 * EVP_DigestVerifyInit.
601 case EVP_PKEY_OP_SIGN
:
602 rv
= EVP_PKEY_sign_init(ctx
);
605 case EVP_PKEY_OP_VERIFY
:
606 rv
= EVP_PKEY_verify_init(ctx
);
609 case EVP_PKEY_OP_VERIFYRECOVER
:
610 rv
= EVP_PKEY_verify_recover_init(ctx
);
613 case EVP_PKEY_OP_ENCRYPT
:
614 rv
= EVP_PKEY_encrypt_init(ctx
);
617 case EVP_PKEY_OP_DECRYPT
:
618 rv
= EVP_PKEY_decrypt_init(ctx
);
621 case EVP_PKEY_OP_DERIVE
:
622 rv
= EVP_PKEY_derive_init(ctx
);
628 EVP_PKEY_CTX_free(ctx
);
633 OPENSSL_free(passin
);
638 static int setup_peer(EVP_PKEY_CTX
*ctx
, int peerform
, const char *file
,
641 EVP_PKEY
*peer
= NULL
;
642 ENGINE
*engine
= NULL
;
645 if (peerform
== FORMAT_ENGINE
)
647 peer
= load_pubkey(file
, peerform
, 0, NULL
, engine
, "peer key");
649 BIO_printf(bio_err
, "Error reading peer key %s\n", file
);
650 ERR_print_errors(bio_err
);
654 ret
= EVP_PKEY_derive_set_peer(ctx
, peer
);
658 ERR_print_errors(bio_err
);
662 static int do_keyop(EVP_PKEY_CTX
*ctx
, int pkey_op
,
663 unsigned char *out
, size_t *poutlen
,
664 const unsigned char *in
, size_t inlen
)
668 case EVP_PKEY_OP_VERIFYRECOVER
:
669 rv
= EVP_PKEY_verify_recover(ctx
, out
, poutlen
, in
, inlen
);
672 case EVP_PKEY_OP_SIGN
:
673 rv
= EVP_PKEY_sign(ctx
, out
, poutlen
, in
, inlen
);
676 case EVP_PKEY_OP_ENCRYPT
:
677 rv
= EVP_PKEY_encrypt(ctx
, out
, poutlen
, in
, inlen
);
680 case EVP_PKEY_OP_DECRYPT
:
681 rv
= EVP_PKEY_decrypt(ctx
, out
, poutlen
, in
, inlen
);
684 case EVP_PKEY_OP_DERIVE
:
685 rv
= EVP_PKEY_derive(ctx
, out
, poutlen
);
692 #define TBUF_MAXSIZE 2048
694 static int do_raw_keyop(int pkey_op
, EVP_PKEY_CTX
*ctx
,
695 const EVP_MD
*md
, EVP_PKEY
*pkey
, BIO
*in
,
696 int filesize
, unsigned char *sig
, int siglen
,
697 unsigned char **out
, size_t *poutlen
)
700 EVP_MD_CTX
*mctx
= NULL
;
701 unsigned char tbuf
[TBUF_MAXSIZE
];
702 unsigned char *mbuf
= NULL
;
705 if ((mctx
= EVP_MD_CTX_new()) == NULL
) {
706 BIO_printf(bio_err
, "Error: out of memory\n");
709 EVP_MD_CTX_set_pkey_ctx(mctx
, ctx
);
711 /* Some algorithms only support oneshot digests */
712 if (EVP_PKEY_id(pkey
) == EVP_PKEY_ED25519
713 || EVP_PKEY_id(pkey
) == EVP_PKEY_ED448
) {
716 "Error: unable to determine file size for oneshot operation\n");
719 mbuf
= app_malloc(filesize
, "oneshot sign/verify buffer");
721 case EVP_PKEY_OP_VERIFY
:
722 if (EVP_DigestVerifyInit(mctx
, NULL
, md
, NULL
, pkey
) != 1)
724 buf_len
= BIO_read(in
, mbuf
, filesize
);
725 if (buf_len
!= filesize
) {
726 BIO_printf(bio_err
, "Error reading raw input data\n");
729 rv
= EVP_DigestVerify(mctx
, sig
, (size_t)siglen
, mbuf
, buf_len
);
731 case EVP_PKEY_OP_SIGN
:
732 if (EVP_DigestSignInit(mctx
, NULL
, md
, NULL
, pkey
) != 1)
734 buf_len
= BIO_read(in
, mbuf
, filesize
);
735 if (buf_len
!= filesize
) {
736 BIO_printf(bio_err
, "Error reading raw input data\n");
739 rv
= EVP_DigestSign(mctx
, NULL
, poutlen
, mbuf
, buf_len
);
740 if (rv
== 1 && out
!= NULL
) {
741 *out
= app_malloc(*poutlen
, "buffer output");
742 rv
= EVP_DigestSign(mctx
, *out
, poutlen
, mbuf
, buf_len
);
750 case EVP_PKEY_OP_VERIFY
:
751 if (EVP_DigestVerifyInit(mctx
, NULL
, md
, NULL
, pkey
) != 1)
754 buf_len
= BIO_read(in
, tbuf
, TBUF_MAXSIZE
);
758 BIO_printf(bio_err
, "Error reading raw input data\n");
761 rv
= EVP_DigestVerifyUpdate(mctx
, tbuf
, (size_t)buf_len
);
763 BIO_printf(bio_err
, "Error verifying raw input data\n");
767 rv
= EVP_DigestVerifyFinal(mctx
, sig
, (size_t)siglen
);
769 case EVP_PKEY_OP_SIGN
:
770 if (EVP_DigestSignInit(mctx
, NULL
, md
, NULL
, pkey
) != 1)
773 buf_len
= BIO_read(in
, tbuf
, TBUF_MAXSIZE
);
777 BIO_printf(bio_err
, "Error reading raw input data\n");
780 rv
= EVP_DigestSignUpdate(mctx
, tbuf
, (size_t)buf_len
);
782 BIO_printf(bio_err
, "Error signing raw input data\n");
786 rv
= EVP_DigestSignFinal(mctx
, NULL
, poutlen
);
787 if (rv
== 1 && out
!= NULL
) {
788 *out
= app_malloc(*poutlen
, "buffer output");
789 rv
= EVP_DigestSignFinal(mctx
, *out
, poutlen
);
796 EVP_MD_CTX_free(mctx
);