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>
69 static EVP_PKEY_CTX
*init_ctx(int *pkeysize
,
70 char *keyfile
, int keyform
, int key_type
,
71 char *passinarg
, int pkey_op
, ENGINE
*e
);
73 static int setup_peer(EVP_PKEY_CTX
*ctx
, int peerform
, const char *file
);
75 static int do_keyop(EVP_PKEY_CTX
*ctx
, int pkey_op
,
76 unsigned char *out
, size_t *poutlen
,
77 unsigned char *in
, size_t inlen
);
79 typedef enum OPTION_choice
{
80 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
81 OPT_ENGINE
, OPT_IN
, OPT_OUT
,
82 OPT_PUBIN
, OPT_CERTIN
, OPT_ASN1PARSE
, OPT_HEXDUMP
, OPT_SIGN
,
83 OPT_VERIFY
, OPT_VERIFYRECOVER
, OPT_REV
, OPT_ENCRYPT
, OPT_DECRYPT
,
84 OPT_DERIVE
, OPT_SIGFILE
, OPT_INKEY
, OPT_PEERKEY
, OPT_PASSIN
,
85 OPT_PEERFORM
, OPT_KEYFORM
, OPT_PKEYOPT
88 OPTIONS pkeyutl_options
[] = {
89 {"help", OPT_HELP
, '-', "Display this summary"},
90 {"in", OPT_IN
, '<', "Input file"},
91 {"out", OPT_OUT
, '>', "Output file"},
92 {"pubin", OPT_PUBIN
, '-', "Input is a public key"},
93 {"certin", OPT_CERTIN
, '-', "Input is a cert with a public key"},
94 {"asn1parse", OPT_ASN1PARSE
, '-'},
95 {"hexdump", OPT_HEXDUMP
, '-', "Hex dump output"},
96 {"sign", OPT_SIGN
, '-', "Sign with private key"},
97 {"verify", OPT_VERIFY
, '-', "Verify with public key"},
98 {"verifyrecover", OPT_VERIFYRECOVER
, '-',
99 "Verify with public key, recover original data"},
100 {"rev", OPT_REV
, '-'},
101 {"encrypt", OPT_ENCRYPT
, '-', "Encrypt with public key"},
102 {"decrypt", OPT_DECRYPT
, '-', "Decrypt with private key"},
103 {"derive", OPT_DERIVE
, '-', "Derive shared secret"},
104 {"sigfile", OPT_SIGFILE
, '<', "Signature file (verify operation only)"},
105 {"inkey", OPT_INKEY
, 's', "Input key"},
106 {"peerkey", OPT_PEERKEY
, 's'},
107 {"passin", OPT_PASSIN
, 's', "Pass phrase source"},
108 {"peerform", OPT_PEERFORM
, 'F'},
109 {"keyform", OPT_KEYFORM
, 'F', "Private key format - default PEM"},
110 {"pkeyopt", OPT_PKEYOPT
, 's', "Public key options as opt:value"},
111 #ifndef OPENSSL_NO_ENGINE
112 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
117 int pkeyutl_main(int argc
, char **argv
)
119 BIO
*in
= NULL
, *out
= NULL
;
121 EVP_PKEY_CTX
*ctx
= NULL
;
122 char *infile
= NULL
, *outfile
= NULL
, *sigfile
= NULL
, *passinarg
= NULL
;
123 char hexdump
= 0, asn1parse
= 0, rev
= 0, *prog
;
124 unsigned char *buf_in
= NULL
, *buf_out
= NULL
, *sig
= NULL
;
126 int buf_inlen
= 0, siglen
= -1, keyform
= FORMAT_PEM
, peerform
=
128 int keysize
= -1, pkey_op
= EVP_PKEY_OP_SIGN
, key_type
= KEY_PRIVKEY
;
129 int ret
= 1, rv
= -1;
132 prog
= opt_init(argc
, argv
, pkeyutl_options
);
133 while ((o
= opt_next()) != OPT_EOF
) {
138 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
141 opt_help(pkeyutl_options
);
154 ctx
= init_ctx(&keysize
, opt_arg(), keyform
, key_type
,
155 passinarg
, pkey_op
, e
);
157 BIO_puts(bio_err
, "%s: Error initializing context\n");
158 ERR_print_errors(bio_err
);
163 if (!setup_peer(ctx
, peerform
, opt_arg()))
167 passinarg
= opt_arg();
170 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &peerform
))
174 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &keyform
))
178 e
= setup_engine(opt_arg(), 0);
181 key_type
= KEY_PUBKEY
;
193 pkey_op
= EVP_PKEY_OP_SIGN
;
196 pkey_op
= EVP_PKEY_OP_VERIFY
;
198 case OPT_VERIFYRECOVER
:
199 pkey_op
= EVP_PKEY_OP_VERIFYRECOVER
;
204 pkey_op
= EVP_PKEY_OP_ENCRYPT
;
207 pkey_op
= EVP_PKEY_OP_DECRYPT
;
210 pkey_op
= EVP_PKEY_OP_DERIVE
;
215 "%s: Must have -inkey before -pkeyopt\n", prog
);
218 if (pkey_ctrl_string(ctx
, opt_arg()) <= 0) {
219 BIO_printf(bio_err
, "%s: Can't set parameter:\n", prog
);
220 ERR_print_errors(bio_err
);
226 argc
= opt_num_rest();
232 if (!app_load_modules(NULL
))
235 if (sigfile
&& (pkey_op
!= EVP_PKEY_OP_VERIFY
)) {
237 "%s: Signature file specified for non verify\n", prog
);
241 if (!sigfile
&& (pkey_op
== EVP_PKEY_OP_VERIFY
)) {
243 "%s: No signature file specified for verify\n", prog
);
247 /* FIXME: seed PRNG only if needed */
248 app_RAND_load_file(NULL
, 0);
250 if (pkey_op
!= EVP_PKEY_OP_DERIVE
) {
251 in
= bio_open_default(infile
, "rb");
255 out
= bio_open_default(outfile
, "wb");
260 BIO
*sigbio
= BIO_new_file(sigfile
, "rb");
262 BIO_printf(bio_err
, "Can't open signature file %s\n", sigfile
);
265 siglen
= bio_to_mem(&sig
, keysize
* 10, sigbio
);
268 BIO_printf(bio_err
, "Error reading signature data\n");
274 /* Read the input data */
275 buf_inlen
= bio_to_mem(&buf_in
, keysize
* 10, in
);
276 if (buf_inlen
<= 0) {
277 BIO_printf(bio_err
, "Error reading input Data\n");
283 size_t l
= (size_t)buf_inlen
;
284 for (i
= 0; i
< l
/ 2; i
++) {
286 buf_in
[i
] = buf_in
[l
- 1 - i
];
287 buf_in
[l
- 1 - i
] = ctmp
;
292 if (pkey_op
== EVP_PKEY_OP_VERIFY
) {
293 rv
= EVP_PKEY_verify(ctx
, sig
, (size_t)siglen
,
294 buf_in
, (size_t)buf_inlen
);
296 BIO_puts(out
, "Signature Verified Successfully\n");
299 BIO_puts(out
, "Signature Verification Failure\n");
302 rv
= do_keyop(ctx
, pkey_op
, NULL
, (size_t *)&buf_outlen
,
303 buf_in
, (size_t)buf_inlen
);
305 buf_out
= app_malloc(buf_outlen
, "buffer output");
306 rv
= do_keyop(ctx
, pkey_op
,
307 buf_out
, (size_t *)&buf_outlen
,
308 buf_in
, (size_t)buf_inlen
);
311 ERR_print_errors(bio_err
);
317 if (!ASN1_parse_dump(out
, buf_out
, buf_outlen
, 1, -1))
318 ERR_print_errors(bio_err
);
320 BIO_dump(out
, (char *)buf_out
, buf_outlen
);
322 BIO_write(out
, buf_out
, buf_outlen
);
325 EVP_PKEY_CTX_free(ctx
);
328 OPENSSL_free(buf_in
);
329 OPENSSL_free(buf_out
);
334 static EVP_PKEY_CTX
*init_ctx(int *pkeysize
,
335 char *keyfile
, int keyform
, int key_type
,
336 char *passinarg
, int pkey_op
, ENGINE
*e
)
338 EVP_PKEY
*pkey
= NULL
;
339 EVP_PKEY_CTX
*ctx
= NULL
;
343 if (((pkey_op
== EVP_PKEY_OP_SIGN
) || (pkey_op
== EVP_PKEY_OP_DECRYPT
)
344 || (pkey_op
== EVP_PKEY_OP_DERIVE
))
345 && (key_type
!= KEY_PRIVKEY
)) {
346 BIO_printf(bio_err
, "A private key is needed for this operation\n");
349 if (!app_passwd(passinarg
, NULL
, &passin
, NULL
)) {
350 BIO_printf(bio_err
, "Error getting password\n");
355 pkey
= load_key(keyfile
, keyform
, 0, passin
, e
, "Private Key");
359 pkey
= load_pubkey(keyfile
, keyform
, 0, NULL
, e
, "Public Key");
363 x
= load_cert(keyfile
, keyform
, NULL
, e
, "Certificate");
365 pkey
= X509_get_pubkey(x
);
372 *pkeysize
= EVP_PKEY_size(pkey
);
377 ctx
= EVP_PKEY_CTX_new(pkey
, e
);
385 case EVP_PKEY_OP_SIGN
:
386 rv
= EVP_PKEY_sign_init(ctx
);
389 case EVP_PKEY_OP_VERIFY
:
390 rv
= EVP_PKEY_verify_init(ctx
);
393 case EVP_PKEY_OP_VERIFYRECOVER
:
394 rv
= EVP_PKEY_verify_recover_init(ctx
);
397 case EVP_PKEY_OP_ENCRYPT
:
398 rv
= EVP_PKEY_encrypt_init(ctx
);
401 case EVP_PKEY_OP_DECRYPT
:
402 rv
= EVP_PKEY_decrypt_init(ctx
);
405 case EVP_PKEY_OP_DERIVE
:
406 rv
= EVP_PKEY_derive_init(ctx
);
411 EVP_PKEY_CTX_free(ctx
);
416 OPENSSL_free(passin
);
421 static int setup_peer(EVP_PKEY_CTX
*ctx
, int peerform
, const char *file
)
423 EVP_PKEY
*peer
= NULL
;
426 BIO_puts(bio_err
, "-peerkey command before -inkey\n");
430 peer
= load_pubkey(file
, peerform
, 0, NULL
, NULL
, "Peer Key");
433 BIO_printf(bio_err
, "Error reading peer key %s\n", file
);
434 ERR_print_errors(bio_err
);
438 ret
= EVP_PKEY_derive_set_peer(ctx
, peer
);
442 ERR_print_errors(bio_err
);
446 static int do_keyop(EVP_PKEY_CTX
*ctx
, int pkey_op
,
447 unsigned char *out
, size_t *poutlen
,
448 unsigned char *in
, size_t inlen
)
452 case EVP_PKEY_OP_VERIFYRECOVER
:
453 rv
= EVP_PKEY_verify_recover(ctx
, out
, poutlen
, in
, inlen
);
456 case EVP_PKEY_OP_SIGN
:
457 rv
= EVP_PKEY_sign(ctx
, out
, poutlen
, in
, inlen
);
460 case EVP_PKEY_OP_ENCRYPT
:
461 rv
= EVP_PKEY_encrypt(ctx
, out
, poutlen
, in
, inlen
);
464 case EVP_PKEY_OP_DECRYPT
:
465 rv
= EVP_PKEY_decrypt(ctx
, out
, poutlen
, in
, inlen
);
468 case EVP_PKEY_OP_DERIVE
:
469 rv
= EVP_PKEY_derive(ctx
, out
, poutlen
);