检索指纹与在属性中使用“数字签名”选项卡看到的指纹相同

时间:2018-07-25 11:38:21

标签: winapi visual-c++ mfc digital-certificate

我一直在搜索很多Windows API,以获取数字证书的指纹。我尝试使用CryptHashCertificate(),但是,我的指纹似乎与在属性中使用“数字签名”选项卡看到的指纹不匹配。 之后,我尝试了OpenSSL,但是它需要'pem'文件作为输入。我的输入将是exe,dll,msi。 最终,我从X509Certificate2获得了解决方案,但由于它来自.Net,因此将具有依赖性。所以我的问题有什么办法可以从Wincrypt之类的Windows库中获取指纹? 编辑:我如何使用CryptHashCertificate()-

 WCHAR szFileName[MAX_PATH]; 
 _swprintf(szFileName,_T("C:\\vsgraphicsremoteengine.exe"));
HCERTSTORE hStore = NULL;
HCRYPTMSG hMsg = NULL; 
PCCERT_CONTEXT pCertContext = NULL;
BOOL fResult;   
DWORD dwEncoding, dwContentType, dwFormatType;
PCMSG_SIGNER_INFO pSignerInfo = NULL;
PCMSG_SIGNER_INFO pCounterSignerInfo = NULL;
DWORD dwSignerInfo;
CERT_INFO CertInfo;     
SPROG_PUBLISHERINFO ProgPubInfo;
SYSTEMTIME st;

ZeroMemory(&ProgPubInfo, sizeof(ProgPubInfo));
__try
{      

    // Get message handle and store handle from the signed file.
    fResult = CryptQueryObject(CERT_QUERY_OBJECT_FILE,
                               szFileName,
                               CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED_EMBED,
                               CERT_QUERY_FORMAT_FLAG_BINARY,
                               0,
                               &dwEncoding,
                               &dwContentType,
                               &dwFormatType,
                               &hStore,
                               &hMsg,
                               NULL);
    if (!fResult)
    {
        _tprintf(_T("CryptQueryObject failed with %x\n"), GetLastError());
        __leave;
    }

    // Get signer information size.
    fResult = CryptMsgGetParam(hMsg, 
                               CMSG_SIGNER_INFO_PARAM, 
                               0, 
                               NULL, 
                               &dwSignerInfo);
    if (!fResult)
    {
        _tprintf(_T("CryptMsgGetParam failed with %x\n"), GetLastError());
        __leave;
    }

    // Allocate memory for signer information.
    pSignerInfo = (PCMSG_SIGNER_INFO)LocalAlloc(LPTR, dwSignerInfo);
    if (!pSignerInfo)
    {
        _tprintf(_T("Unable to allocate memory for Signer Info.\n"));
        __leave;
    }

    // Get Signer Information.
    fResult = CryptMsgGetParam(hMsg, 
                               CMSG_SIGNER_INFO_PARAM, 
                               0, 
                               (PVOID)pSignerInfo, 
                               &dwSignerInfo);
    if (!fResult)
    {
        _tprintf(_T("CryptMsgGetParam failed with %x\n"), GetLastError());
        __leave;
    }

    // Get program name and publisher information from 
    // signer info structure.
    if (GetProgAndPublisherInfo(pSignerInfo, &ProgPubInfo))
    {
        if (ProgPubInfo.lpszProgramName != NULL)
        {
            wprintf(L"Program Name : %s\n",
                ProgPubInfo.lpszProgramName);
        }

        if (ProgPubInfo.lpszPublisherLink != NULL)
        {
            wprintf(L"Publisher Link : %s\n",
                ProgPubInfo.lpszPublisherLink);
        }

        if (ProgPubInfo.lpszMoreInfoLink != NULL)
        {
            wprintf(L"MoreInfo Link : %s\n",
                ProgPubInfo.lpszMoreInfoLink);
        }
    }

    _tprintf(_T("\n"));

    // Search for the signer certificate in the temporary 
    // certificate store.
    CertInfo.Issuer = pSignerInfo->Issuer;
    CertInfo.SerialNumber = pSignerInfo->SerialNumber;

    pCertContext = CertFindCertificateInStore(hStore,
                                              ENCODING,
                                              0,
                                              CERT_FIND_SUBJECT_CERT,
                                              (PVOID)&CertInfo,
                                              NULL);
    if (!pCertContext)
    {
        _tprintf(_T("CertFindCertificateInStore failed with %x\n"),
            GetLastError());
        __leave;
    }

    // Print Signer certificate information.
    _tprintf(_T("Signer Certificate:\n\n"));        
    PrintCertificateInfo(pCertContext);
    _tprintf(_T("\n"));

    // Get the timestamp certificate signerinfo structure.
    if (GetTimeStampSignerInfo(pSignerInfo, &pCounterSignerInfo))
    {
        // Search for Timestamp certificate in the temporary
        // certificate store.
        CertInfo.Issuer = pCounterSignerInfo->Issuer;
        CertInfo.SerialNumber = pCounterSignerInfo->SerialNumber;

        pCertContext = CertFindCertificateInStore(hStore,
                                            ENCODING,
                                            0,
                                            CERT_FIND_SUBJECT_CERT,
                                            (PVOID)&CertInfo,
                                            NULL);
        if (!pCertContext)
        {
            _tprintf(_T("CertFindCertificateInStore failed with %x\n"),
                GetLastError());
            __leave;
        }            
      DWORD cbData;
      BYTE  *pbData;
      if(CryptHashCertificate(NULL, CALG_SHA1, 0, pCertContext->pbCertEncoded,
                                pCertContext->cbCertEncoded, NULL, &cbData))
      {
           _tprintf(_T("The function succeeded.\n"));
      }
      else
      {
           _tprintf(_T("The function Failed.\n"));
      }
        pbData = (BYTE*)malloc(cbData);
        CryptHashCertificate(NULL, CALG_SHA1, 0, pCertContext->pbCertEncoded,
                                pCertContext->cbCertEncoded, pbData, &cbData);
        _tprintf(_T("ThumbPrint:"));
         for (DWORD n = 0; n < cbData; n++)
         {
                _tprintf(_T("%02x "), pbData[n]);
        }

Image for thumbprint on windows 7

0 个答案:

没有答案