xref: /openssl/ssl/bio_ssl.c (revision 38b051a1)
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 
BIO_f_ssl(void)51 const BIO_METHOD *BIO_f_ssl(void)
52 {
53     return &methods_sslp;
54 }
55 
ssl_new(BIO * bi)56 static 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 
ssl_free(BIO * a)72 static 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 
ssl_read(BIO * b,char * buf,size_t size,size_t * readbytes)91 static 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 
ssl_write(BIO * b,const char * buf,size_t size,size_t * written)161 static 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 
ssl_ctrl(BIO * b,int cmd,long num,void * ptr)223 static 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     SSL_CONNECTION *sc = NULL;
231 
232     bs = BIO_get_data(b);
233     next = BIO_next(b);
234     ssl = bs->ssl;
235     if ((ssl == NULL
236          || (sc = SSL_CONNECTION_FROM_SSL(ssl)) == NULL)
237         && cmd != BIO_C_SET_SSL)
238         return 0;
239     /* TODO(QUIC): The rbio/wbio might be from QUIC_CONNECTION instead */
240     switch (cmd) {
241     case BIO_CTRL_RESET:
242         SSL_shutdown(ssl);
243 
244         if (sc->handshake_func == ssl->method->ssl_connect)
245             SSL_set_connect_state(ssl);
246         else if (sc->handshake_func == ssl->method->ssl_accept)
247             SSL_set_accept_state(ssl);
248 
249         if (!SSL_clear(ssl)) {
250             ret = 0;
251             break;
252         }
253 
254         if (next != NULL)
255             ret = BIO_ctrl(next, cmd, num, ptr);
256         else if (sc->rbio != NULL)
257             ret = BIO_ctrl(sc->rbio, cmd, num, ptr);
258         else
259             ret = 1;
260         break;
261     case BIO_CTRL_INFO:
262         ret = 0;
263         break;
264     case BIO_C_SSL_MODE:
265         if (num)                /* client mode */
266             SSL_set_connect_state(ssl);
267         else
268             SSL_set_accept_state(ssl);
269         break;
270     case BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT:
271         ret = bs->renegotiate_timeout;
272         if (num < 60)
273             num = 5;
274         bs->renegotiate_timeout = (unsigned long)num;
275         bs->last_time = (unsigned long)time(NULL);
276         break;
277     case BIO_C_SET_SSL_RENEGOTIATE_BYTES:
278         ret = bs->renegotiate_count;
279         if ((long)num >= 512)
280             bs->renegotiate_count = (unsigned long)num;
281         break;
282     case BIO_C_GET_SSL_NUM_RENEGOTIATES:
283         ret = bs->num_renegotiates;
284         break;
285     case BIO_C_SET_SSL:
286         if (ssl != NULL) {
287             ssl_free(b);
288             if (!ssl_new(b))
289                 return 0;
290             bs = BIO_get_data(b);
291         }
292         BIO_set_shutdown(b, num);
293         ssl = (SSL *)ptr;
294         bs->ssl = ssl;
295         bio = SSL_get_rbio(ssl);
296         if (bio != NULL) {
297             if (next != NULL)
298                 BIO_push(bio, next);
299             BIO_set_next(b, bio);
300             BIO_up_ref(bio);
301         }
302         BIO_set_init(b, 1);
303         break;
304     case BIO_C_GET_SSL:
305         if (ptr != NULL) {
306             sslp = (SSL **)ptr;
307             *sslp = ssl;
308         } else
309             ret = 0;
310         break;
311     case BIO_CTRL_GET_CLOSE:
312         ret = BIO_get_shutdown(b);
313         break;
314     case BIO_CTRL_SET_CLOSE:
315         BIO_set_shutdown(b, (int)num);
316         break;
317     case BIO_CTRL_WPENDING:
318         ret = BIO_ctrl(sc->wbio, cmd, num, ptr);
319         break;
320     case BIO_CTRL_PENDING:
321         ret = SSL_pending(ssl);
322         if (ret == 0)
323             ret = BIO_pending(sc->rbio);
324         break;
325     case BIO_CTRL_FLUSH:
326         BIO_clear_retry_flags(b);
327         ret = BIO_ctrl(sc->wbio, cmd, num, ptr);
328         BIO_copy_next_retry(b);
329         break;
330     case BIO_CTRL_PUSH:
331         if ((next != NULL) && (next != sc->rbio)) {
332             /*
333              * We are going to pass ownership of next to the SSL object...but
334              * we don't own a reference to pass yet - so up ref
335              */
336             BIO_up_ref(next);
337             SSL_set_bio(ssl, next, next);
338         }
339         break;
340     case BIO_CTRL_POP:
341         /* Only detach if we are the BIO explicitly being popped */
342         if (b == ptr) {
343             /* This will clear the reference we obtained during push */
344             SSL_set_bio(ssl, NULL, NULL);
345         }
346         break;
347     case BIO_C_DO_STATE_MACHINE:
348         BIO_clear_retry_flags(b);
349 
350         BIO_set_retry_reason(b, 0);
351         ret = (int)SSL_do_handshake(ssl);
352 
353         switch (SSL_get_error(ssl, (int)ret)) {
354         case SSL_ERROR_WANT_READ:
355             BIO_set_flags(b, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY);
356             break;
357         case SSL_ERROR_WANT_WRITE:
358             BIO_set_flags(b, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY);
359             break;
360         case SSL_ERROR_WANT_CONNECT:
361             BIO_set_flags(b, BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY);
362             BIO_set_retry_reason(b, BIO_get_retry_reason(next));
363             break;
364         case SSL_ERROR_WANT_X509_LOOKUP:
365             BIO_set_retry_special(b);
366             BIO_set_retry_reason(b, BIO_RR_SSL_X509_LOOKUP);
367             break;
368         default:
369             break;
370         }
371         break;
372     case BIO_CTRL_DUP:
373         dbio = (BIO *)ptr;
374         dbs = BIO_get_data(dbio);
375         SSL_free(dbs->ssl);
376         dbs->ssl = SSL_dup(ssl);
377         dbs->num_renegotiates = bs->num_renegotiates;
378         dbs->renegotiate_count = bs->renegotiate_count;
379         dbs->byte_count = bs->byte_count;
380         dbs->renegotiate_timeout = bs->renegotiate_timeout;
381         dbs->last_time = bs->last_time;
382         ret = (dbs->ssl != NULL);
383         break;
384     case BIO_C_GET_FD:
385         ret = BIO_ctrl(sc->rbio, cmd, num, ptr);
386         break;
387     case BIO_CTRL_SET_CALLBACK:
388         ret = 0; /* use callback ctrl */
389         break;
390     default:
391         ret = BIO_ctrl(sc->rbio, cmd, num, ptr);
392         break;
393     }
394     return ret;
395 }
396 
ssl_callback_ctrl(BIO * b,int cmd,BIO_info_cb * fp)397 static long ssl_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
398 {
399     SSL *ssl;
400     BIO_SSL *bs;
401     long ret = 1;
402 
403     bs = BIO_get_data(b);
404     ssl = bs->ssl;
405     switch (cmd) {
406     case BIO_CTRL_SET_CALLBACK:
407         ret = BIO_callback_ctrl(SSL_get_rbio(ssl), cmd, fp);
408         break;
409     default:
410         ret = 0;
411         break;
412     }
413     return ret;
414 }
415 
ssl_puts(BIO * bp,const char * str)416 static int ssl_puts(BIO *bp, const char *str)
417 {
418     int n, ret;
419 
420     n = strlen(str);
421     ret = BIO_write(bp, str, n);
422     return ret;
423 }
424 
BIO_new_buffer_ssl_connect(SSL_CTX * ctx)425 BIO *BIO_new_buffer_ssl_connect(SSL_CTX *ctx)
426 {
427 #ifndef OPENSSL_NO_SOCK
428     BIO *ret = NULL, *buf = NULL, *ssl = NULL;
429 
430     if ((buf = BIO_new(BIO_f_buffer())) == NULL)
431         return NULL;
432     if ((ssl = BIO_new_ssl_connect(ctx)) == NULL)
433         goto err;
434     if ((ret = BIO_push(buf, ssl)) == NULL)
435         goto err;
436     return ret;
437  err:
438     BIO_free(buf);
439     BIO_free(ssl);
440 #endif
441     return NULL;
442 }
443 
BIO_new_ssl_connect(SSL_CTX * ctx)444 BIO *BIO_new_ssl_connect(SSL_CTX *ctx)
445 {
446 #ifndef OPENSSL_NO_SOCK
447     BIO *ret = NULL, *con = NULL, *ssl = NULL;
448 
449     if ((con = BIO_new(BIO_s_connect())) == NULL)
450         return NULL;
451     if ((ssl = BIO_new_ssl(ctx, 1)) == NULL)
452         goto err;
453     if ((ret = BIO_push(ssl, con)) == NULL)
454         goto err;
455     return ret;
456  err:
457     BIO_free(ssl);
458     BIO_free(con);
459 #endif
460     return NULL;
461 }
462 
BIO_new_ssl(SSL_CTX * ctx,int client)463 BIO *BIO_new_ssl(SSL_CTX *ctx, int client)
464 {
465     BIO *ret;
466     SSL *ssl;
467 
468     if ((ret = BIO_new(BIO_f_ssl())) == NULL)
469         return NULL;
470     if ((ssl = SSL_new(ctx)) == NULL) {
471         BIO_free(ret);
472         return NULL;
473     }
474     if (client)
475         SSL_set_connect_state(ssl);
476     else
477         SSL_set_accept_state(ssl);
478 
479     BIO_set_ssl(ret, ssl, BIO_CLOSE);
480     return ret;
481 }
482 
BIO_ssl_copy_session_id(BIO * t,BIO * f)483 int BIO_ssl_copy_session_id(BIO *t, BIO *f)
484 {
485     BIO_SSL *tdata, *fdata;
486     t = BIO_find_type(t, BIO_TYPE_SSL);
487     f = BIO_find_type(f, BIO_TYPE_SSL);
488     if ((t == NULL) || (f == NULL))
489         return 0;
490     tdata = BIO_get_data(t);
491     fdata = BIO_get_data(f);
492     if ((tdata->ssl == NULL) || (fdata->ssl == NULL))
493         return 0;
494     if (!SSL_copy_session_id(tdata->ssl, (fdata->ssl)))
495         return 0;
496     return 1;
497 }
498 
BIO_ssl_shutdown(BIO * b)499 void BIO_ssl_shutdown(BIO *b)
500 {
501     BIO_SSL *bdata;
502 
503     for (; b != NULL; b = BIO_next(b)) {
504         if (BIO_method_type(b) != BIO_TYPE_SSL)
505             continue;
506         bdata = BIO_get_data(b);
507         if (bdata != NULL && bdata->ssl != NULL)
508             SSL_shutdown(bdata->ssl);
509     }
510 }
511