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