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)
128 int enc_err, rret, ret = -1;
135 unsigned char md[EVP_MAX_MD_SIZE];
136 unsigned int version;
139 size_t num_recs = 0, max_recs, j;
140 PACKET pkt, sslv2pkt;
142 rr = RECORD_LAYER_get_rrec(&s->rlayer);
143 rbuf = RECORD_LAYER_get_rbuf(&s->rlayer);
144 max_recs = s->max_pipelines;
150 /* check if we have the header */
151 if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||
152 (RECORD_LAYER_get_packet_length(&s->rlayer)
153 < SSL3_RT_HEADER_LENGTH)) {
157 rret = ssl3_read_n(s, SSL3_RT_HEADER_LENGTH,
158 SSL3_BUFFER_get_len(rbuf), 0,
159 num_recs == 0 ? 1 : 0, &n);
161 return rret; /* error or non-blocking */
162 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);
164 p = RECORD_LAYER_get_packet(&s->rlayer);
165 if (!PACKET_buf_init(&pkt, RECORD_LAYER_get_packet(&s->rlayer),
166 RECORD_LAYER_get_packet_length(&s->rlayer))) {
167 al = SSL_AD_INTERNAL_ERROR;
168 SSLerr(SSL_F_SSL3_GET_RECORD, ERR_R_INTERNAL_ERROR);
172 if (!PACKET_get_net_2_len(&sslv2pkt, &sslv2len)
173 || !PACKET_get_1(&sslv2pkt, &type)) {
174 al = SSL_AD_INTERNAL_ERROR;
175 SSLerr(SSL_F_SSL3_GET_RECORD, ERR_R_INTERNAL_ERROR);
179 * The first record received by the server may be a V2ClientHello.
181 if (s->server && RECORD_LAYER_is_first_record(&s->rlayer)
182 && (sslv2len & 0x8000) != 0
183 && (type == SSL2_MT_CLIENT_HELLO)) {
187 * |num_recs| here will actually always be 0 because
188 * |num_recs > 0| only ever occurs when we are processing
189 * multiple app data records - which we know isn't the case here
190 * because it is an SSLv2ClientHello. We keep it using
191 * |num_recs| for the sake of consistency
193 rr[num_recs].type = SSL3_RT_HANDSHAKE;
194 rr[num_recs].rec_version = SSL2_VERSION;
196 rr[num_recs].length = sslv2len & 0x7fff;
198 if (rr[num_recs].length > SSL3_BUFFER_get_len(rbuf)
199 - SSL2_RT_HEADER_LENGTH) {
200 al = SSL_AD_RECORD_OVERFLOW;
201 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_PACKET_LENGTH_TOO_LONG);
205 if (rr[num_recs].length < MIN_SSL2_RECORD_LEN) {
206 al = SSL_AD_HANDSHAKE_FAILURE;
207 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT);
211 /* SSLv3+ style record */
213 s->msg_callback(0, 0, SSL3_RT_HEADER, p, 5, s,
214 s->msg_callback_arg);
216 /* Pull apart the header into the SSL3_RECORD */
217 if (!PACKET_get_1(&pkt, &type)
218 || !PACKET_get_net_2(&pkt, &version)
219 || !PACKET_get_net_2_len(&pkt, &rr[num_recs].length)) {
220 al = SSL_AD_INTERNAL_ERROR;
221 SSLerr(SSL_F_SSL3_GET_RECORD, ERR_R_INTERNAL_ERROR);
224 rr[num_recs].type = type;
225 rr[num_recs].rec_version = version;
227 /* Lets check version. In TLSv1.3 we ignore this field */
228 if (!s->first_packet && !SSL_IS_TLS13(s)
229 && version != (unsigned int)s->version) {
230 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_WRONG_VERSION_NUMBER);
231 if ((s->version & 0xFF00) == (version & 0xFF00)
232 && !s->enc_write_ctx && !s->write_hash) {
233 if (rr->type == SSL3_RT_ALERT) {
235 * The record is using an incorrect version number,
236 * but what we've got appears to be an alert. We
237 * haven't read the body yet to check whether its a
238 * fatal or not - but chances are it is. We probably
239 * shouldn't send a fatal alert back. We'll just
245 * Send back error using their minor version number :-)
247 s->version = (unsigned short)version;
249 al = SSL_AD_PROTOCOL_VERSION;
253 if ((version >> 8) != SSL3_VERSION_MAJOR) {
254 if (RECORD_LAYER_is_first_record(&s->rlayer)) {
255 /* Go back to start of packet, look at the five bytes
257 p = RECORD_LAYER_get_packet(&s->rlayer);
258 if (strncmp((char *)p, "GET ", 4) == 0 ||
259 strncmp((char *)p, "POST ", 5) == 0 ||
260 strncmp((char *)p, "HEAD ", 5) == 0 ||
261 strncmp((char *)p, "PUT ", 4) == 0) {
262 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_HTTP_REQUEST);
264 } else if (strncmp((char *)p, "CONNE", 5) == 0) {
265 SSLerr(SSL_F_SSL3_GET_RECORD,
266 SSL_R_HTTPS_PROXY_REQUEST);
270 /* Doesn't look like TLS - don't send an alert */
271 SSLerr(SSL_F_SSL3_GET_RECORD,
272 SSL_R_WRONG_VERSION_NUMBER);
275 SSLerr(SSL_F_SSL3_GET_RECORD,
276 SSL_R_WRONG_VERSION_NUMBER);
277 al = SSL_AD_PROTOCOL_VERSION;
282 if (rr[num_recs].length >
283 SSL3_BUFFER_get_len(rbuf) - SSL3_RT_HEADER_LENGTH) {
284 al = SSL_AD_RECORD_OVERFLOW;
285 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_PACKET_LENGTH_TOO_LONG);
290 /* now s->rlayer.rstate == SSL_ST_READ_BODY */
294 * s->rlayer.rstate == SSL_ST_READ_BODY, get and decode the data.
295 * Calculate how much more data we need to read for the rest of the
298 if (rr[num_recs].rec_version == SSL2_VERSION) {
299 more = rr[num_recs].length + SSL2_RT_HEADER_LENGTH
300 - SSL3_RT_HEADER_LENGTH;
302 more = rr[num_recs].length;
305 /* now s->packet_length == SSL3_RT_HEADER_LENGTH */
307 rret = ssl3_read_n(s, more, more, 1, 0, &n);
309 return rret; /* error or non-blocking io */
312 /* set state for later operations */
313 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);
316 * At this point, s->packet_length == SSL3_RT_HEADER_LENGTH + rr->length,
317 * or s->packet_length == SSL2_RT_HEADER_LENGTH + rr->length
318 * and we have that many bytes in s->packet
320 if (rr[num_recs].rec_version == SSL2_VERSION) {
322 &(RECORD_LAYER_get_packet(&s->rlayer)[SSL2_RT_HEADER_LENGTH]);
325 &(RECORD_LAYER_get_packet(&s->rlayer)[SSL3_RT_HEADER_LENGTH]);
329 * ok, we can now read from 's->packet' data into 'rr' rr->input points
330 * at rr->length bytes, which need to be copied into rr->data by either
331 * the decryption or by the decompression When the data is 'copied' into
332 * the rr->data buffer, rr->input will be pointed at the new buffer
336 * We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
337 * bytes of encrypted compressed stuff.
340 /* check is not needed I believe */
341 if (rr[num_recs].length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
342 al = SSL_AD_RECORD_OVERFLOW;
343 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
347 /* decrypt in place in 'rr->input' */
348 rr[num_recs].data = rr[num_recs].input;
349 rr[num_recs].orig_len = rr[num_recs].length;
351 /* Mark this record as not read by upper layers yet */
352 rr[num_recs].read = 0;
356 /* we have pulled in a full packet so zero things */
357 RECORD_LAYER_reset_packet_length(&s->rlayer);
358 RECORD_LAYER_clear_first_record(&s->rlayer);
359 } while (num_recs < max_recs
360 && rr[num_recs - 1].type == SSL3_RT_APPLICATION_DATA
361 && SSL_USE_EXPLICIT_IV(s)
362 && s->enc_read_ctx != NULL
363 && (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_read_ctx))
364 & EVP_CIPH_FLAG_PIPELINE)
365 && ssl3_record_app_data_waiting(s));
368 * If in encrypt-then-mac mode calculate mac from encrypted record. All
369 * the details below are public so no timing details can leak.
371 if (SSL_USE_ETM(s) && s->read_hash) {
373 /* TODO(size_t): convert this to do size_t properly */
374 imac_size = EVP_MD_CTX_size(s->read_hash);
376 al = SSL_AD_INTERNAL_ERROR;
377 SSLerr(SSL_F_SSL3_GET_RECORD, ERR_LIB_EVP);
380 mac_size = (size_t)imac_size;
381 OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
382 for (j = 0; j < num_recs; j++) {
383 if (rr[j].length < mac_size) {
384 al = SSL_AD_DECODE_ERROR;
385 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT);
388 rr[j].length -= mac_size;
389 mac = rr[j].data + rr[j].length;
390 i = s->method->ssl3_enc->mac(s, &rr[j], md, 0 /* not send */ );
391 if (i == 0 || CRYPTO_memcmp(md, mac, mac_size) != 0) {
392 al = SSL_AD_BAD_RECORD_MAC;
393 SSLerr(SSL_F_SSL3_GET_RECORD,
394 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
400 enc_err = s->method->ssl3_enc->enc(s, rr, num_recs, 0);
403 * 0: (in non-constant time) if the record is publically invalid.
404 * 1: if the padding is valid
405 * -1: if the padding is invalid
408 al = SSL_AD_DECRYPTION_FAILED;
409 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_BLOCK_CIPHER_PAD_IS_WRONG);
413 printf("dec %"OSSLzu"\n", rr->length);
416 for (z = 0; z < rr->length; z++)
417 printf("%02X%c", rr->data[z], ((z + 1) % 16) ? ' ' : '\n');
422 /* r->length is now the compressed data plus mac */
423 if ((sess != NULL) &&
424 (s->enc_read_ctx != NULL) &&
425 (EVP_MD_CTX_md(s->read_hash) != NULL) && !SSL_USE_ETM(s)) {
426 /* s->read_hash != NULL => mac_size != -1 */
427 unsigned char *mac = NULL;
428 unsigned char mac_tmp[EVP_MAX_MD_SIZE];
430 mac_size = EVP_MD_CTX_size(s->read_hash);
431 OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
433 for (j = 0; j < num_recs; j++) {
435 * orig_len is the length of the record before any padding was
436 * removed. This is public information, as is the MAC in use,
437 * therefore we can safely process the record in a different amount
438 * of time if it's too short to possibly contain a MAC.
440 if (rr[j].orig_len < mac_size ||
441 /* CBC records must have a padding length byte too. */
442 (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
443 rr[j].orig_len < mac_size + 1)) {
444 al = SSL_AD_DECODE_ERROR;
445 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT);
449 if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {
451 * We update the length so that the TLS header bytes can be
452 * constructed correctly but we need to extract the MAC in
453 * constant time from within the record, without leaking the
454 * contents of the padding bytes.
457 ssl3_cbc_copy_mac(mac_tmp, &rr[j], mac_size);
458 rr[j].length -= mac_size;
461 * In this case there's no padding, so |rec->orig_len| equals
462 * |rec->length| and we checked that there's enough bytes for
465 rr[j].length -= mac_size;
466 mac = &rr[j].data[rr[j].length];
469 i = s->method->ssl3_enc->mac(s, &rr[j], md, 0 /* not send */ );
470 if (i == 0 || mac == NULL
471 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0)
473 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
480 * A separate 'decryption_failed' alert was introduced with TLS 1.0,
481 * SSL 3.0 only has 'bad_record_mac'. But unless a decryption
482 * failure is directly visible from the ciphertext anyway, we should
483 * not reveal which kind of error occurred -- this might become
484 * visible to an attacker (e.g. via a logfile)
486 al = SSL_AD_BAD_RECORD_MAC;
487 SSLerr(SSL_F_SSL3_GET_RECORD,
488 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
492 for (j = 0; j < num_recs; j++) {
493 /* rr[j].length is now just compressed */
494 if (s->expand != NULL) {
495 if (rr[j].length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
496 al = SSL_AD_RECORD_OVERFLOW;
497 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_COMPRESSED_LENGTH_TOO_LONG);
500 if (!ssl3_do_uncompress(s, &rr[j])) {
501 al = SSL_AD_DECOMPRESSION_FAILURE;
502 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_BAD_DECOMPRESSION);
507 if (rr[j].length > SSL3_RT_MAX_PLAIN_LENGTH) {
508 al = SSL_AD_RECORD_OVERFLOW;
509 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_DATA_LENGTH_TOO_LONG);
515 * So at this point the following is true
516 * rr[j].type is the type of record
517 * rr[j].length == number of bytes in record
518 * rr[j].off == offset to first valid byte
519 * rr[j].data == where to take bytes from, increment after use :-).
522 /* just read a 0 length packet */
523 if (rr[j].length == 0) {
524 RECORD_LAYER_inc_empty_record_count(&s->rlayer);
525 if (RECORD_LAYER_get_empty_record_count(&s->rlayer)
526 > MAX_EMPTY_RECORDS) {
527 al = SSL_AD_UNEXPECTED_MESSAGE;
528 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_RECORD_TOO_SMALL);
532 RECORD_LAYER_reset_empty_record_count(&s->rlayer);
536 RECORD_LAYER_set_numrpipes(&s->rlayer, num_recs);
540 ssl3_send_alert(s, SSL3_AL_FATAL, al);
545 int ssl3_do_uncompress(SSL *ssl, SSL3_RECORD *rr)
547 #ifndef OPENSSL_NO_COMP
550 if (rr->comp == NULL) {
551 rr->comp = (unsigned char *)
552 OPENSSL_malloc(SSL3_RT_MAX_ENCRYPTED_LENGTH);
554 if (rr->comp == NULL)
557 /* TODO(size_t): Convert this call */
558 i = COMP_expand_block(ssl->expand, rr->comp,
559 SSL3_RT_MAX_PLAIN_LENGTH, rr->data, (int)rr->length);
569 int ssl3_do_compress(SSL *ssl, SSL3_RECORD *wr)
571 #ifndef OPENSSL_NO_COMP
574 /* TODO(size_t): Convert this call */
575 i = COMP_compress_block(ssl->compress, wr->data,
576 (int)(wr->length + SSL3_RT_MAX_COMPRESSED_OVERHEAD),
577 wr->input, (int)wr->length);
583 wr->input = wr->data;
589 * ssl3_enc encrypts/decrypts |n_recs| records in |inrecs|
592 * 0: (in non-constant time) if the record is publically invalid (i.e. too
594 * 1: if the record's padding is valid / the encryption was successful.
595 * -1: if the record's padding is invalid or, if sending, an internal error
598 int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, size_t n_recs, int send)
603 size_t bs, mac_size = 0;
605 const EVP_CIPHER *enc;
609 * We shouldn't ever be called with more than one record in the SSLv3 case
614 ds = s->enc_write_ctx;
615 if (s->enc_write_ctx == NULL)
618 enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
620 ds = s->enc_read_ctx;
621 if (s->enc_read_ctx == NULL)
624 enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
627 if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
628 memmove(rec->data, rec->input, rec->length);
629 rec->input = rec->data;
632 /* TODO(size_t): Convert this call */
633 bs = EVP_CIPHER_CTX_block_size(ds);
637 if ((bs != 1) && send) {
640 /* we need to add 'i-1' padding bytes */
643 * the last of these zero bytes will be overwritten with the
646 memset(&rec->input[rec->length], 0, i);
648 rec->input[l - 1] = (unsigned char)(i - 1);
652 if (l == 0 || l % bs != 0)
654 /* otherwise, rec->length >= bs */
657 /* TODO(size_t): Convert this call */
658 if (EVP_Cipher(ds, rec->data, rec->input, (unsigned int)l) < 1)
661 if (EVP_MD_CTX_md(s->read_hash) != NULL) {
662 /* TODO(size_t): convert me */
663 imac_size = EVP_MD_CTX_size(s->read_hash);
666 mac_size = (size_t)imac_size;
668 if ((bs != 1) && !send)
669 return ssl3_cbc_remove_padding(rec, bs, mac_size);
674 #define MAX_PADDING 256
676 * tls1_enc encrypts/decrypts |n_recs| in |recs|.
679 * 0: (in non-constant time) if the record is publically invalid (i.e. too
681 * 1: if the record's padding is valid / the encryption was successful.
682 * -1: if the record's padding/AEAD-authenticator is invalid or, if sending,
683 * an internal error occurred.
685 int tls1_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int send)
688 size_t reclen[SSL_MAX_PIPELINES];
689 unsigned char buf[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
690 int i, pad = 0, ret, tmpr;
691 size_t bs, mac_size = 0, ctr, padnum, loop;
692 unsigned char padval;
694 const EVP_CIPHER *enc;
697 if (EVP_MD_CTX_md(s->write_hash)) {
698 int n = EVP_MD_CTX_size(s->write_hash);
699 OPENSSL_assert(n >= 0);
701 ds = s->enc_write_ctx;
702 if (s->enc_write_ctx == NULL)
706 enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
707 /* For TLSv1.1 and later explicit IV */
708 if (SSL_USE_EXPLICIT_IV(s)
709 && EVP_CIPHER_mode(enc) == EVP_CIPH_CBC_MODE)
710 ivlen = EVP_CIPHER_iv_length(enc);
714 for (ctr = 0; ctr < n_recs; ctr++) {
715 if (recs[ctr].data != recs[ctr].input) {
717 * we can't write into the input stream: Can this ever
720 SSLerr(SSL_F_TLS1_ENC, ERR_R_INTERNAL_ERROR);
722 } else if (RAND_bytes(recs[ctr].input, ivlen) <= 0) {
723 SSLerr(SSL_F_TLS1_ENC, ERR_R_INTERNAL_ERROR);
730 if (EVP_MD_CTX_md(s->read_hash)) {
731 int n = EVP_MD_CTX_size(s->read_hash);
732 OPENSSL_assert(n >= 0);
734 ds = s->enc_read_ctx;
735 if (s->enc_read_ctx == NULL)
738 enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
741 if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
742 for (ctr = 0; ctr < n_recs; ctr++) {
743 memmove(recs[ctr].data, recs[ctr].input, recs[ctr].length);
744 recs[ctr].input = recs[ctr].data;
748 bs = EVP_CIPHER_block_size(EVP_CIPHER_CTX_cipher(ds));
751 if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
752 & EVP_CIPH_FLAG_PIPELINE)) {
754 * We shouldn't have been called with pipeline data if the
755 * cipher doesn't support pipelining
757 SSLerr(SSL_F_TLS1_ENC, SSL_R_PIPELINE_FAILURE);
761 for (ctr = 0; ctr < n_recs; ctr++) {
762 reclen[ctr] = recs[ctr].length;
764 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
765 & EVP_CIPH_FLAG_AEAD_CIPHER) {
768 seq = send ? RECORD_LAYER_get_write_sequence(&s->rlayer)
769 : RECORD_LAYER_get_read_sequence(&s->rlayer);
771 if (SSL_IS_DTLS(s)) {
772 /* DTLS does not support pipelining */
773 unsigned char dtlsseq[9], *p = dtlsseq;
775 s2n(send ? DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer) :
776 DTLS_RECORD_LAYER_get_r_epoch(&s->rlayer), p);
777 memcpy(p, &seq[2], 6);
778 memcpy(buf[ctr], dtlsseq, 8);
780 memcpy(buf[ctr], seq, 8);
781 for (i = 7; i >= 0; i--) { /* increment */
788 buf[ctr][8] = recs[ctr].type;
789 buf[ctr][9] = (unsigned char)(s->version >> 8);
790 buf[ctr][10] = (unsigned char)(s->version);
791 buf[ctr][11] = (unsigned char)(recs[ctr].length >> 8);
792 buf[ctr][12] = (unsigned char)(recs[ctr].length & 0xff);
793 pad = EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_AEAD_TLS1_AAD,
794 EVP_AEAD_TLS1_AAD_LEN, buf[ctr]);
800 recs[ctr].length += pad;
803 } else if ((bs != 1) && send) {
804 padnum = bs - (reclen[ctr] % bs);
806 /* Add weird padding of upto 256 bytes */
808 if (padnum > MAX_PADDING)
810 /* we need to add 'padnum' padding bytes of value padval */
811 padval = (unsigned char)(padnum - 1);
812 for (loop = reclen[ctr]; loop < reclen[ctr] + padnum; loop++)
813 recs[ctr].input[loop] = padval;
814 reclen[ctr] += padnum;
815 recs[ctr].length += padnum;
819 if (reclen[ctr] == 0 || reclen[ctr] % bs != 0)
824 unsigned char *data[SSL_MAX_PIPELINES];
826 /* Set the output buffers */
827 for (ctr = 0; ctr < n_recs; ctr++) {
828 data[ctr] = recs[ctr].data;
830 if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS,
831 (int)n_recs, data) <= 0) {
832 SSLerr(SSL_F_TLS1_ENC, SSL_R_PIPELINE_FAILURE);
834 /* Set the input buffers */
835 for (ctr = 0; ctr < n_recs; ctr++) {
836 data[ctr] = recs[ctr].input;
838 if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_BUFS,
839 (int)n_recs, data) <= 0
840 || EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_LENS,
841 (int)n_recs, reclen) <= 0) {
842 SSLerr(SSL_F_TLS1_ENC, SSL_R_PIPELINE_FAILURE);
847 /* TODO(size_t): Convert this call */
848 tmpr = EVP_Cipher(ds, recs[0].data, recs[0].input,
849 (unsigned int)reclen[0]);
850 if ((EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
851 & EVP_CIPH_FLAG_CUSTOM_CIPHER)
854 return -1; /* AEAD can fail to verify MAC */
856 if (EVP_CIPHER_mode(enc) == EVP_CIPH_GCM_MODE) {
857 for (ctr = 0; ctr < n_recs; ctr++) {
858 recs[ctr].data += EVP_GCM_TLS_EXPLICIT_IV_LEN;
859 recs[ctr].input += EVP_GCM_TLS_EXPLICIT_IV_LEN;
860 recs[ctr].length -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
862 } else if (EVP_CIPHER_mode(enc) == EVP_CIPH_CCM_MODE) {
863 for (ctr = 0; ctr < n_recs; ctr++) {
864 recs[ctr].data += EVP_CCM_TLS_EXPLICIT_IV_LEN;
865 recs[ctr].input += EVP_CCM_TLS_EXPLICIT_IV_LEN;
866 recs[ctr].length -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
872 if (!SSL_USE_ETM(s) && EVP_MD_CTX_md(s->read_hash) != NULL) {
873 imac_size = EVP_MD_CTX_size(s->read_hash);
876 mac_size = (size_t)imac_size;
878 if ((bs != 1) && !send) {
880 for (ctr = 0; ctr < n_recs; ctr++) {
881 tmpret = tls1_cbc_remove_padding(s, &recs[ctr], bs, mac_size);
883 * If tmpret == 0 then this means publicly invalid so we can
884 * short circuit things here. Otherwise we must respect constant
889 ret = constant_time_select_int(constant_time_eq_int(tmpret, 1),
894 for (ctr = 0; ctr < n_recs; ctr++) {
895 recs[ctr].length -= pad;
902 int n_ssl3_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int send)
904 unsigned char *mac_sec, *seq;
905 const EVP_MD_CTX *hash;
906 unsigned char *p, rec_char;
912 mac_sec = &(ssl->s3->write_mac_secret[0]);
913 seq = RECORD_LAYER_get_write_sequence(&ssl->rlayer);
914 hash = ssl->write_hash;
916 mac_sec = &(ssl->s3->read_mac_secret[0]);
917 seq = RECORD_LAYER_get_read_sequence(&ssl->rlayer);
918 hash = ssl->read_hash;
921 t = EVP_MD_CTX_size(hash);
925 npad = (48 / md_size) * md_size;
928 EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
929 ssl3_cbc_record_digest_supported(hash)) {
931 * This is a CBC-encrypted record. We must avoid leaking any
932 * timing-side channel information about how many blocks of data we
933 * are hashing because that gives an attacker a timing-oracle.
937 * npad is, at most, 48 bytes and that's with MD5:
938 * 16 + 48 + 8 (sequence bytes) + 1 + 2 = 75.
940 * With SHA-1 (the largest hash speced for SSLv3) the hash size
941 * goes up 4, but npad goes down by 8, resulting in a smaller
944 unsigned char header[75];
946 memcpy(header + j, mac_sec, md_size);
948 memcpy(header + j, ssl3_pad_1, npad);
950 memcpy(header + j, seq, 8);
952 header[j++] = rec->type;
953 header[j++] = (unsigned char)(rec->length >> 8);
954 header[j++] = (unsigned char)(rec->length & 0xff);
956 /* Final param == is SSLv3 */
957 if (ssl3_cbc_digest_record(hash,
960 rec->length + md_size, rec->orig_len,
961 mac_sec, md_size, 1) <= 0)
964 unsigned int md_size_u;
965 /* Chop the digest off the end :-) */
966 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
971 rec_char = rec->type;
974 if (EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
975 || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
976 || EVP_DigestUpdate(md_ctx, ssl3_pad_1, npad) <= 0
977 || EVP_DigestUpdate(md_ctx, seq, 8) <= 0
978 || EVP_DigestUpdate(md_ctx, &rec_char, 1) <= 0
979 || EVP_DigestUpdate(md_ctx, md, 2) <= 0
980 || EVP_DigestUpdate(md_ctx, rec->input, rec->length) <= 0
981 || EVP_DigestFinal_ex(md_ctx, md, NULL) <= 0
982 || EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
983 || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
984 || EVP_DigestUpdate(md_ctx, ssl3_pad_2, npad) <= 0
985 || EVP_DigestUpdate(md_ctx, md, md_size) <= 0
986 || EVP_DigestFinal_ex(md_ctx, md, &md_size_u) <= 0) {
987 EVP_MD_CTX_reset(md_ctx);
991 EVP_MD_CTX_free(md_ctx);
994 ssl3_record_sequence_update(seq);
998 int tls1_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int send)
1004 EVP_MD_CTX *hmac = NULL, *mac_ctx;
1005 unsigned char header[13];
1006 int stream_mac = (send ? (ssl->mac_flags & SSL_MAC_FLAG_WRITE_MAC_STREAM)
1007 : (ssl->mac_flags & SSL_MAC_FLAG_READ_MAC_STREAM));
1011 seq = RECORD_LAYER_get_write_sequence(&ssl->rlayer);
1012 hash = ssl->write_hash;
1014 seq = RECORD_LAYER_get_read_sequence(&ssl->rlayer);
1015 hash = ssl->read_hash;
1018 t = EVP_MD_CTX_size(hash);
1019 OPENSSL_assert(t >= 0);
1022 /* I should fix this up TLS TLS TLS TLS TLS XXXXXXXX */
1026 hmac = EVP_MD_CTX_new();
1027 if (hmac == NULL || !EVP_MD_CTX_copy(hmac, hash))
1032 if (SSL_IS_DTLS(ssl)) {
1033 unsigned char dtlsseq[8], *p = dtlsseq;
1035 s2n(send ? DTLS_RECORD_LAYER_get_w_epoch(&ssl->rlayer) :
1036 DTLS_RECORD_LAYER_get_r_epoch(&ssl->rlayer), p);
1037 memcpy(p, &seq[2], 6);
1039 memcpy(header, dtlsseq, 8);
1041 memcpy(header, seq, 8);
1043 header[8] = rec->type;
1044 header[9] = (unsigned char)(ssl->version >> 8);
1045 header[10] = (unsigned char)(ssl->version);
1046 header[11] = (unsigned char)(rec->length >> 8);
1047 header[12] = (unsigned char)(rec->length & 0xff);
1049 if (!send && !SSL_USE_ETM(ssl) &&
1050 EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
1051 ssl3_cbc_record_digest_supported(mac_ctx)) {
1053 * This is a CBC-encrypted record. We must avoid leaking any
1054 * timing-side channel information about how many blocks of data we
1055 * are hashing because that gives an attacker a timing-oracle.
1057 /* Final param == not SSLv3 */
1058 if (ssl3_cbc_digest_record(mac_ctx,
1061 rec->length + md_size, rec->orig_len,
1062 ssl->s3->read_mac_secret,
1063 ssl->s3->read_mac_secret_size, 0) <= 0) {
1064 EVP_MD_CTX_free(hmac);
1068 /* TODO(size_t): Convert these calls */
1069 if (EVP_DigestSignUpdate(mac_ctx, header, sizeof(header)) <= 0
1070 || EVP_DigestSignUpdate(mac_ctx, rec->input, rec->length) <= 0
1071 || EVP_DigestSignFinal(mac_ctx, md, &md_size) <= 0) {
1072 EVP_MD_CTX_free(hmac);
1075 if (!send && !SSL_USE_ETM(ssl) && FIPS_mode())
1076 if (!tls_fips_digest_extra(ssl->enc_read_ctx,
1077 mac_ctx, rec->input,
1078 rec->length, rec->orig_len)) {
1079 EVP_MD_CTX_free(hmac);
1084 EVP_MD_CTX_free(hmac);
1087 fprintf(stderr, "seq=");
1090 for (z = 0; z < 8; z++)
1091 fprintf(stderr, "%02X ", seq[z]);
1092 fprintf(stderr, "\n");
1094 fprintf(stderr, "rec=");
1097 for (z = 0; z < rec->length; z++)
1098 fprintf(stderr, "%02X ", rec->data[z]);
1099 fprintf(stderr, "\n");
1103 if (!SSL_IS_DTLS(ssl)) {
1104 for (i = 7; i >= 0; i--) {
1113 for (z = 0; z < md_size; z++)
1114 fprintf(stderr, "%02X ", md[z]);
1115 fprintf(stderr, "\n");
1122 * ssl3_cbc_remove_padding removes padding from the decrypted, SSLv3, CBC
1123 * record in |rec| by updating |rec->length| in constant time.
1125 * block_size: the block size of the cipher used to encrypt the record.
1127 * 0: (in non-constant time) if the record is publicly invalid.
1128 * 1: if the padding was valid
1131 int ssl3_cbc_remove_padding(SSL3_RECORD *rec,
1132 size_t block_size, size_t mac_size)
1134 size_t padding_length;
1136 const size_t overhead = 1 /* padding length byte */ + mac_size;
1139 * These lengths are all public so we can test them in non-constant time.
1141 if (overhead > rec->length)
1144 padding_length = rec->data[rec->length - 1];
1145 good = constant_time_ge_s(rec->length, padding_length + overhead);
1146 /* SSLv3 requires that the padding is minimal. */
1147 good &= constant_time_ge_s(block_size, padding_length + 1);
1148 rec->length -= good & (padding_length + 1);
1149 return constant_time_select_int_s(good, 1, -1);
1153 * tls1_cbc_remove_padding removes the CBC padding from the decrypted, TLS, CBC
1154 * record in |rec| in constant time and returns 1 if the padding is valid and
1155 * -1 otherwise. It also removes any explicit IV from the start of the record
1156 * without leaking any timing about whether there was enough space after the
1157 * padding was removed.
1159 * block_size: the block size of the cipher used to encrypt the record.
1161 * 0: (in non-constant time) if the record is publicly invalid.
1162 * 1: if the padding was valid
1165 int tls1_cbc_remove_padding(const SSL *s,
1167 size_t block_size, size_t mac_size)
1170 size_t padding_length, to_check, i;
1171 const size_t overhead = 1 /* padding length byte */ + mac_size;
1172 /* Check if version requires explicit IV */
1173 if (SSL_USE_EXPLICIT_IV(s)) {
1175 * These lengths are all public so we can test them in non-constant
1178 if (overhead + block_size > rec->length)
1180 /* We can now safely skip explicit IV */
1181 rec->data += block_size;
1182 rec->input += block_size;
1183 rec->length -= block_size;
1184 rec->orig_len -= block_size;
1185 } else if (overhead > rec->length)
1188 padding_length = rec->data[rec->length - 1];
1190 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_read_ctx)) &
1191 EVP_CIPH_FLAG_AEAD_CIPHER) {
1192 /* padding is already verified */
1193 rec->length -= padding_length + 1;
1197 good = constant_time_ge_s(rec->length, overhead + padding_length);
1199 * The padding consists of a length byte at the end of the record and
1200 * then that many bytes of padding, all with the same value as the length
1201 * byte. Thus, with the length byte included, there are i+1 bytes of
1202 * padding. We can't check just |padding_length+1| bytes because that
1203 * leaks decrypted information. Therefore we always have to check the
1204 * maximum amount of padding possible. (Again, the length of the record
1205 * is public information so we can use it.)
1207 to_check = 256; /* maximum amount of padding, inc length byte. */
1208 if (to_check > rec->length)
1209 to_check = rec->length;
1211 for (i = 0; i < to_check; i++) {
1212 unsigned char mask = constant_time_ge_8_s(padding_length, i);
1213 unsigned char b = rec->data[rec->length - 1 - i];
1215 * The final |padding_length+1| bytes should all have the value
1216 * |padding_length|. Therefore the XOR should be zero.
1218 good &= ~(mask & (padding_length ^ b));
1222 * If any of the final |padding_length+1| bytes had the wrong value, one
1223 * or more of the lower eight bits of |good| will be cleared.
1225 good = constant_time_eq_s(0xff, good & 0xff);
1226 rec->length -= good & (padding_length + 1);
1228 return constant_time_select_int_s(good, 1, -1);
1232 * ssl3_cbc_copy_mac copies |md_size| bytes from the end of |rec| to |out| in
1233 * constant time (independent of the concrete value of rec->length, which may
1234 * vary within a 256-byte window).
1236 * ssl3_cbc_remove_padding or tls1_cbc_remove_padding must be called prior to
1240 * rec->orig_len >= md_size
1241 * md_size <= EVP_MAX_MD_SIZE
1243 * If CBC_MAC_ROTATE_IN_PLACE is defined then the rotation is performed with
1244 * variable accesses in a 64-byte-aligned buffer. Assuming that this fits into
1245 * a single or pair of cache-lines, then the variable memory accesses don't
1246 * actually affect the timing. CPUs with smaller cache-lines [if any] are
1247 * not multi-core and are not considered vulnerable to cache-timing attacks.
1249 #define CBC_MAC_ROTATE_IN_PLACE
1251 void ssl3_cbc_copy_mac(unsigned char *out,
1252 const SSL3_RECORD *rec, size_t md_size)
1254 #if defined(CBC_MAC_ROTATE_IN_PLACE)
1255 unsigned char rotated_mac_buf[64 + EVP_MAX_MD_SIZE];
1256 unsigned char *rotated_mac;
1258 unsigned char rotated_mac[EVP_MAX_MD_SIZE];
1262 * mac_end is the index of |rec->data| just after the end of the MAC.
1264 size_t mac_end = rec->length;
1265 size_t mac_start = mac_end - md_size;
1267 * scan_start contains the number of bytes that we can ignore because the
1268 * MAC's position can only vary by 255 bytes.
1270 size_t scan_start = 0;
1273 size_t rotate_offset;
1275 OPENSSL_assert(rec->orig_len >= md_size);
1276 OPENSSL_assert(md_size <= EVP_MAX_MD_SIZE);
1278 #if defined(CBC_MAC_ROTATE_IN_PLACE)
1279 rotated_mac = rotated_mac_buf + ((0 - (size_t)rotated_mac_buf) & 63);
1282 /* This information is public so it's safe to branch based on it. */
1283 if (rec->orig_len > md_size + 255 + 1)
1284 scan_start = rec->orig_len - (md_size + 255 + 1);
1286 * div_spoiler contains a multiple of md_size that is used to cause the
1287 * modulo operation to be constant time. Without this, the time varies
1288 * based on the amount of padding when running on Intel chips at least.
1289 * The aim of right-shifting md_size is so that the compiler doesn't
1290 * figure out that it can remove div_spoiler as that would require it to
1291 * prove that md_size is always even, which I hope is beyond it.
1293 div_spoiler = md_size >> 1;
1294 div_spoiler <<= (sizeof(div_spoiler) - 1) * 8;
1295 rotate_offset = (div_spoiler + mac_start - scan_start) % md_size;
1297 memset(rotated_mac, 0, md_size);
1298 for (i = scan_start, j = 0; i < rec->orig_len; i++) {
1299 unsigned char mac_started = constant_time_ge_8_s(i, mac_start);
1300 unsigned char mac_ended = constant_time_ge_8_s(i, mac_end);
1301 unsigned char b = rec->data[i];
1302 rotated_mac[j++] |= b & mac_started & ~mac_ended;
1303 j &= constant_time_lt_s(j, md_size);
1306 /* Now rotate the MAC */
1307 #if defined(CBC_MAC_ROTATE_IN_PLACE)
1309 for (i = 0; i < md_size; i++) {
1310 /* in case cache-line is 32 bytes, touch second line */
1311 ((volatile unsigned char *)rotated_mac)[rotate_offset ^ 32];
1312 out[j++] = rotated_mac[rotate_offset++];
1313 rotate_offset &= constant_time_lt_s(rotate_offset, md_size);
1316 memset(out, 0, md_size);
1317 rotate_offset = md_size - rotate_offset;
1318 rotate_offset &= constant_time_lt_s(rotate_offset, md_size);
1319 for (i = 0; i < md_size; i++) {
1320 for (j = 0; j < md_size; j++)
1321 out[j] |= rotated_mac[i] & constant_time_eq_8_s(j, rotate_offset);
1323 rotate_offset &= constant_time_lt_s(rotate_offset, md_size);
1328 int dtls1_process_record(SSL *s, DTLS1_BITMAP *bitmap)
1336 unsigned char md[EVP_MAX_MD_SIZE];
1338 rr = RECORD_LAYER_get_rrec(&s->rlayer);
1342 * At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length,
1343 * and we have that many bytes in s->packet
1345 rr->input = &(RECORD_LAYER_get_packet(&s->rlayer)[DTLS1_RT_HEADER_LENGTH]);
1348 * ok, we can now read from 's->packet' data into 'rr' rr->input points
1349 * at rr->length bytes, which need to be copied into rr->data by either
1350 * the decryption or by the decompression When the data is 'copied' into
1351 * the rr->data buffer, rr->input will be pointed at the new buffer
1355 * We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
1356 * bytes of encrypted compressed stuff.
1359 /* check is not needed I believe */
1360 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
1361 al = SSL_AD_RECORD_OVERFLOW;
1362 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
1366 /* decrypt in place in 'rr->input' */
1367 rr->data = rr->input;
1368 rr->orig_len = rr->length;
1370 if (SSL_USE_ETM(s) && s->read_hash) {
1372 mac_size = EVP_MD_CTX_size(s->read_hash);
1373 OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
1374 if (rr->orig_len < mac_size) {
1375 al = SSL_AD_DECODE_ERROR;
1376 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_LENGTH_TOO_SHORT);
1379 rr->length -= mac_size;
1380 mac = rr->data + rr->length;
1381 i = s->method->ssl3_enc->mac(s, rr, md, 0 /* not send */ );
1382 if (i == 0 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0) {
1383 al = SSL_AD_BAD_RECORD_MAC;
1384 SSLerr(SSL_F_DTLS1_PROCESS_RECORD,
1385 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
1390 enc_err = s->method->ssl3_enc->enc(s, rr, 1, 0);
1393 * 0: (in non-constant time) if the record is publically invalid.
1394 * 1: if the padding is valid
1395 * -1: if the padding is invalid
1398 /* For DTLS we simply ignore bad packets. */
1400 RECORD_LAYER_reset_packet_length(&s->rlayer);
1404 printf("dec %ld\n", rr->length);
1407 for (z = 0; z < rr->length; z++)
1408 printf("%02X%c", rr->data[z], ((z + 1) % 16) ? ' ' : '\n');
1413 /* r->length is now the compressed data plus mac */
1414 if ((sess != NULL) && !SSL_USE_ETM(s) &&
1415 (s->enc_read_ctx != NULL) && (EVP_MD_CTX_md(s->read_hash) != NULL)) {
1416 /* s->read_hash != NULL => mac_size != -1 */
1417 unsigned char *mac = NULL;
1418 unsigned char mac_tmp[EVP_MAX_MD_SIZE];
1420 /* TODO(size_t): Convert this to do size_t properly */
1421 imac_size = EVP_MD_CTX_size(s->read_hash);
1422 if (imac_size < 0) {
1423 al = SSL_AD_INTERNAL_ERROR;
1424 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, ERR_LIB_EVP);
1427 mac_size = (size_t)imac_size;
1428 OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
1431 * orig_len is the length of the record before any padding was
1432 * removed. This is public information, as is the MAC in use,
1433 * therefore we can safely process the record in a different amount
1434 * of time if it's too short to possibly contain a MAC.
1436 if (rr->orig_len < mac_size ||
1437 /* CBC records must have a padding length byte too. */
1438 (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
1439 rr->orig_len < mac_size + 1)) {
1440 al = SSL_AD_DECODE_ERROR;
1441 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_LENGTH_TOO_SHORT);
1445 if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {
1447 * We update the length so that the TLS header bytes can be
1448 * constructed correctly but we need to extract the MAC in
1449 * constant time from within the record, without leaking the
1450 * contents of the padding bytes.
1453 ssl3_cbc_copy_mac(mac_tmp, rr, mac_size);
1454 rr->length -= mac_size;
1457 * In this case there's no padding, so |rec->orig_len| equals
1458 * |rec->length| and we checked that there's enough bytes for
1461 rr->length -= mac_size;
1462 mac = &rr->data[rr->length];
1465 i = s->method->ssl3_enc->mac(s, rr, md, 0 /* not send */ );
1466 if (i == 0 || mac == NULL
1467 || CRYPTO_memcmp(md, mac, mac_size) != 0)
1469 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
1474 /* decryption failed, silently discard message */
1476 RECORD_LAYER_reset_packet_length(&s->rlayer);
1480 /* r->length is now just compressed */
1481 if (s->expand != NULL) {
1482 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
1483 al = SSL_AD_RECORD_OVERFLOW;
1484 SSLerr(SSL_F_DTLS1_PROCESS_RECORD,
1485 SSL_R_COMPRESSED_LENGTH_TOO_LONG);
1488 if (!ssl3_do_uncompress(s, rr)) {
1489 al = SSL_AD_DECOMPRESSION_FAILURE;
1490 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_BAD_DECOMPRESSION);
1495 if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH) {
1496 al = SSL_AD_RECORD_OVERFLOW;
1497 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_DATA_LENGTH_TOO_LONG);
1503 * So at this point the following is true
1504 * ssl->s3->rrec.type is the type of record
1505 * ssl->s3->rrec.length == number of bytes in record
1506 * ssl->s3->rrec.off == offset to first valid byte
1507 * ssl->s3->rrec.data == where to take bytes from, increment
1511 /* we have pulled in a full packet so zero things */
1512 RECORD_LAYER_reset_packet_length(&s->rlayer);
1514 /* Mark receipt of record. */
1515 dtls1_record_bitmap_update(s, bitmap);
1520 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1526 * retrieve a buffered record that belongs to the current epoch, ie,
1529 #define dtls1_get_processed_record(s) \
1530 dtls1_retrieve_buffered_record((s), \
1531 &(DTLS_RECORD_LAYER_get_processed_rcds(&s->rlayer)))
1534 * Call this to get a new input record.
1535 * It will return <= 0 if more data is needed, normally due to an error
1536 * or non-blocking IO.
1537 * When it finishes, one packet has been decoded and can be found in
1538 * ssl->s3->rrec.type - is the type of record
1539 * ssl->s3->rrec.data, - data
1540 * ssl->s3->rrec.length, - number of bytes
1542 /* used only by dtls1_read_bytes */
1543 int dtls1_get_record(SSL *s)
1545 int ssl_major, ssl_minor;
1549 unsigned char *p = NULL;
1550 unsigned short version;
1551 DTLS1_BITMAP *bitmap;
1552 unsigned int is_next_epoch;
1554 rr = RECORD_LAYER_get_rrec(&s->rlayer);
1558 * The epoch may have changed. If so, process all the pending records.
1559 * This is a non-blocking operation.
1561 if (!dtls1_process_buffered_records(s))
1564 /* if we're renegotiating, then there may be buffered records */
1565 if (dtls1_get_processed_record(s))
1568 /* get something from the wire */
1570 /* check if we have the header */
1571 if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||
1572 (RECORD_LAYER_get_packet_length(&s->rlayer) < DTLS1_RT_HEADER_LENGTH)) {
1573 rret = ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH,
1574 SSL3_BUFFER_get_len(&s->rlayer.rbuf), 0, 1, &n);
1575 /* read timeout is handled by dtls1_read_bytes */
1577 return rret; /* error or non-blocking */
1579 /* this packet contained a partial record, dump it */
1580 if (RECORD_LAYER_get_packet_length(&s->rlayer) !=
1581 DTLS1_RT_HEADER_LENGTH) {
1582 RECORD_LAYER_reset_packet_length(&s->rlayer);
1586 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);
1588 p = RECORD_LAYER_get_packet(&s->rlayer);
1590 if (s->msg_callback)
1591 s->msg_callback(0, 0, SSL3_RT_HEADER, p, DTLS1_RT_HEADER_LENGTH,
1592 s, s->msg_callback_arg);
1594 /* Pull apart the header into the DTLS1_RECORD */
1598 version = (ssl_major << 8) | ssl_minor;
1600 /* sequence number is 64 bits, with top 2 bytes = epoch */
1603 memcpy(&(RECORD_LAYER_get_read_sequence(&s->rlayer)[2]), p, 6);
1608 /* Lets check version */
1609 if (!s->first_packet) {
1610 if (version != s->version) {
1611 /* unexpected version, silently discard */
1613 RECORD_LAYER_reset_packet_length(&s->rlayer);
1618 if ((version & 0xff00) != (s->version & 0xff00)) {
1619 /* wrong version, silently discard record */
1621 RECORD_LAYER_reset_packet_length(&s->rlayer);
1625 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
1626 /* record too long, silently discard it */
1628 RECORD_LAYER_reset_packet_length(&s->rlayer);
1632 /* now s->rlayer.rstate == SSL_ST_READ_BODY */
1635 /* s->rlayer.rstate == SSL_ST_READ_BODY, get and decode the data */
1638 RECORD_LAYER_get_packet_length(&s->rlayer) - DTLS1_RT_HEADER_LENGTH) {
1639 /* now s->packet_length == DTLS1_RT_HEADER_LENGTH */
1641 rret = ssl3_read_n(s, more, more, 1, 1, &n);
1642 /* this packet contained a partial record, dump it */
1643 if (rret <= 0 || n != more) {
1645 RECORD_LAYER_reset_packet_length(&s->rlayer);
1650 * now n == rr->length, and s->packet_length ==
1651 * DTLS1_RT_HEADER_LENGTH + rr->length
1654 /* set state for later operations */
1655 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);
1657 /* match epochs. NULL means the packet is dropped on the floor */
1658 bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);
1659 if (bitmap == NULL) {
1661 RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
1662 goto again; /* get another record */
1664 #ifndef OPENSSL_NO_SCTP
1665 /* Only do replay check if no SCTP bio */
1666 if (!BIO_dgram_is_sctp(SSL_get_rbio(s))) {
1668 /* Check whether this is a repeat, or aged record. */
1670 * TODO: Does it make sense to have replay protection in epoch 0 where
1671 * we have no integrity negotiated yet?
1673 if (!dtls1_record_replay_check(s, bitmap)) {
1675 RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
1676 goto again; /* get another record */
1678 #ifndef OPENSSL_NO_SCTP
1682 /* just read a 0 length packet */
1683 if (rr->length == 0)
1687 * If this record is from the next epoch (either HM or ALERT), and a
1688 * handshake is currently in progress, buffer it since it cannot be
1689 * processed at this time.
1691 if (is_next_epoch) {
1692 if ((SSL_in_init(s) || ossl_statem_get_in_handshake(s))) {
1693 if (dtls1_buffer_record
1694 (s, &(DTLS_RECORD_LAYER_get_unprocessed_rcds(&s->rlayer)),
1699 RECORD_LAYER_reset_packet_length(&s->rlayer);
1703 if (!dtls1_process_record(s, bitmap)) {
1705 RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
1706 goto again; /* get another record */