]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/bio_ssl.c
Make BIO opaque
[thirdparty/openssl.git] / ssl / bio_ssl.c
CommitLineData
58964a49 1/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
d02b48c6
RE
2 * All rights reserved.
3 *
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.
0f113f3e 7 *
d02b48c6
RE
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).
0f113f3e 14 *
d02b48c6
RE
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.
0f113f3e 21 *
d02b48c6
RE
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
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 :-).
0f113f3e 36 * 4. If you include any Windows specific code (or a derivative thereof) from
d02b48c6
RE
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
0f113f3e 39 *
d02b48c6
RE
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
50 * SUCH DAMAGE.
0f113f3e 51 *
d02b48c6
RE
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.]
56 */
57
58#include <stdio.h>
58964a49 59#include <stdlib.h>
d02b48c6
RE
60#include <string.h>
61#include <errno.h>
ec577822 62#include <openssl/crypto.h>
a146ae55 63#include "internal/bio.h"
ec577822 64#include <openssl/err.h>
b6ba4014 65#include "ssl_locl.h"
d02b48c6 66
0e1c0612
UM
67static int ssl_write(BIO *h, const char *buf, int num);
68static int ssl_read(BIO *h, char *buf, int size);
69static int ssl_puts(BIO *h, const char *str);
70static long ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2);
d02b48c6
RE
71static int ssl_new(BIO *h);
72static int ssl_free(BIO *data);
13083215 73static long ssl_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
0f113f3e
MC
74typedef struct bio_ssl_st {
75 SSL *ssl; /* The ssl handle :-) */
76 /* re-negotiate every time the total number of bytes is this size */
77 int num_renegotiates;
78 unsigned long renegotiate_count;
79 unsigned long byte_count;
80 unsigned long renegotiate_timeout;
81 unsigned long last_time;
82} BIO_SSL;
83
04f6b0fd 84static const BIO_METHOD methods_sslp = {
0f113f3e
MC
85 BIO_TYPE_SSL, "ssl",
86 ssl_write,
87 ssl_read,
88 ssl_puts,
89 NULL, /* ssl_gets, */
90 ssl_ctrl,
91 ssl_new,
92 ssl_free,
93 ssl_callback_ctrl,
94};
a9188d4e 95
04f6b0fd 96const BIO_METHOD *BIO_f_ssl(void)
0f113f3e
MC
97{
98 return (&methods_sslp);
99}
d02b48c6 100
6b691a5c 101static int ssl_new(BIO *bi)
0f113f3e 102{
b51bce94 103 BIO_SSL *bs = OPENSSL_zalloc(sizeof(*bs));
0f113f3e 104
0f113f3e
MC
105 if (bs == NULL) {
106 BIOerr(BIO_F_SSL_NEW, ERR_R_MALLOC_FAILURE);
107 return (0);
108 }
a146ae55
MC
109 BIO_set_init(bi, 0);
110 BIO_set_data(bi, bs);
111 /* Clear all flags */
112 BIO_clear_flags(bi, ~0);
113
114 return 1;
0f113f3e 115}
d02b48c6 116
6b691a5c 117static int ssl_free(BIO *a)
0f113f3e
MC
118{
119 BIO_SSL *bs;
120
121 if (a == NULL)
122 return (0);
a146ae55 123 bs = BIO_get_data(a);
0f113f3e
MC
124 if (bs->ssl != NULL)
125 SSL_shutdown(bs->ssl);
a146ae55
MC
126 if (BIO_get_shutdown(a)) {
127 if (BIO_get_init(a))
0f113f3e 128 SSL_free(bs->ssl);
a146ae55
MC
129 /* Clear all flags */
130 BIO_clear_flags(a, ~0);
131 BIO_set_init(a, 0);
0f113f3e 132 }
a146ae55
MC
133 OPENSSL_free(bs);
134 return 1;
0f113f3e
MC
135}
136
6b691a5c 137static int ssl_read(BIO *b, char *out, int outl)
0f113f3e
MC
138{
139 int ret = 1;
140 BIO_SSL *sb;
141 SSL *ssl;
142 int retry_reason = 0;
143 int r = 0;
d02b48c6 144
0f113f3e
MC
145 if (out == NULL)
146 return (0);
a146ae55 147 sb = BIO_get_data(b);
0f113f3e 148 ssl = sb->ssl;
d02b48c6 149
0f113f3e 150 BIO_clear_retry_flags(b);
d02b48c6 151
0f113f3e
MC
152 ret = SSL_read(ssl, out, outl);
153
154 switch (SSL_get_error(ssl, ret)) {
155 case SSL_ERROR_NONE:
156 if (ret <= 0)
157 break;
158 if (sb->renegotiate_count > 0) {
159 sb->byte_count += ret;
160 if (sb->byte_count > sb->renegotiate_count) {
161 sb->byte_count = 0;
162 sb->num_renegotiates++;
163 SSL_renegotiate(ssl);
164 r = 1;
165 }
166 }
167 if ((sb->renegotiate_timeout > 0) && (!r)) {
168 unsigned long tm;
169
170 tm = (unsigned long)time(NULL);
171 if (tm > sb->last_time + sb->renegotiate_timeout) {
172 sb->last_time = tm;
173 sb->num_renegotiates++;
174 SSL_renegotiate(ssl);
175 }
176 }
177
178 break;
179 case SSL_ERROR_WANT_READ:
180 BIO_set_retry_read(b);
181 break;
182 case SSL_ERROR_WANT_WRITE:
183 BIO_set_retry_write(b);
184 break;
185 case SSL_ERROR_WANT_X509_LOOKUP:
186 BIO_set_retry_special(b);
187 retry_reason = BIO_RR_SSL_X509_LOOKUP;
188 break;
189 case SSL_ERROR_WANT_ACCEPT:
190 BIO_set_retry_special(b);
191 retry_reason = BIO_RR_ACCEPT;
192 break;
193 case SSL_ERROR_WANT_CONNECT:
194 BIO_set_retry_special(b);
195 retry_reason = BIO_RR_CONNECT;
196 break;
197 case SSL_ERROR_SYSCALL:
198 case SSL_ERROR_SSL:
199 case SSL_ERROR_ZERO_RETURN:
200 default:
201 break;
202 }
203
a146ae55 204 BIO_set_retry_reason(b, retry_reason);
0f113f3e
MC
205 return (ret);
206}
d02b48c6 207
0e1c0612 208static int ssl_write(BIO *b, const char *out, int outl)
0f113f3e
MC
209{
210 int ret, r = 0;
211 int retry_reason = 0;
212 SSL *ssl;
213 BIO_SSL *bs;
214
215 if (out == NULL)
216 return (0);
a146ae55 217 bs = BIO_get_data(b);
0f113f3e
MC
218 ssl = bs->ssl;
219
220 BIO_clear_retry_flags(b);
221
222 /*
223 * ret=SSL_do_handshake(ssl); if (ret > 0)
224 */
225 ret = SSL_write(ssl, out, outl);
226
227 switch (SSL_get_error(ssl, ret)) {
228 case SSL_ERROR_NONE:
229 if (ret <= 0)
230 break;
231 if (bs->renegotiate_count > 0) {
232 bs->byte_count += ret;
233 if (bs->byte_count > bs->renegotiate_count) {
234 bs->byte_count = 0;
235 bs->num_renegotiates++;
236 SSL_renegotiate(ssl);
237 r = 1;
238 }
239 }
240 if ((bs->renegotiate_timeout > 0) && (!r)) {
241 unsigned long tm;
242
243 tm = (unsigned long)time(NULL);
244 if (tm > bs->last_time + bs->renegotiate_timeout) {
245 bs->last_time = tm;
246 bs->num_renegotiates++;
247 SSL_renegotiate(ssl);
248 }
249 }
250 break;
251 case SSL_ERROR_WANT_WRITE:
252 BIO_set_retry_write(b);
253 break;
254 case SSL_ERROR_WANT_READ:
255 BIO_set_retry_read(b);
256 break;
257 case SSL_ERROR_WANT_X509_LOOKUP:
258 BIO_set_retry_special(b);
259 retry_reason = BIO_RR_SSL_X509_LOOKUP;
260 break;
261 case SSL_ERROR_WANT_CONNECT:
262 BIO_set_retry_special(b);
263 retry_reason = BIO_RR_CONNECT;
264 case SSL_ERROR_SYSCALL:
265 case SSL_ERROR_SSL:
266 default:
267 break;
268 }
269
a146ae55
MC
270 BIO_set_retry_reason(b, retry_reason);
271 return ret;
0f113f3e 272}
d02b48c6 273
0e1c0612 274static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr)
0f113f3e
MC
275{
276 SSL **sslp, *ssl;
a146ae55 277 BIO_SSL *bs, *dbs;
0f113f3e
MC
278 BIO *dbio, *bio;
279 long ret = 1;
a146ae55 280 BIO *next;
0f113f3e 281
a146ae55
MC
282 bs = BIO_get_data(b);
283 next = BIO_next(b);
0f113f3e
MC
284 ssl = bs->ssl;
285 if ((ssl == NULL) && (cmd != BIO_C_SET_SSL))
286 return (0);
287 switch (cmd) {
288 case BIO_CTRL_RESET:
289 SSL_shutdown(ssl);
290
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);
295
61986d32 296 if (!SSL_clear(ssl)) {
69f68237
MC
297 ret = 0;
298 break;
299 }
0f113f3e 300
a146ae55
MC
301 if (next != NULL)
302 ret = BIO_ctrl(next, cmd, num, ptr);
0f113f3e
MC
303 else if (ssl->rbio != NULL)
304 ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);
305 else
306 ret = 1;
307 break;
308 case BIO_CTRL_INFO:
309 ret = 0;
310 break;
311 case BIO_C_SSL_MODE:
312 if (num) /* client mode */
313 SSL_set_connect_state(ssl);
314 else
315 SSL_set_accept_state(ssl);
316 break;
317 case BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT:
318 ret = bs->renegotiate_timeout;
319 if (num < 60)
320 num = 5;
321 bs->renegotiate_timeout = (unsigned long)num;
322 bs->last_time = (unsigned long)time(NULL);
323 break;
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;
328 break;
329 case BIO_C_GET_SSL_NUM_RENEGOTIATES:
330 ret = bs->num_renegotiates;
331 break;
332 case BIO_C_SET_SSL:
333 if (ssl != NULL) {
334 ssl_free(b);
335 if (!ssl_new(b))
336 return 0;
337 }
a146ae55 338 BIO_set_shutdown(b, num);
0f113f3e 339 ssl = (SSL *)ptr;
a146ae55 340 bs->ssl = ssl;
0f113f3e
MC
341 bio = SSL_get_rbio(ssl);
342 if (bio != NULL) {
a146ae55
MC
343 if (next != NULL)
344 BIO_push(bio, next);
345 BIO_set_next(b, bio);
fb46be03 346 BIO_up_ref(bio);
0f113f3e 347 }
a146ae55 348 BIO_set_init(b, 1);
0f113f3e
MC
349 break;
350 case BIO_C_GET_SSL:
351 if (ptr != NULL) {
352 sslp = (SSL **)ptr;
353 *sslp = ssl;
354 } else
355 ret = 0;
356 break;
357 case BIO_CTRL_GET_CLOSE:
a146ae55 358 ret = BIO_get_shutdown(b);
0f113f3e
MC
359 break;
360 case BIO_CTRL_SET_CLOSE:
a146ae55 361 BIO_set_shutdown(b, (int)num);
0f113f3e
MC
362 break;
363 case BIO_CTRL_WPENDING:
364 ret = BIO_ctrl(ssl->wbio, cmd, num, ptr);
365 break;
366 case BIO_CTRL_PENDING:
367 ret = SSL_pending(ssl);
368 if (ret == 0)
369 ret = BIO_pending(ssl->rbio);
370 break;
371 case BIO_CTRL_FLUSH:
372 BIO_clear_retry_flags(b);
373 ret = BIO_ctrl(ssl->wbio, cmd, num, ptr);
374 BIO_copy_next_retry(b);
375 break;
376 case BIO_CTRL_PUSH:
a146ae55
MC
377 if ((next != NULL) && (next != ssl->rbio)) {
378 SSL_set_bio(ssl, next, next);
fb46be03 379 BIO_up_ref(b);
0f113f3e
MC
380 }
381 break;
382 case BIO_CTRL_POP:
383 /* Only detach if we are the BIO explicitly being popped */
384 if (b == ptr) {
385 /*
386 * Shouldn't happen in practice because the rbio and wbio are the
387 * same when pushed.
388 */
389 if (ssl->rbio != ssl->wbio)
390 BIO_free_all(ssl->wbio);
a146ae55
MC
391 if (next != NULL)
392 BIO_free(next);
0f113f3e
MC
393 ssl->wbio = NULL;
394 ssl->rbio = NULL;
395 }
396 break;
397 case BIO_C_DO_STATE_MACHINE:
398 BIO_clear_retry_flags(b);
399
a146ae55 400 BIO_set_retry_reason(b, 0);
0f113f3e
MC
401 ret = (int)SSL_do_handshake(ssl);
402
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);
406 break;
407 case SSL_ERROR_WANT_WRITE:
408 BIO_set_flags(b, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY);
409 break;
410 case SSL_ERROR_WANT_CONNECT:
411 BIO_set_flags(b, BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY);
a146ae55 412 BIO_set_retry_reason(b, BIO_get_retry_reason(next));
0f113f3e 413 break;
f1c412c9
DSH
414 case SSL_ERROR_WANT_X509_LOOKUP:
415 BIO_set_retry_special(b);
a146ae55 416 BIO_set_retry_reason(b, BIO_RR_SSL_X509_LOOKUP);
f1c412c9 417 break;
0f113f3e
MC
418 default:
419 break;
420 }
421 break;
422 case BIO_CTRL_DUP:
423 dbio = (BIO *)ptr;
a146ae55
MC
424 dbs = BIO_get_data(dbio);
425 SSL_free(dbs->ssl);
426 dbs->ssl = SSL_dup(ssl);
427 dbs->renegotiate_count = dbs->renegotiate_count;
428 dbs->byte_count = dbs->byte_count;
429 dbs->renegotiate_timeout = dbs->renegotiate_timeout;
430 dbs->last_time = dbs->last_time;
431 ret = (dbs->ssl != NULL);
0f113f3e
MC
432 break;
433 case BIO_C_GET_FD:
434 ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);
435 break;
436 case BIO_CTRL_SET_CALLBACK:
437 {
438#if 0 /* FIXME: Should this be used? -- Richard
439 * Levitte */
440 SSLerr(SSL_F_SSL_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
441 ret = -1;
d3442bc7 442#else
0f113f3e 443 ret = 0;
d3442bc7 444#endif
0f113f3e
MC
445 }
446 break;
447 case BIO_CTRL_GET_CALLBACK:
448 {
449 void (**fptr) (const SSL *xssl, int type, int val);
450
451 fptr = (void (**)(const SSL *xssl, int type, int val))ptr;
452 *fptr = SSL_get_info_callback(ssl);
453 }
454 break;
455 default:
456 ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);
457 break;
458 }
459 return (ret);
460}
d02b48c6 461
13083215 462static long ssl_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
0f113f3e
MC
463{
464 SSL *ssl;
465 BIO_SSL *bs;
466 long ret = 1;
467
a146ae55 468 bs = BIO_get_data(b);
0f113f3e
MC
469 ssl = bs->ssl;
470 switch (cmd) {
471 case BIO_CTRL_SET_CALLBACK:
472 {
473 /*
474 * FIXME: setting this via a completely different prototype seems
475 * like a crap idea
476 */
477 SSL_set_info_callback(ssl, (void (*)(const SSL *, int, int))fp);
478 }
479 break;
480 default:
481 ret = BIO_callback_ctrl(ssl->rbio, cmd, fp);
482 break;
483 }
484 return (ret);
485}
d3442bc7 486
0e1c0612 487static int ssl_puts(BIO *bp, const char *str)
0f113f3e
MC
488{
489 int n, ret;
d02b48c6 490
0f113f3e
MC
491 n = strlen(str);
492 ret = BIO_write(bp, str, n);
493 return (ret);
494}
d02b48c6 495
6b691a5c 496BIO *BIO_new_buffer_ssl_connect(SSL_CTX *ctx)
0f113f3e 497{
85d686e7 498#ifndef OPENSSL_NO_SOCK
0f113f3e
MC
499 BIO *ret = NULL, *buf = NULL, *ssl = NULL;
500
501 if ((buf = BIO_new(BIO_f_buffer())) == NULL)
502 return (NULL);
503 if ((ssl = BIO_new_ssl_connect(ctx)) == NULL)
504 goto err;
505 if ((ret = BIO_push(buf, ssl)) == NULL)
506 goto err;
507 return (ret);
508 err:
ca3a82c3
RS
509 BIO_free(buf);
510 BIO_free(ssl);
85d686e7 511#endif
0f113f3e
MC
512 return (NULL);
513}
58964a49 514
6b691a5c 515BIO *BIO_new_ssl_connect(SSL_CTX *ctx)
0f113f3e 516{
4a1fbd13 517#ifndef OPENSSL_NO_SOCK
0f113f3e
MC
518 BIO *ret = NULL, *con = NULL, *ssl = NULL;
519
520 if ((con = BIO_new(BIO_s_connect())) == NULL)
521 return (NULL);
522 if ((ssl = BIO_new_ssl(ctx, 1)) == NULL)
523 goto err;
524 if ((ret = BIO_push(ssl, con)) == NULL)
525 goto err;
526 return (ret);
527 err:
ca3a82c3 528 BIO_free(con);
4a1fbd13 529#endif
0f113f3e
MC
530 return (NULL);
531}
58964a49 532
6b691a5c 533BIO *BIO_new_ssl(SSL_CTX *ctx, int client)
0f113f3e
MC
534{
535 BIO *ret;
536 SSL *ssl;
537
538 if ((ret = BIO_new(BIO_f_ssl())) == NULL)
539 return (NULL);
540 if ((ssl = SSL_new(ctx)) == NULL) {
541 BIO_free(ret);
542 return (NULL);
543 }
544 if (client)
545 SSL_set_connect_state(ssl);
546 else
547 SSL_set_accept_state(ssl);
548
549 BIO_set_ssl(ret, ssl, BIO_CLOSE);
550 return (ret);
551}
d02b48c6 552
6b691a5c 553int BIO_ssl_copy_session_id(BIO *t, BIO *f)
0f113f3e 554{
a146ae55 555 BIO_SSL *tdata, *fdata;
0f113f3e
MC
556 t = BIO_find_type(t, BIO_TYPE_SSL);
557 f = BIO_find_type(f, BIO_TYPE_SSL);
558 if ((t == NULL) || (f == NULL))
a146ae55
MC
559 return 0;
560 tdata = BIO_get_data(t);
561 fdata = BIO_get_data(f);
562 if ((tdata->ssl == NULL) || (fdata->ssl == NULL))
0f113f3e 563 return (0);
a146ae55 564 if (!SSL_copy_session_id(tdata->ssl, (fdata->ssl)))
17dd65e6 565 return 0;
0f113f3e
MC
566 return (1);
567}
d02b48c6 568
6b691a5c 569void BIO_ssl_shutdown(BIO *b)
0f113f3e
MC
570{
571 SSL *s;
572
a146ae55
MC
573 b = BIO_find_type(b, BIO_TYPE_SSL);
574 if (b == NULL)
575 return;
576
577 s = BIO_get_data(b);
578 SSL_shutdown(s);
0f113f3e 579}