m_sigver.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /*
  2. * Copyright 2006-2022 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 <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/evp.h>
  12. #include <openssl/objects.h>
  13. #include "crypto/evp.h"
  14. #include "internal/provider.h"
  15. #include "internal/numbers.h" /* includes SIZE_MAX */
  16. #include "evp_local.h"
  17. #ifndef FIPS_MODULE
  18. static int update(EVP_MD_CTX *ctx, const void *data, size_t datalen)
  19. {
  20. ERR_raise(ERR_LIB_EVP, EVP_R_ONLY_ONESHOT_SUPPORTED);
  21. return 0;
  22. }
  23. /*
  24. * If we get the "NULL" md then the name comes back as "UNDEF". We want to use
  25. * NULL for this.
  26. */
  27. static const char *canon_mdname(const char *mdname)
  28. {
  29. if (mdname != NULL && strcmp(mdname, "UNDEF") == 0)
  30. return NULL;
  31. return mdname;
  32. }
  33. static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
  34. const EVP_MD *type, const char *mdname,
  35. OSSL_LIB_CTX *libctx, const char *props,
  36. ENGINE *e, EVP_PKEY *pkey, int ver,
  37. const OSSL_PARAM params[])
  38. {
  39. EVP_PKEY_CTX *locpctx = NULL;
  40. EVP_SIGNATURE *signature = NULL;
  41. EVP_KEYMGMT *tmp_keymgmt = NULL;
  42. const OSSL_PROVIDER *tmp_prov = NULL;
  43. const char *supported_sig = NULL;
  44. char locmdname[80] = ""; /* 80 chars should be enough */
  45. void *provkey = NULL;
  46. int ret, iter, reinit = 1;
  47. if (!evp_md_ctx_free_algctx(ctx))
  48. return 0;
  49. if (ctx->pctx == NULL) {
  50. reinit = 0;
  51. if (e == NULL)
  52. ctx->pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, props);
  53. else
  54. ctx->pctx = EVP_PKEY_CTX_new(pkey, e);
  55. }
  56. if (ctx->pctx == NULL)
  57. return 0;
  58. locpctx = ctx->pctx;
  59. ERR_set_mark();
  60. if (evp_pkey_ctx_is_legacy(locpctx))
  61. goto legacy;
  62. /* do not reinitialize if pkey is set or operation is different */
  63. if (reinit
  64. && (pkey != NULL
  65. || locpctx->operation != (ver ? EVP_PKEY_OP_VERIFYCTX
  66. : EVP_PKEY_OP_SIGNCTX)
  67. || (signature = locpctx->op.sig.signature) == NULL
  68. || locpctx->op.sig.algctx == NULL))
  69. reinit = 0;
  70. if (props == NULL)
  71. props = locpctx->propquery;
  72. if (locpctx->pkey == NULL) {
  73. ERR_clear_last_mark();
  74. ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET);
  75. goto err;
  76. }
  77. if (!reinit) {
  78. evp_pkey_ctx_free_old_ops(locpctx);
  79. } else {
  80. if (mdname == NULL && type == NULL)
  81. mdname = canon_mdname(EVP_MD_get0_name(ctx->reqdigest));
  82. goto reinitialize;
  83. }
  84. /*
  85. * Try to derive the supported signature from |locpctx->keymgmt|.
  86. */
  87. if (!ossl_assert(locpctx->pkey->keymgmt == NULL
  88. || locpctx->pkey->keymgmt == locpctx->keymgmt)) {
  89. ERR_clear_last_mark();
  90. ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
  91. goto err;
  92. }
  93. supported_sig = evp_keymgmt_util_query_operation_name(locpctx->keymgmt,
  94. OSSL_OP_SIGNATURE);
  95. if (supported_sig == NULL) {
  96. ERR_clear_last_mark();
  97. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  98. goto err;
  99. }
  100. /*
  101. * We perform two iterations:
  102. *
  103. * 1. Do the normal signature fetch, using the fetching data given by
  104. * the EVP_PKEY_CTX.
  105. * 2. Do the provider specific signature fetch, from the same provider
  106. * as |ctx->keymgmt|
  107. *
  108. * We then try to fetch the keymgmt from the same provider as the
  109. * signature, and try to export |ctx->pkey| to that keymgmt (when
  110. * this keymgmt happens to be the same as |ctx->keymgmt|, the export
  111. * is a no-op, but we call it anyway to not complicate the code even
  112. * more).
  113. * If the export call succeeds (returns a non-NULL provider key pointer),
  114. * we're done and can perform the operation itself. If not, we perform
  115. * the second iteration, or jump to legacy.
  116. */
  117. for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) {
  118. EVP_KEYMGMT *tmp_keymgmt_tofree = NULL;
  119. /*
  120. * If we're on the second iteration, free the results from the first.
  121. * They are NULL on the first iteration, so no need to check what
  122. * iteration we're on.
  123. */
  124. EVP_SIGNATURE_free(signature);
  125. EVP_KEYMGMT_free(tmp_keymgmt);
  126. switch (iter) {
  127. case 1:
  128. signature = EVP_SIGNATURE_fetch(locpctx->libctx, supported_sig,
  129. locpctx->propquery);
  130. if (signature != NULL)
  131. tmp_prov = EVP_SIGNATURE_get0_provider(signature);
  132. break;
  133. case 2:
  134. tmp_prov = EVP_KEYMGMT_get0_provider(locpctx->keymgmt);
  135. signature =
  136. evp_signature_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
  137. supported_sig, locpctx->propquery);
  138. if (signature == NULL)
  139. goto legacy;
  140. break;
  141. }
  142. if (signature == NULL)
  143. continue;
  144. /*
  145. * Ensure that the key is provided, either natively, or as a cached
  146. * export. We start by fetching the keymgmt with the same name as
  147. * |locpctx->pkey|, but from the provider of the signature method, using
  148. * the same property query as when fetching the signature method.
  149. * With the keymgmt we found (if we did), we try to export |locpctx->pkey|
  150. * to it (evp_pkey_export_to_provider() is smart enough to only actually
  151. * export it if |tmp_keymgmt| is different from |locpctx->pkey|'s keymgmt)
  152. */
  153. tmp_keymgmt_tofree = tmp_keymgmt =
  154. evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov,
  155. EVP_KEYMGMT_get0_name(locpctx->keymgmt),
  156. locpctx->propquery);
  157. if (tmp_keymgmt != NULL)
  158. provkey = evp_pkey_export_to_provider(locpctx->pkey, locpctx->libctx,
  159. &tmp_keymgmt, locpctx->propquery);
  160. if (tmp_keymgmt == NULL)
  161. EVP_KEYMGMT_free(tmp_keymgmt_tofree);
  162. }
  163. if (provkey == NULL) {
  164. EVP_SIGNATURE_free(signature);
  165. ERR_clear_last_mark();
  166. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  167. goto err;
  168. }
  169. ERR_pop_to_mark();
  170. /* No more legacy from here down to legacy: */
  171. locpctx->op.sig.signature = signature;
  172. locpctx->operation = ver ? EVP_PKEY_OP_VERIFYCTX
  173. : EVP_PKEY_OP_SIGNCTX;
  174. locpctx->op.sig.algctx
  175. = signature->newctx(ossl_provider_ctx(signature->prov), props);
  176. if (locpctx->op.sig.algctx == NULL) {
  177. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  178. goto err;
  179. }
  180. reinitialize:
  181. if (pctx != NULL)
  182. *pctx = locpctx;
  183. if (type != NULL) {
  184. ctx->reqdigest = type;
  185. if (mdname == NULL)
  186. mdname = canon_mdname(EVP_MD_get0_name(type));
  187. } else {
  188. if (mdname == NULL && !reinit) {
  189. if (evp_keymgmt_util_get_deflt_digest_name(tmp_keymgmt, provkey,
  190. locmdname,
  191. sizeof(locmdname)) > 0) {
  192. mdname = canon_mdname(locmdname);
  193. }
  194. }
  195. if (mdname != NULL) {
  196. /*
  197. * We're about to get a new digest so clear anything associated with
  198. * an old digest.
  199. */
  200. evp_md_ctx_clear_digest(ctx, 1, 0);
  201. /* legacy code support for engines */
  202. ERR_set_mark();
  203. /*
  204. * This might be requested by a later call to EVP_MD_CTX_get0_md().
  205. * In that case the "explicit fetch" rules apply for that
  206. * function (as per man pages), i.e. the ref count is not updated
  207. * so the EVP_MD should not be used beyond the lifetime of the
  208. * EVP_MD_CTX.
  209. */
  210. ctx->fetched_digest = EVP_MD_fetch(locpctx->libctx, mdname, props);
  211. if (ctx->fetched_digest != NULL) {
  212. ctx->digest = ctx->reqdigest = ctx->fetched_digest;
  213. } else {
  214. /* legacy engine support : remove the mark when this is deleted */
  215. ctx->reqdigest = ctx->digest = EVP_get_digestbyname(mdname);
  216. if (ctx->digest == NULL) {
  217. (void)ERR_clear_last_mark();
  218. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  219. goto err;
  220. }
  221. }
  222. (void)ERR_pop_to_mark();
  223. }
  224. }
  225. if (ver) {
  226. if (signature->digest_verify_init == NULL) {
  227. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  228. goto err;
  229. }
  230. ret = signature->digest_verify_init(locpctx->op.sig.algctx,
  231. mdname, provkey, params);
  232. } else {
  233. if (signature->digest_sign_init == NULL) {
  234. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  235. goto err;
  236. }
  237. ret = signature->digest_sign_init(locpctx->op.sig.algctx,
  238. mdname, provkey, params);
  239. }
  240. /*
  241. * If the operation was not a success and no digest was found, an error
  242. * needs to be raised.
  243. */
  244. if (ret > 0 || mdname != NULL)
  245. goto end;
  246. if (type == NULL) /* This check is redundant but clarifies matters */
  247. ERR_raise(ERR_LIB_EVP, EVP_R_NO_DEFAULT_DIGEST);
  248. err:
  249. evp_pkey_ctx_free_old_ops(locpctx);
  250. locpctx->operation = EVP_PKEY_OP_UNDEFINED;
  251. EVP_KEYMGMT_free(tmp_keymgmt);
  252. return 0;
  253. legacy:
  254. /*
  255. * If we don't have the full support we need with provided methods,
  256. * let's go see if legacy does.
  257. */
  258. ERR_pop_to_mark();
  259. EVP_KEYMGMT_free(tmp_keymgmt);
  260. tmp_keymgmt = NULL;
  261. if (type == NULL && mdname != NULL)
  262. type = evp_get_digestbyname_ex(locpctx->libctx, mdname);
  263. if (ctx->pctx->pmeth == NULL) {
  264. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  265. return 0;
  266. }
  267. if (!(ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)) {
  268. if (type == NULL) {
  269. int def_nid;
  270. if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) > 0)
  271. type = EVP_get_digestbynid(def_nid);
  272. }
  273. if (type == NULL) {
  274. ERR_raise(ERR_LIB_EVP, EVP_R_NO_DEFAULT_DIGEST);
  275. return 0;
  276. }
  277. }
  278. if (ver) {
  279. if (ctx->pctx->pmeth->verifyctx_init) {
  280. if (ctx->pctx->pmeth->verifyctx_init(ctx->pctx, ctx) <= 0)
  281. return 0;
  282. ctx->pctx->operation = EVP_PKEY_OP_VERIFYCTX;
  283. } else if (ctx->pctx->pmeth->digestverify != 0) {
  284. ctx->pctx->operation = EVP_PKEY_OP_VERIFY;
  285. ctx->update = update;
  286. } else if (EVP_PKEY_verify_init(ctx->pctx) <= 0) {
  287. return 0;
  288. }
  289. } else {
  290. if (ctx->pctx->pmeth->signctx_init) {
  291. if (ctx->pctx->pmeth->signctx_init(ctx->pctx, ctx) <= 0)
  292. return 0;
  293. ctx->pctx->operation = EVP_PKEY_OP_SIGNCTX;
  294. } else if (ctx->pctx->pmeth->digestsign != 0) {
  295. ctx->pctx->operation = EVP_PKEY_OP_SIGN;
  296. ctx->update = update;
  297. } else if (EVP_PKEY_sign_init(ctx->pctx) <= 0) {
  298. return 0;
  299. }
  300. }
  301. if (EVP_PKEY_CTX_set_signature_md(ctx->pctx, type) <= 0)
  302. return 0;
  303. if (pctx)
  304. *pctx = ctx->pctx;
  305. if (ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)
  306. return 1;
  307. if (!EVP_DigestInit_ex(ctx, type, e))
  308. return 0;
  309. /*
  310. * This indicates the current algorithm requires
  311. * special treatment before hashing the tbs-message.
  312. */
  313. ctx->pctx->flag_call_digest_custom = 0;
  314. if (ctx->pctx->pmeth->digest_custom != NULL)
  315. ctx->pctx->flag_call_digest_custom = 1;
  316. ret = 1;
  317. end:
  318. #ifndef FIPS_MODULE
  319. if (ret > 0)
  320. ret = evp_pkey_ctx_use_cached_data(locpctx);
  321. #endif
  322. EVP_KEYMGMT_free(tmp_keymgmt);
  323. return ret > 0 ? 1 : 0;
  324. }
  325. int EVP_DigestSignInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
  326. const char *mdname, OSSL_LIB_CTX *libctx,
  327. const char *props, EVP_PKEY *pkey,
  328. const OSSL_PARAM params[])
  329. {
  330. return do_sigver_init(ctx, pctx, NULL, mdname, libctx, props, NULL, pkey, 0,
  331. params);
  332. }
  333. int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
  334. const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
  335. {
  336. return do_sigver_init(ctx, pctx, type, NULL, NULL, NULL, e, pkey, 0,
  337. NULL);
  338. }
  339. int EVP_DigestVerifyInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
  340. const char *mdname, OSSL_LIB_CTX *libctx,
  341. const char *props, EVP_PKEY *pkey,
  342. const OSSL_PARAM params[])
  343. {
  344. return do_sigver_init(ctx, pctx, NULL, mdname, libctx, props, NULL, pkey, 1,
  345. params);
  346. }
  347. int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
  348. const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
  349. {
  350. return do_sigver_init(ctx, pctx, type, NULL, NULL, NULL, e, pkey, 1,
  351. NULL);
  352. }
  353. #endif /* FIPS_MDOE */
  354. int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
  355. {
  356. EVP_PKEY_CTX *pctx = ctx->pctx;
  357. if (pctx == NULL
  358. || pctx->operation != EVP_PKEY_OP_SIGNCTX
  359. || pctx->op.sig.algctx == NULL
  360. || pctx->op.sig.signature == NULL)
  361. goto legacy;
  362. if (pctx->op.sig.signature->digest_sign_update == NULL) {
  363. ERR_raise(ERR_LIB_EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  364. return 0;
  365. }
  366. return pctx->op.sig.signature->digest_sign_update(pctx->op.sig.algctx,
  367. data, dsize);
  368. legacy:
  369. if (pctx != NULL) {
  370. /* do_sigver_init() checked that |digest_custom| is non-NULL */
  371. if (pctx->flag_call_digest_custom
  372. && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
  373. return 0;
  374. pctx->flag_call_digest_custom = 0;
  375. }
  376. return EVP_DigestUpdate(ctx, data, dsize);
  377. }
  378. int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
  379. {
  380. EVP_PKEY_CTX *pctx = ctx->pctx;
  381. if (pctx == NULL
  382. || pctx->operation != EVP_PKEY_OP_VERIFYCTX
  383. || pctx->op.sig.algctx == NULL
  384. || pctx->op.sig.signature == NULL)
  385. goto legacy;
  386. if (pctx->op.sig.signature->digest_verify_update == NULL) {
  387. ERR_raise(ERR_LIB_EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  388. return 0;
  389. }
  390. return pctx->op.sig.signature->digest_verify_update(pctx->op.sig.algctx,
  391. data, dsize);
  392. legacy:
  393. if (pctx != NULL) {
  394. /* do_sigver_init() checked that |digest_custom| is non-NULL */
  395. if (pctx->flag_call_digest_custom
  396. && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
  397. return 0;
  398. pctx->flag_call_digest_custom = 0;
  399. }
  400. return EVP_DigestUpdate(ctx, data, dsize);
  401. }
  402. #ifndef FIPS_MODULE
  403. int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
  404. size_t *siglen)
  405. {
  406. int sctx = 0, r = 0;
  407. EVP_PKEY_CTX *dctx, *pctx = ctx->pctx;
  408. if (pctx == NULL
  409. || pctx->operation != EVP_PKEY_OP_SIGNCTX
  410. || pctx->op.sig.algctx == NULL
  411. || pctx->op.sig.signature == NULL)
  412. goto legacy;
  413. if (sigret == NULL || (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) != 0)
  414. return pctx->op.sig.signature->digest_sign_final(pctx->op.sig.algctx,
  415. sigret, siglen,
  416. sigret == NULL ? 0 : *siglen);
  417. dctx = EVP_PKEY_CTX_dup(pctx);
  418. if (dctx == NULL)
  419. return 0;
  420. r = dctx->op.sig.signature->digest_sign_final(dctx->op.sig.algctx,
  421. sigret, siglen,
  422. *siglen);
  423. EVP_PKEY_CTX_free(dctx);
  424. return r;
  425. legacy:
  426. if (pctx == NULL || pctx->pmeth == NULL) {
  427. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  428. return 0;
  429. }
  430. /* do_sigver_init() checked that |digest_custom| is non-NULL */
  431. if (pctx->flag_call_digest_custom
  432. && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
  433. return 0;
  434. pctx->flag_call_digest_custom = 0;
  435. if (pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) {
  436. if (sigret == NULL)
  437. return pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
  438. if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE)
  439. r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
  440. else {
  441. dctx = EVP_PKEY_CTX_dup(pctx);
  442. if (dctx == NULL)
  443. return 0;
  444. r = dctx->pmeth->signctx(dctx, sigret, siglen, ctx);
  445. EVP_PKEY_CTX_free(dctx);
  446. }
  447. return r;
  448. }
  449. if (pctx->pmeth->signctx != NULL)
  450. sctx = 1;
  451. else
  452. sctx = 0;
  453. if (sigret != NULL) {
  454. unsigned char md[EVP_MAX_MD_SIZE];
  455. unsigned int mdlen = 0;
  456. if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
  457. if (sctx)
  458. r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
  459. else
  460. r = EVP_DigestFinal_ex(ctx, md, &mdlen);
  461. } else {
  462. EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
  463. if (tmp_ctx == NULL)
  464. return 0;
  465. if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
  466. EVP_MD_CTX_free(tmp_ctx);
  467. return 0;
  468. }
  469. if (sctx)
  470. r = tmp_ctx->pctx->pmeth->signctx(tmp_ctx->pctx,
  471. sigret, siglen, tmp_ctx);
  472. else
  473. r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
  474. EVP_MD_CTX_free(tmp_ctx);
  475. }
  476. if (sctx || !r)
  477. return r;
  478. if (EVP_PKEY_sign(pctx, sigret, siglen, md, mdlen) <= 0)
  479. return 0;
  480. } else {
  481. if (sctx) {
  482. if (pctx->pmeth->signctx(pctx, sigret, siglen, ctx) <= 0)
  483. return 0;
  484. } else {
  485. int s = EVP_MD_get_size(ctx->digest);
  486. if (s < 0 || EVP_PKEY_sign(pctx, sigret, siglen, NULL, s) <= 0)
  487. return 0;
  488. }
  489. }
  490. return 1;
  491. }
  492. int EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen,
  493. const unsigned char *tbs, size_t tbslen)
  494. {
  495. EVP_PKEY_CTX *pctx = ctx->pctx;
  496. if (pctx != NULL
  497. && pctx->operation == EVP_PKEY_OP_SIGNCTX
  498. && pctx->op.sig.algctx != NULL
  499. && pctx->op.sig.signature != NULL) {
  500. if (pctx->op.sig.signature->digest_sign != NULL)
  501. return pctx->op.sig.signature->digest_sign(pctx->op.sig.algctx,
  502. sigret, siglen,
  503. sigret == NULL ? 0 : *siglen,
  504. tbs, tbslen);
  505. } else {
  506. /* legacy */
  507. if (ctx->pctx->pmeth != NULL && ctx->pctx->pmeth->digestsign != NULL)
  508. return ctx->pctx->pmeth->digestsign(ctx, sigret, siglen, tbs, tbslen);
  509. }
  510. if (sigret != NULL && EVP_DigestSignUpdate(ctx, tbs, tbslen) <= 0)
  511. return 0;
  512. return EVP_DigestSignFinal(ctx, sigret, siglen);
  513. }
  514. int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig,
  515. size_t siglen)
  516. {
  517. unsigned char md[EVP_MAX_MD_SIZE];
  518. int r = 0;
  519. unsigned int mdlen = 0;
  520. int vctx = 0;
  521. EVP_PKEY_CTX *dctx, *pctx = ctx->pctx;
  522. if (pctx == NULL
  523. || pctx->operation != EVP_PKEY_OP_VERIFYCTX
  524. || pctx->op.sig.algctx == NULL
  525. || pctx->op.sig.signature == NULL)
  526. goto legacy;
  527. if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISE) != 0)
  528. return pctx->op.sig.signature->digest_verify_final(pctx->op.sig.algctx,
  529. sig, siglen);
  530. dctx = EVP_PKEY_CTX_dup(pctx);
  531. if (dctx == NULL)
  532. return 0;
  533. r = dctx->op.sig.signature->digest_verify_final(dctx->op.sig.algctx,
  534. sig, siglen);
  535. EVP_PKEY_CTX_free(dctx);
  536. return r;
  537. legacy:
  538. if (pctx == NULL || pctx->pmeth == NULL) {
  539. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  540. return 0;
  541. }
  542. /* do_sigver_init() checked that |digest_custom| is non-NULL */
  543. if (pctx->flag_call_digest_custom
  544. && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
  545. return 0;
  546. pctx->flag_call_digest_custom = 0;
  547. if (pctx->pmeth->verifyctx != NULL)
  548. vctx = 1;
  549. else
  550. vctx = 0;
  551. if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
  552. if (vctx)
  553. r = pctx->pmeth->verifyctx(pctx, sig, siglen, ctx);
  554. else
  555. r = EVP_DigestFinal_ex(ctx, md, &mdlen);
  556. } else {
  557. EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
  558. if (tmp_ctx == NULL)
  559. return -1;
  560. if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
  561. EVP_MD_CTX_free(tmp_ctx);
  562. return -1;
  563. }
  564. if (vctx)
  565. r = tmp_ctx->pctx->pmeth->verifyctx(tmp_ctx->pctx,
  566. sig, siglen, tmp_ctx);
  567. else
  568. r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
  569. EVP_MD_CTX_free(tmp_ctx);
  570. }
  571. if (vctx || !r)
  572. return r;
  573. return EVP_PKEY_verify(pctx, sig, siglen, md, mdlen);
  574. }
  575. int EVP_DigestVerify(EVP_MD_CTX *ctx, const unsigned char *sigret,
  576. size_t siglen, const unsigned char *tbs, size_t tbslen)
  577. {
  578. EVP_PKEY_CTX *pctx = ctx->pctx;
  579. if (pctx != NULL
  580. && pctx->operation == EVP_PKEY_OP_VERIFYCTX
  581. && pctx->op.sig.algctx != NULL
  582. && pctx->op.sig.signature != NULL) {
  583. if (pctx->op.sig.signature->digest_verify != NULL)
  584. return pctx->op.sig.signature->digest_verify(pctx->op.sig.algctx,
  585. sigret, siglen,
  586. tbs, tbslen);
  587. } else {
  588. /* legacy */
  589. if (ctx->pctx->pmeth != NULL && ctx->pctx->pmeth->digestverify != NULL)
  590. return ctx->pctx->pmeth->digestverify(ctx, sigret, siglen, tbs, tbslen);
  591. }
  592. if (EVP_DigestVerifyUpdate(ctx, tbs, tbslen) <= 0)
  593. return -1;
  594. return EVP_DigestVerifyFinal(ctx, sigret, siglen);
  595. }
  596. #endif /* FIPS_MODULE */