Commit 3d01b078 authored by Oleg Korshul's avatar Oleg Korshul

.

parent fecfcfa7
...@@ -170,7 +170,7 @@ public: ...@@ -170,7 +170,7 @@ public:
return sReturn; return sReturn;
} }
std::string GetHash(std::string& sXml) std::string GetHash(BYTE* pData, DWORD dwSize)
{ {
BOOL bResult = TRUE; BOOL bResult = TRUE;
DWORD dwKeySpec = 0; DWORD dwKeySpec = 0;
...@@ -186,7 +186,7 @@ public: ...@@ -186,7 +186,7 @@ public:
if (!bResult) if (!bResult)
return ""; return "";
bResult = CryptHashData(hHash, (BYTE*)sXml.c_str(), (DWORD)sXml.length(), 0); bResult = CryptHashData(hHash, pData, dwSize, 0);
if (!bResult) if (!bResult)
{ {
CryptDestroyHash(hHash); CryptDestroyHash(hHash);
...@@ -206,11 +206,11 @@ public: ...@@ -206,11 +206,11 @@ public:
bResult = CryptGetHashParam(hHash, HP_HASHVAL, pDataHashRaw, &cbHashSize, 0); bResult = CryptGetHashParam(hHash, HP_HASHVAL, pDataHashRaw, &cbHashSize, 0);
delete[] pDataHashRaw;
CryptDestroyHash(hHash);
if (!bResult) if (!bResult)
{
CryptDestroyHash(hHash);
return ""; return "";
}
char* pBase64_hash = NULL; char* pBase64_hash = NULL;
int nBase64Len_hash = 0; int nBase64Len_hash = 0;
...@@ -219,21 +219,19 @@ public: ...@@ -219,21 +219,19 @@ public:
std::string sReturn(pBase64_hash, nBase64Len_hash); std::string sReturn(pBase64_hash, nBase64Len_hash);
delete [] pBase64_hash; delete [] pBase64_hash;
//delete [] pDataHashRaw;
CryptDestroyHash(hHash);
return sReturn; return sReturn;
} }
std::string GetHash(std::wstring& sXmlFile) std::string GetHash(std::string& sXml)
{ {
BOOL bResult = TRUE; return GetHash((BYTE*)sXml.c_str(), (DWORD)sXml.length());
DWORD dwKeySpec = 0; }
HCRYPTHASH hHash = NULL;
if (NULL == m_hCryptProv)
bResult = CryptAcquireCertificatePrivateKey(m_context, 0, NULL, &m_hCryptProv, &dwKeySpec, NULL);
if (!bResult)
return "";
std::string GetHash(std::wstring& sXmlFile)
{
BYTE* pFileData = NULL; BYTE* pFileData = NULL;
DWORD dwFileDataLen = 0; DWORD dwFileDataLen = 0;
NSFile::CFileBinary::ReadAllBytes(sXmlFile, &pFileData, dwFileDataLen); NSFile::CFileBinary::ReadAllBytes(sXmlFile, &pFileData, dwFileDataLen);
...@@ -241,50 +239,9 @@ public: ...@@ -241,50 +239,9 @@ public:
if (0 == dwFileDataLen) if (0 == dwFileDataLen)
return ""; return "";
bResult = CryptCreateHash(m_hCryptProv, CALG_SHA1, 0, 0, &hHash); std::string sReturn = GetHash(pFileData, dwFileDataLen);
if (!bResult)
{
RELEASEARRAYOBJECTS(pFileData);
return "";
}
bResult = CryptHashData(hHash, pFileData, dwFileDataLen, 0);
if (!bResult)
{
RELEASEARRAYOBJECTS(pFileData); RELEASEARRAYOBJECTS(pFileData);
CryptDestroyHash(hHash);
return "";
}
DWORD cbHashSize = 0, dwCount = sizeof(DWORD);
bResult = CryptGetHashParam(hHash, HP_HASHSIZE, (BYTE*)&cbHashSize, &dwCount, 0);
if (!bResult)
{
RELEASEARRAYOBJECTS(pFileData);
CryptDestroyHash(hHash);
return "";
}
RELEASEARRAYOBJECTS(pFileData);
BYTE* pDataHashRaw = new BYTE[dwCount];
bResult = CryptGetHashParam(hHash, HP_HASHVAL, pDataHashRaw, &cbHashSize, 0);
delete[] pDataHashRaw;
CryptDestroyHash(hHash);
if (!bResult)
return "";
char* pBase64_hash = NULL;
int nBase64Len_hash = 0;
NSFile::CBase64Converter::Encode(pDataHashRaw, (int)cbHashSize, pBase64_hash, nBase64Len_hash, NSBase64::B64_BASE64_FLAG_NOCRLF);
std::string sReturn(pBase64_hash, nBase64Len_hash);
delete [] pBase64_hash;
return sReturn; return sReturn;
} }
...@@ -320,7 +277,7 @@ public: ...@@ -320,7 +277,7 @@ public:
bResult = CryptHashData(hHash, (BYTE*)sXml.c_str(), (DWORD)sXml.length(), 0); bResult = CryptHashData(hHash, (BYTE*)sXml.c_str(), (DWORD)sXml.length(), 0);
// Get the public key from the certificate // Get the public key from the certificate
CryptImportPublicKeyInfo(m_hCryptProv, PKCS_7_ASN_ENCODING | X509_ASN_ENCODING, &m_context->pCertInfo->SubjectPublicKeyInfo, &hPubKey); CryptImportPublicKeyInfo(m_hCryptProv, m_context->dwCertEncodingType, &m_context->pCertInfo->SubjectPublicKeyInfo, &hPubKey);
BOOL bResultRet = CryptVerifySignature(hHash, pDataHashMem, dwHashLen, hPubKey, NULL, 0); BOOL bResultRet = CryptVerifySignature(hHash, pDataHashMem, dwHashLen, hPubKey, NULL, 0);
...@@ -333,6 +290,21 @@ public: ...@@ -333,6 +290,21 @@ public:
return bResultRet && bResult; return bResultRet && bResult;
} }
std::string GetCertificateBase64()
{
char* pData = NULL;
int nDataLen = 0;
NSFile::CBase64Converter::Encode(m_context->pbCertEncoded, (int)m_context->cbCertEncoded, pData, nDataLen, NSBase64::B64_BASE64_FLAG_NOCRLF);
std::string sReturn(pData, nDataLen);
RELEASEARRAYOBJECTS(pData);
return sReturn;
}
std::string GetCertificateHash()
{
return GetHash(m_context->pbCertEncoded, (int)m_context->cbCertEncoded);
}
private: private:
void ConvertEndian(const BYTE* src, BYTE* dst, DWORD size) void ConvertEndian(const BYTE* src, BYTE* dst, DWORD size)
{ {
...@@ -657,6 +629,11 @@ void main(void) ...@@ -657,6 +629,11 @@ void main(void)
bool bRes = true; bool bRes = true;
bRes = Sign(pCertContext, NSFile::GetProcessDirectory() + L"/test.xml", NSFile::GetProcessDirectory() + L"/result.txt"); bRes = Sign(pCertContext, NSFile::GetProcessDirectory() + L"/test.xml", NSFile::GetProcessDirectory() + L"/result.txt");
bRes = Verify(pCertContext, NSFile::GetProcessDirectory() + L"/test.xml", NSFile::GetProcessDirectory() + L"/result.txt"); bRes = Verify(pCertContext, NSFile::GetProcessDirectory() + L"/test.xml", NSFile::GetProcessDirectory() + L"/result.txt");
CXmlSigner oSigner(pCertContext);
std::string sCertBase64 = oSigner.GetCertificateBase64();
std::string sCertHash = oSigner.GetCertificateHash();
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CertFreeCertificateContext(pCertContext); CertFreeCertificateContext(pCertContext);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment