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