1 /*
2 * Copyright 1995-2025 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 #include "internal/ssl_unwrap.h"
19 #include "internal/sockets.h"
20
21 static int ssl_write(BIO *h, const char *buf, size_t size, size_t *written);
22 static int ssl_read(BIO *b, char *buf, size_t size, size_t *readbytes);
23 static int ssl_puts(BIO *h, const char *str);
24 static long ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2);
25 static int ssl_new(BIO *h);
26 static int ssl_free(BIO *data);
27 static long ssl_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
28 typedef struct bio_ssl_st {
29 SSL *ssl; /* The ssl handle :-) */
30 /*
31 * Re-negotiate every time the total number of bytes is this size
32 * or when timeout expires.
33 * There is no proper support for TLS-1.3 or QUIC yet.
34 */
35 int num_renegotiates;
36 unsigned long renegotiate_count;
37 size_t byte_count;
38 unsigned long renegotiate_timeout;
39 unsigned long last_time;
40 } BIO_SSL;
41
42 static const BIO_METHOD methods_sslp = {
43 BIO_TYPE_SSL,
44 "ssl",
45 ssl_write,
46 NULL, /* ssl_write_old, */
47 ssl_read,
48 NULL, /* ssl_read_old, */
49 ssl_puts,
50 NULL, /* ssl_gets, */
51 ssl_ctrl,
52 ssl_new,
53 ssl_free,
54 ssl_callback_ctrl,
55 };
56
BIO_f_ssl(void)57 const BIO_METHOD *BIO_f_ssl(void)
58 {
59 return &methods_sslp;
60 }
61
ssl_new(BIO * bi)62 static int ssl_new(BIO *bi)
63 {
64 BIO_SSL *bs = OPENSSL_zalloc(sizeof(*bs));
65
66 if (bs == NULL)
67 return 0;
68 BIO_set_init(bi, 0);
69 BIO_set_data(bi, bs);
70 /* Clear all flags */
71 BIO_clear_flags(bi, ~0);
72
73 return 1;
74 }
75
ssl_free(BIO * a)76 static int ssl_free(BIO *a)
77 {
78 BIO_SSL *bs;
79
80 if (a == NULL)
81 return 0;
82 bs = BIO_get_data(a);
83 if (BIO_get_shutdown(a)) {
84 if (bs->ssl != NULL && !SSL_in_init(bs->ssl))
85 SSL_shutdown(bs->ssl);
86 if (BIO_get_init(a))
87 SSL_free(bs->ssl);
88 BIO_clear_flags(a, ~0); /* Clear all flags */
89 BIO_set_init(a, 0);
90 }
91 OPENSSL_free(bs);
92 return 1;
93 }
94
ssl_read(BIO * b,char * buf,size_t size,size_t * readbytes)95 static int ssl_read(BIO *b, char *buf, size_t size, size_t *readbytes)
96 {
97 int ret = 1;
98 BIO_SSL *sb;
99 SSL *ssl;
100 int retry_reason = 0;
101 int r = 0;
102
103 if (buf == NULL)
104 return 0;
105 sb = BIO_get_data(b);
106 ssl = sb->ssl;
107
108 BIO_clear_retry_flags(b);
109
110 ret = ssl_read_internal(ssl, buf, size, readbytes);
111
112 switch (SSL_get_error(ssl, ret)) {
113 case SSL_ERROR_NONE:
114 if (sb->renegotiate_count > 0) {
115 sb->byte_count += *readbytes;
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 return ret;
163 }
164
ssl_write(BIO * b,const char * buf,size_t size,size_t * written)165 static int ssl_write(BIO *b, const char *buf, size_t size, size_t *written)
166 {
167 int ret, r = 0;
168 int retry_reason = 0;
169 SSL *ssl;
170 BIO_SSL *bs;
171
172 if (buf == NULL)
173 return 0;
174 bs = BIO_get_data(b);
175 ssl = bs->ssl;
176
177 BIO_clear_retry_flags(b);
178
179 ret = ssl_write_internal(ssl, buf, size, 0, written);
180
181 switch (SSL_get_error(ssl, ret)) {
182 case SSL_ERROR_NONE:
183 if (bs->renegotiate_count > 0) {
184 bs->byte_count += *written;
185 if (bs->byte_count > bs->renegotiate_count) {
186 bs->byte_count = 0;
187 bs->num_renegotiates++;
188 SSL_renegotiate(ssl);
189 r = 1;
190 }
191 }
192 if ((bs->renegotiate_timeout > 0) && (!r)) {
193 unsigned long tm;
194
195 tm = (unsigned long)time(NULL);
196 if (tm > bs->last_time + bs->renegotiate_timeout) {
197 bs->last_time = tm;
198 bs->num_renegotiates++;
199 SSL_renegotiate(ssl);
200 }
201 }
202 break;
203 case SSL_ERROR_WANT_WRITE:
204 BIO_set_retry_write(b);
205 break;
206 case SSL_ERROR_WANT_READ:
207 BIO_set_retry_read(b);
208 break;
209 case SSL_ERROR_WANT_X509_LOOKUP:
210 BIO_set_retry_special(b);
211 retry_reason = BIO_RR_SSL_X509_LOOKUP;
212 break;
213 case SSL_ERROR_WANT_CONNECT:
214 BIO_set_retry_special(b);
215 retry_reason = BIO_RR_CONNECT;
216 case SSL_ERROR_SYSCALL:
217 case SSL_ERROR_SSL:
218 default:
219 break;
220 }
221
222 BIO_set_retry_reason(b, retry_reason);
223
224 return ret;
225 }
226
ssl_ctrl(BIO * b,int cmd,long num,void * ptr)227 static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr)
228 {
229 SSL **sslp, *ssl;
230 BIO_SSL *bs, *dbs;
231 BIO *dbio, *bio;
232 long ret = 1;
233 BIO *next;
234 SSL_CONNECTION *sc = NULL;
235
236 bs = BIO_get_data(b);
237 next = BIO_next(b);
238 ssl = bs->ssl;
239 if (ssl == NULL && cmd != BIO_C_SET_SSL)
240 return 0;
241 switch (cmd) {
242 case BIO_CTRL_RESET:
243 /* TODO(QUIC FUTURE): Add support when SSL_clear() is supported */
244 if ((sc = SSL_CONNECTION_FROM_SSL_ONLY(ssl)) == NULL)
245 return 0;
246
247 SSL_shutdown(ssl);
248
249 if (sc->handshake_func == ssl->method->ssl_connect)
250 SSL_set_connect_state(ssl);
251 else if (sc->handshake_func == ssl->method->ssl_accept)
252 SSL_set_accept_state(ssl);
253
254 if (!SSL_clear(ssl)) {
255 ret = 0;
256 break;
257 }
258
259 if (next != NULL)
260 ret = BIO_ctrl(next, cmd, num, ptr);
261 else if (sc->rbio != NULL)
262 ret = BIO_ctrl(sc->rbio, cmd, num, ptr);
263 else
264 ret = 1;
265 break;
266 case BIO_CTRL_INFO:
267 ret = 0;
268 break;
269 case BIO_C_SSL_MODE:
270 if (num) /* client mode */
271 SSL_set_connect_state(ssl);
272 else
273 SSL_set_accept_state(ssl);
274 break;
275 case BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT:
276 ret = bs->renegotiate_timeout;
277 if (num < 60)
278 num = 5;
279 bs->renegotiate_timeout = (unsigned long)num;
280 bs->last_time = (unsigned long)time(NULL);
281 break;
282 case BIO_C_SET_SSL_RENEGOTIATE_BYTES:
283 ret = bs->renegotiate_count;
284 if ((long)num >= 512)
285 bs->renegotiate_count = (unsigned long)num;
286 break;
287 case BIO_C_GET_SSL_NUM_RENEGOTIATES:
288 ret = bs->num_renegotiates;
289 break;
290 case BIO_C_SET_SSL:
291 if (ssl != NULL) {
292 ssl_free(b);
293 if (!ssl_new(b))
294 return 0;
295 bs = BIO_get_data(b);
296 }
297 BIO_set_shutdown(b, num);
298 ssl = (SSL *)ptr;
299 bs->ssl = ssl;
300 bio = SSL_get_rbio(ssl);
301 if (bio != NULL) {
302 if (!BIO_up_ref(bio)) {
303 ret = 0;
304 break;
305 }
306 if (next != NULL)
307 BIO_push(bio, next);
308 BIO_set_next(b, 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_get_wbio(ssl), cmd, num, ptr);
327 break;
328 case BIO_CTRL_PENDING:
329 ret = SSL_pending(ssl);
330 if (ret == 0)
331 ret = BIO_pending(SSL_get_rbio(ssl));
332 break;
333 case BIO_CTRL_FLUSH:
334 BIO_clear_retry_flags(b);
335 ret = BIO_ctrl(SSL_get_wbio(ssl), cmd, num, ptr);
336 BIO_copy_next_retry(b);
337 break;
338 case BIO_CTRL_PUSH:
339 if ((next != NULL) && (next != SSL_get_rbio(ssl))) {
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 if (!BIO_up_ref(next))
345 ret = 0;
346 else
347 SSL_set_bio(ssl, next, next);
348 }
349 break;
350 case BIO_CTRL_POP:
351 /* Only detach if we are the BIO explicitly being popped */
352 if (b == ptr) {
353 /* This will clear the reference we obtained during push */
354 SSL_set_bio(ssl, NULL, NULL);
355 }
356 break;
357 case BIO_C_DO_STATE_MACHINE:
358 BIO_clear_retry_flags(b);
359
360 BIO_set_retry_reason(b, 0);
361 ret = (int)SSL_do_handshake(ssl);
362
363 switch (SSL_get_error(ssl, (int)ret)) {
364 case SSL_ERROR_WANT_READ:
365 BIO_set_flags(b, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY);
366 break;
367 case SSL_ERROR_WANT_WRITE:
368 BIO_set_flags(b, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY);
369 break;
370 case SSL_ERROR_WANT_CONNECT:
371 BIO_set_flags(b, BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY);
372 BIO_set_retry_reason(b, BIO_get_retry_reason(next));
373 break;
374 case SSL_ERROR_WANT_X509_LOOKUP:
375 BIO_set_retry_special(b);
376 BIO_set_retry_reason(b, BIO_RR_SSL_X509_LOOKUP);
377 break;
378 default:
379 break;
380 }
381 break;
382 case BIO_CTRL_DUP:
383 dbio = (BIO *)ptr;
384 dbs = BIO_get_data(dbio);
385 SSL_free(dbs->ssl);
386 dbs->ssl = SSL_dup(ssl);
387 dbs->num_renegotiates = bs->num_renegotiates;
388 dbs->renegotiate_count = bs->renegotiate_count;
389 dbs->byte_count = bs->byte_count;
390 dbs->renegotiate_timeout = bs->renegotiate_timeout;
391 dbs->last_time = bs->last_time;
392 ret = (dbs->ssl != NULL);
393 break;
394 case BIO_C_GET_FD:
395 ret = BIO_ctrl(SSL_get_rbio(ssl), cmd, num, ptr);
396 break;
397 case BIO_CTRL_SET_CALLBACK:
398 ret = 0; /* use callback ctrl */
399 break;
400 case BIO_CTRL_GET_RPOLL_DESCRIPTOR:
401 if (!SSL_get_rpoll_descriptor(ssl, (BIO_POLL_DESCRIPTOR *)ptr))
402 ret = 0;
403 break;
404 case BIO_CTRL_GET_WPOLL_DESCRIPTOR:
405 if (!SSL_get_wpoll_descriptor(ssl, (BIO_POLL_DESCRIPTOR *)ptr))
406 ret = 0;
407 break;
408 default:
409 ret = BIO_ctrl(SSL_get_rbio(ssl), cmd, num, ptr);
410 break;
411 }
412 return ret;
413 }
414
ssl_callback_ctrl(BIO * b,int cmd,BIO_info_cb * fp)415 static long ssl_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
416 {
417 SSL *ssl;
418 BIO_SSL *bs;
419 long ret = 1;
420
421 bs = BIO_get_data(b);
422 ssl = bs->ssl;
423 switch (cmd) {
424 case BIO_CTRL_SET_CALLBACK:
425 ret = BIO_callback_ctrl(SSL_get_rbio(ssl), cmd, fp);
426 break;
427 default:
428 ret = 0;
429 break;
430 }
431 return ret;
432 }
433
ssl_puts(BIO * bp,const char * str)434 static int ssl_puts(BIO *bp, const char *str)
435 {
436 int n, ret;
437
438 n = strlen(str);
439 ret = BIO_write(bp, str, n);
440 return ret;
441 }
442
BIO_new_buffer_ssl_connect(SSL_CTX * ctx)443 BIO *BIO_new_buffer_ssl_connect(SSL_CTX *ctx)
444 {
445 #ifndef OPENSSL_NO_SOCK
446 BIO *ret = NULL, *buf = NULL, *ssl = NULL;
447
448 # ifndef OPENSSL_NO_QUIC
449 if (ctx != NULL && IS_QUIC_CTX(ctx))
450 /* Never use buffering for QUIC. */
451 return BIO_new_ssl_connect(ctx);
452 # endif
453
454 if ((buf = BIO_new(BIO_f_buffer())) == NULL)
455 return NULL;
456 if ((ssl = BIO_new_ssl_connect(ctx)) == NULL)
457 goto err;
458 if ((ret = BIO_push(buf, ssl)) == NULL)
459 goto err;
460 return ret;
461 err:
462 BIO_free(buf);
463 BIO_free(ssl);
464 #endif
465 return NULL;
466 }
467
BIO_new_ssl_connect(SSL_CTX * ctx)468 BIO *BIO_new_ssl_connect(SSL_CTX *ctx)
469 {
470 #ifndef OPENSSL_NO_SOCK
471 BIO *ret = NULL, *con = NULL, *ssl = NULL;
472
473 if ((con = BIO_new(BIO_s_connect())) == NULL)
474 return NULL;
475
476 # ifndef OPENSSL_NO_QUIC
477 if (ctx != NULL && IS_QUIC_CTX(ctx))
478 if (!BIO_set_sock_type(con, SOCK_DGRAM))
479 goto err;
480 #endif
481
482 if ((ssl = BIO_new_ssl(ctx, 1)) == NULL)
483 goto err;
484 if ((ret = BIO_push(ssl, con)) == NULL)
485 goto err;
486 return ret;
487 err:
488 BIO_free(ssl);
489 BIO_free(con);
490 #endif
491 return NULL;
492 }
493
BIO_new_ssl(SSL_CTX * ctx,int client)494 BIO *BIO_new_ssl(SSL_CTX *ctx, int client)
495 {
496 BIO *ret;
497 SSL *ssl;
498
499 if ((ret = BIO_new(BIO_f_ssl())) == NULL)
500 return NULL;
501 if ((ssl = SSL_new(ctx)) == NULL) {
502 BIO_free(ret);
503 return NULL;
504 }
505 if (client)
506 SSL_set_connect_state(ssl);
507 else
508 SSL_set_accept_state(ssl);
509
510 BIO_set_ssl(ret, ssl, BIO_CLOSE);
511 return ret;
512 }
513
BIO_ssl_copy_session_id(BIO * t,BIO * f)514 int BIO_ssl_copy_session_id(BIO *t, BIO *f)
515 {
516 BIO_SSL *tdata, *fdata;
517 t = BIO_find_type(t, BIO_TYPE_SSL);
518 f = BIO_find_type(f, BIO_TYPE_SSL);
519 if ((t == NULL) || (f == NULL))
520 return 0;
521 tdata = BIO_get_data(t);
522 fdata = BIO_get_data(f);
523 if ((tdata->ssl == NULL) || (fdata->ssl == NULL))
524 return 0;
525 if (!SSL_copy_session_id(tdata->ssl, (fdata->ssl)))
526 return 0;
527 return 1;
528 }
529
BIO_ssl_shutdown(BIO * b)530 void BIO_ssl_shutdown(BIO *b)
531 {
532 BIO_SSL *bdata;
533
534 for (; b != NULL; b = BIO_next(b)) {
535 if (BIO_method_type(b) != BIO_TYPE_SSL)
536 continue;
537 bdata = BIO_get_data(b);
538 if (bdata != NULL && bdata->ssl != NULL)
539 SSL_shutdown(bdata->ssl);
540 }
541 }
542