Skip to contentSkip to conversation

Replace deprecated mbedtls DRBG update function

PATCH In review Max Fillinger 4 participants 15 messages Aug 10, 2021 – Aug 12, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Replace deprecated mbedtls DRBG update function

The function mbedtls_ctr_drbg_update is deprecated as of mbedtls 2.16 and is superseded by mbedtls_ctr_drbg_update_ret, which returns an error code. This commit replaces the call to the deprecated function with the new one and logs a warning in case of an error.

For older versions of mbedtls, we add a compatibility function that runs mbedtls_ctr_drbg_update and returns 0.

---
 src/openvpn/ssl_mbedtls.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
src/openvpn/ssl_mbedtls.c +19 -1
index 265ea36f..1853335e 100644
@@ -62,6 +62,21 @@
 #include <mbedtls/oid.h>
 #include <mbedtls/pem.h>
 
+/**
+ * Compatibility: mbedtls_ctr_drbg_update was deprecated in mbedtls 2.16 and
+ * replaced with mbedtls_ctr_drbg_update_ret, which returns an error code.
+ * For older versions, we call mbedtls_ctr_drbg_update and return 0 (success).
+ */
+#if MBEDTLS_VERSION_NUMBER < 0x02100000
+static int mbedtls_ctr_drbg_update_ret(mbedtls_ctr_drbg_context *ctx,
+                                       const unsigned char *additional,
+                                       size_t add_len)
+{
+    mbedtls_ctr_drbg_update(ctx, additional, add_len);
+    return 0;
+}
+#endif
+
 static const mbedtls_x509_crt_profile openvpn_x509_crt_profile_legacy =
 {
     /* Hashes from SHA-1 and above */
@@ -950,7 +965,10 @@ tls_ctx_personalise_random(struct tls_root_ctx *ctx)
 
         if (0 != memcmp(old_sha256_hash, sha256_hash, sizeof(sha256_hash)))
         {
-            mbedtls_ctr_drbg_update(cd_ctx, sha256_hash, 32);
+            if (!mbed_ok(mbedtls_ctr_drbg_update_ret(cd_ctx, sha256_hash, 32)))
+            {
+                msg(M_WARN, "WARNING: failed to personalise random, could not update CTR_DRBG");
+            }
             memcpy(old_sha256_hash, sha256_hash, sizeof(old_sha256_hash));
         }
     }
Signature · 1 line
2.11.0
#2 lore raw
Quoted 7 lines

From: Arne Schwabe [mailto:[email protected]...]
Sent: dinsdag 10 augustus 2021 12:12
To: Maximilian Fillinger <[email protected]...>; openvpn- [email protected]
Subject: Re: [Openvpn-devel] [PATCH] Replace deprecated mbedtls DRBG update function

Quoted 7 lines

Am 10.08.21 um 08:16 schrieb Max Fillinger:

Quoted 1 line

+#if MBEDTLS_VERSION_NUMBER < 0x02100000

Is that really 2.16? Looking at the API doc
(https://tls.mbed.org/api/version_8h.html#adb4f54ebb33fd1a25e2c4d4480cf4
936)
it sounds like there should be a 16 in that number.

It's a hexadecimal 16. Version 2.26.0 for example does

#define MBEDTLS_VERSION_NUMBER 0x021A0000

#3 lore raw

Am 10.08.21 um 12:17 schrieb Maximilian Fillinger:

Quoted 10 lines
Quoted 10 lines

From: Arne Schwabe [mailto:[email protected]...]
Sent: dinsdag 10 augustus 2021 12:12
To: Maximilian Fillinger <[email protected]...>; openvpn- [email protected]
Subject: Re: [Openvpn-devel] [PATCH] Replace deprecated mbedtls DRBG update function

Am 10.08.21 um 08:16 schrieb Max Fillinger:

Quoted 1 line

+#if MBEDTLS_VERSION_NUMBER < 0x02100000

Quoted 9 lines
Quoted 4 lines

Is that really 2.16? Looking at the API doc
(https://tls.mbed.org/api/version_8h.html#adb4f54ebb33fd1a25e2c4d4480cf4
936)
it sounds like there should be a 16 in that number.

It's a hexadecimal 16. Version 2.26.0 for example does

#define MBEDTLS_VERSION_NUMBER 0x021A0000

Oh well that is confusing. Thanks for clearing that up.

Arne

Acked-by on v1 1/1
#4 lore raw
In reply to Max Fillinger

Am 10.08.21 um 08:16 schrieb Max Fillinger:

Quoted 10 lines

The function mbedtls_ctr_drbg_update is deprecated as of mbedtls 2.16 and is superseded by mbedtls_ctr_drbg_update_ret, which returns an error code. This commit replaces the call to the deprecated function with the new one and logs a warning in case of an error.

For older versions of mbedtls, we add a compatibility function that runs mbedtls_ctr_drbg_update and returns 0.

---

Normally we have patch v2 here and also a patch v2 in the subject (use -v 2 when doing git format-patch) but for this small patch it is not a problem.

Quoted 32 lines
 src/openvpn/ssl_mbedtls.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
src/openvpn/ssl_mbedtls.c +15 -0
index 265ea36f..1853335e 100644
@@ -62,6 +62,21 @@
 #include <mbedtls/oid.h>
 #include <mbedtls/pem.h>
 
+/**
+ * Compatibility: mbedtls_ctr_drbg_update was deprecated in mbedtls 2.16 and
+ * replaced with mbedtls_ctr_drbg_update_ret, which returns an error code.
+ * For older versions, we call mbedtls_ctr_drbg_update and return 0 (success).
+ */
+#if MBEDTLS_VERSION_NUMBER < 0x02100000
+static int mbedtls_ctr_drbg_update_ret(mbedtls_ctr_drbg_context *ctx,
+                                       const unsigned char *additional,
+                                       size_t add_len)
+{
+    mbedtls_ctr_drbg_update(ctx, additional, add_len);
+    return 0;
+}
+#endif
+
 static const mbedtls_x509_crt_profile openvpn_x509_crt_profile_legacy =
 {
     /* Hashes from SHA-1 and above */
@@ -950,7 +965,10 @@ tls_ctx_personalise_random(struct tls_root_ctx *ctx)
 

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

Quoted 10 lines
src/openvpn/ssl_mbedtls.c +4 -1
         if (0 != memcmp(old_sha256_hash, sha256_hash, sizeof(sha256_hash)))
         {
-            mbedtls_ctr_drbg_update(cd_ctx, sha256_hash, 32);
+            if (!mbed_ok(mbedtls_ctr_drbg_update_ret(cd_ctx, sha256_hash, 32)))
+            {
+                msg(M_WARN, "WARNING: failed to personalise random, could not update CTR_DRBG");
+            }
             memcpy(old_sha256_hash, sha256_hash, sizeof(old_sha256_hash));
         }
     }

Apart from the fact that we might want to abort (M_FATAL) if this fails instead basically ignoring the error and just log it, the change is fine. Considering the return status was ignored before, this patch is otherwise good. But failing also does not have any really bad impact... So:

#5 lore raw
Quoted 3 lines

Normally we have patch v2 here and also a patch v2 in the subject (use -v 2 when doing git format-patch) but for this small patch it is not a problem.

I'll keep it in mind for next time!

Quoted 4 lines

Apart from the fact that we might want to abort (M_FATAL) if this fails instead basically ignoring the error and just log it, the change is fine. Considering the return status was ignored before, this patch is otherwise good. But failing also does not have any really bad impact...

I was thinking about making it fatal, but there's another place where personalizing random can fail that also only gives a warning. So that's what I went with. (I wouldn't be opposed to making them both fatal, though.)

Anyway, thanks for the ACK!

#6 lore raw
Quoted 6 lines · Maximilian Fillinger
Quoted 3 lines

Normally we have patch v2 here and also a patch v2 in the subject (use -v 2 when doing git format-patch) but for this small patch it is not a problem.

I'll keep it in mind for next time!

Quoted 9 lines
Quoted 4 lines

Apart from the fact that we might want to abort (M_FATAL) if this fails instead basically ignoring the error and just log it, the change is fine. Considering the return status was ignored before, this patch is otherwise good. But failing also does not have any really bad impact...

I was thinking about making it fatal, but there's another place where personalizing random can fail that also only gives a warning. So that's what I went with. (I wouldn't be opposed to making them both fatal, though.)

IMHO it's ok to keep WARN for now because, like pointed out, other parts of the code path just warn in case of issues.

Just a few line above Max's change there is another failure treated with a WARN only.

Therefore I'd stick to WARN unless a complete revamp is performed on the entire codepath.

Regards,

Signature · 1 line
Antonio Quartulli
Acked-by on v1 1/1
#7 lore raw
In reply to Max Fillinger

Hi,

Quoted 9 lines · Max Fillinger

The function mbedtls_ctr_drbg_update is deprecated as of mbedtls 2.16 and is superseded by mbedtls_ctr_drbg_update_ret, which returns an error code. This commit replaces the call to the deprecated function with the new one and logs a warning in case of an error.

For older versions of mbedtls, we add a compatibility function that runs mbedtls_ctr_drbg_update and returns 0.

I also gave this patch a go and compiled it against:
* mbedTLS 2.26.0
* mbedTLS 2.20.0
* mbedTLS 2.10.0

all good.
Patch looks good to me too.

Signature · 1 line
Antonio Quartulli
#8 lore raw
In reply to Max Fillinger

I have only done a cursory look ("it's like v1 with the compat function"), and done a minimal test compile (Linux, mbedTLS 2.26.0, and FreeBSD, mbedTLS 2.16.11) + t_client test run. No surprises.

Your patch has been applied to the master branch.

commit b99fa3fd4fc41862354be709edb9877aae3e138c

Author: Max Fillinger
Date:   Tue Aug 10 08:16:44 2021 +0200
     Replace deprecated mbedtls DRBG update function

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

--
kind regards,

Gert Doering

#9 lore raw
In reply to Max Fillinger

Hi,

Quoted 7 lines · Max Fillinger

The function mbedtls_ctr_drbg_update is deprecated as of mbedtls 2.16 and is superseded by mbedtls_ctr_drbg_update_ret, which returns an error code. This commit replaces the call to the deprecated function with the new one and logs a warning in case of an error.

For older versions of mbedtls, we add a compatibility function that runs mbedtls_ctr_drbg_update and returns 0.

For the record, this breaks 2.14.*1* - which does have this function, and the static-vs-non-static prototyping causes a compile error.

So, followup patch needed.

2.14.0 does not have it, and from what I understand, 2.15.x does not have it either. But maybe we do not care for 2.15

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...
#10 lore raw

PATCH 1/1 mbedtls: do not define mbedtls_ctr_drbg_update_ret for mbedtls <2.15 and >=2.14.1

The mbedtls_ctr_drbg_update_ret() function was backported to the 2.14 branch of mbedtls since 2.14.1. For this reason, we must exclude the definition of our compat function for mbedtls >=2.14.1 but <2.15

---

Successfully compiled tested on mbedTLS:
* 2.10.0
* 2.14.1
* 2.20.0
* 2.26.0

 src/openvpn/ssl_mbedtls.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
src/openvpn/ssl_mbedtls.c +6 -1
index 1853335e..a2ed59ac 100644
@@ -66,8 +66,13 @@
  * Compatibility: mbedtls_ctr_drbg_update was deprecated in mbedtls 2.16 and
  * replaced with mbedtls_ctr_drbg_update_ret, which returns an error code.
  * For older versions, we call mbedtls_ctr_drbg_update and return 0 (success).
+ *
+ * Note: mbedtls_ctr_drbg_update_ret was backported to the mbedtls 2.14 branch
+ * since 2.14.1. For this reason, the definition of this function has to be
+ * excluded for any mbedtls going from 2.14.1 (included) to 2.15 (excluded)
  */
-#if MBEDTLS_VERSION_NUMBER < 0x02100000
+#if MBEDTLS_VERSION_NUMBER < 0x02100000 && \
+	!(MBEDTLS_VERSION_NUMBER > 0x020E0000 && MBEDTLS_VERSION_NUMBER < 0x020F0000)
 static int mbedtls_ctr_drbg_update_ret(mbedtls_ctr_drbg_context *ctx,
                                        const unsigned char *additional,
                                        size_t add_len)
Signature · 1 line
2.31.1
#11 lore raw

Re: [Openvpn-devel] [PATCH] mbedtls: do not define mbedtls_ctr_drbg_update_ret for mbedtls <2.15 and >=2.14.1

Hi,

Quoted 6 lines · Antonio Quartulli

The mbedtls_ctr_drbg_update_ret() function was backported to the 2.14 branch of mbedtls since 2.14.1. For this reason, we must exclude the definition of our compat function for mbedtls >=2.14.1 but <2.15

Quoted 7 lines

---

Successfully compiled tested on mbedTLS:
* 2.10.0
* 2.14.1
* 2.20.0
* 2.26.0

compilation fails against 2.7.18 as this function was also backported in 2.7.8.

Since we still want to support the 2.7 branch, as some distros are shipping this, we need to further extend the ifdef.

v2 is coming.

Cheers,

Signature · 1 line
Antonio Quartulli
#12 lore raw

PATCH v2 1/1 mbedtls: do not define mbedtls_ctr_drbg_update_ret when not needed

In reply to Antonio Quartulli

The mbedtls_ctr_drbg_update_ret() function was backported to various older branches, including 2.14 and 2.7.
To aqvoid making the if guard too complex, let's detect if this function exist at configure time.
All versions not having this function, will use our compat code.

---

Compile tests:
* Testing mbedtls-2.7.19...
* mbedtls-2.7.19 OK
* Testing mbedtls-2.10.0...
* mbedtls-2.10.0 OK
* Testing mbedtls-2.14.1...
* mbedtls-2.14.1 OK
* Testing mbedtls-2.16.11...
* mbedtls-2.16.11 OK
* Testing mbedtls-2.20.0...
* mbedtls-2.20.0 OK
* Testing mbedtls-2.26.0...
* mbedtls-2.26.0 OK
* Testing mbedtls-2.27.0...
* mbedtls-2.27.0 OK

 configure.ac              | 6 ++++++
 src/openvpn/ssl_mbedtls.c | 5 ++++-
 2 files changed, 10 insertions(+), 1 deletion(-)
configure.ac +6 -0
index 56b536dc..5d378962 100644
@@ -891,6 +891,12 @@ elif test "${with_crypto_library}" = "mbedtls"; then
 		[have_export_keying_material="no"]
 	)
 
+	AC_CHECK_FUNC(
+		[mbedtls_ctr_drbg_update_ret],
+		AC_DEFINE([HAVE_CTR_DRBG_UPDATE_RET], [1],
+			  [Use mbedtls_ctr_drbg_update_ret from mbed TLS]),
+	)
+
 	CFLAGS="${saved_CFLAGS}"
 	LIBS="${saved_LIBS}"
 	AC_DEFINE([ENABLE_CRYPTO_MBEDTLS], [1], [Use mbed TLS library])
src/openvpn/ssl_mbedtls.c +4 -1
index 1853335e..cea88f41 100644
@@ -66,8 +66,11 @@
  * Compatibility: mbedtls_ctr_drbg_update was deprecated in mbedtls 2.16 and
  * replaced with mbedtls_ctr_drbg_update_ret, which returns an error code.
  * For older versions, we call mbedtls_ctr_drbg_update and return 0 (success).
+ *
+ * Note: this change was backported to other mbedTLS branches, therefore we
+ * rely on function detection at configure time.
  */
-#if MBEDTLS_VERSION_NUMBER < 0x02100000
+#ifndef HAVE_CTR_DRBG_UPDATE_RET
 static int mbedtls_ctr_drbg_update_ret(mbedtls_ctr_drbg_context *ctx,
                                        const unsigned char *additional,
                                        size_t add_len)
Signature · 1 line
2.31.1
author Acked-by on v2 1/1
#13 lore raw

Re: [Openvpn-devel] [PATCH v2] mbedtls: do not define mbedtls_ctr_drbg_update_ret when not needed

Quoted 8 lines · Antonio Quartulli

The mbedtls_ctr_drbg_update_ret() function was backported to various older branches, including 2.14 and 2.7.
To aqvoid making the if guard too complex, let's detect if this function exist at configure time.
All versions not having this function, will use our compat code.

Thanks again for cleaning up my mess!

Compile-tested with mbedtls versions
2.27.0
2.26.0
2.25.0
2.16.11
2.15.1
2.14.1
2.14.0
2.12.0
2.7.19
2.7.0

All good!

(Typo: "aqvoid" in the commit message, but that can be fixed when committing it, right?)

#14 lore raw

[Openvpn-devel] [PATCH applied] Re: mbedtls: do not define mbedtls_ctr_drbg_update_ret when not needed

In reply to Antonio Quartulli

Thanks :-) (verified on that slightly old NetBSD 8.1 buildslave, and on more current installations)

Your patch has been applied to the master branch.

commit 2b9bbaadf44d4978c07abfdb5cce147c71cd9e8d

Author: Antonio Quartulli
Date:   Thu Aug 12 10:53:00 2021 +0200
     mbedtls: do not define mbedtls_ctr_drbg_update_ret when not needed

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

--
kind regards,

Gert Doering