2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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>
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;
31 unsigned long byte_count;
32 unsigned long renegotiate_timeout;
33 unsigned long last_time;
36 static const BIO_METHOD methods_sslp = {
50 const BIO_METHOD *BIO_f_ssl(void)
52 return (&methods_sslp);
55 static int ssl_new(BIO *bi)
57 BIO_SSL *bs = OPENSSL_zalloc(sizeof(*bs));
60 BIOerr(BIO_F_SSL_NEW, ERR_R_MALLOC_FAILURE);
66 BIO_clear_flags(bi, ~0);
71 static int ssl_free(BIO *a)
79 SSL_shutdown(bs->ssl);
80 if (BIO_get_shutdown(a)) {
84 BIO_clear_flags(a, ~0);
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);
109 ret = SSL_read(ssl, buf, size);
113 switch (SSL_get_error(ssl, ret)) {
117 if (sb->renegotiate_count > 0) {
118 sb->byte_count += *readbytes;
119 if (sb->byte_count > sb->renegotiate_count) {
121 sb->num_renegotiates++;
122 SSL_renegotiate(ssl);
126 if ((sb->renegotiate_timeout > 0) && (!r)) {
129 tm = (unsigned long)time(NULL);
130 if (tm > sb->last_time + sb->renegotiate_timeout) {
132 sb->num_renegotiates++;
133 SSL_renegotiate(ssl);
138 case SSL_ERROR_WANT_READ:
139 BIO_set_retry_read(b);
141 case SSL_ERROR_WANT_WRITE:
142 BIO_set_retry_write(b);
144 case SSL_ERROR_WANT_X509_LOOKUP:
145 BIO_set_retry_special(b);
146 retry_reason = BIO_RR_SSL_X509_LOOKUP;
148 case SSL_ERROR_WANT_ACCEPT:
149 BIO_set_retry_special(b);
150 retry_reason = BIO_RR_ACCEPT;
152 case SSL_ERROR_WANT_CONNECT:
153 BIO_set_retry_special(b);
154 retry_reason = BIO_RR_CONNECT;
156 case SSL_ERROR_SYSCALL:
158 case SSL_ERROR_ZERO_RETURN:
163 BIO_set_retry_reason(b, retry_reason);
168 static int ssl_write(BIO *b, const char *buf, size_t size, size_t *written)
171 int retry_reason = 0;
177 bs = BIO_get_data(b);
180 BIO_clear_retry_flags(b);
185 ret = SSL_write(ssl, buf, size);
187 switch (SSL_get_error(ssl, ret)) {
191 if (bs->renegotiate_count > 0) {
192 bs->byte_count += ret;
193 if (bs->byte_count > bs->renegotiate_count) {
195 bs->num_renegotiates++;
196 SSL_renegotiate(ssl);
200 if ((bs->renegotiate_timeout > 0) && (!r)) {
203 tm = (unsigned long)time(NULL);
204 if (tm > bs->last_time + bs->renegotiate_timeout) {
206 bs->num_renegotiates++;
207 SSL_renegotiate(ssl);
211 case SSL_ERROR_WANT_WRITE:
212 BIO_set_retry_write(b);
214 case SSL_ERROR_WANT_READ:
215 BIO_set_retry_read(b);
217 case SSL_ERROR_WANT_X509_LOOKUP:
218 BIO_set_retry_special(b);
219 retry_reason = BIO_RR_SSL_X509_LOOKUP;
221 case SSL_ERROR_WANT_CONNECT:
222 BIO_set_retry_special(b);
223 retry_reason = BIO_RR_CONNECT;
224 case SSL_ERROR_SYSCALL:
230 BIO_set_retry_reason(b, retry_reason);
240 static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr)
248 bs = BIO_get_data(b);
251 if ((ssl == NULL) && (cmd != BIO_C_SET_SSL))
257 if (ssl->handshake_func == ssl->method->ssl_connect)
258 SSL_set_connect_state(ssl);
259 else if (ssl->handshake_func == ssl->method->ssl_accept)
260 SSL_set_accept_state(ssl);
262 if (!SSL_clear(ssl)) {
268 ret = BIO_ctrl(next, cmd, num, ptr);
269 else if (ssl->rbio != NULL)
270 ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);
278 if (num) /* client mode */
279 SSL_set_connect_state(ssl);
281 SSL_set_accept_state(ssl);
283 case BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT:
284 ret = bs->renegotiate_timeout;
287 bs->renegotiate_timeout = (unsigned long)num;
288 bs->last_time = (unsigned long)time(NULL);
290 case BIO_C_SET_SSL_RENEGOTIATE_BYTES:
291 ret = bs->renegotiate_count;
292 if ((long)num >= 512)
293 bs->renegotiate_count = (unsigned long)num;
295 case BIO_C_GET_SSL_NUM_RENEGOTIATES:
296 ret = bs->num_renegotiates;
304 BIO_set_shutdown(b, num);
307 bio = SSL_get_rbio(ssl);
311 BIO_set_next(b, bio);
323 case BIO_CTRL_GET_CLOSE:
324 ret = BIO_get_shutdown(b);
326 case BIO_CTRL_SET_CLOSE:
327 BIO_set_shutdown(b, (int)num);
329 case BIO_CTRL_WPENDING:
330 ret = BIO_ctrl(ssl->wbio, cmd, num, ptr);
332 case BIO_CTRL_PENDING:
333 ret = SSL_pending(ssl);
335 ret = BIO_pending(ssl->rbio);
338 BIO_clear_retry_flags(b);
339 ret = BIO_ctrl(ssl->wbio, cmd, num, ptr);
340 BIO_copy_next_retry(b);
343 if ((next != NULL) && (next != ssl->rbio)) {
345 * We are going to pass ownership of next to the SSL object...but
346 * we don't own a reference to pass yet - so up ref
349 SSL_set_bio(ssl, next, next);
353 /* Only detach if we are the BIO explicitly being popped */
355 /* This will clear the reference we obtained during push */
356 SSL_set_bio(ssl, NULL, NULL);
359 case BIO_C_DO_STATE_MACHINE:
360 BIO_clear_retry_flags(b);
362 BIO_set_retry_reason(b, 0);
363 ret = (int)SSL_do_handshake(ssl);
365 switch (SSL_get_error(ssl, (int)ret)) {
366 case SSL_ERROR_WANT_READ:
367 BIO_set_flags(b, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY);
369 case SSL_ERROR_WANT_WRITE:
370 BIO_set_flags(b, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY);
372 case SSL_ERROR_WANT_CONNECT:
373 BIO_set_flags(b, BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY);
374 BIO_set_retry_reason(b, BIO_get_retry_reason(next));
376 case SSL_ERROR_WANT_X509_LOOKUP:
377 BIO_set_retry_special(b);
378 BIO_set_retry_reason(b, BIO_RR_SSL_X509_LOOKUP);
386 dbs = BIO_get_data(dbio);
388 dbs->ssl = SSL_dup(ssl);
389 dbs->num_renegotiates = bs->num_renegotiates;
390 dbs->renegotiate_count = bs->renegotiate_count;
391 dbs->byte_count = bs->byte_count;
392 dbs->renegotiate_timeout = bs->renegotiate_timeout;
393 dbs->last_time = bs->last_time;
394 ret = (dbs->ssl != NULL);
397 ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);
399 case BIO_CTRL_SET_CALLBACK:
401 #if 0 /* FIXME: Should this be used? -- Richard
403 SSLerr(SSL_F_SSL_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
410 case BIO_CTRL_GET_CALLBACK:
412 void (**fptr) (const SSL *xssl, int type, int val);
414 fptr = (void (**)(const SSL *xssl, int type, int val))ptr;
415 *fptr = SSL_get_info_callback(ssl);
419 ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);
425 static long ssl_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
431 bs = BIO_get_data(b);
434 case BIO_CTRL_SET_CALLBACK:
437 * FIXME: setting this via a completely different prototype seems
440 SSL_set_info_callback(ssl, (void (*)(const SSL *, int, int))fp);
444 ret = BIO_callback_ctrl(ssl->rbio, cmd, fp);
450 static int ssl_puts(BIO *bp, const char *str)
455 ret = BIO_write(bp, str, n);
459 BIO *BIO_new_buffer_ssl_connect(SSL_CTX *ctx)
461 #ifndef OPENSSL_NO_SOCK
462 BIO *ret = NULL, *buf = NULL, *ssl = NULL;
464 if ((buf = BIO_new(BIO_f_buffer())) == NULL)
466 if ((ssl = BIO_new_ssl_connect(ctx)) == NULL)
468 if ((ret = BIO_push(buf, ssl)) == NULL)
478 BIO *BIO_new_ssl_connect(SSL_CTX *ctx)
480 #ifndef OPENSSL_NO_SOCK
481 BIO *ret = NULL, *con = NULL, *ssl = NULL;
483 if ((con = BIO_new(BIO_s_connect())) == NULL)
485 if ((ssl = BIO_new_ssl(ctx, 1)) == NULL)
487 if ((ret = BIO_push(ssl, con)) == NULL)
496 BIO *BIO_new_ssl(SSL_CTX *ctx, int client)
501 if ((ret = BIO_new(BIO_f_ssl())) == NULL)
503 if ((ssl = SSL_new(ctx)) == NULL) {
508 SSL_set_connect_state(ssl);
510 SSL_set_accept_state(ssl);
512 BIO_set_ssl(ret, ssl, BIO_CLOSE);
516 int BIO_ssl_copy_session_id(BIO *t, BIO *f)
518 BIO_SSL *tdata, *fdata;
519 t = BIO_find_type(t, BIO_TYPE_SSL);
520 f = BIO_find_type(f, BIO_TYPE_SSL);
521 if ((t == NULL) || (f == NULL))
523 tdata = BIO_get_data(t);
524 fdata = BIO_get_data(f);
525 if ((tdata->ssl == NULL) || (fdata->ssl == NULL))
527 if (!SSL_copy_session_id(tdata->ssl, (fdata->ssl)))
532 void BIO_ssl_shutdown(BIO *b)
536 b = BIO_find_type(b, BIO_TYPE_SSL);