bio.c revision e9ada863a7b3e81f5d2b1e3bdd2305da902a87f5
1/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2 * All rights reserved. 3 * 4 * This package is an SSL implementation written 5 * by Eric Young (eay@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@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@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@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 <errno.h> 60#include <limits.h> 61#include <string.h> 62 63#include <openssl/err.h> 64#include <openssl/mem.h> 65#include <openssl/thread.h> 66 67 68/* BIO_set initialises a BIO structure to have the given type and sets the 69 * reference count to one. It returns one on success or zero on error. */ 70static int bio_set(BIO *bio, const BIO_METHOD *method) { 71 /* This function can be called with a stack allocated |BIO| so we have to 72 * assume that the contents of |BIO| are arbitary. This also means that it'll 73 * leak memory if you call |BIO_set| twice on the same BIO. */ 74 memset(bio, 0, sizeof(BIO)); 75 76 bio->method = method; 77 bio->shutdown = 1; 78 bio->references = 1; 79 80 if (method->create != NULL && !method->create(bio)) { 81 return 0; 82 } 83 84 return 1; 85} 86 87BIO *BIO_new(const BIO_METHOD *method) { 88 BIO *ret = OPENSSL_malloc(sizeof(BIO)); 89 if (ret == NULL) { 90 OPENSSL_PUT_ERROR(BIO, BIO_new, ERR_R_MALLOC_FAILURE); 91 return NULL; 92 } 93 94 if (!bio_set(ret, method)) { 95 OPENSSL_free(ret); 96 ret = NULL; 97 } 98 99 return ret; 100} 101 102int BIO_free(BIO *bio) { 103 BIO *next_bio; 104 105 for (; bio != NULL; bio = next_bio) { 106 int refs = CRYPTO_add(&bio->references, -1, CRYPTO_LOCK_BIO); 107 if (refs > 0) { 108 return 0; 109 } 110 111 if (bio->callback != NULL) { 112 int i = (int)bio->callback(bio, BIO_CB_FREE, NULL, 0, 0, 1); 113 if (i <= 0) { 114 return i; 115 } 116 } 117 118 next_bio = BIO_pop(bio); 119 120 if (bio->method != NULL && bio->method->destroy != NULL) { 121 bio->method->destroy(bio); 122 } 123 124 OPENSSL_free(bio); 125 } 126 return 1; 127} 128 129BIO *BIO_up_ref(BIO *bio) { 130 CRYPTO_add(&bio->references, 1, CRYPTO_LOCK_BIO); 131 return bio; 132} 133 134void BIO_vfree(BIO *bio) { 135 BIO_free(bio); 136} 137 138void BIO_free_all(BIO *bio) { 139 BIO_free(bio); 140} 141 142static int bio_io(BIO *bio, void *buf, int len, size_t method_offset, 143 int callback_flags, size_t *num) { 144 int i; 145 typedef int (*io_func_t)(BIO *, char *, int); 146 io_func_t io_func = NULL; 147 148 if (bio != NULL && bio->method != NULL) { 149 io_func = 150 *((const io_func_t *)(((const uint8_t *)bio->method) + method_offset)); 151 } 152 153 if (io_func == NULL) { 154 OPENSSL_PUT_ERROR(BIO, bio_io, BIO_R_UNSUPPORTED_METHOD); 155 return -2; 156 } 157 158 if (bio->callback != NULL) { 159 i = (int) bio->callback(bio, callback_flags, buf, len, 0L, 1L); 160 if (i <= 0) { 161 return i; 162 } 163 } 164 165 if (!bio->init) { 166 OPENSSL_PUT_ERROR(BIO, bio_io, BIO_R_UNINITIALIZED); 167 return -2; 168 } 169 170 i = 0; 171 if (buf != NULL && len > 0) { 172 i = io_func(bio, buf, len); 173 } 174 175 if (i > 0) { 176 *num += i; 177 } 178 179 if (bio->callback != NULL) { 180 i = (int)(bio->callback(bio, callback_flags | BIO_CB_RETURN, buf, len, 0L, 181 (long)i)); 182 } 183 184 return i; 185} 186 187int BIO_read(BIO *bio, void *buf, int len) { 188 return bio_io(bio, buf, len, offsetof(BIO_METHOD, bread), BIO_CB_READ, 189 &bio->num_read); 190} 191 192int BIO_gets(BIO *bio, char *buf, int len) { 193 return bio_io(bio, buf, len, offsetof(BIO_METHOD, bgets), BIO_CB_GETS, 194 &bio->num_read); 195} 196 197int BIO_write(BIO *bio, const void *in, int inl) { 198 return bio_io(bio, (char *)in, inl, offsetof(BIO_METHOD, bwrite), 199 BIO_CB_WRITE, &bio->num_write); 200} 201 202int BIO_puts(BIO *bio, const char *in) { 203 return BIO_write(bio, in, strlen(in)); 204} 205 206int BIO_flush(BIO *bio) { 207 return BIO_ctrl(bio, BIO_CTRL_FLUSH, 0, NULL); 208} 209 210long BIO_ctrl(BIO *bio, int cmd, long larg, void *parg) { 211 long ret; 212 213 if (bio == NULL) { 214 return 0; 215 } 216 217 if (bio->method == NULL || bio->method->ctrl == NULL) { 218 OPENSSL_PUT_ERROR(BIO, BIO_ctrl, BIO_R_UNSUPPORTED_METHOD); 219 return -2; 220 } 221 222 if (bio->callback != NULL) { 223 ret = bio->callback(bio, BIO_CB_CTRL, parg, cmd, larg, 1); 224 if (ret <= 0) { 225 return ret; 226 } 227 } 228 229 ret = bio->method->ctrl(bio, cmd, larg, parg); 230 231 if (bio->callback != NULL) { 232 ret = bio->callback(bio, BIO_CB_CTRL | BIO_CB_RETURN, parg, cmd, larg, ret); 233 } 234 235 return ret; 236} 237 238char *BIO_ptr_ctrl(BIO *b, int cmd, long larg) { 239 char *p = NULL; 240 241 if (BIO_ctrl(b, cmd, larg, (void *)&p) <= 0) { 242 return NULL; 243 } 244 245 return p; 246} 247 248long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg) { 249 int i = iarg; 250 251 return BIO_ctrl(b, cmd, larg, (void *)&i); 252} 253 254int BIO_reset(BIO *bio) { 255 return BIO_ctrl(bio, BIO_CTRL_RESET, 0, NULL); 256} 257 258void BIO_set_flags(BIO *bio, int flags) { 259 bio->flags |= flags; 260} 261 262int BIO_test_flags(const BIO *bio, int flags) { 263 return bio->flags & flags; 264} 265 266int BIO_should_read(const BIO *bio) { 267 return BIO_test_flags(bio, BIO_FLAGS_READ); 268} 269 270int BIO_should_write(const BIO *bio) { 271 return BIO_test_flags(bio, BIO_FLAGS_WRITE); 272} 273 274int BIO_should_retry(const BIO *bio) { 275 return BIO_test_flags(bio, BIO_FLAGS_SHOULD_RETRY); 276} 277 278int BIO_should_io_special(const BIO *bio) { 279 return BIO_test_flags(bio, BIO_FLAGS_IO_SPECIAL); 280} 281 282int BIO_get_retry_reason(const BIO *bio) { return bio->retry_reason; } 283 284void BIO_clear_flags(BIO *bio, int flags) { 285 bio->flags &= ~flags; 286} 287 288void BIO_set_retry_read(BIO *bio) { 289 bio->flags |= BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY; 290} 291 292void BIO_set_retry_write(BIO *bio) { 293 bio->flags |= BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY; 294} 295 296static const int kRetryFlags = BIO_FLAGS_RWS | BIO_FLAGS_SHOULD_RETRY; 297 298int BIO_get_retry_flags(BIO *bio) { 299 return bio->flags & kRetryFlags; 300} 301 302void BIO_clear_retry_flags(BIO *bio) { 303 bio->flags &= ~kRetryFlags; 304 bio->retry_reason = 0; 305} 306 307int BIO_method_type(const BIO *bio) { return bio->method->type; } 308 309void BIO_copy_next_retry(BIO *bio) { 310 BIO_clear_retry_flags(bio); 311 BIO_set_flags(bio, BIO_get_retry_flags(bio->next_bio)); 312 bio->retry_reason = bio->next_bio->retry_reason; 313} 314 315long BIO_callback_ctrl(BIO *bio, int cmd, bio_info_cb fp) { 316 long ret; 317 bio_info_cb cb; 318 319 if (bio == NULL) { 320 return 0; 321 } 322 323 if (bio->method == NULL || bio->method->callback_ctrl == NULL) { 324 OPENSSL_PUT_ERROR(BIO, BIO_callback_ctrl, BIO_R_UNSUPPORTED_METHOD); 325 return 0; 326 } 327 328 cb = bio->callback; 329 330 if (cb != NULL) { 331 ret = cb(bio, BIO_CB_CTRL, (void *)&fp, cmd, 0, 1L); 332 if (ret <= 0) { 333 return ret; 334 } 335 } 336 337 ret = bio->method->callback_ctrl(bio, cmd, fp); 338 339 if (cb != NULL) { 340 ret = cb(bio, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, cmd, 0, ret); 341 } 342 343 return ret; 344} 345 346size_t BIO_pending(const BIO *bio) { 347 return BIO_ctrl((BIO *) bio, BIO_CTRL_PENDING, 0, NULL); 348} 349 350size_t BIO_ctrl_pending(const BIO *bio) { 351 return BIO_pending(bio); 352} 353 354size_t BIO_wpending(const BIO *bio) { 355 return BIO_ctrl((BIO *) bio, BIO_CTRL_WPENDING, 0, NULL); 356} 357 358int BIO_set_close(BIO *bio, int close_flag) { 359 return BIO_ctrl(bio, BIO_CTRL_SET_CLOSE, close_flag, NULL); 360} 361 362void BIO_set_callback(BIO *bio, bio_info_cb callback_func) { 363 bio->callback = callback_func; 364} 365 366void BIO_set_callback_arg(BIO *bio, char *arg) { 367 bio->cb_arg = arg; 368} 369 370char *BIO_get_callback_arg(const BIO *bio) { 371 return bio->cb_arg; 372} 373 374OPENSSL_EXPORT size_t BIO_number_read(const BIO *bio) { 375 return bio->num_read; 376} 377 378OPENSSL_EXPORT size_t BIO_number_written(const BIO *bio) { 379 return bio->num_write; 380} 381 382BIO *BIO_push(BIO *bio, BIO *appended_bio) { 383 BIO *last_bio; 384 385 if (bio == NULL) { 386 return bio; 387 } 388 389 last_bio = bio; 390 while (last_bio->next_bio != NULL) { 391 last_bio = last_bio->next_bio; 392 } 393 394 last_bio->next_bio = appended_bio; 395 return bio; 396} 397 398BIO *BIO_pop(BIO *bio) { 399 BIO *ret; 400 401 if (bio == NULL) { 402 return NULL; 403 } 404 ret = bio->next_bio; 405 bio->next_bio = NULL; 406 return ret; 407} 408 409BIO *BIO_next(BIO *bio) { 410 if (!bio) { 411 return NULL; 412 } 413 return bio->next_bio; 414} 415 416BIO *BIO_find_type(BIO *bio, int type) { 417 int method_type, mask; 418 419 if (!bio) { 420 return NULL; 421 } 422 mask = type & 0xff; 423 424 do { 425 if (bio->method != NULL) { 426 method_type = bio->method->type; 427 428 if (!mask) { 429 if (method_type & type) { 430 return bio; 431 } 432 } else if (method_type == type) { 433 return bio; 434 } 435 } 436 bio = bio->next_bio; 437 } while (bio != NULL); 438 439 return NULL; 440} 441 442int BIO_indent(BIO *bio, unsigned indent, unsigned max_indent) { 443 if (indent > max_indent) { 444 indent = max_indent; 445 } 446 447 while (indent--) { 448 if (BIO_puts(bio, " ") != 1) { 449 return 0; 450 } 451 } 452 return 1; 453} 454 455static int print_bio(const char *str, size_t len, void *bio) { 456 return BIO_write((BIO *)bio, str, len); 457} 458 459void BIO_print_errors(BIO *bio) { 460 ERR_print_errors_cb(print_bio, bio); 461} 462