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