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
11 #include "../ssl_locl.h"
12 #include "internal/constant_time_locl.h"
13 #include <openssl/rand.h>
14 #include "record_locl.h"
16 static const unsigned char ssl3_pad_1[48] = {
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,
22 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36
25 static const unsigned char ssl3_pad_2[48] = {
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,
31 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c
35 * Clear the contents of an SSL3_RECORD but retain any memory allocated
37 void SSL3_RECORD_clear(SSL3_RECORD *r, size_t num_recs)
42 for (i = 0; i < num_recs; i++) {
45 memset(&r[i], 0, sizeof(*r));
50 void SSL3_RECORD_release(SSL3_RECORD *r, size_t num_recs)
54 for (i = 0; i < num_recs; i++) {
55 OPENSSL_free(r[i].comp);
60 void SSL3_RECORD_set_seq_num(SSL3_RECORD *r, const unsigned char *seq_num)
62 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)
129 int enc_err, rret, ret = -1;
132 SSL3_RECORD *rr, *thisrr;
136 unsigned char md[EVP_MAX_MD_SIZE];
137 unsigned int version;
140 size_t num_recs = 0, max_recs, j;
141 PACKET pkt, sslv2pkt;
143 rr = RECORD_LAYER_get_rrec(&s->rlayer);
144 rbuf = RECORD_LAYER_get_rbuf(&s->rlayer);
145 max_recs = s->max_pipelines;
151 thisrr = &rr[num_recs];
153 /* check if we have the header */
154 if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||
155 (RECORD_LAYER_get_packet_length(&s->rlayer)
156 < SSL3_RT_HEADER_LENGTH)) {
160 rret = ssl3_read_n(s, SSL3_RT_HEADER_LENGTH,
161 SSL3_BUFFER_get_len(rbuf), 0,
162 num_recs == 0 ? 1 : 0, &n);
164 return rret; /* error or non-blocking */
165 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);
167 p = RECORD_LAYER_get_packet(&s->rlayer);
168 if (!PACKET_buf_init(&pkt, RECORD_LAYER_get_packet(&s->rlayer),
169 RECORD_LAYER_get_packet_length(&s->rlayer))) {
170 al = SSL_AD_INTERNAL_ERROR;
171 SSLerr(SSL_F_SSL3_GET_RECORD, ERR_R_INTERNAL_ERROR);
175 if (!PACKET_get_net_2_len(&sslv2pkt, &sslv2len)
176 || !PACKET_get_1(&sslv2pkt, &type)) {
177 al = SSL_AD_INTERNAL_ERROR;
178 SSLerr(SSL_F_SSL3_GET_RECORD, ERR_R_INTERNAL_ERROR);
182 * The first record received by the server may be a V2ClientHello.
184 if (s->server && RECORD_LAYER_is_first_record(&s->rlayer)
185 && (sslv2len & 0x8000) != 0
186 && (type == SSL2_MT_CLIENT_HELLO)) {
190 * |num_recs| here will actually always be 0 because
191 * |num_recs > 0| only ever occurs when we are processing
192 * multiple app data records - which we know isn't the case here
193 * because it is an SSLv2ClientHello. We keep it using
194 * |num_recs| for the sake of consistency
196 thisrr->type = SSL3_RT_HANDSHAKE;
197 thisrr->rec_version = SSL2_VERSION;
199 thisrr->length = sslv2len & 0x7fff;
201 if (thisrr->length > SSL3_BUFFER_get_len(rbuf)
202 - SSL2_RT_HEADER_LENGTH) {
203 al = SSL_AD_RECORD_OVERFLOW;
204 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_PACKET_LENGTH_TOO_LONG);
208 if (thisrr->length < MIN_SSL2_RECORD_LEN) {
209 al = SSL_AD_HANDSHAKE_FAILURE;
210 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT);
214 /* SSLv3+ style record */
216 * TODO(TLS1.3): This callback only provides the "outer" record
217 * type to the callback. Somehow we need to pass the "inner"
221 s->msg_callback(0, 0, SSL3_RT_HEADER, p, 5, s,
222 s->msg_callback_arg);
224 /* Pull apart the header into the SSL3_RECORD */
225 if (!PACKET_get_1(&pkt, &type)
226 || !PACKET_get_net_2(&pkt, &version)
227 || !PACKET_get_net_2_len(&pkt, &thisrr->length)) {
228 al = SSL_AD_INTERNAL_ERROR;
229 SSLerr(SSL_F_SSL3_GET_RECORD, ERR_R_INTERNAL_ERROR);
233 thisrr->rec_version = version;
235 /* Lets check version. In TLSv1.3 we ignore this field */
236 if (!s->first_packet && !SSL_IS_TLS13(s)
237 && version != (unsigned int)s->version) {
238 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_WRONG_VERSION_NUMBER);
239 if ((s->version & 0xFF00) == (version & 0xFF00)
240 && !s->enc_write_ctx && !s->write_hash) {
241 if (thisrr->type == SSL3_RT_ALERT) {
243 * The record is using an incorrect version number,
244 * but what we've got appears to be an alert. We
245 * haven't read the body yet to check whether its a
246 * fatal or not - but chances are it is. We probably
247 * shouldn't send a fatal alert back. We'll just
253 * Send back error using their minor version number :-)
255 s->version = (unsigned short)version;
257 al = SSL_AD_PROTOCOL_VERSION;
261 if ((version >> 8) != SSL3_VERSION_MAJOR) {
262 if (RECORD_LAYER_is_first_record(&s->rlayer)) {
263 /* Go back to start of packet, look at the five bytes
265 p = RECORD_LAYER_get_packet(&s->rlayer);
266 if (strncmp((char *)p, "GET ", 4) == 0 ||
267 strncmp((char *)p, "POST ", 5) == 0 ||
268 strncmp((char *)p, "HEAD ", 5) == 0 ||
269 strncmp((char *)p, "PUT ", 4) == 0) {
270 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_HTTP_REQUEST);
272 } else if (strncmp((char *)p, "CONNE", 5) == 0) {
273 SSLerr(SSL_F_SSL3_GET_RECORD,
274 SSL_R_HTTPS_PROXY_REQUEST);
278 /* Doesn't look like TLS - don't send an alert */
279 SSLerr(SSL_F_SSL3_GET_RECORD,
280 SSL_R_WRONG_VERSION_NUMBER);
283 SSLerr(SSL_F_SSL3_GET_RECORD,
284 SSL_R_WRONG_VERSION_NUMBER);
285 al = SSL_AD_PROTOCOL_VERSION;
290 if (SSL_IS_TLS13(s) && s->enc_read_ctx != NULL
291 && thisrr->type != SSL3_RT_APPLICATION_DATA) {
292 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_BAD_RECORD_TYPE);
293 al = SSL_AD_UNEXPECTED_MESSAGE;
298 SSL3_BUFFER_get_len(rbuf) - SSL3_RT_HEADER_LENGTH) {
299 al = SSL_AD_RECORD_OVERFLOW;
300 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_PACKET_LENGTH_TOO_LONG);
305 /* now s->rlayer.rstate == SSL_ST_READ_BODY */
309 * s->rlayer.rstate == SSL_ST_READ_BODY, get and decode the data.
310 * Calculate how much more data we need to read for the rest of the
313 if (thisrr->rec_version == SSL2_VERSION) {
314 more = thisrr->length + SSL2_RT_HEADER_LENGTH
315 - SSL3_RT_HEADER_LENGTH;
317 more = thisrr->length;
320 /* now s->packet_length == SSL3_RT_HEADER_LENGTH */
322 rret = ssl3_read_n(s, more, more, 1, 0, &n);
324 return rret; /* error or non-blocking io */
327 /* set state for later operations */
328 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);
331 * At this point, s->packet_length == SSL3_RT_HEADER_LENGTH
332 * + thisrr->length, or s->packet_length == SSL2_RT_HEADER_LENGTH
333 * + thisrr->length and we have that many bytes in s->packet
335 if (thisrr->rec_version == SSL2_VERSION) {
337 &(RECORD_LAYER_get_packet(&s->rlayer)[SSL2_RT_HEADER_LENGTH]);
340 &(RECORD_LAYER_get_packet(&s->rlayer)[SSL3_RT_HEADER_LENGTH]);
344 * ok, we can now read from 's->packet' data into 'thisrr' thisrr->input
345 * points at thisrr->length bytes, which need to be copied into
346 * thisrr->data by either the decryption or by the decompression When
347 * the data is 'copied' into the thisrr->data buffer, thisrr->input will
348 * be pointed at the new buffer
352 * We now have - encrypted [ MAC [ compressed [ plain ] ] ]
353 * thisrr->length bytes of encrypted compressed stuff.
356 /* check is not needed I believe */
357 if (thisrr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
358 al = SSL_AD_RECORD_OVERFLOW;
359 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
363 /* decrypt in place in 'thisrr->input' */
364 thisrr->data = thisrr->input;
365 thisrr->orig_len = thisrr->length;
367 /* Mark this record as not read by upper layers yet */
372 /* we have pulled in a full packet so zero things */
373 RECORD_LAYER_reset_packet_length(&s->rlayer);
374 RECORD_LAYER_clear_first_record(&s->rlayer);
375 } while (num_recs < max_recs
376 && thisrr->type == SSL3_RT_APPLICATION_DATA
377 && SSL_USE_EXPLICIT_IV(s)
378 && s->enc_read_ctx != NULL
379 && (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_read_ctx))
380 & EVP_CIPH_FLAG_PIPELINE)
381 && ssl3_record_app_data_waiting(s));
384 * If in encrypt-then-mac mode calculate mac from encrypted record. All
385 * the details below are public so no timing details can leak.
387 if (SSL_READ_ETM(s) && s->read_hash) {
389 /* TODO(size_t): convert this to do size_t properly */
390 imac_size = EVP_MD_CTX_size(s->read_hash);
391 assert(imac_size >= 0 && imac_size <= EVP_MAX_MD_SIZE);
392 if (imac_size < 0 || imac_size > EVP_MAX_MD_SIZE) {
393 al = SSL_AD_INTERNAL_ERROR;
394 SSLerr(SSL_F_SSL3_GET_RECORD, ERR_LIB_EVP);
397 mac_size = (size_t)imac_size;
398 for (j = 0; j < num_recs; j++) {
401 if (thisrr->length < mac_size) {
402 al = SSL_AD_DECODE_ERROR;
403 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT);
406 thisrr->length -= mac_size;
407 mac = thisrr->data + thisrr->length;
408 i = s->method->ssl3_enc->mac(s, thisrr, md, 0 /* not send */ );
409 if (i == 0 || CRYPTO_memcmp(md, mac, mac_size) != 0) {
410 al = SSL_AD_BAD_RECORD_MAC;
411 SSLerr(SSL_F_SSL3_GET_RECORD,
412 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
418 enc_err = s->method->ssl3_enc->enc(s, rr, num_recs, 0);
422 * 0: (in non-constant time) if the record is publically invalid.
423 * 1: if the padding is valid
424 * -1: if the padding is invalid
427 al = SSL_AD_DECRYPTION_FAILED;
428 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_BLOCK_CIPHER_PAD_IS_WRONG);
432 printf("dec %"OSSLzu"\n", rr[0].length);
435 for (z = 0; z < rr[0].length; z++)
436 printf("%02X%c", rr[0].data[z], ((z + 1) % 16) ? ' ' : '\n');
441 /* r->length is now the compressed data plus mac */
442 if ((sess != NULL) &&
443 (s->enc_read_ctx != NULL) &&
444 (!SSL_READ_ETM(s) && EVP_MD_CTX_md(s->read_hash) != NULL)) {
445 /* s->read_hash != NULL => mac_size != -1 */
446 unsigned char *mac = NULL;
447 unsigned char mac_tmp[EVP_MAX_MD_SIZE];
449 mac_size = EVP_MD_CTX_size(s->read_hash);
450 OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
452 for (j = 0; j < num_recs; j++) {
455 * orig_len is the length of the record before any padding was
456 * removed. This is public information, as is the MAC in use,
457 * therefore we can safely process the record in a different amount
458 * of time if it's too short to possibly contain a MAC.
460 if (thisrr->orig_len < mac_size ||
461 /* CBC records must have a padding length byte too. */
462 (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
463 thisrr->orig_len < mac_size + 1)) {
464 al = SSL_AD_DECODE_ERROR;
465 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_LENGTH_TOO_SHORT);
469 if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {
471 * We update the length so that the TLS header bytes can be
472 * constructed correctly but we need to extract the MAC in
473 * constant time from within the record, without leaking the
474 * contents of the padding bytes.
477 ssl3_cbc_copy_mac(mac_tmp, thisrr, mac_size);
478 thisrr->length -= mac_size;
481 * In this case there's no padding, so |rec->orig_len| equals
482 * |rec->length| and we checked that there's enough bytes for
485 thisrr->length -= mac_size;
486 mac = &thisrr->data[thisrr->length];
489 i = s->method->ssl3_enc->mac(s, thisrr, md, 0 /* not send */ );
490 if (i == 0 || mac == NULL
491 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0)
493 if (thisrr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
500 * A separate 'decryption_failed' alert was introduced with TLS 1.0,
501 * SSL 3.0 only has 'bad_record_mac'. But unless a decryption
502 * failure is directly visible from the ciphertext anyway, we should
503 * not reveal which kind of error occurred -- this might become
504 * visible to an attacker (e.g. via a logfile)
506 al = SSL_AD_BAD_RECORD_MAC;
507 SSLerr(SSL_F_SSL3_GET_RECORD,
508 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
512 for (j = 0; j < num_recs; j++) {
515 /* thisrr->length is now just compressed */
516 if (s->expand != NULL) {
517 if (thisrr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
518 al = SSL_AD_RECORD_OVERFLOW;
519 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_COMPRESSED_LENGTH_TOO_LONG);
522 if (!ssl3_do_uncompress(s, thisrr)) {
523 al = SSL_AD_DECOMPRESSION_FAILURE;
524 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_BAD_DECOMPRESSION);
529 if (SSL_IS_TLS13(s) && s->enc_read_ctx != NULL) {
532 if (thisrr->length == 0) {
533 al = SSL_AD_UNEXPECTED_MESSAGE;
534 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_BAD_RECORD_TYPE);
538 /* Strip trailing padding */
539 for (end = thisrr->length - 1; end > 0 && thisrr->data[end] == 0;
543 thisrr->length = end;
544 thisrr->type = thisrr->data[end];
545 if (thisrr->type != SSL3_RT_APPLICATION_DATA
546 && thisrr->type != SSL3_RT_ALERT
547 && thisrr->type != SSL3_RT_HANDSHAKE) {
548 al = SSL_AD_UNEXPECTED_MESSAGE;
549 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_BAD_RECORD_TYPE);
554 if (thisrr->length > SSL3_RT_MAX_PLAIN_LENGTH) {
555 al = SSL_AD_RECORD_OVERFLOW;
556 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_DATA_LENGTH_TOO_LONG);
562 * So at this point the following is true
563 * thisrr->type is the type of record
564 * thisrr->length == number of bytes in record
565 * thisrr->off == offset to first valid byte
566 * thisrr->data == where to take bytes from, increment after use :-).
569 /* just read a 0 length packet */
570 if (thisrr->length == 0) {
571 RECORD_LAYER_inc_empty_record_count(&s->rlayer);
572 if (RECORD_LAYER_get_empty_record_count(&s->rlayer)
573 > MAX_EMPTY_RECORDS) {
574 al = SSL_AD_UNEXPECTED_MESSAGE;
575 SSLerr(SSL_F_SSL3_GET_RECORD, SSL_R_RECORD_TOO_SMALL);
579 RECORD_LAYER_reset_empty_record_count(&s->rlayer);
583 RECORD_LAYER_set_numrpipes(&s->rlayer, num_recs);
587 ssl3_send_alert(s, SSL3_AL_FATAL, al);
592 int ssl3_do_uncompress(SSL *ssl, SSL3_RECORD *rr)
594 #ifndef OPENSSL_NO_COMP
597 if (rr->comp == NULL) {
598 rr->comp = (unsigned char *)
599 OPENSSL_malloc(SSL3_RT_MAX_ENCRYPTED_LENGTH);
601 if (rr->comp == NULL)
604 /* TODO(size_t): Convert this call */
605 i = COMP_expand_block(ssl->expand, rr->comp,
606 SSL3_RT_MAX_PLAIN_LENGTH, rr->data, (int)rr->length);
616 int ssl3_do_compress(SSL *ssl, SSL3_RECORD *wr)
618 #ifndef OPENSSL_NO_COMP
621 /* TODO(size_t): Convert this call */
622 i = COMP_compress_block(ssl->compress, wr->data,
623 (int)(wr->length + SSL3_RT_MAX_COMPRESSED_OVERHEAD),
624 wr->input, (int)wr->length);
630 wr->input = wr->data;
636 * ssl3_enc encrypts/decrypts |n_recs| records in |inrecs|
639 * 0: (in non-constant time) if the record is publically invalid (i.e. too
641 * 1: if the record's padding is valid / the encryption was successful.
642 * -1: if the record's padding is invalid or, if sending, an internal error
645 int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, size_t n_recs, int send)
650 size_t bs, mac_size = 0;
652 const EVP_CIPHER *enc;
656 * We shouldn't ever be called with more than one record in the SSLv3 case
661 ds = s->enc_write_ctx;
662 if (s->enc_write_ctx == NULL)
665 enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
667 ds = s->enc_read_ctx;
668 if (s->enc_read_ctx == NULL)
671 enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
674 if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
675 memmove(rec->data, rec->input, rec->length);
676 rec->input = rec->data;
679 /* TODO(size_t): Convert this call */
680 bs = EVP_CIPHER_CTX_block_size(ds);
684 if ((bs != 1) && send) {
687 /* we need to add 'i-1' padding bytes */
690 * the last of these zero bytes will be overwritten with the
693 memset(&rec->input[rec->length], 0, i);
695 rec->input[l - 1] = (unsigned char)(i - 1);
699 if (l == 0 || l % bs != 0)
701 /* otherwise, rec->length >= bs */
704 /* TODO(size_t): Convert this call */
705 if (EVP_Cipher(ds, rec->data, rec->input, (unsigned int)l) < 1)
708 if (EVP_MD_CTX_md(s->read_hash) != NULL) {
709 /* TODO(size_t): convert me */
710 imac_size = EVP_MD_CTX_size(s->read_hash);
713 mac_size = (size_t)imac_size;
715 if ((bs != 1) && !send)
716 return ssl3_cbc_remove_padding(rec, bs, mac_size);
721 #define MAX_PADDING 256
723 * tls1_enc encrypts/decrypts |n_recs| in |recs|.
726 * 0: (in non-constant time) if the record is publically invalid (i.e. too
728 * 1: if the record's padding is valid / the encryption was successful.
729 * -1: if the record's padding/AEAD-authenticator is invalid or, if sending,
730 * an internal error occurred.
732 int tls1_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int send)
735 size_t reclen[SSL_MAX_PIPELINES];
736 unsigned char buf[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
737 int i, pad = 0, ret, tmpr;
738 size_t bs, mac_size = 0, ctr, padnum, loop;
739 unsigned char padval;
741 const EVP_CIPHER *enc;
744 if (EVP_MD_CTX_md(s->write_hash)) {
745 int n = EVP_MD_CTX_size(s->write_hash);
746 OPENSSL_assert(n >= 0);
748 ds = s->enc_write_ctx;
749 if (s->enc_write_ctx == NULL)
753 enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
754 /* For TLSv1.1 and later explicit IV */
755 if (SSL_USE_EXPLICIT_IV(s)
756 && EVP_CIPHER_mode(enc) == EVP_CIPH_CBC_MODE)
757 ivlen = EVP_CIPHER_iv_length(enc);
761 for (ctr = 0; ctr < n_recs; ctr++) {
762 if (recs[ctr].data != recs[ctr].input) {
764 * we can't write into the input stream: Can this ever
767 SSLerr(SSL_F_TLS1_ENC, ERR_R_INTERNAL_ERROR);
769 } else if (RAND_bytes(recs[ctr].input, ivlen) <= 0) {
770 SSLerr(SSL_F_TLS1_ENC, ERR_R_INTERNAL_ERROR);
777 if (EVP_MD_CTX_md(s->read_hash)) {
778 int n = EVP_MD_CTX_size(s->read_hash);
779 OPENSSL_assert(n >= 0);
781 ds = s->enc_read_ctx;
782 if (s->enc_read_ctx == NULL)
785 enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
788 if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
789 for (ctr = 0; ctr < n_recs; ctr++) {
790 memmove(recs[ctr].data, recs[ctr].input, recs[ctr].length);
791 recs[ctr].input = recs[ctr].data;
795 bs = EVP_CIPHER_block_size(EVP_CIPHER_CTX_cipher(ds));
798 if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
799 & EVP_CIPH_FLAG_PIPELINE)) {
801 * We shouldn't have been called with pipeline data if the
802 * cipher doesn't support pipelining
804 SSLerr(SSL_F_TLS1_ENC, SSL_R_PIPELINE_FAILURE);
808 for (ctr = 0; ctr < n_recs; ctr++) {
809 reclen[ctr] = recs[ctr].length;
811 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
812 & EVP_CIPH_FLAG_AEAD_CIPHER) {
815 seq = send ? RECORD_LAYER_get_write_sequence(&s->rlayer)
816 : RECORD_LAYER_get_read_sequence(&s->rlayer);
818 if (SSL_IS_DTLS(s)) {
819 /* DTLS does not support pipelining */
820 unsigned char dtlsseq[9], *p = dtlsseq;
822 s2n(send ? DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer) :
823 DTLS_RECORD_LAYER_get_r_epoch(&s->rlayer), p);
824 memcpy(p, &seq[2], 6);
825 memcpy(buf[ctr], dtlsseq, 8);
827 memcpy(buf[ctr], seq, 8);
828 for (i = 7; i >= 0; i--) { /* increment */
835 buf[ctr][8] = recs[ctr].type;
836 buf[ctr][9] = (unsigned char)(s->version >> 8);
837 buf[ctr][10] = (unsigned char)(s->version);
838 buf[ctr][11] = (unsigned char)(recs[ctr].length >> 8);
839 buf[ctr][12] = (unsigned char)(recs[ctr].length & 0xff);
840 pad = EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_AEAD_TLS1_AAD,
841 EVP_AEAD_TLS1_AAD_LEN, buf[ctr]);
847 recs[ctr].length += pad;
850 } else if ((bs != 1) && send) {
851 padnum = bs - (reclen[ctr] % bs);
853 /* Add weird padding of upto 256 bytes */
855 if (padnum > MAX_PADDING)
857 /* we need to add 'padnum' padding bytes of value padval */
858 padval = (unsigned char)(padnum - 1);
859 for (loop = reclen[ctr]; loop < reclen[ctr] + padnum; loop++)
860 recs[ctr].input[loop] = padval;
861 reclen[ctr] += padnum;
862 recs[ctr].length += padnum;
866 if (reclen[ctr] == 0 || reclen[ctr] % bs != 0)
871 unsigned char *data[SSL_MAX_PIPELINES];
873 /* Set the output buffers */
874 for (ctr = 0; ctr < n_recs; ctr++) {
875 data[ctr] = recs[ctr].data;
877 if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS,
878 (int)n_recs, data) <= 0) {
879 SSLerr(SSL_F_TLS1_ENC, SSL_R_PIPELINE_FAILURE);
881 /* Set the input buffers */
882 for (ctr = 0; ctr < n_recs; ctr++) {
883 data[ctr] = recs[ctr].input;
885 if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_BUFS,
886 (int)n_recs, data) <= 0
887 || EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_LENS,
888 (int)n_recs, reclen) <= 0) {
889 SSLerr(SSL_F_TLS1_ENC, SSL_R_PIPELINE_FAILURE);
894 /* TODO(size_t): Convert this call */
895 tmpr = EVP_Cipher(ds, recs[0].data, recs[0].input,
896 (unsigned int)reclen[0]);
897 if ((EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
898 & EVP_CIPH_FLAG_CUSTOM_CIPHER)
901 return -1; /* AEAD can fail to verify MAC */
903 if (EVP_CIPHER_mode(enc) == EVP_CIPH_GCM_MODE) {
904 for (ctr = 0; ctr < n_recs; ctr++) {
905 recs[ctr].data += EVP_GCM_TLS_EXPLICIT_IV_LEN;
906 recs[ctr].input += EVP_GCM_TLS_EXPLICIT_IV_LEN;
907 recs[ctr].length -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
909 } else if (EVP_CIPHER_mode(enc) == EVP_CIPH_CCM_MODE) {
910 for (ctr = 0; ctr < n_recs; ctr++) {
911 recs[ctr].data += EVP_CCM_TLS_EXPLICIT_IV_LEN;
912 recs[ctr].input += EVP_CCM_TLS_EXPLICIT_IV_LEN;
913 recs[ctr].length -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
919 if (!SSL_READ_ETM(s) && EVP_MD_CTX_md(s->read_hash) != NULL) {
920 imac_size = EVP_MD_CTX_size(s->read_hash);
923 mac_size = (size_t)imac_size;
925 if ((bs != 1) && !send) {
927 for (ctr = 0; ctr < n_recs; ctr++) {
928 tmpret = tls1_cbc_remove_padding(s, &recs[ctr], bs, mac_size);
930 * If tmpret == 0 then this means publicly invalid so we can
931 * short circuit things here. Otherwise we must respect constant
936 ret = constant_time_select_int(constant_time_eq_int(tmpret, 1),
941 for (ctr = 0; ctr < n_recs; ctr++) {
942 recs[ctr].length -= pad;
949 int n_ssl3_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int send)
951 unsigned char *mac_sec, *seq;
952 const EVP_MD_CTX *hash;
953 unsigned char *p, rec_char;
959 mac_sec = &(ssl->s3->write_mac_secret[0]);
960 seq = RECORD_LAYER_get_write_sequence(&ssl->rlayer);
961 hash = ssl->write_hash;
963 mac_sec = &(ssl->s3->read_mac_secret[0]);
964 seq = RECORD_LAYER_get_read_sequence(&ssl->rlayer);
965 hash = ssl->read_hash;
968 t = EVP_MD_CTX_size(hash);
972 npad = (48 / md_size) * md_size;
975 EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
976 ssl3_cbc_record_digest_supported(hash)) {
978 * This is a CBC-encrypted record. We must avoid leaking any
979 * timing-side channel information about how many blocks of data we
980 * are hashing because that gives an attacker a timing-oracle.
984 * npad is, at most, 48 bytes and that's with MD5:
985 * 16 + 48 + 8 (sequence bytes) + 1 + 2 = 75.
987 * With SHA-1 (the largest hash speced for SSLv3) the hash size
988 * goes up 4, but npad goes down by 8, resulting in a smaller
991 unsigned char header[75];
993 memcpy(header + j, mac_sec, md_size);
995 memcpy(header + j, ssl3_pad_1, npad);
997 memcpy(header + j, seq, 8);
999 header[j++] = rec->type;
1000 header[j++] = (unsigned char)(rec->length >> 8);
1001 header[j++] = (unsigned char)(rec->length & 0xff);
1003 /* Final param == is SSLv3 */
1004 if (ssl3_cbc_digest_record(hash,
1007 rec->length + md_size, rec->orig_len,
1008 mac_sec, md_size, 1) <= 0)
1011 unsigned int md_size_u;
1012 /* Chop the digest off the end :-) */
1013 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
1018 rec_char = rec->type;
1020 s2n(rec->length, p);
1021 if (EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
1022 || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
1023 || EVP_DigestUpdate(md_ctx, ssl3_pad_1, npad) <= 0
1024 || EVP_DigestUpdate(md_ctx, seq, 8) <= 0
1025 || EVP_DigestUpdate(md_ctx, &rec_char, 1) <= 0
1026 || EVP_DigestUpdate(md_ctx, md, 2) <= 0
1027 || EVP_DigestUpdate(md_ctx, rec->input, rec->length) <= 0
1028 || EVP_DigestFinal_ex(md_ctx, md, NULL) <= 0
1029 || EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
1030 || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
1031 || EVP_DigestUpdate(md_ctx, ssl3_pad_2, npad) <= 0
1032 || EVP_DigestUpdate(md_ctx, md, md_size) <= 0
1033 || EVP_DigestFinal_ex(md_ctx, md, &md_size_u) <= 0) {
1034 EVP_MD_CTX_reset(md_ctx);
1038 EVP_MD_CTX_free(md_ctx);
1041 ssl3_record_sequence_update(seq);
1045 int tls1_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int send)
1051 EVP_MD_CTX *hmac = NULL, *mac_ctx;
1052 unsigned char header[13];
1053 int stream_mac = (send ? (ssl->mac_flags & SSL_MAC_FLAG_WRITE_MAC_STREAM)
1054 : (ssl->mac_flags & SSL_MAC_FLAG_READ_MAC_STREAM));
1058 seq = RECORD_LAYER_get_write_sequence(&ssl->rlayer);
1059 hash = ssl->write_hash;
1061 seq = RECORD_LAYER_get_read_sequence(&ssl->rlayer);
1062 hash = ssl->read_hash;
1065 t = EVP_MD_CTX_size(hash);
1066 OPENSSL_assert(t >= 0);
1069 /* I should fix this up TLS TLS TLS TLS TLS XXXXXXXX */
1073 hmac = EVP_MD_CTX_new();
1074 if (hmac == NULL || !EVP_MD_CTX_copy(hmac, hash))
1079 if (SSL_IS_DTLS(ssl)) {
1080 unsigned char dtlsseq[8], *p = dtlsseq;
1082 s2n(send ? DTLS_RECORD_LAYER_get_w_epoch(&ssl->rlayer) :
1083 DTLS_RECORD_LAYER_get_r_epoch(&ssl->rlayer), p);
1084 memcpy(p, &seq[2], 6);
1086 memcpy(header, dtlsseq, 8);
1088 memcpy(header, seq, 8);
1090 header[8] = rec->type;
1091 header[9] = (unsigned char)(ssl->version >> 8);
1092 header[10] = (unsigned char)(ssl->version);
1093 header[11] = (unsigned char)(rec->length >> 8);
1094 header[12] = (unsigned char)(rec->length & 0xff);
1096 if (!send && !SSL_READ_ETM(ssl) &&
1097 EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
1098 ssl3_cbc_record_digest_supported(mac_ctx)) {
1100 * This is a CBC-encrypted record. We must avoid leaking any
1101 * timing-side channel information about how many blocks of data we
1102 * are hashing because that gives an attacker a timing-oracle.
1104 /* Final param == not SSLv3 */
1105 if (ssl3_cbc_digest_record(mac_ctx,
1108 rec->length + md_size, rec->orig_len,
1109 ssl->s3->read_mac_secret,
1110 ssl->s3->read_mac_secret_size, 0) <= 0) {
1111 EVP_MD_CTX_free(hmac);
1115 /* TODO(size_t): Convert these calls */
1116 if (EVP_DigestSignUpdate(mac_ctx, header, sizeof(header)) <= 0
1117 || EVP_DigestSignUpdate(mac_ctx, rec->input, rec->length) <= 0
1118 || EVP_DigestSignFinal(mac_ctx, md, &md_size) <= 0) {
1119 EVP_MD_CTX_free(hmac);
1122 if (!send && !SSL_READ_ETM(ssl) && FIPS_mode())
1123 if (!tls_fips_digest_extra(ssl->enc_read_ctx,
1124 mac_ctx, rec->input,
1125 rec->length, rec->orig_len)) {
1126 EVP_MD_CTX_free(hmac);
1131 EVP_MD_CTX_free(hmac);
1134 fprintf(stderr, "seq=");
1137 for (z = 0; z < 8; z++)
1138 fprintf(stderr, "%02X ", seq[z]);
1139 fprintf(stderr, "\n");
1141 fprintf(stderr, "rec=");
1144 for (z = 0; z < rec->length; z++)
1145 fprintf(stderr, "%02X ", rec->data[z]);
1146 fprintf(stderr, "\n");
1150 if (!SSL_IS_DTLS(ssl)) {
1151 for (i = 7; i >= 0; i--) {
1160 for (z = 0; z < md_size; z++)
1161 fprintf(stderr, "%02X ", md[z]);
1162 fprintf(stderr, "\n");
1169 * ssl3_cbc_remove_padding removes padding from the decrypted, SSLv3, CBC
1170 * record in |rec| by updating |rec->length| in constant time.
1172 * block_size: the block size of the cipher used to encrypt the record.
1174 * 0: (in non-constant time) if the record is publicly invalid.
1175 * 1: if the padding was valid
1178 int ssl3_cbc_remove_padding(SSL3_RECORD *rec,
1179 size_t block_size, size_t mac_size)
1181 size_t padding_length;
1183 const size_t overhead = 1 /* padding length byte */ + mac_size;
1186 * These lengths are all public so we can test them in non-constant time.
1188 if (overhead > rec->length)
1191 padding_length = rec->data[rec->length - 1];
1192 good = constant_time_ge_s(rec->length, padding_length + overhead);
1193 /* SSLv3 requires that the padding is minimal. */
1194 good &= constant_time_ge_s(block_size, padding_length + 1);
1195 rec->length -= good & (padding_length + 1);
1196 return constant_time_select_int_s(good, 1, -1);
1200 * tls1_cbc_remove_padding removes the CBC padding from the decrypted, TLS, CBC
1201 * record in |rec| in constant time and returns 1 if the padding is valid and
1202 * -1 otherwise. It also removes any explicit IV from the start of the record
1203 * without leaking any timing about whether there was enough space after the
1204 * padding was removed.
1206 * block_size: the block size of the cipher used to encrypt the record.
1208 * 0: (in non-constant time) if the record is publicly invalid.
1209 * 1: if the padding was valid
1212 int tls1_cbc_remove_padding(const SSL *s,
1214 size_t block_size, size_t mac_size)
1217 size_t padding_length, to_check, i;
1218 const size_t overhead = 1 /* padding length byte */ + mac_size;
1219 /* Check if version requires explicit IV */
1220 if (SSL_USE_EXPLICIT_IV(s)) {
1222 * These lengths are all public so we can test them in non-constant
1225 if (overhead + block_size > rec->length)
1227 /* We can now safely skip explicit IV */
1228 rec->data += block_size;
1229 rec->input += block_size;
1230 rec->length -= block_size;
1231 rec->orig_len -= block_size;
1232 } else if (overhead > rec->length)
1235 padding_length = rec->data[rec->length - 1];
1237 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_read_ctx)) &
1238 EVP_CIPH_FLAG_AEAD_CIPHER) {
1239 /* padding is already verified */
1240 rec->length -= padding_length + 1;
1244 good = constant_time_ge_s(rec->length, overhead + padding_length);
1246 * The padding consists of a length byte at the end of the record and
1247 * then that many bytes of padding, all with the same value as the length
1248 * byte. Thus, with the length byte included, there are i+1 bytes of
1249 * padding. We can't check just |padding_length+1| bytes because that
1250 * leaks decrypted information. Therefore we always have to check the
1251 * maximum amount of padding possible. (Again, the length of the record
1252 * is public information so we can use it.)
1254 to_check = 256; /* maximum amount of padding, inc length byte. */
1255 if (to_check > rec->length)
1256 to_check = rec->length;
1258 for (i = 0; i < to_check; i++) {
1259 unsigned char mask = constant_time_ge_8_s(padding_length, i);
1260 unsigned char b = rec->data[rec->length - 1 - i];
1262 * The final |padding_length+1| bytes should all have the value
1263 * |padding_length|. Therefore the XOR should be zero.
1265 good &= ~(mask & (padding_length ^ b));
1269 * If any of the final |padding_length+1| bytes had the wrong value, one
1270 * or more of the lower eight bits of |good| will be cleared.
1272 good = constant_time_eq_s(0xff, good & 0xff);
1273 rec->length -= good & (padding_length + 1);
1275 return constant_time_select_int_s(good, 1, -1);
1279 * ssl3_cbc_copy_mac copies |md_size| bytes from the end of |rec| to |out| in
1280 * constant time (independent of the concrete value of rec->length, which may
1281 * vary within a 256-byte window).
1283 * ssl3_cbc_remove_padding or tls1_cbc_remove_padding must be called prior to
1287 * rec->orig_len >= md_size
1288 * md_size <= EVP_MAX_MD_SIZE
1290 * If CBC_MAC_ROTATE_IN_PLACE is defined then the rotation is performed with
1291 * variable accesses in a 64-byte-aligned buffer. Assuming that this fits into
1292 * a single or pair of cache-lines, then the variable memory accesses don't
1293 * actually affect the timing. CPUs with smaller cache-lines [if any] are
1294 * not multi-core and are not considered vulnerable to cache-timing attacks.
1296 #define CBC_MAC_ROTATE_IN_PLACE
1298 void ssl3_cbc_copy_mac(unsigned char *out,
1299 const SSL3_RECORD *rec, size_t md_size)
1301 #if defined(CBC_MAC_ROTATE_IN_PLACE)
1302 unsigned char rotated_mac_buf[64 + EVP_MAX_MD_SIZE];
1303 unsigned char *rotated_mac;
1305 unsigned char rotated_mac[EVP_MAX_MD_SIZE];
1309 * mac_end is the index of |rec->data| just after the end of the MAC.
1311 size_t mac_end = rec->length;
1312 size_t mac_start = mac_end - md_size;
1315 * scan_start contains the number of bytes that we can ignore because the
1316 * MAC's position can only vary by 255 bytes.
1318 size_t scan_start = 0;
1320 size_t rotate_offset;
1322 OPENSSL_assert(rec->orig_len >= md_size);
1323 OPENSSL_assert(md_size <= EVP_MAX_MD_SIZE);
1325 #if defined(CBC_MAC_ROTATE_IN_PLACE)
1326 rotated_mac = rotated_mac_buf + ((0 - (size_t)rotated_mac_buf) & 63);
1329 /* This information is public so it's safe to branch based on it. */
1330 if (rec->orig_len > md_size + 255 + 1)
1331 scan_start = rec->orig_len - (md_size + 255 + 1);
1335 memset(rotated_mac, 0, md_size);
1336 for (i = scan_start, j = 0; i < rec->orig_len; i++) {
1337 size_t mac_started = constant_time_eq_s(i, mac_start);
1338 size_t mac_ended = constant_time_lt_s(i, mac_end);
1339 unsigned char b = rec->data[i];
1341 in_mac |= mac_started;
1342 in_mac &= mac_ended;
1343 rotate_offset |= j & mac_started;
1344 rotated_mac[j++] |= b & in_mac;
1345 j &= constant_time_lt_s(j, md_size);
1348 /* Now rotate the MAC */
1349 #if defined(CBC_MAC_ROTATE_IN_PLACE)
1351 for (i = 0; i < md_size; i++) {
1352 /* in case cache-line is 32 bytes, touch second line */
1353 ((volatile unsigned char *)rotated_mac)[rotate_offset ^ 32];
1354 out[j++] = rotated_mac[rotate_offset++];
1355 rotate_offset &= constant_time_lt_s(rotate_offset, md_size);
1358 memset(out, 0, md_size);
1359 rotate_offset = md_size - rotate_offset;
1360 rotate_offset &= constant_time_lt_s(rotate_offset, md_size);
1361 for (i = 0; i < md_size; i++) {
1362 for (j = 0; j < md_size; j++)
1363 out[j] |= rotated_mac[i] & constant_time_eq_8_s(j, rotate_offset);
1365 rotate_offset &= constant_time_lt_s(rotate_offset, md_size);
1370 int dtls1_process_record(SSL *s, DTLS1_BITMAP *bitmap)
1378 unsigned char md[EVP_MAX_MD_SIZE];
1380 rr = RECORD_LAYER_get_rrec(&s->rlayer);
1384 * At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length,
1385 * and we have that many bytes in s->packet
1387 rr->input = &(RECORD_LAYER_get_packet(&s->rlayer)[DTLS1_RT_HEADER_LENGTH]);
1390 * ok, we can now read from 's->packet' data into 'rr' rr->input points
1391 * at rr->length bytes, which need to be copied into rr->data by either
1392 * the decryption or by the decompression When the data is 'copied' into
1393 * the rr->data buffer, rr->input will be pointed at the new buffer
1397 * We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
1398 * bytes of encrypted compressed stuff.
1401 /* check is not needed I believe */
1402 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
1403 al = SSL_AD_RECORD_OVERFLOW;
1404 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
1408 /* decrypt in place in 'rr->input' */
1409 rr->data = rr->input;
1410 rr->orig_len = rr->length;
1412 if (SSL_READ_ETM(s) && s->read_hash) {
1414 mac_size = EVP_MD_CTX_size(s->read_hash);
1415 OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
1416 if (rr->orig_len < mac_size) {
1417 al = SSL_AD_DECODE_ERROR;
1418 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_LENGTH_TOO_SHORT);
1421 rr->length -= mac_size;
1422 mac = rr->data + rr->length;
1423 i = s->method->ssl3_enc->mac(s, rr, md, 0 /* not send */ );
1424 if (i == 0 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0) {
1425 al = SSL_AD_BAD_RECORD_MAC;
1426 SSLerr(SSL_F_DTLS1_PROCESS_RECORD,
1427 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
1432 enc_err = s->method->ssl3_enc->enc(s, rr, 1, 0);
1435 * 0: (in non-constant time) if the record is publically invalid.
1436 * 1: if the padding is valid
1437 * -1: if the padding is invalid
1440 /* For DTLS we simply ignore bad packets. */
1442 RECORD_LAYER_reset_packet_length(&s->rlayer);
1446 printf("dec %ld\n", rr->length);
1449 for (z = 0; z < rr->length; z++)
1450 printf("%02X%c", rr->data[z], ((z + 1) % 16) ? ' ' : '\n');
1455 /* r->length is now the compressed data plus mac */
1456 if ((sess != NULL) && !SSL_READ_ETM(s) &&
1457 (s->enc_read_ctx != NULL) && (EVP_MD_CTX_md(s->read_hash) != NULL)) {
1458 /* s->read_hash != NULL => mac_size != -1 */
1459 unsigned char *mac = NULL;
1460 unsigned char mac_tmp[EVP_MAX_MD_SIZE];
1462 /* TODO(size_t): Convert this to do size_t properly */
1463 imac_size = EVP_MD_CTX_size(s->read_hash);
1464 if (imac_size < 0) {
1465 al = SSL_AD_INTERNAL_ERROR;
1466 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, ERR_LIB_EVP);
1469 mac_size = (size_t)imac_size;
1470 OPENSSL_assert(mac_size <= EVP_MAX_MD_SIZE);
1473 * orig_len is the length of the record before any padding was
1474 * removed. This is public information, as is the MAC in use,
1475 * therefore we can safely process the record in a different amount
1476 * of time if it's too short to possibly contain a MAC.
1478 if (rr->orig_len < mac_size ||
1479 /* CBC records must have a padding length byte too. */
1480 (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
1481 rr->orig_len < mac_size + 1)) {
1482 al = SSL_AD_DECODE_ERROR;
1483 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_LENGTH_TOO_SHORT);
1487 if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {
1489 * We update the length so that the TLS header bytes can be
1490 * constructed correctly but we need to extract the MAC in
1491 * constant time from within the record, without leaking the
1492 * contents of the padding bytes.
1495 ssl3_cbc_copy_mac(mac_tmp, rr, mac_size);
1496 rr->length -= mac_size;
1499 * In this case there's no padding, so |rec->orig_len| equals
1500 * |rec->length| and we checked that there's enough bytes for
1503 rr->length -= mac_size;
1504 mac = &rr->data[rr->length];
1507 i = s->method->ssl3_enc->mac(s, rr, md, 0 /* not send */ );
1508 if (i == 0 || mac == NULL
1509 || CRYPTO_memcmp(md, mac, mac_size) != 0)
1511 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
1516 /* decryption failed, silently discard message */
1518 RECORD_LAYER_reset_packet_length(&s->rlayer);
1522 /* r->length is now just compressed */
1523 if (s->expand != NULL) {
1524 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
1525 al = SSL_AD_RECORD_OVERFLOW;
1526 SSLerr(SSL_F_DTLS1_PROCESS_RECORD,
1527 SSL_R_COMPRESSED_LENGTH_TOO_LONG);
1530 if (!ssl3_do_uncompress(s, rr)) {
1531 al = SSL_AD_DECOMPRESSION_FAILURE;
1532 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_BAD_DECOMPRESSION);
1537 if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH) {
1538 al = SSL_AD_RECORD_OVERFLOW;
1539 SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_DATA_LENGTH_TOO_LONG);
1545 * So at this point the following is true
1546 * ssl->s3->rrec.type is the type of record
1547 * ssl->s3->rrec.length == number of bytes in record
1548 * ssl->s3->rrec.off == offset to first valid byte
1549 * ssl->s3->rrec.data == where to take bytes from, increment
1553 /* we have pulled in a full packet so zero things */
1554 RECORD_LAYER_reset_packet_length(&s->rlayer);
1556 /* Mark receipt of record. */
1557 dtls1_record_bitmap_update(s, bitmap);
1562 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1568 * retrieve a buffered record that belongs to the current epoch, ie,
1571 #define dtls1_get_processed_record(s) \
1572 dtls1_retrieve_buffered_record((s), \
1573 &(DTLS_RECORD_LAYER_get_processed_rcds(&s->rlayer)))
1576 * Call this to get a new input record.
1577 * It will return <= 0 if more data is needed, normally due to an error
1578 * or non-blocking IO.
1579 * When it finishes, one packet has been decoded and can be found in
1580 * ssl->s3->rrec.type - is the type of record
1581 * ssl->s3->rrec.data, - data
1582 * ssl->s3->rrec.length, - number of bytes
1584 /* used only by dtls1_read_bytes */
1585 int dtls1_get_record(SSL *s)
1587 int ssl_major, ssl_minor;
1591 unsigned char *p = NULL;
1592 unsigned short version;
1593 DTLS1_BITMAP *bitmap;
1594 unsigned int is_next_epoch;
1596 rr = RECORD_LAYER_get_rrec(&s->rlayer);
1600 * The epoch may have changed. If so, process all the pending records.
1601 * This is a non-blocking operation.
1603 if (!dtls1_process_buffered_records(s))
1606 /* if we're renegotiating, then there may be buffered records */
1607 if (dtls1_get_processed_record(s))
1610 /* get something from the wire */
1612 /* check if we have the header */
1613 if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||
1614 (RECORD_LAYER_get_packet_length(&s->rlayer) < DTLS1_RT_HEADER_LENGTH)) {
1615 rret = ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH,
1616 SSL3_BUFFER_get_len(&s->rlayer.rbuf), 0, 1, &n);
1617 /* read timeout is handled by dtls1_read_bytes */
1619 return rret; /* error or non-blocking */
1621 /* this packet contained a partial record, dump it */
1622 if (RECORD_LAYER_get_packet_length(&s->rlayer) !=
1623 DTLS1_RT_HEADER_LENGTH) {
1624 RECORD_LAYER_reset_packet_length(&s->rlayer);
1628 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);
1630 p = RECORD_LAYER_get_packet(&s->rlayer);
1632 if (s->msg_callback)
1633 s->msg_callback(0, 0, SSL3_RT_HEADER, p, DTLS1_RT_HEADER_LENGTH,
1634 s, s->msg_callback_arg);
1636 /* Pull apart the header into the DTLS1_RECORD */
1640 version = (ssl_major << 8) | ssl_minor;
1642 /* sequence number is 64 bits, with top 2 bytes = epoch */
1645 memcpy(&(RECORD_LAYER_get_read_sequence(&s->rlayer)[2]), p, 6);
1650 /* Lets check version */
1651 if (!s->first_packet) {
1652 if (version != s->version) {
1653 /* unexpected version, silently discard */
1655 RECORD_LAYER_reset_packet_length(&s->rlayer);
1660 if ((version & 0xff00) != (s->version & 0xff00)) {
1661 /* wrong version, silently discard record */
1663 RECORD_LAYER_reset_packet_length(&s->rlayer);
1667 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
1668 /* record too long, silently discard it */
1670 RECORD_LAYER_reset_packet_length(&s->rlayer);
1674 /* now s->rlayer.rstate == SSL_ST_READ_BODY */
1677 /* s->rlayer.rstate == SSL_ST_READ_BODY, get and decode the data */
1680 RECORD_LAYER_get_packet_length(&s->rlayer) - DTLS1_RT_HEADER_LENGTH) {
1681 /* now s->packet_length == DTLS1_RT_HEADER_LENGTH */
1683 rret = ssl3_read_n(s, more, more, 1, 1, &n);
1684 /* this packet contained a partial record, dump it */
1685 if (rret <= 0 || n != more) {
1687 RECORD_LAYER_reset_packet_length(&s->rlayer);
1692 * now n == rr->length, and s->packet_length ==
1693 * DTLS1_RT_HEADER_LENGTH + rr->length
1696 /* set state for later operations */
1697 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);
1699 /* match epochs. NULL means the packet is dropped on the floor */
1700 bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);
1701 if (bitmap == NULL) {
1703 RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
1704 goto again; /* get another record */
1706 #ifndef OPENSSL_NO_SCTP
1707 /* Only do replay check if no SCTP bio */
1708 if (!BIO_dgram_is_sctp(SSL_get_rbio(s))) {
1710 /* Check whether this is a repeat, or aged record. */
1712 * TODO: Does it make sense to have replay protection in epoch 0 where
1713 * we have no integrity negotiated yet?
1715 if (!dtls1_record_replay_check(s, bitmap)) {
1717 RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
1718 goto again; /* get another record */
1720 #ifndef OPENSSL_NO_SCTP
1724 /* just read a 0 length packet */
1725 if (rr->length == 0)
1729 * If this record is from the next epoch (either HM or ALERT), and a
1730 * handshake is currently in progress, buffer it since it cannot be
1731 * processed at this time.
1733 if (is_next_epoch) {
1734 if ((SSL_in_init(s) || ossl_statem_get_in_handshake(s))) {
1735 if (dtls1_buffer_record
1736 (s, &(DTLS_RECORD_LAYER_get_unprocessed_rcds(&s->rlayer)),
1741 RECORD_LAYER_reset_packet_length(&s->rlayer);
1745 if (!dtls1_process_record(s, bitmap)) {
1747 RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
1748 goto again; /* get another record */