Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/bio/bio.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/bio.h>
5
6 #include <assert.h>
7 #include <errno.h>
8 #include <limits.h>
9 #include <string.h>
10
11 #include <openssl/asn1.h>
12 #include <openssl/err.h>
13 #include <openssl/mem.h>
14 #include <openssl/thread.h>
15
16 #include "../internal.h"
17
18 // |callback_fn_wrap_ex| adapts the legacy callback interface |BIO_callback_fn| to the
19 // extended callback interface |BIO_callback_fn_ex|. This function should only be
20 // called when |callback_ex| is not available and the legacy callback is set.
21 //
22 // The extended interface parameters |len| and |processed| are mapped to the legacy
23 // interface parameters |argi| and |bio_ret| respectively.
24 //
25 // Returns -1 on NULL |BIO| or callback, otherwise returns the result of the legacy
26 // callback.
27 static long callback_fn_wrap_ex(BIO *bio, int oper, const char *argp,
28 size_t len, int argi, long argl, int bio_ret,
29 size_t *processed) {
30 assert(bio != NULL);
31 assert(bio->callback != NULL);
32 assert(bio->callback_ex == NULL);
33
34 /* Strip off any BIO_CB_RETURN flag */
35 int bareoper = oper & ~BIO_CB_RETURN;
36
37 if (bareoper == BIO_CB_READ || bareoper == BIO_CB_WRITE
38 || bareoper == BIO_CB_GETS) {
39 /* In this case |len| is set, and should be used instead of |argi| */
40 if (len > INT_MAX) {
41 return -1;
42 }
43
44 argi = (int)len;
45 }
46
47 if (bio_ret > 0 && (oper & BIO_CB_RETURN) && bareoper != BIO_CB_CTRL) {
48 if (*processed > INT_MAX) {
49 return -1;
50 }
51
52 bio_ret = *processed;
53 }
54
55
56 long ret = bio->callback(bio, oper, argp, argi, argl, bio_ret);
57
58 if (ret > 0 && (oper & BIO_CB_RETURN) && bareoper != BIO_CB_CTRL) {
59 *processed = (size_t)ret;
60 ret = 1;
61 }
62
63 return ret;
64 }
65
66 // |get_callback| returns the appropriate callback function for a given |BIO|, preferring
67 // the extended interface |callback_ex| over the legacy interface.
68 //
69 // When only the legacy callback is available, it is wrapped in the extended format
70 // via |callback_fn_wrap_ex| to provide a consistent interface. The extended callback
71 // provides additional parameters for length and bytes processed tracking.
72 //
73 // Returns the |callback_ex| function if available, a wrapped legacy callback if only
74 // |callback| is set, or NULL if no callbacks are set.
75 static BIO_callback_fn_ex get_callback(BIO *bio) {
76 assert(bio != NULL);
77
78 if (bio->callback_ex != NULL) {
79 return bio->callback_ex;
80 }
81 if (bio->callback != NULL) {
82 // Wrap old-style callback in extended format
83 return callback_fn_wrap_ex;
84 }
85 return NULL;
86 }
87
88 // Helper function to handle return values from |BIO_read|, |BIO_write|,
89 // |BIO_gets|, and |BIO_puts| operations.
90 static int handle_callback_return(BIO *bio, int oper, const void *buf,
91 int len, int ret) {
92
93 size_t processed = 0;
94
95 if (ret > 0) {
96 if (oper == BIO_CB_READ || oper == BIO_CB_GETS) {
97 bio->num_read += ret;
98 } else if (oper == BIO_CB_WRITE || oper == BIO_CB_PUTS) {
99 bio->num_write += ret;
100 }
101 // |callback_ex| receives the number of bytes processed via the |processed| parameter,
102 // while the legacy callback receives this information through both |argi| and |ret|.
103 // When using the legacy callback, the |processed| value will be mapped back to |ret|.
104 processed = ret;
105 ret = 1;
106 }
107
108 BIO_callback_fn_ex cb = get_callback(bio);
109 if (cb != NULL) {
110 long callback_ret = cb(bio, oper | BIO_CB_RETURN, buf, len, 0, 0L, ret, &processed);
111 if (callback_ret > INT_MAX || callback_ret < INT_MIN) {
112 return -1;
113 }
114 ret = (int)callback_ret;
115 }
116
117
118 if (ret > 0) {
119 if (processed > INT_MAX) {
120 ret = -1; // Value too large to represent as int
121 } else {
122 ret = (int)processed;
123 }
124 }
125
126 return ret;
127 }
128
129 static CRYPTO_EX_DATA_CLASS g_ex_data_class =
130 CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
131
132 BIO *BIO_new(const BIO_METHOD *method) {
133 BIO *ret = OPENSSL_zalloc(sizeof(BIO));
134 if (ret == NULL) {
135 return NULL;
136 }
137
138 ret->method = method;
139 ret->shutdown = 1;
140 ret->references = 1;
141 ret->callback_ex = NULL;
142 ret->callback = NULL;
143 CRYPTO_new_ex_data(&ret->ex_data);
144
145 if (method->create != NULL && !method->create(ret)) {
146 OPENSSL_free(ret);
147 return NULL;
148 }
149
150 return ret;
151 }
152
153 int BIO_free(BIO *bio) {
154 BIO *next_bio;
155
156 for (; bio != NULL; bio = next_bio) {
157 if (!CRYPTO_refcount_dec_and_test_zero(&bio->references)) {
158 return 0;
159 }
160
161 next_bio = BIO_pop(bio);
162
163 if (bio->method != NULL && bio->method->destroy != NULL) {
164 bio->method->destroy(bio);
165 }
166
167 BIO_callback_fn_ex cb = get_callback(bio);
168 if (cb != NULL) {
169 long ret = cb(bio, BIO_CB_FREE, NULL, 0, 0, 0L, 1L, NULL);
170 if (ret <= 0) {
171 if (ret >= INT_MIN) {
172 return (int)ret;
173 }
174 return INT_MIN;
175 }
176 }
177
178 CRYPTO_free_ex_data(&g_ex_data_class, bio, &bio->ex_data);
179 OPENSSL_free(bio);
180 }
181 return 1;
182 }
183
184 int BIO_up_ref(BIO *bio) {
185 CRYPTO_refcount_inc(&bio->references);
186 return 1;
187 }
188
189 void BIO_vfree(BIO *bio) {
190 BIO_free(bio);
191 }
192
193 void BIO_free_all(BIO *bio) {
194 BIO_free(bio);
195 }
196
197 int BIO_read(BIO *bio, void *buf, int len) {
198
199 if (bio == NULL || bio->method == NULL || bio->method->bread == NULL) {
200 OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
201 return -2;
202 }
203 if (len <= 0) {
204 return 0;
205 }
206
207 BIO_callback_fn_ex cb = get_callback(bio);
208 if (cb != NULL) {
209 long callback_ret = cb(bio, BIO_CB_READ, buf, len, 0, 0L, 1L, NULL);
210 if (callback_ret <= 0) {
211 if (callback_ret >= INT_MIN) {
212 return (int)callback_ret;
213 }
214 return INT_MIN;
215 }
216 }
217
218 if (!bio->init) {
219 OPENSSL_PUT_ERROR(BIO, BIO_R_UNINITIALIZED);
220 return -2;
221 }
222 int ret = bio->method->bread(bio, buf, len);
223
224 return handle_callback_return(bio, BIO_CB_READ, buf, len, ret);
225 }
226
227 int BIO_read_ex(BIO *bio, void *data, size_t data_len, size_t *read_bytes) {
228 if (bio == NULL || read_bytes == NULL) {
229 OPENSSL_PUT_ERROR(BIO, BIO_R_NULL_PARAMETER);
230 return 0;
231 }
232
233 int read_len = (int)data_len;
234 if (data_len > INT_MAX) {
235 read_len = INT_MAX;
236 }
237
238 int ret = BIO_read(bio, data, read_len);
239 if (ret > 0) {
240 *read_bytes = ret;
241 return 1;
242 } else {
243 *read_bytes = 0;
244 return 0;
245 }
246 }
247
248 int BIO_gets(BIO *bio, char *buf, int len) {
249 if (bio == NULL || bio->method == NULL || bio->method->bgets == NULL) {
250 OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
251 return -2;
252 }
253 if (len <= 0) {
254 return 0;
255 }
256
257 BIO_callback_fn_ex cb = get_callback(bio);
258 if (cb != NULL) {
259 long callback_ret = cb(bio, BIO_CB_GETS, buf, len, 0, 0L, 1L, NULL);
260 if (callback_ret <= 0) {
261 if (callback_ret >= INT_MIN) {
262 return (int)callback_ret;
263 }
264 return INT_MIN;
265 }
266 }
267
268 if (!bio->init) {
269 OPENSSL_PUT_ERROR(BIO, BIO_R_UNINITIALIZED);
270 return -2;
271 }
272 int ret = bio->method->bgets(bio, buf, len);
273
274 return handle_callback_return(bio, BIO_CB_GETS, buf, len, ret);
275 }
276
277 int BIO_write(BIO *bio, const void *in, int inl) {
278
279 if (bio == NULL || bio->method == NULL || bio->method->bwrite == NULL) {
280 OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
281 return -2;
282 }
283 if (inl <= 0) {
284 return 0;
285 }
286
287 BIO_callback_fn_ex cb = get_callback(bio);
288 if (cb != NULL) {
289 long callback_ret = cb(bio, BIO_CB_WRITE, in, inl, 0, 0L, 1L, NULL);
290 if (callback_ret <= 0) {
291 if (callback_ret >= INT_MIN) {
292 return (int)callback_ret;
293 }
294 return INT_MIN;
295 }
296 }
297
298 if (!bio->init) {
299 OPENSSL_PUT_ERROR(BIO, BIO_R_UNINITIALIZED);
300 return -2;
301 }
302 int ret = bio->method->bwrite(bio, in, inl);
303
304 return handle_callback_return(bio, BIO_CB_WRITE, in, inl, ret);
305 }
306
307 int BIO_write_ex(BIO *bio, const void *data, size_t data_len, size_t *written_bytes) {
308 if (bio == NULL) {
309 OPENSSL_PUT_ERROR(BIO, BIO_R_NULL_PARAMETER);
310 return 0;
311 }
312
313 int write_len = (int)data_len;
314 if (data_len > INT_MAX) {
315 write_len = INT_MAX;
316 }
317
318 int ret = BIO_write(bio, data, write_len);
319 if (ret > 0) {
320 if (written_bytes != NULL) {
321 *written_bytes = ret;
322 }
323 return 1;
324 } else {
325 if (written_bytes != NULL) {
326 *written_bytes = 0;
327 }
328 return 0;
329 }
330 }
331
332 int BIO_write_all(BIO *bio, const void *data, size_t len) {
333 const uint8_t *data_u8 = data;
334 while (len > 0) {
335 const int write_len = ((len > INT_MAX) ? INT_MAX : (int)len);
336 int ret = BIO_write(bio, data_u8, write_len);
337 assert(ret <= write_len);
338 if (ret <= 0) {
339 OPENSSL_PUT_ERROR(ASN1, ASN1_R_BUFFER_TOO_SMALL);
340 return 0;
341 }
342 data_u8 += ret;
343 len -= ret;
344 }
345 return 1;
346 }
347
348 int BIO_puts(BIO *bio, const char *in) {
349 // Check for bwrites here since we use that if bputs is NULL
350 if (bio == NULL || bio->method == NULL || (bio->method->bwrite == NULL &&
351 bio->method->bputs == NULL)) {
352 OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
353 return -2;
354 }
355
356 BIO_callback_fn_ex cb = get_callback(bio);
357 if (cb != NULL) {
358 long callback_ret = cb(bio, BIO_CB_PUTS, in, 0, 0, 0L, 1L, NULL);
359 if (callback_ret <= 0) {
360 if (callback_ret >= INT_MIN) {
361 return (int)callback_ret;
362 }
363 return INT_MIN;
364 }
365 }
366
367 if (!bio->init) {
368 OPENSSL_PUT_ERROR(BIO, BIO_R_UNINITIALIZED);
369 return -2;
370 }
371 int ret = 0;
372
373 if (bio->method->bputs != NULL) {
374 ret = bio->method->bputs(bio, in);
375 } else {
376 const size_t len = strlen(in);
377 if (len > INT_MAX) {
378 // |BIO_write| and the return value both assume the string fits in |int|.
379 OPENSSL_PUT_ERROR(BIO, ERR_R_OVERFLOW);
380 return -1;
381 }
382 ret = bio->method->bwrite(bio, in, len);
383 }
384
385 return handle_callback_return(bio, BIO_CB_PUTS, in, 0, ret);
386 }
387
388 int BIO_flush(BIO *bio) {
389 return (int)BIO_ctrl(bio, BIO_CTRL_FLUSH, 0, NULL);
390 }
391
392 long BIO_ctrl(BIO *bio, int cmd, long larg, void *parg) {
393 if (bio == NULL) {
394 return 0;
395 }
396
397 if (bio->method == NULL || bio->method->ctrl == NULL) {
398 OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
399 return -2;
400 }
401 long ret = 0;
402
403 BIO_callback_fn_ex cb = get_callback(bio);
404 if (cb != NULL) {
405 ret = cb(bio, BIO_CB_CTRL, parg, 0, cmd, larg, 1L, NULL);
406 if (ret <= 0) {
407 return ret;
408 }
409 }
410
411
412 ret = bio->method->ctrl(bio, cmd, larg, parg);
413
414 cb = get_callback(bio);
415 if (cb != NULL) {
416 ret = cb(bio, BIO_CB_CTRL | BIO_CB_RETURN, parg, 0, cmd, larg,
417 ret, NULL);
418 }
419
420 return ret;
421 }
422
423 char *BIO_ptr_ctrl(BIO *b, int cmd, long larg) {
424 char *p = NULL;
425
426 if (BIO_ctrl(b, cmd, larg, (void *)&p) <= 0) {
427 return NULL;
428 }
429
430 return p;
431 }
432
433 long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg) {
434 int i = iarg;
435
436 return BIO_ctrl(b, cmd, larg, (void *)&i);
437 }
438
439 int BIO_reset(BIO *bio) {
440 return (int)BIO_ctrl(bio, BIO_CTRL_RESET, 0, NULL);
441 }
442
443 int BIO_eof(BIO *bio) {
444 return (int)BIO_ctrl(bio, BIO_CTRL_EOF, 0, NULL);
445 }
446
447 void BIO_set_flags(BIO *bio, int flags) {
448 bio->flags |= flags;
449 }
450
451 int BIO_test_flags(const BIO *bio, int flags) {
452 return bio->flags & flags;
453 }
454
455 int BIO_should_read(const BIO *bio) {
456 return BIO_test_flags(bio, BIO_FLAGS_READ);
457 }
458
459 int BIO_should_write(const BIO *bio) {
460 return BIO_test_flags(bio, BIO_FLAGS_WRITE);
461 }
462
463 int BIO_should_retry(const BIO *bio) {
464 return BIO_test_flags(bio, BIO_FLAGS_SHOULD_RETRY);
465 }
466
467 int BIO_should_io_special(const BIO *bio) {
468 return BIO_test_flags(bio, BIO_FLAGS_IO_SPECIAL);
469 }
470
471 int BIO_get_retry_reason(const BIO *bio) { return bio->retry_reason; }
472
473 void BIO_set_retry_reason(BIO *bio, int reason) { bio->retry_reason = reason; }
474
475 void BIO_clear_flags(BIO *bio, int flags) {
476 bio->flags &= ~flags;
477 }
478
479 void BIO_set_retry_read(BIO *bio) {
480 bio->flags |= BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY;
481 }
482
483 void BIO_set_retry_write(BIO *bio) {
484 bio->flags |= BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY;
485 }
486
487 static const int kRetryFlags = BIO_FLAGS_RWS | BIO_FLAGS_SHOULD_RETRY;
488
489 int BIO_get_retry_flags(BIO *bio) {
490 return bio->flags & kRetryFlags;
491 }
492
493 void BIO_clear_retry_flags(BIO *bio) {
494 bio->flags &= ~kRetryFlags;
495 bio->retry_reason = 0;
496 }
497
498 int BIO_method_type(const BIO *bio) { return bio->method->type; }
499
500 const char *BIO_method_name(const BIO *bio) { return bio->method->name; }
501
502 void BIO_copy_next_retry(BIO *bio) {
503 BIO_clear_retry_flags(bio);
504 BIO_set_flags(bio, BIO_get_retry_flags(bio->next_bio));
505 bio->retry_reason = bio->next_bio->retry_reason;
506 }
507
508 long BIO_callback_ctrl(BIO *bio, int cmd, bio_info_cb fp) {
509 if (bio == NULL) {
510 return 0;
511 }
512
513 if (bio->method == NULL || bio->method->callback_ctrl == NULL) {
514 OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
515 return 0;
516 }
517
518 return bio->method->callback_ctrl(bio, cmd, fp);
519 }
520
521 size_t BIO_pending(const BIO *bio) {
522 const long r = BIO_ctrl((BIO *) bio, BIO_CTRL_PENDING, 0, NULL);
523 assert(r >= 0);
524
525 if (r < 0) {
526 return 0;
527 }
528 return r;
529 }
530
531 size_t BIO_ctrl_pending(const BIO *bio) {
532 return BIO_pending(bio);
533 }
534
535 size_t BIO_wpending(const BIO *bio) {
536 const long r = BIO_ctrl((BIO *) bio, BIO_CTRL_WPENDING, 0, NULL);
537 assert(r >= 0);
538
539 if (r < 0) {
540 return 0;
541 }
542 return r;
543 }
544
545 int BIO_set_close(BIO *bio, int close_flag) {
546 return (int)BIO_ctrl(bio, BIO_CTRL_SET_CLOSE, close_flag, NULL);
547 }
548
549 int BIO_get_close(BIO *bio) {
550 return (int)BIO_ctrl(bio, BIO_CTRL_GET_CLOSE, 0, NULL);
551 }
552
553 OPENSSL_EXPORT uint64_t BIO_number_read(const BIO *bio) {
554 return bio->num_read;
555 }
556
557 OPENSSL_EXPORT uint64_t BIO_number_written(const BIO *bio) {
558 return bio->num_write;
559 }
560
561 BIO *BIO_push(BIO *bio, BIO *appended_bio) {
562 BIO *last_bio;
563
564 if (bio == NULL) {
565 return bio;
566 }
567
568 last_bio = bio;
569 while (last_bio->next_bio != NULL) {
570 last_bio = last_bio->next_bio;
571 }
572
573 last_bio->next_bio = appended_bio;
574 return bio;
575 }
576
577 BIO *BIO_pop(BIO *bio) {
578 BIO *ret;
579
580 if (bio == NULL) {
581 return NULL;
582 }
583 ret = bio->next_bio;
584 bio->next_bio = NULL;
585 return ret;
586 }
587
588 BIO *BIO_next(BIO *bio) {
589 if (!bio) {
590 return NULL;
591 }
592 return bio->next_bio;
593 }
594
595 BIO *BIO_find_type(BIO *bio, int type) {
596 int method_type, mask;
597
598 if (!bio) {
599 return NULL;
600 }
601 mask = type & 0xff;
602
603 do {
604 if (bio->method != NULL) {
605 method_type = bio->method->type;
606
607 if (!mask) {
608 if (method_type & type) {
609 return bio;
610 }
611 } else if (method_type == type) {
612 return bio;
613 }
614 }
615 bio = bio->next_bio;
616 } while (bio != NULL);
617
618 return NULL;
619 }
620
621 int BIO_indent(BIO *bio, unsigned indent, unsigned max_indent) {
622 if (indent > max_indent) {
623 indent = max_indent;
624 }
625
626 while (indent--) {
627 if (BIO_puts(bio, " ") != 1) {
628 return 0;
629 }
630 }
631 return 1;
632 }
633
634 static int print_bio(const char *str, size_t len, void *bio) {
635 return BIO_write_all((BIO *)bio, str, len);
636 }
637
638 void ERR_print_errors(BIO *bio) {
639 ERR_print_errors_cb(print_bio, bio);
640 }
641
642 // bio_read_all reads everything from |bio| and prepends |prefix| to it. On
643 // success, |*out| is set to an allocated buffer (which should be freed with
644 // |OPENSSL_free|), |*out_len| is set to its length and one is returned. The
645 // buffer will contain |prefix| followed by the contents of |bio|. On failure,
646 // zero is returned.
647 //
648 // The function will fail if the size of the output would equal or exceed
649 // |max_len|.
650 static int bio_read_all(BIO *bio, uint8_t **out, size_t *out_len,
651 const uint8_t *prefix, size_t prefix_len,
652 size_t max_len) {
653 static const size_t kChunkSize = 4096;
654
655 size_t len = prefix_len + kChunkSize;
656 if (len > max_len) {
657 len = max_len;
658 }
659 if (len < prefix_len) {
660 return 0;
661 }
662 *out = OPENSSL_malloc(len);
663 if (*out == NULL) {
664 return 0;
665 }
666 OPENSSL_memcpy(*out, prefix, prefix_len);
667 size_t done = prefix_len;
668
669 for (;;) {
670 if (done == len) {
671 OPENSSL_free(*out);
672 return 0;
673 }
674 size_t todo = len - done;
675 if (todo > INT_MAX) {
676 todo = INT_MAX;
677 }
678 const int n = BIO_read(bio, *out + done, (int)todo);
679 if (n == 0) {
680 *out_len = done;
681 return 1;
682 } else if (n == -1) {
683 OPENSSL_free(*out);
684 return 0;
685 }
686
687 done += n;
688 if (len < max_len && len - done < kChunkSize / 2) {
689 len += kChunkSize;
690 if (len < kChunkSize || len > max_len) {
691 len = max_len;
692 }
693 uint8_t *new_buf = OPENSSL_realloc(*out, len);
694 if (new_buf == NULL) {
695 OPENSSL_free(*out);
696 return 0;
697 }
698 *out = new_buf;
699 }
700 }
701 }
702
703 // bio_read_full reads |len| bytes |bio| and writes them into |out|. It
704 // tolerates partial reads from |bio| and returns one on success or zero if a
705 // read fails before |len| bytes are read. On failure, it additionally sets
706 // |*out_eof_on_first_read| to whether the error was due to |bio| returning zero
707 // on the first read. |out_eof_on_first_read| may be NULL to discard the value.
708 static int bio_read_full(BIO *bio, uint8_t *out, int *out_eof_on_first_read,
709 size_t len) {
710 int first_read = 1;
711 while (len > 0) {
712 int todo = len <= INT_MAX ? (int)len : INT_MAX;
713 int ret = BIO_read(bio, out, todo);
714 if (ret <= 0) {
715 if (out_eof_on_first_read != NULL) {
716 *out_eof_on_first_read = first_read && ret == 0;
717 }
718 return 0;
719 }
720 out += ret;
721 len -= (size_t)ret;
722 first_read = 0;
723 }
724
725 return 1;
726 }
727
728 // For compatibility with existing |d2i_*_bio| callers, |BIO_read_asn1| uses
729 // |ERR_LIB_ASN1| errors.
730 OPENSSL_DECLARE_ERROR_REASON(ASN1, ASN1_R_DECODE_ERROR)
731 OPENSSL_DECLARE_ERROR_REASON(ASN1, ASN1_R_HEADER_TOO_LONG)
732 OPENSSL_DECLARE_ERROR_REASON(ASN1, ASN1_R_NOT_ENOUGH_DATA)
733 OPENSSL_DECLARE_ERROR_REASON(ASN1, ASN1_R_TOO_LONG)
734
735 int BIO_read_asn1(BIO *bio, uint8_t **out, size_t *out_len, size_t max_len) {
736 uint8_t header[6];
737
738 static const size_t kInitialHeaderLen = 2;
739 int eof_on_first_read;
740 if (!bio_read_full(bio, header, &eof_on_first_read, kInitialHeaderLen)) {
741 if (eof_on_first_read) {
742 // Historically, OpenSSL returned |ASN1_R_HEADER_TOO_LONG| when
743 // |d2i_*_bio| could not read anything. CPython conditions on this to
744 // determine if |bio| was empty.
745 OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG);
746 } else {
747 OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ENOUGH_DATA);
748 }
749 return 0;
750 }
751
752 const uint8_t tag = header[0];
753 const uint8_t length_byte = header[1];
754
755 if ((tag & 0x1f) == 0x1f) {
756 // Long form tags are not supported.
757 OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR);
758 return 0;
759 }
760
761 size_t len, header_len;
762 if ((length_byte & 0x80) == 0) {
763 // Short form length.
764 len = length_byte;
765 header_len = kInitialHeaderLen;
766 } else {
767 const size_t num_bytes = length_byte & 0x7f;
768
769 if ((tag & 0x20 /* constructed */) != 0 && num_bytes == 0) {
770 // indefinite length.
771 if (!bio_read_all(bio, out, out_len, header, kInitialHeaderLen,
772 max_len)) {
773 OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ENOUGH_DATA);
774 return 0;
775 }
776 return 1;
777 }
778
779 if (num_bytes == 0 || num_bytes > 4) {
780 OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR);
781 return 0;
782 }
783
784 if (!bio_read_full(bio, header + kInitialHeaderLen, NULL, num_bytes)) {
785 OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ENOUGH_DATA);
786 return 0;
787 }
788 header_len = kInitialHeaderLen + num_bytes;
789
790 uint32_t len32 = 0;
791 for (unsigned i = 0; i < num_bytes; i++) {
792 len32 <<= 8;
793 len32 |= header[kInitialHeaderLen + i];
794 }
795
796 if (len32 < 128) {
797 // Length should have used short-form encoding.
798 OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR);
799 return 0;
800 }
801
802 if ((len32 >> ((num_bytes-1)*8)) == 0) {
803 // Length should have been at least one byte shorter.
804 OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR);
805 return 0;
806 }
807
808 len = len32;
809 }
810
811 if (len + header_len < len ||
812 len + header_len > max_len ||
813 len > INT_MAX) {
814 OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG);
815 return 0;
816 }
817 len += header_len;
818 *out_len = len;
819
820 *out = OPENSSL_malloc(len);
821 if (*out == NULL) {
822 return 0;
823 }
824 OPENSSL_memcpy(*out, header, header_len);
825 if (!bio_read_full(bio, (*out) + header_len, NULL, len - header_len)) {
826 OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ENOUGH_DATA);
827 OPENSSL_free(*out);
828 return 0;
829 }
830
831 return 1;
832 }
833
834 void BIO_set_retry_special(BIO *bio) {
835 bio->flags |= BIO_FLAGS_READ | BIO_FLAGS_IO_SPECIAL;
836 }
837
838 int BIO_set_write_buffer_size(BIO *bio, int buffer_size) { return 0; }
839
840 static struct CRYPTO_STATIC_MUTEX g_index_lock = CRYPTO_STATIC_MUTEX_INIT;
841 static int g_index = BIO_TYPE_START;
842
843 int BIO_get_new_index(void) {
844 CRYPTO_STATIC_MUTEX_lock_write(&g_index_lock);
845 // If |g_index| exceeds 255, it will collide with the flags bits.
846 int ret = g_index > 255 ? -1 : g_index++;
847 CRYPTO_STATIC_MUTEX_unlock_write(&g_index_lock);
848 return ret;
849 }
850
851 BIO_METHOD *BIO_meth_new(int type, const char *name) {
852 BIO_METHOD *method = OPENSSL_zalloc(sizeof(BIO_METHOD));
853 if (method == NULL) {
854 return NULL;
855 }
856 method->type = type;
857 method->name = name;
858 return method;
859 }
860
861 void BIO_meth_free(BIO_METHOD *method) {
862 OPENSSL_free(method);
863 }
864
865 int BIO_meth_set_create(BIO_METHOD *method,
866 int (*create)(BIO *)) {
867 method->create = create;
868 return 1;
869 }
870
871 int (*BIO_meth_get_create(const BIO_METHOD *method)) (BIO *) {
872 return method->create;
873 }
874
875 int BIO_meth_set_destroy(BIO_METHOD *method,
876 int (*destroy)(BIO *)) {
877 method->destroy = destroy;
878 return 1;
879 }
880
881 int (*BIO_meth_get_destroy(const BIO_METHOD *method)) (BIO *) {
882 return method->destroy;
883 }
884
885 int BIO_meth_set_write(BIO_METHOD *method,
886 int (*write)(BIO *, const char *, int)) {
887 method->bwrite = write;
888 return 1;
889 }
890
891 int BIO_meth_set_read(BIO_METHOD *method,
892 int (*read)(BIO *, char *, int)) {
893 method->bread = read;
894 return 1;
895 }
896
897 int BIO_meth_set_gets(BIO_METHOD *method,
898 int (*gets)(BIO *, char *, int)) {
899 method->bgets = gets;
900 return 1;
901 }
902
903 int (*BIO_meth_get_gets(const BIO_METHOD *method)) (BIO *, char *, int) {
904 return method->bgets;
905 }
906
907 int BIO_meth_set_ctrl(BIO_METHOD *method,
908 long (*ctrl)(BIO *, int, long, void *)) {
909 method->ctrl = ctrl;
910 return 1;
911 }
912
913 long (*BIO_meth_get_ctrl(const BIO_METHOD *method)) (BIO *, int, long, void *) {
914 return method->ctrl;
915 }
916
917 int BIO_meth_set_callback_ctrl(BIO_METHOD *method,
918 long (*callback_ctrl)(BIO *, int, bio_info_cb)) {
919 method->callback_ctrl = callback_ctrl;
920 return 1;
921 }
922
923 long (*BIO_meth_get_callback_ctrl(const BIO_METHOD *method)) (BIO *, int, bio_info_cb) {
924 return method->callback_ctrl;
925 }
926
927 void BIO_set_data(BIO *bio, void *ptr) { bio->ptr = ptr; }
928
929 void *BIO_get_data(BIO *bio) { return bio->ptr; }
930
931 void BIO_set_init(BIO *bio, int init) { bio->init = init; }
932
933 int BIO_get_init(BIO *bio) { return bio->init; }
934
935 void BIO_set_shutdown(BIO *bio, int shutdown) { bio->shutdown = shutdown; }
936
937 int BIO_get_shutdown(BIO *bio) { return bio->shutdown; }
938
939 int BIO_meth_set_puts(BIO_METHOD *method, int (*puts)(BIO *, const char *)) {
940 method->bputs = puts;
941 return 1;
942 }
943
944 int (*BIO_meth_get_puts(const BIO_METHOD *method)) (BIO *, const char *) {
945 return method->bputs;
946 }
947
948 void BIO_set_callback_ex(BIO *bio, BIO_callback_fn_ex callback) {
949 bio->callback_ex = callback;
950 }
951
952 void BIO_set_callback(BIO *bio, BIO_callback_fn callback) {
953 bio->callback = callback;
954 }
955
956 void BIO_set_callback_arg(BIO *bio, char *arg) {
957 bio->cb_arg = arg;
958 }
959
960 char *BIO_get_callback_arg(const BIO *bio) {
961 return bio->cb_arg;
962 }
963
964 int BIO_get_ex_new_index(long argl, void *argp,
965 CRYPTO_EX_unused *unused,
966 CRYPTO_EX_dup *dup_unused,
967 CRYPTO_EX_free *free_func) {
968 int index;
969 if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp,
970 free_func)) {
971 return -1;
972 }
973 return index;
974 }
975
976 int BIO_set_ex_data(BIO *bio, int idx, void *data) {
977 return CRYPTO_set_ex_data(&bio->ex_data, idx, data);
978 }
979
980 void *BIO_get_ex_data(const BIO *bio, int idx) {
981 return CRYPTO_get_ex_data(&bio->ex_data, idx);
982 }