bio_meth.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright 2016-2024 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 "bio_local.h"
  10. #include "internal/thread_once.h"
  11. CRYPTO_REF_COUNT bio_type_count;
  12. static CRYPTO_ONCE bio_type_init = CRYPTO_ONCE_STATIC_INIT;
  13. DEFINE_RUN_ONCE_STATIC(do_bio_type_init)
  14. {
  15. return CRYPTO_NEW_REF(&bio_type_count, BIO_TYPE_START);
  16. }
  17. int BIO_get_new_index(void)
  18. {
  19. int newval;
  20. if (!RUN_ONCE(&bio_type_init, do_bio_type_init)) {
  21. /* Perhaps the error should be raised in do_bio_type_init()? */
  22. ERR_raise(ERR_LIB_BIO, ERR_R_CRYPTO_LIB);
  23. return -1;
  24. }
  25. if (!CRYPTO_UP_REF(&bio_type_count, &newval))
  26. return -1;
  27. if (newval > BIO_TYPE_MASK)
  28. return -1;
  29. return newval;
  30. }
  31. BIO_METHOD *BIO_meth_new(int type, const char *name)
  32. {
  33. BIO_METHOD *biom = OPENSSL_zalloc(sizeof(BIO_METHOD));
  34. if (biom == NULL
  35. || (biom->name = OPENSSL_strdup(name)) == NULL) {
  36. OPENSSL_free(biom);
  37. return NULL;
  38. }
  39. biom->type = type;
  40. return biom;
  41. }
  42. void BIO_meth_free(BIO_METHOD *biom)
  43. {
  44. if (biom != NULL) {
  45. OPENSSL_free(biom->name);
  46. OPENSSL_free(biom);
  47. }
  48. }
  49. #ifndef OPENSSL_NO_DEPRECATED_3_5
  50. int (*BIO_meth_get_write(const BIO_METHOD *biom)) (BIO *, const char *, int)
  51. {
  52. return biom->bwrite_old;
  53. }
  54. int (*BIO_meth_get_write_ex(const BIO_METHOD *biom)) (BIO *, const char *, size_t,
  55. size_t *)
  56. {
  57. return biom->bwrite;
  58. }
  59. #endif
  60. /* Conversion for old style bwrite to new style */
  61. int bwrite_conv(BIO *bio, const char *data, size_t datal, size_t *written)
  62. {
  63. int ret;
  64. if (datal > INT_MAX)
  65. datal = INT_MAX;
  66. ret = bio->method->bwrite_old(bio, data, (int)datal);
  67. if (ret <= 0) {
  68. *written = 0;
  69. return ret;
  70. }
  71. *written = (size_t)ret;
  72. return 1;
  73. }
  74. int BIO_meth_set_write(BIO_METHOD *biom,
  75. int (*bwrite) (BIO *, const char *, int))
  76. {
  77. biom->bwrite_old = bwrite;
  78. biom->bwrite = bwrite_conv;
  79. return 1;
  80. }
  81. int BIO_meth_set_write_ex(BIO_METHOD *biom,
  82. int (*bwrite) (BIO *, const char *, size_t, size_t *))
  83. {
  84. biom->bwrite_old = NULL;
  85. biom->bwrite = bwrite;
  86. return 1;
  87. }
  88. #ifndef OPENSSL_NO_DEPRECATED_3_5
  89. int (*BIO_meth_get_read(const BIO_METHOD *biom)) (BIO *, char *, int)
  90. {
  91. return biom->bread_old;
  92. }
  93. int (*BIO_meth_get_read_ex(const BIO_METHOD *biom)) (BIO *, char *, size_t, size_t *)
  94. {
  95. return biom->bread;
  96. }
  97. #endif
  98. /* Conversion for old style bread to new style */
  99. int bread_conv(BIO *bio, char *data, size_t datal, size_t *readbytes)
  100. {
  101. int ret;
  102. if (datal > INT_MAX)
  103. datal = INT_MAX;
  104. ret = bio->method->bread_old(bio, data, (int)datal);
  105. if (ret <= 0) {
  106. *readbytes = 0;
  107. return ret;
  108. }
  109. *readbytes = (size_t)ret;
  110. return 1;
  111. }
  112. int BIO_meth_set_read(BIO_METHOD *biom,
  113. int (*bread) (BIO *, char *, int))
  114. {
  115. biom->bread_old = bread;
  116. biom->bread = bread_conv;
  117. return 1;
  118. }
  119. int BIO_meth_set_read_ex(BIO_METHOD *biom,
  120. int (*bread) (BIO *, char *, size_t, size_t *))
  121. {
  122. biom->bread_old = NULL;
  123. biom->bread = bread;
  124. return 1;
  125. }
  126. #ifndef OPENSSL_NO_DEPRECATED_3_5
  127. int (*BIO_meth_get_puts(const BIO_METHOD *biom)) (BIO *, const char *)
  128. {
  129. return biom->bputs;
  130. }
  131. #endif
  132. int BIO_meth_set_puts(BIO_METHOD *biom,
  133. int (*bputs) (BIO *, const char *))
  134. {
  135. biom->bputs = bputs;
  136. return 1;
  137. }
  138. #ifndef OPENSSL_NO_DEPRECATED_3_5
  139. int (*BIO_meth_get_gets(const BIO_METHOD *biom)) (BIO *, char *, int)
  140. {
  141. return biom->bgets;
  142. }
  143. #endif
  144. int BIO_meth_set_gets(BIO_METHOD *biom,
  145. int (*bgets) (BIO *, char *, int))
  146. {
  147. biom->bgets = bgets;
  148. return 1;
  149. }
  150. #ifndef OPENSSL_NO_DEPRECATED_3_5
  151. long (*BIO_meth_get_ctrl(const BIO_METHOD *biom)) (BIO *, int, long, void *)
  152. {
  153. return biom->ctrl;
  154. }
  155. #endif
  156. int BIO_meth_set_ctrl(BIO_METHOD *biom,
  157. long (*ctrl) (BIO *, int, long, void *))
  158. {
  159. biom->ctrl = ctrl;
  160. return 1;
  161. }
  162. #ifndef OPENSSL_NO_DEPRECATED_3_5
  163. int (*BIO_meth_get_create(const BIO_METHOD *biom)) (BIO *)
  164. {
  165. return biom->create;
  166. }
  167. #endif
  168. int BIO_meth_set_create(BIO_METHOD *biom, int (*create) (BIO *))
  169. {
  170. biom->create = create;
  171. return 1;
  172. }
  173. #ifndef OPENSSL_NO_DEPRECATED_3_5
  174. int (*BIO_meth_get_destroy(const BIO_METHOD *biom)) (BIO *)
  175. {
  176. return biom->destroy;
  177. }
  178. #endif
  179. int BIO_meth_set_destroy(BIO_METHOD *biom, int (*destroy) (BIO *))
  180. {
  181. biom->destroy = destroy;
  182. return 1;
  183. }
  184. #ifndef OPENSSL_NO_DEPRECATED_3_5
  185. long (*BIO_meth_get_callback_ctrl(const BIO_METHOD *biom)) (BIO *, int, BIO_info_cb *)
  186. {
  187. return biom->callback_ctrl;
  188. }
  189. #endif
  190. int BIO_meth_set_callback_ctrl(BIO_METHOD *biom,
  191. long (*callback_ctrl) (BIO *, int,
  192. BIO_info_cb *))
  193. {
  194. biom->callback_ctrl = callback_ctrl;
  195. return 1;
  196. }
  197. int BIO_meth_set_sendmmsg(BIO_METHOD *biom,
  198. int (*bsendmmsg) (BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *))
  199. {
  200. biom->bsendmmsg = bsendmmsg;
  201. return 1;
  202. }
  203. #ifndef OPENSSL_NO_DEPRECATED_3_5
  204. int (*BIO_meth_get_sendmmsg(const BIO_METHOD *biom))(BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *) {
  205. return biom->bsendmmsg;
  206. }
  207. #endif
  208. int BIO_meth_set_recvmmsg(BIO_METHOD *biom,
  209. int (*brecvmmsg) (BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *))
  210. {
  211. biom->brecvmmsg = brecvmmsg;
  212. return 1;
  213. }
  214. #ifndef OPENSSL_NO_DEPRECATED_3_5
  215. int (*BIO_meth_get_recvmmsg(const BIO_METHOD *biom))(BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *) {
  216. return biom->brecvmmsg;
  217. }
  218. #endif