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,
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 typedef enum OPTION_choice {
35 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
36 OPT_ENGINE, OPT_ENGINE_IMPL, OPT_IN, OPT_OUT,
37 OPT_PUBIN, OPT_CERTIN, OPT_ASN1PARSE, OPT_HEXDUMP, OPT_SIGN,
38 OPT_VERIFY, OPT_VERIFYRECOVER, OPT_REV, OPT_ENCRYPT, OPT_DECRYPT,
39 OPT_DERIVE, OPT_SIGFILE, OPT_INKEY, OPT_PEERKEY, OPT_PASSIN,
40 OPT_PEERFORM, OPT_KEYFORM, OPT_PKEYOPT, OPT_PKEYOPT_PASSIN, OPT_KDF,
41 OPT_KDFLEN, OPT_R_ENUM
44 const OPTIONS pkeyutl_options[] = {
45 {"help", OPT_HELP, '-', "Display this summary"},
46 {"in", OPT_IN, '<', "Input file - default stdin"},
47 {"out", OPT_OUT, '>', "Output file - default stdout"},
48 {"pubin", OPT_PUBIN, '-', "Input is a public key"},
49 {"certin", OPT_CERTIN, '-', "Input is a cert with a public key"},
50 {"asn1parse", OPT_ASN1PARSE, '-', "asn1parse the output data"},
51 {"hexdump", OPT_HEXDUMP, '-', "Hex dump output"},
52 {"sign", OPT_SIGN, '-', "Sign input data with private key"},
53 {"verify", OPT_VERIFY, '-', "Verify with public key"},
54 {"verifyrecover", OPT_VERIFYRECOVER, '-',
55 "Verify with public key, recover original data"},
56 {"rev", OPT_REV, '-', "Reverse the order of the input buffer"},
57 {"encrypt", OPT_ENCRYPT, '-', "Encrypt input data with public key"},
58 {"decrypt", OPT_DECRYPT, '-', "Decrypt input data with private key"},
59 {"derive", OPT_DERIVE, '-', "Derive shared secret"},
60 {"kdf", OPT_KDF, 's', "Use KDF algorithm"},
61 {"kdflen", OPT_KDFLEN, 'p', "KDF algorithm output length"},
62 {"sigfile", OPT_SIGFILE, '<', "Signature file (verify operation only)"},
63 {"inkey", OPT_INKEY, 's', "Input private key file"},
64 {"peerkey", OPT_PEERKEY, 's', "Peer key file used in key derivation"},
65 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
66 {"peerform", OPT_PEERFORM, 'E', "Peer key format - default PEM"},
67 {"keyform", OPT_KEYFORM, 'E', "Private key format - default PEM"},
68 {"pkeyopt", OPT_PKEYOPT, 's', "Public key options as opt:value"},
69 {"pkeyopt_passin", OPT_PKEYOPT_PASSIN, 's',
70 "Public key option that is read as a passphrase argument opt:passphrase"},
72 #ifndef OPENSSL_NO_ENGINE
73 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
74 {"engine_impl", OPT_ENGINE_IMPL, '-',
75 "Also use engine given by -engine for crypto operations"},
80 int pkeyutl_main(int argc, char **argv)
82 BIO *in = NULL, *out = NULL;
84 EVP_PKEY_CTX *ctx = NULL;
85 char *infile = NULL, *outfile = NULL, *sigfile = NULL, *passinarg = NULL;
86 char hexdump = 0, asn1parse = 0, rev = 0, *prog;
87 unsigned char *buf_in = NULL, *buf_out = NULL, *sig = NULL;
89 int buf_inlen = 0, siglen = -1, keyform = FORMAT_PEM, peerform = FORMAT_PEM;
90 int keysize = -1, pkey_op = EVP_PKEY_OP_SIGN, key_type = KEY_PRIVKEY;
94 const char *inkey = NULL;
95 const char *peerkey = NULL;
96 const char *kdfalg = NULL;
98 STACK_OF(OPENSSL_STRING) *pkeyopts = NULL;
99 STACK_OF(OPENSSL_STRING) *pkeyopts_passin = NULL;
101 prog = opt_init(argc, argv, pkeyutl_options);
102 while ((o = opt_next()) != OPT_EOF) {
107 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
110 opt_help(pkeyutl_options);
122 case OPT_ENGINE_IMPL:
132 passinarg = opt_arg();
135 if (!opt_format(opt_arg(), OPT_FMT_PDE, &peerform))
139 if (!opt_format(opt_arg(), OPT_FMT_PDE, &keyform))
147 e = setup_engine(opt_arg(), 0);
150 key_type = KEY_PUBKEY;
162 pkey_op = EVP_PKEY_OP_SIGN;
165 pkey_op = EVP_PKEY_OP_VERIFY;
167 case OPT_VERIFYRECOVER:
168 pkey_op = EVP_PKEY_OP_VERIFYRECOVER;
171 pkey_op = EVP_PKEY_OP_ENCRYPT;
174 pkey_op = EVP_PKEY_OP_DECRYPT;
177 pkey_op = EVP_PKEY_OP_DERIVE;
180 pkey_op = EVP_PKEY_OP_DERIVE;
185 kdflen = atoi(opt_arg());
191 if ((pkeyopts == NULL &&
192 (pkeyopts = sk_OPENSSL_STRING_new_null()) == NULL) ||
193 sk_OPENSSL_STRING_push(pkeyopts, opt_arg()) == 0) {
194 BIO_puts(bio_err, "out of memory\n");
198 case OPT_PKEYOPT_PASSIN:
199 if ((pkeyopts_passin == NULL &&
200 (pkeyopts_passin = sk_OPENSSL_STRING_new_null()) == NULL) ||
201 sk_OPENSSL_STRING_push(pkeyopts_passin, opt_arg()) == 0) {
202 BIO_puts(bio_err, "out of memory\n");
208 argc = opt_num_rest();
212 if (kdfalg != NULL) {
215 "%s: no KDF length given (-kdflen parameter).\n", prog);
218 } else if (inkey == NULL) {
220 "%s: no private key given (-inkey parameter).\n", prog);
222 } else if (peerkey != NULL && pkey_op != EVP_PKEY_OP_DERIVE) {
224 "%s: no peer key given (-peerkey parameter).\n", prog);
227 ctx = init_ctx(kdfalg, &keysize, inkey, keyform, key_type,
228 passinarg, pkey_op, e, engine_impl);
230 BIO_printf(bio_err, "%s: Error initializing context\n", prog);
231 ERR_print_errors(bio_err);
234 if (peerkey != NULL && !setup_peer(ctx, peerform, peerkey, e)) {
235 BIO_printf(bio_err, "%s: Error setting up peer key\n", prog);
236 ERR_print_errors(bio_err);
239 if (pkeyopts != NULL) {
240 int num = sk_OPENSSL_STRING_num(pkeyopts);
243 for (i = 0; i < num; ++i) {
244 const char *opt = sk_OPENSSL_STRING_value(pkeyopts, i);
246 if (pkey_ctrl_string(ctx, opt) <= 0) {
247 BIO_printf(bio_err, "%s: Can't set parameter \"%s\":\n",
249 ERR_print_errors(bio_err);
254 if (pkeyopts_passin != NULL) {
255 int num = sk_OPENSSL_STRING_num(pkeyopts_passin);
258 for (i = 0; i < num; i++) {
259 char *opt = sk_OPENSSL_STRING_value(pkeyopts_passin, i);
260 char *passin = strchr(opt, ':');
263 if (passin == NULL) {
264 /* Get password interactively */
265 char passwd_buf[4096];
266 BIO_snprintf(passwd_buf, sizeof(passwd_buf), "Enter %s: ", opt);
267 EVP_read_pw_string(passwd_buf, sizeof(passwd_buf) - 1,
269 passwd = OPENSSL_strdup(passwd_buf);
270 if (passwd == NULL) {
271 BIO_puts(bio_err, "out of memory\n");
275 /* Get password as a passin argument: First split option name
276 * and passphrase argument into two strings */
279 if (app_passwd(passin, NULL, &passwd, NULL) == 0) {
280 BIO_printf(bio_err, "failed to get '%s'\n", opt);
285 if (EVP_PKEY_CTX_ctrl_str(ctx, opt, passwd) <= 0) {
286 BIO_printf(bio_err, "%s: Can't set parameter \"%s\":\n",
290 OPENSSL_free(passwd);
294 if (sigfile != NULL && (pkey_op != EVP_PKEY_OP_VERIFY)) {
296 "%s: Signature file specified for non verify\n", prog);
300 if (sigfile == NULL && (pkey_op == EVP_PKEY_OP_VERIFY)) {
302 "%s: No signature file specified for verify\n", prog);
306 if (pkey_op != EVP_PKEY_OP_DERIVE) {
307 in = bio_open_default(infile, 'r', FORMAT_BINARY);
311 out = bio_open_default(outfile, 'w', FORMAT_BINARY);
315 if (sigfile != NULL) {
316 BIO *sigbio = BIO_new_file(sigfile, "rb");
318 if (sigbio == NULL) {
319 BIO_printf(bio_err, "Can't open signature file %s\n", sigfile);
322 siglen = bio_to_mem(&sig, keysize * 10, sigbio);
325 BIO_printf(bio_err, "Error reading signature data\n");
331 /* Read the input data */
332 buf_inlen = bio_to_mem(&buf_in, keysize * 10, in);
334 BIO_printf(bio_err, "Error reading input Data\n");
340 size_t l = (size_t)buf_inlen;
341 for (i = 0; i < l / 2; i++) {
343 buf_in[i] = buf_in[l - 1 - i];
344 buf_in[l - 1 - i] = ctmp;
349 /* Sanity check the input */
350 if (buf_inlen > EVP_MAX_MD_SIZE
351 && (pkey_op == EVP_PKEY_OP_SIGN
352 || pkey_op == EVP_PKEY_OP_VERIFY
353 || pkey_op == EVP_PKEY_OP_VERIFYRECOVER)) {
355 "Error: The input data looks too long to be a hash\n");
359 if (pkey_op == EVP_PKEY_OP_VERIFY) {
360 rv = EVP_PKEY_verify(ctx, sig, (size_t)siglen,
361 buf_in, (size_t)buf_inlen);
363 BIO_puts(out, "Signature Verified Successfully\n");
366 BIO_puts(out, "Signature Verification Failure\n");
374 rv = do_keyop(ctx, pkey_op, NULL, (size_t *)&buf_outlen,
375 buf_in, (size_t)buf_inlen);
377 if (rv > 0 && buf_outlen != 0) {
378 buf_out = app_malloc(buf_outlen, "buffer output");
379 rv = do_keyop(ctx, pkey_op,
380 buf_out, (size_t *)&buf_outlen,
381 buf_in, (size_t)buf_inlen);
384 if (pkey_op != EVP_PKEY_OP_DERIVE) {
385 BIO_puts(bio_err, "Public Key operation error\n");
387 BIO_puts(bio_err, "Key derivation failed\n");
389 ERR_print_errors(bio_err);
395 if (!ASN1_parse_dump(out, buf_out, buf_outlen, 1, -1))
396 ERR_print_errors(bio_err);
397 } else if (hexdump) {
398 BIO_dump(out, (char *)buf_out, buf_outlen);
400 BIO_write(out, buf_out, buf_outlen);
404 EVP_PKEY_CTX_free(ctx);
408 OPENSSL_free(buf_in);
409 OPENSSL_free(buf_out);
411 sk_OPENSSL_STRING_free(pkeyopts);
412 sk_OPENSSL_STRING_free(pkeyopts_passin);
416 static EVP_PKEY_CTX *init_ctx(const char *kdfalg, int *pkeysize,
417 const char *keyfile, int keyform, int key_type,
418 char *passinarg, int pkey_op, ENGINE *e,
419 const int engine_impl)
421 EVP_PKEY *pkey = NULL;
422 EVP_PKEY_CTX *ctx = NULL;
427 if (((pkey_op == EVP_PKEY_OP_SIGN) || (pkey_op == EVP_PKEY_OP_DECRYPT)
428 || (pkey_op == EVP_PKEY_OP_DERIVE))
429 && (key_type != KEY_PRIVKEY && kdfalg == NULL)) {
430 BIO_printf(bio_err, "A private key is needed for this operation\n");
433 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
434 BIO_printf(bio_err, "Error getting password\n");
439 pkey = load_key(keyfile, keyform, 0, passin, e, "Private Key");
443 pkey = load_pubkey(keyfile, keyform, 0, NULL, e, "Public Key");
447 x = load_cert(keyfile, keyform, "Certificate");
449 pkey = X509_get_pubkey(x);
459 #ifndef OPENSSL_NO_ENGINE
464 if (kdfalg != NULL) {
465 int kdfnid = OBJ_sn2nid(kdfalg);
467 if (kdfnid == NID_undef) {
468 kdfnid = OBJ_ln2nid(kdfalg);
469 if (kdfnid == NID_undef) {
470 BIO_printf(bio_err, "The given KDF \"%s\" is unknown.\n",
475 ctx = EVP_PKEY_CTX_new_id(kdfnid, impl);
479 *pkeysize = EVP_PKEY_size(pkey);
480 ctx = EVP_PKEY_CTX_new(pkey, impl);
488 case EVP_PKEY_OP_SIGN:
489 rv = EVP_PKEY_sign_init(ctx);
492 case EVP_PKEY_OP_VERIFY:
493 rv = EVP_PKEY_verify_init(ctx);
496 case EVP_PKEY_OP_VERIFYRECOVER:
497 rv = EVP_PKEY_verify_recover_init(ctx);
500 case EVP_PKEY_OP_ENCRYPT:
501 rv = EVP_PKEY_encrypt_init(ctx);
504 case EVP_PKEY_OP_DECRYPT:
505 rv = EVP_PKEY_decrypt_init(ctx);
508 case EVP_PKEY_OP_DERIVE:
509 rv = EVP_PKEY_derive_init(ctx);
514 EVP_PKEY_CTX_free(ctx);
519 OPENSSL_free(passin);
524 static int setup_peer(EVP_PKEY_CTX *ctx, int peerform, const char *file,
527 EVP_PKEY *peer = NULL;
528 ENGINE *engine = NULL;
531 if (peerform == FORMAT_ENGINE)
533 peer = load_pubkey(file, peerform, 0, NULL, engine, "Peer Key");
535 BIO_printf(bio_err, "Error reading peer key %s\n", file);
536 ERR_print_errors(bio_err);
540 ret = EVP_PKEY_derive_set_peer(ctx, peer);
544 ERR_print_errors(bio_err);
548 static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
549 unsigned char *out, size_t *poutlen,
550 const unsigned char *in, size_t inlen)
554 case EVP_PKEY_OP_VERIFYRECOVER:
555 rv = EVP_PKEY_verify_recover(ctx, out, poutlen, in, inlen);
558 case EVP_PKEY_OP_SIGN:
559 rv = EVP_PKEY_sign(ctx, out, poutlen, in, inlen);
562 case EVP_PKEY_OP_ENCRYPT:
563 rv = EVP_PKEY_encrypt(ctx, out, poutlen, in, inlen);
566 case EVP_PKEY_OP_DECRYPT:
567 rv = EVP_PKEY_decrypt(ctx, out, poutlen, in, inlen);
570 case EVP_PKEY_OP_DERIVE:
571 rv = EVP_PKEY_derive(ctx, out, poutlen);