pkeyutl.c 25 KB

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