pkeyutl.c 25 KB

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