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>
16 #ifndef OPENSSL_NO_POSIX_IO
17 # include <sys/stat.h>
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
);
30 static int setup_peer(EVP_PKEY_CTX
*ctx
, int peerform
, const char *file
,
33 static int do_keyop(EVP_PKEY_CTX
*ctx
, int pkey_op
,
34 unsigned char *out
, size_t *poutlen
,
35 const unsigned char *in
, size_t inlen
);
37 static int do_raw_keyop(int pkey_op
, EVP_PKEY_CTX
*ctx
,
38 const EVP_MD
*md
, EVP_PKEY
*pkey
, BIO
*in
,
39 int filesize
, unsigned char *sig
, int siglen
,
40 unsigned char **out
, size_t *poutlen
);
42 typedef enum OPTION_choice
{
43 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
44 OPT_ENGINE
, OPT_ENGINE_IMPL
, OPT_IN
, OPT_OUT
,
45 OPT_PUBIN
, OPT_CERTIN
, OPT_ASN1PARSE
, OPT_HEXDUMP
, OPT_SIGN
,
46 OPT_VERIFY
, OPT_VERIFYRECOVER
, OPT_REV
, OPT_ENCRYPT
, OPT_DECRYPT
,
47 OPT_DERIVE
, OPT_SIGFILE
, OPT_INKEY
, OPT_PEERKEY
, OPT_PASSIN
,
48 OPT_PEERFORM
, OPT_KEYFORM
, OPT_PKEYOPT
, OPT_PKEYOPT_PASSIN
, OPT_KDF
,
49 OPT_KDFLEN
, OPT_R_ENUM
,
53 const OPTIONS pkeyutl_options
[] = {
54 {"help", OPT_HELP
, '-', "Display this summary"},
55 {"in", OPT_IN
, '<', "Input file - default stdin"},
56 {"rawin", OPT_RAWIN
, '-', "Indicate the input data is in raw form"},
57 {"digest", OPT_DIGEST
, 's',
58 "Specify the digest algorithm when signing the raw input data"},
59 {"out", OPT_OUT
, '>', "Output file - default stdout"},
60 {"pubin", OPT_PUBIN
, '-', "Input is a public key"},
61 {"certin", OPT_CERTIN
, '-', "Input is a cert with a public key"},
62 {"asn1parse", OPT_ASN1PARSE
, '-', "asn1parse the output data"},
63 {"hexdump", OPT_HEXDUMP
, '-', "Hex dump output"},
64 {"sign", OPT_SIGN
, '-', "Sign input data with private key"},
65 {"verify", OPT_VERIFY
, '-', "Verify with public key"},
66 {"verifyrecover", OPT_VERIFYRECOVER
, '-',
67 "Verify with public key, recover original data"},
68 {"rev", OPT_REV
, '-', "Reverse the order of the input buffer"},
69 {"encrypt", OPT_ENCRYPT
, '-', "Encrypt input data with public key"},
70 {"decrypt", OPT_DECRYPT
, '-', "Decrypt input data with private key"},
71 {"derive", OPT_DERIVE
, '-', "Derive shared secret"},
72 {"kdf", OPT_KDF
, 's', "Use KDF algorithm"},
73 {"kdflen", OPT_KDFLEN
, 'p', "KDF algorithm output length"},
74 {"sigfile", OPT_SIGFILE
, '<', "Signature file (verify operation only)"},
75 {"inkey", OPT_INKEY
, 's', "Input private key file"},
76 {"peerkey", OPT_PEERKEY
, 's', "Peer key file used in key derivation"},
77 {"passin", OPT_PASSIN
, 's', "Input file pass phrase source"},
78 {"peerform", OPT_PEERFORM
, 'E', "Peer key format - default PEM"},
79 {"keyform", OPT_KEYFORM
, 'E', "Private key format - default PEM"},
80 {"pkeyopt", OPT_PKEYOPT
, 's', "Public key options as opt:value"},
81 {"pkeyopt_passin", OPT_PKEYOPT_PASSIN
, 's',
82 "Public key option that is read as a passphrase argument opt:passphrase"},
84 #ifndef OPENSSL_NO_ENGINE
85 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
86 {"engine_impl", OPT_ENGINE_IMPL
, '-',
87 "Also use engine given by -engine for crypto operations"},
92 int pkeyutl_main(int argc
, char **argv
)
94 BIO
*in
= NULL
, *out
= NULL
;
96 EVP_PKEY_CTX
*ctx
= NULL
;
97 EVP_PKEY
*pkey
= NULL
;
98 char *infile
= NULL
, *outfile
= NULL
, *sigfile
= NULL
, *passinarg
= NULL
;
99 char hexdump
= 0, asn1parse
= 0, rev
= 0, *prog
;
100 unsigned char *buf_in
= NULL
, *buf_out
= NULL
, *sig
= NULL
;
102 int buf_inlen
= 0, siglen
= -1, keyform
= FORMAT_PEM
, peerform
= FORMAT_PEM
;
103 int keysize
= -1, pkey_op
= EVP_PKEY_OP_SIGN
, key_type
= KEY_PRIVKEY
;
105 int ret
= 1, rv
= -1;
107 const char *inkey
= NULL
;
108 const char *peerkey
= NULL
;
109 const char *kdfalg
= NULL
;
111 STACK_OF(OPENSSL_STRING
) *pkeyopts
= NULL
;
112 STACK_OF(OPENSSL_STRING
) *pkeyopts_passin
= NULL
;
114 const EVP_MD
*md
= NULL
;
117 prog
= opt_init(argc
, argv
, pkeyutl_options
);
118 while ((o
= opt_next()) != OPT_EOF
) {
123 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
126 opt_help(pkeyutl_options
);
138 case OPT_ENGINE_IMPL
:
148 passinarg
= opt_arg();
151 if (!opt_format(opt_arg(), OPT_FMT_PDE
, &peerform
))
155 if (!opt_format(opt_arg(), OPT_FMT_PDE
, &keyform
))
163 e
= setup_engine(opt_arg(), 0);
166 key_type
= KEY_PUBKEY
;
178 pkey_op
= EVP_PKEY_OP_SIGN
;
181 pkey_op
= EVP_PKEY_OP_VERIFY
;
183 case OPT_VERIFYRECOVER
:
184 pkey_op
= EVP_PKEY_OP_VERIFYRECOVER
;
187 pkey_op
= EVP_PKEY_OP_ENCRYPT
;
190 pkey_op
= EVP_PKEY_OP_DECRYPT
;
193 pkey_op
= EVP_PKEY_OP_DERIVE
;
196 pkey_op
= EVP_PKEY_OP_DERIVE
;
201 kdflen
= atoi(opt_arg());
207 if ((pkeyopts
== NULL
&&
208 (pkeyopts
= sk_OPENSSL_STRING_new_null()) == NULL
) ||
209 sk_OPENSSL_STRING_push(pkeyopts
, opt_arg()) == 0) {
210 BIO_puts(bio_err
, "out of memory\n");
214 case OPT_PKEYOPT_PASSIN
:
215 if ((pkeyopts_passin
== NULL
&&
216 (pkeyopts_passin
= sk_OPENSSL_STRING_new_null()) == NULL
) ||
217 sk_OPENSSL_STRING_push(pkeyopts_passin
, opt_arg()) == 0) {
218 BIO_puts(bio_err
, "out of memory\n");
226 if (!opt_md(opt_arg(), &md
))
231 argc
= opt_num_rest();
235 if (rawin
&& pkey_op
!= EVP_PKEY_OP_SIGN
&& pkey_op
!= EVP_PKEY_OP_VERIFY
) {
237 "%s: -rawin can only be used with -sign or -verify\n",
242 if (md
!= NULL
&& !rawin
) {
244 "%s: -digest can only be used with -rawin\n",
250 BIO_printf(bio_err
, "%s: -rev cannot be used with raw input\n",
255 if (kdfalg
!= NULL
) {
258 "%s: no KDF length given (-kdflen parameter).\n", prog
);
261 } else if (inkey
== NULL
) {
263 "%s: no private key given (-inkey parameter).\n", prog
);
265 } else if (peerkey
!= NULL
&& pkey_op
!= EVP_PKEY_OP_DERIVE
) {
267 "%s: no peer key given (-peerkey parameter).\n", prog
);
270 ctx
= init_ctx(kdfalg
, &keysize
, inkey
, keyform
, key_type
,
271 passinarg
, pkey_op
, e
, engine_impl
, rawin
, &pkey
);
273 BIO_printf(bio_err
, "%s: Error initializing context\n", prog
);
274 ERR_print_errors(bio_err
);
277 if (peerkey
!= NULL
&& !setup_peer(ctx
, peerform
, peerkey
, e
)) {
278 BIO_printf(bio_err
, "%s: Error setting up peer key\n", prog
);
279 ERR_print_errors(bio_err
);
282 if (pkeyopts
!= NULL
) {
283 int num
= sk_OPENSSL_STRING_num(pkeyopts
);
286 for (i
= 0; i
< num
; ++i
) {
287 const char *opt
= sk_OPENSSL_STRING_value(pkeyopts
, i
);
289 if (pkey_ctrl_string(ctx
, opt
) <= 0) {
290 BIO_printf(bio_err
, "%s: Can't set parameter \"%s\":\n",
292 ERR_print_errors(bio_err
);
297 if (pkeyopts_passin
!= NULL
) {
298 int num
= sk_OPENSSL_STRING_num(pkeyopts_passin
);
301 for (i
= 0; i
< num
; i
++) {
302 char *opt
= sk_OPENSSL_STRING_value(pkeyopts_passin
, i
);
303 char *passin
= strchr(opt
, ':');
306 if (passin
== NULL
) {
307 /* Get password interactively */
308 char passwd_buf
[4096];
309 BIO_snprintf(passwd_buf
, sizeof(passwd_buf
), "Enter %s: ", opt
);
310 EVP_read_pw_string(passwd_buf
, sizeof(passwd_buf
) - 1,
312 passwd
= OPENSSL_strdup(passwd_buf
);
313 if (passwd
== NULL
) {
314 BIO_puts(bio_err
, "out of memory\n");
318 /* Get password as a passin argument: First split option name
319 * and passphrase argument into two strings */
322 if (app_passwd(passin
, NULL
, &passwd
, NULL
) == 0) {
323 BIO_printf(bio_err
, "failed to get '%s'\n", opt
);
328 if (EVP_PKEY_CTX_ctrl_str(ctx
, opt
, passwd
) <= 0) {
329 BIO_printf(bio_err
, "%s: Can't set parameter \"%s\":\n",
333 OPENSSL_free(passwd
);
337 if (sigfile
!= NULL
&& (pkey_op
!= EVP_PKEY_OP_VERIFY
)) {
339 "%s: Signature file specified for non verify\n", prog
);
343 if (sigfile
== NULL
&& (pkey_op
== EVP_PKEY_OP_VERIFY
)) {
345 "%s: No signature file specified for verify\n", prog
);
349 if (pkey_op
!= EVP_PKEY_OP_DERIVE
) {
350 in
= bio_open_default(infile
, 'r', FORMAT_BINARY
);
351 #ifndef OPENSSL_NO_POSIX_IO
356 if (stat(infile
, &st
) == 0 && st
.st_size
<= INT_MAX
)
357 filesize
= (int)st
.st_size
;
363 out
= bio_open_default(outfile
, 'w', FORMAT_BINARY
);
367 if (sigfile
!= NULL
) {
368 BIO
*sigbio
= BIO_new_file(sigfile
, "rb");
370 if (sigbio
== NULL
) {
371 BIO_printf(bio_err
, "Can't open signature file %s\n", sigfile
);
374 siglen
= bio_to_mem(&sig
, keysize
* 10, sigbio
);
377 BIO_printf(bio_err
, "Error reading signature data\n");
382 /* Raw input data is handled elsewhere */
383 if (in
!= NULL
&& !rawin
) {
384 /* Read the input data */
385 buf_inlen
= bio_to_mem(&buf_in
, keysize
* 10, in
);
387 BIO_printf(bio_err
, "Error reading input Data\n");
393 size_t l
= (size_t)buf_inlen
;
394 for (i
= 0; i
< l
/ 2; i
++) {
396 buf_in
[i
] = buf_in
[l
- 1 - i
];
397 buf_in
[l
- 1 - i
] = ctmp
;
402 /* Sanity check the input if the input is not raw */
404 && buf_inlen
> EVP_MAX_MD_SIZE
405 && (pkey_op
== EVP_PKEY_OP_SIGN
406 || pkey_op
== EVP_PKEY_OP_VERIFY
407 || pkey_op
== EVP_PKEY_OP_VERIFYRECOVER
)) {
409 "Error: The input data looks too long to be a hash\n");
413 if (pkey_op
== EVP_PKEY_OP_VERIFY
) {
415 rv
= do_raw_keyop(pkey_op
, ctx
, md
, pkey
, in
, filesize
, sig
, siglen
,
418 rv
= EVP_PKEY_verify(ctx
, sig
, (size_t)siglen
,
419 buf_in
, (size_t)buf_inlen
);
422 BIO_puts(out
, "Signature Verified Successfully\n");
425 BIO_puts(out
, "Signature Verification Failure\n");
434 /* rawin allocates the buffer in do_raw_keyop() */
435 rv
= do_raw_keyop(pkey_op
, ctx
, md
, pkey
, in
, filesize
, NULL
, 0,
436 &buf_out
, (size_t *)&buf_outlen
);
438 rv
= do_keyop(ctx
, pkey_op
, NULL
, (size_t *)&buf_outlen
,
439 buf_in
, (size_t)buf_inlen
);
440 if (rv
> 0 && buf_outlen
!= 0) {
441 buf_out
= app_malloc(buf_outlen
, "buffer output");
442 rv
= do_keyop(ctx
, pkey_op
,
443 buf_out
, (size_t *)&buf_outlen
,
444 buf_in
, (size_t)buf_inlen
);
449 if (pkey_op
!= EVP_PKEY_OP_DERIVE
) {
450 BIO_puts(bio_err
, "Public Key operation error\n");
452 BIO_puts(bio_err
, "Key derivation failed\n");
454 ERR_print_errors(bio_err
);
460 if (!ASN1_parse_dump(out
, buf_out
, buf_outlen
, 1, -1))
461 ERR_print_errors(bio_err
);
462 } else if (hexdump
) {
463 BIO_dump(out
, (char *)buf_out
, buf_outlen
);
465 BIO_write(out
, buf_out
, buf_outlen
);
469 EVP_PKEY_CTX_free(ctx
);
473 OPENSSL_free(buf_in
);
474 OPENSSL_free(buf_out
);
476 sk_OPENSSL_STRING_free(pkeyopts
);
477 sk_OPENSSL_STRING_free(pkeyopts_passin
);
481 static EVP_PKEY_CTX
*init_ctx(const char *kdfalg
, int *pkeysize
,
482 const char *keyfile
, int keyform
, int key_type
,
483 char *passinarg
, int pkey_op
, ENGINE
*e
,
484 const int engine_impl
, int rawin
,
487 EVP_PKEY
*pkey
= NULL
;
488 EVP_PKEY_CTX
*ctx
= NULL
;
493 if (((pkey_op
== EVP_PKEY_OP_SIGN
) || (pkey_op
== EVP_PKEY_OP_DECRYPT
)
494 || (pkey_op
== EVP_PKEY_OP_DERIVE
))
495 && (key_type
!= KEY_PRIVKEY
&& kdfalg
== NULL
)) {
496 BIO_printf(bio_err
, "A private key is needed for this operation\n");
499 if (!app_passwd(passinarg
, NULL
, &passin
, NULL
)) {
500 BIO_printf(bio_err
, "Error getting password\n");
505 pkey
= load_key(keyfile
, keyform
, 0, passin
, e
, "Private Key");
509 pkey
= load_pubkey(keyfile
, keyform
, 0, NULL
, e
, "Public Key");
513 x
= load_cert(keyfile
, keyform
, "Certificate");
515 pkey
= X509_get_pubkey(x
);
525 #ifndef OPENSSL_NO_ENGINE
530 if (kdfalg
!= NULL
) {
531 int kdfnid
= OBJ_sn2nid(kdfalg
);
533 if (kdfnid
== NID_undef
) {
534 kdfnid
= OBJ_ln2nid(kdfalg
);
535 if (kdfnid
== NID_undef
) {
536 BIO_printf(bio_err
, "The given KDF \"%s\" is unknown.\n",
541 ctx
= EVP_PKEY_CTX_new_id(kdfnid
, impl
);
546 #ifndef OPENSSL_NO_EC
547 /* SM2 needs a special treatment */
548 if (EVP_PKEY_id(pkey
) == EVP_PKEY_EC
) {
549 EC_KEY
*eckey
= NULL
;
550 const EC_GROUP
*group
= NULL
;
553 if ((eckey
= EVP_PKEY_get0_EC_KEY(pkey
)) == NULL
554 || (group
= EC_KEY_get0_group(eckey
)) == NULL
555 || (nid
= EC_GROUP_get_curve_name(group
)) == 0)
558 EVP_PKEY_set_alias_type(pkey
, EVP_PKEY_SM2
);
561 *pkeysize
= EVP_PKEY_size(pkey
);
562 ctx
= EVP_PKEY_CTX_new(pkey
, impl
);
572 * If rawin then we don't need to actually initialise the EVP_PKEY_CTX
573 * itself. That will get initialised during EVP_DigestSignInit or
574 * EVP_DigestVerifyInit.
580 case EVP_PKEY_OP_SIGN
:
581 rv
= EVP_PKEY_sign_init(ctx
);
584 case EVP_PKEY_OP_VERIFY
:
585 rv
= EVP_PKEY_verify_init(ctx
);
588 case EVP_PKEY_OP_VERIFYRECOVER
:
589 rv
= EVP_PKEY_verify_recover_init(ctx
);
592 case EVP_PKEY_OP_ENCRYPT
:
593 rv
= EVP_PKEY_encrypt_init(ctx
);
596 case EVP_PKEY_OP_DECRYPT
:
597 rv
= EVP_PKEY_decrypt_init(ctx
);
600 case EVP_PKEY_OP_DERIVE
:
601 rv
= EVP_PKEY_derive_init(ctx
);
607 EVP_PKEY_CTX_free(ctx
);
612 OPENSSL_free(passin
);
617 static int setup_peer(EVP_PKEY_CTX
*ctx
, int peerform
, const char *file
,
620 EVP_PKEY
*peer
= NULL
;
621 ENGINE
*engine
= NULL
;
624 if (peerform
== FORMAT_ENGINE
)
626 peer
= load_pubkey(file
, peerform
, 0, NULL
, engine
, "Peer Key");
628 BIO_printf(bio_err
, "Error reading peer key %s\n", file
);
629 ERR_print_errors(bio_err
);
633 ret
= EVP_PKEY_derive_set_peer(ctx
, peer
);
637 ERR_print_errors(bio_err
);
641 static int do_keyop(EVP_PKEY_CTX
*ctx
, int pkey_op
,
642 unsigned char *out
, size_t *poutlen
,
643 const unsigned char *in
, size_t inlen
)
647 case EVP_PKEY_OP_VERIFYRECOVER
:
648 rv
= EVP_PKEY_verify_recover(ctx
, out
, poutlen
, in
, inlen
);
651 case EVP_PKEY_OP_SIGN
:
652 rv
= EVP_PKEY_sign(ctx
, out
, poutlen
, in
, inlen
);
655 case EVP_PKEY_OP_ENCRYPT
:
656 rv
= EVP_PKEY_encrypt(ctx
, out
, poutlen
, in
, inlen
);
659 case EVP_PKEY_OP_DECRYPT
:
660 rv
= EVP_PKEY_decrypt(ctx
, out
, poutlen
, in
, inlen
);
663 case EVP_PKEY_OP_DERIVE
:
664 rv
= EVP_PKEY_derive(ctx
, out
, poutlen
);
671 #define TBUF_MAXSIZE 2048
673 static int do_raw_keyop(int pkey_op
, EVP_PKEY_CTX
*ctx
,
674 const EVP_MD
*md
, EVP_PKEY
*pkey
, BIO
*in
,
675 int filesize
, unsigned char *sig
, int siglen
,
676 unsigned char **out
, size_t *poutlen
)
679 EVP_MD_CTX
*mctx
= NULL
;
680 unsigned char tbuf
[TBUF_MAXSIZE
];
681 unsigned char *mbuf
= NULL
;
684 if ((mctx
= EVP_MD_CTX_new()) == NULL
) {
685 BIO_printf(bio_err
, "Error: out of memory\n");
688 EVP_MD_CTX_set_pkey_ctx(mctx
, ctx
);
690 /* Some algorithms only support oneshot digests */
691 if (EVP_PKEY_id(pkey
) == EVP_PKEY_ED25519
692 || EVP_PKEY_id(pkey
) == EVP_PKEY_ED448
) {
695 "Error: unable to determine file size for oneshot operation\n");
698 mbuf
= app_malloc(filesize
, "oneshot sign/verify buffer");
700 case EVP_PKEY_OP_VERIFY
:
701 if (EVP_DigestVerifyInit(mctx
, NULL
, md
, NULL
, pkey
) != 1)
703 buf_len
= BIO_read(in
, mbuf
, filesize
);
704 if (buf_len
!= filesize
) {
705 BIO_printf(bio_err
, "Error reading raw input data\n");
708 rv
= EVP_DigestVerify(mctx
, sig
, (size_t)siglen
, mbuf
, buf_len
);
710 case EVP_PKEY_OP_SIGN
:
711 if (EVP_DigestSignInit(mctx
, NULL
, md
, NULL
, pkey
) != 1)
713 buf_len
= BIO_read(in
, mbuf
, filesize
);
714 if (buf_len
!= filesize
) {
715 BIO_printf(bio_err
, "Error reading raw input data\n");
718 rv
= EVP_DigestSign(mctx
, NULL
, poutlen
, mbuf
, buf_len
);
719 if (rv
== 1 && out
!= NULL
) {
720 *out
= app_malloc(*poutlen
, "buffer output");
721 rv
= EVP_DigestSign(mctx
, *out
, poutlen
, mbuf
, buf_len
);
730 case EVP_PKEY_OP_VERIFY
:
731 if (EVP_DigestVerifyInit(mctx
, NULL
, md
, NULL
, pkey
) != 1)
734 buf_len
= BIO_read(in
, tbuf
, TBUF_MAXSIZE
);
738 BIO_printf(bio_err
, "Error reading raw input data\n");
741 rv
= EVP_DigestVerifyUpdate(mctx
, tbuf
, (size_t)buf_len
);
743 BIO_printf(bio_err
, "Error verifying raw input data\n");
747 rv
= EVP_DigestVerifyFinal(mctx
, sig
, (size_t)siglen
);
749 case EVP_PKEY_OP_SIGN
:
750 if (EVP_DigestSignInit(mctx
, NULL
, md
, NULL
, pkey
) != 1)
753 buf_len
= BIO_read(in
, tbuf
, TBUF_MAXSIZE
);
757 BIO_printf(bio_err
, "Error reading raw input data\n");
760 rv
= EVP_DigestSignUpdate(mctx
, tbuf
, (size_t)buf_len
);
762 BIO_printf(bio_err
, "Error signing raw input data\n");
766 rv
= EVP_DigestSignFinal(mctx
, NULL
, poutlen
);
767 if (rv
== 1 && out
!= NULL
) {
768 *out
= app_malloc(*poutlen
, "buffer output");
769 rv
= EVP_DigestSignFinal(mctx
, *out
, poutlen
);
775 EVP_MD_CTX_free(mctx
);