Skip to content

Commit 58f8a22

Browse files
committed
Merge pull request godotengine#90482 from Faless/mbedtls/3.6.0-tls
mbedTLS: Update to new LTS v3.6.0
2 parents 8ec0372 + 40fa684 commit 58f8a22

File tree

276 files changed

+97293
-38624
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

276 files changed

+97293
-38624
lines changed

core/crypto/SCsub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ if not has_module:
3434
"constant_time.c",
3535
"ctr_drbg.c",
3636
"entropy.c",
37+
"md.c",
3738
"md5.c",
3839
"sha1.c",
3940
"sha256.c",

core/crypto/crypto_core.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
#include <mbedtls/md5.h>
4040
#include <mbedtls/sha1.h>
4141
#include <mbedtls/sha256.h>
42+
#if MBEDTLS_VERSION_MAJOR >= 3
43+
#include <mbedtls/compat-2.x.h>
44+
#endif
4245

4346
// RandomGenerator
4447
CryptoCore::RandomGenerator::RandomGenerator() {

modules/mbedtls/SCsub

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,24 @@ thirdparty_obj = []
1212
if env["builtin_mbedtls"]:
1313
thirdparty_sources = [
1414
"aes.c",
15+
"aesce.c",
1516
"aesni.c",
16-
"arc4.c",
1717
"aria.c",
1818
"asn1parse.c",
1919
"asn1write.c",
2020
"base64.c",
2121
"bignum.c",
22-
"blowfish.c",
22+
"bignum_core.c",
23+
"bignum_mod_raw.c",
2324
"camellia.c",
2425
"ccm.c",
25-
"certs.c",
2626
"chacha20.c",
2727
"chachapoly.c",
2828
"cipher.c",
2929
"cipher_wrap.c",
3030
"cmac.c",
31-
"ctr_drbg.c",
3231
"constant_time.c",
32+
"ctr_drbg.c",
3333
"debug.c",
3434
"des.c",
3535
"dhm.c",
@@ -42,13 +42,10 @@ if env["builtin_mbedtls"]:
4242
"entropy_poll.c",
4343
"error.c",
4444
"gcm.c",
45-
"havege.c",
4645
"hkdf.c",
4746
"hmac_drbg.c",
48-
"md2.c",
49-
"md4.c",
50-
"md5.c",
5147
"md.c",
48+
"md5.c",
5249
"memory_buffer_alloc.c",
5350
"mps_reader.c",
5451
"mps_trace.c",
@@ -58,30 +55,37 @@ if env["builtin_mbedtls"]:
5855
"padlock.c",
5956
"pem.c",
6057
"pk.c",
61-
"pkcs11.c",
58+
"pk_ecc.c",
59+
"pk_wrap.c",
6260
"pkcs12.c",
6361
"pkcs5.c",
62+
"pkcs7.c",
6463
"pkparse.c",
65-
"pk_wrap.c",
6664
"pkwrite.c",
6765
"platform.c",
6866
"platform_util.c",
6967
"poly1305.c",
7068
"ripemd160.c",
7169
"rsa.c",
72-
"rsa_internal.c",
70+
"rsa_alt_helpers.c",
7371
"sha1.c",
72+
"sha3.c",
7473
"sha256.c",
7574
"sha512.c",
7675
"ssl_cache.c",
7776
"ssl_ciphersuites.c",
78-
"ssl_cli.c",
77+
"ssl_client.c",
7978
"ssl_cookie.c",
79+
"ssl_debug_helpers_generated.c",
8080
"ssl_msg.c",
81-
"ssl_srv.c",
8281
"ssl_ticket.c",
8382
"ssl_tls.c",
83+
"ssl_tls12_client.c",
84+
"ssl_tls12_server.c",
85+
"ssl_tls13_client.c",
86+
"ssl_tls13_generic.c",
8487
"ssl_tls13_keys.c",
88+
"ssl_tls13_server.c",
8589
"threading.c",
8690
"timing.c",
8791
"version.c",
@@ -91,9 +95,9 @@ if env["builtin_mbedtls"]:
9195
"x509_crl.c",
9296
"x509_crt.c",
9397
"x509_csr.c",
98+
"x509write.c",
9499
"x509write_crt.c",
95100
"x509write_csr.c",
96-
"xtea.c",
97101
]
98102

99103
thirdparty_dir = "#thirdparty/mbedtls/library/"

modules/mbedtls/crypto_mbedtls.cpp

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Error CryptoKeyMbedTLS::load(const String &p_path, bool p_public_only) {
6969
if (p_public_only) {
7070
ret = mbedtls_pk_parse_public_key(&pkey, out.ptr(), out.size());
7171
} else {
72-
ret = mbedtls_pk_parse_key(&pkey, out.ptr(), out.size(), nullptr, 0);
72+
ret = _parse_key(out.ptr(), out.size());
7373
}
7474
// We MUST zeroize the memory for safety!
7575
mbedtls_platform_zeroize(out.ptrw(), out.size());
@@ -108,7 +108,7 @@ Error CryptoKeyMbedTLS::load_from_string(const String &p_string_key, bool p_publ
108108
if (p_public_only) {
109109
ret = mbedtls_pk_parse_public_key(&pkey, (unsigned char *)p_string_key.utf8().get_data(), p_string_key.utf8().size());
110110
} else {
111-
ret = mbedtls_pk_parse_key(&pkey, (unsigned char *)p_string_key.utf8().get_data(), p_string_key.utf8().size(), nullptr, 0);
111+
ret = _parse_key((unsigned char *)p_string_key.utf8().get_data(), p_string_key.utf8().size());
112112
}
113113
ERR_FAIL_COND_V_MSG(ret, FAILED, "Error parsing key '" + itos(ret) + "'.");
114114

@@ -134,6 +134,25 @@ String CryptoKeyMbedTLS::save_to_string(bool p_public_only) {
134134
return s;
135135
}
136136

137+
int CryptoKeyMbedTLS::_parse_key(const uint8_t *p_buf, int p_size) {
138+
#if MBEDTLS_VERSION_MAJOR >= 3
139+
mbedtls_entropy_context rng_entropy;
140+
mbedtls_ctr_drbg_context rng_drbg;
141+
142+
mbedtls_ctr_drbg_init(&rng_drbg);
143+
mbedtls_entropy_init(&rng_entropy);
144+
int ret = mbedtls_ctr_drbg_seed(&rng_drbg, mbedtls_entropy_func, &rng_entropy, nullptr, 0);
145+
ERR_FAIL_COND_V_MSG(ret != 0, ret, vformat("mbedtls_ctr_drbg_seed returned -0x%x\n", (unsigned int)-ret));
146+
147+
ret = mbedtls_pk_parse_key(&pkey, p_buf, p_size, nullptr, 0, mbedtls_ctr_drbg_random, &rng_drbg);
148+
mbedtls_ctr_drbg_free(&rng_drbg);
149+
mbedtls_entropy_free(&rng_entropy);
150+
return ret;
151+
#else
152+
return mbedtls_pk_parse_key(&pkey, p_buf, p_size, nullptr, 0);
153+
#endif
154+
}
155+
137156
X509Certificate *X509CertificateMbedTLS::create() {
138157
return memnew(X509CertificateMbedTLS);
139158
}
@@ -393,12 +412,17 @@ Ref<X509Certificate> CryptoMbedTLS::generate_self_signed_certificate(Ref<CryptoK
393412
mbedtls_x509write_crt_set_version(&crt, MBEDTLS_X509_CRT_VERSION_3);
394413
mbedtls_x509write_crt_set_md_alg(&crt, MBEDTLS_MD_SHA256);
395414

415+
uint8_t rand_serial[20];
416+
mbedtls_ctr_drbg_random(&ctr_drbg, rand_serial, sizeof(rand_serial));
417+
418+
#if MBEDTLS_VERSION_MAJOR >= 3
419+
mbedtls_x509write_crt_set_serial_raw(&crt, rand_serial, sizeof(rand_serial));
420+
#else
396421
mbedtls_mpi serial;
397422
mbedtls_mpi_init(&serial);
398-
uint8_t rand_serial[20];
399-
mbedtls_ctr_drbg_random(&ctr_drbg, rand_serial, 20);
400-
ERR_FAIL_COND_V(mbedtls_mpi_read_binary(&serial, rand_serial, 20), nullptr);
423+
ERR_FAIL_COND_V(mbedtls_mpi_read_binary(&serial, rand_serial, sizeof(rand_serial)), nullptr);
401424
mbedtls_x509write_crt_set_serial(&crt, &serial);
425+
#endif
402426

403427
mbedtls_x509write_crt_set_validity(&crt, p_not_before.utf8().get_data(), p_not_after.utf8().get_data());
404428
mbedtls_x509write_crt_set_basic_constraints(&crt, 1, -1);
@@ -407,7 +431,9 @@ Ref<X509Certificate> CryptoMbedTLS::generate_self_signed_certificate(Ref<CryptoK
407431
unsigned char buf[4096];
408432
memset(buf, 0, 4096);
409433
int ret = mbedtls_x509write_crt_pem(&crt, buf, 4096, mbedtls_ctr_drbg_random, &ctr_drbg);
434+
#if MBEDTLS_VERSION_MAJOR < 3
410435
mbedtls_mpi_free(&serial);
436+
#endif
411437
mbedtls_x509write_crt_free(&crt);
412438
ERR_FAIL_COND_V_MSG(ret != 0, nullptr, "Failed to generate certificate: " + itos(ret));
413439
buf[4095] = '\0'; // Make sure strlen can't fail.
@@ -461,9 +487,17 @@ Vector<uint8_t> CryptoMbedTLS::sign(HashingContext::HashType p_hash_type, const
461487
ERR_FAIL_COND_V_MSG(!key.is_valid(), Vector<uint8_t>(), "Invalid key provided.");
462488
ERR_FAIL_COND_V_MSG(key->is_public_only(), Vector<uint8_t>(), "Invalid key provided. Cannot sign with public_only keys.");
463489
size_t sig_size = 0;
490+
#if MBEDTLS_VERSION_MAJOR >= 3
491+
unsigned char buf[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
492+
#else
464493
unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
494+
#endif
465495
Vector<uint8_t> out;
466-
int ret = mbedtls_pk_sign(&(key->pkey), type, p_hash.ptr(), size, buf, &sig_size, mbedtls_ctr_drbg_random, &ctr_drbg);
496+
int ret = mbedtls_pk_sign(&(key->pkey), type, p_hash.ptr(), size, buf,
497+
#if MBEDTLS_VERSION_MAJOR >= 3
498+
sizeof(buf),
499+
#endif
500+
&sig_size, mbedtls_ctr_drbg_random, &ctr_drbg);
467501
ERR_FAIL_COND_V_MSG(ret, out, "Error while signing: " + itos(ret));
468502
out.resize(sig_size);
469503
memcpy(out.ptrw(), buf, sig_size);

modules/mbedtls/crypto_mbedtls.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class CryptoKeyMbedTLS : public CryptoKey {
4646
int locks = 0;
4747
bool public_only = true;
4848

49+
int _parse_key(const uint8_t *p_buf, int p_size);
50+
4951
public:
5052
static CryptoKey *create();
5153
static void make_default() { CryptoKey::_create = create; }

modules/mbedtls/tls_context_mbedtls.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include "core/io/file_access.h"
3737
#include "core/object/ref_counted.h"
3838

39-
#include <mbedtls/config.h>
4039
#include <mbedtls/ctr_drbg.h>
4140
#include <mbedtls/debug.h>
4241
#include <mbedtls/entropy.h>

thirdparty/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -520,18 +520,18 @@ in the MSVC debugger.
520520
## mbedtls
521521

522522
- Upstream: https://github.com/Mbed-TLS/mbedtls
523-
- Version: 2.28.8 (5a764e5555c64337ed17444410269ff21cb617b1, 2024)
523+
- Version: 3.6.0 (2ca6c285a0dd3f33982dd57299012dacab1ff206, 2024)
524524
- License: Apache 2.0
525525

526526
File extracted from upstream release tarball:
527527

528528
- All `.h` from `include/mbedtls/` to `thirdparty/mbedtls/include/mbedtls/`
529-
except `config_psa.h` and `psa_util.h`
529+
and all `.h` from `include/psa/` to `thirdparty/mbedtls/include/psa/`
530530
- All `.c` and `.h` from `library/` to `thirdparty/mbedtls/library/` except
531-
those starting with `psa_*`
531+
for the `psa_*.c` source files
532532
- The `LICENSE` file (edited to keep only the Apache 2.0 variant)
533-
- Applied the patch `windows-arm64-hardclock.diff` to fix Windows ARM64 build
534-
Applied the patch `windows-entropy-bcrypt.diff` to fix Windows Store support
533+
- Applied the patch `no-flexible-arrays.diff` to fix Windows build (see
534+
upstream GH-9020)
535535
- Added 2 files `godot_core_mbedtls_platform.c` and `godot_core_mbedtls_config.h`
536536
providing configuration for light bundling with core
537537
- Added the file `godot_module_mbedtls_config.h` to customize the build

thirdparty/mbedtls/include/godot_module_mbedtls_config.h

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,34 @@
4141
#else
4242

4343
// Include default mbedTLS config.
44-
#include <mbedtls/config.h>
44+
#include <mbedtls/mbedtls_config.h>
4545

4646
// Disable weak cryptography.
4747
#undef MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED
4848
#undef MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
49-
#undef MBEDTLS_SSL_CBC_RECORD_SPLITTING
50-
#undef MBEDTLS_SSL_PROTO_TLS1
51-
#undef MBEDTLS_SSL_PROTO_TLS1_1
52-
#undef MBEDTLS_ARC4_C
5349
#undef MBEDTLS_DES_C
5450
#undef MBEDTLS_DHM_C
5551

52+
#ifndef __linux__
53+
// ARMv8 hardware AES operations. Detection only possible on linux.
54+
#undef MBEDTLS_AESCE_C
55+
#endif
56+
57+
// Disable deprecated
58+
#define MBEDTLS_DEPRECATED_REMOVED
59+
60+
// mbedTLS 3.6 finally enabled TLSv1.3 by default, but it requires some mobule
61+
// changes, and to enable PSA crypto (new "standard" API specification).
62+
// Disable it for now.
63+
#undef MBEDTLS_SSL_PROTO_TLS1_3
64+
65+
// Disable PSA Crypto.
66+
#undef MBEDTLS_PSA_CRYPTO_CONFIG
67+
#undef MBEDTLS_PSA_CRYPTO_C
68+
#undef MBEDTLS_PSA_CRYPTO_STORAGE_C
69+
#undef MBEDTLS_PSA_ITS_FILE_C
70+
#undef MBEDTLS_LMS_C
71+
5672
#endif // GODOT_MBEDTLS_INCLUDE_H
5773

5874
#endif // GODOT_MODULE_MBEDTLS_CONFIG_H

0 commit comments

Comments
 (0)