]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/bio_ssl.c
Stop raising ERR_R_MALLOC_FAILURE in most places
[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;
0f113f3e 388 default:
38b051a1 389 ret = BIO_ctrl(sc->rbio, cmd, num, ptr);
0f113f3e
MC
390 break;
391 }
26a7d938 392 return ret;
0f113f3e 393}
d02b48c6 394
fce78bd4 395static long ssl_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
0f113f3e
MC
396{
397 SSL *ssl;
398 BIO_SSL *bs;
399 long ret = 1;
400
a146ae55 401 bs = BIO_get_data(b);
0f113f3e
MC
402 ssl = bs->ssl;
403 switch (cmd) {
404 case BIO_CTRL_SET_CALLBACK:
38b051a1 405 ret = BIO_callback_ctrl(SSL_get_rbio(ssl), cmd, fp);
0f113f3e
MC
406 break;
407 default:
fce78bd4 408 ret = 0;
0f113f3e
MC
409 break;
410 }
26a7d938 411 return ret;
0f113f3e 412}
d3442bc7 413
0e1c0612 414static int ssl_puts(BIO *bp, const char *str)
0f113f3e
MC
415{
416 int n, ret;
d02b48c6 417
0f113f3e
MC
418 n = strlen(str);
419 ret = BIO_write(bp, str, n);
26a7d938 420 return ret;
0f113f3e 421}
d02b48c6 422
6b691a5c 423BIO *BIO_new_buffer_ssl_connect(SSL_CTX *ctx)
0f113f3e 424{
85d686e7 425#ifndef OPENSSL_NO_SOCK
0f113f3e
MC
426 BIO *ret = NULL, *buf = NULL, *ssl = NULL;
427
428 if ((buf = BIO_new(BIO_f_buffer())) == NULL)
26a7d938 429 return NULL;
0f113f3e
MC
430 if ((ssl = BIO_new_ssl_connect(ctx)) == NULL)
431 goto err;
432 if ((ret = BIO_push(buf, ssl)) == NULL)
433 goto err;
26a7d938 434 return ret;
0f113f3e 435 err:
ca3a82c3
RS
436 BIO_free(buf);
437 BIO_free(ssl);
85d686e7 438#endif
26a7d938 439 return NULL;
0f113f3e 440}
58964a49 441
6b691a5c 442BIO *BIO_new_ssl_connect(SSL_CTX *ctx)
0f113f3e 443{
4a1fbd13 444#ifndef OPENSSL_NO_SOCK
0f113f3e
MC
445 BIO *ret = NULL, *con = NULL, *ssl = NULL;
446
447 if ((con = BIO_new(BIO_s_connect())) == NULL)
26a7d938 448 return NULL;
0f113f3e
MC
449 if ((ssl = BIO_new_ssl(ctx, 1)) == NULL)
450 goto err;
451 if ((ret = BIO_push(ssl, con)) == NULL)
452 goto err;
26a7d938 453 return ret;
0f113f3e 454 err:
7947a1eb 455 BIO_free(ssl);
ca3a82c3 456 BIO_free(con);
4a1fbd13 457#endif
26a7d938 458 return NULL;
0f113f3e 459}
58964a49 460
6b691a5c 461BIO *BIO_new_ssl(SSL_CTX *ctx, int client)
0f113f3e
MC
462{
463 BIO *ret;
464 SSL *ssl;
465
466 if ((ret = BIO_new(BIO_f_ssl())) == NULL)
26a7d938 467 return NULL;
0f113f3e
MC
468 if ((ssl = SSL_new(ctx)) == NULL) {
469 BIO_free(ret);
26a7d938 470 return NULL;
0f113f3e
MC
471 }
472 if (client)
473 SSL_set_connect_state(ssl);
474 else
475 SSL_set_accept_state(ssl);
476
477 BIO_set_ssl(ret, ssl, BIO_CLOSE);
26a7d938 478 return ret;
0f113f3e 479}
d02b48c6 480
6b691a5c 481int BIO_ssl_copy_session_id(BIO *t, BIO *f)
0f113f3e 482{
a146ae55 483 BIO_SSL *tdata, *fdata;
0f113f3e
MC
484 t = BIO_find_type(t, BIO_TYPE_SSL);
485 f = BIO_find_type(f, BIO_TYPE_SSL);
486 if ((t == NULL) || (f == NULL))
a146ae55
MC
487 return 0;
488 tdata = BIO_get_data(t);
489 fdata = BIO_get_data(f);
490 if ((tdata->ssl == NULL) || (fdata->ssl == NULL))
26a7d938 491 return 0;
a146ae55 492 if (!SSL_copy_session_id(tdata->ssl, (fdata->ssl)))
17dd65e6 493 return 0;
208fb891 494 return 1;
0f113f3e 495}
d02b48c6 496
6b691a5c 497void BIO_ssl_shutdown(BIO *b)
0f113f3e 498{
9015d34e
RS
499 BIO_SSL *bdata;
500
501 for (; b != NULL; b = BIO_next(b)) {
502 if (BIO_method_type(b) != BIO_TYPE_SSL)
503 continue;
504 bdata = BIO_get_data(b);
505 if (bdata != NULL && bdata->ssl != NULL)
506 SSL_shutdown(bdata->ssl);
507 }
0f113f3e 508}