]> git.ipfire.org Git - thirdparty/openssl.git/blob - ssl/record/rec_layer_s3.c
Replaced '{ 0, NULL }' with OSSL_DISPATCH_END in OSSL_DISPATCH arrays
[thirdparty/openssl.git] / ssl / record / rec_layer_s3.c
1 /*
2 * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (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
8 */
9
10 #include <stdio.h>
11 #include <limits.h>
12 #include <errno.h>
13 #include <assert.h>
14 #include "../ssl_local.h"
15 #include <openssl/evp.h>
16 #include <openssl/buffer.h>
17 #include <openssl/rand.h>
18 #include <openssl/core_names.h>
19 #include "record_local.h"
20 #include "internal/packet.h"
21
22 void RECORD_LAYER_init(RECORD_LAYER *rl, SSL_CONNECTION *s)
23 {
24 rl->s = s;
25 }
26
27 void RECORD_LAYER_clear(RECORD_LAYER *rl)
28 {
29 rl->wnum = 0;
30 memset(rl->handshake_fragment, 0, sizeof(rl->handshake_fragment));
31 rl->handshake_fragment_len = 0;
32 rl->wpend_tot = 0;
33 rl->wpend_type = 0;
34 rl->wpend_ret = 0;
35 rl->wpend_buf = NULL;
36
37 if (rl->rrlmethod != NULL)
38 rl->rrlmethod->free(rl->rrl); /* Ignore return value */
39 if (rl->wrlmethod != NULL)
40 rl->wrlmethod->free(rl->wrl); /* Ignore return value */
41 BIO_free(rl->rrlnext);
42 rl->rrlmethod = NULL;
43 rl->wrlmethod = NULL;
44 rl->rrlnext = NULL;
45 rl->rrl = NULL;
46 rl->wrl = NULL;
47
48 if (rl->d)
49 DTLS_RECORD_LAYER_clear(rl);
50 }
51
52 /* Checks if we have unprocessed read ahead data pending */
53 int RECORD_LAYER_read_pending(const RECORD_LAYER *rl)
54 {
55 return rl->rrlmethod->unprocessed_read_pending(rl->rrl);
56 }
57
58 /* Checks if we have decrypted unread record data pending */
59 int RECORD_LAYER_processed_read_pending(const RECORD_LAYER *rl)
60 {
61 return (rl->curr_rec < rl->num_recs)
62 || rl->rrlmethod->processed_read_pending(rl->rrl);
63 }
64
65 int RECORD_LAYER_write_pending(const RECORD_LAYER *rl)
66 {
67 return rl->wpend_tot > 0;
68 }
69
70 static uint32_t ossl_get_max_early_data(SSL_CONNECTION *s)
71 {
72 uint32_t max_early_data;
73 SSL_SESSION *sess = s->session;
74
75 /*
76 * If we are a client then we always use the max_early_data from the
77 * session/psksession. Otherwise we go with the lowest out of the max early
78 * data set in the session and the configured max_early_data.
79 */
80 if (!s->server && sess->ext.max_early_data == 0) {
81 if (!ossl_assert(s->psksession != NULL
82 && s->psksession->ext.max_early_data > 0)) {
83 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
84 return 0;
85 }
86 sess = s->psksession;
87 }
88
89 if (!s->server)
90 max_early_data = sess->ext.max_early_data;
91 else if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED)
92 max_early_data = s->recv_max_early_data;
93 else
94 max_early_data = s->recv_max_early_data < sess->ext.max_early_data
95 ? s->recv_max_early_data : sess->ext.max_early_data;
96
97 return max_early_data;
98 }
99
100 static int ossl_early_data_count_ok(SSL_CONNECTION *s, size_t length,
101 size_t overhead, int send)
102 {
103 uint32_t max_early_data;
104
105 max_early_data = ossl_get_max_early_data(s);
106
107 if (max_early_data == 0) {
108 SSLfatal(s, send ? SSL_AD_INTERNAL_ERROR : SSL_AD_UNEXPECTED_MESSAGE,
109 SSL_R_TOO_MUCH_EARLY_DATA);
110 return 0;
111 }
112
113 /* If we are dealing with ciphertext we need to allow for the overhead */
114 max_early_data += overhead;
115
116 if (s->early_data_count + length > max_early_data) {
117 SSLfatal(s, send ? SSL_AD_INTERNAL_ERROR : SSL_AD_UNEXPECTED_MESSAGE,
118 SSL_R_TOO_MUCH_EARLY_DATA);
119 return 0;
120 }
121 s->early_data_count += length;
122
123 return 1;
124 }
125
126 size_t ssl3_pending(const SSL *s)
127 {
128 size_t i, num = 0;
129 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
130
131 if (sc == NULL)
132 return 0;
133
134 if (SSL_CONNECTION_IS_DTLS(sc)) {
135 TLS_RECORD *rdata;
136 pitem *item, *iter;
137
138 iter = pqueue_iterator(sc->rlayer.d->buffered_app_data.q);
139 while ((item = pqueue_next(&iter)) != NULL) {
140 rdata = item->data;
141 num += rdata->length;
142 }
143 }
144
145 for (i = 0; i < sc->rlayer.num_recs; i++) {
146 if (sc->rlayer.tlsrecs[i].type != SSL3_RT_APPLICATION_DATA)
147 return num;
148 num += sc->rlayer.tlsrecs[i].length;
149 }
150
151 num += sc->rlayer.rrlmethod->app_data_pending(sc->rlayer.rrl);
152
153 return num;
154 }
155
156 void SSL_CTX_set_default_read_buffer_len(SSL_CTX *ctx, size_t len)
157 {
158 ctx->default_read_buf_len = len;
159 }
160
161 void SSL_set_default_read_buffer_len(SSL *s, size_t len)
162 {
163 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
164
165 if (sc == NULL)
166 return;
167 sc->rlayer.default_read_buf_len = len;
168 }
169
170 const char *SSL_rstate_string_long(const SSL *s)
171 {
172 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
173 const char *lng;
174
175 if (sc == NULL)
176 return NULL;
177
178 if (sc->rlayer.rrlmethod == NULL || sc->rlayer.rrl == NULL)
179 return "unknown";
180
181 sc->rlayer.rrlmethod->get_state(sc->rlayer.rrl, NULL, &lng);
182
183 return lng;
184 }
185
186 const char *SSL_rstate_string(const SSL *s)
187 {
188 const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
189 const char *shrt;
190
191 if (sc == NULL)
192 return NULL;
193
194 if (sc->rlayer.rrlmethod == NULL || sc->rlayer.rrl == NULL)
195 return "unknown";
196
197 sc->rlayer.rrlmethod->get_state(sc->rlayer.rrl, &shrt, NULL);
198
199 return shrt;
200 }
201
202 static int tls_write_check_pending(SSL_CONNECTION *s, int type,
203 const unsigned char *buf, size_t len)
204 {
205 if (s->rlayer.wpend_tot == 0)
206 return 0;
207
208 /* We have pending data, so do some sanity checks */
209 if ((s->rlayer.wpend_tot > len)
210 || (!(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)
211 && (s->rlayer.wpend_buf != buf))
212 || (s->rlayer.wpend_type != type)) {
213 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_WRITE_RETRY);
214 return -1;
215 }
216 return 1;
217 }
218
219 /*
220 * Call this to write data in records of type 'type' It will return <= 0 if
221 * not all data has been sent or non-blocking IO.
222 */
223 int ssl3_write_bytes(SSL *ssl, int type, const void *buf_, size_t len,
224 size_t *written)
225 {
226 const unsigned char *buf = buf_;
227 size_t tot;
228 size_t n, max_send_fragment, split_send_fragment, maxpipes;
229 int i;
230 SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
231 OSSL_RECORD_TEMPLATE tmpls[SSL_MAX_PIPELINES];
232 unsigned int recversion;
233
234 if (s == NULL)
235 return -1;
236
237 s->rwstate = SSL_NOTHING;
238 tot = s->rlayer.wnum;
239 /*
240 * ensure that if we end up with a smaller value of data to write out
241 * than the original len from a write which didn't complete for
242 * non-blocking I/O and also somehow ended up avoiding the check for
243 * this in tls_write_check_pending/SSL_R_BAD_WRITE_RETRY as it must never be
244 * possible to end up with (len-tot) as a large number that will then
245 * promptly send beyond the end of the users buffer ... so we trap and
246 * report the error in a way the user will notice
247 */
248 if ((len < s->rlayer.wnum)
249 || ((s->rlayer.wpend_tot != 0)
250 && (len < (s->rlayer.wnum + s->rlayer.wpend_tot)))) {
251 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_LENGTH);
252 return -1;
253 }
254
255 if (s->early_data_state == SSL_EARLY_DATA_WRITING
256 && !ossl_early_data_count_ok(s, len, 0, 1)) {
257 /* SSLfatal() already called */
258 return -1;
259 }
260
261 s->rlayer.wnum = 0;
262
263 /*
264 * If we are supposed to be sending a KeyUpdate or NewSessionTicket then go
265 * into init unless we have writes pending - in which case we should finish
266 * doing that first.
267 */
268 if (s->rlayer.wpend_tot == 0 && (s->key_update != SSL_KEY_UPDATE_NONE
269 || s->ext.extra_tickets_expected > 0))
270 ossl_statem_set_in_init(s, 1);
271
272 /*
273 * When writing early data on the server side we could be "in_init" in
274 * between receiving the EoED and the CF - but we don't want to handle those
275 * messages yet.
276 */
277 if (SSL_in_init(ssl) && !ossl_statem_get_in_handshake(s)
278 && s->early_data_state != SSL_EARLY_DATA_UNAUTH_WRITING) {
279 i = s->handshake_func(ssl);
280 /* SSLfatal() already called */
281 if (i < 0)
282 return i;
283 if (i == 0) {
284 return -1;
285 }
286 }
287
288 i = tls_write_check_pending(s, type, buf, len);
289 if (i < 0) {
290 /* SSLfatal() already called */
291 return i;
292 } else if (i > 0) {
293 /* Retry needed */
294 i = HANDLE_RLAYER_WRITE_RETURN(s,
295 s->rlayer.wrlmethod->retry_write_records(s->rlayer.wrl));
296 if (i <= 0)
297 return i;
298 tot += s->rlayer.wpend_tot;
299 s->rlayer.wpend_tot = 0;
300 } /* else no retry required */
301
302 if (tot == 0) {
303 /*
304 * We've not previously sent any data for this write so memorize
305 * arguments so that we can detect bad write retries later
306 */
307 s->rlayer.wpend_tot = 0;
308 s->rlayer.wpend_type = type;
309 s->rlayer.wpend_buf = buf;
310 s->rlayer.wpend_ret = len;
311 }
312
313 if (tot == len) { /* done? */
314 *written = tot;
315 return 1;
316 }
317
318 /* If we have an alert to send, lets send it */
319 if (s->s3.alert_dispatch > 0) {
320 i = ssl->method->ssl_dispatch_alert(ssl);
321 if (i <= 0) {
322 /* SSLfatal() already called if appropriate */
323 return i;
324 }
325 /* if it went, fall through and send more stuff */
326 }
327
328 n = (len - tot);
329
330 max_send_fragment = ssl_get_max_send_fragment(s);
331 split_send_fragment = ssl_get_split_send_fragment(s);
332
333 if (max_send_fragment == 0
334 || split_send_fragment == 0
335 || split_send_fragment > max_send_fragment) {
336 /*
337 * We should have prevented this when we set/get the split and max send
338 * fragments so we shouldn't get here
339 */
340 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
341 return -1;
342 }
343
344 /*
345 * Some servers hang if initial client hello is larger than 256 bytes
346 * and record version number > TLS 1.0
347 */
348 recversion = (s->version == TLS1_3_VERSION) ? TLS1_2_VERSION : s->version;
349 if (SSL_get_state(ssl) == TLS_ST_CW_CLNT_HELLO
350 && !s->renegotiate
351 && TLS1_get_version(ssl) > TLS1_VERSION
352 && s->hello_retry_request == SSL_HRR_NONE)
353 recversion = TLS1_VERSION;
354
355 for (;;) {
356 size_t tmppipelen, remain;
357 size_t j, lensofar = 0;
358
359 /*
360 * Ask the record layer how it would like to split the amount of data
361 * that we have, and how many of those records it would like in one go.
362 */
363 maxpipes = s->rlayer.wrlmethod->get_max_records(s->rlayer.wrl, type, n,
364 max_send_fragment,
365 &split_send_fragment);
366 /*
367 * If max_pipelines is 0 then this means "undefined" and we default to
368 * whatever the record layer wants to do. Otherwise we use the smallest
369 * value from the number requested by the record layer, and max number
370 * configured by the user.
371 */
372 if (s->max_pipelines > 0 && maxpipes > s->max_pipelines)
373 maxpipes = s->max_pipelines;
374
375 if (maxpipes > SSL_MAX_PIPELINES)
376 maxpipes = SSL_MAX_PIPELINES;
377
378 if (split_send_fragment > max_send_fragment) {
379 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
380 return -1;
381 }
382
383 if (n / maxpipes >= split_send_fragment) {
384 /*
385 * We have enough data to completely fill all available
386 * pipelines
387 */
388 for (j = 0; j < maxpipes; j++) {
389 tmpls[j].type = type;
390 tmpls[j].version = recversion;
391 tmpls[j].buf = &(buf[tot]) + (j * split_send_fragment);
392 tmpls[j].buflen = split_send_fragment;
393 }
394 /* Remember how much data we are going to be sending */
395 s->rlayer.wpend_tot = maxpipes * split_send_fragment;
396 } else {
397 /* We can partially fill all available pipelines */
398 tmppipelen = n / maxpipes;
399 remain = n % maxpipes;
400 /*
401 * If there is a remainder we add an extra byte to the first few
402 * pipelines
403 */
404 if (remain > 0)
405 tmppipelen++;
406 for (j = 0; j < maxpipes; j++) {
407 tmpls[j].type = type;
408 tmpls[j].version = recversion;
409 tmpls[j].buf = &(buf[tot]) + lensofar;
410 tmpls[j].buflen = tmppipelen;
411 lensofar += tmppipelen;
412 if (j + 1 == remain)
413 tmppipelen--;
414 }
415 /* Remember how much data we are going to be sending */
416 s->rlayer.wpend_tot = n;
417 }
418
419 i = HANDLE_RLAYER_WRITE_RETURN(s,
420 s->rlayer.wrlmethod->write_records(s->rlayer.wrl, tmpls, maxpipes));
421 if (i <= 0) {
422 /* SSLfatal() already called if appropriate */
423 s->rlayer.wnum = tot;
424 return i;
425 }
426
427 if (s->rlayer.wpend_tot == n
428 || (type == SSL3_RT_APPLICATION_DATA
429 && (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE) != 0)) {
430 *written = tot + s->rlayer.wpend_tot;
431 s->rlayer.wpend_tot = 0;
432 return 1;
433 }
434
435 n -= s->rlayer.wpend_tot;
436 tot += s->rlayer.wpend_tot;
437 }
438 }
439
440 int ossl_tls_handle_rlayer_return(SSL_CONNECTION *s, int writing, int ret,
441 char *file, int line)
442 {
443 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
444
445 if (ret == OSSL_RECORD_RETURN_RETRY) {
446 s->rwstate = writing ? SSL_WRITING : SSL_READING;
447 ret = -1;
448 } else {
449 s->rwstate = SSL_NOTHING;
450 if (ret == OSSL_RECORD_RETURN_EOF) {
451 if (writing) {
452 /*
453 * This shouldn't happen with a writing operation. We treat it
454 * as fatal.
455 */
456 ERR_new();
457 ERR_set_debug(file, line, 0);
458 ossl_statem_fatal(s, SSL_AD_INTERNAL_ERROR,
459 ERR_R_INTERNAL_ERROR, NULL);
460 ret = OSSL_RECORD_RETURN_FATAL;
461 } else if ((s->options & SSL_OP_IGNORE_UNEXPECTED_EOF) != 0) {
462 SSL_set_shutdown(ssl, SSL_RECEIVED_SHUTDOWN);
463 s->s3.warn_alert = SSL_AD_CLOSE_NOTIFY;
464 } else {
465 ERR_new();
466 ERR_set_debug(file, line, 0);
467 ossl_statem_fatal(s, SSL_AD_DECODE_ERROR,
468 SSL_R_UNEXPECTED_EOF_WHILE_READING, NULL);
469 }
470 } else if (ret == OSSL_RECORD_RETURN_FATAL) {
471 int al = s->rlayer.rrlmethod->get_alert_code(s->rlayer.rrl);
472
473 if (al != SSL_AD_NO_ALERT) {
474 ERR_new();
475 ERR_set_debug(file, line, 0);
476 ossl_statem_fatal(s, al, SSL_R_RECORD_LAYER_FAILURE, NULL);
477 }
478 /*
479 * else some failure but there is no alert code. We don't log an
480 * error for this. The record layer should have logged an error
481 * already or, if not, its due to some sys call error which will be
482 * reported via SSL_ERROR_SYSCALL and errno.
483 */
484 }
485 /*
486 * The record layer distinguishes the cases of EOF, non-fatal
487 * err and retry. Upper layers do not.
488 * If we got a retry or success then *ret is already correct,
489 * otherwise we need to convert the return value.
490 */
491 if (ret == OSSL_RECORD_RETURN_NON_FATAL_ERR || ret == OSSL_RECORD_RETURN_EOF)
492 ret = 0;
493 else if (ret < OSSL_RECORD_RETURN_NON_FATAL_ERR)
494 ret = -1;
495 }
496
497 return ret;
498 }
499
500 int ssl_release_record(SSL_CONNECTION *s, TLS_RECORD *rr, size_t length)
501 {
502 assert(rr->length >= length);
503 if (rr->rechandle != NULL) {
504 if (length == 0)
505 length = rr->length;
506 /* The record layer allocated the buffers for this record */
507 if (HANDLE_RLAYER_READ_RETURN(s,
508 s->rlayer.rrlmethod->release_record(s->rlayer.rrl,
509 rr->rechandle,
510 length)) <= 0) {
511 /* RLAYER_fatal already called */
512 return 0;
513 }
514
515 if (length == rr->length)
516 s->rlayer.curr_rec++;
517 } else if (length == 0 || length == rr->length) {
518 /* We allocated the buffers for this record (only happens with DTLS) */
519 OPENSSL_free(rr->allocdata);
520 rr->allocdata = NULL;
521 }
522 rr->length -= length;
523 if (rr->length > 0)
524 rr->off += length;
525 else
526 rr->off = 0;
527
528 return 1;
529 }
530
531 /*-
532 * Return up to 'len' payload bytes received in 'type' records.
533 * 'type' is one of the following:
534 *
535 * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
536 * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
537 * - 0 (during a shutdown, no data has to be returned)
538 *
539 * If we don't have stored data to work from, read a SSL/TLS record first
540 * (possibly multiple records if we still don't have anything to return).
541 *
542 * This function must handle any surprises the peer may have for us, such as
543 * Alert records (e.g. close_notify) or renegotiation requests. ChangeCipherSpec
544 * messages are treated as if they were handshake messages *if* the |recvd_type|
545 * argument is non NULL.
546 * Also if record payloads contain fragments too small to process, we store
547 * them until there is enough for the respective protocol (the record protocol
548 * may use arbitrary fragmentation and even interleaving):
549 * Change cipher spec protocol
550 * just 1 byte needed, no need for keeping anything stored
551 * Alert protocol
552 * 2 bytes needed (AlertLevel, AlertDescription)
553 * Handshake protocol
554 * 4 bytes needed (HandshakeType, uint24 length) -- we just have
555 * to detect unexpected Client Hello and Hello Request messages
556 * here, anything else is handled by higher layers
557 * Application data protocol
558 * none of our business
559 */
560 int ssl3_read_bytes(SSL *ssl, int type, int *recvd_type, unsigned char *buf,
561 size_t len, int peek, size_t *readbytes)
562 {
563 int i, j, ret;
564 size_t n, curr_rec, totalbytes;
565 TLS_RECORD *rr;
566 void (*cb) (const SSL *ssl, int type2, int val) = NULL;
567 int is_tls13;
568 SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
569
570 is_tls13 = SSL_CONNECTION_IS_TLS13(s);
571
572 if ((type != 0
573 && (type != SSL3_RT_APPLICATION_DATA)
574 && (type != SSL3_RT_HANDSHAKE))
575 || (peek && (type != SSL3_RT_APPLICATION_DATA))) {
576 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
577 return -1;
578 }
579
580 if ((type == SSL3_RT_HANDSHAKE) && (s->rlayer.handshake_fragment_len > 0))
581 /* (partially) satisfy request from storage */
582 {
583 unsigned char *src = s->rlayer.handshake_fragment;
584 unsigned char *dst = buf;
585 unsigned int k;
586
587 /* peek == 0 */
588 n = 0;
589 while ((len > 0) && (s->rlayer.handshake_fragment_len > 0)) {
590 *dst++ = *src++;
591 len--;
592 s->rlayer.handshake_fragment_len--;
593 n++;
594 }
595 /* move any remaining fragment bytes: */
596 for (k = 0; k < s->rlayer.handshake_fragment_len; k++)
597 s->rlayer.handshake_fragment[k] = *src++;
598
599 if (recvd_type != NULL)
600 *recvd_type = SSL3_RT_HANDSHAKE;
601
602 *readbytes = n;
603 return 1;
604 }
605
606 /*
607 * Now s->rlayer.handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE.
608 */
609
610 if (!ossl_statem_get_in_handshake(s) && SSL_in_init(ssl)) {
611 /* type == SSL3_RT_APPLICATION_DATA */
612 i = s->handshake_func(ssl);
613 /* SSLfatal() already called */
614 if (i < 0)
615 return i;
616 if (i == 0)
617 return -1;
618 }
619 start:
620 s->rwstate = SSL_NOTHING;
621
622 /*-
623 * For each record 'i' up to |num_recs]
624 * rr[i].type - is the type of record
625 * rr[i].data, - data
626 * rr[i].off, - offset into 'data' for next read
627 * rr[i].length, - number of bytes.
628 */
629 /* get new records if necessary */
630 if (s->rlayer.curr_rec >= s->rlayer.num_recs) {
631 s->rlayer.curr_rec = s->rlayer.num_recs = 0;
632 do {
633 rr = &s->rlayer.tlsrecs[s->rlayer.num_recs];
634
635 ret = HANDLE_RLAYER_READ_RETURN(s,
636 s->rlayer.rrlmethod->read_record(s->rlayer.rrl,
637 &rr->rechandle,
638 &rr->version, &rr->type,
639 &rr->data, &rr->length,
640 NULL, NULL));
641 if (ret <= 0) {
642 /* SSLfatal() already called if appropriate */
643 return ret;
644 }
645 rr->off = 0;
646 s->rlayer.num_recs++;
647 } while (s->rlayer.rrlmethod->processed_read_pending(s->rlayer.rrl)
648 && s->rlayer.num_recs < SSL_MAX_PIPELINES);
649 }
650 rr = &s->rlayer.tlsrecs[s->rlayer.curr_rec];
651
652 if (s->rlayer.handshake_fragment_len > 0
653 && rr->type != SSL3_RT_HANDSHAKE
654 && SSL_CONNECTION_IS_TLS13(s)) {
655 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
656 SSL_R_MIXED_HANDSHAKE_AND_NON_HANDSHAKE_DATA);
657 return -1;
658 }
659
660 /*
661 * Reset the count of consecutive warning alerts if we've got a non-empty
662 * record that isn't an alert.
663 */
664 if (rr->type != SSL3_RT_ALERT && rr->length != 0)
665 s->rlayer.alert_count = 0;
666
667 /* we now have a packet which can be read and processed */
668
669 if (s->s3.change_cipher_spec /* set when we receive ChangeCipherSpec,
670 * reset by ssl3_get_finished */
671 && (rr->type != SSL3_RT_HANDSHAKE)) {
672 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
673 SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);
674 return -1;
675 }
676
677 /*
678 * If the other end has shut down, throw anything we read away (even in
679 * 'peek' mode)
680 */
681 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
682 s->rlayer.curr_rec++;
683 s->rwstate = SSL_NOTHING;
684 return 0;
685 }
686
687 if (type == rr->type
688 || (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC
689 && type == SSL3_RT_HANDSHAKE && recvd_type != NULL
690 && !is_tls13)) {
691 /*
692 * SSL3_RT_APPLICATION_DATA or
693 * SSL3_RT_HANDSHAKE or
694 * SSL3_RT_CHANGE_CIPHER_SPEC
695 */
696 /*
697 * make sure that we are not getting application data when we are
698 * doing a handshake for the first time
699 */
700 if (SSL_in_init(ssl) && type == SSL3_RT_APPLICATION_DATA
701 && SSL_IS_FIRST_HANDSHAKE(s)) {
702 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_APP_DATA_IN_HANDSHAKE);
703 return -1;
704 }
705
706 if (type == SSL3_RT_HANDSHAKE
707 && rr->type == SSL3_RT_CHANGE_CIPHER_SPEC
708 && s->rlayer.handshake_fragment_len > 0) {
709 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_CCS_RECEIVED_EARLY);
710 return -1;
711 }
712
713 if (recvd_type != NULL)
714 *recvd_type = rr->type;
715
716 if (len == 0) {
717 /*
718 * Skip a zero length record. This ensures multiple calls to
719 * SSL_read() with a zero length buffer will eventually cause
720 * SSL_pending() to report data as being available.
721 */
722 if (rr->length == 0 && !ssl_release_record(s, rr, 0))
723 return -1;
724
725 return 0;
726 }
727
728 totalbytes = 0;
729 curr_rec = s->rlayer.curr_rec;
730 do {
731 if (len - totalbytes > rr->length)
732 n = rr->length;
733 else
734 n = len - totalbytes;
735
736 memcpy(buf, &(rr->data[rr->off]), n);
737 buf += n;
738 if (peek) {
739 /* Mark any zero length record as consumed CVE-2016-6305 */
740 if (rr->length == 0 && !ssl_release_record(s, rr, 0))
741 return -1;
742 } else {
743 if (!ssl_release_record(s, rr, n))
744 return -1;
745 }
746 if (rr->length == 0
747 || (peek && n == rr->length)) {
748 rr++;
749 curr_rec++;
750 }
751 totalbytes += n;
752 } while (type == SSL3_RT_APPLICATION_DATA
753 && curr_rec < s->rlayer.num_recs
754 && totalbytes < len);
755 if (totalbytes == 0) {
756 /* We must have read empty records. Get more data */
757 goto start;
758 }
759 *readbytes = totalbytes;
760 return 1;
761 }
762
763 /*
764 * If we get here, then type != rr->type; if we have a handshake message,
765 * then it was unexpected (Hello Request or Client Hello) or invalid (we
766 * were actually expecting a CCS).
767 */
768
769 /*
770 * Lets just double check that we've not got an SSLv2 record
771 */
772 if (rr->version == SSL2_VERSION) {
773 /*
774 * Should never happen. ssl3_get_record() should only give us an SSLv2
775 * record back if this is the first packet and we are looking for an
776 * initial ClientHello. Therefore |type| should always be equal to
777 * |rr->type|. If not then something has gone horribly wrong
778 */
779 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
780 return -1;
781 }
782
783 if (ssl->method->version == TLS_ANY_VERSION
784 && (s->server || rr->type != SSL3_RT_ALERT)) {
785 /*
786 * If we've got this far and still haven't decided on what version
787 * we're using then this must be a client side alert we're dealing
788 * with. We shouldn't be receiving anything other than a ClientHello
789 * if we are a server.
790 */
791 s->version = rr->version;
792 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE);
793 return -1;
794 }
795
796 /*-
797 * s->rlayer.handshake_fragment_len == 4 iff rr->type == SSL3_RT_HANDSHAKE;
798 * (Possibly rr is 'empty' now, i.e. rr->length may be 0.)
799 */
800
801 if (rr->type == SSL3_RT_ALERT) {
802 unsigned int alert_level, alert_descr;
803 const unsigned char *alert_bytes = rr->data + rr->off;
804 PACKET alert;
805
806 if (!PACKET_buf_init(&alert, alert_bytes, rr->length)
807 || !PACKET_get_1(&alert, &alert_level)
808 || !PACKET_get_1(&alert, &alert_descr)
809 || PACKET_remaining(&alert) != 0) {
810 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_INVALID_ALERT);
811 return -1;
812 }
813
814 if (s->msg_callback)
815 s->msg_callback(0, s->version, SSL3_RT_ALERT, alert_bytes, 2, ssl,
816 s->msg_callback_arg);
817
818 if (s->info_callback != NULL)
819 cb = s->info_callback;
820 else if (ssl->ctx->info_callback != NULL)
821 cb = ssl->ctx->info_callback;
822
823 if (cb != NULL) {
824 j = (alert_level << 8) | alert_descr;
825 cb(ssl, SSL_CB_READ_ALERT, j);
826 }
827
828 if ((!is_tls13 && alert_level == SSL3_AL_WARNING)
829 || (is_tls13 && alert_descr == SSL_AD_USER_CANCELLED)) {
830 s->s3.warn_alert = alert_descr;
831 if (!ssl_release_record(s, rr, 0))
832 return -1;
833
834 s->rlayer.alert_count++;
835 if (s->rlayer.alert_count == MAX_WARN_ALERT_COUNT) {
836 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
837 SSL_R_TOO_MANY_WARN_ALERTS);
838 return -1;
839 }
840 }
841
842 /*
843 * Apart from close_notify the only other warning alert in TLSv1.3
844 * is user_cancelled - which we just ignore.
845 */
846 if (is_tls13 && alert_descr == SSL_AD_USER_CANCELLED) {
847 goto start;
848 } else if (alert_descr == SSL_AD_CLOSE_NOTIFY
849 && (is_tls13 || alert_level == SSL3_AL_WARNING)) {
850 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
851 return 0;
852 } else if (alert_level == SSL3_AL_FATAL || is_tls13) {
853 s->rwstate = SSL_NOTHING;
854 s->s3.fatal_alert = alert_descr;
855 SSLfatal_data(s, SSL_AD_NO_ALERT,
856 SSL_AD_REASON_OFFSET + alert_descr,
857 "SSL alert number %d", alert_descr);
858 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
859 if (!ssl_release_record(s, rr, 0))
860 return -1;
861 SSL_CTX_remove_session(s->session_ctx, s->session);
862 return 0;
863 } else if (alert_descr == SSL_AD_NO_RENEGOTIATION) {
864 /*
865 * This is a warning but we receive it if we requested
866 * renegotiation and the peer denied it. Terminate with a fatal
867 * alert because if application tried to renegotiate it
868 * presumably had a good reason and expects it to succeed. In
869 * future we might have a renegotiation where we don't care if
870 * the peer refused it where we carry on.
871 */
872 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_NO_RENEGOTIATION);
873 return -1;
874 } else if (alert_level == SSL3_AL_WARNING) {
875 /* We ignore any other warning alert in TLSv1.2 and below */
876 goto start;
877 }
878
879 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_ALERT_TYPE);
880 return -1;
881 }
882
883 if ((s->shutdown & SSL_SENT_SHUTDOWN) != 0) {
884 if (rr->type == SSL3_RT_HANDSHAKE) {
885 BIO *rbio;
886
887 /*
888 * We ignore any handshake messages sent to us unless they are
889 * TLSv1.3 in which case we want to process them. For all other
890 * handshake messages we can't do anything reasonable with them
891 * because we are unable to write any response due to having already
892 * sent close_notify.
893 */
894 if (!SSL_CONNECTION_IS_TLS13(s)) {
895 if (!ssl_release_record(s, rr, 0))
896 return -1;
897
898 if ((s->mode & SSL_MODE_AUTO_RETRY) != 0)
899 goto start;
900
901 s->rwstate = SSL_READING;
902 rbio = SSL_get_rbio(ssl);
903 BIO_clear_retry_flags(rbio);
904 BIO_set_retry_read(rbio);
905 return -1;
906 }
907 } else {
908 /*
909 * The peer is continuing to send application data, but we have
910 * already sent close_notify. If this was expected we should have
911 * been called via SSL_read() and this would have been handled
912 * above.
913 * No alert sent because we already sent close_notify
914 */
915 if (!ssl_release_record(s, rr, 0))
916 return -1;
917 SSLfatal(s, SSL_AD_NO_ALERT,
918 SSL_R_APPLICATION_DATA_AFTER_CLOSE_NOTIFY);
919 return -1;
920 }
921 }
922
923 /*
924 * For handshake data we have 'fragment' storage, so fill that so that we
925 * can process the header at a fixed place. This is done after the
926 * "SHUTDOWN" code above to avoid filling the fragment storage with data
927 * that we're just going to discard.
928 */
929 if (rr->type == SSL3_RT_HANDSHAKE) {
930 size_t dest_maxlen = sizeof(s->rlayer.handshake_fragment);
931 unsigned char *dest = s->rlayer.handshake_fragment;
932 size_t *dest_len = &s->rlayer.handshake_fragment_len;
933
934 n = dest_maxlen - *dest_len; /* available space in 'dest' */
935 if (rr->length < n)
936 n = rr->length; /* available bytes */
937
938 /* now move 'n' bytes: */
939 if (n > 0) {
940 memcpy(dest + *dest_len, rr->data + rr->off, n);
941 *dest_len += n;
942 if (!ssl_release_record(s, rr, n))
943 return -1;
944 }
945
946 if (*dest_len < dest_maxlen)
947 goto start; /* fragment was too small */
948 }
949
950 if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) {
951 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_CCS_RECEIVED_EARLY);
952 return -1;
953 }
954
955 /*
956 * Unexpected handshake message (ClientHello, NewSessionTicket (TLS1.3) or
957 * protocol violation)
958 */
959 if ((s->rlayer.handshake_fragment_len >= 4)
960 && !ossl_statem_get_in_handshake(s)) {
961 int ined = (s->early_data_state == SSL_EARLY_DATA_READING);
962
963 /* We found handshake data, so we're going back into init */
964 ossl_statem_set_in_init(s, 1);
965
966 i = s->handshake_func(ssl);
967 /* SSLfatal() already called if appropriate */
968 if (i < 0)
969 return i;
970 if (i == 0) {
971 return -1;
972 }
973
974 /*
975 * If we were actually trying to read early data and we found a
976 * handshake message, then we don't want to continue to try and read
977 * the application data any more. It won't be "early" now.
978 */
979 if (ined)
980 return -1;
981
982 if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
983 if (!RECORD_LAYER_read_pending(&s->rlayer)) {
984 BIO *bio;
985 /*
986 * In the case where we try to read application data, but we
987 * trigger an SSL handshake, we return -1 with the retry
988 * option set. Otherwise renegotiation may cause nasty
989 * problems in the blocking world
990 */
991 s->rwstate = SSL_READING;
992 bio = SSL_get_rbio(ssl);
993 BIO_clear_retry_flags(bio);
994 BIO_set_retry_read(bio);
995 return -1;
996 }
997 }
998 goto start;
999 }
1000
1001 switch (rr->type) {
1002 default:
1003 /*
1004 * TLS 1.0 and 1.1 say you SHOULD ignore unrecognised record types, but
1005 * TLS 1.2 says you MUST send an unexpected message alert. We use the
1006 * TLS 1.2 behaviour for all protocol versions to prevent issues where
1007 * no progress is being made and the peer continually sends unrecognised
1008 * record types, using up resources processing them.
1009 */
1010 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_RECORD);
1011 return -1;
1012 case SSL3_RT_CHANGE_CIPHER_SPEC:
1013 case SSL3_RT_ALERT:
1014 case SSL3_RT_HANDSHAKE:
1015 /*
1016 * we already handled all of these, with the possible exception of
1017 * SSL3_RT_HANDSHAKE when ossl_statem_get_in_handshake(s) is true, but
1018 * that should not happen when type != rr->type
1019 */
1020 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, ERR_R_INTERNAL_ERROR);
1021 return -1;
1022 case SSL3_RT_APPLICATION_DATA:
1023 /*
1024 * At this point, we were expecting handshake data, but have
1025 * application data. If the library was running inside ssl3_read()
1026 * (i.e. in_read_app_data is set) and it makes sense to read
1027 * application data at this point (session renegotiation not yet
1028 * started), we will indulge it.
1029 */
1030 if (ossl_statem_app_data_allowed(s)) {
1031 s->s3.in_read_app_data = 2;
1032 return -1;
1033 } else if (ossl_statem_skip_early_data(s)) {
1034 /*
1035 * This can happen after a client sends a CH followed by early_data,
1036 * but the server responds with a HelloRetryRequest. The server
1037 * reads the next record from the client expecting to find a
1038 * plaintext ClientHello but gets a record which appears to be
1039 * application data. The trial decrypt "works" because null
1040 * decryption was applied. We just skip it and move on to the next
1041 * record.
1042 */
1043 if (!ossl_early_data_count_ok(s, rr->length,
1044 EARLY_DATA_CIPHERTEXT_OVERHEAD, 0)) {
1045 /* SSLfatal() already called */
1046 return -1;
1047 }
1048 if (!ssl_release_record(s, rr, 0))
1049 return -1;
1050 goto start;
1051 } else {
1052 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_RECORD);
1053 return -1;
1054 }
1055 }
1056 }
1057
1058 /*
1059 * Returns true if the current rrec was sent in SSLv2 backwards compatible
1060 * format and false otherwise.
1061 */
1062 int RECORD_LAYER_is_sslv2_record(RECORD_LAYER *rl)
1063 {
1064 if (SSL_CONNECTION_IS_DTLS(rl->s))
1065 return 0;
1066 return rl->tlsrecs[0].version == SSL2_VERSION;
1067 }
1068
1069 static OSSL_FUNC_rlayer_msg_callback_fn rlayer_msg_callback_wrapper;
1070 static void rlayer_msg_callback_wrapper(int write_p, int version,
1071 int content_type, const void *buf,
1072 size_t len, void *cbarg)
1073 {
1074 SSL_CONNECTION *s = cbarg;
1075 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
1076
1077 if (s->msg_callback != NULL)
1078 s->msg_callback(write_p, version, content_type, buf, len, ssl,
1079 s->msg_callback_arg);
1080 }
1081
1082 static OSSL_FUNC_rlayer_security_fn rlayer_security_wrapper;
1083 static int rlayer_security_wrapper(void *cbarg, int op, int bits, int nid,
1084 void *other)
1085 {
1086 SSL_CONNECTION *s = cbarg;
1087
1088 return ssl_security(s, op, bits, nid, other);
1089 }
1090
1091 static OSSL_FUNC_rlayer_padding_fn rlayer_padding_wrapper;
1092 static size_t rlayer_padding_wrapper(void *cbarg, int type, size_t len)
1093 {
1094 SSL_CONNECTION *s = cbarg;
1095 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
1096
1097 return s->rlayer.record_padding_cb(ssl, type, len,
1098 s->rlayer.record_padding_arg);
1099 }
1100
1101 static const OSSL_DISPATCH rlayer_dispatch[] = {
1102 { OSSL_FUNC_RLAYER_SKIP_EARLY_DATA, (void (*)(void))ossl_statem_skip_early_data },
1103 { OSSL_FUNC_RLAYER_MSG_CALLBACK, (void (*)(void))rlayer_msg_callback_wrapper },
1104 { OSSL_FUNC_RLAYER_SECURITY, (void (*)(void))rlayer_security_wrapper },
1105 { OSSL_FUNC_RLAYER_PADDING, (void (*)(void))rlayer_padding_wrapper },
1106 OSSL_DISPATCH_END
1107 };
1108
1109 void ossl_ssl_set_custom_record_layer(SSL_CONNECTION *s,
1110 const OSSL_RECORD_METHOD *meth,
1111 void *rlarg)
1112 {
1113 s->rlayer.custom_rlmethod = meth;
1114 s->rlayer.rlarg = rlarg;
1115 }
1116
1117 static const OSSL_RECORD_METHOD *ssl_select_next_record_layer(SSL_CONNECTION *s,
1118 int direction,
1119 int level)
1120 {
1121 if (s->rlayer.custom_rlmethod != NULL)
1122 return s->rlayer.custom_rlmethod;
1123
1124 if (level == OSSL_RECORD_PROTECTION_LEVEL_NONE) {
1125 if (SSL_CONNECTION_IS_DTLS(s))
1126 return &ossl_dtls_record_method;
1127
1128 return &ossl_tls_record_method;
1129 }
1130
1131 #ifndef OPENSSL_NO_KTLS
1132 /* KTLS does not support renegotiation */
1133 if (level == OSSL_RECORD_PROTECTION_LEVEL_APPLICATION
1134 && (s->options & SSL_OP_ENABLE_KTLS) != 0
1135 && (SSL_CONNECTION_IS_TLS13(s) || SSL_IS_FIRST_HANDSHAKE(s)))
1136 return &ossl_ktls_record_method;
1137 #endif
1138
1139 /* Default to the current OSSL_RECORD_METHOD */
1140 return direction == OSSL_RECORD_DIRECTION_READ ? s->rlayer.rrlmethod
1141 : s->rlayer.wrlmethod;
1142 }
1143
1144 static int ssl_post_record_layer_select(SSL_CONNECTION *s, int direction)
1145 {
1146 const OSSL_RECORD_METHOD *thismethod;
1147 OSSL_RECORD_LAYER *thisrl;
1148
1149 if (direction == OSSL_RECORD_DIRECTION_READ) {
1150 thismethod = s->rlayer.rrlmethod;
1151 thisrl = s->rlayer.rrl;
1152 } else {
1153 thismethod = s->rlayer.wrlmethod;
1154 thisrl = s->rlayer.wrl;
1155 }
1156
1157 #ifndef OPENSSL_NO_KTLS
1158 {
1159 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
1160
1161 if (s->rlayer.rrlmethod == &ossl_ktls_record_method) {
1162 /* KTLS does not support renegotiation so disallow it */
1163 SSL_set_options(ssl, SSL_OP_NO_RENEGOTIATION);
1164 }
1165 }
1166 #endif
1167 if (SSL_IS_FIRST_HANDSHAKE(s) && thismethod->set_first_handshake != NULL)
1168 thismethod->set_first_handshake(thisrl, 1);
1169
1170 if (s->max_pipelines != 0 && thismethod->set_max_pipelines != NULL)
1171 thismethod->set_max_pipelines(thisrl, s->max_pipelines);
1172
1173 return 1;
1174 }
1175
1176 int ssl_set_new_record_layer(SSL_CONNECTION *s, int version,
1177 int direction, int level,
1178 unsigned char *secret, size_t secretlen,
1179 unsigned char *key, size_t keylen,
1180 unsigned char *iv, size_t ivlen,
1181 unsigned char *mackey, size_t mackeylen,
1182 const EVP_CIPHER *ciph, size_t taglen,
1183 int mactype, const EVP_MD *md,
1184 const SSL_COMP *comp, const EVP_MD *kdfdigest)
1185 {
1186 OSSL_PARAM options[5], *opts = options;
1187 OSSL_PARAM settings[6], *set = settings;
1188 const OSSL_RECORD_METHOD **thismethod;
1189 OSSL_RECORD_LAYER **thisrl, *newrl = NULL;
1190 BIO *thisbio;
1191 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
1192 const OSSL_RECORD_METHOD *meth;
1193 int use_etm, stream_mac = 0, tlstree = 0;
1194 unsigned int maxfrag = (direction == OSSL_RECORD_DIRECTION_WRITE)
1195 ? ssl_get_max_send_fragment(s)
1196 : SSL3_RT_MAX_PLAIN_LENGTH;
1197 int use_early_data = 0;
1198 uint32_t max_early_data;
1199 COMP_METHOD *compm = (comp == NULL) ? NULL : comp->method;
1200
1201 meth = ssl_select_next_record_layer(s, direction, level);
1202
1203 if (direction == OSSL_RECORD_DIRECTION_READ) {
1204 thismethod = &s->rlayer.rrlmethod;
1205 thisrl = &s->rlayer.rrl;
1206 thisbio = s->rbio;
1207 } else {
1208 thismethod = &s->rlayer.wrlmethod;
1209 thisrl = &s->rlayer.wrl;
1210 thisbio = s->wbio;
1211 }
1212
1213 if (meth == NULL)
1214 meth = *thismethod;
1215
1216 if (!ossl_assert(meth != NULL)) {
1217 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
1218 return 0;
1219 }
1220
1221 /* Parameters that *may* be supported by a record layer if passed */
1222 *opts++ = OSSL_PARAM_construct_uint64(OSSL_LIBSSL_RECORD_LAYER_PARAM_OPTIONS,
1223 &s->options);
1224 *opts++ = OSSL_PARAM_construct_uint32(OSSL_LIBSSL_RECORD_LAYER_PARAM_MODE,
1225 &s->mode);
1226 if (direction == OSSL_RECORD_DIRECTION_READ) {
1227 *opts++ = OSSL_PARAM_construct_size_t(OSSL_LIBSSL_RECORD_LAYER_READ_BUFFER_LEN,
1228 &s->rlayer.default_read_buf_len);
1229 *opts++ = OSSL_PARAM_construct_int(OSSL_LIBSSL_RECORD_LAYER_PARAM_READ_AHEAD,
1230 &s->rlayer.read_ahead);
1231 } else {
1232 *opts++ = OSSL_PARAM_construct_size_t(OSSL_LIBSSL_RECORD_LAYER_PARAM_BLOCK_PADDING,
1233 &s->rlayer.block_padding);
1234 }
1235 *opts = OSSL_PARAM_construct_end();
1236
1237 /* Parameters that *must* be supported by a record layer if passed */
1238 if (direction == OSSL_RECORD_DIRECTION_READ) {
1239 use_etm = SSL_READ_ETM(s) ? 1 : 0;
1240 if ((s->mac_flags & SSL_MAC_FLAG_READ_MAC_STREAM) != 0)
1241 stream_mac = 1;
1242
1243 if ((s->mac_flags & SSL_MAC_FLAG_READ_MAC_TLSTREE) != 0)
1244 tlstree = 1;
1245 } else {
1246 use_etm = SSL_WRITE_ETM(s) ? 1 : 0;
1247 if ((s->mac_flags & SSL_MAC_FLAG_WRITE_MAC_STREAM) != 0)
1248 stream_mac = 1;
1249
1250 if ((s->mac_flags & SSL_MAC_FLAG_WRITE_MAC_TLSTREE) != 0)
1251 tlstree = 1;
1252 }
1253
1254 if (use_etm)
1255 *set++ = OSSL_PARAM_construct_int(OSSL_LIBSSL_RECORD_LAYER_PARAM_USE_ETM,
1256 &use_etm);
1257
1258 if (stream_mac)
1259 *set++ = OSSL_PARAM_construct_int(OSSL_LIBSSL_RECORD_LAYER_PARAM_STREAM_MAC,
1260 &stream_mac);
1261
1262 if (tlstree)
1263 *set++ = OSSL_PARAM_construct_int(OSSL_LIBSSL_RECORD_LAYER_PARAM_TLSTREE,
1264 &tlstree);
1265
1266 /*
1267 * We only need to do this for the read side. The write side should already
1268 * have the correct value due to the ssl_get_max_send_fragment() call above
1269 */
1270 if (direction == OSSL_RECORD_DIRECTION_READ
1271 && s->session != NULL
1272 && USE_MAX_FRAGMENT_LENGTH_EXT(s->session))
1273 maxfrag = GET_MAX_FRAGMENT_LENGTH(s->session);
1274
1275
1276 if (maxfrag != SSL3_RT_MAX_PLAIN_LENGTH)
1277 *set++ = OSSL_PARAM_construct_uint(OSSL_LIBSSL_RECORD_LAYER_PARAM_MAX_FRAG_LEN,
1278 &maxfrag);
1279
1280 /*
1281 * The record layer must check the amount of early data sent or received
1282 * using the early keys. A server also needs to worry about rejected early
1283 * data that might arrive when the handshake keys are in force.
1284 */
1285 if (s->server && direction == OSSL_RECORD_DIRECTION_READ) {
1286 use_early_data = (level == OSSL_RECORD_PROTECTION_LEVEL_EARLY
1287 || level == OSSL_RECORD_PROTECTION_LEVEL_HANDSHAKE);
1288 } else if (!s->server && direction == OSSL_RECORD_DIRECTION_WRITE) {
1289 use_early_data = (level == OSSL_RECORD_PROTECTION_LEVEL_EARLY);
1290 }
1291 if (use_early_data) {
1292 max_early_data = ossl_get_max_early_data(s);
1293
1294 if (max_early_data != 0)
1295 *set++ = OSSL_PARAM_construct_uint32(OSSL_LIBSSL_RECORD_LAYER_PARAM_MAX_EARLY_DATA,
1296 &max_early_data);
1297 }
1298
1299 *set = OSSL_PARAM_construct_end();
1300
1301 for (;;) {
1302 int rlret;
1303 BIO *prev = NULL;
1304 BIO *next = NULL;
1305 unsigned int epoch = 0;
1306 OSSL_DISPATCH rlayer_dispatch_tmp[OSSL_NELEM(rlayer_dispatch)];
1307 size_t i, j;
1308
1309 if (direction == OSSL_RECORD_DIRECTION_READ) {
1310 prev = s->rlayer.rrlnext;
1311 if (SSL_CONNECTION_IS_DTLS(s)
1312 && level != OSSL_RECORD_PROTECTION_LEVEL_NONE)
1313 epoch = DTLS_RECORD_LAYER_get_r_epoch(&s->rlayer) + 1; /* new epoch */
1314
1315 #ifndef OPENSSL_NO_DGRAM
1316 if (SSL_CONNECTION_IS_DTLS(s))
1317 next = BIO_new(BIO_s_dgram_mem());
1318 else
1319 #endif
1320 next = BIO_new(BIO_s_mem());
1321
1322 if (next == NULL) {
1323 BIO_free(prev);
1324 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1325 return 0;
1326 }
1327 s->rlayer.rrlnext = next;
1328 } else {
1329 if (SSL_CONNECTION_IS_DTLS(s)
1330 && level != OSSL_RECORD_PROTECTION_LEVEL_NONE)
1331 epoch = DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer) + 1; /* new epoch */
1332 }
1333
1334 /*
1335 * Create a copy of the dispatch array, missing out wrappers for
1336 * callbacks that we don't need.
1337 */
1338 for (i = 0, j = 0; i < OSSL_NELEM(rlayer_dispatch); i++) {
1339 switch (rlayer_dispatch[i].function_id) {
1340 case OSSL_FUNC_RLAYER_MSG_CALLBACK:
1341 if (s->msg_callback == NULL)
1342 continue;
1343 break;
1344 case OSSL_FUNC_RLAYER_PADDING:
1345 if (s->rlayer.record_padding_cb == NULL)
1346 continue;
1347 break;
1348 default:
1349 break;
1350 }
1351 rlayer_dispatch_tmp[j++] = rlayer_dispatch[i];
1352 }
1353
1354 rlret = meth->new_record_layer(sctx->libctx, sctx->propq, version,
1355 s->server, direction, level, epoch,
1356 secret, secretlen, key, keylen, iv,
1357 ivlen, mackey, mackeylen, ciph, taglen,
1358 mactype, md, compm, kdfdigest, prev,
1359 thisbio, next, NULL, NULL, settings,
1360 options, rlayer_dispatch_tmp, s,
1361 s->rlayer.rlarg, &newrl);
1362 BIO_free(prev);
1363 switch (rlret) {
1364 case OSSL_RECORD_RETURN_FATAL:
1365 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_RECORD_LAYER_FAILURE);
1366 return 0;
1367
1368 case OSSL_RECORD_RETURN_NON_FATAL_ERR:
1369 if (*thismethod != meth && *thismethod != NULL) {
1370 /*
1371 * We tried a new record layer method, but it didn't work out,
1372 * so we fallback to the original method and try again
1373 */
1374 meth = *thismethod;
1375 continue;
1376 }
1377 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_RECORD_LAYER);
1378 return 0;
1379
1380 case OSSL_RECORD_RETURN_SUCCESS:
1381 break;
1382
1383 default:
1384 /* Should not happen */
1385 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1386 return 0;
1387 }
1388 break;
1389 }
1390
1391 /*
1392 * Free the old record layer if we have one except in the case of DTLS when
1393 * writing and there are still buffered sent messages in our queue. In that
1394 * case the record layer is still referenced by those buffered messages for
1395 * potential retransmit. Only when those buffered messages get freed do we
1396 * free the record layer object (see dtls1_hm_fragment_free)
1397 */
1398 if (!SSL_CONNECTION_IS_DTLS(s)
1399 || direction == OSSL_RECORD_DIRECTION_READ
1400 || pqueue_peek(s->d1->sent_messages) == NULL) {
1401 if (*thismethod != NULL && !(*thismethod)->free(*thisrl)) {
1402 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1403 return 0;
1404 }
1405 }
1406
1407 *thisrl = newrl;
1408 *thismethod = meth;
1409
1410 return ssl_post_record_layer_select(s, direction);
1411 }
1412
1413 int ssl_set_record_protocol_version(SSL_CONNECTION *s, int vers)
1414 {
1415 if (!ossl_assert(s->rlayer.rrlmethod != NULL)
1416 || !ossl_assert(s->rlayer.wrlmethod != NULL))
1417 return 0;
1418 s->rlayer.rrlmethod->set_protocol_version(s->rlayer.rrl, s->version);
1419 s->rlayer.wrlmethod->set_protocol_version(s->rlayer.wrl, s->version);
1420
1421 return 1;
1422 }