sl@0: /* crypto/bio/bss_bio.c -*- Mode: C; c-file-style: "eay" -*- */ sl@0: /* ==================================================================== sl@0: * Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * openssl-core@openssl.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.openssl.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: /* sl@0: © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. sl@0: */ sl@0: sl@0: /* Special method for a BIO where the other endpoint is also a BIO sl@0: * of this kind, handled by the same thread (i.e. the "peer" is actually sl@0: * ourselves, wearing a different hat). sl@0: * Such "BIO pairs" are mainly for using the SSL library with I/O interfaces sl@0: * for which no specific BIO method is available. sl@0: * See ssl/ssltest.c for some hints on how this can be used. */ sl@0: sl@0: /* BIO_DEBUG implies BIO_PAIR_DEBUG */ sl@0: #ifdef BIO_DEBUG sl@0: # ifndef BIO_PAIR_DEBUG sl@0: # define BIO_PAIR_DEBUG sl@0: # endif sl@0: #endif sl@0: sl@0: /* disable assert() unless BIO_PAIR_DEBUG has been defined */ sl@0: #ifndef BIO_PAIR_DEBUG sl@0: # ifndef NDEBUG sl@0: # define NDEBUG sl@0: # endif sl@0: #endif sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #include "e_os.h" sl@0: #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__))) sl@0: #include "libcrypto_wsd_macros.h" sl@0: #include "libcrypto_wsd.h" sl@0: #endif sl@0: sl@0: /* VxWorks defines SSIZE_MAX with an empty value causing compile errors */ sl@0: #if defined(OPENSSL_SYS_VXWORKS) sl@0: # undef SSIZE_MAX sl@0: #endif sl@0: #ifndef SSIZE_MAX sl@0: # define SSIZE_MAX INT_MAX sl@0: #endif sl@0: sl@0: static int bio_new(BIO *bio); sl@0: static int bio_free(BIO *bio); sl@0: static int bio_read(BIO *bio, char *buf, int size); sl@0: static int bio_write(BIO *bio, const char *buf, int num); sl@0: static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr); sl@0: static int bio_puts(BIO *bio, const char *str); sl@0: sl@0: static int bio_make_pair(BIO *bio1, BIO *bio2); sl@0: static void bio_destroy_pair(BIO *bio); sl@0: sl@0: #ifndef EMULATOR sl@0: static BIO_METHOD methods_biop = sl@0: { sl@0: BIO_TYPE_BIO, sl@0: "BIO pair", sl@0: bio_write, sl@0: bio_read, sl@0: bio_puts, sl@0: NULL /* no bio_gets */, sl@0: bio_ctrl, sl@0: bio_new, sl@0: bio_free, sl@0: NULL /* no bio_callback_ctrl */ sl@0: }; sl@0: #else sl@0: sl@0: GET_STATIC_VAR_FROM_TLS(methods_biop,bss_bio,BIO_METHOD) sl@0: #define methods_biop (*GET_WSD_VAR_NAME(methods_biop,bss_bio,s)()) sl@0: const BIO_METHOD temp_s_methods_biop = sl@0: { sl@0: BIO_TYPE_BIO, sl@0: "BIO pair", sl@0: bio_write, sl@0: bio_read, sl@0: bio_puts, sl@0: NULL /* no bio_gets */, sl@0: bio_ctrl, sl@0: bio_new, sl@0: bio_free, sl@0: NULL /* no bio_callback_ctrl */ sl@0: }; sl@0: #endif sl@0: sl@0: EXPORT_C BIO_METHOD *BIO_s_bio(void) sl@0: { sl@0: return &methods_biop; sl@0: } sl@0: sl@0: struct bio_bio_st sl@0: { sl@0: BIO *peer; /* NULL if buf == NULL. sl@0: * If peer != NULL, then peer->ptr is also a bio_bio_st, sl@0: * and its "peer" member points back to us. sl@0: * peer != NULL iff init != 0 in the BIO. */ sl@0: sl@0: /* This is for what we write (i.e. reading uses peer's struct): */ sl@0: int closed; /* valid iff peer != NULL */ sl@0: size_t len; /* valid iff buf != NULL; 0 if peer == NULL */ sl@0: size_t offset; /* valid iff buf != NULL; 0 if len == 0 */ sl@0: size_t size; sl@0: char *buf; /* "size" elements (if != NULL) */ sl@0: sl@0: size_t request; /* valid iff peer != NULL; 0 if len != 0, sl@0: * otherwise set by peer to number of bytes sl@0: * it (unsuccessfully) tried to read, sl@0: * never more than buffer space (size-len) warrants. */ sl@0: }; sl@0: sl@0: static int bio_new(BIO *bio) sl@0: { sl@0: struct bio_bio_st *b; sl@0: sl@0: b = OPENSSL_malloc(sizeof *b); sl@0: if (b == NULL) sl@0: return 0; sl@0: sl@0: b->peer = NULL; sl@0: b->size = 17*1024; /* enough for one TLS record (just a default) */ sl@0: b->buf = NULL; sl@0: sl@0: bio->ptr = b; sl@0: return 1; sl@0: } sl@0: sl@0: sl@0: static int bio_free(BIO *bio) sl@0: { sl@0: struct bio_bio_st *b; sl@0: sl@0: if (bio == NULL) sl@0: return 0; sl@0: b = bio->ptr; sl@0: sl@0: assert(b != NULL); sl@0: sl@0: if (b->peer) sl@0: bio_destroy_pair(bio); sl@0: sl@0: if (b->buf != NULL) sl@0: { sl@0: OPENSSL_free(b->buf); sl@0: } sl@0: sl@0: OPENSSL_free(b); sl@0: sl@0: return 1; sl@0: } sl@0: sl@0: sl@0: sl@0: static int bio_read(BIO *bio, char *buf, int size_) sl@0: { sl@0: size_t size = size_; sl@0: size_t rest; sl@0: struct bio_bio_st *b, *peer_b; sl@0: sl@0: BIO_clear_retry_flags(bio); sl@0: sl@0: if (!bio->init) sl@0: return 0; sl@0: sl@0: b = bio->ptr; sl@0: assert(b != NULL); sl@0: assert(b->peer != NULL); sl@0: peer_b = b->peer->ptr; sl@0: assert(peer_b != NULL); sl@0: assert(peer_b->buf != NULL); sl@0: sl@0: peer_b->request = 0; /* will be set in "retry_read" situation */ sl@0: sl@0: if (buf == NULL || size == 0) sl@0: return 0; sl@0: sl@0: if (peer_b->len == 0) sl@0: { sl@0: if (peer_b->closed) sl@0: return 0; /* writer has closed, and no data is left */ sl@0: else sl@0: { sl@0: BIO_set_retry_read(bio); /* buffer is empty */ sl@0: if (size <= peer_b->size) sl@0: peer_b->request = size; sl@0: else sl@0: /* don't ask for more than the peer can sl@0: * deliver in one write */ sl@0: peer_b->request = peer_b->size; sl@0: return -1; sl@0: } sl@0: } sl@0: sl@0: /* we can read */ sl@0: if (peer_b->len < size) sl@0: size = peer_b->len; sl@0: sl@0: /* now read "size" bytes */ sl@0: sl@0: rest = size; sl@0: sl@0: assert(rest > 0); sl@0: do /* one or two iterations */ sl@0: { sl@0: size_t chunk; sl@0: sl@0: assert(rest <= peer_b->len); sl@0: if (peer_b->offset + rest <= peer_b->size) sl@0: chunk = rest; sl@0: else sl@0: /* wrap around ring buffer */ sl@0: chunk = peer_b->size - peer_b->offset; sl@0: assert(peer_b->offset + chunk <= peer_b->size); sl@0: sl@0: memcpy(buf, peer_b->buf + peer_b->offset, chunk); sl@0: sl@0: peer_b->len -= chunk; sl@0: if (peer_b->len) sl@0: { sl@0: peer_b->offset += chunk; sl@0: assert(peer_b->offset <= peer_b->size); sl@0: if (peer_b->offset == peer_b->size) sl@0: peer_b->offset = 0; sl@0: buf += chunk; sl@0: } sl@0: else sl@0: { sl@0: /* buffer now empty, no need to advance "buf" */ sl@0: assert(chunk == rest); sl@0: peer_b->offset = 0; sl@0: } sl@0: rest -= chunk; sl@0: } sl@0: while (rest); sl@0: sl@0: return size; sl@0: } sl@0: sl@0: /* non-copying interface: provide pointer to available data in buffer sl@0: * bio_nread0: return number of available bytes sl@0: * bio_nread: also advance index sl@0: * (example usage: bio_nread0(), read from buffer, bio_nread() sl@0: * or just bio_nread(), read from buffer) sl@0: */ sl@0: /* WARNING: The non-copying interface is largely untested as of yet sl@0: * and may contain bugs. */ sl@0: static ssize_t bio_nread0(BIO *bio, char **buf) sl@0: { sl@0: struct bio_bio_st *b, *peer_b; sl@0: ssize_t num; sl@0: sl@0: BIO_clear_retry_flags(bio); sl@0: sl@0: if (!bio->init) sl@0: return 0; sl@0: sl@0: b = bio->ptr; sl@0: assert(b != NULL); sl@0: assert(b->peer != NULL); sl@0: peer_b = b->peer->ptr; sl@0: assert(peer_b != NULL); sl@0: assert(peer_b->buf != NULL); sl@0: sl@0: peer_b->request = 0; sl@0: sl@0: if (peer_b->len == 0) sl@0: { sl@0: char dummy; sl@0: sl@0: /* avoid code duplication -- nothing available for reading */ sl@0: return bio_read(bio, &dummy, 1); /* returns 0 or -1 */ sl@0: } sl@0: sl@0: num = peer_b->len; sl@0: if (peer_b->size < peer_b->offset + num) sl@0: /* no ring buffer wrap-around for non-copying interface */ sl@0: num = peer_b->size - peer_b->offset; sl@0: assert(num > 0); sl@0: sl@0: if (buf != NULL) sl@0: *buf = peer_b->buf + peer_b->offset; sl@0: return num; sl@0: } sl@0: sl@0: static ssize_t bio_nread(BIO *bio, char **buf, size_t num_) sl@0: { sl@0: struct bio_bio_st *b, *peer_b; sl@0: ssize_t num, available; sl@0: sl@0: if (num_ > SSIZE_MAX) sl@0: num = SSIZE_MAX; sl@0: else sl@0: num = (ssize_t)num_; sl@0: sl@0: available = bio_nread0(bio, buf); sl@0: if (num > available) sl@0: num = available; sl@0: if (num <= 0) sl@0: return num; sl@0: sl@0: b = bio->ptr; sl@0: peer_b = b->peer->ptr; sl@0: sl@0: peer_b->len -= num; sl@0: if (peer_b->len) sl@0: { sl@0: peer_b->offset += num; sl@0: assert(peer_b->offset <= peer_b->size); sl@0: if (peer_b->offset == peer_b->size) sl@0: peer_b->offset = 0; sl@0: } sl@0: else sl@0: peer_b->offset = 0; sl@0: sl@0: return num; sl@0: } sl@0: sl@0: sl@0: static int bio_write(BIO *bio, const char *buf, int num_) sl@0: { sl@0: size_t num = num_; sl@0: size_t rest; sl@0: struct bio_bio_st *b; sl@0: sl@0: BIO_clear_retry_flags(bio); sl@0: sl@0: if (!bio->init || buf == NULL || num == 0) sl@0: return 0; sl@0: sl@0: b = bio->ptr; sl@0: assert(b != NULL); sl@0: assert(b->peer != NULL); sl@0: assert(b->buf != NULL); sl@0: sl@0: b->request = 0; sl@0: if (b->closed) sl@0: { sl@0: /* we already closed */ sl@0: BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE); sl@0: return -1; sl@0: } sl@0: sl@0: assert(b->len <= b->size); sl@0: sl@0: if (b->len == b->size) sl@0: { sl@0: BIO_set_retry_write(bio); /* buffer is full */ sl@0: return -1; sl@0: } sl@0: sl@0: /* we can write */ sl@0: if (num > b->size - b->len) sl@0: num = b->size - b->len; sl@0: sl@0: /* now write "num" bytes */ sl@0: sl@0: rest = num; sl@0: sl@0: assert(rest > 0); sl@0: do /* one or two iterations */ sl@0: { sl@0: size_t write_offset; sl@0: size_t chunk; sl@0: sl@0: assert(b->len + rest <= b->size); sl@0: sl@0: write_offset = b->offset + b->len; sl@0: if (write_offset >= b->size) sl@0: write_offset -= b->size; sl@0: /* b->buf[write_offset] is the first byte we can write to. */ sl@0: sl@0: if (write_offset + rest <= b->size) sl@0: chunk = rest; sl@0: else sl@0: /* wrap around ring buffer */ sl@0: chunk = b->size - write_offset; sl@0: sl@0: memcpy(b->buf + write_offset, buf, chunk); sl@0: sl@0: b->len += chunk; sl@0: sl@0: assert(b->len <= b->size); sl@0: sl@0: rest -= chunk; sl@0: buf += chunk; sl@0: } sl@0: while (rest); sl@0: sl@0: return num; sl@0: } sl@0: sl@0: /* non-copying interface: provide pointer to region to write to sl@0: * bio_nwrite0: check how much space is available sl@0: * bio_nwrite: also increase length sl@0: * (example usage: bio_nwrite0(), write to buffer, bio_nwrite() sl@0: * or just bio_nwrite(), write to buffer) sl@0: */ sl@0: static ssize_t bio_nwrite0(BIO *bio, char **buf) sl@0: { sl@0: struct bio_bio_st *b; sl@0: size_t num; sl@0: size_t write_offset; sl@0: sl@0: BIO_clear_retry_flags(bio); sl@0: sl@0: if (!bio->init) sl@0: return 0; sl@0: sl@0: b = bio->ptr; sl@0: assert(b != NULL); sl@0: assert(b->peer != NULL); sl@0: assert(b->buf != NULL); sl@0: sl@0: b->request = 0; sl@0: if (b->closed) sl@0: { sl@0: BIOerr(BIO_F_BIO_NWRITE0, BIO_R_BROKEN_PIPE); sl@0: return -1; sl@0: } sl@0: sl@0: assert(b->len <= b->size); sl@0: sl@0: if (b->len == b->size) sl@0: { sl@0: BIO_set_retry_write(bio); sl@0: return -1; sl@0: } sl@0: sl@0: num = b->size - b->len; sl@0: write_offset = b->offset + b->len; sl@0: if (write_offset >= b->size) sl@0: write_offset -= b->size; sl@0: if (write_offset + num > b->size) sl@0: /* no ring buffer wrap-around for non-copying interface sl@0: * (to fulfil the promise by BIO_ctrl_get_write_guarantee, sl@0: * BIO_nwrite may have to be called twice) */ sl@0: num = b->size - write_offset; sl@0: sl@0: if (buf != NULL) sl@0: *buf = b->buf + write_offset; sl@0: assert(write_offset + num <= b->size); sl@0: sl@0: return num; sl@0: } sl@0: sl@0: static ssize_t bio_nwrite(BIO *bio, char **buf, size_t num_) sl@0: { sl@0: struct bio_bio_st *b; sl@0: ssize_t num, space; sl@0: sl@0: if (num_ > SSIZE_MAX) sl@0: num = SSIZE_MAX; sl@0: else sl@0: num = (ssize_t)num_; sl@0: sl@0: space = bio_nwrite0(bio, buf); sl@0: if (num > space) sl@0: num = space; sl@0: if (num <= 0) sl@0: return num; sl@0: b = bio->ptr; sl@0: assert(b != NULL); sl@0: b->len += num; sl@0: assert(b->len <= b->size); sl@0: sl@0: return num; sl@0: } sl@0: sl@0: sl@0: static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr) sl@0: { sl@0: long ret; sl@0: struct bio_bio_st *b = bio->ptr; sl@0: sl@0: assert(b != NULL); sl@0: sl@0: switch (cmd) sl@0: { sl@0: /* specific CTRL codes */ sl@0: sl@0: case BIO_C_SET_WRITE_BUF_SIZE: sl@0: if (b->peer) sl@0: { sl@0: BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE); sl@0: ret = 0; sl@0: } sl@0: else if (num == 0) sl@0: { sl@0: BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT); sl@0: ret = 0; sl@0: } sl@0: else sl@0: { sl@0: size_t new_size = num; sl@0: sl@0: if (b->size != new_size) sl@0: { sl@0: if (b->buf) sl@0: { sl@0: OPENSSL_free(b->buf); sl@0: b->buf = NULL; sl@0: } sl@0: b->size = new_size; sl@0: } sl@0: ret = 1; sl@0: } sl@0: break; sl@0: sl@0: case BIO_C_GET_WRITE_BUF_SIZE: sl@0: ret = (long) b->size; sl@0: break; sl@0: sl@0: case BIO_C_MAKE_BIO_PAIR: sl@0: { sl@0: BIO *other_bio = ptr; sl@0: sl@0: if (bio_make_pair(bio, other_bio)) sl@0: ret = 1; sl@0: else sl@0: ret = 0; sl@0: } sl@0: break; sl@0: sl@0: case BIO_C_DESTROY_BIO_PAIR: sl@0: /* Affects both BIOs in the pair -- call just once! sl@0: * Or let BIO_free(bio1); BIO_free(bio2); do the job. */ sl@0: bio_destroy_pair(bio); sl@0: ret = 1; sl@0: break; sl@0: sl@0: case BIO_C_GET_WRITE_GUARANTEE: sl@0: /* How many bytes can the caller feed to the next write sl@0: * without having to keep any? */ sl@0: if (b->peer == NULL || b->closed) sl@0: ret = 0; sl@0: else sl@0: ret = (long) b->size - b->len; sl@0: break; sl@0: sl@0: case BIO_C_GET_READ_REQUEST: sl@0: /* If the peer unsuccessfully tried to read, how many bytes sl@0: * were requested? (As with BIO_CTRL_PENDING, that number sl@0: * can usually be treated as boolean.) */ sl@0: ret = (long) b->request; sl@0: break; sl@0: sl@0: case BIO_C_RESET_READ_REQUEST: sl@0: /* Reset request. (Can be useful after read attempts sl@0: * at the other side that are meant to be non-blocking, sl@0: * e.g. when probing SSL_read to see if any data is sl@0: * available.) */ sl@0: b->request = 0; sl@0: ret = 1; sl@0: break; sl@0: sl@0: case BIO_C_SHUTDOWN_WR: sl@0: /* similar to shutdown(..., SHUT_WR) */ sl@0: b->closed = 1; sl@0: ret = 1; sl@0: break; sl@0: sl@0: case BIO_C_NREAD0: sl@0: /* prepare for non-copying read */ sl@0: ret = (long) bio_nread0(bio, ptr); sl@0: break; sl@0: sl@0: case BIO_C_NREAD: sl@0: /* non-copying read */ sl@0: ret = (long) bio_nread(bio, ptr, (size_t) num); sl@0: break; sl@0: sl@0: case BIO_C_NWRITE0: sl@0: /* prepare for non-copying write */ sl@0: ret = (long) bio_nwrite0(bio, ptr); sl@0: break; sl@0: sl@0: case BIO_C_NWRITE: sl@0: /* non-copying write */ sl@0: ret = (long) bio_nwrite(bio, ptr, (size_t) num); sl@0: break; sl@0: sl@0: sl@0: /* standard CTRL codes follow */ sl@0: sl@0: case BIO_CTRL_RESET: sl@0: if (b->buf != NULL) sl@0: { sl@0: b->len = 0; sl@0: b->offset = 0; sl@0: } sl@0: ret = 0; sl@0: break; sl@0: sl@0: case BIO_CTRL_GET_CLOSE: sl@0: ret = bio->shutdown; sl@0: break; sl@0: sl@0: case BIO_CTRL_SET_CLOSE: sl@0: bio->shutdown = (int) num; sl@0: ret = 1; sl@0: break; sl@0: sl@0: case BIO_CTRL_PENDING: sl@0: if (b->peer != NULL) sl@0: { sl@0: struct bio_bio_st *peer_b = b->peer->ptr; sl@0: sl@0: ret = (long) peer_b->len; sl@0: } sl@0: else sl@0: ret = 0; sl@0: break; sl@0: sl@0: case BIO_CTRL_WPENDING: sl@0: if (b->buf != NULL) sl@0: ret = (long) b->len; sl@0: else sl@0: ret = 0; sl@0: break; sl@0: sl@0: case BIO_CTRL_DUP: sl@0: /* See BIO_dup_chain for circumstances we have to expect. */ sl@0: { sl@0: BIO *other_bio = ptr; sl@0: struct bio_bio_st *other_b; sl@0: sl@0: assert(other_bio != NULL); sl@0: other_b = other_bio->ptr; sl@0: assert(other_b != NULL); sl@0: sl@0: assert(other_b->buf == NULL); /* other_bio is always fresh */ sl@0: sl@0: other_b->size = b->size; sl@0: } sl@0: sl@0: ret = 1; sl@0: break; sl@0: sl@0: case BIO_CTRL_FLUSH: sl@0: ret = 1; sl@0: break; sl@0: sl@0: case BIO_CTRL_EOF: sl@0: { sl@0: BIO *other_bio = ptr; sl@0: sl@0: if (other_bio) sl@0: { sl@0: struct bio_bio_st *other_b = other_bio->ptr; sl@0: sl@0: assert(other_b != NULL); sl@0: ret = other_b->len == 0 && other_b->closed; sl@0: } sl@0: else sl@0: ret = 1; sl@0: } sl@0: break; sl@0: sl@0: default: sl@0: ret = 0; sl@0: } sl@0: return ret; sl@0: } sl@0: sl@0: static int bio_puts(BIO *bio, const char *str) sl@0: { sl@0: return bio_write(bio, str, strlen(str)); sl@0: } sl@0: sl@0: sl@0: static int bio_make_pair(BIO *bio1, BIO *bio2) sl@0: { sl@0: struct bio_bio_st *b1, *b2; sl@0: sl@0: assert(bio1 != NULL); sl@0: assert(bio2 != NULL); sl@0: sl@0: b1 = bio1->ptr; sl@0: b2 = bio2->ptr; sl@0: sl@0: if (b1->peer != NULL || b2->peer != NULL) sl@0: { sl@0: BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE); sl@0: return 0; sl@0: } sl@0: sl@0: if (b1->buf == NULL) sl@0: { sl@0: b1->buf = OPENSSL_malloc(b1->size); sl@0: if (b1->buf == NULL) sl@0: { sl@0: BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE); sl@0: return 0; sl@0: } sl@0: b1->len = 0; sl@0: b1->offset = 0; sl@0: } sl@0: sl@0: if (b2->buf == NULL) sl@0: { sl@0: b2->buf = OPENSSL_malloc(b2->size); sl@0: if (b2->buf == NULL) sl@0: { sl@0: BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE); sl@0: return 0; sl@0: } sl@0: b2->len = 0; sl@0: b2->offset = 0; sl@0: } sl@0: sl@0: b1->peer = bio2; sl@0: b1->closed = 0; sl@0: b1->request = 0; sl@0: b2->peer = bio1; sl@0: b2->closed = 0; sl@0: b2->request = 0; sl@0: sl@0: bio1->init = 1; sl@0: bio2->init = 1; sl@0: sl@0: return 1; sl@0: } sl@0: sl@0: static void bio_destroy_pair(BIO *bio) sl@0: { sl@0: struct bio_bio_st *b = bio->ptr; sl@0: sl@0: if (b != NULL) sl@0: { sl@0: BIO *peer_bio = b->peer; sl@0: sl@0: if (peer_bio != NULL) sl@0: { sl@0: struct bio_bio_st *peer_b = peer_bio->ptr; sl@0: sl@0: assert(peer_b != NULL); sl@0: assert(peer_b->peer == bio); sl@0: sl@0: peer_b->peer = NULL; sl@0: peer_bio->init = 0; sl@0: assert(peer_b->buf != NULL); sl@0: peer_b->len = 0; sl@0: peer_b->offset = 0; sl@0: sl@0: b->peer = NULL; sl@0: bio->init = 0; sl@0: assert(b->buf != NULL); sl@0: b->len = 0; sl@0: b->offset = 0; sl@0: } sl@0: } sl@0: } sl@0: sl@0: sl@0: /* Exported convenience functions */ sl@0: EXPORT_C int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1, sl@0: BIO **bio2_p, size_t writebuf2) sl@0: { sl@0: BIO *bio1 = NULL, *bio2 = NULL; sl@0: long r; sl@0: int ret = 0; sl@0: sl@0: bio1 = BIO_new(BIO_s_bio()); sl@0: if (bio1 == NULL) sl@0: goto err; sl@0: bio2 = BIO_new(BIO_s_bio()); sl@0: if (bio2 == NULL) sl@0: goto err; sl@0: sl@0: if (writebuf1) sl@0: { sl@0: r = BIO_set_write_buf_size(bio1, writebuf1); sl@0: if (!r) sl@0: goto err; sl@0: } sl@0: if (writebuf2) sl@0: { sl@0: r = BIO_set_write_buf_size(bio2, writebuf2); sl@0: if (!r) sl@0: goto err; sl@0: } sl@0: sl@0: r = BIO_make_bio_pair(bio1, bio2); sl@0: if (!r) sl@0: goto err; sl@0: ret = 1; sl@0: sl@0: err: sl@0: if (ret == 0) sl@0: { sl@0: if (bio1) sl@0: { sl@0: BIO_free(bio1); sl@0: bio1 = NULL; sl@0: } sl@0: if (bio2) sl@0: { sl@0: BIO_free(bio2); sl@0: bio2 = NULL; sl@0: } sl@0: } sl@0: sl@0: *bio1_p = bio1; sl@0: *bio2_p = bio2; sl@0: return ret; sl@0: } sl@0: sl@0: EXPORT_C size_t BIO_ctrl_get_write_guarantee(BIO *bio) sl@0: { sl@0: return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL); sl@0: } sl@0: sl@0: EXPORT_C size_t BIO_ctrl_get_read_request(BIO *bio) sl@0: { sl@0: return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL); sl@0: } sl@0: sl@0: EXPORT_C int BIO_ctrl_reset_read_request(BIO *bio) sl@0: { sl@0: return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0); sl@0: } sl@0: sl@0: sl@0: /* BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now sl@0: * (conceivably some other BIOs could allow non-copying reads and writes too.) sl@0: */ sl@0: EXPORT_C int BIO_nread0(BIO *bio, char **buf) sl@0: { sl@0: long ret; sl@0: sl@0: if (!bio->init) sl@0: { sl@0: BIOerr(BIO_F_BIO_NREAD0, BIO_R_UNINITIALIZED); sl@0: return -2; sl@0: } sl@0: sl@0: ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf); sl@0: if (ret > INT_MAX) sl@0: return INT_MAX; sl@0: else sl@0: return (int) ret; sl@0: } sl@0: sl@0: EXPORT_C int BIO_nread(BIO *bio, char **buf, int num) sl@0: { sl@0: int ret; sl@0: sl@0: if (!bio->init) sl@0: { sl@0: BIOerr(BIO_F_BIO_NREAD, BIO_R_UNINITIALIZED); sl@0: return -2; sl@0: } sl@0: sl@0: ret = (int) BIO_ctrl(bio, BIO_C_NREAD, num, buf); sl@0: if (ret > 0) sl@0: bio->num_read += ret; sl@0: return ret; sl@0: } sl@0: sl@0: EXPORT_C int BIO_nwrite0(BIO *bio, char **buf) sl@0: { sl@0: long ret; sl@0: sl@0: if (!bio->init) sl@0: { sl@0: BIOerr(BIO_F_BIO_NWRITE0, BIO_R_UNINITIALIZED); sl@0: return -2; sl@0: } sl@0: sl@0: ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf); sl@0: if (ret > INT_MAX) sl@0: return INT_MAX; sl@0: else sl@0: return (int) ret; sl@0: } sl@0: sl@0: EXPORT_C int BIO_nwrite(BIO *bio, char **buf, int num) sl@0: { sl@0: int ret; sl@0: sl@0: if (!bio->init) sl@0: { sl@0: BIOerr(BIO_F_BIO_NWRITE, BIO_R_UNINITIALIZED); sl@0: return -2; sl@0: } sl@0: sl@0: ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf); sl@0: if (ret > 0) sl@0: bio->num_read += ret; sl@0: return ret; sl@0: }