bss_core.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 <openssl/core_dispatch.h>
  10. #include "bio_local.h"
  11. #include "internal/cryptlib.h"
  12. typedef struct {
  13. OSSL_FUNC_BIO_read_ex_fn *c_bio_read_ex;
  14. OSSL_FUNC_BIO_write_ex_fn *c_bio_write_ex;
  15. OSSL_FUNC_BIO_gets_fn *c_bio_gets;
  16. OSSL_FUNC_BIO_puts_fn *c_bio_puts;
  17. OSSL_FUNC_BIO_ctrl_fn *c_bio_ctrl;
  18. OSSL_FUNC_BIO_up_ref_fn *c_bio_up_ref;
  19. OSSL_FUNC_BIO_free_fn *c_bio_free;
  20. } BIO_CORE_GLOBALS;
  21. static void bio_core_globals_free(void *vbcg)
  22. {
  23. OPENSSL_free(vbcg);
  24. }
  25. static void *bio_core_globals_new(OSSL_LIB_CTX *ctx)
  26. {
  27. return OPENSSL_zalloc(sizeof(BIO_CORE_GLOBALS));
  28. }
  29. static const OSSL_LIB_CTX_METHOD bio_core_globals_method = {
  30. OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
  31. bio_core_globals_new,
  32. bio_core_globals_free,
  33. };
  34. static ossl_inline BIO_CORE_GLOBALS *get_globals(OSSL_LIB_CTX *libctx)
  35. {
  36. return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_BIO_CORE_INDEX,
  37. &bio_core_globals_method);
  38. }
  39. static int bio_core_read_ex(BIO *bio, char *data, size_t data_len,
  40. size_t *bytes_read)
  41. {
  42. BIO_CORE_GLOBALS *bcgbl = get_globals(bio->libctx);
  43. if (bcgbl->c_bio_read_ex == NULL)
  44. return 0;
  45. return bcgbl->c_bio_read_ex(BIO_get_data(bio), data, data_len, bytes_read);
  46. }
  47. static int bio_core_write_ex(BIO *bio, const char *data, size_t data_len,
  48. size_t *written)
  49. {
  50. BIO_CORE_GLOBALS *bcgbl = get_globals(bio->libctx);
  51. if (bcgbl->c_bio_write_ex == NULL)
  52. return 0;
  53. return bcgbl->c_bio_write_ex(BIO_get_data(bio), data, data_len, written);
  54. }
  55. static long bio_core_ctrl(BIO *bio, int cmd, long num, void *ptr)
  56. {
  57. BIO_CORE_GLOBALS *bcgbl = get_globals(bio->libctx);
  58. if (bcgbl->c_bio_ctrl == NULL)
  59. return -1;
  60. return bcgbl->c_bio_ctrl(BIO_get_data(bio), cmd, num, ptr);
  61. }
  62. static int bio_core_gets(BIO *bio, char *buf, int size)
  63. {
  64. BIO_CORE_GLOBALS *bcgbl = get_globals(bio->libctx);
  65. if (bcgbl->c_bio_gets == NULL)
  66. return -1;
  67. return bcgbl->c_bio_gets(BIO_get_data(bio), buf, size);
  68. }
  69. static int bio_core_puts(BIO *bio, const char *str)
  70. {
  71. BIO_CORE_GLOBALS *bcgbl = get_globals(bio->libctx);
  72. if (bcgbl->c_bio_puts == NULL)
  73. return -1;
  74. return bcgbl->c_bio_puts(BIO_get_data(bio), str);
  75. }
  76. static int bio_core_new(BIO *bio)
  77. {
  78. BIO_set_init(bio, 1);
  79. return 1;
  80. }
  81. static int bio_core_free(BIO *bio)
  82. {
  83. BIO_CORE_GLOBALS *bcgbl = get_globals(bio->libctx);
  84. BIO_set_init(bio, 0);
  85. bcgbl->c_bio_free(BIO_get_data(bio));
  86. return 1;
  87. }
  88. static const BIO_METHOD corebiometh = {
  89. BIO_TYPE_CORE_TO_PROV,
  90. "BIO to Core filter",
  91. bio_core_write_ex,
  92. NULL,
  93. bio_core_read_ex,
  94. NULL,
  95. bio_core_puts,
  96. bio_core_gets,
  97. bio_core_ctrl,
  98. bio_core_new,
  99. bio_core_free,
  100. NULL,
  101. };
  102. const BIO_METHOD *BIO_s_core(void)
  103. {
  104. return &corebiometh;
  105. }
  106. BIO *BIO_new_from_core_bio(OSSL_LIB_CTX *libctx, OSSL_CORE_BIO *corebio)
  107. {
  108. BIO *outbio;
  109. BIO_CORE_GLOBALS *bcgbl = get_globals(libctx);
  110. /* Check the library context has been initialised with the callbacks */
  111. if (bcgbl->c_bio_write_ex == NULL && bcgbl->c_bio_read_ex == NULL)
  112. return NULL;
  113. if ((outbio = BIO_new_ex(libctx, BIO_s_core())) == NULL)
  114. return NULL;
  115. if (!bcgbl->c_bio_up_ref(corebio)) {
  116. BIO_free(outbio);
  117. return NULL;
  118. }
  119. BIO_set_data(outbio, corebio);
  120. return outbio;
  121. }
  122. int ossl_bio_init_core(OSSL_LIB_CTX *libctx, const OSSL_DISPATCH *fns)
  123. {
  124. BIO_CORE_GLOBALS *bcgbl = get_globals(libctx);
  125. for (; fns->function_id != 0; fns++) {
  126. switch (fns->function_id) {
  127. case OSSL_FUNC_BIO_READ_EX:
  128. if (bcgbl->c_bio_read_ex == NULL)
  129. bcgbl->c_bio_read_ex = OSSL_FUNC_BIO_read_ex(fns);
  130. break;
  131. case OSSL_FUNC_BIO_WRITE_EX:
  132. if (bcgbl->c_bio_write_ex == NULL)
  133. bcgbl->c_bio_write_ex = OSSL_FUNC_BIO_write_ex(fns);
  134. break;
  135. case OSSL_FUNC_BIO_GETS:
  136. if (bcgbl->c_bio_gets == NULL)
  137. bcgbl->c_bio_gets = OSSL_FUNC_BIO_gets(fns);
  138. break;
  139. case OSSL_FUNC_BIO_PUTS:
  140. if (bcgbl->c_bio_puts == NULL)
  141. bcgbl->c_bio_puts = OSSL_FUNC_BIO_puts(fns);
  142. break;
  143. case OSSL_FUNC_BIO_CTRL:
  144. if (bcgbl->c_bio_ctrl == NULL)
  145. bcgbl->c_bio_ctrl = OSSL_FUNC_BIO_ctrl(fns);
  146. break;
  147. case OSSL_FUNC_BIO_UP_REF:
  148. if (bcgbl->c_bio_up_ref == NULL)
  149. bcgbl->c_bio_up_ref = OSSL_FUNC_BIO_up_ref(fns);
  150. break;
  151. case OSSL_FUNC_BIO_FREE:
  152. if (bcgbl->c_bio_free == NULL)
  153. bcgbl->c_bio_free = OSSL_FUNC_BIO_free(fns);
  154. break;
  155. }
  156. }
  157. return 1;
  158. }