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 |
Dave Rodgman | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame] | 6 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | /* |
| 9 | * The SSL 3.0 specification was drafted by Netscape in 1996, |
| 10 | * and became an IETF standard in 1999. |
| 11 | * |
| 12 | * http://wp.netscape.com/eng/ssl3/ |
| 13 | * http://www.ietf.org/rfc/rfc2246.txt |
| 14 | * http://www.ietf.org/rfc/rfc4346.txt |
| 15 | */ |
| 16 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 17 | #include "common.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 18 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 19 | #if defined(MBEDTLS_SSL_TLS_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 20 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 21 | #include "mbedtls/platform.h" |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 22 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 23 | #include "mbedtls/ssl.h" |
Manuel Pégourié-Gonnard | 5e94dde | 2015-05-26 11:57:05 +0200 | [diff] [blame] | 24 | #include "mbedtls/ssl_internal.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 25 | #include "mbedtls/debug.h" |
| 26 | #include "mbedtls/error.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 27 | #include "mbedtls/platform_util.h" |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 28 | #include "mbedtls/version.h" |
Gabor Mezei | c0ae1cf | 2021-10-20 12:09:35 +0200 | [diff] [blame] | 29 | #include "constant_time_internal.h" |
Gabor Mezei | e24dea8 | 2021-10-19 12:22:25 +0200 | [diff] [blame] | 30 | #include "mbedtls/constant_time.h" |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 31 | |
Gilles Peskine | a8cd2e6 | 2024-05-17 19:00:46 +0200 | [diff] [blame] | 32 | #include <limits.h> |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 33 | #include <string.h> |
| 34 | |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 35 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 36 | #include "mbedtls/psa_util.h" |
| 37 | #include "psa/crypto.h" |
| 38 | #endif |
| 39 | |
Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 40 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 41 | #include "mbedtls/oid.h" |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 42 | #endif |
| 43 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 44 | 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] | 45 | |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 46 | /* |
| 47 | * Start a timer. |
| 48 | * Passing millisecs = 0 cancels a running timer. |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 49 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 50 | 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] | 51 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 52 | if (ssl->f_set_timer == NULL) { |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 53 | return; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 54 | } |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 55 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 56 | MBEDTLS_SSL_DEBUG_MSG(3, ("set_timer to %d ms", (int) millisecs)); |
| 57 | ssl->f_set_timer(ssl->p_timer, millisecs / 4, millisecs); |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /* |
| 61 | * Return -1 is timer is expired, 0 if it isn't. |
| 62 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 63 | int mbedtls_ssl_check_timer(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 64 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 65 | if (ssl->f_get_timer == NULL) { |
| 66 | return 0; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 67 | } |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 68 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 69 | if (ssl->f_get_timer(ssl->p_timer) == 2) { |
| 70 | MBEDTLS_SSL_DEBUG_MSG(3, ("timer expired")); |
| 71 | return -1; |
| 72 | } |
| 73 | |
| 74 | return 0; |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 75 | } |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 76 | |
Hanno Becker | cfe4579 | 2019-07-03 16:13:00 +0100 | [diff] [blame] | 77 | #if defined(MBEDTLS_SSL_RECORD_CHECKING) |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 78 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 79 | static int ssl_parse_record_header(mbedtls_ssl_context const *ssl, |
| 80 | unsigned char *buf, |
| 81 | size_t len, |
| 82 | mbedtls_record *rec); |
Hanno Becker | 5422981 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 83 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 84 | int mbedtls_ssl_check_record(mbedtls_ssl_context const *ssl, |
| 85 | unsigned char *buf, |
| 86 | size_t buflen) |
Hanno Becker | cfe4579 | 2019-07-03 16:13:00 +0100 | [diff] [blame] | 87 | { |
Hanno Becker | 5422981 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 88 | int ret = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 89 | MBEDTLS_SSL_DEBUG_MSG(1, ("=> mbedtls_ssl_check_record")); |
| 90 | MBEDTLS_SSL_DEBUG_BUF(3, "record buffer", buf, buflen); |
Hanno Becker | 5422981 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 91 | |
| 92 | /* We don't support record checking in TLS because |
| 93 | * (a) there doesn't seem to be a usecase for it, and |
| 94 | * (b) In SSLv3 and TLS 1.0, CBC record decryption has state |
| 95 | * and we'd need to backup the transform here. |
| 96 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 97 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM) { |
Hanno Becker | 5422981 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 98 | ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 99 | goto exit; |
| 100 | } |
| 101 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 102 | else { |
irwir | 734f0cf | 2019-09-26 21:03:24 +0300 | [diff] [blame] | 103 | mbedtls_record rec; |
| 104 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 105 | ret = ssl_parse_record_header(ssl, buf, buflen, &rec); |
| 106 | if (ret != 0) { |
| 107 | MBEDTLS_SSL_DEBUG_RET(3, "ssl_parse_record_header", ret); |
Hanno Becker | 5422981 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 108 | goto exit; |
| 109 | } |
| 110 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 111 | if (ssl->transform_in != NULL) { |
| 112 | ret = mbedtls_ssl_decrypt_buf(ssl, ssl->transform_in, &rec); |
| 113 | if (ret != 0) { |
| 114 | MBEDTLS_SSL_DEBUG_RET(3, "mbedtls_ssl_decrypt_buf", ret); |
Hanno Becker | 5422981 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 115 | goto exit; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 120 | |
| 121 | exit: |
| 122 | /* On success, we have decrypted the buffer in-place, so make |
| 123 | * sure we don't leak any plaintext data. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 124 | mbedtls_platform_zeroize(buf, buflen); |
Hanno Becker | 5422981 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 125 | |
| 126 | /* For the purpose of this API, treat messages with unexpected CID |
| 127 | * as well as such from future epochs as unexpected. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 128 | if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID || |
| 129 | ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) { |
Hanno Becker | 5422981 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 130 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
| 131 | } |
| 132 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 133 | MBEDTLS_SSL_DEBUG_MSG(1, ("<= mbedtls_ssl_check_record")); |
| 134 | return ret; |
Hanno Becker | cfe4579 | 2019-07-03 16:13:00 +0100 | [diff] [blame] | 135 | } |
| 136 | #endif /* MBEDTLS_SSL_RECORD_CHECKING */ |
| 137 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 138 | #define SSL_DONT_FORCE_FLUSH 0 |
| 139 | #define SSL_FORCE_FLUSH 1 |
| 140 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 141 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 142 | |
Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 143 | /* Forward declarations for functions related to message buffering. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 144 | static void ssl_buffering_free_slot(mbedtls_ssl_context *ssl, |
| 145 | uint8_t slot); |
| 146 | static void ssl_free_buffered_record(mbedtls_ssl_context *ssl); |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 147 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 148 | static int ssl_load_buffered_message(mbedtls_ssl_context *ssl); |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 149 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 150 | static int ssl_load_buffered_record(mbedtls_ssl_context *ssl); |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 151 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 152 | static int ssl_buffer_message(mbedtls_ssl_context *ssl); |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 153 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 154 | static int ssl_buffer_future_record(mbedtls_ssl_context *ssl, |
| 155 | mbedtls_record const *rec); |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 156 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 157 | static int ssl_next_record_is_in_datagram(mbedtls_ssl_context *ssl); |
Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 158 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 159 | 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] | 160 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 161 | size_t mtu = mbedtls_ssl_get_current_mtu(ssl); |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 162 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 163 | size_t out_buf_len = ssl->out_buf_len; |
| 164 | #else |
| 165 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 166 | #endif |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 167 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 168 | if (mtu != 0 && mtu < out_buf_len) { |
| 169 | return mtu; |
| 170 | } |
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 | return out_buf_len; |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 173 | } |
| 174 | |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 175 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 176 | 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] | 177 | { |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 178 | size_t const bytes_written = ssl->out_left; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 179 | size_t const mtu = ssl_get_maximum_datagram_size(ssl); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 180 | |
| 181 | /* Double-check that the write-index hasn't gone |
| 182 | * past what we can transmit in a single datagram. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 183 | if (bytes_written > mtu) { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 184 | /* Should never happen... */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 185 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 186 | } |
| 187 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 188 | return (int) (mtu - bytes_written); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 189 | } |
| 190 | |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 191 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 192 | 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] | 193 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 194 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 195 | size_t remaining, expansion; |
Andrzej Kurek | 748face | 2018-10-11 07:20:19 -0400 | [diff] [blame] | 196 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 197 | |
| 198 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 199 | const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 200 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 201 | if (max_len > mfl) { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 202 | max_len = mfl; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 203 | } |
Hanno Becker | f4b010e | 2018-08-24 10:47:29 +0100 | [diff] [blame] | 204 | |
| 205 | /* By the standard (RFC 6066 Sect. 4), the MFL extension |
| 206 | * only limits the maximum record payload size, so in theory |
| 207 | * we would be allowed to pack multiple records of payload size |
| 208 | * MFL into a single datagram. However, this would mean that there's |
| 209 | * no way to explicitly communicate MTU restrictions to the peer. |
| 210 | * |
| 211 | * The following reduction of max_len makes sure that we never |
| 212 | * write datagrams larger than MFL + Record Expansion Overhead. |
| 213 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 214 | if (max_len <= ssl->out_left) { |
| 215 | return 0; |
| 216 | } |
Hanno Becker | f4b010e | 2018-08-24 10:47:29 +0100 | [diff] [blame] | 217 | |
| 218 | max_len -= ssl->out_left; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 219 | #endif |
| 220 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 221 | ret = ssl_get_remaining_space_in_datagram(ssl); |
| 222 | if (ret < 0) { |
| 223 | return ret; |
| 224 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 225 | remaining = (size_t) ret; |
| 226 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 227 | ret = mbedtls_ssl_get_record_expansion(ssl); |
| 228 | if (ret < 0) { |
| 229 | return ret; |
| 230 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 231 | expansion = (size_t) ret; |
| 232 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 233 | if (remaining <= expansion) { |
| 234 | return 0; |
| 235 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 236 | |
| 237 | remaining -= expansion; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 238 | if (remaining >= max_len) { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 239 | remaining = max_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 240 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 241 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 242 | return (int) remaining; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 243 | } |
| 244 | |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 245 | /* |
| 246 | * Double the retransmit timeout value, within the allowed range, |
| 247 | * returning -1 if the maximum value has already been reached. |
| 248 | */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 249 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 250 | static int ssl_double_retransmit_timeout(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 251 | { |
| 252 | uint32_t new_timeout; |
| 253 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 254 | if (ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max) { |
| 255 | return -1; |
| 256 | } |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 257 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 258 | /* Implement the final paragraph of RFC 6347 section 4.1.1.1 |
| 259 | * in the following way: after the initial transmission and a first |
| 260 | * retransmission, back off to a temporary estimated MTU of 508 bytes. |
| 261 | * This value is guaranteed to be deliverable (if not guaranteed to be |
| 262 | * delivered) of any compliant IPv4 (and IPv6) network, and should work |
| 263 | * on most non-IP stacks too. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 264 | if (ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min) { |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 265 | ssl->handshake->mtu = 508; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 266 | 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] | 267 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 268 | |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 269 | new_timeout = 2 * ssl->handshake->retransmit_timeout; |
| 270 | |
| 271 | /* Avoid arithmetic overflow and range overflow */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 272 | if (new_timeout < ssl->handshake->retransmit_timeout || |
| 273 | new_timeout > ssl->conf->hs_timeout_max) { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 274 | new_timeout = ssl->conf->hs_timeout_max; |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | ssl->handshake->retransmit_timeout = new_timeout; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 278 | MBEDTLS_SSL_DEBUG_MSG(3, ("update timeout value to %lu millisecs", |
| 279 | (unsigned long) ssl->handshake->retransmit_timeout)); |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 280 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 281 | return 0; |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 282 | } |
| 283 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 284 | static void ssl_reset_retransmit_timeout(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 285 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 286 | ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 287 | MBEDTLS_SSL_DEBUG_MSG(3, ("update timeout value to %lu millisecs", |
| 288 | (unsigned long) ssl->handshake->retransmit_timeout)); |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 289 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 290 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 291 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 292 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 293 | int (*mbedtls_ssl_hw_record_init)(mbedtls_ssl_context *ssl, |
| 294 | const unsigned char *key_enc, const unsigned char *key_dec, |
| 295 | size_t keylen, |
| 296 | const unsigned char *iv_enc, const unsigned char *iv_dec, |
| 297 | size_t ivlen, |
| 298 | const unsigned char *mac_enc, const unsigned char *mac_dec, |
| 299 | size_t maclen) = NULL; |
| 300 | int (*mbedtls_ssl_hw_record_activate)(mbedtls_ssl_context *ssl, int direction) = NULL; |
| 301 | int (*mbedtls_ssl_hw_record_reset)(mbedtls_ssl_context *ssl) = NULL; |
| 302 | int (*mbedtls_ssl_hw_record_write)(mbedtls_ssl_context *ssl) = NULL; |
| 303 | int (*mbedtls_ssl_hw_record_read)(mbedtls_ssl_context *ssl) = NULL; |
| 304 | 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] | 305 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 306 | |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 307 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 308 | * Encryption/decryption functions |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 309 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 310 | |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 311 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) || \ |
| 312 | defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
Hanno Becker | 1399692 | 2020-05-28 16:15:19 +0100 | [diff] [blame] | 313 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 314 | static size_t ssl_compute_padding_length(size_t len, |
| 315 | size_t granularity) |
Hanno Becker | 1399692 | 2020-05-28 16:15:19 +0100 | [diff] [blame] | 316 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 317 | return (granularity - (len + 1) % granularity) % granularity; |
Hanno Becker | 1399692 | 2020-05-28 16:15:19 +0100 | [diff] [blame] | 318 | } |
| 319 | |
Hanno Becker | 581bc1b | 2020-05-04 12:20:03 +0100 | [diff] [blame] | 320 | /* This functions transforms a (D)TLS plaintext fragment and a record content |
| 321 | * type into an instance of the (D)TLSInnerPlaintext structure. This is used |
| 322 | * in DTLS 1.2 + CID and within TLS 1.3 to allow flexible padding and to protect |
| 323 | * a record's content type. |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 324 | * |
| 325 | * struct { |
| 326 | * opaque content[DTLSPlaintext.length]; |
| 327 | * ContentType real_type; |
| 328 | * uint8 zeros[length_of_padding]; |
Hanno Becker | 581bc1b | 2020-05-04 12:20:03 +0100 | [diff] [blame] | 329 | * } (D)TLSInnerPlaintext; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 330 | * |
| 331 | * Input: |
| 332 | * - `content`: The beginning of the buffer holding the |
| 333 | * plaintext to be wrapped. |
| 334 | * - `*content_size`: The length of the plaintext in Bytes. |
| 335 | * - `max_len`: The number of Bytes available starting from |
| 336 | * `content`. This must be `>= *content_size`. |
| 337 | * - `rec_type`: The desired record content type. |
| 338 | * |
| 339 | * Output: |
Hanno Becker | 581bc1b | 2020-05-04 12:20:03 +0100 | [diff] [blame] | 340 | * - `content`: The beginning of the resulting (D)TLSInnerPlaintext structure. |
| 341 | * - `*content_size`: The length of the resulting (D)TLSInnerPlaintext structure. |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 342 | * |
| 343 | * Returns: |
| 344 | * - `0` on success. |
| 345 | * - A negative error code if `max_len` didn't offer enough space |
| 346 | * for the expansion. |
| 347 | */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 348 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 349 | static int ssl_build_inner_plaintext(unsigned char *content, |
| 350 | size_t *content_size, |
| 351 | size_t remaining, |
| 352 | uint8_t rec_type, |
| 353 | size_t pad) |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 354 | { |
| 355 | size_t len = *content_size; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 356 | |
| 357 | /* Write real content type */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 358 | if (remaining == 0) { |
| 359 | return -1; |
| 360 | } |
| 361 | content[len] = rec_type; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 362 | len++; |
| 363 | remaining--; |
| 364 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 365 | if (remaining < pad) { |
| 366 | return -1; |
| 367 | } |
| 368 | memset(content + len, 0, pad); |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 369 | len += pad; |
| 370 | remaining -= pad; |
| 371 | |
| 372 | *content_size = len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 373 | return 0; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 374 | } |
| 375 | |
Hanno Becker | 581bc1b | 2020-05-04 12:20:03 +0100 | [diff] [blame] | 376 | /* This function parses a (D)TLSInnerPlaintext structure. |
| 377 | * See ssl_build_inner_plaintext() for details. */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 378 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 379 | static int ssl_parse_inner_plaintext(unsigned char const *content, |
| 380 | size_t *content_size, |
| 381 | uint8_t *rec_type) |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 382 | { |
| 383 | size_t remaining = *content_size; |
| 384 | |
| 385 | /* Determine length of padding by skipping zeroes from the back. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 386 | do { |
| 387 | if (remaining == 0) { |
| 388 | return -1; |
| 389 | } |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 390 | remaining--; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 391 | } while (content[remaining] == 0); |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 392 | |
| 393 | *content_size = remaining; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 394 | *rec_type = content[remaining]; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 395 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 396 | return 0; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 397 | } |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 398 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID || |
| 399 | MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 400 | |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 401 | /* `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] | 402 | * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 403 | static void ssl_extract_add_data_from_record(unsigned char *add_data, |
| 404 | size_t *add_data_len, |
| 405 | mbedtls_record *rec, |
| 406 | unsigned minor_ver) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 407 | { |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 408 | /* Quoting RFC 5246 (TLS 1.2): |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 409 | * |
| 410 | * additional_data = seq_num + TLSCompressed.type + |
| 411 | * TLSCompressed.version + TLSCompressed.length; |
| 412 | * |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 413 | * For the CID extension, this is extended as follows |
| 414 | * (quoting draft-ietf-tls-dtls-connection-id-05, |
| 415 | * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05): |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 416 | * |
| 417 | * additional_data = seq_num + DTLSPlaintext.type + |
| 418 | * DTLSPlaintext.version + |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 419 | * cid + |
| 420 | * cid_length + |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 421 | * length_of_DTLSInnerPlaintext; |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 422 | * |
| 423 | * For TLS 1.3, the record sequence number is dropped from the AAD |
| 424 | * and encoded within the nonce of the AEAD operation instead. |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 425 | */ |
| 426 | |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 427 | unsigned char *cur = add_data; |
| 428 | |
David Horstmann | 197b240 | 2022-10-26 18:06:31 +0100 | [diff] [blame] | 429 | int is_tls13 = 0; |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 430 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 431 | if (minor_ver == MBEDTLS_SSL_MINOR_VERSION_4) { |
David Horstmann | 197b240 | 2022-10-26 18:06:31 +0100 | [diff] [blame] | 432 | is_tls13 = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 433 | } |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 434 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 435 | if (!is_tls13) { |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 436 | ((void) minor_ver); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 437 | memcpy(cur, rec->ctr, sizeof(rec->ctr)); |
| 438 | cur += sizeof(rec->ctr); |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | *cur = rec->type; |
| 442 | cur++; |
| 443 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 444 | memcpy(cur, rec->ver, sizeof(rec->ver)); |
| 445 | cur += sizeof(rec->ver); |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 446 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 447 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 448 | if (rec->cid_len != 0) { |
| 449 | memcpy(cur, rec->cid, rec->cid_len); |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 450 | cur += rec->cid_len; |
| 451 | |
| 452 | *cur = rec->cid_len; |
| 453 | cur++; |
| 454 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 455 | MBEDTLS_PUT_UINT16_BE(rec->data_len, cur, 0); |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 456 | cur += 2; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 457 | } else |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 458 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 95e4bbc | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 459 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 460 | MBEDTLS_PUT_UINT16_BE(rec->data_len, cur, 0); |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 461 | cur += 2; |
Hanno Becker | 95e4bbc | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 462 | } |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 463 | |
| 464 | *add_data_len = cur - add_data; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Hanno Becker | 9d062f9 | 2020-02-07 10:26:36 +0000 | [diff] [blame] | 467 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 468 | |
| 469 | #define SSL3_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */ |
| 470 | |
| 471 | /* |
| 472 | * SSLv3.0 MAC functions |
| 473 | */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 474 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 475 | static int ssl_mac(mbedtls_md_context_t *md_ctx, |
| 476 | const unsigned char *secret, |
| 477 | const unsigned char *buf, size_t len, |
| 478 | const unsigned char *ctr, int type, |
| 479 | unsigned char out[SSL3_MAC_MAX_BYTES]) |
Hanno Becker | 9d062f9 | 2020-02-07 10:26:36 +0000 | [diff] [blame] | 480 | { |
| 481 | unsigned char header[11]; |
| 482 | unsigned char padding[48]; |
| 483 | int padlen; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 484 | int md_size = mbedtls_md_get_size(md_ctx->md_info); |
| 485 | int md_type = mbedtls_md_get_type(md_ctx->md_info); |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 486 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 9d062f9 | 2020-02-07 10:26:36 +0000 | [diff] [blame] | 487 | |
| 488 | /* Only MD5 and SHA-1 supported */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 489 | if (md_type == MBEDTLS_MD_MD5) { |
Hanno Becker | 9d062f9 | 2020-02-07 10:26:36 +0000 | [diff] [blame] | 490 | padlen = 48; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 491 | } else { |
Hanno Becker | 9d062f9 | 2020-02-07 10:26:36 +0000 | [diff] [blame] | 492 | padlen = 40; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 493 | } |
Hanno Becker | 9d062f9 | 2020-02-07 10:26:36 +0000 | [diff] [blame] | 494 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 495 | memcpy(header, ctr, 8); |
Joe Subbiani | a651e6f | 2021-08-23 11:35:25 +0100 | [diff] [blame] | 496 | header[8] = (unsigned char) type; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 497 | MBEDTLS_PUT_UINT16_BE(len, header, 9); |
Hanno Becker | 9d062f9 | 2020-02-07 10:26:36 +0000 | [diff] [blame] | 498 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 499 | memset(padding, 0x36, padlen); |
| 500 | ret = mbedtls_md_starts(md_ctx); |
| 501 | if (ret != 0) { |
| 502 | return ret; |
| 503 | } |
| 504 | ret = mbedtls_md_update(md_ctx, secret, md_size); |
| 505 | if (ret != 0) { |
| 506 | return ret; |
| 507 | } |
| 508 | ret = mbedtls_md_update(md_ctx, padding, padlen); |
| 509 | if (ret != 0) { |
| 510 | return ret; |
| 511 | } |
| 512 | ret = mbedtls_md_update(md_ctx, header, 11); |
| 513 | if (ret != 0) { |
| 514 | return ret; |
| 515 | } |
| 516 | ret = mbedtls_md_update(md_ctx, buf, len); |
| 517 | if (ret != 0) { |
| 518 | return ret; |
| 519 | } |
| 520 | ret = mbedtls_md_finish(md_ctx, out); |
| 521 | if (ret != 0) { |
| 522 | return ret; |
| 523 | } |
Hanno Becker | 9d062f9 | 2020-02-07 10:26:36 +0000 | [diff] [blame] | 524 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 525 | memset(padding, 0x5C, padlen); |
| 526 | ret = mbedtls_md_starts(md_ctx); |
| 527 | if (ret != 0) { |
| 528 | return ret; |
| 529 | } |
| 530 | ret = mbedtls_md_update(md_ctx, secret, md_size); |
| 531 | if (ret != 0) { |
| 532 | return ret; |
| 533 | } |
| 534 | ret = mbedtls_md_update(md_ctx, padding, padlen); |
| 535 | if (ret != 0) { |
| 536 | return ret; |
| 537 | } |
| 538 | ret = mbedtls_md_update(md_ctx, out, md_size); |
| 539 | if (ret != 0) { |
| 540 | return ret; |
| 541 | } |
| 542 | ret = mbedtls_md_finish(md_ctx, out); |
| 543 | if (ret != 0) { |
| 544 | return ret; |
| 545 | } |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 546 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 547 | return 0; |
Hanno Becker | 9d062f9 | 2020-02-07 10:26:36 +0000 | [diff] [blame] | 548 | } |
| 549 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 550 | |
Hanno Becker | 67a37db | 2020-05-28 16:27:07 +0100 | [diff] [blame] | 551 | #if defined(MBEDTLS_GCM_C) || \ |
| 552 | defined(MBEDTLS_CCM_C) || \ |
| 553 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 554 | MBEDTLS_CHECK_RETURN_CRITICAL |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 555 | static int ssl_transform_aead_dynamic_iv_is_explicit( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 556 | mbedtls_ssl_transform const *transform) |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 557 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 558 | return transform->ivlen != transform->fixed_ivlen; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 559 | } |
| 560 | |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 561 | /* Compute IV := ( fixed_iv || 0 ) XOR ( 0 || dynamic_IV ) |
| 562 | * |
| 563 | * Concretely, this occurs in two variants: |
| 564 | * |
| 565 | * a) Fixed and dynamic IV lengths add up to total IV length, giving |
| 566 | * IV = fixed_iv || dynamic_iv |
| 567 | * |
Hanno Becker | 1595281 | 2020-06-04 13:31:46 +0100 | [diff] [blame] | 568 | * This variant is used in TLS 1.2 when used with GCM or CCM. |
| 569 | * |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 570 | * b) Fixed IV lengths matches total IV length, giving |
| 571 | * IV = fixed_iv XOR ( 0 || dynamic_iv ) |
Hanno Becker | 1595281 | 2020-06-04 13:31:46 +0100 | [diff] [blame] | 572 | * |
| 573 | * This variant occurs in TLS 1.3 and for TLS 1.2 when using ChaChaPoly. |
| 574 | * |
| 575 | * See also the documentation of mbedtls_ssl_transform. |
Hanno Becker | f486e28 | 2020-06-04 13:33:08 +0100 | [diff] [blame] | 576 | * |
| 577 | * This function has the precondition that |
| 578 | * |
| 579 | * dst_iv_len >= max( fixed_iv_len, dynamic_iv_len ) |
| 580 | * |
| 581 | * which has to be ensured by the caller. If this precondition |
| 582 | * violated, the behavior of this function is undefined. |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 583 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 584 | static void ssl_build_record_nonce(unsigned char *dst_iv, |
| 585 | size_t dst_iv_len, |
| 586 | unsigned char const *fixed_iv, |
| 587 | size_t fixed_iv_len, |
| 588 | unsigned char const *dynamic_iv, |
| 589 | size_t dynamic_iv_len) |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 590 | { |
| 591 | size_t i; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 592 | |
| 593 | /* Start with Fixed IV || 0 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 594 | memset(dst_iv, 0, dst_iv_len); |
| 595 | memcpy(dst_iv, fixed_iv, fixed_iv_len); |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 596 | |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 597 | dst_iv += dst_iv_len - dynamic_iv_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 598 | for (i = 0; i < dynamic_iv_len; i++) { |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 599 | dst_iv[i] ^= dynamic_iv[i]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 600 | } |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 601 | } |
Hanno Becker | 67a37db | 2020-05-28 16:27:07 +0100 | [diff] [blame] | 602 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 603 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 604 | int mbedtls_ssl_encrypt_buf(mbedtls_ssl_context *ssl, |
| 605 | mbedtls_ssl_transform *transform, |
| 606 | mbedtls_record *rec, |
| 607 | int (*f_rng)(void *, unsigned char *, size_t), |
| 608 | void *p_rng) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 609 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 610 | mbedtls_cipher_mode_t mode; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 611 | int auth_done = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 612 | unsigned char *data; |
| 613 | unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX]; |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 614 | size_t add_data_len; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 615 | size_t post_avail; |
| 616 | |
| 617 | /* The SSL context is only used for debugging purposes! */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 618 | #if !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | a7505d1 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 619 | ssl = NULL; /* make sure we don't use it except for debug */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 620 | ((void) ssl); |
| 621 | #endif |
| 622 | |
| 623 | /* The PRNG is used for dynamic IV generation that's used |
| 624 | * for CBC transformations in TLS 1.1 and TLS 1.2. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 625 | #if !(defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) && \ |
| 626 | (defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2))) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 627 | ((void) f_rng); |
| 628 | ((void) p_rng); |
| 629 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 630 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 631 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> encrypt buf")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 632 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 633 | if (transform == NULL) { |
| 634 | MBEDTLS_SSL_DEBUG_MSG(1, ("no transform provided to encrypt_buf")); |
| 635 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 636 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 637 | if (rec == NULL |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 638 | || rec->buf == NULL |
| 639 | || rec->buf_len < rec->data_offset |
| 640 | || rec->buf_len - rec->data_offset < rec->data_len |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 641 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 642 | || rec->cid_len != 0 |
| 643 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 644 | ) { |
| 645 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad record structure provided to encrypt_buf")); |
| 646 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 647 | } |
| 648 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 649 | data = rec->buf + rec->data_offset; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 650 | post_avail = rec->buf_len - (rec->data_len + rec->data_offset); |
| 651 | MBEDTLS_SSL_DEBUG_BUF(4, "before encrypt: output payload", |
| 652 | data, rec->data_len); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 653 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 654 | mode = mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 655 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 656 | if (rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN) { |
| 657 | MBEDTLS_SSL_DEBUG_MSG(1, ("Record content %" MBEDTLS_PRINTF_SIZET |
| 658 | " too large, maximum %" MBEDTLS_PRINTF_SIZET, |
| 659 | rec->data_len, |
| 660 | (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN)); |
| 661 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 662 | } |
Manuel Pégourié-Gonnard | 60346be | 2014-11-21 11:38:37 +0100 | [diff] [blame] | 663 | |
Hanno Becker | 9231340 | 2020-05-20 13:58:58 +0100 | [diff] [blame] | 664 | /* The following two code paths implement the (D)TLSInnerPlaintext |
| 665 | * structure present in TLS 1.3 and DTLS 1.2 + CID. |
| 666 | * |
| 667 | * See ssl_build_inner_plaintext() for more information. |
| 668 | * |
| 669 | * Note that this changes `rec->data_len`, and hence |
| 670 | * `post_avail` needs to be recalculated afterwards. |
| 671 | * |
| 672 | * Note also that the two code paths cannot occur simultaneously |
| 673 | * since they apply to different versions of the protocol. There |
| 674 | * is hence no risk of double-addition of the inner plaintext. |
| 675 | */ |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 676 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 677 | if (transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4) { |
Hanno Becker | 1399692 | 2020-05-28 16:15:19 +0100 | [diff] [blame] | 678 | size_t padding = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 679 | ssl_compute_padding_length(rec->data_len, |
| 680 | MBEDTLS_SSL_TLS1_3_PADDING_GRANULARITY); |
| 681 | if (ssl_build_inner_plaintext(data, |
| 682 | &rec->data_len, |
| 683 | post_avail, |
| 684 | rec->type, |
| 685 | padding) != 0) { |
| 686 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | rec->type = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
| 690 | } |
| 691 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
| 692 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 693 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 694 | /* |
| 695 | * Add CID information |
| 696 | */ |
| 697 | rec->cid_len = transform->out_cid_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 698 | memcpy(rec->cid, transform->out_cid, transform->out_cid_len); |
| 699 | MBEDTLS_SSL_DEBUG_BUF(3, "CID", rec->cid, rec->cid_len); |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 700 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 701 | if (rec->cid_len != 0) { |
Hanno Becker | 1399692 | 2020-05-28 16:15:19 +0100 | [diff] [blame] | 702 | size_t padding = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 703 | ssl_compute_padding_length(rec->data_len, |
| 704 | MBEDTLS_SSL_CID_PADDING_GRANULARITY); |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 705 | /* |
Hanno Becker | 07dc97d | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 706 | * Wrap plaintext into DTLSInnerPlaintext structure. |
Hanno Becker | 581bc1b | 2020-05-04 12:20:03 +0100 | [diff] [blame] | 707 | * See ssl_build_inner_plaintext() for more information. |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 708 | * |
Hanno Becker | 07dc97d | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 709 | * Note that this changes `rec->data_len`, and hence |
| 710 | * `post_avail` needs to be recalculated afterwards. |
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 (ssl_build_inner_plaintext(data, |
| 713 | &rec->data_len, |
| 714 | post_avail, |
| 715 | rec->type, |
| 716 | padding) != 0) { |
| 717 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | rec->type = MBEDTLS_SSL_MSG_CID; |
| 721 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 722 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 723 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 724 | post_avail = rec->buf_len - (rec->data_len + rec->data_offset); |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 725 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 726 | /* |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 727 | * Add MAC before if needed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 728 | */ |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 729 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 730 | if (mode == MBEDTLS_MODE_STREAM || |
| 731 | (mode == MBEDTLS_MODE_CBC |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 732 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 733 | && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 734 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 735 | )) { |
| 736 | if (post_avail < transform->maclen) { |
| 737 | MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough")); |
| 738 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 741 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 742 | if (transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) { |
Hanno Becker | 9d062f9 | 2020-02-07 10:26:36 +0000 | [diff] [blame] | 743 | unsigned char mac[SSL3_MAC_MAX_BYTES]; |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 744 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 745 | ret = ssl_mac(&transform->md_ctx_enc, transform->mac_enc, |
| 746 | data, rec->data_len, rec->ctr, rec->type, mac); |
| 747 | if (ret == 0) { |
| 748 | memcpy(data + rec->data_len, mac, transform->maclen); |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 749 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 750 | mbedtls_platform_zeroize(mac, transform->maclen); |
| 751 | if (ret != 0) { |
| 752 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_mac", ret); |
| 753 | return ret; |
| 754 | } |
| 755 | } else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 756 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 757 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 758 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 759 | if (transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1) { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 760 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 761 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 762 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 763 | ssl_extract_add_data_from_record(add_data, &add_data_len, rec, |
| 764 | transform->minor_ver); |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 765 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 766 | ret = mbedtls_md_hmac_update(&transform->md_ctx_enc, |
| 767 | add_data, add_data_len); |
| 768 | if (ret != 0) { |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 769 | goto hmac_failed_etm_disabled; |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 770 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 771 | ret = mbedtls_md_hmac_update(&transform->md_ctx_enc, |
| 772 | data, rec->data_len); |
| 773 | if (ret != 0) { |
| 774 | goto hmac_failed_etm_disabled; |
| 775 | } |
| 776 | ret = mbedtls_md_hmac_finish(&transform->md_ctx_enc, mac); |
| 777 | if (ret != 0) { |
| 778 | goto hmac_failed_etm_disabled; |
| 779 | } |
| 780 | ret = mbedtls_md_hmac_reset(&transform->md_ctx_enc); |
| 781 | if (ret != 0) { |
| 782 | goto hmac_failed_etm_disabled; |
| 783 | } |
| 784 | |
| 785 | memcpy(data + rec->data_len, mac, transform->maclen); |
| 786 | |
| 787 | hmac_failed_etm_disabled: |
| 788 | mbedtls_platform_zeroize(mac, transform->maclen); |
| 789 | if (ret != 0) { |
| 790 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_hmac_xxx", ret); |
| 791 | return ret; |
| 792 | } |
| 793 | } else |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 794 | #endif |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 795 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 796 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 797 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 798 | } |
| 799 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 800 | MBEDTLS_SSL_DEBUG_BUF(4, "computed mac", data + rec->data_len, |
| 801 | transform->maclen); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 802 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 803 | rec->data_len += transform->maclen; |
| 804 | post_avail -= transform->maclen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 805 | auth_done++; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 806 | } |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 807 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 808 | |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 809 | /* |
| 810 | * Encrypt |
| 811 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 812 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 813 | if (mode == MBEDTLS_MODE_STREAM) { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 814 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 815 | size_t olen; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 816 | MBEDTLS_SSL_DEBUG_MSG(3, ("before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", " |
| 817 | "including %d bytes of padding", |
| 818 | rec->data_len, 0)); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 819 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 820 | if ((ret = mbedtls_cipher_crypt(&transform->cipher_ctx_enc, |
| 821 | transform->iv_enc, transform->ivlen, |
| 822 | data, rec->data_len, |
| 823 | data, &olen)) != 0) { |
| 824 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret); |
| 825 | return ret; |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 826 | } |
| 827 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 828 | if (rec->data_len != olen) { |
| 829 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 830 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 831 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 832 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 833 | #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 834 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 835 | #if defined(MBEDTLS_GCM_C) || \ |
| 836 | defined(MBEDTLS_CCM_C) || \ |
| 837 | defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 838 | if (mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 839 | mode == MBEDTLS_MODE_CCM || |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 840 | mode == MBEDTLS_MODE_CHACHAPOLY) { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 841 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 842 | unsigned char iv[12]; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 843 | unsigned char *dynamic_iv; |
| 844 | size_t dynamic_iv_len; |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 845 | int dynamic_iv_is_explicit = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 846 | ssl_transform_aead_dynamic_iv_is_explicit(transform); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 847 | |
Hanno Becker | bd5ed1d | 2020-05-21 15:26:39 +0100 | [diff] [blame] | 848 | /* Check that there's space for the authentication tag. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 849 | if (post_avail < transform->taglen) { |
| 850 | MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough")); |
| 851 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 852 | } |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 853 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 854 | /* |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 855 | * Build nonce for AEAD encryption. |
| 856 | * |
| 857 | * Note: In the case of CCM and GCM in TLS 1.2, the dynamic |
| 858 | * part of the IV is prepended to the ciphertext and |
| 859 | * can be chosen freely - in particular, it need not |
| 860 | * agree with the record sequence number. |
| 861 | * However, since ChaChaPoly as well as all AEAD modes |
| 862 | * in TLS 1.3 use the record sequence number as the |
| 863 | * dynamic part of the nonce, we uniformly use the |
| 864 | * record sequence number here in all cases. |
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 | dynamic_iv = rec->ctr; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 867 | dynamic_iv_len = sizeof(rec->ctr); |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 868 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 869 | ssl_build_record_nonce(iv, sizeof(iv), |
| 870 | transform->iv_enc, |
| 871 | transform->fixed_ivlen, |
| 872 | dynamic_iv, |
| 873 | dynamic_iv_len); |
Manuel Pégourié-Gonnard | d056ce0 | 2014-10-29 22:29:20 +0100 | [diff] [blame] | 874 | |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 875 | /* |
| 876 | * Build additional data for AEAD encryption. |
| 877 | * This depends on the TLS version. |
| 878 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 879 | ssl_extract_add_data_from_record(add_data, &add_data_len, rec, |
| 880 | transform->minor_ver); |
Hanno Becker | 1f10d76 | 2019-04-26 13:34:37 +0100 | [diff] [blame] | 881 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 882 | MBEDTLS_SSL_DEBUG_BUF(4, "IV used (internal)", |
| 883 | iv, transform->ivlen); |
| 884 | MBEDTLS_SSL_DEBUG_BUF(4, "IV used (transmitted)", |
| 885 | dynamic_iv, |
| 886 | dynamic_iv_is_explicit ? dynamic_iv_len : 0); |
| 887 | MBEDTLS_SSL_DEBUG_BUF(4, "additional data used for AEAD", |
| 888 | add_data, add_data_len); |
| 889 | MBEDTLS_SSL_DEBUG_MSG(3, ("before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", " |
| 890 | "including 0 bytes of padding", |
| 891 | rec->data_len)); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 892 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 893 | /* |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 894 | * Encrypt and authenticate |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 895 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 896 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 897 | if ((ret = mbedtls_cipher_auth_encrypt_ext(&transform->cipher_ctx_enc, |
| 898 | iv, transform->ivlen, |
| 899 | add_data, add_data_len, |
| 900 | data, rec->data_len, /* src */ |
| 901 | data, rec->buf_len - (data - rec->buf), /* dst */ |
| 902 | &rec->data_len, |
| 903 | transform->taglen)) != 0) { |
| 904 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_auth_encrypt", ret); |
| 905 | return ret; |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 906 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 907 | MBEDTLS_SSL_DEBUG_BUF(4, "after encrypt: tag", |
| 908 | data + rec->data_len - transform->taglen, |
| 909 | transform->taglen); |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 910 | /* Account for authentication tag. */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 911 | post_avail -= transform->taglen; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 912 | |
| 913 | /* |
| 914 | * Prefix record content with dynamic IV in case it is explicit. |
| 915 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 916 | if (dynamic_iv_is_explicit != 0) { |
| 917 | if (rec->data_offset < dynamic_iv_len) { |
| 918 | MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough")); |
| 919 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 920 | } |
| 921 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 922 | memcpy(data - dynamic_iv_len, dynamic_iv, dynamic_iv_len); |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 923 | rec->data_offset -= dynamic_iv_len; |
| 924 | rec->data_len += dynamic_iv_len; |
| 925 | } |
| 926 | |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 927 | auth_done++; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 928 | } else |
Hanno Becker | c3f7b0b | 2020-05-28 16:27:16 +0100 | [diff] [blame] | 929 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
Manuel Pégourié-Gonnard | 2df1f1f | 2020-07-09 12:11:39 +0200 | [diff] [blame] | 930 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 931 | if (mode == MBEDTLS_MODE_CBC) { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 932 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 933 | size_t padlen, i; |
| 934 | size_t olen; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 935 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 936 | /* Currently we're always using minimal padding |
| 937 | * (up to 255 bytes would be allowed). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 938 | padlen = transform->ivlen - (rec->data_len + 1) % transform->ivlen; |
| 939 | if (padlen == transform->ivlen) { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 940 | padlen = 0; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 941 | } |
| 942 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 943 | /* Check there's enough space in the buffer for the padding. */ |
| 944 | if (post_avail < padlen + 1) { |
| 945 | MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough")); |
| 946 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 947 | } |
| 948 | |
| 949 | for (i = 0; i <= padlen; i++) { |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 950 | data[rec->data_len + i] = (unsigned char) padlen; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 951 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 952 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 953 | rec->data_len += padlen + 1; |
| 954 | post_avail -= padlen + 1; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 955 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 956 | #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] | 957 | /* |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 958 | * Prepend per-record IV for block cipher in TLS v1.1 and up as per |
| 959 | * Method 1 (6.2.3.2. in RFC4346 and RFC5246) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 960 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 961 | if (transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2) { |
| 962 | if (f_rng == NULL) { |
| 963 | MBEDTLS_SSL_DEBUG_MSG(1, ("No PRNG provided to encrypt_record routine")); |
| 964 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 967 | if (rec->data_offset < transform->ivlen) { |
| 968 | MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough")); |
| 969 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 972 | /* |
| 973 | * Generate IV |
| 974 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 975 | ret = f_rng(p_rng, transform->iv_enc, transform->ivlen); |
| 976 | if (ret != 0) { |
| 977 | return ret; |
| 978 | } |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 979 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 980 | memcpy(data - transform->ivlen, transform->iv_enc, |
| 981 | transform->ivlen); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 982 | |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 983 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 984 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 985 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 986 | MBEDTLS_SSL_DEBUG_MSG(3, ("before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", " |
| 987 | "including %" |
| 988 | MBEDTLS_PRINTF_SIZET |
| 989 | " bytes of IV and %" MBEDTLS_PRINTF_SIZET " bytes of padding", |
| 990 | rec->data_len, transform->ivlen, |
| 991 | padlen + 1)); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 992 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 993 | if ((ret = mbedtls_cipher_crypt(&transform->cipher_ctx_enc, |
| 994 | transform->iv_enc, |
| 995 | transform->ivlen, |
| 996 | data, rec->data_len, |
| 997 | data, &olen)) != 0) { |
| 998 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret); |
| 999 | return ret; |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1000 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1001 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1002 | if (rec->data_len != olen) { |
| 1003 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1004 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1005 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1006 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1007 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1008 | if (transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2) { |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1009 | /* |
| 1010 | * Save IV in SSL3 and TLS1 |
| 1011 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1012 | memcpy(transform->iv_enc, transform->cipher_ctx_enc.iv, |
| 1013 | transform->ivlen); |
| 1014 | } else |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1015 | #endif |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1016 | { |
| 1017 | data -= transform->ivlen; |
| 1018 | rec->data_offset -= transform->ivlen; |
| 1019 | rec->data_len += transform->ivlen; |
| 1020 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1021 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1022 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1023 | if (auth_done == 0) { |
Hanno Becker | 3d8c907 | 2018-01-05 16:24:22 +0000 | [diff] [blame] | 1024 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; |
| 1025 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1026 | /* |
| 1027 | * MAC(MAC_write_key, seq_num + |
| 1028 | * TLSCipherText.type + |
| 1029 | * TLSCipherText.version + |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 1030 | * length_of( (IV +) ENC(...) ) + |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1031 | * IV + // except for TLS 1.0 |
| 1032 | * ENC(content + padding + padding_length)); |
| 1033 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1034 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1035 | if (post_avail < transform->maclen) { |
| 1036 | MBEDTLS_SSL_DEBUG_MSG(1, ("Buffer provided for encrypted record not large enough")); |
| 1037 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1038 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1039 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1040 | ssl_extract_add_data_from_record(add_data, &add_data_len, |
| 1041 | rec, transform->minor_ver); |
Hanno Becker | 1f10d76 | 2019-04-26 13:34:37 +0100 | [diff] [blame] | 1042 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1043 | MBEDTLS_SSL_DEBUG_MSG(3, ("using encrypt then mac")); |
| 1044 | MBEDTLS_SSL_DEBUG_BUF(4, "MAC'd meta-data", add_data, |
| 1045 | add_data_len); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1046 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1047 | ret = mbedtls_md_hmac_update(&transform->md_ctx_enc, add_data, |
| 1048 | add_data_len); |
| 1049 | if (ret != 0) { |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1050 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1051 | } |
| 1052 | ret = mbedtls_md_hmac_update(&transform->md_ctx_enc, |
| 1053 | data, rec->data_len); |
| 1054 | if (ret != 0) { |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1055 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1056 | } |
| 1057 | ret = mbedtls_md_hmac_finish(&transform->md_ctx_enc, mac); |
| 1058 | if (ret != 0) { |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1059 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1060 | } |
| 1061 | ret = mbedtls_md_hmac_reset(&transform->md_ctx_enc); |
| 1062 | if (ret != 0) { |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1063 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1064 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1065 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1066 | memcpy(data + rec->data_len, mac, transform->maclen); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1067 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 1068 | rec->data_len += transform->maclen; |
| 1069 | post_avail -= transform->maclen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1070 | auth_done++; |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1071 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1072 | hmac_failed_etm_enabled: |
| 1073 | mbedtls_platform_zeroize(mac, transform->maclen); |
| 1074 | if (ret != 0) { |
| 1075 | MBEDTLS_SSL_DEBUG_RET(1, "HMAC calculation failed", ret); |
| 1076 | return ret; |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1077 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1078 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1079 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1080 | } else |
Manuel Pégourié-Gonnard | 2df1f1f | 2020-07-09 12:11:39 +0200 | [diff] [blame] | 1081 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */ |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 1082 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1083 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1084 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 1085 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1086 | |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1087 | /* Make extra sure authentication was performed, exactly once */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1088 | if (auth_done != 1) { |
| 1089 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1090 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1091 | } |
| 1092 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1093 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= encrypt buf")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1094 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1095 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1098 | int mbedtls_ssl_decrypt_buf(mbedtls_ssl_context const *ssl, |
| 1099 | mbedtls_ssl_transform *transform, |
| 1100 | mbedtls_record *rec) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1101 | { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1102 | size_t olen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1103 | mbedtls_cipher_mode_t mode; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1104 | int ret, auth_done = 0; |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 1105 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 1106 | size_t padlen = 0, correct = 1; |
| 1107 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1108 | unsigned char *data; |
| 1109 | unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX]; |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1110 | size_t add_data_len; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1111 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1112 | #if !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | a7505d1 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 1113 | ssl = NULL; /* make sure we don't use it except for debug */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1114 | ((void) ssl); |
| 1115 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1116 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1117 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> decrypt buf")); |
| 1118 | if (rec == NULL || |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1119 | rec->buf == NULL || |
| 1120 | rec->buf_len < rec->data_offset || |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1121 | rec->buf_len - rec->data_offset < rec->data_len) { |
| 1122 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad record structure provided to decrypt_buf")); |
| 1123 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1124 | } |
| 1125 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1126 | data = rec->buf + rec->data_offset; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1127 | mode = mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_dec); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1128 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1129 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1130 | /* |
| 1131 | * Match record's CID with incoming CID. |
| 1132 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1133 | if (rec->cid_len != transform->in_cid_len || |
| 1134 | memcmp(rec->cid, transform->in_cid, rec->cid_len) != 0) { |
| 1135 | return MBEDTLS_ERR_SSL_UNEXPECTED_CID; |
Hanno Becker | 938489a | 2019-05-08 13:02:22 +0100 | [diff] [blame] | 1136 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1137 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1138 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1139 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1140 | if (mode == MBEDTLS_MODE_STREAM) { |
Gilles Peskine | 326ba3c | 2023-09-18 14:08:11 +0200 | [diff] [blame] | 1141 | if (rec->data_len < transform->maclen) { |
| 1142 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 1143 | ("Record too short for MAC:" |
| 1144 | " %" MBEDTLS_PRINTF_SIZET " < %" MBEDTLS_PRINTF_SIZET, |
| 1145 | rec->data_len, transform->maclen)); |
| 1146 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
| 1147 | } |
| 1148 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1149 | padlen = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1150 | if ((ret = mbedtls_cipher_crypt(&transform->cipher_ctx_dec, |
| 1151 | transform->iv_dec, |
| 1152 | transform->ivlen, |
| 1153 | data, rec->data_len, |
| 1154 | data, &olen)) != 0) { |
| 1155 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret); |
| 1156 | return ret; |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1157 | } |
| 1158 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1159 | if (rec->data_len != olen) { |
| 1160 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1161 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1162 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1163 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1164 | #endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1165 | #if defined(MBEDTLS_GCM_C) || \ |
| 1166 | defined(MBEDTLS_CCM_C) || \ |
| 1167 | defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1168 | if (mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1169 | mode == MBEDTLS_MODE_CCM || |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1170 | mode == MBEDTLS_MODE_CHACHAPOLY) { |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1171 | unsigned char iv[12]; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1172 | unsigned char *dynamic_iv; |
| 1173 | size_t dynamic_iv_len; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1174 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1175 | /* |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1176 | * Extract dynamic part of nonce for AEAD decryption. |
| 1177 | * |
| 1178 | * Note: In the case of CCM and GCM in TLS 1.2, the dynamic |
| 1179 | * part of the IV is prepended to the ciphertext and |
| 1180 | * can be chosen freely - in particular, it need not |
| 1181 | * agree with the record sequence number. |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1182 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1183 | dynamic_iv_len = sizeof(rec->ctr); |
| 1184 | if (ssl_transform_aead_dynamic_iv_is_explicit(transform) == 1) { |
| 1185 | if (rec->data_len < dynamic_iv_len) { |
| 1186 | MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET |
| 1187 | " ) < explicit_iv_len (%" MBEDTLS_PRINTF_SIZET ") ", |
| 1188 | rec->data_len, |
| 1189 | dynamic_iv_len)); |
| 1190 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1191 | } |
| 1192 | dynamic_iv = data; |
| 1193 | |
| 1194 | data += dynamic_iv_len; |
| 1195 | rec->data_offset += dynamic_iv_len; |
| 1196 | rec->data_len -= dynamic_iv_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1197 | } else { |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 1198 | dynamic_iv = rec->ctr; |
| 1199 | } |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1200 | |
| 1201 | /* Check that there's space for the authentication tag. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1202 | if (rec->data_len < transform->taglen) { |
| 1203 | MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET |
| 1204 | ") < taglen (%" MBEDTLS_PRINTF_SIZET ") ", |
| 1205 | rec->data_len, |
| 1206 | transform->taglen)); |
| 1207 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 1208 | } |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1209 | rec->data_len -= transform->taglen; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1210 | |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1211 | /* |
| 1212 | * Prepare nonce from dynamic and static parts. |
| 1213 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1214 | ssl_build_record_nonce(iv, sizeof(iv), |
| 1215 | transform->iv_dec, |
| 1216 | transform->fixed_ivlen, |
| 1217 | dynamic_iv, |
| 1218 | dynamic_iv_len); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1219 | |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1220 | /* |
| 1221 | * Build additional data for AEAD encryption. |
| 1222 | * This depends on the TLS version. |
| 1223 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1224 | ssl_extract_add_data_from_record(add_data, &add_data_len, rec, |
| 1225 | transform->minor_ver); |
| 1226 | MBEDTLS_SSL_DEBUG_BUF(4, "additional data used for AEAD", |
| 1227 | add_data, add_data_len); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1228 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1229 | /* Because of the check above, we know that there are |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1230 | * explicit_iv_len Bytes preceding data, and taglen |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1231 | * bytes following data + data_len. This justifies |
Hanno Becker | 2001665 | 2019-07-10 11:44:13 +0100 | [diff] [blame] | 1232 | * the debug message and the invocation of |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1233 | * mbedtls_cipher_auth_decrypt() below. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1234 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1235 | MBEDTLS_SSL_DEBUG_BUF(4, "IV used", iv, transform->ivlen); |
| 1236 | MBEDTLS_SSL_DEBUG_BUF(4, "TAG used", data + rec->data_len, |
| 1237 | transform->taglen); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1238 | |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1239 | /* |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1240 | * Decrypt and authenticate |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1241 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1242 | if ((ret = mbedtls_cipher_auth_decrypt_ext(&transform->cipher_ctx_dec, |
| 1243 | iv, transform->ivlen, |
| 1244 | add_data, add_data_len, |
| 1245 | data, rec->data_len + transform->taglen, /* src */ |
| 1246 | data, rec->buf_len - (data - rec->buf), &olen, /* dst */ |
| 1247 | transform->taglen)) != 0) { |
| 1248 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_auth_decrypt", ret); |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1249 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1250 | if (ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED) { |
| 1251 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
| 1252 | } |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1253 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1254 | return ret; |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1255 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1256 | auth_done++; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1257 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1258 | /* Double-check that AEAD decryption doesn't change content length. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1259 | if (olen != rec->data_len) { |
| 1260 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1261 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1262 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1263 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1264 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ |
Manuel Pégourié-Gonnard | 2df1f1f | 2020-07-09 12:11:39 +0200 | [diff] [blame] | 1265 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1266 | if (mode == MBEDTLS_MODE_CBC) { |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 1267 | size_t minlen = 0; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1268 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1269 | /* |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1270 | * Check immediate ciphertext sanity |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1271 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1272 | #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] | 1273 | if (transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2) { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1274 | /* The ciphertext is prefixed with the CBC IV. */ |
| 1275 | minlen += transform->ivlen; |
| 1276 | } |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1277 | #endif |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1278 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1279 | /* Size considerations: |
| 1280 | * |
| 1281 | * - The CBC cipher text must not be empty and hence |
| 1282 | * at least of size transform->ivlen. |
| 1283 | * |
| 1284 | * Together with the potential IV-prefix, this explains |
| 1285 | * the first of the two checks below. |
| 1286 | * |
| 1287 | * - The record must contain a MAC, either in plain or |
| 1288 | * encrypted, depending on whether Encrypt-then-MAC |
| 1289 | * is used or not. |
| 1290 | * - If it is, the message contains the IV-prefix, |
| 1291 | * the CBC ciphertext, and the MAC. |
| 1292 | * - If it is not, the padded plaintext, and hence |
| 1293 | * the CBC ciphertext, has at least length maclen + 1 |
| 1294 | * because there is at least the padding length byte. |
| 1295 | * |
| 1296 | * As the CBC ciphertext is not empty, both cases give the |
| 1297 | * lower bound minlen + maclen + 1 on the record size, which |
| 1298 | * we test for in the second check below. |
| 1299 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1300 | if (rec->data_len < minlen + transform->ivlen || |
| 1301 | rec->data_len < minlen + transform->maclen + 1) { |
| 1302 | MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET |
| 1303 | ") < max( ivlen(%" MBEDTLS_PRINTF_SIZET |
| 1304 | "), maclen (%" MBEDTLS_PRINTF_SIZET ") " |
| 1305 | "+ 1 ) ( + expl IV )", |
| 1306 | rec->data_len, |
| 1307 | transform->ivlen, |
| 1308 | transform->maclen)); |
| 1309 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1310 | } |
| 1311 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1312 | /* |
| 1313 | * Authenticate before decrypt if enabled |
| 1314 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1315 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1316 | if (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED) { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 1317 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD]; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1318 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1319 | MBEDTLS_SSL_DEBUG_MSG(3, ("using encrypt then mac")); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1320 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1321 | /* Update data_len in tandem with add_data. |
| 1322 | * |
| 1323 | * The subtraction is safe because of the previous check |
| 1324 | * data_len >= minlen + maclen + 1. |
| 1325 | * |
| 1326 | * Afterwards, we know that data + data_len is followed by at |
| 1327 | * least maclen Bytes, which justifies the call to |
Gabor Mezei | 18a4494 | 2021-10-20 11:59:27 +0200 | [diff] [blame] | 1328 | * mbedtls_ct_memcmp() below. |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1329 | * |
| 1330 | * Further, we still know that data_len > minlen */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1331 | rec->data_len -= transform->maclen; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1332 | ssl_extract_add_data_from_record(add_data, &add_data_len, rec, |
| 1333 | transform->minor_ver); |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 1334 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1335 | /* Calculate expected MAC. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1336 | MBEDTLS_SSL_DEBUG_BUF(4, "MAC'd meta-data", add_data, |
| 1337 | add_data_len); |
| 1338 | ret = mbedtls_md_hmac_update(&transform->md_ctx_dec, add_data, |
| 1339 | add_data_len); |
| 1340 | if (ret != 0) { |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1341 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1342 | } |
| 1343 | ret = mbedtls_md_hmac_update(&transform->md_ctx_dec, |
| 1344 | data, rec->data_len); |
| 1345 | if (ret != 0) { |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1346 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1347 | } |
| 1348 | ret = mbedtls_md_hmac_finish(&transform->md_ctx_dec, mac_expect); |
| 1349 | if (ret != 0) { |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1350 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1351 | } |
| 1352 | ret = mbedtls_md_hmac_reset(&transform->md_ctx_dec); |
| 1353 | if (ret != 0) { |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1354 | goto hmac_failed_etm_enabled; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1355 | } |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 1356 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1357 | MBEDTLS_SSL_DEBUG_BUF(4, "message mac", data + rec->data_len, |
| 1358 | transform->maclen); |
| 1359 | MBEDTLS_SSL_DEBUG_BUF(4, "expected mac", mac_expect, |
| 1360 | transform->maclen); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1361 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1362 | /* Compare expected MAC with MAC at the end of the record. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1363 | if (mbedtls_ct_memcmp(data + rec->data_len, mac_expect, |
| 1364 | transform->maclen) != 0) { |
| 1365 | MBEDTLS_SSL_DEBUG_MSG(1, ("message mac does not match")); |
Gilles Peskine | d8e2e83 | 2021-12-10 21:33:21 +0100 | [diff] [blame] | 1366 | ret = MBEDTLS_ERR_SSL_INVALID_MAC; |
| 1367 | goto hmac_failed_etm_enabled; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1368 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1369 | auth_done++; |
Gilles Peskine | d8e2e83 | 2021-12-10 21:33:21 +0100 | [diff] [blame] | 1370 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1371 | hmac_failed_etm_enabled: |
| 1372 | mbedtls_platform_zeroize(mac_expect, transform->maclen); |
| 1373 | if (ret != 0) { |
| 1374 | if (ret != MBEDTLS_ERR_SSL_INVALID_MAC) { |
| 1375 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_hmac_xxx", ret); |
| 1376 | } |
| 1377 | return ret; |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1378 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1379 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1380 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1381 | |
| 1382 | /* |
| 1383 | * Check length sanity |
| 1384 | */ |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1385 | |
| 1386 | /* We know from above that data_len > minlen >= 0, |
| 1387 | * so the following check in particular implies that |
| 1388 | * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1389 | if (rec->data_len % transform->ivlen != 0) { |
| 1390 | MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET |
| 1391 | ") %% ivlen (%" MBEDTLS_PRINTF_SIZET ") != 0", |
| 1392 | rec->data_len, transform->ivlen)); |
| 1393 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1394 | } |
| 1395 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1396 | #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] | 1397 | /* |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 1398 | * 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] | 1399 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1400 | if (transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2) { |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1401 | /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1402 | memcpy(transform->iv_dec, data, transform->ivlen); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1403 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1404 | data += transform->ivlen; |
| 1405 | rec->data_offset += transform->ivlen; |
| 1406 | rec->data_len -= transform->ivlen; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1407 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1408 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1409 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1410 | /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */ |
| 1411 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1412 | if ((ret = mbedtls_cipher_crypt(&transform->cipher_ctx_dec, |
| 1413 | transform->iv_dec, transform->ivlen, |
| 1414 | data, rec->data_len, data, &olen)) != 0) { |
| 1415 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret); |
| 1416 | return ret; |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1417 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1418 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1419 | /* Double-check that length hasn't changed during decryption. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1420 | if (rec->data_len != olen) { |
| 1421 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1422 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1423 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1424 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1425 | #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1426 | if (transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2) { |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1427 | /* |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1428 | * Save IV in SSL3 and TLS1, where CBC decryption of consecutive |
| 1429 | * records is equivalent to CBC decryption of the concatenation |
| 1430 | * of the records; in other words, IVs are maintained across |
| 1431 | * record decryptions. |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1432 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1433 | memcpy(transform->iv_dec, transform->cipher_ctx_dec.iv, |
| 1434 | transform->ivlen); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1435 | } |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1436 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1437 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1438 | /* Safe since data_len >= minlen + maclen + 1, so after having |
| 1439 | * subtracted at most minlen and maclen up to this point, |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1440 | * data_len > 0 (because of data_len % ivlen == 0, it's actually |
| 1441 | * >= ivlen ). */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1442 | padlen = data[rec->data_len - 1]; |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1443 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1444 | if (auth_done == 1) { |
Gabor Mezei | 18a4494 | 2021-10-20 11:59:27 +0200 | [diff] [blame] | 1445 | const size_t mask = mbedtls_ct_size_mask_ge( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1446 | rec->data_len, |
| 1447 | padlen + 1); |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 1448 | correct &= mask; |
| 1449 | padlen &= mask; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1450 | } else { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1451 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1452 | if (rec->data_len < transform->maclen + padlen + 1) { |
| 1453 | MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET |
| 1454 | ") < maclen (%" MBEDTLS_PRINTF_SIZET |
| 1455 | ") + padlen (%" MBEDTLS_PRINTF_SIZET ")", |
| 1456 | rec->data_len, |
| 1457 | transform->maclen, |
| 1458 | padlen + 1)); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1459 | } |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 1460 | #endif |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1461 | |
Gabor Mezei | 18a4494 | 2021-10-20 11:59:27 +0200 | [diff] [blame] | 1462 | const size_t mask = mbedtls_ct_size_mask_ge( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1463 | rec->data_len, |
| 1464 | transform->maclen + padlen + 1); |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 1465 | correct &= mask; |
| 1466 | padlen &= mask; |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1467 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1468 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1469 | padlen++; |
| 1470 | |
| 1471 | /* Regardless of the validity of the padding, |
| 1472 | * we have data_len >= padlen here. */ |
| 1473 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1474 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1475 | if (transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) { |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 1476 | /* This is the SSL 3.0 path, we don't have to worry about Lucky |
| 1477 | * 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] | 1478 | * the protocol (known as part of POODLE), so we don't care if the |
| 1479 | * code is not constant-time, in particular branches are OK. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1480 | if (padlen > transform->ivlen) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1481 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1482 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad padding length: is %" MBEDTLS_PRINTF_SIZET ", " |
| 1483 | "should be no more than %" |
| 1484 | MBEDTLS_PRINTF_SIZET, |
| 1485 | padlen, transform->ivlen)); |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 1486 | #endif |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1487 | correct = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1488 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1489 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1490 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 1491 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1492 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1493 | if (transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0) { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1494 | /* The padding check involves a series of up to 256 |
| 1495 | * consecutive memory reads at the end of the record |
| 1496 | * plaintext buffer. In order to hide the length and |
| 1497 | * validity of the padding, always perform exactly |
| 1498 | * `min(256,plaintext_len)` reads (but take into account |
| 1499 | * only the last `padlen` bytes for the padding check). */ |
| 1500 | size_t pad_count = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1501 | volatile unsigned char * const check = data; |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 1502 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1503 | /* Index of first padding byte; it has been ensured above |
| 1504 | * that the subtraction is safe. */ |
| 1505 | size_t const padding_idx = rec->data_len - padlen; |
| 1506 | size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256; |
| 1507 | size_t const start_idx = rec->data_len - num_checks; |
| 1508 | size_t idx; |
Paul Bakker | 956c9e0 | 2013-12-19 14:42:28 +0100 | [diff] [blame] | 1509 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1510 | for (idx = start_idx; idx < rec->data_len; idx++) { |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 1511 | /* pad_count += (idx >= padding_idx) && |
Manuel Pégourié-Gonnard | 6d6f8a4 | 2020-09-25 09:56:53 +0200 | [diff] [blame] | 1512 | * (check[idx] == padlen - 1); |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 1513 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1514 | const size_t mask = mbedtls_ct_size_mask_ge(idx, padding_idx); |
| 1515 | const size_t equal = mbedtls_ct_size_bool_eq(check[idx], |
| 1516 | padlen - 1); |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 1517 | pad_count += mask & equal; |
Paul Bakker | ca9c87e | 2013-09-25 18:52:37 +0200 | [diff] [blame] | 1518 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1519 | correct &= mbedtls_ct_size_bool_eq(pad_count, padlen); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 1520 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1521 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1522 | if (padlen > 0 && correct == 0) { |
| 1523 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad padding byte detected")); |
| 1524 | } |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 1525 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1526 | padlen &= mbedtls_ct_size_mask(correct); |
| 1527 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1528 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 1529 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1530 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1531 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1532 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 1533 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1534 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1535 | /* If the padding was found to be invalid, padlen == 0 |
| 1536 | * and the subtraction is safe. If the padding was found valid, |
| 1537 | * padlen hasn't been changed and the previous assertion |
| 1538 | * data_len >= padlen still holds. */ |
| 1539 | rec->data_len -= padlen; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1540 | } else |
Manuel Pégourié-Gonnard | 2df1f1f | 2020-07-09 12:11:39 +0200 | [diff] [blame] | 1541 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */ |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 1542 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1543 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1544 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 1545 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1546 | |
Manuel Pégourié-Gonnard | 6a25cfa | 2018-07-10 11:15:36 +0200 | [diff] [blame] | 1547 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1548 | MBEDTLS_SSL_DEBUG_BUF(4, "raw buffer after decryption", |
| 1549 | data, rec->data_len); |
Manuel Pégourié-Gonnard | 6a25cfa | 2018-07-10 11:15:36 +0200 | [diff] [blame] | 1550 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1551 | |
| 1552 | /* |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1553 | * Authenticate if not done yet. |
| 1554 | * Compute the MAC regardless of the padding result (RFC4346, CBCTIME). |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1555 | */ |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 1556 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1557 | if (auth_done == 0) { |
Paul Elliott | b830028 | 2022-05-19 18:31:35 +0100 | [diff] [blame] | 1558 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD] = { 0 }; |
| 1559 | unsigned char mac_peer[MBEDTLS_SSL_MAC_ADD] = { 0 }; |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 1560 | |
Gilles Peskine | 326ba3c | 2023-09-18 14:08:11 +0200 | [diff] [blame] | 1561 | /* For CBC+MAC, If the initial value of padlen was such that |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1562 | * data_len < maclen + padlen + 1, then padlen |
| 1563 | * got reset to 1, and the initial check |
| 1564 | * data_len >= minlen + maclen + 1 |
| 1565 | * guarantees that at this point we still |
| 1566 | * have at least data_len >= maclen. |
| 1567 | * |
| 1568 | * If the initial value of padlen was such that |
| 1569 | * data_len >= maclen + padlen + 1, then we have |
| 1570 | * subtracted either padlen + 1 (if the padding was correct) |
| 1571 | * or 0 (if the padding was incorrect) since then, |
| 1572 | * hence data_len >= maclen in any case. |
Gilles Peskine | 326ba3c | 2023-09-18 14:08:11 +0200 | [diff] [blame] | 1573 | * |
| 1574 | * For stream ciphers, we checked above that |
| 1575 | * data_len >= maclen. |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1576 | */ |
| 1577 | rec->data_len -= transform->maclen; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1578 | ssl_extract_add_data_from_record(add_data, &add_data_len, rec, |
| 1579 | transform->minor_ver); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1580 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1581 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1582 | if (transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) { |
| 1583 | ret = ssl_mac(&transform->md_ctx_dec, |
| 1584 | transform->mac_dec, |
| 1585 | data, rec->data_len, |
| 1586 | rec->ctr, rec->type, |
| 1587 | mac_expect); |
| 1588 | if (ret != 0) { |
| 1589 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_mac", ret); |
Gilles Peskine | 2b3f21d | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 1590 | goto hmac_failed_etm_disabled; |
| 1591 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1592 | memcpy(mac_peer, data + rec->data_len, transform->maclen); |
| 1593 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1594 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 1595 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
| 1596 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1597 | if (transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0) { |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1598 | /* |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 1599 | * 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] | 1600 | * data_len over all padlen values. |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 1601 | * |
| 1602 | * They're independent of padlen, since we previously did |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1603 | * data_len -= padlen. |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 1604 | * |
| 1605 | * Note that max_len + maclen is never more than the buffer |
| 1606 | * length, as we previously did in_msglen -= maclen too. |
| 1607 | */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1608 | const size_t max_len = rec->data_len + padlen; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1609 | 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] | 1610 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1611 | ret = mbedtls_ct_hmac(&transform->md_ctx_dec, |
| 1612 | add_data, add_data_len, |
| 1613 | data, rec->data_len, min_len, max_len, |
| 1614 | mac_expect); |
| 1615 | if (ret != 0) { |
| 1616 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ct_hmac", ret); |
Gilles Peskine | d8e2e83 | 2021-12-10 21:33:21 +0100 | [diff] [blame] | 1617 | goto hmac_failed_etm_disabled; |
Gilles Peskine | 20b4408 | 2018-05-29 14:06:49 +0200 | [diff] [blame] | 1618 | } |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 1619 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1620 | mbedtls_ct_memcpy_offset(mac_peer, data, |
| 1621 | rec->data_len, |
| 1622 | min_len, max_len, |
| 1623 | transform->maclen); |
| 1624 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1625 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ |
| 1626 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1627 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1628 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1629 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1630 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1631 | |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 1632 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1633 | MBEDTLS_SSL_DEBUG_BUF(4, "expected mac", mac_expect, transform->maclen); |
| 1634 | 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] | 1635 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1636 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1637 | if (mbedtls_ct_memcmp(mac_peer, mac_expect, |
| 1638 | transform->maclen) != 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1639 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1640 | MBEDTLS_SSL_DEBUG_MSG(1, ("message mac does not match")); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 1641 | #endif |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1642 | correct = 0; |
| 1643 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1644 | auth_done++; |
Gilles Peskine | d8e2e83 | 2021-12-10 21:33:21 +0100 | [diff] [blame] | 1645 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1646 | hmac_failed_etm_disabled: |
| 1647 | mbedtls_platform_zeroize(mac_peer, transform->maclen); |
| 1648 | mbedtls_platform_zeroize(mac_expect, transform->maclen); |
| 1649 | if (ret != 0) { |
| 1650 | return ret; |
| 1651 | } |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1652 | } |
Hanno Becker | dd3ab13 | 2018-10-17 14:43:14 +0100 | [diff] [blame] | 1653 | |
| 1654 | /* |
| 1655 | * Finally check the correct flag |
| 1656 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1657 | if (correct == 0) { |
| 1658 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
| 1659 | } |
Hanno Becker | 52344c2 | 2018-01-03 15:24:20 +0000 | [diff] [blame] | 1660 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1661 | |
| 1662 | /* Make extra sure authentication was performed, exactly once */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1663 | if (auth_done != 1) { |
| 1664 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1665 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1666 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1667 | |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 1668 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1669 | if (transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4) { |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 1670 | /* Remove inner padding and infer true content type. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1671 | ret = ssl_parse_inner_plaintext(data, &rec->data_len, |
| 1672 | &rec->type); |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 1673 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1674 | if (ret != 0) { |
| 1675 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
| 1676 | } |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 1677 | } |
| 1678 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
| 1679 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1680 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1681 | if (rec->cid_len != 0) { |
| 1682 | ret = ssl_parse_inner_plaintext(data, &rec->data_len, |
| 1683 | &rec->type); |
| 1684 | if (ret != 0) { |
| 1685 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
| 1686 | } |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1687 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1688 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1689 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1690 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= decrypt buf")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1691 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1692 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 1695 | #undef MAC_NONE |
| 1696 | #undef MAC_PLAINTEXT |
| 1697 | #undef MAC_CIPHERTEXT |
| 1698 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1699 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1700 | /* |
| 1701 | * Compression/decompression functions |
| 1702 | */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 1703 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1704 | static int ssl_compress_buf(mbedtls_ssl_context *ssl) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1705 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1706 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1707 | unsigned char *msg_post = ssl->out_msg; |
Andrzej Kurek | 5462e02 | 2018-04-20 07:58:53 -0400 | [diff] [blame] | 1708 | ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1709 | size_t len_pre = ssl->out_msglen; |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 1710 | unsigned char *msg_pre = ssl->compress_buf; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1711 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1712 | size_t out_buf_len = ssl->out_buf_len; |
| 1713 | #else |
| 1714 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 1715 | #endif |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1716 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1717 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> compress buf")); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1718 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1719 | if (len_pre == 0) { |
| 1720 | return 0; |
| 1721 | } |
Paul Bakker | abf2f8f | 2013-06-30 14:57:46 +0200 | [diff] [blame] | 1722 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1723 | memcpy(msg_pre, ssl->out_msg, len_pre); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1724 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1725 | MBEDTLS_SSL_DEBUG_MSG(3, ("before compression: msglen = %" MBEDTLS_PRINTF_SIZET ", ", |
| 1726 | ssl->out_msglen)); |
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_BUF(4, "before compression: output payload", |
| 1729 | ssl->out_msg, ssl->out_msglen); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1730 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1731 | ssl->transform_out->ctx_deflate.next_in = msg_pre; |
| 1732 | ssl->transform_out->ctx_deflate.avail_in = len_pre; |
| 1733 | ssl->transform_out->ctx_deflate.next_out = msg_post; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1734 | ssl->transform_out->ctx_deflate.avail_out = out_buf_len - bytes_written; |
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 | ret = deflate(&ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH); |
| 1737 | if (ret != Z_OK) { |
| 1738 | MBEDTLS_SSL_DEBUG_MSG(1, ("failed to perform compression (%d)", ret)); |
| 1739 | return MBEDTLS_ERR_SSL_COMPRESSION_FAILED; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1742 | ssl->out_msglen = out_buf_len - |
Andrzej Kurek | 5462e02 | 2018-04-20 07:58:53 -0400 | [diff] [blame] | 1743 | ssl->transform_out->ctx_deflate.avail_out - bytes_written; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1744 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1745 | MBEDTLS_SSL_DEBUG_MSG(3, ("after compression: msglen = %" MBEDTLS_PRINTF_SIZET ", ", |
| 1746 | ssl->out_msglen)); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1747 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1748 | MBEDTLS_SSL_DEBUG_BUF(4, "after compression: output payload", |
| 1749 | ssl->out_msg, ssl->out_msglen); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1750 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1751 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= compress buf")); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1752 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1753 | return 0; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 1756 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1757 | static int ssl_decompress_buf(mbedtls_ssl_context *ssl) |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1758 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1759 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1760 | unsigned char *msg_post = ssl->in_msg; |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 1761 | ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1762 | size_t len_pre = ssl->in_msglen; |
Paul Bakker | 1677033 | 2013-10-11 09:59:44 +0200 | [diff] [blame] | 1763 | unsigned char *msg_pre = ssl->compress_buf; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1764 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1765 | size_t in_buf_len = ssl->in_buf_len; |
| 1766 | #else |
| 1767 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1768 | #endif |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1769 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1770 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> decompress buf")); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1771 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1772 | if (len_pre == 0) { |
| 1773 | return 0; |
| 1774 | } |
Paul Bakker | abf2f8f | 2013-06-30 14:57:46 +0200 | [diff] [blame] | 1775 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1776 | memcpy(msg_pre, ssl->in_msg, len_pre); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1777 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1778 | MBEDTLS_SSL_DEBUG_MSG(3, ("before decompression: msglen = %" MBEDTLS_PRINTF_SIZET ", ", |
| 1779 | ssl->in_msglen)); |
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_BUF(4, "before decompression: input payload", |
| 1782 | ssl->in_msg, ssl->in_msglen); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1783 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 1784 | ssl->transform_in->ctx_inflate.next_in = msg_pre; |
| 1785 | ssl->transform_in->ctx_inflate.avail_in = len_pre; |
| 1786 | ssl->transform_in->ctx_inflate.next_out = msg_post; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1787 | ssl->transform_in->ctx_inflate.avail_out = in_buf_len - header_bytes; |
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 | ret = inflate(&ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH); |
| 1790 | if (ret != Z_OK) { |
| 1791 | MBEDTLS_SSL_DEBUG_MSG(1, ("failed to perform decompression (%d)", ret)); |
| 1792 | return MBEDTLS_ERR_SSL_COMPRESSION_FAILED; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1795 | ssl->in_msglen = in_buf_len - |
Andrzej Kurek | a9ceef8 | 2018-04-24 06:32:44 -0400 | [diff] [blame] | 1796 | ssl->transform_in->ctx_inflate.avail_out - header_bytes; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1797 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1798 | MBEDTLS_SSL_DEBUG_MSG(3, ("after decompression: msglen = %" MBEDTLS_PRINTF_SIZET ", ", |
| 1799 | ssl->in_msglen)); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1800 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1801 | MBEDTLS_SSL_DEBUG_BUF(4, "after decompression: input payload", |
| 1802 | ssl->in_msg, ssl->in_msglen); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1803 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1804 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= decompress buf")); |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1805 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1806 | return 0; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1807 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1808 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 1809 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1810 | /* |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 1811 | * Fill the input message buffer by appending data to it. |
| 1812 | * 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] | 1813 | * |
| 1814 | * If we return 0, is it guaranteed that (at least) nb_want bytes are |
| 1815 | * available (from this read and/or a previous one). Otherwise, an error code |
| 1816 | * is returned (possibly EOF or WANT_READ). |
| 1817 | * |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 1818 | * With stream transport (TLS) on success ssl->in_left == nb_want, but |
| 1819 | * with datagram transport (DTLS) on success ssl->in_left >= nb_want, |
| 1820 | * since we always read a whole datagram at once. |
| 1821 | * |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 1822 | * 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] | 1823 | * they're done reading a record. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1824 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1825 | 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] | 1826 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1827 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1828 | size_t len; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1829 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1830 | size_t in_buf_len = ssl->in_buf_len; |
| 1831 | #else |
| 1832 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1833 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1834 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1835 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> fetch input")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1836 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1837 | if (ssl->f_recv == NULL && ssl->f_recv_timeout == NULL) { |
| 1838 | MBEDTLS_SSL_DEBUG_MSG(1, ("Bad usage of mbedtls_ssl_set_bio() ")); |
| 1839 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 1840 | } |
| 1841 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1842 | if (nb_want > in_buf_len - (size_t) (ssl->in_hdr - ssl->in_buf)) { |
| 1843 | MBEDTLS_SSL_DEBUG_MSG(1, ("requesting more data than fits")); |
| 1844 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 1845 | } |
| 1846 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1847 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1848 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 1849 | uint32_t timeout; |
| 1850 | |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 1851 | /* |
| 1852 | * The point is, we need to always read a full datagram at once, so we |
| 1853 | * sometimes read more then requested, and handle the additional data. |
| 1854 | * It could be the rest of the current record (while fetching the |
| 1855 | * header) and/or some other records in the same datagram. |
| 1856 | */ |
| 1857 | |
| 1858 | /* |
| 1859 | * Move to the next record in the already read datagram if applicable |
| 1860 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1861 | if (ssl->next_record_offset != 0) { |
| 1862 | if (ssl->in_left < ssl->next_record_offset) { |
| 1863 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1864 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 1865 | } |
| 1866 | |
| 1867 | ssl->in_left -= ssl->next_record_offset; |
| 1868 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1869 | if (ssl->in_left != 0) { |
| 1870 | MBEDTLS_SSL_DEBUG_MSG(2, ("next record in same datagram, offset: %" |
| 1871 | MBEDTLS_PRINTF_SIZET, |
| 1872 | ssl->next_record_offset)); |
| 1873 | memmove(ssl->in_hdr, |
| 1874 | ssl->in_hdr + ssl->next_record_offset, |
| 1875 | ssl->in_left); |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 1876 | } |
| 1877 | |
| 1878 | ssl->next_record_offset = 0; |
| 1879 | } |
| 1880 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1881 | MBEDTLS_SSL_DEBUG_MSG(2, ("in_left: %" MBEDTLS_PRINTF_SIZET |
| 1882 | ", nb_want: %" MBEDTLS_PRINTF_SIZET, |
| 1883 | ssl->in_left, nb_want)); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1884 | |
| 1885 | /* |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 1886 | * Done if we already have enough data. |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1887 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1888 | if (nb_want <= ssl->in_left) { |
| 1889 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= fetch input")); |
| 1890 | return 0; |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 1891 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1892 | |
| 1893 | /* |
Antonin Décimo | 36e89b5 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 1894 | * 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] | 1895 | * are not at the beginning of a new record, the caller did something |
| 1896 | * wrong. |
| 1897 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1898 | if (ssl->in_left != 0) { |
| 1899 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1900 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1901 | } |
| 1902 | |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1903 | /* |
| 1904 | * Don't even try to read if time's out already. |
| 1905 | * This avoids by-passing the timer when repeatedly receiving messages |
| 1906 | * that will end up being dropped. |
| 1907 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1908 | if (mbedtls_ssl_check_timer(ssl) != 0) { |
| 1909 | MBEDTLS_SSL_DEBUG_MSG(2, ("timer has expired")); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 1910 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1911 | } else { |
| 1912 | len = in_buf_len - (ssl->in_hdr - ssl->in_buf); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1913 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1914 | if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1915 | timeout = ssl->handshake->retransmit_timeout; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1916 | } else { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1917 | timeout = ssl->conf->read_timeout; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1918 | } |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1919 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1920 | 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] | 1921 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1922 | if (ssl->f_recv_timeout != NULL) { |
| 1923 | ret = ssl->f_recv_timeout(ssl->p_bio, ssl->in_hdr, len, |
| 1924 | timeout); |
| 1925 | } else { |
| 1926 | ret = ssl->f_recv(ssl->p_bio, ssl->in_hdr, len); |
| 1927 | } |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1928 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1929 | MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_recv(_timeout)", ret); |
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 | if (ret == 0) { |
| 1932 | return MBEDTLS_ERR_SSL_CONN_EOF; |
| 1933 | } |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1934 | } |
| 1935 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1936 | if (ret == MBEDTLS_ERR_SSL_TIMEOUT) { |
| 1937 | MBEDTLS_SSL_DEBUG_MSG(2, ("timeout")); |
| 1938 | mbedtls_ssl_set_timer(ssl, 0); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 1939 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1940 | if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 1941 | if (ssl_double_retransmit_timeout(ssl) != 0) { |
| 1942 | MBEDTLS_SSL_DEBUG_MSG(1, ("handshake timeout")); |
| 1943 | return MBEDTLS_ERR_SSL_TIMEOUT; |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 1944 | } |
| 1945 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1946 | if ((ret = mbedtls_ssl_resend(ssl)) != 0) { |
| 1947 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend", ret); |
| 1948 | return ret; |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 1949 | } |
| 1950 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1951 | return MBEDTLS_ERR_SSL_WANT_READ; |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 1952 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1953 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1954 | else if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 1955 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) { |
| 1956 | if ((ret = mbedtls_ssl_resend_hello_request(ssl)) != 0) { |
| 1957 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend_hello_request", |
| 1958 | ret); |
| 1959 | return ret; |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +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 | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 1963 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1964 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 1965 | } |
| 1966 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1967 | if (ret < 0) { |
| 1968 | return ret; |
| 1969 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1970 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1971 | ssl->in_left = ret; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1972 | } else |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1973 | #endif |
| 1974 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1975 | MBEDTLS_SSL_DEBUG_MSG(2, ("in_left: %" MBEDTLS_PRINTF_SIZET |
| 1976 | ", nb_want: %" MBEDTLS_PRINTF_SIZET, |
| 1977 | ssl->in_left, nb_want)); |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 1978 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1979 | while (ssl->in_left < nb_want) { |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1980 | len = nb_want - ssl->in_left; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1981 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1982 | if (mbedtls_ssl_check_timer(ssl) != 0) { |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1983 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1984 | } else { |
| 1985 | if (ssl->f_recv_timeout != NULL) { |
| 1986 | ret = ssl->f_recv_timeout(ssl->p_bio, |
| 1987 | ssl->in_hdr + ssl->in_left, len, |
| 1988 | ssl->conf->read_timeout); |
| 1989 | } else { |
| 1990 | ret = ssl->f_recv(ssl->p_bio, |
| 1991 | ssl->in_hdr + ssl->in_left, len); |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 1992 | } |
| 1993 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1994 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1995 | MBEDTLS_SSL_DEBUG_MSG(2, ("in_left: %" MBEDTLS_PRINTF_SIZET |
| 1996 | ", nb_want: %" MBEDTLS_PRINTF_SIZET, |
| 1997 | ssl->in_left, nb_want)); |
| 1998 | MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_recv(_timeout)", ret); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1999 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2000 | if (ret == 0) { |
| 2001 | return MBEDTLS_ERR_SSL_CONN_EOF; |
| 2002 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2003 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2004 | if (ret < 0) { |
| 2005 | return ret; |
| 2006 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2007 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2008 | if ((size_t) ret > len || (INT_MAX > SIZE_MAX && ret > (int) SIZE_MAX)) { |
| 2009 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 2010 | ("f_recv returned %d bytes but only %" MBEDTLS_PRINTF_SIZET |
| 2011 | " were requested", |
| 2012 | ret, len)); |
| 2013 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
mohammad1603 | 5bd15cb | 2018-02-28 04:30:59 -0800 | [diff] [blame] | 2014 | } |
| 2015 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 2016 | ssl->in_left += ret; |
| 2017 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2018 | } |
| 2019 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2020 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= fetch input")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2021 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2022 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | /* |
| 2026 | * Flush any data not yet written |
| 2027 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2028 | int mbedtls_ssl_flush_output(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2029 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2030 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 2031 | unsigned char *buf; |
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 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> flush output")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2034 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2035 | if (ssl->f_send == NULL) { |
| 2036 | MBEDTLS_SSL_DEBUG_MSG(1, ("Bad usage of mbedtls_ssl_set_bio() ")); |
| 2037 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 2038 | } |
| 2039 | |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 2040 | /* Avoid incrementing counter if data is flushed */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2041 | if (ssl->out_left == 0) { |
| 2042 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= flush output")); |
| 2043 | return 0; |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 2044 | } |
| 2045 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2046 | while (ssl->out_left > 0) { |
| 2047 | MBEDTLS_SSL_DEBUG_MSG(2, ("message length: %" MBEDTLS_PRINTF_SIZET |
| 2048 | ", out_left: %" MBEDTLS_PRINTF_SIZET, |
| 2049 | mbedtls_ssl_out_hdr_len(ssl) + ssl->out_msglen, ssl->out_left)); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2050 | |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2051 | buf = ssl->out_hdr - ssl->out_left; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2052 | ret = ssl->f_send(ssl->p_bio, buf, ssl->out_left); |
Paul Bakker | 186751d | 2012-05-08 13:16:14 +0000 | [diff] [blame] | 2053 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2054 | MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_send", ret); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2055 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2056 | if (ret <= 0) { |
| 2057 | return ret; |
| 2058 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2059 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2060 | if ((size_t) ret > ssl->out_left || (INT_MAX > SIZE_MAX && ret > (int) SIZE_MAX)) { |
| 2061 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 2062 | ("f_send returned %d bytes but only %" MBEDTLS_PRINTF_SIZET |
| 2063 | " bytes were sent", |
| 2064 | ret, ssl->out_left)); |
| 2065 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
mohammad1603 | 4bbaeb4 | 2018-02-22 04:29:04 -0800 | [diff] [blame] | 2066 | } |
| 2067 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2068 | ssl->out_left -= ret; |
| 2069 | } |
| 2070 | |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2071 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2072 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2073 | ssl->out_hdr = ssl->out_buf; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2074 | } else |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2075 | #endif |
| 2076 | { |
| 2077 | ssl->out_hdr = ssl->out_buf + 8; |
| 2078 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2079 | mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out); |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 2080 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2081 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= flush output")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2082 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2083 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2084 | } |
| 2085 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2086 | /* |
| 2087 | * Functions to handle the DTLS retransmission state machine |
| 2088 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2089 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2090 | /* |
| 2091 | * Append current handshake message to current outgoing flight |
| 2092 | */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 2093 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2094 | static int ssl_flight_append(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2095 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2096 | mbedtls_ssl_flight_item *msg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2097 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_flight_append")); |
| 2098 | MBEDTLS_SSL_DEBUG_BUF(4, "message appended to flight", |
| 2099 | ssl->out_msg, ssl->out_msglen); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2100 | |
| 2101 | /* Allocate space for current message */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2102 | if ((msg = mbedtls_calloc(1, sizeof(mbedtls_ssl_flight_item))) == NULL) { |
| 2103 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc %" MBEDTLS_PRINTF_SIZET " bytes failed", |
| 2104 | sizeof(mbedtls_ssl_flight_item))); |
| 2105 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2106 | } |
| 2107 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2108 | if ((msg->p = mbedtls_calloc(1, ssl->out_msglen)) == NULL) { |
| 2109 | MBEDTLS_SSL_DEBUG_MSG(1, ("alloc %" MBEDTLS_PRINTF_SIZET " bytes failed", |
| 2110 | ssl->out_msglen)); |
| 2111 | mbedtls_free(msg); |
| 2112 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2113 | } |
| 2114 | |
| 2115 | /* Copy current handshake message with headers */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2116 | memcpy(msg->p, ssl->out_msg, ssl->out_msglen); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2117 | msg->len = ssl->out_msglen; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2118 | msg->type = ssl->out_msgtype; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2119 | msg->next = NULL; |
| 2120 | |
| 2121 | /* Append to the current flight */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2122 | if (ssl->handshake->flight == NULL) { |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2123 | ssl->handshake->flight = msg; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2124 | } else { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2125 | mbedtls_ssl_flight_item *cur = ssl->handshake->flight; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2126 | while (cur->next != NULL) { |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2127 | cur = cur->next; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2128 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2129 | cur->next = msg; |
| 2130 | } |
| 2131 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2132 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_flight_append")); |
| 2133 | return 0; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2134 | } |
| 2135 | |
| 2136 | /* |
| 2137 | * Free the current flight of handshake messages |
| 2138 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2139 | void mbedtls_ssl_flight_free(mbedtls_ssl_flight_item *flight) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2140 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2141 | mbedtls_ssl_flight_item *cur = flight; |
| 2142 | mbedtls_ssl_flight_item *next; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2143 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2144 | while (cur != NULL) { |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2145 | next = cur->next; |
| 2146 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2147 | mbedtls_free(cur->p); |
| 2148 | mbedtls_free(cur); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2149 | |
| 2150 | cur = next; |
| 2151 | } |
| 2152 | } |
| 2153 | |
| 2154 | /* |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2155 | * Swap transform_out and out_ctr with the alternative ones |
| 2156 | */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 2157 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2158 | static int ssl_swap_epochs(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2159 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2160 | mbedtls_ssl_transform *tmp_transform; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2161 | unsigned char tmp_out_ctr[8]; |
| 2162 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2163 | if (ssl->transform_out == ssl->handshake->alt_transform_out) { |
| 2164 | MBEDTLS_SSL_DEBUG_MSG(3, ("skip swap epochs")); |
| 2165 | return 0; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2166 | } |
| 2167 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2168 | MBEDTLS_SSL_DEBUG_MSG(3, ("swap epochs")); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2169 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2170 | /* Swap transforms */ |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2171 | tmp_transform = ssl->transform_out; |
| 2172 | ssl->transform_out = ssl->handshake->alt_transform_out; |
| 2173 | ssl->handshake->alt_transform_out = tmp_transform; |
| 2174 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2175 | /* Swap epoch + sequence_number */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2176 | memcpy(tmp_out_ctr, ssl->cur_out_ctr, 8); |
| 2177 | memcpy(ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8); |
| 2178 | memcpy(ssl->handshake->alt_out_ctr, tmp_out_ctr, 8); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2179 | |
| 2180 | /* Adjust to the newly activated transform */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2181 | mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2182 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2183 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2184 | if (mbedtls_ssl_hw_record_activate != NULL) { |
| 2185 | int ret = mbedtls_ssl_hw_record_activate(ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND); |
| 2186 | if (ret != 0) { |
| 2187 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_activate", ret); |
| 2188 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2189 | } |
| 2190 | } |
| 2191 | #endif |
Manuel Pégourié-Gonnard | e07bc20 | 2020-02-26 09:53:42 +0100 | [diff] [blame] | 2192 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2193 | return 0; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2194 | } |
| 2195 | |
| 2196 | /* |
| 2197 | * Retransmit the current flight of messages. |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2198 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2199 | int mbedtls_ssl_resend(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2200 | { |
| 2201 | int ret = 0; |
| 2202 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2203 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_resend")); |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2204 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2205 | ret = mbedtls_ssl_flight_transmit(ssl); |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2206 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2207 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_resend")); |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2208 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2209 | return ret; |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | /* |
| 2213 | * Transmit or retransmit the current flight of messages. |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2214 | * |
| 2215 | * Need to remember the current message in case flush_output returns |
| 2216 | * 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] | 2217 | * 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] | 2218 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2219 | int mbedtls_ssl_flight_transmit(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2220 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2221 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2222 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_flight_transmit")); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2223 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2224 | if (ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING) { |
| 2225 | MBEDTLS_SSL_DEBUG_MSG(2, ("initialise flight transmission")); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2226 | |
| 2227 | ssl->handshake->cur_msg = ssl->handshake->flight; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2228 | ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2229 | ret = ssl_swap_epochs(ssl); |
| 2230 | if (ret != 0) { |
| 2231 | return ret; |
| 2232 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2233 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2234 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2235 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2236 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2237 | while (ssl->handshake->cur_msg != NULL) { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2238 | size_t max_frag_len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2239 | const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2240 | |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 2241 | int const is_finished = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2242 | (cur->type == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2243 | cur->p[0] == MBEDTLS_SSL_HS_FINISHED); |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 2244 | |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 2245 | uint8_t const force_flush = ssl->disable_datagram_packing == 1 ? |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2246 | SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH; |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 2247 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2248 | /* Swap epochs before sending Finished: we can't do it after |
| 2249 | * sending ChangeCipherSpec, in case write returns WANT_READ. |
| 2250 | * Must be done before copying, may change out_msg pointer */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2251 | if (is_finished && ssl->handshake->cur_msg_p == (cur->p + 12)) { |
| 2252 | MBEDTLS_SSL_DEBUG_MSG(2, ("swap epochs to send finished message")); |
| 2253 | ret = ssl_swap_epochs(ssl); |
| 2254 | if (ret != 0) { |
| 2255 | return ret; |
| 2256 | } |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2257 | } |
| 2258 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2259 | ret = ssl_get_remaining_payload_in_datagram(ssl); |
| 2260 | if (ret < 0) { |
| 2261 | return ret; |
| 2262 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2263 | max_frag_len = (size_t) ret; |
| 2264 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2265 | /* CCS is copied as is, while HS messages may need fragmentation */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2266 | if (cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) { |
| 2267 | if (max_frag_len == 0) { |
| 2268 | if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) { |
| 2269 | return ret; |
| 2270 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2271 | |
| 2272 | continue; |
| 2273 | } |
| 2274 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2275 | memcpy(ssl->out_msg, cur->p, cur->len); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2276 | ssl->out_msglen = cur->len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2277 | ssl->out_msgtype = cur->type; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2278 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2279 | /* Update position inside current message */ |
| 2280 | ssl->handshake->cur_msg_p += cur->len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2281 | } else { |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2282 | const unsigned char * const p = ssl->handshake->cur_msg_p; |
| 2283 | const size_t hs_len = cur->len - 12; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2284 | const size_t frag_off = p - (cur->p + 12); |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2285 | const size_t rem_len = hs_len - frag_off; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2286 | size_t cur_hs_frag_len, max_hs_frag_len; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2287 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2288 | if ((max_frag_len < 12) || (max_frag_len == 12 && hs_len != 0)) { |
| 2289 | if (is_finished) { |
| 2290 | ret = ssl_swap_epochs(ssl); |
| 2291 | if (ret != 0) { |
| 2292 | return ret; |
| 2293 | } |
Manuel Pégourié-Gonnard | e07bc20 | 2020-02-26 09:53:42 +0100 | [diff] [blame] | 2294 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2295 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2296 | if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) { |
| 2297 | return ret; |
| 2298 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2299 | |
| 2300 | continue; |
| 2301 | } |
| 2302 | max_hs_frag_len = max_frag_len - 12; |
| 2303 | |
| 2304 | cur_hs_frag_len = rem_len > max_hs_frag_len ? |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2305 | max_hs_frag_len : rem_len; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2306 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2307 | if (frag_off == 0 && cur_hs_frag_len != hs_len) { |
| 2308 | MBEDTLS_SSL_DEBUG_MSG(2, ("fragmenting handshake message (%u > %u)", |
| 2309 | (unsigned) cur_hs_frag_len, |
| 2310 | (unsigned) max_hs_frag_len)); |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 2311 | } |
Manuel Pégourié-Gonnard | b747c6c | 2018-08-12 13:28:53 +0200 | [diff] [blame] | 2312 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2313 | /* Messages are stored with handshake headers as if not fragmented, |
| 2314 | * copy beginning of headers then fill fragmentation fields. |
| 2315 | * 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] | 2316 | memcpy(ssl->out_msg, cur->p, 6); |
Joe Subbiani | 61f7d73 | 2021-06-24 09:06:23 +0100 | [diff] [blame] | 2317 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2318 | ssl->out_msg[6] = MBEDTLS_BYTE_2(frag_off); |
| 2319 | ssl->out_msg[7] = MBEDTLS_BYTE_1(frag_off); |
| 2320 | ssl->out_msg[8] = MBEDTLS_BYTE_0(frag_off); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2321 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2322 | ssl->out_msg[9] = MBEDTLS_BYTE_2(cur_hs_frag_len); |
| 2323 | ssl->out_msg[10] = MBEDTLS_BYTE_1(cur_hs_frag_len); |
| 2324 | 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] | 2325 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2326 | 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] | 2327 | |
Hanno Becker | 3f7b973 | 2018-08-28 09:53:25 +0100 | [diff] [blame] | 2328 | /* Copy the handshake message content and set records fields */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2329 | memcpy(ssl->out_msg + 12, p, cur_hs_frag_len); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2330 | ssl->out_msglen = cur_hs_frag_len + 12; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2331 | ssl->out_msgtype = cur->type; |
| 2332 | |
| 2333 | /* Update position inside current message */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2334 | ssl->handshake->cur_msg_p += cur_hs_frag_len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2335 | } |
| 2336 | |
| 2337 | /* 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] | 2338 | if (ssl->handshake->cur_msg_p >= cur->p + cur->len) { |
| 2339 | if (cur->next != NULL) { |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2340 | ssl->handshake->cur_msg = cur->next; |
| 2341 | ssl->handshake->cur_msg_p = cur->next->p + 12; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2342 | } else { |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2343 | ssl->handshake->cur_msg = NULL; |
| 2344 | ssl->handshake->cur_msg_p = NULL; |
| 2345 | } |
| 2346 | } |
| 2347 | |
| 2348 | /* Actually send the message out */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2349 | if ((ret = mbedtls_ssl_write_record(ssl, force_flush)) != 0) { |
| 2350 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret); |
| 2351 | return ret; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2352 | } |
| 2353 | } |
| 2354 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2355 | if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) { |
| 2356 | return ret; |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2357 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2358 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2359 | /* Update state and set timer */ |
| 2360 | if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 2361 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
| 2362 | } else { |
| 2363 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
| 2364 | mbedtls_ssl_set_timer(ssl, ssl->handshake->retransmit_timeout); |
| 2365 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2366 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2367 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_flight_transmit")); |
| 2368 | |
| 2369 | return 0; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2370 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2371 | |
| 2372 | /* |
| 2373 | * To be called when the last message of an incoming flight is received. |
| 2374 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2375 | void mbedtls_ssl_recv_flight_completed(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2376 | { |
| 2377 | /* We won't need to resend that one any more */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2378 | mbedtls_ssl_flight_free(ssl->handshake->flight); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2379 | ssl->handshake->flight = NULL; |
| 2380 | ssl->handshake->cur_msg = NULL; |
| 2381 | |
| 2382 | /* The next incoming flight will start with this msg_seq */ |
| 2383 | ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq; |
| 2384 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 2385 | /* We don't want to remember CCS's across flight boundaries. */ |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 2386 | ssl->handshake->buffering.seen_ccs = 0; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 2387 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 2388 | /* Clear future message buffering structure. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2389 | mbedtls_ssl_buffering_free(ssl); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 2390 | |
Manuel Pégourié-Gonnard | 6c1fa3a | 2014-10-01 16:58:16 +0200 | [diff] [blame] | 2391 | /* Cancel timer */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2392 | mbedtls_ssl_set_timer(ssl, 0); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2393 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2394 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2395 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2396 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2397 | } else { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2398 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2399 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2400 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2401 | |
| 2402 | /* |
| 2403 | * To be called when the last message of an outgoing flight is send. |
| 2404 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2405 | void mbedtls_ssl_send_flight_completed(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2406 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2407 | ssl_reset_retransmit_timeout(ssl); |
| 2408 | mbedtls_ssl_set_timer(ssl, ssl->handshake->retransmit_timeout); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2409 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2410 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2411 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2412 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2413 | } else { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2414 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2415 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2416 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2417 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2418 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2419 | /* |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2420 | * Handshake layer functions |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2421 | */ |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2422 | |
| 2423 | /* |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2424 | * Write (DTLS: or queue) current handshake (including CCS) message. |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2425 | * |
| 2426 | * - fill in handshake headers |
| 2427 | * - update handshake checksum |
| 2428 | * - DTLS: save message for resending |
| 2429 | * - then pass to the record layer |
| 2430 | * |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2431 | * DTLS: except for HelloRequest, messages are only queued, and will only be |
| 2432 | * actually sent when calling flight_transmit() or resend(). |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2433 | * |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2434 | * Inputs: |
| 2435 | * - ssl->out_msglen: 4 + actual handshake message len |
| 2436 | * (4 is the size of handshake headers for TLS) |
| 2437 | * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc) |
| 2438 | * - ssl->out_msg + 4: the handshake message body |
| 2439 | * |
Manuel Pégourié-Gonnard | 065a2a3 | 2018-08-20 11:09:26 +0200 | [diff] [blame] | 2440 | * 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] | 2441 | * - ssl->out_msglen: the length of the record contents |
| 2442 | * (including handshake headers but excluding record headers) |
| 2443 | * - ssl->out_msg: the record contents (handshake headers + content) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2444 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2445 | int mbedtls_ssl_write_handshake_msg(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2446 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2447 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2448 | const size_t hs_len = ssl->out_msglen - 4; |
| 2449 | const unsigned char hs_type = ssl->out_msg[0]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2450 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2451 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write handshake message")); |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2452 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2453 | /* |
| 2454 | * Sanity checks |
| 2455 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2456 | if (ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2457 | ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) { |
Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 2458 | /* In SSLv3, the client might send a NoCertificate alert. */ |
| 2459 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2460 | if (!(ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 && |
| 2461 | ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT && |
| 2462 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT)) |
Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 2463 | #endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */ |
| 2464 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2465 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 2466 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 2467 | } |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2468 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2469 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 2470 | /* Whenever we send anything different from a |
| 2471 | * HelloRequest we should be in a handshake - double check. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2472 | if (!(ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2473 | hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST) && |
| 2474 | ssl->handshake == NULL) { |
| 2475 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 2476 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2477 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2478 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2479 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2480 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2481 | ssl->handshake != NULL && |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2482 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) { |
| 2483 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 2484 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2485 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2486 | #endif |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2487 | |
Hanno Becker | b50a253 | 2018-08-06 11:52:54 +0100 | [diff] [blame] | 2488 | /* Double-check that we did not exceed the bounds |
| 2489 | * of the outgoing record buffer. |
| 2490 | * This should never fail as the various message |
| 2491 | * writing functions must obey the bounds of the |
| 2492 | * outgoing record buffer, but better be safe. |
| 2493 | * |
| 2494 | * Note: We deliberately do not check for the MTU or MFL here. |
| 2495 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2496 | if (ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN) { |
| 2497 | MBEDTLS_SSL_DEBUG_MSG(1, ("Record too large: " |
| 2498 | "size %" MBEDTLS_PRINTF_SIZET |
| 2499 | ", maximum %" MBEDTLS_PRINTF_SIZET, |
| 2500 | ssl->out_msglen, |
| 2501 | (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN)); |
| 2502 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | b50a253 | 2018-08-06 11:52:54 +0100 | [diff] [blame] | 2503 | } |
| 2504 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2505 | /* |
| 2506 | * Fill handshake headers |
| 2507 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2508 | if (ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 2509 | ssl->out_msg[1] = MBEDTLS_BYTE_2(hs_len); |
| 2510 | ssl->out_msg[2] = MBEDTLS_BYTE_1(hs_len); |
| 2511 | ssl->out_msg[3] = MBEDTLS_BYTE_0(hs_len); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2512 | |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 2513 | /* |
| 2514 | * DTLS has additional fields in the Handshake layer, |
| 2515 | * between the length field and the actual payload: |
| 2516 | * uint16 message_seq; |
| 2517 | * uint24 fragment_offset; |
| 2518 | * uint24 fragment_length; |
| 2519 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2520 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2521 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 2522 | /* Make room for the additional DTLS fields */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2523 | if (MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8) { |
| 2524 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS handshake message too large: " |
| 2525 | "size %" MBEDTLS_PRINTF_SIZET ", maximum %" |
| 2526 | MBEDTLS_PRINTF_SIZET, |
| 2527 | hs_len, |
| 2528 | (size_t) (MBEDTLS_SSL_OUT_CONTENT_LEN - 12))); |
| 2529 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | 9648f8b | 2017-09-18 10:55:54 +0100 | [diff] [blame] | 2530 | } |
| 2531 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2532 | 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] | 2533 | ssl->out_msglen += 8; |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 2534 | |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 2535 | /* Write message_seq and update it, except for HelloRequest */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2536 | if (hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST) { |
| 2537 | MBEDTLS_PUT_UINT16_BE(ssl->handshake->out_msg_seq, ssl->out_msg, 4); |
| 2538 | ++(ssl->handshake->out_msg_seq); |
| 2539 | } else { |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 2540 | ssl->out_msg[4] = 0; |
| 2541 | ssl->out_msg[5] = 0; |
| 2542 | } |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 2543 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2544 | /* Handshake hashes are computed without fragmentation, |
| 2545 | * so set frag_offset = 0 and frag_len = hs_len for now */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2546 | memset(ssl->out_msg + 6, 0x00, 3); |
| 2547 | memcpy(ssl->out_msg + 9, ssl->out_msg + 1, 3); |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 2548 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2549 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 2550 | |
Hanno Becker | 0207e53 | 2018-08-28 10:28:28 +0100 | [diff] [blame] | 2551 | /* Update running hashes of handshake messages seen */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2552 | if (hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST) { |
| 2553 | ssl->handshake->update_checksum(ssl, ssl->out_msg, ssl->out_msglen); |
| 2554 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2555 | } |
| 2556 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2557 | /* 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] | 2558 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2559 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 2560 | !(ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2561 | hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST)) { |
| 2562 | if ((ret = ssl_flight_append(ssl)) != 0) { |
| 2563 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_flight_append", ret); |
| 2564 | return ret; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2565 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2566 | } else |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2567 | #endif |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2568 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2569 | if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) { |
| 2570 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_record", ret); |
| 2571 | return ret; |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2572 | } |
| 2573 | } |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2574 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2575 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write handshake message")); |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2576 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2577 | return 0; |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2578 | } |
| 2579 | |
| 2580 | /* |
| 2581 | * Record layer functions |
| 2582 | */ |
| 2583 | |
| 2584 | /* |
| 2585 | * Write current record. |
| 2586 | * |
| 2587 | * Uses: |
| 2588 | * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS) |
| 2589 | * - ssl->out_msglen: length of the record content (excl headers) |
| 2590 | * - ssl->out_msg: record content |
| 2591 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2592 | 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] | 2593 | { |
| 2594 | int ret, done = 0; |
| 2595 | size_t len = ssl->out_msglen; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2596 | uint8_t flush = force_flush; |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2597 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2598 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write record")); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2599 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2600 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2601 | if (ssl->transform_out != NULL && |
| 2602 | ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE) { |
| 2603 | if ((ret = ssl_compress_buf(ssl)) != 0) { |
| 2604 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_compress_buf", ret); |
| 2605 | return ret; |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2606 | } |
| 2607 | |
| 2608 | len = ssl->out_msglen; |
| 2609 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2610 | #endif /*MBEDTLS_ZLIB_SUPPORT */ |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 2611 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2612 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2613 | if (mbedtls_ssl_hw_record_write != NULL) { |
| 2614 | MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_write()")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2615 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2616 | ret = mbedtls_ssl_hw_record_write(ssl); |
| 2617 | if (ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH) { |
| 2618 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_write", ret); |
| 2619 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2620 | } |
Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 2621 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2622 | if (ret == 0) { |
Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 2623 | done = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2624 | } |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2625 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2626 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2627 | if (!done) { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2628 | unsigned i; |
| 2629 | size_t protected_record_size; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 2630 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 2631 | size_t out_buf_len = ssl->out_buf_len; |
| 2632 | #else |
| 2633 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 2634 | #endif |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 2635 | /* Skip writing the record content type to after the encryption, |
| 2636 | * as it may change when using the CID extension. */ |
| 2637 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2638 | mbedtls_ssl_write_version(ssl->major_ver, ssl->minor_ver, |
| 2639 | ssl->conf->transport, ssl->out_hdr + 1); |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 2640 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2641 | memcpy(ssl->out_ctr, ssl->cur_out_ctr, 8); |
| 2642 | MBEDTLS_PUT_UINT16_BE(len, ssl->out_len, 0); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2643 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2644 | if (ssl->transform_out != NULL) { |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2645 | mbedtls_record rec; |
| 2646 | |
| 2647 | rec.buf = ssl->out_iv; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2648 | rec.buf_len = out_buf_len - (ssl->out_iv - ssl->out_buf); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2649 | rec.data_len = ssl->out_msglen; |
| 2650 | rec.data_offset = ssl->out_msg - rec.buf; |
| 2651 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2652 | memcpy(&rec.ctr[0], ssl->out_ctr, 8); |
| 2653 | mbedtls_ssl_write_version(ssl->major_ver, ssl->minor_ver, |
| 2654 | ssl->conf->transport, rec.ver); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2655 | rec.type = ssl->out_msgtype; |
| 2656 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2657 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 2658 | /* The CID is set by mbedtls_ssl_encrypt_buf(). */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2659 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2660 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2661 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2662 | if ((ret = mbedtls_ssl_encrypt_buf(ssl, ssl->transform_out, &rec, |
| 2663 | ssl->conf->f_rng, ssl->conf->p_rng)) != 0) { |
| 2664 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_encrypt_buf", ret); |
| 2665 | return ret; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2666 | } |
| 2667 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2668 | if (rec.data_offset != 0) { |
| 2669 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 2670 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2671 | } |
| 2672 | |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 2673 | /* Update the record content type and CID. */ |
| 2674 | ssl->out_msgtype = rec.type; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2675 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 2676 | memcpy(ssl->out_cid, rec.cid, rec.cid_len); |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2677 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 78f839d | 2019-03-14 12:56:23 +0000 | [diff] [blame] | 2678 | ssl->out_msglen = len = rec.data_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2679 | MBEDTLS_PUT_UINT16_BE(rec.data_len, ssl->out_len, 0); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2680 | } |
| 2681 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2682 | protected_record_size = len + mbedtls_ssl_out_hdr_len(ssl); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2683 | |
| 2684 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2685 | /* In case of DTLS, double-check that we don't exceed |
| 2686 | * the remaining space in the datagram. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2687 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 2688 | ret = ssl_get_remaining_space_in_datagram(ssl); |
| 2689 | if (ret < 0) { |
| 2690 | return ret; |
| 2691 | } |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2692 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2693 | if (protected_record_size > (size_t) ret) { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2694 | /* Should never happen */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2695 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2696 | } |
| 2697 | } |
| 2698 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2699 | |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 2700 | /* Now write the potentially updated record content type. */ |
| 2701 | ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype; |
| 2702 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2703 | MBEDTLS_SSL_DEBUG_MSG(3, ("output record: msgtype = %u, " |
| 2704 | "version = [%u:%u], msglen = %" MBEDTLS_PRINTF_SIZET, |
| 2705 | ssl->out_hdr[0], ssl->out_hdr[1], |
| 2706 | ssl->out_hdr[2], len)); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2707 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2708 | MBEDTLS_SSL_DEBUG_BUF(4, "output record sent to network", |
| 2709 | ssl->out_hdr, protected_record_size); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2710 | |
| 2711 | ssl->out_left += protected_record_size; |
| 2712 | ssl->out_hdr += protected_record_size; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2713 | mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2714 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2715 | for (i = 8; i > mbedtls_ssl_ep_len(ssl); i--) { |
| 2716 | if (++ssl->cur_out_ctr[i - 1] != 0) { |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 2717 | break; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2718 | } |
| 2719 | } |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 2720 | |
| 2721 | /* The loop goes to its end iff the counter is wrapping */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2722 | if (i == mbedtls_ssl_ep_len(ssl)) { |
| 2723 | MBEDTLS_SSL_DEBUG_MSG(1, ("outgoing message counter would wrap")); |
| 2724 | return MBEDTLS_ERR_SSL_COUNTER_WRAPPING; |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 2725 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2726 | } |
| 2727 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2728 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2729 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 2730 | flush == SSL_DONT_FORCE_FLUSH) { |
Hanno Becker | 1f5a15d | 2018-08-21 13:31:31 +0100 | [diff] [blame] | 2731 | size_t remaining; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2732 | ret = ssl_get_remaining_payload_in_datagram(ssl); |
| 2733 | if (ret < 0) { |
| 2734 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_remaining_payload_in_datagram", |
| 2735 | ret); |
| 2736 | return ret; |
Hanno Becker | 1f5a15d | 2018-08-21 13:31:31 +0100 | [diff] [blame] | 2737 | } |
| 2738 | |
| 2739 | remaining = (size_t) ret; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2740 | if (remaining == 0) { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2741 | flush = SSL_FORCE_FLUSH; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2742 | } else { |
| 2743 | MBEDTLS_SSL_DEBUG_MSG(2, |
| 2744 | ("Still %u bytes available in current datagram", |
| 2745 | (unsigned) remaining)); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2746 | } |
| 2747 | } |
| 2748 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 2749 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2750 | if ((flush == SSL_FORCE_FLUSH) && |
| 2751 | (ret = mbedtls_ssl_flush_output(ssl)) != 0) { |
| 2752 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flush_output", ret); |
| 2753 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2754 | } |
| 2755 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2756 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write record")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2757 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2758 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2759 | } |
| 2760 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2761 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 2762 | |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 2763 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2764 | static int ssl_hs_is_proper_fragment(mbedtls_ssl_context *ssl) |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 2765 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2766 | if (ssl->in_msglen < ssl->in_hslen || |
| 2767 | memcmp(ssl->in_msg + 6, "\0\0\0", 3) != 0 || |
| 2768 | memcmp(ssl->in_msg + 9, ssl->in_msg + 1, 3) != 0) { |
| 2769 | return 1; |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 2770 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2771 | return 0; |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 2772 | } |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2773 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2774 | 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] | 2775 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2776 | return (ssl->in_msg[9] << 16) | |
| 2777 | (ssl->in_msg[10] << 8) | |
| 2778 | ssl->in_msg[11]; |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2779 | } |
| 2780 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2781 | 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] | 2782 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2783 | return (ssl->in_msg[6] << 16) | |
| 2784 | (ssl->in_msg[7] << 8) | |
| 2785 | ssl->in_msg[8]; |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2786 | } |
| 2787 | |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 2788 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2789 | static int ssl_check_hs_header(mbedtls_ssl_context const *ssl) |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2790 | { |
| 2791 | uint32_t msg_len, frag_off, frag_len; |
| 2792 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2793 | msg_len = ssl_get_hs_total_len(ssl); |
| 2794 | frag_off = ssl_get_hs_frag_off(ssl); |
| 2795 | frag_len = ssl_get_hs_frag_len(ssl); |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2796 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2797 | if (frag_off > msg_len) { |
| 2798 | return -1; |
| 2799 | } |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2800 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2801 | if (frag_len > msg_len - frag_off) { |
| 2802 | return -1; |
| 2803 | } |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2804 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2805 | if (frag_len + 12 > ssl->in_msglen) { |
| 2806 | return -1; |
| 2807 | } |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2808 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2809 | return 0; |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2810 | } |
| 2811 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2812 | /* |
| 2813 | * Mark bits in bitmask (used for DTLS HS reassembly) |
| 2814 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2815 | 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] | 2816 | { |
| 2817 | unsigned int start_bits, end_bits; |
| 2818 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2819 | start_bits = 8 - (offset % 8); |
| 2820 | if (start_bits != 8) { |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2821 | size_t first_byte_idx = offset / 8; |
| 2822 | |
Manuel Pégourié-Gonnard | ac03052 | 2014-09-02 14:23:40 +0200 | [diff] [blame] | 2823 | /* Special case */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2824 | if (len <= start_bits) { |
| 2825 | for (; len != 0; len--) { |
| 2826 | mask[first_byte_idx] |= 1 << (start_bits - len); |
| 2827 | } |
Manuel Pégourié-Gonnard | ac03052 | 2014-09-02 14:23:40 +0200 | [diff] [blame] | 2828 | |
| 2829 | /* Avoid potential issues with offset or len becoming invalid */ |
| 2830 | return; |
| 2831 | } |
| 2832 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2833 | offset += start_bits; /* Now offset % 8 == 0 */ |
| 2834 | len -= start_bits; |
| 2835 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2836 | for (; start_bits != 0; start_bits--) { |
| 2837 | mask[first_byte_idx] |= 1 << (start_bits - 1); |
| 2838 | } |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2839 | } |
| 2840 | |
| 2841 | end_bits = len % 8; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2842 | if (end_bits != 0) { |
| 2843 | size_t last_byte_idx = (offset + len) / 8; |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2844 | |
| 2845 | len -= end_bits; /* Now len % 8 == 0 */ |
| 2846 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2847 | for (; end_bits != 0; end_bits--) { |
| 2848 | mask[last_byte_idx] |= 1 << (8 - end_bits); |
| 2849 | } |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2850 | } |
| 2851 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2852 | memset(mask + offset / 8, 0xFF, len / 8); |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2853 | } |
| 2854 | |
| 2855 | /* |
| 2856 | * Check that bitmask is full |
| 2857 | */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 2858 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2859 | 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] | 2860 | { |
| 2861 | size_t i; |
| 2862 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2863 | for (i = 0; i < len / 8; i++) { |
| 2864 | if (mask[i] != 0xFF) { |
| 2865 | return -1; |
| 2866 | } |
| 2867 | } |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2868 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2869 | for (i = 0; i < len % 8; i++) { |
| 2870 | if ((mask[len / 8] & (1 << (7 - i))) == 0) { |
| 2871 | return -1; |
| 2872 | } |
| 2873 | } |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2874 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2875 | return 0; |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2876 | } |
| 2877 | |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 2878 | /* msg_len does not include the handshake header */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2879 | static size_t ssl_get_reassembly_buffer_size(size_t msg_len, |
| 2880 | unsigned add_bitmap) |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2881 | { |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 2882 | size_t alloc_len; |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2883 | |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 2884 | alloc_len = 12; /* Handshake header */ |
| 2885 | alloc_len += msg_len; /* Content buffer */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2886 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2887 | if (add_bitmap) { |
| 2888 | alloc_len += msg_len / 8 + (msg_len % 8 != 0); /* Bitmap */ |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2889 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2890 | } |
| 2891 | return alloc_len; |
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 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2894 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2895 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2896 | 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] | 2897 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2898 | return (ssl->in_msg[1] << 16) | |
| 2899 | (ssl->in_msg[2] << 8) | |
| 2900 | ssl->in_msg[3]; |
Hanno Becker | 12555c6 | 2018-08-16 12:47:53 +0100 | [diff] [blame] | 2901 | } |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 2902 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2903 | int mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 2904 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2905 | if (ssl->in_msglen < mbedtls_ssl_hs_hdr_len(ssl)) { |
| 2906 | MBEDTLS_SSL_DEBUG_MSG(1, ("handshake message too short: %" MBEDTLS_PRINTF_SIZET, |
| 2907 | ssl->in_msglen)); |
| 2908 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 2909 | } |
| 2910 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2911 | 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] | 2912 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2913 | MBEDTLS_SSL_DEBUG_MSG(3, ("handshake message: msglen =" |
| 2914 | " %" MBEDTLS_PRINTF_SIZET ", type = %u, hslen = %" |
| 2915 | MBEDTLS_PRINTF_SIZET, |
| 2916 | ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen)); |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 2917 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2918 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2919 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2920 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2921 | 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] | 2922 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2923 | if (ssl_check_hs_header(ssl) != 0) { |
| 2924 | MBEDTLS_SSL_DEBUG_MSG(1, ("invalid handshake header")); |
| 2925 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2926 | } |
| 2927 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2928 | if (ssl->handshake != NULL && |
| 2929 | ((ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && |
| 2930 | recv_msg_seq != ssl->handshake->in_msg_seq) || |
| 2931 | (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER && |
| 2932 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO))) { |
| 2933 | if (recv_msg_seq > ssl->handshake->in_msg_seq) { |
| 2934 | MBEDTLS_SSL_DEBUG_MSG(2, |
| 2935 | ( |
| 2936 | "received future handshake message of sequence number %u (next %u)", |
| 2937 | recv_msg_seq, |
| 2938 | ssl->handshake->in_msg_seq)); |
| 2939 | return MBEDTLS_ERR_SSL_EARLY_MESSAGE; |
Hanno Becker | 9e1ec22 | 2018-08-15 15:54:43 +0100 | [diff] [blame] | 2940 | } |
| 2941 | |
Manuel Pégourié-Gonnard | fc572dd | 2014-10-09 17:56:57 +0200 | [diff] [blame] | 2942 | /* Retransmit only on last message from previous flight, to avoid |
| 2943 | * too many retransmissions. |
| 2944 | * Besides, No sane server ever retransmits HelloVerifyRequest */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2945 | if (recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 && |
| 2946 | ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST) { |
| 2947 | MBEDTLS_SSL_DEBUG_MSG(2, ("received message from last flight, " |
| 2948 | "message_seq = %u, start_of_flight = %u", |
| 2949 | recv_msg_seq, |
| 2950 | ssl->handshake->in_flight_start_seq)); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 2951 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2952 | if ((ret = mbedtls_ssl_resend(ssl)) != 0) { |
| 2953 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend", ret); |
| 2954 | return ret; |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 2955 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2956 | } else { |
| 2957 | MBEDTLS_SSL_DEBUG_MSG(2, ("dropping out-of-sequence message: " |
| 2958 | "message_seq = %u, expected = %u", |
| 2959 | recv_msg_seq, |
| 2960 | ssl->handshake->in_msg_seq)); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 2961 | } |
| 2962 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2963 | return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 2964 | } |
| 2965 | /* Wait until message completion to increment in_msg_seq */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2966 | |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 2967 | /* Message reassembly is handled alongside buffering of future |
| 2968 | * messages; the commonality is that both handshake fragments and |
Hanno Becker | 83ab41c | 2018-08-28 17:19:38 +0100 | [diff] [blame] | 2969 | * future messages cannot be forwarded immediately to the |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 2970 | * handshake logic layer. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2971 | if (ssl_hs_is_proper_fragment(ssl) == 1) { |
| 2972 | MBEDTLS_SSL_DEBUG_MSG(2, ("found fragmented DTLS handshake message")); |
| 2973 | return MBEDTLS_ERR_SSL_EARLY_MESSAGE; |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2974 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2975 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2976 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2977 | /* With TLS we don't handle fragmentation (for now) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2978 | if (ssl->in_msglen < ssl->in_hslen) { |
| 2979 | MBEDTLS_SSL_DEBUG_MSG(1, ("TLS handshake fragmentation not supported")); |
| 2980 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 2981 | } |
| 2982 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2983 | return 0; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 2984 | } |
| 2985 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2986 | void mbedtls_ssl_update_handshake_status(mbedtls_ssl_context *ssl) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 2987 | { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 2988 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 2989 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2990 | if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL) { |
| 2991 | 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] | 2992 | } |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 2993 | |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 2994 | /* Handshake message is complete, increment counter */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2995 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2996 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 2997 | ssl->handshake != NULL) { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 2998 | unsigned offset; |
| 2999 | mbedtls_ssl_hs_buffer *hs_buf; |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 3000 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3001 | /* Increment handshake sequence number */ |
| 3002 | hs->in_msg_seq++; |
| 3003 | |
| 3004 | /* |
| 3005 | * Clear up handshake buffering and reassembly structure. |
| 3006 | */ |
| 3007 | |
| 3008 | /* Free first entry */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3009 | ssl_buffering_free_slot(ssl, 0); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3010 | |
| 3011 | /* Shift all other entries */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3012 | for (offset = 0, hs_buf = &hs->buffering.hs[0]; |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 3013 | offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3014 | offset++, hs_buf++) { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 3015 | *hs_buf = *(hs_buf + 1); |
| 3016 | } |
| 3017 | |
| 3018 | /* Create a fresh last entry */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3019 | memset(hs_buf, 0, sizeof(mbedtls_ssl_hs_buffer)); |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 3020 | } |
| 3021 | #endif |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 3022 | } |
| 3023 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3024 | /* |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3025 | * DTLS anti-replay: RFC 6347 4.1.2.6 |
| 3026 | * |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3027 | * in_window is a field of bits numbered from 0 (lsb) to 63 (msb). |
| 3028 | * Bit n is set iff record number in_window_top - n has been seen. |
| 3029 | * |
| 3030 | * Usually, in_window_top is the last record number seen and the lsb of |
| 3031 | * in_window is set. The only exception is the initial state (record number 0 |
| 3032 | * not seen yet). |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3033 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3034 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3035 | void mbedtls_ssl_dtls_replay_reset(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3036 | { |
| 3037 | ssl->in_window_top = 0; |
| 3038 | ssl->in_window = 0; |
| 3039 | } |
| 3040 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3041 | 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] | 3042 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3043 | return ((uint64_t) buf[0] << 40) | |
| 3044 | ((uint64_t) buf[1] << 32) | |
| 3045 | ((uint64_t) buf[2] << 24) | |
| 3046 | ((uint64_t) buf[3] << 16) | |
| 3047 | ((uint64_t) buf[4] << 8) | |
| 3048 | ((uint64_t) buf[5]); |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3049 | } |
| 3050 | |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3051 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3052 | 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] | 3053 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3054 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Arto Kinnunen | 7f8089b | 2019-10-29 11:13:33 +0200 | [diff] [blame] | 3055 | unsigned char *original_in_ctr; |
| 3056 | |
| 3057 | // save original in_ctr |
| 3058 | original_in_ctr = ssl->in_ctr; |
| 3059 | |
| 3060 | // use counter from record |
| 3061 | ssl->in_ctr = record_in_ctr; |
| 3062 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3063 | ret = mbedtls_ssl_dtls_replay_check((mbedtls_ssl_context const *) ssl); |
Arto Kinnunen | 7f8089b | 2019-10-29 11:13:33 +0200 | [diff] [blame] | 3064 | |
| 3065 | // restore the counter |
| 3066 | ssl->in_ctr = original_in_ctr; |
| 3067 | |
| 3068 | return ret; |
| 3069 | } |
| 3070 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3071 | /* |
| 3072 | * Return 0 if sequence number is acceptable, -1 otherwise |
| 3073 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3074 | 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] | 3075 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3076 | 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] | 3077 | uint64_t bit; |
| 3078 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3079 | if (ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED) { |
| 3080 | return 0; |
| 3081 | } |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 3082 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3083 | if (rec_seqnum > ssl->in_window_top) { |
| 3084 | return 0; |
| 3085 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3086 | |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3087 | bit = ssl->in_window_top - rec_seqnum; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3088 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3089 | if (bit >= 64) { |
| 3090 | return -1; |
| 3091 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3092 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3093 | if ((ssl->in_window & ((uint64_t) 1 << bit)) != 0) { |
| 3094 | return -1; |
| 3095 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3096 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3097 | return 0; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3098 | } |
| 3099 | |
| 3100 | /* |
| 3101 | * Update replay window on new validated record |
| 3102 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3103 | void mbedtls_ssl_dtls_replay_update(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3104 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3105 | 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] | 3106 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3107 | if (ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED) { |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 3108 | return; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3109 | } |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 3110 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3111 | if (rec_seqnum > ssl->in_window_top) { |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3112 | /* Update window_top and the contents of the window */ |
| 3113 | uint64_t shift = rec_seqnum - ssl->in_window_top; |
| 3114 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3115 | if (shift >= 64) { |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3116 | ssl->in_window = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3117 | } else { |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3118 | ssl->in_window <<= shift; |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3119 | ssl->in_window |= 1; |
| 3120 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3121 | |
| 3122 | ssl->in_window_top = rec_seqnum; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3123 | } else { |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3124 | /* Mark that number as seen in the current window */ |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3125 | uint64_t bit = ssl->in_window_top - rec_seqnum; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3126 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3127 | if (bit < 64) { /* Always true, but be extra sure */ |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3128 | ssl->in_window |= (uint64_t) 1 << bit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3129 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3130 | } |
| 3131 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3132 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3133 | |
Manuel Pégourié-Gonnard | ddfe5d2 | 2015-09-09 12:46:16 +0200 | [diff] [blame] | 3134 | #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] | 3135 | /* |
Gilles Peskine | f333dfa | 2022-02-15 23:53:36 +0100 | [diff] [blame] | 3136 | * Check if a datagram looks like a ClientHello with a valid cookie, |
| 3137 | * and if it doesn't, generate a HelloVerifyRequest message. |
Simon Butcher | 0789aed | 2015-09-11 17:15:17 +0100 | [diff] [blame] | 3138 | * Both input and output include full DTLS headers. |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3139 | * |
| 3140 | * - if cookie is valid, return 0 |
| 3141 | * - if ClientHello looks superficially valid but cookie is not, |
| 3142 | * fill obuf and set olen, then |
| 3143 | * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED |
| 3144 | * - otherwise return a specific error code |
| 3145 | */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3146 | MBEDTLS_CHECK_RETURN_CRITICAL |
Andrzej Kurek | 33f41a8 | 2022-06-08 11:47:33 -0400 | [diff] [blame] | 3147 | MBEDTLS_STATIC_TESTABLE |
| 3148 | int mbedtls_ssl_check_dtls_clihlo_cookie( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3149 | mbedtls_ssl_context *ssl, |
| 3150 | const unsigned char *cli_id, size_t cli_id_len, |
| 3151 | const unsigned char *in, size_t in_len, |
| 3152 | unsigned char *obuf, size_t buf_len, size_t *olen) |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3153 | { |
| 3154 | size_t sid_len, cookie_len; |
| 3155 | unsigned char *p; |
| 3156 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3157 | /* |
| 3158 | * Structure of ClientHello with record and handshake headers, |
| 3159 | * and expected values. We don't need to check a lot, more checks will be |
| 3160 | * done when actually parsing the ClientHello - skipping those checks |
| 3161 | * avoids code duplication and does not make cookie forging any easier. |
| 3162 | * |
| 3163 | * 0-0 ContentType type; copied, must be handshake |
| 3164 | * 1-2 ProtocolVersion version; copied |
| 3165 | * 3-4 uint16 epoch; copied, must be 0 |
| 3166 | * 5-10 uint48 sequence_number; copied |
| 3167 | * 11-12 uint16 length; (ignored) |
| 3168 | * |
| 3169 | * 13-13 HandshakeType msg_type; (ignored) |
| 3170 | * 14-16 uint24 length; (ignored) |
| 3171 | * 17-18 uint16 message_seq; copied |
| 3172 | * 19-21 uint24 fragment_offset; copied, must be 0 |
| 3173 | * 22-24 uint24 fragment_length; (ignored) |
| 3174 | * |
| 3175 | * 25-26 ProtocolVersion client_version; (ignored) |
| 3176 | * 27-58 Random random; (ignored) |
| 3177 | * 59-xx SessionID session_id; 1 byte len + sid_len content |
| 3178 | * 60+ opaque cookie<0..2^8-1>; 1 byte len + content |
| 3179 | * ... |
| 3180 | * |
| 3181 | * Minimum length is 61 bytes. |
| 3182 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3183 | MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: in_len=%u", |
| 3184 | (unsigned) in_len)); |
| 3185 | MBEDTLS_SSL_DEBUG_BUF(4, "cli_id", cli_id, cli_id_len); |
| 3186 | if (in_len < 61) { |
| 3187 | MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: record too short")); |
| 3188 | return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO; |
Gilles Peskine | f333dfa | 2022-02-15 23:53:36 +0100 | [diff] [blame] | 3189 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3190 | if (in[0] != MBEDTLS_SSL_MSG_HANDSHAKE || |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3191 | in[3] != 0 || in[4] != 0 || |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3192 | in[19] != 0 || in[20] != 0 || in[21] != 0) { |
| 3193 | MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: not a good ClientHello")); |
| 3194 | MBEDTLS_SSL_DEBUG_MSG(4, (" type=%u epoch=%u fragment_offset=%u", |
| 3195 | in[0], |
| 3196 | (unsigned) in[3] << 8 | in[4], |
| 3197 | (unsigned) in[19] << 16 | in[20] << 8 | in[21])); |
| 3198 | return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3199 | } |
| 3200 | |
| 3201 | sid_len = in[59]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3202 | if (59 + 1 + sid_len + 1 > in_len) { |
| 3203 | MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: sid_len=%u > %u", |
| 3204 | (unsigned) sid_len, |
| 3205 | (unsigned) in_len - 61)); |
| 3206 | return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO; |
Gilles Peskine | f333dfa | 2022-02-15 23:53:36 +0100 | [diff] [blame] | 3207 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3208 | MBEDTLS_SSL_DEBUG_BUF(4, "sid received from network", |
| 3209 | in + 60, sid_len); |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3210 | |
| 3211 | cookie_len = in[60 + sid_len]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3212 | if (59 + 1 + sid_len + 1 + cookie_len > in_len) { |
| 3213 | MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: cookie_len=%u > %u", |
| 3214 | (unsigned) cookie_len, |
| 3215 | (unsigned) (in_len - sid_len - 61))); |
| 3216 | return MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO; |
Gilles Peskine | f333dfa | 2022-02-15 23:53:36 +0100 | [diff] [blame] | 3217 | } |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3218 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3219 | MBEDTLS_SSL_DEBUG_BUF(4, "cookie received from network", |
| 3220 | in + sid_len + 61, cookie_len); |
| 3221 | if (ssl->conf->f_cookie_check(ssl->conf->p_cookie, |
| 3222 | in + sid_len + 61, cookie_len, |
| 3223 | cli_id, cli_id_len) == 0) { |
| 3224 | MBEDTLS_SSL_DEBUG_MSG(4, ("check cookie: valid")); |
| 3225 | return 0; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3226 | } |
| 3227 | |
| 3228 | /* |
| 3229 | * If we get here, we've got an invalid cookie, let's prepare HVR. |
| 3230 | * |
| 3231 | * 0-0 ContentType type; copied |
| 3232 | * 1-2 ProtocolVersion version; copied |
| 3233 | * 3-4 uint16 epoch; copied |
| 3234 | * 5-10 uint48 sequence_number; copied |
| 3235 | * 11-12 uint16 length; olen - 13 |
| 3236 | * |
| 3237 | * 13-13 HandshakeType msg_type; hello_verify_request |
| 3238 | * 14-16 uint24 length; olen - 25 |
| 3239 | * 17-18 uint16 message_seq; copied |
| 3240 | * 19-21 uint24 fragment_offset; copied |
| 3241 | * 22-24 uint24 fragment_length; olen - 25 |
| 3242 | * |
| 3243 | * 25-26 ProtocolVersion server_version; 0xfe 0xff |
| 3244 | * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie |
| 3245 | * |
| 3246 | * Minimum length is 28. |
| 3247 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3248 | if (buf_len < 28) { |
| 3249 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 3250 | } |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3251 | |
| 3252 | /* Copy most fields and adapt others */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3253 | memcpy(obuf, in, 25); |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3254 | obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST; |
| 3255 | obuf[25] = 0xfe; |
| 3256 | obuf[26] = 0xff; |
| 3257 | |
| 3258 | /* Generate and write actual cookie */ |
| 3259 | p = obuf + 28; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3260 | if (ssl->conf->f_cookie_write(ssl->conf->p_cookie, |
| 3261 | &p, obuf + buf_len, |
| 3262 | cli_id, cli_id_len) != 0) { |
| 3263 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3264 | } |
| 3265 | |
| 3266 | *olen = p - obuf; |
| 3267 | |
| 3268 | /* Go back and fill length fields */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3269 | obuf[27] = (unsigned char) (*olen - 28); |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3270 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3271 | obuf[14] = obuf[22] = MBEDTLS_BYTE_2(*olen - 25); |
| 3272 | obuf[15] = obuf[23] = MBEDTLS_BYTE_1(*olen - 25); |
| 3273 | obuf[16] = obuf[24] = MBEDTLS_BYTE_0(*olen - 25); |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3274 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3275 | MBEDTLS_PUT_UINT16_BE(*olen - 13, obuf, 11); |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3276 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3277 | return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3278 | } |
| 3279 | |
| 3280 | /* |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3281 | * Handle possible client reconnect with the same UDP quadruplet |
| 3282 | * (RFC 6347 Section 4.2.8). |
| 3283 | * |
| 3284 | * Called by ssl_parse_record_header() in case we receive an epoch 0 record |
| 3285 | * that looks like a ClientHello. |
| 3286 | * |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3287 | * - if the input looks like a ClientHello without cookies, |
Manuel Pégourié-Gonnard | 824655c | 2020-03-11 12:51:42 +0100 | [diff] [blame] | 3288 | * send back HelloVerifyRequest, then return 0 |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3289 | * - if the input looks like a ClientHello with a valid cookie, |
| 3290 | * reset the session of the current context, and |
Manuel Pégourié-Gonnard | be619c1 | 2015-09-08 11:21:21 +0200 | [diff] [blame] | 3291 | * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3292 | * - if anything goes wrong, return a specific error code |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3293 | * |
Manuel Pégourié-Gonnard | 824655c | 2020-03-11 12:51:42 +0100 | [diff] [blame] | 3294 | * This function is called (through ssl_check_client_reconnect()) when an |
| 3295 | * unexpected record is found in ssl_get_next_record(), which will discard the |
| 3296 | * record if we return 0, and bubble up the return value otherwise (this |
| 3297 | * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected |
| 3298 | * 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] | 3299 | */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3300 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3301 | static int ssl_handle_possible_reconnect(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3302 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3303 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3304 | size_t len; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3305 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3306 | if (ssl->conf->f_cookie_write == NULL || |
| 3307 | ssl->conf->f_cookie_check == NULL) { |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 3308 | /* If we can't use cookies to verify reachability of the peer, |
| 3309 | * drop the record. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3310 | MBEDTLS_SSL_DEBUG_MSG(1, ("no cookie callbacks, " |
| 3311 | "can't check reconnect validity")); |
| 3312 | return 0; |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 3313 | } |
| 3314 | |
Andrzej Kurek | 33f41a8 | 2022-06-08 11:47:33 -0400 | [diff] [blame] | 3315 | ret = mbedtls_ssl_check_dtls_clihlo_cookie( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3316 | ssl, |
| 3317 | ssl->cli_id, ssl->cli_id_len, |
| 3318 | ssl->in_buf, ssl->in_left, |
| 3319 | ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len); |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3320 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3321 | 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] | 3322 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3323 | if (ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED) { |
Manuel Pégourié-Gonnard | 243d70f | 2020-03-31 12:07:47 +0200 | [diff] [blame] | 3324 | int send_ret; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3325 | MBEDTLS_SSL_DEBUG_MSG(1, ("sending HelloVerifyRequest")); |
| 3326 | MBEDTLS_SSL_DEBUG_BUF(4, "output record sent to network", |
| 3327 | ssl->out_buf, len); |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 3328 | /* 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] | 3329 | * If the error is permanent we'll catch it later, |
| 3330 | * if it's not, then hopefully it'll work next time. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3331 | send_ret = ssl->f_send(ssl->p_bio, ssl->out_buf, len); |
| 3332 | MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_send", send_ret); |
Manuel Pégourié-Gonnard | 243d70f | 2020-03-31 12:07:47 +0200 | [diff] [blame] | 3333 | (void) send_ret; |
| 3334 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3335 | return 0; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3336 | } |
| 3337 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3338 | if (ret == 0) { |
| 3339 | MBEDTLS_SSL_DEBUG_MSG(1, ("cookie is valid, resetting context")); |
| 3340 | if ((ret = mbedtls_ssl_session_reset_int(ssl, 1)) != 0) { |
| 3341 | MBEDTLS_SSL_DEBUG_RET(1, "reset", ret); |
| 3342 | return ret; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3343 | } |
| 3344 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3345 | return MBEDTLS_ERR_SSL_CLIENT_RECONNECT; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3346 | } |
| 3347 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3348 | return ret; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3349 | } |
Manuel Pégourié-Gonnard | ddfe5d2 | 2015-09-09 12:46:16 +0200 | [diff] [blame] | 3350 | #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] | 3351 | |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3352 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3353 | static int ssl_check_record_type(uint8_t record_type) |
Hanno Becker | f661c9c | 2019-05-03 13:25:54 +0100 | [diff] [blame] | 3354 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3355 | if (record_type != MBEDTLS_SSL_MSG_HANDSHAKE && |
Hanno Becker | f661c9c | 2019-05-03 13:25:54 +0100 | [diff] [blame] | 3356 | record_type != MBEDTLS_SSL_MSG_ALERT && |
| 3357 | record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC && |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3358 | record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA) { |
| 3359 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | f661c9c | 2019-05-03 13:25:54 +0100 | [diff] [blame] | 3360 | } |
| 3361 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3362 | return 0; |
Hanno Becker | f661c9c | 2019-05-03 13:25:54 +0100 | [diff] [blame] | 3363 | } |
| 3364 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3365 | /* |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3366 | * ContentType type; |
| 3367 | * ProtocolVersion version; |
| 3368 | * uint16 epoch; // DTLS only |
| 3369 | * uint48 sequence_number; // DTLS only |
| 3370 | * uint16 length; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3371 | * |
| 3372 | * 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] | 3373 | * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad, |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3374 | * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected. |
| 3375 | * |
| 3376 | * With DTLS, mbedtls_ssl_read_record() will: |
Simon Butcher | 207990d | 2015-12-16 01:51:30 +0000 | [diff] [blame] | 3377 | * 1. proceed with the record if this function returns 0 |
| 3378 | * 2. drop only the current record if this function returns UNEXPECTED_RECORD |
| 3379 | * 3. return CLIENT_RECONNECT if this function return that value |
| 3380 | * 4. drop the whole datagram if this function returns anything else. |
| 3381 | * Point 2 is needed when the peer is resending, and we have already received |
| 3382 | * 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] | 3383 | */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3384 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3385 | static int ssl_parse_record_header(mbedtls_ssl_context const *ssl, |
| 3386 | unsigned char *buf, |
| 3387 | size_t len, |
| 3388 | mbedtls_record *rec) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3389 | { |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 3390 | int major_ver, minor_ver; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3391 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3392 | size_t const rec_hdr_type_offset = 0; |
| 3393 | size_t const rec_hdr_type_len = 1; |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 3394 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3395 | size_t const rec_hdr_version_offset = rec_hdr_type_offset + |
| 3396 | rec_hdr_type_len; |
| 3397 | size_t const rec_hdr_version_len = 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3398 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3399 | size_t const rec_hdr_ctr_len = 8; |
| 3400 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | f546625 | 2019-07-25 10:13:02 +0100 | [diff] [blame] | 3401 | uint32_t rec_epoch; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3402 | size_t const rec_hdr_ctr_offset = rec_hdr_version_offset + |
| 3403 | rec_hdr_version_len; |
| 3404 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3405 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3406 | size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset + |
| 3407 | rec_hdr_ctr_len; |
Hanno Becker | f546625 | 2019-07-25 10:13:02 +0100 | [diff] [blame] | 3408 | size_t rec_hdr_cid_len = 0; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3409 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3410 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3411 | |
| 3412 | size_t rec_hdr_len_offset; /* To be determined */ |
| 3413 | size_t const rec_hdr_len_len = 2; |
| 3414 | |
| 3415 | /* |
| 3416 | * Check minimum lengths for record header. |
| 3417 | */ |
| 3418 | |
| 3419 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3420 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3421 | rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3422 | } else |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3423 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3424 | { |
| 3425 | rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len; |
| 3426 | } |
| 3427 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3428 | if (len < rec_hdr_len_offset + rec_hdr_len_len) { |
| 3429 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 3430 | ( |
| 3431 | "datagram of length %u too small to hold DTLS record header of length %u", |
| 3432 | (unsigned) len, |
| 3433 | (unsigned) (rec_hdr_len_len + rec_hdr_len_len))); |
| 3434 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3435 | } |
| 3436 | |
| 3437 | /* |
| 3438 | * Parse and validate record content type |
| 3439 | */ |
| 3440 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3441 | rec->type = buf[rec_hdr_type_offset]; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3442 | |
| 3443 | /* Check record content type */ |
| 3444 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3445 | rec->cid_len = 0; |
| 3446 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3447 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3448 | ssl->conf->cid_len != 0 && |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3449 | rec->type == MBEDTLS_SSL_MSG_CID) { |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 3450 | /* Shift pointers to account for record header including CID |
| 3451 | * struct { |
| 3452 | * ContentType special_type = tls12_cid; |
| 3453 | * ProtocolVersion version; |
| 3454 | * uint16 epoch; |
| 3455 | * uint48 sequence_number; |
Hanno Becker | 8e55b0f | 2019-05-23 17:03:19 +0100 | [diff] [blame] | 3456 | * opaque cid[cid_length]; // Additional field compared to |
| 3457 | * // default DTLS record format |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 3458 | * uint16 length; |
| 3459 | * opaque enc_content[DTLSCiphertext.length]; |
| 3460 | * } DTLSCiphertext; |
| 3461 | */ |
| 3462 | |
| 3463 | /* So far, we only support static CID lengths |
| 3464 | * fixed in the configuration. */ |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3465 | rec_hdr_cid_len = ssl->conf->cid_len; |
| 3466 | rec_hdr_len_offset += rec_hdr_cid_len; |
Hanno Becker | e538d82 | 2019-07-10 14:50:10 +0100 | [diff] [blame] | 3467 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3468 | if (len < rec_hdr_len_offset + rec_hdr_len_len) { |
| 3469 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 3470 | ( |
| 3471 | "datagram of length %u too small to hold DTLS record header including CID, length %u", |
| 3472 | (unsigned) len, |
| 3473 | (unsigned) (rec_hdr_len_offset + rec_hdr_len_len))); |
| 3474 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | e538d82 | 2019-07-10 14:50:10 +0100 | [diff] [blame] | 3475 | } |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3476 | |
Manuel Pégourié-Gonnard | 7e821b5 | 2019-08-02 10:17:15 +0200 | [diff] [blame] | 3477 | /* configured CID len is guaranteed at most 255, see |
| 3478 | * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */ |
| 3479 | rec->cid_len = (uint8_t) rec_hdr_cid_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3480 | memcpy(rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len); |
| 3481 | } else |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3482 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 3483 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3484 | if (ssl_check_record_type(rec->type)) { |
| 3485 | MBEDTLS_SSL_DEBUG_MSG(1, ("unknown record type %u", |
| 3486 | (unsigned) rec->type)); |
| 3487 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3488 | } |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 3489 | } |
| 3490 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3491 | /* |
| 3492 | * Parse and validate record version |
| 3493 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3494 | rec->ver[0] = buf[rec_hdr_version_offset + 0]; |
| 3495 | rec->ver[1] = buf[rec_hdr_version_offset + 1]; |
| 3496 | mbedtls_ssl_read_version(&major_ver, &minor_ver, |
| 3497 | ssl->conf->transport, |
| 3498 | &rec->ver[0]); |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3499 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3500 | if (major_ver != ssl->major_ver) { |
| 3501 | MBEDTLS_SSL_DEBUG_MSG(1, ("major version mismatch: got %u, expected %u", |
| 3502 | (unsigned) major_ver, |
| 3503 | (unsigned) ssl->major_ver)); |
| 3504 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3505 | } |
| 3506 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3507 | if (minor_ver > ssl->conf->max_minor_ver) { |
| 3508 | MBEDTLS_SSL_DEBUG_MSG(1, ("minor version mismatch: got %u, expected max %u", |
| 3509 | (unsigned) minor_ver, |
| 3510 | (unsigned) ssl->conf->max_minor_ver)); |
| 3511 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3512 | } |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3513 | /* |
| 3514 | * Parse/Copy record sequence number. |
| 3515 | */ |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 3516 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3517 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3518 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3519 | /* Copy explicit record sequence number from input buffer. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3520 | memcpy(&rec->ctr[0], buf + rec_hdr_ctr_offset, |
| 3521 | rec_hdr_ctr_len); |
| 3522 | } else |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3523 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3524 | { |
| 3525 | /* Copy implicit record sequence number from SSL context structure. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3526 | memcpy(&rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len); |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3527 | } |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 3528 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3529 | /* |
| 3530 | * Parse record length. |
| 3531 | */ |
| 3532 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3533 | rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3534 | rec->data_len = ((size_t) buf[rec_hdr_len_offset + 0] << 8) | |
| 3535 | ((size_t) buf[rec_hdr_len_offset + 1] << 0); |
| 3536 | MBEDTLS_SSL_DEBUG_BUF(4, "input record header", buf, rec->data_offset); |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 3537 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3538 | MBEDTLS_SSL_DEBUG_MSG(3, ("input record: msgtype = %u, " |
| 3539 | "version = [%d:%d], msglen = %" MBEDTLS_PRINTF_SIZET, |
| 3540 | rec->type, |
| 3541 | major_ver, minor_ver, rec->data_len)); |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3542 | |
| 3543 | rec->buf = buf; |
| 3544 | rec->buf_len = rec->data_offset + rec->data_len; |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 3545 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3546 | if (rec->data_len == 0) { |
| 3547 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
| 3548 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3549 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3550 | /* |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 3551 | * DTLS-related tests. |
| 3552 | * Check epoch before checking length constraint because |
| 3553 | * the latter varies with the epoch. E.g., if a ChangeCipherSpec |
| 3554 | * message gets duplicated before the corresponding Finished message, |
| 3555 | * the second ChangeCipherSpec should be discarded because it belongs |
| 3556 | * to an old epoch, but not because its length is shorter than |
| 3557 | * the minimum record length for packets using the new record transform. |
| 3558 | * Note that these two kinds of failures are handled differently, |
| 3559 | * as an unexpected record is silently skipped but an invalid |
| 3560 | * record leads to the entire datagram being dropped. |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3561 | */ |
| 3562 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3563 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 3564 | rec_epoch = (rec->ctr[0] << 8) | rec->ctr[1]; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3565 | |
Hanno Becker | 955a5c9 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 3566 | /* Check that the datagram is large enough to contain a record |
| 3567 | * of the advertised length. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3568 | if (len < rec->data_offset + rec->data_len) { |
| 3569 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 3570 | ( |
| 3571 | "Datagram of length %u too small to contain record of advertised length %u.", |
| 3572 | (unsigned) len, |
| 3573 | (unsigned) (rec->data_offset + rec->data_len))); |
| 3574 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | 955a5c9 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 3575 | } |
Hanno Becker | 37cfe73 | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 3576 | |
Hanno Becker | 37cfe73 | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 3577 | /* Records from other, non-matching epochs are silently discarded. |
| 3578 | * (The case of same-port Client reconnects must be considered in |
| 3579 | * the caller). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3580 | if (rec_epoch != ssl->in_epoch) { |
| 3581 | MBEDTLS_SSL_DEBUG_MSG(1, ("record from another epoch: " |
| 3582 | "expected %u, received %lu", |
| 3583 | ssl->in_epoch, (unsigned long) rec_epoch)); |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3584 | |
Hanno Becker | 552f747 | 2019-07-19 10:59:12 +0100 | [diff] [blame] | 3585 | /* Records from the next epoch are considered for buffering |
| 3586 | * (concretely: early Finished messages). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3587 | if (rec_epoch == (unsigned) ssl->in_epoch + 1) { |
| 3588 | MBEDTLS_SSL_DEBUG_MSG(2, ("Consider record for buffering")); |
| 3589 | return MBEDTLS_ERR_SSL_EARLY_MESSAGE; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3590 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 3591 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3592 | return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3593 | } |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3594 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Hanno Becker | 37cfe73 | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 3595 | /* For records from the correct epoch, check whether their |
| 3596 | * sequence number has been seen before. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3597 | else if (mbedtls_ssl_dtls_record_replay_check((mbedtls_ssl_context *) ssl, |
| 3598 | &rec->ctr[0]) != 0) { |
| 3599 | MBEDTLS_SSL_DEBUG_MSG(1, ("replayed record")); |
| 3600 | return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3601 | } |
| 3602 | #endif |
| 3603 | } |
| 3604 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3605 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3606 | return 0; |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3607 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3608 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3609 | |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 3610 | #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] | 3611 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3612 | static int ssl_check_client_reconnect(mbedtls_ssl_context *ssl) |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 3613 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3614 | 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] | 3615 | |
| 3616 | /* |
| 3617 | * Check for an epoch 0 ClientHello. We can't use in_msg here to |
| 3618 | * access the first byte of record content (handshake type), as we |
| 3619 | * have an active transform (possibly iv_len != 0), so use the |
| 3620 | * fact that the record header len is 13 instead. |
| 3621 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3622 | if (rec_epoch == 0 && |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 3623 | ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 3624 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER && |
| 3625 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3626 | ssl->in_left > 13 && |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3627 | ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO) { |
| 3628 | MBEDTLS_SSL_DEBUG_MSG(1, ("possible client reconnect " |
| 3629 | "from the same port")); |
| 3630 | return ssl_handle_possible_reconnect(ssl); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3631 | } |
| 3632 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3633 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3634 | } |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 3635 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3636 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3637 | /* |
Manuel Pégourié-Gonnard | c40b685 | 2020-01-03 12:18:49 +0100 | [diff] [blame] | 3638 | * If applicable, decrypt record content |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3639 | */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3640 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3641 | static int ssl_prepare_record_content(mbedtls_ssl_context *ssl, |
| 3642 | mbedtls_record *rec) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3643 | { |
| 3644 | int ret, done = 0; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3645 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3646 | MBEDTLS_SSL_DEBUG_BUF(4, "input record from network", |
| 3647 | rec->buf, rec->buf_len); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3648 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3649 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3650 | if (mbedtls_ssl_hw_record_read != NULL) { |
| 3651 | MBEDTLS_SSL_DEBUG_MSG(2, ("going for mbedtls_ssl_hw_record_read()")); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3652 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3653 | ret = mbedtls_ssl_hw_record_read(ssl); |
| 3654 | if (ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH) { |
| 3655 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_read", ret); |
| 3656 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3657 | } |
Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 3658 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3659 | if (ret == 0) { |
Paul Bakker | c787811 | 2012-12-19 14:41:14 +0100 | [diff] [blame] | 3660 | done = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3661 | } |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 3662 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3663 | #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3664 | if (!done && ssl->transform_in != NULL) { |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 3665 | unsigned char const old_msg_type = rec->type; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3666 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3667 | if ((ret = mbedtls_ssl_decrypt_buf(ssl, ssl->transform_in, |
| 3668 | rec)) != 0) { |
| 3669 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_decrypt_buf", ret); |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 3670 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3671 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3672 | if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID && |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 3673 | ssl->conf->ignore_unexpected_cid |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3674 | == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) { |
| 3675 | MBEDTLS_SSL_DEBUG_MSG(3, ("ignoring unexpected CID")); |
Hanno Becker | 16ded98 | 2019-05-08 13:02:55 +0100 | [diff] [blame] | 3676 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 3677 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3678 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 16ded98 | 2019-05-08 13:02:55 +0100 | [diff] [blame] | 3679 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3680 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3681 | } |
| 3682 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3683 | if (old_msg_type != rec->type) { |
| 3684 | MBEDTLS_SSL_DEBUG_MSG(4, ("record type after decrypt (before %d): %d", |
| 3685 | old_msg_type, rec->type)); |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 3686 | } |
| 3687 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3688 | MBEDTLS_SSL_DEBUG_BUF(4, "input payload after decrypt", |
| 3689 | rec->buf + rec->data_offset, rec->data_len); |
Hanno Becker | 1c0c37f | 2018-08-07 14:29:29 +0100 | [diff] [blame] | 3690 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3691 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 3692 | /* We have already checked the record content type |
| 3693 | * in ssl_parse_record_header(), failing or silently |
| 3694 | * dropping the record in the case of an unknown type. |
| 3695 | * |
| 3696 | * Since with the use of CIDs, the record content type |
| 3697 | * might change during decryption, re-check the record |
| 3698 | * content type, but treat a failure as fatal this time. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3699 | if (ssl_check_record_type(rec->type)) { |
| 3700 | MBEDTLS_SSL_DEBUG_MSG(1, ("unknown record type")); |
| 3701 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 3702 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3703 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 3704 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3705 | if (rec->data_len == 0) { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3706 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3707 | if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 |
| 3708 | && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA) { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3709 | /* 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] | 3710 | MBEDTLS_SSL_DEBUG_MSG(1, ("invalid zero-length message type: %d", ssl->in_msgtype)); |
| 3711 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3712 | } |
| 3713 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3714 | |
| 3715 | ssl->nb_zero++; |
| 3716 | |
| 3717 | /* |
| 3718 | * Three or more empty messages may be a DoS attack |
| 3719 | * (excessive CPU consumption). |
| 3720 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3721 | if (ssl->nb_zero > 3) { |
| 3722 | MBEDTLS_SSL_DEBUG_MSG(1, ("received four consecutive empty " |
| 3723 | "messages, possible DoS attack")); |
Hanno Becker | 6e7700d | 2019-05-08 10:38:32 +0100 | [diff] [blame] | 3724 | /* Treat the records as if they were not properly authenticated, |
| 3725 | * thereby failing the connection if we see more than allowed |
| 3726 | * by the configured bad MAC threshold. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3727 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3728 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3729 | } else { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3730 | ssl->nb_zero = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3731 | } |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3732 | |
| 3733 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3734 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3735 | ; /* in_ctr read from peer, not maintained internally */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3736 | } else |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3737 | #endif |
| 3738 | { |
| 3739 | unsigned i; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3740 | for (i = 8; i > mbedtls_ssl_ep_len(ssl); i--) { |
| 3741 | if (++ssl->in_ctr[i - 1] != 0) { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3742 | break; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3743 | } |
| 3744 | } |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3745 | |
| 3746 | /* The loop goes to its end iff the counter is wrapping */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3747 | if (i == mbedtls_ssl_ep_len(ssl)) { |
| 3748 | MBEDTLS_SSL_DEBUG_MSG(1, ("incoming message counter would wrap")); |
| 3749 | return MBEDTLS_ERR_SSL_COUNTER_WRAPPING; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3750 | } |
| 3751 | } |
| 3752 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3753 | } |
| 3754 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3755 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3756 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 3757 | mbedtls_ssl_dtls_replay_update(ssl); |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 3758 | } |
| 3759 | #endif |
| 3760 | |
Hanno Becker | d96e10b | 2019-07-09 17:30:02 +0100 | [diff] [blame] | 3761 | /* Check actual (decrypted) record content length against |
| 3762 | * configured maximum. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3763 | if (rec->data_len > MBEDTLS_SSL_IN_CONTENT_LEN) { |
| 3764 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad message length")); |
| 3765 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | d96e10b | 2019-07-09 17:30:02 +0100 | [diff] [blame] | 3766 | } |
| 3767 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3768 | return 0; |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3769 | } |
| 3770 | |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 3771 | /* |
| 3772 | * Read a record. |
| 3773 | * |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 3774 | * Silently ignore non-fatal alert (and for DTLS, invalid records as well, |
| 3775 | * RFC 6347 4.1.2.7) and continue reading until a valid record is found. |
| 3776 | * |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 3777 | */ |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 3778 | |
| 3779 | /* Helper functions for mbedtls_ssl_read_record(). */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3780 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3781 | static int ssl_consume_current_message(mbedtls_ssl_context *ssl); |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3782 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3783 | static int ssl_get_next_record(mbedtls_ssl_context *ssl); |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3784 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3785 | static int ssl_record_is_in_progress(mbedtls_ssl_context *ssl); |
Hanno Becker | 4162b11 | 2018-08-15 14:05:04 +0100 | [diff] [blame] | 3786 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3787 | int mbedtls_ssl_read_record(mbedtls_ssl_context *ssl, |
| 3788 | unsigned update_hs_digest) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3789 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3790 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3791 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3792 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> read record")); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3793 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3794 | if (ssl->keep_current_message == 0) { |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3795 | do { |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3796 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3797 | ret = ssl_consume_current_message(ssl); |
| 3798 | if (ret != 0) { |
| 3799 | return ret; |
| 3800 | } |
Hanno Becker | 2699459 | 2018-08-15 14:14:59 +0100 | [diff] [blame] | 3801 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3802 | if (ssl_record_is_in_progress(ssl) == 0) { |
David Horstmann | 5846c9d | 2022-10-06 18:31:25 +0100 | [diff] [blame] | 3803 | int dtls_have_buffered = 0; |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3804 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 3805 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3806 | /* We only check for buffered messages if the |
| 3807 | * current datagram is fully consumed. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3808 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3809 | ssl_next_record_is_in_datagram(ssl) == 0) { |
| 3810 | if (ssl_load_buffered_message(ssl) == 0) { |
David Horstmann | 5846c9d | 2022-10-06 18:31:25 +0100 | [diff] [blame] | 3811 | dtls_have_buffered = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3812 | } |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3813 | } |
| 3814 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3815 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3816 | if (dtls_have_buffered == 0) { |
| 3817 | ret = ssl_get_next_record(ssl); |
| 3818 | if (ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING) { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3819 | continue; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3820 | } |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3821 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3822 | if (ret != 0) { |
| 3823 | MBEDTLS_SSL_DEBUG_RET(1, ("ssl_get_next_record"), ret); |
| 3824 | return ret; |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3825 | } |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 3826 | } |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3827 | } |
| 3828 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3829 | ret = mbedtls_ssl_handle_message_type(ssl); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3830 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3831 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3832 | if (ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3833 | /* Buffer future message */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3834 | ret = ssl_buffer_message(ssl); |
| 3835 | if (ret != 0) { |
| 3836 | return ret; |
| 3837 | } |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3838 | |
| 3839 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
| 3840 | } |
| 3841 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3842 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3843 | } while (MBEDTLS_ERR_SSL_NON_FATAL == ret || |
| 3844 | MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3845 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3846 | if (0 != ret) { |
| 3847 | MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_handle_message_type"), ret); |
| 3848 | return ret; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3849 | } |
| 3850 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3851 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3852 | update_hs_digest == 1) { |
| 3853 | mbedtls_ssl_update_handshake_status(ssl); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3854 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3855 | } else { |
| 3856 | MBEDTLS_SSL_DEBUG_MSG(2, ("reuse previously read message")); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3857 | ssl->keep_current_message = 0; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3858 | } |
| 3859 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3860 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= read record")); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3861 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3862 | return 0; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3863 | } |
| 3864 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3865 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3866 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3867 | static int ssl_next_record_is_in_datagram(mbedtls_ssl_context *ssl) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3868 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3869 | if (ssl->in_left > ssl->next_record_offset) { |
| 3870 | return 1; |
| 3871 | } |
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; |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3874 | } |
| 3875 | |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3876 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3877 | static int ssl_load_buffered_message(mbedtls_ssl_context *ssl) |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3878 | { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3879 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3880 | mbedtls_ssl_hs_buffer *hs_buf; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3881 | int ret = 0; |
| 3882 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3883 | if (hs == NULL) { |
| 3884 | return -1; |
| 3885 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3886 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3887 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_load_buffered_message")); |
Hanno Becker | e00ae37 | 2018-08-20 09:39:42 +0100 | [diff] [blame] | 3888 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3889 | if (ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC || |
| 3890 | ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC) { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3891 | /* Check if we have seen a ChangeCipherSpec before. |
| 3892 | * If yes, synthesize a CCS record. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3893 | if (!hs->buffering.seen_ccs) { |
| 3894 | MBEDTLS_SSL_DEBUG_MSG(2, ("CCS not seen in the current flight")); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3895 | ret = -1; |
Hanno Becker | 0d4b376 | 2018-08-20 09:36:59 +0100 | [diff] [blame] | 3896 | goto exit; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3897 | } |
| 3898 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3899 | MBEDTLS_SSL_DEBUG_MSG(2, ("Injecting buffered CCS message")); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3900 | ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; |
| 3901 | ssl->in_msglen = 1; |
| 3902 | ssl->in_msg[0] = 1; |
| 3903 | |
| 3904 | /* As long as they are equal, the exact value doesn't matter. */ |
| 3905 | ssl->in_left = 0; |
| 3906 | ssl->next_record_offset = 0; |
| 3907 | |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 3908 | hs->buffering.seen_ccs = 0; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3909 | goto exit; |
| 3910 | } |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3911 | |
Hanno Becker | b8f5014 | 2018-08-28 10:01:34 +0100 | [diff] [blame] | 3912 | #if defined(MBEDTLS_DEBUG_C) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3913 | /* Debug only */ |
| 3914 | { |
| 3915 | unsigned offset; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3916 | for (offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++) { |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3917 | hs_buf = &hs->buffering.hs[offset]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3918 | if (hs_buf->is_valid == 1) { |
| 3919 | MBEDTLS_SSL_DEBUG_MSG(2, ("Future message with sequence number %u %s buffered.", |
| 3920 | hs->in_msg_seq + offset, |
| 3921 | hs_buf->is_complete ? "fully" : "partially")); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3922 | } |
| 3923 | } |
| 3924 | } |
Hanno Becker | b8f5014 | 2018-08-28 10:01:34 +0100 | [diff] [blame] | 3925 | #endif /* MBEDTLS_DEBUG_C */ |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3926 | |
| 3927 | /* Check if we have buffered and/or fully reassembled the |
| 3928 | * next handshake message. */ |
| 3929 | hs_buf = &hs->buffering.hs[0]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3930 | if ((hs_buf->is_valid == 1) && (hs_buf->is_complete == 1)) { |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3931 | /* Synthesize a record containing the buffered HS message. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3932 | size_t msg_len = (hs_buf->data[1] << 16) | |
| 3933 | (hs_buf->data[2] << 8) | |
| 3934 | hs_buf->data[3]; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3935 | |
| 3936 | /* Double-check that we haven't accidentally buffered |
| 3937 | * a message that doesn't fit into the input buffer. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3938 | if (msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN) { |
| 3939 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 3940 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3941 | } |
| 3942 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3943 | MBEDTLS_SSL_DEBUG_MSG(2, ("Next handshake message has been buffered - load")); |
| 3944 | MBEDTLS_SSL_DEBUG_BUF(3, "Buffered handshake message (incl. header)", |
| 3945 | hs_buf->data, msg_len + 12); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3946 | |
| 3947 | ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3948 | ssl->in_hslen = msg_len + 12; |
| 3949 | ssl->in_msglen = msg_len + 12; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3950 | memcpy(ssl->in_msg, hs_buf->data, ssl->in_hslen); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3951 | |
| 3952 | ret = 0; |
| 3953 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3954 | } else { |
| 3955 | MBEDTLS_SSL_DEBUG_MSG(2, ("Next handshake message %u not or only partially bufffered", |
| 3956 | hs->in_msg_seq)); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3957 | } |
| 3958 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3959 | ret = -1; |
| 3960 | |
| 3961 | exit: |
| 3962 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3963 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_load_buffered_message")); |
| 3964 | return ret; |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3965 | } |
| 3966 | |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 3967 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3968 | static int ssl_buffer_make_space(mbedtls_ssl_context *ssl, |
| 3969 | size_t desired) |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 3970 | { |
| 3971 | int offset; |
| 3972 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3973 | MBEDTLS_SSL_DEBUG_MSG(2, ("Attempt to free buffered messages to have %u bytes available", |
| 3974 | (unsigned) desired)); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 3975 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 3976 | /* Get rid of future records epoch first, if such exist. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3977 | ssl_free_buffered_record(ssl); |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 3978 | |
| 3979 | /* Check if we have enough space available now. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3980 | if (desired <= (MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 3981 | hs->buffering.total_bytes_buffered)) { |
| 3982 | MBEDTLS_SSL_DEBUG_MSG(2, ("Enough space available after freeing future epoch record")); |
| 3983 | return 0; |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 3984 | } |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 3985 | |
Hanno Becker | 4f432ad | 2018-08-28 10:02:32 +0100 | [diff] [blame] | 3986 | /* We don't have enough space to buffer the next expected handshake |
| 3987 | * message. Remove buffers used for future messages to gain space, |
| 3988 | * starting with the most distant one. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3989 | for (offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1; |
| 3990 | offset >= 0; offset--) { |
| 3991 | MBEDTLS_SSL_DEBUG_MSG(2, |
| 3992 | ( |
| 3993 | "Free buffering slot %d to make space for reassembly of next handshake message", |
| 3994 | offset)); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 3995 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3996 | ssl_buffering_free_slot(ssl, (uint8_t) offset); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 3997 | |
| 3998 | /* Check if we have enough space available now. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3999 | if (desired <= (MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 4000 | hs->buffering.total_bytes_buffered)) { |
| 4001 | MBEDTLS_SSL_DEBUG_MSG(2, ("Enough space available after freeing buffered HS messages")); |
| 4002 | return 0; |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 4003 | } |
| 4004 | } |
| 4005 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4006 | return -1; |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 4007 | } |
| 4008 | |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 4009 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4010 | static int ssl_buffer_message(mbedtls_ssl_context *ssl) |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4011 | { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4012 | int ret = 0; |
| 4013 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 4014 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4015 | if (hs == NULL) { |
| 4016 | return 0; |
| 4017 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4018 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4019 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_buffer_message")); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4020 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4021 | switch (ssl->in_msgtype) { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4022 | case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4023 | MBEDTLS_SSL_DEBUG_MSG(2, ("Remember CCS message")); |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 4024 | |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 4025 | hs->buffering.seen_ccs = 1; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4026 | break; |
| 4027 | |
| 4028 | case MBEDTLS_SSL_MSG_HANDSHAKE: |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4029 | { |
| 4030 | unsigned recv_msg_seq_offset; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4031 | 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] | 4032 | mbedtls_ssl_hs_buffer *hs_buf; |
| 4033 | size_t msg_len = ssl->in_hslen - 12; |
| 4034 | |
| 4035 | /* We should never receive an old handshake |
| 4036 | * message - double-check nonetheless. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4037 | if (recv_msg_seq < ssl->handshake->in_msg_seq) { |
| 4038 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 4039 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4040 | } |
| 4041 | |
| 4042 | recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4043 | if (recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS) { |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4044 | /* Silently ignore -- message too far in the future */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4045 | MBEDTLS_SSL_DEBUG_MSG(2, |
| 4046 | ("Ignore future HS message with sequence number %u, " |
| 4047 | "buffering window %u - %u", |
| 4048 | recv_msg_seq, ssl->handshake->in_msg_seq, |
| 4049 | ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - |
| 4050 | 1)); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4051 | |
| 4052 | goto exit; |
| 4053 | } |
| 4054 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4055 | MBEDTLS_SSL_DEBUG_MSG(2, ("Buffering HS message with sequence number %u, offset %u ", |
| 4056 | recv_msg_seq, recv_msg_seq_offset)); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4057 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4058 | hs_buf = &hs->buffering.hs[recv_msg_seq_offset]; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4059 | |
| 4060 | /* Check if the buffering for this seq nr has already commenced. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4061 | if (!hs_buf->is_valid) { |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 4062 | size_t reassembly_buf_sz; |
| 4063 | |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4064 | hs_buf->is_fragmented = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4065 | (ssl_hs_is_proper_fragment(ssl) == 1); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4066 | |
| 4067 | /* We copy the message back into the input buffer |
| 4068 | * after reassembly, so check that it's not too large. |
| 4069 | * This is an implementation-specific limitation |
| 4070 | * and not one from the standard, hence it is not |
| 4071 | * checked in ssl_check_hs_header(). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4072 | if (msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN) { |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4073 | /* Ignore message */ |
| 4074 | goto exit; |
| 4075 | } |
| 4076 | |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4077 | /* Check if we have enough space to buffer the message. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4078 | if (hs->buffering.total_bytes_buffered > |
| 4079 | MBEDTLS_SSL_DTLS_MAX_BUFFERING) { |
| 4080 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 4081 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4082 | } |
| 4083 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4084 | reassembly_buf_sz = ssl_get_reassembly_buffer_size(msg_len, |
| 4085 | hs_buf->is_fragmented); |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4086 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4087 | if (reassembly_buf_sz > (MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 4088 | hs->buffering.total_bytes_buffered)) { |
| 4089 | if (recv_msg_seq_offset > 0) { |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4090 | /* If we can't buffer a future message because |
| 4091 | * of space limitations -- ignore. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4092 | MBEDTLS_SSL_DEBUG_MSG(2, |
| 4093 | ("Buffering of future message of size %" |
| 4094 | MBEDTLS_PRINTF_SIZET |
| 4095 | " would exceed the compile-time limit %" |
| 4096 | MBEDTLS_PRINTF_SIZET |
| 4097 | " (already %" MBEDTLS_PRINTF_SIZET |
| 4098 | " bytes buffered) -- ignore\n", |
| 4099 | msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 4100 | hs->buffering.total_bytes_buffered)); |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4101 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4102 | } else { |
| 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) -- attempt to make space by freeing buffered future messages\n", |
| 4110 | msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 4111 | hs->buffering.total_bytes_buffered)); |
Hanno Becker | e180139 | 2018-08-21 16:51:05 +0100 | [diff] [blame] | 4112 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4113 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4114 | if (ssl_buffer_make_space(ssl, reassembly_buf_sz) != 0) { |
| 4115 | MBEDTLS_SSL_DEBUG_MSG(2, |
| 4116 | ("Reassembly of next message of size %" |
| 4117 | MBEDTLS_PRINTF_SIZET |
| 4118 | " (%" MBEDTLS_PRINTF_SIZET |
| 4119 | " with bitmap) would exceed" |
| 4120 | " the compile-time limit %" |
| 4121 | MBEDTLS_PRINTF_SIZET |
| 4122 | " (already %" MBEDTLS_PRINTF_SIZET |
| 4123 | " bytes buffered) -- fail\n", |
| 4124 | msg_len, |
| 4125 | reassembly_buf_sz, |
| 4126 | (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 4127 | hs->buffering.total_bytes_buffered)); |
Hanno Becker | 55e9e2a | 2018-08-21 16:07:55 +0100 | [diff] [blame] | 4128 | ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 4129 | goto exit; |
| 4130 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4131 | } |
| 4132 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4133 | MBEDTLS_SSL_DEBUG_MSG(2, |
| 4134 | ("initialize reassembly, total length = %" |
| 4135 | MBEDTLS_PRINTF_SIZET, |
| 4136 | msg_len)); |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4137 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4138 | hs_buf->data = mbedtls_calloc(1, reassembly_buf_sz); |
| 4139 | if (hs_buf->data == NULL) { |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4140 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4141 | goto exit; |
| 4142 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4143 | hs_buf->data_len = reassembly_buf_sz; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4144 | |
| 4145 | /* Prepare final header: copy msg_type, length and message_seq, |
| 4146 | * then add standardised fragment_offset and fragment_length */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4147 | memcpy(hs_buf->data, ssl->in_msg, 6); |
| 4148 | memset(hs_buf->data + 6, 0, 3); |
| 4149 | memcpy(hs_buf->data + 9, hs_buf->data + 1, 3); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4150 | |
| 4151 | hs_buf->is_valid = 1; |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4152 | |
| 4153 | hs->buffering.total_bytes_buffered += reassembly_buf_sz; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4154 | } else { |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4155 | /* Make sure msg_type and length are consistent */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4156 | if (memcmp(hs_buf->data, ssl->in_msg, 4) != 0) { |
| 4157 | MBEDTLS_SSL_DEBUG_MSG(1, ("Fragment header mismatch - ignore")); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4158 | /* Ignore */ |
| 4159 | goto exit; |
| 4160 | } |
| 4161 | } |
| 4162 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4163 | if (!hs_buf->is_complete) { |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4164 | size_t frag_len, frag_off; |
| 4165 | unsigned char * const msg = hs_buf->data + 12; |
| 4166 | |
| 4167 | /* |
| 4168 | * Check and copy current fragment |
| 4169 | */ |
| 4170 | |
| 4171 | /* Validation of header fields already done in |
| 4172 | * mbedtls_ssl_prepare_handshake_record(). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4173 | frag_off = ssl_get_hs_frag_off(ssl); |
| 4174 | frag_len = ssl_get_hs_frag_len(ssl); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4175 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4176 | MBEDTLS_SSL_DEBUG_MSG(2, ("adding fragment, offset = %" MBEDTLS_PRINTF_SIZET |
| 4177 | ", length = %" MBEDTLS_PRINTF_SIZET, |
| 4178 | frag_off, frag_len)); |
| 4179 | memcpy(msg + frag_off, ssl->in_msg + 12, frag_len); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4180 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4181 | if (hs_buf->is_fragmented) { |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4182 | unsigned char * const bitmask = msg + msg_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4183 | ssl_bitmask_set(bitmask, frag_off, frag_len); |
| 4184 | hs_buf->is_complete = (ssl_bitmask_check(bitmask, |
| 4185 | msg_len) == 0); |
| 4186 | } else { |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4187 | hs_buf->is_complete = 1; |
| 4188 | } |
| 4189 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4190 | MBEDTLS_SSL_DEBUG_MSG(2, ("message %scomplete", |
| 4191 | hs_buf->is_complete ? "" : "not yet ")); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4192 | } |
| 4193 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4194 | break; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4195 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4196 | |
| 4197 | default: |
Hanno Becker | 360bef3 | 2018-08-28 10:04:33 +0100 | [diff] [blame] | 4198 | /* We don't buffer other types of messages. */ |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4199 | break; |
| 4200 | } |
| 4201 | |
| 4202 | exit: |
| 4203 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4204 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_buffer_message")); |
| 4205 | return ret; |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4206 | } |
| 4207 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4208 | |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 4209 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4210 | static int ssl_consume_current_message(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4211 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4212 | /* |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4213 | * Consume last content-layer message and potentially |
| 4214 | * update in_msglen which keeps track of the contents' |
| 4215 | * consumption state. |
| 4216 | * |
| 4217 | * (1) Handshake messages: |
| 4218 | * Remove last handshake message, move content |
| 4219 | * and adapt in_msglen. |
| 4220 | * |
| 4221 | * (2) Alert messages: |
| 4222 | * Consume whole record content, in_msglen = 0. |
| 4223 | * |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4224 | * (3) Change cipher spec: |
| 4225 | * Consume whole record content, in_msglen = 0. |
| 4226 | * |
| 4227 | * (4) Application data: |
| 4228 | * Don't do anything - the record layer provides |
| 4229 | * the application data as a stream transport |
| 4230 | * and consumes through mbedtls_ssl_read only. |
| 4231 | * |
| 4232 | */ |
| 4233 | |
| 4234 | /* Case (1): Handshake messages */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4235 | if (ssl->in_hslen != 0) { |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 4236 | /* Hard assertion to be sure that no application data |
| 4237 | * is in flight, as corrupting ssl->in_msglen during |
| 4238 | * ssl->in_offt != NULL is fatal. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4239 | if (ssl->in_offt != NULL) { |
| 4240 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 4241 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 4242 | } |
| 4243 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4244 | /* |
| 4245 | * Get next Handshake message in the current record |
| 4246 | */ |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4247 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4248 | /* Notes: |
Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 4249 | * (1) in_hslen is not necessarily the size of the |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4250 | * current handshake content: If DTLS handshake |
| 4251 | * fragmentation is used, that's the fragment |
| 4252 | * size instead. Using the total handshake message |
Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 4253 | * size here is faulty and should be changed at |
| 4254 | * some point. |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4255 | * (2) While it doesn't seem to cause problems, one |
| 4256 | * has to be very careful not to assume that in_hslen |
| 4257 | * is always <= in_msglen in a sensible communication. |
| 4258 | * Again, it's wrong for DTLS handshake fragmentation. |
| 4259 | * The following check is therefore mandatory, and |
| 4260 | * should not be treated as a silently corrected assertion. |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 4261 | * Additionally, ssl->in_hslen might be arbitrarily out of |
| 4262 | * bounds after handling a DTLS message with an unexpected |
| 4263 | * sequence number, see mbedtls_ssl_prepare_handshake_record. |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4264 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4265 | if (ssl->in_hslen < ssl->in_msglen) { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4266 | ssl->in_msglen -= ssl->in_hslen; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4267 | memmove(ssl->in_msg, ssl->in_msg + ssl->in_hslen, |
| 4268 | ssl->in_msglen); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4269 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4270 | MBEDTLS_SSL_DEBUG_BUF(4, "remaining content in record", |
| 4271 | ssl->in_msg, ssl->in_msglen); |
| 4272 | } else { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4273 | ssl->in_msglen = 0; |
| 4274 | } |
Manuel Pégourié-Gonnard | 4a17536 | 2014-09-09 17:45:31 +0200 | [diff] [blame] | 4275 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4276 | ssl->in_hslen = 0; |
| 4277 | } |
| 4278 | /* Case (4): Application data */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4279 | else if (ssl->in_offt != NULL) { |
| 4280 | return 0; |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4281 | } |
| 4282 | /* Everything else (CCS & Alerts) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4283 | else { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4284 | ssl->in_msglen = 0; |
| 4285 | } |
| 4286 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4287 | return 0; |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 4288 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4289 | |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 4290 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4291 | static int ssl_record_is_in_progress(mbedtls_ssl_context *ssl) |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4292 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4293 | if (ssl->in_msglen > 0) { |
| 4294 | return 1; |
| 4295 | } |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4296 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4297 | return 0; |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4298 | } |
| 4299 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4300 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4301 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4302 | static void ssl_free_buffered_record(mbedtls_ssl_context *ssl) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4303 | { |
| 4304 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4305 | if (hs == NULL) { |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4306 | return; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4307 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4308 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4309 | if (hs->buffering.future_record.data != NULL) { |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 4310 | hs->buffering.total_bytes_buffered -= |
| 4311 | hs->buffering.future_record.len; |
| 4312 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4313 | mbedtls_free(hs->buffering.future_record.data); |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 4314 | hs->buffering.future_record.data = NULL; |
| 4315 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4316 | } |
| 4317 | |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 4318 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4319 | static int ssl_load_buffered_record(mbedtls_ssl_context *ssl) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4320 | { |
| 4321 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4322 | unsigned char *rec; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4323 | size_t rec_len; |
| 4324 | unsigned rec_epoch; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 4325 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4326 | size_t in_buf_len = ssl->in_buf_len; |
| 4327 | #else |
| 4328 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 4329 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4330 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 4331 | return 0; |
| 4332 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4333 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4334 | if (hs == NULL) { |
| 4335 | return 0; |
| 4336 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4337 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4338 | rec = hs->buffering.future_record.data; |
| 4339 | rec_len = hs->buffering.future_record.len; |
| 4340 | rec_epoch = hs->buffering.future_record.epoch; |
| 4341 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4342 | if (rec == NULL) { |
| 4343 | return 0; |
| 4344 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4345 | |
Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 4346 | /* Only consider loading future records if the |
| 4347 | * input buffer is empty. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4348 | if (ssl_next_record_is_in_datagram(ssl) == 1) { |
| 4349 | return 0; |
| 4350 | } |
Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 4351 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4352 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_load_buffered_record")); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4353 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4354 | if (rec_epoch != ssl->in_epoch) { |
| 4355 | MBEDTLS_SSL_DEBUG_MSG(2, ("Buffered record not from current epoch.")); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4356 | goto exit; |
| 4357 | } |
| 4358 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4359 | MBEDTLS_SSL_DEBUG_MSG(2, ("Found buffered record from current epoch - load")); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4360 | |
| 4361 | /* Double-check that the record is not too large */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4362 | if (rec_len > in_buf_len - (size_t) (ssl->in_hdr - ssl->in_buf)) { |
| 4363 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 4364 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4365 | } |
| 4366 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4367 | memcpy(ssl->in_hdr, rec, rec_len); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4368 | ssl->in_left = rec_len; |
| 4369 | ssl->next_record_offset = 0; |
| 4370 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4371 | ssl_free_buffered_record(ssl); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4372 | |
| 4373 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4374 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_load_buffered_record")); |
| 4375 | return 0; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4376 | } |
| 4377 | |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 4378 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4379 | static int ssl_buffer_future_record(mbedtls_ssl_context *ssl, |
| 4380 | mbedtls_record const *rec) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4381 | { |
| 4382 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4383 | |
| 4384 | /* Don't buffer future records outside handshakes. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4385 | if (hs == NULL) { |
| 4386 | return 0; |
| 4387 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4388 | |
| 4389 | /* Only buffer handshake records (we are only interested |
| 4390 | * in Finished messages). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4391 | if (rec->type != MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 4392 | return 0; |
| 4393 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4394 | |
| 4395 | /* Don't buffer more than one future epoch record. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4396 | if (hs->buffering.future_record.data != NULL) { |
| 4397 | return 0; |
| 4398 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4399 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 4400 | /* Don't buffer record if there's not enough buffering space remaining. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4401 | if (rec->buf_len > (MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 4402 | hs->buffering.total_bytes_buffered)) { |
| 4403 | MBEDTLS_SSL_DEBUG_MSG(2, ("Buffering of future epoch record of size %" MBEDTLS_PRINTF_SIZET |
| 4404 | " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET |
| 4405 | " (already %" MBEDTLS_PRINTF_SIZET |
| 4406 | " bytes buffered) -- ignore\n", |
| 4407 | rec->buf_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 4408 | hs->buffering.total_bytes_buffered)); |
| 4409 | return 0; |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4410 | } |
| 4411 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4412 | /* Buffer record */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4413 | MBEDTLS_SSL_DEBUG_MSG(2, ("Buffer record from epoch %u", |
| 4414 | ssl->in_epoch + 1U)); |
| 4415 | MBEDTLS_SSL_DEBUG_BUF(3, "Buffered record", rec->buf, rec->buf_len); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4416 | |
| 4417 | /* ssl_parse_record_header() only considers records |
| 4418 | * of the next epoch as candidates for buffering. */ |
| 4419 | hs->buffering.future_record.epoch = ssl->in_epoch + 1; |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 4420 | hs->buffering.future_record.len = rec->buf_len; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4421 | |
| 4422 | hs->buffering.future_record.data = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4423 | mbedtls_calloc(1, hs->buffering.future_record.len); |
| 4424 | if (hs->buffering.future_record.data == NULL) { |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4425 | /* If we run out of RAM trying to buffer a |
| 4426 | * record from the next epoch, just ignore. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4427 | return 0; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4428 | } |
| 4429 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4430 | memcpy(hs->buffering.future_record.data, rec->buf, rec->buf_len); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4431 | |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 4432 | hs->buffering.total_bytes_buffered += rec->buf_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4433 | return 0; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4434 | } |
| 4435 | |
| 4436 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4437 | |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 4438 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4439 | static int ssl_get_next_record(mbedtls_ssl_context *ssl) |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 4440 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4441 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 4442 | mbedtls_record rec; |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 4443 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4444 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4445 | /* We might have buffered a future record; if so, |
| 4446 | * and if the epoch matches now, load it. |
| 4447 | * On success, this call will set ssl->in_left to |
| 4448 | * the length of the buffered record, so that |
| 4449 | * the calls to ssl_fetch_input() below will |
| 4450 | * essentially be no-ops. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4451 | ret = ssl_load_buffered_record(ssl); |
| 4452 | if (ret != 0) { |
| 4453 | return ret; |
| 4454 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4455 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4456 | |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4457 | /* Ensure that we have enough space available for the default form |
| 4458 | * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS, |
| 4459 | * with no space for CIDs counted in). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4460 | ret = mbedtls_ssl_fetch_input(ssl, mbedtls_ssl_in_hdr_len(ssl)); |
| 4461 | if (ret != 0) { |
| 4462 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret); |
| 4463 | return ret; |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4464 | } |
| 4465 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4466 | ret = ssl_parse_record_header(ssl, ssl->in_hdr, ssl->in_left, &rec); |
| 4467 | if (ret != 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4468 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4469 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 4470 | if (ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) { |
| 4471 | ret = ssl_buffer_future_record(ssl, &rec); |
| 4472 | if (ret != 0) { |
| 4473 | return ret; |
| 4474 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4475 | |
| 4476 | /* Fall through to handling of unexpected records */ |
| 4477 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
| 4478 | } |
| 4479 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4480 | if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD) { |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 4481 | #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] | 4482 | /* Reset in pointers to default state for TLS/DTLS records, |
| 4483 | * assuming no CID and no offset between record content and |
| 4484 | * record plaintext. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4485 | mbedtls_ssl_update_in_pointers(ssl); |
Hanno Becker | d8bf8ce | 2019-07-12 09:23:47 +0100 | [diff] [blame] | 4486 | |
Hanno Becker | 7ae20e0 | 2019-07-12 08:33:49 +0100 | [diff] [blame] | 4487 | /* Setup internal message pointers from record structure. */ |
| 4488 | ssl->in_msgtype = rec.type; |
| 4489 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4490 | ssl->in_len = ssl->in_cid + rec.cid_len; |
| 4491 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4492 | ssl->in_iv = ssl->in_msg = ssl->in_len + 2; |
| 4493 | ssl->in_msglen = rec.data_len; |
| 4494 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4495 | ret = ssl_check_client_reconnect(ssl); |
| 4496 | MBEDTLS_SSL_DEBUG_RET(2, "ssl_check_client_reconnect", ret); |
| 4497 | if (ret != 0) { |
| 4498 | return ret; |
| 4499 | } |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 4500 | #endif |
| 4501 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4502 | /* Skip unexpected record (but not whole datagram) */ |
Hanno Becker | 4acada3 | 2019-07-11 12:48:53 +0100 | [diff] [blame] | 4503 | ssl->next_record_offset = rec.buf_len; |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4504 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4505 | MBEDTLS_SSL_DEBUG_MSG(1, ("discarding unexpected record " |
| 4506 | "(header)")); |
| 4507 | } else { |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4508 | /* Skip invalid record and the rest of the datagram */ |
| 4509 | ssl->next_record_offset = 0; |
| 4510 | ssl->in_left = 0; |
| 4511 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4512 | MBEDTLS_SSL_DEBUG_MSG(1, ("discarding invalid record " |
| 4513 | "(header)")); |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4514 | } |
| 4515 | |
| 4516 | /* Get next record */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4517 | return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
| 4518 | } else |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4519 | #endif |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 4520 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4521 | return ret; |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 4522 | } |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4523 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4524 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4525 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4526 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 4527 | /* Remember offset of next record within datagram. */ |
Hanno Becker | f50da50 | 2019-07-11 12:50:10 +0100 | [diff] [blame] | 4528 | ssl->next_record_offset = rec.buf_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4529 | if (ssl->next_record_offset < ssl->in_left) { |
| 4530 | MBEDTLS_SSL_DEBUG_MSG(3, ("more than one record within datagram")); |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 4531 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4532 | } else |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4533 | #endif |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 4534 | { |
Hanno Becker | 955a5c9 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 4535 | /* |
| 4536 | * Fetch record contents from underlying transport. |
| 4537 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4538 | ret = mbedtls_ssl_fetch_input(ssl, rec.buf_len); |
| 4539 | if (ret != 0) { |
| 4540 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret); |
| 4541 | return ret; |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 4542 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4543 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4544 | ssl->in_left = 0; |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 4545 | } |
| 4546 | |
| 4547 | /* |
| 4548 | * Decrypt record contents. |
| 4549 | */ |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4550 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4551 | if ((ret = ssl_prepare_record_content(ssl, &rec)) != 0) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4552 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4553 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4554 | /* Silently discard invalid records */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4555 | if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) { |
Manuel Pégourié-Gonnard | 0a88574 | 2015-08-04 12:08:35 +0200 | [diff] [blame] | 4556 | /* Except when waiting for Finished as a bad mac here |
| 4557 | * probably means something went wrong in the handshake |
| 4558 | * (eg wrong psk used, mitm downgrade attempt, etc.) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4559 | if (ssl->state == MBEDTLS_SSL_CLIENT_FINISHED || |
| 4560 | ssl->state == MBEDTLS_SSL_SERVER_FINISHED) { |
Manuel Pégourié-Gonnard | 0a88574 | 2015-08-04 12:08:35 +0200 | [diff] [blame] | 4561 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4562 | if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) { |
| 4563 | mbedtls_ssl_send_alert_message(ssl, |
| 4564 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 4565 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC); |
Manuel Pégourié-Gonnard | 0a88574 | 2015-08-04 12:08:35 +0200 | [diff] [blame] | 4566 | } |
| 4567 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4568 | return ret; |
Manuel Pégourié-Gonnard | 0a88574 | 2015-08-04 12:08:35 +0200 | [diff] [blame] | 4569 | } |
| 4570 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4571 | #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4572 | if (ssl->conf->badmac_limit != 0 && |
| 4573 | ++ssl->badmac_seen >= ssl->conf->badmac_limit) { |
| 4574 | MBEDTLS_SSL_DEBUG_MSG(1, ("too many records with bad MAC")); |
| 4575 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 4576 | } |
| 4577 | #endif |
| 4578 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4579 | /* As above, invalid records cause |
| 4580 | * dismissal of the whole datagram. */ |
| 4581 | |
| 4582 | ssl->next_record_offset = 0; |
| 4583 | ssl->in_left = 0; |
| 4584 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4585 | MBEDTLS_SSL_DEBUG_MSG(1, ("discarding invalid record (mac)")); |
| 4586 | return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4587 | } |
| 4588 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4589 | return ret; |
| 4590 | } else |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4591 | #endif |
| 4592 | { |
| 4593 | /* Error out (and send alert) on invalid records */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4594 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4595 | if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) { |
| 4596 | mbedtls_ssl_send_alert_message(ssl, |
| 4597 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 4598 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4599 | } |
| 4600 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4601 | return ret; |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4602 | } |
| 4603 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4604 | |
Hanno Becker | 44d89b2 | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 4605 | |
| 4606 | /* Reset in pointers to default state for TLS/DTLS records, |
| 4607 | * assuming no CID and no offset between record content and |
| 4608 | * record plaintext. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4609 | mbedtls_ssl_update_in_pointers(ssl); |
Hanno Becker | 44d89b2 | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 4610 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4611 | ssl->in_len = ssl->in_cid + rec.cid_len; |
| 4612 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
irwir | 89af51f | 2019-09-26 21:04:56 +0300 | [diff] [blame] | 4613 | ssl->in_iv = ssl->in_len + 2; |
Hanno Becker | 44d89b2 | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 4614 | |
Hanno Becker | 8685c82 | 2019-07-12 09:37:30 +0100 | [diff] [blame] | 4615 | /* The record content type may change during decryption, |
| 4616 | * so re-read it. */ |
| 4617 | ssl->in_msgtype = rec.type; |
| 4618 | /* Also update the input buffer, because unfortunately |
| 4619 | * the server-side ssl_parse_client_hello() reparses the |
| 4620 | * record header when receiving a ClientHello initiating |
| 4621 | * a renegotiation. */ |
| 4622 | ssl->in_hdr[0] = rec.type; |
| 4623 | ssl->in_msg = rec.buf + rec.data_offset; |
| 4624 | ssl->in_msglen = rec.data_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4625 | MBEDTLS_PUT_UINT16_BE(rec.data_len, ssl->in_len, 0); |
Hanno Becker | 8685c82 | 2019-07-12 09:37:30 +0100 | [diff] [blame] | 4626 | |
Manuel Pégourié-Gonnard | c40b685 | 2020-01-03 12:18:49 +0100 | [diff] [blame] | 4627 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4628 | if (ssl->transform_in != NULL && |
| 4629 | ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE) { |
| 4630 | if ((ret = ssl_decompress_buf(ssl)) != 0) { |
| 4631 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_decompress_buf", ret); |
| 4632 | return ret; |
Manuel Pégourié-Gonnard | c40b685 | 2020-01-03 12:18:49 +0100 | [diff] [blame] | 4633 | } |
| 4634 | |
| 4635 | /* Check actual (decompress) record content length against |
| 4636 | * configured maximum. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4637 | if (ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN) { |
| 4638 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad message length")); |
| 4639 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Manuel Pégourié-Gonnard | c40b685 | 2020-01-03 12:18:49 +0100 | [diff] [blame] | 4640 | } |
| 4641 | } |
| 4642 | #endif /* MBEDTLS_ZLIB_SUPPORT */ |
| 4643 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4644 | return 0; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4645 | } |
| 4646 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4647 | int mbedtls_ssl_handle_message_type(mbedtls_ssl_context *ssl) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4648 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4649 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4650 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 4651 | /* |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4652 | * Handle particular types of records |
| 4653 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4654 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 4655 | if ((ret = mbedtls_ssl_prepare_handshake_record(ssl)) != 0) { |
| 4656 | return ret; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4657 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4658 | } |
| 4659 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4660 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) { |
| 4661 | if (ssl->in_msglen != 1) { |
| 4662 | MBEDTLS_SSL_DEBUG_MSG(1, ("invalid CCS message, len: %" MBEDTLS_PRINTF_SIZET, |
| 4663 | ssl->in_msglen)); |
| 4664 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4665 | } |
| 4666 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4667 | if (ssl->in_msg[0] != 1) { |
| 4668 | MBEDTLS_SSL_DEBUG_MSG(1, ("invalid CCS message, content: %02x", |
| 4669 | ssl->in_msg[0])); |
| 4670 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 4671 | } |
| 4672 | |
| 4673 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4674 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 4675 | ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC && |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4676 | ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC) { |
| 4677 | if (ssl->handshake == NULL) { |
| 4678 | MBEDTLS_SSL_DEBUG_MSG(1, ("dropping ChangeCipherSpec outside handshake")); |
| 4679 | return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 4680 | } |
| 4681 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4682 | MBEDTLS_SSL_DEBUG_MSG(1, ("received out-of-order ChangeCipherSpec - remember")); |
| 4683 | return MBEDTLS_ERR_SSL_EARLY_MESSAGE; |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 4684 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4685 | #endif |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 4686 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4687 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4688 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT) { |
| 4689 | if (ssl->in_msglen != 2) { |
Angus Gratton | 1a7a17e | 2018-06-20 15:43:50 +1000 | [diff] [blame] | 4690 | /* Note: Standard allows for more than one 2 byte alert |
| 4691 | to be packed in a single message, but Mbed TLS doesn't |
| 4692 | currently support this. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4693 | MBEDTLS_SSL_DEBUG_MSG(1, ("invalid alert message, len: %" MBEDTLS_PRINTF_SIZET, |
| 4694 | ssl->in_msglen)); |
| 4695 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Angus Gratton | 1a7a17e | 2018-06-20 15:43:50 +1000 | [diff] [blame] | 4696 | } |
| 4697 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4698 | MBEDTLS_SSL_DEBUG_MSG(2, ("got an alert message, type: [%u:%u]", |
| 4699 | ssl->in_msg[0], ssl->in_msg[1])); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4700 | |
| 4701 | /* |
Simon Butcher | 459a950 | 2015-10-27 16:09:03 +0000 | [diff] [blame] | 4702 | * Ignore non-fatal alerts, except close_notify and no_renegotiation |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4703 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4704 | if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL) { |
| 4705 | MBEDTLS_SSL_DEBUG_MSG(1, ("is a fatal alert message (msg %d)", |
| 4706 | ssl->in_msg[1])); |
| 4707 | return MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4708 | } |
| 4709 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4710 | if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 4711 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY) { |
| 4712 | MBEDTLS_SSL_DEBUG_MSG(2, ("is a close notify message")); |
| 4713 | return MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4714 | } |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 4715 | |
| 4716 | #if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4717 | if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 4718 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION) { |
| 4719 | 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] | 4720 | /* Will be handled when trying to parse ServerHello */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4721 | return 0; |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 4722 | } |
| 4723 | #endif |
| 4724 | |
| 4725 | #if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4726 | if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 && |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 4727 | ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 4728 | ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4729 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT) { |
| 4730 | MBEDTLS_SSL_DEBUG_MSG(2, ("is a SSLv3 no_cert")); |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 4731 | /* Will be handled in mbedtls_ssl_parse_certificate() */ |
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 /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */ |
| 4735 | |
| 4736 | /* Silently ignore: fetch new message */ |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4737 | return MBEDTLS_ERR_SSL_NON_FATAL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4738 | } |
| 4739 | |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 4740 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4741 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 4742 | /* Drop unexpected ApplicationData records, |
| 4743 | * except at the beginning of renegotiations */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4744 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA && |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 4745 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER |
| 4746 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4747 | && !(ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 4748 | ssl->state == MBEDTLS_SSL_SERVER_HELLO) |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 4749 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4750 | ) { |
| 4751 | MBEDTLS_SSL_DEBUG_MSG(1, ("dropping unexpected ApplicationData")); |
| 4752 | return MBEDTLS_ERR_SSL_NON_FATAL; |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 4753 | } |
| 4754 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4755 | if (ssl->handshake != NULL && |
| 4756 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 4757 | mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl); |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 4758 | } |
| 4759 | } |
Hanno Becker | 4a4af9f | 2019-05-08 16:26:21 +0100 | [diff] [blame] | 4760 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 4761 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4762 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4763 | } |
| 4764 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4765 | int mbedtls_ssl_send_fatal_handshake_failure(mbedtls_ssl_context *ssl) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 4766 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4767 | return mbedtls_ssl_send_alert_message(ssl, |
| 4768 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 4769 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 4770 | } |
| 4771 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4772 | int mbedtls_ssl_send_alert_message(mbedtls_ssl_context *ssl, |
| 4773 | unsigned char level, |
| 4774 | unsigned char message) |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 4775 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4776 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 4777 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4778 | if (ssl == NULL || ssl->conf == NULL) { |
| 4779 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 4780 | } |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 4781 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4782 | if (ssl->out_left != 0) { |
| 4783 | return mbedtls_ssl_flush_output(ssl); |
| 4784 | } |
Hanno Becker | d9c66c0 | 2018-08-06 11:35:16 +0100 | [diff] [blame] | 4785 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4786 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> send alert message")); |
| 4787 | 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] | 4788 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4789 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 4790 | ssl->out_msglen = 2; |
| 4791 | ssl->out_msg[0] = level; |
| 4792 | ssl->out_msg[1] = message; |
| 4793 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4794 | if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) { |
| 4795 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret); |
| 4796 | return ret; |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 4797 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4798 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= send alert message")); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 4799 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4800 | return 0; |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 4801 | } |
| 4802 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4803 | int mbedtls_ssl_write_change_cipher_spec(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4804 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4805 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4806 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4807 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write change cipher spec")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4808 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4809 | ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4810 | ssl->out_msglen = 1; |
| 4811 | ssl->out_msg[0] = 1; |
| 4812 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4813 | ssl->state++; |
| 4814 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4815 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { |
| 4816 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); |
| 4817 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4818 | } |
| 4819 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4820 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write change cipher spec")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4821 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4822 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4823 | } |
| 4824 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4825 | int mbedtls_ssl_parse_change_cipher_spec(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4826 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4827 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4828 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4829 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse change cipher spec")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4830 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4831 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
| 4832 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
| 4833 | return ret; |
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 | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) { |
| 4837 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad change cipher spec message")); |
| 4838 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 4839 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
| 4840 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4841 | } |
| 4842 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 4843 | /* CCS records are only accepted if they have length 1 and content '1', |
| 4844 | * so we don't need to check this here. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4845 | |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4846 | /* |
| 4847 | * Switch to our negotiated transform and session parameters for inbound |
| 4848 | * data. |
| 4849 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4850 | 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] | 4851 | ssl->transform_in = ssl->transform_negotiate; |
| 4852 | ssl->session_in = ssl->session_negotiate; |
| 4853 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4854 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4855 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4856 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4857 | mbedtls_ssl_dtls_replay_reset(ssl); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4858 | #endif |
| 4859 | |
| 4860 | /* Increment epoch */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4861 | if (++ssl->in_epoch == 0) { |
| 4862 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap")); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 4863 | /* This is highly unlikely to happen for legitimate reasons, so |
| 4864 | treat it as an attack and don't send an alert. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4865 | return MBEDTLS_ERR_SSL_COUNTER_WRAPPING; |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4866 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4867 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4868 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4869 | memset(ssl->in_ctr, 0, 8); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4870 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4871 | mbedtls_ssl_update_in_pointers(ssl); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4872 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4873 | #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4874 | if (mbedtls_ssl_hw_record_activate != NULL) { |
| 4875 | if ((ret = mbedtls_ssl_hw_record_activate(ssl, MBEDTLS_SSL_CHANNEL_INBOUND)) != 0) { |
| 4876 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_hw_record_activate", ret); |
| 4877 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 4878 | MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR); |
| 4879 | return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED; |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4880 | } |
| 4881 | } |
| 4882 | #endif |
| 4883 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4884 | ssl->state++; |
| 4885 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4886 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse change cipher spec")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4887 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4888 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4889 | } |
| 4890 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4891 | /* Once ssl->out_hdr as the address of the beginning of the |
| 4892 | * next outgoing record is set, deduce the other pointers. |
| 4893 | * |
| 4894 | * Note: For TLS, we save the implicit record sequence number |
| 4895 | * (entering MAC computation) in the 8 bytes before ssl->out_hdr, |
| 4896 | * and the caller has to make sure there's space for this. |
| 4897 | */ |
| 4898 | |
Hanno Becker | c0eefa8 | 2020-05-28 07:17:36 +0100 | [diff] [blame] | 4899 | static size_t ssl_transform_get_explicit_iv_len( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4900 | mbedtls_ssl_transform const *transform) |
Hanno Becker | c0eefa8 | 2020-05-28 07:17:36 +0100 | [diff] [blame] | 4901 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4902 | if (transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2) { |
| 4903 | return 0; |
| 4904 | } |
Hanno Becker | c0eefa8 | 2020-05-28 07:17:36 +0100 | [diff] [blame] | 4905 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4906 | return transform->ivlen - transform->fixed_ivlen; |
Hanno Becker | c0eefa8 | 2020-05-28 07:17:36 +0100 | [diff] [blame] | 4907 | } |
| 4908 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4909 | void mbedtls_ssl_update_out_pointers(mbedtls_ssl_context *ssl, |
| 4910 | mbedtls_ssl_transform *transform) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4911 | { |
| 4912 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4913 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4914 | ssl->out_ctr = ssl->out_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4915 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4916 | ssl->out_cid = ssl->out_ctr + 8; |
| 4917 | ssl->out_len = ssl->out_cid; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4918 | if (transform != NULL) { |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4919 | ssl->out_len += transform->out_cid_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4920 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4921 | #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4922 | ssl->out_len = ssl->out_ctr + 8; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4923 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4924 | ssl->out_iv = ssl->out_len + 2; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4925 | } else |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4926 | #endif |
| 4927 | { |
| 4928 | ssl->out_ctr = ssl->out_hdr - 8; |
| 4929 | ssl->out_len = ssl->out_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4930 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 4c3eb7c | 2019-05-08 16:43:21 +0100 | [diff] [blame] | 4931 | ssl->out_cid = ssl->out_len; |
| 4932 | #endif |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4933 | ssl->out_iv = ssl->out_hdr + 5; |
| 4934 | } |
| 4935 | |
Hanno Becker | c0eefa8 | 2020-05-28 07:17:36 +0100 | [diff] [blame] | 4936 | ssl->out_msg = ssl->out_iv; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4937 | /* Adjust out_msg to make space for explicit IV, if used. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4938 | if (transform != NULL) { |
| 4939 | ssl->out_msg += ssl_transform_get_explicit_iv_len(transform); |
| 4940 | } |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4941 | } |
| 4942 | |
| 4943 | /* Once ssl->in_hdr as the address of the beginning of the |
| 4944 | * next incoming record is set, deduce the other pointers. |
| 4945 | * |
| 4946 | * Note: For TLS, we save the implicit record sequence number |
| 4947 | * (entering MAC computation) in the 8 bytes before ssl->in_hdr, |
| 4948 | * and the caller has to make sure there's space for this. |
| 4949 | */ |
| 4950 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4951 | void mbedtls_ssl_update_in_pointers(mbedtls_ssl_context *ssl) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4952 | { |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 4953 | /* This function sets the pointers to match the case |
| 4954 | * of unprotected TLS/DTLS records, with both ssl->in_iv |
| 4955 | * and ssl->in_msg pointing to the beginning of the record |
| 4956 | * content. |
| 4957 | * |
| 4958 | * When decrypting a protected record, ssl->in_msg |
| 4959 | * will be shifted to point to the beginning of the |
| 4960 | * record plaintext. |
| 4961 | */ |
| 4962 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4963 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4964 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4965 | /* This sets the header pointers to match records |
| 4966 | * without CID. When we receive a record containing |
| 4967 | * a CID, the fields are shifted accordingly in |
| 4968 | * ssl_parse_record_header(). */ |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4969 | ssl->in_ctr = ssl->in_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4970 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4971 | ssl->in_cid = ssl->in_ctr + 8; |
| 4972 | ssl->in_len = ssl->in_cid; /* Default: no CID */ |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4973 | #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4974 | ssl->in_len = ssl->in_ctr + 8; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4975 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4976 | ssl->in_iv = ssl->in_len + 2; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4977 | } else |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4978 | #endif |
| 4979 | { |
| 4980 | ssl->in_ctr = ssl->in_hdr - 8; |
| 4981 | ssl->in_len = ssl->in_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4982 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 4c3eb7c | 2019-05-08 16:43:21 +0100 | [diff] [blame] | 4983 | ssl->in_cid = ssl->in_len; |
| 4984 | #endif |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4985 | ssl->in_iv = ssl->in_hdr + 5; |
| 4986 | } |
| 4987 | |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 4988 | /* This will be adjusted at record decryption time. */ |
| 4989 | ssl->in_msg = ssl->in_iv; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4990 | } |
| 4991 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4992 | /* |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 4993 | * Setup an SSL context |
| 4994 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 4995 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4996 | void mbedtls_ssl_reset_in_out_pointers(mbedtls_ssl_context *ssl) |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 4997 | { |
| 4998 | /* Set the incoming and outgoing record pointers. */ |
| 4999 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5000 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 5001 | ssl->out_hdr = ssl->out_buf; |
| 5002 | ssl->in_hdr = ssl->in_buf; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5003 | } else |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 5004 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5005 | { |
| 5006 | ssl->out_hdr = ssl->out_buf + 8; |
| 5007 | ssl->in_hdr = ssl->in_buf + 8; |
| 5008 | } |
| 5009 | |
| 5010 | /* Derive other internal pointers. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5011 | mbedtls_ssl_update_out_pointers(ssl, NULL /* no transform enabled */); |
| 5012 | mbedtls_ssl_update_in_pointers(ssl); |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 5013 | } |
| 5014 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5015 | /* |
| 5016 | * SSL get accessors |
| 5017 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5018 | size_t mbedtls_ssl_get_bytes_avail(const mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5019 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5020 | return ssl->in_offt == NULL ? 0 : ssl->in_msglen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5021 | } |
| 5022 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5023 | int mbedtls_ssl_check_pending(const mbedtls_ssl_context *ssl) |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 5024 | { |
| 5025 | /* |
| 5026 | * Case A: We're currently holding back |
| 5027 | * a message for further processing. |
| 5028 | */ |
| 5029 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5030 | if (ssl->keep_current_message == 1) { |
| 5031 | MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: record held back for processing")); |
| 5032 | return 1; |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 5033 | } |
| 5034 | |
| 5035 | /* |
| 5036 | * Case B: Further records are pending in the current datagram. |
| 5037 | */ |
| 5038 | |
| 5039 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5040 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 5041 | ssl->in_left > ssl->next_record_offset) { |
| 5042 | MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: more records within current datagram")); |
| 5043 | return 1; |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 5044 | } |
| 5045 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5046 | |
| 5047 | /* |
| 5048 | * Case C: A handshake message is being processed. |
| 5049 | */ |
| 5050 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5051 | if (ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen) { |
| 5052 | MBEDTLS_SSL_DEBUG_MSG(3, |
| 5053 | ("ssl_check_pending: more handshake messages within current record")); |
| 5054 | return 1; |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 5055 | } |
| 5056 | |
| 5057 | /* |
| 5058 | * Case D: An application data message is being processed |
| 5059 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5060 | if (ssl->in_offt != NULL) { |
| 5061 | MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: application data record is being processed")); |
| 5062 | return 1; |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 5063 | } |
| 5064 | |
| 5065 | /* |
| 5066 | * In all other cases, the rest of the message can be dropped. |
Hanno Becker | c573ac3 | 2018-08-28 17:15:25 +0100 | [diff] [blame] | 5067 | * As in ssl_get_next_record, this needs to be adapted if |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 5068 | * we implement support for multiple alerts in single records. |
| 5069 | */ |
| 5070 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5071 | MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: nothing pending")); |
| 5072 | return 0; |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 5073 | } |
| 5074 | |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 5075 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5076 | 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] | 5077 | { |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 5078 | size_t transform_expansion = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5079 | const mbedtls_ssl_transform *transform = ssl->transform_out; |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 5080 | unsigned block_size; |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5081 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5082 | size_t out_hdr_len = mbedtls_ssl_out_hdr_len(ssl); |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 5083 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5084 | if (transform == NULL) { |
| 5085 | return (int) out_hdr_len; |
| 5086 | } |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 5087 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5088 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5089 | if (ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL) { |
| 5090 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 5091 | } |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5092 | #endif |
| 5093 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5094 | switch (mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5095 | case MBEDTLS_MODE_GCM: |
| 5096 | case MBEDTLS_MODE_CCM: |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 5097 | case MBEDTLS_MODE_CHACHAPOLY: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5098 | case MBEDTLS_MODE_STREAM: |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5099 | transform_expansion = transform->minlen; |
| 5100 | break; |
| 5101 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5102 | case MBEDTLS_MODE_CBC: |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 5103 | |
| 5104 | block_size = mbedtls_cipher_get_block_size( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5105 | &transform->cipher_ctx_enc); |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 5106 | |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 5107 | /* Expansion due to the addition of the MAC. */ |
| 5108 | transform_expansion += transform->maclen; |
| 5109 | |
| 5110 | /* Expansion due to the addition of CBC padding; |
| 5111 | * Theoretically up to 256 bytes, but we never use |
| 5112 | * more than the block size of the underlying cipher. */ |
| 5113 | transform_expansion += block_size; |
| 5114 | |
| 5115 | /* For TLS 1.1 or higher, an explicit IV is added |
| 5116 | * after the record header. */ |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 5117 | #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] | 5118 | if (ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2) { |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 5119 | transform_expansion += block_size; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5120 | } |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 5121 | #endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 5122 | |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5123 | break; |
| 5124 | |
| 5125 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5126 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 5127 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5128 | } |
| 5129 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5130 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5131 | if (transform->out_cid_len != 0) { |
Hanno Becker | 6cbad55 | 2019-05-08 15:40:11 +0100 | [diff] [blame] | 5132 | transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5133 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 5134 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 6cbad55 | 2019-05-08 15:40:11 +0100 | [diff] [blame] | 5135 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5136 | return (int) (out_hdr_len + transform_expansion); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5137 | } |
| 5138 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5139 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5140 | /* |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 5141 | * Check record counters and renegotiate if they're above the limit. |
| 5142 | */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 5143 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5144 | static int ssl_check_ctr_renegotiate(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 5145 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5146 | size_t ep_len = mbedtls_ssl_ep_len(ssl); |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 5147 | int in_ctr_cmp; |
| 5148 | int out_ctr_cmp; |
| 5149 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5150 | if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5151 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING || |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5152 | ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED) { |
| 5153 | return 0; |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 5154 | } |
| 5155 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5156 | in_ctr_cmp = memcmp(ssl->in_ctr + ep_len, |
| 5157 | ssl->conf->renego_period + ep_len, 8 - ep_len); |
| 5158 | out_ctr_cmp = memcmp(ssl->cur_out_ctr + ep_len, |
| 5159 | ssl->conf->renego_period + ep_len, 8 - ep_len); |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 5160 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5161 | if (in_ctr_cmp <= 0 && out_ctr_cmp <= 0) { |
| 5162 | return 0; |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 5163 | } |
| 5164 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5165 | MBEDTLS_SSL_DEBUG_MSG(1, ("record counter limit reached: renegotiate")); |
| 5166 | return mbedtls_ssl_renegotiate(ssl); |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 5167 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5168 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5169 | |
| 5170 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5171 | * Receive application data decrypted from the SSL layer |
| 5172 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5173 | 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] | 5174 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5175 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 5176 | size_t n; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5177 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5178 | if (ssl == NULL || ssl->conf == NULL) { |
| 5179 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5180 | } |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5181 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5182 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> read")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5183 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5184 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5185 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 5186 | if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) { |
| 5187 | return ret; |
| 5188 | } |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 5189 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5190 | if (ssl->handshake != NULL && |
| 5191 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) { |
| 5192 | if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) { |
| 5193 | return ret; |
| 5194 | } |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 5195 | } |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 5196 | } |
| 5197 | #endif |
| 5198 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5199 | /* |
| 5200 | * Check if renegotiation is necessary and/or handshake is |
| 5201 | * in process. If yes, perform/continue, and fall through |
| 5202 | * if an unexpected packet is received while the client |
| 5203 | * is waiting for the ServerHello. |
| 5204 | * |
| 5205 | * (There is no equivalent to the last condition on |
| 5206 | * the server-side as it is not treated as within |
| 5207 | * a handshake while waiting for the ClientHello |
| 5208 | * after a renegotiation request.) |
| 5209 | */ |
| 5210 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5211 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5212 | ret = ssl_check_ctr_renegotiate(ssl); |
| 5213 | if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 5214 | ret != 0) { |
| 5215 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_check_ctr_renegotiate", ret); |
| 5216 | return ret; |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 5217 | } |
| 5218 | #endif |
| 5219 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5220 | if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 5221 | ret = mbedtls_ssl_handshake(ssl); |
| 5222 | if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 5223 | ret != 0) { |
| 5224 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret); |
| 5225 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5226 | } |
| 5227 | } |
| 5228 | |
Hanno Becker | e41158b | 2017-10-23 13:30:32 +0100 | [diff] [blame] | 5229 | /* Loop as long as no application data record is available */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5230 | while (ssl->in_offt == NULL) { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 5231 | /* Start timer if not already running */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5232 | if (ssl->f_get_timer != NULL && |
| 5233 | ssl->f_get_timer(ssl->p_timer) == -1) { |
| 5234 | mbedtls_ssl_set_timer(ssl, ssl->conf->read_timeout); |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 5235 | } |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 5236 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5237 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
| 5238 | if (ret == MBEDTLS_ERR_SSL_CONN_EOF) { |
| 5239 | return 0; |
| 5240 | } |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 5241 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5242 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
| 5243 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5244 | } |
| 5245 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5246 | if (ssl->in_msglen == 0 && |
| 5247 | ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5248 | /* |
| 5249 | * OpenSSL sends empty messages to randomize the IV |
| 5250 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5251 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
| 5252 | if (ret == MBEDTLS_ERR_SSL_CONN_EOF) { |
| 5253 | return 0; |
| 5254 | } |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 5255 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5256 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
| 5257 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5258 | } |
| 5259 | } |
| 5260 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5261 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 5262 | MBEDTLS_SSL_DEBUG_MSG(1, ("received handshake message")); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5263 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5264 | /* |
| 5265 | * - For client-side, expect SERVER_HELLO_REQUEST. |
| 5266 | * - For server-side, expect CLIENT_HELLO. |
| 5267 | * - Fail (TLS) or silently drop record (DTLS) in other cases. |
| 5268 | */ |
| 5269 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5270 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5271 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 5272 | (ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST || |
| 5273 | ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl))) { |
| 5274 | MBEDTLS_SSL_DEBUG_MSG(1, ("handshake received (not HelloRequest)")); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 5275 | |
| 5276 | /* With DTLS, drop the packet (probably from last handshake) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5277 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5278 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5279 | continue; |
| 5280 | } |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 5281 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5282 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 5283 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5284 | #endif /* MBEDTLS_SSL_CLI_C */ |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 5285 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5286 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5287 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 5288 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO) { |
| 5289 | MBEDTLS_SSL_DEBUG_MSG(1, ("handshake received (not ClientHello)")); |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 5290 | |
| 5291 | /* With DTLS, drop the packet (probably from last handshake) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5292 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5293 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5294 | continue; |
| 5295 | } |
Manuel Pégourié-Gonnard | 990f9e4 | 2014-09-06 12:27:02 +0200 | [diff] [blame] | 5296 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5297 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5298 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5299 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 5300 | |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 5301 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5302 | /* Determine whether renegotiation attempt should be accepted */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5303 | if (!(ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
| 5304 | (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
| 5305 | ssl->conf->allow_legacy_renegotiation == |
| 5306 | MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION))) { |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 5307 | /* |
| 5308 | * Accept renegotiation request |
| 5309 | */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5310 | |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 5311 | /* DTLS clients need to know renego is server-initiated */ |
| 5312 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5313 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 5314 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 5315 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
| 5316 | } |
| 5317 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5318 | ret = mbedtls_ssl_start_renegotiation(ssl); |
| 5319 | if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 5320 | ret != 0) { |
| 5321 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", |
| 5322 | ret); |
| 5323 | return ret; |
Hanno Becker | b4ff0aa | 2017-10-17 11:03:04 +0100 | [diff] [blame] | 5324 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5325 | } else |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 5326 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5327 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5328 | /* |
| 5329 | * Refuse renegotiation |
| 5330 | */ |
| 5331 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5332 | MBEDTLS_SSL_DEBUG_MSG(3, ("refusing renegotiation, sending alert")); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5333 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5334 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5335 | if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0) { |
Gilles Peskine | 92e4426 | 2017-05-10 17:27:49 +0200 | [diff] [blame] | 5336 | /* SSLv3 does not have a "no_renegotiation" warning, so |
| 5337 | we send a fatal alert and abort the connection. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5338 | mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 5339 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
| 5340 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
| 5341 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5342 | #endif /* MBEDTLS_SSL_PROTO_SSL3 */ |
| 5343 | #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5344 | defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5345 | if (ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1) { |
| 5346 | if ((ret = mbedtls_ssl_send_alert_message(ssl, |
| 5347 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, |
| 5348 | MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION)) |
| 5349 | != 0) { |
| 5350 | return ret; |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 5351 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5352 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5353 | #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || |
| 5354 | MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 5355 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5356 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 5357 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 5358 | } |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5359 | } |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 5360 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5361 | /* At this point, we don't know whether the renegotiation has been |
| 5362 | * completed or not. The cases to consider are the following: |
| 5363 | * 1) The renegotiation is complete. In this case, no new record |
| 5364 | * has been read yet. |
| 5365 | * 2) The renegotiation is incomplete because the client received |
| 5366 | * an application data record while awaiting the ServerHello. |
| 5367 | * 3) The renegotiation is incomplete because the client received |
| 5368 | * a non-handshake, non-application data message while awaiting |
| 5369 | * the ServerHello. |
| 5370 | * In each of these case, looping will be the proper action: |
| 5371 | * - For 1), the next iteration will read a new record and check |
| 5372 | * if it's application data. |
| 5373 | * - For 2), the loop condition isn't satisfied as application data |
| 5374 | * is present, hence continue is the same as break |
| 5375 | * - For 3), the loop condition is satisfied and read_record |
| 5376 | * will re-deliver the message that was held back by the client |
| 5377 | * when expecting the ServerHello. |
| 5378 | */ |
| 5379 | continue; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5380 | } |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 5381 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5382 | else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) { |
| 5383 | if (ssl->conf->renego_max_records >= 0) { |
| 5384 | if (++ssl->renego_records_seen > ssl->conf->renego_max_records) { |
| 5385 | MBEDTLS_SSL_DEBUG_MSG(1, ("renegotiation requested, " |
| 5386 | "but not honored by client")); |
| 5387 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 5388 | } |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 5389 | } |
Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 5390 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5391 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 5392 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5393 | /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5394 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT) { |
| 5395 | MBEDTLS_SSL_DEBUG_MSG(2, ("ignoring non-fatal non-closure alert")); |
| 5396 | return MBEDTLS_ERR_SSL_WANT_READ; |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 5397 | } |
| 5398 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5399 | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA) { |
| 5400 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad application data message")); |
| 5401 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5402 | } |
| 5403 | |
| 5404 | ssl->in_offt = ssl->in_msg; |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 5405 | |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 5406 | /* We're going to return something now, cancel timer, |
| 5407 | * except if handshake (renegotiation) is in progress */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5408 | if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 5409 | mbedtls_ssl_set_timer(ssl, 0); |
| 5410 | } |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 5411 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 5412 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 5413 | /* If we requested renego but received AppData, resend HelloRequest. |
| 5414 | * Do it now, after setting in_offt, to avoid taking this branch |
| 5415 | * again if ssl_write_hello_request() returns WANT_WRITE */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5416 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5417 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 5418 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) { |
| 5419 | if ((ret = mbedtls_ssl_resend_hello_request(ssl)) != 0) { |
| 5420 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend_hello_request", |
| 5421 | ret); |
| 5422 | return ret; |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 5423 | } |
| 5424 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5425 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5426 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5427 | } |
| 5428 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5429 | n = (len < ssl->in_msglen) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5430 | ? len : ssl->in_msglen; |
| 5431 | |
ashesman | cf01d78 | 2022-02-17 11:08:27 +1300 | [diff] [blame] | 5432 | if (len != 0) { |
Ashley Duncan | 272cc19 | 2022-02-11 09:57:18 +1300 | [diff] [blame] | 5433 | memcpy(buf, ssl->in_offt, n); |
| 5434 | ssl->in_msglen -= n; |
| 5435 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5436 | |
gabor-mezei-arm | a321413 | 2020-07-15 10:55:00 +0200 | [diff] [blame] | 5437 | /* Zeroising the plaintext buffer to erase unused application data |
| 5438 | from the memory. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5439 | mbedtls_platform_zeroize(ssl->in_offt, n); |
gabor-mezei-arm | a321413 | 2020-07-15 10:55:00 +0200 | [diff] [blame] | 5440 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5441 | if (ssl->in_msglen == 0) { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5442 | /* all bytes consumed */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5443 | ssl->in_offt = NULL; |
Hanno Becker | bdf3905 | 2017-06-09 10:42:03 +0100 | [diff] [blame] | 5444 | ssl->keep_current_message = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5445 | } else { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5446 | /* more data available */ |
| 5447 | ssl->in_offt += n; |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5448 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5449 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5450 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= read")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5451 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5452 | return (int) n; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5453 | } |
| 5454 | |
| 5455 | /* |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 5456 | * Send application data to be encrypted by the SSL layer, taking care of max |
| 5457 | * fragment length and buffer size. |
| 5458 | * |
| 5459 | * According to RFC 5246 Section 6.2.1: |
| 5460 | * |
| 5461 | * Zero-length fragments of Application data MAY be sent as they are |
| 5462 | * potentially useful as a traffic analysis countermeasure. |
| 5463 | * |
| 5464 | * Therefore, it is possible that the input message length is 0 and the |
| 5465 | * corresponding return code is 0 on success. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5466 | */ |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 5467 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5468 | static int ssl_write_real(mbedtls_ssl_context *ssl, |
| 5469 | const unsigned char *buf, size_t len) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5470 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5471 | int ret = mbedtls_ssl_get_max_out_record_payload(ssl); |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 5472 | const size_t max_len = (size_t) ret; |
| 5473 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5474 | if (ret < 0) { |
| 5475 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_max_out_record_payload", ret); |
| 5476 | return ret; |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 5477 | } |
| 5478 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5479 | if (len > max_len) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5480 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5481 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 5482 | MBEDTLS_SSL_DEBUG_MSG(1, ("fragment larger than the (negotiated) " |
| 5483 | "maximum fragment length: %" MBEDTLS_PRINTF_SIZET |
| 5484 | " > %" MBEDTLS_PRINTF_SIZET, |
| 5485 | len, max_len)); |
| 5486 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5487 | } else |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 5488 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5489 | len = max_len; |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 5490 | } |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 5491 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5492 | if (ssl->out_left != 0) { |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 5493 | /* |
| 5494 | * The user has previously tried to send the data and |
| 5495 | * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially |
| 5496 | * written. In this case, we expect the high-level write function |
| 5497 | * (e.g. mbedtls_ssl_write()) to be called with the same parameters |
| 5498 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5499 | if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) { |
| 5500 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flush_output", ret); |
| 5501 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5502 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5503 | } else { |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 5504 | /* |
| 5505 | * The user is trying to send a message the first time, so we need to |
| 5506 | * copy the data into the internal buffers and setup the data structure |
| 5507 | * to keep track of partial writes |
| 5508 | */ |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 5509 | ssl->out_msglen = len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5510 | ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
Dave Rodgman | 1215557 | 2023-02-24 15:41:34 +0000 | [diff] [blame] | 5511 | if (len > 0) { |
| 5512 | memcpy(ssl->out_msg, buf, len); |
| 5513 | } |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 5514 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5515 | if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) { |
| 5516 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret); |
| 5517 | return ret; |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 5518 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5519 | } |
| 5520 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5521 | return (int) len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5522 | } |
| 5523 | |
| 5524 | /* |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 5525 | * Write application data, doing 1/n-1 splitting if necessary. |
| 5526 | * |
| 5527 | * 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] | 5528 | * then the caller will call us again with the same arguments, so |
Hanno Becker | 2b187c4 | 2017-09-18 14:58:11 +0100 | [diff] [blame] | 5529 | * remember whether we already did the split or not. |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 5530 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5531 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Manuel Pégourié-Gonnard | d904d66 | 2022-06-17 10:24:00 +0200 | [diff] [blame] | 5532 | MBEDTLS_CHECK_RETURN_CRITICAL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5533 | static int ssl_write_split(mbedtls_ssl_context *ssl, |
| 5534 | const unsigned char *buf, size_t len) |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 5535 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5536 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 5537 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5538 | if (ssl->conf->cbc_record_splitting == |
| 5539 | MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED || |
Manuel Pégourié-Gonnard | cfa477e | 2015-01-07 14:50:54 +0100 | [diff] [blame] | 5540 | len <= 1 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5541 | ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 || |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5542 | mbedtls_cipher_get_cipher_mode(&ssl->transform_out->cipher_ctx_enc) |
| 5543 | != MBEDTLS_MODE_CBC) { |
| 5544 | return ssl_write_real(ssl, buf, len); |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 5545 | } |
| 5546 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5547 | if (ssl->split_done == 0) { |
| 5548 | if ((ret = ssl_write_real(ssl, buf, 1)) <= 0) { |
| 5549 | return ret; |
| 5550 | } |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 5551 | ssl->split_done = 1; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 5552 | } |
| 5553 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5554 | if ((ret = ssl_write_real(ssl, buf + 1, len - 1)) <= 0) { |
| 5555 | return ret; |
| 5556 | } |
Manuel Pégourié-Gonnard | a852cf4 | 2015-01-13 20:56:15 +0100 | [diff] [blame] | 5557 | ssl->split_done = 0; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 5558 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5559 | return ret + 1; |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 5560 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5561 | #endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */ |
Manuel Pégourié-Gonnard | d76314c | 2015-01-07 12:39:44 +0100 | [diff] [blame] | 5562 | |
| 5563 | /* |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5564 | * Write application data (public-facing wrapper) |
| 5565 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5566 | 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] | 5567 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5568 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5569 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5570 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write")); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5571 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5572 | if (ssl == NULL || ssl->conf == NULL) { |
| 5573 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5574 | } |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5575 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 5576 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5577 | if ((ret = ssl_check_ctr_renegotiate(ssl)) != 0) { |
| 5578 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_check_ctr_renegotiate", ret); |
| 5579 | return ret; |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5580 | } |
| 5581 | #endif |
| 5582 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5583 | if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 5584 | if ((ret = mbedtls_ssl_handshake(ssl)) != 0) { |
| 5585 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret); |
| 5586 | return ret; |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5587 | } |
| 5588 | } |
| 5589 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 5590 | #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5591 | ret = ssl_write_split(ssl, buf, len); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5592 | #else |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5593 | ret = ssl_write_real(ssl, buf, len); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5594 | #endif |
| 5595 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5596 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write")); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5597 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5598 | return ret; |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5599 | } |
| 5600 | |
| 5601 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5602 | * Notify the peer that the connection is being closed |
| 5603 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5604 | int mbedtls_ssl_close_notify(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5605 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5606 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5607 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5608 | if (ssl == NULL || ssl->conf == NULL) { |
| 5609 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5610 | } |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5611 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5612 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write close notify")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5613 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5614 | if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 5615 | if ((ret = mbedtls_ssl_send_alert_message(ssl, |
| 5616 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, |
| 5617 | MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY)) != 0) { |
| 5618 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_send_alert_message", ret); |
| 5619 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5620 | } |
| 5621 | } |
| 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 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5626 | } |
| 5627 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5628 | void mbedtls_ssl_transform_free(mbedtls_ssl_transform *transform) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5629 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5630 | if (transform == NULL) { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 5631 | return; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5632 | } |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 5633 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5634 | #if defined(MBEDTLS_ZLIB_SUPPORT) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5635 | deflateEnd(&transform->ctx_deflate); |
| 5636 | inflateEnd(&transform->ctx_inflate); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5637 | #endif |
| 5638 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5639 | mbedtls_cipher_free(&transform->cipher_ctx_enc); |
| 5640 | mbedtls_cipher_free(&transform->cipher_ctx_dec); |
Manuel Pégourié-Gonnard | f71e587 | 2013-09-23 17:12:43 +0200 | [diff] [blame] | 5641 | |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 5642 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5643 | mbedtls_md_free(&transform->md_ctx_enc); |
| 5644 | mbedtls_md_free(&transform->md_ctx_dec); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 5645 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 5646 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5647 | mbedtls_platform_zeroize(transform, sizeof(mbedtls_ssl_transform)); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5648 | } |
| 5649 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 5650 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5651 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5652 | void mbedtls_ssl_buffering_free(mbedtls_ssl_context *ssl) |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 5653 | { |
| 5654 | unsigned offset; |
| 5655 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 5656 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5657 | if (hs == NULL) { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 5658 | return; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5659 | } |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 5660 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5661 | ssl_free_buffered_record(ssl); |
Hanno Becker | 283f5ef | 2018-08-24 09:34:47 +0100 | [diff] [blame] | 5662 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5663 | for (offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++) { |
| 5664 | ssl_buffering_free_slot(ssl, offset); |
| 5665 | } |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 5666 | } |
| 5667 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5668 | static void ssl_buffering_free_slot(mbedtls_ssl_context *ssl, |
| 5669 | uint8_t slot) |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 5670 | { |
| 5671 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 5672 | mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot]; |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 5673 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5674 | if (slot >= MBEDTLS_SSL_MAX_BUFFERED_HS) { |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 5675 | return; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5676 | } |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 5677 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5678 | if (hs_buf->is_valid == 1) { |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 5679 | hs->buffering.total_bytes_buffered -= hs_buf->data_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5680 | mbedtls_platform_zeroize(hs_buf->data, hs_buf->data_len); |
| 5681 | mbedtls_free(hs_buf->data); |
| 5682 | memset(hs_buf, 0, sizeof(mbedtls_ssl_hs_buffer)); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 5683 | } |
| 5684 | } |
| 5685 | |
| 5686 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5687 | |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5688 | /* |
| 5689 | * Convert version numbers to/from wire format |
| 5690 | * and, for DTLS, to/from TLS equivalent. |
| 5691 | * |
| 5692 | * For TLS this is the identity. |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 5693 | * 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] | 5694 | * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1) |
| 5695 | * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2) |
| 5696 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5697 | void mbedtls_ssl_write_version(int major, int minor, int transport, |
| 5698 | unsigned char ver[2]) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5699 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5700 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5701 | if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 5702 | if (minor == MBEDTLS_SSL_MINOR_VERSION_2) { |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5703 | --minor; /* DTLS 1.0 stored as TLS 1.1 internally */ |
| 5704 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5705 | } |
| 5706 | ver[0] = (unsigned char) (255 - (major - 2)); |
| 5707 | ver[1] = (unsigned char) (255 - (minor - 1)); |
| 5708 | } else |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 5709 | #else |
| 5710 | ((void) transport); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5711 | #endif |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 5712 | { |
| 5713 | ver[0] = (unsigned char) major; |
| 5714 | ver[1] = (unsigned char) minor; |
| 5715 | } |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5716 | } |
| 5717 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5718 | void mbedtls_ssl_read_version(int *major, int *minor, int transport, |
| 5719 | const unsigned char ver[2]) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5720 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5721 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5722 | if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5723 | *major = 255 - ver[0] + 2; |
| 5724 | *minor = 255 - ver[1] + 1; |
| 5725 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5726 | if (*minor == MBEDTLS_SSL_MINOR_VERSION_1) { |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5727 | ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 5728 | } |
| 5729 | } else |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 5730 | #else |
| 5731 | ((void) transport); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5732 | #endif |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 5733 | { |
| 5734 | *major = ver[0]; |
| 5735 | *minor = ver[1]; |
| 5736 | } |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5737 | } |
| 5738 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5739 | #endif /* MBEDTLS_SSL_TLS_C */ |