sign - 使用 WinCrypt 或 CNG 验证签名文件 (PKCS7)
问题描述
我需要使用 Windows 加密 API 方法验证签名的 JAR 文件。我对加密和签名问题只有一个基本的了解。我对这些加密 API(WinCrypt、Bcrypt、Ncrypt)也很陌生。验证文件哈希不是问题,但签名部分阻止了我。
感谢 OpenSSL、PKCS7 RFC ( https://www.rfc-editor.org/rfc/rfc2315 ) 和其他各种来源,我能够找出 JAR 中 META-INF/LOCALSIG.DSA 的实际文件内容. 但经过两周的挖掘、反复试验,我仍然陷入困境,不知道还能尝试什么。
OpenSSL 有 nice command openssl smime -verify -inform der -in LOCALSIG.DSA -content LOCALSIG.SF -noverify
,这正是我想做的。不幸的是,我在 Windows API 中找不到如此高级的命令。
我尝试使用VerifySignature
所有三个 API 中的函数系列,但我需要为这些 API 提供公钥,而且我没有使用任何ImportKey
函数。因此,我尝试使用 手动剖析 ASN1 格式CryptDecodeObjectEx
,以便将各个部分作为 BLOB 传递给 API 函数。虽然我在这方面取得了一些成功,但我再次陷入困境,因为我不知道如何解析集合。我不想从头开始编写自己的 ASN1 解析器......
那么,如何将 PKCS7 签名文件与 Windows 加密 API 一起使用?
我想使用 OpenSSL 可能会更容易,但我必须说服我的雇主将 OpenSSL 添加到我们的代码库中只是为了这个目的......
更新: LOCALSIG.DSA 文件包含签名者证书和 LOCALSIG.SF 文件的签名哈希。这可以使用openssl pkcs7 -inform der -print_certs -text -in LOCALSIG.DSA
或openssl cms -cmsout -inform DER -print -in LOCALSIG.DSA
。
该证书由我们公司自签名并在证书存储中。我可能需要提供整个信任链。这就是为什么我将-noverify
选项添加到openssl smime -verify
.
实际上,有两种使用不同证书(内部和外部版本)的场景,一种使用 DSA(sig 文件包含一个证书),另一种使用 RSA(sig 文件包含三个证书)。这意味着我无法硬编码要使用的证书或方法。我需要从提供的文件中提取该信息。我怎么做?
解决方案
我从您的问题中了解到,您需要使用分离签名验证 PKC7。为此,您可以使用函数CryptVerifyDetachedMessageSignature。
示例代码:
CRYPT_VERIFY_MESSAGE_PARA vparam = { 0 };
vparam.cbSize = sizeof(CRYPT_VERIFY_MESSAGE_PARA);
vparam.dwMsgAndCertEncodingType = X509_ASN_ENCODING | PKCS_7_ASN_ENCODING;
vparam.pfnGetSignerCertificate = nullptr;
vparam.pvGetArg = nullptr;
/* read your files somehow */
std::vector<char> sig;
if (!ReadFile("LOCALSIG.DSA", &sig))
{
std::cout << "Failed to read file" << std::endl;
return;
}
std::vector<char> mes;
if (!ReadFile("LOCALSIG.SF", &mes))
{
std::cout << "Failed to read file" << std::endl;
return;
}
/* CryptVerifyDetachedMessageSignature requires array of messages to verify */
const BYTE* marray[] = {
reinterpret_cast<BYTE*>(mes.data())
};
DWORD marray_len[] = {
static_cast<DWORD>(mes.size())
};
if (!CryptVerifyDetachedMessageSignature(vparam,
0,
reinterpret_cast<BYTE*>(sig.data()),
static_cast<DWORD>(sig.size()),
1, /* number of messages in marray */
marray,
marray_len,
nullptr))
{
std::cout << "Failed to verify signature, error: " << GetLastError() << std::endl;
}
else
{
std::cout << "Verify success, signature valid" << std::endl;
}
更新
要验证证书链,您需要使用CertGetCertificateChain
示例代码:
PCCERT_CHAIN_CONTEXT pChain = nullptr;
CERT_CHAIN_PARA chainPara = {0};
HCERTSTORE hStore = nullptr;
/* you sig file */
DATA_BLOB db = {
static_cast<DWORD>(sig.size()), reinterpret_cast<BYTE *>(sig.data())};
/* you can open your sig file as certificate store */
hStore = CertOpenStore(CERT_STORE_PROV_PKCS7, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, NULL, 0, reinterpret_cast<BYTE *>(&db));
if (!hStore)
{
goto Exit;
}
chainPara.cbSize = sizeof(CERT_CHAIN_PARA);
chainPara.RequestedUsage.dwType = USAGE_MATCH_TYPE_AND;
chainPara.RequestedUsage.Usage.cUsageIdentifier = 0;
if (!CertGetCertificateChain(NULL, /* use default chain engine */
pCert, /* pCert - pointer to signer cert structure (the parameter that was obtained in the previous step) */
NULL,
hStore, /* point to additional store where need to search for certificates to build chain */
&chainPara,
CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT,
NULL,
&pChain))
{
std::cout << "failed to build chain: " << GetLastError();
goto Exit;
}
if (pChain->TrustStatus.dwErrorStatus == CERT_TRUST_NO_ERROR)
{
std::cout << "certificate valid";
goto Exit;
}
if (pChain->TrustStatus.dwErrorStatus & CERT_TRUST_REVOCATION_STATUS_UNKNOWN)
{
std::cout << "certificate revocation status unknown";
}
/* you need to place root certificate to the Trusted Root Store */
if (pChain->TrustStatus.dwErrorStatus & CERT_TRUST_IS_UNTRUSTED_ROOT)
{
std::cout << "untrusted CA";
}
/* and so on */
Exit :
if (pCert)
{
CertFreeCertificateContext(pCert);
}
if (pChain)
{
CertFreeCertificateChain(pChain);
}
if (hStore)
{
CertCloseStore(hStore, 0);
}
推荐阅读
- vue.js - 在这种情况下,对象解构意味着什么
- c++ - 不知道为什么对象是本地 vs 引用
- r - 如何在R中的函数中更改函数的默认参数
- sql - 分区列倾斜时更有效地写入分区拼花
- entity-framework - GroupJoin:抛出异常:System.InvalidOperationException
- google-sheets - 导入 Craigslist 信息
- java - 微服务架构中的错误传播
- python - FileNotFoundError:[Errno 2] 没有这样的文件或目录:使用 csvreader
- powershell - 如何使用 jenkins 管道中的美元斜线调用 powershell 脚本并将输出传递到下一阶段
- sccm - SCCM 1606 和 ADK 1607