]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/bio/ossl_core_bio.c
Raise an error on syscall failure in tls_retry_write_records
[thirdparty/openssl.git] / crypto / bio / ossl_core_bio.c
CommitLineData
141cc94e 1/*
da1c088f 2 * Copyright 2021-2023 The OpenSSL Project Authors. All Rights Reserved.
141cc94e
P
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 */
18struct ossl_core_bio_st {
19 CRYPTO_REF_COUNT ref_cnt;
141cc94e
P
20 BIO *bio;
21};
22
23static OSSL_CORE_BIO *core_bio_new(void)
24{
25 OSSL_CORE_BIO *cb = OPENSSL_malloc(sizeof(*cb));
26
a22d1966 27 if (cb == NULL || !CRYPTO_NEW_REF(&cb->ref_cnt, 1)) {
141cc94e
P
28 OPENSSL_free(cb);
29 return NULL;
30 }
141cc94e
P
31 return cb;
32}
33
34int ossl_core_bio_up_ref(OSSL_CORE_BIO *cb)
35{
36 int ref = 0;
37
a22d1966 38 return CRYPTO_UP_REF(&cb->ref_cnt, &ref);
141cc94e
P
39}
40
41int ossl_core_bio_free(OSSL_CORE_BIO *cb)
42{
43 int ref = 0, res = 1;
44
45 if (cb != NULL) {
a22d1966 46 CRYPTO_DOWN_REF(&cb->ref_cnt, &ref);
141cc94e
P
47 if (ref <= 0) {
48 res = BIO_free(cb->bio);
a22d1966 49 CRYPTO_FREE_REF(&cb->ref_cnt);
141cc94e
P
50 OPENSSL_free(cb);
51 }
52 }
53 return res;
54}
55
56OSSL_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
68static 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
82OSSL_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
87OSSL_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
92int 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
98int 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
104int ossl_core_bio_gets(OSSL_CORE_BIO *cb, char *buf, int size)
105{
106 return BIO_gets(cb->bio, buf, size);
107}
108
109int ossl_core_bio_puts(OSSL_CORE_BIO *cb, const char *buf)
110{
111 return BIO_puts(cb->bio, buf);
112}
113
114long 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
119int ossl_core_bio_vprintf(OSSL_CORE_BIO *cb, const char *format, va_list args)
120{
121 return BIO_vprintf(cb->bio, format, args);
122}