I signed file with my digital signature, how can i read this signature from this file?
Signature is trusted (Globalsign) . Encryption RSA/SHA1. Signed file is .exe
I signed file with my digital signature, how can i read this signature from this file?
Signature is trusted (Globalsign) . Encryption RSA/SHA1. Signed file is .exe
First, you need to specify what kind of certificate you're dealing with. If you are talking about a CLI assembly, then you might be dealing with StrongName signatures, which are entirely different beasts designed to prevent name collisions in the CLR's global assembly cache.
It sounds more like you want to read Authenticode signatures, which are used for both native and CLI applications. If you want to read the certificate itself, then you need to get your hands on the PE/COFF specification, and implement a parser for the PE ( Portable Executable ) file format, which is the format used by Windows NT and its derivatives. If you want to be able to actually verify that certificate, you need to call the WinVerifyTrust function, which will perform the Authenticode validation for you.
Of course, if you just want to check that your cert validates without dealing with writing your own application to do it, you can right click on the file and choose Properties... in Windows Explorer, and it should show you the signing status of the file. Otherwise, you can use the command line utility SigCheck.
The following code should do what you want. It is taken from a installer application to extract its own cert and install it into the local certification store.
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;
}