2 * Copyright 1995-2018 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
10 #include "../ssl_local.h"
11 #include "internal/constant_time.h"
12 #include <openssl/trace.h>
13 #include <openssl/rand.h>
14 #include "record_local.h"
15 #include "internal/cryptlib.h"
17 static const unsigned char ssl3_pad_1
[48] = {
18 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
19 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
20 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
21 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
22 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
23 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36
26 static const unsigned char ssl3_pad_2
[48] = {
27 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
28 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
29 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
30 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
31 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
32 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c
36 * Clear the contents of an SSL3_RECORD but retain any memory allocated
38 void SSL3_RECORD_clear(SSL3_RECORD
*r
, size_t num_recs
)
43 for (i
= 0; i
< num_recs
; i
++) {
46 memset(&r
[i
], 0, sizeof(*r
));
51 void SSL3_RECORD_release(SSL3_RECORD
*r
, size_t num_recs
)
55 for (i
= 0; i
< num_recs
; i
++) {
56 OPENSSL_free(r
[i
].comp
);
61 void SSL3_RECORD_set_seq_num(SSL3_RECORD
*r
, const unsigned char *seq_num
)
63 memcpy(r
->seq_num
, seq_num
, SEQ_NUM_SIZE
);
67 * Peeks ahead into "read_ahead" data to see if we have a whole record waiting
68 * for us in the buffer.
70 static int ssl3_record_app_data_waiting(SSL
*s
)
76 rbuf
= RECORD_LAYER_get_rbuf(&s
->rlayer
);
78 p
= SSL3_BUFFER_get_buf(rbuf
);
82 left
= SSL3_BUFFER_get_left(rbuf
);
84 if (left
< SSL3_RT_HEADER_LENGTH
)
87 p
+= SSL3_BUFFER_get_offset(rbuf
);
90 * We only check the type and record length, we will sanity check version
93 if (*p
!= SSL3_RT_APPLICATION_DATA
)
99 if (left
< SSL3_RT_HEADER_LENGTH
+ len
)
105 int early_data_count_ok(SSL
*s
, size_t length
, size_t overhead
, int send
)
107 uint32_t max_early_data
;
108 SSL_SESSION
*sess
= s
->session
;
111 * If we are a client then we always use the max_early_data from the
112 * session/psksession. Otherwise we go with the lowest out of the max early
113 * data set in the session and the configured max_early_data.
115 if (!s
->server
&& sess
->ext
.max_early_data
== 0) {
116 if (!ossl_assert(s
->psksession
!= NULL
117 && s
->psksession
->ext
.max_early_data
> 0)) {
118 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_EARLY_DATA_COUNT_OK
,
119 ERR_R_INTERNAL_ERROR
);
122 sess
= s
->psksession
;
126 max_early_data
= sess
->ext
.max_early_data
;
127 else if (s
->ext
.early_data
!= SSL_EARLY_DATA_ACCEPTED
)
128 max_early_data
= s
->recv_max_early_data
;
130 max_early_data
= s
->recv_max_early_data
< sess
->ext
.max_early_data
131 ? s
->recv_max_early_data
: sess
->ext
.max_early_data
;
133 if (max_early_data
== 0) {
134 SSLfatal(s
, send
? SSL_AD_INTERNAL_ERROR
: SSL_AD_UNEXPECTED_MESSAGE
,
135 SSL_F_EARLY_DATA_COUNT_OK
, SSL_R_TOO_MUCH_EARLY_DATA
);
139 /* If we are dealing with ciphertext we need to allow for the overhead */
140 max_early_data
+= overhead
;
142 if (s
->early_data_count
+ length
> max_early_data
) {
143 SSLfatal(s
, send
? SSL_AD_INTERNAL_ERROR
: SSL_AD_UNEXPECTED_MESSAGE
,
144 SSL_F_EARLY_DATA_COUNT_OK
, SSL_R_TOO_MUCH_EARLY_DATA
);
147 s
->early_data_count
+= length
;
153 * MAX_EMPTY_RECORDS defines the number of consecutive, empty records that
154 * will be processed per call to ssl3_get_record. Without this limit an
155 * attacker could send empty records at a faster rate than we can process and
156 * cause ssl3_get_record to loop forever.
158 #define MAX_EMPTY_RECORDS 32
160 #define SSL2_RT_HEADER_LENGTH 2
162 * Call this to get new input records.
163 * It will return <= 0 if more data is needed, normally due to an error
164 * or non-blocking IO.
165 * When it finishes, |numrpipes| records have been decoded. For each record 'i':
166 * rr[i].type - is the type of record
168 * rr[i].length, - number of bytes
169 * Multiple records will only be returned if the record types are all
170 * SSL3_RT_APPLICATION_DATA. The number of records returned will always be <=
173 /* used only by ssl3_read_bytes */
174 int ssl3_get_record(SSL
*s
)
179 SSL3_RECORD
*rr
, *thisrr
;
183 unsigned char md
[EVP_MAX_MD_SIZE
];
184 unsigned int version
;
187 size_t num_recs
= 0, max_recs
, j
;
188 PACKET pkt
, sslv2pkt
;
189 size_t first_rec_len
;
192 rr
= RECORD_LAYER_get_rrec(&s
->rlayer
);
193 rbuf
= RECORD_LAYER_get_rbuf(&s
->rlayer
);
194 is_ktls_left
= (rbuf
->left
> 0);
195 max_recs
= s
->max_pipelines
;
201 thisrr
= &rr
[num_recs
];
203 /* check if we have the header */
204 if ((RECORD_LAYER_get_rstate(&s
->rlayer
) != SSL_ST_READ_BODY
) ||
205 (RECORD_LAYER_get_packet_length(&s
->rlayer
)
206 < SSL3_RT_HEADER_LENGTH
)) {
210 rret
= ssl3_read_n(s
, SSL3_RT_HEADER_LENGTH
,
211 SSL3_BUFFER_get_len(rbuf
), 0,
212 num_recs
== 0 ? 1 : 0, &n
);
214 #ifndef OPENSSL_NO_KTLS
215 if (!BIO_get_ktls_recv(s
->rbio
))
216 return rret
; /* error or non-blocking */
219 SSLfatal(s
, SSL_AD_BAD_RECORD_MAC
,
220 SSL_F_SSL3_GET_RECORD
,
221 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC
);
224 SSLfatal(s
, SSL_AD_RECORD_OVERFLOW
,
225 SSL_F_SSL3_GET_RECORD
,
226 SSL_R_PACKET_LENGTH_TOO_LONG
);
229 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
,
230 SSL_F_SSL3_GET_RECORD
,
231 SSL_R_WRONG_VERSION_NUMBER
);
239 RECORD_LAYER_set_rstate(&s
->rlayer
, SSL_ST_READ_BODY
);
241 p
= RECORD_LAYER_get_packet(&s
->rlayer
);
242 if (!PACKET_buf_init(&pkt
, RECORD_LAYER_get_packet(&s
->rlayer
),
243 RECORD_LAYER_get_packet_length(&s
->rlayer
))) {
244 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_SSL3_GET_RECORD
,
245 ERR_R_INTERNAL_ERROR
);
249 if (!PACKET_get_net_2_len(&sslv2pkt
, &sslv2len
)
250 || !PACKET_get_1(&sslv2pkt
, &type
)) {
251 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_SSL3_GET_RECORD
,
252 ERR_R_INTERNAL_ERROR
);
256 * The first record received by the server may be a V2ClientHello.
258 if (s
->server
&& RECORD_LAYER_is_first_record(&s
->rlayer
)
259 && (sslv2len
& 0x8000) != 0
260 && (type
== SSL2_MT_CLIENT_HELLO
)) {
264 * |num_recs| here will actually always be 0 because
265 * |num_recs > 0| only ever occurs when we are processing
266 * multiple app data records - which we know isn't the case here
267 * because it is an SSLv2ClientHello. We keep it using
268 * |num_recs| for the sake of consistency
270 thisrr
->type
= SSL3_RT_HANDSHAKE
;
271 thisrr
->rec_version
= SSL2_VERSION
;
273 thisrr
->length
= sslv2len
& 0x7fff;
275 if (thisrr
->length
> SSL3_BUFFER_get_len(rbuf
)
276 - SSL2_RT_HEADER_LENGTH
) {
277 SSLfatal(s
, SSL_AD_RECORD_OVERFLOW
, SSL_F_SSL3_GET_RECORD
,
278 SSL_R_PACKET_LENGTH_TOO_LONG
);
282 if (thisrr
->length
< MIN_SSL2_RECORD_LEN
) {
283 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_SSL3_GET_RECORD
,
284 SSL_R_LENGTH_TOO_SHORT
);
288 /* SSLv3+ style record */
290 s
->msg_callback(0, 0, SSL3_RT_HEADER
, p
, 5, s
,
291 s
->msg_callback_arg
);
293 /* Pull apart the header into the SSL3_RECORD */
294 if (!PACKET_get_1(&pkt
, &type
)
295 || !PACKET_get_net_2(&pkt
, &version
)
296 || !PACKET_get_net_2_len(&pkt
, &thisrr
->length
)) {
297 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_SSL3_GET_RECORD
,
298 ERR_R_INTERNAL_ERROR
);
302 thisrr
->rec_version
= version
;
305 * Lets check version. In TLSv1.3 we only check this field
306 * when encryption is occurring (see later check). For the
307 * ServerHello after an HRR we haven't actually selected TLSv1.3
308 * yet, but we still treat it as TLSv1.3, so we must check for
311 if (!s
->first_packet
&& !SSL_IS_TLS13(s
)
312 && s
->hello_retry_request
!= SSL_HRR_PENDING
313 && version
!= (unsigned int)s
->version
) {
314 if ((s
->version
& 0xFF00) == (version
& 0xFF00)
315 && !s
->enc_write_ctx
&& !s
->write_hash
) {
316 if (thisrr
->type
== SSL3_RT_ALERT
) {
318 * The record is using an incorrect version number,
319 * but what we've got appears to be an alert. We
320 * haven't read the body yet to check whether its a
321 * fatal or not - but chances are it is. We probably
322 * shouldn't send a fatal alert back. We'll just
325 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_SSL3_GET_RECORD
,
326 SSL_R_WRONG_VERSION_NUMBER
);
330 * Send back error using their minor version number :-)
332 s
->version
= (unsigned short)version
;
334 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
, SSL_F_SSL3_GET_RECORD
,
335 SSL_R_WRONG_VERSION_NUMBER
);
339 if ((version
>> 8) != SSL3_VERSION_MAJOR
) {
340 if (RECORD_LAYER_is_first_record(&s
->rlayer
)) {
341 /* Go back to start of packet, look at the five bytes
343 p
= RECORD_LAYER_get_packet(&s
->rlayer
);
344 if (strncmp((char *)p
, "GET ", 4) == 0 ||
345 strncmp((char *)p
, "POST ", 5) == 0 ||
346 strncmp((char *)p
, "HEAD ", 5) == 0 ||
347 strncmp((char *)p
, "PUT ", 4) == 0) {
348 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_SSL3_GET_RECORD
,
351 } else if (strncmp((char *)p
, "CONNE", 5) == 0) {
352 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_SSL3_GET_RECORD
,
353 SSL_R_HTTPS_PROXY_REQUEST
);
357 /* Doesn't look like TLS - don't send an alert */
358 SSLfatal(s
, SSL_AD_NO_ALERT
, SSL_F_SSL3_GET_RECORD
,
359 SSL_R_WRONG_VERSION_NUMBER
);
362 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
,
363 SSL_F_SSL3_GET_RECORD
,
364 SSL_R_WRONG_VERSION_NUMBER
);
369 if (SSL_IS_TLS13(s
) && s
->enc_read_ctx
!= NULL
) {
370 if (thisrr
->type
!= SSL3_RT_APPLICATION_DATA
371 && (thisrr
->type
!= SSL3_RT_CHANGE_CIPHER_SPEC
372 || !SSL_IS_FIRST_HANDSHAKE(s
))
373 && (thisrr
->type
!= SSL3_RT_ALERT
374 || s
->statem
.enc_read_state
375 != ENC_READ_STATE_ALLOW_PLAIN_ALERTS
)) {
376 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
,
377 SSL_F_SSL3_GET_RECORD
, SSL_R_BAD_RECORD_TYPE
);
380 if (thisrr
->rec_version
!= TLS1_2_VERSION
) {
381 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_SSL3_GET_RECORD
,
382 SSL_R_WRONG_VERSION_NUMBER
);
388 SSL3_BUFFER_get_len(rbuf
) - SSL3_RT_HEADER_LENGTH
) {
389 SSLfatal(s
, SSL_AD_RECORD_OVERFLOW
, SSL_F_SSL3_GET_RECORD
,
390 SSL_R_PACKET_LENGTH_TOO_LONG
);
395 /* now s->rlayer.rstate == SSL_ST_READ_BODY */
398 if (SSL_IS_TLS13(s
)) {
399 if (thisrr
->length
> SSL3_RT_MAX_TLS13_ENCRYPTED_LENGTH
) {
400 SSLfatal(s
, SSL_AD_RECORD_OVERFLOW
, SSL_F_SSL3_GET_RECORD
,
401 SSL_R_ENCRYPTED_LENGTH_TOO_LONG
);
405 size_t len
= SSL3_RT_MAX_ENCRYPTED_LENGTH
;
407 #ifndef OPENSSL_NO_COMP
409 * If OPENSSL_NO_COMP is defined then SSL3_RT_MAX_ENCRYPTED_LENGTH
410 * does not include the compression overhead anyway.
412 if (s
->expand
== NULL
)
413 len
-= SSL3_RT_MAX_COMPRESSED_OVERHEAD
;
416 if (thisrr
->length
> len
&& !BIO_get_ktls_recv(s
->rbio
)) {
417 SSLfatal(s
, SSL_AD_RECORD_OVERFLOW
, SSL_F_SSL3_GET_RECORD
,
418 SSL_R_ENCRYPTED_LENGTH_TOO_LONG
);
424 * s->rlayer.rstate == SSL_ST_READ_BODY, get and decode the data.
425 * Calculate how much more data we need to read for the rest of the
428 if (thisrr
->rec_version
== SSL2_VERSION
) {
429 more
= thisrr
->length
+ SSL2_RT_HEADER_LENGTH
430 - SSL3_RT_HEADER_LENGTH
;
432 more
= thisrr
->length
;
436 /* now s->packet_length == SSL3_RT_HEADER_LENGTH */
438 rret
= ssl3_read_n(s
, more
, more
, 1, 0, &n
);
440 return rret
; /* error or non-blocking io */
443 /* set state for later operations */
444 RECORD_LAYER_set_rstate(&s
->rlayer
, SSL_ST_READ_HEADER
);
447 * At this point, s->packet_length == SSL3_RT_HEADER_LENGTH
448 * + thisrr->length, or s->packet_length == SSL2_RT_HEADER_LENGTH
449 * + thisrr->length and we have that many bytes in s->packet
451 if (thisrr
->rec_version
== SSL2_VERSION
) {
453 &(RECORD_LAYER_get_packet(&s
->rlayer
)[SSL2_RT_HEADER_LENGTH
]);
456 &(RECORD_LAYER_get_packet(&s
->rlayer
)[SSL3_RT_HEADER_LENGTH
]);
460 * ok, we can now read from 's->packet' data into 'thisrr' thisrr->input
461 * points at thisrr->length bytes, which need to be copied into
462 * thisrr->data by either the decryption or by the decompression When
463 * the data is 'copied' into the thisrr->data buffer, thisrr->input will
464 * be pointed at the new buffer
468 * We now have - encrypted [ MAC [ compressed [ plain ] ] ]
469 * thisrr->length bytes of encrypted compressed stuff.
472 /* decrypt in place in 'thisrr->input' */
473 thisrr
->data
= thisrr
->input
;
474 thisrr
->orig_len
= thisrr
->length
;
476 /* Mark this record as not read by upper layers yet */
481 /* we have pulled in a full packet so zero things */
482 RECORD_LAYER_reset_packet_length(&s
->rlayer
);
483 RECORD_LAYER_clear_first_record(&s
->rlayer
);
484 } while (num_recs
< max_recs
485 && thisrr
->type
== SSL3_RT_APPLICATION_DATA
486 && SSL_USE_EXPLICIT_IV(s
)
487 && s
->enc_read_ctx
!= NULL
488 && (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s
->enc_read_ctx
))
489 & EVP_CIPH_FLAG_PIPELINE
)
490 && ssl3_record_app_data_waiting(s
));
493 && thisrr
->type
== SSL3_RT_CHANGE_CIPHER_SPEC
494 && (SSL_IS_TLS13(s
) || s
->hello_retry_request
!= SSL_HRR_NONE
)
495 && SSL_IS_FIRST_HANDSHAKE(s
)) {
497 * CCS messages must be exactly 1 byte long, containing the value 0x01
499 if (thisrr
->length
!= 1 || thisrr
->data
[0] != 0x01) {
500 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_F_SSL3_GET_RECORD
,
501 SSL_R_INVALID_CCS_MESSAGE
);
505 * CCS messages are ignored in TLSv1.3. We treat it like an empty
508 thisrr
->type
= SSL3_RT_HANDSHAKE
;
509 RECORD_LAYER_inc_empty_record_count(&s
->rlayer
);
510 if (RECORD_LAYER_get_empty_record_count(&s
->rlayer
)
511 > MAX_EMPTY_RECORDS
) {
512 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_F_SSL3_GET_RECORD
,
513 SSL_R_UNEXPECTED_CCS_MESSAGE
);
517 RECORD_LAYER_set_numrpipes(&s
->rlayer
, 1);
523 * KTLS reads full records. If there is any data left,
524 * then it is from before enabling ktls
526 if (BIO_get_ktls_recv(s
->rbio
) && !is_ktls_left
)
527 goto skip_decryption
;
530 * If in encrypt-then-mac mode calculate mac from encrypted record. All
531 * the details below are public so no timing details can leak.
533 if (SSL_READ_ETM(s
) && s
->read_hash
) {
535 /* TODO(size_t): convert this to do size_t properly */
536 imac_size
= EVP_MD_CTX_size(s
->read_hash
);
537 if (!ossl_assert(imac_size
>= 0 && imac_size
<= EVP_MAX_MD_SIZE
)) {
538 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_SSL3_GET_RECORD
,
542 mac_size
= (size_t)imac_size
;
543 for (j
= 0; j
< num_recs
; j
++) {
546 if (thisrr
->length
< mac_size
) {
547 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_SSL3_GET_RECORD
,
548 SSL_R_LENGTH_TOO_SHORT
);
551 thisrr
->length
-= mac_size
;
552 mac
= thisrr
->data
+ thisrr
->length
;
553 i
= s
->method
->ssl3_enc
->mac(s
, thisrr
, md
, 0 /* not send */ );
554 if (i
== 0 || CRYPTO_memcmp(md
, mac
, mac_size
) != 0) {
555 SSLfatal(s
, SSL_AD_BAD_RECORD_MAC
, SSL_F_SSL3_GET_RECORD
,
556 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC
);
562 first_rec_len
= rr
[0].length
;
564 enc_err
= s
->method
->ssl3_enc
->enc(s
, rr
, num_recs
, 0);
568 * 0: (in non-constant time) if the record is publicly invalid.
569 * 1: if the padding is valid
570 * -1: if the padding is invalid
573 if (ossl_statem_in_error(s
)) {
574 /* SSLfatal() already got called */
577 if (num_recs
== 1 && ossl_statem_skip_early_data(s
)) {
579 * Valid early_data that we cannot decrypt might fail here as
580 * publicly invalid. We treat it like an empty record.
585 if (!early_data_count_ok(s
, thisrr
->length
,
586 EARLY_DATA_CIPHERTEXT_OVERHEAD
, 0)) {
587 /* SSLfatal() already called */
593 RECORD_LAYER_set_numrpipes(&s
->rlayer
, 1);
594 RECORD_LAYER_reset_read_sequence(&s
->rlayer
);
597 SSLfatal(s
, SSL_AD_BAD_RECORD_MAC
, SSL_F_SSL3_GET_RECORD
,
598 SSL_R_BLOCK_CIPHER_PAD_IS_WRONG
);
601 OSSL_TRACE_BEGIN(TLS
) {
602 BIO_printf(trc_out
, "dec %lu\n", (unsigned long)rr
[0].length
);
603 BIO_dump_indent(trc_out
, rr
[0].data
, rr
[0].length
, 4);
604 } OSSL_TRACE_END(TLS
);
606 /* r->length is now the compressed data plus mac */
607 if ((sess
!= NULL
) &&
608 (s
->enc_read_ctx
!= NULL
) &&
609 (!SSL_READ_ETM(s
) && EVP_MD_CTX_md(s
->read_hash
) != NULL
)) {
610 /* s->read_hash != NULL => mac_size != -1 */
611 unsigned char *mac
= NULL
;
612 unsigned char mac_tmp
[EVP_MAX_MD_SIZE
];
614 mac_size
= EVP_MD_CTX_size(s
->read_hash
);
615 if (!ossl_assert(mac_size
<= EVP_MAX_MD_SIZE
)) {
616 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_SSL3_GET_RECORD
,
617 ERR_R_INTERNAL_ERROR
);
621 for (j
= 0; j
< num_recs
; j
++) {
624 * orig_len is the length of the record before any padding was
625 * removed. This is public information, as is the MAC in use,
626 * therefore we can safely process the record in a different amount
627 * of time if it's too short to possibly contain a MAC.
629 if (thisrr
->orig_len
< mac_size
||
630 /* CBC records must have a padding length byte too. */
631 (EVP_CIPHER_CTX_mode(s
->enc_read_ctx
) == EVP_CIPH_CBC_MODE
&&
632 thisrr
->orig_len
< mac_size
+ 1)) {
633 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_SSL3_GET_RECORD
,
634 SSL_R_LENGTH_TOO_SHORT
);
638 if (EVP_CIPHER_CTX_mode(s
->enc_read_ctx
) == EVP_CIPH_CBC_MODE
) {
640 * We update the length so that the TLS header bytes can be
641 * constructed correctly but we need to extract the MAC in
642 * constant time from within the record, without leaking the
643 * contents of the padding bytes.
646 if (!ssl3_cbc_copy_mac(mac_tmp
, thisrr
, mac_size
)) {
647 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_SSL3_GET_RECORD
,
648 ERR_R_INTERNAL_ERROR
);
651 thisrr
->length
-= mac_size
;
654 * In this case there's no padding, so |rec->orig_len| equals
655 * |rec->length| and we checked that there's enough bytes for
658 thisrr
->length
-= mac_size
;
659 mac
= &thisrr
->data
[thisrr
->length
];
662 i
= s
->method
->ssl3_enc
->mac(s
, thisrr
, md
, 0 /* not send */ );
663 if (i
== 0 || mac
== NULL
664 || CRYPTO_memcmp(md
, mac
, (size_t)mac_size
) != 0)
666 if (thisrr
->length
> SSL3_RT_MAX_COMPRESSED_LENGTH
+ mac_size
)
672 if (ossl_statem_in_error(s
)) {
673 /* We already called SSLfatal() */
676 if (num_recs
== 1 && ossl_statem_skip_early_data(s
)) {
678 * We assume this is unreadable early_data - we treat it like an
683 * The record length may have been modified by the mac check above
684 * so we use the previously saved value
686 if (!early_data_count_ok(s
, first_rec_len
,
687 EARLY_DATA_CIPHERTEXT_OVERHEAD
, 0)) {
688 /* SSLfatal() already called */
695 RECORD_LAYER_set_numrpipes(&s
->rlayer
, 1);
696 RECORD_LAYER_reset_read_sequence(&s
->rlayer
);
700 * A separate 'decryption_failed' alert was introduced with TLS 1.0,
701 * SSL 3.0 only has 'bad_record_mac'. But unless a decryption
702 * failure is directly visible from the ciphertext anyway, we should
703 * not reveal which kind of error occurred -- this might become
704 * visible to an attacker (e.g. via a logfile)
706 SSLfatal(s
, SSL_AD_BAD_RECORD_MAC
, SSL_F_SSL3_GET_RECORD
,
707 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC
);
713 for (j
= 0; j
< num_recs
; j
++) {
716 /* thisrr->length is now just compressed */
717 if (s
->expand
!= NULL
) {
718 if (thisrr
->length
> SSL3_RT_MAX_COMPRESSED_LENGTH
) {
719 SSLfatal(s
, SSL_AD_RECORD_OVERFLOW
, SSL_F_SSL3_GET_RECORD
,
720 SSL_R_COMPRESSED_LENGTH_TOO_LONG
);
723 if (!ssl3_do_uncompress(s
, thisrr
)) {
724 SSLfatal(s
, SSL_AD_DECOMPRESSION_FAILURE
, SSL_F_SSL3_GET_RECORD
,
725 SSL_R_BAD_DECOMPRESSION
);
731 && s
->enc_read_ctx
!= NULL
732 && thisrr
->type
!= SSL3_RT_ALERT
) {
735 if (thisrr
->length
== 0
736 || thisrr
->type
!= SSL3_RT_APPLICATION_DATA
) {
737 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_F_SSL3_GET_RECORD
,
738 SSL_R_BAD_RECORD_TYPE
);
742 /* Strip trailing padding */
743 for (end
= thisrr
->length
- 1; end
> 0 && thisrr
->data
[end
] == 0;
747 thisrr
->length
= end
;
748 thisrr
->type
= thisrr
->data
[end
];
749 if (thisrr
->type
!= SSL3_RT_APPLICATION_DATA
750 && thisrr
->type
!= SSL3_RT_ALERT
751 && thisrr
->type
!= SSL3_RT_HANDSHAKE
) {
752 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_F_SSL3_GET_RECORD
,
753 SSL_R_BAD_RECORD_TYPE
);
757 s
->msg_callback(0, s
->version
, SSL3_RT_INNER_CONTENT_TYPE
,
758 &thisrr
->data
[end
], 1, s
, s
->msg_callback_arg
);
762 * TLSv1.3 alert and handshake records are required to be non-zero in
766 && (thisrr
->type
== SSL3_RT_HANDSHAKE
767 || thisrr
->type
== SSL3_RT_ALERT
)
768 && thisrr
->length
== 0) {
769 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_F_SSL3_GET_RECORD
,
774 if (thisrr
->length
> SSL3_RT_MAX_PLAIN_LENGTH
&& !BIO_get_ktls_recv(s
->rbio
)) {
775 SSLfatal(s
, SSL_AD_RECORD_OVERFLOW
, SSL_F_SSL3_GET_RECORD
,
776 SSL_R_DATA_LENGTH_TOO_LONG
);
780 /* If received packet overflows current Max Fragment Length setting */
781 if (s
->session
!= NULL
&& USE_MAX_FRAGMENT_LENGTH_EXT(s
->session
)
782 && thisrr
->length
> GET_MAX_FRAGMENT_LENGTH(s
->session
)
783 && !BIO_get_ktls_recv(s
->rbio
)) {
784 SSLfatal(s
, SSL_AD_RECORD_OVERFLOW
, SSL_F_SSL3_GET_RECORD
,
785 SSL_R_DATA_LENGTH_TOO_LONG
);
791 * So at this point the following is true
792 * thisrr->type is the type of record
793 * thisrr->length == number of bytes in record
794 * thisrr->off == offset to first valid byte
795 * thisrr->data == where to take bytes from, increment after use :-).
798 /* just read a 0 length packet */
799 if (thisrr
->length
== 0) {
800 RECORD_LAYER_inc_empty_record_count(&s
->rlayer
);
801 if (RECORD_LAYER_get_empty_record_count(&s
->rlayer
)
802 > MAX_EMPTY_RECORDS
) {
803 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_F_SSL3_GET_RECORD
,
804 SSL_R_RECORD_TOO_SMALL
);
808 RECORD_LAYER_reset_empty_record_count(&s
->rlayer
);
812 if (s
->early_data_state
== SSL_EARLY_DATA_READING
) {
814 if (thisrr
->type
== SSL3_RT_APPLICATION_DATA
815 && !early_data_count_ok(s
, thisrr
->length
, 0, 0)) {
816 /* SSLfatal already called */
821 RECORD_LAYER_set_numrpipes(&s
->rlayer
, num_recs
);
825 int ssl3_do_uncompress(SSL
*ssl
, SSL3_RECORD
*rr
)
827 #ifndef OPENSSL_NO_COMP
830 if (rr
->comp
== NULL
) {
831 rr
->comp
= (unsigned char *)
832 OPENSSL_malloc(SSL3_RT_MAX_ENCRYPTED_LENGTH
);
834 if (rr
->comp
== NULL
)
837 /* TODO(size_t): Convert this call */
838 i
= COMP_expand_block(ssl
->expand
, rr
->comp
,
839 SSL3_RT_MAX_PLAIN_LENGTH
, rr
->data
, (int)rr
->length
);
849 int ssl3_do_compress(SSL
*ssl
, SSL3_RECORD
*wr
)
851 #ifndef OPENSSL_NO_COMP
854 /* TODO(size_t): Convert this call */
855 i
= COMP_compress_block(ssl
->compress
, wr
->data
,
856 (int)(wr
->length
+ SSL3_RT_MAX_COMPRESSED_OVERHEAD
),
857 wr
->input
, (int)wr
->length
);
863 wr
->input
= wr
->data
;
869 * ssl3_enc encrypts/decrypts |n_recs| records in |inrecs|. Will call
870 * SSLfatal() for internal errors, but not otherwise.
873 * 0: (in non-constant time) if the record is publicly invalid (i.e. too
875 * 1: if the record's padding is valid / the encryption was successful.
876 * -1: if the record's padding is invalid or, if sending, an internal error
879 int ssl3_enc(SSL
*s
, SSL3_RECORD
*inrecs
, size_t n_recs
, int sending
)
884 size_t bs
, mac_size
= 0;
886 const EVP_CIPHER
*enc
;
890 * We shouldn't ever be called with more than one record in the SSLv3 case
895 ds
= s
->enc_write_ctx
;
896 if (s
->enc_write_ctx
== NULL
)
899 enc
= EVP_CIPHER_CTX_cipher(s
->enc_write_ctx
);
901 ds
= s
->enc_read_ctx
;
902 if (s
->enc_read_ctx
== NULL
)
905 enc
= EVP_CIPHER_CTX_cipher(s
->enc_read_ctx
);
908 if ((s
->session
== NULL
) || (ds
== NULL
) || (enc
== NULL
)) {
909 memmove(rec
->data
, rec
->input
, rec
->length
);
910 rec
->input
= rec
->data
;
913 /* TODO(size_t): Convert this call */
914 bs
= EVP_CIPHER_CTX_block_size(ds
);
918 if ((bs
!= 1) && sending
) {
921 /* we need to add 'i-1' padding bytes */
924 * the last of these zero bytes will be overwritten with the
927 memset(&rec
->input
[rec
->length
], 0, i
);
929 rec
->input
[l
- 1] = (unsigned char)(i
- 1);
933 if (l
== 0 || l
% bs
!= 0)
935 /* otherwise, rec->length >= bs */
938 /* TODO(size_t): Convert this call */
939 if (EVP_Cipher(ds
, rec
->data
, rec
->input
, (unsigned int)l
) < 1)
942 if (EVP_MD_CTX_md(s
->read_hash
) != NULL
) {
943 /* TODO(size_t): convert me */
944 imac_size
= EVP_MD_CTX_size(s
->read_hash
);
946 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_SSL3_ENC
,
947 ERR_R_INTERNAL_ERROR
);
950 mac_size
= (size_t)imac_size
;
952 if ((bs
!= 1) && !sending
)
953 return ssl3_cbc_remove_padding(rec
, bs
, mac_size
);
958 #define MAX_PADDING 256
960 * tls1_enc encrypts/decrypts |n_recs| in |recs|. Will call SSLfatal() for
961 * internal errors, but not otherwise.
964 * 0: (in non-constant time) if the record is publicly invalid (i.e. too
966 * 1: if the record's padding is valid / the encryption was successful.
967 * -1: if the record's padding/AEAD-authenticator is invalid or, if sending,
968 * an internal error occurred.
970 int tls1_enc(SSL
*s
, SSL3_RECORD
*recs
, size_t n_recs
, int sending
)
973 size_t reclen
[SSL_MAX_PIPELINES
];
974 unsigned char buf
[SSL_MAX_PIPELINES
][EVP_AEAD_TLS1_AAD_LEN
];
975 int i
, pad
= 0, ret
, tmpr
;
976 size_t bs
, mac_size
= 0, ctr
, padnum
, loop
;
977 unsigned char padval
;
979 const EVP_CIPHER
*enc
;
982 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS1_ENC
,
983 ERR_R_INTERNAL_ERROR
);
988 if (EVP_MD_CTX_md(s
->write_hash
)) {
989 int n
= EVP_MD_CTX_size(s
->write_hash
);
990 if (!ossl_assert(n
>= 0)) {
991 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS1_ENC
,
992 ERR_R_INTERNAL_ERROR
);
996 ds
= s
->enc_write_ctx
;
997 if (s
->enc_write_ctx
== NULL
)
1001 enc
= EVP_CIPHER_CTX_cipher(s
->enc_write_ctx
);
1002 /* For TLSv1.1 and later explicit IV */
1003 if (SSL_USE_EXPLICIT_IV(s
)
1004 && EVP_CIPHER_mode(enc
) == EVP_CIPH_CBC_MODE
)
1005 ivlen
= EVP_CIPHER_iv_length(enc
);
1009 for (ctr
= 0; ctr
< n_recs
; ctr
++) {
1010 if (recs
[ctr
].data
!= recs
[ctr
].input
) {
1012 * we can't write into the input stream: Can this ever
1015 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS1_ENC
,
1016 ERR_R_INTERNAL_ERROR
);
1018 } else if (RAND_bytes_ex(s
->ctx
->libctx
, recs
[ctr
].input
,
1020 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS1_ENC
,
1021 ERR_R_INTERNAL_ERROR
);
1028 if (EVP_MD_CTX_md(s
->read_hash
)) {
1029 int n
= EVP_MD_CTX_size(s
->read_hash
);
1030 if (!ossl_assert(n
>= 0)) {
1031 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS1_ENC
,
1032 ERR_R_INTERNAL_ERROR
);
1036 ds
= s
->enc_read_ctx
;
1037 if (s
->enc_read_ctx
== NULL
)
1040 enc
= EVP_CIPHER_CTX_cipher(s
->enc_read_ctx
);
1043 if ((s
->session
== NULL
) || (ds
== NULL
) || (enc
== NULL
)) {
1044 for (ctr
= 0; ctr
< n_recs
; ctr
++) {
1045 memmove(recs
[ctr
].data
, recs
[ctr
].input
, recs
[ctr
].length
);
1046 recs
[ctr
].input
= recs
[ctr
].data
;
1050 bs
= EVP_CIPHER_block_size(EVP_CIPHER_CTX_cipher(ds
));
1053 if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds
))
1054 & EVP_CIPH_FLAG_PIPELINE
)) {
1056 * We shouldn't have been called with pipeline data if the
1057 * cipher doesn't support pipelining
1059 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS1_ENC
,
1060 SSL_R_PIPELINE_FAILURE
);
1064 for (ctr
= 0; ctr
< n_recs
; ctr
++) {
1065 reclen
[ctr
] = recs
[ctr
].length
;
1067 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds
))
1068 & EVP_CIPH_FLAG_AEAD_CIPHER
) {
1071 seq
= sending
? RECORD_LAYER_get_write_sequence(&s
->rlayer
)
1072 : RECORD_LAYER_get_read_sequence(&s
->rlayer
);
1074 if (SSL_IS_DTLS(s
)) {
1075 /* DTLS does not support pipelining */
1076 unsigned char dtlsseq
[9], *p
= dtlsseq
;
1078 s2n(sending
? DTLS_RECORD_LAYER_get_w_epoch(&s
->rlayer
) :
1079 DTLS_RECORD_LAYER_get_r_epoch(&s
->rlayer
), p
);
1080 memcpy(p
, &seq
[2], 6);
1081 memcpy(buf
[ctr
], dtlsseq
, 8);
1083 memcpy(buf
[ctr
], seq
, 8);
1084 for (i
= 7; i
>= 0; i
--) { /* increment */
1091 buf
[ctr
][8] = recs
[ctr
].type
;
1092 buf
[ctr
][9] = (unsigned char)(s
->version
>> 8);
1093 buf
[ctr
][10] = (unsigned char)(s
->version
);
1094 buf
[ctr
][11] = (unsigned char)(recs
[ctr
].length
>> 8);
1095 buf
[ctr
][12] = (unsigned char)(recs
[ctr
].length
& 0xff);
1096 pad
= EVP_CIPHER_CTX_ctrl(ds
, EVP_CTRL_AEAD_TLS1_AAD
,
1097 EVP_AEAD_TLS1_AAD_LEN
, buf
[ctr
]);
1099 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS1_ENC
,
1100 ERR_R_INTERNAL_ERROR
);
1106 recs
[ctr
].length
+= pad
;
1109 } else if ((bs
!= 1) && sending
) {
1110 padnum
= bs
- (reclen
[ctr
] % bs
);
1112 /* Add weird padding of up to 256 bytes */
1114 if (padnum
> MAX_PADDING
) {
1115 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS1_ENC
,
1116 ERR_R_INTERNAL_ERROR
);
1119 /* we need to add 'padnum' padding bytes of value padval */
1120 padval
= (unsigned char)(padnum
- 1);
1121 for (loop
= reclen
[ctr
]; loop
< reclen
[ctr
] + padnum
; loop
++)
1122 recs
[ctr
].input
[loop
] = padval
;
1123 reclen
[ctr
] += padnum
;
1124 recs
[ctr
].length
+= padnum
;
1128 if (reclen
[ctr
] == 0 || reclen
[ctr
] % bs
!= 0)
1133 unsigned char *data
[SSL_MAX_PIPELINES
];
1135 /* Set the output buffers */
1136 for (ctr
= 0; ctr
< n_recs
; ctr
++) {
1137 data
[ctr
] = recs
[ctr
].data
;
1139 if (EVP_CIPHER_CTX_ctrl(ds
, EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS
,
1140 (int)n_recs
, data
) <= 0) {
1141 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS1_ENC
,
1142 SSL_R_PIPELINE_FAILURE
);
1145 /* Set the input buffers */
1146 for (ctr
= 0; ctr
< n_recs
; ctr
++) {
1147 data
[ctr
] = recs
[ctr
].input
;
1149 if (EVP_CIPHER_CTX_ctrl(ds
, EVP_CTRL_SET_PIPELINE_INPUT_BUFS
,
1150 (int)n_recs
, data
) <= 0
1151 || EVP_CIPHER_CTX_ctrl(ds
, EVP_CTRL_SET_PIPELINE_INPUT_LENS
,
1152 (int)n_recs
, reclen
) <= 0) {
1153 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS1_ENC
,
1154 SSL_R_PIPELINE_FAILURE
);
1159 /* TODO(size_t): Convert this call */
1160 tmpr
= EVP_Cipher(ds
, recs
[0].data
, recs
[0].input
,
1161 (unsigned int)reclen
[0]);
1162 if ((EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds
))
1163 & EVP_CIPH_FLAG_CUSTOM_CIPHER
)
1166 return -1; /* AEAD can fail to verify MAC */
1169 if (EVP_CIPHER_mode(enc
) == EVP_CIPH_GCM_MODE
) {
1170 for (ctr
= 0; ctr
< n_recs
; ctr
++) {
1171 recs
[ctr
].data
+= EVP_GCM_TLS_EXPLICIT_IV_LEN
;
1172 recs
[ctr
].input
+= EVP_GCM_TLS_EXPLICIT_IV_LEN
;
1173 recs
[ctr
].length
-= EVP_GCM_TLS_EXPLICIT_IV_LEN
;
1175 } else if (EVP_CIPHER_mode(enc
) == EVP_CIPH_CCM_MODE
) {
1176 for (ctr
= 0; ctr
< n_recs
; ctr
++) {
1177 recs
[ctr
].data
+= EVP_CCM_TLS_EXPLICIT_IV_LEN
;
1178 recs
[ctr
].input
+= EVP_CCM_TLS_EXPLICIT_IV_LEN
;
1179 recs
[ctr
].length
-= EVP_CCM_TLS_EXPLICIT_IV_LEN
;
1185 if (!SSL_READ_ETM(s
) && EVP_MD_CTX_md(s
->read_hash
) != NULL
) {
1186 imac_size
= EVP_MD_CTX_size(s
->read_hash
);
1187 if (imac_size
< 0) {
1188 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS1_ENC
,
1189 ERR_R_INTERNAL_ERROR
);
1192 mac_size
= (size_t)imac_size
;
1194 if ((bs
!= 1) && !sending
) {
1196 for (ctr
= 0; ctr
< n_recs
; ctr
++) {
1197 tmpret
= tls1_cbc_remove_padding(s
, &recs
[ctr
], bs
, mac_size
);
1199 * If tmpret == 0 then this means publicly invalid so we can
1200 * short circuit things here. Otherwise we must respect constant
1205 ret
= constant_time_select_int(constant_time_eq_int(tmpret
, 1),
1209 if (pad
&& !sending
) {
1210 for (ctr
= 0; ctr
< n_recs
; ctr
++) {
1211 recs
[ctr
].length
-= pad
;
1218 int n_ssl3_mac(SSL
*ssl
, SSL3_RECORD
*rec
, unsigned char *md
, int sending
)
1220 unsigned char *mac_sec
, *seq
;
1221 const EVP_MD_CTX
*hash
;
1222 unsigned char *p
, rec_char
;
1228 mac_sec
= &(ssl
->s3
.write_mac_secret
[0]);
1229 seq
= RECORD_LAYER_get_write_sequence(&ssl
->rlayer
);
1230 hash
= ssl
->write_hash
;
1232 mac_sec
= &(ssl
->s3
.read_mac_secret
[0]);
1233 seq
= RECORD_LAYER_get_read_sequence(&ssl
->rlayer
);
1234 hash
= ssl
->read_hash
;
1237 t
= EVP_MD_CTX_size(hash
);
1241 npad
= (48 / md_size
) * md_size
;
1244 EVP_CIPHER_CTX_mode(ssl
->enc_read_ctx
) == EVP_CIPH_CBC_MODE
&&
1245 ssl3_cbc_record_digest_supported(hash
)) {
1247 * This is a CBC-encrypted record. We must avoid leaking any
1248 * timing-side channel information about how many blocks of data we
1249 * are hashing because that gives an attacker a timing-oracle.
1253 * npad is, at most, 48 bytes and that's with MD5:
1254 * 16 + 48 + 8 (sequence bytes) + 1 + 2 = 75.
1256 * With SHA-1 (the largest hash speced for SSLv3) the hash size
1257 * goes up 4, but npad goes down by 8, resulting in a smaller
1260 unsigned char header
[75];
1262 memcpy(header
+ j
, mac_sec
, md_size
);
1264 memcpy(header
+ j
, ssl3_pad_1
, npad
);
1266 memcpy(header
+ j
, seq
, 8);
1268 header
[j
++] = rec
->type
;
1269 header
[j
++] = (unsigned char)(rec
->length
>> 8);
1270 header
[j
++] = (unsigned char)(rec
->length
& 0xff);
1272 /* Final param == is SSLv3 */
1273 if (ssl3_cbc_digest_record(hash
,
1276 rec
->length
+ md_size
, rec
->orig_len
,
1277 mac_sec
, md_size
, 1) <= 0)
1280 unsigned int md_size_u
;
1281 /* Chop the digest off the end :-) */
1282 EVP_MD_CTX
*md_ctx
= EVP_MD_CTX_new();
1287 rec_char
= rec
->type
;
1289 s2n(rec
->length
, p
);
1290 if (EVP_MD_CTX_copy_ex(md_ctx
, hash
) <= 0
1291 || EVP_DigestUpdate(md_ctx
, mac_sec
, md_size
) <= 0
1292 || EVP_DigestUpdate(md_ctx
, ssl3_pad_1
, npad
) <= 0
1293 || EVP_DigestUpdate(md_ctx
, seq
, 8) <= 0
1294 || EVP_DigestUpdate(md_ctx
, &rec_char
, 1) <= 0
1295 || EVP_DigestUpdate(md_ctx
, md
, 2) <= 0
1296 || EVP_DigestUpdate(md_ctx
, rec
->input
, rec
->length
) <= 0
1297 || EVP_DigestFinal_ex(md_ctx
, md
, NULL
) <= 0
1298 || EVP_MD_CTX_copy_ex(md_ctx
, hash
) <= 0
1299 || EVP_DigestUpdate(md_ctx
, mac_sec
, md_size
) <= 0
1300 || EVP_DigestUpdate(md_ctx
, ssl3_pad_2
, npad
) <= 0
1301 || EVP_DigestUpdate(md_ctx
, md
, md_size
) <= 0
1302 || EVP_DigestFinal_ex(md_ctx
, md
, &md_size_u
) <= 0) {
1303 EVP_MD_CTX_free(md_ctx
);
1307 EVP_MD_CTX_free(md_ctx
);
1310 ssl3_record_sequence_update(seq
);
1314 int tls1_mac(SSL
*ssl
, SSL3_RECORD
*rec
, unsigned char *md
, int sending
)
1320 EVP_MD_CTX
*hmac
= NULL
, *mac_ctx
;
1321 unsigned char header
[13];
1322 int stream_mac
= (sending
? (ssl
->mac_flags
& SSL_MAC_FLAG_WRITE_MAC_STREAM
)
1323 : (ssl
->mac_flags
& SSL_MAC_FLAG_READ_MAC_STREAM
));
1327 seq
= RECORD_LAYER_get_write_sequence(&ssl
->rlayer
);
1328 hash
= ssl
->write_hash
;
1330 seq
= RECORD_LAYER_get_read_sequence(&ssl
->rlayer
);
1331 hash
= ssl
->read_hash
;
1334 t
= EVP_MD_CTX_size(hash
);
1335 if (!ossl_assert(t
>= 0))
1339 /* I should fix this up TLS TLS TLS TLS TLS XXXXXXXX */
1343 hmac
= EVP_MD_CTX_new();
1344 if (hmac
== NULL
|| !EVP_MD_CTX_copy(hmac
, hash
)) {
1345 EVP_MD_CTX_free(hmac
);
1351 if (SSL_IS_DTLS(ssl
)) {
1352 unsigned char dtlsseq
[8], *p
= dtlsseq
;
1354 s2n(sending
? DTLS_RECORD_LAYER_get_w_epoch(&ssl
->rlayer
) :
1355 DTLS_RECORD_LAYER_get_r_epoch(&ssl
->rlayer
), p
);
1356 memcpy(p
, &seq
[2], 6);
1358 memcpy(header
, dtlsseq
, 8);
1360 memcpy(header
, seq
, 8);
1362 header
[8] = rec
->type
;
1363 header
[9] = (unsigned char)(ssl
->version
>> 8);
1364 header
[10] = (unsigned char)(ssl
->version
);
1365 header
[11] = (unsigned char)(rec
->length
>> 8);
1366 header
[12] = (unsigned char)(rec
->length
& 0xff);
1368 if (!sending
&& !SSL_READ_ETM(ssl
) &&
1369 EVP_CIPHER_CTX_mode(ssl
->enc_read_ctx
) == EVP_CIPH_CBC_MODE
&&
1370 ssl3_cbc_record_digest_supported(mac_ctx
)) {
1372 * This is a CBC-encrypted record. We must avoid leaking any
1373 * timing-side channel information about how many blocks of data we
1374 * are hashing because that gives an attacker a timing-oracle.
1376 /* Final param == not SSLv3 */
1377 if (ssl3_cbc_digest_record(mac_ctx
,
1380 rec
->length
+ md_size
, rec
->orig_len
,
1381 ssl
->s3
.read_mac_secret
,
1382 ssl
->s3
.read_mac_secret_size
, 0) <= 0) {
1383 EVP_MD_CTX_free(hmac
);
1387 /* TODO(size_t): Convert these calls */
1388 if (EVP_DigestSignUpdate(mac_ctx
, header
, sizeof(header
)) <= 0
1389 || EVP_DigestSignUpdate(mac_ctx
, rec
->input
, rec
->length
) <= 0
1390 || EVP_DigestSignFinal(mac_ctx
, md
, &md_size
) <= 0) {
1391 EVP_MD_CTX_free(hmac
);
1396 EVP_MD_CTX_free(hmac
);
1398 OSSL_TRACE_BEGIN(TLS
) {
1399 BIO_printf(trc_out
, "seq:\n");
1400 BIO_dump_indent(trc_out
, seq
, 8, 4);
1401 BIO_printf(trc_out
, "rec:\n");
1402 BIO_dump_indent(trc_out
, rec
->data
, rec
->length
, 4);
1403 } OSSL_TRACE_END(TLS
);
1405 if (!SSL_IS_DTLS(ssl
)) {
1406 for (i
= 7; i
>= 0; i
--) {
1412 OSSL_TRACE_BEGIN(TLS
) {
1413 BIO_printf(trc_out
, "md:\n");
1414 BIO_dump_indent(trc_out
, md
, md_size
, 4);
1415 } OSSL_TRACE_END(TLS
);
1420 * ssl3_cbc_remove_padding removes padding from the decrypted, SSLv3, CBC
1421 * record in |rec| by updating |rec->length| in constant time.
1423 * block_size: the block size of the cipher used to encrypt the record.
1425 * 0: (in non-constant time) if the record is publicly invalid.
1426 * 1: if the padding was valid
1429 int ssl3_cbc_remove_padding(SSL3_RECORD
*rec
,
1430 size_t block_size
, size_t mac_size
)
1432 size_t padding_length
;
1434 const size_t overhead
= 1 /* padding length byte */ + mac_size
;
1437 * These lengths are all public so we can test them in non-constant time.
1439 if (overhead
> rec
->length
)
1442 padding_length
= rec
->data
[rec
->length
- 1];
1443 good
= constant_time_ge_s(rec
->length
, padding_length
+ overhead
);
1444 /* SSLv3 requires that the padding is minimal. */
1445 good
&= constant_time_ge_s(block_size
, padding_length
+ 1);
1446 rec
->length
-= good
& (padding_length
+ 1);
1447 return constant_time_select_int_s(good
, 1, -1);
1451 * tls1_cbc_remove_padding removes the CBC padding from the decrypted, TLS, CBC
1452 * record in |rec| in constant time and returns 1 if the padding is valid and
1453 * -1 otherwise. It also removes any explicit IV from the start of the record
1454 * without leaking any timing about whether there was enough space after the
1455 * padding was removed.
1457 * block_size: the block size of the cipher used to encrypt the record.
1459 * 0: (in non-constant time) if the record is publicly invalid.
1460 * 1: if the padding was valid
1463 int tls1_cbc_remove_padding(const SSL
*s
,
1465 size_t block_size
, size_t mac_size
)
1468 size_t padding_length
, to_check
, i
;
1469 const size_t overhead
= 1 /* padding length byte */ + mac_size
;
1470 /* Check if version requires explicit IV */
1471 if (SSL_USE_EXPLICIT_IV(s
)) {
1473 * These lengths are all public so we can test them in non-constant
1476 if (overhead
+ block_size
> rec
->length
)
1478 /* We can now safely skip explicit IV */
1479 rec
->data
+= block_size
;
1480 rec
->input
+= block_size
;
1481 rec
->length
-= block_size
;
1482 rec
->orig_len
-= block_size
;
1483 } else if (overhead
> rec
->length
)
1486 padding_length
= rec
->data
[rec
->length
- 1];
1488 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s
->enc_read_ctx
)) &
1489 EVP_CIPH_FLAG_AEAD_CIPHER
) {
1490 /* padding is already verified */
1491 rec
->length
-= padding_length
+ 1;
1495 good
= constant_time_ge_s(rec
->length
, overhead
+ padding_length
);
1497 * The padding consists of a length byte at the end of the record and
1498 * then that many bytes of padding, all with the same value as the length
1499 * byte. Thus, with the length byte included, there are i+1 bytes of
1500 * padding. We can't check just |padding_length+1| bytes because that
1501 * leaks decrypted information. Therefore we always have to check the
1502 * maximum amount of padding possible. (Again, the length of the record
1503 * is public information so we can use it.)
1505 to_check
= 256; /* maximum amount of padding, inc length byte. */
1506 if (to_check
> rec
->length
)
1507 to_check
= rec
->length
;
1509 for (i
= 0; i
< to_check
; i
++) {
1510 unsigned char mask
= constant_time_ge_8_s(padding_length
, i
);
1511 unsigned char b
= rec
->data
[rec
->length
- 1 - i
];
1513 * The final |padding_length+1| bytes should all have the value
1514 * |padding_length|. Therefore the XOR should be zero.
1516 good
&= ~(mask
& (padding_length
^ b
));
1520 * If any of the final |padding_length+1| bytes had the wrong value, one
1521 * or more of the lower eight bits of |good| will be cleared.
1523 good
= constant_time_eq_s(0xff, good
& 0xff);
1524 rec
->length
-= good
& (padding_length
+ 1);
1526 return constant_time_select_int_s(good
, 1, -1);
1530 * ssl3_cbc_copy_mac copies |md_size| bytes from the end of |rec| to |out| in
1531 * constant time (independent of the concrete value of rec->length, which may
1532 * vary within a 256-byte window).
1534 * ssl3_cbc_remove_padding or tls1_cbc_remove_padding must be called prior to
1538 * rec->orig_len >= md_size
1539 * md_size <= EVP_MAX_MD_SIZE
1541 * If CBC_MAC_ROTATE_IN_PLACE is defined then the rotation is performed with
1542 * variable accesses in a 64-byte-aligned buffer. Assuming that this fits into
1543 * a single or pair of cache-lines, then the variable memory accesses don't
1544 * actually affect the timing. CPUs with smaller cache-lines [if any] are
1545 * not multi-core and are not considered vulnerable to cache-timing attacks.
1547 #define CBC_MAC_ROTATE_IN_PLACE
1549 int ssl3_cbc_copy_mac(unsigned char *out
,
1550 const SSL3_RECORD
*rec
, size_t md_size
)
1552 #if defined(CBC_MAC_ROTATE_IN_PLACE)
1553 unsigned char rotated_mac_buf
[64 + EVP_MAX_MD_SIZE
];
1554 unsigned char *rotated_mac
;
1556 unsigned char rotated_mac
[EVP_MAX_MD_SIZE
];
1560 * mac_end is the index of |rec->data| just after the end of the MAC.
1562 size_t mac_end
= rec
->length
;
1563 size_t mac_start
= mac_end
- md_size
;
1566 * scan_start contains the number of bytes that we can ignore because the
1567 * MAC's position can only vary by 255 bytes.
1569 size_t scan_start
= 0;
1571 size_t rotate_offset
;
1573 if (!ossl_assert(rec
->orig_len
>= md_size
1574 && md_size
<= EVP_MAX_MD_SIZE
))
1577 #if defined(CBC_MAC_ROTATE_IN_PLACE)
1578 rotated_mac
= rotated_mac_buf
+ ((0 - (size_t)rotated_mac_buf
) & 63);
1581 /* This information is public so it's safe to branch based on it. */
1582 if (rec
->orig_len
> md_size
+ 255 + 1)
1583 scan_start
= rec
->orig_len
- (md_size
+ 255 + 1);
1587 memset(rotated_mac
, 0, md_size
);
1588 for (i
= scan_start
, j
= 0; i
< rec
->orig_len
; i
++) {
1589 size_t mac_started
= constant_time_eq_s(i
, mac_start
);
1590 size_t mac_ended
= constant_time_lt_s(i
, mac_end
);
1591 unsigned char b
= rec
->data
[i
];
1593 in_mac
|= mac_started
;
1594 in_mac
&= mac_ended
;
1595 rotate_offset
|= j
& mac_started
;
1596 rotated_mac
[j
++] |= b
& in_mac
;
1597 j
&= constant_time_lt_s(j
, md_size
);
1600 /* Now rotate the MAC */
1601 #if defined(CBC_MAC_ROTATE_IN_PLACE)
1603 for (i
= 0; i
< md_size
; i
++) {
1604 /* in case cache-line is 32 bytes, touch second line */
1605 ((volatile unsigned char *)rotated_mac
)[rotate_offset
^ 32];
1606 out
[j
++] = rotated_mac
[rotate_offset
++];
1607 rotate_offset
&= constant_time_lt_s(rotate_offset
, md_size
);
1610 memset(out
, 0, md_size
);
1611 rotate_offset
= md_size
- rotate_offset
;
1612 rotate_offset
&= constant_time_lt_s(rotate_offset
, md_size
);
1613 for (i
= 0; i
< md_size
; i
++) {
1614 for (j
= 0; j
< md_size
; j
++)
1615 out
[j
] |= rotated_mac
[i
] & constant_time_eq_8_s(j
, rotate_offset
);
1617 rotate_offset
&= constant_time_lt_s(rotate_offset
, md_size
);
1624 int dtls1_process_record(SSL
*s
, DTLS1_BITMAP
*bitmap
)
1632 unsigned char md
[EVP_MAX_MD_SIZE
];
1634 rr
= RECORD_LAYER_get_rrec(&s
->rlayer
);
1638 * At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length,
1639 * and we have that many bytes in s->packet
1641 rr
->input
= &(RECORD_LAYER_get_packet(&s
->rlayer
)[DTLS1_RT_HEADER_LENGTH
]);
1644 * ok, we can now read from 's->packet' data into 'rr' rr->input points
1645 * at rr->length bytes, which need to be copied into rr->data by either
1646 * the decryption or by the decompression When the data is 'copied' into
1647 * the rr->data buffer, rr->input will be pointed at the new buffer
1651 * We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
1652 * bytes of encrypted compressed stuff.
1655 /* check is not needed I believe */
1656 if (rr
->length
> SSL3_RT_MAX_ENCRYPTED_LENGTH
) {
1657 SSLfatal(s
, SSL_AD_RECORD_OVERFLOW
, SSL_F_DTLS1_PROCESS_RECORD
,
1658 SSL_R_ENCRYPTED_LENGTH_TOO_LONG
);
1662 /* decrypt in place in 'rr->input' */
1663 rr
->data
= rr
->input
;
1664 rr
->orig_len
= rr
->length
;
1666 if (SSL_READ_ETM(s
) && s
->read_hash
) {
1668 mac_size
= EVP_MD_CTX_size(s
->read_hash
);
1669 if (!ossl_assert(mac_size
<= EVP_MAX_MD_SIZE
)) {
1670 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_DTLS1_PROCESS_RECORD
,
1671 ERR_R_INTERNAL_ERROR
);
1674 if (rr
->orig_len
< mac_size
) {
1675 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_DTLS1_PROCESS_RECORD
,
1676 SSL_R_LENGTH_TOO_SHORT
);
1679 rr
->length
-= mac_size
;
1680 mac
= rr
->data
+ rr
->length
;
1681 i
= s
->method
->ssl3_enc
->mac(s
, rr
, md
, 0 /* not send */ );
1682 if (i
== 0 || CRYPTO_memcmp(md
, mac
, (size_t)mac_size
) != 0) {
1683 SSLfatal(s
, SSL_AD_BAD_RECORD_MAC
, SSL_F_DTLS1_PROCESS_RECORD
,
1684 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC
);
1689 enc_err
= s
->method
->ssl3_enc
->enc(s
, rr
, 1, 0);
1692 * 0: (in non-constant time) if the record is publicly invalid.
1693 * 1: if the padding is valid
1694 * -1: if the padding is invalid
1697 if (ossl_statem_in_error(s
)) {
1698 /* SSLfatal() got called */
1701 /* For DTLS we simply ignore bad packets. */
1703 RECORD_LAYER_reset_packet_length(&s
->rlayer
);
1706 OSSL_TRACE_BEGIN(TLS
) {
1707 BIO_printf(trc_out
, "dec %zd\n", rr
->length
);
1708 BIO_dump_indent(trc_out
, rr
->data
, rr
->length
, 4);
1709 } OSSL_TRACE_END(TLS
);
1711 /* r->length is now the compressed data plus mac */
1712 if ((sess
!= NULL
) && !SSL_READ_ETM(s
) &&
1713 (s
->enc_read_ctx
!= NULL
) && (EVP_MD_CTX_md(s
->read_hash
) != NULL
)) {
1714 /* s->read_hash != NULL => mac_size != -1 */
1715 unsigned char *mac
= NULL
;
1716 unsigned char mac_tmp
[EVP_MAX_MD_SIZE
];
1718 /* TODO(size_t): Convert this to do size_t properly */
1719 imac_size
= EVP_MD_CTX_size(s
->read_hash
);
1720 if (imac_size
< 0) {
1721 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_DTLS1_PROCESS_RECORD
,
1725 mac_size
= (size_t)imac_size
;
1726 if (!ossl_assert(mac_size
<= EVP_MAX_MD_SIZE
)) {
1727 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_DTLS1_PROCESS_RECORD
,
1728 ERR_R_INTERNAL_ERROR
);
1733 * orig_len is the length of the record before any padding was
1734 * removed. This is public information, as is the MAC in use,
1735 * therefore we can safely process the record in a different amount
1736 * of time if it's too short to possibly contain a MAC.
1738 if (rr
->orig_len
< mac_size
||
1739 /* CBC records must have a padding length byte too. */
1740 (EVP_CIPHER_CTX_mode(s
->enc_read_ctx
) == EVP_CIPH_CBC_MODE
&&
1741 rr
->orig_len
< mac_size
+ 1)) {
1742 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_F_DTLS1_PROCESS_RECORD
,
1743 SSL_R_LENGTH_TOO_SHORT
);
1747 if (EVP_CIPHER_CTX_mode(s
->enc_read_ctx
) == EVP_CIPH_CBC_MODE
) {
1749 * We update the length so that the TLS header bytes can be
1750 * constructed correctly but we need to extract the MAC in
1751 * constant time from within the record, without leaking the
1752 * contents of the padding bytes.
1755 if (!ssl3_cbc_copy_mac(mac_tmp
, rr
, mac_size
)) {
1756 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_DTLS1_PROCESS_RECORD
,
1757 ERR_R_INTERNAL_ERROR
);
1760 rr
->length
-= mac_size
;
1763 * In this case there's no padding, so |rec->orig_len| equals
1764 * |rec->length| and we checked that there's enough bytes for
1767 rr
->length
-= mac_size
;
1768 mac
= &rr
->data
[rr
->length
];
1771 i
= s
->method
->ssl3_enc
->mac(s
, rr
, md
, 0 /* not send */ );
1772 if (i
== 0 || mac
== NULL
1773 || CRYPTO_memcmp(md
, mac
, mac_size
) != 0)
1775 if (rr
->length
> SSL3_RT_MAX_COMPRESSED_LENGTH
+ mac_size
)
1780 /* decryption failed, silently discard message */
1782 RECORD_LAYER_reset_packet_length(&s
->rlayer
);
1786 /* r->length is now just compressed */
1787 if (s
->expand
!= NULL
) {
1788 if (rr
->length
> SSL3_RT_MAX_COMPRESSED_LENGTH
) {
1789 SSLfatal(s
, SSL_AD_RECORD_OVERFLOW
, SSL_F_DTLS1_PROCESS_RECORD
,
1790 SSL_R_COMPRESSED_LENGTH_TOO_LONG
);
1793 if (!ssl3_do_uncompress(s
, rr
)) {
1794 SSLfatal(s
, SSL_AD_DECOMPRESSION_FAILURE
,
1795 SSL_F_DTLS1_PROCESS_RECORD
, SSL_R_BAD_DECOMPRESSION
);
1800 if (rr
->length
> SSL3_RT_MAX_PLAIN_LENGTH
) {
1801 SSLfatal(s
, SSL_AD_RECORD_OVERFLOW
, SSL_F_DTLS1_PROCESS_RECORD
,
1802 SSL_R_DATA_LENGTH_TOO_LONG
);
1808 * So at this point the following is true
1809 * ssl->s3.rrec.type is the type of record
1810 * ssl->s3.rrec.length == number of bytes in record
1811 * ssl->s3.rrec.off == offset to first valid byte
1812 * ssl->s3.rrec.data == where to take bytes from, increment
1816 /* we have pulled in a full packet so zero things */
1817 RECORD_LAYER_reset_packet_length(&s
->rlayer
);
1819 /* Mark receipt of record. */
1820 dtls1_record_bitmap_update(s
, bitmap
);
1826 * Retrieve a buffered record that belongs to the current epoch, i.e. processed
1828 #define dtls1_get_processed_record(s) \
1829 dtls1_retrieve_buffered_record((s), \
1830 &(DTLS_RECORD_LAYER_get_processed_rcds(&s->rlayer)))
1833 * Call this to get a new input record.
1834 * It will return <= 0 if more data is needed, normally due to an error
1835 * or non-blocking IO.
1836 * When it finishes, one packet has been decoded and can be found in
1837 * ssl->s3.rrec.type - is the type of record
1838 * ssl->s3.rrec.data - data
1839 * ssl->s3.rrec.length - number of bytes
1841 /* used only by dtls1_read_bytes */
1842 int dtls1_get_record(SSL
*s
)
1844 int ssl_major
, ssl_minor
;
1848 unsigned char *p
= NULL
;
1849 unsigned short version
;
1850 DTLS1_BITMAP
*bitmap
;
1851 unsigned int is_next_epoch
;
1853 rr
= RECORD_LAYER_get_rrec(&s
->rlayer
);
1857 * The epoch may have changed. If so, process all the pending records.
1858 * This is a non-blocking operation.
1860 if (!dtls1_process_buffered_records(s
)) {
1861 /* SSLfatal() already called */
1865 /* if we're renegotiating, then there may be buffered records */
1866 if (dtls1_get_processed_record(s
))
1869 /* get something from the wire */
1871 /* check if we have the header */
1872 if ((RECORD_LAYER_get_rstate(&s
->rlayer
) != SSL_ST_READ_BODY
) ||
1873 (RECORD_LAYER_get_packet_length(&s
->rlayer
) < DTLS1_RT_HEADER_LENGTH
)) {
1874 rret
= ssl3_read_n(s
, DTLS1_RT_HEADER_LENGTH
,
1875 SSL3_BUFFER_get_len(&s
->rlayer
.rbuf
), 0, 1, &n
);
1876 /* read timeout is handled by dtls1_read_bytes */
1878 /* SSLfatal() already called if appropriate */
1879 return rret
; /* error or non-blocking */
1882 /* this packet contained a partial record, dump it */
1883 if (RECORD_LAYER_get_packet_length(&s
->rlayer
) !=
1884 DTLS1_RT_HEADER_LENGTH
) {
1885 RECORD_LAYER_reset_packet_length(&s
->rlayer
);
1889 RECORD_LAYER_set_rstate(&s
->rlayer
, SSL_ST_READ_BODY
);
1891 p
= RECORD_LAYER_get_packet(&s
->rlayer
);
1893 if (s
->msg_callback
)
1894 s
->msg_callback(0, 0, SSL3_RT_HEADER
, p
, DTLS1_RT_HEADER_LENGTH
,
1895 s
, s
->msg_callback_arg
);
1897 /* Pull apart the header into the DTLS1_RECORD */
1901 version
= (ssl_major
<< 8) | ssl_minor
;
1903 /* sequence number is 64 bits, with top 2 bytes = epoch */
1906 memcpy(&(RECORD_LAYER_get_read_sequence(&s
->rlayer
)[2]), p
, 6);
1913 * Lets check the version. We tolerate alerts that don't have the exact
1914 * version number (e.g. because of protocol version errors)
1916 if (!s
->first_packet
&& rr
->type
!= SSL3_RT_ALERT
) {
1917 if (version
!= s
->version
) {
1918 /* unexpected version, silently discard */
1921 RECORD_LAYER_reset_packet_length(&s
->rlayer
);
1926 if ((version
& 0xff00) != (s
->version
& 0xff00)) {
1927 /* wrong version, silently discard record */
1930 RECORD_LAYER_reset_packet_length(&s
->rlayer
);
1934 if (rr
->length
> SSL3_RT_MAX_ENCRYPTED_LENGTH
) {
1935 /* record too long, silently discard it */
1938 RECORD_LAYER_reset_packet_length(&s
->rlayer
);
1942 /* If received packet overflows own-client Max Fragment Length setting */
1943 if (s
->session
!= NULL
&& USE_MAX_FRAGMENT_LENGTH_EXT(s
->session
)
1944 && rr
->length
> GET_MAX_FRAGMENT_LENGTH(s
->session
)) {
1945 /* record too long, silently discard it */
1948 RECORD_LAYER_reset_packet_length(&s
->rlayer
);
1952 /* now s->rlayer.rstate == SSL_ST_READ_BODY */
1955 /* s->rlayer.rstate == SSL_ST_READ_BODY, get and decode the data */
1958 RECORD_LAYER_get_packet_length(&s
->rlayer
) - DTLS1_RT_HEADER_LENGTH
) {
1959 /* now s->packet_length == DTLS1_RT_HEADER_LENGTH */
1961 rret
= ssl3_read_n(s
, more
, more
, 1, 1, &n
);
1962 /* this packet contained a partial record, dump it */
1963 if (rret
<= 0 || n
!= more
) {
1964 if (ossl_statem_in_error(s
)) {
1965 /* ssl3_read_n() called SSLfatal() */
1970 RECORD_LAYER_reset_packet_length(&s
->rlayer
);
1975 * now n == rr->length, and s->packet_length ==
1976 * DTLS1_RT_HEADER_LENGTH + rr->length
1979 /* set state for later operations */
1980 RECORD_LAYER_set_rstate(&s
->rlayer
, SSL_ST_READ_HEADER
);
1982 /* match epochs. NULL means the packet is dropped on the floor */
1983 bitmap
= dtls1_get_bitmap(s
, rr
, &is_next_epoch
);
1984 if (bitmap
== NULL
) {
1986 RECORD_LAYER_reset_packet_length(&s
->rlayer
); /* dump this record */
1987 goto again
; /* get another record */
1989 #ifndef OPENSSL_NO_SCTP
1990 /* Only do replay check if no SCTP bio */
1991 if (!BIO_dgram_is_sctp(SSL_get_rbio(s
))) {
1993 /* Check whether this is a repeat, or aged record. */
1995 * TODO: Does it make sense to have replay protection in epoch 0 where
1996 * we have no integrity negotiated yet?
1998 if (!dtls1_record_replay_check(s
, bitmap
)) {
2001 RECORD_LAYER_reset_packet_length(&s
->rlayer
); /* dump this record */
2002 goto again
; /* get another record */
2004 #ifndef OPENSSL_NO_SCTP
2008 /* just read a 0 length packet */
2009 if (rr
->length
== 0) {
2015 * If this record is from the next epoch (either HM or ALERT), and a
2016 * handshake is currently in progress, buffer it since it cannot be
2017 * processed at this time.
2019 if (is_next_epoch
) {
2020 if ((SSL_in_init(s
) || ossl_statem_get_in_handshake(s
))) {
2021 if (dtls1_buffer_record (s
,
2022 &(DTLS_RECORD_LAYER_get_unprocessed_rcds(&s
->rlayer
)),
2024 /* SSLfatal() already called */
2030 RECORD_LAYER_reset_packet_length(&s
->rlayer
);
2034 if (!dtls1_process_record(s
, bitmap
)) {
2035 if (ossl_statem_in_error(s
)) {
2036 /* dtls1_process_record() called SSLfatal */
2041 RECORD_LAYER_reset_packet_length(&s
->rlayer
); /* dump this record */
2042 goto again
; /* get another record */
2049 int dtls_buffer_listen_record(SSL
*s
, size_t len
, unsigned char *seq
, size_t off
)
2053 rr
= RECORD_LAYER_get_rrec(&s
->rlayer
);
2054 memset(rr
, 0, sizeof(SSL3_RECORD
));
2057 rr
->type
= SSL3_RT_HANDSHAKE
;
2058 memcpy(rr
->seq_num
, seq
, sizeof(rr
->seq_num
));
2061 s
->rlayer
.packet
= RECORD_LAYER_get_rbuf(&s
->rlayer
)->buf
;
2062 s
->rlayer
.packet_length
= DTLS1_RT_HEADER_LENGTH
+ len
;
2063 rr
->data
= s
->rlayer
.packet
+ DTLS1_RT_HEADER_LENGTH
;
2065 if (dtls1_buffer_record(s
, &(s
->rlayer
.d
->processed_rcds
),
2066 SSL3_RECORD_get_seq_num(s
->rlayer
.rrec
)) <= 0) {
2067 /* SSLfatal() already called */