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 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [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 |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 35 | #endif |
| 36 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 37 | #include "mbedtls/ssl.h" |
Chris Jones | 84a773f | 2021-03-05 18:38:47 +0000 | [diff] [blame] | 38 | #include "ssl_misc.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 39 | #include "mbedtls/debug.h" |
| 40 | #include "mbedtls/error.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 41 | #include "mbedtls/platform_util.h" |
Hanno Becker | a835da5 | 2019-05-16 12:39:07 +0100 | [diff] [blame] | 42 | #include "mbedtls/version.h" |
gabor-mezei-arm | db9a38c | 2021-09-27 11:28:54 +0200 | [diff] [blame] | 43 | #include "constant_time.h" |
Paul Bakker | 0be444a | 2013-08-27 21:55:01 +0200 | [diff] [blame] | 44 | |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 45 | #include "ssl_invasive.h" |
| 46 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 47 | #include <string.h> |
| 48 | |
Andrzej Kurek | d6db9be | 2019-01-10 05:27:10 -0500 | [diff] [blame] | 49 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 50 | #include "mbedtls/psa_util.h" |
| 51 | #include "psa/crypto.h" |
| 52 | #endif |
| 53 | |
Janos Follath | 23bdca0 | 2016-10-07 14:47:14 +0100 | [diff] [blame] | 54 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 55 | #include "mbedtls/oid.h" |
Manuel Pégourié-Gonnard | 0408fd1 | 2014-04-11 11:06:22 +0200 | [diff] [blame] | 56 | #endif |
| 57 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 58 | 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] | 59 | |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 60 | /* |
| 61 | * Start a timer. |
| 62 | * Passing millisecs = 0 cancels a running timer. |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 63 | */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 64 | 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] | 65 | { |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 66 | if( ssl->f_set_timer == NULL ) |
| 67 | return; |
| 68 | |
| 69 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) ); |
| 70 | ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs ); |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /* |
| 74 | * Return -1 is timer is expired, 0 if it isn't. |
| 75 | */ |
Hanno Becker | 7876d12 | 2020-02-05 10:39:31 +0000 | [diff] [blame] | 76 | int mbedtls_ssl_check_timer( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 77 | { |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 78 | if( ssl->f_get_timer == NULL ) |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 79 | return( 0 ); |
Manuel Pégourié-Gonnard | 2e01291 | 2015-05-12 20:55:41 +0200 | [diff] [blame] | 80 | |
| 81 | if( ssl->f_get_timer( ssl->p_timer ) == 2 ) |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 82 | { |
| 83 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) ); |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 84 | return( -1 ); |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 85 | } |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 86 | |
| 87 | return( 0 ); |
| 88 | } |
Manuel Pégourié-Gonnard | db2858c | 2014-09-29 14:04:42 +0200 | [diff] [blame] | 89 | |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 90 | static int ssl_parse_record_header( mbedtls_ssl_context const *ssl, |
| 91 | unsigned char *buf, |
| 92 | size_t len, |
| 93 | mbedtls_record *rec ); |
| 94 | |
| 95 | int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl, |
| 96 | unsigned char *buf, |
| 97 | size_t buflen ) |
| 98 | { |
| 99 | int ret = 0; |
| 100 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) ); |
| 101 | MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen ); |
| 102 | |
| 103 | /* We don't support record checking in TLS because |
TRodziewicz | 2abf03c | 2021-06-25 14:40:09 +0200 | [diff] [blame] | 104 | * there doesn't seem to be a usecase for it. |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 105 | */ |
| 106 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM ) |
| 107 | { |
| 108 | ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
| 109 | goto exit; |
| 110 | } |
| 111 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 112 | else |
| 113 | { |
| 114 | mbedtls_record rec; |
| 115 | |
| 116 | ret = ssl_parse_record_header( ssl, buf, buflen, &rec ); |
| 117 | if( ret != 0 ) |
| 118 | { |
| 119 | MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret ); |
| 120 | goto exit; |
| 121 | } |
| 122 | |
| 123 | if( ssl->transform_in != NULL ) |
| 124 | { |
| 125 | ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec ); |
| 126 | if( ret != 0 ) |
| 127 | { |
| 128 | MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret ); |
| 129 | goto exit; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 134 | |
| 135 | exit: |
| 136 | /* On success, we have decrypted the buffer in-place, so make |
| 137 | * sure we don't leak any plaintext data. */ |
| 138 | mbedtls_platform_zeroize( buf, buflen ); |
| 139 | |
| 140 | /* For the purpose of this API, treat messages with unexpected CID |
| 141 | * as well as such from future epochs as unexpected. */ |
| 142 | if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID || |
| 143 | ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) |
| 144 | { |
| 145 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
| 146 | } |
| 147 | |
| 148 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) ); |
| 149 | return( ret ); |
| 150 | } |
| 151 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 152 | #define SSL_DONT_FORCE_FLUSH 0 |
| 153 | #define SSL_FORCE_FLUSH 1 |
| 154 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 155 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 156 | |
Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 157 | /* Forward declarations for functions related to message buffering. */ |
Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 158 | static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl, |
| 159 | uint8_t slot ); |
| 160 | static void ssl_free_buffered_record( mbedtls_ssl_context *ssl ); |
| 161 | static int ssl_load_buffered_message( mbedtls_ssl_context *ssl ); |
| 162 | static int ssl_load_buffered_record( mbedtls_ssl_context *ssl ); |
| 163 | static int ssl_buffer_message( mbedtls_ssl_context *ssl ); |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 164 | static int ssl_buffer_future_record( mbedtls_ssl_context *ssl, |
| 165 | mbedtls_record const *rec ); |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 166 | static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl ); |
Hanno Becker | d584777 | 2018-08-28 10:09:23 +0100 | [diff] [blame] | 167 | |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 168 | 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] | 169 | { |
Hanno Becker | 8949071 | 2020-02-05 10:50:12 +0000 | [diff] [blame] | 170 | size_t mtu = mbedtls_ssl_get_current_mtu( ssl ); |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 171 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 172 | size_t out_buf_len = ssl->out_buf_len; |
| 173 | #else |
| 174 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 175 | #endif |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 176 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 177 | if( mtu != 0 && mtu < out_buf_len ) |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 178 | return( mtu ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 179 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 180 | return( out_buf_len ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 181 | } |
| 182 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 183 | static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl ) |
| 184 | { |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 185 | size_t const bytes_written = ssl->out_left; |
| 186 | size_t const mtu = ssl_get_maximum_datagram_size( ssl ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 187 | |
| 188 | /* Double-check that the write-index hasn't gone |
| 189 | * past what we can transmit in a single datagram. */ |
Hanno Becker | 11682cc | 2018-08-22 14:41:02 +0100 | [diff] [blame] | 190 | if( bytes_written > mtu ) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 191 | { |
| 192 | /* Should never happen... */ |
| 193 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 194 | } |
| 195 | |
| 196 | return( (int) ( mtu - bytes_written ) ); |
| 197 | } |
| 198 | |
| 199 | static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl ) |
| 200 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 201 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 202 | size_t remaining, expansion; |
Andrzej Kurek | 748face | 2018-10-11 07:20:19 -0400 | [diff] [blame] | 203 | size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 204 | |
| 205 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Andrzej Kurek | 90c6e84 | 2020-04-03 05:25:29 -0400 | [diff] [blame] | 206 | const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 207 | |
| 208 | if( max_len > mfl ) |
| 209 | max_len = mfl; |
Hanno Becker | f4b010e | 2018-08-24 10:47:29 +0100 | [diff] [blame] | 210 | |
| 211 | /* By the standard (RFC 6066 Sect. 4), the MFL extension |
| 212 | * only limits the maximum record payload size, so in theory |
| 213 | * we would be allowed to pack multiple records of payload size |
| 214 | * MFL into a single datagram. However, this would mean that there's |
| 215 | * no way to explicitly communicate MTU restrictions to the peer. |
| 216 | * |
| 217 | * The following reduction of max_len makes sure that we never |
| 218 | * write datagrams larger than MFL + Record Expansion Overhead. |
| 219 | */ |
| 220 | if( max_len <= ssl->out_left ) |
| 221 | return( 0 ); |
| 222 | |
| 223 | max_len -= ssl->out_left; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 224 | #endif |
| 225 | |
| 226 | ret = ssl_get_remaining_space_in_datagram( ssl ); |
| 227 | if( ret < 0 ) |
| 228 | return( ret ); |
| 229 | remaining = (size_t) ret; |
| 230 | |
| 231 | ret = mbedtls_ssl_get_record_expansion( ssl ); |
| 232 | if( ret < 0 ) |
| 233 | return( ret ); |
| 234 | expansion = (size_t) ret; |
| 235 | |
| 236 | if( remaining <= expansion ) |
| 237 | return( 0 ); |
| 238 | |
| 239 | remaining -= expansion; |
| 240 | if( remaining >= max_len ) |
| 241 | remaining = max_len; |
| 242 | |
| 243 | return( (int) remaining ); |
| 244 | } |
| 245 | |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 246 | /* |
| 247 | * Double the retransmit timeout value, within the allowed range, |
| 248 | * returning -1 if the maximum value has already been reached. |
| 249 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 250 | static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 251 | { |
| 252 | uint32_t new_timeout; |
| 253 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 254 | if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 255 | return( -1 ); |
| 256 | |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 257 | /* Implement the final paragraph of RFC 6347 section 4.1.1.1 |
| 258 | * in the following way: after the initial transmission and a first |
| 259 | * retransmission, back off to a temporary estimated MTU of 508 bytes. |
| 260 | * This value is guaranteed to be deliverable (if not guaranteed to be |
| 261 | * delivered) of any compliant IPv4 (and IPv6) network, and should work |
| 262 | * on most non-IP stacks too. */ |
| 263 | if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min ) |
Andrzej Kurek | 6290dae | 2018-10-05 08:06:01 -0400 | [diff] [blame] | 264 | { |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 265 | ssl->handshake->mtu = 508; |
Andrzej Kurek | 6290dae | 2018-10-05 08:06:01 -0400 | [diff] [blame] | 266 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) ); |
| 267 | } |
Manuel Pégourié-Gonnard | b8eec19 | 2018-08-20 09:34:02 +0200 | [diff] [blame] | 268 | |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 269 | new_timeout = 2 * ssl->handshake->retransmit_timeout; |
| 270 | |
| 271 | /* Avoid arithmetic overflow and range overflow */ |
| 272 | if( new_timeout < ssl->handshake->retransmit_timeout || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 273 | new_timeout > ssl->conf->hs_timeout_max ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 274 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 275 | new_timeout = ssl->conf->hs_timeout_max; |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | ssl->handshake->retransmit_timeout = new_timeout; |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 279 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "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 | |
| 282 | return( 0 ); |
| 283 | } |
| 284 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 285 | static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 286 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 287 | ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min; |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 288 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %lu millisecs", |
| 289 | (unsigned long) ssl->handshake->retransmit_timeout ) ); |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 290 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 291 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 292 | |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 293 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 294 | * Encryption/decryption functions |
Paul Bakker | f7abd42 | 2013-04-16 13:15:56 +0200 | [diff] [blame] | 295 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 296 | |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 297 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) || \ |
| 298 | defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
Hanno Becker | 1399692 | 2020-05-28 16:15:19 +0100 | [diff] [blame] | 299 | |
| 300 | static size_t ssl_compute_padding_length( size_t len, |
| 301 | size_t granularity ) |
| 302 | { |
| 303 | return( ( granularity - ( len + 1 ) % granularity ) % granularity ); |
| 304 | } |
| 305 | |
Hanno Becker | 581bc1b | 2020-05-04 12:20:03 +0100 | [diff] [blame] | 306 | /* This functions transforms a (D)TLS plaintext fragment and a record content |
| 307 | * type into an instance of the (D)TLSInnerPlaintext structure. This is used |
| 308 | * in DTLS 1.2 + CID and within TLS 1.3 to allow flexible padding and to protect |
| 309 | * a record's content type. |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 310 | * |
| 311 | * struct { |
| 312 | * opaque content[DTLSPlaintext.length]; |
| 313 | * ContentType real_type; |
| 314 | * uint8 zeros[length_of_padding]; |
Hanno Becker | 581bc1b | 2020-05-04 12:20:03 +0100 | [diff] [blame] | 315 | * } (D)TLSInnerPlaintext; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 316 | * |
| 317 | * Input: |
| 318 | * - `content`: The beginning of the buffer holding the |
| 319 | * plaintext to be wrapped. |
| 320 | * - `*content_size`: The length of the plaintext in Bytes. |
| 321 | * - `max_len`: The number of Bytes available starting from |
| 322 | * `content`. This must be `>= *content_size`. |
| 323 | * - `rec_type`: The desired record content type. |
| 324 | * |
| 325 | * Output: |
Hanno Becker | 581bc1b | 2020-05-04 12:20:03 +0100 | [diff] [blame] | 326 | * - `content`: The beginning of the resulting (D)TLSInnerPlaintext structure. |
| 327 | * - `*content_size`: The length of the resulting (D)TLSInnerPlaintext structure. |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 328 | * |
| 329 | * Returns: |
| 330 | * - `0` on success. |
| 331 | * - A negative error code if `max_len` didn't offer enough space |
| 332 | * for the expansion. |
| 333 | */ |
Hanno Becker | 581bc1b | 2020-05-04 12:20:03 +0100 | [diff] [blame] | 334 | static int ssl_build_inner_plaintext( unsigned char *content, |
| 335 | size_t *content_size, |
| 336 | size_t remaining, |
Hanno Becker | 1399692 | 2020-05-28 16:15:19 +0100 | [diff] [blame] | 337 | uint8_t rec_type, |
| 338 | size_t pad ) |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 339 | { |
| 340 | size_t len = *content_size; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 341 | |
| 342 | /* Write real content type */ |
| 343 | if( remaining == 0 ) |
| 344 | return( -1 ); |
| 345 | content[ len ] = rec_type; |
| 346 | len++; |
| 347 | remaining--; |
| 348 | |
| 349 | if( remaining < pad ) |
| 350 | return( -1 ); |
| 351 | memset( content + len, 0, pad ); |
| 352 | len += pad; |
| 353 | remaining -= pad; |
| 354 | |
| 355 | *content_size = len; |
| 356 | return( 0 ); |
| 357 | } |
| 358 | |
Hanno Becker | 581bc1b | 2020-05-04 12:20:03 +0100 | [diff] [blame] | 359 | /* This function parses a (D)TLSInnerPlaintext structure. |
| 360 | * See ssl_build_inner_plaintext() for details. */ |
| 361 | static int ssl_parse_inner_plaintext( unsigned char const *content, |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 362 | size_t *content_size, |
| 363 | uint8_t *rec_type ) |
| 364 | { |
| 365 | size_t remaining = *content_size; |
| 366 | |
| 367 | /* Determine length of padding by skipping zeroes from the back. */ |
| 368 | do |
| 369 | { |
| 370 | if( remaining == 0 ) |
| 371 | return( -1 ); |
| 372 | remaining--; |
| 373 | } while( content[ remaining ] == 0 ); |
| 374 | |
| 375 | *content_size = remaining; |
| 376 | *rec_type = content[ remaining ]; |
| 377 | |
| 378 | return( 0 ); |
| 379 | } |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 380 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID || |
| 381 | MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 382 | |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 383 | /* `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] | 384 | * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 385 | static void ssl_extract_add_data_from_record( unsigned char* add_data, |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 386 | size_t *add_data_len, |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 387 | mbedtls_record *rec, |
| 388 | unsigned minor_ver ) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 389 | { |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 390 | /* Quoting RFC 5246 (TLS 1.2): |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 391 | * |
| 392 | * additional_data = seq_num + TLSCompressed.type + |
| 393 | * TLSCompressed.version + TLSCompressed.length; |
| 394 | * |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 395 | * For the CID extension, this is extended as follows |
| 396 | * (quoting draft-ietf-tls-dtls-connection-id-05, |
| 397 | * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05): |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 398 | * |
| 399 | * additional_data = seq_num + DTLSPlaintext.type + |
| 400 | * DTLSPlaintext.version + |
Hanno Becker | d5aeab1 | 2019-05-20 14:50:53 +0100 | [diff] [blame] | 401 | * cid + |
| 402 | * cid_length + |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 403 | * length_of_DTLSInnerPlaintext; |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 404 | * |
| 405 | * For TLS 1.3, the record sequence number is dropped from the AAD |
| 406 | * and encoded within the nonce of the AEAD operation instead. |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 407 | */ |
| 408 | |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 409 | unsigned char *cur = add_data; |
| 410 | |
| 411 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
| 412 | if( minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 ) |
| 413 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
| 414 | { |
| 415 | ((void) minor_ver); |
| 416 | memcpy( cur, rec->ctr, sizeof( rec->ctr ) ); |
| 417 | cur += sizeof( rec->ctr ); |
| 418 | } |
| 419 | |
| 420 | *cur = rec->type; |
| 421 | cur++; |
| 422 | |
| 423 | memcpy( cur, rec->ver, sizeof( rec->ver ) ); |
| 424 | cur += sizeof( rec->ver ); |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 425 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 426 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 95e4bbc | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 427 | if( rec->cid_len != 0 ) |
| 428 | { |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 429 | memcpy( cur, rec->cid, rec->cid_len ); |
| 430 | cur += rec->cid_len; |
| 431 | |
| 432 | *cur = rec->cid_len; |
| 433 | cur++; |
| 434 | |
| 435 | cur[0] = ( rec->data_len >> 8 ) & 0xFF; |
| 436 | cur[1] = ( rec->data_len >> 0 ) & 0xFF; |
| 437 | cur += 2; |
Hanno Becker | 95e4bbc | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 438 | } |
| 439 | else |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 440 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 95e4bbc | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 441 | { |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 442 | cur[0] = ( rec->data_len >> 8 ) & 0xFF; |
| 443 | cur[1] = ( rec->data_len >> 0 ) & 0xFF; |
| 444 | cur += 2; |
Hanno Becker | 95e4bbc | 2019-05-09 11:38:24 +0100 | [diff] [blame] | 445 | } |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 446 | |
| 447 | *add_data_len = cur - add_data; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Hanno Becker | 67a37db | 2020-05-28 16:27:07 +0100 | [diff] [blame] | 450 | #if defined(MBEDTLS_GCM_C) || \ |
| 451 | defined(MBEDTLS_CCM_C) || \ |
| 452 | defined(MBEDTLS_CHACHAPOLY_C) |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 453 | static int ssl_transform_aead_dynamic_iv_is_explicit( |
| 454 | mbedtls_ssl_transform const *transform ) |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 455 | { |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 456 | return( transform->ivlen != transform->fixed_ivlen ); |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 457 | } |
| 458 | |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 459 | /* Compute IV := ( fixed_iv || 0 ) XOR ( 0 || dynamic_IV ) |
| 460 | * |
| 461 | * Concretely, this occurs in two variants: |
| 462 | * |
| 463 | * a) Fixed and dynamic IV lengths add up to total IV length, giving |
| 464 | * IV = fixed_iv || dynamic_iv |
| 465 | * |
Hanno Becker | 1595281 | 2020-06-04 13:31:46 +0100 | [diff] [blame] | 466 | * This variant is used in TLS 1.2 when used with GCM or CCM. |
| 467 | * |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 468 | * b) Fixed IV lengths matches total IV length, giving |
| 469 | * IV = fixed_iv XOR ( 0 || dynamic_iv ) |
Hanno Becker | 1595281 | 2020-06-04 13:31:46 +0100 | [diff] [blame] | 470 | * |
| 471 | * This variant occurs in TLS 1.3 and for TLS 1.2 when using ChaChaPoly. |
| 472 | * |
| 473 | * See also the documentation of mbedtls_ssl_transform. |
Hanno Becker | f486e28 | 2020-06-04 13:33:08 +0100 | [diff] [blame] | 474 | * |
| 475 | * This function has the precondition that |
| 476 | * |
| 477 | * dst_iv_len >= max( fixed_iv_len, dynamic_iv_len ) |
| 478 | * |
| 479 | * which has to be ensured by the caller. If this precondition |
| 480 | * violated, the behavior of this function is undefined. |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 481 | */ |
| 482 | static void ssl_build_record_nonce( unsigned char *dst_iv, |
| 483 | size_t dst_iv_len, |
| 484 | unsigned char const *fixed_iv, |
| 485 | size_t fixed_iv_len, |
| 486 | unsigned char const *dynamic_iv, |
| 487 | size_t dynamic_iv_len ) |
| 488 | { |
| 489 | size_t i; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 490 | |
| 491 | /* Start with Fixed IV || 0 */ |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 492 | memset( dst_iv, 0, dst_iv_len ); |
| 493 | memcpy( dst_iv, fixed_iv, fixed_iv_len ); |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 494 | |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 495 | dst_iv += dst_iv_len - dynamic_iv_len; |
| 496 | for( i = 0; i < dynamic_iv_len; i++ ) |
| 497 | dst_iv[i] ^= dynamic_iv[i]; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 498 | } |
Hanno Becker | 67a37db | 2020-05-28 16:27:07 +0100 | [diff] [blame] | 499 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 500 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 501 | int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl, |
| 502 | mbedtls_ssl_transform *transform, |
| 503 | mbedtls_record *rec, |
| 504 | int (*f_rng)(void *, unsigned char *, size_t), |
| 505 | void *p_rng ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 506 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 507 | mbedtls_cipher_mode_t mode; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 508 | int auth_done = 0; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 509 | unsigned char * data; |
Hanno Becker | 92fb4fa | 2019-05-20 14:54:26 +0100 | [diff] [blame] | 510 | unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ]; |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 511 | size_t add_data_len; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 512 | size_t post_avail; |
| 513 | |
| 514 | /* The SSL context is only used for debugging purposes! */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 515 | #if !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | a7505d1 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 516 | ssl = NULL; /* make sure we don't use it except for debug */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 517 | ((void) ssl); |
| 518 | #endif |
| 519 | |
| 520 | /* The PRNG is used for dynamic IV generation that's used |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 521 | * for CBC transformations in TLS 1.2. */ |
Manuel Pégourié-Gonnard | 2df1f1f | 2020-07-09 12:11:39 +0200 | [diff] [blame] | 522 | #if !( defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) && \ |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 523 | defined(MBEDTLS_SSL_PROTO_TLS1_2) ) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 524 | ((void) f_rng); |
| 525 | ((void) p_rng); |
| 526 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 527 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 528 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 529 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 530 | if( transform == NULL ) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 531 | { |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 532 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) ); |
| 533 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 534 | } |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 535 | if( rec == NULL |
| 536 | || rec->buf == NULL |
| 537 | || rec->buf_len < rec->data_offset |
| 538 | || rec->buf_len - rec->data_offset < rec->data_len |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 539 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 540 | || rec->cid_len != 0 |
| 541 | #endif |
| 542 | ) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 543 | { |
| 544 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 545 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 546 | } |
| 547 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 548 | data = rec->buf + rec->data_offset; |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 549 | post_avail = rec->buf_len - ( rec->data_len + rec->data_offset ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 550 | MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload", |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 551 | data, rec->data_len ); |
| 552 | |
| 553 | mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ); |
| 554 | |
| 555 | if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
| 556 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 557 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %" MBEDTLS_PRINTF_SIZET |
| 558 | " too large, maximum %" MBEDTLS_PRINTF_SIZET, |
Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 559 | rec->data_len, |
| 560 | (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 561 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 562 | } |
Manuel Pégourié-Gonnard | 60346be | 2014-11-21 11:38:37 +0100 | [diff] [blame] | 563 | |
Hanno Becker | 9231340 | 2020-05-20 13:58:58 +0100 | [diff] [blame] | 564 | /* The following two code paths implement the (D)TLSInnerPlaintext |
| 565 | * structure present in TLS 1.3 and DTLS 1.2 + CID. |
| 566 | * |
| 567 | * See ssl_build_inner_plaintext() for more information. |
| 568 | * |
| 569 | * Note that this changes `rec->data_len`, and hence |
| 570 | * `post_avail` needs to be recalculated afterwards. |
| 571 | * |
| 572 | * Note also that the two code paths cannot occur simultaneously |
| 573 | * since they apply to different versions of the protocol. There |
| 574 | * is hence no risk of double-addition of the inner plaintext. |
| 575 | */ |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 576 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
| 577 | if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 ) |
| 578 | { |
Hanno Becker | 1399692 | 2020-05-28 16:15:19 +0100 | [diff] [blame] | 579 | size_t padding = |
| 580 | ssl_compute_padding_length( rec->data_len, |
TRodziewicz | e8dd709 | 2021-05-12 14:19:11 +0200 | [diff] [blame] | 581 | MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY ); |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 582 | if( ssl_build_inner_plaintext( data, |
Hanno Becker | 1399692 | 2020-05-28 16:15:19 +0100 | [diff] [blame] | 583 | &rec->data_len, |
| 584 | post_avail, |
| 585 | rec->type, |
| 586 | padding ) != 0 ) |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 587 | { |
| 588 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 589 | } |
| 590 | |
| 591 | rec->type = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
| 592 | } |
| 593 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
| 594 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 595 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 596 | /* |
| 597 | * Add CID information |
| 598 | */ |
| 599 | rec->cid_len = transform->out_cid_len; |
| 600 | memcpy( rec->cid, transform->out_cid, transform->out_cid_len ); |
| 601 | MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len ); |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 602 | |
| 603 | if( rec->cid_len != 0 ) |
| 604 | { |
Hanno Becker | 1399692 | 2020-05-28 16:15:19 +0100 | [diff] [blame] | 605 | size_t padding = |
| 606 | ssl_compute_padding_length( rec->data_len, |
TRodziewicz | e8dd709 | 2021-05-12 14:19:11 +0200 | [diff] [blame] | 607 | MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY ); |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 608 | /* |
Hanno Becker | 07dc97d | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 609 | * Wrap plaintext into DTLSInnerPlaintext structure. |
Hanno Becker | 581bc1b | 2020-05-04 12:20:03 +0100 | [diff] [blame] | 610 | * See ssl_build_inner_plaintext() for more information. |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 611 | * |
Hanno Becker | 07dc97d | 2019-05-20 15:08:01 +0100 | [diff] [blame] | 612 | * Note that this changes `rec->data_len`, and hence |
| 613 | * `post_avail` needs to be recalculated afterwards. |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 614 | */ |
Hanno Becker | 581bc1b | 2020-05-04 12:20:03 +0100 | [diff] [blame] | 615 | if( ssl_build_inner_plaintext( data, |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 616 | &rec->data_len, |
| 617 | post_avail, |
Hanno Becker | 1399692 | 2020-05-28 16:15:19 +0100 | [diff] [blame] | 618 | rec->type, |
| 619 | padding ) != 0 ) |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 620 | { |
| 621 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 622 | } |
| 623 | |
| 624 | rec->type = MBEDTLS_SSL_MSG_CID; |
| 625 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 626 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 627 | |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 628 | post_avail = rec->buf_len - ( rec->data_len + rec->data_offset ); |
| 629 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 630 | /* |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 631 | * Add MAC before if needed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 632 | */ |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 633 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 634 | if( mode == MBEDTLS_MODE_STREAM || |
| 635 | ( mode == MBEDTLS_MODE_CBC |
| 636 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 637 | && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 638 | #endif |
| 639 | ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 640 | { |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 641 | if( post_avail < transform->maclen ) |
| 642 | { |
| 643 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 644 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 645 | } |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 646 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 647 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 648 | |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 649 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec, |
| 650 | transform->minor_ver ); |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 651 | |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 652 | mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data, |
| 653 | add_data_len ); |
| 654 | mbedtls_md_hmac_update( &transform->md_ctx_enc, data, rec->data_len ); |
| 655 | mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac ); |
| 656 | mbedtls_md_hmac_reset( &transform->md_ctx_enc ); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 657 | |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 658 | memcpy( data + rec->data_len, mac, transform->maclen ); |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 659 | #endif |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 660 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 661 | MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len, |
| 662 | transform->maclen ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 663 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 664 | rec->data_len += transform->maclen; |
| 665 | post_avail -= transform->maclen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 666 | auth_done++; |
Paul Bakker | 577e006 | 2013-08-28 11:57:20 +0200 | [diff] [blame] | 667 | } |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 668 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 669 | |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 670 | /* |
| 671 | * Encrypt |
| 672 | */ |
Hanno Becker | d086bf0 | 2021-03-22 13:01:27 +0000 | [diff] [blame] | 673 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 674 | if( mode == MBEDTLS_MODE_STREAM ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 675 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 676 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 677 | size_t olen; |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 678 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", " |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 679 | "including %d bytes of padding", |
| 680 | rec->data_len, 0 ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 681 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 682 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc, |
| 683 | transform->iv_enc, transform->ivlen, |
| 684 | data, rec->data_len, |
| 685 | data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 686 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 687 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 688 | return( ret ); |
| 689 | } |
| 690 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 691 | if( rec->data_len != olen ) |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 692 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 693 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 694 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 695 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 696 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 697 | else |
Hanno Becker | d086bf0 | 2021-03-22 13:01:27 +0000 | [diff] [blame] | 698 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_STREAM */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 699 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 700 | #if defined(MBEDTLS_GCM_C) || \ |
| 701 | defined(MBEDTLS_CCM_C) || \ |
| 702 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 703 | if( mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 704 | mode == MBEDTLS_MODE_CCM || |
| 705 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 706 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 707 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 708 | unsigned char iv[12]; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 709 | unsigned char *dynamic_iv; |
| 710 | size_t dynamic_iv_len; |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 711 | int dynamic_iv_is_explicit = |
| 712 | ssl_transform_aead_dynamic_iv_is_explicit( transform ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 713 | |
Hanno Becker | bd5ed1d | 2020-05-21 15:26:39 +0100 | [diff] [blame] | 714 | /* Check that there's space for the authentication tag. */ |
| 715 | if( post_avail < transform->taglen ) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 716 | { |
| 717 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 718 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 719 | } |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 720 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 721 | /* |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 722 | * Build nonce for AEAD encryption. |
| 723 | * |
| 724 | * Note: In the case of CCM and GCM in TLS 1.2, the dynamic |
| 725 | * part of the IV is prepended to the ciphertext and |
| 726 | * can be chosen freely - in particular, it need not |
| 727 | * agree with the record sequence number. |
| 728 | * However, since ChaChaPoly as well as all AEAD modes |
| 729 | * in TLS 1.3 use the record sequence number as the |
| 730 | * dynamic part of the nonce, we uniformly use the |
| 731 | * record sequence number here in all cases. |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 732 | */ |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 733 | dynamic_iv = rec->ctr; |
| 734 | dynamic_iv_len = sizeof( rec->ctr ); |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 735 | |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 736 | ssl_build_record_nonce( iv, sizeof( iv ), |
| 737 | transform->iv_enc, |
| 738 | transform->fixed_ivlen, |
| 739 | dynamic_iv, |
| 740 | dynamic_iv_len ); |
Manuel Pégourié-Gonnard | d056ce0 | 2014-10-29 22:29:20 +0100 | [diff] [blame] | 741 | |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 742 | /* |
| 743 | * Build additional data for AEAD encryption. |
| 744 | * This depends on the TLS version. |
| 745 | */ |
| 746 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec, |
| 747 | transform->minor_ver ); |
Hanno Becker | 1f10d76 | 2019-04-26 13:34:37 +0100 | [diff] [blame] | 748 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 749 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)", |
Hanno Becker | 7cca358 | 2020-06-04 13:27:22 +0100 | [diff] [blame] | 750 | iv, transform->ivlen ); |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 751 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)", |
Hanno Becker | 16bf0e2 | 2020-06-04 13:27:34 +0100 | [diff] [blame] | 752 | dynamic_iv, |
| 753 | dynamic_iv_is_explicit ? dynamic_iv_len : 0 ); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 754 | MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD", |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 755 | add_data, add_data_len ); |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 756 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", " |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 757 | "including 0 bytes of padding", |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 758 | rec->data_len ) ); |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 759 | |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 760 | /* |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 761 | * Encrypt and authenticate |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 762 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 763 | |
Manuel Pégourié-Gonnard | f5cf71e | 2020-12-01 11:43:40 +0100 | [diff] [blame] | 764 | if( ( ret = mbedtls_cipher_auth_encrypt_ext( &transform->cipher_ctx_enc, |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 765 | iv, transform->ivlen, |
Manuel Pégourié-Gonnard | f5cf71e | 2020-12-01 11:43:40 +0100 | [diff] [blame] | 766 | add_data, add_data_len, |
| 767 | data, rec->data_len, /* src */ |
| 768 | data, rec->buf_len - (data - rec->buf), /* dst */ |
| 769 | &rec->data_len, |
| 770 | transform->taglen ) ) != 0 ) |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 771 | { |
TRodziewicz | 18efb73 | 2021-04-29 23:12:19 +0200 | [diff] [blame] | 772 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt_ext", ret ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 773 | return( ret ); |
| 774 | } |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 775 | MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag", |
Manuel Pégourié-Gonnard | f5cf71e | 2020-12-01 11:43:40 +0100 | [diff] [blame] | 776 | data + rec->data_len - transform->taglen, |
| 777 | transform->taglen ); |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 778 | /* Account for authentication tag. */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 779 | post_avail -= transform->taglen; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 780 | |
| 781 | /* |
| 782 | * Prefix record content with dynamic IV in case it is explicit. |
| 783 | */ |
Hanno Becker | 1cda266 | 2020-06-04 13:28:28 +0100 | [diff] [blame] | 784 | if( dynamic_iv_is_explicit != 0 ) |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 785 | { |
| 786 | if( rec->data_offset < dynamic_iv_len ) |
| 787 | { |
| 788 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 789 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 790 | } |
| 791 | |
| 792 | memcpy( data - dynamic_iv_len, dynamic_iv, dynamic_iv_len ); |
| 793 | rec->data_offset -= dynamic_iv_len; |
| 794 | rec->data_len += dynamic_iv_len; |
| 795 | } |
| 796 | |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 797 | auth_done++; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 798 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 799 | else |
Hanno Becker | c3f7b0b | 2020-05-28 16:27:16 +0100 | [diff] [blame] | 800 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */ |
Manuel Pégourié-Gonnard | 2df1f1f | 2020-07-09 12:11:39 +0200 | [diff] [blame] | 801 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 802 | if( mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 803 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 804 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 805 | size_t padlen, i; |
| 806 | size_t olen; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 807 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 808 | /* Currently we're always using minimal padding |
| 809 | * (up to 255 bytes would be allowed). */ |
| 810 | padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen; |
| 811 | if( padlen == transform->ivlen ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 812 | padlen = 0; |
| 813 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 814 | /* Check there's enough space in the buffer for the padding. */ |
| 815 | if( post_avail < padlen + 1 ) |
| 816 | { |
| 817 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 818 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 819 | } |
| 820 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 821 | for( i = 0; i <= padlen; i++ ) |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 822 | data[rec->data_len + i] = (unsigned char) padlen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 823 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 824 | rec->data_len += padlen + 1; |
| 825 | post_avail -= padlen + 1; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 826 | |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 827 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 828 | /* |
TRodziewicz | 2d8800e | 2021-05-13 19:14:19 +0200 | [diff] [blame] | 829 | * 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] | 830 | * Method 1 (6.2.3.2. in RFC4346 and RFC5246) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 831 | */ |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 832 | if( f_rng == NULL ) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 833 | { |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 834 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) ); |
| 835 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 836 | } |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 837 | |
| 838 | if( rec->data_offset < transform->ivlen ) |
| 839 | { |
| 840 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 841 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 842 | } |
| 843 | |
| 844 | /* |
| 845 | * Generate IV |
| 846 | */ |
| 847 | ret = f_rng( p_rng, transform->iv_enc, transform->ivlen ); |
| 848 | if( ret != 0 ) |
| 849 | return( ret ); |
| 850 | |
| 851 | memcpy( data - transform->ivlen, transform->iv_enc, transform->ivlen ); |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 852 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 853 | |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 854 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %" MBEDTLS_PRINTF_SIZET ", " |
| 855 | "including %" MBEDTLS_PRINTF_SIZET |
| 856 | " bytes of IV and %" MBEDTLS_PRINTF_SIZET " bytes of padding", |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 857 | rec->data_len, transform->ivlen, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 858 | padlen + 1 ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 859 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 860 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc, |
| 861 | transform->iv_enc, |
| 862 | transform->ivlen, |
| 863 | data, rec->data_len, |
| 864 | data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 865 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 866 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 867 | return( ret ); |
| 868 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 869 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 870 | if( rec->data_len != olen ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 871 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 872 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 873 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 874 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 875 | |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 876 | data -= transform->ivlen; |
| 877 | rec->data_offset -= transform->ivlen; |
| 878 | rec->data_len += transform->ivlen; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 879 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 880 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 881 | if( auth_done == 0 ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 882 | { |
Hanno Becker | 3d8c907 | 2018-01-05 16:24:22 +0000 | [diff] [blame] | 883 | unsigned char mac[MBEDTLS_SSL_MAC_ADD]; |
| 884 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 885 | /* |
| 886 | * MAC(MAC_write_key, seq_num + |
| 887 | * TLSCipherText.type + |
| 888 | * TLSCipherText.version + |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 889 | * length_of( (IV +) ENC(...) ) + |
TRodziewicz | 2abf03c | 2021-06-25 14:40:09 +0200 | [diff] [blame] | 890 | * IV + |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 891 | * ENC(content + padding + padding_length)); |
| 892 | */ |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 893 | |
| 894 | if( post_avail < transform->maclen) |
| 895 | { |
| 896 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) ); |
| 897 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 898 | } |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 899 | |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 900 | ssl_extract_add_data_from_record( add_data, &add_data_len, |
| 901 | rec, transform->minor_ver ); |
Hanno Becker | 1f10d76 | 2019-04-26 13:34:37 +0100 | [diff] [blame] | 902 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 903 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) ); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 904 | MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 905 | add_data_len ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 906 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 907 | mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data, |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 908 | add_data_len ); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 909 | mbedtls_md_hmac_update( &transform->md_ctx_enc, |
| 910 | data, rec->data_len ); |
| 911 | mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac ); |
| 912 | mbedtls_md_hmac_reset( &transform->md_ctx_enc ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 913 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 914 | memcpy( data + rec->data_len, mac, transform->maclen ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 915 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 916 | rec->data_len += transform->maclen; |
| 917 | post_avail -= transform->maclen; |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 918 | auth_done++; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 919 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 920 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 921 | } |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 922 | else |
Manuel Pégourié-Gonnard | 2df1f1f | 2020-07-09 12:11:39 +0200 | [diff] [blame] | 923 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC) */ |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 924 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 925 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 926 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 927 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 928 | |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 929 | /* Make extra sure authentication was performed, exactly once */ |
| 930 | if( auth_done != 1 ) |
| 931 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 932 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 933 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 934 | } |
| 935 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 936 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 937 | |
| 938 | return( 0 ); |
| 939 | } |
| 940 | |
Manuel Pégourié-Gonnard | ed0e864 | 2020-07-21 11:20:30 +0200 | [diff] [blame] | 941 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC) |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 942 | /* |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 943 | * Constant-flow mask generation for "less than" comparison: |
| 944 | * - if x < y, return all bits 1, that is (size_t) -1 |
| 945 | * - otherwise, return all bits 0, that is 0 |
| 946 | * |
Manuel Pégourié-Gonnard | 6d6f8a4 | 2020-09-25 09:56:53 +0200 | [diff] [blame] | 947 | * This function can be used to write constant-time code by replacing branches |
| 948 | * with bit operations using masks. |
| 949 | * |
| 950 | * This function is implemented without using comparison operators, as those |
| 951 | * 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] | 952 | */ |
gabor-mezei-arm | 9fa43ce | 2021-09-28 16:14:47 +0200 | [diff] [blame] | 953 | static size_t mbedtls_cf_size_mask_lt( size_t x, size_t y ) |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 954 | { |
Manuel Pégourié-Gonnard | 6d6f8a4 | 2020-09-25 09:56:53 +0200 | [diff] [blame] | 955 | /* 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] | 956 | const size_t sub = x - y; |
| 957 | |
Manuel Pégourié-Gonnard | 6d6f8a4 | 2020-09-25 09:56:53 +0200 | [diff] [blame] | 958 | /* sub1 = (x < y) ? 1 : 0 */ |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 959 | const size_t sub1 = sub >> ( sizeof( sub ) * 8 - 1 ); |
| 960 | |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 961 | /* mask = (x < y) ? 0xff... : 0x00... */ |
gabor-mezei-arm | 9fa43ce | 2021-09-28 16:14:47 +0200 | [diff] [blame] | 962 | const size_t mask = mbedtls_cf_size_mask( sub1 ); |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 963 | |
| 964 | return( mask ); |
| 965 | } |
| 966 | |
| 967 | /* |
| 968 | * Constant-flow mask generation for "greater or equal" comparison: |
| 969 | * - if x >= y, return all bits 1, that is (size_t) -1 |
| 970 | * - otherwise, return all bits 0, that is 0 |
| 971 | * |
Manuel Pégourié-Gonnard | 6d6f8a4 | 2020-09-25 09:56:53 +0200 | [diff] [blame] | 972 | * This function can be used to write constant-time code by replacing branches |
| 973 | * with bit operations using masks. |
| 974 | * |
| 975 | * This function is implemented without using comparison operators, as those |
| 976 | * 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] | 977 | */ |
gabor-mezei-arm | 9fa43ce | 2021-09-28 16:14:47 +0200 | [diff] [blame] | 978 | static size_t mbedtls_cf_size_mask_ge( size_t x, size_t y ) |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 979 | { |
gabor-mezei-arm | 9fa43ce | 2021-09-28 16:14:47 +0200 | [diff] [blame] | 980 | return( ~mbedtls_cf_size_mask_lt( x, y ) ); |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | /* |
| 984 | * Constant-flow boolean "equal" comparison: |
| 985 | * return x == y |
| 986 | * |
Manuel Pégourié-Gonnard | 6d6f8a4 | 2020-09-25 09:56:53 +0200 | [diff] [blame] | 987 | * This function can be used to write constant-time code by replacing branches |
| 988 | * with bit operations - it can be used in conjunction with |
gabor-mezei-arm | 9fa43ce | 2021-09-28 16:14:47 +0200 | [diff] [blame] | 989 | * mbedtls_cf_size_mask(). |
Manuel Pégourié-Gonnard | 6d6f8a4 | 2020-09-25 09:56:53 +0200 | [diff] [blame] | 990 | * |
| 991 | * This function is implemented without using comparison operators, as those |
| 992 | * 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] | 993 | */ |
gabor-mezei-arm | 9fa43ce | 2021-09-28 16:14:47 +0200 | [diff] [blame] | 994 | static size_t mbedtls_cf_size_bool_eq( size_t x, size_t y ) |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 995 | { |
| 996 | /* diff = 0 if x == y, non-zero otherwise */ |
| 997 | const size_t diff = x ^ y; |
| 998 | |
| 999 | /* MSVC has a warning about unary minus on unsigned integer types, |
| 1000 | * but this is well-defined and precisely what we want to do here. */ |
| 1001 | #if defined(_MSC_VER) |
| 1002 | #pragma warning( push ) |
| 1003 | #pragma warning( disable : 4146 ) |
| 1004 | #endif |
| 1005 | |
| 1006 | /* diff_msb's most significant bit is equal to x != y */ |
| 1007 | const size_t diff_msb = ( diff | -diff ); |
| 1008 | |
| 1009 | #if defined(_MSC_VER) |
| 1010 | #pragma warning( pop ) |
| 1011 | #endif |
| 1012 | |
Manuel Pégourié-Gonnard | 6d6f8a4 | 2020-09-25 09:56:53 +0200 | [diff] [blame] | 1013 | /* diff1 = (x != y) ? 1 : 0 */ |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 1014 | const size_t diff1 = diff_msb >> ( sizeof( diff_msb ) * 8 - 1 ); |
| 1015 | |
| 1016 | return( 1 ^ diff1 ); |
| 1017 | } |
| 1018 | |
| 1019 | /* |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1020 | * Constant-flow conditional memcpy: |
| 1021 | * - if c1 == c2, equivalent to memcpy(dst, src, len), |
| 1022 | * - otherwise, a no-op, |
| 1023 | * but with execution flow independent of the values of c1 and c2. |
| 1024 | * |
Manuel Pégourié-Gonnard | 6d6f8a4 | 2020-09-25 09:56:53 +0200 | [diff] [blame] | 1025 | * This function is implemented without using comparison operators, as those |
| 1026 | * 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] | 1027 | */ |
gabor-mezei-arm | 9fa43ce | 2021-09-28 16:14:47 +0200 | [diff] [blame] | 1028 | static void mbedtls_cf_memcpy_if_eq( unsigned char *dst, |
| 1029 | const unsigned char *src, |
| 1030 | size_t len, |
| 1031 | size_t c1, size_t c2 ) |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1032 | { |
Manuel Pégourié-Gonnard | 6e2a9a7 | 2020-08-25 10:01:00 +0200 | [diff] [blame] | 1033 | /* mask = c1 == c2 ? 0xff : 0x00 */ |
gabor-mezei-arm | 9fa43ce | 2021-09-28 16:14:47 +0200 | [diff] [blame] | 1034 | const size_t equal = mbedtls_cf_size_bool_eq( c1, c2 ); |
| 1035 | const unsigned char mask = (unsigned char) mbedtls_cf_size_mask( equal ); |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1036 | |
Manuel Pégourié-Gonnard | 6d6f8a4 | 2020-09-25 09:56:53 +0200 | [diff] [blame] | 1037 | /* dst[i] = c1 == c2 ? src[i] : dst[i] */ |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1038 | for( size_t i = 0; i < len; i++ ) |
Manuel Pégourié-Gonnard | 6d6f8a4 | 2020-09-25 09:56:53 +0200 | [diff] [blame] | 1039 | dst[i] = ( src[i] & mask ) | ( dst[i] & ~mask ); |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | /* |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 1043 | * Compute HMAC of variable-length data with constant flow. |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1044 | * |
| 1045 | * Only works with MD-5, SHA-1, SHA-256 and SHA-384. |
| 1046 | * (Otherwise, computation of block_size needs to be adapted.) |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 1047 | */ |
gabor-mezei-arm | 9fa43ce | 2021-09-28 16:14:47 +0200 | [diff] [blame] | 1048 | MBEDTLS_STATIC_TESTABLE int mbedtls_cf_hmac( |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 1049 | mbedtls_md_context_t *ctx, |
| 1050 | const unsigned char *add_data, size_t add_data_len, |
| 1051 | const unsigned char *data, size_t data_len_secret, |
| 1052 | size_t min_data_len, size_t max_data_len, |
| 1053 | unsigned char *output ) |
| 1054 | { |
Manuel Pégourié-Gonnard | 8aa29e3 | 2020-07-07 12:30:39 +0200 | [diff] [blame] | 1055 | /* |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1056 | * This function breaks the HMAC abstraction and uses the md_clone() |
| 1057 | * 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] | 1058 | * |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1059 | * 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] | 1060 | * 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] | 1061 | * 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] | 1062 | * |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1063 | * We'll first compute inner_hash = HASH(ikey + msg) by hashing up to |
| 1064 | * minlen, then cloning the context, and for each byte up to maxlen |
| 1065 | * finishing up the hash computation, keeping only the correct result. |
Manuel Pégourié-Gonnard | 8aa29e3 | 2020-07-07 12:30:39 +0200 | [diff] [blame] | 1066 | * |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1067 | * 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] | 1068 | */ |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1069 | 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] | 1070 | /* 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] | 1071 | * 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] | 1072 | const size_t block_size = md_alg == MBEDTLS_MD_SHA384 ? 128 : 64; |
Manuel Pégourié-Gonnard | 9713e13 | 2020-07-22 10:40:31 +0200 | [diff] [blame] | 1073 | const unsigned char * const ikey = ctx->hmac_ctx; |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1074 | const unsigned char * const okey = ikey + block_size; |
| 1075 | 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] | 1076 | |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1077 | unsigned char aux_out[MBEDTLS_MD_MAX_SIZE]; |
| 1078 | mbedtls_md_context_t aux; |
| 1079 | size_t offset; |
Manuel Pégourié-Gonnard | e0765f3 | 2020-07-22 12:22:51 +0200 | [diff] [blame] | 1080 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 8aa29e3 | 2020-07-07 12:30:39 +0200 | [diff] [blame] | 1081 | |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1082 | mbedtls_md_init( &aux ); |
Manuel Pégourié-Gonnard | 44c9fdd | 2020-07-22 10:48:47 +0200 | [diff] [blame] | 1083 | |
| 1084 | #define MD_CHK( func_call ) \ |
| 1085 | do { \ |
| 1086 | ret = (func_call); \ |
| 1087 | if( ret != 0 ) \ |
| 1088 | goto cleanup; \ |
| 1089 | } while( 0 ) |
| 1090 | |
| 1091 | MD_CHK( mbedtls_md_setup( &aux, ctx->md_info, 0 ) ); |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1092 | |
| 1093 | /* After hmac_start() of hmac_reset(), ikey has already been hashed, |
| 1094 | * so we can start directly with the message */ |
Manuel Pégourié-Gonnard | 44c9fdd | 2020-07-22 10:48:47 +0200 | [diff] [blame] | 1095 | MD_CHK( mbedtls_md_update( ctx, add_data, add_data_len ) ); |
| 1096 | MD_CHK( mbedtls_md_update( ctx, data, min_data_len ) ); |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1097 | |
| 1098 | /* For each possible length, compute the hash up to that point */ |
| 1099 | for( offset = min_data_len; offset <= max_data_len; offset++ ) |
Manuel Pégourié-Gonnard | 8aa29e3 | 2020-07-07 12:30:39 +0200 | [diff] [blame] | 1100 | { |
Manuel Pégourié-Gonnard | 44c9fdd | 2020-07-22 10:48:47 +0200 | [diff] [blame] | 1101 | MD_CHK( mbedtls_md_clone( &aux, ctx ) ); |
| 1102 | MD_CHK( mbedtls_md_finish( &aux, aux_out ) ); |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1103 | /* Keep only the correct inner_hash in the output buffer */ |
gabor-mezei-arm | 9fa43ce | 2021-09-28 16:14:47 +0200 | [diff] [blame] | 1104 | mbedtls_cf_memcpy_if_eq( output, aux_out, hash_size, |
| 1105 | offset, data_len_secret ); |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1106 | |
| 1107 | if( offset < max_data_len ) |
Manuel Pégourié-Gonnard | 44c9fdd | 2020-07-22 10:48:47 +0200 | [diff] [blame] | 1108 | MD_CHK( mbedtls_md_update( ctx, data + offset, 1 ) ); |
Manuel Pégourié-Gonnard | 8aa29e3 | 2020-07-07 12:30:39 +0200 | [diff] [blame] | 1109 | } |
| 1110 | |
Manuel Pégourié-Gonnard | 5ca21db | 2021-05-17 12:28:08 +0200 | [diff] [blame] | 1111 | /* The context needs to finish() before it starts() again */ |
| 1112 | MD_CHK( mbedtls_md_finish( ctx, aux_out ) ); |
| 1113 | |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1114 | /* Now compute HASH(okey + inner_hash) */ |
Manuel Pégourié-Gonnard | 44c9fdd | 2020-07-22 10:48:47 +0200 | [diff] [blame] | 1115 | MD_CHK( mbedtls_md_starts( ctx ) ); |
| 1116 | MD_CHK( mbedtls_md_update( ctx, okey, block_size ) ); |
| 1117 | MD_CHK( mbedtls_md_update( ctx, output, hash_size ) ); |
| 1118 | MD_CHK( mbedtls_md_finish( ctx, output ) ); |
Manuel Pégourié-Gonnard | 8aa29e3 | 2020-07-07 12:30:39 +0200 | [diff] [blame] | 1119 | |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1120 | /* Done, get ready for next time */ |
Manuel Pégourié-Gonnard | 44c9fdd | 2020-07-22 10:48:47 +0200 | [diff] [blame] | 1121 | MD_CHK( mbedtls_md_hmac_reset( ctx ) ); |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 1122 | |
Manuel Pégourié-Gonnard | 44c9fdd | 2020-07-22 10:48:47 +0200 | [diff] [blame] | 1123 | #undef MD_CHK |
| 1124 | |
| 1125 | cleanup: |
Manuel Pégourié-Gonnard | 7a8b1e6 | 2020-07-15 11:52:14 +0200 | [diff] [blame] | 1126 | mbedtls_md_free( &aux ); |
Manuel Pégourié-Gonnard | 44c9fdd | 2020-07-22 10:48:47 +0200 | [diff] [blame] | 1127 | return( ret ); |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 1128 | } |
Manuel Pégourié-Gonnard | 7fe2c5f | 2020-08-18 12:02:54 +0200 | [diff] [blame] | 1129 | |
| 1130 | /* |
| 1131 | * Constant-flow memcpy from variable position in buffer. |
| 1132 | * - functionally equivalent to memcpy(dst, src + offset_secret, len) |
Manuel Pégourié-Gonnard | ba6fc97 | 2020-08-24 12:59:55 +0200 | [diff] [blame] | 1133 | * - 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] | 1134 | */ |
gabor-mezei-arm | 9fa43ce | 2021-09-28 16:14:47 +0200 | [diff] [blame] | 1135 | MBEDTLS_STATIC_TESTABLE void mbedtls_cf_memcpy_offset( |
Manuel Pégourié-Gonnard | 7fe2c5f | 2020-08-18 12:02:54 +0200 | [diff] [blame] | 1136 | unsigned char *dst, |
| 1137 | const unsigned char *src_base, |
| 1138 | size_t offset_secret, |
| 1139 | size_t offset_min, size_t offset_max, |
| 1140 | size_t len ) |
| 1141 | { |
Manuel Pégourié-Gonnard | de1cf2c5 | 2020-08-19 12:35:30 +0200 | [diff] [blame] | 1142 | size_t offset; |
| 1143 | |
| 1144 | for( offset = offset_min; offset <= offset_max; offset++ ) |
| 1145 | { |
gabor-mezei-arm | 9fa43ce | 2021-09-28 16:14:47 +0200 | [diff] [blame] | 1146 | mbedtls_cf_memcpy_if_eq( dst, src_base + offset, len, |
| 1147 | offset, offset_secret ); |
Manuel Pégourié-Gonnard | de1cf2c5 | 2020-08-19 12:35:30 +0200 | [diff] [blame] | 1148 | } |
Manuel Pégourié-Gonnard | 7fe2c5f | 2020-08-18 12:02:54 +0200 | [diff] [blame] | 1149 | } |
Manuel Pégourié-Gonnard | ed0e864 | 2020-07-21 11:20:30 +0200 | [diff] [blame] | 1150 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC */ |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 1151 | |
Hanno Becker | 605949f | 2019-07-12 08:23:59 +0100 | [diff] [blame] | 1152 | int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl, |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1153 | mbedtls_ssl_transform *transform, |
| 1154 | mbedtls_record *rec ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1155 | { |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1156 | size_t olen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1157 | mbedtls_cipher_mode_t mode; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1158 | int ret, auth_done = 0; |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 1159 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 1160 | size_t padlen = 0, correct = 1; |
| 1161 | #endif |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1162 | unsigned char* data; |
Hanno Becker | 92fb4fa | 2019-05-20 14:54:26 +0100 | [diff] [blame] | 1163 | unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ]; |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1164 | size_t add_data_len; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1165 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1166 | #if !defined(MBEDTLS_DEBUG_C) |
Manuel Pégourié-Gonnard | a7505d1 | 2019-05-07 10:17:56 +0200 | [diff] [blame] | 1167 | ssl = NULL; /* make sure we don't use it except for debug */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1168 | ((void) ssl); |
| 1169 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1170 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1171 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) ); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1172 | if( rec == NULL || |
| 1173 | rec->buf == NULL || |
| 1174 | rec->buf_len < rec->data_offset || |
| 1175 | rec->buf_len - rec->data_offset < rec->data_len ) |
| 1176 | { |
| 1177 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1178 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1179 | } |
| 1180 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1181 | data = rec->buf + rec->data_offset; |
| 1182 | mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1183 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1184 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1185 | /* |
| 1186 | * Match record's CID with incoming CID. |
| 1187 | */ |
Hanno Becker | 938489a | 2019-05-08 13:02:22 +0100 | [diff] [blame] | 1188 | if( rec->cid_len != transform->in_cid_len || |
| 1189 | memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) |
| 1190 | { |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 1191 | return( MBEDTLS_ERR_SSL_UNEXPECTED_CID ); |
Hanno Becker | 938489a | 2019-05-08 13:02:22 +0100 | [diff] [blame] | 1192 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1193 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1194 | |
Hanno Becker | d086bf0 | 2021-03-22 13:01:27 +0000 | [diff] [blame] | 1195 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_STREAM) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1196 | if( mode == MBEDTLS_MODE_STREAM ) |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1197 | { |
| 1198 | padlen = 0; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1199 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec, |
| 1200 | transform->iv_dec, |
| 1201 | transform->ivlen, |
| 1202 | data, rec->data_len, |
| 1203 | data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 1204 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1205 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1206 | return( ret ); |
| 1207 | } |
| 1208 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1209 | if( rec->data_len != olen ) |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1210 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1211 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1212 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | ea6ad3f | 2013-09-02 14:57:01 +0200 | [diff] [blame] | 1213 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1214 | } |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1215 | else |
Hanno Becker | d086bf0 | 2021-03-22 13:01:27 +0000 | [diff] [blame] | 1216 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_STREAM */ |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1217 | #if defined(MBEDTLS_GCM_C) || \ |
| 1218 | defined(MBEDTLS_CCM_C) || \ |
| 1219 | defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1220 | if( mode == MBEDTLS_MODE_GCM || |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1221 | mode == MBEDTLS_MODE_CCM || |
| 1222 | mode == MBEDTLS_MODE_CHACHAPOLY ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1223 | { |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1224 | unsigned char iv[12]; |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1225 | unsigned char *dynamic_iv; |
| 1226 | size_t dynamic_iv_len; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1227 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1228 | /* |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1229 | * Extract dynamic part of nonce for AEAD decryption. |
| 1230 | * |
| 1231 | * Note: In the case of CCM and GCM in TLS 1.2, the dynamic |
| 1232 | * part of the IV is prepended to the ciphertext and |
| 1233 | * can be chosen freely - in particular, it need not |
| 1234 | * agree with the record sequence number. |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1235 | */ |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1236 | dynamic_iv_len = sizeof( rec->ctr ); |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 1237 | if( ssl_transform_aead_dynamic_iv_is_explicit( transform ) == 1 ) |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1238 | { |
| 1239 | if( rec->data_len < dynamic_iv_len ) |
| 1240 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1241 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET |
| 1242 | " ) < explicit_iv_len (%" MBEDTLS_PRINTF_SIZET ") ", |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1243 | rec->data_len, |
| 1244 | dynamic_iv_len ) ); |
| 1245 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
| 1246 | } |
| 1247 | dynamic_iv = data; |
| 1248 | |
| 1249 | data += dynamic_iv_len; |
| 1250 | rec->data_offset += dynamic_iv_len; |
| 1251 | rec->data_len -= dynamic_iv_len; |
| 1252 | } |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 1253 | else |
| 1254 | { |
| 1255 | dynamic_iv = rec->ctr; |
| 1256 | } |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1257 | |
| 1258 | /* Check that there's space for the authentication tag. */ |
| 1259 | if( rec->data_len < transform->taglen ) |
| 1260 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1261 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET |
| 1262 | ") < taglen (%" MBEDTLS_PRINTF_SIZET ") ", |
Christian von Arnim | 883d304 | 2020-12-01 11:58:29 +0100 | [diff] [blame] | 1263 | rec->data_len, |
| 1264 | transform->taglen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1265 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 0bcc4e1 | 2014-06-17 10:54:17 +0200 | [diff] [blame] | 1266 | } |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1267 | rec->data_len -= transform->taglen; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1268 | |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1269 | /* |
| 1270 | * Prepare nonce from dynamic and static parts. |
| 1271 | */ |
Hanno Becker | 1726380 | 2020-05-28 07:05:48 +0100 | [diff] [blame] | 1272 | ssl_build_record_nonce( iv, sizeof( iv ), |
| 1273 | transform->iv_dec, |
| 1274 | transform->fixed_ivlen, |
| 1275 | dynamic_iv, |
| 1276 | dynamic_iv_len ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1277 | |
Hanno Becker | df8be22 | 2020-05-21 15:30:57 +0100 | [diff] [blame] | 1278 | /* |
| 1279 | * Build additional data for AEAD encryption. |
| 1280 | * This depends on the TLS version. |
| 1281 | */ |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 1282 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec, |
| 1283 | transform->minor_ver ); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1284 | MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD", |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1285 | add_data, add_data_len ); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1286 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1287 | /* Because of the check above, we know that there are |
| 1288 | * explicit_iv_len Bytes preceeding data, and taglen |
| 1289 | * bytes following data + data_len. This justifies |
Hanno Becker | 2001665 | 2019-07-10 11:44:13 +0100 | [diff] [blame] | 1290 | * the debug message and the invocation of |
TRodziewicz | 18efb73 | 2021-04-29 23:12:19 +0200 | [diff] [blame] | 1291 | * mbedtls_cipher_auth_decrypt_ext() below. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1292 | |
Manuel Pégourié-Gonnard | 2e58e8e | 2018-06-18 11:16:43 +0200 | [diff] [blame] | 1293 | MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen ); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1294 | MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len, |
Hanno Becker | e694c3e | 2017-12-27 21:34:08 +0000 | [diff] [blame] | 1295 | transform->taglen ); |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 1296 | |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1297 | /* |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1298 | * Decrypt and authenticate |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1299 | */ |
Manuel Pégourié-Gonnard | f5cf71e | 2020-12-01 11:43:40 +0100 | [diff] [blame] | 1300 | if( ( ret = mbedtls_cipher_auth_decrypt_ext( &transform->cipher_ctx_dec, |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1301 | iv, transform->ivlen, |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1302 | add_data, add_data_len, |
Manuel Pégourié-Gonnard | f5cf71e | 2020-12-01 11:43:40 +0100 | [diff] [blame] | 1303 | data, rec->data_len + transform->taglen, /* src */ |
| 1304 | data, rec->buf_len - (data - rec->buf), &olen, /* dst */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1305 | transform->taglen ) ) != 0 ) |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1306 | { |
TRodziewicz | 18efb73 | 2021-04-29 23:12:19 +0200 | [diff] [blame] | 1307 | 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] | 1308 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1309 | if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED ) |
| 1310 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | de7bb44 | 2014-05-13 12:41:10 +0200 | [diff] [blame] | 1311 | |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1312 | return( ret ); |
| 1313 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1314 | auth_done++; |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1315 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1316 | /* Double-check that AEAD decryption doesn't change content length. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1317 | if( olen != rec->data_len ) |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1318 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1319 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1320 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | d13a409 | 2013-09-05 16:10:41 +0200 | [diff] [blame] | 1321 | } |
Paul Bakker | ca4ab49 | 2012-04-18 14:23:57 +0000 | [diff] [blame] | 1322 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1323 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1324 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */ |
Manuel Pégourié-Gonnard | 2df1f1f | 2020-07-09 12:11:39 +0200 | [diff] [blame] | 1325 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1326 | if( mode == MBEDTLS_MODE_CBC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1327 | { |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 1328 | size_t minlen = 0; |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1329 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1330 | /* |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1331 | * Check immediate ciphertext sanity |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1332 | */ |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 1333 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 1334 | /* The ciphertext is prefixed with the CBC IV. */ |
| 1335 | minlen += transform->ivlen; |
Paul Bakker | d2f068e | 2013-08-27 21:19:20 +0200 | [diff] [blame] | 1336 | #endif |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1337 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1338 | /* Size considerations: |
| 1339 | * |
| 1340 | * - The CBC cipher text must not be empty and hence |
| 1341 | * at least of size transform->ivlen. |
| 1342 | * |
| 1343 | * Together with the potential IV-prefix, this explains |
| 1344 | * the first of the two checks below. |
| 1345 | * |
| 1346 | * - The record must contain a MAC, either in plain or |
| 1347 | * encrypted, depending on whether Encrypt-then-MAC |
| 1348 | * is used or not. |
| 1349 | * - If it is, the message contains the IV-prefix, |
| 1350 | * the CBC ciphertext, and the MAC. |
| 1351 | * - If it is not, the padded plaintext, and hence |
| 1352 | * the CBC ciphertext, has at least length maclen + 1 |
| 1353 | * because there is at least the padding length byte. |
| 1354 | * |
| 1355 | * As the CBC ciphertext is not empty, both cases give the |
| 1356 | * lower bound minlen + maclen + 1 on the record size, which |
| 1357 | * we test for in the second check below. |
| 1358 | */ |
| 1359 | if( rec->data_len < minlen + transform->ivlen || |
| 1360 | rec->data_len < minlen + transform->maclen + 1 ) |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1361 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1362 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET |
| 1363 | ") < max( ivlen(%" MBEDTLS_PRINTF_SIZET |
| 1364 | "), maclen (%" MBEDTLS_PRINTF_SIZET ") " |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1365 | "+ 1 ) ( + expl IV )", rec->data_len, |
| 1366 | transform->ivlen, |
| 1367 | transform->maclen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1368 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1369 | } |
| 1370 | |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1371 | /* |
| 1372 | * Authenticate before decrypt if enabled |
| 1373 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1374 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1375 | if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1376 | { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 1377 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD]; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1378 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1379 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1380 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1381 | /* Update data_len in tandem with add_data. |
| 1382 | * |
| 1383 | * The subtraction is safe because of the previous check |
| 1384 | * data_len >= minlen + maclen + 1. |
| 1385 | * |
| 1386 | * Afterwards, we know that data + data_len is followed by at |
| 1387 | * least maclen Bytes, which justifies the call to |
| 1388 | * mbedtls_ssl_safer_memcmp() below. |
| 1389 | * |
| 1390 | * Further, we still know that data_len > minlen */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1391 | rec->data_len -= transform->maclen; |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 1392 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec, |
| 1393 | transform->minor_ver ); |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 1394 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1395 | /* Calculate expected MAC. */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 1396 | MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data, |
| 1397 | add_data_len ); |
| 1398 | mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data, |
| 1399 | add_data_len ); |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1400 | mbedtls_md_hmac_update( &transform->md_ctx_dec, |
| 1401 | data, rec->data_len ); |
| 1402 | mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect ); |
| 1403 | mbedtls_md_hmac_reset( &transform->md_ctx_dec ); |
Manuel Pégourié-Gonnard | 08558e5 | 2014-11-04 14:40:21 +0100 | [diff] [blame] | 1404 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1405 | MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, |
| 1406 | transform->maclen ); |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 1407 | MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1408 | transform->maclen ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1409 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1410 | /* Compare expected MAC with MAC at the end of the record. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1411 | if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect, |
| 1412 | transform->maclen ) != 0 ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1413 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1414 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1415 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1416 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1417 | auth_done++; |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1418 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1419 | #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */ |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1420 | |
| 1421 | /* |
| 1422 | * Check length sanity |
| 1423 | */ |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1424 | |
| 1425 | /* We know from above that data_len > minlen >= 0, |
| 1426 | * so the following check in particular implies that |
| 1427 | * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1428 | if( rec->data_len % transform->ivlen != 0 ) |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1429 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1430 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET |
| 1431 | ") %% ivlen (%" MBEDTLS_PRINTF_SIZET ") != 0", |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1432 | rec->data_len, transform->ivlen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1433 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1434 | } |
| 1435 | |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 1436 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1437 | /* |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 1438 | * Initialize for prepended IV for block cipher in TLS v1.2 |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1439 | */ |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 1440 | /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */ |
| 1441 | memcpy( transform->iv_dec, data, transform->ivlen ); |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1442 | |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 1443 | data += transform->ivlen; |
| 1444 | rec->data_offset += transform->ivlen; |
| 1445 | rec->data_len -= transform->ivlen; |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 1446 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 2e11f7d | 2010-07-25 14:24:53 +0000 | [diff] [blame] | 1447 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1448 | /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */ |
| 1449 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1450 | if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec, |
| 1451 | transform->iv_dec, transform->ivlen, |
| 1452 | data, rec->data_len, data, &olen ) ) != 0 ) |
Paul Bakker | 45125bc | 2013-09-04 16:47:11 +0200 | [diff] [blame] | 1453 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1454 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1455 | return( ret ); |
| 1456 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1457 | |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1458 | /* Double-check that length hasn't changed during decryption. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1459 | if( rec->data_len != olen ) |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1460 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1461 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1462 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Paul Bakker | cca5b81 | 2013-08-31 17:40:26 +0200 | [diff] [blame] | 1463 | } |
Paul Bakker | da02a7f | 2013-08-31 17:25:14 +0200 | [diff] [blame] | 1464 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1465 | /* Safe since data_len >= minlen + maclen + 1, so after having |
| 1466 | * subtracted at most minlen and maclen up to this point, |
Hanno Becker | d96a652 | 2019-07-10 13:55:25 +0100 | [diff] [blame] | 1467 | * data_len > 0 (because of data_len % ivlen == 0, it's actually |
| 1468 | * >= ivlen ). */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1469 | padlen = data[rec->data_len - 1]; |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1470 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1471 | if( auth_done == 1 ) |
| 1472 | { |
gabor-mezei-arm | 9fa43ce | 2021-09-28 16:14:47 +0200 | [diff] [blame] | 1473 | const size_t mask = mbedtls_cf_size_mask_ge( |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 1474 | rec->data_len, |
| 1475 | padlen + 1 ); |
| 1476 | correct &= mask; |
| 1477 | padlen &= mask; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1478 | } |
| 1479 | else |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1480 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1481 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1482 | if( rec->data_len < transform->maclen + padlen + 1 ) |
| 1483 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1484 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%" MBEDTLS_PRINTF_SIZET |
| 1485 | ") < maclen (%" MBEDTLS_PRINTF_SIZET |
| 1486 | ") + padlen (%" MBEDTLS_PRINTF_SIZET ")", |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1487 | rec->data_len, |
| 1488 | transform->maclen, |
| 1489 | padlen + 1 ) ); |
| 1490 | } |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 1491 | #endif |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1492 | |
gabor-mezei-arm | 9fa43ce | 2021-09-28 16:14:47 +0200 | [diff] [blame] | 1493 | const size_t mask = mbedtls_cf_size_mask_ge( |
Manuel Pégourié-Gonnard | 2ddec43 | 2020-08-24 12:49:23 +0200 | [diff] [blame] | 1494 | rec->data_len, |
| 1495 | transform->maclen + padlen + 1 ); |
| 1496 | correct &= mask; |
| 1497 | padlen &= mask; |
Paul Bakker | 4582999 | 2013-01-03 14:52:21 +0100 | [diff] [blame] | 1498 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1499 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1500 | padlen++; |
| 1501 | |
| 1502 | /* Regardless of the validity of the padding, |
| 1503 | * we have data_len >= padlen here. */ |
| 1504 | |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 1505 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1506 | /* The padding check involves a series of up to 256 |
| 1507 | * consecutive memory reads at the end of the record |
| 1508 | * plaintext buffer. In order to hide the length and |
| 1509 | * validity of the padding, always perform exactly |
| 1510 | * `min(256,plaintext_len)` reads (but take into account |
| 1511 | * only the last `padlen` bytes for the padding check). */ |
| 1512 | size_t pad_count = 0; |
| 1513 | volatile unsigned char* const check = data; |
| 1514 | |
| 1515 | /* Index of first padding byte; it has been ensured above |
| 1516 | * that the subtraction is safe. */ |
| 1517 | size_t const padding_idx = rec->data_len - padlen; |
| 1518 | size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256; |
| 1519 | size_t const start_idx = rec->data_len - num_checks; |
| 1520 | size_t idx; |
| 1521 | |
| 1522 | for( idx = start_idx; idx < rec->data_len; idx++ ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1523 | { |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1524 | /* pad_count += (idx >= padding_idx) && |
| 1525 | * (check[idx] == padlen - 1); |
| 1526 | */ |
gabor-mezei-arm | 9fa43ce | 2021-09-28 16:14:47 +0200 | [diff] [blame] | 1527 | const size_t mask = mbedtls_cf_size_mask_ge( idx, padding_idx ); |
| 1528 | const size_t equal = mbedtls_cf_size_bool_eq( check[idx], |
| 1529 | padlen - 1 ); |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1530 | pad_count += mask & equal; |
| 1531 | } |
gabor-mezei-arm | 9fa43ce | 2021-09-28 16:14:47 +0200 | [diff] [blame] | 1532 | correct &= mbedtls_cf_size_bool_eq( pad_count, padlen ); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 1533 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1534 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1535 | if( padlen > 0 && correct == 0 ) |
| 1536 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) ); |
Paul Bakker | d66f070 | 2013-01-31 16:57:45 +0100 | [diff] [blame] | 1537 | #endif |
gabor-mezei-arm | 9fa43ce | 2021-09-28 16:14:47 +0200 | [diff] [blame] | 1538 | padlen &= mbedtls_cf_size_mask( correct ); |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1539 | |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 1540 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1541 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1542 | /* If the padding was found to be invalid, padlen == 0 |
| 1543 | * and the subtraction is safe. If the padding was found valid, |
| 1544 | * padlen hasn't been changed and the previous assertion |
| 1545 | * data_len >= padlen still holds. */ |
| 1546 | rec->data_len -= padlen; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1547 | } |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 1548 | else |
Manuel Pégourié-Gonnard | 2df1f1f | 2020-07-09 12:11:39 +0200 | [diff] [blame] | 1549 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC */ |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 1550 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1551 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1552 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | f7dc378 | 2013-09-13 14:10:44 +0200 | [diff] [blame] | 1553 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1554 | |
Manuel Pégourié-Gonnard | 6a25cfa | 2018-07-10 11:15:36 +0200 | [diff] [blame] | 1555 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1556 | MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption", |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1557 | data, rec->data_len ); |
Manuel Pégourié-Gonnard | 6a25cfa | 2018-07-10 11:15:36 +0200 | [diff] [blame] | 1558 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1559 | |
| 1560 | /* |
Manuel Pégourié-Gonnard | 313d796 | 2014-10-29 12:07:57 +0100 | [diff] [blame] | 1561 | * Authenticate if not done yet. |
| 1562 | * Compute the MAC regardless of the padding result (RFC4346, CBCTIME). |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1563 | */ |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 1564 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1565 | if( auth_done == 0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1566 | { |
Hanno Becker | 992b687 | 2017-11-09 18:57:39 +0000 | [diff] [blame] | 1567 | unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD]; |
Manuel Pégourié-Gonnard | 3c31afa | 2020-08-13 12:08:54 +0200 | [diff] [blame] | 1568 | unsigned char mac_peer[MBEDTLS_SSL_MAC_ADD]; |
Paul Bakker | 1e5369c | 2013-12-19 16:40:57 +0100 | [diff] [blame] | 1569 | |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1570 | /* If the initial value of padlen was such that |
| 1571 | * data_len < maclen + padlen + 1, then padlen |
| 1572 | * got reset to 1, and the initial check |
| 1573 | * data_len >= minlen + maclen + 1 |
| 1574 | * guarantees that at this point we still |
| 1575 | * have at least data_len >= maclen. |
| 1576 | * |
| 1577 | * If the initial value of padlen was such that |
| 1578 | * data_len >= maclen + padlen + 1, then we have |
| 1579 | * subtracted either padlen + 1 (if the padding was correct) |
| 1580 | * or 0 (if the padding was incorrect) since then, |
| 1581 | * hence data_len >= maclen in any case. |
| 1582 | */ |
| 1583 | rec->data_len -= transform->maclen; |
Hanno Becker | 1cb6c2a | 2020-05-21 15:25:21 +0100 | [diff] [blame] | 1584 | ssl_extract_add_data_from_record( add_data, &add_data_len, rec, |
| 1585 | transform->minor_ver ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1586 | |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 1587 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1588 | /* |
| 1589 | * The next two sizes are the minimum and maximum values of |
| 1590 | * data_len over all padlen values. |
| 1591 | * |
| 1592 | * They're independent of padlen, since we previously did |
| 1593 | * data_len -= padlen. |
| 1594 | * |
| 1595 | * Note that max_len + maclen is never more than the buffer |
| 1596 | * length, as we previously did in_msglen -= maclen too. |
| 1597 | */ |
| 1598 | const size_t max_len = rec->data_len + padlen; |
| 1599 | const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0; |
| 1600 | |
gabor-mezei-arm | 9fa43ce | 2021-09-28 16:14:47 +0200 | [diff] [blame] | 1601 | ret = mbedtls_cf_hmac( &transform->md_ctx_dec, |
| 1602 | add_data, add_data_len, |
| 1603 | data, rec->data_len, min_len, max_len, |
| 1604 | mac_expect ); |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1605 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1606 | { |
gabor-mezei-arm | 9fa43ce | 2021-09-28 16:14:47 +0200 | [diff] [blame] | 1607 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cf_hmac", ret ); |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1608 | return( ret ); |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1609 | } |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1610 | |
gabor-mezei-arm | 9fa43ce | 2021-09-28 16:14:47 +0200 | [diff] [blame] | 1611 | mbedtls_cf_memcpy_offset( mac_peer, data, |
| 1612 | rec->data_len, |
| 1613 | min_len, max_len, |
| 1614 | transform->maclen ); |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 1615 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1616 | |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 1617 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1618 | MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen ); |
Manuel Pégourié-Gonnard | 3c31afa | 2020-08-13 12:08:54 +0200 | [diff] [blame] | 1619 | MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", mac_peer, transform->maclen ); |
Manuel Pégourié-Gonnard | 7b42030 | 2018-06-28 10:38:35 +0200 | [diff] [blame] | 1620 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1621 | |
Manuel Pégourié-Gonnard | 3c31afa | 2020-08-13 12:08:54 +0200 | [diff] [blame] | 1622 | if( mbedtls_ssl_safer_memcmp( mac_peer, mac_expect, |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 1623 | transform->maclen ) != 0 ) |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1624 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1625 | #if defined(MBEDTLS_SSL_DEBUG_ALL) |
| 1626 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) ); |
Paul Bakker | e47b34b | 2013-02-27 14:48:00 +0100 | [diff] [blame] | 1627 | #endif |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1628 | correct = 0; |
| 1629 | } |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1630 | auth_done++; |
Manuel Pégourié-Gonnard | 7109624 | 2013-10-25 19:31:25 +0200 | [diff] [blame] | 1631 | } |
Hanno Becker | dd3ab13 | 2018-10-17 14:43:14 +0100 | [diff] [blame] | 1632 | |
| 1633 | /* |
| 1634 | * Finally check the correct flag |
| 1635 | */ |
| 1636 | if( correct == 0 ) |
| 1637 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 1638 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1639 | |
| 1640 | /* Make extra sure authentication was performed, exactly once */ |
| 1641 | if( auth_done != 1 ) |
| 1642 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1643 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1644 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 352143f | 2015-01-13 10:59:51 +0100 | [diff] [blame] | 1645 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1646 | |
Hanno Becker | ccc13d0 | 2020-05-04 12:30:04 +0100 | [diff] [blame] | 1647 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
| 1648 | if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 ) |
| 1649 | { |
| 1650 | /* Remove inner padding and infer true content type. */ |
| 1651 | ret = ssl_parse_inner_plaintext( data, &rec->data_len, |
| 1652 | &rec->type ); |
| 1653 | |
| 1654 | if( ret != 0 ) |
| 1655 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 1656 | } |
| 1657 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
| 1658 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1659 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1660 | if( rec->cid_len != 0 ) |
| 1661 | { |
Hanno Becker | 581bc1b | 2020-05-04 12:20:03 +0100 | [diff] [blame] | 1662 | ret = ssl_parse_inner_plaintext( data, &rec->data_len, |
| 1663 | &rec->type ); |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1664 | if( ret != 0 ) |
| 1665 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 1666 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1667 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 8b3eb5a | 2019-04-29 17:31:37 +0100 | [diff] [blame] | 1668 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1669 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1670 | |
| 1671 | return( 0 ); |
| 1672 | } |
| 1673 | |
Manuel Pégourié-Gonnard | 0098e7d | 2014-10-28 13:08:59 +0100 | [diff] [blame] | 1674 | #undef MAC_NONE |
| 1675 | #undef MAC_PLAINTEXT |
| 1676 | #undef MAC_CIPHERTEXT |
| 1677 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1678 | /* |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 1679 | * Fill the input message buffer by appending data to it. |
| 1680 | * 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] | 1681 | * |
| 1682 | * If we return 0, is it guaranteed that (at least) nb_want bytes are |
| 1683 | * available (from this read and/or a previous one). Otherwise, an error code |
| 1684 | * is returned (possibly EOF or WANT_READ). |
| 1685 | * |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 1686 | * With stream transport (TLS) on success ssl->in_left == nb_want, but |
| 1687 | * with datagram transport (DTLS) on success ssl->in_left >= nb_want, |
| 1688 | * since we always read a whole datagram at once. |
| 1689 | * |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 1690 | * 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] | 1691 | * they're done reading a record. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1692 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1693 | 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] | 1694 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1695 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1696 | size_t len; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1697 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1698 | size_t in_buf_len = ssl->in_buf_len; |
| 1699 | #else |
| 1700 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 1701 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1702 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1703 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1704 | |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 1705 | if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL ) |
| 1706 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1707 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() " |
Manuel Pégourié-Gonnard | 1b511f9 | 2015-05-06 15:54:23 +0100 | [diff] [blame] | 1708 | "or mbedtls_ssl_set_bio()" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1709 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 1710 | } |
| 1711 | |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1712 | if( nb_want > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) ) |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 1713 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1714 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) ); |
| 1715 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 1716 | } |
| 1717 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1718 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1719 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1720 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 1721 | uint32_t timeout; |
| 1722 | |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 1723 | /* |
| 1724 | * The point is, we need to always read a full datagram at once, so we |
| 1725 | * sometimes read more then requested, and handle the additional data. |
| 1726 | * It could be the rest of the current record (while fetching the |
| 1727 | * header) and/or some other records in the same datagram. |
| 1728 | */ |
| 1729 | |
| 1730 | /* |
| 1731 | * Move to the next record in the already read datagram if applicable |
| 1732 | */ |
| 1733 | if( ssl->next_record_offset != 0 ) |
| 1734 | { |
| 1735 | if( ssl->in_left < ssl->next_record_offset ) |
| 1736 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1737 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1738 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 1739 | } |
| 1740 | |
| 1741 | ssl->in_left -= ssl->next_record_offset; |
| 1742 | |
| 1743 | if( ssl->in_left != 0 ) |
| 1744 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1745 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %" |
| 1746 | MBEDTLS_PRINTF_SIZET, |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 1747 | ssl->next_record_offset ) ); |
| 1748 | memmove( ssl->in_hdr, |
| 1749 | ssl->in_hdr + ssl->next_record_offset, |
| 1750 | ssl->in_left ); |
| 1751 | } |
| 1752 | |
| 1753 | ssl->next_record_offset = 0; |
| 1754 | } |
| 1755 | |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1756 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %" MBEDTLS_PRINTF_SIZET |
| 1757 | ", nb_want: %" MBEDTLS_PRINTF_SIZET, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1758 | ssl->in_left, nb_want ) ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1759 | |
| 1760 | /* |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 1761 | * Done if we already have enough data. |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1762 | */ |
| 1763 | if( nb_want <= ssl->in_left) |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 1764 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1765 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1766 | return( 0 ); |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 1767 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1768 | |
| 1769 | /* |
Antonin Décimo | 36e89b5 | 2019-01-23 15:24:37 +0100 | [diff] [blame] | 1770 | * 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] | 1771 | * are not at the beginning of a new record, the caller did something |
| 1772 | * wrong. |
| 1773 | */ |
| 1774 | if( ssl->in_left != 0 ) |
| 1775 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1776 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 1777 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1778 | } |
| 1779 | |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1780 | /* |
| 1781 | * Don't even try to read if time's out already. |
| 1782 | * This avoids by-passing the timer when repeatedly receiving messages |
| 1783 | * that will end up being dropped. |
| 1784 | */ |
Hanno Becker | 7876d12 | 2020-02-05 10:39:31 +0000 | [diff] [blame] | 1785 | if( mbedtls_ssl_check_timer( ssl ) != 0 ) |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 1786 | { |
| 1787 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 1788 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 1789 | } |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 1790 | else |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 1791 | { |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 1792 | len = in_buf_len - ( ssl->in_hdr - ssl->in_buf ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1793 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1794 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1795 | timeout = ssl->handshake->retransmit_timeout; |
| 1796 | else |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1797 | timeout = ssl->conf->read_timeout; |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1798 | |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 1799 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %lu ms", (unsigned long) timeout ) ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1800 | |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 1801 | if( ssl->f_recv_timeout != NULL ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1802 | ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len, |
| 1803 | timeout ); |
| 1804 | else |
| 1805 | ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len ); |
| 1806 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1807 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1808 | |
| 1809 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1810 | return( MBEDTLS_ERR_SSL_CONN_EOF ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1811 | } |
| 1812 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 1813 | if( ret == MBEDTLS_ERR_SSL_TIMEOUT ) |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 1814 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1815 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) ); |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 1816 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 1817 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1818 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 1819 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 1820 | if( ssl_double_retransmit_timeout( ssl ) != 0 ) |
| 1821 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1822 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 1823 | return( MBEDTLS_ERR_SSL_TIMEOUT ); |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 1824 | } |
| 1825 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1826 | if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 1827 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1828 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret ); |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 1829 | return( ret ); |
| 1830 | } |
| 1831 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 1832 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | 0ac247f | 2014-09-30 22:21:31 +0200 | [diff] [blame] | 1833 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1834 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 1835 | else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1836 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 1837 | { |
Hanno Becker | 786300f | 2020-02-05 10:46:40 +0000 | [diff] [blame] | 1838 | if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 1839 | { |
Hanno Becker | 786300f | 2020-02-05 10:46:40 +0000 | [diff] [blame] | 1840 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request", |
| 1841 | ret ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 1842 | return( ret ); |
| 1843 | } |
| 1844 | |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 1845 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 1846 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1847 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 1848 | } |
| 1849 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1850 | if( ret < 0 ) |
| 1851 | return( ret ); |
| 1852 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1853 | ssl->in_left = ret; |
| 1854 | } |
| 1855 | else |
| 1856 | #endif |
| 1857 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1858 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %" MBEDTLS_PRINTF_SIZET |
| 1859 | ", nb_want: %" MBEDTLS_PRINTF_SIZET, |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 1860 | ssl->in_left, nb_want ) ); |
| 1861 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1862 | while( ssl->in_left < nb_want ) |
| 1863 | { |
| 1864 | len = nb_want - ssl->in_left; |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1865 | |
Hanno Becker | 7876d12 | 2020-02-05 10:39:31 +0000 | [diff] [blame] | 1866 | if( mbedtls_ssl_check_timer( ssl ) != 0 ) |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 1867 | ret = MBEDTLS_ERR_SSL_TIMEOUT; |
| 1868 | else |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 1869 | { |
| 1870 | if( ssl->f_recv_timeout != NULL ) |
| 1871 | { |
| 1872 | ret = ssl->f_recv_timeout( ssl->p_bio, |
| 1873 | ssl->in_hdr + ssl->in_left, len, |
| 1874 | ssl->conf->read_timeout ); |
| 1875 | } |
| 1876 | else |
| 1877 | { |
| 1878 | ret = ssl->f_recv( ssl->p_bio, |
| 1879 | ssl->in_hdr + ssl->in_left, len ); |
| 1880 | } |
| 1881 | } |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1882 | |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1883 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %" MBEDTLS_PRINTF_SIZET |
| 1884 | ", nb_want: %" MBEDTLS_PRINTF_SIZET, |
Manuel Pégourié-Gonnard | 0761733 | 2015-06-24 23:00:03 +0200 | [diff] [blame] | 1885 | ssl->in_left, nb_want ) ); |
| 1886 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1887 | |
| 1888 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1889 | return( MBEDTLS_ERR_SSL_CONN_EOF ); |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1890 | |
| 1891 | if( ret < 0 ) |
| 1892 | return( ret ); |
| 1893 | |
makise-homura | af9513b | 2020-08-24 18:26:27 +0300 | [diff] [blame] | 1894 | if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) ) |
mohammad1603 | 5bd15cb | 2018-02-28 04:30:59 -0800 | [diff] [blame] | 1895 | { |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 1896 | MBEDTLS_SSL_DEBUG_MSG( 1, |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1897 | ( "f_recv returned %d bytes but only %" MBEDTLS_PRINTF_SIZET " were requested", |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 1898 | ret, len ) ); |
mohammad1603 | 5bd15cb | 2018-02-28 04:30:59 -0800 | [diff] [blame] | 1899 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1900 | } |
| 1901 | |
Manuel Pégourié-Gonnard | fe98ace | 2014-03-24 13:13:01 +0100 | [diff] [blame] | 1902 | ssl->in_left += ret; |
| 1903 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1904 | } |
| 1905 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1906 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1907 | |
| 1908 | return( 0 ); |
| 1909 | } |
| 1910 | |
| 1911 | /* |
| 1912 | * Flush any data not yet written |
| 1913 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1914 | int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1915 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 1916 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 1917 | unsigned char *buf; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1918 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1919 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1920 | |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 1921 | if( ssl->f_send == NULL ) |
| 1922 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1923 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() " |
Manuel Pégourié-Gonnard | 1b511f9 | 2015-05-06 15:54:23 +0100 | [diff] [blame] | 1924 | "or mbedtls_ssl_set_bio()" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1925 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 1926 | } |
| 1927 | |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 1928 | /* Avoid incrementing counter if data is flushed */ |
| 1929 | if( ssl->out_left == 0 ) |
| 1930 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1931 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) ); |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 1932 | return( 0 ); |
| 1933 | } |
| 1934 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1935 | while( ssl->out_left > 0 ) |
| 1936 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1937 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %" MBEDTLS_PRINTF_SIZET |
| 1938 | ", out_left: %" MBEDTLS_PRINTF_SIZET, |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 1939 | mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1940 | |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 1941 | buf = ssl->out_hdr - ssl->out_left; |
Manuel Pégourié-Gonnard | e6bdc44 | 2014-09-17 11:34:57 +0200 | [diff] [blame] | 1942 | ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left ); |
Paul Bakker | 186751d | 2012-05-08 13:16:14 +0000 | [diff] [blame] | 1943 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1944 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1945 | |
| 1946 | if( ret <= 0 ) |
| 1947 | return( ret ); |
| 1948 | |
makise-homura | af9513b | 2020-08-24 18:26:27 +0300 | [diff] [blame] | 1949 | if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > (int)SIZE_MAX ) ) |
mohammad1603 | 4bbaeb4 | 2018-02-22 04:29:04 -0800 | [diff] [blame] | 1950 | { |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 1951 | MBEDTLS_SSL_DEBUG_MSG( 1, |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1952 | ( "f_send returned %d bytes but only %" MBEDTLS_PRINTF_SIZET " bytes were sent", |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 1953 | ret, ssl->out_left ) ); |
mohammad1603 | 4bbaeb4 | 2018-02-22 04:29:04 -0800 | [diff] [blame] | 1954 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 1955 | } |
| 1956 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1957 | ssl->out_left -= ret; |
| 1958 | } |
| 1959 | |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 1960 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 1961 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 1962 | { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 1963 | ssl->out_hdr = ssl->out_buf; |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 1964 | } |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 1965 | else |
| 1966 | #endif |
| 1967 | { |
| 1968 | ssl->out_hdr = ssl->out_buf + 8; |
| 1969 | } |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 1970 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out ); |
Manuel Pégourié-Gonnard | 0619348 | 2014-02-14 08:39:32 +0100 | [diff] [blame] | 1971 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1972 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1973 | |
| 1974 | return( 0 ); |
| 1975 | } |
| 1976 | |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 1977 | /* |
| 1978 | * Functions to handle the DTLS retransmission state machine |
| 1979 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1980 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 1981 | /* |
| 1982 | * Append current handshake message to current outgoing flight |
| 1983 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1984 | static int ssl_flight_append( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 1985 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1986 | mbedtls_ssl_flight_item *msg; |
Hanno Becker | 3b23590 | 2018-08-06 09:54:53 +0100 | [diff] [blame] | 1987 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) ); |
| 1988 | MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight", |
| 1989 | ssl->out_msg, ssl->out_msglen ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 1990 | |
| 1991 | /* Allocate space for current message */ |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 1992 | if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 1993 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 1994 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %" MBEDTLS_PRINTF_SIZET " bytes failed", |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1995 | sizeof( mbedtls_ssl_flight_item ) ) ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 1996 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 1997 | } |
| 1998 | |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 1999 | if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2000 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 2001 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %" MBEDTLS_PRINTF_SIZET " bytes failed", |
| 2002 | ssl->out_msglen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2003 | mbedtls_free( msg ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 2004 | return( MBEDTLS_ERR_SSL_ALLOC_FAILED ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2005 | } |
| 2006 | |
| 2007 | /* Copy current handshake message with headers */ |
| 2008 | memcpy( msg->p, ssl->out_msg, ssl->out_msglen ); |
| 2009 | msg->len = ssl->out_msglen; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2010 | msg->type = ssl->out_msgtype; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2011 | msg->next = NULL; |
| 2012 | |
| 2013 | /* Append to the current flight */ |
| 2014 | if( ssl->handshake->flight == NULL ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2015 | ssl->handshake->flight = msg; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2016 | else |
| 2017 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2018 | mbedtls_ssl_flight_item *cur = ssl->handshake->flight; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2019 | while( cur->next != NULL ) |
| 2020 | cur = cur->next; |
| 2021 | cur->next = msg; |
| 2022 | } |
| 2023 | |
Hanno Becker | 3b23590 | 2018-08-06 09:54:53 +0100 | [diff] [blame] | 2024 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2025 | return( 0 ); |
| 2026 | } |
| 2027 | |
| 2028 | /* |
| 2029 | * Free the current flight of handshake messages |
| 2030 | */ |
Hanno Becker | 533ab5f | 2020-02-05 10:49:13 +0000 | [diff] [blame] | 2031 | void mbedtls_ssl_flight_free( mbedtls_ssl_flight_item *flight ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2032 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2033 | mbedtls_ssl_flight_item *cur = flight; |
| 2034 | mbedtls_ssl_flight_item *next; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2035 | |
| 2036 | while( cur != NULL ) |
| 2037 | { |
| 2038 | next = cur->next; |
| 2039 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2040 | mbedtls_free( cur->p ); |
| 2041 | mbedtls_free( cur ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2042 | |
| 2043 | cur = next; |
| 2044 | } |
| 2045 | } |
| 2046 | |
| 2047 | /* |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2048 | * Swap transform_out and out_ctr with the alternative ones |
| 2049 | */ |
Manuel Pégourié-Gonnard | e07bc20 | 2020-02-26 09:53:42 +0100 | [diff] [blame] | 2050 | static int ssl_swap_epochs( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2051 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2052 | mbedtls_ssl_transform *tmp_transform; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2053 | unsigned char tmp_out_ctr[8]; |
| 2054 | |
| 2055 | if( ssl->transform_out == ssl->handshake->alt_transform_out ) |
| 2056 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2057 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) ); |
Manuel Pégourié-Gonnard | e07bc20 | 2020-02-26 09:53:42 +0100 | [diff] [blame] | 2058 | return( 0 ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2059 | } |
| 2060 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2061 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2062 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2063 | /* Swap transforms */ |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2064 | tmp_transform = ssl->transform_out; |
| 2065 | ssl->transform_out = ssl->handshake->alt_transform_out; |
| 2066 | ssl->handshake->alt_transform_out = tmp_transform; |
| 2067 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2068 | /* Swap epoch + sequence_number */ |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 2069 | memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 ); |
| 2070 | memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2071 | memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2072 | |
| 2073 | /* Adjust to the newly activated transform */ |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 2074 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2075 | |
Manuel Pégourié-Gonnard | e07bc20 | 2020-02-26 09:53:42 +0100 | [diff] [blame] | 2076 | return( 0 ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2077 | } |
| 2078 | |
| 2079 | /* |
| 2080 | * Retransmit the current flight of messages. |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2081 | */ |
| 2082 | int mbedtls_ssl_resend( mbedtls_ssl_context *ssl ) |
| 2083 | { |
| 2084 | int ret = 0; |
| 2085 | |
| 2086 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) ); |
| 2087 | |
| 2088 | ret = mbedtls_ssl_flight_transmit( ssl ); |
| 2089 | |
| 2090 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) ); |
| 2091 | |
| 2092 | return( ret ); |
| 2093 | } |
| 2094 | |
| 2095 | /* |
| 2096 | * Transmit or retransmit the current flight of messages. |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2097 | * |
| 2098 | * Need to remember the current message in case flush_output returns |
| 2099 | * 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] | 2100 | * 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] | 2101 | */ |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2102 | int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2103 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2104 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2105 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2106 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2107 | if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2108 | { |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 2109 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2110 | |
| 2111 | ssl->handshake->cur_msg = ssl->handshake->flight; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2112 | ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12; |
Manuel Pégourié-Gonnard | e07bc20 | 2020-02-26 09:53:42 +0100 | [diff] [blame] | 2113 | ret = ssl_swap_epochs( ssl ); |
| 2114 | if( ret != 0 ) |
| 2115 | return( ret ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2116 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2117 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2118 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2119 | |
| 2120 | while( ssl->handshake->cur_msg != NULL ) |
| 2121 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2122 | size_t max_frag_len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2123 | const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2124 | |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 2125 | int const is_finished = |
| 2126 | ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2127 | cur->p[0] == MBEDTLS_SSL_HS_FINISHED ); |
| 2128 | |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 2129 | uint8_t const force_flush = ssl->disable_datagram_packing == 1 ? |
| 2130 | SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH; |
| 2131 | |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2132 | /* Swap epochs before sending Finished: we can't do it after |
| 2133 | * sending ChangeCipherSpec, in case write returns WANT_READ. |
| 2134 | * Must be done before copying, may change out_msg pointer */ |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 2135 | if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) ) |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2136 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2137 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) ); |
Manuel Pégourié-Gonnard | e07bc20 | 2020-02-26 09:53:42 +0100 | [diff] [blame] | 2138 | ret = ssl_swap_epochs( ssl ); |
| 2139 | if( ret != 0 ) |
| 2140 | return( ret ); |
Manuel Pégourié-Gonnard | c715aed | 2014-09-19 21:39:13 +0200 | [diff] [blame] | 2141 | } |
| 2142 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2143 | ret = ssl_get_remaining_payload_in_datagram( ssl ); |
| 2144 | if( ret < 0 ) |
| 2145 | return( ret ); |
| 2146 | max_frag_len = (size_t) ret; |
| 2147 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2148 | /* CCS is copied as is, while HS messages may need fragmentation */ |
| 2149 | if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
| 2150 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2151 | if( max_frag_len == 0 ) |
| 2152 | { |
| 2153 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 2154 | return( ret ); |
| 2155 | |
| 2156 | continue; |
| 2157 | } |
| 2158 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2159 | memcpy( ssl->out_msg, cur->p, cur->len ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2160 | ssl->out_msglen = cur->len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2161 | ssl->out_msgtype = cur->type; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2162 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2163 | /* Update position inside current message */ |
| 2164 | ssl->handshake->cur_msg_p += cur->len; |
| 2165 | } |
| 2166 | else |
| 2167 | { |
| 2168 | const unsigned char * const p = ssl->handshake->cur_msg_p; |
| 2169 | const size_t hs_len = cur->len - 12; |
| 2170 | const size_t frag_off = p - ( cur->p + 12 ); |
| 2171 | const size_t rem_len = hs_len - frag_off; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2172 | size_t cur_hs_frag_len, max_hs_frag_len; |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2173 | |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 2174 | if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) ) |
Manuel Pégourié-Gonnard | a1071a5 | 2018-08-20 11:56:14 +0200 | [diff] [blame] | 2175 | { |
Hanno Becker | e1dcb03 | 2018-08-17 16:47:58 +0100 | [diff] [blame] | 2176 | if( is_finished ) |
Manuel Pégourié-Gonnard | e07bc20 | 2020-02-26 09:53:42 +0100 | [diff] [blame] | 2177 | { |
| 2178 | ret = ssl_swap_epochs( ssl ); |
| 2179 | if( ret != 0 ) |
| 2180 | return( ret ); |
| 2181 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2182 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2183 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 2184 | return( ret ); |
| 2185 | |
| 2186 | continue; |
| 2187 | } |
| 2188 | max_hs_frag_len = max_frag_len - 12; |
| 2189 | |
| 2190 | cur_hs_frag_len = rem_len > max_hs_frag_len ? |
| 2191 | max_hs_frag_len : rem_len; |
| 2192 | |
| 2193 | if( frag_off == 0 && cur_hs_frag_len != hs_len ) |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 2194 | { |
| 2195 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)", |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2196 | (unsigned) cur_hs_frag_len, |
| 2197 | (unsigned) max_hs_frag_len ) ); |
Manuel Pégourié-Gonnard | 19c62f9 | 2018-08-16 10:50:39 +0200 | [diff] [blame] | 2198 | } |
Manuel Pégourié-Gonnard | b747c6c | 2018-08-12 13:28:53 +0200 | [diff] [blame] | 2199 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2200 | /* Messages are stored with handshake headers as if not fragmented, |
| 2201 | * copy beginning of headers then fill fragmentation fields. |
| 2202 | * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */ |
| 2203 | memcpy( ssl->out_msg, cur->p, 6 ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2204 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2205 | ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff ); |
| 2206 | ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff ); |
| 2207 | ssl->out_msg[8] = ( ( frag_off ) & 0xff ); |
| 2208 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2209 | ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff ); |
| 2210 | ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff ); |
| 2211 | ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff ); |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2212 | |
| 2213 | MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 ); |
| 2214 | |
Hanno Becker | 3f7b973 | 2018-08-28 09:53:25 +0100 | [diff] [blame] | 2215 | /* Copy the handshake message content and set records fields */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2216 | memcpy( ssl->out_msg + 12, p, cur_hs_frag_len ); |
| 2217 | ssl->out_msglen = cur_hs_frag_len + 12; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2218 | ssl->out_msgtype = cur->type; |
| 2219 | |
| 2220 | /* Update position inside current message */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2221 | ssl->handshake->cur_msg_p += cur_hs_frag_len; |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2222 | } |
| 2223 | |
| 2224 | /* If done with the current message move to the next one if any */ |
| 2225 | if( ssl->handshake->cur_msg_p >= cur->p + cur->len ) |
| 2226 | { |
| 2227 | if( cur->next != NULL ) |
| 2228 | { |
| 2229 | ssl->handshake->cur_msg = cur->next; |
| 2230 | ssl->handshake->cur_msg_p = cur->next->p + 12; |
| 2231 | } |
| 2232 | else |
| 2233 | { |
| 2234 | ssl->handshake->cur_msg = NULL; |
| 2235 | ssl->handshake->cur_msg_p = NULL; |
| 2236 | } |
| 2237 | } |
| 2238 | |
| 2239 | /* Actually send the message out */ |
Hanno Becker | 04da189 | 2018-08-14 13:22:10 +0100 | [diff] [blame] | 2240 | if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2241 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2242 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2243 | return( ret ); |
| 2244 | } |
| 2245 | } |
| 2246 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2247 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
| 2248 | return( ret ); |
| 2249 | |
Manuel Pégourié-Gonnard | 28f4bea | 2017-09-13 14:00:05 +0200 | [diff] [blame] | 2250 | /* Update state and set timer */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2251 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 2252 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 23b7b70 | 2014-09-25 13:50:12 +0200 | [diff] [blame] | 2253 | else |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2254 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2255 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 2256 | mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout ); |
Manuel Pégourié-Gonnard | 4e2f245 | 2014-10-02 16:51:56 +0200 | [diff] [blame] | 2257 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2258 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2259 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2260 | |
| 2261 | return( 0 ); |
| 2262 | } |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2263 | |
| 2264 | /* |
| 2265 | * To be called when the last message of an incoming flight is received. |
| 2266 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2267 | void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2268 | { |
| 2269 | /* We won't need to resend that one any more */ |
Hanno Becker | 533ab5f | 2020-02-05 10:49:13 +0000 | [diff] [blame] | 2270 | mbedtls_ssl_flight_free( ssl->handshake->flight ); |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2271 | ssl->handshake->flight = NULL; |
| 2272 | ssl->handshake->cur_msg = NULL; |
| 2273 | |
| 2274 | /* The next incoming flight will start with this msg_seq */ |
| 2275 | ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq; |
| 2276 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 2277 | /* We don't want to remember CCS's across flight boundaries. */ |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 2278 | ssl->handshake->buffering.seen_ccs = 0; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 2279 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 2280 | /* Clear future message buffering structure. */ |
Hanno Becker | 533ab5f | 2020-02-05 10:49:13 +0000 | [diff] [blame] | 2281 | mbedtls_ssl_buffering_free( ssl ); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 2282 | |
Manuel Pégourié-Gonnard | 6c1fa3a | 2014-10-01 16:58:16 +0200 | [diff] [blame] | 2283 | /* Cancel timer */ |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 2284 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2285 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2286 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2287 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED ) |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2288 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2289 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2290 | } |
| 2291 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2292 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING; |
Manuel Pégourié-Gonnard | 5d8ba53 | 2014-09-19 15:09:21 +0200 | [diff] [blame] | 2293 | } |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2294 | |
| 2295 | /* |
| 2296 | * To be called when the last message of an outgoing flight is send. |
| 2297 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2298 | void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2299 | { |
Manuel Pégourié-Gonnard | 6c1fa3a | 2014-10-01 16:58:16 +0200 | [diff] [blame] | 2300 | ssl_reset_retransmit_timeout( ssl ); |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 2301 | mbedtls_ssl_set_timer( ssl, ssl->handshake->retransmit_timeout ); |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2302 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2303 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2304 | ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED ) |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2305 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2306 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED; |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2307 | } |
| 2308 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2309 | ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING; |
Manuel Pégourié-Gonnard | 7de3c9e | 2014-09-29 15:29:48 +0200 | [diff] [blame] | 2310 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2311 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2312 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2313 | /* |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2314 | * Handshake layer functions |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2315 | */ |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2316 | |
| 2317 | /* |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2318 | * Write (DTLS: or queue) current handshake (including CCS) message. |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2319 | * |
| 2320 | * - fill in handshake headers |
| 2321 | * - update handshake checksum |
| 2322 | * - DTLS: save message for resending |
| 2323 | * - then pass to the record layer |
| 2324 | * |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2325 | * DTLS: except for HelloRequest, messages are only queued, and will only be |
| 2326 | * actually sent when calling flight_transmit() or resend(). |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2327 | * |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2328 | * Inputs: |
| 2329 | * - ssl->out_msglen: 4 + actual handshake message len |
| 2330 | * (4 is the size of handshake headers for TLS) |
| 2331 | * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc) |
| 2332 | * - ssl->out_msg + 4: the handshake message body |
| 2333 | * |
Manuel Pégourié-Gonnard | 065a2a3 | 2018-08-20 11:09:26 +0200 | [diff] [blame] | 2334 | * 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] | 2335 | * - ssl->out_msglen: the length of the record contents |
| 2336 | * (including handshake headers but excluding record headers) |
| 2337 | * - ssl->out_msg: the record contents (handshake headers + content) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2338 | */ |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2339 | int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2340 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2341 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2342 | const size_t hs_len = ssl->out_msglen - 4; |
| 2343 | const unsigned char hs_type = ssl->out_msg[0]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2344 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2345 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) ); |
| 2346 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2347 | /* |
| 2348 | * Sanity checks |
| 2349 | */ |
Hanno Becker | c83d2b3 | 2018-08-22 16:05:47 +0100 | [diff] [blame] | 2350 | if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE && |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2351 | ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
| 2352 | { |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 2353 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2354 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2355 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2356 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 2357 | /* Whenever we send anything different from a |
| 2358 | * HelloRequest we should be in a handshake - double check. */ |
| 2359 | if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2360 | hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) && |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2361 | ssl->handshake == NULL ) |
| 2362 | { |
| 2363 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2364 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2365 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2366 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2367 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2368 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2369 | ssl->handshake != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2370 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2371 | { |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2372 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2373 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2374 | } |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2375 | #endif |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2376 | |
Hanno Becker | b50a253 | 2018-08-06 11:52:54 +0100 | [diff] [blame] | 2377 | /* Double-check that we did not exceed the bounds |
| 2378 | * of the outgoing record buffer. |
| 2379 | * This should never fail as the various message |
| 2380 | * writing functions must obey the bounds of the |
| 2381 | * outgoing record buffer, but better be safe. |
| 2382 | * |
| 2383 | * Note: We deliberately do not check for the MTU or MFL here. |
| 2384 | */ |
| 2385 | if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN ) |
| 2386 | { |
| 2387 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: " |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 2388 | "size %" MBEDTLS_PRINTF_SIZET |
| 2389 | ", maximum %" MBEDTLS_PRINTF_SIZET, |
Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 2390 | ssl->out_msglen, |
| 2391 | (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN ) ); |
Hanno Becker | b50a253 | 2018-08-06 11:52:54 +0100 | [diff] [blame] | 2392 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2393 | } |
| 2394 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2395 | /* |
| 2396 | * Fill handshake headers |
| 2397 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2398 | if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2399 | { |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2400 | ssl->out_msg[1] = (unsigned char)( hs_len >> 16 ); |
| 2401 | ssl->out_msg[2] = (unsigned char)( hs_len >> 8 ); |
| 2402 | ssl->out_msg[3] = (unsigned char)( hs_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2403 | |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 2404 | /* |
| 2405 | * DTLS has additional fields in the Handshake layer, |
| 2406 | * between the length field and the actual payload: |
| 2407 | * uint16 message_seq; |
| 2408 | * uint24 fragment_offset; |
| 2409 | * uint24 fragment_length; |
| 2410 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2411 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2412 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 2413 | { |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 2414 | /* Make room for the additional DTLS fields */ |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 2415 | if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 ) |
Hanno Becker | 9648f8b | 2017-09-18 10:55:54 +0100 | [diff] [blame] | 2416 | { |
| 2417 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: " |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 2418 | "size %" MBEDTLS_PRINTF_SIZET ", maximum %" MBEDTLS_PRINTF_SIZET, |
Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 2419 | hs_len, |
| 2420 | (size_t) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) ); |
Hanno Becker | 9648f8b | 2017-09-18 10:55:54 +0100 | [diff] [blame] | 2421 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 2422 | } |
| 2423 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2424 | 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] | 2425 | ssl->out_msglen += 8; |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 2426 | |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 2427 | /* Write message_seq and update it, except for HelloRequest */ |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2428 | if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 2429 | { |
Manuel Pégourié-Gonnard | d9ba0d9 | 2014-09-02 18:30:26 +0200 | [diff] [blame] | 2430 | ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF; |
| 2431 | ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF; |
| 2432 | ++( ssl->handshake->out_msg_seq ); |
Manuel Pégourié-Gonnard | c392b24 | 2014-08-19 17:53:11 +0200 | [diff] [blame] | 2433 | } |
| 2434 | else |
| 2435 | { |
| 2436 | ssl->out_msg[4] = 0; |
| 2437 | ssl->out_msg[5] = 0; |
| 2438 | } |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 2439 | |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2440 | /* Handshake hashes are computed without fragmentation, |
| 2441 | * so set frag_offset = 0 and frag_len = hs_len for now */ |
Manuel Pégourié-Gonnard | e89bcf0 | 2014-02-18 18:50:02 +0100 | [diff] [blame] | 2442 | memset( ssl->out_msg + 6, 0x00, 3 ); |
| 2443 | memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 ); |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 2444 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2445 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 2446 | |
Hanno Becker | 0207e53 | 2018-08-28 10:28:28 +0100 | [diff] [blame] | 2447 | /* Update running hashes of handshake messages seen */ |
Manuel Pégourié-Gonnard | 9c3a8ca | 2017-09-13 09:54:27 +0200 | [diff] [blame] | 2448 | if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) |
| 2449 | ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2450 | } |
| 2451 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2452 | /* Either send now, or just save to be sent (and resent) later */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2453 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2454 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 2455 | ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 2456 | hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) ) |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2457 | { |
| 2458 | if( ( ret = ssl_flight_append( ssl ) ) != 0 ) |
| 2459 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2460 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2461 | return( ret ); |
| 2462 | } |
| 2463 | } |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2464 | else |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2465 | #endif |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2466 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2467 | if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 ) |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2468 | { |
| 2469 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret ); |
| 2470 | return( ret ); |
| 2471 | } |
| 2472 | } |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2473 | |
| 2474 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) ); |
| 2475 | |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 2476 | return( 0 ); |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2477 | } |
| 2478 | |
| 2479 | /* |
| 2480 | * Record layer functions |
| 2481 | */ |
| 2482 | |
| 2483 | /* |
| 2484 | * Write current record. |
| 2485 | * |
| 2486 | * Uses: |
| 2487 | * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS) |
| 2488 | * - ssl->out_msglen: length of the record content (excl headers) |
| 2489 | * - ssl->out_msg: record content |
| 2490 | */ |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2491 | 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] | 2492 | { |
| 2493 | int ret, done = 0; |
| 2494 | size_t len = ssl->out_msglen; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2495 | uint8_t flush = force_flush; |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 2496 | |
| 2497 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) ); |
Manuel Pégourié-Gonnard | ffa67be | 2014-09-19 11:18:57 +0200 | [diff] [blame] | 2498 | |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2499 | if( !done ) |
| 2500 | { |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2501 | unsigned i; |
| 2502 | size_t protected_record_size; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 2503 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 2504 | size_t out_buf_len = ssl->out_buf_len; |
| 2505 | #else |
| 2506 | size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN; |
| 2507 | #endif |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 2508 | /* Skip writing the record content type to after the encryption, |
| 2509 | * as it may change when using the CID extension. */ |
| 2510 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2511 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2512 | ssl->conf->transport, ssl->out_hdr + 1 ); |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 2513 | |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 2514 | memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 ); |
Manuel Pégourié-Gonnard | 507e1e4 | 2014-02-13 11:17:34 +0100 | [diff] [blame] | 2515 | ssl->out_len[0] = (unsigned char)( len >> 8 ); |
| 2516 | ssl->out_len[1] = (unsigned char)( len ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2517 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 2518 | if( ssl->transform_out != NULL ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2519 | { |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2520 | mbedtls_record rec; |
| 2521 | |
| 2522 | rec.buf = ssl->out_iv; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 2523 | rec.buf_len = out_buf_len - ( ssl->out_iv - ssl->out_buf ); |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2524 | rec.data_len = ssl->out_msglen; |
| 2525 | rec.data_offset = ssl->out_msg - rec.buf; |
| 2526 | |
| 2527 | memcpy( &rec.ctr[0], ssl->out_ctr, 8 ); |
| 2528 | mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver, |
| 2529 | ssl->conf->transport, rec.ver ); |
| 2530 | rec.type = ssl->out_msgtype; |
| 2531 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2532 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 2533 | /* The CID is set by mbedtls_ssl_encrypt_buf(). */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2534 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2535 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | cab87e6 | 2019-04-29 13:52:53 +0100 | [diff] [blame] | 2536 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 2537 | if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec, |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2538 | ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 ) |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2539 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2540 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2541 | return( ret ); |
| 2542 | } |
| 2543 | |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2544 | if( rec.data_offset != 0 ) |
| 2545 | { |
| 2546 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 2547 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2548 | } |
| 2549 | |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 2550 | /* Update the record content type and CID. */ |
| 2551 | ssl->out_msgtype = rec.type; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2552 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID ) |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 2553 | memcpy( ssl->out_cid, rec.cid, rec.cid_len ); |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 2554 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 78f839d | 2019-03-14 12:56:23 +0000 | [diff] [blame] | 2555 | ssl->out_msglen = len = rec.data_len; |
Hanno Becker | 9eddaeb | 2017-12-27 21:37:21 +0000 | [diff] [blame] | 2556 | ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 ); |
| 2557 | ssl->out_len[1] = (unsigned char)( rec.data_len ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2558 | } |
| 2559 | |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 2560 | protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2561 | |
| 2562 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 2563 | /* In case of DTLS, double-check that we don't exceed |
| 2564 | * the remaining space in the datagram. */ |
| 2565 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 2566 | { |
Hanno Becker | 554b0af | 2018-08-22 20:33:41 +0100 | [diff] [blame] | 2567 | ret = ssl_get_remaining_space_in_datagram( ssl ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2568 | if( ret < 0 ) |
| 2569 | return( ret ); |
| 2570 | |
| 2571 | if( protected_record_size > (size_t) ret ) |
| 2572 | { |
| 2573 | /* Should never happen */ |
| 2574 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 2575 | } |
| 2576 | } |
| 2577 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2578 | |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 2579 | /* Now write the potentially updated record content type. */ |
| 2580 | ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype; |
| 2581 | |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 2582 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %u, " |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 2583 | "version = [%u:%u], msglen = %" MBEDTLS_PRINTF_SIZET, |
Hanno Becker | ecbdf1c | 2018-08-28 09:53:54 +0100 | [diff] [blame] | 2584 | ssl->out_hdr[0], ssl->out_hdr[1], |
| 2585 | ssl->out_hdr[2], len ) ); |
Paul Bakker | 05ef835 | 2012-05-08 09:17:57 +0000 | [diff] [blame] | 2586 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2587 | MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network", |
Hanno Becker | ecbdf1c | 2018-08-28 09:53:54 +0100 | [diff] [blame] | 2588 | ssl->out_hdr, protected_record_size ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2589 | |
| 2590 | ssl->out_left += protected_record_size; |
| 2591 | ssl->out_hdr += protected_record_size; |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 2592 | mbedtls_ssl_update_out_pointers( ssl, ssl->transform_out ); |
Hanno Becker | 2b1e354 | 2018-08-06 11:19:13 +0100 | [diff] [blame] | 2593 | |
Hanno Becker | dd77229 | 2020-02-05 10:38:31 +0000 | [diff] [blame] | 2594 | for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- ) |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 2595 | if( ++ssl->cur_out_ctr[i - 1] != 0 ) |
| 2596 | break; |
| 2597 | |
| 2598 | /* The loop goes to its end iff the counter is wrapping */ |
Hanno Becker | dd77229 | 2020-02-05 10:38:31 +0000 | [diff] [blame] | 2599 | if( i == mbedtls_ssl_ep_len( ssl ) ) |
Hanno Becker | 0448462 | 2018-08-06 09:49:38 +0100 | [diff] [blame] | 2600 | { |
| 2601 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) ); |
| 2602 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 2603 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2604 | } |
| 2605 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2606 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 47db877 | 2018-08-21 13:32:13 +0100 | [diff] [blame] | 2607 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 2608 | flush == SSL_DONT_FORCE_FLUSH ) |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2609 | { |
Hanno Becker | 1f5a15d | 2018-08-21 13:31:31 +0100 | [diff] [blame] | 2610 | size_t remaining; |
| 2611 | ret = ssl_get_remaining_payload_in_datagram( ssl ); |
| 2612 | if( ret < 0 ) |
| 2613 | { |
| 2614 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram", |
| 2615 | ret ); |
| 2616 | return( ret ); |
| 2617 | } |
| 2618 | |
| 2619 | remaining = (size_t) ret; |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2620 | if( remaining == 0 ) |
Hanno Becker | f0da667 | 2018-08-28 09:55:10 +0100 | [diff] [blame] | 2621 | { |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2622 | flush = SSL_FORCE_FLUSH; |
Hanno Becker | f0da667 | 2018-08-28 09:55:10 +0100 | [diff] [blame] | 2623 | } |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2624 | else |
| 2625 | { |
Hanno Becker | 513815a | 2018-08-20 11:56:09 +0100 | [diff] [blame] | 2626 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) ); |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 2627 | } |
| 2628 | } |
| 2629 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 2630 | |
| 2631 | if( ( flush == SSL_FORCE_FLUSH ) && |
| 2632 | ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2633 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2634 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2635 | return( ret ); |
| 2636 | } |
| 2637 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2638 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2639 | |
| 2640 | return( 0 ); |
| 2641 | } |
| 2642 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2643 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 2644 | |
| 2645 | static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl ) |
| 2646 | { |
| 2647 | if( ssl->in_msglen < ssl->in_hslen || |
| 2648 | memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 || |
| 2649 | memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 ) |
| 2650 | { |
| 2651 | return( 1 ); |
| 2652 | } |
| 2653 | return( 0 ); |
| 2654 | } |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2655 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 2656 | 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] | 2657 | { |
| 2658 | return( ( ssl->in_msg[9] << 16 ) | |
| 2659 | ( ssl->in_msg[10] << 8 ) | |
| 2660 | ssl->in_msg[11] ); |
| 2661 | } |
| 2662 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 2663 | 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] | 2664 | { |
| 2665 | return( ( ssl->in_msg[6] << 16 ) | |
| 2666 | ( ssl->in_msg[7] << 8 ) | |
| 2667 | ssl->in_msg[8] ); |
| 2668 | } |
| 2669 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 2670 | static int ssl_check_hs_header( mbedtls_ssl_context const *ssl ) |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2671 | { |
| 2672 | uint32_t msg_len, frag_off, frag_len; |
| 2673 | |
| 2674 | msg_len = ssl_get_hs_total_len( ssl ); |
| 2675 | frag_off = ssl_get_hs_frag_off( ssl ); |
| 2676 | frag_len = ssl_get_hs_frag_len( ssl ); |
| 2677 | |
| 2678 | if( frag_off > msg_len ) |
| 2679 | return( -1 ); |
| 2680 | |
| 2681 | if( frag_len > msg_len - frag_off ) |
| 2682 | return( -1 ); |
| 2683 | |
| 2684 | if( frag_len + 12 > ssl->in_msglen ) |
| 2685 | return( -1 ); |
| 2686 | |
| 2687 | return( 0 ); |
| 2688 | } |
| 2689 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2690 | /* |
| 2691 | * Mark bits in bitmask (used for DTLS HS reassembly) |
| 2692 | */ |
| 2693 | static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len ) |
| 2694 | { |
| 2695 | unsigned int start_bits, end_bits; |
| 2696 | |
| 2697 | start_bits = 8 - ( offset % 8 ); |
| 2698 | if( start_bits != 8 ) |
| 2699 | { |
| 2700 | size_t first_byte_idx = offset / 8; |
| 2701 | |
Manuel Pégourié-Gonnard | ac03052 | 2014-09-02 14:23:40 +0200 | [diff] [blame] | 2702 | /* Special case */ |
| 2703 | if( len <= start_bits ) |
| 2704 | { |
| 2705 | for( ; len != 0; len-- ) |
| 2706 | mask[first_byte_idx] |= 1 << ( start_bits - len ); |
| 2707 | |
| 2708 | /* Avoid potential issues with offset or len becoming invalid */ |
| 2709 | return; |
| 2710 | } |
| 2711 | |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2712 | offset += start_bits; /* Now offset % 8 == 0 */ |
| 2713 | len -= start_bits; |
| 2714 | |
| 2715 | for( ; start_bits != 0; start_bits-- ) |
| 2716 | mask[first_byte_idx] |= 1 << ( start_bits - 1 ); |
| 2717 | } |
| 2718 | |
| 2719 | end_bits = len % 8; |
| 2720 | if( end_bits != 0 ) |
| 2721 | { |
| 2722 | size_t last_byte_idx = ( offset + len ) / 8; |
| 2723 | |
| 2724 | len -= end_bits; /* Now len % 8 == 0 */ |
| 2725 | |
| 2726 | for( ; end_bits != 0; end_bits-- ) |
| 2727 | mask[last_byte_idx] |= 1 << ( 8 - end_bits ); |
| 2728 | } |
| 2729 | |
| 2730 | memset( mask + offset / 8, 0xFF, len / 8 ); |
| 2731 | } |
| 2732 | |
| 2733 | /* |
| 2734 | * Check that bitmask is full |
| 2735 | */ |
| 2736 | static int ssl_bitmask_check( unsigned char *mask, size_t len ) |
| 2737 | { |
| 2738 | size_t i; |
| 2739 | |
| 2740 | for( i = 0; i < len / 8; i++ ) |
| 2741 | if( mask[i] != 0xFF ) |
| 2742 | return( -1 ); |
| 2743 | |
| 2744 | for( i = 0; i < len % 8; i++ ) |
| 2745 | if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 ) |
| 2746 | return( -1 ); |
| 2747 | |
| 2748 | return( 0 ); |
| 2749 | } |
| 2750 | |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 2751 | /* msg_len does not include the handshake header */ |
Hanno Becker | 65dc885 | 2018-08-23 09:40:49 +0100 | [diff] [blame] | 2752 | static size_t ssl_get_reassembly_buffer_size( size_t msg_len, |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 2753 | unsigned add_bitmap ) |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2754 | { |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 2755 | size_t alloc_len; |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2756 | |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 2757 | alloc_len = 12; /* Handshake header */ |
| 2758 | alloc_len += msg_len; /* Content buffer */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2759 | |
Hanno Becker | d07df86 | 2018-08-16 09:14:58 +0100 | [diff] [blame] | 2760 | if( add_bitmap ) |
| 2761 | alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */ |
Manuel Pégourié-Gonnard | 502bf30 | 2014-08-20 13:12:58 +0200 | [diff] [blame] | 2762 | |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 2763 | return( alloc_len ); |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2764 | } |
Hanno Becker | 56e205e | 2018-08-16 09:06:12 +0100 | [diff] [blame] | 2765 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2766 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2767 | |
Hanno Becker | cd9dcda | 2018-08-28 17:18:56 +0100 | [diff] [blame] | 2768 | 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] | 2769 | { |
| 2770 | return( ( ssl->in_msg[1] << 16 ) | |
| 2771 | ( ssl->in_msg[2] << 8 ) | |
| 2772 | ssl->in_msg[3] ); |
| 2773 | } |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 2774 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 2775 | int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 2776 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2777 | if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) ) |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 2778 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 2779 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %" MBEDTLS_PRINTF_SIZET, |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 2780 | ssl->in_msglen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2781 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Manuel Pégourié-Gonnard | 9d1d719 | 2014-09-03 11:01:14 +0200 | [diff] [blame] | 2782 | } |
| 2783 | |
Hanno Becker | 12555c6 | 2018-08-16 12:47:53 +0100 | [diff] [blame] | 2784 | 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] | 2785 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2786 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen =" |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 2787 | " %" MBEDTLS_PRINTF_SIZET ", type = %u, hslen = %" MBEDTLS_PRINTF_SIZET, |
Manuel Pégourié-Gonnard | ce441b3 | 2014-02-18 17:40:52 +0100 | [diff] [blame] | 2788 | ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) ); |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 2789 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2790 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2791 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 2792 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2793 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 2794 | 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] | 2795 | |
Hanno Becker | 44650b7 | 2018-08-16 12:51:11 +0100 | [diff] [blame] | 2796 | if( ssl_check_hs_header( ssl ) != 0 ) |
| 2797 | { |
| 2798 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) ); |
| 2799 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 2800 | } |
| 2801 | |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 2802 | if( ssl->handshake != NULL && |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 2803 | ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && |
| 2804 | recv_msg_seq != ssl->handshake->in_msg_seq ) || |
| 2805 | ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER && |
| 2806 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) ) |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 2807 | { |
Hanno Becker | 9e1ec22 | 2018-08-15 15:54:43 +0100 | [diff] [blame] | 2808 | if( recv_msg_seq > ssl->handshake->in_msg_seq ) |
| 2809 | { |
| 2810 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)", |
| 2811 | recv_msg_seq, |
| 2812 | ssl->handshake->in_msg_seq ) ); |
| 2813 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
| 2814 | } |
| 2815 | |
Manuel Pégourié-Gonnard | fc572dd | 2014-10-09 17:56:57 +0200 | [diff] [blame] | 2816 | /* Retransmit only on last message from previous flight, to avoid |
| 2817 | * too many retransmissions. |
| 2818 | * Besides, No sane server ever retransmits HelloVerifyRequest */ |
| 2819 | if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2820 | ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST ) |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 2821 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2822 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, " |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 2823 | "message_seq = %u, start_of_flight = %u", |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 2824 | recv_msg_seq, |
| 2825 | ssl->handshake->in_flight_start_seq ) ); |
| 2826 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2827 | if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 2828 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2829 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret ); |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 2830 | return( ret ); |
| 2831 | } |
| 2832 | } |
| 2833 | else |
| 2834 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2835 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: " |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 2836 | "message_seq = %u, expected = %u", |
Manuel Pégourié-Gonnard | 6a2bdfa | 2014-09-19 21:18:23 +0200 | [diff] [blame] | 2837 | recv_msg_seq, |
| 2838 | ssl->handshake->in_msg_seq ) ); |
| 2839 | } |
| 2840 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 2841 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 2842 | } |
| 2843 | /* Wait until message completion to increment in_msg_seq */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2844 | |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 2845 | /* Message reassembly is handled alongside buffering of future |
| 2846 | * messages; the commonality is that both handshake fragments and |
Hanno Becker | 83ab41c | 2018-08-28 17:19:38 +0100 | [diff] [blame] | 2847 | * future messages cannot be forwarded immediately to the |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 2848 | * handshake logic layer. */ |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 2849 | if( ssl_hs_is_proper_fragment( ssl ) == 1 ) |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2850 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2851 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) ); |
Hanno Becker | 6d97ef5 | 2018-08-16 13:09:04 +0100 | [diff] [blame] | 2852 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2853 | } |
| 2854 | } |
| 2855 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2856 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | ed79a4b | 2014-08-20 10:43:01 +0200 | [diff] [blame] | 2857 | /* With TLS we don't handle fragmentation (for now) */ |
| 2858 | if( ssl->in_msglen < ssl->in_hslen ) |
| 2859 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2860 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) ); |
| 2861 | return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 2862 | } |
| 2863 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 2864 | return( 0 ); |
| 2865 | } |
| 2866 | |
| 2867 | void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl ) |
| 2868 | { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 2869 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 2870 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 2871 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL ) |
Manuel Pégourié-Gonnard | 14bf706 | 2015-06-23 14:07:13 +0200 | [diff] [blame] | 2872 | { |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 2873 | 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] | 2874 | } |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 2875 | |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 2876 | /* Handshake message is complete, increment counter */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2877 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2878 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 2879 | ssl->handshake != NULL ) |
| 2880 | { |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 2881 | unsigned offset; |
| 2882 | mbedtls_ssl_hs_buffer *hs_buf; |
Hanno Becker | e25e3b7 | 2018-08-16 09:30:53 +0100 | [diff] [blame] | 2883 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 2884 | /* Increment handshake sequence number */ |
| 2885 | hs->in_msg_seq++; |
| 2886 | |
| 2887 | /* |
| 2888 | * Clear up handshake buffering and reassembly structure. |
| 2889 | */ |
| 2890 | |
| 2891 | /* Free first entry */ |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 2892 | ssl_buffering_free_slot( ssl, 0 ); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 2893 | |
| 2894 | /* Shift all other entries */ |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 2895 | for( offset = 0, hs_buf = &hs->buffering.hs[0]; |
| 2896 | offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS; |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 2897 | offset++, hs_buf++ ) |
| 2898 | { |
| 2899 | *hs_buf = *(hs_buf + 1); |
| 2900 | } |
| 2901 | |
| 2902 | /* Create a fresh last entry */ |
| 2903 | memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) ); |
Manuel Pégourié-Gonnard | 1aa586e | 2014-09-03 12:54:04 +0200 | [diff] [blame] | 2904 | } |
| 2905 | #endif |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 2906 | } |
| 2907 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 2908 | /* |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 2909 | * DTLS anti-replay: RFC 6347 4.1.2.6 |
| 2910 | * |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2911 | * in_window is a field of bits numbered from 0 (lsb) to 63 (msb). |
| 2912 | * Bit n is set iff record number in_window_top - n has been seen. |
| 2913 | * |
| 2914 | * Usually, in_window_top is the last record number seen and the lsb of |
| 2915 | * in_window is set. The only exception is the initial state (record number 0 |
| 2916 | * not seen yet). |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 2917 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2918 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Hanno Becker | 7e8e6a6 | 2020-02-05 10:45:48 +0000 | [diff] [blame] | 2919 | void mbedtls_ssl_dtls_replay_reset( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 2920 | { |
| 2921 | ssl->in_window_top = 0; |
| 2922 | ssl->in_window = 0; |
| 2923 | } |
| 2924 | |
| 2925 | static inline uint64_t ssl_load_six_bytes( unsigned char *buf ) |
| 2926 | { |
| 2927 | return( ( (uint64_t) buf[0] << 40 ) | |
| 2928 | ( (uint64_t) buf[1] << 32 ) | |
| 2929 | ( (uint64_t) buf[2] << 24 ) | |
| 2930 | ( (uint64_t) buf[3] << 16 ) | |
| 2931 | ( (uint64_t) buf[4] << 8 ) | |
| 2932 | ( (uint64_t) buf[5] ) ); |
| 2933 | } |
| 2934 | |
Arto Kinnunen | 7f8089b | 2019-10-29 11:13:33 +0200 | [diff] [blame] | 2935 | static int mbedtls_ssl_dtls_record_replay_check( mbedtls_ssl_context *ssl, uint8_t *record_in_ctr ) |
| 2936 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 2937 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Arto Kinnunen | 7f8089b | 2019-10-29 11:13:33 +0200 | [diff] [blame] | 2938 | unsigned char *original_in_ctr; |
| 2939 | |
| 2940 | // save original in_ctr |
| 2941 | original_in_ctr = ssl->in_ctr; |
| 2942 | |
| 2943 | // use counter from record |
| 2944 | ssl->in_ctr = record_in_ctr; |
| 2945 | |
| 2946 | ret = mbedtls_ssl_dtls_replay_check( (mbedtls_ssl_context const *) ssl ); |
| 2947 | |
| 2948 | // restore the counter |
| 2949 | ssl->in_ctr = original_in_ctr; |
| 2950 | |
| 2951 | return ret; |
| 2952 | } |
| 2953 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 2954 | /* |
| 2955 | * Return 0 if sequence number is acceptable, -1 otherwise |
| 2956 | */ |
Hanno Becker | 0183d69 | 2019-07-12 08:50:37 +0100 | [diff] [blame] | 2957 | 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] | 2958 | { |
| 2959 | uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 ); |
| 2960 | uint64_t bit; |
| 2961 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2962 | if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED ) |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 2963 | return( 0 ); |
| 2964 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 2965 | if( rec_seqnum > ssl->in_window_top ) |
| 2966 | return( 0 ); |
| 2967 | |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2968 | bit = ssl->in_window_top - rec_seqnum; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 2969 | |
| 2970 | if( bit >= 64 ) |
| 2971 | return( -1 ); |
| 2972 | |
| 2973 | if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 ) |
| 2974 | return( -1 ); |
| 2975 | |
| 2976 | return( 0 ); |
| 2977 | } |
| 2978 | |
| 2979 | /* |
| 2980 | * Update replay window on new validated record |
| 2981 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2982 | void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 2983 | { |
| 2984 | uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 ); |
| 2985 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 2986 | if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED ) |
Manuel Pégourié-Gonnard | 2739313 | 2014-09-24 14:41:11 +0200 | [diff] [blame] | 2987 | return; |
| 2988 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 2989 | if( rec_seqnum > ssl->in_window_top ) |
| 2990 | { |
| 2991 | /* Update window_top and the contents of the window */ |
| 2992 | uint64_t shift = rec_seqnum - ssl->in_window_top; |
| 2993 | |
| 2994 | if( shift >= 64 ) |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2995 | ssl->in_window = 1; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 2996 | else |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2997 | { |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 2998 | ssl->in_window <<= shift; |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2999 | ssl->in_window |= 1; |
| 3000 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3001 | |
| 3002 | ssl->in_window_top = rec_seqnum; |
| 3003 | } |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3004 | else |
| 3005 | { |
| 3006 | /* Mark that number as seen in the current window */ |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3007 | uint64_t bit = ssl->in_window_top - rec_seqnum; |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3008 | |
| 3009 | if( bit < 64 ) /* Always true, but be extra sure */ |
| 3010 | ssl->in_window |= (uint64_t) 1 << bit; |
| 3011 | } |
| 3012 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3013 | #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3014 | |
Manuel Pégourié-Gonnard | ddfe5d2 | 2015-09-09 12:46:16 +0200 | [diff] [blame] | 3015 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C) |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3016 | /* |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3017 | * Without any SSL context, check if a datagram looks like a ClientHello with |
| 3018 | * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message. |
Simon Butcher | 0789aed | 2015-09-11 17:15:17 +0100 | [diff] [blame] | 3019 | * Both input and output include full DTLS headers. |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3020 | * |
| 3021 | * - if cookie is valid, return 0 |
| 3022 | * - if ClientHello looks superficially valid but cookie is not, |
| 3023 | * fill obuf and set olen, then |
| 3024 | * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED |
| 3025 | * - otherwise return a specific error code |
| 3026 | */ |
| 3027 | static int ssl_check_dtls_clihlo_cookie( |
| 3028 | mbedtls_ssl_cookie_write_t *f_cookie_write, |
| 3029 | mbedtls_ssl_cookie_check_t *f_cookie_check, |
| 3030 | void *p_cookie, |
| 3031 | const unsigned char *cli_id, size_t cli_id_len, |
| 3032 | const unsigned char *in, size_t in_len, |
| 3033 | unsigned char *obuf, size_t buf_len, size_t *olen ) |
| 3034 | { |
| 3035 | size_t sid_len, cookie_len; |
| 3036 | unsigned char *p; |
| 3037 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3038 | /* |
| 3039 | * Structure of ClientHello with record and handshake headers, |
| 3040 | * and expected values. We don't need to check a lot, more checks will be |
| 3041 | * done when actually parsing the ClientHello - skipping those checks |
| 3042 | * avoids code duplication and does not make cookie forging any easier. |
| 3043 | * |
| 3044 | * 0-0 ContentType type; copied, must be handshake |
| 3045 | * 1-2 ProtocolVersion version; copied |
| 3046 | * 3-4 uint16 epoch; copied, must be 0 |
| 3047 | * 5-10 uint48 sequence_number; copied |
| 3048 | * 11-12 uint16 length; (ignored) |
| 3049 | * |
| 3050 | * 13-13 HandshakeType msg_type; (ignored) |
| 3051 | * 14-16 uint24 length; (ignored) |
| 3052 | * 17-18 uint16 message_seq; copied |
| 3053 | * 19-21 uint24 fragment_offset; copied, must be 0 |
| 3054 | * 22-24 uint24 fragment_length; (ignored) |
| 3055 | * |
| 3056 | * 25-26 ProtocolVersion client_version; (ignored) |
| 3057 | * 27-58 Random random; (ignored) |
| 3058 | * 59-xx SessionID session_id; 1 byte len + sid_len content |
| 3059 | * 60+ opaque cookie<0..2^8-1>; 1 byte len + content |
| 3060 | * ... |
| 3061 | * |
| 3062 | * Minimum length is 61 bytes. |
| 3063 | */ |
| 3064 | if( in_len < 61 || |
| 3065 | in[0] != MBEDTLS_SSL_MSG_HANDSHAKE || |
| 3066 | in[3] != 0 || in[4] != 0 || |
| 3067 | in[19] != 0 || in[20] != 0 || in[21] != 0 ) |
| 3068 | { |
Hanno Becker | 90d59dd | 2021-06-24 11:17:13 +0100 | [diff] [blame] | 3069 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3070 | } |
| 3071 | |
| 3072 | sid_len = in[59]; |
| 3073 | if( sid_len > in_len - 61 ) |
Hanno Becker | 90d59dd | 2021-06-24 11:17:13 +0100 | [diff] [blame] | 3074 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3075 | |
| 3076 | cookie_len = in[60 + sid_len]; |
| 3077 | if( cookie_len > in_len - 60 ) |
Hanno Becker | 90d59dd | 2021-06-24 11:17:13 +0100 | [diff] [blame] | 3078 | return( MBEDTLS_ERR_SSL_DECODE_ERROR ); |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3079 | |
| 3080 | if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len, |
| 3081 | cli_id, cli_id_len ) == 0 ) |
| 3082 | { |
| 3083 | /* Valid cookie */ |
| 3084 | return( 0 ); |
| 3085 | } |
| 3086 | |
| 3087 | /* |
| 3088 | * If we get here, we've got an invalid cookie, let's prepare HVR. |
| 3089 | * |
| 3090 | * 0-0 ContentType type; copied |
| 3091 | * 1-2 ProtocolVersion version; copied |
| 3092 | * 3-4 uint16 epoch; copied |
| 3093 | * 5-10 uint48 sequence_number; copied |
| 3094 | * 11-12 uint16 length; olen - 13 |
| 3095 | * |
| 3096 | * 13-13 HandshakeType msg_type; hello_verify_request |
| 3097 | * 14-16 uint24 length; olen - 25 |
| 3098 | * 17-18 uint16 message_seq; copied |
| 3099 | * 19-21 uint24 fragment_offset; copied |
| 3100 | * 22-24 uint24 fragment_length; olen - 25 |
| 3101 | * |
| 3102 | * 25-26 ProtocolVersion server_version; 0xfe 0xff |
| 3103 | * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie |
| 3104 | * |
| 3105 | * Minimum length is 28. |
| 3106 | */ |
| 3107 | if( buf_len < 28 ) |
| 3108 | return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 3109 | |
| 3110 | /* Copy most fields and adapt others */ |
| 3111 | memcpy( obuf, in, 25 ); |
| 3112 | obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST; |
| 3113 | obuf[25] = 0xfe; |
| 3114 | obuf[26] = 0xff; |
| 3115 | |
| 3116 | /* Generate and write actual cookie */ |
| 3117 | p = obuf + 28; |
| 3118 | if( f_cookie_write( p_cookie, |
| 3119 | &p, obuf + buf_len, cli_id, cli_id_len ) != 0 ) |
| 3120 | { |
| 3121 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3122 | } |
| 3123 | |
| 3124 | *olen = p - obuf; |
| 3125 | |
| 3126 | /* Go back and fill length fields */ |
| 3127 | obuf[27] = (unsigned char)( *olen - 28 ); |
| 3128 | |
| 3129 | obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 ); |
| 3130 | obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 ); |
| 3131 | obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) ); |
| 3132 | |
| 3133 | obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 ); |
| 3134 | obuf[12] = (unsigned char)( ( *olen - 13 ) ); |
| 3135 | |
| 3136 | return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ); |
| 3137 | } |
| 3138 | |
| 3139 | /* |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3140 | * Handle possible client reconnect with the same UDP quadruplet |
| 3141 | * (RFC 6347 Section 4.2.8). |
| 3142 | * |
| 3143 | * Called by ssl_parse_record_header() in case we receive an epoch 0 record |
| 3144 | * that looks like a ClientHello. |
| 3145 | * |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3146 | * - if the input looks like a ClientHello without cookies, |
Manuel Pégourié-Gonnard | 824655c | 2020-03-11 12:51:42 +0100 | [diff] [blame] | 3147 | * send back HelloVerifyRequest, then return 0 |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3148 | * - if the input looks like a ClientHello with a valid cookie, |
| 3149 | * reset the session of the current context, and |
Manuel Pégourié-Gonnard | be619c1 | 2015-09-08 11:21:21 +0200 | [diff] [blame] | 3150 | * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3151 | * - if anything goes wrong, return a specific error code |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3152 | * |
Manuel Pégourié-Gonnard | 824655c | 2020-03-11 12:51:42 +0100 | [diff] [blame] | 3153 | * This function is called (through ssl_check_client_reconnect()) when an |
| 3154 | * unexpected record is found in ssl_get_next_record(), which will discard the |
| 3155 | * record if we return 0, and bubble up the return value otherwise (this |
| 3156 | * includes the case of MBEDTLS_ERR_SSL_CLIENT_RECONNECT and of unexpected |
| 3157 | * 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] | 3158 | */ |
| 3159 | static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl ) |
| 3160 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3161 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3162 | size_t len; |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3163 | |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 3164 | if( ssl->conf->f_cookie_write == NULL || |
| 3165 | ssl->conf->f_cookie_check == NULL ) |
| 3166 | { |
| 3167 | /* If we can't use cookies to verify reachability of the peer, |
| 3168 | * drop the record. */ |
Manuel Pégourié-Gonnard | 243d70f | 2020-03-31 12:07:47 +0200 | [diff] [blame] | 3169 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "no cookie callbacks, " |
| 3170 | "can't check reconnect validity" ) ); |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 3171 | return( 0 ); |
| 3172 | } |
| 3173 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3174 | ret = ssl_check_dtls_clihlo_cookie( |
| 3175 | ssl->conf->f_cookie_write, |
| 3176 | ssl->conf->f_cookie_check, |
| 3177 | ssl->conf->p_cookie, |
| 3178 | ssl->cli_id, ssl->cli_id_len, |
| 3179 | ssl->in_buf, ssl->in_left, |
Angus Gratton | d8213d0 | 2016-05-25 20:56:48 +1000 | [diff] [blame] | 3180 | ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len ); |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3181 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3182 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret ); |
| 3183 | |
| 3184 | if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ) |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3185 | { |
Manuel Pégourié-Gonnard | 243d70f | 2020-03-31 12:07:47 +0200 | [diff] [blame] | 3186 | int send_ret; |
| 3187 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "sending HelloVerifyRequest" ) ); |
| 3188 | MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network", |
| 3189 | ssl->out_buf, len ); |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 3190 | /* 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] | 3191 | * If the error is permanent we'll catch it later, |
| 3192 | * if it's not, then hopefully it'll work next time. */ |
Manuel Pégourié-Gonnard | 243d70f | 2020-03-31 12:07:47 +0200 | [diff] [blame] | 3193 | send_ret = ssl->f_send( ssl->p_bio, ssl->out_buf, len ); |
| 3194 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", send_ret ); |
| 3195 | (void) send_ret; |
| 3196 | |
Manuel Pégourié-Gonnard | 824655c | 2020-03-11 12:51:42 +0100 | [diff] [blame] | 3197 | return( 0 ); |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3198 | } |
| 3199 | |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3200 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3201 | { |
Manuel Pégourié-Gonnard | 243d70f | 2020-03-31 12:07:47 +0200 | [diff] [blame] | 3202 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "cookie is valid, resetting context" ) ); |
Hanno Becker | 43aefe2 | 2020-02-05 10:44:56 +0000 | [diff] [blame] | 3203 | if( ( ret = mbedtls_ssl_session_reset_int( ssl, 1 ) ) != 0 ) |
Manuel Pégourié-Gonnard | 62c74bb | 2015-09-08 17:50:29 +0200 | [diff] [blame] | 3204 | { |
| 3205 | MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret ); |
| 3206 | return( ret ); |
| 3207 | } |
| 3208 | |
| 3209 | return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT ); |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3210 | } |
| 3211 | |
Manuel Pégourié-Gonnard | 11331fc | 2015-09-08 10:30:55 +0200 | [diff] [blame] | 3212 | return( ret ); |
| 3213 | } |
Manuel Pégourié-Gonnard | ddfe5d2 | 2015-09-09 12:46:16 +0200 | [diff] [blame] | 3214 | #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] | 3215 | |
Hanno Becker | f661c9c | 2019-05-03 13:25:54 +0100 | [diff] [blame] | 3216 | static int ssl_check_record_type( uint8_t record_type ) |
| 3217 | { |
| 3218 | if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3219 | record_type != MBEDTLS_SSL_MSG_ALERT && |
| 3220 | record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC && |
| 3221 | record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
| 3222 | { |
| 3223 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 3224 | } |
| 3225 | |
| 3226 | return( 0 ); |
| 3227 | } |
| 3228 | |
Manuel Pégourié-Gonnard | 7a7e140 | 2014-09-24 10:52:58 +0200 | [diff] [blame] | 3229 | /* |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3230 | * ContentType type; |
| 3231 | * ProtocolVersion version; |
| 3232 | * uint16 epoch; // DTLS only |
| 3233 | * uint48 sequence_number; // DTLS only |
| 3234 | * uint16 length; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3235 | * |
| 3236 | * 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] | 3237 | * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad, |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3238 | * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected. |
| 3239 | * |
| 3240 | * With DTLS, mbedtls_ssl_read_record() will: |
Simon Butcher | 207990d | 2015-12-16 01:51:30 +0000 | [diff] [blame] | 3241 | * 1. proceed with the record if this function returns 0 |
| 3242 | * 2. drop only the current record if this function returns UNEXPECTED_RECORD |
| 3243 | * 3. return CLIENT_RECONNECT if this function return that value |
| 3244 | * 4. drop the whole datagram if this function returns anything else. |
| 3245 | * Point 2 is needed when the peer is resending, and we have already received |
| 3246 | * 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] | 3247 | */ |
Hanno Becker | 331de3d | 2019-07-12 11:10:16 +0100 | [diff] [blame] | 3248 | static int ssl_parse_record_header( mbedtls_ssl_context const *ssl, |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3249 | unsigned char *buf, |
| 3250 | size_t len, |
| 3251 | mbedtls_record *rec ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3252 | { |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 3253 | int major_ver, minor_ver; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3254 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3255 | size_t const rec_hdr_type_offset = 0; |
| 3256 | size_t const rec_hdr_type_len = 1; |
Manuel Pégourié-Gonnard | 64dffc5 | 2014-09-02 13:39:16 +0200 | [diff] [blame] | 3257 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3258 | size_t const rec_hdr_version_offset = rec_hdr_type_offset + |
| 3259 | rec_hdr_type_len; |
| 3260 | size_t const rec_hdr_version_len = 2; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3261 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3262 | size_t const rec_hdr_ctr_len = 8; |
| 3263 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | f546625 | 2019-07-25 10:13:02 +0100 | [diff] [blame] | 3264 | uint32_t rec_epoch; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3265 | size_t const rec_hdr_ctr_offset = rec_hdr_version_offset + |
| 3266 | rec_hdr_version_len; |
| 3267 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3268 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3269 | size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset + |
| 3270 | rec_hdr_ctr_len; |
Hanno Becker | f546625 | 2019-07-25 10:13:02 +0100 | [diff] [blame] | 3271 | size_t rec_hdr_cid_len = 0; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3272 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3273 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3274 | |
| 3275 | size_t rec_hdr_len_offset; /* To be determined */ |
| 3276 | size_t const rec_hdr_len_len = 2; |
| 3277 | |
| 3278 | /* |
| 3279 | * Check minimum lengths for record header. |
| 3280 | */ |
| 3281 | |
| 3282 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3283 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 3284 | { |
| 3285 | rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len; |
| 3286 | } |
| 3287 | else |
| 3288 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3289 | { |
| 3290 | rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len; |
| 3291 | } |
| 3292 | |
| 3293 | if( len < rec_hdr_len_offset + rec_hdr_len_len ) |
| 3294 | { |
| 3295 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u", |
| 3296 | (unsigned) len, |
| 3297 | (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) ); |
| 3298 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 3299 | } |
| 3300 | |
| 3301 | /* |
| 3302 | * Parse and validate record content type |
| 3303 | */ |
| 3304 | |
| 3305 | rec->type = buf[ rec_hdr_type_offset ]; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3306 | |
| 3307 | /* Check record content type */ |
| 3308 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3309 | rec->cid_len = 0; |
| 3310 | |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 3311 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3312 | ssl->conf->cid_len != 0 && |
| 3313 | rec->type == MBEDTLS_SSL_MSG_CID ) |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 3314 | { |
| 3315 | /* Shift pointers to account for record header including CID |
| 3316 | * struct { |
| 3317 | * ContentType special_type = tls12_cid; |
| 3318 | * ProtocolVersion version; |
| 3319 | * uint16 epoch; |
| 3320 | * uint48 sequence_number; |
Hanno Becker | 8e55b0f | 2019-05-23 17:03:19 +0100 | [diff] [blame] | 3321 | * opaque cid[cid_length]; // Additional field compared to |
| 3322 | * // default DTLS record format |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 3323 | * uint16 length; |
| 3324 | * opaque enc_content[DTLSCiphertext.length]; |
| 3325 | * } DTLSCiphertext; |
| 3326 | */ |
| 3327 | |
| 3328 | /* So far, we only support static CID lengths |
| 3329 | * fixed in the configuration. */ |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3330 | rec_hdr_cid_len = ssl->conf->cid_len; |
| 3331 | rec_hdr_len_offset += rec_hdr_cid_len; |
Hanno Becker | e538d82 | 2019-07-10 14:50:10 +0100 | [diff] [blame] | 3332 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3333 | if( len < rec_hdr_len_offset + rec_hdr_len_len ) |
Hanno Becker | e538d82 | 2019-07-10 14:50:10 +0100 | [diff] [blame] | 3334 | { |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3335 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u", |
| 3336 | (unsigned) len, |
| 3337 | (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) ); |
Hanno Becker | 59be60e | 2019-07-10 14:53:43 +0100 | [diff] [blame] | 3338 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Hanno Becker | e538d82 | 2019-07-10 14:50:10 +0100 | [diff] [blame] | 3339 | } |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3340 | |
Manuel Pégourié-Gonnard | 7e821b5 | 2019-08-02 10:17:15 +0200 | [diff] [blame] | 3341 | /* configured CID len is guaranteed at most 255, see |
| 3342 | * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */ |
| 3343 | rec->cid_len = (uint8_t) rec_hdr_cid_len; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3344 | memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len ); |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 3345 | } |
| 3346 | else |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3347 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 3348 | { |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3349 | if( ssl_check_record_type( rec->type ) ) |
| 3350 | { |
Hanno Becker | 5422981 | 2019-07-12 14:40:00 +0100 | [diff] [blame] | 3351 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u", |
| 3352 | (unsigned) rec->type ) ); |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3353 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 3354 | } |
Manuel Pégourié-Gonnard | edcbe54 | 2014-08-11 19:27:24 +0200 | [diff] [blame] | 3355 | } |
| 3356 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3357 | /* |
| 3358 | * Parse and validate record version |
| 3359 | */ |
| 3360 | |
Hanno Becker | d0b66d0 | 2019-07-26 08:07:03 +0100 | [diff] [blame] | 3361 | rec->ver[0] = buf[ rec_hdr_version_offset + 0 ]; |
| 3362 | rec->ver[1] = buf[ rec_hdr_version_offset + 1 ]; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3363 | mbedtls_ssl_read_version( &major_ver, &minor_ver, |
| 3364 | ssl->conf->transport, |
Hanno Becker | d0b66d0 | 2019-07-26 08:07:03 +0100 | [diff] [blame] | 3365 | &rec->ver[0] ); |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3366 | |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 3367 | if( major_ver != ssl->major_ver ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3368 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3369 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) ); |
| 3370 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3371 | } |
| 3372 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3373 | if( minor_ver > ssl->conf->max_minor_ver ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3374 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3375 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) ); |
| 3376 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3377 | } |
| 3378 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3379 | /* |
| 3380 | * Parse/Copy record sequence number. |
| 3381 | */ |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 3382 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3383 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3384 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 3385 | { |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3386 | /* Copy explicit record sequence number from input buffer. */ |
| 3387 | memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset, |
| 3388 | rec_hdr_ctr_len ); |
Paul Bakker | 1a1fbba | 2014-04-30 14:38:05 +0200 | [diff] [blame] | 3389 | } |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3390 | else |
| 3391 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3392 | { |
| 3393 | /* Copy implicit record sequence number from SSL context structure. */ |
| 3394 | memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len ); |
| 3395 | } |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 3396 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3397 | /* |
| 3398 | * Parse record length. |
| 3399 | */ |
| 3400 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3401 | rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len; |
Hanno Becker | 9eca276 | 2019-07-25 10:16:37 +0100 | [diff] [blame] | 3402 | rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) | |
| 3403 | ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 ); |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3404 | MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3405 | |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 3406 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %u, " |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 3407 | "version = [%d:%d], msglen = %" MBEDTLS_PRINTF_SIZET, |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3408 | rec->type, |
| 3409 | major_ver, minor_ver, rec->data_len ) ); |
| 3410 | |
| 3411 | rec->buf = buf; |
| 3412 | rec->buf_len = rec->data_offset + rec->data_len; |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 3413 | |
Hanno Becker | d417cc9 | 2019-07-26 08:20:27 +0100 | [diff] [blame] | 3414 | if( rec->data_len == 0 ) |
| 3415 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3416 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3417 | /* |
Hanno Becker | 52c6dc6 | 2017-05-26 16:07:36 +0100 | [diff] [blame] | 3418 | * DTLS-related tests. |
| 3419 | * Check epoch before checking length constraint because |
| 3420 | * the latter varies with the epoch. E.g., if a ChangeCipherSpec |
| 3421 | * message gets duplicated before the corresponding Finished message, |
| 3422 | * the second ChangeCipherSpec should be discarded because it belongs |
| 3423 | * to an old epoch, but not because its length is shorter than |
| 3424 | * the minimum record length for packets using the new record transform. |
| 3425 | * Note that these two kinds of failures are handled differently, |
| 3426 | * as an unexpected record is silently skipped but an invalid |
| 3427 | * record leads to the entire datagram being dropped. |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3428 | */ |
| 3429 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3430 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 3431 | { |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3432 | rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1]; |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3433 | |
Hanno Becker | 955a5c9 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 3434 | /* Check that the datagram is large enough to contain a record |
| 3435 | * of the advertised length. */ |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3436 | if( len < rec->data_offset + rec->data_len ) |
Hanno Becker | 955a5c9 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 3437 | { |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 3438 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.", |
| 3439 | (unsigned) len, |
| 3440 | (unsigned)( rec->data_offset + rec->data_len ) ) ); |
Hanno Becker | 955a5c9 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 3441 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 3442 | } |
Hanno Becker | 37cfe73 | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 3443 | |
Hanno Becker | 37cfe73 | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 3444 | /* Records from other, non-matching epochs are silently discarded. |
| 3445 | * (The case of same-port Client reconnects must be considered in |
| 3446 | * the caller). */ |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3447 | if( rec_epoch != ssl->in_epoch ) |
| 3448 | { |
| 3449 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: " |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 3450 | "expected %u, received %lu", |
| 3451 | ssl->in_epoch, (unsigned long) rec_epoch ) ); |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3452 | |
Hanno Becker | 552f747 | 2019-07-19 10:59:12 +0100 | [diff] [blame] | 3453 | /* Records from the next epoch are considered for buffering |
| 3454 | * (concretely: early Finished messages). */ |
| 3455 | if( rec_epoch == (unsigned) ssl->in_epoch + 1 ) |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3456 | { |
Hanno Becker | 552f747 | 2019-07-19 10:59:12 +0100 | [diff] [blame] | 3457 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) ); |
| 3458 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3459 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 3460 | |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 3461 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3462 | } |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3463 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Hanno Becker | 37cfe73 | 2019-07-10 17:20:01 +0100 | [diff] [blame] | 3464 | /* For records from the correct epoch, check whether their |
| 3465 | * sequence number has been seen before. */ |
Arto Kinnunen | 7f8089b | 2019-10-29 11:13:33 +0200 | [diff] [blame] | 3466 | else if( mbedtls_ssl_dtls_record_replay_check( (mbedtls_ssl_context *) ssl, |
| 3467 | &rec->ctr[0] ) != 0 ) |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 3468 | { |
| 3469 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) ); |
| 3470 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
| 3471 | } |
| 3472 | #endif |
| 3473 | } |
| 3474 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3475 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3476 | return( 0 ); |
| 3477 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3478 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3479 | |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 3480 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C) |
| 3481 | static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl ) |
| 3482 | { |
| 3483 | unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1]; |
| 3484 | |
| 3485 | /* |
| 3486 | * Check for an epoch 0 ClientHello. We can't use in_msg here to |
| 3487 | * access the first byte of record content (handshake type), as we |
| 3488 | * have an active transform (possibly iv_len != 0), so use the |
| 3489 | * fact that the record header len is 13 instead. |
| 3490 | */ |
| 3491 | if( rec_epoch == 0 && |
| 3492 | ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 3493 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER && |
| 3494 | ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
| 3495 | ssl->in_left > 13 && |
| 3496 | ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO ) |
| 3497 | { |
| 3498 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect " |
| 3499 | "from the same port" ) ); |
| 3500 | return( ssl_handle_possible_reconnect( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3501 | } |
| 3502 | |
| 3503 | return( 0 ); |
| 3504 | } |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 3505 | #endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3506 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3507 | /* |
Manuel Pégourié-Gonnard | c40b685 | 2020-01-03 12:18:49 +0100 | [diff] [blame] | 3508 | * If applicable, decrypt record content |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3509 | */ |
Hanno Becker | fdf6604 | 2019-07-11 13:07:45 +0100 | [diff] [blame] | 3510 | static int ssl_prepare_record_content( mbedtls_ssl_context *ssl, |
| 3511 | mbedtls_record *rec ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3512 | { |
| 3513 | int ret, done = 0; |
Manuel Pégourié-Gonnard | b2f3be8 | 2014-07-10 17:54:52 +0200 | [diff] [blame] | 3514 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3515 | MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network", |
Hanno Becker | fdf6604 | 2019-07-11 13:07:45 +0100 | [diff] [blame] | 3516 | rec->buf, rec->buf_len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3517 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 3518 | if( !done && ssl->transform_in != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3519 | { |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 3520 | unsigned char const old_msg_type = rec->type; |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3521 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3522 | if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, |
Hanno Becker | fdf6604 | 2019-07-11 13:07:45 +0100 | [diff] [blame] | 3523 | rec ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3524 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3525 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret ); |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 3526 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3527 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 3528 | if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID && |
| 3529 | ssl->conf->ignore_unexpected_cid |
| 3530 | == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
| 3531 | { |
Hanno Becker | e8d6afd | 2019-05-24 10:11:06 +0100 | [diff] [blame] | 3532 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) ); |
Hanno Becker | 16ded98 | 2019-05-08 13:02:55 +0100 | [diff] [blame] | 3533 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
Hanno Becker | 8367ccc | 2019-05-14 11:30:10 +0100 | [diff] [blame] | 3534 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3535 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 16ded98 | 2019-05-08 13:02:55 +0100 | [diff] [blame] | 3536 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3537 | return( ret ); |
| 3538 | } |
| 3539 | |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 3540 | if( old_msg_type != rec->type ) |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 3541 | { |
| 3542 | MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d", |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 3543 | old_msg_type, rec->type ) ); |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 3544 | } |
| 3545 | |
Hanno Becker | 1c0c37f | 2018-08-07 14:29:29 +0100 | [diff] [blame] | 3546 | MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt", |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 3547 | rec->buf + rec->data_offset, rec->data_len ); |
Hanno Becker | 1c0c37f | 2018-08-07 14:29:29 +0100 | [diff] [blame] | 3548 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3549 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 3550 | /* We have already checked the record content type |
| 3551 | * in ssl_parse_record_header(), failing or silently |
| 3552 | * dropping the record in the case of an unknown type. |
| 3553 | * |
| 3554 | * Since with the use of CIDs, the record content type |
| 3555 | * might change during decryption, re-check the record |
| 3556 | * content type, but treat a failure as fatal this time. */ |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 3557 | if( ssl_check_record_type( rec->type ) ) |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 3558 | { |
| 3559 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) ); |
| 3560 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 3561 | } |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3562 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 6430faf | 2019-05-08 11:57:13 +0100 | [diff] [blame] | 3563 | |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 3564 | if( rec->data_len == 0 ) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3565 | { |
| 3566 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 3567 | if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 |
Hanno Becker | 58ef0bf | 2019-07-12 09:35:58 +0100 | [diff] [blame] | 3568 | && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3569 | { |
| 3570 | /* TLS v1.2 explicitly disallows zero-length messages which are not application data */ |
| 3571 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) ); |
| 3572 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 3573 | } |
| 3574 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 3575 | |
| 3576 | ssl->nb_zero++; |
| 3577 | |
| 3578 | /* |
| 3579 | * Three or more empty messages may be a DoS attack |
| 3580 | * (excessive CPU consumption). |
| 3581 | */ |
| 3582 | if( ssl->nb_zero > 3 ) |
| 3583 | { |
| 3584 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty " |
Hanno Becker | 6e7700d | 2019-05-08 10:38:32 +0100 | [diff] [blame] | 3585 | "messages, possible DoS attack" ) ); |
| 3586 | /* Treat the records as if they were not properly authenticated, |
| 3587 | * thereby failing the connection if we see more than allowed |
| 3588 | * by the configured bad MAC threshold. */ |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3589 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
| 3590 | } |
| 3591 | } |
| 3592 | else |
| 3593 | ssl->nb_zero = 0; |
| 3594 | |
| 3595 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3596 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 3597 | { |
| 3598 | ; /* in_ctr read from peer, not maintained internally */ |
| 3599 | } |
| 3600 | else |
| 3601 | #endif |
| 3602 | { |
| 3603 | unsigned i; |
Hanno Becker | dd77229 | 2020-02-05 10:38:31 +0000 | [diff] [blame] | 3604 | for( i = 8; i > mbedtls_ssl_ep_len( ssl ); i-- ) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3605 | if( ++ssl->in_ctr[i - 1] != 0 ) |
| 3606 | break; |
| 3607 | |
| 3608 | /* The loop goes to its end iff the counter is wrapping */ |
Hanno Becker | dd77229 | 2020-02-05 10:38:31 +0000 | [diff] [blame] | 3609 | if( i == mbedtls_ssl_ep_len( ssl ) ) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 3610 | { |
| 3611 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) ); |
| 3612 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
| 3613 | } |
| 3614 | } |
| 3615 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 3616 | } |
| 3617 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3618 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 3619 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 3620 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3621 | mbedtls_ssl_dtls_replay_update( ssl ); |
Manuel Pégourié-Gonnard | b47368a | 2014-09-24 13:29:58 +0200 | [diff] [blame] | 3622 | } |
| 3623 | #endif |
| 3624 | |
Hanno Becker | d96e10b | 2019-07-09 17:30:02 +0100 | [diff] [blame] | 3625 | /* Check actual (decrypted) record content length against |
| 3626 | * configured maximum. */ |
| 3627 | if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN ) |
| 3628 | { |
| 3629 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) ); |
| 3630 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 3631 | } |
| 3632 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3633 | return( 0 ); |
| 3634 | } |
| 3635 | |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 3636 | /* |
| 3637 | * Read a record. |
| 3638 | * |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 3639 | * Silently ignore non-fatal alert (and for DTLS, invalid records as well, |
| 3640 | * RFC 6347 4.1.2.7) and continue reading until a valid record is found. |
| 3641 | * |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 3642 | */ |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 3643 | |
| 3644 | /* Helper functions for mbedtls_ssl_read_record(). */ |
| 3645 | static int ssl_consume_current_message( mbedtls_ssl_context *ssl ); |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 3646 | static int ssl_get_next_record( mbedtls_ssl_context *ssl ); |
| 3647 | static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl ); |
Hanno Becker | 4162b11 | 2018-08-15 14:05:04 +0100 | [diff] [blame] | 3648 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 3649 | int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl, |
Hanno Becker | 3a0aad1 | 2018-08-20 09:44:02 +0100 | [diff] [blame] | 3650 | unsigned update_hs_digest ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3651 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 3652 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3653 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3654 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 3655 | |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3656 | if( ssl->keep_current_message == 0 ) |
| 3657 | { |
| 3658 | do { |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3659 | |
Hanno Becker | 2699459 | 2018-08-15 14:14:59 +0100 | [diff] [blame] | 3660 | ret = ssl_consume_current_message( ssl ); |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 3661 | if( ret != 0 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3662 | return( ret ); |
Hanno Becker | 2699459 | 2018-08-15 14:14:59 +0100 | [diff] [blame] | 3663 | |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 3664 | if( ssl_record_is_in_progress( ssl ) == 0 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3665 | { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3666 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3667 | int have_buffered = 0; |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 3668 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3669 | /* We only check for buffered messages if the |
| 3670 | * current datagram is fully consumed. */ |
| 3671 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 3672 | ssl_next_record_is_in_datagram( ssl ) == 0 ) |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 3673 | { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3674 | if( ssl_load_buffered_message( ssl ) == 0 ) |
| 3675 | have_buffered = 1; |
| 3676 | } |
| 3677 | |
| 3678 | if( have_buffered == 0 ) |
| 3679 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3680 | { |
| 3681 | ret = ssl_get_next_record( ssl ); |
| 3682 | if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ) |
| 3683 | continue; |
| 3684 | |
| 3685 | if( ret != 0 ) |
| 3686 | { |
Hanno Becker | c573ac3 | 2018-08-28 17:15:25 +0100 | [diff] [blame] | 3687 | MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret ); |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3688 | return( ret ); |
| 3689 | } |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 3690 | } |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3691 | } |
| 3692 | |
| 3693 | ret = mbedtls_ssl_handle_message_type( ssl ); |
| 3694 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3695 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 3696 | if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) |
| 3697 | { |
| 3698 | /* Buffer future message */ |
| 3699 | ret = ssl_buffer_message( ssl ); |
| 3700 | if( ret != 0 ) |
| 3701 | return( ret ); |
| 3702 | |
| 3703 | ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING; |
| 3704 | } |
| 3705 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 3706 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 3707 | } while( MBEDTLS_ERR_SSL_NON_FATAL == ret || |
| 3708 | MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret ); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3709 | |
| 3710 | if( 0 != ret ) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3711 | { |
Hanno Becker | 05c4fc8 | 2017-11-09 14:34:06 +0000 | [diff] [blame] | 3712 | MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3713 | return( ret ); |
| 3714 | } |
| 3715 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 3716 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && |
Hanno Becker | 3a0aad1 | 2018-08-20 09:44:02 +0100 | [diff] [blame] | 3717 | update_hs_digest == 1 ) |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3718 | { |
| 3719 | mbedtls_ssl_update_handshake_status( ssl ); |
| 3720 | } |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3721 | } |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3722 | else |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3723 | { |
Hanno Becker | 02f5907 | 2018-08-15 14:00:24 +0100 | [diff] [blame] | 3724 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) ); |
Hanno Becker | af0665d | 2017-05-24 09:16:26 +0100 | [diff] [blame] | 3725 | ssl->keep_current_message = 0; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3726 | } |
| 3727 | |
| 3728 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) ); |
| 3729 | |
| 3730 | return( 0 ); |
| 3731 | } |
| 3732 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3733 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 3734 | static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl ) |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3735 | { |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3736 | if( ssl->in_left > ssl->next_record_offset ) |
| 3737 | return( 1 ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 3738 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3739 | return( 0 ); |
| 3740 | } |
| 3741 | |
| 3742 | static int ssl_load_buffered_message( mbedtls_ssl_context *ssl ) |
| 3743 | { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3744 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3745 | mbedtls_ssl_hs_buffer * hs_buf; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3746 | int ret = 0; |
| 3747 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3748 | if( hs == NULL ) |
| 3749 | return( -1 ); |
| 3750 | |
Hanno Becker | e00ae37 | 2018-08-20 09:39:42 +0100 | [diff] [blame] | 3751 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) ); |
| 3752 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3753 | if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC || |
| 3754 | ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC ) |
| 3755 | { |
| 3756 | /* Check if we have seen a ChangeCipherSpec before. |
| 3757 | * If yes, synthesize a CCS record. */ |
Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 3758 | if( !hs->buffering.seen_ccs ) |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3759 | { |
| 3760 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) ); |
| 3761 | ret = -1; |
Hanno Becker | 0d4b376 | 2018-08-20 09:36:59 +0100 | [diff] [blame] | 3762 | goto exit; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3763 | } |
| 3764 | |
Hanno Becker | 39b8bc9 | 2018-08-28 17:17:13 +0100 | [diff] [blame] | 3765 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) ); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3766 | ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; |
| 3767 | ssl->in_msglen = 1; |
| 3768 | ssl->in_msg[0] = 1; |
| 3769 | |
| 3770 | /* As long as they are equal, the exact value doesn't matter. */ |
| 3771 | ssl->in_left = 0; |
| 3772 | ssl->next_record_offset = 0; |
| 3773 | |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 3774 | hs->buffering.seen_ccs = 0; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3775 | goto exit; |
| 3776 | } |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3777 | |
Hanno Becker | b8f5014 | 2018-08-28 10:01:34 +0100 | [diff] [blame] | 3778 | #if defined(MBEDTLS_DEBUG_C) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3779 | /* Debug only */ |
| 3780 | { |
| 3781 | unsigned offset; |
| 3782 | for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ ) |
| 3783 | { |
| 3784 | hs_buf = &hs->buffering.hs[offset]; |
| 3785 | if( hs_buf->is_valid == 1 ) |
| 3786 | { |
| 3787 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.", |
| 3788 | hs->in_msg_seq + offset, |
Hanno Becker | a591c48 | 2018-08-28 17:20:00 +0100 | [diff] [blame] | 3789 | hs_buf->is_complete ? "fully" : "partially" ) ); |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3790 | } |
| 3791 | } |
| 3792 | } |
Hanno Becker | b8f5014 | 2018-08-28 10:01:34 +0100 | [diff] [blame] | 3793 | #endif /* MBEDTLS_DEBUG_C */ |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3794 | |
| 3795 | /* Check if we have buffered and/or fully reassembled the |
| 3796 | * next handshake message. */ |
| 3797 | hs_buf = &hs->buffering.hs[0]; |
| 3798 | if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) ) |
| 3799 | { |
| 3800 | /* Synthesize a record containing the buffered HS message. */ |
| 3801 | size_t msg_len = ( hs_buf->data[1] << 16 ) | |
| 3802 | ( hs_buf->data[2] << 8 ) | |
| 3803 | hs_buf->data[3]; |
| 3804 | |
| 3805 | /* Double-check that we haven't accidentally buffered |
| 3806 | * a message that doesn't fit into the input buffer. */ |
| 3807 | if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN ) |
| 3808 | { |
| 3809 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3810 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3811 | } |
| 3812 | |
| 3813 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) ); |
| 3814 | MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)", |
| 3815 | hs_buf->data, msg_len + 12 ); |
| 3816 | |
| 3817 | ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; |
| 3818 | ssl->in_hslen = msg_len + 12; |
| 3819 | ssl->in_msglen = msg_len + 12; |
| 3820 | memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen ); |
| 3821 | |
| 3822 | ret = 0; |
| 3823 | goto exit; |
| 3824 | } |
| 3825 | else |
| 3826 | { |
| 3827 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered", |
| 3828 | hs->in_msg_seq ) ); |
| 3829 | } |
| 3830 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3831 | ret = -1; |
| 3832 | |
| 3833 | exit: |
| 3834 | |
| 3835 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) ); |
| 3836 | return( ret ); |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3837 | } |
| 3838 | |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 3839 | static int ssl_buffer_make_space( mbedtls_ssl_context *ssl, |
| 3840 | size_t desired ) |
| 3841 | { |
| 3842 | int offset; |
| 3843 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 3844 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available", |
| 3845 | (unsigned) desired ) ); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 3846 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 3847 | /* Get rid of future records epoch first, if such exist. */ |
| 3848 | ssl_free_buffered_record( ssl ); |
| 3849 | |
| 3850 | /* Check if we have enough space available now. */ |
| 3851 | if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 3852 | hs->buffering.total_bytes_buffered ) ) |
| 3853 | { |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 3854 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) ); |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 3855 | return( 0 ); |
| 3856 | } |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 3857 | |
Hanno Becker | 4f432ad | 2018-08-28 10:02:32 +0100 | [diff] [blame] | 3858 | /* We don't have enough space to buffer the next expected handshake |
| 3859 | * message. Remove buffers used for future messages to gain space, |
| 3860 | * starting with the most distant one. */ |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 3861 | for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1; |
| 3862 | offset >= 0; offset-- ) |
| 3863 | { |
| 3864 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message", |
| 3865 | offset ) ); |
| 3866 | |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 3867 | ssl_buffering_free_slot( ssl, (uint8_t) offset ); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 3868 | |
| 3869 | /* Check if we have enough space available now. */ |
| 3870 | if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 3871 | hs->buffering.total_bytes_buffered ) ) |
| 3872 | { |
Hanno Becker | 6e12c1e | 2018-08-24 14:39:15 +0100 | [diff] [blame] | 3873 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) ); |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 3874 | return( 0 ); |
| 3875 | } |
| 3876 | } |
| 3877 | |
| 3878 | return( -1 ); |
| 3879 | } |
| 3880 | |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 3881 | static int ssl_buffer_message( mbedtls_ssl_context *ssl ) |
| 3882 | { |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3883 | int ret = 0; |
| 3884 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 3885 | |
| 3886 | if( hs == NULL ) |
| 3887 | return( 0 ); |
| 3888 | |
| 3889 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) ); |
| 3890 | |
| 3891 | switch( ssl->in_msgtype ) |
| 3892 | { |
| 3893 | case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC: |
| 3894 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) ); |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 3895 | |
Hanno Becker | d7f8ae2 | 2018-08-16 09:45:56 +0100 | [diff] [blame] | 3896 | hs->buffering.seen_ccs = 1; |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 3897 | break; |
| 3898 | |
| 3899 | case MBEDTLS_SSL_MSG_HANDSHAKE: |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3900 | { |
| 3901 | unsigned recv_msg_seq_offset; |
| 3902 | unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5]; |
| 3903 | mbedtls_ssl_hs_buffer *hs_buf; |
| 3904 | size_t msg_len = ssl->in_hslen - 12; |
| 3905 | |
| 3906 | /* We should never receive an old handshake |
| 3907 | * message - double-check nonetheless. */ |
| 3908 | if( recv_msg_seq < ssl->handshake->in_msg_seq ) |
| 3909 | { |
| 3910 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3911 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3912 | } |
| 3913 | |
| 3914 | recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq; |
| 3915 | if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS ) |
| 3916 | { |
| 3917 | /* Silently ignore -- message too far in the future */ |
| 3918 | MBEDTLS_SSL_DEBUG_MSG( 2, |
| 3919 | ( "Ignore future HS message with sequence number %u, " |
| 3920 | "buffering window %u - %u", |
| 3921 | recv_msg_seq, ssl->handshake->in_msg_seq, |
| 3922 | ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) ); |
| 3923 | |
| 3924 | goto exit; |
| 3925 | } |
| 3926 | |
| 3927 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ", |
| 3928 | recv_msg_seq, recv_msg_seq_offset ) ); |
| 3929 | |
| 3930 | hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ]; |
| 3931 | |
| 3932 | /* Check if the buffering for this seq nr has already commenced. */ |
Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 3933 | if( !hs_buf->is_valid ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3934 | { |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 3935 | size_t reassembly_buf_sz; |
| 3936 | |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3937 | hs_buf->is_fragmented = |
| 3938 | ( ssl_hs_is_proper_fragment( ssl ) == 1 ); |
| 3939 | |
| 3940 | /* We copy the message back into the input buffer |
| 3941 | * after reassembly, so check that it's not too large. |
| 3942 | * This is an implementation-specific limitation |
| 3943 | * and not one from the standard, hence it is not |
| 3944 | * checked in ssl_check_hs_header(). */ |
Hanno Becker | 96a6c69 | 2018-08-21 15:56:03 +0100 | [diff] [blame] | 3945 | if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 3946 | { |
| 3947 | /* Ignore message */ |
| 3948 | goto exit; |
| 3949 | } |
| 3950 | |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 3951 | /* Check if we have enough space to buffer the message. */ |
| 3952 | if( hs->buffering.total_bytes_buffered > |
| 3953 | MBEDTLS_SSL_DTLS_MAX_BUFFERING ) |
| 3954 | { |
| 3955 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 3956 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 3957 | } |
| 3958 | |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 3959 | reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len, |
| 3960 | hs_buf->is_fragmented ); |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 3961 | |
| 3962 | if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
| 3963 | hs->buffering.total_bytes_buffered ) ) |
| 3964 | { |
| 3965 | if( recv_msg_seq_offset > 0 ) |
| 3966 | { |
| 3967 | /* If we can't buffer a future message because |
| 3968 | * of space limitations -- ignore. */ |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 3969 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %" MBEDTLS_PRINTF_SIZET |
| 3970 | " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET |
| 3971 | " (already %" MBEDTLS_PRINTF_SIZET |
| 3972 | " bytes buffered) -- ignore\n", |
Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 3973 | msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 3974 | hs->buffering.total_bytes_buffered ) ); |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 3975 | goto exit; |
| 3976 | } |
Hanno Becker | e180139 | 2018-08-21 16:51:05 +0100 | [diff] [blame] | 3977 | else |
| 3978 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 3979 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %" MBEDTLS_PRINTF_SIZET |
| 3980 | " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET |
| 3981 | " (already %" MBEDTLS_PRINTF_SIZET |
| 3982 | " bytes buffered) -- attempt to make space by freeing buffered future messages\n", |
Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 3983 | msg_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 3984 | hs->buffering.total_bytes_buffered ) ); |
Hanno Becker | e180139 | 2018-08-21 16:51:05 +0100 | [diff] [blame] | 3985 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 3986 | |
Hanno Becker | a02b0b4 | 2018-08-21 17:20:27 +0100 | [diff] [blame] | 3987 | if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 ) |
Hanno Becker | 55e9e2a | 2018-08-21 16:07:55 +0100 | [diff] [blame] | 3988 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 3989 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %" MBEDTLS_PRINTF_SIZET |
| 3990 | " (%" MBEDTLS_PRINTF_SIZET " with bitmap) would exceed" |
| 3991 | " the compile-time limit %" MBEDTLS_PRINTF_SIZET |
| 3992 | " (already %" MBEDTLS_PRINTF_SIZET |
| 3993 | " bytes buffered) -- fail\n", |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 3994 | msg_len, |
| 3995 | reassembly_buf_sz, |
Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 3996 | (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 3997 | hs->buffering.total_bytes_buffered ) ); |
Hanno Becker | 55e9e2a | 2018-08-21 16:07:55 +0100 | [diff] [blame] | 3998 | ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL; |
| 3999 | goto exit; |
| 4000 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4001 | } |
| 4002 | |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 4003 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %" MBEDTLS_PRINTF_SIZET, |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4004 | msg_len ) ); |
| 4005 | |
Hanno Becker | 2a97b0e | 2018-08-21 15:47:49 +0100 | [diff] [blame] | 4006 | hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz ); |
| 4007 | if( hs_buf->data == NULL ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4008 | { |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4009 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4010 | goto exit; |
| 4011 | } |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4012 | hs_buf->data_len = reassembly_buf_sz; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4013 | |
| 4014 | /* Prepare final header: copy msg_type, length and message_seq, |
| 4015 | * then add standardised fragment_offset and fragment_length */ |
| 4016 | memcpy( hs_buf->data, ssl->in_msg, 6 ); |
| 4017 | memset( hs_buf->data + 6, 0, 3 ); |
| 4018 | memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 ); |
| 4019 | |
| 4020 | hs_buf->is_valid = 1; |
Hanno Becker | e0b150f | 2018-08-21 15:51:03 +0100 | [diff] [blame] | 4021 | |
| 4022 | hs->buffering.total_bytes_buffered += reassembly_buf_sz; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4023 | } |
| 4024 | else |
| 4025 | { |
| 4026 | /* Make sure msg_type and length are consistent */ |
| 4027 | if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 ) |
| 4028 | { |
| 4029 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) ); |
| 4030 | /* Ignore */ |
| 4031 | goto exit; |
| 4032 | } |
| 4033 | } |
| 4034 | |
Hanno Becker | 4422bbb | 2018-08-20 09:40:19 +0100 | [diff] [blame] | 4035 | if( !hs_buf->is_complete ) |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4036 | { |
| 4037 | size_t frag_len, frag_off; |
| 4038 | unsigned char * const msg = hs_buf->data + 12; |
| 4039 | |
| 4040 | /* |
| 4041 | * Check and copy current fragment |
| 4042 | */ |
| 4043 | |
| 4044 | /* Validation of header fields already done in |
| 4045 | * mbedtls_ssl_prepare_handshake_record(). */ |
| 4046 | frag_off = ssl_get_hs_frag_off( ssl ); |
| 4047 | frag_len = ssl_get_hs_frag_len( ssl ); |
| 4048 | |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 4049 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %" MBEDTLS_PRINTF_SIZET |
| 4050 | ", length = %" MBEDTLS_PRINTF_SIZET, |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4051 | frag_off, frag_len ) ); |
| 4052 | memcpy( msg + frag_off, ssl->in_msg + 12, frag_len ); |
| 4053 | |
| 4054 | if( hs_buf->is_fragmented ) |
| 4055 | { |
| 4056 | unsigned char * const bitmask = msg + msg_len; |
| 4057 | ssl_bitmask_set( bitmask, frag_off, frag_len ); |
| 4058 | hs_buf->is_complete = ( ssl_bitmask_check( bitmask, |
| 4059 | msg_len ) == 0 ); |
| 4060 | } |
| 4061 | else |
| 4062 | { |
| 4063 | hs_buf->is_complete = 1; |
| 4064 | } |
| 4065 | |
| 4066 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete", |
| 4067 | hs_buf->is_complete ? "" : "not yet " ) ); |
| 4068 | } |
| 4069 | |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4070 | break; |
Hanno Becker | 37f9532 | 2018-08-16 13:55:32 +0100 | [diff] [blame] | 4071 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4072 | |
| 4073 | default: |
Hanno Becker | 360bef3 | 2018-08-28 10:04:33 +0100 | [diff] [blame] | 4074 | /* We don't buffer other types of messages. */ |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4075 | break; |
| 4076 | } |
| 4077 | |
| 4078 | exit: |
| 4079 | |
| 4080 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) ); |
| 4081 | return( ret ); |
Hanno Becker | 40f5084 | 2018-08-15 14:48:01 +0100 | [diff] [blame] | 4082 | } |
| 4083 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4084 | |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 4085 | static int ssl_consume_current_message( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4086 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4087 | /* |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4088 | * Consume last content-layer message and potentially |
| 4089 | * update in_msglen which keeps track of the contents' |
| 4090 | * consumption state. |
| 4091 | * |
| 4092 | * (1) Handshake messages: |
| 4093 | * Remove last handshake message, move content |
| 4094 | * and adapt in_msglen. |
| 4095 | * |
| 4096 | * (2) Alert messages: |
| 4097 | * Consume whole record content, in_msglen = 0. |
| 4098 | * |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4099 | * (3) Change cipher spec: |
| 4100 | * Consume whole record content, in_msglen = 0. |
| 4101 | * |
| 4102 | * (4) Application data: |
| 4103 | * Don't do anything - the record layer provides |
| 4104 | * the application data as a stream transport |
| 4105 | * and consumes through mbedtls_ssl_read only. |
| 4106 | * |
| 4107 | */ |
| 4108 | |
| 4109 | /* Case (1): Handshake messages */ |
| 4110 | if( ssl->in_hslen != 0 ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4111 | { |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 4112 | /* Hard assertion to be sure that no application data |
| 4113 | * is in flight, as corrupting ssl->in_msglen during |
| 4114 | * ssl->in_offt != NULL is fatal. */ |
| 4115 | if( ssl->in_offt != NULL ) |
| 4116 | { |
| 4117 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4118 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4119 | } |
| 4120 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4121 | /* |
| 4122 | * Get next Handshake message in the current record |
| 4123 | */ |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4124 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4125 | /* Notes: |
Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 4126 | * (1) in_hslen is not necessarily the size of the |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4127 | * current handshake content: If DTLS handshake |
| 4128 | * fragmentation is used, that's the fragment |
| 4129 | * size instead. Using the total handshake message |
Hanno Becker | e72489d | 2017-10-23 13:23:50 +0100 | [diff] [blame] | 4130 | * size here is faulty and should be changed at |
| 4131 | * some point. |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4132 | * (2) While it doesn't seem to cause problems, one |
| 4133 | * has to be very careful not to assume that in_hslen |
| 4134 | * is always <= in_msglen in a sensible communication. |
| 4135 | * Again, it's wrong for DTLS handshake fragmentation. |
| 4136 | * The following check is therefore mandatory, and |
| 4137 | * should not be treated as a silently corrected assertion. |
Hanno Becker | bb9dd0c | 2017-06-08 11:55:34 +0100 | [diff] [blame] | 4138 | * Additionally, ssl->in_hslen might be arbitrarily out of |
| 4139 | * bounds after handling a DTLS message with an unexpected |
| 4140 | * sequence number, see mbedtls_ssl_prepare_handshake_record. |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4141 | */ |
| 4142 | if( ssl->in_hslen < ssl->in_msglen ) |
| 4143 | { |
| 4144 | ssl->in_msglen -= ssl->in_hslen; |
| 4145 | memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen, |
| 4146 | ssl->in_msglen ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4147 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4148 | MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record", |
| 4149 | ssl->in_msg, ssl->in_msglen ); |
| 4150 | } |
| 4151 | else |
| 4152 | { |
| 4153 | ssl->in_msglen = 0; |
| 4154 | } |
Manuel Pégourié-Gonnard | 4a17536 | 2014-09-09 17:45:31 +0200 | [diff] [blame] | 4155 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4156 | ssl->in_hslen = 0; |
| 4157 | } |
| 4158 | /* Case (4): Application data */ |
| 4159 | else if( ssl->in_offt != NULL ) |
| 4160 | { |
| 4161 | return( 0 ); |
| 4162 | } |
| 4163 | /* Everything else (CCS & Alerts) */ |
| 4164 | else |
| 4165 | { |
| 4166 | ssl->in_msglen = 0; |
| 4167 | } |
| 4168 | |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 4169 | return( 0 ); |
| 4170 | } |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4171 | |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4172 | static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl ) |
| 4173 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4174 | if( ssl->in_msglen > 0 ) |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4175 | return( 1 ); |
| 4176 | |
| 4177 | return( 0 ); |
| 4178 | } |
| 4179 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4180 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4181 | |
| 4182 | static void ssl_free_buffered_record( mbedtls_ssl_context *ssl ) |
| 4183 | { |
| 4184 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 4185 | if( hs == NULL ) |
| 4186 | return; |
| 4187 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 4188 | if( hs->buffering.future_record.data != NULL ) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4189 | { |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 4190 | hs->buffering.total_bytes_buffered -= |
| 4191 | hs->buffering.future_record.len; |
| 4192 | |
| 4193 | mbedtls_free( hs->buffering.future_record.data ); |
| 4194 | hs->buffering.future_record.data = NULL; |
| 4195 | } |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4196 | } |
| 4197 | |
| 4198 | static int ssl_load_buffered_record( mbedtls_ssl_context *ssl ) |
| 4199 | { |
| 4200 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 4201 | unsigned char * rec; |
| 4202 | size_t rec_len; |
| 4203 | unsigned rec_epoch; |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 4204 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 4205 | size_t in_buf_len = ssl->in_buf_len; |
| 4206 | #else |
| 4207 | size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN; |
| 4208 | #endif |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4209 | if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 4210 | return( 0 ); |
| 4211 | |
| 4212 | if( hs == NULL ) |
| 4213 | return( 0 ); |
| 4214 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4215 | rec = hs->buffering.future_record.data; |
| 4216 | rec_len = hs->buffering.future_record.len; |
| 4217 | rec_epoch = hs->buffering.future_record.epoch; |
| 4218 | |
| 4219 | if( rec == NULL ) |
| 4220 | return( 0 ); |
| 4221 | |
Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 4222 | /* Only consider loading future records if the |
| 4223 | * input buffer is empty. */ |
Hanno Becker | ef7afdf | 2018-08-28 17:16:31 +0100 | [diff] [blame] | 4224 | if( ssl_next_record_is_in_datagram( ssl ) == 1 ) |
Hanno Becker | 4cb782d | 2018-08-20 11:19:05 +0100 | [diff] [blame] | 4225 | return( 0 ); |
| 4226 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4227 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) ); |
| 4228 | |
| 4229 | if( rec_epoch != ssl->in_epoch ) |
| 4230 | { |
| 4231 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) ); |
| 4232 | goto exit; |
| 4233 | } |
| 4234 | |
| 4235 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) ); |
| 4236 | |
| 4237 | /* Double-check that the record is not too large */ |
Darryl Green | b33cc76 | 2019-11-28 14:29:44 +0000 | [diff] [blame] | 4238 | if( rec_len > in_buf_len - (size_t)( ssl->in_hdr - ssl->in_buf ) ) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4239 | { |
| 4240 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
| 4241 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
| 4242 | } |
| 4243 | |
| 4244 | memcpy( ssl->in_hdr, rec, rec_len ); |
| 4245 | ssl->in_left = rec_len; |
| 4246 | ssl->next_record_offset = 0; |
| 4247 | |
| 4248 | ssl_free_buffered_record( ssl ); |
| 4249 | |
| 4250 | exit: |
| 4251 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) ); |
| 4252 | return( 0 ); |
| 4253 | } |
| 4254 | |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 4255 | static int ssl_buffer_future_record( mbedtls_ssl_context *ssl, |
| 4256 | mbedtls_record const *rec ) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4257 | { |
| 4258 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4259 | |
| 4260 | /* Don't buffer future records outside handshakes. */ |
| 4261 | if( hs == NULL ) |
| 4262 | return( 0 ); |
| 4263 | |
| 4264 | /* Only buffer handshake records (we are only interested |
| 4265 | * in Finished messages). */ |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 4266 | if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE ) |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4267 | return( 0 ); |
| 4268 | |
| 4269 | /* Don't buffer more than one future epoch record. */ |
| 4270 | if( hs->buffering.future_record.data != NULL ) |
| 4271 | return( 0 ); |
| 4272 | |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 4273 | /* Don't buffer record if there's not enough buffering space remaining. */ |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 4274 | if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING - |
Hanno Becker | 01315ea | 2018-08-21 17:22:17 +0100 | [diff] [blame] | 4275 | hs->buffering.total_bytes_buffered ) ) |
| 4276 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 4277 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %" MBEDTLS_PRINTF_SIZET |
| 4278 | " would exceed the compile-time limit %" MBEDTLS_PRINTF_SIZET |
| 4279 | " (already %" MBEDTLS_PRINTF_SIZET |
| 4280 | " bytes buffered) -- ignore\n", |
Paul Elliott | 3891caf | 2020-12-17 18:42:40 +0000 | [diff] [blame] | 4281 | rec->buf_len, (size_t) MBEDTLS_SSL_DTLS_MAX_BUFFERING, |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 4282 | hs->buffering.total_bytes_buffered ) ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4283 | return( 0 ); |
| 4284 | } |
| 4285 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4286 | /* Buffer record */ |
| 4287 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u", |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 4288 | ssl->in_epoch + 1U ) ); |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 4289 | MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len ); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4290 | |
| 4291 | /* ssl_parse_record_header() only considers records |
| 4292 | * of the next epoch as candidates for buffering. */ |
| 4293 | hs->buffering.future_record.epoch = ssl->in_epoch + 1; |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 4294 | hs->buffering.future_record.len = rec->buf_len; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4295 | |
| 4296 | hs->buffering.future_record.data = |
| 4297 | mbedtls_calloc( 1, hs->buffering.future_record.len ); |
| 4298 | if( hs->buffering.future_record.data == NULL ) |
| 4299 | { |
| 4300 | /* If we run out of RAM trying to buffer a |
| 4301 | * record from the next epoch, just ignore. */ |
| 4302 | return( 0 ); |
| 4303 | } |
| 4304 | |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 4305 | memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len ); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4306 | |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 4307 | hs->buffering.total_bytes_buffered += rec->buf_len; |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4308 | return( 0 ); |
| 4309 | } |
| 4310 | |
| 4311 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4312 | |
Hanno Becker | e74d556 | 2018-08-15 14:26:08 +0100 | [diff] [blame] | 4313 | static int ssl_get_next_record( mbedtls_ssl_context *ssl ) |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 4314 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4315 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 4316 | mbedtls_record rec; |
Hanno Becker | 1097b34 | 2018-08-15 14:09:41 +0100 | [diff] [blame] | 4317 | |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4318 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4319 | /* We might have buffered a future record; if so, |
| 4320 | * and if the epoch matches now, load it. |
| 4321 | * On success, this call will set ssl->in_left to |
| 4322 | * the length of the buffered record, so that |
| 4323 | * the calls to ssl_fetch_input() below will |
| 4324 | * essentially be no-ops. */ |
| 4325 | ret = ssl_load_buffered_record( ssl ); |
| 4326 | if( ret != 0 ) |
| 4327 | return( ret ); |
| 4328 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4329 | |
Hanno Becker | ca59c2b | 2019-05-08 12:03:28 +0100 | [diff] [blame] | 4330 | /* Ensure that we have enough space available for the default form |
| 4331 | * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS, |
| 4332 | * with no space for CIDs counted in). */ |
| 4333 | ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) ); |
| 4334 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4335 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4336 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4337 | return( ret ); |
| 4338 | } |
| 4339 | |
Hanno Becker | e5e7e78 | 2019-07-11 12:29:35 +0100 | [diff] [blame] | 4340 | ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec ); |
| 4341 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4342 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4343 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 4344 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4345 | { |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4346 | if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE ) |
| 4347 | { |
Hanno Becker | 519f15d | 2019-07-11 12:43:20 +0100 | [diff] [blame] | 4348 | ret = ssl_buffer_future_record( ssl, &rec ); |
Hanno Becker | 5f066e7 | 2018-08-16 14:56:31 +0100 | [diff] [blame] | 4349 | if( ret != 0 ) |
| 4350 | return( ret ); |
| 4351 | |
| 4352 | /* Fall through to handling of unexpected records */ |
| 4353 | ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD; |
| 4354 | } |
| 4355 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4356 | if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ) |
| 4357 | { |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 4358 | #if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C) |
Hanno Becker | d8bf8ce | 2019-07-12 09:23:47 +0100 | [diff] [blame] | 4359 | /* Reset in pointers to default state for TLS/DTLS records, |
| 4360 | * assuming no CID and no offset between record content and |
| 4361 | * record plaintext. */ |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 4362 | mbedtls_ssl_update_in_pointers( ssl ); |
Hanno Becker | d8bf8ce | 2019-07-12 09:23:47 +0100 | [diff] [blame] | 4363 | |
Hanno Becker | 7ae20e0 | 2019-07-12 08:33:49 +0100 | [diff] [blame] | 4364 | /* Setup internal message pointers from record structure. */ |
| 4365 | ssl->in_msgtype = rec.type; |
| 4366 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4367 | ssl->in_len = ssl->in_cid + rec.cid_len; |
| 4368 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4369 | ssl->in_iv = ssl->in_msg = ssl->in_len + 2; |
| 4370 | ssl->in_msglen = rec.data_len; |
| 4371 | |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 4372 | ret = ssl_check_client_reconnect( ssl ); |
Manuel Pégourié-Gonnard | 243d70f | 2020-03-31 12:07:47 +0200 | [diff] [blame] | 4373 | MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_client_reconnect", ret ); |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 4374 | if( ret != 0 ) |
| 4375 | return( ret ); |
| 4376 | #endif |
| 4377 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4378 | /* Skip unexpected record (but not whole datagram) */ |
Hanno Becker | 4acada3 | 2019-07-11 12:48:53 +0100 | [diff] [blame] | 4379 | ssl->next_record_offset = rec.buf_len; |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4380 | |
Manuel Pégourié-Gonnard | e2e25e7 | 2015-12-03 16:13:17 +0100 | [diff] [blame] | 4381 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record " |
| 4382 | "(header)" ) ); |
| 4383 | } |
| 4384 | else |
| 4385 | { |
| 4386 | /* Skip invalid record and the rest of the datagram */ |
| 4387 | ssl->next_record_offset = 0; |
| 4388 | ssl->in_left = 0; |
| 4389 | |
| 4390 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record " |
| 4391 | "(header)" ) ); |
| 4392 | } |
| 4393 | |
| 4394 | /* Get next record */ |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 4395 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4396 | } |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 4397 | else |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4398 | #endif |
Hanno Becker | 2fddd37 | 2019-07-10 14:37:41 +0100 | [diff] [blame] | 4399 | { |
| 4400 | return( ret ); |
| 4401 | } |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4402 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4403 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4404 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4405 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 4406 | { |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 4407 | /* Remember offset of next record within datagram. */ |
Hanno Becker | f50da50 | 2019-07-11 12:50:10 +0100 | [diff] [blame] | 4408 | ssl->next_record_offset = rec.buf_len; |
Hanno Becker | e65ce78 | 2017-05-22 14:47:48 +0100 | [diff] [blame] | 4409 | if( ssl->next_record_offset < ssl->in_left ) |
| 4410 | { |
| 4411 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) ); |
| 4412 | } |
| 4413 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4414 | else |
| 4415 | #endif |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 4416 | { |
Hanno Becker | 955a5c9 | 2019-07-10 17:12:07 +0100 | [diff] [blame] | 4417 | /* |
| 4418 | * Fetch record contents from underlying transport. |
| 4419 | */ |
Hanno Becker | a317566 | 2019-07-11 12:50:29 +0100 | [diff] [blame] | 4420 | ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len ); |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 4421 | if( ret != 0 ) |
| 4422 | { |
| 4423 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret ); |
| 4424 | return( ret ); |
| 4425 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4426 | |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4427 | ssl->in_left = 0; |
Hanno Becker | a881479 | 2019-07-10 15:01:45 +0100 | [diff] [blame] | 4428 | } |
| 4429 | |
| 4430 | /* |
| 4431 | * Decrypt record contents. |
| 4432 | */ |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4433 | |
Hanno Becker | fdf6604 | 2019-07-11 13:07:45 +0100 | [diff] [blame] | 4434 | if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4435 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4436 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4437 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4438 | { |
| 4439 | /* Silently discard invalid records */ |
Hanno Becker | 82e2a39 | 2019-05-03 16:36:59 +0100 | [diff] [blame] | 4440 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4441 | { |
Manuel Pégourié-Gonnard | 0a88574 | 2015-08-04 12:08:35 +0200 | [diff] [blame] | 4442 | /* Except when waiting for Finished as a bad mac here |
| 4443 | * probably means something went wrong in the handshake |
| 4444 | * (eg wrong psk used, mitm downgrade attempt, etc.) */ |
| 4445 | if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED || |
| 4446 | ssl->state == MBEDTLS_SSL_SERVER_FINISHED ) |
| 4447 | { |
| 4448 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) |
| 4449 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
| 4450 | { |
| 4451 | mbedtls_ssl_send_alert_message( ssl, |
| 4452 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 4453 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC ); |
| 4454 | } |
| 4455 | #endif |
| 4456 | return( ret ); |
| 4457 | } |
| 4458 | |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4459 | if( ssl->conf->badmac_limit != 0 && |
| 4460 | ++ssl->badmac_seen >= ssl->conf->badmac_limit ) |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 4461 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4462 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) ); |
| 4463 | return( MBEDTLS_ERR_SSL_INVALID_MAC ); |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 4464 | } |
Manuel Pégourié-Gonnard | b0643d1 | 2014-10-14 18:30:36 +0200 | [diff] [blame] | 4465 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 4466 | /* As above, invalid records cause |
| 4467 | * dismissal of the whole datagram. */ |
| 4468 | |
| 4469 | ssl->next_record_offset = 0; |
| 4470 | ssl->in_left = 0; |
| 4471 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4472 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) ); |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 4473 | return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4474 | } |
| 4475 | |
| 4476 | return( ret ); |
| 4477 | } |
| 4478 | else |
| 4479 | #endif |
| 4480 | { |
| 4481 | /* Error out (and send alert) on invalid records */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4482 | #if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES) |
| 4483 | if( ret == MBEDTLS_ERR_SSL_INVALID_MAC ) |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4484 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4485 | mbedtls_ssl_send_alert_message( ssl, |
| 4486 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 4487 | MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC ); |
Manuel Pégourié-Gonnard | 63eca93 | 2014-09-08 16:39:08 +0200 | [diff] [blame] | 4488 | } |
| 4489 | #endif |
| 4490 | return( ret ); |
| 4491 | } |
| 4492 | } |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4493 | |
Hanno Becker | 44d89b2 | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 4494 | |
| 4495 | /* Reset in pointers to default state for TLS/DTLS records, |
| 4496 | * assuming no CID and no offset between record content and |
| 4497 | * record plaintext. */ |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 4498 | mbedtls_ssl_update_in_pointers( ssl ); |
Hanno Becker | 44d89b2 | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 4499 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4500 | ssl->in_len = ssl->in_cid + rec.cid_len; |
| 4501 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
irwir | 89af51f | 2019-09-26 21:04:56 +0300 | [diff] [blame] | 4502 | ssl->in_iv = ssl->in_len + 2; |
Hanno Becker | 44d89b2 | 2019-07-12 09:40:44 +0100 | [diff] [blame] | 4503 | |
Hanno Becker | 8685c82 | 2019-07-12 09:37:30 +0100 | [diff] [blame] | 4504 | /* The record content type may change during decryption, |
| 4505 | * so re-read it. */ |
| 4506 | ssl->in_msgtype = rec.type; |
| 4507 | /* Also update the input buffer, because unfortunately |
| 4508 | * the server-side ssl_parse_client_hello() reparses the |
| 4509 | * record header when receiving a ClientHello initiating |
| 4510 | * a renegotiation. */ |
| 4511 | ssl->in_hdr[0] = rec.type; |
| 4512 | ssl->in_msg = rec.buf + rec.data_offset; |
| 4513 | ssl->in_msglen = rec.data_len; |
| 4514 | ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 ); |
| 4515 | ssl->in_len[1] = (unsigned char)( rec.data_len ); |
| 4516 | |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4517 | return( 0 ); |
| 4518 | } |
| 4519 | |
| 4520 | int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl ) |
| 4521 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4522 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4523 | |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 4524 | /* |
Manuel Pégourié-Gonnard | 167a376 | 2014-09-08 16:14:10 +0200 | [diff] [blame] | 4525 | * Handle particular types of records |
| 4526 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4527 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4528 | { |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4529 | if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 ) |
| 4530 | { |
Manuel Pégourié-Gonnard | a59543a | 2014-02-18 11:33:49 +0100 | [diff] [blame] | 4531 | return( ret ); |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4532 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4533 | } |
| 4534 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 4535 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4536 | { |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 4537 | if( ssl->in_msglen != 1 ) |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4538 | { |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 4539 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %" MBEDTLS_PRINTF_SIZET, |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 4540 | ssl->in_msglen ) ); |
| 4541 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4542 | } |
| 4543 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 4544 | if( ssl->in_msg[0] != 1 ) |
| 4545 | { |
| 4546 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x", |
| 4547 | ssl->in_msg[0] ) ); |
| 4548 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4549 | } |
| 4550 | |
| 4551 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4552 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 4553 | ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC && |
| 4554 | ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC ) |
| 4555 | { |
| 4556 | if( ssl->handshake == NULL ) |
| 4557 | { |
| 4558 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) ); |
| 4559 | return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD ); |
| 4560 | } |
| 4561 | |
| 4562 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) ); |
| 4563 | return( MBEDTLS_ERR_SSL_EARLY_MESSAGE ); |
| 4564 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4565 | #endif |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 4566 | } |
Hanno Becker | 2ed6bcc | 2018-08-15 15:11:57 +0100 | [diff] [blame] | 4567 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4568 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4569 | { |
Angus Gratton | 1a7a17e | 2018-06-20 15:43:50 +1000 | [diff] [blame] | 4570 | if( ssl->in_msglen != 2 ) |
| 4571 | { |
| 4572 | /* Note: Standard allows for more than one 2 byte alert |
| 4573 | to be packed in a single message, but Mbed TLS doesn't |
| 4574 | currently support this. */ |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 4575 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %" MBEDTLS_PRINTF_SIZET, |
Angus Gratton | 1a7a17e | 2018-06-20 15:43:50 +1000 | [diff] [blame] | 4576 | ssl->in_msglen ) ); |
| 4577 | return( MBEDTLS_ERR_SSL_INVALID_RECORD ); |
| 4578 | } |
| 4579 | |
Paul Elliott | 9f35211 | 2020-12-09 14:55:45 +0000 | [diff] [blame] | 4580 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%u:%u]", |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4581 | ssl->in_msg[0], ssl->in_msg[1] ) ); |
| 4582 | |
| 4583 | /* |
Simon Butcher | 459a950 | 2015-10-27 16:09:03 +0000 | [diff] [blame] | 4584 | * Ignore non-fatal alerts, except close_notify and no_renegotiation |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4585 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4586 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4587 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4588 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)", |
Paul Bakker | 2770fbd | 2012-07-03 13:30:23 +0000 | [diff] [blame] | 4589 | ssl->in_msg[1] ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4590 | return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4591 | } |
| 4592 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4593 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 4594 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4595 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4596 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) ); |
| 4597 | return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4598 | } |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 4599 | |
| 4600 | #if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED) |
| 4601 | if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING && |
| 4602 | ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) |
| 4603 | { |
Mateusz Starzyk | f5c5351 | 2021-04-15 13:28:52 +0200 | [diff] [blame] | 4604 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a no renegotiation alert" ) ); |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 4605 | /* Will be handled when trying to parse ServerHello */ |
| 4606 | return( 0 ); |
| 4607 | } |
| 4608 | #endif |
Manuel Pégourié-Gonnard | fbdf06c | 2015-10-23 11:13:28 +0200 | [diff] [blame] | 4609 | /* Silently ignore: fetch new message */ |
Simon Butcher | 9900014 | 2016-10-13 17:21:01 +0100 | [diff] [blame] | 4610 | return MBEDTLS_ERR_SSL_NON_FATAL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4611 | } |
| 4612 | |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 4613 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 4614 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 4615 | { |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 4616 | /* Drop unexpected ApplicationData records, |
| 4617 | * except at the beginning of renegotiations */ |
| 4618 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA && |
| 4619 | ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER |
| 4620 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 4621 | && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS && |
| 4622 | ssl->state == MBEDTLS_SSL_SERVER_HELLO ) |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 4623 | #endif |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 4624 | ) |
| 4625 | { |
| 4626 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) ); |
| 4627 | return( MBEDTLS_ERR_SSL_NON_FATAL ); |
| 4628 | } |
| 4629 | |
| 4630 | if( ssl->handshake != NULL && |
| 4631 | ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
| 4632 | { |
Hanno Becker | ce5f5fd | 2020-02-05 10:47:44 +0000 | [diff] [blame] | 4633 | mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl ); |
Hanno Becker | 37ae952 | 2019-05-03 16:54:26 +0100 | [diff] [blame] | 4634 | } |
| 4635 | } |
Hanno Becker | 4a4af9f | 2019-05-08 16:26:21 +0100 | [diff] [blame] | 4636 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Hanno Becker | c76c619 | 2017-06-06 10:03:17 +0100 | [diff] [blame] | 4637 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4638 | return( 0 ); |
| 4639 | } |
| 4640 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4641 | int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl ) |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 4642 | { |
irwir | 6c0da64 | 2019-09-26 21:07:41 +0300 | [diff] [blame] | 4643 | return( mbedtls_ssl_send_alert_message( ssl, |
| 4644 | MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 4645 | MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ); |
Paul Bakker | d0f6fa7 | 2012-09-17 09:18:12 +0000 | [diff] [blame] | 4646 | } |
| 4647 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4648 | int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl, |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 4649 | unsigned char level, |
| 4650 | unsigned char message ) |
| 4651 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4652 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 4653 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 4654 | if( ssl == NULL || ssl->conf == NULL ) |
| 4655 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4656 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4657 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 4658 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message )); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 4659 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4660 | ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT; |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 4661 | ssl->out_msglen = 2; |
| 4662 | ssl->out_msg[0] = level; |
| 4663 | ssl->out_msg[1] = message; |
| 4664 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 4665 | if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 ) |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 4666 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4667 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 4668 | return( ret ); |
| 4669 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4670 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) ); |
Paul Bakker | 0a92518 | 2012-04-16 06:46:41 +0000 | [diff] [blame] | 4671 | |
| 4672 | return( 0 ); |
| 4673 | } |
| 4674 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4675 | int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4676 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4677 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4678 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4679 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4680 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4681 | ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4682 | ssl->out_msglen = 1; |
| 4683 | ssl->out_msg[0] = 1; |
| 4684 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4685 | ssl->state++; |
| 4686 | |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4687 | if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4688 | { |
Manuel Pégourié-Gonnard | 31c1586 | 2017-09-13 09:38:11 +0200 | [diff] [blame] | 4689 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4690 | return( ret ); |
| 4691 | } |
| 4692 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4693 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4694 | |
| 4695 | return( 0 ); |
| 4696 | } |
| 4697 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4698 | int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4699 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 4700 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4701 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4702 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4703 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 4704 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4705 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4706 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4707 | return( ret ); |
| 4708 | } |
| 4709 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4710 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4711 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4712 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 4713 | mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, |
| 4714 | MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4715 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4716 | } |
| 4717 | |
Hanno Becker | e678eaa | 2018-08-21 14:57:46 +0100 | [diff] [blame] | 4718 | /* CCS records are only accepted if they have length 1 and content '1', |
| 4719 | * so we don't need to check this here. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4720 | |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4721 | /* |
| 4722 | * Switch to our negotiated transform and session parameters for inbound |
| 4723 | * data. |
| 4724 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4725 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4726 | ssl->transform_in = ssl->transform_negotiate; |
| 4727 | ssl->session_in = ssl->session_negotiate; |
| 4728 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4729 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 4730 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4731 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4732 | #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) |
Hanno Becker | 7e8e6a6 | 2020-02-05 10:45:48 +0000 | [diff] [blame] | 4733 | mbedtls_ssl_dtls_replay_reset( ssl ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4734 | #endif |
| 4735 | |
| 4736 | /* Increment epoch */ |
| 4737 | if( ++ssl->in_epoch == 0 ) |
| 4738 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4739 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) ); |
Gilles Peskine | 1cc8e34 | 2017-05-03 16:28:34 +0200 | [diff] [blame] | 4740 | /* This is highly unlikely to happen for legitimate reasons, so |
| 4741 | treat it as an attack and don't send an alert. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4742 | return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4743 | } |
| 4744 | } |
| 4745 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4746 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4747 | memset( ssl->in_ctr, 0, 8 ); |
| 4748 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 4749 | mbedtls_ssl_update_in_pointers( ssl ); |
Manuel Pégourié-Gonnard | 246c13a | 2014-09-24 13:56:09 +0200 | [diff] [blame] | 4750 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4751 | ssl->state++; |
| 4752 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4753 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4754 | |
| 4755 | return( 0 ); |
| 4756 | } |
| 4757 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4758 | /* Once ssl->out_hdr as the address of the beginning of the |
| 4759 | * next outgoing record is set, deduce the other pointers. |
| 4760 | * |
| 4761 | * Note: For TLS, we save the implicit record sequence number |
| 4762 | * (entering MAC computation) in the 8 bytes before ssl->out_hdr, |
| 4763 | * and the caller has to make sure there's space for this. |
| 4764 | */ |
| 4765 | |
Hanno Becker | c0eefa8 | 2020-05-28 07:17:36 +0100 | [diff] [blame] | 4766 | static size_t ssl_transform_get_explicit_iv_len( |
| 4767 | mbedtls_ssl_transform const *transform ) |
| 4768 | { |
TRodziewicz | ef73f01 | 2021-05-13 14:53:36 +0200 | [diff] [blame] | 4769 | if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ) |
Hanno Becker | c0eefa8 | 2020-05-28 07:17:36 +0100 | [diff] [blame] | 4770 | return( 0 ); |
| 4771 | |
| 4772 | return( transform->ivlen - transform->fixed_ivlen ); |
| 4773 | } |
| 4774 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 4775 | void mbedtls_ssl_update_out_pointers( mbedtls_ssl_context *ssl, |
| 4776 | mbedtls_ssl_transform *transform ) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4777 | { |
| 4778 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4779 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 4780 | { |
| 4781 | ssl->out_ctr = ssl->out_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4782 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4783 | ssl->out_cid = ssl->out_ctr + 8; |
| 4784 | ssl->out_len = ssl->out_cid; |
| 4785 | if( transform != NULL ) |
| 4786 | ssl->out_len += transform->out_cid_len; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4787 | #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4788 | ssl->out_len = ssl->out_ctr + 8; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4789 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4790 | ssl->out_iv = ssl->out_len + 2; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4791 | } |
| 4792 | else |
| 4793 | #endif |
| 4794 | { |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4795 | ssl->out_len = ssl->out_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4796 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 4c3eb7c | 2019-05-08 16:43:21 +0100 | [diff] [blame] | 4797 | ssl->out_cid = ssl->out_len; |
| 4798 | #endif |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4799 | ssl->out_iv = ssl->out_hdr + 5; |
| 4800 | } |
| 4801 | |
Hanno Becker | c0eefa8 | 2020-05-28 07:17:36 +0100 | [diff] [blame] | 4802 | ssl->out_msg = ssl->out_iv; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4803 | /* Adjust out_msg to make space for explicit IV, if used. */ |
Hanno Becker | c0eefa8 | 2020-05-28 07:17:36 +0100 | [diff] [blame] | 4804 | if( transform != NULL ) |
| 4805 | ssl->out_msg += ssl_transform_get_explicit_iv_len( transform ); |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4806 | } |
| 4807 | |
| 4808 | /* Once ssl->in_hdr as the address of the beginning of the |
| 4809 | * next incoming record is set, deduce the other pointers. |
| 4810 | * |
| 4811 | * Note: For TLS, we save the implicit record sequence number |
| 4812 | * (entering MAC computation) in the 8 bytes before ssl->in_hdr, |
| 4813 | * and the caller has to make sure there's space for this. |
| 4814 | */ |
| 4815 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 4816 | void mbedtls_ssl_update_in_pointers( mbedtls_ssl_context *ssl ) |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4817 | { |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 4818 | /* This function sets the pointers to match the case |
| 4819 | * of unprotected TLS/DTLS records, with both ssl->in_iv |
| 4820 | * and ssl->in_msg pointing to the beginning of the record |
| 4821 | * content. |
| 4822 | * |
| 4823 | * When decrypting a protected record, ssl->in_msg |
| 4824 | * will be shifted to point to the beginning of the |
| 4825 | * record plaintext. |
| 4826 | */ |
| 4827 | |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4828 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4829 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 4830 | { |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4831 | /* This sets the header pointers to match records |
| 4832 | * without CID. When we receive a record containing |
| 4833 | * a CID, the fields are shifted accordingly in |
| 4834 | * ssl_parse_record_header(). */ |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4835 | ssl->in_ctr = ssl->in_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4836 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4837 | ssl->in_cid = ssl->in_ctr + 8; |
| 4838 | ssl->in_len = ssl->in_cid; /* Default: no CID */ |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4839 | #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4840 | ssl->in_len = ssl->in_ctr + 8; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4841 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | f9c6a4b | 2019-05-03 14:34:53 +0100 | [diff] [blame] | 4842 | ssl->in_iv = ssl->in_len + 2; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4843 | } |
| 4844 | else |
| 4845 | #endif |
| 4846 | { |
| 4847 | ssl->in_ctr = ssl->in_hdr - 8; |
| 4848 | ssl->in_len = ssl->in_hdr + 3; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4849 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 4c3eb7c | 2019-05-08 16:43:21 +0100 | [diff] [blame] | 4850 | ssl->in_cid = ssl->in_len; |
| 4851 | #endif |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4852 | ssl->in_iv = ssl->in_hdr + 5; |
| 4853 | } |
| 4854 | |
Hanno Becker | 79594fd | 2019-05-08 09:38:41 +0100 | [diff] [blame] | 4855 | /* This will be adjusted at record decryption time. */ |
| 4856 | ssl->in_msg = ssl->in_iv; |
Hanno Becker | 5aa4e2c | 2018-08-06 09:26:08 +0100 | [diff] [blame] | 4857 | } |
| 4858 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4859 | /* |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 4860 | * Setup an SSL context |
| 4861 | */ |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 4862 | |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 4863 | void mbedtls_ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl ) |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 4864 | { |
| 4865 | /* Set the incoming and outgoing record pointers. */ |
| 4866 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4867 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 4868 | { |
| 4869 | ssl->out_hdr = ssl->out_buf; |
| 4870 | ssl->in_hdr = ssl->in_buf; |
| 4871 | } |
| 4872 | else |
| 4873 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4874 | { |
Hanno Becker | 12078f4 | 2021-03-02 15:28:41 +0000 | [diff] [blame] | 4875 | ssl->out_ctr = ssl->out_buf; |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 4876 | ssl->out_hdr = ssl->out_buf + 8; |
| 4877 | ssl->in_hdr = ssl->in_buf + 8; |
| 4878 | } |
| 4879 | |
| 4880 | /* Derive other internal pointers. */ |
Hanno Becker | 3e6f8ab | 2020-02-05 10:40:57 +0000 | [diff] [blame] | 4881 | mbedtls_ssl_update_out_pointers( ssl, NULL /* no transform enabled */ ); |
| 4882 | mbedtls_ssl_update_in_pointers ( ssl ); |
Hanno Becker | 2a43f6f | 2018-08-10 11:12:52 +0100 | [diff] [blame] | 4883 | } |
| 4884 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4885 | /* |
| 4886 | * SSL get accessors |
| 4887 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4888 | size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 4889 | { |
| 4890 | return( ssl->in_offt == NULL ? 0 : ssl->in_msglen ); |
| 4891 | } |
| 4892 | |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 4893 | int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl ) |
| 4894 | { |
| 4895 | /* |
| 4896 | * Case A: We're currently holding back |
| 4897 | * a message for further processing. |
| 4898 | */ |
| 4899 | |
| 4900 | if( ssl->keep_current_message == 1 ) |
| 4901 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 4902 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) ); |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 4903 | return( 1 ); |
| 4904 | } |
| 4905 | |
| 4906 | /* |
| 4907 | * Case B: Further records are pending in the current datagram. |
| 4908 | */ |
| 4909 | |
| 4910 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 4911 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 4912 | ssl->in_left > ssl->next_record_offset ) |
| 4913 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 4914 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) ); |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 4915 | return( 1 ); |
| 4916 | } |
| 4917 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 4918 | |
| 4919 | /* |
| 4920 | * Case C: A handshake message is being processed. |
| 4921 | */ |
| 4922 | |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 4923 | if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen ) |
| 4924 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 4925 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) ); |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 4926 | return( 1 ); |
| 4927 | } |
| 4928 | |
| 4929 | /* |
| 4930 | * Case D: An application data message is being processed |
| 4931 | */ |
| 4932 | if( ssl->in_offt != NULL ) |
| 4933 | { |
Hanno Becker | a6fb089 | 2017-10-23 13:17:48 +0100 | [diff] [blame] | 4934 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) ); |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 4935 | return( 1 ); |
| 4936 | } |
| 4937 | |
| 4938 | /* |
| 4939 | * In all other cases, the rest of the message can be dropped. |
Hanno Becker | c573ac3 | 2018-08-28 17:15:25 +0100 | [diff] [blame] | 4940 | * As in ssl_get_next_record, this needs to be adapted if |
Hanno Becker | 8b170a0 | 2017-10-10 11:51:19 +0100 | [diff] [blame] | 4941 | * we implement support for multiple alerts in single records. |
| 4942 | */ |
| 4943 | |
| 4944 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) ); |
| 4945 | return( 0 ); |
| 4946 | } |
| 4947 | |
Paul Bakker | 43ca69c | 2011-01-15 17:35:19 +0000 | [diff] [blame] | 4948 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4949 | 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] | 4950 | { |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 4951 | size_t transform_expansion = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4952 | const mbedtls_ssl_transform *transform = ssl->transform_out; |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 4953 | unsigned block_size; |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 4954 | |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 4955 | size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl ); |
| 4956 | |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 4957 | if( transform == NULL ) |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 4958 | return( (int) out_hdr_len ); |
Hanno Becker | 7864090 | 2018-08-13 16:35:15 +0100 | [diff] [blame] | 4959 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4960 | switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) ) |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 4961 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4962 | case MBEDTLS_MODE_GCM: |
| 4963 | case MBEDTLS_MODE_CCM: |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 4964 | case MBEDTLS_MODE_CHACHAPOLY: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4965 | case MBEDTLS_MODE_STREAM: |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 4966 | transform_expansion = transform->minlen; |
| 4967 | break; |
| 4968 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4969 | case MBEDTLS_MODE_CBC: |
Hanno Becker | 5b559ac | 2018-08-03 09:40:07 +0100 | [diff] [blame] | 4970 | |
| 4971 | block_size = mbedtls_cipher_get_block_size( |
| 4972 | &transform->cipher_ctx_enc ); |
| 4973 | |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 4974 | /* Expansion due to the addition of the MAC. */ |
| 4975 | transform_expansion += transform->maclen; |
| 4976 | |
| 4977 | /* Expansion due to the addition of CBC padding; |
| 4978 | * Theoretically up to 256 bytes, but we never use |
| 4979 | * more than the block size of the underlying cipher. */ |
| 4980 | transform_expansion += block_size; |
| 4981 | |
TRodziewicz | 4ca18aa | 2021-05-20 14:46:20 +0200 | [diff] [blame] | 4982 | /* For TLS 1.2 or higher, an explicit IV is added |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 4983 | * after the record header. */ |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 4984 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 4985 | transform_expansion += block_size; |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 4986 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | 3136ede | 2018-08-17 15:28:19 +0100 | [diff] [blame] | 4987 | |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 4988 | break; |
| 4989 | |
| 4990 | default: |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 4991 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 4992 | return( MBEDTLS_ERR_SSL_INTERNAL_ERROR ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 4993 | } |
| 4994 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4995 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | 6cbad55 | 2019-05-08 15:40:11 +0100 | [diff] [blame] | 4996 | if( transform->out_cid_len != 0 ) |
| 4997 | transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 4998 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | 6cbad55 | 2019-05-08 15:40:11 +0100 | [diff] [blame] | 4999 | |
Hanno Becker | 5903de4 | 2019-05-03 14:46:38 +0100 | [diff] [blame] | 5000 | return( (int)( out_hdr_len + transform_expansion ) ); |
Manuel Pégourié-Gonnard | 9b35f18 | 2014-10-14 17:47:31 +0200 | [diff] [blame] | 5001 | } |
| 5002 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5003 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 214eed3 | 2013-10-30 13:06:54 +0100 | [diff] [blame] | 5004 | /* |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 5005 | * Check record counters and renegotiate if they're above the limit. |
| 5006 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5007 | static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 5008 | { |
Hanno Becker | dd77229 | 2020-02-05 10:38:31 +0000 | [diff] [blame] | 5009 | size_t ep_len = mbedtls_ssl_ep_len( ssl ); |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 5010 | int in_ctr_cmp; |
| 5011 | int out_ctr_cmp; |
| 5012 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5013 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER || |
| 5014 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING || |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5015 | ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 5016 | { |
| 5017 | return( 0 ); |
| 5018 | } |
| 5019 | |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 5020 | in_ctr_cmp = memcmp( ssl->in_ctr + ep_len, |
| 5021 | ssl->conf->renego_period + ep_len, 8 - ep_len ); |
Hanno Becker | 1985947 | 2018-08-06 09:40:20 +0100 | [diff] [blame] | 5022 | out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len, |
Andres AG | 2196c7f | 2016-12-15 17:01:16 +0000 | [diff] [blame] | 5023 | ssl->conf->renego_period + ep_len, 8 - ep_len ); |
| 5024 | |
| 5025 | if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 5026 | { |
| 5027 | return( 0 ); |
| 5028 | } |
| 5029 | |
Manuel Pégourié-Gonnard | cb0d212 | 2015-07-22 11:52:11 +0200 | [diff] [blame] | 5030 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5031 | return( mbedtls_ssl_renegotiate( ssl ) ); |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 5032 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5033 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5034 | |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 5035 | /* 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] | 5036 | * received after the initial handshake. In this context, handshake messages |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 5037 | * may only be sent for the purpose of initiating renegotiations. |
| 5038 | * |
| 5039 | * This function is introduced as a separate helper since the handling |
| 5040 | * of post-handshake handshake messages changes significantly in TLS 1.3, |
| 5041 | * and having a helper function allows to distinguish between TLS <= 1.2 and |
| 5042 | * TLS 1.3 in the future without bloating the logic of mbedtls_ssl_read(). |
| 5043 | */ |
Hanno Becker | cad3dba | 2020-11-24 06:57:13 +0000 | [diff] [blame] | 5044 | static int ssl_handle_hs_message_post_handshake( mbedtls_ssl_context *ssl ) |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 5045 | { |
Hanno Becker | fae12cf | 2021-04-21 07:20:20 +0100 | [diff] [blame] | 5046 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 5047 | |
| 5048 | /* |
| 5049 | * - For client-side, expect SERVER_HELLO_REQUEST. |
| 5050 | * - For server-side, expect CLIENT_HELLO. |
| 5051 | * - Fail (TLS) or silently drop record (DTLS) in other cases. |
| 5052 | */ |
| 5053 | |
| 5054 | #if defined(MBEDTLS_SSL_CLI_C) |
| 5055 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT && |
| 5056 | ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST || |
| 5057 | ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) ) |
| 5058 | { |
| 5059 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) ); |
| 5060 | |
| 5061 | /* With DTLS, drop the packet (probably from last handshake) */ |
| 5062 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5063 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 5064 | { |
| 5065 | return( 0 ); |
| 5066 | } |
| 5067 | #endif |
| 5068 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 5069 | } |
| 5070 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 5071 | |
| 5072 | #if defined(MBEDTLS_SSL_SRV_C) |
| 5073 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
| 5074 | ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) |
| 5075 | { |
| 5076 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) ); |
| 5077 | |
| 5078 | /* With DTLS, drop the packet (probably from last handshake) */ |
| 5079 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5080 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 5081 | { |
| 5082 | return( 0 ); |
| 5083 | } |
| 5084 | #endif |
| 5085 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
| 5086 | } |
| 5087 | #endif /* MBEDTLS_SSL_SRV_C */ |
| 5088 | |
| 5089 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 5090 | /* Determine whether renegotiation attempt should be accepted */ |
| 5091 | if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED || |
| 5092 | ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION && |
| 5093 | ssl->conf->allow_legacy_renegotiation == |
| 5094 | MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) ) |
| 5095 | { |
| 5096 | /* |
| 5097 | * Accept renegotiation request |
| 5098 | */ |
| 5099 | |
| 5100 | /* DTLS clients need to know renego is server-initiated */ |
| 5101 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5102 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM && |
| 5103 | ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 5104 | { |
| 5105 | ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING; |
| 5106 | } |
| 5107 | #endif |
| 5108 | ret = mbedtls_ssl_start_renegotiation( ssl ); |
| 5109 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 5110 | ret != 0 ) |
| 5111 | { |
| 5112 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", |
| 5113 | ret ); |
| 5114 | return( ret ); |
| 5115 | } |
| 5116 | } |
| 5117 | else |
| 5118 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
| 5119 | { |
| 5120 | /* |
| 5121 | * Refuse renegotiation |
| 5122 | */ |
| 5123 | |
| 5124 | MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) ); |
| 5125 | |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 5126 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 5127 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, |
| 5128 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, |
| 5129 | MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 ) |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 5130 | { |
TRodziewicz | 345165c | 2021-07-06 13:42:11 +0200 | [diff] [blame] | 5131 | return( ret ); |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 5132 | } |
TRodziewicz | 0f82ec6 | 2021-05-12 17:49:18 +0200 | [diff] [blame] | 5133 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 5134 | } |
| 5135 | |
| 5136 | return( 0 ); |
| 5137 | } |
| 5138 | |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5139 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5140 | * Receive application data decrypted from the SSL layer |
| 5141 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5142 | 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] | 5143 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5144 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 5145 | size_t n; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5146 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5147 | if( ssl == NULL || ssl->conf == NULL ) |
| 5148 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5149 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5150 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5151 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5152 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5153 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 5154 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5155 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 5156 | return( ret ); |
| 5157 | |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 5158 | if( ssl->handshake != NULL && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5159 | ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 5160 | { |
Manuel Pégourié-Gonnard | 87a346f | 2017-09-13 12:45:21 +0200 | [diff] [blame] | 5161 | if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 5162 | return( ret ); |
| 5163 | } |
Manuel Pégourié-Gonnard | abf1624 | 2014-09-23 09:42:16 +0200 | [diff] [blame] | 5164 | } |
| 5165 | #endif |
| 5166 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5167 | /* |
| 5168 | * Check if renegotiation is necessary and/or handshake is |
| 5169 | * in process. If yes, perform/continue, and fall through |
| 5170 | * if an unexpected packet is received while the client |
| 5171 | * is waiting for the ServerHello. |
| 5172 | * |
| 5173 | * (There is no equivalent to the last condition on |
| 5174 | * the server-side as it is not treated as within |
| 5175 | * a handshake while waiting for the ClientHello |
| 5176 | * after a renegotiation request.) |
| 5177 | */ |
| 5178 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5179 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5180 | ret = ssl_check_ctr_renegotiate( ssl ); |
| 5181 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 5182 | ret != 0 ) |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 5183 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5184 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret ); |
Manuel Pégourié-Gonnard | b445805 | 2014-11-04 21:04:22 +0100 | [diff] [blame] | 5185 | return( ret ); |
| 5186 | } |
| 5187 | #endif |
| 5188 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5189 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5190 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5191 | ret = mbedtls_ssl_handshake( ssl ); |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5192 | if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO && |
| 5193 | ret != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5194 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5195 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5196 | return( ret ); |
| 5197 | } |
| 5198 | } |
| 5199 | |
Hanno Becker | e41158b | 2017-10-23 13:30:32 +0100 | [diff] [blame] | 5200 | /* Loop as long as no application data record is available */ |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5201 | while( ssl->in_offt == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5202 | { |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 5203 | /* Start timer if not already running */ |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 5204 | if( ssl->f_get_timer != NULL && |
| 5205 | ssl->f_get_timer( ssl->p_timer ) == -1 ) |
| 5206 | { |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 5207 | mbedtls_ssl_set_timer( ssl, ssl->conf->read_timeout ); |
Manuel Pégourié-Gonnard | 545102e | 2015-05-13 17:28:43 +0200 | [diff] [blame] | 5208 | } |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 5209 | |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 5210 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5211 | { |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5212 | if( ret == MBEDTLS_ERR_SSL_CONN_EOF ) |
| 5213 | return( 0 ); |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 5214 | |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5215 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
| 5216 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5217 | } |
| 5218 | |
| 5219 | if( ssl->in_msglen == 0 && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5220 | ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5221 | { |
| 5222 | /* |
| 5223 | * OpenSSL sends empty messages to randomize the IV |
| 5224 | */ |
Hanno Becker | 327c93b | 2018-08-15 13:56:18 +0100 | [diff] [blame] | 5225 | if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5226 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5227 | if( ret == MBEDTLS_ERR_SSL_CONN_EOF ) |
Paul Bakker | 831a755 | 2011-05-18 13:32:51 +0000 | [diff] [blame] | 5228 | return( 0 ); |
| 5229 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5230 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5231 | return( ret ); |
| 5232 | } |
| 5233 | } |
| 5234 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5235 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5236 | { |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 5237 | ret = ssl_handle_hs_message_post_handshake( ssl ); |
| 5238 | if( ret != 0) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5239 | { |
Hanno Becker | b03f88f | 2020-11-24 06:41:37 +0000 | [diff] [blame] | 5240 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_handle_hs_message_post_handshake", |
| 5241 | ret ); |
| 5242 | return( ret ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5243 | } |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 5244 | |
Hanno Becker | f26cc72 | 2021-04-21 07:30:13 +0100 | [diff] [blame] | 5245 | /* At this point, we don't know whether the renegotiation triggered |
| 5246 | * by the post-handshake message has been completed or not. The cases |
| 5247 | * to consider are the following: |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5248 | * 1) The renegotiation is complete. In this case, no new record |
| 5249 | * has been read yet. |
| 5250 | * 2) The renegotiation is incomplete because the client received |
| 5251 | * an application data record while awaiting the ServerHello. |
| 5252 | * 3) The renegotiation is incomplete because the client received |
| 5253 | * a non-handshake, non-application data message while awaiting |
| 5254 | * the ServerHello. |
Hanno Becker | f26cc72 | 2021-04-21 07:30:13 +0100 | [diff] [blame] | 5255 | * |
| 5256 | * In each of these cases, looping will be the proper action: |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5257 | * - For 1), the next iteration will read a new record and check |
| 5258 | * if it's application data. |
| 5259 | * - For 2), the loop condition isn't satisfied as application data |
| 5260 | * is present, hence continue is the same as break |
| 5261 | * - For 3), the loop condition is satisfied and read_record |
| 5262 | * will re-deliver the message that was held back by the client |
| 5263 | * when expecting the ServerHello. |
| 5264 | */ |
Hanno Becker | f26cc72 | 2021-04-21 07:30:13 +0100 | [diff] [blame] | 5265 | |
Hanno Becker | 90333da | 2017-10-10 11:27:13 +0100 | [diff] [blame] | 5266 | continue; |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5267 | } |
Hanno Becker | 21df7f9 | 2017-10-17 11:03:26 +0100 | [diff] [blame] | 5268 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5269 | else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 5270 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5271 | if( ssl->conf->renego_max_records >= 0 ) |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 5272 | { |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5273 | if( ++ssl->renego_records_seen > ssl->conf->renego_max_records ) |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 5274 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5275 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, " |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 5276 | "but not honored by client" ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5277 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Manuel Pégourié-Gonnard | df3acd8 | 2014-10-15 15:07:45 +0200 | [diff] [blame] | 5278 | } |
Manuel Pégourié-Gonnard | a9964db | 2014-07-03 19:29:16 +0200 | [diff] [blame] | 5279 | } |
Manuel Pégourié-Gonnard | 6d8404d | 2013-10-30 16:41:45 +0100 | [diff] [blame] | 5280 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5281 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 5282 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5283 | /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */ |
| 5284 | if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT ) |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 5285 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5286 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) ); |
Manuel Pégourié-Gonnard | 8836994 | 2015-05-06 16:19:31 +0100 | [diff] [blame] | 5287 | return( MBEDTLS_ERR_SSL_WANT_READ ); |
Manuel Pégourié-Gonnard | f26a1e8 | 2014-08-19 12:28:50 +0200 | [diff] [blame] | 5288 | } |
| 5289 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5290 | if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5291 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5292 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) ); |
| 5293 | return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5294 | } |
| 5295 | |
| 5296 | ssl->in_offt = ssl->in_msg; |
Manuel Pégourié-Gonnard | 6b65141 | 2014-10-01 18:29:03 +0200 | [diff] [blame] | 5297 | |
Manuel Pégourié-Gonnard | ba958b8 | 2014-10-09 16:13:44 +0200 | [diff] [blame] | 5298 | /* We're going to return something now, cancel timer, |
| 5299 | * except if handshake (renegotiation) is in progress */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5300 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
Hanno Becker | 0f57a65 | 2020-02-05 10:37:26 +0000 | [diff] [blame] | 5301 | mbedtls_ssl_set_timer( ssl, 0 ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 5302 | |
Manuel Pégourié-Gonnard | 286a136 | 2015-05-13 16:22:05 +0200 | [diff] [blame] | 5303 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 5304 | /* If we requested renego but received AppData, resend HelloRequest. |
| 5305 | * Do it now, after setting in_offt, to avoid taking this branch |
| 5306 | * again if ssl_write_hello_request() returns WANT_WRITE */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5307 | #if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5308 | if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5309 | ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 5310 | { |
Hanno Becker | 786300f | 2020-02-05 10:46:40 +0000 | [diff] [blame] | 5311 | if( ( ret = mbedtls_ssl_resend_hello_request( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 5312 | { |
Hanno Becker | 786300f | 2020-02-05 10:46:40 +0000 | [diff] [blame] | 5313 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend_hello_request", |
| 5314 | ret ); |
Manuel Pégourié-Gonnard | 26a4cf6 | 2014-10-15 13:52:48 +0200 | [diff] [blame] | 5315 | return( ret ); |
| 5316 | } |
| 5317 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5318 | #endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */ |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5319 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5320 | } |
| 5321 | |
| 5322 | n = ( len < ssl->in_msglen ) |
| 5323 | ? len : ssl->in_msglen; |
| 5324 | |
| 5325 | memcpy( buf, ssl->in_offt, n ); |
| 5326 | ssl->in_msglen -= n; |
| 5327 | |
gabor-mezei-arm | a321413 | 2020-07-15 10:55:00 +0200 | [diff] [blame] | 5328 | /* Zeroising the plaintext buffer to erase unused application data |
| 5329 | from the memory. */ |
| 5330 | mbedtls_platform_zeroize( ssl->in_offt, n ); |
| 5331 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5332 | if( ssl->in_msglen == 0 ) |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5333 | { |
| 5334 | /* all bytes consumed */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5335 | ssl->in_offt = NULL; |
Hanno Becker | bdf3905 | 2017-06-09 10:42:03 +0100 | [diff] [blame] | 5336 | ssl->keep_current_message = 0; |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5337 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5338 | else |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5339 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5340 | /* more data available */ |
| 5341 | ssl->in_offt += n; |
Hanno Becker | 4a810fb | 2017-05-24 16:27:30 +0100 | [diff] [blame] | 5342 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5343 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5344 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5345 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 5346 | return( (int) n ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5347 | } |
| 5348 | |
| 5349 | /* |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 5350 | * Send application data to be encrypted by the SSL layer, taking care of max |
| 5351 | * fragment length and buffer size. |
| 5352 | * |
| 5353 | * According to RFC 5246 Section 6.2.1: |
| 5354 | * |
| 5355 | * Zero-length fragments of Application data MAY be sent as they are |
| 5356 | * potentially useful as a traffic analysis countermeasure. |
| 5357 | * |
| 5358 | * Therefore, it is possible that the input message length is 0 and the |
| 5359 | * corresponding return code is 0 on success. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5360 | */ |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 5361 | static int ssl_write_real( mbedtls_ssl_context *ssl, |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5362 | const unsigned char *buf, size_t len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5363 | { |
Manuel Pégourié-Gonnard | 9468ff1 | 2017-09-21 13:49:50 +0200 | [diff] [blame] | 5364 | int ret = mbedtls_ssl_get_max_out_record_payload( ssl ); |
| 5365 | const size_t max_len = (size_t) ret; |
| 5366 | |
| 5367 | if( ret < 0 ) |
| 5368 | { |
| 5369 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret ); |
| 5370 | return( ret ); |
| 5371 | } |
| 5372 | |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 5373 | if( len > max_len ) |
| 5374 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5375 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
Manuel Pégourié-Gonnard | 7ca4e4d | 2015-05-04 10:55:58 +0200 | [diff] [blame] | 5376 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 5377 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5378 | MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) " |
Paul Elliott | d48d5c6 | 2021-01-07 14:47:05 +0000 | [diff] [blame] | 5379 | "maximum fragment length: %" MBEDTLS_PRINTF_SIZET |
| 5380 | " > %" MBEDTLS_PRINTF_SIZET, |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 5381 | len, max_len ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5382 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 5383 | } |
| 5384 | else |
| 5385 | #endif |
| 5386 | len = max_len; |
| 5387 | } |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 5388 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5389 | if( ssl->out_left != 0 ) |
| 5390 | { |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 5391 | /* |
| 5392 | * The user has previously tried to send the data and |
| 5393 | * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially |
| 5394 | * written. In this case, we expect the high-level write function |
| 5395 | * (e.g. mbedtls_ssl_write()) to be called with the same parameters |
| 5396 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5397 | if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5398 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5399 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5400 | return( ret ); |
| 5401 | } |
| 5402 | } |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 5403 | else |
Paul Bakker | 1fd00bf | 2011-03-14 20:50:15 +0000 | [diff] [blame] | 5404 | { |
Andres Amaya Garcia | 5b92352 | 2017-09-28 14:41:17 +0100 | [diff] [blame] | 5405 | /* |
| 5406 | * The user is trying to send a message the first time, so we need to |
| 5407 | * copy the data into the internal buffers and setup the data structure |
| 5408 | * to keep track of partial writes |
| 5409 | */ |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 5410 | ssl->out_msglen = len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5411 | ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 5412 | memcpy( ssl->out_msg, buf, len ); |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 5413 | |
Hanno Becker | 67bc7c3 | 2018-08-06 11:33:50 +0100 | [diff] [blame] | 5414 | if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 ) |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 5415 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5416 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret ); |
Paul Bakker | 887bd50 | 2011-06-08 13:10:54 +0000 | [diff] [blame] | 5417 | return( ret ); |
| 5418 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5419 | } |
| 5420 | |
Manuel Pégourié-Gonnard | 37e08e1 | 2014-10-13 17:55:52 +0200 | [diff] [blame] | 5421 | return( (int) len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5422 | } |
| 5423 | |
| 5424 | /* |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5425 | * Write application data (public-facing wrapper) |
| 5426 | */ |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 5427 | int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len ) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5428 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5429 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5430 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 5431 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5432 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5433 | if( ssl == NULL || ssl->conf == NULL ) |
| 5434 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5435 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 5436 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5437 | if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 ) |
| 5438 | { |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 5439 | MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5440 | return( ret ); |
| 5441 | } |
| 5442 | #endif |
| 5443 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 5444 | if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5445 | { |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 5446 | if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 ) |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5447 | { |
Manuel Pégourié-Gonnard | 151dc77 | 2015-05-14 13:55:51 +0200 | [diff] [blame] | 5448 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5449 | return( ret ); |
| 5450 | } |
| 5451 | } |
| 5452 | |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5453 | ret = ssl_write_real( ssl, buf, len ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5454 | |
Manuel Pégourié-Gonnard | 144bc22 | 2015-04-17 20:39:07 +0200 | [diff] [blame] | 5455 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) ); |
Manuel Pégourié-Gonnard | a2fce21 | 2015-04-15 19:09:03 +0200 | [diff] [blame] | 5456 | |
| 5457 | return( ret ); |
| 5458 | } |
| 5459 | |
| 5460 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5461 | * Notify the peer that the connection is being closed |
| 5462 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5463 | int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5464 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 5465 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5466 | |
Manuel Pégourié-Gonnard | f81ee2e | 2015-09-01 17:43:40 +0200 | [diff] [blame] | 5467 | if( ssl == NULL || ssl->conf == NULL ) |
| 5468 | return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5469 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5470 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5471 | |
Manuel Pégourié-Gonnard | a13500f | 2014-08-19 16:14:04 +0200 | [diff] [blame] | 5472 | if( ssl->out_left != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5473 | return( mbedtls_ssl_flush_output( ssl ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5474 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5475 | if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5476 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5477 | if( ( ret = mbedtls_ssl_send_alert_message( ssl, |
| 5478 | MBEDTLS_SSL_ALERT_LEVEL_WARNING, |
| 5479 | MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5480 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5481 | MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5482 | return( ret ); |
| 5483 | } |
| 5484 | } |
| 5485 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5486 | MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5487 | |
Manuel Pégourié-Gonnard | a13500f | 2014-08-19 16:14:04 +0200 | [diff] [blame] | 5488 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 5489 | } |
| 5490 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5491 | void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform ) |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5492 | { |
Paul Bakker | accaffe | 2014-06-26 13:37:14 +0200 | [diff] [blame] | 5493 | if( transform == NULL ) |
| 5494 | return; |
| 5495 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5496 | mbedtls_cipher_free( &transform->cipher_ctx_enc ); |
| 5497 | mbedtls_cipher_free( &transform->cipher_ctx_dec ); |
Manuel Pégourié-Gonnard | f71e587 | 2013-09-23 17:12:43 +0200 | [diff] [blame] | 5498 | |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 5499 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5500 | mbedtls_md_free( &transform->md_ctx_enc ); |
| 5501 | mbedtls_md_free( &transform->md_ctx_dec ); |
Hanno Becker | d56ed24 | 2018-01-03 15:32:51 +0000 | [diff] [blame] | 5502 | #endif |
Paul Bakker | 61d113b | 2013-07-04 11:51:43 +0200 | [diff] [blame] | 5503 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 5504 | mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) ); |
Paul Bakker | 48916f9 | 2012-09-16 19:57:18 +0000 | [diff] [blame] | 5505 | } |
| 5506 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 5507 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5508 | |
Hanno Becker | 533ab5f | 2020-02-05 10:49:13 +0000 | [diff] [blame] | 5509 | void mbedtls_ssl_buffering_free( mbedtls_ssl_context *ssl ) |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 5510 | { |
| 5511 | unsigned offset; |
| 5512 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 5513 | |
| 5514 | if( hs == NULL ) |
| 5515 | return; |
| 5516 | |
Hanno Becker | 283f5ef | 2018-08-24 09:34:47 +0100 | [diff] [blame] | 5517 | ssl_free_buffered_record( ssl ); |
| 5518 | |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 5519 | for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ ) |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 5520 | ssl_buffering_free_slot( ssl, offset ); |
| 5521 | } |
| 5522 | |
| 5523 | static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl, |
| 5524 | uint8_t slot ) |
| 5525 | { |
| 5526 | mbedtls_ssl_handshake_params * const hs = ssl->handshake; |
| 5527 | mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot]; |
Hanno Becker | b309b92 | 2018-08-23 13:18:05 +0100 | [diff] [blame] | 5528 | |
| 5529 | if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS ) |
| 5530 | return; |
| 5531 | |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 5532 | if( hs_buf->is_valid == 1 ) |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 5533 | { |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 5534 | hs->buffering.total_bytes_buffered -= hs_buf->data_len; |
Hanno Becker | 805f2e1 | 2018-10-12 16:31:41 +0100 | [diff] [blame] | 5535 | mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len ); |
Hanno Becker | e605b19 | 2018-08-21 15:59:07 +0100 | [diff] [blame] | 5536 | mbedtls_free( hs_buf->data ); |
| 5537 | memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) ); |
Hanno Becker | 0271f96 | 2018-08-16 13:23:47 +0100 | [diff] [blame] | 5538 | } |
| 5539 | } |
| 5540 | |
| 5541 | #endif /* MBEDTLS_SSL_PROTO_DTLS */ |
| 5542 | |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5543 | /* |
| 5544 | * Convert version numbers to/from wire format |
| 5545 | * and, for DTLS, to/from TLS equivalent. |
| 5546 | * |
| 5547 | * For TLS this is the identity. |
Brian J Murray | 1903fb3 | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 5548 | * 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] | 5549 | * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2) |
| 5550 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5551 | void mbedtls_ssl_write_version( int major, int minor, int transport, |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5552 | unsigned char ver[2] ) |
| 5553 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5554 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5555 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5556 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5557 | if( minor == MBEDTLS_SSL_MINOR_VERSION_2 ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5558 | --minor; /* DTLS 1.0 stored as TLS 1.1 internally */ |
| 5559 | |
| 5560 | ver[0] = (unsigned char)( 255 - ( major - 2 ) ); |
| 5561 | ver[1] = (unsigned char)( 255 - ( minor - 1 ) ); |
| 5562 | } |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 5563 | else |
| 5564 | #else |
| 5565 | ((void) transport); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5566 | #endif |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 5567 | { |
| 5568 | ver[0] = (unsigned char) major; |
| 5569 | ver[1] = (unsigned char) minor; |
| 5570 | } |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5571 | } |
| 5572 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5573 | void mbedtls_ssl_read_version( int *major, int *minor, int transport, |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5574 | const unsigned char ver[2] ) |
| 5575 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5576 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 5577 | if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5578 | { |
| 5579 | *major = 255 - ver[0] + 2; |
| 5580 | *minor = 255 - ver[1] + 1; |
| 5581 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5582 | if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 ) |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5583 | ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */ |
| 5584 | } |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 5585 | else |
| 5586 | #else |
| 5587 | ((void) transport); |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5588 | #endif |
Manuel Pégourié-Gonnard | 34c1011 | 2014-03-25 13:36:22 +0100 | [diff] [blame] | 5589 | { |
| 5590 | *major = ver[0]; |
| 5591 | *minor = ver[1]; |
| 5592 | } |
Manuel Pégourié-Gonnard | abc7e3b | 2014-02-11 18:15:03 +0100 | [diff] [blame] | 5593 | } |
| 5594 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5595 | #endif /* MBEDTLS_SSL_TLS_C */ |