bio_md.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright 1995-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 <errno.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/buffer.h>
  13. #include <openssl/evp.h>
  14. #include "internal/evp_int.h"
  15. #include "evp_locl.h"
  16. #include "internal/bio.h"
  17. /*
  18. * BIO_put and BIO_get both add to the digest, BIO_gets returns the digest
  19. */
  20. static int md_write(BIO *h, char const *buf, int num);
  21. static int md_read(BIO *h, char *buf, int size);
  22. static int md_gets(BIO *h, char *str, int size);
  23. static long md_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  24. static int md_new(BIO *h);
  25. static int md_free(BIO *data);
  26. static long md_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
  27. static const BIO_METHOD methods_md = {
  28. BIO_TYPE_MD,
  29. "message digest",
  30. /* TODO: Convert to new style write function */
  31. bwrite_conv,
  32. md_write,
  33. /* TODO: Convert to new style read function */
  34. bread_conv,
  35. md_read,
  36. NULL, /* md_puts, */
  37. md_gets,
  38. md_ctrl,
  39. md_new,
  40. md_free,
  41. md_callback_ctrl,
  42. };
  43. const BIO_METHOD *BIO_f_md(void)
  44. {
  45. return &methods_md;
  46. }
  47. static int md_new(BIO *bi)
  48. {
  49. EVP_MD_CTX *ctx;
  50. ctx = EVP_MD_CTX_new();
  51. if (ctx == NULL)
  52. return 0;
  53. BIO_set_init(bi, 1);
  54. BIO_set_data(bi, ctx);
  55. return 1;
  56. }
  57. static int md_free(BIO *a)
  58. {
  59. if (a == NULL)
  60. return 0;
  61. EVP_MD_CTX_free(BIO_get_data(a));
  62. BIO_set_data(a, NULL);
  63. BIO_set_init(a, 0);
  64. return 1;
  65. }
  66. static int md_read(BIO *b, char *out, int outl)
  67. {
  68. int ret = 0;
  69. EVP_MD_CTX *ctx;
  70. BIO *next;
  71. if (out == NULL)
  72. return 0;
  73. ctx = BIO_get_data(b);
  74. next = BIO_next(b);
  75. if ((ctx == NULL) || (next == NULL))
  76. return 0;
  77. ret = BIO_read(next, out, outl);
  78. if (BIO_get_init(b)) {
  79. if (ret > 0) {
  80. if (EVP_DigestUpdate(ctx, (unsigned char *)out,
  81. (unsigned int)ret) <= 0)
  82. return -1;
  83. }
  84. }
  85. BIO_clear_retry_flags(b);
  86. BIO_copy_next_retry(b);
  87. return ret;
  88. }
  89. static int md_write(BIO *b, const char *in, int inl)
  90. {
  91. int ret = 0;
  92. EVP_MD_CTX *ctx;
  93. BIO *next;
  94. if ((in == NULL) || (inl <= 0))
  95. return 0;
  96. ctx = BIO_get_data(b);
  97. next = BIO_next(b);
  98. if ((ctx != NULL) && (next != NULL))
  99. ret = BIO_write(next, in, inl);
  100. if (BIO_get_init(b)) {
  101. if (ret > 0) {
  102. if (!EVP_DigestUpdate(ctx, (const unsigned char *)in,
  103. (unsigned int)ret)) {
  104. BIO_clear_retry_flags(b);
  105. return 0;
  106. }
  107. }
  108. }
  109. if (next != NULL) {
  110. BIO_clear_retry_flags(b);
  111. BIO_copy_next_retry(b);
  112. }
  113. return ret;
  114. }
  115. static long md_ctrl(BIO *b, int cmd, long num, void *ptr)
  116. {
  117. EVP_MD_CTX *ctx, *dctx, **pctx;
  118. const EVP_MD **ppmd;
  119. EVP_MD *md;
  120. long ret = 1;
  121. BIO *dbio, *next;
  122. ctx = BIO_get_data(b);
  123. next = BIO_next(b);
  124. switch (cmd) {
  125. case BIO_CTRL_RESET:
  126. if (BIO_get_init(b))
  127. ret = EVP_DigestInit_ex(ctx, ctx->digest, NULL);
  128. else
  129. ret = 0;
  130. if (ret > 0)
  131. ret = BIO_ctrl(next, cmd, num, ptr);
  132. break;
  133. case BIO_C_GET_MD:
  134. if (BIO_get_init(b)) {
  135. ppmd = ptr;
  136. *ppmd = ctx->digest;
  137. } else
  138. ret = 0;
  139. break;
  140. case BIO_C_GET_MD_CTX:
  141. pctx = ptr;
  142. *pctx = ctx;
  143. BIO_set_init(b, 1);
  144. break;
  145. case BIO_C_SET_MD_CTX:
  146. if (BIO_get_init(b))
  147. BIO_set_data(b, ptr);
  148. else
  149. ret = 0;
  150. break;
  151. case BIO_C_DO_STATE_MACHINE:
  152. BIO_clear_retry_flags(b);
  153. ret = BIO_ctrl(next, cmd, num, ptr);
  154. BIO_copy_next_retry(b);
  155. break;
  156. case BIO_C_SET_MD:
  157. md = ptr;
  158. ret = EVP_DigestInit_ex(ctx, md, NULL);
  159. if (ret > 0)
  160. BIO_set_init(b, 1);
  161. break;
  162. case BIO_CTRL_DUP:
  163. dbio = ptr;
  164. dctx = BIO_get_data(dbio);
  165. if (!EVP_MD_CTX_copy_ex(dctx, ctx))
  166. return 0;
  167. BIO_set_init(b, 1);
  168. break;
  169. default:
  170. ret = BIO_ctrl(next, cmd, num, ptr);
  171. break;
  172. }
  173. return ret;
  174. }
  175. static long md_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  176. {
  177. long ret = 1;
  178. BIO *next;
  179. next = BIO_next(b);
  180. if (next == NULL)
  181. return 0;
  182. switch (cmd) {
  183. default:
  184. ret = BIO_callback_ctrl(next, cmd, fp);
  185. break;
  186. }
  187. return ret;
  188. }
  189. static int md_gets(BIO *bp, char *buf, int size)
  190. {
  191. EVP_MD_CTX *ctx;
  192. unsigned int ret;
  193. ctx = BIO_get_data(bp);
  194. if (size < ctx->digest->md_size)
  195. return 0;
  196. if (EVP_DigestFinal_ex(ctx, (unsigned char *)buf, &ret) <= 0)
  197. return -1;
  198. return (int)ret;
  199. }