Fix certificate generation with mbedtls 2.16.8 .
When generating certificates with `Crypto.generate_self_signed_certificate` we generate the PEM in a buffer via `mbedtls_x509write_crt_pem`. Since version 2.16.8, mbedtls adds spurious data at the end of the buffer due to internal optimizations, this breaks our logic when we try to immediately parse it and return a proper `X509Certificate` object. This commit updates the code to find the actual PEM length to parse using `strlen`, takes extra caution always adding the terminator to the buffer, and slightly improve error messages.
This commit is contained in:
parent
7f0352b4d1
commit
60687ce778
|
@ -43,8 +43,8 @@
|
||||||
#define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n"
|
#define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n"
|
||||||
#define PEM_END_CRT "-----END CERTIFICATE-----\n"
|
#define PEM_END_CRT "-----END CERTIFICATE-----\n"
|
||||||
|
|
||||||
#include "mbedtls/pem.h"
|
|
||||||
#include <mbedtls/debug.h>
|
#include <mbedtls/debug.h>
|
||||||
|
#include <mbedtls/pem.h>
|
||||||
|
|
||||||
CryptoKey *CryptoKeyMbedTLS::create() {
|
CryptoKey *CryptoKeyMbedTLS::create() {
|
||||||
return memnew(CryptoKeyMbedTLS);
|
return memnew(CryptoKeyMbedTLS);
|
||||||
|
@ -294,20 +294,15 @@ Ref<X509Certificate> CryptoMbedTLS::generate_self_signed_certificate(Ref<CryptoK
|
||||||
|
|
||||||
unsigned char buf[4096];
|
unsigned char buf[4096];
|
||||||
memset(buf, 0, 4096);
|
memset(buf, 0, 4096);
|
||||||
|
int ret = mbedtls_x509write_crt_pem(&crt, buf, 4096, mbedtls_ctr_drbg_random, &ctr_drbg);
|
||||||
|
mbedtls_mpi_free(&serial);
|
||||||
|
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.
|
||||||
|
|
||||||
Ref<X509CertificateMbedTLS> out;
|
Ref<X509CertificateMbedTLS> out;
|
||||||
out.instance();
|
out.instance();
|
||||||
mbedtls_x509write_crt_pem(&crt, buf, 4096, mbedtls_ctr_drbg_random, &ctr_drbg);
|
out->load_from_memory(buf, strlen((char *)buf) + 1); // Use strlen to find correct output size.
|
||||||
|
|
||||||
int err = mbedtls_x509_crt_parse(&(out->cert), buf, 4096);
|
|
||||||
if (err != 0) {
|
|
||||||
mbedtls_mpi_free(&serial);
|
|
||||||
mbedtls_x509write_crt_free(&crt);
|
|
||||||
ERR_PRINT("Generated invalid certificate: " + itos(err));
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
mbedtls_mpi_free(&serial);
|
|
||||||
mbedtls_x509write_crt_free(&crt);
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue