| 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 <errno.h> |
| 19 | #include <limits.h> |
| 20 | #include <string.h> |
| 21 |
| 22 | #include <openssl/asn1.h> |
| 23 | #include <openssl/err.h> |
| 24 | #include <openssl/mem.h> |
| 25 | #include <openssl/thread.h> |
| 26 |
| 27 | #include "../internal.h" |
| 28 |
| 29 |
| 30 | static CRYPTO_EX_DATA_CLASS g_ex_data_class = |
| 31 | CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA; |
| 32 |
| 33 | BIO *BIO_new(const BIO_METHOD *method) { |
| 34 | BIO *ret = reinterpret_cast<BIO *>(OPENSSL_zalloc(sizeof(BIO))); |
| 35 | if (ret == NULL) { |
| 36 | return NULL; |
| 37 | } |
| 38 |
| 39 | ret->method = method; |
| 40 | ret->shutdown = 1; |
| 41 | ret->references = 1; |
| 42 | CRYPTO_new_ex_data(&ret->ex_data); |
| 43 |
| 44 | if (method->create != NULL && !method->create(ret)) { |
| 45 | OPENSSL_free(ret); |
| 46 | return NULL; |
| 47 | } |
| 48 |
| 49 | return ret; |
| 50 | } |
| 51 |
| 52 | int BIO_free(BIO *bio) { |
| 53 | BIO *next_bio; |
| 54 |
| 55 | for (; bio != NULL; bio = next_bio) { |
| 56 | if (!CRYPTO_refcount_dec_and_test_zero(&bio->references)) { |
| 57 | return 0; |
| 58 | } |
| 59 |
| 60 | next_bio = BIO_pop(bio); |
| 61 |
| 62 | if (bio->method != NULL && bio->method->destroy != NULL) { |
| 63 | bio->method->destroy(bio); |
| 64 | } |
| 65 |
| 66 | CRYPTO_free_ex_data(&g_ex_data_class, bio, &bio->ex_data); |
| 67 | OPENSSL_free(bio); |
| 68 | } |
| 69 | return 1; |
| 70 | } |
| 71 |
| 72 | int BIO_up_ref(BIO *bio) { |
| 73 | CRYPTO_refcount_inc(&bio->references); |
| 74 | return 1; |
| 75 | } |
| 76 |
| 77 | void BIO_vfree(BIO *bio) { BIO_free(bio); } |
| 78 |
| 79 | void BIO_free_all(BIO *bio) { BIO_free(bio); } |
| 80 |
| 81 | int BIO_read(BIO *bio, void *buf, int len) { |
| 82 | if (bio == NULL || bio->method == NULL || bio->method->bread == NULL) { |
| 83 | OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD); |
| 84 | return -2; |
| 85 | } |
| 86 | if (!bio->init) { |
| 87 | OPENSSL_PUT_ERROR(BIO, BIO_R_UNINITIALIZED); |
| 88 | return -2; |
| 89 | } |
| 90 | if (len <= 0) { |
| 91 | return 0; |
| 92 | } |
| 93 | int ret = bio->method->bread(bio, reinterpret_cast<char *>(buf), len); |
| 94 | if (ret > 0) { |
| 95 | bio->num_read += ret; |
| 96 | } |
| 97 | return ret; |
| 98 | } |
| 99 |
| 100 | int BIO_gets(BIO *bio, char *buf, int len) { |
| 101 | if (bio == NULL || bio->method == NULL || bio->method->bgets == NULL) { |
| 102 | OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD); |
| 103 | return -2; |
| 104 | } |
| 105 | if (!bio->init) { |
| 106 | OPENSSL_PUT_ERROR(BIO, BIO_R_UNINITIALIZED); |
| 107 | return -2; |
| 108 | } |
| 109 | if (len <= 0) { |
| 110 | return 0; |
| 111 | } |
| 112 | int ret = bio->method->bgets(bio, buf, len); |
| 113 | if (ret > 0) { |
| 114 | bio->num_read += ret; |
| 115 | } |
| 116 | return ret; |
| 117 | } |
| 118 |
| 119 | int BIO_write(BIO *bio, const void *in, int inl) { |
| 120 | if (bio == NULL || bio->method == NULL || bio->method->bwrite == NULL) { |
| 121 | OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD); |
| 122 | return -2; |
| 123 | } |
| 124 | if (!bio->init) { |
| 125 | OPENSSL_PUT_ERROR(BIO, BIO_R_UNINITIALIZED); |
| 126 | return -2; |
| 127 | } |
| 128 | if (inl <= 0) { |
| 129 | return 0; |
| 130 | } |
| 131 | int ret = bio->method->bwrite(bio, reinterpret_cast<const char *>(in), inl); |
| 132 | if (ret > 0) { |
| 133 | bio->num_write += ret; |
| 134 | } |
| 135 | return ret; |
| 136 | } |
| 137 |
| 138 | int BIO_write_all(BIO *bio, const void *data, size_t len) { |
| 139 | const uint8_t *data_u8 = reinterpret_cast<const uint8_t *>(data); |
| 140 | while (len > 0) { |
| 141 | int ret = BIO_write(bio, data_u8, len > INT_MAX ? INT_MAX : (int)len); |
| 142 | if (ret <= 0) { |
| 143 | return 0; |
| 144 | } |
| 145 | data_u8 += ret; |
| 146 | len -= ret; |
| 147 | } |
| 148 | return 1; |
| 149 | } |
| 150 |
| 151 | int BIO_puts(BIO *bio, const char *in) { |
| 152 | size_t len = strlen(in); |
| 153 | if (len > INT_MAX) { |
| 154 | // |BIO_write| and the return value both assume the string fits in |int|. |
| 155 | OPENSSL_PUT_ERROR(BIO, ERR_R_OVERFLOW); |
| 156 | return -1; |
| 157 | } |
| 158 | return BIO_write(bio, in, (int)len); |
| 159 | } |
| 160 |
| 161 | int BIO_flush(BIO *bio) { return (int)BIO_ctrl(bio, BIO_CTRL_FLUSH, 0, NULL); } |
| 162 |
| 163 | long BIO_ctrl(BIO *bio, int cmd, long larg, void *parg) { |
| 164 | if (bio == NULL) { |
| 165 | return 0; |
| 166 | } |
| 167 |
| 168 | if (bio->method == NULL || bio->method->ctrl == NULL) { |
| 169 | OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD); |
| 170 | return -2; |
| 171 | } |
| 172 |
| 173 | return bio->method->ctrl(bio, cmd, larg, parg); |
| 174 | } |
| 175 |
| 176 | char *BIO_ptr_ctrl(BIO *b, int cmd, long larg) { |
| 177 | char *p = NULL; |
| 178 |
| 179 | if (BIO_ctrl(b, cmd, larg, (void *)&p) <= 0) { |
| 180 | return NULL; |
| 181 | } |
| 182 |
| 183 | return p; |
| 184 | } |
| 185 |
| 186 | long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg) { |
| 187 | int i = iarg; |
| 188 |
| 189 | return BIO_ctrl(b, cmd, larg, (void *)&i); |
| 190 | } |
| 191 |
| 192 | int BIO_reset(BIO *bio) { return (int)BIO_ctrl(bio, BIO_CTRL_RESET, 0, NULL); } |
| 193 |
| 194 | int BIO_eof(BIO *bio) { return (int)BIO_ctrl(bio, BIO_CTRL_EOF, 0, NULL); } |
| 195 |
| 196 | void BIO_set_flags(BIO *bio, int flags) { bio->flags |= flags; } |
| 197 |
| 198 | int BIO_test_flags(const BIO *bio, int flags) { return bio->flags & flags; } |
| 199 |
| 200 | int BIO_should_read(const BIO *bio) { |
| 201 | return BIO_test_flags(bio, BIO_FLAGS_READ); |
| 202 | } |
| 203 |
| 204 | int BIO_should_write(const BIO *bio) { |
| 205 | return BIO_test_flags(bio, BIO_FLAGS_WRITE); |
| 206 | } |
| 207 |
| 208 | int BIO_should_retry(const BIO *bio) { |
| 209 | return BIO_test_flags(bio, BIO_FLAGS_SHOULD_RETRY); |
| 210 | } |
| 211 |
| 212 | int BIO_should_io_special(const BIO *bio) { |
| 213 | return BIO_test_flags(bio, BIO_FLAGS_IO_SPECIAL); |
| 214 | } |
| 215 |
| 216 | int BIO_get_retry_reason(const BIO *bio) { return bio->retry_reason; } |
| 217 |
| 218 | void BIO_set_retry_reason(BIO *bio, int reason) { bio->retry_reason = reason; } |
| 219 |
| 220 | void BIO_clear_flags(BIO *bio, int flags) { bio->flags &= ~flags; } |
| 221 |
| 222 | void BIO_set_retry_read(BIO *bio) { |
| 223 | bio->flags |= BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY; |
| 224 | } |
| 225 |
| 226 | void BIO_set_retry_write(BIO *bio) { |
| 227 | bio->flags |= BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY; |
| 228 | } |
| 229 |
| 230 | static const int kRetryFlags = BIO_FLAGS_RWS | BIO_FLAGS_SHOULD_RETRY; |
| 231 |
| 232 | int BIO_get_retry_flags(BIO *bio) { return bio->flags & kRetryFlags; } |
| 233 |
| 234 | void BIO_clear_retry_flags(BIO *bio) { |
| 235 | bio->flags &= ~kRetryFlags; |
| 236 | bio->retry_reason = 0; |
| 237 | } |
| 238 |
| 239 | int BIO_method_type(const BIO *bio) { return bio->method->type; } |
| 240 |
| 241 | void BIO_copy_next_retry(BIO *bio) { |
| 242 | BIO_clear_retry_flags(bio); |
| 243 | BIO_set_flags(bio, BIO_get_retry_flags(bio->next_bio)); |
| 244 | bio->retry_reason = bio->next_bio->retry_reason; |
| 245 | } |
| 246 |
| 247 | long BIO_callback_ctrl(BIO *bio, int cmd, bio_info_cb fp) { |
| 248 | if (bio == NULL) { |
| 249 | return 0; |
| 250 | } |
| 251 |
| 252 | if (bio->method == NULL || bio->method->callback_ctrl == NULL) { |
| 253 | OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD); |
| 254 | return 0; |
| 255 | } |
| 256 |
| 257 | return bio->method->callback_ctrl(bio, cmd, fp); |
| 258 | } |
| 259 |
| 260 | size_t BIO_pending(const BIO *bio) { |
| 261 | const long r = BIO_ctrl((BIO *)bio, BIO_CTRL_PENDING, 0, NULL); |
| 262 | assert(r >= 0); |
| 263 |
| 264 | if (r < 0) { |
| 265 | return 0; |
| 266 | } |
| 267 | return r; |
| 268 | } |
| 269 |
| 270 | size_t BIO_ctrl_pending(const BIO *bio) { return BIO_pending(bio); } |
| 271 |
| 272 | size_t BIO_wpending(const BIO *bio) { |
| 273 | const long r = BIO_ctrl((BIO *)bio, BIO_CTRL_WPENDING, 0, NULL); |
| 274 | assert(r >= 0); |
| 275 |
| 276 | if (r < 0) { |
| 277 | return 0; |
| 278 | } |
| 279 | return r; |
| 280 | } |
| 281 |
| 282 | int BIO_set_close(BIO *bio, int close_flag) { |
| 283 | return (int)BIO_ctrl(bio, BIO_CTRL_SET_CLOSE, close_flag, NULL); |
| 284 | } |
| 285 |
| 286 | OPENSSL_EXPORT uint64_t BIO_number_read(const BIO *bio) { |
| 287 | return bio->num_read; |
| 288 | } |
| 289 |
| 290 | OPENSSL_EXPORT uint64_t BIO_number_written(const BIO *bio) { |
| 291 | return bio->num_write; |
| 292 | } |
| 293 |
| 294 | BIO *BIO_push(BIO *bio, BIO *appended_bio) { |
| 295 | BIO *last_bio; |
| 296 |
| 297 | if (bio == NULL) { |
| 298 | return bio; |
| 299 | } |
| 300 |
| 301 | last_bio = bio; |
| 302 | while (last_bio->next_bio != NULL) { |
| 303 | last_bio = last_bio->next_bio; |
| 304 | } |
| 305 |
| 306 | last_bio->next_bio = appended_bio; |
| 307 | return bio; |
| 308 | } |
| 309 |
| 310 | BIO *BIO_pop(BIO *bio) { |
| 311 | BIO *ret; |
| 312 |
| 313 | if (bio == NULL) { |
| 314 | return NULL; |
| 315 | } |
| 316 | ret = bio->next_bio; |
| 317 | bio->next_bio = NULL; |
| 318 | return ret; |
| 319 | } |
| 320 |
| 321 | BIO *BIO_next(BIO *bio) { |
| 322 | if (!bio) { |
| 323 | return NULL; |
| 324 | } |
| 325 | return bio->next_bio; |
| 326 | } |
| 327 |
| 328 | BIO *BIO_find_type(BIO *bio, int type) { |
| 329 | int method_type, mask; |
| 330 |
| 331 | if (!bio) { |
| 332 | return NULL; |
| 333 | } |
| 334 | mask = type & 0xff; |
| 335 |
| 336 | do { |
| 337 | if (bio->method != NULL) { |
| 338 | method_type = bio->method->type; |
| 339 |
| 340 | if (!mask) { |
| 341 | if (method_type & type) { |
| 342 | return bio; |
| 343 | } |
| 344 | } else if (method_type == type) { |
| 345 | return bio; |
| 346 | } |
| 347 | } |
| 348 | bio = bio->next_bio; |
| 349 | } while (bio != NULL); |
| 350 |
| 351 | return NULL; |
| 352 | } |
| 353 |
| 354 | int BIO_indent(BIO *bio, unsigned indent, unsigned max_indent) { |
| 355 | if (indent > max_indent) { |
| 356 | indent = max_indent; |
| 357 | } |
| 358 |
| 359 | while (indent--) { |
| 360 | if (BIO_puts(bio, " ") != 1) { |
| 361 | return 0; |
| 362 | } |
| 363 | } |
| 364 | return 1; |
| 365 | } |
| 366 |
| 367 | static int print_bio(const char *str, size_t len, void *bio) { |
| 368 | return BIO_write_all((BIO *)bio, str, len); |
| 369 | } |
| 370 |
| 371 | void ERR_print_errors(BIO *bio) { ERR_print_errors_cb(print_bio, bio); } |
| 372 |
| 373 | // bio_read_all reads everything from |bio| and prepends |prefix| to it. On |
| 374 | // success, |*out| is set to an allocated buffer (which should be freed with |
| 375 | // |OPENSSL_free|), |*out_len| is set to its length and one is returned. The |
| 376 | // buffer will contain |prefix| followed by the contents of |bio|. On failure, |
| 377 | // zero is returned. |
| 378 | // |
| 379 | // The function will fail if the size of the output would equal or exceed |
| 380 | // |max_len|. |
| 381 | static int bio_read_all(BIO *bio, uint8_t **out, size_t *out_len, |
| 382 | const uint8_t *prefix, size_t prefix_len, |
| 383 | size_t max_len) { |
| 384 | static const size_t kChunkSize = 4096; |
| 385 |
| 386 | size_t len = prefix_len + kChunkSize; |
| 387 | if (len > max_len) { |
| 388 | len = max_len; |
| 389 | } |
| 390 | if (len < prefix_len) { |
| 391 | return 0; |
| 392 | } |
| 393 | *out = reinterpret_cast<uint8_t *>(OPENSSL_malloc(len)); |
| 394 | if (*out == NULL) { |
| 395 | return 0; |
| 396 | } |
| 397 | OPENSSL_memcpy(*out, prefix, prefix_len); |
| 398 | size_t done = prefix_len; |
| 399 |
| 400 | for (;;) { |
| 401 | if (done == len) { |
| 402 | OPENSSL_free(*out); |
| 403 | return 0; |
| 404 | } |
| 405 | size_t todo = len - done; |
| 406 | if (todo > INT_MAX) { |
| 407 | todo = INT_MAX; |
| 408 | } |
| 409 | const int n = BIO_read(bio, *out + done, (int)todo); |
| 410 | if (n == 0) { |
| 411 | *out_len = done; |
| 412 | return 1; |
| 413 | } else if (n == -1) { |
| 414 | OPENSSL_free(*out); |
| 415 | return 0; |
| 416 | } |
| 417 |
| 418 | done += n; |
| 419 | if (len < max_len && len - done < kChunkSize / 2) { |
| 420 | len += kChunkSize; |
| 421 | if (len < kChunkSize || len > max_len) { |
| 422 | len = max_len; |
| 423 | } |
| 424 | uint8_t *new_buf = |
| 425 | reinterpret_cast<uint8_t *>(OPENSSL_realloc(*out, len)); |
| 426 | if (new_buf == NULL) { |
| 427 | OPENSSL_free(*out); |
| 428 | return 0; |
| 429 | } |
| 430 | *out = new_buf; |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 |
| 435 | // bio_read_full reads |len| bytes |bio| and writes them into |out|. It |
| 436 | // tolerates partial reads from |bio| and returns one on success or zero if a |
| 437 | // read fails before |len| bytes are read. On failure, it additionally sets |
| 438 | // |*out_eof_on_first_read| to whether the error was due to |bio| returning zero |
| 439 | // on the first read. |out_eof_on_first_read| may be NULL to discard the value. |
| 440 | static int bio_read_full(BIO *bio, uint8_t *out, int *out_eof_on_first_read, |
| 441 | size_t len) { |
| 442 | int first_read = 1; |
| 443 | while (len > 0) { |
| 444 | int todo = len <= INT_MAX ? (int)len : INT_MAX; |
| 445 | int ret = BIO_read(bio, out, todo); |
| 446 | if (ret <= 0) { |
| 447 | if (out_eof_on_first_read != NULL) { |
| 448 | *out_eof_on_first_read = first_read && ret == 0; |
| 449 | } |
| 450 | return 0; |
| 451 | } |
| 452 | out += ret; |
| 453 | len -= (size_t)ret; |
| 454 | first_read = 0; |
| 455 | } |
| 456 |
| 457 | return 1; |
| 458 | } |
| 459 |
| 460 | // For compatibility with existing |d2i_*_bio| callers, |BIO_read_asn1| uses |
| 461 | // |ERR_LIB_ASN1| errors. |
| 462 | OPENSSL_DECLARE_ERROR_REASON(ASN1, ASN1_R_DECODE_ERROR) |
| 463 | OPENSSL_DECLARE_ERROR_REASON(ASN1, ASN1_R_HEADER_TOO_LONG) |
| 464 | OPENSSL_DECLARE_ERROR_REASON(ASN1, ASN1_R_NOT_ENOUGH_DATA) |
| 465 | OPENSSL_DECLARE_ERROR_REASON(ASN1, ASN1_R_TOO_LONG) |
| 466 |
| 467 | int BIO_read_asn1(BIO *bio, uint8_t **out, size_t *out_len, size_t max_len) { |
| 468 | uint8_t header[6]; |
| 469 |
| 470 | static const size_t kInitialHeaderLen = 2; |
| 471 | int eof_on_first_read; |
| 472 | if (!bio_read_full(bio, header, &eof_on_first_read, kInitialHeaderLen)) { |
| 473 | if (eof_on_first_read) { |
| 474 | // Historically, OpenSSL returned |ASN1_R_HEADER_TOO_LONG| when |
| 475 | // |d2i_*_bio| could not read anything. CPython conditions on this to |
| 476 | // determine if |bio| was empty. |
| 477 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG); |
| 478 | } else { |
| 479 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ENOUGH_DATA); |
| 480 | } |
| 481 | return 0; |
| 482 | } |
| 483 |
| 484 | const uint8_t tag = header[0]; |
| 485 | const uint8_t length_byte = header[1]; |
| 486 |
| 487 | if ((tag & 0x1f) == 0x1f) { |
| 488 | // Long form tags are not supported. |
| 489 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR); |
| 490 | return 0; |
| 491 | } |
| 492 |
| 493 | size_t len, header_len; |
| 494 | if ((length_byte & 0x80) == 0) { |
| 495 | // Short form length. |
| 496 | len = length_byte; |
| 497 | header_len = kInitialHeaderLen; |
| 498 | } else { |
| 499 | const size_t num_bytes = length_byte & 0x7f; |
| 500 |
| 501 | if ((tag & 0x20 /* constructed */) != 0 && num_bytes == 0) { |
| 502 | // indefinite length. |
| 503 | if (!bio_read_all(bio, out, out_len, header, kInitialHeaderLen, |
| 504 | max_len)) { |
| 505 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ENOUGH_DATA); |
| 506 | return 0; |
| 507 | } |
| 508 | return 1; |
| 509 | } |
| 510 |
| 511 | if (num_bytes == 0 || num_bytes > 4) { |
| 512 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR); |
| 513 | return 0; |
| 514 | } |
| 515 |
| 516 | if (!bio_read_full(bio, header + kInitialHeaderLen, NULL, num_bytes)) { |
| 517 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ENOUGH_DATA); |
| 518 | return 0; |
| 519 | } |
| 520 | header_len = kInitialHeaderLen + num_bytes; |
| 521 |
| 522 | uint32_t len32 = 0; |
| 523 | for (unsigned i = 0; i < num_bytes; i++) { |
| 524 | len32 <<= 8; |
| 525 | len32 |= header[kInitialHeaderLen + i]; |
| 526 | } |
| 527 |
| 528 | if (len32 < 128) { |
| 529 | // Length should have used short-form encoding. |
| 530 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR); |
| 531 | return 0; |
| 532 | } |
| 533 |
| 534 | if ((len32 >> ((num_bytes - 1) * 8)) == 0) { |
| 535 | // Length should have been at least one byte shorter. |
| 536 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR); |
| 537 | return 0; |
| 538 | } |
| 539 |
| 540 | len = len32; |
| 541 | } |
| 542 |
| 543 | if (len + header_len < len || len + header_len > max_len || len > INT_MAX) { |
| 544 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG); |
| 545 | return 0; |
| 546 | } |
| 547 | len += header_len; |
| 548 | *out_len = len; |
| 549 |
| 550 | *out = reinterpret_cast<uint8_t *>(OPENSSL_malloc(len)); |
| 551 | if (*out == NULL) { |
| 552 | return 0; |
| 553 | } |
| 554 | OPENSSL_memcpy(*out, header, header_len); |
| 555 | if (!bio_read_full(bio, (*out) + header_len, NULL, len - header_len)) { |
| 556 | OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ENOUGH_DATA); |
| 557 | OPENSSL_free(*out); |
| 558 | return 0; |
| 559 | } |
| 560 |
| 561 | return 1; |
| 562 | } |
| 563 |
| 564 | void BIO_set_retry_special(BIO *bio) { |
| 565 | bio->flags |= BIO_FLAGS_READ | BIO_FLAGS_IO_SPECIAL; |
| 566 | } |
| 567 |
| 568 | int BIO_set_write_buffer_size(BIO *bio, int buffer_size) { return 0; } |
| 569 |
| 570 | static CRYPTO_MUTEX g_index_lock = CRYPTO_MUTEX_INIT; |
| 571 | static int g_index = BIO_TYPE_START; |
| 572 |
| 573 | int BIO_get_new_index(void) { |
| 574 | CRYPTO_MUTEX_lock_write(&g_index_lock); |
| 575 | // If |g_index| exceeds 255, it will collide with the flags bits. |
| 576 | int ret = g_index > 255 ? -1 : g_index++; |
| 577 | CRYPTO_MUTEX_unlock_write(&g_index_lock); |
| 578 | return ret; |
| 579 | } |
| 580 |
| 581 | BIO_METHOD *BIO_meth_new(int type, const char *name) { |
| 582 | BIO_METHOD *method = |
| 583 | reinterpret_cast<BIO_METHOD *>(OPENSSL_zalloc(sizeof(BIO_METHOD))); |
| 584 | if (method == NULL) { |
| 585 | return NULL; |
| 586 | } |
| 587 | method->type = type; |
| 588 | method->name = name; |
| 589 | return method; |
| 590 | } |
| 591 |
| 592 | void BIO_meth_free(BIO_METHOD *method) { OPENSSL_free(method); } |
| 593 |
| 594 | int BIO_meth_set_create(BIO_METHOD *method, int (*create_func)(BIO *)) { |
| 595 | method->create = create_func; |
| 596 | return 1; |
| 597 | } |
| 598 |
| 599 | int BIO_meth_set_destroy(BIO_METHOD *method, int (*destroy_func)(BIO *)) { |
| 600 | method->destroy = destroy_func; |
| 601 | return 1; |
| 602 | } |
| 603 |
| 604 | int BIO_meth_set_write(BIO_METHOD *method, |
| 605 | int (*write_func)(BIO *, const char *, int)) { |
| 606 | method->bwrite = write_func; |
| 607 | return 1; |
| 608 | } |
| 609 |
| 610 | int BIO_meth_set_read(BIO_METHOD *method, |
| 611 | int (*read_func)(BIO *, char *, int)) { |
| 612 | method->bread = read_func; |
| 613 | return 1; |
| 614 | } |
| 615 |
| 616 | int BIO_meth_set_gets(BIO_METHOD *method, |
| 617 | int (*gets_func)(BIO *, char *, int)) { |
| 618 | method->bgets = gets_func; |
| 619 | return 1; |
| 620 | } |
| 621 |
| 622 | int BIO_meth_set_ctrl(BIO_METHOD *method, |
| 623 | long (*ctrl_func)(BIO *, int, long, void *)) { |
| 624 | method->ctrl = ctrl_func; |
| 625 | return 1; |
| 626 | } |
| 627 |
| 628 | void BIO_set_data(BIO *bio, void *ptr) { bio->ptr = ptr; } |
| 629 |
| 630 | void *BIO_get_data(BIO *bio) { return bio->ptr; } |
| 631 |
| 632 | void BIO_set_init(BIO *bio, int init) { bio->init = init; } |
| 633 |
| 634 | int BIO_get_init(BIO *bio) { return bio->init; } |
| 635 |
| 636 | void BIO_set_shutdown(BIO *bio, int shutdown) { bio->shutdown = shutdown; } |
| 637 |
| 638 | int BIO_get_shutdown(BIO *bio) { return bio->shutdown; } |
| 639 |
| 640 | int BIO_meth_set_puts(BIO_METHOD *method, int (*puts)(BIO *, const char *)) { |
| 641 | // Ignore the parameter. We implement |BIO_puts| using |BIO_write|. |
| 642 | return 1; |
| 643 | } |
| 644 |
| 645 | int BIO_get_ex_new_index(long argl, void *argp, // |
| 646 | CRYPTO_EX_unused *unused, // |
| 647 | CRYPTO_EX_dup *dup_unused, // |
| 648 | CRYPTO_EX_free *free_func) { |
| 649 | return CRYPTO_get_ex_new_index_ex(&g_ex_data_class, argl, argp, free_func); |
| 650 | } |
| 651 |
| 652 | int BIO_set_ex_data(BIO *bio, int idx, void *data) { |
| 653 | return CRYPTO_set_ex_data(&bio->ex_data, idx, data); |
| 654 | } |
| 655 |
| 656 | void *BIO_get_ex_data(const BIO *bio, int idx) { |
| 657 | return CRYPTO_get_ex_data(&bio->ex_data, idx); |
| 658 | } |
| 659 |