1 .\" $OpenBSD: BIO_f_ssl.3,v 1.5 2017/08/20 17:35:18 schwarze Exp $
2 .\" OpenSSL BIO_f_ssl.pod e90fc053 Jul 15 09:39:45 2017 -0400
3 .\" OpenSSL BIO_f_ssl.pod f672aee4 Feb 9 11:52:40 2016 -0500
5 .\" This file was written by Dr. Stephen Henson <steve@openssl.org>.
6 .\" Copyright (c) 2000, 2003, 2009, 2014-2016 The OpenSSL Project.
7 .\" All rights reserved.
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
13 .\" 1. Redistributions of source code must retain the above copyright
14 .\" notice, this list of conditions and the following disclaimer.
16 .\" 2. Redistributions in binary form must reproduce the above copyright
17 .\" notice, this list of conditions and the following disclaimer in
18 .\" the documentation and/or other materials provided with the
21 .\" 3. All advertising materials mentioning features or use of this
22 .\" software must display the following acknowledgment:
23 .\" "This product includes software developed by the OpenSSL Project
24 .\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
26 .\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 .\" endorse or promote products derived from this software without
28 .\" prior written permission. For written permission, please contact
29 .\" openssl-core@openssl.org.
31 .\" 5. Products derived from this software may not be called "OpenSSL"
32 .\" nor may "OpenSSL" appear in their names without prior written
33 .\" permission of the OpenSSL Project.
35 .\" 6. Redistributions of any form whatsoever must retain the following
37 .\" "This product includes software developed by the OpenSSL Project
38 .\" for use in the OpenSSL Toolkit (http://www.openssl.org/)"
40 .\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 .\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 .\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 .\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 .\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 .\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 .\" OF THE POSSIBILITY OF SUCH DAMAGE.
53 .Dd $Mdocdate: August 20 2017 $
60 .Nm BIO_set_ssl_mode ,
61 .Nm BIO_set_ssl_renegotiate_bytes ,
62 .Nm BIO_get_num_renegotiates ,
63 .Nm BIO_set_ssl_renegotiate_timeout ,
65 .Nm BIO_new_ssl_connect ,
66 .Nm BIO_new_buffer_ssl_connect ,
67 .Nm BIO_ssl_copy_session_id ,
68 .Nm BIO_ssl_shutdown ,
93 .Fo BIO_set_ssl_renegotiate_bytes
98 .Fo BIO_set_ssl_renegotiate_timeout
103 .Fo BIO_get_num_renegotiates
107 .Fn BIO_new_ssl "SSL_CTX *ctx" "int client"
109 .Fn BIO_new_ssl_connect "SSL_CTX *ctx"
111 .Fn BIO_new_buffer_ssl_connect "SSL_CTX *ctx"
113 .Fn BIO_ssl_copy_session_id "BIO *to" "BIO *from"
115 .Fn BIO_ssl_shutdown "BIO *bio"
117 .Fn BIO_do_handshake "BIO *b"
126 which is a wrapper around the OpenSSL
136 communicates using the SSL protocol with
141 If an SSL connection is not established then an attempt is made to establish
142 one on the first I/O call.
151 it is automatically used as the
153 .Vt BIO Ns 's read and write
161 closes down any current SSL connection by calling
164 is then sent to the next
166 in the chain; this will typically disconnect the underlying transport.
170 is then reset to the initial accept or connect state.
172 If the close flag is set when an
175 is freed then the internal
177 structure is also freed using
198 it can then be manipulated using the standard SSL library functions.
208 is 1, client mode is set.
211 is 0, server mode is set.
213 .Fn BIO_set_ssl_renegotiate_bytes
214 sets the renegotiate byte count to
218 bytes of I/O (read and write) the SSL session is automatically renegotiated.
220 must be at least 512 bytes.
222 .Fn BIO_set_ssl_renegotiate_timeout
223 sets the renegotiate timeout to
225 When the renegotiate timeout elapses the session is automatically renegotiated.
227 .Fn BIO_get_num_renegotiates
228 returns the total number of session renegotiations due to I/O or timeout.
237 and using client mode if
241 .Fn BIO_new_ssl_connect
244 chain consisting of an
249 followed by a connect BIO.
251 .Fn BIO_new_buffer_ssl_connect
254 chain consisting of a buffering
264 .Fn BIO_ssl_copy_session_id
265 copies an SSL session id between
271 It does this by locating the
274 in each chain and calling
275 .Xr SSL_copy_session_id 3
281 closes down an SSL connection on
285 It does this by locating the
296 attempts to complete an SSL handshake on the supplied
298 and establish the SSL connection.
299 It returns 1 if the connection was established successfully.
300 A zero or negative value is returned if the connection could not be
301 established; the call
302 .Xr BIO_should_retry 3
303 should be used for non blocking connect
305 to determine if the call should be retried.
306 If an SSL connection has already been established this call has no effect.
310 are exceptional in that if the underlying transport is non-blocking they can
311 still request a retry in exceptional circumstances.
312 Specifically this will happen if a session renegotiation takes place during a
315 One case where this happens is when step up occurs.
317 In OpenSSL 0.9.6 and later the SSL flag
319 can be set to disable this behaviour.
320 In other words, when this flag is set an
323 using a blocking transport will never request a retry.
327 operations are sent through filter
329 the server name and port can be set using
330 .Xr BIO_set_conn_hostname 3
332 .Xr BIO_set_conn_port 3
336 .Fn BIO_new_ssl_connect
337 without having to locate the connect
341 Applications do not have to call
343 but may wish to do so to separate the handshake process from other I/O
348 .Fn BIO_set_ssl_mode ,
349 .Fn BIO_set_ssl_renegotiate_bytes ,
350 .Fn BIO_set_ssl_renegotiate_timeout ,
351 .Fn BIO_get_num_renegotiates ,
354 are implemented as macros.
357 This section is incomplete.
359 This SSL/TLS client example attempts to retrieve a page from an SSL/TLS web
361 The I/O routines are identical to those of the unencrypted example in
362 .Xr BIO_s_connect 3 .
370 ERR_load_crypto_strings();
371 ERR_load_SSL_strings();
372 OpenSSL_add_all_algorithms();
375 * We would seed the PRNG here if the platform didn't do it automatically
378 ctx = SSL_CTX_new(SSLv23_client_method());
381 * We'd normally set some stuff like the verify paths and mode here because
382 * as things stand this will connect to any server whose certificate is
386 sbio = BIO_new_ssl_connect(ctx);
388 BIO_get_ssl(sbio, &ssl);
391 fprintf(stderr, "Can't locate SSL pointer\en");
395 /* Don't want any retries */
396 SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
398 /* We might want to do other things with ssl here */
400 BIO_set_conn_hostname(sbio, "localhost:https");
402 out = BIO_new_fp(stdout, BIO_NOCLOSE);
403 if (BIO_do_connect(sbio) <= 0) {
404 fprintf(stderr, "Error connecting to server\en");
405 ERR_print_errors_fp(stderr);
409 if (BIO_do_handshake(sbio) <= 0) {
410 fprintf(stderr, "Error establishing SSL connection\en");
411 ERR_print_errors_fp(stderr);
415 /* Could examine ssl here to get connection info */
417 BIO_puts(sbio, "GET / HTTP/1.0\en\en");
419 len = BIO_read(sbio, tmpbuf, 1024);
421 BIO_write(out, tmpbuf, len);
427 Here is a simple server example.
428 It makes use of a buffering
430 to allow lines to be read from the
435 It creates a pseudo web page containing the actual request from a client and
436 also echoes the request to standard output.
438 BIO *sbio, *bbio, *acpt, *out;
444 ERR_load_crypto_strings();
445 ERR_load_SSL_strings();
446 OpenSSL_add_all_algorithms();
448 /* Might seed PRNG here */
450 ctx = SSL_CTX_new(SSLv23_server_method());
452 if (!SSL_CTX_use_certificate_file(ctx,"server.pem",SSL_FILETYPE_PEM)
453 || !SSL_CTX_use_PrivateKey_file(ctx,"server.pem",SSL_FILETYPE_PEM)
454 || !SSL_CTX_check_private_key(ctx)) {
455 fprintf(stderr, "Error setting up SSL_CTX\en");
456 ERR_print_errors_fp(stderr);
461 * Might do other things here like setting verify locations and DH and/or
462 * RSA temporary key callbacks
465 /* New SSL BIO setup as server */
466 sbio = BIO_new_ssl(ctx,0);
468 BIO_get_ssl(sbio, &ssl);
471 fprintf(stderr, "Can't locate SSL pointer\en");
475 /* Don't want any retries */
476 SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
478 /* Create the buffering BIO */
480 bbio = BIO_new(BIO_f_buffer());
483 sbio = BIO_push(bbio, sbio);
485 acpt = BIO_new_accept("4433");
488 * By doing this when a new connection is established we automatically
489 * have sbio inserted into it. The BIO chain is now 'swallowed' by the
490 * accept BIO and will be freed when the accept BIO is freed.
493 BIO_set_accept_bios(acpt,sbio);
495 out = BIO_new_fp(stdout, BIO_NOCLOSE);
497 /* Wait for incoming connection */
498 if (BIO_do_accept(acpt) <= 0) {
499 fprintf(stderr, "Error setting up accept BIO\en");
500 ERR_print_errors_fp(stderr);
504 /* We only want one connection so remove and free accept BIO */
506 sbio = BIO_pop(acpt);
510 if (BIO_do_handshake(sbio) <= 0) {
511 fprintf(stderr, "Error in SSL handshake\en");
512 ERR_print_errors_fp(stderr);
516 BIO_puts(sbio, "HTTP/1.0 200 OK\er\enContent-type: text/plain\er\en\er\en");
517 BIO_puts(sbio, "\er\enConnection Established\er\enRequest headers:\er\en");
518 BIO_puts(sbio, "--------------------------------------------------\er\en");
521 len = BIO_gets(sbio, tmpbuf, 1024);
524 BIO_write(sbio, tmpbuf, len);
525 BIO_write(out, tmpbuf, len);
526 /* Look for blank line signifying end of headers */
527 if ((tmpbuf[0] == '\er') || (tmpbuf[0] == '\en'))
531 BIO_puts(sbio, "--------------------------------------------------\er\en");
532 BIO_puts(sbio, "\er\en");
534 /* Since there is a buffering BIO present we had better flush it */
540 In OpenSSL versions before 1.0.0 the
542 call was handled incorrectly:
543 the I/O BIO reference count was incorrectly incremented (instead of
544 decremented) and dissociated with the
551 explicitly being popped (e.g., a pop higher up the chain).
552 Applications which included workarounds for this bug (e.g., freeing BIOs more
553 than once) should be modified to handle this fix or they may free up an already