Add CNG manual memory management code to support Vista

Windows 7 improved the memory management in CNG.  To support Vista we
need to manage memory for cryptographic objects ourselves.  This change
adds a key object memory pointer to the cipher context and adds code to
allocate and free it along with the key handle.
This commit is contained in:
dkulwin 2015-10-29 15:46:37 -05:00
parent 728c299d67
commit bc6871e862
2 changed files with 17 additions and 2 deletions

View File

@ -216,6 +216,7 @@ unsigned int cng_cipher_init(PSSH_CNG_CIPHER_CTX x, const unsigned char *key, un
DWORD cbData = 0; DWORD cbData = 0;
LPCWSTR pAlg = NULL; LPCWSTR pAlg = NULL;
DWORD cbBlockLen = 0; DWORD cbBlockLen = 0;
DWORD cbKeyObject = 0;
if ((0 == (flags & _CNG_CIPHER_AES)) || (0 == (flags & (_CNG_MODE_CBC | _CNG_MODE_CTR)))) if ((0 == (flags & _CNG_CIPHER_AES)) || (0 == (flags & (_CNG_MODE_CBC | _CNG_MODE_CTR))))
return STATUS_INVALID_PARAMETER; return STATUS_INVALID_PARAMETER;
@ -281,12 +282,23 @@ unsigned int cng_cipher_init(PSSH_CNG_CIPHER_CTX x, const unsigned char *key, un
} }
if (status == S_OK) if (status == S_OK)
{
status = BCryptGetProperty(
hAlg,
BCRYPT_OBJECT_LENGTH,
(PBYTE)cbKeyObject,
sizeof(DWORD),
&cbData,
0);
}
if ((status == S_OK) && (x->pKeyObject = (PBYTE)HeapAlloc(GetProcessHeap(),0,cbKeyObject)))
{ {
status = BCryptGenerateSymmetricKey( status = BCryptGenerateSymmetricKey(
hAlg, hAlg,
&(x->hKey), &(x->hKey),
NULL, x->pKeyObject,
0, cbKeyObject,
(PBYTE)key, (PBYTE)key,
keylen, keylen,
0); 0);
@ -310,6 +322,8 @@ void cng_cipher_cleanup(PSSH_CNG_CIPHER_CTX x)
HeapFree(GetProcessHeap(), 0, x->pbIV); HeapFree(GetProcessHeap(), 0, x->pbIV);
if (x->hKey) if (x->hKey)
BCryptDestroyKey(x->hKey); BCryptDestroyKey(x->hKey);
if (x->pKeyObject)
HeapFree(GetProcessHeap(), 0, x->pKeyObject);
} }
#endif #endif

View File

@ -63,6 +63,7 @@ extern "C" {
unsigned char * pbIV; unsigned char * pbIV;
unsigned int cbBlockSize; unsigned int cbBlockSize;
unsigned int flags; unsigned int flags;
PBYTE pKeyObject;
} SSH_CNG_CIPHER_CTX, *PSSH_CNG_CIPHER_CTX; } SSH_CNG_CIPHER_CTX, *PSSH_CNG_CIPHER_CTX;