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);
66 * Peeks ahead into "read_ahead" data to see if we have a whole record waiting
67 * for us in the buffer.
69 static int ssl3_record_app_data_waiting(SSL *s)
75 rbuf = RECORD_LAYER_get_rbuf(&s->rlayer);
77 p = SSL3_BUFFER_get_buf(rbuf);
81 left = SSL3_BUFFER_get_left(rbuf);
83 if (left < SSL3_RT_HEADER_LENGTH)
86 p += SSL3_BUFFER_get_offset(rbuf);
89 * We only check the type and record length, we will sanity check version
92 if (*p != SSL3_RT_APPLICATION_DATA)
98 if (left < SSL3_RT_HEADER_LENGTH + len)
105 * MAX_EMPTY_RECORDS defines the number of consecutive, empty records that
106 * will be processed per call to ssl3_get_record. Without this limit an
107 * attacker could send empty records at a faster rate than we can process and
108 * cause ssl3_get_record to loop forever.
110 #define MAX_EMPTY_RECORDS 32
112 #define SSL2_RT_HEADER_LENGTH 2
114 * Call this to get new input records.
115 * It will return <= 0 if more data is needed, normally due to an error
116 * or non-blocking IO.
117 * When it finishes, |numrpipes| records have been decoded. For each record 'i':
118 * rr[i].type - is the type of record
120 * rr[i].length, - number of bytes
121 * Multiple records will only be returned if the record types are all
122 * SSL3_RT_APPLICATION_DATA. The number of records returned will always be <=
125 /* used only by ssl3_read_bytes */
126 int ssl3_get_record(SSL *s)
128 int ssl_major, ssl_minor, al;
129 int enc_err, n, i, ret = -1;
134 unsigned char md[EVP_MAX_MD_SIZE];
137 unsigned empty_record_count = 0, curr_empty = 0;
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;
151 /* check if we have the header */
152 if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||
153 (RECORD_LAYER_get_packet_length(&s->rlayer)
154 < SSL3_RT_HEADER_LENGTH)) {
155 n = ssl3_read_n(s, SSL3_RT_HEADER_LENGTH,
156 SSL3_BUFFER_get_len(rbuf), 0, num_recs == 0 ? 1 : 0);
158 return (n); /* error or non-blocking */
159 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);
161 p = RECORD_LAYER_get_packet(&s->rlayer);
164 * Check whether this is a regular record or an SSLv2 style record.
165 * The latter is only used in an initial ClientHello for old
166 * clients. We check s->read_hash and s->enc_read_ctx to ensure this
167 * does not apply during renegotiation
169 if (s->first_packet && s->server && !s->read_hash
171 && (p[0] & 0x80) && (p[2] == SSL2_MT_CLIENT_HELLO)) {
175 * |num_recs| here will actually always be 0 because
176 * |num_recs > 0| only ever occurs when we are processing
177 * multiple app data records - which we know isn't the case here
178 * because it is an SSLv2ClientHello. We keep it using
179 * |num_recs| for the sake of consistency
181 rr[num_recs].type = SSL3_RT_HANDSHAKE;
182 rr[num_recs].rec_version = SSL2_VERSION;
184 rr[num_recs].length = ((p[0] & 0x7f) << 8) | p[1];
186 if (rr[num_recs].length > SSL3_BUFFER_get_len(rbuf)
187 - SSL2_RT_HEADER_LENGTH) {
188 al = SSL_AD_RECORD_OVERFLOW;
189 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_PACKET_LENGTH_TOO_LONG);
193 if (rr[num_recs].length < MIN_SSL2_RECORD_LEN) {
194 al = SSL_AD_HANDSHAKE_FAILURE;
195 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT);
199 /* SSLv3+ style record */
201 s->msg_callback(0, 0, SSL3_RT_HEADER, p, 5, s,
202 s->msg_callback_arg);
204 /* Pull apart the header into the SSL3_RECORD */
205 rr[num_recs].type = *(p++);
208 version = (ssl_major << 8) | ssl_minor;
209 rr[num_recs].rec_version = version;
210 n2s(p, rr[num_recs].length);
212 /* Lets check version */
213 if (!s->first_packet && version != s->version) {
214 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_WRONG_VERSION_NUMBER);
215 if ((s->version & 0xFF00) == (version & 0xFF00)
216 && !s->enc_write_ctx && !s->write_hash) {
217 if (rr->type == SSL3_RT_ALERT) {
219 * The record is using an incorrect version number,
220 * but what we've got appears to be an alert. We
221 * haven't read the body yet to check whether its a
222 * fatal or not - but chances are it is. We probably
223 * shouldn't send a fatal alert back. We'll just
229 * Send back error using their minor version number :-)
231 s->version = (unsigned short)version;
233 al = SSL_AD_PROTOCOL_VERSION;
237 if ((version >> 8) != SSL3_VERSION_MAJOR) {
238 if (s->first_packet) {
239 /* Go back to start of packet, look at the five bytes
241 p = RECORD_LAYER_get_packet(&s->rlayer);
242 if (strncmp((char *)p, "GET ", 4) == 0 ||
243 strncmp((char *)p, "POST ", 5) == 0 ||
244 strncmp((char *)p, "HEAD ", 5) == 0 ||
245 strncmp((char *)p, "PUT ", 4) == 0) {
246 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_HTTP_REQUEST);
248 } else if (strncmp((char *)p, "CONNE", 5) == 0) {
249 SSLerr(SSL_F_SSL3_GET_RECORD,
250 SSL_R_HTTPS_PROXY_REQUEST);
254 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_WRONG_VERSION_NUMBER);
258 if (rr[num_recs].length >
259 SSL3_BUFFER_get_len(rbuf) - SSL3_RT_HEADER_LENGTH) {
260 al = SSL_AD_RECORD_OVERFLOW;
261 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_PACKET_LENGTH_TOO_LONG);
266 /* now s->rlayer.rstate == SSL_ST_READ_BODY */
270 * s->rlayer.rstate == SSL_ST_READ_BODY, get and decode the data.
271 * Calculate how much more data we need to read for the rest of the
274 if (rr[num_recs].rec_version == SSL2_VERSION) {
275 i = rr[num_recs].length + SSL2_RT_HEADER_LENGTH
276 - SSL3_RT_HEADER_LENGTH;
278 i = rr[num_recs].length;
281 /* now s->packet_length == SSL3_RT_HEADER_LENGTH */
283 n = ssl3_read_n(s, i, i, 1, 0);
285 return (n); /* error or non-blocking io */
288 /* set state for later operations */
289 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);
292 * At this point, s->packet_length == SSL3_RT_HEADER_LENGTH + rr->length,
293 * or s->packet_length == SSL2_RT_HEADER_LENGTH + rr->length
294 * and we have that many bytes in s->packet
296 if(rr[num_recs].rec_version == SSL2_VERSION) {
298 &(RECORD_LAYER_get_packet(&s->rlayer)[SSL2_RT_HEADER_LENGTH]);
301 &(RECORD_LAYER_get_packet(&s->rlayer)[SSL3_RT_HEADER_LENGTH]);
305 * ok, we can now read from 's->packet' data into 'rr' rr->input points
306 * at rr->length bytes, which need to be copied into rr->data by either
307 * the decryption or by the decompression When the data is 'copied' into
308 * the rr->data buffer, rr->input will be pointed at the new buffer
312 * We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
313 * bytes of encrypted compressed stuff.
316 /* check is not needed I believe */
317 if (rr[num_recs].length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
318 al = SSL_AD_RECORD_OVERFLOW;
319 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
323 /* decrypt in place in 'rr->input' */
324 rr[num_recs].data = rr[num_recs].input;
325 rr[num_recs].orig_len = rr[num_recs].length;
328 /* we have pulled in a full packet so zero things */
329 RECORD_LAYER_reset_packet_length(&s->rlayer);
330 } while (num_recs < max_recs
331 && rr[num_recs-1].type == SSL3_RT_APPLICATION_DATA
332 && SSL_USE_EXPLICIT_IV(s)
333 && s->enc_read_ctx != NULL
334 && (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_read_ctx))
335 & EVP_CIPH_FLAG_PIPELINE)
336 && ssl3_record_app_data_waiting(s));
340 * If in encrypt-then-mac mode calculate mac from encrypted record. All
341 * the details below are public so no timing details can leak.
343 if (SSL_USE_ETM(s) && s->read_hash) {
345 mac_size = EVP_MD_CTX_size(s->read_hash);
346 OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
347 for (j = 0; j < num_recs; j++) {
348 if (rr[j].length < mac_size) {
349 al = SSL_AD_DECODE_ERROR;
350 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT);
353 rr[j].length -= mac_size;
354 mac = rr[j].data + rr[j].length;
355 i = s->method->ssl3_enc->mac(s, &rr[j], md, 0 /* not send */ );
356 if (i < 0 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0) {
357 al = SSL_AD_BAD_RECORD_MAC;
358 SSLerr(SSL_F_SSL3_GET_RECORD,
359 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
365 enc_err = s->method->ssl3_enc->enc(s, rr, num_recs, 0);
368 * 0: (in non-constant time) if the record is publically invalid.
369 * 1: if the padding is valid
370 * -1: if the padding is invalid
373 al = SSL_AD_DECRYPTION_FAILED;
374 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_BLOCK_CIPHER_PAD_IS_WRONG);
378 printf("dec %d\n", rr->length);
381 for (z = 0; z < rr->length; z++)
382 printf("%02X%c", rr->data[z], ((z + 1) % 16) ? ' ' : '\n');
387 /* r->length is now the compressed data plus mac */
388 if ((sess != NULL) &&
389 (s->enc_read_ctx != NULL) &&
390 (EVP_MD_CTX_md(s->read_hash) != NULL) && !SSL_USE_ETM(s)) {
391 /* s->read_hash != NULL => mac_size != -1 */
392 unsigned char *mac = NULL;
393 unsigned char mac_tmp[EVP_MAX_MD_SIZE];
395 mac_size = EVP_MD_CTX_size(s->read_hash);
396 OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
398 for (j=0; j < num_recs; j++) {
400 * orig_len is the length of the record before any padding was
401 * removed. This is public information, as is the MAC in use,
402 * therefore we can safely process the record in a different amount
403 * of time if it's too short to possibly contain a MAC.
405 if (rr[j].orig_len < mac_size ||
406 /* CBC records must have a padding length byte too. */
407 (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
408 rr[j].orig_len < mac_size + 1)) {
409 al = SSL_AD_DECODE_ERROR;
410 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT);
414 if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {
416 * We update the length so that the TLS header bytes can be
417 * constructed correctly but we need to extract the MAC in
418 * constant time from within the record, without leaking the
419 * contents of the padding bytes.
422 ssl3_cbc_copy_mac(mac_tmp, &rr[j], mac_size);
423 rr[j].length -= mac_size;
426 * In this case there's no padding, so |rec->orig_len| equals
427 * |rec->length| and we checked that there's enough bytes for
430 rr[j].length -= mac_size;
431 mac = &rr[j].data[rr[j].length];
434 i = s->method->ssl3_enc->mac(s, &rr[j], md, 0 /* not send */ );
435 if (i < 0 || mac == NULL
436 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0)
438 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
445 * A separate 'decryption_failed' alert was introduced with TLS 1.0,
446 * SSL 3.0 only has 'bad_record_mac'. But unless a decryption
447 * failure is directly visible from the ciphertext anyway, we should
448 * not reveal which kind of error occurred -- this might become
449 * visible to an attacker (e.g. via a logfile)
451 al = SSL_AD_BAD_RECORD_MAC;
452 SSLerr(SSL_F_SSL3_GET_RECORD,
453 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
457 for (j = 0; j < num_recs; j++) {
458 /* rr[j].length is now just compressed */
459 if (s->expand != NULL) {
460 if (rr[j].length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
461 al = SSL_AD_RECORD_OVERFLOW;
462 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_COMPRESSED_LENGTH_TOO_LONG);
465 if (!ssl3_do_uncompress(s, &rr[j])) {
466 al = SSL_AD_DECOMPRESSION_FAILURE;
467 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_BAD_DECOMPRESSION);
472 if (rr[j].length > SSL3_RT_MAX_PLAIN_LENGTH) {
473 al = SSL_AD_RECORD_OVERFLOW;
474 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_DATA_LENGTH_TOO_LONG);
480 * So at this point the following is true
481 * rr[j].type is the type of record
482 * rr[j].length == number of bytes in record
483 * rr[j].off == offset to first valid byte
484 * rr[j].data == where to take bytes from, increment after use :-).
487 /* just read a 0 length packet */
488 if (rr[j].length == 0) {
490 empty_record_count++;
491 if (empty_record_count > MAX_EMPTY_RECORDS) {
492 al = SSL_AD_UNEXPECTED_MESSAGE;
493 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_RECORD_TOO_SMALL);
498 if (curr_empty == num_recs) {
499 /* We have no data - do it all again */
505 RECORD_LAYER_set_numrpipes(&s->rlayer, num_recs);
509 ssl3_send_alert(s, SSL3_AL_FATAL, al);
514 int ssl3_do_uncompress(SSL *ssl, SSL3_RECORD *rr)
516 #ifndef OPENSSL_NO_COMP
519 if (rr->comp == NULL) {
520 rr->comp = (unsigned char *)
521 OPENSSL_malloc(SSL3_RT_MAX_ENCRYPTED_LENGTH);
523 if (rr->comp == NULL)
526 i = COMP_expand_block(ssl->expand, rr->comp,
527 SSL3_RT_MAX_PLAIN_LENGTH, rr->data,
538 int ssl3_do_compress(SSL *ssl, SSL3_RECORD *wr)
540 #ifndef OPENSSL_NO_COMP
543 i = COMP_compress_block(ssl->compress, wr->data,
544 SSL3_RT_MAX_COMPRESSED_LENGTH,
545 wr->input, (int)wr->length);
551 wr->input = wr->data;
557 * ssl3_enc encrypts/decrypts |n_recs| records in |inrecs|
560 * 0: (in non-constant time) if the record is publically invalid (i.e. too
562 * 1: if the record's padding is valid / the encryption was successful.
563 * -1: if the record's padding is invalid or, if sending, an internal error
566 int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, unsigned int n_recs, int send)
571 int bs, i, mac_size = 0;
572 const EVP_CIPHER *enc;
576 * We shouldn't ever be called with more than one record in the SSLv3 case
581 ds = s->enc_write_ctx;
582 if (s->enc_write_ctx == NULL)
585 enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
587 ds = s->enc_read_ctx;
588 if (s->enc_read_ctx == NULL)
591 enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
594 if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
595 memmove(rec->data, rec->input, rec->length);
596 rec->input = rec->data;
599 bs = EVP_CIPHER_CTX_block_size(ds);
603 if ((bs != 1) && send) {
604 i = bs - ((int)l % bs);
606 /* we need to add 'i-1' padding bytes */
609 * the last of these zero bytes will be overwritten with the
612 memset(&rec->input[rec->length], 0, i);
614 rec->input[l - 1] = (i - 1);
618 if (l == 0 || l % bs != 0)
620 /* otherwise, rec->length >= bs */
623 if (EVP_Cipher(ds, rec->data, rec->input, l) < 1)
626 if (EVP_MD_CTX_md(s->read_hash) != NULL)
627 mac_size = EVP_MD_CTX_size(s->read_hash);
628 if ((bs != 1) && !send)
629 return ssl3_cbc_remove_padding(rec, bs, mac_size);
635 * tls1_enc encrypts/decrypts |n_recs| in |recs|.
638 * 0: (in non-constant time) if the record is publically invalid (i.e. too
640 * 1: if the record's padding is valid / the encryption was successful.
641 * -1: if the record's padding/AEAD-authenticator is invalid or, if sending,
642 * an internal error occurred.
644 int tls1_enc(SSL *s, SSL3_RECORD *recs, unsigned int n_recs, int send)
647 size_t reclen[SSL_MAX_PIPELINES];
648 unsigned char buf[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
649 int bs, i, j, k, pad = 0, ret, mac_size = 0;
650 const EVP_CIPHER *enc;
654 if (EVP_MD_CTX_md(s->write_hash)) {
655 int n = EVP_MD_CTX_size(s->write_hash);
656 OPENSSL_assert(n >= 0);
658 ds = s->enc_write_ctx;
659 if (s->enc_write_ctx == NULL)
663 enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
664 /* For TLSv1.1 and later explicit IV */
665 if (SSL_USE_EXPLICIT_IV(s)
666 && EVP_CIPHER_mode(enc) == EVP_CIPH_CBC_MODE)
667 ivlen = EVP_CIPHER_iv_length(enc);
671 for (ctr = 0; ctr < n_recs; ctr++) {
672 if (recs[ctr].data != recs[ctr].input) {
674 * we can't write into the input stream: Can this ever
677 SSLerr(SSL_F_TLS1_ENC, ERR_R_INTERNAL_ERROR);
679 } else if (RAND_bytes(recs[ctr].input, ivlen) <= 0) {
680 SSLerr(SSL_F_TLS1_ENC, ERR_R_INTERNAL_ERROR);
687 if (EVP_MD_CTX_md(s->read_hash)) {
688 int n = EVP_MD_CTX_size(s->read_hash);
689 OPENSSL_assert(n >= 0);
691 ds = s->enc_read_ctx;
692 if (s->enc_read_ctx == NULL)
695 enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
698 if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
699 for (ctr = 0; ctr < n_recs; ctr++) {
700 memmove(recs[ctr].data, recs[ctr].input, recs[ctr].length);
701 recs[ctr].input = recs[ctr].data;
705 bs = EVP_CIPHER_block_size(EVP_CIPHER_CTX_cipher(ds));
708 if(!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
709 & EVP_CIPH_FLAG_PIPELINE)) {
711 * We shouldn't have been called with pipeline data if the
712 * cipher doesn't support pipelining
714 SSLerr(SSL_F_TLS1_ENC, SSL_R_PIPELINE_FAILURE);
718 for (ctr = 0; ctr < n_recs; ctr++) {
719 reclen[ctr] = recs[ctr].length;
721 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
722 & EVP_CIPH_FLAG_AEAD_CIPHER) {
725 seq = send ? RECORD_LAYER_get_write_sequence(&s->rlayer)
726 : RECORD_LAYER_get_read_sequence(&s->rlayer);
728 if (SSL_IS_DTLS(s)) {
729 /* DTLS does not support pipelining */
730 unsigned char dtlsseq[9], *p = dtlsseq;
732 s2n(send ? DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer) :
733 DTLS_RECORD_LAYER_get_r_epoch(&s->rlayer), p);
734 memcpy(p, &seq[2], 6);
735 memcpy(buf[ctr], dtlsseq, 8);
737 memcpy(buf[ctr], seq, 8);
738 for (i = 7; i >= 0; i--) { /* increment */
745 buf[ctr][8] = recs[ctr].type;
746 buf[ctr][9] = (unsigned char)(s->version >> 8);
747 buf[ctr][10] = (unsigned char)(s->version);
748 buf[ctr][11] = recs[ctr].length >> 8;
749 buf[ctr][12] = recs[ctr].length & 0xff;
750 pad = EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_AEAD_TLS1_AAD,
751 EVP_AEAD_TLS1_AAD_LEN, buf[ctr]);
757 recs[ctr].length += pad;
760 } else if ((bs != 1) && send) {
761 i = bs - ((int)reclen[ctr] % bs);
763 /* Add weird padding of upto 256 bytes */
765 /* we need to add 'i' padding bytes of value j */
767 for (k = (int)reclen[ctr]; k < (int)(reclen[ctr] + i); k++)
768 recs[ctr].input[k] = j;
770 recs[ctr].length += i;
774 if (reclen[ctr] == 0 || reclen[ctr] % bs != 0)
779 unsigned char *data[SSL_MAX_PIPELINES];
781 /* Set the output buffers */
782 for(ctr = 0; ctr < n_recs; ctr++) {
783 data[ctr] = recs[ctr].data;
785 if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS,
786 n_recs, data) <= 0) {
787 SSLerr(SSL_F_TLS1_ENC, SSL_R_PIPELINE_FAILURE);
789 /* Set the input buffers */
790 for(ctr = 0; ctr < n_recs; ctr++) {
791 data[ctr] = recs[ctr].input;
793 if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_BUFS,
795 || EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_LENS,
796 n_recs, reclen) <= 0) {
797 SSLerr(SSL_F_TLS1_ENC, SSL_R_PIPELINE_FAILURE);
802 i = EVP_Cipher(ds, recs[0].data, recs[0].input, reclen[0]);
803 if ((EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
804 & EVP_CIPH_FLAG_CUSTOM_CIPHER)
807 return -1; /* AEAD can fail to verify MAC */
809 if (EVP_CIPHER_mode(enc) == EVP_CIPH_GCM_MODE) {
810 for (ctr = 0; ctr < n_recs; ctr++) {
811 recs[ctr].data += EVP_GCM_TLS_EXPLICIT_IV_LEN;
812 recs[ctr].input += EVP_GCM_TLS_EXPLICIT_IV_LEN;
813 recs[ctr].length -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
815 } else if (EVP_CIPHER_mode(enc) == EVP_CIPH_CCM_MODE) {
816 for (ctr = 0; ctr < n_recs; ctr++) {
817 recs[ctr].data += EVP_CCM_TLS_EXPLICIT_IV_LEN;
818 recs[ctr].input += EVP_CCM_TLS_EXPLICIT_IV_LEN;
819 recs[ctr].length -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
825 if (!SSL_USE_ETM(s) && EVP_MD_CTX_md(s->read_hash) != NULL)
826 mac_size = EVP_MD_CTX_size(s->read_hash);
827 if ((bs != 1) && !send) {
829 for (ctr = 0; ctr < n_recs; ctr++) {
830 tmpret = tls1_cbc_remove_padding(s, &recs[ctr], bs, mac_size);
837 for (ctr = 0; ctr < n_recs; ctr++) {
838 recs[ctr].length -= pad;
845 int n_ssl3_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int send)
847 unsigned char *mac_sec, *seq;
848 const EVP_MD_CTX *hash;
849 unsigned char *p, rec_char;
855 mac_sec = &(ssl->s3->write_mac_secret[0]);
856 seq = RECORD_LAYER_get_write_sequence(&ssl->rlayer);
857 hash = ssl->write_hash;
859 mac_sec = &(ssl->s3->read_mac_secret[0]);
860 seq = RECORD_LAYER_get_read_sequence(&ssl->rlayer);
861 hash = ssl->read_hash;
864 t = EVP_MD_CTX_size(hash);
868 npad = (48 / md_size) * md_size;
871 EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
872 ssl3_cbc_record_digest_supported(hash)) {
874 * This is a CBC-encrypted record. We must avoid leaking any
875 * timing-side channel information about how many blocks of data we
876 * are hashing because that gives an attacker a timing-oracle.
880 * npad is, at most, 48 bytes and that's with MD5:
881 * 16 + 48 + 8 (sequence bytes) + 1 + 2 = 75.
883 * With SHA-1 (the largest hash speced for SSLv3) the hash size
884 * goes up 4, but npad goes down by 8, resulting in a smaller
887 unsigned char header[75];
889 memcpy(header + j, mac_sec, md_size);
891 memcpy(header + j, ssl3_pad_1, npad);
893 memcpy(header + j, seq, 8);
895 header[j++] = rec->type;
896 header[j++] = rec->length >> 8;
897 header[j++] = rec->length & 0xff;
899 /* Final param == is SSLv3 */
900 if (ssl3_cbc_digest_record(hash,
903 rec->length + md_size, rec->orig_len,
904 mac_sec, md_size, 1) <= 0)
907 unsigned int md_size_u;
908 /* Chop the digest off the end :-) */
909 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
914 rec_char = rec->type;
917 if (EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
918 || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
919 || EVP_DigestUpdate(md_ctx, ssl3_pad_1, npad) <= 0
920 || EVP_DigestUpdate(md_ctx, seq, 8) <= 0
921 || EVP_DigestUpdate(md_ctx, &rec_char, 1) <= 0
922 || EVP_DigestUpdate(md_ctx, md, 2) <= 0
923 || EVP_DigestUpdate(md_ctx, rec->input, rec->length) <= 0
924 || EVP_DigestFinal_ex(md_ctx, md, NULL) <= 0
925 || EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
926 || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
927 || EVP_DigestUpdate(md_ctx, ssl3_pad_2, npad) <= 0
928 || EVP_DigestUpdate(md_ctx, md, md_size) <= 0
929 || EVP_DigestFinal_ex(md_ctx, md, &md_size_u) <= 0) {
930 EVP_MD_CTX_reset(md_ctx);
935 EVP_MD_CTX_free(md_ctx);
938 ssl3_record_sequence_update(seq);
942 int tls1_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int send)
948 EVP_MD_CTX *hmac = NULL, *mac_ctx;
949 unsigned char header[13];
950 int stream_mac = (send ? (ssl->mac_flags & SSL_MAC_FLAG_WRITE_MAC_STREAM)
951 : (ssl->mac_flags & SSL_MAC_FLAG_READ_MAC_STREAM));
955 seq = RECORD_LAYER_get_write_sequence(&ssl->rlayer);
956 hash = ssl->write_hash;
958 seq = RECORD_LAYER_get_read_sequence(&ssl->rlayer);
959 hash = ssl->read_hash;
962 t = EVP_MD_CTX_size(hash);
963 OPENSSL_assert(t >= 0);
966 /* I should fix this up TLS TLS TLS TLS TLS XXXXXXXX */
970 hmac = EVP_MD_CTX_new();
972 || !EVP_MD_CTX_copy(hmac, hash))
977 if (SSL_IS_DTLS(ssl)) {
978 unsigned char dtlsseq[8], *p = dtlsseq;
980 s2n(send ? DTLS_RECORD_LAYER_get_w_epoch(&ssl->rlayer) :
981 DTLS_RECORD_LAYER_get_r_epoch(&ssl->rlayer), p);
982 memcpy(p, &seq[2], 6);
984 memcpy(header, dtlsseq, 8);
986 memcpy(header, seq, 8);
988 header[8] = rec->type;
989 header[9] = (unsigned char)(ssl->version >> 8);
990 header[10] = (unsigned char)(ssl->version);
991 header[11] = (rec->length) >> 8;
992 header[12] = (rec->length) & 0xff;
994 if (!send && !SSL_USE_ETM(ssl) &&
995 EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
996 ssl3_cbc_record_digest_supported(mac_ctx)) {
998 * This is a CBC-encrypted record. We must avoid leaking any
999 * timing-side channel information about how many blocks of data we
1000 * are hashing because that gives an attacker a timing-oracle.
1002 /* Final param == not SSLv3 */
1003 if (ssl3_cbc_digest_record(mac_ctx,
1006 rec->length + md_size, rec->orig_len,
1007 ssl->s3->read_mac_secret,
1008 ssl->s3->read_mac_secret_size, 0) <= 0) {
1009 EVP_MD_CTX_free(hmac);
1013 if (EVP_DigestSignUpdate(mac_ctx, header, sizeof(header)) <= 0
1014 || EVP_DigestSignUpdate(mac_ctx, rec->input, rec->length) <= 0
1015 || EVP_DigestSignFinal(mac_ctx, md, &md_size) <= 0) {
1016 EVP_MD_CTX_free(hmac);
1019 if (!send && !SSL_USE_ETM(ssl) && FIPS_mode())
1020 tls_fips_digest_extra(ssl->enc_read_ctx,
1021 mac_ctx, rec->input,
1022 rec->length, rec->orig_len);
1025 EVP_MD_CTX_free(hmac);
1028 fprintf(stderr, "seq=");
1031 for (z = 0; z < 8; z++)
1032 fprintf(stderr, "%02X ", seq[z]);
1033 fprintf(stderr, "\n");
1035 fprintf(stderr, "rec=");
1038 for (z = 0; z < rec->length; z++)
1039 fprintf(stderr, "%02X ", rec->data[z]);
1040 fprintf(stderr, "\n");
1044 if (!SSL_IS_DTLS(ssl)) {
1045 for (i = 7; i >= 0; i--) {
1054 for (z = 0; z < md_size; z++)
1055 fprintf(stderr, "%02X ", md[z]);
1056 fprintf(stderr, "\n");
1063 * ssl3_cbc_remove_padding removes padding from the decrypted, SSLv3, CBC
1064 * record in |rec| by updating |rec->length| in constant time.
1066 * block_size: the block size of the cipher used to encrypt the record.
1068 * 0: (in non-constant time) if the record is publicly invalid.
1069 * 1: if the padding was valid
1072 int ssl3_cbc_remove_padding(SSL3_RECORD *rec,
1073 unsigned block_size, unsigned mac_size)
1075 unsigned padding_length, good;
1076 const unsigned overhead = 1 /* padding length byte */ + mac_size;
1079 * These lengths are all public so we can test them in non-constant time.
1081 if (overhead > rec->length)
1084 padding_length = rec->data[rec->length - 1];
1085 good = constant_time_ge(rec->length, padding_length + overhead);
1086 /* SSLv3 requires that the padding is minimal. */
1087 good &= constant_time_ge(block_size, padding_length + 1);
1088 rec->length -= good & (padding_length + 1);
1089 return constant_time_select_int(good, 1, -1);
1093 * tls1_cbc_remove_padding removes the CBC padding from the decrypted, TLS, CBC
1094 * record in |rec| in constant time and returns 1 if the padding is valid and
1095 * -1 otherwise. It also removes any explicit IV from the start of the record
1096 * without leaking any timing about whether there was enough space after the
1097 * padding was removed.
1099 * block_size: the block size of the cipher used to encrypt the record.
1101 * 0: (in non-constant time) if the record is publicly invalid.
1102 * 1: if the padding was valid
1105 int tls1_cbc_remove_padding(const SSL *s,
1107 unsigned block_size, unsigned mac_size)
1109 unsigned padding_length, good, to_check, i;
1110 const unsigned overhead = 1 /* padding length byte */ + mac_size;
1111 /* Check if version requires explicit IV */
1112 if (SSL_USE_EXPLICIT_IV(s)) {
1114 * These lengths are all public so we can test them in non-constant
1117 if (overhead + block_size > rec->length)
1119 /* We can now safely skip explicit IV */
1120 rec->data += block_size;
1121 rec->input += block_size;
1122 rec->length -= block_size;
1123 rec->orig_len -= block_size;
1124 } else if (overhead > rec->length)
1127 padding_length = rec->data[rec->length - 1];
1129 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_read_ctx)) & EVP_CIPH_FLAG_AEAD_CIPHER) {
1130 /* padding is already verified */
1131 rec->length -= padding_length + 1;
1135 good = constant_time_ge(rec->length, overhead + padding_length);
1137 * The padding consists of a length byte at the end of the record and
1138 * then that many bytes of padding, all with the same value as the length
1139 * byte. Thus, with the length byte included, there are i+1 bytes of
1140 * padding. We can't check just |padding_length+1| bytes because that
1141 * leaks decrypted information. Therefore we always have to check the
1142 * maximum amount of padding possible. (Again, the length of the record
1143 * is public information so we can use it.)
1145 to_check = 255; /* maximum amount of padding. */
1146 if (to_check > rec->length - 1)
1147 to_check = rec->length - 1;
1149 for (i = 0; i < to_check; i++) {
1150 unsigned char mask = constant_time_ge_8(padding_length, i);
1151 unsigned char b = rec->data[rec->length - 1 - i];
1153 * The final |padding_length+1| bytes should all have the value
1154 * |padding_length|. Therefore the XOR should be zero.
1156 good &= ~(mask & (padding_length ^ b));
1160 * If any of the final |padding_length+1| bytes had the wrong value, one
1161 * or more of the lower eight bits of |good| will be cleared.
1163 good = constant_time_eq(0xff, good & 0xff);
1164 rec->length -= good & (padding_length + 1);
1166 return constant_time_select_int(good, 1, -1);
1170 * ssl3_cbc_copy_mac copies |md_size| bytes from the end of |rec| to |out| in
1171 * constant time (independent of the concrete value of rec->length, which may
1172 * vary within a 256-byte window).
1174 * ssl3_cbc_remove_padding or tls1_cbc_remove_padding must be called prior to
1178 * rec->orig_len >= md_size
1179 * md_size <= EVP_MAX_MD_SIZE
1181 * If CBC_MAC_ROTATE_IN_PLACE is defined then the rotation is performed with
1182 * variable accesses in a 64-byte-aligned buffer. Assuming that this fits into
1183 * a single or pair of cache-lines, then the variable memory accesses don't
1184 * actually affect the timing. CPUs with smaller cache-lines [if any] are
1185 * not multi-core and are not considered vulnerable to cache-timing attacks.
1187 #define CBC_MAC_ROTATE_IN_PLACE
1189 void ssl3_cbc_copy_mac(unsigned char *out,
1190 const SSL3_RECORD *rec, unsigned md_size)
1192 #if defined(CBC_MAC_ROTATE_IN_PLACE)
1193 unsigned char rotated_mac_buf[64 + EVP_MAX_MD_SIZE];
1194 unsigned char *rotated_mac;
1196 unsigned char rotated_mac[EVP_MAX_MD_SIZE];
1200 * mac_end is the index of |rec->data| just after the end of the MAC.
1202 unsigned mac_end = rec->length;
1203 unsigned mac_start = mac_end - md_size;
1205 * scan_start contains the number of bytes that we can ignore because the
1206 * MAC's position can only vary by 255 bytes.
1208 unsigned scan_start = 0;
1210 unsigned div_spoiler;
1211 unsigned rotate_offset;
1213 OPENSSL_assert(rec->orig_len >= md_size);
1214 OPENSSL_assert(md_size <= EVP_MAX_MD_SIZE);
1216 #if defined(CBC_MAC_ROTATE_IN_PLACE)
1217 rotated_mac = rotated_mac_buf + ((0 - (size_t)rotated_mac_buf) & 63);
1220 /* This information is public so it's safe to branch based on it. */
1221 if (rec->orig_len > md_size + 255 + 1)
1222 scan_start = rec->orig_len - (md_size + 255 + 1);
1224 * div_spoiler contains a multiple of md_size that is used to cause the
1225 * modulo operation to be constant time. Without this, the time varies
1226 * based on the amount of padding when running on Intel chips at least.
1227 * The aim of right-shifting md_size is so that the compiler doesn't
1228 * figure out that it can remove div_spoiler as that would require it to
1229 * prove that md_size is always even, which I hope is beyond it.
1231 div_spoiler = md_size >> 1;
1232 div_spoiler <<= (sizeof(div_spoiler) - 1) * 8;
1233 rotate_offset = (div_spoiler + mac_start - scan_start) % md_size;
1235 memset(rotated_mac, 0, md_size);
1236 for (i = scan_start, j = 0; i < rec->orig_len; i++) {
1237 unsigned char mac_started = constant_time_ge_8(i, mac_start);
1238 unsigned char mac_ended = constant_time_ge_8(i, mac_end);
1239 unsigned char b = rec->data[i];
1240 rotated_mac[j++] |= b & mac_started & ~mac_ended;
1241 j &= constant_time_lt(j, md_size);
1244 /* Now rotate the MAC */
1245 #if defined(CBC_MAC_ROTATE_IN_PLACE)
1247 for (i = 0; i < md_size; i++) {
1248 /* in case cache-line is 32 bytes, touch second line */
1249 ((volatile unsigned char *)rotated_mac)[rotate_offset ^ 32];
1250 out[j++] = rotated_mac[rotate_offset++];
1251 rotate_offset &= constant_time_lt(rotate_offset, md_size);
1254 memset(out, 0, md_size);
1255 rotate_offset = md_size - rotate_offset;
1256 rotate_offset &= constant_time_lt(rotate_offset, md_size);
1257 for (i = 0; i < md_size; i++) {
1258 for (j = 0; j < md_size; j++)
1259 out[j] |= rotated_mac[i] & constant_time_eq_8(j, rotate_offset);
1261 rotate_offset &= constant_time_lt(rotate_offset, md_size);
1266 int dtls1_process_record(SSL *s)
1272 unsigned int mac_size;
1273 unsigned char md[EVP_MAX_MD_SIZE];
1275 rr = RECORD_LAYER_get_rrec(&s->rlayer);
1279 * At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length,
1280 * and we have that many bytes in s->packet
1282 rr->input = &(RECORD_LAYER_get_packet(&s->rlayer)[DTLS1_RT_HEADER_LENGTH]);
1285 * ok, we can now read from 's->packet' data into 'rr' rr->input points
1286 * at rr->length bytes, which need to be copied into rr->data by either
1287 * the decryption or by the decompression When the data is 'copied' into
1288 * the rr->data buffer, rr->input will be pointed at the new buffer
1292 * We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
1293 * bytes of encrypted compressed stuff.
1296 /* check is not needed I believe */
1297 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
1298 al = SSL_AD_RECORD_OVERFLOW;
1299 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
1303 /* decrypt in place in 'rr->input' */
1304 rr->data = rr->input;
1305 rr->orig_len = rr->length;
1307 enc_err = s->method->ssl3_enc->enc(s, rr, 1, 0);
1310 * 0: (in non-constant time) if the record is publically invalid.
1311 * 1: if the padding is valid
1312 * -1: if the padding is invalid
1315 /* For DTLS we simply ignore bad packets. */
1317 RECORD_LAYER_reset_packet_length(&s->rlayer);
1321 printf("dec %d\n", rr->length);
1324 for (z = 0; z < rr->length; z++)
1325 printf("%02X%c", rr->data[z], ((z + 1) % 16) ? ' ' : '\n');
1330 /* r->length is now the compressed data plus mac */
1331 if ((sess != NULL) &&
1332 (s->enc_read_ctx != NULL) && (EVP_MD_CTX_md(s->read_hash) != NULL)) {
1333 /* s->read_hash != NULL => mac_size != -1 */
1334 unsigned char *mac = NULL;
1335 unsigned char mac_tmp[EVP_MAX_MD_SIZE];
1336 mac_size = EVP_MD_CTX_size(s->read_hash);
1337 OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
1340 * orig_len is the length of the record before any padding was
1341 * removed. This is public information, as is the MAC in use,
1342 * therefore we can safely process the record in a different amount
1343 * of time if it's too short to possibly contain a MAC.
1345 if (rr->orig_len < mac_size ||
1346 /* CBC records must have a padding length byte too. */
1347 (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
1348 rr->orig_len < mac_size + 1)) {
1349 al = SSL_AD_DECODE_ERROR;
1350 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_LENGTH_TOO_SHORT);
1354 if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {
1356 * We update the length so that the TLS header bytes can be
1357 * constructed correctly but we need to extract the MAC in
1358 * constant time from within the record, without leaking the
1359 * contents of the padding bytes.
1362 ssl3_cbc_copy_mac(mac_tmp, rr, mac_size);
1363 rr->length -= mac_size;
1366 * In this case there's no padding, so |rec->orig_len| equals
1367 * |rec->length| and we checked that there's enough bytes for
1370 rr->length -= mac_size;
1371 mac = &rr->data[rr->length];
1374 i = s->method->ssl3_enc->mac(s, rr, md, 0 /* not send */ );
1375 if (i < 0 || mac == NULL
1376 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0)
1378 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
1383 /* decryption failed, silently discard message */
1385 RECORD_LAYER_reset_packet_length(&s->rlayer);
1389 /* r->length is now just compressed */
1390 if (s->expand != NULL) {
1391 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
1392 al = SSL_AD_RECORD_OVERFLOW;
1393 SSLerr(SSL_F_DTLS1_PROCESS_RECORD,
1394 SSL_R_COMPRESSED_LENGTH_TOO_LONG);
1397 if (!ssl3_do_uncompress(s, rr)) {
1398 al = SSL_AD_DECOMPRESSION_FAILURE;
1399 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_BAD_DECOMPRESSION);
1404 if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH) {
1405 al = SSL_AD_RECORD_OVERFLOW;
1406 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_DATA_LENGTH_TOO_LONG);
1412 * So at this point the following is true
1413 * ssl->s3->rrec.type is the type of record
1414 * ssl->s3->rrec.length == number of bytes in record
1415 * ssl->s3->rrec.off == offset to first valid byte
1416 * ssl->s3->rrec.data == where to take bytes from, increment
1420 /* we have pulled in a full packet so zero things */
1421 RECORD_LAYER_reset_packet_length(&s->rlayer);
1425 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1432 * retrieve a buffered record that belongs to the current epoch, ie,
1435 #define dtls1_get_processed_record(s) \
1436 dtls1_retrieve_buffered_record((s), \
1437 &(DTLS_RECORD_LAYER_get_processed_rcds(&s->rlayer)))
1440 * Call this to get a new input record.
1441 * It will return <= 0 if more data is needed, normally due to an error
1442 * or non-blocking IO.
1443 * When it finishes, one packet has been decoded and can be found in
1444 * ssl->s3->rrec.type - is the type of record
1445 * ssl->s3->rrec.data, - data
1446 * ssl->s3->rrec.length, - number of bytes
1448 /* used only by dtls1_read_bytes */
1449 int dtls1_get_record(SSL *s)
1451 int ssl_major, ssl_minor;
1454 unsigned char *p = NULL;
1455 unsigned short version;
1456 DTLS1_BITMAP *bitmap;
1457 unsigned int is_next_epoch;
1459 rr = RECORD_LAYER_get_rrec(&s->rlayer);
1462 * The epoch may have changed. If so, process all the pending records.
1463 * This is a non-blocking operation.
1465 if (dtls1_process_buffered_records(s) < 0)
1468 /* if we're renegotiating, then there may be buffered records */
1469 if (dtls1_get_processed_record(s))
1472 /* get something from the wire */
1474 /* check if we have the header */
1475 if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||
1476 (RECORD_LAYER_get_packet_length(&s->rlayer) < DTLS1_RT_HEADER_LENGTH)) {
1477 n = ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH,
1478 SSL3_BUFFER_get_len(&s->rlayer.rbuf), 0, 1);
1479 /* read timeout is handled by dtls1_read_bytes */
1481 return (n); /* error or non-blocking */
1483 /* this packet contained a partial record, dump it */
1484 if (RECORD_LAYER_get_packet_length(&s->rlayer) != DTLS1_RT_HEADER_LENGTH) {
1485 RECORD_LAYER_reset_packet_length(&s->rlayer);
1489 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);
1491 p = RECORD_LAYER_get_packet(&s->rlayer);
1493 if (s->msg_callback)
1494 s->msg_callback(0, 0, SSL3_RT_HEADER, p, DTLS1_RT_HEADER_LENGTH,
1495 s, s->msg_callback_arg);
1497 /* Pull apart the header into the DTLS1_RECORD */
1501 version = (ssl_major << 8) | ssl_minor;
1503 /* sequence number is 64 bits, with top 2 bytes = epoch */
1506 memcpy(&(RECORD_LAYER_get_read_sequence(&s->rlayer)[2]), p, 6);
1511 /* Lets check version */
1512 if (!s->first_packet) {
1513 if (version != s->version) {
1514 /* unexpected version, silently discard */
1516 RECORD_LAYER_reset_packet_length(&s->rlayer);
1521 if ((version & 0xff00) != (s->version & 0xff00)) {
1522 /* wrong version, silently discard record */
1524 RECORD_LAYER_reset_packet_length(&s->rlayer);
1528 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
1529 /* record too long, silently discard it */
1531 RECORD_LAYER_reset_packet_length(&s->rlayer);
1535 /* now s->rlayer.rstate == SSL_ST_READ_BODY */
1538 /* s->rlayer.rstate == SSL_ST_READ_BODY, get and decode the data */
1541 RECORD_LAYER_get_packet_length(&s->rlayer) - DTLS1_RT_HEADER_LENGTH) {
1542 /* now s->packet_length == DTLS1_RT_HEADER_LENGTH */
1544 n = ssl3_read_n(s, i, i, 1, 1);
1545 /* this packet contained a partial record, dump it */
1548 RECORD_LAYER_reset_packet_length(&s->rlayer);
1553 * now n == rr->length, and s->packet_length ==
1554 * DTLS1_RT_HEADER_LENGTH + rr->length
1557 /* set state for later operations */
1558 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);
1560 /* match epochs. NULL means the packet is dropped on the floor */
1561 bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);
1562 if (bitmap == NULL) {
1564 RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
1565 goto again; /* get another record */
1567 #ifndef OPENSSL_NO_SCTP
1568 /* Only do replay check if no SCTP bio */
1569 if (!BIO_dgram_is_sctp(SSL_get_rbio(s))) {
1571 /* Check whether this is a repeat, or aged record. */
1572 if (!dtls1_record_replay_check(s, bitmap)) {
1574 RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
1575 goto again; /* get another record */
1577 #ifndef OPENSSL_NO_SCTP
1581 /* just read a 0 length packet */
1582 if (rr->length == 0)
1586 * If this record is from the next epoch (either HM or ALERT), and a
1587 * handshake is currently in progress, buffer it since it cannot be
1588 * processed at this time.
1590 if (is_next_epoch) {
1591 if ((SSL_in_init(s) || ossl_statem_get_in_handshake(s))) {
1592 if (dtls1_buffer_record
1593 (s, &(DTLS_RECORD_LAYER_get_unprocessed_rcds(&s->rlayer)),
1596 /* Mark receipt of record. */
1597 dtls1_record_bitmap_update(s, bitmap);
1600 RECORD_LAYER_reset_packet_length(&s->rlayer);
1604 if (!dtls1_process_record(s)) {
1606 RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
1607 goto again; /* get another record */
1609 dtls1_record_bitmap_update(s, bitmap); /* Mark receipt of record. */