2
0

bss_core.c 5.0 KB

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