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