2 * Copyright 2006-2021 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,
27 EVP_MD_CTX *mctx, const char *digestname,
28 OSSL_LIB_CTX *libctx, const char *propq);
30 static int setup_peer(EVP_PKEY_CTX *ctx, int peerform, const char *file,
33 static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
34 unsigned char *out, size_t *poutlen,
35 const unsigned char *in, size_t inlen);
37 static int do_raw_keyop(int pkey_op, EVP_MD_CTX *mctx,
38 EVP_PKEY *pkey, BIO *in,
39 int filesize, unsigned char *sig, int siglen,
40 unsigned char **out, size_t *poutlen);
42 typedef enum OPTION_choice {
44 OPT_ENGINE, OPT_ENGINE_IMPL, OPT_IN, OPT_OUT,
45 OPT_PUBIN, OPT_CERTIN, OPT_ASN1PARSE, OPT_HEXDUMP, OPT_SIGN,
46 OPT_VERIFY, OPT_VERIFYRECOVER, OPT_REV, OPT_ENCRYPT, OPT_DECRYPT,
47 OPT_DERIVE, OPT_SIGFILE, OPT_INKEY, OPT_PEERKEY, OPT_PASSIN,
48 OPT_PEERFORM, OPT_KEYFORM, OPT_PKEYOPT, OPT_PKEYOPT_PASSIN, OPT_KDF,
49 OPT_KDFLEN, OPT_R_ENUM, OPT_PROV_ENUM,
54 const OPTIONS pkeyutl_options[] = {
55 OPT_SECTION("General"),
56 {"help", OPT_HELP, '-', "Display this summary"},
57 #ifndef OPENSSL_NO_ENGINE
58 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
59 {"engine_impl", OPT_ENGINE_IMPL, '-',
60 "Also use engine given by -engine for crypto operations"},
62 {"sign", OPT_SIGN, '-', "Sign input data with private key"},
63 {"verify", OPT_VERIFY, '-', "Verify with public key"},
64 {"encrypt", OPT_ENCRYPT, '-', "Encrypt input data with public key"},
65 {"decrypt", OPT_DECRYPT, '-', "Decrypt input data with private key"},
66 {"derive", OPT_DERIVE, '-', "Derive shared secret"},
70 {"in", OPT_IN, '<', "Input file - default stdin"},
71 {"rawin", OPT_RAWIN, '-', "Indicate the input data is in raw form"},
72 {"pubin", OPT_PUBIN, '-', "Input is a public key"},
73 {"inkey", OPT_INKEY, 's', "Input private key file"},
74 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
75 {"peerkey", OPT_PEERKEY, 's', "Peer key file used in key derivation"},
76 {"peerform", OPT_PEERFORM, 'E', "Peer key format (DER/PEM/P12/ENGINE)"},
77 {"certin", OPT_CERTIN, '-', "Input is a cert with a public key"},
78 {"rev", OPT_REV, '-', "Reverse the order of the input buffer"},
79 {"sigfile", OPT_SIGFILE, '<', "Signature file (verify operation only)"},
80 {"keyform", OPT_KEYFORM, 'E', "Private key format (ENGINE, other values ignored)"},
82 OPT_SECTION("Output"),
83 {"out", OPT_OUT, '>', "Output file - default stdout"},
84 {"asn1parse", OPT_ASN1PARSE, '-', "asn1parse the output data"},
85 {"hexdump", OPT_HEXDUMP, '-', "Hex dump output"},
86 {"verifyrecover", OPT_VERIFYRECOVER, '-',
87 "Verify with public key, recover original data"},
89 OPT_SECTION("Signing/Derivation"),
90 {"digest", OPT_DIGEST, 's',
91 "Specify the digest algorithm when signing the raw input data"},
92 {"pkeyopt", OPT_PKEYOPT, 's', "Public key options as opt:value"},
93 {"pkeyopt_passin", OPT_PKEYOPT_PASSIN, 's',
94 "Public key option that is read as a passphrase argument opt:passphrase"},
95 {"kdf", OPT_KDF, 's', "Use KDF algorithm"},
96 {"kdflen", OPT_KDFLEN, 'p', "KDF algorithm output length"},
103 int pkeyutl_main(int argc, char **argv)
106 BIO *in = NULL, *out = NULL;
108 EVP_PKEY_CTX *ctx = NULL;
109 EVP_PKEY *pkey = NULL;
110 char *infile = NULL, *outfile = NULL, *sigfile = NULL, *passinarg = NULL;
111 char hexdump = 0, asn1parse = 0, rev = 0, *prog;
112 unsigned char *buf_in = NULL, *buf_out = NULL, *sig = NULL;
114 int buf_inlen = 0, siglen = -1;
115 int keyform = FORMAT_UNDEF, peerform = FORMAT_UNDEF;
116 int keysize = -1, pkey_op = EVP_PKEY_OP_SIGN, key_type = KEY_PRIVKEY;
118 int ret = 1, rv = -1;
120 const char *inkey = NULL;
121 const char *peerkey = NULL;
122 const char *kdfalg = NULL, *digestname = NULL;
124 STACK_OF(OPENSSL_STRING) *pkeyopts = NULL;
125 STACK_OF(OPENSSL_STRING) *pkeyopts_passin = NULL;
127 EVP_MD_CTX *mctx = NULL;
130 OSSL_LIB_CTX *libctx = app_get0_libctx();
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);
153 case OPT_ENGINE_IMPL:
163 passinarg = opt_arg();
166 if (!opt_format(opt_arg(), OPT_FMT_ANY, &peerform))
170 if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
178 conf = app_load_config_modules(opt_arg());
183 if (!opt_provider(o))
187 e = setup_engine(opt_arg(), 0);
190 key_type = KEY_PUBKEY;
202 pkey_op = EVP_PKEY_OP_SIGN;
205 pkey_op = EVP_PKEY_OP_VERIFY;
207 case OPT_VERIFYRECOVER:
208 pkey_op = EVP_PKEY_OP_VERIFYRECOVER;
211 pkey_op = EVP_PKEY_OP_ENCRYPT;
214 pkey_op = EVP_PKEY_OP_DECRYPT;
217 pkey_op = EVP_PKEY_OP_DERIVE;
220 pkey_op = EVP_PKEY_OP_DERIVE;
225 kdflen = atoi(opt_arg());
231 if ((pkeyopts == NULL &&
232 (pkeyopts = sk_OPENSSL_STRING_new_null()) == NULL) ||
233 sk_OPENSSL_STRING_push(pkeyopts, opt_arg()) == 0) {
234 BIO_puts(bio_err, "out of memory\n");
238 case OPT_PKEYOPT_PASSIN:
239 if ((pkeyopts_passin == NULL &&
240 (pkeyopts_passin = sk_OPENSSL_STRING_new_null()) == NULL) ||
241 sk_OPENSSL_STRING_push(pkeyopts_passin, opt_arg()) == 0) {
242 BIO_puts(bio_err, "out of memory\n");
250 digestname = opt_arg();
255 /* No extra arguments. */
256 argc = opt_num_rest();
260 if (!app_RAND_load())
263 if (rawin && pkey_op != EVP_PKEY_OP_SIGN && pkey_op != EVP_PKEY_OP_VERIFY) {
265 "%s: -rawin can only be used with -sign or -verify\n",
270 if (digestname != NULL && !rawin) {
272 "%s: -digest can only be used with -rawin\n",
278 BIO_printf(bio_err, "%s: -rev cannot be used with raw input\n",
283 if (kdfalg != NULL) {
286 "%s: no KDF length given (-kdflen parameter).\n", prog);
289 } else if (inkey == NULL) {
291 "%s: no private key given (-inkey parameter).\n", prog);
293 } else if (peerkey != NULL && pkey_op != EVP_PKEY_OP_DERIVE) {
295 "%s: no peer key given (-peerkey parameter).\n", prog);
300 if ((mctx = EVP_MD_CTX_new()) == NULL) {
301 BIO_printf(bio_err, "Error: out of memory\n");
305 ctx = init_ctx(kdfalg, &keysize, inkey, keyform, key_type,
306 passinarg, pkey_op, e, engine_impl, rawin, &pkey,
307 mctx, digestname, libctx, app_get0_propq());
309 BIO_printf(bio_err, "%s: Error initializing context\n", prog);
310 ERR_print_errors(bio_err);
313 if (peerkey != NULL && !setup_peer(ctx, peerform, peerkey, e)) {
314 BIO_printf(bio_err, "%s: Error setting up peer key\n", prog);
315 ERR_print_errors(bio_err);
318 if (pkeyopts != NULL) {
319 int num = sk_OPENSSL_STRING_num(pkeyopts);
322 for (i = 0; i < num; ++i) {
323 const char *opt = sk_OPENSSL_STRING_value(pkeyopts, i);
325 if (pkey_ctrl_string(ctx, opt) <= 0) {
326 BIO_printf(bio_err, "%s: Can't set parameter \"%s\":\n",
328 ERR_print_errors(bio_err);
333 if (pkeyopts_passin != NULL) {
334 int num = sk_OPENSSL_STRING_num(pkeyopts_passin);
337 for (i = 0; i < num; i++) {
338 char *opt = sk_OPENSSL_STRING_value(pkeyopts_passin, i);
339 char *passin = strchr(opt, ':');
342 if (passin == NULL) {
343 /* Get password interactively */
344 char passwd_buf[4096];
347 BIO_snprintf(passwd_buf, sizeof(passwd_buf), "Enter %s: ", opt);
348 r = EVP_read_pw_string(passwd_buf, sizeof(passwd_buf) - 1,
352 BIO_puts(bio_err, "user abort\n");
354 BIO_puts(bio_err, "entry failed\n");
357 passwd = OPENSSL_strdup(passwd_buf);
358 if (passwd == NULL) {
359 BIO_puts(bio_err, "out of memory\n");
363 /* Get password as a passin argument: First split option name
364 * and passphrase argument into two strings */
367 if (app_passwd(passin, NULL, &passwd, NULL) == 0) {
368 BIO_printf(bio_err, "failed to get '%s'\n", opt);
373 if (EVP_PKEY_CTX_ctrl_str(ctx, opt, passwd) <= 0) {
374 BIO_printf(bio_err, "%s: Can't set parameter \"%s\":\n",
378 OPENSSL_free(passwd);
382 if (sigfile != NULL && (pkey_op != EVP_PKEY_OP_VERIFY)) {
384 "%s: Signature file specified for non verify\n", prog);
388 if (sigfile == NULL && (pkey_op == EVP_PKEY_OP_VERIFY)) {
390 "%s: No signature file specified for verify\n", prog);
394 if (pkey_op != EVP_PKEY_OP_DERIVE) {
395 in = bio_open_default(infile, 'r', FORMAT_BINARY);
396 if (infile != NULL) {
399 if (stat(infile, &st) == 0 && st.st_size <= INT_MAX)
400 filesize = (int)st.st_size;
405 out = bio_open_default(outfile, 'w', FORMAT_BINARY);
409 if (sigfile != NULL) {
410 BIO *sigbio = BIO_new_file(sigfile, "rb");
412 if (sigbio == NULL) {
413 BIO_printf(bio_err, "Can't open signature file %s\n", sigfile);
416 siglen = bio_to_mem(&sig, keysize * 10, sigbio);
419 BIO_printf(bio_err, "Error reading signature data\n");
424 /* Raw input data is handled elsewhere */
425 if (in != NULL && !rawin) {
426 /* Read the input data */
427 buf_inlen = bio_to_mem(&buf_in, keysize * 10, in);
429 BIO_printf(bio_err, "Error reading input Data\n");
435 size_t l = (size_t)buf_inlen;
436 for (i = 0; i < l / 2; i++) {
438 buf_in[i] = buf_in[l - 1 - i];
439 buf_in[l - 1 - i] = ctmp;
444 /* Sanity check the input if the input is not raw */
446 && buf_inlen > EVP_MAX_MD_SIZE
447 && (pkey_op == EVP_PKEY_OP_SIGN
448 || pkey_op == EVP_PKEY_OP_VERIFY)) {
450 "Error: The input data looks too long to be a hash\n");
454 if (pkey_op == EVP_PKEY_OP_VERIFY) {
456 rv = do_raw_keyop(pkey_op, mctx, pkey, in, filesize, sig, siglen,
459 rv = EVP_PKEY_verify(ctx, sig, (size_t)siglen,
460 buf_in, (size_t)buf_inlen);
463 BIO_puts(out, "Signature Verified Successfully\n");
466 BIO_puts(out, "Signature Verification Failure\n");
475 /* rawin allocates the buffer in do_raw_keyop() */
476 rv = do_raw_keyop(pkey_op, mctx, pkey, in, filesize, NULL, 0,
477 &buf_out, (size_t *)&buf_outlen);
479 rv = do_keyop(ctx, pkey_op, NULL, (size_t *)&buf_outlen,
480 buf_in, (size_t)buf_inlen);
481 if (rv > 0 && buf_outlen != 0) {
482 buf_out = app_malloc(buf_outlen, "buffer output");
483 rv = do_keyop(ctx, pkey_op,
484 buf_out, (size_t *)&buf_outlen,
485 buf_in, (size_t)buf_inlen);
490 if (pkey_op != EVP_PKEY_OP_DERIVE) {
491 BIO_puts(bio_err, "Public Key operation error\n");
493 BIO_puts(bio_err, "Key derivation failed\n");
495 ERR_print_errors(bio_err);
501 if (!ASN1_parse_dump(out, buf_out, buf_outlen, 1, -1))
502 ERR_print_errors(bio_err);
503 } else if (hexdump) {
504 BIO_dump(out, (char *)buf_out, buf_outlen);
506 BIO_write(out, buf_out, buf_outlen);
510 EVP_MD_CTX_free(mctx);
511 EVP_PKEY_CTX_free(ctx);
516 OPENSSL_free(buf_in);
517 OPENSSL_free(buf_out);
519 sk_OPENSSL_STRING_free(pkeyopts);
520 sk_OPENSSL_STRING_free(pkeyopts_passin);
525 static EVP_PKEY_CTX *init_ctx(const char *kdfalg, int *pkeysize,
526 const char *keyfile, int keyform, int key_type,
527 char *passinarg, int pkey_op, ENGINE *e,
528 const int engine_impl, int rawin,
529 EVP_PKEY **ppkey, EVP_MD_CTX *mctx, const char *digestname,
530 OSSL_LIB_CTX *libctx, const char *propq)
532 EVP_PKEY *pkey = NULL;
533 EVP_PKEY_CTX *ctx = NULL;
539 if (((pkey_op == EVP_PKEY_OP_SIGN) || (pkey_op == EVP_PKEY_OP_DECRYPT)
540 || (pkey_op == EVP_PKEY_OP_DERIVE))
541 && (key_type != KEY_PRIVKEY && kdfalg == NULL)) {
542 BIO_printf(bio_err, "A private key is needed for this operation\n");
545 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
546 BIO_printf(bio_err, "Error getting password\n");
551 pkey = load_key(keyfile, keyform, 0, passin, e, "private key");
555 pkey = load_pubkey(keyfile, keyform, 0, NULL, e, "public key");
559 x = load_cert(keyfile, keyform, "Certificate");
561 pkey = X509_get_pubkey(x);
571 #ifndef OPENSSL_NO_ENGINE
576 if (kdfalg != NULL) {
577 int kdfnid = OBJ_sn2nid(kdfalg);
579 if (kdfnid == NID_undef) {
580 kdfnid = OBJ_ln2nid(kdfalg);
581 if (kdfnid == NID_undef) {
582 BIO_printf(bio_err, "The given KDF \"%s\" is unknown.\n",
588 ctx = EVP_PKEY_CTX_new_id(kdfnid, impl);
590 ctx = EVP_PKEY_CTX_new_from_name(libctx, kdfalg, propq);
595 *pkeysize = EVP_PKEY_get_size(pkey);
597 ctx = EVP_PKEY_CTX_new(pkey, impl);
599 ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
609 EVP_MD_CTX_set_pkey_ctx(mctx, ctx);
612 case EVP_PKEY_OP_SIGN:
613 rv = EVP_DigestSignInit_ex(mctx, NULL, digestname, libctx, propq,
617 case EVP_PKEY_OP_VERIFY:
618 rv = EVP_DigestVerifyInit_ex(mctx, NULL, digestname, libctx, propq,
625 case EVP_PKEY_OP_SIGN:
626 rv = EVP_PKEY_sign_init(ctx);
629 case EVP_PKEY_OP_VERIFY:
630 rv = EVP_PKEY_verify_init(ctx);
633 case EVP_PKEY_OP_VERIFYRECOVER:
634 rv = EVP_PKEY_verify_recover_init(ctx);
637 case EVP_PKEY_OP_ENCRYPT:
638 rv = EVP_PKEY_encrypt_init(ctx);
641 case EVP_PKEY_OP_DECRYPT:
642 rv = EVP_PKEY_decrypt_init(ctx);
645 case EVP_PKEY_OP_DERIVE:
646 rv = EVP_PKEY_derive_init(ctx);
652 EVP_PKEY_CTX_free(ctx);
657 OPENSSL_free(passin);
662 static int setup_peer(EVP_PKEY_CTX *ctx, int peerform, const char *file,
665 EVP_PKEY *peer = NULL;
666 ENGINE *engine = NULL;
669 if (peerform == FORMAT_ENGINE)
671 peer = load_pubkey(file, peerform, 0, NULL, engine, "peer key");
673 BIO_printf(bio_err, "Error reading peer key %s\n", file);
674 ERR_print_errors(bio_err);
678 ret = EVP_PKEY_derive_set_peer(ctx, peer);
682 ERR_print_errors(bio_err);
686 static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
687 unsigned char *out, size_t *poutlen,
688 const unsigned char *in, size_t inlen)
692 case EVP_PKEY_OP_VERIFYRECOVER:
693 rv = EVP_PKEY_verify_recover(ctx, out, poutlen, in, inlen);
696 case EVP_PKEY_OP_SIGN:
697 rv = EVP_PKEY_sign(ctx, out, poutlen, in, inlen);
700 case EVP_PKEY_OP_ENCRYPT:
701 rv = EVP_PKEY_encrypt(ctx, out, poutlen, in, inlen);
704 case EVP_PKEY_OP_DECRYPT:
705 rv = EVP_PKEY_decrypt(ctx, out, poutlen, in, inlen);
708 case EVP_PKEY_OP_DERIVE:
709 rv = EVP_PKEY_derive(ctx, out, poutlen);
716 #define TBUF_MAXSIZE 2048
718 static int do_raw_keyop(int pkey_op, EVP_MD_CTX *mctx,
719 EVP_PKEY *pkey, BIO *in,
720 int filesize, unsigned char *sig, int siglen,
721 unsigned char **out, size_t *poutlen)
724 unsigned char tbuf[TBUF_MAXSIZE];
725 unsigned char *mbuf = NULL;
728 /* Some algorithms only support oneshot digests */
729 if (EVP_PKEY_get_id(pkey) == EVP_PKEY_ED25519
730 || EVP_PKEY_get_id(pkey) == EVP_PKEY_ED448) {
733 "Error: unable to determine file size for oneshot operation\n");
736 mbuf = app_malloc(filesize, "oneshot sign/verify buffer");
738 case EVP_PKEY_OP_VERIFY:
739 buf_len = BIO_read(in, mbuf, filesize);
740 if (buf_len != filesize) {
741 BIO_printf(bio_err, "Error reading raw input data\n");
744 rv = EVP_DigestVerify(mctx, sig, (size_t)siglen, mbuf, buf_len);
746 case EVP_PKEY_OP_SIGN:
747 buf_len = BIO_read(in, mbuf, filesize);
748 if (buf_len != filesize) {
749 BIO_printf(bio_err, "Error reading raw input data\n");
752 rv = EVP_DigestSign(mctx, NULL, poutlen, mbuf, buf_len);
753 if (rv == 1 && out != NULL) {
754 *out = app_malloc(*poutlen, "buffer output");
755 rv = EVP_DigestSign(mctx, *out, poutlen, mbuf, buf_len);
763 case EVP_PKEY_OP_VERIFY:
765 buf_len = BIO_read(in, tbuf, TBUF_MAXSIZE);
769 BIO_printf(bio_err, "Error reading raw input data\n");
772 rv = EVP_DigestVerifyUpdate(mctx, tbuf, (size_t)buf_len);
774 BIO_printf(bio_err, "Error verifying raw input data\n");
778 rv = EVP_DigestVerifyFinal(mctx, sig, (size_t)siglen);
780 case EVP_PKEY_OP_SIGN:
782 buf_len = BIO_read(in, tbuf, TBUF_MAXSIZE);
786 BIO_printf(bio_err, "Error reading raw input data\n");
789 rv = EVP_DigestSignUpdate(mctx, tbuf, (size_t)buf_len);
791 BIO_printf(bio_err, "Error signing raw input data\n");
795 rv = EVP_DigestSignFinal(mctx, NULL, poutlen);
796 if (rv == 1 && out != NULL) {
797 *out = app_malloc(*poutlen, "buffer output");
798 rv = EVP_DigestSignFinal(mctx, *out, poutlen);