xref: /minix3/crypto/external/bsd/openssl/dist/ssl/bio_ssl.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* ssl/bio_ssl.c */
2ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ebfedea0SLionel Sambuc  * All rights reserved.
4ebfedea0SLionel Sambuc  *
5ebfedea0SLionel Sambuc  * This package is an SSL implementation written
6ebfedea0SLionel Sambuc  * by Eric Young (eay@cryptsoft.com).
7ebfedea0SLionel Sambuc  * The implementation was written so as to conform with Netscapes SSL.
8ebfedea0SLionel Sambuc  *
9ebfedea0SLionel Sambuc  * This library is free for commercial and non-commercial use as long as
10ebfedea0SLionel Sambuc  * the following conditions are aheared to.  The following conditions
11ebfedea0SLionel Sambuc  * apply to all code found in this distribution, be it the RC4, RSA,
12ebfedea0SLionel Sambuc  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13ebfedea0SLionel Sambuc  * included with this distribution is covered by the same copyright terms
14ebfedea0SLionel Sambuc  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ebfedea0SLionel Sambuc  *
16ebfedea0SLionel Sambuc  * Copyright remains Eric Young's, and as such any Copyright notices in
17ebfedea0SLionel Sambuc  * the code are not to be removed.
18ebfedea0SLionel Sambuc  * If this package is used in a product, Eric Young should be given attribution
19ebfedea0SLionel Sambuc  * as the author of the parts of the library used.
20ebfedea0SLionel Sambuc  * This can be in the form of a textual message at program startup or
21ebfedea0SLionel Sambuc  * in documentation (online or textual) provided with the package.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
24ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
25ebfedea0SLionel Sambuc  * are met:
26ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the copyright
27ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
28ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
29ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
30ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
31ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this software
32ebfedea0SLionel Sambuc  *    must display the following acknowledgement:
33ebfedea0SLionel Sambuc  *    "This product includes cryptographic software written by
34ebfedea0SLionel Sambuc  *     Eric Young (eay@cryptsoft.com)"
35ebfedea0SLionel Sambuc  *    The word 'cryptographic' can be left out if the rouines from the library
36ebfedea0SLionel Sambuc  *    being used are not cryptographic related :-).
37ebfedea0SLionel Sambuc  * 4. If you include any Windows specific code (or a derivative thereof) from
38ebfedea0SLionel Sambuc  *    the apps directory (application code) you must include an acknowledgement:
39ebfedea0SLionel Sambuc  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ebfedea0SLionel Sambuc  *
41ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ebfedea0SLionel Sambuc  * SUCH DAMAGE.
52ebfedea0SLionel Sambuc  *
53ebfedea0SLionel Sambuc  * The licence and distribution terms for any publically available version or
54ebfedea0SLionel Sambuc  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55ebfedea0SLionel Sambuc  * copied and put under another distribution licence
56ebfedea0SLionel Sambuc  * [including the GNU Public Licence.]
57ebfedea0SLionel Sambuc  */
58ebfedea0SLionel Sambuc 
59ebfedea0SLionel Sambuc #include <stdio.h>
60ebfedea0SLionel Sambuc #include <stdlib.h>
61ebfedea0SLionel Sambuc #include <string.h>
62ebfedea0SLionel Sambuc #include <errno.h>
63ebfedea0SLionel Sambuc #include <openssl/crypto.h>
64ebfedea0SLionel Sambuc #include <openssl/bio.h>
65ebfedea0SLionel Sambuc #include <openssl/err.h>
66ebfedea0SLionel Sambuc #include <openssl/ssl.h>
67ebfedea0SLionel Sambuc 
68ebfedea0SLionel Sambuc static int ssl_write(BIO *h, const char *buf, int num);
69ebfedea0SLionel Sambuc static int ssl_read(BIO *h, char *buf, int size);
70ebfedea0SLionel Sambuc static int ssl_puts(BIO *h, const char *str);
71ebfedea0SLionel Sambuc static long ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2);
72ebfedea0SLionel Sambuc static int ssl_new(BIO *h);
73ebfedea0SLionel Sambuc static int ssl_free(BIO *data);
74ebfedea0SLionel Sambuc static long ssl_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
75*0a6a1f1dSLionel Sambuc typedef struct bio_ssl_st {
76ebfedea0SLionel Sambuc     SSL *ssl;                   /* The ssl handle :-) */
77ebfedea0SLionel Sambuc     /* re-negotiate every time the total number of bytes is this size */
78ebfedea0SLionel Sambuc     int num_renegotiates;
79ebfedea0SLionel Sambuc     unsigned long renegotiate_count;
80ebfedea0SLionel Sambuc     unsigned long byte_count;
81ebfedea0SLionel Sambuc     unsigned long renegotiate_timeout;
82ebfedea0SLionel Sambuc     unsigned long last_time;
83ebfedea0SLionel Sambuc } BIO_SSL;
84ebfedea0SLionel Sambuc 
85*0a6a1f1dSLionel Sambuc static BIO_METHOD methods_sslp = {
86ebfedea0SLionel Sambuc     BIO_TYPE_SSL, "ssl",
87ebfedea0SLionel Sambuc     ssl_write,
88ebfedea0SLionel Sambuc     ssl_read,
89ebfedea0SLionel Sambuc     ssl_puts,
90ebfedea0SLionel Sambuc     NULL,                       /* ssl_gets, */
91ebfedea0SLionel Sambuc     ssl_ctrl,
92ebfedea0SLionel Sambuc     ssl_new,
93ebfedea0SLionel Sambuc     ssl_free,
94ebfedea0SLionel Sambuc     ssl_callback_ctrl,
95ebfedea0SLionel Sambuc };
96ebfedea0SLionel Sambuc 
BIO_f_ssl(void)97ebfedea0SLionel Sambuc BIO_METHOD *BIO_f_ssl(void)
98ebfedea0SLionel Sambuc {
99ebfedea0SLionel Sambuc     return (&methods_sslp);
100ebfedea0SLionel Sambuc }
101ebfedea0SLionel Sambuc 
ssl_new(BIO * bi)102ebfedea0SLionel Sambuc static int ssl_new(BIO *bi)
103ebfedea0SLionel Sambuc {
104ebfedea0SLionel Sambuc     BIO_SSL *bs;
105ebfedea0SLionel Sambuc 
106ebfedea0SLionel Sambuc     bs = (BIO_SSL *)OPENSSL_malloc(sizeof(BIO_SSL));
107*0a6a1f1dSLionel Sambuc     if (bs == NULL) {
108ebfedea0SLionel Sambuc         BIOerr(BIO_F_SSL_NEW, ERR_R_MALLOC_FAILURE);
109ebfedea0SLionel Sambuc         return (0);
110ebfedea0SLionel Sambuc     }
111ebfedea0SLionel Sambuc     memset(bs, 0, sizeof(BIO_SSL));
112ebfedea0SLionel Sambuc     bi->init = 0;
113ebfedea0SLionel Sambuc     bi->ptr = (char *)bs;
114ebfedea0SLionel Sambuc     bi->flags = 0;
115ebfedea0SLionel Sambuc     return (1);
116ebfedea0SLionel Sambuc }
117ebfedea0SLionel Sambuc 
ssl_free(BIO * a)118ebfedea0SLionel Sambuc static int ssl_free(BIO *a)
119ebfedea0SLionel Sambuc {
120ebfedea0SLionel Sambuc     BIO_SSL *bs;
121ebfedea0SLionel Sambuc 
122*0a6a1f1dSLionel Sambuc     if (a == NULL)
123*0a6a1f1dSLionel Sambuc         return (0);
124ebfedea0SLionel Sambuc     bs = (BIO_SSL *)a->ptr;
125*0a6a1f1dSLionel Sambuc     if (bs->ssl != NULL)
126*0a6a1f1dSLionel Sambuc         SSL_shutdown(bs->ssl);
127*0a6a1f1dSLionel Sambuc     if (a->shutdown) {
128ebfedea0SLionel Sambuc         if (a->init && (bs->ssl != NULL))
129ebfedea0SLionel Sambuc             SSL_free(bs->ssl);
130ebfedea0SLionel Sambuc         a->init = 0;
131ebfedea0SLionel Sambuc         a->flags = 0;
132ebfedea0SLionel Sambuc     }
133ebfedea0SLionel Sambuc     if (a->ptr != NULL)
134ebfedea0SLionel Sambuc         OPENSSL_free(a->ptr);
135ebfedea0SLionel Sambuc     return (1);
136ebfedea0SLionel Sambuc }
137ebfedea0SLionel Sambuc 
ssl_read(BIO * b,char * out,int outl)138ebfedea0SLionel Sambuc static int ssl_read(BIO *b, char *out, int outl)
139ebfedea0SLionel Sambuc {
140ebfedea0SLionel Sambuc     int ret = 1;
141ebfedea0SLionel Sambuc     BIO_SSL *sb;
142ebfedea0SLionel Sambuc     SSL *ssl;
143ebfedea0SLionel Sambuc     int retry_reason = 0;
144ebfedea0SLionel Sambuc     int r = 0;
145ebfedea0SLionel Sambuc 
146*0a6a1f1dSLionel Sambuc     if (out == NULL)
147*0a6a1f1dSLionel Sambuc         return (0);
148ebfedea0SLionel Sambuc     sb = (BIO_SSL *)b->ptr;
149ebfedea0SLionel Sambuc     ssl = sb->ssl;
150ebfedea0SLionel Sambuc 
151ebfedea0SLionel Sambuc     BIO_clear_retry_flags(b);
152ebfedea0SLionel Sambuc 
153ebfedea0SLionel Sambuc #if 0
154*0a6a1f1dSLionel Sambuc     if (!SSL_is_init_finished(ssl)) {
155ebfedea0SLionel Sambuc /*              ret=SSL_do_handshake(ssl); */
156*0a6a1f1dSLionel Sambuc         if (ret > 0) {
157ebfedea0SLionel Sambuc 
158ebfedea0SLionel Sambuc             outflags = (BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY);
159ebfedea0SLionel Sambuc             ret = -1;
160ebfedea0SLionel Sambuc             goto end;
161ebfedea0SLionel Sambuc         }
162ebfedea0SLionel Sambuc     }
163ebfedea0SLionel Sambuc #endif
164ebfedea0SLionel Sambuc /*      if (ret > 0) */
165ebfedea0SLionel Sambuc     ret = SSL_read(ssl, out, outl);
166ebfedea0SLionel Sambuc 
167*0a6a1f1dSLionel Sambuc     switch (SSL_get_error(ssl, ret)) {
168ebfedea0SLionel Sambuc     case SSL_ERROR_NONE:
169*0a6a1f1dSLionel Sambuc         if (ret <= 0)
170*0a6a1f1dSLionel Sambuc             break;
171*0a6a1f1dSLionel Sambuc         if (sb->renegotiate_count > 0) {
172ebfedea0SLionel Sambuc             sb->byte_count += ret;
173*0a6a1f1dSLionel Sambuc             if (sb->byte_count > sb->renegotiate_count) {
174ebfedea0SLionel Sambuc                 sb->byte_count = 0;
175ebfedea0SLionel Sambuc                 sb->num_renegotiates++;
176ebfedea0SLionel Sambuc                 SSL_renegotiate(ssl);
177ebfedea0SLionel Sambuc                 r = 1;
178ebfedea0SLionel Sambuc             }
179ebfedea0SLionel Sambuc         }
180*0a6a1f1dSLionel Sambuc         if ((sb->renegotiate_timeout > 0) && (!r)) {
181ebfedea0SLionel Sambuc             unsigned long tm;
182ebfedea0SLionel Sambuc 
183ebfedea0SLionel Sambuc             tm = (unsigned long)time(NULL);
184*0a6a1f1dSLionel Sambuc             if (tm > sb->last_time + sb->renegotiate_timeout) {
185ebfedea0SLionel Sambuc                 sb->last_time = tm;
186ebfedea0SLionel Sambuc                 sb->num_renegotiates++;
187ebfedea0SLionel Sambuc                 SSL_renegotiate(ssl);
188ebfedea0SLionel Sambuc             }
189ebfedea0SLionel Sambuc         }
190ebfedea0SLionel Sambuc 
191ebfedea0SLionel Sambuc         break;
192ebfedea0SLionel Sambuc     case SSL_ERROR_WANT_READ:
193ebfedea0SLionel Sambuc         BIO_set_retry_read(b);
194ebfedea0SLionel Sambuc         break;
195ebfedea0SLionel Sambuc     case SSL_ERROR_WANT_WRITE:
196ebfedea0SLionel Sambuc         BIO_set_retry_write(b);
197ebfedea0SLionel Sambuc         break;
198ebfedea0SLionel Sambuc     case SSL_ERROR_WANT_X509_LOOKUP:
199ebfedea0SLionel Sambuc         BIO_set_retry_special(b);
200ebfedea0SLionel Sambuc         retry_reason = BIO_RR_SSL_X509_LOOKUP;
201ebfedea0SLionel Sambuc         break;
202ebfedea0SLionel Sambuc     case SSL_ERROR_WANT_ACCEPT:
203ebfedea0SLionel Sambuc         BIO_set_retry_special(b);
204ebfedea0SLionel Sambuc         retry_reason = BIO_RR_ACCEPT;
205ebfedea0SLionel Sambuc         break;
206ebfedea0SLionel Sambuc     case SSL_ERROR_WANT_CONNECT:
207ebfedea0SLionel Sambuc         BIO_set_retry_special(b);
208ebfedea0SLionel Sambuc         retry_reason = BIO_RR_CONNECT;
209ebfedea0SLionel Sambuc         break;
210ebfedea0SLionel Sambuc     case SSL_ERROR_SYSCALL:
211ebfedea0SLionel Sambuc     case SSL_ERROR_SSL:
212ebfedea0SLionel Sambuc     case SSL_ERROR_ZERO_RETURN:
213ebfedea0SLionel Sambuc     default:
214ebfedea0SLionel Sambuc         break;
215ebfedea0SLionel Sambuc     }
216ebfedea0SLionel Sambuc 
217ebfedea0SLionel Sambuc     b->retry_reason = retry_reason;
218ebfedea0SLionel Sambuc     return (ret);
219ebfedea0SLionel Sambuc }
220ebfedea0SLionel Sambuc 
ssl_write(BIO * b,const char * out,int outl)221ebfedea0SLionel Sambuc static int ssl_write(BIO *b, const char *out, int outl)
222ebfedea0SLionel Sambuc {
223ebfedea0SLionel Sambuc     int ret, r = 0;
224ebfedea0SLionel Sambuc     int retry_reason = 0;
225ebfedea0SLionel Sambuc     SSL *ssl;
226ebfedea0SLionel Sambuc     BIO_SSL *bs;
227ebfedea0SLionel Sambuc 
228*0a6a1f1dSLionel Sambuc     if (out == NULL)
229*0a6a1f1dSLionel Sambuc         return (0);
230ebfedea0SLionel Sambuc     bs = (BIO_SSL *)b->ptr;
231ebfedea0SLionel Sambuc     ssl = bs->ssl;
232ebfedea0SLionel Sambuc 
233ebfedea0SLionel Sambuc     BIO_clear_retry_flags(b);
234ebfedea0SLionel Sambuc 
235*0a6a1f1dSLionel Sambuc     /*
236*0a6a1f1dSLionel Sambuc      * ret=SSL_do_handshake(ssl); if (ret > 0)
237*0a6a1f1dSLionel Sambuc      */
238ebfedea0SLionel Sambuc     ret = SSL_write(ssl, out, outl);
239ebfedea0SLionel Sambuc 
240*0a6a1f1dSLionel Sambuc     switch (SSL_get_error(ssl, ret)) {
241ebfedea0SLionel Sambuc     case SSL_ERROR_NONE:
242*0a6a1f1dSLionel Sambuc         if (ret <= 0)
243*0a6a1f1dSLionel Sambuc             break;
244*0a6a1f1dSLionel Sambuc         if (bs->renegotiate_count > 0) {
245ebfedea0SLionel Sambuc             bs->byte_count += ret;
246*0a6a1f1dSLionel Sambuc             if (bs->byte_count > bs->renegotiate_count) {
247ebfedea0SLionel Sambuc                 bs->byte_count = 0;
248ebfedea0SLionel Sambuc                 bs->num_renegotiates++;
249ebfedea0SLionel Sambuc                 SSL_renegotiate(ssl);
250ebfedea0SLionel Sambuc                 r = 1;
251ebfedea0SLionel Sambuc             }
252ebfedea0SLionel Sambuc         }
253*0a6a1f1dSLionel Sambuc         if ((bs->renegotiate_timeout > 0) && (!r)) {
254ebfedea0SLionel Sambuc             unsigned long tm;
255ebfedea0SLionel Sambuc 
256ebfedea0SLionel Sambuc             tm = (unsigned long)time(NULL);
257*0a6a1f1dSLionel Sambuc             if (tm > bs->last_time + bs->renegotiate_timeout) {
258ebfedea0SLionel Sambuc                 bs->last_time = tm;
259ebfedea0SLionel Sambuc                 bs->num_renegotiates++;
260ebfedea0SLionel Sambuc                 SSL_renegotiate(ssl);
261ebfedea0SLionel Sambuc             }
262ebfedea0SLionel Sambuc         }
263ebfedea0SLionel Sambuc         break;
264ebfedea0SLionel Sambuc     case SSL_ERROR_WANT_WRITE:
265ebfedea0SLionel Sambuc         BIO_set_retry_write(b);
266ebfedea0SLionel Sambuc         break;
267ebfedea0SLionel Sambuc     case SSL_ERROR_WANT_READ:
268ebfedea0SLionel Sambuc         BIO_set_retry_read(b);
269ebfedea0SLionel Sambuc         break;
270ebfedea0SLionel Sambuc     case SSL_ERROR_WANT_X509_LOOKUP:
271ebfedea0SLionel Sambuc         BIO_set_retry_special(b);
272ebfedea0SLionel Sambuc         retry_reason = BIO_RR_SSL_X509_LOOKUP;
273ebfedea0SLionel Sambuc         break;
274ebfedea0SLionel Sambuc     case SSL_ERROR_WANT_CONNECT:
275ebfedea0SLionel Sambuc         BIO_set_retry_special(b);
276ebfedea0SLionel Sambuc         retry_reason = BIO_RR_CONNECT;
277ebfedea0SLionel Sambuc     case SSL_ERROR_SYSCALL:
278ebfedea0SLionel Sambuc     case SSL_ERROR_SSL:
279ebfedea0SLionel Sambuc     default:
280ebfedea0SLionel Sambuc         break;
281ebfedea0SLionel Sambuc     }
282ebfedea0SLionel Sambuc 
283ebfedea0SLionel Sambuc     b->retry_reason = retry_reason;
284ebfedea0SLionel Sambuc     return (ret);
285ebfedea0SLionel Sambuc }
286ebfedea0SLionel Sambuc 
ssl_ctrl(BIO * b,int cmd,long num,void * ptr)287ebfedea0SLionel Sambuc static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr)
288ebfedea0SLionel Sambuc {
289ebfedea0SLionel Sambuc     SSL **sslp, *ssl;
290ebfedea0SLionel Sambuc     BIO_SSL *bs;
291ebfedea0SLionel Sambuc     BIO *dbio, *bio;
292ebfedea0SLionel Sambuc     long ret = 1;
293ebfedea0SLionel Sambuc 
294ebfedea0SLionel Sambuc     bs = (BIO_SSL *)b->ptr;
295ebfedea0SLionel Sambuc     ssl = bs->ssl;
296ebfedea0SLionel Sambuc     if ((ssl == NULL) && (cmd != BIO_C_SET_SSL))
297ebfedea0SLionel Sambuc         return (0);
298*0a6a1f1dSLionel Sambuc     switch (cmd) {
299ebfedea0SLionel Sambuc     case BIO_CTRL_RESET:
300ebfedea0SLionel Sambuc         SSL_shutdown(ssl);
301ebfedea0SLionel Sambuc 
302ebfedea0SLionel Sambuc         if (ssl->handshake_func == ssl->method->ssl_connect)
303ebfedea0SLionel Sambuc             SSL_set_connect_state(ssl);
304ebfedea0SLionel Sambuc         else if (ssl->handshake_func == ssl->method->ssl_accept)
305ebfedea0SLionel Sambuc             SSL_set_accept_state(ssl);
306ebfedea0SLionel Sambuc 
307ebfedea0SLionel Sambuc         SSL_clear(ssl);
308ebfedea0SLionel Sambuc 
309ebfedea0SLionel Sambuc         if (b->next_bio != NULL)
310ebfedea0SLionel Sambuc             ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
311ebfedea0SLionel Sambuc         else if (ssl->rbio != NULL)
312ebfedea0SLionel Sambuc             ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);
313ebfedea0SLionel Sambuc         else
314ebfedea0SLionel Sambuc             ret = 1;
315ebfedea0SLionel Sambuc         break;
316ebfedea0SLionel Sambuc     case BIO_CTRL_INFO:
317ebfedea0SLionel Sambuc         ret = 0;
318ebfedea0SLionel Sambuc         break;
319ebfedea0SLionel Sambuc     case BIO_C_SSL_MODE:
320ebfedea0SLionel Sambuc         if (num)                /* client mode */
321ebfedea0SLionel Sambuc             SSL_set_connect_state(ssl);
322ebfedea0SLionel Sambuc         else
323ebfedea0SLionel Sambuc             SSL_set_accept_state(ssl);
324ebfedea0SLionel Sambuc         break;
325ebfedea0SLionel Sambuc     case BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT:
326ebfedea0SLionel Sambuc         ret = bs->renegotiate_timeout;
327*0a6a1f1dSLionel Sambuc         if (num < 60)
328*0a6a1f1dSLionel Sambuc             num = 5;
329ebfedea0SLionel Sambuc         bs->renegotiate_timeout = (unsigned long)num;
330ebfedea0SLionel Sambuc         bs->last_time = (unsigned long)time(NULL);
331ebfedea0SLionel Sambuc         break;
332ebfedea0SLionel Sambuc     case BIO_C_SET_SSL_RENEGOTIATE_BYTES:
333ebfedea0SLionel Sambuc         ret = bs->renegotiate_count;
334ebfedea0SLionel Sambuc         if ((long)num >= 512)
335ebfedea0SLionel Sambuc             bs->renegotiate_count = (unsigned long)num;
336ebfedea0SLionel Sambuc         break;
337ebfedea0SLionel Sambuc     case BIO_C_GET_SSL_NUM_RENEGOTIATES:
338ebfedea0SLionel Sambuc         ret = bs->num_renegotiates;
339ebfedea0SLionel Sambuc         break;
340ebfedea0SLionel Sambuc     case BIO_C_SET_SSL:
341*0a6a1f1dSLionel Sambuc         if (ssl != NULL) {
342ebfedea0SLionel Sambuc             ssl_free(b);
343ebfedea0SLionel Sambuc             if (!ssl_new(b))
344ebfedea0SLionel Sambuc                 return 0;
345ebfedea0SLionel Sambuc         }
346ebfedea0SLionel Sambuc         b->shutdown = (int)num;
347ebfedea0SLionel Sambuc         ssl = (SSL *)ptr;
348ebfedea0SLionel Sambuc         ((BIO_SSL *)b->ptr)->ssl = ssl;
349ebfedea0SLionel Sambuc         bio = SSL_get_rbio(ssl);
350*0a6a1f1dSLionel Sambuc         if (bio != NULL) {
351ebfedea0SLionel Sambuc             if (b->next_bio != NULL)
352ebfedea0SLionel Sambuc                 BIO_push(bio, b->next_bio);
353ebfedea0SLionel Sambuc             b->next_bio = bio;
354ebfedea0SLionel Sambuc             CRYPTO_add(&bio->references, 1, CRYPTO_LOCK_BIO);
355ebfedea0SLionel Sambuc         }
356ebfedea0SLionel Sambuc         b->init = 1;
357ebfedea0SLionel Sambuc         break;
358ebfedea0SLionel Sambuc     case BIO_C_GET_SSL:
359*0a6a1f1dSLionel Sambuc         if (ptr != NULL) {
360ebfedea0SLionel Sambuc             sslp = (SSL **)ptr;
361ebfedea0SLionel Sambuc             *sslp = ssl;
362*0a6a1f1dSLionel Sambuc         } else
363ebfedea0SLionel Sambuc             ret = 0;
364ebfedea0SLionel Sambuc         break;
365ebfedea0SLionel Sambuc     case BIO_CTRL_GET_CLOSE:
366ebfedea0SLionel Sambuc         ret = b->shutdown;
367ebfedea0SLionel Sambuc         break;
368ebfedea0SLionel Sambuc     case BIO_CTRL_SET_CLOSE:
369ebfedea0SLionel Sambuc         b->shutdown = (int)num;
370ebfedea0SLionel Sambuc         break;
371ebfedea0SLionel Sambuc     case BIO_CTRL_WPENDING:
372ebfedea0SLionel Sambuc         ret = BIO_ctrl(ssl->wbio, cmd, num, ptr);
373ebfedea0SLionel Sambuc         break;
374ebfedea0SLionel Sambuc     case BIO_CTRL_PENDING:
375ebfedea0SLionel Sambuc         ret = SSL_pending(ssl);
376ebfedea0SLionel Sambuc         if (ret == 0)
377ebfedea0SLionel Sambuc             ret = BIO_pending(ssl->rbio);
378ebfedea0SLionel Sambuc         break;
379ebfedea0SLionel Sambuc     case BIO_CTRL_FLUSH:
380ebfedea0SLionel Sambuc         BIO_clear_retry_flags(b);
381ebfedea0SLionel Sambuc         ret = BIO_ctrl(ssl->wbio, cmd, num, ptr);
382ebfedea0SLionel Sambuc         BIO_copy_next_retry(b);
383ebfedea0SLionel Sambuc         break;
384ebfedea0SLionel Sambuc     case BIO_CTRL_PUSH:
385*0a6a1f1dSLionel Sambuc         if ((b->next_bio != NULL) && (b->next_bio != ssl->rbio)) {
386ebfedea0SLionel Sambuc             SSL_set_bio(ssl, b->next_bio, b->next_bio);
387ebfedea0SLionel Sambuc             CRYPTO_add(&b->next_bio->references, 1, CRYPTO_LOCK_BIO);
388ebfedea0SLionel Sambuc         }
389ebfedea0SLionel Sambuc         break;
390ebfedea0SLionel Sambuc     case BIO_CTRL_POP:
391ebfedea0SLionel Sambuc         /* Only detach if we are the BIO explicitly being popped */
392*0a6a1f1dSLionel Sambuc         if (b == ptr) {
393*0a6a1f1dSLionel Sambuc             /*
394*0a6a1f1dSLionel Sambuc              * Shouldn't happen in practice because the rbio and wbio are the
395*0a6a1f1dSLionel Sambuc              * same when pushed.
396ebfedea0SLionel Sambuc              */
397ebfedea0SLionel Sambuc             if (ssl->rbio != ssl->wbio)
398ebfedea0SLionel Sambuc                 BIO_free_all(ssl->wbio);
399ebfedea0SLionel Sambuc             if (b->next_bio != NULL)
400ebfedea0SLionel Sambuc                 CRYPTO_add(&b->next_bio->references, -1, CRYPTO_LOCK_BIO);
401ebfedea0SLionel Sambuc             ssl->wbio = NULL;
402ebfedea0SLionel Sambuc             ssl->rbio = NULL;
403ebfedea0SLionel Sambuc         }
404ebfedea0SLionel Sambuc         break;
405ebfedea0SLionel Sambuc     case BIO_C_DO_STATE_MACHINE:
406ebfedea0SLionel Sambuc         BIO_clear_retry_flags(b);
407ebfedea0SLionel Sambuc 
408ebfedea0SLionel Sambuc         b->retry_reason = 0;
409ebfedea0SLionel Sambuc         ret = (int)SSL_do_handshake(ssl);
410ebfedea0SLionel Sambuc 
411*0a6a1f1dSLionel Sambuc         switch (SSL_get_error(ssl, (int)ret)) {
412ebfedea0SLionel Sambuc         case SSL_ERROR_WANT_READ:
413*0a6a1f1dSLionel Sambuc             BIO_set_flags(b, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY);
414ebfedea0SLionel Sambuc             break;
415ebfedea0SLionel Sambuc         case SSL_ERROR_WANT_WRITE:
416*0a6a1f1dSLionel Sambuc             BIO_set_flags(b, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY);
417ebfedea0SLionel Sambuc             break;
418ebfedea0SLionel Sambuc         case SSL_ERROR_WANT_CONNECT:
419*0a6a1f1dSLionel Sambuc             BIO_set_flags(b, BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY);
420ebfedea0SLionel Sambuc             b->retry_reason = b->next_bio->retry_reason;
421ebfedea0SLionel Sambuc             break;
422ebfedea0SLionel Sambuc         default:
423ebfedea0SLionel Sambuc             break;
424ebfedea0SLionel Sambuc         }
425ebfedea0SLionel Sambuc         break;
426ebfedea0SLionel Sambuc     case BIO_CTRL_DUP:
427ebfedea0SLionel Sambuc         dbio = (BIO *)ptr;
428ebfedea0SLionel Sambuc         if (((BIO_SSL *)dbio->ptr)->ssl != NULL)
429ebfedea0SLionel Sambuc             SSL_free(((BIO_SSL *)dbio->ptr)->ssl);
430ebfedea0SLionel Sambuc         ((BIO_SSL *)dbio->ptr)->ssl = SSL_dup(ssl);
431ebfedea0SLionel Sambuc         ((BIO_SSL *)dbio->ptr)->renegotiate_count =
432ebfedea0SLionel Sambuc             ((BIO_SSL *)b->ptr)->renegotiate_count;
433*0a6a1f1dSLionel Sambuc         ((BIO_SSL *)dbio->ptr)->byte_count = ((BIO_SSL *)b->ptr)->byte_count;
434ebfedea0SLionel Sambuc         ((BIO_SSL *)dbio->ptr)->renegotiate_timeout =
435ebfedea0SLionel Sambuc             ((BIO_SSL *)b->ptr)->renegotiate_timeout;
436*0a6a1f1dSLionel Sambuc         ((BIO_SSL *)dbio->ptr)->last_time = ((BIO_SSL *)b->ptr)->last_time;
437ebfedea0SLionel Sambuc         ret = (((BIO_SSL *)dbio->ptr)->ssl != NULL);
438ebfedea0SLionel Sambuc         break;
439ebfedea0SLionel Sambuc     case BIO_C_GET_FD:
440ebfedea0SLionel Sambuc         ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);
441ebfedea0SLionel Sambuc         break;
442ebfedea0SLionel Sambuc     case BIO_CTRL_SET_CALLBACK:
443ebfedea0SLionel Sambuc         {
444*0a6a1f1dSLionel Sambuc #if 0                           /* FIXME: Should this be used? -- Richard
445*0a6a1f1dSLionel Sambuc                                  * Levitte */
446ebfedea0SLionel Sambuc             SSLerr(SSL_F_SSL_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
447ebfedea0SLionel Sambuc             ret = -1;
448ebfedea0SLionel Sambuc #else
449ebfedea0SLionel Sambuc             ret = 0;
450ebfedea0SLionel Sambuc #endif
451ebfedea0SLionel Sambuc         }
452ebfedea0SLionel Sambuc         break;
453ebfedea0SLionel Sambuc     case BIO_CTRL_GET_CALLBACK:
454ebfedea0SLionel Sambuc         {
455ebfedea0SLionel Sambuc             void (**fptr) (const SSL *xssl, int type, int val);
456ebfedea0SLionel Sambuc 
457ebfedea0SLionel Sambuc             fptr = (void (**)(const SSL *xssl, int type, int val))ptr;
458ebfedea0SLionel Sambuc             *fptr = SSL_get_info_callback(ssl);
459ebfedea0SLionel Sambuc         }
460ebfedea0SLionel Sambuc         break;
461ebfedea0SLionel Sambuc     default:
462ebfedea0SLionel Sambuc         ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);
463ebfedea0SLionel Sambuc         break;
464ebfedea0SLionel Sambuc     }
465ebfedea0SLionel Sambuc     return (ret);
466ebfedea0SLionel Sambuc }
467ebfedea0SLionel Sambuc 
ssl_callback_ctrl(BIO * b,int cmd,bio_info_cb * fp)468ebfedea0SLionel Sambuc static long ssl_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
469ebfedea0SLionel Sambuc {
470ebfedea0SLionel Sambuc     SSL *ssl;
471ebfedea0SLionel Sambuc     BIO_SSL *bs;
472ebfedea0SLionel Sambuc     long ret = 1;
473ebfedea0SLionel Sambuc 
474ebfedea0SLionel Sambuc     bs = (BIO_SSL *)b->ptr;
475ebfedea0SLionel Sambuc     ssl = bs->ssl;
476*0a6a1f1dSLionel Sambuc     switch (cmd) {
477ebfedea0SLionel Sambuc     case BIO_CTRL_SET_CALLBACK:
478ebfedea0SLionel Sambuc         {
479*0a6a1f1dSLionel Sambuc             /*
480*0a6a1f1dSLionel Sambuc              * FIXME: setting this via a completely different prototype seems
481*0a6a1f1dSLionel Sambuc              * like a crap idea
482*0a6a1f1dSLionel Sambuc              */
483ebfedea0SLionel Sambuc             SSL_set_info_callback(ssl, (void (*)(const SSL *, int, int))fp);
484ebfedea0SLionel Sambuc         }
485ebfedea0SLionel Sambuc         break;
486ebfedea0SLionel Sambuc     default:
487ebfedea0SLionel Sambuc         ret = BIO_callback_ctrl(ssl->rbio, cmd, fp);
488ebfedea0SLionel Sambuc         break;
489ebfedea0SLionel Sambuc     }
490ebfedea0SLionel Sambuc     return (ret);
491ebfedea0SLionel Sambuc }
492ebfedea0SLionel Sambuc 
ssl_puts(BIO * bp,const char * str)493ebfedea0SLionel Sambuc static int ssl_puts(BIO *bp, const char *str)
494ebfedea0SLionel Sambuc {
495ebfedea0SLionel Sambuc     int n, ret;
496ebfedea0SLionel Sambuc 
497ebfedea0SLionel Sambuc     n = strlen(str);
498ebfedea0SLionel Sambuc     ret = BIO_write(bp, str, n);
499ebfedea0SLionel Sambuc     return (ret);
500ebfedea0SLionel Sambuc }
501ebfedea0SLionel Sambuc 
BIO_new_buffer_ssl_connect(SSL_CTX * ctx)502ebfedea0SLionel Sambuc BIO *BIO_new_buffer_ssl_connect(SSL_CTX *ctx)
503ebfedea0SLionel Sambuc {
504ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SOCK
505ebfedea0SLionel Sambuc     BIO *ret = NULL, *buf = NULL, *ssl = NULL;
506ebfedea0SLionel Sambuc 
507ebfedea0SLionel Sambuc     if ((buf = BIO_new(BIO_f_buffer())) == NULL)
508ebfedea0SLionel Sambuc         return (NULL);
509ebfedea0SLionel Sambuc     if ((ssl = BIO_new_ssl_connect(ctx)) == NULL)
510ebfedea0SLionel Sambuc         goto err;
511ebfedea0SLionel Sambuc     if ((ret = BIO_push(buf, ssl)) == NULL)
512ebfedea0SLionel Sambuc         goto err;
513ebfedea0SLionel Sambuc     return (ret);
514ebfedea0SLionel Sambuc  err:
515*0a6a1f1dSLionel Sambuc     if (buf != NULL)
516*0a6a1f1dSLionel Sambuc         BIO_free(buf);
517*0a6a1f1dSLionel Sambuc     if (ssl != NULL)
518*0a6a1f1dSLionel Sambuc         BIO_free(ssl);
519ebfedea0SLionel Sambuc #endif
520ebfedea0SLionel Sambuc     return (NULL);
521ebfedea0SLionel Sambuc }
522ebfedea0SLionel Sambuc 
BIO_new_ssl_connect(SSL_CTX * ctx)523ebfedea0SLionel Sambuc BIO *BIO_new_ssl_connect(SSL_CTX *ctx)
524ebfedea0SLionel Sambuc {
525ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SOCK
526ebfedea0SLionel Sambuc     BIO *ret = NULL, *con = NULL, *ssl = NULL;
527ebfedea0SLionel Sambuc 
528ebfedea0SLionel Sambuc     if ((con = BIO_new(BIO_s_connect())) == NULL)
529ebfedea0SLionel Sambuc         return (NULL);
530ebfedea0SLionel Sambuc     if ((ssl = BIO_new_ssl(ctx, 1)) == NULL)
531ebfedea0SLionel Sambuc         goto err;
532ebfedea0SLionel Sambuc     if ((ret = BIO_push(ssl, con)) == NULL)
533ebfedea0SLionel Sambuc         goto err;
534ebfedea0SLionel Sambuc     return (ret);
535ebfedea0SLionel Sambuc  err:
536*0a6a1f1dSLionel Sambuc     if (con != NULL)
537*0a6a1f1dSLionel Sambuc         BIO_free(con);
538ebfedea0SLionel Sambuc #endif
539ebfedea0SLionel Sambuc     return (NULL);
540ebfedea0SLionel Sambuc }
541ebfedea0SLionel Sambuc 
BIO_new_ssl(SSL_CTX * ctx,int client)542ebfedea0SLionel Sambuc BIO *BIO_new_ssl(SSL_CTX *ctx, int client)
543ebfedea0SLionel Sambuc {
544ebfedea0SLionel Sambuc     BIO *ret;
545ebfedea0SLionel Sambuc     SSL *ssl;
546ebfedea0SLionel Sambuc 
547ebfedea0SLionel Sambuc     if ((ret = BIO_new(BIO_f_ssl())) == NULL)
548ebfedea0SLionel Sambuc         return (NULL);
549*0a6a1f1dSLionel Sambuc     if ((ssl = SSL_new(ctx)) == NULL) {
550ebfedea0SLionel Sambuc         BIO_free(ret);
551ebfedea0SLionel Sambuc         return (NULL);
552ebfedea0SLionel Sambuc     }
553ebfedea0SLionel Sambuc     if (client)
554ebfedea0SLionel Sambuc         SSL_set_connect_state(ssl);
555ebfedea0SLionel Sambuc     else
556ebfedea0SLionel Sambuc         SSL_set_accept_state(ssl);
557ebfedea0SLionel Sambuc 
558ebfedea0SLionel Sambuc     BIO_set_ssl(ret, ssl, BIO_CLOSE);
559ebfedea0SLionel Sambuc     return (ret);
560ebfedea0SLionel Sambuc }
561ebfedea0SLionel Sambuc 
BIO_ssl_copy_session_id(BIO * t,BIO * f)562ebfedea0SLionel Sambuc int BIO_ssl_copy_session_id(BIO *t, BIO *f)
563ebfedea0SLionel Sambuc {
564ebfedea0SLionel Sambuc     t = BIO_find_type(t, BIO_TYPE_SSL);
565ebfedea0SLionel Sambuc     f = BIO_find_type(f, BIO_TYPE_SSL);
566ebfedea0SLionel Sambuc     if ((t == NULL) || (f == NULL))
567ebfedea0SLionel Sambuc         return (0);
568ebfedea0SLionel Sambuc     if ((((BIO_SSL *)t->ptr)->ssl == NULL) ||
569ebfedea0SLionel Sambuc         (((BIO_SSL *)f->ptr)->ssl == NULL))
570ebfedea0SLionel Sambuc         return (0);
571ebfedea0SLionel Sambuc     SSL_copy_session_id(((BIO_SSL *)t->ptr)->ssl, ((BIO_SSL *)f->ptr)->ssl);
572ebfedea0SLionel Sambuc     return (1);
573ebfedea0SLionel Sambuc }
574ebfedea0SLionel Sambuc 
BIO_ssl_shutdown(BIO * b)575ebfedea0SLionel Sambuc void BIO_ssl_shutdown(BIO *b)
576ebfedea0SLionel Sambuc {
577ebfedea0SLionel Sambuc     SSL *s;
578ebfedea0SLionel Sambuc 
579*0a6a1f1dSLionel Sambuc     while (b != NULL) {
580*0a6a1f1dSLionel Sambuc         if (b->method->type == BIO_TYPE_SSL) {
581ebfedea0SLionel Sambuc             s = ((BIO_SSL *)b->ptr)->ssl;
582ebfedea0SLionel Sambuc             SSL_shutdown(s);
583ebfedea0SLionel Sambuc             break;
584ebfedea0SLionel Sambuc         }
585ebfedea0SLionel Sambuc         b = b->next_bio;
586ebfedea0SLionel Sambuc     }
587ebfedea0SLionel Sambuc }
588