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