| 1 |
// Copyright (c) 2014, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include "async_bio.h" |
| 5 |
|
| 6 |
#include <errno.h> |
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
#include <openssl/bio.h> |
| 10 |
#include <openssl/mem.h> |
| 11 |
|
| 12 |
#include "../../crypto/internal.h" |
| 13 |
|
| 14 |
|
| 15 |
namespace { |
| 16 |
|
| 17 |
extern const BIO_METHOD g_async_bio_method; |
| 18 |
|
| 19 |
struct AsyncBio { |
| 20 |
bool datagram; |
| 21 |
bool enforce_write_quota; |
| 22 |
size_t read_quota; |
| 23 |
size_t write_quota; |
| 24 |
}; |
| 25 |
|
| 26 |
AsyncBio *GetData(BIO *bio) { |
| 27 |
if (bio->method != &g_async_bio_method) { |
| 28 |
return NULL; |
| 29 |
} |
| 30 |
return (AsyncBio *)bio->ptr; |
| 31 |
} |
| 32 |
|
| 33 |
static int AsyncWrite(BIO *bio, const char *in, int inl) { |
| 34 |
AsyncBio *a = GetData(bio); |
| 35 |
if (a == NULL || bio->next_bio == NULL) { |
| 36 |
return 0; |
| 37 |
} |
| 38 |
|
| 39 |
if (!a->enforce_write_quota) { |
| 40 |
return BIO_write(bio->next_bio, in, inl); |
| 41 |
} |
| 42 |
|
| 43 |
BIO_clear_retry_flags(bio); |
| 44 |
|
| 45 |
if (a->write_quota == 0) { |
| 46 |
BIO_set_retry_write(bio); |
| 47 |
errno = EAGAIN; |
| 48 |
return -1; |
| 49 |
} |
| 50 |
|
| 51 |
if (!a->datagram && static_cast<size_t>(inl) > a->write_quota) { |
| 52 |
inl = static_cast<int>(a->write_quota); |
| 53 |
} |
| 54 |
int ret = BIO_write(bio->next_bio, in, inl); |
| 55 |
if (ret <= 0) { |
| 56 |
BIO_copy_next_retry(bio); |
| 57 |
} else { |
| 58 |
a->write_quota -= (a->datagram ? 1 : ret); |
| 59 |
} |
| 60 |
return ret; |
| 61 |
} |
| 62 |
|
| 63 |
static int AsyncRead(BIO *bio, char *out, int outl) { |
| 64 |
AsyncBio *a = GetData(bio); |
| 65 |
if (a == NULL || bio->next_bio == NULL) { |
| 66 |
return 0; |
| 67 |
} |
| 68 |
|
| 69 |
BIO_clear_retry_flags(bio); |
| 70 |
|
| 71 |
if (a->read_quota == 0) { |
| 72 |
BIO_set_retry_read(bio); |
| 73 |
errno = EAGAIN; |
| 74 |
return -1; |
| 75 |
} |
| 76 |
|
| 77 |
if (!a->datagram && static_cast<size_t>(outl) > a->read_quota) { |
| 78 |
outl = static_cast<int>(a->read_quota); |
| 79 |
} |
| 80 |
int ret = BIO_read(bio->next_bio, out, outl); |
| 81 |
if (ret <= 0) { |
| 82 |
BIO_copy_next_retry(bio); |
| 83 |
} else { |
| 84 |
a->read_quota -= (a->datagram ? 1 : ret); |
| 85 |
} |
| 86 |
return ret; |
| 87 |
} |
| 88 |
|
| 89 |
static long AsyncCtrl(BIO *bio, int cmd, long num, void *ptr) { |
| 90 |
if (bio->next_bio == NULL) { |
| 91 |
return 0; |
| 92 |
} |
| 93 |
BIO_clear_retry_flags(bio); |
| 94 |
long ret = BIO_ctrl(bio->next_bio, cmd, num, ptr); |
| 95 |
BIO_copy_next_retry(bio); |
| 96 |
return ret; |
| 97 |
} |
| 98 |
|
| 99 |
static int AsyncNew(BIO *bio) { |
| 100 |
AsyncBio *a = (AsyncBio *)OPENSSL_zalloc(sizeof(*a)); |
| 101 |
if (a == NULL) { |
| 102 |
return 0; |
| 103 |
} |
| 104 |
a->enforce_write_quota = true; |
| 105 |
bio->init = 1; |
| 106 |
bio->ptr = (char *)a; |
| 107 |
return 1; |
| 108 |
} |
| 109 |
|
| 110 |
static int AsyncFree(BIO *bio) { |
| 111 |
if (bio == NULL) { |
| 112 |
return 0; |
| 113 |
} |
| 114 |
|
| 115 |
OPENSSL_free(bio->ptr); |
| 116 |
bio->ptr = NULL; |
| 117 |
bio->init = 0; |
| 118 |
bio->flags = 0; |
| 119 |
return 1; |
| 120 |
} |
| 121 |
|
| 122 |
static long AsyncCallbackCtrl(BIO *bio, int cmd, bio_info_cb fp) { |
| 123 |
if (bio->next_bio == NULL) { |
| 124 |
return 0; |
| 125 |
} |
| 126 |
return BIO_callback_ctrl(bio->next_bio, cmd, fp); |
| 127 |
} |
| 128 |
|
| 129 |
const BIO_METHOD g_async_bio_method = { |
| 130 |
BIO_TYPE_FILTER, |
| 131 |
"async bio", |
| 132 |
AsyncWrite, |
| 133 |
AsyncRead, |
| 134 |
NULL /* puts */, |
| 135 |
NULL /* gets */, |
| 136 |
AsyncCtrl, |
| 137 |
AsyncNew, |
| 138 |
AsyncFree, |
| 139 |
AsyncCallbackCtrl, |
| 140 |
}; |
| 141 |
|
| 142 |
} // namespace |
| 143 |
|
| 144 |
bssl::UniquePtr<BIO> AsyncBioCreate() { |
| 145 |
return bssl::UniquePtr<BIO>(BIO_new(&g_async_bio_method)); |
| 146 |
} |
| 147 |
|
| 148 |
bssl::UniquePtr<BIO> AsyncBioCreateDatagram() { |
| 149 |
bssl::UniquePtr<BIO> ret(BIO_new(&g_async_bio_method)); |
| 150 |
if (!ret) { |
| 151 |
return nullptr; |
| 152 |
} |
| 153 |
GetData(ret.get())->datagram = true; |
| 154 |
return ret; |
| 155 |
} |
| 156 |
|
| 157 |
void AsyncBioAllowRead(BIO *bio, size_t count) { |
| 158 |
AsyncBio *a = GetData(bio); |
| 159 |
if (a == NULL) { |
| 160 |
return; |
| 161 |
} |
| 162 |
a->read_quota += count; |
| 163 |
} |
| 164 |
|
| 165 |
void AsyncBioAllowWrite(BIO *bio, size_t count) { |
| 166 |
AsyncBio *a = GetData(bio); |
| 167 |
if (a == NULL) { |
| 168 |
return; |
| 169 |
} |
| 170 |
a->write_quota += count; |
| 171 |
} |
| 172 |
|
| 173 |
void AsyncBioEnforceWriteQuota(BIO *bio, bool enforce) { |
| 174 |
AsyncBio *a = GetData(bio); |
| 175 |
if (a == NULL) { |
| 176 |
return; |
| 177 |
} |
| 178 |
a->enforce_write_quota = enforce; |
| 179 |
} |