1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
62 #include <openssl/crypto.h>
63 #include "internal/bio.h"
64 #include <openssl/err.h>
67 static int ssl_write(BIO *h, const char *buf, int num);
68 static int ssl_read(BIO *h, char *buf, int size);
69 static int ssl_puts(BIO *h, const char *str);
70 static long ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2);
71 static int ssl_new(BIO *h);
72 static int ssl_free(BIO *data);
73 static long ssl_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
74 typedef struct bio_ssl_st {
75 SSL *ssl; /* The ssl handle :-) */
76 /* re-negotiate every time the total number of bytes is this size */
78 unsigned long renegotiate_count;
79 unsigned long byte_count;
80 unsigned long renegotiate_timeout;
81 unsigned long last_time;
84 static const BIO_METHOD methods_sslp = {
96 const BIO_METHOD *BIO_f_ssl(void)
98 return (&methods_sslp);
101 static int ssl_new(BIO *bi)
103 BIO_SSL *bs = OPENSSL_zalloc(sizeof(*bs));
106 BIOerr(BIO_F_SSL_NEW, ERR_R_MALLOC_FAILURE);
110 BIO_set_data(bi, bs);
111 /* Clear all flags */
112 BIO_clear_flags(bi, ~0);
117 static int ssl_free(BIO *a)
123 bs = BIO_get_data(a);
125 SSL_shutdown(bs->ssl);
126 if (BIO_get_shutdown(a)) {
129 /* Clear all flags */
130 BIO_clear_flags(a, ~0);
137 static int ssl_read(BIO *b, char *out, int outl)
142 int retry_reason = 0;
147 sb = BIO_get_data(b);
150 BIO_clear_retry_flags(b);
152 ret = SSL_read(ssl, out, outl);
154 switch (SSL_get_error(ssl, ret)) {
158 if (sb->renegotiate_count > 0) {
159 sb->byte_count += ret;
160 if (sb->byte_count > sb->renegotiate_count) {
162 sb->num_renegotiates++;
163 SSL_renegotiate(ssl);
167 if ((sb->renegotiate_timeout > 0) && (!r)) {
170 tm = (unsigned long)time(NULL);
171 if (tm > sb->last_time + sb->renegotiate_timeout) {
173 sb->num_renegotiates++;
174 SSL_renegotiate(ssl);
179 case SSL_ERROR_WANT_READ:
180 BIO_set_retry_read(b);
182 case SSL_ERROR_WANT_WRITE:
183 BIO_set_retry_write(b);
185 case SSL_ERROR_WANT_X509_LOOKUP:
186 BIO_set_retry_special(b);
187 retry_reason = BIO_RR_SSL_X509_LOOKUP;
189 case SSL_ERROR_WANT_ACCEPT:
190 BIO_set_retry_special(b);
191 retry_reason = BIO_RR_ACCEPT;
193 case SSL_ERROR_WANT_CONNECT:
194 BIO_set_retry_special(b);
195 retry_reason = BIO_RR_CONNECT;
197 case SSL_ERROR_SYSCALL:
199 case SSL_ERROR_ZERO_RETURN:
204 BIO_set_retry_reason(b, retry_reason);
208 static int ssl_write(BIO *b, const char *out, int outl)
211 int retry_reason = 0;
217 bs = BIO_get_data(b);
220 BIO_clear_retry_flags(b);
223 * ret=SSL_do_handshake(ssl); if (ret > 0)
225 ret = SSL_write(ssl, out, outl);
227 switch (SSL_get_error(ssl, ret)) {
231 if (bs->renegotiate_count > 0) {
232 bs->byte_count += ret;
233 if (bs->byte_count > bs->renegotiate_count) {
235 bs->num_renegotiates++;
236 SSL_renegotiate(ssl);
240 if ((bs->renegotiate_timeout > 0) && (!r)) {
243 tm = (unsigned long)time(NULL);
244 if (tm > bs->last_time + bs->renegotiate_timeout) {
246 bs->num_renegotiates++;
247 SSL_renegotiate(ssl);
251 case SSL_ERROR_WANT_WRITE:
252 BIO_set_retry_write(b);
254 case SSL_ERROR_WANT_READ:
255 BIO_set_retry_read(b);
257 case SSL_ERROR_WANT_X509_LOOKUP:
258 BIO_set_retry_special(b);
259 retry_reason = BIO_RR_SSL_X509_LOOKUP;
261 case SSL_ERROR_WANT_CONNECT:
262 BIO_set_retry_special(b);
263 retry_reason = BIO_RR_CONNECT;
264 case SSL_ERROR_SYSCALL:
270 BIO_set_retry_reason(b, retry_reason);
274 static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr)
282 bs = BIO_get_data(b);
285 if ((ssl == NULL) && (cmd != BIO_C_SET_SSL))
291 if (ssl->handshake_func == ssl->method->ssl_connect)
292 SSL_set_connect_state(ssl);
293 else if (ssl->handshake_func == ssl->method->ssl_accept)
294 SSL_set_accept_state(ssl);
296 if (!SSL_clear(ssl)) {
302 ret = BIO_ctrl(next, cmd, num, ptr);
303 else if (ssl->rbio != NULL)
304 ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);
312 if (num) /* client mode */
313 SSL_set_connect_state(ssl);
315 SSL_set_accept_state(ssl);
317 case BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT:
318 ret = bs->renegotiate_timeout;
321 bs->renegotiate_timeout = (unsigned long)num;
322 bs->last_time = (unsigned long)time(NULL);
324 case BIO_C_SET_SSL_RENEGOTIATE_BYTES:
325 ret = bs->renegotiate_count;
326 if ((long)num >= 512)
327 bs->renegotiate_count = (unsigned long)num;
329 case BIO_C_GET_SSL_NUM_RENEGOTIATES:
330 ret = bs->num_renegotiates;
338 BIO_set_shutdown(b, num);
341 bio = SSL_get_rbio(ssl);
345 BIO_set_next(b, bio);
357 case BIO_CTRL_GET_CLOSE:
358 ret = BIO_get_shutdown(b);
360 case BIO_CTRL_SET_CLOSE:
361 BIO_set_shutdown(b, (int)num);
363 case BIO_CTRL_WPENDING:
364 ret = BIO_ctrl(ssl->wbio, cmd, num, ptr);
366 case BIO_CTRL_PENDING:
367 ret = SSL_pending(ssl);
369 ret = BIO_pending(ssl->rbio);
372 BIO_clear_retry_flags(b);
373 ret = BIO_ctrl(ssl->wbio, cmd, num, ptr);
374 BIO_copy_next_retry(b);
377 if ((next != NULL) && (next != ssl->rbio)) {
378 SSL_set_bio(ssl, next, next);
383 /* Only detach if we are the BIO explicitly being popped */
386 * Shouldn't happen in practice because the rbio and wbio are the
389 if (ssl->rbio != ssl->wbio)
390 BIO_free_all(ssl->wbio);
397 case BIO_C_DO_STATE_MACHINE:
398 BIO_clear_retry_flags(b);
400 BIO_set_retry_reason(b, 0);
401 ret = (int)SSL_do_handshake(ssl);
403 switch (SSL_get_error(ssl, (int)ret)) {
404 case SSL_ERROR_WANT_READ:
405 BIO_set_flags(b, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY);
407 case SSL_ERROR_WANT_WRITE:
408 BIO_set_flags(b, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY);
410 case SSL_ERROR_WANT_CONNECT:
411 BIO_set_flags(b, BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY);
412 BIO_set_retry_reason(b, BIO_get_retry_reason(next));
414 case SSL_ERROR_WANT_X509_LOOKUP:
415 BIO_set_retry_special(b);
416 BIO_set_retry_reason(b, BIO_RR_SSL_X509_LOOKUP);
424 dbs = BIO_get_data(dbio);
426 dbs->ssl = SSL_dup(ssl);
427 dbs->num_renegotiates = bs->num_renegotiates;
428 dbs->renegotiate_count = bs->renegotiate_count;
429 dbs->byte_count = bs->byte_count;
430 dbs->renegotiate_timeout = bs->renegotiate_timeout;
431 dbs->last_time = bs->last_time;
432 ret = (dbs->ssl != NULL);
435 ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);
437 case BIO_CTRL_SET_CALLBACK:
439 #if 0 /* FIXME: Should this be used? -- Richard
441 SSLerr(SSL_F_SSL_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
448 case BIO_CTRL_GET_CALLBACK:
450 void (**fptr) (const SSL *xssl, int type, int val);
452 fptr = (void (**)(const SSL *xssl, int type, int val))ptr;
453 *fptr = SSL_get_info_callback(ssl);
457 ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);
463 static long ssl_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
469 bs = BIO_get_data(b);
472 case BIO_CTRL_SET_CALLBACK:
475 * FIXME: setting this via a completely different prototype seems
478 SSL_set_info_callback(ssl, (void (*)(const SSL *, int, int))fp);
482 ret = BIO_callback_ctrl(ssl->rbio, cmd, fp);
488 static int ssl_puts(BIO *bp, const char *str)
493 ret = BIO_write(bp, str, n);
497 BIO *BIO_new_buffer_ssl_connect(SSL_CTX *ctx)
499 #ifndef OPENSSL_NO_SOCK
500 BIO *ret = NULL, *buf = NULL, *ssl = NULL;
502 if ((buf = BIO_new(BIO_f_buffer())) == NULL)
504 if ((ssl = BIO_new_ssl_connect(ctx)) == NULL)
506 if ((ret = BIO_push(buf, ssl)) == NULL)
516 BIO *BIO_new_ssl_connect(SSL_CTX *ctx)
518 #ifndef OPENSSL_NO_SOCK
519 BIO *ret = NULL, *con = NULL, *ssl = NULL;
521 if ((con = BIO_new(BIO_s_connect())) == NULL)
523 if ((ssl = BIO_new_ssl(ctx, 1)) == NULL)
525 if ((ret = BIO_push(ssl, con)) == NULL)
534 BIO *BIO_new_ssl(SSL_CTX *ctx, int client)
539 if ((ret = BIO_new(BIO_f_ssl())) == NULL)
541 if ((ssl = SSL_new(ctx)) == NULL) {
546 SSL_set_connect_state(ssl);
548 SSL_set_accept_state(ssl);
550 BIO_set_ssl(ret, ssl, BIO_CLOSE);
554 int BIO_ssl_copy_session_id(BIO *t, BIO *f)
556 BIO_SSL *tdata, *fdata;
557 t = BIO_find_type(t, BIO_TYPE_SSL);
558 f = BIO_find_type(f, BIO_TYPE_SSL);
559 if ((t == NULL) || (f == NULL))
561 tdata = BIO_get_data(t);
562 fdata = BIO_get_data(f);
563 if ((tdata->ssl == NULL) || (fdata->ssl == NULL))
565 if (!SSL_copy_session_id(tdata->ssl, (fdata->ssl)))
570 void BIO_ssl_shutdown(BIO *b)
574 b = BIO_find_type(b, BIO_TYPE_SSL);