]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - secure/lib/libcrypto/man/BIO_f_ssl.3
MFC: r325328
[FreeBSD/FreeBSD.git] / secure / lib / libcrypto / man / BIO_f_ssl.3
1 .\" Automatically generated by Pod::Man 4.07 (Pod::Simple 3.35)
2 .\"
3 .\" Standard preamble:
4 .\" ========================================================================
5 .de Sp \" Vertical space (when we can't use .PP)
6 .if t .sp .5v
7 .if n .sp
8 ..
9 .de Vb \" Begin verbatim text
10 .ft CW
11 .nf
12 .ne \\$1
13 ..
14 .de Ve \" End verbatim text
15 .ft R
16 .fi
17 ..
18 .\" Set up some character translations and predefined strings.  \*(-- will
19 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
20 .\" double quote, and \*(R" will give a right double quote.  \*(C+ will
21 .\" give a nicer C++.  Capital omega is used to do unbreakable dashes and
22 .\" therefore won't be available.  \*(C` and \*(C' expand to `' in nroff,
23 .\" nothing in troff, for use with C<>.
24 .tr \(*W-
25 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
26 .ie n \{\
27 .    ds -- \(*W-
28 .    ds PI pi
29 .    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
30 .    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
31 .    ds L" ""
32 .    ds R" ""
33 .    ds C` ""
34 .    ds C' ""
35 'br\}
36 .el\{\
37 .    ds -- \|\(em\|
38 .    ds PI \(*p
39 .    ds L" ``
40 .    ds R" ''
41 .    ds C`
42 .    ds C'
43 'br\}
44 .\"
45 .\" Escape single quotes in literal strings from groff's Unicode transform.
46 .ie \n(.g .ds Aq \(aq
47 .el       .ds Aq '
48 .\"
49 .\" If the F register is >0, we'll generate index entries on stderr for
50 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
51 .\" entries marked with X<> in POD.  Of course, you'll have to process the
52 .\" output yourself in some meaningful fashion.
53 .\"
54 .\" Avoid warning from groff about undefined register 'F'.
55 .de IX
56 ..
57 .if !\nF .nr F 0
58 .if \nF>0 \{\
59 .    de IX
60 .    tm Index:\\$1\t\\n%\t"\\$2"
61 ..
62 .    if !\nF==2 \{\
63 .        nr % 0
64 .        nr F 2
65 .    \}
66 .\}
67 .\"
68 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69 .\" Fear.  Run.  Save yourself.  No user-serviceable parts.
70 .    \" fudge factors for nroff and troff
71 .if n \{\
72 .    ds #H 0
73 .    ds #V .8m
74 .    ds #F .3m
75 .    ds #[ \f1
76 .    ds #] \fP
77 .\}
78 .if t \{\
79 .    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80 .    ds #V .6m
81 .    ds #F 0
82 .    ds #[ \&
83 .    ds #] \&
84 .\}
85 .    \" simple accents for nroff and troff
86 .if n \{\
87 .    ds ' \&
88 .    ds ` \&
89 .    ds ^ \&
90 .    ds , \&
91 .    ds ~ ~
92 .    ds /
93 .\}
94 .if t \{\
95 .    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96 .    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97 .    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98 .    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99 .    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100 .    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101 .\}
102 .    \" troff and (daisy-wheel) nroff accents
103 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110 .ds ae a\h'-(\w'a'u*4/10)'e
111 .ds Ae A\h'-(\w'A'u*4/10)'E
112 .    \" corrections for vroff
113 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115 .    \" for low resolution devices (crt and lpr)
116 .if \n(.H>23 .if \n(.V>19 \
117 \{\
118 .    ds : e
119 .    ds 8 ss
120 .    ds o a
121 .    ds d- d\h'-1'\(ga
122 .    ds D- D\h'-1'\(hy
123 .    ds th \o'bp'
124 .    ds Th \o'LP'
125 .    ds ae ae
126 .    ds Ae AE
127 .\}
128 .rm #[ #] #H #V #F C
129 .\" ========================================================================
130 .\"
131 .IX Title "BIO_f_ssl 3"
132 .TH BIO_f_ssl 3 "2017-11-02" "1.0.2m" "OpenSSL"
133 .\" For nroff, turn off justification.  Always turn off hyphenation; it makes
134 .\" way too many mistakes in technical documents.
135 .if n .ad l
136 .nh
137 .SH "NAME"
138 BIO_f_ssl, BIO_set_ssl, BIO_get_ssl, BIO_set_ssl_mode, BIO_set_ssl_renegotiate_bytes,
139 BIO_get_num_renegotiates, BIO_set_ssl_renegotiate_timeout, BIO_new_ssl,
140 BIO_new_ssl_connect, BIO_new_buffer_ssl_connect, BIO_ssl_copy_session_id,
141 BIO_ssl_shutdown \- SSL BIO
142 .SH "SYNOPSIS"
143 .IX Header "SYNOPSIS"
144 .Vb 2
145 \& #include <openssl/bio.h>
146 \& #include <openssl/ssl.h>
147 \&
148 \& BIO_METHOD *BIO_f_ssl(void);
149 \&
150 \& #define BIO_set_ssl(b,ssl,c)   BIO_ctrl(b,BIO_C_SET_SSL,c,(char *)ssl)
151 \& #define BIO_get_ssl(b,sslp)    BIO_ctrl(b,BIO_C_GET_SSL,0,(char *)sslp)
152 \& #define BIO_set_ssl_mode(b,client)     BIO_ctrl(b,BIO_C_SSL_MODE,client,NULL)
153 \& #define BIO_set_ssl_renegotiate_bytes(b,num) \e
154 \&        BIO_ctrl(b,BIO_C_SET_SSL_RENEGOTIATE_BYTES,num,NULL);
155 \& #define BIO_set_ssl_renegotiate_timeout(b,seconds) \e
156 \&        BIO_ctrl(b,BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT,seconds,NULL);
157 \& #define BIO_get_num_renegotiates(b) \e
158 \&        BIO_ctrl(b,BIO_C_SET_SSL_NUM_RENEGOTIATES,0,NULL);
159 \&
160 \& BIO *BIO_new_ssl(SSL_CTX *ctx,int client);
161 \& BIO *BIO_new_ssl_connect(SSL_CTX *ctx);
162 \& BIO *BIO_new_buffer_ssl_connect(SSL_CTX *ctx);
163 \& int BIO_ssl_copy_session_id(BIO *to,BIO *from);
164 \& void BIO_ssl_shutdown(BIO *bio);
165 \&
166 \& #define BIO_do_handshake(b)    BIO_ctrl(b,BIO_C_DO_STATE_MACHINE,0,NULL)
167 .Ve
168 .SH "DESCRIPTION"
169 .IX Header "DESCRIPTION"
170 \&\fIBIO_f_ssl()\fR returns the \s-1SSL BIO\s0 method. This is a filter \s-1BIO\s0 which
171 is a wrapper round the OpenSSL \s-1SSL\s0 routines adding a \s-1BIO \s0\*(L"flavour\*(R" to
172 \&\s-1SSL I/O. \s0
173 .PP
174 I/O performed on an \s-1SSL BIO\s0 communicates using the \s-1SSL\s0 protocol with
175 the SSLs read and write BIOs. If an \s-1SSL\s0 connection is not established
176 then an attempt is made to establish one on the first I/O call.
177 .PP
178 If a \s-1BIO\s0 is appended to an \s-1SSL BIO\s0 using \fIBIO_push()\fR it is automatically
179 used as the \s-1SSL\s0 BIOs read and write BIOs.
180 .PP
181 Calling \fIBIO_reset()\fR on an \s-1SSL BIO\s0 closes down any current \s-1SSL\s0 connection
182 by calling \fISSL_shutdown()\fR. \fIBIO_reset()\fR is then sent to the next \s-1BIO\s0 in
183 the chain: this will typically disconnect the underlying transport.
184 The \s-1SSL BIO\s0 is then reset to the initial accept or connect state.
185 .PP
186 If the close flag is set when an \s-1SSL BIO\s0 is freed then the internal
187 \&\s-1SSL\s0 structure is also freed using \fISSL_free()\fR.
188 .PP
189 \&\fIBIO_set_ssl()\fR sets the internal \s-1SSL\s0 pointer of \s-1BIO \s0\fBb\fR to \fBssl\fR using
190 the close flag \fBc\fR.
191 .PP
192 \&\fIBIO_get_ssl()\fR retrieves the \s-1SSL\s0 pointer of \s-1BIO \s0\fBb\fR, it can then be
193 manipulated using the standard \s-1SSL\s0 library functions.
194 .PP
195 \&\fIBIO_set_ssl_mode()\fR sets the \s-1SSL BIO\s0 mode to \fBclient\fR. If \fBclient\fR
196 is 1 client mode is set. If \fBclient\fR is 0 server mode is set.
197 .PP
198 \&\fIBIO_set_ssl_renegotiate_bytes()\fR sets the renegotiate byte count
199 to \fBnum\fR. When set after every \fBnum\fR bytes of I/O (read and write) 
200 the \s-1SSL\s0 session is automatically renegotiated. \fBnum\fR must be at
201 least 512 bytes.
202 .PP
203 \&\fIBIO_set_ssl_renegotiate_timeout()\fR sets the renegotiate timeout to
204 \&\fBseconds\fR. When the renegotiate timeout elapses the session is
205 automatically renegotiated.
206 .PP
207 \&\fIBIO_get_num_renegotiates()\fR returns the total number of session
208 renegotiations due to I/O or timeout.
209 .PP
210 \&\fIBIO_new_ssl()\fR allocates an \s-1SSL BIO\s0 using \s-1SSL_CTX \s0\fBctx\fR and using
211 client mode if \fBclient\fR is non zero.
212 .PP
213 \&\fIBIO_new_ssl_connect()\fR creates a new \s-1BIO\s0 chain consisting of an
214 \&\s-1SSL BIO \s0(using \fBctx\fR) followed by a connect \s-1BIO.\s0
215 .PP
216 \&\fIBIO_new_buffer_ssl_connect()\fR creates a new \s-1BIO\s0 chain consisting
217 of a buffering \s-1BIO,\s0 an \s-1SSL BIO \s0(using \fBctx\fR) and a connect
218 \&\s-1BIO.\s0
219 .PP
220 \&\fIBIO_ssl_copy_session_id()\fR copies an \s-1SSL\s0 session id between 
221 \&\s-1BIO\s0 chains \fBfrom\fR and \fBto\fR. It does this by locating the
222 \&\s-1SSL\s0 BIOs in each chain and calling \fISSL_copy_session_id()\fR on
223 the internal \s-1SSL\s0 pointer.
224 .PP
225 \&\fIBIO_ssl_shutdown()\fR closes down an \s-1SSL\s0 connection on \s-1BIO\s0
226 chain \fBbio\fR. It does this by locating the \s-1SSL BIO\s0 in the
227 chain and calling \fISSL_shutdown()\fR on its internal \s-1SSL\s0
228 pointer.
229 .PP
230 \&\fIBIO_do_handshake()\fR attempts to complete an \s-1SSL\s0 handshake on the
231 supplied \s-1BIO\s0 and establish the \s-1SSL\s0 connection. It returns 1
232 if the connection was established successfully. A zero or negative
233 value is returned if the connection could not be established, the
234 call \fIBIO_should_retry()\fR should be used for non blocking connect BIOs
235 to determine if the call should be retried. If an \s-1SSL\s0 connection has
236 already been established this call has no effect.
237 .SH "NOTES"
238 .IX Header "NOTES"
239 \&\s-1SSL\s0 BIOs are exceptional in that if the underlying transport
240 is non blocking they can still request a retry in exceptional
241 circumstances. Specifically this will happen if a session
242 renegotiation takes place during a \fIBIO_read()\fR operation, one
243 case where this happens is when step up occurs.
244 .PP
245 In OpenSSL 0.9.6 and later the \s-1SSL\s0 flag \s-1SSL_AUTO_RETRY\s0 can be
246 set to disable this behaviour. That is when this flag is set
247 an \s-1SSL BIO\s0 using a blocking transport will never request a
248 retry.
249 .PP
250 Since unknown \fIBIO_ctrl()\fR operations are sent through filter
251 BIOs the servers name and port can be set using \fIBIO_set_host()\fR
252 on the \s-1BIO\s0 returned by \fIBIO_new_ssl_connect()\fR without having
253 to locate the connect \s-1BIO\s0 first.
254 .PP
255 Applications do not have to call \fIBIO_do_handshake()\fR but may wish
256 to do so to separate the handshake process from other I/O
257 processing.
258 .SH "RETURN VALUES"
259 .IX Header "RETURN VALUES"
260 \&\s-1TBA\s0
261 .SH "EXAMPLE"
262 .IX Header "EXAMPLE"
263 This \s-1SSL/TLS\s0 client example, attempts to retrieve a page from an
264 \&\s-1SSL/TLS\s0 web server. The I/O routines are identical to those of the
265 unencrypted example in \fIBIO_s_connect\fR\|(3).
266 .PP
267 .Vb 5
268 \& BIO *sbio, *out;
269 \& int len;
270 \& char tmpbuf[1024];
271 \& SSL_CTX *ctx;
272 \& SSL *ssl;
273 \&
274 \& ERR_load_crypto_strings();
275 \& ERR_load_SSL_strings();
276 \& OpenSSL_add_all_algorithms();
277 \&
278 \& /* We would seed the PRNG here if the platform didn\*(Aqt
279 \&  * do it automatically
280 \&  */
281 \&
282 \& ctx = SSL_CTX_new(SSLv23_client_method());
283 \&
284 \& /* We\*(Aqd normally set some stuff like the verify paths and
285 \&  * mode here because as things stand this will connect to
286 \&  * any server whose certificate is signed by any CA.
287 \&  */
288 \&
289 \& sbio = BIO_new_ssl_connect(ctx);
290 \&
291 \& BIO_get_ssl(sbio, &ssl);
292 \&
293 \& if(!ssl) {
294 \&   fprintf(stderr, "Can\*(Aqt locate SSL pointer\en");
295 \&   /* whatever ... */
296 \& }
297 \&
298 \& /* Don\*(Aqt want any retries */
299 \& SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
300 \&
301 \& /* We might want to do other things with ssl here */
302 \&
303 \& BIO_set_conn_hostname(sbio, "localhost:https");
304 \&
305 \& out = BIO_new_fp(stdout, BIO_NOCLOSE);
306 \& if(BIO_do_connect(sbio) <= 0) {
307 \&        fprintf(stderr, "Error connecting to server\en");
308 \&        ERR_print_errors_fp(stderr);
309 \&        /* whatever ... */
310 \& }
311 \&
312 \& if(BIO_do_handshake(sbio) <= 0) {
313 \&        fprintf(stderr, "Error establishing SSL connection\en");
314 \&        ERR_print_errors_fp(stderr);
315 \&        /* whatever ... */
316 \& }
317 \&
318 \& /* Could examine ssl here to get connection info */
319 \&
320 \& BIO_puts(sbio, "GET / HTTP/1.0\en\en");
321 \& for(;;) {      
322 \&        len = BIO_read(sbio, tmpbuf, 1024);
323 \&        if(len <= 0) break;
324 \&        BIO_write(out, tmpbuf, len);
325 \& }
326 \& BIO_free_all(sbio);
327 \& BIO_free(out);
328 .Ve
329 .PP
330 Here is a simple server example. It makes use of a buffering
331 \&\s-1BIO\s0 to allow lines to be read from the \s-1SSL BIO\s0 using BIO_gets.
332 It creates a pseudo web page containing the actual request from
333 a client and also echoes the request to standard output.
334 .PP
335 .Vb 5
336 \& BIO *sbio, *bbio, *acpt, *out;
337 \& int len;
338 \& char tmpbuf[1024];
339 \& SSL_CTX *ctx;
340 \& SSL *ssl;
341 \&
342 \& ERR_load_crypto_strings();
343 \& ERR_load_SSL_strings();
344 \& OpenSSL_add_all_algorithms();
345 \&
346 \& /* Might seed PRNG here */
347 \&
348 \& ctx = SSL_CTX_new(SSLv23_server_method());
349 \&
350 \& if (!SSL_CTX_use_certificate_file(ctx,"server.pem",SSL_FILETYPE_PEM)
351 \&        || !SSL_CTX_use_PrivateKey_file(ctx,"server.pem",SSL_FILETYPE_PEM)
352 \&        || !SSL_CTX_check_private_key(ctx)) {
353 \&
354 \&        fprintf(stderr, "Error setting up SSL_CTX\en");
355 \&        ERR_print_errors_fp(stderr);
356 \&        return 0;
357 \& }
358 \&
359 \& /* Might do other things here like setting verify locations and
360 \&  * DH and/or RSA temporary key callbacks
361 \&  */
362 \&
363 \& /* New SSL BIO setup as server */
364 \& sbio=BIO_new_ssl(ctx,0);
365 \&
366 \& BIO_get_ssl(sbio, &ssl);
367 \&
368 \& if(!ssl) {
369 \&   fprintf(stderr, "Can\*(Aqt locate SSL pointer\en");
370 \&   /* whatever ... */
371 \& }
372 \&
373 \& /* Don\*(Aqt want any retries */
374 \& SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
375 \&
376 \& /* Create the buffering BIO */
377 \&
378 \& bbio = BIO_new(BIO_f_buffer());
379 \&
380 \& /* Add to chain */
381 \& sbio = BIO_push(bbio, sbio);
382 \&
383 \& acpt=BIO_new_accept("4433");
384 \&
385 \& /* By doing this when a new connection is established
386 \&  * we automatically have sbio inserted into it. The
387 \&  * BIO chain is now \*(Aqswallowed\*(Aq by the accept BIO and
388 \&  * will be freed when the accept BIO is freed. 
389 \&  */
390 \& 
391 \& BIO_set_accept_bios(acpt,sbio);
392 \&
393 \& out = BIO_new_fp(stdout, BIO_NOCLOSE);
394 \&
395 \& /* Setup accept BIO */
396 \& if(BIO_do_accept(acpt) <= 0) {
397 \&        fprintf(stderr, "Error setting up accept BIO\en");
398 \&        ERR_print_errors_fp(stderr);
399 \&        return 0;
400 \& }
401 \&
402 \& /* Now wait for incoming connection */
403 \& if(BIO_do_accept(acpt) <= 0) {
404 \&        fprintf(stderr, "Error in connection\en");
405 \&        ERR_print_errors_fp(stderr);
406 \&        return 0;
407 \& }
408 \&
409 \& /* We only want one connection so remove and free
410 \&  * accept BIO
411 \&  */
412 \&
413 \& sbio = BIO_pop(acpt);
414 \&
415 \& BIO_free_all(acpt);
416 \&
417 \& if(BIO_do_handshake(sbio) <= 0) {
418 \&        fprintf(stderr, "Error in SSL handshake\en");
419 \&        ERR_print_errors_fp(stderr);
420 \&        return 0;
421 \& }
422 \&
423 \& BIO_puts(sbio, "HTTP/1.0 200 OK\er\enContent\-type: text/plain\er\en\er\en");
424 \& BIO_puts(sbio, "\er\enConnection Established\er\enRequest headers:\er\en");
425 \& BIO_puts(sbio, "\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\er\en");
426 \&
427 \& for(;;) {
428 \&        len = BIO_gets(sbio, tmpbuf, 1024);
429 \&        if(len <= 0) break;
430 \&        BIO_write(sbio, tmpbuf, len);
431 \&        BIO_write(out, tmpbuf, len);
432 \&        /* Look for blank line signifying end of headers*/
433 \&        if((tmpbuf[0] == \*(Aq\er\*(Aq) || (tmpbuf[0] == \*(Aq\en\*(Aq)) break;
434 \& }
435 \&
436 \& BIO_puts(sbio, "\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\er\en");
437 \& BIO_puts(sbio, "\er\en");
438 \&
439 \& /* Since there is a buffering BIO present we had better flush it */
440 \& BIO_flush(sbio);
441 \&
442 \& BIO_free_all(sbio);
443 .Ve
444 .SH "BUGS"
445 .IX Header "BUGS"
446 In OpenSSL versions before 1.0.0 the \fIBIO_pop()\fR call was handled incorrectly,
447 the I/O \s-1BIO\s0 reference count was incorrectly incremented (instead of
448 decremented) and dissociated with the \s-1SSL BIO\s0 even if the \s-1SSL BIO\s0 was not
449 explicitly being popped (e.g. a pop higher up the chain). Applications which
450 included workarounds for this bug (e.g. freeing BIOs more than once) should
451 be modified to handle this fix or they may free up an already freed \s-1BIO.\s0
452 .SH "SEE ALSO"
453 .IX Header "SEE ALSO"
454 \&\s-1TBA\s0