1 /* Copyright (C) 1995-1998 Eric Young (eay (at) cryptsoft.com) 2 * All rights reserved. 3 * 4 * This package is an SSL implementation written 5 * by Eric Young (eay (at) cryptsoft.com). 6 * The implementation was written so as to conform with Netscapes SSL. 7 * 8 * This library is free for commercial and non-commercial use as long as 9 * the following conditions are aheared to. The following conditions 10 * apply to all code found in this distribution, be it the RC4, RSA, 11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 12 * included with this distribution is covered by the same copyright terms 13 * except that the holder is Tim Hudson (tjh (at) cryptsoft.com). 14 * 15 * Copyright remains Eric Young's, and as such any Copyright notices in 16 * the code are not to be removed. 17 * If this package is used in a product, Eric Young should be given attribution 18 * as the author of the parts of the library used. 19 * This can be in the form of a textual message at program startup or 20 * in documentation (online or textual) provided with the package. 21 * 22 * Redistribution and use in source and binary forms, with or without 23 * modification, are permitted provided that the following conditions 24 * are met: 25 * 1. Redistributions of source code must retain the copyright 26 * notice, this list of conditions and the following disclaimer. 27 * 2. Redistributions in binary form must reproduce the above copyright 28 * notice, this list of conditions and the following disclaimer in the 29 * documentation and/or other materials provided with the distribution. 30 * 3. All advertising materials mentioning features or use of this software 31 * must display the following acknowledgement: 32 * "This product includes cryptographic software written by 33 * Eric Young (eay (at) cryptsoft.com)" 34 * The word 'cryptographic' can be left out if the rouines from the library 35 * being used are not cryptographic related :-). 36 * 4. If you include any Windows specific code (or a derivative thereof) from 37 * the apps directory (application code) you must include an acknowledgement: 38 * "This product includes software written by Tim Hudson (tjh (at) cryptsoft.com)" 39 * 40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 50 * SUCH DAMAGE. 51 * 52 * The licence and distribution terms for any publically available version or 53 * derivative of this code cannot be changed. i.e. this code cannot simply be 54 * copied and put under another distribution licence 55 * [including the GNU Public Licence.] */ 56 57 #include <openssl/bio.h> 58 59 #include <assert.h> 60 #include <errno.h> 61 #include <limits.h> 62 #include <string.h> 63 64 #include <openssl/err.h> 65 #include <openssl/mem.h> 66 #include <openssl/thread.h> 67 68 #include "../internal.h" 69 70 71 BIO *BIO_new(const BIO_METHOD *method) { 72 BIO *ret = OPENSSL_malloc(sizeof(BIO)); 73 if (ret == NULL) { 74 OPENSSL_PUT_ERROR(BIO, ERR_R_MALLOC_FAILURE); 75 return NULL; 76 } 77 78 OPENSSL_memset(ret, 0, sizeof(BIO)); 79 ret->method = method; 80 ret->shutdown = 1; 81 ret->references = 1; 82 83 if (method->create != NULL && !method->create(ret)) { 84 OPENSSL_free(ret); 85 return NULL; 86 } 87 88 return ret; 89 } 90 91 int BIO_free(BIO *bio) { 92 BIO *next_bio; 93 94 for (; bio != NULL; bio = next_bio) { 95 if (!CRYPTO_refcount_dec_and_test_zero(&bio->references)) { 96 return 0; 97 } 98 99 next_bio = BIO_pop(bio); 100 101 if (bio->method != NULL && bio->method->destroy != NULL) { 102 bio->method->destroy(bio); 103 } 104 105 OPENSSL_free(bio); 106 } 107 return 1; 108 } 109 110 int BIO_up_ref(BIO *bio) { 111 CRYPTO_refcount_inc(&bio->references); 112 return 1; 113 } 114 115 void BIO_vfree(BIO *bio) { 116 BIO_free(bio); 117 } 118 119 void BIO_free_all(BIO *bio) { 120 BIO_free(bio); 121 } 122 123 int BIO_read(BIO *bio, void *buf, int len) { 124 if (bio == NULL || bio->method == NULL || bio->method->bread == NULL) { 125 OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD); 126 return -2; 127 } 128 if (!bio->init) { 129 OPENSSL_PUT_ERROR(BIO, BIO_R_UNINITIALIZED); 130 return -2; 131 } 132 if (len <= 0) { 133 return 0; 134 } 135 int ret = bio->method->bread(bio, buf, len); 136 if (ret > 0) { 137 bio->num_read += ret; 138 } 139 return ret; 140 } 141 142 int BIO_gets(BIO *bio, char *buf, int len) { 143 if (bio == NULL || bio->method == NULL || bio->method->bgets == NULL) { 144 OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD); 145 return -2; 146 } 147 if (!bio->init) { 148 OPENSSL_PUT_ERROR(BIO, BIO_R_UNINITIALIZED); 149 return -2; 150 } 151 if (len <= 0) { 152 return 0; 153 } 154 int ret = bio->method->bgets(bio, buf, len); 155 if (ret > 0) { 156 bio->num_read += ret; 157 } 158 return ret; 159 } 160 161 int BIO_write(BIO *bio, const void *in, int inl) { 162 if (bio == NULL || bio->method == NULL || bio->method->bwrite == NULL) { 163 OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD); 164 return -2; 165 } 166 if (!bio->init) { 167 OPENSSL_PUT_ERROR(BIO, BIO_R_UNINITIALIZED); 168 return -2; 169 } 170 if (inl <= 0) { 171 return 0; 172 } 173 int ret = bio->method->bwrite(bio, in, inl); 174 if (ret > 0) { 175 bio->num_write += ret; 176 } 177 return ret; 178 } 179 180 int BIO_puts(BIO *bio, const char *in) { 181 return BIO_write(bio, in, strlen(in)); 182 } 183 184 int BIO_flush(BIO *bio) { 185 return BIO_ctrl(bio, BIO_CTRL_FLUSH, 0, NULL); 186 } 187 188 long BIO_ctrl(BIO *bio, int cmd, long larg, void *parg) { 189 if (bio == NULL) { 190 return 0; 191 } 192 193 if (bio->method == NULL || bio->method->ctrl == NULL) { 194 OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD); 195 return -2; 196 } 197 198 return bio->method->ctrl(bio, cmd, larg, parg); 199 } 200 201 char *BIO_ptr_ctrl(BIO *b, int cmd, long larg) { 202 char *p = NULL; 203 204 if (BIO_ctrl(b, cmd, larg, (void *)&p) <= 0) { 205 return NULL; 206 } 207 208 return p; 209 } 210 211 long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg) { 212 int i = iarg; 213 214 return BIO_ctrl(b, cmd, larg, (void *)&i); 215 } 216 217 int BIO_reset(BIO *bio) { 218 return BIO_ctrl(bio, BIO_CTRL_RESET, 0, NULL); 219 } 220 221 int BIO_eof(BIO *bio) { 222 return BIO_ctrl(bio, BIO_CTRL_EOF, 0, NULL); 223 } 224 225 void BIO_set_flags(BIO *bio, int flags) { 226 bio->flags |= flags; 227 } 228 229 int BIO_test_flags(const BIO *bio, int flags) { 230 return bio->flags & flags; 231 } 232 233 int BIO_should_read(const BIO *bio) { 234 return BIO_test_flags(bio, BIO_FLAGS_READ); 235 } 236 237 int BIO_should_write(const BIO *bio) { 238 return BIO_test_flags(bio, BIO_FLAGS_WRITE); 239 } 240 241 int BIO_should_retry(const BIO *bio) { 242 return BIO_test_flags(bio, BIO_FLAGS_SHOULD_RETRY); 243 } 244 245 int BIO_should_io_special(const BIO *bio) { 246 return BIO_test_flags(bio, BIO_FLAGS_IO_SPECIAL); 247 } 248 249 int BIO_get_retry_reason(const BIO *bio) { return bio->retry_reason; } 250 251 void BIO_clear_flags(BIO *bio, int flags) { 252 bio->flags &= ~flags; 253 } 254 255 void BIO_set_retry_read(BIO *bio) { 256 bio->flags |= BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY; 257 } 258 259 void BIO_set_retry_write(BIO *bio) { 260 bio->flags |= BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY; 261 } 262 263 static const int kRetryFlags = BIO_FLAGS_RWS | BIO_FLAGS_SHOULD_RETRY; 264 265 int BIO_get_retry_flags(BIO *bio) { 266 return bio->flags & kRetryFlags; 267 } 268 269 void BIO_clear_retry_flags(BIO *bio) { 270 bio->flags &= ~kRetryFlags; 271 bio->retry_reason = 0; 272 } 273 274 int BIO_method_type(const BIO *bio) { return bio->method->type; } 275 276 void BIO_copy_next_retry(BIO *bio) { 277 BIO_clear_retry_flags(bio); 278 BIO_set_flags(bio, BIO_get_retry_flags(bio->next_bio)); 279 bio->retry_reason = bio->next_bio->retry_reason; 280 } 281 282 long BIO_callback_ctrl(BIO *bio, int cmd, bio_info_cb fp) { 283 if (bio == NULL) { 284 return 0; 285 } 286 287 if (bio->method == NULL || bio->method->callback_ctrl == NULL) { 288 OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD); 289 return 0; 290 } 291 292 return bio->method->callback_ctrl(bio, cmd, fp); 293 } 294 295 size_t BIO_pending(const BIO *bio) { 296 const long r = BIO_ctrl((BIO *) bio, BIO_CTRL_PENDING, 0, NULL); 297 assert(r >= 0); 298 299 if (r < 0) { 300 return 0; 301 } 302 return r; 303 } 304 305 size_t BIO_ctrl_pending(const BIO *bio) { 306 return BIO_pending(bio); 307 } 308 309 size_t BIO_wpending(const BIO *bio) { 310 const long r = BIO_ctrl((BIO *) bio, BIO_CTRL_WPENDING, 0, NULL); 311 assert(r >= 0); 312 313 if (r < 0) { 314 return 0; 315 } 316 return r; 317 } 318 319 int BIO_set_close(BIO *bio, int close_flag) { 320 return BIO_ctrl(bio, BIO_CTRL_SET_CLOSE, close_flag, NULL); 321 } 322 323 OPENSSL_EXPORT size_t BIO_number_read(const BIO *bio) { 324 return bio->num_read; 325 } 326 327 OPENSSL_EXPORT size_t BIO_number_written(const BIO *bio) { 328 return bio->num_write; 329 } 330 331 BIO *BIO_push(BIO *bio, BIO *appended_bio) { 332 BIO *last_bio; 333 334 if (bio == NULL) { 335 return bio; 336 } 337 338 last_bio = bio; 339 while (last_bio->next_bio != NULL) { 340 last_bio = last_bio->next_bio; 341 } 342 343 last_bio->next_bio = appended_bio; 344 return bio; 345 } 346 347 BIO *BIO_pop(BIO *bio) { 348 BIO *ret; 349 350 if (bio == NULL) { 351 return NULL; 352 } 353 ret = bio->next_bio; 354 bio->next_bio = NULL; 355 return ret; 356 } 357 358 BIO *BIO_next(BIO *bio) { 359 if (!bio) { 360 return NULL; 361 } 362 return bio->next_bio; 363 } 364 365 BIO *BIO_find_type(BIO *bio, int type) { 366 int method_type, mask; 367 368 if (!bio) { 369 return NULL; 370 } 371 mask = type & 0xff; 372 373 do { 374 if (bio->method != NULL) { 375 method_type = bio->method->type; 376 377 if (!mask) { 378 if (method_type & type) { 379 return bio; 380 } 381 } else if (method_type == type) { 382 return bio; 383 } 384 } 385 bio = bio->next_bio; 386 } while (bio != NULL); 387 388 return NULL; 389 } 390 391 int BIO_indent(BIO *bio, unsigned indent, unsigned max_indent) { 392 if (indent > max_indent) { 393 indent = max_indent; 394 } 395 396 while (indent--) { 397 if (BIO_puts(bio, " ") != 1) { 398 return 0; 399 } 400 } 401 return 1; 402 } 403 404 static int print_bio(const char *str, size_t len, void *bio) { 405 return BIO_write((BIO *)bio, str, len); 406 } 407 408 void ERR_print_errors(BIO *bio) { 409 ERR_print_errors_cb(print_bio, bio); 410 } 411 412 // bio_read_all reads everything from |bio| and prepends |prefix| to it. On 413 // success, |*out| is set to an allocated buffer (which should be freed with 414 // |OPENSSL_free|), |*out_len| is set to its length and one is returned. The 415 // buffer will contain |prefix| followed by the contents of |bio|. On failure, 416 // zero is returned. 417 // 418 // The function will fail if the size of the output would equal or exceed 419 // |max_len|. 420 static int bio_read_all(BIO *bio, uint8_t **out, size_t *out_len, 421 const uint8_t *prefix, size_t prefix_len, 422 size_t max_len) { 423 static const size_t kChunkSize = 4096; 424 425 size_t len = prefix_len + kChunkSize; 426 if (len > max_len) { 427 len = max_len; 428 } 429 if (len < prefix_len) { 430 return 0; 431 } 432 *out = OPENSSL_malloc(len); 433 if (*out == NULL) { 434 return 0; 435 } 436 OPENSSL_memcpy(*out, prefix, prefix_len); 437 size_t done = prefix_len; 438 439 for (;;) { 440 if (done == len) { 441 OPENSSL_free(*out); 442 return 0; 443 } 444 const size_t todo = len - done; 445 assert(todo < INT_MAX); 446 const int n = BIO_read(bio, *out + done, todo); 447 if (n == 0) { 448 *out_len = done; 449 return 1; 450 } else if (n == -1) { 451 OPENSSL_free(*out); 452 return 0; 453 } 454 455 done += n; 456 if (len < max_len && len - done < kChunkSize / 2) { 457 len += kChunkSize; 458 if (len < kChunkSize || len > max_len) { 459 len = max_len; 460 } 461 uint8_t *new_buf = OPENSSL_realloc(*out, len); 462 if (new_buf == NULL) { 463 OPENSSL_free(*out); 464 return 0; 465 } 466 *out = new_buf; 467 } 468 } 469 } 470 471 int BIO_read_asn1(BIO *bio, uint8_t **out, size_t *out_len, size_t max_len) { 472 uint8_t header[6]; 473 474 static const size_t kInitialHeaderLen = 2; 475 if (BIO_read(bio, header, kInitialHeaderLen) != (int) kInitialHeaderLen) { 476 return 0; 477 } 478 479 const uint8_t tag = header[0]; 480 const uint8_t length_byte = header[1]; 481 482 if ((tag & 0x1f) == 0x1f) { 483 // Long form tags are not supported. 484 return 0; 485 } 486 487 size_t len, header_len; 488 if ((length_byte & 0x80) == 0) { 489 // Short form length. 490 len = length_byte; 491 header_len = kInitialHeaderLen; 492 } else { 493 const size_t num_bytes = length_byte & 0x7f; 494 495 if ((tag & 0x20 /* constructed */) != 0 && num_bytes == 0) { 496 // indefinite length. 497 return bio_read_all(bio, out, out_len, header, kInitialHeaderLen, 498 max_len); 499 } 500 501 if (num_bytes == 0 || num_bytes > 4) { 502 return 0; 503 } 504 505 if (BIO_read(bio, header + kInitialHeaderLen, num_bytes) != 506 (int)num_bytes) { 507 return 0; 508 } 509 header_len = kInitialHeaderLen + num_bytes; 510 511 uint32_t len32 = 0; 512 unsigned i; 513 for (i = 0; i < num_bytes; i++) { 514 len32 <<= 8; 515 len32 |= header[kInitialHeaderLen + i]; 516 } 517 518 if (len32 < 128) { 519 // Length should have used short-form encoding. 520 return 0; 521 } 522 523 if ((len32 >> ((num_bytes-1)*8)) == 0) { 524 // Length should have been at least one byte shorter. 525 return 0; 526 } 527 528 len = len32; 529 } 530 531 if (len + header_len < len || 532 len + header_len > max_len || 533 len > INT_MAX) { 534 return 0; 535 } 536 len += header_len; 537 *out_len = len; 538 539 *out = OPENSSL_malloc(len); 540 if (*out == NULL) { 541 return 0; 542 } 543 OPENSSL_memcpy(*out, header, header_len); 544 if (BIO_read(bio, (*out) + header_len, len - header_len) != 545 (int) (len - header_len)) { 546 OPENSSL_free(*out); 547 return 0; 548 } 549 550 return 1; 551 } 552 553 void BIO_set_retry_special(BIO *bio) { 554 bio->flags |= BIO_FLAGS_READ | BIO_FLAGS_IO_SPECIAL; 555 } 556 557 int BIO_set_write_buffer_size(BIO *bio, int buffer_size) { return 0; } 558 559 static struct CRYPTO_STATIC_MUTEX g_index_lock = CRYPTO_STATIC_MUTEX_INIT; 560 static int g_index = BIO_TYPE_START; 561 562 int BIO_get_new_index(void) { 563 CRYPTO_STATIC_MUTEX_lock_write(&g_index_lock); 564 // If |g_index| exceeds 255, it will collide with the flags bits. 565 int ret = g_index > 255 ? -1 : g_index++; 566 CRYPTO_STATIC_MUTEX_unlock_write(&g_index_lock); 567 return ret; 568 } 569 570 BIO_METHOD *BIO_meth_new(int type, const char *name) { 571 BIO_METHOD *method = OPENSSL_malloc(sizeof(BIO_METHOD)); 572 if (method == NULL) { 573 return NULL; 574 } 575 OPENSSL_memset(method, 0, sizeof(BIO_METHOD)); 576 method->type = type; 577 method->name = name; 578 return method; 579 } 580 581 void BIO_meth_free(BIO_METHOD *method) { 582 OPENSSL_free(method); 583 } 584 585 int BIO_meth_set_create(BIO_METHOD *method, 586 int (*create)(BIO *)) { 587 method->create = create; 588 return 1; 589 } 590 591 int BIO_meth_set_destroy(BIO_METHOD *method, 592 int (*destroy)(BIO *)) { 593 method->destroy = destroy; 594 return 1; 595 } 596 597 int BIO_meth_set_write(BIO_METHOD *method, 598 int (*write)(BIO *, const char *, int)) { 599 method->bwrite = write; 600 return 1; 601 } 602 603 int BIO_meth_set_read(BIO_METHOD *method, 604 int (*read)(BIO *, char *, int)) { 605 method->bread = read; 606 return 1; 607 } 608 609 int BIO_meth_set_gets(BIO_METHOD *method, 610 int (*gets)(BIO *, char *, int)) { 611 method->bgets = gets; 612 return 1; 613 } 614 615 int BIO_meth_set_ctrl(BIO_METHOD *method, 616 long (*ctrl)(BIO *, int, long, void *)) { 617 method->ctrl = ctrl; 618 return 1; 619 } 620 621 void BIO_set_data(BIO *bio, void *ptr) { bio->ptr = ptr; } 622 623 void *BIO_get_data(BIO *bio) { return bio->ptr; } 624 625 void BIO_set_init(BIO *bio, int init) { bio->init = init; } 626 627 int BIO_get_init(BIO *bio) { return bio->init; } 628 629 void BIO_set_shutdown(BIO *bio, int shutdown) { bio->shutdown = shutdown; } 630 631 int BIO_get_shutdown(BIO *bio) { return bio->shutdown; } 632 633 int BIO_meth_set_puts(BIO_METHOD *method, int (*puts)(BIO *, const char *)) { 634 // Ignore the parameter. We implement |BIO_puts| using |BIO_write|. 635 return 1; 636 } 637