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 const char *keyfile, int keyform, int key_type,
71 char *passinarg, int pkey_op, ENGINE *e,
74 static int setup_peer(EVP_PKEY_CTX *ctx, int peerform, const char *file,
77 static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
78 unsigned char *out, size_t *poutlen,
79 unsigned char *in, size_t inlen);
81 typedef enum OPTION_choice {
82 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
83 OPT_ENGINE, OPT_ENGINE_IMPL, OPT_IN, OPT_OUT,
84 OPT_PUBIN, OPT_CERTIN, OPT_ASN1PARSE, OPT_HEXDUMP, OPT_SIGN,
85 OPT_VERIFY, OPT_VERIFYRECOVER, OPT_REV, OPT_ENCRYPT, OPT_DECRYPT,
86 OPT_DERIVE, OPT_SIGFILE, OPT_INKEY, OPT_PEERKEY, OPT_PASSIN,
87 OPT_PEERFORM, OPT_KEYFORM, OPT_PKEYOPT
90 OPTIONS pkeyutl_options[] = {
91 {"help", OPT_HELP, '-', "Display this summary"},
92 {"in", OPT_IN, '<', "Input file - default stdin"},
93 {"out", OPT_OUT, '>', "Output file - default stdout"},
94 {"pubin", OPT_PUBIN, '-', "Input is a public key"},
95 {"certin", OPT_CERTIN, '-', "Input is a cert with a public key"},
96 {"asn1parse", OPT_ASN1PARSE, '-', "asn1parse the output data"},
97 {"hexdump", OPT_HEXDUMP, '-', "Hex dump output"},
98 {"sign", OPT_SIGN, '-', "Sign input data with private key"},
99 {"verify", OPT_VERIFY, '-', "Verify with public key"},
100 {"verifyrecover", OPT_VERIFYRECOVER, '-',
101 "Verify with public key, recover original data"},
102 {"rev", OPT_REV, '-', "Reverse the order of the input buffer"},
103 {"encrypt", OPT_ENCRYPT, '-', "Encrypt input data with public key"},
104 {"decrypt", OPT_DECRYPT, '-', "Decrypt input data with private key"},
105 {"derive", OPT_DERIVE, '-', "Derive shared secret"},
106 {"sigfile", OPT_SIGFILE, '<', "Signature file (verify operation only)"},
107 {"inkey", OPT_INKEY, 's', "Input private key file"},
108 {"peerkey", OPT_PEERKEY, 's', "Peer key file used in key derivation"},
109 {"passin", OPT_PASSIN, 's', "Pass phrase source"},
110 {"peerform", OPT_PEERFORM, 'E', "Peer key format - default PEM"},
111 {"keyform", OPT_KEYFORM, 'E', "Private key format - default PEM"},
112 {"pkeyopt", OPT_PKEYOPT, 's', "Public key options as opt:value"},
113 #ifndef OPENSSL_NO_ENGINE
114 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
115 {"engine_impl", OPT_ENGINE_IMPL, '-',
116 "Also use engine given by -engine for crypto operations"},
121 int pkeyutl_main(int argc, char **argv)
123 BIO *in = NULL, *out = NULL;
125 EVP_PKEY_CTX *ctx = NULL;
126 char *infile = NULL, *outfile = NULL, *sigfile = NULL, *passinarg = NULL;
127 char hexdump = 0, asn1parse = 0, rev = 0, *prog;
128 unsigned char *buf_in = NULL, *buf_out = NULL, *sig = NULL;
130 int buf_inlen = 0, siglen = -1, keyform = FORMAT_PEM, peerform =
132 int keysize = -1, pkey_op = EVP_PKEY_OP_SIGN, key_type = KEY_PRIVKEY;
134 int ret = 1, rv = -1;
136 const char *inkey = NULL;
137 const char *peerkey = NULL;
138 STACK_OF(OPENSSL_STRING) *pkeyopts = NULL;
140 prog = opt_init(argc, argv, pkeyutl_options);
141 while ((o = opt_next()) != OPT_EOF) {
146 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
149 opt_help(pkeyutl_options);
161 case OPT_ENGINE_IMPL:
171 passinarg = opt_arg();
174 if (!opt_format(opt_arg(), OPT_FMT_PDE, &peerform))
178 if (!opt_format(opt_arg(), OPT_FMT_PDE, &keyform))
182 e = setup_engine(opt_arg(), 0);
185 key_type = KEY_PUBKEY;
197 pkey_op = EVP_PKEY_OP_SIGN;
200 pkey_op = EVP_PKEY_OP_VERIFY;
202 case OPT_VERIFYRECOVER:
203 pkey_op = EVP_PKEY_OP_VERIFYRECOVER;
206 pkey_op = EVP_PKEY_OP_ENCRYPT;
209 pkey_op = EVP_PKEY_OP_DECRYPT;
212 pkey_op = EVP_PKEY_OP_DERIVE;
218 if ((pkeyopts == NULL &&
219 (pkeyopts = sk_OPENSSL_STRING_new_null()) == NULL) ||
220 sk_OPENSSL_STRING_push(pkeyopts, opt_arg()) == 0) {
221 BIO_puts(bio_err, "out of memory\n");
227 argc = opt_num_rest();
232 (peerkey != NULL && pkey_op != EVP_PKEY_OP_DERIVE))
235 ctx = init_ctx(&keysize, inkey, keyform, key_type,
236 passinarg, pkey_op, e, engine_impl);
238 BIO_printf(bio_err, "%s: Error initializing context\n", prog);
239 ERR_print_errors(bio_err);
242 if (peerkey != NULL && !setup_peer(ctx, peerform, peerkey, e)) {
243 BIO_printf(bio_err, "%s: Error setting up peer key\n", prog);
244 ERR_print_errors(bio_err);
247 if (pkeyopts != NULL) {
248 int num = sk_OPENSSL_STRING_num(pkeyopts);
251 for (i = 0; i < num; ++i) {
252 const char *opt = sk_OPENSSL_STRING_value(pkeyopts, i);
254 if (pkey_ctrl_string(ctx, opt) <= 0) {
255 BIO_printf(bio_err, "%s: Can't set parameter:\n", prog);
256 ERR_print_errors(bio_err);
262 if (sigfile && (pkey_op != EVP_PKEY_OP_VERIFY)) {
264 "%s: Signature file specified for non verify\n", prog);
268 if (!sigfile && (pkey_op == EVP_PKEY_OP_VERIFY)) {
270 "%s: No signature file specified for verify\n", prog);
274 /* FIXME: seed PRNG only if needed */
275 app_RAND_load_file(NULL, 0);
277 if (pkey_op != EVP_PKEY_OP_DERIVE) {
278 in = bio_open_default(infile, 'r', FORMAT_BINARY);
282 out = bio_open_default(outfile, 'w', FORMAT_BINARY);
287 BIO *sigbio = BIO_new_file(sigfile, "rb");
289 BIO_printf(bio_err, "Can't open signature file %s\n", sigfile);
292 siglen = bio_to_mem(&sig, keysize * 10, sigbio);
295 BIO_printf(bio_err, "Error reading signature data\n");
301 /* Read the input data */
302 buf_inlen = bio_to_mem(&buf_in, keysize * 10, in);
304 BIO_printf(bio_err, "Error reading input Data\n");
310 size_t l = (size_t)buf_inlen;
311 for (i = 0; i < l / 2; i++) {
313 buf_in[i] = buf_in[l - 1 - i];
314 buf_in[l - 1 - i] = ctmp;
319 if (pkey_op == EVP_PKEY_OP_VERIFY) {
320 rv = EVP_PKEY_verify(ctx, sig, (size_t)siglen,
321 buf_in, (size_t)buf_inlen);
323 BIO_puts(out, "Signature Verified Successfully\n");
326 BIO_puts(out, "Signature Verification Failure\n");
329 rv = do_keyop(ctx, pkey_op, NULL, (size_t *)&buf_outlen,
330 buf_in, (size_t)buf_inlen);
331 if (rv > 0 && buf_outlen != 0) {
332 buf_out = app_malloc(buf_outlen, "buffer output");
333 rv = do_keyop(ctx, pkey_op,
334 buf_out, (size_t *)&buf_outlen,
335 buf_in, (size_t)buf_inlen);
338 BIO_puts(bio_err, "Public Key operation error\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);
348 BIO_dump(out, (char *)buf_out, buf_outlen);
350 BIO_write(out, buf_out, buf_outlen);
353 EVP_PKEY_CTX_free(ctx);
356 OPENSSL_free(buf_in);
357 OPENSSL_free(buf_out);
359 sk_OPENSSL_STRING_free(pkeyopts);
363 static EVP_PKEY_CTX *init_ctx(int *pkeysize,
364 const char *keyfile, int keyform, int key_type,
365 char *passinarg, int pkey_op, ENGINE *e,
366 const int engine_impl)
368 EVP_PKEY *pkey = NULL;
369 EVP_PKEY_CTX *ctx = NULL;
374 if (((pkey_op == EVP_PKEY_OP_SIGN) || (pkey_op == EVP_PKEY_OP_DECRYPT)
375 || (pkey_op == EVP_PKEY_OP_DERIVE))
376 && (key_type != KEY_PRIVKEY)) {
377 BIO_printf(bio_err, "A private key is needed for this operation\n");
380 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
381 BIO_printf(bio_err, "Error getting password\n");
386 pkey = load_key(keyfile, keyform, 0, passin, e, "Private Key");
390 pkey = load_pubkey(keyfile, keyform, 0, NULL, e, "Public Key");
394 x = load_cert(keyfile, keyform, "Certificate");
396 pkey = X509_get_pubkey(x);
403 *pkeysize = EVP_PKEY_size(pkey);
408 #ifndef OPENSSL_NO_ENGINE
413 ctx = EVP_PKEY_CTX_new(pkey, impl);
421 case EVP_PKEY_OP_SIGN:
422 rv = EVP_PKEY_sign_init(ctx);
425 case EVP_PKEY_OP_VERIFY:
426 rv = EVP_PKEY_verify_init(ctx);
429 case EVP_PKEY_OP_VERIFYRECOVER:
430 rv = EVP_PKEY_verify_recover_init(ctx);
433 case EVP_PKEY_OP_ENCRYPT:
434 rv = EVP_PKEY_encrypt_init(ctx);
437 case EVP_PKEY_OP_DECRYPT:
438 rv = EVP_PKEY_decrypt_init(ctx);
441 case EVP_PKEY_OP_DERIVE:
442 rv = EVP_PKEY_derive_init(ctx);
447 EVP_PKEY_CTX_free(ctx);
452 OPENSSL_free(passin);
457 static int setup_peer(EVP_PKEY_CTX *ctx, int peerform, const char *file,
460 EVP_PKEY *peer = NULL;
461 ENGINE* engine = NULL;
464 if (peerform == FORMAT_ENGINE)
466 peer = load_pubkey(file, peerform, 0, NULL, engine, "Peer Key");
468 BIO_printf(bio_err, "Error reading peer key %s\n", file);
469 ERR_print_errors(bio_err);
473 ret = EVP_PKEY_derive_set_peer(ctx, peer);
477 ERR_print_errors(bio_err);
481 static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
482 unsigned char *out, size_t *poutlen,
483 unsigned char *in, size_t inlen)
487 case EVP_PKEY_OP_VERIFYRECOVER:
488 rv = EVP_PKEY_verify_recover(ctx, out, poutlen, in, inlen);
491 case EVP_PKEY_OP_SIGN:
492 rv = EVP_PKEY_sign(ctx, out, poutlen, in, inlen);
495 case EVP_PKEY_OP_ENCRYPT:
496 rv = EVP_PKEY_encrypt(ctx, out, poutlen, in, inlen);
499 case EVP_PKEY_OP_DECRYPT:
500 rv = EVP_PKEY_decrypt(ctx, out, poutlen, in, inlen);
503 case EVP_PKEY_OP_DERIVE:
504 rv = EVP_PKEY_derive(ctx, out, poutlen);