os/ossrv/ssl/libcrypto/src/crypto/bio/bss_bio.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* crypto/bio/bss_bio.c  -*- Mode: C; c-file-style: "eay" -*- */
sl@0
     2
/* ====================================================================
sl@0
     3
 * Copyright (c) 1998-2003 The OpenSSL Project.  All rights reserved.
sl@0
     4
 *
sl@0
     5
 * Redistribution and use in source and binary forms, with or without
sl@0
     6
 * modification, are permitted provided that the following conditions
sl@0
     7
 * are met:
sl@0
     8
 *
sl@0
     9
 * 1. Redistributions of source code must retain the above copyright
sl@0
    10
 *    notice, this list of conditions and the following disclaimer. 
sl@0
    11
 *
sl@0
    12
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    13
 *    notice, this list of conditions and the following disclaimer in
sl@0
    14
 *    the documentation and/or other materials provided with the
sl@0
    15
 *    distribution.
sl@0
    16
 *
sl@0
    17
 * 3. All advertising materials mentioning features or use of this
sl@0
    18
 *    software must display the following acknowledgment:
sl@0
    19
 *    "This product includes software developed by the OpenSSL Project
sl@0
    20
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
sl@0
    21
 *
sl@0
    22
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
sl@0
    23
 *    endorse or promote products derived from this software without
sl@0
    24
 *    prior written permission. For written permission, please contact
sl@0
    25
 *    openssl-core@openssl.org.
sl@0
    26
 *
sl@0
    27
 * 5. Products derived from this software may not be called "OpenSSL"
sl@0
    28
 *    nor may "OpenSSL" appear in their names without prior written
sl@0
    29
 *    permission of the OpenSSL Project.
sl@0
    30
 *
sl@0
    31
 * 6. Redistributions of any form whatsoever must retain the following
sl@0
    32
 *    acknowledgment:
sl@0
    33
 *    "This product includes software developed by the OpenSSL Project
sl@0
    34
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
sl@0
    35
 *
sl@0
    36
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
sl@0
    37
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    38
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
sl@0
    39
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
sl@0
    40
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
sl@0
    41
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
sl@0
    42
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
sl@0
    43
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    44
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
sl@0
    45
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
sl@0
    46
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
sl@0
    47
 * OF THE POSSIBILITY OF SUCH DAMAGE.
sl@0
    48
 * ====================================================================
sl@0
    49
 *
sl@0
    50
 * This product includes cryptographic software written by Eric Young
sl@0
    51
 * (eay@cryptsoft.com).  This product includes software written by Tim
sl@0
    52
 * Hudson (tjh@cryptsoft.com).
sl@0
    53
 *
sl@0
    54
 */
sl@0
    55
/*
sl@0
    56
 © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
sl@0
    57
 */
sl@0
    58
sl@0
    59
/* Special method for a BIO where the other endpoint is also a BIO
sl@0
    60
 * of this kind, handled by the same thread (i.e. the "peer" is actually
sl@0
    61
 * ourselves, wearing a different hat).
sl@0
    62
 * Such "BIO pairs" are mainly for using the SSL library with I/O interfaces
sl@0
    63
 * for which no specific BIO method is available.
sl@0
    64
 * See ssl/ssltest.c for some hints on how this can be used. */
sl@0
    65
sl@0
    66
/* BIO_DEBUG implies BIO_PAIR_DEBUG */
sl@0
    67
#ifdef BIO_DEBUG
sl@0
    68
# ifndef BIO_PAIR_DEBUG
sl@0
    69
#  define BIO_PAIR_DEBUG
sl@0
    70
# endif
sl@0
    71
#endif
sl@0
    72
sl@0
    73
/* disable assert() unless BIO_PAIR_DEBUG has been defined */
sl@0
    74
#ifndef BIO_PAIR_DEBUG
sl@0
    75
# ifndef NDEBUG
sl@0
    76
#  define NDEBUG
sl@0
    77
# endif
sl@0
    78
#endif
sl@0
    79
sl@0
    80
#include <assert.h>
sl@0
    81
#include <limits.h>
sl@0
    82
#include <stdlib.h>
sl@0
    83
#include <string.h>
sl@0
    84
sl@0
    85
#include <openssl/bio.h>
sl@0
    86
#include <openssl/err.h>
sl@0
    87
#include <openssl/crypto.h>
sl@0
    88
sl@0
    89
#include "e_os.h"
sl@0
    90
#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
sl@0
    91
#include "libcrypto_wsd_macros.h"
sl@0
    92
#include "libcrypto_wsd.h"
sl@0
    93
#endif
sl@0
    94
sl@0
    95
/* VxWorks defines SSIZE_MAX with an empty value causing compile errors */
sl@0
    96
#if defined(OPENSSL_SYS_VXWORKS)
sl@0
    97
# undef SSIZE_MAX
sl@0
    98
#endif
sl@0
    99
#ifndef SSIZE_MAX
sl@0
   100
# define SSIZE_MAX INT_MAX
sl@0
   101
#endif
sl@0
   102
sl@0
   103
static int bio_new(BIO *bio);
sl@0
   104
static int bio_free(BIO *bio);
sl@0
   105
static int bio_read(BIO *bio, char *buf, int size);
sl@0
   106
static int bio_write(BIO *bio, const char *buf, int num);
sl@0
   107
static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr);
sl@0
   108
static int bio_puts(BIO *bio, const char *str);
sl@0
   109
sl@0
   110
static int bio_make_pair(BIO *bio1, BIO *bio2);
sl@0
   111
static void bio_destroy_pair(BIO *bio);
sl@0
   112
sl@0
   113
#ifndef EMULATOR
sl@0
   114
static BIO_METHOD methods_biop =
sl@0
   115
{
sl@0
   116
	BIO_TYPE_BIO,
sl@0
   117
	"BIO pair",
sl@0
   118
	bio_write,
sl@0
   119
	bio_read,
sl@0
   120
	bio_puts,
sl@0
   121
	NULL /* no bio_gets */,
sl@0
   122
	bio_ctrl,
sl@0
   123
	bio_new,
sl@0
   124
	bio_free,
sl@0
   125
	NULL /* no bio_callback_ctrl */
sl@0
   126
};
sl@0
   127
#else
sl@0
   128
sl@0
   129
GET_STATIC_VAR_FROM_TLS(methods_biop,bss_bio,BIO_METHOD)
sl@0
   130
#define methods_biop (*GET_WSD_VAR_NAME(methods_biop,bss_bio,s)())
sl@0
   131
const BIO_METHOD temp_s_methods_biop =
sl@0
   132
{
sl@0
   133
	BIO_TYPE_BIO,
sl@0
   134
	"BIO pair",
sl@0
   135
	bio_write,
sl@0
   136
	bio_read,
sl@0
   137
	bio_puts,
sl@0
   138
	NULL /* no bio_gets */,
sl@0
   139
	bio_ctrl,
sl@0
   140
	bio_new,
sl@0
   141
	bio_free,
sl@0
   142
	NULL /* no bio_callback_ctrl */
sl@0
   143
};
sl@0
   144
#endif	
sl@0
   145
sl@0
   146
EXPORT_C BIO_METHOD *BIO_s_bio(void)
sl@0
   147
	{
sl@0
   148
	return &methods_biop;
sl@0
   149
	}
sl@0
   150
sl@0
   151
struct bio_bio_st
sl@0
   152
{
sl@0
   153
	BIO *peer;     /* NULL if buf == NULL.
sl@0
   154
	                * If peer != NULL, then peer->ptr is also a bio_bio_st,
sl@0
   155
	                * and its "peer" member points back to us.
sl@0
   156
	                * peer != NULL iff init != 0 in the BIO. */
sl@0
   157
	
sl@0
   158
	/* This is for what we write (i.e. reading uses peer's struct): */
sl@0
   159
	int closed;     /* valid iff peer != NULL */
sl@0
   160
	size_t len;     /* valid iff buf != NULL; 0 if peer == NULL */
sl@0
   161
	size_t offset;  /* valid iff buf != NULL; 0 if len == 0 */
sl@0
   162
	size_t size;
sl@0
   163
	char *buf;      /* "size" elements (if != NULL) */
sl@0
   164
sl@0
   165
	size_t request; /* valid iff peer != NULL; 0 if len != 0,
sl@0
   166
	                 * otherwise set by peer to number of bytes
sl@0
   167
	                 * it (unsuccessfully) tried to read,
sl@0
   168
	                 * never more than buffer space (size-len) warrants. */
sl@0
   169
};
sl@0
   170
sl@0
   171
static int bio_new(BIO *bio)
sl@0
   172
	{
sl@0
   173
	struct bio_bio_st *b;
sl@0
   174
	
sl@0
   175
	b = OPENSSL_malloc(sizeof *b);
sl@0
   176
	if (b == NULL)
sl@0
   177
		return 0;
sl@0
   178
sl@0
   179
	b->peer = NULL;
sl@0
   180
	b->size = 17*1024; /* enough for one TLS record (just a default) */
sl@0
   181
	b->buf = NULL;
sl@0
   182
sl@0
   183
	bio->ptr = b;
sl@0
   184
	return 1;
sl@0
   185
	}
sl@0
   186
sl@0
   187
sl@0
   188
static int bio_free(BIO *bio)
sl@0
   189
	{
sl@0
   190
	struct bio_bio_st *b;
sl@0
   191
sl@0
   192
	if (bio == NULL)
sl@0
   193
		return 0;
sl@0
   194
	b = bio->ptr;
sl@0
   195
sl@0
   196
	assert(b != NULL);
sl@0
   197
sl@0
   198
	if (b->peer)
sl@0
   199
		bio_destroy_pair(bio);
sl@0
   200
	
sl@0
   201
	if (b->buf != NULL)
sl@0
   202
		{
sl@0
   203
		OPENSSL_free(b->buf);
sl@0
   204
		}
sl@0
   205
sl@0
   206
	OPENSSL_free(b);
sl@0
   207
sl@0
   208
	return 1;
sl@0
   209
	}
sl@0
   210
sl@0
   211
sl@0
   212
sl@0
   213
static int bio_read(BIO *bio, char *buf, int size_)
sl@0
   214
	{
sl@0
   215
	size_t size = size_;
sl@0
   216
	size_t rest;
sl@0
   217
	struct bio_bio_st *b, *peer_b;
sl@0
   218
sl@0
   219
	BIO_clear_retry_flags(bio);
sl@0
   220
sl@0
   221
	if (!bio->init)
sl@0
   222
		return 0;
sl@0
   223
sl@0
   224
	b = bio->ptr;
sl@0
   225
	assert(b != NULL);
sl@0
   226
	assert(b->peer != NULL);
sl@0
   227
	peer_b = b->peer->ptr;
sl@0
   228
	assert(peer_b != NULL);
sl@0
   229
	assert(peer_b->buf != NULL);
sl@0
   230
sl@0
   231
	peer_b->request = 0; /* will be set in "retry_read" situation */
sl@0
   232
sl@0
   233
	if (buf == NULL || size == 0)
sl@0
   234
		return 0;
sl@0
   235
sl@0
   236
	if (peer_b->len == 0)
sl@0
   237
		{
sl@0
   238
		if (peer_b->closed)
sl@0
   239
			return 0; /* writer has closed, and no data is left */
sl@0
   240
		else
sl@0
   241
			{
sl@0
   242
			BIO_set_retry_read(bio); /* buffer is empty */
sl@0
   243
			if (size <= peer_b->size)
sl@0
   244
				peer_b->request = size;
sl@0
   245
			else
sl@0
   246
				/* don't ask for more than the peer can
sl@0
   247
				 * deliver in one write */
sl@0
   248
				peer_b->request = peer_b->size;
sl@0
   249
			return -1;
sl@0
   250
			}
sl@0
   251
		}
sl@0
   252
sl@0
   253
	/* we can read */
sl@0
   254
	if (peer_b->len < size)
sl@0
   255
		size = peer_b->len;
sl@0
   256
sl@0
   257
	/* now read "size" bytes */
sl@0
   258
	
sl@0
   259
	rest = size;
sl@0
   260
	
sl@0
   261
	assert(rest > 0);
sl@0
   262
	do /* one or two iterations */
sl@0
   263
		{
sl@0
   264
		size_t chunk;
sl@0
   265
		
sl@0
   266
		assert(rest <= peer_b->len);
sl@0
   267
		if (peer_b->offset + rest <= peer_b->size)
sl@0
   268
			chunk = rest;
sl@0
   269
		else
sl@0
   270
			/* wrap around ring buffer */
sl@0
   271
			chunk = peer_b->size - peer_b->offset;
sl@0
   272
		assert(peer_b->offset + chunk <= peer_b->size);
sl@0
   273
		
sl@0
   274
		memcpy(buf, peer_b->buf + peer_b->offset, chunk);
sl@0
   275
		
sl@0
   276
		peer_b->len -= chunk;
sl@0
   277
		if (peer_b->len)
sl@0
   278
			{
sl@0
   279
			peer_b->offset += chunk;
sl@0
   280
			assert(peer_b->offset <= peer_b->size);
sl@0
   281
			if (peer_b->offset == peer_b->size)
sl@0
   282
				peer_b->offset = 0;
sl@0
   283
			buf += chunk;
sl@0
   284
			}
sl@0
   285
		else
sl@0
   286
			{
sl@0
   287
			/* buffer now empty, no need to advance "buf" */
sl@0
   288
			assert(chunk == rest);
sl@0
   289
			peer_b->offset = 0;
sl@0
   290
			}
sl@0
   291
		rest -= chunk;
sl@0
   292
		}
sl@0
   293
	while (rest);
sl@0
   294
	
sl@0
   295
	return size;
sl@0
   296
	}
sl@0
   297
sl@0
   298
/* non-copying interface: provide pointer to available data in buffer
sl@0
   299
 *    bio_nread0:  return number of available bytes
sl@0
   300
 *    bio_nread:   also advance index
sl@0
   301
 * (example usage:  bio_nread0(), read from buffer, bio_nread()
sl@0
   302
 *  or just         bio_nread(), read from buffer)
sl@0
   303
 */
sl@0
   304
/* WARNING: The non-copying interface is largely untested as of yet
sl@0
   305
 * and may contain bugs. */
sl@0
   306
static ssize_t bio_nread0(BIO *bio, char **buf)
sl@0
   307
	{
sl@0
   308
	struct bio_bio_st *b, *peer_b;
sl@0
   309
	ssize_t num;
sl@0
   310
	
sl@0
   311
	BIO_clear_retry_flags(bio);
sl@0
   312
sl@0
   313
	if (!bio->init)
sl@0
   314
		return 0;
sl@0
   315
	
sl@0
   316
	b = bio->ptr;
sl@0
   317
	assert(b != NULL);
sl@0
   318
	assert(b->peer != NULL);
sl@0
   319
	peer_b = b->peer->ptr;
sl@0
   320
	assert(peer_b != NULL);
sl@0
   321
	assert(peer_b->buf != NULL);
sl@0
   322
	
sl@0
   323
	peer_b->request = 0;
sl@0
   324
	
sl@0
   325
	if (peer_b->len == 0)
sl@0
   326
		{
sl@0
   327
		char dummy;
sl@0
   328
		
sl@0
   329
		/* avoid code duplication -- nothing available for reading */
sl@0
   330
		return bio_read(bio, &dummy, 1); /* returns 0 or -1 */
sl@0
   331
		}
sl@0
   332
sl@0
   333
	num = peer_b->len;
sl@0
   334
	if (peer_b->size < peer_b->offset + num)
sl@0
   335
		/* no ring buffer wrap-around for non-copying interface */
sl@0
   336
		num = peer_b->size - peer_b->offset;
sl@0
   337
	assert(num > 0);
sl@0
   338
sl@0
   339
	if (buf != NULL)
sl@0
   340
		*buf = peer_b->buf + peer_b->offset;
sl@0
   341
	return num;
sl@0
   342
	}
sl@0
   343
sl@0
   344
static ssize_t bio_nread(BIO *bio, char **buf, size_t num_)
sl@0
   345
	{
sl@0
   346
	struct bio_bio_st *b, *peer_b;
sl@0
   347
	ssize_t num, available;
sl@0
   348
sl@0
   349
	if (num_ > SSIZE_MAX)
sl@0
   350
		num = SSIZE_MAX;
sl@0
   351
	else
sl@0
   352
		num = (ssize_t)num_;
sl@0
   353
sl@0
   354
	available = bio_nread0(bio, buf);
sl@0
   355
	if (num > available)
sl@0
   356
		num = available;
sl@0
   357
	if (num <= 0)
sl@0
   358
		return num;
sl@0
   359
sl@0
   360
	b = bio->ptr;
sl@0
   361
	peer_b = b->peer->ptr;
sl@0
   362
sl@0
   363
	peer_b->len -= num;
sl@0
   364
	if (peer_b->len) 
sl@0
   365
		{
sl@0
   366
		peer_b->offset += num;
sl@0
   367
		assert(peer_b->offset <= peer_b->size);
sl@0
   368
		if (peer_b->offset == peer_b->size)
sl@0
   369
			peer_b->offset = 0;
sl@0
   370
		}
sl@0
   371
	else
sl@0
   372
		peer_b->offset = 0;
sl@0
   373
sl@0
   374
	return num;
sl@0
   375
	}
sl@0
   376
sl@0
   377
sl@0
   378
static int bio_write(BIO *bio, const char *buf, int num_)
sl@0
   379
	{
sl@0
   380
	size_t num = num_;
sl@0
   381
	size_t rest;
sl@0
   382
	struct bio_bio_st *b;
sl@0
   383
sl@0
   384
	BIO_clear_retry_flags(bio);
sl@0
   385
sl@0
   386
	if (!bio->init || buf == NULL || num == 0)
sl@0
   387
		return 0;
sl@0
   388
sl@0
   389
	b = bio->ptr;		
sl@0
   390
	assert(b != NULL);
sl@0
   391
	assert(b->peer != NULL);
sl@0
   392
	assert(b->buf != NULL);
sl@0
   393
sl@0
   394
	b->request = 0;
sl@0
   395
	if (b->closed)
sl@0
   396
		{
sl@0
   397
		/* we already closed */
sl@0
   398
		BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
sl@0
   399
		return -1;
sl@0
   400
		}
sl@0
   401
sl@0
   402
	assert(b->len <= b->size);
sl@0
   403
sl@0
   404
	if (b->len == b->size)
sl@0
   405
		{
sl@0
   406
		BIO_set_retry_write(bio); /* buffer is full */
sl@0
   407
		return -1;
sl@0
   408
		}
sl@0
   409
sl@0
   410
	/* we can write */
sl@0
   411
	if (num > b->size - b->len)
sl@0
   412
		num = b->size - b->len;
sl@0
   413
	
sl@0
   414
	/* now write "num" bytes */
sl@0
   415
sl@0
   416
	rest = num;
sl@0
   417
	
sl@0
   418
	assert(rest > 0);
sl@0
   419
	do /* one or two iterations */
sl@0
   420
		{
sl@0
   421
		size_t write_offset;
sl@0
   422
		size_t chunk;
sl@0
   423
sl@0
   424
		assert(b->len + rest <= b->size);
sl@0
   425
sl@0
   426
		write_offset = b->offset + b->len;
sl@0
   427
		if (write_offset >= b->size)
sl@0
   428
			write_offset -= b->size;
sl@0
   429
		/* b->buf[write_offset] is the first byte we can write to. */
sl@0
   430
sl@0
   431
		if (write_offset + rest <= b->size)
sl@0
   432
			chunk = rest;
sl@0
   433
		else
sl@0
   434
			/* wrap around ring buffer */
sl@0
   435
			chunk = b->size - write_offset;
sl@0
   436
		
sl@0
   437
		memcpy(b->buf + write_offset, buf, chunk);
sl@0
   438
		
sl@0
   439
		b->len += chunk;
sl@0
   440
sl@0
   441
		assert(b->len <= b->size);
sl@0
   442
		
sl@0
   443
		rest -= chunk;
sl@0
   444
		buf += chunk;
sl@0
   445
		}
sl@0
   446
	while (rest);
sl@0
   447
sl@0
   448
	return num;
sl@0
   449
	}
sl@0
   450
sl@0
   451
/* non-copying interface: provide pointer to region to write to
sl@0
   452
 *   bio_nwrite0:  check how much space is available
sl@0
   453
 *   bio_nwrite:   also increase length
sl@0
   454
 * (example usage:  bio_nwrite0(), write to buffer, bio_nwrite()
sl@0
   455
 *  or just         bio_nwrite(), write to buffer)
sl@0
   456
 */
sl@0
   457
static ssize_t bio_nwrite0(BIO *bio, char **buf)
sl@0
   458
	{
sl@0
   459
	struct bio_bio_st *b;
sl@0
   460
	size_t num;
sl@0
   461
	size_t write_offset;
sl@0
   462
sl@0
   463
	BIO_clear_retry_flags(bio);
sl@0
   464
sl@0
   465
	if (!bio->init)
sl@0
   466
		return 0;
sl@0
   467
sl@0
   468
	b = bio->ptr;		
sl@0
   469
	assert(b != NULL);
sl@0
   470
	assert(b->peer != NULL);
sl@0
   471
	assert(b->buf != NULL);
sl@0
   472
sl@0
   473
	b->request = 0;
sl@0
   474
	if (b->closed)
sl@0
   475
		{
sl@0
   476
		BIOerr(BIO_F_BIO_NWRITE0, BIO_R_BROKEN_PIPE);
sl@0
   477
		return -1;
sl@0
   478
		}
sl@0
   479
sl@0
   480
	assert(b->len <= b->size);
sl@0
   481
sl@0
   482
	if (b->len == b->size)
sl@0
   483
		{
sl@0
   484
		BIO_set_retry_write(bio);
sl@0
   485
		return -1;
sl@0
   486
		}
sl@0
   487
sl@0
   488
	num = b->size - b->len;
sl@0
   489
	write_offset = b->offset + b->len;
sl@0
   490
	if (write_offset >= b->size)
sl@0
   491
		write_offset -= b->size;
sl@0
   492
	if (write_offset + num > b->size)
sl@0
   493
		/* no ring buffer wrap-around for non-copying interface
sl@0
   494
		 * (to fulfil the promise by BIO_ctrl_get_write_guarantee,
sl@0
   495
		 * BIO_nwrite may have to be called twice) */
sl@0
   496
		num = b->size - write_offset;
sl@0
   497
sl@0
   498
	if (buf != NULL)
sl@0
   499
		*buf = b->buf + write_offset;
sl@0
   500
	assert(write_offset + num <= b->size);
sl@0
   501
sl@0
   502
	return num;
sl@0
   503
	}
sl@0
   504
sl@0
   505
static ssize_t bio_nwrite(BIO *bio, char **buf, size_t num_)
sl@0
   506
	{
sl@0
   507
	struct bio_bio_st *b;
sl@0
   508
	ssize_t num, space;
sl@0
   509
sl@0
   510
	if (num_ > SSIZE_MAX)
sl@0
   511
		num = SSIZE_MAX;
sl@0
   512
	else
sl@0
   513
		num = (ssize_t)num_;
sl@0
   514
sl@0
   515
	space = bio_nwrite0(bio, buf);
sl@0
   516
	if (num > space)
sl@0
   517
		num = space;
sl@0
   518
	if (num <= 0)
sl@0
   519
		return num;
sl@0
   520
	b = bio->ptr;
sl@0
   521
	assert(b != NULL);
sl@0
   522
	b->len += num;
sl@0
   523
	assert(b->len <= b->size);
sl@0
   524
sl@0
   525
	return num;
sl@0
   526
	}
sl@0
   527
sl@0
   528
sl@0
   529
static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
sl@0
   530
	{
sl@0
   531
	long ret;
sl@0
   532
	struct bio_bio_st *b = bio->ptr;
sl@0
   533
	
sl@0
   534
	assert(b != NULL);
sl@0
   535
sl@0
   536
	switch (cmd)
sl@0
   537
		{
sl@0
   538
	/* specific CTRL codes */
sl@0
   539
sl@0
   540
	case BIO_C_SET_WRITE_BUF_SIZE:
sl@0
   541
		if (b->peer)
sl@0
   542
			{
sl@0
   543
			BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
sl@0
   544
			ret = 0;
sl@0
   545
			}
sl@0
   546
		else if (num == 0)
sl@0
   547
			{
sl@0
   548
			BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
sl@0
   549
			ret = 0;
sl@0
   550
			}
sl@0
   551
		else
sl@0
   552
			{
sl@0
   553
			size_t new_size = num;
sl@0
   554
sl@0
   555
			if (b->size != new_size)
sl@0
   556
				{
sl@0
   557
				if (b->buf) 
sl@0
   558
					{
sl@0
   559
					OPENSSL_free(b->buf);
sl@0
   560
					b->buf = NULL;
sl@0
   561
					}
sl@0
   562
				b->size = new_size;
sl@0
   563
				}
sl@0
   564
			ret = 1;
sl@0
   565
			}
sl@0
   566
		break;
sl@0
   567
sl@0
   568
	case BIO_C_GET_WRITE_BUF_SIZE:
sl@0
   569
		ret = (long) b->size;
sl@0
   570
		break;
sl@0
   571
sl@0
   572
	case BIO_C_MAKE_BIO_PAIR:
sl@0
   573
		{
sl@0
   574
		BIO *other_bio = ptr;
sl@0
   575
		
sl@0
   576
		if (bio_make_pair(bio, other_bio))
sl@0
   577
			ret = 1;
sl@0
   578
		else
sl@0
   579
			ret = 0;
sl@0
   580
		}
sl@0
   581
		break;
sl@0
   582
		
sl@0
   583
	case BIO_C_DESTROY_BIO_PAIR:
sl@0
   584
		/* Affects both BIOs in the pair -- call just once!
sl@0
   585
		 * Or let BIO_free(bio1); BIO_free(bio2); do the job. */
sl@0
   586
		bio_destroy_pair(bio);
sl@0
   587
		ret = 1;
sl@0
   588
		break;
sl@0
   589
sl@0
   590
	case BIO_C_GET_WRITE_GUARANTEE:
sl@0
   591
		/* How many bytes can the caller feed to the next write
sl@0
   592
		 * without having to keep any? */
sl@0
   593
		if (b->peer == NULL || b->closed)
sl@0
   594
			ret = 0;
sl@0
   595
		else
sl@0
   596
			ret = (long) b->size - b->len;
sl@0
   597
		break;
sl@0
   598
sl@0
   599
	case BIO_C_GET_READ_REQUEST:
sl@0
   600
		/* If the peer unsuccessfully tried to read, how many bytes
sl@0
   601
		 * were requested?  (As with BIO_CTRL_PENDING, that number
sl@0
   602
		 * can usually be treated as boolean.) */
sl@0
   603
		ret = (long) b->request;
sl@0
   604
		break;
sl@0
   605
sl@0
   606
	case BIO_C_RESET_READ_REQUEST:
sl@0
   607
		/* Reset request.  (Can be useful after read attempts
sl@0
   608
		 * at the other side that are meant to be non-blocking,
sl@0
   609
		 * e.g. when probing SSL_read to see if any data is
sl@0
   610
		 * available.) */
sl@0
   611
		b->request = 0;
sl@0
   612
		ret = 1;
sl@0
   613
		break;
sl@0
   614
sl@0
   615
	case BIO_C_SHUTDOWN_WR:
sl@0
   616
		/* similar to shutdown(..., SHUT_WR) */
sl@0
   617
		b->closed = 1;
sl@0
   618
		ret = 1;
sl@0
   619
		break;
sl@0
   620
sl@0
   621
	case BIO_C_NREAD0:
sl@0
   622
		/* prepare for non-copying read */
sl@0
   623
		ret = (long) bio_nread0(bio, ptr);
sl@0
   624
		break;
sl@0
   625
		
sl@0
   626
	case BIO_C_NREAD:
sl@0
   627
		/* non-copying read */
sl@0
   628
		ret = (long) bio_nread(bio, ptr, (size_t) num);
sl@0
   629
		break;
sl@0
   630
		
sl@0
   631
	case BIO_C_NWRITE0:
sl@0
   632
		/* prepare for non-copying write */
sl@0
   633
		ret = (long) bio_nwrite0(bio, ptr);
sl@0
   634
		break;
sl@0
   635
sl@0
   636
	case BIO_C_NWRITE:
sl@0
   637
		/* non-copying write */
sl@0
   638
		ret = (long) bio_nwrite(bio, ptr, (size_t) num);
sl@0
   639
		break;
sl@0
   640
		
sl@0
   641
sl@0
   642
	/* standard CTRL codes follow */
sl@0
   643
sl@0
   644
	case BIO_CTRL_RESET:
sl@0
   645
		if (b->buf != NULL)
sl@0
   646
			{
sl@0
   647
			b->len = 0;
sl@0
   648
			b->offset = 0;
sl@0
   649
			}
sl@0
   650
		ret = 0;
sl@0
   651
		break;		
sl@0
   652
sl@0
   653
	case BIO_CTRL_GET_CLOSE:
sl@0
   654
		ret = bio->shutdown;
sl@0
   655
		break;
sl@0
   656
sl@0
   657
	case BIO_CTRL_SET_CLOSE:
sl@0
   658
		bio->shutdown = (int) num;
sl@0
   659
		ret = 1;
sl@0
   660
		break;
sl@0
   661
sl@0
   662
	case BIO_CTRL_PENDING:
sl@0
   663
		if (b->peer != NULL)
sl@0
   664
			{
sl@0
   665
			struct bio_bio_st *peer_b = b->peer->ptr;
sl@0
   666
			
sl@0
   667
			ret = (long) peer_b->len;
sl@0
   668
			}
sl@0
   669
		else
sl@0
   670
			ret = 0;
sl@0
   671
		break;
sl@0
   672
sl@0
   673
	case BIO_CTRL_WPENDING:
sl@0
   674
		if (b->buf != NULL)
sl@0
   675
			ret = (long) b->len;
sl@0
   676
		else
sl@0
   677
			ret = 0;
sl@0
   678
		break;
sl@0
   679
sl@0
   680
	case BIO_CTRL_DUP:
sl@0
   681
		/* See BIO_dup_chain for circumstances we have to expect. */
sl@0
   682
		{
sl@0
   683
		BIO *other_bio = ptr;
sl@0
   684
		struct bio_bio_st *other_b;
sl@0
   685
		
sl@0
   686
		assert(other_bio != NULL);
sl@0
   687
		other_b = other_bio->ptr;
sl@0
   688
		assert(other_b != NULL);
sl@0
   689
		
sl@0
   690
		assert(other_b->buf == NULL); /* other_bio is always fresh */
sl@0
   691
sl@0
   692
		other_b->size = b->size;
sl@0
   693
		}
sl@0
   694
sl@0
   695
		ret = 1;
sl@0
   696
		break;
sl@0
   697
sl@0
   698
	case BIO_CTRL_FLUSH:
sl@0
   699
		ret = 1;
sl@0
   700
		break;
sl@0
   701
sl@0
   702
	case BIO_CTRL_EOF:
sl@0
   703
		{
sl@0
   704
		BIO *other_bio = ptr;
sl@0
   705
		
sl@0
   706
		if (other_bio)
sl@0
   707
			{
sl@0
   708
			struct bio_bio_st *other_b = other_bio->ptr;
sl@0
   709
			
sl@0
   710
			assert(other_b != NULL);
sl@0
   711
			ret = other_b->len == 0 && other_b->closed;
sl@0
   712
			}
sl@0
   713
		else
sl@0
   714
			ret = 1;
sl@0
   715
		}
sl@0
   716
		break;
sl@0
   717
sl@0
   718
	default:
sl@0
   719
		ret = 0;
sl@0
   720
		}
sl@0
   721
	return ret;
sl@0
   722
	}
sl@0
   723
sl@0
   724
static int bio_puts(BIO *bio, const char *str)
sl@0
   725
	{
sl@0
   726
	return bio_write(bio, str, strlen(str));
sl@0
   727
	}
sl@0
   728
sl@0
   729
sl@0
   730
static int bio_make_pair(BIO *bio1, BIO *bio2)
sl@0
   731
	{
sl@0
   732
	struct bio_bio_st *b1, *b2;
sl@0
   733
sl@0
   734
	assert(bio1 != NULL);
sl@0
   735
	assert(bio2 != NULL);
sl@0
   736
sl@0
   737
	b1 = bio1->ptr;
sl@0
   738
	b2 = bio2->ptr;
sl@0
   739
	
sl@0
   740
	if (b1->peer != NULL || b2->peer != NULL)
sl@0
   741
		{
sl@0
   742
		BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
sl@0
   743
		return 0;
sl@0
   744
		}
sl@0
   745
	
sl@0
   746
	if (b1->buf == NULL)
sl@0
   747
		{
sl@0
   748
		b1->buf = OPENSSL_malloc(b1->size);
sl@0
   749
		if (b1->buf == NULL)
sl@0
   750
			{
sl@0
   751
			BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
sl@0
   752
			return 0;
sl@0
   753
			}
sl@0
   754
		b1->len = 0;
sl@0
   755
		b1->offset = 0;
sl@0
   756
		}
sl@0
   757
	
sl@0
   758
	if (b2->buf == NULL)
sl@0
   759
		{
sl@0
   760
		b2->buf = OPENSSL_malloc(b2->size);
sl@0
   761
		if (b2->buf == NULL)
sl@0
   762
			{
sl@0
   763
			BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
sl@0
   764
			return 0;
sl@0
   765
			}
sl@0
   766
		b2->len = 0;
sl@0
   767
		b2->offset = 0;
sl@0
   768
		}
sl@0
   769
	
sl@0
   770
	b1->peer = bio2;
sl@0
   771
	b1->closed = 0;
sl@0
   772
	b1->request = 0;
sl@0
   773
	b2->peer = bio1;
sl@0
   774
	b2->closed = 0;
sl@0
   775
	b2->request = 0;
sl@0
   776
sl@0
   777
	bio1->init = 1;
sl@0
   778
	bio2->init = 1;
sl@0
   779
sl@0
   780
	return 1;
sl@0
   781
	}
sl@0
   782
sl@0
   783
static void bio_destroy_pair(BIO *bio)
sl@0
   784
	{
sl@0
   785
	struct bio_bio_st *b = bio->ptr;
sl@0
   786
sl@0
   787
	if (b != NULL)
sl@0
   788
		{
sl@0
   789
		BIO *peer_bio = b->peer;
sl@0
   790
sl@0
   791
		if (peer_bio != NULL)
sl@0
   792
			{
sl@0
   793
			struct bio_bio_st *peer_b = peer_bio->ptr;
sl@0
   794
sl@0
   795
			assert(peer_b != NULL);
sl@0
   796
			assert(peer_b->peer == bio);
sl@0
   797
sl@0
   798
			peer_b->peer = NULL;
sl@0
   799
			peer_bio->init = 0;
sl@0
   800
			assert(peer_b->buf != NULL);
sl@0
   801
			peer_b->len = 0;
sl@0
   802
			peer_b->offset = 0;
sl@0
   803
			
sl@0
   804
			b->peer = NULL;
sl@0
   805
			bio->init = 0;
sl@0
   806
			assert(b->buf != NULL);
sl@0
   807
			b->len = 0;
sl@0
   808
			b->offset = 0;
sl@0
   809
			}
sl@0
   810
		}
sl@0
   811
	}
sl@0
   812
 
sl@0
   813
sl@0
   814
/* Exported convenience functions */
sl@0
   815
EXPORT_C int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
sl@0
   816
	BIO **bio2_p, size_t writebuf2)
sl@0
   817
	 {
sl@0
   818
	 BIO *bio1 = NULL, *bio2 = NULL;
sl@0
   819
	 long r;
sl@0
   820
	 int ret = 0;
sl@0
   821
sl@0
   822
	 bio1 = BIO_new(BIO_s_bio());
sl@0
   823
	 if (bio1 == NULL)
sl@0
   824
		 goto err;
sl@0
   825
	 bio2 = BIO_new(BIO_s_bio());
sl@0
   826
	 if (bio2 == NULL)
sl@0
   827
		 goto err;
sl@0
   828
sl@0
   829
	 if (writebuf1)
sl@0
   830
		 {
sl@0
   831
		 r = BIO_set_write_buf_size(bio1, writebuf1);
sl@0
   832
		 if (!r)
sl@0
   833
			 goto err;
sl@0
   834
		 }
sl@0
   835
	 if (writebuf2)
sl@0
   836
		 {
sl@0
   837
		 r = BIO_set_write_buf_size(bio2, writebuf2);
sl@0
   838
		 if (!r)
sl@0
   839
			 goto err;
sl@0
   840
		 }
sl@0
   841
sl@0
   842
	 r = BIO_make_bio_pair(bio1, bio2);
sl@0
   843
	 if (!r)
sl@0
   844
		 goto err;
sl@0
   845
	 ret = 1;
sl@0
   846
sl@0
   847
 err:
sl@0
   848
	 if (ret == 0)
sl@0
   849
		 {
sl@0
   850
		 if (bio1)
sl@0
   851
			 {
sl@0
   852
			 BIO_free(bio1);
sl@0
   853
			 bio1 = NULL;
sl@0
   854
			 }
sl@0
   855
		 if (bio2)
sl@0
   856
			 {
sl@0
   857
			 BIO_free(bio2);
sl@0
   858
			 bio2 = NULL;
sl@0
   859
			 }
sl@0
   860
		 }
sl@0
   861
sl@0
   862
	 *bio1_p = bio1;
sl@0
   863
	 *bio2_p = bio2;
sl@0
   864
	 return ret;
sl@0
   865
	 }
sl@0
   866
sl@0
   867
EXPORT_C size_t BIO_ctrl_get_write_guarantee(BIO *bio)
sl@0
   868
	{
sl@0
   869
	return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
sl@0
   870
	}
sl@0
   871
sl@0
   872
EXPORT_C size_t BIO_ctrl_get_read_request(BIO *bio)
sl@0
   873
	{
sl@0
   874
	return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
sl@0
   875
	}
sl@0
   876
sl@0
   877
EXPORT_C int BIO_ctrl_reset_read_request(BIO *bio)
sl@0
   878
	{
sl@0
   879
	return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0);
sl@0
   880
	}
sl@0
   881
sl@0
   882
sl@0
   883
/* BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
sl@0
   884
 * (conceivably some other BIOs could allow non-copying reads and writes too.)
sl@0
   885
 */
sl@0
   886
EXPORT_C int BIO_nread0(BIO *bio, char **buf)
sl@0
   887
	{
sl@0
   888
	long ret;
sl@0
   889
sl@0
   890
	if (!bio->init)
sl@0
   891
		{
sl@0
   892
		BIOerr(BIO_F_BIO_NREAD0, BIO_R_UNINITIALIZED);
sl@0
   893
		return -2;
sl@0
   894
		}
sl@0
   895
sl@0
   896
	ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf);
sl@0
   897
	if (ret > INT_MAX)
sl@0
   898
		return INT_MAX;
sl@0
   899
	else
sl@0
   900
		return (int) ret;
sl@0
   901
	}
sl@0
   902
sl@0
   903
EXPORT_C int BIO_nread(BIO *bio, char **buf, int num)
sl@0
   904
	{
sl@0
   905
	int ret;
sl@0
   906
sl@0
   907
	if (!bio->init)
sl@0
   908
		{
sl@0
   909
		BIOerr(BIO_F_BIO_NREAD, BIO_R_UNINITIALIZED);
sl@0
   910
		return -2;
sl@0
   911
		}
sl@0
   912
sl@0
   913
	ret = (int) BIO_ctrl(bio, BIO_C_NREAD, num, buf);
sl@0
   914
	if (ret > 0)
sl@0
   915
		bio->num_read += ret;
sl@0
   916
	return ret;
sl@0
   917
	}
sl@0
   918
sl@0
   919
EXPORT_C int BIO_nwrite0(BIO *bio, char **buf)
sl@0
   920
	{
sl@0
   921
	long ret;
sl@0
   922
sl@0
   923
	if (!bio->init)
sl@0
   924
		{
sl@0
   925
		BIOerr(BIO_F_BIO_NWRITE0, BIO_R_UNINITIALIZED);
sl@0
   926
		return -2;
sl@0
   927
		}
sl@0
   928
sl@0
   929
	ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf);
sl@0
   930
	if (ret > INT_MAX)
sl@0
   931
		return INT_MAX;
sl@0
   932
	else
sl@0
   933
		return (int) ret;
sl@0
   934
	}
sl@0
   935
sl@0
   936
EXPORT_C int BIO_nwrite(BIO *bio, char **buf, int num)
sl@0
   937
	{
sl@0
   938
	int ret;
sl@0
   939
sl@0
   940
	if (!bio->init)
sl@0
   941
		{
sl@0
   942
		BIOerr(BIO_F_BIO_NWRITE, BIO_R_UNINITIALIZED);
sl@0
   943
		return -2;
sl@0
   944
		}
sl@0
   945
sl@0
   946
	ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf);
sl@0
   947
	if (ret > 0)
sl@0
   948
		bio->num_read += ret;
sl@0
   949
	return ret;
sl@0
   950
	}