bio_prov.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright 2019-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 <assert.h>
  10. #include <openssl/core_dispatch.h>
  11. #include "internal/cryptlib.h"
  12. #include "prov/bio.h"
  13. static OSSL_FUNC_BIO_new_file_fn *c_bio_new_file = NULL;
  14. static OSSL_FUNC_BIO_new_membuf_fn *c_bio_new_membuf = NULL;
  15. static OSSL_FUNC_BIO_read_ex_fn *c_bio_read_ex = NULL;
  16. static OSSL_FUNC_BIO_write_ex_fn *c_bio_write_ex = NULL;
  17. static OSSL_FUNC_BIO_gets_fn *c_bio_gets = NULL;
  18. static OSSL_FUNC_BIO_puts_fn *c_bio_puts = NULL;
  19. static OSSL_FUNC_BIO_ctrl_fn *c_bio_ctrl = NULL;
  20. static OSSL_FUNC_BIO_up_ref_fn *c_bio_up_ref = NULL;
  21. static OSSL_FUNC_BIO_free_fn *c_bio_free = NULL;
  22. static OSSL_FUNC_BIO_vprintf_fn *c_bio_vprintf = NULL;
  23. int ossl_prov_bio_from_dispatch(const OSSL_DISPATCH *fns)
  24. {
  25. for (; fns->function_id != 0; fns++) {
  26. switch (fns->function_id) {
  27. case OSSL_FUNC_BIO_NEW_FILE:
  28. if (c_bio_new_file == NULL)
  29. c_bio_new_file = OSSL_FUNC_BIO_new_file(fns);
  30. break;
  31. case OSSL_FUNC_BIO_NEW_MEMBUF:
  32. if (c_bio_new_membuf == NULL)
  33. c_bio_new_membuf = OSSL_FUNC_BIO_new_membuf(fns);
  34. break;
  35. case OSSL_FUNC_BIO_READ_EX:
  36. if (c_bio_read_ex == NULL)
  37. c_bio_read_ex = OSSL_FUNC_BIO_read_ex(fns);
  38. break;
  39. case OSSL_FUNC_BIO_WRITE_EX:
  40. if (c_bio_write_ex == NULL)
  41. c_bio_write_ex = OSSL_FUNC_BIO_write_ex(fns);
  42. break;
  43. case OSSL_FUNC_BIO_GETS:
  44. if (c_bio_gets == NULL)
  45. c_bio_gets = OSSL_FUNC_BIO_gets(fns);
  46. break;
  47. case OSSL_FUNC_BIO_PUTS:
  48. if (c_bio_puts == NULL)
  49. c_bio_puts = OSSL_FUNC_BIO_puts(fns);
  50. break;
  51. case OSSL_FUNC_BIO_CTRL:
  52. if (c_bio_ctrl == NULL)
  53. c_bio_ctrl = OSSL_FUNC_BIO_ctrl(fns);
  54. break;
  55. case OSSL_FUNC_BIO_UP_REF:
  56. if (c_bio_up_ref == NULL)
  57. c_bio_up_ref = OSSL_FUNC_BIO_up_ref(fns);
  58. break;
  59. case OSSL_FUNC_BIO_FREE:
  60. if (c_bio_free == NULL)
  61. c_bio_free = OSSL_FUNC_BIO_free(fns);
  62. break;
  63. case OSSL_FUNC_BIO_VPRINTF:
  64. if (c_bio_vprintf == NULL)
  65. c_bio_vprintf = OSSL_FUNC_BIO_vprintf(fns);
  66. break;
  67. }
  68. }
  69. return 1;
  70. }
  71. OSSL_CORE_BIO *ossl_prov_bio_new_file(const char *filename, const char *mode)
  72. {
  73. if (c_bio_new_file == NULL)
  74. return NULL;
  75. return c_bio_new_file(filename, mode);
  76. }
  77. OSSL_CORE_BIO *ossl_prov_bio_new_membuf(const char *filename, int len)
  78. {
  79. if (c_bio_new_membuf == NULL)
  80. return NULL;
  81. return c_bio_new_membuf(filename, len);
  82. }
  83. int ossl_prov_bio_read_ex(OSSL_CORE_BIO *bio, void *data, size_t data_len,
  84. size_t *bytes_read)
  85. {
  86. if (c_bio_read_ex == NULL)
  87. return 0;
  88. return c_bio_read_ex(bio, data, data_len, bytes_read);
  89. }
  90. int ossl_prov_bio_write_ex(OSSL_CORE_BIO *bio, const void *data, size_t data_len,
  91. size_t *written)
  92. {
  93. if (c_bio_write_ex == NULL)
  94. return 0;
  95. return c_bio_write_ex(bio, data, data_len, written);
  96. }
  97. int ossl_prov_bio_gets(OSSL_CORE_BIO *bio, char *buf, int size)
  98. {
  99. if (c_bio_gets == NULL)
  100. return -1;
  101. return c_bio_gets(bio, buf, size);
  102. }
  103. int ossl_prov_bio_puts(OSSL_CORE_BIO *bio, const char *str)
  104. {
  105. if (c_bio_puts == NULL)
  106. return -1;
  107. return c_bio_puts(bio, str);
  108. }
  109. int ossl_prov_bio_ctrl(OSSL_CORE_BIO *bio, int cmd, long num, void *ptr)
  110. {
  111. if (c_bio_ctrl == NULL)
  112. return -1;
  113. return c_bio_ctrl(bio, cmd, num, ptr);
  114. }
  115. int ossl_prov_bio_up_ref(OSSL_CORE_BIO *bio)
  116. {
  117. if (c_bio_up_ref == NULL)
  118. return 0;
  119. return c_bio_up_ref(bio);
  120. }
  121. int ossl_prov_bio_free(OSSL_CORE_BIO *bio)
  122. {
  123. if (c_bio_free == NULL)
  124. return 0;
  125. return c_bio_free(bio);
  126. }
  127. int ossl_prov_bio_vprintf(OSSL_CORE_BIO *bio, const char *format, va_list ap)
  128. {
  129. if (c_bio_vprintf == NULL)
  130. return -1;
  131. return c_bio_vprintf(bio, format, ap);
  132. }
  133. int ossl_prov_bio_printf(OSSL_CORE_BIO *bio, const char *format, ...)
  134. {
  135. va_list ap;
  136. int ret;
  137. va_start(ap, format);
  138. ret = ossl_prov_bio_vprintf(bio, format, ap);
  139. va_end(ap);
  140. return ret;
  141. }
  142. #ifndef FIPS_MODULE
  143. /* No direct BIO support in the FIPS module */
  144. static int bio_core_read_ex(BIO *bio, char *data, size_t data_len,
  145. size_t *bytes_read)
  146. {
  147. return ossl_prov_bio_read_ex(BIO_get_data(bio), data, data_len, bytes_read);
  148. }
  149. static int bio_core_write_ex(BIO *bio, const char *data, size_t data_len,
  150. size_t *written)
  151. {
  152. return ossl_prov_bio_write_ex(BIO_get_data(bio), data, data_len, written);
  153. }
  154. static long bio_core_ctrl(BIO *bio, int cmd, long num, void *ptr)
  155. {
  156. return ossl_prov_bio_ctrl(BIO_get_data(bio), cmd, num, ptr);
  157. }
  158. static int bio_core_gets(BIO *bio, char *buf, int size)
  159. {
  160. return ossl_prov_bio_gets(BIO_get_data(bio), buf, size);
  161. }
  162. static int bio_core_puts(BIO *bio, const char *str)
  163. {
  164. return ossl_prov_bio_puts(BIO_get_data(bio), str);
  165. }
  166. static int bio_core_new(BIO *bio)
  167. {
  168. BIO_set_init(bio, 1);
  169. return 1;
  170. }
  171. static int bio_core_free(BIO *bio)
  172. {
  173. BIO_set_init(bio, 0);
  174. ossl_prov_bio_free(BIO_get_data(bio));
  175. return 1;
  176. }
  177. BIO_METHOD *ossl_bio_prov_init_bio_method(void)
  178. {
  179. BIO_METHOD *corebiometh = NULL;
  180. corebiometh = BIO_meth_new(BIO_TYPE_CORE_TO_PROV, "BIO to Core filter");
  181. if (corebiometh == NULL
  182. || !BIO_meth_set_write_ex(corebiometh, bio_core_write_ex)
  183. || !BIO_meth_set_read_ex(corebiometh, bio_core_read_ex)
  184. || !BIO_meth_set_puts(corebiometh, bio_core_puts)
  185. || !BIO_meth_set_gets(corebiometh, bio_core_gets)
  186. || !BIO_meth_set_ctrl(corebiometh, bio_core_ctrl)
  187. || !BIO_meth_set_create(corebiometh, bio_core_new)
  188. || !BIO_meth_set_destroy(corebiometh, bio_core_free)) {
  189. BIO_meth_free(corebiometh);
  190. return NULL;
  191. }
  192. return corebiometh;
  193. }
  194. BIO *ossl_bio_new_from_core_bio(PROV_CTX *provctx, OSSL_CORE_BIO *corebio)
  195. {
  196. BIO *outbio;
  197. BIO_METHOD *corebiometh = ossl_prov_ctx_get0_core_bio_method(provctx);
  198. if (corebiometh == NULL)
  199. return NULL;
  200. if ((outbio = BIO_new(corebiometh)) == NULL)
  201. return NULL;
  202. if (!ossl_prov_bio_up_ref(corebio)) {
  203. BIO_free(outbio);
  204. return NULL;
  205. }
  206. BIO_set_data(outbio, corebio);
  207. return outbio;
  208. }
  209. #endif