title: How to verify that my orgainization signed a trusted windows binary?
body: <p>This is a followup question to <a href="https://stackoverflow.com/questions/1072540/winverifytrust-to-check-for-a-specific-signature">question 1072540, 'WinVerifyTrust to check for a specific signature?'</a>.</p>
  
  <p>I want to write a C++ function Lets call it <code>TrustedByUs</code> of the form:</p>
  
  <pre><code>bool TrustedByUs(std::string pathToBinary, std::string pathToPublicKey)
  </code></pre>
  
  <p>The idea is that we give this function a path to a binary .dll or .exe file that has been signed with a digital signature.  The <code>pathToPublicKey</code> string is the path to a public key of our particular signing certificate.  </p>
  
  <p>Using the code in <a href="http://support.microsoft.com/kb/323809" rel="nofollow noreferrer">http://support.microsoft.com/kb/323809</a> it's pretty straight forward to verify that the <code>pathToBinary</code> file is in fact trusted by the operating system.  </p>
  
  <p>Now I'm in the same place as the writer of question 1072540, I know the OS trusts the signer of this binary, but I want to know if my organization's RSA key is the one that signed the binary.  </p>
  
  <p>The KB323809 shows how to extract the strings from certificate embedded in our binary file. This example shows how to extract strings from the signing certificate in its <code>GetProgAndPublisherInfo</code> function, but I'm uncomfortable using a string match to verfiy the certificate.</p>
  
  <p>What I would like to do is extract the public key from the embedded signature and compare it to the public key that corresponds with the private key that signed my binary file in the first place.</p>
  
  <p>The documentation for <code>CryptMsgGetParam</code> says that the <code>CMSG_SIGNER_CERT_ID_PARAM</code>
  parameter 'Returns information on a message signer needed to identify the signer's public key'. I succeed in getting the certificate's serial number with this key. My code looks like this:</p>
  
  <pre><code>// Get message handle and store handle from the signed file.
  fResult = CryptQueryObject(CERT_QUERY_OBJECT_FILE,
      L"C:\\Program Files\\MySignedProgram.exe",
      CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED_EMBED,
      CERT_QUERY_FORMAT_FLAG_BINARY,
      0, &amp;dwEncoding, &amp;dwContentType, &amp;dwFormatType, &amp;hStore, &amp;hMsg, NULL);
  
  // Get the public key information about the signer
  // First get the size
  DWORD dwCertIdSize(0);
  fResult = CryptMsgGetParam(hMsg, CMSG_SIGNER_CERT_ID_PARAM,
      0, NULL, &amp;dwCertIdSize);
  BYTE* pCertId = new BYTE(dwCertIdSize);
  ::ZeroMemory(pCertId,dwCertIdSize);
  
  // Now get the cert info
  fResult = CryptMsgGetParam(hMsg, CMSG_SIGNER_CERT_ID_PARAM,
      0, (PVOID)pCertId, &amp;dwCertIdSize);
  
  if(fResult)
  {      
      CERT_ID* pId = (CERT_ID*)pCertId;  
      pId-&gt;HashId;
      pId-&gt;dwIdChoice;
      pId-&gt;IssuerSerialNumber;  // Valid serial number (reversed)
      pId-&gt;KeyId;   
      _tprintf("pid\n");
  }
  </code></pre>
  
  <p>This is close to what I want, but really I'd like to use the signing certificate's public key to verify that the target signed binary file was in fact created with my particular public/private key pair.  </p>
  
  <p>Using the <code>CMSG_ENCRYPTED_DIGEST</code> flag this code succeeds:</p>
  
  <pre><code>// Get digest which was encrypted with the private key
  DWORD digestSize(0);
  fResult = CryptMsgGetParam(hMsg, CMSG_ENCRYPTED_DIGEST, 0, NULL, &amp;digestSize);
  
  BYTE* pDigest = new BYTE[digestSize];
  
  // Next CryptMsgGetParam call succeds,
  // pDigest looks valid, can I use this to confirm my public key
  // was used to sign MySignedProgram.exe ?
  fResult = CryptMsgGetParam(hMsg, CMSG_ENCRYPTED_DIGEST, 0, pDigest, &amp;digestSize);
  </code></pre>
  
  <p><strong>Bottom line question</strong>:  Given the certificate information discovered by <code>CryptQueryObject</code>, what technique should I used to ensure that the target file was in fact signed using the private key that corresponds to the public key that is available to me when the above code executes?</p>