pkeyutl.c 24 KB

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