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