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