
#use wml::openssl-macros area=docs page=BIO_f_ssl

<title>Documents, BIO_f_ssl(3)</title>

<h1>BIO_f_ssl(3)</h1>

#use wml::imp::generic

{:
## What's this? [[s|(<STRONG>[^<].+?)</A>(</STRONG><DD>)|$1$2|sg]]
[[s|<P>\s+<P>|<P>|sg]]
[[s|<P>\s+</|</|sg]]
[[s|<DD>\s*<DT>|<DD>&nbsp;<DT>|sg]]
[[s|<DD>\s*</DL>|<DD>&nbsp;</DL>|sg]]
[[s|\[|&#91;|sg]]
[[s|\]|&#93;|sg]]

<!-- INDEX BEGIN -->

<UL>

	<LI><A HREF="#NAME">NAME</A>
	<LI><A HREF="#SYNOPSIS">SYNOPSIS</A>
	<LI><A HREF="#DESCRIPTION">DESCRIPTION</A>
	<LI><A HREF="#NOTES">NOTES</A>
	<LI><A HREF="#RETURN_VALUES">RETURN VALUES</A>
	<LI><A HREF="#EXAMPLE">EXAMPLE</A>
	<LI><A HREF="#BUGS">BUGS</A>
	<LI><A HREF="#SEE_ALSO">SEE ALSO</A>
</UL>
<!-- INDEX END -->

<HR>
<P>
<HR>
<H1><A NAME="NAME">NAME</A></H1>
<P>
BIO_f_ssl, BIO_set_ssl, BIO_get_ssl, BIO_set_ssl_mode,
BIO_set_ssl_renegotiate_bytes, BIO_get_num_renegotiates,
BIO_set_ssl_renegotiate_timeout, BIO_new_ssl, BIO_new_ssl_connect,
BIO_new_buffer_ssl_connect, BIO_ssl_copy_session_id, BIO_ssl_shutdown - SSL
BIO

</P>
<P>
<HR>
<H1><A NAME="SYNOPSIS">SYNOPSIS</A></H1>
<PRE> #include &lt;openssl/bio.h&gt;
 \#include &lt;openssl/ssl.h&gt;
</PRE>
<PRE> BIO_METHOD *BIO_f_ssl(void);
</PRE>
<PRE> #define BIO_set_ssl(b,ssl,c)   BIO_ctrl(b,BIO_C_SET_SSL,c,(char *)ssl)
 \#define BIO_get_ssl(b,sslp)    BIO_ctrl(b,BIO_C_GET_SSL,0,(char *)sslp)
 \#define BIO_set_ssl_mode(b,client)     BIO_ctrl(b,BIO_C_SSL_MODE,client,NULL)
 \#define BIO_set_ssl_renegotiate_bytes(b,num) \\
        BIO_ctrl(b,BIO_C_SET_SSL_RENEGOTIATE_BYTES,num,NULL);
 \#define BIO_set_ssl_renegotiate_timeout(b,seconds) \\
        BIO_ctrl(b,BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT,seconds,NULL);
 \#define BIO_get_num_renegotiates(b) \\
        BIO_ctrl(b,BIO_C_SET_SSL_NUM_RENEGOTIATES,0,NULL);
</PRE>
<PRE> BIO *BIO_new_ssl(SSL_CTX *ctx,int client);
 BIO *BIO_new_ssl_connect(SSL_CTX *ctx);
 BIO *BIO_new_buffer_ssl_connect(SSL_CTX *ctx);
 int BIO_ssl_copy_session_id(BIO *to,BIO *from);
 void BIO_ssl_shutdown(BIO *bio);
</PRE>
<PRE> #define BIO_do_handshake(b)    BIO_ctrl(b,BIO_C_DO_STATE_MACHINE,0,NULL)
</PRE>
<P>
<HR>
<H1><A NAME="DESCRIPTION">DESCRIPTION</A></H1>
<P>
<CODE>BIO_f_ssl()</CODE> returns the SSL BIO method. This is a filter BIO
which is a wrapper round the OpenSSL SSL routines adding a BIO ``flavour''
to SSL I/O. 

</P>
<P>
I/O performed on an SSL BIO communicates using the SSL protocol with the
SSLs read and write BIOs. If an SSL connection is not established then an
attempt is made to establish one on the first I/O call.

</P>
<P>
If a BIO is appended to an SSL BIO using <CODE>BIO_push()</CODE> it is
automatically used as the SSL BIOs read and write BIOs.

</P>
<P>
Calling <CODE>BIO_reset()</CODE> on an SSL BIO closes down any current SSL
connection by calling <CODE>SSL_shutdown().</CODE> <CODE>BIO_reset()</CODE>
is then sent to the next BIO in the chain: this will typically disconnect
the underlying transport. The SSL BIO is then reset to the initial accept
or connect state.

</P>
<P>
If the close flag is set when an SSL BIO is freed then the internal SSL
structure is also freed using <CODE>SSL_free().</CODE>

</P>
<P>
<CODE>BIO_set_ssl()</CODE> sets the internal SSL pointer of BIO <STRONG>b</STRONG> to <STRONG>ssl</STRONG> using the close flag <STRONG>c</STRONG>.

</P>
<P>
<CODE>BIO_get_ssl()</CODE> retrieves the SSL pointer of BIO <STRONG>b</STRONG>, it can then be manipulated using the standard SSL library functions.

</P>
<P>
<CODE>BIO_set_ssl_mode()</CODE> sets the SSL BIO mode to <STRONG>client</STRONG>. If <STRONG>client</STRONG>
is 1 client mode is set. If <STRONG>client</STRONG> is 0 server mode is set.

</P>
<P>
<CODE>BIO_set_ssl_renegotiate_bytes()</CODE> sets the renegotiate byte
count to <STRONG>num</STRONG>. When set after every <STRONG>num</STRONG> bytes of I/O (read and write) the SSL session is automatically
renegotiated. <STRONG>num</STRONG> must be at least 512 bytes.

</P>
<P>
<CODE>BIO_set_ssl_renegotiate_timeout()</CODE> sets the renegotiate timeout
to
<STRONG>seconds</STRONG>. When the renegotiate timeout elapses the session is automatically
renegotiated.

</P>
<P>
<CODE>BIO_get_num_renegotiates()</CODE> returns the total number of session
renegotiations due to I/O or timeout.

</P>
<P>
<CODE>BIO_new_ssl()</CODE> allocates an SSL BIO using SSL_CTX <STRONG>ctx</STRONG> and using client mode if <STRONG>client</STRONG> is non zero.

</P>
<P>
<CODE>BIO_new_ssl_connect()</CODE> creates a new BIO chain consisting of an
SSL BIO (using <STRONG>ctx</STRONG>) followed by a connect BIO.

</P>
<P>
<CODE>BIO_new_buffer_ssl_connect()</CODE> creates a new BIO chain
consisting of a buffering BIO, an SSL BIO (using <STRONG>ctx</STRONG>) and a connect BIO.

</P>
<P>
<CODE>BIO_ssl_copy_session_id()</CODE> copies an SSL session id between BIO
chains <STRONG>from</STRONG> and <STRONG>to</STRONG>. It does this by locating the SSL BIOs in each chain and calling
<CODE>SSL_copy_session_id()</CODE> on the internal SSL pointer.

</P>
<P>
<CODE>BIO_ssl_shutdown()</CODE> closes down an SSL connection on BIO chain <STRONG>bio</STRONG>. It does this by locating the SSL BIO in the chain and calling
<CODE>SSL_shutdown()</CODE> on its internal SSL pointer.

</P>
<P>
<CODE>BIO_do_handshake()</CODE> attempts to complete an SSL handshake on
the supplied BIO and establish the SSL connection. It returns 1 if the
connection was established successfully. A zero or negative value is
returned if the connection could not be established, the call
<CODE>BIO_should_retry()</CODE> should be used for non blocking connect
BIOs to determine if the call should be retried. If an SSL connection has
already been established this call has no effect.

</P>
<P>
<HR>
<H1><A NAME="NOTES">NOTES</A></H1>
<P>
SSL BIOs are exceptional in that if the underlying transport is non
blocking they can still request a retry in exceptional circumstances.
Specifically this will happen if a session renegotiation takes place during
a <CODE>BIO_read()</CODE> operation, one case where this happens is when
SGC or step up occurs.

</P>
<P>
In OpenSSL 0.9.6 and later the SSL flag SSL_AUTO_RETRY can be set to
disable this behaviour. That is when this flag is set an SSL BIO using a
blocking transport will never request a retry.

</P>
<P>
Since unknown <CODE>BIO_ctrl()</CODE> operations are sent through filter
BIOs the servers name and port can be set using <CODE>BIO_set_host()</CODE>
on the BIO returned by <CODE>BIO_new_ssl_connect()</CODE> without having to
locate the connect BIO first.

</P>
<P>
Applications do not have to call <CODE>BIO_do_handshake()</CODE> but may
wish to do so to separate the handshake process from other I/O processing.

</P>
<P>
<HR>
<H1><A NAME="RETURN_VALUES">RETURN VALUES</A></H1>
<P>
TBA

</P>
<P>
<HR>
<H1><A NAME="EXAMPLE">EXAMPLE</A></H1>
<P>
This SSL/TLS client example, attempts to retrieve a page from an SSL/TLS
web server. The I/O routines are identical to those of the unencrypted
example in <A HREF="../crypto/BIO_s_connect.html#">BIO_s_connect(3)</A>.

</P>
<PRE> BIO *sbio, *out;
 int len;
 char tmpbuf[1024];
 SSL_CTX *ctx;
 SSL *ssl;
</PRE>
<PRE> ERR_load_crypto_strings();
 ERR_load_SSL_strings();
 OpenSSL_add_all_algorithms();
</PRE>
<PRE> /* We would seed the PRNG here if the platform didn't
  * do it automatically
  */
</PRE>
<PRE> ctx = SSL_CTX_new(SSLv23_client_method());
</PRE>
<PRE> /* We'd normally set some stuff like the verify paths and
  * mode here because as things stand this will connect to
  * any server whose certificate is signed by any CA.
  */
</PRE>
<PRE> sbio = BIO_new_ssl_connect(ctx);
</PRE>
<PRE> BIO_get_ssl(sbio, &amp;ssl);
</PRE>
<PRE> if(!ssl) {
   fprintf(stderr, &quot;Can't locate SSL pointer\n&quot;);
   /* whatever ... */
 }
</PRE>
<PRE> /* Don't want any retries */
 SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
</PRE>
<PRE> /* We might want to do other things with ssl here */
</PRE>
<PRE> BIO_set_conn_hostname(sbio, &quot;localhost:https&quot;);
</PRE>
<PRE> out = BIO_new_fp(stdout, BIO_NOCLOSE);
 if(BIO_do_connect(sbio) &lt;= 0) {
        fprintf(stderr, &quot;Error connecting to server\n&quot;);
        ERR_print_errors_fp(stderr);
        /* whatever ... */
 }
</PRE>
<PRE> if(BIO_do_handshake(sbio) &lt;= 0) {
        fprintf(stderr, &quot;Error establishing SSL connection\n&quot;);
        ERR_print_errors_fp(stderr);
        /* whatever ... */
 }
</PRE>
<PRE> /* Could examine ssl here to get connection info */
</PRE>
<PRE> BIO_puts(sbio, &quot;GET / HTTP/1.0\n\n&quot;);
 for(;;) {      
        len = BIO_read(sbio, tmpbuf, 1024);
        if(len &lt;= 0) break;
        BIO_write(out, tmpbuf, len);
 }
 BIO_free_all(sbio);
 BIO_free(out);
</PRE>
<P>
Here is a simple server example. It makes use of a buffering BIO to allow
lines to be read from the SSL BIO using BIO_gets. It creates a pseudo web
page containing the actual request from a client and also echoes the
request to standard output.

</P>
<PRE> BIO *sbio, *bbio, *acpt, *out;
 int len;
 char tmpbuf[1024];
 SSL_CTX *ctx;
 SSL *ssl;
</PRE>
<PRE> ERR_load_crypto_strings();
 ERR_load_SSL_strings();
 OpenSSL_add_all_algorithms();
</PRE>
<PRE> /* Might seed PRNG here */
</PRE>
<PRE> ctx = SSL_CTX_new(SSLv23_server_method());
</PRE>
<PRE> if (!SSL_CTX_use_certificate_file(ctx,&quot;server.pem&quot;,SSL_FILETYPE_PEM)
        || !SSL_CTX_use_PrivateKey_file(ctx,&quot;server.pem&quot;,SSL_FILETYPE_PEM)
        || !SSL_CTX_check_private_key(ctx)) {
</PRE>
<PRE>        fprintf(stderr, &quot;Error setting up SSL_CTX\n&quot;);
        ERR_print_errors_fp(stderr);
        return 0;
 }
</PRE>
<PRE> /* Might do other things here like setting verify locations and
  * DH and/or RSA temporary key callbacks
  */
</PRE>
<PRE> /* New SSL BIO setup as server */
 sbio=BIO_new_ssl(ctx,0);
</PRE>
<PRE> BIO_get_ssl(sbio, &amp;ssl);
</PRE>
<PRE> if(!ssl) {
   fprintf(stderr, &quot;Can't locate SSL pointer\n&quot;);
   /* whatever ... */
 }
</PRE>
<PRE> /* Don't want any retries */
 SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
</PRE>
<PRE> /* Create the buffering BIO */
</PRE>
<PRE> bbio = BIO_new(BIO_f_buffer());
</PRE>
<PRE> /* Add to chain */
 sbio = BIO_push(bbio, sbio);
</PRE>
<PRE> acpt=BIO_new_accept(&quot;4433&quot;);
</PRE>
<PRE> /* By doing this when a new connection is established
  * we automatically have sbio inserted into it. The
  * BIO chain is now 'swallowed' by the accept BIO and
  * will be freed when the accept BIO is freed. 
  */
 
 BIO_set_accept_bios(acpt,sbio);
</PRE>
<PRE> out = BIO_new_fp(stdout, BIO_NOCLOSE);
</PRE>
<PRE> /* Setup accept BIO */
 if(BIO_do_accept(acpt) &lt;= 0) {
        fprintf(stderr, &quot;Error setting up accept BIO\n&quot;);
        ERR_print_errors_fp(stderr);
        return 0;
 }
</PRE>
<PRE> /* Now wait for incoming connection */
 if(BIO_do_accept(acpt) &lt;= 0) {
        fprintf(stderr, &quot;Error in connection\n&quot;);
        ERR_print_errors_fp(stderr);
        return 0;
 }
</PRE>
<PRE> /* We only want one connection so remove and free
  * accept BIO
  */
</PRE>
<PRE> sbio = BIO_pop(acpt);
</PRE>
<PRE> BIO_free_all(acpt);
</PRE>
<PRE> if(BIO_do_handshake(sbio) &lt;= 0) {
        fprintf(stderr, &quot;Error in SSL handshake\n&quot;);
        ERR_print_errors_fp(stderr);
        return 0;
 }
</PRE>
<PRE> BIO_puts(sbio, &quot;HTTP/1.0 200 OK\r\nContent-type: text/plain\r\n\r\n&quot;);
 BIO_puts(sbio, &quot;\r\nConnection Established\r\nRequest headers:\r\n&quot;);
 BIO_puts(sbio, &quot;--------------------------------------------------\r\n&quot;);
</PRE>
<PRE> for(;;) {
        len = BIO_gets(sbio, tmpbuf, 1024);
        if(len &lt;= 0) break;
        BIO_write(sbio, tmpbuf, len);
        BIO_write(out, tmpbuf, len);
        /* Look for blank line signifying end of headers*/
        if((tmpbuf[0] == '\r') || (tmpbuf[0] == '\n')) break;
 }
</PRE>
<PRE> BIO_puts(sbio, &quot;--------------------------------------------------\r\n&quot;);
 BIO_puts(sbio, &quot;\r\n&quot;);
</PRE>
<PRE> /* Since there is a buffering BIO present we had better flush it */
 BIO_flush(sbio);
</PRE>
<PRE> BIO_free_all(sbio);
</PRE>
<P>
<HR>
<H1><A NAME="BUGS">BUGS</A></H1>
<P>
In OpenSSL versions before 1.0.0 the <CODE>BIO_pop()</CODE> call was
handled incorrectly, the I/O BIO reference count was incorrectly
incremented (instead of decremented) and dissociated with the SSL BIO even
if the SSL BIO was not explicitly being popped (e.g. a pop higher up the
chain). Applications which included workarounds for this bug (e.g. freeing
BIOs more than once) should be modified to handle this fix or they may free
up an already freed BIO.

</P>
<P>
<HR>
<H1><A NAME="SEE_ALSO">SEE ALSO</A></H1>
<P>
TBA
</P>
:}


