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
10 #include "../ssl_locl.h"
11 #include "internal/constant_time_locl.h"
12 #include <openssl/rand.h>
13 #include "record_locl.h"
15 static const unsigned char ssl3_pad_1[48] = {
16 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
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
24 static const unsigned char ssl3_pad_2[48] = {
25 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
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
34 * Clear the contents of an SSL3_RECORD but retain any memory allocated
36 void SSL3_RECORD_clear(SSL3_RECORD *r, size_t num_recs)
41 for (i = 0; i < num_recs; i++) {
44 memset(&r[i], 0, sizeof(*r));
49 void SSL3_RECORD_release(SSL3_RECORD *r, size_t num_recs)
53 for (i = 0; i < num_recs; i++) {
54 OPENSSL_free(r[i].comp);
59 void SSL3_RECORD_set_seq_num(SSL3_RECORD *r, const unsigned char *seq_num)
61 memcpy(r->seq_num, seq_num, SEQ_NUM_SIZE);
65 * Peeks ahead into "read_ahead" data to see if we have a whole record waiting
66 * for us in the buffer.
68 static int ssl3_record_app_data_waiting(SSL *s)
74 rbuf = RECORD_LAYER_get_rbuf(&s->rlayer);
76 p = SSL3_BUFFER_get_buf(rbuf);
80 left = SSL3_BUFFER_get_left(rbuf);
82 if (left < SSL3_RT_HEADER_LENGTH)
85 p += SSL3_BUFFER_get_offset(rbuf);
88 * We only check the type and record length, we will sanity check version
91 if (*p != SSL3_RT_APPLICATION_DATA)
97 if (left < SSL3_RT_HEADER_LENGTH + len)
104 * MAX_EMPTY_RECORDS defines the number of consecutive, empty records that
105 * will be processed per call to ssl3_get_record. Without this limit an
106 * attacker could send empty records at a faster rate than we can process and
107 * cause ssl3_get_record to loop forever.
109 #define MAX_EMPTY_RECORDS 32
111 #define SSL2_RT_HEADER_LENGTH 2
113 * Call this to get new input records.
114 * It will return <= 0 if more data is needed, normally due to an error
115 * or non-blocking IO.
116 * When it finishes, |numrpipes| records have been decoded. For each record 'i':
117 * rr[i].type - is the type of record
119 * rr[i].length, - number of bytes
120 * Multiple records will only be returned if the record types are all
121 * SSL3_RT_APPLICATION_DATA. The number of records returned will always be <=
124 /* used only by ssl3_read_bytes */
125 int ssl3_get_record(SSL *s)
127 int ssl_major, ssl_minor, al;
128 int enc_err, rret, ret = -1;
135 unsigned char md[EVP_MAX_MD_SIZE];
139 size_t num_recs = 0, max_recs, j;
141 rr = RECORD_LAYER_get_rrec(&s->rlayer);
142 rbuf = RECORD_LAYER_get_rbuf(&s->rlayer);
143 max_recs = s->max_pipelines;
149 /* check if we have the header */
150 if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||
151 (RECORD_LAYER_get_packet_length(&s->rlayer)
152 < SSL3_RT_HEADER_LENGTH)) {
153 rret = ssl3_read_n(s, SSL3_RT_HEADER_LENGTH,
154 SSL3_BUFFER_get_len(rbuf), 0,
155 num_recs == 0 ? 1 : 0, &n);
157 return rret; /* error or non-blocking */
158 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);
160 p = RECORD_LAYER_get_packet(&s->rlayer);
163 * The first record received by the server may be a V2ClientHello.
165 if (s->server && RECORD_LAYER_is_first_record(&s->rlayer)
166 && (p[0] & 0x80) && (p[2] == SSL2_MT_CLIENT_HELLO)) {
170 * |num_recs| here will actually always be 0 because
171 * |num_recs > 0| only ever occurs when we are processing
172 * multiple app data records - which we know isn't the case here
173 * because it is an SSLv2ClientHello. We keep it using
174 * |num_recs| for the sake of consistency
176 rr[num_recs].type = SSL3_RT_HANDSHAKE;
177 rr[num_recs].rec_version = SSL2_VERSION;
179 rr[num_recs].length = ((p[0] & 0x7f) << 8) | p[1];
181 if (rr[num_recs].length > SSL3_BUFFER_get_len(rbuf)
182 - SSL2_RT_HEADER_LENGTH) {
183 al = SSL_AD_RECORD_OVERFLOW;
184 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_PACKET_LENGTH_TOO_LONG);
188 if (rr[num_recs].length < MIN_SSL2_RECORD_LEN) {
189 al = SSL_AD_HANDSHAKE_FAILURE;
190 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT);
194 /* SSLv3+ style record */
196 s->msg_callback(0, 0, SSL3_RT_HEADER, p, 5, s,
197 s->msg_callback_arg);
199 /* Pull apart the header into the SSL3_RECORD */
200 rr[num_recs].type = *(p++);
203 version = (ssl_major << 8) | ssl_minor;
204 rr[num_recs].rec_version = version;
205 /* TODO(size_t): CHECK ME */
206 n2s(p, rr[num_recs].length);
208 /* Lets check version */
209 if (!s->first_packet && version != s->version) {
210 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_WRONG_VERSION_NUMBER);
211 if ((s->version & 0xFF00) == (version & 0xFF00)
212 && !s->enc_write_ctx && !s->write_hash) {
213 if (rr->type == SSL3_RT_ALERT) {
215 * The record is using an incorrect version number,
216 * but what we've got appears to be an alert. We
217 * haven't read the body yet to check whether its a
218 * fatal or not - but chances are it is. We probably
219 * shouldn't send a fatal alert back. We'll just
225 * Send back error using their minor version number :-)
227 s->version = (unsigned short)version;
229 al = SSL_AD_PROTOCOL_VERSION;
233 if ((version >> 8) != SSL3_VERSION_MAJOR) {
234 if (RECORD_LAYER_is_first_record(&s->rlayer)) {
235 /* Go back to start of packet, look at the five bytes
237 p = RECORD_LAYER_get_packet(&s->rlayer);
238 if (strncmp((char *)p, "GET ", 4) == 0 ||
239 strncmp((char *)p, "POST ", 5) == 0 ||
240 strncmp((char *)p, "HEAD ", 5) == 0 ||
241 strncmp((char *)p, "PUT ", 4) == 0) {
242 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_HTTP_REQUEST);
244 } else if (strncmp((char *)p, "CONNE", 5) == 0) {
245 SSLerr(SSL_F_SSL3_GET_RECORD,
246 SSL_R_HTTPS_PROXY_REQUEST);
250 /* Doesn't look like TLS - don't send an alert */
251 SSLerr(SSL_F_SSL3_GET_RECORD,
252 SSL_R_WRONG_VERSION_NUMBER);
255 SSLerr(SSL_F_SSL3_GET_RECORD,
256 SSL_R_WRONG_VERSION_NUMBER);
257 al = SSL_AD_PROTOCOL_VERSION;
262 if (rr[num_recs].length >
263 SSL3_BUFFER_get_len(rbuf) - SSL3_RT_HEADER_LENGTH) {
264 al = SSL_AD_RECORD_OVERFLOW;
265 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_PACKET_LENGTH_TOO_LONG);
270 /* now s->rlayer.rstate == SSL_ST_READ_BODY */
274 * s->rlayer.rstate == SSL_ST_READ_BODY, get and decode the data.
275 * Calculate how much more data we need to read for the rest of the
278 if (rr[num_recs].rec_version == SSL2_VERSION) {
279 more = rr[num_recs].length + SSL2_RT_HEADER_LENGTH
280 - SSL3_RT_HEADER_LENGTH;
282 more = rr[num_recs].length;
285 /* now s->packet_length == SSL3_RT_HEADER_LENGTH */
287 rret = ssl3_read_n(s, more, more, 1, 0, &n);
289 return rret; /* error or non-blocking io */
292 /* set state for later operations */
293 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);
296 * At this point, s->packet_length == SSL3_RT_HEADER_LENGTH + rr->length,
297 * or s->packet_length == SSL2_RT_HEADER_LENGTH + rr->length
298 * and we have that many bytes in s->packet
300 if (rr[num_recs].rec_version == SSL2_VERSION) {
302 &(RECORD_LAYER_get_packet(&s->rlayer)[SSL2_RT_HEADER_LENGTH]);
305 &(RECORD_LAYER_get_packet(&s->rlayer)[SSL3_RT_HEADER_LENGTH]);
309 * ok, we can now read from 's->packet' data into 'rr' rr->input points
310 * at rr->length bytes, which need to be copied into rr->data by either
311 * the decryption or by the decompression When the data is 'copied' into
312 * the rr->data buffer, rr->input will be pointed at the new buffer
316 * We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
317 * bytes of encrypted compressed stuff.
320 /* check is not needed I believe */
321 if (rr[num_recs].length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
322 al = SSL_AD_RECORD_OVERFLOW;
323 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
327 /* decrypt in place in 'rr->input' */
328 rr[num_recs].data = rr[num_recs].input;
329 rr[num_recs].orig_len = rr[num_recs].length;
331 /* Mark this record as not read by upper layers yet */
332 rr[num_recs].read = 0;
336 /* we have pulled in a full packet so zero things */
337 RECORD_LAYER_reset_packet_length(&s->rlayer);
338 RECORD_LAYER_clear_first_record(&s->rlayer);
339 } while (num_recs < max_recs
340 && rr[num_recs - 1].type == SSL3_RT_APPLICATION_DATA
341 && SSL_USE_EXPLICIT_IV(s)
342 && s->enc_read_ctx != NULL
343 && (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_read_ctx))
344 & EVP_CIPH_FLAG_PIPELINE)
345 && ssl3_record_app_data_waiting(s));
348 * If in encrypt-then-mac mode calculate mac from encrypted record. All
349 * the details below are public so no timing details can leak.
351 if (SSL_USE_ETM(s) && s->read_hash) {
353 /* TODO(size_t): convert this to do size_t properly */
354 imac_size = EVP_MD_CTX_size(s->read_hash);
356 al = SSL_AD_INTERNAL_ERROR;
357 SSLerr(SSL_F_SSL3_GET_RECORD, ERR_LIB_EVP);
360 mac_size = (size_t)imac_size;
361 OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
362 for (j = 0; j < num_recs; j++) {
363 if (rr[j].length < mac_size) {
364 al = SSL_AD_DECODE_ERROR;
365 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT);
368 rr[j].length -= mac_size;
369 mac = rr[j].data + rr[j].length;
370 i = s->method->ssl3_enc->mac(s, &rr[j], md, 0 /* not send */ );
371 if (i < 0 || CRYPTO_memcmp(md, mac, mac_size) != 0) {
372 al = SSL_AD_BAD_RECORD_MAC;
373 SSLerr(SSL_F_SSL3_GET_RECORD,
374 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
380 enc_err = s->method->ssl3_enc->enc(s, rr, num_recs, 0);
383 * 0: (in non-constant time) if the record is publically invalid.
384 * 1: if the padding is valid
385 * -1: if the padding is invalid
388 al = SSL_AD_DECRYPTION_FAILED;
389 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_BLOCK_CIPHER_PAD_IS_WRONG);
393 printf("dec %ld\n", rr->length);
396 for (z = 0; z < rr->length; z++)
397 printf("%02X%c", rr->data[z], ((z + 1) % 16) ? ' ' : '\n');
402 /* r->length is now the compressed data plus mac */
403 if ((sess != NULL) &&
404 (s->enc_read_ctx != NULL) &&
405 (EVP_MD_CTX_md(s->read_hash) != NULL) && !SSL_USE_ETM(s)) {
406 /* s->read_hash != NULL => mac_size != -1 */
407 unsigned char *mac = NULL;
408 unsigned char mac_tmp[EVP_MAX_MD_SIZE];
410 mac_size = EVP_MD_CTX_size(s->read_hash);
411 OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
413 for (j = 0; j < num_recs; j++) {
415 * orig_len is the length of the record before any padding was
416 * removed. This is public information, as is the MAC in use,
417 * therefore we can safely process the record in a different amount
418 * of time if it's too short to possibly contain a MAC.
420 if (rr[j].orig_len < mac_size ||
421 /* CBC records must have a padding length byte too. */
422 (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
423 rr[j].orig_len < mac_size + 1)) {
424 al = SSL_AD_DECODE_ERROR;
425 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT);
429 if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {
431 * We update the length so that the TLS header bytes can be
432 * constructed correctly but we need to extract the MAC in
433 * constant time from within the record, without leaking the
434 * contents of the padding bytes.
437 ssl3_cbc_copy_mac(mac_tmp, &rr[j], mac_size);
438 rr[j].length -= mac_size;
441 * In this case there's no padding, so |rec->orig_len| equals
442 * |rec->length| and we checked that there's enough bytes for
445 rr[j].length -= mac_size;
446 mac = &rr[j].data[rr[j].length];
449 i = s->method->ssl3_enc->mac(s, &rr[j], md, 0 /* not send */ );
450 if (i < 0 || mac == NULL
451 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0)
453 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
460 * A separate 'decryption_failed' alert was introduced with TLS 1.0,
461 * SSL 3.0 only has 'bad_record_mac'. But unless a decryption
462 * failure is directly visible from the ciphertext anyway, we should
463 * not reveal which kind of error occurred -- this might become
464 * visible to an attacker (e.g. via a logfile)
466 al = SSL_AD_BAD_RECORD_MAC;
467 SSLerr(SSL_F_SSL3_GET_RECORD,
468 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
472 for (j = 0; j < num_recs; j++) {
473 /* rr[j].length is now just compressed */
474 if (s->expand != NULL) {
475 if (rr[j].length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
476 al = SSL_AD_RECORD_OVERFLOW;
477 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_COMPRESSED_LENGTH_TOO_LONG);
480 if (!ssl3_do_uncompress(s, &rr[j])) {
481 al = SSL_AD_DECOMPRESSION_FAILURE;
482 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_BAD_DECOMPRESSION);
487 if (rr[j].length > SSL3_RT_MAX_PLAIN_LENGTH) {
488 al = SSL_AD_RECORD_OVERFLOW;
489 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_DATA_LENGTH_TOO_LONG);
495 * So at this point the following is true
496 * rr[j].type is the type of record
497 * rr[j].length == number of bytes in record
498 * rr[j].off == offset to first valid byte
499 * rr[j].data == where to take bytes from, increment after use :-).
502 /* just read a 0 length packet */
503 if (rr[j].length == 0) {
504 RECORD_LAYER_inc_empty_record_count(&s->rlayer);
505 if (RECORD_LAYER_get_empty_record_count(&s->rlayer)
506 > MAX_EMPTY_RECORDS) {
507 al = SSL_AD_UNEXPECTED_MESSAGE;
508 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_RECORD_TOO_SMALL);
512 RECORD_LAYER_reset_empty_record_count(&s->rlayer);
516 RECORD_LAYER_set_numrpipes(&s->rlayer, num_recs);
520 ssl3_send_alert(s, SSL3_AL_FATAL, al);
525 int ssl3_do_uncompress(SSL *ssl, SSL3_RECORD *rr)
527 #ifndef OPENSSL_NO_COMP
530 if (rr->comp == NULL) {
531 rr->comp = (unsigned char *)
532 OPENSSL_malloc(SSL3_RT_MAX_ENCRYPTED_LENGTH);
534 if (rr->comp == NULL)
537 /* TODO(size_t): Convert this call */
538 i = COMP_expand_block(ssl->expand, rr->comp,
539 SSL3_RT_MAX_PLAIN_LENGTH, rr->data, (int)rr->length);
549 int ssl3_do_compress(SSL *ssl, SSL3_RECORD *wr)
551 #ifndef OPENSSL_NO_COMP
554 /* TODO(size_t): Convert this call */
555 i = COMP_compress_block(ssl->compress, wr->data,
556 SSL3_RT_MAX_COMPRESSED_LENGTH,
557 wr->input, (int)wr->length);
563 wr->input = wr->data;
569 * ssl3_enc encrypts/decrypts |n_recs| records in |inrecs|
572 * 0: (in non-constant time) if the record is publically invalid (i.e. too
574 * 1: if the record's padding is valid / the encryption was successful.
575 * -1: if the record's padding is invalid or, if sending, an internal error
578 int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, size_t n_recs, int send)
583 size_t bs, mac_size = 0;
585 const EVP_CIPHER *enc;
589 * We shouldn't ever be called with more than one record in the SSLv3 case
594 ds = s->enc_write_ctx;
595 if (s->enc_write_ctx == NULL)
598 enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
600 ds = s->enc_read_ctx;
601 if (s->enc_read_ctx == NULL)
604 enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
607 if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
608 memmove(rec->data, rec->input, rec->length);
609 rec->input = rec->data;
612 /* TODO(size_t): Convert this call */
613 bs = EVP_CIPHER_CTX_block_size(ds);
617 if ((bs != 1) && send) {
618 i = bs - ((int)l % bs);
620 /* we need to add 'i-1' padding bytes */
623 * the last of these zero bytes will be overwritten with the
626 memset(&rec->input[rec->length], 0, i);
628 rec->input[l - 1] = (i - 1);
632 if (l == 0 || l % bs != 0)
634 /* otherwise, rec->length >= bs */
637 /* TODO(size_t): Convert this call */
638 if (EVP_Cipher(ds, rec->data, rec->input, l) < 1)
641 if (EVP_MD_CTX_md(s->read_hash) != NULL) {
642 /* TODO(size_t): convert me */
643 imac_size = EVP_MD_CTX_size(s->read_hash);
646 mac_size = (size_t)imac_size;
648 if ((bs != 1) && !send)
649 return ssl3_cbc_remove_padding(rec, bs, mac_size);
654 #define MAX_PADDING 256
656 * tls1_enc encrypts/decrypts |n_recs| in |recs|.
659 * 0: (in non-constant time) if the record is publically invalid (i.e. too
661 * 1: if the record's padding is valid / the encryption was successful.
662 * -1: if the record's padding/AEAD-authenticator is invalid or, if sending,
663 * an internal error occurred.
665 int tls1_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int send)
668 size_t reclen[SSL_MAX_PIPELINES];
669 unsigned char buf[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
670 int i, pad = 0, ret, tmpr;
671 size_t bs, mac_size = 0, ctr, padnum, loop;
672 unsigned char padval;
674 const EVP_CIPHER *enc;
677 if (EVP_MD_CTX_md(s->write_hash)) {
678 int n = EVP_MD_CTX_size(s->write_hash);
679 OPENSSL_assert(n >= 0);
681 ds = s->enc_write_ctx;
682 if (s->enc_write_ctx == NULL)
686 enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
687 /* For TLSv1.1 and later explicit IV */
688 if (SSL_USE_EXPLICIT_IV(s)
689 && EVP_CIPHER_mode(enc) == EVP_CIPH_CBC_MODE)
690 ivlen = EVP_CIPHER_iv_length(enc);
694 for (ctr = 0; ctr < n_recs; ctr++) {
695 if (recs[ctr].data != recs[ctr].input) {
697 * we can't write into the input stream: Can this ever
700 SSLerr(SSL_F_TLS1_ENC, ERR_R_INTERNAL_ERROR);
702 } else if (RAND_bytes(recs[ctr].input, ivlen) <= 0) {
703 SSLerr(SSL_F_TLS1_ENC, ERR_R_INTERNAL_ERROR);
710 if (EVP_MD_CTX_md(s->read_hash)) {
711 int n = EVP_MD_CTX_size(s->read_hash);
712 OPENSSL_assert(n >= 0);
714 ds = s->enc_read_ctx;
715 if (s->enc_read_ctx == NULL)
718 enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
721 if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
722 for (ctr = 0; ctr < n_recs; ctr++) {
723 memmove(recs[ctr].data, recs[ctr].input, recs[ctr].length);
724 recs[ctr].input = recs[ctr].data;
728 bs = EVP_CIPHER_block_size(EVP_CIPHER_CTX_cipher(ds));
731 if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
732 & EVP_CIPH_FLAG_PIPELINE)) {
734 * We shouldn't have been called with pipeline data if the
735 * cipher doesn't support pipelining
737 SSLerr(SSL_F_TLS1_ENC, SSL_R_PIPELINE_FAILURE);
741 for (ctr = 0; ctr < n_recs; ctr++) {
742 reclen[ctr] = recs[ctr].length;
744 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
745 & EVP_CIPH_FLAG_AEAD_CIPHER) {
748 seq = send ? RECORD_LAYER_get_write_sequence(&s->rlayer)
749 : RECORD_LAYER_get_read_sequence(&s->rlayer);
751 if (SSL_IS_DTLS(s)) {
752 /* DTLS does not support pipelining */
753 unsigned char dtlsseq[9], *p = dtlsseq;
755 s2n(send ? DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer) :
756 DTLS_RECORD_LAYER_get_r_epoch(&s->rlayer), p);
757 memcpy(p, &seq[2], 6);
758 memcpy(buf[ctr], dtlsseq, 8);
760 memcpy(buf[ctr], seq, 8);
761 for (i = 7; i >= 0; i--) { /* increment */
768 buf[ctr][8] = recs[ctr].type;
769 buf[ctr][9] = (unsigned char)(s->version >> 8);
770 buf[ctr][10] = (unsigned char)(s->version);
771 buf[ctr][11] = recs[ctr].length >> 8;
772 buf[ctr][12] = recs[ctr].length & 0xff;
773 pad = EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_AEAD_TLS1_AAD,
774 EVP_AEAD_TLS1_AAD_LEN, buf[ctr]);
780 recs[ctr].length += pad;
783 } else if ((bs != 1) && send) {
784 padnum = bs - ((int)reclen[ctr] % bs);
786 /* Add weird padding of upto 256 bytes */
788 if (padnum > MAX_PADDING)
790 /* we need to add 'padnum' padding bytes of value padval */
792 for (loop = reclen[ctr]; loop < reclen[ctr] + padnum; loop++)
793 recs[ctr].input[loop] = padval;
794 reclen[ctr] += padnum;
795 recs[ctr].length += padnum;
799 if (reclen[ctr] == 0 || reclen[ctr] % bs != 0)
804 unsigned char *data[SSL_MAX_PIPELINES];
806 /* Set the output buffers */
807 for (ctr = 0; ctr < n_recs; ctr++) {
808 data[ctr] = recs[ctr].data;
810 if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS,
811 n_recs, data) <= 0) {
812 SSLerr(SSL_F_TLS1_ENC, SSL_R_PIPELINE_FAILURE);
814 /* Set the input buffers */
815 for (ctr = 0; ctr < n_recs; ctr++) {
816 data[ctr] = recs[ctr].input;
818 if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_BUFS,
820 || EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_LENS,
821 n_recs, reclen) <= 0) {
822 SSLerr(SSL_F_TLS1_ENC, SSL_R_PIPELINE_FAILURE);
827 tmpr = EVP_Cipher(ds, recs[0].data, recs[0].input, reclen[0]);
828 if ((EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
829 & EVP_CIPH_FLAG_CUSTOM_CIPHER)
832 return -1; /* AEAD can fail to verify MAC */
834 if (EVP_CIPHER_mode(enc) == EVP_CIPH_GCM_MODE) {
835 for (ctr = 0; ctr < n_recs; ctr++) {
836 recs[ctr].data += EVP_GCM_TLS_EXPLICIT_IV_LEN;
837 recs[ctr].input += EVP_GCM_TLS_EXPLICIT_IV_LEN;
838 recs[ctr].length -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
840 } else if (EVP_CIPHER_mode(enc) == EVP_CIPH_CCM_MODE) {
841 for (ctr = 0; ctr < n_recs; ctr++) {
842 recs[ctr].data += EVP_CCM_TLS_EXPLICIT_IV_LEN;
843 recs[ctr].input += EVP_CCM_TLS_EXPLICIT_IV_LEN;
844 recs[ctr].length -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
850 if (!SSL_USE_ETM(s) && EVP_MD_CTX_md(s->read_hash) != NULL) {
851 imac_size = EVP_MD_CTX_size(s->read_hash);
854 mac_size = (size_t)imac_size;
856 if ((bs != 1) && !send) {
858 for (ctr = 0; ctr < n_recs; ctr++) {
859 tmpret = tls1_cbc_remove_padding(s, &recs[ctr], bs, mac_size);
861 * If tmpret == 0 then this means publicly invalid so we can
862 * short circuit things here. Otherwise we must respect constant
867 ret = constant_time_select_int(constant_time_eq_int(tmpret, 1),
872 for (ctr = 0; ctr < n_recs; ctr++) {
873 recs[ctr].length -= pad;
880 int n_ssl3_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int send)
882 unsigned char *mac_sec, *seq;
883 const EVP_MD_CTX *hash;
884 unsigned char *p, rec_char;
890 mac_sec = &(ssl->s3->write_mac_secret[0]);
891 seq = RECORD_LAYER_get_write_sequence(&ssl->rlayer);
892 hash = ssl->write_hash;
894 mac_sec = &(ssl->s3->read_mac_secret[0]);
895 seq = RECORD_LAYER_get_read_sequence(&ssl->rlayer);
896 hash = ssl->read_hash;
899 t = EVP_MD_CTX_size(hash);
903 npad = (48 / md_size) * md_size;
906 EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
907 ssl3_cbc_record_digest_supported(hash)) {
909 * This is a CBC-encrypted record. We must avoid leaking any
910 * timing-side channel information about how many blocks of data we
911 * are hashing because that gives an attacker a timing-oracle.
915 * npad is, at most, 48 bytes and that's with MD5:
916 * 16 + 48 + 8 (sequence bytes) + 1 + 2 = 75.
918 * With SHA-1 (the largest hash speced for SSLv3) the hash size
919 * goes up 4, but npad goes down by 8, resulting in a smaller
922 unsigned char header[75];
924 memcpy(header + j, mac_sec, md_size);
926 memcpy(header + j, ssl3_pad_1, npad);
928 memcpy(header + j, seq, 8);
930 header[j++] = rec->type;
931 header[j++] = rec->length >> 8;
932 header[j++] = rec->length & 0xff;
934 /* Final param == is SSLv3 */
935 if (ssl3_cbc_digest_record(hash,
938 rec->length + md_size, rec->orig_len,
939 mac_sec, md_size, 1) <= 0)
942 unsigned int md_size_u;
943 /* Chop the digest off the end :-) */
944 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
949 rec_char = rec->type;
952 if (EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
953 || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
954 || EVP_DigestUpdate(md_ctx, ssl3_pad_1, npad) <= 0
955 || EVP_DigestUpdate(md_ctx, seq, 8) <= 0
956 || EVP_DigestUpdate(md_ctx, &rec_char, 1) <= 0
957 || EVP_DigestUpdate(md_ctx, md, 2) <= 0
958 || EVP_DigestUpdate(md_ctx, rec->input, rec->length) <= 0
959 || EVP_DigestFinal_ex(md_ctx, md, NULL) <= 0
960 || EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
961 || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
962 || EVP_DigestUpdate(md_ctx, ssl3_pad_2, npad) <= 0
963 || EVP_DigestUpdate(md_ctx, md, md_size) <= 0
964 || EVP_DigestFinal_ex(md_ctx, md, &md_size_u) <= 0) {
965 EVP_MD_CTX_reset(md_ctx);
970 EVP_MD_CTX_free(md_ctx);
973 ssl3_record_sequence_update(seq);
977 int tls1_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int send)
983 EVP_MD_CTX *hmac = NULL, *mac_ctx;
984 unsigned char header[13];
985 int stream_mac = (send ? (ssl->mac_flags & SSL_MAC_FLAG_WRITE_MAC_STREAM)
986 : (ssl->mac_flags & SSL_MAC_FLAG_READ_MAC_STREAM));
990 seq = RECORD_LAYER_get_write_sequence(&ssl->rlayer);
991 hash = ssl->write_hash;
993 seq = RECORD_LAYER_get_read_sequence(&ssl->rlayer);
994 hash = ssl->read_hash;
997 t = EVP_MD_CTX_size(hash);
998 OPENSSL_assert(t >= 0);
1001 /* I should fix this up TLS TLS TLS TLS TLS XXXXXXXX */
1005 hmac = EVP_MD_CTX_new();
1006 if (hmac == NULL || !EVP_MD_CTX_copy(hmac, hash))
1011 if (SSL_IS_DTLS(ssl)) {
1012 unsigned char dtlsseq[8], *p = dtlsseq;
1014 s2n(send ? DTLS_RECORD_LAYER_get_w_epoch(&ssl->rlayer) :
1015 DTLS_RECORD_LAYER_get_r_epoch(&ssl->rlayer), p);
1016 memcpy(p, &seq[2], 6);
1018 memcpy(header, dtlsseq, 8);
1020 memcpy(header, seq, 8);
1022 header[8] = rec->type;
1023 header[9] = (unsigned char)(ssl->version >> 8);
1024 header[10] = (unsigned char)(ssl->version);
1025 header[11] = (rec->length) >> 8;
1026 header[12] = (rec->length) & 0xff;
1028 if (!send && !SSL_USE_ETM(ssl) &&
1029 EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
1030 ssl3_cbc_record_digest_supported(mac_ctx)) {
1032 * This is a CBC-encrypted record. We must avoid leaking any
1033 * timing-side channel information about how many blocks of data we
1034 * are hashing because that gives an attacker a timing-oracle.
1036 /* Final param == not SSLv3 */
1037 /* TODO(size_t): Convert this call */
1038 if (ssl3_cbc_digest_record(mac_ctx,
1041 rec->length + md_size, rec->orig_len,
1042 ssl->s3->read_mac_secret,
1043 ssl->s3->read_mac_secret_size, 0) <= 0) {
1044 EVP_MD_CTX_free(hmac);
1048 /* TODO(size_t): Convert these calls */
1049 if (EVP_DigestSignUpdate(mac_ctx, header, sizeof(header)) <= 0
1050 || EVP_DigestSignUpdate(mac_ctx, rec->input, rec->length) <= 0
1051 || EVP_DigestSignFinal(mac_ctx, md, &md_size) <= 0) {
1052 EVP_MD_CTX_free(hmac);
1055 if (!send && !SSL_USE_ETM(ssl) && FIPS_mode())
1056 if (!tls_fips_digest_extra(ssl->enc_read_ctx,
1057 mac_ctx, rec->input,
1058 rec->length, rec->orig_len)) {
1059 EVP_MD_CTX_free(hmac);
1064 EVP_MD_CTX_free(hmac);
1067 fprintf(stderr, "seq=");
1070 for (z = 0; z < 8; z++)
1071 fprintf(stderr, "%02X ", seq[z]);
1072 fprintf(stderr, "\n");
1074 fprintf(stderr, "rec=");
1077 for (z = 0; z < rec->length; z++)
1078 fprintf(stderr, "%02X ", rec->data[z]);
1079 fprintf(stderr, "\n");
1083 if (!SSL_IS_DTLS(ssl)) {
1084 for (i = 7; i >= 0; i--) {
1093 for (z = 0; z < md_size; z++)
1094 fprintf(stderr, "%02X ", md[z]);
1095 fprintf(stderr, "\n");
1102 * ssl3_cbc_remove_padding removes padding from the decrypted, SSLv3, CBC
1103 * record in |rec| by updating |rec->length| in constant time.
1105 * block_size: the block size of the cipher used to encrypt the record.
1107 * 0: (in non-constant time) if the record is publicly invalid.
1108 * 1: if the padding was valid
1111 /* TODO(size_t): Convert me */
1112 int ssl3_cbc_remove_padding(SSL3_RECORD *rec,
1113 size_t block_size, size_t mac_size)
1115 size_t padding_length;
1117 const size_t overhead = 1 /* padding length byte */ + mac_size;
1120 * These lengths are all public so we can test them in non-constant time.
1122 if (overhead > rec->length)
1125 padding_length = rec->data[rec->length - 1];
1126 /* TODO(size_t): size_t constant_time ? */
1127 good = constant_time_ge(rec->length, padding_length + overhead);
1128 /* SSLv3 requires that the padding is minimal. */
1129 good &= constant_time_ge(block_size, padding_length + 1);
1130 rec->length -= good & (padding_length + 1);
1131 return constant_time_select_int(good, 1, -1);
1135 * tls1_cbc_remove_padding removes the CBC padding from the decrypted, TLS, CBC
1136 * record in |rec| in constant time and returns 1 if the padding is valid and
1137 * -1 otherwise. It also removes any explicit IV from the start of the record
1138 * without leaking any timing about whether there was enough space after the
1139 * padding was removed.
1141 * block_size: the block size of the cipher used to encrypt the record.
1143 * 0: (in non-constant time) if the record is publicly invalid.
1144 * 1: if the padding was valid
1147 int tls1_cbc_remove_padding(const SSL *s,
1149 size_t block_size, size_t mac_size)
1152 size_t padding_length, to_check, i;
1153 const size_t overhead = 1 /* padding length byte */ + mac_size;
1154 /* Check if version requires explicit IV */
1155 if (SSL_USE_EXPLICIT_IV(s)) {
1157 * These lengths are all public so we can test them in non-constant
1160 if (overhead + block_size > rec->length)
1162 /* We can now safely skip explicit IV */
1163 rec->data += block_size;
1164 rec->input += block_size;
1165 rec->length -= block_size;
1166 rec->orig_len -= block_size;
1167 } else if (overhead > rec->length)
1170 padding_length = rec->data[rec->length - 1];
1172 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_read_ctx)) &
1173 EVP_CIPH_FLAG_AEAD_CIPHER) {
1174 /* padding is already verified */
1175 rec->length -= padding_length + 1;
1179 /* TODO(size_t): size_t constant_time?? */
1180 good = constant_time_ge(rec->length, overhead + padding_length);
1182 * The padding consists of a length byte at the end of the record and
1183 * then that many bytes of padding, all with the same value as the length
1184 * byte. Thus, with the length byte included, there are i+1 bytes of
1185 * padding. We can't check just |padding_length+1| bytes because that
1186 * leaks decrypted information. Therefore we always have to check the
1187 * maximum amount of padding possible. (Again, the length of the record
1188 * is public information so we can use it.)
1190 to_check = 256; /* maximum amount of padding, inc length byte. */
1191 if (to_check > rec->length)
1192 to_check = rec->length;
1194 for (i = 0; i < to_check; i++) {
1195 unsigned char mask = constant_time_ge_8(padding_length, i);
1196 unsigned char b = rec->data[rec->length - 1 - i];
1198 * The final |padding_length+1| bytes should all have the value
1199 * |padding_length|. Therefore the XOR should be zero.
1201 good &= ~(mask & (padding_length ^ b));
1205 * If any of the final |padding_length+1| bytes had the wrong value, one
1206 * or more of the lower eight bits of |good| will be cleared.
1208 good = constant_time_eq(0xff, good & 0xff);
1209 rec->length -= good & (padding_length + 1);
1211 return constant_time_select_int(good, 1, -1);
1215 * ssl3_cbc_copy_mac copies |md_size| bytes from the end of |rec| to |out| in
1216 * constant time (independent of the concrete value of rec->length, which may
1217 * vary within a 256-byte window).
1219 * ssl3_cbc_remove_padding or tls1_cbc_remove_padding must be called prior to
1223 * rec->orig_len >= md_size
1224 * md_size <= EVP_MAX_MD_SIZE
1226 * If CBC_MAC_ROTATE_IN_PLACE is defined then the rotation is performed with
1227 * variable accesses in a 64-byte-aligned buffer. Assuming that this fits into
1228 * a single or pair of cache-lines, then the variable memory accesses don't
1229 * actually affect the timing. CPUs with smaller cache-lines [if any] are
1230 * not multi-core and are not considered vulnerable to cache-timing attacks.
1232 #define CBC_MAC_ROTATE_IN_PLACE
1234 void ssl3_cbc_copy_mac(unsigned char *out,
1235 const SSL3_RECORD *rec, size_t md_size)
1237 #if defined(CBC_MAC_ROTATE_IN_PLACE)
1238 unsigned char rotated_mac_buf[64 + EVP_MAX_MD_SIZE];
1239 unsigned char *rotated_mac;
1241 unsigned char rotated_mac[EVP_MAX_MD_SIZE];
1245 * mac_end is the index of |rec->data| just after the end of the MAC.
1247 size_t mac_end = rec->length;
1248 size_t mac_start = mac_end - md_size;
1250 * scan_start contains the number of bytes that we can ignore because the
1251 * MAC's position can only vary by 255 bytes.
1253 size_t scan_start = 0;
1255 unsigned div_spoiler;
1256 unsigned rotate_offset;
1258 OPENSSL_assert(rec->orig_len >= md_size);
1259 OPENSSL_assert(md_size <= EVP_MAX_MD_SIZE);
1261 #if defined(CBC_MAC_ROTATE_IN_PLACE)
1262 rotated_mac = rotated_mac_buf + ((0 - (size_t)rotated_mac_buf) & 63);
1265 /* This information is public so it's safe to branch based on it. */
1266 if (rec->orig_len > md_size + 255 + 1)
1267 scan_start = rec->orig_len - (md_size + 255 + 1);
1269 * div_spoiler contains a multiple of md_size that is used to cause the
1270 * modulo operation to be constant time. Without this, the time varies
1271 * based on the amount of padding when running on Intel chips at least.
1272 * The aim of right-shifting md_size is so that the compiler doesn't
1273 * figure out that it can remove div_spoiler as that would require it to
1274 * prove that md_size is always even, which I hope is beyond it.
1276 div_spoiler = md_size >> 1;
1277 div_spoiler <<= (sizeof(div_spoiler) - 1) * 8;
1278 rotate_offset = (div_spoiler + mac_start - scan_start) % md_size;
1280 memset(rotated_mac, 0, md_size);
1281 for (i = scan_start, j = 0; i < rec->orig_len; i++) {
1282 /* TODO(size_t): should we have constant_time variants for size_t? */
1283 unsigned char mac_started = constant_time_ge_8(i, mac_start);
1284 unsigned char mac_ended = constant_time_ge_8(i, mac_end);
1285 unsigned char b = rec->data[i];
1286 rotated_mac[j++] |= b & mac_started & ~mac_ended;
1287 j &= constant_time_lt(j, md_size);
1290 /* Now rotate the MAC */
1291 #if defined(CBC_MAC_ROTATE_IN_PLACE)
1293 for (i = 0; i < md_size; i++) {
1294 /* in case cache-line is 32 bytes, touch second line */
1295 ((volatile unsigned char *)rotated_mac)[rotate_offset ^ 32];
1296 out[j++] = rotated_mac[rotate_offset++];
1297 rotate_offset &= constant_time_lt(rotate_offset, md_size);
1300 memset(out, 0, md_size);
1301 rotate_offset = md_size - rotate_offset;
1302 rotate_offset &= constant_time_lt(rotate_offset, md_size);
1303 for (i = 0; i < md_size; i++) {
1304 for (j = 0; j < md_size; j++)
1305 out[j] |= rotated_mac[i] & constant_time_eq_8(j, rotate_offset);
1307 rotate_offset &= constant_time_lt(rotate_offset, md_size);
1312 int dtls1_process_record(SSL *s, DTLS1_BITMAP *bitmap)
1320 unsigned char md[EVP_MAX_MD_SIZE];
1322 rr = RECORD_LAYER_get_rrec(&s->rlayer);
1326 * At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length,
1327 * and we have that many bytes in s->packet
1329 rr->input = &(RECORD_LAYER_get_packet(&s->rlayer)[DTLS1_RT_HEADER_LENGTH]);
1332 * ok, we can now read from 's->packet' data into 'rr' rr->input points
1333 * at rr->length bytes, which need to be copied into rr->data by either
1334 * the decryption or by the decompression When the data is 'copied' into
1335 * the rr->data buffer, rr->input will be pointed at the new buffer
1339 * We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
1340 * bytes of encrypted compressed stuff.
1343 /* check is not needed I believe */
1344 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
1345 al = SSL_AD_RECORD_OVERFLOW;
1346 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
1350 /* decrypt in place in 'rr->input' */
1351 rr->data = rr->input;
1352 rr->orig_len = rr->length;
1354 if (SSL_USE_ETM(s) && s->read_hash) {
1356 mac_size = EVP_MD_CTX_size(s->read_hash);
1357 OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
1358 if (rr->orig_len < mac_size) {
1359 al = SSL_AD_DECODE_ERROR;
1360 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_LENGTH_TOO_SHORT);
1363 rr->length -= mac_size;
1364 mac = rr->data + rr->length;
1365 i = s->method->ssl3_enc->mac(s, rr, md, 0 /* not send */ );
1366 if (i < 0 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0) {
1367 al = SSL_AD_BAD_RECORD_MAC;
1368 SSLerr(SSL_F_DTLS1_PROCESS_RECORD,
1369 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
1374 enc_err = s->method->ssl3_enc->enc(s, rr, 1, 0);
1377 * 0: (in non-constant time) if the record is publically invalid.
1378 * 1: if the padding is valid
1379 * -1: if the padding is invalid
1382 /* For DTLS we simply ignore bad packets. */
1384 RECORD_LAYER_reset_packet_length(&s->rlayer);
1388 printf("dec %ld\n", rr->length);
1391 for (z = 0; z < rr->length; z++)
1392 printf("%02X%c", rr->data[z], ((z + 1) % 16) ? ' ' : '\n');
1397 /* r->length is now the compressed data plus mac */
1398 if ((sess != NULL) && !SSL_USE_ETM(s) &&
1399 (s->enc_read_ctx != NULL) && (EVP_MD_CTX_md(s->read_hash) != NULL)) {
1400 /* s->read_hash != NULL => mac_size != -1 */
1401 unsigned char *mac = NULL;
1402 unsigned char mac_tmp[EVP_MAX_MD_SIZE];
1404 /* TODO(size_t): Convert this to do size_t properly */
1405 imac_size = EVP_MD_CTX_size(s->read_hash);
1406 if (imac_size < 0) {
1407 al = SSL_AD_INTERNAL_ERROR;
1408 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, ERR_LIB_EVP);
1411 mac_size = (size_t)imac_size;
1412 OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
1415 * orig_len is the length of the record before any padding was
1416 * removed. This is public information, as is the MAC in use,
1417 * therefore we can safely process the record in a different amount
1418 * of time if it's too short to possibly contain a MAC.
1420 if (rr->orig_len < mac_size ||
1421 /* CBC records must have a padding length byte too. */
1422 (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
1423 rr->orig_len < mac_size + 1)) {
1424 al = SSL_AD_DECODE_ERROR;
1425 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_LENGTH_TOO_SHORT);
1429 if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {
1431 * We update the length so that the TLS header bytes can be
1432 * constructed correctly but we need to extract the MAC in
1433 * constant time from within the record, without leaking the
1434 * contents of the padding bytes.
1437 ssl3_cbc_copy_mac(mac_tmp, rr, mac_size);
1438 rr->length -= mac_size;
1441 * In this case there's no padding, so |rec->orig_len| equals
1442 * |rec->length| and we checked that there's enough bytes for
1445 rr->length -= mac_size;
1446 mac = &rr->data[rr->length];
1449 i = s->method->ssl3_enc->mac(s, rr, md, 0 /* not send */ );
1450 if (i < 0 || mac == NULL
1451 || CRYPTO_memcmp(md, mac, mac_size) != 0)
1453 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
1458 /* decryption failed, silently discard message */
1460 RECORD_LAYER_reset_packet_length(&s->rlayer);
1464 /* r->length is now just compressed */
1465 if (s->expand != NULL) {
1466 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
1467 al = SSL_AD_RECORD_OVERFLOW;
1468 SSLerr(SSL_F_DTLS1_PROCESS_RECORD,
1469 SSL_R_COMPRESSED_LENGTH_TOO_LONG);
1472 if (!ssl3_do_uncompress(s, rr)) {
1473 al = SSL_AD_DECOMPRESSION_FAILURE;
1474 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_BAD_DECOMPRESSION);
1479 if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH) {
1480 al = SSL_AD_RECORD_OVERFLOW;
1481 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_DATA_LENGTH_TOO_LONG);
1487 * So at this point the following is true
1488 * ssl->s3->rrec.type is the type of record
1489 * ssl->s3->rrec.length == number of bytes in record
1490 * ssl->s3->rrec.off == offset to first valid byte
1491 * ssl->s3->rrec.data == where to take bytes from, increment
1495 /* we have pulled in a full packet so zero things */
1496 RECORD_LAYER_reset_packet_length(&s->rlayer);
1498 /* Mark receipt of record. */
1499 dtls1_record_bitmap_update(s, bitmap);
1504 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1510 * retrieve a buffered record that belongs to the current epoch, ie,
1513 #define dtls1_get_processed_record(s) \
1514 dtls1_retrieve_buffered_record((s), \
1515 &(DTLS_RECORD_LAYER_get_processed_rcds(&s->rlayer)))
1518 * Call this to get a new input record.
1519 * It will return <= 0 if more data is needed, normally due to an error
1520 * or non-blocking IO.
1521 * When it finishes, one packet has been decoded and can be found in
1522 * ssl->s3->rrec.type - is the type of record
1523 * ssl->s3->rrec.data, - data
1524 * ssl->s3->rrec.length, - number of bytes
1526 /* used only by dtls1_read_bytes */
1527 int dtls1_get_record(SSL *s)
1529 int ssl_major, ssl_minor;
1533 unsigned char *p = NULL;
1534 unsigned short version;
1535 DTLS1_BITMAP *bitmap;
1536 unsigned int is_next_epoch;
1538 rr = RECORD_LAYER_get_rrec(&s->rlayer);
1542 * The epoch may have changed. If so, process all the pending records.
1543 * This is a non-blocking operation.
1545 if (!dtls1_process_buffered_records(s))
1548 /* if we're renegotiating, then there may be buffered records */
1549 if (dtls1_get_processed_record(s))
1552 /* get something from the wire */
1554 /* check if we have the header */
1555 if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||
1556 (RECORD_LAYER_get_packet_length(&s->rlayer) < DTLS1_RT_HEADER_LENGTH)) {
1557 rret = ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH,
1558 SSL3_BUFFER_get_len(&s->rlayer.rbuf), 0, 1, &n);
1559 /* read timeout is handled by dtls1_read_bytes */
1561 return rret; /* error or non-blocking */
1563 /* this packet contained a partial record, dump it */
1564 if (RECORD_LAYER_get_packet_length(&s->rlayer) !=
1565 DTLS1_RT_HEADER_LENGTH) {
1566 RECORD_LAYER_reset_packet_length(&s->rlayer);
1570 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);
1572 p = RECORD_LAYER_get_packet(&s->rlayer);
1574 if (s->msg_callback)
1575 s->msg_callback(0, 0, SSL3_RT_HEADER, p, DTLS1_RT_HEADER_LENGTH,
1576 s, s->msg_callback_arg);
1578 /* Pull apart the header into the DTLS1_RECORD */
1582 version = (ssl_major << 8) | ssl_minor;
1584 /* sequence number is 64 bits, with top 2 bytes = epoch */
1587 memcpy(&(RECORD_LAYER_get_read_sequence(&s->rlayer)[2]), p, 6);
1590 /* TODO(size_t): CHECK ME */
1593 /* Lets check version */
1594 if (!s->first_packet) {
1595 if (version != s->version) {
1596 /* unexpected version, silently discard */
1598 RECORD_LAYER_reset_packet_length(&s->rlayer);
1603 if ((version & 0xff00) != (s->version & 0xff00)) {
1604 /* wrong version, silently discard record */
1606 RECORD_LAYER_reset_packet_length(&s->rlayer);
1610 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
1611 /* record too long, silently discard it */
1613 RECORD_LAYER_reset_packet_length(&s->rlayer);
1617 /* now s->rlayer.rstate == SSL_ST_READ_BODY */
1620 /* s->rlayer.rstate == SSL_ST_READ_BODY, get and decode the data */
1623 RECORD_LAYER_get_packet_length(&s->rlayer) - DTLS1_RT_HEADER_LENGTH) {
1624 /* now s->packet_length == DTLS1_RT_HEADER_LENGTH */
1626 rret = ssl3_read_n(s, more, more, 1, 1, &n);
1627 /* this packet contained a partial record, dump it */
1628 if (rret <= 0 || n != more) {
1630 RECORD_LAYER_reset_packet_length(&s->rlayer);
1635 * now n == rr->length, and s->packet_length ==
1636 * DTLS1_RT_HEADER_LENGTH + rr->length
1639 /* set state for later operations */
1640 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);
1642 /* match epochs. NULL means the packet is dropped on the floor */
1643 bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);
1644 if (bitmap == NULL) {
1646 RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
1647 goto again; /* get another record */
1649 #ifndef OPENSSL_NO_SCTP
1650 /* Only do replay check if no SCTP bio */
1651 if (!BIO_dgram_is_sctp(SSL_get_rbio(s))) {
1653 /* Check whether this is a repeat, or aged record. */
1655 * TODO: Does it make sense to have replay protection in epoch 0 where
1656 * we have no integrity negotiated yet?
1658 if (!dtls1_record_replay_check(s, bitmap)) {
1660 RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
1661 goto again; /* get another record */
1663 #ifndef OPENSSL_NO_SCTP
1667 /* just read a 0 length packet */
1668 if (rr->length == 0)
1672 * If this record is from the next epoch (either HM or ALERT), and a
1673 * handshake is currently in progress, buffer it since it cannot be
1674 * processed at this time.
1676 if (is_next_epoch) {
1677 if ((SSL_in_init(s) || ossl_statem_get_in_handshake(s))) {
1678 if (dtls1_buffer_record
1679 (s, &(DTLS_RECORD_LAYER_get_unprocessed_rcds(&s->rlayer)),
1684 RECORD_LAYER_reset_packet_length(&s->rlayer);
1688 if (!dtls1_process_record(s, bitmap)) {
1690 RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
1691 goto again; /* get another record */