2 * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
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
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"
21 #if defined(OPENSSL_SMALL_FOOTPRINT) || \
22 !( defined(AES_ASM) && ( \
23 defined(__x86_64) || defined(__x86_64__) || \
24 defined(_M_AMD64) || defined(_M_X64) ) \
26 # undef EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
27 # define EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK 0
30 void RECORD_LAYER_init(RECORD_LAYER
*rl
, SSL_CONNECTION
*s
)
35 void RECORD_LAYER_clear(RECORD_LAYER
*rl
)
38 memset(rl
->handshake_fragment
, 0, sizeof(rl
->handshake_fragment
));
39 rl
->handshake_fragment_len
= 0;
45 ssl3_release_write_buffer(rl
->s
);
47 RECORD_LAYER_reset_write_sequence(rl
);
49 if (rl
->rrlmethod
!= NULL
)
50 rl
->rrlmethod
->free(rl
->rrl
); /* Ignore return value */
51 if (rl
->wrlmethod
!= NULL
)
52 rl
->wrlmethod
->free(rl
->wrl
); /* Ignore return value */
53 BIO_free(rl
->rrlnext
);
61 DTLS_RECORD_LAYER_clear(rl
);
64 void RECORD_LAYER_release(RECORD_LAYER
*rl
)
66 if (rl
->numwpipes
> 0)
67 ssl3_release_write_buffer(rl
->s
);
70 /* Checks if we have unprocessed read ahead data pending */
71 int RECORD_LAYER_read_pending(const RECORD_LAYER
*rl
)
73 return rl
->rrlmethod
->unprocessed_read_pending(rl
->rrl
);
76 /* Checks if we have decrypted unread record data pending */
77 int RECORD_LAYER_processed_read_pending(const RECORD_LAYER
*rl
)
79 return (rl
->curr_rec
< rl
->num_recs
)
80 || rl
->rrlmethod
->processed_read_pending(rl
->rrl
);
83 int RECORD_LAYER_write_pending(const RECORD_LAYER
*rl
)
85 /* TODO(RECLAYER): Remove me when DTLS is moved to the write record layer */
86 if (SSL_CONNECTION_IS_DTLS(rl
->s
))
87 return (rl
->numwpipes
> 0)
88 && SSL3_BUFFER_get_left(&rl
->wbuf
[rl
->numwpipes
- 1]) != 0;
89 return rl
->wpend_tot
> 0;
92 void RECORD_LAYER_reset_write_sequence(RECORD_LAYER
*rl
)
94 memset(rl
->write_sequence
, 0, sizeof(rl
->write_sequence
));
97 size_t ssl3_pending(const SSL
*s
)
100 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
105 if (SSL_CONNECTION_IS_DTLS(sc
)) {
109 iter
= pqueue_iterator(sc
->rlayer
.d
->buffered_app_data
.q
);
110 while ((item
= pqueue_next(&iter
)) != NULL
) {
112 num
+= rdata
->length
;
116 for (i
= 0; i
< sc
->rlayer
.num_recs
; i
++) {
117 if (sc
->rlayer
.tlsrecs
[i
].type
!= SSL3_RT_APPLICATION_DATA
)
119 num
+= sc
->rlayer
.tlsrecs
[i
].length
;
122 num
+= sc
->rlayer
.rrlmethod
->app_data_pending(sc
->rlayer
.rrl
);
127 void SSL_CTX_set_default_read_buffer_len(SSL_CTX
*ctx
, size_t len
)
129 ctx
->default_read_buf_len
= len
;
132 void SSL_set_default_read_buffer_len(SSL
*s
, size_t len
)
134 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
138 sc
->rlayer
.default_read_buf_len
= len
;
141 const char *SSL_rstate_string_long(const SSL
*s
)
143 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
149 if (sc
->rlayer
.rrlmethod
== NULL
|| sc
->rlayer
.rrl
== NULL
)
152 sc
->rlayer
.rrlmethod
->get_state(sc
->rlayer
.rrl
, NULL
, &lng
);
157 const char *SSL_rstate_string(const SSL
*s
)
159 const SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_CONST_SSL(s
);
165 if (sc
->rlayer
.rrlmethod
== NULL
|| sc
->rlayer
.rrl
== NULL
)
168 sc
->rlayer
.rrlmethod
->get_state(sc
->rlayer
.rrl
, &shrt
, NULL
);
173 static int tls_write_check_pending(SSL_CONNECTION
*s
, int type
,
174 const unsigned char *buf
, size_t len
)
176 if (s
->rlayer
.wpend_tot
== 0)
179 /* We have pending data, so do some sanity checks */
180 if ((s
->rlayer
.wpend_tot
> len
)
181 || (!(s
->mode
& SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER
)
182 && (s
->rlayer
.wpend_buf
!= buf
))
183 || (s
->rlayer
.wpend_type
!= type
)) {
184 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_BAD_WRITE_RETRY
);
191 * Call this to write data in records of type 'type' It will return <= 0 if
192 * not all data has been sent or non-blocking IO.
194 int ssl3_write_bytes(SSL
*ssl
, int type
, const void *buf_
, size_t len
,
197 const unsigned char *buf
= buf_
;
199 size_t n
, max_send_fragment
, split_send_fragment
, maxpipes
;
200 /* TODO(RECLAYER): Re-enable multiblock code */
201 #if 0 && !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
205 SSL_CONNECTION
*s
= SSL_CONNECTION_FROM_SSL_ONLY(ssl
);
206 OSSL_RECORD_TEMPLATE tmpls
[SSL_MAX_PIPELINES
];
207 unsigned int recversion
;
212 s
->rwstate
= SSL_NOTHING
;
213 tot
= s
->rlayer
.wnum
;
215 * ensure that if we end up with a smaller value of data to write out
216 * than the original len from a write which didn't complete for
217 * non-blocking I/O and also somehow ended up avoiding the check for
218 * this in tls_write_check_pending/SSL_R_BAD_WRITE_RETRY as it must never be
219 * possible to end up with (len-tot) as a large number that will then
220 * promptly send beyond the end of the users buffer ... so we trap and
221 * report the error in a way the user will notice
223 if ((len
< s
->rlayer
.wnum
)
224 || ((s
->rlayer
.wpend_tot
!= 0)
225 && (len
< (s
->rlayer
.wnum
+ s
->rlayer
.wpend_tot
)))) {
226 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_BAD_LENGTH
);
230 if (s
->early_data_state
== SSL_EARLY_DATA_WRITING
231 && !ossl_early_data_count_ok(s
, len
, 0, 1)) {
232 /* SSLfatal() already called */
239 * If we are supposed to be sending a KeyUpdate or NewSessionTicket then go
240 * into init unless we have writes pending - in which case we should finish
243 if (s
->rlayer
.wpend_tot
== 0 && (s
->key_update
!= SSL_KEY_UPDATE_NONE
244 || s
->ext
.extra_tickets_expected
> 0))
245 ossl_statem_set_in_init(s
, 1);
248 * When writing early data on the server side we could be "in_init" in
249 * between receiving the EoED and the CF - but we don't want to handle those
252 if (SSL_in_init(ssl
) && !ossl_statem_get_in_handshake(s
)
253 && s
->early_data_state
!= SSL_EARLY_DATA_UNAUTH_WRITING
) {
254 i
= s
->handshake_func(ssl
);
255 /* SSLfatal() already called */
263 i
= tls_write_check_pending(s
, type
, buf
, len
);
265 /* SSLfatal() already called */
269 i
= s
->rlayer
.wrlmethod
->retry_write_records(s
->rlayer
.wrl
);
272 tot
+= s
->rlayer
.wpend_tot
;
273 s
->rlayer
.wpend_tot
= 0;
274 } /* else no retry required */
278 * We've not previously sent any data for this write so memorize
279 * arguments so that we can detect bad write retries later
281 s
->rlayer
.wpend_tot
= 0;
282 s
->rlayer
.wpend_type
= type
;
283 s
->rlayer
.wpend_buf
= buf
;
284 s
->rlayer
.wpend_ret
= len
;
287 /* TODO(RECLAYER): Re-enable multiblock code */
288 #if 0 && !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
290 * Depending on platform multi-block can deliver several *times*
291 * better performance. Downside is that it has to allocate
292 * jumbo buffer to accommodate up to 8 records, but the
293 * compromise is considered worthy.
295 if (type
== SSL3_RT_APPLICATION_DATA
296 && len
>= 4 * (max_send_fragment
= ssl_get_max_send_fragment(s
))
297 && s
->compress
== NULL
298 && s
->msg_callback
== NULL
300 && SSL_USE_EXPLICIT_IV(s
)
301 && !BIO_get_ktls_send(s
->wbio
)
302 && (EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(s
->enc_write_ctx
))
303 & EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
) != 0) {
304 unsigned char aad
[13];
305 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param
;
309 /* minimize address aliasing conflicts */
310 if ((max_send_fragment
& 0xfff) == 0)
311 max_send_fragment
-= 512;
313 if (tot
== 0 || wb
->buf
== NULL
) { /* allocate jumbo buffer */
314 ssl3_release_write_buffer(s
);
316 packlen
= EVP_CIPHER_CTX_ctrl(s
->enc_write_ctx
,
317 EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE
,
318 (int)max_send_fragment
, NULL
);
320 if (len
>= 8 * max_send_fragment
)
325 if (!ssl3_setup_write_buffer(s
, 1, packlen
)) {
326 /* SSLfatal() already called */
329 } else if (tot
== len
) { /* done? */
330 /* free jumbo buffer */
331 ssl3_release_write_buffer(s
);
338 if (n
< 4 * max_send_fragment
) {
339 /* free jumbo buffer */
340 ssl3_release_write_buffer(s
);
344 if (s
->s3
.alert_dispatch
) {
345 i
= ssl
->method
->ssl_dispatch_alert(ssl
);
347 /* SSLfatal() already called if appropriate */
348 s
->rlayer
.wnum
= tot
;
353 if (n
>= 8 * max_send_fragment
)
354 nw
= max_send_fragment
* (mb_param
.interleave
= 8);
356 nw
= max_send_fragment
* (mb_param
.interleave
= 4);
358 memcpy(aad
, s
->rlayer
.write_sequence
, 8);
360 aad
[9] = (unsigned char)(s
->version
>> 8);
361 aad
[10] = (unsigned char)(s
->version
);
368 packleni
= EVP_CIPHER_CTX_ctrl(s
->enc_write_ctx
,
369 EVP_CTRL_TLS1_1_MULTIBLOCK_AAD
,
370 sizeof(mb_param
), &mb_param
);
371 packlen
= (size_t)packleni
;
372 if (packleni
<= 0 || packlen
> wb
->len
) { /* never happens */
373 /* free jumbo buffer */
374 ssl3_release_write_buffer(s
);
378 mb_param
.out
= wb
->buf
;
379 mb_param
.inp
= &buf
[tot
];
382 if (EVP_CIPHER_CTX_ctrl(s
->enc_write_ctx
,
383 EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT
,
384 sizeof(mb_param
), &mb_param
) <= 0)
387 s
->rlayer
.write_sequence
[7] += mb_param
.interleave
;
388 if (s
->rlayer
.write_sequence
[7] < mb_param
.interleave
) {
390 while (j
>= 0 && (++s
->rlayer
.write_sequence
[j
--]) == 0) ;
396 s
->rlayer
.wpend_tot
= nw
;
397 s
->rlayer
.wpend_buf
= &buf
[tot
];
398 s
->rlayer
.wpend_type
= type
;
399 s
->rlayer
.wpend_ret
= nw
;
401 i
= ssl3_write_pending(s
, type
, &buf
[tot
], nw
, &tmpwrit
);
403 /* SSLfatal() already called if appropriate */
404 if (i
< 0 && (!s
->wbio
|| !BIO_should_retry(s
->wbio
))) {
405 /* free jumbo buffer */
406 ssl3_release_write_buffer(s
);
408 s
->rlayer
.wnum
= tot
;
412 /* free jumbo buffer */
413 ssl3_release_write_buffer(s
);
414 *written
= tot
+ tmpwrit
;
421 #endif /* !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK */
422 if (tot
== len
) { /* done? */
427 /* If we have an alert to send, lets send it */
428 if (s
->s3
.alert_dispatch
) {
429 i
= ssl
->method
->ssl_dispatch_alert(ssl
);
431 /* SSLfatal() already called if appropriate */
434 /* if it went, fall through and send more stuff */
439 max_send_fragment
= ssl_get_max_send_fragment(s
);
440 split_send_fragment
= ssl_get_split_send_fragment(s
);
442 * TODO(RECLAYER): This comment is now out-of-date and probably needs to
443 * move somewhere else
445 * If max_pipelines is 0 then this means "undefined" and we default to
446 * 1 pipeline. Similarly if the cipher does not support pipelined
447 * processing then we also only use 1 pipeline, or if we're not using
450 maxpipes
= s
->max_pipelines
;
451 if (maxpipes
> SSL_MAX_PIPELINES
) {
453 * We should have prevented this when we set max_pipelines so we
456 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
459 /* If no explicit maxpipes configuration - default to 1 */
460 /* TODO(RECLAYER): Should we ask the record layer how many pipes it supports? */
464 /* TODO(RECLAYER): FIX ME */
466 || s
->enc_write_ctx
== NULL
467 || (EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(s
->enc_write_ctx
))
468 & EVP_CIPH_FLAG_PIPELINE
) == 0
469 || !SSL_USE_EXPLICIT_IV(s
))
472 if (max_send_fragment
== 0
473 || split_send_fragment
== 0
474 || split_send_fragment
> max_send_fragment
) {
476 * We should have prevented this when we set/get the split and max send
477 * fragments so we shouldn't get here
479 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
484 * Some servers hang if initial client hello is larger than 256 bytes
485 * and record version number > TLS 1.0
487 /* TODO(RECLAYER): Does this also need to be in the DTLS equivalent code? */
488 recversion
= (s
->version
== TLS1_3_VERSION
) ? TLS1_2_VERSION
: s
->version
;
489 if (SSL_get_state(ssl
) == TLS_ST_CW_CLNT_HELLO
491 && TLS1_get_version(ssl
) > TLS1_VERSION
492 && s
->hello_retry_request
== SSL_HRR_NONE
)
493 recversion
= TLS1_VERSION
;
496 size_t tmppipelen
, remain
;
497 size_t numpipes
, j
, lensofar
= 0;
502 numpipes
= ((n
- 1) / split_send_fragment
) + 1;
503 if (numpipes
> maxpipes
)
506 if (n
/ numpipes
>= max_send_fragment
) {
508 * We have enough data to completely fill all available
511 for (j
= 0; j
< numpipes
; j
++) {
512 tmpls
[j
].type
= type
;
513 tmpls
[j
].version
= recversion
;
514 tmpls
[j
].buf
= &(buf
[tot
]) + (j
* max_send_fragment
);
515 tmpls
[j
].buflen
= max_send_fragment
;
517 /* Remember how much data we are going to be sending */
518 s
->rlayer
.wpend_tot
= numpipes
* max_send_fragment
;
520 /* We can partially fill all available pipelines */
521 tmppipelen
= n
/ numpipes
;
522 remain
= n
% numpipes
;
524 * If there is a remainder we add an extra byte to the first few
529 for (j
= 0; j
< numpipes
; j
++) {
530 tmpls
[j
].type
= type
;
531 tmpls
[j
].version
= recversion
;
532 tmpls
[j
].buf
= &(buf
[tot
]) + lensofar
;
533 tmpls
[j
].buflen
= tmppipelen
;
534 lensofar
+= tmppipelen
;
538 /* Remember how much data we are going to be sending */
539 s
->rlayer
.wpend_tot
= n
;
542 i
= s
->rlayer
.wrlmethod
->write_records(s
->rlayer
.wrl
, tmpls
, numpipes
);
544 /* SSLfatal() already called if appropriate */
545 s
->rlayer
.wnum
= tot
;
549 if (s
->rlayer
.wpend_tot
== n
550 || (type
== SSL3_RT_APPLICATION_DATA
551 && (s
->mode
& SSL_MODE_ENABLE_PARTIAL_WRITE
) != 0)) {
552 *written
= tot
+ s
->rlayer
.wpend_tot
;
553 s
->rlayer
.wpend_tot
= 0;
557 n
-= s
->rlayer
.wpend_tot
;
558 tot
+= s
->rlayer
.wpend_tot
;
562 int ossl_tls_handle_rlayer_return(SSL_CONNECTION
*s
, int ret
, char *file
,
565 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
567 if (ret
== OSSL_RECORD_RETURN_RETRY
) {
568 s
->rwstate
= SSL_READING
;
571 s
->rwstate
= SSL_NOTHING
;
572 if (ret
== OSSL_RECORD_RETURN_EOF
) {
573 if (s
->options
& SSL_OP_IGNORE_UNEXPECTED_EOF
) {
574 SSL_set_shutdown(ssl
, SSL_RECEIVED_SHUTDOWN
);
575 s
->s3
.warn_alert
= SSL_AD_CLOSE_NOTIFY
;
578 ERR_set_debug(file
, line
, 0);
579 ossl_statem_fatal(s
, SSL_AD_DECODE_ERROR
,
580 SSL_R_UNEXPECTED_EOF_WHILE_READING
, NULL
);
582 } else if (ret
== OSSL_RECORD_RETURN_FATAL
) {
583 int al
= s
->rlayer
.rrlmethod
->get_alert_code(s
->rlayer
.rrl
);
585 if (al
!= SSL_AD_NO_ALERT
) {
587 ERR_set_debug(file
, line
, 0);
588 ossl_statem_fatal(s
, al
, SSL_R_RECORD_LAYER_FAILURE
, NULL
);
591 * else some failure but there is no alert code. We don't log an
592 * error for this. The record layer should have logged an error
593 * already or, if not, its due to some sys call error which will be
594 * reported via SSL_ERROR_SYSCALL and errno.
598 * The record layer distinguishes the cases of EOF, non-fatal
599 * err and retry. Upper layers do not.
600 * If we got a retry or success then *ret is already correct,
601 * otherwise we need to convert the return value.
603 if (ret
== OSSL_RECORD_RETURN_NON_FATAL_ERR
|| ret
== OSSL_RECORD_RETURN_EOF
)
605 else if (ret
< OSSL_RECORD_RETURN_NON_FATAL_ERR
)
612 void ssl_release_record(SSL_CONNECTION
*s
, TLS_RECORD
*rr
)
614 if (rr
->rechandle
!= NULL
) {
615 /* The record layer allocated the buffers for this record */
616 s
->rlayer
.rrlmethod
->release_record(s
->rlayer
.rrl
, rr
->rechandle
);
618 /* We allocated the buffers for this record (only happens with DTLS) */
619 OPENSSL_free(rr
->data
);
621 s
->rlayer
.curr_rec
++;
625 * Return up to 'len' payload bytes received in 'type' records.
626 * 'type' is one of the following:
628 * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
629 * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
630 * - 0 (during a shutdown, no data has to be returned)
632 * If we don't have stored data to work from, read a SSL/TLS record first
633 * (possibly multiple records if we still don't have anything to return).
635 * This function must handle any surprises the peer may have for us, such as
636 * Alert records (e.g. close_notify) or renegotiation requests. ChangeCipherSpec
637 * messages are treated as if they were handshake messages *if* the |recvd_type|
638 * argument is non NULL.
639 * Also if record payloads contain fragments too small to process, we store
640 * them until there is enough for the respective protocol (the record protocol
641 * may use arbitrary fragmentation and even interleaving):
642 * Change cipher spec protocol
643 * just 1 byte needed, no need for keeping anything stored
645 * 2 bytes needed (AlertLevel, AlertDescription)
647 * 4 bytes needed (HandshakeType, uint24 length) -- we just have
648 * to detect unexpected Client Hello and Hello Request messages
649 * here, anything else is handled by higher layers
650 * Application data protocol
651 * none of our business
653 int ssl3_read_bytes(SSL
*ssl
, int type
, int *recvd_type
, unsigned char *buf
,
654 size_t len
, int peek
, size_t *readbytes
)
657 size_t n
, curr_rec
, totalbytes
;
659 void (*cb
) (const SSL
*ssl
, int type2
, int val
) = NULL
;
661 SSL_CONNECTION
*s
= SSL_CONNECTION_FROM_SSL_ONLY(ssl
);
663 is_tls13
= SSL_CONNECTION_IS_TLS13(s
);
666 && (type
!= SSL3_RT_APPLICATION_DATA
)
667 && (type
!= SSL3_RT_HANDSHAKE
))
668 || (peek
&& (type
!= SSL3_RT_APPLICATION_DATA
))) {
669 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
673 if ((type
== SSL3_RT_HANDSHAKE
) && (s
->rlayer
.handshake_fragment_len
> 0))
674 /* (partially) satisfy request from storage */
676 unsigned char *src
= s
->rlayer
.handshake_fragment
;
677 unsigned char *dst
= buf
;
682 while ((len
> 0) && (s
->rlayer
.handshake_fragment_len
> 0)) {
685 s
->rlayer
.handshake_fragment_len
--;
688 /* move any remaining fragment bytes: */
689 for (k
= 0; k
< s
->rlayer
.handshake_fragment_len
; k
++)
690 s
->rlayer
.handshake_fragment
[k
] = *src
++;
692 if (recvd_type
!= NULL
)
693 *recvd_type
= SSL3_RT_HANDSHAKE
;
700 * Now s->rlayer.handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE.
703 if (!ossl_statem_get_in_handshake(s
) && SSL_in_init(ssl
)) {
704 /* type == SSL3_RT_APPLICATION_DATA */
705 i
= s
->handshake_func(ssl
);
706 /* SSLfatal() already called */
713 s
->rwstate
= SSL_NOTHING
;
716 * For each record 'i' up to |num_recs]
717 * rr[i].type - is the type of record
719 * rr[i].off, - offset into 'data' for next read
720 * rr[i].length, - number of bytes.
722 /* get new records if necessary */
723 if (s
->rlayer
.curr_rec
>= s
->rlayer
.num_recs
) {
724 s
->rlayer
.curr_rec
= s
->rlayer
.num_recs
= 0;
726 rr
= &s
->rlayer
.tlsrecs
[s
->rlayer
.num_recs
];
728 ret
= HANDLE_RLAYER_RETURN(s
,
729 s
->rlayer
.rrlmethod
->read_record(s
->rlayer
.rrl
,
731 &rr
->version
, &rr
->type
,
732 &rr
->data
, &rr
->length
,
735 /* SSLfatal() already called if appropriate */
739 s
->rlayer
.num_recs
++;
740 } while (s
->rlayer
.rrlmethod
->processed_read_pending(s
->rlayer
.rrl
)
741 && s
->rlayer
.num_recs
< SSL_MAX_PIPELINES
);
743 rr
= &s
->rlayer
.tlsrecs
[s
->rlayer
.curr_rec
];
745 if (s
->rlayer
.handshake_fragment_len
> 0
746 && rr
->type
!= SSL3_RT_HANDSHAKE
747 && SSL_CONNECTION_IS_TLS13(s
)) {
748 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
,
749 SSL_R_MIXED_HANDSHAKE_AND_NON_HANDSHAKE_DATA
);
754 * Reset the count of consecutive warning alerts if we've got a non-empty
755 * record that isn't an alert.
757 if (rr
->type
!= SSL3_RT_ALERT
&& rr
->length
!= 0)
758 s
->rlayer
.alert_count
= 0;
760 /* we now have a packet which can be read and processed */
762 if (s
->s3
.change_cipher_spec
/* set when we receive ChangeCipherSpec,
763 * reset by ssl3_get_finished */
764 && (rr
->type
!= SSL3_RT_HANDSHAKE
)) {
765 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
,
766 SSL_R_DATA_BETWEEN_CCS_AND_FINISHED
);
771 * If the other end has shut down, throw anything we read away (even in
774 if (s
->shutdown
& SSL_RECEIVED_SHUTDOWN
) {
775 s
->rlayer
.curr_rec
++;
776 s
->rwstate
= SSL_NOTHING
;
781 || (rr
->type
== SSL3_RT_CHANGE_CIPHER_SPEC
782 && type
== SSL3_RT_HANDSHAKE
&& recvd_type
!= NULL
785 * SSL3_RT_APPLICATION_DATA or
786 * SSL3_RT_HANDSHAKE or
787 * SSL3_RT_CHANGE_CIPHER_SPEC
790 * make sure that we are not getting application data when we are
791 * doing a handshake for the first time
793 if (SSL_in_init(ssl
) && type
== SSL3_RT_APPLICATION_DATA
794 && s
->enc_read_ctx
== NULL
) {
795 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_APP_DATA_IN_HANDSHAKE
);
799 if (type
== SSL3_RT_HANDSHAKE
800 && rr
->type
== SSL3_RT_CHANGE_CIPHER_SPEC
801 && s
->rlayer
.handshake_fragment_len
> 0) {
802 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_CCS_RECEIVED_EARLY
);
806 if (recvd_type
!= NULL
)
807 *recvd_type
= rr
->type
;
811 * Skip a zero length record. This ensures multiple calls to
812 * SSL_read() with a zero length buffer will eventually cause
813 * SSL_pending() to report data as being available.
816 ssl_release_record(s
, rr
);
822 curr_rec
= s
->rlayer
.curr_rec
;
824 if (len
- totalbytes
> rr
->length
)
827 n
= len
- totalbytes
;
829 memcpy(buf
, &(rr
->data
[rr
->off
]), n
);
832 /* Mark any zero length record as consumed CVE-2016-6305 */
834 ssl_release_record(s
, rr
);
836 if (s
->options
& SSL_OP_CLEANSE_PLAINTEXT
)
837 OPENSSL_cleanse(&(rr
->data
[rr
->off
]), n
);
841 ssl_release_record(s
, rr
);
844 || (peek
&& n
== rr
->length
)) {
849 } while (type
== SSL3_RT_APPLICATION_DATA
850 && curr_rec
< s
->rlayer
.num_recs
851 && totalbytes
< len
);
852 if (totalbytes
== 0) {
853 /* We must have read empty records. Get more data */
856 *readbytes
= totalbytes
;
861 * If we get here, then type != rr->type; if we have a handshake message,
862 * then it was unexpected (Hello Request or Client Hello) or invalid (we
863 * were actually expecting a CCS).
867 * Lets just double check that we've not got an SSLv2 record
869 if (rr
->version
== SSL2_VERSION
) {
871 * Should never happen. ssl3_get_record() should only give us an SSLv2
872 * record back if this is the first packet and we are looking for an
873 * initial ClientHello. Therefore |type| should always be equal to
874 * |rr->type|. If not then something has gone horribly wrong
876 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
880 if (ssl
->method
->version
== TLS_ANY_VERSION
881 && (s
->server
|| rr
->type
!= SSL3_RT_ALERT
)) {
883 * If we've got this far and still haven't decided on what version
884 * we're using then this must be a client side alert we're dealing
885 * with. We shouldn't be receiving anything other than a ClientHello
886 * if we are a server.
888 s
->version
= rr
->version
;
889 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_UNEXPECTED_MESSAGE
);
894 * s->rlayer.handshake_fragment_len == 4 iff rr->type == SSL3_RT_HANDSHAKE;
895 * (Possibly rr is 'empty' now, i.e. rr->length may be 0.)
898 if (rr
->type
== SSL3_RT_ALERT
) {
899 unsigned int alert_level
, alert_descr
;
900 unsigned char *alert_bytes
= rr
->data
904 if (!PACKET_buf_init(&alert
, alert_bytes
, rr
->length
)
905 || !PACKET_get_1(&alert
, &alert_level
)
906 || !PACKET_get_1(&alert
, &alert_descr
)
907 || PACKET_remaining(&alert
) != 0) {
908 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_INVALID_ALERT
);
913 s
->msg_callback(0, s
->version
, SSL3_RT_ALERT
, alert_bytes
, 2, ssl
,
914 s
->msg_callback_arg
);
916 if (s
->info_callback
!= NULL
)
917 cb
= s
->info_callback
;
918 else if (ssl
->ctx
->info_callback
!= NULL
)
919 cb
= ssl
->ctx
->info_callback
;
922 j
= (alert_level
<< 8) | alert_descr
;
923 cb(ssl
, SSL_CB_READ_ALERT
, j
);
926 if ((!is_tls13
&& alert_level
== SSL3_AL_WARNING
)
927 || (is_tls13
&& alert_descr
== SSL_AD_USER_CANCELLED
)) {
928 s
->s3
.warn_alert
= alert_descr
;
929 ssl_release_record(s
, rr
);
931 s
->rlayer
.alert_count
++;
932 if (s
->rlayer
.alert_count
== MAX_WARN_ALERT_COUNT
) {
933 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
,
934 SSL_R_TOO_MANY_WARN_ALERTS
);
940 * Apart from close_notify the only other warning alert in TLSv1.3
941 * is user_cancelled - which we just ignore.
943 if (is_tls13
&& alert_descr
== SSL_AD_USER_CANCELLED
) {
945 } else if (alert_descr
== SSL_AD_CLOSE_NOTIFY
946 && (is_tls13
|| alert_level
== SSL3_AL_WARNING
)) {
947 s
->shutdown
|= SSL_RECEIVED_SHUTDOWN
;
949 } else if (alert_level
== SSL3_AL_FATAL
|| is_tls13
) {
950 s
->rwstate
= SSL_NOTHING
;
951 s
->s3
.fatal_alert
= alert_descr
;
952 SSLfatal_data(s
, SSL_AD_NO_ALERT
,
953 SSL_AD_REASON_OFFSET
+ alert_descr
,
954 "SSL alert number %d", alert_descr
);
955 s
->shutdown
|= SSL_RECEIVED_SHUTDOWN
;
956 ssl_release_record(s
, rr
);
957 SSL_CTX_remove_session(s
->session_ctx
, s
->session
);
959 } else if (alert_descr
== SSL_AD_NO_RENEGOTIATION
) {
961 * This is a warning but we receive it if we requested
962 * renegotiation and the peer denied it. Terminate with a fatal
963 * alert because if application tried to renegotiate it
964 * presumably had a good reason and expects it to succeed. In
965 * future we might have a renegotiation where we don't care if
966 * the peer refused it where we carry on.
968 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, SSL_R_NO_RENEGOTIATION
);
970 } else if (alert_level
== SSL3_AL_WARNING
) {
971 /* We ignore any other warning alert in TLSv1.2 and below */
975 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_UNKNOWN_ALERT_TYPE
);
979 if ((s
->shutdown
& SSL_SENT_SHUTDOWN
) != 0) {
980 if (rr
->type
== SSL3_RT_HANDSHAKE
) {
984 * We ignore any handshake messages sent to us unless they are
985 * TLSv1.3 in which case we want to process them. For all other
986 * handshake messages we can't do anything reasonable with them
987 * because we are unable to write any response due to having already
990 if (!SSL_CONNECTION_IS_TLS13(s
)) {
991 ssl_release_record(s
, rr
);
993 if ((s
->mode
& SSL_MODE_AUTO_RETRY
) != 0)
996 s
->rwstate
= SSL_READING
;
997 rbio
= SSL_get_rbio(ssl
);
998 BIO_clear_retry_flags(rbio
);
999 BIO_set_retry_read(rbio
);
1004 * The peer is continuing to send application data, but we have
1005 * already sent close_notify. If this was expected we should have
1006 * been called via SSL_read() and this would have been handled
1008 * No alert sent because we already sent close_notify
1010 ssl_release_record(s
, rr
);
1011 SSLfatal(s
, SSL_AD_NO_ALERT
,
1012 SSL_R_APPLICATION_DATA_AFTER_CLOSE_NOTIFY
);
1018 * For handshake data we have 'fragment' storage, so fill that so that we
1019 * can process the header at a fixed place. This is done after the
1020 * "SHUTDOWN" code above to avoid filling the fragment storage with data
1021 * that we're just going to discard.
1023 if (rr
->type
== SSL3_RT_HANDSHAKE
) {
1024 size_t dest_maxlen
= sizeof(s
->rlayer
.handshake_fragment
);
1025 unsigned char *dest
= s
->rlayer
.handshake_fragment
;
1026 size_t *dest_len
= &s
->rlayer
.handshake_fragment_len
;
1028 n
= dest_maxlen
- *dest_len
; /* available space in 'dest' */
1030 n
= rr
->length
; /* available bytes */
1032 /* now move 'n' bytes: */
1033 memcpy(dest
+ *dest_len
, rr
->data
+ rr
->off
, n
);
1037 if (rr
->length
== 0)
1038 ssl_release_record(s
, rr
);
1040 if (*dest_len
< dest_maxlen
)
1041 goto start
; /* fragment was too small */
1044 if (rr
->type
== SSL3_RT_CHANGE_CIPHER_SPEC
) {
1045 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_CCS_RECEIVED_EARLY
);
1050 * Unexpected handshake message (ClientHello, NewSessionTicket (TLS1.3) or
1051 * protocol violation)
1053 if ((s
->rlayer
.handshake_fragment_len
>= 4)
1054 && !ossl_statem_get_in_handshake(s
)) {
1055 int ined
= (s
->early_data_state
== SSL_EARLY_DATA_READING
);
1057 /* We found handshake data, so we're going back into init */
1058 ossl_statem_set_in_init(s
, 1);
1060 i
= s
->handshake_func(ssl
);
1061 /* SSLfatal() already called if appropriate */
1069 * If we were actually trying to read early data and we found a
1070 * handshake message, then we don't want to continue to try and read
1071 * the application data any more. It won't be "early" now.
1076 if (!(s
->mode
& SSL_MODE_AUTO_RETRY
)) {
1077 if (!RECORD_LAYER_read_pending(&s
->rlayer
)) {
1080 * In the case where we try to read application data, but we
1081 * trigger an SSL handshake, we return -1 with the retry
1082 * option set. Otherwise renegotiation may cause nasty
1083 * problems in the blocking world
1085 s
->rwstate
= SSL_READING
;
1086 bio
= SSL_get_rbio(ssl
);
1087 BIO_clear_retry_flags(bio
);
1088 BIO_set_retry_read(bio
);
1098 * TLS 1.0 and 1.1 say you SHOULD ignore unrecognised record types, but
1099 * TLS 1.2 says you MUST send an unexpected message alert. We use the
1100 * TLS 1.2 behaviour for all protocol versions to prevent issues where
1101 * no progress is being made and the peer continually sends unrecognised
1102 * record types, using up resources processing them.
1104 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_UNEXPECTED_RECORD
);
1106 case SSL3_RT_CHANGE_CIPHER_SPEC
:
1108 case SSL3_RT_HANDSHAKE
:
1110 * we already handled all of these, with the possible exception of
1111 * SSL3_RT_HANDSHAKE when ossl_statem_get_in_handshake(s) is true, but
1112 * that should not happen when type != rr->type
1114 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, ERR_R_INTERNAL_ERROR
);
1116 case SSL3_RT_APPLICATION_DATA
:
1118 * At this point, we were expecting handshake data, but have
1119 * application data. If the library was running inside ssl3_read()
1120 * (i.e. in_read_app_data is set) and it makes sense to read
1121 * application data at this point (session renegotiation not yet
1122 * started), we will indulge it.
1124 if (ossl_statem_app_data_allowed(s
)) {
1125 s
->s3
.in_read_app_data
= 2;
1127 } else if (ossl_statem_skip_early_data(s
)) {
1129 * This can happen after a client sends a CH followed by early_data,
1130 * but the server responds with a HelloRetryRequest. The server
1131 * reads the next record from the client expecting to find a
1132 * plaintext ClientHello but gets a record which appears to be
1133 * application data. The trial decrypt "works" because null
1134 * decryption was applied. We just skip it and move on to the next
1137 if (!ossl_early_data_count_ok(s
, rr
->length
,
1138 EARLY_DATA_CIPHERTEXT_OVERHEAD
, 0)) {
1139 /* SSLfatal() already called */
1142 ssl_release_record(s
, rr
);
1145 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_UNEXPECTED_RECORD
);
1151 void ssl3_record_sequence_update(unsigned char *seq
)
1155 for (i
= 7; i
>= 0; i
--) {
1163 * Returns true if the current rrec was sent in SSLv2 backwards compatible
1164 * format and false otherwise.
1166 int RECORD_LAYER_is_sslv2_record(RECORD_LAYER
*rl
)
1168 if (SSL_CONNECTION_IS_DTLS(rl
->s
))
1170 return rl
->tlsrecs
[0].version
== SSL2_VERSION
;
1173 static OSSL_FUNC_rlayer_msg_callback_fn rlayer_msg_callback_wrapper
;
1174 static void rlayer_msg_callback_wrapper(int write_p
, int version
,
1175 int content_type
, const void *buf
,
1176 size_t len
, void *cbarg
)
1178 SSL_CONNECTION
*s
= cbarg
;
1179 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
1181 if (s
->msg_callback
!= NULL
)
1182 s
->msg_callback(write_p
, version
, content_type
, buf
, len
, ssl
,
1183 s
->msg_callback_arg
);
1186 static OSSL_FUNC_rlayer_security_fn rlayer_security_wrapper
;
1187 static int rlayer_security_wrapper(void *cbarg
, int op
, int bits
, int nid
,
1190 SSL_CONNECTION
*s
= cbarg
;
1192 return ssl_security(s
, op
, bits
, nid
, other
);
1195 static const OSSL_DISPATCH rlayer_dispatch
[] = {
1196 { OSSL_FUNC_RLAYER_SKIP_EARLY_DATA
, (void (*)(void))ossl_statem_skip_early_data
},
1197 { OSSL_FUNC_RLAYER_MSG_CALLBACK
, (void (*)(void))rlayer_msg_callback_wrapper
},
1198 { OSSL_FUNC_RLAYER_SECURITY
, (void (*)(void))rlayer_security_wrapper
},
1202 static const OSSL_RECORD_METHOD
*ssl_select_next_record_layer(SSL_CONNECTION
*s
,
1206 if (level
== OSSL_RECORD_PROTECTION_LEVEL_NONE
) {
1207 if (SSL_CONNECTION_IS_DTLS(s
))
1208 return &ossl_dtls_record_method
;
1210 return &ossl_tls_record_method
;
1213 #ifndef OPENSSL_NO_KTLS
1214 /* KTLS does not support renegotiation */
1215 if (level
== OSSL_RECORD_PROTECTION_LEVEL_APPLICATION
1216 && (s
->options
& SSL_OP_ENABLE_KTLS
) != 0
1217 && (SSL_CONNECTION_IS_TLS13(s
) || SSL_IS_FIRST_HANDSHAKE(s
)))
1218 return &ossl_ktls_record_method
;
1221 /* Default to the current OSSL_RECORD_METHOD */
1222 return s
->rlayer
.rrlmethod
;
1225 static int ssl_post_record_layer_select(SSL_CONNECTION
*s
, int direction
)
1227 const OSSL_RECORD_METHOD
*thismethod
;
1228 OSSL_RECORD_LAYER
*thisrl
;
1230 if (direction
== OSSL_RECORD_DIRECTION_READ
) {
1231 thismethod
= s
->rlayer
.rrlmethod
;
1232 thisrl
= s
->rlayer
.rrl
;
1234 thismethod
= s
->rlayer
.wrlmethod
;
1235 thisrl
= s
->rlayer
.wrl
;
1238 #ifndef OPENSSL_NO_KTLS
1240 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
1242 if (s
->rlayer
.rrlmethod
== &ossl_ktls_record_method
) {
1243 /* KTLS does not support renegotiation so disallow it */
1244 SSL_set_options(ssl
, SSL_OP_NO_RENEGOTIATION
);
1248 if (SSL_IS_FIRST_HANDSHAKE(s
) && thismethod
->set_first_handshake
!= NULL
)
1249 thismethod
->set_first_handshake(thisrl
, 1);
1251 if (s
->max_pipelines
!= 0 && thismethod
->set_max_pipelines
!= NULL
)
1252 thismethod
->set_max_pipelines(thisrl
, s
->max_pipelines
);
1257 int ssl_set_new_record_layer(SSL_CONNECTION
*s
, int version
,
1258 int direction
, int level
,
1259 unsigned char *key
, size_t keylen
,
1260 unsigned char *iv
, size_t ivlen
,
1261 unsigned char *mackey
, size_t mackeylen
,
1262 const EVP_CIPHER
*ciph
, size_t taglen
,
1263 int mactype
, const EVP_MD
*md
,
1264 const SSL_COMP
*comp
)
1266 OSSL_PARAM options
[5], *opts
= options
;
1267 OSSL_PARAM settings
[6], *set
= settings
;
1268 const OSSL_RECORD_METHOD
**thismethod
;
1269 OSSL_RECORD_LAYER
**thisrl
, *newrl
= NULL
;
1271 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
1272 const OSSL_RECORD_METHOD
*meth
;
1273 int use_etm
, stream_mac
= 0, tlstree
= 0;
1274 unsigned int maxfrag
= SSL3_RT_MAX_PLAIN_LENGTH
;
1275 int use_early_data
= 0;
1276 uint32_t max_early_data
;
1278 meth
= ssl_select_next_record_layer(s
, level
);
1280 if (direction
== OSSL_RECORD_DIRECTION_READ
) {
1281 thismethod
= &s
->rlayer
.rrlmethod
;
1282 thisrl
= &s
->rlayer
.rrl
;
1285 thismethod
= &s
->rlayer
.wrlmethod
;
1286 thisrl
= &s
->rlayer
.wrl
;
1293 if (!ossl_assert(meth
!= NULL
)) {
1294 ERR_raise(ERR_LIB_SSL
, ERR_R_INTERNAL_ERROR
);
1298 /* Parameters that *may* be supported by a record layer if passed */
1299 *opts
++ = OSSL_PARAM_construct_uint64(OSSL_LIBSSL_RECORD_LAYER_PARAM_OPTIONS
,
1301 *opts
++ = OSSL_PARAM_construct_uint32(OSSL_LIBSSL_RECORD_LAYER_PARAM_MODE
,
1303 if (direction
== OSSL_RECORD_DIRECTION_READ
) {
1304 *opts
++ = OSSL_PARAM_construct_size_t(OSSL_LIBSSL_RECORD_LAYER_READ_BUFFER_LEN
,
1305 &s
->rlayer
.default_read_buf_len
);
1306 *opts
++ = OSSL_PARAM_construct_int(OSSL_LIBSSL_RECORD_LAYER_PARAM_READ_AHEAD
,
1307 &s
->rlayer
.read_ahead
);
1309 *opts
= OSSL_PARAM_construct_end();
1311 /* Parameters that *must* be supported by a record layer if passed */
1312 if (direction
== OSSL_RECORD_DIRECTION_READ
) {
1313 use_etm
= SSL_READ_ETM(s
) ? 1 : 0;
1314 if ((s
->mac_flags
& SSL_MAC_FLAG_READ_MAC_STREAM
) != 0)
1317 if ((s
->mac_flags
& SSL_MAC_FLAG_READ_MAC_TLSTREE
) != 0)
1320 use_etm
= SSL_WRITE_ETM(s
) ? 1 : 0;
1321 if ((s
->mac_flags
& SSL_MAC_FLAG_WRITE_MAC_STREAM
) != 0)
1324 if ((s
->mac_flags
& SSL_MAC_FLAG_WRITE_MAC_TLSTREE
) != 0)
1329 *set
++ = OSSL_PARAM_construct_int(OSSL_LIBSSL_RECORD_LAYER_PARAM_USE_ETM
,
1333 *set
++ = OSSL_PARAM_construct_int(OSSL_LIBSSL_RECORD_LAYER_PARAM_STREAM_MAC
,
1337 *set
++ = OSSL_PARAM_construct_int(OSSL_LIBSSL_RECORD_LAYER_PARAM_TLSTREE
,
1340 if (s
->session
!= NULL
&& USE_MAX_FRAGMENT_LENGTH_EXT(s
->session
))
1341 maxfrag
= GET_MAX_FRAGMENT_LENGTH(s
->session
);
1343 if (maxfrag
!= SSL3_RT_MAX_PLAIN_LENGTH
)
1344 *set
++ = OSSL_PARAM_construct_uint(OSSL_LIBSSL_RECORD_LAYER_PARAM_MAX_FRAG_LEN
,
1348 * The record layer must check the amount of early data sent or received
1349 * using the early keys. A server also needs to worry about rejected early
1350 * data that might arrive when the handshake keys are in force.
1352 /* TODO(RECLAYER): Check this when doing the "write" record layer */
1353 if (s
->server
&& direction
== OSSL_RECORD_DIRECTION_READ
) {
1354 use_early_data
= (level
== OSSL_RECORD_PROTECTION_LEVEL_EARLY
1355 || level
== OSSL_RECORD_PROTECTION_LEVEL_HANDSHAKE
);
1356 } else if (!s
->server
&& direction
== OSSL_RECORD_DIRECTION_WRITE
) {
1357 use_early_data
= (level
== OSSL_RECORD_PROTECTION_LEVEL_EARLY
);
1359 if (use_early_data
) {
1360 max_early_data
= ossl_get_max_early_data(s
);
1362 if (max_early_data
!= 0)
1363 *set
++ = OSSL_PARAM_construct_uint(OSSL_LIBSSL_RECORD_LAYER_PARAM_MAX_EARLY_DATA
,
1367 *set
= OSSL_PARAM_construct_end();
1373 unsigned int epoch
= 0;;
1375 if (direction
== OSSL_RECORD_DIRECTION_READ
) {
1376 prev
= s
->rlayer
.rrlnext
;
1377 if (SSL_CONNECTION_IS_DTLS(s
)
1378 && level
!= OSSL_RECORD_PROTECTION_LEVEL_NONE
)
1379 epoch
= DTLS_RECORD_LAYER_get_r_epoch(&s
->rlayer
) + 1; /* new epoch */
1381 if (SSL_CONNECTION_IS_DTLS(s
))
1382 next
= BIO_new(BIO_s_dgram_mem());
1384 next
= BIO_new(BIO_s_mem());
1388 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1391 s
->rlayer
.rrlnext
= next
;
1394 rlret
= meth
->new_record_layer(sctx
->libctx
, sctx
->propq
, version
,
1395 s
->server
, direction
, level
, epoch
,
1396 key
, keylen
, iv
, ivlen
, mackey
,
1397 mackeylen
, ciph
, taglen
, mactype
, md
,
1398 comp
, prev
, thisbio
, next
, NULL
, NULL
,
1399 settings
, options
, rlayer_dispatch
, s
,
1403 case OSSL_RECORD_RETURN_FATAL
:
1404 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_RECORD_LAYER_FAILURE
);
1407 case OSSL_RECORD_RETURN_NON_FATAL_ERR
:
1408 if (*thismethod
!= meth
&& *thismethod
!= NULL
) {
1410 * We tried a new record layer method, but it didn't work out,
1411 * so we fallback to the original method and try again
1416 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_NO_SUITABLE_RECORD_LAYER
);
1419 case OSSL_RECORD_RETURN_SUCCESS
:
1423 /* Should not happen */
1424 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1430 if (*thismethod
!= NULL
&& !(*thismethod
)->free(*thisrl
)) {
1431 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1438 return ssl_post_record_layer_select(s
, direction
);
1441 int ssl_set_record_protocol_version(SSL_CONNECTION
*s
, int vers
)
1443 if (!ossl_assert(s
->rlayer
.rrlmethod
!= NULL
)
1444 || !ossl_assert(s
->rlayer
.wrlmethod
!= NULL
))
1446 s
->rlayer
.rrlmethod
->set_protocol_version(s
->rlayer
.rrl
, s
->version
);
1447 s
->rlayer
.wrlmethod
->set_protocol_version(s
->rlayer
.wrl
, s
->version
);