blob: d629a37a5a1dbfa85efbc1506f4969e828af52a9 [file] [log] [blame]
Bodo Möller9e06f6f1999-06-07 16:04:45 +00001/* crypto/bio/bss_bio.c -*- Mode: C; c-file-style: "eay" -*- */
Bodo Möller3aa8d3a2003-08-06 10:36:25 +00002/* ====================================================================
3 * Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
Matt Caswell0f113f32015-01-22 03:40:55 +000010 * notice, this list of conditions and the following disclaimer.
Bodo Möller3aa8d3a2003-08-06 10:36:25 +000011 *
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
15 * distribution.
16 *
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/)"
21 *
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.
26 *
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.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 *
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 * ====================================================================
49 *
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).
53 *
54 */
Bodo Möller9e06f6f1999-06-07 16:04:45 +000055
Matt Caswell0f113f32015-01-22 03:40:55 +000056/*
57 * Special method for a BIO where the other endpoint is also a BIO of this
58 * kind, handled by the same thread (i.e. the "peer" is actually ourselves,
59 * wearing a different hat). Such "BIO pairs" are mainly for using the SSL
60 * library with I/O interfaces for which no specific BIO method is available.
61 * See ssl/ssltest.c for some hints on how this can be used.
62 */
Bodo Möller9e06f6f1999-06-07 16:04:45 +000063
Bodo Möller60e5f362002-05-14 19:40:58 +000064/* BIO_DEBUG implies BIO_PAIR_DEBUG */
65#ifdef BIO_DEBUG
66# ifndef BIO_PAIR_DEBUG
67# define BIO_PAIR_DEBUG
68# endif
69#endif
70
71/* disable assert() unless BIO_PAIR_DEBUG has been defined */
Bodo Möllere334d781999-07-05 10:18:51 +000072#ifndef BIO_PAIR_DEBUG
Bodo Möller60e5f362002-05-14 19:40:58 +000073# ifndef NDEBUG
74# define NDEBUG
75# endif
Bodo Möllere334d781999-07-05 10:18:51 +000076#endif
77
Bodo Möller9e06f6f1999-06-07 16:04:45 +000078#include <assert.h>
Bodo Möllerc1082a91999-09-07 21:37:09 +000079#include <limits.h>
Bodo Möller9e06f6f1999-06-07 16:04:45 +000080#include <stdlib.h>
Bodo Möllerd58d0921999-06-10 16:29:32 +000081#include <string.h>
Bodo Möller9e06f6f1999-06-07 16:04:45 +000082
83#include <openssl/bio.h>
84#include <openssl/err.h>
85#include <openssl/crypto.h>
86
Richard Levitte41d2a332001-02-22 14:45:02 +000087#include "e_os.h"
Richard Levitte3e83e682002-02-14 15:37:38 +000088
89/* VxWorks defines SSIZE_MAX with an empty value causing compile errors */
Richard Levitte6a89a252002-10-09 13:40:48 +000090#if defined(OPENSSL_SYS_VXWORKS)
Richard Levitte3e83e682002-02-14 15:37:38 +000091# undef SSIZE_MAX
92#endif
Bodo Möller4991d072000-03-10 21:44:38 +000093#ifndef SSIZE_MAX
94# define SSIZE_MAX INT_MAX
95#endif
96
Bodo Möller9e06f6f1999-06-07 16:04:45 +000097static int bio_new(BIO *bio);
98static int bio_free(BIO *bio);
99static int bio_read(BIO *bio, char *buf, int size);
Ulf Möller0e1c0612000-05-15 22:54:43 +0000100static int bio_write(BIO *bio, const char *buf, int num);
Bodo Möller95d29591999-06-12 01:03:40 +0000101static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr);
Ulf Möller0e1c0612000-05-15 22:54:43 +0000102static int bio_puts(BIO *bio, const char *str);
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000103
104static int bio_make_pair(BIO *bio1, BIO *bio2);
105static void bio_destroy_pair(BIO *bio);
106
Matt Caswell0f113f32015-01-22 03:40:55 +0000107static BIO_METHOD methods_biop = {
108 BIO_TYPE_BIO,
109 "BIO pair",
110 bio_write,
111 bio_read,
112 bio_puts,
113 NULL /* no bio_gets */ ,
114 bio_ctrl,
115 bio_new,
116 bio_free,
117 NULL /* no bio_callback_ctrl */
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000118};
119
120BIO_METHOD *BIO_s_bio(void)
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000121{
Matt Caswell0f113f32015-01-22 03:40:55 +0000122 return &methods_biop;
123}
Bodo Möllerc035b0c1999-06-08 10:18:48 +0000124
Matt Caswell0f113f32015-01-22 03:40:55 +0000125struct bio_bio_st {
126 BIO *peer; /* NULL if buf == NULL. If peer != NULL, then
127 * peer->ptr is also a bio_bio_st, and its
128 * "peer" member points back to us. peer !=
129 * NULL iff init != 0 in the BIO. */
130 /* This is for what we write (i.e. reading uses peer's struct): */
131 int closed; /* valid iff peer != NULL */
132 size_t len; /* valid iff buf != NULL; 0 if peer == NULL */
133 size_t offset; /* valid iff buf != NULL; 0 if len == 0 */
134 size_t size;
135 char *buf; /* "size" elements (if != NULL) */
136 size_t request; /* valid iff peer != NULL; 0 if len != 0,
137 * otherwise set by peer to number of bytes
138 * it (unsuccessfully) tried to read, never
139 * more than buffer space (size-len)
140 * warrants. */
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000141};
142
143static int bio_new(BIO *bio)
Matt Caswell0f113f32015-01-22 03:40:55 +0000144{
145 struct bio_bio_st *b;
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000146
Matt Caswell0f113f32015-01-22 03:40:55 +0000147 b = OPENSSL_malloc(sizeof *b);
148 if (b == NULL)
149 return 0;
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000150
Matt Caswell0f113f32015-01-22 03:40:55 +0000151 b->peer = NULL;
152 /* enough for one TLS record (just a default) */
153 b->size = 17 * 1024;
154 b->buf = NULL;
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000155
Matt Caswell0f113f32015-01-22 03:40:55 +0000156 bio->ptr = b;
157 return 1;
158}
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000159
160static int bio_free(BIO *bio)
Matt Caswell0f113f32015-01-22 03:40:55 +0000161{
162 struct bio_bio_st *b;
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000163
Matt Caswell0f113f32015-01-22 03:40:55 +0000164 if (bio == NULL)
165 return 0;
166 b = bio->ptr;
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000167
Matt Caswell0f113f32015-01-22 03:40:55 +0000168 assert(b != NULL);
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000169
Matt Caswell0f113f32015-01-22 03:40:55 +0000170 if (b->peer)
171 bio_destroy_pair(bio);
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000172
Matt Caswell0f113f32015-01-22 03:40:55 +0000173 if (b->buf != NULL) {
174 OPENSSL_free(b->buf);
175 }
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000176
Matt Caswell0f113f32015-01-22 03:40:55 +0000177 OPENSSL_free(b);
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000178
Matt Caswell0f113f32015-01-22 03:40:55 +0000179 return 1;
180}
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000181
Bodo Möller95d29591999-06-12 01:03:40 +0000182static int bio_read(BIO *bio, char *buf, int size_)
Matt Caswell0f113f32015-01-22 03:40:55 +0000183{
184 size_t size = size_;
185 size_t rest;
186 struct bio_bio_st *b, *peer_b;
Bodo Möller95d29591999-06-12 01:03:40 +0000187
Matt Caswell0f113f32015-01-22 03:40:55 +0000188 BIO_clear_retry_flags(bio);
Bodo Möller95d29591999-06-12 01:03:40 +0000189
Matt Caswell0f113f32015-01-22 03:40:55 +0000190 if (!bio->init)
191 return 0;
Bodo Möller95d29591999-06-12 01:03:40 +0000192
Matt Caswell0f113f32015-01-22 03:40:55 +0000193 b = bio->ptr;
194 assert(b != NULL);
195 assert(b->peer != NULL);
196 peer_b = b->peer->ptr;
197 assert(peer_b != NULL);
198 assert(peer_b->buf != NULL);
Bodo Möller95d29591999-06-12 01:03:40 +0000199
Matt Caswell0f113f32015-01-22 03:40:55 +0000200 peer_b->request = 0; /* will be set in "retry_read" situation */
Bodo Möller95d29591999-06-12 01:03:40 +0000201
Matt Caswell0f113f32015-01-22 03:40:55 +0000202 if (buf == NULL || size == 0)
203 return 0;
Bodo Möller95d29591999-06-12 01:03:40 +0000204
Matt Caswell0f113f32015-01-22 03:40:55 +0000205 if (peer_b->len == 0) {
206 if (peer_b->closed)
207 return 0; /* writer has closed, and no data is left */
208 else {
209 BIO_set_retry_read(bio); /* buffer is empty */
210 if (size <= peer_b->size)
211 peer_b->request = size;
212 else
213 /*
214 * don't ask for more than the peer can deliver in one write
215 */
216 peer_b->request = peer_b->size;
217 return -1;
218 }
219 }
Bodo Möller95d29591999-06-12 01:03:40 +0000220
Matt Caswell0f113f32015-01-22 03:40:55 +0000221 /* we can read */
222 if (peer_b->len < size)
223 size = peer_b->len;
Bodo Möller95d29591999-06-12 01:03:40 +0000224
Matt Caswell0f113f32015-01-22 03:40:55 +0000225 /* now read "size" bytes */
226
227 rest = size;
228
229 assert(rest > 0);
230 do { /* one or two iterations */
231 size_t chunk;
232
233 assert(rest <= peer_b->len);
234 if (peer_b->offset + rest <= peer_b->size)
235 chunk = rest;
236 else
237 /* wrap around ring buffer */
238 chunk = peer_b->size - peer_b->offset;
239 assert(peer_b->offset + chunk <= peer_b->size);
240
241 memcpy(buf, peer_b->buf + peer_b->offset, chunk);
242
243 peer_b->len -= chunk;
244 if (peer_b->len) {
245 peer_b->offset += chunk;
246 assert(peer_b->offset <= peer_b->size);
247 if (peer_b->offset == peer_b->size)
248 peer_b->offset = 0;
249 buf += chunk;
250 } else {
251 /* buffer now empty, no need to advance "buf" */
252 assert(chunk == rest);
253 peer_b->offset = 0;
254 }
255 rest -= chunk;
256 }
257 while (rest);
258
259 return size;
260}
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000261
Tim Hudson1d97c842014-12-28 12:48:40 +1000262/*-
263 * non-copying interface: provide pointer to available data in buffer
Bodo Möllerc1082a91999-09-07 21:37:09 +0000264 * bio_nread0: return number of available bytes
265 * bio_nread: also advance index
266 * (example usage: bio_nread0(), read from buffer, bio_nread()
267 * or just bio_nread(), read from buffer)
268 */
Matt Caswell0f113f32015-01-22 03:40:55 +0000269/*
270 * WARNING: The non-copying interface is largely untested as of yet and may
271 * contain bugs.
272 */
Dr. Stephen Hensoneb1c48b2010-07-26 18:15:59 +0000273static ossl_ssize_t bio_nread0(BIO *bio, char **buf)
Matt Caswell0f113f32015-01-22 03:40:55 +0000274{
275 struct bio_bio_st *b, *peer_b;
276 ossl_ssize_t num;
Bodo Möllerc1082a91999-09-07 21:37:09 +0000277
Matt Caswell0f113f32015-01-22 03:40:55 +0000278 BIO_clear_retry_flags(bio);
Bodo Möllerc1082a91999-09-07 21:37:09 +0000279
Matt Caswell0f113f32015-01-22 03:40:55 +0000280 if (!bio->init)
281 return 0;
Bodo Möllerc1082a91999-09-07 21:37:09 +0000282
Matt Caswell0f113f32015-01-22 03:40:55 +0000283 b = bio->ptr;
284 assert(b != NULL);
285 assert(b->peer != NULL);
286 peer_b = b->peer->ptr;
287 assert(peer_b != NULL);
288 assert(peer_b->buf != NULL);
289
290 peer_b->request = 0;
291
292 if (peer_b->len == 0) {
293 char dummy;
294
295 /* avoid code duplication -- nothing available for reading */
296 return bio_read(bio, &dummy, 1); /* returns 0 or -1 */
297 }
298
299 num = peer_b->len;
300 if (peer_b->size < peer_b->offset + num)
301 /* no ring buffer wrap-around for non-copying interface */
302 num = peer_b->size - peer_b->offset;
303 assert(num > 0);
304
305 if (buf != NULL)
306 *buf = peer_b->buf + peer_b->offset;
307 return num;
308}
Bodo Möllerc1082a91999-09-07 21:37:09 +0000309
Dr. Stephen Hensoneb1c48b2010-07-26 18:15:59 +0000310static ossl_ssize_t bio_nread(BIO *bio, char **buf, size_t num_)
Matt Caswell0f113f32015-01-22 03:40:55 +0000311{
312 struct bio_bio_st *b, *peer_b;
313 ossl_ssize_t num, available;
Bodo Möller4991d072000-03-10 21:44:38 +0000314
Matt Caswell0f113f32015-01-22 03:40:55 +0000315 if (num_ > SSIZE_MAX)
316 num = SSIZE_MAX;
317 else
318 num = (ossl_ssize_t) num_;
Bodo Möllerc1082a91999-09-07 21:37:09 +0000319
Matt Caswell0f113f32015-01-22 03:40:55 +0000320 available = bio_nread0(bio, buf);
321 if (num > available)
322 num = available;
323 if (num <= 0)
324 return num;
Bodo Möllerc1082a91999-09-07 21:37:09 +0000325
Matt Caswell0f113f32015-01-22 03:40:55 +0000326 b = bio->ptr;
327 peer_b = b->peer->ptr;
Bodo Möllerc1082a91999-09-07 21:37:09 +0000328
Matt Caswell0f113f32015-01-22 03:40:55 +0000329 peer_b->len -= num;
330 if (peer_b->len) {
331 peer_b->offset += num;
332 assert(peer_b->offset <= peer_b->size);
333 if (peer_b->offset == peer_b->size)
334 peer_b->offset = 0;
335 } else
336 peer_b->offset = 0;
Bodo Möllerc1082a91999-09-07 21:37:09 +0000337
Matt Caswell0f113f32015-01-22 03:40:55 +0000338 return num;
339}
Bodo Möllerc1082a91999-09-07 21:37:09 +0000340
Ulf Möller0e1c0612000-05-15 22:54:43 +0000341static int bio_write(BIO *bio, const char *buf, int num_)
Matt Caswell0f113f32015-01-22 03:40:55 +0000342{
343 size_t num = num_;
344 size_t rest;
345 struct bio_bio_st *b;
Bodo Möller95d29591999-06-12 01:03:40 +0000346
Matt Caswell0f113f32015-01-22 03:40:55 +0000347 BIO_clear_retry_flags(bio);
Bodo Möller95d29591999-06-12 01:03:40 +0000348
Matt Caswell0f113f32015-01-22 03:40:55 +0000349 if (!bio->init || buf == NULL || num == 0)
350 return 0;
Bodo Möller95d29591999-06-12 01:03:40 +0000351
Matt Caswell0f113f32015-01-22 03:40:55 +0000352 b = bio->ptr;
353 assert(b != NULL);
354 assert(b->peer != NULL);
355 assert(b->buf != NULL);
Bodo Möller95d29591999-06-12 01:03:40 +0000356
Matt Caswell0f113f32015-01-22 03:40:55 +0000357 b->request = 0;
358 if (b->closed) {
359 /* we already closed */
360 BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE);
361 return -1;
362 }
Bodo Möller95d29591999-06-12 01:03:40 +0000363
Matt Caswell0f113f32015-01-22 03:40:55 +0000364 assert(b->len <= b->size);
Bodo Möller95d29591999-06-12 01:03:40 +0000365
Matt Caswell0f113f32015-01-22 03:40:55 +0000366 if (b->len == b->size) {
367 BIO_set_retry_write(bio); /* buffer is full */
368 return -1;
369 }
Bodo Möller95d29591999-06-12 01:03:40 +0000370
Matt Caswell0f113f32015-01-22 03:40:55 +0000371 /* we can write */
372 if (num > b->size - b->len)
373 num = b->size - b->len;
Bodo Möller95d29591999-06-12 01:03:40 +0000374
Matt Caswell0f113f32015-01-22 03:40:55 +0000375 /* now write "num" bytes */
Bodo Möller95d29591999-06-12 01:03:40 +0000376
Matt Caswell0f113f32015-01-22 03:40:55 +0000377 rest = num;
Bodo Möller95d29591999-06-12 01:03:40 +0000378
Matt Caswell0f113f32015-01-22 03:40:55 +0000379 assert(rest > 0);
380 do { /* one or two iterations */
381 size_t write_offset;
382 size_t chunk;
Bodo Möller95d29591999-06-12 01:03:40 +0000383
Matt Caswell0f113f32015-01-22 03:40:55 +0000384 assert(b->len + rest <= b->size);
Bodo Möller95d29591999-06-12 01:03:40 +0000385
Matt Caswell0f113f32015-01-22 03:40:55 +0000386 write_offset = b->offset + b->len;
387 if (write_offset >= b->size)
388 write_offset -= b->size;
389 /* b->buf[write_offset] is the first byte we can write to. */
Bodo Möller95d29591999-06-12 01:03:40 +0000390
Matt Caswell0f113f32015-01-22 03:40:55 +0000391 if (write_offset + rest <= b->size)
392 chunk = rest;
393 else
394 /* wrap around ring buffer */
395 chunk = b->size - write_offset;
396
397 memcpy(b->buf + write_offset, buf, chunk);
398
399 b->len += chunk;
400
401 assert(b->len <= b->size);
402
403 rest -= chunk;
404 buf += chunk;
405 }
406 while (rest);
407
408 return num;
409}
Bodo Möller95d29591999-06-12 01:03:40 +0000410
Tim Hudson1d97c842014-12-28 12:48:40 +1000411/*-
412 * non-copying interface: provide pointer to region to write to
Bodo Möllerc1082a91999-09-07 21:37:09 +0000413 * bio_nwrite0: check how much space is available
414 * bio_nwrite: also increase length
415 * (example usage: bio_nwrite0(), write to buffer, bio_nwrite()
416 * or just bio_nwrite(), write to buffer)
417 */
Dr. Stephen Hensoneb1c48b2010-07-26 18:15:59 +0000418static ossl_ssize_t bio_nwrite0(BIO *bio, char **buf)
Matt Caswell0f113f32015-01-22 03:40:55 +0000419{
420 struct bio_bio_st *b;
421 size_t num;
422 size_t write_offset;
Bodo Möllerc1082a91999-09-07 21:37:09 +0000423
Matt Caswell0f113f32015-01-22 03:40:55 +0000424 BIO_clear_retry_flags(bio);
Bodo Möllerc1082a91999-09-07 21:37:09 +0000425
Matt Caswell0f113f32015-01-22 03:40:55 +0000426 if (!bio->init)
427 return 0;
Bodo Möllerc1082a91999-09-07 21:37:09 +0000428
Matt Caswell0f113f32015-01-22 03:40:55 +0000429 b = bio->ptr;
430 assert(b != NULL);
431 assert(b->peer != NULL);
432 assert(b->buf != NULL);
Bodo Möllerc1082a91999-09-07 21:37:09 +0000433
Matt Caswell0f113f32015-01-22 03:40:55 +0000434 b->request = 0;
435 if (b->closed) {
436 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_BROKEN_PIPE);
437 return -1;
438 }
Bodo Möllerc1082a91999-09-07 21:37:09 +0000439
Matt Caswell0f113f32015-01-22 03:40:55 +0000440 assert(b->len <= b->size);
Bodo Möllerc1082a91999-09-07 21:37:09 +0000441
Matt Caswell0f113f32015-01-22 03:40:55 +0000442 if (b->len == b->size) {
443 BIO_set_retry_write(bio);
444 return -1;
445 }
Bodo Möllerc1082a91999-09-07 21:37:09 +0000446
Matt Caswell0f113f32015-01-22 03:40:55 +0000447 num = b->size - b->len;
448 write_offset = b->offset + b->len;
449 if (write_offset >= b->size)
450 write_offset -= b->size;
451 if (write_offset + num > b->size)
452 /*
453 * no ring buffer wrap-around for non-copying interface (to fulfil
454 * the promise by BIO_ctrl_get_write_guarantee, BIO_nwrite may have
455 * to be called twice)
456 */
457 num = b->size - write_offset;
Bodo Möllerc1082a91999-09-07 21:37:09 +0000458
Matt Caswell0f113f32015-01-22 03:40:55 +0000459 if (buf != NULL)
460 *buf = b->buf + write_offset;
461 assert(write_offset + num <= b->size);
Bodo Möllerc1082a91999-09-07 21:37:09 +0000462
Matt Caswell0f113f32015-01-22 03:40:55 +0000463 return num;
464}
Bodo Möllerc1082a91999-09-07 21:37:09 +0000465
Dr. Stephen Hensoneb1c48b2010-07-26 18:15:59 +0000466static ossl_ssize_t bio_nwrite(BIO *bio, char **buf, size_t num_)
Matt Caswell0f113f32015-01-22 03:40:55 +0000467{
468 struct bio_bio_st *b;
469 ossl_ssize_t num, space;
Bodo Möller4991d072000-03-10 21:44:38 +0000470
Matt Caswell0f113f32015-01-22 03:40:55 +0000471 if (num_ > SSIZE_MAX)
472 num = SSIZE_MAX;
473 else
474 num = (ossl_ssize_t) num_;
Bodo Möllerc1082a91999-09-07 21:37:09 +0000475
Matt Caswell0f113f32015-01-22 03:40:55 +0000476 space = bio_nwrite0(bio, buf);
477 if (num > space)
478 num = space;
479 if (num <= 0)
480 return num;
481 b = bio->ptr;
482 assert(b != NULL);
483 b->len += num;
484 assert(b->len <= b->size);
Bodo Möllerc1082a91999-09-07 21:37:09 +0000485
Matt Caswell0f113f32015-01-22 03:40:55 +0000486 return num;
487}
Bodo Möller95d29591999-06-12 01:03:40 +0000488
489static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr)
Matt Caswell0f113f32015-01-22 03:40:55 +0000490{
491 long ret;
492 struct bio_bio_st *b = bio->ptr;
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000493
Matt Caswell0f113f32015-01-22 03:40:55 +0000494 assert(b != NULL);
Bodo Möller95d29591999-06-12 01:03:40 +0000495
Matt Caswell0f113f32015-01-22 03:40:55 +0000496 switch (cmd) {
497 /* specific CTRL codes */
Bodo Möller95d29591999-06-12 01:03:40 +0000498
Matt Caswell0f113f32015-01-22 03:40:55 +0000499 case BIO_C_SET_WRITE_BUF_SIZE:
500 if (b->peer) {
501 BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE);
502 ret = 0;
503 } else if (num == 0) {
504 BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT);
505 ret = 0;
506 } else {
507 size_t new_size = num;
Bodo Möller95d29591999-06-12 01:03:40 +0000508
Matt Caswell0f113f32015-01-22 03:40:55 +0000509 if (b->size != new_size) {
510 if (b->buf) {
511 OPENSSL_free(b->buf);
512 b->buf = NULL;
513 }
514 b->size = new_size;
515 }
516 ret = 1;
517 }
518 break;
Bodo Möller95d29591999-06-12 01:03:40 +0000519
Matt Caswell0f113f32015-01-22 03:40:55 +0000520 case BIO_C_GET_WRITE_BUF_SIZE:
521 ret = (long)b->size;
522 break;
Bodo Möller95d29591999-06-12 01:03:40 +0000523
Matt Caswell0f113f32015-01-22 03:40:55 +0000524 case BIO_C_MAKE_BIO_PAIR:
525 {
526 BIO *other_bio = ptr;
Bodo Möller95d29591999-06-12 01:03:40 +0000527
Matt Caswell0f113f32015-01-22 03:40:55 +0000528 if (bio_make_pair(bio, other_bio))
529 ret = 1;
530 else
531 ret = 0;
532 }
533 break;
Bodo Möller95d29591999-06-12 01:03:40 +0000534
Matt Caswell0f113f32015-01-22 03:40:55 +0000535 case BIO_C_DESTROY_BIO_PAIR:
536 /*
537 * Affects both BIOs in the pair -- call just once! Or let
538 * BIO_free(bio1); BIO_free(bio2); do the job.
539 */
540 bio_destroy_pair(bio);
541 ret = 1;
542 break;
Bodo Möllere405b8d1999-09-27 13:43:59 +0000543
Matt Caswell0f113f32015-01-22 03:40:55 +0000544 case BIO_C_GET_WRITE_GUARANTEE:
545 /*
546 * How many bytes can the caller feed to the next write without
547 * having to keep any?
548 */
549 if (b->peer == NULL || b->closed)
550 ret = 0;
551 else
552 ret = (long)b->size - b->len;
553 break;
Bodo Möller95d29591999-06-12 01:03:40 +0000554
Matt Caswell0f113f32015-01-22 03:40:55 +0000555 case BIO_C_GET_READ_REQUEST:
556 /*
557 * If the peer unsuccessfully tried to read, how many bytes were
558 * requested? (As with BIO_CTRL_PENDING, that number can usually be
559 * treated as boolean.)
560 */
561 ret = (long)b->request;
562 break;
Bodo Möllerc1082a91999-09-07 21:37:09 +0000563
Matt Caswell0f113f32015-01-22 03:40:55 +0000564 case BIO_C_RESET_READ_REQUEST:
565 /*
566 * Reset request. (Can be useful after read attempts at the other
567 * side that are meant to be non-blocking, e.g. when probing SSL_read
568 * to see if any data is available.)
569 */
570 b->request = 0;
571 ret = 1;
572 break;
Bodo Möller95d29591999-06-12 01:03:40 +0000573
Matt Caswell0f113f32015-01-22 03:40:55 +0000574 case BIO_C_SHUTDOWN_WR:
575 /* similar to shutdown(..., SHUT_WR) */
576 b->closed = 1;
577 ret = 1;
578 break;
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000579
Matt Caswell0f113f32015-01-22 03:40:55 +0000580 case BIO_C_NREAD0:
581 /* prepare for non-copying read */
582 ret = (long)bio_nread0(bio, ptr);
583 break;
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000584
Matt Caswell0f113f32015-01-22 03:40:55 +0000585 case BIO_C_NREAD:
586 /* non-copying read */
587 ret = (long)bio_nread(bio, ptr, (size_t)num);
588 break;
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000589
Matt Caswell0f113f32015-01-22 03:40:55 +0000590 case BIO_C_NWRITE0:
591 /* prepare for non-copying write */
592 ret = (long)bio_nwrite0(bio, ptr);
593 break;
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000594
Matt Caswell0f113f32015-01-22 03:40:55 +0000595 case BIO_C_NWRITE:
596 /* non-copying write */
597 ret = (long)bio_nwrite(bio, ptr, (size_t)num);
598 break;
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000599
Matt Caswell0f113f32015-01-22 03:40:55 +0000600 /* standard CTRL codes follow */
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000601
Matt Caswell0f113f32015-01-22 03:40:55 +0000602 case BIO_CTRL_RESET:
603 if (b->buf != NULL) {
604 b->len = 0;
605 b->offset = 0;
606 }
607 ret = 0;
608 break;
Bodo Möller95d29591999-06-12 01:03:40 +0000609
Matt Caswell0f113f32015-01-22 03:40:55 +0000610 case BIO_CTRL_GET_CLOSE:
611 ret = bio->shutdown;
612 break;
Bodo Möller95d29591999-06-12 01:03:40 +0000613
Matt Caswell0f113f32015-01-22 03:40:55 +0000614 case BIO_CTRL_SET_CLOSE:
615 bio->shutdown = (int)num;
616 ret = 1;
617 break;
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000618
Matt Caswell0f113f32015-01-22 03:40:55 +0000619 case BIO_CTRL_PENDING:
620 if (b->peer != NULL) {
621 struct bio_bio_st *peer_b = b->peer->ptr;
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000622
Matt Caswell0f113f32015-01-22 03:40:55 +0000623 ret = (long)peer_b->len;
624 } else
625 ret = 0;
626 break;
Bodo Möller95d29591999-06-12 01:03:40 +0000627
Matt Caswell0f113f32015-01-22 03:40:55 +0000628 case BIO_CTRL_WPENDING:
629 if (b->buf != NULL)
630 ret = (long)b->len;
631 else
632 ret = 0;
633 break;
634
635 case BIO_CTRL_DUP:
636 /* See BIO_dup_chain for circumstances we have to expect. */
637 {
638 BIO *other_bio = ptr;
639 struct bio_bio_st *other_b;
640
641 assert(other_bio != NULL);
642 other_b = other_bio->ptr;
643 assert(other_b != NULL);
644
645 assert(other_b->buf == NULL); /* other_bio is always fresh */
646
647 other_b->size = b->size;
648 }
649
650 ret = 1;
651 break;
652
653 case BIO_CTRL_FLUSH:
654 ret = 1;
655 break;
656
657 case BIO_CTRL_EOF:
658 {
659 BIO *other_bio = ptr;
660
661 if (other_bio) {
662 struct bio_bio_st *other_b = other_bio->ptr;
663
664 assert(other_b != NULL);
665 ret = other_b->len == 0 && other_b->closed;
666 } else
667 ret = 1;
668 }
669 break;
670
671 default:
672 ret = 0;
673 }
674 return ret;
675}
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000676
Ulf Möller0e1c0612000-05-15 22:54:43 +0000677static int bio_puts(BIO *bio, const char *str)
Matt Caswell0f113f32015-01-22 03:40:55 +0000678{
679 return bio_write(bio, str, strlen(str));
680}
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000681
682static int bio_make_pair(BIO *bio1, BIO *bio2)
Matt Caswell0f113f32015-01-22 03:40:55 +0000683{
684 struct bio_bio_st *b1, *b2;
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000685
Matt Caswell0f113f32015-01-22 03:40:55 +0000686 assert(bio1 != NULL);
687 assert(bio2 != NULL);
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000688
Matt Caswell0f113f32015-01-22 03:40:55 +0000689 b1 = bio1->ptr;
690 b2 = bio2->ptr;
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000691
Matt Caswell0f113f32015-01-22 03:40:55 +0000692 if (b1->peer != NULL || b2->peer != NULL) {
693 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE);
694 return 0;
695 }
Bodo Möller3928b6b1999-06-07 20:34:36 +0000696
Matt Caswell0f113f32015-01-22 03:40:55 +0000697 if (b1->buf == NULL) {
698 b1->buf = OPENSSL_malloc(b1->size);
699 if (b1->buf == NULL) {
700 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
701 return 0;
702 }
703 b1->len = 0;
704 b1->offset = 0;
705 }
706
707 if (b2->buf == NULL) {
708 b2->buf = OPENSSL_malloc(b2->size);
709 if (b2->buf == NULL) {
710 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE);
711 return 0;
712 }
713 b2->len = 0;
714 b2->offset = 0;
715 }
716
717 b1->peer = bio2;
718 b1->closed = 0;
719 b1->request = 0;
720 b2->peer = bio1;
721 b2->closed = 0;
722 b2->request = 0;
723
724 bio1->init = 1;
725 bio2->init = 1;
726
727 return 1;
728}
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000729
730static void bio_destroy_pair(BIO *bio)
Matt Caswell0f113f32015-01-22 03:40:55 +0000731{
732 struct bio_bio_st *b = bio->ptr;
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000733
Matt Caswell0f113f32015-01-22 03:40:55 +0000734 if (b != NULL) {
735 BIO *peer_bio = b->peer;
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000736
Matt Caswell0f113f32015-01-22 03:40:55 +0000737 if (peer_bio != NULL) {
738 struct bio_bio_st *peer_b = peer_bio->ptr;
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000739
Matt Caswell0f113f32015-01-22 03:40:55 +0000740 assert(peer_b != NULL);
741 assert(peer_b->peer == bio);
Bodo Möller9e06f6f1999-06-07 16:04:45 +0000742
Matt Caswell0f113f32015-01-22 03:40:55 +0000743 peer_b->peer = NULL;
744 peer_bio->init = 0;
745 assert(peer_b->buf != NULL);
746 peer_b->len = 0;
747 peer_b->offset = 0;
748
749 b->peer = NULL;
750 bio->init = 0;
751 assert(b->buf != NULL);
752 b->len = 0;
753 b->offset = 0;
754 }
755 }
756}
Bodo Möller95d29591999-06-12 01:03:40 +0000757
758/* Exported convenience functions */
759int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1,
Matt Caswell0f113f32015-01-22 03:40:55 +0000760 BIO **bio2_p, size_t writebuf2)
761{
762 BIO *bio1 = NULL, *bio2 = NULL;
763 long r;
764 int ret = 0;
Bodo Möller95d29591999-06-12 01:03:40 +0000765
Matt Caswell0f113f32015-01-22 03:40:55 +0000766 bio1 = BIO_new(BIO_s_bio());
767 if (bio1 == NULL)
768 goto err;
769 bio2 = BIO_new(BIO_s_bio());
770 if (bio2 == NULL)
771 goto err;
Bodo Möller95d29591999-06-12 01:03:40 +0000772
Matt Caswell0f113f32015-01-22 03:40:55 +0000773 if (writebuf1) {
774 r = BIO_set_write_buf_size(bio1, writebuf1);
775 if (!r)
776 goto err;
777 }
778 if (writebuf2) {
779 r = BIO_set_write_buf_size(bio2, writebuf2);
780 if (!r)
781 goto err;
782 }
Bodo Möller95d29591999-06-12 01:03:40 +0000783
Matt Caswell0f113f32015-01-22 03:40:55 +0000784 r = BIO_make_bio_pair(bio1, bio2);
785 if (!r)
786 goto err;
787 ret = 1;
Bodo Möller95d29591999-06-12 01:03:40 +0000788
789 err:
Matt Caswell0f113f32015-01-22 03:40:55 +0000790 if (ret == 0) {
791 if (bio1) {
792 BIO_free(bio1);
793 bio1 = NULL;
794 }
795 if (bio2) {
796 BIO_free(bio2);
797 bio2 = NULL;
798 }
799 }
Bodo Möller95d29591999-06-12 01:03:40 +0000800
Matt Caswell0f113f32015-01-22 03:40:55 +0000801 *bio1_p = bio1;
802 *bio2_p = bio2;
803 return ret;
804}
Bodo Möller95d29591999-06-12 01:03:40 +0000805
806size_t BIO_ctrl_get_write_guarantee(BIO *bio)
Matt Caswell0f113f32015-01-22 03:40:55 +0000807{
808 return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL);
809}
Bodo Möller95d29591999-06-12 01:03:40 +0000810
Bodo Möller2de62541999-06-18 12:28:29 +0000811size_t BIO_ctrl_get_read_request(BIO *bio)
Matt Caswell0f113f32015-01-22 03:40:55 +0000812{
813 return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL);
814}
Bodo Möllerc1082a91999-09-07 21:37:09 +0000815
Bodo Möllere405b8d1999-09-27 13:43:59 +0000816int BIO_ctrl_reset_read_request(BIO *bio)
Matt Caswell0f113f32015-01-22 03:40:55 +0000817{
818 return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0);
819}
Bodo Möllere405b8d1999-09-27 13:43:59 +0000820
Matt Caswell0f113f32015-01-22 03:40:55 +0000821/*
822 * BIO_nread0/nread/nwrite0/nwrite are available only for BIO pairs for now
823 * (conceivably some other BIOs could allow non-copying reads and writes
824 * too.)
Bodo Möllerc1082a91999-09-07 21:37:09 +0000825 */
826int BIO_nread0(BIO *bio, char **buf)
Matt Caswell0f113f32015-01-22 03:40:55 +0000827{
828 long ret;
Bodo Möllerc1082a91999-09-07 21:37:09 +0000829
Matt Caswell0f113f32015-01-22 03:40:55 +0000830 if (!bio->init) {
831 BIOerr(BIO_F_BIO_NREAD0, BIO_R_UNINITIALIZED);
832 return -2;
833 }
Bodo Möllerc1082a91999-09-07 21:37:09 +0000834
Matt Caswell0f113f32015-01-22 03:40:55 +0000835 ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf);
836 if (ret > INT_MAX)
837 return INT_MAX;
838 else
839 return (int)ret;
840}
Bodo Möllerc1082a91999-09-07 21:37:09 +0000841
842int BIO_nread(BIO *bio, char **buf, int num)
Matt Caswell0f113f32015-01-22 03:40:55 +0000843{
844 int ret;
Bodo Möllerc1082a91999-09-07 21:37:09 +0000845
Matt Caswell0f113f32015-01-22 03:40:55 +0000846 if (!bio->init) {
847 BIOerr(BIO_F_BIO_NREAD, BIO_R_UNINITIALIZED);
848 return -2;
849 }
Bodo Möllerc1082a91999-09-07 21:37:09 +0000850
Matt Caswell0f113f32015-01-22 03:40:55 +0000851 ret = (int)BIO_ctrl(bio, BIO_C_NREAD, num, buf);
852 if (ret > 0)
853 bio->num_read += ret;
854 return ret;
855}
Bodo Möllerc1082a91999-09-07 21:37:09 +0000856
857int BIO_nwrite0(BIO *bio, char **buf)
Matt Caswell0f113f32015-01-22 03:40:55 +0000858{
859 long ret;
Bodo Möllerc1082a91999-09-07 21:37:09 +0000860
Matt Caswell0f113f32015-01-22 03:40:55 +0000861 if (!bio->init) {
862 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_UNINITIALIZED);
863 return -2;
864 }
Bodo Möllerc1082a91999-09-07 21:37:09 +0000865
Matt Caswell0f113f32015-01-22 03:40:55 +0000866 ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf);
867 if (ret > INT_MAX)
868 return INT_MAX;
869 else
870 return (int)ret;
871}
Bodo Möllerc1082a91999-09-07 21:37:09 +0000872
873int BIO_nwrite(BIO *bio, char **buf, int num)
Matt Caswell0f113f32015-01-22 03:40:55 +0000874{
875 int ret;
Bodo Möllerc1082a91999-09-07 21:37:09 +0000876
Matt Caswell0f113f32015-01-22 03:40:55 +0000877 if (!bio->init) {
878 BIOerr(BIO_F_BIO_NWRITE, BIO_R_UNINITIALIZED);
879 return -2;
880 }
Bodo Möllerc1082a91999-09-07 21:37:09 +0000881
Matt Caswell0f113f32015-01-22 03:40:55 +0000882 ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf);
883 if (ret > 0)
884 bio->num_write += ret;
885 return ret;
886}