From e1e69194118e16931b35b0f26dfe60fe8c97232c Mon Sep 17 00:00:00 2001 From: Federico Pellegrin Date: Tue, 18 Oct 2022 06:46:25 +0200 Subject: [PATCH] tls: fix order of operations to copy peer certificate The memcpy is done before assigning the length, so the length is not set and is therefore either 0 (so no peer certificate will be available) or a random number (that can lead to crashes) making the feature not work. The MR simply copies first the length that will make the memcpy work. --- hal/tls/mbedtls/tls_mbedtls.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hal/tls/mbedtls/tls_mbedtls.c b/hal/tls/mbedtls/tls_mbedtls.c index aa289222..9aac1172 100644 --- a/hal/tls/mbedtls/tls_mbedtls.c +++ b/hal/tls/mbedtls/tls_mbedtls.c @@ -189,8 +189,8 @@ verifyCertificate (void* parameter, mbedtls_x509_crt *crt, int certificate_depth self->peerCert = (uint8_t*) GLOBAL_MALLOC(crt->raw.len); if (self->peerCert) { - memcpy(self->peerCert, crt->raw.p, self->peerCertLength); self->peerCertLength = (int)crt->raw.len; + memcpy(self->peerCert, crt->raw.p, self->peerCertLength); } }