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