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>
23 static EVP_PKEY_CTX *init_ctx(const char *kdfalg, int *pkeysize,
24 const char *keyfile, int keyform, int key_type,
25 char *passinarg, int pkey_op, ENGINE *e,
26 const int impl, int rawin, EVP_PKEY **ppkey);
28 static int setup_peer(EVP_PKEY_CTX *ctx, int peerform, const char *file,
31 static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
32 unsigned char *out, size_t *poutlen,
33 const unsigned char *in, size_t inlen);
35 static int do_raw_keyop(int pkey_op, EVP_PKEY_CTX *ctx,
36 const EVP_MD *md, EVP_PKEY *pkey, BIO *in,
37 int filesize, unsigned char *sig, int siglen,
38 unsigned char **out, size_t *poutlen);
40 typedef enum OPTION_choice {
41 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
42 OPT_ENGINE, OPT_ENGINE_IMPL, OPT_IN, OPT_OUT,
43 OPT_PUBIN, OPT_CERTIN, OPT_ASN1PARSE, OPT_HEXDUMP, OPT_SIGN,
44 OPT_VERIFY, OPT_VERIFYRECOVER, OPT_REV, OPT_ENCRYPT, OPT_DECRYPT,
45 OPT_DERIVE, OPT_SIGFILE, OPT_INKEY, OPT_PEERKEY, OPT_PASSIN,
46 OPT_PEERFORM, OPT_KEYFORM, OPT_PKEYOPT, OPT_PKEYOPT_PASSIN, OPT_KDF,
47 OPT_KDFLEN, OPT_R_ENUM,
51 const OPTIONS pkeyutl_options[] = {
52 OPT_SECTION("General"),
53 {"help", OPT_HELP, '-', "Display this summary"},
54 #ifndef OPENSSL_NO_ENGINE
55 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
56 {"engine_impl", OPT_ENGINE_IMPL, '-',
57 "Also use engine given by -engine for crypto operations"},
59 {"sign", OPT_SIGN, '-', "Sign input data with private key"},
60 {"verify", OPT_VERIFY, '-', "Verify with public key"},
61 {"encrypt", OPT_ENCRYPT, '-', "Encrypt input data with public key"},
62 {"decrypt", OPT_DECRYPT, '-', "Decrypt input data with private key"},
63 {"derive", OPT_DERIVE, '-', "Derive shared secret"},
66 {"in", OPT_IN, '<', "Input file - default stdin"},
67 {"rawin", OPT_RAWIN, '-', "Indicate the input data is in raw form"},
68 {"pubin", OPT_PUBIN, '-', "Input is a public key"},
69 {"inkey", OPT_INKEY, 's', "Input private key file"},
70 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
71 {"peerkey", OPT_PEERKEY, 's', "Peer key file used in key derivation"},
72 {"peerform", OPT_PEERFORM, 'E', "Peer key format - default PEM"},
73 {"certin", OPT_CERTIN, '-', "Input is a cert with a public key"},
74 {"rev", OPT_REV, '-', "Reverse the order of the input buffer"},
75 {"sigfile", OPT_SIGFILE, '<', "Signature file (verify operation only)"},
76 {"keyform", OPT_KEYFORM, 'E', "Private key format - default PEM"},
78 OPT_SECTION("Output"),
79 {"out", OPT_OUT, '>', "Output file - default stdout"},
80 {"asn1parse", OPT_ASN1PARSE, '-', "asn1parse the output data"},
81 {"hexdump", OPT_HEXDUMP, '-', "Hex dump output"},
82 {"verifyrecover", OPT_VERIFYRECOVER, '-',
83 "Verify with public key, recover original data"},
85 OPT_SECTION("Signing/Derivation"),
86 {"digest", OPT_DIGEST, 's',
87 "Specify the digest algorithm when signing the raw input data"},
88 {"pkeyopt", OPT_PKEYOPT, 's', "Public key options as opt:value"},
89 {"pkeyopt_passin", OPT_PKEYOPT_PASSIN, 's',
90 "Public key option that is read as a passphrase argument opt:passphrase"},
91 {"kdf", OPT_KDF, 's', "Use KDF algorithm"},
92 {"kdflen", OPT_KDFLEN, 'p', "KDF algorithm output length"},
98 int pkeyutl_main(int argc, char **argv)
100 BIO *in = NULL, *out = NULL;
102 EVP_PKEY_CTX *ctx = NULL;
103 EVP_PKEY *pkey = NULL;
104 char *infile = NULL, *outfile = NULL, *sigfile = NULL, *passinarg = NULL;
105 char hexdump = 0, asn1parse = 0, rev = 0, *prog;
106 unsigned char *buf_in = NULL, *buf_out = NULL, *sig = NULL;
108 int buf_inlen = 0, siglen = -1, keyform = FORMAT_PEM, peerform = FORMAT_PEM;
109 int keysize = -1, pkey_op = EVP_PKEY_OP_SIGN, key_type = KEY_PRIVKEY;
111 int ret = 1, rv = -1;
113 const char *inkey = NULL;
114 const char *peerkey = NULL;
115 const char *kdfalg = NULL;
117 STACK_OF(OPENSSL_STRING) *pkeyopts = NULL;
118 STACK_OF(OPENSSL_STRING) *pkeyopts_passin = NULL;
120 const EVP_MD *md = NULL;
123 prog = opt_init(argc, argv, pkeyutl_options);
124 while ((o = opt_next()) != OPT_EOF) {
129 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
132 opt_help(pkeyutl_options);
144 case OPT_ENGINE_IMPL:
154 passinarg = opt_arg();
157 if (!opt_format(opt_arg(), OPT_FMT_PDE, &peerform))
161 if (!opt_format(opt_arg(), OPT_FMT_PDE, &keyform))
169 e = setup_engine(opt_arg(), 0);
172 key_type = KEY_PUBKEY;
184 pkey_op = EVP_PKEY_OP_SIGN;
187 pkey_op = EVP_PKEY_OP_VERIFY;
189 case OPT_VERIFYRECOVER:
190 pkey_op = EVP_PKEY_OP_VERIFYRECOVER;
193 pkey_op = EVP_PKEY_OP_ENCRYPT;
196 pkey_op = EVP_PKEY_OP_DECRYPT;
199 pkey_op = EVP_PKEY_OP_DERIVE;
202 pkey_op = EVP_PKEY_OP_DERIVE;
207 kdflen = atoi(opt_arg());
213 if ((pkeyopts == NULL &&
214 (pkeyopts = sk_OPENSSL_STRING_new_null()) == NULL) ||
215 sk_OPENSSL_STRING_push(pkeyopts, opt_arg()) == 0) {
216 BIO_puts(bio_err, "out of memory\n");
220 case OPT_PKEYOPT_PASSIN:
221 if ((pkeyopts_passin == NULL &&
222 (pkeyopts_passin = sk_OPENSSL_STRING_new_null()) == NULL) ||
223 sk_OPENSSL_STRING_push(pkeyopts_passin, opt_arg()) == 0) {
224 BIO_puts(bio_err, "out of memory\n");
232 if (!opt_md(opt_arg(), &md))
237 argc = opt_num_rest();
241 if (rawin && pkey_op != EVP_PKEY_OP_SIGN && pkey_op != EVP_PKEY_OP_VERIFY) {
243 "%s: -rawin can only be used with -sign or -verify\n",
248 if (md != NULL && !rawin) {
250 "%s: -digest can only be used with -rawin\n",
256 BIO_printf(bio_err, "%s: -rev cannot be used with raw input\n",
261 if (kdfalg != NULL) {
264 "%s: no KDF length given (-kdflen parameter).\n", prog);
267 } else if (inkey == NULL) {
269 "%s: no private key given (-inkey parameter).\n", prog);
271 } else if (peerkey != NULL && pkey_op != EVP_PKEY_OP_DERIVE) {
273 "%s: no peer key given (-peerkey parameter).\n", prog);
276 ctx = init_ctx(kdfalg, &keysize, inkey, keyform, key_type,
277 passinarg, pkey_op, e, engine_impl, rawin, &pkey);
279 BIO_printf(bio_err, "%s: Error initializing context\n", prog);
280 ERR_print_errors(bio_err);
283 if (peerkey != NULL && !setup_peer(ctx, peerform, peerkey, e)) {
284 BIO_printf(bio_err, "%s: Error setting up peer key\n", prog);
285 ERR_print_errors(bio_err);
288 if (pkeyopts != NULL) {
289 int num = sk_OPENSSL_STRING_num(pkeyopts);
292 for (i = 0; i < num; ++i) {
293 const char *opt = sk_OPENSSL_STRING_value(pkeyopts, i);
295 if (pkey_ctrl_string(ctx, opt) <= 0) {
296 BIO_printf(bio_err, "%s: Can't set parameter \"%s\":\n",
298 ERR_print_errors(bio_err);
303 if (pkeyopts_passin != NULL) {
304 int num = sk_OPENSSL_STRING_num(pkeyopts_passin);
307 for (i = 0; i < num; i++) {
308 char *opt = sk_OPENSSL_STRING_value(pkeyopts_passin, i);
309 char *passin = strchr(opt, ':');
312 if (passin == NULL) {
313 /* Get password interactively */
314 char passwd_buf[4096];
315 BIO_snprintf(passwd_buf, sizeof(passwd_buf), "Enter %s: ", opt);
316 EVP_read_pw_string(passwd_buf, sizeof(passwd_buf) - 1,
318 passwd = OPENSSL_strdup(passwd_buf);
319 if (passwd == NULL) {
320 BIO_puts(bio_err, "out of memory\n");
324 /* Get password as a passin argument: First split option name
325 * and passphrase argument into two strings */
328 if (app_passwd(passin, NULL, &passwd, NULL) == 0) {
329 BIO_printf(bio_err, "failed to get '%s'\n", opt);
334 if (EVP_PKEY_CTX_ctrl_str(ctx, opt, passwd) <= 0) {
335 BIO_printf(bio_err, "%s: Can't set parameter \"%s\":\n",
339 OPENSSL_free(passwd);
343 if (sigfile != NULL && (pkey_op != EVP_PKEY_OP_VERIFY)) {
345 "%s: Signature file specified for non verify\n", prog);
349 if (sigfile == NULL && (pkey_op == EVP_PKEY_OP_VERIFY)) {
351 "%s: No signature file specified for verify\n", prog);
355 if (pkey_op != EVP_PKEY_OP_DERIVE) {
356 in = bio_open_default(infile, 'r', FORMAT_BINARY);
357 if (infile != NULL) {
360 if (stat(infile, &st) == 0 && st.st_size <= INT_MAX)
361 filesize = (int)st.st_size;
366 out = bio_open_default(outfile, 'w', FORMAT_BINARY);
370 if (sigfile != NULL) {
371 BIO *sigbio = BIO_new_file(sigfile, "rb");
373 if (sigbio == NULL) {
374 BIO_printf(bio_err, "Can't open signature file %s\n", sigfile);
377 siglen = bio_to_mem(&sig, keysize * 10, sigbio);
380 BIO_printf(bio_err, "Error reading signature data\n");
385 /* Raw input data is handled elsewhere */
386 if (in != NULL && !rawin) {
387 /* Read the input data */
388 buf_inlen = bio_to_mem(&buf_in, keysize * 10, in);
390 BIO_printf(bio_err, "Error reading input Data\n");
396 size_t l = (size_t)buf_inlen;
397 for (i = 0; i < l / 2; i++) {
399 buf_in[i] = buf_in[l - 1 - i];
400 buf_in[l - 1 - i] = ctmp;
405 /* Sanity check the input if the input is not raw */
407 && buf_inlen > EVP_MAX_MD_SIZE
408 && (pkey_op == EVP_PKEY_OP_SIGN
409 || pkey_op == EVP_PKEY_OP_VERIFY)) {
411 "Error: The input data looks too long to be a hash\n");
415 if (pkey_op == EVP_PKEY_OP_VERIFY) {
417 rv = do_raw_keyop(pkey_op, ctx, md, pkey, in, filesize, sig, siglen,
420 rv = EVP_PKEY_verify(ctx, sig, (size_t)siglen,
421 buf_in, (size_t)buf_inlen);
424 BIO_puts(out, "Signature Verified Successfully\n");
427 BIO_puts(out, "Signature Verification Failure\n");
436 /* rawin allocates the buffer in do_raw_keyop() */
437 rv = do_raw_keyop(pkey_op, ctx, md, pkey, in, filesize, NULL, 0,
438 &buf_out, (size_t *)&buf_outlen);
440 rv = do_keyop(ctx, pkey_op, NULL, (size_t *)&buf_outlen,
441 buf_in, (size_t)buf_inlen);
442 if (rv > 0 && buf_outlen != 0) {
443 buf_out = app_malloc(buf_outlen, "buffer output");
444 rv = do_keyop(ctx, pkey_op,
445 buf_out, (size_t *)&buf_outlen,
446 buf_in, (size_t)buf_inlen);
451 if (pkey_op != EVP_PKEY_OP_DERIVE) {
452 BIO_puts(bio_err, "Public Key operation error\n");
454 BIO_puts(bio_err, "Key derivation failed\n");
456 ERR_print_errors(bio_err);
462 if (!ASN1_parse_dump(out, buf_out, buf_outlen, 1, -1))
463 ERR_print_errors(bio_err);
464 } else if (hexdump) {
465 BIO_dump(out, (char *)buf_out, buf_outlen);
467 BIO_write(out, buf_out, buf_outlen);
471 EVP_PKEY_CTX_free(ctx);
475 OPENSSL_free(buf_in);
476 OPENSSL_free(buf_out);
478 sk_OPENSSL_STRING_free(pkeyopts);
479 sk_OPENSSL_STRING_free(pkeyopts_passin);
483 static EVP_PKEY_CTX *init_ctx(const char *kdfalg, int *pkeysize,
484 const char *keyfile, int keyform, int key_type,
485 char *passinarg, int pkey_op, ENGINE *e,
486 const int engine_impl, int rawin,
489 EVP_PKEY *pkey = NULL;
490 EVP_PKEY_CTX *ctx = NULL;
495 if (((pkey_op == EVP_PKEY_OP_SIGN) || (pkey_op == EVP_PKEY_OP_DECRYPT)
496 || (pkey_op == EVP_PKEY_OP_DERIVE))
497 && (key_type != KEY_PRIVKEY && kdfalg == NULL)) {
498 BIO_printf(bio_err, "A private key is needed for this operation\n");
501 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
502 BIO_printf(bio_err, "Error getting password\n");
507 pkey = load_key(keyfile, keyform, 0, passin, e, "Private Key");
511 pkey = load_pubkey(keyfile, keyform, 0, NULL, e, "Public Key");
515 x = load_cert(keyfile, keyform, "Certificate");
517 pkey = X509_get_pubkey(x);
527 #ifndef OPENSSL_NO_ENGINE
532 if (kdfalg != NULL) {
533 int kdfnid = OBJ_sn2nid(kdfalg);
535 if (kdfnid == NID_undef) {
536 kdfnid = OBJ_ln2nid(kdfalg);
537 if (kdfnid == NID_undef) {
538 BIO_printf(bio_err, "The given KDF \"%s\" is unknown.\n",
543 ctx = EVP_PKEY_CTX_new_id(kdfnid, impl);
548 #ifndef OPENSSL_NO_EC
549 /* SM2 needs a special treatment */
550 if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) {
551 EC_KEY *eckey = NULL;
552 const EC_GROUP *group = NULL;
555 if ((eckey = EVP_PKEY_get0_EC_KEY(pkey)) == NULL
556 || (group = EC_KEY_get0_group(eckey)) == NULL
557 || (nid = EC_GROUP_get_curve_name(group)) == 0)
560 EVP_PKEY_set_alias_type(pkey, EVP_PKEY_SM2);
563 *pkeysize = EVP_PKEY_size(pkey);
564 ctx = EVP_PKEY_CTX_new(pkey, impl);
574 * If rawin then we don't need to actually initialise the EVP_PKEY_CTX
575 * itself. That will get initialised during EVP_DigestSignInit or
576 * EVP_DigestVerifyInit.
582 case EVP_PKEY_OP_SIGN:
583 rv = EVP_PKEY_sign_init(ctx);
586 case EVP_PKEY_OP_VERIFY:
587 rv = EVP_PKEY_verify_init(ctx);
590 case EVP_PKEY_OP_VERIFYRECOVER:
591 rv = EVP_PKEY_verify_recover_init(ctx);
594 case EVP_PKEY_OP_ENCRYPT:
595 rv = EVP_PKEY_encrypt_init(ctx);
598 case EVP_PKEY_OP_DECRYPT:
599 rv = EVP_PKEY_decrypt_init(ctx);
602 case EVP_PKEY_OP_DERIVE:
603 rv = EVP_PKEY_derive_init(ctx);
609 EVP_PKEY_CTX_free(ctx);
614 OPENSSL_free(passin);
619 static int setup_peer(EVP_PKEY_CTX *ctx, int peerform, const char *file,
622 EVP_PKEY *peer = NULL;
623 ENGINE *engine = NULL;
626 if (peerform == FORMAT_ENGINE)
628 peer = load_pubkey(file, peerform, 0, NULL, engine, "Peer Key");
630 BIO_printf(bio_err, "Error reading peer key %s\n", file);
631 ERR_print_errors(bio_err);
635 ret = EVP_PKEY_derive_set_peer(ctx, peer);
639 ERR_print_errors(bio_err);
643 static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
644 unsigned char *out, size_t *poutlen,
645 const unsigned char *in, size_t inlen)
649 case EVP_PKEY_OP_VERIFYRECOVER:
650 rv = EVP_PKEY_verify_recover(ctx, out, poutlen, in, inlen);
653 case EVP_PKEY_OP_SIGN:
654 rv = EVP_PKEY_sign(ctx, out, poutlen, in, inlen);
657 case EVP_PKEY_OP_ENCRYPT:
658 rv = EVP_PKEY_encrypt(ctx, out, poutlen, in, inlen);
661 case EVP_PKEY_OP_DECRYPT:
662 rv = EVP_PKEY_decrypt(ctx, out, poutlen, in, inlen);
665 case EVP_PKEY_OP_DERIVE:
666 rv = EVP_PKEY_derive(ctx, out, poutlen);
673 #define TBUF_MAXSIZE 2048
675 static int do_raw_keyop(int pkey_op, EVP_PKEY_CTX *ctx,
676 const EVP_MD *md, EVP_PKEY *pkey, BIO *in,
677 int filesize, unsigned char *sig, int siglen,
678 unsigned char **out, size_t *poutlen)
681 EVP_MD_CTX *mctx = NULL;
682 unsigned char tbuf[TBUF_MAXSIZE];
683 unsigned char *mbuf = NULL;
686 if ((mctx = EVP_MD_CTX_new()) == NULL) {
687 BIO_printf(bio_err, "Error: out of memory\n");
690 EVP_MD_CTX_set_pkey_ctx(mctx, ctx);
692 /* Some algorithms only support oneshot digests */
693 if (EVP_PKEY_id(pkey) == EVP_PKEY_ED25519
694 || EVP_PKEY_id(pkey) == EVP_PKEY_ED448) {
697 "Error: unable to determine file size for oneshot operation\n");
700 mbuf = app_malloc(filesize, "oneshot sign/verify buffer");
702 case EVP_PKEY_OP_VERIFY:
703 if (EVP_DigestVerifyInit(mctx, NULL, md, NULL, pkey) != 1)
705 buf_len = BIO_read(in, mbuf, filesize);
706 if (buf_len != filesize) {
707 BIO_printf(bio_err, "Error reading raw input data\n");
710 rv = EVP_DigestVerify(mctx, sig, (size_t)siglen, mbuf, buf_len);
712 case EVP_PKEY_OP_SIGN:
713 if (EVP_DigestSignInit(mctx, NULL, md, NULL, pkey) != 1)
715 buf_len = BIO_read(in, mbuf, filesize);
716 if (buf_len != filesize) {
717 BIO_printf(bio_err, "Error reading raw input data\n");
720 rv = EVP_DigestSign(mctx, NULL, poutlen, mbuf, buf_len);
721 if (rv == 1 && out != NULL) {
722 *out = app_malloc(*poutlen, "buffer output");
723 rv = EVP_DigestSign(mctx, *out, poutlen, mbuf, buf_len);
731 case EVP_PKEY_OP_VERIFY:
732 if (EVP_DigestVerifyInit(mctx, NULL, md, NULL, pkey) != 1)
735 buf_len = BIO_read(in, tbuf, TBUF_MAXSIZE);
739 BIO_printf(bio_err, "Error reading raw input data\n");
742 rv = EVP_DigestVerifyUpdate(mctx, tbuf, (size_t)buf_len);
744 BIO_printf(bio_err, "Error verifying raw input data\n");
748 rv = EVP_DigestVerifyFinal(mctx, sig, (size_t)siglen);
750 case EVP_PKEY_OP_SIGN:
751 if (EVP_DigestSignInit(mctx, NULL, md, NULL, pkey) != 1)
754 buf_len = BIO_read(in, tbuf, TBUF_MAXSIZE);
758 BIO_printf(bio_err, "Error reading raw input data\n");
761 rv = EVP_DigestSignUpdate(mctx, tbuf, (size_t)buf_len);
763 BIO_printf(bio_err, "Error signing raw input data\n");
767 rv = EVP_DigestSignFinal(mctx, NULL, poutlen);
768 if (rv == 1 && out != NULL) {
769 *out = app_malloc(*poutlen, "buffer output");
770 rv = EVP_DigestSignFinal(mctx, *out, poutlen);
777 EVP_MD_CTX_free(mctx);