0002-pr-9981-defragment-incoming-tls-handshake-messages.patch 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. diff --git a/thirdparty/README.md b/thirdparty/README.md
  2. index 16a7661f5b..7ad8524e1a 100644
  3. --- a/thirdparty/README.md
  4. +++ b/thirdparty/README.md
  5. @@ -627,6 +627,7 @@ File extracted from upstream release tarball:
  6. Patches:
  7. - `0001-msvc-2019-psa-redeclaration.patch` (GH-90535)
  8. +- `0002-pr-9981-defragment-incoming-tls-handshake-messages.patch` (GH-102770)
  9. ## meshoptimizer
  10. diff --git a/thirdparty/mbedtls/include/mbedtls/ssl.h b/thirdparty/mbedtls/include/mbedtls/ssl.h
  11. index 42fffbf860..597da2571f 100644
  12. --- a/thirdparty/mbedtls/include/mbedtls/ssl.h
  13. +++ b/thirdparty/mbedtls/include/mbedtls/ssl.h
  14. @@ -1724,7 +1724,16 @@ struct mbedtls_ssl_context {
  15. int MBEDTLS_PRIVATE(early_data_state);
  16. #endif
  17. - unsigned MBEDTLS_PRIVATE(badmac_seen); /*!< records with a bad MAC received */
  18. + /** Multipurpose field.
  19. + *
  20. + * - DTLS: records with a bad MAC received.
  21. + * - TLS: accumulated length of handshake fragments (up to \c in_hslen).
  22. + *
  23. + * This field is multipurpose in order to preserve the ABI in the
  24. + * Mbed TLS 3.6 LTS branch. Until 3.6.2, it was only used in DTLS
  25. + * and called `badmac_seen`.
  26. + */
  27. + unsigned MBEDTLS_PRIVATE(badmac_seen_or_in_hsfraglen);
  28. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  29. /** Callback to customize X.509 certificate chain verification */
  30. diff --git a/thirdparty/mbedtls/library/ssl_misc.h b/thirdparty/mbedtls/library/ssl_misc.h
  31. index 98668798a8..bfadac7be3 100644
  32. --- a/thirdparty/mbedtls/library/ssl_misc.h
  33. +++ b/thirdparty/mbedtls/library/ssl_misc.h
  34. @@ -1829,10 +1829,11 @@ void mbedtls_ssl_set_timer(mbedtls_ssl_context *ssl, uint32_t millisecs);
  35. MBEDTLS_CHECK_RETURN_CRITICAL
  36. int mbedtls_ssl_check_timer(mbedtls_ssl_context *ssl);
  37. -void mbedtls_ssl_reset_in_out_pointers(mbedtls_ssl_context *ssl);
  38. +void mbedtls_ssl_reset_in_pointers(mbedtls_ssl_context *ssl);
  39. +void mbedtls_ssl_update_in_pointers(mbedtls_ssl_context *ssl);
  40. +void mbedtls_ssl_reset_out_pointers(mbedtls_ssl_context *ssl);
  41. void mbedtls_ssl_update_out_pointers(mbedtls_ssl_context *ssl,
  42. mbedtls_ssl_transform *transform);
  43. -void mbedtls_ssl_update_in_pointers(mbedtls_ssl_context *ssl);
  44. MBEDTLS_CHECK_RETURN_CRITICAL
  45. int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial);
  46. diff --git a/thirdparty/mbedtls/library/ssl_msg.c b/thirdparty/mbedtls/library/ssl_msg.c
  47. index ef722d7bdc..08d197e08c 100644
  48. --- a/thirdparty/mbedtls/library/ssl_msg.c
  49. +++ b/thirdparty/mbedtls/library/ssl_msg.c
  50. @@ -25,6 +25,7 @@
  51. #include "constant_time_internal.h"
  52. #include "mbedtls/constant_time.h"
  53. +#include <limits.h>
  54. #include <string.h>
  55. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  56. @@ -3220,13 +3221,17 @@ static uint32_t ssl_get_hs_total_len(mbedtls_ssl_context const *ssl)
  57. int mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl)
  58. {
  59. - if (ssl->in_msglen < mbedtls_ssl_hs_hdr_len(ssl)) {
  60. + /* First handshake fragment must at least include the header. */
  61. + if (ssl->in_msglen < mbedtls_ssl_hs_hdr_len(ssl) && ssl->in_hslen == 0) {
  62. MBEDTLS_SSL_DEBUG_MSG(1, ("handshake message too short: %" MBEDTLS_PRINTF_SIZET,
  63. ssl->in_msglen));
  64. return MBEDTLS_ERR_SSL_INVALID_RECORD;
  65. }
  66. - ssl->in_hslen = mbedtls_ssl_hs_hdr_len(ssl) + ssl_get_hs_total_len(ssl);
  67. + if (ssl->in_hslen == 0) {
  68. + ssl->in_hslen = mbedtls_ssl_hs_hdr_len(ssl) + ssl_get_hs_total_len(ssl);
  69. + ssl->badmac_seen_or_in_hsfraglen = 0;
  70. + }
  71. MBEDTLS_SSL_DEBUG_MSG(3, ("handshake message: msglen ="
  72. " %" MBEDTLS_PRINTF_SIZET ", type = %u, hslen = %"
  73. @@ -3292,10 +3297,67 @@ int mbedtls_ssl_prepare_handshake_record(mbedtls_ssl_context *ssl)
  74. }
  75. } else
  76. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  77. - /* With TLS we don't handle fragmentation (for now) */
  78. - if (ssl->in_msglen < ssl->in_hslen) {
  79. - MBEDTLS_SSL_DEBUG_MSG(1, ("TLS handshake fragmentation not supported"));
  80. - return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  81. + if (ssl->badmac_seen_or_in_hsfraglen <= ssl->in_hslen) {
  82. + int ret;
  83. + const size_t hs_remain = ssl->in_hslen - ssl->badmac_seen_or_in_hsfraglen;
  84. + MBEDTLS_SSL_DEBUG_MSG(3,
  85. + ("handshake fragment: %u .. %"
  86. + MBEDTLS_PRINTF_SIZET " of %"
  87. + MBEDTLS_PRINTF_SIZET " msglen %" MBEDTLS_PRINTF_SIZET,
  88. + ssl->badmac_seen_or_in_hsfraglen,
  89. + (size_t) ssl->badmac_seen_or_in_hsfraglen +
  90. + (hs_remain <= ssl->in_msglen ? hs_remain : ssl->in_msglen),
  91. + ssl->in_hslen, ssl->in_msglen));
  92. + if (ssl->in_msglen < hs_remain) {
  93. + /* ssl->in_msglen is a 25-bit value since it is the sum of the
  94. + * header length plus the payload length, the header length is 4
  95. + * and the payload length was received on the wire encoded as
  96. + * 3 octets. We don't support 16-bit platforms; more specifically,
  97. + * we assume that both unsigned and size_t are at least 32 bits.
  98. + * Therefore there is no possible integer overflow here.
  99. + */
  100. + ssl->badmac_seen_or_in_hsfraglen += (unsigned) ssl->in_msglen;
  101. + ssl->in_hdr = ssl->in_msg + ssl->in_msglen;
  102. + ssl->in_msglen = 0;
  103. + mbedtls_ssl_update_in_pointers(ssl);
  104. + return MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
  105. + }
  106. + if (ssl->badmac_seen_or_in_hsfraglen > 0) {
  107. + /*
  108. + * At in_first_hdr we have a sequence of records that cover the next handshake
  109. + * record, each with its own record header that we need to remove.
  110. + * Note that the reassembled record size may not equal the size of the message,
  111. + * there may be more messages after it, complete or partial.
  112. + */
  113. + unsigned char *in_first_hdr = ssl->in_buf + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
  114. + unsigned char *p = in_first_hdr, *q = NULL;
  115. + size_t merged_rec_len = 0;
  116. + do {
  117. + mbedtls_record rec;
  118. + ret = ssl_parse_record_header(ssl, p, mbedtls_ssl_in_hdr_len(ssl), &rec);
  119. + if (ret != 0) {
  120. + return ret;
  121. + }
  122. + merged_rec_len += rec.data_len;
  123. + p = rec.buf + rec.buf_len;
  124. + if (q != NULL) {
  125. + memmove(q, rec.buf + rec.data_offset, rec.data_len);
  126. + q += rec.data_len;
  127. + } else {
  128. + q = p;
  129. + }
  130. + } while (merged_rec_len < ssl->in_hslen);
  131. + ssl->in_hdr = in_first_hdr;
  132. + mbedtls_ssl_update_in_pointers(ssl);
  133. + ssl->in_msglen = merged_rec_len;
  134. + /* Adjust message length. */
  135. + MBEDTLS_PUT_UINT16_BE(merged_rec_len, ssl->in_len, 0);
  136. + ssl->badmac_seen_or_in_hsfraglen = 0;
  137. + MBEDTLS_SSL_DEBUG_BUF(4, "reassembled record",
  138. + ssl->in_hdr, mbedtls_ssl_in_hdr_len(ssl) + merged_rec_len);
  139. + }
  140. + } else {
  141. + return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  142. }
  143. return 0;
  144. @@ -4640,6 +4702,16 @@ static int ssl_consume_current_message(mbedtls_ssl_context *ssl)
  145. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  146. }
  147. + if (ssl->badmac_seen_or_in_hsfraglen != 0) {
  148. + /* Not all handshake fragments have arrived, do not consume. */
  149. + MBEDTLS_SSL_DEBUG_MSG(3,
  150. + ("waiting for more fragments (%u of %"
  151. + MBEDTLS_PRINTF_SIZET ", %" MBEDTLS_PRINTF_SIZET " left)",
  152. + ssl->badmac_seen_or_in_hsfraglen, ssl->in_hslen,
  153. + ssl->in_hslen - ssl->badmac_seen_or_in_hsfraglen));
  154. + return 0;
  155. + }
  156. +
  157. /*
  158. * Get next Handshake message in the current record
  159. */
  160. @@ -4665,6 +4737,7 @@ static int ssl_consume_current_message(mbedtls_ssl_context *ssl)
  161. ssl->in_msglen -= ssl->in_hslen;
  162. memmove(ssl->in_msg, ssl->in_msg + ssl->in_hslen,
  163. ssl->in_msglen);
  164. + MBEDTLS_PUT_UINT16_BE(ssl->in_msglen, ssl->in_len, 0);
  165. MBEDTLS_SSL_DEBUG_BUF(4, "remaining content in record",
  166. ssl->in_msg, ssl->in_msglen);
  167. @@ -4967,10 +5040,12 @@ static int ssl_get_next_record(mbedtls_ssl_context *ssl)
  168. return ret;
  169. }
  170. - if (ssl->conf->badmac_limit != 0 &&
  171. - ++ssl->badmac_seen >= ssl->conf->badmac_limit) {
  172. - MBEDTLS_SSL_DEBUG_MSG(1, ("too many records with bad MAC"));
  173. - return MBEDTLS_ERR_SSL_INVALID_MAC;
  174. + if (ssl->conf->badmac_limit != 0) {
  175. + ++ssl->badmac_seen_or_in_hsfraglen;
  176. + if (ssl->badmac_seen_or_in_hsfraglen >= ssl->conf->badmac_limit) {
  177. + MBEDTLS_SSL_DEBUG_MSG(1, ("too many records with bad MAC"));
  178. + return MBEDTLS_ERR_SSL_INVALID_MAC;
  179. + }
  180. }
  181. /* As above, invalid records cause
  182. @@ -5345,7 +5420,7 @@ void mbedtls_ssl_update_in_pointers(mbedtls_ssl_context *ssl)
  183. } else
  184. #endif
  185. {
  186. - ssl->in_ctr = ssl->in_hdr - MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
  187. + ssl->in_ctr = ssl->in_buf;
  188. ssl->in_len = ssl->in_hdr + 3;
  189. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  190. ssl->in_cid = ssl->in_len;
  191. @@ -5361,24 +5436,35 @@ void mbedtls_ssl_update_in_pointers(mbedtls_ssl_context *ssl)
  192. * Setup an SSL context
  193. */
  194. -void mbedtls_ssl_reset_in_out_pointers(mbedtls_ssl_context *ssl)
  195. +void mbedtls_ssl_reset_in_pointers(mbedtls_ssl_context *ssl)
  196. +{
  197. +#if defined(MBEDTLS_SSL_PROTO_DTLS)
  198. + if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  199. + ssl->in_hdr = ssl->in_buf;
  200. + } else
  201. +#endif /* MBEDTLS_SSL_PROTO_DTLS */
  202. + {
  203. + ssl->in_hdr = ssl->in_buf + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
  204. + }
  205. +
  206. + /* Derive other internal pointers. */
  207. + mbedtls_ssl_update_in_pointers(ssl);
  208. +}
  209. +
  210. +void mbedtls_ssl_reset_out_pointers(mbedtls_ssl_context *ssl)
  211. {
  212. /* Set the incoming and outgoing record pointers. */
  213. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  214. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  215. ssl->out_hdr = ssl->out_buf;
  216. - ssl->in_hdr = ssl->in_buf;
  217. } else
  218. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  219. {
  220. ssl->out_ctr = ssl->out_buf;
  221. - ssl->out_hdr = ssl->out_buf + 8;
  222. - ssl->in_hdr = ssl->in_buf + 8;
  223. + ssl->out_hdr = ssl->out_buf + MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
  224. }
  225. -
  226. /* Derive other internal pointers. */
  227. mbedtls_ssl_update_out_pointers(ssl, NULL /* no transform enabled */);
  228. - mbedtls_ssl_update_in_pointers(ssl);
  229. }
  230. /*
  231. diff --git a/thirdparty/mbedtls/library/ssl_tls.c b/thirdparty/mbedtls/library/ssl_tls.c
  232. index c773365bf6..7f74248252 100644
  233. --- a/thirdparty/mbedtls/library/ssl_tls.c
  234. +++ b/thirdparty/mbedtls/library/ssl_tls.c
  235. @@ -344,12 +344,13 @@ static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
  236. size_t out_buf_new_len)
  237. {
  238. int modified = 0;
  239. - size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
  240. + size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0, hdr_in = 0;
  241. size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
  242. if (ssl->in_buf != NULL) {
  243. written_in = ssl->in_msg - ssl->in_buf;
  244. iv_offset_in = ssl->in_iv - ssl->in_buf;
  245. len_offset_in = ssl->in_len - ssl->in_buf;
  246. + hdr_in = ssl->in_hdr - ssl->in_buf;
  247. if (downsizing ?
  248. ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
  249. ssl->in_buf_len < in_buf_new_len) {
  250. @@ -381,7 +382,10 @@ static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
  251. }
  252. if (modified) {
  253. /* Update pointers here to avoid doing it twice. */
  254. - mbedtls_ssl_reset_in_out_pointers(ssl);
  255. + ssl->in_hdr = ssl->in_buf + hdr_in;
  256. + mbedtls_ssl_update_in_pointers(ssl);
  257. + mbedtls_ssl_reset_out_pointers(ssl);
  258. +
  259. /* Fields below might not be properly updated with record
  260. * splitting or with CID, so they are manually updated here. */
  261. ssl->out_msg = ssl->out_buf + written_out;
  262. @@ -1409,7 +1413,8 @@ int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
  263. goto error;
  264. }
  265. - mbedtls_ssl_reset_in_out_pointers(ssl);
  266. + mbedtls_ssl_reset_in_pointers(ssl);
  267. + mbedtls_ssl_reset_out_pointers(ssl);
  268. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  269. memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
  270. @@ -1474,7 +1479,8 @@ void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
  271. /* Cancel any possibly running timer */
  272. mbedtls_ssl_set_timer(ssl, 0);
  273. - mbedtls_ssl_reset_in_out_pointers(ssl);
  274. + mbedtls_ssl_reset_in_pointers(ssl);
  275. + mbedtls_ssl_reset_out_pointers(ssl);
  276. /* Reset incoming message parsing */
  277. ssl->in_offt = NULL;
  278. @@ -1485,6 +1491,12 @@ void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
  279. ssl->keep_current_message = 0;
  280. ssl->transform_in = NULL;
  281. + /* TLS: reset in_hsfraglen, which is part of message parsing.
  282. + * DTLS: on a client reconnect, don't reset badmac_seen. */
  283. + if (!partial) {
  284. + ssl->badmac_seen_or_in_hsfraglen = 0;
  285. + }
  286. +
  287. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  288. ssl->next_record_offset = 0;
  289. ssl->in_epoch = 0;
  290. @@ -5014,7 +5026,7 @@ static const unsigned char ssl_serialized_context_header[] = {
  291. * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
  292. * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
  293. * // fields from ssl_context
  294. - * uint32 badmac_seen; // DTLS: number of records with failing MAC
  295. + * uint32 badmac_seen_or_in_hsfraglen; // DTLS: number of records with failing MAC
  296. * uint64 in_window_top; // DTLS: last validated record seq_num
  297. * uint64 in_window; // DTLS: bitmask for replay protection
  298. * uint8 disable_datagram_packing; // DTLS: only one record per datagram
  299. @@ -5156,7 +5168,7 @@ int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
  300. */
  301. used += 4;
  302. if (used <= buf_len) {
  303. - MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
  304. + MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen_or_in_hsfraglen, p, 0);
  305. p += 4;
  306. }
  307. @@ -5386,7 +5398,7 @@ static int ssl_context_load(mbedtls_ssl_context *ssl,
  308. return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  309. }
  310. - ssl->badmac_seen = MBEDTLS_GET_UINT32_BE(p, 0);
  311. + ssl->badmac_seen_or_in_hsfraglen = MBEDTLS_GET_UINT32_BE(p, 0);
  312. p += 4;
  313. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  314. diff --git a/thirdparty/mbedtls/library/ssl_tls12_server.c b/thirdparty/mbedtls/library/ssl_tls12_server.c
  315. index 03722ac33c..67df4284a4 100644
  316. --- a/thirdparty/mbedtls/library/ssl_tls12_server.c
  317. +++ b/thirdparty/mbedtls/library/ssl_tls12_server.c
  318. @@ -1057,28 +1057,6 @@ read_record_header:
  319. MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
  320. return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
  321. }
  322. - {
  323. - size_t handshake_len = MBEDTLS_GET_UINT24_BE(buf, 1);
  324. - MBEDTLS_SSL_DEBUG_MSG(3, ("client hello v3, handshake len.: %u",
  325. - (unsigned) handshake_len));
  326. -
  327. - /* The record layer has a record size limit of 2^14 - 1 and
  328. - * fragmentation is not supported, so buf[1] should be zero. */
  329. - if (buf[1] != 0) {
  330. - MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message: %u != 0",
  331. - (unsigned) buf[1]));
  332. - return MBEDTLS_ERR_SSL_DECODE_ERROR;
  333. - }
  334. -
  335. - /* We don't support fragmentation of ClientHello (yet?) */
  336. - if (msg_len != mbedtls_ssl_hs_hdr_len(ssl) + handshake_len) {
  337. - MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message: %u != %u + %u",
  338. - (unsigned) msg_len,
  339. - (unsigned) mbedtls_ssl_hs_hdr_len(ssl),
  340. - (unsigned) handshake_len));
  341. - return MBEDTLS_ERR_SSL_DECODE_ERROR;
  342. - }
  343. - }
  344. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  345. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {