2 * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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,
25 const int impl, EVP_PKEY **ppkey);
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 static int do_raw_keyop(int pkey_op, EVP_PKEY_CTX *ctx,
35 const EVP_MD *md, EVP_PKEY *pkey, BIO *in,
36 unsigned char *sig, int siglen,
37 unsigned char **out, size_t *poutlen);
39 typedef enum OPTION_choice {
40 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
41 OPT_ENGINE, OPT_ENGINE_IMPL, OPT_IN, OPT_OUT,
42 OPT_PUBIN, OPT_CERTIN, OPT_ASN1PARSE, OPT_HEXDUMP, OPT_SIGN,
43 OPT_VERIFY, OPT_VERIFYRECOVER, OPT_REV, OPT_ENCRYPT, OPT_DECRYPT,
44 OPT_DERIVE, OPT_SIGFILE, OPT_INKEY, OPT_PEERKEY, OPT_PASSIN,
45 OPT_PEERFORM, OPT_KEYFORM, OPT_PKEYOPT, OPT_PKEYOPT_PASSIN, OPT_KDF,
46 OPT_KDFLEN, OPT_R_ENUM,
50 const OPTIONS pkeyutl_options[] = {
51 {"help", OPT_HELP, '-', "Display this summary"},
52 {"in", OPT_IN, '<', "Input file - default stdin"},
53 {"rawin", OPT_RAWIN, '-', "Indicate the input data is in raw form"},
54 {"digest", OPT_DIGEST, 's',
55 "Specify the digest algorithm when signing the raw input data"},
56 {"out", OPT_OUT, '>', "Output file - default stdout"},
57 {"pubin", OPT_PUBIN, '-', "Input is a public key"},
58 {"certin", OPT_CERTIN, '-', "Input is a cert with a public key"},
59 {"asn1parse", OPT_ASN1PARSE, '-', "asn1parse the output data"},
60 {"hexdump", OPT_HEXDUMP, '-', "Hex dump output"},
61 {"sign", OPT_SIGN, '-', "Sign input data with private key"},
62 {"verify", OPT_VERIFY, '-', "Verify with public key"},
63 {"verifyrecover", OPT_VERIFYRECOVER, '-',
64 "Verify with public key, recover original data"},
65 {"rev", OPT_REV, '-', "Reverse the order of the input buffer"},
66 {"encrypt", OPT_ENCRYPT, '-', "Encrypt input data with public key"},
67 {"decrypt", OPT_DECRYPT, '-', "Decrypt input data with private key"},
68 {"derive", OPT_DERIVE, '-', "Derive shared secret"},
69 {"kdf", OPT_KDF, 's', "Use KDF algorithm"},
70 {"kdflen", OPT_KDFLEN, 'p', "KDF algorithm output length"},
71 {"sigfile", OPT_SIGFILE, '<', "Signature file (verify operation only)"},
72 {"inkey", OPT_INKEY, 's', "Input private key file"},
73 {"peerkey", OPT_PEERKEY, 's', "Peer key file used in key derivation"},
74 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
75 {"peerform", OPT_PEERFORM, 'E', "Peer key format - default PEM"},
76 {"keyform", OPT_KEYFORM, 'E', "Private key format - default PEM"},
77 {"pkeyopt", OPT_PKEYOPT, 's', "Public key options as opt:value"},
78 {"pkeyopt_passin", OPT_PKEYOPT_PASSIN, 's',
79 "Public key option that is read as a passphrase argument opt:passphrase"},
81 #ifndef OPENSSL_NO_ENGINE
82 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
83 {"engine_impl", OPT_ENGINE_IMPL, '-',
84 "Also use engine given by -engine for crypto operations"},
89 int pkeyutl_main(int argc, char **argv)
91 BIO *in = NULL, *out = NULL;
93 EVP_PKEY_CTX *ctx = NULL;
94 EVP_PKEY *pkey = NULL;
95 char *infile = NULL, *outfile = NULL, *sigfile = NULL, *passinarg = NULL;
96 char hexdump = 0, asn1parse = 0, rev = 0, *prog;
97 unsigned char *buf_in = NULL, *buf_out = NULL, *sig = NULL;
99 int buf_inlen = 0, siglen = -1, keyform = FORMAT_PEM, peerform = FORMAT_PEM;
100 int keysize = -1, pkey_op = EVP_PKEY_OP_SIGN, key_type = KEY_PRIVKEY;
102 int ret = 1, rv = -1;
104 const char *inkey = NULL;
105 const char *peerkey = NULL;
106 const char *kdfalg = NULL;
108 STACK_OF(OPENSSL_STRING) *pkeyopts = NULL;
109 STACK_OF(OPENSSL_STRING) *pkeyopts_passin = NULL;
111 const EVP_MD *md = NULL;
113 prog = opt_init(argc, argv, pkeyutl_options);
114 while ((o = opt_next()) != OPT_EOF) {
119 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
122 opt_help(pkeyutl_options);
134 case OPT_ENGINE_IMPL:
144 passinarg = opt_arg();
147 if (!opt_format(opt_arg(), OPT_FMT_PDE, &peerform))
151 if (!opt_format(opt_arg(), OPT_FMT_PDE, &keyform))
159 e = setup_engine(opt_arg(), 0);
162 key_type = KEY_PUBKEY;
174 pkey_op = EVP_PKEY_OP_SIGN;
177 pkey_op = EVP_PKEY_OP_VERIFY;
179 case OPT_VERIFYRECOVER:
180 pkey_op = EVP_PKEY_OP_VERIFYRECOVER;
183 pkey_op = EVP_PKEY_OP_ENCRYPT;
186 pkey_op = EVP_PKEY_OP_DECRYPT;
189 pkey_op = EVP_PKEY_OP_DERIVE;
192 pkey_op = EVP_PKEY_OP_DERIVE;
197 kdflen = atoi(opt_arg());
203 if ((pkeyopts == NULL &&
204 (pkeyopts = sk_OPENSSL_STRING_new_null()) == NULL) ||
205 sk_OPENSSL_STRING_push(pkeyopts, opt_arg()) == 0) {
206 BIO_puts(bio_err, "out of memory\n");
210 case OPT_PKEYOPT_PASSIN:
211 if ((pkeyopts_passin == NULL &&
212 (pkeyopts_passin = sk_OPENSSL_STRING_new_null()) == NULL) ||
213 sk_OPENSSL_STRING_push(pkeyopts_passin, opt_arg()) == 0) {
214 BIO_puts(bio_err, "out of memory\n");
222 if (!opt_md(opt_arg(), &md))
227 argc = opt_num_rest();
231 if (rawin && pkey_op != EVP_PKEY_OP_SIGN && pkey_op != EVP_PKEY_OP_VERIFY) {
233 "%s: -rawin can only be used with -sign or -verify\n",
238 if (md != NULL && !rawin) {
240 "%s: -digest can only be used with -rawin\n",
246 BIO_printf(bio_err, "%s: -rev cannot be used with raw input\n",
251 if (kdfalg != NULL) {
254 "%s: no KDF length given (-kdflen parameter).\n", prog);
257 } else if (inkey == NULL) {
259 "%s: no private key given (-inkey parameter).\n", prog);
261 } else if (peerkey != NULL && pkey_op != EVP_PKEY_OP_DERIVE) {
263 "%s: no peer key given (-peerkey parameter).\n", prog);
266 ctx = init_ctx(kdfalg, &keysize, inkey, keyform, key_type,
267 passinarg, pkey_op, e, engine_impl, &pkey);
269 BIO_printf(bio_err, "%s: Error initializing context\n", prog);
270 ERR_print_errors(bio_err);
273 if (peerkey != NULL && !setup_peer(ctx, peerform, peerkey, e)) {
274 BIO_printf(bio_err, "%s: Error setting up peer key\n", prog);
275 ERR_print_errors(bio_err);
278 if (pkeyopts != NULL) {
279 int num = sk_OPENSSL_STRING_num(pkeyopts);
282 for (i = 0; i < num; ++i) {
283 const char *opt = sk_OPENSSL_STRING_value(pkeyopts, i);
285 if (pkey_ctrl_string(ctx, opt) <= 0) {
286 BIO_printf(bio_err, "%s: Can't set parameter \"%s\":\n",
288 ERR_print_errors(bio_err);
293 if (pkeyopts_passin != NULL) {
294 int num = sk_OPENSSL_STRING_num(pkeyopts_passin);
297 for (i = 0; i < num; i++) {
298 char *opt = sk_OPENSSL_STRING_value(pkeyopts_passin, i);
299 char *passin = strchr(opt, ':');
302 if (passin == NULL) {
303 /* Get password interactively */
304 char passwd_buf[4096];
305 BIO_snprintf(passwd_buf, sizeof(passwd_buf), "Enter %s: ", opt);
306 EVP_read_pw_string(passwd_buf, sizeof(passwd_buf) - 1,
308 passwd = OPENSSL_strdup(passwd_buf);
309 if (passwd == NULL) {
310 BIO_puts(bio_err, "out of memory\n");
314 /* Get password as a passin argument: First split option name
315 * and passphrase argument into two strings */
318 if (app_passwd(passin, NULL, &passwd, NULL) == 0) {
319 BIO_printf(bio_err, "failed to get '%s'\n", opt);
324 if (EVP_PKEY_CTX_ctrl_str(ctx, opt, passwd) <= 0) {
325 BIO_printf(bio_err, "%s: Can't set parameter \"%s\":\n",
329 OPENSSL_free(passwd);
333 if (sigfile != NULL && (pkey_op != EVP_PKEY_OP_VERIFY)) {
335 "%s: Signature file specified for non verify\n", prog);
339 if (sigfile == NULL && (pkey_op == EVP_PKEY_OP_VERIFY)) {
341 "%s: No signature file specified for verify\n", prog);
345 if (pkey_op != EVP_PKEY_OP_DERIVE) {
346 in = bio_open_default(infile, 'r', FORMAT_BINARY);
350 out = bio_open_default(outfile, 'w', FORMAT_BINARY);
354 if (sigfile != NULL) {
355 BIO *sigbio = BIO_new_file(sigfile, "rb");
357 if (sigbio == NULL) {
358 BIO_printf(bio_err, "Can't open signature file %s\n", sigfile);
361 siglen = bio_to_mem(&sig, keysize * 10, sigbio);
364 BIO_printf(bio_err, "Error reading signature data\n");
369 /* Raw input data is handled elsewhere */
370 if (in != NULL && !rawin) {
371 /* Read the input data */
372 buf_inlen = bio_to_mem(&buf_in, keysize * 10, in);
374 BIO_printf(bio_err, "Error reading input Data\n");
380 size_t l = (size_t)buf_inlen;
381 for (i = 0; i < l / 2; i++) {
383 buf_in[i] = buf_in[l - 1 - i];
384 buf_in[l - 1 - i] = ctmp;
389 /* Sanity check the input if the input is not raw */
391 && buf_inlen > EVP_MAX_MD_SIZE
392 && (pkey_op == EVP_PKEY_OP_SIGN
393 || pkey_op == EVP_PKEY_OP_VERIFY
394 || pkey_op == EVP_PKEY_OP_VERIFYRECOVER)) {
396 "Error: The input data looks too long to be a hash\n");
400 if (pkey_op == EVP_PKEY_OP_VERIFY) {
402 rv = do_raw_keyop(pkey_op, ctx, md, pkey, in, sig, siglen,
405 rv = EVP_PKEY_verify(ctx, sig, (size_t)siglen,
406 buf_in, (size_t)buf_inlen);
409 BIO_puts(out, "Signature Verified Successfully\n");
412 BIO_puts(out, "Signature Verification Failure\n");
421 /* rawin allocates the buffer in do_raw_keyop() */
422 rv = do_raw_keyop(pkey_op, ctx, md, pkey, in, NULL, 0,
423 &buf_out, (size_t *)&buf_outlen);
425 rv = do_keyop(ctx, pkey_op, NULL, (size_t *)&buf_outlen,
426 buf_in, (size_t)buf_inlen);
427 if (rv > 0 && buf_outlen != 0) {
428 buf_out = app_malloc(buf_outlen, "buffer output");
429 rv = do_keyop(ctx, pkey_op,
430 buf_out, (size_t *)&buf_outlen,
431 buf_in, (size_t)buf_inlen);
436 if (pkey_op != EVP_PKEY_OP_DERIVE) {
437 BIO_puts(bio_err, "Public Key operation error\n");
439 BIO_puts(bio_err, "Key derivation failed\n");
441 ERR_print_errors(bio_err);
447 if (!ASN1_parse_dump(out, buf_out, buf_outlen, 1, -1))
448 ERR_print_errors(bio_err);
449 } else if (hexdump) {
450 BIO_dump(out, (char *)buf_out, buf_outlen);
452 BIO_write(out, buf_out, buf_outlen);
456 EVP_PKEY_CTX_free(ctx);
460 OPENSSL_free(buf_in);
461 OPENSSL_free(buf_out);
463 sk_OPENSSL_STRING_free(pkeyopts);
464 sk_OPENSSL_STRING_free(pkeyopts_passin);
468 static EVP_PKEY_CTX *init_ctx(const char *kdfalg, int *pkeysize,
469 const char *keyfile, int keyform, int key_type,
470 char *passinarg, int pkey_op, ENGINE *e,
471 const int engine_impl, EVP_PKEY **ppkey)
473 EVP_PKEY *pkey = NULL;
474 EVP_PKEY_CTX *ctx = NULL;
479 if (((pkey_op == EVP_PKEY_OP_SIGN) || (pkey_op == EVP_PKEY_OP_DECRYPT)
480 || (pkey_op == EVP_PKEY_OP_DERIVE))
481 && (key_type != KEY_PRIVKEY && kdfalg == NULL)) {
482 BIO_printf(bio_err, "A private key is needed for this operation\n");
485 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
486 BIO_printf(bio_err, "Error getting password\n");
491 pkey = load_key(keyfile, keyform, 0, passin, e, "Private Key");
495 pkey = load_pubkey(keyfile, keyform, 0, NULL, e, "Public Key");
499 x = load_cert(keyfile, keyform, "Certificate");
501 pkey = X509_get_pubkey(x);
511 #ifndef OPENSSL_NO_ENGINE
516 if (kdfalg != NULL) {
517 int kdfnid = OBJ_sn2nid(kdfalg);
519 if (kdfnid == NID_undef) {
520 kdfnid = OBJ_ln2nid(kdfalg);
521 if (kdfnid == NID_undef) {
522 BIO_printf(bio_err, "The given KDF \"%s\" is unknown.\n",
527 ctx = EVP_PKEY_CTX_new_id(kdfnid, impl);
529 EC_KEY *eckey = NULL;
530 const EC_GROUP *group = NULL;
535 /* SM2 needs a special treatment */
536 if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) {
537 if ((eckey = EVP_PKEY_get0_EC_KEY(pkey)) == NULL
538 || (group = EC_KEY_get0_group(eckey)) == NULL
539 || (nid = EC_GROUP_get_curve_name(group)) == 0)
542 EVP_PKEY_set_alias_type(pkey, EVP_PKEY_SM2);
544 *pkeysize = EVP_PKEY_size(pkey);
545 ctx = EVP_PKEY_CTX_new(pkey, impl);
555 case EVP_PKEY_OP_SIGN:
556 rv = EVP_PKEY_sign_init(ctx);
559 case EVP_PKEY_OP_VERIFY:
560 rv = EVP_PKEY_verify_init(ctx);
563 case EVP_PKEY_OP_VERIFYRECOVER:
564 rv = EVP_PKEY_verify_recover_init(ctx);
567 case EVP_PKEY_OP_ENCRYPT:
568 rv = EVP_PKEY_encrypt_init(ctx);
571 case EVP_PKEY_OP_DECRYPT:
572 rv = EVP_PKEY_decrypt_init(ctx);
575 case EVP_PKEY_OP_DERIVE:
576 rv = EVP_PKEY_derive_init(ctx);
581 EVP_PKEY_CTX_free(ctx);
586 OPENSSL_free(passin);
591 static int setup_peer(EVP_PKEY_CTX *ctx, int peerform, const char *file,
594 EVP_PKEY *peer = NULL;
595 ENGINE *engine = NULL;
598 if (peerform == FORMAT_ENGINE)
600 peer = load_pubkey(file, peerform, 0, NULL, engine, "Peer Key");
602 BIO_printf(bio_err, "Error reading peer key %s\n", file);
603 ERR_print_errors(bio_err);
607 ret = EVP_PKEY_derive_set_peer(ctx, peer);
611 ERR_print_errors(bio_err);
615 static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
616 unsigned char *out, size_t *poutlen,
617 const unsigned char *in, size_t inlen)
621 case EVP_PKEY_OP_VERIFYRECOVER:
622 rv = EVP_PKEY_verify_recover(ctx, out, poutlen, in, inlen);
625 case EVP_PKEY_OP_SIGN:
626 rv = EVP_PKEY_sign(ctx, out, poutlen, in, inlen);
629 case EVP_PKEY_OP_ENCRYPT:
630 rv = EVP_PKEY_encrypt(ctx, out, poutlen, in, inlen);
633 case EVP_PKEY_OP_DECRYPT:
634 rv = EVP_PKEY_decrypt(ctx, out, poutlen, in, inlen);
637 case EVP_PKEY_OP_DERIVE:
638 rv = EVP_PKEY_derive(ctx, out, poutlen);
645 #define TBUF_MAXSIZE 2048
647 static int do_raw_keyop(int pkey_op, EVP_PKEY_CTX *ctx,
648 const EVP_MD *md, EVP_PKEY *pkey, BIO *in,
649 unsigned char *sig, int siglen,
650 unsigned char **out, size_t *poutlen)
653 EVP_MD_CTX *mctx = NULL;
654 unsigned char tbuf[TBUF_MAXSIZE];
657 if ((mctx = EVP_MD_CTX_new()) == NULL) {
658 BIO_printf(bio_err, "Error: out of memory\n");
661 EVP_MD_CTX_set_pkey_ctx(mctx, ctx);
664 case EVP_PKEY_OP_VERIFY:
665 if (EVP_DigestVerifyInit(mctx, NULL, md, NULL, pkey) != 1)
668 tbuf_len = BIO_read(in, tbuf, TBUF_MAXSIZE);
672 BIO_printf(bio_err, "Error reading raw input data\n");
675 rv = EVP_DigestVerifyUpdate(mctx, tbuf, (size_t)tbuf_len);
677 BIO_printf(bio_err, "Error verifying raw input data\n");
681 rv = EVP_DigestVerifyFinal(mctx, sig, (size_t)siglen);
683 case EVP_PKEY_OP_SIGN:
684 if (EVP_DigestSignInit(mctx, NULL, md, NULL, pkey) != 1)
687 tbuf_len = BIO_read(in, tbuf, TBUF_MAXSIZE);
691 BIO_printf(bio_err, "Error reading raw input data\n");
694 rv = EVP_DigestSignUpdate(mctx, tbuf, (size_t)tbuf_len);
696 BIO_printf(bio_err, "Error signing raw input data\n");
700 rv = EVP_DigestSignFinal(mctx, NULL, poutlen);
701 if (rv == 1 && out != NULL) {
702 *out = app_malloc(*poutlen, "buffer output");
703 rv = EVP_DigestSignFinal(mctx, *out, poutlen);
709 EVP_MD_CTX_free(mctx);