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