]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/bio_ssl.c
QUIC fault testing TODOs are changed into regular comments
[thirdparty/openssl.git] / ssl / bio_ssl.c
CommitLineData
846e33c7 1/*
3c2bdd7d 2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
2c18d164 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
846e33c7
RS
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>
706457b7 17#include "ssl_local.h"
d02b48c6 18
f42fd819
MC
19static int ssl_write(BIO *h, const char *buf, size_t size, size_t *written);
20static int ssl_read(BIO *b, char *buf, size_t size, size_t *readbytes);
0e1c0612
UM
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);
fce78bd4 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;
8051ab2b 31 size_t byte_count;
0f113f3e
MC
32 unsigned long renegotiate_timeout;
33 unsigned long last_time;
34} BIO_SSL;
35
04f6b0fd 36static const BIO_METHOD methods_sslp = {
27ab9195
DB
37 BIO_TYPE_SSL,
38 "ssl",
0f113f3e 39 ssl_write,
b4ff6622 40 NULL, /* ssl_write_old, */
0f113f3e 41 ssl_read,
b4ff6622 42 NULL, /* ssl_read_old, */
0f113f3e 43 ssl_puts,
b4ff6622 44 NULL, /* ssl_gets, */
0f113f3e
MC
45 ssl_ctrl,
46 ssl_new,
47 ssl_free,
48 ssl_callback_ctrl,
49};
a9188d4e 50
04f6b0fd 51const BIO_METHOD *BIO_f_ssl(void)
0f113f3e 52{
26a7d938 53 return &methods_sslp;
0f113f3e 54}
d02b48c6 55
6b691a5c 56static int ssl_new(BIO *bi)
0f113f3e 57{
b51bce94 58 BIO_SSL *bs = OPENSSL_zalloc(sizeof(*bs));
0f113f3e 59
e077455e 60 if (bs == NULL)
26a7d938 61 return 0;
a146ae55
MC
62 BIO_set_init(bi, 0);
63 BIO_set_data(bi, bs);
64 /* Clear all flags */
65 BIO_clear_flags(bi, ~0);
66
67 return 1;
0f113f3e 68}
d02b48c6 69
6b691a5c 70static int ssl_free(BIO *a)
0f113f3e
MC
71{
72 BIO_SSL *bs;
73
74 if (a == NULL)
26a7d938 75 return 0;
a146ae55 76 bs = BIO_get_data(a);
a146ae55 77 if (BIO_get_shutdown(a)) {
dce910af
DDO
78 if (bs->ssl != NULL)
79 SSL_shutdown(bs->ssl);
a146ae55 80 if (BIO_get_init(a))
0f113f3e 81 SSL_free(bs->ssl);
dce910af 82 BIO_clear_flags(a, ~0); /* Clear all flags */
a146ae55 83 BIO_set_init(a, 0);
0f113f3e 84 }
a146ae55
MC
85 OPENSSL_free(bs);
86 return 1;
0f113f3e
MC
87}
88
f42fd819 89static int ssl_read(BIO *b, char *buf, size_t size, size_t *readbytes)
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
f42fd819
MC
97 if (buf == 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
4ee7d3f9 104 ret = ssl_read_internal(ssl, buf, size, readbytes);
0f113f3e
MC
105
106 switch (SSL_get_error(ssl, ret)) {
107 case SSL_ERROR_NONE:
0f113f3e 108 if (sb->renegotiate_count > 0) {
ac0edec1 109 sb->byte_count += *readbytes;
0f113f3e
MC
110 if (sb->byte_count > sb->renegotiate_count) {
111 sb->byte_count = 0;
112 sb->num_renegotiates++;
113 SSL_renegotiate(ssl);
114 r = 1;
115 }
116 }
117 if ((sb->renegotiate_timeout > 0) && (!r)) {
118 unsigned long tm;
119
120 tm = (unsigned long)time(NULL);
121 if (tm > sb->last_time + sb->renegotiate_timeout) {
122 sb->last_time = tm;
123 sb->num_renegotiates++;
124 SSL_renegotiate(ssl);
125 }
126 }
127
128 break;
129 case SSL_ERROR_WANT_READ:
130 BIO_set_retry_read(b);
131 break;
132 case SSL_ERROR_WANT_WRITE:
133 BIO_set_retry_write(b);
134 break;
135 case SSL_ERROR_WANT_X509_LOOKUP:
136 BIO_set_retry_special(b);
137 retry_reason = BIO_RR_SSL_X509_LOOKUP;
138 break;
139 case SSL_ERROR_WANT_ACCEPT:
140 BIO_set_retry_special(b);
141 retry_reason = BIO_RR_ACCEPT;
142 break;
143 case SSL_ERROR_WANT_CONNECT:
144 BIO_set_retry_special(b);
145 retry_reason = BIO_RR_CONNECT;
146 break;
147 case SSL_ERROR_SYSCALL:
148 case SSL_ERROR_SSL:
149 case SSL_ERROR_ZERO_RETURN:
150 default:
151 break;
152 }
153
a146ae55 154 BIO_set_retry_reason(b, retry_reason);
d07aee2c 155
3befffa3 156 return ret;
0f113f3e 157}
d02b48c6 158
f42fd819 159static int ssl_write(BIO *b, const char *buf, size_t size, size_t *written)
0f113f3e
MC
160{
161 int ret, r = 0;
162 int retry_reason = 0;
163 SSL *ssl;
164 BIO_SSL *bs;
165
f42fd819
MC
166 if (buf == NULL)
167 return 0;
a146ae55 168 bs = BIO_get_data(b);
0f113f3e
MC
169 ssl = bs->ssl;
170
171 BIO_clear_retry_flags(b);
172
4ee7d3f9 173 ret = ssl_write_internal(ssl, buf, size, written);
0f113f3e
MC
174
175 switch (SSL_get_error(ssl, ret)) {
176 case SSL_ERROR_NONE:
0f113f3e 177 if (bs->renegotiate_count > 0) {
8051ab2b 178 bs->byte_count += *written;
0f113f3e
MC
179 if (bs->byte_count > bs->renegotiate_count) {
180 bs->byte_count = 0;
181 bs->num_renegotiates++;
182 SSL_renegotiate(ssl);
183 r = 1;
184 }
185 }
186 if ((bs->renegotiate_timeout > 0) && (!r)) {
187 unsigned long tm;
188
189 tm = (unsigned long)time(NULL);
190 if (tm > bs->last_time + bs->renegotiate_timeout) {
191 bs->last_time = tm;
192 bs->num_renegotiates++;
193 SSL_renegotiate(ssl);
194 }
195 }
196 break;
197 case SSL_ERROR_WANT_WRITE:
198 BIO_set_retry_write(b);
199 break;
200 case SSL_ERROR_WANT_READ:
201 BIO_set_retry_read(b);
202 break;
203 case SSL_ERROR_WANT_X509_LOOKUP:
204 BIO_set_retry_special(b);
205 retry_reason = BIO_RR_SSL_X509_LOOKUP;
206 break;
207 case SSL_ERROR_WANT_CONNECT:
208 BIO_set_retry_special(b);
209 retry_reason = BIO_RR_CONNECT;
210 case SSL_ERROR_SYSCALL:
211 case SSL_ERROR_SSL:
212 default:
213 break;
214 }
215
a146ae55 216 BIO_set_retry_reason(b, retry_reason);
3befffa3 217
a146ae55 218 return ret;
0f113f3e 219}
d02b48c6 220
0e1c0612 221static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr)
0f113f3e
MC
222{
223 SSL **sslp, *ssl;
a146ae55 224 BIO_SSL *bs, *dbs;
0f113f3e
MC
225 BIO *dbio, *bio;
226 long ret = 1;
a146ae55 227 BIO *next;
38b051a1 228 SSL_CONNECTION *sc = NULL;
0f113f3e 229
a146ae55
MC
230 bs = BIO_get_data(b);
231 next = BIO_next(b);
0f113f3e 232 ssl = bs->ssl;
38b051a1
TM
233 if ((ssl == NULL
234 || (sc = SSL_CONNECTION_FROM_SSL(ssl)) == NULL)
235 && cmd != BIO_C_SET_SSL)
26a7d938 236 return 0;
38b051a1 237 /* TODO(QUIC): The rbio/wbio might be from QUIC_CONNECTION instead */
0f113f3e
MC
238 switch (cmd) {
239 case BIO_CTRL_RESET:
240 SSL_shutdown(ssl);
241
38b051a1 242 if (sc->handshake_func == ssl->method->ssl_connect)
0f113f3e 243 SSL_set_connect_state(ssl);
38b051a1 244 else if (sc->handshake_func == ssl->method->ssl_accept)
0f113f3e
MC
245 SSL_set_accept_state(ssl);
246
61986d32 247 if (!SSL_clear(ssl)) {
69f68237
MC
248 ret = 0;
249 break;
250 }
0f113f3e 251
a146ae55
MC
252 if (next != NULL)
253 ret = BIO_ctrl(next, cmd, num, ptr);
38b051a1
TM
254 else if (sc->rbio != NULL)
255 ret = BIO_ctrl(sc->rbio, cmd, num, ptr);
0f113f3e
MC
256 else
257 ret = 1;
258 break;
259 case BIO_CTRL_INFO:
260 ret = 0;
261 break;
262 case BIO_C_SSL_MODE:
263 if (num) /* client mode */
264 SSL_set_connect_state(ssl);
265 else
266 SSL_set_accept_state(ssl);
267 break;
268 case BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT:
269 ret = bs->renegotiate_timeout;
270 if (num < 60)
271 num = 5;
272 bs->renegotiate_timeout = (unsigned long)num;
273 bs->last_time = (unsigned long)time(NULL);
274 break;
275 case BIO_C_SET_SSL_RENEGOTIATE_BYTES:
276 ret = bs->renegotiate_count;
277 if ((long)num >= 512)
278 bs->renegotiate_count = (unsigned long)num;
279 break;
280 case BIO_C_GET_SSL_NUM_RENEGOTIATES:
281 ret = bs->num_renegotiates;
282 break;
283 case BIO_C_SET_SSL:
284 if (ssl != NULL) {
285 ssl_free(b);
286 if (!ssl_new(b))
287 return 0;
73d6b4ef 288 bs = BIO_get_data(b);
0f113f3e 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:
38b051a1 316 ret = BIO_ctrl(sc->wbio, cmd, num, ptr);
0f113f3e
MC
317 break;
318 case BIO_CTRL_PENDING:
319 ret = SSL_pending(ssl);
320 if (ret == 0)
38b051a1 321 ret = BIO_pending(sc->rbio);
0f113f3e
MC
322 break;
323 case BIO_CTRL_FLUSH:
324 BIO_clear_retry_flags(b);
38b051a1 325 ret = BIO_ctrl(sc->wbio, cmd, num, ptr);
0f113f3e
MC
326 BIO_copy_next_retry(b);
327 break;
328 case BIO_CTRL_PUSH:
38b051a1 329 if ((next != NULL) && (next != sc->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) {
b46fe860
MC
341 /* This will clear the reference we obtained during push */
342 SSL_set_bio(ssl, NULL, NULL);
0f113f3e
MC
343 }
344 break;
345 case BIO_C_DO_STATE_MACHINE:
346 BIO_clear_retry_flags(b);
347
a146ae55 348 BIO_set_retry_reason(b, 0);
0f113f3e
MC
349 ret = (int)SSL_do_handshake(ssl);
350
351 switch (SSL_get_error(ssl, (int)ret)) {
352 case SSL_ERROR_WANT_READ:
353 BIO_set_flags(b, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY);
354 break;
355 case SSL_ERROR_WANT_WRITE:
356 BIO_set_flags(b, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY);
357 break;
358 case SSL_ERROR_WANT_CONNECT:
359 BIO_set_flags(b, BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY);
a146ae55 360 BIO_set_retry_reason(b, BIO_get_retry_reason(next));
0f113f3e 361 break;
f1c412c9
DSH
362 case SSL_ERROR_WANT_X509_LOOKUP:
363 BIO_set_retry_special(b);
a146ae55 364 BIO_set_retry_reason(b, BIO_RR_SSL_X509_LOOKUP);
f1c412c9 365 break;
0f113f3e
MC
366 default:
367 break;
368 }
369 break;
370 case BIO_CTRL_DUP:
371 dbio = (BIO *)ptr;
a146ae55
MC
372 dbs = BIO_get_data(dbio);
373 SSL_free(dbs->ssl);
374 dbs->ssl = SSL_dup(ssl);
dbd5c34f
MC
375 dbs->num_renegotiates = bs->num_renegotiates;
376 dbs->renegotiate_count = bs->renegotiate_count;
377 dbs->byte_count = bs->byte_count;
378 dbs->renegotiate_timeout = bs->renegotiate_timeout;
379 dbs->last_time = bs->last_time;
a146ae55 380 ret = (dbs->ssl != NULL);
0f113f3e
MC
381 break;
382 case BIO_C_GET_FD:
38b051a1 383 ret = BIO_ctrl(sc->rbio, cmd, num, ptr);
0f113f3e
MC
384 break;
385 case BIO_CTRL_SET_CALLBACK:
2722ff50 386 ret = 0; /* use callback ctrl */
0f113f3e 387 break;
68801bcb
HL
388 case BIO_CTRL_GET_RPOLL_DESCRIPTOR:
389 if (!SSL_get_rpoll_descriptor(ssl, (BIO_POLL_DESCRIPTOR *)ptr))
390 ret = 0;
391 break;
392 case BIO_CTRL_GET_WPOLL_DESCRIPTOR:
393 if (!SSL_get_wpoll_descriptor(ssl, (BIO_POLL_DESCRIPTOR *)ptr))
394 ret = 0;
395 break;
0f113f3e 396 default:
38b051a1 397 ret = BIO_ctrl(sc->rbio, cmd, num, ptr);
0f113f3e
MC
398 break;
399 }
26a7d938 400 return ret;
0f113f3e 401}
d02b48c6 402
fce78bd4 403static long ssl_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
0f113f3e
MC
404{
405 SSL *ssl;
406 BIO_SSL *bs;
407 long ret = 1;
408
a146ae55 409 bs = BIO_get_data(b);
0f113f3e
MC
410 ssl = bs->ssl;
411 switch (cmd) {
412 case BIO_CTRL_SET_CALLBACK:
38b051a1 413 ret = BIO_callback_ctrl(SSL_get_rbio(ssl), cmd, fp);
0f113f3e
MC
414 break;
415 default:
fce78bd4 416 ret = 0;
0f113f3e
MC
417 break;
418 }
26a7d938 419 return ret;
0f113f3e 420}
d3442bc7 421
0e1c0612 422static int ssl_puts(BIO *bp, const char *str)
0f113f3e
MC
423{
424 int n, ret;
d02b48c6 425
0f113f3e
MC
426 n = strlen(str);
427 ret = BIO_write(bp, str, n);
26a7d938 428 return ret;
0f113f3e 429}
d02b48c6 430
6b691a5c 431BIO *BIO_new_buffer_ssl_connect(SSL_CTX *ctx)
0f113f3e 432{
85d686e7 433#ifndef OPENSSL_NO_SOCK
0f113f3e
MC
434 BIO *ret = NULL, *buf = NULL, *ssl = NULL;
435
436 if ((buf = BIO_new(BIO_f_buffer())) == NULL)
26a7d938 437 return NULL;
0f113f3e
MC
438 if ((ssl = BIO_new_ssl_connect(ctx)) == NULL)
439 goto err;
440 if ((ret = BIO_push(buf, ssl)) == NULL)
441 goto err;
26a7d938 442 return ret;
0f113f3e 443 err:
ca3a82c3
RS
444 BIO_free(buf);
445 BIO_free(ssl);
85d686e7 446#endif
26a7d938 447 return NULL;
0f113f3e 448}
58964a49 449
6b691a5c 450BIO *BIO_new_ssl_connect(SSL_CTX *ctx)
0f113f3e 451{
4a1fbd13 452#ifndef OPENSSL_NO_SOCK
0f113f3e
MC
453 BIO *ret = NULL, *con = NULL, *ssl = NULL;
454
455 if ((con = BIO_new(BIO_s_connect())) == NULL)
26a7d938 456 return NULL;
0f113f3e
MC
457 if ((ssl = BIO_new_ssl(ctx, 1)) == NULL)
458 goto err;
459 if ((ret = BIO_push(ssl, con)) == NULL)
460 goto err;
26a7d938 461 return ret;
0f113f3e 462 err:
7947a1eb 463 BIO_free(ssl);
ca3a82c3 464 BIO_free(con);
4a1fbd13 465#endif
26a7d938 466 return NULL;
0f113f3e 467}
58964a49 468
6b691a5c 469BIO *BIO_new_ssl(SSL_CTX *ctx, int client)
0f113f3e
MC
470{
471 BIO *ret;
472 SSL *ssl;
473
474 if ((ret = BIO_new(BIO_f_ssl())) == NULL)
26a7d938 475 return NULL;
0f113f3e
MC
476 if ((ssl = SSL_new(ctx)) == NULL) {
477 BIO_free(ret);
26a7d938 478 return NULL;
0f113f3e
MC
479 }
480 if (client)
481 SSL_set_connect_state(ssl);
482 else
483 SSL_set_accept_state(ssl);
484
485 BIO_set_ssl(ret, ssl, BIO_CLOSE);
26a7d938 486 return ret;
0f113f3e 487}
d02b48c6 488
6b691a5c 489int BIO_ssl_copy_session_id(BIO *t, BIO *f)
0f113f3e 490{
a146ae55 491 BIO_SSL *tdata, *fdata;
0f113f3e
MC
492 t = BIO_find_type(t, BIO_TYPE_SSL);
493 f = BIO_find_type(f, BIO_TYPE_SSL);
494 if ((t == NULL) || (f == NULL))
a146ae55
MC
495 return 0;
496 tdata = BIO_get_data(t);
497 fdata = BIO_get_data(f);
498 if ((tdata->ssl == NULL) || (fdata->ssl == NULL))
26a7d938 499 return 0;
a146ae55 500 if (!SSL_copy_session_id(tdata->ssl, (fdata->ssl)))
17dd65e6 501 return 0;
208fb891 502 return 1;
0f113f3e 503}
d02b48c6 504
6b691a5c 505void BIO_ssl_shutdown(BIO *b)
0f113f3e 506{
9015d34e
RS
507 BIO_SSL *bdata;
508
509 for (; b != NULL; b = BIO_next(b)) {
510 if (BIO_method_type(b) != BIO_TYPE_SSL)
511 continue;
512 bdata = BIO_get_data(b);
513 if (bdata != NULL && bdata->ssl != NULL)
514 SSL_shutdown(bdata->ssl);
515 }
0f113f3e 516}