Update contrib.
1 /* crypto/bio/bss_bio.c -*- Mode: C; c-file-style: "eay" -*- */
2 /* ====================================================================
3 * Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@openssl.org.
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
56 © Portions copyright (c) 2006 Nokia Corporation. All rights reserved.
59 /* Special method for a BIO where the other endpoint is also a BIO
60 * of this kind, handled by the same thread (i.e. the "peer" is actually
61 * ourselves, wearing a different hat).
62 * Such "BIO pairs" are mainly for using the SSL library with I/O interfaces
63 * for which no specific BIO method is available.
64 * See ssl/ssltest.c for some hints on how this can be used. */
66 /* BIO_DEBUG implies BIO_PAIR_DEBUG */
68 # ifndef BIO_PAIR_DEBUG
69 # define BIO_PAIR_DEBUG
73 /* disable assert() unless BIO_PAIR_DEBUG has been defined */
74 #ifndef BIO_PAIR_DEBUG
85 #include <openssl/bio.h>
86 #include <openssl/err.h>
87 #include <openssl/crypto.h>
90 #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
91 #include "libcrypto_wsd_macros.h"
92 #include "libcrypto_wsd.h"
95 /* VxWorks defines SSIZE_MAX with an empty value causing compile errors */
96 #if defined(OPENSSL_SYS_VXWORKS)
100 # define SSIZE_MAX INT_MAX
103 static int bio_new(BIO *bio);
104 static int bio_free(BIO *bio);
105 static int bio_read(BIO *bio, char *buf, int size);
106 static int bio_write(BIO *bio, const char *buf, int num);
107 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr);
108 static int bio_puts(BIO *bio, const char *str);
110 static int bio_make_pair(BIO *bio1, BIO *bio2);
111 static void bio_destroy_pair(BIO *bio);
114 static BIO_METHOD methods_biop =
121 NULL /* no bio_gets */,
125 NULL /* no bio_callback_ctrl */
129 GET_STATIC_VAR_FROM_TLS(methods_biop,bss_bio,BIO_METHOD)
130 #define methods_biop (*GET_WSD_VAR_NAME(methods_biop,bss_bio,s)())
131 const BIO_METHOD temp_s_methods_biop =
138 NULL /* no bio_gets */,
142 NULL /* no bio_callback_ctrl */
146 EXPORT_C BIO_METHOD *BIO_s_bio(void)
148 return &methods_biop;
153 BIO *peer; /* NULL if buf == NULL.
154 * If peer != NULL, then peer->ptr is also a bio_bio_st,
155 * and its "peer" member points back to us.
156 * peer != NULL iff init != 0 in the BIO. */
158 /* This is for what we write (i.e. reading uses peer's struct): */
159 int closed; /* valid iff peer != NULL */
160 size_t len; /* valid iff buf != NULL; 0 if peer == NULL */
161 size_t offset; /* valid iff buf != NULL; 0 if len == 0 */
163 char *buf; /* "size" elements (if != NULL) */
165 size_t request; /* valid iff peer != NULL; 0 if len != 0,
166 * otherwise set by peer to number of bytes
167 * it (unsuccessfully) tried to read,
168 * never more than buffer space (size-len) warrants. */
171 static int bio_new(BIO *bio)
173 struct bio_bio_st *b;
175 b = OPENSSL_malloc(sizeof *b);
180 b->size = 17*1024; /* enough for one TLS record (just a default) */
188 static int bio_free(BIO *bio)
190 struct bio_bio_st *b;
199 bio_destroy_pair(bio);
203 OPENSSL_free(b->buf);
213 static int bio_read(BIO *bio, char *buf, int size_)
217 struct bio_bio_st *b, *peer_b;
219 BIO_clear_retry_flags(bio);
226 assert(b->peer != NULL);
227 peer_b = b->peer->ptr;
228 assert(peer_b != NULL);
229 assert(peer_b->buf != NULL);
231 peer_b->request = 0; /* will be set in "retry_read" situation */
233 if (buf == NULL || size == 0)
236 if (peer_b->len == 0)
239 return 0; /* writer has closed, and no data is left */
242 BIO_set_retry_read(bio); /* buffer is empty */
243 if (size <= peer_b->size)
244 peer_b->request = size;
246 /* don't ask for more than the peer can
247 * deliver in one write */
248 peer_b->request = peer_b->size;
254 if (peer_b->len < size)
257 /* now read "size" bytes */
262 do /* one or two iterations */
266 assert(rest <= peer_b->len);
267 if (peer_b->offset + rest <= peer_b->size)
270 /* wrap around ring buffer */
271 chunk = peer_b->size - peer_b->offset;
272 assert(peer_b->offset + chunk <= peer_b->size);
274 memcpy(buf, peer_b->buf + peer_b->offset, chunk);
276 peer_b->len -= chunk;
279 peer_b->offset += chunk;
280 assert(peer_b->offset <= peer_b->size);
281 if (peer_b->offset == peer_b->size)
287 /* buffer now empty, no need to advance "buf" */
288 assert(chunk == rest);
298 /* non-copying interface: provide pointer to available data in buffer
299 * bio_nread0: return number of available bytes
300 * bio_nread: also advance index
301 * (example usage: bio_nread0(), read from buffer, bio_nread()
302 * or just bio_nread(), read from buffer)
304 /* WARNING: The non-copying interface is largely untested as of yet
305 * and may contain bugs. */
306 static ssize_t bio_nread0(BIO *bio, char **buf)
308 struct bio_bio_st *b, *peer_b;
311 BIO_clear_retry_flags(bio);
318 assert(b->peer != NULL);
319 peer_b = b->peer->ptr;
320 assert(peer_b != NULL);
321 assert(peer_b->buf != NULL);
325 if (peer_b->len == 0)
329 /* avoid code duplication -- nothing available for reading */
330 return bio_read(bio, &dummy, 1); /* returns 0 or -1 */
334 if (peer_b->size < peer_b->offset + num)
335 /* no ring buffer wrap-around for non-copying interface */
336 num = peer_b->size - peer_b->offset;
340 *buf = peer_b->buf + peer_b->offset;
344 static ssize_t bio_nread(BIO *bio, char **buf, size_t num_)
346 struct bio_bio_st *b, *peer_b;
347 ssize_t num, available;
349 if (num_ > SSIZE_MAX)
354 available = bio_nread0(bio, buf);
361 peer_b = b->peer->ptr;
366 peer_b->offset += num;
367 assert(peer_b->offset <= peer_b->size);
368 if (peer_b->offset == peer_b->size)
378 static int bio_write(BIO *bio, const char *buf, int num_)
382 struct bio_bio_st *b;
384 BIO_clear_retry_flags(bio);
386 if (!bio->init || buf == NULL || num == 0)
391 assert(b->peer != NULL);
392 assert(b->buf != NULL);
397 /* we already closed */
398 BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
402 assert(b->len <= b->size);
404 if (b->len == b->size)
406 BIO_set_retry_write(bio); /* buffer is full */
411 if (num > b->size - b->len)
412 num = b->size - b->len;
414 /* now write "num" bytes */
419 do /* one or two iterations */
424 assert(b->len + rest <= b->size);
426 write_offset = b->offset + b->len;
427 if (write_offset >= b->size)
428 write_offset -= b->size;
429 /* b->buf[write_offset] is the first byte we can write to. */
431 if (write_offset + rest <= b->size)
434 /* wrap around ring buffer */
435 chunk = b->size - write_offset;
437 memcpy(b->buf + write_offset, buf, chunk);
441 assert(b->len <= b->size);
451 /* non-copying interface: provide pointer to region to write to
452 * bio_nwrite0: check how much space is available
453 * bio_nwrite: also increase length
454 * (example usage: bio_nwrite0(), write to buffer, bio_nwrite()
455 * or just bio_nwrite(), write to buffer)
457 static ssize_t bio_nwrite0(BIO *bio, char **buf)
459 struct bio_bio_st *b;
463 BIO_clear_retry_flags(bio);
470 assert(b->peer != NULL);
471 assert(b->buf != NULL);
476 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_BROKEN_PIPE);
480 assert(b->len <= b->size);
482 if (b->len == b->size)
484 BIO_set_retry_write(bio);
488 num = b->size - b->len;
489 write_offset = b->offset + b->len;
490 if (write_offset >= b->size)
491 write_offset -= b->size;
492 if (write_offset + num > b->size)
493 /* no ring buffer wrap-around for non-copying interface
494 * (to fulfil the promise by BIO_ctrl_get_write_guarantee,
495 * BIO_nwrite may have to be called twice) */
496 num = b->size - write_offset;
499 *buf = b->buf + write_offset;
500 assert(write_offset + num <= b->size);
505 static ssize_t bio_nwrite(BIO *bio, char **buf, size_t num_)
507 struct bio_bio_st *b;
510 if (num_ > SSIZE_MAX)
515 space = bio_nwrite0(bio, buf);
523 assert(b->len <= b->size);
529 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
532 struct bio_bio_st *b = bio->ptr;
538 /* specific CTRL codes */
540 case BIO_C_SET_WRITE_BUF_SIZE:
543 BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
548 BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
553 size_t new_size = num;
555 if (b->size != new_size)
559 OPENSSL_free(b->buf);
568 case BIO_C_GET_WRITE_BUF_SIZE:
569 ret = (long) b->size;
572 case BIO_C_MAKE_BIO_PAIR:
574 BIO *other_bio = ptr;
576 if (bio_make_pair(bio, other_bio))
583 case BIO_C_DESTROY_BIO_PAIR:
584 /* Affects both BIOs in the pair -- call just once!
585 * Or let BIO_free(bio1); BIO_free(bio2); do the job. */
586 bio_destroy_pair(bio);
590 case BIO_C_GET_WRITE_GUARANTEE:
591 /* How many bytes can the caller feed to the next write
592 * without having to keep any? */
593 if (b->peer == NULL || b->closed)
596 ret = (long) b->size - b->len;
599 case BIO_C_GET_READ_REQUEST:
600 /* If the peer unsuccessfully tried to read, how many bytes
601 * were requested? (As with BIO_CTRL_PENDING, that number
602 * can usually be treated as boolean.) */
603 ret = (long) b->request;
606 case BIO_C_RESET_READ_REQUEST:
607 /* Reset request. (Can be useful after read attempts
608 * at the other side that are meant to be non-blocking,
609 * e.g. when probing SSL_read to see if any data is
615 case BIO_C_SHUTDOWN_WR:
616 /* similar to shutdown(..., SHUT_WR) */
622 /* prepare for non-copying read */
623 ret = (long) bio_nread0(bio, ptr);
627 /* non-copying read */
628 ret = (long) bio_nread(bio, ptr, (size_t) num);
632 /* prepare for non-copying write */
633 ret = (long) bio_nwrite0(bio, ptr);
637 /* non-copying write */
638 ret = (long) bio_nwrite(bio, ptr, (size_t) num);
642 /* standard CTRL codes follow */
653 case BIO_CTRL_GET_CLOSE:
657 case BIO_CTRL_SET_CLOSE:
658 bio->shutdown = (int) num;
662 case BIO_CTRL_PENDING:
665 struct bio_bio_st *peer_b = b->peer->ptr;
667 ret = (long) peer_b->len;
673 case BIO_CTRL_WPENDING:
681 /* See BIO_dup_chain for circumstances we have to expect. */
683 BIO *other_bio = ptr;
684 struct bio_bio_st *other_b;
686 assert(other_bio != NULL);
687 other_b = other_bio->ptr;
688 assert(other_b != NULL);
690 assert(other_b->buf == NULL); /* other_bio is always fresh */
692 other_b->size = b->size;
704 BIO *other_bio = ptr;
708 struct bio_bio_st *other_b = other_bio->ptr;
710 assert(other_b != NULL);
711 ret = other_b->len == 0 && other_b->closed;
724 static int bio_puts(BIO *bio, const char *str)
726 return bio_write(bio, str, strlen(str));
730 static int bio_make_pair(BIO *bio1, BIO *bio2)
732 struct bio_bio_st *b1, *b2;
734 assert(bio1 != NULL);
735 assert(bio2 != NULL);
740 if (b1->peer != NULL || b2->peer != NULL)
742 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
748 b1->buf = OPENSSL_malloc(b1->size);
751 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
760 b2->buf = OPENSSL_malloc(b2->size);
763 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
783 static void bio_destroy_pair(BIO *bio)
785 struct bio_bio_st *b = bio->ptr;
789 BIO *peer_bio = b->peer;
791 if (peer_bio != NULL)
793 struct bio_bio_st *peer_b = peer_bio->ptr;
795 assert(peer_b != NULL);
796 assert(peer_b->peer == bio);
800 assert(peer_b->buf != NULL);
806 assert(b->buf != NULL);
814 /* Exported convenience functions */
815 EXPORT_C int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
816 BIO **bio2_p, size_t writebuf2)
818 BIO *bio1 = NULL, *bio2 = NULL;
822 bio1 = BIO_new(BIO_s_bio());
825 bio2 = BIO_new(BIO_s_bio());
831 r = BIO_set_write_buf_size(bio1, writebuf1);
837 r = BIO_set_write_buf_size(bio2, writebuf2);
842 r = BIO_make_bio_pair(bio1, bio2);
867 EXPORT_C size_t BIO_ctrl_get_write_guarantee(BIO *bio)
869 return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
872 EXPORT_C size_t BIO_ctrl_get_read_request(BIO *bio)
874 return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
877 EXPORT_C int BIO_ctrl_reset_read_request(BIO *bio)
879 return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0);
883 /* BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
884 * (conceivably some other BIOs could allow non-copying reads and writes too.)
886 EXPORT_C int BIO_nread0(BIO *bio, char **buf)
892 BIOerr(BIO_F_BIO_NREAD0, BIO_R_UNINITIALIZED);
896 ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf);
903 EXPORT_C int BIO_nread(BIO *bio, char **buf, int num)
909 BIOerr(BIO_F_BIO_NREAD, BIO_R_UNINITIALIZED);
913 ret = (int) BIO_ctrl(bio, BIO_C_NREAD, num, buf);
915 bio->num_read += ret;
919 EXPORT_C int BIO_nwrite0(BIO *bio, char **buf)
925 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_UNINITIALIZED);
929 ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf);
936 EXPORT_C int BIO_nwrite(BIO *bio, char **buf, int num)
942 BIOerr(BIO_F_BIO_NWRITE, BIO_R_UNINITIALIZED);
946 ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf);
948 bio->num_read += ret;