m_sigver.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*
  2. * Copyright 2006-2020 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 <openssl/x509.h>
  14. #include "crypto/evp.h"
  15. #include "internal/provider.h"
  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. EVPerr(EVP_F_UPDATE, 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. {
  38. EVP_PKEY_CTX *locpctx = NULL;
  39. EVP_SIGNATURE *signature = NULL;
  40. EVP_KEYMGMT *tmp_keymgmt = NULL;
  41. const char *supported_sig = NULL;
  42. char locmdname[80] = ""; /* 80 chars should be enough */
  43. void *provkey = NULL;
  44. int ret;
  45. if (ctx->provctx != NULL) {
  46. if (!ossl_assert(ctx->digest != NULL)) {
  47. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  48. return 0;
  49. }
  50. if (ctx->digest->freectx != NULL)
  51. ctx->digest->freectx(ctx->provctx);
  52. ctx->provctx = NULL;
  53. }
  54. if (ctx->pctx == NULL) {
  55. if (libctx != NULL)
  56. ctx->pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, props);
  57. else
  58. ctx->pctx = EVP_PKEY_CTX_new(pkey, e);
  59. }
  60. if (ctx->pctx == NULL)
  61. return 0;
  62. locpctx = ctx->pctx;
  63. evp_pkey_ctx_free_old_ops(locpctx);
  64. if (props == NULL)
  65. props = locpctx->propquery;
  66. /*
  67. * TODO when we stop falling back to legacy, this and the ERR_pop_to_mark()
  68. * calls can be removed.
  69. */
  70. ERR_set_mark();
  71. if (evp_pkey_ctx_is_legacy(locpctx))
  72. goto legacy;
  73. /*
  74. * Ensure that the key is provided, either natively, or as a cached export.
  75. */
  76. tmp_keymgmt = locpctx->keymgmt;
  77. provkey = evp_pkey_export_to_provider(locpctx->pkey, locpctx->libctx,
  78. &tmp_keymgmt, locpctx->propquery);
  79. if (provkey == NULL) {
  80. /*
  81. * If we couldn't find a keymgmt at all try legacy.
  82. * TODO(3.0): Once all legacy algorithms (SM2, HMAC etc) have provider
  83. * based implementations this fallback shouldn't be necessary. Either
  84. * we have an ENGINE based implementation (in which case we should have
  85. * already fallen back in the test above here), or we don't have the
  86. * provider based implementation loaded (in which case this is an
  87. * application config error)
  88. */
  89. if (locpctx->keymgmt == NULL)
  90. goto legacy;
  91. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  92. goto err;
  93. }
  94. if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) {
  95. ERR_clear_last_mark();
  96. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  97. goto err;
  98. }
  99. EVP_KEYMGMT_free(locpctx->keymgmt);
  100. locpctx->keymgmt = tmp_keymgmt;
  101. if (locpctx->keymgmt->query_operation_name != NULL)
  102. supported_sig =
  103. locpctx->keymgmt->query_operation_name(OSSL_OP_SIGNATURE);
  104. /*
  105. * If we didn't get a supported sig, assume there is one with the
  106. * same name as the key type.
  107. */
  108. if (supported_sig == NULL)
  109. supported_sig = locpctx->keytype;
  110. /*
  111. * Because we cleared out old ops, we shouldn't need to worry about
  112. * checking if signature is already there.
  113. */
  114. signature = EVP_SIGNATURE_fetch(locpctx->libctx, supported_sig,
  115. locpctx->propquery);
  116. if (signature == NULL
  117. || (EVP_KEYMGMT_provider(locpctx->keymgmt)
  118. != EVP_SIGNATURE_provider(signature))) {
  119. /*
  120. * We don't need to free ctx->keymgmt here, as it's not necessarily
  121. * tied to this operation. It will be freed by EVP_PKEY_CTX_free().
  122. */
  123. EVP_SIGNATURE_free(signature);
  124. goto legacy;
  125. }
  126. /*
  127. * TODO remove this when legacy is gone
  128. * If we don't have the full support we need with provided methods,
  129. * let's go see if legacy does.
  130. */
  131. ERR_pop_to_mark();
  132. /* No more legacy from here down to legacy: */
  133. if (pctx != NULL)
  134. *pctx = locpctx;
  135. locpctx->op.sig.signature = signature;
  136. locpctx->operation = ver ? EVP_PKEY_OP_VERIFYCTX
  137. : EVP_PKEY_OP_SIGNCTX;
  138. locpctx->op.sig.sigprovctx
  139. = signature->newctx(ossl_provider_ctx(signature->prov), props);
  140. if (locpctx->op.sig.sigprovctx == NULL) {
  141. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  142. goto err;
  143. }
  144. if (type != NULL) {
  145. ctx->reqdigest = type;
  146. if (mdname == NULL)
  147. mdname = canon_mdname(EVP_MD_name(type));
  148. } else {
  149. if (mdname == NULL) {
  150. if (evp_keymgmt_util_get_deflt_digest_name(tmp_keymgmt, provkey,
  151. locmdname,
  152. sizeof(locmdname)) > 0) {
  153. mdname = canon_mdname(locmdname);
  154. }
  155. }
  156. if (mdname != NULL) {
  157. /*
  158. * We're about to get a new digest so clear anything associated with
  159. * an old digest.
  160. */
  161. evp_md_ctx_clear_digest(ctx, 1);
  162. /* legacy code support for engines */
  163. ERR_set_mark();
  164. /*
  165. * This might be requested by a later call to EVP_MD_CTX_md().
  166. * In that case the "explicit fetch" rules apply for that
  167. * function (as per man pages), i.e. the ref count is not updated
  168. * so the EVP_MD should not be used beyound the lifetime of the
  169. * EVP_MD_CTX.
  170. */
  171. ctx->fetched_digest = EVP_MD_fetch(locpctx->libctx, mdname, props);
  172. if (ctx->fetched_digest != NULL) {
  173. ctx->digest = ctx->reqdigest = ctx->fetched_digest;
  174. } else {
  175. /* legacy engine support : remove the mark when this is deleted */
  176. ctx->reqdigest = ctx->digest = EVP_get_digestbyname(mdname);
  177. if (ctx->digest == NULL) {
  178. (void)ERR_clear_last_mark();
  179. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  180. goto err;
  181. }
  182. }
  183. (void)ERR_pop_to_mark();
  184. }
  185. }
  186. if (ver) {
  187. if (signature->digest_verify_init == NULL) {
  188. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  189. goto err;
  190. }
  191. ret = signature->digest_verify_init(locpctx->op.sig.sigprovctx,
  192. mdname, provkey);
  193. } else {
  194. if (signature->digest_sign_init == NULL) {
  195. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  196. goto err;
  197. }
  198. ret = signature->digest_sign_init(locpctx->op.sig.sigprovctx,
  199. mdname, provkey);
  200. }
  201. goto end;
  202. err:
  203. evp_pkey_ctx_free_old_ops(locpctx);
  204. locpctx->operation = EVP_PKEY_OP_UNDEFINED;
  205. return 0;
  206. legacy:
  207. /*
  208. * TODO remove this when legacy is gone
  209. * If we don't have the full support we need with provided methods,
  210. * let's go see if legacy does.
  211. */
  212. ERR_pop_to_mark();
  213. if (type == NULL && mdname != NULL)
  214. type = evp_get_digestbyname_ex(locpctx->libctx, mdname);
  215. if (ctx->pctx->pmeth == NULL) {
  216. EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  217. return 0;
  218. }
  219. if (!(ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)) {
  220. if (type == NULL) {
  221. int def_nid;
  222. if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) > 0)
  223. type = EVP_get_digestbynid(def_nid);
  224. }
  225. if (type == NULL) {
  226. EVPerr(EVP_F_DO_SIGVER_INIT, EVP_R_NO_DEFAULT_DIGEST);
  227. return 0;
  228. }
  229. }
  230. if (ver) {
  231. if (ctx->pctx->pmeth->verifyctx_init) {
  232. if (ctx->pctx->pmeth->verifyctx_init(ctx->pctx, ctx) <= 0)
  233. return 0;
  234. ctx->pctx->operation = EVP_PKEY_OP_VERIFYCTX;
  235. } else if (ctx->pctx->pmeth->digestverify != 0) {
  236. ctx->pctx->operation = EVP_PKEY_OP_VERIFY;
  237. ctx->update = update;
  238. } else if (EVP_PKEY_verify_init(ctx->pctx) <= 0) {
  239. return 0;
  240. }
  241. } else {
  242. if (ctx->pctx->pmeth->signctx_init) {
  243. if (ctx->pctx->pmeth->signctx_init(ctx->pctx, ctx) <= 0)
  244. return 0;
  245. ctx->pctx->operation = EVP_PKEY_OP_SIGNCTX;
  246. } else if (ctx->pctx->pmeth->digestsign != 0) {
  247. ctx->pctx->operation = EVP_PKEY_OP_SIGN;
  248. ctx->update = update;
  249. } else if (EVP_PKEY_sign_init(ctx->pctx) <= 0) {
  250. return 0;
  251. }
  252. }
  253. if (EVP_PKEY_CTX_set_signature_md(ctx->pctx, type) <= 0)
  254. return 0;
  255. if (pctx)
  256. *pctx = ctx->pctx;
  257. if (ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)
  258. return 1;
  259. if (!EVP_DigestInit_ex(ctx, type, e))
  260. return 0;
  261. /*
  262. * This indicates the current algorithm requires
  263. * special treatment before hashing the tbs-message.
  264. */
  265. ctx->pctx->flag_call_digest_custom = 0;
  266. if (ctx->pctx->pmeth->digest_custom != NULL)
  267. ctx->pctx->flag_call_digest_custom = 1;
  268. ret = 1;
  269. end:
  270. #ifndef FIPS_MODULE
  271. if (ret > 0)
  272. ret = evp_pkey_ctx_use_cached_data(locpctx);
  273. #endif
  274. return ret > 0 ? 1 : 0;
  275. }
  276. int EVP_DigestSignInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
  277. const char *mdname, OSSL_LIB_CTX *libctx,
  278. const char *props, EVP_PKEY *pkey)
  279. {
  280. return do_sigver_init(ctx, pctx, NULL, mdname, libctx, props, NULL, pkey, 0);
  281. }
  282. int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
  283. const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
  284. {
  285. return do_sigver_init(ctx, pctx, type, NULL, NULL, NULL, e, pkey, 0);
  286. }
  287. int EVP_DigestVerifyInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
  288. const char *mdname, OSSL_LIB_CTX *libctx,
  289. const char *props, EVP_PKEY *pkey)
  290. {
  291. return do_sigver_init(ctx, pctx, NULL, mdname, libctx, props, NULL, pkey, 1);
  292. }
  293. int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
  294. const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
  295. {
  296. return do_sigver_init(ctx, pctx, type, NULL, NULL, NULL, e, pkey, 1);
  297. }
  298. #endif /* FIPS_MDOE */
  299. int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
  300. {
  301. EVP_PKEY_CTX *pctx = ctx->pctx;
  302. if (pctx == NULL
  303. || pctx->operation != EVP_PKEY_OP_SIGNCTX
  304. || pctx->op.sig.sigprovctx == NULL
  305. || pctx->op.sig.signature == NULL)
  306. goto legacy;
  307. if (pctx->op.sig.signature->digest_sign_update == NULL) {
  308. ERR_raise(ERR_LIB_EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  309. return 0;
  310. }
  311. return pctx->op.sig.signature->digest_sign_update(pctx->op.sig.sigprovctx,
  312. data, dsize);
  313. legacy:
  314. if (pctx != NULL) {
  315. /* do_sigver_init() checked that |digest_custom| is non-NULL */
  316. if (pctx->flag_call_digest_custom
  317. && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
  318. return 0;
  319. pctx->flag_call_digest_custom = 0;
  320. }
  321. return EVP_DigestUpdate(ctx, data, dsize);
  322. }
  323. int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize)
  324. {
  325. EVP_PKEY_CTX *pctx = ctx->pctx;
  326. if (pctx == NULL
  327. || pctx->operation != EVP_PKEY_OP_VERIFYCTX
  328. || pctx->op.sig.sigprovctx == NULL
  329. || pctx->op.sig.signature == NULL)
  330. goto legacy;
  331. if (pctx->op.sig.signature->digest_verify_update == NULL) {
  332. ERR_raise(ERR_LIB_EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  333. return 0;
  334. }
  335. return pctx->op.sig.signature->digest_verify_update(pctx->op.sig.sigprovctx,
  336. data, dsize);
  337. legacy:
  338. if (pctx != NULL) {
  339. /* do_sigver_init() checked that |digest_custom| is non-NULL */
  340. if (pctx->flag_call_digest_custom
  341. && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
  342. return 0;
  343. pctx->flag_call_digest_custom = 0;
  344. }
  345. return EVP_DigestUpdate(ctx, data, dsize);
  346. }
  347. #ifndef FIPS_MODULE
  348. int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
  349. size_t *siglen)
  350. {
  351. int sctx = 0, r = 0;
  352. EVP_PKEY_CTX *pctx = ctx->pctx;
  353. if (pctx == NULL
  354. || pctx->operation != EVP_PKEY_OP_SIGNCTX
  355. || pctx->op.sig.sigprovctx == NULL
  356. || pctx->op.sig.signature == NULL)
  357. goto legacy;
  358. return pctx->op.sig.signature->digest_sign_final(pctx->op.sig.sigprovctx,
  359. sigret, siglen, SIZE_MAX);
  360. legacy:
  361. if (pctx == NULL || pctx->pmeth == NULL) {
  362. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  363. return 0;
  364. }
  365. /* do_sigver_init() checked that |digest_custom| is non-NULL */
  366. if (pctx->flag_call_digest_custom
  367. && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
  368. return 0;
  369. pctx->flag_call_digest_custom = 0;
  370. if (pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) {
  371. if (sigret == NULL)
  372. return pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
  373. if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE)
  374. r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
  375. else {
  376. EVP_PKEY_CTX *dctx = EVP_PKEY_CTX_dup(pctx);
  377. if (dctx == NULL)
  378. return 0;
  379. r = dctx->pmeth->signctx(dctx, sigret, siglen, ctx);
  380. EVP_PKEY_CTX_free(dctx);
  381. }
  382. return r;
  383. }
  384. if (pctx->pmeth->signctx != NULL)
  385. sctx = 1;
  386. else
  387. sctx = 0;
  388. if (sigret != NULL) {
  389. unsigned char md[EVP_MAX_MD_SIZE];
  390. unsigned int mdlen = 0;
  391. if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
  392. if (sctx)
  393. r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
  394. else
  395. r = EVP_DigestFinal_ex(ctx, md, &mdlen);
  396. } else {
  397. EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
  398. if (tmp_ctx == NULL)
  399. return 0;
  400. if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
  401. EVP_MD_CTX_free(tmp_ctx);
  402. return 0;
  403. }
  404. if (sctx)
  405. r = tmp_ctx->pctx->pmeth->signctx(tmp_ctx->pctx,
  406. sigret, siglen, tmp_ctx);
  407. else
  408. r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
  409. EVP_MD_CTX_free(tmp_ctx);
  410. }
  411. if (sctx || !r)
  412. return r;
  413. if (EVP_PKEY_sign(pctx, sigret, siglen, md, mdlen) <= 0)
  414. return 0;
  415. } else {
  416. if (sctx) {
  417. if (pctx->pmeth->signctx(pctx, sigret, siglen, ctx) <= 0)
  418. return 0;
  419. } else {
  420. int s = EVP_MD_size(ctx->digest);
  421. if (s < 0 || EVP_PKEY_sign(pctx, sigret, siglen, NULL, s) <= 0)
  422. return 0;
  423. }
  424. }
  425. return 1;
  426. }
  427. int EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen,
  428. const unsigned char *tbs, size_t tbslen)
  429. {
  430. EVP_PKEY_CTX *pctx = ctx->pctx;
  431. if (pctx != NULL
  432. && pctx->operation == EVP_PKEY_OP_SIGNCTX
  433. && pctx->op.sig.sigprovctx != NULL
  434. && pctx->op.sig.signature != NULL) {
  435. if (pctx->op.sig.signature->digest_sign != NULL)
  436. return pctx->op.sig.signature->digest_sign(pctx->op.sig.sigprovctx,
  437. sigret, siglen, SIZE_MAX,
  438. tbs, tbslen);
  439. } else {
  440. /* legacy */
  441. if (ctx->pctx->pmeth != NULL && ctx->pctx->pmeth->digestsign != NULL)
  442. return ctx->pctx->pmeth->digestsign(ctx, sigret, siglen, tbs, tbslen);
  443. }
  444. if (sigret != NULL && EVP_DigestSignUpdate(ctx, tbs, tbslen) <= 0)
  445. return 0;
  446. return EVP_DigestSignFinal(ctx, sigret, siglen);
  447. }
  448. int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig,
  449. size_t siglen)
  450. {
  451. unsigned char md[EVP_MAX_MD_SIZE];
  452. int r = 0;
  453. unsigned int mdlen = 0;
  454. int vctx = 0;
  455. EVP_PKEY_CTX *pctx = ctx->pctx;
  456. if (pctx == NULL
  457. || pctx->operation != EVP_PKEY_OP_VERIFYCTX
  458. || pctx->op.sig.sigprovctx == NULL
  459. || pctx->op.sig.signature == NULL)
  460. goto legacy;
  461. return pctx->op.sig.signature->digest_verify_final(pctx->op.sig.sigprovctx,
  462. sig, siglen);
  463. legacy:
  464. if (pctx == NULL || pctx->pmeth == NULL) {
  465. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  466. return 0;
  467. }
  468. /* do_sigver_init() checked that |digest_custom| is non-NULL */
  469. if (pctx->flag_call_digest_custom
  470. && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx))
  471. return 0;
  472. pctx->flag_call_digest_custom = 0;
  473. if (pctx->pmeth->verifyctx != NULL)
  474. vctx = 1;
  475. else
  476. vctx = 0;
  477. if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
  478. if (vctx)
  479. r = pctx->pmeth->verifyctx(pctx, sig, siglen, ctx);
  480. else
  481. r = EVP_DigestFinal_ex(ctx, md, &mdlen);
  482. } else {
  483. EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
  484. if (tmp_ctx == NULL)
  485. return -1;
  486. if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
  487. EVP_MD_CTX_free(tmp_ctx);
  488. return -1;
  489. }
  490. if (vctx)
  491. r = tmp_ctx->pctx->pmeth->verifyctx(tmp_ctx->pctx,
  492. sig, siglen, tmp_ctx);
  493. else
  494. r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
  495. EVP_MD_CTX_free(tmp_ctx);
  496. }
  497. if (vctx || !r)
  498. return r;
  499. return EVP_PKEY_verify(pctx, sig, siglen, md, mdlen);
  500. }
  501. int EVP_DigestVerify(EVP_MD_CTX *ctx, const unsigned char *sigret,
  502. size_t siglen, const unsigned char *tbs, size_t tbslen)
  503. {
  504. EVP_PKEY_CTX *pctx = ctx->pctx;
  505. if (pctx != NULL
  506. && pctx->operation == EVP_PKEY_OP_VERIFYCTX
  507. && pctx->op.sig.sigprovctx != NULL
  508. && pctx->op.sig.signature != NULL) {
  509. if (pctx->op.sig.signature->digest_verify != NULL)
  510. return pctx->op.sig.signature->digest_verify(pctx->op.sig.sigprovctx,
  511. sigret, siglen,
  512. tbs, tbslen);
  513. } else {
  514. /* legacy */
  515. if (ctx->pctx->pmeth != NULL && ctx->pctx->pmeth->digestverify != NULL)
  516. return ctx->pctx->pmeth->digestverify(ctx, sigret, siglen, tbs, tbslen);
  517. }
  518. if (EVP_DigestVerifyUpdate(ctx, tbs, tbslen) <= 0)
  519. return -1;
  520. return EVP_DigestVerifyFinal(ctx, sigret, siglen);
  521. }
  522. #endif /* FIPS_MODULE */