v2 / thirdparty / mbedtls / mbedtls.patch
318 lines · 295 sloc · 12.54 KB · d45d40e98dcc1e9a2deb8d2b09ed0ced2c4fc5f7
Raw
1diff -ur mbedtls.orig/include/mbedtls/check_config.h mbedtls/include/mbedtls/check_config.h
2--- mbedtls.orig/include/mbedtls/check_config.h 2026-04-02 17:26:31.906276356 +0200
3+++ mbedtls/include/mbedtls/check_config.h 2026-04-02 17:27:29.374384448 +0200
4@@ -247,9 +247,10 @@
5 #if defined(MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN) && !defined(MBEDTLS_HAS_MEMSAN)
6 #error "MBEDTLS_TEST_CONSTANT_FLOW_MEMSAN requires building with MemorySanitizer"
7 #endif
8-#if defined(MBEDTLS_HAS_MEMSAN) && defined(MBEDTLS_HAVE_ASM)
9-#error "MemorySanitizer does not support assembly implementation"
10-#endif
11+// skip this check for now because V test-self need a `fsanitizer`
12+//#if defined(MBEDTLS_HAS_MEMSAN) && defined(MBEDTLS_HAVE_ASM)
13+//#error "MemorySanitizer does not support assembly implementation"
14+//#endif
15 #undef MBEDTLS_HAS_MEMSAN // temporary macro defined above
16
17 #if defined(MBEDTLS_CCM_C) && \
18diff -ur mbedtls.orig/include/mbedtls/mbedtls_config.h mbedtls/include/mbedtls/mbedtls_config.h
19--- mbedtls.orig/include/mbedtls/mbedtls_config.h 2026-04-02 17:26:31.910276365 +0200
20+++ mbedtls/include/mbedtls/mbedtls_config.h 2026-04-02 17:27:29.374384448 +0200
21@@ -4435,3 +4435,22 @@
22 //#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512 /**< Maximum length of a path/filename string in bytes including the null terminator character ('\0'). */
23
24 /** \} name SECTION: Module configuration options */
25+
26+
27+#if defined(__TINYC__)
28+#undef MBEDTLS_HAVE_ASM
29+#undef MBEDTLS_AESNI_C
30+#undef MBEDTLS_PADLOCK_C
31+#else // __TINYC__
32+#define MBEDTLS_HAVE_ASM
33+#define MBEDTLS_AESNI_C
34+#define MBEDTLS_PADLOCK_C
35+#endif // __TINYC__
36+
37+#if ( defined(__linux__) || defined(__FreeBSD__) ) || defined (__OpenBSD__)
38+#define MBEDTLS_THREADING_PTHREAD
39+#define MBEDTLS_THREADING_C
40+#else
41+#undef MBEDTLS_THREADING_PTHREAD
42+#undef MBEDTLS_THREADING_C
43+#endif
44diff -ur mbedtls.orig/library/alignment.h mbedtls/library/alignment.h
45--- mbedtls.orig/library/alignment.h 2026-04-02 17:26:31.918276380 +0200
46+++ mbedtls/library/alignment.h 2026-04-02 17:30:51.350689732 +0200
47@@ -280,7 +280,7 @@
48 /*
49 * Detect GCC built-in byteswap routines
50 */
51-#if defined(__GNUC__)
52+#if defined(__GNUC__) && !(defined(__TINYC__) && defined(__FreeBSD__))
53 #if MBEDTLS_GCC_VERSION >= 40800
54 #define MBEDTLS_BSWAP16 __builtin_bswap16
55 #endif
56@@ -293,7 +293,7 @@
57 /*
58 * Detect Clang built-in byteswap routines
59 */
60-#if defined(__clang__) && defined(__has_builtin)
61+#if defined(__clang__) && defined(__has_builtin) && !(defined(__TINYC__) && defined(__FreeBSD__))
62 #if __has_builtin(__builtin_bswap16) && !defined(MBEDTLS_BSWAP16)
63 #define MBEDTLS_BSWAP16 __builtin_bswap16
64 #endif /* __has_builtin(__builtin_bswap16) */
65diff -ur mbedtls.orig/library/entropy_poll.c mbedtls/library/entropy_poll.c
66--- mbedtls.orig/library/entropy_poll.c 2026-04-02 17:26:31.922276388 +0200
67+++ mbedtls/library/entropy_poll.c 2026-04-02 17:27:29.374384448 +0200
68@@ -38,35 +38,36 @@
69
70 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
71
72+// fallback to 3.3.0 implmentation, as 3.6.5 need a high version of Windows SDK
73+#if !defined(_WIN32_WINNT)
74+#define _WIN32_WINNT 0x0400
75+#endif
76 #include <windows.h>
77-#include <bcrypt.h>
78-#include <intsafe.h>
79+#include <wincrypt.h>
80
81-int mbedtls_platform_entropy_poll(void *data, unsigned char *output, size_t len,
82- size_t *olen)
83+int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
84+ size_t *olen )
85 {
86+ HCRYPTPROV provider;
87 ((void) data);
88 *olen = 0;
89
90- /*
91- * BCryptGenRandom takes ULONG for size, which is smaller than size_t on
92- * 64-bit Windows platforms. Extract entropy in chunks of len (dependent
93- * on ULONG_MAX) size.
94- */
95- while (len != 0) {
96- unsigned long ulong_bytes =
97- (len > ULONG_MAX) ? ULONG_MAX : (unsigned long) len;
98-
99- if (!BCRYPT_SUCCESS(BCryptGenRandom(NULL, output, ulong_bytes,
100- BCRYPT_USE_SYSTEM_PREFERRED_RNG))) {
101- return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
102- }
103+ if( CryptAcquireContext( &provider, NULL, NULL,
104+ PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
105+ {
106+ return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
107+ }
108
109- *olen += ulong_bytes;
110- len -= ulong_bytes;
111+ if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
112+ {
113+ CryptReleaseContext( provider, 0 );
114+ return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
115 }
116
117- return 0;
118+ CryptReleaseContext( provider, 0 );
119+ *olen = len;
120+
121+ return( 0 );
122 }
123 #else /* _WIN32 && !EFIX64 && !EFI32 */
124
125diff -ur mbedtls.orig/library/pk.c mbedtls/library/pk.c
126--- mbedtls.orig/library/pk.c 2026-04-02 17:26:31.926276396 +0200
127+++ mbedtls/library/pk.c 2026-04-02 17:27:29.374384448 +0200
128@@ -1336,6 +1336,7 @@
129 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
130 }
131
132+ if (ctx == NULL) return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
133 if (ctx->pk_info == NULL || pk_hashlen_helper(md_alg, &hash_len) != 0) {
134 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
135 }
136diff -ur mbedtls.orig/library/platform_util.c mbedtls/library/platform_util.c
137--- mbedtls.orig/library/platform_util.c 2026-04-02 17:26:31.926276396 +0200
138+++ mbedtls/library/platform_util.c 2026-04-02 17:27:29.374384448 +0200
139@@ -87,7 +87,7 @@
140 */
141 #if !defined(MBEDTLS_PLATFORM_HAS_EXPLICIT_BZERO) && !(defined(__STDC_LIB_EXT1__) && \
142 !defined(__IAR_SYSTEMS_ICC__)) \
143- && !defined(_WIN32)
144+ && !(defined(_WIN32) && !defined(__TINYC__))
145 static void *(*const volatile memset_func)(void *, int, size_t) = memset;
146 #endif
147
148@@ -107,7 +107,8 @@
149 #endif
150 #elif defined(__STDC_LIB_EXT1__) && !defined(__IAR_SYSTEMS_ICC__)
151 memset_s(buf, len, 0, len);
152-#elif defined(_WIN32)
153+#elif defined(_WIN32) && !defined(__TINYC__)
154+ /* tcc has a bad implementation of `SecureZeroMemory` */
155 SecureZeroMemory(buf, len);
156 #else
157 memset_func(buf, 0, len);
158diff -ur mbedtls.orig/library/ssl_misc.h mbedtls/library/ssl_misc.h
159--- mbedtls.orig/library/ssl_misc.h 2026-04-02 17:26:31.934276411 +0200
160+++ mbedtls/library/ssl_misc.h 2026-04-02 17:27:29.374384448 +0200
161@@ -1674,26 +1674,30 @@
162 {
163 mbedtls_ssl_key_cert *key_cert;
164
165+ if (ssl == NULL) return NULL;
166 if (ssl->handshake != NULL && ssl->handshake->key_cert != NULL) {
167 key_cert = ssl->handshake->key_cert;
168 } else {
169+ if (ssl->conf == NULL) return NULL;
170 key_cert = ssl->conf->key_cert;
171 }
172-
173- return key_cert == NULL ? NULL : key_cert->key;
174+ if (key_cert == NULL) return NULL;
175+ return key_cert->key;
176 }
177
178 static inline mbedtls_x509_crt *mbedtls_ssl_own_cert(mbedtls_ssl_context *ssl)
179 {
180 mbedtls_ssl_key_cert *key_cert;
181
182+ if (ssl == NULL) return NULL;
183 if (ssl->handshake != NULL && ssl->handshake->key_cert != NULL) {
184 key_cert = ssl->handshake->key_cert;
185 } else {
186+ if (ssl->conf == NULL) return NULL;
187 key_cert = ssl->conf->key_cert;
188 }
189-
190- return key_cert == NULL ? NULL : key_cert->cert;
191+ if (key_cert == NULL) return NULL;
192+ return key_cert->cert;
193 }
194
195 /*
196diff -ur mbedtls.orig/library/ssl_tls.c mbedtls/library/ssl_tls.c
197--- mbedtls.orig/library/ssl_tls.c 2026-04-02 17:26:31.938276419 +0200
198+++ mbedtls/library/ssl_tls.c 2026-04-02 17:27:29.374384448 +0200
199@@ -4563,8 +4563,8 @@
200 {
201 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
202
203- if (ssl == NULL ||
204- ssl->conf == NULL ||
205+ if (ssl == NULL) return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
206+ if (ssl->conf == NULL ||
207 ssl->handshake == NULL ||
208 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
209 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
210@@ -4653,10 +4653,8 @@
211 int ret = 0;
212
213 /* Sanity checks */
214-
215- if (ssl == NULL || ssl->conf == NULL) {
216- return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
217- }
218+ if (ssl == NULL) return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
219+ if (ssl->conf == NULL) return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
220
221 #if defined(MBEDTLS_SSL_PROTO_DTLS)
222 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
223diff -ur mbedtls.orig/library/ssl_tls12_client.c mbedtls/library/ssl_tls12_client.c
224--- mbedtls.orig/library/ssl_tls12_client.c 2026-04-02 17:26:31.940276423 +0200
225+++ mbedtls/library/ssl_tls12_client.c 2026-04-02 17:27:29.374384448 +0200
226@@ -13,6 +13,7 @@
227
228 #include "mbedtls/ssl.h"
229 #include "ssl_client.h"
230+#include "ssl_debug_helpers.h"
231 #include "ssl_misc.h"
232 #include "debug_internal.h"
233 #include "mbedtls/error.h"
234@@ -2087,32 +2088,73 @@ static int ssl_parse_signature_algorithm(mbedtls_ssl_context *ssl,
235 {
236 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(sig_alg, pk_alg, md_alg) != 0) {
237 MBEDTLS_SSL_DEBUG_MSG(1,
238- ("Server used unsupported value in SigAlg extension 0x%04x",
239- sig_alg));
240+ ("Server used unsupported %s signature algorithm",
241+ mbedtls_ssl_sig_alg_to_str(sig_alg)));
242 return MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
243 }
244
245 /*
246- * mbedtls_ssl_get_pk_sigalg_and_md_alg_from_sig_alg() understands sig_alg code points across
247- * TLS versions. Make sure that the received sig_alg extension is valid in TLS 1.2.
248+ * mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg() understands
249+ * signature algorithm code points from both TLS 1.2 and TLS 1.3. Make sure
250+ * that the selected signature algorithm is acceptable when TLS 1.2 is
251+ * negotiated.
252+ *
253+ * In TLS 1.2, RSA-PSS signature algorithms (rsa_pss_rsae_*) are not
254+ * defined by RFC 5246. However, RFC 8446 Section 4.2.3 requires that
255+ * implementations which advertise support for RSASSA-PSS must be
256+ * prepared to accept such signatures even when TLS 1.2 is negotiated,
257+ * provided they were offered in the signature_algorithms extension.
258+ *
259+ * Therefore, we allow rsa_pss_rsae_* here if:
260+ * - the implementation supports them, and
261+ * - they were offered in the signature_algorithms extension (checked by
262+ * `mbedtls_ssl_sig_alg_is_offered()` below).
263+ *
264+ * If we were to add full support for rsa_pss_rsae_* signature algorithms
265+ * in TLS 1.2, we should then integrate RSA-PSS into the TLS 1.2 signature
266+ * algorithm support logic (`mbedtls_ssl_tls12_sig_alg_is_supported()`)
267+ * instead of handling it as a special case here.
268 */
269 if (!mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg)) {
270- MBEDTLS_SSL_DEBUG_MSG(1,
271- ("Server used unsupported value in SigAlg extension 0x%04x",
272- sig_alg));
273- return MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
274+ switch (sig_alg) {
275+#if defined(PSA_WANT_ALG_RSA_PSS)
276+#if defined(PSA_WANT_ALG_SHA_256)
277+ case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
278+#endif
279+#if defined(PSA_WANT_ALG_SHA_384)
280+ case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
281+#endif
282+#if defined(PSA_WANT_ALG_SHA_512)
283+ case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
284+#endif
285+#if defined(PSA_WANT_ALG_SHA_256) || defined(PSA_WANT_ALG_SHA_384) || defined(PSA_WANT_ALG_SHA_512)
286+ MBEDTLS_SSL_DEBUG_MSG(3,
287+ (
288+ "Accepting TLS 1.2 RSA-PSS signature algorithm %s via compatibility exception",
289+ mbedtls_ssl_sig_alg_to_str(sig_alg)));
290+ break;
291+#endif
292+#endif /* PSA_WANT_ALG_RSA_PSS */
293+ default:
294+ MBEDTLS_SSL_DEBUG_MSG(1,
295+ ("Server used unsupported %s signature algorithm",
296+ mbedtls_ssl_sig_alg_to_str(sig_alg)));
297+ return MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
298+ }
299 }
300
301 /*
302 * Check if the signature algorithm is acceptable
303 */
304 if (!mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)) {
305- MBEDTLS_SSL_DEBUG_MSG(1, ("Server used SigAlg value 0x%04x that was not offered", sig_alg));
306+ MBEDTLS_SSL_DEBUG_MSG(1,
307+ ("Server used the signature algorithm %s that was not offered",
308+ mbedtls_ssl_sig_alg_to_str(sig_alg)));
309 return MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
310 }
311
312- MBEDTLS_SSL_DEBUG_MSG(2, ("Server used SignatureAlgorithm %d", sig_alg & 0x00FF));
313- MBEDTLS_SSL_DEBUG_MSG(2, ("Server used HashAlgorithm %d", sig_alg >> 8));
314+ MBEDTLS_SSL_DEBUG_MSG(2, ("Server used the signature algorithm %s",
315+ mbedtls_ssl_sig_alg_to_str(sig_alg)));
316
317 return 0;
318 }
319