2000-09-14 18:55:39 +00:00
|
|
|
=pod
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
2016-05-20 12:11:46 +00:00
|
|
|
BIO_s_bio, BIO_make_bio_pair, BIO_destroy_bio_pair, BIO_shutdown_wr,
|
2000-09-16 21:21:01 +00:00
|
|
|
BIO_set_write_buf_size, BIO_get_write_buf_size, BIO_new_bio_pair,
|
|
|
|
BIO_get_write_guarantee, BIO_ctrl_get_write_guarantee, BIO_get_read_request,
|
|
|
|
BIO_ctrl_get_read_request, BIO_ctrl_reset_read_request - BIO pair BIO
|
2000-09-14 18:55:39 +00:00
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
|
|
|
#include <openssl/bio.h>
|
|
|
|
|
2016-03-19 16:32:14 +00:00
|
|
|
const BIO_METHOD *BIO_s_bio(void);
|
2000-09-14 18:55:39 +00:00
|
|
|
|
2016-07-08 16:55:45 +00:00
|
|
|
int BIO_make_bio_pair(BIO *b1, BIO *b2);
|
|
|
|
int BIO_destroy_bio_pair(BIO *b);
|
|
|
|
int BIO_shutdown_wr(BIO *b);
|
2000-09-14 18:55:39 +00:00
|
|
|
|
2016-07-08 16:55:45 +00:00
|
|
|
int BIO_set_write_buf_size(BIO *b, long size);
|
|
|
|
size_t BIO_get_write_buf_size(BIO *b, long size);
|
2000-09-14 18:55:39 +00:00
|
|
|
|
|
|
|
int BIO_new_bio_pair(BIO **bio1, size_t writebuf1, BIO **bio2, size_t writebuf2);
|
|
|
|
|
2016-07-08 16:55:45 +00:00
|
|
|
int BIO_get_write_guarantee(BIO *b);
|
2000-09-14 18:55:39 +00:00
|
|
|
size_t BIO_ctrl_get_write_guarantee(BIO *b);
|
2016-07-08 16:55:45 +00:00
|
|
|
int BIO_get_read_request(BIO *b);
|
2000-09-14 18:55:39 +00:00
|
|
|
size_t BIO_ctrl_get_read_request(BIO *b);
|
|
|
|
int BIO_ctrl_reset_read_request(BIO *b);
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
BIO_s_bio() returns the method for a BIO pair. A BIO pair is a pair of source/sink
|
|
|
|
BIOs where data written to either half of the pair is buffered and can be read from
|
2000-09-14 22:09:55 +00:00
|
|
|
the other half. Both halves must usually by handled by the same application thread
|
|
|
|
since no locking is done on the internal data structures.
|
2000-09-14 18:55:39 +00:00
|
|
|
|
|
|
|
Since BIO chains typically end in a source/sink BIO it is possible to make this
|
|
|
|
one half of a BIO pair and have all the data processed by the chain under application
|
|
|
|
control.
|
|
|
|
|
2000-09-14 22:09:55 +00:00
|
|
|
One typical use of BIO pairs is to place TLS/SSL I/O under application control, this
|
|
|
|
can be used when the application wishes to use a non standard transport for
|
|
|
|
TLS/SSL or the normal socket routines are inappropriate.
|
2000-09-14 18:55:39 +00:00
|
|
|
|
2016-10-20 08:56:18 +00:00
|
|
|
Calls to BIO_read_ex() will read data from the buffer or request a retry if no
|
2000-09-14 18:55:39 +00:00
|
|
|
data is available.
|
|
|
|
|
2016-10-20 08:56:18 +00:00
|
|
|
Calls to BIO_write_ex() will place data in the buffer or request a retry if the
|
2000-09-14 18:55:39 +00:00
|
|
|
buffer is full.
|
|
|
|
|
|
|
|
The standard calls BIO_ctrl_pending() and BIO_ctrl_wpending() can be used to
|
|
|
|
determine the amount of pending data in the read or write buffer.
|
|
|
|
|
|
|
|
BIO_reset() clears any data in the write buffer.
|
|
|
|
|
|
|
|
BIO_make_bio_pair() joins two separate BIOs into a connected pair.
|
|
|
|
|
|
|
|
BIO_destroy_pair() destroys the association between two connected BIOs. Freeing
|
2000-09-14 22:09:55 +00:00
|
|
|
up any half of the pair will automatically destroy the association.
|
2000-09-14 18:55:39 +00:00
|
|
|
|
2000-09-17 01:23:53 +00:00
|
|
|
BIO_shutdown_wr() is used to close down a BIO B<b>. After this call no further
|
2000-09-16 21:21:01 +00:00
|
|
|
writes on BIO B<b> are allowed (they will return an error). Reads on the other
|
|
|
|
half of the pair will return any pending data or EOF when all pending data has
|
2016-05-20 12:11:46 +00:00
|
|
|
been read.
|
2000-09-16 21:21:01 +00:00
|
|
|
|
2000-09-14 18:55:39 +00:00
|
|
|
BIO_set_write_buf_size() sets the write buffer size of BIO B<b> to B<size>.
|
2000-09-16 15:39:28 +00:00
|
|
|
If the size is not initialized a default value is used. This is currently
|
2000-09-14 18:55:39 +00:00
|
|
|
17K, sufficient for a maximum size TLS record.
|
|
|
|
|
|
|
|
BIO_get_write_buf_size() returns the size of the write buffer.
|
|
|
|
|
|
|
|
BIO_new_bio_pair() combines the calls to BIO_new(), BIO_make_bio_pair() and
|
|
|
|
BIO_set_write_buf_size() to create a connected pair of BIOs B<bio1>, B<bio2>
|
|
|
|
with write buffer sizes B<writebuf1> and B<writebuf2>. If either size is
|
2002-12-12 22:12:02 +00:00
|
|
|
zero then the default size is used. BIO_new_bio_pair() does not check whether
|
|
|
|
B<bio1> or B<bio2> do point to some other BIO, the values are overwritten,
|
|
|
|
BIO_free() is not called.
|
2000-09-14 18:55:39 +00:00
|
|
|
|
2000-09-16 15:39:28 +00:00
|
|
|
BIO_get_write_guarantee() and BIO_ctrl_get_write_guarantee() return the maximum
|
2000-09-14 18:55:39 +00:00
|
|
|
length of data that can be currently written to the BIO. Writes larger than this
|
2016-10-20 08:56:18 +00:00
|
|
|
value will return a value from BIO_write_ex() less than the amount requested or
|
|
|
|
if the buffer is full request a retry. BIO_ctrl_get_write_guarantee() is a
|
|
|
|
function whereas BIO_get_write_guarantee() is a macro.
|
2000-09-14 18:55:39 +00:00
|
|
|
|
2000-09-16 15:55:57 +00:00
|
|
|
BIO_get_read_request() and BIO_ctrl_get_read_request() return the
|
|
|
|
amount of data requested, or the buffer size if it is less, if the
|
|
|
|
last read attempt at the other half of the BIO pair failed due to an
|
|
|
|
empty buffer. This can be used to determine how much data should be
|
|
|
|
written to the BIO so the next read will succeed: this is most useful
|
|
|
|
in TLS/SSL applications where the amount of data read is usually
|
|
|
|
meaningful rather than just a buffer size. After a successful read
|
|
|
|
this call will return zero. It also will return zero once new data
|
|
|
|
has been written satisfying the read request or part of it.
|
|
|
|
Note that BIO_get_read_request() never returns an amount larger
|
|
|
|
than that returned by BIO_get_write_guarantee().
|
2000-09-14 18:55:39 +00:00
|
|
|
|
|
|
|
BIO_ctrl_reset_read_request() can also be used to reset the value returned by
|
|
|
|
BIO_get_read_request() to zero.
|
|
|
|
|
|
|
|
=head1 NOTES
|
|
|
|
|
2000-09-16 15:39:28 +00:00
|
|
|
Both halves of a BIO pair should be freed. That is even if one half is implicit
|
2000-09-14 18:55:39 +00:00
|
|
|
freed due to a BIO_free_all() or SSL_free() call the other half needs to be freed.
|
|
|
|
|
2000-09-14 22:09:55 +00:00
|
|
|
When used in bidirectional applications (such as TLS/SSL) care should be taken to
|
2000-09-14 18:55:39 +00:00
|
|
|
flush any data in the write buffer. This can be done by calling BIO_pending()
|
|
|
|
on the other half of the pair and, if any data is pending, reading it and sending
|
|
|
|
it to the underlying transport. This must be done before any normal processing
|
|
|
|
(such as calling select() ) due to a request and BIO_should_read() being true.
|
|
|
|
|
|
|
|
To see why this is important consider a case where a request is sent using
|
2016-10-20 08:56:18 +00:00
|
|
|
BIO_write_ex() and a response read with BIO_read_ex(), this can occur during an
|
|
|
|
TLS/SSL handshake for example. BIO_write_ex() will succeed and place data in the
|
|
|
|
write buffer. BIO_read_ex() will initially fail and BIO_should_read() will be
|
|
|
|
true. If the application then waits for data to be available on the underlying
|
|
|
|
transport before flushing the write buffer it will never succeed because the
|
|
|
|
request was never sent!
|
2000-09-14 18:55:39 +00:00
|
|
|
|
2016-05-09 14:04:11 +00:00
|
|
|
BIO_eof() is true if no data is in the peer BIO and the peer BIO has been
|
|
|
|
shutdown.
|
|
|
|
|
2016-07-08 16:55:45 +00:00
|
|
|
BIO_make_bio_pair(), BIO_destroy_bio_pair(), BIO_shutdown_wr(),
|
|
|
|
BIO_set_write_buf_size(), BIO_get_write_buf_size(),
|
|
|
|
BIO_get_write_guarantee(), and BIO_get_read_request() are implemented
|
|
|
|
as macros.
|
|
|
|
|
2002-12-12 22:12:02 +00:00
|
|
|
=head1 RETURN VALUES
|
|
|
|
|
|
|
|
BIO_new_bio_pair() returns 1 on success, with the new BIOs available in
|
|
|
|
B<bio1> and B<bio2>, or 0 on failure, with NULL pointers stored into the
|
|
|
|
locations for B<bio1> and B<bio2>. Check the error stack for more information.
|
|
|
|
|
2003-05-23 09:08:59 +00:00
|
|
|
[XXXXX: More return values need to be added here]
|
2002-12-12 22:12:02 +00:00
|
|
|
|
2019-08-19 00:20:37 +00:00
|
|
|
=head1 EXAMPLES
|
2000-09-14 18:55:39 +00:00
|
|
|
|
2002-12-12 22:12:02 +00:00
|
|
|
The BIO pair can be used to have full control over the network access of an
|
|
|
|
application. The application can call select() on the socket as required
|
|
|
|
without having to go through the SSL-interface.
|
|
|
|
|
|
|
|
BIO *internal_bio, *network_bio;
|
2017-01-20 18:58:49 +00:00
|
|
|
|
2002-12-12 22:12:02 +00:00
|
|
|
...
|
2015-08-27 16:28:08 +00:00
|
|
|
BIO_new_bio_pair(&internal_bio, 0, &network_bio, 0);
|
2002-12-12 22:12:02 +00:00
|
|
|
SSL_set_bio(ssl, internal_bio, internal_bio);
|
2017-04-03 17:52:33 +00:00
|
|
|
SSL_operations(); /* e.g SSL_read and SSL_write */
|
2002-12-12 22:12:02 +00:00
|
|
|
...
|
|
|
|
|
|
|
|
application | TLS-engine
|
|
|
|
| |
|
|
|
|
+----------> SSL_operations()
|
|
|
|
| /\ ||
|
|
|
|
| || \/
|
|
|
|
| BIO-pair (internal_bio)
|
2015-08-27 16:28:08 +00:00
|
|
|
| BIO-pair (network_bio)
|
|
|
|
| || /\
|
|
|
|
| \/ ||
|
|
|
|
+-----------< BIO_operations()
|
2002-12-12 22:12:02 +00:00
|
|
|
| |
|
2015-08-27 16:28:08 +00:00
|
|
|
| |
|
|
|
|
socket
|
2002-12-12 22:12:02 +00:00
|
|
|
|
|
|
|
...
|
2016-05-20 12:11:46 +00:00
|
|
|
SSL_free(ssl); /* implicitly frees internal_bio */
|
2002-12-12 22:12:02 +00:00
|
|
|
BIO_free(network_bio);
|
|
|
|
...
|
|
|
|
|
|
|
|
As the BIO pair will only buffer the data and never directly access the
|
|
|
|
connection, it behaves non-blocking and will return as soon as the write
|
|
|
|
buffer is full or the read buffer is drained. Then the application has to
|
|
|
|
flush the write buffer and/or fill the read buffer.
|
|
|
|
|
|
|
|
Use the BIO_ctrl_pending(), to find out whether data is buffered in the BIO
|
2015-04-13 18:05:13 +00:00
|
|
|
and must be transferred to the network. Use BIO_ctrl_get_read_request() to
|
2002-12-12 22:12:02 +00:00
|
|
|
find out, how many bytes must be written into the buffer before the
|
|
|
|
SSL_operation() can successfully be continued.
|
|
|
|
|
2019-08-19 00:20:37 +00:00
|
|
|
=head1 WARNINGS
|
2002-12-12 22:12:02 +00:00
|
|
|
|
2016-03-20 15:51:06 +00:00
|
|
|
As the data is buffered, SSL_operation() may return with an ERROR_SSL_WANT_READ
|
2002-12-12 22:12:02 +00:00
|
|
|
condition, but there is still data in the write buffer. An application must
|
|
|
|
not rely on the error value of SSL_operation() but must assure that the
|
|
|
|
write buffer is always flushed first. Otherwise a deadlock may occur as
|
|
|
|
the peer might be waiting for the data before being able to continue.
|
2000-09-14 18:55:39 +00:00
|
|
|
|
|
|
|
=head1 SEE ALSO
|
|
|
|
|
2016-11-11 08:33:09 +00:00
|
|
|
L<SSL_set_bio(3)>, L<ssl(7)>, L<bio(7)>,
|
2016-10-20 08:56:18 +00:00
|
|
|
L<BIO_should_retry(3)>, L<BIO_read_ex(3)>
|
2000-09-14 18:55:39 +00:00
|
|
|
|
2016-05-18 15:44:05 +00:00
|
|
|
=head1 COPYRIGHT
|
|
|
|
|
2019-09-10 12:56:40 +00:00
|
|
|
Copyright 2000-2019 The OpenSSL Project Authors. All Rights Reserved.
|
2016-05-18 15:44:05 +00:00
|
|
|
|
|
|
|
Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
this file except in compliance with the License. You can obtain a copy
|
|
|
|
in the file LICENSE in the source distribution or at
|
|
|
|
L<https://www.openssl.org/source/license.html>.
|
|
|
|
|
|
|
|
=cut
|