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 <assert.h>
60 #include <errno.h>
61 #include <limits.h>
62 #include <string.h>
63
64 #include <openssl/asn1.h>
65 #include <openssl/err.h>
66 #include <openssl/mem.h>
67 #include <openssl/thread.h>
68
69 #include "../internal.h"
70
71
BIO_new(const BIO_METHOD * method)72 BIO *BIO_new(const BIO_METHOD *method) {
73 BIO *ret = OPENSSL_zalloc(sizeof(BIO));
74 if (ret == NULL) {
75 return NULL;
76 }
77
78 ret->method = method;
79 ret->shutdown = 1;
80 ret->references = 1;
81
82 if (method->create != NULL && !method->create(ret)) {
83 OPENSSL_free(ret);
84 return NULL;
85 }
86
87 return ret;
88 }
89
BIO_free(BIO * bio)90 int BIO_free(BIO *bio) {
91 BIO *next_bio;
92
93 for (; bio != NULL; bio = next_bio) {
94 if (!CRYPTO_refcount_dec_and_test_zero(&bio->references)) {
95 return 0;
96 }
97
98 next_bio = BIO_pop(bio);
99
100 if (bio->method != NULL && bio->method->destroy != NULL) {
101 bio->method->destroy(bio);
102 }
103
104 OPENSSL_free(bio);
105 }
106 return 1;
107 }
108
BIO_up_ref(BIO * bio)109 int BIO_up_ref(BIO *bio) {
110 CRYPTO_refcount_inc(&bio->references);
111 return 1;
112 }
113
BIO_vfree(BIO * bio)114 void BIO_vfree(BIO *bio) {
115 BIO_free(bio);
116 }
117
BIO_free_all(BIO * bio)118 void BIO_free_all(BIO *bio) {
119 BIO_free(bio);
120 }
121
BIO_read(BIO * bio,void * buf,int len)122 int BIO_read(BIO *bio, void *buf, int len) {
123 if (bio == NULL || bio->method == NULL || bio->method->bread == NULL) {
124 OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
125 return -2;
126 }
127 if (!bio->init) {
128 OPENSSL_PUT_ERROR(BIO, BIO_R_UNINITIALIZED);
129 return -2;
130 }
131 if (len <= 0) {
132 return 0;
133 }
134 int ret = bio->method->bread(bio, buf, len);
135 if (ret > 0) {
136 bio->num_read += ret;
137 }
138 return ret;
139 }
140
BIO_gets(BIO * bio,char * buf,int len)141 int BIO_gets(BIO *bio, char *buf, int len) {
142 if (bio == NULL || bio->method == NULL || bio->method->bgets == NULL) {
143 OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
144 return -2;
145 }
146 if (!bio->init) {
147 OPENSSL_PUT_ERROR(BIO, BIO_R_UNINITIALIZED);
148 return -2;
149 }
150 if (len <= 0) {
151 return 0;
152 }
153 int ret = bio->method->bgets(bio, buf, len);
154 if (ret > 0) {
155 bio->num_read += ret;
156 }
157 return ret;
158 }
159
BIO_write(BIO * bio,const void * in,int inl)160 int BIO_write(BIO *bio, const void *in, int inl) {
161 if (bio == NULL || bio->method == NULL || bio->method->bwrite == NULL) {
162 OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
163 return -2;
164 }
165 if (!bio->init) {
166 OPENSSL_PUT_ERROR(BIO, BIO_R_UNINITIALIZED);
167 return -2;
168 }
169 if (inl <= 0) {
170 return 0;
171 }
172 int ret = bio->method->bwrite(bio, in, inl);
173 if (ret > 0) {
174 bio->num_write += ret;
175 }
176 return ret;
177 }
178
BIO_write_all(BIO * bio,const void * data,size_t len)179 int BIO_write_all(BIO *bio, const void *data, size_t len) {
180 const uint8_t *data_u8 = data;
181 while (len > 0) {
182 int ret = BIO_write(bio, data_u8, len > INT_MAX ? INT_MAX : (int)len);
183 if (ret <= 0) {
184 return 0;
185 }
186 data_u8 += ret;
187 len -= ret;
188 }
189 return 1;
190 }
191
BIO_puts(BIO * bio,const char * in)192 int BIO_puts(BIO *bio, const char *in) {
193 size_t len = strlen(in);
194 if (len > INT_MAX) {
195 // |BIO_write| and the return value both assume the string fits in |int|.
196 OPENSSL_PUT_ERROR(BIO, ERR_R_OVERFLOW);
197 return -1;
198 }
199 return BIO_write(bio, in, (int)len);
200 }
201
BIO_flush(BIO * bio)202 int BIO_flush(BIO *bio) {
203 return (int)BIO_ctrl(bio, BIO_CTRL_FLUSH, 0, NULL);
204 }
205
BIO_ctrl(BIO * bio,int cmd,long larg,void * parg)206 long BIO_ctrl(BIO *bio, int cmd, long larg, void *parg) {
207 if (bio == NULL) {
208 return 0;
209 }
210
211 if (bio->method == NULL || bio->method->ctrl == NULL) {
212 OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
213 return -2;
214 }
215
216 return bio->method->ctrl(bio, cmd, larg, parg);
217 }
218
BIO_ptr_ctrl(BIO * b,int cmd,long larg)219 char *BIO_ptr_ctrl(BIO *b, int cmd, long larg) {
220 char *p = NULL;
221
222 if (BIO_ctrl(b, cmd, larg, (void *)&p) <= 0) {
223 return NULL;
224 }
225
226 return p;
227 }
228
BIO_int_ctrl(BIO * b,int cmd,long larg,int iarg)229 long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg) {
230 int i = iarg;
231
232 return BIO_ctrl(b, cmd, larg, (void *)&i);
233 }
234
BIO_reset(BIO * bio)235 int BIO_reset(BIO *bio) {
236 return (int)BIO_ctrl(bio, BIO_CTRL_RESET, 0, NULL);
237 }
238
BIO_eof(BIO * bio)239 int BIO_eof(BIO *bio) {
240 return (int)BIO_ctrl(bio, BIO_CTRL_EOF, 0, NULL);
241 }
242
BIO_set_flags(BIO * bio,int flags)243 void BIO_set_flags(BIO *bio, int flags) {
244 bio->flags |= flags;
245 }
246
BIO_test_flags(const BIO * bio,int flags)247 int BIO_test_flags(const BIO *bio, int flags) {
248 return bio->flags & flags;
249 }
250
BIO_should_read(const BIO * bio)251 int BIO_should_read(const BIO *bio) {
252 return BIO_test_flags(bio, BIO_FLAGS_READ);
253 }
254
BIO_should_write(const BIO * bio)255 int BIO_should_write(const BIO *bio) {
256 return BIO_test_flags(bio, BIO_FLAGS_WRITE);
257 }
258
BIO_should_retry(const BIO * bio)259 int BIO_should_retry(const BIO *bio) {
260 return BIO_test_flags(bio, BIO_FLAGS_SHOULD_RETRY);
261 }
262
BIO_should_io_special(const BIO * bio)263 int BIO_should_io_special(const BIO *bio) {
264 return BIO_test_flags(bio, BIO_FLAGS_IO_SPECIAL);
265 }
266
BIO_get_retry_reason(const BIO * bio)267 int BIO_get_retry_reason(const BIO *bio) { return bio->retry_reason; }
268
BIO_set_retry_reason(BIO * bio,int reason)269 void BIO_set_retry_reason(BIO *bio, int reason) { bio->retry_reason = reason; }
270
BIO_clear_flags(BIO * bio,int flags)271 void BIO_clear_flags(BIO *bio, int flags) {
272 bio->flags &= ~flags;
273 }
274
BIO_set_retry_read(BIO * bio)275 void BIO_set_retry_read(BIO *bio) {
276 bio->flags |= BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY;
277 }
278
BIO_set_retry_write(BIO * bio)279 void BIO_set_retry_write(BIO *bio) {
280 bio->flags |= BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY;
281 }
282
283 static const int kRetryFlags = BIO_FLAGS_RWS | BIO_FLAGS_SHOULD_RETRY;
284
BIO_get_retry_flags(BIO * bio)285 int BIO_get_retry_flags(BIO *bio) {
286 return bio->flags & kRetryFlags;
287 }
288
BIO_clear_retry_flags(BIO * bio)289 void BIO_clear_retry_flags(BIO *bio) {
290 bio->flags &= ~kRetryFlags;
291 bio->retry_reason = 0;
292 }
293
BIO_method_type(const BIO * bio)294 int BIO_method_type(const BIO *bio) { return bio->method->type; }
295
BIO_copy_next_retry(BIO * bio)296 void BIO_copy_next_retry(BIO *bio) {
297 BIO_clear_retry_flags(bio);
298 BIO_set_flags(bio, BIO_get_retry_flags(bio->next_bio));
299 bio->retry_reason = bio->next_bio->retry_reason;
300 }
301
BIO_callback_ctrl(BIO * bio,int cmd,bio_info_cb fp)302 long BIO_callback_ctrl(BIO *bio, int cmd, bio_info_cb fp) {
303 if (bio == NULL) {
304 return 0;
305 }
306
307 if (bio->method == NULL || bio->method->callback_ctrl == NULL) {
308 OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
309 return 0;
310 }
311
312 return bio->method->callback_ctrl(bio, cmd, fp);
313 }
314
BIO_pending(const BIO * bio)315 size_t BIO_pending(const BIO *bio) {
316 const long r = BIO_ctrl((BIO *) bio, BIO_CTRL_PENDING, 0, NULL);
317 assert(r >= 0);
318
319 if (r < 0) {
320 return 0;
321 }
322 return r;
323 }
324
BIO_ctrl_pending(const BIO * bio)325 size_t BIO_ctrl_pending(const BIO *bio) {
326 return BIO_pending(bio);
327 }
328
BIO_wpending(const BIO * bio)329 size_t BIO_wpending(const BIO *bio) {
330 const long r = BIO_ctrl((BIO *) bio, BIO_CTRL_WPENDING, 0, NULL);
331 assert(r >= 0);
332
333 if (r < 0) {
334 return 0;
335 }
336 return r;
337 }
338
BIO_set_close(BIO * bio,int close_flag)339 int BIO_set_close(BIO *bio, int close_flag) {
340 return (int)BIO_ctrl(bio, BIO_CTRL_SET_CLOSE, close_flag, NULL);
341 }
342
BIO_number_read(const BIO * bio)343 OPENSSL_EXPORT size_t BIO_number_read(const BIO *bio) {
344 return bio->num_read;
345 }
346
BIO_number_written(const BIO * bio)347 OPENSSL_EXPORT size_t BIO_number_written(const BIO *bio) {
348 return bio->num_write;
349 }
350
BIO_push(BIO * bio,BIO * appended_bio)351 BIO *BIO_push(BIO *bio, BIO *appended_bio) {
352 BIO *last_bio;
353
354 if (bio == NULL) {
355 return bio;
356 }
357
358 last_bio = bio;
359 while (last_bio->next_bio != NULL) {
360 last_bio = last_bio->next_bio;
361 }
362
363 last_bio->next_bio = appended_bio;
364 return bio;
365 }
366
BIO_pop(BIO * bio)367 BIO *BIO_pop(BIO *bio) {
368 BIO *ret;
369
370 if (bio == NULL) {
371 return NULL;
372 }
373 ret = bio->next_bio;
374 bio->next_bio = NULL;
375 return ret;
376 }
377
BIO_next(BIO * bio)378 BIO *BIO_next(BIO *bio) {
379 if (!bio) {
380 return NULL;
381 }
382 return bio->next_bio;
383 }
384
BIO_find_type(BIO * bio,int type)385 BIO *BIO_find_type(BIO *bio, int type) {
386 int method_type, mask;
387
388 if (!bio) {
389 return NULL;
390 }
391 mask = type & 0xff;
392
393 do {
394 if (bio->method != NULL) {
395 method_type = bio->method->type;
396
397 if (!mask) {
398 if (method_type & type) {
399 return bio;
400 }
401 } else if (method_type == type) {
402 return bio;
403 }
404 }
405 bio = bio->next_bio;
406 } while (bio != NULL);
407
408 return NULL;
409 }
410
BIO_indent(BIO * bio,unsigned indent,unsigned max_indent)411 int BIO_indent(BIO *bio, unsigned indent, unsigned max_indent) {
412 if (indent > max_indent) {
413 indent = max_indent;
414 }
415
416 while (indent--) {
417 if (BIO_puts(bio, " ") != 1) {
418 return 0;
419 }
420 }
421 return 1;
422 }
423
print_bio(const char * str,size_t len,void * bio)424 static int print_bio(const char *str, size_t len, void *bio) {
425 return BIO_write_all((BIO *)bio, str, len);
426 }
427
ERR_print_errors(BIO * bio)428 void ERR_print_errors(BIO *bio) {
429 ERR_print_errors_cb(print_bio, bio);
430 }
431
432 // bio_read_all reads everything from |bio| and prepends |prefix| to it. On
433 // success, |*out| is set to an allocated buffer (which should be freed with
434 // |OPENSSL_free|), |*out_len| is set to its length and one is returned. The
435 // buffer will contain |prefix| followed by the contents of |bio|. On failure,
436 // zero is returned.
437 //
438 // The function will fail if the size of the output would equal or exceed
439 // |max_len|.
bio_read_all(BIO * bio,uint8_t ** out,size_t * out_len,const uint8_t * prefix,size_t prefix_len,size_t max_len)440 static int bio_read_all(BIO *bio, uint8_t **out, size_t *out_len,
441 const uint8_t *prefix, size_t prefix_len,
442 size_t max_len) {
443 static const size_t kChunkSize = 4096;
444
445 size_t len = prefix_len + kChunkSize;
446 if (len > max_len) {
447 len = max_len;
448 }
449 if (len < prefix_len) {
450 return 0;
451 }
452 *out = OPENSSL_malloc(len);
453 if (*out == NULL) {
454 return 0;
455 }
456 OPENSSL_memcpy(*out, prefix, prefix_len);
457 size_t done = prefix_len;
458
459 for (;;) {
460 if (done == len) {
461 OPENSSL_free(*out);
462 return 0;
463 }
464 size_t todo = len - done;
465 if (todo > INT_MAX) {
466 todo = INT_MAX;
467 }
468 const int n = BIO_read(bio, *out + done, (int)todo);
469 if (n == 0) {
470 *out_len = done;
471 return 1;
472 } else if (n == -1) {
473 OPENSSL_free(*out);
474 return 0;
475 }
476
477 done += n;
478 if (len < max_len && len - done < kChunkSize / 2) {
479 len += kChunkSize;
480 if (len < kChunkSize || len > max_len) {
481 len = max_len;
482 }
483 uint8_t *new_buf = OPENSSL_realloc(*out, len);
484 if (new_buf == NULL) {
485 OPENSSL_free(*out);
486 return 0;
487 }
488 *out = new_buf;
489 }
490 }
491 }
492
493 // bio_read_full reads |len| bytes |bio| and writes them into |out|. It
494 // tolerates partial reads from |bio| and returns one on success or zero if a
495 // read fails before |len| bytes are read. On failure, it additionally sets
496 // |*out_eof_on_first_read| to whether the error was due to |bio| returning zero
497 // on the first read. |out_eof_on_first_read| may be NULL to discard the value.
bio_read_full(BIO * bio,uint8_t * out,int * out_eof_on_first_read,size_t len)498 static int bio_read_full(BIO *bio, uint8_t *out, int *out_eof_on_first_read,
499 size_t len) {
500 int first_read = 1;
501 while (len > 0) {
502 int todo = len <= INT_MAX ? (int)len : INT_MAX;
503 int ret = BIO_read(bio, out, todo);
504 if (ret <= 0) {
505 if (out_eof_on_first_read != NULL) {
506 *out_eof_on_first_read = first_read && ret == 0;
507 }
508 return 0;
509 }
510 out += ret;
511 len -= (size_t)ret;
512 first_read = 0;
513 }
514
515 return 1;
516 }
517
518 // For compatibility with existing |d2i_*_bio| callers, |BIO_read_asn1| uses
519 // |ERR_LIB_ASN1| errors.
OPENSSL_DECLARE_ERROR_REASON(ASN1,ASN1_R_DECODE_ERROR)520 OPENSSL_DECLARE_ERROR_REASON(ASN1, ASN1_R_DECODE_ERROR)
521 OPENSSL_DECLARE_ERROR_REASON(ASN1, ASN1_R_HEADER_TOO_LONG)
522 OPENSSL_DECLARE_ERROR_REASON(ASN1, ASN1_R_NOT_ENOUGH_DATA)
523 OPENSSL_DECLARE_ERROR_REASON(ASN1, ASN1_R_TOO_LONG)
524
525 int BIO_read_asn1(BIO *bio, uint8_t **out, size_t *out_len, size_t max_len) {
526 uint8_t header[6];
527
528 static const size_t kInitialHeaderLen = 2;
529 int eof_on_first_read;
530 if (!bio_read_full(bio, header, &eof_on_first_read, kInitialHeaderLen)) {
531 if (eof_on_first_read) {
532 // Historically, OpenSSL returned |ASN1_R_HEADER_TOO_LONG| when
533 // |d2i_*_bio| could not read anything. CPython conditions on this to
534 // determine if |bio| was empty.
535 OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG);
536 } else {
537 OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ENOUGH_DATA);
538 }
539 return 0;
540 }
541
542 const uint8_t tag = header[0];
543 const uint8_t length_byte = header[1];
544
545 if ((tag & 0x1f) == 0x1f) {
546 // Long form tags are not supported.
547 OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR);
548 return 0;
549 }
550
551 size_t len, header_len;
552 if ((length_byte & 0x80) == 0) {
553 // Short form length.
554 len = length_byte;
555 header_len = kInitialHeaderLen;
556 } else {
557 const size_t num_bytes = length_byte & 0x7f;
558
559 if ((tag & 0x20 /* constructed */) != 0 && num_bytes == 0) {
560 // indefinite length.
561 if (!bio_read_all(bio, out, out_len, header, kInitialHeaderLen,
562 max_len)) {
563 OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ENOUGH_DATA);
564 return 0;
565 }
566 return 1;
567 }
568
569 if (num_bytes == 0 || num_bytes > 4) {
570 OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR);
571 return 0;
572 }
573
574 if (!bio_read_full(bio, header + kInitialHeaderLen, NULL, num_bytes)) {
575 OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ENOUGH_DATA);
576 return 0;
577 }
578 header_len = kInitialHeaderLen + num_bytes;
579
580 uint32_t len32 = 0;
581 for (unsigned i = 0; i < num_bytes; i++) {
582 len32 <<= 8;
583 len32 |= header[kInitialHeaderLen + i];
584 }
585
586 if (len32 < 128) {
587 // Length should have used short-form encoding.
588 OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR);
589 return 0;
590 }
591
592 if ((len32 >> ((num_bytes-1)*8)) == 0) {
593 // Length should have been at least one byte shorter.
594 OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR);
595 return 0;
596 }
597
598 len = len32;
599 }
600
601 if (len + header_len < len ||
602 len + header_len > max_len ||
603 len > INT_MAX) {
604 OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG);
605 return 0;
606 }
607 len += header_len;
608 *out_len = len;
609
610 *out = OPENSSL_malloc(len);
611 if (*out == NULL) {
612 return 0;
613 }
614 OPENSSL_memcpy(*out, header, header_len);
615 if (!bio_read_full(bio, (*out) + header_len, NULL, len - header_len)) {
616 OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ENOUGH_DATA);
617 OPENSSL_free(*out);
618 return 0;
619 }
620
621 return 1;
622 }
623
BIO_set_retry_special(BIO * bio)624 void BIO_set_retry_special(BIO *bio) {
625 bio->flags |= BIO_FLAGS_READ | BIO_FLAGS_IO_SPECIAL;
626 }
627
BIO_set_write_buffer_size(BIO * bio,int buffer_size)628 int BIO_set_write_buffer_size(BIO *bio, int buffer_size) { return 0; }
629
630 static CRYPTO_MUTEX g_index_lock = CRYPTO_MUTEX_INIT;
631 static int g_index = BIO_TYPE_START;
632
BIO_get_new_index(void)633 int BIO_get_new_index(void) {
634 CRYPTO_MUTEX_lock_write(&g_index_lock);
635 // If |g_index| exceeds 255, it will collide with the flags bits.
636 int ret = g_index > 255 ? -1 : g_index++;
637 CRYPTO_MUTEX_unlock_write(&g_index_lock);
638 return ret;
639 }
640
BIO_meth_new(int type,const char * name)641 BIO_METHOD *BIO_meth_new(int type, const char *name) {
642 BIO_METHOD *method = OPENSSL_zalloc(sizeof(BIO_METHOD));
643 if (method == NULL) {
644 return NULL;
645 }
646 method->type = type;
647 method->name = name;
648 return method;
649 }
650
BIO_meth_free(BIO_METHOD * method)651 void BIO_meth_free(BIO_METHOD *method) {
652 OPENSSL_free(method);
653 }
654
BIO_meth_set_create(BIO_METHOD * method,int (* create)(BIO *))655 int BIO_meth_set_create(BIO_METHOD *method,
656 int (*create)(BIO *)) {
657 method->create = create;
658 return 1;
659 }
660
BIO_meth_set_destroy(BIO_METHOD * method,int (* destroy)(BIO *))661 int BIO_meth_set_destroy(BIO_METHOD *method,
662 int (*destroy)(BIO *)) {
663 method->destroy = destroy;
664 return 1;
665 }
666
BIO_meth_set_write(BIO_METHOD * method,int (* write)(BIO *,const char *,int))667 int BIO_meth_set_write(BIO_METHOD *method,
668 int (*write)(BIO *, const char *, int)) {
669 method->bwrite = write;
670 return 1;
671 }
672
BIO_meth_set_read(BIO_METHOD * method,int (* read)(BIO *,char *,int))673 int BIO_meth_set_read(BIO_METHOD *method,
674 int (*read)(BIO *, char *, int)) {
675 method->bread = read;
676 return 1;
677 }
678
BIO_meth_set_gets(BIO_METHOD * method,int (* gets)(BIO *,char *,int))679 int BIO_meth_set_gets(BIO_METHOD *method,
680 int (*gets)(BIO *, char *, int)) {
681 method->bgets = gets;
682 return 1;
683 }
684
BIO_meth_set_ctrl(BIO_METHOD * method,long (* ctrl)(BIO *,int,long,void *))685 int BIO_meth_set_ctrl(BIO_METHOD *method,
686 long (*ctrl)(BIO *, int, long, void *)) {
687 method->ctrl = ctrl;
688 return 1;
689 }
690
BIO_set_data(BIO * bio,void * ptr)691 void BIO_set_data(BIO *bio, void *ptr) { bio->ptr = ptr; }
692
BIO_get_data(BIO * bio)693 void *BIO_get_data(BIO *bio) { return bio->ptr; }
694
BIO_set_init(BIO * bio,int init)695 void BIO_set_init(BIO *bio, int init) { bio->init = init; }
696
BIO_get_init(BIO * bio)697 int BIO_get_init(BIO *bio) { return bio->init; }
698
BIO_set_shutdown(BIO * bio,int shutdown)699 void BIO_set_shutdown(BIO *bio, int shutdown) { bio->shutdown = shutdown; }
700
BIO_get_shutdown(BIO * bio)701 int BIO_get_shutdown(BIO *bio) { return bio->shutdown; }
702
BIO_meth_set_puts(BIO_METHOD * method,int (* puts)(BIO *,const char *))703 int BIO_meth_set_puts(BIO_METHOD *method, int (*puts)(BIO *, const char *)) {
704 // Ignore the parameter. We implement |BIO_puts| using |BIO_write|.
705 return 1;
706 }
707