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 n2s(p, rr[num_recs].length);
207 /* Lets check version */
208 if (!s->first_packet && version != s->version) {
209 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_WRONG_VERSION_NUMBER);
210 if ((s->version & 0xFF00) == (version & 0xFF00)
211 && !s->enc_write_ctx && !s->write_hash) {
212 if (rr->type == SSL3_RT_ALERT) {
214 * The record is using an incorrect version number,
215 * but what we've got appears to be an alert. We
216 * haven't read the body yet to check whether its a
217 * fatal or not - but chances are it is. We probably
218 * shouldn't send a fatal alert back. We'll just
224 * Send back error using their minor version number :-)
226 s->version = (unsigned short)version;
228 al = SSL_AD_PROTOCOL_VERSION;
232 if ((version >> 8) != SSL3_VERSION_MAJOR) {
233 if (RECORD_LAYER_is_first_record(&s->rlayer)) {
234 /* Go back to start of packet, look at the five bytes
236 p = RECORD_LAYER_get_packet(&s->rlayer);
237 if (strncmp((char *)p, "GET ", 4) == 0 ||
238 strncmp((char *)p, "POST ", 5) == 0 ||
239 strncmp((char *)p, "HEAD ", 5) == 0 ||
240 strncmp((char *)p, "PUT ", 4) == 0) {
241 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_HTTP_REQUEST);
243 } else if (strncmp((char *)p, "CONNE", 5) == 0) {
244 SSLerr(SSL_F_SSL3_GET_RECORD,
245 SSL_R_HTTPS_PROXY_REQUEST);
249 /* Doesn't look like TLS - don't send an alert */
250 SSLerr(SSL_F_SSL3_GET_RECORD,
251 SSL_R_WRONG_VERSION_NUMBER);
254 SSLerr(SSL_F_SSL3_GET_RECORD,
255 SSL_R_WRONG_VERSION_NUMBER);
256 al = SSL_AD_PROTOCOL_VERSION;
261 if (rr[num_recs].length >
262 SSL3_BUFFER_get_len(rbuf) - SSL3_RT_HEADER_LENGTH) {
263 al = SSL_AD_RECORD_OVERFLOW;
264 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_PACKET_LENGTH_TOO_LONG);
269 /* now s->rlayer.rstate == SSL_ST_READ_BODY */
273 * s->rlayer.rstate == SSL_ST_READ_BODY, get and decode the data.
274 * Calculate how much more data we need to read for the rest of the
277 if (rr[num_recs].rec_version == SSL2_VERSION) {
278 more = rr[num_recs].length + SSL2_RT_HEADER_LENGTH
279 - SSL3_RT_HEADER_LENGTH;
281 more = rr[num_recs].length;
284 /* now s->packet_length == SSL3_RT_HEADER_LENGTH */
286 rret = ssl3_read_n(s, more, more, 1, 0, &n);
288 return rret; /* error or non-blocking io */
291 /* set state for later operations */
292 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);
295 * At this point, s->packet_length == SSL3_RT_HEADER_LENGTH + rr->length,
296 * or s->packet_length == SSL2_RT_HEADER_LENGTH + rr->length
297 * and we have that many bytes in s->packet
299 if (rr[num_recs].rec_version == SSL2_VERSION) {
301 &(RECORD_LAYER_get_packet(&s->rlayer)[SSL2_RT_HEADER_LENGTH]);
304 &(RECORD_LAYER_get_packet(&s->rlayer)[SSL3_RT_HEADER_LENGTH]);
308 * ok, we can now read from 's->packet' data into 'rr' rr->input points
309 * at rr->length bytes, which need to be copied into rr->data by either
310 * the decryption or by the decompression When the data is 'copied' into
311 * the rr->data buffer, rr->input will be pointed at the new buffer
315 * We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
316 * bytes of encrypted compressed stuff.
319 /* check is not needed I believe */
320 if (rr[num_recs].length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
321 al = SSL_AD_RECORD_OVERFLOW;
322 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
326 /* decrypt in place in 'rr->input' */
327 rr[num_recs].data = rr[num_recs].input;
328 rr[num_recs].orig_len = rr[num_recs].length;
330 /* Mark this record as not read by upper layers yet */
331 rr[num_recs].read = 0;
335 /* we have pulled in a full packet so zero things */
336 RECORD_LAYER_reset_packet_length(&s->rlayer);
337 RECORD_LAYER_clear_first_record(&s->rlayer);
338 } while (num_recs < max_recs
339 && rr[num_recs - 1].type == SSL3_RT_APPLICATION_DATA
340 && SSL_USE_EXPLICIT_IV(s)
341 && s->enc_read_ctx != NULL
342 && (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_read_ctx))
343 & EVP_CIPH_FLAG_PIPELINE)
344 && ssl3_record_app_data_waiting(s));
347 * If in encrypt-then-mac mode calculate mac from encrypted record. All
348 * the details below are public so no timing details can leak.
350 if (SSL_USE_ETM(s) && s->read_hash) {
352 /* TODO(size_t): convert this to do size_t properly */
353 imac_size = EVP_MD_CTX_size(s->read_hash);
355 al = SSL_AD_INTERNAL_ERROR;
356 SSLerr(SSL_F_SSL3_GET_RECORD, ERR_LIB_EVP);
359 mac_size = (size_t)imac_size;
360 OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
361 for (j = 0; j < num_recs; j++) {
362 if (rr[j].length < mac_size) {
363 al = SSL_AD_DECODE_ERROR;
364 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT);
367 rr[j].length -= mac_size;
368 mac = rr[j].data + rr[j].length;
369 i = s->method->ssl3_enc->mac(s, &rr[j], md, 0 /* not send */ );
370 if (i < 0 || CRYPTO_memcmp(md, mac, mac_size) != 0) {
371 al = SSL_AD_BAD_RECORD_MAC;
372 SSLerr(SSL_F_SSL3_GET_RECORD,
373 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
379 enc_err = s->method->ssl3_enc->enc(s, rr, num_recs, 0);
382 * 0: (in non-constant time) if the record is publically invalid.
383 * 1: if the padding is valid
384 * -1: if the padding is invalid
387 al = SSL_AD_DECRYPTION_FAILED;
388 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_BLOCK_CIPHER_PAD_IS_WRONG);
392 printf("dec %ld\n", rr->length);
395 for (z = 0; z < rr->length; z++)
396 printf("%02X%c", rr->data[z], ((z + 1) % 16) ? ' ' : '\n');
401 /* r->length is now the compressed data plus mac */
402 if ((sess != NULL) &&
403 (s->enc_read_ctx != NULL) &&
404 (EVP_MD_CTX_md(s->read_hash) != NULL) && !SSL_USE_ETM(s)) {
405 /* s->read_hash != NULL => mac_size != -1 */
406 unsigned char *mac = NULL;
407 unsigned char mac_tmp[EVP_MAX_MD_SIZE];
409 mac_size = EVP_MD_CTX_size(s->read_hash);
410 OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
412 for (j = 0; j < num_recs; j++) {
414 * orig_len is the length of the record before any padding was
415 * removed. This is public information, as is the MAC in use,
416 * therefore we can safely process the record in a different amount
417 * of time if it's too short to possibly contain a MAC.
419 if (rr[j].orig_len < mac_size ||
420 /* CBC records must have a padding length byte too. */
421 (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
422 rr[j].orig_len < mac_size + 1)) {
423 al = SSL_AD_DECODE_ERROR;
424 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT);
428 if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {
430 * We update the length so that the TLS header bytes can be
431 * constructed correctly but we need to extract the MAC in
432 * constant time from within the record, without leaking the
433 * contents of the padding bytes.
436 ssl3_cbc_copy_mac(mac_tmp, &rr[j], mac_size);
437 rr[j].length -= mac_size;
440 * In this case there's no padding, so |rec->orig_len| equals
441 * |rec->length| and we checked that there's enough bytes for
444 rr[j].length -= mac_size;
445 mac = &rr[j].data[rr[j].length];
448 i = s->method->ssl3_enc->mac(s, &rr[j], md, 0 /* not send */ );
449 if (i < 0 || mac == NULL
450 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0)
452 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
459 * A separate 'decryption_failed' alert was introduced with TLS 1.0,
460 * SSL 3.0 only has 'bad_record_mac'. But unless a decryption
461 * failure is directly visible from the ciphertext anyway, we should
462 * not reveal which kind of error occurred -- this might become
463 * visible to an attacker (e.g. via a logfile)
465 al = SSL_AD_BAD_RECORD_MAC;
466 SSLerr(SSL_F_SSL3_GET_RECORD,
467 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
471 for (j = 0; j < num_recs; j++) {
472 /* rr[j].length is now just compressed */
473 if (s->expand != NULL) {
474 if (rr[j].length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
475 al = SSL_AD_RECORD_OVERFLOW;
476 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_COMPRESSED_LENGTH_TOO_LONG);
479 if (!ssl3_do_uncompress(s, &rr[j])) {
480 al = SSL_AD_DECOMPRESSION_FAILURE;
481 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_BAD_DECOMPRESSION);
486 if (rr[j].length > SSL3_RT_MAX_PLAIN_LENGTH) {
487 al = SSL_AD_RECORD_OVERFLOW;
488 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_DATA_LENGTH_TOO_LONG);
494 * So at this point the following is true
495 * rr[j].type is the type of record
496 * rr[j].length == number of bytes in record
497 * rr[j].off == offset to first valid byte
498 * rr[j].data == where to take bytes from, increment after use :-).
501 /* just read a 0 length packet */
502 if (rr[j].length == 0) {
503 RECORD_LAYER_inc_empty_record_count(&s->rlayer);
504 if (RECORD_LAYER_get_empty_record_count(&s->rlayer)
505 > MAX_EMPTY_RECORDS) {
506 al = SSL_AD_UNEXPECTED_MESSAGE;
507 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_RECORD_TOO_SMALL);
511 RECORD_LAYER_reset_empty_record_count(&s->rlayer);
515 RECORD_LAYER_set_numrpipes(&s->rlayer, num_recs);
519 ssl3_send_alert(s, SSL3_AL_FATAL, al);
524 int ssl3_do_uncompress(SSL *ssl, SSL3_RECORD *rr)
526 #ifndef OPENSSL_NO_COMP
529 if (rr->comp == NULL) {
530 rr->comp = (unsigned char *)
531 OPENSSL_malloc(SSL3_RT_MAX_ENCRYPTED_LENGTH);
533 if (rr->comp == NULL)
536 /* TODO(size_t): Convert this call */
537 i = COMP_expand_block(ssl->expand, rr->comp,
538 SSL3_RT_MAX_PLAIN_LENGTH, rr->data, (int)rr->length);
548 int ssl3_do_compress(SSL *ssl, SSL3_RECORD *wr)
550 #ifndef OPENSSL_NO_COMP
553 /* TODO(size_t): Convert this call */
554 i = COMP_compress_block(ssl->compress, wr->data,
555 SSL3_RT_MAX_COMPRESSED_LENGTH,
556 wr->input, (int)wr->length);
562 wr->input = wr->data;
568 * ssl3_enc encrypts/decrypts |n_recs| records in |inrecs|
571 * 0: (in non-constant time) if the record is publically invalid (i.e. too
573 * 1: if the record's padding is valid / the encryption was successful.
574 * -1: if the record's padding is invalid or, if sending, an internal error
577 int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, size_t n_recs, int send)
582 size_t bs, mac_size = 0;
584 const EVP_CIPHER *enc;
588 * We shouldn't ever be called with more than one record in the SSLv3 case
593 ds = s->enc_write_ctx;
594 if (s->enc_write_ctx == NULL)
597 enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
599 ds = s->enc_read_ctx;
600 if (s->enc_read_ctx == NULL)
603 enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
606 if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
607 memmove(rec->data, rec->input, rec->length);
608 rec->input = rec->data;
611 /* TODO(size_t): Convert this call */
612 bs = EVP_CIPHER_CTX_block_size(ds);
616 if ((bs != 1) && send) {
617 i = bs - ((int)l % bs);
619 /* we need to add 'i-1' padding bytes */
622 * the last of these zero bytes will be overwritten with the
625 memset(&rec->input[rec->length], 0, i);
627 rec->input[l - 1] = (i - 1);
631 if (l == 0 || l % bs != 0)
633 /* otherwise, rec->length >= bs */
636 /* TODO(size_t): Convert this call */
637 if (EVP_Cipher(ds, rec->data, rec->input, l) < 1)
640 if (EVP_MD_CTX_md(s->read_hash) != NULL) {
641 /* TODO(size_t): convert me */
642 imac_size = EVP_MD_CTX_size(s->read_hash);
645 mac_size = (size_t)imac_size;
647 if ((bs != 1) && !send)
648 return ssl3_cbc_remove_padding(rec, bs, mac_size);
653 #define MAX_PADDING 256
655 * tls1_enc encrypts/decrypts |n_recs| in |recs|.
658 * 0: (in non-constant time) if the record is publically invalid (i.e. too
660 * 1: if the record's padding is valid / the encryption was successful.
661 * -1: if the record's padding/AEAD-authenticator is invalid or, if sending,
662 * an internal error occurred.
664 int tls1_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int send)
667 size_t reclen[SSL_MAX_PIPELINES];
668 unsigned char buf[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
669 int i, pad = 0, ret, tmpr;
670 size_t bs, mac_size = 0, ctr, padnum, loop;
671 unsigned char padval;
673 const EVP_CIPHER *enc;
676 if (EVP_MD_CTX_md(s->write_hash)) {
677 int n = EVP_MD_CTX_size(s->write_hash);
678 OPENSSL_assert(n >= 0);
680 ds = s->enc_write_ctx;
681 if (s->enc_write_ctx == NULL)
685 enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
686 /* For TLSv1.1 and later explicit IV */
687 if (SSL_USE_EXPLICIT_IV(s)
688 && EVP_CIPHER_mode(enc) == EVP_CIPH_CBC_MODE)
689 ivlen = EVP_CIPHER_iv_length(enc);
693 for (ctr = 0; ctr < n_recs; ctr++) {
694 if (recs[ctr].data != recs[ctr].input) {
696 * we can't write into the input stream: Can this ever
699 SSLerr(SSL_F_TLS1_ENC, ERR_R_INTERNAL_ERROR);
701 } else if (RAND_bytes(recs[ctr].input, ivlen) <= 0) {
702 SSLerr(SSL_F_TLS1_ENC, ERR_R_INTERNAL_ERROR);
709 if (EVP_MD_CTX_md(s->read_hash)) {
710 int n = EVP_MD_CTX_size(s->read_hash);
711 OPENSSL_assert(n >= 0);
713 ds = s->enc_read_ctx;
714 if (s->enc_read_ctx == NULL)
717 enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
720 if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
721 for (ctr = 0; ctr < n_recs; ctr++) {
722 memmove(recs[ctr].data, recs[ctr].input, recs[ctr].length);
723 recs[ctr].input = recs[ctr].data;
727 bs = EVP_CIPHER_block_size(EVP_CIPHER_CTX_cipher(ds));
730 if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
731 & EVP_CIPH_FLAG_PIPELINE)) {
733 * We shouldn't have been called with pipeline data if the
734 * cipher doesn't support pipelining
736 SSLerr(SSL_F_TLS1_ENC, SSL_R_PIPELINE_FAILURE);
740 for (ctr = 0; ctr < n_recs; ctr++) {
741 reclen[ctr] = recs[ctr].length;
743 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
744 & EVP_CIPH_FLAG_AEAD_CIPHER) {
747 seq = send ? RECORD_LAYER_get_write_sequence(&s->rlayer)
748 : RECORD_LAYER_get_read_sequence(&s->rlayer);
750 if (SSL_IS_DTLS(s)) {
751 /* DTLS does not support pipelining */
752 unsigned char dtlsseq[9], *p = dtlsseq;
754 s2n(send ? DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer) :
755 DTLS_RECORD_LAYER_get_r_epoch(&s->rlayer), p);
756 memcpy(p, &seq[2], 6);
757 memcpy(buf[ctr], dtlsseq, 8);
759 memcpy(buf[ctr], seq, 8);
760 for (i = 7; i >= 0; i--) { /* increment */
767 buf[ctr][8] = recs[ctr].type;
768 buf[ctr][9] = (unsigned char)(s->version >> 8);
769 buf[ctr][10] = (unsigned char)(s->version);
770 buf[ctr][11] = recs[ctr].length >> 8;
771 buf[ctr][12] = recs[ctr].length & 0xff;
772 pad = EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_AEAD_TLS1_AAD,
773 EVP_AEAD_TLS1_AAD_LEN, buf[ctr]);
779 recs[ctr].length += pad;
782 } else if ((bs != 1) && send) {
783 padnum = bs - ((int)reclen[ctr] % bs);
785 /* Add weird padding of upto 256 bytes */
787 if (padnum > MAX_PADDING)
789 /* we need to add 'padnum' padding bytes of value padval */
791 for (loop = reclen[ctr]; loop < reclen[ctr] + padnum; loop++)
792 recs[ctr].input[loop] = padval;
793 reclen[ctr] += padnum;
794 recs[ctr].length += padnum;
798 if (reclen[ctr] == 0 || reclen[ctr] % bs != 0)
803 unsigned char *data[SSL_MAX_PIPELINES];
805 /* Set the output buffers */
806 for (ctr = 0; ctr < n_recs; ctr++) {
807 data[ctr] = recs[ctr].data;
809 if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS,
810 n_recs, data) <= 0) {
811 SSLerr(SSL_F_TLS1_ENC, SSL_R_PIPELINE_FAILURE);
813 /* Set the input buffers */
814 for (ctr = 0; ctr < n_recs; ctr++) {
815 data[ctr] = recs[ctr].input;
817 if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_BUFS,
819 || EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_LENS,
820 n_recs, reclen) <= 0) {
821 SSLerr(SSL_F_TLS1_ENC, SSL_R_PIPELINE_FAILURE);
826 tmpr = EVP_Cipher(ds, recs[0].data, recs[0].input, reclen[0]);
827 if ((EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
828 & EVP_CIPH_FLAG_CUSTOM_CIPHER)
831 return -1; /* AEAD can fail to verify MAC */
833 if (EVP_CIPHER_mode(enc) == EVP_CIPH_GCM_MODE) {
834 for (ctr = 0; ctr < n_recs; ctr++) {
835 recs[ctr].data += EVP_GCM_TLS_EXPLICIT_IV_LEN;
836 recs[ctr].input += EVP_GCM_TLS_EXPLICIT_IV_LEN;
837 recs[ctr].length -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
839 } else if (EVP_CIPHER_mode(enc) == EVP_CIPH_CCM_MODE) {
840 for (ctr = 0; ctr < n_recs; ctr++) {
841 recs[ctr].data += EVP_CCM_TLS_EXPLICIT_IV_LEN;
842 recs[ctr].input += EVP_CCM_TLS_EXPLICIT_IV_LEN;
843 recs[ctr].length -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
849 if (!SSL_USE_ETM(s) && EVP_MD_CTX_md(s->read_hash) != NULL) {
850 imac_size = EVP_MD_CTX_size(s->read_hash);
853 mac_size = (size_t)imac_size;
855 if ((bs != 1) && !send) {
857 for (ctr = 0; ctr < n_recs; ctr++) {
858 tmpret = tls1_cbc_remove_padding(s, &recs[ctr], bs, mac_size);
860 * If tmpret == 0 then this means publicly invalid so we can
861 * short circuit things here. Otherwise we must respect constant
866 ret = constant_time_select_int(constant_time_eq_int(tmpret, 1),
871 for (ctr = 0; ctr < n_recs; ctr++) {
872 recs[ctr].length -= pad;
879 int n_ssl3_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int send)
881 unsigned char *mac_sec, *seq;
882 const EVP_MD_CTX *hash;
883 unsigned char *p, rec_char;
889 mac_sec = &(ssl->s3->write_mac_secret[0]);
890 seq = RECORD_LAYER_get_write_sequence(&ssl->rlayer);
891 hash = ssl->write_hash;
893 mac_sec = &(ssl->s3->read_mac_secret[0]);
894 seq = RECORD_LAYER_get_read_sequence(&ssl->rlayer);
895 hash = ssl->read_hash;
898 t = EVP_MD_CTX_size(hash);
902 npad = (48 / md_size) * md_size;
905 EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
906 ssl3_cbc_record_digest_supported(hash)) {
908 * This is a CBC-encrypted record. We must avoid leaking any
909 * timing-side channel information about how many blocks of data we
910 * are hashing because that gives an attacker a timing-oracle.
914 * npad is, at most, 48 bytes and that's with MD5:
915 * 16 + 48 + 8 (sequence bytes) + 1 + 2 = 75.
917 * With SHA-1 (the largest hash speced for SSLv3) the hash size
918 * goes up 4, but npad goes down by 8, resulting in a smaller
921 unsigned char header[75];
923 memcpy(header + j, mac_sec, md_size);
925 memcpy(header + j, ssl3_pad_1, npad);
927 memcpy(header + j, seq, 8);
929 header[j++] = rec->type;
930 header[j++] = rec->length >> 8;
931 header[j++] = rec->length & 0xff;
933 /* Final param == is SSLv3 */
934 if (ssl3_cbc_digest_record(hash,
937 rec->length + md_size, rec->orig_len,
938 mac_sec, md_size, 1) <= 0)
941 unsigned int md_size_u;
942 /* Chop the digest off the end :-) */
943 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
948 rec_char = rec->type;
951 if (EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
952 || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
953 || EVP_DigestUpdate(md_ctx, ssl3_pad_1, npad) <= 0
954 || EVP_DigestUpdate(md_ctx, seq, 8) <= 0
955 || EVP_DigestUpdate(md_ctx, &rec_char, 1) <= 0
956 || EVP_DigestUpdate(md_ctx, md, 2) <= 0
957 || EVP_DigestUpdate(md_ctx, rec->input, rec->length) <= 0
958 || EVP_DigestFinal_ex(md_ctx, md, NULL) <= 0
959 || EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
960 || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
961 || EVP_DigestUpdate(md_ctx, ssl3_pad_2, npad) <= 0
962 || EVP_DigestUpdate(md_ctx, md, md_size) <= 0
963 || EVP_DigestFinal_ex(md_ctx, md, &md_size_u) <= 0) {
964 EVP_MD_CTX_reset(md_ctx);
969 EVP_MD_CTX_free(md_ctx);
972 ssl3_record_sequence_update(seq);
976 int tls1_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int send)
982 EVP_MD_CTX *hmac = NULL, *mac_ctx;
983 unsigned char header[13];
984 int stream_mac = (send ? (ssl->mac_flags & SSL_MAC_FLAG_WRITE_MAC_STREAM)
985 : (ssl->mac_flags & SSL_MAC_FLAG_READ_MAC_STREAM));
989 seq = RECORD_LAYER_get_write_sequence(&ssl->rlayer);
990 hash = ssl->write_hash;
992 seq = RECORD_LAYER_get_read_sequence(&ssl->rlayer);
993 hash = ssl->read_hash;
996 t = EVP_MD_CTX_size(hash);
997 OPENSSL_assert(t >= 0);
1000 /* I should fix this up TLS TLS TLS TLS TLS XXXXXXXX */
1004 hmac = EVP_MD_CTX_new();
1005 if (hmac == NULL || !EVP_MD_CTX_copy(hmac, hash))
1010 if (SSL_IS_DTLS(ssl)) {
1011 unsigned char dtlsseq[8], *p = dtlsseq;
1013 s2n(send ? DTLS_RECORD_LAYER_get_w_epoch(&ssl->rlayer) :
1014 DTLS_RECORD_LAYER_get_r_epoch(&ssl->rlayer), p);
1015 memcpy(p, &seq[2], 6);
1017 memcpy(header, dtlsseq, 8);
1019 memcpy(header, seq, 8);
1021 header[8] = rec->type;
1022 header[9] = (unsigned char)(ssl->version >> 8);
1023 header[10] = (unsigned char)(ssl->version);
1024 header[11] = (rec->length) >> 8;
1025 header[12] = (rec->length) & 0xff;
1027 if (!send && !SSL_USE_ETM(ssl) &&
1028 EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
1029 ssl3_cbc_record_digest_supported(mac_ctx)) {
1031 * This is a CBC-encrypted record. We must avoid leaking any
1032 * timing-side channel information about how many blocks of data we
1033 * are hashing because that gives an attacker a timing-oracle.
1035 /* Final param == not SSLv3 */
1036 /* TODO(size_t): Convert this call */
1037 if (ssl3_cbc_digest_record(mac_ctx,
1040 rec->length + md_size, rec->orig_len,
1041 ssl->s3->read_mac_secret,
1042 ssl->s3->read_mac_secret_size, 0) <= 0) {
1043 EVP_MD_CTX_free(hmac);
1047 /* TODO(size_t): Convert these calls */
1048 if (EVP_DigestSignUpdate(mac_ctx, header, sizeof(header)) <= 0
1049 || EVP_DigestSignUpdate(mac_ctx, rec->input, rec->length) <= 0
1050 || EVP_DigestSignFinal(mac_ctx, md, &md_size) <= 0) {
1051 EVP_MD_CTX_free(hmac);
1054 if (!send && !SSL_USE_ETM(ssl) && FIPS_mode())
1055 if (!tls_fips_digest_extra(ssl->enc_read_ctx,
1056 mac_ctx, rec->input,
1057 rec->length, rec->orig_len)) {
1058 EVP_MD_CTX_free(hmac);
1063 EVP_MD_CTX_free(hmac);
1066 fprintf(stderr, "seq=");
1069 for (z = 0; z < 8; z++)
1070 fprintf(stderr, "%02X ", seq[z]);
1071 fprintf(stderr, "\n");
1073 fprintf(stderr, "rec=");
1076 for (z = 0; z < rec->length; z++)
1077 fprintf(stderr, "%02X ", rec->data[z]);
1078 fprintf(stderr, "\n");
1082 if (!SSL_IS_DTLS(ssl)) {
1083 for (i = 7; i >= 0; i--) {
1092 for (z = 0; z < md_size; z++)
1093 fprintf(stderr, "%02X ", md[z]);
1094 fprintf(stderr, "\n");
1101 * ssl3_cbc_remove_padding removes padding from the decrypted, SSLv3, CBC
1102 * record in |rec| by updating |rec->length| in constant time.
1104 * block_size: the block size of the cipher used to encrypt the record.
1106 * 0: (in non-constant time) if the record is publicly invalid.
1107 * 1: if the padding was valid
1110 int ssl3_cbc_remove_padding(SSL3_RECORD *rec,
1111 size_t block_size, size_t mac_size)
1113 size_t padding_length;
1115 const size_t overhead = 1 /* padding length byte */ + mac_size;
1118 * These lengths are all public so we can test them in non-constant time.
1120 if (overhead > rec->length)
1123 padding_length = rec->data[rec->length - 1];
1124 good = constant_time_ge(rec->length, padding_length + overhead);
1125 /* SSLv3 requires that the padding is minimal. */
1126 good &= constant_time_ge(block_size, padding_length + 1);
1127 rec->length -= good & (padding_length + 1);
1128 return constant_time_select_int(good, 1, -1);
1132 * tls1_cbc_remove_padding removes the CBC padding from the decrypted, TLS, CBC
1133 * record in |rec| in constant time and returns 1 if the padding is valid and
1134 * -1 otherwise. It also removes any explicit IV from the start of the record
1135 * without leaking any timing about whether there was enough space after the
1136 * padding was removed.
1138 * block_size: the block size of the cipher used to encrypt the record.
1140 * 0: (in non-constant time) if the record is publicly invalid.
1141 * 1: if the padding was valid
1144 int tls1_cbc_remove_padding(const SSL *s,
1146 size_t block_size, size_t mac_size)
1149 size_t padding_length, to_check, i;
1150 const size_t overhead = 1 /* padding length byte */ + mac_size;
1151 /* Check if version requires explicit IV */
1152 if (SSL_USE_EXPLICIT_IV(s)) {
1154 * These lengths are all public so we can test them in non-constant
1157 if (overhead + block_size > rec->length)
1159 /* We can now safely skip explicit IV */
1160 rec->data += block_size;
1161 rec->input += block_size;
1162 rec->length -= block_size;
1163 rec->orig_len -= block_size;
1164 } else if (overhead > rec->length)
1167 padding_length = rec->data[rec->length - 1];
1169 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_read_ctx)) &
1170 EVP_CIPH_FLAG_AEAD_CIPHER) {
1171 /* padding is already verified */
1172 rec->length -= padding_length + 1;
1176 good = constant_time_ge(rec->length, overhead + padding_length);
1178 * The padding consists of a length byte at the end of the record and
1179 * then that many bytes of padding, all with the same value as the length
1180 * byte. Thus, with the length byte included, there are i+1 bytes of
1181 * padding. We can't check just |padding_length+1| bytes because that
1182 * leaks decrypted information. Therefore we always have to check the
1183 * maximum amount of padding possible. (Again, the length of the record
1184 * is public information so we can use it.)
1186 to_check = 256; /* maximum amount of padding, inc length byte. */
1187 if (to_check > rec->length)
1188 to_check = rec->length;
1190 for (i = 0; i < to_check; i++) {
1191 unsigned char mask = constant_time_ge_8(padding_length, i);
1192 unsigned char b = rec->data[rec->length - 1 - i];
1194 * The final |padding_length+1| bytes should all have the value
1195 * |padding_length|. Therefore the XOR should be zero.
1197 good &= ~(mask & (padding_length ^ b));
1201 * If any of the final |padding_length+1| bytes had the wrong value, one
1202 * or more of the lower eight bits of |good| will be cleared.
1204 good = constant_time_eq(0xff, good & 0xff);
1205 rec->length -= good & (padding_length + 1);
1207 return constant_time_select_int(good, 1, -1);
1211 * ssl3_cbc_copy_mac copies |md_size| bytes from the end of |rec| to |out| in
1212 * constant time (independent of the concrete value of rec->length, which may
1213 * vary within a 256-byte window).
1215 * ssl3_cbc_remove_padding or tls1_cbc_remove_padding must be called prior to
1219 * rec->orig_len >= md_size
1220 * md_size <= EVP_MAX_MD_SIZE
1222 * If CBC_MAC_ROTATE_IN_PLACE is defined then the rotation is performed with
1223 * variable accesses in a 64-byte-aligned buffer. Assuming that this fits into
1224 * a single or pair of cache-lines, then the variable memory accesses don't
1225 * actually affect the timing. CPUs with smaller cache-lines [if any] are
1226 * not multi-core and are not considered vulnerable to cache-timing attacks.
1228 #define CBC_MAC_ROTATE_IN_PLACE
1230 void ssl3_cbc_copy_mac(unsigned char *out,
1231 const SSL3_RECORD *rec, size_t md_size)
1233 #if defined(CBC_MAC_ROTATE_IN_PLACE)
1234 unsigned char rotated_mac_buf[64 + EVP_MAX_MD_SIZE];
1235 unsigned char *rotated_mac;
1237 unsigned char rotated_mac[EVP_MAX_MD_SIZE];
1241 * mac_end is the index of |rec->data| just after the end of the MAC.
1243 size_t mac_end = rec->length;
1244 size_t mac_start = mac_end - md_size;
1246 * scan_start contains the number of bytes that we can ignore because the
1247 * MAC's position can only vary by 255 bytes.
1249 size_t scan_start = 0;
1251 unsigned div_spoiler;
1252 unsigned rotate_offset;
1254 OPENSSL_assert(rec->orig_len >= md_size);
1255 OPENSSL_assert(md_size <= EVP_MAX_MD_SIZE);
1257 #if defined(CBC_MAC_ROTATE_IN_PLACE)
1258 rotated_mac = rotated_mac_buf + ((0 - (size_t)rotated_mac_buf) & 63);
1261 /* This information is public so it's safe to branch based on it. */
1262 if (rec->orig_len > md_size + 255 + 1)
1263 scan_start = rec->orig_len - (md_size + 255 + 1);
1265 * div_spoiler contains a multiple of md_size that is used to cause the
1266 * modulo operation to be constant time. Without this, the time varies
1267 * based on the amount of padding when running on Intel chips at least.
1268 * The aim of right-shifting md_size is so that the compiler doesn't
1269 * figure out that it can remove div_spoiler as that would require it to
1270 * prove that md_size is always even, which I hope is beyond it.
1272 div_spoiler = md_size >> 1;
1273 div_spoiler <<= (sizeof(div_spoiler) - 1) * 8;
1274 rotate_offset = (div_spoiler + mac_start - scan_start) % md_size;
1276 memset(rotated_mac, 0, md_size);
1277 for (i = scan_start, j = 0; i < rec->orig_len; i++) {
1278 unsigned char mac_started = constant_time_ge_8(i, mac_start);
1279 unsigned char mac_ended = constant_time_ge_8(i, mac_end);
1280 unsigned char b = rec->data[i];
1281 rotated_mac[j++] |= b & mac_started & ~mac_ended;
1282 j &= constant_time_lt(j, md_size);
1285 /* Now rotate the MAC */
1286 #if defined(CBC_MAC_ROTATE_IN_PLACE)
1288 for (i = 0; i < md_size; i++) {
1289 /* in case cache-line is 32 bytes, touch second line */
1290 ((volatile unsigned char *)rotated_mac)[rotate_offset ^ 32];
1291 out[j++] = rotated_mac[rotate_offset++];
1292 rotate_offset &= constant_time_lt(rotate_offset, md_size);
1295 memset(out, 0, md_size);
1296 rotate_offset = md_size - rotate_offset;
1297 rotate_offset &= constant_time_lt(rotate_offset, md_size);
1298 for (i = 0; i < md_size; i++) {
1299 for (j = 0; j < md_size; j++)
1300 out[j] |= rotated_mac[i] & constant_time_eq_8(j, rotate_offset);
1302 rotate_offset &= constant_time_lt(rotate_offset, md_size);
1307 int dtls1_process_record(SSL *s, DTLS1_BITMAP *bitmap)
1315 unsigned char md[EVP_MAX_MD_SIZE];
1317 rr = RECORD_LAYER_get_rrec(&s->rlayer);
1321 * At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length,
1322 * and we have that many bytes in s->packet
1324 rr->input = &(RECORD_LAYER_get_packet(&s->rlayer)[DTLS1_RT_HEADER_LENGTH]);
1327 * ok, we can now read from 's->packet' data into 'rr' rr->input points
1328 * at rr->length bytes, which need to be copied into rr->data by either
1329 * the decryption or by the decompression When the data is 'copied' into
1330 * the rr->data buffer, rr->input will be pointed at the new buffer
1334 * We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
1335 * bytes of encrypted compressed stuff.
1338 /* check is not needed I believe */
1339 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
1340 al = SSL_AD_RECORD_OVERFLOW;
1341 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
1345 /* decrypt in place in 'rr->input' */
1346 rr->data = rr->input;
1347 rr->orig_len = rr->length;
1349 if (SSL_USE_ETM(s) && s->read_hash) {
1351 mac_size = EVP_MD_CTX_size(s->read_hash);
1352 OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
1353 if (rr->orig_len < mac_size) {
1354 al = SSL_AD_DECODE_ERROR;
1355 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_LENGTH_TOO_SHORT);
1358 rr->length -= mac_size;
1359 mac = rr->data + rr->length;
1360 i = s->method->ssl3_enc->mac(s, rr, md, 0 /* not send */ );
1361 if (i < 0 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0) {
1362 al = SSL_AD_BAD_RECORD_MAC;
1363 SSLerr(SSL_F_DTLS1_PROCESS_RECORD,
1364 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
1369 enc_err = s->method->ssl3_enc->enc(s, rr, 1, 0);
1372 * 0: (in non-constant time) if the record is publically invalid.
1373 * 1: if the padding is valid
1374 * -1: if the padding is invalid
1377 /* For DTLS we simply ignore bad packets. */
1379 RECORD_LAYER_reset_packet_length(&s->rlayer);
1383 printf("dec %ld\n", rr->length);
1386 for (z = 0; z < rr->length; z++)
1387 printf("%02X%c", rr->data[z], ((z + 1) % 16) ? ' ' : '\n');
1392 /* r->length is now the compressed data plus mac */
1393 if ((sess != NULL) && !SSL_USE_ETM(s) &&
1394 (s->enc_read_ctx != NULL) && (EVP_MD_CTX_md(s->read_hash) != NULL)) {
1395 /* s->read_hash != NULL => mac_size != -1 */
1396 unsigned char *mac = NULL;
1397 unsigned char mac_tmp[EVP_MAX_MD_SIZE];
1399 /* TODO(size_t): Convert this to do size_t properly */
1400 imac_size = EVP_MD_CTX_size(s->read_hash);
1401 if (imac_size < 0) {
1402 al = SSL_AD_INTERNAL_ERROR;
1403 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, ERR_LIB_EVP);
1406 mac_size = (size_t)imac_size;
1407 OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
1410 * orig_len is the length of the record before any padding was
1411 * removed. This is public information, as is the MAC in use,
1412 * therefore we can safely process the record in a different amount
1413 * of time if it's too short to possibly contain a MAC.
1415 if (rr->orig_len < mac_size ||
1416 /* CBC records must have a padding length byte too. */
1417 (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
1418 rr->orig_len < mac_size + 1)) {
1419 al = SSL_AD_DECODE_ERROR;
1420 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_LENGTH_TOO_SHORT);
1424 if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {
1426 * We update the length so that the TLS header bytes can be
1427 * constructed correctly but we need to extract the MAC in
1428 * constant time from within the record, without leaking the
1429 * contents of the padding bytes.
1432 ssl3_cbc_copy_mac(mac_tmp, rr, mac_size);
1433 rr->length -= mac_size;
1436 * In this case there's no padding, so |rec->orig_len| equals
1437 * |rec->length| and we checked that there's enough bytes for
1440 rr->length -= mac_size;
1441 mac = &rr->data[rr->length];
1444 i = s->method->ssl3_enc->mac(s, rr, md, 0 /* not send */ );
1445 if (i < 0 || mac == NULL
1446 || CRYPTO_memcmp(md, mac, mac_size) != 0)
1448 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
1453 /* decryption failed, silently discard message */
1455 RECORD_LAYER_reset_packet_length(&s->rlayer);
1459 /* r->length is now just compressed */
1460 if (s->expand != NULL) {
1461 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
1462 al = SSL_AD_RECORD_OVERFLOW;
1463 SSLerr(SSL_F_DTLS1_PROCESS_RECORD,
1464 SSL_R_COMPRESSED_LENGTH_TOO_LONG);
1467 if (!ssl3_do_uncompress(s, rr)) {
1468 al = SSL_AD_DECOMPRESSION_FAILURE;
1469 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_BAD_DECOMPRESSION);
1474 if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH) {
1475 al = SSL_AD_RECORD_OVERFLOW;
1476 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_DATA_LENGTH_TOO_LONG);
1482 * So at this point the following is true
1483 * ssl->s3->rrec.type is the type of record
1484 * ssl->s3->rrec.length == number of bytes in record
1485 * ssl->s3->rrec.off == offset to first valid byte
1486 * ssl->s3->rrec.data == where to take bytes from, increment
1490 /* we have pulled in a full packet so zero things */
1491 RECORD_LAYER_reset_packet_length(&s->rlayer);
1493 /* Mark receipt of record. */
1494 dtls1_record_bitmap_update(s, bitmap);
1499 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1505 * retrieve a buffered record that belongs to the current epoch, ie,
1508 #define dtls1_get_processed_record(s) \
1509 dtls1_retrieve_buffered_record((s), \
1510 &(DTLS_RECORD_LAYER_get_processed_rcds(&s->rlayer)))
1513 * Call this to get a new input record.
1514 * It will return <= 0 if more data is needed, normally due to an error
1515 * or non-blocking IO.
1516 * When it finishes, one packet has been decoded and can be found in
1517 * ssl->s3->rrec.type - is the type of record
1518 * ssl->s3->rrec.data, - data
1519 * ssl->s3->rrec.length, - number of bytes
1521 /* used only by dtls1_read_bytes */
1522 int dtls1_get_record(SSL *s)
1524 int ssl_major, ssl_minor;
1528 unsigned char *p = NULL;
1529 unsigned short version;
1530 DTLS1_BITMAP *bitmap;
1531 unsigned int is_next_epoch;
1533 rr = RECORD_LAYER_get_rrec(&s->rlayer);
1537 * The epoch may have changed. If so, process all the pending records.
1538 * This is a non-blocking operation.
1540 if (!dtls1_process_buffered_records(s))
1543 /* if we're renegotiating, then there may be buffered records */
1544 if (dtls1_get_processed_record(s))
1547 /* get something from the wire */
1549 /* check if we have the header */
1550 if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||
1551 (RECORD_LAYER_get_packet_length(&s->rlayer) < DTLS1_RT_HEADER_LENGTH)) {
1552 rret = ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH,
1553 SSL3_BUFFER_get_len(&s->rlayer.rbuf), 0, 1, &n);
1554 /* read timeout is handled by dtls1_read_bytes */
1556 return rret; /* error or non-blocking */
1558 /* this packet contained a partial record, dump it */
1559 if (RECORD_LAYER_get_packet_length(&s->rlayer) !=
1560 DTLS1_RT_HEADER_LENGTH) {
1561 RECORD_LAYER_reset_packet_length(&s->rlayer);
1565 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);
1567 p = RECORD_LAYER_get_packet(&s->rlayer);
1569 if (s->msg_callback)
1570 s->msg_callback(0, 0, SSL3_RT_HEADER, p, DTLS1_RT_HEADER_LENGTH,
1571 s, s->msg_callback_arg);
1573 /* Pull apart the header into the DTLS1_RECORD */
1577 version = (ssl_major << 8) | ssl_minor;
1579 /* sequence number is 64 bits, with top 2 bytes = epoch */
1582 memcpy(&(RECORD_LAYER_get_read_sequence(&s->rlayer)[2]), p, 6);
1587 /* Lets check version */
1588 if (!s->first_packet) {
1589 if (version != s->version) {
1590 /* unexpected version, silently discard */
1592 RECORD_LAYER_reset_packet_length(&s->rlayer);
1597 if ((version & 0xff00) != (s->version & 0xff00)) {
1598 /* wrong version, silently discard record */
1600 RECORD_LAYER_reset_packet_length(&s->rlayer);
1604 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
1605 /* record too long, silently discard it */
1607 RECORD_LAYER_reset_packet_length(&s->rlayer);
1611 /* now s->rlayer.rstate == SSL_ST_READ_BODY */
1614 /* s->rlayer.rstate == SSL_ST_READ_BODY, get and decode the data */
1617 RECORD_LAYER_get_packet_length(&s->rlayer) - DTLS1_RT_HEADER_LENGTH) {
1618 /* now s->packet_length == DTLS1_RT_HEADER_LENGTH */
1620 rret = ssl3_read_n(s, more, more, 1, 1, &n);
1621 /* this packet contained a partial record, dump it */
1622 if (rret <= 0 || n != more) {
1624 RECORD_LAYER_reset_packet_length(&s->rlayer);
1629 * now n == rr->length, and s->packet_length ==
1630 * DTLS1_RT_HEADER_LENGTH + rr->length
1633 /* set state for later operations */
1634 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);
1636 /* match epochs. NULL means the packet is dropped on the floor */
1637 bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);
1638 if (bitmap == NULL) {
1640 RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
1641 goto again; /* get another record */
1643 #ifndef OPENSSL_NO_SCTP
1644 /* Only do replay check if no SCTP bio */
1645 if (!BIO_dgram_is_sctp(SSL_get_rbio(s))) {
1647 /* Check whether this is a repeat, or aged record. */
1649 * TODO: Does it make sense to have replay protection in epoch 0 where
1650 * we have no integrity negotiated yet?
1652 if (!dtls1_record_replay_check(s, bitmap)) {
1654 RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
1655 goto again; /* get another record */
1657 #ifndef OPENSSL_NO_SCTP
1661 /* just read a 0 length packet */
1662 if (rr->length == 0)
1666 * If this record is from the next epoch (either HM or ALERT), and a
1667 * handshake is currently in progress, buffer it since it cannot be
1668 * processed at this time.
1670 if (is_next_epoch) {
1671 if ((SSL_in_init(s) || ossl_statem_get_in_handshake(s))) {
1672 if (dtls1_buffer_record
1673 (s, &(DTLS_RECORD_LAYER_get_unprocessed_rcds(&s->rlayer)),
1678 RECORD_LAYER_reset_packet_length(&s->rlayer);
1682 if (!dtls1_process_record(s, bitmap)) {
1684 RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
1685 goto again; /* get another record */