pkeyutl.c 30 KB

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