/src/openssl30/crypto/bio/bio_lib.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (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 | | #define OPENSSL_SUPPRESS_DEPRECATED |
11 | | |
12 | | #include <stdio.h> |
13 | | #include <errno.h> |
14 | | #include <openssl/crypto.h> |
15 | | #include "internal/numbers.h" |
16 | | #include "bio_local.h" |
17 | | |
18 | | /* |
19 | | * Helper macro for the callback to determine whether an operator expects a |
20 | | * len parameter or not |
21 | | */ |
22 | 0 | #define HAS_LEN_OPER(o) ((o) == BIO_CB_READ || (o) == BIO_CB_WRITE \ |
23 | 0 | || (o) == BIO_CB_GETS) |
24 | | |
25 | | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
26 | 15.8G | #define HAS_CALLBACK(b) ((b)->callback != NULL || (b)->callback_ex != NULL) |
27 | | #else |
28 | | #define HAS_CALLBACK(b) ((b)->callback_ex != NULL) |
29 | | #endif |
30 | | /* |
31 | | * Helper function to work out whether to call the new style callback or the old |
32 | | * one, and translate between the two. |
33 | | * |
34 | | * This has a long return type for consistency with the old callback. Similarly |
35 | | * for the "long" used for "inret" |
36 | | */ |
37 | | static long bio_call_callback(BIO *b, int oper, const char *argp, size_t len, |
38 | | int argi, long argl, long inret, |
39 | | size_t *processed) |
40 | 0 | { |
41 | 0 | long ret = inret; |
42 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
43 | 0 | int bareoper; |
44 | |
|
45 | 0 | if (b->callback_ex != NULL) |
46 | 0 | #endif |
47 | 0 | return b->callback_ex(b, oper, argp, len, argi, argl, inret, processed); |
48 | | |
49 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
50 | | /* Strip off any BIO_CB_RETURN flag */ |
51 | 0 | bareoper = oper & ~BIO_CB_RETURN; |
52 | | |
53 | | /* |
54 | | * We have an old style callback, so we will have to do nasty casts and |
55 | | * check for overflows. |
56 | | */ |
57 | 0 | if (HAS_LEN_OPER(bareoper)) { |
58 | | /* In this case |len| is set, and should be used instead of |argi| */ |
59 | 0 | if (len > INT_MAX) |
60 | 0 | return -1; |
61 | | |
62 | 0 | argi = (int)len; |
63 | 0 | } |
64 | | |
65 | 0 | if (inret > 0 && (oper & BIO_CB_RETURN) && bareoper != BIO_CB_CTRL) { |
66 | 0 | if (*processed > INT_MAX) |
67 | 0 | return -1; |
68 | 0 | inret = *processed; |
69 | 0 | } |
70 | | |
71 | 0 | ret = b->callback(b, oper, argp, argi, argl, inret); |
72 | |
|
73 | 0 | if (ret > 0 && (oper & BIO_CB_RETURN) && bareoper != BIO_CB_CTRL) { |
74 | 0 | *processed = (size_t)ret; |
75 | 0 | ret = 1; |
76 | 0 | } |
77 | 0 | #endif |
78 | 0 | return ret; |
79 | 0 | } |
80 | | |
81 | | BIO *BIO_new_ex(OSSL_LIB_CTX *libctx, const BIO_METHOD *method) |
82 | 3.63M | { |
83 | 3.63M | BIO *bio = OPENSSL_zalloc(sizeof(*bio)); |
84 | | |
85 | 3.63M | if (bio == NULL) { |
86 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE); |
87 | 0 | return NULL; |
88 | 0 | } |
89 | | |
90 | 3.63M | bio->libctx = libctx; |
91 | 3.63M | bio->method = method; |
92 | 3.63M | bio->shutdown = 1; |
93 | 3.63M | bio->references = 1; |
94 | | |
95 | 3.63M | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data)) |
96 | 0 | goto err; |
97 | | |
98 | 3.63M | bio->lock = CRYPTO_THREAD_lock_new(); |
99 | 3.63M | if (bio->lock == NULL) { |
100 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE); |
101 | 0 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data); |
102 | 0 | goto err; |
103 | 0 | } |
104 | | |
105 | 3.63M | if (method->create != NULL && !method->create(bio)) { |
106 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_INIT_FAIL); |
107 | 0 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data); |
108 | 0 | CRYPTO_THREAD_lock_free(bio->lock); |
109 | 0 | goto err; |
110 | 0 | } |
111 | 3.63M | if (method->create == NULL) |
112 | 73.6k | bio->init = 1; |
113 | | |
114 | 3.63M | return bio; |
115 | | |
116 | 0 | err: |
117 | 0 | OPENSSL_free(bio); |
118 | 0 | return NULL; |
119 | 3.63M | } |
120 | | |
121 | | BIO *BIO_new(const BIO_METHOD *method) |
122 | 28.4M | { |
123 | 28.4M | return BIO_new_ex(NULL, method); |
124 | 28.4M | } |
125 | | |
126 | | int BIO_free(BIO *a) |
127 | 39.9M | { |
128 | 39.9M | int ret; |
129 | | |
130 | 39.9M | if (a == NULL) |
131 | 7.16M | return 0; |
132 | | |
133 | 32.7M | if (CRYPTO_DOWN_REF(&a->references, &ret, a->lock) <= 0) |
134 | 0 | return 0; |
135 | | |
136 | 32.7M | REF_PRINT_COUNT("BIO", a); |
137 | 32.7M | if (ret > 0) |
138 | 4.28M | return 1; |
139 | 28.4M | REF_ASSERT_ISNT(ret < 0); |
140 | | |
141 | 28.4M | if (HAS_CALLBACK(a)) { |
142 | 0 | ret = (int)bio_call_callback(a, BIO_CB_FREE, NULL, 0, 0, 0L, 1L, NULL); |
143 | 0 | if (ret <= 0) |
144 | 0 | return 0; |
145 | 0 | } |
146 | | |
147 | 28.4M | if ((a->method != NULL) && (a->method->destroy != NULL)) |
148 | 27.9M | a->method->destroy(a); |
149 | | |
150 | 28.4M | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data); |
151 | | |
152 | 28.4M | CRYPTO_THREAD_lock_free(a->lock); |
153 | | |
154 | 28.4M | OPENSSL_free(a); |
155 | | |
156 | 28.4M | return 1; |
157 | 28.4M | } |
158 | | |
159 | | void BIO_set_data(BIO *a, void *ptr) |
160 | 20.6M | { |
161 | 20.6M | a->ptr = ptr; |
162 | 20.6M | } |
163 | | |
164 | | void *BIO_get_data(BIO *a) |
165 | 2.31G | { |
166 | 2.31G | return a->ptr; |
167 | 2.31G | } |
168 | | |
169 | | void BIO_set_init(BIO *a, int init) |
170 | 26.9M | { |
171 | 26.9M | a->init = init; |
172 | 26.9M | } |
173 | | |
174 | | int BIO_get_init(BIO *a) |
175 | 0 | { |
176 | 0 | return a->init; |
177 | 0 | } |
178 | | |
179 | | void BIO_set_shutdown(BIO *a, int shut) |
180 | 0 | { |
181 | 0 | a->shutdown = shut; |
182 | 0 | } |
183 | | |
184 | | int BIO_get_shutdown(BIO *a) |
185 | 0 | { |
186 | 0 | return a->shutdown; |
187 | 0 | } |
188 | | |
189 | | void BIO_vfree(BIO *a) |
190 | 2.63M | { |
191 | 2.63M | BIO_free(a); |
192 | 2.63M | } |
193 | | |
194 | | int BIO_up_ref(BIO *a) |
195 | 4.28M | { |
196 | 4.28M | int i; |
197 | | |
198 | 4.28M | if (CRYPTO_UP_REF(&a->references, &i, a->lock) <= 0) |
199 | 0 | return 0; |
200 | | |
201 | 4.28M | REF_PRINT_COUNT("BIO", a); |
202 | 4.28M | REF_ASSERT_ISNT(i < 2); |
203 | 4.28M | return i > 1; |
204 | 4.28M | } |
205 | | |
206 | | void BIO_clear_flags(BIO *b, int flags) |
207 | 2.60G | { |
208 | 2.60G | b->flags &= ~flags; |
209 | 2.60G | } |
210 | | |
211 | | int BIO_test_flags(const BIO *b, int flags) |
212 | 79.8M | { |
213 | 79.8M | return (b->flags & flags); |
214 | 79.8M | } |
215 | | |
216 | | void BIO_set_flags(BIO *b, int flags) |
217 | 79.9M | { |
218 | 79.9M | b->flags |= flags; |
219 | 79.9M | } |
220 | | |
221 | | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
222 | | BIO_callback_fn BIO_get_callback(const BIO *b) |
223 | 0 | { |
224 | 0 | return b->callback; |
225 | 0 | } |
226 | | |
227 | | void BIO_set_callback(BIO *b, BIO_callback_fn cb) |
228 | 0 | { |
229 | 0 | b->callback = cb; |
230 | 0 | } |
231 | | #endif |
232 | | |
233 | | BIO_callback_fn_ex BIO_get_callback_ex(const BIO *b) |
234 | 0 | { |
235 | 0 | return b->callback_ex; |
236 | 0 | } |
237 | | |
238 | | void BIO_set_callback_ex(BIO *b, BIO_callback_fn_ex cb) |
239 | 0 | { |
240 | 0 | b->callback_ex = cb; |
241 | 0 | } |
242 | | |
243 | | void BIO_set_callback_arg(BIO *b, char *arg) |
244 | 0 | { |
245 | 0 | b->cb_arg = arg; |
246 | 0 | } |
247 | | |
248 | | char *BIO_get_callback_arg(const BIO *b) |
249 | 0 | { |
250 | 0 | return b->cb_arg; |
251 | 0 | } |
252 | | |
253 | | const char *BIO_method_name(const BIO *b) |
254 | 0 | { |
255 | 0 | return b->method->name; |
256 | 0 | } |
257 | | |
258 | | int BIO_method_type(const BIO *b) |
259 | 0 | { |
260 | 0 | return b->method->type; |
261 | 0 | } |
262 | | |
263 | | /* |
264 | | * This is essentially the same as BIO_read_ex() except that it allows |
265 | | * 0 or a negative value to indicate failure (retryable or not) in the return. |
266 | | * This is for compatibility with the old style BIO_read(), where existing code |
267 | | * may make assumptions about the return value that it might get. |
268 | | */ |
269 | | static int bio_read_intern(BIO *b, void *data, size_t dlen, size_t *readbytes) |
270 | 4.32G | { |
271 | 4.32G | int ret; |
272 | | |
273 | 4.32G | if (b == NULL) { |
274 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); |
275 | 0 | return -1; |
276 | 0 | } |
277 | 4.32G | if (b->method == NULL || b->method->bread == NULL) { |
278 | 0 | ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD); |
279 | 0 | return -2; |
280 | 0 | } |
281 | | |
282 | 4.32G | if (HAS_CALLBACK(b) && ((ret = (int)bio_call_callback(b, BIO_CB_READ, data, dlen, 0, 0L, 1L, NULL)) <= 0)) |
283 | 0 | return ret; |
284 | | |
285 | 4.32G | if (!b->init) { |
286 | 0 | ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED); |
287 | 0 | return -1; |
288 | 0 | } |
289 | | |
290 | 4.32G | ret = b->method->bread(b, data, dlen, readbytes); |
291 | | |
292 | 4.32G | if (ret > 0) |
293 | 4.31G | b->num_read += (uint64_t)*readbytes; |
294 | | |
295 | 4.32G | if (HAS_CALLBACK(b)) |
296 | 0 | ret = (int)bio_call_callback(b, BIO_CB_READ | BIO_CB_RETURN, data, |
297 | 0 | dlen, 0, 0L, ret, readbytes); |
298 | | |
299 | | /* Shouldn't happen */ |
300 | 4.32G | if (ret > 0 && *readbytes > dlen) { |
301 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR); |
302 | 0 | return -1; |
303 | 0 | } |
304 | | |
305 | 4.32G | return ret; |
306 | 4.32G | } |
307 | | |
308 | | int BIO_read(BIO *b, void *data, int dlen) |
309 | 2.30G | { |
310 | 2.30G | size_t readbytes; |
311 | 2.30G | int ret; |
312 | | |
313 | 2.30G | if (dlen < 0) |
314 | 0 | return 0; |
315 | | |
316 | 2.30G | ret = bio_read_intern(b, data, (size_t)dlen, &readbytes); |
317 | | |
318 | 2.30G | if (ret > 0) { |
319 | | /* *readbytes should always be <= dlen */ |
320 | 2.29G | ret = (int)readbytes; |
321 | 2.29G | } |
322 | | |
323 | 2.30G | return ret; |
324 | 2.30G | } |
325 | | |
326 | | int BIO_read_ex(BIO *b, void *data, size_t dlen, size_t *readbytes) |
327 | 2.02G | { |
328 | 2.02G | return bio_read_intern(b, data, dlen, readbytes) > 0; |
329 | 2.02G | } |
330 | | |
331 | | static int bio_write_intern(BIO *b, const void *data, size_t dlen, |
332 | | size_t *written) |
333 | 681M | { |
334 | 681M | size_t local_written; |
335 | 681M | int ret; |
336 | | |
337 | 681M | if (written != NULL) |
338 | 681M | *written = 0; |
339 | | /* |
340 | | * b == NULL is not an error but just means that zero bytes are written. |
341 | | * Do not raise an error here. |
342 | | */ |
343 | 681M | if (b == NULL) |
344 | 0 | return 0; |
345 | | |
346 | 681M | if (b->method == NULL || b->method->bwrite == NULL) { |
347 | 0 | ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD); |
348 | 0 | return -2; |
349 | 0 | } |
350 | | |
351 | 681M | if (HAS_CALLBACK(b) && ((ret = (int)bio_call_callback(b, BIO_CB_WRITE, data, dlen, 0, 0L, 1L, NULL)) <= 0)) |
352 | 0 | return ret; |
353 | | |
354 | 681M | if (!b->init) { |
355 | 0 | ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED); |
356 | 0 | return -1; |
357 | 0 | } |
358 | | |
359 | 681M | ret = b->method->bwrite(b, data, dlen, &local_written); |
360 | | |
361 | 681M | if (ret > 0) |
362 | 681M | b->num_write += (uint64_t)local_written; |
363 | | |
364 | 681M | if (HAS_CALLBACK(b)) |
365 | 0 | ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN, data, |
366 | 0 | dlen, 0, 0L, ret, &local_written); |
367 | | |
368 | 681M | if (written != NULL) |
369 | 681M | *written = local_written; |
370 | 681M | return ret; |
371 | 681M | } |
372 | | |
373 | | int BIO_write(BIO *b, const void *data, int dlen) |
374 | 498M | { |
375 | 498M | size_t written; |
376 | 498M | int ret; |
377 | | |
378 | 498M | if (dlen <= 0) |
379 | 2.12M | return 0; |
380 | | |
381 | 496M | ret = bio_write_intern(b, data, (size_t)dlen, &written); |
382 | | |
383 | 496M | if (ret > 0) { |
384 | | /* written should always be <= dlen */ |
385 | 496M | ret = (int)written; |
386 | 496M | } |
387 | | |
388 | 496M | return ret; |
389 | 498M | } |
390 | | |
391 | | int BIO_write_ex(BIO *b, const void *data, size_t dlen, size_t *written) |
392 | 184M | { |
393 | 184M | return bio_write_intern(b, data, dlen, written) > 0 |
394 | 0 | || (b != NULL && dlen == 0); /* order is important for *written */ |
395 | 184M | } |
396 | | |
397 | | int BIO_puts(BIO *b, const char *buf) |
398 | 84.0M | { |
399 | 84.0M | int ret; |
400 | 84.0M | size_t written = 0; |
401 | | |
402 | 84.0M | if (b == NULL) { |
403 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); |
404 | 0 | return -1; |
405 | 0 | } |
406 | 84.0M | if (b->method == NULL || b->method->bputs == NULL) { |
407 | 0 | ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD); |
408 | 0 | return -2; |
409 | 0 | } |
410 | | |
411 | 84.0M | if (HAS_CALLBACK(b)) { |
412 | 0 | ret = (int)bio_call_callback(b, BIO_CB_PUTS, buf, 0, 0, 0L, 1L, NULL); |
413 | 0 | if (ret <= 0) |
414 | 0 | return ret; |
415 | 0 | } |
416 | | |
417 | 84.0M | if (!b->init) { |
418 | 0 | ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED); |
419 | 0 | return -1; |
420 | 0 | } |
421 | | |
422 | 84.0M | ret = b->method->bputs(b, buf); |
423 | | |
424 | 84.0M | if (ret > 0) { |
425 | 84.0M | b->num_write += (uint64_t)ret; |
426 | 84.0M | written = ret; |
427 | 84.0M | ret = 1; |
428 | 84.0M | } |
429 | | |
430 | 84.0M | if (HAS_CALLBACK(b)) |
431 | 0 | ret = (int)bio_call_callback(b, BIO_CB_PUTS | BIO_CB_RETURN, buf, 0, 0, |
432 | 0 | 0L, ret, &written); |
433 | | |
434 | 84.0M | if (ret > 0) { |
435 | 84.0M | if (written > INT_MAX) { |
436 | 0 | ERR_raise(ERR_LIB_BIO, BIO_R_LENGTH_TOO_LONG); |
437 | 0 | ret = -1; |
438 | 84.0M | } else { |
439 | 84.0M | ret = (int)written; |
440 | 84.0M | } |
441 | 84.0M | } |
442 | | |
443 | 84.0M | return ret; |
444 | 84.0M | } |
445 | | |
446 | | int BIO_gets(BIO *b, char *buf, int size) |
447 | 91.9M | { |
448 | 91.9M | int ret; |
449 | 91.9M | size_t readbytes = 0; |
450 | | |
451 | 91.9M | if (b == NULL) { |
452 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); |
453 | 0 | return -1; |
454 | 0 | } |
455 | 91.9M | if (b->method == NULL || b->method->bgets == NULL) { |
456 | 0 | ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD); |
457 | 0 | return -2; |
458 | 0 | } |
459 | | |
460 | 91.9M | if (size < 0) { |
461 | 0 | ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT); |
462 | 0 | return -1; |
463 | 0 | } |
464 | | |
465 | 91.9M | if (HAS_CALLBACK(b)) { |
466 | 0 | ret = (int)bio_call_callback(b, BIO_CB_GETS, buf, size, 0, 0L, 1, NULL); |
467 | 0 | if (ret <= 0) |
468 | 0 | return ret; |
469 | 0 | } |
470 | | |
471 | 91.9M | if (!b->init) { |
472 | 0 | ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED); |
473 | 0 | return -1; |
474 | 0 | } |
475 | | |
476 | 91.9M | ret = b->method->bgets(b, buf, size); |
477 | | |
478 | 91.9M | if (ret > 0) { |
479 | 91.8M | readbytes = ret; |
480 | 91.8M | ret = 1; |
481 | 91.8M | } |
482 | | |
483 | 91.9M | if (HAS_CALLBACK(b)) |
484 | 0 | ret = (int)bio_call_callback(b, BIO_CB_GETS | BIO_CB_RETURN, buf, size, |
485 | 0 | 0, 0L, ret, &readbytes); |
486 | | |
487 | 91.9M | if (ret > 0) { |
488 | | /* Shouldn't happen */ |
489 | 91.8M | if (readbytes > (size_t)size) |
490 | 0 | ret = -1; |
491 | 91.8M | else |
492 | 91.8M | ret = (int)readbytes; |
493 | 91.8M | } |
494 | | |
495 | 91.9M | return ret; |
496 | 91.9M | } |
497 | | |
498 | | int BIO_get_line(BIO *bio, char *buf, int size) |
499 | 54.4M | { |
500 | 54.4M | int ret = 0; |
501 | 54.4M | char *ptr = buf; |
502 | | |
503 | 54.4M | if (buf == NULL) { |
504 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); |
505 | 0 | return -1; |
506 | 0 | } |
507 | 54.4M | if (size <= 0) { |
508 | 0 | ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT); |
509 | 0 | return -1; |
510 | 0 | } |
511 | 54.4M | *buf = '\0'; |
512 | | |
513 | 54.4M | if (bio == NULL) { |
514 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); |
515 | 0 | return -1; |
516 | 0 | } |
517 | 54.4M | if (!bio->init) { |
518 | 0 | ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED); |
519 | 0 | return -1; |
520 | 0 | } |
521 | | |
522 | 125M | while (size-- > 1 && (ret = BIO_read(bio, ptr, 1)) > 0) |
523 | 125M | if (*ptr++ == '\n') |
524 | 54.4M | break; |
525 | 54.4M | *ptr = '\0'; |
526 | 54.4M | return ret > 0 || BIO_eof(bio) ? ptr - buf : ret; |
527 | 54.4M | } |
528 | | |
529 | | int BIO_indent(BIO *b, int indent, int max) |
530 | 5.31M | { |
531 | 5.31M | if (indent < 0) |
532 | 0 | indent = 0; |
533 | 5.31M | if (indent > max) |
534 | 0 | indent = max; |
535 | 46.4M | while (indent--) |
536 | 41.1M | if (BIO_puts(b, " ") != 1) |
537 | 0 | return 0; |
538 | 5.31M | return 1; |
539 | 5.31M | } |
540 | | |
541 | | long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg) |
542 | 202k | { |
543 | 202k | int i; |
544 | | |
545 | 202k | i = iarg; |
546 | 202k | return BIO_ctrl(b, cmd, larg, (char *)&i); |
547 | 202k | } |
548 | | |
549 | | void *BIO_ptr_ctrl(BIO *b, int cmd, long larg) |
550 | 0 | { |
551 | 0 | void *p = NULL; |
552 | |
|
553 | 0 | if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0) |
554 | 0 | return NULL; |
555 | 0 | else |
556 | 0 | return p; |
557 | 0 | } |
558 | | |
559 | | long BIO_ctrl(BIO *b, int cmd, long larg, void *parg) |
560 | 247M | { |
561 | 247M | long ret; |
562 | | |
563 | 247M | if (b == NULL) |
564 | 0 | return -1; |
565 | 247M | if (b->method == NULL || b->method->ctrl == NULL) { |
566 | 0 | ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD); |
567 | 0 | return -2; |
568 | 0 | } |
569 | | |
570 | 247M | if (HAS_CALLBACK(b)) { |
571 | 0 | ret = bio_call_callback(b, BIO_CB_CTRL, parg, 0, cmd, larg, 1L, NULL); |
572 | 0 | if (ret <= 0) |
573 | 0 | return ret; |
574 | 0 | } |
575 | | |
576 | 247M | ret = b->method->ctrl(b, cmd, larg, parg); |
577 | | |
578 | 247M | if (HAS_CALLBACK(b)) |
579 | 0 | ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, 0, cmd, |
580 | 0 | larg, ret, NULL); |
581 | | |
582 | 247M | return ret; |
583 | 247M | } |
584 | | |
585 | | long BIO_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp) |
586 | 0 | { |
587 | 0 | long ret; |
588 | |
|
589 | 0 | if (b == NULL) |
590 | 0 | return -2; |
591 | 0 | if (b->method == NULL || b->method->callback_ctrl == NULL |
592 | 0 | || cmd != BIO_CTRL_SET_CALLBACK) { |
593 | 0 | ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD); |
594 | 0 | return -2; |
595 | 0 | } |
596 | | |
597 | 0 | if (HAS_CALLBACK(b)) { |
598 | 0 | ret = bio_call_callback(b, BIO_CB_CTRL, (void *)&fp, 0, cmd, 0, 1L, |
599 | 0 | NULL); |
600 | 0 | if (ret <= 0) |
601 | 0 | return ret; |
602 | 0 | } |
603 | | |
604 | 0 | ret = b->method->callback_ctrl(b, cmd, fp); |
605 | |
|
606 | 0 | if (HAS_CALLBACK(b)) |
607 | 0 | ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, 0, |
608 | 0 | cmd, 0, ret, NULL); |
609 | |
|
610 | 0 | return ret; |
611 | 0 | } |
612 | | |
613 | | /* |
614 | | * It is unfortunate to duplicate in functions what the BIO_(w)pending macros |
615 | | * do; but those macros have inappropriate return type, and for interfacing |
616 | | * from other programming languages, C macros aren't much of a help anyway. |
617 | | */ |
618 | | size_t BIO_ctrl_pending(BIO *bio) |
619 | 0 | { |
620 | 0 | long ret = BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL); |
621 | |
|
622 | 0 | if (ret < 0) |
623 | 0 | ret = 0; |
624 | | #if LONG_MAX > SIZE_MAX |
625 | | if (ret > SIZE_MAX) |
626 | | ret = SIZE_MAX; |
627 | | #endif |
628 | 0 | return (size_t)ret; |
629 | 0 | } |
630 | | |
631 | | size_t BIO_ctrl_wpending(BIO *bio) |
632 | 0 | { |
633 | 0 | long ret = BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL); |
634 | |
|
635 | 0 | if (ret < 0) |
636 | 0 | ret = 0; |
637 | | #if LONG_MAX > SIZE_MAX |
638 | | if (ret > SIZE_MAX) |
639 | | ret = SIZE_MAX; |
640 | | #endif |
641 | 0 | return (size_t)ret; |
642 | 0 | } |
643 | | |
644 | | /* put the 'bio' on the end of b's list of operators */ |
645 | | BIO *BIO_push(BIO *b, BIO *bio) |
646 | 14.5M | { |
647 | 14.5M | BIO *lb; |
648 | | |
649 | 14.5M | if (b == NULL) |
650 | 0 | return bio; |
651 | 14.5M | lb = b; |
652 | 14.5M | while (lb->next_bio != NULL) |
653 | 0 | lb = lb->next_bio; |
654 | 14.5M | lb->next_bio = bio; |
655 | 14.5M | if (bio != NULL) |
656 | 14.5M | bio->prev_bio = lb; |
657 | | /* called to do internal processing */ |
658 | 14.5M | BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb); |
659 | 14.5M | return b; |
660 | 14.5M | } |
661 | | |
662 | | /* Remove the first and return the rest */ |
663 | | BIO *BIO_pop(BIO *b) |
664 | 14.5M | { |
665 | 14.5M | BIO *ret; |
666 | | |
667 | 14.5M | if (b == NULL) |
668 | 0 | return NULL; |
669 | 14.5M | ret = b->next_bio; |
670 | | |
671 | 14.5M | BIO_ctrl(b, BIO_CTRL_POP, 0, b); |
672 | | |
673 | 14.5M | if (b->prev_bio != NULL) |
674 | 0 | b->prev_bio->next_bio = b->next_bio; |
675 | 14.5M | if (b->next_bio != NULL) |
676 | 14.5M | b->next_bio->prev_bio = b->prev_bio; |
677 | | |
678 | 14.5M | b->next_bio = NULL; |
679 | 14.5M | b->prev_bio = NULL; |
680 | 14.5M | return ret; |
681 | 14.5M | } |
682 | | |
683 | | BIO *BIO_get_retry_BIO(BIO *bio, int *reason) |
684 | 0 | { |
685 | 0 | BIO *b, *last; |
686 | |
|
687 | 0 | b = last = bio; |
688 | 0 | for (;;) { |
689 | 0 | if (!BIO_should_retry(b)) |
690 | 0 | break; |
691 | 0 | last = b; |
692 | 0 | b = b->next_bio; |
693 | 0 | if (b == NULL) |
694 | 0 | break; |
695 | 0 | } |
696 | 0 | if (reason != NULL) |
697 | 0 | *reason = last->retry_reason; |
698 | 0 | return last; |
699 | 0 | } |
700 | | |
701 | | int BIO_get_retry_reason(BIO *bio) |
702 | 0 | { |
703 | 0 | return bio->retry_reason; |
704 | 0 | } |
705 | | |
706 | | void BIO_set_retry_reason(BIO *bio, int reason) |
707 | 0 | { |
708 | 0 | bio->retry_reason = reason; |
709 | 0 | } |
710 | | |
711 | | BIO *BIO_find_type(BIO *bio, int type) |
712 | 0 | { |
713 | 0 | int mt, mask; |
714 | |
|
715 | 0 | if (bio == NULL) { |
716 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); |
717 | 0 | return NULL; |
718 | 0 | } |
719 | 0 | mask = type & 0xff; |
720 | 0 | do { |
721 | 0 | if (bio->method != NULL) { |
722 | 0 | mt = bio->method->type; |
723 | |
|
724 | 0 | if (!mask) { |
725 | 0 | if (mt & type) |
726 | 0 | return bio; |
727 | 0 | } else if (mt == type) { |
728 | 0 | return bio; |
729 | 0 | } |
730 | 0 | } |
731 | 0 | bio = bio->next_bio; |
732 | 0 | } while (bio != NULL); |
733 | 0 | return NULL; |
734 | 0 | } |
735 | | |
736 | | BIO *BIO_next(BIO *b) |
737 | 190M | { |
738 | 190M | if (b == NULL) |
739 | 0 | return NULL; |
740 | 190M | return b->next_bio; |
741 | 190M | } |
742 | | |
743 | | void BIO_set_next(BIO *b, BIO *next) |
744 | 0 | { |
745 | 0 | b->next_bio = next; |
746 | 0 | } |
747 | | |
748 | | void BIO_free_all(BIO *bio) |
749 | 855k | { |
750 | 855k | BIO *b; |
751 | 855k | int ref; |
752 | | |
753 | 1.23M | while (bio != NULL) { |
754 | 427k | b = bio; |
755 | 427k | ref = b->references; |
756 | 427k | bio = bio->next_bio; |
757 | 427k | BIO_free(b); |
758 | | /* Since ref count > 1, don't free anyone else. */ |
759 | 427k | if (ref > 1) |
760 | 50.4k | break; |
761 | 427k | } |
762 | 855k | } |
763 | | |
764 | | BIO *BIO_dup_chain(BIO *in) |
765 | 0 | { |
766 | 0 | BIO *ret = NULL, *eoc = NULL, *bio, *new_bio; |
767 | |
|
768 | 0 | for (bio = in; bio != NULL; bio = bio->next_bio) { |
769 | 0 | if ((new_bio = BIO_new(bio->method)) == NULL) |
770 | 0 | goto err; |
771 | 0 | #ifndef OPENSSL_NO_DEPRECATED_3_0 |
772 | 0 | new_bio->callback = bio->callback; |
773 | 0 | #endif |
774 | 0 | new_bio->callback_ex = bio->callback_ex; |
775 | 0 | new_bio->cb_arg = bio->cb_arg; |
776 | 0 | new_bio->init = bio->init; |
777 | 0 | new_bio->shutdown = bio->shutdown; |
778 | 0 | new_bio->flags = bio->flags; |
779 | | |
780 | | /* This will let SSL_s_sock() work with stdin/stdout */ |
781 | 0 | new_bio->num = bio->num; |
782 | |
|
783 | 0 | if (BIO_dup_state(bio, (char *)new_bio) <= 0) { |
784 | 0 | BIO_free(new_bio); |
785 | 0 | goto err; |
786 | 0 | } |
787 | | |
788 | | /* copy app data */ |
789 | 0 | if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data, |
790 | 0 | &bio->ex_data)) { |
791 | 0 | BIO_free(new_bio); |
792 | 0 | goto err; |
793 | 0 | } |
794 | | |
795 | 0 | if (ret == NULL) { |
796 | 0 | eoc = new_bio; |
797 | 0 | ret = eoc; |
798 | 0 | } else { |
799 | 0 | BIO_push(eoc, new_bio); |
800 | 0 | eoc = new_bio; |
801 | 0 | } |
802 | 0 | } |
803 | 0 | return ret; |
804 | 0 | err: |
805 | 0 | BIO_free_all(ret); |
806 | |
|
807 | 0 | return NULL; |
808 | 0 | } |
809 | | |
810 | | void BIO_copy_next_retry(BIO *b) |
811 | 8.94M | { |
812 | 8.94M | BIO_set_flags(b, BIO_get_retry_flags(b->next_bio)); |
813 | 8.94M | b->retry_reason = b->next_bio->retry_reason; |
814 | 8.94M | } |
815 | | |
816 | | int BIO_set_ex_data(BIO *bio, int idx, void *data) |
817 | 0 | { |
818 | 0 | return CRYPTO_set_ex_data(&(bio->ex_data), idx, data); |
819 | 0 | } |
820 | | |
821 | | void *BIO_get_ex_data(const BIO *bio, int idx) |
822 | 0 | { |
823 | 0 | return CRYPTO_get_ex_data(&(bio->ex_data), idx); |
824 | 0 | } |
825 | | |
826 | | uint64_t BIO_number_read(BIO *bio) |
827 | 0 | { |
828 | 0 | if (bio) |
829 | 0 | return bio->num_read; |
830 | 0 | return 0; |
831 | 0 | } |
832 | | |
833 | | uint64_t BIO_number_written(BIO *bio) |
834 | 0 | { |
835 | 0 | if (bio) |
836 | 0 | return bio->num_write; |
837 | 0 | return 0; |
838 | 0 | } |
839 | | |
840 | | void bio_free_ex_data(BIO *bio) |
841 | 0 | { |
842 | 0 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data); |
843 | 0 | } |
844 | | |
845 | | void bio_cleanup(void) |
846 | 220 | { |
847 | 220 | #ifndef OPENSSL_NO_SOCK |
848 | 220 | bio_sock_cleanup_int(); |
849 | 220 | CRYPTO_THREAD_lock_free(bio_lookup_lock); |
850 | 220 | bio_lookup_lock = NULL; |
851 | 220 | #endif |
852 | 220 | CRYPTO_THREAD_lock_free(bio_type_lock); |
853 | 220 | bio_type_lock = NULL; |
854 | 220 | } |
855 | | |
856 | | /* Internal variant of the below BIO_wait() not calling BIOerr() */ |
857 | | static int bio_wait(BIO *bio, time_t max_time, unsigned int nap_milliseconds) |
858 | 0 | { |
859 | 0 | #ifndef OPENSSL_NO_SOCK |
860 | 0 | int fd; |
861 | 0 | #endif |
862 | 0 | long sec_diff; |
863 | |
|
864 | 0 | if (max_time == 0) /* no timeout */ |
865 | 0 | return 1; |
866 | | |
867 | 0 | #ifndef OPENSSL_NO_SOCK |
868 | 0 | if (BIO_get_fd(bio, &fd) > 0) { |
869 | 0 | int ret = BIO_socket_wait(fd, BIO_should_read(bio), max_time); |
870 | |
|
871 | 0 | if (ret != -1) |
872 | 0 | return ret; |
873 | 0 | } |
874 | 0 | #endif |
875 | | /* fall back to polling since no sockets are available */ |
876 | | |
877 | 0 | sec_diff = (long)(max_time - time(NULL)); /* might overflow */ |
878 | 0 | if (sec_diff < 0) |
879 | 0 | return 0; /* clearly timeout */ |
880 | | |
881 | | /* now take a nap at most the given number of milliseconds */ |
882 | 0 | if (sec_diff == 0) { /* we are below the 1 seconds resolution of max_time */ |
883 | 0 | if (nap_milliseconds > 1000) |
884 | 0 | nap_milliseconds = 1000; |
885 | 0 | } else { /* for sec_diff > 0, take min(sec_diff * 1000, nap_milliseconds) */ |
886 | 0 | if ((unsigned long)sec_diff * 1000 < nap_milliseconds) |
887 | 0 | nap_milliseconds = (unsigned int)sec_diff * 1000; |
888 | 0 | } |
889 | 0 | ossl_sleep(nap_milliseconds); |
890 | 0 | return 1; |
891 | 0 | } |
892 | | |
893 | | /*- |
894 | | * Wait on (typically socket-based) BIO at most until max_time. |
895 | | * Succeed immediately if max_time == 0. |
896 | | * If sockets are not available support polling: succeed after waiting at most |
897 | | * the number of nap_milliseconds in order to avoid a tight busy loop. |
898 | | * Call BIOerr(...) on timeout or error. |
899 | | * Returns -1 on error, 0 on timeout, and 1 on success. |
900 | | */ |
901 | | int BIO_wait(BIO *bio, time_t max_time, unsigned int nap_milliseconds) |
902 | 0 | { |
903 | 0 | int rv = bio_wait(bio, max_time, nap_milliseconds); |
904 | |
|
905 | 0 | if (rv <= 0) |
906 | 0 | ERR_raise(ERR_LIB_BIO, |
907 | 0 | rv == 0 ? BIO_R_TRANSFER_TIMEOUT : BIO_R_TRANSFER_ERROR); |
908 | 0 | return rv; |
909 | 0 | } |
910 | | |
911 | | /* |
912 | | * Connect via given BIO using BIO_do_connect() until success/timeout/error. |
913 | | * Parameter timeout == 0 means no timeout, < 0 means exactly one try. |
914 | | * For non-blocking and potentially even non-socket BIOs perform polling with |
915 | | * the given density: between polls sleep nap_milliseconds using BIO_wait() |
916 | | * in order to avoid a tight busy loop. |
917 | | * Returns -1 on error, 0 on timeout, and 1 on success. |
918 | | */ |
919 | | int BIO_do_connect_retry(BIO *bio, int timeout, int nap_milliseconds) |
920 | 0 | { |
921 | 0 | int blocking = timeout <= 0; |
922 | 0 | time_t max_time = timeout > 0 ? time(NULL) + timeout : 0; |
923 | 0 | int rv; |
924 | |
|
925 | 0 | if (bio == NULL) { |
926 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER); |
927 | 0 | return -1; |
928 | 0 | } |
929 | | |
930 | 0 | if (nap_milliseconds < 0) |
931 | 0 | nap_milliseconds = 100; |
932 | 0 | BIO_set_nbio(bio, !blocking); |
933 | |
|
934 | 0 | retry: |
935 | 0 | ERR_set_mark(); |
936 | 0 | rv = BIO_do_connect(bio); |
937 | |
|
938 | 0 | if (rv <= 0) { /* could be timeout or retryable error or fatal error */ |
939 | 0 | int err = ERR_peek_last_error(); |
940 | 0 | int reason = ERR_GET_REASON(err); |
941 | 0 | int do_retry = BIO_should_retry(bio); /* may be 1 only if !blocking */ |
942 | |
|
943 | 0 | if (ERR_GET_LIB(err) == ERR_LIB_BIO) { |
944 | 0 | switch (reason) { |
945 | 0 | case ERR_R_SYS_LIB: |
946 | | /* |
947 | | * likely retryable system error occurred, which may be |
948 | | * EAGAIN (resource temporarily unavailable) some 40 secs after |
949 | | * calling getaddrinfo(): Temporary failure in name resolution |
950 | | * or a premature ETIMEDOUT, some 30 seconds after connect() |
951 | | */ |
952 | 0 | case BIO_R_CONNECT_ERROR: |
953 | 0 | case BIO_R_NBIO_CONNECT_ERROR: |
954 | | /* some likely retryable connection error occurred */ |
955 | 0 | (void)BIO_reset(bio); /* often needed to avoid retry failure */ |
956 | 0 | do_retry = 1; |
957 | 0 | break; |
958 | 0 | default: |
959 | 0 | break; |
960 | 0 | } |
961 | 0 | } |
962 | 0 | if (timeout >= 0 && do_retry) { |
963 | 0 | ERR_pop_to_mark(); |
964 | | /* will not actually wait if timeout == 0 (i.e., blocking BIO): */ |
965 | 0 | rv = bio_wait(bio, max_time, nap_milliseconds); |
966 | 0 | if (rv > 0) |
967 | 0 | goto retry; |
968 | 0 | ERR_raise(ERR_LIB_BIO, |
969 | 0 | rv == 0 ? BIO_R_CONNECT_TIMEOUT : BIO_R_CONNECT_ERROR); |
970 | 0 | } else { |
971 | 0 | ERR_clear_last_mark(); |
972 | 0 | rv = -1; |
973 | 0 | if (err == 0) /* missing error queue entry */ |
974 | | /* workaround: general error */ |
975 | 0 | ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR); |
976 | 0 | } |
977 | 0 | } else { |
978 | 0 | ERR_clear_last_mark(); |
979 | 0 | } |
980 | | |
981 | 0 | return rv; |
982 | 0 | } |