Create BIO_read_ex() which handles size_t arguments
[openssl.git] / ssl / bio_ssl.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <errno.h>
14 #include <openssl/crypto.h>
15 #include "internal/bio.h"
16 #include <openssl/err.h>
17 #include "ssl_locl.h"
18
19 static int ssl_write(BIO *h, const char *buf, int num);
20 static int ssl_read(BIO *b, char *out, size_t outl, size_t *read);
21 static int ssl_puts(BIO *h, const char *str);
22 static long ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2);
23 static int ssl_new(BIO *h);
24 static int ssl_free(BIO *data);
25 static long ssl_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
26 typedef struct bio_ssl_st {
27     SSL *ssl;                   /* The ssl handle :-) */
28     /* re-negotiate every time the total number of bytes is this size */
29     int num_renegotiates;
30     unsigned long renegotiate_count;
31     unsigned long byte_count;
32     unsigned long renegotiate_timeout;
33     unsigned long last_time;
34 } BIO_SSL;
35
36 static const BIO_METHOD methods_sslp = {
37     BIO_TYPE_SSL, "ssl",
38     ssl_write,
39     ssl_read,
40     NULL,
41     ssl_puts,
42     NULL,                       /* ssl_gets, */
43     ssl_ctrl,
44     ssl_new,
45     ssl_free,
46     ssl_callback_ctrl,
47 };
48
49 const BIO_METHOD *BIO_f_ssl(void)
50 {
51     return (&methods_sslp);
52 }
53
54 static int ssl_new(BIO *bi)
55 {
56     BIO_SSL *bs = OPENSSL_zalloc(sizeof(*bs));
57
58     if (bs == NULL) {
59         BIOerr(BIO_F_SSL_NEW, ERR_R_MALLOC_FAILURE);
60         return (0);
61     }
62     BIO_set_init(bi, 0);
63     BIO_set_data(bi, bs);
64     /* Clear all flags */
65     BIO_clear_flags(bi, ~0);
66
67     return 1;
68 }
69
70 static int ssl_free(BIO *a)
71 {
72     BIO_SSL *bs;
73
74     if (a == NULL)
75         return (0);
76     bs = BIO_get_data(a);
77     if (bs->ssl != NULL)
78         SSL_shutdown(bs->ssl);
79     if (BIO_get_shutdown(a)) {
80         if (BIO_get_init(a))
81             SSL_free(bs->ssl);
82         /* Clear all flags */
83         BIO_clear_flags(a, ~0);
84         BIO_set_init(a, 0);
85     }
86     OPENSSL_free(bs);
87     return 1;
88 }
89
90 static int ssl_read(BIO *b, char *out, size_t outl, size_t *read)
91 {
92     int ret = 1;
93     BIO_SSL *sb;
94     SSL *ssl;
95     int retry_reason = 0;
96     int r = 0;
97
98     if (out == NULL)
99         return (0);
100     sb = BIO_get_data(b);
101     ssl = sb->ssl;
102
103     BIO_clear_retry_flags(b);
104
105     if (outl > INT_MAX)
106         return -1;
107
108     ret = SSL_read(ssl, out, outl);
109
110     switch (SSL_get_error(ssl, ret)) {
111     case SSL_ERROR_NONE:
112         if (ret <= 0)
113             break;
114         if (sb->renegotiate_count > 0) {
115             sb->byte_count += ret;
116             if (sb->byte_count > sb->renegotiate_count) {
117                 sb->byte_count = 0;
118                 sb->num_renegotiates++;
119                 SSL_renegotiate(ssl);
120                 r = 1;
121             }
122         }
123         if ((sb->renegotiate_timeout > 0) && (!r)) {
124             unsigned long tm;
125
126             tm = (unsigned long)time(NULL);
127             if (tm > sb->last_time + sb->renegotiate_timeout) {
128                 sb->last_time = tm;
129                 sb->num_renegotiates++;
130                 SSL_renegotiate(ssl);
131             }
132         }
133
134         break;
135     case SSL_ERROR_WANT_READ:
136         BIO_set_retry_read(b);
137         break;
138     case SSL_ERROR_WANT_WRITE:
139         BIO_set_retry_write(b);
140         break;
141     case SSL_ERROR_WANT_X509_LOOKUP:
142         BIO_set_retry_special(b);
143         retry_reason = BIO_RR_SSL_X509_LOOKUP;
144         break;
145     case SSL_ERROR_WANT_ACCEPT:
146         BIO_set_retry_special(b);
147         retry_reason = BIO_RR_ACCEPT;
148         break;
149     case SSL_ERROR_WANT_CONNECT:
150         BIO_set_retry_special(b);
151         retry_reason = BIO_RR_CONNECT;
152         break;
153     case SSL_ERROR_SYSCALL:
154     case SSL_ERROR_SSL:
155     case SSL_ERROR_ZERO_RETURN:
156     default:
157         break;
158     }
159
160     BIO_set_retry_reason(b, retry_reason);
161
162     if (ret < 0)
163         return ret;
164
165     *read = (size_t)ret;
166
167     return 1;
168 }
169
170 static int ssl_write(BIO *b, const char *out, int outl)
171 {
172     int ret, r = 0;
173     int retry_reason = 0;
174     SSL *ssl;
175     BIO_SSL *bs;
176
177     if (out == NULL)
178         return (0);
179     bs = BIO_get_data(b);
180     ssl = bs->ssl;
181
182     BIO_clear_retry_flags(b);
183
184     /*
185      * ret=SSL_do_handshake(ssl); if (ret > 0)
186      */
187     ret = SSL_write(ssl, out, outl);
188
189     switch (SSL_get_error(ssl, ret)) {
190     case SSL_ERROR_NONE:
191         if (ret <= 0)
192             break;
193         if (bs->renegotiate_count > 0) {
194             bs->byte_count += ret;
195             if (bs->byte_count > bs->renegotiate_count) {
196                 bs->byte_count = 0;
197                 bs->num_renegotiates++;
198                 SSL_renegotiate(ssl);
199                 r = 1;
200             }
201         }
202         if ((bs->renegotiate_timeout > 0) && (!r)) {
203             unsigned long tm;
204
205             tm = (unsigned long)time(NULL);
206             if (tm > bs->last_time + bs->renegotiate_timeout) {
207                 bs->last_time = tm;
208                 bs->num_renegotiates++;
209                 SSL_renegotiate(ssl);
210             }
211         }
212         break;
213     case SSL_ERROR_WANT_WRITE:
214         BIO_set_retry_write(b);
215         break;
216     case SSL_ERROR_WANT_READ:
217         BIO_set_retry_read(b);
218         break;
219     case SSL_ERROR_WANT_X509_LOOKUP:
220         BIO_set_retry_special(b);
221         retry_reason = BIO_RR_SSL_X509_LOOKUP;
222         break;
223     case SSL_ERROR_WANT_CONNECT:
224         BIO_set_retry_special(b);
225         retry_reason = BIO_RR_CONNECT;
226     case SSL_ERROR_SYSCALL:
227     case SSL_ERROR_SSL:
228     default:
229         break;
230     }
231
232     BIO_set_retry_reason(b, retry_reason);
233     return ret;
234 }
235
236 static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr)
237 {
238     SSL **sslp, *ssl;
239     BIO_SSL *bs, *dbs;
240     BIO *dbio, *bio;
241     long ret = 1;
242     BIO *next;
243
244     bs = BIO_get_data(b);
245     next = BIO_next(b);
246     ssl = bs->ssl;
247     if ((ssl == NULL) && (cmd != BIO_C_SET_SSL))
248         return (0);
249     switch (cmd) {
250     case BIO_CTRL_RESET:
251         SSL_shutdown(ssl);
252
253         if (ssl->handshake_func == ssl->method->ssl_connect)
254             SSL_set_connect_state(ssl);
255         else if (ssl->handshake_func == ssl->method->ssl_accept)
256             SSL_set_accept_state(ssl);
257
258         if (!SSL_clear(ssl)) {
259             ret = 0;
260             break;
261         }
262
263         if (next != NULL)
264             ret = BIO_ctrl(next, cmd, num, ptr);
265         else if (ssl->rbio != NULL)
266             ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);
267         else
268             ret = 1;
269         break;
270     case BIO_CTRL_INFO:
271         ret = 0;
272         break;
273     case BIO_C_SSL_MODE:
274         if (num)                /* client mode */
275             SSL_set_connect_state(ssl);
276         else
277             SSL_set_accept_state(ssl);
278         break;
279     case BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT:
280         ret = bs->renegotiate_timeout;
281         if (num < 60)
282             num = 5;
283         bs->renegotiate_timeout = (unsigned long)num;
284         bs->last_time = (unsigned long)time(NULL);
285         break;
286     case BIO_C_SET_SSL_RENEGOTIATE_BYTES:
287         ret = bs->renegotiate_count;
288         if ((long)num >= 512)
289             bs->renegotiate_count = (unsigned long)num;
290         break;
291     case BIO_C_GET_SSL_NUM_RENEGOTIATES:
292         ret = bs->num_renegotiates;
293         break;
294     case BIO_C_SET_SSL:
295         if (ssl != NULL) {
296             ssl_free(b);
297             if (!ssl_new(b))
298                 return 0;
299         }
300         BIO_set_shutdown(b, num);
301         ssl = (SSL *)ptr;
302         bs->ssl = ssl;
303         bio = SSL_get_rbio(ssl);
304         if (bio != NULL) {
305             if (next != NULL)
306                 BIO_push(bio, next);
307             BIO_set_next(b, bio);
308             BIO_up_ref(bio);
309         }
310         BIO_set_init(b, 1);
311         break;
312     case BIO_C_GET_SSL:
313         if (ptr != NULL) {
314             sslp = (SSL **)ptr;
315             *sslp = ssl;
316         } else
317             ret = 0;
318         break;
319     case BIO_CTRL_GET_CLOSE:
320         ret = BIO_get_shutdown(b);
321         break;
322     case BIO_CTRL_SET_CLOSE:
323         BIO_set_shutdown(b, (int)num);
324         break;
325     case BIO_CTRL_WPENDING:
326         ret = BIO_ctrl(ssl->wbio, cmd, num, ptr);
327         break;
328     case BIO_CTRL_PENDING:
329         ret = SSL_pending(ssl);
330         if (ret == 0)
331             ret = BIO_pending(ssl->rbio);
332         break;
333     case BIO_CTRL_FLUSH:
334         BIO_clear_retry_flags(b);
335         ret = BIO_ctrl(ssl->wbio, cmd, num, ptr);
336         BIO_copy_next_retry(b);
337         break;
338     case BIO_CTRL_PUSH:
339         if ((next != NULL) && (next != ssl->rbio)) {
340             /*
341              * We are going to pass ownership of next to the SSL object...but
342              * we don't own a reference to pass yet - so up ref
343              */
344             BIO_up_ref(next);
345             SSL_set_bio(ssl, next, next);
346         }
347         break;
348     case BIO_CTRL_POP:
349         /* Only detach if we are the BIO explicitly being popped */
350         if (b == ptr) {
351             /* This will clear the reference we obtained during push */
352             SSL_set_bio(ssl, NULL, NULL);
353         }
354         break;
355     case BIO_C_DO_STATE_MACHINE:
356         BIO_clear_retry_flags(b);
357
358         BIO_set_retry_reason(b, 0);
359         ret = (int)SSL_do_handshake(ssl);
360
361         switch (SSL_get_error(ssl, (int)ret)) {
362         case SSL_ERROR_WANT_READ:
363             BIO_set_flags(b, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY);
364             break;
365         case SSL_ERROR_WANT_WRITE:
366             BIO_set_flags(b, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY);
367             break;
368         case SSL_ERROR_WANT_CONNECT:
369             BIO_set_flags(b, BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY);
370             BIO_set_retry_reason(b, BIO_get_retry_reason(next));
371             break;
372         case SSL_ERROR_WANT_X509_LOOKUP:
373             BIO_set_retry_special(b);
374             BIO_set_retry_reason(b, BIO_RR_SSL_X509_LOOKUP);
375             break;
376         default:
377             break;
378         }
379         break;
380     case BIO_CTRL_DUP:
381         dbio = (BIO *)ptr;
382         dbs = BIO_get_data(dbio);
383         SSL_free(dbs->ssl);
384         dbs->ssl = SSL_dup(ssl);
385         dbs->num_renegotiates = bs->num_renegotiates;
386         dbs->renegotiate_count = bs->renegotiate_count;
387         dbs->byte_count = bs->byte_count;
388         dbs->renegotiate_timeout = bs->renegotiate_timeout;
389         dbs->last_time = bs->last_time;
390         ret = (dbs->ssl != NULL);
391         break;
392     case BIO_C_GET_FD:
393         ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);
394         break;
395     case BIO_CTRL_SET_CALLBACK:
396         {
397 #if 0                           /* FIXME: Should this be used? -- Richard
398                                  * Levitte */
399             SSLerr(SSL_F_SSL_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
400             ret = -1;
401 #else
402             ret = 0;
403 #endif
404         }
405         break;
406     case BIO_CTRL_GET_CALLBACK:
407         {
408             void (**fptr) (const SSL *xssl, int type, int val);
409
410             fptr = (void (**)(const SSL *xssl, int type, int val))ptr;
411             *fptr = SSL_get_info_callback(ssl);
412         }
413         break;
414     default:
415         ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);
416         break;
417     }
418     return (ret);
419 }
420
421 static long ssl_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
422 {
423     SSL *ssl;
424     BIO_SSL *bs;
425     long ret = 1;
426
427     bs = BIO_get_data(b);
428     ssl = bs->ssl;
429     switch (cmd) {
430     case BIO_CTRL_SET_CALLBACK:
431         {
432             /*
433              * FIXME: setting this via a completely different prototype seems
434              * like a crap idea
435              */
436             SSL_set_info_callback(ssl, (void (*)(const SSL *, int, int))fp);
437         }
438         break;
439     default:
440         ret = BIO_callback_ctrl(ssl->rbio, cmd, fp);
441         break;
442     }
443     return (ret);
444 }
445
446 static int ssl_puts(BIO *bp, const char *str)
447 {
448     int n, ret;
449
450     n = strlen(str);
451     ret = BIO_write(bp, str, n);
452     return (ret);
453 }
454
455 BIO *BIO_new_buffer_ssl_connect(SSL_CTX *ctx)
456 {
457 #ifndef OPENSSL_NO_SOCK
458     BIO *ret = NULL, *buf = NULL, *ssl = NULL;
459
460     if ((buf = BIO_new(BIO_f_buffer())) == NULL)
461         return (NULL);
462     if ((ssl = BIO_new_ssl_connect(ctx)) == NULL)
463         goto err;
464     if ((ret = BIO_push(buf, ssl)) == NULL)
465         goto err;
466     return (ret);
467  err:
468     BIO_free(buf);
469     BIO_free(ssl);
470 #endif
471     return (NULL);
472 }
473
474 BIO *BIO_new_ssl_connect(SSL_CTX *ctx)
475 {
476 #ifndef OPENSSL_NO_SOCK
477     BIO *ret = NULL, *con = NULL, *ssl = NULL;
478
479     if ((con = BIO_new(BIO_s_connect())) == NULL)
480         return (NULL);
481     if ((ssl = BIO_new_ssl(ctx, 1)) == NULL)
482         goto err;
483     if ((ret = BIO_push(ssl, con)) == NULL)
484         goto err;
485     return (ret);
486  err:
487     BIO_free(con);
488 #endif
489     return (NULL);
490 }
491
492 BIO *BIO_new_ssl(SSL_CTX *ctx, int client)
493 {
494     BIO *ret;
495     SSL *ssl;
496
497     if ((ret = BIO_new(BIO_f_ssl())) == NULL)
498         return (NULL);
499     if ((ssl = SSL_new(ctx)) == NULL) {
500         BIO_free(ret);
501         return (NULL);
502     }
503     if (client)
504         SSL_set_connect_state(ssl);
505     else
506         SSL_set_accept_state(ssl);
507
508     BIO_set_ssl(ret, ssl, BIO_CLOSE);
509     return (ret);
510 }
511
512 int BIO_ssl_copy_session_id(BIO *t, BIO *f)
513 {
514     BIO_SSL *tdata, *fdata;
515     t = BIO_find_type(t, BIO_TYPE_SSL);
516     f = BIO_find_type(f, BIO_TYPE_SSL);
517     if ((t == NULL) || (f == NULL))
518         return 0;
519     tdata = BIO_get_data(t);
520     fdata = BIO_get_data(f);
521     if ((tdata->ssl == NULL) || (fdata->ssl == NULL))
522         return (0);
523     if (!SSL_copy_session_id(tdata->ssl, (fdata->ssl)))
524         return 0;
525     return (1);
526 }
527
528 void BIO_ssl_shutdown(BIO *b)
529 {
530     SSL *s;
531
532     b = BIO_find_type(b, BIO_TYPE_SSL);
533     if (b == NULL)
534         return;
535
536     s = BIO_get_data(b);
537     SSL_shutdown(s);
538 }