pkeyutl.c 25 KB

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