Skip to content

Commit d0dbb2f

Browse files
authored
Merge pull request #108382 from Faless/mbedtls/3.x_update_to_mbedtls_3.6.4
[3.x] mbedTLS: Update to mbedTLS 3.6.4
2 parents df4bca6 + 0770c9a commit d0dbb2f

File tree

296 files changed

+108373
-38881
lines changed

Some content is hidden

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

296 files changed

+108373
-38881
lines changed

core/crypto/SCsub

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,36 @@ if is_builtin or not has_module:
2020
# Only if the module is not enabled, we must compile here the required sources
2121
# to make a "light" build with only the necessary mbedtls files.
2222
if not has_module:
23+
# Minimal mbedTLS config file
24+
config_path = '\\"thirdparty/mbedtls/include/godot_core_mbedtls_config.h\\"'
25+
env_crypto.Append(CPPDEFINES=[("MBEDTLS_CONFIG_FILE", config_path)])
26+
# Build minimal mbedTLS library (MD5/SHA/Base64/AES).
2327
env_thirdparty = env_crypto.Clone()
2428
env_thirdparty.disable_warnings()
25-
# Custom config file
26-
env_thirdparty.Append(
27-
CPPDEFINES=[("MBEDTLS_CONFIG_FILE", '\\"thirdparty/mbedtls/include/godot_core_mbedtls_config.h\\"')]
28-
)
2929
thirdparty_mbedtls_dir = "#thirdparty/mbedtls/library/"
3030
thirdparty_mbedtls_sources = [
3131
"aes.c",
3232
"base64.c",
3333
"constant_time.c",
34+
"ctr_drbg.c",
35+
"entropy.c",
36+
"md.c",
3437
"md5.c",
3538
"sha1.c",
3639
"sha256.c",
3740
"godot_core_mbedtls_platform.c",
3841
]
3942
thirdparty_mbedtls_sources = [thirdparty_mbedtls_dir + file for file in thirdparty_mbedtls_sources]
4043
env_thirdparty.add_source_files(thirdparty_obj, thirdparty_mbedtls_sources)
44+
# Needed to force rebuilding the library when the configuration file is updated.
45+
env_thirdparty.Depends(thirdparty_obj, "#thirdparty/mbedtls/include/godot_core_mbedtls_config.h")
4146
env.core_sources += thirdparty_obj
42-
47+
elif is_builtin:
48+
# Module mbedTLS config file
49+
config_path = '\\"thirdparty/mbedtls/include/godot_module_mbedtls_config.h\\"'
50+
env_crypto.Append(CPPDEFINES=[("MBEDTLS_CONFIG_FILE", config_path)])
51+
# Needed to force rebuilding the core files when the configuration file is updated.
52+
thirdparty_obj = ["#thirdparty/mbedtls/include/godot_module_mbedtls_config.h"]
4353

4454
# Godot source files
4555

core/crypto/crypto_core.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
#include <mbedtls/md5.h>
3636
#include <mbedtls/sha1.h>
3737
#include <mbedtls/sha256.h>
38+
#if MBEDTLS_VERSION_MAJOR >= 3
39+
#include <mbedtls/compat-2.x.h>
40+
#endif
3841

3942
// MD5
4043
CryptoCore::MD5Context::MD5Context() {

doc/classes/EditorSettings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,10 @@
561561
<member name="network/ssl/editor_ssl_certificates" type="String" setter="" getter="">
562562
The SSL certificate bundle to use for HTTP requests made within the editor (e.g. from the AssetLib tab). If left empty, the [url=https://github.com/godotengine/godot/blob/master/thirdparty/certs/ca-certificates.crt]included Mozilla certificate bundle[/url] will be used.
563563
</member>
564+
<member name="network/ssl/enable_tls_v1.3" type="bool" setter="" getter="">
565+
If [code]true[/code], enable TLSv1.3 negotiation.
566+
[b]Note:[/b] Only supported when using Mbed TLS 3.0 or later (Linux distribution packages may be compiled against older system Mbed TLS packages), otherwise the maximum supported TLS version is always TLSv1.2.
567+
</member>
564568
<member name="project_manager/sorting_order" type="int" setter="" getter="">
565569
The sorting order to use in the project manager. When changing the sorting order in the project manager, this setting is set permanently in the editor settings.
566570
</member>

doc/classes/ProjectSettings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,10 @@
13321332
The CA certificates bundle to use for SSL connections. If this is set to a non-empty value, this will [i]override[/i] Godot's default [url=https://github.com/godotengine/godot/blob/master/thirdparty/certs/ca-certificates.crt]Mozilla certificate bundle[/url]. If left empty, the default certificate bundle will be used.
13331333
If in doubt, leave this setting empty.
13341334
</member>
1335+
<member name="network/ssl/enable_tls_v1.3" type="bool" setter="" getter="" default="true">
1336+
If [code]true[/code], enable TLSv1.3 negotiation.
1337+
[b]Note:[/b] Only supported when using Mbed TLS 3.0 or later (Linux distribution packages may be compiled against older system Mbed TLS packages), otherwise the maximum supported TLS version is always TLSv1.2.
1338+
</member>
13351339
<member name="node/name_casing" type="int" setter="" getter="" default="0">
13361340
When creating node names automatically, set the type of casing in this project. This is mostly an editor setting.
13371341
</member>

editor/editor_settings.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,7 @@ void EditorSettings::_load_defaults(Ref<ConfigFile> p_extra_config) {
687687
// SSL
688688
_initial_set("network/ssl/editor_ssl_certificates", _SYSTEM_CERTS_PATH);
689689
hints["network/ssl/editor_ssl_certificates"] = PropertyInfo(Variant::STRING, "network/ssl/editor_ssl_certificates", PROPERTY_HINT_GLOBAL_FILE, "*.crt,*.pem", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED);
690+
_initial_set("network/ssl/enable_tls_v1.3", true);
690691

691692
// HTTP Proxy
692693
_initial_set("network/http_proxy/host", "");

modules/mbedtls/SCsub

Lines changed: 37 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,53 @@ 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",
68+
"psa_crypto.c",
69+
"psa_crypto_aead.c",
70+
"psa_crypto_cipher.c",
71+
"psa_crypto_client.c",
72+
"psa_crypto_driver_wrappers_no_static.c",
73+
"psa_crypto_ecp.c",
74+
"psa_crypto_ffdh.c",
75+
"psa_crypto_hash.c",
76+
"psa_crypto_mac.c",
77+
"psa_crypto_pake.c",
78+
"psa_crypto_rsa.c",
79+
"psa_crypto_se.c",
80+
"psa_crypto_slot_management.c",
81+
"psa_crypto_storage.c",
82+
"psa_its_file.c",
83+
"psa_util.c",
7084
"ripemd160.c",
7185
"rsa.c",
72-
"rsa_internal.c",
86+
"rsa_alt_helpers.c",
7387
"sha1.c",
88+
"sha3.c",
7489
"sha256.c",
7590
"sha512.c",
7691
"ssl_cache.c",
7792
"ssl_ciphersuites.c",
78-
"ssl_cli.c",
93+
"ssl_client.c",
7994
"ssl_cookie.c",
95+
"ssl_debug_helpers_generated.c",
8096
"ssl_msg.c",
81-
"ssl_srv.c",
8297
"ssl_ticket.c",
8398
"ssl_tls.c",
99+
"ssl_tls12_client.c",
100+
"ssl_tls12_server.c",
101+
"ssl_tls13_client.c",
102+
"ssl_tls13_generic.c",
84103
"ssl_tls13_keys.c",
104+
"ssl_tls13_server.c",
85105
"threading.c",
86106
"timing.c",
87107
"version.c",
@@ -91,19 +111,22 @@ if env["builtin_mbedtls"]:
91111
"x509_crl.c",
92112
"x509_crt.c",
93113
"x509_csr.c",
114+
"x509write.c",
94115
"x509write_crt.c",
95116
"x509write_csr.c",
96-
"xtea.c",
97117
]
98118

99119
thirdparty_dir = "#thirdparty/mbedtls/library/"
100120
thirdparty_sources = [thirdparty_dir + file for file in thirdparty_sources]
101121

102122
env_mbed_tls.Prepend(CPPPATH=["#thirdparty/mbedtls/include/"])
123+
config_path = '\\"thirdparty/mbedtls/include/godot_module_mbedtls_config.h\\"'
124+
env_mbed_tls.Append(CPPDEFINES=[("MBEDTLS_CONFIG_FILE", config_path)])
103125

104126
env_thirdparty = env_mbed_tls.Clone()
105127
env_thirdparty.disable_warnings()
106128
env_thirdparty.add_source_files(thirdparty_obj, thirdparty_sources)
129+
env_thirdparty.Depends(thirdparty_obj, "#thirdparty/mbedtls/include/godot_module_mbedtls_config.h")
107130
env.modules_sources += thirdparty_obj
108131

109132

modules/mbedtls/crypto_mbedtls.cpp

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Error CryptoKeyMbedTLS::load(String p_path, bool p_public_only) {
7171
if (p_public_only) {
7272
ret = mbedtls_pk_parse_public_key(&pkey, out.read().ptr(), out.size());
7373
} else {
74-
ret = mbedtls_pk_parse_key(&pkey, out.read().ptr(), out.size(), nullptr, 0);
74+
ret = _parse_key(out.read().ptr(), out.size());
7575
}
7676
// We MUST zeroize the memory for safety!
7777
mbedtls_platform_zeroize(out.write().ptr(), out.size());
@@ -112,7 +112,7 @@ Error CryptoKeyMbedTLS::load_from_string(String p_string_key, bool p_public_only
112112
if (p_public_only) {
113113
ret = mbedtls_pk_parse_public_key(&pkey, (unsigned char *)p_string_key.utf8().get_data(), p_string_key.utf8().size());
114114
} else {
115-
ret = mbedtls_pk_parse_key(&pkey, (unsigned char *)p_string_key.utf8().get_data(), p_string_key.utf8().size(), nullptr, 0);
115+
ret = _parse_key((unsigned char *)p_string_key.utf8().get_data(), p_string_key.utf8().size());
116116
}
117117
ERR_FAIL_COND_V_MSG(ret, FAILED, "Error parsing key '" + itos(ret) + "'.");
118118

@@ -138,6 +138,25 @@ String CryptoKeyMbedTLS::save_to_string(bool p_public_only) {
138138
return s;
139139
}
140140

141+
int CryptoKeyMbedTLS::_parse_key(const uint8_t *p_buf, int p_size) {
142+
#if MBEDTLS_VERSION_MAJOR >= 3
143+
mbedtls_entropy_context rng_entropy;
144+
mbedtls_ctr_drbg_context rng_drbg;
145+
146+
mbedtls_ctr_drbg_init(&rng_drbg);
147+
mbedtls_entropy_init(&rng_entropy);
148+
int ret = mbedtls_ctr_drbg_seed(&rng_drbg, mbedtls_entropy_func, &rng_entropy, nullptr, 0);
149+
ERR_FAIL_COND_V_MSG(ret != 0, ret, vformat("mbedtls_ctr_drbg_seed returned -0x%x\n", (unsigned int)-ret));
150+
151+
ret = mbedtls_pk_parse_key(&pkey, p_buf, p_size, nullptr, 0, mbedtls_ctr_drbg_random, &rng_drbg);
152+
mbedtls_ctr_drbg_free(&rng_drbg);
153+
mbedtls_entropy_free(&rng_entropy);
154+
return ret;
155+
#else
156+
return mbedtls_pk_parse_key(&pkey, p_buf, p_size, nullptr, 0);
157+
#endif
158+
}
159+
141160
X509Certificate *X509CertificateMbedTLS::create() {
142161
return memnew(X509CertificateMbedTLS);
143162
}
@@ -267,10 +286,6 @@ Crypto *CryptoMbedTLS::create() {
267286
}
268287

269288
void CryptoMbedTLS::initialize_crypto() {
270-
#ifdef DEBUG_ENABLED
271-
mbedtls_debug_set_threshold(1);
272-
#endif
273-
274289
Crypto::_create = create;
275290
Crypto::_load_default_certificates = load_default_certificates;
276291
X509CertificateMbedTLS::make_default();
@@ -360,12 +375,18 @@ Ref<X509Certificate> CryptoMbedTLS::generate_self_signed_certificate(Ref<CryptoK
360375
mbedtls_x509write_crt_set_version(&crt, MBEDTLS_X509_CRT_VERSION_3);
361376
mbedtls_x509write_crt_set_md_alg(&crt, MBEDTLS_MD_SHA256);
362377

378+
uint8_t rand_serial[20];
379+
mbedtls_ctr_drbg_random(&ctr_drbg, rand_serial, sizeof(rand_serial));
380+
381+
#if MBEDTLS_VERSION_MAJOR >= 3
382+
mbedtls_x509write_crt_set_serial_raw(&crt, rand_serial, sizeof(rand_serial));
383+
#else
384+
363385
mbedtls_mpi serial;
364386
mbedtls_mpi_init(&serial);
365-
uint8_t rand_serial[20];
366-
mbedtls_ctr_drbg_random(&ctr_drbg, rand_serial, 20);
367-
ERR_FAIL_COND_V(mbedtls_mpi_read_binary(&serial, rand_serial, 20), nullptr);
387+
ERR_FAIL_COND_V(mbedtls_mpi_read_binary(&serial, rand_serial, sizeof(rand_serial)), nullptr);
368388
mbedtls_x509write_crt_set_serial(&crt, &serial);
389+
#endif
369390

370391
mbedtls_x509write_crt_set_validity(&crt, p_not_before.utf8().get_data(), p_not_after.utf8().get_data());
371392
mbedtls_x509write_crt_set_basic_constraints(&crt, 1, -1);
@@ -374,7 +395,9 @@ Ref<X509Certificate> CryptoMbedTLS::generate_self_signed_certificate(Ref<CryptoK
374395
unsigned char buf[4096];
375396
memset(buf, 0, 4096);
376397
int ret = mbedtls_x509write_crt_pem(&crt, buf, 4096, mbedtls_ctr_drbg_random, &ctr_drbg);
398+
#if MBEDTLS_VERSION_MAJOR < 3
377399
mbedtls_mpi_free(&serial);
400+
#endif
378401
mbedtls_x509write_crt_free(&crt);
379402
ERR_FAIL_COND_V_MSG(ret != 0, nullptr, "Failed to generate certificate: " + itos(ret));
380403
buf[4095] = '\0'; // Make sure strlen can't fail.
@@ -418,9 +441,18 @@ Vector<uint8_t> CryptoMbedTLS::sign(HashingContext::HashType p_hash_type, Vector
418441
ERR_FAIL_COND_V_MSG(!key.is_valid(), Vector<uint8_t>(), "Invalid key provided.");
419442
ERR_FAIL_COND_V_MSG(key->is_public_only(), Vector<uint8_t>(), "Invalid key provided. Cannot sign with public_only keys.");
420443
size_t sig_size = 0;
444+
#if MBEDTLS_VERSION_MAJOR >= 3
445+
unsigned char buf[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
446+
#else
421447
unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
448+
#endif
422449
Vector<uint8_t> out;
423-
int ret = mbedtls_pk_sign(&(key->pkey), type, p_hash.ptr(), size, buf, &sig_size, mbedtls_ctr_drbg_random, &ctr_drbg);
450+
int ret = mbedtls_pk_sign(&(key->pkey), type, p_hash.ptr(), size, buf,
451+
#if MBEDTLS_VERSION_MAJOR >= 3
452+
sizeof(buf),
453+
#endif
454+
&sig_size, mbedtls_ctr_drbg_random, &ctr_drbg);
455+
424456
ERR_FAIL_COND_V_MSG(ret, out, "Error while signing: " + itos(ret));
425457
out.resize(sig_size);
426458
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
@@ -48,6 +48,8 @@ class CryptoKeyMbedTLS : public CryptoKey {
4848
int locks = 0;
4949
bool public_only = true;
5050

51+
int _parse_key(const uint8_t *p_buf, int p_size);
52+
5153
public:
5254
static CryptoKey *create();
5355
static void make_default() { CryptoKey::_create = create; }

0 commit comments

Comments
 (0)