bio_meth.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright 2016-2018 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_RWLOCK *bio_type_lock = NULL;
  12. static CRYPTO_ONCE bio_type_init = CRYPTO_ONCE_STATIC_INIT;
  13. DEFINE_RUN_ONCE_STATIC(do_bio_type_init)
  14. {
  15. bio_type_lock = CRYPTO_THREAD_lock_new();
  16. return bio_type_lock != NULL;
  17. }
  18. int BIO_get_new_index(void)
  19. {
  20. static CRYPTO_REF_COUNT bio_count = BIO_TYPE_START;
  21. int newval;
  22. if (!RUN_ONCE(&bio_type_init, do_bio_type_init)) {
  23. BIOerr(BIO_F_BIO_GET_NEW_INDEX, ERR_R_MALLOC_FAILURE);
  24. return -1;
  25. }
  26. if (!CRYPTO_UP_REF(&bio_count, &newval, bio_type_lock))
  27. return -1;
  28. return newval;
  29. }
  30. BIO_METHOD *BIO_meth_new(int type, const char *name)
  31. {
  32. BIO_METHOD *biom = OPENSSL_zalloc(sizeof(BIO_METHOD));
  33. if (biom == NULL
  34. || (biom->name = OPENSSL_strdup(name)) == NULL) {
  35. OPENSSL_free(biom);
  36. BIOerr(BIO_F_BIO_METH_NEW, ERR_R_MALLOC_FAILURE);
  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. int (*BIO_meth_get_write(const BIO_METHOD *biom)) (BIO *, const char *, int)
  50. {
  51. return biom->bwrite_old;
  52. }
  53. int (*BIO_meth_get_write_ex(const BIO_METHOD *biom)) (BIO *, const char *, size_t,
  54. size_t *)
  55. {
  56. return biom->bwrite;
  57. }
  58. /* Conversion for old style bwrite to new style */
  59. int bwrite_conv(BIO *bio, const char *data, size_t datal, size_t *written)
  60. {
  61. int ret;
  62. if (datal > INT_MAX)
  63. datal = INT_MAX;
  64. ret = bio->method->bwrite_old(bio, data, (int)datal);
  65. if (ret <= 0) {
  66. *written = 0;
  67. return ret;
  68. }
  69. *written = (size_t)ret;
  70. return 1;
  71. }
  72. int BIO_meth_set_write(BIO_METHOD *biom,
  73. int (*bwrite) (BIO *, const char *, int))
  74. {
  75. biom->bwrite_old = bwrite;
  76. biom->bwrite = bwrite_conv;
  77. return 1;
  78. }
  79. int BIO_meth_set_write_ex(BIO_METHOD *biom,
  80. int (*bwrite) (BIO *, const char *, size_t, size_t *))
  81. {
  82. biom->bwrite_old = NULL;
  83. biom->bwrite = bwrite;
  84. return 1;
  85. }
  86. int (*BIO_meth_get_read(const BIO_METHOD *biom)) (BIO *, char *, int)
  87. {
  88. return biom->bread_old;
  89. }
  90. int (*BIO_meth_get_read_ex(const BIO_METHOD *biom)) (BIO *, char *, size_t, size_t *)
  91. {
  92. return biom->bread;
  93. }
  94. /* Conversion for old style bread to new style */
  95. int bread_conv(BIO *bio, char *data, size_t datal, size_t *readbytes)
  96. {
  97. int ret;
  98. if (datal > INT_MAX)
  99. datal = INT_MAX;
  100. ret = bio->method->bread_old(bio, data, (int)datal);
  101. if (ret <= 0) {
  102. *readbytes = 0;
  103. return ret;
  104. }
  105. *readbytes = (size_t)ret;
  106. return 1;
  107. }
  108. int BIO_meth_set_read(BIO_METHOD *biom,
  109. int (*bread) (BIO *, char *, int))
  110. {
  111. biom->bread_old = bread;
  112. biom->bread = bread_conv;
  113. return 1;
  114. }
  115. int BIO_meth_set_read_ex(BIO_METHOD *biom,
  116. int (*bread) (BIO *, char *, size_t, size_t *))
  117. {
  118. biom->bread_old = NULL;
  119. biom->bread = bread;
  120. return 1;
  121. }
  122. int (*BIO_meth_get_puts(const BIO_METHOD *biom)) (BIO *, const char *)
  123. {
  124. return biom->bputs;
  125. }
  126. int BIO_meth_set_puts(BIO_METHOD *biom,
  127. int (*bputs) (BIO *, const char *))
  128. {
  129. biom->bputs = bputs;
  130. return 1;
  131. }
  132. int (*BIO_meth_get_gets(const BIO_METHOD *biom)) (BIO *, char *, int)
  133. {
  134. return biom->bgets;
  135. }
  136. int BIO_meth_set_gets(BIO_METHOD *biom,
  137. int (*bgets) (BIO *, char *, int))
  138. {
  139. biom->bgets = bgets;
  140. return 1;
  141. }
  142. long (*BIO_meth_get_ctrl(const BIO_METHOD *biom)) (BIO *, int, long, void *)
  143. {
  144. return biom->ctrl;
  145. }
  146. int BIO_meth_set_ctrl(BIO_METHOD *biom,
  147. long (*ctrl) (BIO *, int, long, void *))
  148. {
  149. biom->ctrl = ctrl;
  150. return 1;
  151. }
  152. int (*BIO_meth_get_create(const BIO_METHOD *biom)) (BIO *)
  153. {
  154. return biom->create;
  155. }
  156. int BIO_meth_set_create(BIO_METHOD *biom, int (*create) (BIO *))
  157. {
  158. biom->create = create;
  159. return 1;
  160. }
  161. int (*BIO_meth_get_destroy(const BIO_METHOD *biom)) (BIO *)
  162. {
  163. return biom->destroy;
  164. }
  165. int BIO_meth_set_destroy(BIO_METHOD *biom, int (*destroy) (BIO *))
  166. {
  167. biom->destroy = destroy;
  168. return 1;
  169. }
  170. long (*BIO_meth_get_callback_ctrl(const BIO_METHOD *biom)) (BIO *, int, BIO_info_cb *)
  171. {
  172. return biom->callback_ctrl;
  173. }
  174. int BIO_meth_set_callback_ctrl(BIO_METHOD *biom,
  175. long (*callback_ctrl) (BIO *, int,
  176. BIO_info_cb *))
  177. {
  178. biom->callback_ctrl = callback_ctrl;
  179. return 1;
  180. }