2 * Copyright 2006-2016 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
12 #include <openssl/err.h>
13 #include <openssl/pem.h>
14 #include <openssl/evp.h>
21 static EVP_PKEY_CTX *init_ctx(const char *kdfalg, int *pkeysize,
22 const char *keyfile, int keyform, int key_type,
23 char *passinarg, int pkey_op, ENGINE *e,
26 static int setup_peer(EVP_PKEY_CTX *ctx, int peerform, const char *file,
29 static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
30 unsigned char *out, size_t *poutlen,
31 const unsigned char *in, size_t inlen);
33 typedef enum OPTION_choice {
34 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
35 OPT_ENGINE, OPT_ENGINE_IMPL, OPT_IN, OPT_OUT,
36 OPT_PUBIN, OPT_CERTIN, OPT_ASN1PARSE, OPT_HEXDUMP, OPT_SIGN,
37 OPT_VERIFY, OPT_VERIFYRECOVER, OPT_REV, OPT_ENCRYPT, OPT_DECRYPT,
38 OPT_DERIVE, OPT_SIGFILE, OPT_INKEY, OPT_PEERKEY, OPT_PASSIN,
39 OPT_PEERFORM, OPT_KEYFORM, OPT_PKEYOPT, OPT_KDF, OPT_KDFLEN
42 OPTIONS pkeyutl_options[] = {
43 {"help", OPT_HELP, '-', "Display this summary"},
44 {"in", OPT_IN, '<', "Input file - default stdin"},
45 {"out", OPT_OUT, '>', "Output file - default stdout"},
46 {"pubin", OPT_PUBIN, '-', "Input is a public key"},
47 {"certin", OPT_CERTIN, '-', "Input is a cert with a public key"},
48 {"asn1parse", OPT_ASN1PARSE, '-', "asn1parse the output data"},
49 {"hexdump", OPT_HEXDUMP, '-', "Hex dump output"},
50 {"sign", OPT_SIGN, '-', "Sign input data with private key"},
51 {"verify", OPT_VERIFY, '-', "Verify with public key"},
52 {"verifyrecover", OPT_VERIFYRECOVER, '-',
53 "Verify with public key, recover original data"},
54 {"rev", OPT_REV, '-', "Reverse the order of the input buffer"},
55 {"encrypt", OPT_ENCRYPT, '-', "Encrypt input data with public key"},
56 {"decrypt", OPT_DECRYPT, '-', "Decrypt input data with private key"},
57 {"derive", OPT_DERIVE, '-', "Derive shared secret"},
58 {"kdf", OPT_KDF, 's', "Use KDF algorithm"},
59 {"kdflen", OPT_KDFLEN, 'p', "KDF algorithm output length"},
60 {"sigfile", OPT_SIGFILE, '<', "Signature file (verify operation only)"},
61 {"inkey", OPT_INKEY, 's', "Input private key file"},
62 {"peerkey", OPT_PEERKEY, 's', "Peer key file used in key derivation"},
63 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
64 {"peerform", OPT_PEERFORM, 'E', "Peer key format - default PEM"},
65 {"keyform", OPT_KEYFORM, 'E', "Private key format - default PEM"},
66 {"pkeyopt", OPT_PKEYOPT, 's', "Public key options as opt:value"},
67 #ifndef OPENSSL_NO_ENGINE
68 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
69 {"engine_impl", OPT_ENGINE_IMPL, '-',
70 "Also use engine given by -engine for crypto operations"},
75 int pkeyutl_main(int argc, char **argv)
77 BIO *in = NULL, *out = NULL;
79 EVP_PKEY_CTX *ctx = NULL;
80 char *infile = NULL, *outfile = NULL, *sigfile = NULL, *passinarg = NULL;
81 char hexdump = 0, asn1parse = 0, rev = 0, *prog;
82 unsigned char *buf_in = NULL, *buf_out = NULL, *sig = NULL;
84 int buf_inlen = 0, siglen = -1, keyform = FORMAT_PEM, peerform =
86 int keysize = -1, pkey_op = EVP_PKEY_OP_SIGN, key_type = KEY_PRIVKEY;
90 const char *inkey = NULL;
91 const char *peerkey = NULL;
92 const char *kdfalg = NULL;
94 STACK_OF(OPENSSL_STRING) *pkeyopts = NULL;
96 prog = opt_init(argc, argv, pkeyutl_options);
97 while ((o = opt_next()) != OPT_EOF) {
102 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
105 opt_help(pkeyutl_options);
117 case OPT_ENGINE_IMPL:
127 passinarg = opt_arg();
130 if (!opt_format(opt_arg(), OPT_FMT_PDE, &peerform))
134 if (!opt_format(opt_arg(), OPT_FMT_PDE, &keyform))
138 e = setup_engine(opt_arg(), 0);
141 key_type = KEY_PUBKEY;
153 pkey_op = EVP_PKEY_OP_SIGN;
156 pkey_op = EVP_PKEY_OP_VERIFY;
158 case OPT_VERIFYRECOVER:
159 pkey_op = EVP_PKEY_OP_VERIFYRECOVER;
162 pkey_op = EVP_PKEY_OP_ENCRYPT;
165 pkey_op = EVP_PKEY_OP_DECRYPT;
168 pkey_op = EVP_PKEY_OP_DERIVE;
171 pkey_op = EVP_PKEY_OP_DERIVE;
176 kdflen = atoi(opt_arg());
182 if ((pkeyopts == NULL &&
183 (pkeyopts = sk_OPENSSL_STRING_new_null()) == NULL) ||
184 sk_OPENSSL_STRING_push(pkeyopts, opt_arg()) == 0) {
185 BIO_puts(bio_err, "out of memory\n");
191 argc = opt_num_rest();
195 if (kdfalg != NULL) {
198 } else if ((inkey == NULL)
199 || (peerkey != NULL && pkey_op != EVP_PKEY_OP_DERIVE)) {
202 ctx = init_ctx(kdfalg, &keysize, inkey, keyform, key_type,
203 passinarg, pkey_op, e, engine_impl);
205 BIO_printf(bio_err, "%s: Error initializing context\n", prog);
206 ERR_print_errors(bio_err);
209 if (peerkey != NULL && !setup_peer(ctx, peerform, peerkey, e)) {
210 BIO_printf(bio_err, "%s: Error setting up peer key\n", prog);
211 ERR_print_errors(bio_err);
214 if (pkeyopts != NULL) {
215 int num = sk_OPENSSL_STRING_num(pkeyopts);
218 for (i = 0; i < num; ++i) {
219 const char *opt = sk_OPENSSL_STRING_value(pkeyopts, i);
221 if (pkey_ctrl_string(ctx, opt) <= 0) {
222 BIO_printf(bio_err, "%s: Can't set parameter:\n", prog);
223 ERR_print_errors(bio_err);
229 if (sigfile && (pkey_op != EVP_PKEY_OP_VERIFY)) {
231 "%s: Signature file specified for non verify\n", prog);
235 if (!sigfile && (pkey_op == EVP_PKEY_OP_VERIFY)) {
237 "%s: No signature file specified for verify\n", prog);
241 /* FIXME: seed PRNG only if needed */
242 app_RAND_load_file(NULL, 0);
244 if (pkey_op != EVP_PKEY_OP_DERIVE) {
245 in = bio_open_default(infile, 'r', FORMAT_BINARY);
249 out = bio_open_default(outfile, 'w', FORMAT_BINARY);
254 BIO *sigbio = BIO_new_file(sigfile, "rb");
256 BIO_printf(bio_err, "Can't open signature file %s\n", sigfile);
259 siglen = bio_to_mem(&sig, keysize * 10, sigbio);
262 BIO_printf(bio_err, "Error reading signature data\n");
268 /* Read the input data */
269 buf_inlen = bio_to_mem(&buf_in, keysize * 10, in);
271 BIO_printf(bio_err, "Error reading input Data\n");
277 size_t l = (size_t)buf_inlen;
278 for (i = 0; i < l / 2; i++) {
280 buf_in[i] = buf_in[l - 1 - i];
281 buf_in[l - 1 - i] = ctmp;
286 if (pkey_op == EVP_PKEY_OP_VERIFY) {
287 rv = EVP_PKEY_verify(ctx, sig, (size_t)siglen,
288 buf_in, (size_t)buf_inlen);
290 BIO_puts(out, "Signature Verified Successfully\n");
293 BIO_puts(out, "Signature Verification Failure\n");
300 rv = do_keyop(ctx, pkey_op, NULL, (size_t *)&buf_outlen,
301 buf_in, (size_t)buf_inlen);
303 if (rv > 0 && buf_outlen != 0) {
304 buf_out = app_malloc(buf_outlen, "buffer output");
305 rv = do_keyop(ctx, pkey_op,
306 buf_out, (size_t *)&buf_outlen,
307 buf_in, (size_t)buf_inlen);
310 BIO_puts(bio_err, "Public Key operation error\n");
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);
329 OPENSSL_free(buf_in);
330 OPENSSL_free(buf_out);
332 sk_OPENSSL_STRING_free(pkeyopts);
336 static EVP_PKEY_CTX *init_ctx(const char *kdfalg, int *pkeysize,
337 const char *keyfile, int keyform, int key_type,
338 char *passinarg, int pkey_op, ENGINE *e,
339 const int engine_impl)
341 EVP_PKEY *pkey = NULL;
342 EVP_PKEY_CTX *ctx = NULL;
347 if (((pkey_op == EVP_PKEY_OP_SIGN) || (pkey_op == EVP_PKEY_OP_DECRYPT)
348 || (pkey_op == EVP_PKEY_OP_DERIVE))
349 && (key_type != KEY_PRIVKEY && kdfalg == NULL)) {
350 BIO_printf(bio_err, "A private key is needed for this operation\n");
353 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
354 BIO_printf(bio_err, "Error getting password\n");
359 pkey = load_key(keyfile, keyform, 0, passin, e, "Private Key");
363 pkey = load_pubkey(keyfile, keyform, 0, NULL, e, "Public Key");
367 x = load_cert(keyfile, keyform, "Certificate");
369 pkey = X509_get_pubkey(x);
379 #ifndef OPENSSL_NO_ENGINE
385 int kdfnid = OBJ_sn2nid(kdfalg);
386 if (kdfnid == NID_undef)
388 ctx = EVP_PKEY_CTX_new_id(kdfnid, impl);
392 *pkeysize = EVP_PKEY_size(pkey);
393 ctx = EVP_PKEY_CTX_new(pkey, impl);
401 case EVP_PKEY_OP_SIGN:
402 rv = EVP_PKEY_sign_init(ctx);
405 case EVP_PKEY_OP_VERIFY:
406 rv = EVP_PKEY_verify_init(ctx);
409 case EVP_PKEY_OP_VERIFYRECOVER:
410 rv = EVP_PKEY_verify_recover_init(ctx);
413 case EVP_PKEY_OP_ENCRYPT:
414 rv = EVP_PKEY_encrypt_init(ctx);
417 case EVP_PKEY_OP_DECRYPT:
418 rv = EVP_PKEY_decrypt_init(ctx);
421 case EVP_PKEY_OP_DERIVE:
422 rv = EVP_PKEY_derive_init(ctx);
427 EVP_PKEY_CTX_free(ctx);
432 OPENSSL_free(passin);
437 static int setup_peer(EVP_PKEY_CTX *ctx, int peerform, const char *file,
440 EVP_PKEY *peer = NULL;
441 ENGINE* engine = NULL;
444 if (peerform == FORMAT_ENGINE)
446 peer = load_pubkey(file, peerform, 0, NULL, engine, "Peer Key");
448 BIO_printf(bio_err, "Error reading peer key %s\n", file);
449 ERR_print_errors(bio_err);
453 ret = EVP_PKEY_derive_set_peer(ctx, peer);
457 ERR_print_errors(bio_err);
461 static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
462 unsigned char *out, size_t *poutlen,
463 const unsigned char *in, size_t inlen)
467 case EVP_PKEY_OP_VERIFYRECOVER:
468 rv = EVP_PKEY_verify_recover(ctx, out, poutlen, in, inlen);
471 case EVP_PKEY_OP_SIGN:
472 rv = EVP_PKEY_sign(ctx, out, poutlen, in, inlen);
475 case EVP_PKEY_OP_ENCRYPT:
476 rv = EVP_PKEY_encrypt(ctx, out, poutlen, in, inlen);
479 case EVP_PKEY_OP_DECRYPT:
480 rv = EVP_PKEY_decrypt(ctx, out, poutlen, in, inlen);
483 case EVP_PKEY_OP_DERIVE:
484 rv = EVP_PKEY_derive(ctx, out, poutlen);