1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
57 #include <openssl/bio.h>
63 #include <openssl/err.h>
64 #include <openssl/mem.h>
65 #include <openssl/thread.h>
68 /* BIO_set initialises a BIO structure to have the given type and sets the
69 * reference count to one. It returns one on success or zero on error. */
70 static int bio_set(BIO *bio, const BIO_METHOD *method) {
71 /* This function can be called with a stack allocated |BIO| so we have to
72 * assume that the contents of |BIO| are arbitary. This also means that it'll
73 * leak memory if you call |BIO_set| twice on the same BIO. */
74 memset(bio, 0, sizeof(BIO));
80 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data)) {
84 if (method->create != NULL) {
85 if (!method->create(bio)) {
86 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
94 BIO *BIO_new(const BIO_METHOD *method) {
95 BIO *ret = OPENSSL_malloc(sizeof(BIO));
97 OPENSSL_PUT_ERROR(BIO, BIO_new, ERR_R_MALLOC_FAILURE);
101 if (!bio_set(ret, method)) {
109 int BIO_free(BIO *bio) {
112 for (; bio != NULL; bio = next_bio) {
113 int refs = CRYPTO_add(&bio->references, -1, CRYPTO_LOCK_BIO);
118 if (bio->callback != NULL) {
119 int i = (int)bio->callback(bio, BIO_CB_FREE, NULL, 0, 0, 1);
125 next_bio = BIO_pop(bio);
127 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
129 if (bio->method != NULL && bio->method->destroy != NULL) {
130 bio->method->destroy(bio);
138 void BIO_vfree(BIO *bio) {
142 void BIO_free_all(BIO *bio) {
146 static int bio_io(BIO *bio, void *buf, int len, size_t method_offset,
147 int callback_flags, size_t *num) {
149 typedef int (*io_func_t)(BIO *, char *, int);
150 io_func_t io_func = NULL;
152 if (bio != NULL && bio->method != NULL) {
154 *((const io_func_t *)(((const uint8_t *)bio->method) + method_offset));
157 if (io_func == NULL) {
158 OPENSSL_PUT_ERROR(BIO, bio_io, BIO_R_UNSUPPORTED_METHOD);
162 if (bio->callback != NULL) {
163 i = (int) bio->callback(bio, callback_flags, buf, len, 0L, 1L);
170 OPENSSL_PUT_ERROR(BIO, bio_io, BIO_R_UNINITIALIZED);
175 if (buf != NULL && len > 0) {
176 i = io_func(bio, buf, len);
183 if (bio->callback != NULL) {
184 i = (int)(bio->callback(bio, callback_flags | BIO_CB_RETURN, buf, len, 0L,
191 int BIO_read(BIO *bio, void *buf, int len) {
192 return bio_io(bio, buf, len, offsetof(BIO_METHOD, bread), BIO_CB_READ,
196 int BIO_gets(BIO *bio, char *buf, int len) {
197 return bio_io(bio, buf, len, offsetof(BIO_METHOD, bgets), BIO_CB_GETS,
201 int BIO_write(BIO *bio, const void *in, int inl) {
202 return bio_io(bio, (char *)in, inl, offsetof(BIO_METHOD, bwrite),
203 BIO_CB_WRITE, &bio->num_write);
206 int BIO_puts(BIO *bio, const char *in) {
207 return BIO_write(bio, in, strlen(in));
210 int BIO_flush(BIO *bio) {
211 return BIO_ctrl(bio, BIO_CTRL_FLUSH, 0, NULL);
214 long BIO_ctrl(BIO *bio, int cmd, long larg, void *parg) {
221 if (bio->method == NULL || bio->method->ctrl == NULL) {
222 OPENSSL_PUT_ERROR(BIO, BIO_ctrl, BIO_R_UNSUPPORTED_METHOD);
226 if (bio->callback != NULL) {
227 ret = bio->callback(bio, BIO_CB_CTRL, parg, cmd, larg, 1);
233 ret = bio->method->ctrl(bio, cmd, larg, parg);
235 if (bio->callback != NULL) {
236 ret = bio->callback(bio, BIO_CB_CTRL | BIO_CB_RETURN, parg, cmd, larg, ret);
242 char *BIO_ptr_ctrl(BIO *b, int cmd, long larg) {
245 if (BIO_ctrl(b, cmd, larg, (void *)&p) <= 0) {
252 long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg) {
255 return BIO_ctrl(b, cmd, larg, (void *)&i);
258 int BIO_reset(BIO *bio) {
259 return BIO_ctrl(bio, BIO_CTRL_RESET, 0, NULL);
262 void BIO_set_flags(BIO *bio, int flags) {
266 int BIO_test_flags(const BIO *bio, int flags) {
267 return bio->flags & flags;
270 int BIO_should_read(const BIO *bio) {
271 return BIO_test_flags(bio, BIO_FLAGS_READ);
274 int BIO_should_write(const BIO *bio) {
275 return BIO_test_flags(bio, BIO_FLAGS_WRITE);
278 int BIO_should_retry(const BIO *bio) {
279 return BIO_test_flags(bio, BIO_FLAGS_SHOULD_RETRY);
282 int BIO_should_io_special(const BIO *bio) {
283 return BIO_test_flags(bio, BIO_FLAGS_IO_SPECIAL);
286 int BIO_get_retry_reason(const BIO *bio) { return bio->retry_reason; }
288 void BIO_clear_flags(BIO *bio, int flags) {
289 bio->flags &= ~flags;
292 void BIO_set_retry_read(BIO *bio) {
293 bio->flags |= BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY;
296 void BIO_set_retry_write(BIO *bio) {
297 bio->flags |= BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY;
300 static const int kRetryFlags = BIO_FLAGS_RWS | BIO_FLAGS_SHOULD_RETRY;
302 int BIO_get_retry_flags(BIO *bio) {
303 return bio->flags & kRetryFlags;
306 void BIO_clear_retry_flags(BIO *bio) {
307 bio->flags &= ~kRetryFlags;
308 bio->retry_reason = 0;
311 int BIO_method_type(const BIO *bio) { return bio->method->type; }
313 void BIO_copy_next_retry(BIO *bio) {
314 BIO_clear_retry_flags(bio);
315 BIO_set_flags(bio, BIO_get_retry_flags(bio->next_bio));
316 bio->retry_reason = bio->next_bio->retry_reason;
319 long BIO_callback_ctrl(BIO *bio, int cmd, bio_info_cb fp) {
327 if (bio->method == NULL || bio->method->callback_ctrl == NULL) {
328 OPENSSL_PUT_ERROR(BIO, BIO_callback_ctrl, BIO_R_UNSUPPORTED_METHOD);
335 ret = cb(bio, BIO_CB_CTRL, (void *)&fp, cmd, 0, 1L);
341 ret = bio->method->callback_ctrl(bio, cmd, fp);
344 ret = cb(bio, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, cmd, 0, ret);
350 size_t BIO_pending(const BIO *bio) {
351 return BIO_ctrl((BIO *) bio, BIO_CTRL_PENDING, 0, NULL);
354 size_t BIO_ctrl_pending(const BIO *bio) {
355 return BIO_pending(bio);
358 size_t BIO_wpending(const BIO *bio) {
359 return BIO_ctrl((BIO *) bio, BIO_CTRL_WPENDING, 0, NULL);
362 int BIO_set_close(BIO *bio, int close_flag) {
363 return BIO_ctrl(bio, BIO_CTRL_SET_CLOSE, close_flag, NULL);
366 void BIO_set_callback(BIO *bio, bio_info_cb callback_func) {
367 bio->callback = callback_func;
370 void BIO_set_callback_arg(BIO *bio, char *arg) {
374 char *BIO_get_callback_arg(const BIO *bio) {
378 OPENSSL_EXPORT size_t BIO_number_read(const BIO *bio) {
379 return bio->num_read;
382 OPENSSL_EXPORT size_t BIO_number_written(const BIO *bio) {
383 return bio->num_write;
386 BIO *BIO_push(BIO *bio, BIO *appended_bio) {
394 while (last_bio->next_bio != NULL) {
395 last_bio = last_bio->next_bio;
398 last_bio->next_bio = appended_bio;
399 /* TODO(fork): this seems very suspect. If we got rid of BIO SSL, we could
400 * get rid of this. */
401 BIO_ctrl(bio, BIO_CTRL_PUSH, 0, bio);
406 BIO *BIO_pop(BIO *bio) {
413 BIO_ctrl(bio, BIO_CTRL_POP, 0, bio);
414 bio->next_bio = NULL;
418 BIO *BIO_next(BIO *bio) {
422 return bio->next_bio;
425 BIO *BIO_find_type(BIO *bio, int type) {
426 int method_type, mask;
434 if (bio->method != NULL) {
435 method_type = bio->method->type;
438 if (method_type & type) {
441 } else if (method_type == type) {
446 } while (bio != NULL);
451 int BIO_indent(BIO *bio, unsigned indent, unsigned max_indent) {
452 if (indent > max_indent) {
457 if (BIO_puts(bio, " ") != 1) {
464 void BIO_print_errors_fp(FILE *out) {
465 BIO *bio = BIO_new_fp(out, BIO_NOCLOSE);
466 BIO_print_errors(bio);
470 static int print_bio(const char *str, size_t len, void *bio) {
471 return BIO_write((BIO *)bio, str, len);
474 void BIO_print_errors(BIO *bio) {
475 ERR_print_errors_cb(print_bio, bio);