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