2 * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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>
22 static EVP_PKEY_CTX
*init_ctx(const char *kdfalg
, int *pkeysize
,
23 const char *keyfile
, int keyform
, int key_type
,
24 char *passinarg
, int pkey_op
, ENGINE
*e
,
27 static int setup_peer(EVP_PKEY_CTX
*ctx
, int peerform
, const char *file
,
30 static int do_keyop(EVP_PKEY_CTX
*ctx
, int pkey_op
,
31 unsigned char *out
, size_t *poutlen
,
32 const unsigned char *in
, size_t inlen
);
34 typedef enum OPTION_choice
{
35 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
36 OPT_ENGINE
, OPT_ENGINE_IMPL
, OPT_IN
, OPT_OUT
,
37 OPT_PUBIN
, OPT_CERTIN
, OPT_ASN1PARSE
, OPT_HEXDUMP
, OPT_SIGN
,
38 OPT_VERIFY
, OPT_VERIFYRECOVER
, OPT_REV
, OPT_ENCRYPT
, OPT_DECRYPT
,
39 OPT_DERIVE
, OPT_SIGFILE
, OPT_INKEY
, OPT_PEERKEY
, OPT_PASSIN
,
40 OPT_PEERFORM
, OPT_KEYFORM
, OPT_PKEYOPT
, OPT_KDF
, OPT_KDFLEN
,
44 const OPTIONS pkeyutl_options
[] = {
45 {"help", OPT_HELP
, '-', "Display this summary"},
46 {"in", OPT_IN
, '<', "Input file - default stdin"},
47 {"out", OPT_OUT
, '>', "Output file - default stdout"},
48 {"pubin", OPT_PUBIN
, '-', "Input is a public key"},
49 {"certin", OPT_CERTIN
, '-', "Input is a cert with a public key"},
50 {"asn1parse", OPT_ASN1PARSE
, '-', "asn1parse the output data"},
51 {"hexdump", OPT_HEXDUMP
, '-', "Hex dump output"},
52 {"sign", OPT_SIGN
, '-', "Sign input data with private key"},
53 {"verify", OPT_VERIFY
, '-', "Verify with public key"},
54 {"verifyrecover", OPT_VERIFYRECOVER
, '-',
55 "Verify with public key, recover original data"},
56 {"rev", OPT_REV
, '-', "Reverse the order of the input buffer"},
57 {"encrypt", OPT_ENCRYPT
, '-', "Encrypt input data with public key"},
58 {"decrypt", OPT_DECRYPT
, '-', "Decrypt input data with private key"},
59 {"derive", OPT_DERIVE
, '-', "Derive shared secret"},
60 {"kdf", OPT_KDF
, 's', "Use KDF algorithm"},
61 {"kdflen", OPT_KDFLEN
, 'p', "KDF algorithm output length"},
62 {"sigfile", OPT_SIGFILE
, '<', "Signature file (verify operation only)"},
63 {"inkey", OPT_INKEY
, 's', "Input private key file"},
64 {"peerkey", OPT_PEERKEY
, 's', "Peer key file used in key derivation"},
65 {"passin", OPT_PASSIN
, 's', "Input file pass phrase source"},
66 {"peerform", OPT_PEERFORM
, 'E', "Peer key format - default PEM"},
67 {"keyform", OPT_KEYFORM
, 'E', "Private key format - default PEM"},
68 {"pkeyopt", OPT_PKEYOPT
, 's', "Public key options as opt:value"},
70 #ifndef OPENSSL_NO_ENGINE
71 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
72 {"engine_impl", OPT_ENGINE_IMPL
, '-',
73 "Also use engine given by -engine for crypto operations"},
78 int pkeyutl_main(int argc
, char **argv
)
80 BIO
*in
= NULL
, *out
= NULL
;
82 EVP_PKEY_CTX
*ctx
= NULL
;
83 char *infile
= NULL
, *outfile
= NULL
, *sigfile
= NULL
, *passinarg
= NULL
;
84 char hexdump
= 0, asn1parse
= 0, rev
= 0, *prog
;
85 unsigned char *buf_in
= NULL
, *buf_out
= NULL
, *sig
= NULL
;
87 int buf_inlen
= 0, siglen
= -1, keyform
= FORMAT_PEM
, peerform
= FORMAT_PEM
;
88 int keysize
= -1, pkey_op
= EVP_PKEY_OP_SIGN
, key_type
= KEY_PRIVKEY
;
92 const char *inkey
= NULL
;
93 const char *peerkey
= NULL
;
94 const char *kdfalg
= NULL
;
96 STACK_OF(OPENSSL_STRING
) *pkeyopts
= NULL
;
98 prog
= opt_init(argc
, argv
, pkeyutl_options
);
99 while ((o
= opt_next()) != OPT_EOF
) {
104 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
107 opt_help(pkeyutl_options
);
119 case OPT_ENGINE_IMPL
:
129 passinarg
= opt_arg();
132 if (!opt_format(opt_arg(), OPT_FMT_PDE
, &peerform
))
136 if (!opt_format(opt_arg(), OPT_FMT_PDE
, &keyform
))
144 e
= setup_engine(opt_arg(), 0);
147 key_type
= KEY_PUBKEY
;
159 pkey_op
= EVP_PKEY_OP_SIGN
;
162 pkey_op
= EVP_PKEY_OP_VERIFY
;
164 case OPT_VERIFYRECOVER
:
165 pkey_op
= EVP_PKEY_OP_VERIFYRECOVER
;
168 pkey_op
= EVP_PKEY_OP_ENCRYPT
;
171 pkey_op
= EVP_PKEY_OP_DECRYPT
;
174 pkey_op
= EVP_PKEY_OP_DERIVE
;
177 pkey_op
= EVP_PKEY_OP_DERIVE
;
182 kdflen
= atoi(opt_arg());
188 if ((pkeyopts
== NULL
&&
189 (pkeyopts
= sk_OPENSSL_STRING_new_null()) == NULL
) ||
190 sk_OPENSSL_STRING_push(pkeyopts
, opt_arg()) == 0) {
191 BIO_puts(bio_err
, "out of memory\n");
197 argc
= opt_num_rest();
201 if (kdfalg
!= NULL
) {
204 "%s: no KDF length given (-kdflen parameter).\n", prog
);
207 } else if (inkey
== NULL
) {
209 "%s: no private key given (-inkey parameter).\n", prog
);
211 } else if (peerkey
!= NULL
&& pkey_op
!= EVP_PKEY_OP_DERIVE
) {
213 "%s: no peer key given (-peerkey parameter).\n", prog
);
216 ctx
= init_ctx(kdfalg
, &keysize
, inkey
, keyform
, key_type
,
217 passinarg
, pkey_op
, e
, engine_impl
);
219 BIO_printf(bio_err
, "%s: Error initializing context\n", prog
);
220 ERR_print_errors(bio_err
);
223 if (peerkey
!= NULL
&& !setup_peer(ctx
, peerform
, peerkey
, e
)) {
224 BIO_printf(bio_err
, "%s: Error setting up peer key\n", prog
);
225 ERR_print_errors(bio_err
);
228 if (pkeyopts
!= NULL
) {
229 int num
= sk_OPENSSL_STRING_num(pkeyopts
);
232 for (i
= 0; i
< num
; ++i
) {
233 const char *opt
= sk_OPENSSL_STRING_value(pkeyopts
, i
);
235 if (pkey_ctrl_string(ctx
, opt
) <= 0) {
236 BIO_printf(bio_err
, "%s: Can't set parameter \"%s\":\n",
238 ERR_print_errors(bio_err
);
244 if (sigfile
!= NULL
&& (pkey_op
!= EVP_PKEY_OP_VERIFY
)) {
246 "%s: Signature file specified for non verify\n", prog
);
250 if (sigfile
== NULL
&& (pkey_op
== EVP_PKEY_OP_VERIFY
)) {
252 "%s: No signature file specified for verify\n", prog
);
256 if (pkey_op
!= EVP_PKEY_OP_DERIVE
) {
257 in
= bio_open_default(infile
, 'r', FORMAT_BINARY
);
261 out
= bio_open_default(outfile
, 'w', FORMAT_BINARY
);
265 if (sigfile
!= NULL
) {
266 BIO
*sigbio
= BIO_new_file(sigfile
, "rb");
268 if (sigbio
== NULL
) {
269 BIO_printf(bio_err
, "Can't open signature file %s\n", sigfile
);
272 siglen
= bio_to_mem(&sig
, keysize
* 10, sigbio
);
275 BIO_printf(bio_err
, "Error reading signature data\n");
281 /* Read the input data */
282 buf_inlen
= bio_to_mem(&buf_in
, keysize
* 10, in
);
284 BIO_printf(bio_err
, "Error reading input Data\n");
290 size_t l
= (size_t)buf_inlen
;
291 for (i
= 0; i
< l
/ 2; i
++) {
293 buf_in
[i
] = buf_in
[l
- 1 - i
];
294 buf_in
[l
- 1 - i
] = ctmp
;
299 /* Sanity check the input */
300 if (buf_inlen
> EVP_MAX_MD_SIZE
301 && (pkey_op
== EVP_PKEY_OP_SIGN
302 || pkey_op
== EVP_PKEY_OP_VERIFY
303 || pkey_op
== EVP_PKEY_OP_VERIFYRECOVER
)) {
305 "Error: The input data looks too long to be a hash\n");
309 if (pkey_op
== EVP_PKEY_OP_VERIFY
) {
310 rv
= EVP_PKEY_verify(ctx
, sig
, (size_t)siglen
,
311 buf_in
, (size_t)buf_inlen
);
313 BIO_puts(out
, "Signature Verified Successfully\n");
316 BIO_puts(out
, "Signature Verification Failure\n");
324 rv
= do_keyop(ctx
, pkey_op
, NULL
, (size_t *)&buf_outlen
,
325 buf_in
, (size_t)buf_inlen
);
327 if (rv
> 0 && buf_outlen
!= 0) {
328 buf_out
= app_malloc(buf_outlen
, "buffer output");
329 rv
= do_keyop(ctx
, pkey_op
,
330 buf_out
, (size_t *)&buf_outlen
,
331 buf_in
, (size_t)buf_inlen
);
334 if (pkey_op
!= EVP_PKEY_OP_DERIVE
) {
335 BIO_puts(bio_err
, "Public Key operation error\n");
337 BIO_puts(bio_err
, "Key derivation failed\n");
339 ERR_print_errors(bio_err
);
345 if (!ASN1_parse_dump(out
, buf_out
, buf_outlen
, 1, -1))
346 ERR_print_errors(bio_err
);
347 } else if (hexdump
) {
348 BIO_dump(out
, (char *)buf_out
, buf_outlen
);
350 BIO_write(out
, buf_out
, buf_outlen
);
354 EVP_PKEY_CTX_free(ctx
);
358 OPENSSL_free(buf_in
);
359 OPENSSL_free(buf_out
);
361 sk_OPENSSL_STRING_free(pkeyopts
);
365 static EVP_PKEY_CTX
*init_ctx(const char *kdfalg
, int *pkeysize
,
366 const char *keyfile
, int keyform
, int key_type
,
367 char *passinarg
, int pkey_op
, ENGINE
*e
,
368 const int engine_impl
)
370 EVP_PKEY
*pkey
= NULL
;
371 EVP_PKEY_CTX
*ctx
= NULL
;
376 if (((pkey_op
== EVP_PKEY_OP_SIGN
) || (pkey_op
== EVP_PKEY_OP_DECRYPT
)
377 || (pkey_op
== EVP_PKEY_OP_DERIVE
))
378 && (key_type
!= KEY_PRIVKEY
&& kdfalg
== NULL
)) {
379 BIO_printf(bio_err
, "A private key is needed for this operation\n");
382 if (!app_passwd(passinarg
, NULL
, &passin
, NULL
)) {
383 BIO_printf(bio_err
, "Error getting password\n");
388 pkey
= load_key(keyfile
, keyform
, 0, passin
, e
, "Private Key");
392 pkey
= load_pubkey(keyfile
, keyform
, 0, NULL
, e
, "Public Key");
396 x
= load_cert(keyfile
, keyform
, "Certificate");
398 pkey
= X509_get_pubkey(x
);
408 #ifndef OPENSSL_NO_ENGINE
413 if (kdfalg
!= NULL
) {
414 int kdfnid
= OBJ_sn2nid(kdfalg
);
416 if (kdfnid
== NID_undef
) {
417 kdfnid
= OBJ_ln2nid(kdfalg
);
418 if (kdfnid
== NID_undef
) {
419 BIO_printf(bio_err
, "The given KDF \"%s\" is unknown.\n",
424 ctx
= EVP_PKEY_CTX_new_id(kdfnid
, impl
);
428 *pkeysize
= EVP_PKEY_size(pkey
);
429 ctx
= EVP_PKEY_CTX_new(pkey
, impl
);
437 case EVP_PKEY_OP_SIGN
:
438 rv
= EVP_PKEY_sign_init(ctx
);
441 case EVP_PKEY_OP_VERIFY
:
442 rv
= EVP_PKEY_verify_init(ctx
);
445 case EVP_PKEY_OP_VERIFYRECOVER
:
446 rv
= EVP_PKEY_verify_recover_init(ctx
);
449 case EVP_PKEY_OP_ENCRYPT
:
450 rv
= EVP_PKEY_encrypt_init(ctx
);
453 case EVP_PKEY_OP_DECRYPT
:
454 rv
= EVP_PKEY_decrypt_init(ctx
);
457 case EVP_PKEY_OP_DERIVE
:
458 rv
= EVP_PKEY_derive_init(ctx
);
463 EVP_PKEY_CTX_free(ctx
);
468 OPENSSL_free(passin
);
473 static int setup_peer(EVP_PKEY_CTX
*ctx
, int peerform
, const char *file
,
476 EVP_PKEY
*peer
= NULL
;
477 ENGINE
*engine
= NULL
;
480 if (peerform
== FORMAT_ENGINE
)
482 peer
= load_pubkey(file
, peerform
, 0, NULL
, engine
, "Peer Key");
484 BIO_printf(bio_err
, "Error reading peer key %s\n", file
);
485 ERR_print_errors(bio_err
);
489 ret
= EVP_PKEY_derive_set_peer(ctx
, peer
);
493 ERR_print_errors(bio_err
);
497 static int do_keyop(EVP_PKEY_CTX
*ctx
, int pkey_op
,
498 unsigned char *out
, size_t *poutlen
,
499 const unsigned char *in
, size_t inlen
)
503 case EVP_PKEY_OP_VERIFYRECOVER
:
504 rv
= EVP_PKEY_verify_recover(ctx
, out
, poutlen
, in
, inlen
);
507 case EVP_PKEY_OP_SIGN
:
508 rv
= EVP_PKEY_sign(ctx
, out
, poutlen
, in
, inlen
);
511 case EVP_PKEY_OP_ENCRYPT
:
512 rv
= EVP_PKEY_encrypt(ctx
, out
, poutlen
, in
, inlen
);
515 case EVP_PKEY_OP_DECRYPT
:
516 rv
= EVP_PKEY_decrypt(ctx
, out
, poutlen
, in
, inlen
);
519 case EVP_PKEY_OP_DERIVE
:
520 rv
= EVP_PKEY_derive(ctx
, out
, poutlen
);