m_sigver.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 "evp_local.h"
  16. static int update(EVP_MD_CTX *ctx, const void *data, size_t datalen)
  17. {
  18. EVPerr(EVP_F_UPDATE, EVP_R_ONLY_ONESHOT_SUPPORTED);
  19. return 0;
  20. }
  21. static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
  22. const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey,
  23. int ver)
  24. {
  25. if (ctx->pctx == NULL)
  26. ctx->pctx = EVP_PKEY_CTX_new(pkey, e);
  27. if (ctx->pctx == NULL)
  28. return 0;
  29. if (!(ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)) {
  30. if (type == NULL) {
  31. int def_nid;
  32. if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) > 0)
  33. type = EVP_get_digestbynid(def_nid);
  34. }
  35. if (type == NULL) {
  36. EVPerr(EVP_F_DO_SIGVER_INIT, EVP_R_NO_DEFAULT_DIGEST);
  37. return 0;
  38. }
  39. }
  40. if (ver) {
  41. if (ctx->pctx->pmeth->verifyctx_init) {
  42. if (ctx->pctx->pmeth->verifyctx_init(ctx->pctx, ctx) <= 0)
  43. return 0;
  44. ctx->pctx->operation = EVP_PKEY_OP_VERIFYCTX;
  45. } else if (ctx->pctx->pmeth->digestverify != 0) {
  46. ctx->pctx->operation = EVP_PKEY_OP_VERIFY;
  47. ctx->update = update;
  48. } else if (EVP_PKEY_verify_init(ctx->pctx) <= 0) {
  49. return 0;
  50. }
  51. } else {
  52. if (ctx->pctx->pmeth->signctx_init) {
  53. if (ctx->pctx->pmeth->signctx_init(ctx->pctx, ctx) <= 0)
  54. return 0;
  55. ctx->pctx->operation = EVP_PKEY_OP_SIGNCTX;
  56. } else if (ctx->pctx->pmeth->digestsign != 0) {
  57. ctx->pctx->operation = EVP_PKEY_OP_SIGN;
  58. ctx->update = update;
  59. } else if (EVP_PKEY_sign_init(ctx->pctx) <= 0) {
  60. return 0;
  61. }
  62. }
  63. if (EVP_PKEY_CTX_set_signature_md(ctx->pctx, type) <= 0)
  64. return 0;
  65. if (pctx)
  66. *pctx = ctx->pctx;
  67. if (ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)
  68. return 1;
  69. if (!EVP_DigestInit_ex(ctx, type, e))
  70. return 0;
  71. /*
  72. * This indicates the current algorithm requires
  73. * special treatment before hashing the tbs-message.
  74. */
  75. if (ctx->pctx->pmeth->digest_custom != NULL)
  76. return ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx);
  77. return 1;
  78. }
  79. int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
  80. const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
  81. {
  82. return do_sigver_init(ctx, pctx, type, e, pkey, 0);
  83. }
  84. int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
  85. const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
  86. {
  87. return do_sigver_init(ctx, pctx, type, e, pkey, 1);
  88. }
  89. int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
  90. size_t *siglen)
  91. {
  92. int sctx = 0, r = 0;
  93. EVP_PKEY_CTX *pctx = ctx->pctx;
  94. if (pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) {
  95. if (!sigret)
  96. return pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
  97. if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE)
  98. r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
  99. else {
  100. EVP_PKEY_CTX *dctx = EVP_PKEY_CTX_dup(ctx->pctx);
  101. if (!dctx)
  102. return 0;
  103. r = dctx->pmeth->signctx(dctx, sigret, siglen, ctx);
  104. EVP_PKEY_CTX_free(dctx);
  105. }
  106. return r;
  107. }
  108. if (pctx->pmeth->signctx)
  109. sctx = 1;
  110. else
  111. sctx = 0;
  112. if (sigret) {
  113. unsigned char md[EVP_MAX_MD_SIZE];
  114. unsigned int mdlen = 0;
  115. if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
  116. if (sctx)
  117. r = ctx->pctx->pmeth->signctx(ctx->pctx, sigret, siglen, ctx);
  118. else
  119. r = EVP_DigestFinal_ex(ctx, md, &mdlen);
  120. } else {
  121. EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
  122. if (tmp_ctx == NULL)
  123. return 0;
  124. if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
  125. EVP_MD_CTX_free(tmp_ctx);
  126. return 0;
  127. }
  128. if (sctx)
  129. r = tmp_ctx->pctx->pmeth->signctx(tmp_ctx->pctx,
  130. sigret, siglen, tmp_ctx);
  131. else
  132. r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
  133. EVP_MD_CTX_free(tmp_ctx);
  134. }
  135. if (sctx || !r)
  136. return r;
  137. if (EVP_PKEY_sign(ctx->pctx, sigret, siglen, md, mdlen) <= 0)
  138. return 0;
  139. } else {
  140. if (sctx) {
  141. if (pctx->pmeth->signctx(pctx, sigret, siglen, ctx) <= 0)
  142. return 0;
  143. } else {
  144. int s = EVP_MD_size(ctx->digest);
  145. if (s < 0 || EVP_PKEY_sign(pctx, sigret, siglen, NULL, s) <= 0)
  146. return 0;
  147. }
  148. }
  149. return 1;
  150. }
  151. int EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen,
  152. const unsigned char *tbs, size_t tbslen)
  153. {
  154. if (ctx->pctx->pmeth->digestsign != NULL)
  155. return ctx->pctx->pmeth->digestsign(ctx, sigret, siglen, tbs, tbslen);
  156. if (sigret != NULL && EVP_DigestSignUpdate(ctx, tbs, tbslen) <= 0)
  157. return 0;
  158. return EVP_DigestSignFinal(ctx, sigret, siglen);
  159. }
  160. int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig,
  161. size_t siglen)
  162. {
  163. unsigned char md[EVP_MAX_MD_SIZE];
  164. int r = 0;
  165. unsigned int mdlen = 0;
  166. int vctx = 0;
  167. if (ctx->pctx->pmeth->verifyctx)
  168. vctx = 1;
  169. else
  170. vctx = 0;
  171. if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
  172. if (vctx)
  173. r = ctx->pctx->pmeth->verifyctx(ctx->pctx, sig, siglen, ctx);
  174. else
  175. r = EVP_DigestFinal_ex(ctx, md, &mdlen);
  176. } else {
  177. EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
  178. if (tmp_ctx == NULL)
  179. return -1;
  180. if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
  181. EVP_MD_CTX_free(tmp_ctx);
  182. return -1;
  183. }
  184. if (vctx)
  185. r = tmp_ctx->pctx->pmeth->verifyctx(tmp_ctx->pctx,
  186. sig, siglen, tmp_ctx);
  187. else
  188. r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
  189. EVP_MD_CTX_free(tmp_ctx);
  190. }
  191. if (vctx || !r)
  192. return r;
  193. return EVP_PKEY_verify(ctx->pctx, sig, siglen, md, mdlen);
  194. }
  195. int EVP_DigestVerify(EVP_MD_CTX *ctx, const unsigned char *sigret,
  196. size_t siglen, const unsigned char *tbs, size_t tbslen)
  197. {
  198. if (ctx->pctx->pmeth->digestverify != NULL)
  199. return ctx->pctx->pmeth->digestverify(ctx, sigret, siglen, tbs, tbslen);
  200. if (EVP_DigestVerifyUpdate(ctx, tbs, tbslen) <= 0)
  201. return -1;
  202. return EVP_DigestVerifyFinal(ctx, sigret, siglen);
  203. }