rsaenh: Use CRT memory allocators.

Signed-off-by: Paul Gofman <pgofman@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Paul Gofman 2022-05-13 11:23:25 -05:00 committed by Alexandre Julliard
parent d72942e907
commit cec9705008
4 changed files with 58 additions and 54 deletions

View File

@ -23,6 +23,7 @@
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include "windef.h"
#include "winbase.h"
@ -70,7 +71,7 @@ void destroy_handle_table(struct handle_table *lpTable)
{
TRACE("(lpTable=%p)\n", lpTable);
HeapFree(GetProcessHeap(), 0, lpTable->paEntries);
free(lpTable->paEntries);
lpTable->mutex.DebugInfo->Spare[0] = 0;
DeleteCriticalSection(&lpTable->mutex);
}
@ -139,14 +140,14 @@ static BOOL grow_handle_table(struct handle_table *lpTable)
newIEntries = lpTable->iEntries + TABLE_SIZE_INCREMENT;
newEntries = HeapAlloc(GetProcessHeap(), 0, sizeof(struct handle_table_entry)*newIEntries);
newEntries = malloc(sizeof(struct handle_table_entry)*newIEntries);
if (!newEntries)
return FALSE;
if (lpTable->paEntries)
{
memcpy(newEntries, lpTable->paEntries, sizeof(struct handle_table_entry)*lpTable->iEntries);
HeapFree(GetProcessHeap(), 0, lpTable->paEntries);
free(lpTable->paEntries);
}
for (i=lpTable->iEntries; i<newIEntries; i++)
@ -356,7 +357,7 @@ HCRYPTKEY new_object(struct handle_table *lpTable, size_t cbSize, DWORD dwType,
if (ppObject)
*ppObject = NULL;
pObject = HeapAlloc(GetProcessHeap(), 0, cbSize);
pObject = malloc(cbSize);
if (!pObject)
return (HCRYPTKEY)INVALID_HANDLE_VALUE;
@ -365,7 +366,7 @@ HCRYPTKEY new_object(struct handle_table *lpTable, size_t cbSize, DWORD dwType,
pObject->destructor = destructor;
if (!alloc_handle(lpTable, pObject, &hObject))
HeapFree(GetProcessHeap(), 0, pObject);
free(pObject);
else
if (ppObject)
*ppObject = pObject;

View File

@ -23,6 +23,7 @@
*/
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include "windef.h"
#include "winbase.h"
@ -273,7 +274,7 @@ BOOL encrypt_block_impl(ALG_ID aiAlgid, DWORD dwKeySpec, KEY_CONTEXT *pKeyContex
}
reverse_bytes(out, outlen);
} else {
in_reversed = HeapAlloc(GetProcessHeap(), 0, inlen);
in_reversed = malloc(inlen);
if (!in_reversed) {
SetLastError(NTE_NO_MEMORY);
return FALSE;
@ -281,11 +282,11 @@ BOOL encrypt_block_impl(ALG_ID aiAlgid, DWORD dwKeySpec, KEY_CONTEXT *pKeyContex
memcpy(in_reversed, in, inlen);
reverse_bytes(in_reversed, inlen);
if (rsa_exptmod(in_reversed, inlen, out, &outlen, dwKeySpec, &pKeyContext->rsa) != CRYPT_OK) {
HeapFree(GetProcessHeap(), 0, in_reversed);
free(in_reversed);
SetLastError(NTE_FAIL);
return FALSE;
}
HeapFree(GetProcessHeap(), 0, in_reversed);
free(in_reversed);
}
break;
@ -341,14 +342,14 @@ BOOL import_public_key_impl(const BYTE *pbSrc, KEY_CONTEXT *pKeyContext, DWORD d
return FALSE;
}
pbTemp = HeapAlloc(GetProcessHeap(), 0, dwKeyLen);
pbTemp = malloc(dwKeyLen);
if (!pbTemp) return FALSE;
memcpy(pbTemp, pbSrc, dwKeyLen);
pKeyContext->rsa.type = PK_PUBLIC;
reverse_bytes(pbTemp, dwKeyLen);
mp_read_unsigned_bin(&pKeyContext->rsa.N, pbTemp, dwKeyLen);
HeapFree(GetProcessHeap(), 0, pbTemp);
free(pbTemp);
mp_set_int(&pKeyContext->rsa.e, dwPubExp);
return TRUE;
@ -416,7 +417,7 @@ BOOL import_private_key_impl(const BYTE *pbSrc, KEY_CONTEXT *pKeyContext, DWORD
return FALSE;
}
pbTemp = HeapAlloc(GetProcessHeap(), 0, 2*dwKeyLen+5*((dwKeyLen+1)>>1));
pbTemp = malloc(2*dwKeyLen+5*((dwKeyLen+1)>>1));
if (!pbTemp) return FALSE;
memcpy(pbTemp, pbSrc, min(dwDataLen, 2*dwKeyLen+5*((dwKeyLen+1)>>1)));
pbBigNum = pbTemp;
@ -448,6 +449,6 @@ BOOL import_private_key_impl(const BYTE *pbSrc, KEY_CONTEXT *pKeyContext, DWORD
mp_read_unsigned_bin(&pKeyContext->rsa.d, pbBigNum, dwKeyLen);
mp_set_int(&pKeyContext->rsa.e, dwPubExp);
HeapFree(GetProcessHeap(), 0, pbTemp);
free(pbTemp);
return TRUE;
}

View File

@ -29,6 +29,7 @@
*/
#include <stdarg.h>
#include <stdlib.h>
#include "windef.h"
#include "winbase.h"
@ -119,7 +120,7 @@ static int mp_grow (mp_int * a, int size)
* in case the operation failed we don't want
* to overwrite the dp member of a.
*/
tmp = HeapReAlloc(GetProcessHeap(), 0, a->dp, sizeof (mp_digit) * size);
tmp = realloc(a->dp, sizeof (mp_digit) * size);
if (tmp == NULL) {
/* reallocation failed but "a" is still valid [can be freed] */
return MP_MEM;
@ -204,7 +205,7 @@ static int mp_init (mp_int * a)
int i;
/* allocate memory required and clear it */
a->dp = HeapAlloc(GetProcessHeap(), 0, sizeof (mp_digit) * MP_PREC);
a->dp = malloc(sizeof (mp_digit) * MP_PREC);
if (a->dp == NULL) {
return MP_MEM;
}
@ -232,7 +233,7 @@ static int mp_init_size (mp_int * a, int size)
size += (MP_PREC * 2) - (size % MP_PREC);
/* alloc mem */
a->dp = HeapAlloc(GetProcessHeap(), 0, sizeof (mp_digit) * size);
a->dp = malloc(sizeof (mp_digit) * size);
if (a->dp == NULL) {
return MP_MEM;
}
@ -264,7 +265,7 @@ mp_clear (mp_int * a)
}
/* free ram */
HeapFree(GetProcessHeap(), 0, a->dp);
free(a->dp);
/* reset members to make debugging easier */
a->dp = NULL;
@ -3425,7 +3426,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
bsize = (size>>3)+((size&7)?1:0);
/* we need a buffer of bsize bytes */
tmp = HeapAlloc(GetProcessHeap(), 0, bsize);
tmp = malloc(bsize);
if (tmp == NULL) {
return MP_MEM;
}
@ -3490,7 +3491,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
err = MP_OKAY;
error:
HeapFree(GetProcessHeap(), 0, tmp);
free(tmp);
return err;
}
@ -3712,7 +3713,7 @@ int mp_shrink (mp_int * a)
{
mp_digit *tmp;
if (a->alloc != a->used && a->used > 0) {
if ((tmp = HeapReAlloc(GetProcessHeap(), 0, a->dp, sizeof (mp_digit) * a->used)) == NULL) {
if ((tmp = realloc(a->dp, sizeof (mp_digit) * a->used)) == NULL) {
return MP_MEM;
}
a->dp = tmp;

View File

@ -23,6 +23,7 @@
*/
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include "windef.h"
@ -463,7 +464,7 @@ static inline const PROV_ENUMALGS_EX* get_algid_info(HCRYPTPROV hProv, ALG_ID al
*/
static inline BOOL copy_data_blob(PCRYPT_DATA_BLOB dst, const PCRYPT_DATA_BLOB src)
{
dst->pbData = HeapAlloc(GetProcessHeap(), 0, src->cbData);
dst->pbData = malloc(src->cbData);
if (!dst->pbData) {
SetLastError(NTE_NO_MEMORY);
return FALSE;
@ -494,7 +495,7 @@ static inline BOOL concat_data_blobs(PCRYPT_DATA_BLOB dst, const PCRYPT_DATA_BLO
const PCRYPT_DATA_BLOB src2)
{
dst->cbData = src1->cbData + src2->cbData;
dst->pbData = HeapAlloc(GetProcessHeap(), 0, dst->cbData);
dst->pbData = malloc(dst->cbData);
if (!dst->pbData) {
SetLastError(NTE_NO_MEMORY);
return FALSE;
@ -513,7 +514,7 @@ static inline BOOL concat_data_blobs(PCRYPT_DATA_BLOB dst, const PCRYPT_DATA_BLO
* pBlob [I] Heap space occupied by pBlob->pbData is released
*/
static inline void free_data_blob(PCRYPT_DATA_BLOB pBlob) {
HeapFree(GetProcessHeap(), 0, pBlob->pbData);
free(pBlob->pbData);
}
/******************************************************************************
@ -537,9 +538,9 @@ static inline void init_data_blob(PCRYPT_DATA_BLOB pBlob) {
*/
static inline void free_hmac_info(PHMAC_INFO hmac_info) {
if (!hmac_info) return;
HeapFree(GetProcessHeap(), 0, hmac_info->pbInnerString);
HeapFree(GetProcessHeap(), 0, hmac_info->pbOuterString);
HeapFree(GetProcessHeap(), 0, hmac_info);
free(hmac_info->pbInnerString);
free(hmac_info->pbOuterString);
free(hmac_info);
}
/******************************************************************************
@ -560,13 +561,13 @@ static inline void free_hmac_info(PHMAC_INFO hmac_info) {
*/
static BOOL copy_hmac_info(PHMAC_INFO *dst, const HMAC_INFO *src) {
if (!src) return FALSE;
*dst = HeapAlloc(GetProcessHeap(), 0, sizeof(HMAC_INFO));
*dst = malloc(sizeof(HMAC_INFO));
if (!*dst) return FALSE;
**dst = *src;
(*dst)->pbInnerString = NULL;
(*dst)->pbOuterString = NULL;
if ((*dst)->cbInnerString == 0) (*dst)->cbInnerString = RSAENH_HMAC_DEF_PAD_LEN;
(*dst)->pbInnerString = HeapAlloc(GetProcessHeap(), 0, (*dst)->cbInnerString);
(*dst)->pbInnerString = malloc((*dst)->cbInnerString);
if (!(*dst)->pbInnerString) {
free_hmac_info(*dst);
return FALSE;
@ -576,7 +577,7 @@ static BOOL copy_hmac_info(PHMAC_INFO *dst, const HMAC_INFO *src) {
else
memset((*dst)->pbInnerString, RSAENH_HMAC_DEF_IPAD_CHAR, RSAENH_HMAC_DEF_PAD_LEN);
if ((*dst)->cbOuterString == 0) (*dst)->cbOuterString = RSAENH_HMAC_DEF_PAD_LEN;
(*dst)->pbOuterString = HeapAlloc(GetProcessHeap(), 0, (*dst)->cbOuterString);
(*dst)->pbOuterString = malloc((*dst)->cbOuterString);
if (!(*dst)->pbOuterString) {
free_hmac_info(*dst);
return FALSE;
@ -605,7 +606,7 @@ static void destroy_hash(OBJECTHDR *pObject)
free_hmac_info(pCryptHash->pHMACInfo);
free_data_blob(&pCryptHash->tpPRFParams.blobLabel);
free_data_blob(&pCryptHash->tpPRFParams.blobSeed);
HeapFree(GetProcessHeap(), 0, pCryptHash);
free(pCryptHash);
}
/******************************************************************************
@ -669,12 +670,12 @@ static inline void update_hash(CRYPTHASH *pCryptHash, const BYTE *pbData, DWORD
break;
case CALG_MAC:
pbTemp = HeapAlloc(GetProcessHeap(), 0, dwDataLen);
pbTemp = malloc(dwDataLen);
if (!pbTemp) return;
memcpy(pbTemp, pbData, dwDataLen);
RSAENH_CPEncrypt(pCryptHash->hProv, pCryptHash->hKey, 0, FALSE, 0,
pbTemp, &dwDataLen, dwDataLen);
HeapFree(GetProcessHeap(), 0, pbTemp);
free(pbTemp);
break;
default:
@ -742,7 +743,7 @@ static void destroy_key(OBJECTHDR *pObject)
free_data_blob(&pCryptKey->siSChannelInfo.blobClientRandom);
free_data_blob(&pCryptKey->siSChannelInfo.blobServerRandom);
free_data_blob(&pCryptKey->blobHmacKey);
HeapFree(GetProcessHeap(), 0, pCryptKey);
free(pCryptKey);
}
/******************************************************************************
@ -990,7 +991,7 @@ static void store_key_pair(HCRYPTKEY hCryptKey, HKEY hKey, DWORD dwKeySpec, DWOR
{
if (crypt_export_key(pKey, 0, PRIVATEKEYBLOB, 0, TRUE, 0, &dwLen))
{
pbKey = HeapAlloc(GetProcessHeap(), 0, dwLen);
pbKey = malloc(dwLen);
if (pbKey)
{
if (crypt_export_key(pKey, 0, PRIVATEKEYBLOB, 0, TRUE, pbKey,
@ -1007,7 +1008,7 @@ static void store_key_pair(HCRYPTKEY hCryptKey, HKEY hKey, DWORD dwKeySpec, DWOR
LocalFree(blobOut.pbData);
}
}
HeapFree(GetProcessHeap(), 0, pbKey);
free(pbKey);
}
}
}
@ -1248,7 +1249,7 @@ static void destroy_key_container(OBJECTHDR *pObjectHdr)
}
else
release_key_container_keys(pKeyContainer);
HeapFree( GetProcessHeap(), 0, pKeyContainer );
free( pKeyContainer );
}
/******************************************************************************
@ -1333,7 +1334,7 @@ static BOOL read_key_value(HCRYPTPROV hKeyContainer, HKEY hKey, DWORD dwKeySpec,
if (RegQueryValueExA(hKey, szValueName, 0, &dwValueType, NULL, &dwLen) ==
ERROR_SUCCESS)
{
pbKey = HeapAlloc(GetProcessHeap(), 0, dwLen);
pbKey = malloc(dwLen);
if (pbKey)
{
if (RegQueryValueExA(hKey, szValueName, 0, &dwValueType, pbKey, &dwLen) ==
@ -1350,7 +1351,7 @@ static BOOL read_key_value(HCRYPTPROV hKeyContainer, HKEY hKey, DWORD dwKeySpec,
LocalFree(blobOut.pbData);
}
}
HeapFree(GetProcessHeap(), 0, pbKey);
free(pbKey);
}
}
if (ret)
@ -1720,7 +1721,7 @@ static BOOL pkcs1_mgf1(HCRYPTPROV hProv, const BYTE *pbSeed, DWORD dwSeedLength,
RSAENH_CPDestroyHash(hProv, hHash);
/* Allocate multiples of hash value */
pbMask->pbData = HeapAlloc(GetProcessHeap(), 0, (dwLength + dwHashLen - 1) / dwHashLen * dwHashLen);
pbMask->pbData = malloc((dwLength + dwHashLen - 1) / dwHashLen * dwHashLen);
if (!pbMask->pbData)
{
SetLastError(NTE_NO_MEMORY);
@ -1728,7 +1729,7 @@ static BOOL pkcs1_mgf1(HCRYPTPROV hProv, const BYTE *pbSeed, DWORD dwSeedLength,
}
pbMask->cbData = dwLength;
pbHashInput = HeapAlloc(GetProcessHeap(), 0, dwSeedLength + sizeof(DWORD));
pbHashInput = malloc(dwSeedLength + sizeof(DWORD));
if (!pbHashInput)
{
free_data_blob(pbMask);
@ -1752,7 +1753,7 @@ static BOOL pkcs1_mgf1(HCRYPTPROV hProv, const BYTE *pbSeed, DWORD dwSeedLength,
RSAENH_CPDestroyHash(hProv, hHash);
}
HeapFree(GetProcessHeap(), 0, pbHashInput);
free(pbHashInput);
return TRUE;
}
@ -1802,7 +1803,7 @@ static BOOL pad_data_oaep(HCRYPTPROV hProv, const BYTE *abData, DWORD dwDataLen,
goto done;
}
pbPadded = HeapAlloc(GetProcessHeap(), 0, dwBufferLen);
pbPadded = malloc(dwBufferLen);
if (!pbPadded)
{
SetLastError(NTE_NO_MEMORY);
@ -1843,7 +1844,7 @@ static BOOL pad_data_oaep(HCRYPTPROV hProv, const BYTE *abData, DWORD dwDataLen,
ret = TRUE;
done:
RSAENH_CPDestroyHash(hProv, hHash);
HeapFree(GetProcessHeap(), 0, pbPadded);
free(pbPadded);
free_data_blob(&blobDbMask);
free_data_blob(&blobSeedMask);
return ret;
@ -1958,7 +1959,7 @@ static BOOL unpad_data_oaep(HCRYPTPROV hProv, const BYTE *abData, DWORD dwDataLe
}
/* Get default hash value */
pbHashValue = HeapAlloc(GetProcessHeap(), 0, dwHashLen);
pbHashValue = malloc(dwHashLen);
if (!pbHashValue)
{
SetLastError(NTE_NO_MEMORY);
@ -1968,7 +1969,7 @@ static BOOL unpad_data_oaep(HCRYPTPROV hProv, const BYTE *abData, DWORD dwDataLe
RSAENH_CPGetHashParam(hProv, hHash, HP_HASHVAL, pbHashValue, &dwLen, 0);
/* Store seed and DB */
pbBuffer = HeapAlloc(GetProcessHeap(), 0, dwDataLen - 1);
pbBuffer = malloc(dwDataLen - 1);
if (!pbBuffer)
{
SetLastError(NTE_NO_MEMORY);
@ -2012,8 +2013,8 @@ static BOOL unpad_data_oaep(HCRYPTPROV hProv, const BYTE *abData, DWORD dwDataLe
ret = TRUE;
done:
RSAENH_CPDestroyHash(hProv, hHash);
HeapFree(GetProcessHeap(), 0, pbHashValue);
HeapFree(GetProcessHeap(), 0, pbBuffer);
free(pbHashValue);
free(pbBuffer);
free_data_blob(&blobDbMask);
free_data_blob(&blobSeedMask);
return ret;
@ -3227,25 +3228,25 @@ static BOOL import_symmetric_key(HCRYPTPROV hProv, const BYTE *pbData, DWORD dwD
return FALSE;
}
pbDecrypted = HeapAlloc(GetProcessHeap(), 0, pPubKey->dwBlockLen);
pbDecrypted = malloc(pPubKey->dwBlockLen);
if (!pbDecrypted) return FALSE;
encrypt_block_impl(pPubKey->aiAlgid, PK_PRIVATE, &pPubKey->context, pbKeyStream, pbDecrypted,
RSAENH_DECRYPT);
dwKeyLen = RSAENH_MAX_KEY_SIZE;
if (!unpad_data(hProv, pbDecrypted, pPubKey->dwBlockLen, pbDecrypted, &dwKeyLen, dwFlags)) {
HeapFree(GetProcessHeap(), 0, pbDecrypted);
free(pbDecrypted);
return FALSE;
}
*phKey = new_key(hProv, pBlobHeader->aiKeyAlg, dwKeyLen<<19, &pCryptKey);
if (*phKey == (HCRYPTKEY)INVALID_HANDLE_VALUE)
{
HeapFree(GetProcessHeap(), 0, pbDecrypted);
free(pbDecrypted);
return FALSE;
}
memcpy(pCryptKey->abKeyValue, pbDecrypted, dwKeyLen);
HeapFree(GetProcessHeap(), 0, pbDecrypted);
free(pbDecrypted);
setup_key(pCryptKey);
if (dwFlags & CRYPT_EXPORTABLE)
pCryptKey->dwPermissions |= CRYPT_EXPORT;
@ -4897,13 +4898,13 @@ BOOL WINAPI RSAENH_CPVerifySignature(HCRYPTPROV hProv, HCRYPTHASH hHash, const B
dwHashLen = RSAENH_MAX_HASH_SIZE;
if (!RSAENH_CPGetHashParam(hProv, hHash, HP_HASHVAL, abHashValue, &dwHashLen, 0)) return FALSE;
pbConstructed = HeapAlloc(GetProcessHeap(), 0, dwSigLen);
pbConstructed = malloc(dwSigLen);
if (!pbConstructed) {
SetLastError(NTE_NO_MEMORY);
goto cleanup;
}
pbDecrypted = HeapAlloc(GetProcessHeap(), 0, dwSigLen);
pbDecrypted = malloc(dwSigLen);
if (!pbDecrypted) {
SetLastError(NTE_NO_MEMORY);
goto cleanup;
@ -4931,7 +4932,7 @@ BOOL WINAPI RSAENH_CPVerifySignature(HCRYPTPROV hProv, HCRYPTHASH hHash, const B
SetLastError(NTE_BAD_SIGNATURE);
cleanup:
HeapFree(GetProcessHeap(), 0, pbConstructed);
HeapFree(GetProcessHeap(), 0, pbDecrypted);
free(pbConstructed);
free(pbDecrypted);
return res;
}