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