xref: /freebsd/crypto/openssl/doc/man3/BIO_f_ssl.pod (revision 7877fdebeeb35fad1cbbafce22598b1bdf97c786)
1=pod
2
3=head1 NAME
4
5BIO_do_handshake,
6BIO_f_ssl, BIO_set_ssl, BIO_get_ssl, BIO_set_ssl_mode,
7BIO_set_ssl_renegotiate_bytes,
8BIO_get_num_renegotiates, BIO_set_ssl_renegotiate_timeout, BIO_new_ssl,
9BIO_new_ssl_connect, BIO_new_buffer_ssl_connect, BIO_ssl_copy_session_id,
10BIO_ssl_shutdown - SSL BIO
11
12=head1 SYNOPSIS
13
14=for comment multiple includes
15
16 #include <openssl/bio.h>
17 #include <openssl/ssl.h>
18
19 const BIO_METHOD *BIO_f_ssl(void);
20
21 long BIO_set_ssl(BIO *b, SSL *ssl, long c);
22 long BIO_get_ssl(BIO *b, SSL **sslp);
23 long BIO_set_ssl_mode(BIO *b, long client);
24 long BIO_set_ssl_renegotiate_bytes(BIO *b, long num);
25 long BIO_set_ssl_renegotiate_timeout(BIO *b, long seconds);
26 long BIO_get_num_renegotiates(BIO *b);
27
28 BIO *BIO_new_ssl(SSL_CTX *ctx, int client);
29 BIO *BIO_new_ssl_connect(SSL_CTX *ctx);
30 BIO *BIO_new_buffer_ssl_connect(SSL_CTX *ctx);
31 int BIO_ssl_copy_session_id(BIO *to, BIO *from);
32 void BIO_ssl_shutdown(BIO *bio);
33
34 long BIO_do_handshake(BIO *b);
35
36=head1 DESCRIPTION
37
38BIO_f_ssl() returns the SSL BIO method. This is a filter BIO which
39is a wrapper round the OpenSSL SSL routines adding a BIO "flavour" to
40SSL I/O.
41
42I/O performed on an SSL BIO communicates using the SSL protocol with
43the SSLs read and write BIOs. If an SSL connection is not established
44then an attempt is made to establish one on the first I/O call.
45
46If a BIO is appended to an SSL BIO using BIO_push() it is automatically
47used as the SSL BIOs read and write BIOs.
48
49Calling BIO_reset() on an SSL BIO closes down any current SSL connection
50by calling SSL_shutdown(). BIO_reset() is then sent to the next BIO in
51the chain: this will typically disconnect the underlying transport.
52The SSL BIO is then reset to the initial accept or connect state.
53
54If the close flag is set when an SSL BIO is freed then the internal
55SSL structure is also freed using SSL_free().
56
57BIO_set_ssl() sets the internal SSL pointer of BIO B<b> to B<ssl> using
58the close flag B<c>.
59
60BIO_get_ssl() retrieves the SSL pointer of BIO B<b>, it can then be
61manipulated using the standard SSL library functions.
62
63BIO_set_ssl_mode() sets the SSL BIO mode to B<client>. If B<client>
64is 1 client mode is set. If B<client> is 0 server mode is set.
65
66BIO_set_ssl_renegotiate_bytes() sets the renegotiate byte count
67to B<num>. When set after every B<num> bytes of I/O (read and write)
68the SSL session is automatically renegotiated. B<num> must be at
69least 512 bytes.
70
71BIO_set_ssl_renegotiate_timeout() sets the renegotiate timeout to
72B<seconds>. When the renegotiate timeout elapses the session is
73automatically renegotiated.
74
75BIO_get_num_renegotiates() returns the total number of session
76renegotiations due to I/O or timeout.
77
78BIO_new_ssl() allocates an SSL BIO using SSL_CTX B<ctx> and using
79client mode if B<client> is non zero.
80
81BIO_new_ssl_connect() creates a new BIO chain consisting of an
82SSL BIO (using B<ctx>) followed by a connect BIO.
83
84BIO_new_buffer_ssl_connect() creates a new BIO chain consisting
85of a buffering BIO, an SSL BIO (using B<ctx>) and a connect
86BIO.
87
88BIO_ssl_copy_session_id() copies an SSL session id between
89BIO chains B<from> and B<to>. It does this by locating the
90SSL BIOs in each chain and calling SSL_copy_session_id() on
91the internal SSL pointer.
92
93BIO_ssl_shutdown() closes down an SSL connection on BIO
94chain B<bio>. It does this by locating the SSL BIO in the
95chain and calling SSL_shutdown() on its internal SSL
96pointer.
97
98BIO_do_handshake() attempts to complete an SSL handshake on the
99supplied BIO and establish the SSL connection. It returns 1
100if the connection was established successfully. A zero or negative
101value is returned if the connection could not be established, the
102call BIO_should_retry() should be used for non blocking connect BIOs
103to determine if the call should be retried. If an SSL connection has
104already been established this call has no effect.
105
106=head1 NOTES
107
108SSL BIOs are exceptional in that if the underlying transport
109is non blocking they can still request a retry in exceptional
110circumstances. Specifically this will happen if a session
111renegotiation takes place during a BIO_read_ex() operation, one
112case where this happens is when step up occurs.
113
114The SSL flag SSL_AUTO_RETRY can be
115set to disable this behaviour. That is when this flag is set
116an SSL BIO using a blocking transport will never request a
117retry.
118
119Since unknown BIO_ctrl() operations are sent through filter
120BIOs the servers name and port can be set using BIO_set_host()
121on the BIO returned by BIO_new_ssl_connect() without having
122to locate the connect BIO first.
123
124Applications do not have to call BIO_do_handshake() but may wish
125to do so to separate the handshake process from other I/O
126processing.
127
128BIO_set_ssl(), BIO_get_ssl(), BIO_set_ssl_mode(),
129BIO_set_ssl_renegotiate_bytes(), BIO_set_ssl_renegotiate_timeout(),
130BIO_get_num_renegotiates(), and BIO_do_handshake() are implemented as macros.
131
132=head1 RETURN VALUES
133
134BIO_f_ssl() returns the SSL B<BIO_METHOD> structure.
135
136BIO_set_ssl(), BIO_get_ssl(), BIO_set_ssl_mode(), BIO_set_ssl_renegotiate_bytes(),
137BIO_set_ssl_renegotiate_timeout() and BIO_get_num_renegotiates() return 1 on
138success or a value which is less than or equal to 0 if an error occurred.
139
140BIO_new_ssl(), BIO_new_ssl_connect() and BIO_new_buffer_ssl_connect() return
141a valid B<BIO> structure on success or B<NULL> if an error occurred.
142
143BIO_ssl_copy_session_id() returns 1 on success or 0 on error.
144
145BIO_do_handshake() returns 1 if the connection was established successfully.
146A zero or negative value is returned if the connection could not be established.
147
148=head1 EXAMPLES
149
150This SSL/TLS client example attempts to retrieve a page from an
151SSL/TLS web server. The I/O routines are identical to those of the
152unencrypted example in L<BIO_s_connect(3)>.
153
154 BIO *sbio, *out;
155 int len;
156 char tmpbuf[1024];
157 SSL_CTX *ctx;
158 SSL *ssl;
159
160 /* XXX Seed the PRNG if needed. */
161
162 ctx = SSL_CTX_new(TLS_client_method());
163
164 /* XXX Set verify paths and mode here. */
165
166 sbio = BIO_new_ssl_connect(ctx);
167 BIO_get_ssl(sbio, &ssl);
168 if (ssl == NULL) {
169     fprintf(stderr, "Can't locate SSL pointer\n");
170     ERR_print_errors_fp(stderr);
171     exit(1);
172 }
173
174 /* Don't want any retries */
175 SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
176
177 /* XXX We might want to do other things with ssl here */
178
179 /* An empty host part means the loopback address */
180 BIO_set_conn_hostname(sbio, ":https");
181
182 out = BIO_new_fp(stdout, BIO_NOCLOSE);
183 if (BIO_do_connect(sbio) <= 0) {
184     fprintf(stderr, "Error connecting to server\n");
185     ERR_print_errors_fp(stderr);
186     exit(1);
187 }
188 if (BIO_do_handshake(sbio) <= 0) {
189     fprintf(stderr, "Error establishing SSL connection\n");
190     ERR_print_errors_fp(stderr);
191     exit(1);
192 }
193
194 /* XXX Could examine ssl here to get connection info */
195
196 BIO_puts(sbio, "GET / HTTP/1.0\n\n");
197 for (;;) {
198     len = BIO_read(sbio, tmpbuf, 1024);
199     if (len <= 0)
200         break;
201     BIO_write(out, tmpbuf, len);
202 }
203 BIO_free_all(sbio);
204 BIO_free(out);
205
206Here is a simple server example. It makes use of a buffering
207BIO to allow lines to be read from the SSL BIO using BIO_gets.
208It creates a pseudo web page containing the actual request from
209a client and also echoes the request to standard output.
210
211 BIO *sbio, *bbio, *acpt, *out;
212 int len;
213 char tmpbuf[1024];
214 SSL_CTX *ctx;
215 SSL *ssl;
216
217 /* XXX Seed the PRNG if needed. */
218
219 ctx = SSL_CTX_new(TLS_server_method());
220 if (!SSL_CTX_use_certificate_file(ctx, "server.pem", SSL_FILETYPE_PEM)
221         || !SSL_CTX_use_PrivateKey_file(ctx, "server.pem", SSL_FILETYPE_PEM)
222         || !SSL_CTX_check_private_key(ctx)) {
223     fprintf(stderr, "Error setting up SSL_CTX\n");
224     ERR_print_errors_fp(stderr);
225     exit(1);
226 }
227
228 /* XXX Other things like set verify locations, EDH temp callbacks. */
229
230 /* New SSL BIO setup as server */
231 sbio = BIO_new_ssl(ctx, 0);
232 BIO_get_ssl(sbio, &ssl);
233 if (ssl == NULL) {
234     fprintf(stderr, "Can't locate SSL pointer\n");
235     ERR_print_errors_fp(stderr);
236     exit(1);
237 }
238
239 SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
240 bbio = BIO_new(BIO_f_buffer());
241 sbio = BIO_push(bbio, sbio);
242 acpt = BIO_new_accept("4433");
243
244 /*
245  * By doing this when a new connection is established
246  * we automatically have sbio inserted into it. The
247  * BIO chain is now 'swallowed' by the accept BIO and
248  * will be freed when the accept BIO is freed.
249  */
250 BIO_set_accept_bios(acpt, sbio);
251 out = BIO_new_fp(stdout, BIO_NOCLOSE);
252
253 /* Setup accept BIO */
254 if (BIO_do_accept(acpt) <= 0) {
255     fprintf(stderr, "Error setting up accept BIO\n");
256     ERR_print_errors_fp(stderr);
257     exit(1);
258 }
259
260 /* We only want one connection so remove and free accept BIO */
261 sbio = BIO_pop(acpt);
262 BIO_free_all(acpt);
263
264 if (BIO_do_handshake(sbio) <= 0) {
265     fprintf(stderr, "Error in SSL handshake\n");
266     ERR_print_errors_fp(stderr);
267     exit(1);
268 }
269
270 BIO_puts(sbio, "HTTP/1.0 200 OK\r\nContent-type: text/plain\r\n\r\n");
271 BIO_puts(sbio, "\r\nConnection Established\r\nRequest headers:\r\n");
272 BIO_puts(sbio, "--------------------------------------------------\r\n");
273
274 for (;;) {
275     len = BIO_gets(sbio, tmpbuf, 1024);
276     if (len <= 0)
277         break;
278     BIO_write(sbio, tmpbuf, len);
279     BIO_write(out, tmpbuf, len);
280     /* Look for blank line signifying end of headers*/
281     if (tmpbuf[0] == '\r' || tmpbuf[0] == '\n')
282         break;
283 }
284
285 BIO_puts(sbio, "--------------------------------------------------\r\n");
286 BIO_puts(sbio, "\r\n");
287 BIO_flush(sbio);
288 BIO_free_all(sbio);
289
290=head1 HISTORY
291
292In OpenSSL before 1.0.0 the BIO_pop() call was handled incorrectly,
293the I/O BIO reference count was incorrectly incremented (instead of
294decremented) and dissociated with the SSL BIO even if the SSL BIO was not
295explicitly being popped (e.g. a pop higher up the chain). Applications which
296included workarounds for this bug (e.g. freeing BIOs more than once) should
297be modified to handle this fix or they may free up an already freed BIO.
298
299=head1 COPYRIGHT
300
301Copyright 2000-2019 The OpenSSL Project Authors. All Rights Reserved.
302
303Licensed under the OpenSSL license (the "License").  You may not use
304this file except in compliance with the License.  You can obtain a copy
305in the file LICENSE in the source distribution or at
306L<https://www.openssl.org/source/license.html>.
307
308=cut
309