pkeyutl.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. /*
  2. * Copyright 2006-2023 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. goto end;
  347. }
  348. OPENSSL_free(passwd);
  349. }
  350. }
  351. if (sigfile != NULL && (pkey_op != EVP_PKEY_OP_VERIFY)) {
  352. BIO_printf(bio_err,
  353. "%s: Signature file specified for non verify\n", prog);
  354. goto end;
  355. }
  356. if (sigfile == NULL && (pkey_op == EVP_PKEY_OP_VERIFY)) {
  357. BIO_printf(bio_err,
  358. "%s: No signature file specified for verify\n", prog);
  359. goto end;
  360. }
  361. if (pkey_op != EVP_PKEY_OP_DERIVE) {
  362. in = bio_open_default(infile, 'r', FORMAT_BINARY);
  363. if (infile != NULL) {
  364. struct stat st;
  365. if (stat(infile, &st) == 0 && st.st_size <= INT_MAX)
  366. filesize = (int)st.st_size;
  367. }
  368. if (in == NULL)
  369. goto end;
  370. }
  371. out = bio_open_default(outfile, 'w', FORMAT_BINARY);
  372. if (out == NULL)
  373. goto end;
  374. if (sigfile != NULL) {
  375. BIO *sigbio = BIO_new_file(sigfile, "rb");
  376. if (sigbio == NULL) {
  377. BIO_printf(bio_err, "Can't open signature file %s\n", sigfile);
  378. goto end;
  379. }
  380. siglen = bio_to_mem(&sig, keysize * 10, sigbio);
  381. BIO_free(sigbio);
  382. if (siglen < 0) {
  383. BIO_printf(bio_err, "Error reading signature data\n");
  384. goto end;
  385. }
  386. }
  387. /* Raw input data is handled elsewhere */
  388. if (in != NULL && !rawin) {
  389. /* Read the input data */
  390. buf_inlen = bio_to_mem(&buf_in, -1, in);
  391. if (buf_inlen < 0) {
  392. BIO_printf(bio_err, "Error reading input Data\n");
  393. goto end;
  394. }
  395. if (rev) {
  396. size_t i;
  397. unsigned char ctmp;
  398. size_t l = (size_t)buf_inlen;
  399. for (i = 0; i < l / 2; i++) {
  400. ctmp = buf_in[i];
  401. buf_in[i] = buf_in[l - 1 - i];
  402. buf_in[l - 1 - i] = ctmp;
  403. }
  404. }
  405. }
  406. /* Sanity check the input if the input is not raw */
  407. if (!rawin
  408. && buf_inlen > EVP_MAX_MD_SIZE
  409. && (pkey_op == EVP_PKEY_OP_SIGN
  410. || pkey_op == EVP_PKEY_OP_VERIFY)) {
  411. BIO_printf(bio_err,
  412. "Error: The input data looks too long to be a hash\n");
  413. goto end;
  414. }
  415. if (pkey_op == EVP_PKEY_OP_VERIFY) {
  416. if (rawin) {
  417. rv = do_raw_keyop(pkey_op, mctx, pkey, in, filesize, sig, siglen,
  418. NULL, 0);
  419. } else {
  420. rv = EVP_PKEY_verify(ctx, sig, (size_t)siglen,
  421. buf_in, (size_t)buf_inlen);
  422. }
  423. if (rv == 1) {
  424. BIO_puts(out, "Signature Verified Successfully\n");
  425. ret = 0;
  426. } else {
  427. BIO_puts(out, "Signature Verification Failure\n");
  428. }
  429. goto end;
  430. }
  431. if (rawin) {
  432. /* rawin allocates the buffer in do_raw_keyop() */
  433. rv = do_raw_keyop(pkey_op, mctx, pkey, in, filesize, NULL, 0,
  434. &buf_out, (size_t *)&buf_outlen);
  435. } else {
  436. if (kdflen != 0) {
  437. buf_outlen = kdflen;
  438. rv = 1;
  439. } else {
  440. rv = do_keyop(ctx, pkey_op, NULL, (size_t *)&buf_outlen,
  441. buf_in, (size_t)buf_inlen);
  442. }
  443. if (rv > 0 && buf_outlen != 0) {
  444. buf_out = app_malloc(buf_outlen, "buffer output");
  445. rv = do_keyop(ctx, pkey_op,
  446. buf_out, (size_t *)&buf_outlen,
  447. buf_in, (size_t)buf_inlen);
  448. }
  449. }
  450. if (rv <= 0) {
  451. if (pkey_op != EVP_PKEY_OP_DERIVE) {
  452. BIO_puts(bio_err, "Public Key operation error\n");
  453. } else {
  454. BIO_puts(bio_err, "Key derivation failed\n");
  455. }
  456. goto end;
  457. }
  458. ret = 0;
  459. if (asn1parse) {
  460. if (!ASN1_parse_dump(out, buf_out, buf_outlen, 1, -1))
  461. ERR_print_errors(bio_err); /* but still return success */
  462. } else if (hexdump) {
  463. BIO_dump(out, (char *)buf_out, buf_outlen);
  464. } else {
  465. BIO_write(out, buf_out, buf_outlen);
  466. }
  467. end:
  468. if (ret != 0)
  469. ERR_print_errors(bio_err);
  470. EVP_MD_CTX_free(mctx);
  471. EVP_PKEY_CTX_free(ctx);
  472. EVP_MD_free(md);
  473. release_engine(e);
  474. BIO_free(in);
  475. BIO_free_all(out);
  476. OPENSSL_free(buf_in);
  477. OPENSSL_free(buf_out);
  478. OPENSSL_free(sig);
  479. sk_OPENSSL_STRING_free(pkeyopts);
  480. sk_OPENSSL_STRING_free(pkeyopts_passin);
  481. NCONF_free(conf);
  482. return ret;
  483. }
  484. static EVP_PKEY_CTX *init_ctx(const char *kdfalg, int *pkeysize,
  485. const char *keyfile, int keyform, int key_type,
  486. char *passinarg, int pkey_op, ENGINE *e,
  487. const int engine_impl, int rawin,
  488. EVP_PKEY **ppkey, EVP_MD_CTX *mctx, const char *digestname,
  489. OSSL_LIB_CTX *libctx, const char *propq)
  490. {
  491. EVP_PKEY *pkey = NULL;
  492. EVP_PKEY_CTX *ctx = NULL;
  493. ENGINE *impl = NULL;
  494. char *passin = NULL;
  495. int rv = -1;
  496. X509 *x;
  497. if (((pkey_op == EVP_PKEY_OP_SIGN) || (pkey_op == EVP_PKEY_OP_DECRYPT)
  498. || (pkey_op == EVP_PKEY_OP_DERIVE))
  499. && (key_type != KEY_PRIVKEY && kdfalg == NULL)) {
  500. BIO_printf(bio_err, "A private key is needed for this operation\n");
  501. goto end;
  502. }
  503. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  504. BIO_printf(bio_err, "Error getting password\n");
  505. goto end;
  506. }
  507. switch (key_type) {
  508. case KEY_PRIVKEY:
  509. pkey = load_key(keyfile, keyform, 0, passin, e, "private key");
  510. break;
  511. case KEY_PUBKEY:
  512. pkey = load_pubkey(keyfile, keyform, 0, NULL, e, "public key");
  513. break;
  514. case KEY_CERT:
  515. x = load_cert(keyfile, keyform, "Certificate");
  516. if (x) {
  517. pkey = X509_get_pubkey(x);
  518. X509_free(x);
  519. }
  520. break;
  521. case KEY_NONE:
  522. break;
  523. }
  524. #ifndef OPENSSL_NO_ENGINE
  525. if (engine_impl)
  526. impl = e;
  527. #endif
  528. if (kdfalg != NULL) {
  529. int kdfnid = OBJ_sn2nid(kdfalg);
  530. if (kdfnid == NID_undef) {
  531. kdfnid = OBJ_ln2nid(kdfalg);
  532. if (kdfnid == NID_undef) {
  533. BIO_printf(bio_err, "The given KDF \"%s\" is unknown.\n",
  534. kdfalg);
  535. goto end;
  536. }
  537. }
  538. if (impl != NULL)
  539. ctx = EVP_PKEY_CTX_new_id(kdfnid, impl);
  540. else
  541. ctx = EVP_PKEY_CTX_new_from_name(libctx, kdfalg, propq);
  542. } else {
  543. if (pkey == NULL)
  544. goto end;
  545. *pkeysize = EVP_PKEY_get_size(pkey);
  546. if (impl != NULL)
  547. ctx = EVP_PKEY_CTX_new(pkey, impl);
  548. else
  549. ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, propq);
  550. if (ppkey != NULL)
  551. *ppkey = pkey;
  552. EVP_PKEY_free(pkey);
  553. }
  554. if (ctx == NULL)
  555. goto end;
  556. if (rawin) {
  557. EVP_MD_CTX_set_pkey_ctx(mctx, ctx);
  558. switch (pkey_op) {
  559. case EVP_PKEY_OP_SIGN:
  560. rv = EVP_DigestSignInit_ex(mctx, NULL, digestname, libctx, propq,
  561. pkey, NULL);
  562. break;
  563. case EVP_PKEY_OP_VERIFY:
  564. rv = EVP_DigestVerifyInit_ex(mctx, NULL, digestname, libctx, propq,
  565. pkey, NULL);
  566. break;
  567. }
  568. } else {
  569. switch (pkey_op) {
  570. case EVP_PKEY_OP_SIGN:
  571. rv = EVP_PKEY_sign_init(ctx);
  572. break;
  573. case EVP_PKEY_OP_VERIFY:
  574. rv = EVP_PKEY_verify_init(ctx);
  575. break;
  576. case EVP_PKEY_OP_VERIFYRECOVER:
  577. rv = EVP_PKEY_verify_recover_init(ctx);
  578. break;
  579. case EVP_PKEY_OP_ENCRYPT:
  580. rv = EVP_PKEY_encrypt_init(ctx);
  581. break;
  582. case EVP_PKEY_OP_DECRYPT:
  583. rv = EVP_PKEY_decrypt_init(ctx);
  584. break;
  585. case EVP_PKEY_OP_DERIVE:
  586. rv = EVP_PKEY_derive_init(ctx);
  587. break;
  588. }
  589. }
  590. if (rv <= 0) {
  591. EVP_PKEY_CTX_free(ctx);
  592. ctx = NULL;
  593. }
  594. end:
  595. OPENSSL_free(passin);
  596. return ctx;
  597. }
  598. static int setup_peer(EVP_PKEY_CTX *ctx, int peerform, const char *file,
  599. ENGINE *e)
  600. {
  601. EVP_PKEY *peer = NULL;
  602. ENGINE *engine = NULL;
  603. int ret;
  604. if (peerform == FORMAT_ENGINE)
  605. engine = e;
  606. peer = load_pubkey(file, peerform, 0, NULL, engine, "peer key");
  607. if (peer == NULL) {
  608. BIO_printf(bio_err, "Error reading peer key %s\n", file);
  609. return 0;
  610. }
  611. ret = EVP_PKEY_derive_set_peer(ctx, peer) > 0;
  612. EVP_PKEY_free(peer);
  613. return ret;
  614. }
  615. static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
  616. unsigned char *out, size_t *poutlen,
  617. const unsigned char *in, size_t inlen)
  618. {
  619. int rv = 0;
  620. switch (pkey_op) {
  621. case EVP_PKEY_OP_VERIFYRECOVER:
  622. rv = EVP_PKEY_verify_recover(ctx, out, poutlen, in, inlen);
  623. break;
  624. case EVP_PKEY_OP_SIGN:
  625. rv = EVP_PKEY_sign(ctx, out, poutlen, in, inlen);
  626. break;
  627. case EVP_PKEY_OP_ENCRYPT:
  628. rv = EVP_PKEY_encrypt(ctx, out, poutlen, in, inlen);
  629. break;
  630. case EVP_PKEY_OP_DECRYPT:
  631. rv = EVP_PKEY_decrypt(ctx, out, poutlen, in, inlen);
  632. break;
  633. case EVP_PKEY_OP_DERIVE:
  634. rv = EVP_PKEY_derive(ctx, out, poutlen);
  635. break;
  636. }
  637. return rv;
  638. }
  639. #define TBUF_MAXSIZE 2048
  640. static int do_raw_keyop(int pkey_op, EVP_MD_CTX *mctx,
  641. EVP_PKEY *pkey, BIO *in,
  642. int filesize, unsigned char *sig, int siglen,
  643. unsigned char **out, size_t *poutlen)
  644. {
  645. int rv = 0;
  646. unsigned char tbuf[TBUF_MAXSIZE];
  647. unsigned char *mbuf = NULL;
  648. int buf_len = 0;
  649. /* Some algorithms only support oneshot digests */
  650. if (EVP_PKEY_get_id(pkey) == EVP_PKEY_ED25519
  651. || EVP_PKEY_get_id(pkey) == EVP_PKEY_ED448) {
  652. if (filesize < 0) {
  653. BIO_printf(bio_err,
  654. "Error: unable to determine file size for oneshot operation\n");
  655. goto end;
  656. }
  657. mbuf = app_malloc(filesize, "oneshot sign/verify buffer");
  658. switch (pkey_op) {
  659. case EVP_PKEY_OP_VERIFY:
  660. buf_len = BIO_read(in, mbuf, filesize);
  661. if (buf_len != filesize) {
  662. BIO_printf(bio_err, "Error reading raw input data\n");
  663. goto end;
  664. }
  665. rv = EVP_DigestVerify(mctx, sig, (size_t)siglen, mbuf, buf_len);
  666. break;
  667. case EVP_PKEY_OP_SIGN:
  668. buf_len = BIO_read(in, mbuf, filesize);
  669. if (buf_len != filesize) {
  670. BIO_printf(bio_err, "Error reading raw input data\n");
  671. goto end;
  672. }
  673. rv = EVP_DigestSign(mctx, NULL, poutlen, mbuf, buf_len);
  674. if (rv == 1 && out != NULL) {
  675. *out = app_malloc(*poutlen, "buffer output");
  676. rv = EVP_DigestSign(mctx, *out, poutlen, mbuf, buf_len);
  677. }
  678. break;
  679. }
  680. goto end;
  681. }
  682. switch (pkey_op) {
  683. case EVP_PKEY_OP_VERIFY:
  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. for (;;) {
  702. buf_len = BIO_read(in, tbuf, TBUF_MAXSIZE);
  703. if (buf_len == 0)
  704. break;
  705. if (buf_len < 0) {
  706. BIO_printf(bio_err, "Error reading raw input data\n");
  707. goto end;
  708. }
  709. rv = EVP_DigestSignUpdate(mctx, tbuf, (size_t)buf_len);
  710. if (rv != 1) {
  711. BIO_printf(bio_err, "Error signing raw input data\n");
  712. goto end;
  713. }
  714. }
  715. rv = EVP_DigestSignFinal(mctx, NULL, poutlen);
  716. if (rv == 1 && out != NULL) {
  717. *out = app_malloc(*poutlen, "buffer output");
  718. rv = EVP_DigestSignFinal(mctx, *out, poutlen);
  719. }
  720. break;
  721. }
  722. end:
  723. OPENSSL_free(mbuf);
  724. return rv;
  725. }