Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
Hanno Becker | f1a3828 | 2020-02-05 16:14:29 +0000 | [diff] [blame] | 2 | * Generic SSL/TLS messaging layer functions |
| 3 | * (record layer + retransmission state machine) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 5 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: Apache-2.0 |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | * not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 19 | */ |
| 20 | /* |
| 21 | * The SSL 3.0 specification was drafted by Netscape in 1996, |
| 22 | * and became an IETF standard in 1999. |
| 23 | * |
| 24 | * http://wp.netscape.com/eng/ssl3/ |
| 25 | * http://www.ietf.org/rfc/rfc2246.txt |
| 26 | * http://www.ietf.org/rfc/rfc4346.txt |
| 27 | */ |
| 28 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 29 | #include "common.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 30 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 31 | #if defined(MBEDTLS_SSL_TLS_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 32 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 33 | #include "mbedtls/platform.h" |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 34 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 35 | #include "mbedtls/ssl.h" |
Manuel Pégourié-Gonnard | 5e94dde | 2015-05-26 11:57:05 +0200 | [diff] [blame] | 36 | #include "mbedtls/ssl_internal.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 37 | #include "mbedtls/debug.h" |
| 38 | #include "mbedtls/error.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 39 | #include "mbedtls/platform_util.h" |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 40 | #include "mbedtls/version.h" |
Gabor Mezei | c0ae1cf | 2021-10-20 12:09:35 +0200 | [diff] [blame] | 41 | #include "constant_time_internal.h" |
Gabor Mezei | e24dea8 | 2021-10-19 12:22:25 +0200 | [diff] [blame] | 42 | #include "mbedtls/constant_time.h" |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 43 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 44 | #include <string.h> |
| 45 | |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 46 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 47 | #include "mbedtls/psa_util.h" |
| 48 | #include "psa/crypto.h" |
| 49 | #endif |
| 50 | |
Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 51 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 52 | #include "mbedtls/oid.h" |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 53 | #endif |
| 54 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 55 | static uint32_t ssl_get_hs_total_len(mbedtls_ssl_context const *ssl); |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 56 | |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 57 | /* |
| 58 | * Start a timer. |
| 59 | * Passing millisecs = 0 cancels a running timer. |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 60 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 61 | void mbedtls_ssl_set_timer(mbedtls_ssl_context *ssl, uint32_t millisecs) |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 62 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 63 | if (ssl->f_set_timer == NULL) { |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 64 | return; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 65 | } |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 66 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 67 | MBEDTLS_SSL_DEBUG_MSG(3, ("set_timer to %d ms", (int) millisecs)); |
| 68 | ssl->f_set_timer(ssl->p_timer, millisecs / 4, millisecs); |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /* |
| 72 | * Return -1 is timer is expired, 0 if it isn't. |
| 73 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 74 | int mbedtls_ssl_check_timer(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 75 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 76 | if (ssl->f_get_timer == NULL) { |
| 77 | return 0; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 78 | } |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 79 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 80 | if (ssl->f_get_timer(ssl->p_timer) == 2) { |
| 81 | MBEDTLS_SSL_DEBUG_MSG(3, ("timer expired")); |
| 82 | return -1; |
| 83 | } |
| 84 | |
| 85 | return 0; |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 86 | } |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 87 | |
Hanno Becker | cfe4579 | 2019-07-03 16:13:00 +0100 | [diff] [blame] | 88 | #if defined(MBEDTLS_SSL_RECORD_CHECKING) |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 89 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 90 | static int ssl_parse_record_header(mbedtls_ssl_context const *ssl, |
| 91 | unsigned char *buf, |
| 92 | size_t len, |
| 93 | mbedtls_record *rec); |
Hanno Becker | 5422981 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 94 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 95 | int mbedtls_ssl_check_record(mbedtls_ssl_context const *ssl, |
| 96 | unsigned char *buf, |
| 97 | size_t buflen) |
Hanno Becker | cfe4579 | 2019-07-03 16:13:00 +0100 | [diff] [blame] | 98 | { |
Hanno Becker | 5422981 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 99 | int ret = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 100 | MBEDTLS_SSL_DEBUG_MSG(1, ("=> mbedtls_ssl_check_record")); |
| 101 | MBEDTLS_SSL_DEBUG_BUF(3, "record buffer", buf, buflen); |
Hanno Becker | 5422981 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 102 | |
| 103 | /* We don't support record checking in TLS because |
| 104 | * (a) there doesn't seem to be a usecase for it, and |
| 105 | * (b) In SSLv3 and TLS 1.0, CBC record decryption has state |
| 106 | * and we'd need to backup the transform here. |
| 107 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 108 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM) { |
Hanno Becker | 5422981 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 109 | ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 110 | goto exit; |
| 111 | } |
| 112 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 113 | else { |
irwir | 734f0cf | 2019-09-26 21:03:24 +0300 | [diff] [blame] | 114 | mbedtls_record rec; |
| 115 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 116 | ret = ssl_parse_record_header(ssl, buf, buflen, &rec); |
| 117 | if (ret != 0) { |
| 118 | MBEDTLS_SSL_DEBUG_RET(3, "ssl_parse_record_header", ret); |
Hanno Becker | 5422981 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 119 | goto exit; |
| 120 | } |
| 121 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 122 | if (ssl->transform_in != NULL) { |
| 123 | ret = mbedtls_ssl_decrypt_buf(ssl, ssl->transform_in, &rec); |
| 124 | if (ret != 0) { |
| 125 | MBEDTLS_SSL_DEBUG_RET(3, "mbedtls_ssl_decrypt_buf", ret); |
Hanno Becker | 5422981 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 126 | goto exit; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 131 | |
| 132 | exit: |
| 133 | /* On success, we have decrypted the buffer in-place, so make |
| 134 | * sure we don't leak any plaintext data. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 135 | mbedtls_platform_zeroize(buf, buflen); |
Hanno Becker | 5422981 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 136 | |
| 137 | /* For the purpose of this API, treat messages with unexpected CID |
| 138 | * as well as such from future epochs as unexpected. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 139 | if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID || |
| 140 | ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) { |
Hanno Becker | 5422981 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 141 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
| 142 | } |
| 143 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 144 | MBEDTLS_SSL_DEBUG_MSG(1, ("<= mbedtls_ssl_check_record")); |
| 145 | return ret; |
Hanno Becker | cfe4579 | 2019-07-03 16:13:00 +0100 | [diff] [blame] | 146 | } |
| 147 | #endif /* MBEDTLS_SSL_RECORD_CHECKING */ |
| 148 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 149 | #define SSL_DONT_FORCE_FLUSH 0 |
| 150 | #define SSL_FORCE_FLUSH 1 |
| 151 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 152 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 153 | |
Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 154 | /* Forward declarations for functions related to message buffering. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 155 | static void ssl_buffering_free_slot(mbedtls_ssl_context *ssl, |
| 156 | uint8_t slot); |
| 157 | static void ssl_free_buffered_record(mbedtls_ssl_context *ssl); |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 158 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 159 | static int ssl_load_buffered_message(mbedtls_ssl_context *ssl); |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 160 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 161 | static int ssl_load_buffered_record(mbedtls_ssl_context *ssl); |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 162 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 163 | static int ssl_buffer_message(mbedtls_ssl_context *ssl); |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 164 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 165 | static int ssl_buffer_future_record(mbedtls_ssl_context *ssl, |
| 166 | mbedtls_record const *rec); |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 167 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 168 | static int ssl_next_record_is_in_datagram(mbedtls_ssl_context *ssl); |
Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 169 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 170 | static size_t ssl_get_maximum_datagram_size(mbedtls_ssl_context const *ssl) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 171 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 172 | size_t mtu = mbedtls_ssl_get_current_mtu(ssl); |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 173 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 174 | size_t out_buf_len = ssl->out_buf_len; |
| 175 | #else |
| 176 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 177 | #endif |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 178 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 179 | if (mtu != 0 && mtu < out_buf_len) { |
| 180 | return mtu; |
| 181 | } |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 182 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 183 | return out_buf_len; |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 186 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 187 | static int ssl_get_remaining_space_in_datagram(mbedtls_ssl_context const *ssl) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 188 | { |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 189 | size_t const bytes_written = ssl->out_left; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 190 | size_t const mtu = ssl_get_maximum_datagram_size(ssl); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 191 | |
| 192 | /* Double-check that the write-index hasn't gone |
| 193 | * past what we can transmit in a single datagram. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 194 | if (bytes_written > mtu) { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 195 | /* Should never happen... */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 196 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 197 | } |
| 198 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 199 | return (int) (mtu - bytes_written); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 200 | } |
| 201 | |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 202 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 203 | static int ssl_get_remaining_payload_in_datagram(mbedtls_ssl_context const *ssl) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 204 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 205 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 206 | size_t remaining, expansion; |
Andrzej Kurek | 748face | 2018-10-11 07:20:19 -0400 | [diff] [blame] | 207 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 208 | |
| 209 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 210 | const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 211 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 212 | if (max_len > mfl) { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 213 | max_len = mfl; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 214 | } |
Hanno Becker | f4b010e | 2018-08-24 10:47:29 +0100 | [diff] [blame] | 215 | |
| 216 | /* By the standard (RFC 6066 Sect. 4), the MFL extension |
| 217 | * only limits the maximum record payload size, so in theory |
| 218 | * we would be allowed to pack multiple records of payload size |
| 219 | * MFL into a single datagram. However, this would mean that there's |
| 220 | * no way to explicitly communicate MTU restrictions to the peer. |
| 221 | * |
| 222 | * The following reduction of max_len makes sure that we never |
| 223 | * write datagrams larger than MFL + Record Expansion Overhead. |
| 224 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 225 | if (max_len <= ssl->out_left) { |
| 226 | return 0; |
| 227 | } |
Hanno Becker | f4b010e | 2018-08-24 10:47:29 +0100 | [diff] [blame] | 228 | |
| 229 | max_len -= ssl->out_left; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 230 | #endif |
| 231 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 232 | ret = ssl_get_remaining_space_in_datagram(ssl); |
| 233 | if (ret < 0) { |
| 234 | return ret; |
| 235 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 236 | remaining = (size_t) ret; |
| 237 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 238 | ret = mbedtls_ssl_get_record_expansion(ssl); |
| 239 | if (ret < 0) { |
| 240 | return ret; |
| 241 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 242 | expansion = (size_t) ret; |
| 243 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 244 | if (remaining <= expansion) { |
| 245 | return 0; |
| 246 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 247 | |
| 248 | remaining -= expansion; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 249 | if (remaining >= max_len) { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 250 | remaining = max_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 251 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 252 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 253 | return (int) remaining; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 254 | } |
| 255 | |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 256 | /* |
| 257 | * Double the retransmit timeout value, within the allowed range, |
| 258 | * returning -1 if the maximum value has already been reached. |
| 259 | */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 260 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 261 | static int ssl_double_retransmit_timeout(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 262 | { |
| 263 | uint32_t new_timeout; |
| 264 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 265 | if (ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max) { |
| 266 | return -1; |
| 267 | } |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 268 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 269 | /* Implement the final paragraph of RFC 6347 section 4.1.1.1 |
| 270 | * in the following way: after the initial transmission and a first |
| 271 | * retransmission, back off to a temporary estimated MTU of 508 bytes. |
| 272 | * This value is guaranteed to be deliverable (if not guaranteed to be |
| 273 | * delivered) of any compliant IPv4 (and IPv6) network, and should work |
| 274 | * on most non-IP stacks too. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 275 | if (ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min) { |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 276 | ssl->handshake->mtu = 508; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 277 | MBEDTLS_SSL_DEBUG_MSG(2, ("mtu autoreduction to %d bytes", ssl->handshake->mtu)); |
Andrzej Kurek | 6290dae | 2018-10-05 08:06:01 -0400 | [diff] [blame] | 278 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 279 | |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 280 | new_timeout = 2 * ssl->handshake->retransmit_timeout; |
| 281 | |
| 282 | /* Avoid arithmetic overflow and range overflow */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 283 | if (new_timeout < ssl->handshake->retransmit_timeout || |
| 284 | new_timeout > ssl->conf->hs_timeout_max) { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 285 | new_timeout = ssl->conf->hs_timeout_max; |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | ssl->handshake->retransmit_timeout = new_timeout; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 289 | MBEDTLS_SSL_DEBUG_MSG(3, ("update timeout value to %lu millisecs", |
| 290 | (unsigned long) ssl->handshake->retransmit_timeout)); |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 291 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 292 | return 0; |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 293 | } |
| 294 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 295 | static void ssl_reset_retransmit_timeout(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 296 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 297 | ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 298 | MBEDTLS_SSL_DEBUG_MSG(3, ("update timeout value to %lu millisecs", |
| 299 | (unsigned long) ssl->handshake->retransmit_timeout)); |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 300 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 301 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 302 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 303 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 304 | int (*mbedtls_ssl_hw_record_init)(mbedtls_ssl_context *ssl, |
| 305 | const unsigned char *key_enc, const unsigned char *key_dec, |
| 306 | size_t keylen, |
| 307 | const unsigned char *iv_enc, const unsigned char *iv_dec, |
| 308 | size_t ivlen, |
| 309 | const unsigned char *mac_enc, const unsigned char *mac_dec, |
| 310 | size_t maclen) = NULL; |
| 311 | int (*mbedtls_ssl_hw_record_activate)(mbedtls_ssl_context *ssl, int direction) = NULL; |
| 312 | int (*mbedtls_ssl_hw_record_reset)(mbedtls_ssl_context *ssl) = NULL; |
| 313 | int (*mbedtls_ssl_hw_record_write)(mbedtls_ssl_context *ssl) = NULL; |
| 314 | int (*mbedtls_ssl_hw_record_read)(mbedtls_ssl_context *ssl) = NULL; |
| 315 | int (*mbedtls_ssl_hw_record_finish)(mbedtls_ssl_context *ssl) = NULL; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 316 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 317 | |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 318 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 319 | * Encryption/decryption functions |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 320 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 321 | |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 322 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) || \ |
| 323 | defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
Hanno Becker | 1399692 | 2020-05-28 16:15:19 +0100 | [diff] [blame] | 324 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 325 | static size_t ssl_compute_padding_length(size_t len, |
| 326 | size_t granularity) |
Hanno Becker | 1399692 | 2020-05-28 16:15:19 +0100 | [diff] [blame] | 327 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 328 | return (granularity - (len + 1) % granularity) % granularity; |
Hanno Becker | 1399692 | 2020-05-28 16:15:19 +0100 | [diff] [blame] | 329 | } |
| 330 | |
Hanno Becker | 581bc1b | 2020-05-04 12:20:03 +0100 | [diff] [blame] | 331 | /* This functions transforms a (D)TLS plaintext fragment and a record content |
| 332 | * type into an instance of the (D)TLSInnerPlaintext structure. This is used |
| 333 | * in DTLS 1.2 + CID and within TLS 1.3 to allow flexible padding and to protect |
| 334 | * a record's content type. |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 335 | * |
| 336 | * struct { |
| 337 | * opaque content[DTLSPlaintext.length]; |
| 338 | * ContentType real_type; |
| 339 | * uint8 zeros[length_of_padding]; |
Hanno Becker | 581bc1b | 2020-05-04 12:20:03 +0100 | [diff] [blame] | 340 | * } (D)TLSInnerPlaintext; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 341 | * |
| 342 | * Input: |
| 343 | * - `content`: The beginning of the buffer holding the |
| 344 | * plaintext to be wrapped. |
| 345 | * - `*content_size`: The length of the plaintext in Bytes. |
| 346 | * - `max_len`: The number of Bytes available starting from |
| 347 | * `content`. This must be `>= *content_size`. |
| 348 | * - `rec_type`: The desired record content type. |
| 349 | * |
| 350 | * Output: |
Hanno Becker | 581bc1b | 2020-05-04 12:20:03 +0100 | [diff] [blame] | 351 | * - `content`: The beginning of the resulting (D)TLSInnerPlaintext structure. |
| 352 | * - `*content_size`: The length of the resulting (D)TLSInnerPlaintext structure. |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 353 | * |
| 354 | * Returns: |
| 355 | * - `0` on success. |
| 356 | * - A negative error code if `max_len` didn't offer enough space |
| 357 | * for the expansion. |
| 358 | */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 359 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 360 | static int ssl_build_inner_plaintext(unsigned char *content, |
| 361 | size_t *content_size, |
| 362 | size_t remaining, |
| 363 | uint8_t rec_type, |
| 364 | size_t pad) |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 365 | { |
| 366 | size_t len = *content_size; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 367 | |
| 368 | /* Write real content type */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 369 | if (remaining == 0) { |
| 370 | return -1; |
| 371 | } |
| 372 | content[len] = rec_type; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 373 | len++; |
| 374 | remaining--; |
| 375 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 376 | if (remaining < pad) { |
| 377 | return -1; |
| 378 | } |
| 379 | memset(content + len, 0, pad); |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 380 | len += pad; |
| 381 | remaining -= pad; |
| 382 | |
| 383 | *content_size = len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 384 | return 0; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 385 | } |
| 386 | |
Hanno Becker | 581bc1b | 2020-05-04 12:20:03 +0100 | [diff] [blame] | 387 | /* This function parses a (D)TLSInnerPlaintext structure. |
| 388 | * See ssl_build_inner_plaintext() for details. */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 389 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 390 | static int ssl_parse_inner_plaintext(unsigned char const *content, |
| 391 | size_t *content_size, |
| 392 | uint8_t *rec_type) |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 393 | { |
| 394 | size_t remaining = *content_size; |
| 395 | |
| 396 | /* Determine length of padding by skipping zeroes from the back. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 397 | do { |
| 398 | if (remaining == 0) { |
| 399 | return -1; |
| 400 | } |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 401 | remaining--; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 402 | } while (content[remaining] == 0); |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 403 | |
| 404 | *content_size = remaining; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 405 | *rec_type = content[remaining]; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 406 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 407 | return 0; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 408 | } |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 409 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID || |
| 410 | MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 411 | |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 412 | /* `add_data` must have size 13 Bytes if the CID extension is disabled, |
Hanno Becker | c4a190b | 2019-05-08 18:15:21 +0100 | [diff] [blame] | 413 | * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 414 | static void ssl_extract_add_data_from_record(unsigned char *add_data, |
| 415 | size_t *add_data_len, |
| 416 | mbedtls_record *rec, |
| 417 | unsigned minor_ver) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 418 | { |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 419 | /* Quoting RFC 5246 (TLS 1.2): |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 420 | * |
| 421 | * additional_data = seq_num + TLSCompressed.type + |
| 422 | * TLSCompressed.version + TLSCompressed.length; |
| 423 | * |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 424 | * For the CID extension, this is extended as follows |
| 425 | * (quoting draft-ietf-tls-dtls-connection-id-05, |
| 426 | * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05): |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 427 | * |
| 428 | * additional_data = seq_num + DTLSPlaintext.type + |
| 429 | * DTLSPlaintext.version + |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 430 | * cid + |
| 431 | * cid_length + |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 432 | * length_of_DTLSInnerPlaintext; |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 433 | * |
| 434 | * For TLS 1.3, the record sequence number is dropped from the AAD |
| 435 | * and encoded within the nonce of the AEAD operation instead. |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 436 | */ |
| 437 | |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 438 | unsigned char *cur = add_data; |
| 439 | |
David Horstmann | 197b240 | 2022-10-26 18:06:31 +0100 | [diff] [blame] | 440 | int is_tls13 = 0; |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 441 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 442 | if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_4) { |
David Horstmann | 197b240 | 2022-10-26 18:06:31 +0100 | [diff] [blame] | 443 | is_tls13 = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 444 | } |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 445 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 446 | if (!is_tls13) { |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 447 | ((void) minor_ver); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 448 | memcpy(cur, rec->ctr, sizeof(rec->ctr)); |
| 449 | cur += sizeof(rec->ctr); |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | *cur = rec->type; |
| 453 | cur++; |
| 454 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 455 | memcpy(cur, rec->ver, sizeof(rec->ver)); |
| 456 | cur += sizeof(rec->ver); |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 457 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 458 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 459 | if (rec->cid_len != 0) { |
| 460 | memcpy(cur, rec->cid, rec->cid_len); |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 461 | cur += rec->cid_len; |
| 462 | |
| 463 | *cur = rec->cid_len; |
| 464 | cur++; |
| 465 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 466 | MBEDTLS_PUT_UINT16_BE(rec->data_len, cur, 0); |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 467 | cur += 2; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 468 | } else |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 469 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 95e4bbc | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 470 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 471 | MBEDTLS_PUT_UINT16_BE(rec->data_len, cur, 0); |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 472 | cur += 2; |
Hanno Becker | 95e4bbc | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 473 | } |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 474 | |
| 475 | *add_data_len = cur - add_data; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Hanno Becker | 9d062f9 | 2020-02-07 10:26:36 +0000 | [diff] [blame] | 478 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 479 | |
| 480 | #define SSL3_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */ |
| 481 | |
| 482 | /* |
| 483 | * SSLv3.0 MAC functions |
| 484 | */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 485 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 486 | static int ssl_mac(mbedtls_md_context_t *md_ctx, |
| 487 | const unsigned char *secret, |
| 488 | const unsigned char *buf, size_t len, |
| 489 | const unsigned char *ctr, int type, |
| 490 | unsigned char out[SSL3_MAC_MAX_BYTES]) |
Hanno Becker | 9d062f9 | 2020-02-07 10:26:36 +0000 | [diff] [blame] | 491 | { |
| 492 | unsigned char header[11]; |
| 493 | unsigned char padding[48]; |
| 494 | int padlen; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 495 | int md_size = mbedtls_md_get_size(md_ctx->md_info); |
| 496 | int md_type = mbedtls_md_get_type(md_ctx->md_info); |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 497 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 9d062f9 | 2020-02-07 10:26:36 +0000 | [diff] [blame] | 498 | |
| 499 | /* Only MD5 and SHA-1 supported */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 500 | if (md_type == MBEDTLS_MD_MD5) { |
Hanno Becker | 9d062f9 | 2020-02-07 10:26:36 +0000 | [diff] [blame] | 501 | padlen = 48; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 502 | } else { |
Hanno Becker | 9d062f9 | 2020-02-07 10:26:36 +0000 | [diff] [blame] | 503 | padlen = 40; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 504 | } |
Hanno Becker | 9d062f9 | 2020-02-07 10:26:36 +0000 | [diff] [blame] | 505 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 506 | memcpy(header, ctr, 8); |
Joe Subbiani | a651e6f | 2021-08-23 11:35:25 +0100 | [diff] [blame] | 507 | header[8] = (unsigned char) type; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 508 | MBEDTLS_PUT_UINT16_BE(len, header, 9); |
Hanno Becker | 9d062f9 | 2020-02-07 10:26:36 +0000 | [diff] [blame] | 509 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 510 | memset(padding, 0x36, padlen); |
| 511 | ret = mbedtls_md_starts(md_ctx); |
| 512 | if (ret != 0) { |
| 513 | return ret; |
| 514 | } |
| 515 | ret = mbedtls_md_update(md_ctx, secret, md_size); |
| 516 | if (ret != 0) { |
| 517 | return ret; |
| 518 | } |
| 519 | ret = mbedtls_md_update(md_ctx, padding, padlen); |
| 520 | if (ret != 0) { |
| 521 | return ret; |
| 522 | } |
| 523 | ret = mbedtls_md_update(md_ctx, header, 11); |
| 524 | if (ret != 0) { |
| 525 | return ret; |
| 526 | } |
| 527 | ret = mbedtls_md_update(md_ctx, buf, len); |
| 528 | if (ret != 0) { |
| 529 | return ret; |
| 530 | } |
| 531 | ret = mbedtls_md_finish(md_ctx, out); |
| 532 | if (ret != 0) { |
| 533 | return ret; |
| 534 | } |
Hanno Becker | 9d062f9 | 2020-02-07 10:26:36 +0000 | [diff] [blame] | 535 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 536 | memset(padding, 0x5C, padlen); |
| 537 | ret = mbedtls_md_starts(md_ctx); |
| 538 | if (ret != 0) { |
| 539 | return ret; |
| 540 | } |
| 541 | ret = mbedtls_md_update(md_ctx, secret, md_size); |
| 542 | if (ret != 0) { |
| 543 | return ret; |
| 544 | } |
| 545 | ret = mbedtls_md_update(md_ctx, padding, padlen); |
| 546 | if (ret != 0) { |
| 547 | return ret; |
| 548 | } |
| 549 | ret = mbedtls_md_update(md_ctx, out, md_size); |
| 550 | if (ret != 0) { |
| 551 | return ret; |
| 552 | } |
| 553 | ret = mbedtls_md_finish(md_ctx, out); |
| 554 | if (ret != 0) { |
| 555 | return ret; |
| 556 | } |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 557 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 558 | return 0; |
Hanno Becker | 9d062f9 | 2020-02-07 10:26:36 +0000 | [diff] [blame] | 559 | } |
| 560 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 561 | |
Hanno Becker | 67a37db | 2020-05-28 16:27:07 +0100 | [diff] [blame] | 562 | #if defined(MBEDTLS_GCM_C) || \ |
| 563 | defined(MBEDTLS_CCM_C) || \ |
| 564 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 565 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 566 | static int ssl_transform_aead_dynamic_iv_is_explicit( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 567 | mbedtls_ssl_transform const *transform) |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 568 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 569 | return transform->ivlen != transform->fixed_ivlen; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 570 | } |
| 571 | |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 572 | /* Compute IV := ( fixed_iv || 0 ) XOR ( 0 || dynamic_IV ) |
| 573 | * |
| 574 | * Concretely, this occurs in two variants: |
| 575 | * |
| 576 | * a) Fixed and dynamic IV lengths add up to total IV length, giving |
| 577 | * IV = fixed_iv || dynamic_iv |
| 578 | * |
Hanno Becker | 1595281 | 2020-06-04 13:31:46 +0100 | [diff] [blame] | 579 | * This variant is used in TLS 1.2 when used with GCM or CCM. |
| 580 | * |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 581 | * b) Fixed IV lengths matches total IV length, giving |
| 582 | * IV = fixed_iv XOR ( 0 || dynamic_iv ) |
Hanno Becker | 1595281 | 2020-06-04 13:31:46 +0100 | [diff] [blame] | 583 | * |
| 584 | * This variant occurs in TLS 1.3 and for TLS 1.2 when using ChaChaPoly. |
| 585 | * |
| 586 | * See also the documentation of mbedtls_ssl_transform. |
Hanno Becker | f486e28 | 2020-06-04 13:33:08 +0100 | [diff] [blame] | 587 | * |
| 588 | * This function has the precondition that |
| 589 | * |
| 590 | * dst_iv_len >= max( fixed_iv_len, dynamic_iv_len ) |
| 591 | * |
| 592 | * which has to be ensured by the caller. If this precondition |
| 593 | * violated, the behavior of this function is undefined. |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 594 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 595 | static void ssl_build_record_nonce(unsigned char *dst_iv, |
| 596 | size_t dst_iv_len, |
| 597 | unsigned char const *fixed_iv, |
| 598 | size_t fixed_iv_len, |
| 599 | unsigned char const *dynamic_iv, |
| 600 | size_t dynamic_iv_len) |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 601 | { |
| 602 | size_t i; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 603 | |
| 604 | /* Start with Fixed IV || 0 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 605 | memset(dst_iv, 0, dst_iv_len); |
| 606 | memcpy(dst_iv, fixed_iv, fixed_iv_len); |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 607 | |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 608 | dst_iv += dst_iv_len - dynamic_iv_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 609 | for (i = 0; i < dynamic_iv_len; i++) { |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 610 | dst_iv[i] ^= dynamic_iv[i]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 611 | } |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 612 | } |
Hanno Becker | 67a37db | 2020-05-28 16:27:07 +0100 | [diff] [blame] | 613 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 614 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 615 | int mbedtls_ssl_encrypt_buf(mbedtls_ssl_context *ssl, |
| 616 | mbedtls_ssl_transform *transform, |
| 617 | mbedtls_record *rec, |
| 618 | int (*f_rng)(void *, unsigned char *, size_t), |
| 619 | void *p_rng) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 620 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 621 | mbedtls_cipher_mode_t mode; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 622 | int auth_done = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 623 | unsigned char *data; |
| 624 | unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX]; |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 625 | size_t add_data_len; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 626 | size_t post_avail; |
| 627 | |
| 628 | /* The SSL context is only used for debugging purposes! */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 629 | #if !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | a7505d1 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 630 | ssl = NULL; /* make sure we don't use it except for debug */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 631 | ((void) ssl); |
| 632 | #endif |
| 633 | |
| 634 | /* The PRNG is used for dynamic IV generation that's used |
| 635 | * for CBC transformations in TLS 1.1 and TLS 1.2. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 636 | #if !(defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) && \ |
| 637 | (defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2))) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 638 | ((void) f_rng); |
| 639 | ((void) p_rng); |
| 640 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 641 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 642 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> encrypt buf")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 643 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 644 | if (transform == NULL) { |
| 645 | MBEDTLS_SSL_DEBUG_MSG(1, ("no transform provided to encrypt_buf")); |
| 646 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 647 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 648 | if (rec == NULL |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 649 | || rec->buf == NULL |
| 650 | || rec->buf_len < rec->data_offset |
| 651 | || rec->buf_len - rec->data_offset < rec->data_len |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 652 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 653 | || rec->cid_len != 0 |
| 654 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 655 | ) { |
| 656 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad record structure provided to encrypt_buf")); |
| 657 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 658 | } |
| 659 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 660 | data = rec->buf + rec->data_offset; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 661 | post_avail = rec->buf_len - (rec->data_len + rec->data_offset); |
| 662 | MBEDTLS_SSL_DEBUG_BUF(4, "before encrypt: output payload", |
| 663 | data, rec->data_len); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 664 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 665 | mode = mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 666 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 667 | if (rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN) { |
| 668 | MBEDTLS_SSL_DEBUG_MSG(1, ("Record content %" MBEDTLS_PRINTF_SIZET |
| 669 | " too large, maximum %" MBEDTLS_PRINTF_SIZET, |
| 670 | rec->data_len, |
| 671 | (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN)); |
| 672 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 673 | } |
Manuel Pégourié-Gonnard | 60346be | 2014-11-21 11:38:37 +0100 | [diff] [blame] | 674 | |
Hanno Becker | 9231340 | 2020-05-20 13:58:58 +0100 | [diff] [blame] | 675 | /* The following two code paths implement the (D)TLSInnerPlaintext |
| 676 | * structure present in TLS 1.3 and DTLS 1.2 + CID. |
| 677 | * |
| 678 | * See ssl_build_inner_plaintext() for more information. |
| 679 | * |
| 680 | * Note that this changes `rec->data_len`, and hence |
| 681 | * `post_avail` needs to be recalculated afterwards. |
| 682 | * |
| 683 | * Note also that the two code paths cannot occur simultaneously |
| 684 | * since they apply to different versions of the protocol. There |
| 685 | * is hence no risk of double-addition of the inner plaintext. |
| 686 | */ |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 687 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 688 | if (transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4) { |
Hanno Becker | 1399692 | 2020-05-28 16:15:19 +0100 | [diff] [blame] | 689 | size_t padding = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 690 | ssl_compute_padding_length(rec->data_len, |
| 691 | MBEDTLS_SSL_TLS1_3_PADDING_GRANULARITY); |
| 692 | if (ssl_build_inner_plaintext(data, |
| 693 | &rec->data_len, |
| 694 | post_avail, |
| 695 | rec->type, |
| 696 | padding) != 0) { |
| 697 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | rec->type = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
| 701 | } |
| 702 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
| 703 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 704 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 705 | /* |
| 706 | * Add CID information |
| 707 | */ |
| 708 | rec->cid_len = transform->out_cid_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 709 | memcpy(rec->cid, transform->out_cid, transform->out_cid_len); |
| 710 | MBEDTLS_SSL_DEBUG_BUF(3, "CID", rec->cid, rec->cid_len); |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 711 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 712 | if (rec->cid_len != 0) { |
Hanno Becker | 1399692 | 2020-05-28 16:15:19 +0100 | [diff] [blame] | 713 | size_t padding = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 714 | ssl_compute_padding_length(rec->data_len, |
| 715 | MBEDTLS_SSL_CID_PADDING_GRANULARITY); |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 716 | /* |
Hanno Becker | 07dc97d | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 717 | * Wrap plaintext into DTLSInnerPlaintext structure. |
Hanno Becker | 581bc1b | 2020-05-04 12:20:03 +0100 | [diff] [blame] | 718 | * See ssl_build_inner_plaintext() for more information. |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 719 | * |
Hanno Becker | 07dc97d | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 720 | * Note that this changes `rec->data_len`, and hence |
| 721 | * `post_avail` needs to be recalculated afterwards. |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 722 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 723 | if (ssl_build_inner_plaintext(data, |
| 724 | &rec->data_len, |
| 725 | post_avail, |
| 726 | rec->type, |
| 727 | padding) != 0) { |
| 728 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | rec->type = MBEDTLS_SSL_MSG_CID; |
| 732 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 733 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 734 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 735 | post_avail = rec->buf_len - (rec->data_len + rec->data_offset); |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 736 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 737 | /* |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 738 | * Add MAC before if needed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 739 | */ |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 740 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 741 | if (mode == MBEDTLS_MODE_STREAM || |
| 742 | (mode == MBEDTLS_MODE_CBC |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 743 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 744 | && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 745 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 746 | )) { |
| 747 | if (post_avail < transform->maclen) { |
| 748 | MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough")); |
| 749 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 752 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 753 | if (transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) { |
Hanno Becker | 9d062f9 | 2020-02-07 10:26:36 +0000 | [diff] [blame] | 754 | unsigned char mac[SSL3_MAC_MAX_BYTES]; |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 755 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 756 | ret = ssl_mac(&transform->md_ctx_enc, transform->mac_enc, |
| 757 | data, rec->data_len, rec->ctr, rec->type, mac); |
| 758 | if (ret == 0) { |
| 759 | memcpy(data + rec->data_len, mac, transform->maclen); |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 760 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 761 | mbedtls_platform_zeroize(mac, transform->maclen); |
| 762 | if (ret != 0) { |
| 763 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_mac", ret); |
| 764 | return ret; |
| 765 | } |
| 766 | } else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 767 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 768 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 769 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 770 | if (transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1) { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 771 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 772 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 773 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 774 | ssl_extract_add_data_from_record(add_data, &add_data_len, rec, |
| 775 | transform->minor_ver); |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 776 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 777 | ret = mbedtls_md_hmac_update(&transform->md_ctx_enc, |
| 778 | add_data, add_data_len); |
| 779 | if (ret != 0) { |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 780 | goto hmac_failed_etm_disabled; |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 781 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 782 | ret = mbedtls_md_hmac_update(&transform->md_ctx_enc, |
| 783 | data, rec->data_len); |
| 784 | if (ret != 0) { |
| 785 | goto hmac_failed_etm_disabled; |
| 786 | } |
| 787 | ret = mbedtls_md_hmac_finish(&transform->md_ctx_enc, mac); |
| 788 | if (ret != 0) { |
| 789 | goto hmac_failed_etm_disabled; |
| 790 | } |
| 791 | ret = mbedtls_md_hmac_reset(&transform->md_ctx_enc); |
| 792 | if (ret != 0) { |
| 793 | goto hmac_failed_etm_disabled; |
| 794 | } |
| 795 | |
| 796 | memcpy(data + rec->data_len, mac, transform->maclen); |
| 797 | |
| 798 | hmac_failed_etm_disabled: |
| 799 | mbedtls_platform_zeroize(mac, transform->maclen); |
| 800 | if (ret != 0) { |
| 801 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_hmac_xxx", ret); |
| 802 | return ret; |
| 803 | } |
| 804 | } else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 805 | #endif |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 806 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 807 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 808 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 809 | } |
| 810 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 811 | MBEDTLS_SSL_DEBUG_BUF(4, "computed mac", data + rec->data_len, |
| 812 | transform->maclen); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 813 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 814 | rec->data_len += transform->maclen; |
| 815 | post_avail -= transform->maclen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 816 | auth_done++; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 817 | } |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 818 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 819 | |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 820 | /* |
| 821 | * Encrypt |
| 822 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 823 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 824 | if (mode == MBEDTLS_MODE_STREAM) { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 825 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 826 | size_t olen; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 827 | MBEDTLS_SSL_DEBUG_MSG(3, ("before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", " |
| 828 | "including %d bytes of padding", |
| 829 | rec->data_len, 0)); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 830 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 831 | if ((ret = mbedtls_cipher_crypt(&transform->cipher_ctx_enc, |
| 832 | transform->iv_enc, transform->ivlen, |
| 833 | data, rec->data_len, |
| 834 | data, &olen)) != 0) { |
| 835 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret); |
| 836 | return ret; |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 837 | } |
| 838 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 839 | if (rec->data_len != olen) { |
| 840 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 841 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 842 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 843 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 844 | #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 845 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 846 | #if defined(MBEDTLS_GCM_C) || \ |
| 847 | defined(MBEDTLS_CCM_C) || \ |
| 848 | defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 849 | if (mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 850 | mode == MBEDTLS_MODE_CCM || |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 851 | mode == MBEDTLS_MODE_CHACHAPOLY) { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 852 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 853 | unsigned char iv[12]; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 854 | unsigned char *dynamic_iv; |
| 855 | size_t dynamic_iv_len; |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 856 | int dynamic_iv_is_explicit = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 857 | ssl_transform_aead_dynamic_iv_is_explicit(transform); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 858 | |
Hanno Becker | bd5ed1d | 2020-05-21 15:26:39 +0100 | [diff] [blame] | 859 | /* Check that there's space for the authentication tag. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 860 | if (post_avail < transform->taglen) { |
| 861 | MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough")); |
| 862 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 863 | } |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 864 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 865 | /* |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 866 | * Build nonce for AEAD encryption. |
| 867 | * |
| 868 | * Note: In the case of CCM and GCM in TLS 1.2, the dynamic |
| 869 | * part of the IV is prepended to the ciphertext and |
| 870 | * can be chosen freely - in particular, it need not |
| 871 | * agree with the record sequence number. |
| 872 | * However, since ChaChaPoly as well as all AEAD modes |
| 873 | * in TLS 1.3 use the record sequence number as the |
| 874 | * dynamic part of the nonce, we uniformly use the |
| 875 | * record sequence number here in all cases. |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 876 | */ |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 877 | dynamic_iv = rec->ctr; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 878 | dynamic_iv_len = sizeof(rec->ctr); |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 879 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 880 | ssl_build_record_nonce(iv, sizeof(iv), |
| 881 | transform->iv_enc, |
| 882 | transform->fixed_ivlen, |
| 883 | dynamic_iv, |
| 884 | dynamic_iv_len); |
Manuel Pégourié-Gonnard | d056ce0 | 2014-10-29 22:29:20 +0100 | [diff] [blame] | 885 | |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 886 | /* |
| 887 | * Build additional data for AEAD encryption. |
| 888 | * This depends on the TLS version. |
| 889 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 890 | ssl_extract_add_data_from_record(add_data, &add_data_len, rec, |
| 891 | transform->minor_ver); |
Hanno Becker | 1f10d76 | 2019-04-26 13:34:37 +0100 | [diff] [blame] | 892 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 893 | MBEDTLS_SSL_DEBUG_BUF(4, "IV used (internal)", |
| 894 | iv, transform->ivlen); |
| 895 | MBEDTLS_SSL_DEBUG_BUF(4, "IV used (transmitted)", |
| 896 | dynamic_iv, |
| 897 | dynamic_iv_is_explicit ? dynamic_iv_len : 0); |
| 898 | MBEDTLS_SSL_DEBUG_BUF(4, "additional data used for AEAD", |
| 899 | add_data, add_data_len); |
| 900 | MBEDTLS_SSL_DEBUG_MSG(3, ("before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", " |
| 901 | "including 0 bytes of padding", |
| 902 | rec->data_len)); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 903 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 904 | /* |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 905 | * Encrypt and authenticate |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 906 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 907 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 908 | if ((ret = mbedtls_cipher_auth_encrypt_ext(&transform->cipher_ctx_enc, |
| 909 | iv, transform->ivlen, |
| 910 | add_data, add_data_len, |
| 911 | data, rec->data_len, /* src */ |
| 912 | data, rec->buf_len - (data - rec->buf), /* dst */ |
| 913 | &rec->data_len, |
| 914 | transform->taglen)) != 0) { |
| 915 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_auth_encrypt", ret); |
| 916 | return ret; |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 917 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 918 | MBEDTLS_SSL_DEBUG_BUF(4, "after encrypt: tag", |
| 919 | data + rec->data_len - transform->taglen, |
| 920 | transform->taglen); |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 921 | /* Account for authentication tag. */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 922 | post_avail -= transform->taglen; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 923 | |
| 924 | /* |
| 925 | * Prefix record content with dynamic IV in case it is explicit. |
| 926 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 927 | if (dynamic_iv_is_explicit != 0) { |
| 928 | if (rec->data_offset < dynamic_iv_len) { |
| 929 | MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough")); |
| 930 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 931 | } |
| 932 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 933 | memcpy(data - dynamic_iv_len, dynamic_iv, dynamic_iv_len); |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 934 | rec->data_offset -= dynamic_iv_len; |
| 935 | rec->data_len += dynamic_iv_len; |
| 936 | } |
| 937 | |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 938 | auth_done++; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 939 | } else |
Hanno Becker | c3f7b0b | 2020-05-28 16:27:16 +0100 | [diff] [blame] | 940 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
Manuel Pégourié-Gonnard | 2df1f1f | 2020-07-09 12:11:39 +0200 | [diff] [blame] | 941 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 942 | if (mode == MBEDTLS_MODE_CBC) { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 943 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 944 | size_t padlen, i; |
| 945 | size_t olen; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 946 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 947 | /* Currently we're always using minimal padding |
| 948 | * (up to 255 bytes would be allowed). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 949 | padlen = transform->ivlen - (rec->data_len + 1) % transform->ivlen; |
| 950 | if (padlen == transform->ivlen) { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 951 | padlen = 0; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 952 | } |
| 953 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 954 | /* Check there's enough space in the buffer for the padding. */ |
| 955 | if (post_avail < padlen + 1) { |
| 956 | MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough")); |
| 957 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 958 | } |
| 959 | |
| 960 | for (i = 0; i <= padlen; i++) { |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 961 | data[rec->data_len + i] = (unsigned char) padlen; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 962 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 963 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 964 | rec->data_len += padlen + 1; |
| 965 | post_avail -= padlen + 1; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 966 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 967 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 968 | /* |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 969 | * Prepend per-record IV for block cipher in TLS v1.1 and up as per |
| 970 | * Method 1 (6.2.3.2. in RFC4346 and RFC5246) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 971 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 972 | if (transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2) { |
| 973 | if (f_rng == NULL) { |
| 974 | MBEDTLS_SSL_DEBUG_MSG(1, ("No PRNG provided to encrypt_record routine")); |
| 975 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 976 | } |
| 977 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 978 | if (rec->data_offset < transform->ivlen) { |
| 979 | MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough")); |
| 980 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 981 | } |
| 982 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 983 | /* |
| 984 | * Generate IV |
| 985 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 986 | ret = f_rng(p_rng, transform->iv_enc, transform->ivlen); |
| 987 | if (ret != 0) { |
| 988 | return ret; |
| 989 | } |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 990 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 991 | memcpy(data - transform->ivlen, transform->iv_enc, |
| 992 | transform->ivlen); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 993 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 994 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 995 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 996 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 997 | MBEDTLS_SSL_DEBUG_MSG(3, ("before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", " |
| 998 | "including %" |
| 999 | MBEDTLS_PRINTF_SIZET |
| 1000 | " bytes of IV and %" MBEDTLS_PRINTF_SIZET " bytes of padding", |
| 1001 | rec->data_len, transform->ivlen, |
| 1002 | padlen + 1)); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1003 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1004 | if ((ret = mbedtls_cipher_crypt(&transform->cipher_ctx_enc, |
| 1005 | transform->iv_enc, |
| 1006 | transform->ivlen, |
| 1007 | data, rec->data_len, |
| 1008 | data, &olen)) != 0) { |
| 1009 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret); |
| 1010 | return ret; |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1011 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1012 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1013 | if (rec->data_len != olen) { |
| 1014 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1015 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1016 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1017 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1018 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1019 | if (transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2) { |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1020 | /* |
| 1021 | * Save IV in SSL3 and TLS1 |
| 1022 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1023 | memcpy(transform->iv_enc, transform->cipher_ctx_enc.iv, |
| 1024 | transform->ivlen); |
| 1025 | } else |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1026 | #endif |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1027 | { |
| 1028 | data -= transform->ivlen; |
| 1029 | rec->data_offset -= transform->ivlen; |
| 1030 | rec->data_len += transform->ivlen; |
| 1031 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1032 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1033 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1034 | if (auth_done == 0) { |
Hanno Becker | 3d8c907 | 2018-01-05 16:24:22 +0000 | [diff] [blame] | 1035 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; |
| 1036 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1037 | /* |
| 1038 | * MAC(MAC_write_key, seq_num + |
| 1039 | * TLSCipherText.type + |
| 1040 | * TLSCipherText.version + |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 1041 | * length_of( (IV +) ENC(...) ) + |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1042 | * IV + // except for TLS 1.0 |
| 1043 | * ENC(content + padding + padding_length)); |
| 1044 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1045 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1046 | if (post_avail < transform->maclen) { |
| 1047 | MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough")); |
| 1048 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1049 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1050 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1051 | ssl_extract_add_data_from_record(add_data, &add_data_len, |
| 1052 | rec, transform->minor_ver); |
Hanno Becker | 1f10d76 | 2019-04-26 13:34:37 +0100 | [diff] [blame] | 1053 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1054 | MBEDTLS_SSL_DEBUG_MSG(3, ("using encrypt then mac")); |
| 1055 | MBEDTLS_SSL_DEBUG_BUF(4, "MAC'd meta-data", add_data, |
| 1056 | add_data_len); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1057 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1058 | ret = mbedtls_md_hmac_update(&transform->md_ctx_enc, add_data, |
| 1059 | add_data_len); |
| 1060 | if (ret != 0) { |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1061 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1062 | } |
| 1063 | ret = mbedtls_md_hmac_update(&transform->md_ctx_enc, |
| 1064 | data, rec->data_len); |
| 1065 | if (ret != 0) { |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1066 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1067 | } |
| 1068 | ret = mbedtls_md_hmac_finish(&transform->md_ctx_enc, mac); |
| 1069 | if (ret != 0) { |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1070 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1071 | } |
| 1072 | ret = mbedtls_md_hmac_reset(&transform->md_ctx_enc); |
| 1073 | if (ret != 0) { |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1074 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1075 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1076 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1077 | memcpy(data + rec->data_len, mac, transform->maclen); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1078 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1079 | rec->data_len += transform->maclen; |
| 1080 | post_avail -= transform->maclen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1081 | auth_done++; |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1082 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1083 | hmac_failed_etm_enabled: |
| 1084 | mbedtls_platform_zeroize(mac, transform->maclen); |
| 1085 | if (ret != 0) { |
| 1086 | MBEDTLS_SSL_DEBUG_RET(1, "HMAC calculation failed", ret); |
| 1087 | return ret; |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1088 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1089 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1090 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1091 | } else |
Manuel Pégourié-Gonnard | 2df1f1f | 2020-07-09 12:11:39 +0200 | [diff] [blame] | 1092 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */ |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 1093 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1094 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1095 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 1096 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1097 | |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1098 | /* Make extra sure authentication was performed, exactly once */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1099 | if (auth_done != 1) { |
| 1100 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1101 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1102 | } |
| 1103 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1104 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= encrypt buf")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1105 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1106 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1109 | int mbedtls_ssl_decrypt_buf(mbedtls_ssl_context const *ssl, |
| 1110 | mbedtls_ssl_transform *transform, |
| 1111 | mbedtls_record *rec) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1112 | { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1113 | size_t olen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1114 | mbedtls_cipher_mode_t mode; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1115 | int ret, auth_done = 0; |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 1116 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 1117 | size_t padlen = 0, correct = 1; |
| 1118 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1119 | unsigned char *data; |
| 1120 | unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX]; |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1121 | size_t add_data_len; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1122 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1123 | #if !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | a7505d1 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 1124 | ssl = NULL; /* make sure we don't use it except for debug */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1125 | ((void) ssl); |
| 1126 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1127 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1128 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> decrypt buf")); |
| 1129 | if (rec == NULL || |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1130 | rec->buf == NULL || |
| 1131 | rec->buf_len < rec->data_offset || |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1132 | rec->buf_len - rec->data_offset < rec->data_len) { |
| 1133 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad record structure provided to decrypt_buf")); |
| 1134 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1135 | } |
| 1136 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1137 | data = rec->buf + rec->data_offset; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1138 | mode = mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_dec); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1139 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1140 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1141 | /* |
| 1142 | * Match record's CID with incoming CID. |
| 1143 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1144 | if (rec->cid_len != transform->in_cid_len || |
| 1145 | memcmp(rec->cid, transform->in_cid, rec->cid_len) != 0) { |
| 1146 | return MBEDTLS_ERR_SSL_UNEXPECTED_CID; |
Hanno Becker | 938489a | 2019-05-08 13:02:22 +0100 | [diff] [blame] | 1147 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1148 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1149 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1150 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1151 | if (mode == MBEDTLS_MODE_STREAM) { |
Gilles Peskine | 326ba3c | 2023-09-18 14:08:11 +0200 | [diff] [blame] | 1152 | if (rec->data_len < transform->maclen) { |
| 1153 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 1154 | ("Record too short for MAC:" |
| 1155 | " %" MBEDTLS_PRINTF_SIZET " < %" MBEDTLS_PRINTF_SIZET, |
| 1156 | rec->data_len, transform->maclen)); |
| 1157 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
| 1158 | } |
| 1159 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1160 | padlen = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1161 | if ((ret = mbedtls_cipher_crypt(&transform->cipher_ctx_dec, |
| 1162 | transform->iv_dec, |
| 1163 | transform->ivlen, |
| 1164 | data, rec->data_len, |
| 1165 | data, &olen)) != 0) { |
| 1166 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret); |
| 1167 | return ret; |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1168 | } |
| 1169 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1170 | if (rec->data_len != olen) { |
| 1171 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1172 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1173 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1174 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1175 | #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1176 | #if defined(MBEDTLS_GCM_C) || \ |
| 1177 | defined(MBEDTLS_CCM_C) || \ |
| 1178 | defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1179 | if (mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1180 | mode == MBEDTLS_MODE_CCM || |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1181 | mode == MBEDTLS_MODE_CHACHAPOLY) { |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1182 | unsigned char iv[12]; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1183 | unsigned char *dynamic_iv; |
| 1184 | size_t dynamic_iv_len; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1185 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1186 | /* |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1187 | * Extract dynamic part of nonce for AEAD decryption. |
| 1188 | * |
| 1189 | * Note: In the case of CCM and GCM in TLS 1.2, the dynamic |
| 1190 | * part of the IV is prepended to the ciphertext and |
| 1191 | * can be chosen freely - in particular, it need not |
| 1192 | * agree with the record sequence number. |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1193 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1194 | dynamic_iv_len = sizeof(rec->ctr); |
| 1195 | if (ssl_transform_aead_dynamic_iv_is_explicit(transform) == 1) { |
| 1196 | if (rec->data_len < dynamic_iv_len) { |
| 1197 | MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET |
| 1198 | " ) < explicit_iv_len (%" MBEDTLS_PRINTF_SIZET ") ", |
| 1199 | rec->data_len, |
| 1200 | dynamic_iv_len)); |
| 1201 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1202 | } |
| 1203 | dynamic_iv = data; |
| 1204 | |
| 1205 | data += dynamic_iv_len; |
| 1206 | rec->data_offset += dynamic_iv_len; |
| 1207 | rec->data_len -= dynamic_iv_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1208 | } else { |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 1209 | dynamic_iv = rec->ctr; |
| 1210 | } |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1211 | |
| 1212 | /* Check that there's space for the authentication tag. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1213 | if (rec->data_len < transform->taglen) { |
| 1214 | MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET |
| 1215 | ") < taglen (%" MBEDTLS_PRINTF_SIZET ") ", |
| 1216 | rec->data_len, |
| 1217 | transform->taglen)); |
| 1218 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 1219 | } |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1220 | rec->data_len -= transform->taglen; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1221 | |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1222 | /* |
| 1223 | * Prepare nonce from dynamic and static parts. |
| 1224 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1225 | ssl_build_record_nonce(iv, sizeof(iv), |
| 1226 | transform->iv_dec, |
| 1227 | transform->fixed_ivlen, |
| 1228 | dynamic_iv, |
| 1229 | dynamic_iv_len); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1230 | |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1231 | /* |
| 1232 | * Build additional data for AEAD encryption. |
| 1233 | * This depends on the TLS version. |
| 1234 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1235 | ssl_extract_add_data_from_record(add_data, &add_data_len, rec, |
| 1236 | transform->minor_ver); |
| 1237 | MBEDTLS_SSL_DEBUG_BUF(4, "additional data used for AEAD", |
| 1238 | add_data, add_data_len); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1239 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1240 | /* Because of the check above, we know that there are |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1241 | * explicit_iv_len Bytes preceding data, and taglen |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1242 | * bytes following data + data_len. This justifies |
Hanno Becker | 2001665 | 2019-07-10 11:44:13 +0100 | [diff] [blame] | 1243 | * the debug message and the invocation of |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1244 | * mbedtls_cipher_auth_decrypt() below. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1245 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1246 | MBEDTLS_SSL_DEBUG_BUF(4, "IV used", iv, transform->ivlen); |
| 1247 | MBEDTLS_SSL_DEBUG_BUF(4, "TAG used", data + rec->data_len, |
| 1248 | transform->taglen); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1249 | |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1250 | /* |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1251 | * Decrypt and authenticate |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1252 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1253 | if ((ret = mbedtls_cipher_auth_decrypt_ext(&transform->cipher_ctx_dec, |
| 1254 | iv, transform->ivlen, |
| 1255 | add_data, add_data_len, |
| 1256 | data, rec->data_len + transform->taglen, /* src */ |
| 1257 | data, rec->buf_len - (data - rec->buf), &olen, /* dst */ |
| 1258 | transform->taglen)) != 0) { |
| 1259 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_auth_decrypt", ret); |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1260 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1261 | if (ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED) { |
| 1262 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
| 1263 | } |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1264 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1265 | return ret; |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1266 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1267 | auth_done++; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1268 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1269 | /* Double-check that AEAD decryption doesn't change content length. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1270 | if (olen != rec->data_len) { |
| 1271 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1272 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1273 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1274 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1275 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ |
Manuel Pégourié-Gonnard | 2df1f1f | 2020-07-09 12:11:39 +0200 | [diff] [blame] | 1276 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1277 | if (mode == MBEDTLS_MODE_CBC) { |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 1278 | size_t minlen = 0; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1279 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1280 | /* |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1281 | * Check immediate ciphertext sanity |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1282 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1283 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1284 | if (transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2) { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1285 | /* The ciphertext is prefixed with the CBC IV. */ |
| 1286 | minlen += transform->ivlen; |
| 1287 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1288 | #endif |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1289 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1290 | /* Size considerations: |
| 1291 | * |
| 1292 | * - The CBC cipher text must not be empty and hence |
| 1293 | * at least of size transform->ivlen. |
| 1294 | * |
| 1295 | * Together with the potential IV-prefix, this explains |
| 1296 | * the first of the two checks below. |
| 1297 | * |
| 1298 | * - The record must contain a MAC, either in plain or |
| 1299 | * encrypted, depending on whether Encrypt-then-MAC |
| 1300 | * is used or not. |
| 1301 | * - If it is, the message contains the IV-prefix, |
| 1302 | * the CBC ciphertext, and the MAC. |
| 1303 | * - If it is not, the padded plaintext, and hence |
| 1304 | * the CBC ciphertext, has at least length maclen + 1 |
| 1305 | * because there is at least the padding length byte. |
| 1306 | * |
| 1307 | * As the CBC ciphertext is not empty, both cases give the |
| 1308 | * lower bound minlen + maclen + 1 on the record size, which |
| 1309 | * we test for in the second check below. |
| 1310 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1311 | if (rec->data_len < minlen + transform->ivlen || |
| 1312 | rec->data_len < minlen + transform->maclen + 1) { |
| 1313 | MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET |
| 1314 | ") < max( ivlen(%" MBEDTLS_PRINTF_SIZET |
| 1315 | "), maclen (%" MBEDTLS_PRINTF_SIZET ") " |
| 1316 | "+ 1 ) ( + expl IV )", |
| 1317 | rec->data_len, |
| 1318 | transform->ivlen, |
| 1319 | transform->maclen)); |
| 1320 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1321 | } |
| 1322 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1323 | /* |
| 1324 | * Authenticate before decrypt if enabled |
| 1325 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1326 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1327 | if (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED) { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 1328 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD]; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1329 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1330 | MBEDTLS_SSL_DEBUG_MSG(3, ("using encrypt then mac")); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1331 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1332 | /* Update data_len in tandem with add_data. |
| 1333 | * |
| 1334 | * The subtraction is safe because of the previous check |
| 1335 | * data_len >= minlen + maclen + 1. |
| 1336 | * |
| 1337 | * Afterwards, we know that data + data_len is followed by at |
| 1338 | * least maclen Bytes, which justifies the call to |
Gabor Mezei | 18a4494 | 2021-10-20 11:59:27 +0200 | [diff] [blame] | 1339 | * mbedtls_ct_memcmp() below. |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1340 | * |
| 1341 | * Further, we still know that data_len > minlen */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1342 | rec->data_len -= transform->maclen; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1343 | ssl_extract_add_data_from_record(add_data, &add_data_len, rec, |
| 1344 | transform->minor_ver); |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 1345 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1346 | /* Calculate expected MAC. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1347 | MBEDTLS_SSL_DEBUG_BUF(4, "MAC'd meta-data", add_data, |
| 1348 | add_data_len); |
| 1349 | ret = mbedtls_md_hmac_update(&transform->md_ctx_dec, add_data, |
| 1350 | add_data_len); |
| 1351 | if (ret != 0) { |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1352 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1353 | } |
| 1354 | ret = mbedtls_md_hmac_update(&transform->md_ctx_dec, |
| 1355 | data, rec->data_len); |
| 1356 | if (ret != 0) { |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1357 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1358 | } |
| 1359 | ret = mbedtls_md_hmac_finish(&transform->md_ctx_dec, mac_expect); |
| 1360 | if (ret != 0) { |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1361 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1362 | } |
| 1363 | ret = mbedtls_md_hmac_reset(&transform->md_ctx_dec); |
| 1364 | if (ret != 0) { |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1365 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1366 | } |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 1367 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1368 | MBEDTLS_SSL_DEBUG_BUF(4, "message mac", data + rec->data_len, |
| 1369 | transform->maclen); |
| 1370 | MBEDTLS_SSL_DEBUG_BUF(4, "expected mac", mac_expect, |
| 1371 | transform->maclen); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1372 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1373 | /* Compare expected MAC with MAC at the end of the record. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1374 | if (mbedtls_ct_memcmp(data + rec->data_len, mac_expect, |
| 1375 | transform->maclen) != 0) { |
| 1376 | MBEDTLS_SSL_DEBUG_MSG(1, ("message mac does not match")); |
Gilles Peskine | d8e2e83 | 2021-12-10 21:33:21 +0100 | [diff] [blame] | 1377 | ret = MBEDTLS_ERR_SSL_INVALID_MAC; |
| 1378 | goto hmac_failed_etm_enabled; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1379 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1380 | auth_done++; |
Gilles Peskine | d8e2e83 | 2021-12-10 21:33:21 +0100 | [diff] [blame] | 1381 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1382 | hmac_failed_etm_enabled: |
| 1383 | mbedtls_platform_zeroize(mac_expect, transform->maclen); |
| 1384 | if (ret != 0) { |
| 1385 | if (ret != MBEDTLS_ERR_SSL_INVALID_MAC) { |
| 1386 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_hmac_xxx", ret); |
| 1387 | } |
| 1388 | return ret; |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1389 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1390 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1391 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1392 | |
| 1393 | /* |
| 1394 | * Check length sanity |
| 1395 | */ |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1396 | |
| 1397 | /* We know from above that data_len > minlen >= 0, |
| 1398 | * so the following check in particular implies that |
| 1399 | * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1400 | if (rec->data_len % transform->ivlen != 0) { |
| 1401 | MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET |
| 1402 | ") %% ivlen (%" MBEDTLS_PRINTF_SIZET ") != 0", |
| 1403 | rec->data_len, transform->ivlen)); |
| 1404 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1405 | } |
| 1406 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1407 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1408 | /* |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 1409 | * Initialize for prepended IV for block cipher in TLS v1.1 and up |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1410 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1411 | if (transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2) { |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1412 | /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1413 | memcpy(transform->iv_dec, data, transform->ivlen); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1414 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1415 | data += transform->ivlen; |
| 1416 | rec->data_offset += transform->ivlen; |
| 1417 | rec->data_len -= transform->ivlen; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1418 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1419 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1420 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1421 | /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */ |
| 1422 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1423 | if ((ret = mbedtls_cipher_crypt(&transform->cipher_ctx_dec, |
| 1424 | transform->iv_dec, transform->ivlen, |
| 1425 | data, rec->data_len, data, &olen)) != 0) { |
| 1426 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret); |
| 1427 | return ret; |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1428 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1429 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1430 | /* Double-check that length hasn't changed during decryption. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1431 | if (rec->data_len != olen) { |
| 1432 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1433 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1434 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1435 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1436 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1437 | if (transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2) { |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1438 | /* |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1439 | * Save IV in SSL3 and TLS1, where CBC decryption of consecutive |
| 1440 | * records is equivalent to CBC decryption of the concatenation |
| 1441 | * of the records; in other words, IVs are maintained across |
| 1442 | * record decryptions. |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1443 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1444 | memcpy(transform->iv_dec, transform->cipher_ctx_dec.iv, |
| 1445 | transform->ivlen); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1446 | } |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1447 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1448 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1449 | /* Safe since data_len >= minlen + maclen + 1, so after having |
| 1450 | * subtracted at most minlen and maclen up to this point, |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1451 | * data_len > 0 (because of data_len % ivlen == 0, it's actually |
| 1452 | * >= ivlen ). */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1453 | padlen = data[rec->data_len - 1]; |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1454 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1455 | if (auth_done == 1) { |
Gabor Mezei | 18a4494 | 2021-10-20 11:59:27 +0200 | [diff] [blame] | 1456 | const size_t mask = mbedtls_ct_size_mask_ge( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1457 | rec->data_len, |
| 1458 | padlen + 1); |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 1459 | correct &= mask; |
| 1460 | padlen &= mask; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1461 | } else { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1462 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1463 | if (rec->data_len < transform->maclen + padlen + 1) { |
| 1464 | MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET |
| 1465 | ") < maclen (%" MBEDTLS_PRINTF_SIZET |
| 1466 | ") + padlen (%" MBEDTLS_PRINTF_SIZET ")", |
| 1467 | rec->data_len, |
| 1468 | transform->maclen, |
| 1469 | padlen + 1)); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1470 | } |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 1471 | #endif |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1472 | |
Gabor Mezei | 18a4494 | 2021-10-20 11:59:27 +0200 | [diff] [blame] | 1473 | const size_t mask = mbedtls_ct_size_mask_ge( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1474 | rec->data_len, |
| 1475 | transform->maclen + padlen + 1); |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 1476 | correct &= mask; |
| 1477 | padlen &= mask; |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1478 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1479 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1480 | padlen++; |
| 1481 | |
| 1482 | /* Regardless of the validity of the padding, |
| 1483 | * we have data_len >= padlen here. */ |
| 1484 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1485 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1486 | if (transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) { |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 1487 | /* This is the SSL 3.0 path, we don't have to worry about Lucky |
| 1488 | * 13, because there's a strictly worse padding attack built in |
Manuel Pégourié-Gonnard | 6d6f8a4 | 2020-09-25 09:56:53 +0200 | [diff] [blame] | 1489 | * the protocol (known as part of POODLE), so we don't care if the |
| 1490 | * code is not constant-time, in particular branches are OK. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1491 | if (padlen > transform->ivlen) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1492 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1493 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad padding length: is %" MBEDTLS_PRINTF_SIZET ", " |
| 1494 | "should be no more than %" |
| 1495 | MBEDTLS_PRINTF_SIZET, |
| 1496 | padlen, transform->ivlen)); |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 1497 | #endif |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1498 | correct = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1499 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1500 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1501 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 1502 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1503 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1504 | if (transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0) { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1505 | /* The padding check involves a series of up to 256 |
| 1506 | * consecutive memory reads at the end of the record |
| 1507 | * plaintext buffer. In order to hide the length and |
| 1508 | * validity of the padding, always perform exactly |
| 1509 | * `min(256,plaintext_len)` reads (but take into account |
| 1510 | * only the last `padlen` bytes for the padding check). */ |
| 1511 | size_t pad_count = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1512 | volatile unsigned char * const check = data; |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 1513 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1514 | /* Index of first padding byte; it has been ensured above |
| 1515 | * that the subtraction is safe. */ |
| 1516 | size_t const padding_idx = rec->data_len - padlen; |
| 1517 | size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256; |
| 1518 | size_t const start_idx = rec->data_len - num_checks; |
| 1519 | size_t idx; |
Paul Bakker | 956c9e0 | 2013-12-19 14:42:28 +0100 | [diff] [blame] | 1520 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1521 | for (idx = start_idx; idx < rec->data_len; idx++) { |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 1522 | /* pad_count += (idx >= padding_idx) && |
Manuel Pégourié-Gonnard | 6d6f8a4 | 2020-09-25 09:56:53 +0200 | [diff] [blame] | 1523 | * (check[idx] == padlen - 1); |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 1524 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1525 | const size_t mask = mbedtls_ct_size_mask_ge(idx, padding_idx); |
| 1526 | const size_t equal = mbedtls_ct_size_bool_eq(check[idx], |
| 1527 | padlen - 1); |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 1528 | pad_count += mask & equal; |
Paul Bakker | ca9c87e | 2013-09-25 18:52:37 +0200 | [diff] [blame] | 1529 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1530 | correct &= mbedtls_ct_size_bool_eq(pad_count, padlen); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 1531 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1532 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1533 | if (padlen > 0 && correct == 0) { |
| 1534 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad padding byte detected")); |
| 1535 | } |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 1536 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1537 | padlen &= mbedtls_ct_size_mask(correct); |
| 1538 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1539 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 1540 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1541 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1542 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1543 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1544 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1545 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1546 | /* If the padding was found to be invalid, padlen == 0 |
| 1547 | * and the subtraction is safe. If the padding was found valid, |
| 1548 | * padlen hasn't been changed and the previous assertion |
| 1549 | * data_len >= padlen still holds. */ |
| 1550 | rec->data_len -= padlen; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1551 | } else |
Manuel Pégourié-Gonnard | 2df1f1f | 2020-07-09 12:11:39 +0200 | [diff] [blame] | 1552 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */ |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 1553 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1554 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1555 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 1556 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1557 | |
Manuel Pégourié-Gonnard | 6a25cfa | 2018-07-10 11:15:36 +0200 | [diff] [blame] | 1558 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1559 | MBEDTLS_SSL_DEBUG_BUF(4, "raw buffer after decryption", |
| 1560 | data, rec->data_len); |
Manuel Pégourié-Gonnard | 6a25cfa | 2018-07-10 11:15:36 +0200 | [diff] [blame] | 1561 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1562 | |
| 1563 | /* |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1564 | * Authenticate if not done yet. |
| 1565 | * Compute the MAC regardless of the padding result (RFC4346, CBCTIME). |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1566 | */ |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 1567 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1568 | if (auth_done == 0) { |
Paul Elliott | b830028 | 2022-05-19 18:31:35 +0100 | [diff] [blame] | 1569 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD] = { 0 }; |
| 1570 | unsigned char mac_peer[MBEDTLS_SSL_MAC_ADD] = { 0 }; |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 1571 | |
Gilles Peskine | 326ba3c | 2023-09-18 14:08:11 +0200 | [diff] [blame] | 1572 | /* For CBC+MAC, If the initial value of padlen was such that |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1573 | * data_len < maclen + padlen + 1, then padlen |
| 1574 | * got reset to 1, and the initial check |
| 1575 | * data_len >= minlen + maclen + 1 |
| 1576 | * guarantees that at this point we still |
| 1577 | * have at least data_len >= maclen. |
| 1578 | * |
| 1579 | * If the initial value of padlen was such that |
| 1580 | * data_len >= maclen + padlen + 1, then we have |
| 1581 | * subtracted either padlen + 1 (if the padding was correct) |
| 1582 | * or 0 (if the padding was incorrect) since then, |
| 1583 | * hence data_len >= maclen in any case. |
Gilles Peskine | 326ba3c | 2023-09-18 14:08:11 +0200 | [diff] [blame] | 1584 | * |
| 1585 | * For stream ciphers, we checked above that |
| 1586 | * data_len >= maclen. |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1587 | */ |
| 1588 | rec->data_len -= transform->maclen; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1589 | ssl_extract_add_data_from_record(add_data, &add_data_len, rec, |
| 1590 | transform->minor_ver); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1591 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1592 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1593 | if (transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) { |
| 1594 | ret = ssl_mac(&transform->md_ctx_dec, |
| 1595 | transform->mac_dec, |
| 1596 | data, rec->data_len, |
| 1597 | rec->ctr, rec->type, |
| 1598 | mac_expect); |
| 1599 | if (ret != 0) { |
| 1600 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_mac", ret); |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1601 | goto hmac_failed_etm_disabled; |
| 1602 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1603 | memcpy(mac_peer, data + rec->data_len, transform->maclen); |
| 1604 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1605 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 1606 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 1607 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1608 | if (transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0) { |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1609 | /* |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 1610 | * The next two sizes are the minimum and maximum values of |
Manuel Pégourié-Gonnard | 7fe2c5f | 2020-08-18 12:02:54 +0200 | [diff] [blame] | 1611 | * data_len over all padlen values. |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 1612 | * |
| 1613 | * They're independent of padlen, since we previously did |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1614 | * data_len -= padlen. |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 1615 | * |
| 1616 | * Note that max_len + maclen is never more than the buffer |
| 1617 | * length, as we previously did in_msglen -= maclen too. |
| 1618 | */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1619 | const size_t max_len = rec->data_len + padlen; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1620 | const size_t min_len = (max_len > 256) ? max_len - 256 : 0; |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 1621 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1622 | ret = mbedtls_ct_hmac(&transform->md_ctx_dec, |
| 1623 | add_data, add_data_len, |
| 1624 | data, rec->data_len, min_len, max_len, |
| 1625 | mac_expect); |
| 1626 | if (ret != 0) { |
| 1627 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ct_hmac", ret); |
Gilles Peskine | d8e2e83 | 2021-12-10 21:33:21 +0100 | [diff] [blame] | 1628 | goto hmac_failed_etm_disabled; |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 1629 | } |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 1630 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1631 | mbedtls_ct_memcpy_offset(mac_peer, data, |
| 1632 | rec->data_len, |
| 1633 | min_len, max_len, |
| 1634 | transform->maclen); |
| 1635 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1636 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 1637 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1638 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1639 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1640 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1641 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1642 | |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 1643 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1644 | MBEDTLS_SSL_DEBUG_BUF(4, "expected mac", mac_expect, transform->maclen); |
| 1645 | MBEDTLS_SSL_DEBUG_BUF(4, "message mac", mac_peer, transform->maclen); |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 1646 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1647 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1648 | if (mbedtls_ct_memcmp(mac_peer, mac_expect, |
| 1649 | transform->maclen) != 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1650 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1651 | MBEDTLS_SSL_DEBUG_MSG(1, ("message mac does not match")); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 1652 | #endif |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1653 | correct = 0; |
| 1654 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1655 | auth_done++; |
Gilles Peskine | d8e2e83 | 2021-12-10 21:33:21 +0100 | [diff] [blame] | 1656 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1657 | hmac_failed_etm_disabled: |
| 1658 | mbedtls_platform_zeroize(mac_peer, transform->maclen); |
| 1659 | mbedtls_platform_zeroize(mac_expect, transform->maclen); |
| 1660 | if (ret != 0) { |
| 1661 | return ret; |
| 1662 | } |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1663 | } |
Hanno Becker | dd3ab13 | 2018-10-17 14:43:14 +0100 | [diff] [blame] | 1664 | |
| 1665 | /* |
| 1666 | * Finally check the correct flag |
| 1667 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1668 | if (correct == 0) { |
| 1669 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
| 1670 | } |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 1671 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1672 | |
| 1673 | /* Make extra sure authentication was performed, exactly once */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1674 | if (auth_done != 1) { |
| 1675 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1676 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1677 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1678 | |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 1679 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1680 | if (transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4) { |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 1681 | /* Remove inner padding and infer true content type. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1682 | ret = ssl_parse_inner_plaintext(data, &rec->data_len, |
| 1683 | &rec->type); |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 1684 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1685 | if (ret != 0) { |
| 1686 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
| 1687 | } |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 1688 | } |
| 1689 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
| 1690 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1691 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1692 | if (rec->cid_len != 0) { |
| 1693 | ret = ssl_parse_inner_plaintext(data, &rec->data_len, |
| 1694 | &rec->type); |
| 1695 | if (ret != 0) { |
| 1696 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
| 1697 | } |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1698 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1699 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1700 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1701 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= decrypt buf")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1702 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1703 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 1706 | #undef MAC_NONE |
| 1707 | #undef MAC_PLAINTEXT |
| 1708 | #undef MAC_CIPHERTEXT |
| 1709 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1710 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1711 | /* |
| 1712 | * Compression/decompression functions |
| 1713 | */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 1714 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1715 | static int ssl_compress_buf(mbedtls_ssl_context *ssl) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1716 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1717 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1718 | unsigned char *msg_post = ssl->out_msg; |
Andrzej Kurek | 5462e02 | 2018-04-20 07:58:53 -0400 | [diff] [blame] | 1719 | ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1720 | size_t len_pre = ssl->out_msglen; |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 1721 | unsigned char *msg_pre = ssl->compress_buf; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1722 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1723 | size_t out_buf_len = ssl->out_buf_len; |
| 1724 | #else |
| 1725 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 1726 | #endif |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1727 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1728 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> compress buf")); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1729 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1730 | if (len_pre == 0) { |
| 1731 | return 0; |
| 1732 | } |
Paul Bakker | abf2f8f | 2013-06-30 14:57:46 +0200 | [diff] [blame] | 1733 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1734 | memcpy(msg_pre, ssl->out_msg, len_pre); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1735 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1736 | MBEDTLS_SSL_DEBUG_MSG(3, ("before compression: msglen = %" MBEDTLS_PRINTF_SIZET ", ", |
| 1737 | ssl->out_msglen)); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1738 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1739 | MBEDTLS_SSL_DEBUG_BUF(4, "before compression: output payload", |
| 1740 | ssl->out_msg, ssl->out_msglen); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1741 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1742 | ssl->transform_out->ctx_deflate.next_in = msg_pre; |
| 1743 | ssl->transform_out->ctx_deflate.avail_in = len_pre; |
| 1744 | ssl->transform_out->ctx_deflate.next_out = msg_post; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1745 | ssl->transform_out->ctx_deflate.avail_out = out_buf_len - bytes_written; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1746 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1747 | ret = deflate(&ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH); |
| 1748 | if (ret != Z_OK) { |
| 1749 | MBEDTLS_SSL_DEBUG_MSG(1, ("failed to perform compression (%d)", ret)); |
| 1750 | return MBEDTLS_ERR_SSL_COMPRESSION_FAILED; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1753 | ssl->out_msglen = out_buf_len - |
Andrzej Kurek | 5462e02 | 2018-04-20 07:58:53 -0400 | [diff] [blame] | 1754 | ssl->transform_out->ctx_deflate.avail_out - bytes_written; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1755 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1756 | MBEDTLS_SSL_DEBUG_MSG(3, ("after compression: msglen = %" MBEDTLS_PRINTF_SIZET ", ", |
| 1757 | ssl->out_msglen)); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1758 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1759 | MBEDTLS_SSL_DEBUG_BUF(4, "after compression: output payload", |
| 1760 | ssl->out_msg, ssl->out_msglen); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1761 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1762 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= compress buf")); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1763 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1764 | return 0; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1765 | } |
| 1766 | |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 1767 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1768 | static int ssl_decompress_buf(mbedtls_ssl_context *ssl) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1769 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1770 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1771 | unsigned char *msg_post = ssl->in_msg; |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 1772 | ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1773 | size_t len_pre = ssl->in_msglen; |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 1774 | unsigned char *msg_pre = ssl->compress_buf; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1775 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1776 | size_t in_buf_len = ssl->in_buf_len; |
| 1777 | #else |
| 1778 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1779 | #endif |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1780 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1781 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> decompress buf")); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1782 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1783 | if (len_pre == 0) { |
| 1784 | return 0; |
| 1785 | } |
Paul Bakker | abf2f8f | 2013-06-30 14:57:46 +0200 | [diff] [blame] | 1786 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1787 | memcpy(msg_pre, ssl->in_msg, len_pre); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1788 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1789 | MBEDTLS_SSL_DEBUG_MSG(3, ("before decompression: msglen = %" MBEDTLS_PRINTF_SIZET ", ", |
| 1790 | ssl->in_msglen)); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1791 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1792 | MBEDTLS_SSL_DEBUG_BUF(4, "before decompression: input payload", |
| 1793 | ssl->in_msg, ssl->in_msglen); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1794 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1795 | ssl->transform_in->ctx_inflate.next_in = msg_pre; |
| 1796 | ssl->transform_in->ctx_inflate.avail_in = len_pre; |
| 1797 | ssl->transform_in->ctx_inflate.next_out = msg_post; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1798 | ssl->transform_in->ctx_inflate.avail_out = in_buf_len - header_bytes; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1799 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1800 | ret = inflate(&ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH); |
| 1801 | if (ret != Z_OK) { |
| 1802 | MBEDTLS_SSL_DEBUG_MSG(1, ("failed to perform decompression (%d)", ret)); |
| 1803 | return MBEDTLS_ERR_SSL_COMPRESSION_FAILED; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1806 | ssl->in_msglen = in_buf_len - |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 1807 | ssl->transform_in->ctx_inflate.avail_out - header_bytes; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1808 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1809 | MBEDTLS_SSL_DEBUG_MSG(3, ("after decompression: msglen = %" MBEDTLS_PRINTF_SIZET ", ", |
| 1810 | ssl->in_msglen)); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1811 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1812 | MBEDTLS_SSL_DEBUG_BUF(4, "after decompression: input payload", |
| 1813 | ssl->in_msg, ssl->in_msglen); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1814 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1815 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= decompress buf")); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1816 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1817 | return 0; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1818 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1819 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1820 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1821 | /* |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 1822 | * Fill the input message buffer by appending data to it. |
| 1823 | * The amount of data already fetched is in ssl->in_left. |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1824 | * |
| 1825 | * If we return 0, is it guaranteed that (at least) nb_want bytes are |
| 1826 | * available (from this read and/or a previous one). Otherwise, an error code |
| 1827 | * is returned (possibly EOF or WANT_READ). |
| 1828 | * |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 1829 | * With stream transport (TLS) on success ssl->in_left == nb_want, but |
| 1830 | * with datagram transport (DTLS) on success ssl->in_left >= nb_want, |
| 1831 | * since we always read a whole datagram at once. |
| 1832 | * |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 1833 | * For DTLS, it is up to the caller to set ssl->next_record_offset when |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 1834 | * they're done reading a record. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1835 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1836 | int mbedtls_ssl_fetch_input(mbedtls_ssl_context *ssl, size_t nb_want) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1837 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1838 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1839 | size_t len; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1840 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1841 | size_t in_buf_len = ssl->in_buf_len; |
| 1842 | #else |
| 1843 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1844 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1845 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1846 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> fetch input")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1847 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1848 | if (ssl->f_recv == NULL && ssl->f_recv_timeout == NULL) { |
| 1849 | MBEDTLS_SSL_DEBUG_MSG(1, ("Bad usage of mbedtls_ssl_set_bio() ")); |
| 1850 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 1851 | } |
| 1852 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1853 | if (nb_want > in_buf_len - (size_t) (ssl->in_hdr - ssl->in_buf)) { |
| 1854 | MBEDTLS_SSL_DEBUG_MSG(1, ("requesting more data than fits")); |
| 1855 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 1856 | } |
| 1857 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1858 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1859 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 1860 | uint32_t timeout; |
| 1861 | |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 1862 | /* |
| 1863 | * The point is, we need to always read a full datagram at once, so we |
| 1864 | * sometimes read more then requested, and handle the additional data. |
| 1865 | * It could be the rest of the current record (while fetching the |
| 1866 | * header) and/or some other records in the same datagram. |
| 1867 | */ |
| 1868 | |
| 1869 | /* |
| 1870 | * Move to the next record in the already read datagram if applicable |
| 1871 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1872 | if (ssl->next_record_offset != 0) { |
| 1873 | if (ssl->in_left < ssl->next_record_offset) { |
| 1874 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1875 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 1876 | } |
| 1877 | |
| 1878 | ssl->in_left -= ssl->next_record_offset; |
| 1879 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1880 | if (ssl->in_left != 0) { |
| 1881 | MBEDTLS_SSL_DEBUG_MSG(2, ("next record in same datagram, offset: %" |
| 1882 | MBEDTLS_PRINTF_SIZET, |
| 1883 | ssl->next_record_offset)); |
| 1884 | memmove(ssl->in_hdr, |
| 1885 | ssl->in_hdr + ssl->next_record_offset, |
| 1886 | ssl->in_left); |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 1887 | } |
| 1888 | |
| 1889 | ssl->next_record_offset = 0; |
| 1890 | } |
| 1891 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1892 | MBEDTLS_SSL_DEBUG_MSG(2, ("in_left: %" MBEDTLS_PRINTF_SIZET |
| 1893 | ", nb_want: %" MBEDTLS_PRINTF_SIZET, |
| 1894 | ssl->in_left, nb_want)); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1895 | |
| 1896 | /* |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 1897 | * Done if we already have enough data. |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1898 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1899 | if (nb_want <= ssl->in_left) { |
| 1900 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= fetch input")); |
| 1901 | return 0; |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 1902 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1903 | |
| 1904 | /* |
Antonin Décimo | 36e89b5 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 1905 | * A record can't be split across datagrams. If we need to read but |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1906 | * are not at the beginning of a new record, the caller did something |
| 1907 | * wrong. |
| 1908 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1909 | if (ssl->in_left != 0) { |
| 1910 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1911 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1912 | } |
| 1913 | |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1914 | /* |
| 1915 | * Don't even try to read if time's out already. |
| 1916 | * This avoids by-passing the timer when repeatedly receiving messages |
| 1917 | * that will end up being dropped. |
| 1918 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1919 | if (mbedtls_ssl_check_timer(ssl) != 0) { |
| 1920 | MBEDTLS_SSL_DEBUG_MSG(2, ("timer has expired")); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 1921 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1922 | } else { |
| 1923 | len = in_buf_len - (ssl->in_hdr - ssl->in_buf); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1924 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1925 | if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1926 | timeout = ssl->handshake->retransmit_timeout; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1927 | } else { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1928 | timeout = ssl->conf->read_timeout; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1929 | } |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1930 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1931 | MBEDTLS_SSL_DEBUG_MSG(3, ("f_recv_timeout: %lu ms", (unsigned long) timeout)); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1932 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1933 | if (ssl->f_recv_timeout != NULL) { |
| 1934 | ret = ssl->f_recv_timeout(ssl->p_bio, ssl->in_hdr, len, |
| 1935 | timeout); |
| 1936 | } else { |
| 1937 | ret = ssl->f_recv(ssl->p_bio, ssl->in_hdr, len); |
| 1938 | } |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1939 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1940 | MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_recv(_timeout)", ret); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1941 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1942 | if (ret == 0) { |
| 1943 | return MBEDTLS_ERR_SSL_CONN_EOF; |
| 1944 | } |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1945 | } |
| 1946 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1947 | if (ret == MBEDTLS_ERR_SSL_TIMEOUT) { |
| 1948 | MBEDTLS_SSL_DEBUG_MSG(2, ("timeout")); |
| 1949 | mbedtls_ssl_set_timer(ssl, 0); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 1950 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1951 | if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 1952 | if (ssl_double_retransmit_timeout(ssl) != 0) { |
| 1953 | MBEDTLS_SSL_DEBUG_MSG(1, ("handshake timeout")); |
| 1954 | return MBEDTLS_ERR_SSL_TIMEOUT; |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 1955 | } |
| 1956 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1957 | if ((ret = mbedtls_ssl_resend(ssl)) != 0) { |
| 1958 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend", ret); |
| 1959 | return ret; |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 1960 | } |
| 1961 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1962 | return MBEDTLS_ERR_SSL_WANT_READ; |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 1963 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1964 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1965 | else if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 1966 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) { |
| 1967 | if ((ret = mbedtls_ssl_resend_hello_request(ssl)) != 0) { |
| 1968 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend_hello_request", |
| 1969 | ret); |
| 1970 | return ret; |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 1971 | } |
| 1972 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1973 | return MBEDTLS_ERR_SSL_WANT_READ; |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 1974 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1975 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 1976 | } |
| 1977 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1978 | if (ret < 0) { |
| 1979 | return ret; |
| 1980 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1981 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1982 | ssl->in_left = ret; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1983 | } else |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1984 | #endif |
| 1985 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1986 | MBEDTLS_SSL_DEBUG_MSG(2, ("in_left: %" MBEDTLS_PRINTF_SIZET |
| 1987 | ", nb_want: %" MBEDTLS_PRINTF_SIZET, |
| 1988 | ssl->in_left, nb_want)); |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 1989 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1990 | while (ssl->in_left < nb_want) { |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1991 | len = nb_want - ssl->in_left; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1992 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1993 | if (mbedtls_ssl_check_timer(ssl) != 0) { |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1994 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1995 | } else { |
| 1996 | if (ssl->f_recv_timeout != NULL) { |
| 1997 | ret = ssl->f_recv_timeout(ssl->p_bio, |
| 1998 | ssl->in_hdr + ssl->in_left, len, |
| 1999 | ssl->conf->read_timeout); |
| 2000 | } else { |
| 2001 | ret = ssl->f_recv(ssl->p_bio, |
| 2002 | ssl->in_hdr + ssl->in_left, len); |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 2003 | } |
| 2004 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2005 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2006 | MBEDTLS_SSL_DEBUG_MSG(2, ("in_left: %" MBEDTLS_PRINTF_SIZET |
| 2007 | ", nb_want: %" MBEDTLS_PRINTF_SIZET, |
| 2008 | ssl->in_left, nb_want)); |
| 2009 | MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_recv(_timeout)", ret); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2010 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2011 | if (ret == 0) { |
| 2012 | return MBEDTLS_ERR_SSL_CONN_EOF; |
| 2013 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2014 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2015 | if (ret < 0) { |
| 2016 | return ret; |
| 2017 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2018 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2019 | if ((size_t) ret > len || (INT_MAX > SIZE_MAX && ret > (int) SIZE_MAX)) { |
| 2020 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 2021 | ("f_recv returned %d bytes but only %" MBEDTLS_PRINTF_SIZET |
| 2022 | " were requested", |
| 2023 | ret, len)); |
| 2024 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
mohammad1603 | 5bd15cb | 2018-02-28 04:30:59 -0800 | [diff] [blame] | 2025 | } |
| 2026 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2027 | ssl->in_left += ret; |
| 2028 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2031 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= fetch input")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2032 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2033 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
| 2036 | /* |
| 2037 | * Flush any data not yet written |
| 2038 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2039 | int mbedtls_ssl_flush_output(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2040 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2041 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 2042 | unsigned char *buf; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2043 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2044 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> flush output")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2045 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2046 | if (ssl->f_send == NULL) { |
| 2047 | MBEDTLS_SSL_DEBUG_MSG(1, ("Bad usage of mbedtls_ssl_set_bio() ")); |
| 2048 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 2049 | } |
| 2050 | |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 2051 | /* Avoid incrementing counter if data is flushed */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2052 | if (ssl->out_left == 0) { |
| 2053 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= flush output")); |
| 2054 | return 0; |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 2055 | } |
| 2056 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2057 | while (ssl->out_left > 0) { |
| 2058 | MBEDTLS_SSL_DEBUG_MSG(2, ("message length: %" MBEDTLS_PRINTF_SIZET |
| 2059 | ", out_left: %" MBEDTLS_PRINTF_SIZET, |
| 2060 | mbedtls_ssl_out_hdr_len(ssl) + ssl->out_msglen, ssl->out_left)); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2061 | |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2062 | buf = ssl->out_hdr - ssl->out_left; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2063 | ret = ssl->f_send(ssl->p_bio, buf, ssl->out_left); |
Paul Bakker | 186751d | 2012-05-08 13:16:14 +0000 | [diff] [blame] | 2064 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2065 | MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_send", ret); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2066 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2067 | if (ret <= 0) { |
| 2068 | return ret; |
| 2069 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2070 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2071 | if ((size_t) ret > ssl->out_left || (INT_MAX > SIZE_MAX && ret > (int) SIZE_MAX)) { |
| 2072 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 2073 | ("f_send returned %d bytes but only %" MBEDTLS_PRINTF_SIZET |
| 2074 | " bytes were sent", |
| 2075 | ret, ssl->out_left)); |
| 2076 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
mohammad1603 | 4bbaeb4 | 2018-02-22 04:29:04 -0800 | [diff] [blame] | 2077 | } |
| 2078 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2079 | ssl->out_left -= ret; |
| 2080 | } |
| 2081 | |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2082 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2083 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2084 | ssl->out_hdr = ssl->out_buf; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2085 | } else |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2086 | #endif |
| 2087 | { |
| 2088 | ssl->out_hdr = ssl->out_buf + 8; |
| 2089 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2090 | mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out); |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 2091 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2092 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= flush output")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2093 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2094 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2095 | } |
| 2096 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2097 | /* |
| 2098 | * Functions to handle the DTLS retransmission state machine |
| 2099 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2100 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2101 | /* |
| 2102 | * Append current handshake message to current outgoing flight |
| 2103 | */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 2104 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2105 | static int ssl_flight_append(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2106 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2107 | mbedtls_ssl_flight_item *msg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2108 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_flight_append")); |
| 2109 | MBEDTLS_SSL_DEBUG_BUF(4, "message appended to flight", |
| 2110 | ssl->out_msg, ssl->out_msglen); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2111 | |
| 2112 | /* Allocate space for current message */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2113 | if ((msg = mbedtls_calloc(1, sizeof(mbedtls_ssl_flight_item))) == NULL) { |
| 2114 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc %" MBEDTLS_PRINTF_SIZET " bytes failed", |
| 2115 | sizeof(mbedtls_ssl_flight_item))); |
| 2116 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2117 | } |
| 2118 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2119 | if ((msg->p = mbedtls_calloc(1, ssl->out_msglen)) == NULL) { |
| 2120 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc %" MBEDTLS_PRINTF_SIZET " bytes failed", |
| 2121 | ssl->out_msglen)); |
| 2122 | mbedtls_free(msg); |
| 2123 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2124 | } |
| 2125 | |
| 2126 | /* Copy current handshake message with headers */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2127 | memcpy(msg->p, ssl->out_msg, ssl->out_msglen); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2128 | msg->len = ssl->out_msglen; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2129 | msg->type = ssl->out_msgtype; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2130 | msg->next = NULL; |
| 2131 | |
| 2132 | /* Append to the current flight */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2133 | if (ssl->handshake->flight == NULL) { |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2134 | ssl->handshake->flight = msg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2135 | } else { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2136 | mbedtls_ssl_flight_item *cur = ssl->handshake->flight; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2137 | while (cur->next != NULL) { |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2138 | cur = cur->next; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2139 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2140 | cur->next = msg; |
| 2141 | } |
| 2142 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2143 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_flight_append")); |
| 2144 | return 0; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | /* |
| 2148 | * Free the current flight of handshake messages |
| 2149 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2150 | void mbedtls_ssl_flight_free(mbedtls_ssl_flight_item *flight) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2151 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2152 | mbedtls_ssl_flight_item *cur = flight; |
| 2153 | mbedtls_ssl_flight_item *next; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2154 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2155 | while (cur != NULL) { |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2156 | next = cur->next; |
| 2157 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2158 | mbedtls_free(cur->p); |
| 2159 | mbedtls_free(cur); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2160 | |
| 2161 | cur = next; |
| 2162 | } |
| 2163 | } |
| 2164 | |
| 2165 | /* |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2166 | * Swap transform_out and out_ctr with the alternative ones |
| 2167 | */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 2168 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2169 | static int ssl_swap_epochs(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2170 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2171 | mbedtls_ssl_transform *tmp_transform; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2172 | unsigned char tmp_out_ctr[8]; |
| 2173 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2174 | if (ssl->transform_out == ssl->handshake->alt_transform_out) { |
| 2175 | MBEDTLS_SSL_DEBUG_MSG(3, ("skip swap epochs")); |
| 2176 | return 0; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2177 | } |
| 2178 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2179 | MBEDTLS_SSL_DEBUG_MSG(3, ("swap epochs")); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2180 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2181 | /* Swap transforms */ |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2182 | tmp_transform = ssl->transform_out; |
| 2183 | ssl->transform_out = ssl->handshake->alt_transform_out; |
| 2184 | ssl->handshake->alt_transform_out = tmp_transform; |
| 2185 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2186 | /* Swap epoch + sequence_number */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2187 | memcpy(tmp_out_ctr, ssl->cur_out_ctr, 8); |
| 2188 | memcpy(ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8); |
| 2189 | memcpy(ssl->handshake->alt_out_ctr, tmp_out_ctr, 8); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2190 | |
| 2191 | /* Adjust to the newly activated transform */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2192 | mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2193 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2194 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2195 | if (mbedtls_ssl_hw_record_activate != NULL) { |
| 2196 | int ret = mbedtls_ssl_hw_record_activate(ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND); |
| 2197 | if (ret != 0) { |
| 2198 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_activate", ret); |
| 2199 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2200 | } |
| 2201 | } |
| 2202 | #endif |
Manuel Pégourié-Gonnard | e07bc20 | 2020-02-26 09:53:42 +0100 | [diff] [blame] | 2203 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2204 | return 0; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2205 | } |
| 2206 | |
| 2207 | /* |
| 2208 | * Retransmit the current flight of messages. |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2209 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2210 | int mbedtls_ssl_resend(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2211 | { |
| 2212 | int ret = 0; |
| 2213 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2214 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_resend")); |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2215 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2216 | ret = mbedtls_ssl_flight_transmit(ssl); |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2217 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2218 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_resend")); |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2219 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2220 | return ret; |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | /* |
| 2224 | * Transmit or retransmit the current flight of messages. |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2225 | * |
| 2226 | * Need to remember the current message in case flush_output returns |
| 2227 | * WANT_WRITE, causing us to exit this function and come back later. |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2228 | * This function must be called until state is no longer SENDING. |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2229 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2230 | int mbedtls_ssl_flight_transmit(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2231 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2232 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2233 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_flight_transmit")); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2234 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2235 | if (ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING) { |
| 2236 | MBEDTLS_SSL_DEBUG_MSG(2, ("initialise flight transmission")); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2237 | |
| 2238 | ssl->handshake->cur_msg = ssl->handshake->flight; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2239 | ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2240 | ret = ssl_swap_epochs(ssl); |
| 2241 | if (ret != 0) { |
| 2242 | return ret; |
| 2243 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2244 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2245 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2246 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2247 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2248 | while (ssl->handshake->cur_msg != NULL) { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2249 | size_t max_frag_len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2250 | const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2251 | |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 2252 | int const is_finished = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2253 | (cur->type == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2254 | cur->p[0] == MBEDTLS_SSL_HS_FINISHED); |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 2255 | |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 2256 | uint8_t const force_flush = ssl->disable_datagram_packing == 1 ? |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2257 | SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH; |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 2258 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2259 | /* Swap epochs before sending Finished: we can't do it after |
| 2260 | * sending ChangeCipherSpec, in case write returns WANT_READ. |
| 2261 | * Must be done before copying, may change out_msg pointer */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2262 | if (is_finished && ssl->handshake->cur_msg_p == (cur->p + 12)) { |
| 2263 | MBEDTLS_SSL_DEBUG_MSG(2, ("swap epochs to send finished message")); |
| 2264 | ret = ssl_swap_epochs(ssl); |
| 2265 | if (ret != 0) { |
| 2266 | return ret; |
| 2267 | } |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2268 | } |
| 2269 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2270 | ret = ssl_get_remaining_payload_in_datagram(ssl); |
| 2271 | if (ret < 0) { |
| 2272 | return ret; |
| 2273 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2274 | max_frag_len = (size_t) ret; |
| 2275 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2276 | /* CCS is copied as is, while HS messages may need fragmentation */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2277 | if (cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) { |
| 2278 | if (max_frag_len == 0) { |
| 2279 | if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) { |
| 2280 | return ret; |
| 2281 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2282 | |
| 2283 | continue; |
| 2284 | } |
| 2285 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2286 | memcpy(ssl->out_msg, cur->p, cur->len); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2287 | ssl->out_msglen = cur->len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2288 | ssl->out_msgtype = cur->type; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2289 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2290 | /* Update position inside current message */ |
| 2291 | ssl->handshake->cur_msg_p += cur->len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2292 | } else { |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2293 | const unsigned char * const p = ssl->handshake->cur_msg_p; |
| 2294 | const size_t hs_len = cur->len - 12; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2295 | const size_t frag_off = p - (cur->p + 12); |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2296 | const size_t rem_len = hs_len - frag_off; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2297 | size_t cur_hs_frag_len, max_hs_frag_len; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2298 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2299 | if ((max_frag_len < 12) || (max_frag_len == 12 && hs_len != 0)) { |
| 2300 | if (is_finished) { |
| 2301 | ret = ssl_swap_epochs(ssl); |
| 2302 | if (ret != 0) { |
| 2303 | return ret; |
| 2304 | } |
Manuel Pégourié-Gonnard | e07bc20 | 2020-02-26 09:53:42 +0100 | [diff] [blame] | 2305 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2306 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2307 | if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) { |
| 2308 | return ret; |
| 2309 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2310 | |
| 2311 | continue; |
| 2312 | } |
| 2313 | max_hs_frag_len = max_frag_len - 12; |
| 2314 | |
| 2315 | cur_hs_frag_len = rem_len > max_hs_frag_len ? |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2316 | max_hs_frag_len : rem_len; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2317 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2318 | if (frag_off == 0 && cur_hs_frag_len != hs_len) { |
| 2319 | MBEDTLS_SSL_DEBUG_MSG(2, ("fragmenting handshake message (%u > %u)", |
| 2320 | (unsigned) cur_hs_frag_len, |
| 2321 | (unsigned) max_hs_frag_len)); |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 2322 | } |
Manuel Pégourié-Gonnard | b747c6c | 2018-08-12 13:28:53 +0200 | [diff] [blame] | 2323 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2324 | /* Messages are stored with handshake headers as if not fragmented, |
| 2325 | * copy beginning of headers then fill fragmentation fields. |
| 2326 | * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2327 | memcpy(ssl->out_msg, cur->p, 6); |
Joe Subbiani | 61f7d73 | 2021-06-24 09:06:23 +0100 | [diff] [blame] | 2328 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2329 | ssl->out_msg[6] = MBEDTLS_BYTE_2(frag_off); |
| 2330 | ssl->out_msg[7] = MBEDTLS_BYTE_1(frag_off); |
| 2331 | ssl->out_msg[8] = MBEDTLS_BYTE_0(frag_off); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2332 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2333 | ssl->out_msg[9] = MBEDTLS_BYTE_2(cur_hs_frag_len); |
| 2334 | ssl->out_msg[10] = MBEDTLS_BYTE_1(cur_hs_frag_len); |
| 2335 | ssl->out_msg[11] = MBEDTLS_BYTE_0(cur_hs_frag_len); |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2336 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2337 | MBEDTLS_SSL_DEBUG_BUF(3, "handshake header", ssl->out_msg, 12); |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2338 | |
Hanno Becker | 3f7b973 | 2018-08-28 09:53:25 +0100 | [diff] [blame] | 2339 | /* Copy the handshake message content and set records fields */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2340 | memcpy(ssl->out_msg + 12, p, cur_hs_frag_len); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2341 | ssl->out_msglen = cur_hs_frag_len + 12; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2342 | ssl->out_msgtype = cur->type; |
| 2343 | |
| 2344 | /* Update position inside current message */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2345 | ssl->handshake->cur_msg_p += cur_hs_frag_len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2346 | } |
| 2347 | |
| 2348 | /* If done with the current message move to the next one if any */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2349 | if (ssl->handshake->cur_msg_p >= cur->p + cur->len) { |
| 2350 | if (cur->next != NULL) { |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2351 | ssl->handshake->cur_msg = cur->next; |
| 2352 | ssl->handshake->cur_msg_p = cur->next->p + 12; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2353 | } else { |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2354 | ssl->handshake->cur_msg = NULL; |
| 2355 | ssl->handshake->cur_msg_p = NULL; |
| 2356 | } |
| 2357 | } |
| 2358 | |
| 2359 | /* Actually send the message out */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2360 | if ((ret = mbedtls_ssl_write_record(ssl, force_flush)) != 0) { |
| 2361 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret); |
| 2362 | return ret; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2363 | } |
| 2364 | } |
| 2365 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2366 | if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) { |
| 2367 | return ret; |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2368 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2369 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2370 | /* Update state and set timer */ |
| 2371 | if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 2372 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
| 2373 | } else { |
| 2374 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
| 2375 | mbedtls_ssl_set_timer(ssl, ssl->handshake->retransmit_timeout); |
| 2376 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2377 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2378 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_flight_transmit")); |
| 2379 | |
| 2380 | return 0; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2381 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2382 | |
| 2383 | /* |
| 2384 | * To be called when the last message of an incoming flight is received. |
| 2385 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2386 | void mbedtls_ssl_recv_flight_completed(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2387 | { |
| 2388 | /* We won't need to resend that one any more */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2389 | mbedtls_ssl_flight_free(ssl->handshake->flight); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2390 | ssl->handshake->flight = NULL; |
| 2391 | ssl->handshake->cur_msg = NULL; |
| 2392 | |
| 2393 | /* The next incoming flight will start with this msg_seq */ |
| 2394 | ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq; |
| 2395 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 2396 | /* We don't want to remember CCS's across flight boundaries. */ |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 2397 | ssl->handshake->buffering.seen_ccs = 0; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 2398 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 2399 | /* Clear future message buffering structure. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2400 | mbedtls_ssl_buffering_free(ssl); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 2401 | |
Manuel Pégourié-Gonnard | 6c1fa3a | 2014-10-01 16:58:16 +0200 | [diff] [blame] | 2402 | /* Cancel timer */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2403 | mbedtls_ssl_set_timer(ssl, 0); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2404 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2405 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2406 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2407 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2408 | } else { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2409 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2410 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2411 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2412 | |
| 2413 | /* |
| 2414 | * To be called when the last message of an outgoing flight is send. |
| 2415 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2416 | void mbedtls_ssl_send_flight_completed(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2417 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2418 | ssl_reset_retransmit_timeout(ssl); |
| 2419 | mbedtls_ssl_set_timer(ssl, ssl->handshake->retransmit_timeout); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2420 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2421 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2422 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2423 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2424 | } else { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2425 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2426 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2427 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2428 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2429 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2430 | /* |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2431 | * Handshake layer functions |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2432 | */ |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2433 | |
| 2434 | /* |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2435 | * Write (DTLS: or queue) current handshake (including CCS) message. |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2436 | * |
| 2437 | * - fill in handshake headers |
| 2438 | * - update handshake checksum |
| 2439 | * - DTLS: save message for resending |
| 2440 | * - then pass to the record layer |
| 2441 | * |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2442 | * DTLS: except for HelloRequest, messages are only queued, and will only be |
| 2443 | * actually sent when calling flight_transmit() or resend(). |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2444 | * |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2445 | * Inputs: |
| 2446 | * - ssl->out_msglen: 4 + actual handshake message len |
| 2447 | * (4 is the size of handshake headers for TLS) |
| 2448 | * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc) |
| 2449 | * - ssl->out_msg + 4: the handshake message body |
| 2450 | * |
Manuel Pégourié-Gonnard | 065a2a3 | 2018-08-20 11:09:26 +0200 | [diff] [blame] | 2451 | * Outputs, ie state before passing to flight_append() or write_record(): |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2452 | * - ssl->out_msglen: the length of the record contents |
| 2453 | * (including handshake headers but excluding record headers) |
| 2454 | * - ssl->out_msg: the record contents (handshake headers + content) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2455 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2456 | int mbedtls_ssl_write_handshake_msg(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2457 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2458 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2459 | const size_t hs_len = ssl->out_msglen - 4; |
| 2460 | const unsigned char hs_type = ssl->out_msg[0]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2461 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2462 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write handshake message")); |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2463 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2464 | /* |
| 2465 | * Sanity checks |
| 2466 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2467 | if (ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2468 | ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) { |
Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 2469 | /* In SSLv3, the client might send a NoCertificate alert. */ |
| 2470 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2471 | if (!(ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 && |
| 2472 | ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT && |
| 2473 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT)) |
Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 2474 | #endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */ |
| 2475 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2476 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 2477 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 2478 | } |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2479 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2480 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 2481 | /* Whenever we send anything different from a |
| 2482 | * HelloRequest we should be in a handshake - double check. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2483 | if (!(ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2484 | hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST) && |
| 2485 | ssl->handshake == NULL) { |
| 2486 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 2487 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2488 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2489 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2490 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2491 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2492 | ssl->handshake != NULL && |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2493 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) { |
| 2494 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 2495 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2496 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2497 | #endif |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2498 | |
Hanno Becker | b50a253 | 2018-08-06 11:52:54 +0100 | [diff] [blame] | 2499 | /* Double-check that we did not exceed the bounds |
| 2500 | * of the outgoing record buffer. |
| 2501 | * This should never fail as the various message |
| 2502 | * writing functions must obey the bounds of the |
| 2503 | * outgoing record buffer, but better be safe. |
| 2504 | * |
| 2505 | * Note: We deliberately do not check for the MTU or MFL here. |
| 2506 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2507 | if (ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN) { |
| 2508 | MBEDTLS_SSL_DEBUG_MSG(1, ("Record too large: " |
| 2509 | "size %" MBEDTLS_PRINTF_SIZET |
| 2510 | ", maximum %" MBEDTLS_PRINTF_SIZET, |
| 2511 | ssl->out_msglen, |
| 2512 | (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN)); |
| 2513 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | b50a253 | 2018-08-06 11:52:54 +0100 | [diff] [blame] | 2514 | } |
| 2515 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2516 | /* |
| 2517 | * Fill handshake headers |
| 2518 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2519 | if (ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 2520 | ssl->out_msg[1] = MBEDTLS_BYTE_2(hs_len); |
| 2521 | ssl->out_msg[2] = MBEDTLS_BYTE_1(hs_len); |
| 2522 | ssl->out_msg[3] = MBEDTLS_BYTE_0(hs_len); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2523 | |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 2524 | /* |
| 2525 | * DTLS has additional fields in the Handshake layer, |
| 2526 | * between the length field and the actual payload: |
| 2527 | * uint16 message_seq; |
| 2528 | * uint24 fragment_offset; |
| 2529 | * uint24 fragment_length; |
| 2530 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2531 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2532 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 2533 | /* Make room for the additional DTLS fields */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2534 | if (MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8) { |
| 2535 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS handshake message too large: " |
| 2536 | "size %" MBEDTLS_PRINTF_SIZET ", maximum %" |
| 2537 | MBEDTLS_PRINTF_SIZET, |
| 2538 | hs_len, |
| 2539 | (size_t) (MBEDTLS_SSL_OUT_CONTENT_LEN - 12))); |
| 2540 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | 9648f8b | 2017-09-18 10:55:54 +0100 | [diff] [blame] | 2541 | } |
| 2542 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2543 | memmove(ssl->out_msg + 12, ssl->out_msg + 4, hs_len); |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 2544 | ssl->out_msglen += 8; |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 2545 | |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 2546 | /* Write message_seq and update it, except for HelloRequest */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2547 | if (hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST) { |
| 2548 | MBEDTLS_PUT_UINT16_BE(ssl->handshake->out_msg_seq, ssl->out_msg, 4); |
| 2549 | ++(ssl->handshake->out_msg_seq); |
| 2550 | } else { |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 2551 | ssl->out_msg[4] = 0; |
| 2552 | ssl->out_msg[5] = 0; |
| 2553 | } |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 2554 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2555 | /* Handshake hashes are computed without fragmentation, |
| 2556 | * so set frag_offset = 0 and frag_len = hs_len for now */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2557 | memset(ssl->out_msg + 6, 0x00, 3); |
| 2558 | memcpy(ssl->out_msg + 9, ssl->out_msg + 1, 3); |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 2559 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2560 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 2561 | |
Hanno Becker | 0207e53 | 2018-08-28 10:28:28 +0100 | [diff] [blame] | 2562 | /* Update running hashes of handshake messages seen */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2563 | if (hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST) { |
| 2564 | ssl->handshake->update_checksum(ssl, ssl->out_msg, ssl->out_msglen); |
| 2565 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2566 | } |
| 2567 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2568 | /* Either send now, or just save to be sent (and resent) later */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2569 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2570 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 2571 | !(ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2572 | hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST)) { |
| 2573 | if ((ret = ssl_flight_append(ssl)) != 0) { |
| 2574 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_flight_append", ret); |
| 2575 | return ret; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2576 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2577 | } else |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2578 | #endif |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2579 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2580 | if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) { |
| 2581 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_record", ret); |
| 2582 | return ret; |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2583 | } |
| 2584 | } |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2585 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2586 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write handshake message")); |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2587 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2588 | return 0; |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2589 | } |
| 2590 | |
| 2591 | /* |
| 2592 | * Record layer functions |
| 2593 | */ |
| 2594 | |
| 2595 | /* |
| 2596 | * Write current record. |
| 2597 | * |
| 2598 | * Uses: |
| 2599 | * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS) |
| 2600 | * - ssl->out_msglen: length of the record content (excl headers) |
| 2601 | * - ssl->out_msg: record content |
| 2602 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2603 | int mbedtls_ssl_write_record(mbedtls_ssl_context *ssl, uint8_t force_flush) |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2604 | { |
| 2605 | int ret, done = 0; |
| 2606 | size_t len = ssl->out_msglen; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2607 | uint8_t flush = force_flush; |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2608 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2609 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write record")); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2610 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2611 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2612 | if (ssl->transform_out != NULL && |
| 2613 | ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE) { |
| 2614 | if ((ret = ssl_compress_buf(ssl)) != 0) { |
| 2615 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_compress_buf", ret); |
| 2616 | return ret; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2617 | } |
| 2618 | |
| 2619 | len = ssl->out_msglen; |
| 2620 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2621 | #endif /*MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2622 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2623 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2624 | if (mbedtls_ssl_hw_record_write != NULL) { |
| 2625 | MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_write()")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2626 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2627 | ret = mbedtls_ssl_hw_record_write(ssl); |
| 2628 | if (ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH) { |
| 2629 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_write", ret); |
| 2630 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2631 | } |
Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 2632 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2633 | if (ret == 0) { |
Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 2634 | done = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2635 | } |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2636 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2637 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2638 | if (!done) { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2639 | unsigned i; |
| 2640 | size_t protected_record_size; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 2641 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 2642 | size_t out_buf_len = ssl->out_buf_len; |
| 2643 | #else |
| 2644 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 2645 | #endif |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 2646 | /* Skip writing the record content type to after the encryption, |
| 2647 | * as it may change when using the CID extension. */ |
| 2648 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2649 | mbedtls_ssl_write_version(ssl->major_ver, ssl->minor_ver, |
| 2650 | ssl->conf->transport, ssl->out_hdr + 1); |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 2651 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2652 | memcpy(ssl->out_ctr, ssl->cur_out_ctr, 8); |
| 2653 | MBEDTLS_PUT_UINT16_BE(len, ssl->out_len, 0); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2654 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2655 | if (ssl->transform_out != NULL) { |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2656 | mbedtls_record rec; |
| 2657 | |
| 2658 | rec.buf = ssl->out_iv; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2659 | rec.buf_len = out_buf_len - (ssl->out_iv - ssl->out_buf); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2660 | rec.data_len = ssl->out_msglen; |
| 2661 | rec.data_offset = ssl->out_msg - rec.buf; |
| 2662 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2663 | memcpy(&rec.ctr[0], ssl->out_ctr, 8); |
| 2664 | mbedtls_ssl_write_version(ssl->major_ver, ssl->minor_ver, |
| 2665 | ssl->conf->transport, rec.ver); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2666 | rec.type = ssl->out_msgtype; |
| 2667 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2668 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 2669 | /* The CID is set by mbedtls_ssl_encrypt_buf(). */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2670 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2671 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2672 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2673 | if ((ret = mbedtls_ssl_encrypt_buf(ssl, ssl->transform_out, &rec, |
| 2674 | ssl->conf->f_rng, ssl->conf->p_rng)) != 0) { |
| 2675 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_encrypt_buf", ret); |
| 2676 | return ret; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2677 | } |
| 2678 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2679 | if (rec.data_offset != 0) { |
| 2680 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 2681 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2682 | } |
| 2683 | |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 2684 | /* Update the record content type and CID. */ |
| 2685 | ssl->out_msgtype = rec.type; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2686 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 2687 | memcpy(ssl->out_cid, rec.cid, rec.cid_len); |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2688 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 78f839d | 2019-03-14 12:56:23 +0000 | [diff] [blame] | 2689 | ssl->out_msglen = len = rec.data_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2690 | MBEDTLS_PUT_UINT16_BE(rec.data_len, ssl->out_len, 0); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2691 | } |
| 2692 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2693 | protected_record_size = len + mbedtls_ssl_out_hdr_len(ssl); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2694 | |
| 2695 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2696 | /* In case of DTLS, double-check that we don't exceed |
| 2697 | * the remaining space in the datagram. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2698 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 2699 | ret = ssl_get_remaining_space_in_datagram(ssl); |
| 2700 | if (ret < 0) { |
| 2701 | return ret; |
| 2702 | } |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2703 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2704 | if (protected_record_size > (size_t) ret) { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2705 | /* Should never happen */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2706 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2707 | } |
| 2708 | } |
| 2709 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2710 | |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 2711 | /* Now write the potentially updated record content type. */ |
| 2712 | ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype; |
| 2713 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2714 | MBEDTLS_SSL_DEBUG_MSG(3, ("output record: msgtype = %u, " |
| 2715 | "version = [%u:%u], msglen = %" MBEDTLS_PRINTF_SIZET, |
| 2716 | ssl->out_hdr[0], ssl->out_hdr[1], |
| 2717 | ssl->out_hdr[2], len)); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2718 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2719 | MBEDTLS_SSL_DEBUG_BUF(4, "output record sent to network", |
| 2720 | ssl->out_hdr, protected_record_size); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2721 | |
| 2722 | ssl->out_left += protected_record_size; |
| 2723 | ssl->out_hdr += protected_record_size; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2724 | mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2725 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2726 | for (i = 8; i > mbedtls_ssl_ep_len(ssl); i--) { |
| 2727 | if (++ssl->cur_out_ctr[i - 1] != 0) { |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 2728 | break; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2729 | } |
| 2730 | } |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 2731 | |
| 2732 | /* The loop goes to its end iff the counter is wrapping */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2733 | if (i == mbedtls_ssl_ep_len(ssl)) { |
| 2734 | MBEDTLS_SSL_DEBUG_MSG(1, ("outgoing message counter would wrap")); |
| 2735 | return MBEDTLS_ERR_SSL_COUNTER_WRAPPING; |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 2736 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2737 | } |
| 2738 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2739 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2740 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 2741 | flush == SSL_DONT_FORCE_FLUSH) { |
Hanno Becker | 1f5a15d | 2018-08-21 13:31:31 +0100 | [diff] [blame] | 2742 | size_t remaining; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2743 | ret = ssl_get_remaining_payload_in_datagram(ssl); |
| 2744 | if (ret < 0) { |
| 2745 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_remaining_payload_in_datagram", |
| 2746 | ret); |
| 2747 | return ret; |
Hanno Becker | 1f5a15d | 2018-08-21 13:31:31 +0100 | [diff] [blame] | 2748 | } |
| 2749 | |
| 2750 | remaining = (size_t) ret; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2751 | if (remaining == 0) { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2752 | flush = SSL_FORCE_FLUSH; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2753 | } else { |
| 2754 | MBEDTLS_SSL_DEBUG_MSG(2, |
| 2755 | ("Still %u bytes available in current datagram", |
| 2756 | (unsigned) remaining)); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2757 | } |
| 2758 | } |
| 2759 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 2760 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2761 | if ((flush == SSL_FORCE_FLUSH) && |
| 2762 | (ret = mbedtls_ssl_flush_output(ssl)) != 0) { |
| 2763 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flush_output", ret); |
| 2764 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2765 | } |
| 2766 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2767 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write record")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2768 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2769 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2770 | } |
| 2771 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2772 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 2773 | |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 2774 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2775 | static int ssl_hs_is_proper_fragment(mbedtls_ssl_context *ssl) |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 2776 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2777 | if (ssl->in_msglen < ssl->in_hslen || |
| 2778 | memcmp(ssl->in_msg + 6, "\0\0\0", 3) != 0 || |
| 2779 | memcmp(ssl->in_msg + 9, ssl->in_msg + 1, 3) != 0) { |
| 2780 | return 1; |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 2781 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2782 | return 0; |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 2783 | } |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2784 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2785 | static uint32_t ssl_get_hs_frag_len(mbedtls_ssl_context const *ssl) |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2786 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2787 | return (ssl->in_msg[9] << 16) | |
| 2788 | (ssl->in_msg[10] << 8) | |
| 2789 | ssl->in_msg[11]; |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2790 | } |
| 2791 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2792 | static uint32_t ssl_get_hs_frag_off(mbedtls_ssl_context const *ssl) |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2793 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2794 | return (ssl->in_msg[6] << 16) | |
| 2795 | (ssl->in_msg[7] << 8) | |
| 2796 | ssl->in_msg[8]; |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2797 | } |
| 2798 | |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 2799 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2800 | static int ssl_check_hs_header(mbedtls_ssl_context const *ssl) |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2801 | { |
| 2802 | uint32_t msg_len, frag_off, frag_len; |
| 2803 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2804 | msg_len = ssl_get_hs_total_len(ssl); |
| 2805 | frag_off = ssl_get_hs_frag_off(ssl); |
| 2806 | frag_len = ssl_get_hs_frag_len(ssl); |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2807 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2808 | if (frag_off > msg_len) { |
| 2809 | return -1; |
| 2810 | } |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2811 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2812 | if (frag_len > msg_len - frag_off) { |
| 2813 | return -1; |
| 2814 | } |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2815 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2816 | if (frag_len + 12 > ssl->in_msglen) { |
| 2817 | return -1; |
| 2818 | } |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2819 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2820 | return 0; |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2821 | } |
| 2822 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2823 | /* |
| 2824 | * Mark bits in bitmask (used for DTLS HS reassembly) |
| 2825 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2826 | static void ssl_bitmask_set(unsigned char *mask, size_t offset, size_t len) |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2827 | { |
| 2828 | unsigned int start_bits, end_bits; |
| 2829 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2830 | start_bits = 8 - (offset % 8); |
| 2831 | if (start_bits != 8) { |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2832 | size_t first_byte_idx = offset / 8; |
| 2833 | |
Manuel Pégourié-Gonnard | ac03052 | 2014-09-02 14:23:40 +0200 | [diff] [blame] | 2834 | /* Special case */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2835 | if (len <= start_bits) { |
| 2836 | for (; len != 0; len--) { |
| 2837 | mask[first_byte_idx] |= 1 << (start_bits - len); |
| 2838 | } |
Manuel Pégourié-Gonnard | ac03052 | 2014-09-02 14:23:40 +0200 | [diff] [blame] | 2839 | |
| 2840 | /* Avoid potential issues with offset or len becoming invalid */ |
| 2841 | return; |
| 2842 | } |
| 2843 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2844 | offset += start_bits; /* Now offset % 8 == 0 */ |
| 2845 | len -= start_bits; |
| 2846 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2847 | for (; start_bits != 0; start_bits--) { |
| 2848 | mask[first_byte_idx] |= 1 << (start_bits - 1); |
| 2849 | } |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2850 | } |
| 2851 | |
| 2852 | end_bits = len % 8; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2853 | if (end_bits != 0) { |
| 2854 | size_t last_byte_idx = (offset + len) / 8; |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2855 | |
| 2856 | len -= end_bits; /* Now len % 8 == 0 */ |
| 2857 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2858 | for (; end_bits != 0; end_bits--) { |
| 2859 | mask[last_byte_idx] |= 1 << (8 - end_bits); |
| 2860 | } |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2861 | } |
| 2862 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2863 | memset(mask + offset / 8, 0xFF, len / 8); |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2864 | } |
| 2865 | |
| 2866 | /* |
| 2867 | * Check that bitmask is full |
| 2868 | */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 2869 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2870 | static int ssl_bitmask_check(unsigned char *mask, size_t len) |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2871 | { |
| 2872 | size_t i; |
| 2873 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2874 | for (i = 0; i < len / 8; i++) { |
| 2875 | if (mask[i] != 0xFF) { |
| 2876 | return -1; |
| 2877 | } |
| 2878 | } |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2879 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2880 | for (i = 0; i < len % 8; i++) { |
| 2881 | if ((mask[len / 8] & (1 << (7 - i))) == 0) { |
| 2882 | return -1; |
| 2883 | } |
| 2884 | } |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2885 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2886 | return 0; |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2887 | } |
| 2888 | |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 2889 | /* msg_len does not include the handshake header */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2890 | static size_t ssl_get_reassembly_buffer_size(size_t msg_len, |
| 2891 | unsigned add_bitmap) |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2892 | { |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 2893 | size_t alloc_len; |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2894 | |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 2895 | alloc_len = 12; /* Handshake header */ |
| 2896 | alloc_len += msg_len; /* Content buffer */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2897 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2898 | if (add_bitmap) { |
| 2899 | alloc_len += msg_len / 8 + (msg_len % 8 != 0); /* Bitmap */ |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2900 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2901 | } |
| 2902 | return alloc_len; |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2903 | } |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 2904 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2905 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2906 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2907 | static uint32_t ssl_get_hs_total_len(mbedtls_ssl_context const *ssl) |
Hanno Becker | 12555c6 | 2018-08-16 12:47:53 +0100 | [diff] [blame] | 2908 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2909 | return (ssl->in_msg[1] << 16) | |
| 2910 | (ssl->in_msg[2] << 8) | |
| 2911 | ssl->in_msg[3]; |
Hanno Becker | 12555c6 | 2018-08-16 12:47:53 +0100 | [diff] [blame] | 2912 | } |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 2913 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2914 | int mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 2915 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2916 | if (ssl->in_msglen < mbedtls_ssl_hs_hdr_len(ssl)) { |
| 2917 | MBEDTLS_SSL_DEBUG_MSG(1, ("handshake message too short: %" MBEDTLS_PRINTF_SIZET, |
| 2918 | ssl->in_msglen)); |
| 2919 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 2920 | } |
| 2921 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2922 | ssl->in_hslen = mbedtls_ssl_hs_hdr_len(ssl) + ssl_get_hs_total_len(ssl); |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 2923 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2924 | MBEDTLS_SSL_DEBUG_MSG(3, ("handshake message: msglen =" |
| 2925 | " %" MBEDTLS_PRINTF_SIZET ", type = %u, hslen = %" |
| 2926 | MBEDTLS_PRINTF_SIZET, |
| 2927 | ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen)); |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 2928 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2929 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2930 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2931 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2932 | unsigned int recv_msg_seq = (ssl->in_msg[4] << 8) | ssl->in_msg[5]; |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2933 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2934 | if (ssl_check_hs_header(ssl) != 0) { |
| 2935 | MBEDTLS_SSL_DEBUG_MSG(1, ("invalid handshake header")); |
| 2936 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2937 | } |
| 2938 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2939 | if (ssl->handshake != NULL && |
| 2940 | ((ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && |
| 2941 | recv_msg_seq != ssl->handshake->in_msg_seq) || |
| 2942 | (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER && |
| 2943 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO))) { |
| 2944 | if (recv_msg_seq > ssl->handshake->in_msg_seq) { |
| 2945 | MBEDTLS_SSL_DEBUG_MSG(2, |
| 2946 | ( |
| 2947 | "received future handshake message of sequence number %u (next %u)", |
| 2948 | recv_msg_seq, |
| 2949 | ssl->handshake->in_msg_seq)); |
| 2950 | return MBEDTLS_ERR_SSL_EARLY_MESSAGE; |
Hanno Becker | 9e1ec22 | 2018-08-15 15:54:43 +0100 | [diff] [blame] | 2951 | } |
| 2952 | |
Manuel Pégourié-Gonnard | fc572dd | 2014-10-09 17:56:57 +0200 | [diff] [blame] | 2953 | /* Retransmit only on last message from previous flight, to avoid |
| 2954 | * too many retransmissions. |
| 2955 | * Besides, No sane server ever retransmits HelloVerifyRequest */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2956 | if (recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 && |
| 2957 | ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST) { |
| 2958 | MBEDTLS_SSL_DEBUG_MSG(2, ("received message from last flight, " |
| 2959 | "message_seq = %u, start_of_flight = %u", |
| 2960 | recv_msg_seq, |
| 2961 | ssl->handshake->in_flight_start_seq)); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 2962 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2963 | if ((ret = mbedtls_ssl_resend(ssl)) != 0) { |
| 2964 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend", ret); |
| 2965 | return ret; |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 2966 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2967 | } else { |
| 2968 | MBEDTLS_SSL_DEBUG_MSG(2, ("dropping out-of-sequence message: " |
| 2969 | "message_seq = %u, expected = %u", |
| 2970 | recv_msg_seq, |
| 2971 | ssl->handshake->in_msg_seq)); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 2972 | } |
| 2973 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2974 | return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 2975 | } |
| 2976 | /* Wait until message completion to increment in_msg_seq */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2977 | |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 2978 | /* Message reassembly is handled alongside buffering of future |
| 2979 | * messages; the commonality is that both handshake fragments and |
Hanno Becker | 83ab41c | 2018-08-28 17:19:38 +0100 | [diff] [blame] | 2980 | * future messages cannot be forwarded immediately to the |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 2981 | * handshake logic layer. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2982 | if (ssl_hs_is_proper_fragment(ssl) == 1) { |
| 2983 | MBEDTLS_SSL_DEBUG_MSG(2, ("found fragmented DTLS handshake message")); |
| 2984 | return MBEDTLS_ERR_SSL_EARLY_MESSAGE; |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2985 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2986 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2987 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2988 | /* With TLS we don't handle fragmentation (for now) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2989 | if (ssl->in_msglen < ssl->in_hslen) { |
| 2990 | MBEDTLS_SSL_DEBUG_MSG(1, ("TLS handshake fragmentation not supported")); |
| 2991 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 2992 | } |
| 2993 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2994 | return 0; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 2995 | } |
| 2996 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2997 | void mbedtls_ssl_update_handshake_status(mbedtls_ssl_context *ssl) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 2998 | { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 2999 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3000 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3001 | if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL) { |
| 3002 | ssl->handshake->update_checksum(ssl, ssl->in_msg, ssl->in_hslen); |
Manuel Pégourié-Gonnard | 14bf706 | 2015-06-23 14:07:13 +0200 | [diff] [blame] | 3003 | } |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3004 | |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3005 | /* Handshake message is complete, increment counter */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3006 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3007 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3008 | ssl->handshake != NULL) { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3009 | unsigned offset; |
| 3010 | mbedtls_ssl_hs_buffer *hs_buf; |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 3011 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3012 | /* Increment handshake sequence number */ |
| 3013 | hs->in_msg_seq++; |
| 3014 | |
| 3015 | /* |
| 3016 | * Clear up handshake buffering and reassembly structure. |
| 3017 | */ |
| 3018 | |
| 3019 | /* Free first entry */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3020 | ssl_buffering_free_slot(ssl, 0); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3021 | |
| 3022 | /* Shift all other entries */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3023 | for (offset = 0, hs_buf = &hs->buffering.hs[0]; |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 3024 | offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3025 | offset++, hs_buf++) { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3026 | *hs_buf = *(hs_buf + 1); |
| 3027 | } |
| 3028 | |
| 3029 | /* Create a fresh last entry */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3030 | memset(hs_buf, 0, sizeof(mbedtls_ssl_hs_buffer)); |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3031 | } |
| 3032 | #endif |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3033 | } |
| 3034 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3035 | /* |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3036 | * DTLS anti-replay: RFC 6347 4.1.2.6 |
| 3037 | * |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3038 | * in_window is a field of bits numbered from 0 (lsb) to 63 (msb). |
| 3039 | * Bit n is set iff record number in_window_top - n has been seen. |
| 3040 | * |
| 3041 | * Usually, in_window_top is the last record number seen and the lsb of |
| 3042 | * in_window is set. The only exception is the initial state (record number 0 |
| 3043 | * not seen yet). |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3044 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3045 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3046 | void mbedtls_ssl_dtls_replay_reset(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3047 | { |
| 3048 | ssl->in_window_top = 0; |
| 3049 | ssl->in_window = 0; |
| 3050 | } |
| 3051 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3052 | static inline uint64_t ssl_load_six_bytes(unsigned char *buf) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3053 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3054 | return ((uint64_t) buf[0] << 40) | |
| 3055 | ((uint64_t) buf[1] << 32) | |
| 3056 | ((uint64_t) buf[2] << 24) | |
| 3057 | ((uint64_t) buf[3] << 16) | |
| 3058 | ((uint64_t) buf[4] << 8) | |
| 3059 | ((uint64_t) buf[5]); |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3060 | } |
| 3061 | |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3062 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3063 | static int mbedtls_ssl_dtls_record_replay_check(mbedtls_ssl_context *ssl, uint8_t *record_in_ctr) |
Arto Kinnunen | 7f8089b | 2019-10-29 11:13:33 +0200 | [diff] [blame] | 3064 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3065 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Arto Kinnunen | 7f8089b | 2019-10-29 11:13:33 +0200 | [diff] [blame] | 3066 | unsigned char *original_in_ctr; |
| 3067 | |
| 3068 | // save original in_ctr |
| 3069 | original_in_ctr = ssl->in_ctr; |
| 3070 | |
| 3071 | // use counter from record |
| 3072 | ssl->in_ctr = record_in_ctr; |
| 3073 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3074 | ret = mbedtls_ssl_dtls_replay_check((mbedtls_ssl_context const *) ssl); |
Arto Kinnunen | 7f8089b | 2019-10-29 11:13:33 +0200 | [diff] [blame] | 3075 | |
| 3076 | // restore the counter |
| 3077 | ssl->in_ctr = original_in_ctr; |
| 3078 | |
| 3079 | return ret; |
| 3080 | } |
| 3081 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3082 | /* |
| 3083 | * Return 0 if sequence number is acceptable, -1 otherwise |
| 3084 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3085 | int mbedtls_ssl_dtls_replay_check(mbedtls_ssl_context const *ssl) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3086 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3087 | uint64_t rec_seqnum = ssl_load_six_bytes(ssl->in_ctr + 2); |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3088 | uint64_t bit; |
| 3089 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3090 | if (ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED) { |
| 3091 | return 0; |
| 3092 | } |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 3093 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3094 | if (rec_seqnum > ssl->in_window_top) { |
| 3095 | return 0; |
| 3096 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3097 | |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3098 | bit = ssl->in_window_top - rec_seqnum; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3099 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3100 | if (bit >= 64) { |
| 3101 | return -1; |
| 3102 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3103 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3104 | if ((ssl->in_window & ((uint64_t) 1 << bit)) != 0) { |
| 3105 | return -1; |
| 3106 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3107 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3108 | return 0; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3109 | } |
| 3110 | |
| 3111 | /* |
| 3112 | * Update replay window on new validated record |
| 3113 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3114 | void mbedtls_ssl_dtls_replay_update(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3115 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3116 | uint64_t rec_seqnum = ssl_load_six_bytes(ssl->in_ctr + 2); |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3117 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3118 | if (ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED) { |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 3119 | return; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3120 | } |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 3121 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3122 | if (rec_seqnum > ssl->in_window_top) { |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3123 | /* Update window_top and the contents of the window */ |
| 3124 | uint64_t shift = rec_seqnum - ssl->in_window_top; |
| 3125 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3126 | if (shift >= 64) { |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3127 | ssl->in_window = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3128 | } else { |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3129 | ssl->in_window <<= shift; |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3130 | ssl->in_window |= 1; |
| 3131 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3132 | |
| 3133 | ssl->in_window_top = rec_seqnum; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3134 | } else { |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3135 | /* Mark that number as seen in the current window */ |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3136 | uint64_t bit = ssl->in_window_top - rec_seqnum; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3137 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3138 | if (bit < 64) { /* Always true, but be extra sure */ |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3139 | ssl->in_window |= (uint64_t) 1 << bit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3140 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3141 | } |
| 3142 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3143 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3144 | |
Manuel Pégourié-Gonnard | ddfe5d2 | 2015-09-09 12:46:16 +0200 | [diff] [blame] | 3145 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3146 | /* |
Gilles Peskine | f333dfa | 2022-02-15 23:53:36 +0100 | [diff] [blame] | 3147 | * Check if a datagram looks like a ClientHello with a valid cookie, |
| 3148 | * and if it doesn't, generate a HelloVerifyRequest message. |
Simon Butcher | 0789aed | 2015-09-11 17:15:17 +0100 | [diff] [blame] | 3149 | * Both input and output include full DTLS headers. |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3150 | * |
| 3151 | * - if cookie is valid, return 0 |
| 3152 | * - if ClientHello looks superficially valid but cookie is not, |
| 3153 | * fill obuf and set olen, then |
| 3154 | * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED |
| 3155 | * - otherwise return a specific error code |
| 3156 | */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3157 | MBEDTLS_CHECK_RETURN_CRITICAL |
Andrzej Kurek | 33f41a8 | 2022-06-08 11:47:33 -0400 | [diff] [blame] | 3158 | MBEDTLS_STATIC_TESTABLE |
| 3159 | int mbedtls_ssl_check_dtls_clihlo_cookie( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3160 | mbedtls_ssl_context *ssl, |
| 3161 | const unsigned char *cli_id, size_t cli_id_len, |
| 3162 | const unsigned char *in, size_t in_len, |
| 3163 | unsigned char *obuf, size_t buf_len, size_t *olen) |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3164 | { |
| 3165 | size_t sid_len, cookie_len; |
| 3166 | unsigned char *p; |
| 3167 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3168 | /* |
| 3169 | * Structure of ClientHello with record and handshake headers, |
| 3170 | * and expected values. We don't need to check a lot, more checks will be |
| 3171 | * done when actually parsing the ClientHello - skipping those checks |
| 3172 | * avoids code duplication and does not make cookie forging any easier. |
| 3173 | * |
| 3174 | * 0-0 ContentType type; copied, must be handshake |
| 3175 | * 1-2 ProtocolVersion version; copied |
| 3176 | * 3-4 uint16 epoch; copied, must be 0 |
| 3177 | * 5-10 uint48 sequence_number; copied |
| 3178 | * 11-12 uint16 length; (ignored) |
| 3179 | * |
| 3180 | * 13-13 HandshakeType msg_type; (ignored) |
| 3181 | * 14-16 uint24 length; (ignored) |
| 3182 | * 17-18 uint16 message_seq; copied |
| 3183 | * 19-21 uint24 fragment_offset; copied, must be 0 |
| 3184 | * 22-24 uint24 fragment_length; (ignored) |
| 3185 | * |
| 3186 | * 25-26 ProtocolVersion client_version; (ignored) |
| 3187 | * 27-58 Random random; (ignored) |
| 3188 | * 59-xx SessionID session_id; 1 byte len + sid_len content |
| 3189 | * 60+ opaque cookie<0..2^8-1>; 1 byte len + content |
| 3190 | * ... |
| 3191 | * |
| 3192 | * Minimum length is 61 bytes. |
| 3193 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3194 | MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: in_len=%u", |
| 3195 | (unsigned) in_len)); |
| 3196 | MBEDTLS_SSL_DEBUG_BUF(4, "cli_id", cli_id, cli_id_len); |
| 3197 | if (in_len < 61) { |
| 3198 | MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: record too short")); |
| 3199 | return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO; |
Gilles Peskine | f333dfa | 2022-02-15 23:53:36 +0100 | [diff] [blame] | 3200 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3201 | if (in[0] != MBEDTLS_SSL_MSG_HANDSHAKE || |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3202 | in[3] != 0 || in[4] != 0 || |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3203 | in[19] != 0 || in[20] != 0 || in[21] != 0) { |
| 3204 | MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: not a good ClientHello")); |
| 3205 | MBEDTLS_SSL_DEBUG_MSG(4, (" type=%u epoch=%u fragment_offset=%u", |
| 3206 | in[0], |
| 3207 | (unsigned) in[3] << 8 | in[4], |
| 3208 | (unsigned) in[19] << 16 | in[20] << 8 | in[21])); |
| 3209 | return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3210 | } |
| 3211 | |
| 3212 | sid_len = in[59]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3213 | if (59 + 1 + sid_len + 1 > in_len) { |
| 3214 | MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: sid_len=%u > %u", |
| 3215 | (unsigned) sid_len, |
| 3216 | (unsigned) in_len - 61)); |
| 3217 | return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO; |
Gilles Peskine | f333dfa | 2022-02-15 23:53:36 +0100 | [diff] [blame] | 3218 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3219 | MBEDTLS_SSL_DEBUG_BUF(4, "sid received from network", |
| 3220 | in + 60, sid_len); |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3221 | |
| 3222 | cookie_len = in[60 + sid_len]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3223 | if (59 + 1 + sid_len + 1 + cookie_len > in_len) { |
| 3224 | MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: cookie_len=%u > %u", |
| 3225 | (unsigned) cookie_len, |
| 3226 | (unsigned) (in_len - sid_len - 61))); |
| 3227 | return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO; |
Gilles Peskine | f333dfa | 2022-02-15 23:53:36 +0100 | [diff] [blame] | 3228 | } |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3229 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3230 | MBEDTLS_SSL_DEBUG_BUF(4, "cookie received from network", |
| 3231 | in + sid_len + 61, cookie_len); |
| 3232 | if (ssl->conf->f_cookie_check(ssl->conf->p_cookie, |
| 3233 | in + sid_len + 61, cookie_len, |
| 3234 | cli_id, cli_id_len) == 0) { |
| 3235 | MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: valid")); |
| 3236 | return 0; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3237 | } |
| 3238 | |
| 3239 | /* |
| 3240 | * If we get here, we've got an invalid cookie, let's prepare HVR. |
| 3241 | * |
| 3242 | * 0-0 ContentType type; copied |
| 3243 | * 1-2 ProtocolVersion version; copied |
| 3244 | * 3-4 uint16 epoch; copied |
| 3245 | * 5-10 uint48 sequence_number; copied |
| 3246 | * 11-12 uint16 length; olen - 13 |
| 3247 | * |
| 3248 | * 13-13 HandshakeType msg_type; hello_verify_request |
| 3249 | * 14-16 uint24 length; olen - 25 |
| 3250 | * 17-18 uint16 message_seq; copied |
| 3251 | * 19-21 uint24 fragment_offset; copied |
| 3252 | * 22-24 uint24 fragment_length; olen - 25 |
| 3253 | * |
| 3254 | * 25-26 ProtocolVersion server_version; 0xfe 0xff |
| 3255 | * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie |
| 3256 | * |
| 3257 | * Minimum length is 28. |
| 3258 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3259 | if (buf_len < 28) { |
| 3260 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 3261 | } |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3262 | |
| 3263 | /* Copy most fields and adapt others */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3264 | memcpy(obuf, in, 25); |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3265 | obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST; |
| 3266 | obuf[25] = 0xfe; |
| 3267 | obuf[26] = 0xff; |
| 3268 | |
| 3269 | /* Generate and write actual cookie */ |
| 3270 | p = obuf + 28; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3271 | if (ssl->conf->f_cookie_write(ssl->conf->p_cookie, |
| 3272 | &p, obuf + buf_len, |
| 3273 | cli_id, cli_id_len) != 0) { |
| 3274 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3275 | } |
| 3276 | |
| 3277 | *olen = p - obuf; |
| 3278 | |
| 3279 | /* Go back and fill length fields */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3280 | obuf[27] = (unsigned char) (*olen - 28); |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3281 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3282 | obuf[14] = obuf[22] = MBEDTLS_BYTE_2(*olen - 25); |
| 3283 | obuf[15] = obuf[23] = MBEDTLS_BYTE_1(*olen - 25); |
| 3284 | obuf[16] = obuf[24] = MBEDTLS_BYTE_0(*olen - 25); |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3285 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3286 | MBEDTLS_PUT_UINT16_BE(*olen - 13, obuf, 11); |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3287 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3288 | return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3289 | } |
| 3290 | |
| 3291 | /* |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3292 | * Handle possible client reconnect with the same UDP quadruplet |
| 3293 | * (RFC 6347 Section 4.2.8). |
| 3294 | * |
| 3295 | * Called by ssl_parse_record_header() in case we receive an epoch 0 record |
| 3296 | * that looks like a ClientHello. |
| 3297 | * |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3298 | * - if the input looks like a ClientHello without cookies, |
Manuel Pégourié-Gonnard | 824655c | 2020-03-11 12:51:42 +0100 | [diff] [blame] | 3299 | * send back HelloVerifyRequest, then return 0 |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3300 | * - if the input looks like a ClientHello with a valid cookie, |
| 3301 | * reset the session of the current context, and |
Manuel Pégourié-Gonnard | be619c1 | 2015-09-08 11:21:21 +0200 | [diff] [blame] | 3302 | * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3303 | * - if anything goes wrong, return a specific error code |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3304 | * |
Manuel Pégourié-Gonnard | 824655c | 2020-03-11 12:51:42 +0100 | [diff] [blame] | 3305 | * This function is called (through ssl_check_client_reconnect()) when an |
| 3306 | * unexpected record is found in ssl_get_next_record(), which will discard the |
| 3307 | * record if we return 0, and bubble up the return value otherwise (this |
| 3308 | * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected |
| 3309 | * errors, and is the right thing to do in both cases). |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3310 | */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3311 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3312 | static int ssl_handle_possible_reconnect(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3313 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3314 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3315 | size_t len; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3316 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3317 | if (ssl->conf->f_cookie_write == NULL || |
| 3318 | ssl->conf->f_cookie_check == NULL) { |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 3319 | /* If we can't use cookies to verify reachability of the peer, |
| 3320 | * drop the record. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3321 | MBEDTLS_SSL_DEBUG_MSG(1, ("no cookie callbacks, " |
| 3322 | "can't check reconnect validity")); |
| 3323 | return 0; |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 3324 | } |
| 3325 | |
Andrzej Kurek | 33f41a8 | 2022-06-08 11:47:33 -0400 | [diff] [blame] | 3326 | ret = mbedtls_ssl_check_dtls_clihlo_cookie( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3327 | ssl, |
| 3328 | ssl->cli_id, ssl->cli_id_len, |
| 3329 | ssl->in_buf, ssl->in_left, |
| 3330 | ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len); |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3331 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3332 | MBEDTLS_SSL_DEBUG_RET(2, "mbedtls_ssl_check_dtls_clihlo_cookie", ret); |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3333 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3334 | if (ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED) { |
Manuel Pégourié-Gonnard | 243d70f | 2020-03-31 12:07:47 +0200 | [diff] [blame] | 3335 | int send_ret; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3336 | MBEDTLS_SSL_DEBUG_MSG(1, ("sending HelloVerifyRequest")); |
| 3337 | MBEDTLS_SSL_DEBUG_BUF(4, "output record sent to network", |
| 3338 | ssl->out_buf, len); |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 3339 | /* Don't check write errors as we can't do anything here. |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3340 | * If the error is permanent we'll catch it later, |
| 3341 | * if it's not, then hopefully it'll work next time. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3342 | send_ret = ssl->f_send(ssl->p_bio, ssl->out_buf, len); |
| 3343 | MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_send", send_ret); |
Manuel Pégourié-Gonnard | 243d70f | 2020-03-31 12:07:47 +0200 | [diff] [blame] | 3344 | (void) send_ret; |
| 3345 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3346 | return 0; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3347 | } |
| 3348 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3349 | if (ret == 0) { |
| 3350 | MBEDTLS_SSL_DEBUG_MSG(1, ("cookie is valid, resetting context")); |
| 3351 | if ((ret = mbedtls_ssl_session_reset_int(ssl, 1)) != 0) { |
| 3352 | MBEDTLS_SSL_DEBUG_RET(1, "reset", ret); |
| 3353 | return ret; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3354 | } |
| 3355 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3356 | return MBEDTLS_ERR_SSL_CLIENT_RECONNECT; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3357 | } |
| 3358 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3359 | return ret; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3360 | } |
Manuel Pégourié-Gonnard | ddfe5d2 | 2015-09-09 12:46:16 +0200 | [diff] [blame] | 3361 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3362 | |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3363 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3364 | static int ssl_check_record_type(uint8_t record_type) |
Hanno Becker | f661c9c | 2019-05-03 13:25:54 +0100 | [diff] [blame] | 3365 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3366 | if (record_type != MBEDTLS_SSL_MSG_HANDSHAKE && |
Hanno Becker | f661c9c | 2019-05-03 13:25:54 +0100 | [diff] [blame] | 3367 | record_type != MBEDTLS_SSL_MSG_ALERT && |
| 3368 | record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC && |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3369 | record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA) { |
| 3370 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | f661c9c | 2019-05-03 13:25:54 +0100 | [diff] [blame] | 3371 | } |
| 3372 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3373 | return 0; |
Hanno Becker | f661c9c | 2019-05-03 13:25:54 +0100 | [diff] [blame] | 3374 | } |
| 3375 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3376 | /* |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3377 | * ContentType type; |
| 3378 | * ProtocolVersion version; |
| 3379 | * uint16 epoch; // DTLS only |
| 3380 | * uint48 sequence_number; // DTLS only |
| 3381 | * uint16 length; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3382 | * |
| 3383 | * Return 0 if header looks sane (and, for DTLS, the record is expected) |
Simon Butcher | 207990d | 2015-12-16 01:51:30 +0000 | [diff] [blame] | 3384 | * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad, |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3385 | * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected. |
| 3386 | * |
| 3387 | * With DTLS, mbedtls_ssl_read_record() will: |
Simon Butcher | 207990d | 2015-12-16 01:51:30 +0000 | [diff] [blame] | 3388 | * 1. proceed with the record if this function returns 0 |
| 3389 | * 2. drop only the current record if this function returns UNEXPECTED_RECORD |
| 3390 | * 3. return CLIENT_RECONNECT if this function return that value |
| 3391 | * 4. drop the whole datagram if this function returns anything else. |
| 3392 | * Point 2 is needed when the peer is resending, and we have already received |
| 3393 | * the first record from a datagram but are still waiting for the others. |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3394 | */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3395 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3396 | static int ssl_parse_record_header(mbedtls_ssl_context const *ssl, |
| 3397 | unsigned char *buf, |
| 3398 | size_t len, |
| 3399 | mbedtls_record *rec) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3400 | { |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 3401 | int major_ver, minor_ver; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3402 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3403 | size_t const rec_hdr_type_offset = 0; |
| 3404 | size_t const rec_hdr_type_len = 1; |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 3405 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3406 | size_t const rec_hdr_version_offset = rec_hdr_type_offset + |
| 3407 | rec_hdr_type_len; |
| 3408 | size_t const rec_hdr_version_len = 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3409 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3410 | size_t const rec_hdr_ctr_len = 8; |
| 3411 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | f546625 | 2019-07-25 10:13:02 +0100 | [diff] [blame] | 3412 | uint32_t rec_epoch; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3413 | size_t const rec_hdr_ctr_offset = rec_hdr_version_offset + |
| 3414 | rec_hdr_version_len; |
| 3415 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3416 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3417 | size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset + |
| 3418 | rec_hdr_ctr_len; |
Hanno Becker | f546625 | 2019-07-25 10:13:02 +0100 | [diff] [blame] | 3419 | size_t rec_hdr_cid_len = 0; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3420 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3421 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3422 | |
| 3423 | size_t rec_hdr_len_offset; /* To be determined */ |
| 3424 | size_t const rec_hdr_len_len = 2; |
| 3425 | |
| 3426 | /* |
| 3427 | * Check minimum lengths for record header. |
| 3428 | */ |
| 3429 | |
| 3430 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3431 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3432 | rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3433 | } else |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3434 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3435 | { |
| 3436 | rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len; |
| 3437 | } |
| 3438 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3439 | if (len < rec_hdr_len_offset + rec_hdr_len_len) { |
| 3440 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 3441 | ( |
| 3442 | "datagram of length %u too small to hold DTLS record header of length %u", |
| 3443 | (unsigned) len, |
| 3444 | (unsigned) (rec_hdr_len_len + rec_hdr_len_len))); |
| 3445 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3446 | } |
| 3447 | |
| 3448 | /* |
| 3449 | * Parse and validate record content type |
| 3450 | */ |
| 3451 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3452 | rec->type = buf[rec_hdr_type_offset]; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3453 | |
| 3454 | /* Check record content type */ |
| 3455 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3456 | rec->cid_len = 0; |
| 3457 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3458 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3459 | ssl->conf->cid_len != 0 && |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3460 | rec->type == MBEDTLS_SSL_MSG_CID) { |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 3461 | /* Shift pointers to account for record header including CID |
| 3462 | * struct { |
| 3463 | * ContentType special_type = tls12_cid; |
| 3464 | * ProtocolVersion version; |
| 3465 | * uint16 epoch; |
| 3466 | * uint48 sequence_number; |
Hanno Becker | 8e55b0f | 2019-05-23 17:03:19 +0100 | [diff] [blame] | 3467 | * opaque cid[cid_length]; // Additional field compared to |
| 3468 | * // default DTLS record format |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 3469 | * uint16 length; |
| 3470 | * opaque enc_content[DTLSCiphertext.length]; |
| 3471 | * } DTLSCiphertext; |
| 3472 | */ |
| 3473 | |
| 3474 | /* So far, we only support static CID lengths |
| 3475 | * fixed in the configuration. */ |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3476 | rec_hdr_cid_len = ssl->conf->cid_len; |
| 3477 | rec_hdr_len_offset += rec_hdr_cid_len; |
Hanno Becker | e538d82 | 2019-07-10 14:50:10 +0100 | [diff] [blame] | 3478 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3479 | if (len < rec_hdr_len_offset + rec_hdr_len_len) { |
| 3480 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 3481 | ( |
| 3482 | "datagram of length %u too small to hold DTLS record header including CID, length %u", |
| 3483 | (unsigned) len, |
| 3484 | (unsigned) (rec_hdr_len_offset + rec_hdr_len_len))); |
| 3485 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | e538d82 | 2019-07-10 14:50:10 +0100 | [diff] [blame] | 3486 | } |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3487 | |
Manuel Pégourié-Gonnard | 7e821b5 | 2019-08-02 10:17:15 +0200 | [diff] [blame] | 3488 | /* configured CID len is guaranteed at most 255, see |
| 3489 | * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */ |
| 3490 | rec->cid_len = (uint8_t) rec_hdr_cid_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3491 | memcpy(rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len); |
| 3492 | } else |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3493 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 3494 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3495 | if (ssl_check_record_type(rec->type)) { |
| 3496 | MBEDTLS_SSL_DEBUG_MSG(1, ("unknown record type %u", |
| 3497 | (unsigned) rec->type)); |
| 3498 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3499 | } |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 3500 | } |
| 3501 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3502 | /* |
| 3503 | * Parse and validate record version |
| 3504 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3505 | rec->ver[0] = buf[rec_hdr_version_offset + 0]; |
| 3506 | rec->ver[1] = buf[rec_hdr_version_offset + 1]; |
| 3507 | mbedtls_ssl_read_version(&major_ver, &minor_ver, |
| 3508 | ssl->conf->transport, |
| 3509 | &rec->ver[0]); |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3510 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3511 | if (major_ver != ssl->major_ver) { |
| 3512 | MBEDTLS_SSL_DEBUG_MSG(1, ("major version mismatch: got %u, expected %u", |
| 3513 | (unsigned) major_ver, |
| 3514 | (unsigned) ssl->major_ver)); |
| 3515 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3516 | } |
| 3517 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3518 | if (minor_ver > ssl->conf->max_minor_ver) { |
| 3519 | MBEDTLS_SSL_DEBUG_MSG(1, ("minor version mismatch: got %u, expected max %u", |
| 3520 | (unsigned) minor_ver, |
| 3521 | (unsigned) ssl->conf->max_minor_ver)); |
| 3522 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3523 | } |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3524 | /* |
| 3525 | * Parse/Copy record sequence number. |
| 3526 | */ |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 3527 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3528 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3529 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3530 | /* Copy explicit record sequence number from input buffer. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3531 | memcpy(&rec->ctr[0], buf + rec_hdr_ctr_offset, |
| 3532 | rec_hdr_ctr_len); |
| 3533 | } else |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3534 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3535 | { |
| 3536 | /* Copy implicit record sequence number from SSL context structure. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3537 | memcpy(&rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len); |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3538 | } |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 3539 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3540 | /* |
| 3541 | * Parse record length. |
| 3542 | */ |
| 3543 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3544 | rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3545 | rec->data_len = ((size_t) buf[rec_hdr_len_offset + 0] << 8) | |
| 3546 | ((size_t) buf[rec_hdr_len_offset + 1] << 0); |
| 3547 | MBEDTLS_SSL_DEBUG_BUF(4, "input record header", buf, rec->data_offset); |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 3548 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3549 | MBEDTLS_SSL_DEBUG_MSG(3, ("input record: msgtype = %u, " |
| 3550 | "version = [%d:%d], msglen = %" MBEDTLS_PRINTF_SIZET, |
| 3551 | rec->type, |
| 3552 | major_ver, minor_ver, rec->data_len)); |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3553 | |
| 3554 | rec->buf = buf; |
| 3555 | rec->buf_len = rec->data_offset + rec->data_len; |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 3556 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3557 | if (rec->data_len == 0) { |
| 3558 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
| 3559 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3560 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3561 | /* |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 3562 | * DTLS-related tests. |
| 3563 | * Check epoch before checking length constraint because |
| 3564 | * the latter varies with the epoch. E.g., if a ChangeCipherSpec |
| 3565 | * message gets duplicated before the corresponding Finished message, |
| 3566 | * the second ChangeCipherSpec should be discarded because it belongs |
| 3567 | * to an old epoch, but not because its length is shorter than |
| 3568 | * the minimum record length for packets using the new record transform. |
| 3569 | * Note that these two kinds of failures are handled differently, |
| 3570 | * as an unexpected record is silently skipped but an invalid |
| 3571 | * record leads to the entire datagram being dropped. |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3572 | */ |
| 3573 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3574 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 3575 | rec_epoch = (rec->ctr[0] << 8) | rec->ctr[1]; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3576 | |
Hanno Becker | 955a5c9 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 3577 | /* Check that the datagram is large enough to contain a record |
| 3578 | * of the advertised length. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3579 | if (len < rec->data_offset + rec->data_len) { |
| 3580 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 3581 | ( |
| 3582 | "Datagram of length %u too small to contain record of advertised length %u.", |
| 3583 | (unsigned) len, |
| 3584 | (unsigned) (rec->data_offset + rec->data_len))); |
| 3585 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | 955a5c9 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 3586 | } |
Hanno Becker | 37cfe73 | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 3587 | |
Hanno Becker | 37cfe73 | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 3588 | /* Records from other, non-matching epochs are silently discarded. |
| 3589 | * (The case of same-port Client reconnects must be considered in |
| 3590 | * the caller). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3591 | if (rec_epoch != ssl->in_epoch) { |
| 3592 | MBEDTLS_SSL_DEBUG_MSG(1, ("record from another epoch: " |
| 3593 | "expected %u, received %lu", |
| 3594 | ssl->in_epoch, (unsigned long) rec_epoch)); |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3595 | |
Hanno Becker | 552f747 | 2019-07-19 10:59:12 +0100 | [diff] [blame] | 3596 | /* Records from the next epoch are considered for buffering |
| 3597 | * (concretely: early Finished messages). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3598 | if (rec_epoch == (unsigned) ssl->in_epoch + 1) { |
| 3599 | MBEDTLS_SSL_DEBUG_MSG(2, ("Consider record for buffering")); |
| 3600 | return MBEDTLS_ERR_SSL_EARLY_MESSAGE; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3601 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 3602 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3603 | return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3604 | } |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3605 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Hanno Becker | 37cfe73 | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 3606 | /* For records from the correct epoch, check whether their |
| 3607 | * sequence number has been seen before. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3608 | else if (mbedtls_ssl_dtls_record_replay_check((mbedtls_ssl_context *) ssl, |
| 3609 | &rec->ctr[0]) != 0) { |
| 3610 | MBEDTLS_SSL_DEBUG_MSG(1, ("replayed record")); |
| 3611 | return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3612 | } |
| 3613 | #endif |
| 3614 | } |
| 3615 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3616 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3617 | return 0; |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3618 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3619 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3620 | |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 3621 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3622 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3623 | static int ssl_check_client_reconnect(mbedtls_ssl_context *ssl) |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 3624 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3625 | unsigned int rec_epoch = (ssl->in_ctr[0] << 8) | ssl->in_ctr[1]; |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 3626 | |
| 3627 | /* |
| 3628 | * Check for an epoch 0 ClientHello. We can't use in_msg here to |
| 3629 | * access the first byte of record content (handshake type), as we |
| 3630 | * have an active transform (possibly iv_len != 0), so use the |
| 3631 | * fact that the record header len is 13 instead. |
| 3632 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3633 | if (rec_epoch == 0 && |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 3634 | ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 3635 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER && |
| 3636 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3637 | ssl->in_left > 13 && |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3638 | ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO) { |
| 3639 | MBEDTLS_SSL_DEBUG_MSG(1, ("possible client reconnect " |
| 3640 | "from the same port")); |
| 3641 | return ssl_handle_possible_reconnect(ssl); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3642 | } |
| 3643 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3644 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3645 | } |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 3646 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3647 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3648 | /* |
Manuel Pégourié-Gonnard | c40b685 | 2020-01-03 12:18:49 +0100 | [diff] [blame] | 3649 | * If applicable, decrypt record content |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3650 | */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3651 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3652 | static int ssl_prepare_record_content(mbedtls_ssl_context *ssl, |
| 3653 | mbedtls_record *rec) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3654 | { |
| 3655 | int ret, done = 0; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3656 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3657 | MBEDTLS_SSL_DEBUG_BUF(4, "input record from network", |
| 3658 | rec->buf, rec->buf_len); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3659 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3660 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3661 | if (mbedtls_ssl_hw_record_read != NULL) { |
| 3662 | MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_read()")); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3663 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3664 | ret = mbedtls_ssl_hw_record_read(ssl); |
| 3665 | if (ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH) { |
| 3666 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_read", ret); |
| 3667 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3668 | } |
Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 3669 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3670 | if (ret == 0) { |
Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 3671 | done = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3672 | } |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3673 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3674 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3675 | if (!done && ssl->transform_in != NULL) { |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 3676 | unsigned char const old_msg_type = rec->type; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3677 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3678 | if ((ret = mbedtls_ssl_decrypt_buf(ssl, ssl->transform_in, |
| 3679 | rec)) != 0) { |
| 3680 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_decrypt_buf", ret); |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 3681 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3682 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3683 | if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID && |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 3684 | ssl->conf->ignore_unexpected_cid |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3685 | == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) { |
| 3686 | MBEDTLS_SSL_DEBUG_MSG(3, ("ignoring unexpected CID")); |
Hanno Becker | 16ded98 | 2019-05-08 13:02:55 +0100 | [diff] [blame] | 3687 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 3688 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3689 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 16ded98 | 2019-05-08 13:02:55 +0100 | [diff] [blame] | 3690 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3691 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3692 | } |
| 3693 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3694 | if (old_msg_type != rec->type) { |
| 3695 | MBEDTLS_SSL_DEBUG_MSG(4, ("record type after decrypt (before %d): %d", |
| 3696 | old_msg_type, rec->type)); |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 3697 | } |
| 3698 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3699 | MBEDTLS_SSL_DEBUG_BUF(4, "input payload after decrypt", |
| 3700 | rec->buf + rec->data_offset, rec->data_len); |
Hanno Becker | 1c0c37f | 2018-08-07 14:29:29 +0100 | [diff] [blame] | 3701 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3702 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 3703 | /* We have already checked the record content type |
| 3704 | * in ssl_parse_record_header(), failing or silently |
| 3705 | * dropping the record in the case of an unknown type. |
| 3706 | * |
| 3707 | * Since with the use of CIDs, the record content type |
| 3708 | * might change during decryption, re-check the record |
| 3709 | * content type, but treat a failure as fatal this time. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3710 | if (ssl_check_record_type(rec->type)) { |
| 3711 | MBEDTLS_SSL_DEBUG_MSG(1, ("unknown record type")); |
| 3712 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 3713 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3714 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 3715 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3716 | if (rec->data_len == 0) { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3717 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3718 | if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 |
| 3719 | && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA) { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3720 | /* TLS v1.2 explicitly disallows zero-length messages which are not application data */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3721 | MBEDTLS_SSL_DEBUG_MSG(1, ("invalid zero-length message type: %d", ssl->in_msgtype)); |
| 3722 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3723 | } |
| 3724 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3725 | |
| 3726 | ssl->nb_zero++; |
| 3727 | |
| 3728 | /* |
| 3729 | * Three or more empty messages may be a DoS attack |
| 3730 | * (excessive CPU consumption). |
| 3731 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3732 | if (ssl->nb_zero > 3) { |
| 3733 | MBEDTLS_SSL_DEBUG_MSG(1, ("received four consecutive empty " |
| 3734 | "messages, possible DoS attack")); |
Hanno Becker | 6e7700d | 2019-05-08 10:38:32 +0100 | [diff] [blame] | 3735 | /* Treat the records as if they were not properly authenticated, |
| 3736 | * thereby failing the connection if we see more than allowed |
| 3737 | * by the configured bad MAC threshold. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3738 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3739 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3740 | } else { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3741 | ssl->nb_zero = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3742 | } |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3743 | |
| 3744 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3745 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3746 | ; /* in_ctr read from peer, not maintained internally */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3747 | } else |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3748 | #endif |
| 3749 | { |
| 3750 | unsigned i; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3751 | for (i = 8; i > mbedtls_ssl_ep_len(ssl); i--) { |
| 3752 | if (++ssl->in_ctr[i - 1] != 0) { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3753 | break; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3754 | } |
| 3755 | } |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3756 | |
| 3757 | /* The loop goes to its end iff the counter is wrapping */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3758 | if (i == mbedtls_ssl_ep_len(ssl)) { |
| 3759 | MBEDTLS_SSL_DEBUG_MSG(1, ("incoming message counter would wrap")); |
| 3760 | return MBEDTLS_ERR_SSL_COUNTER_WRAPPING; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3761 | } |
| 3762 | } |
| 3763 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3764 | } |
| 3765 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3766 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3767 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 3768 | mbedtls_ssl_dtls_replay_update(ssl); |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 3769 | } |
| 3770 | #endif |
| 3771 | |
Hanno Becker | d96e10b | 2019-07-09 17:30:02 +0100 | [diff] [blame] | 3772 | /* Check actual (decrypted) record content length against |
| 3773 | * configured maximum. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3774 | if (rec->data_len > MBEDTLS_SSL_IN_CONTENT_LEN) { |
| 3775 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad message length")); |
| 3776 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | d96e10b | 2019-07-09 17:30:02 +0100 | [diff] [blame] | 3777 | } |
| 3778 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3779 | return 0; |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3780 | } |
| 3781 | |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 3782 | /* |
| 3783 | * Read a record. |
| 3784 | * |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 3785 | * Silently ignore non-fatal alert (and for DTLS, invalid records as well, |
| 3786 | * RFC 6347 4.1.2.7) and continue reading until a valid record is found. |
| 3787 | * |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 3788 | */ |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 3789 | |
| 3790 | /* Helper functions for mbedtls_ssl_read_record(). */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3791 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3792 | static int ssl_consume_current_message(mbedtls_ssl_context *ssl); |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3793 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3794 | static int ssl_get_next_record(mbedtls_ssl_context *ssl); |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3795 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3796 | static int ssl_record_is_in_progress(mbedtls_ssl_context *ssl); |
Hanno Becker | 4162b11 | 2018-08-15 14:05:04 +0100 | [diff] [blame] | 3797 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3798 | int mbedtls_ssl_read_record(mbedtls_ssl_context *ssl, |
| 3799 | unsigned update_hs_digest) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3800 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3801 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3802 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3803 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> read record")); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3804 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3805 | if (ssl->keep_current_message == 0) { |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3806 | do { |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3807 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3808 | ret = ssl_consume_current_message(ssl); |
| 3809 | if (ret != 0) { |
| 3810 | return ret; |
| 3811 | } |
Hanno Becker | 2699459 | 2018-08-15 14:14:59 +0100 | [diff] [blame] | 3812 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3813 | if (ssl_record_is_in_progress(ssl) == 0) { |
David Horstmann | 5846c9d | 2022-10-06 18:31:25 +0100 | [diff] [blame] | 3814 | int dtls_have_buffered = 0; |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3815 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 3816 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3817 | /* We only check for buffered messages if the |
| 3818 | * current datagram is fully consumed. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3819 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3820 | ssl_next_record_is_in_datagram(ssl) == 0) { |
| 3821 | if (ssl_load_buffered_message(ssl) == 0) { |
David Horstmann | 5846c9d | 2022-10-06 18:31:25 +0100 | [diff] [blame] | 3822 | dtls_have_buffered = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3823 | } |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3824 | } |
| 3825 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3826 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3827 | if (dtls_have_buffered == 0) { |
| 3828 | ret = ssl_get_next_record(ssl); |
| 3829 | if (ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING) { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3830 | continue; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3831 | } |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3832 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3833 | if (ret != 0) { |
| 3834 | MBEDTLS_SSL_DEBUG_RET(1, ("ssl_get_next_record"), ret); |
| 3835 | return ret; |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3836 | } |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 3837 | } |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3838 | } |
| 3839 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3840 | ret = mbedtls_ssl_handle_message_type(ssl); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3841 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3842 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3843 | if (ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3844 | /* Buffer future message */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3845 | ret = ssl_buffer_message(ssl); |
| 3846 | if (ret != 0) { |
| 3847 | return ret; |
| 3848 | } |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3849 | |
| 3850 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
| 3851 | } |
| 3852 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3853 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3854 | } while (MBEDTLS_ERR_SSL_NON_FATAL == ret || |
| 3855 | MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3856 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3857 | if (0 != ret) { |
| 3858 | MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_handle_message_type"), ret); |
| 3859 | return ret; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3860 | } |
| 3861 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3862 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3863 | update_hs_digest == 1) { |
| 3864 | mbedtls_ssl_update_handshake_status(ssl); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3865 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3866 | } else { |
| 3867 | MBEDTLS_SSL_DEBUG_MSG(2, ("reuse previously read message")); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3868 | ssl->keep_current_message = 0; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3869 | } |
| 3870 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3871 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= read record")); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3872 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3873 | return 0; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3874 | } |
| 3875 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3876 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3877 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3878 | static int ssl_next_record_is_in_datagram(mbedtls_ssl_context *ssl) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3879 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3880 | if (ssl->in_left > ssl->next_record_offset) { |
| 3881 | return 1; |
| 3882 | } |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3883 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3884 | return 0; |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3885 | } |
| 3886 | |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3887 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3888 | static int ssl_load_buffered_message(mbedtls_ssl_context *ssl) |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3889 | { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3890 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3891 | mbedtls_ssl_hs_buffer *hs_buf; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3892 | int ret = 0; |
| 3893 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3894 | if (hs == NULL) { |
| 3895 | return -1; |
| 3896 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3897 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3898 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_load_buffered_message")); |
Hanno Becker | e00ae37 | 2018-08-20 09:39:42 +0100 | [diff] [blame] | 3899 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3900 | if (ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC || |
| 3901 | ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC) { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3902 | /* Check if we have seen a ChangeCipherSpec before. |
| 3903 | * If yes, synthesize a CCS record. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3904 | if (!hs->buffering.seen_ccs) { |
| 3905 | MBEDTLS_SSL_DEBUG_MSG(2, ("CCS not seen in the current flight")); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3906 | ret = -1; |
Hanno Becker | 0d4b376 | 2018-08-20 09:36:59 +0100 | [diff] [blame] | 3907 | goto exit; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3908 | } |
| 3909 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3910 | MBEDTLS_SSL_DEBUG_MSG(2, ("Injecting buffered CCS message")); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3911 | ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; |
| 3912 | ssl->in_msglen = 1; |
| 3913 | ssl->in_msg[0] = 1; |
| 3914 | |
| 3915 | /* As long as they are equal, the exact value doesn't matter. */ |
| 3916 | ssl->in_left = 0; |
| 3917 | ssl->next_record_offset = 0; |
| 3918 | |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 3919 | hs->buffering.seen_ccs = 0; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3920 | goto exit; |
| 3921 | } |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3922 | |
Hanno Becker | b8f5014 | 2018-08-28 10:01:34 +0100 | [diff] [blame] | 3923 | #if defined(MBEDTLS_DEBUG_C) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3924 | /* Debug only */ |
| 3925 | { |
| 3926 | unsigned offset; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3927 | for (offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++) { |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3928 | hs_buf = &hs->buffering.hs[offset]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3929 | if (hs_buf->is_valid == 1) { |
| 3930 | MBEDTLS_SSL_DEBUG_MSG(2, ("Future message with sequence number %u %s buffered.", |
| 3931 | hs->in_msg_seq + offset, |
| 3932 | hs_buf->is_complete ? "fully" : "partially")); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3933 | } |
| 3934 | } |
| 3935 | } |
Hanno Becker | b8f5014 | 2018-08-28 10:01:34 +0100 | [diff] [blame] | 3936 | #endif /* MBEDTLS_DEBUG_C */ |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3937 | |
| 3938 | /* Check if we have buffered and/or fully reassembled the |
| 3939 | * next handshake message. */ |
| 3940 | hs_buf = &hs->buffering.hs[0]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3941 | if ((hs_buf->is_valid == 1) && (hs_buf->is_complete == 1)) { |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3942 | /* Synthesize a record containing the buffered HS message. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3943 | size_t msg_len = (hs_buf->data[1] << 16) | |
| 3944 | (hs_buf->data[2] << 8) | |
| 3945 | hs_buf->data[3]; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3946 | |
| 3947 | /* Double-check that we haven't accidentally buffered |
| 3948 | * a message that doesn't fit into the input buffer. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3949 | if (msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN) { |
| 3950 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 3951 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3952 | } |
| 3953 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3954 | MBEDTLS_SSL_DEBUG_MSG(2, ("Next handshake message has been buffered - load")); |
| 3955 | MBEDTLS_SSL_DEBUG_BUF(3, "Buffered handshake message (incl. header)", |
| 3956 | hs_buf->data, msg_len + 12); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3957 | |
| 3958 | ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3959 | ssl->in_hslen = msg_len + 12; |
| 3960 | ssl->in_msglen = msg_len + 12; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3961 | memcpy(ssl->in_msg, hs_buf->data, ssl->in_hslen); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3962 | |
| 3963 | ret = 0; |
| 3964 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3965 | } else { |
| 3966 | MBEDTLS_SSL_DEBUG_MSG(2, ("Next handshake message %u not or only partially bufffered", |
| 3967 | hs->in_msg_seq)); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3968 | } |
| 3969 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3970 | ret = -1; |
| 3971 | |
| 3972 | exit: |
| 3973 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3974 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_load_buffered_message")); |
| 3975 | return ret; |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3976 | } |
| 3977 | |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3978 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3979 | static int ssl_buffer_make_space(mbedtls_ssl_context *ssl, |
| 3980 | size_t desired) |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 3981 | { |
| 3982 | int offset; |
| 3983 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3984 | MBEDTLS_SSL_DEBUG_MSG(2, ("Attempt to free buffered messages to have %u bytes available", |
| 3985 | (unsigned) desired)); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 3986 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 3987 | /* Get rid of future records epoch first, if such exist. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3988 | ssl_free_buffered_record(ssl); |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 3989 | |
| 3990 | /* Check if we have enough space available now. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3991 | if (desired <= (MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 3992 | hs->buffering.total_bytes_buffered)) { |
| 3993 | MBEDTLS_SSL_DEBUG_MSG(2, ("Enough space available after freeing future epoch record")); |
| 3994 | return 0; |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 3995 | } |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 3996 | |
Hanno Becker | 4f432ad | 2018-08-28 10:02:32 +0100 | [diff] [blame] | 3997 | /* We don't have enough space to buffer the next expected handshake |
| 3998 | * message. Remove buffers used for future messages to gain space, |
| 3999 | * starting with the most distant one. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4000 | for (offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1; |
| 4001 | offset >= 0; offset--) { |
| 4002 | MBEDTLS_SSL_DEBUG_MSG(2, |
| 4003 | ( |
| 4004 | "Free buffering slot %d to make space for reassembly of next handshake message", |
| 4005 | offset)); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 4006 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4007 | ssl_buffering_free_slot(ssl, (uint8_t) offset); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 4008 | |
| 4009 | /* Check if we have enough space available now. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4010 | if (desired <= (MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 4011 | hs->buffering.total_bytes_buffered)) { |
| 4012 | MBEDTLS_SSL_DEBUG_MSG(2, ("Enough space available after freeing buffered HS messages")); |
| 4013 | return 0; |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 4014 | } |
| 4015 | } |
| 4016 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4017 | return -1; |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 4018 | } |
| 4019 | |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 4020 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4021 | static int ssl_buffer_message(mbedtls_ssl_context *ssl) |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4022 | { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4023 | int ret = 0; |
| 4024 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 4025 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4026 | if (hs == NULL) { |
| 4027 | return 0; |
| 4028 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4029 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4030 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_buffer_message")); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4031 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4032 | switch (ssl->in_msgtype) { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4033 | case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4034 | MBEDTLS_SSL_DEBUG_MSG(2, ("Remember CCS message")); |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 4035 | |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 4036 | hs->buffering.seen_ccs = 1; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4037 | break; |
| 4038 | |
| 4039 | case MBEDTLS_SSL_MSG_HANDSHAKE: |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4040 | { |
| 4041 | unsigned recv_msg_seq_offset; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4042 | unsigned recv_msg_seq = (ssl->in_msg[4] << 8) | ssl->in_msg[5]; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4043 | mbedtls_ssl_hs_buffer *hs_buf; |
| 4044 | size_t msg_len = ssl->in_hslen - 12; |
| 4045 | |
| 4046 | /* We should never receive an old handshake |
| 4047 | * message - double-check nonetheless. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4048 | if (recv_msg_seq < ssl->handshake->in_msg_seq) { |
| 4049 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 4050 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4051 | } |
| 4052 | |
| 4053 | recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4054 | if (recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS) { |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4055 | /* Silently ignore -- message too far in the future */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4056 | MBEDTLS_SSL_DEBUG_MSG(2, |
| 4057 | ("Ignore future HS message with sequence number %u, " |
| 4058 | "buffering window %u - %u", |
| 4059 | recv_msg_seq, ssl->handshake->in_msg_seq, |
| 4060 | ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - |
| 4061 | 1)); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4062 | |
| 4063 | goto exit; |
| 4064 | } |
| 4065 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4066 | MBEDTLS_SSL_DEBUG_MSG(2, ("Buffering HS message with sequence number %u, offset %u ", |
| 4067 | recv_msg_seq, recv_msg_seq_offset)); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4068 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4069 | hs_buf = &hs->buffering.hs[recv_msg_seq_offset]; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4070 | |
| 4071 | /* Check if the buffering for this seq nr has already commenced. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4072 | if (!hs_buf->is_valid) { |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 4073 | size_t reassembly_buf_sz; |
| 4074 | |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4075 | hs_buf->is_fragmented = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4076 | (ssl_hs_is_proper_fragment(ssl) == 1); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4077 | |
| 4078 | /* We copy the message back into the input buffer |
| 4079 | * after reassembly, so check that it's not too large. |
| 4080 | * This is an implementation-specific limitation |
| 4081 | * and not one from the standard, hence it is not |
| 4082 | * checked in ssl_check_hs_header(). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4083 | if (msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN) { |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4084 | /* Ignore message */ |
| 4085 | goto exit; |
| 4086 | } |
| 4087 | |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4088 | /* Check if we have enough space to buffer the message. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4089 | if (hs->buffering.total_bytes_buffered > |
| 4090 | MBEDTLS_SSL_DTLS_MAX_BUFFERING) { |
| 4091 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 4092 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4093 | } |
| 4094 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4095 | reassembly_buf_sz = ssl_get_reassembly_buffer_size(msg_len, |
| 4096 | hs_buf->is_fragmented); |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4097 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4098 | if (reassembly_buf_sz > (MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 4099 | hs->buffering.total_bytes_buffered)) { |
| 4100 | if (recv_msg_seq_offset > 0) { |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4101 | /* If we can't buffer a future message because |
| 4102 | * of space limitations -- ignore. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4103 | MBEDTLS_SSL_DEBUG_MSG(2, |
| 4104 | ("Buffering of future message of size %" |
| 4105 | MBEDTLS_PRINTF_SIZET |
| 4106 | " would exceed the compile-time limit %" |
| 4107 | MBEDTLS_PRINTF_SIZET |
| 4108 | " (already %" MBEDTLS_PRINTF_SIZET |
| 4109 | " bytes buffered) -- ignore\n", |
| 4110 | msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 4111 | hs->buffering.total_bytes_buffered)); |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4112 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4113 | } else { |
| 4114 | MBEDTLS_SSL_DEBUG_MSG(2, |
| 4115 | ("Buffering of future message of size %" |
| 4116 | MBEDTLS_PRINTF_SIZET |
| 4117 | " would exceed the compile-time limit %" |
| 4118 | MBEDTLS_PRINTF_SIZET |
| 4119 | " (already %" MBEDTLS_PRINTF_SIZET |
| 4120 | " bytes buffered) -- attempt to make space by freeing buffered future messages\n", |
| 4121 | msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 4122 | hs->buffering.total_bytes_buffered)); |
Hanno Becker | e180139 | 2018-08-21 16:51:05 +0100 | [diff] [blame] | 4123 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4124 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4125 | if (ssl_buffer_make_space(ssl, reassembly_buf_sz) != 0) { |
| 4126 | MBEDTLS_SSL_DEBUG_MSG(2, |
| 4127 | ("Reassembly of next message of size %" |
| 4128 | MBEDTLS_PRINTF_SIZET |
| 4129 | " (%" MBEDTLS_PRINTF_SIZET |
| 4130 | " with bitmap) would exceed" |
| 4131 | " the compile-time limit %" |
| 4132 | MBEDTLS_PRINTF_SIZET |
| 4133 | " (already %" MBEDTLS_PRINTF_SIZET |
| 4134 | " bytes buffered) -- fail\n", |
| 4135 | msg_len, |
| 4136 | reassembly_buf_sz, |
| 4137 | (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 4138 | hs->buffering.total_bytes_buffered)); |
Hanno Becker | 55e9e2a | 2018-08-21 16:07:55 +0100 | [diff] [blame] | 4139 | ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 4140 | goto exit; |
| 4141 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4142 | } |
| 4143 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4144 | MBEDTLS_SSL_DEBUG_MSG(2, |
| 4145 | ("initialize reassembly, total length = %" |
| 4146 | MBEDTLS_PRINTF_SIZET, |
| 4147 | msg_len)); |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4148 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4149 | hs_buf->data = mbedtls_calloc(1, reassembly_buf_sz); |
| 4150 | if (hs_buf->data == NULL) { |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4151 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4152 | goto exit; |
| 4153 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4154 | hs_buf->data_len = reassembly_buf_sz; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4155 | |
| 4156 | /* Prepare final header: copy msg_type, length and message_seq, |
| 4157 | * then add standardised fragment_offset and fragment_length */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4158 | memcpy(hs_buf->data, ssl->in_msg, 6); |
| 4159 | memset(hs_buf->data + 6, 0, 3); |
| 4160 | memcpy(hs_buf->data + 9, hs_buf->data + 1, 3); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4161 | |
| 4162 | hs_buf->is_valid = 1; |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4163 | |
| 4164 | hs->buffering.total_bytes_buffered += reassembly_buf_sz; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4165 | } else { |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4166 | /* Make sure msg_type and length are consistent */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4167 | if (memcmp(hs_buf->data, ssl->in_msg, 4) != 0) { |
| 4168 | MBEDTLS_SSL_DEBUG_MSG(1, ("Fragment header mismatch - ignore")); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4169 | /* Ignore */ |
| 4170 | goto exit; |
| 4171 | } |
| 4172 | } |
| 4173 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4174 | if (!hs_buf->is_complete) { |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4175 | size_t frag_len, frag_off; |
| 4176 | unsigned char * const msg = hs_buf->data + 12; |
| 4177 | |
| 4178 | /* |
| 4179 | * Check and copy current fragment |
| 4180 | */ |
| 4181 | |
| 4182 | /* Validation of header fields already done in |
| 4183 | * mbedtls_ssl_prepare_handshake_record(). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4184 | frag_off = ssl_get_hs_frag_off(ssl); |
| 4185 | frag_len = ssl_get_hs_frag_len(ssl); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4186 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4187 | MBEDTLS_SSL_DEBUG_MSG(2, ("adding fragment, offset = %" MBEDTLS_PRINTF_SIZET |
| 4188 | ", length = %" MBEDTLS_PRINTF_SIZET, |
| 4189 | frag_off, frag_len)); |
| 4190 | memcpy(msg + frag_off, ssl->in_msg + 12, frag_len); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4191 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4192 | if (hs_buf->is_fragmented) { |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4193 | unsigned char * const bitmask = msg + msg_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4194 | ssl_bitmask_set(bitmask, frag_off, frag_len); |
| 4195 | hs_buf->is_complete = (ssl_bitmask_check(bitmask, |
| 4196 | msg_len) == 0); |
| 4197 | } else { |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4198 | hs_buf->is_complete = 1; |
| 4199 | } |
| 4200 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4201 | MBEDTLS_SSL_DEBUG_MSG(2, ("message %scomplete", |
| 4202 | hs_buf->is_complete ? "" : "not yet ")); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4203 | } |
| 4204 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4205 | break; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4206 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4207 | |
| 4208 | default: |
Hanno Becker | 360bef3 | 2018-08-28 10:04:33 +0100 | [diff] [blame] | 4209 | /* We don't buffer other types of messages. */ |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4210 | break; |
| 4211 | } |
| 4212 | |
| 4213 | exit: |
| 4214 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4215 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_buffer_message")); |
| 4216 | return ret; |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4217 | } |
| 4218 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4219 | |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 4220 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4221 | static int ssl_consume_current_message(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4222 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4223 | /* |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4224 | * Consume last content-layer message and potentially |
| 4225 | * update in_msglen which keeps track of the contents' |
| 4226 | * consumption state. |
| 4227 | * |
| 4228 | * (1) Handshake messages: |
| 4229 | * Remove last handshake message, move content |
| 4230 | * and adapt in_msglen. |
| 4231 | * |
| 4232 | * (2) Alert messages: |
| 4233 | * Consume whole record content, in_msglen = 0. |
| 4234 | * |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4235 | * (3) Change cipher spec: |
| 4236 | * Consume whole record content, in_msglen = 0. |
| 4237 | * |
| 4238 | * (4) Application data: |
| 4239 | * Don't do anything - the record layer provides |
| 4240 | * the application data as a stream transport |
| 4241 | * and consumes through mbedtls_ssl_read only. |
| 4242 | * |
| 4243 | */ |
| 4244 | |
| 4245 | /* Case (1): Handshake messages */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4246 | if (ssl->in_hslen != 0) { |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 4247 | /* Hard assertion to be sure that no application data |
| 4248 | * is in flight, as corrupting ssl->in_msglen during |
| 4249 | * ssl->in_offt != NULL is fatal. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4250 | if (ssl->in_offt != NULL) { |
| 4251 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 4252 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 4253 | } |
| 4254 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4255 | /* |
| 4256 | * Get next Handshake message in the current record |
| 4257 | */ |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4258 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4259 | /* Notes: |
Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 4260 | * (1) in_hslen is not necessarily the size of the |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4261 | * current handshake content: If DTLS handshake |
| 4262 | * fragmentation is used, that's the fragment |
| 4263 | * size instead. Using the total handshake message |
Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 4264 | * size here is faulty and should be changed at |
| 4265 | * some point. |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4266 | * (2) While it doesn't seem to cause problems, one |
| 4267 | * has to be very careful not to assume that in_hslen |
| 4268 | * is always <= in_msglen in a sensible communication. |
| 4269 | * Again, it's wrong for DTLS handshake fragmentation. |
| 4270 | * The following check is therefore mandatory, and |
| 4271 | * should not be treated as a silently corrected assertion. |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 4272 | * Additionally, ssl->in_hslen might be arbitrarily out of |
| 4273 | * bounds after handling a DTLS message with an unexpected |
| 4274 | * sequence number, see mbedtls_ssl_prepare_handshake_record. |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4275 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4276 | if (ssl->in_hslen < ssl->in_msglen) { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4277 | ssl->in_msglen -= ssl->in_hslen; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4278 | memmove(ssl->in_msg, ssl->in_msg + ssl->in_hslen, |
| 4279 | ssl->in_msglen); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4280 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4281 | MBEDTLS_SSL_DEBUG_BUF(4, "remaining content in record", |
| 4282 | ssl->in_msg, ssl->in_msglen); |
| 4283 | } else { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4284 | ssl->in_msglen = 0; |
| 4285 | } |
Manuel Pégourié-Gonnard | 4a17536 | 2014-09-09 17:45:31 +0200 | [diff] [blame] | 4286 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4287 | ssl->in_hslen = 0; |
| 4288 | } |
| 4289 | /* Case (4): Application data */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4290 | else if (ssl->in_offt != NULL) { |
| 4291 | return 0; |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4292 | } |
| 4293 | /* Everything else (CCS & Alerts) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4294 | else { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4295 | ssl->in_msglen = 0; |
| 4296 | } |
| 4297 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4298 | return 0; |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 4299 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4300 | |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 4301 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4302 | static int ssl_record_is_in_progress(mbedtls_ssl_context *ssl) |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4303 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4304 | if (ssl->in_msglen > 0) { |
| 4305 | return 1; |
| 4306 | } |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4307 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4308 | return 0; |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4309 | } |
| 4310 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4311 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4312 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4313 | static void ssl_free_buffered_record(mbedtls_ssl_context *ssl) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4314 | { |
| 4315 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4316 | if (hs == NULL) { |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4317 | return; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4318 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4319 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4320 | if (hs->buffering.future_record.data != NULL) { |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 4321 | hs->buffering.total_bytes_buffered -= |
| 4322 | hs->buffering.future_record.len; |
| 4323 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4324 | mbedtls_free(hs->buffering.future_record.data); |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 4325 | hs->buffering.future_record.data = NULL; |
| 4326 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4327 | } |
| 4328 | |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 4329 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4330 | static int ssl_load_buffered_record(mbedtls_ssl_context *ssl) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4331 | { |
| 4332 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4333 | unsigned char *rec; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4334 | size_t rec_len; |
| 4335 | unsigned rec_epoch; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 4336 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4337 | size_t in_buf_len = ssl->in_buf_len; |
| 4338 | #else |
| 4339 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 4340 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4341 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 4342 | return 0; |
| 4343 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4344 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4345 | if (hs == NULL) { |
| 4346 | return 0; |
| 4347 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4348 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4349 | rec = hs->buffering.future_record.data; |
| 4350 | rec_len = hs->buffering.future_record.len; |
| 4351 | rec_epoch = hs->buffering.future_record.epoch; |
| 4352 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4353 | if (rec == NULL) { |
| 4354 | return 0; |
| 4355 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4356 | |
Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 4357 | /* Only consider loading future records if the |
| 4358 | * input buffer is empty. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4359 | if (ssl_next_record_is_in_datagram(ssl) == 1) { |
| 4360 | return 0; |
| 4361 | } |
Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 4362 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4363 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_load_buffered_record")); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4364 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4365 | if (rec_epoch != ssl->in_epoch) { |
| 4366 | MBEDTLS_SSL_DEBUG_MSG(2, ("Buffered record not from current epoch.")); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4367 | goto exit; |
| 4368 | } |
| 4369 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4370 | MBEDTLS_SSL_DEBUG_MSG(2, ("Found buffered record from current epoch - load")); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4371 | |
| 4372 | /* Double-check that the record is not too large */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4373 | if (rec_len > in_buf_len - (size_t) (ssl->in_hdr - ssl->in_buf)) { |
| 4374 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 4375 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4376 | } |
| 4377 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4378 | memcpy(ssl->in_hdr, rec, rec_len); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4379 | ssl->in_left = rec_len; |
| 4380 | ssl->next_record_offset = 0; |
| 4381 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4382 | ssl_free_buffered_record(ssl); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4383 | |
| 4384 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4385 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_load_buffered_record")); |
| 4386 | return 0; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4387 | } |
| 4388 | |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 4389 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4390 | static int ssl_buffer_future_record(mbedtls_ssl_context *ssl, |
| 4391 | mbedtls_record const *rec) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4392 | { |
| 4393 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4394 | |
| 4395 | /* Don't buffer future records outside handshakes. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4396 | if (hs == NULL) { |
| 4397 | return 0; |
| 4398 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4399 | |
| 4400 | /* Only buffer handshake records (we are only interested |
| 4401 | * in Finished messages). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4402 | if (rec->type != MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 4403 | return 0; |
| 4404 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4405 | |
| 4406 | /* Don't buffer more than one future epoch record. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4407 | if (hs->buffering.future_record.data != NULL) { |
| 4408 | return 0; |
| 4409 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4410 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 4411 | /* Don't buffer record if there's not enough buffering space remaining. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4412 | if (rec->buf_len > (MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 4413 | hs->buffering.total_bytes_buffered)) { |
| 4414 | MBEDTLS_SSL_DEBUG_MSG(2, ("Buffering of future epoch record of size %" MBEDTLS_PRINTF_SIZET |
| 4415 | " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET |
| 4416 | " (already %" MBEDTLS_PRINTF_SIZET |
| 4417 | " bytes buffered) -- ignore\n", |
| 4418 | rec->buf_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 4419 | hs->buffering.total_bytes_buffered)); |
| 4420 | return 0; |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4421 | } |
| 4422 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4423 | /* Buffer record */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4424 | MBEDTLS_SSL_DEBUG_MSG(2, ("Buffer record from epoch %u", |
| 4425 | ssl->in_epoch + 1U)); |
| 4426 | MBEDTLS_SSL_DEBUG_BUF(3, "Buffered record", rec->buf, rec->buf_len); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4427 | |
| 4428 | /* ssl_parse_record_header() only considers records |
| 4429 | * of the next epoch as candidates for buffering. */ |
| 4430 | hs->buffering.future_record.epoch = ssl->in_epoch + 1; |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 4431 | hs->buffering.future_record.len = rec->buf_len; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4432 | |
| 4433 | hs->buffering.future_record.data = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4434 | mbedtls_calloc(1, hs->buffering.future_record.len); |
| 4435 | if (hs->buffering.future_record.data == NULL) { |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4436 | /* If we run out of RAM trying to buffer a |
| 4437 | * record from the next epoch, just ignore. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4438 | return 0; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4439 | } |
| 4440 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4441 | memcpy(hs->buffering.future_record.data, rec->buf, rec->buf_len); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4442 | |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 4443 | hs->buffering.total_bytes_buffered += rec->buf_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4444 | return 0; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4445 | } |
| 4446 | |
| 4447 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4448 | |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 4449 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4450 | static int ssl_get_next_record(mbedtls_ssl_context *ssl) |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 4451 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4452 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 4453 | mbedtls_record rec; |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 4454 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4455 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4456 | /* We might have buffered a future record; if so, |
| 4457 | * and if the epoch matches now, load it. |
| 4458 | * On success, this call will set ssl->in_left to |
| 4459 | * the length of the buffered record, so that |
| 4460 | * the calls to ssl_fetch_input() below will |
| 4461 | * essentially be no-ops. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4462 | ret = ssl_load_buffered_record(ssl); |
| 4463 | if (ret != 0) { |
| 4464 | return ret; |
| 4465 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4466 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4467 | |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4468 | /* Ensure that we have enough space available for the default form |
| 4469 | * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS, |
| 4470 | * with no space for CIDs counted in). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4471 | ret = mbedtls_ssl_fetch_input(ssl, mbedtls_ssl_in_hdr_len(ssl)); |
| 4472 | if (ret != 0) { |
| 4473 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret); |
| 4474 | return ret; |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4475 | } |
| 4476 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4477 | ret = ssl_parse_record_header(ssl, ssl->in_hdr, ssl->in_left, &rec); |
| 4478 | if (ret != 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4479 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4480 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 4481 | if (ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) { |
| 4482 | ret = ssl_buffer_future_record(ssl, &rec); |
| 4483 | if (ret != 0) { |
| 4484 | return ret; |
| 4485 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4486 | |
| 4487 | /* Fall through to handling of unexpected records */ |
| 4488 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
| 4489 | } |
| 4490 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4491 | if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD) { |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 4492 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | d8bf8ce | 2019-07-12 09:23:47 +0100 | [diff] [blame] | 4493 | /* Reset in pointers to default state for TLS/DTLS records, |
| 4494 | * assuming no CID and no offset between record content and |
| 4495 | * record plaintext. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4496 | mbedtls_ssl_update_in_pointers(ssl); |
Hanno Becker | d8bf8ce | 2019-07-12 09:23:47 +0100 | [diff] [blame] | 4497 | |
Hanno Becker | 7ae20e0 | 2019-07-12 08:33:49 +0100 | [diff] [blame] | 4498 | /* Setup internal message pointers from record structure. */ |
| 4499 | ssl->in_msgtype = rec.type; |
| 4500 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4501 | ssl->in_len = ssl->in_cid + rec.cid_len; |
| 4502 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4503 | ssl->in_iv = ssl->in_msg = ssl->in_len + 2; |
| 4504 | ssl->in_msglen = rec.data_len; |
| 4505 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4506 | ret = ssl_check_client_reconnect(ssl); |
| 4507 | MBEDTLS_SSL_DEBUG_RET(2, "ssl_check_client_reconnect", ret); |
| 4508 | if (ret != 0) { |
| 4509 | return ret; |
| 4510 | } |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 4511 | #endif |
| 4512 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4513 | /* Skip unexpected record (but not whole datagram) */ |
Hanno Becker | 4acada3 | 2019-07-11 12:48:53 +0100 | [diff] [blame] | 4514 | ssl->next_record_offset = rec.buf_len; |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4515 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4516 | MBEDTLS_SSL_DEBUG_MSG(1, ("discarding unexpected record " |
| 4517 | "(header)")); |
| 4518 | } else { |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4519 | /* Skip invalid record and the rest of the datagram */ |
| 4520 | ssl->next_record_offset = 0; |
| 4521 | ssl->in_left = 0; |
| 4522 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4523 | MBEDTLS_SSL_DEBUG_MSG(1, ("discarding invalid record " |
| 4524 | "(header)")); |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4525 | } |
| 4526 | |
| 4527 | /* Get next record */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4528 | return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
| 4529 | } else |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4530 | #endif |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 4531 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4532 | return ret; |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 4533 | } |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4534 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4535 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4536 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4537 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 4538 | /* Remember offset of next record within datagram. */ |
Hanno Becker | f50da50 | 2019-07-11 12:50:10 +0100 | [diff] [blame] | 4539 | ssl->next_record_offset = rec.buf_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4540 | if (ssl->next_record_offset < ssl->in_left) { |
| 4541 | MBEDTLS_SSL_DEBUG_MSG(3, ("more than one record within datagram")); |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 4542 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4543 | } else |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4544 | #endif |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 4545 | { |
Hanno Becker | 955a5c9 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 4546 | /* |
| 4547 | * Fetch record contents from underlying transport. |
| 4548 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4549 | ret = mbedtls_ssl_fetch_input(ssl, rec.buf_len); |
| 4550 | if (ret != 0) { |
| 4551 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret); |
| 4552 | return ret; |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 4553 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4554 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4555 | ssl->in_left = 0; |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 4556 | } |
| 4557 | |
| 4558 | /* |
| 4559 | * Decrypt record contents. |
| 4560 | */ |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4561 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4562 | if ((ret = ssl_prepare_record_content(ssl, &rec)) != 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4563 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4564 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4565 | /* Silently discard invalid records */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4566 | if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) { |
Manuel Pégourié-Gonnard | 0a88574 | 2015-08-04 12:08:35 +0200 | [diff] [blame] | 4567 | /* Except when waiting for Finished as a bad mac here |
| 4568 | * probably means something went wrong in the handshake |
| 4569 | * (eg wrong psk used, mitm downgrade attempt, etc.) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4570 | if (ssl->state == MBEDTLS_SSL_CLIENT_FINISHED || |
| 4571 | ssl->state == MBEDTLS_SSL_SERVER_FINISHED) { |
Manuel Pégourié-Gonnard | 0a88574 | 2015-08-04 12:08:35 +0200 | [diff] [blame] | 4572 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4573 | if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) { |
| 4574 | mbedtls_ssl_send_alert_message(ssl, |
| 4575 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 4576 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC); |
Manuel Pégourié-Gonnard | 0a88574 | 2015-08-04 12:08:35 +0200 | [diff] [blame] | 4577 | } |
| 4578 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4579 | return ret; |
Manuel Pégourié-Gonnard | 0a88574 | 2015-08-04 12:08:35 +0200 | [diff] [blame] | 4580 | } |
| 4581 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4582 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4583 | if (ssl->conf->badmac_limit != 0 && |
| 4584 | ++ssl->badmac_seen >= ssl->conf->badmac_limit) { |
| 4585 | MBEDTLS_SSL_DEBUG_MSG(1, ("too many records with bad MAC")); |
| 4586 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 4587 | } |
| 4588 | #endif |
| 4589 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4590 | /* As above, invalid records cause |
| 4591 | * dismissal of the whole datagram. */ |
| 4592 | |
| 4593 | ssl->next_record_offset = 0; |
| 4594 | ssl->in_left = 0; |
| 4595 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4596 | MBEDTLS_SSL_DEBUG_MSG(1, ("discarding invalid record (mac)")); |
| 4597 | return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4598 | } |
| 4599 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4600 | return ret; |
| 4601 | } else |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4602 | #endif |
| 4603 | { |
| 4604 | /* Error out (and send alert) on invalid records */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4605 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4606 | if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) { |
| 4607 | mbedtls_ssl_send_alert_message(ssl, |
| 4608 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 4609 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4610 | } |
| 4611 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4612 | return ret; |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4613 | } |
| 4614 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4615 | |
Hanno Becker | 44d89b2 | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 4616 | |
| 4617 | /* Reset in pointers to default state for TLS/DTLS records, |
| 4618 | * assuming no CID and no offset between record content and |
| 4619 | * record plaintext. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4620 | mbedtls_ssl_update_in_pointers(ssl); |
Hanno Becker | 44d89b2 | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 4621 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4622 | ssl->in_len = ssl->in_cid + rec.cid_len; |
| 4623 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
irwir | 89af51f | 2019-09-26 21:04:56 +0300 | [diff] [blame] | 4624 | ssl->in_iv = ssl->in_len + 2; |
Hanno Becker | 44d89b2 | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 4625 | |
Hanno Becker | 8685c82 | 2019-07-12 09:37:30 +0100 | [diff] [blame] | 4626 | /* The record content type may change during decryption, |
| 4627 | * so re-read it. */ |
| 4628 | ssl->in_msgtype = rec.type; |
| 4629 | /* Also update the input buffer, because unfortunately |
| 4630 | * the server-side ssl_parse_client_hello() reparses the |
| 4631 | * record header when receiving a ClientHello initiating |
| 4632 | * a renegotiation. */ |
| 4633 | ssl->in_hdr[0] = rec.type; |
| 4634 | ssl->in_msg = rec.buf + rec.data_offset; |
| 4635 | ssl->in_msglen = rec.data_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4636 | MBEDTLS_PUT_UINT16_BE(rec.data_len, ssl->in_len, 0); |
Hanno Becker | 8685c82 | 2019-07-12 09:37:30 +0100 | [diff] [blame] | 4637 | |
Manuel Pégourié-Gonnard | c40b685 | 2020-01-03 12:18:49 +0100 | [diff] [blame] | 4638 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4639 | if (ssl->transform_in != NULL && |
| 4640 | ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE) { |
| 4641 | if ((ret = ssl_decompress_buf(ssl)) != 0) { |
| 4642 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_decompress_buf", ret); |
| 4643 | return ret; |
Manuel Pégourié-Gonnard | c40b685 | 2020-01-03 12:18:49 +0100 | [diff] [blame] | 4644 | } |
| 4645 | |
| 4646 | /* Check actual (decompress) record content length against |
| 4647 | * configured maximum. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4648 | if (ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN) { |
| 4649 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad message length")); |
| 4650 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Manuel Pégourié-Gonnard | c40b685 | 2020-01-03 12:18:49 +0100 | [diff] [blame] | 4651 | } |
| 4652 | } |
| 4653 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
| 4654 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4655 | return 0; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4656 | } |
| 4657 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4658 | int mbedtls_ssl_handle_message_type(mbedtls_ssl_context *ssl) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4659 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4660 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4661 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 4662 | /* |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4663 | * Handle particular types of records |
| 4664 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4665 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 4666 | if ((ret = mbedtls_ssl_prepare_handshake_record(ssl)) != 0) { |
| 4667 | return ret; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4668 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4669 | } |
| 4670 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4671 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) { |
| 4672 | if (ssl->in_msglen != 1) { |
| 4673 | MBEDTLS_SSL_DEBUG_MSG(1, ("invalid CCS message, len: %" MBEDTLS_PRINTF_SIZET, |
| 4674 | ssl->in_msglen)); |
| 4675 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4676 | } |
| 4677 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4678 | if (ssl->in_msg[0] != 1) { |
| 4679 | MBEDTLS_SSL_DEBUG_MSG(1, ("invalid CCS message, content: %02x", |
| 4680 | ssl->in_msg[0])); |
| 4681 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 4682 | } |
| 4683 | |
| 4684 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4685 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 4686 | ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC && |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4687 | ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC) { |
| 4688 | if (ssl->handshake == NULL) { |
| 4689 | MBEDTLS_SSL_DEBUG_MSG(1, ("dropping ChangeCipherSpec outside handshake")); |
| 4690 | return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 4691 | } |
| 4692 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4693 | MBEDTLS_SSL_DEBUG_MSG(1, ("received out-of-order ChangeCipherSpec - remember")); |
| 4694 | return MBEDTLS_ERR_SSL_EARLY_MESSAGE; |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 4695 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4696 | #endif |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 4697 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4698 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4699 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT) { |
| 4700 | if (ssl->in_msglen != 2) { |
Angus Gratton | 1a7a17e | 2018-06-20 15:43:50 +1000 | [diff] [blame] | 4701 | /* Note: Standard allows for more than one 2 byte alert |
| 4702 | to be packed in a single message, but Mbed TLS doesn't |
| 4703 | currently support this. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4704 | MBEDTLS_SSL_DEBUG_MSG(1, ("invalid alert message, len: %" MBEDTLS_PRINTF_SIZET, |
| 4705 | ssl->in_msglen)); |
| 4706 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Angus Gratton | 1a7a17e | 2018-06-20 15:43:50 +1000 | [diff] [blame] | 4707 | } |
| 4708 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4709 | MBEDTLS_SSL_DEBUG_MSG(2, ("got an alert message, type: [%u:%u]", |
| 4710 | ssl->in_msg[0], ssl->in_msg[1])); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4711 | |
| 4712 | /* |
Simon Butcher | 459a950 | 2015-10-27 16:09:03 +0000 | [diff] [blame] | 4713 | * Ignore non-fatal alerts, except close_notify and no_renegotiation |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4714 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4715 | if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL) { |
| 4716 | MBEDTLS_SSL_DEBUG_MSG(1, ("is a fatal alert message (msg %d)", |
| 4717 | ssl->in_msg[1])); |
| 4718 | return MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4719 | } |
| 4720 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4721 | if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 4722 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY) { |
| 4723 | MBEDTLS_SSL_DEBUG_MSG(2, ("is a close notify message")); |
| 4724 | return MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4725 | } |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 4726 | |
| 4727 | #if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4728 | if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 4729 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION) { |
| 4730 | MBEDTLS_SSL_DEBUG_MSG(2, ("is a SSLv3 no renegotiation alert")); |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 4731 | /* Will be handled when trying to parse ServerHello */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4732 | return 0; |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 4733 | } |
| 4734 | #endif |
| 4735 | |
| 4736 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4737 | if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 && |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 4738 | ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 4739 | ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4740 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT) { |
| 4741 | MBEDTLS_SSL_DEBUG_MSG(2, ("is a SSLv3 no_cert")); |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 4742 | /* Will be handled in mbedtls_ssl_parse_certificate() */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4743 | return 0; |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 4744 | } |
| 4745 | #endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */ |
| 4746 | |
| 4747 | /* Silently ignore: fetch new message */ |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4748 | return MBEDTLS_ERR_SSL_NON_FATAL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4749 | } |
| 4750 | |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 4751 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4752 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 4753 | /* Drop unexpected ApplicationData records, |
| 4754 | * except at the beginning of renegotiations */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4755 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA && |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 4756 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER |
| 4757 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4758 | && !(ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 4759 | ssl->state == MBEDTLS_SSL_SERVER_HELLO) |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 4760 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4761 | ) { |
| 4762 | MBEDTLS_SSL_DEBUG_MSG(1, ("dropping unexpected ApplicationData")); |
| 4763 | return MBEDTLS_ERR_SSL_NON_FATAL; |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 4764 | } |
| 4765 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4766 | if (ssl->handshake != NULL && |
| 4767 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 4768 | mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl); |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 4769 | } |
| 4770 | } |
Hanno Becker | 4a4af9f | 2019-05-08 16:26:21 +0100 | [diff] [blame] | 4771 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 4772 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4773 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4774 | } |
| 4775 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4776 | int mbedtls_ssl_send_fatal_handshake_failure(mbedtls_ssl_context *ssl) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 4777 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4778 | return mbedtls_ssl_send_alert_message(ssl, |
| 4779 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 4780 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 4781 | } |
| 4782 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4783 | int mbedtls_ssl_send_alert_message(mbedtls_ssl_context *ssl, |
| 4784 | unsigned char level, |
| 4785 | unsigned char message) |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 4786 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4787 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 4788 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4789 | if (ssl == NULL || ssl->conf == NULL) { |
| 4790 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4791 | } |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 4792 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4793 | if (ssl->out_left != 0) { |
| 4794 | return mbedtls_ssl_flush_output(ssl); |
| 4795 | } |
Hanno Becker | d9c66c0 | 2018-08-06 11:35:16 +0100 | [diff] [blame] | 4796 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4797 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> send alert message")); |
| 4798 | MBEDTLS_SSL_DEBUG_MSG(3, ("send alert level=%u message=%u", level, message)); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 4799 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4800 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 4801 | ssl->out_msglen = 2; |
| 4802 | ssl->out_msg[0] = level; |
| 4803 | ssl->out_msg[1] = message; |
| 4804 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4805 | if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) { |
| 4806 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret); |
| 4807 | return ret; |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 4808 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4809 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= send alert message")); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 4810 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4811 | return 0; |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 4812 | } |
| 4813 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4814 | int mbedtls_ssl_write_change_cipher_spec(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4815 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4816 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4817 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4818 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write change cipher spec")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4819 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4820 | ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4821 | ssl->out_msglen = 1; |
| 4822 | ssl->out_msg[0] = 1; |
| 4823 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4824 | ssl->state++; |
| 4825 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4826 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { |
| 4827 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); |
| 4828 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4829 | } |
| 4830 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4831 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write change cipher spec")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4832 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4833 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4834 | } |
| 4835 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4836 | int mbedtls_ssl_parse_change_cipher_spec(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4837 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4838 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4839 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4840 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse change cipher spec")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4841 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4842 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
| 4843 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
| 4844 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4845 | } |
| 4846 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4847 | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) { |
| 4848 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad change cipher spec message")); |
| 4849 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 4850 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
| 4851 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4852 | } |
| 4853 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 4854 | /* CCS records are only accepted if they have length 1 and content '1', |
| 4855 | * so we don't need to check this here. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4856 | |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4857 | /* |
| 4858 | * Switch to our negotiated transform and session parameters for inbound |
| 4859 | * data. |
| 4860 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4861 | MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for inbound data")); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4862 | ssl->transform_in = ssl->transform_negotiate; |
| 4863 | ssl->session_in = ssl->session_negotiate; |
| 4864 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4865 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4866 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4867 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4868 | mbedtls_ssl_dtls_replay_reset(ssl); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4869 | #endif |
| 4870 | |
| 4871 | /* Increment epoch */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4872 | if (++ssl->in_epoch == 0) { |
| 4873 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap")); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 4874 | /* This is highly unlikely to happen for legitimate reasons, so |
| 4875 | treat it as an attack and don't send an alert. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4876 | return MBEDTLS_ERR_SSL_COUNTER_WRAPPING; |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4877 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4878 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4879 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4880 | memset(ssl->in_ctr, 0, 8); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4881 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4882 | mbedtls_ssl_update_in_pointers(ssl); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4883 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4884 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4885 | if (mbedtls_ssl_hw_record_activate != NULL) { |
| 4886 | if ((ret = mbedtls_ssl_hw_record_activate(ssl, MBEDTLS_SSL_CHANNEL_INBOUND)) != 0) { |
| 4887 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_activate", ret); |
| 4888 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 4889 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
| 4890 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4891 | } |
| 4892 | } |
| 4893 | #endif |
| 4894 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4895 | ssl->state++; |
| 4896 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4897 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse change cipher spec")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4898 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4899 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4900 | } |
| 4901 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4902 | /* Once ssl->out_hdr as the address of the beginning of the |
| 4903 | * next outgoing record is set, deduce the other pointers. |
| 4904 | * |
| 4905 | * Note: For TLS, we save the implicit record sequence number |
| 4906 | * (entering MAC computation) in the 8 bytes before ssl->out_hdr, |
| 4907 | * and the caller has to make sure there's space for this. |
| 4908 | */ |
| 4909 | |
Hanno Becker | c0eefa8 | 2020-05-28 07:17:36 +0100 | [diff] [blame] | 4910 | static size_t ssl_transform_get_explicit_iv_len( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4911 | mbedtls_ssl_transform const *transform) |
Hanno Becker | c0eefa8 | 2020-05-28 07:17:36 +0100 | [diff] [blame] | 4912 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4913 | if (transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2) { |
| 4914 | return 0; |
| 4915 | } |
Hanno Becker | c0eefa8 | 2020-05-28 07:17:36 +0100 | [diff] [blame] | 4916 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4917 | return transform->ivlen - transform->fixed_ivlen; |
Hanno Becker | c0eefa8 | 2020-05-28 07:17:36 +0100 | [diff] [blame] | 4918 | } |
| 4919 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4920 | void mbedtls_ssl_update_out_pointers(mbedtls_ssl_context *ssl, |
| 4921 | mbedtls_ssl_transform *transform) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4922 | { |
| 4923 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4924 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4925 | ssl->out_ctr = ssl->out_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4926 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4927 | ssl->out_cid = ssl->out_ctr + 8; |
| 4928 | ssl->out_len = ssl->out_cid; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4929 | if (transform != NULL) { |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4930 | ssl->out_len += transform->out_cid_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4931 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4932 | #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4933 | ssl->out_len = ssl->out_ctr + 8; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4934 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4935 | ssl->out_iv = ssl->out_len + 2; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4936 | } else |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4937 | #endif |
| 4938 | { |
| 4939 | ssl->out_ctr = ssl->out_hdr - 8; |
| 4940 | ssl->out_len = ssl->out_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4941 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 4c3eb7c | 2019-05-08 16:43:21 +0100 | [diff] [blame] | 4942 | ssl->out_cid = ssl->out_len; |
| 4943 | #endif |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4944 | ssl->out_iv = ssl->out_hdr + 5; |
| 4945 | } |
| 4946 | |
Hanno Becker | c0eefa8 | 2020-05-28 07:17:36 +0100 | [diff] [blame] | 4947 | ssl->out_msg = ssl->out_iv; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4948 | /* Adjust out_msg to make space for explicit IV, if used. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4949 | if (transform != NULL) { |
| 4950 | ssl->out_msg += ssl_transform_get_explicit_iv_len(transform); |
| 4951 | } |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4952 | } |
| 4953 | |
| 4954 | /* Once ssl->in_hdr as the address of the beginning of the |
| 4955 | * next incoming record is set, deduce the other pointers. |
| 4956 | * |
| 4957 | * Note: For TLS, we save the implicit record sequence number |
| 4958 | * (entering MAC computation) in the 8 bytes before ssl->in_hdr, |
| 4959 | * and the caller has to make sure there's space for this. |
| 4960 | */ |
| 4961 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4962 | void mbedtls_ssl_update_in_pointers(mbedtls_ssl_context *ssl) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4963 | { |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 4964 | /* This function sets the pointers to match the case |
| 4965 | * of unprotected TLS/DTLS records, with both ssl->in_iv |
| 4966 | * and ssl->in_msg pointing to the beginning of the record |
| 4967 | * content. |
| 4968 | * |
| 4969 | * When decrypting a protected record, ssl->in_msg |
| 4970 | * will be shifted to point to the beginning of the |
| 4971 | * record plaintext. |
| 4972 | */ |
| 4973 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4974 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4975 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4976 | /* This sets the header pointers to match records |
| 4977 | * without CID. When we receive a record containing |
| 4978 | * a CID, the fields are shifted accordingly in |
| 4979 | * ssl_parse_record_header(). */ |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4980 | ssl->in_ctr = ssl->in_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4981 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4982 | ssl->in_cid = ssl->in_ctr + 8; |
| 4983 | ssl->in_len = ssl->in_cid; /* Default: no CID */ |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4984 | #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4985 | ssl->in_len = ssl->in_ctr + 8; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4986 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4987 | ssl->in_iv = ssl->in_len + 2; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4988 | } else |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4989 | #endif |
| 4990 | { |
| 4991 | ssl->in_ctr = ssl->in_hdr - 8; |
| 4992 | ssl->in_len = ssl->in_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4993 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 4c3eb7c | 2019-05-08 16:43:21 +0100 | [diff] [blame] | 4994 | ssl->in_cid = ssl->in_len; |
| 4995 | #endif |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4996 | ssl->in_iv = ssl->in_hdr + 5; |
| 4997 | } |
| 4998 | |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 4999 | /* This will be adjusted at record decryption time. */ |
| 5000 | ssl->in_msg = ssl->in_iv; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 5001 | } |
| 5002 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5003 | /* |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 5004 | * Setup an SSL context |
| 5005 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 5006 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5007 | void mbedtls_ssl_reset_in_out_pointers(mbedtls_ssl_context *ssl) |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 5008 | { |
| 5009 | /* Set the incoming and outgoing record pointers. */ |
| 5010 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5011 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 5012 | ssl->out_hdr = ssl->out_buf; |
| 5013 | ssl->in_hdr = ssl->in_buf; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5014 | } else |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 5015 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5016 | { |
| 5017 | ssl->out_hdr = ssl->out_buf + 8; |
| 5018 | ssl->in_hdr = ssl->in_buf + 8; |
| 5019 | } |
| 5020 | |
| 5021 | /* Derive other internal pointers. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5022 | mbedtls_ssl_update_out_pointers(ssl, NULL /* no transform enabled */); |
| 5023 | mbedtls_ssl_update_in_pointers(ssl); |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 5024 | } |
| 5025 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5026 | /* |
| 5027 | * SSL get accessors |
| 5028 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5029 | size_t mbedtls_ssl_get_bytes_avail(const mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5030 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5031 | return ssl->in_offt == NULL ? 0 : ssl->in_msglen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5032 | } |
| 5033 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5034 | int mbedtls_ssl_check_pending(const mbedtls_ssl_context *ssl) |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 5035 | { |
| 5036 | /* |
| 5037 | * Case A: We're currently holding back |
| 5038 | * a message for further processing. |
| 5039 | */ |
| 5040 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5041 | if (ssl->keep_current_message == 1) { |
| 5042 | MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: record held back for processing")); |
| 5043 | return 1; |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 5044 | } |
| 5045 | |
| 5046 | /* |
| 5047 | * Case B: Further records are pending in the current datagram. |
| 5048 | */ |
| 5049 | |
| 5050 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5051 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 5052 | ssl->in_left > ssl->next_record_offset) { |
| 5053 | MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: more records within current datagram")); |
| 5054 | return 1; |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 5055 | } |
| 5056 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5057 | |
| 5058 | /* |
| 5059 | * Case C: A handshake message is being processed. |
| 5060 | */ |
| 5061 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5062 | if (ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen) { |
| 5063 | MBEDTLS_SSL_DEBUG_MSG(3, |
| 5064 | ("ssl_check_pending: more handshake messages within current record")); |
| 5065 | return 1; |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 5066 | } |
| 5067 | |
| 5068 | /* |
| 5069 | * Case D: An application data message is being processed |
| 5070 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5071 | if (ssl->in_offt != NULL) { |
| 5072 | MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: application data record is being processed")); |
| 5073 | return 1; |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 5074 | } |
| 5075 | |
| 5076 | /* |
| 5077 | * In all other cases, the rest of the message can be dropped. |
Hanno Becker | c573ac3 | 2018-08-28 17:15:25 +0100 | [diff] [blame] | 5078 | * As in ssl_get_next_record, this needs to be adapted if |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 5079 | * we implement support for multiple alerts in single records. |
| 5080 | */ |
| 5081 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5082 | MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: nothing pending")); |
| 5083 | return 0; |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 5084 | } |
| 5085 | |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 5086 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5087 | int mbedtls_ssl_get_record_expansion(const mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5088 | { |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 5089 | size_t transform_expansion = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5090 | const mbedtls_ssl_transform *transform = ssl->transform_out; |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 5091 | unsigned block_size; |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5092 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5093 | size_t out_hdr_len = mbedtls_ssl_out_hdr_len(ssl); |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 5094 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5095 | if (transform == NULL) { |
| 5096 | return (int) out_hdr_len; |
| 5097 | } |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 5098 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5099 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5100 | if (ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL) { |
| 5101 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 5102 | } |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5103 | #endif |
| 5104 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5105 | switch (mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5106 | case MBEDTLS_MODE_GCM: |
| 5107 | case MBEDTLS_MODE_CCM: |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 5108 | case MBEDTLS_MODE_CHACHAPOLY: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5109 | case MBEDTLS_MODE_STREAM: |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5110 | transform_expansion = transform->minlen; |
| 5111 | break; |
| 5112 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5113 | case MBEDTLS_MODE_CBC: |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 5114 | |
| 5115 | block_size = mbedtls_cipher_get_block_size( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5116 | &transform->cipher_ctx_enc); |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 5117 | |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 5118 | /* Expansion due to the addition of the MAC. */ |
| 5119 | transform_expansion += transform->maclen; |
| 5120 | |
| 5121 | /* Expansion due to the addition of CBC padding; |
| 5122 | * Theoretically up to 256 bytes, but we never use |
| 5123 | * more than the block size of the underlying cipher. */ |
| 5124 | transform_expansion += block_size; |
| 5125 | |
| 5126 | /* For TLS 1.1 or higher, an explicit IV is added |
| 5127 | * after the record header. */ |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 5128 | #if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5129 | if (ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2) { |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 5130 | transform_expansion += block_size; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5131 | } |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 5132 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 5133 | |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5134 | break; |
| 5135 | |
| 5136 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5137 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 5138 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5139 | } |
| 5140 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5141 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5142 | if (transform->out_cid_len != 0) { |
Hanno Becker | 6cbad55 | 2019-05-08 15:40:11 +0100 | [diff] [blame] | 5143 | transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5144 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5145 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 6cbad55 | 2019-05-08 15:40:11 +0100 | [diff] [blame] | 5146 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5147 | return (int) (out_hdr_len + transform_expansion); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5148 | } |
| 5149 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5150 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5151 | /* |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 5152 | * Check record counters and renegotiate if they're above the limit. |
| 5153 | */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 5154 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5155 | static int ssl_check_ctr_renegotiate(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 5156 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5157 | size_t ep_len = mbedtls_ssl_ep_len(ssl); |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 5158 | int in_ctr_cmp; |
| 5159 | int out_ctr_cmp; |
| 5160 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5161 | if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5162 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING || |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5163 | ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED) { |
| 5164 | return 0; |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 5165 | } |
| 5166 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5167 | in_ctr_cmp = memcmp(ssl->in_ctr + ep_len, |
| 5168 | ssl->conf->renego_period + ep_len, 8 - ep_len); |
| 5169 | out_ctr_cmp = memcmp(ssl->cur_out_ctr + ep_len, |
| 5170 | ssl->conf->renego_period + ep_len, 8 - ep_len); |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 5171 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5172 | if (in_ctr_cmp <= 0 && out_ctr_cmp <= 0) { |
| 5173 | return 0; |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 5174 | } |
| 5175 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5176 | MBEDTLS_SSL_DEBUG_MSG(1, ("record counter limit reached: renegotiate")); |
| 5177 | return mbedtls_ssl_renegotiate(ssl); |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 5178 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5179 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5180 | |
| 5181 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5182 | * Receive application data decrypted from the SSL layer |
| 5183 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5184 | int mbedtls_ssl_read(mbedtls_ssl_context *ssl, unsigned char *buf, size_t len) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5185 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5186 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 5187 | size_t n; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5188 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5189 | if (ssl == NULL || ssl->conf == NULL) { |
| 5190 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5191 | } |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5192 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5193 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> read")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5194 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5195 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5196 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 5197 | if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) { |
| 5198 | return ret; |
| 5199 | } |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 5200 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5201 | if (ssl->handshake != NULL && |
| 5202 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) { |
| 5203 | if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) { |
| 5204 | return ret; |
| 5205 | } |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 5206 | } |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 5207 | } |
| 5208 | #endif |
| 5209 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5210 | /* |
| 5211 | * Check if renegotiation is necessary and/or handshake is |
| 5212 | * in process. If yes, perform/continue, and fall through |
| 5213 | * if an unexpected packet is received while the client |
| 5214 | * is waiting for the ServerHello. |
| 5215 | * |
| 5216 | * (There is no equivalent to the last condition on |
| 5217 | * the server-side as it is not treated as within |
| 5218 | * a handshake while waiting for the ClientHello |
| 5219 | * after a renegotiation request.) |
| 5220 | */ |
| 5221 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5222 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5223 | ret = ssl_check_ctr_renegotiate(ssl); |
| 5224 | if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 5225 | ret != 0) { |
| 5226 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_check_ctr_renegotiate", ret); |
| 5227 | return ret; |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 5228 | } |
| 5229 | #endif |
| 5230 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5231 | if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 5232 | ret = mbedtls_ssl_handshake(ssl); |
| 5233 | if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 5234 | ret != 0) { |
| 5235 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret); |
| 5236 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5237 | } |
| 5238 | } |
| 5239 | |
Hanno Becker | e41158b | 2017-10-23 13:30:32 +0100 | [diff] [blame] | 5240 | /* Loop as long as no application data record is available */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5241 | while (ssl->in_offt == NULL) { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 5242 | /* Start timer if not already running */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5243 | if (ssl->f_get_timer != NULL && |
| 5244 | ssl->f_get_timer(ssl->p_timer) == -1) { |
| 5245 | mbedtls_ssl_set_timer(ssl, ssl->conf->read_timeout); |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 5246 | } |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 5247 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5248 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
| 5249 | if (ret == MBEDTLS_ERR_SSL_CONN_EOF) { |
| 5250 | return 0; |
| 5251 | } |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 5252 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5253 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
| 5254 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5255 | } |
| 5256 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5257 | if (ssl->in_msglen == 0 && |
| 5258 | ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5259 | /* |
| 5260 | * OpenSSL sends empty messages to randomize the IV |
| 5261 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5262 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
| 5263 | if (ret == MBEDTLS_ERR_SSL_CONN_EOF) { |
| 5264 | return 0; |
| 5265 | } |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 5266 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5267 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
| 5268 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5269 | } |
| 5270 | } |
| 5271 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5272 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 5273 | MBEDTLS_SSL_DEBUG_MSG(1, ("received handshake message")); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5274 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5275 | /* |
| 5276 | * - For client-side, expect SERVER_HELLO_REQUEST. |
| 5277 | * - For server-side, expect CLIENT_HELLO. |
| 5278 | * - Fail (TLS) or silently drop record (DTLS) in other cases. |
| 5279 | */ |
| 5280 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5281 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5282 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 5283 | (ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST || |
| 5284 | ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl))) { |
| 5285 | MBEDTLS_SSL_DEBUG_MSG(1, ("handshake received (not HelloRequest)")); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 5286 | |
| 5287 | /* With DTLS, drop the packet (probably from last handshake) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5288 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5289 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5290 | continue; |
| 5291 | } |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 5292 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5293 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 5294 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5295 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 5296 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5297 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5298 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 5299 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO) { |
| 5300 | MBEDTLS_SSL_DEBUG_MSG(1, ("handshake received (not ClientHello)")); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 5301 | |
| 5302 | /* With DTLS, drop the packet (probably from last handshake) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5303 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5304 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5305 | continue; |
| 5306 | } |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 5307 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5308 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5309 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5310 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 5311 | |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 5312 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5313 | /* Determine whether renegotiation attempt should be accepted */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5314 | if (!(ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
| 5315 | (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
| 5316 | ssl->conf->allow_legacy_renegotiation == |
| 5317 | MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION))) { |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 5318 | /* |
| 5319 | * Accept renegotiation request |
| 5320 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5321 | |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 5322 | /* DTLS clients need to know renego is server-initiated */ |
| 5323 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5324 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 5325 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 5326 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
| 5327 | } |
| 5328 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5329 | ret = mbedtls_ssl_start_renegotiation(ssl); |
| 5330 | if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 5331 | ret != 0) { |
| 5332 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", |
| 5333 | ret); |
| 5334 | return ret; |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 5335 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5336 | } else |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 5337 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5338 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5339 | /* |
| 5340 | * Refuse renegotiation |
| 5341 | */ |
| 5342 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5343 | MBEDTLS_SSL_DEBUG_MSG(3, ("refusing renegotiation, sending alert")); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5344 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5345 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5346 | if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) { |
Gilles Peskine | 92e4426 | 2017-05-10 17:27:49 +0200 | [diff] [blame] | 5347 | /* SSLv3 does not have a "no_renegotiation" warning, so |
| 5348 | we send a fatal alert and abort the connection. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5349 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5350 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
| 5351 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 5352 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5353 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 5354 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5355 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5356 | if (ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1) { |
| 5357 | if ((ret = mbedtls_ssl_send_alert_message(ssl, |
| 5358 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, |
| 5359 | MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION)) |
| 5360 | != 0) { |
| 5361 | return ret; |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 5362 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5363 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5364 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || |
| 5365 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 5366 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5367 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 5368 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 5369 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5370 | } |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 5371 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5372 | /* At this point, we don't know whether the renegotiation has been |
| 5373 | * completed or not. The cases to consider are the following: |
| 5374 | * 1) The renegotiation is complete. In this case, no new record |
| 5375 | * has been read yet. |
| 5376 | * 2) The renegotiation is incomplete because the client received |
| 5377 | * an application data record while awaiting the ServerHello. |
| 5378 | * 3) The renegotiation is incomplete because the client received |
| 5379 | * a non-handshake, non-application data message while awaiting |
| 5380 | * the ServerHello. |
| 5381 | * In each of these case, looping will be the proper action: |
| 5382 | * - For 1), the next iteration will read a new record and check |
| 5383 | * if it's application data. |
| 5384 | * - For 2), the loop condition isn't satisfied as application data |
| 5385 | * is present, hence continue is the same as break |
| 5386 | * - For 3), the loop condition is satisfied and read_record |
| 5387 | * will re-deliver the message that was held back by the client |
| 5388 | * when expecting the ServerHello. |
| 5389 | */ |
| 5390 | continue; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5391 | } |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 5392 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5393 | else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) { |
| 5394 | if (ssl->conf->renego_max_records >= 0) { |
| 5395 | if (++ssl->renego_records_seen > ssl->conf->renego_max_records) { |
| 5396 | MBEDTLS_SSL_DEBUG_MSG(1, ("renegotiation requested, " |
| 5397 | "but not honored by client")); |
| 5398 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 5399 | } |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 5400 | } |
Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 5401 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5402 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 5403 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5404 | /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5405 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT) { |
| 5406 | MBEDTLS_SSL_DEBUG_MSG(2, ("ignoring non-fatal non-closure alert")); |
| 5407 | return MBEDTLS_ERR_SSL_WANT_READ; |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 5408 | } |
| 5409 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5410 | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA) { |
| 5411 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad application data message")); |
| 5412 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5413 | } |
| 5414 | |
| 5415 | ssl->in_offt = ssl->in_msg; |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 5416 | |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 5417 | /* We're going to return something now, cancel timer, |
| 5418 | * except if handshake (renegotiation) is in progress */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5419 | if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 5420 | mbedtls_ssl_set_timer(ssl, 0); |
| 5421 | } |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 5422 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 5423 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 5424 | /* If we requested renego but received AppData, resend HelloRequest. |
| 5425 | * Do it now, after setting in_offt, to avoid taking this branch |
| 5426 | * again if ssl_write_hello_request() returns WANT_WRITE */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5427 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5428 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 5429 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) { |
| 5430 | if ((ret = mbedtls_ssl_resend_hello_request(ssl)) != 0) { |
| 5431 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend_hello_request", |
| 5432 | ret); |
| 5433 | return ret; |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 5434 | } |
| 5435 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5436 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5437 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5438 | } |
| 5439 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5440 | n = (len < ssl->in_msglen) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5441 | ? len : ssl->in_msglen; |
| 5442 | |
ashesman | cf01d78 | 2022-02-17 11:08:27 +1300 | [diff] [blame] | 5443 | if (len != 0) { |
Ashley Duncan | 272cc19 | 2022-02-11 09:57:18 +1300 | [diff] [blame] | 5444 | memcpy(buf, ssl->in_offt, n); |
| 5445 | ssl->in_msglen -= n; |
| 5446 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5447 | |
gabor-mezei-arm | a321413 | 2020-07-15 10:55:00 +0200 | [diff] [blame] | 5448 | /* Zeroising the plaintext buffer to erase unused application data |
| 5449 | from the memory. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5450 | mbedtls_platform_zeroize(ssl->in_offt, n); |
gabor-mezei-arm | a321413 | 2020-07-15 10:55:00 +0200 | [diff] [blame] | 5451 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5452 | if (ssl->in_msglen == 0) { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5453 | /* all bytes consumed */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5454 | ssl->in_offt = NULL; |
Hanno Becker | bdf3905 | 2017-06-09 10:42:03 +0100 | [diff] [blame] | 5455 | ssl->keep_current_message = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5456 | } else { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5457 | /* more data available */ |
| 5458 | ssl->in_offt += n; |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5459 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5460 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5461 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= read")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5462 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5463 | return (int) n; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5464 | } |
| 5465 | |
| 5466 | /* |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 5467 | * Send application data to be encrypted by the SSL layer, taking care of max |
| 5468 | * fragment length and buffer size. |
| 5469 | * |
| 5470 | * According to RFC 5246 Section 6.2.1: |
| 5471 | * |
| 5472 | * Zero-length fragments of Application data MAY be sent as they are |
| 5473 | * potentially useful as a traffic analysis countermeasure. |
| 5474 | * |
| 5475 | * Therefore, it is possible that the input message length is 0 and the |
| 5476 | * corresponding return code is 0 on success. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5477 | */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 5478 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5479 | static int ssl_write_real(mbedtls_ssl_context *ssl, |
| 5480 | const unsigned char *buf, size_t len) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5481 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5482 | int ret = mbedtls_ssl_get_max_out_record_payload(ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 5483 | const size_t max_len = (size_t) ret; |
| 5484 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5485 | if (ret < 0) { |
| 5486 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_max_out_record_payload", ret); |
| 5487 | return ret; |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 5488 | } |
| 5489 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5490 | if (len > max_len) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5491 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5492 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 5493 | MBEDTLS_SSL_DEBUG_MSG(1, ("fragment larger than the (negotiated) " |
| 5494 | "maximum fragment length: %" MBEDTLS_PRINTF_SIZET |
| 5495 | " > %" MBEDTLS_PRINTF_SIZET, |
| 5496 | len, max_len)); |
| 5497 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5498 | } else |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 5499 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5500 | len = max_len; |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 5501 | } |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 5502 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5503 | if (ssl->out_left != 0) { |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 5504 | /* |
| 5505 | * The user has previously tried to send the data and |
| 5506 | * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially |
| 5507 | * written. In this case, we expect the high-level write function |
| 5508 | * (e.g. mbedtls_ssl_write()) to be called with the same parameters |
| 5509 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5510 | if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) { |
| 5511 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flush_output", ret); |
| 5512 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5513 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5514 | } else { |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 5515 | /* |
| 5516 | * The user is trying to send a message the first time, so we need to |
| 5517 | * copy the data into the internal buffers and setup the data structure |
| 5518 | * to keep track of partial writes |
| 5519 | */ |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 5520 | ssl->out_msglen = len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5521 | ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
Dave Rodgman | 1215557 | 2023-02-24 15:41:34 +0000 | [diff] [blame] | 5522 | if (len > 0) { |
| 5523 | memcpy(ssl->out_msg, buf, len); |
| 5524 | } |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 5525 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5526 | if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) { |
| 5527 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret); |
| 5528 | return ret; |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 5529 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5530 | } |
| 5531 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5532 | return (int) len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5533 | } |
| 5534 | |
| 5535 | /* |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 5536 | * Write application data, doing 1/n-1 splitting if necessary. |
| 5537 | * |
| 5538 | * With non-blocking I/O, ssl_write_real() may return WANT_WRITE, |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 5539 | * then the caller will call us again with the same arguments, so |
Hanno Becker | 2b187c4 | 2017-09-18 14:58:11 +0100 | [diff] [blame] | 5540 | * remember whether we already did the split or not. |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 5541 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5542 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 5543 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5544 | static int ssl_write_split(mbedtls_ssl_context *ssl, |
| 5545 | const unsigned char *buf, size_t len) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 5546 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5547 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 5548 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5549 | if (ssl->conf->cbc_record_splitting == |
| 5550 | MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED || |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 5551 | len <= 1 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5552 | ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 || |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5553 | mbedtls_cipher_get_cipher_mode(&ssl->transform_out->cipher_ctx_enc) |
| 5554 | != MBEDTLS_MODE_CBC) { |
| 5555 | return ssl_write_real(ssl, buf, len); |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 5556 | } |
| 5557 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5558 | if (ssl->split_done == 0) { |
| 5559 | if ((ret = ssl_write_real(ssl, buf, 1)) <= 0) { |
| 5560 | return ret; |
| 5561 | } |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 5562 | ssl->split_done = 1; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 5563 | } |
| 5564 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5565 | if ((ret = ssl_write_real(ssl, buf + 1, len - 1)) <= 0) { |
| 5566 | return ret; |
| 5567 | } |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 5568 | ssl->split_done = 0; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 5569 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5570 | return ret + 1; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 5571 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5572 | #endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */ |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 5573 | |
| 5574 | /* |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5575 | * Write application data (public-facing wrapper) |
| 5576 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5577 | int mbedtls_ssl_write(mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5578 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5579 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5580 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5581 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write")); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5582 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5583 | if (ssl == NULL || ssl->conf == NULL) { |
| 5584 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5585 | } |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5586 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 5587 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5588 | if ((ret = ssl_check_ctr_renegotiate(ssl)) != 0) { |
| 5589 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_check_ctr_renegotiate", ret); |
| 5590 | return ret; |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5591 | } |
| 5592 | #endif |
| 5593 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5594 | if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 5595 | if ((ret = mbedtls_ssl_handshake(ssl)) != 0) { |
| 5596 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret); |
| 5597 | return ret; |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5598 | } |
| 5599 | } |
| 5600 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 5601 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5602 | ret = ssl_write_split(ssl, buf, len); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5603 | #else |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5604 | ret = ssl_write_real(ssl, buf, len); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5605 | #endif |
| 5606 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5607 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write")); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5608 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5609 | return ret; |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5610 | } |
| 5611 | |
| 5612 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5613 | * Notify the peer that the connection is being closed |
| 5614 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5615 | int mbedtls_ssl_close_notify(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5616 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5617 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5618 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5619 | if (ssl == NULL || ssl->conf == NULL) { |
| 5620 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5621 | } |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5622 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5623 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write close notify")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5624 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5625 | if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 5626 | if ((ret = mbedtls_ssl_send_alert_message(ssl, |
| 5627 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, |
| 5628 | MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY)) != 0) { |
| 5629 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_send_alert_message", ret); |
| 5630 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5631 | } |
| 5632 | } |
| 5633 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5634 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write close notify")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5635 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5636 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5637 | } |
| 5638 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5639 | void mbedtls_ssl_transform_free(mbedtls_ssl_transform *transform) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5640 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5641 | if (transform == NULL) { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 5642 | return; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5643 | } |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 5644 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5645 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5646 | deflateEnd(&transform->ctx_deflate); |
| 5647 | inflateEnd(&transform->ctx_inflate); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5648 | #endif |
| 5649 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5650 | mbedtls_cipher_free(&transform->cipher_ctx_enc); |
| 5651 | mbedtls_cipher_free(&transform->cipher_ctx_dec); |
Manuel Pégourié-Gonnard | f71e587 | 2013-09-23 17:12:43 +0200 | [diff] [blame] | 5652 | |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 5653 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5654 | mbedtls_md_free(&transform->md_ctx_enc); |
| 5655 | mbedtls_md_free(&transform->md_ctx_dec); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 5656 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 5657 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5658 | mbedtls_platform_zeroize(transform, sizeof(mbedtls_ssl_transform)); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5659 | } |
| 5660 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 5661 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5662 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5663 | void mbedtls_ssl_buffering_free(mbedtls_ssl_context *ssl) |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 5664 | { |
| 5665 | unsigned offset; |
| 5666 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 5667 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5668 | if (hs == NULL) { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 5669 | return; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5670 | } |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 5671 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5672 | ssl_free_buffered_record(ssl); |
Hanno Becker | 283f5ef | 2018-08-24 09:34:47 +0100 | [diff] [blame] | 5673 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5674 | for (offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++) { |
| 5675 | ssl_buffering_free_slot(ssl, offset); |
| 5676 | } |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 5677 | } |
| 5678 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5679 | static void ssl_buffering_free_slot(mbedtls_ssl_context *ssl, |
| 5680 | uint8_t slot) |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 5681 | { |
| 5682 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 5683 | mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot]; |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 5684 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5685 | if (slot >= MBEDTLS_SSL_MAX_BUFFERED_HS) { |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 5686 | return; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5687 | } |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 5688 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5689 | if (hs_buf->is_valid == 1) { |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 5690 | hs->buffering.total_bytes_buffered -= hs_buf->data_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5691 | mbedtls_platform_zeroize(hs_buf->data, hs_buf->data_len); |
| 5692 | mbedtls_free(hs_buf->data); |
| 5693 | memset(hs_buf, 0, sizeof(mbedtls_ssl_hs_buffer)); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 5694 | } |
| 5695 | } |
| 5696 | |
| 5697 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5698 | |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5699 | /* |
| 5700 | * Convert version numbers to/from wire format |
| 5701 | * and, for DTLS, to/from TLS equivalent. |
| 5702 | * |
| 5703 | * For TLS this is the identity. |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 5704 | * For DTLS, use 1's complement (v -> 255 - v, and then map as follows: |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5705 | * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1) |
| 5706 | * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2) |
| 5707 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5708 | void mbedtls_ssl_write_version(int major, int minor, int transport, |
| 5709 | unsigned char ver[2]) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5710 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5711 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5712 | if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 5713 | if (minor == MBEDTLS_SSL_MINOR_VERSION_2) { |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5714 | --minor; /* DTLS 1.0 stored as TLS 1.1 internally */ |
| 5715 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5716 | } |
| 5717 | ver[0] = (unsigned char) (255 - (major - 2)); |
| 5718 | ver[1] = (unsigned char) (255 - (minor - 1)); |
| 5719 | } else |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 5720 | #else |
| 5721 | ((void) transport); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5722 | #endif |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 5723 | { |
| 5724 | ver[0] = (unsigned char) major; |
| 5725 | ver[1] = (unsigned char) minor; |
| 5726 | } |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5727 | } |
| 5728 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5729 | void mbedtls_ssl_read_version(int *major, int *minor, int transport, |
| 5730 | const unsigned char ver[2]) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5731 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5732 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5733 | if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5734 | *major = 255 - ver[0] + 2; |
| 5735 | *minor = 255 - ver[1] + 1; |
| 5736 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5737 | if (*minor == MBEDTLS_SSL_MINOR_VERSION_1) { |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5738 | ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5739 | } |
| 5740 | } else |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 5741 | #else |
| 5742 | ((void) transport); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5743 | #endif |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 5744 | { |
| 5745 | *major = ver[0]; |
| 5746 | *minor = ver[1]; |
| 5747 | } |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5748 | } |
| 5749 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5750 | #endif /* MBEDTLS_SSL_TLS_C */ |