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