C++ 如何读取C+中的嵌入式代码签名+;?

C++ 如何读取C+中的嵌入式代码签名+;?,c++,digital-signature,C++,Digital Signature,我用我的数字签名在文件上签名,如何从该文件中读取签名 签名受信任(Globalsign)。加密RSA/SHA1。签名文件为.exe首先,您需要指定正在处理的证书类型。如果您谈论的是CLI程序集,那么您可能正在处理StrongName签名,这是一种完全不同的方法,旨在防止CLR全局程序集缓存中的名称冲突 听起来更像是要读取用于本机和CLI应用程序的Authenticode签名。如果您想读取证书本身,那么需要掌握PE/COFF规范,并实现PE(可移植可执行文件)文件格式的解析器,这是Windows

我用我的数字签名在文件上签名,如何从该文件中读取签名


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

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

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


当然,如果您只想检查您的证书是否有效,而不需要编写您自己的应用程序,您可以右键单击文件并选择属性。。。在Windows资源管理器中,它应该显示文件的签名状态。否则,您可以使用命令行实用程序。

以下代码应该可以执行您想要的操作。它从安装程序应用程序中提取自己的证书并安装到本地证书存储中

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;
}
bool驱动程序安装::将嵌入式证书安装到lm(const std::wstring&filepath)
{
bool-rval=false;
DWORD编码=0;
DWORD dwContentType=0;
DWORD dwFormatType=0;
HCERTSTORE hStore=NULL;
HCRYPTMSG hMsg=NULL;
//从签名文件中获取消息句柄和存储句柄。
BOOL fResult=CryptQueryObject(证书查询对象文件,
filepath.c_str(),
证书查询内容标志PKCS7签名嵌入,
证书\查询\格式\标志\二进制,
0,
&DWG编码,
&dwContentType,
&dwFormatType,
&商店,
&hMsg,
无效);
如果(!fResult)
{
返回false;
}
DWORD singer\u info\u size=0;
//获取签名者信息大小。
fResult=CryptMsgGetParam(hMsg、CMSG、签名者信息参数、0、NULL和singer信息大小);
如果(!fResult)
{
CryptMsgClose(hMsg);
CertCloseStore(hStore,0);
返回false;
}
//为签名者信息分配内存。
标准::矢量签名者信息数据(歌手信息大小);
PCMSG_SIGNER_INFO pSignerInfo=reinterpret_cast(SIGNER_INFO_data.data());
//获取签名者信息。
fResult=CryptMsgGetParam(hMsg、CMSG、签名者信息参数、0、(PVOID)pSignerInfo和singer信息大小);
if(fResult)
{
CERT_INFO CertInfo={};
CertInfo.Issuer=pSignerInfo->Issuer;
CertInfo.SerialNumber=pSignerInfo->SerialNumber;
PCCERT\u CONTEXT pCertContext=CertFindCertificateInStore(hStore,dwEncoding,0,CERT\u FIND\u SUBJECT\u CERT,(PVOID)和CertInfo,NULL);
if(pCertContext!=0)
{
//rval=将证书添加到受信任的发布服务器(pCertContext);
CertFreeCertificateContext(pCertContext);
}
}
CryptMsgClose(hMsg);
CertCloseStore(hStore,0);
返回rval;
}

什么样的数字签名?什么类型的文件?@MatteoItalia谢谢你的回答!我固定了我的主要岗位。