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