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