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