2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
5 /* ====================================================================
6 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
61 #include <openssl/err.h>
62 #include <openssl/pem.h>
63 #include <openssl/evp.h>
70 static EVP_PKEY_CTX
*init_ctx(const char *kdfalg
, int *pkeysize
,
71 const char *keyfile
, int keyform
, int key_type
,
72 char *passinarg
, int pkey_op
, ENGINE
*e
,
75 static int setup_peer(EVP_PKEY_CTX
*ctx
, int peerform
, const char *file
,
78 static int do_keyop(EVP_PKEY_CTX
*ctx
, int pkey_op
,
79 unsigned char *out
, size_t *poutlen
,
80 unsigned char *in
, size_t inlen
);
82 typedef enum OPTION_choice
{
83 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
84 OPT_ENGINE
, OPT_ENGINE_IMPL
, OPT_IN
, OPT_OUT
,
85 OPT_PUBIN
, OPT_CERTIN
, OPT_ASN1PARSE
, OPT_HEXDUMP
, OPT_SIGN
,
86 OPT_VERIFY
, OPT_VERIFYRECOVER
, OPT_REV
, OPT_ENCRYPT
, OPT_DECRYPT
,
87 OPT_DERIVE
, OPT_SIGFILE
, OPT_INKEY
, OPT_PEERKEY
, OPT_PASSIN
,
88 OPT_PEERFORM
, OPT_KEYFORM
, OPT_PKEYOPT
, OPT_KDF
, OPT_KDFLEN
91 OPTIONS pkeyutl_options
[] = {
92 {"help", OPT_HELP
, '-', "Display this summary"},
93 {"in", OPT_IN
, '<', "Input file - default stdin"},
94 {"out", OPT_OUT
, '>', "Output file - default stdout"},
95 {"pubin", OPT_PUBIN
, '-', "Input is a public key"},
96 {"certin", OPT_CERTIN
, '-', "Input is a cert with a public key"},
97 {"asn1parse", OPT_ASN1PARSE
, '-', "asn1parse the output data"},
98 {"hexdump", OPT_HEXDUMP
, '-', "Hex dump output"},
99 {"sign", OPT_SIGN
, '-', "Sign input data with private key"},
100 {"verify", OPT_VERIFY
, '-', "Verify with public key"},
101 {"verifyrecover", OPT_VERIFYRECOVER
, '-',
102 "Verify with public key, recover original data"},
103 {"rev", OPT_REV
, '-', "Reverse the order of the input buffer"},
104 {"encrypt", OPT_ENCRYPT
, '-', "Encrypt input data with public key"},
105 {"decrypt", OPT_DECRYPT
, '-', "Decrypt input data with private key"},
106 {"derive", OPT_DERIVE
, '-', "Derive shared secret"},
107 {"kdf", OPT_KDF
, 's', "Use KDF algorithm"},
108 {"kdflen", OPT_KDFLEN
, 'p', "KDF algorithm output length"},
109 {"sigfile", OPT_SIGFILE
, '<', "Signature file (verify operation only)"},
110 {"inkey", OPT_INKEY
, 's', "Input private key file"},
111 {"peerkey", OPT_PEERKEY
, 's', "Peer key file used in key derivation"},
112 {"passin", OPT_PASSIN
, 's', "Pass phrase source"},
113 {"peerform", OPT_PEERFORM
, 'E', "Peer key format - default PEM"},
114 {"keyform", OPT_KEYFORM
, 'E', "Private key format - default PEM"},
115 {"pkeyopt", OPT_PKEYOPT
, 's', "Public key options as opt:value"},
116 #ifndef OPENSSL_NO_ENGINE
117 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
118 {"engine_impl", OPT_ENGINE_IMPL
, '-',
119 "Also use engine given by -engine for crypto operations"},
124 int pkeyutl_main(int argc
, char **argv
)
126 BIO
*in
= NULL
, *out
= NULL
;
128 EVP_PKEY_CTX
*ctx
= NULL
;
129 char *infile
= NULL
, *outfile
= NULL
, *sigfile
= NULL
, *passinarg
= NULL
;
130 char hexdump
= 0, asn1parse
= 0, rev
= 0, *prog
;
131 unsigned char *buf_in
= NULL
, *buf_out
= NULL
, *sig
= NULL
;
133 int buf_inlen
= 0, siglen
= -1, keyform
= FORMAT_PEM
, peerform
=
135 int keysize
= -1, pkey_op
= EVP_PKEY_OP_SIGN
, key_type
= KEY_PRIVKEY
;
137 int ret
= 1, rv
= -1;
139 const char *inkey
= NULL
;
140 const char *peerkey
= NULL
;
141 const char *kdfalg
= NULL
;
143 STACK_OF(OPENSSL_STRING
) *pkeyopts
= NULL
;
145 prog
= opt_init(argc
, argv
, pkeyutl_options
);
146 while ((o
= opt_next()) != OPT_EOF
) {
151 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
154 opt_help(pkeyutl_options
);
166 case OPT_ENGINE_IMPL
:
176 passinarg
= opt_arg();
179 if (!opt_format(opt_arg(), OPT_FMT_PDE
, &peerform
))
183 if (!opt_format(opt_arg(), OPT_FMT_PDE
, &keyform
))
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");
240 argc
= opt_num_rest();
244 if (kdfalg
!= NULL
) {
247 } else if ((inkey
== NULL
)
248 || (peerkey
!= NULL
&& pkey_op
!= EVP_PKEY_OP_DERIVE
)) {
251 ctx
= init_ctx(kdfalg
, &keysize
, inkey
, keyform
, key_type
,
252 passinarg
, pkey_op
, e
, engine_impl
);
254 BIO_printf(bio_err
, "%s: Error initializing context\n", prog
);
255 ERR_print_errors(bio_err
);
258 if (peerkey
!= NULL
&& !setup_peer(ctx
, peerform
, peerkey
, e
)) {
259 BIO_printf(bio_err
, "%s: Error setting up peer key\n", prog
);
260 ERR_print_errors(bio_err
);
263 if (pkeyopts
!= NULL
) {
264 int num
= sk_OPENSSL_STRING_num(pkeyopts
);
267 for (i
= 0; i
< num
; ++i
) {
268 const char *opt
= sk_OPENSSL_STRING_value(pkeyopts
, i
);
270 if (pkey_ctrl_string(ctx
, opt
) <= 0) {
271 BIO_printf(bio_err
, "%s: Can't set parameter:\n", prog
);
272 ERR_print_errors(bio_err
);
278 if (sigfile
&& (pkey_op
!= EVP_PKEY_OP_VERIFY
)) {
280 "%s: Signature file specified for non verify\n", prog
);
284 if (!sigfile
&& (pkey_op
== EVP_PKEY_OP_VERIFY
)) {
286 "%s: No signature file specified for verify\n", prog
);
290 /* FIXME: seed PRNG only if needed */
291 app_RAND_load_file(NULL
, 0);
293 if (pkey_op
!= EVP_PKEY_OP_DERIVE
) {
294 in
= bio_open_default(infile
, 'r', FORMAT_BINARY
);
298 out
= bio_open_default(outfile
, 'w', FORMAT_BINARY
);
303 BIO
*sigbio
= BIO_new_file(sigfile
, "rb");
305 BIO_printf(bio_err
, "Can't open signature file %s\n", sigfile
);
308 siglen
= bio_to_mem(&sig
, keysize
* 10, sigbio
);
311 BIO_printf(bio_err
, "Error reading signature data\n");
317 /* Read the input data */
318 buf_inlen
= bio_to_mem(&buf_in
, keysize
* 10, in
);
320 BIO_printf(bio_err
, "Error reading input Data\n");
326 size_t l
= (size_t)buf_inlen
;
327 for (i
= 0; i
< l
/ 2; i
++) {
329 buf_in
[i
] = buf_in
[l
- 1 - i
];
330 buf_in
[l
- 1 - i
] = ctmp
;
335 if (pkey_op
== EVP_PKEY_OP_VERIFY
) {
336 rv
= EVP_PKEY_verify(ctx
, sig
, (size_t)siglen
,
337 buf_in
, (size_t)buf_inlen
);
339 BIO_puts(out
, "Signature Verified Successfully\n");
342 BIO_puts(out
, "Signature Verification Failure\n");
349 rv
= do_keyop(ctx
, pkey_op
, NULL
, (size_t *)&buf_outlen
,
350 buf_in
, (size_t)buf_inlen
);
352 if (rv
> 0 && buf_outlen
!= 0) {
353 buf_out
= app_malloc(buf_outlen
, "buffer output");
354 rv
= do_keyop(ctx
, pkey_op
,
355 buf_out
, (size_t *)&buf_outlen
,
356 buf_in
, (size_t)buf_inlen
);
359 BIO_puts(bio_err
, "Public Key operation error\n");
360 ERR_print_errors(bio_err
);
366 if (!ASN1_parse_dump(out
, buf_out
, buf_outlen
, 1, -1))
367 ERR_print_errors(bio_err
);
369 BIO_dump(out
, (char *)buf_out
, buf_outlen
);
371 BIO_write(out
, buf_out
, buf_outlen
);
374 EVP_PKEY_CTX_free(ctx
);
377 OPENSSL_free(buf_in
);
378 OPENSSL_free(buf_out
);
380 sk_OPENSSL_STRING_free(pkeyopts
);
384 static EVP_PKEY_CTX
*init_ctx(const char *kdfalg
, int *pkeysize
,
385 const char *keyfile
, int keyform
, int key_type
,
386 char *passinarg
, int pkey_op
, ENGINE
*e
,
387 const int engine_impl
)
389 EVP_PKEY
*pkey
= NULL
;
390 EVP_PKEY_CTX
*ctx
= NULL
;
395 if (((pkey_op
== EVP_PKEY_OP_SIGN
) || (pkey_op
== EVP_PKEY_OP_DECRYPT
)
396 || (pkey_op
== EVP_PKEY_OP_DERIVE
))
397 && (key_type
!= KEY_PRIVKEY
&& kdfalg
== NULL
)) {
398 BIO_printf(bio_err
, "A private key is needed for this operation\n");
401 if (!app_passwd(passinarg
, NULL
, &passin
, NULL
)) {
402 BIO_printf(bio_err
, "Error getting password\n");
407 pkey
= load_key(keyfile
, keyform
, 0, passin
, e
, "Private Key");
411 pkey
= load_pubkey(keyfile
, keyform
, 0, NULL
, e
, "Public Key");
415 x
= load_cert(keyfile
, keyform
, "Certificate");
417 pkey
= X509_get_pubkey(x
);
427 #ifndef OPENSSL_NO_ENGINE
433 int kdfnid
= OBJ_sn2nid(kdfalg
);
434 if (kdfnid
== NID_undef
)
436 ctx
= EVP_PKEY_CTX_new_id(kdfnid
, impl
);
440 *pkeysize
= EVP_PKEY_size(pkey
);
441 ctx
= EVP_PKEY_CTX_new(pkey
, impl
);
449 case EVP_PKEY_OP_SIGN
:
450 rv
= EVP_PKEY_sign_init(ctx
);
453 case EVP_PKEY_OP_VERIFY
:
454 rv
= EVP_PKEY_verify_init(ctx
);
457 case EVP_PKEY_OP_VERIFYRECOVER
:
458 rv
= EVP_PKEY_verify_recover_init(ctx
);
461 case EVP_PKEY_OP_ENCRYPT
:
462 rv
= EVP_PKEY_encrypt_init(ctx
);
465 case EVP_PKEY_OP_DECRYPT
:
466 rv
= EVP_PKEY_decrypt_init(ctx
);
469 case EVP_PKEY_OP_DERIVE
:
470 rv
= EVP_PKEY_derive_init(ctx
);
475 EVP_PKEY_CTX_free(ctx
);
480 OPENSSL_free(passin
);
485 static int setup_peer(EVP_PKEY_CTX
*ctx
, int peerform
, const char *file
,
488 EVP_PKEY
*peer
= NULL
;
489 ENGINE
* engine
= NULL
;
492 if (peerform
== FORMAT_ENGINE
)
494 peer
= load_pubkey(file
, peerform
, 0, NULL
, engine
, "Peer Key");
496 BIO_printf(bio_err
, "Error reading peer key %s\n", file
);
497 ERR_print_errors(bio_err
);
501 ret
= EVP_PKEY_derive_set_peer(ctx
, peer
);
505 ERR_print_errors(bio_err
);
509 static int do_keyop(EVP_PKEY_CTX
*ctx
, int pkey_op
,
510 unsigned char *out
, size_t *poutlen
,
511 unsigned char *in
, size_t inlen
)
515 case EVP_PKEY_OP_VERIFYRECOVER
:
516 rv
= EVP_PKEY_verify_recover(ctx
, out
, poutlen
, in
, inlen
);
519 case EVP_PKEY_OP_SIGN
:
520 rv
= EVP_PKEY_sign(ctx
, out
, poutlen
, in
, inlen
);
523 case EVP_PKEY_OP_ENCRYPT
:
524 rv
= EVP_PKEY_encrypt(ctx
, out
, poutlen
, in
, inlen
);
527 case EVP_PKEY_OP_DECRYPT
:
528 rv
= EVP_PKEY_decrypt(ctx
, out
, poutlen
, in
, inlen
);
531 case EVP_PKEY_OP_DERIVE
:
532 rv
= EVP_PKEY_derive(ctx
, out
, poutlen
);