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