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