1/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
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.
7 *
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).
14 *
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.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
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)"
39 *
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
50 * SUCH DAMAGE.
51 *
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.] */
56
57#include <openssl/bio.h>
58
59#include <errno.h>
60#include <stddef.h>
61#include <limits.h>
62
63#include <openssl/err.h>
64#include <openssl/mem.h>
65#include <openssl/thread.h>
66
67
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. */
70static 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));
75
76  bio->method = method;
77  bio->shutdown = 1;
78  bio->references = 1;
79
80  if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data)) {
81    return 0;
82  }
83
84  if (method->create != NULL) {
85    if (!method->create(bio)) {
86      CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
87      return 0;
88    }
89  }
90
91  return 1;
92}
93
94BIO *BIO_new(const BIO_METHOD *method) {
95  BIO *ret = OPENSSL_malloc(sizeof(BIO));
96  if (ret == NULL) {
97    OPENSSL_PUT_ERROR(BIO, BIO_new, ERR_R_MALLOC_FAILURE);
98    return NULL;
99  }
100
101  if (!bio_set(ret, method)) {
102    OPENSSL_free(ret);
103    ret = NULL;
104  }
105
106  return ret;
107}
108
109int BIO_free(BIO *bio) {
110  BIO *next_bio;
111
112  for (; bio != NULL; bio = next_bio) {
113    int refs = CRYPTO_add(&bio->references, -1, CRYPTO_LOCK_BIO);
114    if (refs > 0) {
115      return 0;
116    }
117
118    if (bio->callback != NULL) {
119      int i = (int)bio->callback(bio, BIO_CB_FREE, NULL, 0, 0, 1);
120      if (i <= 0) {
121        return i;
122      }
123    }
124
125    next_bio = BIO_pop(bio);
126
127    CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
128
129    if (bio->method != NULL && bio->method->destroy != NULL) {
130      bio->method->destroy(bio);
131    }
132
133    OPENSSL_free(bio);
134  }
135  return 1;
136}
137
138void BIO_vfree(BIO *bio) {
139  BIO_free(bio);
140}
141
142void BIO_free_all(BIO *bio) {
143  BIO_free(bio);
144}
145
146static int bio_io(BIO *bio, void *buf, int len, size_t method_offset,
147                  int callback_flags, size_t *num) {
148  int i;
149  typedef int (*io_func_t)(BIO *, char *, int);
150  io_func_t io_func = NULL;
151
152  if (bio != NULL && bio->method != NULL) {
153    io_func =
154        *((const io_func_t *)(((const uint8_t *)bio->method) + method_offset));
155  }
156
157  if (io_func == NULL) {
158    OPENSSL_PUT_ERROR(BIO, bio_io, BIO_R_UNSUPPORTED_METHOD);
159    return -2;
160  }
161
162  if (bio->callback != NULL) {
163    i = (int) bio->callback(bio, callback_flags, buf, len, 0L, 1L);
164    if (i <= 0) {
165      return i;
166    }
167  }
168
169  if (!bio->init) {
170    OPENSSL_PUT_ERROR(BIO, bio_io, BIO_R_UNINITIALIZED);
171    return -2;
172  }
173
174  i = 0;
175  if (buf != NULL && len > 0) {
176    i = io_func(bio, buf, len);
177  }
178
179  if (i > 0) {
180    *num += i;
181  }
182
183  if (bio->callback != NULL) {
184    i = (int)(bio->callback(bio, callback_flags | BIO_CB_RETURN, buf, len, 0L,
185                            (long)i));
186  }
187
188  return i;
189}
190
191int BIO_read(BIO *bio, void *buf, int len) {
192  return bio_io(bio, buf, len, offsetof(BIO_METHOD, bread), BIO_CB_READ,
193                &bio->num_read);
194}
195
196int BIO_gets(BIO *bio, char *buf, int len) {
197  return bio_io(bio, buf, len, offsetof(BIO_METHOD, bgets), BIO_CB_GETS,
198                &bio->num_read);
199}
200
201int 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);
204}
205
206int BIO_puts(BIO *bio, const char *in) {
207  return BIO_write(bio, in, strlen(in));
208}
209
210int BIO_flush(BIO *bio) {
211  return BIO_ctrl(bio, BIO_CTRL_FLUSH, 0, NULL);
212}
213
214long BIO_ctrl(BIO *bio, int cmd, long larg, void *parg) {
215  long ret;
216
217  if (bio == NULL) {
218    return 0;
219  }
220
221  if (bio->method == NULL || bio->method->ctrl == NULL) {
222    OPENSSL_PUT_ERROR(BIO, BIO_ctrl, BIO_R_UNSUPPORTED_METHOD);
223    return -2;
224  }
225
226  if (bio->callback != NULL) {
227    ret = bio->callback(bio, BIO_CB_CTRL, parg, cmd, larg, 1);
228    if (ret <= 0) {
229      return ret;
230    }
231  }
232
233  ret = bio->method->ctrl(bio, cmd, larg, parg);
234
235  if (bio->callback != NULL) {
236    ret = bio->callback(bio, BIO_CB_CTRL | BIO_CB_RETURN, parg, cmd, larg, ret);
237  }
238
239  return ret;
240}
241
242char *BIO_ptr_ctrl(BIO *b, int cmd, long larg) {
243  char *p = NULL;
244
245  if (BIO_ctrl(b, cmd, larg, (void *)&p) <= 0) {
246    return NULL;
247  }
248
249  return p;
250}
251
252long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg) {
253  int i = iarg;
254
255  return BIO_ctrl(b, cmd, larg, (void *)&i);
256}
257
258int BIO_reset(BIO *bio) {
259  return BIO_ctrl(bio, BIO_CTRL_RESET, 0, NULL);
260}
261
262void BIO_set_flags(BIO *bio, int flags) {
263  bio->flags |= flags;
264}
265
266int BIO_test_flags(const BIO *bio, int flags) {
267  return bio->flags & flags;
268}
269
270int BIO_should_read(const BIO *bio) {
271  return BIO_test_flags(bio, BIO_FLAGS_READ);
272}
273
274int BIO_should_write(const BIO *bio) {
275  return BIO_test_flags(bio, BIO_FLAGS_WRITE);
276}
277
278int BIO_should_retry(const BIO *bio) {
279  return BIO_test_flags(bio, BIO_FLAGS_SHOULD_RETRY);
280}
281
282int BIO_should_io_special(const BIO *bio) {
283  return BIO_test_flags(bio, BIO_FLAGS_IO_SPECIAL);
284}
285
286int BIO_get_retry_reason(const BIO *bio) { return bio->retry_reason; }
287
288void BIO_clear_flags(BIO *bio, int flags) {
289  bio->flags &= ~flags;
290}
291
292void BIO_set_retry_read(BIO *bio) {
293  bio->flags |= BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY;
294}
295
296void BIO_set_retry_write(BIO *bio) {
297  bio->flags |= BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY;
298}
299
300static const int kRetryFlags = BIO_FLAGS_RWS | BIO_FLAGS_SHOULD_RETRY;
301
302int BIO_get_retry_flags(BIO *bio) {
303  return bio->flags & kRetryFlags;
304}
305
306void BIO_clear_retry_flags(BIO *bio) {
307  bio->flags &= ~kRetryFlags;
308  bio->retry_reason = 0;
309}
310
311int BIO_method_type(const BIO *bio) { return bio->method->type; }
312
313void 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;
317}
318
319long BIO_callback_ctrl(BIO *bio, int cmd, bio_info_cb fp) {
320  long ret;
321  bio_info_cb cb;
322
323  if (bio == NULL) {
324    return 0;
325  }
326
327  if (bio->method == NULL || bio->method->callback_ctrl == NULL) {
328    OPENSSL_PUT_ERROR(BIO, BIO_callback_ctrl, BIO_R_UNSUPPORTED_METHOD);
329    return 0;
330  }
331
332  cb = bio->callback;
333
334  if (cb != NULL) {
335    ret = cb(bio, BIO_CB_CTRL, (void *)&fp, cmd, 0, 1L);
336    if (ret <= 0) {
337      return ret;
338    }
339  }
340
341  ret = bio->method->callback_ctrl(bio, cmd, fp);
342
343  if (cb != NULL) {
344    ret = cb(bio, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, cmd, 0, ret);
345  }
346
347  return ret;
348}
349
350size_t BIO_pending(const BIO *bio) {
351  return BIO_ctrl((BIO *) bio, BIO_CTRL_PENDING, 0, NULL);
352}
353
354size_t BIO_ctrl_pending(const BIO *bio) {
355  return BIO_pending(bio);
356}
357
358size_t BIO_wpending(const BIO *bio) {
359  return BIO_ctrl((BIO *) bio, BIO_CTRL_WPENDING, 0, NULL);
360}
361
362int BIO_set_close(BIO *bio, int close_flag) {
363  return BIO_ctrl(bio, BIO_CTRL_SET_CLOSE, close_flag, NULL);
364}
365
366void BIO_set_callback(BIO *bio, bio_info_cb callback_func) {
367  bio->callback = callback_func;
368}
369
370void BIO_set_callback_arg(BIO *bio, char *arg) {
371  bio->cb_arg = arg;
372}
373
374char *BIO_get_callback_arg(const BIO *bio) {
375  return bio->cb_arg;
376}
377
378OPENSSL_EXPORT size_t BIO_number_read(const BIO *bio) {
379  return bio->num_read;
380}
381
382OPENSSL_EXPORT size_t BIO_number_written(const BIO *bio) {
383  return bio->num_write;
384}
385
386BIO *BIO_push(BIO *bio, BIO *appended_bio) {
387  BIO *last_bio;
388
389  if (bio == NULL) {
390    return bio;
391  }
392
393  last_bio = bio;
394  while (last_bio->next_bio != NULL) {
395    last_bio = last_bio->next_bio;
396  }
397
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);
402
403  return bio;
404}
405
406BIO *BIO_pop(BIO *bio) {
407  BIO *ret;
408
409  if (bio == NULL) {
410    return NULL;
411  }
412  ret = bio->next_bio;
413  BIO_ctrl(bio, BIO_CTRL_POP, 0, bio);
414  bio->next_bio = NULL;
415  return ret;
416}
417
418BIO *BIO_next(BIO *bio) {
419  if (!bio) {
420    return NULL;
421  }
422  return bio->next_bio;
423}
424
425BIO *BIO_find_type(BIO *bio, int type) {
426  int method_type, mask;
427
428  if (!bio) {
429    return NULL;
430  }
431  mask = type & 0xff;
432
433  do {
434    if (bio->method != NULL) {
435      method_type = bio->method->type;
436
437      if (!mask) {
438        if (method_type & type) {
439          return bio;
440        }
441      } else if (method_type == type) {
442        return bio;
443      }
444    }
445    bio = bio->next_bio;
446  } while (bio != NULL);
447
448  return NULL;
449}
450
451int BIO_indent(BIO *bio, unsigned indent, unsigned max_indent) {
452  if (indent > max_indent) {
453    indent = max_indent;
454  }
455
456  while (indent--) {
457    if (BIO_puts(bio, " ") != 1) {
458      return 0;
459    }
460  }
461  return 1;
462}
463
464void BIO_print_errors_fp(FILE *out) {
465  BIO *bio = BIO_new_fp(out, BIO_NOCLOSE);
466  BIO_print_errors(bio);
467  BIO_free(bio);
468}
469
470static int print_bio(const char *str, size_t len, void *bio) {
471  return BIO_write((BIO *)bio, str, len);
472}
473
474void BIO_print_errors(BIO *bio) {
475  ERR_print_errors_cb(print_bio, bio);
476}
477