Skip to contentSkip to conversation

mbedtls: don't use API deprecated in mbed 2.7

PATCH In review Steffan Karger 3 participants 5 messages Feb 7, 2018 – Feb 26, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mbedtls: don't use API deprecated in mbed 2.7

The void-returning mbedtls_sha256() was deprecated in mbed TLS 2.7. Use our own md_full() abstraction instead.

(The new function can theoretically fail, but only in case of highly unlikely digest function failures. The personalisation on random using the certificate is a best-effort measure, so we simply log a warning and skip the personalisation if such highly unlikely errors occur.)

---
 src/openvpn/ssl_mbedtls.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
src/openvpn/ssl_mbedtls.c +6 -2
index 2a1215d..3906cd5 100644
@@ -60,7 +60,6 @@
 
 #include <mbedtls/oid.h>
 #include <mbedtls/pem.h>
-#include <mbedtls/sha256.h>
 
 static const mbedtls_x509_crt_profile openvpn_x509_crt_profile_legacy =
 {
@@ -851,9 +850,14 @@ tls_ctx_personalise_random(struct tls_root_ctx *ctx)
 
     if (NULL != ctx->crt_chain)
     {
+        const md_kt_t *sha256_kt = md_kt_get("SHA256");
         mbedtls_x509_crt *cert = ctx->crt_chain;
 
-        mbedtls_sha256(cert->tbs.p, cert->tbs.len, sha256_hash, false);
+        if (0 != md_full(sha256_kt, cert->tbs.p, cert->tbs.len, sha256_hash))
+        {
+            msg(M_WARN, "WARNING: failed to personalise random");
+        }
+
         if (0 != memcmp(old_sha256_hash, sha256_hash, sizeof(sha256_hash)))
         {
             mbedtls_ctr_drbg_update(cd_ctx, sha256_hash, 32);
Signature · 1 line
2.7.4
#1 lore raw

Hi,

src/openvpn/ssl_mbedtls.c tls_ctx_personalise_random(struct tls_root_ctx *ctx) · line 856

Quoted 2 lines · Steffan Karger
src/openvpn/ssl_mbedtls.c +1 -1
-        mbedtls_sha256(cert->tbs.p, cert->tbs.len, sha256_hash, false);
+        if (0 != md_full(sha256_kt, cert->tbs.p, cert->tbs.len, sha256_hash))

Why not using mbedtls_sha256_ret() since we are already in mbedtls-specific code here?
Any advantage in using a wrapper for another wrapper? :-P

(mbedtls_sha256_ret() is also the suggested replacement for mbedtls_sha256())

Moreover, SHA256 is statically selected, therefore using mbedtls_sha256_ret() would also avoid the md_kt_t local variable.

src/openvpn/ssl_mbedtls.c tls_ctx_personalise_random(struct tls_root_ctx *ctx) · line 860

Quoted 4 lines
src/openvpn/ssl_mbedtls.c +4 -0
+        {
+            msg(M_WARN, "WARNING: failed to personalise random");
+        }
+

Since we now have a reason for the failure, may it make sense to print a proper description based on the return value? (even though I think mbedtls_sha256_ret() can't really return something different from 0)

Quoted 4 lines
         if (0 != memcmp(old_sha256_hash, sha256_hash, sizeof(sha256_hash)))
         {
             mbedtls_ctr_drbg_update(cd_ctx, sha256_hash, 32);

Cheers,

Signature · 1 line
Antonio Quartulli
#2 lore raw

Hi,

Thanks for reviewing!

Quoted 8 lines · Antonio Quartulli

src/openvpn/ssl_mbedtls.c tls_ctx_personalise_random(struct tls_root_ctx *ctx) · line 856

Quoted 2 lines · Steffan Karger
src/openvpn/ssl_mbedtls.c +1 -1
-        mbedtls_sha256(cert->tbs.p, cert->tbs.len, sha256_hash, false);
+        if (0 != md_full(sha256_kt, cert->tbs.p, cert->tbs.len, sha256_hash))

Why not using mbedtls_sha256_ret() since we are already in mbedtls-specific code here?
Any advantage in using a wrapper for another wrapper? :-P

Quoted 6 lines

(mbedtls_sha256_ret() is also the suggested replacement for mbedtls_sha256())

Moreover, SHA256 is statically selected, therefore using mbedtls_sha256_ret() would also avoid the md_kt_t local variable.

This was the only place in the code where we took a (direct) dependency on functions from sha256.h. By using our internal wrapper, I could also do:

@@ -60,7 +60,6 @@

#include <mbedtls/oid.h>
#include <mbedtls/pem.h>
-#include <mbedtls/sha256.h>

Since reducing dependencies usually reduces maintenance burden, I prefer this solution.

Quoted 8 lines

src/openvpn/ssl_mbedtls.c tls_ctx_personalise_random(struct tls_root_ctx *ctx) · line 860

Quoted 4 lines
src/openvpn/ssl_mbedtls.c +4 -0
+        {
+            msg(M_WARN, "WARNING: failed to personalise random");
+        }
+

Since we now have a reason for the failure, may it make sense to print a proper description based on the return value? (even though I think mbedtls_sha256_ret() can't really return something different from 0)

md_full returns true/false, but you're right we can improve error reporting. Are you satisfied if I add an mbed_ok() wrapper *inside* md_full() to print the internal mbed error? I think that should be sufficient information.

-Steffan

#3 lore raw

Hi,

Quoted 2 lines · Steffan Karger

Since reducing dependencies usually reduces maintenance burden, I prefer this solution.

You're right here. Ok, let's keep md_full() then

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
src/openvpn/ssl_mbedtls.c +4 -0
+        {
+            msg(M_WARN, "WARNING: failed to personalise random");
+        }
+
Quoted 8 lines
Quoted 3 lines

Since we now have a reason for the failure, may it make sense to print a proper description based on the return value? (even though I think mbedtls_sha256_ret() can't really return something different from 0)

md_full returns true/false, but you're right we can improve error reporting. Are you satisfied if I add an mbed_ok() wrapper *inside* md_full() to print the internal mbed error? I think that should be sufficient information.

Yeah, that would be nice. Thanks

I guess this patch fine as it is then. The mbed_ok() can be introduced with another patch.

Cheers,

Signature · 1 line
Antonio Quartulli
#4 lore raw
In reply to Steffan Karger

Your patch has been applied to the master and release/2.4 branch.

(release/2.4 due to "long term compatibility" reasoning)

Smoke-tested 2.4 vs. mbedTLS 2.7 on Gentoo ("works").

commit f22e89bd2311d3cab511e574746c6f82f1fa1a54 (master) commit 7c913600105505bd7f539d6b4206d358b6811943 (release/2.4)

Author: Steffan Karger
Date:   Wed Feb 7 13:22:46 2018 +0100
     mbedtls: don't use API deprecated in mbed 2.7

     Signed-off-by: Steffan Karger <[email protected]...>
     Acked-by: Antonio Quartulli <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg16445.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering