1 /* $OpenBSD: bio_ssl.c,v 1.33 2022/01/14 09:12:53 tb Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
64 #include <openssl/bio.h>
65 #include <openssl/crypto.h>
66 #include <openssl/err.h>
67 #include <openssl/ssl.h>
69 #include "bio_local.h"
72 static int ssl_write(BIO
*h
, const char *buf
, int num
);
73 static int ssl_read(BIO
*h
, char *buf
, int size
);
74 static int ssl_puts(BIO
*h
, const char *str
);
75 static long ssl_ctrl(BIO
*h
, int cmd
, long arg1
, void *arg2
);
76 static int ssl_new(BIO
*h
);
77 static int ssl_free(BIO
*data
);
78 static long ssl_callback_ctrl(BIO
*h
, int cmd
, BIO_info_cb
*fp
);
79 typedef struct bio_ssl_st
{
80 SSL
*ssl
; /* The ssl handle :-) */
81 /* re-negotiate every time the total number of bytes is this size */
83 unsigned long renegotiate_count
;
84 unsigned long byte_count
;
85 unsigned long renegotiate_timeout
;
89 static const BIO_METHOD methods_sslp
= {
98 .callback_ctrl
= ssl_callback_ctrl
,
104 return (&methods_sslp
);
112 bs
= calloc(1, sizeof(BIO_SSL
));
114 SSLerrorx(ERR_R_MALLOC_FAILURE
);
118 bi
->ptr
= (char *)bs
;
130 bs
= (BIO_SSL
*)a
->ptr
;
132 SSL_shutdown(bs
->ssl
);
134 if (a
->init
&& (bs
->ssl
!= NULL
))
144 ssl_read(BIO
*b
, char *out
, int outl
)
149 int retry_reason
= 0;
154 sb
= (BIO_SSL
*)b
->ptr
;
157 BIO_clear_retry_flags(b
);
159 ret
= SSL_read(ssl
, out
, outl
);
161 switch (SSL_get_error(ssl
, ret
)) {
165 if (sb
->renegotiate_count
> 0) {
166 sb
->byte_count
+= ret
;
167 if (sb
->byte_count
> sb
->renegotiate_count
) {
169 sb
->num_renegotiates
++;
170 SSL_renegotiate(ssl
);
174 if ((sb
->renegotiate_timeout
> 0) && (!r
)) {
178 if (tm
> sb
->last_time
+ sb
->renegotiate_timeout
) {
180 sb
->num_renegotiates
++;
181 SSL_renegotiate(ssl
);
186 case SSL_ERROR_WANT_READ
:
187 BIO_set_retry_read(b
);
189 case SSL_ERROR_WANT_WRITE
:
190 BIO_set_retry_write(b
);
192 case SSL_ERROR_WANT_X509_LOOKUP
:
193 BIO_set_retry_special(b
);
194 retry_reason
= BIO_RR_SSL_X509_LOOKUP
;
196 case SSL_ERROR_WANT_ACCEPT
:
197 BIO_set_retry_special(b
);
198 retry_reason
= BIO_RR_ACCEPT
;
200 case SSL_ERROR_WANT_CONNECT
:
201 BIO_set_retry_special(b
);
202 retry_reason
= BIO_RR_CONNECT
;
204 case SSL_ERROR_SYSCALL
:
206 case SSL_ERROR_ZERO_RETURN
:
211 b
->retry_reason
= retry_reason
;
216 ssl_write(BIO
*b
, const char *out
, int outl
)
219 int retry_reason
= 0;
225 bs
= (BIO_SSL
*)b
->ptr
;
228 BIO_clear_retry_flags(b
);
230 /* ret=SSL_do_handshake(ssl);
232 ret
= SSL_write(ssl
, out
, outl
);
234 switch (SSL_get_error(ssl
, ret
)) {
238 if (bs
->renegotiate_count
> 0) {
239 bs
->byte_count
+= ret
;
240 if (bs
->byte_count
> bs
->renegotiate_count
) {
242 bs
->num_renegotiates
++;
243 SSL_renegotiate(ssl
);
247 if ((bs
->renegotiate_timeout
> 0) && (!r
)) {
251 if (tm
> bs
->last_time
+ bs
->renegotiate_timeout
) {
253 bs
->num_renegotiates
++;
254 SSL_renegotiate(ssl
);
258 case SSL_ERROR_WANT_WRITE
:
259 BIO_set_retry_write(b
);
261 case SSL_ERROR_WANT_READ
:
262 BIO_set_retry_read(b
);
264 case SSL_ERROR_WANT_X509_LOOKUP
:
265 BIO_set_retry_special(b
);
266 retry_reason
= BIO_RR_SSL_X509_LOOKUP
;
268 case SSL_ERROR_WANT_CONNECT
:
269 BIO_set_retry_special(b
);
270 retry_reason
= BIO_RR_CONNECT
;
271 case SSL_ERROR_SYSCALL
:
277 b
->retry_reason
= retry_reason
;
282 ssl_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
289 bs
= (BIO_SSL
*)b
->ptr
;
291 if ((ssl
== NULL
) && (cmd
!= BIO_C_SET_SSL
))
297 if (ssl
->internal
->handshake_func
==
298 ssl
->method
->ssl_connect
)
299 SSL_set_connect_state(ssl
);
300 else if (ssl
->internal
->handshake_func
==
301 ssl
->method
->ssl_accept
)
302 SSL_set_accept_state(ssl
);
306 if (b
->next_bio
!= NULL
)
307 ret
= BIO_ctrl(b
->next_bio
, cmd
, num
, ptr
);
308 else if (ssl
->rbio
!= NULL
)
309 ret
= BIO_ctrl(ssl
->rbio
, cmd
, num
, ptr
);
317 if (num
) /* client mode */
318 SSL_set_connect_state(ssl
);
320 SSL_set_accept_state(ssl
);
322 case BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT
:
323 ret
= bs
->renegotiate_timeout
;
326 bs
->renegotiate_timeout
= (unsigned long)num
;
327 bs
->last_time
= time(NULL
);
329 case BIO_C_SET_SSL_RENEGOTIATE_BYTES
:
330 ret
= bs
->renegotiate_count
;
332 bs
->renegotiate_count
= (unsigned long)num
;
334 case BIO_C_GET_SSL_NUM_RENEGOTIATES
:
335 ret
= bs
->num_renegotiates
;
343 b
->shutdown
= (int)num
;
345 ((BIO_SSL
*)b
->ptr
)->ssl
= ssl
;
346 bio
= SSL_get_rbio(ssl
);
348 if (b
->next_bio
!= NULL
)
349 BIO_push(bio
, b
->next_bio
);
351 CRYPTO_add(&bio
->references
, 1, CRYPTO_LOCK_BIO
);
362 case BIO_CTRL_GET_CLOSE
:
365 case BIO_CTRL_SET_CLOSE
:
366 b
->shutdown
= (int)num
;
368 case BIO_CTRL_WPENDING
:
369 ret
= BIO_ctrl(ssl
->wbio
, cmd
, num
, ptr
);
371 case BIO_CTRL_PENDING
:
372 ret
= SSL_pending(ssl
);
374 ret
= BIO_pending(ssl
->rbio
);
377 BIO_clear_retry_flags(b
);
378 ret
= BIO_ctrl(ssl
->wbio
, cmd
, num
, ptr
);
379 BIO_copy_next_retry(b
);
382 if ((b
->next_bio
!= NULL
) && (b
->next_bio
!= ssl
->rbio
)) {
383 SSL_set_bio(ssl
, b
->next_bio
, b
->next_bio
);
384 CRYPTO_add(&b
->next_bio
->references
, 1,
389 /* Only detach if we are the BIO explicitly being popped */
391 /* Shouldn't happen in practice because the
392 * rbio and wbio are the same when pushed.
394 if (ssl
->rbio
!= ssl
->wbio
)
395 BIO_free_all(ssl
->wbio
);
396 if (b
->next_bio
!= NULL
)
397 CRYPTO_add(&b
->next_bio
->references
, -1, CRYPTO_LOCK_BIO
);
402 case BIO_C_DO_STATE_MACHINE
:
403 BIO_clear_retry_flags(b
);
406 ret
= (int)SSL_do_handshake(ssl
);
408 switch (SSL_get_error(ssl
, (int)ret
)) {
409 case SSL_ERROR_WANT_READ
:
411 BIO_FLAGS_READ
|BIO_FLAGS_SHOULD_RETRY
);
413 case SSL_ERROR_WANT_WRITE
:
415 BIO_FLAGS_WRITE
|BIO_FLAGS_SHOULD_RETRY
);
417 case SSL_ERROR_WANT_CONNECT
:
419 BIO_FLAGS_IO_SPECIAL
|BIO_FLAGS_SHOULD_RETRY
);
420 b
->retry_reason
= b
->next_bio
->retry_reason
;
428 if (((BIO_SSL
*)dbio
->ptr
)->ssl
!= NULL
)
429 SSL_free(((BIO_SSL
*)dbio
->ptr
)->ssl
);
430 ((BIO_SSL
*)dbio
->ptr
)->ssl
= SSL_dup(ssl
);
431 ((BIO_SSL
*)dbio
->ptr
)->renegotiate_count
=
432 ((BIO_SSL
*)b
->ptr
)->renegotiate_count
;
433 ((BIO_SSL
*)dbio
->ptr
)->byte_count
=
434 ((BIO_SSL
*)b
->ptr
)->byte_count
;
435 ((BIO_SSL
*)dbio
->ptr
)->renegotiate_timeout
=
436 ((BIO_SSL
*)b
->ptr
)->renegotiate_timeout
;
437 ((BIO_SSL
*)dbio
->ptr
)->last_time
=
438 ((BIO_SSL
*)b
->ptr
)->last_time
;
439 ret
= (((BIO_SSL
*)dbio
->ptr
)->ssl
!= NULL
);
442 ret
= BIO_ctrl(ssl
->rbio
, cmd
, num
, ptr
);
444 case BIO_CTRL_SET_CALLBACK
:
449 case BIO_CTRL_GET_CALLBACK
:
451 void (**fptr
)(const SSL
*xssl
, int type
, int val
);
453 fptr
= (void (**)(const SSL
*xssl
, int type
, int val
))
455 *fptr
= SSL_get_info_callback(ssl
);
459 ret
= BIO_ctrl(ssl
->rbio
, cmd
, num
, ptr
);
466 ssl_callback_ctrl(BIO
*b
, int cmd
, BIO_info_cb
*fp
)
472 bs
= (BIO_SSL
*)b
->ptr
;
475 case BIO_CTRL_SET_CALLBACK
:
477 /* FIXME: setting this via a completely different prototype
478 seems like a crap idea */
479 SSL_set_info_callback(ssl
,
480 (void (*)(const SSL
*, int, int))fp
);
484 ret
= BIO_callback_ctrl(ssl
->rbio
, cmd
, fp
);
491 ssl_puts(BIO
*bp
, const char *str
)
496 ret
= BIO_write(bp
, str
, n
);
501 BIO_new_buffer_ssl_connect(SSL_CTX
*ctx
)
503 BIO
*ret
= NULL
, *buf
= NULL
, *ssl
= NULL
;
505 if ((buf
= BIO_new(BIO_f_buffer())) == NULL
)
507 if ((ssl
= BIO_new_ssl_connect(ctx
)) == NULL
)
509 if ((ret
= BIO_push(buf
, ssl
)) == NULL
)
520 BIO_new_ssl_connect(SSL_CTX
*ctx
)
522 BIO
*ret
= NULL
, *con
= NULL
, *ssl
= NULL
;
524 if ((con
= BIO_new(BIO_s_connect())) == NULL
)
526 if ((ssl
= BIO_new_ssl(ctx
, 1)) == NULL
)
528 if ((ret
= BIO_push(ssl
, con
)) == NULL
)
539 BIO_new_ssl(SSL_CTX
*ctx
, int client
)
544 if ((ret
= BIO_new(BIO_f_ssl())) == NULL
)
546 if ((ssl
= SSL_new(ctx
)) == NULL
)
550 SSL_set_connect_state(ssl
);
552 SSL_set_accept_state(ssl
);
554 BIO_set_ssl(ret
, ssl
, BIO_CLOSE
);
563 BIO_ssl_copy_session_id(BIO
*t
, BIO
*f
)
565 t
= BIO_find_type(t
, BIO_TYPE_SSL
);
566 f
= BIO_find_type(f
, BIO_TYPE_SSL
);
567 if ((t
== NULL
) || (f
== NULL
))
569 if ((((BIO_SSL
*)t
->ptr
)->ssl
== NULL
) ||
570 (((BIO_SSL
*)f
->ptr
)->ssl
== NULL
))
572 if (!SSL_copy_session_id(((BIO_SSL
*)t
->ptr
)->ssl
,
573 ((BIO_SSL
*)f
->ptr
)->ssl
))
579 BIO_ssl_shutdown(BIO
*b
)
584 if (b
->method
->type
== BIO_TYPE_SSL
) {
585 s
= ((BIO_SSL
*)b
->ptr
)->ssl
;