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