bio_md.c 4.8 KB

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