]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/bio_ssl.c
aarch64: support BTI and pointer authentication in assembly
[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
0f113f3e 60 if (bs == NULL) {
6849b73c 61 ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
26a7d938 62 return 0;
0f113f3e 63 }
a146ae55
MC
64 BIO_set_init(bi, 0);
65 BIO_set_data(bi, bs);
66 /* Clear all flags */
67 BIO_clear_flags(bi, ~0);
68
69 return 1;
0f113f3e 70}
d02b48c6 71
6b691a5c 72static int ssl_free(BIO *a)
0f113f3e
MC
73{
74 BIO_SSL *bs;
75
76 if (a == NULL)
26a7d938 77 return 0;
a146ae55 78 bs = BIO_get_data(a);
0f113f3e
MC
79 if (bs->ssl != NULL)
80 SSL_shutdown(bs->ssl);
a146ae55
MC
81 if (BIO_get_shutdown(a)) {
82 if (BIO_get_init(a))
0f113f3e 83 SSL_free(bs->ssl);
a146ae55
MC
84 /* Clear all flags */
85 BIO_clear_flags(a, ~0);
86 BIO_set_init(a, 0);
0f113f3e 87 }
a146ae55
MC
88 OPENSSL_free(bs);
89 return 1;
0f113f3e
MC
90}
91
f42fd819 92static int ssl_read(BIO *b, char *buf, size_t size, size_t *readbytes)
0f113f3e
MC
93{
94 int ret = 1;
95 BIO_SSL *sb;
96 SSL *ssl;
97 int retry_reason = 0;
98 int r = 0;
d02b48c6 99
f42fd819
MC
100 if (buf == NULL)
101 return 0;
a146ae55 102 sb = BIO_get_data(b);
0f113f3e 103 ssl = sb->ssl;
d02b48c6 104
0f113f3e 105 BIO_clear_retry_flags(b);
d02b48c6 106
4ee7d3f9 107 ret = ssl_read_internal(ssl, buf, size, readbytes);
0f113f3e
MC
108
109 switch (SSL_get_error(ssl, ret)) {
110 case SSL_ERROR_NONE:
0f113f3e 111 if (sb->renegotiate_count > 0) {
ac0edec1 112 sb->byte_count += *readbytes;
0f113f3e
MC
113 if (sb->byte_count > sb->renegotiate_count) {
114 sb->byte_count = 0;
115 sb->num_renegotiates++;
116 SSL_renegotiate(ssl);
117 r = 1;
118 }
119 }
120 if ((sb->renegotiate_timeout > 0) && (!r)) {
121 unsigned long tm;
122
123 tm = (unsigned long)time(NULL);
124 if (tm > sb->last_time + sb->renegotiate_timeout) {
125 sb->last_time = tm;
126 sb->num_renegotiates++;
127 SSL_renegotiate(ssl);
128 }
129 }
130
131 break;
132 case SSL_ERROR_WANT_READ:
133 BIO_set_retry_read(b);
134 break;
135 case SSL_ERROR_WANT_WRITE:
136 BIO_set_retry_write(b);
137 break;
138 case SSL_ERROR_WANT_X509_LOOKUP:
139 BIO_set_retry_special(b);
140 retry_reason = BIO_RR_SSL_X509_LOOKUP;
141 break;
142 case SSL_ERROR_WANT_ACCEPT:
143 BIO_set_retry_special(b);
144 retry_reason = BIO_RR_ACCEPT;
145 break;
146 case SSL_ERROR_WANT_CONNECT:
147 BIO_set_retry_special(b);
148 retry_reason = BIO_RR_CONNECT;
149 break;
150 case SSL_ERROR_SYSCALL:
151 case SSL_ERROR_SSL:
152 case SSL_ERROR_ZERO_RETURN:
153 default:
154 break;
155 }
156
a146ae55 157 BIO_set_retry_reason(b, retry_reason);
d07aee2c 158
3befffa3 159 return ret;
0f113f3e 160}
d02b48c6 161
f42fd819 162static int ssl_write(BIO *b, const char *buf, size_t size, size_t *written)
0f113f3e
MC
163{
164 int ret, r = 0;
165 int retry_reason = 0;
166 SSL *ssl;
167 BIO_SSL *bs;
168
f42fd819
MC
169 if (buf == NULL)
170 return 0;
a146ae55 171 bs = BIO_get_data(b);
0f113f3e
MC
172 ssl = bs->ssl;
173
174 BIO_clear_retry_flags(b);
175
4ee7d3f9 176 ret = ssl_write_internal(ssl, buf, size, written);
0f113f3e
MC
177
178 switch (SSL_get_error(ssl, ret)) {
179 case SSL_ERROR_NONE:
0f113f3e 180 if (bs->renegotiate_count > 0) {
8051ab2b 181 bs->byte_count += *written;
0f113f3e
MC
182 if (bs->byte_count > bs->renegotiate_count) {
183 bs->byte_count = 0;
184 bs->num_renegotiates++;
185 SSL_renegotiate(ssl);
186 r = 1;
187 }
188 }
189 if ((bs->renegotiate_timeout > 0) && (!r)) {
190 unsigned long tm;
191
192 tm = (unsigned long)time(NULL);
193 if (tm > bs->last_time + bs->renegotiate_timeout) {
194 bs->last_time = tm;
195 bs->num_renegotiates++;
196 SSL_renegotiate(ssl);
197 }
198 }
199 break;
200 case SSL_ERROR_WANT_WRITE:
201 BIO_set_retry_write(b);
202 break;
203 case SSL_ERROR_WANT_READ:
204 BIO_set_retry_read(b);
205 break;
206 case SSL_ERROR_WANT_X509_LOOKUP:
207 BIO_set_retry_special(b);
208 retry_reason = BIO_RR_SSL_X509_LOOKUP;
209 break;
210 case SSL_ERROR_WANT_CONNECT:
211 BIO_set_retry_special(b);
212 retry_reason = BIO_RR_CONNECT;
213 case SSL_ERROR_SYSCALL:
214 case SSL_ERROR_SSL:
215 default:
216 break;
217 }
218
a146ae55 219 BIO_set_retry_reason(b, retry_reason);
3befffa3 220
a146ae55 221 return ret;
0f113f3e 222}
d02b48c6 223
0e1c0612 224static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr)
0f113f3e
MC
225{
226 SSL **sslp, *ssl;
a146ae55 227 BIO_SSL *bs, *dbs;
0f113f3e
MC
228 BIO *dbio, *bio;
229 long ret = 1;
a146ae55 230 BIO *next;
0f113f3e 231
a146ae55
MC
232 bs = BIO_get_data(b);
233 next = BIO_next(b);
0f113f3e
MC
234 ssl = bs->ssl;
235 if ((ssl == NULL) && (cmd != BIO_C_SET_SSL))
26a7d938 236 return 0;
0f113f3e
MC
237 switch (cmd) {
238 case BIO_CTRL_RESET:
239 SSL_shutdown(ssl);
240
241 if (ssl->handshake_func == ssl->method->ssl_connect)
242 SSL_set_connect_state(ssl);
243 else if (ssl->handshake_func == ssl->method->ssl_accept)
244 SSL_set_accept_state(ssl);
245
61986d32 246 if (!SSL_clear(ssl)) {
69f68237
MC
247 ret = 0;
248 break;
249 }
0f113f3e 250
a146ae55
MC
251 if (next != NULL)
252 ret = BIO_ctrl(next, cmd, num, ptr);
0f113f3e
MC
253 else if (ssl->rbio != NULL)
254 ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);
255 else
256 ret = 1;
257 break;
258 case BIO_CTRL_INFO:
259 ret = 0;
260 break;
261 case BIO_C_SSL_MODE:
262 if (num) /* client mode */
263 SSL_set_connect_state(ssl);
264 else
265 SSL_set_accept_state(ssl);
266 break;
267 case BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT:
268 ret = bs->renegotiate_timeout;
269 if (num < 60)
270 num = 5;
271 bs->renegotiate_timeout = (unsigned long)num;
272 bs->last_time = (unsigned long)time(NULL);
273 break;
274 case BIO_C_SET_SSL_RENEGOTIATE_BYTES:
275 ret = bs->renegotiate_count;
276 if ((long)num >= 512)
277 bs->renegotiate_count = (unsigned long)num;
278 break;
279 case BIO_C_GET_SSL_NUM_RENEGOTIATES:
280 ret = bs->num_renegotiates;
281 break;
282 case BIO_C_SET_SSL:
283 if (ssl != NULL) {
284 ssl_free(b);
285 if (!ssl_new(b))
286 return 0;
73d6b4ef 287 bs = BIO_get_data(b);
0f113f3e 288 }
a146ae55 289 BIO_set_shutdown(b, num);
0f113f3e 290 ssl = (SSL *)ptr;
a146ae55 291 bs->ssl = ssl;
0f113f3e
MC
292 bio = SSL_get_rbio(ssl);
293 if (bio != NULL) {
a146ae55
MC
294 if (next != NULL)
295 BIO_push(bio, next);
296 BIO_set_next(b, bio);
fb46be03 297 BIO_up_ref(bio);
0f113f3e 298 }
a146ae55 299 BIO_set_init(b, 1);
0f113f3e
MC
300 break;
301 case BIO_C_GET_SSL:
302 if (ptr != NULL) {
303 sslp = (SSL **)ptr;
304 *sslp = ssl;
305 } else
306 ret = 0;
307 break;
308 case BIO_CTRL_GET_CLOSE:
a146ae55 309 ret = BIO_get_shutdown(b);
0f113f3e
MC
310 break;
311 case BIO_CTRL_SET_CLOSE:
a146ae55 312 BIO_set_shutdown(b, (int)num);
0f113f3e
MC
313 break;
314 case BIO_CTRL_WPENDING:
315 ret = BIO_ctrl(ssl->wbio, cmd, num, ptr);
316 break;
317 case BIO_CTRL_PENDING:
318 ret = SSL_pending(ssl);
319 if (ret == 0)
320 ret = BIO_pending(ssl->rbio);
321 break;
322 case BIO_CTRL_FLUSH:
323 BIO_clear_retry_flags(b);
324 ret = BIO_ctrl(ssl->wbio, cmd, num, ptr);
325 BIO_copy_next_retry(b);
326 break;
327 case BIO_CTRL_PUSH:
a146ae55 328 if ((next != NULL) && (next != ssl->rbio)) {
eddef305
MC
329 /*
330 * We are going to pass ownership of next to the SSL object...but
331 * we don't own a reference to pass yet - so up ref
332 */
333 BIO_up_ref(next);
a146ae55 334 SSL_set_bio(ssl, next, next);
0f113f3e
MC
335 }
336 break;
337 case BIO_CTRL_POP:
338 /* Only detach if we are the BIO explicitly being popped */
339 if (b == ptr) {
b46fe860
MC
340 /* This will clear the reference we obtained during push */
341 SSL_set_bio(ssl, NULL, NULL);
0f113f3e
MC
342 }
343 break;
344 case BIO_C_DO_STATE_MACHINE:
345 BIO_clear_retry_flags(b);
346
a146ae55 347 BIO_set_retry_reason(b, 0);
0f113f3e
MC
348 ret = (int)SSL_do_handshake(ssl);
349
350 switch (SSL_get_error(ssl, (int)ret)) {
351 case SSL_ERROR_WANT_READ:
352 BIO_set_flags(b, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY);
353 break;
354 case SSL_ERROR_WANT_WRITE:
355 BIO_set_flags(b, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY);
356 break;
357 case SSL_ERROR_WANT_CONNECT:
358 BIO_set_flags(b, BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY);
a146ae55 359 BIO_set_retry_reason(b, BIO_get_retry_reason(next));
0f113f3e 360 break;
f1c412c9
DSH
361 case SSL_ERROR_WANT_X509_LOOKUP:
362 BIO_set_retry_special(b);
a146ae55 363 BIO_set_retry_reason(b, BIO_RR_SSL_X509_LOOKUP);
f1c412c9 364 break;
0f113f3e
MC
365 default:
366 break;
367 }
368 break;
369 case BIO_CTRL_DUP:
370 dbio = (BIO *)ptr;
a146ae55
MC
371 dbs = BIO_get_data(dbio);
372 SSL_free(dbs->ssl);
373 dbs->ssl = SSL_dup(ssl);
dbd5c34f
MC
374 dbs->num_renegotiates = bs->num_renegotiates;
375 dbs->renegotiate_count = bs->renegotiate_count;
376 dbs->byte_count = bs->byte_count;
377 dbs->renegotiate_timeout = bs->renegotiate_timeout;
378 dbs->last_time = bs->last_time;
a146ae55 379 ret = (dbs->ssl != NULL);
0f113f3e
MC
380 break;
381 case BIO_C_GET_FD:
382 ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);
383 break;
384 case BIO_CTRL_SET_CALLBACK:
2722ff50 385 ret = 0; /* use callback ctrl */
0f113f3e 386 break;
0f113f3e
MC
387 default:
388 ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);
389 break;
390 }
26a7d938 391 return ret;
0f113f3e 392}
d02b48c6 393
fce78bd4 394static long ssl_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
0f113f3e
MC
395{
396 SSL *ssl;
397 BIO_SSL *bs;
398 long ret = 1;
399
a146ae55 400 bs = BIO_get_data(b);
0f113f3e
MC
401 ssl = bs->ssl;
402 switch (cmd) {
403 case BIO_CTRL_SET_CALLBACK:
fce78bd4 404 ret = BIO_callback_ctrl(ssl->rbio, cmd, fp);
0f113f3e
MC
405 break;
406 default:
fce78bd4 407 ret = 0;
0f113f3e
MC
408 break;
409 }
26a7d938 410 return ret;
0f113f3e 411}
d3442bc7 412
0e1c0612 413static int ssl_puts(BIO *bp, const char *str)
0f113f3e
MC
414{
415 int n, ret;
d02b48c6 416
0f113f3e
MC
417 n = strlen(str);
418 ret = BIO_write(bp, str, n);
26a7d938 419 return ret;
0f113f3e 420}
d02b48c6 421
6b691a5c 422BIO *BIO_new_buffer_ssl_connect(SSL_CTX *ctx)
0f113f3e 423{
85d686e7 424#ifndef OPENSSL_NO_SOCK
0f113f3e
MC
425 BIO *ret = NULL, *buf = NULL, *ssl = NULL;
426
427 if ((buf = BIO_new(BIO_f_buffer())) == NULL)
26a7d938 428 return NULL;
0f113f3e
MC
429 if ((ssl = BIO_new_ssl_connect(ctx)) == NULL)
430 goto err;
431 if ((ret = BIO_push(buf, ssl)) == NULL)
432 goto err;
26a7d938 433 return ret;
0f113f3e 434 err:
ca3a82c3
RS
435 BIO_free(buf);
436 BIO_free(ssl);
85d686e7 437#endif
26a7d938 438 return NULL;
0f113f3e 439}
58964a49 440
6b691a5c 441BIO *BIO_new_ssl_connect(SSL_CTX *ctx)
0f113f3e 442{
4a1fbd13 443#ifndef OPENSSL_NO_SOCK
0f113f3e
MC
444 BIO *ret = NULL, *con = NULL, *ssl = NULL;
445
446 if ((con = BIO_new(BIO_s_connect())) == NULL)
26a7d938 447 return NULL;
0f113f3e
MC
448 if ((ssl = BIO_new_ssl(ctx, 1)) == NULL)
449 goto err;
450 if ((ret = BIO_push(ssl, con)) == NULL)
451 goto err;
26a7d938 452 return ret;
0f113f3e 453 err:
7947a1eb 454 BIO_free(ssl);
ca3a82c3 455 BIO_free(con);
4a1fbd13 456#endif
26a7d938 457 return NULL;
0f113f3e 458}
58964a49 459
6b691a5c 460BIO *BIO_new_ssl(SSL_CTX *ctx, int client)
0f113f3e
MC
461{
462 BIO *ret;
463 SSL *ssl;
464
465 if ((ret = BIO_new(BIO_f_ssl())) == NULL)
26a7d938 466 return NULL;
0f113f3e
MC
467 if ((ssl = SSL_new(ctx)) == NULL) {
468 BIO_free(ret);
26a7d938 469 return NULL;
0f113f3e
MC
470 }
471 if (client)
472 SSL_set_connect_state(ssl);
473 else
474 SSL_set_accept_state(ssl);
475
476 BIO_set_ssl(ret, ssl, BIO_CLOSE);
26a7d938 477 return ret;
0f113f3e 478}
d02b48c6 479
6b691a5c 480int BIO_ssl_copy_session_id(BIO *t, BIO *f)
0f113f3e 481{
a146ae55 482 BIO_SSL *tdata, *fdata;
0f113f3e
MC
483 t = BIO_find_type(t, BIO_TYPE_SSL);
484 f = BIO_find_type(f, BIO_TYPE_SSL);
485 if ((t == NULL) || (f == NULL))
a146ae55
MC
486 return 0;
487 tdata = BIO_get_data(t);
488 fdata = BIO_get_data(f);
489 if ((tdata->ssl == NULL) || (fdata->ssl == NULL))
26a7d938 490 return 0;
a146ae55 491 if (!SSL_copy_session_id(tdata->ssl, (fdata->ssl)))
17dd65e6 492 return 0;
208fb891 493 return 1;
0f113f3e 494}
d02b48c6 495
6b691a5c 496void BIO_ssl_shutdown(BIO *b)
0f113f3e 497{
9015d34e
RS
498 BIO_SSL *bdata;
499
500 for (; b != NULL; b = BIO_next(b)) {
501 if (BIO_method_type(b) != BIO_TYPE_SSL)
502 continue;
503 bdata = BIO_get_data(b);
504 if (bdata != NULL && bdata->ssl != NULL)
505 SSL_shutdown(bdata->ssl);
506 }
0f113f3e 507}