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 {
43 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
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, keyform = FORMAT_PEM, peerform = FORMAT_PEM;
115 int keysize = -1, pkey_op = EVP_PKEY_OP_SIGN, key_type = KEY_PRIVKEY;
117 int ret = 1, rv = -1;
119 const char *inkey = NULL;
120 const char *peerkey = NULL;
121 const char *kdfalg = NULL, *digestname = NULL;
123 STACK_OF(OPENSSL_STRING) *pkeyopts = NULL;
124 STACK_OF(OPENSSL_STRING) *pkeyopts_passin = NULL;
126 EVP_MD_CTX *mctx = NULL;
128 OSSL_LIB_CTX *libctx = app_get0_libctx();
130 prog = opt_init(argc, argv, pkeyutl_options);
131 while ((o = opt_next()) != OPT_EOF) {
136 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
139 opt_help(pkeyutl_options);
151 case OPT_ENGINE_IMPL:
161 passinarg = opt_arg();
164 if (!opt_format(opt_arg(), OPT_FMT_ANY, &peerform))
168 if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
176 conf = app_load_config_modules(opt_arg());
181 if (!opt_provider(o))
185 e = setup_engine(opt_arg(), 0);
188 key_type = KEY_PUBKEY;
200 pkey_op = EVP_PKEY_OP_SIGN;
203 pkey_op = EVP_PKEY_OP_VERIFY;
205 case OPT_VERIFYRECOVER:
206 pkey_op = EVP_PKEY_OP_VERIFYRECOVER;
209 pkey_op = EVP_PKEY_OP_ENCRYPT;
212 pkey_op = EVP_PKEY_OP_DECRYPT;
215 pkey_op = EVP_PKEY_OP_DERIVE;
218 pkey_op = EVP_PKEY_OP_DERIVE;
223 kdflen = atoi(opt_arg());
229 if ((pkeyopts == NULL &&
230 (pkeyopts = sk_OPENSSL_STRING_new_null()) == NULL) ||
231 sk_OPENSSL_STRING_push(pkeyopts, opt_arg()) == 0) {
232 BIO_puts(bio_err, "out of memory\n");
236 case OPT_PKEYOPT_PASSIN:
237 if ((pkeyopts_passin == NULL &&
238 (pkeyopts_passin = sk_OPENSSL_STRING_new_null()) == NULL) ||
239 sk_OPENSSL_STRING_push(pkeyopts_passin, opt_arg()) == 0) {
240 BIO_puts(bio_err, "out of memory\n");
248 digestname = opt_arg();
253 /* No extra arguments. */
254 argc = opt_num_rest();
260 if (rawin && pkey_op != EVP_PKEY_OP_SIGN && pkey_op != EVP_PKEY_OP_VERIFY) {
262 "%s: -rawin can only be used with -sign or -verify\n",
267 if (digestname != NULL && !rawin) {
269 "%s: -digest can only be used with -rawin\n",
275 BIO_printf(bio_err, "%s: -rev cannot be used with raw input\n",
280 if (kdfalg != NULL) {
283 "%s: no KDF length given (-kdflen parameter).\n", prog);
286 } else if (inkey == NULL) {
288 "%s: no private key given (-inkey parameter).\n", prog);
290 } else if (peerkey != NULL && pkey_op != EVP_PKEY_OP_DERIVE) {
292 "%s: no peer key given (-peerkey parameter).\n", prog);
297 if ((mctx = EVP_MD_CTX_new()) == NULL) {
298 BIO_printf(bio_err, "Error: out of memory\n");
302 ctx = init_ctx(kdfalg, &keysize, inkey, keyform, key_type,
303 passinarg, pkey_op, e, engine_impl, rawin, &pkey,
304 mctx, digestname, libctx, app_get0_propq());
306 BIO_printf(bio_err, "%s: Error initializing context\n", prog);
307 ERR_print_errors(bio_err);
310 if (peerkey != NULL && !setup_peer(ctx, peerform, peerkey, e)) {
311 BIO_printf(bio_err, "%s: Error setting up peer key\n", prog);
312 ERR_print_errors(bio_err);
315 if (pkeyopts != NULL) {
316 int num = sk_OPENSSL_STRING_num(pkeyopts);
319 for (i = 0; i < num; ++i) {
320 const char *opt = sk_OPENSSL_STRING_value(pkeyopts, i);
322 if (pkey_ctrl_string(ctx, opt) <= 0) {
323 BIO_printf(bio_err, "%s: Can't set parameter \"%s\":\n",
325 ERR_print_errors(bio_err);
330 if (pkeyopts_passin != NULL) {
331 int num = sk_OPENSSL_STRING_num(pkeyopts_passin);
334 for (i = 0; i < num; i++) {
335 char *opt = sk_OPENSSL_STRING_value(pkeyopts_passin, i);
336 char *passin = strchr(opt, ':');
339 if (passin == NULL) {
340 /* Get password interactively */
341 char passwd_buf[4096];
344 BIO_snprintf(passwd_buf, sizeof(passwd_buf), "Enter %s: ", opt);
345 r = EVP_read_pw_string(passwd_buf, sizeof(passwd_buf) - 1,
349 BIO_puts(bio_err, "user abort\n");
351 BIO_puts(bio_err, "entry failed\n");
354 passwd = OPENSSL_strdup(passwd_buf);
355 if (passwd == NULL) {
356 BIO_puts(bio_err, "out of memory\n");
360 /* Get password as a passin argument: First split option name
361 * and passphrase argument into two strings */
364 if (app_passwd(passin, NULL, &passwd, NULL) == 0) {
365 BIO_printf(bio_err, "failed to get '%s'\n", opt);
370 if (EVP_PKEY_CTX_ctrl_str(ctx, opt, passwd) <= 0) {
371 BIO_printf(bio_err, "%s: Can't set parameter \"%s\":\n",
375 OPENSSL_free(passwd);
379 if (sigfile != NULL && (pkey_op != EVP_PKEY_OP_VERIFY)) {
381 "%s: Signature file specified for non verify\n", prog);
385 if (sigfile == NULL && (pkey_op == EVP_PKEY_OP_VERIFY)) {
387 "%s: No signature file specified for verify\n", prog);
391 if (pkey_op != EVP_PKEY_OP_DERIVE) {
392 in = bio_open_default(infile, 'r', FORMAT_BINARY);
393 if (infile != NULL) {
396 if (stat(infile, &st) == 0 && st.st_size <= INT_MAX)
397 filesize = (int)st.st_size;
402 out = bio_open_default(outfile, 'w', FORMAT_BINARY);
406 if (sigfile != NULL) {
407 BIO *sigbio = BIO_new_file(sigfile, "rb");
409 if (sigbio == NULL) {
410 BIO_printf(bio_err, "Can't open signature file %s\n", sigfile);
413 siglen = bio_to_mem(&sig, keysize * 10, sigbio);
416 BIO_printf(bio_err, "Error reading signature data\n");
421 /* Raw input data is handled elsewhere */
422 if (in != NULL && !rawin) {
423 /* Read the input data */
424 buf_inlen = bio_to_mem(&buf_in, keysize * 10, in);
426 BIO_printf(bio_err, "Error reading input Data\n");
432 size_t l = (size_t)buf_inlen;
433 for (i = 0; i < l / 2; i++) {
435 buf_in[i] = buf_in[l - 1 - i];
436 buf_in[l - 1 - i] = ctmp;
441 /* Sanity check the input if the input is not raw */
443 && buf_inlen > EVP_MAX_MD_SIZE
444 && (pkey_op == EVP_PKEY_OP_SIGN
445 || pkey_op == EVP_PKEY_OP_VERIFY)) {
447 "Error: The input data looks too long to be a hash\n");
451 if (pkey_op == EVP_PKEY_OP_VERIFY) {
453 rv = do_raw_keyop(pkey_op, mctx, pkey, in, filesize, sig, siglen,
456 rv = EVP_PKEY_verify(ctx, sig, (size_t)siglen,
457 buf_in, (size_t)buf_inlen);
460 BIO_puts(out, "Signature Verified Successfully\n");
463 BIO_puts(out, "Signature Verification Failure\n");
472 /* rawin allocates the buffer in do_raw_keyop() */
473 rv = do_raw_keyop(pkey_op, mctx, pkey, in, filesize, NULL, 0,
474 &buf_out, (size_t *)&buf_outlen);
476 rv = do_keyop(ctx, pkey_op, NULL, (size_t *)&buf_outlen,
477 buf_in, (size_t)buf_inlen);
478 if (rv > 0 && buf_outlen != 0) {
479 buf_out = app_malloc(buf_outlen, "buffer output");
480 rv = do_keyop(ctx, pkey_op,
481 buf_out, (size_t *)&buf_outlen,
482 buf_in, (size_t)buf_inlen);
487 if (pkey_op != EVP_PKEY_OP_DERIVE) {
488 BIO_puts(bio_err, "Public Key operation error\n");
490 BIO_puts(bio_err, "Key derivation failed\n");
492 ERR_print_errors(bio_err);
498 if (!ASN1_parse_dump(out, buf_out, buf_outlen, 1, -1))
499 ERR_print_errors(bio_err);
500 } else if (hexdump) {
501 BIO_dump(out, (char *)buf_out, buf_outlen);
503 BIO_write(out, buf_out, buf_outlen);
507 EVP_MD_CTX_free(mctx);
508 EVP_PKEY_CTX_free(ctx);
512 OPENSSL_free(buf_in);
513 OPENSSL_free(buf_out);
515 sk_OPENSSL_STRING_free(pkeyopts);
516 sk_OPENSSL_STRING_free(pkeyopts_passin);
521 static EVP_PKEY_CTX *init_ctx(const char *kdfalg, int *pkeysize,
522 const char *keyfile, int keyform, int key_type,
523 char *passinarg, int pkey_op, ENGINE *e,
524 const int engine_impl, int rawin,
525 EVP_PKEY **ppkey, EVP_MD_CTX *mctx, const char *digestname,
526 OSSL_LIB_CTX *libctx, const char *propq)
528 EVP_PKEY *pkey = NULL;
529 EVP_PKEY_CTX *ctx = NULL;
535 if (((pkey_op == EVP_PKEY_OP_SIGN) || (pkey_op == EVP_PKEY_OP_DECRYPT)
536 || (pkey_op == EVP_PKEY_OP_DERIVE))
537 && (key_type != KEY_PRIVKEY && kdfalg == NULL)) {
538 BIO_printf(bio_err, "A private key is needed for this operation\n");
541 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
542 BIO_printf(bio_err, "Error getting password\n");
547 pkey = load_key(keyfile, keyform, 0, passin, e, "private key");
551 pkey = load_pubkey(keyfile, keyform, 0, NULL, e, "public key");
555 x = load_cert(keyfile, "Certificate");
557 pkey = X509_get_pubkey(x);
567 #ifndef OPENSSL_NO_ENGINE
572 if (kdfalg != NULL) {
573 int kdfnid = OBJ_sn2nid(kdfalg);
575 if (kdfnid == NID_undef) {
576 kdfnid = OBJ_ln2nid(kdfalg);
577 if (kdfnid == NID_undef) {
578 BIO_printf(bio_err, "The given KDF \"%s\" is unknown.\n",
584 ctx = EVP_PKEY_CTX_new_id(kdfnid, impl);
586 ctx = EVP_PKEY_CTX_new_from_name(libctx, kdfalg, propq);
591 *pkeysize = EVP_PKEY_size(pkey);
593 ctx = EVP_PKEY_CTX_new(pkey, impl);
595 ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
605 EVP_MD_CTX_set_pkey_ctx(mctx, ctx);
608 case EVP_PKEY_OP_SIGN:
609 rv = EVP_DigestSignInit_ex(mctx, NULL, digestname, libctx, propq,
613 case EVP_PKEY_OP_VERIFY:
614 rv = EVP_DigestVerifyInit_ex(mctx, NULL, digestname, libctx, propq,
621 case EVP_PKEY_OP_SIGN:
622 rv = EVP_PKEY_sign_init(ctx);
625 case EVP_PKEY_OP_VERIFY:
626 rv = EVP_PKEY_verify_init(ctx);
629 case EVP_PKEY_OP_VERIFYRECOVER:
630 rv = EVP_PKEY_verify_recover_init(ctx);
633 case EVP_PKEY_OP_ENCRYPT:
634 rv = EVP_PKEY_encrypt_init(ctx);
637 case EVP_PKEY_OP_DECRYPT:
638 rv = EVP_PKEY_decrypt_init(ctx);
641 case EVP_PKEY_OP_DERIVE:
642 rv = EVP_PKEY_derive_init(ctx);
648 EVP_PKEY_CTX_free(ctx);
653 OPENSSL_free(passin);
658 static int setup_peer(EVP_PKEY_CTX *ctx, int peerform, const char *file,
661 EVP_PKEY *peer = NULL;
662 ENGINE *engine = NULL;
665 if (peerform == FORMAT_ENGINE)
667 peer = load_pubkey(file, peerform, 0, NULL, engine, "peer key");
669 BIO_printf(bio_err, "Error reading peer key %s\n", file);
670 ERR_print_errors(bio_err);
674 ret = EVP_PKEY_derive_set_peer(ctx, peer);
678 ERR_print_errors(bio_err);
682 static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
683 unsigned char *out, size_t *poutlen,
684 const unsigned char *in, size_t inlen)
688 case EVP_PKEY_OP_VERIFYRECOVER:
689 rv = EVP_PKEY_verify_recover(ctx, out, poutlen, in, inlen);
692 case EVP_PKEY_OP_SIGN:
693 rv = EVP_PKEY_sign(ctx, out, poutlen, in, inlen);
696 case EVP_PKEY_OP_ENCRYPT:
697 rv = EVP_PKEY_encrypt(ctx, out, poutlen, in, inlen);
700 case EVP_PKEY_OP_DECRYPT:
701 rv = EVP_PKEY_decrypt(ctx, out, poutlen, in, inlen);
704 case EVP_PKEY_OP_DERIVE:
705 rv = EVP_PKEY_derive(ctx, out, poutlen);
712 #define TBUF_MAXSIZE 2048
714 static int do_raw_keyop(int pkey_op, EVP_MD_CTX *mctx,
715 EVP_PKEY *pkey, BIO *in,
716 int filesize, unsigned char *sig, int siglen,
717 unsigned char **out, size_t *poutlen)
720 unsigned char tbuf[TBUF_MAXSIZE];
721 unsigned char *mbuf = NULL;
724 /* Some algorithms only support oneshot digests */
725 if (EVP_PKEY_id(pkey) == EVP_PKEY_ED25519
726 || EVP_PKEY_id(pkey) == EVP_PKEY_ED448) {
729 "Error: unable to determine file size for oneshot operation\n");
732 mbuf = app_malloc(filesize, "oneshot sign/verify buffer");
734 case EVP_PKEY_OP_VERIFY:
735 buf_len = BIO_read(in, mbuf, filesize);
736 if (buf_len != filesize) {
737 BIO_printf(bio_err, "Error reading raw input data\n");
740 rv = EVP_DigestVerify(mctx, sig, (size_t)siglen, mbuf, buf_len);
742 case EVP_PKEY_OP_SIGN:
743 buf_len = BIO_read(in, mbuf, filesize);
744 if (buf_len != filesize) {
745 BIO_printf(bio_err, "Error reading raw input data\n");
748 rv = EVP_DigestSign(mctx, NULL, poutlen, mbuf, buf_len);
749 if (rv == 1 && out != NULL) {
750 *out = app_malloc(*poutlen, "buffer output");
751 rv = EVP_DigestSign(mctx, *out, poutlen, mbuf, buf_len);
759 case EVP_PKEY_OP_VERIFY:
761 buf_len = BIO_read(in, tbuf, TBUF_MAXSIZE);
765 BIO_printf(bio_err, "Error reading raw input data\n");
768 rv = EVP_DigestVerifyUpdate(mctx, tbuf, (size_t)buf_len);
770 BIO_printf(bio_err, "Error verifying raw input data\n");
774 rv = EVP_DigestVerifyFinal(mctx, sig, (size_t)siglen);
776 case EVP_PKEY_OP_SIGN:
778 buf_len = BIO_read(in, tbuf, TBUF_MAXSIZE);
782 BIO_printf(bio_err, "Error reading raw input data\n");
785 rv = EVP_DigestSignUpdate(mctx, tbuf, (size_t)buf_len);
787 BIO_printf(bio_err, "Error signing raw input data\n");
791 rv = EVP_DigestSignFinal(mctx, NULL, poutlen);
792 if (rv == 1 && out != NULL) {
793 *out = app_malloc(*poutlen, "buffer output");
794 rv = EVP_DigestSignFinal(mctx, *out, poutlen);