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