1 .\" $OpenBSD: BIO_f_ssl.3,v 1.3 2016/12/16 15:39:08 jmc Exp $
2 .\" OpenSSL f672aee4 Feb 9 11:52:40 2016 -0500
4 .\" This file was written by Dr. Stephen Henson <steve@openssl.org>.
5 .\" Copyright (c) 2000, 2003, 2009, 2014-2016 The OpenSSL Project.
6 .\" All rights reserved.
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\" notice, this list of conditions and the following disclaimer.
15 .\" 2. Redistributions in binary form must reproduce the above copyright
16 .\" notice, this list of conditions and the following disclaimer in
17 .\" the documentation and/or other materials provided with the
20 .\" 3. All advertising materials mentioning features or use of this
21 .\" software must display the following acknowledgment:
22 .\" "This product includes software developed by the OpenSSL Project
23 .\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
25 .\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 .\" endorse or promote products derived from this software without
27 .\" prior written permission. For written permission, please contact
28 .\" openssl-core@openssl.org.
30 .\" 5. Products derived from this software may not be called "OpenSSL"
31 .\" nor may "OpenSSL" appear in their names without prior written
32 .\" permission of the OpenSSL Project.
34 .\" 6. Redistributions of any form whatsoever must retain the following
36 .\" "This product includes software developed by the OpenSSL Project
37 .\" for use in the OpenSSL Toolkit (http://www.openssl.org/)"
39 .\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 .\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 .\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 .\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 .\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 .\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 .\" OF THE POSSIBILITY OF SUCH DAMAGE.
52 .Dd $Mdocdate: December 16 2016 $
59 .Nm BIO_set_ssl_mode ,
60 .Nm BIO_set_ssl_renegotiate_bytes ,
61 .Nm BIO_get_num_renegotiates ,
62 .Nm BIO_set_ssl_renegotiate_timeout ,
64 .Nm BIO_new_ssl_connect ,
65 .Nm BIO_new_buffer_ssl_connect ,
66 .Nm BIO_ssl_copy_session_id ,
67 .Nm BIO_ssl_shutdown ,
92 .Fo BIO_set_ssl_renegotiate_bytes
97 .Fo BIO_set_ssl_renegotiate_timeout
102 .Fo BIO_get_num_renegotiates
106 .Fn BIO_new_ssl "SSL_CTX *ctx" "int client"
108 .Fn BIO_new_ssl_connect "SSL_CTX *ctx"
110 .Fn BIO_new_buffer_ssl_connect "SSL_CTX *ctx"
112 .Fn BIO_ssl_copy_session_id "BIO *to" "BIO *from"
114 .Fn BIO_ssl_shutdown "BIO *bio"
116 .Fn BIO_do_handshake "BIO *b"
125 which is a wrapper around the OpenSSL
135 communicates using the SSL protocol with
140 If an SSL connection is not established then an attempt is made to establish
141 one on the first I/O call.
150 it is automatically used as the
152 .Vt BIO Ns 's read and write
160 closes down any current SSL connection by calling
163 is then sent to the next
165 in the chain; this will typically disconnect the underlying transport.
169 is then reset to the initial accept or connect state.
171 If the close flag is set when an
174 is freed then the internal
176 structure is also freed using
197 it can then be manipulated using the standard SSL library functions.
207 is 1, client mode is set.
210 is 0, server mode is set.
212 .Fn BIO_set_ssl_renegotiate_bytes
213 sets the renegotiate byte count to
217 bytes of I/O (read and write) the SSL session is automatically renegotiated.
219 must be at least 512 bytes.
221 .Fn BIO_set_ssl_renegotiate_timeout
222 sets the renegotiate timeout to
224 When the renegotiate timeout elapses the session is automatically renegotiated.
226 .Fn BIO_get_num_renegotiates
227 returns the total number of session renegotiations due to I/O or timeout.
236 and using client mode if
240 .Fn BIO_new_ssl_connect
243 chain consisting of an
248 followed by a connect BIO.
250 .Fn BIO_new_buffer_ssl_connect
253 chain consisting of a buffering
263 .Fn BIO_ssl_copy_session_id
264 copies an SSL session id between
270 It does this by locating the
273 in each chain and calling
274 .Xr SSL_copy_session_id 3
280 closes down an SSL connection on
284 It does this by locating the
295 attempts to complete an SSL handshake on the supplied
297 and establish the SSL connection.
298 It returns 1 if the connection was established successfully.
299 A zero or negative value is returned if the connection could not be
300 established; the call
301 .Xr BIO_should_retry 3
302 should be used for non blocking connect
304 to determine if the call should be retried.
305 If an SSL connection has already been established this call has no effect.
309 are exceptional in that if the underlying transport is non-blocking they can
310 still request a retry in exceptional circumstances.
311 Specifically this will happen if a session renegotiation takes place during a
314 One case where this happens is when step up occurs.
316 In OpenSSL 0.9.6 and later the SSL flag
318 can be set to disable this behaviour.
319 In other words, when this flag is set an
322 using a blocking transport will never request a retry.
326 operations are sent through filter
328 the server name and port can be set using
333 .Fn BIO_new_ssl_connect
334 without having to locate the connect
338 Applications do not have to call
340 but may wish to do so to separate the handshake process from other I/O
345 .Fn BIO_set_ssl_mode ,
346 .Fn BIO_set_ssl_renegotiate_bytes ,
347 .Fn BIO_set_ssl_renegotiate_timeout ,
348 .Fn BIO_get_num_renegotiates ,
351 are implemented as macros.
354 This section is incomplete.
356 This SSL/TLS client example attempts to retrieve a page from an SSL/TLS web
358 The I/O routines are identical to those of the unencrypted example in
359 .Xr BIO_s_connect 3 .
367 ERR_load_crypto_strings();
368 ERR_load_SSL_strings();
369 OpenSSL_add_all_algorithms();
372 * We would seed the PRNG here if the platform didn't do it automatically
375 ctx = SSL_CTX_new(SSLv23_client_method());
378 * We'd normally set some stuff like the verify paths and mode here because
379 * as things stand this will connect to any server whose certificate is
383 sbio = BIO_new_ssl_connect(ctx);
385 BIO_get_ssl(sbio, &ssl);
388 fprintf(stderr, "Can't locate SSL pointer\en");
392 /* Don't want any retries */
393 SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
395 /* We might want to do other things with ssl here */
397 BIO_set_conn_hostname(sbio, "localhost:https");
399 out = BIO_new_fp(stdout, BIO_NOCLOSE);
400 if (BIO_do_connect(sbio) <= 0) {
401 fprintf(stderr, "Error connecting to server\en");
402 ERR_print_errors_fp(stderr);
406 if (BIO_do_handshake(sbio) <= 0) {
407 fprintf(stderr, "Error establishing SSL connection\en");
408 ERR_print_errors_fp(stderr);
412 /* Could examine ssl here to get connection info */
414 BIO_puts(sbio, "GET / HTTP/1.0\en\en");
416 len = BIO_read(sbio, tmpbuf, 1024);
418 BIO_write(out, tmpbuf, len);
424 Here is a simple server example.
425 It makes use of a buffering
427 to allow lines to be read from the
432 It creates a pseudo web page containing the actual request from a client and
433 also echoes the request to standard output.
435 BIO *sbio, *bbio, *acpt, *out;
441 ERR_load_crypto_strings();
442 ERR_load_SSL_strings();
443 OpenSSL_add_all_algorithms();
445 /* Might seed PRNG here */
447 ctx = SSL_CTX_new(SSLv23_server_method());
449 if (!SSL_CTX_use_certificate_file(ctx,"server.pem",SSL_FILETYPE_PEM)
450 || !SSL_CTX_use_PrivateKey_file(ctx,"server.pem",SSL_FILETYPE_PEM)
451 || !SSL_CTX_check_private_key(ctx)) {
452 fprintf(stderr, "Error setting up SSL_CTX\en");
453 ERR_print_errors_fp(stderr);
458 * Might do other things here like setting verify locations and DH and/or
459 * RSA temporary key callbacks
462 /* New SSL BIO setup as server */
463 sbio = BIO_new_ssl(ctx,0);
465 BIO_get_ssl(sbio, &ssl);
468 fprintf(stderr, "Can't locate SSL pointer\en");
472 /* Don't want any retries */
473 SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
475 /* Create the buffering BIO */
477 bbio = BIO_new(BIO_f_buffer());
480 sbio = BIO_push(bbio, sbio);
482 acpt = BIO_new_accept("4433");
485 * By doing this when a new connection is established we automatically
486 * have sbio inserted into it. The BIO chain is now 'swallowed' by the
487 * accept BIO and will be freed when the accept BIO is freed.
490 BIO_set_accept_bios(acpt,sbio);
492 out = BIO_new_fp(stdout, BIO_NOCLOSE);
494 /* Setup accept BIO */
495 if (BIO_do_accept(acpt) <= 0) {
496 fprintf(stderr, "Error setting up accept BIO\en");
497 ERR_print_errors_fp(stderr);
501 /* Now wait for incoming connection */
502 if (BIO_do_accept(acpt) <= 0) {
503 fprintf(stderr, "Error in connection\en");
504 ERR_print_errors_fp(stderr);
508 /* We only want one connection so remove and free accept BIO */
510 sbio = BIO_pop(acpt);
514 if (BIO_do_handshake(sbio) <= 0) {
515 fprintf(stderr, "Error in SSL handshake\en");
516 ERR_print_errors_fp(stderr);
520 BIO_puts(sbio, "HTTP/1.0 200 OK\er\enContent-type: text/plain\er\en\er\en");
521 BIO_puts(sbio, "\er\enConnection Established\er\enRequest headers:\er\en");
522 BIO_puts(sbio, "--------------------------------------------------\er\en");
525 len = BIO_gets(sbio, tmpbuf, 1024);
528 BIO_write(sbio, tmpbuf, len);
529 BIO_write(out, tmpbuf, len);
530 /* Look for blank line signifying end of headers */
531 if ((tmpbuf[0] == '\er') || (tmpbuf[0] == '\en'))
535 BIO_puts(sbio, "--------------------------------------------------\er\en");
536 BIO_puts(sbio, "\er\en");
538 /* Since there is a buffering BIO present we had better flush it */
544 In OpenSSL versions before 1.0.0 the
546 call was handled incorrectly:
547 the I/O BIO reference count was incorrectly incremented (instead of
548 decremented) and dissociated with the
555 explicitly being popped (e.g., a pop higher up the chain).
556 Applications which included workarounds for this bug (e.g., freeing BIOs more
557 than once) should be modified to handle this fix or they may free up an already