Skip to contentSkip to conversation

Do not write extra 0 byte for --gen-key with auth-token/tls-crypt-v2

PATCH Superseded Arne Schwabe 4 participants 9 messages Apr 6, 2020 – May 15, 2020 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Do not write extra 0 byte for --gen-key with auth-token/tls-crypt-v2

crypto_pem_encode put a nul-terminated terminated string into the buffer. This is useful for printf but should not be written into the file.

Also for static keys, we were missing the nul termination when priting it to stadout but since the buffer was cleared before, there was always a NULL byte in the right place. Make it explicit instead.

---
 src/openvpn/crypto.c    | 11 +++++++++--
 src/openvpn/tls_crypt.c | 10 ++++++++--
 2 files changed, 17 insertions(+), 4 deletions(-)
src/openvpn/crypto.c +9 -2
index 453cb20a..7af48df0 100644
@@ -1477,6 +1477,7 @@ write_key_file(const int nkeys, const char *filename)
     /* write key file to stdout if no filename given */
     if (!filename || strcmp(filename, "")==0)
     {
+        buf_null_terminate(&out);
         printf("%s\n", BPTR(&out));
     }
     /* write key file, now formatted in out, to file */
@@ -1888,9 +1889,15 @@ write_pem_key_file(const char *filename, const char *pem_name)
     {
         printf("%s\n", BPTR(&server_key_pem));
     }
-    else if (!buffer_write_file(filename, &server_key_pem))
+    else
     {
-        msg(M_ERR, "ERROR: could not write key file");
+        /* crypto_pem_encode null terminates the buffer, do
+         * not write this to the file */
+        buf_inc_len(&server_key_pem, -1);
+        if (!buffer_write_file(filename, &server_key_pem))
+        {
+            msg(M_ERR, "ERROR: could not write key file");
+        }
         goto cleanup;
     }
 
src/openvpn/tls_crypt.c +8 -2
index e9f9cc2a..85f2862b 100644
@@ -706,9 +706,15 @@ tls_crypt_v2_write_client_key_file(const char *filename,
         client_filename = INLINE_FILE_TAG;
         client_inline = (const char *)BPTR(&client_key_pem);
     }
-    else if (!buffer_write_file(filename, &client_key_pem))
+    else
     {
-        msg(M_FATAL, "ERROR: could not write client key file");
+        /* crypto_pem_encode null terminates the buffer, do
+         * not write this to the file */
+        buf_inc_len(&client_key_pem, -1);
+        if (!buffer_write_file(filename, &client_key_pem))
+        {
+            msg(M_FATAL, "ERROR: could not write client key file");
+        }
         goto cleanup;
     }
 
Signature · 1 line
2.26.0
Tested-by on v1 1/1
#1 lore raw

I have tested this fix all the way to ensuring that tls-cypt-v2.keys are created successfully and do allow successful VPN connection.

I have not tested auth-tokens.

Quoted 67 lines · Arne Schwabe

crypto_pem_encode put a nul-terminated terminated string into the buffer. This is useful for printf but should not be written into the file.

Also for static keys, we were missing the nul termination when priting it to stadout but since the buffer was cleared before, there was always a NULL byte in the right place. Make it explicit instead.

---
  src/openvpn/crypto.c    | 11 +++++++++--
  src/openvpn/tls_crypt.c | 10 ++++++++--
  2 files changed, 17 insertions(+), 4 deletions(-)
src/openvpn/crypto.c +9 -2
index 453cb20a..7af48df0 100644
@@ -1477,6 +1477,7 @@ write_key_file(const int nkeys, const char *filename)
      /* write key file to stdout if no filename given */
      if (!filename || strcmp(filename, "")==0)
      {
+        buf_null_terminate(&out);
          printf("%s\n", BPTR(&out));
      }
      /* write key file, now formatted in out, to file */
@@ -1888,9 +1889,15 @@ write_pem_key_file(const char *filename, const char *pem_name)
      {
          printf("%s\n", BPTR(&server_key_pem));
      }
-    else if (!buffer_write_file(filename, &server_key_pem))
+    else
      {
-        msg(M_ERR, "ERROR: could not write key file");
+        /* crypto_pem_encode null terminates the buffer, do
+         * not write this to the file */
+        buf_inc_len(&server_key_pem, -1);
+        if (!buffer_write_file(filename, &server_key_pem))
+        {
+            msg(M_ERR, "ERROR: could not write key file");
+        }
          goto cleanup;
      }
 
src/openvpn/tls_crypt.c +8 -2
index e9f9cc2a..85f2862b 100644
@@ -706,9 +706,15 @@ tls_crypt_v2_write_client_key_file(const char *filename,
          client_filename = INLINE_FILE_TAG;
          client_inline = (const char *)BPTR(&client_key_pem);
      }
-    else if (!buffer_write_file(filename, &client_key_pem))
+    else
      {
-        msg(M_FATAL, "ERROR: could not write client key file");
+        /* crypto_pem_encode null terminates the buffer, do
+         * not write this to the file */
+        buf_inc_len(&client_key_pem, -1);
+        if (!buffer_write_file(filename, &client_key_pem))
+        {
+            msg(M_FATAL, "ERROR: could not write client key file");
+        }
          goto cleanup;
      }
 

A message that is not in the archive (sent off-list or not yet received): [email protected]...

#3 lore raw

Am 06.04.20 um 17:44 schrieb Steffan Karger:

Quoted 75 lines

Hi,

Quoted 57 lines · Arne Schwabe

crypto_pem_encode put a nul-terminated terminated string into the buffer. This is useful for printf but should not be written into the file.

Also for static keys, we were missing the nul termination when priting it to stadout but since the buffer was cleared before, there was always a NULL byte in the right place. Make it explicit instead.

---
 src/openvpn/crypto.c    | 11 +++++++++--
 src/openvpn/tls_crypt.c | 10 ++++++++--
 2 files changed, 17 insertions(+), 4 deletions(-)
src/openvpn/crypto.c +9 -2
index 453cb20a..7af48df0 100644
@@ -1477,6 +1477,7 @@ write_key_file(const int nkeys, const char *filename)
     /* write key file to stdout if no filename given */
     if (!filename || strcmp(filename, "")==0)
     {
+        buf_null_terminate(&out);
         printf("%s\n", BPTR(&out));
     }
     /* write key file, now formatted in out, to file */
@@ -1888,9 +1889,15 @@ write_pem_key_file(const char *filename, const char *pem_name)
     {
         printf("%s\n", BPTR(&server_key_pem));
     }
-    else if (!buffer_write_file(filename, &server_key_pem))
+    else
     {
-        msg(M_ERR, "ERROR: could not write key file");
+        /* crypto_pem_encode null terminates the buffer, do
+         * not write this to the file */
+        buf_inc_len(&server_key_pem, -1);
+        if (!buffer_write_file(filename, &server_key_pem))
+        {
+            msg(M_ERR, "ERROR: could not write key file");
+        }
         goto cleanup;
     }
 
src/openvpn/tls_crypt.c +1 -2
index e9f9cc2a..85f2862b 100644
@@ -706,9 +706,15 @@ tls_crypt_v2_write_client_key_file(const char *filename,
         client_filename = INLINE_FILE_TAG;
         client_inline = (const char *)BPTR(&client_key_pem);
     }
-    else if (!buffer_write_file(filename, &client_key_pem))
+    else
     {
-        msg(M_FATAL, "ERROR: could not write client key file");

src/openvpn/tls_crypt.c tls_crypt_v2_write_client_key_file(const char *filename, · line 720

Quoted 10 lines
src/openvpn/tls_crypt.c +7 -0
+        /* crypto_pem_encode null terminates the buffer, do
+         * not write this to the file */
+        buf_inc_len(&client_key_pem, -1);
+        if (!buffer_write_file(filename, &client_key_pem))
+        {
+            msg(M_FATAL, "ERROR: could not write client key file");
+        }
         goto cleanup;
     }
 

Fix is correct, but I'm not too happy with changing the buffer from being null-terminated to not-null-terminated. This change itself is correct, but it feels like a recipe for mistakes in the future.

Quoted 3 lines

What would you think about adding a length parameter to buffer_write_file() instead, so that the API there changes to "write len bytes of data to file"?

So there are three alternatives how to things:

a) make pem_encode behave more like other similar functions in OepnVPN and do not null terminate. Then we would need to call buf_null_terminate like in the static key path

b) do what this patch does

c) have buffer_write_file take a length argument.

I considered c) but it felt wrong because the function currently just does a very simple job.

I did not do a) because that would make printing the buffer etc more complicated since either pem_encode needs to allocate a bigger buffer than it needs to or we need to copy the buffer into one that allows an extra null byte.

That being said I don't feel stronlgy about this and can send an updated patch that is a) or c)

Arne

#4 lore raw

PATCH v2 1/1 Do not write extra 0 byte for --gen-key with auth-token/tls-crypt-v2

In reply to Arne Schwabe

Change crypto_pem_encode to not put a nul-terminated terminated string into the buffer. This was useful for printf but should not be written into the file.

Instead do not assume that the buffer is null terminated and print only the number of bytes in the buffer. Also fix a similar case in printing static key where the 0 byte was never added to the buffer

Patch V2: make pem_encode behave more like other similar functions in OpenVPN
and do not null terminate.

---
 src/openvpn/crypto.c                      | 4 ++--
 src/openvpn/crypto_mbedtls.c              | 4 +++-
 src/openvpn/crypto_openssl.c              | 3 +--
 src/openvpn/tls_crypt.c                   | 2 +-
 tests/unit_tests/openvpn/test_tls_crypt.c | 6 ++++--
 5 files changed, 11 insertions(+), 8 deletions(-)
src/openvpn/crypto.c +2 -2
index 1678cba8..b05262e1 100644
@@ -1478,7 +1478,7 @@ write_key_file(const int nkeys, const char *filename)
     /* write key file to stdout if no filename given */
     if (!filename || strcmp(filename, "")==0)
     {
-        printf("%s\n", BPTR(&out));
+        printf("%.*s\n", BLEN(&out), BPTR(&out));
     }
     /* write key file, now formatted in out, to file */
     else if (!buffer_write_file(filename, &out))
@@ -1887,7 +1887,7 @@ write_pem_key_file(const char *filename, const char *pem_name)
 
     if (!filename || strcmp(filename, "")==0)
     {
-        printf("%s\n", BPTR(&server_key_pem));
+        printf("%.*s", BLEN(&server_key_pem), BPTR(&server_key_pem));
     }
     else if (!buffer_write_file(filename, &server_key_pem))
     {
src/openvpn/crypto_mbedtls.c +3 -1
index 3e77fa9e..14a528af 100644
@@ -239,10 +239,12 @@ crypto_pem_encode(const char *name, struct buffer *dst,
         return false;
     }
 
+    /* We set the size buf to out_len-1 to NOT include the 0 byte that
+     * mbedtls_pem_write_buffer in its length calculation */
     *dst = alloc_buf_gc(out_len, gc);
     if (!mbed_ok(mbedtls_pem_write_buffer(header, footer, BPTR(src), BLEN(src),
                                           BPTR(dst), BCAP(dst), &out_len))
-        || !buf_inc_len(dst, out_len))
+        || !buf_inc_len(dst, out_len-1))
     {
         CLEAR(*dst);
         return false;
src/openvpn/crypto_openssl.c +1 -2
index a81dcfd8..4fa65ba8 100644
@@ -400,9 +400,8 @@ crypto_pem_encode(const char *name, struct buffer *dst,
     BUF_MEM *bptr;
     BIO_get_mem_ptr(bio, &bptr);
 
-    *dst = alloc_buf_gc(bptr->length + 1, gc);
+    *dst = alloc_buf_gc(bptr->length, gc);
     ASSERT(buf_write(dst, bptr->data, bptr->length));
-    buf_null_terminate(dst);
 
     ret = true;
 cleanup:
src/openvpn/tls_crypt.c +1 -1
index e9f9cc2a..3018c18e 100644
@@ -702,7 +702,7 @@ tls_crypt_v2_write_client_key_file(const char *filename,
 
     if (!filename || streq(filename, ""))
     {
-        printf("%s\n", BPTR(&client_key_pem));
+        printf("%.*s\n", BLEN(&client_key_pem), BPTR(&client_key_pem));
         client_filename = INLINE_FILE_TAG;
         client_inline = (const char *)BPTR(&client_key_pem);
     }
tests/unit_tests/openvpn/test_tls_crypt.c +4 -2
index b9e3a7a6..54fc917d 100644
@@ -512,7 +512,8 @@ test_tls_crypt_v2_write_server_key_file(void **state) {
     const char *filename = "testfilename.key";
 
     expect_string(__wrap_buffer_write_file, filename, filename);
-    expect_string(__wrap_buffer_write_file, pem, test_server_key);
+    expect_memory(__wrap_buffer_write_file, pem, test_server_key,
+                  strlen(test_server_key));
     will_return(__wrap_buffer_write_file, true);
 
     tls_crypt_v2_write_server_key_file(filename);
@@ -524,7 +525,8 @@ test_tls_crypt_v2_write_client_key_file(void **state) {
 
     /* Test writing the client key */
     expect_string(__wrap_buffer_write_file, filename, filename);
-    expect_string(__wrap_buffer_write_file, pem, test_client_key);
+    expect_memory(__wrap_buffer_write_file, pem, test_client_key,
+                  strlen(test_client_key));
     will_return(__wrap_buffer_write_file, true);
 
     /* Key generation re-reads the created file as a sanity check */
Signature · 1 line
2.26.0
#5 lore raw

Hi,

Quoted 105 lines · Arne Schwabe

Change crypto_pem_encode to not put a nul-terminated terminated string into the buffer. This was useful for printf but should not be written into the file.

Instead do not assume that the buffer is null terminated and print only the number of bytes in the buffer. Also fix a similar case in printing static key where the 0 byte was never added to the buffer

Patch V2: make pem_encode behave more like other similar functions in OpenVPN
and do not null terminate.

---
 src/openvpn/crypto.c                      | 4 ++--
 src/openvpn/crypto_mbedtls.c              | 4 +++-
 src/openvpn/crypto_openssl.c              | 3 +--
 src/openvpn/tls_crypt.c                   | 2 +-
 tests/unit_tests/openvpn/test_tls_crypt.c | 6 ++++--
 5 files changed, 11 insertions(+), 8 deletions(-)
src/openvpn/crypto.c +2 -2
index 1678cba8..b05262e1 100644
@@ -1478,7 +1478,7 @@ write_key_file(const int nkeys, const char *filename)
     /* write key file to stdout if no filename given */
     if (!filename || strcmp(filename, "")==0)
     {
-        printf("%s\n", BPTR(&out));
+        printf("%.*s\n", BLEN(&out), BPTR(&out));
     }
     /* write key file, now formatted in out, to file */
     else if (!buffer_write_file(filename, &out))
@@ -1887,7 +1887,7 @@ write_pem_key_file(const char *filename, const char *pem_name)
 
     if (!filename || strcmp(filename, "")==0)
     {
-        printf("%s\n", BPTR(&server_key_pem));
+        printf("%.*s", BLEN(&server_key_pem), BPTR(&server_key_pem));
     }
     else if (!buffer_write_file(filename, &server_key_pem))
     {
src/openvpn/crypto_mbedtls.c +3 -1
index 3e77fa9e..14a528af 100644
@@ -239,10 +239,12 @@ crypto_pem_encode(const char *name, struct buffer *dst,
         return false;
     }
 
+    /* We set the size buf to out_len-1 to NOT include the 0 byte that
+     * mbedtls_pem_write_buffer in its length calculation */
     *dst = alloc_buf_gc(out_len, gc);
     if (!mbed_ok(mbedtls_pem_write_buffer(header, footer, BPTR(src), BLEN(src),
                                           BPTR(dst), BCAP(dst), &out_len))
-        || !buf_inc_len(dst, out_len))
+        || !buf_inc_len(dst, out_len-1))
     {
         CLEAR(*dst);
         return false;
src/openvpn/crypto_openssl.c +1 -2
index a81dcfd8..4fa65ba8 100644
@@ -400,9 +400,8 @@ crypto_pem_encode(const char *name, struct buffer *dst,
     BUF_MEM *bptr;
     BIO_get_mem_ptr(bio, &bptr);
 
-    *dst = alloc_buf_gc(bptr->length + 1, gc);
+    *dst = alloc_buf_gc(bptr->length, gc);
     ASSERT(buf_write(dst, bptr->data, bptr->length));
-    buf_null_terminate(dst);
 
     ret = true;
 cleanup:
src/openvpn/tls_crypt.c +1 -1
index e9f9cc2a..3018c18e 100644
@@ -702,7 +702,7 @@ tls_crypt_v2_write_client_key_file(const char *filename,
 
     if (!filename || streq(filename, ""))
     {
-        printf("%s\n", BPTR(&client_key_pem));
+        printf("%.*s\n", BLEN(&client_key_pem), BPTR(&client_key_pem));
         client_filename = INLINE_FILE_TAG;
         client_inline = (const char *)BPTR(&client_key_pem);
     }
tests/unit_tests/openvpn/test_tls_crypt.c +2 -1
index b9e3a7a6..54fc917d 100644
@@ -512,7 +512,8 @@ test_tls_crypt_v2_write_server_key_file(void **state) {
     const char *filename = "testfilename.key";
 
     expect_string(__wrap_buffer_write_file, filename, filename);
-    expect_string(__wrap_buffer_write_file, pem, test_server_key);
+    expect_memory(__wrap_buffer_write_file, pem, test_server_key,
+                  strlen(test_server_key));
     will_return(__wrap_buffer_write_file, true);
 
     tls_crypt_v2_write_server_key_file(filename);

tests/unit_tests/openvpn/test_tls_crypt.c test_tls_crypt_v2_write_client_key_file(void **state) { · line 532

Quoted 9 lines
tests/unit_tests/openvpn/test_tls_crypt.c +2 -1
@@ -524,7 +525,8 @@ test_tls_crypt_v2_write_client_key_file(void **state) {
 
     /* Test writing the client key */
     expect_string(__wrap_buffer_write_file, filename, filename);
-    expect_string(__wrap_buffer_write_file, pem, test_client_key);
+    expect_memory(__wrap_buffer_write_file, pem, test_client_key,
+                  strlen(test_client_key));
     will_return(__wrap_buffer_write_file, true);
 
     /* Key generation re-reads the created file as a sanity check */

Didn't review in detail yet, but this patch now fails "make check" with mbedtls:

[==========] Running 1 test(s).
[ RUN ] crypto_pem_encode_decode_loopback
PEM decode error: source buffer not null-terminated PEM decode error: source buffer not null-terminated
[ ERROR ] --- crypto_pem_decode("TESTKEYNAME", &dec_buf, &pem_buf)
[ LINE ] --- openvpn/tests/unit_tests/openvpn/test_crypto.c:64:
error: Failure!
[ FAILED ] crypto_pem_encode_decode_loopback [==========] 1 test(s) run.

[  PASSED  ] 0 test(s).
[  FAILED  ] 1 test(s), listed below:
[  FAILED  ] crypto_pem_encode_decode_loopback

1 FAILED TEST(S)
FAIL: crypto_testdriver

With openssl, make check succeeds.

-Steffan

#6 lore raw

PATCH v3 1/1 Do not write extra 0 byte for --gen-key with auth-token/tls-crypt-v2

Change crypto_pem_encode to not put a nul-terminated terminated string into the buffer. This was useful for printf but should not be written into the file.

Instead do not assume that the buffer is null terminated and print only the number of bytes in the buffer. Also fix a similar case in printing static key where the 0 byte was never added to the buffer

Patch V2: make pem_encode behave more like other similar functions in OpenVPN
and do not null terminate.

Patch V3: also make the mbed TLS variant of pem_decode behave like other
similar functions in OpeNVPN and accept a not null-terminated buffer.

---
 src/openvpn/crypto.c                      |  4 ++--
 src/openvpn/crypto_mbedtls.c              | 28 +++++++++++++++++------
 src/openvpn/crypto_openssl.c              |  3 +--
 src/openvpn/tls_crypt.c                   |  2 +-
 tests/unit_tests/openvpn/test_tls_crypt.c |  6 +++--
 5 files changed, 29 insertions(+), 14 deletions(-)
src/openvpn/crypto.c +2 -2
index 1678cba8..b05262e1 100644
@@ -1478,7 +1478,7 @@ write_key_file(const int nkeys, const char *filename)
     /* write key file to stdout if no filename given */
     if (!filename || strcmp(filename, "")==0)
     {
-        printf("%s\n", BPTR(&out));
+        printf("%.*s\n", BLEN(&out), BPTR(&out));
     }
     /* write key file, now formatted in out, to file */
     else if (!buffer_write_file(filename, &out))
@@ -1887,7 +1887,7 @@ write_pem_key_file(const char *filename, const char *pem_name)
 
     if (!filename || strcmp(filename, "")==0)
     {
-        printf("%s\n", BPTR(&server_key_pem));
+        printf("%.*s", BLEN(&server_key_pem), BPTR(&server_key_pem));
     }
     else if (!buffer_write_file(filename, &server_key_pem))
     {
src/openvpn/crypto_mbedtls.c +21 -7
index 3e77fa9e..9529b319 100644
@@ -239,10 +239,12 @@ crypto_pem_encode(const char *name, struct buffer *dst,
         return false;
     }
 
+    /* We set the size buf to out_len-1 to NOT include the 0 byte that
+     * mbedtls_pem_write_buffer in its length calculation */
     *dst = alloc_buf_gc(out_len, gc);
     if (!mbed_ok(mbedtls_pem_write_buffer(header, footer, BPTR(src), BLEN(src),
                                           BPTR(dst), BCAP(dst), &out_len))
-        || !buf_inc_len(dst, out_len))
+        || !buf_inc_len(dst, out_len-1))
     {
         CLEAR(*dst);
         return false;
@@ -259,11 +261,6 @@ crypto_pem_decode(const char *name, struct buffer *dst,
     char header[1000+1] = { 0 };
     char footer[1000+1] = { 0 };
 
-    if (*(BLAST(src)) != '\0')
-    {
-        msg(M_WARN, "PEM decode error: source buffer not null-terminated");
-        return false;
-    }
     if (!openvpn_snprintf(header, sizeof(header), "-----BEGIN %s-----", name))
     {
         return false;
@@ -273,9 +270,25 @@ crypto_pem_decode(const char *name, struct buffer *dst,
         return false;
     }
 
+    /* mbed TLS requires the src to be null-terminated */
+    struct gc_arena gc = gc_new();
+    struct buffer input;
+
+    if (*(BLAST(src)) == '\0')
+    {
+        input = *src;
+    }
+    else
+    {
+        /* allocate a new buffer to avoid modifying the src buffer */
+        input = alloc_buf_gc(BLEN(src) + 1, &gc);
+        buf_copy(&input, src);
+        buf_null_terminate(&input);
+    }
+
     size_t use_len = 0;
     mbedtls_pem_context ctx = { 0 };
-    bool ret = mbed_ok(mbedtls_pem_read_buffer(&ctx, header, footer, BPTR(src),
+    bool ret = mbed_ok(mbedtls_pem_read_buffer(&ctx, header, footer, BPTR(&input),
                                                NULL, 0, &use_len));
     if (ret && !buf_write(dst, ctx.buf, ctx.buflen))
     {
@@ -284,6 +297,7 @@ crypto_pem_decode(const char *name, struct buffer *dst,
     }
 
     mbedtls_pem_free(&ctx);
+    gc_free(&gc);
     return ret;
 }
 
src/openvpn/crypto_openssl.c +1 -2
index a81dcfd8..4fa65ba8 100644
@@ -400,9 +400,8 @@ crypto_pem_encode(const char *name, struct buffer *dst,
     BUF_MEM *bptr;
     BIO_get_mem_ptr(bio, &bptr);
 
-    *dst = alloc_buf_gc(bptr->length + 1, gc);
+    *dst = alloc_buf_gc(bptr->length, gc);
     ASSERT(buf_write(dst, bptr->data, bptr->length));
-    buf_null_terminate(dst);
 
     ret = true;
 cleanup:
src/openvpn/tls_crypt.c +1 -1
index e9f9cc2a..3018c18e 100644
@@ -702,7 +702,7 @@ tls_crypt_v2_write_client_key_file(const char *filename,
 
     if (!filename || streq(filename, ""))
     {
-        printf("%s\n", BPTR(&client_key_pem));
+        printf("%.*s\n", BLEN(&client_key_pem), BPTR(&client_key_pem));
         client_filename = INLINE_FILE_TAG;
         client_inline = (const char *)BPTR(&client_key_pem);
     }
tests/unit_tests/openvpn/test_tls_crypt.c +4 -2
index 366b48d5..f4b1f860 100644
@@ -530,7 +530,8 @@ test_tls_crypt_v2_write_server_key_file(void **state) {
     const char *filename = "testfilename.key";
 
     expect_string(__wrap_buffer_write_file, filename, filename);
-    expect_string(__wrap_buffer_write_file, pem, test_server_key);
+    expect_memory(__wrap_buffer_write_file, pem, test_server_key,
+                  strlen(test_server_key));
     will_return(__wrap_buffer_write_file, true);
 
     tls_crypt_v2_write_server_key_file(filename);
@@ -542,7 +543,8 @@ test_tls_crypt_v2_write_client_key_file(void **state) {
 
     /* Test writing the client key */
     expect_string(__wrap_buffer_write_file, filename, filename);
-    expect_string(__wrap_buffer_write_file, pem, test_client_key);
+    expect_memory(__wrap_buffer_write_file, pem, test_client_key,
+                  strlen(test_client_key));
     will_return(__wrap_buffer_write_file, true);
 
     /* Key generation re-reads the created file as a sanity check */
Signature · 1 line
2.26.0
#7 lore raw

Hi,

Quoted 9 lines · Arne Schwabe

Change crypto_pem_encode to not put a nul-terminated terminated string into the buffer. This was useful for printf but should not be written into the file.

Instead do not assume that the buffer is null terminated and print only the number of bytes in the buffer. Also fix a similar case in printing static key where the 0 byte was never added to the buffer

Quoted 6 lines

Patch V2: make pem_encode behave more like other similar functions in OpenVPN
and do not null terminate.

Patch V3: also make the mbed TLS variant of pem_decode behave like other
similar functions in OpeNVPN and accept a not null-terminated buffer.

Thanks, this looks better. Two more things:

Quoted 17 lines
src/openvpn/crypto_mbedtls.c +13 -0
@@ -273,9 +270,25 @@ crypto_pem_decode(const char *name, struct buffer *dst,
         return false;
     }
 
+    /* mbed TLS requires the src to be null-terminated */
+    struct gc_arena gc = gc_new();
+    struct buffer input;
+
+    if (*(BLAST(src)) == '\0')
+    {
+        input = *src;
+    }
+    else
+    {
+        /* allocate a new buffer to avoid modifying the src buffer */
+        input = alloc_buf_gc(BLEN(src) + 1, &gc);
+        buf_copy(&input, src);

src/openvpn/crypto_mbedtls.c crypto_pem_decode(const char *name, struct buffer *dst, · line 294

Quoted 10 lines
src/openvpn/crypto_mbedtls.c +4 -1
+        buf_null_terminate(&input);
+    }
+
     size_t use_len = 0;
     mbedtls_pem_context ctx = { 0 };
-    bool ret = mbed_ok(mbedtls_pem_read_buffer(&ctx, header, footer, BPTR(src),
+    bool ret = mbed_ok(mbedtls_pem_read_buffer(&ctx, header, footer, BPTR(&input),
                                                NULL, 0, &use_len));
     if (ret && !buf_write(dst, ctx.buf, ctx.buflen))
     {

Maybe a matter of taste, but why not always alloc the buffer? That simplifies the code, and performance nor memory footprint is relevant here. Just a suggestion. I'm also fine with keeping it like this.

Quoted 16 lines
tests/unit_tests/openvpn/test_tls_crypt.c +2 -1
index 366b48d5..f4b1f860 100644
@@ -530,7 +530,8 @@ test_tls_crypt_v2_write_server_key_file(void **state) {
     const char *filename = "testfilename.key";
 
     expect_string(__wrap_buffer_write_file, filename, filename);
-    expect_string(__wrap_buffer_write_file, pem, test_server_key);
+    expect_memory(__wrap_buffer_write_file, pem, test_server_key,
+                  strlen(test_server_key));
     will_return(__wrap_buffer_write_file, true);
 
     tls_crypt_v2_write_server_key_file(filename);

tests/unit_tests/openvpn/test_tls_crypt.c test_tls_crypt_v2_write_client_key_file(void **state) { · line 550

Quoted 9 lines
tests/unit_tests/openvpn/test_tls_crypt.c +2 -1
@@ -542,7 +543,8 @@ test_tls_crypt_v2_write_client_key_file(void **state) {
 
     /* Test writing the client key */
     expect_string(__wrap_buffer_write_file, filename, filename);
-    expect_string(__wrap_buffer_write_file, pem, test_client_key);
+    expect_memory(__wrap_buffer_write_file, pem, test_client_key,
+                  strlen(test_client_key));
     will_return(__wrap_buffer_write_file, true);
 
     /* Key generation re-reads the created file as a sanity check */

ASAN tells me you're missing another one:

==26863==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6180000023d6 at pc 0x0000004310c8 bp 0x7fffbae53b90 sp 0x7fffbae53338 READ of size 847 at 0x6180000023d6 thread T0

    #0 0x4310c7 in strcmp
(tests/unit_tests/openvpn/tls_crypt_testdriver+0x4310c7)
    #1 0x7f9234541c6e  (/lib/x86_64-linux-gnu/libcmocka.so.0+0x2c6e)
    #2 0x7f92345446f9 in _check_expected
(/lib/x86_64-linux-gnu/libcmocka.so.0+0x56f9)
    #3 0x4cd414 in __wrap_buffer_write_file
tests/unit_tests/openvpn/../../.././../openvpn/tests/unit_tests/openvpn/test_tls_crypt.c:108:5
    #4 0x4cc23b in tls_crypt_v2_write_client_key_file
tests/unit_tests/openvpn/../../.././../openvpn/src/openvpn/tls_crypt.c:709:15
    #5 0x4d0f15 in test_tls_crypt_v2_write_client_key_file_metadata
tests/unit_tests/openvpn/../../.././../openvpn/tests/unit_tests/openvpn/test_tls_crypt.c:592:5
    #6 0x7f92345444e0  (/lib/x86_64-linux-gnu/libcmocka.so.0+0x54e0)
    #7 0x7f9234544ec4 in _cmocka_run_group_tests
(/lib/x86_64-linux-gnu/libcmocka.so.0+0x5ec4)
    #8 0x4cd75a in main
tests/unit_tests/openvpn/../../.././../openvpn/tests/unit_tests/openvpn/test_tls_crypt.c:645:15
    #9 0x7f92341fd0b2 in __libc_start_main
/build/glibc-YYA7BZ/glibc-2.31/csu/../csu/libc-start.c:308:16
    #10 0x41d92d in _start

(tests/unit_tests/openvpn/tls_crypt_testdriver+0x41d92d)

-Steffan

#8 lore raw

PATCH v4 1/1 Do not write extra 0 byte for --gen-key with auth-token/tls-crypt-v2

Change crypto_pem_encode to not put a nul-terminated terminated string into the buffer. This was useful for printf but should not be written into the file.

Instead do not assume that the buffer is null terminated and print only the number of bytes in the buffer. Also fix a similar case in printing static key where the 0 byte was never added to the buffer

Patch V2: make pem_encode behave more like other similar functions in OpenVPN
and do not null terminate.

Patch V3: also make the mbed TLS variant of pem_decode behave like other
similar functions in OpeNVPN and accept a not null-terminated buffer.

Patch V4: The newly introduced unit test
test_tls_crypt_v2_write_client_key_file_metadata was added after the V3 version of the patch and now misses the strlen with memcmp replacment that were added to test_tls_crypt_v2_write_client_key_file. Also add the modifictions to this function.

Unconditionally allocate buffer in mbed TLS path as requested by Steffan.

---
 src/openvpn/crypto.c                      |  4 ++--
 src/openvpn/crypto_mbedtls.c              | 19 ++++++++++++-------
 src/openvpn/crypto_openssl.c              |  3 +--
 src/openvpn/tls_crypt.c                   |  2 +-
 tests/unit_tests/openvpn/test_tls_crypt.c |  9 ++++++---
 5 files changed, 22 insertions(+), 15 deletions(-)
src/openvpn/crypto.c +2 -2
index 1678cba8..b05262e1 100644
@@ -1478,7 +1478,7 @@ write_key_file(const int nkeys, const char *filename)
     /* write key file to stdout if no filename given */
     if (!filename || strcmp(filename, "")==0)
     {
-        printf("%s\n", BPTR(&out));
+        printf("%.*s\n", BLEN(&out), BPTR(&out));
     }
     /* write key file, now formatted in out, to file */
     else if (!buffer_write_file(filename, &out))
@@ -1887,7 +1887,7 @@ write_pem_key_file(const char *filename, const char *pem_name)
 
     if (!filename || strcmp(filename, "")==0)
     {
-        printf("%s\n", BPTR(&server_key_pem));
+        printf("%.*s", BLEN(&server_key_pem), BPTR(&server_key_pem));
     }
     else if (!buffer_write_file(filename, &server_key_pem))
     {
src/openvpn/crypto_mbedtls.c +12 -7
index 3e77fa9e..b6458abf 100644
@@ -239,10 +239,12 @@ crypto_pem_encode(const char *name, struct buffer *dst,
         return false;
     }
 
+    /* We set the size buf to out_len-1 to NOT include the 0 byte that
+     * mbedtls_pem_write_buffer in its length calculation */
     *dst = alloc_buf_gc(out_len, gc);
     if (!mbed_ok(mbedtls_pem_write_buffer(header, footer, BPTR(src), BLEN(src),
                                           BPTR(dst), BCAP(dst), &out_len))
-        || !buf_inc_len(dst, out_len))
+        || !buf_inc_len(dst, out_len-1))
     {
         CLEAR(*dst);
         return false;
@@ -259,11 +261,6 @@ crypto_pem_decode(const char *name, struct buffer *dst,
     char header[1000+1] = { 0 };
     char footer[1000+1] = { 0 };
 
-    if (*(BLAST(src)) != '\0')
-    {
-        msg(M_WARN, "PEM decode error: source buffer not null-terminated");
-        return false;
-    }
     if (!openvpn_snprintf(header, sizeof(header), "-----BEGIN %s-----", name))
     {
         return false;
@@ -273,9 +270,16 @@ crypto_pem_decode(const char *name, struct buffer *dst,
         return false;
     }
 
+    /* mbed TLS requires the src to be null-terminated */
+    /* allocate a new buffer to avoid modifying the src buffer */
+    struct gc_arena gc = gc_new();
+    struct buffer input = alloc_buf_gc(BLEN(src) + 1, &gc);
+    buf_copy(&input, src);
+    buf_null_terminate(&input);
+
     size_t use_len = 0;
     mbedtls_pem_context ctx = { 0 };
-    bool ret = mbed_ok(mbedtls_pem_read_buffer(&ctx, header, footer, BPTR(src),
+    bool ret = mbed_ok(mbedtls_pem_read_buffer(&ctx, header, footer, BPTR(&input),
                                                NULL, 0, &use_len));
     if (ret && !buf_write(dst, ctx.buf, ctx.buflen))
     {
@@ -284,6 +288,7 @@ crypto_pem_decode(const char *name, struct buffer *dst,
     }
 
     mbedtls_pem_free(&ctx);
+    gc_free(&gc);
     return ret;
 }
 
src/openvpn/crypto_openssl.c +1 -2
index a81dcfd8..4fa65ba8 100644
@@ -400,9 +400,8 @@ crypto_pem_encode(const char *name, struct buffer *dst,
     BUF_MEM *bptr;
     BIO_get_mem_ptr(bio, &bptr);
 
-    *dst = alloc_buf_gc(bptr->length + 1, gc);
+    *dst = alloc_buf_gc(bptr->length, gc);
     ASSERT(buf_write(dst, bptr->data, bptr->length));
-    buf_null_terminate(dst);
 
     ret = true;
 cleanup:
src/openvpn/tls_crypt.c +1 -1
index e9f9cc2a..3018c18e 100644
@@ -702,7 +702,7 @@ tls_crypt_v2_write_client_key_file(const char *filename,
 
     if (!filename || streq(filename, ""))
     {
-        printf("%s\n", BPTR(&client_key_pem));
+        printf("%.*s\n", BLEN(&client_key_pem), BPTR(&client_key_pem));
         client_filename = INLINE_FILE_TAG;
         client_inline = (const char *)BPTR(&client_key_pem);
     }
tests/unit_tests/openvpn/test_tls_crypt.c +6 -3
index 8406d89d..28bebb6e 100644
@@ -548,7 +548,8 @@ test_tls_crypt_v2_write_server_key_file(void **state)
     const char *filename = "testfilename.key";
 
     expect_string(__wrap_buffer_write_file, filename, filename);
-    expect_string(__wrap_buffer_write_file, pem, test_server_key);
+    expect_memory(__wrap_buffer_write_file, pem, test_server_key,
+                  strlen(test_server_key));
     will_return(__wrap_buffer_write_file, true);
 
     tls_crypt_v2_write_server_key_file(filename);
@@ -561,7 +562,8 @@ test_tls_crypt_v2_write_client_key_file(void **state)
 
     /* Test writing the client key */
     expect_string(__wrap_buffer_write_file, filename, filename);
-    expect_string(__wrap_buffer_write_file, pem, test_client_key);
+    expect_memory(__wrap_buffer_write_file, pem, test_client_key,
+                  strlen(test_client_key));
     will_return(__wrap_buffer_write_file, true);
 
     /* Key generation re-reads the created file as a sanity check */
@@ -580,7 +582,8 @@ test_tls_crypt_v2_write_client_key_file_metadata(void **state)
 
     /* Test writing the client key */
     expect_string(__wrap_buffer_write_file, filename, filename);
-    expect_string(__wrap_buffer_write_file, pem, test_client_key_metadata);
+    expect_memory(__wrap_buffer_write_file, pem, test_client_key_metadata,
+                strlen(test_client_key_metadata));
     will_return(__wrap_buffer_write_file, true);
 
     /* Key generation re-reads the created file as a sanity check */
Signature · 1 line
2.26.0
#9 lore raw

Your patch has been applied to the master branch.

Lightly tested on my side ("make check" with OpenSSL and mbedTLS builds).

commit 14a57be4609fc23e4775072948bf722f21f25099

Author: Arne Schwabe
Date:   Thu May 7 15:25:34 2020 +0200
     Do not write extra 0 byte for --gen-key with auth-token/tls-crypt-v2

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

--
kind regards,

Gert Doering