xref: /openssl/crypto/bio/bio_lib.c (revision e0c4e43e)
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 "bio_local.h"
16 
17 /*
18  * Helper macro for the callback to determine whether an operator expects a
19  * len parameter or not
20  */
21 #define HAS_LEN_OPER(o) ((o) == BIO_CB_READ || (o) == BIO_CB_WRITE \
22                          || (o) == BIO_CB_GETS)
23 
24 #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
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  */
bio_call_callback(BIO * b,int oper,const char * argp,size_t len,int argi,long argl,long inret,size_t * processed)36 static long bio_call_callback(BIO *b, int oper, const char *argp, size_t len,
37                               int argi, long argl, long inret,
38                               size_t *processed)
39 {
40     long ret = inret;
41 #ifndef OPENSSL_NO_DEPRECATED_3_0
42     int bareoper;
43 
44     if (b->callback_ex != NULL)
45 #endif
46         return b->callback_ex(b, oper, argp, len, argi, argl, inret, processed);
47 
48 #ifndef OPENSSL_NO_DEPRECATED_3_0
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;
62     }
63 
64     if (inret > 0 && (oper & BIO_CB_RETURN) && bareoper != BIO_CB_CTRL) {
65         if (*processed > INT_MAX)
66             return -1;
67         inret = *processed;
68     }
69 
70     ret = b->callback(b, oper, argp, argi, argl, inret);
71 
72     if (ret > 0 && (oper & BIO_CB_RETURN) && bareoper != BIO_CB_CTRL) {
73         *processed = (size_t)ret;
74         ret = 1;
75     }
76 #endif
77     return ret;
78 }
79 
BIO_new_ex(OSSL_LIB_CTX * libctx,const BIO_METHOD * method)80 BIO *BIO_new_ex(OSSL_LIB_CTX *libctx, const BIO_METHOD *method)
81 {
82     BIO *bio = OPENSSL_zalloc(sizeof(*bio));
83 
84     if (bio == NULL) {
85         ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
86         return NULL;
87     }
88 
89     bio->libctx = libctx;
90     bio->method = method;
91     bio->shutdown = 1;
92     bio->references = 1;
93 
94     if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data))
95         goto err;
96 
97     bio->lock = CRYPTO_THREAD_lock_new();
98     if (bio->lock == NULL) {
99         ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
100         CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
101         goto err;
102     }
103 
104     if (method->create != NULL && !method->create(bio)) {
105         ERR_raise(ERR_LIB_BIO, ERR_R_INIT_FAIL);
106         CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
107         CRYPTO_THREAD_lock_free(bio->lock);
108         goto err;
109     }
110     if (method->create == NULL)
111         bio->init = 1;
112 
113     return bio;
114 
115 err:
116     OPENSSL_free(bio);
117     return NULL;
118 }
119 
BIO_new(const BIO_METHOD * method)120 BIO *BIO_new(const BIO_METHOD *method)
121 {
122     return BIO_new_ex(NULL, method);
123 }
124 
BIO_free(BIO * a)125 int BIO_free(BIO *a)
126 {
127     int ret;
128 
129     if (a == NULL)
130         return 0;
131 
132     if (CRYPTO_DOWN_REF(&a->references, &ret, a->lock) <= 0)
133         return 0;
134 
135     REF_PRINT_COUNT("BIO", a);
136     if (ret > 0)
137         return 1;
138     REF_ASSERT_ISNT(ret < 0);
139 
140     if (HAS_CALLBACK(a)) {
141         ret = (int)bio_call_callback(a, BIO_CB_FREE, NULL, 0, 0, 0L, 1L, NULL);
142         if (ret <= 0)
143             return 0;
144     }
145 
146     if ((a->method != NULL) && (a->method->destroy != NULL))
147         a->method->destroy(a);
148 
149     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
150 
151     CRYPTO_THREAD_lock_free(a->lock);
152 
153     OPENSSL_free(a);
154 
155     return 1;
156 }
157 
BIO_set_data(BIO * a,void * ptr)158 void BIO_set_data(BIO *a, void *ptr)
159 {
160     a->ptr = ptr;
161 }
162 
BIO_get_data(BIO * a)163 void *BIO_get_data(BIO *a)
164 {
165     return a->ptr;
166 }
167 
BIO_set_init(BIO * a,int init)168 void BIO_set_init(BIO *a, int init)
169 {
170     a->init = init;
171 }
172 
BIO_get_init(BIO * a)173 int BIO_get_init(BIO *a)
174 {
175     return a->init;
176 }
177 
BIO_set_shutdown(BIO * a,int shut)178 void BIO_set_shutdown(BIO *a, int shut)
179 {
180     a->shutdown = shut;
181 }
182 
BIO_get_shutdown(BIO * a)183 int BIO_get_shutdown(BIO *a)
184 {
185     return a->shutdown;
186 }
187 
BIO_vfree(BIO * a)188 void BIO_vfree(BIO *a)
189 {
190     BIO_free(a);
191 }
192 
BIO_up_ref(BIO * a)193 int BIO_up_ref(BIO *a)
194 {
195     int i;
196 
197     if (CRYPTO_UP_REF(&a->references, &i, a->lock) <= 0)
198         return 0;
199 
200     REF_PRINT_COUNT("BIO", a);
201     REF_ASSERT_ISNT(i < 2);
202     return i > 1;
203 }
204 
BIO_clear_flags(BIO * b,int flags)205 void BIO_clear_flags(BIO *b, int flags)
206 {
207     b->flags &= ~flags;
208 }
209 
BIO_test_flags(const BIO * b,int flags)210 int BIO_test_flags(const BIO *b, int flags)
211 {
212     return (b->flags & flags);
213 }
214 
BIO_set_flags(BIO * b,int flags)215 void BIO_set_flags(BIO *b, int flags)
216 {
217     b->flags |= flags;
218 }
219 
220 #ifndef OPENSSL_NO_DEPRECATED_3_0
BIO_get_callback(const BIO * b)221 BIO_callback_fn BIO_get_callback(const BIO *b)
222 {
223     return b->callback;
224 }
225 
BIO_set_callback(BIO * b,BIO_callback_fn cb)226 void BIO_set_callback(BIO *b, BIO_callback_fn cb)
227 {
228     b->callback = cb;
229 }
230 #endif
231 
BIO_get_callback_ex(const BIO * b)232 BIO_callback_fn_ex BIO_get_callback_ex(const BIO *b)
233 {
234     return b->callback_ex;
235 }
236 
BIO_set_callback_ex(BIO * b,BIO_callback_fn_ex cb)237 void BIO_set_callback_ex(BIO *b, BIO_callback_fn_ex cb)
238 {
239     b->callback_ex = cb;
240 }
241 
BIO_set_callback_arg(BIO * b,char * arg)242 void BIO_set_callback_arg(BIO *b, char *arg)
243 {
244     b->cb_arg = arg;
245 }
246 
BIO_get_callback_arg(const BIO * b)247 char *BIO_get_callback_arg(const BIO *b)
248 {
249     return b->cb_arg;
250 }
251 
BIO_method_name(const BIO * b)252 const char *BIO_method_name(const BIO *b)
253 {
254     return b->method->name;
255 }
256 
BIO_method_type(const BIO * b)257 int BIO_method_type(const BIO *b)
258 {
259     return b->method->type;
260 }
261 
262 /*
263  * This is essentially the same as BIO_read_ex() except that it allows
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.
267  */
bio_read_intern(BIO * b,void * data,size_t dlen,size_t * readbytes)268 static int bio_read_intern(BIO *b, void *data, size_t dlen, size_t *readbytes)
269 {
270     int ret;
271 
272     if (b == NULL) {
273         ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
274         return -1;
275     }
276     if (b->method == NULL || b->method->bread == NULL) {
277         ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
278         return -2;
279     }
280 
281     if (HAS_CALLBACK(b) &&
282         ((ret = (int)bio_call_callback(b, BIO_CB_READ, data, dlen, 0, 0L, 1L,
283                                        NULL)) <= 0))
284         return ret;
285 
286     if (!b->init) {
287         ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
288         return -1;
289     }
290 
291     ret = b->method->bread(b, data, dlen, readbytes);
292 
293     if (ret > 0)
294         b->num_read += (uint64_t)*readbytes;
295 
296     if (HAS_CALLBACK(b))
297         ret = (int)bio_call_callback(b, BIO_CB_READ | BIO_CB_RETURN, data,
298                                      dlen, 0, 0L, ret, readbytes);
299 
300     /* Shouldn't happen */
301     if (ret > 0 && *readbytes > dlen) {
302         ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR);
303         return -1;
304     }
305 
306     return ret;
307 }
308 
BIO_read(BIO * b,void * data,int dlen)309 int BIO_read(BIO *b, void *data, int dlen)
310 {
311     size_t readbytes;
312     int ret;
313 
314     if (dlen < 0)
315         return 0;
316 
317     ret = bio_read_intern(b, data, (size_t)dlen, &readbytes);
318 
319     if (ret > 0) {
320         /* *readbytes should always be <= dlen */
321         ret = (int)readbytes;
322     }
323 
324     return ret;
325 }
326 
BIO_read_ex(BIO * b,void * data,size_t dlen,size_t * readbytes)327 int BIO_read_ex(BIO *b, void *data, size_t dlen, size_t *readbytes)
328 {
329     return bio_read_intern(b, data, dlen, readbytes) > 0;
330 }
331 
bio_write_intern(BIO * b,const void * data,size_t dlen,size_t * written)332 static int bio_write_intern(BIO *b, const void *data, size_t dlen,
333                             size_t *written)
334 {
335     size_t local_written;
336     int ret;
337 
338     if (written != NULL)
339         *written = 0;
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 
347     if (b->method == NULL || b->method->bwrite == NULL) {
348         ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
349         return -2;
350     }
351 
352     if (HAS_CALLBACK(b) &&
353         ((ret = (int)bio_call_callback(b, BIO_CB_WRITE, data, dlen, 0, 0L, 1L,
354                                        NULL)) <= 0))
355         return ret;
356 
357     if (!b->init) {
358         ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
359         return -1;
360     }
361 
362     ret = b->method->bwrite(b, data, dlen, &local_written);
363 
364     if (ret > 0)
365         b->num_write += (uint64_t)local_written;
366 
367     if (HAS_CALLBACK(b))
368         ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN, data,
369                                      dlen, 0, 0L, ret, &local_written);
370 
371     if (written != NULL)
372         *written = local_written;
373     return ret;
374 }
375 
BIO_write(BIO * b,const void * data,int dlen)376 int BIO_write(BIO *b, const void *data, int dlen)
377 {
378     size_t written;
379     int ret;
380 
381     if (dlen <= 0)
382         return 0;
383 
384     ret = bio_write_intern(b, data, (size_t)dlen, &written);
385 
386     if (ret > 0) {
387         /* written should always be <= dlen */
388         ret = (int)written;
389     }
390 
391     return ret;
392 }
393 
BIO_write_ex(BIO * b,const void * data,size_t dlen,size_t * written)394 int BIO_write_ex(BIO *b, const void *data, size_t dlen, size_t *written)
395 {
396     return bio_write_intern(b, data, dlen, written) > 0
397         || (b != NULL && dlen == 0); /* order is important for *written */
398 }
399 
BIO_sendmmsg(BIO * b,BIO_MSG * msg,size_t stride,size_t num_msg,uint64_t flags,size_t * msgs_processed)400 int BIO_sendmmsg(BIO *b, BIO_MSG *msg,
401                  size_t stride, size_t num_msg, uint64_t flags,
402                  size_t *msgs_processed)
403 {
404     size_t ret;
405     BIO_MMSG_CB_ARGS args;
406 
407     if (b == NULL) {
408         *msgs_processed = 0;
409         ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
410         return 0;
411     }
412 
413     if (b->method == NULL || b->method->bsendmmsg == NULL) {
414         *msgs_processed = 0;
415         ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
416         return 0;
417     }
418 
419     if (HAS_CALLBACK(b)) {
420         args.msg            = msg;
421         args.stride         = stride;
422         args.num_msg        = num_msg;
423         args.flags          = flags;
424         args.msgs_processed = msgs_processed;
425 
426         ret = (size_t)bio_call_callback(b, BIO_CB_SENDMMSG, (void *)&args,
427                                         0, 0, 0, 0, NULL);
428         if (ret == 0)
429             return 0;
430     }
431 
432     if (!b->init) {
433         *msgs_processed = 0;
434         ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
435         return 0;
436     }
437 
438     ret = b->method->bsendmmsg(b, msg, stride, num_msg, flags, msgs_processed);
439 
440     if (HAS_CALLBACK(b))
441         ret = (size_t)bio_call_callback(b, BIO_CB_SENDMMSG | BIO_CB_RETURN,
442                                         (void *)&args, ret, 0, 0, 0, NULL);
443 
444     return ret;
445 }
446 
BIO_recvmmsg(BIO * b,BIO_MSG * msg,size_t stride,size_t num_msg,uint64_t flags,size_t * msgs_processed)447 int BIO_recvmmsg(BIO *b, BIO_MSG *msg,
448                  size_t stride, size_t num_msg, uint64_t flags,
449                  size_t *msgs_processed)
450 {
451     size_t ret;
452     BIO_MMSG_CB_ARGS args;
453 
454     if (b == NULL) {
455         *msgs_processed = 0;
456         ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
457         return 0;
458     }
459 
460     if (b->method == NULL || b->method->brecvmmsg == NULL) {
461         *msgs_processed = 0;
462         ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
463         return 0;
464     }
465 
466     if (HAS_CALLBACK(b)) {
467         args.msg            = msg;
468         args.stride         = stride;
469         args.num_msg        = num_msg;
470         args.flags          = flags;
471         args.msgs_processed = msgs_processed;
472 
473         ret = bio_call_callback(b, BIO_CB_RECVMMSG, (void *)&args,
474                                 0, 0, 0, 0, NULL);
475         if (ret == 0)
476             return 0;
477     }
478 
479     if (!b->init) {
480         *msgs_processed = 0;
481         ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
482         return 0;
483     }
484 
485     ret = b->method->brecvmmsg(b, msg, stride, num_msg, flags, msgs_processed);
486 
487     if (HAS_CALLBACK(b))
488         ret = (size_t)bio_call_callback(b, BIO_CB_RECVMMSG | BIO_CB_RETURN,
489                                         (void *)&args, ret, 0, 0, 0, NULL);
490 
491     return ret;
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     return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
718 }
719 
BIO_ctrl_wpending(BIO * bio)720 size_t BIO_ctrl_wpending(BIO *bio)
721 {
722     return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
723 }
724 
725 /* put the 'bio' on the end of b's list of operators */
BIO_push(BIO * b,BIO * bio)726 BIO *BIO_push(BIO *b, BIO *bio)
727 {
728     BIO *lb;
729 
730     if (b == NULL)
731         return bio;
732     lb = b;
733     while (lb->next_bio != NULL)
734         lb = lb->next_bio;
735     lb->next_bio = bio;
736     if (bio != NULL)
737         bio->prev_bio = lb;
738     /* called to do internal processing */
739     BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb);
740     return b;
741 }
742 
743 /* Remove the first and return the rest */
BIO_pop(BIO * b)744 BIO *BIO_pop(BIO *b)
745 {
746     BIO *ret;
747 
748     if (b == NULL)
749         return NULL;
750     ret = b->next_bio;
751 
752     BIO_ctrl(b, BIO_CTRL_POP, 0, b);
753 
754     if (b->prev_bio != NULL)
755         b->prev_bio->next_bio = b->next_bio;
756     if (b->next_bio != NULL)
757         b->next_bio->prev_bio = b->prev_bio;
758 
759     b->next_bio = NULL;
760     b->prev_bio = NULL;
761     return ret;
762 }
763 
BIO_get_retry_BIO(BIO * bio,int * reason)764 BIO *BIO_get_retry_BIO(BIO *bio, int *reason)
765 {
766     BIO *b, *last;
767 
768     b = last = bio;
769     for (;;) {
770         if (!BIO_should_retry(b))
771             break;
772         last = b;
773         b = b->next_bio;
774         if (b == NULL)
775             break;
776     }
777     if (reason != NULL)
778         *reason = last->retry_reason;
779     return last;
780 }
781 
BIO_get_retry_reason(BIO * bio)782 int BIO_get_retry_reason(BIO *bio)
783 {
784     return bio->retry_reason;
785 }
786 
BIO_set_retry_reason(BIO * bio,int reason)787 void BIO_set_retry_reason(BIO *bio, int reason)
788 {
789     bio->retry_reason = reason;
790 }
791 
BIO_find_type(BIO * bio,int type)792 BIO *BIO_find_type(BIO *bio, int type)
793 {
794     int mt, mask;
795 
796     if (bio == NULL) {
797         ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
798         return NULL;
799     }
800     mask = type & 0xff;
801     do {
802         if (bio->method != NULL) {
803             mt = bio->method->type;
804 
805             if (!mask) {
806                 if (mt & type)
807                     return bio;
808             } else if (mt == type) {
809                 return bio;
810             }
811         }
812         bio = bio->next_bio;
813     } while (bio != NULL);
814     return NULL;
815 }
816 
BIO_next(BIO * b)817 BIO *BIO_next(BIO *b)
818 {
819     if (b == NULL)
820         return NULL;
821     return b->next_bio;
822 }
823 
BIO_set_next(BIO * b,BIO * next)824 void BIO_set_next(BIO *b, BIO *next)
825 {
826     b->next_bio = next;
827 }
828 
BIO_free_all(BIO * bio)829 void BIO_free_all(BIO *bio)
830 {
831     BIO *b;
832     int ref;
833 
834     while (bio != NULL) {
835         b = bio;
836         ref = b->references;
837         bio = bio->next_bio;
838         BIO_free(b);
839         /* Since ref count > 1, don't free anyone else. */
840         if (ref > 1)
841             break;
842     }
843 }
844 
BIO_dup_chain(BIO * in)845 BIO *BIO_dup_chain(BIO *in)
846 {
847     BIO *ret = NULL, *eoc = NULL, *bio, *new_bio;
848 
849     for (bio = in; bio != NULL; bio = bio->next_bio) {
850         if ((new_bio = BIO_new(bio->method)) == NULL)
851             goto err;
852 #ifndef OPENSSL_NO_DEPRECATED_3_0
853         new_bio->callback = bio->callback;
854 #endif
855         new_bio->callback_ex = bio->callback_ex;
856         new_bio->cb_arg = bio->cb_arg;
857         new_bio->init = bio->init;
858         new_bio->shutdown = bio->shutdown;
859         new_bio->flags = bio->flags;
860 
861         /* This will let SSL_s_sock() work with stdin/stdout */
862         new_bio->num = bio->num;
863 
864         if (!BIO_dup_state(bio, (char *)new_bio)) {
865             BIO_free(new_bio);
866             goto err;
867         }
868 
869         /* copy app data */
870         if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data,
871                                 &bio->ex_data)) {
872             BIO_free(new_bio);
873             goto err;
874         }
875 
876         if (ret == NULL) {
877             eoc = new_bio;
878             ret = eoc;
879         } else {
880             BIO_push(eoc, new_bio);
881             eoc = new_bio;
882         }
883     }
884     return ret;
885  err:
886     BIO_free_all(ret);
887 
888     return NULL;
889 }
890 
BIO_copy_next_retry(BIO * b)891 void BIO_copy_next_retry(BIO *b)
892 {
893     BIO_set_flags(b, BIO_get_retry_flags(b->next_bio));
894     b->retry_reason = b->next_bio->retry_reason;
895 }
896 
BIO_set_ex_data(BIO * bio,int idx,void * data)897 int BIO_set_ex_data(BIO *bio, int idx, void *data)
898 {
899     return CRYPTO_set_ex_data(&(bio->ex_data), idx, data);
900 }
901 
BIO_get_ex_data(const BIO * bio,int idx)902 void *BIO_get_ex_data(const BIO *bio, int idx)
903 {
904     return CRYPTO_get_ex_data(&(bio->ex_data), idx);
905 }
906 
BIO_number_read(BIO * bio)907 uint64_t BIO_number_read(BIO *bio)
908 {
909     if (bio)
910         return bio->num_read;
911     return 0;
912 }
913 
BIO_number_written(BIO * bio)914 uint64_t BIO_number_written(BIO *bio)
915 {
916     if (bio)
917         return bio->num_write;
918     return 0;
919 }
920 
bio_free_ex_data(BIO * bio)921 void bio_free_ex_data(BIO *bio)
922 {
923     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
924 }
925 
bio_cleanup(void)926 void bio_cleanup(void)
927 {
928 #ifndef OPENSSL_NO_SOCK
929     bio_sock_cleanup_int();
930     CRYPTO_THREAD_lock_free(bio_lookup_lock);
931     bio_lookup_lock = NULL;
932 #endif
933     CRYPTO_THREAD_lock_free(bio_type_lock);
934     bio_type_lock = NULL;
935 }
936 
937 /* Internal variant of the below BIO_wait() not calling BIOerr() */
bio_wait(BIO * bio,time_t max_time,unsigned int nap_milliseconds)938 static int bio_wait(BIO *bio, time_t max_time, unsigned int nap_milliseconds)
939 {
940 #ifndef OPENSSL_NO_SOCK
941     int fd;
942 #endif
943     long sec_diff;
944 
945     if (max_time == 0) /* no timeout */
946         return 1;
947 
948 #ifndef OPENSSL_NO_SOCK
949     if (BIO_get_fd(bio, &fd) > 0 && fd < FD_SETSIZE)
950         return BIO_socket_wait(fd, BIO_should_read(bio), max_time);
951 #endif
952     /* fall back to polling since no sockets are available */
953 
954     sec_diff = (long)(max_time - time(NULL)); /* might overflow */
955     if (sec_diff < 0)
956         return 0; /* clearly timeout */
957 
958     /* now take a nap at most the given number of milliseconds */
959     if (sec_diff == 0) { /* we are below the 1 seconds resolution of max_time */
960         if (nap_milliseconds > 1000)
961             nap_milliseconds = 1000;
962     } else { /* for sec_diff > 0, take min(sec_diff * 1000, nap_milliseconds) */
963         if ((unsigned long)sec_diff * 1000 < nap_milliseconds)
964             nap_milliseconds = (unsigned int)sec_diff * 1000;
965     }
966     ossl_sleep(nap_milliseconds);
967     return 1;
968 }
969 
970 /*-
971  * Wait on (typically socket-based) BIO at most until max_time.
972  * Succeed immediately if max_time == 0.
973  * If sockets are not available support polling: succeed after waiting at most
974  * the number of nap_milliseconds in order to avoid a tight busy loop.
975  * Call BIOerr(...) on timeout or error.
976  * Returns -1 on error, 0 on timeout, and 1 on success.
977  */
BIO_wait(BIO * bio,time_t max_time,unsigned int nap_milliseconds)978 int BIO_wait(BIO *bio, time_t max_time, unsigned int nap_milliseconds)
979 {
980     int rv = bio_wait(bio, max_time, nap_milliseconds);
981 
982     if (rv <= 0)
983         ERR_raise(ERR_LIB_BIO,
984                   rv == 0 ? BIO_R_TRANSFER_TIMEOUT : BIO_R_TRANSFER_ERROR);
985     return rv;
986 }
987 
988 /*
989  * Connect via given BIO using BIO_do_connect() until success/timeout/error.
990  * Parameter timeout == 0 means no timeout, < 0 means exactly one try.
991  * For non-blocking and potentially even non-socket BIOs perform polling with
992  * the given density: between polls sleep nap_milliseconds using BIO_wait()
993  * in order to avoid a tight busy loop.
994  * Returns -1 on error, 0 on timeout, and 1 on success.
995  */
BIO_do_connect_retry(BIO * bio,int timeout,int nap_milliseconds)996 int BIO_do_connect_retry(BIO *bio, int timeout, int nap_milliseconds)
997 {
998     int blocking = timeout <= 0;
999     time_t max_time = timeout > 0 ? time(NULL) + timeout : 0;
1000     int rv;
1001 
1002     if (bio == NULL) {
1003         ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
1004         return -1;
1005     }
1006 
1007     if (nap_milliseconds < 0)
1008         nap_milliseconds = 100;
1009     BIO_set_nbio(bio, !blocking);
1010 
1011  retry:
1012     ERR_set_mark();
1013     rv = BIO_do_connect(bio);
1014 
1015     if (rv <= 0) { /* could be timeout or retryable error or fatal error */
1016         int err = ERR_peek_last_error();
1017         int reason = ERR_GET_REASON(err);
1018         int do_retry = BIO_should_retry(bio); /* may be 1 only if !blocking */
1019 
1020         if (ERR_GET_LIB(err) == ERR_LIB_BIO) {
1021             switch (reason) {
1022             case ERR_R_SYS_LIB:
1023                 /*
1024                  * likely retryable system error occurred, which may be
1025                  * EAGAIN (resource temporarily unavailable) some 40 secs after
1026                  * calling getaddrinfo(): Temporary failure in name resolution
1027                  * or a premature ETIMEDOUT, some 30 seconds after connect()
1028                  */
1029             case BIO_R_CONNECT_ERROR:
1030             case BIO_R_NBIO_CONNECT_ERROR:
1031                 /* some likely retryable connection error occurred */
1032                 (void)BIO_reset(bio); /* often needed to avoid retry failure */
1033                 do_retry = 1;
1034                 break;
1035             default:
1036                 break;
1037             }
1038         }
1039         if (timeout >= 0 && do_retry) {
1040             ERR_pop_to_mark();
1041             /* will not actually wait if timeout == 0 (i.e., blocking BIO): */
1042             rv = bio_wait(bio, max_time, nap_milliseconds);
1043             if (rv > 0)
1044                 goto retry;
1045             ERR_raise(ERR_LIB_BIO,
1046                       rv == 0 ? BIO_R_CONNECT_TIMEOUT : BIO_R_CONNECT_ERROR);
1047         } else {
1048             ERR_clear_last_mark();
1049             rv = -1;
1050             if (err == 0) /* missing error queue entry */
1051                 /* workaround: general error */
1052                 ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR);
1053         }
1054     } else {
1055         ERR_clear_last_mark();
1056     }
1057 
1058     return rv;
1059 }
1060