2 * Copyright 2006-2018 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
);
28 static int setup_peer(EVP_PKEY_CTX
*ctx
, int peerform
, const char *file
,
31 static int do_keyop(EVP_PKEY_CTX
*ctx
, int pkey_op
,
32 unsigned char *out
, size_t *poutlen
,
33 const unsigned char *in
, size_t inlen
);
35 static int do_raw_keyop(int pkey_op
, EVP_PKEY_CTX
*ctx
,
36 const EVP_MD
*md
, EVP_PKEY
*pkey
, BIO
*in
,
37 int filesize
, unsigned char *sig
, int siglen
,
38 unsigned char **out
, size_t *poutlen
);
40 typedef enum OPTION_choice
{
41 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
42 OPT_ENGINE
, OPT_ENGINE_IMPL
, OPT_IN
, OPT_OUT
,
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
,
46 OPT_PEERFORM
, OPT_KEYFORM
, OPT_PKEYOPT
, OPT_PKEYOPT_PASSIN
, OPT_KDF
,
47 OPT_KDFLEN
, OPT_R_ENUM
,
51 const OPTIONS pkeyutl_options
[] = {
52 {"help", OPT_HELP
, '-', "Display this summary"},
53 {"in", OPT_IN
, '<', "Input file - default stdin"},
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"},
57 {"out", OPT_OUT
, '>', "Output file - default stdout"},
58 {"pubin", OPT_PUBIN
, '-', "Input is a public key"},
59 {"certin", OPT_CERTIN
, '-', "Input is a cert with a public key"},
60 {"asn1parse", OPT_ASN1PARSE
, '-', "asn1parse the output data"},
61 {"hexdump", OPT_HEXDUMP
, '-', "Hex dump output"},
62 {"sign", OPT_SIGN
, '-', "Sign input data with private key"},
63 {"verify", OPT_VERIFY
, '-', "Verify with public key"},
64 {"verifyrecover", OPT_VERIFYRECOVER
, '-',
65 "Verify with public key, recover original data"},
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"},
69 {"derive", OPT_DERIVE
, '-', "Derive shared secret"},
70 {"kdf", OPT_KDF
, 's', "Use KDF algorithm"},
71 {"kdflen", OPT_KDFLEN
, 'p', "KDF algorithm output length"},
72 {"sigfile", OPT_SIGFILE
, '<', "Signature file (verify operation only)"},
73 {"inkey", OPT_INKEY
, 's', "Input private key file"},
74 {"peerkey", OPT_PEERKEY
, 's', "Peer key file used in key derivation"},
75 {"passin", OPT_PASSIN
, 's', "Input file pass phrase source"},
76 {"peerform", OPT_PEERFORM
, 'E', "Peer key format - default PEM"},
77 {"keyform", OPT_KEYFORM
, 'E', "Private key format - default PEM"},
78 {"pkeyopt", OPT_PKEYOPT
, 's', "Public key options as opt:value"},
79 {"pkeyopt_passin", OPT_PKEYOPT_PASSIN
, 's',
80 "Public key option that is read as a passphrase argument opt:passphrase"},
82 #ifndef OPENSSL_NO_ENGINE
83 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
84 {"engine_impl", OPT_ENGINE_IMPL
, '-',
85 "Also use engine given by -engine for crypto operations"},
90 int pkeyutl_main(int argc
, char **argv
)
92 BIO
*in
= NULL
, *out
= NULL
;
94 EVP_PKEY_CTX
*ctx
= NULL
;
95 EVP_PKEY
*pkey
= NULL
;
96 char *infile
= NULL
, *outfile
= NULL
, *sigfile
= NULL
, *passinarg
= NULL
;
97 char hexdump
= 0, asn1parse
= 0, rev
= 0, *prog
;
98 unsigned char *buf_in
= NULL
, *buf_out
= NULL
, *sig
= NULL
;
100 int buf_inlen
= 0, siglen
= -1, keyform
= FORMAT_PEM
, peerform
= FORMAT_PEM
;
101 int keysize
= -1, pkey_op
= EVP_PKEY_OP_SIGN
, key_type
= KEY_PRIVKEY
;
103 int ret
= 1, rv
= -1;
105 const char *inkey
= NULL
;
106 const char *peerkey
= NULL
;
107 const char *kdfalg
= NULL
;
109 STACK_OF(OPENSSL_STRING
) *pkeyopts
= NULL
;
110 STACK_OF(OPENSSL_STRING
) *pkeyopts_passin
= NULL
;
112 const EVP_MD
*md
= NULL
;
115 prog
= opt_init(argc
, argv
, pkeyutl_options
);
116 while ((o
= opt_next()) != OPT_EOF
) {
121 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
124 opt_help(pkeyutl_options
);
136 case OPT_ENGINE_IMPL
:
146 passinarg
= opt_arg();
149 if (!opt_format(opt_arg(), OPT_FMT_PDE
, &peerform
))
153 if (!opt_format(opt_arg(), OPT_FMT_PDE
, &keyform
))
161 e
= setup_engine(opt_arg(), 0);
164 key_type
= KEY_PUBKEY
;
176 pkey_op
= EVP_PKEY_OP_SIGN
;
179 pkey_op
= EVP_PKEY_OP_VERIFY
;
181 case OPT_VERIFYRECOVER
:
182 pkey_op
= EVP_PKEY_OP_VERIFYRECOVER
;
185 pkey_op
= EVP_PKEY_OP_ENCRYPT
;
188 pkey_op
= EVP_PKEY_OP_DECRYPT
;
191 pkey_op
= EVP_PKEY_OP_DERIVE
;
194 pkey_op
= EVP_PKEY_OP_DERIVE
;
199 kdflen
= atoi(opt_arg());
205 if ((pkeyopts
== NULL
&&
206 (pkeyopts
= sk_OPENSSL_STRING_new_null()) == NULL
) ||
207 sk_OPENSSL_STRING_push(pkeyopts
, opt_arg()) == 0) {
208 BIO_puts(bio_err
, "out of memory\n");
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");
224 if (!opt_md(opt_arg(), &md
))
229 argc
= opt_num_rest();
233 if (rawin
&& pkey_op
!= EVP_PKEY_OP_SIGN
&& pkey_op
!= EVP_PKEY_OP_VERIFY
) {
235 "%s: -rawin can only be used with -sign or -verify\n",
240 if (md
!= NULL
&& !rawin
) {
242 "%s: -digest can only be used with -rawin\n",
248 BIO_printf(bio_err
, "%s: -rev cannot be used with raw input\n",
253 if (kdfalg
!= NULL
) {
256 "%s: no KDF length given (-kdflen parameter).\n", prog
);
259 } else if (inkey
== NULL
) {
261 "%s: no private key given (-inkey parameter).\n", prog
);
263 } else if (peerkey
!= NULL
&& pkey_op
!= EVP_PKEY_OP_DERIVE
) {
265 "%s: no peer key given (-peerkey parameter).\n", prog
);
268 ctx
= init_ctx(kdfalg
, &keysize
, inkey
, keyform
, key_type
,
269 passinarg
, pkey_op
, e
, engine_impl
, rawin
, &pkey
);
271 BIO_printf(bio_err
, "%s: Error initializing context\n", prog
);
272 ERR_print_errors(bio_err
);
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
);
280 if (pkeyopts
!= NULL
) {
281 int num
= sk_OPENSSL_STRING_num(pkeyopts
);
284 for (i
= 0; i
< num
; ++i
) {
285 const char *opt
= sk_OPENSSL_STRING_value(pkeyopts
, i
);
287 if (pkey_ctrl_string(ctx
, opt
) <= 0) {
288 BIO_printf(bio_err
, "%s: Can't set parameter \"%s\":\n",
290 ERR_print_errors(bio_err
);
295 if (pkeyopts_passin
!= NULL
) {
296 int num
= sk_OPENSSL_STRING_num(pkeyopts_passin
);
299 for (i
= 0; i
< num
; i
++) {
300 char *opt
= sk_OPENSSL_STRING_value(pkeyopts_passin
, i
);
301 char *passin
= strchr(opt
, ':');
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,
310 passwd
= OPENSSL_strdup(passwd_buf
);
311 if (passwd
== NULL
) {
312 BIO_puts(bio_err
, "out of memory\n");
316 /* Get password as a passin argument: First split option name
317 * and passphrase argument into two strings */
320 if (app_passwd(passin
, NULL
, &passwd
, NULL
) == 0) {
321 BIO_printf(bio_err
, "failed to get '%s'\n", opt
);
326 if (EVP_PKEY_CTX_ctrl_str(ctx
, opt
, passwd
) <= 0) {
327 BIO_printf(bio_err
, "%s: Can't set parameter \"%s\":\n",
331 OPENSSL_free(passwd
);
335 if (sigfile
!= NULL
&& (pkey_op
!= EVP_PKEY_OP_VERIFY
)) {
337 "%s: Signature file specified for non verify\n", prog
);
341 if (sigfile
== NULL
&& (pkey_op
== EVP_PKEY_OP_VERIFY
)) {
343 "%s: No signature file specified for verify\n", prog
);
347 if (pkey_op
!= EVP_PKEY_OP_DERIVE
) {
348 in
= bio_open_default(infile
, 'r', FORMAT_BINARY
);
349 if (infile
!= NULL
) {
352 if (stat(infile
, &st
) == 0 && st
.st_size
<= INT_MAX
)
353 filesize
= (int)st
.st_size
;
358 out
= bio_open_default(outfile
, 'w', FORMAT_BINARY
);
362 if (sigfile
!= NULL
) {
363 BIO
*sigbio
= BIO_new_file(sigfile
, "rb");
365 if (sigbio
== NULL
) {
366 BIO_printf(bio_err
, "Can't open signature file %s\n", sigfile
);
369 siglen
= bio_to_mem(&sig
, keysize
* 10, sigbio
);
372 BIO_printf(bio_err
, "Error reading signature data\n");
377 /* Raw input data is handled elsewhere */
378 if (in
!= NULL
&& !rawin
) {
379 /* Read the input data */
380 buf_inlen
= bio_to_mem(&buf_in
, keysize
* 10, in
);
382 BIO_printf(bio_err
, "Error reading input Data\n");
388 size_t l
= (size_t)buf_inlen
;
389 for (i
= 0; i
< l
/ 2; i
++) {
391 buf_in
[i
] = buf_in
[l
- 1 - i
];
392 buf_in
[l
- 1 - i
] = ctmp
;
397 /* Sanity check the input if the input is not raw */
399 && buf_inlen
> EVP_MAX_MD_SIZE
400 && (pkey_op
== EVP_PKEY_OP_SIGN
401 || pkey_op
== EVP_PKEY_OP_VERIFY
402 || pkey_op
== EVP_PKEY_OP_VERIFYRECOVER
)) {
404 "Error: The input data looks too long to be a hash\n");
408 if (pkey_op
== EVP_PKEY_OP_VERIFY
) {
410 rv
= do_raw_keyop(pkey_op
, ctx
, md
, pkey
, in
, filesize
, sig
, siglen
,
413 rv
= EVP_PKEY_verify(ctx
, sig
, (size_t)siglen
,
414 buf_in
, (size_t)buf_inlen
);
417 BIO_puts(out
, "Signature Verified Successfully\n");
420 BIO_puts(out
, "Signature Verification Failure\n");
429 /* rawin allocates the buffer in do_raw_keyop() */
430 rv
= do_raw_keyop(pkey_op
, ctx
, md
, pkey
, in
, filesize
, NULL
, 0,
431 &buf_out
, (size_t *)&buf_outlen
);
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
);
444 if (pkey_op
!= EVP_PKEY_OP_DERIVE
) {
445 BIO_puts(bio_err
, "Public Key operation error\n");
447 BIO_puts(bio_err
, "Key derivation failed\n");
449 ERR_print_errors(bio_err
);
455 if (!ASN1_parse_dump(out
, buf_out
, buf_outlen
, 1, -1))
456 ERR_print_errors(bio_err
);
457 } else if (hexdump
) {
458 BIO_dump(out
, (char *)buf_out
, buf_outlen
);
460 BIO_write(out
, buf_out
, buf_outlen
);
464 EVP_PKEY_CTX_free(ctx
);
468 OPENSSL_free(buf_in
);
469 OPENSSL_free(buf_out
);
471 sk_OPENSSL_STRING_free(pkeyopts
);
472 sk_OPENSSL_STRING_free(pkeyopts_passin
);
476 static EVP_PKEY_CTX
*init_ctx(const char *kdfalg
, int *pkeysize
,
477 const char *keyfile
, int keyform
, int key_type
,
478 char *passinarg
, int pkey_op
, ENGINE
*e
,
479 const int engine_impl
, int rawin
,
482 EVP_PKEY
*pkey
= NULL
;
483 EVP_PKEY_CTX
*ctx
= NULL
;
488 if (((pkey_op
== EVP_PKEY_OP_SIGN
) || (pkey_op
== EVP_PKEY_OP_DECRYPT
)
489 || (pkey_op
== EVP_PKEY_OP_DERIVE
))
490 && (key_type
!= KEY_PRIVKEY
&& kdfalg
== NULL
)) {
491 BIO_printf(bio_err
, "A private key is needed for this operation\n");
494 if (!app_passwd(passinarg
, NULL
, &passin
, NULL
)) {
495 BIO_printf(bio_err
, "Error getting password\n");
500 pkey
= load_key(keyfile
, keyform
, 0, passin
, e
, "Private Key");
504 pkey
= load_pubkey(keyfile
, keyform
, 0, NULL
, e
, "Public Key");
508 x
= load_cert(keyfile
, keyform
, "Certificate");
510 pkey
= X509_get_pubkey(x
);
520 #ifndef OPENSSL_NO_ENGINE
525 if (kdfalg
!= NULL
) {
526 int kdfnid
= OBJ_sn2nid(kdfalg
);
528 if (kdfnid
== NID_undef
) {
529 kdfnid
= OBJ_ln2nid(kdfalg
);
530 if (kdfnid
== NID_undef
) {
531 BIO_printf(bio_err
, "The given KDF \"%s\" is unknown.\n",
536 ctx
= EVP_PKEY_CTX_new_id(kdfnid
, impl
);
541 #ifndef OPENSSL_NO_EC
542 /* SM2 needs a special treatment */
543 if (EVP_PKEY_id(pkey
) == EVP_PKEY_EC
) {
544 EC_KEY
*eckey
= NULL
;
545 const EC_GROUP
*group
= NULL
;
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)
553 EVP_PKEY_set_alias_type(pkey
, EVP_PKEY_SM2
);
556 *pkeysize
= EVP_PKEY_size(pkey
);
557 ctx
= EVP_PKEY_CTX_new(pkey
, impl
);
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.
575 case EVP_PKEY_OP_SIGN
:
576 rv
= EVP_PKEY_sign_init(ctx
);
579 case EVP_PKEY_OP_VERIFY
:
580 rv
= EVP_PKEY_verify_init(ctx
);
583 case EVP_PKEY_OP_VERIFYRECOVER
:
584 rv
= EVP_PKEY_verify_recover_init(ctx
);
587 case EVP_PKEY_OP_ENCRYPT
:
588 rv
= EVP_PKEY_encrypt_init(ctx
);
591 case EVP_PKEY_OP_DECRYPT
:
592 rv
= EVP_PKEY_decrypt_init(ctx
);
595 case EVP_PKEY_OP_DERIVE
:
596 rv
= EVP_PKEY_derive_init(ctx
);
602 EVP_PKEY_CTX_free(ctx
);
607 OPENSSL_free(passin
);
612 static int setup_peer(EVP_PKEY_CTX
*ctx
, int peerform
, const char *file
,
615 EVP_PKEY
*peer
= NULL
;
616 ENGINE
*engine
= NULL
;
619 if (peerform
== FORMAT_ENGINE
)
621 peer
= load_pubkey(file
, peerform
, 0, NULL
, engine
, "Peer Key");
623 BIO_printf(bio_err
, "Error reading peer key %s\n", file
);
624 ERR_print_errors(bio_err
);
628 ret
= EVP_PKEY_derive_set_peer(ctx
, peer
);
632 ERR_print_errors(bio_err
);
636 static int do_keyop(EVP_PKEY_CTX
*ctx
, int pkey_op
,
637 unsigned char *out
, size_t *poutlen
,
638 const unsigned char *in
, size_t inlen
)
642 case EVP_PKEY_OP_VERIFYRECOVER
:
643 rv
= EVP_PKEY_verify_recover(ctx
, out
, poutlen
, in
, inlen
);
646 case EVP_PKEY_OP_SIGN
:
647 rv
= EVP_PKEY_sign(ctx
, out
, poutlen
, in
, inlen
);
650 case EVP_PKEY_OP_ENCRYPT
:
651 rv
= EVP_PKEY_encrypt(ctx
, out
, poutlen
, in
, inlen
);
654 case EVP_PKEY_OP_DECRYPT
:
655 rv
= EVP_PKEY_decrypt(ctx
, out
, poutlen
, in
, inlen
);
658 case EVP_PKEY_OP_DERIVE
:
659 rv
= EVP_PKEY_derive(ctx
, out
, poutlen
);
666 #define TBUF_MAXSIZE 2048
668 static int do_raw_keyop(int pkey_op
, EVP_PKEY_CTX
*ctx
,
669 const EVP_MD
*md
, EVP_PKEY
*pkey
, BIO
*in
,
670 int filesize
, unsigned char *sig
, int siglen
,
671 unsigned char **out
, size_t *poutlen
)
674 EVP_MD_CTX
*mctx
= NULL
;
675 unsigned char tbuf
[TBUF_MAXSIZE
];
676 unsigned char *mbuf
= NULL
;
679 if ((mctx
= EVP_MD_CTX_new()) == NULL
) {
680 BIO_printf(bio_err
, "Error: out of memory\n");
683 EVP_MD_CTX_set_pkey_ctx(mctx
, ctx
);
685 /* Some algorithms only support oneshot digests */
686 if (EVP_PKEY_id(pkey
) == EVP_PKEY_ED25519
687 || EVP_PKEY_id(pkey
) == EVP_PKEY_ED448
) {
690 "Error: unable to determine file size for oneshot operation\n");
693 mbuf
= app_malloc(filesize
, "oneshot sign/verify buffer");
695 case EVP_PKEY_OP_VERIFY
:
696 if (EVP_DigestVerifyInit(mctx
, NULL
, md
, NULL
, pkey
) != 1)
698 buf_len
= BIO_read(in
, mbuf
, filesize
);
699 if (buf_len
!= filesize
) {
700 BIO_printf(bio_err
, "Error reading raw input data\n");
703 rv
= EVP_DigestVerify(mctx
, sig
, (size_t)siglen
, mbuf
, buf_len
);
705 case EVP_PKEY_OP_SIGN
:
706 if (EVP_DigestSignInit(mctx
, NULL
, md
, NULL
, pkey
) != 1)
708 buf_len
= BIO_read(in
, mbuf
, filesize
);
709 if (buf_len
!= filesize
) {
710 BIO_printf(bio_err
, "Error reading raw input data\n");
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
);
724 case EVP_PKEY_OP_VERIFY
:
725 if (EVP_DigestVerifyInit(mctx
, NULL
, md
, NULL
, pkey
) != 1)
728 buf_len
= BIO_read(in
, tbuf
, TBUF_MAXSIZE
);
732 BIO_printf(bio_err
, "Error reading raw input data\n");
735 rv
= EVP_DigestVerifyUpdate(mctx
, tbuf
, (size_t)buf_len
);
737 BIO_printf(bio_err
, "Error verifying raw input data\n");
741 rv
= EVP_DigestVerifyFinal(mctx
, sig
, (size_t)siglen
);
743 case EVP_PKEY_OP_SIGN
:
744 if (EVP_DigestSignInit(mctx
, NULL
, md
, NULL
, pkey
) != 1)
747 buf_len
= BIO_read(in
, tbuf
, TBUF_MAXSIZE
);
751 BIO_printf(bio_err
, "Error reading raw input data\n");
754 rv
= EVP_DigestSignUpdate(mctx
, tbuf
, (size_t)buf_len
);
756 BIO_printf(bio_err
, "Error signing raw input data\n");
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
);
770 EVP_MD_CTX_free(mctx
);