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