core_bio.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright 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.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. CRYPTO_RWLOCK *ref_lock;
  19. BIO *bio;
  20. };
  21. static OSSL_CORE_BIO *core_bio_new(void)
  22. {
  23. OSSL_CORE_BIO *cb = OPENSSL_malloc(sizeof(*cb));
  24. if (cb == NULL || (cb->ref_lock = CRYPTO_THREAD_lock_new()) == NULL) {
  25. OPENSSL_free(cb);
  26. return NULL;
  27. }
  28. cb->ref_cnt = 1;
  29. return cb;
  30. }
  31. int ossl_core_bio_up_ref(OSSL_CORE_BIO *cb)
  32. {
  33. int ref = 0;
  34. return CRYPTO_UP_REF(&cb->ref_cnt, &ref, cb->ref_lock);
  35. }
  36. int ossl_core_bio_free(OSSL_CORE_BIO *cb)
  37. {
  38. int ref = 0, res = 1;
  39. if (cb != NULL) {
  40. CRYPTO_DOWN_REF(&cb->ref_cnt, &ref, cb->ref_lock);
  41. if (ref <= 0) {
  42. res = BIO_free(cb->bio);
  43. CRYPTO_THREAD_lock_free(cb->ref_lock);
  44. OPENSSL_free(cb);
  45. }
  46. }
  47. return res;
  48. }
  49. OSSL_CORE_BIO *ossl_core_bio_new_from_bio(BIO *bio)
  50. {
  51. OSSL_CORE_BIO *cb = core_bio_new();
  52. if (cb == NULL || !BIO_up_ref(bio)) {
  53. ossl_core_bio_free(cb);
  54. return NULL;
  55. }
  56. cb->bio = bio;
  57. return cb;
  58. }
  59. static OSSL_CORE_BIO *core_bio_new_from_new_bio(BIO *bio)
  60. {
  61. OSSL_CORE_BIO *cb = NULL;
  62. if (bio == NULL)
  63. return NULL;
  64. if ((cb = core_bio_new()) == NULL) {
  65. BIO_free(bio);
  66. return NULL;
  67. }
  68. cb->bio = bio;
  69. return cb;
  70. }
  71. OSSL_CORE_BIO *ossl_core_bio_new_file(const char *filename, const char *mode)
  72. {
  73. return core_bio_new_from_new_bio(BIO_new_file(filename, mode));
  74. }
  75. OSSL_CORE_BIO *ossl_core_bio_new_mem_buf(const void *buf, int len)
  76. {
  77. return core_bio_new_from_new_bio(BIO_new_mem_buf(buf, len));
  78. }
  79. int ossl_core_bio_read_ex(OSSL_CORE_BIO *cb, void *data, size_t dlen,
  80. size_t *readbytes)
  81. {
  82. return BIO_read_ex(cb->bio, data, dlen, readbytes);
  83. }
  84. int ossl_core_bio_write_ex(OSSL_CORE_BIO *cb, const void *data, size_t dlen,
  85. size_t *written)
  86. {
  87. return BIO_write_ex(cb->bio, data, dlen, written);
  88. }
  89. int ossl_core_bio_gets(OSSL_CORE_BIO *cb, char *buf, int size)
  90. {
  91. return BIO_gets(cb->bio, buf, size);
  92. }
  93. int ossl_core_bio_puts(OSSL_CORE_BIO *cb, const char *buf)
  94. {
  95. return BIO_puts(cb->bio, buf);
  96. }
  97. long ossl_core_bio_ctrl(OSSL_CORE_BIO *cb, int cmd, long larg, void *parg)
  98. {
  99. return BIO_ctrl(cb->bio, cmd, larg, parg);
  100. }
  101. int ossl_core_bio_vprintf(OSSL_CORE_BIO *cb, const char *format, va_list args)
  102. {
  103. return BIO_vprintf(cb->bio, format, args);
  104. }