diff options
Diffstat (limited to 'src/lib/libcrypto/man/BIO_s_bio.3')
-rw-r--r-- | src/lib/libcrypto/man/BIO_s_bio.3 | 295 |
1 files changed, 295 insertions, 0 deletions
diff --git a/src/lib/libcrypto/man/BIO_s_bio.3 b/src/lib/libcrypto/man/BIO_s_bio.3 new file mode 100644 index 0000000000..af7bdabd33 --- /dev/null +++ b/src/lib/libcrypto/man/BIO_s_bio.3 | |||
@@ -0,0 +1,295 @@ | |||
1 | .Dd $Mdocdate: February 16 2015 $ | ||
2 | .Dt BIO_S_BIO 3 | ||
3 | .Os | ||
4 | .Sh NAME | ||
5 | .Nm BIO_s_bio , | ||
6 | .Nm BIO_make_bio_pair , | ||
7 | .Nm BIO_destroy_bio_pair , | ||
8 | .Nm BIO_shutdown_wr , | ||
9 | .Nm BIO_set_write_buf_size , | ||
10 | .Nm BIO_get_write_buf_size , | ||
11 | .Nm BIO_new_bio_pair , | ||
12 | .Nm BIO_get_write_guarantee , | ||
13 | .Nm BIO_ctrl_get_write_guarantee , | ||
14 | .Nm BIO_get_read_request , | ||
15 | .Nm BIO_ctrl_get_read_request , | ||
16 | .Nm BIO_ctrl_reset_read_request | ||
17 | .Nd BIO pair BIO | ||
18 | .Sh SYNOPSIS | ||
19 | .In openssl/bio.h | ||
20 | .Ft BIO_METHOD * | ||
21 | .Fo BIO_s_bio | ||
22 | .Fa void | ||
23 | .Fc | ||
24 | .Bd -unfilled | ||
25 | #define BIO_make_bio_pair(b1, b2) \e | ||
26 | (int)BIO_ctrl(b1, BIO_C_MAKE_BIO_PAIR, 0, b2) | ||
27 | #define BIO_destroy_bio_pair(b) \e | ||
28 | (int)BIO_ctrl(b, BIO_C_DESTROY_BIO_PAIR, 0, NULL) | ||
29 | #define BIO_shutdown_wr(b) \e | ||
30 | (int)BIO_ctrl(b, BIO_C_SHUTDOWN_WR, 0, NULL) | ||
31 | #define BIO_set_write_buf_size(b, size) \e | ||
32 | (int)BIO_ctrl(b, BIO_C_SET_WRITE_BUF_SIZE, size, NULL) | ||
33 | #define BIO_get_write_buf_size(b, size) \e | ||
34 | (size_t)BIO_ctrl(b, BIO_C_GET_WRITE_BUF_SIZE, size, NULL) | ||
35 | .Ed | ||
36 | .Pp | ||
37 | .Ft int | ||
38 | .Fo BIO_new_bio_pair | ||
39 | .Fa "BIO **bio1" | ||
40 | .Fa "size_t writebuf1" | ||
41 | .Fa "BIO **bio2" | ||
42 | .Fa "size_t writebuf2" | ||
43 | .Fc | ||
44 | .Bd -unfilled | ||
45 | #define BIO_get_write_guarantee(b) \e | ||
46 | (int)BIO_ctrl(b, BIO_C_GET_WRITE_GUARANTEE, 0, NULL) | ||
47 | .Ed | ||
48 | .Pp | ||
49 | .Ft size_t | ||
50 | .Fo BIO_ctrl_get_write_guarantee | ||
51 | .Fa "BIO *b" | ||
52 | .Fc | ||
53 | .Bd -unfilled | ||
54 | #define BIO_get_read_request(b) \e | ||
55 | (int)BIO_ctrl(b, BIO_C_GET_READ_REQUEST, 0, NULL) | ||
56 | .Ed | ||
57 | .Pp | ||
58 | .Ft size_t | ||
59 | .Fo BIO_ctrl_get_read_request | ||
60 | .Fa "BIO *b" | ||
61 | .Fc | ||
62 | .Ft int | ||
63 | .Fo BIO_ctrl_reset_read_request | ||
64 | .Fa "BIO *b" | ||
65 | .Fc | ||
66 | .Sh DESCRIPTION | ||
67 | .Fn BIO_s_bio | ||
68 | returns the method for a BIO pair. | ||
69 | A BIO pair is a pair of source/sink BIOs where data written to either | ||
70 | half of the pair is buffered and can be read from the other half. | ||
71 | Both halves must usually be handled by the same application thread | ||
72 | since no locking is done on the internal data structures. | ||
73 | .Pp | ||
74 | Since BIO chains typically end in a source/sink BIO, | ||
75 | it is possible to make this one half of a BIO pair and | ||
76 | have all the data processed by the chain under application control. | ||
77 | .Pp | ||
78 | One typical use of BIO pairs is | ||
79 | to place TLS/SSL I/O under application control. | ||
80 | This can be used when the application wishes to use a non standard | ||
81 | transport for TLS/SSL or the normal socket routines are inappropriate. | ||
82 | .Pp | ||
83 | Calls to | ||
84 | .Xr BIO_read 3 | ||
85 | will read data from the buffer or request a retry if no data is available. | ||
86 | .Pp | ||
87 | Calls to | ||
88 | .Xr BIO_write 3 | ||
89 | will place data in the buffer or request a retry if the buffer is full. | ||
90 | .Pp | ||
91 | The standard calls | ||
92 | .Xr BIO_ctrl_pending 3 | ||
93 | and | ||
94 | .Xr BIO_ctrl_wpending 3 | ||
95 | can be used to determine the amount of pending data | ||
96 | in the read or write buffer. | ||
97 | .Pp | ||
98 | .Xr BIO_reset 3 | ||
99 | clears any data in the write buffer. | ||
100 | .Pp | ||
101 | .Fn BIO_make_bio_pair | ||
102 | joins two separate BIOs into a connected pair. | ||
103 | .Pp | ||
104 | .Fn BIO_destroy_pair | ||
105 | destroys the association between two connected BIOs. | ||
106 | Freeing up any half of the pair will automatically destroy the association. | ||
107 | .Pp | ||
108 | .Fn BIO_shutdown_wr | ||
109 | is used to close down a BIO | ||
110 | .Fa b . | ||
111 | After this call no further writes on BIO | ||
112 | .Fa b | ||
113 | are allowed; they will return an error. | ||
114 | Reads on the other half of the pair will return any pending data | ||
115 | or EOF when all pending data has been read. | ||
116 | .Pp | ||
117 | .Fn BIO_set_write_buf_size | ||
118 | sets the write buffer size of BIO | ||
119 | .Fa b | ||
120 | to | ||
121 | .Fa size . | ||
122 | If the size is not initialized a default value is used. | ||
123 | This is currently 17K, sufficient for a maximum size TLS record. | ||
124 | .Pp | ||
125 | .Fn BIO_get_write_buf_size | ||
126 | returns the size of the write buffer. | ||
127 | .Pp | ||
128 | .Fn BIO_new_bio_pair | ||
129 | combines the calls to | ||
130 | .Xr BIO_new 3 , | ||
131 | .Fn BIO_make_bio_pair | ||
132 | and | ||
133 | .Fn BIO_set_write_buf_size | ||
134 | to create a connected pair of BIOs | ||
135 | .Fa bio1 | ||
136 | and | ||
137 | .Fa bio2 | ||
138 | with write buffer sizes | ||
139 | .Fa writebuf1 | ||
140 | and | ||
141 | .Fa writebuf2 . | ||
142 | If either size is zero, then the default size is used. | ||
143 | .Fn BIO_new_bio_pair | ||
144 | does not check whether | ||
145 | .Fa bio1 | ||
146 | or | ||
147 | .Fa bio2 | ||
148 | do point to some other BIO, the values are overwritten, | ||
149 | .Xr BIO_free 3 | ||
150 | is not called. | ||
151 | .Pp | ||
152 | .Fn BIO_get_write_guarantee | ||
153 | and | ||
154 | .Fn BIO_ctrl_get_write_guarantee | ||
155 | return the maximum length of data | ||
156 | that can be currently written to the BIO. | ||
157 | Writes larger than this value will return a value from | ||
158 | .Xr BIO_write 3 | ||
159 | less than the amount requested or if the buffer is full request a retry. | ||
160 | .Fn BIO_ctrl_get_write_guarantee | ||
161 | is a function whereas | ||
162 | .Fn BIO_get_write_guarantee | ||
163 | is a macro. | ||
164 | .Pp | ||
165 | .Fn BIO_get_read_request | ||
166 | and | ||
167 | .Fn BIO_ctrl_get_read_request | ||
168 | return the amount of data requested, or the buffer size if it is less, | ||
169 | if the last read attempt at the other half of the BIO pair failed | ||
170 | due to an empty buffer. | ||
171 | This can be used to determine how much data should be | ||
172 | written to the BIO so the next read will succeed: | ||
173 | this is most useful in TLS/SSL applications where the amount of | ||
174 | data read is usually meaningful rather than just a buffer size. | ||
175 | After a successful read this call will return zero. | ||
176 | It also will return zero once new data has been written | ||
177 | satisfying the read request or part of it. | ||
178 | Note that | ||
179 | .Fn BIO_get_read_request | ||
180 | never returns an amount larger than that returned by | ||
181 | .Fn BIO_get_write_guarantee . | ||
182 | .Pp | ||
183 | .Fn BIO_ctrl_reset_read_request | ||
184 | can also be used to reset the value returned by | ||
185 | .Fn BIO_get_read_request | ||
186 | to zero. | ||
187 | .Sh RETURN VALUES | ||
188 | .Fn BIO_new_bio_pair | ||
189 | returns 1 on success, with the new BIOs available in | ||
190 | .Fa bio1 | ||
191 | and | ||
192 | .Fa bio2 , | ||
193 | or 0 on failure, with NULL pointers stored into the locations for | ||
194 | .Fa bio1 | ||
195 | and | ||
196 | .Fa bio2 . | ||
197 | Check the error stack for more information. | ||
198 | .\" XXX More return values need to be added here. | ||
199 | .Sh NOTES | ||
200 | Both halves of a BIO pair should be freed. | ||
201 | Even if one half is implicitly freed due to a | ||
202 | .Xr BIO_free_all 3 | ||
203 | or | ||
204 | .Xr SSL_free 3 | ||
205 | call, the other half still needs to be freed. | ||
206 | .Pp | ||
207 | When used in bidirectional applications (such as TLS/SSL) | ||
208 | care should be taken to flush any data in the write buffer. | ||
209 | This can be done by calling | ||
210 | .Xr BIO_pending 3 | ||
211 | on the other half of the pair and, if any data is pending, | ||
212 | reading it and sending it to the underlying transport. | ||
213 | This must be done before any normal processing (such as calling | ||
214 | .Xr select 2 ) | ||
215 | due to a request and | ||
216 | .Xr BIO_should_read 3 | ||
217 | being true. | ||
218 | .Pp | ||
219 | To see why this is important, | ||
220 | consider a case where a request is sent using | ||
221 | .Xr BIO_write 3 | ||
222 | and a response read with | ||
223 | .Xr BIO_read 3 , | ||
224 | this can occur during an TLS/SSL handshake for example. | ||
225 | .Xr BIO_write 3 | ||
226 | will succeed and place data in the write buffer. | ||
227 | .Xr BIO_read 3 | ||
228 | will initially fail and | ||
229 | .Xr BIO_should_read 3 | ||
230 | will be true. | ||
231 | If the application then waits for data to become available | ||
232 | on the underlying transport before flushing the write buffer, | ||
233 | it will never succeed because the request was never sent. | ||
234 | .Sh EXAMPLE | ||
235 | The BIO pair can be used to have full control | ||
236 | over the network access of an application. | ||
237 | The application can call | ||
238 | .Xr select 2 | ||
239 | on the socket as required without having to go through the SSL-interface. | ||
240 | .Bd -literal -offset 2n | ||
241 | BIO *internal_bio, *network_bio; | ||
242 | \&... | ||
243 | BIO_new_bio_pair(internal_bio, 0, network_bio, 0); | ||
244 | SSL_set_bio(ssl, internal_bio, internal_bio); | ||
245 | SSL_operations(); | ||
246 | \&... | ||
247 | |||
248 | application | TLS-engine | ||
249 | | | | ||
250 | +----------> SSL_operations() | ||
251 | | /\e || | ||
252 | | || \e/ | ||
253 | | BIO-pair (internal_bio) | ||
254 | +----------< BIO-pair (network_bio) | ||
255 | | | | ||
256 | socket | | ||
257 | |||
258 | \&... | ||
259 | SSL_free(ssl); /* implicitly frees internal_bio */ | ||
260 | BIO_free(network_bio); | ||
261 | \&... | ||
262 | .Ed | ||
263 | .Pp | ||
264 | As the BIO pair will only buffer the data and never directly access | ||
265 | the connection, it behaves non-blocking and will return as soon as | ||
266 | the write buffer is full or the read buffer is drained. | ||
267 | Then the application has to flush the write buffer | ||
268 | and/or fill the read buffer. | ||
269 | .Pp | ||
270 | Use | ||
271 | .Xr BIO_ctrl_pending 3 | ||
272 | to find out whether data is buffered in the BIO | ||
273 | and must be transfered to the network. | ||
274 | Use | ||
275 | .Fn BIO_ctrl_get_read_request | ||
276 | to find out how many bytes must be written into the buffer before the | ||
277 | .Xr SSL_operation 3 | ||
278 | can successfully be continued. | ||
279 | .Sh SEE ALSO | ||
280 | .Xr bio 3 , | ||
281 | .Xr BIO_read 3 , | ||
282 | .Xr BIO_should_retry 3 , | ||
283 | .Xr ssl 3 , | ||
284 | .Xr SSL_set_bio 3 | ||
285 | .Sh CAVEATS | ||
286 | As the data is buffered, | ||
287 | .Xr SSL_operation 3 | ||
288 | may return with an | ||
289 | .Dv ERROR_SSL_WANT_READ | ||
290 | condition, but there is still data in the write buffer. | ||
291 | An application must not rely on the error value of | ||
292 | .Xr SSL_operation 3 | ||
293 | but must assure that the write buffer is always flushed first. | ||
294 | Otherwise a deadlock may occur as the peer might be waiting | ||
295 | for the data before being able to continue. | ||