1BIO_S_BIO(3) OpenSSL BIO_S_BIO(3)
2
3
4
6 BIO_s_bio, BIO_make_bio_pair, BIO_destroy_bio_pair, BIO_shutdown_wr,
7 BIO_set_write_buf_size, BIO_get_write_buf_size, BIO_new_bio_pair,
8 BIO_get_write_guarantee, BIO_ctrl_get_write_guarantee,
9 BIO_get_read_request, BIO_ctrl_get_read_request,
10 BIO_ctrl_reset_read_request - BIO pair BIO
11
13 #include <openssl/bio.h>
14
15 const BIO_METHOD *BIO_s_bio(void);
16
17 int BIO_make_bio_pair(BIO *b1, BIO *b2);
18 int BIO_destroy_bio_pair(BIO *b);
19 int BIO_shutdown_wr(BIO *b);
20
21 int BIO_set_write_buf_size(BIO *b, long size);
22 size_t BIO_get_write_buf_size(BIO *b, long size);
23
24 int BIO_new_bio_pair(BIO **bio1, size_t writebuf1, BIO **bio2, size_t writebuf2);
25
26 int BIO_get_write_guarantee(BIO *b);
27 size_t BIO_ctrl_get_write_guarantee(BIO *b);
28 int BIO_get_read_request(BIO *b);
29 size_t BIO_ctrl_get_read_request(BIO *b);
30 int BIO_ctrl_reset_read_request(BIO *b);
31
33 BIO_s_bio() returns the method for a BIO pair. A BIO pair is a pair of
34 source/sink BIOs where data written to either half of the pair is
35 buffered and can be read from the other half. Both halves must usually
36 by handled by the same application thread since no locking is done on
37 the internal data structures.
38
39 Since BIO chains typically end in a source/sink BIO it is possible to
40 make this one half of a BIO pair and have all the data processed by the
41 chain under application control.
42
43 One typical use of BIO pairs is to place TLS/SSL I/O under application
44 control, this can be used when the application wishes to use a non
45 standard transport for TLS/SSL or the normal socket routines are
46 inappropriate.
47
48 Calls to BIO_read_ex() will read data from the buffer or request a
49 retry if no data is available.
50
51 Calls to BIO_write_ex() will place data in the buffer or request a
52 retry if the buffer is full.
53
54 The standard calls BIO_ctrl_pending() and BIO_ctrl_wpending() can be
55 used to determine the amount of pending data in the read or write
56 buffer.
57
58 BIO_reset() clears any data in the write buffer.
59
60 BIO_make_bio_pair() joins two separate BIOs into a connected pair.
61
62 BIO_destroy_pair() destroys the association between two connected BIOs.
63 Freeing up any half of the pair will automatically destroy the
64 association.
65
66 BIO_shutdown_wr() is used to close down a BIO b. After this call no
67 further writes on BIO b are allowed (they will return an error). Reads
68 on the other half of the pair will return any pending data or EOF when
69 all pending data has been read.
70
71 BIO_set_write_buf_size() sets the write buffer size of BIO b to size.
72 If the size is not initialized a default value is used. This is
73 currently 17K, sufficient for a maximum size TLS record.
74
75 BIO_get_write_buf_size() returns the size of the write buffer.
76
77 BIO_new_bio_pair() combines the calls to BIO_new(), BIO_make_bio_pair()
78 and BIO_set_write_buf_size() to create a connected pair of BIOs bio1,
79 bio2 with write buffer sizes writebuf1 and writebuf2. If either size is
80 zero then the default size is used. BIO_new_bio_pair() does not check
81 whether bio1 or bio2 do point to some other BIO, the values are
82 overwritten, BIO_free() is not called.
83
84 BIO_get_write_guarantee() and BIO_ctrl_get_write_guarantee() return the
85 maximum length of data that can be currently written to the BIO. Writes
86 larger than this value will return a value from BIO_write_ex() less
87 than the amount requested or if the buffer is full request a retry.
88 BIO_ctrl_get_write_guarantee() is a function whereas
89 BIO_get_write_guarantee() is a macro.
90
91 BIO_get_read_request() and BIO_ctrl_get_read_request() return the
92 amount of data requested, or the buffer size if it is less, if the last
93 read attempt at the other half of the BIO pair failed due to an empty
94 buffer. This can be used to determine how much data should be written
95 to the BIO so the next read will succeed: this is most useful in
96 TLS/SSL applications where the amount of data read is usually
97 meaningful rather than just a buffer size. After a successful read this
98 call will return zero. It also will return zero once new data has been
99 written satisfying the read request or part of it. Note that
100 BIO_get_read_request() never returns an amount larger than that
101 returned by BIO_get_write_guarantee().
102
103 BIO_ctrl_reset_read_request() can also be used to reset the value
104 returned by BIO_get_read_request() to zero.
105
107 Both halves of a BIO pair should be freed. That is even if one half is
108 implicit freed due to a BIO_free_all() or SSL_free() call the other
109 half needs to be freed.
110
111 When used in bidirectional applications (such as TLS/SSL) care should
112 be taken to flush any data in the write buffer. This can be done by
113 calling BIO_pending() on the other half of the pair and, if any data is
114 pending, reading it and sending it to the underlying transport. This
115 must be done before any normal processing (such as calling select() )
116 due to a request and BIO_should_read() being true.
117
118 To see why this is important consider a case where a request is sent
119 using BIO_write_ex() and a response read with BIO_read_ex(), this can
120 occur during an TLS/SSL handshake for example. BIO_write_ex() will
121 succeed and place data in the write buffer. BIO_read_ex() will
122 initially fail and BIO_should_read() will be true. If the application
123 then waits for data to be available on the underlying transport before
124 flushing the write buffer it will never succeed because the request was
125 never sent!
126
127 BIO_eof() is true if no data is in the peer BIO and the peer BIO has
128 been shutdown.
129
130 BIO_make_bio_pair(), BIO_destroy_bio_pair(), BIO_shutdown_wr(),
131 BIO_set_write_buf_size(), BIO_get_write_buf_size(),
132 BIO_get_write_guarantee(), and BIO_get_read_request() are implemented
133 as macros.
134
136 BIO_new_bio_pair() returns 1 on success, with the new BIOs available in
137 bio1 and bio2, or 0 on failure, with NULL pointers stored into the
138 locations for bio1 and bio2. Check the error stack for more
139 information.
140
141 [XXXXX: More return values need to be added here]
142
144 The BIO pair can be used to have full control over the network access
145 of an application. The application can call select() on the socket as
146 required without having to go through the SSL-interface.
147
148 BIO *internal_bio, *network_bio;
149
150 ...
151 BIO_new_bio_pair(&internal_bio, 0, &network_bio, 0);
152 SSL_set_bio(ssl, internal_bio, internal_bio);
153 SSL_operations(); /* e.g. SSL_read and SSL_write */
154 ...
155
156 application | TLS-engine
157 | |
158 +----------> SSL_operations()
159 | /\ ||
160 | || \/
161 | BIO-pair (internal_bio)
162 | BIO-pair (network_bio)
163 | || /\
164 | \/ ||
165 +-----------< BIO_operations()
166 | |
167 | |
168 socket
169
170 ...
171 SSL_free(ssl); /* implicitly frees internal_bio */
172 BIO_free(network_bio);
173 ...
174
175 As the BIO pair will only buffer the data and never directly access the
176 connection, it behaves nonblocking and will return as soon as the write
177 buffer is full or the read buffer is drained. Then the application has
178 to flush the write buffer and/or fill the read buffer.
179
180 Use the BIO_ctrl_pending(), to find out whether data is buffered in the
181 BIO and must be transferred to the network. Use
182 BIO_ctrl_get_read_request() to find out, how many bytes must be written
183 into the buffer before the SSL_operation() can successfully be
184 continued.
185
187 As the data is buffered, SSL_operation() may return with an
188 ERROR_SSL_WANT_READ condition, but there is still data in the write
189 buffer. An application must not rely on the error value of
190 SSL_operation() but must assure that the write buffer is always flushed
191 first. Otherwise a deadlock may occur as the peer might be waiting for
192 the data before being able to continue.
193
195 SSL_set_bio(3), ssl(7), bio(7), BIO_should_retry(3), BIO_read_ex(3)
196
198 Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
199
200 Licensed under the OpenSSL license (the "License"). You may not use
201 this file except in compliance with the License. You can obtain a copy
202 in the file LICENSE in the source distribution or at
203 <https://www.openssl.org/source/license.html>.
204
205
206
2071.1.1q 2023-02-06 BIO_S_BIO(3)