Skip to content

Commit

Permalink
crypto.c: ensure we don't pass too large key size to CryptoNG
Browse files Browse the repository at this point in the history
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 1.0.1.

Reported-by: Vladimir Tokarev <[email protected]>
Signed-off-by: Lev Stipakov <[email protected]>
  • Loading branch information
lstipakov committed Mar 18, 2024
1 parent 1b8b417 commit e668de0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion PropertySheet.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PropertyGroup Label="UserMacros">
<OVPN_DCO_VERSION_MAJOR>1</OVPN_DCO_VERSION_MAJOR>
<OVPN_DCO_VERSION_MINOR>0</OVPN_DCO_VERSION_MINOR>
<OVPN_DCO_VERSION_PATCH>0</OVPN_DCO_VERSION_PATCH>
<OVPN_DCO_VERSION_PATCH>1</OVPN_DCO_VERSION_PATCH>
</PropertyGroup>
<PropertyGroup />
<ItemDefinitionGroup>
Expand Down
8 changes: 8 additions & 0 deletions crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,14 @@ OvpnCryptoNewKey(OvpnCryptoContext* cryptoContext, POVPN_CRYPTO_DATA cryptoData)
algHandle = cryptoContext->ChachaAlgHandle;
}

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));
Expand Down

0 comments on commit e668de0

Please sign in to comment.