/src/openssl/crypto/bio/bio_lib.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include <stdio.h> |
11 | | #include <errno.h> |
12 | | #include <openssl/crypto.h> |
13 | | #include "bio_lcl.h" |
14 | | #include "internal/cryptlib.h" |
15 | | |
16 | | |
17 | | /* |
18 | | * Helper macro for the callback to determine whether an operator expects a |
19 | | * len parameter or not |
20 | | */ |
21 | 0 | #define HAS_LEN_OPER(o) ((o) == BIO_CB_READ || (o) == BIO_CB_WRITE || \ |
22 | 0 | (o) == BIO_CB_GETS) |
23 | | |
24 | | /* |
25 | | * Helper function to work out whether to call the new style callback or the old |
26 | | * one, and translate between the two. |
27 | | * |
28 | | * This has a long return type for consistency with the old callback. Similarly |
29 | | * for the "long" used for "inret" |
30 | | */ |
31 | | static long bio_call_callback(BIO *b, int oper, const char *argp, size_t len, |
32 | | int argi, long argl, long inret, size_t *processed) |
33 | 0 | { |
34 | 0 | long ret; |
35 | 0 | int bareoper; |
36 | 0 |
|
37 | 0 | if (b->callback_ex != NULL) |
38 | 0 | return b->callback_ex(b, oper, argp, len, argi, argl, inret, processed); |
39 | 0 | |
40 | 0 | /* Strip off any BIO_CB_RETURN flag */ |
41 | 0 | bareoper = oper & ~BIO_CB_RETURN; |
42 | 0 |
|
43 | 0 | /* |
44 | 0 | * We have an old style callback, so we will have to do nasty casts and |
45 | 0 | * check for overflows. |
46 | 0 | */ |
47 | 0 | if (HAS_LEN_OPER(bareoper)) { |
48 | 0 | /* In this case |len| is set, and should be used instead of |argi| */ |
49 | 0 | if (len > INT_MAX) |
50 | 0 | return -1; |
51 | 0 | |
52 | 0 | argi = (int)len; |
53 | 0 | } |
54 | 0 |
|
55 | 0 | if (inret && (oper & BIO_CB_RETURN) && bareoper != BIO_CB_CTRL) { |
56 | 0 | if (*processed > INT_MAX) |
57 | 0 | return -1; |
58 | 0 | inret = *processed; |
59 | 0 | } |
60 | 0 |
|
61 | 0 | ret = b->callback(b, oper, argp, argi, argl, inret); |
62 | 0 |
|
63 | 0 | if (ret >= 0 && (oper & BIO_CB_RETURN) && bareoper != BIO_CB_CTRL) { |
64 | 0 | *processed = (size_t)ret; |
65 | 0 | ret = 1; |
66 | 0 | } |
67 | 0 |
|
68 | 0 | return ret; |
69 | 0 | } |
70 | | |
71 | | BIO *BIO_new(const BIO_METHOD *method) |
72 | 861k | { |
73 | 861k | BIO *bio = OPENSSL_zalloc(sizeof(*bio)); |
74 | 861k | |
75 | 861k | if (bio == NULL) { |
76 | 0 | BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE); |
77 | 0 | return NULL; |
78 | 0 | } |
79 | 861k | |
80 | 861k | bio->method = method; |
81 | 861k | bio->shutdown = 1; |
82 | 861k | bio->references = 1; |
83 | 861k | |
84 | 861k | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data)) |
85 | 0 | goto err; |
86 | 861k | |
87 | 861k | bio->lock = CRYPTO_THREAD_lock_new(); |
88 | 861k | if (bio->lock == NULL) { |
89 | 0 | BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE); |
90 | 0 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data); |
91 | 0 | goto err; |
92 | 0 | } |
93 | 861k | |
94 | 861k | if (method->create != NULL && !method->create(bio)) { |
95 | 0 | BIOerr(BIO_F_BIO_NEW, ERR_R_INIT_FAIL); |
96 | 0 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data); |
97 | 0 | CRYPTO_THREAD_lock_free(bio->lock); |
98 | 0 | goto err; |
99 | 0 | } |
100 | 861k | if (method->create == NULL) |
101 | 861k | bio->init = 1; |
102 | 861k | |
103 | 861k | return bio; |
104 | 0 | |
105 | 0 | err: |
106 | 0 | OPENSSL_free(bio); |
107 | 0 | return NULL; |
108 | 861k | } |
109 | | |
110 | | int BIO_free(BIO *a) |
111 | 861k | { |
112 | 861k | int ret; |
113 | 861k | |
114 | 861k | if (a == NULL) |
115 | 861k | return 0; |
116 | 861k | |
117 | 861k | if (CRYPTO_DOWN_REF(&a->references, &ret, a->lock) <= 0) |
118 | 0 | return 0; |
119 | 861k | |
120 | 861k | REF_PRINT_COUNT("BIO", a); |
121 | 861k | if (ret > 0) |
122 | 0 | return 1; |
123 | 861k | REF_ASSERT_ISNT(ret < 0); |
124 | 861k | |
125 | 861k | if (a->callback != NULL || a->callback_ex != NULL) { |
126 | 0 | ret = (int)bio_call_callback(a, BIO_CB_FREE, NULL, 0, 0, 0L, 1L, NULL); |
127 | 0 | if (ret <= 0) |
128 | 0 | return ret; |
129 | 861k | } |
130 | 861k | |
131 | 861k | if ((a->method != NULL) && (a->method->destroy != NULL)) |
132 | 861k | a->method->destroy(a); |
133 | 861k | |
134 | 861k | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data); |
135 | 861k | |
136 | 861k | CRYPTO_THREAD_lock_free(a->lock); |
137 | 861k | |
138 | 861k | OPENSSL_free(a); |
139 | 861k | |
140 | 861k | return 1; |
141 | 861k | } |
142 | | |
143 | | void BIO_set_data(BIO *a, void *ptr) |
144 | 0 | { |
145 | 0 | a->ptr = ptr; |
146 | 0 | } |
147 | | |
148 | | void *BIO_get_data(BIO *a) |
149 | 0 | { |
150 | 0 | return a->ptr; |
151 | 0 | } |
152 | | |
153 | | void BIO_set_init(BIO *a, int init) |
154 | 0 | { |
155 | 0 | a->init = init; |
156 | 0 | } |
157 | | |
158 | | int BIO_get_init(BIO *a) |
159 | 0 | { |
160 | 0 | return a->init; |
161 | 0 | } |
162 | | |
163 | | void BIO_set_shutdown(BIO *a, int shut) |
164 | 0 | { |
165 | 0 | a->shutdown = shut; |
166 | 0 | } |
167 | | |
168 | | int BIO_get_shutdown(BIO *a) |
169 | 0 | { |
170 | 0 | return a->shutdown; |
171 | 0 | } |
172 | | |
173 | | void BIO_vfree(BIO *a) |
174 | 0 | { |
175 | 0 | BIO_free(a); |
176 | 0 | } |
177 | | |
178 | | int BIO_up_ref(BIO *a) |
179 | 0 | { |
180 | 0 | int i; |
181 | 0 |
|
182 | 0 | if (CRYPTO_UP_REF(&a->references, &i, a->lock) <= 0) |
183 | 0 | return 0; |
184 | 0 | |
185 | 0 | REF_PRINT_COUNT("BIO", a); |
186 | 0 | REF_ASSERT_ISNT(i < 2); |
187 | 0 | return ((i > 1) ? 1 : 0); |
188 | 0 | } |
189 | | |
190 | | void BIO_clear_flags(BIO *b, int flags) |
191 | 6.96M | { |
192 | 6.96M | b->flags &= ~flags; |
193 | 6.96M | } |
194 | | |
195 | | int BIO_test_flags(const BIO *b, int flags) |
196 | 0 | { |
197 | 0 | return (b->flags & flags); |
198 | 0 | } |
199 | | |
200 | | void BIO_set_flags(BIO *b, int flags) |
201 | 258k | { |
202 | 258k | b->flags |= flags; |
203 | 258k | } |
204 | | |
205 | | BIO_callback_fn BIO_get_callback(const BIO *b) |
206 | 0 | { |
207 | 0 | return b->callback; |
208 | 0 | } |
209 | | |
210 | | void BIO_set_callback(BIO *b, BIO_callback_fn cb) |
211 | 0 | { |
212 | 0 | b->callback = cb; |
213 | 0 | } |
214 | | |
215 | | BIO_callback_fn_ex BIO_get_callback_ex(const BIO *b) |
216 | 0 | { |
217 | 0 | return b->callback_ex; |
218 | 0 | } |
219 | | |
220 | | void BIO_set_callback_ex(BIO *b, BIO_callback_fn_ex cb) |
221 | 0 | { |
222 | 0 | b->callback_ex = cb; |
223 | 0 | } |
224 | | |
225 | | void BIO_set_callback_arg(BIO *b, char *arg) |
226 | 0 | { |
227 | 0 | b->cb_arg = arg; |
228 | 0 | } |
229 | | |
230 | | char *BIO_get_callback_arg(const BIO *b) |
231 | 0 | { |
232 | 0 | return b->cb_arg; |
233 | 0 | } |
234 | | |
235 | | const char *BIO_method_name(const BIO *b) |
236 | 0 | { |
237 | 0 | return b->method->name; |
238 | 0 | } |
239 | | |
240 | | int BIO_method_type(const BIO *b) |
241 | 0 | { |
242 | 0 | return b->method->type; |
243 | 0 | } |
244 | | |
245 | | /* |
246 | | * This is essentially the same as BIO_read_ex() except that it allows |
247 | | * 0 or a negative value to indicate failure (retryable or not) in the return. |
248 | | * This is for compatibility with the old style BIO_read(), where existing code |
249 | | * may make assumptions about the return value that it might get. |
250 | | */ |
251 | | static int bio_read_intern(BIO *b, void *data, size_t dlen, size_t *readbytes) |
252 | 537k | { |
253 | 537k | int ret; |
254 | 537k | |
255 | 537k | if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL)) { |
256 | 0 | BIOerr(BIO_F_BIO_READ_INTERN, BIO_R_UNSUPPORTED_METHOD); |
257 | 0 | return -2; |
258 | 0 | } |
259 | 537k | |
260 | 537k | if ((b->callback != NULL || b->callback_ex != NULL) && |
261 | 537k | ((ret = (int)bio_call_callback(b, BIO_CB_READ, data, dlen, 0, 0L, 1L, |
262 | 0 | NULL)) <= 0)) |
263 | 0 | return ret; |
264 | 537k | |
265 | 537k | if (!b->init) { |
266 | 0 | BIOerr(BIO_F_BIO_READ_INTERN, BIO_R_UNINITIALIZED); |
267 | 0 | return -2; |
268 | 0 | } |
269 | 537k | |
270 | 537k | ret = b->method->bread(b, data, dlen, readbytes); |
271 | 537k | |
272 | 537k | if (ret > 0) |
273 | 278k | b->num_read += (uint64_t)*readbytes; |
274 | 537k | |
275 | 537k | if (b->callback != NULL || b->callback_ex != NULL) |
276 | 537k | ret = (int)bio_call_callback(b, BIO_CB_READ | BIO_CB_RETURN, data, |
277 | 0 | dlen, 0, 0L, ret, readbytes); |
278 | 537k | |
279 | 537k | /* Shouldn't happen */ |
280 | 537k | if (ret > 0 && *readbytes > dlen) { |
281 | 0 | BIOerr(BIO_F_BIO_READ_INTERN, ERR_R_INTERNAL_ERROR); |
282 | 0 | return -1; |
283 | 0 | } |
284 | 537k | |
285 | 537k | return ret; |
286 | 537k | } |
287 | | |
288 | | int BIO_read(BIO *b, void *data, int dlen) |
289 | 537k | { |
290 | 537k | size_t readbytes; |
291 | 537k | int ret; |
292 | 537k | |
293 | 537k | if (dlen < 0) |
294 | 0 | return 0; |
295 | 537k | |
296 | 537k | ret = bio_read_intern(b, data, (size_t)dlen, &readbytes); |
297 | 537k | |
298 | 537k | if (ret > 0) { |
299 | 278k | /* *readbytes should always be <= dlen */ |
300 | 278k | ret = (int)readbytes; |
301 | 278k | } |
302 | 537k | |
303 | 537k | return ret; |
304 | 537k | } |
305 | | |
306 | | int BIO_read_ex(BIO *b, void *data, size_t dlen, size_t *readbytes) |
307 | 0 | { |
308 | 0 | int ret; |
309 | 0 |
|
310 | 0 | ret = bio_read_intern(b, data, dlen, readbytes); |
311 | 0 |
|
312 | 0 | if (ret > 0) |
313 | 0 | ret = 1; |
314 | 0 | else |
315 | 0 | ret = 0; |
316 | 0 |
|
317 | 0 | return ret; |
318 | 0 | } |
319 | | |
320 | | static int bio_write_intern(BIO *b, const void *data, size_t dlen, |
321 | | size_t *written) |
322 | 84.3k | { |
323 | 84.3k | int ret; |
324 | 84.3k | |
325 | 84.3k | if (b == NULL) |
326 | 84.3k | return 0; |
327 | 84.3k | |
328 | 84.3k | if ((b->method == NULL) || (b->method->bwrite == NULL)) { |
329 | 0 | BIOerr(BIO_F_BIO_WRITE_INTERN, BIO_R_UNSUPPORTED_METHOD); |
330 | 0 | return -2; |
331 | 0 | } |
332 | 84.3k | |
333 | 84.3k | if ((b->callback != NULL || b->callback_ex != NULL) && |
334 | 84.3k | ((ret = (int)bio_call_callback(b, BIO_CB_WRITE, data, dlen, 0, 0L, 1L, |
335 | 0 | NULL)) <= 0)) |
336 | 0 | return ret; |
337 | 84.3k | |
338 | 84.3k | if (!b->init) { |
339 | 0 | BIOerr(BIO_F_BIO_WRITE_INTERN, BIO_R_UNINITIALIZED); |
340 | 0 | return -2; |
341 | 0 | } |
342 | 84.3k | |
343 | 84.3k | ret = b->method->bwrite(b, data, dlen, written); |
344 | 84.3k | |
345 | 84.3k | if (ret > 0) |
346 | 84.3k | b->num_write += (uint64_t)*written; |
347 | 84.3k | |
348 | 84.3k | if (b->callback != NULL || b->callback_ex != NULL) |
349 | 84.3k | ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN, data, |
350 | 0 | dlen, 0, 0L, ret, written); |
351 | 84.3k | |
352 | 84.3k | return ret; |
353 | 84.3k | } |
354 | | |
355 | | int BIO_write(BIO *b, const void *data, int dlen) |
356 | 84.3k | { |
357 | 84.3k | size_t written; |
358 | 84.3k | int ret; |
359 | 84.3k | |
360 | 84.3k | if (dlen < 0) |
361 | 0 | return 0; |
362 | 84.3k | |
363 | 84.3k | ret = bio_write_intern(b, data, (size_t)dlen, &written); |
364 | 84.3k | |
365 | 84.3k | if (ret > 0) { |
366 | 84.3k | /* *written should always be <= dlen */ |
367 | 84.3k | ret = (int)written; |
368 | 84.3k | } |
369 | 84.3k | |
370 | 84.3k | return ret; |
371 | 84.3k | } |
372 | | |
373 | | int BIO_write_ex(BIO *b, const void *data, size_t dlen, size_t *written) |
374 | 0 | { |
375 | 0 | int ret; |
376 | 0 |
|
377 | 0 | ret = bio_write_intern(b, data, dlen, written); |
378 | 0 |
|
379 | 0 | if (ret > 0) |
380 | 0 | ret = 1; |
381 | 0 | else |
382 | 0 | ret = 0; |
383 | 0 |
|
384 | 0 | return ret; |
385 | 0 | } |
386 | | |
387 | | int BIO_puts(BIO *b, const char *buf) |
388 | 1.71M | { |
389 | 1.71M | int ret; |
390 | 1.71M | size_t written = 0; |
391 | 1.71M | |
392 | 1.71M | if ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL)) { |
393 | 0 | BIOerr(BIO_F_BIO_PUTS, BIO_R_UNSUPPORTED_METHOD); |
394 | 0 | return -2; |
395 | 0 | } |
396 | 1.71M | |
397 | 1.71M | if (b->callback != NULL || b->callback_ex != NULL) { |
398 | 0 | ret = (int)bio_call_callback(b, BIO_CB_PUTS, buf, 0, 0, 0L, 1L, NULL); |
399 | 0 | if (ret <= 0) |
400 | 0 | return ret; |
401 | 1.71M | } |
402 | 1.71M | |
403 | 1.71M | if (!b->init) { |
404 | 0 | BIOerr(BIO_F_BIO_PUTS, BIO_R_UNINITIALIZED); |
405 | 0 | return -2; |
406 | 0 | } |
407 | 1.71M | |
408 | 1.71M | ret = b->method->bputs(b, buf); |
409 | 1.71M | |
410 | 1.71M | if (ret > 0) { |
411 | 1.71M | b->num_write += (uint64_t)ret; |
412 | 1.71M | written = ret; |
413 | 1.71M | ret = 1; |
414 | 1.71M | } |
415 | 1.71M | |
416 | 1.71M | if (b->callback != NULL || b->callback_ex != NULL) |
417 | 1.71M | ret = (int)bio_call_callback(b, BIO_CB_PUTS | BIO_CB_RETURN, buf, 0, 0, |
418 | 0 | 0L, ret, &written); |
419 | 1.71M | |
420 | 1.71M | if (ret > 0) { |
421 | 1.71M | if (written > INT_MAX) { |
422 | 0 | BIOerr(BIO_F_BIO_PUTS, BIO_R_LENGTH_TOO_LONG); |
423 | 0 | ret = -1; |
424 | 1.71M | } else { |
425 | 1.71M | ret = (int)written; |
426 | 1.71M | } |
427 | 1.71M | } |
428 | 1.71M | |
429 | 1.71M | return ret; |
430 | 1.71M | } |
431 | | |
432 | | int BIO_gets(BIO *b, char *buf, int size) |
433 | 2.31M | { |
434 | 2.31M | int ret; |
435 | 2.31M | size_t readbytes = 0; |
436 | 2.31M | |
437 | 2.31M | if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL)) { |
438 | 0 | BIOerr(BIO_F_BIO_GETS, BIO_R_UNSUPPORTED_METHOD); |
439 | 0 | return -2; |
440 | 0 | } |
441 | 2.31M | |
442 | 2.31M | if (size < 0) { |
443 | 0 | BIOerr(BIO_F_BIO_GETS, BIO_R_INVALID_ARGUMENT); |
444 | 0 | return 0; |
445 | 0 | } |
446 | 2.31M | |
447 | 2.31M | if (b->callback != NULL || b->callback_ex != NULL) { |
448 | 0 | ret = (int)bio_call_callback(b, BIO_CB_GETS, buf, size, 0, 0L, 1, NULL); |
449 | 0 | if (ret <= 0) |
450 | 0 | return ret; |
451 | 2.31M | } |
452 | 2.31M | |
453 | 2.31M | if (!b->init) { |
454 | 0 | BIOerr(BIO_F_BIO_GETS, BIO_R_UNINITIALIZED); |
455 | 0 | return -2; |
456 | 0 | } |
457 | 2.31M | |
458 | 2.31M | ret = b->method->bgets(b, buf, size); |
459 | 2.31M | |
460 | 2.31M | if (ret > 0) { |
461 | 2.31M | readbytes = ret; |
462 | 2.31M | ret = 1; |
463 | 2.31M | } |
464 | 2.31M | |
465 | 2.31M | if (b->callback != NULL || b->callback_ex != NULL) |
466 | 2.31M | ret = (int)bio_call_callback(b, BIO_CB_GETS | BIO_CB_RETURN, buf, size, |
467 | 0 | 0, 0L, ret, &readbytes); |
468 | 2.31M | |
469 | 2.31M | if (ret > 0) { |
470 | 2.31M | /* Shouldn't happen */ |
471 | 2.31M | if (readbytes > (size_t)size) |
472 | 0 | ret = -1; |
473 | 2.31M | else |
474 | 2.31M | ret = (int)readbytes; |
475 | 2.31M | } |
476 | 2.31M | |
477 | 2.31M | return ret; |
478 | 2.31M | } |
479 | | |
480 | | int BIO_indent(BIO *b, int indent, int max) |
481 | 0 | { |
482 | 0 | if (indent < 0) |
483 | 0 | indent = 0; |
484 | 0 | if (indent > max) |
485 | 0 | indent = max; |
486 | 0 | while (indent--) |
487 | 0 | if (BIO_puts(b, " ") != 1) |
488 | 0 | return 0; |
489 | 0 | return 1; |
490 | 0 | } |
491 | | |
492 | | long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg) |
493 | 0 | { |
494 | 0 | int i; |
495 | 0 |
|
496 | 0 | i = iarg; |
497 | 0 | return BIO_ctrl(b, cmd, larg, (char *)&i); |
498 | 0 | } |
499 | | |
500 | | void *BIO_ptr_ctrl(BIO *b, int cmd, long larg) |
501 | 0 | { |
502 | 0 | void *p = NULL; |
503 | 0 |
|
504 | 0 | if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0) |
505 | 0 | return NULL; |
506 | 0 | else |
507 | 0 | return p; |
508 | 0 | } |
509 | | |
510 | | long BIO_ctrl(BIO *b, int cmd, long larg, void *parg) |
511 | 550k | { |
512 | 550k | long ret; |
513 | 550k | |
514 | 550k | if (b == NULL) |
515 | 550k | return 0; |
516 | 550k | |
517 | 550k | if ((b->method == NULL) || (b->method->ctrl == NULL)) { |
518 | 0 | BIOerr(BIO_F_BIO_CTRL, BIO_R_UNSUPPORTED_METHOD); |
519 | 0 | return -2; |
520 | 0 | } |
521 | 550k | |
522 | 550k | if (b->callback != NULL || b->callback_ex != NULL) { |
523 | 0 | ret = bio_call_callback(b, BIO_CB_CTRL, parg, 0, cmd, larg, 1L, NULL); |
524 | 0 | if (ret <= 0) |
525 | 0 | return ret; |
526 | 550k | } |
527 | 550k | |
528 | 550k | ret = b->method->ctrl(b, cmd, larg, parg); |
529 | 550k | |
530 | 550k | if (b->callback != NULL || b->callback_ex != NULL) |
531 | 550k | ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, 0, cmd, |
532 | 0 | larg, ret, NULL); |
533 | 550k | |
534 | 550k | return ret; |
535 | 550k | } |
536 | | |
537 | | long BIO_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp) |
538 | 0 | { |
539 | 0 | long ret; |
540 | 0 |
|
541 | 0 | if (b == NULL) |
542 | 0 | return 0; |
543 | 0 | |
544 | 0 | if ((b->method == NULL) || (b->method->callback_ctrl == NULL) |
545 | 0 | || (cmd != BIO_CTRL_SET_CALLBACK)) { |
546 | 0 | BIOerr(BIO_F_BIO_CALLBACK_CTRL, BIO_R_UNSUPPORTED_METHOD); |
547 | 0 | return -2; |
548 | 0 | } |
549 | 0 |
|
550 | 0 | if (b->callback != NULL || b->callback_ex != NULL) { |
551 | 0 | ret = bio_call_callback(b, BIO_CB_CTRL, (void *)&fp, 0, cmd, 0, 1L, |
552 | 0 | NULL); |
553 | 0 | if (ret <= 0) |
554 | 0 | return ret; |
555 | 0 | } |
556 | 0 | |
557 | 0 | ret = b->method->callback_ctrl(b, cmd, fp); |
558 | 0 |
|
559 | 0 | if (b->callback != NULL || b->callback_ex != NULL) |
560 | 0 | ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, 0, |
561 | 0 | cmd, 0, ret, NULL); |
562 | 0 |
|
563 | 0 | return ret; |
564 | 0 | } |
565 | | |
566 | | /* |
567 | | * It is unfortunate to duplicate in functions what the BIO_(w)pending macros |
568 | | * do; but those macros have inappropriate return type, and for interfacing |
569 | | * from other programming languages, C macros aren't much of a help anyway. |
570 | | */ |
571 | | size_t BIO_ctrl_pending(BIO *bio) |
572 | 0 | { |
573 | 0 | return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL); |
574 | 0 | } |
575 | | |
576 | | size_t BIO_ctrl_wpending(BIO *bio) |
577 | 0 | { |
578 | 0 | return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL); |
579 | 0 | } |
580 | | |
581 | | /* put the 'bio' on the end of b's list of operators */ |
582 | | BIO *BIO_push(BIO *b, BIO *bio) |
583 | 0 | { |
584 | 0 | BIO *lb; |
585 | 0 |
|
586 | 0 | if (b == NULL) |
587 | 0 | return bio; |
588 | 0 | lb = b; |
589 | 0 | while (lb->next_bio != NULL) |
590 | 0 | lb = lb->next_bio; |
591 | 0 | lb->next_bio = bio; |
592 | 0 | if (bio != NULL) |
593 | 0 | bio->prev_bio = lb; |
594 | 0 | /* called to do internal processing */ |
595 | 0 | BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb); |
596 | 0 | return b; |
597 | 0 | } |
598 | | |
599 | | /* Remove the first and return the rest */ |
600 | | BIO *BIO_pop(BIO *b) |
601 | 0 | { |
602 | 0 | BIO *ret; |
603 | 0 |
|
604 | 0 | if (b == NULL) |
605 | 0 | return NULL; |
606 | 0 | ret = b->next_bio; |
607 | 0 |
|
608 | 0 | BIO_ctrl(b, BIO_CTRL_POP, 0, b); |
609 | 0 |
|
610 | 0 | if (b->prev_bio != NULL) |
611 | 0 | b->prev_bio->next_bio = b->next_bio; |
612 | 0 | if (b->next_bio != NULL) |
613 | 0 | b->next_bio->prev_bio = b->prev_bio; |
614 | 0 |
|
615 | 0 | b->next_bio = NULL; |
616 | 0 | b->prev_bio = NULL; |
617 | 0 | return ret; |
618 | 0 | } |
619 | | |
620 | | BIO *BIO_get_retry_BIO(BIO *bio, int *reason) |
621 | 0 | { |
622 | 0 | BIO *b, *last; |
623 | 0 |
|
624 | 0 | b = last = bio; |
625 | 0 | for (;;) { |
626 | 0 | if (!BIO_should_retry(b)) |
627 | 0 | break; |
628 | 0 | last = b; |
629 | 0 | b = b->next_bio; |
630 | 0 | if (b == NULL) |
631 | 0 | break; |
632 | 0 | } |
633 | 0 | if (reason != NULL) |
634 | 0 | *reason = last->retry_reason; |
635 | 0 | return last; |
636 | 0 | } |
637 | | |
638 | | int BIO_get_retry_reason(BIO *bio) |
639 | 0 | { |
640 | 0 | return bio->retry_reason; |
641 | 0 | } |
642 | | |
643 | | void BIO_set_retry_reason(BIO *bio, int reason) |
644 | 0 | { |
645 | 0 | bio->retry_reason = reason; |
646 | 0 | } |
647 | | |
648 | | BIO *BIO_find_type(BIO *bio, int type) |
649 | 0 | { |
650 | 0 | int mt, mask; |
651 | 0 |
|
652 | 0 | if (bio == NULL) |
653 | 0 | return NULL; |
654 | 0 | mask = type & 0xff; |
655 | 0 | do { |
656 | 0 | if (bio->method != NULL) { |
657 | 0 | mt = bio->method->type; |
658 | 0 |
|
659 | 0 | if (!mask) { |
660 | 0 | if (mt & type) |
661 | 0 | return bio; |
662 | 0 | } else if (mt == type) |
663 | 0 | return bio; |
664 | 0 | } |
665 | 0 | bio = bio->next_bio; |
666 | 0 | } while (bio != NULL); |
667 | 0 | return NULL; |
668 | 0 | } |
669 | | |
670 | | BIO *BIO_next(BIO *b) |
671 | 0 | { |
672 | 0 | if (b == NULL) |
673 | 0 | return NULL; |
674 | 0 | return b->next_bio; |
675 | 0 | } |
676 | | |
677 | | void BIO_set_next(BIO *b, BIO *next) |
678 | 0 | { |
679 | 0 | b->next_bio = next; |
680 | 0 | } |
681 | | |
682 | | void BIO_free_all(BIO *bio) |
683 | 0 | { |
684 | 0 | BIO *b; |
685 | 0 | int ref; |
686 | 0 |
|
687 | 0 | while (bio != NULL) { |
688 | 0 | b = bio; |
689 | 0 | ref = b->references; |
690 | 0 | bio = bio->next_bio; |
691 | 0 | BIO_free(b); |
692 | 0 | /* Since ref count > 1, don't free anyone else. */ |
693 | 0 | if (ref > 1) |
694 | 0 | break; |
695 | 0 | } |
696 | 0 | } |
697 | | |
698 | | BIO *BIO_dup_chain(BIO *in) |
699 | 0 | { |
700 | 0 | BIO *ret = NULL, *eoc = NULL, *bio, *new_bio; |
701 | 0 |
|
702 | 0 | for (bio = in; bio != NULL; bio = bio->next_bio) { |
703 | 0 | if ((new_bio = BIO_new(bio->method)) == NULL) |
704 | 0 | goto err; |
705 | 0 | new_bio->callback = bio->callback; |
706 | 0 | new_bio->callback_ex = bio->callback_ex; |
707 | 0 | new_bio->cb_arg = bio->cb_arg; |
708 | 0 | new_bio->init = bio->init; |
709 | 0 | new_bio->shutdown = bio->shutdown; |
710 | 0 | new_bio->flags = bio->flags; |
711 | 0 |
|
712 | 0 | /* This will let SSL_s_sock() work with stdin/stdout */ |
713 | 0 | new_bio->num = bio->num; |
714 | 0 |
|
715 | 0 | if (!BIO_dup_state(bio, (char *)new_bio)) { |
716 | 0 | BIO_free(new_bio); |
717 | 0 | goto err; |
718 | 0 | } |
719 | 0 | |
720 | 0 | /* copy app data */ |
721 | 0 | if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data, |
722 | 0 | &bio->ex_data)) { |
723 | 0 | BIO_free(new_bio); |
724 | 0 | goto err; |
725 | 0 | } |
726 | 0 | |
727 | 0 | if (ret == NULL) { |
728 | 0 | eoc = new_bio; |
729 | 0 | ret = eoc; |
730 | 0 | } else { |
731 | 0 | BIO_push(eoc, new_bio); |
732 | 0 | eoc = new_bio; |
733 | 0 | } |
734 | 0 | } |
735 | 0 | return ret; |
736 | 0 | err: |
737 | 0 | BIO_free_all(ret); |
738 | 0 |
|
739 | 0 | return NULL; |
740 | 0 | } |
741 | | |
742 | | void BIO_copy_next_retry(BIO *b) |
743 | 0 | { |
744 | 0 | BIO_set_flags(b, BIO_get_retry_flags(b->next_bio)); |
745 | 0 | b->retry_reason = b->next_bio->retry_reason; |
746 | 0 | } |
747 | | |
748 | | int BIO_set_ex_data(BIO *bio, int idx, void *data) |
749 | 0 | { |
750 | 0 | return CRYPTO_set_ex_data(&(bio->ex_data), idx, data); |
751 | 0 | } |
752 | | |
753 | | void *BIO_get_ex_data(BIO *bio, int idx) |
754 | 0 | { |
755 | 0 | return CRYPTO_get_ex_data(&(bio->ex_data), idx); |
756 | 0 | } |
757 | | |
758 | | uint64_t BIO_number_read(BIO *bio) |
759 | 0 | { |
760 | 0 | if (bio) |
761 | 0 | return bio->num_read; |
762 | 0 | return 0; |
763 | 0 | } |
764 | | |
765 | | uint64_t BIO_number_written(BIO *bio) |
766 | 0 | { |
767 | 0 | if (bio) |
768 | 0 | return bio->num_write; |
769 | 0 | return 0; |
770 | 0 | } |
771 | | |
772 | | void bio_free_ex_data(BIO *bio) |
773 | 0 | { |
774 | 0 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data); |
775 | 0 | } |
776 | | |
777 | | void bio_cleanup(void) |
778 | 8 | { |
779 | 8 | #ifndef OPENSSL_NO_SOCK |
780 | 8 | bio_sock_cleanup_int(); |
781 | 8 | CRYPTO_THREAD_lock_free(bio_lookup_lock); |
782 | 8 | bio_lookup_lock = NULL; |
783 | 8 | #endif |
784 | 8 | CRYPTO_THREAD_lock_free(bio_type_lock); |
785 | 8 | bio_type_lock = NULL; |
786 | 8 | } |