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, unsigned int 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, unsigned int 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];
138 unsigned int num_recs = 0;
139 unsigned int max_recs;
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)) {
154 rret = ssl3_read_n(s, SSL3_RT_HEADER_LENGTH,
155 SSL3_BUFFER_get_len(rbuf), 0,
156 num_recs == 0 ? 1 : 0, &n);
158 return rret; /* error or non-blocking */
159 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);
161 p = RECORD_LAYER_get_packet(&s->rlayer);
164 * The first record received by the server may be a V2ClientHello.
166 if (s->server && RECORD_LAYER_is_first_record(&s->rlayer)
167 && (p[0] & 0x80) && (p[2] == SSL2_MT_CLIENT_HELLO)) {
171 * |num_recs| here will actually always be 0 because
172 * |num_recs > 0| only ever occurs when we are processing
173 * multiple app data records - which we know isn't the case here
174 * because it is an SSLv2ClientHello. We keep it using
175 * |num_recs| for the sake of consistency
177 rr[num_recs].type = SSL3_RT_HANDSHAKE;
178 rr[num_recs].rec_version = SSL2_VERSION;
180 rr[num_recs].length = ((p[0] & 0x7f) << 8) | p[1];
182 if (rr[num_recs].length > SSL3_BUFFER_get_len(rbuf)
183 - SSL2_RT_HEADER_LENGTH) {
184 al = SSL_AD_RECORD_OVERFLOW;
185 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_PACKET_LENGTH_TOO_LONG);
189 if (rr[num_recs].length < MIN_SSL2_RECORD_LEN) {
190 al = SSL_AD_HANDSHAKE_FAILURE;
191 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT);
195 /* SSLv3+ style record */
197 s->msg_callback(0, 0, SSL3_RT_HEADER, p, 5, s,
198 s->msg_callback_arg);
200 /* Pull apart the header into the SSL3_RECORD */
201 rr[num_recs].type = *(p++);
204 version = (ssl_major << 8) | ssl_minor;
205 rr[num_recs].rec_version = version;
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 mac_size = EVP_MD_CTX_size(s->read_hash);
354 OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
355 for (j = 0; j < num_recs; j++) {
356 if (rr[j].length < mac_size) {
357 al = SSL_AD_DECODE_ERROR;
358 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT);
361 rr[j].length -= mac_size;
362 mac = rr[j].data + rr[j].length;
363 i = s->method->ssl3_enc->mac(s, &rr[j], md, 0 /* not send */ );
364 if (i < 0 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0) {
365 al = SSL_AD_BAD_RECORD_MAC;
366 SSLerr(SSL_F_SSL3_GET_RECORD,
367 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
373 enc_err = s->method->ssl3_enc->enc(s, rr, num_recs, 0);
376 * 0: (in non-constant time) if the record is publically invalid.
377 * 1: if the padding is valid
378 * -1: if the padding is invalid
381 al = SSL_AD_DECRYPTION_FAILED;
382 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_BLOCK_CIPHER_PAD_IS_WRONG);
386 printf("dec %d\n", rr->length);
389 for (z = 0; z < rr->length; z++)
390 printf("%02X%c", rr->data[z], ((z + 1) % 16) ? ' ' : '\n');
395 /* r->length is now the compressed data plus mac */
396 if ((sess != NULL) &&
397 (s->enc_read_ctx != NULL) &&
398 (EVP_MD_CTX_md(s->read_hash) != NULL) && !SSL_USE_ETM(s)) {
399 /* s->read_hash != NULL => mac_size != -1 */
400 unsigned char *mac = NULL;
401 unsigned char mac_tmp[EVP_MAX_MD_SIZE];
403 mac_size = EVP_MD_CTX_size(s->read_hash);
404 OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
406 for (j = 0; j < num_recs; j++) {
408 * orig_len is the length of the record before any padding was
409 * removed. This is public information, as is the MAC in use,
410 * therefore we can safely process the record in a different amount
411 * of time if it's too short to possibly contain a MAC.
413 if (rr[j].orig_len < mac_size ||
414 /* CBC records must have a padding length byte too. */
415 (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
416 rr[j].orig_len < mac_size + 1)) {
417 al = SSL_AD_DECODE_ERROR;
418 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT);
422 if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {
424 * We update the length so that the TLS header bytes can be
425 * constructed correctly but we need to extract the MAC in
426 * constant time from within the record, without leaking the
427 * contents of the padding bytes.
430 ssl3_cbc_copy_mac(mac_tmp, &rr[j], mac_size);
431 rr[j].length -= mac_size;
434 * In this case there's no padding, so |rec->orig_len| equals
435 * |rec->length| and we checked that there's enough bytes for
438 rr[j].length -= mac_size;
439 mac = &rr[j].data[rr[j].length];
442 i = s->method->ssl3_enc->mac(s, &rr[j], md, 0 /* not send */ );
443 if (i < 0 || mac == NULL
444 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0)
446 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
453 * A separate 'decryption_failed' alert was introduced with TLS 1.0,
454 * SSL 3.0 only has 'bad_record_mac'. But unless a decryption
455 * failure is directly visible from the ciphertext anyway, we should
456 * not reveal which kind of error occurred -- this might become
457 * visible to an attacker (e.g. via a logfile)
459 al = SSL_AD_BAD_RECORD_MAC;
460 SSLerr(SSL_F_SSL3_GET_RECORD,
461 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
465 for (j = 0; j < num_recs; j++) {
466 /* rr[j].length is now just compressed */
467 if (s->expand != NULL) {
468 if (rr[j].length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
469 al = SSL_AD_RECORD_OVERFLOW;
470 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_COMPRESSED_LENGTH_TOO_LONG);
473 if (!ssl3_do_uncompress(s, &rr[j])) {
474 al = SSL_AD_DECOMPRESSION_FAILURE;
475 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_BAD_DECOMPRESSION);
480 if (rr[j].length > SSL3_RT_MAX_PLAIN_LENGTH) {
481 al = SSL_AD_RECORD_OVERFLOW;
482 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_DATA_LENGTH_TOO_LONG);
488 * So at this point the following is true
489 * rr[j].type is the type of record
490 * rr[j].length == number of bytes in record
491 * rr[j].off == offset to first valid byte
492 * rr[j].data == where to take bytes from, increment after use :-).
495 /* just read a 0 length packet */
496 if (rr[j].length == 0) {
497 RECORD_LAYER_inc_empty_record_count(&s->rlayer);
498 if (RECORD_LAYER_get_empty_record_count(&s->rlayer)
499 > MAX_EMPTY_RECORDS) {
500 al = SSL_AD_UNEXPECTED_MESSAGE;
501 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_RECORD_TOO_SMALL);
505 RECORD_LAYER_reset_empty_record_count(&s->rlayer);
509 RECORD_LAYER_set_numrpipes(&s->rlayer, num_recs);
513 ssl3_send_alert(s, SSL3_AL_FATAL, al);
518 int ssl3_do_uncompress(SSL *ssl, SSL3_RECORD *rr)
520 #ifndef OPENSSL_NO_COMP
523 if (rr->comp == NULL) {
524 rr->comp = (unsigned char *)
525 OPENSSL_malloc(SSL3_RT_MAX_ENCRYPTED_LENGTH);
527 if (rr->comp == NULL)
530 i = COMP_expand_block(ssl->expand, rr->comp,
531 SSL3_RT_MAX_PLAIN_LENGTH, rr->data, (int)rr->length);
541 int ssl3_do_compress(SSL *ssl, SSL3_RECORD *wr)
543 #ifndef OPENSSL_NO_COMP
546 i = COMP_compress_block(ssl->compress, wr->data,
547 SSL3_RT_MAX_COMPRESSED_LENGTH,
548 wr->input, (int)wr->length);
554 wr->input = wr->data;
560 * ssl3_enc encrypts/decrypts |n_recs| records in |inrecs|
563 * 0: (in non-constant time) if the record is publically invalid (i.e. too
565 * 1: if the record's padding is valid / the encryption was successful.
566 * -1: if the record's padding is invalid or, if sending, an internal error
569 int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, unsigned int n_recs, int send)
574 int bs, i, mac_size = 0;
575 const EVP_CIPHER *enc;
579 * We shouldn't ever be called with more than one record in the SSLv3 case
584 ds = s->enc_write_ctx;
585 if (s->enc_write_ctx == NULL)
588 enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
590 ds = s->enc_read_ctx;
591 if (s->enc_read_ctx == NULL)
594 enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
597 if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
598 memmove(rec->data, rec->input, rec->length);
599 rec->input = rec->data;
602 bs = EVP_CIPHER_CTX_block_size(ds);
606 if ((bs != 1) && send) {
607 i = bs - ((int)l % bs);
609 /* we need to add 'i-1' padding bytes */
612 * the last of these zero bytes will be overwritten with the
615 memset(&rec->input[rec->length], 0, i);
617 rec->input[l - 1] = (i - 1);
621 if (l == 0 || l % bs != 0)
623 /* otherwise, rec->length >= bs */
626 if (EVP_Cipher(ds, rec->data, rec->input, l) < 1)
629 if (EVP_MD_CTX_md(s->read_hash) != NULL)
630 mac_size = EVP_MD_CTX_size(s->read_hash);
631 if ((bs != 1) && !send)
632 return ssl3_cbc_remove_padding(rec, bs, mac_size);
638 * tls1_enc encrypts/decrypts |n_recs| in |recs|.
641 * 0: (in non-constant time) if the record is publically invalid (i.e. too
643 * 1: if the record's padding is valid / the encryption was successful.
644 * -1: if the record's padding/AEAD-authenticator is invalid or, if sending,
645 * an internal error occurred.
647 int tls1_enc(SSL *s, SSL3_RECORD *recs, unsigned int n_recs, int send)
650 size_t reclen[SSL_MAX_PIPELINES];
651 unsigned char buf[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
652 int bs, i, j, k, pad = 0, ret, mac_size = 0;
653 const EVP_CIPHER *enc;
657 if (EVP_MD_CTX_md(s->write_hash)) {
658 int n = EVP_MD_CTX_size(s->write_hash);
659 OPENSSL_assert(n >= 0);
661 ds = s->enc_write_ctx;
662 if (s->enc_write_ctx == NULL)
666 enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
667 /* For TLSv1.1 and later explicit IV */
668 if (SSL_USE_EXPLICIT_IV(s)
669 && EVP_CIPHER_mode(enc) == EVP_CIPH_CBC_MODE)
670 ivlen = EVP_CIPHER_iv_length(enc);
674 for (ctr = 0; ctr < n_recs; ctr++) {
675 if (recs[ctr].data != recs[ctr].input) {
677 * we can't write into the input stream: Can this ever
680 SSLerr(SSL_F_TLS1_ENC, ERR_R_INTERNAL_ERROR);
682 } else if (RAND_bytes(recs[ctr].input, ivlen) <= 0) {
683 SSLerr(SSL_F_TLS1_ENC, ERR_R_INTERNAL_ERROR);
690 if (EVP_MD_CTX_md(s->read_hash)) {
691 int n = EVP_MD_CTX_size(s->read_hash);
692 OPENSSL_assert(n >= 0);
694 ds = s->enc_read_ctx;
695 if (s->enc_read_ctx == NULL)
698 enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
701 if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
702 for (ctr = 0; ctr < n_recs; ctr++) {
703 memmove(recs[ctr].data, recs[ctr].input, recs[ctr].length);
704 recs[ctr].input = recs[ctr].data;
708 bs = EVP_CIPHER_block_size(EVP_CIPHER_CTX_cipher(ds));
711 if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
712 & EVP_CIPH_FLAG_PIPELINE)) {
714 * We shouldn't have been called with pipeline data if the
715 * cipher doesn't support pipelining
717 SSLerr(SSL_F_TLS1_ENC, SSL_R_PIPELINE_FAILURE);
721 for (ctr = 0; ctr < n_recs; ctr++) {
722 reclen[ctr] = recs[ctr].length;
724 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
725 & EVP_CIPH_FLAG_AEAD_CIPHER) {
728 seq = send ? RECORD_LAYER_get_write_sequence(&s->rlayer)
729 : RECORD_LAYER_get_read_sequence(&s->rlayer);
731 if (SSL_IS_DTLS(s)) {
732 /* DTLS does not support pipelining */
733 unsigned char dtlsseq[9], *p = dtlsseq;
735 s2n(send ? DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer) :
736 DTLS_RECORD_LAYER_get_r_epoch(&s->rlayer), p);
737 memcpy(p, &seq[2], 6);
738 memcpy(buf[ctr], dtlsseq, 8);
740 memcpy(buf[ctr], seq, 8);
741 for (i = 7; i >= 0; i--) { /* increment */
748 buf[ctr][8] = recs[ctr].type;
749 buf[ctr][9] = (unsigned char)(s->version >> 8);
750 buf[ctr][10] = (unsigned char)(s->version);
751 buf[ctr][11] = recs[ctr].length >> 8;
752 buf[ctr][12] = recs[ctr].length & 0xff;
753 pad = EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_AEAD_TLS1_AAD,
754 EVP_AEAD_TLS1_AAD_LEN, buf[ctr]);
760 recs[ctr].length += pad;
763 } else if ((bs != 1) && send) {
764 i = bs - ((int)reclen[ctr] % bs);
766 /* Add weird padding of upto 256 bytes */
768 /* we need to add 'i' padding bytes of value j */
770 for (k = (int)reclen[ctr]; k < (int)(reclen[ctr] + i); k++)
771 recs[ctr].input[k] = j;
773 recs[ctr].length += i;
777 if (reclen[ctr] == 0 || reclen[ctr] % bs != 0)
782 unsigned char *data[SSL_MAX_PIPELINES];
784 /* Set the output buffers */
785 for (ctr = 0; ctr < n_recs; ctr++) {
786 data[ctr] = recs[ctr].data;
788 if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS,
789 n_recs, data) <= 0) {
790 SSLerr(SSL_F_TLS1_ENC, SSL_R_PIPELINE_FAILURE);
792 /* Set the input buffers */
793 for (ctr = 0; ctr < n_recs; ctr++) {
794 data[ctr] = recs[ctr].input;
796 if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_BUFS,
798 || EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_LENS,
799 n_recs, reclen) <= 0) {
800 SSLerr(SSL_F_TLS1_ENC, SSL_R_PIPELINE_FAILURE);
805 i = EVP_Cipher(ds, recs[0].data, recs[0].input, reclen[0]);
806 if ((EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
807 & EVP_CIPH_FLAG_CUSTOM_CIPHER)
810 return -1; /* AEAD can fail to verify MAC */
812 if (EVP_CIPHER_mode(enc) == EVP_CIPH_GCM_MODE) {
813 for (ctr = 0; ctr < n_recs; ctr++) {
814 recs[ctr].data += EVP_GCM_TLS_EXPLICIT_IV_LEN;
815 recs[ctr].input += EVP_GCM_TLS_EXPLICIT_IV_LEN;
816 recs[ctr].length -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
818 } else if (EVP_CIPHER_mode(enc) == EVP_CIPH_CCM_MODE) {
819 for (ctr = 0; ctr < n_recs; ctr++) {
820 recs[ctr].data += EVP_CCM_TLS_EXPLICIT_IV_LEN;
821 recs[ctr].input += EVP_CCM_TLS_EXPLICIT_IV_LEN;
822 recs[ctr].length -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
828 if (!SSL_USE_ETM(s) && EVP_MD_CTX_md(s->read_hash) != NULL)
829 mac_size = EVP_MD_CTX_size(s->read_hash);
830 if ((bs != 1) && !send) {
832 for (ctr = 0; ctr < n_recs; ctr++) {
833 tmpret = tls1_cbc_remove_padding(s, &recs[ctr], bs, mac_size);
835 * If tmpret == 0 then this means publicly invalid so we can
836 * short circuit things here. Otherwise we must respect constant
841 ret = constant_time_select_int(constant_time_eq_int(tmpret, 1),
846 for (ctr = 0; ctr < n_recs; ctr++) {
847 recs[ctr].length -= pad;
854 int n_ssl3_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int send)
856 unsigned char *mac_sec, *seq;
857 const EVP_MD_CTX *hash;
858 unsigned char *p, rec_char;
864 mac_sec = &(ssl->s3->write_mac_secret[0]);
865 seq = RECORD_LAYER_get_write_sequence(&ssl->rlayer);
866 hash = ssl->write_hash;
868 mac_sec = &(ssl->s3->read_mac_secret[0]);
869 seq = RECORD_LAYER_get_read_sequence(&ssl->rlayer);
870 hash = ssl->read_hash;
873 t = EVP_MD_CTX_size(hash);
877 npad = (48 / md_size) * md_size;
880 EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
881 ssl3_cbc_record_digest_supported(hash)) {
883 * This is a CBC-encrypted record. We must avoid leaking any
884 * timing-side channel information about how many blocks of data we
885 * are hashing because that gives an attacker a timing-oracle.
889 * npad is, at most, 48 bytes and that's with MD5:
890 * 16 + 48 + 8 (sequence bytes) + 1 + 2 = 75.
892 * With SHA-1 (the largest hash speced for SSLv3) the hash size
893 * goes up 4, but npad goes down by 8, resulting in a smaller
896 unsigned char header[75];
898 memcpy(header + j, mac_sec, md_size);
900 memcpy(header + j, ssl3_pad_1, npad);
902 memcpy(header + j, seq, 8);
904 header[j++] = rec->type;
905 header[j++] = rec->length >> 8;
906 header[j++] = rec->length & 0xff;
908 /* Final param == is SSLv3 */
909 if (ssl3_cbc_digest_record(hash,
912 rec->length + md_size, rec->orig_len,
913 mac_sec, md_size, 1) <= 0)
916 unsigned int md_size_u;
917 /* Chop the digest off the end :-) */
918 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
923 rec_char = rec->type;
926 if (EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
927 || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
928 || EVP_DigestUpdate(md_ctx, ssl3_pad_1, npad) <= 0
929 || EVP_DigestUpdate(md_ctx, seq, 8) <= 0
930 || EVP_DigestUpdate(md_ctx, &rec_char, 1) <= 0
931 || EVP_DigestUpdate(md_ctx, md, 2) <= 0
932 || EVP_DigestUpdate(md_ctx, rec->input, rec->length) <= 0
933 || EVP_DigestFinal_ex(md_ctx, md, NULL) <= 0
934 || EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
935 || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
936 || EVP_DigestUpdate(md_ctx, ssl3_pad_2, npad) <= 0
937 || EVP_DigestUpdate(md_ctx, md, md_size) <= 0
938 || EVP_DigestFinal_ex(md_ctx, md, &md_size_u) <= 0) {
939 EVP_MD_CTX_reset(md_ctx);
944 EVP_MD_CTX_free(md_ctx);
947 ssl3_record_sequence_update(seq);
951 int tls1_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int send)
957 EVP_MD_CTX *hmac = NULL, *mac_ctx;
958 unsigned char header[13];
959 int stream_mac = (send ? (ssl->mac_flags & SSL_MAC_FLAG_WRITE_MAC_STREAM)
960 : (ssl->mac_flags & SSL_MAC_FLAG_READ_MAC_STREAM));
964 seq = RECORD_LAYER_get_write_sequence(&ssl->rlayer);
965 hash = ssl->write_hash;
967 seq = RECORD_LAYER_get_read_sequence(&ssl->rlayer);
968 hash = ssl->read_hash;
971 t = EVP_MD_CTX_size(hash);
972 OPENSSL_assert(t >= 0);
975 /* I should fix this up TLS TLS TLS TLS TLS XXXXXXXX */
979 hmac = EVP_MD_CTX_new();
980 if (hmac == NULL || !EVP_MD_CTX_copy(hmac, hash))
985 if (SSL_IS_DTLS(ssl)) {
986 unsigned char dtlsseq[8], *p = dtlsseq;
988 s2n(send ? DTLS_RECORD_LAYER_get_w_epoch(&ssl->rlayer) :
989 DTLS_RECORD_LAYER_get_r_epoch(&ssl->rlayer), p);
990 memcpy(p, &seq[2], 6);
992 memcpy(header, dtlsseq, 8);
994 memcpy(header, seq, 8);
996 header[8] = rec->type;
997 header[9] = (unsigned char)(ssl->version >> 8);
998 header[10] = (unsigned char)(ssl->version);
999 header[11] = (rec->length) >> 8;
1000 header[12] = (rec->length) & 0xff;
1002 if (!send && !SSL_USE_ETM(ssl) &&
1003 EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
1004 ssl3_cbc_record_digest_supported(mac_ctx)) {
1006 * This is a CBC-encrypted record. We must avoid leaking any
1007 * timing-side channel information about how many blocks of data we
1008 * are hashing because that gives an attacker a timing-oracle.
1010 /* Final param == not SSLv3 */
1011 if (ssl3_cbc_digest_record(mac_ctx,
1014 rec->length + md_size, rec->orig_len,
1015 ssl->s3->read_mac_secret,
1016 ssl->s3->read_mac_secret_size, 0) <= 0) {
1017 EVP_MD_CTX_free(hmac);
1021 if (EVP_DigestSignUpdate(mac_ctx, header, sizeof(header)) <= 0
1022 || EVP_DigestSignUpdate(mac_ctx, rec->input, rec->length) <= 0
1023 || EVP_DigestSignFinal(mac_ctx, md, &md_size) <= 0) {
1024 EVP_MD_CTX_free(hmac);
1027 if (!send && !SSL_USE_ETM(ssl) && FIPS_mode())
1028 if (!tls_fips_digest_extra(ssl->enc_read_ctx,
1029 mac_ctx, rec->input,
1030 rec->length, rec->orig_len)) {
1031 EVP_MD_CTX_free(hmac);
1036 EVP_MD_CTX_free(hmac);
1039 fprintf(stderr, "seq=");
1042 for (z = 0; z < 8; z++)
1043 fprintf(stderr, "%02X ", seq[z]);
1044 fprintf(stderr, "\n");
1046 fprintf(stderr, "rec=");
1049 for (z = 0; z < rec->length; z++)
1050 fprintf(stderr, "%02X ", rec->data[z]);
1051 fprintf(stderr, "\n");
1055 if (!SSL_IS_DTLS(ssl)) {
1056 for (i = 7; i >= 0; i--) {
1065 for (z = 0; z < md_size; z++)
1066 fprintf(stderr, "%02X ", md[z]);
1067 fprintf(stderr, "\n");
1074 * ssl3_cbc_remove_padding removes padding from the decrypted, SSLv3, CBC
1075 * record in |rec| by updating |rec->length| in constant time.
1077 * block_size: the block size of the cipher used to encrypt the record.
1079 * 0: (in non-constant time) if the record is publicly invalid.
1080 * 1: if the padding was valid
1083 int ssl3_cbc_remove_padding(SSL3_RECORD *rec,
1084 unsigned block_size, unsigned mac_size)
1086 unsigned padding_length, good;
1087 const unsigned overhead = 1 /* padding length byte */ + mac_size;
1090 * These lengths are all public so we can test them in non-constant time.
1092 if (overhead > rec->length)
1095 padding_length = rec->data[rec->length - 1];
1096 good = constant_time_ge(rec->length, padding_length + overhead);
1097 /* SSLv3 requires that the padding is minimal. */
1098 good &= constant_time_ge(block_size, padding_length + 1);
1099 rec->length -= good & (padding_length + 1);
1100 return constant_time_select_int(good, 1, -1);
1104 * tls1_cbc_remove_padding removes the CBC padding from the decrypted, TLS, CBC
1105 * record in |rec| in constant time and returns 1 if the padding is valid and
1106 * -1 otherwise. It also removes any explicit IV from the start of the record
1107 * without leaking any timing about whether there was enough space after the
1108 * padding was removed.
1110 * block_size: the block size of the cipher used to encrypt the record.
1112 * 0: (in non-constant time) if the record is publicly invalid.
1113 * 1: if the padding was valid
1116 int tls1_cbc_remove_padding(const SSL *s,
1118 unsigned block_size, unsigned mac_size)
1120 unsigned padding_length, good, to_check, i;
1121 const unsigned overhead = 1 /* padding length byte */ + mac_size;
1122 /* Check if version requires explicit IV */
1123 if (SSL_USE_EXPLICIT_IV(s)) {
1125 * These lengths are all public so we can test them in non-constant
1128 if (overhead + block_size > rec->length)
1130 /* We can now safely skip explicit IV */
1131 rec->data += block_size;
1132 rec->input += block_size;
1133 rec->length -= block_size;
1134 rec->orig_len -= block_size;
1135 } else if (overhead > rec->length)
1138 padding_length = rec->data[rec->length - 1];
1140 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_read_ctx)) &
1141 EVP_CIPH_FLAG_AEAD_CIPHER) {
1142 /* padding is already verified */
1143 rec->length -= padding_length + 1;
1147 good = constant_time_ge(rec->length, overhead + padding_length);
1149 * The padding consists of a length byte at the end of the record and
1150 * then that many bytes of padding, all with the same value as the length
1151 * byte. Thus, with the length byte included, there are i+1 bytes of
1152 * padding. We can't check just |padding_length+1| bytes because that
1153 * leaks decrypted information. Therefore we always have to check the
1154 * maximum amount of padding possible. (Again, the length of the record
1155 * is public information so we can use it.)
1157 to_check = 256; /* maximum amount of padding, inc length byte. */
1158 if (to_check > rec->length)
1159 to_check = rec->length;
1161 for (i = 0; i < to_check; i++) {
1162 unsigned char mask = constant_time_ge_8(padding_length, i);
1163 unsigned char b = rec->data[rec->length - 1 - i];
1165 * The final |padding_length+1| bytes should all have the value
1166 * |padding_length|. Therefore the XOR should be zero.
1168 good &= ~(mask & (padding_length ^ b));
1172 * If any of the final |padding_length+1| bytes had the wrong value, one
1173 * or more of the lower eight bits of |good| will be cleared.
1175 good = constant_time_eq(0xff, good & 0xff);
1176 rec->length -= good & (padding_length + 1);
1178 return constant_time_select_int(good, 1, -1);
1182 * ssl3_cbc_copy_mac copies |md_size| bytes from the end of |rec| to |out| in
1183 * constant time (independent of the concrete value of rec->length, which may
1184 * vary within a 256-byte window).
1186 * ssl3_cbc_remove_padding or tls1_cbc_remove_padding must be called prior to
1190 * rec->orig_len >= md_size
1191 * md_size <= EVP_MAX_MD_SIZE
1193 * If CBC_MAC_ROTATE_IN_PLACE is defined then the rotation is performed with
1194 * variable accesses in a 64-byte-aligned buffer. Assuming that this fits into
1195 * a single or pair of cache-lines, then the variable memory accesses don't
1196 * actually affect the timing. CPUs with smaller cache-lines [if any] are
1197 * not multi-core and are not considered vulnerable to cache-timing attacks.
1199 #define CBC_MAC_ROTATE_IN_PLACE
1201 void ssl3_cbc_copy_mac(unsigned char *out,
1202 const SSL3_RECORD *rec, unsigned md_size)
1204 #if defined(CBC_MAC_ROTATE_IN_PLACE)
1205 unsigned char rotated_mac_buf[64 + EVP_MAX_MD_SIZE];
1206 unsigned char *rotated_mac;
1208 unsigned char rotated_mac[EVP_MAX_MD_SIZE];
1212 * mac_end is the index of |rec->data| just after the end of the MAC.
1214 unsigned mac_end = rec->length;
1215 unsigned mac_start = mac_end - md_size;
1217 * scan_start contains the number of bytes that we can ignore because the
1218 * MAC's position can only vary by 255 bytes.
1220 unsigned scan_start = 0;
1222 unsigned div_spoiler;
1223 unsigned rotate_offset;
1225 OPENSSL_assert(rec->orig_len >= md_size);
1226 OPENSSL_assert(md_size <= EVP_MAX_MD_SIZE);
1228 #if defined(CBC_MAC_ROTATE_IN_PLACE)
1229 rotated_mac = rotated_mac_buf + ((0 - (size_t)rotated_mac_buf) & 63);
1232 /* This information is public so it's safe to branch based on it. */
1233 if (rec->orig_len > md_size + 255 + 1)
1234 scan_start = rec->orig_len - (md_size + 255 + 1);
1236 * div_spoiler contains a multiple of md_size that is used to cause the
1237 * modulo operation to be constant time. Without this, the time varies
1238 * based on the amount of padding when running on Intel chips at least.
1239 * The aim of right-shifting md_size is so that the compiler doesn't
1240 * figure out that it can remove div_spoiler as that would require it to
1241 * prove that md_size is always even, which I hope is beyond it.
1243 div_spoiler = md_size >> 1;
1244 div_spoiler <<= (sizeof(div_spoiler) - 1) * 8;
1245 rotate_offset = (div_spoiler + mac_start - scan_start) % md_size;
1247 memset(rotated_mac, 0, md_size);
1248 for (i = scan_start, j = 0; i < rec->orig_len; i++) {
1249 unsigned char mac_started = constant_time_ge_8(i, mac_start);
1250 unsigned char mac_ended = constant_time_ge_8(i, mac_end);
1251 unsigned char b = rec->data[i];
1252 rotated_mac[j++] |= b & mac_started & ~mac_ended;
1253 j &= constant_time_lt(j, md_size);
1256 /* Now rotate the MAC */
1257 #if defined(CBC_MAC_ROTATE_IN_PLACE)
1259 for (i = 0; i < md_size; i++) {
1260 /* in case cache-line is 32 bytes, touch second line */
1261 ((volatile unsigned char *)rotated_mac)[rotate_offset ^ 32];
1262 out[j++] = rotated_mac[rotate_offset++];
1263 rotate_offset &= constant_time_lt(rotate_offset, md_size);
1266 memset(out, 0, md_size);
1267 rotate_offset = md_size - rotate_offset;
1268 rotate_offset &= constant_time_lt(rotate_offset, md_size);
1269 for (i = 0; i < md_size; i++) {
1270 for (j = 0; j < md_size; j++)
1271 out[j] |= rotated_mac[i] & constant_time_eq_8(j, rotate_offset);
1273 rotate_offset &= constant_time_lt(rotate_offset, md_size);
1278 int dtls1_process_record(SSL *s, DTLS1_BITMAP *bitmap)
1284 unsigned int mac_size;
1285 unsigned char md[EVP_MAX_MD_SIZE];
1287 rr = RECORD_LAYER_get_rrec(&s->rlayer);
1291 * At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length,
1292 * and we have that many bytes in s->packet
1294 rr->input = &(RECORD_LAYER_get_packet(&s->rlayer)[DTLS1_RT_HEADER_LENGTH]);
1297 * ok, we can now read from 's->packet' data into 'rr' rr->input points
1298 * at rr->length bytes, which need to be copied into rr->data by either
1299 * the decryption or by the decompression When the data is 'copied' into
1300 * the rr->data buffer, rr->input will be pointed at the new buffer
1304 * We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
1305 * bytes of encrypted compressed stuff.
1308 /* check is not needed I believe */
1309 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
1310 al = SSL_AD_RECORD_OVERFLOW;
1311 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
1315 /* decrypt in place in 'rr->input' */
1316 rr->data = rr->input;
1317 rr->orig_len = rr->length;
1319 if (SSL_USE_ETM(s) && s->read_hash) {
1321 mac_size = EVP_MD_CTX_size(s->read_hash);
1322 OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
1323 if (rr->orig_len < mac_size) {
1324 al = SSL_AD_DECODE_ERROR;
1325 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_LENGTH_TOO_SHORT);
1328 rr->length -= mac_size;
1329 mac = rr->data + rr->length;
1330 i = s->method->ssl3_enc->mac(s, rr, md, 0 /* not send */ );
1331 if (i < 0 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0) {
1332 al = SSL_AD_BAD_RECORD_MAC;
1333 SSLerr(SSL_F_DTLS1_PROCESS_RECORD,
1334 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
1339 enc_err = s->method->ssl3_enc->enc(s, rr, 1, 0);
1342 * 0: (in non-constant time) if the record is publically invalid.
1343 * 1: if the padding is valid
1344 * -1: if the padding is invalid
1347 /* For DTLS we simply ignore bad packets. */
1349 RECORD_LAYER_reset_packet_length(&s->rlayer);
1353 printf("dec %d\n", rr->length);
1356 for (z = 0; z < rr->length; z++)
1357 printf("%02X%c", rr->data[z], ((z + 1) % 16) ? ' ' : '\n');
1362 /* r->length is now the compressed data plus mac */
1363 if ((sess != NULL) && !SSL_USE_ETM(s) &&
1364 (s->enc_read_ctx != NULL) && (EVP_MD_CTX_md(s->read_hash) != NULL)) {
1365 /* s->read_hash != NULL => mac_size != -1 */
1366 unsigned char *mac = NULL;
1367 unsigned char mac_tmp[EVP_MAX_MD_SIZE];
1368 mac_size = EVP_MD_CTX_size(s->read_hash);
1369 OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
1372 * orig_len is the length of the record before any padding was
1373 * removed. This is public information, as is the MAC in use,
1374 * therefore we can safely process the record in a different amount
1375 * of time if it's too short to possibly contain a MAC.
1377 if (rr->orig_len < mac_size ||
1378 /* CBC records must have a padding length byte too. */
1379 (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
1380 rr->orig_len < mac_size + 1)) {
1381 al = SSL_AD_DECODE_ERROR;
1382 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_LENGTH_TOO_SHORT);
1386 if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {
1388 * We update the length so that the TLS header bytes can be
1389 * constructed correctly but we need to extract the MAC in
1390 * constant time from within the record, without leaking the
1391 * contents of the padding bytes.
1394 ssl3_cbc_copy_mac(mac_tmp, rr, mac_size);
1395 rr->length -= mac_size;
1398 * In this case there's no padding, so |rec->orig_len| equals
1399 * |rec->length| and we checked that there's enough bytes for
1402 rr->length -= mac_size;
1403 mac = &rr->data[rr->length];
1406 i = s->method->ssl3_enc->mac(s, rr, md, 0 /* not send */ );
1407 if (i < 0 || mac == NULL
1408 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0)
1410 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
1415 /* decryption failed, silently discard message */
1417 RECORD_LAYER_reset_packet_length(&s->rlayer);
1421 /* r->length is now just compressed */
1422 if (s->expand != NULL) {
1423 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
1424 al = SSL_AD_RECORD_OVERFLOW;
1425 SSLerr(SSL_F_DTLS1_PROCESS_RECORD,
1426 SSL_R_COMPRESSED_LENGTH_TOO_LONG);
1429 if (!ssl3_do_uncompress(s, rr)) {
1430 al = SSL_AD_DECOMPRESSION_FAILURE;
1431 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_BAD_DECOMPRESSION);
1436 if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH) {
1437 al = SSL_AD_RECORD_OVERFLOW;
1438 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_DATA_LENGTH_TOO_LONG);
1444 * So at this point the following is true
1445 * ssl->s3->rrec.type is the type of record
1446 * ssl->s3->rrec.length == number of bytes in record
1447 * ssl->s3->rrec.off == offset to first valid byte
1448 * ssl->s3->rrec.data == where to take bytes from, increment
1452 /* we have pulled in a full packet so zero things */
1453 RECORD_LAYER_reset_packet_length(&s->rlayer);
1455 /* Mark receipt of record. */
1456 dtls1_record_bitmap_update(s, bitmap);
1461 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1467 * retrieve a buffered record that belongs to the current epoch, ie,
1470 #define dtls1_get_processed_record(s) \
1471 dtls1_retrieve_buffered_record((s), \
1472 &(DTLS_RECORD_LAYER_get_processed_rcds(&s->rlayer)))
1475 * Call this to get a new input record.
1476 * It will return <= 0 if more data is needed, normally due to an error
1477 * or non-blocking IO.
1478 * When it finishes, one packet has been decoded and can be found in
1479 * ssl->s3->rrec.type - is the type of record
1480 * ssl->s3->rrec.data, - data
1481 * ssl->s3->rrec.length, - number of bytes
1483 /* used only by dtls1_read_bytes */
1484 int dtls1_get_record(SSL *s)
1486 int ssl_major, ssl_minor;
1490 unsigned char *p = NULL;
1491 unsigned short version;
1492 DTLS1_BITMAP *bitmap;
1493 unsigned int is_next_epoch;
1495 rr = RECORD_LAYER_get_rrec(&s->rlayer);
1499 * The epoch may have changed. If so, process all the pending records.
1500 * This is a non-blocking operation.
1502 if (!dtls1_process_buffered_records(s))
1505 /* if we're renegotiating, then there may be buffered records */
1506 if (dtls1_get_processed_record(s))
1509 /* get something from the wire */
1511 /* check if we have the header */
1512 if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||
1513 (RECORD_LAYER_get_packet_length(&s->rlayer) < DTLS1_RT_HEADER_LENGTH)) {
1514 rret = ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH,
1515 SSL3_BUFFER_get_len(&s->rlayer.rbuf), 0, 1, &n);
1516 /* read timeout is handled by dtls1_read_bytes */
1518 return rret; /* error or non-blocking */
1520 /* this packet contained a partial record, dump it */
1521 if (RECORD_LAYER_get_packet_length(&s->rlayer) !=
1522 DTLS1_RT_HEADER_LENGTH) {
1523 RECORD_LAYER_reset_packet_length(&s->rlayer);
1527 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);
1529 p = RECORD_LAYER_get_packet(&s->rlayer);
1531 if (s->msg_callback)
1532 s->msg_callback(0, 0, SSL3_RT_HEADER, p, DTLS1_RT_HEADER_LENGTH,
1533 s, s->msg_callback_arg);
1535 /* Pull apart the header into the DTLS1_RECORD */
1539 version = (ssl_major << 8) | ssl_minor;
1541 /* sequence number is 64 bits, with top 2 bytes = epoch */
1544 memcpy(&(RECORD_LAYER_get_read_sequence(&s->rlayer)[2]), p, 6);
1549 /* Lets check version */
1550 if (!s->first_packet) {
1551 if (version != s->version) {
1552 /* unexpected version, silently discard */
1554 RECORD_LAYER_reset_packet_length(&s->rlayer);
1559 if ((version & 0xff00) != (s->version & 0xff00)) {
1560 /* wrong version, silently discard record */
1562 RECORD_LAYER_reset_packet_length(&s->rlayer);
1566 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
1567 /* record too long, silently discard it */
1569 RECORD_LAYER_reset_packet_length(&s->rlayer);
1573 /* now s->rlayer.rstate == SSL_ST_READ_BODY */
1576 /* s->rlayer.rstate == SSL_ST_READ_BODY, get and decode the data */
1579 RECORD_LAYER_get_packet_length(&s->rlayer) - DTLS1_RT_HEADER_LENGTH) {
1580 /* now s->packet_length == DTLS1_RT_HEADER_LENGTH */
1582 rret = ssl3_read_n(s, more, more, 1, 1, &n);
1583 /* this packet contained a partial record, dump it */
1584 if (rret <= 0 || n != more) {
1586 RECORD_LAYER_reset_packet_length(&s->rlayer);
1591 * now n == rr->length, and s->packet_length ==
1592 * DTLS1_RT_HEADER_LENGTH + rr->length
1595 /* set state for later operations */
1596 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);
1598 /* match epochs. NULL means the packet is dropped on the floor */
1599 bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);
1600 if (bitmap == NULL) {
1602 RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
1603 goto again; /* get another record */
1605 #ifndef OPENSSL_NO_SCTP
1606 /* Only do replay check if no SCTP bio */
1607 if (!BIO_dgram_is_sctp(SSL_get_rbio(s))) {
1609 /* Check whether this is a repeat, or aged record. */
1611 * TODO: Does it make sense to have replay protection in epoch 0 where
1612 * we have no integrity negotiated yet?
1614 if (!dtls1_record_replay_check(s, bitmap)) {
1616 RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
1617 goto again; /* get another record */
1619 #ifndef OPENSSL_NO_SCTP
1623 /* just read a 0 length packet */
1624 if (rr->length == 0)
1628 * If this record is from the next epoch (either HM or ALERT), and a
1629 * handshake is currently in progress, buffer it since it cannot be
1630 * processed at this time.
1632 if (is_next_epoch) {
1633 if ((SSL_in_init(s) || ossl_statem_get_in_handshake(s))) {
1634 if (dtls1_buffer_record
1635 (s, &(DTLS_RECORD_LAYER_get_unprocessed_rcds(&s->rlayer)),
1640 RECORD_LAYER_reset_packet_length(&s->rlayer);
1644 if (!dtls1_process_record(s, bitmap)) {
1646 RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
1647 goto again; /* get another record */