m_sigver.c 24 KB

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