使用 WinCrypt 或 CNG 验证签名文件 (PKCS7)

Sim*_*ton 2 sign asn.1 cng wincrypt

我需要使用 Windows 加密 API 方法验证已签名的 JAR 文件。我对加密和签名问题只有基本的了解。我也是那些加密 API(WinCrypt、Bcrypt、Ncrypt)的新手。验证文件哈希不是问题,但签名部分阻止了我。

多亏了 OpenSSL、PKCS7 RFC ( https://tools.ietf.org/html/rfc2315 ) 和各种其他来源,我才能够找出 JAR 中包含的 META-INF/LOCALSIG.DSA 的实际文件内容。但是经过两周的挖掘、反复试验,我仍然被卡住,不知道还能尝试什么。

OpenSSL 有很好的命令openssl smime -verify -inform der -in LOCALSIG.DSA -content LOCALSIG.SF -noverify,它完全符合我的要求。不幸的是,我在 Windows API 中找不到这样的高级命令。

我已经尝试使用VerifySignature来自所有三个 API 的函数系列,但是我需要为它们提供公钥,并且我没有使用任何ImportKey函数。因此,我尝试使用CryptDecodeObjectEx,手动剖析 ASN1 格式,以便将各个部分作为 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 文件包含三个证书)。这意味着我无法硬编码要使用的证书或方法。我需要从提供的文件中提取该信息。我怎么做?

pls*_*ain 5

正如我从您的问题中了解到的,您需要使用分离签名验证 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;
    }
Run Code Online (Sandbox Code Playgroud)

更新
要验证证书链,您需要使用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);
}
Run Code Online (Sandbox Code Playgroud)