Skip to content

Commit

Permalink
mbedTLS: Update to new LTS v3.6.0
Browse files Browse the repository at this point in the history
Keep module compatibility with mbedtls 2.x (old LTS branch).

A patch has been added to allow compiling after removing all the `psa_*`
files from the library folder (will look into upstreaming it).

Note: mbedTLS 3.6 finally enabled TLSv1.3 by default, but it requires
some module changes, and to enable PSA crypto (new "standard" API
specification), so it might be best done in a separate commit/PR.
  • Loading branch information
lyuma authored and Faless committed Apr 10, 2024
1 parent 6c57928 commit 40fa684
Show file tree
Hide file tree
Showing 276 changed files with 97,293 additions and 38,624 deletions.
1 change: 1 addition & 0 deletions core/crypto/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ if not has_module:
"constant_time.c",
"ctr_drbg.c",
"entropy.c",
"md.c",
"md5.c",
"sha1.c",
"sha256.c",
Expand Down
3 changes: 3 additions & 0 deletions core/crypto/crypto_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
#include <mbedtls/md5.h>
#include <mbedtls/sha1.h>
#include <mbedtls/sha256.h>
#if MBEDTLS_VERSION_MAJOR >= 3
#include <mbedtls/compat-2.x.h>
#endif

// RandomGenerator
CryptoCore::RandomGenerator::RandomGenerator() {
Expand Down
32 changes: 18 additions & 14 deletions modules/mbedtls/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ thirdparty_obj = []
if env["builtin_mbedtls"]:
thirdparty_sources = [
"aes.c",
"aesce.c",
"aesni.c",
"arc4.c",
"aria.c",
"asn1parse.c",
"asn1write.c",
"base64.c",
"bignum.c",
"blowfish.c",
"bignum_core.c",
"bignum_mod_raw.c",
"camellia.c",
"ccm.c",
"certs.c",
"chacha20.c",
"chachapoly.c",
"cipher.c",
"cipher_wrap.c",
"cmac.c",
"ctr_drbg.c",
"constant_time.c",
"ctr_drbg.c",
"debug.c",
"des.c",
"dhm.c",
Expand All @@ -42,13 +42,10 @@ if env["builtin_mbedtls"]:
"entropy_poll.c",
"error.c",
"gcm.c",
"havege.c",
"hkdf.c",
"hmac_drbg.c",
"md2.c",
"md4.c",
"md5.c",
"md.c",
"md5.c",
"memory_buffer_alloc.c",
"mps_reader.c",
"mps_trace.c",
Expand All @@ -58,30 +55,37 @@ if env["builtin_mbedtls"]:
"padlock.c",
"pem.c",
"pk.c",
"pkcs11.c",
"pk_ecc.c",
"pk_wrap.c",
"pkcs12.c",
"pkcs5.c",
"pkcs7.c",
"pkparse.c",
"pk_wrap.c",
"pkwrite.c",
"platform.c",
"platform_util.c",
"poly1305.c",
"ripemd160.c",
"rsa.c",
"rsa_internal.c",
"rsa_alt_helpers.c",
"sha1.c",
"sha3.c",
"sha256.c",
"sha512.c",
"ssl_cache.c",
"ssl_ciphersuites.c",
"ssl_cli.c",
"ssl_client.c",
"ssl_cookie.c",
"ssl_debug_helpers_generated.c",
"ssl_msg.c",
"ssl_srv.c",
"ssl_ticket.c",
"ssl_tls.c",
"ssl_tls12_client.c",
"ssl_tls12_server.c",
"ssl_tls13_client.c",
"ssl_tls13_generic.c",
"ssl_tls13_keys.c",
"ssl_tls13_server.c",
"threading.c",
"timing.c",
"version.c",
Expand All @@ -91,9 +95,9 @@ if env["builtin_mbedtls"]:
"x509_crl.c",
"x509_crt.c",
"x509_csr.c",
"x509write.c",
"x509write_crt.c",
"x509write_csr.c",
"xtea.c",
]

thirdparty_dir = "#thirdparty/mbedtls/library/"
Expand Down
46 changes: 40 additions & 6 deletions modules/mbedtls/crypto_mbedtls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Error CryptoKeyMbedTLS::load(const String &p_path, bool p_public_only) {
if (p_public_only) {
ret = mbedtls_pk_parse_public_key(&pkey, out.ptr(), out.size());
} else {
ret = mbedtls_pk_parse_key(&pkey, out.ptr(), out.size(), nullptr, 0);
ret = _parse_key(out.ptr(), out.size());
}
// We MUST zeroize the memory for safety!
mbedtls_platform_zeroize(out.ptrw(), out.size());
Expand Down Expand Up @@ -108,7 +108,7 @@ Error CryptoKeyMbedTLS::load_from_string(const String &p_string_key, bool p_publ
if (p_public_only) {
ret = mbedtls_pk_parse_public_key(&pkey, (unsigned char *)p_string_key.utf8().get_data(), p_string_key.utf8().size());
} else {
ret = mbedtls_pk_parse_key(&pkey, (unsigned char *)p_string_key.utf8().get_data(), p_string_key.utf8().size(), nullptr, 0);
ret = _parse_key((unsigned char *)p_string_key.utf8().get_data(), p_string_key.utf8().size());
}
ERR_FAIL_COND_V_MSG(ret, FAILED, "Error parsing key '" + itos(ret) + "'.");

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

int CryptoKeyMbedTLS::_parse_key(const uint8_t *p_buf, int p_size) {
#if MBEDTLS_VERSION_MAJOR >= 3
mbedtls_entropy_context rng_entropy;
mbedtls_ctr_drbg_context rng_drbg;

mbedtls_ctr_drbg_init(&rng_drbg);
mbedtls_entropy_init(&rng_entropy);
int ret = mbedtls_ctr_drbg_seed(&rng_drbg, mbedtls_entropy_func, &rng_entropy, nullptr, 0);
ERR_FAIL_COND_V_MSG(ret != 0, ret, vformat("mbedtls_ctr_drbg_seed returned -0x%x\n", (unsigned int)-ret));

ret = mbedtls_pk_parse_key(&pkey, p_buf, p_size, nullptr, 0, mbedtls_ctr_drbg_random, &rng_drbg);
mbedtls_ctr_drbg_free(&rng_drbg);
mbedtls_entropy_free(&rng_entropy);
return ret;
#else
return mbedtls_pk_parse_key(&pkey, p_buf, p_size, nullptr, 0);
#endif
}

X509Certificate *X509CertificateMbedTLS::create() {
return memnew(X509CertificateMbedTLS);
}
Expand Down Expand Up @@ -393,12 +412,17 @@ Ref<X509Certificate> CryptoMbedTLS::generate_self_signed_certificate(Ref<CryptoK
mbedtls_x509write_crt_set_version(&crt, MBEDTLS_X509_CRT_VERSION_3);
mbedtls_x509write_crt_set_md_alg(&crt, MBEDTLS_MD_SHA256);

uint8_t rand_serial[20];
mbedtls_ctr_drbg_random(&ctr_drbg, rand_serial, sizeof(rand_serial));

#if MBEDTLS_VERSION_MAJOR >= 3
mbedtls_x509write_crt_set_serial_raw(&crt, rand_serial, sizeof(rand_serial));
#else
mbedtls_mpi serial;
mbedtls_mpi_init(&serial);
uint8_t rand_serial[20];
mbedtls_ctr_drbg_random(&ctr_drbg, rand_serial, 20);
ERR_FAIL_COND_V(mbedtls_mpi_read_binary(&serial, rand_serial, 20), nullptr);
ERR_FAIL_COND_V(mbedtls_mpi_read_binary(&serial, rand_serial, sizeof(rand_serial)), nullptr);
mbedtls_x509write_crt_set_serial(&crt, &serial);
#endif

mbedtls_x509write_crt_set_validity(&crt, p_not_before.utf8().get_data(), p_not_after.utf8().get_data());
mbedtls_x509write_crt_set_basic_constraints(&crt, 1, -1);
Expand All @@ -407,7 +431,9 @@ Ref<X509Certificate> CryptoMbedTLS::generate_self_signed_certificate(Ref<CryptoK
unsigned char buf[4096];
memset(buf, 0, 4096);
int ret = mbedtls_x509write_crt_pem(&crt, buf, 4096, mbedtls_ctr_drbg_random, &ctr_drbg);
#if MBEDTLS_VERSION_MAJOR < 3
mbedtls_mpi_free(&serial);
#endif
mbedtls_x509write_crt_free(&crt);
ERR_FAIL_COND_V_MSG(ret != 0, nullptr, "Failed to generate certificate: " + itos(ret));
buf[4095] = '\0'; // Make sure strlen can't fail.
Expand Down Expand Up @@ -461,9 +487,17 @@ Vector<uint8_t> CryptoMbedTLS::sign(HashingContext::HashType p_hash_type, const
ERR_FAIL_COND_V_MSG(!key.is_valid(), Vector<uint8_t>(), "Invalid key provided.");
ERR_FAIL_COND_V_MSG(key->is_public_only(), Vector<uint8_t>(), "Invalid key provided. Cannot sign with public_only keys.");
size_t sig_size = 0;
#if MBEDTLS_VERSION_MAJOR >= 3
unsigned char buf[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
#else
unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
#endif
Vector<uint8_t> out;
int ret = mbedtls_pk_sign(&(key->pkey), type, p_hash.ptr(), size, buf, &sig_size, mbedtls_ctr_drbg_random, &ctr_drbg);
int ret = mbedtls_pk_sign(&(key->pkey), type, p_hash.ptr(), size, buf,
#if MBEDTLS_VERSION_MAJOR >= 3
sizeof(buf),
#endif
&sig_size, mbedtls_ctr_drbg_random, &ctr_drbg);
ERR_FAIL_COND_V_MSG(ret, out, "Error while signing: " + itos(ret));
out.resize(sig_size);
memcpy(out.ptrw(), buf, sig_size);
Expand Down
2 changes: 2 additions & 0 deletions modules/mbedtls/crypto_mbedtls.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class CryptoKeyMbedTLS : public CryptoKey {
int locks = 0;
bool public_only = true;

int _parse_key(const uint8_t *p_buf, int p_size);

public:
static CryptoKey *create();
static void make_default() { CryptoKey::_create = create; }
Expand Down
1 change: 0 additions & 1 deletion modules/mbedtls/tls_context_mbedtls.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include "core/io/file_access.h"
#include "core/object/ref_counted.h"

#include <mbedtls/config.h>
#include <mbedtls/ctr_drbg.h>
#include <mbedtls/debug.h>
#include <mbedtls/entropy.h>
Expand Down
10 changes: 5 additions & 5 deletions thirdparty/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -520,18 +520,18 @@ in the MSVC debugger.
## mbedtls

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

File extracted from upstream release tarball:

- All `.h` from `include/mbedtls/` to `thirdparty/mbedtls/include/mbedtls/`
except `config_psa.h` and `psa_util.h`
and all `.h` from `include/psa/` to `thirdparty/mbedtls/include/psa/`
- All `.c` and `.h` from `library/` to `thirdparty/mbedtls/library/` except
those starting with `psa_*`
for the `psa_*.c` source files
- The `LICENSE` file (edited to keep only the Apache 2.0 variant)
- Applied the patch `windows-arm64-hardclock.diff` to fix Windows ARM64 build
Applied the patch `windows-entropy-bcrypt.diff` to fix Windows Store support
- Applied the patch `no-flexible-arrays.diff` to fix Windows build (see
upstream GH-9020)
- Added 2 files `godot_core_mbedtls_platform.c` and `godot_core_mbedtls_config.h`
providing configuration for light bundling with core
- Added the file `godot_module_mbedtls_config.h` to customize the build
Expand Down
26 changes: 21 additions & 5 deletions thirdparty/mbedtls/include/godot_module_mbedtls_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,34 @@
#else

// Include default mbedTLS config.
#include <mbedtls/config.h>
#include <mbedtls/mbedtls_config.h>

// Disable weak cryptography.
#undef MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED
#undef MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
#undef MBEDTLS_SSL_CBC_RECORD_SPLITTING
#undef MBEDTLS_SSL_PROTO_TLS1
#undef MBEDTLS_SSL_PROTO_TLS1_1
#undef MBEDTLS_ARC4_C
#undef MBEDTLS_DES_C
#undef MBEDTLS_DHM_C

#ifndef __linux__
// ARMv8 hardware AES operations. Detection only possible on linux.
#undef MBEDTLS_AESCE_C
#endif

// Disable deprecated
#define MBEDTLS_DEPRECATED_REMOVED

// mbedTLS 3.6 finally enabled TLSv1.3 by default, but it requires some mobule
// changes, and to enable PSA crypto (new "standard" API specification).
// Disable it for now.
#undef MBEDTLS_SSL_PROTO_TLS1_3

// Disable PSA Crypto.
#undef MBEDTLS_PSA_CRYPTO_CONFIG
#undef MBEDTLS_PSA_CRYPTO_C
#undef MBEDTLS_PSA_CRYPTO_STORAGE_C
#undef MBEDTLS_PSA_ITS_FILE_C
#undef MBEDTLS_LMS_C

#endif // GODOT_MBEDTLS_INCLUDE_H

#endif // GODOT_MODULE_MBEDTLS_CONFIG_H
Loading

0 comments on commit 40fa684

Please sign in to comment.