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