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