|
1 /* crypto/bio/bss_bio.c -*- Mode: C; c-file-style: "eay" -*- */ |
|
2 /* ==================================================================== |
|
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 |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
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 */ |
|
55 /* |
|
56 © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. |
|
57 */ |
|
58 |
|
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. */ |
|
65 |
|
66 /* BIO_DEBUG implies BIO_PAIR_DEBUG */ |
|
67 #ifdef BIO_DEBUG |
|
68 # ifndef BIO_PAIR_DEBUG |
|
69 # define BIO_PAIR_DEBUG |
|
70 # endif |
|
71 #endif |
|
72 |
|
73 /* disable assert() unless BIO_PAIR_DEBUG has been defined */ |
|
74 #ifndef BIO_PAIR_DEBUG |
|
75 # ifndef NDEBUG |
|
76 # define NDEBUG |
|
77 # endif |
|
78 #endif |
|
79 |
|
80 #include <assert.h> |
|
81 #include <limits.h> |
|
82 #include <stdlib.h> |
|
83 #include <string.h> |
|
84 |
|
85 #include <openssl/bio.h> |
|
86 #include <openssl/err.h> |
|
87 #include <openssl/crypto.h> |
|
88 |
|
89 #include "e_os.h" |
|
90 #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__))) |
|
91 #include "libcrypto_wsd_macros.h" |
|
92 #include "libcrypto_wsd.h" |
|
93 #endif |
|
94 |
|
95 /* VxWorks defines SSIZE_MAX with an empty value causing compile errors */ |
|
96 #if defined(OPENSSL_SYS_VXWORKS) |
|
97 # undef SSIZE_MAX |
|
98 #endif |
|
99 #ifndef SSIZE_MAX |
|
100 # define SSIZE_MAX INT_MAX |
|
101 #endif |
|
102 |
|
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); |
|
109 |
|
110 static int bio_make_pair(BIO *bio1, BIO *bio2); |
|
111 static void bio_destroy_pair(BIO *bio); |
|
112 |
|
113 #ifndef EMULATOR |
|
114 static BIO_METHOD methods_biop = |
|
115 { |
|
116 BIO_TYPE_BIO, |
|
117 "BIO pair", |
|
118 bio_write, |
|
119 bio_read, |
|
120 bio_puts, |
|
121 NULL /* no bio_gets */, |
|
122 bio_ctrl, |
|
123 bio_new, |
|
124 bio_free, |
|
125 NULL /* no bio_callback_ctrl */ |
|
126 }; |
|
127 #else |
|
128 |
|
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 = |
|
132 { |
|
133 BIO_TYPE_BIO, |
|
134 "BIO pair", |
|
135 bio_write, |
|
136 bio_read, |
|
137 bio_puts, |
|
138 NULL /* no bio_gets */, |
|
139 bio_ctrl, |
|
140 bio_new, |
|
141 bio_free, |
|
142 NULL /* no bio_callback_ctrl */ |
|
143 }; |
|
144 #endif |
|
145 |
|
146 EXPORT_C BIO_METHOD *BIO_s_bio(void) |
|
147 { |
|
148 return &methods_biop; |
|
149 } |
|
150 |
|
151 struct bio_bio_st |
|
152 { |
|
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. */ |
|
157 |
|
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 */ |
|
162 size_t size; |
|
163 char *buf; /* "size" elements (if != NULL) */ |
|
164 |
|
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. */ |
|
169 }; |
|
170 |
|
171 static int bio_new(BIO *bio) |
|
172 { |
|
173 struct bio_bio_st *b; |
|
174 |
|
175 b = OPENSSL_malloc(sizeof *b); |
|
176 if (b == NULL) |
|
177 return 0; |
|
178 |
|
179 b->peer = NULL; |
|
180 b->size = 17*1024; /* enough for one TLS record (just a default) */ |
|
181 b->buf = NULL; |
|
182 |
|
183 bio->ptr = b; |
|
184 return 1; |
|
185 } |
|
186 |
|
187 |
|
188 static int bio_free(BIO *bio) |
|
189 { |
|
190 struct bio_bio_st *b; |
|
191 |
|
192 if (bio == NULL) |
|
193 return 0; |
|
194 b = bio->ptr; |
|
195 |
|
196 assert(b != NULL); |
|
197 |
|
198 if (b->peer) |
|
199 bio_destroy_pair(bio); |
|
200 |
|
201 if (b->buf != NULL) |
|
202 { |
|
203 OPENSSL_free(b->buf); |
|
204 } |
|
205 |
|
206 OPENSSL_free(b); |
|
207 |
|
208 return 1; |
|
209 } |
|
210 |
|
211 |
|
212 |
|
213 static int bio_read(BIO *bio, char *buf, int size_) |
|
214 { |
|
215 size_t size = size_; |
|
216 size_t rest; |
|
217 struct bio_bio_st *b, *peer_b; |
|
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; /* will be set in "retry_read" situation */ |
|
232 |
|
233 if (buf == NULL || size == 0) |
|
234 return 0; |
|
235 |
|
236 if (peer_b->len == 0) |
|
237 { |
|
238 if (peer_b->closed) |
|
239 return 0; /* writer has closed, and no data is left */ |
|
240 else |
|
241 { |
|
242 BIO_set_retry_read(bio); /* buffer is empty */ |
|
243 if (size <= peer_b->size) |
|
244 peer_b->request = size; |
|
245 else |
|
246 /* don't ask for more than the peer can |
|
247 * deliver in one write */ |
|
248 peer_b->request = peer_b->size; |
|
249 return -1; |
|
250 } |
|
251 } |
|
252 |
|
253 /* we can read */ |
|
254 if (peer_b->len < size) |
|
255 size = peer_b->len; |
|
256 |
|
257 /* now read "size" bytes */ |
|
258 |
|
259 rest = size; |
|
260 |
|
261 assert(rest > 0); |
|
262 do /* one or two iterations */ |
|
263 { |
|
264 size_t chunk; |
|
265 |
|
266 assert(rest <= peer_b->len); |
|
267 if (peer_b->offset + rest <= peer_b->size) |
|
268 chunk = rest; |
|
269 else |
|
270 /* wrap around ring buffer */ |
|
271 chunk = peer_b->size - peer_b->offset; |
|
272 assert(peer_b->offset + chunk <= peer_b->size); |
|
273 |
|
274 memcpy(buf, peer_b->buf + peer_b->offset, chunk); |
|
275 |
|
276 peer_b->len -= chunk; |
|
277 if (peer_b->len) |
|
278 { |
|
279 peer_b->offset += chunk; |
|
280 assert(peer_b->offset <= peer_b->size); |
|
281 if (peer_b->offset == peer_b->size) |
|
282 peer_b->offset = 0; |
|
283 buf += chunk; |
|
284 } |
|
285 else |
|
286 { |
|
287 /* buffer now empty, no need to advance "buf" */ |
|
288 assert(chunk == rest); |
|
289 peer_b->offset = 0; |
|
290 } |
|
291 rest -= chunk; |
|
292 } |
|
293 while (rest); |
|
294 |
|
295 return size; |
|
296 } |
|
297 |
|
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) |
|
303 */ |
|
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) |
|
307 { |
|
308 struct bio_bio_st *b, *peer_b; |
|
309 ssize_t num; |
|
310 |
|
311 BIO_clear_retry_flags(bio); |
|
312 |
|
313 if (!bio->init) |
|
314 return 0; |
|
315 |
|
316 b = bio->ptr; |
|
317 assert(b != NULL); |
|
318 assert(b->peer != NULL); |
|
319 peer_b = b->peer->ptr; |
|
320 assert(peer_b != NULL); |
|
321 assert(peer_b->buf != NULL); |
|
322 |
|
323 peer_b->request = 0; |
|
324 |
|
325 if (peer_b->len == 0) |
|
326 { |
|
327 char dummy; |
|
328 |
|
329 /* avoid code duplication -- nothing available for reading */ |
|
330 return bio_read(bio, &dummy, 1); /* returns 0 or -1 */ |
|
331 } |
|
332 |
|
333 num = peer_b->len; |
|
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; |
|
337 assert(num > 0); |
|
338 |
|
339 if (buf != NULL) |
|
340 *buf = peer_b->buf + peer_b->offset; |
|
341 return num; |
|
342 } |
|
343 |
|
344 static ssize_t bio_nread(BIO *bio, char **buf, size_t num_) |
|
345 { |
|
346 struct bio_bio_st *b, *peer_b; |
|
347 ssize_t num, available; |
|
348 |
|
349 if (num_ > SSIZE_MAX) |
|
350 num = SSIZE_MAX; |
|
351 else |
|
352 num = (ssize_t)num_; |
|
353 |
|
354 available = bio_nread0(bio, buf); |
|
355 if (num > available) |
|
356 num = available; |
|
357 if (num <= 0) |
|
358 return num; |
|
359 |
|
360 b = bio->ptr; |
|
361 peer_b = b->peer->ptr; |
|
362 |
|
363 peer_b->len -= num; |
|
364 if (peer_b->len) |
|
365 { |
|
366 peer_b->offset += num; |
|
367 assert(peer_b->offset <= peer_b->size); |
|
368 if (peer_b->offset == peer_b->size) |
|
369 peer_b->offset = 0; |
|
370 } |
|
371 else |
|
372 peer_b->offset = 0; |
|
373 |
|
374 return num; |
|
375 } |
|
376 |
|
377 |
|
378 static int bio_write(BIO *bio, const char *buf, int num_) |
|
379 { |
|
380 size_t num = num_; |
|
381 size_t rest; |
|
382 struct bio_bio_st *b; |
|
383 |
|
384 BIO_clear_retry_flags(bio); |
|
385 |
|
386 if (!bio->init || buf == NULL || num == 0) |
|
387 return 0; |
|
388 |
|
389 b = bio->ptr; |
|
390 assert(b != NULL); |
|
391 assert(b->peer != NULL); |
|
392 assert(b->buf != NULL); |
|
393 |
|
394 b->request = 0; |
|
395 if (b->closed) |
|
396 { |
|
397 /* we already closed */ |
|
398 BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE); |
|
399 return -1; |
|
400 } |
|
401 |
|
402 assert(b->len <= b->size); |
|
403 |
|
404 if (b->len == b->size) |
|
405 { |
|
406 BIO_set_retry_write(bio); /* buffer is full */ |
|
407 return -1; |
|
408 } |
|
409 |
|
410 /* we can write */ |
|
411 if (num > b->size - b->len) |
|
412 num = b->size - b->len; |
|
413 |
|
414 /* now write "num" bytes */ |
|
415 |
|
416 rest = num; |
|
417 |
|
418 assert(rest > 0); |
|
419 do /* one or two iterations */ |
|
420 { |
|
421 size_t write_offset; |
|
422 size_t chunk; |
|
423 |
|
424 assert(b->len + rest <= b->size); |
|
425 |
|
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. */ |
|
430 |
|
431 if (write_offset + rest <= b->size) |
|
432 chunk = rest; |
|
433 else |
|
434 /* wrap around ring buffer */ |
|
435 chunk = b->size - write_offset; |
|
436 |
|
437 memcpy(b->buf + write_offset, buf, chunk); |
|
438 |
|
439 b->len += chunk; |
|
440 |
|
441 assert(b->len <= b->size); |
|
442 |
|
443 rest -= chunk; |
|
444 buf += chunk; |
|
445 } |
|
446 while (rest); |
|
447 |
|
448 return num; |
|
449 } |
|
450 |
|
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) |
|
456 */ |
|
457 static ssize_t bio_nwrite0(BIO *bio, char **buf) |
|
458 { |
|
459 struct bio_bio_st *b; |
|
460 size_t num; |
|
461 size_t write_offset; |
|
462 |
|
463 BIO_clear_retry_flags(bio); |
|
464 |
|
465 if (!bio->init) |
|
466 return 0; |
|
467 |
|
468 b = bio->ptr; |
|
469 assert(b != NULL); |
|
470 assert(b->peer != NULL); |
|
471 assert(b->buf != NULL); |
|
472 |
|
473 b->request = 0; |
|
474 if (b->closed) |
|
475 { |
|
476 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_BROKEN_PIPE); |
|
477 return -1; |
|
478 } |
|
479 |
|
480 assert(b->len <= b->size); |
|
481 |
|
482 if (b->len == b->size) |
|
483 { |
|
484 BIO_set_retry_write(bio); |
|
485 return -1; |
|
486 } |
|
487 |
|
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; |
|
497 |
|
498 if (buf != NULL) |
|
499 *buf = b->buf + write_offset; |
|
500 assert(write_offset + num <= b->size); |
|
501 |
|
502 return num; |
|
503 } |
|
504 |
|
505 static ssize_t bio_nwrite(BIO *bio, char **buf, size_t num_) |
|
506 { |
|
507 struct bio_bio_st *b; |
|
508 ssize_t num, space; |
|
509 |
|
510 if (num_ > SSIZE_MAX) |
|
511 num = SSIZE_MAX; |
|
512 else |
|
513 num = (ssize_t)num_; |
|
514 |
|
515 space = bio_nwrite0(bio, buf); |
|
516 if (num > space) |
|
517 num = space; |
|
518 if (num <= 0) |
|
519 return num; |
|
520 b = bio->ptr; |
|
521 assert(b != NULL); |
|
522 b->len += num; |
|
523 assert(b->len <= b->size); |
|
524 |
|
525 return num; |
|
526 } |
|
527 |
|
528 |
|
529 static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr) |
|
530 { |
|
531 long ret; |
|
532 struct bio_bio_st *b = bio->ptr; |
|
533 |
|
534 assert(b != NULL); |
|
535 |
|
536 switch (cmd) |
|
537 { |
|
538 /* specific CTRL codes */ |
|
539 |
|
540 case BIO_C_SET_WRITE_BUF_SIZE: |
|
541 if (b->peer) |
|
542 { |
|
543 BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE); |
|
544 ret = 0; |
|
545 } |
|
546 else if (num == 0) |
|
547 { |
|
548 BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT); |
|
549 ret = 0; |
|
550 } |
|
551 else |
|
552 { |
|
553 size_t new_size = num; |
|
554 |
|
555 if (b->size != new_size) |
|
556 { |
|
557 if (b->buf) |
|
558 { |
|
559 OPENSSL_free(b->buf); |
|
560 b->buf = NULL; |
|
561 } |
|
562 b->size = new_size; |
|
563 } |
|
564 ret = 1; |
|
565 } |
|
566 break; |
|
567 |
|
568 case BIO_C_GET_WRITE_BUF_SIZE: |
|
569 ret = (long) b->size; |
|
570 break; |
|
571 |
|
572 case BIO_C_MAKE_BIO_PAIR: |
|
573 { |
|
574 BIO *other_bio = ptr; |
|
575 |
|
576 if (bio_make_pair(bio, other_bio)) |
|
577 ret = 1; |
|
578 else |
|
579 ret = 0; |
|
580 } |
|
581 break; |
|
582 |
|
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); |
|
587 ret = 1; |
|
588 break; |
|
589 |
|
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) |
|
594 ret = 0; |
|
595 else |
|
596 ret = (long) b->size - b->len; |
|
597 break; |
|
598 |
|
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; |
|
604 break; |
|
605 |
|
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 |
|
610 * available.) */ |
|
611 b->request = 0; |
|
612 ret = 1; |
|
613 break; |
|
614 |
|
615 case BIO_C_SHUTDOWN_WR: |
|
616 /* similar to shutdown(..., SHUT_WR) */ |
|
617 b->closed = 1; |
|
618 ret = 1; |
|
619 break; |
|
620 |
|
621 case BIO_C_NREAD0: |
|
622 /* prepare for non-copying read */ |
|
623 ret = (long) bio_nread0(bio, ptr); |
|
624 break; |
|
625 |
|
626 case BIO_C_NREAD: |
|
627 /* non-copying read */ |
|
628 ret = (long) bio_nread(bio, ptr, (size_t) num); |
|
629 break; |
|
630 |
|
631 case BIO_C_NWRITE0: |
|
632 /* prepare for non-copying write */ |
|
633 ret = (long) bio_nwrite0(bio, ptr); |
|
634 break; |
|
635 |
|
636 case BIO_C_NWRITE: |
|
637 /* non-copying write */ |
|
638 ret = (long) bio_nwrite(bio, ptr, (size_t) num); |
|
639 break; |
|
640 |
|
641 |
|
642 /* standard CTRL codes follow */ |
|
643 |
|
644 case BIO_CTRL_RESET: |
|
645 if (b->buf != NULL) |
|
646 { |
|
647 b->len = 0; |
|
648 b->offset = 0; |
|
649 } |
|
650 ret = 0; |
|
651 break; |
|
652 |
|
653 case BIO_CTRL_GET_CLOSE: |
|
654 ret = bio->shutdown; |
|
655 break; |
|
656 |
|
657 case BIO_CTRL_SET_CLOSE: |
|
658 bio->shutdown = (int) num; |
|
659 ret = 1; |
|
660 break; |
|
661 |
|
662 case BIO_CTRL_PENDING: |
|
663 if (b->peer != NULL) |
|
664 { |
|
665 struct bio_bio_st *peer_b = b->peer->ptr; |
|
666 |
|
667 ret = (long) peer_b->len; |
|
668 } |
|
669 else |
|
670 ret = 0; |
|
671 break; |
|
672 |
|
673 case BIO_CTRL_WPENDING: |
|
674 if (b->buf != NULL) |
|
675 ret = (long) b->len; |
|
676 else |
|
677 ret = 0; |
|
678 break; |
|
679 |
|
680 case BIO_CTRL_DUP: |
|
681 /* See BIO_dup_chain for circumstances we have to expect. */ |
|
682 { |
|
683 BIO *other_bio = ptr; |
|
684 struct bio_bio_st *other_b; |
|
685 |
|
686 assert(other_bio != NULL); |
|
687 other_b = other_bio->ptr; |
|
688 assert(other_b != NULL); |
|
689 |
|
690 assert(other_b->buf == NULL); /* other_bio is always fresh */ |
|
691 |
|
692 other_b->size = b->size; |
|
693 } |
|
694 |
|
695 ret = 1; |
|
696 break; |
|
697 |
|
698 case BIO_CTRL_FLUSH: |
|
699 ret = 1; |
|
700 break; |
|
701 |
|
702 case BIO_CTRL_EOF: |
|
703 { |
|
704 BIO *other_bio = ptr; |
|
705 |
|
706 if (other_bio) |
|
707 { |
|
708 struct bio_bio_st *other_b = other_bio->ptr; |
|
709 |
|
710 assert(other_b != NULL); |
|
711 ret = other_b->len == 0 && other_b->closed; |
|
712 } |
|
713 else |
|
714 ret = 1; |
|
715 } |
|
716 break; |
|
717 |
|
718 default: |
|
719 ret = 0; |
|
720 } |
|
721 return ret; |
|
722 } |
|
723 |
|
724 static int bio_puts(BIO *bio, const char *str) |
|
725 { |
|
726 return bio_write(bio, str, strlen(str)); |
|
727 } |
|
728 |
|
729 |
|
730 static int bio_make_pair(BIO *bio1, BIO *bio2) |
|
731 { |
|
732 struct bio_bio_st *b1, *b2; |
|
733 |
|
734 assert(bio1 != NULL); |
|
735 assert(bio2 != NULL); |
|
736 |
|
737 b1 = bio1->ptr; |
|
738 b2 = bio2->ptr; |
|
739 |
|
740 if (b1->peer != NULL || b2->peer != NULL) |
|
741 { |
|
742 BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE); |
|
743 return 0; |
|
744 } |
|
745 |
|
746 if (b1->buf == NULL) |
|
747 { |
|
748 b1->buf = OPENSSL_malloc(b1->size); |
|
749 if (b1->buf == NULL) |
|
750 { |
|
751 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE); |
|
752 return 0; |
|
753 } |
|
754 b1->len = 0; |
|
755 b1->offset = 0; |
|
756 } |
|
757 |
|
758 if (b2->buf == NULL) |
|
759 { |
|
760 b2->buf = OPENSSL_malloc(b2->size); |
|
761 if (b2->buf == NULL) |
|
762 { |
|
763 BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE); |
|
764 return 0; |
|
765 } |
|
766 b2->len = 0; |
|
767 b2->offset = 0; |
|
768 } |
|
769 |
|
770 b1->peer = bio2; |
|
771 b1->closed = 0; |
|
772 b1->request = 0; |
|
773 b2->peer = bio1; |
|
774 b2->closed = 0; |
|
775 b2->request = 0; |
|
776 |
|
777 bio1->init = 1; |
|
778 bio2->init = 1; |
|
779 |
|
780 return 1; |
|
781 } |
|
782 |
|
783 static void bio_destroy_pair(BIO *bio) |
|
784 { |
|
785 struct bio_bio_st *b = bio->ptr; |
|
786 |
|
787 if (b != NULL) |
|
788 { |
|
789 BIO *peer_bio = b->peer; |
|
790 |
|
791 if (peer_bio != NULL) |
|
792 { |
|
793 struct bio_bio_st *peer_b = peer_bio->ptr; |
|
794 |
|
795 assert(peer_b != NULL); |
|
796 assert(peer_b->peer == bio); |
|
797 |
|
798 peer_b->peer = NULL; |
|
799 peer_bio->init = 0; |
|
800 assert(peer_b->buf != NULL); |
|
801 peer_b->len = 0; |
|
802 peer_b->offset = 0; |
|
803 |
|
804 b->peer = NULL; |
|
805 bio->init = 0; |
|
806 assert(b->buf != NULL); |
|
807 b->len = 0; |
|
808 b->offset = 0; |
|
809 } |
|
810 } |
|
811 } |
|
812 |
|
813 |
|
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) |
|
817 { |
|
818 BIO *bio1 = NULL, *bio2 = NULL; |
|
819 long r; |
|
820 int ret = 0; |
|
821 |
|
822 bio1 = BIO_new(BIO_s_bio()); |
|
823 if (bio1 == NULL) |
|
824 goto err; |
|
825 bio2 = BIO_new(BIO_s_bio()); |
|
826 if (bio2 == NULL) |
|
827 goto err; |
|
828 |
|
829 if (writebuf1) |
|
830 { |
|
831 r = BIO_set_write_buf_size(bio1, writebuf1); |
|
832 if (!r) |
|
833 goto err; |
|
834 } |
|
835 if (writebuf2) |
|
836 { |
|
837 r = BIO_set_write_buf_size(bio2, writebuf2); |
|
838 if (!r) |
|
839 goto err; |
|
840 } |
|
841 |
|
842 r = BIO_make_bio_pair(bio1, bio2); |
|
843 if (!r) |
|
844 goto err; |
|
845 ret = 1; |
|
846 |
|
847 err: |
|
848 if (ret == 0) |
|
849 { |
|
850 if (bio1) |
|
851 { |
|
852 BIO_free(bio1); |
|
853 bio1 = NULL; |
|
854 } |
|
855 if (bio2) |
|
856 { |
|
857 BIO_free(bio2); |
|
858 bio2 = NULL; |
|
859 } |
|
860 } |
|
861 |
|
862 *bio1_p = bio1; |
|
863 *bio2_p = bio2; |
|
864 return ret; |
|
865 } |
|
866 |
|
867 EXPORT_C size_t BIO_ctrl_get_write_guarantee(BIO *bio) |
|
868 { |
|
869 return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL); |
|
870 } |
|
871 |
|
872 EXPORT_C size_t BIO_ctrl_get_read_request(BIO *bio) |
|
873 { |
|
874 return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL); |
|
875 } |
|
876 |
|
877 EXPORT_C int BIO_ctrl_reset_read_request(BIO *bio) |
|
878 { |
|
879 return (BIO_ctrl(bio, BIO_C_RESET_READ_REQUEST, 0, NULL) != 0); |
|
880 } |
|
881 |
|
882 |
|
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.) |
|
885 */ |
|
886 EXPORT_C int BIO_nread0(BIO *bio, char **buf) |
|
887 { |
|
888 long ret; |
|
889 |
|
890 if (!bio->init) |
|
891 { |
|
892 BIOerr(BIO_F_BIO_NREAD0, BIO_R_UNINITIALIZED); |
|
893 return -2; |
|
894 } |
|
895 |
|
896 ret = BIO_ctrl(bio, BIO_C_NREAD0, 0, buf); |
|
897 if (ret > INT_MAX) |
|
898 return INT_MAX; |
|
899 else |
|
900 return (int) ret; |
|
901 } |
|
902 |
|
903 EXPORT_C int BIO_nread(BIO *bio, char **buf, int num) |
|
904 { |
|
905 int ret; |
|
906 |
|
907 if (!bio->init) |
|
908 { |
|
909 BIOerr(BIO_F_BIO_NREAD, BIO_R_UNINITIALIZED); |
|
910 return -2; |
|
911 } |
|
912 |
|
913 ret = (int) BIO_ctrl(bio, BIO_C_NREAD, num, buf); |
|
914 if (ret > 0) |
|
915 bio->num_read += ret; |
|
916 return ret; |
|
917 } |
|
918 |
|
919 EXPORT_C int BIO_nwrite0(BIO *bio, char **buf) |
|
920 { |
|
921 long ret; |
|
922 |
|
923 if (!bio->init) |
|
924 { |
|
925 BIOerr(BIO_F_BIO_NWRITE0, BIO_R_UNINITIALIZED); |
|
926 return -2; |
|
927 } |
|
928 |
|
929 ret = BIO_ctrl(bio, BIO_C_NWRITE0, 0, buf); |
|
930 if (ret > INT_MAX) |
|
931 return INT_MAX; |
|
932 else |
|
933 return (int) ret; |
|
934 } |
|
935 |
|
936 EXPORT_C int BIO_nwrite(BIO *bio, char **buf, int num) |
|
937 { |
|
938 int ret; |
|
939 |
|
940 if (!bio->init) |
|
941 { |
|
942 BIOerr(BIO_F_BIO_NWRITE, BIO_R_UNINITIALIZED); |
|
943 return -2; |
|
944 } |
|
945 |
|
946 ret = BIO_ctrl(bio, BIO_C_NWRITE, num, buf); |
|
947 if (ret > 0) |
|
948 bio->num_read += ret; |
|
949 return ret; |
|
950 } |