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