pkeyutl.c 25 KB

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