Apply Lutz Behnke's 56 bit cipher patch with a few
[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 <openssl/crypto.h>
64 #include <openssl/bio.h>
65 #include <openssl/err.h>
66 #include <openssl/ssl.h>
67
68 static int ssl_write(BIO *h,char *buf,int num);
69 static int ssl_read(BIO *h,char *buf,int size);
70 static int ssl_puts(BIO *h,char *str);
71 static long ssl_ctrl(BIO *h,int cmd,long arg1,char *arg2);
72 static int ssl_new(BIO *h);
73 static int ssl_free(BIO *data);
74 typedef struct bio_ssl_st
75         {
76         SSL *ssl; /* The ssl handle :-) */
77         /* re-negotiate every time the total number of bytes is this size */
78         int num_renegotiates;
79         unsigned long renegotiate_count;
80         unsigned long byte_count;
81         unsigned long renegotiate_timeout;
82         unsigned long last_time;
83         } BIO_SSL;
84
85 static BIO_METHOD methods_sslp=
86         {
87         BIO_TYPE_SSL,"ssl",
88         ssl_write,
89         ssl_read,
90         ssl_puts,
91         NULL, /* ssl_gets, */
92         ssl_ctrl,
93         ssl_new,
94         ssl_free,
95         };
96
97 union void_fn_to_char_u
98         {
99         char *char_p;
100         void (*fn_p)();
101         };
102
103 BIO_METHOD *BIO_f_ssl(void)
104         {
105         return(&methods_sslp);
106         }
107
108 static int ssl_new(BIO *bi)
109         {
110         BIO_SSL *bs;
111
112         bs=(BIO_SSL *)Malloc(sizeof(BIO_SSL));
113         if (bs == NULL)
114                 {
115                 BIOerr(BIO_F_SSL_NEW,ERR_R_MALLOC_FAILURE);
116                 return(0);
117                 }
118         memset(bs,0,sizeof(BIO_SSL));
119         bi->init=0;
120         bi->ptr=(char *)bs;
121         bi->flags=0;
122         return(1);
123         }
124
125 static int ssl_free(BIO *a)
126         {
127         BIO_SSL *bs;
128
129         if (a == NULL) return(0);
130         bs=(BIO_SSL *)a->ptr;
131         if (bs->ssl != NULL) SSL_shutdown(bs->ssl);
132         if (a->shutdown)
133                 {
134                 if (a->init && (bs->ssl != NULL))
135                         SSL_free(bs->ssl);
136                 a->init=0;
137                 a->flags=0;
138                 }
139         if (a->ptr != NULL)
140                 Free(a->ptr);
141         return(1);
142         }
143         
144 static int ssl_read(BIO *b, char *out, int outl)
145         {
146         int ret=1;
147         BIO_SSL *sb;
148         SSL *ssl;
149         int retry_reason=0;
150         int r=0;
151
152         if (out == NULL) return(0);
153         sb=(BIO_SSL *)b->ptr;
154         ssl=sb->ssl;
155
156         BIO_clear_retry_flags(b);
157
158 #if 0
159         if (!SSL_is_init_finished(ssl))
160                 {
161 /*              ret=SSL_do_handshake(ssl); */
162                 if (ret > 0)
163                         {
164
165                         outflags=(BIO_FLAGS_READ|BIO_FLAGS_SHOULD_RETRY);
166                         ret= -1;
167                         goto end;
168                         }
169                 }
170 #endif
171 /*      if (ret > 0) */
172         ret=SSL_read(ssl,out,outl);
173
174         switch (SSL_get_error(ssl,ret))
175                 {
176         case SSL_ERROR_NONE:
177                 if (ret <= 0) break;
178                 if (sb->renegotiate_count > 0)
179                         {
180                         sb->byte_count+=ret;
181                         if (sb->byte_count > sb->renegotiate_count)
182                                 {
183                                 sb->byte_count=0;
184                                 sb->num_renegotiates++;
185                                 SSL_renegotiate(ssl);
186                                 r=1;
187                                 }
188                         }
189                 if ((sb->renegotiate_timeout > 0) && (!r))
190                         {
191                         unsigned long tm;
192
193                         tm=(unsigned long)time(NULL);
194                         if (tm > sb->last_time+sb->renegotiate_timeout)
195                                 {
196                                 sb->last_time=tm;
197                                 sb->num_renegotiates++;
198                                 SSL_renegotiate(ssl);
199                                 }
200                         }
201
202                 break;
203         case SSL_ERROR_WANT_READ:
204                 BIO_set_retry_read(b);
205                 break;
206         case SSL_ERROR_WANT_WRITE:
207                 BIO_set_retry_write(b);
208                 break;
209         case SSL_ERROR_WANT_X509_LOOKUP:
210                 BIO_set_retry_special(b);
211                 retry_reason=BIO_RR_SSL_X509_LOOKUP;
212                 break;
213         case SSL_ERROR_WANT_CONNECT:
214                 BIO_set_retry_special(b);
215                 retry_reason=BIO_RR_CONNECT;
216                 break;
217         case SSL_ERROR_SYSCALL:
218         case SSL_ERROR_SSL:
219         case SSL_ERROR_ZERO_RETURN:
220         default:
221                 break;
222                 }
223
224         b->retry_reason=retry_reason;
225         return(ret);
226         }
227
228 static int ssl_write(BIO *b, char *out, int outl)
229         {
230         int ret,r=0;
231         int retry_reason=0;
232         SSL *ssl;
233         BIO_SSL *bs;
234
235         if (out == NULL) return(0);
236         bs=(BIO_SSL *)b->ptr;
237         ssl=bs->ssl;
238
239         BIO_clear_retry_flags(b);
240
241 /*      ret=SSL_do_handshake(ssl);
242         if (ret > 0) */
243         ret=SSL_write(ssl,out,outl);
244
245         switch (SSL_get_error(ssl,ret))
246                 {
247         case SSL_ERROR_NONE:
248                 if (ret <= 0) break;
249                 if (bs->renegotiate_count > 0)
250                         {
251                         bs->byte_count+=ret;
252                         if (bs->byte_count > bs->renegotiate_count)
253                                 {
254                                 bs->byte_count=0;
255                                 bs->num_renegotiates++;
256                                 SSL_renegotiate(ssl);
257                                 r=1;
258                                 }
259                         }
260                 if ((bs->renegotiate_timeout > 0) && (!r))
261                         {
262                         unsigned long tm;
263
264                         tm=(unsigned long)time(NULL);
265                         if (tm > bs->last_time+bs->renegotiate_timeout)
266                                 {
267                                 bs->last_time=tm;
268                                 bs->num_renegotiates++;
269                                 SSL_renegotiate(ssl);
270                                 }
271                         }
272                 break;
273         case SSL_ERROR_WANT_WRITE:
274                 BIO_set_retry_write(b);
275                 break;
276         case SSL_ERROR_WANT_READ:
277                 BIO_set_retry_read(b);
278                 break;
279         case SSL_ERROR_WANT_X509_LOOKUP:
280                 BIO_set_retry_special(b);
281                 retry_reason=BIO_RR_SSL_X509_LOOKUP;
282                 break;
283         case SSL_ERROR_WANT_CONNECT:
284                 BIO_set_retry_special(b);
285                 retry_reason=BIO_RR_CONNECT;
286         case SSL_ERROR_SYSCALL:
287         case SSL_ERROR_SSL:
288         default:
289                 break;
290                 }
291
292         b->retry_reason=retry_reason;
293         return(ret);
294         }
295
296 static long ssl_ctrl(BIO *b, int cmd, long num, char *ptr)
297         {
298         SSL **sslp,*ssl;
299         BIO_SSL *bs;
300         BIO *dbio,*bio;
301         long ret=1;
302
303         bs=(BIO_SSL *)b->ptr;
304         ssl=bs->ssl;
305         if ((ssl == NULL)  && (cmd != BIO_C_SET_SSL))
306                 return(0);
307         switch (cmd)
308                 {
309         case BIO_CTRL_RESET:
310                 SSL_shutdown(ssl);
311
312                 if (ssl->handshake_func == ssl->method->ssl_connect)
313                         SSL_set_connect_state(ssl);
314                 else if (ssl->handshake_func == ssl->method->ssl_accept)
315                         SSL_set_accept_state(ssl);
316
317                 SSL_clear(ssl);
318
319                 if (b->next_bio != NULL)
320                         ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
321                 else if (ssl->rbio != NULL)
322                         ret=BIO_ctrl(ssl->rbio,cmd,num,ptr);
323                 else
324                         ret=1;
325                 break;
326         case BIO_CTRL_INFO:
327                 ret=0;
328                 break;
329         case BIO_C_SSL_MODE:
330                 if (num) /* client mode */
331                         SSL_set_connect_state(ssl);
332                 else
333                         SSL_set_accept_state(ssl);
334                 break;
335         case BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT:
336                 ret=bs->renegotiate_timeout;
337                 if (num < 60) num=5;
338                 bs->renegotiate_timeout=(unsigned long)num;
339                 bs->last_time=(unsigned long)time(NULL);
340                 break;
341         case BIO_C_SET_SSL_RENEGOTIATE_BYTES:
342                 ret=bs->renegotiate_count;
343                 if ((long)num >=512)
344                         bs->renegotiate_count=(unsigned long)num;
345                 break;
346         case BIO_C_GET_SSL_NUM_RENEGOTIATES:
347                 ret=bs->num_renegotiates;
348                 break;
349         case BIO_C_SET_SSL:
350                 if (ssl != NULL)
351                         ssl_free(b);
352                 b->shutdown=(int)num;
353                 ssl=(SSL *)ptr;
354                 ((BIO_SSL *)b->ptr)->ssl=ssl;
355                 bio=SSL_get_rbio(ssl);
356                 if (bio != NULL)
357                         {
358                         if (b->next_bio != NULL)
359                                 BIO_push(bio,b->next_bio);
360                         b->next_bio=bio;
361                         CRYPTO_add(&bio->references,1,CRYPTO_LOCK_BIO);
362                         }
363                 b->init=1;
364                 break;
365         case BIO_C_GET_SSL:
366                 if (ptr != NULL)
367                         {
368                         sslp=(SSL **)ptr;
369                         *sslp=ssl;
370                         }
371                 else
372                         ret=0;
373                 break;
374         case BIO_CTRL_GET_CLOSE:
375                 ret=b->shutdown;
376                 break;
377         case BIO_CTRL_SET_CLOSE:
378                 b->shutdown=(int)num;
379                 break;
380         case BIO_CTRL_WPENDING:
381                 ret=BIO_ctrl(ssl->wbio,cmd,num,ptr);
382                 break;
383         case BIO_CTRL_PENDING:
384                 ret=SSL_pending(ssl);
385                 if (ret == 0)
386                         ret=BIO_pending(ssl->rbio);
387                 break;
388         case BIO_CTRL_FLUSH:
389                 BIO_clear_retry_flags(b);
390                 ret=BIO_ctrl(ssl->wbio,cmd,num,ptr);
391                 BIO_copy_next_retry(b);
392                 break;
393         case BIO_CTRL_PUSH:
394                 if ((b->next_bio != NULL) && (b->next_bio != ssl->rbio))
395                         {
396                         SSL_set_bio(ssl,b->next_bio,b->next_bio);
397                         CRYPTO_add(&b->next_bio->references,1,CRYPTO_LOCK_BIO);
398                         }
399                 break;
400         case BIO_CTRL_POP:
401                 /* ugly bit of a hack */
402                 if (ssl->rbio != ssl->wbio) /* we are in trouble :-( */
403                         {
404                         BIO_free_all(ssl->wbio);
405                         }
406                 ssl->wbio=NULL;
407                 ssl->rbio=NULL;
408                 break;
409         case BIO_C_DO_STATE_MACHINE:
410                 BIO_clear_retry_flags(b);
411
412                 b->retry_reason=0;
413                 ret=(int)SSL_do_handshake(ssl);
414
415                 switch (SSL_get_error(ssl,(int)ret))
416                         {
417                 case SSL_ERROR_WANT_READ:
418                         BIO_set_flags(b,
419                                 BIO_FLAGS_READ|BIO_FLAGS_SHOULD_RETRY);
420                         break;
421                 case SSL_ERROR_WANT_WRITE:
422                         BIO_set_flags(b,
423                                 BIO_FLAGS_WRITE|BIO_FLAGS_SHOULD_RETRY);
424                         break;
425                 case SSL_ERROR_WANT_CONNECT:
426                         BIO_set_flags(b,
427                                 BIO_FLAGS_IO_SPECIAL|BIO_FLAGS_SHOULD_RETRY);
428                         b->retry_reason=b->next_bio->retry_reason;
429                         break;
430                 default:
431                         break;
432                         }
433                 break;
434         case BIO_CTRL_DUP:
435                 dbio=(BIO *)ptr;
436                 if (((BIO_SSL *)dbio->ptr)->ssl != NULL)
437                         SSL_free(((BIO_SSL *)dbio->ptr)->ssl);
438                 ((BIO_SSL *)dbio->ptr)->ssl=SSL_dup(ssl);
439                 ((BIO_SSL *)dbio->ptr)->renegotiate_count=
440                         ((BIO_SSL *)b->ptr)->renegotiate_count;
441                 ((BIO_SSL *)dbio->ptr)->byte_count=
442                         ((BIO_SSL *)b->ptr)->byte_count;
443                 ((BIO_SSL *)dbio->ptr)->renegotiate_timeout=
444                         ((BIO_SSL *)b->ptr)->renegotiate_timeout;
445                 ((BIO_SSL *)dbio->ptr)->last_time=
446                         ((BIO_SSL *)b->ptr)->last_time;
447                 ret=(((BIO_SSL *)dbio->ptr)->ssl != NULL);
448                 break;
449         case BIO_C_GET_FD:
450                 ret=BIO_ctrl(ssl->rbio,cmd,num,ptr);
451                 break;
452         case BIO_CTRL_SET_CALLBACK:
453                 {
454                 union void_fn_to_char_u tmp_cb;
455
456                 tmp_cb.char_p = ptr;
457                 SSL_set_info_callback(ssl,tmp_cb.fn_p);
458                 }
459                 break;
460         case BIO_CTRL_GET_CALLBACK:
461                 {
462                 void (**fptr)();
463
464                 fptr=(void (**)())ptr;
465                 *fptr=SSL_get_info_callback(ssl);
466                 }
467                 break;
468         default:
469                 ret=BIO_ctrl(ssl->rbio,cmd,num,ptr);
470                 break;
471                 }
472         return(ret);
473         }
474
475 static int ssl_puts(BIO *bp, char *str)
476         {
477         int n,ret;
478
479         n=strlen(str);
480         ret=BIO_write(bp,str,n);
481         return(ret);
482         }
483
484 BIO *BIO_new_buffer_ssl_connect(SSL_CTX *ctx)
485         {
486         BIO *ret=NULL,*buf=NULL,*ssl=NULL;
487
488         if ((buf=BIO_new(BIO_f_buffer())) == NULL)
489                 return(NULL);
490         if ((ssl=BIO_new_ssl_connect(ctx)) == NULL)
491                 goto err;
492         if ((ret=BIO_push(buf,ssl)) == NULL)
493                 goto err;
494         return(ret);
495 err:
496         if (buf != NULL) BIO_free(buf);
497         if (ssl != NULL) BIO_free(ssl);
498         return(NULL);
499         }
500
501 BIO *BIO_new_ssl_connect(SSL_CTX *ctx)
502         {
503         BIO *ret=NULL,*con=NULL,*ssl=NULL;
504
505         if ((con=BIO_new(BIO_s_connect())) == NULL)
506                 return(NULL);
507         if ((ssl=BIO_new_ssl(ctx,1)) == NULL)
508                 goto err;
509         if ((ret=BIO_push(ssl,con)) == NULL)
510                 goto err;
511         return(ret);
512 err:
513         if (con != NULL) BIO_free(con);
514         if (ret != NULL) BIO_free(ret);
515         return(NULL);
516         }
517
518 BIO *BIO_new_ssl(SSL_CTX *ctx, int client)
519         {
520         BIO *ret;
521         SSL *ssl;
522
523         if ((ret=BIO_new(BIO_f_ssl())) == NULL)
524                 return(NULL);
525         if ((ssl=SSL_new(ctx)) == NULL)
526                 {
527                 BIO_free(ret);
528                 return(NULL);
529                 }
530         if (client)
531                 SSL_set_connect_state(ssl);
532         else
533                 SSL_set_accept_state(ssl);
534                 
535         BIO_set_ssl(ret,ssl,BIO_CLOSE);
536         return(ret);
537         }
538
539 int BIO_ssl_copy_session_id(BIO *t, BIO *f)
540         {
541         t=BIO_find_type(t,BIO_TYPE_SSL);
542         f=BIO_find_type(f,BIO_TYPE_SSL);
543         if ((t == NULL) || (f == NULL))
544                 return(0);
545         if (    (((BIO_SSL *)t->ptr)->ssl == NULL) || 
546                 (((BIO_SSL *)f->ptr)->ssl == NULL))
547                 return(0);
548         SSL_copy_session_id(((BIO_SSL *)t->ptr)->ssl,((BIO_SSL *)f->ptr)->ssl);
549         return(1);
550         }
551
552 void BIO_ssl_shutdown(BIO *b)
553         {
554         SSL *s;
555
556         while (b != NULL)
557                 {
558                 if (b->method->type == BIO_TYPE_SSL)
559                         {
560                         s=((BIO_SSL *)b->ptr)->ssl;
561                         SSL_shutdown(s);
562                         break;
563                         }
564                 b=b->next_bio;
565                 }
566         }