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