advapi32: Add magic codes to HCRYPTKEY and HCRYPTHASH.

This commit is contained in:
Austin Lund 2010-08-18 10:14:42 +10:00 committed by Alexandre Julliard
parent 068450363e
commit c98e6c09ae
2 changed files with 56 additions and 22 deletions

View File

@ -725,7 +725,8 @@ BOOL WINAPI CryptCreateHash (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey,
SetLastError(ERROR_INVALID_HANDLE); SetLastError(ERROR_INVALID_HANDLE);
return FALSE; return FALSE;
} }
if (!phHash || prov->dwMagic != MAGIC_CRYPTPROV) if (!phHash || prov->dwMagic != MAGIC_CRYPTPROV ||
(key && key->dwMagic != MAGIC_CRYPTKEY))
{ {
SetLastError(ERROR_INVALID_PARAMETER); SetLastError(ERROR_INVALID_PARAMETER);
return FALSE; return FALSE;
@ -742,7 +743,7 @@ BOOL WINAPI CryptCreateHash (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey,
} }
hash->pProvider = prov; hash->pProvider = prov;
hash->dwMagic = MAGIC_CRYPTHASH;
if (prov->pFuncs->pCPCreateHash(prov->hPrivate, Algid, if (prov->pFuncs->pCPCreateHash(prov->hPrivate, Algid,
key ? key->hPrivate : 0, 0, &hash->hPrivate)) key ? key->hPrivate : 0, 0, &hash->hPrivate))
{ {
@ -751,6 +752,7 @@ BOOL WINAPI CryptCreateHash (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey,
} }
/* CSP error! */ /* CSP error! */
hash->dwMagic = 0;
CRYPT_Free(hash); CRYPT_Free(hash);
*phHash = 0; *phHash = 0;
return FALSE; return FALSE;
@ -783,7 +785,9 @@ BOOL WINAPI CryptDecrypt (HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p)\n", hKey, hHash, Final, dwFlags, pbData, pdwDataLen); TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p)\n", hKey, hHash, Final, dwFlags, pbData, pdwDataLen);
if (!key || !pbData || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV) if (!key || !pbData || !pdwDataLen ||
!key->pProvider || key->dwMagic != MAGIC_CRYPTKEY ||
key->pProvider->dwMagic != MAGIC_CRYPTPROV)
{ {
SetLastError(ERROR_INVALID_PARAMETER); SetLastError(ERROR_INVALID_PARAMETER);
return FALSE; return FALSE;
@ -824,7 +828,7 @@ BOOL WINAPI CryptDeriveKey (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData
SetLastError(ERROR_INVALID_HANDLE); SetLastError(ERROR_INVALID_HANDLE);
return FALSE; return FALSE;
} }
if (!phKey || prov->dwMagic != MAGIC_CRYPTPROV) if (!phKey || prov->dwMagic != MAGIC_CRYPTPROV || hash->dwMagic != MAGIC_CRYPTHASH)
{ {
SetLastError(ERROR_INVALID_PARAMETER); SetLastError(ERROR_INVALID_PARAMETER);
return FALSE; return FALSE;
@ -836,6 +840,7 @@ BOOL WINAPI CryptDeriveKey (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData
} }
key->pProvider = prov; key->pProvider = prov;
key->dwMagic = MAGIC_CRYPTKEY;
if (prov->pFuncs->pCPDeriveKey(prov->hPrivate, Algid, hash->hPrivate, dwFlags, &key->hPrivate)) if (prov->pFuncs->pCPDeriveKey(prov->hPrivate, Algid, hash->hPrivate, dwFlags, &key->hPrivate))
{ {
*phKey = (HCRYPTKEY)key; *phKey = (HCRYPTKEY)key;
@ -843,6 +848,7 @@ BOOL WINAPI CryptDeriveKey (HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData
} }
/* CSP error! */ /* CSP error! */
key->dwMagic = 0;
CRYPT_Free(key); CRYPT_Free(key);
*phKey = 0; *phKey = 0;
return FALSE; return FALSE;
@ -874,7 +880,8 @@ BOOL WINAPI CryptDestroyHash (HCRYPTHASH hHash)
return FALSE; return FALSE;
} }
if (!hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV) if (!hash->pProvider || hash->dwMagic != MAGIC_CRYPTHASH ||
hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
{ {
SetLastError(ERROR_INVALID_PARAMETER); SetLastError(ERROR_INVALID_PARAMETER);
return FALSE; return FALSE;
@ -882,6 +889,7 @@ BOOL WINAPI CryptDestroyHash (HCRYPTHASH hHash)
prov = hash->pProvider; prov = hash->pProvider;
ret = prov->pFuncs->pCPDestroyHash(prov->hPrivate, hash->hPrivate); ret = prov->pFuncs->pCPDestroyHash(prov->hPrivate, hash->hPrivate);
hash->dwMagic = 0;
CRYPT_Free(hash); CRYPT_Free(hash);
return ret; return ret;
} }
@ -912,7 +920,8 @@ BOOL WINAPI CryptDestroyKey (HCRYPTKEY hKey)
return FALSE; return FALSE;
} }
if (!key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV) if (!key->pProvider || key->dwMagic != MAGIC_CRYPTKEY ||
key->pProvider->dwMagic != MAGIC_CRYPTPROV)
{ {
SetLastError(ERROR_INVALID_PARAMETER); SetLastError(ERROR_INVALID_PARAMETER);
return FALSE; return FALSE;
@ -920,6 +929,7 @@ BOOL WINAPI CryptDestroyKey (HCRYPTKEY hKey)
prov = key->pProvider; prov = key->pProvider;
ret = prov->pFuncs->pCPDestroyKey(prov->hPrivate, key->hPrivate); ret = prov->pFuncs->pCPDestroyKey(prov->hPrivate, key->hPrivate);
key->dwMagic = 0;
CRYPT_Free(key); CRYPT_Free(key);
return ret; return ret;
} }
@ -949,7 +959,7 @@ BOOL WINAPI CryptDuplicateHash (HCRYPTHASH hHash, DWORD *pdwReserved,
orghash = (PCRYPTHASH)hHash; orghash = (PCRYPTHASH)hHash;
if (!orghash || pdwReserved || !phHash || !orghash->pProvider || if (!orghash || pdwReserved || !phHash || !orghash->pProvider ||
orghash->pProvider->dwMagic != MAGIC_CRYPTPROV) orghash->dwMagic != MAGIC_CRYPTHASH || orghash->pProvider->dwMagic != MAGIC_CRYPTPROV)
{ {
SetLastError(ERROR_INVALID_PARAMETER); SetLastError(ERROR_INVALID_PARAMETER);
return FALSE; return FALSE;
@ -969,11 +979,13 @@ BOOL WINAPI CryptDuplicateHash (HCRYPTHASH hHash, DWORD *pdwReserved,
} }
newhash->pProvider = prov; newhash->pProvider = prov;
newhash->dwMagic = MAGIC_CRYPTHASH;
if (prov->pFuncs->pCPDuplicateHash(prov->hPrivate, orghash->hPrivate, pdwReserved, dwFlags, &newhash->hPrivate)) if (prov->pFuncs->pCPDuplicateHash(prov->hPrivate, orghash->hPrivate, pdwReserved, dwFlags, &newhash->hPrivate))
{ {
*phHash = (HCRYPTHASH)newhash; *phHash = (HCRYPTHASH)newhash;
return TRUE; return TRUE;
} }
newhash->dwMagic = 0;
CRYPT_Free(newhash); CRYPT_Free(newhash);
return FALSE; return FALSE;
} }
@ -1002,6 +1014,7 @@ BOOL WINAPI CryptDuplicateKey (HCRYPTKEY hKey, DWORD *pdwReserved, DWORD dwFlags
orgkey = (PCRYPTKEY)hKey; orgkey = (PCRYPTKEY)hKey;
if (!orgkey || pdwReserved || !phKey || !orgkey->pProvider || if (!orgkey || pdwReserved || !phKey || !orgkey->pProvider ||
orgkey->dwMagic != MAGIC_CRYPTKEY ||
orgkey->pProvider->dwMagic != MAGIC_CRYPTPROV) orgkey->pProvider->dwMagic != MAGIC_CRYPTPROV)
{ {
SetLastError(ERROR_INVALID_PARAMETER); SetLastError(ERROR_INVALID_PARAMETER);
@ -1022,11 +1035,13 @@ BOOL WINAPI CryptDuplicateKey (HCRYPTKEY hKey, DWORD *pdwReserved, DWORD dwFlags
} }
newkey->pProvider = prov; newkey->pProvider = prov;
newkey->dwMagic = MAGIC_CRYPTKEY;
if (prov->pFuncs->pCPDuplicateKey(prov->hPrivate, orgkey->hPrivate, pdwReserved, dwFlags, &newkey->hPrivate)) if (prov->pFuncs->pCPDuplicateKey(prov->hPrivate, orgkey->hPrivate, pdwReserved, dwFlags, &newkey->hPrivate))
{ {
*phKey = (HCRYPTKEY)newkey; *phKey = (HCRYPTKEY)newkey;
return TRUE; return TRUE;
} }
newkey->dwMagic = 0;
CRYPT_Free(newkey); CRYPT_Free(newkey);
return FALSE; return FALSE;
} }
@ -1063,7 +1078,8 @@ BOOL WINAPI CryptEncrypt (HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final,
TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p, %d)\n", hKey, hHash, Final, dwFlags, pbData, pdwDataLen, dwBufLen); TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p, %d)\n", hKey, hHash, Final, dwFlags, pbData, pdwDataLen, dwBufLen);
if (!key || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV) if (!key || !pdwDataLen || !key->pProvider ||
key->dwMagic != MAGIC_CRYPTKEY || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
{ {
SetLastError(ERROR_INVALID_PARAMETER); SetLastError(ERROR_INVALID_PARAMETER);
return FALSE; return FALSE;
@ -1370,7 +1386,8 @@ BOOL WINAPI CryptExportKey (HCRYPTKEY hKey, HCRYPTKEY hExpKey, DWORD dwBlobType,
TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p)\n", hKey, hExpKey, dwBlobType, dwFlags, pbData, pdwDataLen); TRACE("(0x%lx, 0x%lx, %d, %08x, %p, %p)\n", hKey, hExpKey, dwBlobType, dwFlags, pbData, pdwDataLen);
if (!key || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV) if (!key || !pdwDataLen || !key->pProvider ||
key->dwMagic != MAGIC_CRYPTKEY || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
{ {
SetLastError(ERROR_INVALID_PARAMETER); SetLastError(ERROR_INVALID_PARAMETER);
return FALSE; return FALSE;
@ -1420,7 +1437,7 @@ BOOL WINAPI CryptGenKey (HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, HCRYPTKE
} }
key->pProvider = prov; key->pProvider = prov;
key->dwMagic = MAGIC_CRYPTKEY;
if (prov->pFuncs->pCPGenKey(prov->hPrivate, Algid, dwFlags, &key->hPrivate)) if (prov->pFuncs->pCPGenKey(prov->hPrivate, Algid, dwFlags, &key->hPrivate))
{ {
*phKey = (HCRYPTKEY)key; *phKey = (HCRYPTKEY)key;
@ -1428,6 +1445,7 @@ BOOL WINAPI CryptGenKey (HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, HCRYPTKE
} }
/* CSP error! */ /* CSP error! */
key->dwMagic = 0;
CRYPT_Free(key); CRYPT_Free(key);
return FALSE; return FALSE;
} }
@ -1566,7 +1584,8 @@ BOOL WINAPI CryptGetHashParam (HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData,
TRACE("(0x%lx, %d, %p, %p, %08x)\n", hHash, dwParam, pbData, pdwDataLen, dwFlags); TRACE("(0x%lx, %d, %p, %p, %08x)\n", hHash, dwParam, pbData, pdwDataLen, dwFlags);
if (!hash || !pdwDataLen || !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV) if (!hash || !pdwDataLen || !hash->pProvider ||
hash->dwMagic != MAGIC_CRYPTHASH || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
{ {
SetLastError(ERROR_INVALID_PARAMETER); SetLastError(ERROR_INVALID_PARAMETER);
return FALSE; return FALSE;
@ -1604,7 +1623,8 @@ BOOL WINAPI CryptGetKeyParam (HCRYPTKEY hKey, DWORD dwParam, BYTE *pbData,
TRACE("(0x%lx, %d, %p, %p, %08x)\n", hKey, dwParam, pbData, pdwDataLen, dwFlags); TRACE("(0x%lx, %d, %p, %p, %08x)\n", hKey, dwParam, pbData, pdwDataLen, dwFlags);
if (!key || !pdwDataLen || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV) if (!key || !pdwDataLen || !key->pProvider ||
key->dwMagic != MAGIC_CRYPTKEY || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
{ {
SetLastError(ERROR_INVALID_PARAMETER); SetLastError(ERROR_INVALID_PARAMETER);
return FALSE; return FALSE;
@ -1688,7 +1708,7 @@ BOOL WINAPI CryptGetUserKey (HCRYPTPROV hProv, DWORD dwKeySpec, HCRYPTKEY *phUse
} }
key->pProvider = prov; key->pProvider = prov;
key->dwMagic = MAGIC_CRYPTKEY;
if (prov->pFuncs->pCPGetUserKey(prov->hPrivate, dwKeySpec, &key->hPrivate)) if (prov->pFuncs->pCPGetUserKey(prov->hPrivate, dwKeySpec, &key->hPrivate))
{ {
*phUserKey = (HCRYPTKEY)key; *phUserKey = (HCRYPTKEY)key;
@ -1696,6 +1716,7 @@ BOOL WINAPI CryptGetUserKey (HCRYPTPROV hProv, DWORD dwKeySpec, HCRYPTKEY *phUse
} }
/* CSP Error */ /* CSP Error */
key->dwMagic = 0;
CRYPT_Free(key); CRYPT_Free(key);
*phUserKey = 0; *phUserKey = 0;
return FALSE; return FALSE;
@ -1728,7 +1749,8 @@ BOOL WINAPI CryptHashData (HCRYPTHASH hHash, const BYTE *pbData, DWORD dwDataLen
SetLastError(ERROR_INVALID_HANDLE); SetLastError(ERROR_INVALID_HANDLE);
return FALSE; return FALSE;
} }
if (!hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV) if (!hash->pProvider || hash->dwMagic != MAGIC_CRYPTHASH ||
hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
{ {
SetLastError(ERROR_INVALID_PARAMETER); SetLastError(ERROR_INVALID_PARAMETER);
return FALSE; return FALSE;
@ -1766,7 +1788,8 @@ BOOL WINAPI CryptHashSessionKey (HCRYPTHASH hHash, HCRYPTKEY hKey, DWORD dwFlags
return FALSE; return FALSE;
} }
if (!hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV) if (!hash->pProvider || hash->dwMagic != MAGIC_CRYPTHASH ||
hash->pProvider->dwMagic != MAGIC_CRYPTPROV || key->dwMagic != MAGIC_CRYPTKEY)
{ {
SetLastError(ERROR_INVALID_PARAMETER); SetLastError(ERROR_INVALID_PARAMETER);
return FALSE; return FALSE;
@ -1801,7 +1824,9 @@ BOOL WINAPI CryptImportKey (HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLe
TRACE("(0x%lx, %p, %d, 0x%lx, %08x, %p)\n", hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey); TRACE("(0x%lx, %p, %d, 0x%lx, %08x, %p)\n", hProv, pbData, dwDataLen, hPubKey, dwFlags, phKey);
if (!prov || !pbData || !dwDataLen || !phKey || prov->dwMagic != MAGIC_CRYPTPROV) if (!prov || !pbData || !dwDataLen || !phKey ||
prov->dwMagic != MAGIC_CRYPTPROV ||
(pubkey && pubkey->dwMagic != MAGIC_CRYPTKEY))
{ {
SetLastError(ERROR_INVALID_PARAMETER); SetLastError(ERROR_INVALID_PARAMETER);
return FALSE; return FALSE;
@ -1814,6 +1839,7 @@ BOOL WINAPI CryptImportKey (HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLe
} }
importkey->pProvider = prov; importkey->pProvider = prov;
importkey->dwMagic = MAGIC_CRYPTKEY;
if (prov->pFuncs->pCPImportKey(prov->hPrivate, pbData, dwDataLen, if (prov->pFuncs->pCPImportKey(prov->hPrivate, pbData, dwDataLen,
pubkey ? pubkey->hPrivate : 0, dwFlags, &importkey->hPrivate)) pubkey ? pubkey->hPrivate : 0, dwFlags, &importkey->hPrivate))
{ {
@ -1821,6 +1847,7 @@ BOOL WINAPI CryptImportKey (HCRYPTPROV hProv, CONST BYTE *pbData, DWORD dwDataLe
return TRUE; return TRUE;
} }
importkey->dwMagic = 0;
CRYPT_Free(importkey); CRYPT_Free(importkey);
return FALSE; return FALSE;
} }
@ -1861,7 +1888,8 @@ BOOL WINAPI CryptSignHashW (HCRYPTHASH hHash, DWORD dwKeySpec, LPCWSTR sDescript
SetLastError(ERROR_INVALID_HANDLE); SetLastError(ERROR_INVALID_HANDLE);
return FALSE; return FALSE;
} }
if (!pdwSigLen || !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV) if (!pdwSigLen || !hash->pProvider || hash->dwMagic != MAGIC_CRYPTHASH ||
hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
{ {
SetLastError(ERROR_INVALID_PARAMETER); SetLastError(ERROR_INVALID_PARAMETER);
return FALSE; return FALSE;
@ -1915,7 +1943,8 @@ BOOL WINAPI CryptSetHashParam (HCRYPTHASH hHash, DWORD dwParam, CONST BYTE *pbDa
TRACE("(0x%lx, %d, %p, %08x)\n", hHash, dwParam, pbData, dwFlags); TRACE("(0x%lx, %d, %p, %08x)\n", hHash, dwParam, pbData, dwFlags);
if (!hash || !pbData || !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV) if (!hash || !pbData || !hash->pProvider ||
hash->dwMagic != MAGIC_CRYPTHASH || hash->pProvider->dwMagic != MAGIC_CRYPTPROV)
{ {
SetLastError(ERROR_INVALID_PARAMETER); SetLastError(ERROR_INVALID_PARAMETER);
return FALSE; return FALSE;
@ -1948,7 +1977,8 @@ BOOL WINAPI CryptSetKeyParam (HCRYPTKEY hKey, DWORD dwParam, CONST BYTE *pbData,
TRACE("(0x%lx, %d, %p, %08x)\n", hKey, dwParam, pbData, dwFlags); TRACE("(0x%lx, %d, %p, %08x)\n", hKey, dwParam, pbData, dwFlags);
if (!key || !pbData || !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV) if (!key || !pbData || !key->pProvider ||
key->dwMagic != MAGIC_CRYPTKEY || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
{ {
SetLastError(ERROR_INVALID_PARAMETER); SetLastError(ERROR_INVALID_PARAMETER);
return FALSE; return FALSE;
@ -2184,7 +2214,7 @@ BOOL WINAPI CryptVerifySignatureW (HCRYPTHASH hHash, CONST BYTE *pbSignature, DW
TRACE("(0x%lx, %p, %d, 0x%lx, %s, %08x)\n", hHash, pbSignature, TRACE("(0x%lx, %p, %d, 0x%lx, %s, %08x)\n", hHash, pbSignature,
dwSigLen, hPubKey, debugstr_w(sDescription), dwFlags); dwSigLen, hPubKey, debugstr_w(sDescription), dwFlags);
if (!hash || !key || if (!hash || !key || key->dwMagic != MAGIC_CRYPTKEY || hash->dwMagic != MAGIC_CRYPTHASH ||
!hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV || !hash->pProvider || hash->pProvider->dwMagic != MAGIC_CRYPTPROV ||
!key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV) !key->pProvider || key->pProvider->dwMagic != MAGIC_CRYPTPROV)
{ {

View File

@ -57,6 +57,8 @@ typedef struct tagPROVFUNCS
} PROVFUNCS, *PPROVFUNCS; } PROVFUNCS, *PPROVFUNCS;
#define MAGIC_CRYPTPROV 0xA39E741F #define MAGIC_CRYPTPROV 0xA39E741F
#define MAGIC_CRYPTKEY 0xA39E741E
#define MAGIC_CRYPTHASH 0xA39E741D
typedef struct tagCRYPTPROV typedef struct tagCRYPTPROV
{ {
@ -70,12 +72,14 @@ typedef struct tagCRYPTPROV
typedef struct tagCRYPTKEY typedef struct tagCRYPTKEY
{ {
DWORD dwMagic;
PCRYPTPROV pProvider; PCRYPTPROV pProvider;
HCRYPTKEY hPrivate; /*CSP's handle - Should not be given to application under any circumstances!*/ HCRYPTKEY hPrivate; /*CSP's handle - Should not be given to application under any circumstances!*/
} CRYPTKEY, *PCRYPTKEY; } CRYPTKEY, *PCRYPTKEY;
typedef struct tagCRYPTHASH typedef struct tagCRYPTHASH
{ {
DWORD dwMagic;
PCRYPTPROV pProvider; PCRYPTPROV pProvider;
HCRYPTHASH hPrivate; /*CSP's handle - Should not be given to application under any circumstances!*/ HCRYPTHASH hPrivate; /*CSP's handle - Should not be given to application under any circumstances!*/
} CRYPTHASH, *PCRYPTHASH; } CRYPTHASH, *PCRYPTHASH;