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, OPT_PROV_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"},
99 int pkeyutl_main(int argc, char **argv)
101 BIO *in = NULL, *out = NULL;
103 EVP_PKEY_CTX *ctx = NULL;
104 EVP_PKEY *pkey = NULL;
105 char *infile = NULL, *outfile = NULL, *sigfile = NULL, *passinarg = NULL;
106 char hexdump = 0, asn1parse = 0, rev = 0, *prog;
107 unsigned char *buf_in = NULL, *buf_out = NULL, *sig = NULL;
109 int buf_inlen = 0, siglen = -1, keyform = FORMAT_PEM, peerform = FORMAT_PEM;
110 int keysize = -1, pkey_op = EVP_PKEY_OP_SIGN, key_type = KEY_PRIVKEY;
112 int ret = 1, rv = -1;
114 const char *inkey = NULL;
115 const char *peerkey = NULL;
116 const char *kdfalg = NULL;
118 STACK_OF(OPENSSL_STRING) *pkeyopts = NULL;
119 STACK_OF(OPENSSL_STRING) *pkeyopts_passin = NULL;
121 const EVP_MD *md = NULL;
124 prog = opt_init(argc, argv, pkeyutl_options);
125 while ((o = opt_next()) != OPT_EOF) {
130 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
133 opt_help(pkeyutl_options);
145 case OPT_ENGINE_IMPL:
155 passinarg = opt_arg();
158 if (!opt_format(opt_arg(), OPT_FMT_PDE, &peerform))
162 if (!opt_format(opt_arg(), OPT_FMT_PDE, &keyform))
170 if (!opt_provider(o))
174 e = setup_engine(opt_arg(), 0);
177 key_type = KEY_PUBKEY;
189 pkey_op = EVP_PKEY_OP_SIGN;
192 pkey_op = EVP_PKEY_OP_VERIFY;
194 case OPT_VERIFYRECOVER:
195 pkey_op = EVP_PKEY_OP_VERIFYRECOVER;
198 pkey_op = EVP_PKEY_OP_ENCRYPT;
201 pkey_op = EVP_PKEY_OP_DECRYPT;
204 pkey_op = EVP_PKEY_OP_DERIVE;
207 pkey_op = EVP_PKEY_OP_DERIVE;
212 kdflen = atoi(opt_arg());
218 if ((pkeyopts == NULL &&
219 (pkeyopts = sk_OPENSSL_STRING_new_null()) == NULL) ||
220 sk_OPENSSL_STRING_push(pkeyopts, opt_arg()) == 0) {
221 BIO_puts(bio_err, "out of memory\n");
225 case OPT_PKEYOPT_PASSIN:
226 if ((pkeyopts_passin == NULL &&
227 (pkeyopts_passin = sk_OPENSSL_STRING_new_null()) == NULL) ||
228 sk_OPENSSL_STRING_push(pkeyopts_passin, opt_arg()) == 0) {
229 BIO_puts(bio_err, "out of memory\n");
237 if (!opt_md(opt_arg(), &md))
242 argc = opt_num_rest();
246 if (rawin && pkey_op != EVP_PKEY_OP_SIGN && pkey_op != EVP_PKEY_OP_VERIFY) {
248 "%s: -rawin can only be used with -sign or -verify\n",
253 if (md != NULL && !rawin) {
255 "%s: -digest can only be used with -rawin\n",
261 BIO_printf(bio_err, "%s: -rev cannot be used with raw input\n",
266 if (kdfalg != NULL) {
269 "%s: no KDF length given (-kdflen parameter).\n", prog);
272 } else if (inkey == NULL) {
274 "%s: no private key given (-inkey parameter).\n", prog);
276 } else if (peerkey != NULL && pkey_op != EVP_PKEY_OP_DERIVE) {
278 "%s: no peer key given (-peerkey parameter).\n", prog);
281 ctx = init_ctx(kdfalg, &keysize, inkey, keyform, key_type,
282 passinarg, pkey_op, e, engine_impl, rawin, &pkey);
284 BIO_printf(bio_err, "%s: Error initializing context\n", prog);
285 ERR_print_errors(bio_err);
288 if (peerkey != NULL && !setup_peer(ctx, peerform, peerkey, e)) {
289 BIO_printf(bio_err, "%s: Error setting up peer key\n", prog);
290 ERR_print_errors(bio_err);
293 if (pkeyopts != NULL) {
294 int num = sk_OPENSSL_STRING_num(pkeyopts);
297 for (i = 0; i < num; ++i) {
298 const char *opt = sk_OPENSSL_STRING_value(pkeyopts, i);
300 if (pkey_ctrl_string(ctx, opt) <= 0) {
301 BIO_printf(bio_err, "%s: Can't set parameter \"%s\":\n",
303 ERR_print_errors(bio_err);
308 if (pkeyopts_passin != NULL) {
309 int num = sk_OPENSSL_STRING_num(pkeyopts_passin);
312 for (i = 0; i < num; i++) {
313 char *opt = sk_OPENSSL_STRING_value(pkeyopts_passin, i);
314 char *passin = strchr(opt, ':');
317 if (passin == NULL) {
318 /* Get password interactively */
319 char passwd_buf[4096];
320 BIO_snprintf(passwd_buf, sizeof(passwd_buf), "Enter %s: ", opt);
321 EVP_read_pw_string(passwd_buf, sizeof(passwd_buf) - 1,
323 passwd = OPENSSL_strdup(passwd_buf);
324 if (passwd == NULL) {
325 BIO_puts(bio_err, "out of memory\n");
329 /* Get password as a passin argument: First split option name
330 * and passphrase argument into two strings */
333 if (app_passwd(passin, NULL, &passwd, NULL) == 0) {
334 BIO_printf(bio_err, "failed to get '%s'\n", opt);
339 if (EVP_PKEY_CTX_ctrl_str(ctx, opt, passwd) <= 0) {
340 BIO_printf(bio_err, "%s: Can't set parameter \"%s\":\n",
344 OPENSSL_free(passwd);
348 if (sigfile != NULL && (pkey_op != EVP_PKEY_OP_VERIFY)) {
350 "%s: Signature file specified for non verify\n", prog);
354 if (sigfile == NULL && (pkey_op == EVP_PKEY_OP_VERIFY)) {
356 "%s: No signature file specified for verify\n", prog);
360 if (pkey_op != EVP_PKEY_OP_DERIVE) {
361 in = bio_open_default(infile, 'r', FORMAT_BINARY);
362 if (infile != NULL) {
365 if (stat(infile, &st) == 0 && st.st_size <= INT_MAX)
366 filesize = (int)st.st_size;
371 out = bio_open_default(outfile, 'w', FORMAT_BINARY);
375 if (sigfile != NULL) {
376 BIO *sigbio = BIO_new_file(sigfile, "rb");
378 if (sigbio == NULL) {
379 BIO_printf(bio_err, "Can't open signature file %s\n", sigfile);
382 siglen = bio_to_mem(&sig, keysize * 10, sigbio);
385 BIO_printf(bio_err, "Error reading signature data\n");
390 /* Raw input data is handled elsewhere */
391 if (in != NULL && !rawin) {
392 /* Read the input data */
393 buf_inlen = bio_to_mem(&buf_in, keysize * 10, in);
395 BIO_printf(bio_err, "Error reading input Data\n");
401 size_t l = (size_t)buf_inlen;
402 for (i = 0; i < l / 2; i++) {
404 buf_in[i] = buf_in[l - 1 - i];
405 buf_in[l - 1 - i] = ctmp;
410 /* Sanity check the input if the input is not raw */
412 && buf_inlen > EVP_MAX_MD_SIZE
413 && (pkey_op == EVP_PKEY_OP_SIGN
414 || pkey_op == EVP_PKEY_OP_VERIFY)) {
416 "Error: The input data looks too long to be a hash\n");
420 if (pkey_op == EVP_PKEY_OP_VERIFY) {
422 rv = do_raw_keyop(pkey_op, ctx, md, pkey, in, filesize, sig, siglen,
425 rv = EVP_PKEY_verify(ctx, sig, (size_t)siglen,
426 buf_in, (size_t)buf_inlen);
429 BIO_puts(out, "Signature Verified Successfully\n");
432 BIO_puts(out, "Signature Verification Failure\n");
441 /* rawin allocates the buffer in do_raw_keyop() */
442 rv = do_raw_keyop(pkey_op, ctx, md, pkey, in, filesize, NULL, 0,
443 &buf_out, (size_t *)&buf_outlen);
445 rv = do_keyop(ctx, pkey_op, NULL, (size_t *)&buf_outlen,
446 buf_in, (size_t)buf_inlen);
447 if (rv > 0 && buf_outlen != 0) {
448 buf_out = app_malloc(buf_outlen, "buffer output");
449 rv = do_keyop(ctx, pkey_op,
450 buf_out, (size_t *)&buf_outlen,
451 buf_in, (size_t)buf_inlen);
456 if (pkey_op != EVP_PKEY_OP_DERIVE) {
457 BIO_puts(bio_err, "Public Key operation error\n");
459 BIO_puts(bio_err, "Key derivation failed\n");
461 ERR_print_errors(bio_err);
467 if (!ASN1_parse_dump(out, buf_out, buf_outlen, 1, -1))
468 ERR_print_errors(bio_err);
469 } else if (hexdump) {
470 BIO_dump(out, (char *)buf_out, buf_outlen);
472 BIO_write(out, buf_out, buf_outlen);
476 EVP_PKEY_CTX_free(ctx);
480 OPENSSL_free(buf_in);
481 OPENSSL_free(buf_out);
483 sk_OPENSSL_STRING_free(pkeyopts);
484 sk_OPENSSL_STRING_free(pkeyopts_passin);
488 static EVP_PKEY_CTX *init_ctx(const char *kdfalg, int *pkeysize,
489 const char *keyfile, int keyform, int key_type,
490 char *passinarg, int pkey_op, ENGINE *e,
491 const int engine_impl, int rawin,
494 EVP_PKEY *pkey = NULL;
495 EVP_PKEY_CTX *ctx = NULL;
500 if (((pkey_op == EVP_PKEY_OP_SIGN) || (pkey_op == EVP_PKEY_OP_DECRYPT)
501 || (pkey_op == EVP_PKEY_OP_DERIVE))
502 && (key_type != KEY_PRIVKEY && kdfalg == NULL)) {
503 BIO_printf(bio_err, "A private key is needed for this operation\n");
506 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
507 BIO_printf(bio_err, "Error getting password\n");
512 pkey = load_key(keyfile, keyform, 0, passin, e, "Private Key");
516 pkey = load_pubkey(keyfile, keyform, 0, NULL, e, "Public Key");
520 x = load_cert(keyfile, keyform, "Certificate");
522 pkey = X509_get_pubkey(x);
532 #ifndef OPENSSL_NO_ENGINE
537 if (kdfalg != NULL) {
538 int kdfnid = OBJ_sn2nid(kdfalg);
540 if (kdfnid == NID_undef) {
541 kdfnid = OBJ_ln2nid(kdfalg);
542 if (kdfnid == NID_undef) {
543 BIO_printf(bio_err, "The given KDF \"%s\" is unknown.\n",
548 ctx = EVP_PKEY_CTX_new_id(kdfnid, impl);
553 #ifndef OPENSSL_NO_EC
554 /* SM2 needs a special treatment */
555 if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) {
556 EC_KEY *eckey = NULL;
557 const EC_GROUP *group = NULL;
560 if ((eckey = EVP_PKEY_get0_EC_KEY(pkey)) == NULL
561 || (group = EC_KEY_get0_group(eckey)) == NULL
562 || (nid = EC_GROUP_get_curve_name(group)) == 0)
565 && !EVP_PKEY_set_alias_type(pkey, EVP_PKEY_SM2))
569 *pkeysize = EVP_PKEY_size(pkey);
570 ctx = EVP_PKEY_CTX_new(pkey, impl);
580 * If rawin then we don't need to actually initialise the EVP_PKEY_CTX
581 * itself. That will get initialised during EVP_DigestSignInit or
582 * EVP_DigestVerifyInit.
588 case EVP_PKEY_OP_SIGN:
589 rv = EVP_PKEY_sign_init(ctx);
592 case EVP_PKEY_OP_VERIFY:
593 rv = EVP_PKEY_verify_init(ctx);
596 case EVP_PKEY_OP_VERIFYRECOVER:
597 rv = EVP_PKEY_verify_recover_init(ctx);
600 case EVP_PKEY_OP_ENCRYPT:
601 rv = EVP_PKEY_encrypt_init(ctx);
604 case EVP_PKEY_OP_DECRYPT:
605 rv = EVP_PKEY_decrypt_init(ctx);
608 case EVP_PKEY_OP_DERIVE:
609 rv = EVP_PKEY_derive_init(ctx);
615 EVP_PKEY_CTX_free(ctx);
620 OPENSSL_free(passin);
625 static int setup_peer(EVP_PKEY_CTX *ctx, int peerform, const char *file,
628 EVP_PKEY *peer = NULL;
629 ENGINE *engine = NULL;
632 if (peerform == FORMAT_ENGINE)
634 peer = load_pubkey(file, peerform, 0, NULL, engine, "Peer Key");
636 BIO_printf(bio_err, "Error reading peer key %s\n", file);
637 ERR_print_errors(bio_err);
641 ret = EVP_PKEY_derive_set_peer(ctx, peer);
645 ERR_print_errors(bio_err);
649 static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
650 unsigned char *out, size_t *poutlen,
651 const unsigned char *in, size_t inlen)
655 case EVP_PKEY_OP_VERIFYRECOVER:
656 rv = EVP_PKEY_verify_recover(ctx, out, poutlen, in, inlen);
659 case EVP_PKEY_OP_SIGN:
660 rv = EVP_PKEY_sign(ctx, out, poutlen, in, inlen);
663 case EVP_PKEY_OP_ENCRYPT:
664 rv = EVP_PKEY_encrypt(ctx, out, poutlen, in, inlen);
667 case EVP_PKEY_OP_DECRYPT:
668 rv = EVP_PKEY_decrypt(ctx, out, poutlen, in, inlen);
671 case EVP_PKEY_OP_DERIVE:
672 rv = EVP_PKEY_derive(ctx, out, poutlen);
679 #define TBUF_MAXSIZE 2048
681 static int do_raw_keyop(int pkey_op, EVP_PKEY_CTX *ctx,
682 const EVP_MD *md, EVP_PKEY *pkey, BIO *in,
683 int filesize, unsigned char *sig, int siglen,
684 unsigned char **out, size_t *poutlen)
687 EVP_MD_CTX *mctx = NULL;
688 unsigned char tbuf[TBUF_MAXSIZE];
689 unsigned char *mbuf = NULL;
692 if ((mctx = EVP_MD_CTX_new()) == NULL) {
693 BIO_printf(bio_err, "Error: out of memory\n");
696 EVP_MD_CTX_set_pkey_ctx(mctx, ctx);
698 /* Some algorithms only support oneshot digests */
699 if (EVP_PKEY_id(pkey) == EVP_PKEY_ED25519
700 || EVP_PKEY_id(pkey) == EVP_PKEY_ED448) {
703 "Error: unable to determine file size for oneshot operation\n");
706 mbuf = app_malloc(filesize, "oneshot sign/verify buffer");
708 case EVP_PKEY_OP_VERIFY:
709 if (EVP_DigestVerifyInit(mctx, NULL, md, NULL, pkey) != 1)
711 buf_len = BIO_read(in, mbuf, filesize);
712 if (buf_len != filesize) {
713 BIO_printf(bio_err, "Error reading raw input data\n");
716 rv = EVP_DigestVerify(mctx, sig, (size_t)siglen, mbuf, buf_len);
718 case EVP_PKEY_OP_SIGN:
719 if (EVP_DigestSignInit(mctx, NULL, md, NULL, pkey) != 1)
721 buf_len = BIO_read(in, mbuf, filesize);
722 if (buf_len != filesize) {
723 BIO_printf(bio_err, "Error reading raw input data\n");
726 rv = EVP_DigestSign(mctx, NULL, poutlen, mbuf, buf_len);
727 if (rv == 1 && out != NULL) {
728 *out = app_malloc(*poutlen, "buffer output");
729 rv = EVP_DigestSign(mctx, *out, poutlen, mbuf, buf_len);
737 case EVP_PKEY_OP_VERIFY:
738 if (EVP_DigestVerifyInit(mctx, NULL, md, NULL, pkey) != 1)
741 buf_len = BIO_read(in, tbuf, TBUF_MAXSIZE);
745 BIO_printf(bio_err, "Error reading raw input data\n");
748 rv = EVP_DigestVerifyUpdate(mctx, tbuf, (size_t)buf_len);
750 BIO_printf(bio_err, "Error verifying raw input data\n");
754 rv = EVP_DigestVerifyFinal(mctx, sig, (size_t)siglen);
756 case EVP_PKEY_OP_SIGN:
757 if (EVP_DigestSignInit(mctx, NULL, md, NULL, pkey) != 1)
760 buf_len = BIO_read(in, tbuf, TBUF_MAXSIZE);
764 BIO_printf(bio_err, "Error reading raw input data\n");
767 rv = EVP_DigestSignUpdate(mctx, tbuf, (size_t)buf_len);
769 BIO_printf(bio_err, "Error signing raw input data\n");
773 rv = EVP_DigestSignFinal(mctx, NULL, poutlen);
774 if (rv == 1 && out != NULL) {
775 *out = app_malloc(*poutlen, "buffer output");
776 rv = EVP_DigestSignFinal(mctx, *out, poutlen);
783 EVP_MD_CTX_free(mctx);