From 395f63b4abf3de18cf8abbc9534cf5d40b03a8d1 Mon Sep 17 00:00:00 2001 From: Lev Stipakov Date: Mon, 18 Mar 2024 19:56:52 +0200 Subject: [PATCH] crypto.c: ensure we don't pass too large key size to CryptoNG We use BCryptGenerateSymmetricKey() to generate a symmetric key object, passing a buffer containing a key and a key length. While buffer length is guaranteed not to exceed 32 bytes, the key length value is passed from userspace and could be at max 256 bytes. The documentation says that: If the data passed in exceeds the target key size, the data will be truncated and the excess will be ignored. which means that passing large length should not be a problem. I confirmed it with test with driver verifier enabled - I passed "256" as key length and haven't got any errors (and got key objected created and VPN session set up). Nevertheless, let's be good citizens and error out if passed key length exceeds 32 bytes - maximum key length for AES-GCM and ChaCha20 ciphers. Bump version to 2.0.1. Cherry-picked from https://github.com/OpenVPN/ovpn-dco-win/commit/9d4083cd62cfb8268176cc6b75e0726743afa5fd Reported-by: Vladimir Tokarev Signed-off-by: Lev Stipakov --- PropertySheet.props | 2 +- crypto.cpp | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/PropertySheet.props b/PropertySheet.props index 6b52bf1..f63952e 100644 --- a/PropertySheet.props +++ b/PropertySheet.props @@ -4,7 +4,7 @@ 2 0 - 0 + 1 diff --git a/crypto.cpp b/crypto.cpp index 0e2d9a0..75005f4 100644 --- a/crypto.cpp +++ b/crypto.cpp @@ -249,6 +249,14 @@ OvpnCryptoNewKey(OvpnCryptoContext* cryptoContext, POVPN_CRYPTO_DATA cryptoData, keySlot->DecKey = NULL; } + if ((cryptoData->Encrypt.KeyLen > 32) || (cryptoData->Decrypt.KeyLen > 32)) + { + status = STATUS_INVALID_DEVICE_REQUEST; + LOG_ERROR("Incorrect encrypt or decrypt key length", TraceLoggingValue(cryptoData->Encrypt.KeyLen, "Encrypt.KeyLen"), + TraceLoggingValue(cryptoData->Decrypt.KeyLen, "Decrypt.KeyLen")); + goto done; + } + // generate keys from key materials GOTO_IF_NOT_NT_SUCCESS(done, status, BCryptGenerateSymmetricKey(algHandle, &keySlot->EncKey, NULL, 0, cryptoData->Encrypt.Key, cryptoData->Encrypt.KeyLen, 0)); GOTO_IF_NOT_NT_SUCCESS(done, status, BCryptGenerateSymmetricKey(algHandle, &keySlot->DecKey, NULL, 0, cryptoData->Decrypt.Key, cryptoData->Decrypt.KeyLen, 0));