2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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_local.h"
11 #include <openssl/trace.h>
12 #include <openssl/rand.h>
13 #include <openssl/core_names.h>
14 #include "record_local.h"
15 #include "internal/cryptlib.h"
17 static const unsigned char ssl3_pad_1[48] = {
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,
23 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36
26 static const unsigned char ssl3_pad_2[48] = {
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,
32 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c
36 * Clear the contents of an SSL3_RECORD but retain any memory allocated
38 void SSL3_RECORD_clear(SSL3_RECORD *r, size_t num_recs)
43 for (i = 0; i < num_recs; i++) {
46 memset(&r[i], 0, sizeof(*r));
51 void SSL3_RECORD_release(SSL3_RECORD *r, size_t num_recs)
55 for (i = 0; i < num_recs; i++) {
56 OPENSSL_free(r[i].comp);
61 void SSL3_RECORD_set_seq_num(SSL3_RECORD *r, const unsigned char *seq_num)
63 memcpy(r->seq_num, seq_num, SEQ_NUM_SIZE);
67 * Peeks ahead into "read_ahead" data to see if we have a whole record waiting
68 * for us in the buffer.
70 static int ssl3_record_app_data_waiting(SSL *s)
76 rbuf = RECORD_LAYER_get_rbuf(&s->rlayer);
78 p = SSL3_BUFFER_get_buf(rbuf);
82 left = SSL3_BUFFER_get_left(rbuf);
84 if (left < SSL3_RT_HEADER_LENGTH)
87 p += SSL3_BUFFER_get_offset(rbuf);
90 * We only check the type and record length, we will sanity check version
93 if (*p != SSL3_RT_APPLICATION_DATA)
99 if (left < SSL3_RT_HEADER_LENGTH + len)
105 int early_data_count_ok(SSL *s, size_t length, size_t overhead, int send)
107 uint32_t max_early_data;
108 SSL_SESSION *sess = s->session;
111 * If we are a client then we always use the max_early_data from the
112 * session/psksession. Otherwise we go with the lowest out of the max early
113 * data set in the session and the configured max_early_data.
115 if (!s->server && sess->ext.max_early_data == 0) {
116 if (!ossl_assert(s->psksession != NULL
117 && s->psksession->ext.max_early_data > 0)) {
118 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_EARLY_DATA_COUNT_OK,
119 ERR_R_INTERNAL_ERROR);
122 sess = s->psksession;
126 max_early_data = sess->ext.max_early_data;
127 else if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED)
128 max_early_data = s->recv_max_early_data;
130 max_early_data = s->recv_max_early_data < sess->ext.max_early_data
131 ? s->recv_max_early_data : sess->ext.max_early_data;
133 if (max_early_data == 0) {
134 SSLfatal(s, send ? SSL_AD_INTERNAL_ERROR : SSL_AD_UNEXPECTED_MESSAGE,
135 SSL_F_EARLY_DATA_COUNT_OK, SSL_R_TOO_MUCH_EARLY_DATA);
139 /* If we are dealing with ciphertext we need to allow for the overhead */
140 max_early_data += overhead;
142 if (s->early_data_count + length > max_early_data) {
143 SSLfatal(s, send ? SSL_AD_INTERNAL_ERROR : SSL_AD_UNEXPECTED_MESSAGE,
144 SSL_F_EARLY_DATA_COUNT_OK, SSL_R_TOO_MUCH_EARLY_DATA);
147 s->early_data_count += length;
153 * MAX_EMPTY_RECORDS defines the number of consecutive, empty records that
154 * will be processed per call to ssl3_get_record. Without this limit an
155 * attacker could send empty records at a faster rate than we can process and
156 * cause ssl3_get_record to loop forever.
158 #define MAX_EMPTY_RECORDS 32
160 #define SSL2_RT_HEADER_LENGTH 2
162 * Call this to get new input records.
163 * It will return <= 0 if more data is needed, normally due to an error
164 * or non-blocking IO.
165 * When it finishes, |numrpipes| records have been decoded. For each record 'i':
166 * rr[i].type - is the type of record
168 * rr[i].length, - number of bytes
169 * Multiple records will only be returned if the record types are all
170 * SSL3_RT_APPLICATION_DATA. The number of records returned will always be <=
173 /* used only by ssl3_read_bytes */
174 int ssl3_get_record(SSL *s)
179 SSL3_RECORD *rr, *thisrr;
183 unsigned char md[EVP_MAX_MD_SIZE];
184 unsigned int version;
187 size_t num_recs = 0, max_recs, j;
188 PACKET pkt, sslv2pkt;
190 SSL_MAC_BUF *macbufs = NULL;
193 rr = RECORD_LAYER_get_rrec(&s->rlayer);
194 rbuf = RECORD_LAYER_get_rbuf(&s->rlayer);
195 is_ktls_left = (rbuf->left > 0);
196 max_recs = s->max_pipelines;
202 thisrr = &rr[num_recs];
204 /* check if we have the header */
205 if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||
206 (RECORD_LAYER_get_packet_length(&s->rlayer)
207 < SSL3_RT_HEADER_LENGTH)) {
211 rret = ssl3_read_n(s, SSL3_RT_HEADER_LENGTH,
212 SSL3_BUFFER_get_len(rbuf), 0,
213 num_recs == 0 ? 1 : 0, &n);
215 #ifndef OPENSSL_NO_KTLS
216 if (!BIO_get_ktls_recv(s->rbio) || rret == 0)
217 return rret; /* error or non-blocking */
220 SSLfatal(s, SSL_AD_BAD_RECORD_MAC,
221 SSL_F_SSL3_GET_RECORD,
222 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
225 SSLfatal(s, SSL_AD_RECORD_OVERFLOW,
226 SSL_F_SSL3_GET_RECORD,
227 SSL_R_PACKET_LENGTH_TOO_LONG);
230 SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
231 SSL_F_SSL3_GET_RECORD,
232 SSL_R_WRONG_VERSION_NUMBER);
240 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);
242 p = RECORD_LAYER_get_packet(&s->rlayer);
243 if (!PACKET_buf_init(&pkt, RECORD_LAYER_get_packet(&s->rlayer),
244 RECORD_LAYER_get_packet_length(&s->rlayer))) {
245 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GET_RECORD,
246 ERR_R_INTERNAL_ERROR);
250 if (!PACKET_get_net_2_len(&sslv2pkt, &sslv2len)
251 || !PACKET_get_1(&sslv2pkt, &type)) {
252 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL3_GET_RECORD,
253 ERR_R_INTERNAL_ERROR);
257 * The first record received by the server may be a V2ClientHello.
259 if (s->server && RECORD_LAYER_is_first_record(&s->rlayer)
260 && (sslv2len & 0x8000) != 0
261 && (type == SSL2_MT_CLIENT_HELLO)) {
265 * |num_recs| here will actually always be 0 because
266 * |num_recs > 0| only ever occurs when we are processing
267 * multiple app data records - which we know isn't the case here
268 * because it is an SSLv2ClientHello. We keep it using
269 * |num_recs| for the sake of consistency
271 thisrr->type = SSL3_RT_HANDSHAKE;
272 thisrr->rec_version = SSL2_VERSION;
274 thisrr->length = sslv2len & 0x7fff;
276 if (thisrr->length > SSL3_BUFFER_get_len(rbuf)
277 - SSL2_RT_HEADER_LENGTH) {
278 SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
279 SSL_R_PACKET_LENGTH_TOO_LONG);
283 if (thisrr->length < MIN_SSL2_RECORD_LEN) {
284 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL3_GET_RECORD,
285 SSL_R_LENGTH_TOO_SHORT);
289 /* SSLv3+ style record */
291 /* Pull apart the header into the SSL3_RECORD */
292 if (!PACKET_get_1(&pkt, &type)
293 || !PACKET_get_net_2(&pkt, &version)
294 || !PACKET_get_net_2_len(&pkt, &thisrr->length)) {
296 s->msg_callback(0, 0, SSL3_RT_HEADER, p, 5, s,
297 s->msg_callback_arg);
298 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL3_GET_RECORD,
299 ERR_R_INTERNAL_ERROR);
303 thisrr->rec_version = version;
306 s->msg_callback(0, version, SSL3_RT_HEADER, p, 5, s,
307 s->msg_callback_arg);
310 * Lets check version. In TLSv1.3 we only check this field
311 * when encryption is occurring (see later check). For the
312 * ServerHello after an HRR we haven't actually selected TLSv1.3
313 * yet, but we still treat it as TLSv1.3, so we must check for
316 if (!s->first_packet && !SSL_IS_TLS13(s)
317 && s->hello_retry_request != SSL_HRR_PENDING
318 && version != (unsigned int)s->version) {
319 if ((s->version & 0xFF00) == (version & 0xFF00)
320 && !s->enc_write_ctx && !s->write_hash) {
321 if (thisrr->type == SSL3_RT_ALERT) {
323 * The record is using an incorrect version number,
324 * but what we've got appears to be an alert. We
325 * haven't read the body yet to check whether its a
326 * fatal or not - but chances are it is. We probably
327 * shouldn't send a fatal alert back. We'll just
330 SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_SSL3_GET_RECORD,
331 SSL_R_WRONG_VERSION_NUMBER);
335 * Send back error using their minor version number :-)
337 s->version = (unsigned short)version;
339 SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_F_SSL3_GET_RECORD,
340 SSL_R_WRONG_VERSION_NUMBER);
344 if ((version >> 8) != SSL3_VERSION_MAJOR) {
345 if (RECORD_LAYER_is_first_record(&s->rlayer)) {
346 /* Go back to start of packet, look at the five bytes
348 p = RECORD_LAYER_get_packet(&s->rlayer);
349 if (strncmp((char *)p, "GET ", 4) == 0 ||
350 strncmp((char *)p, "POST ", 5) == 0 ||
351 strncmp((char *)p, "HEAD ", 5) == 0 ||
352 strncmp((char *)p, "PUT ", 4) == 0) {
353 SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_SSL3_GET_RECORD,
356 } else if (strncmp((char *)p, "CONNE", 5) == 0) {
357 SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_SSL3_GET_RECORD,
358 SSL_R_HTTPS_PROXY_REQUEST);
362 /* Doesn't look like TLS - don't send an alert */
363 SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_SSL3_GET_RECORD,
364 SSL_R_WRONG_VERSION_NUMBER);
367 SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
368 SSL_F_SSL3_GET_RECORD,
369 SSL_R_WRONG_VERSION_NUMBER);
374 if (SSL_IS_TLS13(s) && s->enc_read_ctx != NULL) {
375 if (thisrr->type != SSL3_RT_APPLICATION_DATA
376 && (thisrr->type != SSL3_RT_CHANGE_CIPHER_SPEC
377 || !SSL_IS_FIRST_HANDSHAKE(s))
378 && (thisrr->type != SSL3_RT_ALERT
379 || s->statem.enc_read_state
380 != ENC_READ_STATE_ALLOW_PLAIN_ALERTS)) {
381 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
382 SSL_F_SSL3_GET_RECORD, SSL_R_BAD_RECORD_TYPE);
385 if (thisrr->rec_version != TLS1_2_VERSION) {
386 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL3_GET_RECORD,
387 SSL_R_WRONG_VERSION_NUMBER);
393 SSL3_BUFFER_get_len(rbuf) - SSL3_RT_HEADER_LENGTH) {
394 SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
395 SSL_R_PACKET_LENGTH_TOO_LONG);
400 /* now s->rlayer.rstate == SSL_ST_READ_BODY */
403 if (SSL_IS_TLS13(s)) {
404 if (thisrr->length > SSL3_RT_MAX_TLS13_ENCRYPTED_LENGTH) {
405 SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
406 SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
410 size_t len = SSL3_RT_MAX_ENCRYPTED_LENGTH;
412 #ifndef OPENSSL_NO_COMP
414 * If OPENSSL_NO_COMP is defined then SSL3_RT_MAX_ENCRYPTED_LENGTH
415 * does not include the compression overhead anyway.
417 if (s->expand == NULL)
418 len -= SSL3_RT_MAX_COMPRESSED_OVERHEAD;
421 if (thisrr->length > len && !BIO_get_ktls_recv(s->rbio)) {
422 SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
423 SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
429 * s->rlayer.rstate == SSL_ST_READ_BODY, get and decode the data.
430 * Calculate how much more data we need to read for the rest of the
433 if (thisrr->rec_version == SSL2_VERSION) {
434 more = thisrr->length + SSL2_RT_HEADER_LENGTH
435 - SSL3_RT_HEADER_LENGTH;
437 more = thisrr->length;
441 /* now s->packet_length == SSL3_RT_HEADER_LENGTH */
443 rret = ssl3_read_n(s, more, more, 1, 0, &n);
445 return rret; /* error or non-blocking io */
448 /* set state for later operations */
449 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);
452 * At this point, s->packet_length == SSL3_RT_HEADER_LENGTH
453 * + thisrr->length, or s->packet_length == SSL2_RT_HEADER_LENGTH
454 * + thisrr->length and we have that many bytes in s->packet
456 if (thisrr->rec_version == SSL2_VERSION) {
458 &(RECORD_LAYER_get_packet(&s->rlayer)[SSL2_RT_HEADER_LENGTH]);
461 &(RECORD_LAYER_get_packet(&s->rlayer)[SSL3_RT_HEADER_LENGTH]);
465 * ok, we can now read from 's->packet' data into 'thisrr' thisrr->input
466 * points at thisrr->length bytes, which need to be copied into
467 * thisrr->data by either the decryption or by the decompression When
468 * the data is 'copied' into the thisrr->data buffer, thisrr->input will
469 * be pointed at the new buffer
473 * We now have - encrypted [ MAC [ compressed [ plain ] ] ]
474 * thisrr->length bytes of encrypted compressed stuff.
477 /* decrypt in place in 'thisrr->input' */
478 thisrr->data = thisrr->input;
479 thisrr->orig_len = thisrr->length;
481 /* Mark this record as not read by upper layers yet */
486 /* we have pulled in a full packet so zero things */
487 RECORD_LAYER_reset_packet_length(&s->rlayer);
488 RECORD_LAYER_clear_first_record(&s->rlayer);
489 } while (num_recs < max_recs
490 && thisrr->type == SSL3_RT_APPLICATION_DATA
491 && SSL_USE_EXPLICIT_IV(s)
492 && s->enc_read_ctx != NULL
493 && (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(s->enc_read_ctx))
494 & EVP_CIPH_FLAG_PIPELINE)
495 && ssl3_record_app_data_waiting(s));
498 && thisrr->type == SSL3_RT_CHANGE_CIPHER_SPEC
499 && (SSL_IS_TLS13(s) || s->hello_retry_request != SSL_HRR_NONE)
500 && SSL_IS_FIRST_HANDSHAKE(s)) {
502 * CCS messages must be exactly 1 byte long, containing the value 0x01
504 if (thisrr->length != 1 || thisrr->data[0] != 0x01) {
505 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_SSL3_GET_RECORD,
506 SSL_R_INVALID_CCS_MESSAGE);
510 * CCS messages are ignored in TLSv1.3. We treat it like an empty
513 thisrr->type = SSL3_RT_HANDSHAKE;
514 RECORD_LAYER_inc_empty_record_count(&s->rlayer);
515 if (RECORD_LAYER_get_empty_record_count(&s->rlayer)
516 > MAX_EMPTY_RECORDS) {
517 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_GET_RECORD,
518 SSL_R_UNEXPECTED_CCS_MESSAGE);
522 RECORD_LAYER_set_numrpipes(&s->rlayer, 1);
528 * KTLS reads full records. If there is any data left,
529 * then it is from before enabling ktls
531 if (BIO_get_ktls_recv(s->rbio) && !is_ktls_left)
532 goto skip_decryption;
534 /* TODO(size_t): convert this to do size_t properly */
535 if (s->read_hash != NULL) {
536 const EVP_MD *tmpmd = EVP_MD_CTX_md(s->read_hash);
539 imac_size = EVP_MD_size(tmpmd);
540 if (!ossl_assert(imac_size >= 0 && imac_size <= EVP_MAX_MD_SIZE)) {
541 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GET_RECORD,
545 mac_size = (size_t)imac_size;
550 * If in encrypt-then-mac mode calculate mac from encrypted record. All
551 * the details below are public so no timing details can leak.
553 if (SSL_READ_ETM(s) && s->read_hash) {
556 for (j = 0; j < num_recs; j++) {
559 if (thisrr->length < mac_size) {
560 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL3_GET_RECORD,
561 SSL_R_LENGTH_TOO_SHORT);
564 thisrr->length -= mac_size;
565 mac = thisrr->data + thisrr->length;
566 i = s->method->ssl3_enc->mac(s, thisrr, md, 0 /* not send */ );
567 if (i == 0 || CRYPTO_memcmp(md, mac, mac_size) != 0) {
568 SSLfatal(s, SSL_AD_BAD_RECORD_MAC, SSL_F_SSL3_GET_RECORD,
569 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
574 * We've handled the mac now - there is no MAC inside the encrypted
581 macbufs = OPENSSL_zalloc(sizeof(*macbufs) * num_recs);
582 if (macbufs == NULL) {
583 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GET_RECORD,
584 ERR_R_MALLOC_FAILURE);
589 enc_err = s->method->ssl3_enc->enc(s, rr, num_recs, 0, macbufs, mac_size);
593 * 0: if the record is publicly invalid, or an internal error, or AEAD
594 * decryption failed, or ETM decryption failed.
595 * 1: Success or MTE decryption failed (MAC will be randomised)
598 if (ossl_statem_in_error(s)) {
599 /* SSLfatal() already got called */
602 if (num_recs == 1 && ossl_statem_skip_early_data(s)) {
604 * Valid early_data that we cannot decrypt will fail here. We treat
605 * it like an empty record.
610 if (!early_data_count_ok(s, thisrr->length,
611 EARLY_DATA_CIPHERTEXT_OVERHEAD, 0)) {
612 /* SSLfatal() already called */
618 RECORD_LAYER_set_numrpipes(&s->rlayer, 1);
619 RECORD_LAYER_reset_read_sequence(&s->rlayer);
623 SSLfatal(s, SSL_AD_BAD_RECORD_MAC, SSL_F_SSL3_GET_RECORD,
624 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
627 OSSL_TRACE_BEGIN(TLS) {
628 BIO_printf(trc_out, "dec %lu\n", (unsigned long)rr[0].length);
629 BIO_dump_indent(trc_out, rr[0].data, rr[0].length, 4);
630 } OSSL_TRACE_END(TLS);
632 /* r->length is now the compressed data plus mac */
633 if ((sess != NULL) &&
634 (s->enc_read_ctx != NULL) &&
635 (!SSL_READ_ETM(s) && EVP_MD_CTX_md(s->read_hash) != NULL)) {
636 /* s->read_hash != NULL => mac_size != -1 */
638 for (j = 0; j < num_recs; j++) {
639 SSL_MAC_BUF *thismb = &macbufs[j];
642 i = s->method->ssl3_enc->mac(s, thisrr, md, 0 /* not send */ );
643 if (i == 0 || thismb == NULL || thismb->mac == NULL
644 || CRYPTO_memcmp(md, thismb->mac, (size_t)mac_size) != 0)
646 if (thisrr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
652 if (ossl_statem_in_error(s)) {
653 /* We already called SSLfatal() */
657 * A separate 'decryption_failed' alert was introduced with TLS 1.0,
658 * SSL 3.0 only has 'bad_record_mac'. But unless a decryption
659 * failure is directly visible from the ciphertext anyway, we should
660 * not reveal which kind of error occurred -- this might become
661 * visible to an attacker (e.g. via a logfile)
663 SSLfatal(s, SSL_AD_BAD_RECORD_MAC, SSL_F_SSL3_GET_RECORD,
664 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
670 for (j = 0; j < num_recs; j++) {
673 /* thisrr->length is now just compressed */
674 if (s->expand != NULL) {
675 if (thisrr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
676 SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
677 SSL_R_COMPRESSED_LENGTH_TOO_LONG);
680 if (!ssl3_do_uncompress(s, thisrr)) {
681 SSLfatal(s, SSL_AD_DECOMPRESSION_FAILURE, SSL_F_SSL3_GET_RECORD,
682 SSL_R_BAD_DECOMPRESSION);
688 && s->enc_read_ctx != NULL
689 && thisrr->type != SSL3_RT_ALERT) {
692 if (thisrr->length == 0
693 || thisrr->type != SSL3_RT_APPLICATION_DATA) {
694 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_GET_RECORD,
695 SSL_R_BAD_RECORD_TYPE);
699 /* Strip trailing padding */
700 for (end = thisrr->length - 1; end > 0 && thisrr->data[end] == 0;
704 thisrr->length = end;
705 thisrr->type = thisrr->data[end];
706 if (thisrr->type != SSL3_RT_APPLICATION_DATA
707 && thisrr->type != SSL3_RT_ALERT
708 && thisrr->type != SSL3_RT_HANDSHAKE) {
709 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_GET_RECORD,
710 SSL_R_BAD_RECORD_TYPE);
714 s->msg_callback(0, s->version, SSL3_RT_INNER_CONTENT_TYPE,
715 &thisrr->data[end], 1, s, s->msg_callback_arg);
719 * TLSv1.3 alert and handshake records are required to be non-zero in
723 && (thisrr->type == SSL3_RT_HANDSHAKE
724 || thisrr->type == SSL3_RT_ALERT)
725 && thisrr->length == 0) {
726 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_GET_RECORD,
731 if (thisrr->length > SSL3_RT_MAX_PLAIN_LENGTH && !BIO_get_ktls_recv(s->rbio)) {
732 SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
733 SSL_R_DATA_LENGTH_TOO_LONG);
737 /* If received packet overflows current Max Fragment Length setting */
738 if (s->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(s->session)
739 && thisrr->length > GET_MAX_FRAGMENT_LENGTH(s->session)
740 && !BIO_get_ktls_recv(s->rbio)) {
741 SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_SSL3_GET_RECORD,
742 SSL_R_DATA_LENGTH_TOO_LONG);
748 * So at this point the following is true
749 * thisrr->type is the type of record
750 * thisrr->length == number of bytes in record
751 * thisrr->off == offset to first valid byte
752 * thisrr->data == where to take bytes from, increment after use :-).
755 /* just read a 0 length packet */
756 if (thisrr->length == 0) {
757 RECORD_LAYER_inc_empty_record_count(&s->rlayer);
758 if (RECORD_LAYER_get_empty_record_count(&s->rlayer)
759 > MAX_EMPTY_RECORDS) {
760 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_SSL3_GET_RECORD,
761 SSL_R_RECORD_TOO_SMALL);
765 RECORD_LAYER_reset_empty_record_count(&s->rlayer);
769 if (s->early_data_state == SSL_EARLY_DATA_READING) {
771 if (thisrr->type == SSL3_RT_APPLICATION_DATA
772 && !early_data_count_ok(s, thisrr->length, 0, 0)) {
773 /* SSLfatal already called */
778 RECORD_LAYER_set_numrpipes(&s->rlayer, num_recs);
781 if (macbufs != NULL) {
782 for (j = 0; j < num_recs; j++) {
783 if (macbufs[j].alloced)
784 OPENSSL_free(macbufs[j].mac);
786 OPENSSL_free(macbufs);
791 int ssl3_do_uncompress(SSL *ssl, SSL3_RECORD *rr)
793 #ifndef OPENSSL_NO_COMP
796 if (rr->comp == NULL) {
797 rr->comp = (unsigned char *)
798 OPENSSL_malloc(SSL3_RT_MAX_ENCRYPTED_LENGTH);
800 if (rr->comp == NULL)
803 /* TODO(size_t): Convert this call */
804 i = COMP_expand_block(ssl->expand, rr->comp,
805 SSL3_RT_MAX_PLAIN_LENGTH, rr->data, (int)rr->length);
815 int ssl3_do_compress(SSL *ssl, SSL3_RECORD *wr)
817 #ifndef OPENSSL_NO_COMP
820 /* TODO(size_t): Convert this call */
821 i = COMP_compress_block(ssl->compress, wr->data,
822 (int)(wr->length + SSL3_RT_MAX_COMPRESSED_OVERHEAD),
823 wr->input, (int)wr->length);
829 wr->input = wr->data;
835 * ssl3_enc encrypts/decrypts |n_recs| records in |inrecs|. Calls SSLfatal on
836 * internal error, but not otherwise. It is the responsibility of the caller to
837 * report a bad_record_mac
840 * 0: if the record is publicly invalid, or an internal error
841 * 1: Success or Mac-then-encrypt decryption failed (MAC will be randomised)
843 int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, size_t n_recs, int sending,
844 SSL_MAC_BUF *mac, size_t macsize)
850 const EVP_CIPHER *enc;
854 * We shouldn't ever be called with more than one record in the SSLv3 case
859 ds = s->enc_write_ctx;
860 if (s->enc_write_ctx == NULL)
863 enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
865 ds = s->enc_read_ctx;
866 if (s->enc_read_ctx == NULL)
869 enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
872 if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
873 memmove(rec->data, rec->input, rec->length);
874 rec->input = rec->data;
876 int provided = (EVP_CIPHER_provider(enc) != NULL);
879 /* TODO(size_t): Convert this call */
880 bs = EVP_CIPHER_CTX_block_size(ds);
884 if ((bs != 1) && sending && !provided) {
886 * We only do this for legacy ciphers. Provided ciphers add the
887 * padding on the provider side.
891 /* we need to add 'i-1' padding bytes */
894 * the last of these zero bytes will be overwritten with the
897 memset(&rec->input[rec->length], 0, i);
899 rec->input[l - 1] = (unsigned char)(i - 1);
903 if (l == 0 || l % bs != 0) {
904 /* Publicly invalid */
907 /* otherwise, rec->length >= bs */
910 if (EVP_CIPHER_provider(enc) != NULL) {
913 if (!EVP_CipherUpdate(ds, rec->data, &outlen, rec->input,
916 rec->length = outlen;
918 if (!sending && mac != NULL) {
919 /* Now get a pointer to the MAC */
920 OSSL_PARAM params[2], *p = params;
925 *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_TLS_MAC,
928 *p = OSSL_PARAM_construct_end();
930 if (!EVP_CIPHER_CTX_get_params(ds, params)) {
931 /* Shouldn't normally happen */
932 SSLfatal(s, SSL_AD_INTERNAL_ERROR, 0,
933 ERR_R_INTERNAL_ERROR);
938 /* TODO(size_t): Convert this call */
939 if (EVP_Cipher(ds, rec->data, rec->input, (unsigned int)l) < 1) {
940 /* Shouldn't happen */
941 SSLfatal(s, SSL_AD_BAD_RECORD_MAC, 0, ERR_R_INTERNAL_ERROR);
946 return ssl3_cbc_remove_padding_and_mac(&rec->length,
949 (mac != NULL) ? &mac->mac : NULL,
950 (mac != NULL) ? &mac->alloced : NULL,
959 #define MAX_PADDING 256
961 * tls1_enc encrypts/decrypts |n_recs| in |recs|. Calls SSLfatal on internal
962 * error, but not otherwise. It is the responsibility of the caller to report
963 * a bad_record_mac - if appropriate (DTLS just drops the record).
966 * 0: if the record is publicly invalid, or an internal error, or AEAD
967 * decryption failed, or Encrypt-then-mac decryption failed.
968 * 1: Success or Mac-then-encrypt decryption failed (MAC will be randomised)
970 int tls1_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int sending,
971 SSL_MAC_BUF *macs, size_t macsize)
974 size_t reclen[SSL_MAX_PIPELINES];
975 unsigned char buf[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
976 int i, pad = 0, tmpr;
977 size_t bs, ctr, padnum, loop;
978 unsigned char padval;
979 const EVP_CIPHER *enc;
980 int tlstree_enc = sending ? (s->mac_flags & SSL_MAC_FLAG_WRITE_MAC_TLSTREE)
981 : (s->mac_flags & SSL_MAC_FLAG_READ_MAC_TLSTREE);
984 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
985 ERR_R_INTERNAL_ERROR);
990 if (EVP_MD_CTX_md(s->write_hash)) {
991 int n = EVP_MD_CTX_size(s->write_hash);
992 if (!ossl_assert(n >= 0)) {
993 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
994 ERR_R_INTERNAL_ERROR);
998 ds = s->enc_write_ctx;
999 if (s->enc_write_ctx == NULL)
1003 enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
1004 /* For TLSv1.1 and later explicit IV */
1005 if (SSL_USE_EXPLICIT_IV(s)
1006 && EVP_CIPHER_mode(enc) == EVP_CIPH_CBC_MODE)
1007 ivlen = EVP_CIPHER_iv_length(enc);
1011 for (ctr = 0; ctr < n_recs; ctr++) {
1012 if (recs[ctr].data != recs[ctr].input) {
1014 * we can't write into the input stream: Can this ever
1017 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
1018 ERR_R_INTERNAL_ERROR);
1020 } else if (RAND_bytes_ex(s->ctx->libctx, recs[ctr].input,
1022 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
1023 ERR_R_INTERNAL_ERROR);
1030 if (EVP_MD_CTX_md(s->read_hash)) {
1031 int n = EVP_MD_CTX_size(s->read_hash);
1032 if (!ossl_assert(n >= 0)) {
1033 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
1034 ERR_R_INTERNAL_ERROR);
1038 ds = s->enc_read_ctx;
1039 if (s->enc_read_ctx == NULL)
1042 enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
1045 if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
1046 for (ctr = 0; ctr < n_recs; ctr++) {
1047 memmove(recs[ctr].data, recs[ctr].input, recs[ctr].length);
1048 recs[ctr].input = recs[ctr].data;
1051 int provided = (EVP_CIPHER_provider(enc) != NULL);
1053 bs = EVP_CIPHER_block_size(EVP_CIPHER_CTX_cipher(ds));
1056 if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
1057 & EVP_CIPH_FLAG_PIPELINE)) {
1059 * We shouldn't have been called with pipeline data if the
1060 * cipher doesn't support pipelining
1062 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
1063 SSL_R_PIPELINE_FAILURE);
1067 for (ctr = 0; ctr < n_recs; ctr++) {
1068 reclen[ctr] = recs[ctr].length;
1070 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
1071 & EVP_CIPH_FLAG_AEAD_CIPHER) {
1074 seq = sending ? RECORD_LAYER_get_write_sequence(&s->rlayer)
1075 : RECORD_LAYER_get_read_sequence(&s->rlayer);
1077 if (SSL_IS_DTLS(s)) {
1078 /* DTLS does not support pipelining */
1079 unsigned char dtlsseq[9], *p = dtlsseq;
1081 s2n(sending ? DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer) :
1082 DTLS_RECORD_LAYER_get_r_epoch(&s->rlayer), p);
1083 memcpy(p, &seq[2], 6);
1084 memcpy(buf[ctr], dtlsseq, 8);
1086 memcpy(buf[ctr], seq, 8);
1087 for (i = 7; i >= 0; i--) { /* increment */
1094 buf[ctr][8] = recs[ctr].type;
1095 buf[ctr][9] = (unsigned char)(s->version >> 8);
1096 buf[ctr][10] = (unsigned char)(s->version);
1097 buf[ctr][11] = (unsigned char)(recs[ctr].length >> 8);
1098 buf[ctr][12] = (unsigned char)(recs[ctr].length & 0xff);
1099 pad = EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_AEAD_TLS1_AAD,
1100 EVP_AEAD_TLS1_AAD_LEN, buf[ctr]);
1102 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
1103 ERR_R_INTERNAL_ERROR);
1109 recs[ctr].length += pad;
1112 } else if ((bs != 1) && sending && !provided) {
1114 * We only do this for legacy ciphers. Provided ciphers add the
1115 * padding on the provider side.
1117 padnum = bs - (reclen[ctr] % bs);
1119 /* Add weird padding of up to 256 bytes */
1121 if (padnum > MAX_PADDING) {
1122 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
1123 ERR_R_INTERNAL_ERROR);
1126 /* we need to add 'padnum' padding bytes of value padval */
1127 padval = (unsigned char)(padnum - 1);
1128 for (loop = reclen[ctr]; loop < reclen[ctr] + padnum; loop++)
1129 recs[ctr].input[loop] = padval;
1130 reclen[ctr] += padnum;
1131 recs[ctr].length += padnum;
1135 if (reclen[ctr] == 0 || reclen[ctr] % bs != 0) {
1136 /* Publicly invalid */
1142 unsigned char *data[SSL_MAX_PIPELINES];
1144 /* Set the output buffers */
1145 for (ctr = 0; ctr < n_recs; ctr++) {
1146 data[ctr] = recs[ctr].data;
1148 if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS,
1149 (int)n_recs, data) <= 0) {
1150 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
1151 SSL_R_PIPELINE_FAILURE);
1154 /* Set the input buffers */
1155 for (ctr = 0; ctr < n_recs; ctr++) {
1156 data[ctr] = recs[ctr].input;
1158 if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_BUFS,
1159 (int)n_recs, data) <= 0
1160 || EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_LENS,
1161 (int)n_recs, reclen) <= 0) {
1162 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
1163 SSL_R_PIPELINE_FAILURE);
1168 if (!SSL_IS_DTLS(s) && tlstree_enc) {
1170 int decrement_seq = 0;
1173 * When sending, seq is incremented after MAC calculation.
1174 * So if we are in ETM mode, we use seq 'as is' in the ctrl-function.
1175 * Otherwise we have to decrease it in the implementation
1177 if (sending && !SSL_WRITE_ETM(s))
1180 seq = sending ? RECORD_LAYER_get_write_sequence(&s->rlayer)
1181 : RECORD_LAYER_get_read_sequence(&s->rlayer);
1182 if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_TLSTREE, decrement_seq, seq) <= 0) {
1183 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
1184 ERR_R_INTERNAL_ERROR);
1192 /* Provided cipher - we do not support pipelining on this path */
1194 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
1195 ERR_R_INTERNAL_ERROR);
1199 if (!EVP_CipherUpdate(ds, recs[0].data, &outlen, recs[0].input,
1200 (unsigned int)reclen[0]))
1202 recs[0].length = outlen;
1205 * The length returned from EVP_CipherUpdate above is the actual
1206 * payload length. We need to adjust the data/input ptr to skip over
1210 if (EVP_CIPHER_mode(enc) == EVP_CIPH_GCM_MODE) {
1211 recs[0].data += EVP_GCM_TLS_EXPLICIT_IV_LEN;
1212 recs[0].input += EVP_GCM_TLS_EXPLICIT_IV_LEN;
1213 } else if (EVP_CIPHER_mode(enc) == EVP_CIPH_CCM_MODE) {
1214 recs[0].data += EVP_CCM_TLS_EXPLICIT_IV_LEN;
1215 recs[0].input += EVP_CCM_TLS_EXPLICIT_IV_LEN;
1216 } else if (bs != 1 && SSL_USE_EXPLICIT_IV(s)) {
1218 recs[0].input += bs;
1219 recs[0].orig_len -= bs;
1222 /* Now get a pointer to the MAC (if applicable) */
1224 OSSL_PARAM params[2], *p = params;
1227 macs[0].alloced = 0;
1229 *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_TLS_MAC,
1230 (void **)&macs[0].mac,
1232 *p = OSSL_PARAM_construct_end();
1234 if (!EVP_CIPHER_CTX_get_params(ds, params)) {
1235 /* Shouldn't normally happen */
1236 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_ENC,
1237 ERR_R_INTERNAL_ERROR);
1245 /* TODO(size_t): Convert this call */
1246 tmpr = EVP_Cipher(ds, recs[0].data, recs[0].input,
1247 (unsigned int)reclen[0]);
1248 if ((EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ds))
1249 & EVP_CIPH_FLAG_CUSTOM_CIPHER)
1252 /* AEAD can fail to verify MAC */
1257 /* Adjust the record to remove the explicit IV/MAC/Tag */
1258 if (EVP_CIPHER_mode(enc) == EVP_CIPH_GCM_MODE) {
1259 for (ctr = 0; ctr < n_recs; ctr++) {
1260 recs[ctr].data += EVP_GCM_TLS_EXPLICIT_IV_LEN;
1261 recs[ctr].input += EVP_GCM_TLS_EXPLICIT_IV_LEN;
1262 recs[ctr].length -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
1264 } else if (EVP_CIPHER_mode(enc) == EVP_CIPH_CCM_MODE) {
1265 for (ctr = 0; ctr < n_recs; ctr++) {
1266 recs[ctr].data += EVP_CCM_TLS_EXPLICIT_IV_LEN;
1267 recs[ctr].input += EVP_CCM_TLS_EXPLICIT_IV_LEN;
1268 recs[ctr].length -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
1272 for (ctr = 0; ctr < n_recs; ctr++) {
1273 if (bs != 1 && SSL_USE_EXPLICIT_IV(s)) {
1274 if (recs[ctr].length < bs)
1276 recs[ctr].data += bs;
1277 recs[ctr].input += bs;
1278 recs[ctr].length -= bs;
1279 recs[ctr].orig_len -= bs;
1283 * If using Mac-then-encrypt, then this will succeed but
1284 * with a random MAC if padding is invalid
1286 if (!tls1_cbc_remove_padding_and_mac(&recs[ctr].length,
1289 (macs != NULL) ? &macs[ctr].mac : NULL,
1290 (macs != NULL) ? &macs[ctr].alloced
1294 (EVP_CIPHER_flags(enc)
1295 & EVP_CIPH_FLAG_AEAD_CIPHER) != 0,
1300 for (ctr = 0; ctr < n_recs; ctr++) {
1301 recs[ctr].length -= pad;
1310 int n_ssl3_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int sending)
1312 unsigned char *mac_sec, *seq;
1313 const EVP_MD_CTX *hash;
1314 unsigned char *p, rec_char;
1320 mac_sec = &(ssl->s3.write_mac_secret[0]);
1321 seq = RECORD_LAYER_get_write_sequence(&ssl->rlayer);
1322 hash = ssl->write_hash;
1324 mac_sec = &(ssl->s3.read_mac_secret[0]);
1325 seq = RECORD_LAYER_get_read_sequence(&ssl->rlayer);
1326 hash = ssl->read_hash;
1329 t = EVP_MD_CTX_size(hash);
1333 npad = (48 / md_size) * md_size;
1336 EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
1337 ssl3_cbc_record_digest_supported(hash)) {
1339 * This is a CBC-encrypted record. We must avoid leaking any
1340 * timing-side channel information about how many blocks of data we
1341 * are hashing because that gives an attacker a timing-oracle.
1345 * npad is, at most, 48 bytes and that's with MD5:
1346 * 16 + 48 + 8 (sequence bytes) + 1 + 2 = 75.
1348 * With SHA-1 (the largest hash speced for SSLv3) the hash size
1349 * goes up 4, but npad goes down by 8, resulting in a smaller
1352 unsigned char header[75];
1354 memcpy(header + j, mac_sec, md_size);
1356 memcpy(header + j, ssl3_pad_1, npad);
1358 memcpy(header + j, seq, 8);
1360 header[j++] = rec->type;
1361 header[j++] = (unsigned char)(rec->length >> 8);
1362 header[j++] = (unsigned char)(rec->length & 0xff);
1364 /* Final param == is SSLv3 */
1365 if (ssl3_cbc_digest_record(EVP_MD_CTX_md(hash),
1368 rec->length + md_size, rec->orig_len,
1369 mac_sec, md_size, 1) <= 0)
1372 unsigned int md_size_u;
1373 /* Chop the digest off the end :-) */
1374 EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
1379 rec_char = rec->type;
1381 s2n(rec->length, p);
1382 if (EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
1383 || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
1384 || EVP_DigestUpdate(md_ctx, ssl3_pad_1, npad) <= 0
1385 || EVP_DigestUpdate(md_ctx, seq, 8) <= 0
1386 || EVP_DigestUpdate(md_ctx, &rec_char, 1) <= 0
1387 || EVP_DigestUpdate(md_ctx, md, 2) <= 0
1388 || EVP_DigestUpdate(md_ctx, rec->input, rec->length) <= 0
1389 || EVP_DigestFinal_ex(md_ctx, md, NULL) <= 0
1390 || EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
1391 || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
1392 || EVP_DigestUpdate(md_ctx, ssl3_pad_2, npad) <= 0
1393 || EVP_DigestUpdate(md_ctx, md, md_size) <= 0
1394 || EVP_DigestFinal_ex(md_ctx, md, &md_size_u) <= 0) {
1395 EVP_MD_CTX_free(md_ctx);
1399 EVP_MD_CTX_free(md_ctx);
1402 ssl3_record_sequence_update(seq);
1406 int tls1_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int sending)
1412 EVP_MD_CTX *hmac = NULL, *mac_ctx;
1413 unsigned char header[13];
1414 int stream_mac = sending ? (ssl->mac_flags & SSL_MAC_FLAG_WRITE_MAC_STREAM)
1415 : (ssl->mac_flags & SSL_MAC_FLAG_READ_MAC_STREAM);
1416 int tlstree_mac = sending ? (ssl->mac_flags & SSL_MAC_FLAG_WRITE_MAC_TLSTREE)
1417 : (ssl->mac_flags & SSL_MAC_FLAG_READ_MAC_TLSTREE);
1421 seq = RECORD_LAYER_get_write_sequence(&ssl->rlayer);
1422 hash = ssl->write_hash;
1424 seq = RECORD_LAYER_get_read_sequence(&ssl->rlayer);
1425 hash = ssl->read_hash;
1428 t = EVP_MD_CTX_size(hash);
1429 if (!ossl_assert(t >= 0))
1433 /* I should fix this up TLS TLS TLS TLS TLS XXXXXXXX */
1437 hmac = EVP_MD_CTX_new();
1438 if (hmac == NULL || !EVP_MD_CTX_copy(hmac, hash)) {
1439 EVP_MD_CTX_free(hmac);
1445 if (!SSL_IS_DTLS(ssl) && tlstree_mac && EVP_MD_CTX_ctrl(mac_ctx, EVP_MD_CTRL_TLSTREE, 0, seq) <= 0) {
1446 EVP_MD_CTX_free(hmac);
1450 if (SSL_IS_DTLS(ssl)) {
1451 unsigned char dtlsseq[8], *p = dtlsseq;
1453 s2n(sending ? DTLS_RECORD_LAYER_get_w_epoch(&ssl->rlayer) :
1454 DTLS_RECORD_LAYER_get_r_epoch(&ssl->rlayer), p);
1455 memcpy(p, &seq[2], 6);
1457 memcpy(header, dtlsseq, 8);
1459 memcpy(header, seq, 8);
1461 header[8] = rec->type;
1462 header[9] = (unsigned char)(ssl->version >> 8);
1463 header[10] = (unsigned char)(ssl->version);
1464 header[11] = (unsigned char)(rec->length >> 8);
1465 header[12] = (unsigned char)(rec->length & 0xff);
1467 if (!sending && !SSL_READ_ETM(ssl) &&
1468 EVP_CIPHER_CTX_mode(ssl->enc_read_ctx) == EVP_CIPH_CBC_MODE &&
1469 ssl3_cbc_record_digest_supported(mac_ctx)) {
1470 OSSL_PARAM tls_hmac_params[2], *p = tls_hmac_params;
1472 *p++ = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_TLS_DATA_SIZE,
1474 *p++ = OSSL_PARAM_construct_end();
1476 if (!EVP_PKEY_CTX_set_params(EVP_MD_CTX_pkey_ctx(mac_ctx),
1481 /* TODO(size_t): Convert these calls */
1482 if (EVP_DigestSignUpdate(mac_ctx, header, sizeof(header)) <= 0
1483 || EVP_DigestSignUpdate(mac_ctx, rec->input, rec->length) <= 0
1484 || EVP_DigestSignFinal(mac_ctx, md, &md_size) <= 0) {
1485 EVP_MD_CTX_free(hmac);
1489 EVP_MD_CTX_free(hmac);
1491 OSSL_TRACE_BEGIN(TLS) {
1492 BIO_printf(trc_out, "seq:\n");
1493 BIO_dump_indent(trc_out, seq, 8, 4);
1494 BIO_printf(trc_out, "rec:\n");
1495 BIO_dump_indent(trc_out, rec->data, rec->length, 4);
1496 } OSSL_TRACE_END(TLS);
1498 if (!SSL_IS_DTLS(ssl)) {
1499 for (i = 7; i >= 0; i--) {
1505 OSSL_TRACE_BEGIN(TLS) {
1506 BIO_printf(trc_out, "md:\n");
1507 BIO_dump_indent(trc_out, md, md_size, 4);
1508 } OSSL_TRACE_END(TLS);
1512 int dtls1_process_record(SSL *s, DTLS1_BITMAP *bitmap)
1519 size_t mac_size = 0;
1520 unsigned char md[EVP_MAX_MD_SIZE];
1521 size_t max_plain_length = SSL3_RT_MAX_PLAIN_LENGTH;
1522 SSL_MAC_BUF macbuf = { NULL, 0 };
1525 rr = RECORD_LAYER_get_rrec(&s->rlayer);
1529 * At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length,
1530 * and we have that many bytes in s->packet
1532 rr->input = &(RECORD_LAYER_get_packet(&s->rlayer)[DTLS1_RT_HEADER_LENGTH]);
1535 * ok, we can now read from 's->packet' data into 'rr' rr->input points
1536 * at rr->length bytes, which need to be copied into rr->data by either
1537 * the decryption or by the decompression When the data is 'copied' into
1538 * the rr->data buffer, rr->input will be pointed at the new buffer
1542 * We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length
1543 * bytes of encrypted compressed stuff.
1546 /* check is not needed I believe */
1547 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
1548 SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_DTLS1_PROCESS_RECORD,
1549 SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
1553 /* decrypt in place in 'rr->input' */
1554 rr->data = rr->input;
1555 rr->orig_len = rr->length;
1557 /* TODO(size_t): convert this to do size_t properly */
1558 if (s->read_hash != NULL) {
1559 const EVP_MD *tmpmd = EVP_MD_CTX_md(s->read_hash);
1561 if (tmpmd != NULL) {
1562 imac_size = EVP_MD_size(tmpmd);
1563 if (!ossl_assert(imac_size >= 0 && imac_size <= EVP_MAX_MD_SIZE)) {
1564 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_GET_RECORD,
1568 mac_size = (size_t)imac_size;
1572 if (SSL_READ_ETM(s) && s->read_hash) {
1575 if (rr->orig_len < mac_size) {
1576 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_DTLS1_PROCESS_RECORD,
1577 SSL_R_LENGTH_TOO_SHORT);
1580 rr->length -= mac_size;
1581 mac = rr->data + rr->length;
1582 i = s->method->ssl3_enc->mac(s, rr, md, 0 /* not send */ );
1583 if (i == 0 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0) {
1584 SSLfatal(s, SSL_AD_BAD_RECORD_MAC, SSL_F_DTLS1_PROCESS_RECORD,
1585 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
1589 * We've handled the mac now - there is no MAC inside the encrypted
1595 enc_err = s->method->ssl3_enc->enc(s, rr, 1, 0, &macbuf, mac_size);
1599 * 0: if the record is publicly invalid, or an internal error, or AEAD
1600 * decryption failed, or ETM decryption failed.
1601 * 1: Success or MTE decryption failed (MAC will be randomised)
1604 if (ossl_statem_in_error(s)) {
1605 /* SSLfatal() got called */
1608 /* For DTLS we simply ignore bad packets. */
1610 RECORD_LAYER_reset_packet_length(&s->rlayer);
1613 OSSL_TRACE_BEGIN(TLS) {
1614 BIO_printf(trc_out, "dec %zd\n", rr->length);
1615 BIO_dump_indent(trc_out, rr->data, rr->length, 4);
1616 } OSSL_TRACE_END(TLS);
1618 /* r->length is now the compressed data plus mac */
1619 if ((sess != NULL) && !SSL_READ_ETM(s) &&
1620 (s->enc_read_ctx != NULL) && (EVP_MD_CTX_md(s->read_hash) != NULL)) {
1621 /* s->read_hash != NULL => mac_size != -1 */
1623 i = s->method->ssl3_enc->mac(s, rr, md, 0 /* not send */ );
1624 if (i == 0 || macbuf.mac == NULL
1625 || CRYPTO_memcmp(md, macbuf.mac, mac_size) != 0)
1627 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)
1632 /* decryption failed, silently discard message */
1634 RECORD_LAYER_reset_packet_length(&s->rlayer);
1638 /* r->length is now just compressed */
1639 if (s->expand != NULL) {
1640 if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {
1641 SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_DTLS1_PROCESS_RECORD,
1642 SSL_R_COMPRESSED_LENGTH_TOO_LONG);
1645 if (!ssl3_do_uncompress(s, rr)) {
1646 SSLfatal(s, SSL_AD_DECOMPRESSION_FAILURE,
1647 SSL_F_DTLS1_PROCESS_RECORD, SSL_R_BAD_DECOMPRESSION);
1652 /* use current Max Fragment Length setting if applicable */
1653 if (s->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(s->session))
1654 max_plain_length = GET_MAX_FRAGMENT_LENGTH(s->session);
1656 /* send overflow if the plaintext is too long now it has passed MAC */
1657 if (rr->length > max_plain_length) {
1658 SSLfatal(s, SSL_AD_RECORD_OVERFLOW, SSL_F_DTLS1_PROCESS_RECORD,
1659 SSL_R_DATA_LENGTH_TOO_LONG);
1665 * So at this point the following is true
1666 * ssl->s3.rrec.type is the type of record
1667 * ssl->s3.rrec.length == number of bytes in record
1668 * ssl->s3.rrec.off == offset to first valid byte
1669 * ssl->s3.rrec.data == where to take bytes from, increment
1673 /* we have pulled in a full packet so zero things */
1674 RECORD_LAYER_reset_packet_length(&s->rlayer);
1676 /* Mark receipt of record. */
1677 dtls1_record_bitmap_update(s, bitmap);
1682 OPENSSL_free(macbuf.mac);
1687 * Retrieve a buffered record that belongs to the current epoch, i.e. processed
1689 #define dtls1_get_processed_record(s) \
1690 dtls1_retrieve_buffered_record((s), \
1691 &(DTLS_RECORD_LAYER_get_processed_rcds(&s->rlayer)))
1694 * Call this to get a new input record.
1695 * It will return <= 0 if more data is needed, normally due to an error
1696 * or non-blocking IO.
1697 * When it finishes, one packet has been decoded and can be found in
1698 * ssl->s3.rrec.type - is the type of record
1699 * ssl->s3.rrec.data - data
1700 * ssl->s3.rrec.length - number of bytes
1702 /* used only by dtls1_read_bytes */
1703 int dtls1_get_record(SSL *s)
1705 int ssl_major, ssl_minor;
1709 unsigned char *p = NULL;
1710 unsigned short version;
1711 DTLS1_BITMAP *bitmap;
1712 unsigned int is_next_epoch;
1714 rr = RECORD_LAYER_get_rrec(&s->rlayer);
1718 * The epoch may have changed. If so, process all the pending records.
1719 * This is a non-blocking operation.
1721 if (!dtls1_process_buffered_records(s)) {
1722 /* SSLfatal() already called */
1726 /* if we're renegotiating, then there may be buffered records */
1727 if (dtls1_get_processed_record(s))
1730 /* get something from the wire */
1732 /* check if we have the header */
1733 if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||
1734 (RECORD_LAYER_get_packet_length(&s->rlayer) < DTLS1_RT_HEADER_LENGTH)) {
1735 rret = ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH,
1736 SSL3_BUFFER_get_len(&s->rlayer.rbuf), 0, 1, &n);
1737 /* read timeout is handled by dtls1_read_bytes */
1739 /* SSLfatal() already called if appropriate */
1740 return rret; /* error or non-blocking */
1743 /* this packet contained a partial record, dump it */
1744 if (RECORD_LAYER_get_packet_length(&s->rlayer) !=
1745 DTLS1_RT_HEADER_LENGTH) {
1746 RECORD_LAYER_reset_packet_length(&s->rlayer);
1750 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);
1752 p = RECORD_LAYER_get_packet(&s->rlayer);
1754 if (s->msg_callback)
1755 s->msg_callback(0, 0, SSL3_RT_HEADER, p, DTLS1_RT_HEADER_LENGTH,
1756 s, s->msg_callback_arg);
1758 /* Pull apart the header into the DTLS1_RECORD */
1762 version = (ssl_major << 8) | ssl_minor;
1764 /* sequence number is 64 bits, with top 2 bytes = epoch */
1767 memcpy(&(RECORD_LAYER_get_read_sequence(&s->rlayer)[2]), p, 6);
1774 * Lets check the version. We tolerate alerts that don't have the exact
1775 * version number (e.g. because of protocol version errors)
1777 if (!s->first_packet && rr->type != SSL3_RT_ALERT) {
1778 if (version != s->version) {
1779 /* unexpected version, silently discard */
1782 RECORD_LAYER_reset_packet_length(&s->rlayer);
1787 if ((version & 0xff00) != (s->version & 0xff00)) {
1788 /* wrong version, silently discard record */
1791 RECORD_LAYER_reset_packet_length(&s->rlayer);
1795 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
1796 /* record too long, silently discard it */
1799 RECORD_LAYER_reset_packet_length(&s->rlayer);
1803 /* If received packet overflows own-client Max Fragment Length setting */
1804 if (s->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(s->session)
1805 && rr->length > GET_MAX_FRAGMENT_LENGTH(s->session) + SSL3_RT_MAX_ENCRYPTED_OVERHEAD) {
1806 /* record too long, silently discard it */
1809 RECORD_LAYER_reset_packet_length(&s->rlayer);
1813 /* now s->rlayer.rstate == SSL_ST_READ_BODY */
1816 /* s->rlayer.rstate == SSL_ST_READ_BODY, get and decode the data */
1819 RECORD_LAYER_get_packet_length(&s->rlayer) - DTLS1_RT_HEADER_LENGTH) {
1820 /* now s->packet_length == DTLS1_RT_HEADER_LENGTH */
1822 rret = ssl3_read_n(s, more, more, 1, 1, &n);
1823 /* this packet contained a partial record, dump it */
1824 if (rret <= 0 || n != more) {
1825 if (ossl_statem_in_error(s)) {
1826 /* ssl3_read_n() called SSLfatal() */
1831 RECORD_LAYER_reset_packet_length(&s->rlayer);
1836 * now n == rr->length, and s->packet_length ==
1837 * DTLS1_RT_HEADER_LENGTH + rr->length
1840 /* set state for later operations */
1841 RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);
1843 /* match epochs. NULL means the packet is dropped on the floor */
1844 bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);
1845 if (bitmap == NULL) {
1847 RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
1848 goto again; /* get another record */
1850 #ifndef OPENSSL_NO_SCTP
1851 /* Only do replay check if no SCTP bio */
1852 if (!BIO_dgram_is_sctp(SSL_get_rbio(s))) {
1854 /* Check whether this is a repeat, or aged record. */
1856 * TODO: Does it make sense to have replay protection in epoch 0 where
1857 * we have no integrity negotiated yet?
1859 if (!dtls1_record_replay_check(s, bitmap)) {
1862 RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
1863 goto again; /* get another record */
1865 #ifndef OPENSSL_NO_SCTP
1869 /* just read a 0 length packet */
1870 if (rr->length == 0) {
1876 * If this record is from the next epoch (either HM or ALERT), and a
1877 * handshake is currently in progress, buffer it since it cannot be
1878 * processed at this time.
1880 if (is_next_epoch) {
1881 if ((SSL_in_init(s) || ossl_statem_get_in_handshake(s))) {
1882 if (dtls1_buffer_record (s,
1883 &(DTLS_RECORD_LAYER_get_unprocessed_rcds(&s->rlayer)),
1885 /* SSLfatal() already called */
1891 RECORD_LAYER_reset_packet_length(&s->rlayer);
1895 if (!dtls1_process_record(s, bitmap)) {
1896 if (ossl_statem_in_error(s)) {
1897 /* dtls1_process_record() called SSLfatal */
1902 RECORD_LAYER_reset_packet_length(&s->rlayer); /* dump this record */
1903 goto again; /* get another record */
1910 int dtls_buffer_listen_record(SSL *s, size_t len, unsigned char *seq, size_t off)
1914 rr = RECORD_LAYER_get_rrec(&s->rlayer);
1915 memset(rr, 0, sizeof(SSL3_RECORD));
1918 rr->type = SSL3_RT_HANDSHAKE;
1919 memcpy(rr->seq_num, seq, sizeof(rr->seq_num));
1922 s->rlayer.packet = RECORD_LAYER_get_rbuf(&s->rlayer)->buf;
1923 s->rlayer.packet_length = DTLS1_RT_HEADER_LENGTH + len;
1924 rr->data = s->rlayer.packet + DTLS1_RT_HEADER_LENGTH;
1926 if (dtls1_buffer_record(s, &(s->rlayer.d->processed_rcds),
1927 SSL3_RECORD_get_seq_num(s->rlayer.rrec)) <= 0) {
1928 /* SSLfatal() already called */