| 1 |
// Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/bio.h> |
| 5 |
|
| 6 |
#include <assert.h> |
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
#include <openssl/err.h> |
| 10 |
#include <openssl/mem.h> |
| 11 |
|
| 12 |
#include "../internal.h" |
| 13 |
|
| 14 |
|
| 15 |
struct bio_bio_st { |
| 16 |
BIO *peer; // NULL if buf == NULL. |
| 17 |
// If peer != NULL, then peer->ptr is also a bio_bio_st, |
| 18 |
// and its "peer" member points back to us. |
| 19 |
// peer != NULL iff init != 0 in the BIO. |
| 20 |
|
| 21 |
// This is for what we write (i.e. reading uses peer's struct): |
| 22 |
int closed; // valid iff peer != NULL |
| 23 |
size_t len; // valid iff buf != NULL; 0 if peer == NULL |
| 24 |
size_t offset; // valid iff buf != NULL; 0 if len == 0 |
| 25 |
size_t size; |
| 26 |
uint8_t *buf; // "size" elements (if != NULL) |
| 27 |
|
| 28 |
size_t request; // valid iff peer != NULL; 0 if len != 0, |
| 29 |
// otherwise set by peer to number of bytes |
| 30 |
// it (unsuccessfully) tried to read, |
| 31 |
// never more than buffer space (size-len) warrants. |
| 32 |
}; |
| 33 |
|
| 34 |
static int bio_new(BIO *bio) { |
| 35 |
struct bio_bio_st *b = OPENSSL_zalloc(sizeof *b); |
| 36 |
if (b == NULL) { |
| 37 |
return 0; |
| 38 |
} |
| 39 |
|
| 40 |
b->size = 17 * 1024; // enough for one TLS record (just a default) |
| 41 |
bio->ptr = b; |
| 42 |
return 1; |
| 43 |
} |
| 44 |
|
| 45 |
static void bio_destroy_pair(BIO *bio) { |
| 46 |
struct bio_bio_st *b = bio->ptr; |
| 47 |
BIO *peer_bio; |
| 48 |
struct bio_bio_st *peer_b; |
| 49 |
|
| 50 |
if (b == NULL) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
peer_bio = b->peer; |
| 55 |
if (peer_bio == NULL) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
peer_b = peer_bio->ptr; |
| 60 |
|
| 61 |
assert(peer_b != NULL); |
| 62 |
assert(peer_b->peer == bio); |
| 63 |
|
| 64 |
peer_b->peer = NULL; |
| 65 |
peer_bio->init = 0; |
| 66 |
assert(peer_b->buf != NULL); |
| 67 |
peer_b->len = 0; |
| 68 |
peer_b->offset = 0; |
| 69 |
|
| 70 |
b->peer = NULL; |
| 71 |
bio->init = 0; |
| 72 |
assert(b->buf != NULL); |
| 73 |
b->len = 0; |
| 74 |
b->offset = 0; |
| 75 |
} |
| 76 |
|
| 77 |
static int bio_free(BIO *bio) { |
| 78 |
struct bio_bio_st *b = bio->ptr; |
| 79 |
|
| 80 |
assert(b != NULL); |
| 81 |
|
| 82 |
if (b->peer) { |
| 83 |
bio_destroy_pair(bio); |
| 84 |
} |
| 85 |
|
| 86 |
OPENSSL_free(b->buf); |
| 87 |
OPENSSL_free(b); |
| 88 |
|
| 89 |
return 1; |
| 90 |
} |
| 91 |
|
| 92 |
static int bio_read(BIO *bio, char *buf, int size_) { |
| 93 |
size_t size = size_; |
| 94 |
size_t rest; |
| 95 |
struct bio_bio_st *b, *peer_b; |
| 96 |
|
| 97 |
BIO_clear_retry_flags(bio); |
| 98 |
|
| 99 |
if (!bio->init) { |
| 100 |
return 0; |
| 101 |
} |
| 102 |
|
| 103 |
b = bio->ptr; |
| 104 |
assert(b != NULL); |
| 105 |
assert(b->peer != NULL); |
| 106 |
peer_b = b->peer->ptr; |
| 107 |
assert(peer_b != NULL); |
| 108 |
assert(peer_b->buf != NULL); |
| 109 |
|
| 110 |
peer_b->request = 0; // will be set in "retry_read" situation |
| 111 |
|
| 112 |
if (buf == NULL || size == 0) { |
| 113 |
return 0; |
| 114 |
} |
| 115 |
|
| 116 |
if (peer_b->len == 0) { |
| 117 |
if (peer_b->closed) { |
| 118 |
return 0; // writer has closed, and no data is left |
| 119 |
} else { |
| 120 |
BIO_set_retry_read(bio); // buffer is empty |
| 121 |
if (size <= peer_b->size) { |
| 122 |
peer_b->request = size; |
| 123 |
} else { |
| 124 |
// don't ask for more than the peer can |
| 125 |
// deliver in one write |
| 126 |
peer_b->request = peer_b->size; |
| 127 |
} |
| 128 |
return -1; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
// we can read |
| 133 |
if (peer_b->len < size) { |
| 134 |
size = peer_b->len; |
| 135 |
} |
| 136 |
|
| 137 |
// now read "size" bytes |
| 138 |
rest = size; |
| 139 |
|
| 140 |
assert(rest > 0); |
| 141 |
// one or two iterations |
| 142 |
do { |
| 143 |
size_t chunk; |
| 144 |
|
| 145 |
assert(rest <= peer_b->len); |
| 146 |
if (peer_b->offset + rest <= peer_b->size) { |
| 147 |
chunk = rest; |
| 148 |
} else { |
| 149 |
// wrap around ring buffer |
| 150 |
chunk = peer_b->size - peer_b->offset; |
| 151 |
} |
| 152 |
assert(peer_b->offset + chunk <= peer_b->size); |
| 153 |
|
| 154 |
OPENSSL_memcpy(buf, peer_b->buf + peer_b->offset, chunk); |
| 155 |
|
| 156 |
peer_b->len -= chunk; |
| 157 |
if (peer_b->len) { |
| 158 |
peer_b->offset += chunk; |
| 159 |
assert(peer_b->offset <= peer_b->size); |
| 160 |
if (peer_b->offset == peer_b->size) { |
| 161 |
peer_b->offset = 0; |
| 162 |
} |
| 163 |
buf += chunk; |
| 164 |
} else { |
| 165 |
// buffer now empty, no need to advance "buf" |
| 166 |
assert(chunk == rest); |
| 167 |
peer_b->offset = 0; |
| 168 |
} |
| 169 |
rest -= chunk; |
| 170 |
} while (rest); |
| 171 |
|
| 172 |
// |size| is bounded by the buffer size, which fits in |int|. |
| 173 |
return (int)size; |
| 174 |
} |
| 175 |
|
| 176 |
static int bio_write(BIO *bio, const char *buf, int num_) { |
| 177 |
size_t num = num_; |
| 178 |
size_t rest; |
| 179 |
struct bio_bio_st *b; |
| 180 |
|
| 181 |
BIO_clear_retry_flags(bio); |
| 182 |
|
| 183 |
if (!bio->init || buf == NULL || num == 0) { |
| 184 |
return 0; |
| 185 |
} |
| 186 |
|
| 187 |
b = bio->ptr; |
| 188 |
assert(b != NULL); |
| 189 |
assert(b->peer != NULL); |
| 190 |
assert(b->buf != NULL); |
| 191 |
|
| 192 |
b->request = 0; |
| 193 |
if (b->closed) { |
| 194 |
// we already closed |
| 195 |
OPENSSL_PUT_ERROR(BIO, BIO_R_BROKEN_PIPE); |
| 196 |
return -1; |
| 197 |
} |
| 198 |
|
| 199 |
assert(b->len <= b->size); |
| 200 |
|
| 201 |
if (b->len == b->size) { |
| 202 |
BIO_set_retry_write(bio); // buffer is full |
| 203 |
return -1; |
| 204 |
} |
| 205 |
|
| 206 |
// we can write |
| 207 |
if (num > b->size - b->len) { |
| 208 |
num = b->size - b->len; |
| 209 |
} |
| 210 |
|
| 211 |
// now write "num" bytes |
| 212 |
rest = num; |
| 213 |
|
| 214 |
assert(rest > 0); |
| 215 |
// one or two iterations |
| 216 |
do { |
| 217 |
size_t write_offset; |
| 218 |
size_t chunk; |
| 219 |
|
| 220 |
assert(b->len + rest <= b->size); |
| 221 |
|
| 222 |
write_offset = b->offset + b->len; |
| 223 |
if (write_offset >= b->size) { |
| 224 |
write_offset -= b->size; |
| 225 |
} |
| 226 |
// b->buf[write_offset] is the first byte we can write to. |
| 227 |
|
| 228 |
if (write_offset + rest <= b->size) { |
| 229 |
chunk = rest; |
| 230 |
} else { |
| 231 |
// wrap around ring buffer |
| 232 |
chunk = b->size - write_offset; |
| 233 |
} |
| 234 |
|
| 235 |
OPENSSL_memcpy(b->buf + write_offset, buf, chunk); |
| 236 |
|
| 237 |
b->len += chunk; |
| 238 |
|
| 239 |
assert(b->len <= b->size); |
| 240 |
|
| 241 |
rest -= chunk; |
| 242 |
buf += chunk; |
| 243 |
} while (rest); |
| 244 |
|
| 245 |
// |num| is bounded by the buffer size, which fits in |int|. |
| 246 |
return (int)num; |
| 247 |
} |
| 248 |
|
| 249 |
static int bio_make_pair(BIO *bio1, BIO *bio2, size_t writebuf1_len, |
| 250 |
size_t writebuf2_len) { |
| 251 |
struct bio_bio_st *b1, *b2; |
| 252 |
|
| 253 |
assert(bio1 != NULL); |
| 254 |
assert(bio2 != NULL); |
| 255 |
|
| 256 |
b1 = bio1->ptr; |
| 257 |
b2 = bio2->ptr; |
| 258 |
|
| 259 |
if (b1->peer != NULL || b2->peer != NULL) { |
| 260 |
OPENSSL_PUT_ERROR(BIO, BIO_R_IN_USE); |
| 261 |
return 0; |
| 262 |
} |
| 263 |
|
| 264 |
if (b1->buf == NULL) { |
| 265 |
if (writebuf1_len) { |
| 266 |
b1->size = writebuf1_len; |
| 267 |
} |
| 268 |
b1->buf = OPENSSL_malloc(b1->size); |
| 269 |
if (b1->buf == NULL) { |
| 270 |
return 0; |
| 271 |
} |
| 272 |
b1->len = 0; |
| 273 |
b1->offset = 0; |
| 274 |
} |
| 275 |
|
| 276 |
if (b2->buf == NULL) { |
| 277 |
if (writebuf2_len) { |
| 278 |
b2->size = writebuf2_len; |
| 279 |
} |
| 280 |
b2->buf = OPENSSL_malloc(b2->size); |
| 281 |
if (b2->buf == NULL) { |
| 282 |
return 0; |
| 283 |
} |
| 284 |
b2->len = 0; |
| 285 |
b2->offset = 0; |
| 286 |
} |
| 287 |
|
| 288 |
b1->peer = bio2; |
| 289 |
b1->closed = 0; |
| 290 |
b1->request = 0; |
| 291 |
b2->peer = bio1; |
| 292 |
b2->closed = 0; |
| 293 |
b2->request = 0; |
| 294 |
|
| 295 |
bio1->init = 1; |
| 296 |
bio2->init = 1; |
| 297 |
|
| 298 |
return 1; |
| 299 |
} |
| 300 |
|
| 301 |
static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr) { |
| 302 |
long ret; |
| 303 |
struct bio_bio_st *b = bio->ptr; |
| 304 |
|
| 305 |
assert(b != NULL); |
| 306 |
|
| 307 |
switch (cmd) { |
| 308 |
// specific CTRL codes |
| 309 |
|
| 310 |
case BIO_C_GET_WRITE_BUF_SIZE: |
| 311 |
ret = (long)b->size; |
| 312 |
break; |
| 313 |
|
| 314 |
case BIO_C_GET_WRITE_GUARANTEE: |
| 315 |
// How many bytes can the caller feed to the next write |
| 316 |
// without having to keep any? |
| 317 |
if (b->peer == NULL || b->closed) { |
| 318 |
ret = 0; |
| 319 |
} else { |
| 320 |
ret = (long)b->size - b->len; |
| 321 |
} |
| 322 |
break; |
| 323 |
|
| 324 |
case BIO_C_GET_READ_REQUEST: |
| 325 |
// If the peer unsuccessfully tried to read, how many bytes |
| 326 |
// were requested? (As with BIO_CTRL_PENDING, that number |
| 327 |
// can usually be treated as boolean.) |
| 328 |
ret = (long)b->request; |
| 329 |
break; |
| 330 |
|
| 331 |
case BIO_C_RESET_READ_REQUEST: |
| 332 |
// Reset request. (Can be useful after read attempts |
| 333 |
// at the other side that are meant to be non-blocking, |
| 334 |
// e.g. when probing SSL_read to see if any data is |
| 335 |
// available.) |
| 336 |
b->request = 0; |
| 337 |
ret = 1; |
| 338 |
break; |
| 339 |
|
| 340 |
case BIO_C_SHUTDOWN_WR: |
| 341 |
// similar to shutdown(..., SHUT_WR) |
| 342 |
b->closed = 1; |
| 343 |
ret = 1; |
| 344 |
break; |
| 345 |
|
| 346 |
// standard CTRL codes follow |
| 347 |
|
| 348 |
case BIO_CTRL_GET_CLOSE: |
| 349 |
ret = bio->shutdown; |
| 350 |
break; |
| 351 |
|
| 352 |
case BIO_CTRL_SET_CLOSE: |
| 353 |
bio->shutdown = (int)num; |
| 354 |
ret = 1; |
| 355 |
break; |
| 356 |
|
| 357 |
case BIO_CTRL_PENDING: |
| 358 |
if (b->peer != NULL) { |
| 359 |
struct bio_bio_st *peer_b = b->peer->ptr; |
| 360 |
ret = (long)peer_b->len; |
| 361 |
} else { |
| 362 |
ret = 0; |
| 363 |
} |
| 364 |
break; |
| 365 |
|
| 366 |
case BIO_CTRL_WPENDING: |
| 367 |
ret = 0; |
| 368 |
if (b->buf != NULL) { |
| 369 |
ret = (long)b->len; |
| 370 |
} |
| 371 |
break; |
| 372 |
|
| 373 |
case BIO_CTRL_FLUSH: |
| 374 |
ret = 1; |
| 375 |
break; |
| 376 |
|
| 377 |
case BIO_CTRL_EOF: { |
| 378 |
ret = 1; |
| 379 |
if (b->peer) { |
| 380 |
struct bio_bio_st *peer_b = b->peer->ptr; |
| 381 |
assert(peer_b != NULL); |
| 382 |
ret = peer_b->len == 0 && peer_b->closed; |
| 383 |
} |
| 384 |
} break; |
| 385 |
|
| 386 |
default: |
| 387 |
ret = 0; |
| 388 |
} |
| 389 |
return ret; |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
static const BIO_METHOD methods_biop = { |
| 394 |
BIO_TYPE_BIO, "BIO pair", bio_write, bio_read, NULL /* puts */, |
| 395 |
NULL /* gets */, bio_ctrl, bio_new, bio_free, NULL /* callback_ctrl */, |
| 396 |
}; |
| 397 |
|
| 398 |
static const BIO_METHOD *bio_s_bio(void) { return &methods_biop; } |
| 399 |
|
| 400 |
int BIO_new_bio_pair(BIO** bio1_p, size_t writebuf1_len, |
| 401 |
BIO** bio2_p, size_t writebuf2_len) { |
| 402 |
BIO *bio1 = BIO_new(bio_s_bio()); |
| 403 |
BIO *bio2 = BIO_new(bio_s_bio()); |
| 404 |
if (bio1 == NULL || bio2 == NULL || |
| 405 |
!bio_make_pair(bio1, bio2, writebuf1_len, writebuf2_len)) { |
| 406 |
BIO_free(bio1); |
| 407 |
BIO_free(bio2); |
| 408 |
*bio1_p = NULL; |
| 409 |
*bio2_p = NULL; |
| 410 |
return 0; |
| 411 |
} |
| 412 |
|
| 413 |
*bio1_p = bio1; |
| 414 |
*bio2_p = bio2; |
| 415 |
return 1; |
| 416 |
} |
| 417 |
|
| 418 |
int BIO_destroy_bio_pair(BIO *b) { |
| 419 |
if (b == NULL) { |
| 420 |
return 0; |
| 421 |
} |
| 422 |
bio_destroy_pair(b); |
| 423 |
return 1; |
| 424 |
} |
| 425 |
|
| 426 |
size_t BIO_ctrl_get_read_request(BIO *bio) { |
| 427 |
return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL); |
| 428 |
} |
| 429 |
|
| 430 |
size_t BIO_ctrl_get_write_guarantee(BIO *bio) { |
| 431 |
return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL); |
| 432 |
} |
| 433 |
|
| 434 |
int BIO_shutdown_wr(BIO *bio) { |
| 435 |
return (int)BIO_ctrl(bio, BIO_C_SHUTDOWN_WR, 0, NULL); |
| 436 |
} |