mirror of
https://github.com/ONLYOFFICE/core.git
synced 2026-02-10 18:05:41 +08:00
Add not sha1 algorithms to sign/verify providers
This commit is contained in:
@ -8,6 +8,9 @@
|
||||
#define OOXML_HASH_ALG_SHA1 0
|
||||
#define OOXML_HASH_ALG_INVALID 1
|
||||
#define OOXML_HASH_ALG_SHA256 2
|
||||
#define OOXML_HASH_ALG_SHA224 3
|
||||
#define OOXML_HASH_ALG_SHA384 4
|
||||
#define OOXML_HASH_ALG_SHA512 5
|
||||
|
||||
#define OPEN_SSL_WARNING_OK 0
|
||||
#define OPEN_SSL_WARNING_ERR 1
|
||||
@ -103,7 +106,10 @@ public:
|
||||
virtual std::string GetHash(const std::wstring& sXmlFile, int nAlg) = 0;
|
||||
virtual bool Verify(const std::string& sXml, std::string& sXmlSignature, int nAlg) = 0;
|
||||
|
||||
virtual bool LoadFromBase64Data(const std::string& data) = 0;
|
||||
virtual bool LoadFromBase64Data(const std::string& data) = 0;
|
||||
|
||||
virtual std::vector<int> GetHashAlgs() = 0;
|
||||
virtual int GetHashAlg() = 0;
|
||||
|
||||
public:
|
||||
virtual int ShowSelectDialog(void* parent = NULL) = 0;
|
||||
@ -118,7 +124,12 @@ public:
|
||||
|
||||
public:
|
||||
static int GetOOXMLHashAlg(const std::string& sAlg);
|
||||
static ICertificate* CreateInstance();
|
||||
static std::string GetDigestMethodA(const int& nAlg);
|
||||
static std::wstring GetDigestMethod(const int& nAlg);
|
||||
static std::string GetSignatureMethodA(const int& nAlg);
|
||||
static std::wstring GetSignatureMethod(const int& nAlg);
|
||||
|
||||
static ICertificate* CreateInstance();
|
||||
};
|
||||
|
||||
#endif // _XML_SERTIFICATE_BASE_H_
|
||||
|
||||
@ -46,7 +46,9 @@ public:
|
||||
m_date = NSFile::CUtf8Converter::GetUnicodeFromCharPtr(date);
|
||||
|
||||
m_signed_info.WriteString("<CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\"/>");
|
||||
m_signed_info.WriteString("<SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\"/>");
|
||||
m_signed_info.WriteString("<SignatureMethod Algorithm=\"");
|
||||
m_signed_info.WriteString(ICertificate::GetSignatureMethodA(m_certificate->GetHashAlg()));
|
||||
m_signed_info.WriteString("\"/>");
|
||||
}
|
||||
~COOXMLSigner_private()
|
||||
{
|
||||
@ -55,9 +57,9 @@ public:
|
||||
std::wstring GetReference(const std::wstring& file, const std::wstring& content_type)
|
||||
{
|
||||
std::wstring sXml = L"<Reference URI=\"" + file + L"?ContentType=" + content_type + L"\">";
|
||||
sXml += L"<DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\"/>";
|
||||
sXml += (L"<DigestMethod Algorithm=\"" + ICertificate::GetDigestMethod(m_certificate->GetHashAlg()) + L"\"/>");
|
||||
sXml += L"<DigestValue>";
|
||||
std::string sTmp = m_certificate->GetHash(m_sFolder + file, OOXML_HASH_ALG_SHA1);
|
||||
std::string sTmp = m_certificate->GetHash(m_sFolder + file, m_certificate->GetHashAlg());
|
||||
sXml += UTF8_TO_U(sTmp);
|
||||
sXml += L"</DigestValue>";
|
||||
sXml += L"</Reference>";
|
||||
@ -68,7 +70,7 @@ public:
|
||||
{
|
||||
std::string sXmlSigned = U_TO_UTF8(xml);
|
||||
sXmlSigned = CXmlCanonicalizator::Execute(sXmlSigned, XML_C14N_1_0);
|
||||
return m_certificate->GetHash(sXmlSigned, OOXML_HASH_ALG_SHA1);
|
||||
return m_certificate->GetHash(sXmlSigned, m_certificate->GetHashAlg());
|
||||
}
|
||||
|
||||
std::string GetReferenceMain(const std::wstring& xml, const std::wstring& id, const bool& isCannon = true)
|
||||
@ -87,7 +89,7 @@ public:
|
||||
if (isCannon)
|
||||
sRet = "<Transforms><Transform Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\"/></Transforms>";
|
||||
|
||||
sRet += ("<DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\"/><DigestValue>" + sHash + "</DigestValue>");
|
||||
sRet += ("<DigestMethod Algorithm=\"" + ICertificate::GetDigestMethodA(m_certificate->GetHashAlg()) + "\"/><DigestValue>" + sHash + "</DigestValue>");
|
||||
|
||||
return sRet;
|
||||
}
|
||||
@ -144,7 +146,7 @@ public:
|
||||
builder.WriteString(file);
|
||||
builder.WriteString(L"?ContentType=application/vnd.openxmlformats-package.relationships+xml\">");
|
||||
builder.WriteString(oRels.GetTransforms());
|
||||
builder.WriteString(L"<DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\"/><DigestValue>");
|
||||
builder.WriteString(L"<DigestMethod Algorithm=\"" + ICertificate::GetDigestMethod(m_certificate->GetHashAlg()) + L"\"/><DigestValue>");
|
||||
|
||||
std::wstring sXml = oRels.GetXml();
|
||||
std::string sHash = GetHashXml(sXml);
|
||||
@ -489,13 +491,16 @@ Type=\"http://schemas.openxmlformats.org/package/2006/relationships/digital-sign
|
||||
std::string sKeyA = m_certificate->GetNumber();
|
||||
std::wstring sKey = UTF8_TO_U(sKeyA);
|
||||
|
||||
std::string sCertHA = m_certificate->GetCertificateHash();
|
||||
std::wstring sCertHW = UTF8_TO_U(sCertHA);
|
||||
|
||||
std::wstring sXml = (L"<xd:SignedSignatureProperties>\
|
||||
<xd:SigningTime>" + m_date + L"</xd:SigningTime>\
|
||||
<xd:SigningCertificate>\
|
||||
<xd:Cert>\
|
||||
<xd:CertDigest>\
|
||||
<DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\"/>\
|
||||
<DigestValue>MJJT2Y0iMxaPGVXBmOLb9bY60pA=</DigestValue>\
|
||||
<DigestMethod Algorithm=\"" + ICertificate::GetDigestMethod(m_certificate->GetHashAlg()) + L"\"/>\
|
||||
<DigestValue>" + sCertHW + L"</DigestValue>\
|
||||
</xd:CertDigest>\
|
||||
<xd:IssuerSerial>\
|
||||
<X509IssuerName>CN=" + sName + L"</X509IssuerName>\
|
||||
@ -516,8 +521,8 @@ Type=\"http://schemas.openxmlformats.org/package/2006/relationships/digital-sign
|
||||
|
||||
m_signed_info.WriteString("<Reference Type=\"http://uri.etsi.org/01903#SignedProperties\" URI=\"#idSignedProperties\">");
|
||||
m_signed_info.WriteString("<Transforms><Transform Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\"/></Transforms>");
|
||||
m_signed_info.WriteString("<DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\"/><DigestValue>");
|
||||
m_signed_info.WriteString(m_certificate->GetHash(sXmlTmp, OOXML_HASH_ALG_SHA1));
|
||||
m_signed_info.WriteString("<DigestMethod Algorithm=\"" + ICertificate::GetDigestMethodA(m_certificate->GetHashAlg()) + "\"/><DigestValue>");
|
||||
m_signed_info.WriteString(m_certificate->GetHash(sXmlTmp, m_certificate->GetHashAlg()));
|
||||
m_signed_info.WriteString("</DigestValue></Reference>");
|
||||
|
||||
return (L"<Object><xd:QualifyingProperties xmlns:xd=\"http://uri.etsi.org/01903/v1.3.2#\" Target=\"#idPackageSignature\">\
|
||||
|
||||
@ -252,7 +252,9 @@ public:
|
||||
return;
|
||||
}
|
||||
|
||||
// 5) Check signature
|
||||
// 5) Check sert digest! (TODO:)
|
||||
|
||||
// 6) Check signature
|
||||
CXmlStackNamespaces stack(m_node);
|
||||
CXmlStackNamespaces stackRes = stack.GetById("SignedInfo", true);
|
||||
std::string sXml = stackRes.GetXml();
|
||||
|
||||
@ -17,6 +17,8 @@ namespace NSOpenSSL
|
||||
|
||||
#endif
|
||||
|
||||
#include "../../../common/File.h"
|
||||
|
||||
#if defined(_LINUX) && !defined(_MAC)
|
||||
#include "./XmlSigner_openssl.h"
|
||||
#define XML_CERTIFICATE_USE_OPENSSL
|
||||
@ -35,9 +37,78 @@ int ICertificate::GetOOXMLHashAlg(const std::string& sAlg)
|
||||
"http://www.w3.org/2000/09/xmldsig#sha1" == sAlg)
|
||||
return OOXML_HASH_ALG_SHA1;
|
||||
|
||||
/*
|
||||
if ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha224" == sAlg ||
|
||||
"http://www.w3.org/2001/04/xmldsig-more#sha224" == sAlg)
|
||||
return OOXML_HASH_ALG_SHA224;
|
||||
*/
|
||||
|
||||
if ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" == sAlg ||
|
||||
"http://www.w3.org/2001/04/xmldsig-more#sha256" == sAlg ||
|
||||
"http://www.w3.org/2001/04/xmlenc#sha256" == sAlg)
|
||||
return OOXML_HASH_ALG_SHA256;
|
||||
|
||||
if ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha384" == sAlg ||
|
||||
"http://www.w3.org/2001/04/xmldsig-more#sha384" == sAlg)
|
||||
return OOXML_HASH_ALG_SHA384;
|
||||
|
||||
if ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha512" == sAlg ||
|
||||
"http://www.w3.org/2001/04/xmldsig-more#sha512" == sAlg ||
|
||||
"http://www.w3.org/2001/04/xmlenc#sha512" == sAlg)
|
||||
return OOXML_HASH_ALG_SHA512;
|
||||
|
||||
return OOXML_HASH_ALG_INVALID;
|
||||
}
|
||||
|
||||
std::string ICertificate::GetDigestMethodA(const int& nAlg)
|
||||
{
|
||||
switch (nAlg)
|
||||
{
|
||||
case OOXML_HASH_ALG_SHA1:
|
||||
return "http://www.w3.org/2000/09/xmldsig#sha1";
|
||||
case OOXML_HASH_ALG_SHA224:
|
||||
return "http://www.w3.org/2001/04/xmldsig-more#sha224";
|
||||
case OOXML_HASH_ALG_SHA256:
|
||||
return "http://www.w3.org/2001/04/xmlenc#sha256";
|
||||
case OOXML_HASH_ALG_SHA384:
|
||||
return "http://www.w3.org/2001/04/xmldsig-more#sha384";
|
||||
case OOXML_HASH_ALG_SHA512:
|
||||
return "http://www.w3.org/2001/04/xmlenc#sha512";
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return "http://www.w3.org/2000/09/xmldsig#sha1";
|
||||
}
|
||||
std::wstring ICertificate::GetDigestMethod(const int& nAlg)
|
||||
{
|
||||
std::string sTmp = GetDigestMethodA(nAlg);
|
||||
return NSFile::CUtf8Converter::GetUnicodeFromCharPtr(sTmp);
|
||||
}
|
||||
std::string ICertificate::GetSignatureMethodA(const int& nAlg)
|
||||
{
|
||||
switch (nAlg)
|
||||
{
|
||||
case OOXML_HASH_ALG_SHA1:
|
||||
return "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
|
||||
case OOXML_HASH_ALG_SHA224:
|
||||
return "http://www.w3.org/2001/04/xmldsig-more#rsa-sha224";
|
||||
case OOXML_HASH_ALG_SHA256:
|
||||
return "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256";
|
||||
case OOXML_HASH_ALG_SHA384:
|
||||
return "http://www.w3.org/2001/04/xmldsig-more#rsa-sha384";
|
||||
case OOXML_HASH_ALG_SHA512:
|
||||
return "http://www.w3.org/2001/04/xmldsig-more#rsa-sha512";
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return "http://www.w3.org/2000/09/xmldsig#rsa-sha1";
|
||||
}
|
||||
std::wstring ICertificate::GetSignatureMethod(const int& nAlg)
|
||||
{
|
||||
std::string sTmp = GetSignatureMethodA(nAlg);
|
||||
return NSFile::CUtf8Converter::GetUnicodeFromCharPtr(sTmp);
|
||||
}
|
||||
|
||||
ICertificate* ICertificate::CreateInstance()
|
||||
{
|
||||
ICertificate* pCert = new CCertificate();
|
||||
|
||||
@ -22,6 +22,8 @@ protected:
|
||||
BYTE* m_rawData;
|
||||
int m_rawDataLen;
|
||||
|
||||
int m_alg;
|
||||
|
||||
public:
|
||||
CCertificate_mscrypto() : ICertificate()
|
||||
{
|
||||
@ -32,6 +34,7 @@ public:
|
||||
m_rawDataLen = 0;
|
||||
|
||||
m_release = false;
|
||||
m_alg = OOXML_HASH_ALG_INVALID;
|
||||
}
|
||||
CCertificate_mscrypto(PCCERT_CONTEXT ctx) : ICertificate()
|
||||
{
|
||||
@ -42,6 +45,8 @@ public:
|
||||
m_rawDataLen = 0;
|
||||
|
||||
m_release = false;
|
||||
|
||||
GetHashAlgs();
|
||||
}
|
||||
|
||||
virtual ~CCertificate_mscrypto()
|
||||
@ -101,7 +106,7 @@ public:
|
||||
|
||||
virtual std::string GetCertificateHash()
|
||||
{
|
||||
return GetHash(m_context->pbCertEncoded, (unsigned int)m_context->cbCertEncoded, OOXML_HASH_ALG_SHA1);
|
||||
return GetHash(m_context->pbCertEncoded, (unsigned int)m_context->cbCertEncoded, GetHashAlg());
|
||||
}
|
||||
|
||||
virtual std::string GetDate()
|
||||
@ -136,7 +141,62 @@ public:
|
||||
return OPEN_SSL_WARNING_OK;
|
||||
}
|
||||
|
||||
std::vector<int> GetHashAlgs()
|
||||
{
|
||||
std::vector<int> algs;
|
||||
if (!m_context || !m_context->pCertInfo)
|
||||
return algs;
|
||||
|
||||
std::string sAlg(m_context->pCertInfo->SignatureAlgorithm.pszObjId);
|
||||
|
||||
if ("1.2.840.113549.1.1.5" == sAlg)
|
||||
algs.push_back(OOXML_HASH_ALG_SHA1);
|
||||
else if ("1.2.840.113549.1.1.11" == sAlg)
|
||||
algs.push_back(OOXML_HASH_ALG_SHA256);
|
||||
else if ("1.2.840.113549.1.1.12" == sAlg)
|
||||
algs.push_back(OOXML_HASH_ALG_SHA384);
|
||||
else if ("1.2.840.113549.1.1.13" == sAlg)
|
||||
algs.push_back(OOXML_HASH_ALG_SHA512);
|
||||
else
|
||||
algs.push_back(OOXML_HASH_ALG_SHA1);
|
||||
|
||||
if (algs.empty())
|
||||
m_alg = OOXML_HASH_ALG_SHA1;
|
||||
else
|
||||
m_alg = algs[0];
|
||||
|
||||
return algs;
|
||||
}
|
||||
int GetHashAlg()
|
||||
{
|
||||
if (m_alg == OOXML_HASH_ALG_INVALID)
|
||||
GetHashAlgs();
|
||||
return m_alg;
|
||||
}
|
||||
|
||||
public:
|
||||
PCRYPT_KEY_PROV_INFO GetProviderInfo()
|
||||
{
|
||||
LPBYTE pInfoData = NULL;
|
||||
DWORD dwInfoDataLength = 0;
|
||||
|
||||
if (!CertGetCertificateContextProperty(m_context, CERT_KEY_PROV_INFO_PROP_ID, NULL, &dwInfoDataLength))
|
||||
return NULL;
|
||||
|
||||
if (dwInfoDataLength > 0)
|
||||
{
|
||||
pInfoData = (LPBYTE)malloc(dwInfoDataLength * sizeof(BYTE));
|
||||
|
||||
if (!CertGetCertificateContextProperty(m_context, CERT_KEY_PROV_INFO_PROP_ID, pInfoData, &dwInfoDataLength))
|
||||
{
|
||||
free(pInfoData);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return (PCRYPT_KEY_PROV_INFO)pInfoData;
|
||||
}
|
||||
|
||||
virtual std::string Sign(const std::string& sXml)
|
||||
{
|
||||
BOOL bResult = TRUE;
|
||||
@ -144,16 +204,46 @@ public:
|
||||
HCRYPTHASH hHash = NULL;
|
||||
|
||||
HCRYPTPROV hCryptProv = NULL;
|
||||
bResult = CryptAcquireCertificatePrivateKey(m_context, 0, NULL, &hCryptProv, &dwKeySpec, NULL);
|
||||
bResult = CryptAcquireCertificatePrivateKey(m_context, CRYPT_ACQUIRE_COMPARE_KEY_FLAG, NULL, &hCryptProv, &dwKeySpec, NULL);
|
||||
|
||||
if (!bResult)
|
||||
return "";
|
||||
|
||||
bResult = CryptCreateHash(hCryptProv, CALG_SHA1, 0, 0, &hHash);
|
||||
int nAlg = GetHashAlg();
|
||||
bResult = CryptCreateHash(hCryptProv, GetHashId(nAlg), 0, 0, &hHash);
|
||||
if (!bResult)
|
||||
{
|
||||
PCRYPT_KEY_PROV_INFO info = GetProviderInfo();
|
||||
|
||||
CryptReleaseContext(hCryptProv, 0);
|
||||
return "";
|
||||
if (!CryptAcquireContextW(&hCryptProv, info->pwszContainerName, info->pwszProvName, info->dwProvType, 0))
|
||||
{
|
||||
CryptReleaseContext(hCryptProv, 0);
|
||||
free(info);
|
||||
return "";
|
||||
}
|
||||
|
||||
bResult = CryptCreateHash(hCryptProv, GetHashId(nAlg), 0, 0, &hHash);
|
||||
if (!bResult)
|
||||
{
|
||||
CryptReleaseContext(hCryptProv, 0);
|
||||
if (!CryptAcquireContextW(&hCryptProv, info->pwszContainerName, NULL, PROV_RSA_AES, 0))
|
||||
{
|
||||
CryptReleaseContext(hCryptProv, 0);
|
||||
free(info);
|
||||
return "";
|
||||
}
|
||||
|
||||
bResult = CryptCreateHash(hCryptProv, GetHashId(nAlg), 0, 0, &hHash);
|
||||
}
|
||||
|
||||
free(info);
|
||||
|
||||
if (!bResult)
|
||||
{
|
||||
CryptReleaseContext(hCryptProv, 0);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
bResult = CryptHashData(hHash, (BYTE*)sXml.c_str(), (DWORD)sXml.length(), 0);
|
||||
@ -223,10 +313,23 @@ public:
|
||||
|
||||
HCRYPTPROV hCryptProv = NULL;
|
||||
|
||||
bResult = (NULL != m_context) ? CryptAcquireCertificatePrivateKey(m_context, 0, NULL, &hCryptProv, &dwKeySpec, NULL) : FALSE;
|
||||
bResult = FALSE;//(NULL != m_context) ? CryptAcquireCertificatePrivateKey(m_context, 0, NULL, &hCryptProv, &dwKeySpec, NULL) : FALSE;
|
||||
|
||||
DWORD dwProvType = PROV_RSA_FULL;
|
||||
switch (nAlg)
|
||||
{
|
||||
case OOXML_HASH_ALG_SHA256:
|
||||
case OOXML_HASH_ALG_SHA512:
|
||||
{
|
||||
dwProvType = PROV_RSA_AES;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (!bResult)
|
||||
bResult = CryptAcquireContext(&hCryptProv, NULL, NULL, (nAlg == OOXML_HASH_ALG_SHA256) ? PROV_RSA_AES : PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
|
||||
bResult = CryptAcquireContext(&hCryptProv, NULL, NULL, dwProvType, CRYPT_VERIFYCONTEXT);
|
||||
|
||||
if (!bResult)
|
||||
return "";
|
||||
@ -308,10 +411,23 @@ public:
|
||||
HCRYPTKEY hPubKey = NULL;
|
||||
|
||||
HCRYPTPROV hCryptProv = NULL;
|
||||
BOOL bResult = CryptAcquireCertificatePrivateKey(m_context, 0, NULL, &hCryptProv, &dwKeySpec, NULL);
|
||||
BOOL bResult = FALSE;//CryptAcquireCertificatePrivateKey(m_context, 0, NULL, &hCryptProv, &dwKeySpec, NULL);
|
||||
|
||||
DWORD dwProvType = PROV_RSA_FULL;
|
||||
switch (nAlg)
|
||||
{
|
||||
case OOXML_HASH_ALG_SHA256:
|
||||
case OOXML_HASH_ALG_SHA512:
|
||||
{
|
||||
dwProvType = PROV_RSA_AES;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (!bResult)
|
||||
bResult = CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
|
||||
bResult = CryptAcquireContext(&hCryptProv, NULL, NULL, dwProvType, CRYPT_VERIFYCONTEXT);
|
||||
|
||||
if (!bResult)
|
||||
return false;
|
||||
@ -365,7 +481,7 @@ public:
|
||||
m_rawDataLen = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
GetHashAlgs();
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -388,6 +504,7 @@ public:
|
||||
m_store = NULL;
|
||||
return 0;
|
||||
}
|
||||
GetHashAlgs();
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -406,6 +523,12 @@ private:
|
||||
return CALG_SHA1;
|
||||
case OOXML_HASH_ALG_SHA256:
|
||||
return CALG_SHA_256;
|
||||
case OOXML_HASH_ALG_SHA384:
|
||||
return CALG_SHA_384;
|
||||
case OOXML_HASH_ALG_SHA512:
|
||||
return CALG_SHA_512;
|
||||
case OOXML_HASH_ALG_SHA224:
|
||||
return CALG_SHA1;
|
||||
default:
|
||||
return CALG_SHA1;
|
||||
}
|
||||
|
||||
@ -17,6 +17,36 @@
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/conf.h>
|
||||
|
||||
const EVP_MD* Get_EVP_MD(int nAlg)
|
||||
{
|
||||
switch (nAlg)
|
||||
{
|
||||
case OOXML_HASH_ALG_SHA1:
|
||||
{
|
||||
return EVP_sha1();
|
||||
}
|
||||
case OOXML_HASH_ALG_SHA224:
|
||||
{
|
||||
return EVP_sha224();
|
||||
}
|
||||
case OOXML_HASH_ALG_SHA256:
|
||||
{
|
||||
return EVP_sha256();
|
||||
}
|
||||
case OOXML_HASH_ALG_SHA384:
|
||||
{
|
||||
return EVP_sha384();
|
||||
}
|
||||
case OOXML_HASH_ALG_SHA512:
|
||||
{
|
||||
return EVP_sha512();
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return EVP_sha1();
|
||||
}
|
||||
|
||||
void BIO_FREE(BIO*& bio)
|
||||
{
|
||||
if (bio)
|
||||
@ -50,6 +80,8 @@ class CCertificate_openssl_private
|
||||
std::string m_separator;
|
||||
std::string m_id;
|
||||
|
||||
int m_alg;
|
||||
|
||||
public:
|
||||
ICertificate* m_pBase;
|
||||
|
||||
@ -169,7 +201,7 @@ public:
|
||||
int nLen = 0;
|
||||
if (NSFile::CBase64Converter::Decode(sBase64.c_str(), (int)sBase64.length(), pData, nLen))
|
||||
{
|
||||
std::string sHash = GetHash(pData, (unsigned int)nLen, OOXML_HASH_ALG_SHA1);
|
||||
std::string sHash = GetHash(pData, (unsigned int)nLen, GetHashAlg());
|
||||
RELEASEARRAYOBJECTS(pData);
|
||||
return sHash;
|
||||
}
|
||||
@ -211,11 +243,11 @@ public:
|
||||
return GetNumber();
|
||||
}
|
||||
|
||||
public:
|
||||
public:
|
||||
std::string Sign(const std::string& sXml)
|
||||
{
|
||||
EVP_MD_CTX* pCtx = EVP_MD_CTX_create();
|
||||
const EVP_MD* pDigest = EVP_sha1();
|
||||
const EVP_MD* pDigest = Get_EVP_MD(this->GetHashAlg());
|
||||
|
||||
int n1 = EVP_SignInit(pCtx, pDigest);
|
||||
n1 = n1;
|
||||
@ -243,35 +275,63 @@ public:
|
||||
|
||||
std::string GetHash(unsigned char* pData, unsigned int nSize, int nAlg)
|
||||
{
|
||||
if (nAlg == OOXML_HASH_ALG_SHA1)
|
||||
int nBufLen = 0;
|
||||
unsigned char* pBufData = NULL;
|
||||
|
||||
switch (nAlg)
|
||||
{
|
||||
unsigned char obuf[20];
|
||||
SHA1(pData, (size_t)nSize, obuf);
|
||||
|
||||
char* pBase64_hash = NULL;
|
||||
int nBase64Len_hash = 0;
|
||||
NSFile::CBase64Converter::Encode(obuf, 20, pBase64_hash, nBase64Len_hash, NSBase64::B64_BASE64_FLAG_NOCRLF);
|
||||
|
||||
std::string sReturn(pBase64_hash, nBase64Len_hash);
|
||||
delete [] pBase64_hash;
|
||||
|
||||
return sReturn;
|
||||
case OOXML_HASH_ALG_SHA1:
|
||||
{
|
||||
nBufLen = 20;
|
||||
pBufData = new unsigned char[nBufLen];
|
||||
SHA1(pData, (size_t)nSize, pBufData);
|
||||
break;
|
||||
}
|
||||
case OOXML_HASH_ALG_SHA224:
|
||||
{
|
||||
nBufLen = 28;
|
||||
pBufData = new unsigned char[nBufLen];
|
||||
SHA224(pData, (size_t)nSize, pBufData);
|
||||
break;
|
||||
}
|
||||
case OOXML_HASH_ALG_SHA256:
|
||||
{
|
||||
nBufLen = 32;
|
||||
pBufData = new unsigned char[nBufLen];
|
||||
SHA256(pData, (size_t)nSize, pBufData);
|
||||
break;
|
||||
}
|
||||
case OOXML_HASH_ALG_SHA384:
|
||||
{
|
||||
nBufLen = 48;
|
||||
pBufData = new unsigned char[nBufLen];
|
||||
SHA384(pData, (size_t)nSize, pBufData);
|
||||
break;
|
||||
}
|
||||
case OOXML_HASH_ALG_SHA512:
|
||||
{
|
||||
nBufLen = 64;
|
||||
pBufData = new unsigned char[nBufLen];
|
||||
SHA512(pData, (size_t)nSize, pBufData);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
else if (nAlg == OOXML_HASH_ALG_SHA256)
|
||||
{
|
||||
unsigned char obuf[32];
|
||||
SHA256(pData, (size_t)nSize, obuf);
|
||||
|
||||
char* pBase64_hash = NULL;
|
||||
int nBase64Len_hash = 0;
|
||||
NSFile::CBase64Converter::Encode(obuf, 32, pBase64_hash, nBase64Len_hash, NSBase64::B64_BASE64_FLAG_NOCRLF);
|
||||
if (0 == nBufLen)
|
||||
return "";
|
||||
|
||||
std::string sReturn(pBase64_hash, nBase64Len_hash);
|
||||
delete [] pBase64_hash;
|
||||
char* pBase64_hash = NULL;
|
||||
int nBase64Len_hash = 0;
|
||||
NSFile::CBase64Converter::Encode(pBufData, nBufLen, pBase64_hash, nBase64Len_hash, NSBase64::B64_BASE64_FLAG_NOCRLF);
|
||||
|
||||
return sReturn;
|
||||
}
|
||||
return "";
|
||||
std::string sReturn(pBase64_hash, nBase64Len_hash);
|
||||
delete [] pBase64_hash;
|
||||
|
||||
delete [] pBufData;
|
||||
|
||||
return sReturn;
|
||||
}
|
||||
|
||||
std::string GetHash(const std::string& sXml, int nAlg)
|
||||
@ -297,7 +357,7 @@ public:
|
||||
bool Verify(const std::string& sXml, std::string& sXmlSignature, int nAlg)
|
||||
{
|
||||
EVP_MD_CTX* pCtx = EVP_MD_CTX_create();
|
||||
const EVP_MD* pDigest = EVP_sha1();
|
||||
const EVP_MD* pDigest = Get_EVP_MD(this->GetHashAlg());
|
||||
|
||||
int n1 = EVP_VerifyInit(pCtx, pDigest);
|
||||
n1 = n1;
|
||||
@ -348,6 +408,29 @@ public:
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<int> GetHashAlgs()
|
||||
{
|
||||
std::vector<int> algs;
|
||||
if (!m_cert)
|
||||
return algs;
|
||||
|
||||
// TODO:
|
||||
// Check algs in cert
|
||||
|
||||
if (algs.empty())
|
||||
m_alg = OOXML_HASH_ALG_SHA1;
|
||||
else
|
||||
m_alg = algs[0];
|
||||
|
||||
return algs;
|
||||
}
|
||||
int GetHashAlg()
|
||||
{
|
||||
if (m_alg == OOXML_HASH_ALG_INVALID)
|
||||
GetHashAlgs();
|
||||
return m_alg;
|
||||
}
|
||||
|
||||
public:
|
||||
int ShowSelectDialog()
|
||||
{
|
||||
@ -390,6 +473,7 @@ public:
|
||||
m_id += m_separator;
|
||||
m_id += sCertPassword;
|
||||
|
||||
GetHashAlgs();
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -798,6 +882,15 @@ bool CCertificate_openssl::FromId(const std::string& id)
|
||||
return m_internal->FromKey(id);
|
||||
}
|
||||
|
||||
std::vector<int> CCertificate_openssl::GetHashAlgs()
|
||||
{
|
||||
return m_internal->GetHashAlgs();
|
||||
}
|
||||
int CCertificate_openssl::GetHashAlg()
|
||||
{
|
||||
return m_internal->GetHashAlg();
|
||||
}
|
||||
|
||||
namespace NSOpenSSL
|
||||
{
|
||||
int LoadKey(std::wstring file, std::string password)
|
||||
|
||||
@ -41,6 +41,9 @@ public:
|
||||
|
||||
virtual bool LoadFromBase64Data(const std::string& data);
|
||||
|
||||
virtual std::vector<int> GetHashAlgs();
|
||||
virtual int GetHashAlg();
|
||||
|
||||
public:
|
||||
virtual int ShowSelectDialog(void* parent = NULL);
|
||||
virtual int ShowCertificate(void* parent = NULL);
|
||||
|
||||
Reference in New Issue
Block a user