blob: 3e788b876e8485acacef32fcf81197591f6ba494 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* 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
Adam Langley71106ad2015-05-18 17:25:20 -070059#include <assert.h>
Adam Langley95c29f32014-06-20 12:00:00 -070060#include <errno.h>
Adam Langley95c29f32014-06-20 12:00:00 -070061#include <limits.h>
Adam Langley2b2d66d2015-01-30 17:08:37 -080062#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -070063
64#include <openssl/err.h>
65#include <openssl/mem.h>
66#include <openssl/thread.h>
67
Adam Langley0da323a2015-05-15 12:49:30 -070068#include "../internal.h"
69
Adam Langley95c29f32014-06-20 12:00:00 -070070
Adam Langley95c29f32014-06-20 12:00:00 -070071BIO *BIO_new(const BIO_METHOD *method) {
72 BIO *ret = OPENSSL_malloc(sizeof(BIO));
73 if (ret == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -040074 OPENSSL_PUT_ERROR(BIO, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -070075 return NULL;
76 }
77
David Benjamin17cf2cb2016-12-13 01:07:13 -050078 OPENSSL_memset(ret, 0, sizeof(BIO));
David Benjaminebec9c32016-07-25 14:05:17 -040079 ret->method = method;
80 ret->shutdown = 1;
81 ret->references = 1;
82
83 if (method->create != NULL && !method->create(ret)) {
Adam Langley95c29f32014-06-20 12:00:00 -070084 OPENSSL_free(ret);
David Benjaminebec9c32016-07-25 14:05:17 -040085 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -070086 }
87
88 return ret;
89}
90
91int BIO_free(BIO *bio) {
92 BIO *next_bio;
93
94 for (; bio != NULL; bio = next_bio) {
Adam Langley0da323a2015-05-15 12:49:30 -070095 if (!CRYPTO_refcount_dec_and_test_zero(&bio->references)) {
Adam Langleyab9017b2015-04-20 15:25:21 -070096 return 0;
97 }
98
Adam Langley95c29f32014-06-20 12:00:00 -070099 next_bio = BIO_pop(bio);
100
Adam Langley95c29f32014-06-20 12:00:00 -0700101 if (bio->method != NULL && bio->method->destroy != NULL) {
102 bio->method->destroy(bio);
103 }
104
105 OPENSSL_free(bio);
106 }
107 return 1;
108}
109
David Benjamin96a16cd2016-08-11 12:14:47 -0400110int BIO_up_ref(BIO *bio) {
Adam Langley0da323a2015-05-15 12:49:30 -0700111 CRYPTO_refcount_inc(&bio->references);
David Benjamin96a16cd2016-08-11 12:14:47 -0400112 return 1;
Adam Langley517da2f2015-05-05 10:27:54 -0700113}
114
Adam Langley95c29f32014-06-20 12:00:00 -0700115void BIO_vfree(BIO *bio) {
116 BIO_free(bio);
117}
118
119void BIO_free_all(BIO *bio) {
120 BIO_free(bio);
121}
122
Adam Langley95c29f32014-06-20 12:00:00 -0700123int BIO_read(BIO *bio, void *buf, int len) {
Vlad Tsyrklevich02b1d192017-08-10 14:32:37 -0700124 if (bio == NULL || bio->method == NULL || bio->method->bread == NULL) {
125 OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
126 return -2;
127 }
David Benjamin3e2001c2017-08-14 19:58:06 -0400128 if (!bio->init) {
129 OPENSSL_PUT_ERROR(BIO, BIO_R_UNINITIALIZED);
130 return -2;
131 }
132 if (len <= 0) {
133 return 0;
134 }
135 int ret = bio->method->bread(bio, buf, len);
136 if (ret > 0) {
137 bio->num_read += ret;
138 }
139 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700140}
141
142int BIO_gets(BIO *bio, char *buf, int len) {
Vlad Tsyrklevich02b1d192017-08-10 14:32:37 -0700143 if (bio == NULL || bio->method == NULL || bio->method->bgets == NULL) {
144 OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
145 return -2;
146 }
David Benjamin3e2001c2017-08-14 19:58:06 -0400147 if (!bio->init) {
148 OPENSSL_PUT_ERROR(BIO, BIO_R_UNINITIALIZED);
149 return -2;
150 }
151 if (len <= 0) {
152 return 0;
153 }
154 int ret = bio->method->bgets(bio, buf, len);
155 if (ret > 0) {
156 bio->num_read += ret;
157 }
158 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700159}
160
161int BIO_write(BIO *bio, const void *in, int inl) {
Vlad Tsyrklevich02b1d192017-08-10 14:32:37 -0700162 if (bio == NULL || bio->method == NULL || bio->method->bwrite == NULL) {
163 OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
164 return -2;
165 }
David Benjamin3e2001c2017-08-14 19:58:06 -0400166 if (!bio->init) {
167 OPENSSL_PUT_ERROR(BIO, BIO_R_UNINITIALIZED);
168 return -2;
169 }
170 if (inl <= 0) {
171 return 0;
172 }
173 int ret = bio->method->bwrite(bio, in, inl);
174 if (ret > 0) {
175 bio->num_write += ret;
176 }
177 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700178}
179
180int BIO_puts(BIO *bio, const char *in) {
181 return BIO_write(bio, in, strlen(in));
182}
183
184int BIO_flush(BIO *bio) {
185 return BIO_ctrl(bio, BIO_CTRL_FLUSH, 0, NULL);
186}
187
188long BIO_ctrl(BIO *bio, int cmd, long larg, void *parg) {
Adam Langley95c29f32014-06-20 12:00:00 -0700189 if (bio == NULL) {
190 return 0;
191 }
192
193 if (bio->method == NULL || bio->method->ctrl == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400194 OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
Adam Langley95c29f32014-06-20 12:00:00 -0700195 return -2;
196 }
197
David Benjamin3e2001c2017-08-14 19:58:06 -0400198 return bio->method->ctrl(bio, cmd, larg, parg);
Adam Langley95c29f32014-06-20 12:00:00 -0700199}
200
201char *BIO_ptr_ctrl(BIO *b, int cmd, long larg) {
202 char *p = NULL;
203
204 if (BIO_ctrl(b, cmd, larg, (void *)&p) <= 0) {
205 return NULL;
206 }
207
208 return p;
209}
210
211long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg) {
212 int i = iarg;
213
214 return BIO_ctrl(b, cmd, larg, (void *)&i);
215}
216
217int BIO_reset(BIO *bio) {
218 return BIO_ctrl(bio, BIO_CTRL_RESET, 0, NULL);
219}
220
Alessandro Ghedini32d961a2016-09-11 01:49:12 +0100221int BIO_eof(BIO *bio) {
222 return BIO_ctrl(bio, BIO_CTRL_EOF, 0, NULL);
223}
224
Adam Langley95c29f32014-06-20 12:00:00 -0700225void BIO_set_flags(BIO *bio, int flags) {
226 bio->flags |= flags;
227}
228
229int BIO_test_flags(const BIO *bio, int flags) {
230 return bio->flags & flags;
231}
232
233int BIO_should_read(const BIO *bio) {
234 return BIO_test_flags(bio, BIO_FLAGS_READ);
235}
236
237int BIO_should_write(const BIO *bio) {
238 return BIO_test_flags(bio, BIO_FLAGS_WRITE);
239}
240
241int BIO_should_retry(const BIO *bio) {
242 return BIO_test_flags(bio, BIO_FLAGS_SHOULD_RETRY);
243}
244
245int BIO_should_io_special(const BIO *bio) {
246 return BIO_test_flags(bio, BIO_FLAGS_IO_SPECIAL);
247}
248
249int BIO_get_retry_reason(const BIO *bio) { return bio->retry_reason; }
250
251void BIO_clear_flags(BIO *bio, int flags) {
252 bio->flags &= ~flags;
253}
254
255void BIO_set_retry_read(BIO *bio) {
256 bio->flags |= BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY;
257}
258
259void BIO_set_retry_write(BIO *bio) {
260 bio->flags |= BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY;
261}
262
263static const int kRetryFlags = BIO_FLAGS_RWS | BIO_FLAGS_SHOULD_RETRY;
264
265int BIO_get_retry_flags(BIO *bio) {
266 return bio->flags & kRetryFlags;
267}
268
269void BIO_clear_retry_flags(BIO *bio) {
270 bio->flags &= ~kRetryFlags;
271 bio->retry_reason = 0;
272}
273
274int BIO_method_type(const BIO *bio) { return bio->method->type; }
275
276void BIO_copy_next_retry(BIO *bio) {
277 BIO_clear_retry_flags(bio);
278 BIO_set_flags(bio, BIO_get_retry_flags(bio->next_bio));
279 bio->retry_reason = bio->next_bio->retry_reason;
280}
281
282long BIO_callback_ctrl(BIO *bio, int cmd, bio_info_cb fp) {
Adam Langley95c29f32014-06-20 12:00:00 -0700283 if (bio == NULL) {
284 return 0;
285 }
286
287 if (bio->method == NULL || bio->method->callback_ctrl == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400288 OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
Adam Langley95c29f32014-06-20 12:00:00 -0700289 return 0;
290 }
291
David Benjamin3e2001c2017-08-14 19:58:06 -0400292 return bio->method->callback_ctrl(bio, cmd, fp);
Adam Langley95c29f32014-06-20 12:00:00 -0700293}
294
295size_t BIO_pending(const BIO *bio) {
Adam Langleyafdbb622016-10-21 12:26:49 -0700296 const long r = BIO_ctrl((BIO *) bio, BIO_CTRL_PENDING, 0, NULL);
297 assert(r >= 0);
298
299 if (r < 0) {
300 return 0;
301 }
302 return r;
Adam Langley95c29f32014-06-20 12:00:00 -0700303}
304
Adam Langleybed8ce72014-09-05 17:04:51 -0700305size_t BIO_ctrl_pending(const BIO *bio) {
306 return BIO_pending(bio);
307}
308
Adam Langley95c29f32014-06-20 12:00:00 -0700309size_t BIO_wpending(const BIO *bio) {
Adam Langleyafdbb622016-10-21 12:26:49 -0700310 const long r = BIO_ctrl((BIO *) bio, BIO_CTRL_WPENDING, 0, NULL);
311 assert(r >= 0);
312
313 if (r < 0) {
314 return 0;
315 }
316 return r;
Adam Langley95c29f32014-06-20 12:00:00 -0700317}
318
319int BIO_set_close(BIO *bio, int close_flag) {
320 return BIO_ctrl(bio, BIO_CTRL_SET_CLOSE, close_flag, NULL);
321}
322
Adam Langleye2c4d262014-08-12 16:18:08 -0700323OPENSSL_EXPORT size_t BIO_number_read(const BIO *bio) {
324 return bio->num_read;
325}
326
327OPENSSL_EXPORT size_t BIO_number_written(const BIO *bio) {
328 return bio->num_write;
329}
330
Adam Langley95c29f32014-06-20 12:00:00 -0700331BIO *BIO_push(BIO *bio, BIO *appended_bio) {
332 BIO *last_bio;
333
334 if (bio == NULL) {
335 return bio;
336 }
337
338 last_bio = bio;
339 while (last_bio->next_bio != NULL) {
340 last_bio = last_bio->next_bio;
341 }
342
343 last_bio->next_bio = appended_bio;
Adam Langley95c29f32014-06-20 12:00:00 -0700344 return bio;
345}
346
347BIO *BIO_pop(BIO *bio) {
348 BIO *ret;
349
350 if (bio == NULL) {
351 return NULL;
352 }
353 ret = bio->next_bio;
Adam Langley95c29f32014-06-20 12:00:00 -0700354 bio->next_bio = NULL;
355 return ret;
356}
357
358BIO *BIO_next(BIO *bio) {
359 if (!bio) {
360 return NULL;
361 }
362 return bio->next_bio;
363}
364
365BIO *BIO_find_type(BIO *bio, int type) {
366 int method_type, mask;
367
368 if (!bio) {
369 return NULL;
370 }
371 mask = type & 0xff;
372
373 do {
374 if (bio->method != NULL) {
375 method_type = bio->method->type;
376
377 if (!mask) {
378 if (method_type & type) {
379 return bio;
380 }
381 } else if (method_type == type) {
382 return bio;
383 }
384 }
385 bio = bio->next_bio;
386 } while (bio != NULL);
387
388 return NULL;
389}
390
391int BIO_indent(BIO *bio, unsigned indent, unsigned max_indent) {
392 if (indent > max_indent) {
393 indent = max_indent;
394 }
395
396 while (indent--) {
397 if (BIO_puts(bio, " ") != 1) {
398 return 0;
399 }
400 }
401 return 1;
402}
403
Adam Langley95c29f32014-06-20 12:00:00 -0700404static int print_bio(const char *str, size_t len, void *bio) {
405 return BIO_write((BIO *)bio, str, len);
406}
407
Matt Braithwaite4cd4edf2015-06-17 15:39:49 -0700408void ERR_print_errors(BIO *bio) {
David Benjamindda85e82016-10-28 20:26:42 -0400409 ERR_print_errors_cb(print_bio, bio);
Matt Braithwaite4cd4edf2015-06-17 15:39:49 -0700410}
411
David Benjamin808f8322017-08-18 14:06:02 -0400412// bio_read_all reads everything from |bio| and prepends |prefix| to it. On
413// success, |*out| is set to an allocated buffer (which should be freed with
414// |OPENSSL_free|), |*out_len| is set to its length and one is returned. The
415// buffer will contain |prefix| followed by the contents of |bio|. On failure,
416// zero is returned.
417//
418// The function will fail if the size of the output would equal or exceed
419// |max_len|.
Adam Langley71106ad2015-05-18 17:25:20 -0700420static int bio_read_all(BIO *bio, uint8_t **out, size_t *out_len,
421 const uint8_t *prefix, size_t prefix_len,
422 size_t max_len) {
423 static const size_t kChunkSize = 4096;
424
425 size_t len = prefix_len + kChunkSize;
426 if (len > max_len) {
427 len = max_len;
428 }
429 if (len < prefix_len) {
430 return 0;
431 }
432 *out = OPENSSL_malloc(len);
433 if (*out == NULL) {
434 return 0;
435 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500436 OPENSSL_memcpy(*out, prefix, prefix_len);
Adam Langley71106ad2015-05-18 17:25:20 -0700437 size_t done = prefix_len;
438
439 for (;;) {
440 if (done == len) {
441 OPENSSL_free(*out);
442 return 0;
443 }
444 const size_t todo = len - done;
445 assert(todo < INT_MAX);
446 const int n = BIO_read(bio, *out + done, todo);
447 if (n == 0) {
448 *out_len = done;
449 return 1;
450 } else if (n == -1) {
451 OPENSSL_free(*out);
452 return 0;
453 }
454
455 done += n;
456 if (len < max_len && len - done < kChunkSize / 2) {
457 len += kChunkSize;
458 if (len < kChunkSize || len > max_len) {
459 len = max_len;
460 }
461 uint8_t *new_buf = OPENSSL_realloc(*out, len);
462 if (new_buf == NULL) {
463 OPENSSL_free(*out);
464 return 0;
465 }
466 *out = new_buf;
467 }
468 }
469}
470
471int BIO_read_asn1(BIO *bio, uint8_t **out, size_t *out_len, size_t max_len) {
472 uint8_t header[6];
473
474 static const size_t kInitialHeaderLen = 2;
Adam Langley96c2a282015-06-02 14:16:44 -0700475 if (BIO_read(bio, header, kInitialHeaderLen) != (int) kInitialHeaderLen) {
Adam Langley71106ad2015-05-18 17:25:20 -0700476 return 0;
477 }
478
479 const uint8_t tag = header[0];
480 const uint8_t length_byte = header[1];
481
482 if ((tag & 0x1f) == 0x1f) {
David Benjamin808f8322017-08-18 14:06:02 -0400483 // Long form tags are not supported.
Adam Langley71106ad2015-05-18 17:25:20 -0700484 return 0;
485 }
486
487 size_t len, header_len;
488 if ((length_byte & 0x80) == 0) {
David Benjamin808f8322017-08-18 14:06:02 -0400489 // Short form length.
Adam Langley71106ad2015-05-18 17:25:20 -0700490 len = length_byte;
491 header_len = kInitialHeaderLen;
492 } else {
493 const size_t num_bytes = length_byte & 0x7f;
494
495 if ((tag & 0x20 /* constructed */) != 0 && num_bytes == 0) {
David Benjamin808f8322017-08-18 14:06:02 -0400496 // indefinite length.
Adam Langley71106ad2015-05-18 17:25:20 -0700497 return bio_read_all(bio, out, out_len, header, kInitialHeaderLen,
498 max_len);
499 }
500
501 if (num_bytes == 0 || num_bytes > 4) {
502 return 0;
503 }
504
Adam Langley96c2a282015-06-02 14:16:44 -0700505 if (BIO_read(bio, header + kInitialHeaderLen, num_bytes) !=
506 (int)num_bytes) {
Adam Langley71106ad2015-05-18 17:25:20 -0700507 return 0;
508 }
509 header_len = kInitialHeaderLen + num_bytes;
510
511 uint32_t len32 = 0;
512 unsigned i;
513 for (i = 0; i < num_bytes; i++) {
514 len32 <<= 8;
515 len32 |= header[kInitialHeaderLen + i];
516 }
517
518 if (len32 < 128) {
David Benjamin808f8322017-08-18 14:06:02 -0400519 // Length should have used short-form encoding.
Adam Langley71106ad2015-05-18 17:25:20 -0700520 return 0;
521 }
522
523 if ((len32 >> ((num_bytes-1)*8)) == 0) {
David Benjamin808f8322017-08-18 14:06:02 -0400524 // Length should have been at least one byte shorter.
Adam Langley71106ad2015-05-18 17:25:20 -0700525 return 0;
526 }
527
528 len = len32;
529 }
530
531 if (len + header_len < len ||
Adam Langley96c2a282015-06-02 14:16:44 -0700532 len + header_len > max_len ||
533 len > INT_MAX) {
Adam Langley71106ad2015-05-18 17:25:20 -0700534 return 0;
535 }
536 len += header_len;
537 *out_len = len;
538
539 *out = OPENSSL_malloc(len);
540 if (*out == NULL) {
541 return 0;
542 }
David Benjamin17cf2cb2016-12-13 01:07:13 -0500543 OPENSSL_memcpy(*out, header, header_len);
Adam Langley71106ad2015-05-18 17:25:20 -0700544 if (BIO_read(bio, (*out) + header_len, len - header_len) !=
Adam Langley96c2a282015-06-02 14:16:44 -0700545 (int) (len - header_len)) {
Adam Langley71106ad2015-05-18 17:25:20 -0700546 OPENSSL_free(*out);
547 return 0;
548 }
549
550 return 1;
551}
Adam Langleyf5b30cc2016-12-07 10:55:27 -0800552
553void BIO_set_retry_special(BIO *bio) {
554 bio->flags |= BIO_FLAGS_READ | BIO_FLAGS_IO_SPECIAL;
555}
David Benjamin41a26e82017-01-26 17:29:00 -0500556
557int BIO_set_write_buffer_size(BIO *bio, int buffer_size) { return 0; }
David Benjamin81f030b2016-08-12 14:48:19 -0400558
559static struct CRYPTO_STATIC_MUTEX g_index_lock = CRYPTO_STATIC_MUTEX_INIT;
560static int g_index = BIO_TYPE_START;
561
562int BIO_get_new_index(void) {
563 CRYPTO_STATIC_MUTEX_lock_write(&g_index_lock);
564 // If |g_index| exceeds 255, it will collide with the flags bits.
565 int ret = g_index > 255 ? -1 : g_index++;
566 CRYPTO_STATIC_MUTEX_unlock_write(&g_index_lock);
567 return ret;
568}
569
570BIO_METHOD *BIO_meth_new(int type, const char *name) {
571 BIO_METHOD *method = OPENSSL_malloc(sizeof(BIO_METHOD));
572 if (method == NULL) {
573 return NULL;
574 }
575 OPENSSL_memset(method, 0, sizeof(BIO_METHOD));
576 method->type = type;
577 method->name = name;
578 return method;
579}
580
581void BIO_meth_free(BIO_METHOD *method) {
582 OPENSSL_free(method);
583}
584
585int BIO_meth_set_create(BIO_METHOD *method,
586 int (*create)(BIO *)) {
587 method->create = create;
588 return 1;
589}
590
591int BIO_meth_set_destroy(BIO_METHOD *method,
592 int (*destroy)(BIO *)) {
593 method->destroy = destroy;
594 return 1;
595}
596
597int BIO_meth_set_write(BIO_METHOD *method,
598 int (*write)(BIO *, const char *, int)) {
599 method->bwrite = write;
600 return 1;
601}
602
603int BIO_meth_set_read(BIO_METHOD *method,
604 int (*read)(BIO *, char *, int)) {
605 method->bread = read;
606 return 1;
607}
608
609int BIO_meth_set_gets(BIO_METHOD *method,
610 int (*gets)(BIO *, char *, int)) {
611 method->bgets = gets;
612 return 1;
613}
614
615int BIO_meth_set_ctrl(BIO_METHOD *method,
616 long (*ctrl)(BIO *, int, long, void *)) {
617 method->ctrl = ctrl;
618 return 1;
619}
620
621void BIO_set_data(BIO *bio, void *ptr) { bio->ptr = ptr; }
622
623void *BIO_get_data(BIO *bio) { return bio->ptr; }
624
625void BIO_set_init(BIO *bio, int init) { bio->init = init; }
626
627int BIO_get_init(BIO *bio) { return bio->init; }
628
629void BIO_set_shutdown(BIO *bio, int shutdown) { bio->shutdown = shutdown; }
630
631int BIO_get_shutdown(BIO *bio) { return bio->shutdown; }
632
633int BIO_meth_set_puts(BIO_METHOD *method, int (*puts)(BIO *, const char *)) {
634 // Ignore the parameter. We implement |BIO_puts| using |BIO_write|.
635 return 1;
636}