2014-10-09 82 views
0

我希望解析并显示从Window PE二进制安全目录中提取的Authenticode PKCS#7签名的内容。通过Windows API解码PKCS#7签名?

我可以使用OpenSSL在命令行上使用“openssl pkcs7 -text -in extracted_signature.pks -inform DER -print_certs”执行此操作,但是我需要通过C/C++和Windows API执行此操作。我不能使用OpenSSL库本身。

使用CryptDecodeObjectEx API我可以开始提取的签名进行解码:

CRYPT_CONTENT_INFO * content_info; 
DWORD len; 

CryptDecodeObjectEx(
    X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, 
    PKCS_CONTENT_INFO, 
    pointer_to_extracted_signature, 
    length_of_extracted_signature, 
    CRYPT_DECODE_ALLOC_FLAG, 
    NULL, 
    &content_info, 
    &len 
); 

以上调用成功完成,并content_info->pszObjId将有“1.2.840.113549.1.7.2”(szOID_RSA_signedData)的OID但是我我无法找到继续解码所需的结构。 CryptDecodeObjectEx的可用OID列于here

任何人可以请告知如何将验证码PKCS#7签名通过Windows API解码?

回答

0

我发现解码Authenticode PKCS#7签名的正确方法是使用CryptQueryObject并设置CERT_QUERY_OBJECT_BLOBCERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED标志。代码snippit下面的任何人可能需要这样做。

CERT_BLOB cert_blob; 
HCERTSTORE cert_store = NULL; 
HCRYPTMSG cert_msg = NULL; 

cert_blob.pbData = pointer_to_extracted_signature; 
cert_blob.cbData = length_of_extracted_signature; 

CryptQueryObject(
    CERT_QUERY_OBJECT_BLOB, 
    &cert_blob, 
    CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED, 
    CERT_QUERY_FORMAT_FLAG_BINARY, 
    0, 
    NULL, 
    NULL, 
    NULL, 
    &cert_store, 
    &cert_msg, 
    NULL 
); 

PCCERT_CONTEXT next_cert = NULL; 

while((next_cert = CertEnumCertificatesInStore(cert_store, next_cert)) != NULL) 
{ 
    // process next_cert... 
}