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.]
63 #include <openssl/crypto.h>
64 #include <openssl/bio.h>
65 #include <openssl/err.h>
68 static int ssl_write(BIO
*h
, const char *buf
, int num
);
69 static int ssl_read(BIO
*h
, char *buf
, int size
);
70 static int ssl_puts(BIO
*h
, const char *str
);
71 static long ssl_ctrl(BIO
*h
, int cmd
, long arg1
, void *arg2
);
72 static int ssl_new(BIO
*h
);
73 static int ssl_free(BIO
*data
);
74 static long ssl_callback_ctrl(BIO
*h
, int cmd
, bio_info_cb
*fp
);
75 typedef struct bio_ssl_st
{
76 SSL
*ssl
; /* The ssl handle :-) */
77 /* re-negotiate every time the total number of bytes is this size */
79 unsigned long renegotiate_count
;
80 unsigned long byte_count
;
81 unsigned long renegotiate_timeout
;
82 unsigned long last_time
;
85 static BIO_METHOD methods_sslp
= {
97 BIO_METHOD
*BIO_f_ssl(void)
99 return (&methods_sslp
);
102 static int ssl_new(BIO
*bi
)
106 bs
= (BIO_SSL
*)OPENSSL_malloc(sizeof(BIO_SSL
));
108 BIOerr(BIO_F_SSL_NEW
, ERR_R_MALLOC_FAILURE
);
111 memset(bs
, 0, sizeof(BIO_SSL
));
113 bi
->ptr
= (char *)bs
;
118 static int ssl_free(BIO
*a
)
124 bs
= (BIO_SSL
*)a
->ptr
;
126 SSL_shutdown(bs
->ssl
);
134 OPENSSL_free(a
->ptr
);
138 static int ssl_read(BIO
*b
, char *out
, int outl
)
143 int retry_reason
= 0;
148 sb
= (BIO_SSL
*)b
->ptr
;
151 BIO_clear_retry_flags(b
);
153 ret
= SSL_read(ssl
, out
, outl
);
155 switch (SSL_get_error(ssl
, ret
)) {
159 if (sb
->renegotiate_count
> 0) {
160 sb
->byte_count
+= ret
;
161 if (sb
->byte_count
> sb
->renegotiate_count
) {
163 sb
->num_renegotiates
++;
164 SSL_renegotiate(ssl
);
168 if ((sb
->renegotiate_timeout
> 0) && (!r
)) {
171 tm
= (unsigned long)time(NULL
);
172 if (tm
> sb
->last_time
+ sb
->renegotiate_timeout
) {
174 sb
->num_renegotiates
++;
175 SSL_renegotiate(ssl
);
180 case SSL_ERROR_WANT_READ
:
181 BIO_set_retry_read(b
);
183 case SSL_ERROR_WANT_WRITE
:
184 BIO_set_retry_write(b
);
186 case SSL_ERROR_WANT_X509_LOOKUP
:
187 BIO_set_retry_special(b
);
188 retry_reason
= BIO_RR_SSL_X509_LOOKUP
;
190 case SSL_ERROR_WANT_ACCEPT
:
191 BIO_set_retry_special(b
);
192 retry_reason
= BIO_RR_ACCEPT
;
194 case SSL_ERROR_WANT_CONNECT
:
195 BIO_set_retry_special(b
);
196 retry_reason
= BIO_RR_CONNECT
;
198 case SSL_ERROR_SYSCALL
:
200 case SSL_ERROR_ZERO_RETURN
:
205 b
->retry_reason
= retry_reason
;
209 static int ssl_write(BIO
*b
, const char *out
, int outl
)
212 int retry_reason
= 0;
218 bs
= (BIO_SSL
*)b
->ptr
;
221 BIO_clear_retry_flags(b
);
224 * ret=SSL_do_handshake(ssl); if (ret > 0)
226 ret
= SSL_write(ssl
, out
, outl
);
228 switch (SSL_get_error(ssl
, ret
)) {
232 if (bs
->renegotiate_count
> 0) {
233 bs
->byte_count
+= ret
;
234 if (bs
->byte_count
> bs
->renegotiate_count
) {
236 bs
->num_renegotiates
++;
237 SSL_renegotiate(ssl
);
241 if ((bs
->renegotiate_timeout
> 0) && (!r
)) {
244 tm
= (unsigned long)time(NULL
);
245 if (tm
> bs
->last_time
+ bs
->renegotiate_timeout
) {
247 bs
->num_renegotiates
++;
248 SSL_renegotiate(ssl
);
252 case SSL_ERROR_WANT_WRITE
:
253 BIO_set_retry_write(b
);
255 case SSL_ERROR_WANT_READ
:
256 BIO_set_retry_read(b
);
258 case SSL_ERROR_WANT_X509_LOOKUP
:
259 BIO_set_retry_special(b
);
260 retry_reason
= BIO_RR_SSL_X509_LOOKUP
;
262 case SSL_ERROR_WANT_CONNECT
:
263 BIO_set_retry_special(b
);
264 retry_reason
= BIO_RR_CONNECT
;
265 case SSL_ERROR_SYSCALL
:
271 b
->retry_reason
= retry_reason
;
275 static long ssl_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
282 bs
= (BIO_SSL
*)b
->ptr
;
284 if ((ssl
== NULL
) && (cmd
!= BIO_C_SET_SSL
))
290 if (ssl
->handshake_func
== ssl
->method
->ssl_connect
)
291 SSL_set_connect_state(ssl
);
292 else if (ssl
->handshake_func
== ssl
->method
->ssl_accept
)
293 SSL_set_accept_state(ssl
);
295 if(!SSL_clear(ssl
)) {
300 if (b
->next_bio
!= NULL
)
301 ret
= BIO_ctrl(b
->next_bio
, cmd
, num
, ptr
);
302 else if (ssl
->rbio
!= NULL
)
303 ret
= BIO_ctrl(ssl
->rbio
, cmd
, num
, ptr
);
311 if (num
) /* client mode */
312 SSL_set_connect_state(ssl
);
314 SSL_set_accept_state(ssl
);
316 case BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT
:
317 ret
= bs
->renegotiate_timeout
;
320 bs
->renegotiate_timeout
= (unsigned long)num
;
321 bs
->last_time
= (unsigned long)time(NULL
);
323 case BIO_C_SET_SSL_RENEGOTIATE_BYTES
:
324 ret
= bs
->renegotiate_count
;
325 if ((long)num
>= 512)
326 bs
->renegotiate_count
= (unsigned long)num
;
328 case BIO_C_GET_SSL_NUM_RENEGOTIATES
:
329 ret
= bs
->num_renegotiates
;
337 b
->shutdown
= (int)num
;
339 ((BIO_SSL
*)b
->ptr
)->ssl
= ssl
;
340 bio
= SSL_get_rbio(ssl
);
342 if (b
->next_bio
!= NULL
)
343 BIO_push(bio
, b
->next_bio
);
345 CRYPTO_add(&bio
->references
, 1, CRYPTO_LOCK_BIO
);
356 case BIO_CTRL_GET_CLOSE
:
359 case BIO_CTRL_SET_CLOSE
:
360 b
->shutdown
= (int)num
;
362 case BIO_CTRL_WPENDING
:
363 ret
= BIO_ctrl(ssl
->wbio
, cmd
, num
, ptr
);
365 case BIO_CTRL_PENDING
:
366 ret
= SSL_pending(ssl
);
368 ret
= BIO_pending(ssl
->rbio
);
371 BIO_clear_retry_flags(b
);
372 ret
= BIO_ctrl(ssl
->wbio
, cmd
, num
, ptr
);
373 BIO_copy_next_retry(b
);
376 if ((b
->next_bio
!= NULL
) && (b
->next_bio
!= ssl
->rbio
)) {
377 SSL_set_bio(ssl
, b
->next_bio
, b
->next_bio
);
378 CRYPTO_add(&b
->next_bio
->references
, 1, CRYPTO_LOCK_BIO
);
382 /* Only detach if we are the BIO explicitly being popped */
385 * Shouldn't happen in practice because the rbio and wbio are the
388 if (ssl
->rbio
!= ssl
->wbio
)
389 BIO_free_all(ssl
->wbio
);
390 if (b
->next_bio
!= NULL
)
391 CRYPTO_add(&b
->next_bio
->references
, -1, CRYPTO_LOCK_BIO
);
396 case BIO_C_DO_STATE_MACHINE
:
397 BIO_clear_retry_flags(b
);
400 ret
= (int)SSL_do_handshake(ssl
);
402 switch (SSL_get_error(ssl
, (int)ret
)) {
403 case SSL_ERROR_WANT_READ
:
404 BIO_set_flags(b
, BIO_FLAGS_READ
| BIO_FLAGS_SHOULD_RETRY
);
406 case SSL_ERROR_WANT_WRITE
:
407 BIO_set_flags(b
, BIO_FLAGS_WRITE
| BIO_FLAGS_SHOULD_RETRY
);
409 case SSL_ERROR_WANT_CONNECT
:
410 BIO_set_flags(b
, BIO_FLAGS_IO_SPECIAL
| BIO_FLAGS_SHOULD_RETRY
);
411 b
->retry_reason
= b
->next_bio
->retry_reason
;
419 SSL_free(((BIO_SSL
*)dbio
->ptr
)->ssl
);
420 ((BIO_SSL
*)dbio
->ptr
)->ssl
= SSL_dup(ssl
);
421 ((BIO_SSL
*)dbio
->ptr
)->renegotiate_count
=
422 ((BIO_SSL
*)b
->ptr
)->renegotiate_count
;
423 ((BIO_SSL
*)dbio
->ptr
)->byte_count
= ((BIO_SSL
*)b
->ptr
)->byte_count
;
424 ((BIO_SSL
*)dbio
->ptr
)->renegotiate_timeout
=
425 ((BIO_SSL
*)b
->ptr
)->renegotiate_timeout
;
426 ((BIO_SSL
*)dbio
->ptr
)->last_time
= ((BIO_SSL
*)b
->ptr
)->last_time
;
427 ret
= (((BIO_SSL
*)dbio
->ptr
)->ssl
!= NULL
);
430 ret
= BIO_ctrl(ssl
->rbio
, cmd
, num
, ptr
);
432 case BIO_CTRL_SET_CALLBACK
:
434 #if 0 /* FIXME: Should this be used? -- Richard
436 SSLerr(SSL_F_SSL_CTRL
, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
);
443 case BIO_CTRL_GET_CALLBACK
:
445 void (**fptr
) (const SSL
*xssl
, int type
, int val
);
447 fptr
= (void (**)(const SSL
*xssl
, int type
, int val
))ptr
;
448 *fptr
= SSL_get_info_callback(ssl
);
452 ret
= BIO_ctrl(ssl
->rbio
, cmd
, num
, ptr
);
458 static long ssl_callback_ctrl(BIO
*b
, int cmd
, bio_info_cb
*fp
)
464 bs
= (BIO_SSL
*)b
->ptr
;
467 case BIO_CTRL_SET_CALLBACK
:
470 * FIXME: setting this via a completely different prototype seems
473 SSL_set_info_callback(ssl
, (void (*)(const SSL
*, int, int))fp
);
477 ret
= BIO_callback_ctrl(ssl
->rbio
, cmd
, fp
);
483 static int ssl_puts(BIO
*bp
, const char *str
)
488 ret
= BIO_write(bp
, str
, n
);
492 BIO
*BIO_new_buffer_ssl_connect(SSL_CTX
*ctx
)
494 #ifndef OPENSSL_NO_SOCK
495 BIO
*ret
= NULL
, *buf
= NULL
, *ssl
= NULL
;
497 if ((buf
= BIO_new(BIO_f_buffer())) == NULL
)
499 if ((ssl
= BIO_new_ssl_connect(ctx
)) == NULL
)
501 if ((ret
= BIO_push(buf
, ssl
)) == NULL
)
511 BIO
*BIO_new_ssl_connect(SSL_CTX
*ctx
)
513 #ifndef OPENSSL_NO_SOCK
514 BIO
*ret
= NULL
, *con
= NULL
, *ssl
= NULL
;
516 if ((con
= BIO_new(BIO_s_connect())) == NULL
)
518 if ((ssl
= BIO_new_ssl(ctx
, 1)) == NULL
)
520 if ((ret
= BIO_push(ssl
, con
)) == NULL
)
529 BIO
*BIO_new_ssl(SSL_CTX
*ctx
, int client
)
534 if ((ret
= BIO_new(BIO_f_ssl())) == NULL
)
536 if ((ssl
= SSL_new(ctx
)) == NULL
) {
541 SSL_set_connect_state(ssl
);
543 SSL_set_accept_state(ssl
);
545 BIO_set_ssl(ret
, ssl
, BIO_CLOSE
);
549 int BIO_ssl_copy_session_id(BIO
*t
, BIO
*f
)
551 t
= BIO_find_type(t
, BIO_TYPE_SSL
);
552 f
= BIO_find_type(f
, BIO_TYPE_SSL
);
553 if ((t
== NULL
) || (f
== NULL
))
555 if ((((BIO_SSL
*)t
->ptr
)->ssl
== NULL
) ||
556 (((BIO_SSL
*)f
->ptr
)->ssl
== NULL
))
558 if(!SSL_copy_session_id(((BIO_SSL
*)t
->ptr
)->ssl
, ((BIO_SSL
*)f
->ptr
)->ssl
))
563 void BIO_ssl_shutdown(BIO
*b
)
568 if (b
->method
->type
== BIO_TYPE_SSL
) {
569 s
= ((BIO_SSL
*)b
->ptr
)->ssl
;