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));
65 BIO_clear_flags(bi, ~0);
70 static int ssl_free(BIO *a)
77 if (BIO_get_shutdown(a)) {
79 SSL_shutdown(bs->ssl);
82 BIO_clear_flags(a, ~0); /* Clear all flags */
89 static int ssl_read(BIO *b, char *buf, size_t size, size_t *readbytes)
102 BIO_clear_retry_flags(b);
104 ret = ssl_read_internal(ssl, buf, size, readbytes);
106 switch (SSL_get_error(ssl, ret)) {
108 if (sb->renegotiate_count > 0) {
109 sb->byte_count += *readbytes;
110 if (sb->byte_count > sb->renegotiate_count) {
112 sb->num_renegotiates++;
113 SSL_renegotiate(ssl);
117 if ((sb->renegotiate_timeout > 0) && (!r)) {
120 tm = (unsigned long)time(NULL);
121 if (tm > sb->last_time + sb->renegotiate_timeout) {
123 sb->num_renegotiates++;
124 SSL_renegotiate(ssl);
129 case SSL_ERROR_WANT_READ:
130 BIO_set_retry_read(b);
132 case SSL_ERROR_WANT_WRITE:
133 BIO_set_retry_write(b);
135 case SSL_ERROR_WANT_X509_LOOKUP:
136 BIO_set_retry_special(b);
137 retry_reason = BIO_RR_SSL_X509_LOOKUP;
139 case SSL_ERROR_WANT_ACCEPT:
140 BIO_set_retry_special(b);
141 retry_reason = BIO_RR_ACCEPT;
143 case SSL_ERROR_WANT_CONNECT:
144 BIO_set_retry_special(b);
145 retry_reason = BIO_RR_CONNECT;
147 case SSL_ERROR_SYSCALL:
149 case SSL_ERROR_ZERO_RETURN:
154 BIO_set_retry_reason(b, retry_reason);
159 static int ssl_write(BIO *b, const char *buf, size_t size, size_t *written)
162 int retry_reason = 0;
168 bs = BIO_get_data(b);
171 BIO_clear_retry_flags(b);
173 ret = ssl_write_internal(ssl, buf, size, written);
175 switch (SSL_get_error(ssl, ret)) {
177 if (bs->renegotiate_count > 0) {
178 bs->byte_count += *written;
179 if (bs->byte_count > bs->renegotiate_count) {
181 bs->num_renegotiates++;
182 SSL_renegotiate(ssl);
186 if ((bs->renegotiate_timeout > 0) && (!r)) {
189 tm = (unsigned long)time(NULL);
190 if (tm > bs->last_time + bs->renegotiate_timeout) {
192 bs->num_renegotiates++;
193 SSL_renegotiate(ssl);
197 case SSL_ERROR_WANT_WRITE:
198 BIO_set_retry_write(b);
200 case SSL_ERROR_WANT_READ:
201 BIO_set_retry_read(b);
203 case SSL_ERROR_WANT_X509_LOOKUP:
204 BIO_set_retry_special(b);
205 retry_reason = BIO_RR_SSL_X509_LOOKUP;
207 case SSL_ERROR_WANT_CONNECT:
208 BIO_set_retry_special(b);
209 retry_reason = BIO_RR_CONNECT;
210 case SSL_ERROR_SYSCALL:
216 BIO_set_retry_reason(b, retry_reason);
221 static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr)
228 SSL_CONNECTION *sc = NULL;
230 bs = BIO_get_data(b);
234 || (sc = SSL_CONNECTION_FROM_SSL(ssl)) == NULL)
235 && cmd != BIO_C_SET_SSL)
237 /* TODO(QUIC): The rbio/wbio might be from QUIC_CONNECTION instead */
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);
247 if (!SSL_clear(ssl)) {
253 ret = BIO_ctrl(next, cmd, num, ptr);
254 else if (sc->rbio != NULL)
255 ret = BIO_ctrl(sc->rbio, cmd, num, ptr);
263 if (num) /* client mode */
264 SSL_set_connect_state(ssl);
266 SSL_set_accept_state(ssl);
268 case BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT:
269 ret = bs->renegotiate_timeout;
272 bs->renegotiate_timeout = (unsigned long)num;
273 bs->last_time = (unsigned long)time(NULL);
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;
280 case BIO_C_GET_SSL_NUM_RENEGOTIATES:
281 ret = bs->num_renegotiates;
288 bs = BIO_get_data(b);
290 BIO_set_shutdown(b, num);
293 bio = SSL_get_rbio(ssl);
297 BIO_set_next(b, bio);
309 case BIO_CTRL_GET_CLOSE:
310 ret = BIO_get_shutdown(b);
312 case BIO_CTRL_SET_CLOSE:
313 BIO_set_shutdown(b, (int)num);
315 case BIO_CTRL_WPENDING:
316 ret = BIO_ctrl(sc->wbio, cmd, num, ptr);
318 case BIO_CTRL_PENDING:
319 ret = SSL_pending(ssl);
321 ret = BIO_pending(sc->rbio);
324 BIO_clear_retry_flags(b);
325 ret = BIO_ctrl(sc->wbio, cmd, num, ptr);
326 BIO_copy_next_retry(b);
329 if ((next != NULL) && (next != sc->rbio)) {
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
335 SSL_set_bio(ssl, next, next);
339 /* Only detach if we are the BIO explicitly being popped */
341 /* This will clear the reference we obtained during push */
342 SSL_set_bio(ssl, NULL, NULL);
345 case BIO_C_DO_STATE_MACHINE:
346 BIO_clear_retry_flags(b);
348 BIO_set_retry_reason(b, 0);
349 ret = (int)SSL_do_handshake(ssl);
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);
355 case SSL_ERROR_WANT_WRITE:
356 BIO_set_flags(b, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY);
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));
362 case SSL_ERROR_WANT_X509_LOOKUP:
363 BIO_set_retry_special(b);
364 BIO_set_retry_reason(b, BIO_RR_SSL_X509_LOOKUP);
372 dbs = BIO_get_data(dbio);
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);
383 ret = BIO_ctrl(sc->rbio, cmd, num, ptr);
385 case BIO_CTRL_SET_CALLBACK:
386 ret = 0; /* use callback ctrl */
389 ret = BIO_ctrl(sc->rbio, cmd, num, ptr);
395 static long ssl_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
401 bs = BIO_get_data(b);
404 case BIO_CTRL_SET_CALLBACK:
405 ret = BIO_callback_ctrl(SSL_get_rbio(ssl), cmd, fp);
414 static int ssl_puts(BIO *bp, const char *str)
419 ret = BIO_write(bp, str, n);
423 BIO *BIO_new_buffer_ssl_connect(SSL_CTX *ctx)
425 #ifndef OPENSSL_NO_SOCK
426 BIO *ret = NULL, *buf = NULL, *ssl = NULL;
428 if ((buf = BIO_new(BIO_f_buffer())) == NULL)
430 if ((ssl = BIO_new_ssl_connect(ctx)) == NULL)
432 if ((ret = BIO_push(buf, ssl)) == NULL)
442 BIO *BIO_new_ssl_connect(SSL_CTX *ctx)
444 #ifndef OPENSSL_NO_SOCK
445 BIO *ret = NULL, *con = NULL, *ssl = NULL;
447 if ((con = BIO_new(BIO_s_connect())) == NULL)
449 if ((ssl = BIO_new_ssl(ctx, 1)) == NULL)
451 if ((ret = BIO_push(ssl, con)) == NULL)
461 BIO *BIO_new_ssl(SSL_CTX *ctx, int client)
466 if ((ret = BIO_new(BIO_f_ssl())) == NULL)
468 if ((ssl = SSL_new(ctx)) == NULL) {
473 SSL_set_connect_state(ssl);
475 SSL_set_accept_state(ssl);
477 BIO_set_ssl(ret, ssl, BIO_CLOSE);
481 int BIO_ssl_copy_session_id(BIO *t, BIO *f)
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))
488 tdata = BIO_get_data(t);
489 fdata = BIO_get_data(f);
490 if ((tdata->ssl == NULL) || (fdata->ssl == NULL))
492 if (!SSL_copy_session_id(tdata->ssl, (fdata->ssl)))
497 void BIO_ssl_shutdown(BIO *b)
501 for (; b != NULL; b = BIO_next(b)) {
502 if (BIO_method_type(b) != BIO_TYPE_SSL)
504 bdata = BIO_get_data(b);
505 if (bdata != NULL && bdata->ssl != NULL)
506 SSL_shutdown(bdata->ssl);