2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
11 #include "../ssl_locl.h"
12 #include "internal/constant_time_locl.h"
13 #include <openssl/rand.h>
14 #include "record_locl.h"
16 static const unsigned char ssl3_pad_1
[48] = {
17 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
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
25 static const unsigned char ssl3_pad_2
[48] = {
26 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
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
35 * Clear the contents of an SSL3_RECORD but retain any memory allocated
37 void SSL3_RECORD_clear(SSL3_RECORD
*r
, size_t num_recs
)
42 for (i
= 0; i
< num_recs
; i
++) {
45 memset(&r
[i
], 0, sizeof(*r
));
50 void SSL3_RECORD_release(SSL3_RECORD
*r
, size_t num_recs
)
54 for (i
= 0; i
< num_recs
; i
++) {
55 OPENSSL_free(r
[i
].comp
);
60 void SSL3_RECORD_set_seq_num(SSL3_RECORD
*r
, const unsigned char *seq_num
)
62 memcpy(r
->seq_num
, seq_num
, SEQ_NUM_SIZE
);
66 * Peeks ahead into "read_ahead" data to see if we have a whole record waiting
67 * for us in the buffer.
69 static int ssl3_record_app_data_waiting(SSL
*s
)
75 rbuf
= RECORD_LAYER_get_rbuf(&s
->rlayer
);
77 p
= SSL3_BUFFER_get_buf(rbuf
);
81 left
= SSL3_BUFFER_get_left(rbuf
);
83 if (left
< SSL3_RT_HEADER_LENGTH
)
86 p
+= SSL3_BUFFER_get_offset(rbuf
);
89 * We only check the type and record length, we will sanity check version
92 if (*p
!= SSL3_RT_APPLICATION_DATA
)
98 if (left
< SSL3_RT_HEADER_LENGTH
+ len
)
104 int early_data_count_ok(SSL
*s
, size_t length
, size_t overhead
, int *al
)
106 uint32_t max_early_data
= s
->max_early_data
;
109 * If we are a client then we always use the max_early_data from the
110 * session. Otherwise we go with the lowest out of the max early data set in
111 * the session and the configured max_early_data.
113 if (!s
->server
|| (s
->hit
114 && s
->session
->ext
.max_early_data
< s
->max_early_data
))
115 max_early_data
= s
->session
->ext
.max_early_data
;
117 if (max_early_data
== 0) {
119 *al
= SSL_AD_UNEXPECTED_MESSAGE
;
120 SSLerr(SSL_F_EARLY_DATA_COUNT_OK
, SSL_R_TOO_MUCH_EARLY_DATA
);
124 /* If we are dealing with ciphertext we need to allow for the overhead */
125 max_early_data
+= overhead
;
127 if (s
->early_data_count
+ length
> max_early_data
) {
129 *al
= SSL_AD_UNEXPECTED_MESSAGE
;
130 SSLerr(SSL_F_EARLY_DATA_COUNT_OK
, SSL_R_TOO_MUCH_EARLY_DATA
);
133 s
->early_data_count
+= length
;
139 * MAX_EMPTY_RECORDS defines the number of consecutive, empty records that
140 * will be processed per call to ssl3_get_record. Without this limit an
141 * attacker could send empty records at a faster rate than we can process and
142 * cause ssl3_get_record to loop forever.
144 #define MAX_EMPTY_RECORDS 32
146 #define SSL2_RT_HEADER_LENGTH 2
148 * Call this to get new input records.
149 * It will return <= 0 if more data is needed, normally due to an error
150 * or non-blocking IO.
151 * When it finishes, |numrpipes| records have been decoded. For each record 'i':
152 * rr[i].type - is the type of record
154 * rr[i].length, - number of bytes
155 * Multiple records will only be returned if the record types are all
156 * SSL3_RT_APPLICATION_DATA. The number of records returned will always be <=
159 /* used only by ssl3_read_bytes */
160 int ssl3_get_record(SSL
*s
)
163 int enc_err
, rret
, ret
= -1;
166 SSL3_RECORD
*rr
, *thisrr
;
170 unsigned char md
[EVP_MAX_MD_SIZE
];
171 unsigned int version
;
174 size_t num_recs
= 0, max_recs
, j
;
175 PACKET pkt
, sslv2pkt
;
176 size_t first_rec_len
;
178 rr
= RECORD_LAYER_get_rrec(&s
->rlayer
);
179 rbuf
= RECORD_LAYER_get_rbuf(&s
->rlayer
);
180 max_recs
= s
->max_pipelines
;
186 thisrr
= &rr
[num_recs
];
188 /* check if we have the header */
189 if ((RECORD_LAYER_get_rstate(&s
->rlayer
) != SSL_ST_READ_BODY
) ||
190 (RECORD_LAYER_get_packet_length(&s
->rlayer
)
191 < SSL3_RT_HEADER_LENGTH
)) {
195 rret
= ssl3_read_n(s
, SSL3_RT_HEADER_LENGTH
,
196 SSL3_BUFFER_get_len(rbuf
), 0,
197 num_recs
== 0 ? 1 : 0, &n
);
199 return rret
; /* error or non-blocking */
200 RECORD_LAYER_set_rstate(&s
->rlayer
, SSL_ST_READ_BODY
);
202 p
= RECORD_LAYER_get_packet(&s
->rlayer
);
203 if (!PACKET_buf_init(&pkt
, RECORD_LAYER_get_packet(&s
->rlayer
),
204 RECORD_LAYER_get_packet_length(&s
->rlayer
))) {
205 al
= SSL_AD_INTERNAL_ERROR
;
206 SSLerr(SSL_F_SSL3_GET_RECORD
, ERR_R_INTERNAL_ERROR
);
210 if (!PACKET_get_net_2_len(&sslv2pkt
, &sslv2len
)
211 || !PACKET_get_1(&sslv2pkt
, &type
)) {
212 al
= SSL_AD_INTERNAL_ERROR
;
213 SSLerr(SSL_F_SSL3_GET_RECORD
, ERR_R_INTERNAL_ERROR
);
217 * The first record received by the server may be a V2ClientHello.
219 if (s
->server
&& RECORD_LAYER_is_first_record(&s
->rlayer
)
220 && (sslv2len
& 0x8000) != 0
221 && (type
== SSL2_MT_CLIENT_HELLO
)) {
225 * |num_recs| here will actually always be 0 because
226 * |num_recs > 0| only ever occurs when we are processing
227 * multiple app data records - which we know isn't the case here
228 * because it is an SSLv2ClientHello. We keep it using
229 * |num_recs| for the sake of consistency
231 thisrr
->type
= SSL3_RT_HANDSHAKE
;
232 thisrr
->rec_version
= SSL2_VERSION
;
234 thisrr
->length
= sslv2len
& 0x7fff;
236 if (thisrr
->length
> SSL3_BUFFER_get_len(rbuf
)
237 - SSL2_RT_HEADER_LENGTH
) {
238 al
= SSL_AD_RECORD_OVERFLOW
;
239 SSLerr(SSL_F_SSL3_GET_RECORD
, SSL_R_PACKET_LENGTH_TOO_LONG
);
243 if (thisrr
->length
< MIN_SSL2_RECORD_LEN
) {
244 al
= SSL_AD_HANDSHAKE_FAILURE
;
245 SSLerr(SSL_F_SSL3_GET_RECORD
, SSL_R_LENGTH_TOO_SHORT
);
249 /* SSLv3+ style record */
251 * TODO(TLS1.3): This callback only provides the "outer" record
252 * type to the callback. Somehow we need to pass the "inner"
256 s
->msg_callback(0, 0, SSL3_RT_HEADER
, p
, 5, s
,
257 s
->msg_callback_arg
);
259 /* Pull apart the header into the SSL3_RECORD */
260 if (!PACKET_get_1(&pkt
, &type
)
261 || !PACKET_get_net_2(&pkt
, &version
)
262 || !PACKET_get_net_2_len(&pkt
, &thisrr
->length
)) {
263 al
= SSL_AD_INTERNAL_ERROR
;
264 SSLerr(SSL_F_SSL3_GET_RECORD
, ERR_R_INTERNAL_ERROR
);
268 thisrr
->rec_version
= version
;
270 /* Lets check version. In TLSv1.3 we ignore this field */
271 if (!s
->first_packet
&& !SSL_IS_TLS13(s
)
272 && version
!= (unsigned int)s
->version
) {
273 SSLerr(SSL_F_SSL3_GET_RECORD
, SSL_R_WRONG_VERSION_NUMBER
);
274 if ((s
->version
& 0xFF00) == (version
& 0xFF00)
275 && !s
->enc_write_ctx
&& !s
->write_hash
) {
276 if (thisrr
->type
== SSL3_RT_ALERT
) {
278 * The record is using an incorrect version number,
279 * but what we've got appears to be an alert. We
280 * haven't read the body yet to check whether its a
281 * fatal or not - but chances are it is. We probably
282 * shouldn't send a fatal alert back. We'll just
288 * Send back error using their minor version number :-)
290 s
->version
= (unsigned short)version
;
292 al
= SSL_AD_PROTOCOL_VERSION
;
296 if ((version
>> 8) != SSL3_VERSION_MAJOR
) {
297 if (RECORD_LAYER_is_first_record(&s
->rlayer
)) {
298 /* Go back to start of packet, look at the five bytes
300 p
= RECORD_LAYER_get_packet(&s
->rlayer
);
301 if (strncmp((char *)p
, "GET ", 4) == 0 ||
302 strncmp((char *)p
, "POST ", 5) == 0 ||
303 strncmp((char *)p
, "HEAD ", 5) == 0 ||
304 strncmp((char *)p
, "PUT ", 4) == 0) {
305 SSLerr(SSL_F_SSL3_GET_RECORD
, SSL_R_HTTP_REQUEST
);
307 } else if (strncmp((char *)p
, "CONNE", 5) == 0) {
308 SSLerr(SSL_F_SSL3_GET_RECORD
,
309 SSL_R_HTTPS_PROXY_REQUEST
);
313 /* Doesn't look like TLS - don't send an alert */
314 SSLerr(SSL_F_SSL3_GET_RECORD
,
315 SSL_R_WRONG_VERSION_NUMBER
);
318 SSLerr(SSL_F_SSL3_GET_RECORD
,
319 SSL_R_WRONG_VERSION_NUMBER
);
320 al
= SSL_AD_PROTOCOL_VERSION
;
325 if (SSL_IS_TLS13(s
) && s
->enc_read_ctx
!= NULL
326 && thisrr
->type
!= SSL3_RT_APPLICATION_DATA
) {
327 SSLerr(SSL_F_SSL3_GET_RECORD
, SSL_R_BAD_RECORD_TYPE
);
328 al
= SSL_AD_UNEXPECTED_MESSAGE
;
333 SSL3_BUFFER_get_len(rbuf
) - SSL3_RT_HEADER_LENGTH
) {
334 al
= SSL_AD_RECORD_OVERFLOW
;
335 SSLerr(SSL_F_SSL3_GET_RECORD
, SSL_R_PACKET_LENGTH_TOO_LONG
);
340 /* now s->rlayer.rstate == SSL_ST_READ_BODY */
343 if (SSL_IS_TLS13(s
)) {
344 if (thisrr
->length
> SSL3_RT_MAX_TLS13_ENCRYPTED_LENGTH
) {
345 al
= SSL_AD_RECORD_OVERFLOW
;
346 SSLerr(SSL_F_SSL3_GET_RECORD
, SSL_R_ENCRYPTED_LENGTH_TOO_LONG
);
350 size_t len
= SSL3_RT_MAX_ENCRYPTED_LENGTH
;
352 #ifndef OPENSSL_NO_COMP
354 * If OPENSSL_NO_COMP is defined then SSL3_RT_MAX_ENCRYPTED_LENGTH
355 * does not include the compression overhead anyway.
357 if (s
->expand
== NULL
)
358 len
-= SSL3_RT_MAX_COMPRESSED_OVERHEAD
;
361 if (thisrr
->length
> len
) {
362 al
= SSL_AD_RECORD_OVERFLOW
;
363 SSLerr(SSL_F_SSL3_GET_RECORD
, SSL_R_ENCRYPTED_LENGTH_TOO_LONG
);
369 * s->rlayer.rstate == SSL_ST_READ_BODY, get and decode the data.
370 * Calculate how much more data we need to read for the rest of the
373 if (thisrr
->rec_version
== SSL2_VERSION
) {
374 more
= thisrr
->length
+ SSL2_RT_HEADER_LENGTH
375 - SSL3_RT_HEADER_LENGTH
;
377 more
= thisrr
->length
;
380 /* now s->packet_length == SSL3_RT_HEADER_LENGTH */
382 rret
= ssl3_read_n(s
, more
, more
, 1, 0, &n
);
384 return rret
; /* error or non-blocking io */
387 /* set state for later operations */
388 RECORD_LAYER_set_rstate(&s
->rlayer
, SSL_ST_READ_HEADER
);
391 * At this point, s->packet_length == SSL3_RT_HEADER_LENGTH
392 * + thisrr->length, or s->packet_length == SSL2_RT_HEADER_LENGTH
393 * + thisrr->length and we have that many bytes in s->packet
395 if (thisrr
->rec_version
== SSL2_VERSION
) {
397 &(RECORD_LAYER_get_packet(&s
->rlayer
)[SSL2_RT_HEADER_LENGTH
]);
400 &(RECORD_LAYER_get_packet(&s
->rlayer
)[SSL3_RT_HEADER_LENGTH
]);
404 * ok, we can now read from 's->packet' data into 'thisrr' thisrr->input
405 * points at thisrr->length bytes, which need to be copied into
406 * thisrr->data by either the decryption or by the decompression When
407 * the data is 'copied' into the thisrr->data buffer, thisrr->input will
408 * be pointed at the new buffer
412 * We now have - encrypted [ MAC [ compressed [ plain ] ] ]
413 * thisrr->length bytes of encrypted compressed stuff.
416 /* decrypt in place in 'thisrr->input' */
417 thisrr
->data
= thisrr
->input
;
418 thisrr
->orig_len
= thisrr
->length
;
420 /* Mark this record as not read by upper layers yet */
425 /* we have pulled in a full packet so zero things */
426 RECORD_LAYER_reset_packet_length(&s
->rlayer
);
427 RECORD_LAYER_clear_first_record(&s
->rlayer
);
428 } while (num_recs
< max_recs
429 && thisrr
->type
== SSL3_RT_APPLICATION_DATA
430 && SSL_USE_EXPLICIT_IV(s
)
431 && s
->enc_read_ctx
!= NULL
432 && (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s
->enc_read_ctx
))
433 & EVP_CIPH_FLAG_PIPELINE
)
434 && ssl3_record_app_data_waiting(s
));
437 * If in encrypt-then-mac mode calculate mac from encrypted record. All
438 * the details below are public so no timing details can leak.
440 if (SSL_READ_ETM(s
) && s
->read_hash
) {
442 /* TODO(size_t): convert this to do size_t properly */
443 imac_size
= EVP_MD_CTX_size(s
->read_hash
);
444 assert(imac_size
>= 0 && imac_size
<= EVP_MAX_MD_SIZE
);
445 if (imac_size
< 0 || imac_size
> EVP_MAX_MD_SIZE
) {
446 al
= SSL_AD_INTERNAL_ERROR
;
447 SSLerr(SSL_F_SSL3_GET_RECORD
, ERR_LIB_EVP
);
450 mac_size
= (size_t)imac_size
;
451 for (j
= 0; j
< num_recs
; j
++) {
454 if (thisrr
->length
< mac_size
) {
455 al
= SSL_AD_DECODE_ERROR
;
456 SSLerr(SSL_F_SSL3_GET_RECORD
, SSL_R_LENGTH_TOO_SHORT
);
459 thisrr
->length
-= mac_size
;
460 mac
= thisrr
->data
+ thisrr
->length
;
461 i
= s
->method
->ssl3_enc
->mac(s
, thisrr
, md
, 0 /* not send */ );
462 if (i
== 0 || CRYPTO_memcmp(md
, mac
, mac_size
) != 0) {
463 al
= SSL_AD_BAD_RECORD_MAC
;
464 SSLerr(SSL_F_SSL3_GET_RECORD
,
465 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC
);
471 first_rec_len
= rr
[0].length
;
473 enc_err
= s
->method
->ssl3_enc
->enc(s
, rr
, num_recs
, 0);
477 * 0: (in non-constant time) if the record is publicly invalid.
478 * 1: if the padding is valid
479 * -1: if the padding is invalid
482 if (num_recs
== 1 && ossl_statem_skip_early_data(s
)) {
484 * Valid early_data that we cannot decrypt might fail here as
485 * publicly invalid. We treat it like an empty record.
490 if (!early_data_count_ok(s
, thisrr
->length
,
491 EARLY_DATA_CIPHERTEXT_OVERHEAD
, &al
))
496 RECORD_LAYER_set_numrpipes(&s
->rlayer
, 1);
497 RECORD_LAYER_reset_read_sequence(&s
->rlayer
);
500 al
= SSL_AD_DECRYPTION_FAILED
;
501 SSLerr(SSL_F_SSL3_GET_RECORD
, SSL_R_BLOCK_CIPHER_PAD_IS_WRONG
);
505 printf("dec %"OSSLzu
"\n", rr
[0].length
);
508 for (z
= 0; z
< rr
[0].length
; z
++)
509 printf("%02X%c", rr
[0].data
[z
], ((z
+ 1) % 16) ? ' ' : '\n');
514 /* r->length is now the compressed data plus mac */
515 if ((sess
!= NULL
) &&
516 (s
->enc_read_ctx
!= NULL
) &&
517 (!SSL_READ_ETM(s
) && EVP_MD_CTX_md(s
->read_hash
) != NULL
)) {
518 /* s->read_hash != NULL => mac_size != -1 */
519 unsigned char *mac
= NULL
;
520 unsigned char mac_tmp
[EVP_MAX_MD_SIZE
];
522 mac_size
= EVP_MD_CTX_size(s
->read_hash
);
523 OPENSSL_assert(mac_size
<= EVP_MAX_MD_SIZE
);
525 for (j
= 0; j
< num_recs
; j
++) {
528 * orig_len is the length of the record before any padding was
529 * removed. This is public information, as is the MAC in use,
530 * therefore we can safely process the record in a different amount
531 * of time if it's too short to possibly contain a MAC.
533 if (thisrr
->orig_len
< mac_size
||
534 /* CBC records must have a padding length byte too. */
535 (EVP_CIPHER_CTX_mode(s
->enc_read_ctx
) == EVP_CIPH_CBC_MODE
&&
536 thisrr
->orig_len
< mac_size
+ 1)) {
537 al
= SSL_AD_DECODE_ERROR
;
538 SSLerr(SSL_F_SSL3_GET_RECORD
, SSL_R_LENGTH_TOO_SHORT
);
542 if (EVP_CIPHER_CTX_mode(s
->enc_read_ctx
) == EVP_CIPH_CBC_MODE
) {
544 * We update the length so that the TLS header bytes can be
545 * constructed correctly but we need to extract the MAC in
546 * constant time from within the record, without leaking the
547 * contents of the padding bytes.
550 ssl3_cbc_copy_mac(mac_tmp
, thisrr
, mac_size
);
551 thisrr
->length
-= mac_size
;
554 * In this case there's no padding, so |rec->orig_len| equals
555 * |rec->length| and we checked that there's enough bytes for
558 thisrr
->length
-= mac_size
;
559 mac
= &thisrr
->data
[thisrr
->length
];
562 i
= s
->method
->ssl3_enc
->mac(s
, thisrr
, md
, 0 /* not send */ );
563 if (i
== 0 || mac
== NULL
564 || CRYPTO_memcmp(md
, mac
, (size_t)mac_size
) != 0)
566 if (thisrr
->length
> SSL3_RT_MAX_COMPRESSED_LENGTH
+ mac_size
)
572 if (num_recs
== 1 && ossl_statem_skip_early_data(s
)) {
574 * We assume this is unreadable early_data - we treat it like an
579 * The record length may have been modified by the mac check above
580 * so we use the previously saved value
582 if (!early_data_count_ok(s
, first_rec_len
,
583 EARLY_DATA_CIPHERTEXT_OVERHEAD
, &al
))
589 RECORD_LAYER_set_numrpipes(&s
->rlayer
, 1);
590 RECORD_LAYER_reset_read_sequence(&s
->rlayer
);
594 * A separate 'decryption_failed' alert was introduced with TLS 1.0,
595 * SSL 3.0 only has 'bad_record_mac'. But unless a decryption
596 * failure is directly visible from the ciphertext anyway, we should
597 * not reveal which kind of error occurred -- this might become
598 * visible to an attacker (e.g. via a logfile)
600 al
= SSL_AD_BAD_RECORD_MAC
;
601 SSLerr(SSL_F_SSL3_GET_RECORD
,
602 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC
);
606 for (j
= 0; j
< num_recs
; j
++) {
609 /* thisrr->length is now just compressed */
610 if (s
->expand
!= NULL
) {
611 if (thisrr
->length
> SSL3_RT_MAX_COMPRESSED_LENGTH
) {
612 al
= SSL_AD_RECORD_OVERFLOW
;
613 SSLerr(SSL_F_SSL3_GET_RECORD
, SSL_R_COMPRESSED_LENGTH_TOO_LONG
);
616 if (!ssl3_do_uncompress(s
, thisrr
)) {
617 al
= SSL_AD_DECOMPRESSION_FAILURE
;
618 SSLerr(SSL_F_SSL3_GET_RECORD
, SSL_R_BAD_DECOMPRESSION
);
623 if (SSL_IS_TLS13(s
) && s
->enc_read_ctx
!= NULL
) {
626 if (thisrr
->length
== 0) {
627 al
= SSL_AD_UNEXPECTED_MESSAGE
;
628 SSLerr(SSL_F_SSL3_GET_RECORD
, SSL_R_BAD_RECORD_TYPE
);
632 /* Strip trailing padding */
633 for (end
= thisrr
->length
- 1; end
> 0 && thisrr
->data
[end
] == 0;
637 thisrr
->length
= end
;
638 thisrr
->type
= thisrr
->data
[end
];
639 if (thisrr
->type
!= SSL3_RT_APPLICATION_DATA
640 && thisrr
->type
!= SSL3_RT_ALERT
641 && thisrr
->type
!= SSL3_RT_HANDSHAKE
) {
642 al
= SSL_AD_UNEXPECTED_MESSAGE
;
643 SSLerr(SSL_F_SSL3_GET_RECORD
, SSL_R_BAD_RECORD_TYPE
);
648 if (thisrr
->length
> SSL3_RT_MAX_PLAIN_LENGTH
) {
649 al
= SSL_AD_RECORD_OVERFLOW
;
650 SSLerr(SSL_F_SSL3_GET_RECORD
, SSL_R_DATA_LENGTH_TOO_LONG
);
656 * So at this point the following is true
657 * thisrr->type is the type of record
658 * thisrr->length == number of bytes in record
659 * thisrr->off == offset to first valid byte
660 * thisrr->data == where to take bytes from, increment after use :-).
663 /* just read a 0 length packet */
664 if (thisrr
->length
== 0) {
665 RECORD_LAYER_inc_empty_record_count(&s
->rlayer
);
666 if (RECORD_LAYER_get_empty_record_count(&s
->rlayer
)
667 > MAX_EMPTY_RECORDS
) {
668 al
= SSL_AD_UNEXPECTED_MESSAGE
;
669 SSLerr(SSL_F_SSL3_GET_RECORD
, SSL_R_RECORD_TOO_SMALL
);
673 RECORD_LAYER_reset_empty_record_count(&s
->rlayer
);
677 if (s
->early_data_state
== SSL_EARLY_DATA_READING
) {
679 if (thisrr
->type
== SSL3_RT_APPLICATION_DATA
680 && !early_data_count_ok(s
, thisrr
->length
, 0, &al
))
684 RECORD_LAYER_set_numrpipes(&s
->rlayer
, num_recs
);
688 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
693 int ssl3_do_uncompress(SSL
*ssl
, SSL3_RECORD
*rr
)
695 #ifndef OPENSSL_NO_COMP
698 if (rr
->comp
== NULL
) {
699 rr
->comp
= (unsigned char *)
700 OPENSSL_malloc(SSL3_RT_MAX_ENCRYPTED_LENGTH
);
702 if (rr
->comp
== NULL
)
705 /* TODO(size_t): Convert this call */
706 i
= COMP_expand_block(ssl
->expand
, rr
->comp
,
707 SSL3_RT_MAX_PLAIN_LENGTH
, rr
->data
, (int)rr
->length
);
717 int ssl3_do_compress(SSL
*ssl
, SSL3_RECORD
*wr
)
719 #ifndef OPENSSL_NO_COMP
722 /* TODO(size_t): Convert this call */
723 i
= COMP_compress_block(ssl
->compress
, wr
->data
,
724 (int)(wr
->length
+ SSL3_RT_MAX_COMPRESSED_OVERHEAD
),
725 wr
->input
, (int)wr
->length
);
731 wr
->input
= wr
->data
;
737 * ssl3_enc encrypts/decrypts |n_recs| records in |inrecs|
740 * 0: (in non-constant time) if the record is publically invalid (i.e. too
742 * 1: if the record's padding is valid / the encryption was successful.
743 * -1: if the record's padding is invalid or, if sending, an internal error
746 int ssl3_enc(SSL
*s
, SSL3_RECORD
*inrecs
, size_t n_recs
, int sending
)
751 size_t bs
, mac_size
= 0;
753 const EVP_CIPHER
*enc
;
757 * We shouldn't ever be called with more than one record in the SSLv3 case
762 ds
= s
->enc_write_ctx
;
763 if (s
->enc_write_ctx
== NULL
)
766 enc
= EVP_CIPHER_CTX_cipher(s
->enc_write_ctx
);
768 ds
= s
->enc_read_ctx
;
769 if (s
->enc_read_ctx
== NULL
)
772 enc
= EVP_CIPHER_CTX_cipher(s
->enc_read_ctx
);
775 if ((s
->session
== NULL
) || (ds
== NULL
) || (enc
== NULL
)) {
776 memmove(rec
->data
, rec
->input
, rec
->length
);
777 rec
->input
= rec
->data
;
780 /* TODO(size_t): Convert this call */
781 bs
= EVP_CIPHER_CTX_block_size(ds
);
785 if ((bs
!= 1) && sending
) {
788 /* we need to add 'i-1' padding bytes */
791 * the last of these zero bytes will be overwritten with the
794 memset(&rec
->input
[rec
->length
], 0, i
);
796 rec
->input
[l
- 1] = (unsigned char)(i
- 1);
800 if (l
== 0 || l
% bs
!= 0)
802 /* otherwise, rec->length >= bs */
805 /* TODO(size_t): Convert this call */
806 if (EVP_Cipher(ds
, rec
->data
, rec
->input
, (unsigned int)l
) < 1)
809 if (EVP_MD_CTX_md(s
->read_hash
) != NULL
) {
810 /* TODO(size_t): convert me */
811 imac_size
= EVP_MD_CTX_size(s
->read_hash
);
814 mac_size
= (size_t)imac_size
;
816 if ((bs
!= 1) && !sending
)
817 return ssl3_cbc_remove_padding(rec
, bs
, mac_size
);
822 #define MAX_PADDING 256
824 * tls1_enc encrypts/decrypts |n_recs| in |recs|.
827 * 0: (in non-constant time) if the record is publically invalid (i.e. too
829 * 1: if the record's padding is valid / the encryption was successful.
830 * -1: if the record's padding/AEAD-authenticator is invalid or, if sending,
831 * an internal error occurred.
833 int tls1_enc(SSL
*s
, SSL3_RECORD
*recs
, size_t n_recs
, int sending
)
836 size_t reclen
[SSL_MAX_PIPELINES
];
837 unsigned char buf
[SSL_MAX_PIPELINES
][EVP_AEAD_TLS1_AAD_LEN
];
838 int i
, pad
= 0, ret
, tmpr
;
839 size_t bs
, mac_size
= 0, ctr
, padnum
, loop
;
840 unsigned char padval
;
842 const EVP_CIPHER
*enc
;
848 if (EVP_MD_CTX_md(s
->write_hash
)) {
849 int n
= EVP_MD_CTX_size(s
->write_hash
);
850 OPENSSL_assert(n
>= 0);
852 ds
= s
->enc_write_ctx
;
853 if (s
->enc_write_ctx
== NULL
)
857 enc
= EVP_CIPHER_CTX_cipher(s
->enc_write_ctx
);
858 /* For TLSv1.1 and later explicit IV */
859 if (SSL_USE_EXPLICIT_IV(s
)
860 && EVP_CIPHER_mode(enc
) == EVP_CIPH_CBC_MODE
)
861 ivlen
= EVP_CIPHER_iv_length(enc
);
865 for (ctr
= 0; ctr
< n_recs
; ctr
++) {
866 if (recs
[ctr
].data
!= recs
[ctr
].input
) {
868 * we can't write into the input stream: Can this ever
871 SSLerr(SSL_F_TLS1_ENC
, ERR_R_INTERNAL_ERROR
);
873 } else if (RAND_bytes(recs
[ctr
].input
, ivlen
) <= 0) {
874 SSLerr(SSL_F_TLS1_ENC
, ERR_R_INTERNAL_ERROR
);
881 if (EVP_MD_CTX_md(s
->read_hash
)) {
882 int n
= EVP_MD_CTX_size(s
->read_hash
);
883 OPENSSL_assert(n
>= 0);
885 ds
= s
->enc_read_ctx
;
886 if (s
->enc_read_ctx
== NULL
)
889 enc
= EVP_CIPHER_CTX_cipher(s
->enc_read_ctx
);
892 if ((s
->session
== NULL
) || (ds
== NULL
) || (enc
== NULL
)) {
893 for (ctr
= 0; ctr
< n_recs
; ctr
++) {
894 memmove(recs
[ctr
].data
, recs
[ctr
].input
, recs
[ctr
].length
);
895 recs
[ctr
].input
= recs
[ctr
].data
;
899 bs
= EVP_CIPHER_block_size(EVP_CIPHER_CTX_cipher(ds
));
902 if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds
))
903 & EVP_CIPH_FLAG_PIPELINE
)) {
905 * We shouldn't have been called with pipeline data if the
906 * cipher doesn't support pipelining
908 SSLerr(SSL_F_TLS1_ENC
, SSL_R_PIPELINE_FAILURE
);
912 for (ctr
= 0; ctr
< n_recs
; ctr
++) {
913 reclen
[ctr
] = recs
[ctr
].length
;
915 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds
))
916 & EVP_CIPH_FLAG_AEAD_CIPHER
) {
919 seq
= sending
? RECORD_LAYER_get_write_sequence(&s
->rlayer
)
920 : RECORD_LAYER_get_read_sequence(&s
->rlayer
);
922 if (SSL_IS_DTLS(s
)) {
923 /* DTLS does not support pipelining */
924 unsigned char dtlsseq
[9], *p
= dtlsseq
;
926 s2n(sending
? DTLS_RECORD_LAYER_get_w_epoch(&s
->rlayer
) :
927 DTLS_RECORD_LAYER_get_r_epoch(&s
->rlayer
), p
);
928 memcpy(p
, &seq
[2], 6);
929 memcpy(buf
[ctr
], dtlsseq
, 8);
931 memcpy(buf
[ctr
], seq
, 8);
932 for (i
= 7; i
>= 0; i
--) { /* increment */
939 buf
[ctr
][8] = recs
[ctr
].type
;
940 buf
[ctr
][9] = (unsigned char)(s
->version
>> 8);
941 buf
[ctr
][10] = (unsigned char)(s
->version
);
942 buf
[ctr
][11] = (unsigned char)(recs
[ctr
].length
>> 8);
943 buf
[ctr
][12] = (unsigned char)(recs
[ctr
].length
& 0xff);
944 pad
= EVP_CIPHER_CTX_ctrl(ds
, EVP_CTRL_AEAD_TLS1_AAD
,
945 EVP_AEAD_TLS1_AAD_LEN
, buf
[ctr
]);
951 recs
[ctr
].length
+= pad
;
954 } else if ((bs
!= 1) && sending
) {
955 padnum
= bs
- (reclen
[ctr
] % bs
);
957 /* Add weird padding of upto 256 bytes */
959 if (padnum
> MAX_PADDING
)
961 /* we need to add 'padnum' padding bytes of value padval */
962 padval
= (unsigned char)(padnum
- 1);
963 for (loop
= reclen
[ctr
]; loop
< reclen
[ctr
] + padnum
; loop
++)
964 recs
[ctr
].input
[loop
] = padval
;
965 reclen
[ctr
] += padnum
;
966 recs
[ctr
].length
+= padnum
;
970 if (reclen
[ctr
] == 0 || reclen
[ctr
] % bs
!= 0)
975 unsigned char *data
[SSL_MAX_PIPELINES
];
977 /* Set the output buffers */
978 for (ctr
= 0; ctr
< n_recs
; ctr
++) {
979 data
[ctr
] = recs
[ctr
].data
;
981 if (EVP_CIPHER_CTX_ctrl(ds
, EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS
,
982 (int)n_recs
, data
) <= 0) {
983 SSLerr(SSL_F_TLS1_ENC
, SSL_R_PIPELINE_FAILURE
);
985 /* Set the input buffers */
986 for (ctr
= 0; ctr
< n_recs
; ctr
++) {
987 data
[ctr
] = recs
[ctr
].input
;
989 if (EVP_CIPHER_CTX_ctrl(ds
, EVP_CTRL_SET_PIPELINE_INPUT_BUFS
,
990 (int)n_recs
, data
) <= 0
991 || EVP_CIPHER_CTX_ctrl(ds
, EVP_CTRL_SET_PIPELINE_INPUT_LENS
,
992 (int)n_recs
, reclen
) <= 0) {
993 SSLerr(SSL_F_TLS1_ENC
, SSL_R_PIPELINE_FAILURE
);
998 /* TODO(size_t): Convert this call */
999 tmpr
= EVP_Cipher(ds
, recs
[0].data
, recs
[0].input
,
1000 (unsigned int)reclen
[0]);
1001 if ((EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds
))
1002 & EVP_CIPH_FLAG_CUSTOM_CIPHER
)
1005 return -1; /* AEAD can fail to verify MAC */
1007 if (EVP_CIPHER_mode(enc
) == EVP_CIPH_GCM_MODE
) {
1008 for (ctr
= 0; ctr
< n_recs
; ctr
++) {
1009 recs
[ctr
].data
+= EVP_GCM_TLS_EXPLICIT_IV_LEN
;
1010 recs
[ctr
].input
+= EVP_GCM_TLS_EXPLICIT_IV_LEN
;
1011 recs
[ctr
].length
-= EVP_GCM_TLS_EXPLICIT_IV_LEN
;
1013 } else if (EVP_CIPHER_mode(enc
) == EVP_CIPH_CCM_MODE
) {
1014 for (ctr
= 0; ctr
< n_recs
; ctr
++) {
1015 recs
[ctr
].data
+= EVP_CCM_TLS_EXPLICIT_IV_LEN
;
1016 recs
[ctr
].input
+= EVP_CCM_TLS_EXPLICIT_IV_LEN
;
1017 recs
[ctr
].length
-= EVP_CCM_TLS_EXPLICIT_IV_LEN
;
1023 if (!SSL_READ_ETM(s
) && EVP_MD_CTX_md(s
->read_hash
) != NULL
) {
1024 imac_size
= EVP_MD_CTX_size(s
->read_hash
);
1027 mac_size
= (size_t)imac_size
;
1029 if ((bs
!= 1) && !sending
) {
1031 for (ctr
= 0; ctr
< n_recs
; ctr
++) {
1032 tmpret
= tls1_cbc_remove_padding(s
, &recs
[ctr
], bs
, mac_size
);
1034 * If tmpret == 0 then this means publicly invalid so we can
1035 * short circuit things here. Otherwise we must respect constant
1040 ret
= constant_time_select_int(constant_time_eq_int(tmpret
, 1),
1044 if (pad
&& !sending
) {
1045 for (ctr
= 0; ctr
< n_recs
; ctr
++) {
1046 recs
[ctr
].length
-= pad
;
1053 int n_ssl3_mac(SSL
*ssl
, SSL3_RECORD
*rec
, unsigned char *md
, int sending
)
1055 unsigned char *mac_sec
, *seq
;
1056 const EVP_MD_CTX
*hash
;
1057 unsigned char *p
, rec_char
;
1063 mac_sec
= &(ssl
->s3
->write_mac_secret
[0]);
1064 seq
= RECORD_LAYER_get_write_sequence(&ssl
->rlayer
);
1065 hash
= ssl
->write_hash
;
1067 mac_sec
= &(ssl
->s3
->read_mac_secret
[0]);
1068 seq
= RECORD_LAYER_get_read_sequence(&ssl
->rlayer
);
1069 hash
= ssl
->read_hash
;
1072 t
= EVP_MD_CTX_size(hash
);
1076 npad
= (48 / md_size
) * md_size
;
1079 EVP_CIPHER_CTX_mode(ssl
->enc_read_ctx
) == EVP_CIPH_CBC_MODE
&&
1080 ssl3_cbc_record_digest_supported(hash
)) {
1082 * This is a CBC-encrypted record. We must avoid leaking any
1083 * timing-side channel information about how many blocks of data we
1084 * are hashing because that gives an attacker a timing-oracle.
1088 * npad is, at most, 48 bytes and that's with MD5:
1089 * 16 + 48 + 8 (sequence bytes) + 1 + 2 = 75.
1091 * With SHA-1 (the largest hash speced for SSLv3) the hash size
1092 * goes up 4, but npad goes down by 8, resulting in a smaller
1095 unsigned char header
[75];
1097 memcpy(header
+ j
, mac_sec
, md_size
);
1099 memcpy(header
+ j
, ssl3_pad_1
, npad
);
1101 memcpy(header
+ j
, seq
, 8);
1103 header
[j
++] = rec
->type
;
1104 header
[j
++] = (unsigned char)(rec
->length
>> 8);
1105 header
[j
++] = (unsigned char)(rec
->length
& 0xff);
1107 /* Final param == is SSLv3 */
1108 if (ssl3_cbc_digest_record(hash
,
1111 rec
->length
+ md_size
, rec
->orig_len
,
1112 mac_sec
, md_size
, 1) <= 0)
1115 unsigned int md_size_u
;
1116 /* Chop the digest off the end :-) */
1117 EVP_MD_CTX
*md_ctx
= EVP_MD_CTX_new();
1122 rec_char
= rec
->type
;
1124 s2n(rec
->length
, p
);
1125 if (EVP_MD_CTX_copy_ex(md_ctx
, hash
) <= 0
1126 || EVP_DigestUpdate(md_ctx
, mac_sec
, md_size
) <= 0
1127 || EVP_DigestUpdate(md_ctx
, ssl3_pad_1
, npad
) <= 0
1128 || EVP_DigestUpdate(md_ctx
, seq
, 8) <= 0
1129 || EVP_DigestUpdate(md_ctx
, &rec_char
, 1) <= 0
1130 || EVP_DigestUpdate(md_ctx
, md
, 2) <= 0
1131 || EVP_DigestUpdate(md_ctx
, rec
->input
, rec
->length
) <= 0
1132 || EVP_DigestFinal_ex(md_ctx
, md
, NULL
) <= 0
1133 || EVP_MD_CTX_copy_ex(md_ctx
, hash
) <= 0
1134 || EVP_DigestUpdate(md_ctx
, mac_sec
, md_size
) <= 0
1135 || EVP_DigestUpdate(md_ctx
, ssl3_pad_2
, npad
) <= 0
1136 || EVP_DigestUpdate(md_ctx
, md
, md_size
) <= 0
1137 || EVP_DigestFinal_ex(md_ctx
, md
, &md_size_u
) <= 0) {
1138 EVP_MD_CTX_reset(md_ctx
);
1142 EVP_MD_CTX_free(md_ctx
);
1145 ssl3_record_sequence_update(seq
);
1149 int tls1_mac(SSL
*ssl
, SSL3_RECORD
*rec
, unsigned char *md
, int sending
)
1155 EVP_MD_CTX
*hmac
= NULL
, *mac_ctx
;
1156 unsigned char header
[13];
1157 int stream_mac
= (sending
? (ssl
->mac_flags
& SSL_MAC_FLAG_WRITE_MAC_STREAM
)
1158 : (ssl
->mac_flags
& SSL_MAC_FLAG_READ_MAC_STREAM
));
1162 seq
= RECORD_LAYER_get_write_sequence(&ssl
->rlayer
);
1163 hash
= ssl
->write_hash
;
1165 seq
= RECORD_LAYER_get_read_sequence(&ssl
->rlayer
);
1166 hash
= ssl
->read_hash
;
1169 t
= EVP_MD_CTX_size(hash
);
1170 OPENSSL_assert(t
>= 0);
1173 /* I should fix this up TLS TLS TLS TLS TLS XXXXXXXX */
1177 hmac
= EVP_MD_CTX_new();
1178 if (hmac
== NULL
|| !EVP_MD_CTX_copy(hmac
, hash
))
1183 if (SSL_IS_DTLS(ssl
)) {
1184 unsigned char dtlsseq
[8], *p
= dtlsseq
;
1186 s2n(sending
? DTLS_RECORD_LAYER_get_w_epoch(&ssl
->rlayer
) :
1187 DTLS_RECORD_LAYER_get_r_epoch(&ssl
->rlayer
), p
);
1188 memcpy(p
, &seq
[2], 6);
1190 memcpy(header
, dtlsseq
, 8);
1192 memcpy(header
, seq
, 8);
1194 header
[8] = rec
->type
;
1195 header
[9] = (unsigned char)(ssl
->version
>> 8);
1196 header
[10] = (unsigned char)(ssl
->version
);
1197 header
[11] = (unsigned char)(rec
->length
>> 8);
1198 header
[12] = (unsigned char)(rec
->length
& 0xff);
1200 if (!sending
&& !SSL_READ_ETM(ssl
) &&
1201 EVP_CIPHER_CTX_mode(ssl
->enc_read_ctx
) == EVP_CIPH_CBC_MODE
&&
1202 ssl3_cbc_record_digest_supported(mac_ctx
)) {
1204 * This is a CBC-encrypted record. We must avoid leaking any
1205 * timing-side channel information about how many blocks of data we
1206 * are hashing because that gives an attacker a timing-oracle.
1208 /* Final param == not SSLv3 */
1209 if (ssl3_cbc_digest_record(mac_ctx
,
1212 rec
->length
+ md_size
, rec
->orig_len
,
1213 ssl
->s3
->read_mac_secret
,
1214 ssl
->s3
->read_mac_secret_size
, 0) <= 0) {
1215 EVP_MD_CTX_free(hmac
);
1219 /* TODO(size_t): Convert these calls */
1220 if (EVP_DigestSignUpdate(mac_ctx
, header
, sizeof(header
)) <= 0
1221 || EVP_DigestSignUpdate(mac_ctx
, rec
->input
, rec
->length
) <= 0
1222 || EVP_DigestSignFinal(mac_ctx
, md
, &md_size
) <= 0) {
1223 EVP_MD_CTX_free(hmac
);
1228 EVP_MD_CTX_free(hmac
);
1231 fprintf(stderr
, "seq=");
1234 for (z
= 0; z
< 8; z
++)
1235 fprintf(stderr
, "%02X ", seq
[z
]);
1236 fprintf(stderr
, "\n");
1238 fprintf(stderr
, "rec=");
1241 for (z
= 0; z
< rec
->length
; z
++)
1242 fprintf(stderr
, "%02X ", rec
->data
[z
]);
1243 fprintf(stderr
, "\n");
1247 if (!SSL_IS_DTLS(ssl
)) {
1248 for (i
= 7; i
>= 0; i
--) {
1257 for (z
= 0; z
< md_size
; z
++)
1258 fprintf(stderr
, "%02X ", md
[z
]);
1259 fprintf(stderr
, "\n");
1266 * ssl3_cbc_remove_padding removes padding from the decrypted, SSLv3, CBC
1267 * record in |rec| by updating |rec->length| in constant time.
1269 * block_size: the block size of the cipher used to encrypt the record.
1271 * 0: (in non-constant time) if the record is publicly invalid.
1272 * 1: if the padding was valid
1275 int ssl3_cbc_remove_padding(SSL3_RECORD
*rec
,
1276 size_t block_size
, size_t mac_size
)
1278 size_t padding_length
;
1280 const size_t overhead
= 1 /* padding length byte */ + mac_size
;
1283 * These lengths are all public so we can test them in non-constant time.
1285 if (overhead
> rec
->length
)
1288 padding_length
= rec
->data
[rec
->length
- 1];
1289 good
= constant_time_ge_s(rec
->length
, padding_length
+ overhead
);
1290 /* SSLv3 requires that the padding is minimal. */
1291 good
&= constant_time_ge_s(block_size
, padding_length
+ 1);
1292 rec
->length
-= good
& (padding_length
+ 1);
1293 return constant_time_select_int_s(good
, 1, -1);
1297 * tls1_cbc_remove_padding removes the CBC padding from the decrypted, TLS, CBC
1298 * record in |rec| in constant time and returns 1 if the padding is valid and
1299 * -1 otherwise. It also removes any explicit IV from the start of the record
1300 * without leaking any timing about whether there was enough space after the
1301 * padding was removed.
1303 * block_size: the block size of the cipher used to encrypt the record.
1305 * 0: (in non-constant time) if the record is publicly invalid.
1306 * 1: if the padding was valid
1309 int tls1_cbc_remove_padding(const SSL
*s
,
1311 size_t block_size
, size_t mac_size
)
1314 size_t padding_length
, to_check
, i
;
1315 const size_t overhead
= 1 /* padding length byte */ + mac_size
;
1316 /* Check if version requires explicit IV */
1317 if (SSL_USE_EXPLICIT_IV(s
)) {
1319 * These lengths are all public so we can test them in non-constant
1322 if (overhead
+ block_size
> rec
->length
)
1324 /* We can now safely skip explicit IV */
1325 rec
->data
+= block_size
;
1326 rec
->input
+= block_size
;
1327 rec
->length
-= block_size
;
1328 rec
->orig_len
-= block_size
;
1329 } else if (overhead
> rec
->length
)
1332 padding_length
= rec
->data
[rec
->length
- 1];
1334 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s
->enc_read_ctx
)) &
1335 EVP_CIPH_FLAG_AEAD_CIPHER
) {
1336 /* padding is already verified */
1337 rec
->length
-= padding_length
+ 1;
1341 good
= constant_time_ge_s(rec
->length
, overhead
+ padding_length
);
1343 * The padding consists of a length byte at the end of the record and
1344 * then that many bytes of padding, all with the same value as the length
1345 * byte. Thus, with the length byte included, there are i+1 bytes of
1346 * padding. We can't check just |padding_length+1| bytes because that
1347 * leaks decrypted information. Therefore we always have to check the
1348 * maximum amount of padding possible. (Again, the length of the record
1349 * is public information so we can use it.)
1351 to_check
= 256; /* maximum amount of padding, inc length byte. */
1352 if (to_check
> rec
->length
)
1353 to_check
= rec
->length
;
1355 for (i
= 0; i
< to_check
; i
++) {
1356 unsigned char mask
= constant_time_ge_8_s(padding_length
, i
);
1357 unsigned char b
= rec
->data
[rec
->length
- 1 - i
];
1359 * The final |padding_length+1| bytes should all have the value
1360 * |padding_length|. Therefore the XOR should be zero.
1362 good
&= ~(mask
& (padding_length
^ b
));
1366 * If any of the final |padding_length+1| bytes had the wrong value, one
1367 * or more of the lower eight bits of |good| will be cleared.
1369 good
= constant_time_eq_s(0xff, good
& 0xff);
1370 rec
->length
-= good
& (padding_length
+ 1);
1372 return constant_time_select_int_s(good
, 1, -1);
1376 * ssl3_cbc_copy_mac copies |md_size| bytes from the end of |rec| to |out| in
1377 * constant time (independent of the concrete value of rec->length, which may
1378 * vary within a 256-byte window).
1380 * ssl3_cbc_remove_padding or tls1_cbc_remove_padding must be called prior to
1384 * rec->orig_len >= md_size
1385 * md_size <= EVP_MAX_MD_SIZE
1387 * If CBC_MAC_ROTATE_IN_PLACE is defined then the rotation is performed with
1388 * variable accesses in a 64-byte-aligned buffer. Assuming that this fits into
1389 * a single or pair of cache-lines, then the variable memory accesses don't
1390 * actually affect the timing. CPUs with smaller cache-lines [if any] are
1391 * not multi-core and are not considered vulnerable to cache-timing attacks.
1393 #define CBC_MAC_ROTATE_IN_PLACE
1395 void ssl3_cbc_copy_mac(unsigned char *out
,
1396 const SSL3_RECORD
*rec
, size_t md_size
)
1398 #if defined(CBC_MAC_ROTATE_IN_PLACE)
1399 unsigned char rotated_mac_buf
[64 + EVP_MAX_MD_SIZE
];
1400 unsigned char *rotated_mac
;
1402 unsigned char rotated_mac
[EVP_MAX_MD_SIZE
];
1406 * mac_end is the index of |rec->data| just after the end of the MAC.
1408 size_t mac_end
= rec
->length
;
1409 size_t mac_start
= mac_end
- md_size
;
1412 * scan_start contains the number of bytes that we can ignore because the
1413 * MAC's position can only vary by 255 bytes.
1415 size_t scan_start
= 0;
1417 size_t rotate_offset
;
1419 OPENSSL_assert(rec
->orig_len
>= md_size
);
1420 OPENSSL_assert(md_size
<= EVP_MAX_MD_SIZE
);
1422 #if defined(CBC_MAC_ROTATE_IN_PLACE)
1423 rotated_mac
= rotated_mac_buf
+ ((0 - (size_t)rotated_mac_buf
) & 63);
1426 /* This information is public so it's safe to branch based on it. */
1427 if (rec
->orig_len
> md_size
+ 255 + 1)
1428 scan_start
= rec
->orig_len
- (md_size
+ 255 + 1);
1432 memset(rotated_mac
, 0, md_size
);
1433 for (i
= scan_start
, j
= 0; i
< rec
->orig_len
; i
++) {
1434 size_t mac_started
= constant_time_eq_s(i
, mac_start
);
1435 size_t mac_ended
= constant_time_lt_s(i
, mac_end
);
1436 unsigned char b
= rec
->data
[i
];
1438 in_mac
|= mac_started
;
1439 in_mac
&= mac_ended
;
1440 rotate_offset
|= j
& mac_started
;
1441 rotated_mac
[j
++] |= b
& in_mac
;
1442 j
&= constant_time_lt_s(j
, md_size
);
1445 /* Now rotate the MAC */
1446 #if defined(CBC_MAC_ROTATE_IN_PLACE)
1448 for (i
= 0; i
< md_size
; i
++) {
1449 /* in case cache-line is 32 bytes, touch second line */
1450 ((volatile unsigned char *)rotated_mac
)[rotate_offset
^ 32];
1451 out
[j
++] = rotated_mac
[rotate_offset
++];
1452 rotate_offset
&= constant_time_lt_s(rotate_offset
, md_size
);
1455 memset(out
, 0, md_size
);
1456 rotate_offset
= md_size
- rotate_offset
;
1457 rotate_offset
&= constant_time_lt_s(rotate_offset
, md_size
);
1458 for (i
= 0; i
< md_size
; i
++) {
1459 for (j
= 0; j
< md_size
; j
++)
1460 out
[j
] |= rotated_mac
[i
] & constant_time_eq_8_s(j
, rotate_offset
);
1462 rotate_offset
&= constant_time_lt_s(rotate_offset
, md_size
);
1467 int dtls1_process_record(SSL
*s
, DTLS1_BITMAP
*bitmap
)
1475 unsigned char md
[EVP_MAX_MD_SIZE
];
1477 rr
= RECORD_LAYER_get_rrec(&s
->rlayer
);
1481 * At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length,
1482 * and we have that many bytes in s->packet
1484 rr
->input
= &(RECORD_LAYER_get_packet(&s
->rlayer
)[DTLS1_RT_HEADER_LENGTH
]);
1487 * ok, we can now read from 's->packet' data into 'rr' rr->input points
1488 * at rr->length bytes, which need to be copied into rr->data by either
1489 * the decryption or by the decompression When the data is 'copied' into
1490 * the rr->data buffer, rr->input will be pointed at the new buffer
1494 * We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
1495 * bytes of encrypted compressed stuff.
1498 /* check is not needed I believe */
1499 if (rr
->length
> SSL3_RT_MAX_ENCRYPTED_LENGTH
) {
1500 al
= SSL_AD_RECORD_OVERFLOW
;
1501 SSLerr(SSL_F_DTLS1_PROCESS_RECORD
, SSL_R_ENCRYPTED_LENGTH_TOO_LONG
);
1505 /* decrypt in place in 'rr->input' */
1506 rr
->data
= rr
->input
;
1507 rr
->orig_len
= rr
->length
;
1509 if (SSL_READ_ETM(s
) && s
->read_hash
) {
1511 mac_size
= EVP_MD_CTX_size(s
->read_hash
);
1512 OPENSSL_assert(mac_size
<= EVP_MAX_MD_SIZE
);
1513 if (rr
->orig_len
< mac_size
) {
1514 al
= SSL_AD_DECODE_ERROR
;
1515 SSLerr(SSL_F_DTLS1_PROCESS_RECORD
, SSL_R_LENGTH_TOO_SHORT
);
1518 rr
->length
-= mac_size
;
1519 mac
= rr
->data
+ rr
->length
;
1520 i
= s
->method
->ssl3_enc
->mac(s
, rr
, md
, 0 /* not send */ );
1521 if (i
== 0 || CRYPTO_memcmp(md
, mac
, (size_t)mac_size
) != 0) {
1522 al
= SSL_AD_BAD_RECORD_MAC
;
1523 SSLerr(SSL_F_DTLS1_PROCESS_RECORD
,
1524 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC
);
1529 enc_err
= s
->method
->ssl3_enc
->enc(s
, rr
, 1, 0);
1532 * 0: (in non-constant time) if the record is publically invalid.
1533 * 1: if the padding is valid
1534 * -1: if the padding is invalid
1537 /* For DTLS we simply ignore bad packets. */
1539 RECORD_LAYER_reset_packet_length(&s
->rlayer
);
1543 printf("dec %ld\n", rr
->length
);
1546 for (z
= 0; z
< rr
->length
; z
++)
1547 printf("%02X%c", rr
->data
[z
], ((z
+ 1) % 16) ? ' ' : '\n');
1552 /* r->length is now the compressed data plus mac */
1553 if ((sess
!= NULL
) && !SSL_READ_ETM(s
) &&
1554 (s
->enc_read_ctx
!= NULL
) && (EVP_MD_CTX_md(s
->read_hash
) != NULL
)) {
1555 /* s->read_hash != NULL => mac_size != -1 */
1556 unsigned char *mac
= NULL
;
1557 unsigned char mac_tmp
[EVP_MAX_MD_SIZE
];
1559 /* TODO(size_t): Convert this to do size_t properly */
1560 imac_size
= EVP_MD_CTX_size(s
->read_hash
);
1561 if (imac_size
< 0) {
1562 al
= SSL_AD_INTERNAL_ERROR
;
1563 SSLerr(SSL_F_DTLS1_PROCESS_RECORD
, ERR_LIB_EVP
);
1566 mac_size
= (size_t)imac_size
;
1567 OPENSSL_assert(mac_size
<= EVP_MAX_MD_SIZE
);
1570 * orig_len is the length of the record before any padding was
1571 * removed. This is public information, as is the MAC in use,
1572 * therefore we can safely process the record in a different amount
1573 * of time if it's too short to possibly contain a MAC.
1575 if (rr
->orig_len
< mac_size
||
1576 /* CBC records must have a padding length byte too. */
1577 (EVP_CIPHER_CTX_mode(s
->enc_read_ctx
) == EVP_CIPH_CBC_MODE
&&
1578 rr
->orig_len
< mac_size
+ 1)) {
1579 al
= SSL_AD_DECODE_ERROR
;
1580 SSLerr(SSL_F_DTLS1_PROCESS_RECORD
, SSL_R_LENGTH_TOO_SHORT
);
1584 if (EVP_CIPHER_CTX_mode(s
->enc_read_ctx
) == EVP_CIPH_CBC_MODE
) {
1586 * We update the length so that the TLS header bytes can be
1587 * constructed correctly but we need to extract the MAC in
1588 * constant time from within the record, without leaking the
1589 * contents of the padding bytes.
1592 ssl3_cbc_copy_mac(mac_tmp
, rr
, mac_size
);
1593 rr
->length
-= mac_size
;
1596 * In this case there's no padding, so |rec->orig_len| equals
1597 * |rec->length| and we checked that there's enough bytes for
1600 rr
->length
-= mac_size
;
1601 mac
= &rr
->data
[rr
->length
];
1604 i
= s
->method
->ssl3_enc
->mac(s
, rr
, md
, 0 /* not send */ );
1605 if (i
== 0 || mac
== NULL
1606 || CRYPTO_memcmp(md
, mac
, mac_size
) != 0)
1608 if (rr
->length
> SSL3_RT_MAX_COMPRESSED_LENGTH
+ mac_size
)
1613 /* decryption failed, silently discard message */
1615 RECORD_LAYER_reset_packet_length(&s
->rlayer
);
1619 /* r->length is now just compressed */
1620 if (s
->expand
!= NULL
) {
1621 if (rr
->length
> SSL3_RT_MAX_COMPRESSED_LENGTH
) {
1622 al
= SSL_AD_RECORD_OVERFLOW
;
1623 SSLerr(SSL_F_DTLS1_PROCESS_RECORD
,
1624 SSL_R_COMPRESSED_LENGTH_TOO_LONG
);
1627 if (!ssl3_do_uncompress(s
, rr
)) {
1628 al
= SSL_AD_DECOMPRESSION_FAILURE
;
1629 SSLerr(SSL_F_DTLS1_PROCESS_RECORD
, SSL_R_BAD_DECOMPRESSION
);
1634 if (rr
->length
> SSL3_RT_MAX_PLAIN_LENGTH
) {
1635 al
= SSL_AD_RECORD_OVERFLOW
;
1636 SSLerr(SSL_F_DTLS1_PROCESS_RECORD
, SSL_R_DATA_LENGTH_TOO_LONG
);
1642 * So at this point the following is true
1643 * ssl->s3->rrec.type is the type of record
1644 * ssl->s3->rrec.length == number of bytes in record
1645 * ssl->s3->rrec.off == offset to first valid byte
1646 * ssl->s3->rrec.data == where to take bytes from, increment
1650 /* we have pulled in a full packet so zero things */
1651 RECORD_LAYER_reset_packet_length(&s
->rlayer
);
1653 /* Mark receipt of record. */
1654 dtls1_record_bitmap_update(s
, bitmap
);
1659 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1665 * retrieve a buffered record that belongs to the current epoch, ie,
1668 #define dtls1_get_processed_record(s) \
1669 dtls1_retrieve_buffered_record((s), \
1670 &(DTLS_RECORD_LAYER_get_processed_rcds(&s->rlayer)))
1673 * Call this to get a new input record.
1674 * It will return <= 0 if more data is needed, normally due to an error
1675 * or non-blocking IO.
1676 * When it finishes, one packet has been decoded and can be found in
1677 * ssl->s3->rrec.type - is the type of record
1678 * ssl->s3->rrec.data, - data
1679 * ssl->s3->rrec.length, - number of bytes
1681 /* used only by dtls1_read_bytes */
1682 int dtls1_get_record(SSL
*s
)
1684 int ssl_major
, ssl_minor
;
1688 unsigned char *p
= NULL
;
1689 unsigned short version
;
1690 DTLS1_BITMAP
*bitmap
;
1691 unsigned int is_next_epoch
;
1693 rr
= RECORD_LAYER_get_rrec(&s
->rlayer
);
1697 * The epoch may have changed. If so, process all the pending records.
1698 * This is a non-blocking operation.
1700 if (!dtls1_process_buffered_records(s
))
1703 /* if we're renegotiating, then there may be buffered records */
1704 if (dtls1_get_processed_record(s
))
1707 /* get something from the wire */
1709 /* check if we have the header */
1710 if ((RECORD_LAYER_get_rstate(&s
->rlayer
) != SSL_ST_READ_BODY
) ||
1711 (RECORD_LAYER_get_packet_length(&s
->rlayer
) < DTLS1_RT_HEADER_LENGTH
)) {
1712 rret
= ssl3_read_n(s
, DTLS1_RT_HEADER_LENGTH
,
1713 SSL3_BUFFER_get_len(&s
->rlayer
.rbuf
), 0, 1, &n
);
1714 /* read timeout is handled by dtls1_read_bytes */
1716 return rret
; /* error or non-blocking */
1718 /* this packet contained a partial record, dump it */
1719 if (RECORD_LAYER_get_packet_length(&s
->rlayer
) !=
1720 DTLS1_RT_HEADER_LENGTH
) {
1721 RECORD_LAYER_reset_packet_length(&s
->rlayer
);
1725 RECORD_LAYER_set_rstate(&s
->rlayer
, SSL_ST_READ_BODY
);
1727 p
= RECORD_LAYER_get_packet(&s
->rlayer
);
1729 if (s
->msg_callback
)
1730 s
->msg_callback(0, 0, SSL3_RT_HEADER
, p
, DTLS1_RT_HEADER_LENGTH
,
1731 s
, s
->msg_callback_arg
);
1733 /* Pull apart the header into the DTLS1_RECORD */
1737 version
= (ssl_major
<< 8) | ssl_minor
;
1739 /* sequence number is 64 bits, with top 2 bytes = epoch */
1742 memcpy(&(RECORD_LAYER_get_read_sequence(&s
->rlayer
)[2]), p
, 6);
1747 /* Lets check version */
1748 if (!s
->first_packet
) {
1749 if (version
!= s
->version
) {
1750 /* unexpected version, silently discard */
1752 RECORD_LAYER_reset_packet_length(&s
->rlayer
);
1757 if ((version
& 0xff00) != (s
->version
& 0xff00)) {
1758 /* wrong version, silently discard record */
1760 RECORD_LAYER_reset_packet_length(&s
->rlayer
);
1764 if (rr
->length
> SSL3_RT_MAX_ENCRYPTED_LENGTH
) {
1765 /* record too long, silently discard it */
1767 RECORD_LAYER_reset_packet_length(&s
->rlayer
);
1771 /* now s->rlayer.rstate == SSL_ST_READ_BODY */
1774 /* s->rlayer.rstate == SSL_ST_READ_BODY, get and decode the data */
1777 RECORD_LAYER_get_packet_length(&s
->rlayer
) - DTLS1_RT_HEADER_LENGTH
) {
1778 /* now s->packet_length == DTLS1_RT_HEADER_LENGTH */
1780 rret
= ssl3_read_n(s
, more
, more
, 1, 1, &n
);
1781 /* this packet contained a partial record, dump it */
1782 if (rret
<= 0 || n
!= more
) {
1784 RECORD_LAYER_reset_packet_length(&s
->rlayer
);
1789 * now n == rr->length, and s->packet_length ==
1790 * DTLS1_RT_HEADER_LENGTH + rr->length
1793 /* set state for later operations */
1794 RECORD_LAYER_set_rstate(&s
->rlayer
, SSL_ST_READ_HEADER
);
1796 /* match epochs. NULL means the packet is dropped on the floor */
1797 bitmap
= dtls1_get_bitmap(s
, rr
, &is_next_epoch
);
1798 if (bitmap
== NULL
) {
1800 RECORD_LAYER_reset_packet_length(&s
->rlayer
); /* dump this record */
1801 goto again
; /* get another record */
1803 #ifndef OPENSSL_NO_SCTP
1804 /* Only do replay check if no SCTP bio */
1805 if (!BIO_dgram_is_sctp(SSL_get_rbio(s
))) {
1807 /* Check whether this is a repeat, or aged record. */
1809 * TODO: Does it make sense to have replay protection in epoch 0 where
1810 * we have no integrity negotiated yet?
1812 if (!dtls1_record_replay_check(s
, bitmap
)) {
1814 RECORD_LAYER_reset_packet_length(&s
->rlayer
); /* dump this record */
1815 goto again
; /* get another record */
1817 #ifndef OPENSSL_NO_SCTP
1821 /* just read a 0 length packet */
1822 if (rr
->length
== 0)
1826 * If this record is from the next epoch (either HM or ALERT), and a
1827 * handshake is currently in progress, buffer it since it cannot be
1828 * processed at this time.
1830 if (is_next_epoch
) {
1831 if ((SSL_in_init(s
) || ossl_statem_get_in_handshake(s
))) {
1832 if (dtls1_buffer_record
1833 (s
, &(DTLS_RECORD_LAYER_get_unprocessed_rcds(&s
->rlayer
)),
1838 RECORD_LAYER_reset_packet_length(&s
->rlayer
);
1842 if (!dtls1_process_record(s
, bitmap
)) {
1844 RECORD_LAYER_reset_packet_length(&s
->rlayer
); /* dump this record */
1845 goto again
; /* get another record */