简体   繁体   中英

print digital signature infos

As in the title specified, I'd like to print the digital signature information out to the console. Here is the code I wrote:

bool CheckDigSig(const std::wstring& filepath)
{
    bool rval = false;

    DWORD dwEncoding = 0;
    DWORD dwContentType = 0;
    DWORD dwFormatType = 0;
    HCERTSTORE hStore = NULL;
    HCRYPTMSG hMsg = NULL;

    // Get message handle and store handle from the signed file.
    BOOL fResult = CryptQueryObject(CERT_QUERY_OBJECT_FILE,
        filepath.c_str(),
        CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED_EMBED,
        CERT_QUERY_FORMAT_FLAG_BINARY,
        0,
        &dwEncoding,
        &dwContentType,
        &dwFormatType,
        &hStore,
        &hMsg,
        NULL);
    if (!fResult)
        return false;

    DWORD singer_info_size = 0;
    // Get signer information size.
    fResult = CryptMsgGetParam(hMsg, CMSG_SIGNER_INFO_PARAM, 0, NULL, &singer_info_size);
    if (!fResult)
    {
        CryptMsgClose(hMsg);
        CertCloseStore(hStore, 0);
        return false;
    }

    // Allocate memory for signer information.
    std::vector<byte> signer_info_data(singer_info_size);
    PCMSG_SIGNER_INFO pSignerInfo = reinterpret_cast<PCMSG_SIGNER_INFO>(signer_info_data.data());

    // Get Signer Information.
    fResult = CryptMsgGetParam(hMsg, CMSG_SIGNER_INFO_PARAM, 0, (PVOID)pSignerInfo, &singer_info_size);
    if (fResult)
    {
        //pSignerInfo->Issuer;
        //pSignerInfo->SerialNumber;
    }

    CryptMsgClose(hMsg);
    CertCloseStore(hStore, 0);
    return rval;
}

I would like to print those two variables at the end (which is now commendted): pSignerInfo->Issuer; pSignerInfo->SerialNumber;

I've got no idea how could I make it readable format, like a string, byte or char array. Could you help me with it?

This article http://support.microsoft.com/kb/323809 has the code you need. Here's a short snippet of it:

    // Get Issuer name.
    if (!(CertGetNameString(pCertContext, 
                            CERT_NAME_SIMPLE_DISPLAY_TYPE,
                            CERT_NAME_ISSUER_FLAG,
                            NULL,
                            szName,
                            dwData)))
    // ...

There's more code in there, obviously, covering all the various corners of this task. Including printing the SerialNumber as well

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM