Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
Hanno Becker | f1a3828 | 2020-02-05 16:14:29 +0000 | [diff] [blame] | 2 | * Generic SSL/TLS messaging layer functions |
| 3 | * (record layer + retransmission state machine) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 5 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: Apache-2.0 |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | * not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 19 | */ |
| 20 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 21 | * http://www.ietf.org/rfc/rfc2246.txt |
| 22 | * http://www.ietf.org/rfc/rfc4346.txt |
| 23 | */ |
| 24 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 25 | #include "common.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 26 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 27 | #if defined(MBEDTLS_SSL_TLS_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 28 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 29 | # if defined(MBEDTLS_PLATFORM_C) |
| 30 | # include "mbedtls/platform.h" |
| 31 | # else |
| 32 | # include <stdlib.h> |
| 33 | # define mbedtls_calloc calloc |
| 34 | # define mbedtls_free free |
| 35 | # endif |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 36 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 37 | # include "mbedtls/ssl.h" |
| 38 | # include "ssl_misc.h" |
| 39 | # include "mbedtls/debug.h" |
| 40 | # include "mbedtls/error.h" |
| 41 | # include "mbedtls/platform_util.h" |
| 42 | # include "mbedtls/version.h" |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 43 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 44 | # include "ssl_invasive.h" |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 45 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 46 | # include <string.h> |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 47 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 48 | # if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 49 | # include "mbedtls/psa_util.h" |
| 50 | # include "psa/crypto.h" |
| 51 | # endif |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 52 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 53 | # if defined(MBEDTLS_X509_CRT_PARSE_C) |
| 54 | # include "mbedtls/oid.h" |
| 55 | # endif |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 56 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 57 | 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] | 58 | |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 59 | /* |
| 60 | * Start a timer. |
| 61 | * Passing millisecs = 0 cancels a running timer. |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 62 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 63 | 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] | 64 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 65 | if (ssl->f_set_timer == NULL) |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 66 | return; |
| 67 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 68 | MBEDTLS_SSL_DEBUG_MSG(3, ("set_timer to %d ms", (int)millisecs)); |
| 69 | ssl->f_set_timer(ssl->p_timer, millisecs / 4, millisecs); |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /* |
| 73 | * Return -1 is timer is expired, 0 if it isn't. |
| 74 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 75 | int mbedtls_ssl_check_timer(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 76 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 77 | if (ssl->f_get_timer == NULL) |
| 78 | return 0; |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 79 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 80 | if (ssl->f_get_timer(ssl->p_timer) == 2) { |
| 81 | MBEDTLS_SSL_DEBUG_MSG(3, ("timer expired")); |
| 82 | return -1; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 83 | } |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 84 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 85 | return 0; |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 86 | } |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 87 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 88 | static int ssl_parse_record_header(mbedtls_ssl_context const *ssl, |
| 89 | unsigned char *buf, |
| 90 | size_t len, |
| 91 | mbedtls_record *rec); |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 92 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 93 | int mbedtls_ssl_check_record(mbedtls_ssl_context const *ssl, |
| 94 | unsigned char *buf, |
| 95 | size_t buflen) |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 96 | { |
| 97 | int ret = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 98 | MBEDTLS_SSL_DEBUG_MSG(1, ("=> mbedtls_ssl_check_record")); |
| 99 | MBEDTLS_SSL_DEBUG_BUF(3, "record buffer", buf, buflen); |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 100 | |
| 101 | /* We don't support record checking in TLS because |
TRodziewicz | 2abf03c | 2021-06-25 14:40:09 +0200 | [diff] [blame] | 102 | * there doesn't seem to be a usecase for it. |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 103 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 104 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM) { |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 105 | ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 106 | goto exit; |
| 107 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 108 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 109 | else { |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 110 | mbedtls_record rec; |
| 111 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 112 | ret = ssl_parse_record_header(ssl, buf, buflen, &rec); |
| 113 | if (ret != 0) { |
| 114 | MBEDTLS_SSL_DEBUG_RET(3, "ssl_parse_record_header", ret); |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 115 | goto exit; |
| 116 | } |
| 117 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 118 | if (ssl->transform_in != NULL) { |
| 119 | ret = mbedtls_ssl_decrypt_buf(ssl, ssl->transform_in, &rec); |
| 120 | if (ret != 0) { |
| 121 | MBEDTLS_SSL_DEBUG_RET(3, "mbedtls_ssl_decrypt_buf", ret); |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 122 | goto exit; |
| 123 | } |
| 124 | } |
| 125 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 126 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 127 | |
| 128 | exit: |
| 129 | /* On success, we have decrypted the buffer in-place, so make |
| 130 | * sure we don't leak any plaintext data. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 131 | mbedtls_platform_zeroize(buf, buflen); |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 132 | |
| 133 | /* For the purpose of this API, treat messages with unexpected CID |
| 134 | * as well as such from future epochs as unexpected. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 135 | if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID || |
| 136 | ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) { |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 137 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
| 138 | } |
| 139 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 140 | MBEDTLS_SSL_DEBUG_MSG(1, ("<= mbedtls_ssl_check_record")); |
| 141 | return ret; |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 142 | } |
| 143 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 144 | # define SSL_DONT_FORCE_FLUSH 0 |
| 145 | # define SSL_FORCE_FLUSH 1 |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 146 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 147 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 148 | |
Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 149 | /* Forward declarations for functions related to message buffering. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 150 | static void ssl_buffering_free_slot(mbedtls_ssl_context *ssl, uint8_t slot); |
| 151 | static void ssl_free_buffered_record(mbedtls_ssl_context *ssl); |
| 152 | static int ssl_load_buffered_message(mbedtls_ssl_context *ssl); |
| 153 | static int ssl_load_buffered_record(mbedtls_ssl_context *ssl); |
| 154 | static int ssl_buffer_message(mbedtls_ssl_context *ssl); |
| 155 | static int ssl_buffer_future_record(mbedtls_ssl_context *ssl, |
| 156 | mbedtls_record const *rec); |
| 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 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [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 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 161 | size_t mtu = mbedtls_ssl_get_current_mtu(ssl); |
| 162 | # if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 163 | size_t out_buf_len = ssl->out_buf_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 164 | # else |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 165 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 166 | # endif |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 167 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 168 | if (mtu != 0 && mtu < out_buf_len) |
| 169 | return mtu; |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 170 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 171 | return out_buf_len; |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 172 | } |
| 173 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 174 | 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] | 175 | { |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 176 | size_t const bytes_written = ssl->out_left; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 177 | size_t const mtu = ssl_get_maximum_datagram_size(ssl); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 178 | |
| 179 | /* Double-check that the write-index hasn't gone |
| 180 | * past what we can transmit in a single datagram. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 181 | if (bytes_written > mtu) { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 182 | /* Should never happen... */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 183 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 186 | return ((int)(mtu - bytes_written)); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 187 | } |
| 188 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 189 | 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] | 190 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 191 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 192 | size_t remaining, expansion; |
Andrzej Kurek | 748face | 2018-10-11 07:20:19 -0400 | [diff] [blame] | 193 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 194 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 195 | # if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 196 | const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 197 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 198 | if (max_len > mfl) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 199 | max_len = mfl; |
Hanno Becker | f4b010e | 2018-08-24 10:47:29 +0100 | [diff] [blame] | 200 | |
| 201 | /* By the standard (RFC 6066 Sect. 4), the MFL extension |
| 202 | * only limits the maximum record payload size, so in theory |
| 203 | * we would be allowed to pack multiple records of payload size |
| 204 | * MFL into a single datagram. However, this would mean that there's |
| 205 | * no way to explicitly communicate MTU restrictions to the peer. |
| 206 | * |
| 207 | * The following reduction of max_len makes sure that we never |
| 208 | * write datagrams larger than MFL + Record Expansion Overhead. |
| 209 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 210 | if (max_len <= ssl->out_left) |
| 211 | return 0; |
Hanno Becker | f4b010e | 2018-08-24 10:47:29 +0100 | [diff] [blame] | 212 | |
| 213 | max_len -= ssl->out_left; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 214 | # endif |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 215 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 216 | ret = ssl_get_remaining_space_in_datagram(ssl); |
| 217 | if (ret < 0) |
| 218 | return ret; |
| 219 | remaining = (size_t)ret; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 220 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 221 | ret = mbedtls_ssl_get_record_expansion(ssl); |
| 222 | if (ret < 0) |
| 223 | return ret; |
| 224 | expansion = (size_t)ret; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 225 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 226 | if (remaining <= expansion) |
| 227 | return 0; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 228 | |
| 229 | remaining -= expansion; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 230 | if (remaining >= max_len) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 231 | remaining = max_len; |
| 232 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 233 | return (int)remaining; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 234 | } |
| 235 | |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 236 | /* |
| 237 | * Double the retransmit timeout value, within the allowed range, |
| 238 | * returning -1 if the maximum value has already been reached. |
| 239 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 240 | static int ssl_double_retransmit_timeout(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 241 | { |
| 242 | uint32_t new_timeout; |
| 243 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 244 | if (ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max) |
| 245 | return -1; |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 246 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 247 | /* Implement the final paragraph of RFC 6347 section 4.1.1.1 |
| 248 | * in the following way: after the initial transmission and a first |
| 249 | * retransmission, back off to a temporary estimated MTU of 508 bytes. |
| 250 | * This value is guaranteed to be deliverable (if not guaranteed to be |
| 251 | * delivered) of any compliant IPv4 (and IPv6) network, and should work |
| 252 | * on most non-IP stacks too. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 253 | if (ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min) { |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 254 | ssl->handshake->mtu = 508; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 255 | MBEDTLS_SSL_DEBUG_MSG(2, ("mtu autoreduction to %d bytes", |
| 256 | ssl->handshake->mtu)); |
Andrzej Kurek | 6290dae | 2018-10-05 08:06:01 -0400 | [diff] [blame] | 257 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 258 | |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 259 | new_timeout = 2 * ssl->handshake->retransmit_timeout; |
| 260 | |
| 261 | /* Avoid arithmetic overflow and range overflow */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 262 | if (new_timeout < ssl->handshake->retransmit_timeout || |
| 263 | new_timeout > ssl->conf->hs_timeout_max) { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 264 | new_timeout = ssl->conf->hs_timeout_max; |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | ssl->handshake->retransmit_timeout = new_timeout; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 268 | MBEDTLS_SSL_DEBUG_MSG(3, |
| 269 | ("update timeout value to %lu millisecs", |
| 270 | (unsigned long)ssl->handshake->retransmit_timeout)); |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 271 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 272 | return 0; |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 273 | } |
| 274 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 275 | static void ssl_reset_retransmit_timeout(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 276 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 277 | ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 278 | MBEDTLS_SSL_DEBUG_MSG(3, |
| 279 | ("update timeout value to %lu millisecs", |
| 280 | (unsigned long)ssl->handshake->retransmit_timeout)); |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 281 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 282 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 283 | |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 284 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 285 | * Encryption/decryption functions |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 286 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 287 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 288 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) || \ |
| 289 | defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
Hanno Becker | 1399692 | 2020-05-28 16:15:19 +0100 | [diff] [blame] | 290 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 291 | static size_t ssl_compute_padding_length(size_t len, size_t granularity) |
Hanno Becker | 1399692 | 2020-05-28 16:15:19 +0100 | [diff] [blame] | 292 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 293 | return (granularity - (len + 1) % granularity) % granularity; |
Hanno Becker | 1399692 | 2020-05-28 16:15:19 +0100 | [diff] [blame] | 294 | } |
| 295 | |
Hanno Becker | 581bc1b | 2020-05-04 12:20:03 +0100 | [diff] [blame] | 296 | /* This functions transforms a (D)TLS plaintext fragment and a record content |
| 297 | * type into an instance of the (D)TLSInnerPlaintext structure. This is used |
| 298 | * in DTLS 1.2 + CID and within TLS 1.3 to allow flexible padding and to protect |
| 299 | * a record's content type. |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 300 | * |
| 301 | * struct { |
| 302 | * opaque content[DTLSPlaintext.length]; |
| 303 | * ContentType real_type; |
| 304 | * uint8 zeros[length_of_padding]; |
Hanno Becker | 581bc1b | 2020-05-04 12:20:03 +0100 | [diff] [blame] | 305 | * } (D)TLSInnerPlaintext; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 306 | * |
| 307 | * Input: |
| 308 | * - `content`: The beginning of the buffer holding the |
| 309 | * plaintext to be wrapped. |
| 310 | * - `*content_size`: The length of the plaintext in Bytes. |
| 311 | * - `max_len`: The number of Bytes available starting from |
| 312 | * `content`. This must be `>= *content_size`. |
| 313 | * - `rec_type`: The desired record content type. |
| 314 | * |
| 315 | * Output: |
Hanno Becker | 581bc1b | 2020-05-04 12:20:03 +0100 | [diff] [blame] | 316 | * - `content`: The beginning of the resulting (D)TLSInnerPlaintext structure. |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 317 | * - `*content_size`: The length of the resulting (D)TLSInnerPlaintext |
| 318 | * structure. |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 319 | * |
| 320 | * Returns: |
| 321 | * - `0` on success. |
| 322 | * - A negative error code if `max_len` didn't offer enough space |
| 323 | * for the expansion. |
| 324 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 325 | static int ssl_build_inner_plaintext(unsigned char *content, |
| 326 | size_t *content_size, |
| 327 | size_t remaining, |
| 328 | uint8_t rec_type, |
| 329 | size_t pad) |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 330 | { |
| 331 | size_t len = *content_size; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 332 | |
| 333 | /* Write real content type */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 334 | if (remaining == 0) |
| 335 | return -1; |
| 336 | content[len] = rec_type; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 337 | len++; |
| 338 | remaining--; |
| 339 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 340 | if (remaining < pad) |
| 341 | return -1; |
| 342 | memset(content + len, 0, pad); |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 343 | len += pad; |
| 344 | remaining -= pad; |
| 345 | |
| 346 | *content_size = len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 347 | return 0; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 348 | } |
| 349 | |
Hanno Becker | 581bc1b | 2020-05-04 12:20:03 +0100 | [diff] [blame] | 350 | /* This function parses a (D)TLSInnerPlaintext structure. |
| 351 | * See ssl_build_inner_plaintext() for details. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 352 | static int ssl_parse_inner_plaintext(unsigned char const *content, |
| 353 | size_t *content_size, |
| 354 | uint8_t *rec_type) |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 355 | { |
| 356 | size_t remaining = *content_size; |
| 357 | |
| 358 | /* Determine length of padding by skipping zeroes from the back. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 359 | do { |
| 360 | if (remaining == 0) |
| 361 | return -1; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 362 | remaining--; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 363 | } while (content[remaining] == 0); |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 364 | |
| 365 | *content_size = remaining; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 366 | *rec_type = content[remaining]; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 367 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 368 | return 0; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 369 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 370 | # endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID || \ |
| 371 | MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 372 | |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 373 | /* `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] | 374 | * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 375 | static void ssl_extract_add_data_from_record(unsigned char *add_data, |
| 376 | size_t *add_data_len, |
| 377 | mbedtls_record *rec, |
| 378 | unsigned minor_ver) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 379 | { |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 380 | /* Quoting RFC 5246 (TLS 1.2): |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 381 | * |
| 382 | * additional_data = seq_num + TLSCompressed.type + |
| 383 | * TLSCompressed.version + TLSCompressed.length; |
| 384 | * |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 385 | * For the CID extension, this is extended as follows |
| 386 | * (quoting draft-ietf-tls-dtls-connection-id-05, |
| 387 | * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05): |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 388 | * |
| 389 | * additional_data = seq_num + DTLSPlaintext.type + |
| 390 | * DTLSPlaintext.version + |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 391 | * cid + |
| 392 | * cid_length + |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 393 | * length_of_DTLSInnerPlaintext; |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 394 | * |
| 395 | * For TLS 1.3, the record sequence number is dropped from the AAD |
| 396 | * and encoded within the nonce of the AEAD operation instead. |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 397 | */ |
| 398 | |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 399 | unsigned char *cur = add_data; |
| 400 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 401 | # if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
| 402 | if (minor_ver != MBEDTLS_SSL_MINOR_VERSION_4) |
| 403 | # endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 404 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 405 | ((void)minor_ver); |
| 406 | memcpy(cur, rec->ctr, sizeof(rec->ctr)); |
| 407 | cur += sizeof(rec->ctr); |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | *cur = rec->type; |
| 411 | cur++; |
| 412 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 413 | memcpy(cur, rec->ver, sizeof(rec->ver)); |
| 414 | cur += sizeof(rec->ver); |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 415 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 416 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 417 | if (rec->cid_len != 0) { |
| 418 | memcpy(cur, rec->cid, rec->cid_len); |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 419 | cur += rec->cid_len; |
| 420 | |
| 421 | *cur = rec->cid_len; |
| 422 | cur++; |
| 423 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 424 | cur[0] = (rec->data_len >> 8) & 0xFF; |
| 425 | cur[1] = (rec->data_len >> 0) & 0xFF; |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 426 | cur += 2; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 427 | } else |
| 428 | # endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 95e4bbc | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 429 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 430 | cur[0] = (rec->data_len >> 8) & 0xFF; |
| 431 | cur[1] = (rec->data_len >> 0) & 0xFF; |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 432 | cur += 2; |
Hanno Becker | 95e4bbc | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 433 | } |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 434 | |
| 435 | *add_data_len = cur - add_data; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 438 | # if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C) || \ |
| 439 | defined(MBEDTLS_CHACHAPOLY_C) |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 440 | static int ssl_transform_aead_dynamic_iv_is_explicit( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 441 | mbedtls_ssl_transform const *transform) |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 442 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 443 | return transform->ivlen != transform->fixed_ivlen; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 444 | } |
| 445 | |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 446 | /* Compute IV := ( fixed_iv || 0 ) XOR ( 0 || dynamic_IV ) |
| 447 | * |
| 448 | * Concretely, this occurs in two variants: |
| 449 | * |
| 450 | * a) Fixed and dynamic IV lengths add up to total IV length, giving |
| 451 | * IV = fixed_iv || dynamic_iv |
| 452 | * |
Hanno Becker | 1595281 | 2020-06-04 13:31:46 +0100 | [diff] [blame] | 453 | * This variant is used in TLS 1.2 when used with GCM or CCM. |
| 454 | * |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 455 | * b) Fixed IV lengths matches total IV length, giving |
| 456 | * IV = fixed_iv XOR ( 0 || dynamic_iv ) |
Hanno Becker | 1595281 | 2020-06-04 13:31:46 +0100 | [diff] [blame] | 457 | * |
| 458 | * This variant occurs in TLS 1.3 and for TLS 1.2 when using ChaChaPoly. |
| 459 | * |
| 460 | * See also the documentation of mbedtls_ssl_transform. |
Hanno Becker | f486e28 | 2020-06-04 13:33:08 +0100 | [diff] [blame] | 461 | * |
| 462 | * This function has the precondition that |
| 463 | * |
| 464 | * dst_iv_len >= max( fixed_iv_len, dynamic_iv_len ) |
| 465 | * |
| 466 | * which has to be ensured by the caller. If this precondition |
| 467 | * violated, the behavior of this function is undefined. |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 468 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 469 | static void ssl_build_record_nonce(unsigned char *dst_iv, |
| 470 | size_t dst_iv_len, |
| 471 | unsigned char const *fixed_iv, |
| 472 | size_t fixed_iv_len, |
| 473 | unsigned char const *dynamic_iv, |
| 474 | size_t dynamic_iv_len) |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 475 | { |
| 476 | size_t i; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 477 | |
| 478 | /* Start with Fixed IV || 0 */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 479 | memset(dst_iv, 0, dst_iv_len); |
| 480 | memcpy(dst_iv, fixed_iv, fixed_iv_len); |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 481 | |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 482 | dst_iv += dst_iv_len - dynamic_iv_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 483 | for (i = 0; i < dynamic_iv_len; i++) |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 484 | dst_iv[i] ^= dynamic_iv[i]; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 485 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 486 | # endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 487 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 488 | int mbedtls_ssl_encrypt_buf(mbedtls_ssl_context *ssl, |
| 489 | mbedtls_ssl_transform *transform, |
| 490 | mbedtls_record *rec, |
| 491 | int (*f_rng)(void *, unsigned char *, size_t), |
| 492 | void *p_rng) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 493 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 494 | mbedtls_cipher_mode_t mode; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 495 | int auth_done = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 496 | unsigned char *data; |
| 497 | unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX]; |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 498 | size_t add_data_len; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 499 | size_t post_avail; |
| 500 | |
| 501 | /* The SSL context is only used for debugging purposes! */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 502 | # if !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | a7505d1 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 503 | ssl = NULL; /* make sure we don't use it except for debug */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 504 | ((void)ssl); |
| 505 | # endif |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 506 | |
| 507 | /* The PRNG is used for dynamic IV generation that's used |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 508 | * for CBC transformations in TLS 1.2. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 509 | # if !(defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) && \ |
| 510 | defined(MBEDTLS_SSL_PROTO_TLS1_2)) |
| 511 | ((void)f_rng); |
| 512 | ((void)p_rng); |
| 513 | # endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 514 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 515 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> encrypt buf")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 516 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 517 | if (transform == NULL) { |
| 518 | MBEDTLS_SSL_DEBUG_MSG(1, ("no transform provided to encrypt_buf")); |
| 519 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 520 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 521 | if (rec == NULL || rec->buf == NULL || rec->buf_len < rec->data_offset || |
| 522 | rec->buf_len - rec->data_offset < rec->data_len |
| 523 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 524 | || rec->cid_len != 0 |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 525 | # endif |
| 526 | ) { |
| 527 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 528 | ("bad record structure provided to encrypt_buf")); |
| 529 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 530 | } |
| 531 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 532 | data = rec->buf + rec->data_offset; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 533 | post_avail = rec->buf_len - (rec->data_len + rec->data_offset); |
| 534 | MBEDTLS_SSL_DEBUG_BUF(4, "before encrypt: output payload", data, |
| 535 | rec->data_len); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 536 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 537 | mode = mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 538 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 539 | if (rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN) { |
| 540 | MBEDTLS_SSL_DEBUG_MSG(1, ("Record content %" MBEDTLS_PRINTF_SIZET |
| 541 | " too large, maximum %" MBEDTLS_PRINTF_SIZET, |
| 542 | rec->data_len, |
| 543 | (size_t)MBEDTLS_SSL_OUT_CONTENT_LEN)); |
| 544 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 545 | } |
Manuel Pégourié-Gonnard | 60346be | 2014-11-21 11:38:37 +0100 | [diff] [blame] | 546 | |
Hanno Becker | 9231340 | 2020-05-20 13:58:58 +0100 | [diff] [blame] | 547 | /* The following two code paths implement the (D)TLSInnerPlaintext |
| 548 | * structure present in TLS 1.3 and DTLS 1.2 + CID. |
| 549 | * |
| 550 | * See ssl_build_inner_plaintext() for more information. |
| 551 | * |
| 552 | * Note that this changes `rec->data_len`, and hence |
| 553 | * `post_avail` needs to be recalculated afterwards. |
| 554 | * |
| 555 | * Note also that the two code paths cannot occur simultaneously |
| 556 | * since they apply to different versions of the protocol. There |
| 557 | * is hence no risk of double-addition of the inner plaintext. |
| 558 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 559 | # if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
| 560 | if (transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4) { |
| 561 | size_t padding = ssl_compute_padding_length( |
| 562 | rec->data_len, MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY); |
| 563 | if (ssl_build_inner_plaintext(data, &rec->data_len, post_avail, |
| 564 | rec->type, padding) != 0) { |
| 565 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | rec->type = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
| 569 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 570 | # endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 571 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 572 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 573 | /* |
| 574 | * Add CID information |
| 575 | */ |
| 576 | rec->cid_len = transform->out_cid_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 577 | memcpy(rec->cid, transform->out_cid, transform->out_cid_len); |
| 578 | MBEDTLS_SSL_DEBUG_BUF(3, "CID", rec->cid, rec->cid_len); |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 579 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 580 | if (rec->cid_len != 0) { |
| 581 | size_t padding = ssl_compute_padding_length( |
| 582 | rec->data_len, MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY); |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 583 | /* |
Hanno Becker | 07dc97d | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 584 | * Wrap plaintext into DTLSInnerPlaintext structure. |
Hanno Becker | 581bc1b | 2020-05-04 12:20:03 +0100 | [diff] [blame] | 585 | * See ssl_build_inner_plaintext() for more information. |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 586 | * |
Hanno Becker | 07dc97d | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 587 | * Note that this changes `rec->data_len`, and hence |
| 588 | * `post_avail` needs to be recalculated afterwards. |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 589 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 590 | if (ssl_build_inner_plaintext(data, &rec->data_len, post_avail, |
| 591 | rec->type, padding) != 0) { |
| 592 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | rec->type = MBEDTLS_SSL_MSG_CID; |
| 596 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 597 | # endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 598 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 599 | post_avail = rec->buf_len - (rec->data_len + rec->data_offset); |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 600 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 601 | /* |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 602 | * Add MAC before if needed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 603 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 604 | # if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 605 | if (mode == MBEDTLS_MODE_STREAM || |
| 606 | (mode == MBEDTLS_MODE_CBC |
| 607 | # if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 608 | && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED |
| 609 | # endif |
| 610 | )) { |
| 611 | if (post_avail < transform->maclen) { |
| 612 | MBEDTLS_SSL_DEBUG_MSG( |
| 613 | 1, ("Buffer provided for encrypted record not large enough")); |
| 614 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 615 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 616 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 617 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 618 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 619 | ssl_extract_add_data_from_record(add_data, &add_data_len, rec, |
| 620 | transform->minor_ver); |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 621 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 622 | mbedtls_md_hmac_update(&transform->md_ctx_enc, add_data, add_data_len); |
| 623 | mbedtls_md_hmac_update(&transform->md_ctx_enc, data, rec->data_len); |
| 624 | mbedtls_md_hmac_finish(&transform->md_ctx_enc, mac); |
| 625 | mbedtls_md_hmac_reset(&transform->md_ctx_enc); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 626 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 627 | memcpy(data + rec->data_len, mac, transform->maclen); |
| 628 | # endif |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 629 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 630 | MBEDTLS_SSL_DEBUG_BUF(4, "computed mac", data + rec->data_len, |
| 631 | transform->maclen); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 632 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 633 | rec->data_len += transform->maclen; |
| 634 | post_avail -= transform->maclen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 635 | auth_done++; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 636 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 637 | # endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 638 | |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 639 | /* |
| 640 | * Encrypt |
| 641 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 642 | # if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM) |
| 643 | if (mode == MBEDTLS_MODE_STREAM) { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 644 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 645 | size_t olen; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 646 | MBEDTLS_SSL_DEBUG_MSG( |
| 647 | 3, ("before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", " |
| 648 | "including %d bytes of padding", |
| 649 | rec->data_len, 0)); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 650 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 651 | if ((ret = mbedtls_cipher_crypt( |
| 652 | &transform->cipher_ctx_enc, transform->iv_enc, |
| 653 | transform->ivlen, data, rec->data_len, data, &olen)) != 0) { |
| 654 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret); |
| 655 | return ret; |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 656 | } |
| 657 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 658 | if (rec->data_len != olen) { |
| 659 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 660 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 661 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 662 | } else |
| 663 | # endif /* MBEDTLS_SSL_SOME_SUITES_USE_STREAM */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 664 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 665 | # if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C) || \ |
| 666 | defined(MBEDTLS_CHACHAPOLY_C) |
| 667 | if (mode == MBEDTLS_MODE_GCM || mode == MBEDTLS_MODE_CCM || |
| 668 | mode == MBEDTLS_MODE_CHACHAPOLY) { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 669 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 670 | unsigned char iv[12]; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 671 | unsigned char *dynamic_iv; |
| 672 | size_t dynamic_iv_len; |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 673 | int dynamic_iv_is_explicit = |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 674 | ssl_transform_aead_dynamic_iv_is_explicit(transform); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 675 | |
Hanno Becker | bd5ed1d | 2020-05-21 15:26:39 +0100 | [diff] [blame] | 676 | /* Check that there's space for the authentication tag. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 677 | if (post_avail < transform->taglen) { |
| 678 | MBEDTLS_SSL_DEBUG_MSG( |
| 679 | 1, ("Buffer provided for encrypted record not large enough")); |
| 680 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 681 | } |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 682 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 683 | /* |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 684 | * Build nonce for AEAD encryption. |
| 685 | * |
| 686 | * Note: In the case of CCM and GCM in TLS 1.2, the dynamic |
| 687 | * part of the IV is prepended to the ciphertext and |
| 688 | * can be chosen freely - in particular, it need not |
| 689 | * agree with the record sequence number. |
| 690 | * However, since ChaChaPoly as well as all AEAD modes |
| 691 | * in TLS 1.3 use the record sequence number as the |
| 692 | * dynamic part of the nonce, we uniformly use the |
| 693 | * record sequence number here in all cases. |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 694 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 695 | dynamic_iv = rec->ctr; |
| 696 | dynamic_iv_len = sizeof(rec->ctr); |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 697 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 698 | ssl_build_record_nonce(iv, sizeof(iv), transform->iv_enc, |
| 699 | transform->fixed_ivlen, dynamic_iv, |
| 700 | dynamic_iv_len); |
Manuel Pégourié-Gonnard | d056ce0 | 2014-10-29 22:29:20 +0100 | [diff] [blame] | 701 | |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 702 | /* |
| 703 | * Build additional data for AEAD encryption. |
| 704 | * This depends on the TLS version. |
| 705 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 706 | ssl_extract_add_data_from_record(add_data, &add_data_len, rec, |
| 707 | transform->minor_ver); |
Hanno Becker | 1f10d76 | 2019-04-26 13:34:37 +0100 | [diff] [blame] | 708 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 709 | MBEDTLS_SSL_DEBUG_BUF(4, "IV used (internal)", iv, transform->ivlen); |
| 710 | MBEDTLS_SSL_DEBUG_BUF(4, "IV used (transmitted)", dynamic_iv, |
| 711 | dynamic_iv_is_explicit ? dynamic_iv_len : 0); |
| 712 | MBEDTLS_SSL_DEBUG_BUF(4, "additional data used for AEAD", add_data, |
| 713 | add_data_len); |
| 714 | MBEDTLS_SSL_DEBUG_MSG( |
| 715 | 3, ("before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", " |
| 716 | "including 0 bytes of padding", |
| 717 | rec->data_len)); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 718 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 719 | /* |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 720 | * Encrypt and authenticate |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 721 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 722 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 723 | if ((ret = mbedtls_cipher_auth_encrypt_ext( |
| 724 | &transform->cipher_ctx_enc, iv, transform->ivlen, add_data, |
| 725 | add_data_len, data, rec->data_len, /* src */ |
| 726 | data, rec->buf_len - (data - rec->buf), /* dst */ |
| 727 | &rec->data_len, transform->taglen)) != 0) { |
| 728 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_auth_encrypt_ext", ret); |
| 729 | return ret; |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 730 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 731 | MBEDTLS_SSL_DEBUG_BUF(4, "after encrypt: tag", |
| 732 | data + rec->data_len - transform->taglen, |
| 733 | transform->taglen); |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 734 | /* Account for authentication tag. */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 735 | post_avail -= transform->taglen; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 736 | |
| 737 | /* |
| 738 | * Prefix record content with dynamic IV in case it is explicit. |
| 739 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 740 | if (dynamic_iv_is_explicit != 0) { |
| 741 | if (rec->data_offset < dynamic_iv_len) { |
| 742 | MBEDTLS_SSL_DEBUG_MSG( |
| 743 | 1, |
| 744 | ("Buffer provided for encrypted record not large enough")); |
| 745 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 746 | } |
| 747 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 748 | memcpy(data - dynamic_iv_len, dynamic_iv, dynamic_iv_len); |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 749 | rec->data_offset -= dynamic_iv_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 750 | rec->data_len += dynamic_iv_len; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 751 | } |
| 752 | |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 753 | auth_done++; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 754 | } else |
| 755 | # endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
| 756 | # if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) |
| 757 | if (mode == MBEDTLS_MODE_CBC) { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 758 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 759 | size_t padlen, i; |
| 760 | size_t olen; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 761 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 762 | /* Currently we're always using minimal padding |
| 763 | * (up to 255 bytes would be allowed). */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 764 | padlen = transform->ivlen - (rec->data_len + 1) % transform->ivlen; |
| 765 | if (padlen == transform->ivlen) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 766 | padlen = 0; |
| 767 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 768 | /* Check there's enough space in the buffer for the padding. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 769 | if (post_avail < padlen + 1) { |
| 770 | MBEDTLS_SSL_DEBUG_MSG( |
| 771 | 1, ("Buffer provided for encrypted record not large enough")); |
| 772 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 775 | for (i = 0; i <= padlen; i++) |
| 776 | data[rec->data_len + i] = (unsigned char)padlen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 777 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 778 | rec->data_len += padlen + 1; |
| 779 | post_avail -= padlen + 1; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 780 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 781 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 782 | /* |
TRodziewicz | 2d8800e | 2021-05-13 19:14:19 +0200 | [diff] [blame] | 783 | * Prepend per-record IV for block cipher in TLS v1.2 as per |
Paul Bakker | 1ef83d6 | 2012-04-11 12:09:53 +0000 | [diff] [blame] | 784 | * Method 1 (6.2.3.2. in RFC4346 and RFC5246) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 785 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 786 | if (f_rng == NULL) { |
| 787 | MBEDTLS_SSL_DEBUG_MSG( |
| 788 | 1, ("No PRNG provided to encrypt_record routine")); |
| 789 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 790 | } |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 791 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 792 | if (rec->data_offset < transform->ivlen) { |
| 793 | MBEDTLS_SSL_DEBUG_MSG( |
| 794 | 1, ("Buffer provided for encrypted record not large enough")); |
| 795 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | /* |
| 799 | * Generate IV |
| 800 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 801 | ret = f_rng(p_rng, transform->iv_enc, transform->ivlen); |
| 802 | if (ret != 0) |
| 803 | return ret; |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 804 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 805 | memcpy(data - transform->ivlen, transform->iv_enc, transform->ivlen); |
| 806 | # endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 807 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 808 | MBEDTLS_SSL_DEBUG_MSG( |
| 809 | 3, ("before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", " |
| 810 | "including %" MBEDTLS_PRINTF_SIZET |
| 811 | " bytes of IV and %" MBEDTLS_PRINTF_SIZET " bytes of padding", |
| 812 | rec->data_len, transform->ivlen, padlen + 1)); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 813 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 814 | if ((ret = mbedtls_cipher_crypt( |
| 815 | &transform->cipher_ctx_enc, transform->iv_enc, |
| 816 | transform->ivlen, data, rec->data_len, data, &olen)) != 0) { |
| 817 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret); |
| 818 | return ret; |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 819 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 820 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 821 | if (rec->data_len != olen) { |
| 822 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 823 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 824 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 825 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 826 | data -= transform->ivlen; |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 827 | rec->data_offset -= transform->ivlen; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 828 | rec->data_len += transform->ivlen; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 829 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 830 | # if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 831 | if (auth_done == 0) { |
Hanno Becker | 3d8c907 | 2018-01-05 16:24:22 +0000 | [diff] [blame] | 832 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; |
| 833 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 834 | /* |
| 835 | * MAC(MAC_write_key, seq_num + |
| 836 | * TLSCipherText.type + |
| 837 | * TLSCipherText.version + |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 838 | * length_of( (IV +) ENC(...) ) + |
TRodziewicz | 2abf03c | 2021-06-25 14:40:09 +0200 | [diff] [blame] | 839 | * IV + |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 840 | * ENC(content + padding + padding_length)); |
| 841 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 842 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 843 | if (post_avail < transform->maclen) { |
| 844 | MBEDTLS_SSL_DEBUG_MSG( |
| 845 | 1, |
| 846 | ("Buffer provided for encrypted record not large enough")); |
| 847 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 848 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 849 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 850 | ssl_extract_add_data_from_record(add_data, &add_data_len, rec, |
| 851 | transform->minor_ver); |
Hanno Becker | 1f10d76 | 2019-04-26 13:34:37 +0100 | [diff] [blame] | 852 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 853 | MBEDTLS_SSL_DEBUG_MSG(3, ("using encrypt then mac")); |
| 854 | MBEDTLS_SSL_DEBUG_BUF(4, "MAC'd meta-data", add_data, add_data_len); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 855 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 856 | mbedtls_md_hmac_update(&transform->md_ctx_enc, add_data, |
| 857 | add_data_len); |
| 858 | mbedtls_md_hmac_update(&transform->md_ctx_enc, data, rec->data_len); |
| 859 | mbedtls_md_hmac_finish(&transform->md_ctx_enc, mac); |
| 860 | mbedtls_md_hmac_reset(&transform->md_ctx_enc); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 861 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 862 | memcpy(data + rec->data_len, mac, transform->maclen); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 863 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 864 | rec->data_len += transform->maclen; |
| 865 | post_avail -= transform->maclen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 866 | auth_done++; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 867 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 868 | # endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
| 869 | } else |
| 870 | # endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */ |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 871 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 872 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 873 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 874 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 875 | |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 876 | /* Make extra sure authentication was performed, exactly once */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 877 | if (auth_done != 1) { |
| 878 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 879 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 880 | } |
| 881 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 882 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= encrypt buf")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 883 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 884 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 887 | # if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC) |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 888 | /* |
Manuel Pégourié-Gonnard | 6e2a9a7 | 2020-08-25 10:01:00 +0200 | [diff] [blame] | 889 | * Turn a bit into a mask: |
| 890 | * - if bit == 1, return the all-bits 1 mask, aka (size_t) -1 |
| 891 | * - if bit == 0, return the all-bits 0 mask, aka 0 |
Manuel Pégourié-Gonnard | 6d6f8a4 | 2020-09-25 09:56:53 +0200 | [diff] [blame] | 892 | * |
| 893 | * This function can be used to write constant-time code by replacing branches |
| 894 | * with bit operations using masks. |
| 895 | * |
| 896 | * This function is implemented without using comparison operators, as those |
| 897 | * might be translated to branches by some compilers on some platforms. |
Manuel Pégourié-Gonnard | 6e2a9a7 | 2020-08-25 10:01:00 +0200 | [diff] [blame] | 898 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 899 | static size_t mbedtls_ssl_cf_mask_from_bit(size_t bit) |
Manuel Pégourié-Gonnard | 6e2a9a7 | 2020-08-25 10:01:00 +0200 | [diff] [blame] | 900 | { |
| 901 | /* MSVC has a warning about unary minus on unsigned integer types, |
| 902 | * but this is well-defined and precisely what we want to do here. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 903 | # if defined(_MSC_VER) |
| 904 | # pragma warning(push) |
| 905 | # pragma warning(disable : 4146) |
| 906 | # endif |
Manuel Pégourié-Gonnard | 6e2a9a7 | 2020-08-25 10:01:00 +0200 | [diff] [blame] | 907 | return -bit; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 908 | # if defined(_MSC_VER) |
| 909 | # pragma warning(pop) |
| 910 | # endif |
Manuel Pégourié-Gonnard | 6e2a9a7 | 2020-08-25 10:01:00 +0200 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | /* |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 914 | * Constant-flow mask generation for "less than" comparison: |
| 915 | * - if x < y, return all bits 1, that is (size_t) -1 |
| 916 | * - otherwise, return all bits 0, that is 0 |
| 917 | * |
Manuel Pégourié-Gonnard | 6d6f8a4 | 2020-09-25 09:56:53 +0200 | [diff] [blame] | 918 | * This function can be used to write constant-time code by replacing branches |
| 919 | * with bit operations using masks. |
| 920 | * |
| 921 | * This function is implemented without using comparison operators, as those |
| 922 | * might be translated to branches by some compilers on some platforms. |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 923 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 924 | static size_t mbedtls_ssl_cf_mask_lt(size_t x, size_t y) |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 925 | { |
Manuel Pégourié-Gonnard | 6d6f8a4 | 2020-09-25 09:56:53 +0200 | [diff] [blame] | 926 | /* This has the most significant bit set if and only if x < y */ |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 927 | const size_t sub = x - y; |
| 928 | |
Manuel Pégourié-Gonnard | 6d6f8a4 | 2020-09-25 09:56:53 +0200 | [diff] [blame] | 929 | /* sub1 = (x < y) ? 1 : 0 */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 930 | const size_t sub1 = sub >> (sizeof(sub) * 8 - 1); |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 931 | |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 932 | /* mask = (x < y) ? 0xff... : 0x00... */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 933 | const size_t mask = mbedtls_ssl_cf_mask_from_bit(sub1); |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 934 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 935 | return mask; |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | /* |
| 939 | * Constant-flow mask generation for "greater or equal" comparison: |
| 940 | * - if x >= y, return all bits 1, that is (size_t) -1 |
| 941 | * - otherwise, return all bits 0, that is 0 |
| 942 | * |
Manuel Pégourié-Gonnard | 6d6f8a4 | 2020-09-25 09:56:53 +0200 | [diff] [blame] | 943 | * This function can be used to write constant-time code by replacing branches |
| 944 | * with bit operations using masks. |
| 945 | * |
| 946 | * This function is implemented without using comparison operators, as those |
| 947 | * might be translated to branches by some compilers on some platforms. |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 948 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 949 | static size_t mbedtls_ssl_cf_mask_ge(size_t x, size_t y) |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 950 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 951 | return ~mbedtls_ssl_cf_mask_lt(x, y); |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | /* |
| 955 | * Constant-flow boolean "equal" comparison: |
| 956 | * return x == y |
| 957 | * |
Manuel Pégourié-Gonnard | 6d6f8a4 | 2020-09-25 09:56:53 +0200 | [diff] [blame] | 958 | * This function can be used to write constant-time code by replacing branches |
| 959 | * with bit operations - it can be used in conjunction with |
| 960 | * mbedtls_ssl_cf_mask_from_bit(). |
| 961 | * |
| 962 | * This function is implemented without using comparison operators, as those |
| 963 | * might be translated to branches by some compilers on some platforms. |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 964 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 965 | static size_t mbedtls_ssl_cf_bool_eq(size_t x, size_t y) |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 966 | { |
| 967 | /* diff = 0 if x == y, non-zero otherwise */ |
| 968 | const size_t diff = x ^ y; |
| 969 | |
| 970 | /* MSVC has a warning about unary minus on unsigned integer types, |
| 971 | * but this is well-defined and precisely what we want to do here. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 972 | # if defined(_MSC_VER) |
| 973 | # pragma warning(push) |
| 974 | # pragma warning(disable : 4146) |
| 975 | # endif |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 976 | |
| 977 | /* diff_msb's most significant bit is equal to x != y */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 978 | const size_t diff_msb = (diff | -diff); |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 979 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 980 | # if defined(_MSC_VER) |
| 981 | # pragma warning(pop) |
| 982 | # endif |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 983 | |
Manuel Pégourié-Gonnard | 6d6f8a4 | 2020-09-25 09:56:53 +0200 | [diff] [blame] | 984 | /* diff1 = (x != y) ? 1 : 0 */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 985 | const size_t diff1 = diff_msb >> (sizeof(diff_msb) * 8 - 1); |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 986 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 987 | return 1 ^ diff1; |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | /* |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 991 | * Constant-flow conditional memcpy: |
| 992 | * - if c1 == c2, equivalent to memcpy(dst, src, len), |
| 993 | * - otherwise, a no-op, |
| 994 | * but with execution flow independent of the values of c1 and c2. |
| 995 | * |
Manuel Pégourié-Gonnard | 6d6f8a4 | 2020-09-25 09:56:53 +0200 | [diff] [blame] | 996 | * This function is implemented without using comparison operators, as those |
| 997 | * might be translated to branches by some compilers on some platforms. |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 998 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 999 | static void mbedtls_ssl_cf_memcpy_if_eq(unsigned char *dst, |
| 1000 | const unsigned char *src, |
| 1001 | size_t len, |
| 1002 | size_t c1, |
| 1003 | size_t c2) |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1004 | { |
Manuel Pégourié-Gonnard | 6e2a9a7 | 2020-08-25 10:01:00 +0200 | [diff] [blame] | 1005 | /* mask = c1 == c2 ? 0xff : 0x00 */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1006 | const size_t equal = mbedtls_ssl_cf_bool_eq(c1, c2); |
| 1007 | const unsigned char mask = |
| 1008 | (unsigned char)mbedtls_ssl_cf_mask_from_bit(equal); |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1009 | |
Manuel Pégourié-Gonnard | 6d6f8a4 | 2020-09-25 09:56:53 +0200 | [diff] [blame] | 1010 | /* dst[i] = c1 == c2 ? src[i] : dst[i] */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1011 | for (size_t i = 0; i < len; i++) |
| 1012 | dst[i] = (src[i] & mask) | (dst[i] & ~mask); |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | /* |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 1016 | * Compute HMAC of variable-length data with constant flow. |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1017 | * |
| 1018 | * Only works with MD-5, SHA-1, SHA-256 and SHA-384. |
| 1019 | * (Otherwise, computation of block_size needs to be adapted.) |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 1020 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1021 | MBEDTLS_STATIC_TESTABLE int mbedtls_ssl_cf_hmac(mbedtls_md_context_t *ctx, |
| 1022 | const unsigned char *add_data, |
| 1023 | size_t add_data_len, |
| 1024 | const unsigned char *data, |
| 1025 | size_t data_len_secret, |
| 1026 | size_t min_data_len, |
| 1027 | size_t max_data_len, |
| 1028 | unsigned char *output) |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 1029 | { |
Manuel Pégourié-Gonnard | 8aa29e3 | 2020-07-07 12:30:39 +0200 | [diff] [blame] | 1030 | /* |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1031 | * This function breaks the HMAC abstraction and uses the md_clone() |
| 1032 | * extension to the MD API in order to get constant-flow behaviour. |
Manuel Pégourié-Gonnard | 8aa29e3 | 2020-07-07 12:30:39 +0200 | [diff] [blame] | 1033 | * |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1034 | * HMAC(msg) is defined as HASH(okey + HASH(ikey + msg)) where + means |
Manuel Pégourié-Gonnard | baccf80 | 2020-07-22 10:37:27 +0200 | [diff] [blame] | 1035 | * concatenation, and okey/ikey are the XOR of the key with some fixed bit |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1036 | * patterns (see RFC 2104, sec. 2), which are stored in ctx->hmac_ctx. |
Manuel Pégourié-Gonnard | 8aa29e3 | 2020-07-07 12:30:39 +0200 | [diff] [blame] | 1037 | * |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1038 | * We'll first compute inner_hash = HASH(ikey + msg) by hashing up to |
| 1039 | * minlen, then cloning the context, and for each byte up to maxlen |
| 1040 | * finishing up the hash computation, keeping only the correct result. |
Manuel Pégourié-Gonnard | 8aa29e3 | 2020-07-07 12:30:39 +0200 | [diff] [blame] | 1041 | * |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1042 | * Then we only need to compute HASH(okey + inner_hash) and we're done. |
Manuel Pégourié-Gonnard | 8aa29e3 | 2020-07-07 12:30:39 +0200 | [diff] [blame] | 1043 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1044 | const mbedtls_md_type_t md_alg = mbedtls_md_get_type(ctx->md_info); |
TRodziewicz | 2abf03c | 2021-06-25 14:40:09 +0200 | [diff] [blame] | 1045 | /* TLS 1.2 only supports SHA-384, SHA-256, SHA-1, MD-5, |
Manuel Pégourié-Gonnard | baccf80 | 2020-07-22 10:37:27 +0200 | [diff] [blame] | 1046 | * all of which have the same block size except SHA-384. */ |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1047 | const size_t block_size = md_alg == MBEDTLS_MD_SHA384 ? 128 : 64; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1048 | const unsigned char *const ikey = ctx->hmac_ctx; |
| 1049 | const unsigned char *const okey = ikey + block_size; |
| 1050 | const size_t hash_size = mbedtls_md_get_size(ctx->md_info); |
Manuel Pégourié-Gonnard | 8aa29e3 | 2020-07-07 12:30:39 +0200 | [diff] [blame] | 1051 | |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1052 | unsigned char aux_out[MBEDTLS_MD_MAX_SIZE]; |
| 1053 | mbedtls_md_context_t aux; |
| 1054 | size_t offset; |
Manuel Pégourié-Gonnard | e0765f3 | 2020-07-22 12:22:51 +0200 | [diff] [blame] | 1055 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8aa29e3 | 2020-07-07 12:30:39 +0200 | [diff] [blame] | 1056 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1057 | mbedtls_md_init(&aux); |
Manuel Pégourié-Gonnard | 44c9fdd | 2020-07-22 10:48:47 +0200 | [diff] [blame] | 1058 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1059 | # define MD_CHK(func_call) \ |
| 1060 | do { \ |
| 1061 | ret = (func_call); \ |
| 1062 | if (ret != 0) \ |
| 1063 | goto cleanup; \ |
| 1064 | } while (0) |
Manuel Pégourié-Gonnard | 44c9fdd | 2020-07-22 10:48:47 +0200 | [diff] [blame] | 1065 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1066 | MD_CHK(mbedtls_md_setup(&aux, ctx->md_info, 0)); |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1067 | |
| 1068 | /* After hmac_start() of hmac_reset(), ikey has already been hashed, |
| 1069 | * so we can start directly with the message */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1070 | MD_CHK(mbedtls_md_update(ctx, add_data, add_data_len)); |
| 1071 | MD_CHK(mbedtls_md_update(ctx, data, min_data_len)); |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1072 | |
| 1073 | /* For each possible length, compute the hash up to that point */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1074 | for (offset = min_data_len; offset <= max_data_len; offset++) { |
| 1075 | MD_CHK(mbedtls_md_clone(&aux, ctx)); |
| 1076 | MD_CHK(mbedtls_md_finish(&aux, aux_out)); |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1077 | /* Keep only the correct inner_hash in the output buffer */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1078 | mbedtls_ssl_cf_memcpy_if_eq(output, aux_out, hash_size, offset, |
| 1079 | data_len_secret); |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1080 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1081 | if (offset < max_data_len) |
| 1082 | MD_CHK(mbedtls_md_update(ctx, data + offset, 1)); |
Manuel Pégourié-Gonnard | 8aa29e3 | 2020-07-07 12:30:39 +0200 | [diff] [blame] | 1083 | } |
| 1084 | |
Manuel Pégourié-Gonnard | 5ca21db | 2021-05-17 12:28:08 +0200 | [diff] [blame] | 1085 | /* The context needs to finish() before it starts() again */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1086 | MD_CHK(mbedtls_md_finish(ctx, aux_out)); |
Manuel Pégourié-Gonnard | 5ca21db | 2021-05-17 12:28:08 +0200 | [diff] [blame] | 1087 | |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1088 | /* Now compute HASH(okey + inner_hash) */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1089 | MD_CHK(mbedtls_md_starts(ctx)); |
| 1090 | MD_CHK(mbedtls_md_update(ctx, okey, block_size)); |
| 1091 | MD_CHK(mbedtls_md_update(ctx, output, hash_size)); |
| 1092 | MD_CHK(mbedtls_md_finish(ctx, output)); |
Manuel Pégourié-Gonnard | 8aa29e3 | 2020-07-07 12:30:39 +0200 | [diff] [blame] | 1093 | |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1094 | /* Done, get ready for next time */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1095 | MD_CHK(mbedtls_md_hmac_reset(ctx)); |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 1096 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1097 | # undef MD_CHK |
Manuel Pégourié-Gonnard | 44c9fdd | 2020-07-22 10:48:47 +0200 | [diff] [blame] | 1098 | |
| 1099 | cleanup: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1100 | mbedtls_md_free(&aux); |
| 1101 | return ret; |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 1102 | } |
Manuel Pégourié-Gonnard | 7fe2c5f | 2020-08-18 12:02:54 +0200 | [diff] [blame] | 1103 | |
| 1104 | /* |
| 1105 | * Constant-flow memcpy from variable position in buffer. |
| 1106 | * - functionally equivalent to memcpy(dst, src + offset_secret, len) |
Manuel Pégourié-Gonnard | ba6fc97 | 2020-08-24 12:59:55 +0200 | [diff] [blame] | 1107 | * - but with execution flow independent from the value of offset_secret. |
Manuel Pégourié-Gonnard | 7fe2c5f | 2020-08-18 12:02:54 +0200 | [diff] [blame] | 1108 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1109 | MBEDTLS_STATIC_TESTABLE void |
| 1110 | mbedtls_ssl_cf_memcpy_offset(unsigned char *dst, |
| 1111 | const unsigned char *src_base, |
| 1112 | size_t offset_secret, |
| 1113 | size_t offset_min, |
| 1114 | size_t offset_max, |
| 1115 | size_t len) |
Manuel Pégourié-Gonnard | 7fe2c5f | 2020-08-18 12:02:54 +0200 | [diff] [blame] | 1116 | { |
Manuel Pégourié-Gonnard | de1cf2c5 | 2020-08-19 12:35:30 +0200 | [diff] [blame] | 1117 | size_t offset; |
| 1118 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1119 | for (offset = offset_min; offset <= offset_max; offset++) { |
| 1120 | mbedtls_ssl_cf_memcpy_if_eq(dst, src_base + offset, len, offset, |
| 1121 | offset_secret); |
Manuel Pégourié-Gonnard | de1cf2c5 | 2020-08-19 12:35:30 +0200 | [diff] [blame] | 1122 | } |
Manuel Pégourié-Gonnard | 7fe2c5f | 2020-08-18 12:02:54 +0200 | [diff] [blame] | 1123 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1124 | # endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */ |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 1125 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1126 | int mbedtls_ssl_decrypt_buf(mbedtls_ssl_context const *ssl, |
| 1127 | mbedtls_ssl_transform *transform, |
| 1128 | mbedtls_record *rec) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1129 | { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1130 | size_t olen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1131 | mbedtls_cipher_mode_t mode; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1132 | int ret, auth_done = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1133 | # if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 1134 | size_t padlen = 0, correct = 1; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1135 | # endif |
| 1136 | unsigned char *data; |
| 1137 | unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX]; |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1138 | size_t add_data_len; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1139 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1140 | # if !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | a7505d1 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 1141 | ssl = NULL; /* make sure we don't use it except for debug */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1142 | ((void)ssl); |
| 1143 | # endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1144 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1145 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> decrypt buf")); |
| 1146 | if (rec == NULL || rec->buf == NULL || rec->buf_len < rec->data_offset || |
| 1147 | rec->buf_len - rec->data_offset < rec->data_len) { |
| 1148 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 1149 | ("bad record structure provided to decrypt_buf")); |
| 1150 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1151 | } |
| 1152 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1153 | data = rec->buf + rec->data_offset; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1154 | mode = mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_dec); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1155 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1156 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1157 | /* |
| 1158 | * Match record's CID with incoming CID. |
| 1159 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1160 | if (rec->cid_len != transform->in_cid_len || |
| 1161 | memcmp(rec->cid, transform->in_cid, rec->cid_len) != 0) { |
| 1162 | return MBEDTLS_ERR_SSL_UNEXPECTED_CID; |
Hanno Becker | 938489a | 2019-05-08 13:02:22 +0100 | [diff] [blame] | 1163 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1164 | # endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1165 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1166 | # if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM) |
| 1167 | if (mode == MBEDTLS_MODE_STREAM) { |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1168 | padlen = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1169 | if ((ret = mbedtls_cipher_crypt( |
| 1170 | &transform->cipher_ctx_dec, transform->iv_dec, |
| 1171 | transform->ivlen, data, rec->data_len, data, &olen)) != 0) { |
| 1172 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret); |
| 1173 | return ret; |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1174 | } |
| 1175 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1176 | if (rec->data_len != olen) { |
| 1177 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1178 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1179 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1180 | } else |
| 1181 | # endif /* MBEDTLS_SSL_SOME_SUITES_USE_STREAM */ |
| 1182 | # if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C) || \ |
| 1183 | defined(MBEDTLS_CHACHAPOLY_C) |
| 1184 | if (mode == MBEDTLS_MODE_GCM || mode == MBEDTLS_MODE_CCM || |
| 1185 | mode == MBEDTLS_MODE_CHACHAPOLY) { |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1186 | unsigned char iv[12]; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1187 | unsigned char *dynamic_iv; |
| 1188 | size_t dynamic_iv_len; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1189 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1190 | /* |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1191 | * Extract dynamic part of nonce for AEAD decryption. |
| 1192 | * |
| 1193 | * Note: In the case of CCM and GCM in TLS 1.2, the dynamic |
| 1194 | * part of the IV is prepended to the ciphertext and |
| 1195 | * can be chosen freely - in particular, it need not |
| 1196 | * agree with the record sequence number. |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1197 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1198 | dynamic_iv_len = sizeof(rec->ctr); |
| 1199 | if (ssl_transform_aead_dynamic_iv_is_explicit(transform) == 1) { |
| 1200 | if (rec->data_len < dynamic_iv_len) { |
| 1201 | MBEDTLS_SSL_DEBUG_MSG( |
| 1202 | 1, ("msglen (%" MBEDTLS_PRINTF_SIZET |
| 1203 | " ) < explicit_iv_len (%" MBEDTLS_PRINTF_SIZET ") ", |
| 1204 | rec->data_len, dynamic_iv_len)); |
| 1205 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1206 | } |
| 1207 | dynamic_iv = data; |
| 1208 | |
| 1209 | data += dynamic_iv_len; |
| 1210 | rec->data_offset += dynamic_iv_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1211 | rec->data_len -= dynamic_iv_len; |
| 1212 | } else { |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 1213 | dynamic_iv = rec->ctr; |
| 1214 | } |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1215 | |
| 1216 | /* Check that there's space for the authentication tag. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1217 | if (rec->data_len < transform->taglen) { |
| 1218 | MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET |
| 1219 | ") < taglen (%" MBEDTLS_PRINTF_SIZET ") ", |
| 1220 | rec->data_len, transform->taglen)); |
| 1221 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 1222 | } |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1223 | rec->data_len -= transform->taglen; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1224 | |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1225 | /* |
| 1226 | * Prepare nonce from dynamic and static parts. |
| 1227 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1228 | ssl_build_record_nonce(iv, sizeof(iv), transform->iv_dec, |
| 1229 | transform->fixed_ivlen, dynamic_iv, |
| 1230 | dynamic_iv_len); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1231 | |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1232 | /* |
| 1233 | * Build additional data for AEAD encryption. |
| 1234 | * This depends on the TLS version. |
| 1235 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1236 | ssl_extract_add_data_from_record(add_data, &add_data_len, rec, |
| 1237 | transform->minor_ver); |
| 1238 | MBEDTLS_SSL_DEBUG_BUF(4, "additional data used for AEAD", add_data, |
| 1239 | add_data_len); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1240 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1241 | /* Because of the check above, we know that there are |
| 1242 | * explicit_iv_len Bytes preceeding data, and taglen |
| 1243 | * bytes following data + data_len. This justifies |
Hanno Becker | 2001665 | 2019-07-10 11:44:13 +0100 | [diff] [blame] | 1244 | * the debug message and the invocation of |
TRodziewicz | 18efb73 | 2021-04-29 23:12:19 +0200 | [diff] [blame] | 1245 | * mbedtls_cipher_auth_decrypt_ext() below. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1246 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1247 | MBEDTLS_SSL_DEBUG_BUF(4, "IV used", iv, transform->ivlen); |
| 1248 | MBEDTLS_SSL_DEBUG_BUF(4, "TAG used", data + rec->data_len, |
| 1249 | transform->taglen); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1250 | |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1251 | /* |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1252 | * Decrypt and authenticate |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1253 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1254 | if ((ret = mbedtls_cipher_auth_decrypt_ext( |
| 1255 | &transform->cipher_ctx_dec, iv, transform->ivlen, add_data, |
| 1256 | add_data_len, data, rec->data_len + transform->taglen, /* src |
| 1257 | */ |
| 1258 | data, rec->buf_len - (data - rec->buf), &olen, /* dst */ |
| 1259 | transform->taglen)) != 0) { |
| 1260 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_auth_decrypt_ext", ret); |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1261 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1262 | if (ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED) |
| 1263 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1264 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1265 | return ret; |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1266 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1267 | auth_done++; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1268 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1269 | /* Double-check that AEAD decryption doesn't change content length. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1270 | if (olen != rec->data_len) { |
| 1271 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1272 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1273 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1274 | } else |
| 1275 | # endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ |
| 1276 | # if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) |
| 1277 | if (mode == MBEDTLS_MODE_CBC) { |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 1278 | size_t minlen = 0; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1279 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1280 | /* |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1281 | * Check immediate ciphertext sanity |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1282 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1283 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 1284 | /* The ciphertext is prefixed with the CBC IV. */ |
| 1285 | minlen += transform->ivlen; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1286 | # endif |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1287 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1288 | /* Size considerations: |
| 1289 | * |
| 1290 | * - The CBC cipher text must not be empty and hence |
| 1291 | * at least of size transform->ivlen. |
| 1292 | * |
| 1293 | * Together with the potential IV-prefix, this explains |
| 1294 | * the first of the two checks below. |
| 1295 | * |
| 1296 | * - The record must contain a MAC, either in plain or |
| 1297 | * encrypted, depending on whether Encrypt-then-MAC |
| 1298 | * is used or not. |
| 1299 | * - If it is, the message contains the IV-prefix, |
| 1300 | * the CBC ciphertext, and the MAC. |
| 1301 | * - If it is not, the padded plaintext, and hence |
| 1302 | * the CBC ciphertext, has at least length maclen + 1 |
| 1303 | * because there is at least the padding length byte. |
| 1304 | * |
| 1305 | * As the CBC ciphertext is not empty, both cases give the |
| 1306 | * lower bound minlen + maclen + 1 on the record size, which |
| 1307 | * we test for in the second check below. |
| 1308 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1309 | if (rec->data_len < minlen + transform->ivlen || |
| 1310 | rec->data_len < minlen + transform->maclen + 1) { |
| 1311 | MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET |
| 1312 | ") < max( ivlen(%" MBEDTLS_PRINTF_SIZET |
| 1313 | "), maclen (%" MBEDTLS_PRINTF_SIZET ") " |
| 1314 | "+ 1 ) ( + expl IV )", |
| 1315 | rec->data_len, transform->ivlen, |
| 1316 | transform->maclen)); |
| 1317 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1318 | } |
| 1319 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1320 | /* |
| 1321 | * Authenticate before decrypt if enabled |
| 1322 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1323 | # if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1324 | if (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED) { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 1325 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD]; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1326 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1327 | MBEDTLS_SSL_DEBUG_MSG(3, ("using encrypt then mac")); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1328 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1329 | /* Update data_len in tandem with add_data. |
| 1330 | * |
| 1331 | * The subtraction is safe because of the previous check |
| 1332 | * data_len >= minlen + maclen + 1. |
| 1333 | * |
| 1334 | * Afterwards, we know that data + data_len is followed by at |
| 1335 | * least maclen Bytes, which justifies the call to |
| 1336 | * mbedtls_ssl_safer_memcmp() below. |
| 1337 | * |
| 1338 | * Further, we still know that data_len > minlen */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1339 | rec->data_len -= transform->maclen; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1340 | ssl_extract_add_data_from_record(add_data, &add_data_len, rec, |
| 1341 | transform->minor_ver); |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 1342 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1343 | /* Calculate expected MAC. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1344 | MBEDTLS_SSL_DEBUG_BUF(4, "MAC'd meta-data", add_data, add_data_len); |
| 1345 | mbedtls_md_hmac_update(&transform->md_ctx_dec, add_data, |
| 1346 | add_data_len); |
| 1347 | mbedtls_md_hmac_update(&transform->md_ctx_dec, data, rec->data_len); |
| 1348 | mbedtls_md_hmac_finish(&transform->md_ctx_dec, mac_expect); |
| 1349 | mbedtls_md_hmac_reset(&transform->md_ctx_dec); |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 1350 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1351 | MBEDTLS_SSL_DEBUG_BUF(4, "message mac", data + rec->data_len, |
| 1352 | transform->maclen); |
| 1353 | MBEDTLS_SSL_DEBUG_BUF(4, "expected mac", mac_expect, |
| 1354 | transform->maclen); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1355 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1356 | /* Compare expected MAC with MAC at the end of the record. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1357 | if (mbedtls_ssl_safer_memcmp(data + rec->data_len, mac_expect, |
| 1358 | transform->maclen) != 0) { |
| 1359 | MBEDTLS_SSL_DEBUG_MSG(1, ("message mac does not match")); |
| 1360 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1361 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1362 | auth_done++; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1363 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1364 | # endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1365 | |
| 1366 | /* |
| 1367 | * Check length sanity |
| 1368 | */ |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1369 | |
| 1370 | /* We know from above that data_len > minlen >= 0, |
| 1371 | * so the following check in particular implies that |
| 1372 | * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1373 | if (rec->data_len % transform->ivlen != 0) { |
| 1374 | MBEDTLS_SSL_DEBUG_MSG(1, ("msglen (%" MBEDTLS_PRINTF_SIZET |
| 1375 | ") %% ivlen (%" MBEDTLS_PRINTF_SIZET |
| 1376 | ") != 0", |
| 1377 | rec->data_len, transform->ivlen)); |
| 1378 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1379 | } |
| 1380 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1381 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1382 | /* |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 1383 | * Initialize for prepended IV for block cipher in TLS v1.2 |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1384 | */ |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 1385 | /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1386 | memcpy(transform->iv_dec, data, transform->ivlen); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1387 | |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 1388 | data += transform->ivlen; |
| 1389 | rec->data_offset += transform->ivlen; |
| 1390 | rec->data_len -= transform->ivlen; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1391 | # endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1392 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1393 | /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */ |
| 1394 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1395 | if ((ret = mbedtls_cipher_crypt( |
| 1396 | &transform->cipher_ctx_dec, transform->iv_dec, |
| 1397 | transform->ivlen, data, rec->data_len, data, &olen)) != 0) { |
| 1398 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret); |
| 1399 | return ret; |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1400 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1401 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1402 | /* Double-check that length hasn't changed during decryption. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1403 | if (rec->data_len != olen) { |
| 1404 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1405 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1406 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1407 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1408 | /* Safe since data_len >= minlen + maclen + 1, so after having |
| 1409 | * subtracted at most minlen and maclen up to this point, |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1410 | * data_len > 0 (because of data_len % ivlen == 0, it's actually |
| 1411 | * >= ivlen ). */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1412 | padlen = data[rec->data_len - 1]; |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1413 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1414 | if (auth_done == 1) { |
| 1415 | const size_t mask = |
| 1416 | mbedtls_ssl_cf_mask_ge(rec->data_len, padlen + 1); |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 1417 | correct &= mask; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1418 | padlen &= mask; |
| 1419 | } else { |
| 1420 | # if defined(MBEDTLS_SSL_DEBUG_ALL) |
| 1421 | if (rec->data_len < transform->maclen + padlen + 1) { |
| 1422 | MBEDTLS_SSL_DEBUG_MSG( |
| 1423 | 1, ("msglen (%" MBEDTLS_PRINTF_SIZET |
| 1424 | ") < maclen (%" MBEDTLS_PRINTF_SIZET |
| 1425 | ") + padlen (%" MBEDTLS_PRINTF_SIZET ")", |
| 1426 | rec->data_len, transform->maclen, padlen + 1)); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1427 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1428 | # endif |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1429 | |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 1430 | const size_t mask = mbedtls_ssl_cf_mask_ge( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1431 | rec->data_len, transform->maclen + padlen + 1); |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 1432 | correct &= mask; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1433 | padlen &= mask; |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1434 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1435 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1436 | padlen++; |
| 1437 | |
| 1438 | /* Regardless of the validity of the padding, |
| 1439 | * we have data_len >= padlen here. */ |
| 1440 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1441 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1442 | /* The padding check involves a series of up to 256 |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1443 | * consecutive memory reads at the end of the record |
| 1444 | * plaintext buffer. In order to hide the length and |
| 1445 | * validity of the padding, always perform exactly |
| 1446 | * `min(256,plaintext_len)` reads (but take into account |
| 1447 | * only the last `padlen` bytes for the padding check). */ |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1448 | size_t pad_count = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1449 | volatile unsigned char *const check = data; |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1450 | |
| 1451 | /* Index of first padding byte; it has been ensured above |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1452 | * that the subtraction is safe. */ |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1453 | size_t const padding_idx = rec->data_len - padlen; |
| 1454 | size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256; |
| 1455 | size_t const start_idx = rec->data_len - num_checks; |
| 1456 | size_t idx; |
| 1457 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1458 | for (idx = start_idx; idx < rec->data_len; idx++) { |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1459 | /* pad_count += (idx >= padding_idx) && |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1460 | * (check[idx] == padlen - 1); |
| 1461 | */ |
| 1462 | const size_t mask = mbedtls_ssl_cf_mask_ge(idx, padding_idx); |
| 1463 | const size_t equal = mbedtls_ssl_cf_bool_eq(check[idx], padlen - 1); |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1464 | pad_count += mask & equal; |
| 1465 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1466 | correct &= mbedtls_ssl_cf_bool_eq(pad_count, padlen); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 1467 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1468 | # if defined(MBEDTLS_SSL_DEBUG_ALL) |
| 1469 | if (padlen > 0 && correct == 0) |
| 1470 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad padding byte detected")); |
| 1471 | # endif |
| 1472 | padlen &= mbedtls_ssl_cf_mask_from_bit(correct); |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1473 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1474 | # endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1475 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1476 | /* If the padding was found to be invalid, padlen == 0 |
| 1477 | * and the subtraction is safe. If the padding was found valid, |
| 1478 | * padlen hasn't been changed and the previous assertion |
| 1479 | * data_len >= padlen still holds. */ |
| 1480 | rec->data_len -= padlen; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1481 | } else |
| 1482 | # endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */ |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 1483 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1484 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1485 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 1486 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1487 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1488 | # if defined(MBEDTLS_SSL_DEBUG_ALL) |
| 1489 | MBEDTLS_SSL_DEBUG_BUF(4, "raw buffer after decryption", data, |
| 1490 | rec->data_len); |
| 1491 | # endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1492 | |
| 1493 | /* |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1494 | * Authenticate if not done yet. |
| 1495 | * Compute the MAC regardless of the padding result (RFC4346, CBCTIME). |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1496 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1497 | # if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 1498 | if (auth_done == 0) { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 1499 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD]; |
Manuel Pégourié-Gonnard | 3c31afa | 2020-08-13 12:08:54 +0200 | [diff] [blame] | 1500 | unsigned char mac_peer[MBEDTLS_SSL_MAC_ADD]; |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 1501 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1502 | /* If the initial value of padlen was such that |
| 1503 | * data_len < maclen + padlen + 1, then padlen |
| 1504 | * got reset to 1, and the initial check |
| 1505 | * data_len >= minlen + maclen + 1 |
| 1506 | * guarantees that at this point we still |
| 1507 | * have at least data_len >= maclen. |
| 1508 | * |
| 1509 | * If the initial value of padlen was such that |
| 1510 | * data_len >= maclen + padlen + 1, then we have |
| 1511 | * subtracted either padlen + 1 (if the padding was correct) |
| 1512 | * or 0 (if the padding was incorrect) since then, |
| 1513 | * hence data_len >= maclen in any case. |
| 1514 | */ |
| 1515 | rec->data_len -= transform->maclen; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1516 | ssl_extract_add_data_from_record(add_data, &add_data_len, rec, |
| 1517 | transform->minor_ver); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1518 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1519 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1520 | /* |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1521 | * The next two sizes are the minimum and maximum values of |
| 1522 | * data_len over all padlen values. |
| 1523 | * |
| 1524 | * They're independent of padlen, since we previously did |
| 1525 | * data_len -= padlen. |
| 1526 | * |
| 1527 | * Note that max_len + maclen is never more than the buffer |
| 1528 | * length, as we previously did in_msglen -= maclen too. |
| 1529 | */ |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1530 | const size_t max_len = rec->data_len + padlen; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1531 | const size_t min_len = (max_len > 256) ? max_len - 256 : 0; |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1532 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1533 | ret = mbedtls_ssl_cf_hmac(&transform->md_ctx_dec, add_data, |
| 1534 | add_data_len, data, rec->data_len, min_len, |
| 1535 | max_len, mac_expect); |
| 1536 | if (ret != 0) { |
| 1537 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cf_hmac", ret); |
| 1538 | return ret; |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1539 | } |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1540 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1541 | mbedtls_ssl_cf_memcpy_offset(mac_peer, data, rec->data_len, min_len, |
| 1542 | max_len, transform->maclen); |
| 1543 | # endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1544 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1545 | # if defined(MBEDTLS_SSL_DEBUG_ALL) |
| 1546 | MBEDTLS_SSL_DEBUG_BUF(4, "expected mac", mac_expect, transform->maclen); |
| 1547 | MBEDTLS_SSL_DEBUG_BUF(4, "message mac", mac_peer, transform->maclen); |
| 1548 | # endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1549 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1550 | if (mbedtls_ssl_safer_memcmp(mac_peer, mac_expect, transform->maclen) != |
| 1551 | 0) { |
| 1552 | # if defined(MBEDTLS_SSL_DEBUG_ALL) |
| 1553 | MBEDTLS_SSL_DEBUG_MSG(1, ("message mac does not match")); |
| 1554 | # endif |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1555 | correct = 0; |
| 1556 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1557 | auth_done++; |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1558 | } |
Hanno Becker | dd3ab13 | 2018-10-17 14:43:14 +0100 | [diff] [blame] | 1559 | |
| 1560 | /* |
| 1561 | * Finally check the correct flag |
| 1562 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1563 | if (correct == 0) |
| 1564 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
| 1565 | # endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1566 | |
| 1567 | /* Make extra sure authentication was performed, exactly once */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1568 | if (auth_done != 1) { |
| 1569 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1570 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1571 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1572 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1573 | # if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
| 1574 | if (transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4) { |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 1575 | /* Remove inner padding and infer true content type. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1576 | ret = ssl_parse_inner_plaintext(data, &rec->data_len, &rec->type); |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 1577 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1578 | if (ret != 0) |
| 1579 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 1580 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1581 | # endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 1582 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1583 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 1584 | if (rec->cid_len != 0) { |
| 1585 | ret = ssl_parse_inner_plaintext(data, &rec->data_len, &rec->type); |
| 1586 | if (ret != 0) |
| 1587 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1588 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1589 | # endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1590 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1591 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= decrypt buf")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1592 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1593 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1596 | # undef MAC_NONE |
| 1597 | # undef MAC_PLAINTEXT |
| 1598 | # undef MAC_CIPHERTEXT |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 1599 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1600 | /* |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 1601 | * Fill the input message buffer by appending data to it. |
| 1602 | * 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] | 1603 | * |
| 1604 | * If we return 0, is it guaranteed that (at least) nb_want bytes are |
| 1605 | * available (from this read and/or a previous one). Otherwise, an error code |
| 1606 | * is returned (possibly EOF or WANT_READ). |
| 1607 | * |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 1608 | * With stream transport (TLS) on success ssl->in_left == nb_want, but |
| 1609 | * with datagram transport (DTLS) on success ssl->in_left >= nb_want, |
| 1610 | * since we always read a whole datagram at once. |
| 1611 | * |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 1612 | * 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] | 1613 | * they're done reading a record. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1614 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1615 | 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] | 1616 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1617 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1618 | size_t len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1619 | # if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1620 | size_t in_buf_len = ssl->in_buf_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1621 | # else |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1622 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1623 | # endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1624 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1625 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> fetch input")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1626 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1627 | if (ssl->f_recv == NULL && ssl->f_recv_timeout == NULL) { |
| 1628 | MBEDTLS_SSL_DEBUG_MSG(1, ("Bad usage of mbedtls_ssl_set_bio() " |
| 1629 | "or mbedtls_ssl_set_bio()")); |
| 1630 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 1631 | } |
| 1632 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1633 | if (nb_want > in_buf_len - (size_t)(ssl->in_hdr - ssl->in_buf)) { |
| 1634 | MBEDTLS_SSL_DEBUG_MSG(1, ("requesting more data than fits")); |
| 1635 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 1636 | } |
| 1637 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1638 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1639 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 1640 | uint32_t timeout; |
| 1641 | |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 1642 | /* |
| 1643 | * The point is, we need to always read a full datagram at once, so we |
| 1644 | * sometimes read more then requested, and handle the additional data. |
| 1645 | * It could be the rest of the current record (while fetching the |
| 1646 | * header) and/or some other records in the same datagram. |
| 1647 | */ |
| 1648 | |
| 1649 | /* |
| 1650 | * Move to the next record in the already read datagram if applicable |
| 1651 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1652 | if (ssl->next_record_offset != 0) { |
| 1653 | if (ssl->in_left < ssl->next_record_offset) { |
| 1654 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1655 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | ssl->in_left -= ssl->next_record_offset; |
| 1659 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1660 | if (ssl->in_left != 0) { |
| 1661 | MBEDTLS_SSL_DEBUG_MSG( |
| 1662 | 2, |
| 1663 | ("next record in same datagram, offset: %" MBEDTLS_PRINTF_SIZET, |
| 1664 | ssl->next_record_offset)); |
| 1665 | memmove(ssl->in_hdr, ssl->in_hdr + ssl->next_record_offset, |
| 1666 | ssl->in_left); |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | ssl->next_record_offset = 0; |
| 1670 | } |
| 1671 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1672 | MBEDTLS_SSL_DEBUG_MSG(2, ("in_left: %" MBEDTLS_PRINTF_SIZET |
| 1673 | ", nb_want: %" MBEDTLS_PRINTF_SIZET, |
| 1674 | ssl->in_left, nb_want)); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1675 | |
| 1676 | /* |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 1677 | * Done if we already have enough data. |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1678 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1679 | if (nb_want <= ssl->in_left) { |
| 1680 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= fetch input")); |
| 1681 | return 0; |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 1682 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1683 | |
| 1684 | /* |
Antonin Décimo | 36e89b5 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 1685 | * 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] | 1686 | * are not at the beginning of a new record, the caller did something |
| 1687 | * wrong. |
| 1688 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1689 | if (ssl->in_left != 0) { |
| 1690 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 1691 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1692 | } |
| 1693 | |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1694 | /* |
| 1695 | * Don't even try to read if time's out already. |
| 1696 | * This avoids by-passing the timer when repeatedly receiving messages |
| 1697 | * that will end up being dropped. |
| 1698 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1699 | if (mbedtls_ssl_check_timer(ssl) != 0) { |
| 1700 | MBEDTLS_SSL_DEBUG_MSG(2, ("timer has expired")); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 1701 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1702 | } else { |
| 1703 | len = in_buf_len - (ssl->in_hdr - ssl->in_buf); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1704 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1705 | if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1706 | timeout = ssl->handshake->retransmit_timeout; |
| 1707 | else |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1708 | timeout = ssl->conf->read_timeout; |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1709 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1710 | MBEDTLS_SSL_DEBUG_MSG(3, ("f_recv_timeout: %lu ms", |
| 1711 | (unsigned long)timeout)); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1712 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1713 | if (ssl->f_recv_timeout != NULL) |
| 1714 | ret = |
| 1715 | ssl->f_recv_timeout(ssl->p_bio, ssl->in_hdr, len, timeout); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1716 | else |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1717 | ret = ssl->f_recv(ssl->p_bio, ssl->in_hdr, len); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1718 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1719 | MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_recv(_timeout)", ret); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1720 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1721 | if (ret == 0) |
| 1722 | return MBEDTLS_ERR_SSL_CONN_EOF; |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1723 | } |
| 1724 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1725 | if (ret == MBEDTLS_ERR_SSL_TIMEOUT) { |
| 1726 | MBEDTLS_SSL_DEBUG_MSG(2, ("timeout")); |
| 1727 | mbedtls_ssl_set_timer(ssl, 0); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 1728 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1729 | if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 1730 | if (ssl_double_retransmit_timeout(ssl) != 0) { |
| 1731 | MBEDTLS_SSL_DEBUG_MSG(1, ("handshake timeout")); |
| 1732 | return MBEDTLS_ERR_SSL_TIMEOUT; |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 1733 | } |
| 1734 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1735 | if ((ret = mbedtls_ssl_resend(ssl)) != 0) { |
| 1736 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend", ret); |
| 1737 | return ret; |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 1738 | } |
| 1739 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1740 | return MBEDTLS_ERR_SSL_WANT_READ; |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 1741 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1742 | # if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1743 | else if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 1744 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) { |
| 1745 | if ((ret = mbedtls_ssl_resend_hello_request(ssl)) != 0) { |
| 1746 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend_hello_request", |
| 1747 | ret); |
| 1748 | return ret; |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 1749 | } |
| 1750 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1751 | return MBEDTLS_ERR_SSL_WANT_READ; |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 1752 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1753 | # endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 1754 | } |
| 1755 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1756 | if (ret < 0) |
| 1757 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1758 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1759 | ssl->in_left = ret; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1760 | } else |
| 1761 | # endif |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1762 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1763 | MBEDTLS_SSL_DEBUG_MSG(2, ("in_left: %" MBEDTLS_PRINTF_SIZET |
| 1764 | ", nb_want: %" MBEDTLS_PRINTF_SIZET, |
| 1765 | ssl->in_left, nb_want)); |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 1766 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1767 | while (ssl->in_left < nb_want) { |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1768 | len = nb_want - ssl->in_left; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1769 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1770 | if (mbedtls_ssl_check_timer(ssl) != 0) |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1771 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1772 | else { |
| 1773 | if (ssl->f_recv_timeout != NULL) { |
| 1774 | ret = ssl->f_recv_timeout(ssl->p_bio, |
| 1775 | ssl->in_hdr + ssl->in_left, len, |
| 1776 | ssl->conf->read_timeout); |
| 1777 | } else { |
| 1778 | ret = ssl->f_recv(ssl->p_bio, ssl->in_hdr + ssl->in_left, |
| 1779 | len); |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 1780 | } |
| 1781 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1782 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1783 | MBEDTLS_SSL_DEBUG_MSG(2, ("in_left: %" MBEDTLS_PRINTF_SIZET |
| 1784 | ", nb_want: %" MBEDTLS_PRINTF_SIZET, |
| 1785 | ssl->in_left, nb_want)); |
| 1786 | MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_recv(_timeout)", ret); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1787 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1788 | if (ret == 0) |
| 1789 | return MBEDTLS_ERR_SSL_CONN_EOF; |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1790 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1791 | if (ret < 0) |
| 1792 | return ret; |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1793 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1794 | if ((size_t)ret > len || |
| 1795 | (INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX)) { |
| 1796 | MBEDTLS_SSL_DEBUG_MSG( |
| 1797 | 1, |
| 1798 | ("f_recv returned %d bytes but only %" MBEDTLS_PRINTF_SIZET |
| 1799 | " were requested", |
| 1800 | ret, len)); |
| 1801 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
mohammad1603 | 5bd15cb | 2018-02-28 04:30:59 -0800 | [diff] [blame] | 1802 | } |
| 1803 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1804 | ssl->in_left += ret; |
| 1805 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1806 | } |
| 1807 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1808 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= fetch input")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1809 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1810 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | /* |
| 1814 | * Flush any data not yet written |
| 1815 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1816 | int mbedtls_ssl_flush_output(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1817 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1818 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 1819 | unsigned char *buf; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1820 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1821 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> flush output")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1822 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1823 | if (ssl->f_send == NULL) { |
| 1824 | MBEDTLS_SSL_DEBUG_MSG(1, ("Bad usage of mbedtls_ssl_set_bio() " |
| 1825 | "or mbedtls_ssl_set_bio()")); |
| 1826 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 1827 | } |
| 1828 | |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 1829 | /* Avoid incrementing counter if data is flushed */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1830 | if (ssl->out_left == 0) { |
| 1831 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= flush output")); |
| 1832 | return 0; |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 1833 | } |
| 1834 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1835 | while (ssl->out_left > 0) { |
| 1836 | MBEDTLS_SSL_DEBUG_MSG( |
| 1837 | 2, ("message length: %" MBEDTLS_PRINTF_SIZET |
| 1838 | ", out_left: %" MBEDTLS_PRINTF_SIZET, |
| 1839 | mbedtls_ssl_out_hdr_len(ssl) + ssl->out_msglen, ssl->out_left)); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1840 | |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 1841 | buf = ssl->out_hdr - ssl->out_left; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1842 | ret = ssl->f_send(ssl->p_bio, buf, ssl->out_left); |
Paul Bakker | 186751d | 2012-05-08 13:16:14 +0000 | [diff] [blame] | 1843 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1844 | MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_send", ret); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1845 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1846 | if (ret <= 0) |
| 1847 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1848 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1849 | if ((size_t)ret > ssl->out_left || |
| 1850 | (INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX)) { |
| 1851 | MBEDTLS_SSL_DEBUG_MSG( |
| 1852 | 1, ("f_send returned %d bytes but only %" MBEDTLS_PRINTF_SIZET |
| 1853 | " bytes were sent", |
| 1854 | ret, ssl->out_left)); |
| 1855 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
mohammad1603 | 4bbaeb4 | 2018-02-22 04:29:04 -0800 | [diff] [blame] | 1856 | } |
| 1857 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1858 | ssl->out_left -= ret; |
| 1859 | } |
| 1860 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1861 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1862 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 1863 | ssl->out_hdr = ssl->out_buf; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1864 | } else |
| 1865 | # endif |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 1866 | { |
| 1867 | ssl->out_hdr = ssl->out_buf + 8; |
| 1868 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1869 | mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out); |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 1870 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1871 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= flush output")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1872 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1873 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1876 | /* |
| 1877 | * Functions to handle the DTLS retransmission state machine |
| 1878 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1879 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 1880 | /* |
| 1881 | * Append current handshake message to current outgoing flight |
| 1882 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1883 | static int ssl_flight_append(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 1884 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1885 | mbedtls_ssl_flight_item *msg; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1886 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_flight_append")); |
| 1887 | MBEDTLS_SSL_DEBUG_BUF(4, "message appended to flight", ssl->out_msg, |
| 1888 | ssl->out_msglen); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 1889 | |
| 1890 | /* Allocate space for current message */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1891 | if ((msg = mbedtls_calloc(1, sizeof(mbedtls_ssl_flight_item))) == NULL) { |
| 1892 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 1893 | ("alloc %" MBEDTLS_PRINTF_SIZET " bytes failed", |
| 1894 | sizeof(mbedtls_ssl_flight_item))); |
| 1895 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 1896 | } |
| 1897 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1898 | if ((msg->p = mbedtls_calloc(1, ssl->out_msglen)) == NULL) { |
| 1899 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 1900 | ("alloc %" MBEDTLS_PRINTF_SIZET " bytes failed", |
| 1901 | ssl->out_msglen)); |
| 1902 | mbedtls_free(msg); |
| 1903 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 1904 | } |
| 1905 | |
| 1906 | /* Copy current handshake message with headers */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1907 | memcpy(msg->p, ssl->out_msg, ssl->out_msglen); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 1908 | msg->len = ssl->out_msglen; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1909 | msg->type = ssl->out_msgtype; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 1910 | msg->next = NULL; |
| 1911 | |
| 1912 | /* Append to the current flight */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1913 | if (ssl->handshake->flight == NULL) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1914 | ssl->handshake->flight = msg; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1915 | else { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1916 | mbedtls_ssl_flight_item *cur = ssl->handshake->flight; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1917 | while (cur->next != NULL) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 1918 | cur = cur->next; |
| 1919 | cur->next = msg; |
| 1920 | } |
| 1921 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1922 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_flight_append")); |
| 1923 | return 0; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 1924 | } |
| 1925 | |
| 1926 | /* |
| 1927 | * Free the current flight of handshake messages |
| 1928 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1929 | void mbedtls_ssl_flight_free(mbedtls_ssl_flight_item *flight) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 1930 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1931 | mbedtls_ssl_flight_item *cur = flight; |
| 1932 | mbedtls_ssl_flight_item *next; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 1933 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1934 | while (cur != NULL) { |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 1935 | next = cur->next; |
| 1936 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1937 | mbedtls_free(cur->p); |
| 1938 | mbedtls_free(cur); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 1939 | |
| 1940 | cur = next; |
| 1941 | } |
| 1942 | } |
| 1943 | |
| 1944 | /* |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1945 | * Swap transform_out and out_ctr with the alternative ones |
| 1946 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1947 | static int ssl_swap_epochs(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1948 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1949 | mbedtls_ssl_transform *tmp_transform; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1950 | unsigned char tmp_out_ctr[8]; |
| 1951 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1952 | if (ssl->transform_out == ssl->handshake->alt_transform_out) { |
| 1953 | MBEDTLS_SSL_DEBUG_MSG(3, ("skip swap epochs")); |
| 1954 | return 0; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1955 | } |
| 1956 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1957 | MBEDTLS_SSL_DEBUG_MSG(3, ("swap epochs")); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1958 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 1959 | /* Swap transforms */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1960 | tmp_transform = ssl->transform_out; |
| 1961 | ssl->transform_out = ssl->handshake->alt_transform_out; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1962 | ssl->handshake->alt_transform_out = tmp_transform; |
| 1963 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 1964 | /* Swap epoch + sequence_number */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1965 | memcpy(tmp_out_ctr, ssl->cur_out_ctr, 8); |
| 1966 | memcpy(ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8); |
| 1967 | memcpy(ssl->handshake->alt_out_ctr, tmp_out_ctr, 8); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 1968 | |
| 1969 | /* Adjust to the newly activated transform */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1970 | mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 1971 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1972 | return 0; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1973 | } |
| 1974 | |
| 1975 | /* |
| 1976 | * Retransmit the current flight of messages. |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 1977 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1978 | int mbedtls_ssl_resend(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 1979 | { |
| 1980 | int ret = 0; |
| 1981 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1982 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_resend")); |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 1983 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1984 | ret = mbedtls_ssl_flight_transmit(ssl); |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 1985 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1986 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_resend")); |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 1987 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1988 | return ret; |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 1989 | } |
| 1990 | |
| 1991 | /* |
| 1992 | * Transmit or retransmit the current flight of messages. |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 1993 | * |
| 1994 | * Need to remember the current message in case flush_output returns |
| 1995 | * 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] | 1996 | * 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] | 1997 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 1998 | int mbedtls_ssl_flight_transmit(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 1999 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2000 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2001 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> mbedtls_ssl_flight_transmit")); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2002 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2003 | if (ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING) { |
| 2004 | MBEDTLS_SSL_DEBUG_MSG(2, ("initialise flight transmission")); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2005 | |
| 2006 | ssl->handshake->cur_msg = ssl->handshake->flight; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2007 | ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2008 | ret = ssl_swap_epochs(ssl); |
| 2009 | if (ret != 0) |
| 2010 | return ret; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2011 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2012 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2013 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2014 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2015 | while (ssl->handshake->cur_msg != NULL) { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2016 | size_t max_frag_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2017 | const mbedtls_ssl_flight_item *const cur = ssl->handshake->cur_msg; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2018 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2019 | int const is_finished = (cur->type == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2020 | cur->p[0] == MBEDTLS_SSL_HS_FINISHED); |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 2021 | |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 2022 | uint8_t const force_flush = ssl->disable_datagram_packing == 1 ? |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2023 | SSL_FORCE_FLUSH : |
| 2024 | SSL_DONT_FORCE_FLUSH; |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 2025 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2026 | /* Swap epochs before sending Finished: we can't do it after |
| 2027 | * sending ChangeCipherSpec, in case write returns WANT_READ. |
| 2028 | * Must be done before copying, may change out_msg pointer */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2029 | if (is_finished && ssl->handshake->cur_msg_p == (cur->p + 12)) { |
| 2030 | MBEDTLS_SSL_DEBUG_MSG(2, ("swap epochs to send finished message")); |
| 2031 | ret = ssl_swap_epochs(ssl); |
| 2032 | if (ret != 0) |
| 2033 | return ret; |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2034 | } |
| 2035 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2036 | ret = ssl_get_remaining_payload_in_datagram(ssl); |
| 2037 | if (ret < 0) |
| 2038 | return ret; |
| 2039 | max_frag_len = (size_t)ret; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2040 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2041 | /* CCS is copied as is, while HS messages may need fragmentation */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2042 | if (cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) { |
| 2043 | if (max_frag_len == 0) { |
| 2044 | if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) |
| 2045 | return ret; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2046 | |
| 2047 | continue; |
| 2048 | } |
| 2049 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2050 | memcpy(ssl->out_msg, cur->p, cur->len); |
| 2051 | ssl->out_msglen = cur->len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2052 | ssl->out_msgtype = cur->type; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2053 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2054 | /* Update position inside current message */ |
| 2055 | ssl->handshake->cur_msg_p += cur->len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2056 | } else { |
| 2057 | const unsigned char *const p = ssl->handshake->cur_msg_p; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2058 | const size_t hs_len = cur->len - 12; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2059 | const size_t frag_off = p - (cur->p + 12); |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2060 | const size_t rem_len = hs_len - frag_off; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2061 | size_t cur_hs_frag_len, max_hs_frag_len; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2062 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2063 | if ((max_frag_len < 12) || (max_frag_len == 12 && hs_len != 0)) { |
| 2064 | if (is_finished) { |
| 2065 | ret = ssl_swap_epochs(ssl); |
| 2066 | if (ret != 0) |
| 2067 | return ret; |
Manuel Pégourié-Gonnard | e07bc20 | 2020-02-26 09:53:42 +0100 | [diff] [blame] | 2068 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2069 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2070 | if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) |
| 2071 | return ret; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2072 | |
| 2073 | continue; |
| 2074 | } |
| 2075 | max_hs_frag_len = max_frag_len - 12; |
| 2076 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2077 | cur_hs_frag_len = rem_len > max_hs_frag_len ? max_hs_frag_len : |
| 2078 | rem_len; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2079 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2080 | if (frag_off == 0 && cur_hs_frag_len != hs_len) { |
| 2081 | MBEDTLS_SSL_DEBUG_MSG( |
| 2082 | 2, ("fragmenting handshake message (%u > %u)", |
| 2083 | (unsigned)cur_hs_frag_len, (unsigned)max_hs_frag_len)); |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 2084 | } |
Manuel Pégourié-Gonnard | b747c6c | 2018-08-12 13:28:53 +0200 | [diff] [blame] | 2085 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2086 | /* Messages are stored with handshake headers as if not fragmented, |
| 2087 | * copy beginning of headers then fill fragmentation fields. |
| 2088 | * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2089 | memcpy(ssl->out_msg, cur->p, 6); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2090 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2091 | ssl->out_msg[6] = ((frag_off >> 16) & 0xff); |
| 2092 | ssl->out_msg[7] = ((frag_off >> 8) & 0xff); |
| 2093 | ssl->out_msg[8] = ((frag_off)&0xff); |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2094 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2095 | ssl->out_msg[9] = ((cur_hs_frag_len >> 16) & 0xff); |
| 2096 | ssl->out_msg[10] = ((cur_hs_frag_len >> 8) & 0xff); |
| 2097 | ssl->out_msg[11] = ((cur_hs_frag_len)&0xff); |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2098 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2099 | 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] | 2100 | |
Hanno Becker | 3f7b973 | 2018-08-28 09:53:25 +0100 | [diff] [blame] | 2101 | /* Copy the handshake message content and set records fields */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2102 | memcpy(ssl->out_msg + 12, p, cur_hs_frag_len); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2103 | ssl->out_msglen = cur_hs_frag_len + 12; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2104 | ssl->out_msgtype = cur->type; |
| 2105 | |
| 2106 | /* Update position inside current message */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2107 | ssl->handshake->cur_msg_p += cur_hs_frag_len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2108 | } |
| 2109 | |
| 2110 | /* If done with the current message move to the next one if any */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2111 | if (ssl->handshake->cur_msg_p >= cur->p + cur->len) { |
| 2112 | if (cur->next != NULL) { |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2113 | ssl->handshake->cur_msg = cur->next; |
| 2114 | ssl->handshake->cur_msg_p = cur->next->p + 12; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2115 | } else { |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2116 | ssl->handshake->cur_msg = NULL; |
| 2117 | ssl->handshake->cur_msg_p = NULL; |
| 2118 | } |
| 2119 | } |
| 2120 | |
| 2121 | /* Actually send the message out */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2122 | if ((ret = mbedtls_ssl_write_record(ssl, force_flush)) != 0) { |
| 2123 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret); |
| 2124 | return ret; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2125 | } |
| 2126 | } |
| 2127 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2128 | if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) |
| 2129 | return ret; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2130 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2131 | /* Update state and set timer */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2132 | if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2133 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2134 | else { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2135 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2136 | mbedtls_ssl_set_timer(ssl, ssl->handshake->retransmit_timeout); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2137 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2138 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2139 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= mbedtls_ssl_flight_transmit")); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2140 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2141 | return 0; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2142 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2143 | |
| 2144 | /* |
| 2145 | * To be called when the last message of an incoming flight is received. |
| 2146 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2147 | void mbedtls_ssl_recv_flight_completed(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2148 | { |
| 2149 | /* We won't need to resend that one any more */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2150 | mbedtls_ssl_flight_free(ssl->handshake->flight); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2151 | ssl->handshake->flight = NULL; |
| 2152 | ssl->handshake->cur_msg = NULL; |
| 2153 | |
| 2154 | /* The next incoming flight will start with this msg_seq */ |
| 2155 | ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq; |
| 2156 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 2157 | /* We don't want to remember CCS's across flight boundaries. */ |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 2158 | ssl->handshake->buffering.seen_ccs = 0; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 2159 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 2160 | /* Clear future message buffering structure. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2161 | mbedtls_ssl_buffering_free(ssl); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 2162 | |
Manuel Pégourié-Gonnard | 6c1fa3a | 2014-10-01 16:58:16 +0200 | [diff] [blame] | 2163 | /* Cancel timer */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2164 | mbedtls_ssl_set_timer(ssl, 0); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2165 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2166 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2167 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2168 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2169 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2170 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2171 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2172 | |
| 2173 | /* |
| 2174 | * To be called when the last message of an outgoing flight is send. |
| 2175 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2176 | void mbedtls_ssl_send_flight_completed(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2177 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2178 | ssl_reset_retransmit_timeout(ssl); |
| 2179 | mbedtls_ssl_set_timer(ssl, ssl->handshake->retransmit_timeout); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2180 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2181 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2182 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2183 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2184 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2185 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2186 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2187 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2188 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2189 | /* |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2190 | * Handshake layer functions |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2191 | */ |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2192 | |
| 2193 | /* |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2194 | * Write (DTLS: or queue) current handshake (including CCS) message. |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2195 | * |
| 2196 | * - fill in handshake headers |
| 2197 | * - update handshake checksum |
| 2198 | * - DTLS: save message for resending |
| 2199 | * - then pass to the record layer |
| 2200 | * |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2201 | * DTLS: except for HelloRequest, messages are only queued, and will only be |
| 2202 | * actually sent when calling flight_transmit() or resend(). |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2203 | * |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2204 | * Inputs: |
| 2205 | * - ssl->out_msglen: 4 + actual handshake message len |
| 2206 | * (4 is the size of handshake headers for TLS) |
| 2207 | * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc) |
| 2208 | * - ssl->out_msg + 4: the handshake message body |
| 2209 | * |
Manuel Pégourié-Gonnard | 065a2a3 | 2018-08-20 11:09:26 +0200 | [diff] [blame] | 2210 | * 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] | 2211 | * - ssl->out_msglen: the length of the record contents |
| 2212 | * (including handshake headers but excluding record headers) |
| 2213 | * - ssl->out_msg: the record contents (handshake headers + content) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2214 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2215 | int mbedtls_ssl_write_handshake_msg(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2216 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2217 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2218 | const size_t hs_len = ssl->out_msglen - 4; |
| 2219 | const unsigned char hs_type = ssl->out_msg[0]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2220 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2221 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write handshake message")); |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2222 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2223 | /* |
| 2224 | * Sanity checks |
| 2225 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2226 | if (ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2227 | ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) { |
| 2228 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 2229 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2230 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2231 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 2232 | /* Whenever we send anything different from a |
| 2233 | * HelloRequest we should be in a handshake - double check. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2234 | if (!(ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2235 | hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST) && |
| 2236 | ssl->handshake == NULL) { |
| 2237 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 2238 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2239 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2240 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2241 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2242 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2243 | ssl->handshake != NULL && |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2244 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) { |
| 2245 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 2246 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2247 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2248 | # endif |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2249 | |
Hanno Becker | b50a253 | 2018-08-06 11:52:54 +0100 | [diff] [blame] | 2250 | /* Double-check that we did not exceed the bounds |
| 2251 | * of the outgoing record buffer. |
| 2252 | * This should never fail as the various message |
| 2253 | * writing functions must obey the bounds of the |
| 2254 | * outgoing record buffer, but better be safe. |
| 2255 | * |
| 2256 | * Note: We deliberately do not check for the MTU or MFL here. |
| 2257 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2258 | if (ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN) { |
| 2259 | MBEDTLS_SSL_DEBUG_MSG(1, ("Record too large: " |
| 2260 | "size %" MBEDTLS_PRINTF_SIZET |
| 2261 | ", maximum %" MBEDTLS_PRINTF_SIZET, |
| 2262 | ssl->out_msglen, |
| 2263 | (size_t)MBEDTLS_SSL_OUT_CONTENT_LEN)); |
| 2264 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | b50a253 | 2018-08-06 11:52:54 +0100 | [diff] [blame] | 2265 | } |
| 2266 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2267 | /* |
| 2268 | * Fill handshake headers |
| 2269 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2270 | if (ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 2271 | ssl->out_msg[1] = (unsigned char)(hs_len >> 16); |
| 2272 | ssl->out_msg[2] = (unsigned char)(hs_len >> 8); |
| 2273 | ssl->out_msg[3] = (unsigned char)(hs_len); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2274 | |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 2275 | /* |
| 2276 | * DTLS has additional fields in the Handshake layer, |
| 2277 | * between the length field and the actual payload: |
| 2278 | * uint16 message_seq; |
| 2279 | * uint24 fragment_offset; |
| 2280 | * uint24 fragment_length; |
| 2281 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2282 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2283 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 2284 | /* Make room for the additional DTLS fields */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2285 | if (MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8) { |
| 2286 | MBEDTLS_SSL_DEBUG_MSG( |
| 2287 | 1, ("DTLS handshake message too large: " |
| 2288 | "size %" MBEDTLS_PRINTF_SIZET |
| 2289 | ", maximum %" MBEDTLS_PRINTF_SIZET, |
| 2290 | hs_len, (size_t)(MBEDTLS_SSL_OUT_CONTENT_LEN - 12))); |
| 2291 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Hanno Becker | 9648f8b | 2017-09-18 10:55:54 +0100 | [diff] [blame] | 2292 | } |
| 2293 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2294 | 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] | 2295 | ssl->out_msglen += 8; |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 2296 | |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 2297 | /* Write message_seq and update it, except for HelloRequest */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2298 | if (hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST) { |
| 2299 | ssl->out_msg[4] = (ssl->handshake->out_msg_seq >> 8) & 0xFF; |
| 2300 | ssl->out_msg[5] = (ssl->handshake->out_msg_seq) & 0xFF; |
| 2301 | ++(ssl->handshake->out_msg_seq); |
| 2302 | } else { |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 2303 | ssl->out_msg[4] = 0; |
| 2304 | ssl->out_msg[5] = 0; |
| 2305 | } |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 2306 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2307 | /* Handshake hashes are computed without fragmentation, |
| 2308 | * so set frag_offset = 0 and frag_len = hs_len for now */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2309 | memset(ssl->out_msg + 6, 0x00, 3); |
| 2310 | memcpy(ssl->out_msg + 9, ssl->out_msg + 1, 3); |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 2311 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2312 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 2313 | |
Hanno Becker | 0207e53 | 2018-08-28 10:28:28 +0100 | [diff] [blame] | 2314 | /* Update running hashes of handshake messages seen */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2315 | if (hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST) |
| 2316 | ssl->handshake->update_checksum(ssl, ssl->out_msg, ssl->out_msglen); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2317 | } |
| 2318 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2319 | /* Either send now, or just save to be sent (and resent) later */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2320 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2321 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 2322 | !(ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2323 | hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST)) { |
| 2324 | if ((ret = ssl_flight_append(ssl)) != 0) { |
| 2325 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_flight_append", ret); |
| 2326 | return ret; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2327 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2328 | } else |
| 2329 | # endif |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2330 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2331 | if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) { |
| 2332 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_write_record", ret); |
| 2333 | return ret; |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2334 | } |
| 2335 | } |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2336 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2337 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write handshake message")); |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2338 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2339 | return 0; |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2340 | } |
| 2341 | |
| 2342 | /* |
| 2343 | * Record layer functions |
| 2344 | */ |
| 2345 | |
| 2346 | /* |
| 2347 | * Write current record. |
| 2348 | * |
| 2349 | * Uses: |
| 2350 | * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS) |
| 2351 | * - ssl->out_msglen: length of the record content (excl headers) |
| 2352 | * - ssl->out_msg: record content |
| 2353 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2354 | 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] | 2355 | { |
| 2356 | int ret, done = 0; |
| 2357 | size_t len = ssl->out_msglen; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2358 | uint8_t flush = force_flush; |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2359 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2360 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write record")); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2361 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2362 | if (!done) { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2363 | unsigned i; |
| 2364 | size_t protected_record_size; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2365 | # if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 2366 | size_t out_buf_len = ssl->out_buf_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2367 | # else |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 2368 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2369 | # endif |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 2370 | /* Skip writing the record content type to after the encryption, |
| 2371 | * as it may change when using the CID extension. */ |
| 2372 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2373 | mbedtls_ssl_write_version(ssl->major_ver, ssl->minor_ver, |
| 2374 | ssl->conf->transport, ssl->out_hdr + 1); |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 2375 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2376 | memcpy(ssl->out_ctr, ssl->cur_out_ctr, 8); |
| 2377 | ssl->out_len[0] = (unsigned char)(len >> 8); |
| 2378 | ssl->out_len[1] = (unsigned char)(len); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2379 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2380 | if (ssl->transform_out != NULL) { |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2381 | mbedtls_record rec; |
| 2382 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2383 | rec.buf = ssl->out_iv; |
| 2384 | rec.buf_len = out_buf_len - (ssl->out_iv - ssl->out_buf); |
| 2385 | rec.data_len = ssl->out_msglen; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2386 | rec.data_offset = ssl->out_msg - rec.buf; |
| 2387 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2388 | memcpy(&rec.ctr[0], ssl->out_ctr, 8); |
| 2389 | mbedtls_ssl_write_version(ssl->major_ver, ssl->minor_ver, |
| 2390 | ssl->conf->transport, rec.ver); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2391 | rec.type = ssl->out_msgtype; |
| 2392 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2393 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 2394 | /* The CID is set by mbedtls_ssl_encrypt_buf(). */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2395 | rec.cid_len = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2396 | # endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2397 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2398 | if ((ret = mbedtls_ssl_encrypt_buf(ssl, ssl->transform_out, &rec, |
| 2399 | ssl->conf->f_rng, |
| 2400 | ssl->conf->p_rng)) != 0) { |
| 2401 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_encrypt_buf", ret); |
| 2402 | return ret; |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2403 | } |
| 2404 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2405 | if (rec.data_offset != 0) { |
| 2406 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 2407 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2408 | } |
| 2409 | |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 2410 | /* Update the record content type and CID. */ |
| 2411 | ssl->out_msgtype = rec.type; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2412 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 2413 | memcpy(ssl->out_cid, rec.cid, rec.cid_len); |
| 2414 | # endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 78f839d | 2019-03-14 12:56:23 +0000 | [diff] [blame] | 2415 | ssl->out_msglen = len = rec.data_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2416 | ssl->out_len[0] = (unsigned char)(rec.data_len >> 8); |
| 2417 | ssl->out_len[1] = (unsigned char)(rec.data_len); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2418 | } |
| 2419 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2420 | protected_record_size = len + mbedtls_ssl_out_hdr_len(ssl); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2421 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2422 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2423 | /* In case of DTLS, double-check that we don't exceed |
| 2424 | * the remaining space in the datagram. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2425 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 2426 | ret = ssl_get_remaining_space_in_datagram(ssl); |
| 2427 | if (ret < 0) |
| 2428 | return ret; |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2429 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2430 | if (protected_record_size > (size_t)ret) { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2431 | /* Should never happen */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2432 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2433 | } |
| 2434 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2435 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2436 | |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 2437 | /* Now write the potentially updated record content type. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2438 | ssl->out_hdr[0] = (unsigned char)ssl->out_msgtype; |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 2439 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2440 | MBEDTLS_SSL_DEBUG_MSG( |
| 2441 | 3, ("output record: msgtype = %u, " |
| 2442 | "version = [%u:%u], msglen = %" MBEDTLS_PRINTF_SIZET, |
| 2443 | ssl->out_hdr[0], ssl->out_hdr[1], ssl->out_hdr[2], len)); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2444 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2445 | MBEDTLS_SSL_DEBUG_BUF(4, "output record sent to network", ssl->out_hdr, |
| 2446 | protected_record_size); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2447 | |
| 2448 | ssl->out_left += protected_record_size; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2449 | ssl->out_hdr += protected_record_size; |
| 2450 | mbedtls_ssl_update_out_pointers(ssl, ssl->transform_out); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2451 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2452 | for (i = 8; i > mbedtls_ssl_ep_len(ssl); i--) |
| 2453 | if (++ssl->cur_out_ctr[i - 1] != 0) |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 2454 | break; |
| 2455 | |
| 2456 | /* The loop goes to its end iff the counter is wrapping */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2457 | if (i == mbedtls_ssl_ep_len(ssl)) { |
| 2458 | MBEDTLS_SSL_DEBUG_MSG(1, ("outgoing message counter would wrap")); |
| 2459 | return MBEDTLS_ERR_SSL_COUNTER_WRAPPING; |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 2460 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2461 | } |
| 2462 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2463 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2464 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 2465 | flush == SSL_DONT_FORCE_FLUSH) { |
Hanno Becker | 1f5a15d | 2018-08-21 13:31:31 +0100 | [diff] [blame] | 2466 | size_t remaining; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2467 | ret = ssl_get_remaining_payload_in_datagram(ssl); |
| 2468 | if (ret < 0) { |
| 2469 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_get_remaining_payload_in_datagram", |
| 2470 | ret); |
| 2471 | return ret; |
Hanno Becker | 1f5a15d | 2018-08-21 13:31:31 +0100 | [diff] [blame] | 2472 | } |
| 2473 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2474 | remaining = (size_t)ret; |
| 2475 | if (remaining == 0) { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2476 | flush = SSL_FORCE_FLUSH; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2477 | } else { |
| 2478 | MBEDTLS_SSL_DEBUG_MSG( |
| 2479 | 2, ("Still %u bytes available in current datagram", |
| 2480 | (unsigned)remaining)); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2481 | } |
| 2482 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2483 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2484 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2485 | if ((flush == SSL_FORCE_FLUSH) && |
| 2486 | (ret = mbedtls_ssl_flush_output(ssl)) != 0) { |
| 2487 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flush_output", ret); |
| 2488 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2489 | } |
| 2490 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2491 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write record")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2492 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2493 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2494 | } |
| 2495 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2496 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 2497 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2498 | static int ssl_hs_is_proper_fragment(mbedtls_ssl_context *ssl) |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 2499 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2500 | if (ssl->in_msglen < ssl->in_hslen || |
| 2501 | memcmp(ssl->in_msg + 6, "\0\0\0", 3) != 0 || |
| 2502 | memcmp(ssl->in_msg + 9, ssl->in_msg + 1, 3) != 0) { |
| 2503 | return 1; |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 2504 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2505 | return 0; |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 2506 | } |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2507 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2508 | 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] | 2509 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2510 | return ((ssl->in_msg[9] << 16) | (ssl->in_msg[10] << 8) | ssl->in_msg[11]); |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2511 | } |
| 2512 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2513 | 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] | 2514 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2515 | return ((ssl->in_msg[6] << 16) | (ssl->in_msg[7] << 8) | ssl->in_msg[8]); |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2516 | } |
| 2517 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2518 | static int ssl_check_hs_header(mbedtls_ssl_context const *ssl) |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2519 | { |
| 2520 | uint32_t msg_len, frag_off, frag_len; |
| 2521 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2522 | msg_len = ssl_get_hs_total_len(ssl); |
| 2523 | frag_off = ssl_get_hs_frag_off(ssl); |
| 2524 | frag_len = ssl_get_hs_frag_len(ssl); |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2525 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2526 | if (frag_off > msg_len) |
| 2527 | return -1; |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2528 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2529 | if (frag_len > msg_len - frag_off) |
| 2530 | return -1; |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2531 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2532 | if (frag_len + 12 > ssl->in_msglen) |
| 2533 | return -1; |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2534 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2535 | return 0; |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2536 | } |
| 2537 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2538 | /* |
| 2539 | * Mark bits in bitmask (used for DTLS HS reassembly) |
| 2540 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2541 | 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] | 2542 | { |
| 2543 | unsigned int start_bits, end_bits; |
| 2544 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2545 | start_bits = 8 - (offset % 8); |
| 2546 | if (start_bits != 8) { |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2547 | size_t first_byte_idx = offset / 8; |
| 2548 | |
Manuel Pégourié-Gonnard | ac03052 | 2014-09-02 14:23:40 +0200 | [diff] [blame] | 2549 | /* Special case */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2550 | if (len <= start_bits) { |
| 2551 | for (; len != 0; len--) |
| 2552 | mask[first_byte_idx] |= 1 << (start_bits - len); |
Manuel Pégourié-Gonnard | ac03052 | 2014-09-02 14:23:40 +0200 | [diff] [blame] | 2553 | |
| 2554 | /* Avoid potential issues with offset or len becoming invalid */ |
| 2555 | return; |
| 2556 | } |
| 2557 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2558 | offset += start_bits; /* Now offset % 8 == 0 */ |
| 2559 | len -= start_bits; |
| 2560 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2561 | for (; start_bits != 0; start_bits--) |
| 2562 | mask[first_byte_idx] |= 1 << (start_bits - 1); |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2563 | } |
| 2564 | |
| 2565 | end_bits = len % 8; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2566 | if (end_bits != 0) { |
| 2567 | size_t last_byte_idx = (offset + len) / 8; |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2568 | |
| 2569 | len -= end_bits; /* Now len % 8 == 0 */ |
| 2570 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2571 | for (; end_bits != 0; end_bits--) |
| 2572 | mask[last_byte_idx] |= 1 << (8 - end_bits); |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2573 | } |
| 2574 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2575 | memset(mask + offset / 8, 0xFF, len / 8); |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2576 | } |
| 2577 | |
| 2578 | /* |
| 2579 | * Check that bitmask is full |
| 2580 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2581 | 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] | 2582 | { |
| 2583 | size_t i; |
| 2584 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2585 | for (i = 0; i < len / 8; i++) |
| 2586 | if (mask[i] != 0xFF) |
| 2587 | return -1; |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2588 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2589 | for (i = 0; i < len % 8; i++) |
| 2590 | if ((mask[len / 8] & (1 << (7 - i))) == 0) |
| 2591 | return -1; |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2592 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2593 | return 0; |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2594 | } |
| 2595 | |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 2596 | /* msg_len does not include the handshake header */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2597 | static size_t ssl_get_reassembly_buffer_size(size_t msg_len, |
| 2598 | unsigned add_bitmap) |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2599 | { |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 2600 | size_t alloc_len; |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2601 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2602 | alloc_len = 12; /* Handshake header */ |
| 2603 | alloc_len += msg_len; /* Content buffer */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2604 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2605 | if (add_bitmap) |
| 2606 | alloc_len += msg_len / 8 + (msg_len % 8 != 0); /* Bitmap */ |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2607 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2608 | return alloc_len; |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2609 | } |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 2610 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2611 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2612 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2613 | 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] | 2614 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2615 | return ((ssl->in_msg[1] << 16) | (ssl->in_msg[2] << 8) | ssl->in_msg[3]); |
Hanno Becker | 12555c6 | 2018-08-16 12:47:53 +0100 | [diff] [blame] | 2616 | } |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 2617 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2618 | int mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 2619 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2620 | if (ssl->in_msglen < mbedtls_ssl_hs_hdr_len(ssl)) { |
| 2621 | MBEDTLS_SSL_DEBUG_MSG( |
| 2622 | 1, ("handshake message too short: %" MBEDTLS_PRINTF_SIZET, |
| 2623 | ssl->in_msglen)); |
| 2624 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 2625 | } |
| 2626 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2627 | 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] | 2628 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2629 | MBEDTLS_SSL_DEBUG_MSG(3, ("handshake message: msglen =" |
| 2630 | " %" MBEDTLS_PRINTF_SIZET |
| 2631 | ", type = %u, hslen = %" MBEDTLS_PRINTF_SIZET, |
| 2632 | ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen)); |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 2633 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2634 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2635 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2636 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2637 | 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] | 2638 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2639 | if (ssl_check_hs_header(ssl) != 0) { |
| 2640 | MBEDTLS_SSL_DEBUG_MSG(1, ("invalid handshake header")); |
| 2641 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2642 | } |
| 2643 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2644 | if (ssl->handshake != NULL && |
| 2645 | ((ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && |
| 2646 | recv_msg_seq != ssl->handshake->in_msg_seq) || |
| 2647 | (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER && |
| 2648 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO))) { |
| 2649 | if (recv_msg_seq > ssl->handshake->in_msg_seq) { |
| 2650 | MBEDTLS_SSL_DEBUG_MSG( |
| 2651 | 2, |
| 2652 | ("received future handshake message of sequence number %u (next %u)", |
| 2653 | recv_msg_seq, ssl->handshake->in_msg_seq)); |
| 2654 | return MBEDTLS_ERR_SSL_EARLY_MESSAGE; |
Hanno Becker | 9e1ec22 | 2018-08-15 15:54:43 +0100 | [diff] [blame] | 2655 | } |
| 2656 | |
Manuel Pégourié-Gonnard | fc572dd | 2014-10-09 17:56:57 +0200 | [diff] [blame] | 2657 | /* Retransmit only on last message from previous flight, to avoid |
| 2658 | * too many retransmissions. |
| 2659 | * Besides, No sane server ever retransmits HelloVerifyRequest */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2660 | if (recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 && |
| 2661 | ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST) { |
| 2662 | MBEDTLS_SSL_DEBUG_MSG( |
| 2663 | 2, ("received message from last flight, " |
| 2664 | "message_seq = %u, start_of_flight = %u", |
| 2665 | recv_msg_seq, ssl->handshake->in_flight_start_seq)); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 2666 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2667 | if ((ret = mbedtls_ssl_resend(ssl)) != 0) { |
| 2668 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend", ret); |
| 2669 | return ret; |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 2670 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2671 | } else { |
| 2672 | MBEDTLS_SSL_DEBUG_MSG(2, ("dropping out-of-sequence message: " |
| 2673 | "message_seq = %u, expected = %u", |
| 2674 | recv_msg_seq, |
| 2675 | ssl->handshake->in_msg_seq)); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 2676 | } |
| 2677 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2678 | return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 2679 | } |
| 2680 | /* Wait until message completion to increment in_msg_seq */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2681 | |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 2682 | /* Message reassembly is handled alongside buffering of future |
| 2683 | * messages; the commonality is that both handshake fragments and |
Hanno Becker | 83ab41c | 2018-08-28 17:19:38 +0100 | [diff] [blame] | 2684 | * future messages cannot be forwarded immediately to the |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 2685 | * handshake logic layer. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2686 | if (ssl_hs_is_proper_fragment(ssl) == 1) { |
| 2687 | MBEDTLS_SSL_DEBUG_MSG(2, |
| 2688 | ("found fragmented DTLS handshake message")); |
| 2689 | return MBEDTLS_ERR_SSL_EARLY_MESSAGE; |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2690 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2691 | } else |
| 2692 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 2693 | /* With TLS we don't handle fragmentation (for now) */ |
| 2694 | if (ssl->in_msglen < ssl->in_hslen) { |
| 2695 | MBEDTLS_SSL_DEBUG_MSG(1, ("TLS handshake fragmentation not supported")); |
| 2696 | return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 2697 | } |
| 2698 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2699 | return 0; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 2700 | } |
| 2701 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2702 | void mbedtls_ssl_update_handshake_status(mbedtls_ssl_context *ssl) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 2703 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2704 | mbedtls_ssl_handshake_params *const hs = ssl->handshake; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 2705 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2706 | if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL) { |
| 2707 | 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] | 2708 | } |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 2709 | |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 2710 | /* Handshake message is complete, increment counter */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2711 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2712 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 2713 | ssl->handshake != NULL) { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 2714 | unsigned offset; |
| 2715 | mbedtls_ssl_hs_buffer *hs_buf; |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 2716 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 2717 | /* Increment handshake sequence number */ |
| 2718 | hs->in_msg_seq++; |
| 2719 | |
| 2720 | /* |
| 2721 | * Clear up handshake buffering and reassembly structure. |
| 2722 | */ |
| 2723 | |
| 2724 | /* Free first entry */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2725 | ssl_buffering_free_slot(ssl, 0); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 2726 | |
| 2727 | /* Shift all other entries */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2728 | for (offset = 0, hs_buf = &hs->buffering.hs[0]; |
| 2729 | offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++, hs_buf++) { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 2730 | *hs_buf = *(hs_buf + 1); |
| 2731 | } |
| 2732 | |
| 2733 | /* Create a fresh last entry */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2734 | memset(hs_buf, 0, sizeof(mbedtls_ssl_hs_buffer)); |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 2735 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2736 | # endif |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 2737 | } |
| 2738 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 2739 | /* |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 2740 | * DTLS anti-replay: RFC 6347 4.1.2.6 |
| 2741 | * |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2742 | * in_window is a field of bits numbered from 0 (lsb) to 63 (msb). |
| 2743 | * Bit n is set iff record number in_window_top - n has been seen. |
| 2744 | * |
| 2745 | * Usually, in_window_top is the last record number seen and the lsb of |
| 2746 | * in_window is set. The only exception is the initial state (record number 0 |
| 2747 | * not seen yet). |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 2748 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2749 | # if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 2750 | void mbedtls_ssl_dtls_replay_reset(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 2751 | { |
| 2752 | ssl->in_window_top = 0; |
| 2753 | ssl->in_window = 0; |
| 2754 | } |
| 2755 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2756 | 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] | 2757 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2758 | return (((uint64_t)buf[0] << 40) | ((uint64_t)buf[1] << 32) | |
| 2759 | ((uint64_t)buf[2] << 24) | ((uint64_t)buf[3] << 16) | |
| 2760 | ((uint64_t)buf[4] << 8) | ((uint64_t)buf[5])); |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 2761 | } |
| 2762 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2763 | static int mbedtls_ssl_dtls_record_replay_check(mbedtls_ssl_context *ssl, |
| 2764 | uint8_t *record_in_ctr) |
Arto Kinnunen | 7f8089b | 2019-10-29 11:13:33 +0200 | [diff] [blame] | 2765 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2766 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Arto Kinnunen | 7f8089b | 2019-10-29 11:13:33 +0200 | [diff] [blame] | 2767 | unsigned char *original_in_ctr; |
| 2768 | |
| 2769 | // save original in_ctr |
| 2770 | original_in_ctr = ssl->in_ctr; |
| 2771 | |
| 2772 | // use counter from record |
| 2773 | ssl->in_ctr = record_in_ctr; |
| 2774 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2775 | ret = mbedtls_ssl_dtls_replay_check((mbedtls_ssl_context const *)ssl); |
Arto Kinnunen | 7f8089b | 2019-10-29 11:13:33 +0200 | [diff] [blame] | 2776 | |
| 2777 | // restore the counter |
| 2778 | ssl->in_ctr = original_in_ctr; |
| 2779 | |
| 2780 | return ret; |
| 2781 | } |
| 2782 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 2783 | /* |
| 2784 | * Return 0 if sequence number is acceptable, -1 otherwise |
| 2785 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2786 | 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] | 2787 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2788 | 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] | 2789 | uint64_t bit; |
| 2790 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2791 | if (ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED) |
| 2792 | return 0; |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 2793 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2794 | if (rec_seqnum > ssl->in_window_top) |
| 2795 | return 0; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 2796 | |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2797 | bit = ssl->in_window_top - rec_seqnum; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 2798 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2799 | if (bit >= 64) |
| 2800 | return -1; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 2801 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2802 | if ((ssl->in_window & ((uint64_t)1 << bit)) != 0) |
| 2803 | return -1; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 2804 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2805 | return 0; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 2806 | } |
| 2807 | |
| 2808 | /* |
| 2809 | * Update replay window on new validated record |
| 2810 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2811 | void mbedtls_ssl_dtls_replay_update(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 2812 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2813 | 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] | 2814 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2815 | if (ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED) |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 2816 | return; |
| 2817 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2818 | if (rec_seqnum > ssl->in_window_top) { |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 2819 | /* Update window_top and the contents of the window */ |
| 2820 | uint64_t shift = rec_seqnum - ssl->in_window_top; |
| 2821 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2822 | if (shift >= 64) |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2823 | ssl->in_window = 1; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2824 | else { |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 2825 | ssl->in_window <<= shift; |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2826 | ssl->in_window |= 1; |
| 2827 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 2828 | |
| 2829 | ssl->in_window_top = rec_seqnum; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2830 | } else { |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 2831 | /* Mark that number as seen in the current window */ |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2832 | uint64_t bit = ssl->in_window_top - rec_seqnum; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 2833 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2834 | if (bit < 64) /* Always true, but be extra sure */ |
| 2835 | ssl->in_window |= (uint64_t)1 << bit; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 2836 | } |
| 2837 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2838 | # endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 2839 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2840 | # if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && \ |
| 2841 | defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 2842 | /* |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 2843 | * Without any SSL context, check if a datagram looks like a ClientHello with |
| 2844 | * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message. |
Simon Butcher | 0789aed | 2015-09-11 17:15:17 +0100 | [diff] [blame] | 2845 | * Both input and output include full DTLS headers. |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 2846 | * |
| 2847 | * - if cookie is valid, return 0 |
| 2848 | * - if ClientHello looks superficially valid but cookie is not, |
| 2849 | * fill obuf and set olen, then |
| 2850 | * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED |
| 2851 | * - otherwise return a specific error code |
| 2852 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2853 | static int |
| 2854 | ssl_check_dtls_clihlo_cookie(mbedtls_ssl_cookie_write_t *f_cookie_write, |
| 2855 | mbedtls_ssl_cookie_check_t *f_cookie_check, |
| 2856 | void *p_cookie, |
| 2857 | const unsigned char *cli_id, |
| 2858 | size_t cli_id_len, |
| 2859 | const unsigned char *in, |
| 2860 | size_t in_len, |
| 2861 | unsigned char *obuf, |
| 2862 | size_t buf_len, |
| 2863 | size_t *olen) |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 2864 | { |
| 2865 | size_t sid_len, cookie_len; |
| 2866 | unsigned char *p; |
| 2867 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 2868 | /* |
| 2869 | * Structure of ClientHello with record and handshake headers, |
| 2870 | * and expected values. We don't need to check a lot, more checks will be |
| 2871 | * done when actually parsing the ClientHello - skipping those checks |
| 2872 | * avoids code duplication and does not make cookie forging any easier. |
| 2873 | * |
| 2874 | * 0-0 ContentType type; copied, must be handshake |
| 2875 | * 1-2 ProtocolVersion version; copied |
| 2876 | * 3-4 uint16 epoch; copied, must be 0 |
| 2877 | * 5-10 uint48 sequence_number; copied |
| 2878 | * 11-12 uint16 length; (ignored) |
| 2879 | * |
| 2880 | * 13-13 HandshakeType msg_type; (ignored) |
| 2881 | * 14-16 uint24 length; (ignored) |
| 2882 | * 17-18 uint16 message_seq; copied |
| 2883 | * 19-21 uint24 fragment_offset; copied, must be 0 |
| 2884 | * 22-24 uint24 fragment_length; (ignored) |
| 2885 | * |
| 2886 | * 25-26 ProtocolVersion client_version; (ignored) |
| 2887 | * 27-58 Random random; (ignored) |
| 2888 | * 59-xx SessionID session_id; 1 byte len + sid_len content |
| 2889 | * 60+ opaque cookie<0..2^8-1>; 1 byte len + content |
| 2890 | * ... |
| 2891 | * |
| 2892 | * Minimum length is 61 bytes. |
| 2893 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2894 | if (in_len < 61 || in[0] != MBEDTLS_SSL_MSG_HANDSHAKE || in[3] != 0 || |
| 2895 | in[4] != 0 || in[19] != 0 || in[20] != 0 || in[21] != 0) { |
| 2896 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 2897 | } |
| 2898 | |
| 2899 | sid_len = in[59]; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2900 | if (sid_len > in_len - 61) |
| 2901 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 2902 | |
| 2903 | cookie_len = in[60 + sid_len]; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2904 | if (cookie_len > in_len - 60) |
| 2905 | return MBEDTLS_ERR_SSL_DECODE_ERROR; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 2906 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2907 | if (f_cookie_check(p_cookie, in + sid_len + 61, cookie_len, cli_id, |
| 2908 | cli_id_len) == 0) { |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 2909 | /* Valid cookie */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2910 | return 0; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 2911 | } |
| 2912 | |
| 2913 | /* |
| 2914 | * If we get here, we've got an invalid cookie, let's prepare HVR. |
| 2915 | * |
| 2916 | * 0-0 ContentType type; copied |
| 2917 | * 1-2 ProtocolVersion version; copied |
| 2918 | * 3-4 uint16 epoch; copied |
| 2919 | * 5-10 uint48 sequence_number; copied |
| 2920 | * 11-12 uint16 length; olen - 13 |
| 2921 | * |
| 2922 | * 13-13 HandshakeType msg_type; hello_verify_request |
| 2923 | * 14-16 uint24 length; olen - 25 |
| 2924 | * 17-18 uint16 message_seq; copied |
| 2925 | * 19-21 uint24 fragment_offset; copied |
| 2926 | * 22-24 uint24 fragment_length; olen - 25 |
| 2927 | * |
| 2928 | * 25-26 ProtocolVersion server_version; 0xfe 0xff |
| 2929 | * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie |
| 2930 | * |
| 2931 | * Minimum length is 28. |
| 2932 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2933 | if (buf_len < 28) |
| 2934 | return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 2935 | |
| 2936 | /* Copy most fields and adapt others */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2937 | memcpy(obuf, in, 25); |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 2938 | obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST; |
| 2939 | obuf[25] = 0xfe; |
| 2940 | obuf[26] = 0xff; |
| 2941 | |
| 2942 | /* Generate and write actual cookie */ |
| 2943 | p = obuf + 28; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2944 | if (f_cookie_write(p_cookie, &p, obuf + buf_len, cli_id, cli_id_len) != 0) { |
| 2945 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 2946 | } |
| 2947 | |
| 2948 | *olen = p - obuf; |
| 2949 | |
| 2950 | /* Go back and fill length fields */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2951 | obuf[27] = (unsigned char)(*olen - 28); |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 2952 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2953 | obuf[14] = obuf[22] = (unsigned char)((*olen - 25) >> 16); |
| 2954 | obuf[15] = obuf[23] = (unsigned char)((*olen - 25) >> 8); |
| 2955 | obuf[16] = obuf[24] = (unsigned char)((*olen - 25)); |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 2956 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2957 | obuf[11] = (unsigned char)((*olen - 13) >> 8); |
| 2958 | obuf[12] = (unsigned char)((*olen - 13)); |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 2959 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2960 | return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 2961 | } |
| 2962 | |
| 2963 | /* |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 2964 | * Handle possible client reconnect with the same UDP quadruplet |
| 2965 | * (RFC 6347 Section 4.2.8). |
| 2966 | * |
| 2967 | * Called by ssl_parse_record_header() in case we receive an epoch 0 record |
| 2968 | * that looks like a ClientHello. |
| 2969 | * |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 2970 | * - if the input looks like a ClientHello without cookies, |
Manuel Pégourié-Gonnard | 824655c | 2020-03-11 12:51:42 +0100 | [diff] [blame] | 2971 | * send back HelloVerifyRequest, then return 0 |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 2972 | * - if the input looks like a ClientHello with a valid cookie, |
| 2973 | * reset the session of the current context, and |
Manuel Pégourié-Gonnard | be619c1 | 2015-09-08 11:21:21 +0200 | [diff] [blame] | 2974 | * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 2975 | * - if anything goes wrong, return a specific error code |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 2976 | * |
Manuel Pégourié-Gonnard | 824655c | 2020-03-11 12:51:42 +0100 | [diff] [blame] | 2977 | * This function is called (through ssl_check_client_reconnect()) when an |
| 2978 | * unexpected record is found in ssl_get_next_record(), which will discard the |
| 2979 | * record if we return 0, and bubble up the return value otherwise (this |
| 2980 | * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected |
| 2981 | * 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] | 2982 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2983 | static int ssl_handle_possible_reconnect(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 2984 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2985 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 2986 | size_t len; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 2987 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2988 | if (ssl->conf->f_cookie_write == NULL || |
| 2989 | ssl->conf->f_cookie_check == NULL) { |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 2990 | /* If we can't use cookies to verify reachability of the peer, |
| 2991 | * drop the record. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2992 | MBEDTLS_SSL_DEBUG_MSG(1, ("no cookie callbacks, " |
| 2993 | "can't check reconnect validity")); |
| 2994 | return 0; |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 2995 | } |
| 2996 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 2997 | ret = ssl_check_dtls_clihlo_cookie( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 2998 | ssl->conf->f_cookie_write, ssl->conf->f_cookie_check, |
| 2999 | ssl->conf->p_cookie, ssl->cli_id, ssl->cli_id_len, ssl->in_buf, |
| 3000 | ssl->in_left, ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len); |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3001 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3002 | MBEDTLS_SSL_DEBUG_RET(2, "ssl_check_dtls_clihlo_cookie", ret); |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3003 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3004 | if (ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED) { |
Manuel Pégourié-Gonnard | 243d70f | 2020-03-31 12:07:47 +0200 | [diff] [blame] | 3005 | int send_ret; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3006 | MBEDTLS_SSL_DEBUG_MSG(1, ("sending HelloVerifyRequest")); |
| 3007 | MBEDTLS_SSL_DEBUG_BUF(4, "output record sent to network", ssl->out_buf, |
| 3008 | len); |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 3009 | /* 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] | 3010 | * If the error is permanent we'll catch it later, |
| 3011 | * if it's not, then hopefully it'll work next time. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3012 | send_ret = ssl->f_send(ssl->p_bio, ssl->out_buf, len); |
| 3013 | MBEDTLS_SSL_DEBUG_RET(2, "ssl->f_send", send_ret); |
| 3014 | (void)send_ret; |
Manuel Pégourié-Gonnard | 243d70f | 2020-03-31 12:07:47 +0200 | [diff] [blame] | 3015 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3016 | return 0; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3017 | } |
| 3018 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3019 | if (ret == 0) { |
| 3020 | MBEDTLS_SSL_DEBUG_MSG(1, ("cookie is valid, resetting context")); |
| 3021 | if ((ret = mbedtls_ssl_session_reset_int(ssl, 1)) != 0) { |
| 3022 | MBEDTLS_SSL_DEBUG_RET(1, "reset", ret); |
| 3023 | return ret; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3024 | } |
| 3025 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3026 | return MBEDTLS_ERR_SSL_CLIENT_RECONNECT; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3027 | } |
| 3028 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3029 | return ret; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3030 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3031 | # 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] | 3032 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3033 | static int ssl_check_record_type(uint8_t record_type) |
Hanno Becker | f661c9c | 2019-05-03 13:25:54 +0100 | [diff] [blame] | 3034 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3035 | if (record_type != MBEDTLS_SSL_MSG_HANDSHAKE && |
Hanno Becker | f661c9c | 2019-05-03 13:25:54 +0100 | [diff] [blame] | 3036 | record_type != MBEDTLS_SSL_MSG_ALERT && |
| 3037 | record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC && |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3038 | record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA) { |
| 3039 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | f661c9c | 2019-05-03 13:25:54 +0100 | [diff] [blame] | 3040 | } |
| 3041 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3042 | return 0; |
Hanno Becker | f661c9c | 2019-05-03 13:25:54 +0100 | [diff] [blame] | 3043 | } |
| 3044 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3045 | /* |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3046 | * ContentType type; |
| 3047 | * ProtocolVersion version; |
| 3048 | * uint16 epoch; // DTLS only |
| 3049 | * uint48 sequence_number; // DTLS only |
| 3050 | * uint16 length; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3051 | * |
| 3052 | * 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] | 3053 | * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad, |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3054 | * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected. |
| 3055 | * |
| 3056 | * With DTLS, mbedtls_ssl_read_record() will: |
Simon Butcher | 207990d | 2015-12-16 01:51:30 +0000 | [diff] [blame] | 3057 | * 1. proceed with the record if this function returns 0 |
| 3058 | * 2. drop only the current record if this function returns UNEXPECTED_RECORD |
| 3059 | * 3. return CLIENT_RECONNECT if this function return that value |
| 3060 | * 4. drop the whole datagram if this function returns anything else. |
| 3061 | * Point 2 is needed when the peer is resending, and we have already received |
| 3062 | * 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] | 3063 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3064 | static int ssl_parse_record_header(mbedtls_ssl_context const *ssl, |
| 3065 | unsigned char *buf, |
| 3066 | size_t len, |
| 3067 | mbedtls_record *rec) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3068 | { |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 3069 | int major_ver, minor_ver; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3070 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3071 | size_t const rec_hdr_type_offset = 0; |
| 3072 | size_t const rec_hdr_type_len = 1; |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 3073 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3074 | size_t const rec_hdr_version_offset = |
| 3075 | rec_hdr_type_offset + rec_hdr_type_len; |
| 3076 | size_t const rec_hdr_version_len = 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3077 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3078 | size_t const rec_hdr_ctr_len = 8; |
| 3079 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3080 | uint32_t rec_epoch; |
| 3081 | size_t const rec_hdr_ctr_offset = |
| 3082 | rec_hdr_version_offset + rec_hdr_version_len; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3083 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3084 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3085 | size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len; |
| 3086 | size_t rec_hdr_cid_len = 0; |
| 3087 | # endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3088 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3089 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3090 | size_t rec_hdr_len_offset; /* To be determined */ |
| 3091 | size_t const rec_hdr_len_len = 2; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3092 | |
| 3093 | /* |
| 3094 | * Check minimum lengths for record header. |
| 3095 | */ |
| 3096 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3097 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3098 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3099 | rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3100 | } else |
| 3101 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3102 | { |
| 3103 | rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len; |
| 3104 | } |
| 3105 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3106 | if (len < rec_hdr_len_offset + rec_hdr_len_len) { |
| 3107 | MBEDTLS_SSL_DEBUG_MSG( |
| 3108 | 1, |
| 3109 | ("datagram of length %u too small to hold DTLS record header of length %u", |
| 3110 | (unsigned)len, (unsigned)(rec_hdr_len_len + rec_hdr_len_len))); |
| 3111 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3112 | } |
| 3113 | |
| 3114 | /* |
| 3115 | * Parse and validate record content type |
| 3116 | */ |
| 3117 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3118 | rec->type = buf[rec_hdr_type_offset]; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3119 | |
| 3120 | /* Check record content type */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3121 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3122 | rec->cid_len = 0; |
| 3123 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3124 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3125 | ssl->conf->cid_len != 0 && rec->type == MBEDTLS_SSL_MSG_CID) { |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 3126 | /* Shift pointers to account for record header including CID |
| 3127 | * struct { |
| 3128 | * ContentType special_type = tls12_cid; |
| 3129 | * ProtocolVersion version; |
| 3130 | * uint16 epoch; |
| 3131 | * uint48 sequence_number; |
Hanno Becker | 8e55b0f | 2019-05-23 17:03:19 +0100 | [diff] [blame] | 3132 | * opaque cid[cid_length]; // Additional field compared to |
| 3133 | * // default DTLS record format |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 3134 | * uint16 length; |
| 3135 | * opaque enc_content[DTLSCiphertext.length]; |
| 3136 | * } DTLSCiphertext; |
| 3137 | */ |
| 3138 | |
| 3139 | /* So far, we only support static CID lengths |
| 3140 | * fixed in the configuration. */ |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3141 | rec_hdr_cid_len = ssl->conf->cid_len; |
| 3142 | rec_hdr_len_offset += rec_hdr_cid_len; |
Hanno Becker | e538d82 | 2019-07-10 14:50:10 +0100 | [diff] [blame] | 3143 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3144 | if (len < rec_hdr_len_offset + rec_hdr_len_len) { |
| 3145 | MBEDTLS_SSL_DEBUG_MSG( |
| 3146 | 1, |
| 3147 | ("datagram of length %u too small to hold DTLS record header including CID, length %u", |
| 3148 | (unsigned)len, |
| 3149 | (unsigned)(rec_hdr_len_offset + rec_hdr_len_len))); |
| 3150 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | e538d82 | 2019-07-10 14:50:10 +0100 | [diff] [blame] | 3151 | } |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3152 | |
Manuel Pégourié-Gonnard | 7e821b5 | 2019-08-02 10:17:15 +0200 | [diff] [blame] | 3153 | /* configured CID len is guaranteed at most 255, see |
| 3154 | * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3155 | rec->cid_len = (uint8_t)rec_hdr_cid_len; |
| 3156 | memcpy(rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len); |
| 3157 | } else |
| 3158 | # endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 3159 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3160 | if (ssl_check_record_type(rec->type)) { |
| 3161 | MBEDTLS_SSL_DEBUG_MSG(1, ("unknown record type %u", |
| 3162 | (unsigned)rec->type)); |
| 3163 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3164 | } |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 3165 | } |
| 3166 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3167 | /* |
| 3168 | * Parse and validate record version |
| 3169 | */ |
| 3170 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3171 | rec->ver[0] = buf[rec_hdr_version_offset + 0]; |
| 3172 | rec->ver[1] = buf[rec_hdr_version_offset + 1]; |
| 3173 | mbedtls_ssl_read_version(&major_ver, &minor_ver, ssl->conf->transport, |
| 3174 | &rec->ver[0]); |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3175 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3176 | if (major_ver != ssl->major_ver) { |
| 3177 | MBEDTLS_SSL_DEBUG_MSG(1, ("major version mismatch")); |
| 3178 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3179 | } |
| 3180 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3181 | if (minor_ver > ssl->conf->max_minor_ver) { |
| 3182 | MBEDTLS_SSL_DEBUG_MSG(1, ("minor version mismatch")); |
| 3183 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3184 | } |
| 3185 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3186 | /* |
| 3187 | * Parse/Copy record sequence number. |
| 3188 | */ |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 3189 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3190 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3191 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3192 | /* Copy explicit record sequence number from input buffer. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3193 | memcpy(&rec->ctr[0], buf + rec_hdr_ctr_offset, rec_hdr_ctr_len); |
| 3194 | } else |
| 3195 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3196 | { |
| 3197 | /* Copy implicit record sequence number from SSL context structure. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3198 | memcpy(&rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len); |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3199 | } |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 3200 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3201 | /* |
| 3202 | * Parse record length. |
| 3203 | */ |
| 3204 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3205 | rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3206 | rec->data_len = ((size_t)buf[rec_hdr_len_offset + 0] << 8) | |
| 3207 | ((size_t)buf[rec_hdr_len_offset + 1] << 0); |
| 3208 | MBEDTLS_SSL_DEBUG_BUF(4, "input record header", buf, rec->data_offset); |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 3209 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3210 | MBEDTLS_SSL_DEBUG_MSG(3, |
| 3211 | ("input record: msgtype = %u, " |
| 3212 | "version = [%d:%d], msglen = %" MBEDTLS_PRINTF_SIZET, |
| 3213 | rec->type, major_ver, minor_ver, rec->data_len)); |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3214 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3215 | rec->buf = buf; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3216 | rec->buf_len = rec->data_offset + rec->data_len; |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 3217 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3218 | if (rec->data_len == 0) |
| 3219 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3220 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3221 | /* |
| 3222 | * DTLS-related tests. |
| 3223 | * Check epoch before checking length constraint because |
| 3224 | * the latter varies with the epoch. E.g., if a ChangeCipherSpec |
| 3225 | * message gets duplicated before the corresponding Finished message, |
| 3226 | * the second ChangeCipherSpec should be discarded because it belongs |
| 3227 | * to an old epoch, but not because its length is shorter than |
| 3228 | * the minimum record length for packets using the new record transform. |
| 3229 | * Note that these two kinds of failures are handled differently, |
| 3230 | * as an unexpected record is silently skipped but an invalid |
| 3231 | * record leads to the entire datagram being dropped. |
| 3232 | */ |
| 3233 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3234 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 3235 | rec_epoch = (rec->ctr[0] << 8) | rec->ctr[1]; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3236 | |
Hanno Becker | 955a5c9 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 3237 | /* Check that the datagram is large enough to contain a record |
| 3238 | * of the advertised length. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3239 | if (len < rec->data_offset + rec->data_len) { |
| 3240 | MBEDTLS_SSL_DEBUG_MSG( |
| 3241 | 1, |
| 3242 | ("Datagram of length %u too small to contain record of advertised length %u.", |
| 3243 | (unsigned)len, (unsigned)(rec->data_offset + rec->data_len))); |
| 3244 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | 955a5c9 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 3245 | } |
Hanno Becker | 37cfe73 | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 3246 | |
Hanno Becker | 37cfe73 | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 3247 | /* Records from other, non-matching epochs are silently discarded. |
| 3248 | * (The case of same-port Client reconnects must be considered in |
| 3249 | * the caller). */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3250 | if (rec_epoch != ssl->in_epoch) { |
| 3251 | MBEDTLS_SSL_DEBUG_MSG(1, ("record from another epoch: " |
| 3252 | "expected %u, received %lu", |
| 3253 | ssl->in_epoch, (unsigned long)rec_epoch)); |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3254 | |
Hanno Becker | 552f747 | 2019-07-19 10:59:12 +0100 | [diff] [blame] | 3255 | /* Records from the next epoch are considered for buffering |
| 3256 | * (concretely: early Finished messages). */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3257 | if (rec_epoch == (unsigned)ssl->in_epoch + 1) { |
| 3258 | MBEDTLS_SSL_DEBUG_MSG(2, ("Consider record for buffering")); |
| 3259 | return MBEDTLS_ERR_SSL_EARLY_MESSAGE; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3260 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 3261 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3262 | return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3263 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3264 | # if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Hanno Becker | 37cfe73 | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 3265 | /* For records from the correct epoch, check whether their |
| 3266 | * sequence number has been seen before. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3267 | else if (mbedtls_ssl_dtls_record_replay_check( |
| 3268 | (mbedtls_ssl_context *)ssl, &rec->ctr[0]) != 0) { |
| 3269 | MBEDTLS_SSL_DEBUG_MSG(1, ("replayed record")); |
| 3270 | return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3271 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3272 | # endif |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3273 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3274 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3275 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3276 | return 0; |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3277 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3278 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3279 | # if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && \ |
| 3280 | defined(MBEDTLS_SSL_SRV_C) |
| 3281 | static int ssl_check_client_reconnect(mbedtls_ssl_context *ssl) |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 3282 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3283 | 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] | 3284 | |
| 3285 | /* |
| 3286 | * Check for an epoch 0 ClientHello. We can't use in_msg here to |
| 3287 | * access the first byte of record content (handshake type), as we |
| 3288 | * have an active transform (possibly iv_len != 0), so use the |
| 3289 | * fact that the record header len is 13 instead. |
| 3290 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3291 | if (rec_epoch == 0 && ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 3292 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER && |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3293 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && ssl->in_left > 13 && |
| 3294 | ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO) { |
| 3295 | MBEDTLS_SSL_DEBUG_MSG(1, ("possible client reconnect " |
| 3296 | "from the same port")); |
| 3297 | return ssl_handle_possible_reconnect(ssl); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3298 | } |
| 3299 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3300 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3301 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3302 | # endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3303 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3304 | /* |
Manuel Pégourié-Gonnard | c40b685 | 2020-01-03 12:18:49 +0100 | [diff] [blame] | 3305 | * If applicable, decrypt record content |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3306 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3307 | static int ssl_prepare_record_content(mbedtls_ssl_context *ssl, |
| 3308 | mbedtls_record *rec) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3309 | { |
| 3310 | int ret, done = 0; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3311 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3312 | MBEDTLS_SSL_DEBUG_BUF(4, "input record from network", rec->buf, |
| 3313 | rec->buf_len); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3314 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3315 | if (!done && ssl->transform_in != NULL) { |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 3316 | unsigned char const old_msg_type = rec->type; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3317 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3318 | if ((ret = mbedtls_ssl_decrypt_buf(ssl, ssl->transform_in, rec)) != 0) { |
| 3319 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_decrypt_buf", ret); |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 3320 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3321 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3322 | if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID && |
| 3323 | ssl->conf->ignore_unexpected_cid == |
| 3324 | MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) { |
| 3325 | MBEDTLS_SSL_DEBUG_MSG(3, ("ignoring unexpected CID")); |
Hanno Becker | 16ded98 | 2019-05-08 13:02:55 +0100 | [diff] [blame] | 3326 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 3327 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3328 | # endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 16ded98 | 2019-05-08 13:02:55 +0100 | [diff] [blame] | 3329 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3330 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3331 | } |
| 3332 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3333 | if (old_msg_type != rec->type) { |
| 3334 | MBEDTLS_SSL_DEBUG_MSG(4, |
| 3335 | ("record type after decrypt (before %d): %d", |
| 3336 | old_msg_type, rec->type)); |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 3337 | } |
| 3338 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3339 | MBEDTLS_SSL_DEBUG_BUF(4, "input payload after decrypt", |
| 3340 | rec->buf + rec->data_offset, rec->data_len); |
Hanno Becker | 1c0c37f | 2018-08-07 14:29:29 +0100 | [diff] [blame] | 3341 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3342 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 3343 | /* We have already checked the record content type |
| 3344 | * in ssl_parse_record_header(), failing or silently |
| 3345 | * dropping the record in the case of an unknown type. |
| 3346 | * |
| 3347 | * Since with the use of CIDs, the record content type |
| 3348 | * might change during decryption, re-check the record |
| 3349 | * content type, but treat a failure as fatal this time. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3350 | if (ssl_check_record_type(rec->type)) { |
| 3351 | MBEDTLS_SSL_DEBUG_MSG(1, ("unknown record type")); |
| 3352 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 3353 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3354 | # endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 3355 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3356 | if (rec->data_len == 0) { |
| 3357 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3358 | if (ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 && |
| 3359 | rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA) { |
| 3360 | /* TLS v1.2 explicitly disallows zero-length messages which are |
| 3361 | * not application data */ |
| 3362 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 3363 | ("invalid zero-length message type: %d", |
| 3364 | ssl->in_msgtype)); |
| 3365 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3366 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3367 | # endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3368 | |
| 3369 | ssl->nb_zero++; |
| 3370 | |
| 3371 | /* |
| 3372 | * Three or more empty messages may be a DoS attack |
| 3373 | * (excessive CPU consumption). |
| 3374 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3375 | if (ssl->nb_zero > 3) { |
| 3376 | MBEDTLS_SSL_DEBUG_MSG(1, ("received four consecutive empty " |
| 3377 | "messages, possible DoS attack")); |
Hanno Becker | 6e7700d | 2019-05-08 10:38:32 +0100 | [diff] [blame] | 3378 | /* Treat the records as if they were not properly authenticated, |
| 3379 | * thereby failing the connection if we see more than allowed |
| 3380 | * by the configured bad MAC threshold. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3381 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3382 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3383 | } else |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3384 | ssl->nb_zero = 0; |
| 3385 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3386 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3387 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3388 | ; /* in_ctr read from peer, not maintained internally */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3389 | } else |
| 3390 | # endif |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3391 | { |
| 3392 | unsigned i; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3393 | for (i = 8; i > mbedtls_ssl_ep_len(ssl); i--) |
| 3394 | if (++ssl->in_ctr[i - 1] != 0) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3395 | break; |
| 3396 | |
| 3397 | /* The loop goes to its end iff the counter is wrapping */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3398 | if (i == mbedtls_ssl_ep_len(ssl)) { |
| 3399 | MBEDTLS_SSL_DEBUG_MSG(1, |
| 3400 | ("incoming message counter would wrap")); |
| 3401 | return MBEDTLS_ERR_SSL_COUNTER_WRAPPING; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3402 | } |
| 3403 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3404 | } |
| 3405 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3406 | # if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 3407 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 3408 | mbedtls_ssl_dtls_replay_update(ssl); |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 3409 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3410 | # endif |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 3411 | |
Hanno Becker | d96e10b | 2019-07-09 17:30:02 +0100 | [diff] [blame] | 3412 | /* Check actual (decrypted) record content length against |
| 3413 | * configured maximum. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3414 | if (ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN) { |
| 3415 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad message length")); |
| 3416 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | d96e10b | 2019-07-09 17:30:02 +0100 | [diff] [blame] | 3417 | } |
| 3418 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3419 | return 0; |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3420 | } |
| 3421 | |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 3422 | /* |
| 3423 | * Read a record. |
| 3424 | * |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 3425 | * Silently ignore non-fatal alert (and for DTLS, invalid records as well, |
| 3426 | * RFC 6347 4.1.2.7) and continue reading until a valid record is found. |
| 3427 | * |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 3428 | */ |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 3429 | |
| 3430 | /* Helper functions for mbedtls_ssl_read_record(). */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3431 | static int ssl_consume_current_message(mbedtls_ssl_context *ssl); |
| 3432 | static int ssl_get_next_record(mbedtls_ssl_context *ssl); |
| 3433 | static int ssl_record_is_in_progress(mbedtls_ssl_context *ssl); |
Hanno Becker | 4162b11 | 2018-08-15 14:05:04 +0100 | [diff] [blame] | 3434 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3435 | int mbedtls_ssl_read_record(mbedtls_ssl_context *ssl, unsigned update_hs_digest) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3436 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3437 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3438 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3439 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> read record")); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3440 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3441 | if (ssl->keep_current_message == 0) { |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3442 | do { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3443 | ret = ssl_consume_current_message(ssl); |
| 3444 | if (ret != 0) |
| 3445 | return ret; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3446 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3447 | if (ssl_record_is_in_progress(ssl) == 0) { |
| 3448 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3449 | int have_buffered = 0; |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 3450 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3451 | /* We only check for buffered messages if the |
| 3452 | * current datagram is fully consumed. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3453 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 3454 | ssl_next_record_is_in_datagram(ssl) == 0) { |
| 3455 | if (ssl_load_buffered_message(ssl) == 0) |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3456 | have_buffered = 1; |
| 3457 | } |
| 3458 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3459 | if (have_buffered == 0) |
| 3460 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3461 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3462 | ret = ssl_get_next_record(ssl); |
| 3463 | if (ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING) |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3464 | continue; |
| 3465 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3466 | if (ret != 0) { |
| 3467 | MBEDTLS_SSL_DEBUG_RET(1, ("ssl_get_next_record"), ret); |
| 3468 | return ret; |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3469 | } |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 3470 | } |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3471 | } |
| 3472 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3473 | ret = mbedtls_ssl_handle_message_type(ssl); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3474 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3475 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3476 | if (ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3477 | /* Buffer future message */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3478 | ret = ssl_buffer_message(ssl); |
| 3479 | if (ret != 0) |
| 3480 | return ret; |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3481 | |
| 3482 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
| 3483 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3484 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3485 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3486 | } while (MBEDTLS_ERR_SSL_NON_FATAL == ret || |
| 3487 | MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3488 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3489 | if (0 != ret) { |
| 3490 | MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_handle_message_type"), ret); |
| 3491 | return ret; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3492 | } |
| 3493 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3494 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3495 | update_hs_digest == 1) { |
| 3496 | mbedtls_ssl_update_handshake_status(ssl); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3497 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3498 | } else { |
| 3499 | MBEDTLS_SSL_DEBUG_MSG(2, ("reuse previously read message")); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3500 | ssl->keep_current_message = 0; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3501 | } |
| 3502 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3503 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= read record")); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3504 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3505 | return 0; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3506 | } |
| 3507 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3508 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3509 | static int ssl_next_record_is_in_datagram(mbedtls_ssl_context *ssl) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3510 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3511 | if (ssl->in_left > ssl->next_record_offset) |
| 3512 | return 1; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3513 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3514 | return 0; |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3515 | } |
| 3516 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3517 | static int ssl_load_buffered_message(mbedtls_ssl_context *ssl) |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3518 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3519 | mbedtls_ssl_handshake_params *const hs = ssl->handshake; |
| 3520 | mbedtls_ssl_hs_buffer *hs_buf; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3521 | int ret = 0; |
| 3522 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3523 | if (hs == NULL) |
| 3524 | return -1; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3525 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3526 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_load_buffered_messsage")); |
Hanno Becker | e00ae37 | 2018-08-20 09:39:42 +0100 | [diff] [blame] | 3527 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3528 | if (ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC || |
| 3529 | ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC) { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3530 | /* Check if we have seen a ChangeCipherSpec before. |
| 3531 | * If yes, synthesize a CCS record. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3532 | if (!hs->buffering.seen_ccs) { |
| 3533 | MBEDTLS_SSL_DEBUG_MSG(2, ("CCS not seen in the current flight")); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3534 | ret = -1; |
Hanno Becker | 0d4b376 | 2018-08-20 09:36:59 +0100 | [diff] [blame] | 3535 | goto exit; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3536 | } |
| 3537 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3538 | MBEDTLS_SSL_DEBUG_MSG(2, ("Injecting buffered CCS message")); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3539 | ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; |
| 3540 | ssl->in_msglen = 1; |
| 3541 | ssl->in_msg[0] = 1; |
| 3542 | |
| 3543 | /* As long as they are equal, the exact value doesn't matter. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3544 | ssl->in_left = 0; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3545 | ssl->next_record_offset = 0; |
| 3546 | |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 3547 | hs->buffering.seen_ccs = 0; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3548 | goto exit; |
| 3549 | } |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3550 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3551 | # if defined(MBEDTLS_DEBUG_C) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3552 | /* Debug only */ |
| 3553 | { |
| 3554 | unsigned offset; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3555 | for (offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++) { |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3556 | hs_buf = &hs->buffering.hs[offset]; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3557 | if (hs_buf->is_valid == 1) { |
| 3558 | MBEDTLS_SSL_DEBUG_MSG( |
| 3559 | 2, ("Future message with sequence number %u %s buffered.", |
| 3560 | hs->in_msg_seq + offset, |
| 3561 | hs_buf->is_complete ? "fully" : "partially")); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3562 | } |
| 3563 | } |
| 3564 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3565 | # endif /* MBEDTLS_DEBUG_C */ |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3566 | |
| 3567 | /* Check if we have buffered and/or fully reassembled the |
| 3568 | * next handshake message. */ |
| 3569 | hs_buf = &hs->buffering.hs[0]; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3570 | if ((hs_buf->is_valid == 1) && (hs_buf->is_complete == 1)) { |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3571 | /* Synthesize a record containing the buffered HS message. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3572 | size_t msg_len = (hs_buf->data[1] << 16) | (hs_buf->data[2] << 8) | |
| 3573 | hs_buf->data[3]; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3574 | |
| 3575 | /* Double-check that we haven't accidentally buffered |
| 3576 | * a message that doesn't fit into the input buffer. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3577 | if (msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN) { |
| 3578 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 3579 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3580 | } |
| 3581 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3582 | MBEDTLS_SSL_DEBUG_MSG( |
| 3583 | 2, ("Next handshake message has been buffered - load")); |
| 3584 | MBEDTLS_SSL_DEBUG_BUF(3, "Buffered handshake message (incl. header)", |
| 3585 | hs_buf->data, msg_len + 12); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3586 | |
| 3587 | ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3588 | ssl->in_hslen = msg_len + 12; |
| 3589 | ssl->in_msglen = msg_len + 12; |
| 3590 | memcpy(ssl->in_msg, hs_buf->data, ssl->in_hslen); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3591 | |
| 3592 | ret = 0; |
| 3593 | goto exit; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3594 | } else { |
| 3595 | MBEDTLS_SSL_DEBUG_MSG( |
| 3596 | 2, ("Next handshake message %u not or only partially bufffered", |
| 3597 | hs->in_msg_seq)); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3598 | } |
| 3599 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3600 | ret = -1; |
| 3601 | |
| 3602 | exit: |
| 3603 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3604 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_load_buffered_message")); |
| 3605 | return ret; |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3606 | } |
| 3607 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3608 | static int ssl_buffer_make_space(mbedtls_ssl_context *ssl, size_t desired) |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 3609 | { |
| 3610 | int offset; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3611 | mbedtls_ssl_handshake_params *const hs = ssl->handshake; |
| 3612 | MBEDTLS_SSL_DEBUG_MSG( |
| 3613 | 2, ("Attempt to free buffered messages to have %u bytes available", |
| 3614 | (unsigned)desired)); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 3615 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 3616 | /* Get rid of future records epoch first, if such exist. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3617 | ssl_free_buffered_record(ssl); |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 3618 | |
| 3619 | /* Check if we have enough space available now. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3620 | if (desired <= |
| 3621 | (MBEDTLS_SSL_DTLS_MAX_BUFFERING - hs->buffering.total_bytes_buffered)) { |
| 3622 | MBEDTLS_SSL_DEBUG_MSG( |
| 3623 | 2, ("Enough space available after freeing future epoch record")); |
| 3624 | return 0; |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 3625 | } |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 3626 | |
Hanno Becker | 4f432ad | 2018-08-28 10:02:32 +0100 | [diff] [blame] | 3627 | /* We don't have enough space to buffer the next expected handshake |
| 3628 | * message. Remove buffers used for future messages to gain space, |
| 3629 | * starting with the most distant one. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3630 | for (offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1; offset >= 0; offset--) { |
| 3631 | MBEDTLS_SSL_DEBUG_MSG( |
| 3632 | 2, |
| 3633 | ("Free buffering slot %d to make space for reassembly of next handshake message", |
| 3634 | offset)); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 3635 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3636 | ssl_buffering_free_slot(ssl, (uint8_t)offset); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 3637 | |
| 3638 | /* Check if we have enough space available now. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3639 | if (desired <= (MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 3640 | hs->buffering.total_bytes_buffered)) { |
| 3641 | MBEDTLS_SSL_DEBUG_MSG( |
| 3642 | 2, |
| 3643 | ("Enough space available after freeing buffered HS messages")); |
| 3644 | return 0; |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 3645 | } |
| 3646 | } |
| 3647 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3648 | return -1; |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 3649 | } |
| 3650 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3651 | static int ssl_buffer_message(mbedtls_ssl_context *ssl) |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3652 | { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3653 | int ret = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3654 | mbedtls_ssl_handshake_params *const hs = ssl->handshake; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3655 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3656 | if (hs == NULL) |
| 3657 | return 0; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3658 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3659 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_buffer_message")); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3660 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3661 | switch (ssl->in_msgtype) { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3662 | case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3663 | MBEDTLS_SSL_DEBUG_MSG(2, ("Remember CCS message")); |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 3664 | |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 3665 | hs->buffering.seen_ccs = 1; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3666 | break; |
| 3667 | |
| 3668 | case MBEDTLS_SSL_MSG_HANDSHAKE: |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3669 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3670 | unsigned recv_msg_seq_offset; |
| 3671 | unsigned recv_msg_seq = (ssl->in_msg[4] << 8) | ssl->in_msg[5]; |
| 3672 | mbedtls_ssl_hs_buffer *hs_buf; |
| 3673 | size_t msg_len = ssl->in_hslen - 12; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3674 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3675 | /* We should never receive an old handshake |
| 3676 | * message - double-check nonetheless. */ |
| 3677 | if (recv_msg_seq < ssl->handshake->in_msg_seq) { |
| 3678 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 3679 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 3680 | } |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3681 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3682 | recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq; |
| 3683 | if (recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS) { |
| 3684 | /* Silently ignore -- message too far in the future */ |
| 3685 | MBEDTLS_SSL_DEBUG_MSG( |
| 3686 | 2, ("Ignore future HS message with sequence number %u, " |
| 3687 | "buffering window %u - %u", |
| 3688 | recv_msg_seq, ssl->handshake->in_msg_seq, |
| 3689 | ssl->handshake->in_msg_seq + |
| 3690 | MBEDTLS_SSL_MAX_BUFFERED_HS - 1)); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3691 | |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3692 | goto exit; |
| 3693 | } |
| 3694 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3695 | MBEDTLS_SSL_DEBUG_MSG( |
| 3696 | 2, |
| 3697 | ("Buffering HS message with sequence number %u, offset %u ", |
| 3698 | recv_msg_seq, recv_msg_seq_offset)); |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 3699 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3700 | hs_buf = &hs->buffering.hs[recv_msg_seq_offset]; |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 3701 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3702 | /* Check if the buffering for this seq nr has already commenced. |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3703 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3704 | if (!hs_buf->is_valid) { |
| 3705 | size_t reassembly_buf_sz; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3706 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3707 | hs_buf->is_fragmented = |
| 3708 | (ssl_hs_is_proper_fragment(ssl) == 1); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3709 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3710 | /* We copy the message back into the input buffer |
| 3711 | * after reassembly, so check that it's not too large. |
| 3712 | * This is an implementation-specific limitation |
| 3713 | * and not one from the standard, hence it is not |
| 3714 | * checked in ssl_check_hs_header(). */ |
| 3715 | if (msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN) { |
| 3716 | /* Ignore message */ |
| 3717 | goto exit; |
| 3718 | } |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3719 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3720 | /* Check if we have enough space to buffer the message. */ |
| 3721 | if (hs->buffering.total_bytes_buffered > |
| 3722 | MBEDTLS_SSL_DTLS_MAX_BUFFERING) { |
| 3723 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 3724 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
| 3725 | } |
| 3726 | |
| 3727 | reassembly_buf_sz = ssl_get_reassembly_buffer_size( |
| 3728 | msg_len, hs_buf->is_fragmented); |
| 3729 | |
| 3730 | if (reassembly_buf_sz > |
| 3731 | (MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 3732 | hs->buffering.total_bytes_buffered)) { |
| 3733 | if (recv_msg_seq_offset > 0) { |
| 3734 | /* If we can't buffer a future message because |
| 3735 | * of space limitations -- ignore. */ |
| 3736 | MBEDTLS_SSL_DEBUG_MSG( |
| 3737 | 2, |
| 3738 | ("Buffering of future message of size %" MBEDTLS_PRINTF_SIZET |
| 3739 | " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET |
| 3740 | " (already %" MBEDTLS_PRINTF_SIZET |
| 3741 | " bytes buffered) -- ignore\n", |
| 3742 | msg_len, |
| 3743 | (size_t)MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 3744 | hs->buffering.total_bytes_buffered)); |
| 3745 | goto exit; |
| 3746 | } else { |
| 3747 | MBEDTLS_SSL_DEBUG_MSG( |
| 3748 | 2, |
| 3749 | ("Buffering of future message of size %" MBEDTLS_PRINTF_SIZET |
| 3750 | " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET |
| 3751 | " (already %" MBEDTLS_PRINTF_SIZET |
| 3752 | " bytes buffered) -- attempt to make space by freeing buffered future messages\n", |
| 3753 | msg_len, |
| 3754 | (size_t)MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 3755 | hs->buffering.total_bytes_buffered)); |
| 3756 | } |
| 3757 | |
| 3758 | if (ssl_buffer_make_space(ssl, reassembly_buf_sz) != |
| 3759 | 0) { |
| 3760 | MBEDTLS_SSL_DEBUG_MSG( |
| 3761 | 2, |
| 3762 | ("Reassembly of next message of size %" MBEDTLS_PRINTF_SIZET |
| 3763 | " (%" MBEDTLS_PRINTF_SIZET |
| 3764 | " with bitmap) would exceed" |
| 3765 | " the compile-time limit %" MBEDTLS_PRINTF_SIZET |
| 3766 | " (already %" MBEDTLS_PRINTF_SIZET |
| 3767 | " bytes buffered) -- fail\n", |
| 3768 | msg_len, reassembly_buf_sz, |
| 3769 | (size_t)MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 3770 | hs->buffering.total_bytes_buffered)); |
| 3771 | ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 3772 | goto exit; |
| 3773 | } |
| 3774 | } |
| 3775 | |
| 3776 | MBEDTLS_SSL_DEBUG_MSG( |
| 3777 | 2, |
| 3778 | ("initialize reassembly, total length = %" MBEDTLS_PRINTF_SIZET, |
| 3779 | msg_len)); |
| 3780 | |
| 3781 | hs_buf->data = mbedtls_calloc(1, reassembly_buf_sz); |
| 3782 | if (hs_buf->data == NULL) { |
| 3783 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 3784 | goto exit; |
| 3785 | } |
| 3786 | hs_buf->data_len = reassembly_buf_sz; |
| 3787 | |
| 3788 | /* Prepare final header: copy msg_type, length and |
| 3789 | * message_seq, then add standardised fragment_offset and |
| 3790 | * fragment_length */ |
| 3791 | memcpy(hs_buf->data, ssl->in_msg, 6); |
| 3792 | memset(hs_buf->data + 6, 0, 3); |
| 3793 | memcpy(hs_buf->data + 9, hs_buf->data + 1, 3); |
| 3794 | |
| 3795 | hs_buf->is_valid = 1; |
| 3796 | |
| 3797 | hs->buffering.total_bytes_buffered += reassembly_buf_sz; |
| 3798 | } else { |
| 3799 | /* Make sure msg_type and length are consistent */ |
| 3800 | if (memcmp(hs_buf->data, ssl->in_msg, 4) != 0) { |
| 3801 | MBEDTLS_SSL_DEBUG_MSG( |
| 3802 | 1, ("Fragment header mismatch - ignore")); |
| 3803 | /* Ignore */ |
| 3804 | goto exit; |
| 3805 | } |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3806 | } |
| 3807 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3808 | if (!hs_buf->is_complete) { |
| 3809 | size_t frag_len, frag_off; |
| 3810 | unsigned char *const msg = hs_buf->data + 12; |
| 3811 | |
| 3812 | /* |
| 3813 | * Check and copy current fragment |
| 3814 | */ |
| 3815 | |
| 3816 | /* Validation of header fields already done in |
| 3817 | * mbedtls_ssl_prepare_handshake_record(). */ |
| 3818 | frag_off = ssl_get_hs_frag_off(ssl); |
| 3819 | frag_len = ssl_get_hs_frag_len(ssl); |
| 3820 | |
| 3821 | MBEDTLS_SSL_DEBUG_MSG( |
| 3822 | 2, ("adding fragment, offset = %" MBEDTLS_PRINTF_SIZET |
| 3823 | ", length = %" MBEDTLS_PRINTF_SIZET, |
| 3824 | frag_off, frag_len)); |
| 3825 | memcpy(msg + frag_off, ssl->in_msg + 12, frag_len); |
| 3826 | |
| 3827 | if (hs_buf->is_fragmented) { |
| 3828 | unsigned char *const bitmask = msg + msg_len; |
| 3829 | ssl_bitmask_set(bitmask, frag_off, frag_len); |
| 3830 | hs_buf->is_complete = |
| 3831 | (ssl_bitmask_check(bitmask, msg_len) == 0); |
| 3832 | } else { |
| 3833 | hs_buf->is_complete = 1; |
| 3834 | } |
| 3835 | |
| 3836 | MBEDTLS_SSL_DEBUG_MSG( |
| 3837 | 2, ("message %scomplete", |
| 3838 | hs_buf->is_complete ? "" : "not yet ")); |
| 3839 | } |
| 3840 | |
| 3841 | break; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3842 | } |
| 3843 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3844 | default: |
Hanno Becker | 360bef3 | 2018-08-28 10:04:33 +0100 | [diff] [blame] | 3845 | /* We don't buffer other types of messages. */ |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3846 | break; |
| 3847 | } |
| 3848 | |
| 3849 | exit: |
| 3850 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3851 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_buffer_message")); |
| 3852 | return ret; |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3853 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3854 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3855 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3856 | static int ssl_consume_current_message(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3857 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 3858 | /* |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 3859 | * Consume last content-layer message and potentially |
| 3860 | * update in_msglen which keeps track of the contents' |
| 3861 | * consumption state. |
| 3862 | * |
| 3863 | * (1) Handshake messages: |
| 3864 | * Remove last handshake message, move content |
| 3865 | * and adapt in_msglen. |
| 3866 | * |
| 3867 | * (2) Alert messages: |
| 3868 | * Consume whole record content, in_msglen = 0. |
| 3869 | * |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 3870 | * (3) Change cipher spec: |
| 3871 | * Consume whole record content, in_msglen = 0. |
| 3872 | * |
| 3873 | * (4) Application data: |
| 3874 | * Don't do anything - the record layer provides |
| 3875 | * the application data as a stream transport |
| 3876 | * and consumes through mbedtls_ssl_read only. |
| 3877 | * |
| 3878 | */ |
| 3879 | |
| 3880 | /* Case (1): Handshake messages */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3881 | if (ssl->in_hslen != 0) { |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 3882 | /* Hard assertion to be sure that no application data |
| 3883 | * is in flight, as corrupting ssl->in_msglen during |
| 3884 | * ssl->in_offt != NULL is fatal. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3885 | if (ssl->in_offt != NULL) { |
| 3886 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 3887 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 3888 | } |
| 3889 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3890 | /* |
| 3891 | * Get next Handshake message in the current record |
| 3892 | */ |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3893 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 3894 | /* Notes: |
Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 3895 | * (1) in_hslen is not necessarily the size of the |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 3896 | * current handshake content: If DTLS handshake |
| 3897 | * fragmentation is used, that's the fragment |
| 3898 | * size instead. Using the total handshake message |
Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 3899 | * size here is faulty and should be changed at |
| 3900 | * some point. |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 3901 | * (2) While it doesn't seem to cause problems, one |
| 3902 | * has to be very careful not to assume that in_hslen |
| 3903 | * is always <= in_msglen in a sensible communication. |
| 3904 | * Again, it's wrong for DTLS handshake fragmentation. |
| 3905 | * The following check is therefore mandatory, and |
| 3906 | * should not be treated as a silently corrected assertion. |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 3907 | * Additionally, ssl->in_hslen might be arbitrarily out of |
| 3908 | * bounds after handling a DTLS message with an unexpected |
| 3909 | * sequence number, see mbedtls_ssl_prepare_handshake_record. |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 3910 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3911 | if (ssl->in_hslen < ssl->in_msglen) { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 3912 | ssl->in_msglen -= ssl->in_hslen; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3913 | memmove(ssl->in_msg, ssl->in_msg + ssl->in_hslen, ssl->in_msglen); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3914 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3915 | MBEDTLS_SSL_DEBUG_BUF(4, "remaining content in record", ssl->in_msg, |
| 3916 | ssl->in_msglen); |
| 3917 | } else { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 3918 | ssl->in_msglen = 0; |
| 3919 | } |
Manuel Pégourié-Gonnard | 4a17536 | 2014-09-09 17:45:31 +0200 | [diff] [blame] | 3920 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3921 | ssl->in_hslen = 0; |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 3922 | } |
| 3923 | /* Case (4): Application data */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3924 | else if (ssl->in_offt != NULL) { |
| 3925 | return 0; |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 3926 | } |
| 3927 | /* Everything else (CCS & Alerts) */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3928 | else { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 3929 | ssl->in_msglen = 0; |
| 3930 | } |
| 3931 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3932 | return 0; |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 3933 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 3934 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3935 | static int ssl_record_is_in_progress(mbedtls_ssl_context *ssl) |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 3936 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3937 | if (ssl->in_msglen > 0) |
| 3938 | return 1; |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 3939 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3940 | return 0; |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 3941 | } |
| 3942 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3943 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 3944 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3945 | static void ssl_free_buffered_record(mbedtls_ssl_context *ssl) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 3946 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3947 | mbedtls_ssl_handshake_params *const hs = ssl->handshake; |
| 3948 | if (hs == NULL) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 3949 | return; |
| 3950 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3951 | if (hs->buffering.future_record.data != NULL) { |
| 3952 | hs->buffering.total_bytes_buffered -= hs->buffering.future_record.len; |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 3953 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3954 | mbedtls_free(hs->buffering.future_record.data); |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 3955 | hs->buffering.future_record.data = NULL; |
| 3956 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 3957 | } |
| 3958 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3959 | static int ssl_load_buffered_record(mbedtls_ssl_context *ssl) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 3960 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3961 | mbedtls_ssl_handshake_params *const hs = ssl->handshake; |
| 3962 | unsigned char *rec; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 3963 | size_t rec_len; |
| 3964 | unsigned rec_epoch; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3965 | # if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3966 | size_t in_buf_len = ssl->in_buf_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3967 | # else |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 3968 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3969 | # endif |
| 3970 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) |
| 3971 | return 0; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 3972 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3973 | if (hs == NULL) |
| 3974 | return 0; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 3975 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3976 | rec = hs->buffering.future_record.data; |
| 3977 | rec_len = hs->buffering.future_record.len; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 3978 | rec_epoch = hs->buffering.future_record.epoch; |
| 3979 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3980 | if (rec == NULL) |
| 3981 | return 0; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 3982 | |
Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 3983 | /* Only consider loading future records if the |
| 3984 | * input buffer is empty. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3985 | if (ssl_next_record_is_in_datagram(ssl) == 1) |
| 3986 | return 0; |
Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 3987 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3988 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_load_buffered_record")); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 3989 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3990 | if (rec_epoch != ssl->in_epoch) { |
| 3991 | MBEDTLS_SSL_DEBUG_MSG(2, ("Buffered record not from current epoch.")); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 3992 | goto exit; |
| 3993 | } |
| 3994 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3995 | MBEDTLS_SSL_DEBUG_MSG(2, |
| 3996 | ("Found buffered record from current epoch - load")); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 3997 | |
| 3998 | /* Double-check that the record is not too large */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 3999 | if (rec_len > in_buf_len - (size_t)(ssl->in_hdr - ssl->in_buf)) { |
| 4000 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 4001 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4002 | } |
| 4003 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4004 | memcpy(ssl->in_hdr, rec, rec_len); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4005 | ssl->in_left = rec_len; |
| 4006 | ssl->next_record_offset = 0; |
| 4007 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4008 | ssl_free_buffered_record(ssl); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4009 | |
| 4010 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4011 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_load_buffered_record")); |
| 4012 | return 0; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4013 | } |
| 4014 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4015 | static int ssl_buffer_future_record(mbedtls_ssl_context *ssl, |
| 4016 | mbedtls_record const *rec) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4017 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4018 | mbedtls_ssl_handshake_params *const hs = ssl->handshake; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4019 | |
| 4020 | /* Don't buffer future records outside handshakes. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4021 | if (hs == NULL) |
| 4022 | return 0; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4023 | |
| 4024 | /* Only buffer handshake records (we are only interested |
| 4025 | * in Finished messages). */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4026 | if (rec->type != MBEDTLS_SSL_MSG_HANDSHAKE) |
| 4027 | return 0; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4028 | |
| 4029 | /* Don't buffer more than one future epoch record. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4030 | if (hs->buffering.future_record.data != NULL) |
| 4031 | return 0; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4032 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 4033 | /* Don't buffer record if there's not enough buffering space remaining. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4034 | if (rec->buf_len > |
| 4035 | (MBEDTLS_SSL_DTLS_MAX_BUFFERING - hs->buffering.total_bytes_buffered)) { |
| 4036 | MBEDTLS_SSL_DEBUG_MSG( |
| 4037 | 2, |
| 4038 | ("Buffering of future epoch record of size %" MBEDTLS_PRINTF_SIZET |
| 4039 | " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET |
| 4040 | " (already %" MBEDTLS_PRINTF_SIZET " bytes buffered) -- ignore\n", |
| 4041 | rec->buf_len, (size_t)MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
| 4042 | hs->buffering.total_bytes_buffered)); |
| 4043 | return 0; |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4044 | } |
| 4045 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4046 | /* Buffer record */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4047 | MBEDTLS_SSL_DEBUG_MSG(2, |
| 4048 | ("Buffer record from epoch %u", ssl->in_epoch + 1U)); |
| 4049 | MBEDTLS_SSL_DEBUG_BUF(3, "Buffered record", rec->buf, rec->buf_len); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4050 | |
| 4051 | /* ssl_parse_record_header() only considers records |
| 4052 | * of the next epoch as candidates for buffering. */ |
| 4053 | hs->buffering.future_record.epoch = ssl->in_epoch + 1; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4054 | hs->buffering.future_record.len = rec->buf_len; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4055 | |
| 4056 | hs->buffering.future_record.data = |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4057 | mbedtls_calloc(1, hs->buffering.future_record.len); |
| 4058 | if (hs->buffering.future_record.data == NULL) { |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4059 | /* If we run out of RAM trying to buffer a |
| 4060 | * record from the next epoch, just ignore. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4061 | return 0; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4062 | } |
| 4063 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4064 | memcpy(hs->buffering.future_record.data, rec->buf, rec->buf_len); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4065 | |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 4066 | hs->buffering.total_bytes_buffered += rec->buf_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4067 | return 0; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4068 | } |
| 4069 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4070 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4071 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4072 | static int ssl_get_next_record(mbedtls_ssl_context *ssl) |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 4073 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4074 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 4075 | mbedtls_record rec; |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 4076 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4077 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4078 | /* We might have buffered a future record; if so, |
| 4079 | * and if the epoch matches now, load it. |
| 4080 | * On success, this call will set ssl->in_left to |
| 4081 | * the length of the buffered record, so that |
| 4082 | * the calls to ssl_fetch_input() below will |
| 4083 | * essentially be no-ops. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4084 | ret = ssl_load_buffered_record(ssl); |
| 4085 | if (ret != 0) |
| 4086 | return ret; |
| 4087 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4088 | |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4089 | /* Ensure that we have enough space available for the default form |
| 4090 | * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS, |
| 4091 | * with no space for CIDs counted in). */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4092 | ret = mbedtls_ssl_fetch_input(ssl, mbedtls_ssl_in_hdr_len(ssl)); |
| 4093 | if (ret != 0) { |
| 4094 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret); |
| 4095 | return ret; |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4096 | } |
| 4097 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4098 | ret = ssl_parse_record_header(ssl, ssl->in_hdr, ssl->in_left, &rec); |
| 4099 | if (ret != 0) { |
| 4100 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4101 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 4102 | if (ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE) { |
| 4103 | ret = ssl_buffer_future_record(ssl, &rec); |
| 4104 | if (ret != 0) |
| 4105 | return ret; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4106 | |
| 4107 | /* Fall through to handling of unexpected records */ |
| 4108 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
| 4109 | } |
| 4110 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4111 | if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD) { |
| 4112 | # if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && \ |
| 4113 | defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | d8bf8ce | 2019-07-12 09:23:47 +0100 | [diff] [blame] | 4114 | /* Reset in pointers to default state for TLS/DTLS records, |
| 4115 | * assuming no CID and no offset between record content and |
| 4116 | * record plaintext. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4117 | mbedtls_ssl_update_in_pointers(ssl); |
Hanno Becker | d8bf8ce | 2019-07-12 09:23:47 +0100 | [diff] [blame] | 4118 | |
Hanno Becker | 7ae20e0 | 2019-07-12 08:33:49 +0100 | [diff] [blame] | 4119 | /* Setup internal message pointers from record structure. */ |
| 4120 | ssl->in_msgtype = rec.type; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4121 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 7ae20e0 | 2019-07-12 08:33:49 +0100 | [diff] [blame] | 4122 | ssl->in_len = ssl->in_cid + rec.cid_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4123 | # endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4124 | ssl->in_iv = ssl->in_msg = ssl->in_len + 2; |
Hanno Becker | 7ae20e0 | 2019-07-12 08:33:49 +0100 | [diff] [blame] | 4125 | ssl->in_msglen = rec.data_len; |
| 4126 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4127 | ret = ssl_check_client_reconnect(ssl); |
| 4128 | MBEDTLS_SSL_DEBUG_RET(2, "ssl_check_client_reconnect", ret); |
| 4129 | if (ret != 0) |
| 4130 | return ret; |
| 4131 | # endif |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 4132 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4133 | /* Skip unexpected record (but not whole datagram) */ |
Hanno Becker | 4acada3 | 2019-07-11 12:48:53 +0100 | [diff] [blame] | 4134 | ssl->next_record_offset = rec.buf_len; |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4135 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4136 | MBEDTLS_SSL_DEBUG_MSG(1, ("discarding unexpected record " |
| 4137 | "(header)")); |
| 4138 | } else { |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4139 | /* Skip invalid record and the rest of the datagram */ |
| 4140 | ssl->next_record_offset = 0; |
| 4141 | ssl->in_left = 0; |
| 4142 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4143 | MBEDTLS_SSL_DEBUG_MSG(1, ("discarding invalid record " |
| 4144 | "(header)")); |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4145 | } |
| 4146 | |
| 4147 | /* Get next record */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4148 | return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
| 4149 | } else |
| 4150 | # endif |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 4151 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4152 | return ret; |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 4153 | } |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4154 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4155 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4156 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4157 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 4158 | /* Remember offset of next record within datagram. */ |
Hanno Becker | f50da50 | 2019-07-11 12:50:10 +0100 | [diff] [blame] | 4159 | ssl->next_record_offset = rec.buf_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4160 | if (ssl->next_record_offset < ssl->in_left) { |
| 4161 | MBEDTLS_SSL_DEBUG_MSG(3, ("more than one record within datagram")); |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 4162 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4163 | } else |
| 4164 | # endif |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 4165 | { |
Hanno Becker | 955a5c9 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 4166 | /* |
| 4167 | * Fetch record contents from underlying transport. |
| 4168 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4169 | ret = mbedtls_ssl_fetch_input(ssl, rec.buf_len); |
| 4170 | if (ret != 0) { |
| 4171 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_fetch_input", ret); |
| 4172 | return ret; |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 4173 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4174 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4175 | ssl->in_left = 0; |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 4176 | } |
| 4177 | |
| 4178 | /* |
| 4179 | * Decrypt record contents. |
| 4180 | */ |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4181 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4182 | if ((ret = ssl_prepare_record_content(ssl, &rec)) != 0) { |
| 4183 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4184 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4185 | /* Silently discard invalid records */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4186 | if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) { |
Manuel Pégourié-Gonnard | 0a88574 | 2015-08-04 12:08:35 +0200 | [diff] [blame] | 4187 | /* Except when waiting for Finished as a bad mac here |
| 4188 | * probably means something went wrong in the handshake |
| 4189 | * (eg wrong psk used, mitm downgrade attempt, etc.) */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4190 | if (ssl->state == MBEDTLS_SSL_CLIENT_FINISHED || |
| 4191 | ssl->state == MBEDTLS_SSL_SERVER_FINISHED) { |
| 4192 | # if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) |
| 4193 | if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) { |
| 4194 | mbedtls_ssl_send_alert_message( |
| 4195 | ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 4196 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC); |
Manuel Pégourié-Gonnard | 0a88574 | 2015-08-04 12:08:35 +0200 | [diff] [blame] | 4197 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4198 | # endif |
| 4199 | return ret; |
Manuel Pégourié-Gonnard | 0a88574 | 2015-08-04 12:08:35 +0200 | [diff] [blame] | 4200 | } |
| 4201 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4202 | if (ssl->conf->badmac_limit != 0 && |
| 4203 | ++ssl->badmac_seen >= ssl->conf->badmac_limit) { |
| 4204 | MBEDTLS_SSL_DEBUG_MSG(1, ("too many records with bad MAC")); |
| 4205 | return MBEDTLS_ERR_SSL_INVALID_MAC; |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 4206 | } |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 4207 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4208 | /* As above, invalid records cause |
| 4209 | * dismissal of the whole datagram. */ |
| 4210 | |
| 4211 | ssl->next_record_offset = 0; |
| 4212 | ssl->in_left = 0; |
| 4213 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4214 | MBEDTLS_SSL_DEBUG_MSG(1, ("discarding invalid record (mac)")); |
| 4215 | return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4216 | } |
| 4217 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4218 | return ret; |
| 4219 | } else |
| 4220 | # endif |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4221 | { |
| 4222 | /* Error out (and send alert) on invalid records */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4223 | # if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) |
| 4224 | if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) { |
| 4225 | mbedtls_ssl_send_alert_message( |
| 4226 | ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 4227 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4228 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4229 | # endif |
| 4230 | return ret; |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4231 | } |
| 4232 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4233 | |
Hanno Becker | 44d89b2 | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 4234 | /* Reset in pointers to default state for TLS/DTLS records, |
| 4235 | * assuming no CID and no offset between record content and |
| 4236 | * record plaintext. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4237 | mbedtls_ssl_update_in_pointers(ssl); |
| 4238 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 44d89b2 | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 4239 | ssl->in_len = ssl->in_cid + rec.cid_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4240 | # endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4241 | ssl->in_iv = ssl->in_len + 2; |
Hanno Becker | 44d89b2 | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 4242 | |
Hanno Becker | 8685c82 | 2019-07-12 09:37:30 +0100 | [diff] [blame] | 4243 | /* The record content type may change during decryption, |
| 4244 | * so re-read it. */ |
| 4245 | ssl->in_msgtype = rec.type; |
| 4246 | /* Also update the input buffer, because unfortunately |
| 4247 | * the server-side ssl_parse_client_hello() reparses the |
| 4248 | * record header when receiving a ClientHello initiating |
| 4249 | * a renegotiation. */ |
| 4250 | ssl->in_hdr[0] = rec.type; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4251 | ssl->in_msg = rec.buf + rec.data_offset; |
Hanno Becker | 8685c82 | 2019-07-12 09:37:30 +0100 | [diff] [blame] | 4252 | ssl->in_msglen = rec.data_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4253 | ssl->in_len[0] = (unsigned char)(rec.data_len >> 8); |
| 4254 | ssl->in_len[1] = (unsigned char)(rec.data_len); |
Hanno Becker | 8685c82 | 2019-07-12 09:37:30 +0100 | [diff] [blame] | 4255 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4256 | return 0; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4257 | } |
| 4258 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4259 | int mbedtls_ssl_handle_message_type(mbedtls_ssl_context *ssl) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4260 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4261 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4262 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 4263 | /* |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4264 | * Handle particular types of records |
| 4265 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4266 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 4267 | if ((ret = mbedtls_ssl_prepare_handshake_record(ssl)) != 0) { |
| 4268 | return ret; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4269 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4270 | } |
| 4271 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4272 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) { |
| 4273 | if (ssl->in_msglen != 1) { |
| 4274 | MBEDTLS_SSL_DEBUG_MSG( |
| 4275 | 1, ("invalid CCS message, len: %" MBEDTLS_PRINTF_SIZET, |
| 4276 | ssl->in_msglen)); |
| 4277 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4278 | } |
| 4279 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4280 | if (ssl->in_msg[0] != 1) { |
| 4281 | MBEDTLS_SSL_DEBUG_MSG(1, ("invalid CCS message, content: %02x", |
| 4282 | ssl->in_msg[0])); |
| 4283 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 4284 | } |
| 4285 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4286 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4287 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 4288 | ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC && |
| 4289 | ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC) { |
| 4290 | if (ssl->handshake == NULL) { |
| 4291 | MBEDTLS_SSL_DEBUG_MSG( |
| 4292 | 1, ("dropping ChangeCipherSpec outside handshake")); |
| 4293 | return MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 4294 | } |
| 4295 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4296 | MBEDTLS_SSL_DEBUG_MSG( |
| 4297 | 1, ("received out-of-order ChangeCipherSpec - remember")); |
| 4298 | return MBEDTLS_ERR_SSL_EARLY_MESSAGE; |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 4299 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4300 | # endif |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 4301 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4302 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4303 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT) { |
| 4304 | if (ssl->in_msglen != 2) { |
Angus Gratton | 1a7a17e | 2018-06-20 15:43:50 +1000 | [diff] [blame] | 4305 | /* Note: Standard allows for more than one 2 byte alert |
| 4306 | to be packed in a single message, but Mbed TLS doesn't |
| 4307 | currently support this. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4308 | MBEDTLS_SSL_DEBUG_MSG( |
| 4309 | 1, ("invalid alert message, len: %" MBEDTLS_PRINTF_SIZET, |
| 4310 | ssl->in_msglen)); |
| 4311 | return MBEDTLS_ERR_SSL_INVALID_RECORD; |
Angus Gratton | 1a7a17e | 2018-06-20 15:43:50 +1000 | [diff] [blame] | 4312 | } |
| 4313 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4314 | MBEDTLS_SSL_DEBUG_MSG(2, ("got an alert message, type: [%u:%u]", |
| 4315 | ssl->in_msg[0], ssl->in_msg[1])); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4316 | |
| 4317 | /* |
Simon Butcher | 459a950 | 2015-10-27 16:09:03 +0000 | [diff] [blame] | 4318 | * Ignore non-fatal alerts, except close_notify and no_renegotiation |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4319 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4320 | if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL) { |
| 4321 | MBEDTLS_SSL_DEBUG_MSG(1, ("is a fatal alert message (msg %d)", |
| 4322 | ssl->in_msg[1])); |
| 4323 | return MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4324 | } |
| 4325 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4326 | if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 4327 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY) { |
| 4328 | MBEDTLS_SSL_DEBUG_MSG(2, ("is a close notify message")); |
| 4329 | return MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4330 | } |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 4331 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4332 | # if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED) |
| 4333 | if (ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 4334 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION) { |
| 4335 | MBEDTLS_SSL_DEBUG_MSG(2, ("is a no renegotiation alert")); |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 4336 | /* Will be handled when trying to parse ServerHello */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4337 | return 0; |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 4338 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4339 | # endif |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 4340 | /* Silently ignore: fetch new message */ |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4341 | return MBEDTLS_ERR_SSL_NON_FATAL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4342 | } |
| 4343 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4344 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4345 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 4346 | /* Drop unexpected ApplicationData records, |
| 4347 | * except at the beginning of renegotiations */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4348 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA && |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 4349 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4350 | # if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 4351 | && !(ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 4352 | ssl->state == MBEDTLS_SSL_SERVER_HELLO) |
| 4353 | # endif |
| 4354 | ) { |
| 4355 | MBEDTLS_SSL_DEBUG_MSG(1, ("dropping unexpected ApplicationData")); |
| 4356 | return MBEDTLS_ERR_SSL_NON_FATAL; |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 4357 | } |
| 4358 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4359 | if (ssl->handshake != NULL && |
| 4360 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 4361 | mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl); |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 4362 | } |
| 4363 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4364 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 4365 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4366 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4367 | } |
| 4368 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4369 | int mbedtls_ssl_send_fatal_handshake_failure(mbedtls_ssl_context *ssl) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 4370 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4371 | return (mbedtls_ssl_send_alert_message( |
| 4372 | ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 4373 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE)); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 4374 | } |
| 4375 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4376 | int mbedtls_ssl_send_alert_message(mbedtls_ssl_context *ssl, |
| 4377 | unsigned char level, |
| 4378 | unsigned char message) |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 4379 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4380 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 4381 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4382 | if (ssl == NULL || ssl->conf == NULL) |
| 4383 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 4384 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4385 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> send alert message")); |
| 4386 | MBEDTLS_SSL_DEBUG_MSG(3, |
| 4387 | ("send alert level=%u message=%u", level, message)); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 4388 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4389 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 4390 | ssl->out_msglen = 2; |
| 4391 | ssl->out_msg[0] = level; |
| 4392 | ssl->out_msg[1] = message; |
| 4393 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4394 | if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) { |
| 4395 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret); |
| 4396 | return ret; |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 4397 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4398 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= send alert message")); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 4399 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4400 | return 0; |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 4401 | } |
| 4402 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4403 | int mbedtls_ssl_write_change_cipher_spec(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4404 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4405 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4406 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4407 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write change cipher spec")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4408 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4409 | ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4410 | ssl->out_msglen = 1; |
| 4411 | ssl->out_msg[0] = 1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4412 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4413 | ssl->state++; |
| 4414 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4415 | if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) { |
| 4416 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret); |
| 4417 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4418 | } |
| 4419 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4420 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write change cipher spec")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4421 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4422 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4423 | } |
| 4424 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4425 | int mbedtls_ssl_parse_change_cipher_spec(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4426 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4427 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4428 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4429 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse change cipher spec")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4430 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4431 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
| 4432 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
| 4433 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4434 | } |
| 4435 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4436 | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC) { |
| 4437 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad change cipher spec message")); |
| 4438 | mbedtls_ssl_send_alert_message( |
| 4439 | ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 4440 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE); |
| 4441 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4442 | } |
| 4443 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 4444 | /* CCS records are only accepted if they have length 1 and content '1', |
| 4445 | * so we don't need to check this here. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4446 | |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4447 | /* |
| 4448 | * Switch to our negotiated transform and session parameters for inbound |
| 4449 | * data. |
| 4450 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4451 | MBEDTLS_SSL_DEBUG_MSG(3, |
| 4452 | ("switching to new transform spec for inbound data")); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4453 | ssl->transform_in = ssl->transform_negotiate; |
| 4454 | ssl->session_in = ssl->session_negotiate; |
| 4455 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4456 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4457 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 4458 | # if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
| 4459 | mbedtls_ssl_dtls_replay_reset(ssl); |
| 4460 | # endif |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4461 | |
| 4462 | /* Increment epoch */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4463 | if (++ssl->in_epoch == 0) { |
| 4464 | MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap")); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 4465 | /* This is highly unlikely to happen for legitimate reasons, so |
| 4466 | treat it as an attack and don't send an alert. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4467 | return MBEDTLS_ERR_SSL_COUNTER_WRAPPING; |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4468 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4469 | } else |
| 4470 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4471 | memset(ssl->in_ctr, 0, 8); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4472 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4473 | mbedtls_ssl_update_in_pointers(ssl); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4474 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4475 | ssl->state++; |
| 4476 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4477 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse change cipher spec")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4478 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4479 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4480 | } |
| 4481 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4482 | /* Once ssl->out_hdr as the address of the beginning of the |
| 4483 | * next outgoing record is set, deduce the other pointers. |
| 4484 | * |
| 4485 | * Note: For TLS, we save the implicit record sequence number |
| 4486 | * (entering MAC computation) in the 8 bytes before ssl->out_hdr, |
| 4487 | * and the caller has to make sure there's space for this. |
| 4488 | */ |
| 4489 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4490 | static size_t |
| 4491 | ssl_transform_get_explicit_iv_len(mbedtls_ssl_transform const *transform) |
Hanno Becker | c0eefa8 | 2020-05-28 07:17:36 +0100 | [diff] [blame] | 4492 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4493 | if (transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3) |
| 4494 | return 0; |
Hanno Becker | c0eefa8 | 2020-05-28 07:17:36 +0100 | [diff] [blame] | 4495 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4496 | return transform->ivlen - transform->fixed_ivlen; |
Hanno Becker | c0eefa8 | 2020-05-28 07:17:36 +0100 | [diff] [blame] | 4497 | } |
| 4498 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4499 | void mbedtls_ssl_update_out_pointers(mbedtls_ssl_context *ssl, |
| 4500 | mbedtls_ssl_transform *transform) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4501 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4502 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4503 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 4504 | ssl->out_ctr = ssl->out_hdr + 3; |
| 4505 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4506 | ssl->out_cid = ssl->out_ctr + 8; |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4507 | ssl->out_len = ssl->out_cid; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4508 | if (transform != NULL) |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4509 | ssl->out_len += transform->out_cid_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4510 | # else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4511 | ssl->out_len = ssl->out_ctr + 8; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4512 | # endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4513 | ssl->out_iv = ssl->out_len + 2; |
| 4514 | } else |
| 4515 | # endif |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4516 | { |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4517 | ssl->out_len = ssl->out_hdr + 3; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4518 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 4c3eb7c | 2019-05-08 16:43:21 +0100 | [diff] [blame] | 4519 | ssl->out_cid = ssl->out_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4520 | # endif |
| 4521 | ssl->out_iv = ssl->out_hdr + 5; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4522 | } |
| 4523 | |
Hanno Becker | c0eefa8 | 2020-05-28 07:17:36 +0100 | [diff] [blame] | 4524 | ssl->out_msg = ssl->out_iv; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4525 | /* Adjust out_msg to make space for explicit IV, if used. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4526 | if (transform != NULL) |
| 4527 | ssl->out_msg += ssl_transform_get_explicit_iv_len(transform); |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4528 | } |
| 4529 | |
| 4530 | /* Once ssl->in_hdr as the address of the beginning of the |
| 4531 | * next incoming record is set, deduce the other pointers. |
| 4532 | * |
| 4533 | * Note: For TLS, we save the implicit record sequence number |
| 4534 | * (entering MAC computation) in the 8 bytes before ssl->in_hdr, |
| 4535 | * and the caller has to make sure there's space for this. |
| 4536 | */ |
| 4537 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4538 | void mbedtls_ssl_update_in_pointers(mbedtls_ssl_context *ssl) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4539 | { |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 4540 | /* This function sets the pointers to match the case |
| 4541 | * of unprotected TLS/DTLS records, with both ssl->in_iv |
| 4542 | * and ssl->in_msg pointing to the beginning of the record |
| 4543 | * content. |
| 4544 | * |
| 4545 | * When decrypting a protected record, ssl->in_msg |
| 4546 | * will be shifted to point to the beginning of the |
| 4547 | * record plaintext. |
| 4548 | */ |
| 4549 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4550 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4551 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4552 | /* This sets the header pointers to match records |
| 4553 | * without CID. When we receive a record containing |
| 4554 | * a CID, the fields are shifted accordingly in |
| 4555 | * ssl_parse_record_header(). */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4556 | ssl->in_ctr = ssl->in_hdr + 3; |
| 4557 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4558 | ssl->in_cid = ssl->in_ctr + 8; |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4559 | ssl->in_len = ssl->in_cid; /* Default: no CID */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4560 | # else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4561 | ssl->in_len = ssl->in_ctr + 8; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4562 | # endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4563 | ssl->in_iv = ssl->in_len + 2; |
| 4564 | } else |
| 4565 | # endif |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4566 | { |
| 4567 | ssl->in_ctr = ssl->in_hdr - 8; |
| 4568 | ssl->in_len = ssl->in_hdr + 3; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4569 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 4c3eb7c | 2019-05-08 16:43:21 +0100 | [diff] [blame] | 4570 | ssl->in_cid = ssl->in_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4571 | # endif |
| 4572 | ssl->in_iv = ssl->in_hdr + 5; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4573 | } |
| 4574 | |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 4575 | /* This will be adjusted at record decryption time. */ |
| 4576 | ssl->in_msg = ssl->in_iv; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4577 | } |
| 4578 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4579 | /* |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 4580 | * Setup an SSL context |
| 4581 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 4582 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4583 | void mbedtls_ssl_reset_in_out_pointers(mbedtls_ssl_context *ssl) |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 4584 | { |
| 4585 | /* Set the incoming and outgoing record pointers. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4586 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4587 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 4588 | ssl->out_hdr = ssl->out_buf; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4589 | ssl->in_hdr = ssl->in_buf; |
| 4590 | } else |
| 4591 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 4592 | { |
Hanno Becker | 12078f4 | 2021-03-02 15:28:41 +0000 | [diff] [blame] | 4593 | ssl->out_ctr = ssl->out_buf; |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 4594 | ssl->out_hdr = ssl->out_buf + 8; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4595 | ssl->in_hdr = ssl->in_buf + 8; |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 4596 | } |
| 4597 | |
| 4598 | /* Derive other internal pointers. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4599 | mbedtls_ssl_update_out_pointers(ssl, NULL /* no transform enabled */); |
| 4600 | mbedtls_ssl_update_in_pointers(ssl); |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 4601 | } |
| 4602 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4603 | /* |
| 4604 | * SSL get accessors |
| 4605 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4606 | size_t mbedtls_ssl_get_bytes_avail(const mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4607 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4608 | return ssl->in_offt == NULL ? 0 : ssl->in_msglen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4609 | } |
| 4610 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4611 | int mbedtls_ssl_check_pending(const mbedtls_ssl_context *ssl) |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 4612 | { |
| 4613 | /* |
| 4614 | * Case A: We're currently holding back |
| 4615 | * a message for further processing. |
| 4616 | */ |
| 4617 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4618 | if (ssl->keep_current_message == 1) { |
| 4619 | MBEDTLS_SSL_DEBUG_MSG( |
| 4620 | 3, ("ssl_check_pending: record held back for processing")); |
| 4621 | return 1; |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 4622 | } |
| 4623 | |
| 4624 | /* |
| 4625 | * Case B: Further records are pending in the current datagram. |
| 4626 | */ |
| 4627 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4628 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4629 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 4630 | ssl->in_left > ssl->next_record_offset) { |
| 4631 | MBEDTLS_SSL_DEBUG_MSG( |
| 4632 | 3, ("ssl_check_pending: more records within current datagram")); |
| 4633 | return 1; |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 4634 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4635 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 4636 | |
| 4637 | /* |
| 4638 | * Case C: A handshake message is being processed. |
| 4639 | */ |
| 4640 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4641 | if (ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen) { |
| 4642 | MBEDTLS_SSL_DEBUG_MSG( |
| 4643 | 3, |
| 4644 | ("ssl_check_pending: more handshake messages within current record")); |
| 4645 | return 1; |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 4646 | } |
| 4647 | |
| 4648 | /* |
| 4649 | * Case D: An application data message is being processed |
| 4650 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4651 | if (ssl->in_offt != NULL) { |
| 4652 | MBEDTLS_SSL_DEBUG_MSG( |
| 4653 | 3, |
| 4654 | ("ssl_check_pending: application data record is being processed")); |
| 4655 | return 1; |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 4656 | } |
| 4657 | |
| 4658 | /* |
| 4659 | * In all other cases, the rest of the message can be dropped. |
Hanno Becker | c573ac3 | 2018-08-28 17:15:25 +0100 | [diff] [blame] | 4660 | * As in ssl_get_next_record, this needs to be adapted if |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 4661 | * we implement support for multiple alerts in single records. |
| 4662 | */ |
| 4663 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4664 | MBEDTLS_SSL_DEBUG_MSG(3, ("ssl_check_pending: nothing pending")); |
| 4665 | return 0; |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 4666 | } |
| 4667 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4668 | 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] | 4669 | { |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 4670 | size_t transform_expansion = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4671 | const mbedtls_ssl_transform *transform = ssl->transform_out; |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 4672 | unsigned block_size; |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 4673 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4674 | size_t out_hdr_len = mbedtls_ssl_out_hdr_len(ssl); |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 4675 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4676 | if (transform == NULL) |
| 4677 | return (int)out_hdr_len; |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 4678 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4679 | switch (mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4680 | case MBEDTLS_MODE_GCM: |
| 4681 | case MBEDTLS_MODE_CCM: |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 4682 | case MBEDTLS_MODE_CHACHAPOLY: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4683 | case MBEDTLS_MODE_STREAM: |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 4684 | transform_expansion = transform->minlen; |
| 4685 | break; |
| 4686 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4687 | case MBEDTLS_MODE_CBC: |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 4688 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4689 | block_size = |
| 4690 | mbedtls_cipher_get_block_size(&transform->cipher_ctx_enc); |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 4691 | |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 4692 | /* Expansion due to the addition of the MAC. */ |
| 4693 | transform_expansion += transform->maclen; |
| 4694 | |
| 4695 | /* Expansion due to the addition of CBC padding; |
| 4696 | * Theoretically up to 256 bytes, but we never use |
| 4697 | * more than the block size of the underlying cipher. */ |
| 4698 | transform_expansion += block_size; |
| 4699 | |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 4700 | /* For TLS 1.2 or higher, an explicit IV is added |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 4701 | * after the record header. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4702 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 4703 | transform_expansion += block_size; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4704 | # endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 4705 | |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 4706 | break; |
| 4707 | |
| 4708 | default: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4709 | MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); |
| 4710 | return MBEDTLS_ERR_SSL_INTERNAL_ERROR; |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 4711 | } |
| 4712 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4713 | # if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4714 | if (transform->out_cid_len != 0) |
Hanno Becker | 6cbad55 | 2019-05-08 15:40:11 +0100 | [diff] [blame] | 4715 | transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4716 | # endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 6cbad55 | 2019-05-08 15:40:11 +0100 | [diff] [blame] | 4717 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4718 | return ((int)(out_hdr_len + transform_expansion)); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 4719 | } |
| 4720 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4721 | # if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 4722 | /* |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 4723 | * Check record counters and renegotiate if they're above the limit. |
| 4724 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4725 | static int ssl_check_ctr_renegotiate(mbedtls_ssl_context *ssl) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 4726 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4727 | size_t ep_len = mbedtls_ssl_ep_len(ssl); |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 4728 | int in_ctr_cmp; |
| 4729 | int out_ctr_cmp; |
| 4730 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4731 | if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4732 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING || |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4733 | ssl->conf->disable_renegotiation == |
| 4734 | MBEDTLS_SSL_RENEGOTIATION_DISABLED) { |
| 4735 | return 0; |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 4736 | } |
| 4737 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4738 | in_ctr_cmp = memcmp(ssl->in_ctr + ep_len, ssl->conf->renego_period + ep_len, |
| 4739 | 8 - ep_len); |
| 4740 | out_ctr_cmp = memcmp(ssl->cur_out_ctr + ep_len, |
| 4741 | ssl->conf->renego_period + ep_len, 8 - ep_len); |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 4742 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4743 | if (in_ctr_cmp <= 0 && out_ctr_cmp <= 0) { |
| 4744 | return 0; |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 4745 | } |
| 4746 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4747 | MBEDTLS_SSL_DEBUG_MSG(1, ("record counter limit reached: renegotiate")); |
| 4748 | return mbedtls_ssl_renegotiate(ssl); |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 4749 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4750 | # endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4751 | |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 4752 | /* This function is called from mbedtls_ssl_read() when a handshake message is |
Hanno Becker | f26cc72 | 2021-04-21 07:30:13 +0100 | [diff] [blame] | 4753 | * received after the initial handshake. In this context, handshake messages |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 4754 | * may only be sent for the purpose of initiating renegotiations. |
| 4755 | * |
| 4756 | * This function is introduced as a separate helper since the handling |
| 4757 | * of post-handshake handshake messages changes significantly in TLS 1.3, |
| 4758 | * and having a helper function allows to distinguish between TLS <= 1.2 and |
| 4759 | * TLS 1.3 in the future without bloating the logic of mbedtls_ssl_read(). |
| 4760 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4761 | static int ssl_handle_hs_message_post_handshake(mbedtls_ssl_context *ssl) |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 4762 | { |
Hanno Becker | fae12cf | 2021-04-21 07:20:20 +0100 | [diff] [blame] | 4763 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 4764 | |
| 4765 | /* |
| 4766 | * - For client-side, expect SERVER_HELLO_REQUEST. |
| 4767 | * - For server-side, expect CLIENT_HELLO. |
| 4768 | * - Fail (TLS) or silently drop record (DTLS) in other cases. |
| 4769 | */ |
| 4770 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4771 | # if defined(MBEDTLS_SSL_CLI_C) |
| 4772 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 4773 | (ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST || |
| 4774 | ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl))) { |
| 4775 | MBEDTLS_SSL_DEBUG_MSG(1, ("handshake received (not HelloRequest)")); |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 4776 | |
| 4777 | /* With DTLS, drop the packet (probably from last handshake) */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4778 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4779 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 4780 | return 0; |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 4781 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4782 | # endif |
| 4783 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 4784 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4785 | # endif /* MBEDTLS_SSL_CLI_C */ |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 4786 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4787 | # if defined(MBEDTLS_SSL_SRV_C) |
| 4788 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 4789 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO) { |
| 4790 | MBEDTLS_SSL_DEBUG_MSG(1, ("handshake received (not ClientHello)")); |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 4791 | |
| 4792 | /* With DTLS, drop the packet (probably from last handshake) */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4793 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4794 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 4795 | return 0; |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 4796 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4797 | # endif |
| 4798 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 4799 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4800 | # endif /* MBEDTLS_SSL_SRV_C */ |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 4801 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4802 | # if defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 4803 | /* Determine whether renegotiation attempt should be accepted */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4804 | if (!(ssl->conf->disable_renegotiation == |
| 4805 | MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
| 4806 | (ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
| 4807 | ssl->conf->allow_legacy_renegotiation == |
| 4808 | MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION))) { |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 4809 | /* |
| 4810 | * Accept renegotiation request |
| 4811 | */ |
| 4812 | |
| 4813 | /* DTLS clients need to know renego is server-initiated */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4814 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4815 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 4816 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 4817 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
| 4818 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4819 | # endif |
| 4820 | ret = mbedtls_ssl_start_renegotiation(ssl); |
| 4821 | if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && ret != 0) { |
| 4822 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret); |
| 4823 | return ret; |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 4824 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4825 | } else |
| 4826 | # endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 4827 | { |
| 4828 | /* |
| 4829 | * Refuse renegotiation |
| 4830 | */ |
| 4831 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4832 | MBEDTLS_SSL_DEBUG_MSG(3, ("refusing renegotiation, sending alert")); |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 4833 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4834 | # if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 4835 | if ((ret = mbedtls_ssl_send_alert_message( |
| 4836 | ssl, MBEDTLS_SSL_ALERT_LEVEL_WARNING, |
| 4837 | MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION)) != 0) { |
| 4838 | return ret; |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 4839 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4840 | # endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 4841 | } |
| 4842 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4843 | return 0; |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 4844 | } |
| 4845 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4846 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4847 | * Receive application data decrypted from the SSL layer |
| 4848 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4849 | 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] | 4850 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4851 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 4852 | size_t n; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4853 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4854 | if (ssl == NULL || ssl->conf == NULL) |
| 4855 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 4856 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4857 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> read")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4858 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4859 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4860 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 4861 | if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) |
| 4862 | return ret; |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 4863 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4864 | if (ssl->handshake != NULL && |
| 4865 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) { |
| 4866 | if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) |
| 4867 | return ret; |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 4868 | } |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 4869 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4870 | # endif |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 4871 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4872 | /* |
| 4873 | * Check if renegotiation is necessary and/or handshake is |
| 4874 | * in process. If yes, perform/continue, and fall through |
| 4875 | * if an unexpected packet is received while the client |
| 4876 | * is waiting for the ServerHello. |
| 4877 | * |
| 4878 | * (There is no equivalent to the last condition on |
| 4879 | * the server-side as it is not treated as within |
| 4880 | * a handshake while waiting for the ClientHello |
| 4881 | * after a renegotiation request.) |
| 4882 | */ |
| 4883 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4884 | # if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 4885 | ret = ssl_check_ctr_renegotiate(ssl); |
| 4886 | if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && ret != 0) { |
| 4887 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_check_ctr_renegotiate", ret); |
| 4888 | return ret; |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 4889 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4890 | # endif |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 4891 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4892 | if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 4893 | ret = mbedtls_ssl_handshake(ssl); |
| 4894 | if (ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && ret != 0) { |
| 4895 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret); |
| 4896 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4897 | } |
| 4898 | } |
| 4899 | |
Hanno Becker | e41158b | 2017-10-23 13:30:32 +0100 | [diff] [blame] | 4900 | /* Loop as long as no application data record is available */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4901 | while (ssl->in_offt == NULL) { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 4902 | /* Start timer if not already running */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4903 | if (ssl->f_get_timer != NULL && ssl->f_get_timer(ssl->p_timer) == -1) { |
| 4904 | mbedtls_ssl_set_timer(ssl, ssl->conf->read_timeout); |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 4905 | } |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 4906 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4907 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
| 4908 | if (ret == MBEDTLS_ERR_SSL_CONN_EOF) |
| 4909 | return 0; |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 4910 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4911 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
| 4912 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4913 | } |
| 4914 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4915 | if (ssl->in_msglen == 0 && |
| 4916 | ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4917 | /* |
| 4918 | * OpenSSL sends empty messages to randomize the IV |
| 4919 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4920 | if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) { |
| 4921 | if (ret == MBEDTLS_ERR_SSL_CONN_EOF) |
| 4922 | return 0; |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 4923 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4924 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); |
| 4925 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4926 | } |
| 4927 | } |
| 4928 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4929 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) { |
| 4930 | ret = ssl_handle_hs_message_post_handshake(ssl); |
| 4931 | if (ret != 0) { |
| 4932 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_handle_hs_message_post_handshake", |
| 4933 | ret); |
| 4934 | return ret; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4935 | } |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 4936 | |
Hanno Becker | f26cc72 | 2021-04-21 07:30:13 +0100 | [diff] [blame] | 4937 | /* At this point, we don't know whether the renegotiation triggered |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4938 | * by the post-handshake message has been completed or not. The |
| 4939 | * cases to consider are the following: 1) The renegotiation is |
| 4940 | * complete. In this case, no new record has been read yet. 2) The |
| 4941 | * renegotiation is incomplete because the client received an |
| 4942 | * application data record while awaiting the ServerHello. 3) The |
| 4943 | * renegotiation is incomplete because the client received a |
| 4944 | * non-handshake, non-application data message while awaiting the |
| 4945 | * ServerHello. |
Hanno Becker | f26cc72 | 2021-04-21 07:30:13 +0100 | [diff] [blame] | 4946 | * |
| 4947 | * In each of these cases, looping will be the proper action: |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 4948 | * - For 1), the next iteration will read a new record and check |
| 4949 | * if it's application data. |
| 4950 | * - For 2), the loop condition isn't satisfied as application data |
| 4951 | * is present, hence continue is the same as break |
| 4952 | * - For 3), the loop condition is satisfied and read_record |
| 4953 | * will re-deliver the message that was held back by the client |
| 4954 | * when expecting the ServerHello. |
| 4955 | */ |
Hanno Becker | f26cc72 | 2021-04-21 07:30:13 +0100 | [diff] [blame] | 4956 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 4957 | continue; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 4958 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4959 | # if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 4960 | else if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) { |
| 4961 | if (ssl->conf->renego_max_records >= 0) { |
| 4962 | if (++ssl->renego_records_seen > |
| 4963 | ssl->conf->renego_max_records) { |
| 4964 | MBEDTLS_SSL_DEBUG_MSG(1, ("renegotiation requested, " |
| 4965 | "but not honored by client")); |
| 4966 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 4967 | } |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 4968 | } |
Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 4969 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4970 | # endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 4971 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4972 | /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4973 | if (ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT) { |
| 4974 | MBEDTLS_SSL_DEBUG_MSG(2, ("ignoring non-fatal non-closure alert")); |
| 4975 | return MBEDTLS_ERR_SSL_WANT_READ; |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 4976 | } |
| 4977 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4978 | if (ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA) { |
| 4979 | MBEDTLS_SSL_DEBUG_MSG(1, ("bad application data message")); |
| 4980 | return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4981 | } |
| 4982 | |
| 4983 | ssl->in_offt = ssl->in_msg; |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 4984 | |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 4985 | /* We're going to return something now, cancel timer, |
| 4986 | * except if handshake (renegotiation) is in progress */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4987 | if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) |
| 4988 | mbedtls_ssl_set_timer(ssl, 0); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 4989 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 4990 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4991 | /* If we requested renego but received AppData, resend HelloRequest. |
| 4992 | * Do it now, after setting in_offt, to avoid taking this branch |
| 4993 | * again if ssl_write_hello_request() returns WANT_WRITE */ |
| 4994 | # if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
| 4995 | if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 4996 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) { |
| 4997 | if ((ret = mbedtls_ssl_resend_hello_request(ssl)) != 0) { |
| 4998 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_resend_hello_request", |
| 4999 | ret); |
| 5000 | return ret; |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 5001 | } |
| 5002 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5003 | # endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
| 5004 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5005 | } |
| 5006 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5007 | n = (len < ssl->in_msglen) ? len : ssl->in_msglen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5008 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5009 | memcpy(buf, ssl->in_offt, n); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5010 | ssl->in_msglen -= n; |
| 5011 | |
gabor-mezei-arm | a321413 | 2020-07-15 10:55:00 +0200 | [diff] [blame] | 5012 | /* Zeroising the plaintext buffer to erase unused application data |
| 5013 | from the memory. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5014 | mbedtls_platform_zeroize(ssl->in_offt, n); |
gabor-mezei-arm | a321413 | 2020-07-15 10:55:00 +0200 | [diff] [blame] | 5015 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5016 | if (ssl->in_msglen == 0) { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5017 | /* all bytes consumed */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5018 | ssl->in_offt = NULL; |
Hanno Becker | bdf3905 | 2017-06-09 10:42:03 +0100 | [diff] [blame] | 5019 | ssl->keep_current_message = 0; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5020 | } else { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5021 | /* more data available */ |
| 5022 | ssl->in_offt += n; |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5023 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5024 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5025 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= read")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5026 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5027 | return (int)n; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5028 | } |
| 5029 | |
| 5030 | /* |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 5031 | * Send application data to be encrypted by the SSL layer, taking care of max |
| 5032 | * fragment length and buffer size. |
| 5033 | * |
| 5034 | * According to RFC 5246 Section 6.2.1: |
| 5035 | * |
| 5036 | * Zero-length fragments of Application data MAY be sent as they are |
| 5037 | * potentially useful as a traffic analysis countermeasure. |
| 5038 | * |
| 5039 | * Therefore, it is possible that the input message length is 0 and the |
| 5040 | * corresponding return code is 0 on success. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5041 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5042 | static int |
| 5043 | ssl_write_real(mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5044 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5045 | int ret = mbedtls_ssl_get_max_out_record_payload(ssl); |
| 5046 | const size_t max_len = (size_t)ret; |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 5047 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5048 | if (ret < 0) { |
| 5049 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_max_out_record_payload", ret); |
| 5050 | return ret; |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 5051 | } |
| 5052 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5053 | if (len > max_len) { |
| 5054 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5055 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 5056 | MBEDTLS_SSL_DEBUG_MSG( |
| 5057 | 1, ("fragment larger than the (negotiated) " |
| 5058 | "maximum fragment length: %" MBEDTLS_PRINTF_SIZET |
| 5059 | " > %" MBEDTLS_PRINTF_SIZET, |
| 5060 | len, max_len)); |
| 5061 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 5062 | } else |
| 5063 | # endif |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 5064 | len = max_len; |
| 5065 | } |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 5066 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5067 | if (ssl->out_left != 0) { |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 5068 | /* |
| 5069 | * The user has previously tried to send the data and |
| 5070 | * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially |
| 5071 | * written. In this case, we expect the high-level write function |
| 5072 | * (e.g. mbedtls_ssl_write()) to be called with the same parameters |
| 5073 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5074 | if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) { |
| 5075 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flush_output", ret); |
| 5076 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5077 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5078 | } else { |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 5079 | /* |
| 5080 | * The user is trying to send a message the first time, so we need to |
| 5081 | * copy the data into the internal buffers and setup the data structure |
| 5082 | * to keep track of partial writes |
| 5083 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5084 | ssl->out_msglen = len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5085 | ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5086 | memcpy(ssl->out_msg, buf, len); |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 5087 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5088 | if ((ret = mbedtls_ssl_write_record(ssl, SSL_FORCE_FLUSH)) != 0) { |
| 5089 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_record", ret); |
| 5090 | return ret; |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 5091 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5092 | } |
| 5093 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5094 | return (int)len; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5095 | } |
| 5096 | |
| 5097 | /* |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5098 | * Write application data (public-facing wrapper) |
| 5099 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5100 | int mbedtls_ssl_write(mbedtls_ssl_context *ssl, |
| 5101 | const unsigned char *buf, |
| 5102 | size_t len) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5103 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5104 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5105 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5106 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write")); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5107 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5108 | if (ssl == NULL || ssl->conf == NULL) |
| 5109 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5110 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5111 | # if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5112 | if ((ret = ssl_check_ctr_renegotiate(ssl)) != 0) { |
| 5113 | MBEDTLS_SSL_DEBUG_RET(1, "ssl_check_ctr_renegotiate", ret); |
| 5114 | return ret; |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5115 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5116 | # endif |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5117 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5118 | if (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 5119 | if ((ret = mbedtls_ssl_handshake(ssl)) != 0) { |
| 5120 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret); |
| 5121 | return ret; |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5122 | } |
| 5123 | } |
| 5124 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5125 | ret = ssl_write_real(ssl, buf, len); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5126 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5127 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write")); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5128 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5129 | return ret; |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5130 | } |
| 5131 | |
| 5132 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5133 | * Notify the peer that the connection is being closed |
| 5134 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5135 | int mbedtls_ssl_close_notify(mbedtls_ssl_context *ssl) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5136 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5137 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5138 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5139 | if (ssl == NULL || ssl->conf == NULL) |
| 5140 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5141 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5142 | MBEDTLS_SSL_DEBUG_MSG(2, ("=> write close notify")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5143 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5144 | if (ssl->out_left != 0) |
| 5145 | return mbedtls_ssl_flush_output(ssl); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5146 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5147 | if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 5148 | if ((ret = mbedtls_ssl_send_alert_message( |
| 5149 | ssl, MBEDTLS_SSL_ALERT_LEVEL_WARNING, |
| 5150 | MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY)) != 0) { |
| 5151 | MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_send_alert_message", ret); |
| 5152 | return ret; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5153 | } |
| 5154 | } |
| 5155 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5156 | MBEDTLS_SSL_DEBUG_MSG(2, ("<= write close notify")); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5157 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5158 | return 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5159 | } |
| 5160 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5161 | void mbedtls_ssl_transform_free(mbedtls_ssl_transform *transform) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5162 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5163 | if (transform == NULL) |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 5164 | return; |
| 5165 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5166 | mbedtls_cipher_free(&transform->cipher_ctx_enc); |
| 5167 | mbedtls_cipher_free(&transform->cipher_ctx_dec); |
Manuel Pégourié-Gonnard | f71e587 | 2013-09-23 17:12:43 +0200 | [diff] [blame] | 5168 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5169 | # if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 5170 | mbedtls_md_free(&transform->md_ctx_enc); |
| 5171 | mbedtls_md_free(&transform->md_ctx_dec); |
| 5172 | # endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 5173 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5174 | mbedtls_platform_zeroize(transform, sizeof(mbedtls_ssl_transform)); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5175 | } |
| 5176 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5177 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 5178 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5179 | void mbedtls_ssl_buffering_free(mbedtls_ssl_context *ssl) |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 5180 | { |
| 5181 | unsigned offset; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5182 | mbedtls_ssl_handshake_params *const hs = ssl->handshake; |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 5183 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5184 | if (hs == NULL) |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 5185 | return; |
| 5186 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5187 | ssl_free_buffered_record(ssl); |
Hanno Becker | 283f5ef | 2018-08-24 09:34:47 +0100 | [diff] [blame] | 5188 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5189 | for (offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++) |
| 5190 | ssl_buffering_free_slot(ssl, offset); |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 5191 | } |
| 5192 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5193 | static void ssl_buffering_free_slot(mbedtls_ssl_context *ssl, uint8_t slot) |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 5194 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5195 | mbedtls_ssl_handshake_params *const hs = ssl->handshake; |
| 5196 | mbedtls_ssl_hs_buffer *const hs_buf = &hs->buffering.hs[slot]; |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 5197 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5198 | if (slot >= MBEDTLS_SSL_MAX_BUFFERED_HS) |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 5199 | return; |
| 5200 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5201 | if (hs_buf->is_valid == 1) { |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 5202 | hs->buffering.total_bytes_buffered -= hs_buf->data_len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5203 | mbedtls_platform_zeroize(hs_buf->data, hs_buf->data_len); |
| 5204 | mbedtls_free(hs_buf->data); |
| 5205 | memset(hs_buf, 0, sizeof(mbedtls_ssl_hs_buffer)); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 5206 | } |
| 5207 | } |
| 5208 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5209 | # endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 5210 | |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5211 | /* |
| 5212 | * Convert version numbers to/from wire format |
| 5213 | * and, for DTLS, to/from TLS equivalent. |
| 5214 | * |
| 5215 | * For TLS this is the identity. |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 5216 | * 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] | 5217 | * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2) |
| 5218 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5219 | void mbedtls_ssl_write_version(int major, |
| 5220 | int minor, |
| 5221 | int transport, |
| 5222 | unsigned char ver[2]) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5223 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5224 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5225 | if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 5226 | if (minor == MBEDTLS_SSL_MINOR_VERSION_2) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5227 | --minor; /* DTLS 1.0 stored as TLS 1.1 internally */ |
| 5228 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5229 | ver[0] = (unsigned char)(255 - (major - 2)); |
| 5230 | ver[1] = (unsigned char)(255 - (minor - 1)); |
| 5231 | } else |
| 5232 | # else |
| 5233 | ((void)transport); |
| 5234 | # endif |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 5235 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5236 | ver[0] = (unsigned char)major; |
| 5237 | ver[1] = (unsigned char)minor; |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 5238 | } |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5239 | } |
| 5240 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5241 | void mbedtls_ssl_read_version(int *major, |
| 5242 | int *minor, |
| 5243 | int transport, |
| 5244 | const unsigned char ver[2]) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5245 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5246 | # if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5247 | if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5248 | *major = 255 - ver[0] + 2; |
| 5249 | *minor = 255 - ver[1] + 1; |
| 5250 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5251 | if (*minor == MBEDTLS_SSL_MINOR_VERSION_1) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5252 | ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 5253 | } else |
| 5254 | # else |
| 5255 | ((void)transport); |
| 5256 | # endif |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 5257 | { |
| 5258 | *major = ver[0]; |
| 5259 | *minor = ver[1]; |
| 5260 | } |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5261 | } |
| 5262 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5263 | #endif /* MBEDTLS_SSL_TLS_C */ |