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