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 int num_recs = 0;
138 unsigned int max_recs;
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 n = ssl3_read_n(s, SSL3_RT_HEADER_LENGTH,
154 SSL3_BUFFER_get_len(rbuf), 0, num_recs == 0 ? 1 : 0);
156 return (n); /* error or non-blocking */
157 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);
159 p = RECORD_LAYER_get_packet(&s->rlayer);
162 * Check whether this is a regular record or an SSLv2 style record.
163 * The latter can only be used in the first record of an initial
164 * ClientHello for old clients. Initial ClientHello means
165 * s->first_packet is set and s->server is true. The first record
166 * means we've not received any data so far (s->init_num == 0) and
167 * have had no empty records. We check s->read_hash and
168 * s->enc_read_ctx to ensure this does not apply during
171 if (s->first_packet && s->server
173 && RECORD_LAYER_get_empty_record_count(&s->rlayer) == 0
174 && s->read_hash == NULL && s->enc_read_ctx == NULL
175 && (p[0] & 0x80) && (p[2] == SSL2_MT_CLIENT_HELLO)) {
179 * |num_recs| here will actually always be 0 because
180 * |num_recs > 0| only ever occurs when we are processing
181 * multiple app data records - which we know isn't the case here
182 * because it is an SSLv2ClientHello. We keep it using
183 * |num_recs| for the sake of consistency
185 rr[num_recs].type = SSL3_RT_HANDSHAKE;
186 rr[num_recs].rec_version = SSL2_VERSION;
188 rr[num_recs].length = ((p[0] & 0x7f) << 8) | p[1];
190 if (rr[num_recs].length > SSL3_BUFFER_get_len(rbuf)
191 - SSL2_RT_HEADER_LENGTH) {
192 al = SSL_AD_RECORD_OVERFLOW;
193 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_PACKET_LENGTH_TOO_LONG);
197 if (rr[num_recs].length < MIN_SSL2_RECORD_LEN) {
198 al = SSL_AD_HANDSHAKE_FAILURE;
199 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT);
203 /* SSLv3+ style record */
205 s->msg_callback(0, 0, SSL3_RT_HEADER, p, 5, s,
206 s->msg_callback_arg);
208 /* Pull apart the header into the SSL3_RECORD */
209 rr[num_recs].type = *(p++);
212 version = (ssl_major << 8) | ssl_minor;
213 rr[num_recs].rec_version = version;
214 n2s(p, rr[num_recs].length);
216 /* Lets check version */
217 if (!s->first_packet && version != s->version) {
218 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_WRONG_VERSION_NUMBER);
219 if ((s->version & 0xFF00) == (version & 0xFF00)
220 && !s->enc_write_ctx && !s->write_hash) {
221 if (rr->type == SSL3_RT_ALERT) {
223 * The record is using an incorrect version number,
224 * but what we've got appears to be an alert. We
225 * haven't read the body yet to check whether its a
226 * fatal or not - but chances are it is. We probably
227 * shouldn't send a fatal alert back. We'll just
233 * Send back error using their minor version number :-)
235 s->version = (unsigned short)version;
237 al = SSL_AD_PROTOCOL_VERSION;
241 if ((version >> 8) != SSL3_VERSION_MAJOR) {
242 if (s->first_packet) {
243 /* Go back to start of packet, look at the five bytes
245 p = RECORD_LAYER_get_packet(&s->rlayer);
246 if (strncmp((char *)p, "GET ", 4) == 0 ||
247 strncmp((char *)p, "POST ", 5) == 0 ||
248 strncmp((char *)p, "HEAD ", 5) == 0 ||
249 strncmp((char *)p, "PUT ", 4) == 0) {
250 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_HTTP_REQUEST);
252 } else if (strncmp((char *)p, "CONNE", 5) == 0) {
253 SSLerr(SSL_F_SSL3_GET_RECORD,
254 SSL_R_HTTPS_PROXY_REQUEST);
258 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_WRONG_VERSION_NUMBER);
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 i = rr[num_recs].length + SSL2_RT_HEADER_LENGTH
280 - SSL3_RT_HEADER_LENGTH;
282 i = rr[num_recs].length;
285 /* now s->packet_length == SSL3_RT_HEADER_LENGTH */
287 n = ssl3_read_n(s, i, i, 1, 0);
289 return (n); /* 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 } 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));
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,
542 int ssl3_do_compress(SSL *ssl, SSL3_RECORD *wr)
544 #ifndef OPENSSL_NO_COMP
547 i = COMP_compress_block(ssl->compress, wr->data,
548 SSL3_RT_MAX_COMPRESSED_LENGTH,
549 wr->input, (int)wr->length);
555 wr->input = wr->data;
561 * ssl3_enc encrypts/decrypts |n_recs| records in |inrecs|
564 * 0: (in non-constant time) if the record is publically invalid (i.e. too
566 * 1: if the record's padding is valid / the encryption was successful.
567 * -1: if the record's padding is invalid or, if sending, an internal error
570 int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, unsigned int n_recs, int send)
575 int bs, i, mac_size = 0;
576 const EVP_CIPHER *enc;
580 * We shouldn't ever be called with more than one record in the SSLv3 case
585 ds = s->enc_write_ctx;
586 if (s->enc_write_ctx == NULL)
589 enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
591 ds = s->enc_read_ctx;
592 if (s->enc_read_ctx == NULL)
595 enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
598 if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
599 memmove(rec->data, rec->input, rec->length);
600 rec->input = rec->data;
603 bs = EVP_CIPHER_CTX_block_size(ds);
607 if ((bs != 1) && send) {
608 i = bs - ((int)l % bs);
610 /* we need to add 'i-1' padding bytes */
613 * the last of these zero bytes will be overwritten with the
616 memset(&rec->input[rec->length], 0, i);
618 rec->input[l - 1] = (i - 1);
622 if (l == 0 || l % bs != 0)
624 /* otherwise, rec->length >= bs */
627 if (EVP_Cipher(ds, rec->data, rec->input, l) < 1)
630 if (EVP_MD_CTX_md(s->read_hash) != NULL)
631 mac_size = EVP_MD_CTX_size(s->read_hash);
632 if ((bs != 1) && !send)
633 return ssl3_cbc_remove_padding(rec, bs, mac_size);
639 * tls1_enc encrypts/decrypts |n_recs| in |recs|.
642 * 0: (in non-constant time) if the record is publically invalid (i.e. too
644 * 1: if the record's padding is valid / the encryption was successful.
645 * -1: if the record's padding/AEAD-authenticator is invalid or, if sending,
646 * an internal error occurred.
648 int tls1_enc(SSL *s, SSL3_RECORD *recs, unsigned int n_recs, int send)
651 size_t reclen[SSL_MAX_PIPELINES];
652 unsigned char buf[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
653 int bs, i, j, k, pad = 0, ret, mac_size = 0;
654 const EVP_CIPHER *enc;
658 if (EVP_MD_CTX_md(s->write_hash)) {
659 int n = EVP_MD_CTX_size(s->write_hash);
660 OPENSSL_assert(n >= 0);
662 ds = s->enc_write_ctx;
663 if (s->enc_write_ctx == NULL)
667 enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
668 /* For TLSv1.1 and later explicit IV */
669 if (SSL_USE_EXPLICIT_IV(s)
670 && EVP_CIPHER_mode(enc) == EVP_CIPH_CBC_MODE)
671 ivlen = EVP_CIPHER_iv_length(enc);
675 for (ctr = 0; ctr < n_recs; ctr++) {
676 if (recs[ctr].data != recs[ctr].input) {
678 * we can't write into the input stream: Can this ever
681 SSLerr(SSL_F_TLS1_ENC, ERR_R_INTERNAL_ERROR);
683 } else if (RAND_bytes(recs[ctr].input, ivlen) <= 0) {
684 SSLerr(SSL_F_TLS1_ENC, ERR_R_INTERNAL_ERROR);
691 if (EVP_MD_CTX_md(s->read_hash)) {
692 int n = EVP_MD_CTX_size(s->read_hash);
693 OPENSSL_assert(n >= 0);
695 ds = s->enc_read_ctx;
696 if (s->enc_read_ctx == NULL)
699 enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
702 if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
703 for (ctr = 0; ctr < n_recs; ctr++) {
704 memmove(recs[ctr].data, recs[ctr].input, recs[ctr].length);
705 recs[ctr].input = recs[ctr].data;
709 bs = EVP_CIPHER_block_size(EVP_CIPHER_CTX_cipher(ds));
712 if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
713 & EVP_CIPH_FLAG_PIPELINE)) {
715 * We shouldn't have been called with pipeline data if the
716 * cipher doesn't support pipelining
718 SSLerr(SSL_F_TLS1_ENC, SSL_R_PIPELINE_FAILURE);
722 for (ctr = 0; ctr < n_recs; ctr++) {
723 reclen[ctr] = recs[ctr].length;
725 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
726 & EVP_CIPH_FLAG_AEAD_CIPHER) {
729 seq = send ? RECORD_LAYER_get_write_sequence(&s->rlayer)
730 : RECORD_LAYER_get_read_sequence(&s->rlayer);
732 if (SSL_IS_DTLS(s)) {
733 /* DTLS does not support pipelining */
734 unsigned char dtlsseq[9], *p = dtlsseq;
736 s2n(send ? DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer) :
737 DTLS_RECORD_LAYER_get_r_epoch(&s->rlayer), p);
738 memcpy(p, &seq[2], 6);
739 memcpy(buf[ctr], dtlsseq, 8);
741 memcpy(buf[ctr], seq, 8);
742 for (i = 7; i >= 0; i--) { /* increment */
749 buf[ctr][8] = recs[ctr].type;
750 buf[ctr][9] = (unsigned char)(s->version >> 8);
751 buf[ctr][10] = (unsigned char)(s->version);
752 buf[ctr][11] = recs[ctr].length >> 8;
753 buf[ctr][12] = recs[ctr].length & 0xff;
754 pad = EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_AEAD_TLS1_AAD,
755 EVP_AEAD_TLS1_AAD_LEN, buf[ctr]);
761 recs[ctr].length += pad;
764 } else if ((bs != 1) && send) {
765 i = bs - ((int)reclen[ctr] % bs);
767 /* Add weird padding of upto 256 bytes */
769 /* we need to add 'i' padding bytes of value j */
771 for (k = (int)reclen[ctr]; k < (int)(reclen[ctr] + i); k++)
772 recs[ctr].input[k] = j;
774 recs[ctr].length += i;
778 if (reclen[ctr] == 0 || reclen[ctr] % bs != 0)
783 unsigned char *data[SSL_MAX_PIPELINES];
785 /* Set the output buffers */
786 for (ctr = 0; ctr < n_recs; ctr++) {
787 data[ctr] = recs[ctr].data;
789 if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS,
790 n_recs, data) <= 0) {
791 SSLerr(SSL_F_TLS1_ENC, SSL_R_PIPELINE_FAILURE);
793 /* Set the input buffers */
794 for (ctr = 0; ctr < n_recs; ctr++) {
795 data[ctr] = recs[ctr].input;
797 if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_BUFS,
799 || EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_LENS,
800 n_recs, reclen) <= 0) {
801 SSLerr(SSL_F_TLS1_ENC, SSL_R_PIPELINE_FAILURE);
806 i = EVP_Cipher(ds, recs[0].data, recs[0].input, reclen[0]);
807 if ((EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
808 & EVP_CIPH_FLAG_CUSTOM_CIPHER)
811 return -1; /* AEAD can fail to verify MAC */
813 if (EVP_CIPHER_mode(enc) == EVP_CIPH_GCM_MODE) {
814 for (ctr = 0; ctr < n_recs; ctr++) {
815 recs[ctr].data += EVP_GCM_TLS_EXPLICIT_IV_LEN;
816 recs[ctr].input += EVP_GCM_TLS_EXPLICIT_IV_LEN;
817 recs[ctr].length -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
819 } else if (EVP_CIPHER_mode(enc) == EVP_CIPH_CCM_MODE) {
820 for (ctr = 0; ctr < n_recs; ctr++) {
821 recs[ctr].data += EVP_CCM_TLS_EXPLICIT_IV_LEN;
822 recs[ctr].input += EVP_CCM_TLS_EXPLICIT_IV_LEN;
823 recs[ctr].length -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
829 if (!SSL_USE_ETM(s) && EVP_MD_CTX_md(s->read_hash) != NULL)
830 mac_size = EVP_MD_CTX_size(s->read_hash);
831 if ((bs != 1) && !send) {
833 for (ctr = 0; ctr < n_recs; ctr++) {
834 tmpret = tls1_cbc_remove_padding(s, &recs[ctr], bs, mac_size);
841 for (ctr = 0; ctr < n_recs; ctr++) {
842 recs[ctr].length -= pad;
849 int n_ssl3_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int send)
851 unsigned char *mac_sec, *seq;
852 const EVP_MD_CTX *hash;
853 unsigned char *p, rec_char;
859 mac_sec = &(ssl->s3->write_mac_secret[0]);
860 seq = RECORD_LAYER_get_write_sequence(&ssl->rlayer);
861 hash = ssl->write_hash;
863 mac_sec = &(ssl->s3->read_mac_secret[0]);
864 seq = RECORD_LAYER_get_read_sequence(&ssl->rlayer);
865 hash = ssl->read_hash;
868 t = EVP_MD_CTX_size(hash);
872 npad = (48 / md_size) * md_size;
875 EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
876 ssl3_cbc_record_digest_supported(hash)) {
878 * This is a CBC-encrypted record. We must avoid leaking any
879 * timing-side channel information about how many blocks of data we
880 * are hashing because that gives an attacker a timing-oracle.
884 * npad is, at most, 48 bytes and that's with MD5:
885 * 16 + 48 + 8 (sequence bytes) + 1 + 2 = 75.
887 * With SHA-1 (the largest hash speced for SSLv3) the hash size
888 * goes up 4, but npad goes down by 8, resulting in a smaller
891 unsigned char header[75];
893 memcpy(header + j, mac_sec, md_size);
895 memcpy(header + j, ssl3_pad_1, npad);
897 memcpy(header + j, seq, 8);
899 header[j++] = rec->type;
900 header[j++] = rec->length >> 8;
901 header[j++] = rec->length & 0xff;
903 /* Final param == is SSLv3 */
904 if (ssl3_cbc_digest_record(hash,
907 rec->length + md_size, rec->orig_len,
908 mac_sec, md_size, 1) <= 0)
911 unsigned int md_size_u;
912 /* Chop the digest off the end :-) */
913 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
918 rec_char = rec->type;
921 if (EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
922 || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
923 || EVP_DigestUpdate(md_ctx, ssl3_pad_1, npad) <= 0
924 || EVP_DigestUpdate(md_ctx, seq, 8) <= 0
925 || EVP_DigestUpdate(md_ctx, &rec_char, 1) <= 0
926 || EVP_DigestUpdate(md_ctx, md, 2) <= 0
927 || EVP_DigestUpdate(md_ctx, rec->input, rec->length) <= 0
928 || EVP_DigestFinal_ex(md_ctx, md, NULL) <= 0
929 || EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
930 || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
931 || EVP_DigestUpdate(md_ctx, ssl3_pad_2, npad) <= 0
932 || EVP_DigestUpdate(md_ctx, md, md_size) <= 0
933 || EVP_DigestFinal_ex(md_ctx, md, &md_size_u) <= 0) {
934 EVP_MD_CTX_reset(md_ctx);
939 EVP_MD_CTX_free(md_ctx);
942 ssl3_record_sequence_update(seq);
946 int tls1_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int send)
952 EVP_MD_CTX *hmac = NULL, *mac_ctx;
953 unsigned char header[13];
954 int stream_mac = (send ? (ssl->mac_flags & SSL_MAC_FLAG_WRITE_MAC_STREAM)
955 : (ssl->mac_flags & SSL_MAC_FLAG_READ_MAC_STREAM));
959 seq = RECORD_LAYER_get_write_sequence(&ssl->rlayer);
960 hash = ssl->write_hash;
962 seq = RECORD_LAYER_get_read_sequence(&ssl->rlayer);
963 hash = ssl->read_hash;
966 t = EVP_MD_CTX_size(hash);
967 OPENSSL_assert(t >= 0);
970 /* I should fix this up TLS TLS TLS TLS TLS XXXXXXXX */
974 hmac = EVP_MD_CTX_new();
976 || !EVP_MD_CTX_copy(hmac, hash))
981 if (SSL_IS_DTLS(ssl)) {
982 unsigned char dtlsseq[8], *p = dtlsseq;
984 s2n(send ? DTLS_RECORD_LAYER_get_w_epoch(&ssl->rlayer) :
985 DTLS_RECORD_LAYER_get_r_epoch(&ssl->rlayer), p);
986 memcpy(p, &seq[2], 6);
988 memcpy(header, dtlsseq, 8);
990 memcpy(header, seq, 8);
992 header[8] = rec->type;
993 header[9] = (unsigned char)(ssl->version >> 8);
994 header[10] = (unsigned char)(ssl->version);
995 header[11] = (rec->length) >> 8;
996 header[12] = (rec->length) & 0xff;
998 if (!send && !SSL_USE_ETM(ssl) &&
999 EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
1000 ssl3_cbc_record_digest_supported(mac_ctx)) {
1002 * This is a CBC-encrypted record. We must avoid leaking any
1003 * timing-side channel information about how many blocks of data we
1004 * are hashing because that gives an attacker a timing-oracle.
1006 /* Final param == not SSLv3 */
1007 if (ssl3_cbc_digest_record(mac_ctx,
1010 rec->length + md_size, rec->orig_len,
1011 ssl->s3->read_mac_secret,
1012 ssl->s3->read_mac_secret_size, 0) <= 0) {
1013 EVP_MD_CTX_free(hmac);
1017 if (EVP_DigestSignUpdate(mac_ctx, header, sizeof(header)) <= 0
1018 || EVP_DigestSignUpdate(mac_ctx, rec->input, rec->length) <= 0
1019 || EVP_DigestSignFinal(mac_ctx, md, &md_size) <= 0) {
1020 EVP_MD_CTX_free(hmac);
1023 if (!send && !SSL_USE_ETM(ssl) && FIPS_mode())
1024 if (!tls_fips_digest_extra(ssl->enc_read_ctx,
1025 mac_ctx, rec->input,
1026 rec->length, rec->orig_len)) {
1027 EVP_MD_CTX_free(hmac);
1032 EVP_MD_CTX_free(hmac);
1035 fprintf(stderr, "seq=");
1038 for (z = 0; z < 8; z++)
1039 fprintf(stderr, "%02X ", seq[z]);
1040 fprintf(stderr, "\n");
1042 fprintf(stderr, "rec=");
1045 for (z = 0; z < rec->length; z++)
1046 fprintf(stderr, "%02X ", rec->data[z]);
1047 fprintf(stderr, "\n");
1051 if (!SSL_IS_DTLS(ssl)) {
1052 for (i = 7; i >= 0; i--) {
1061 for (z = 0; z < md_size; z++)
1062 fprintf(stderr, "%02X ", md[z]);
1063 fprintf(stderr, "\n");
1070 * ssl3_cbc_remove_padding removes padding from the decrypted, SSLv3, CBC
1071 * record in |rec| by updating |rec->length| in constant time.
1073 * block_size: the block size of the cipher used to encrypt the record.
1075 * 0: (in non-constant time) if the record is publicly invalid.
1076 * 1: if the padding was valid
1079 int ssl3_cbc_remove_padding(SSL3_RECORD *rec,
1080 unsigned block_size, unsigned mac_size)
1082 unsigned padding_length, good;
1083 const unsigned overhead = 1 /* padding length byte */ + mac_size;
1086 * These lengths are all public so we can test them in non-constant time.
1088 if (overhead > rec->length)
1091 padding_length = rec->data[rec->length - 1];
1092 good = constant_time_ge(rec->length, padding_length + overhead);
1093 /* SSLv3 requires that the padding is minimal. */
1094 good &= constant_time_ge(block_size, padding_length + 1);
1095 rec->length -= good & (padding_length + 1);
1096 return constant_time_select_int(good, 1, -1);
1100 * tls1_cbc_remove_padding removes the CBC padding from the decrypted, TLS, CBC
1101 * record in |rec| in constant time and returns 1 if the padding is valid and
1102 * -1 otherwise. It also removes any explicit IV from the start of the record
1103 * without leaking any timing about whether there was enough space after the
1104 * padding was removed.
1106 * block_size: the block size of the cipher used to encrypt the record.
1108 * 0: (in non-constant time) if the record is publicly invalid.
1109 * 1: if the padding was valid
1112 int tls1_cbc_remove_padding(const SSL *s,
1114 unsigned block_size, unsigned mac_size)
1116 unsigned padding_length, good, to_check, i;
1117 const unsigned overhead = 1 /* padding length byte */ + mac_size;
1118 /* Check if version requires explicit IV */
1119 if (SSL_USE_EXPLICIT_IV(s)) {
1121 * These lengths are all public so we can test them in non-constant
1124 if (overhead + block_size > rec->length)
1126 /* We can now safely skip explicit IV */
1127 rec->data += block_size;
1128 rec->input += block_size;
1129 rec->length -= block_size;
1130 rec->orig_len -= block_size;
1131 } else if (overhead > rec->length)
1134 padding_length = rec->data[rec->length - 1];
1136 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_read_ctx)) & EVP_CIPH_FLAG_AEAD_CIPHER) {
1137 /* padding is already verified */
1138 rec->length -= padding_length + 1;
1142 good = constant_time_ge(rec->length, overhead + padding_length);
1144 * The padding consists of a length byte at the end of the record and
1145 * then that many bytes of padding, all with the same value as the length
1146 * byte. Thus, with the length byte included, there are i+1 bytes of
1147 * padding. We can't check just |padding_length+1| bytes because that
1148 * leaks decrypted information. Therefore we always have to check the
1149 * maximum amount of padding possible. (Again, the length of the record
1150 * is public information so we can use it.)
1152 to_check = 256; /* maximum amount of padding, inc length byte. */
1153 if (to_check > rec->length)
1154 to_check = rec->length;
1156 for (i = 0; i < to_check; i++) {
1157 unsigned char mask = constant_time_ge_8(padding_length, i);
1158 unsigned char b = rec->data[rec->length - 1 - i];
1160 * The final |padding_length+1| bytes should all have the value
1161 * |padding_length|. Therefore the XOR should be zero.
1163 good &= ~(mask & (padding_length ^ b));
1167 * If any of the final |padding_length+1| bytes had the wrong value, one
1168 * or more of the lower eight bits of |good| will be cleared.
1170 good = constant_time_eq(0xff, good & 0xff);
1171 rec->length -= good & (padding_length + 1);
1173 return constant_time_select_int(good, 1, -1);
1177 * ssl3_cbc_copy_mac copies |md_size| bytes from the end of |rec| to |out| in
1178 * constant time (independent of the concrete value of rec->length, which may
1179 * vary within a 256-byte window).
1181 * ssl3_cbc_remove_padding or tls1_cbc_remove_padding must be called prior to
1185 * rec->orig_len >= md_size
1186 * md_size <= EVP_MAX_MD_SIZE
1188 * If CBC_MAC_ROTATE_IN_PLACE is defined then the rotation is performed with
1189 * variable accesses in a 64-byte-aligned buffer. Assuming that this fits into
1190 * a single or pair of cache-lines, then the variable memory accesses don't
1191 * actually affect the timing. CPUs with smaller cache-lines [if any] are
1192 * not multi-core and are not considered vulnerable to cache-timing attacks.
1194 #define CBC_MAC_ROTATE_IN_PLACE
1196 void ssl3_cbc_copy_mac(unsigned char *out,
1197 const SSL3_RECORD *rec, unsigned md_size)
1199 #if defined(CBC_MAC_ROTATE_IN_PLACE)
1200 unsigned char rotated_mac_buf[64 + EVP_MAX_MD_SIZE];
1201 unsigned char *rotated_mac;
1203 unsigned char rotated_mac[EVP_MAX_MD_SIZE];
1207 * mac_end is the index of |rec->data| just after the end of the MAC.
1209 unsigned mac_end = rec->length;
1210 unsigned mac_start = mac_end - md_size;
1212 * scan_start contains the number of bytes that we can ignore because the
1213 * MAC's position can only vary by 255 bytes.
1215 unsigned scan_start = 0;
1217 unsigned div_spoiler;
1218 unsigned rotate_offset;
1220 OPENSSL_assert(rec->orig_len >= md_size);
1221 OPENSSL_assert(md_size <= EVP_MAX_MD_SIZE);
1223 #if defined(CBC_MAC_ROTATE_IN_PLACE)
1224 rotated_mac = rotated_mac_buf + ((0 - (size_t)rotated_mac_buf) & 63);
1227 /* This information is public so it's safe to branch based on it. */
1228 if (rec->orig_len > md_size + 255 + 1)
1229 scan_start = rec->orig_len - (md_size + 255 + 1);
1231 * div_spoiler contains a multiple of md_size that is used to cause the
1232 * modulo operation to be constant time. Without this, the time varies
1233 * based on the amount of padding when running on Intel chips at least.
1234 * The aim of right-shifting md_size is so that the compiler doesn't
1235 * figure out that it can remove div_spoiler as that would require it to
1236 * prove that md_size is always even, which I hope is beyond it.
1238 div_spoiler = md_size >> 1;
1239 div_spoiler <<= (sizeof(div_spoiler) - 1) * 8;
1240 rotate_offset = (div_spoiler + mac_start - scan_start) % md_size;
1242 memset(rotated_mac, 0, md_size);
1243 for (i = scan_start, j = 0; i < rec->orig_len; i++) {
1244 unsigned char mac_started = constant_time_ge_8(i, mac_start);
1245 unsigned char mac_ended = constant_time_ge_8(i, mac_end);
1246 unsigned char b = rec->data[i];
1247 rotated_mac[j++] |= b & mac_started & ~mac_ended;
1248 j &= constant_time_lt(j, md_size);
1251 /* Now rotate the MAC */
1252 #if defined(CBC_MAC_ROTATE_IN_PLACE)
1254 for (i = 0; i < md_size; i++) {
1255 /* in case cache-line is 32 bytes, touch second line */
1256 ((volatile unsigned char *)rotated_mac)[rotate_offset ^ 32];
1257 out[j++] = rotated_mac[rotate_offset++];
1258 rotate_offset &= constant_time_lt(rotate_offset, md_size);
1261 memset(out, 0, md_size);
1262 rotate_offset = md_size - rotate_offset;
1263 rotate_offset &= constant_time_lt(rotate_offset, md_size);
1264 for (i = 0; i < md_size; i++) {
1265 for (j = 0; j < md_size; j++)
1266 out[j] |= rotated_mac[i] & constant_time_eq_8(j, rotate_offset);
1268 rotate_offset &= constant_time_lt(rotate_offset, md_size);
1273 int dtls1_process_record(SSL *s)
1279 unsigned int mac_size;
1280 unsigned char md[EVP_MAX_MD_SIZE];
1282 rr = RECORD_LAYER_get_rrec(&s->rlayer);
1286 * At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length,
1287 * and we have that many bytes in s->packet
1289 rr->input = &(RECORD_LAYER_get_packet(&s->rlayer)[DTLS1_RT_HEADER_LENGTH]);
1292 * ok, we can now read from 's->packet' data into 'rr' rr->input points
1293 * at rr->length bytes, which need to be copied into rr->data by either
1294 * the decryption or by the decompression When the data is 'copied' into
1295 * the rr->data buffer, rr->input will be pointed at the new buffer
1299 * We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
1300 * bytes of encrypted compressed stuff.
1303 /* check is not needed I believe */
1304 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
1305 al = SSL_AD_RECORD_OVERFLOW;
1306 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
1310 /* decrypt in place in 'rr->input' */
1311 rr->data = rr->input;
1312 rr->orig_len = rr->length;
1314 enc_err = s->method->ssl3_enc->enc(s, rr, 1, 0);
1317 * 0: (in non-constant time) if the record is publically invalid.
1318 * 1: if the padding is valid
1319 * -1: if the padding is invalid
1322 /* For DTLS we simply ignore bad packets. */
1324 RECORD_LAYER_reset_packet_length(&s->rlayer);
1328 printf("dec %d\n", rr->length);
1331 for (z = 0; z < rr->length; z++)
1332 printf("%02X%c", rr->data[z], ((z + 1) % 16) ? ' ' : '\n');
1337 /* r->length is now the compressed data plus mac */
1338 if ((sess != NULL) &&
1339 (s->enc_read_ctx != NULL) && (EVP_MD_CTX_md(s->read_hash) != NULL)) {
1340 /* s->read_hash != NULL => mac_size != -1 */
1341 unsigned char *mac = NULL;
1342 unsigned char mac_tmp[EVP_MAX_MD_SIZE];
1343 mac_size = EVP_MD_CTX_size(s->read_hash);
1344 OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
1347 * orig_len is the length of the record before any padding was
1348 * removed. This is public information, as is the MAC in use,
1349 * therefore we can safely process the record in a different amount
1350 * of time if it's too short to possibly contain a MAC.
1352 if (rr->orig_len < mac_size ||
1353 /* CBC records must have a padding length byte too. */
1354 (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
1355 rr->orig_len < mac_size + 1)) {
1356 al = SSL_AD_DECODE_ERROR;
1357 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_LENGTH_TOO_SHORT);
1361 if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {
1363 * We update the length so that the TLS header bytes can be
1364 * constructed correctly but we need to extract the MAC in
1365 * constant time from within the record, without leaking the
1366 * contents of the padding bytes.
1369 ssl3_cbc_copy_mac(mac_tmp, rr, mac_size);
1370 rr->length -= mac_size;
1373 * In this case there's no padding, so |rec->orig_len| equals
1374 * |rec->length| and we checked that there's enough bytes for
1377 rr->length -= mac_size;
1378 mac = &rr->data[rr->length];
1381 i = s->method->ssl3_enc->mac(s, rr, md, 0 /* not send */ );
1382 if (i < 0 || mac == NULL
1383 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0)
1385 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
1390 /* decryption failed, silently discard message */
1392 RECORD_LAYER_reset_packet_length(&s->rlayer);
1396 /* r->length is now just compressed */
1397 if (s->expand != NULL) {
1398 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
1399 al = SSL_AD_RECORD_OVERFLOW;
1400 SSLerr(SSL_F_DTLS1_PROCESS_RECORD,
1401 SSL_R_COMPRESSED_LENGTH_TOO_LONG);
1404 if (!ssl3_do_uncompress(s, rr)) {
1405 al = SSL_AD_DECOMPRESSION_FAILURE;
1406 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_BAD_DECOMPRESSION);
1411 if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH) {
1412 al = SSL_AD_RECORD_OVERFLOW;
1413 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_DATA_LENGTH_TOO_LONG);
1419 * So at this point the following is true
1420 * ssl->s3->rrec.type is the type of record
1421 * ssl->s3->rrec.length == number of bytes in record
1422 * ssl->s3->rrec.off == offset to first valid byte
1423 * ssl->s3->rrec.data == where to take bytes from, increment
1427 /* we have pulled in a full packet so zero things */
1428 RECORD_LAYER_reset_packet_length(&s->rlayer);
1432 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1439 * retrieve a buffered record that belongs to the current epoch, ie,
1442 #define dtls1_get_processed_record(s) \
1443 dtls1_retrieve_buffered_record((s), \
1444 &(DTLS_RECORD_LAYER_get_processed_rcds(&s->rlayer)))
1447 * Call this to get a new input record.
1448 * It will return <= 0 if more data is needed, normally due to an error
1449 * or non-blocking IO.
1450 * When it finishes, one packet has been decoded and can be found in
1451 * ssl->s3->rrec.type - is the type of record
1452 * ssl->s3->rrec.data, - data
1453 * ssl->s3->rrec.length, - number of bytes
1455 /* used only by dtls1_read_bytes */
1456 int dtls1_get_record(SSL *s)
1458 int ssl_major, ssl_minor;
1461 unsigned char *p = NULL;
1462 unsigned short version;
1463 DTLS1_BITMAP *bitmap;
1464 unsigned int is_next_epoch;
1466 rr = RECORD_LAYER_get_rrec(&s->rlayer);
1469 * The epoch may have changed. If so, process all the pending records.
1470 * This is a non-blocking operation.
1472 if (dtls1_process_buffered_records(s) < 0)
1475 /* if we're renegotiating, then there may be buffered records */
1476 if (dtls1_get_processed_record(s))
1479 /* get something from the wire */
1481 /* check if we have the header */
1482 if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||
1483 (RECORD_LAYER_get_packet_length(&s->rlayer) < DTLS1_RT_HEADER_LENGTH)) {
1484 n = ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH,
1485 SSL3_BUFFER_get_len(&s->rlayer.rbuf), 0, 1);
1486 /* read timeout is handled by dtls1_read_bytes */
1488 return (n); /* error or non-blocking */
1490 /* this packet contained a partial record, dump it */
1491 if (RECORD_LAYER_get_packet_length(&s->rlayer) != DTLS1_RT_HEADER_LENGTH) {
1492 RECORD_LAYER_reset_packet_length(&s->rlayer);
1496 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);
1498 p = RECORD_LAYER_get_packet(&s->rlayer);
1500 if (s->msg_callback)
1501 s->msg_callback(0, 0, SSL3_RT_HEADER, p, DTLS1_RT_HEADER_LENGTH,
1502 s, s->msg_callback_arg);
1504 /* Pull apart the header into the DTLS1_RECORD */
1508 version = (ssl_major << 8) | ssl_minor;
1510 /* sequence number is 64 bits, with top 2 bytes = epoch */
1513 memcpy(&(RECORD_LAYER_get_read_sequence(&s->rlayer)[2]), p, 6);
1518 /* Lets check version */
1519 if (!s->first_packet) {
1520 if (version != s->version) {
1521 /* unexpected version, silently discard */
1523 RECORD_LAYER_reset_packet_length(&s->rlayer);
1528 if ((version & 0xff00) != (s->version & 0xff00)) {
1529 /* wrong version, silently discard record */
1531 RECORD_LAYER_reset_packet_length(&s->rlayer);
1535 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
1536 /* record too long, silently discard it */
1538 RECORD_LAYER_reset_packet_length(&s->rlayer);
1542 /* now s->rlayer.rstate == SSL_ST_READ_BODY */
1545 /* s->rlayer.rstate == SSL_ST_READ_BODY, get and decode the data */
1548 RECORD_LAYER_get_packet_length(&s->rlayer) - DTLS1_RT_HEADER_LENGTH) {
1549 /* now s->packet_length == DTLS1_RT_HEADER_LENGTH */
1551 n = ssl3_read_n(s, i, i, 1, 1);
1552 /* this packet contained a partial record, dump it */
1555 RECORD_LAYER_reset_packet_length(&s->rlayer);
1560 * now n == rr->length, and s->packet_length ==
1561 * DTLS1_RT_HEADER_LENGTH + rr->length
1564 /* set state for later operations */
1565 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);
1567 /* match epochs. NULL means the packet is dropped on the floor */
1568 bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);
1569 if (bitmap == NULL) {
1571 RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
1572 goto again; /* get another record */
1574 #ifndef OPENSSL_NO_SCTP
1575 /* Only do replay check if no SCTP bio */
1576 if (!BIO_dgram_is_sctp(SSL_get_rbio(s))) {
1578 /* Check whether this is a repeat, or aged record. */
1579 if (!dtls1_record_replay_check(s, bitmap)) {
1581 RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
1582 goto again; /* get another record */
1584 #ifndef OPENSSL_NO_SCTP
1588 /* just read a 0 length packet */
1589 if (rr->length == 0)
1593 * If this record is from the next epoch (either HM or ALERT), and a
1594 * handshake is currently in progress, buffer it since it cannot be
1595 * processed at this time.
1597 if (is_next_epoch) {
1598 if ((SSL_in_init(s) || ossl_statem_get_in_handshake(s))) {
1599 if (dtls1_buffer_record
1600 (s, &(DTLS_RECORD_LAYER_get_unprocessed_rcds(&s->rlayer)),
1603 /* Mark receipt of record. */
1604 dtls1_record_bitmap_update(s, bitmap);
1607 RECORD_LAYER_reset_packet_length(&s->rlayer);
1611 if (!dtls1_process_record(s)) {
1613 RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
1614 goto again; /* get another record */
1616 dtls1_record_bitmap_update(s, bitmap); /* Mark receipt of record. */