pkeyutl.c 25 KB

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