m_sigver.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright 2006-2016 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 "internal/evp_int.h"
  15. #include "evp_locl.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. return 1;
  72. }
  73. int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
  74. const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
  75. {
  76. return do_sigver_init(ctx, pctx, type, e, pkey, 0);
  77. }
  78. int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
  79. const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey)
  80. {
  81. return do_sigver_init(ctx, pctx, type, e, pkey, 1);
  82. }
  83. int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret,
  84. size_t *siglen)
  85. {
  86. int sctx = 0, r = 0;
  87. EVP_PKEY_CTX *pctx = ctx->pctx;
  88. if (pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) {
  89. if (!sigret)
  90. return pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
  91. if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE)
  92. r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx);
  93. else {
  94. EVP_PKEY_CTX *dctx = EVP_PKEY_CTX_dup(ctx->pctx);
  95. if (!dctx)
  96. return 0;
  97. r = dctx->pmeth->signctx(dctx, sigret, siglen, ctx);
  98. EVP_PKEY_CTX_free(dctx);
  99. }
  100. return r;
  101. }
  102. if (pctx->pmeth->signctx)
  103. sctx = 1;
  104. else
  105. sctx = 0;
  106. if (sigret) {
  107. unsigned char md[EVP_MAX_MD_SIZE];
  108. unsigned int mdlen = 0;
  109. if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
  110. if (sctx)
  111. r = ctx->pctx->pmeth->signctx(ctx->pctx, sigret, siglen, ctx);
  112. else
  113. r = EVP_DigestFinal_ex(ctx, md, &mdlen);
  114. } else {
  115. EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
  116. if (tmp_ctx == NULL)
  117. return 0;
  118. if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
  119. EVP_MD_CTX_free(tmp_ctx);
  120. return 0;
  121. }
  122. if (sctx)
  123. r = tmp_ctx->pctx->pmeth->signctx(tmp_ctx->pctx,
  124. sigret, siglen, tmp_ctx);
  125. else
  126. r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
  127. EVP_MD_CTX_free(tmp_ctx);
  128. }
  129. if (sctx || !r)
  130. return r;
  131. if (EVP_PKEY_sign(ctx->pctx, sigret, siglen, md, mdlen) <= 0)
  132. return 0;
  133. } else {
  134. if (sctx) {
  135. if (pctx->pmeth->signctx(pctx, sigret, siglen, ctx) <= 0)
  136. return 0;
  137. } else {
  138. int s = EVP_MD_size(ctx->digest);
  139. if (s < 0 || EVP_PKEY_sign(pctx, sigret, siglen, NULL, s) <= 0)
  140. return 0;
  141. }
  142. }
  143. return 1;
  144. }
  145. int EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen,
  146. const unsigned char *tbs, size_t tbslen)
  147. {
  148. if (ctx->pctx->pmeth->digestsign != NULL)
  149. return ctx->pctx->pmeth->digestsign(ctx, sigret, siglen, tbs, tbslen);
  150. if (sigret != NULL && EVP_DigestSignUpdate(ctx, tbs, tbslen) <= 0)
  151. return 0;
  152. return EVP_DigestSignFinal(ctx, sigret, siglen);
  153. }
  154. int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig,
  155. size_t siglen)
  156. {
  157. unsigned char md[EVP_MAX_MD_SIZE];
  158. int r = 0;
  159. unsigned int mdlen = 0;
  160. int vctx = 0;
  161. if (ctx->pctx->pmeth->verifyctx)
  162. vctx = 1;
  163. else
  164. vctx = 0;
  165. if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) {
  166. if (vctx) {
  167. r = ctx->pctx->pmeth->verifyctx(ctx->pctx, sig, siglen, ctx);
  168. } else
  169. r = EVP_DigestFinal_ex(ctx, md, &mdlen);
  170. } else {
  171. EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new();
  172. if (tmp_ctx == NULL)
  173. return -1;
  174. if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) {
  175. EVP_MD_CTX_free(tmp_ctx);
  176. return -1;
  177. }
  178. if (vctx) {
  179. r = tmp_ctx->pctx->pmeth->verifyctx(tmp_ctx->pctx,
  180. sig, siglen, tmp_ctx);
  181. } else
  182. r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen);
  183. EVP_MD_CTX_free(tmp_ctx);
  184. }
  185. if (vctx || !r)
  186. return r;
  187. return EVP_PKEY_verify(ctx->pctx, sig, siglen, md, mdlen);
  188. }
  189. int EVP_DigestVerify(EVP_MD_CTX *ctx, const unsigned char *sigret,
  190. size_t siglen, const unsigned char *tbs, size_t tbslen)
  191. {
  192. if (ctx->pctx->pmeth->digestverify != NULL)
  193. return ctx->pctx->pmeth->digestverify(ctx, sigret, siglen, tbs, tbslen);
  194. if (EVP_DigestVerifyUpdate(ctx, tbs, tbslen) <= 0)
  195. return -1;
  196. return EVP_DigestVerifyFinal(ctx, sigret, siglen);
  197. }