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