pkeyutl.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. /*
  2. * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  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
  8. */
  9. #include "apps.h"
  10. #include "progs.h"
  11. #include <string.h>
  12. #include <openssl/err.h>
  13. #include <openssl/pem.h>
  14. #include <openssl/evp.h>
  15. #include <sys/stat.h>
  16. DEFINE_STACK_OF_STRING()
  17. #define KEY_NONE 0
  18. #define KEY_PRIVKEY 1
  19. #define KEY_PUBKEY 2
  20. #define KEY_CERT 3
  21. static EVP_PKEY_CTX *init_ctx(const char *kdfalg, int *pkeysize,
  22. const char *keyfile, int keyform, int key_type,
  23. char *passinarg, int pkey_op, ENGINE *e,
  24. const int impl, int rawin, EVP_PKEY **ppkey);
  25. static int setup_peer(EVP_PKEY_CTX *ctx, int peerform, const char *file,
  26. ENGINE *e);
  27. static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
  28. unsigned char *out, size_t *poutlen,
  29. const unsigned char *in, size_t inlen);
  30. static int do_raw_keyop(int pkey_op, EVP_PKEY_CTX *ctx,
  31. const EVP_MD *md, EVP_PKEY *pkey, BIO *in,
  32. int filesize, unsigned char *sig, int siglen,
  33. unsigned char **out, size_t *poutlen);
  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, OPT_PROV_ENUM,
  42. OPT_RAWIN, OPT_DIGEST
  43. } OPTION_CHOICE;
  44. const OPTIONS pkeyutl_options[] = {
  45. OPT_SECTION("General"),
  46. {"help", OPT_HELP, '-', "Display this summary"},
  47. #ifndef OPENSSL_NO_ENGINE
  48. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  49. {"engine_impl", OPT_ENGINE_IMPL, '-',
  50. "Also use engine given by -engine for crypto operations"},
  51. #endif
  52. {"sign", OPT_SIGN, '-', "Sign input data with private key"},
  53. {"verify", OPT_VERIFY, '-', "Verify with public key"},
  54. {"encrypt", OPT_ENCRYPT, '-', "Encrypt input data with public key"},
  55. {"decrypt", OPT_DECRYPT, '-', "Decrypt input data with private key"},
  56. {"derive", OPT_DERIVE, '-', "Derive shared secret"},
  57. OPT_SECTION("Input"),
  58. {"in", OPT_IN, '<', "Input file - default stdin"},
  59. {"rawin", OPT_RAWIN, '-', "Indicate the input data is in raw form"},
  60. {"pubin", OPT_PUBIN, '-', "Input is a public key"},
  61. {"inkey", OPT_INKEY, 's', "Input private key file"},
  62. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  63. {"peerkey", OPT_PEERKEY, 's', "Peer key file used in key derivation"},
  64. {"peerform", OPT_PEERFORM, 'E', "Peer key format (DER/PEM/P12/ENGINE)"},
  65. {"certin", OPT_CERTIN, '-', "Input is a cert with a public key"},
  66. {"rev", OPT_REV, '-', "Reverse the order of the input buffer"},
  67. {"sigfile", OPT_SIGFILE, '<', "Signature file (verify operation only)"},
  68. {"keyform", OPT_KEYFORM, 'E', "Private key format (ENGINE, other values ignored)"},
  69. OPT_SECTION("Output"),
  70. {"out", OPT_OUT, '>', "Output file - default stdout"},
  71. {"asn1parse", OPT_ASN1PARSE, '-', "asn1parse the output data"},
  72. {"hexdump", OPT_HEXDUMP, '-', "Hex dump output"},
  73. {"verifyrecover", OPT_VERIFYRECOVER, '-',
  74. "Verify with public key, recover original data"},
  75. OPT_SECTION("Signing/Derivation"),
  76. {"digest", OPT_DIGEST, 's',
  77. "Specify the digest algorithm when signing the raw input data"},
  78. {"pkeyopt", OPT_PKEYOPT, 's', "Public key options as opt:value"},
  79. {"pkeyopt_passin", OPT_PKEYOPT_PASSIN, 's',
  80. "Public key option that is read as a passphrase argument opt:passphrase"},
  81. {"kdf", OPT_KDF, 's', "Use KDF algorithm"},
  82. {"kdflen", OPT_KDFLEN, 'p', "KDF algorithm output length"},
  83. OPT_R_OPTIONS,
  84. OPT_PROV_OPTIONS,
  85. {NULL}
  86. };
  87. int pkeyutl_main(int argc, char **argv)
  88. {
  89. BIO *in = NULL, *out = NULL;
  90. ENGINE *e = NULL;
  91. EVP_PKEY_CTX *ctx = NULL;
  92. EVP_PKEY *pkey = NULL;
  93. char *infile = NULL, *outfile = NULL, *sigfile = NULL, *passinarg = NULL;
  94. char hexdump = 0, asn1parse = 0, rev = 0, *prog;
  95. unsigned char *buf_in = NULL, *buf_out = NULL, *sig = NULL;
  96. OPTION_CHOICE o;
  97. int buf_inlen = 0, siglen = -1, keyform = FORMAT_PEM, peerform = FORMAT_PEM;
  98. int keysize = -1, pkey_op = EVP_PKEY_OP_SIGN, key_type = KEY_PRIVKEY;
  99. int engine_impl = 0;
  100. int ret = 1, rv = -1;
  101. size_t buf_outlen;
  102. const char *inkey = NULL;
  103. const char *peerkey = NULL;
  104. const char *kdfalg = NULL;
  105. int kdflen = 0;
  106. STACK_OF(OPENSSL_STRING) *pkeyopts = NULL;
  107. STACK_OF(OPENSSL_STRING) *pkeyopts_passin = NULL;
  108. int rawin = 0;
  109. const EVP_MD *md = NULL;
  110. int filesize = -1;
  111. prog = opt_init(argc, argv, pkeyutl_options);
  112. while ((o = opt_next()) != OPT_EOF) {
  113. switch (o) {
  114. case OPT_EOF:
  115. case OPT_ERR:
  116. opthelp:
  117. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  118. goto end;
  119. case OPT_HELP:
  120. opt_help(pkeyutl_options);
  121. ret = 0;
  122. goto end;
  123. case OPT_IN:
  124. infile = opt_arg();
  125. break;
  126. case OPT_OUT:
  127. outfile = opt_arg();
  128. break;
  129. case OPT_SIGFILE:
  130. sigfile = opt_arg();
  131. break;
  132. case OPT_ENGINE_IMPL:
  133. engine_impl = 1;
  134. break;
  135. case OPT_INKEY:
  136. inkey = opt_arg();
  137. break;
  138. case OPT_PEERKEY:
  139. peerkey = opt_arg();
  140. break;
  141. case OPT_PASSIN:
  142. passinarg = opt_arg();
  143. break;
  144. case OPT_PEERFORM:
  145. if (!opt_format(opt_arg(), OPT_FMT_ANY, &peerform))
  146. goto opthelp;
  147. break;
  148. case OPT_KEYFORM:
  149. if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
  150. goto opthelp;
  151. break;
  152. case OPT_R_CASES:
  153. if (!opt_rand(o))
  154. goto end;
  155. break;
  156. case OPT_PROV_CASES:
  157. if (!opt_provider(o))
  158. goto end;
  159. break;
  160. case OPT_ENGINE:
  161. e = setup_engine(opt_arg(), 0);
  162. break;
  163. case OPT_PUBIN:
  164. key_type = KEY_PUBKEY;
  165. break;
  166. case OPT_CERTIN:
  167. key_type = KEY_CERT;
  168. break;
  169. case OPT_ASN1PARSE:
  170. asn1parse = 1;
  171. break;
  172. case OPT_HEXDUMP:
  173. hexdump = 1;
  174. break;
  175. case OPT_SIGN:
  176. pkey_op = EVP_PKEY_OP_SIGN;
  177. break;
  178. case OPT_VERIFY:
  179. pkey_op = EVP_PKEY_OP_VERIFY;
  180. break;
  181. case OPT_VERIFYRECOVER:
  182. pkey_op = EVP_PKEY_OP_VERIFYRECOVER;
  183. break;
  184. case OPT_ENCRYPT:
  185. pkey_op = EVP_PKEY_OP_ENCRYPT;
  186. break;
  187. case OPT_DECRYPT:
  188. pkey_op = EVP_PKEY_OP_DECRYPT;
  189. break;
  190. case OPT_DERIVE:
  191. pkey_op = EVP_PKEY_OP_DERIVE;
  192. break;
  193. case OPT_KDF:
  194. pkey_op = EVP_PKEY_OP_DERIVE;
  195. key_type = KEY_NONE;
  196. kdfalg = opt_arg();
  197. break;
  198. case OPT_KDFLEN:
  199. kdflen = atoi(opt_arg());
  200. break;
  201. case OPT_REV:
  202. rev = 1;
  203. break;
  204. case OPT_PKEYOPT:
  205. if ((pkeyopts == NULL &&
  206. (pkeyopts = sk_OPENSSL_STRING_new_null()) == NULL) ||
  207. sk_OPENSSL_STRING_push(pkeyopts, opt_arg()) == 0) {
  208. BIO_puts(bio_err, "out of memory\n");
  209. goto end;
  210. }
  211. break;
  212. case OPT_PKEYOPT_PASSIN:
  213. if ((pkeyopts_passin == NULL &&
  214. (pkeyopts_passin = sk_OPENSSL_STRING_new_null()) == NULL) ||
  215. sk_OPENSSL_STRING_push(pkeyopts_passin, opt_arg()) == 0) {
  216. BIO_puts(bio_err, "out of memory\n");
  217. goto end;
  218. }
  219. break;
  220. case OPT_RAWIN:
  221. rawin = 1;
  222. break;
  223. case OPT_DIGEST:
  224. if (!opt_md(opt_arg(), &md))
  225. goto end;
  226. break;
  227. }
  228. }
  229. argc = opt_num_rest();
  230. if (argc != 0)
  231. goto opthelp;
  232. if (rawin && pkey_op != EVP_PKEY_OP_SIGN && pkey_op != EVP_PKEY_OP_VERIFY) {
  233. BIO_printf(bio_err,
  234. "%s: -rawin can only be used with -sign or -verify\n",
  235. prog);
  236. goto opthelp;
  237. }
  238. if (md != NULL && !rawin) {
  239. BIO_printf(bio_err,
  240. "%s: -digest can only be used with -rawin\n",
  241. prog);
  242. goto opthelp;
  243. }
  244. if (rawin && rev) {
  245. BIO_printf(bio_err, "%s: -rev cannot be used with raw input\n",
  246. prog);
  247. goto opthelp;
  248. }
  249. if (kdfalg != NULL) {
  250. if (kdflen == 0) {
  251. BIO_printf(bio_err,
  252. "%s: no KDF length given (-kdflen parameter).\n", prog);
  253. goto opthelp;
  254. }
  255. } else if (inkey == NULL) {
  256. BIO_printf(bio_err,
  257. "%s: no private key given (-inkey parameter).\n", prog);
  258. goto opthelp;
  259. } else if (peerkey != NULL && pkey_op != EVP_PKEY_OP_DERIVE) {
  260. BIO_printf(bio_err,
  261. "%s: no peer key given (-peerkey parameter).\n", prog);
  262. goto opthelp;
  263. }
  264. ctx = init_ctx(kdfalg, &keysize, inkey, keyform, key_type,
  265. passinarg, pkey_op, e, engine_impl, rawin, &pkey);
  266. if (ctx == NULL) {
  267. BIO_printf(bio_err, "%s: Error initializing context\n", prog);
  268. ERR_print_errors(bio_err);
  269. goto end;
  270. }
  271. if (peerkey != NULL && !setup_peer(ctx, peerform, peerkey, e)) {
  272. BIO_printf(bio_err, "%s: Error setting up peer key\n", prog);
  273. ERR_print_errors(bio_err);
  274. goto end;
  275. }
  276. if (pkeyopts != NULL) {
  277. int num = sk_OPENSSL_STRING_num(pkeyopts);
  278. int i;
  279. for (i = 0; i < num; ++i) {
  280. const char *opt = sk_OPENSSL_STRING_value(pkeyopts, i);
  281. if (pkey_ctrl_string(ctx, opt) <= 0) {
  282. BIO_printf(bio_err, "%s: Can't set parameter \"%s\":\n",
  283. prog, opt);
  284. ERR_print_errors(bio_err);
  285. goto end;
  286. }
  287. }
  288. }
  289. if (pkeyopts_passin != NULL) {
  290. int num = sk_OPENSSL_STRING_num(pkeyopts_passin);
  291. int i;
  292. for (i = 0; i < num; i++) {
  293. char *opt = sk_OPENSSL_STRING_value(pkeyopts_passin, i);
  294. char *passin = strchr(opt, ':');
  295. char *passwd;
  296. if (passin == NULL) {
  297. /* Get password interactively */
  298. char passwd_buf[4096];
  299. BIO_snprintf(passwd_buf, sizeof(passwd_buf), "Enter %s: ", opt);
  300. EVP_read_pw_string(passwd_buf, sizeof(passwd_buf) - 1,
  301. passwd_buf, 0);
  302. passwd = OPENSSL_strdup(passwd_buf);
  303. if (passwd == NULL) {
  304. BIO_puts(bio_err, "out of memory\n");
  305. goto end;
  306. }
  307. } else {
  308. /* Get password as a passin argument: First split option name
  309. * and passphrase argument into two strings */
  310. *passin = 0;
  311. passin++;
  312. if (app_passwd(passin, NULL, &passwd, NULL) == 0) {
  313. BIO_printf(bio_err, "failed to get '%s'\n", opt);
  314. goto end;
  315. }
  316. }
  317. if (EVP_PKEY_CTX_ctrl_str(ctx, opt, passwd) <= 0) {
  318. BIO_printf(bio_err, "%s: Can't set parameter \"%s\":\n",
  319. prog, opt);
  320. goto end;
  321. }
  322. OPENSSL_free(passwd);
  323. }
  324. }
  325. if (sigfile != NULL && (pkey_op != EVP_PKEY_OP_VERIFY)) {
  326. BIO_printf(bio_err,
  327. "%s: Signature file specified for non verify\n", prog);
  328. goto end;
  329. }
  330. if (sigfile == NULL && (pkey_op == EVP_PKEY_OP_VERIFY)) {
  331. BIO_printf(bio_err,
  332. "%s: No signature file specified for verify\n", prog);
  333. goto end;
  334. }
  335. if (pkey_op != EVP_PKEY_OP_DERIVE) {
  336. in = bio_open_default(infile, 'r', FORMAT_BINARY);
  337. if (infile != NULL) {
  338. struct stat st;
  339. if (stat(infile, &st) == 0 && st.st_size <= INT_MAX)
  340. filesize = (int)st.st_size;
  341. }
  342. if (in == NULL)
  343. goto end;
  344. }
  345. out = bio_open_default(outfile, 'w', FORMAT_BINARY);
  346. if (out == NULL)
  347. goto end;
  348. if (sigfile != NULL) {
  349. BIO *sigbio = BIO_new_file(sigfile, "rb");
  350. if (sigbio == NULL) {
  351. BIO_printf(bio_err, "Can't open signature file %s\n", sigfile);
  352. goto end;
  353. }
  354. siglen = bio_to_mem(&sig, keysize * 10, sigbio);
  355. BIO_free(sigbio);
  356. if (siglen < 0) {
  357. BIO_printf(bio_err, "Error reading signature data\n");
  358. goto end;
  359. }
  360. }
  361. /* Raw input data is handled elsewhere */
  362. if (in != NULL && !rawin) {
  363. /* Read the input data */
  364. buf_inlen = bio_to_mem(&buf_in, keysize * 10, in);
  365. if (buf_inlen < 0) {
  366. BIO_printf(bio_err, "Error reading input Data\n");
  367. goto end;
  368. }
  369. if (rev) {
  370. size_t i;
  371. unsigned char ctmp;
  372. size_t l = (size_t)buf_inlen;
  373. for (i = 0; i < l / 2; i++) {
  374. ctmp = buf_in[i];
  375. buf_in[i] = buf_in[l - 1 - i];
  376. buf_in[l - 1 - i] = ctmp;
  377. }
  378. }
  379. }
  380. /* Sanity check the input if the input is not raw */
  381. if (!rawin
  382. && buf_inlen > EVP_MAX_MD_SIZE
  383. && (pkey_op == EVP_PKEY_OP_SIGN
  384. || pkey_op == EVP_PKEY_OP_VERIFY)) {
  385. BIO_printf(bio_err,
  386. "Error: The input data looks too long to be a hash\n");
  387. goto end;
  388. }
  389. if (pkey_op == EVP_PKEY_OP_VERIFY) {
  390. if (rawin) {
  391. rv = do_raw_keyop(pkey_op, ctx, md, pkey, in, filesize, sig, siglen,
  392. NULL, 0);
  393. } else {
  394. rv = EVP_PKEY_verify(ctx, sig, (size_t)siglen,
  395. buf_in, (size_t)buf_inlen);
  396. }
  397. if (rv == 1) {
  398. BIO_puts(out, "Signature Verified Successfully\n");
  399. ret = 0;
  400. } else {
  401. BIO_puts(out, "Signature Verification Failure\n");
  402. }
  403. goto end;
  404. }
  405. if (kdflen != 0) {
  406. buf_outlen = kdflen;
  407. rv = 1;
  408. } else {
  409. if (rawin) {
  410. /* rawin allocates the buffer in do_raw_keyop() */
  411. rv = do_raw_keyop(pkey_op, ctx, md, pkey, in, filesize, NULL, 0,
  412. &buf_out, (size_t *)&buf_outlen);
  413. } else {
  414. rv = do_keyop(ctx, pkey_op, NULL, (size_t *)&buf_outlen,
  415. buf_in, (size_t)buf_inlen);
  416. if (rv > 0 && buf_outlen != 0) {
  417. buf_out = app_malloc(buf_outlen, "buffer output");
  418. rv = do_keyop(ctx, pkey_op,
  419. buf_out, (size_t *)&buf_outlen,
  420. buf_in, (size_t)buf_inlen);
  421. }
  422. }
  423. }
  424. if (rv <= 0) {
  425. if (pkey_op != EVP_PKEY_OP_DERIVE) {
  426. BIO_puts(bio_err, "Public Key operation error\n");
  427. } else {
  428. BIO_puts(bio_err, "Key derivation failed\n");
  429. }
  430. ERR_print_errors(bio_err);
  431. goto end;
  432. }
  433. ret = 0;
  434. if (asn1parse) {
  435. if (!ASN1_parse_dump(out, buf_out, buf_outlen, 1, -1))
  436. ERR_print_errors(bio_err);
  437. } else if (hexdump) {
  438. BIO_dump(out, (char *)buf_out, buf_outlen);
  439. } else {
  440. BIO_write(out, buf_out, buf_outlen);
  441. }
  442. end:
  443. EVP_PKEY_CTX_free(ctx);
  444. release_engine(e);
  445. BIO_free(in);
  446. BIO_free_all(out);
  447. OPENSSL_free(buf_in);
  448. OPENSSL_free(buf_out);
  449. OPENSSL_free(sig);
  450. sk_OPENSSL_STRING_free(pkeyopts);
  451. sk_OPENSSL_STRING_free(pkeyopts_passin);
  452. return ret;
  453. }
  454. static EVP_PKEY_CTX *init_ctx(const char *kdfalg, int *pkeysize,
  455. const char *keyfile, int keyform, int key_type,
  456. char *passinarg, int pkey_op, ENGINE *e,
  457. const int engine_impl, int rawin,
  458. EVP_PKEY **ppkey)
  459. {
  460. EVP_PKEY *pkey = NULL;
  461. EVP_PKEY_CTX *ctx = NULL;
  462. ENGINE *impl = NULL;
  463. char *passin = NULL;
  464. int rv = -1;
  465. X509 *x;
  466. if (((pkey_op == EVP_PKEY_OP_SIGN) || (pkey_op == EVP_PKEY_OP_DECRYPT)
  467. || (pkey_op == EVP_PKEY_OP_DERIVE))
  468. && (key_type != KEY_PRIVKEY && kdfalg == NULL)) {
  469. BIO_printf(bio_err, "A private key is needed for this operation\n");
  470. goto end;
  471. }
  472. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  473. BIO_printf(bio_err, "Error getting password\n");
  474. goto end;
  475. }
  476. switch (key_type) {
  477. case KEY_PRIVKEY:
  478. pkey = load_key(keyfile, keyform, 0, passin, e, "Private Key");
  479. break;
  480. case KEY_PUBKEY:
  481. pkey = load_pubkey(keyfile, keyform, 0, NULL, e, "Public Key");
  482. break;
  483. case KEY_CERT:
  484. x = load_cert(keyfile, FORMAT_UNDEF, "Certificate");
  485. if (x) {
  486. pkey = X509_get_pubkey(x);
  487. X509_free(x);
  488. }
  489. break;
  490. case KEY_NONE:
  491. break;
  492. }
  493. #ifndef OPENSSL_NO_ENGINE
  494. if (engine_impl)
  495. impl = e;
  496. #endif
  497. if (kdfalg != NULL) {
  498. int kdfnid = OBJ_sn2nid(kdfalg);
  499. if (kdfnid == NID_undef) {
  500. kdfnid = OBJ_ln2nid(kdfalg);
  501. if (kdfnid == NID_undef) {
  502. BIO_printf(bio_err, "The given KDF \"%s\" is unknown.\n",
  503. kdfalg);
  504. goto end;
  505. }
  506. }
  507. ctx = EVP_PKEY_CTX_new_id(kdfnid, impl);
  508. } else {
  509. if (pkey == NULL)
  510. goto end;
  511. *pkeysize = EVP_PKEY_size(pkey);
  512. ctx = EVP_PKEY_CTX_new(pkey, impl);
  513. if (ppkey != NULL)
  514. *ppkey = pkey;
  515. EVP_PKEY_free(pkey);
  516. }
  517. if (ctx == NULL)
  518. goto end;
  519. /*
  520. * If rawin then we don't need to actually initialise the EVP_PKEY_CTX
  521. * itself. That will get initialised during EVP_DigestSignInit or
  522. * EVP_DigestVerifyInit.
  523. */
  524. if (rawin) {
  525. rv = 1;
  526. } else {
  527. switch (pkey_op) {
  528. case EVP_PKEY_OP_SIGN:
  529. rv = EVP_PKEY_sign_init(ctx);
  530. break;
  531. case EVP_PKEY_OP_VERIFY:
  532. rv = EVP_PKEY_verify_init(ctx);
  533. break;
  534. case EVP_PKEY_OP_VERIFYRECOVER:
  535. rv = EVP_PKEY_verify_recover_init(ctx);
  536. break;
  537. case EVP_PKEY_OP_ENCRYPT:
  538. rv = EVP_PKEY_encrypt_init(ctx);
  539. break;
  540. case EVP_PKEY_OP_DECRYPT:
  541. rv = EVP_PKEY_decrypt_init(ctx);
  542. break;
  543. case EVP_PKEY_OP_DERIVE:
  544. rv = EVP_PKEY_derive_init(ctx);
  545. break;
  546. }
  547. }
  548. if (rv <= 0) {
  549. EVP_PKEY_CTX_free(ctx);
  550. ctx = NULL;
  551. }
  552. end:
  553. OPENSSL_free(passin);
  554. return ctx;
  555. }
  556. static int setup_peer(EVP_PKEY_CTX *ctx, int peerform, const char *file,
  557. ENGINE *e)
  558. {
  559. EVP_PKEY *peer = NULL;
  560. ENGINE *engine = NULL;
  561. int ret;
  562. if (peerform == FORMAT_ENGINE)
  563. engine = e;
  564. peer = load_pubkey(file, peerform, 0, NULL, engine, "Peer Key");
  565. if (peer == NULL) {
  566. BIO_printf(bio_err, "Error reading peer key %s\n", file);
  567. ERR_print_errors(bio_err);
  568. return 0;
  569. }
  570. ret = EVP_PKEY_derive_set_peer(ctx, peer);
  571. EVP_PKEY_free(peer);
  572. if (ret <= 0)
  573. ERR_print_errors(bio_err);
  574. return ret;
  575. }
  576. static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
  577. unsigned char *out, size_t *poutlen,
  578. const unsigned char *in, size_t inlen)
  579. {
  580. int rv = 0;
  581. switch (pkey_op) {
  582. case EVP_PKEY_OP_VERIFYRECOVER:
  583. rv = EVP_PKEY_verify_recover(ctx, out, poutlen, in, inlen);
  584. break;
  585. case EVP_PKEY_OP_SIGN:
  586. rv = EVP_PKEY_sign(ctx, out, poutlen, in, inlen);
  587. break;
  588. case EVP_PKEY_OP_ENCRYPT:
  589. rv = EVP_PKEY_encrypt(ctx, out, poutlen, in, inlen);
  590. break;
  591. case EVP_PKEY_OP_DECRYPT:
  592. rv = EVP_PKEY_decrypt(ctx, out, poutlen, in, inlen);
  593. break;
  594. case EVP_PKEY_OP_DERIVE:
  595. rv = EVP_PKEY_derive(ctx, out, poutlen);
  596. break;
  597. }
  598. return rv;
  599. }
  600. #define TBUF_MAXSIZE 2048
  601. static int do_raw_keyop(int pkey_op, EVP_PKEY_CTX *ctx,
  602. const EVP_MD *md, EVP_PKEY *pkey, BIO *in,
  603. int filesize, unsigned char *sig, int siglen,
  604. unsigned char **out, size_t *poutlen)
  605. {
  606. int rv = 0;
  607. EVP_MD_CTX *mctx = NULL;
  608. unsigned char tbuf[TBUF_MAXSIZE];
  609. unsigned char *mbuf = NULL;
  610. int buf_len = 0;
  611. if ((mctx = EVP_MD_CTX_new()) == NULL) {
  612. BIO_printf(bio_err, "Error: out of memory\n");
  613. return rv;
  614. }
  615. EVP_MD_CTX_set_pkey_ctx(mctx, ctx);
  616. /* Some algorithms only support oneshot digests */
  617. if (EVP_PKEY_id(pkey) == EVP_PKEY_ED25519
  618. || EVP_PKEY_id(pkey) == EVP_PKEY_ED448) {
  619. if (filesize < 0) {
  620. BIO_printf(bio_err,
  621. "Error: unable to determine file size for oneshot operation\n");
  622. goto end;
  623. }
  624. mbuf = app_malloc(filesize, "oneshot sign/verify buffer");
  625. switch(pkey_op) {
  626. case EVP_PKEY_OP_VERIFY:
  627. if (EVP_DigestVerifyInit(mctx, NULL, md, NULL, pkey) != 1)
  628. goto end;
  629. buf_len = BIO_read(in, mbuf, filesize);
  630. if (buf_len != filesize) {
  631. BIO_printf(bio_err, "Error reading raw input data\n");
  632. goto end;
  633. }
  634. rv = EVP_DigestVerify(mctx, sig, (size_t)siglen, mbuf, buf_len);
  635. break;
  636. case EVP_PKEY_OP_SIGN:
  637. if (EVP_DigestSignInit(mctx, NULL, md, NULL, pkey) != 1)
  638. goto end;
  639. buf_len = BIO_read(in, mbuf, filesize);
  640. if (buf_len != filesize) {
  641. BIO_printf(bio_err, "Error reading raw input data\n");
  642. goto end;
  643. }
  644. rv = EVP_DigestSign(mctx, NULL, poutlen, mbuf, buf_len);
  645. if (rv == 1 && out != NULL) {
  646. *out = app_malloc(*poutlen, "buffer output");
  647. rv = EVP_DigestSign(mctx, *out, poutlen, mbuf, buf_len);
  648. }
  649. break;
  650. }
  651. goto end;
  652. }
  653. switch(pkey_op) {
  654. case EVP_PKEY_OP_VERIFY:
  655. if (EVP_DigestVerifyInit(mctx, NULL, md, NULL, pkey) != 1)
  656. goto end;
  657. for (;;) {
  658. buf_len = BIO_read(in, tbuf, TBUF_MAXSIZE);
  659. if (buf_len == 0)
  660. break;
  661. if (buf_len < 0) {
  662. BIO_printf(bio_err, "Error reading raw input data\n");
  663. goto end;
  664. }
  665. rv = EVP_DigestVerifyUpdate(mctx, tbuf, (size_t)buf_len);
  666. if (rv != 1) {
  667. BIO_printf(bio_err, "Error verifying raw input data\n");
  668. goto end;
  669. }
  670. }
  671. rv = EVP_DigestVerifyFinal(mctx, sig, (size_t)siglen);
  672. break;
  673. case EVP_PKEY_OP_SIGN:
  674. if (EVP_DigestSignInit(mctx, NULL, md, NULL, pkey) != 1)
  675. goto end;
  676. for (;;) {
  677. buf_len = BIO_read(in, tbuf, TBUF_MAXSIZE);
  678. if (buf_len == 0)
  679. break;
  680. if (buf_len < 0) {
  681. BIO_printf(bio_err, "Error reading raw input data\n");
  682. goto end;
  683. }
  684. rv = EVP_DigestSignUpdate(mctx, tbuf, (size_t)buf_len);
  685. if (rv != 1) {
  686. BIO_printf(bio_err, "Error signing raw input data\n");
  687. goto end;
  688. }
  689. }
  690. rv = EVP_DigestSignFinal(mctx, NULL, poutlen);
  691. if (rv == 1 && out != NULL) {
  692. *out = app_malloc(*poutlen, "buffer output");
  693. rv = EVP_DigestSignFinal(mctx, *out, poutlen);
  694. }
  695. break;
  696. }
  697. end:
  698. OPENSSL_free(mbuf);
  699. EVP_MD_CTX_free(mctx);
  700. return rv;
  701. }