m_sigver.c 19 KB

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