From: "cron2 (Code Review)" Date: Wed, 15 May 2024 11:40:44 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <8187b55abc93f2365399385210fc5e02af2b197c-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="gET+2Vh01TE="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: Remove custom TLS 1.0 PRF implementation only used by LibreSSL/wolfSSL List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , flichtenheld Cc: openvpn-devel --gET+2Vh01TE= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has uploaded a new patch set (#6) to the change originally created by= plaisthos. ( http://gerrit.openvpn.net/c/openvpn/+/560?usp=3Demail ) The following approvals got outdated and were removed: Code-Review+2 by cron2 Change subject: Remove custom TLS 1.0 PRF implementation only used by Libre= SSL/wolfSSL ...................................................................... Remove custom TLS 1.0 PRF implementation only used by LibreSSL/wolfSSL After the removal of the OpenSSL 1.0.2 support, LibreSSL/wolfSSL are the only libraries that still needs the custom implementation. Since our LibreSSL/wolfSSL support is always best effort, we can afford to limit LibreSSL support in this way. If they want to support this, they should expose the functionality as well. Change-Id: I5bfa3630ad4dff2807705658bc877c4a429a39ce Signed-off-by: Arne Schwabe Acked-by: Gert Doering Message-Id: <20240515100115.11056-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg28= 672.html Signed-off-by: Gert Doering --- M src/openvpn/crypto_openssl.c M tests/unit_tests/openvpn/test_crypto.c 2 files changed, 16 insertions(+), 180 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/60/560/6 diff --git a/src/openvpn/crypto_openssl.c b/src/openvpn/crypto_openssl.c index 0473fad..fbd38f3 100644 --- a/src/openvpn/crypto_openssl.c +++ b/src/openvpn/crypto_openssl.c @@ -1397,7 +1397,7 @@ return ret; } -#elif !defined(LIBRESSL_VERSION_NUMBER) +#elif !defined(LIBRESSL_VERSION_NUMBER) && !defined(ENABLE_CRYPTO_WOLFSSL) bool ssl_tls1_PRF(const uint8_t *seed, int seed_len, const uint8_t *secret, int secret_len, uint8_t *output, int output_len) @@ -1444,183 +1444,14 @@ return ret; } #else /* if defined(LIBRESSL_VERSION_NUMBER) */ -/* - * Generate the hash required by for the \c tls1_PRF function. - * - * We cannot use our normal hmac_* function as they do not work - * in a FIPS environment (no MD5 allowed, which we need). Instead - * we need to directly use the EVP_MD_* API with the special - * EVP_MD_CTX_FLAG_NON_FIPS_ALLOW flag. - * - * The function below is adapted from OpenSSL 1.0.2t - * - * @param md_kt Message digest to use - * @param sec Secret to base the hash on - * @param sec_len Length of the secret - * @param seed Seed to hash - * @param seed_len Length of the seed - * @param out Output buffer - * @param olen Length of the output buffer - */ -static -bool -tls1_P_hash(const EVP_MD *md, const unsigned char *sec, - int sec_len, const void *seed, int seed_len, - unsigned char *out, int olen) -{ - int chunk; - size_t j; - EVP_MD_CTX *ctx, *ctx_tmp, *ctx_init; - EVP_PKEY *mac_key; - unsigned char A1[EVP_MAX_MD_SIZE]; - size_t A1_len =3D EVP_MAX_MD_SIZE; - int ret =3D false; - - chunk =3D EVP_MD_size(md); - OPENSSL_assert(chunk >=3D 0); - - ctx =3D md_ctx_new(); - ctx_tmp =3D md_ctx_new(); - ctx_init =3D md_ctx_new(); - EVP_MD_CTX_set_flags(ctx_init, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW); - mac_key =3D EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, sec, sec_len); - if (!mac_key) - { - goto err; - } - if (!EVP_DigestSignInit(ctx_init, NULL, md, NULL, mac_key)) - { - goto err; - } - if (!EVP_MD_CTX_copy_ex(ctx, ctx_init)) - { - goto err; - } - if (!EVP_DigestSignUpdate(ctx, seed, seed_len)) - { - goto err; - } - if (!EVP_DigestSignFinal(ctx, A1, &A1_len)) - { - goto err; - } - - for (;; ) - { - /* Reinit mac contexts */ - if (!EVP_MD_CTX_copy_ex(ctx, ctx_init)) - { - goto err; - } - if (!EVP_DigestSignUpdate(ctx, A1, A1_len)) - { - goto err; - } - if (olen > chunk && !EVP_MD_CTX_copy_ex(ctx_tmp, ctx)) - { - goto err; - } - if (!EVP_DigestSignUpdate(ctx, seed, seed_len)) - { - goto err; - } - - if (olen > chunk) - { - j =3D olen; - if (!EVP_DigestSignFinal(ctx, out, &j)) - { - goto err; - } - out +=3D j; - olen -=3D j; - /* calc the next A1 value */ - if (!EVP_DigestSignFinal(ctx_tmp, A1, &A1_len)) - { - goto err; - } - } - else - { - A1_len =3D EVP_MAX_MD_SIZE; - /* last one */ - if (!EVP_DigestSignFinal(ctx, A1, &A1_len)) - { - goto err; - } - memcpy(out, A1, olen); - break; - } - } - ret =3D true; -err: - EVP_PKEY_free(mac_key); - EVP_MD_CTX_free(ctx); - EVP_MD_CTX_free(ctx_tmp); - EVP_MD_CTX_free(ctx_init); - OPENSSL_cleanse(A1, sizeof(A1)); - return ret; -} - -/* - * Use the TLS PRF function for generating data channel keys. - * This code is based on the OpenSSL library. - * - * TLS generates keys as such: - * - * master_secret[48] =3D PRF(pre_master_secret[48], "master secret", - * ClientHello.random[32] + ServerHello.random[32]) - * - * key_block[] =3D PRF(SecurityParameters.master_secret[48], - * "key expansion", - * SecurityParameters.server_random[32] + - * SecurityParameters.client_random[32]); - * - * Notes: - * - * (1) key_block contains a full set of 4 keys. - * (2) The pre-master secret is generated by the client. - */ +/* LibreSSL and wolfSSL do not expose a TLS 1.0/1.1 PRF via the same APIs = as + * OpenSSL does. As result they will only be able to support + * peers that support TLS EKM like when running with OpenSSL 3.x FIPS */ bool ssl_tls1_PRF(const uint8_t *label, int label_len, const uint8_t *sec, int slen, uint8_t *out1, int olen) { - bool ret =3D true; - struct gc_arena gc =3D gc_new(); - /* For some reason our md_get("MD5") fails otherwise in the unit test = */ - const EVP_MD *md5 =3D EVP_md5(); - const EVP_MD *sha1 =3D EVP_sha1(); - - uint8_t *out2 =3D (uint8_t *)gc_malloc(olen, false, &gc); - - int len =3D slen/2; - const uint8_t *S1 =3D sec; - const uint8_t *S2 =3D &(sec[len]); - len +=3D (slen&1); /* add for odd, make longer */ - - if (!tls1_P_hash(md5, S1, len, label, label_len, out1, olen)) - { - ret =3D false; - goto done; - } - - if (!tls1_P_hash(sha1, S2, len, label, label_len, out2, olen)) - { - ret =3D false; - goto done; - } - - for (int i =3D 0; i < olen; i++) - { - out1[i] ^=3D out2[i]; - } - - secure_memzero(out2, olen); - - dmsg(D_SHOW_KEY_SOURCE, "tls1_PRF out[%d]: %s", olen, format_hex(out1,= olen, 0, &gc)); -done: - gc_free(&gc); - return ret; + return false; } #endif /* if LIBRESSL_VERSION_NUMBER */ #endif /* ENABLE_CRYPTO_OPENSSL */ diff --git a/tests/unit_tests/openvpn/test_crypto.c b/tests/unit_tests/open= vpn/test_crypto.c index 01c16c9..9d3ea1a 100644 --- a/tests/unit_tests/openvpn/test_crypto.c +++ b/tests/unit_tests/openvpn/test_crypto.c @@ -137,11 +137,6 @@ } -static uint8_t good_prf[32] =3D {0xd9, 0x8c, 0x85, 0x18, 0xc8, 0x5e, 0x94,= 0x69, - 0x27, 0x91, 0x6a, 0xcf, 0xc2, 0xd5, 0x92, 0= xfb, - 0xb1, 0x56, 0x7e, 0x4b, 0x4b, 0x14, 0x59, 0= xe6, - 0xa9, 0x04, 0xac, 0x2d, 0xda, 0xb7, 0x2d, 0= x67}; - static const char *ipsumlorem =3D "Lorem ipsum dolor sit amet, consectetur= " "adipisici elit, sed eiusmod tempor incidu= nt " "ut labore et dolore magna aliqua."; @@ -160,9 +155,19 @@ uint8_t out[32]; - ssl_tls1_PRF(seed, seed_len, secret, secret_len, out, sizeof(out)); + bool ret =3D ssl_tls1_PRF(seed, seed_len, secret, secret_len, out, siz= eof(out)); +#if defined(LIBRESSL_VERSION_NUMBER) || defined(ENABLE_CRYPTO_WOLFSSL) + /* No TLS1 PRF support in these libraries */ + assert_false(ret); +#else + assert_true(ret); + uint8_t good_prf[32] =3D {0xd9, 0x8c, 0x85, 0x18, 0xc8, 0x5e, 0x94, 0x= 69, + 0x27, 0x91, 0x6a, 0xcf, 0xc2, 0xd5, 0x92, 0xfb, + 0xb1, 0x56, 0x7e, 0x4b, 0x4b, 0x14, 0x59, 0xe6, + 0xa9, 0x04, 0xac, 0x2d, 0xda, 0xb7, 0x2d, 0x67= }; assert_memory_equal(good_prf, out, sizeof(out)); +#endif } static uint8_t testkey[20] =3D {0x0b, 0x00}; -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/560?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I5bfa3630ad4dff2807705658bc877c4a429a39ce Gerrit-Change-Number: 560 Gerrit-PatchSet: 6 Gerrit-Owner: plaisthos Gerrit-Reviewer: cron2 Gerrit-Reviewer: flichtenheld Gerrit-CC: openvpn-devel Gerrit-MessageType: newpatchset --gET+2Vh01TE= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 uploaded patch set #6 to the change originally cr= eated by plaisthos.

View Change

The following approvals got outdated= and were removed: Code-Review+2 by cron2

Remove custom TLS 1.0 PRF implementation only used b=
y LibreSSL/wolfSSL

After the removal of the OpenSSL 1.0.2 support, L= ibreSSL/wolfSSL are the
only libraries that still needs the custom imple= mentation.

Since our LibreSSL/wolfSSL support is always best effort,= we can afford to
limit LibreSSL support in this way. If they want to su= pport this, they
should expose the functionality as well.

Change-= Id: I5bfa3630ad4dff2807705658bc877c4a429a39ce
Signed-off-by: Arne Schwab= e <arne@...1227...>
Acked-by: Gert Doering <gert@...1296...>=
Message-Id: <20240515100115.11056-1-gert@...1296...>
URL: http= s://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg28672.html<= br>Signed-off-by: Gert Doering <gert@...1296...>
---
M src/open= vpn/crypto_openssl.c
M tests/unit_tests/openvpn/test_crypto.c
2 files= changed, 16 insertions(+), 180 deletions(-)

git pull ssh://gerri=
t.openvpn.net:29418/openvpn refs/changes/60/560/6
diff --git a/src/=
openvpn/crypto_openssl.c b/src/openvpn/crypto_openssl.c
ind= ex 0473fad..fbd38f3 100644
--- a/src/openvpn/crypto_openssl= .c
+++ b/src/openvpn/crypto_openssl.c
@@ -1= 397,7 +1397,7 @@

return ret;<= br> }
-#elif !def= ined(LIBRESSL_VERSION_NUMBER)
+#elif !defined(LIBRESSL_VERSION_NUMBER) && !defined(ENABL= E_CRYPTO_WOLFSSL)
bool
ssl_tls1_PRF(const= uint8_t *seed, int seed_len, const uint8_t *secret,
= int secret_len, uint8_t *output, int output_len)
@@= -1444,183 +1444,14 @@
return ret;
}<= /span>
#else /* if defined(LIBRESSL_VERSION_NUMBER) */-/*
- * Generate the hash required by for the \c tls1_P= RF function.
- *=
- * We cannot use our normal = hmac_* function as they do not work
- * in a FIPS environment (no MD5 allowed, which we need). Ins= tead
- * we need to dir= ectly use the EVP_MD_* API with the special
- * EVP_MD_CTX_FLAG_NON_FIPS_ALLOW flag.
- *

- * The function below is adapted from OpenSSL 1.0.2t
- *
- * @param md_kt Message digest to = use
- * @param sec = Secret to base the hash on
- * @param sec_len Length of the secret
- * @param seed Seed to hash
- * @param seed_len Le= ngth of the seed
- * @p= aram out Output buffer
- * @param olen Length of the output buffer
- */

-static
-bool
-tls1_P_ha= sh(const EVP_MD *md, const unsigned char *sec,
- int sec_len, const void *seed, int see= d_len,
- uns= igned char *out, int olen)
-{
- int chunk;<= /span>
- size_t j;- EVP_MD_CTX *ctx, *ctx_tmp,= *ctx_init;
- EVP_PK= EY *mac_key;
- unsig= ned char A1[EVP_MAX_MD_SIZE];
- size_t A1_len =3D EVP_MAX_MD_SIZE;
- int ret =3D false;
-
- chunk =3D EVP_MD_size(md);
- OPENSSL_assert(chunk >=3D 0);
-
- ctx =3D md_ctx_new();
- ctx_tmp =3D md_ctx_new();
- ctx_init =3D md_ctx_new();
- EVP_MD_CTX_set_flags(ctx_init, EVP_MD_CTX_= FLAG_NON_FIPS_ALLOW);
-= mac_key =3D EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, sec, sec_len);
- if (!mac_key)=
- {
- goto err;
- }
- if (!EVP_DigestSignInit(ctx_init, NULL, md, NULL, mac_key))=
- {
- goto err;

- }
- if (!EVP_MD_CTX_copy_ex(ctx, ctx_init))
- {

- goto err;
- }
- if (!EVP_DigestSignUpdate(ctx, seed, seed_len))
- {
- goto err;
- }
- = if (!EVP_DigestSignFinal(ctx, A1, &A1_len))
- {
- goto err;
- }
-<= br>- for (;; )
- {

- /* Reinit mac contexts */
- if (!EVP_MD_CTX_copy_ex(ctx, ctx_= init))
- {
- goto err;
- }
- if (!EVP_DigestSignUpdate(ctx, = A1, A1_len))
- {=
- goto err;=
- }
<= span style=3D"color: hsl(0, 100%, 40%);">- if (olen > chunk &= & !EVP_MD_CTX_copy_ex(ctx_tmp, ctx))

- {
- goto err;
- }
- = if (!EVP_DigestSignUpdate(ctx, seed, seed_len))
- {
- goto err;
- }
-
- if (olen= > chunk)
- {=
- j =3D ole= n;
- if (!EV= P_DigestSignFinal(ctx, out, &j))
- {
- goto err;
- }
- olen -=3D j;
- /* calc the next A1 value */
- if (!EVP_DigestSignFinal(ctx_tmp, A1= , &A1_len))
- = {
- = goto err;
- = }
- }- else
- {
- A1_len =3D EVP_MAX_MD_SIZE;
<= span style=3D"color: hsl(0, 100%, 40%);">- /* last one */
=
- if (!EVP_DigestS= ignFinal(ctx, A1, &A1_len))
- {
- goto err;
- }
- = memcpy(out, A1, olen);
- break;
- }
- = }
- ret =3D true;
-err:
- EVP_PKEY_free(mac_key);
- EVP_MD_CTX_free(ctx);
- EVP_MD_CTX_free(ctx_tmp);
- EVP_MD_CTX_free(ctx= _init);
- OPENSSL_cl= eanse(A1, sizeof(A1));
= - return ret;
-}
-
-/*
- * Use the TLS PRF function for generating data channel keys.
- * This code is based on = the OpenSSL library.
- = *
- * TLS generates key= s as such:
- *- * master_secret[48] =3D PRF(p= re_master_secret[48], "master secret",
- * ClientHello.random[32= ] + ServerHello.random[32])
- *
- * key_block[= ] =3D PRF(SecurityParameters.master_secret[48],
- * "key expansion",
- * Securi= tyParameters.server_random[32] +
- * SecurityParameters.client_random[32]);=
- *
- * Notes:
- *
- * = (1) key_block contains a full set of 4 keys.
- * (2) The pre-master secret is generated by the cli= ent.
- */
+/* LibreSSL and wolfSSL do not ex= pose a TLS 1.0/1.1 PRF via the same APIs as

+ * OpenSSL does. As result they will only be able t= o support
+ * peers t= hat support TLS EKM like when running with OpenSSL 3.x FIPS */
bool

ssl_tls1_PRF(const uint8_t *label, int label_len= , const uint8_t *sec,
int slen, uint8_t *out1= , int olen)
{
- bool ret =3D true;
- struct gc_arena gc =3D gc_new();
- /* For some reason our md_get("MD5")= fails otherwise in the unit test */
- const EVP_MD *md5 =3D EVP_md5();
- const EVP_MD *sha1 =3D EVP_sha1();
-
- uint8_t *out2 =3D (uint8_t *)gc_malloc(olen= , false, &gc);
-
- int len =3D slen/2;<= /span>
- const uint8_t *S1 = =3D sec;
- const uin= t8_t *S2 =3D &(sec[len]);
- len +=3D (slen&1); /* add for odd, make longer */-
- if (!tls1_P_hash(md5, S1, len, label, label_len,= out1, olen))
- {
- ret =3D false;
- goto done;
- }
-
- if (!tls1_P_hash(sha1, S2, len, label, label_len, out2, ole= n))
- {
- ret =3D false;

<= span style=3D"color: hsl(0, 100%, 40%);">- goto done;

- }

-
- for (int i =3D 0; i < olen; i++)
- {
- out1[i] ^=3D out2[i];
- }
-
- secure_memzero(out2, = olen);
-
- dmsg(D_SHOW_KEY_SOURCE, "tl= s1_PRF out[%d]: %s", olen, format_hex(out1, olen, 0, &gc));
=
-done:
- gc_free(&gc);
- return ret;
+ return false;
}
= #endif /* if LIBRESSL_VERSION_NUMBER */
#endif /* E= NABLE_CRYPTO_OPENSSL */
diff --git a/tests/unit_tests/openv= pn/test_crypto.c b/tests/unit_tests/openvpn/test_crypto.c
i= ndex 01c16c9..9d3ea1a 100644
--- a/tests/unit_tests/openvpn= /test_crypto.c
+++ b/tests/unit_tests/openvpn/test_crypto.c=
@@ -137,11 +137,6 @@
}
<= /span>

-stat= ic uint8_t good_prf[32] =3D {0xd9, 0x8c, 0x85, 0x18, 0xc8, 0x5e, 0x94, 0x69= ,
- = 0x27, 0x91, 0x6a, 0xcf, 0xc2, 0xd5, 0x92, 0xfb,
- 0xb1, 0= x56, 0x7e, 0x4b, 0x4b, 0x14, 0x59, 0xe6,
- 0xa9, 0x04, 0xac, 0x2d, 0= xda, 0xb7, 0x2d, 0x67};
-
static const char *ipsumlorem =3D "Lorem ipsum dol= or sit amet, consectetur "
= "adipisici elit, sed eiusmod tempor incidunt "
"ut labore et dolore magna aliqua= .";

@@ -160,9 +155,19 @@

<= span>

uint8_t out[32];
- ssl_tls1_PRF(seed, seed_len, secret, secret_len,= out, sizeof(out));
+= bool ret =3D ssl_tls1_PRF(seed, seed_len, secret, secret_len, out, size= of(out));

+#if defined(LIBRESSL_VERSION_NUMBER) || defined(ENABLE_CRYPTO_WOLFS= SL)
+ /* No TLS1 P= RF support in these libraries */
+ assert_false(ret);
+#else
+ assert_true(ret);
+ uint8_t good_prf[32] =3D {0xd9, 0x8c, 0x85, 0x18, 0xc8, 0x5e, 0x94,= 0x69,
+ = 0x27, 0x91, 0x6a, 0xcf, 0xc2, 0xd5, 0x92, 0xfb,
+ 0xb1= , 0x56, 0x7e, 0x4b, 0x4b, 0x14, 0x59, 0xe6,

+ 0xa9, 0x04, 0xac, 0x2d,= 0xda, 0xb7, 0x2d, 0x67};
assert_memory_equal(good_prf= , out, sizeof(out));
= +#endif
}

static uint8_= t testkey[20] =3D {0x0b, 0x00};

To view= , visit c= hange 560. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I5bfa3630ad4dff2807705658bc8= 77c4a429a39ce
Gerrit-Change-Number: 560
Gerrit-PatchSet: 6
Gerrit-Owner: plaisthos <arne-openvpn@...1711...= 27...>
Gerrit-Reviewer: cron2 <gert@...1296...>=
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-MessageType: newpatchset
--gET+2Vh01TE=--