ossl_core_bio.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright 2021-2023 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.h>
  10. #include "bio_local.h"
  11. /*-
  12. * Core BIO structure
  13. * This is distinct from a BIO to prevent casting between the two which could
  14. * lead to versioning problems.
  15. */
  16. struct ossl_core_bio_st {
  17. CRYPTO_REF_COUNT ref_cnt;
  18. BIO *bio;
  19. };
  20. static OSSL_CORE_BIO *core_bio_new(void)
  21. {
  22. OSSL_CORE_BIO *cb = OPENSSL_malloc(sizeof(*cb));
  23. if (cb == NULL || !CRYPTO_NEW_REF(&cb->ref_cnt, 1)) {
  24. OPENSSL_free(cb);
  25. return NULL;
  26. }
  27. return cb;
  28. }
  29. int ossl_core_bio_up_ref(OSSL_CORE_BIO *cb)
  30. {
  31. int ref = 0;
  32. return CRYPTO_UP_REF(&cb->ref_cnt, &ref);
  33. }
  34. int ossl_core_bio_free(OSSL_CORE_BIO *cb)
  35. {
  36. int ref = 0, res = 1;
  37. if (cb != NULL) {
  38. CRYPTO_DOWN_REF(&cb->ref_cnt, &ref);
  39. if (ref <= 0) {
  40. res = BIO_free(cb->bio);
  41. CRYPTO_FREE_REF(&cb->ref_cnt);
  42. OPENSSL_free(cb);
  43. }
  44. }
  45. return res;
  46. }
  47. OSSL_CORE_BIO *ossl_core_bio_new_from_bio(BIO *bio)
  48. {
  49. OSSL_CORE_BIO *cb = core_bio_new();
  50. if (cb == NULL || !BIO_up_ref(bio)) {
  51. ossl_core_bio_free(cb);
  52. return NULL;
  53. }
  54. cb->bio = bio;
  55. return cb;
  56. }
  57. static OSSL_CORE_BIO *core_bio_new_from_new_bio(BIO *bio)
  58. {
  59. OSSL_CORE_BIO *cb = NULL;
  60. if (bio == NULL)
  61. return NULL;
  62. if ((cb = core_bio_new()) == NULL) {
  63. BIO_free(bio);
  64. return NULL;
  65. }
  66. cb->bio = bio;
  67. return cb;
  68. }
  69. OSSL_CORE_BIO *ossl_core_bio_new_file(const char *filename, const char *mode)
  70. {
  71. return core_bio_new_from_new_bio(BIO_new_file(filename, mode));
  72. }
  73. OSSL_CORE_BIO *ossl_core_bio_new_mem_buf(const void *buf, int len)
  74. {
  75. return core_bio_new_from_new_bio(BIO_new_mem_buf(buf, len));
  76. }
  77. int ossl_core_bio_read_ex(OSSL_CORE_BIO *cb, void *data, size_t dlen,
  78. size_t *readbytes)
  79. {
  80. return BIO_read_ex(cb->bio, data, dlen, readbytes);
  81. }
  82. int ossl_core_bio_write_ex(OSSL_CORE_BIO *cb, const void *data, size_t dlen,
  83. size_t *written)
  84. {
  85. return BIO_write_ex(cb->bio, data, dlen, written);
  86. }
  87. int ossl_core_bio_gets(OSSL_CORE_BIO *cb, char *buf, int size)
  88. {
  89. return BIO_gets(cb->bio, buf, size);
  90. }
  91. int ossl_core_bio_puts(OSSL_CORE_BIO *cb, const char *buf)
  92. {
  93. return BIO_puts(cb->bio, buf);
  94. }
  95. long ossl_core_bio_ctrl(OSSL_CORE_BIO *cb, int cmd, long larg, void *parg)
  96. {
  97. return BIO_ctrl(cb->bio, cmd, larg, parg);
  98. }
  99. int ossl_core_bio_vprintf(OSSL_CORE_BIO *cb, const char *format, va_list args)
  100. {
  101. return BIO_vprintf(cb->bio, format, args);
  102. }