From 4b223b14cd5d719e9378d89710ffa7e6ee11bfea Mon Sep 17 00:00:00 2001 From: Matthew Endsley Date: Wed, 3 Feb 2021 13:23:33 -0800 Subject: [PATCH] Do not compute HMAC for zero-length cookies This is a common case since all clients send a non-signed ClientHello message to initiate a new session. There is no need to perform the hash+hmac for signatures that will always fail verification due to a mismatched size. --- Hazel/Dtls/Handshake.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Hazel/Dtls/Handshake.cs b/Hazel/Dtls/Handshake.cs index a70af4e..880311b 100644 --- a/Hazel/Dtls/Handshake.cs +++ b/Hazel/Dtls/Handshake.cs @@ -452,6 +452,11 @@ namespace Hazel.Dtls /// True if the cookie is valid. Otherwise false public static bool VerifyCookie(ByteSpan cookie, EndPoint peerAddress, HMAC hmac) { + if (cookie.Length != CookieSize) + { + return false; + } + ByteSpan expectedHash = ComputeAddressMac(peerAddress, hmac); if (expectedHash.Length != cookie.Length) { -- 2.39.5