xref: /onnv-gate/usr/src/common/openssl/doc/crypto/BIO_f_ssl.pod (revision 2175:b0b2f052a486)
1*2175Sjp161948=pod
2*2175Sjp161948
3*2175Sjp161948=head1 NAME
4*2175Sjp161948
5*2175Sjp161948BIO_f_ssl, BIO_set_ssl, BIO_get_ssl, BIO_set_ssl_mode, BIO_set_ssl_renegotiate_bytes,
6*2175Sjp161948BIO_get_num_renegotiates, BIO_set_ssl_renegotiate_timeout, BIO_new_ssl,
7*2175Sjp161948BIO_new_ssl_connect, BIO_new_buffer_ssl_connect, BIO_ssl_copy_session_id,
8*2175Sjp161948BIO_ssl_shutdown - SSL BIO
9*2175Sjp161948
10*2175Sjp161948=head1 SYNOPSIS
11*2175Sjp161948
12*2175Sjp161948 #include <openssl/bio.h>
13*2175Sjp161948 #include <openssl/ssl.h>
14*2175Sjp161948
15*2175Sjp161948 BIO_METHOD *BIO_f_ssl(void);
16*2175Sjp161948
17*2175Sjp161948 #define BIO_set_ssl(b,ssl,c)	BIO_ctrl(b,BIO_C_SET_SSL,c,(char *)ssl)
18*2175Sjp161948 #define BIO_get_ssl(b,sslp)	BIO_ctrl(b,BIO_C_GET_SSL,0,(char *)sslp)
19*2175Sjp161948 #define BIO_set_ssl_mode(b,client)	BIO_ctrl(b,BIO_C_SSL_MODE,client,NULL)
20*2175Sjp161948 #define BIO_set_ssl_renegotiate_bytes(b,num) \
21*2175Sjp161948	BIO_ctrl(b,BIO_C_SET_SSL_RENEGOTIATE_BYTES,num,NULL);
22*2175Sjp161948 #define BIO_set_ssl_renegotiate_timeout(b,seconds) \
23*2175Sjp161948	BIO_ctrl(b,BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT,seconds,NULL);
24*2175Sjp161948 #define BIO_get_num_renegotiates(b) \
25*2175Sjp161948	BIO_ctrl(b,BIO_C_SET_SSL_NUM_RENEGOTIATES,0,NULL);
26*2175Sjp161948
27*2175Sjp161948 BIO *BIO_new_ssl(SSL_CTX *ctx,int client);
28*2175Sjp161948 BIO *BIO_new_ssl_connect(SSL_CTX *ctx);
29*2175Sjp161948 BIO *BIO_new_buffer_ssl_connect(SSL_CTX *ctx);
30*2175Sjp161948 int BIO_ssl_copy_session_id(BIO *to,BIO *from);
31*2175Sjp161948 void BIO_ssl_shutdown(BIO *bio);
32*2175Sjp161948
33*2175Sjp161948 #define BIO_do_handshake(b)	BIO_ctrl(b,BIO_C_DO_STATE_MACHINE,0,NULL)
34*2175Sjp161948
35*2175Sjp161948=head1 DESCRIPTION
36*2175Sjp161948
37*2175Sjp161948BIO_f_ssl() returns the SSL BIO method. This is a filter BIO which
38*2175Sjp161948is a wrapper round the OpenSSL SSL routines adding a BIO "flavour" to
39*2175Sjp161948SSL I/O.
40*2175Sjp161948
41*2175Sjp161948I/O performed on an SSL BIO communicates using the SSL protocol with
42*2175Sjp161948the SSLs read and write BIOs. If an SSL connection is not established
43*2175Sjp161948then an attempt is made to establish one on the first I/O call.
44*2175Sjp161948
45*2175Sjp161948If a BIO is appended to an SSL BIO using BIO_push() it is automatically
46*2175Sjp161948used as the SSL BIOs read and write BIOs.
47*2175Sjp161948
48*2175Sjp161948Calling BIO_reset() on an SSL BIO closes down any current SSL connection
49*2175Sjp161948by calling SSL_shutdown(). BIO_reset() is then sent to the next BIO in
50*2175Sjp161948the chain: this will typically disconnect the underlying transport.
51*2175Sjp161948The SSL BIO is then reset to the initial accept or connect state.
52*2175Sjp161948
53*2175Sjp161948If the close flag is set when an SSL BIO is freed then the internal
54*2175Sjp161948SSL structure is also freed using SSL_free().
55*2175Sjp161948
56*2175Sjp161948BIO_set_ssl() sets the internal SSL pointer of BIO B<b> to B<ssl> using
57*2175Sjp161948the close flag B<c>.
58*2175Sjp161948
59*2175Sjp161948BIO_get_ssl() retrieves the SSL pointer of BIO B<b>, it can then be
60*2175Sjp161948manipulated using the standard SSL library functions.
61*2175Sjp161948
62*2175Sjp161948BIO_set_ssl_mode() sets the SSL BIO mode to B<client>. If B<client>
63*2175Sjp161948is 1 client mode is set. If B<client> is 0 server mode is set.
64*2175Sjp161948
65*2175Sjp161948BIO_set_ssl_renegotiate_bytes() sets the renegotiate byte count
66*2175Sjp161948to B<num>. When set after every B<num> bytes of I/O (read and write)
67*2175Sjp161948the SSL session is automatically renegotiated. B<num> must be at
68*2175Sjp161948least 512 bytes.
69*2175Sjp161948
70*2175Sjp161948BIO_set_ssl_renegotiate_timeout() sets the renegotiate timeout to
71*2175Sjp161948B<seconds>. When the renegotiate timeout elapses the session is
72*2175Sjp161948automatically renegotiated.
73*2175Sjp161948
74*2175Sjp161948BIO_get_num_renegotiates() returns the total number of session
75*2175Sjp161948renegotiations due to I/O or timeout.
76*2175Sjp161948
77*2175Sjp161948BIO_new_ssl() allocates an SSL BIO using SSL_CTX B<ctx> and using
78*2175Sjp161948client mode if B<client> is non zero.
79*2175Sjp161948
80*2175Sjp161948BIO_new_ssl_connect() creates a new BIO chain consisting of an
81*2175Sjp161948SSL BIO (using B<ctx>) followed by a connect BIO.
82*2175Sjp161948
83*2175Sjp161948BIO_new_buffer_ssl_connect() creates a new BIO chain consisting
84*2175Sjp161948of a buffering BIO, an SSL BIO (using B<ctx>) and a connect
85*2175Sjp161948BIO.
86*2175Sjp161948
87*2175Sjp161948BIO_ssl_copy_session_id() copies an SSL session id between
88*2175Sjp161948BIO chains B<from> and B<to>. It does this by locating the
89*2175Sjp161948SSL BIOs in each chain and calling SSL_copy_session_id() on
90*2175Sjp161948the internal SSL pointer.
91*2175Sjp161948
92*2175Sjp161948BIO_ssl_shutdown() closes down an SSL connection on BIO
93*2175Sjp161948chain B<bio>. It does this by locating the SSL BIO in the
94*2175Sjp161948chain and calling SSL_shutdown() on its internal SSL
95*2175Sjp161948pointer.
96*2175Sjp161948
97*2175Sjp161948BIO_do_handshake() attempts to complete an SSL handshake on the
98*2175Sjp161948supplied BIO and establish the SSL connection. It returns 1
99*2175Sjp161948if the connection was established successfully. A zero or negative
100*2175Sjp161948value is returned if the connection could not be established, the
101*2175Sjp161948call BIO_should_retry() should be used for non blocking connect BIOs
102*2175Sjp161948to determine if the call should be retried. If an SSL connection has
103*2175Sjp161948already been established this call has no effect.
104*2175Sjp161948
105*2175Sjp161948=head1 NOTES
106*2175Sjp161948
107*2175Sjp161948SSL BIOs are exceptional in that if the underlying transport
108*2175Sjp161948is non blocking they can still request a retry in exceptional
109*2175Sjp161948circumstances. Specifically this will happen if a session
110*2175Sjp161948renegotiation takes place during a BIO_read() operation, one
111*2175Sjp161948case where this happens is when SGC or step up occurs.
112*2175Sjp161948
113*2175Sjp161948In OpenSSL 0.9.6 and later the SSL flag SSL_AUTO_RETRY can be
114*2175Sjp161948set to disable this behaviour. That is when this flag is set
115*2175Sjp161948an SSL BIO using a blocking transport will never request a
116*2175Sjp161948retry.
117*2175Sjp161948
118*2175Sjp161948Since unknown BIO_ctrl() operations are sent through filter
119*2175Sjp161948BIOs the servers name and port can be set using BIO_set_host()
120*2175Sjp161948on the BIO returned by BIO_new_ssl_connect() without having
121*2175Sjp161948to locate the connect BIO first.
122*2175Sjp161948
123*2175Sjp161948Applications do not have to call BIO_do_handshake() but may wish
124*2175Sjp161948to do so to separate the handshake process from other I/O
125*2175Sjp161948processing.
126*2175Sjp161948
127*2175Sjp161948=head1 RETURN VALUES
128*2175Sjp161948
129*2175Sjp161948TBA
130*2175Sjp161948
131*2175Sjp161948=head1 EXAMPLE
132*2175Sjp161948
133*2175Sjp161948This SSL/TLS client example, attempts to retrieve a page from an
134*2175Sjp161948SSL/TLS web server. The I/O routines are identical to those of the
135*2175Sjp161948unencrypted example in L<BIO_s_connect(3)|BIO_s_connect(3)>.
136*2175Sjp161948
137*2175Sjp161948 BIO *sbio, *out;
138*2175Sjp161948 int len;
139*2175Sjp161948 char tmpbuf[1024];
140*2175Sjp161948 SSL_CTX *ctx;
141*2175Sjp161948 SSL *ssl;
142*2175Sjp161948
143*2175Sjp161948 ERR_load_crypto_strings();
144*2175Sjp161948 ERR_load_SSL_strings();
145*2175Sjp161948 OpenSSL_add_all_algorithms();
146*2175Sjp161948
147*2175Sjp161948 /* We would seed the PRNG here if the platform didn't
148*2175Sjp161948  * do it automatically
149*2175Sjp161948  */
150*2175Sjp161948
151*2175Sjp161948 ctx = SSL_CTX_new(SSLv23_client_method());
152*2175Sjp161948
153*2175Sjp161948 /* We'd normally set some stuff like the verify paths and
154*2175Sjp161948  * mode here because as things stand this will connect to
155*2175Sjp161948  * any server whose certificate is signed by any CA.
156*2175Sjp161948  */
157*2175Sjp161948
158*2175Sjp161948 sbio = BIO_new_ssl_connect(ctx);
159*2175Sjp161948
160*2175Sjp161948 BIO_get_ssl(sbio, &ssl);
161*2175Sjp161948
162*2175Sjp161948 if(!ssl) {
163*2175Sjp161948   fprintf(stderr, "Can't locate SSL pointer\n");
164*2175Sjp161948   /* whatever ... */
165*2175Sjp161948 }
166*2175Sjp161948
167*2175Sjp161948 /* Don't want any retries */
168*2175Sjp161948 SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
169*2175Sjp161948
170*2175Sjp161948 /* We might want to do other things with ssl here */
171*2175Sjp161948
172*2175Sjp161948 BIO_set_conn_hostname(sbio, "localhost:https");
173*2175Sjp161948
174*2175Sjp161948 out = BIO_new_fp(stdout, BIO_NOCLOSE);
175*2175Sjp161948 if(BIO_do_connect(sbio) <= 0) {
176*2175Sjp161948	fprintf(stderr, "Error connecting to server\n");
177*2175Sjp161948	ERR_print_errors_fp(stderr);
178*2175Sjp161948	/* whatever ... */
179*2175Sjp161948 }
180*2175Sjp161948
181*2175Sjp161948 if(BIO_do_handshake(sbio) <= 0) {
182*2175Sjp161948	fprintf(stderr, "Error establishing SSL connection\n");
183*2175Sjp161948	ERR_print_errors_fp(stderr);
184*2175Sjp161948	/* whatever ... */
185*2175Sjp161948 }
186*2175Sjp161948
187*2175Sjp161948 /* Could examine ssl here to get connection info */
188*2175Sjp161948
189*2175Sjp161948 BIO_puts(sbio, "GET / HTTP/1.0\n\n");
190*2175Sjp161948 for(;;) {
191*2175Sjp161948	len = BIO_read(sbio, tmpbuf, 1024);
192*2175Sjp161948	if(len <= 0) break;
193*2175Sjp161948	BIO_write(out, tmpbuf, len);
194*2175Sjp161948 }
195*2175Sjp161948 BIO_free_all(sbio);
196*2175Sjp161948 BIO_free(out);
197*2175Sjp161948
198*2175Sjp161948Here is a simple server example. It makes use of a buffering
199*2175Sjp161948BIO to allow lines to be read from the SSL BIO using BIO_gets.
200*2175Sjp161948It creates a pseudo web page containing the actual request from
201*2175Sjp161948a client and also echoes the request to standard output.
202*2175Sjp161948
203*2175Sjp161948 BIO *sbio, *bbio, *acpt, *out;
204*2175Sjp161948 int len;
205*2175Sjp161948 char tmpbuf[1024];
206*2175Sjp161948 SSL_CTX *ctx;
207*2175Sjp161948 SSL *ssl;
208*2175Sjp161948
209*2175Sjp161948 ERR_load_crypto_strings();
210*2175Sjp161948 ERR_load_SSL_strings();
211*2175Sjp161948 OpenSSL_add_all_algorithms();
212*2175Sjp161948
213*2175Sjp161948 /* Might seed PRNG here */
214*2175Sjp161948
215*2175Sjp161948 ctx = SSL_CTX_new(SSLv23_server_method());
216*2175Sjp161948
217*2175Sjp161948 if (!SSL_CTX_use_certificate_file(ctx,"server.pem",SSL_FILETYPE_PEM)
218*2175Sjp161948	|| !SSL_CTX_use_PrivateKey_file(ctx,"server.pem",SSL_FILETYPE_PEM)
219*2175Sjp161948	|| !SSL_CTX_check_private_key(ctx)) {
220*2175Sjp161948
221*2175Sjp161948	fprintf(stderr, "Error setting up SSL_CTX\n");
222*2175Sjp161948	ERR_print_errors_fp(stderr);
223*2175Sjp161948	return 0;
224*2175Sjp161948 }
225*2175Sjp161948
226*2175Sjp161948 /* Might do other things here like setting verify locations and
227*2175Sjp161948  * DH and/or RSA temporary key callbacks
228*2175Sjp161948  */
229*2175Sjp161948
230*2175Sjp161948 /* New SSL BIO setup as server */
231*2175Sjp161948 sbio=BIO_new_ssl(ctx,0);
232*2175Sjp161948
233*2175Sjp161948 BIO_get_ssl(sbio, &ssl);
234*2175Sjp161948
235*2175Sjp161948 if(!ssl) {
236*2175Sjp161948   fprintf(stderr, "Can't locate SSL pointer\n");
237*2175Sjp161948   /* whatever ... */
238*2175Sjp161948 }
239*2175Sjp161948
240*2175Sjp161948 /* Don't want any retries */
241*2175Sjp161948 SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
242*2175Sjp161948
243*2175Sjp161948 /* Create the buffering BIO */
244*2175Sjp161948
245*2175Sjp161948 bbio = BIO_new(BIO_f_buffer());
246*2175Sjp161948
247*2175Sjp161948 /* Add to chain */
248*2175Sjp161948 sbio = BIO_push(bbio, sbio);
249*2175Sjp161948
250*2175Sjp161948 acpt=BIO_new_accept("4433");
251*2175Sjp161948
252*2175Sjp161948 /* By doing this when a new connection is established
253*2175Sjp161948  * we automatically have sbio inserted into it. The
254*2175Sjp161948  * BIO chain is now 'swallowed' by the accept BIO and
255*2175Sjp161948  * will be freed when the accept BIO is freed.
256*2175Sjp161948  */
257*2175Sjp161948
258*2175Sjp161948 BIO_set_accept_bios(acpt,sbio);
259*2175Sjp161948
260*2175Sjp161948 out = BIO_new_fp(stdout, BIO_NOCLOSE);
261*2175Sjp161948
262*2175Sjp161948 /* Setup accept BIO */
263*2175Sjp161948 if(BIO_do_accept(acpt) <= 0) {
264*2175Sjp161948	fprintf(stderr, "Error setting up accept BIO\n");
265*2175Sjp161948	ERR_print_errors_fp(stderr);
266*2175Sjp161948	return 0;
267*2175Sjp161948 }
268*2175Sjp161948
269*2175Sjp161948 /* Now wait for incoming connection */
270*2175Sjp161948 if(BIO_do_accept(acpt) <= 0) {
271*2175Sjp161948	fprintf(stderr, "Error in connection\n");
272*2175Sjp161948	ERR_print_errors_fp(stderr);
273*2175Sjp161948	return 0;
274*2175Sjp161948 }
275*2175Sjp161948
276*2175Sjp161948 /* We only want one connection so remove and free
277*2175Sjp161948  * accept BIO
278*2175Sjp161948  */
279*2175Sjp161948
280*2175Sjp161948 sbio = BIO_pop(acpt);
281*2175Sjp161948
282*2175Sjp161948 BIO_free_all(acpt);
283*2175Sjp161948
284*2175Sjp161948 if(BIO_do_handshake(sbio) <= 0) {
285*2175Sjp161948	fprintf(stderr, "Error in SSL handshake\n");
286*2175Sjp161948	ERR_print_errors_fp(stderr);
287*2175Sjp161948	return 0;
288*2175Sjp161948 }
289*2175Sjp161948
290*2175Sjp161948 BIO_puts(sbio, "HTTP/1.0 200 OK\r\nContent-type: text/plain\r\n\r\n");
291*2175Sjp161948 BIO_puts(sbio, "\r\nConnection Established\r\nRequest headers:\r\n");
292*2175Sjp161948 BIO_puts(sbio, "--------------------------------------------------\r\n");
293*2175Sjp161948
294*2175Sjp161948 for(;;) {
295*2175Sjp161948 	len = BIO_gets(sbio, tmpbuf, 1024);
296*2175Sjp161948        if(len <= 0) break;
297*2175Sjp161948	BIO_write(sbio, tmpbuf, len);
298*2175Sjp161948	BIO_write(out, tmpbuf, len);
299*2175Sjp161948	/* Look for blank line signifying end of headers*/
300*2175Sjp161948	if((tmpbuf[0] == '\r') || (tmpbuf[0] == '\n')) break;
301*2175Sjp161948 }
302*2175Sjp161948
303*2175Sjp161948 BIO_puts(sbio, "--------------------------------------------------\r\n");
304*2175Sjp161948 BIO_puts(sbio, "\r\n");
305*2175Sjp161948
306*2175Sjp161948 /* Since there is a buffering BIO present we had better flush it */
307*2175Sjp161948 BIO_flush(sbio);
308*2175Sjp161948
309*2175Sjp161948 BIO_free_all(sbio);
310*2175Sjp161948
311*2175Sjp161948=head1 SEE ALSO
312*2175Sjp161948
313*2175Sjp161948TBA
314