2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
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
14 #include <openssl/crypto.h>
15 #include "internal/bio.h"
16 #include <openssl/err.h>
17 #include "ssl_local.h"
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 */
30 unsigned long renegotiate_count
;
32 unsigned long renegotiate_timeout
;
33 unsigned long last_time
;
36 static const BIO_METHOD methods_sslp
= {
40 NULL
, /* ssl_write_old, */
42 NULL
, /* ssl_read_old, */
51 const BIO_METHOD
*BIO_f_ssl(void)
56 static int ssl_new(BIO
*bi
)
58 BIO_SSL
*bs
= OPENSSL_zalloc(sizeof(*bs
));
61 ERR_raise(ERR_LIB_BIO
, ERR_R_MALLOC_FAILURE
);
67 BIO_clear_flags(bi
, ~0);
72 static int ssl_free(BIO
*a
)
79 if (BIO_get_shutdown(a
)) {
81 SSL_shutdown(bs
->ssl
);
84 BIO_clear_flags(a
, ~0); /* Clear all flags */
91 static int ssl_read(BIO
*b
, char *buf
, size_t size
, size_t *readbytes
)
101 sb
= BIO_get_data(b
);
104 BIO_clear_retry_flags(b
);
106 ret
= ssl_read_internal(ssl
, buf
, size
, readbytes
);
108 switch (SSL_get_error(ssl
, ret
)) {
110 if (sb
->renegotiate_count
> 0) {
111 sb
->byte_count
+= *readbytes
;
112 if (sb
->byte_count
> sb
->renegotiate_count
) {
114 sb
->num_renegotiates
++;
115 SSL_renegotiate(ssl
);
119 if ((sb
->renegotiate_timeout
> 0) && (!r
)) {
122 tm
= (unsigned long)time(NULL
);
123 if (tm
> sb
->last_time
+ sb
->renegotiate_timeout
) {
125 sb
->num_renegotiates
++;
126 SSL_renegotiate(ssl
);
131 case SSL_ERROR_WANT_READ
:
132 BIO_set_retry_read(b
);
134 case SSL_ERROR_WANT_WRITE
:
135 BIO_set_retry_write(b
);
137 case SSL_ERROR_WANT_X509_LOOKUP
:
138 BIO_set_retry_special(b
);
139 retry_reason
= BIO_RR_SSL_X509_LOOKUP
;
141 case SSL_ERROR_WANT_ACCEPT
:
142 BIO_set_retry_special(b
);
143 retry_reason
= BIO_RR_ACCEPT
;
145 case SSL_ERROR_WANT_CONNECT
:
146 BIO_set_retry_special(b
);
147 retry_reason
= BIO_RR_CONNECT
;
149 case SSL_ERROR_SYSCALL
:
151 case SSL_ERROR_ZERO_RETURN
:
156 BIO_set_retry_reason(b
, retry_reason
);
161 static int ssl_write(BIO
*b
, const char *buf
, size_t size
, size_t *written
)
164 int retry_reason
= 0;
170 bs
= BIO_get_data(b
);
173 BIO_clear_retry_flags(b
);
175 ret
= ssl_write_internal(ssl
, buf
, size
, written
);
177 switch (SSL_get_error(ssl
, ret
)) {
179 if (bs
->renegotiate_count
> 0) {
180 bs
->byte_count
+= *written
;
181 if (bs
->byte_count
> bs
->renegotiate_count
) {
183 bs
->num_renegotiates
++;
184 SSL_renegotiate(ssl
);
188 if ((bs
->renegotiate_timeout
> 0) && (!r
)) {
191 tm
= (unsigned long)time(NULL
);
192 if (tm
> bs
->last_time
+ bs
->renegotiate_timeout
) {
194 bs
->num_renegotiates
++;
195 SSL_renegotiate(ssl
);
199 case SSL_ERROR_WANT_WRITE
:
200 BIO_set_retry_write(b
);
202 case SSL_ERROR_WANT_READ
:
203 BIO_set_retry_read(b
);
205 case SSL_ERROR_WANT_X509_LOOKUP
:
206 BIO_set_retry_special(b
);
207 retry_reason
= BIO_RR_SSL_X509_LOOKUP
;
209 case SSL_ERROR_WANT_CONNECT
:
210 BIO_set_retry_special(b
);
211 retry_reason
= BIO_RR_CONNECT
;
212 case SSL_ERROR_SYSCALL
:
218 BIO_set_retry_reason(b
, retry_reason
);
223 static long ssl_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
230 SSL_CONNECTION
*sc
= NULL
;
232 bs
= BIO_get_data(b
);
236 || (sc
= SSL_CONNECTION_FROM_SSL(ssl
)) == NULL
)
237 && cmd
!= BIO_C_SET_SSL
)
239 /* TODO(QUIC): The rbio/wbio might be from QUIC_CONNECTION instead */
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
);
249 if (!SSL_clear(ssl
)) {
255 ret
= BIO_ctrl(next
, cmd
, num
, ptr
);
256 else if (sc
->rbio
!= NULL
)
257 ret
= BIO_ctrl(sc
->rbio
, cmd
, num
, ptr
);
265 if (num
) /* client mode */
266 SSL_set_connect_state(ssl
);
268 SSL_set_accept_state(ssl
);
270 case BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT
:
271 ret
= bs
->renegotiate_timeout
;
274 bs
->renegotiate_timeout
= (unsigned long)num
;
275 bs
->last_time
= (unsigned long)time(NULL
);
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
;
282 case BIO_C_GET_SSL_NUM_RENEGOTIATES
:
283 ret
= bs
->num_renegotiates
;
290 bs
= BIO_get_data(b
);
292 BIO_set_shutdown(b
, num
);
295 bio
= SSL_get_rbio(ssl
);
299 BIO_set_next(b
, bio
);
311 case BIO_CTRL_GET_CLOSE
:
312 ret
= BIO_get_shutdown(b
);
314 case BIO_CTRL_SET_CLOSE
:
315 BIO_set_shutdown(b
, (int)num
);
317 case BIO_CTRL_WPENDING
:
318 ret
= BIO_ctrl(sc
->wbio
, cmd
, num
, ptr
);
320 case BIO_CTRL_PENDING
:
321 ret
= SSL_pending(ssl
);
323 ret
= BIO_pending(sc
->rbio
);
326 BIO_clear_retry_flags(b
);
327 ret
= BIO_ctrl(sc
->wbio
, cmd
, num
, ptr
);
328 BIO_copy_next_retry(b
);
331 if ((next
!= NULL
) && (next
!= sc
->rbio
)) {
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
337 SSL_set_bio(ssl
, next
, next
);
341 /* Only detach if we are the BIO explicitly being popped */
343 /* This will clear the reference we obtained during push */
344 SSL_set_bio(ssl
, NULL
, NULL
);
347 case BIO_C_DO_STATE_MACHINE
:
348 BIO_clear_retry_flags(b
);
350 BIO_set_retry_reason(b
, 0);
351 ret
= (int)SSL_do_handshake(ssl
);
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
);
357 case SSL_ERROR_WANT_WRITE
:
358 BIO_set_flags(b
, BIO_FLAGS_WRITE
| BIO_FLAGS_SHOULD_RETRY
);
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
));
364 case SSL_ERROR_WANT_X509_LOOKUP
:
365 BIO_set_retry_special(b
);
366 BIO_set_retry_reason(b
, BIO_RR_SSL_X509_LOOKUP
);
374 dbs
= BIO_get_data(dbio
);
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
);
385 ret
= BIO_ctrl(sc
->rbio
, cmd
, num
, ptr
);
387 case BIO_CTRL_SET_CALLBACK
:
388 ret
= 0; /* use callback ctrl */
391 ret
= BIO_ctrl(sc
->rbio
, cmd
, num
, ptr
);
397 static long ssl_callback_ctrl(BIO
*b
, int cmd
, BIO_info_cb
*fp
)
403 bs
= BIO_get_data(b
);
406 case BIO_CTRL_SET_CALLBACK
:
407 ret
= BIO_callback_ctrl(SSL_get_rbio(ssl
), cmd
, fp
);
416 static int ssl_puts(BIO
*bp
, const char *str
)
421 ret
= BIO_write(bp
, str
, n
);
425 BIO
*BIO_new_buffer_ssl_connect(SSL_CTX
*ctx
)
427 #ifndef OPENSSL_NO_SOCK
428 BIO
*ret
= NULL
, *buf
= NULL
, *ssl
= NULL
;
430 if ((buf
= BIO_new(BIO_f_buffer())) == NULL
)
432 if ((ssl
= BIO_new_ssl_connect(ctx
)) == NULL
)
434 if ((ret
= BIO_push(buf
, ssl
)) == NULL
)
444 BIO
*BIO_new_ssl_connect(SSL_CTX
*ctx
)
446 #ifndef OPENSSL_NO_SOCK
447 BIO
*ret
= NULL
, *con
= NULL
, *ssl
= NULL
;
449 if ((con
= BIO_new(BIO_s_connect())) == NULL
)
451 if ((ssl
= BIO_new_ssl(ctx
, 1)) == NULL
)
453 if ((ret
= BIO_push(ssl
, con
)) == NULL
)
463 BIO
*BIO_new_ssl(SSL_CTX
*ctx
, int client
)
468 if ((ret
= BIO_new(BIO_f_ssl())) == NULL
)
470 if ((ssl
= SSL_new(ctx
)) == NULL
) {
475 SSL_set_connect_state(ssl
);
477 SSL_set_accept_state(ssl
);
479 BIO_set_ssl(ret
, ssl
, BIO_CLOSE
);
483 int BIO_ssl_copy_session_id(BIO
*t
, BIO
*f
)
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
))
490 tdata
= BIO_get_data(t
);
491 fdata
= BIO_get_data(f
);
492 if ((tdata
->ssl
== NULL
) || (fdata
->ssl
== NULL
))
494 if (!SSL_copy_session_id(tdata
->ssl
, (fdata
->ssl
)))
499 void BIO_ssl_shutdown(BIO
*b
)
503 for (; b
!= NULL
; b
= BIO_next(b
)) {
504 if (BIO_method_type(b
) != BIO_TYPE_SSL
)
506 bdata
= BIO_get_data(b
);
507 if (bdata
!= NULL
&& bdata
->ssl
!= NULL
)
508 SSL_shutdown(bdata
->ssl
);