?? handlecontainer.cpp
字號:
% Parameters of entry :
% IN hKey - Handle on a key of session used for coding
% IN hHash - Handle on a hash object if one wants to carry out a hash data before coding
% IN Final - Boolean allowing to know if it is the last part to be ciphered
% IN dwFlags - not used
% IN\OUT pbData - data to be ciphered
% IN\OUT pdwDataLen - length of data to be ciphered
% IN dwBufLen - length of the data pbData in bytes
%
% return : TRUE if the operation occurred well, FALSE if not
%---------------------------------------------------------------------------
*/
BOOL HandleContainer::Encrypt(HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final, DWORD dwFlags, BYTE* pbData, DWORD*pdwDataLen, DWORD dwBufLen)
{
TRACE(__LINE__,"HandleContainer::Encrypt ",NULL);
return CryptEncrypt(hKey, hHash, Final, dwFlags, pbData, pdwDataLen, dwBufLen);
}
/*
%--------------------------------------------------------------------------
% Decrypt
%
% Decrypt is used to decipher data: managed by Provider Microsoft
%
% Parameters of entry :
% IN hKey - Handle on a key of session used for the deciphering
% IN hHash - Handle on a hash object if one wants to carry out a hash data after the deciphering
% IN Final - Boolean allowing to know if it is the last part to be deciphered
% IN dwFlags - not used
% IN\OUT pbData -data to be deciphered\data deciphered
% IN\OUT pdwDataLen -length of data to be deciphered\data deciphered
%
% return : TRUE if the operation occurred well, FALSE if not
%---------------------------------------------------------------------------
*/
BOOL HandleContainer::Decrypt(HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final, DWORD dwFlags, BYTE*pbData, DWORD*pdwDataLen)
{
TRACE(__LINE__,"HandleContainer::Decrypt ",NULL);
return CryptDecrypt(hKey, hHash, Final, dwFlags, pbData, pdwDataLen);
}
/*
%--------------------------------------------------------------------------
% SignHash
%
% SignHash is used to carry out the signature of one starting from a handle on a hash object.
%
% Parameters of entry :
% IN hHash - Handle on the hash object to be signed
% IN dwKeySpec -type of key to use to sign(AT_KEYEXCHANGE\AT_SIGNATURE)
% IN szDescription - not used
% IN dwFlags - not used
% OUT pbSignature - pointer on the signed data
% OUT pdwSigLen - address of a DWORD containing the length of the signed data
%
% return : TRUE if the operation occurred well, FALSE if not
%---------------------------------------------------------------------------
*/
BOOL HandleContainer::SignHash(HCRYPTHASH hHash, DWORD dwKeySpec, LPCWSTR sDescription, DWORD dwFlags, BYTE*pbSignature, DWORD* pdwSigLen)
{
TRACE(__LINE__,"HandleContainer::SignHash BEGIN ",NULL);
ALG_ID algidHash;
unsigned long dwBufferLen;
LPBYTE pbyHash = NULL;
unsigned long dwHashLen;
const BYTE * pbyOID = NULL;
unsigned long dwOIDLen = 0;
BOOL bRet = FALSE;
dwBufferLen = sizeof(algidHash);
if(!CryptGetHashParam(hHash, HP_ALGID, (LPBYTE)&algidHash, &dwBufferLen, 0))
{
TRACE(__LINE__,"HandleContainer::SignHash FALSE ",NULL);
SetLastError(NTE_BAD_HASH);
return FALSE;
}
dwBufferLen = sizeof(dwHashLen);
if(!CryptGetHashParam(hHash, HP_HASHSIZE, (LPBYTE)&dwHashLen, &dwBufferLen, 0))
{
TRACE(__LINE__,"HandleContainer::SignHash FALSE ",NULL);
SetLastError(NTE_BAD_HASH);
return FALSE;
}
switch(algidHash)
{
case CALG_SSL3_SHAMD5:
dwOIDLen = 0;
break;
case CALG_MD2:
pbyOID = &derEncodedMD2[0];
dwOIDLen = sizeof(derEncodedMD2);
break;
case CALG_MD5:
pbyOID = &derEncodedMD5[0];
dwOIDLen = sizeof(derEncodedMD5);
break;
case CALG_SHA1:
pbyOID = &derEncodedSHA1[0];
dwOIDLen = sizeof(derEncodedSHA1);
break;
default:
SetLastError(NTE_BAD_ALGID);
return FALSE;
}
pbyHash = new BYTE[dwHashLen + dwOIDLen];
if(!pbyHash)
{
TRACE(__LINE__,"HandleContainer::SignHash FALSE ",NULL);
SetLastError(NTE_FAIL);
return FALSE;
}
if(pbyOID)
memcpy(pbyHash, pbyOID, dwOIDLen);
if(!CryptGetHashParam(hHash, HP_HASHVAL, pbyHash+dwOIDLen, &dwHashLen, 0))
{
TRACE(__LINE__,"HandleContainer::SignHash FALSE ",NULL);
SetLastError(NTE_BAD_HASH);
if(pbyHash)
free(pbyHash);
return FALSE;
}
if(!Pkcs::DoSign(currentPContainer,dwHashLen + dwOIDLen, pbyHash, dwKeySpec,pbSignature,pdwSigLen))
{
TRACE(__LINE__,"HandleContainer::SignHash FALSE ",NULL);
SetLastError(NTE_FAIL);
if(pbyHash)
free(pbyHash);
return FALSE;
}
TRACE(__LINE__,"HandleContainer::SignHash TRUE ",NULL);
return TRUE;
}
/*
%--------------------------------------------------------------------------
% VerifySignature
%
% VerifySignature is used to check a signature: managed by Provider Microsoft
%
% Parameters of entry :
% IN hHash - Handle on a hash object to be checked
% IN pbSignature - buffer containing the signature
% IN dwSigLen - length in bytes of the signature
% IN hPubKey - Handle on the public key with used to check the signature
% IN sDescription - not used
% IN dwFlags - not used
%
% return : TRUE if the operation occurred well, FALSE if not
%---------------------------------------------------------------------------
*/
BOOL HandleContainer::VerifySignature(HCRYPTHASH hHash, CONST BYTE* pbSignature, DWORD dwSigLen, HCRYPTKEY hPubKey,LPCTSTR sDescription, DWORD dwFlags)
{
TRACE(__LINE__,"HandleContainer::VerifySignature ",NULL);
return CryptVerifySignature(hHash, pbSignature, dwSigLen, hPubKey,sDescription, dwFlags);
}
/*
%--------------------------------------------------------------------------
% DestroyKey
%
% The DestroyKey function releases the handle referred by the parameter hKey: managed by Provider Microsoft
%
% Parameters of entry :
% IN phKey - Handle on the key
%
% return : TRUE if the operation occurred well, FALSE if not
%---------------------------------------------------------------------------
*/
BOOL HandleContainer::DestroyKey(HCRYPTKEY hKey)
{
TRACE(__LINE__,"HandleContainer::DestroyKey ",NULL);
return CryptDestroyKey(hKey);
}
/*
%--------------------------------------------------------------------------
% ImportKey
%
% ImportKey import keys of a "Keyblob" towards a container of key
%
% Parameters of entry :
% IN pbData - data of "Keyblob"
% IN dwDataLen - length of data
% IN hPubKey - Handle on a key public of exchange of the user recipient
% IN dwFlags - Flags values
%
% Parameters of exit :
% OUT phKey - Address to which the function copies the handle key to be imported
%
% return : TRUE if the operation occurred well, FALSE if not
%---------------------------------------------------------------------------
*/
BOOL HandleContainer::ImportKey(BYTE* pbData, DWORD dwDataLen, HCRYPTKEY hPubKey, DWORD dwFlags, HCRYPTKEY* phKey)
{
DWORD i,j;
HCRYPTPROV hProv_m = 0;
HCRYPTKEY hPubPrivKey = 0;
BOOL fResult;
TRACE(__LINE__,"HandleContainer::ImportKey BEGIN ",NULL);
if(!pbData)
{
TRACE(__LINE__,"HandleContainer::ImportKey FALSE ",NULL);
SetLastError(NTE_FAIL);
return FALSE;
}
/*If it's a PUBLICKEYBLOB import it into the base provider.*/
pSessionKeyHeader pskh = (pSessionKeyHeader)pbData;
if(PUBLICKEYBLOB == pskh->blobHeader.bType || !hPubKey)
return CryptImportKey(microsoft_Provider, pbData, dwDataLen, hPubKey, dwFlags, phKey);
/*If it's not a SIMPLEBLOB we don't know what to do with it.*/
if(SIMPLEBLOB != pskh->blobHeader.bType)
{
TRACE(__LINE__,"HandleContainer::ImportKey FALSE ",NULL);
SetLastError(NTE_BAD_TYPE);
return FALSE;
}
/*We only know how to deal with RSA keys.*/
if(CALG_RSA_KEYX != pskh->algid)
{
TRACE(__LINE__,"HandleContainer::ImportKey FALSE ",NULL);
SetLastError(NTE_BAD_TYPE);
return FALSE;
}
BOOL bRet = FALSE;
//Wrapped blob size.
DWORD wEncryptedDataLength = (DWORD)(dwDataLen - sizeof(SessionKeyHeader));
BYTE* pKeySource = pbData + sizeof(SessionKeyHeader);
BYTE* pbyDecryptedData = new BYTE[wEncryptedDataLength];
DWORD wDecryptedDataLength = wEncryptedDataLength;
if(!pbyDecryptedData)
{
TRACE(__LINE__,"HandleContainer::ImportKey FALSE ",NULL);
SetLastError(NTE_FAIL);
if(pbyDecryptedData)
delete [] pbyDecryptedData;
return FALSE;
}
/*reversal of the block pKeySource */
for (i=wEncryptedDataLength-1, j=0; i > j; --i, ++j)
{
BYTE bTmp = pKeySource[i];
pKeySource[i] = pKeySource[j];
pKeySource[j] = bTmp;
}
if(!Pkcs::Decrypt(hPubKey, pKeySource, wEncryptedDataLength, pbyDecryptedData, &wDecryptedDataLength))
{
TRACE(__LINE__,"HandleContainer::ImportKey FALSE ",NULL);
SetLastError(NTE_BAD_ALGID);
if(pbyDecryptedData)
delete [] pbyDecryptedData;
return FALSE;
}
LPTSTR MicroProvider;
// Try to create new container
if(pskh->blobHeader.aiKeyAlg==CALG_RC2 && wDecryptedDataLength==5)
MicroProvider="Microsoft Base Cryptographic Provider v1.0";
else
MicroProvider="Microsoft Enhanced Cryptographic Provider v1.0";
fResult =CreatePrivateExponentOneKey(MicroProvider, PROV_RSA_FULL,
"importKey", AT_KEYEXCHANGE,
&hProv_m, &hPubPrivKey);
if(!fResult)
{
TRACE(__LINE__,"HandleContainer::ImportKey FALSE ",NULL);
SetLastError(NTE_FAIL);
if(pbyDecryptedData)
delete [] pbyDecryptedData;
return FALSE;
}
// Import this key and get an HCRYPTKEY handle
if (!ImportPlainSessionBlob(hProv_m, hPubPrivKey,pskh->blobHeader.aiKeyAlg , pbyDecryptedData, wDecryptedDataLength, phKey))
{
TRACE(__LINE__,"HandleContainer::ImportKey FALSE ",NULL);
SetLastError(NTE_FAIL);
if(pbyDecryptedData)
delete [] pbyDecryptedData;
return FALSE;
}
bRet = TRUE;
if(pbyDecryptedData)
delete [] pbyDecryptedData;
TRACE(__LINE__,"HandleContainer::ImportKey TRUE ",NULL);
return bRet;
}
/*
%--------------------------------------------------------------------------
% GetKeyParam
%
% GetKeyParam seeks the data which govern the operations of a key: manage by Provider Microsoft
%
% Parameters of entry :
% IN hKey - Handle on the key
% IN ulParametre - value of parameter
% IN pulDataLen - length of the parameter pucData
% IN ulFlags - values of the flag
%
% Parameters of exit :
% OUT pucData - Address to which the function copies the data corresponding to the ulFlags
%
% return : TRUE if the operation occurred well, FALSE if not
%---------------------------------------------------------------------------
*/
BOOL HandleContainer::GetKeyParam(HCRYPTKEY hKey, DWORD dwParam, BYTE* pbData, DWORD* pdwDataLen, DWORD dwFlags)
{
TRACE(__LINE__,"HandleContainer::GetKeyParam",NULL);
return CryptGetKeyParam(hKey, dwParam, pbData, pdwDataLen, dwFlags);
}
/*
%--------------------------------------------------------------------------
% SetKeyParam
%
% SetKeyParam adapt the operations to the customer requirements of a key:
% manage by Provider Microsoft
%
% Parameters of entry :
% IN hKey - Handle on a key
% IN ulParametre - value of the parameter
% IN pucData - Pointer containing the data which correspond to the value
% of parameter dwParam
% IN ulFlags - Values of the flag (not used)
%
% return : TRUE if the operation occurred well, FALSE if not
%---------------------------------------------------------------------------
*/
BOOL HandleContainer::SetKeyParam(HCRYPTKEY hKey, DWORD dwParam,CONST BYTE*pbData, DWORD dwFlags)
{
TRACE(__LINE__,"HandleContainer::SetKeyParam",NULL);
return CryptSetKeyParam(hKey, dwParam, pbData, dwFlags);
}
/*
%--------------------------------------------------------------------------
% GetUserKey
%
% GetUserKey is used to return a handle on the pair of key of the user
%
% Parameters of entry :
% IN dwKeySpec - desired type of key (AT_KEYEXCHANGE\AT_SIGNATURE)
% OUT phUserKey - address to which is copied the handle desired key
%
% return : TRUE if the operation occurred well, FALSE if not
%---------------------------------------------------------------------------
*/
BOOL HandleContainer::GetUserKey(DWORD dwKeySpec, HCRYPTKEY* phUserKey)
{
TRACE(__LINE__,"HandleContainer::GetUserKey BEGIN",NULL);
if(!(currentPContainer->GetUserKey(dwKeySpec,phUserKey)))
{
TRACE(__LINE__,"HandleContainer::GetUserKey FALSE",NULL);
SetLastError(NTE_NO_KEY);
return FALSE;
}
TRACE(__LINE__,"HandleContainer::GetUserKey TRUE",NULL);
return TRUE;
}
/*
%--------------------------------------------------------------------------
% CreatePrivateExponentOneKey
%
% CreatePrivateExponentOneKey is used to create a key private which carries
% out during coding the identity
%
% Parameters of entry :
% IN szProvider Name of provider
% IN dwProvType
% IN szContainer Name of container
% IN dwKeySpec type of key
% IN hProv handle to the provider
% IN hPrivateKey handle to the private key
%
%
% return : TRUE if the operation occurred well, FALSE if not
%---------------------------------------------------------------------------
*/
BOOL HandleContainer::CreatePrivateExponentOneKey(LPTSTR szProvider,DWORD dwProvType,LPTSTR szContainer,DWORD dwKeySpec,HCRYPTPROV *hProv,HCRYPTKEY *hPrivateKey)
{
BOOL fReturn = FALSE;
BOOL fResult;
int n;
LPBYTE keyblob = NULL;
DWORD dwkeyblob;
DWORD dwBitLen;
BYTE *ptr;
TRACE(__LINE__,"HandleContainer::CreatePrivateExponentOneKey BEGIN",NULL);
__try
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -