From: "plaisthos (Code Review)" Date: Thu, 28 Nov 2024 18:54:57 +0000 In-Reply-To: References: Reply-To: arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641..., gert@...1296... Message-ID: <25d998b172b67d59bf2ddd4e64731d73dd88d2e9-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="9q1uDxzSmto="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: Trigger renegotiation of data key if getting close to the AEAD usage ... List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: flichtenheld Cc: cron2 , openvpn-devel --9q1uDxzSmto= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: cron2, flichtenheld. Hello flichtenheld, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/796?usp=3Demail to look at the new patch set (#7). Change subject: Trigger renegotiation of data key if getting close to the A= EAD usage limit ...................................................................... Trigger renegotiation of data key if getting close to the AEAD usage limit This implements the limitation of AEAD key usage[1] with a confidentiality margin of 2^-57, the same as TLS 1.3. In this implementation, unlike TLS 1.3 that counts the number of records, we count the actual number of packets and plaintext blocks. TLS 1.3 can reasonable assume that for large data transfers, full records are used and therefore the maximum record size of 2**14 (2*10 blocks) is used to calculate the number of records before a new key needs to be used. For a VPN like OpenVPN, the same calculation would either require using a pessimistic assumption of using a MTU size of 65k which limits us to 2^24 packets, which equals only 24 GB with more common MTU/MSS of 1400 or requiring a dynamic calculation which includes the actual MTU that we allow to send. For 1500 the calculation yields 2*29.4 which is a quite significant higher number of packets (923 GB at 1400 MSS/MTU). To avoid this dynamic calculation and also avoid needing to know the MSS/MTU size in the crypto layer, this implementation foregoes the simplification of counting just packets but will count blocks and packets instead and determines the limit from that. This also has the side effect that connections with a lot of small packets (like TCP ACKs) mixed with large packets will be able to keep using the same key much longer until requiring a renegotiation. This patch will set the limit where to trigger the renegotiation at 7/8 of the recommended maximum value. [1] https://www.ietf.org/archive/id/draft-irtf-cfrg-aead-limits-08.html Testing instructions: The easiest way to test if this patch works as intended is to manually change the return value of cipher_get_aead_limits to some silly low value like 2048. After a bit of VPN traffic, a soft reset should occur that indicates being over the TLS: soft reset sec=3D41/3600 bytes=3D59720/-1 pkts=3D78/0 aead_limit_s= end=3D1883/1792 aead_limit_recv=3D1937/1792 Here the send limit is over the limit (1792 =3D 2048 * 8/7). Change-Id: I057f007577f10c6ac917ee4620ee3d2559187dc7 Signed-off-by: Arne Schwabe --- M Changes.rst M src/openvpn/crypto.c M src/openvpn/crypto.h M src/openvpn/ssl.c M src/openvpn/ssl_common.h M tests/unit_tests/openvpn/test_crypto.c 6 files changed, 159 insertions(+), 3 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/96/796/7 diff --git a/Changes.rst b/Changes.rst index a6cc3be..34542a5 100644 --- a/Changes.rst +++ b/Changes.rst @@ -22,6 +22,14 @@ For more details see [lwipovpn on Gihtub](https://github.com/OpenVPN/l= wipovpn). +Enforcement of AES-GCM usage limit + OpenVPN will now enforce the usage limits on AES-GCM with the same + confidentiality margin as TLS 1.3 does. This mean that renegotiation w= ill + be triggered after roughly 2^28 to 2^31 packets depending of the packet + size. More details about usage limit of AES-GCM can be found here: + + https://datatracker.ietf.org/doc/draft-irtf-cfrg-aead-limits/ + Deprecated features ------------------- ``secret`` support has been removed by default. diff --git a/src/openvpn/crypto.c b/src/openvpn/crypto.c index 8f34eaa..000a9a7 100644 --- a/src/openvpn/crypto.c +++ b/src/openvpn/crypto.c @@ -134,6 +134,11 @@ ASSERT(cipher_ctx_update(ctx->cipher, BEND(&work), &outlen, BPTR(buf),= BLEN(buf))); ASSERT(buf_inc_len(&work, outlen)); + /* update number of plaintext blocks encrypted. Use the (x + (n-1))/n = trick + * to round up the result to the number of blocks used */ + const int blocksize =3D AEAD_LIMIT_BLOCKSIZE; + opt->key_ctx_bi.encrypt.plaintext_blocks +=3D (outlen + (blocksize - 1= ))/blocksize; + /* Flush the encryption buffer */ ASSERT(cipher_ctx_final(ctx->cipher, BEND(&work), &outlen)); ASSERT(buf_inc_len(&work, outlen)); @@ -321,6 +326,38 @@ } } +int64_t +cipher_get_aead_limits(const char *ciphername) +{ + if (!cipher_kt_mode_aead(ciphername)) + { + return 0; + } + + if (cipher_kt_name(ciphername) =3D=3D cipher_kt_name("CHACHA20-POLY130= 5")) + { + return 0; + } + + /* Assume all other ciphers require the limit */ + + /* We focus here on the equation + * + * q + s <=3D p^(1/2) * 2^(129/2) - 1 + * q <=3D (p^(1/2) * 2^(129/2) - 1) / (L + 1) + * + * as is the one that is limiting us. + * + * With p =3D 2^-57 this becomes + * + * q + s <=3D (2^36 - 1) + * + */ + int64_t rs =3D (1ull << 36) - 1; + + return rs; +} + bool crypto_check_replay(struct crypto_options *opt, const struct packet_id_net *pin, const char *error_pre= fix, @@ -478,6 +515,12 @@ goto error_exit; } + + /* update number of plaintext blocks decrypted. Use the (x + (n-1))/n = trick + * to round up the result to the number of blocks used. */ + const int blocksize =3D AEAD_LIMIT_BLOCKSIZE; + opt->key_ctx_bi.decrypt.plaintext_blocks +=3D (outlen + (blocksize - 1= ))/blocksize; + *buf =3D work; gc_free(&gc); diff --git a/src/openvpn/crypto.h b/src/openvpn/crypto.h index 074dad6..052eacd 100644 --- a/src/openvpn/crypto.h +++ b/src/openvpn/crypto.h @@ -166,6 +166,9 @@ uint8_t implicit_iv[OPENVPN_MAX_IV_LENGTH]; /**< The implicit part of the IV */ size_t implicit_iv_len; /**< The length of implicit_iv */ + /** Counter for the number of plaintext encrypted using this cipher + * in number of 128 bit blocks (only used for AEAD ciphers) */ + uint64_t plaintext_blocks; }; #define KEY_DIRECTION_BIDIRECTIONAL 0 /* same keys for both directions */ @@ -596,6 +599,24 @@ } /** + * Check if the cipher is an AEAD cipher and needs to be limited to a cert= ain + * number of number of blocks + packets. Return -1 if ciphername is not an= AEAD + * cipher or no limit (e.g. Chacha20-Poly1305) is needed. + * + * For reference see the OpenVPN RFC draft and + * https://www.ietf.org/archive/id/draft-irtf-cfrg-aead-limits-08.html + */ +int64_t +cipher_get_aead_limits(const char *ciphername); + +/** + * Blocksize used for the AEAD limit caluclation + * + * Since cipher_ctx_block_size() is not reliable and will return 1 in many + * cases use a hardcoded blocksize instead */ +#define AEAD_LIMIT_BLOCKSIZE 16 + +/** * Checks if the current TLS library supports the TLS 1.0 PRF with MD5+SHA1 * that OpenVPN uses when TLS Keying Material Export is not available. * @@ -603,4 +624,20 @@ */ bool check_tls_prf_working(void); +/** + * Checks if the usage limit for an AEAD cipher is reached + * + * This method abstracts the calculation to make the calling function easi= er + * to read. + */ +static inline bool +aead_usage_limit_reached(const int64_t limit, const struct key_ctx *key_ct= x, + int64_t higest_pid) +{ + /* This is the q + s <=3D p^(1/2) * 2^(129/2) - 1 calculation where + * q is the number of protected messages (highest_pid) + * s Total plaintext length in all messages (in blocks) */ + return (limit > 0 && key_ctx->plaintext_blocks + higest_pid > limit); +} + #endif /* CRYPTO_H */ diff --git a/src/openvpn/ssl.c b/src/openvpn/ssl.c index 1f8eb1e..2acf985 100644 --- a/src/openvpn/ssl.c +++ b/src/openvpn/ssl.c @@ -131,6 +131,26 @@ } } +static int64_t +tls_get_limit_aead(const char *ciphername) +{ + int64_t limit =3D cipher_get_aead_limits(ciphername); + + if (limit =3D=3D 0) + { + return 0; + } + + /* set limit to 7/8 of the limit so the renogiation can succeed before + * we go over the limit */ + limit =3D limit * 7/8; + + msg(D_SHOW_KEYS, "Note: AEAD cipher %s will be limited to a sum of %" + PRIi64 " for block and packets before renegotiation", + ciphername, limit); + return limit; +} + void tls_init_control_channel_frame_parameters(struct frame *frame, int tls_mtu) { @@ -1576,6 +1596,8 @@ tls_limit_reneg_bytes(session->opt->key_type.cipher, &session->opt->renegotiate_bytes); + session->opt->aead_usage_limit =3D tls_get_limit_aead(session->opt->ke= y_type.cipher); + /* set the state of the keys for the session to generated */ ks->state =3D S_GENERATED_KEYS; @@ -2996,6 +3018,18 @@ return true; } + /* Check the AEAD usage limit of cleartext blocks + packets */ + const struct key_ctx_bi *key_ctx_bi =3D &ks->crypto_options.key_ctx_bi; + const int64_t usage_limit =3D session->opt->aead_usage_limit; + + if (aead_usage_limit_reached(usage_limit, &key_ctx_bi->encrypt, + ks->crypto_options.packet_id.send.id) + || aead_usage_limit_reached(usage_limit, &key_ctx_bi->decrypt, + ks->crypto_options.packet_id.rec.id)) + { + return true; + } + return false; } /* @@ -3028,10 +3062,17 @@ && should_trigger_renegotiation(session, ks)) { msg(D_TLS_DEBUG_LOW, "TLS: soft reset sec=3D%d/%d bytes=3D" counte= r_format - "/%" PRIi64 " pkts=3D" counter_format "/%" PRIi64, + "/%" PRIi64 " pkts=3D" counter_format "/%" PRIi64 + " aead_limit_send=3D%" PRIu64 "/%" PRIu64 + " aead_limit_recv=3D%" PRIu64 "/%" PRIu64, (int) (now - ks->established), session->opt->renegotiate_secon= ds, ks->n_bytes, session->opt->renegotiate_bytes, - ks->n_packets, session->opt->renegotiate_packets); + ks->n_packets, session->opt->renegotiate_packets, + ks->crypto_options.key_ctx_bi.encrypt.plaintext_blocks + ks->n= _packets, + session->opt->aead_usage_limit, + ks->crypto_options.key_ctx_bi.decrypt.plaintext_blocks + ks->n= _packets, + session->opt->aead_usage_limit + ); key_state_soft_reset(session); } diff --git a/src/openvpn/ssl_common.h b/src/openvpn/ssl_common.h index 5840e2d..ced5299 100644 --- a/src/openvpn/ssl_common.h +++ b/src/openvpn/ssl_common.h @@ -333,6 +333,9 @@ interval_t packet_timeout; int64_t renegotiate_bytes; int64_t renegotiate_packets; + /** limit for AEAD cipher, this is the sum of packets + blocks + * that are allowed to be used */ + int64_t aead_usage_limit; interval_t renegotiate_seconds; /* cert verification parms */ diff --git a/tests/unit_tests/openvpn/test_crypto.c b/tests/unit_tests/open= vpn/test_crypto.c index fdc8fbd..ec8e661 100644 --- a/tests/unit_tests/openvpn/test_crypto.c +++ b/tests/unit_tests/openvpn/test_crypto.c @@ -448,6 +448,29 @@ gc_free(&gc); } +void +crypto_test_aead_limits(void **state) +{ + /* if ChaCha20-Poly1305 is not supported by the crypto library or in t= he + * current mode (FIPS), this will still return -1 */ + assert_int_equal(cipher_get_aead_limits("CHACHA20-POLY1305"), 0); + + int64_t aeslimit =3D cipher_get_aead_limits("AES-128-GCM"); + + assert_int_equal(aeslimit, (1ull << 36) - 1); + + /* Check if this matches our exception for 1600 size packets assuming + * AEAD_LIMIT_BLOCKSIZE (128 bits/ 16 bytes). Gives us 100 blocks + * + 1 for the packet */ + int64_t L =3D 101; + /* 2 ^ 29.34, using the result here to avoid linking to libm */ + assert_int_equal(aeslimit / L, 680390858); + + /* and for 9000, 2^26.86 */ + L =3D 563; + assert_int_equal(aeslimit / L, 122059461); +} + int main(void) { @@ -458,7 +481,8 @@ cmocka_unit_test(crypto_test_tls_prf), cmocka_unit_test(crypto_test_hmac), cmocka_unit_test(test_occ_mtu_calculation), - cmocka_unit_test(test_mssfix_mtu_calculation) + cmocka_unit_test(test_mssfix_mtu_calculation), + cmocka_unit_test(crypto_test_aead_limits) }; #if defined(ENABLE_CRYPTO_OPENSSL) -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/796?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: I057f007577f10c6ac917ee4620ee3d2559187dc7 Gerrit-Change-Number: 796 Gerrit-PatchSet: 7 Gerrit-Owner: plaisthos Gerrit-Reviewer: flichtenheld Gerrit-CC: cron2 Gerrit-CC: openvpn-devel Gerrit-Attention: cron2 Gerrit-Attention: flichtenheld Gerrit-MessageType: newpatchset --9q1uDxzSmto= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: cron2, flichtenheld.

plaisthos uploaded patch set #7 to this change.

<= a href=3D"http://gerrit.openvpn.net/c/openvpn/+/796?usp=3Demail">View Chang= e

Trigger renegotiation of data key if getting close to the AEAD usage =
limit

This implements the limitation of AEAD key usage[1] with a con= fidentiality
margin of 2^-57, the same as TLS 1.3. In this implementati= on, unlike
TLS 1.3 that counts the number of records, we count the actua= l number of
packets and plaintext blocks. TLS 1.3 can reasonable assume = that for
large data transfers, full records are used and therefore the m= aximum
record size of 2**14 (2*10 blocks) is used to calculate the numbe= r of
records before a new key needs to be used.

For a VPN like Op= enVPN, the same calculation would either require using a
pessimistic ass= umption of using a MTU size of 65k which limits us to
2^24 packets, whic= h equals only 24 GB with more common MTU/MSS of 1400
or requiring a dyna= mic calculation which includes the actual MTU that
we allow to send. For= 1500 the calculation yields 2*29.4 which is a
quite significant higher = number of packets (923 GB at 1400 MSS/MTU).

To avoid this dynamic ca= lculation and also avoid needing to know the
MSS/MTU size in the crypto = layer, this implementation foregoes the
simplification of counting just = packets but will count blocks and packets
instead and determines the lim= it from that.

This also has the side effect that connections with a = lot of small packets
(like TCP ACKs) mixed with large packets will be ab= le to keep using the same
key much longer until requiring a renegotiatio= n.

This patch will set the limit where to trigger the renegotiation = at 7/8
of the recommended maximum value.

[1] https://www.ietf.or= g/archive/id/draft-irtf-cfrg-aead-limits-08.html

Testing instruction= s:

The easiest way to test if this patch works as
intended is to = manually change the return value of cipher_get_aead_limits
to some silly= low value like 2048. After a bit of VPN traffic, a soft
reset should oc= cur that indicates being over the

TLS: soft reset sec=3D41/3600 = bytes=3D59720/-1 pkts=3D78/0 aead_limit_send=3D1883/1792 aead_limit_recv=3D= 1937/1792

Here the send limit is over the limit (1792 =3D 2048 * 8/7= ).

Change-Id: I057f007577f10c6ac917ee4620ee3d2559187dc7
Signed-of= f-by: Arne Schwabe <arne@...1227...>
---
M Changes.rst
M src= /openvpn/crypto.c
M src/openvpn/crypto.h
M src/openvpn/ssl.c
M src= /openvpn/ssl_common.h
M tests/unit_tests/openvpn/test_crypto.c
6 file= s changed, 159 insertions(+), 3 deletions(-)

git pull ssh://gerri=
t.openvpn.net:29418/openvpn refs/changes/96/796/7
diff --git a/Chan=
ges.rst b/Changes.rst
index a6cc3be..34542a5 100644<= br>--- a/Changes.rst
+++ b/Changes.rst
@@ -22,6 +22,14 @@

For more detail= s see [lwipovpn on Gihtub](https://github.com/OpenVPN/lwipovpn).
=
+Enforcement = of AES-GCM usage limit
+ confidentiality m= argin as TLS 1.3 does. This mean that renegotiation will
+ be triggered after roughly 2^28 to= 2^31 packets depending of the packet
+ size. More details about usage limit of AES-GCM can b= e found here:
+
+ https://datatracker.i= etf.org/doc/draft-irtf-cfrg-aead-limits/
+
Deprecated features
= -------------------
``secret`` support has been removed b= y default.
diff --git a/src/openvpn/crypto.c b/src/openvpn/= crypto.c
index 8f34eaa..000a9a7 100644
--- = a/src/openvpn/crypto.c
+++ b/src/openvpn/crypto.c@@ -134,6 +134,11 @@
ASSERT(cipher_ctx_update(c= tx->cipher, BEND(&work), &outlen, BPTR(buf), BLEN(buf)));=
ASSERT(buf_inc_len(&work, outlen));

+ /* update number o= f plaintext blocks encrypted. Use the (x + (n-1))/n trick
+ * to round up the result to the = number of blocks used */
+ const int blocksize =3D AEAD_LIMIT_BLOCKSIZE;
+ opt->key_ctx_bi.encrypt.plaintext= _blocks +=3D (outlen + (blocksize - 1))/blocksize;
+
/* Flush the encryptio= n buffer */
ASSERT(cipher_ctx_final(ctx->cipher, BE= ND(&work), &outlen));
ASSERT(buf_inc_len(&= work, outlen));
@@ -321,6 +326,38 @@
}=
}

+int64_t
+cipher_get_aead_limits(const char *ciphername)
+{
+ if (!cipher_kt_mode_aead(ciphername))
+ {
+ return 0;
+ }
+
+ if (cip= her_kt_name(ciphername) =3D=3D cipher_kt_name("CHACHA20-POLY1305"= ))
+ {
<= span style=3D"color: hsl(120, 100%, 40%);">+ return 0;

+ }

+
+ /* Assume all other ciphers require the limit */
+
+ /* We focus here on the equation
+ *
+ * q + s <=3D p^(1/2) * 2^(129/2) -= 1
+ * q &l= t;=3D (p^(1/2) * 2^(129/2) - 1) / (L + 1)
+ *
+ * as is the one that is limiting us.
+ *
+ * With p =3D 2^-57 this becomes
+ *
+ * q + s <=3D (2^36 - 1)
+ *
+ */
+ int64_t rs =3D (1ull << 36) - 1;
+
+ return rs;
+}
+

bool
crypto_check_replay(struct crypto_optio= ns *opt,
const struct packet_id_net *p= in, const char *error_prefix,
@@ -478,6 +515,12 @@ goto error_exit;
}
<= /span>
+
+ /* update number of plaintext block= s decrypted. Use the (x + (n-1))/n trick
+ * to round up the result to the number of blocks = used. */
+ const i= nt blocksize =3D AEAD_LIMIT_BLOCKSIZE;
+ opt->key_ctx_bi.decrypt.plaintext_blocks +=3D (ou= tlen + (blocksize - 1))/blocksize;
+
*buf =3D work;

gc_free(&gc);
diff --git a/src/openvpn/= crypto.h b/src/openvpn/crypto.h
index 074dad6..052eacd 1006= 44
--- a/src/openvpn/crypto.h
+++ b/src/ope= nvpn/crypto.h
@@ -166,6 +166,9 @@
uint= 8_t implicit_iv[OPENVPN_MAX_IV_LENGTH];
/**< The im= plicit part of the IV */
size_t implicit_iv_len; /= **< The length of implicit_iv */
+ /** Counter for the number of plaintext encrypted using= this cipher
+ * = in number of 128 bit blocks (only used for AEAD ciphers) */
+ uint64_t plaintext_blocks;

};

#define KEY_DIRECTION_BI= DIRECTIONAL 0 /* same keys for both directions */
@@ -596,6= +599,24 @@
}

/**
+ * Check if the cipher is= an AEAD cipher and needs to be limited to a certain
+ * number of number of blocks + packets. = Return -1 if ciphername is not an AEAD
+ * cipher or no limit (e.g. Chacha20-Poly1305) is needed= .
+ *
+ * For reference see the OpenVPN RF= C draft and

+ * https= ://www.ietf.org/archive/id/draft-irtf-cfrg-aead-limits-08.html
+ */
+int64_t
+cipher_get_aead_limits(const char *ciphername);
+
+/**
+ * Blocksize used for the AEAD limit caluclation
+ *
+ * Since cipher_ctx_block_size() is not reliable and wi= ll return 1 in many
+= * cases use a hardcoded blocksize instead */
+#define AEAD_LIMIT_BLOCKSIZE 16
= +
+/**
* Checks if the current TLS= library supports the TLS 1.0 PRF with MD5+SHA1
* that Op= enVPN uses when TLS Keying Material Export is not available.
*
@@ -603,4 +624,20 @@
*/
<= span> bool check_tls_prf_working(void);

+/**
+ * Checks if the usage limit for an AEAD cipher is re= ached
+ *
<= span style=3D"color: hsl(120, 100%, 40%);">+ * This method abstracts the ca= lculation to make the calling function easier
+ * to read.
+ */
+static inline bool
= +aead_usage_limit_reached(const int64_t limit, const struct key_ctx *key_ct= x,
+ = int64_t higest_pid)
+{
+ /* Thi= s is the q + s <=3D p^(1/2) * 2^(129/2) - 1 calculation where+ * q is the number of pr= otected messages (highest_pid)
+ * s Total plaintext length in all messages (in blocks) */
+ return (limit >= ; 0 && key_ctx->plaintext_blocks + higest_pid > limit);
+}
+
#endif /* CRYPTO_H */
diff --git a/src/openvpn/ssl.c b/src/openvpn/ssl.cindex 1f8eb1e..2acf985 100644
--- a/src/openvpn/ssl.= c
+++ b/src/openvpn/ssl.c
@@ -131,6 +131,26= @@
}
}

<= span style=3D"color: hsl(120, 100%, 40%);">+static int64_t

+tls_get_limit_aead(const char *ciphe= rname)
+{
<= span style=3D"color: hsl(120, 100%, 40%);">+ int64_t limit =3D cipher_ge= t_aead_limits(ciphername);

+
+ if (limi= t =3D=3D 0)
+ {

+ return 0;
+ }
+
+ /* set limit to 7/8 of the limit so the renogiation c= an succeed before
+ = * we go over the limit */
+ limit =3D limit * 7/8;
+
+ = msg(D_SHOW_KEYS, "Note: AEAD cipher %s will be limited to a sum of %&= quot;
+ PRIi64= " for block and packets before renegotiation",
+ ciphername, limit);+ return limit;
<= span style=3D"color: hsl(120, 100%, 40%);">+}

+
void
tls_init_= control_channel_frame_parameters(struct frame *frame, int tls_mtu) {
@@ -1576,6 +1596,8 @@
tls_l= imit_reneg_bytes(session->opt->key_type.cipher,
= &session->opt->renegotiate_bytes);
+ sessio= n->opt->aead_usage_limit =3D tls_get_limit_aead(session->opt->k= ey_type.cipher);
+

/* set the state of the keys for the session to generate= d */
ks->state =3D S_GENERATED_KEYS;

@@ -2996,6 +3018,18 @@
return = true;
}

+ /* Check the AEAD usage limit of cleartext b= locks + packets */
+ = const struct key_ctx_bi *key_ctx_bi =3D &ks->crypto_options.key_c= tx_bi;
+ const int= 64_t usage_limit =3D session->opt->aead_usage_limit;
+
+ if (aead_usage_limit_reached(usage_limit, &key_= ctx_bi->encrypt,
+= ks->crypto_options.packet_id.send.id)
+ || aead_usage= _limit_reached(usage_limit, &key_ctx_bi->decrypt,
+ ks= ->crypto_options.packet_id.rec.id))
+ {
+ return true;
+ }
+<= br> return false;
}
/*@@ -3028,10 +3062,17 @@
&& should_= trigger_renegotiation(session, ks))
{
= msg(D_TLS_DEBUG_LOW, "TLS: soft reset sec=3D%d/%d bytes=3D&qu= ot; counter_format
- = "/%" PRIi64 " pkts=3D" counter_format "/%= " PRIi64,
+ = "/%" PRIi64 " pkts=3D" counter_format "/%&q= uot; PRIi64
+ = " aead_limit_send=3D%" PRIu64 "/%" PRIu64+ " aead_limit= _recv=3D%" PRIu64 "/%" PRIu64,
= (int) (now - ks->established), session->opt->renegotiate_seconds,<= /span>
ks->n_bytes, session->opt->renegotiat= e_bytes,
- k= s->n_packets, session->opt->renegotiate_packets);
+ ks->n_packets, sessio= n->opt->renegotiate_packets,
+ ks->crypto_options.key_ctx_bi.encrypt.plainte= xt_blocks + ks->n_packets,
+ session->opt->aead_usage_limit,
+ ks->crypto_options.k= ey_ctx_bi.decrypt.plaintext_blocks + ks->n_packets,

+ session->opt->aead_usa= ge_limit
+ = );
key_state_soft_reset(session);
}


diff --git a/src/openvpn/ssl_comm= on.h b/src/openvpn/ssl_common.h
index 5840e2d..ced5299 1006= 44
--- a/src/openvpn/ssl_common.h
+++ b/src= /openvpn/ssl_common.h
@@ -333,6 +333,9 @@
= interval_t packet_timeout;
int64_t renegotiate_byt= es;
int64_t renegotiate_packets;
+ /** limit for AEAD cipher, this is th= e sum of packets + blocks
+ * that are allowed to be used */
+ int64_t aead_usage_limit;
= interval_t renegotiate_seconds;

/* c= ert verification parms */
diff --git a/tests/unit_tests/ope= nvpn/test_crypto.c b/tests/unit_tests/openvpn/test_crypto.c
index fdc8fbd..ec8e661 100644
--- a/tests/unit_tests/openv= pn/test_crypto.c
+++ b/tests/unit_tests/openvpn/test_crypto= .c
@@ -448,6 +448,29 @@
gc_free(&g= c);
}

+void
+crypto_test_aead_limits(void **state)
+{
+ /* if ChaCha20-Poly1305 is not supported by the crypto library or= in the
+ * curre= nt mode (FIPS), this will still return -1 */
+ assert_int_equal(cipher_get_aead_limits("= CHACHA20-POLY1305"), 0);
+
+ int64= _t aeslimit =3D cipher_get_aead_limits("AES-128-GCM");
= +
+ assert_int_equal(aeslimit, (1ull << 36)= - 1);
+
+ /* Check if this matches our= exception for 1600 size packets assuming
+ * AEAD_LIMIT_BLOCKSIZE (128 bits/ 16 bytes). Giv= es us 100 blocks
+ = * + 1 for the packet */
+ int64_t L =3D 101;
+ /* 2 ^ 29.34, using the result here to avoid linking to libm *= /
+ assert_int_equ= al(aeslimit / L, 680390858);
+
+ /* and= for 9000, 2^26.86 */
+ L =3D 563;
+ = assert_int_equal(aeslimit / L, 122059461);
+}
+
int
main(void)
{<= /span>
@@ -458,7 +481,8 @@
cmocka_unit_te= st(crypto_test_tls_prf),
cmocka_unit_test(crypto_t= est_hmac),
cmocka_unit_test(test_occ_mtu_calculati= on),
- cmocka_un= it_test(test_mssfix_mtu_calculation)
+ cmocka_unit_test(test_mssfix_mtu_calculation),
+ cmocka_unit_tes= t(crypto_test_aead_limits)
};
=
#if defined(ENABLE_CRYPTO_OPENSSL)

To view, visit change 796. To unsubscribe, or for help writing mail filte= rs, visit settings.

<= div itemscope itemtype=3D"http://schema.org/EmailMessage">
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I057f007577f10c6ac917ee4620e= e3d2559187dc7
Gerrit-Change-Number: 796
Gerrit-PatchSet: 7
Gerrit-Owner: plaisthos <arne-openvpn@...1711...= 27...>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-CC: cron2 <gert@...1296...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: cron2 <gert@...1296...>= ;
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-MessageType: newpatchset
--9q1uDxzSmto=--