pkeyutl.c 25 KB

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