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