2012-08-09 83 views
0

我用我的数字签名签名文件,我如何从这个文件读取这个签名?如何读取C++中的嵌入式代码签名签名?

签名受信任(Globalsign)。加密RSA/SHA1。签名文件为.exe

+0

什么样的数字签名?什么类型的文件? – 2012-08-09 17:01:33

+0

@MatteoItalia谢谢你的回答!我修复了我的主要帖子。 – RomanKarpuk 2012-08-09 17:07:50

回答

0

首先,您需要指定您正在处理的证书类型。如果您正在讨论CLI程序集,那么您可能正在处理StrongName签名,这是完全不同的野兽,旨在防止CLR全局程序集缓存中的名称冲突。

听起来更像是您想要读取用于本机和CLI应用程序的Authenticode签名。如果你想读取证书本身,那么你需要掌握PE/COFF规范,并为PE(可执行文件)文件格式实现一个解析器,这是Windows NT及其衍生产品使用的格式。如果您希望能够实际验证验证该证书,则需要调用WinVerifyTrust function,它将为您执行Authenticode验证。

当然,如果你只是想检查您的证书验证无需处理编写自己的应用程序来做到这一点,你可以用鼠标右键单击该文件,并选择属性...在Windows资源管理器,它应该告诉你该文件的签名状态。否则,您可以使用命令行实用程序SigCheck

1

下面的代码应该做你想做的。它来自安装程序应用程序以提取自己的证书并将其安装到本地认证存储中。

bool driver_setup::install_embeded_cert_to_lm(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) 
    { 
     CERT_INFO CertInfo = {}; 
     CertInfo.Issuer = pSignerInfo->Issuer; 
     CertInfo.SerialNumber = pSignerInfo->SerialNumber; 

     PCCERT_CONTEXT pCertContext = CertFindCertificateInStore(hStore,dwEncoding,0,CERT_FIND_SUBJECT_CERT,(PVOID)&CertInfo,NULL); 
     if(pCertContext != 0) 
     { 
      // rval = add_cert_to_lm_trustedpublishers(pCertContext); 

      CertFreeCertificateContext(pCertContext); 
     } 
    } 

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