VC++ : x509 certificate & custom extension parsing WinTrust API
I am working of certificate parsing, where after issuing some PKI API, I will be getting certificate format in form of hex/hex64 or binary string.
I want to parse that string into certificate into x509 certificate format and get customer specific extension.
I am thinking to use WinTrust API to parse certificate and fetch those extension.
I am able to retrieve data in CERT_BLOP struct but CryQueryObject API fails and thus I am not able to retrieve certificate data
Following are sequence of API I am using
Retrieve length using CrypStringToBinary to retrieve length
pCertData = new byte[dwCertLen];
if (pCertData != NULL)
{
if (!CryptStringToBinaryA(sCertData.c_str(), 0, nEncoding, (byte*)pCertData, &dwCertLen, NULL, NULL))
{
return false;
}
/*
* Decode from DER format to CERT_PUBLIC_KEY_INFO
/
CERT_BLOB oCertData;
DWORD dwCertDataLen;
if(!CryptDecodeObjectEx((X509_ASN_ENCODING | PKCS_7_ASN_ENCODING), X509_ANY_STRING, (byte)pCertData, dwCertLen,
CRYPT_ENCODE_ALLOC_FLAG, NULL, &oCertData.pbData, &oCertData.cbData))
{
fprintf(stderr, "CryptDecodeObjectEx 1 failed. Err: %d\n", GetLastError());
return -1;
}
DWORD dwEncoding, dwContentType, dwFormatType;
// Below API is failing and return error code -2146885623
bResult is false
bool bResult = CryptQueryObject(CERT_QUERY_OBJECT_BLOB,
(void*)&oCertData,
CERT_QUERY_CONTENT_FLAG_PFX_AND_LOAD,
CERT_QUERY_FORMAT_FLAG_BINARY,
0,
&dwEncoding,
&dwContentType,
&dwFormatType,
&m_hStore,
&m_hMsg,
NULL);
I want to perform following task.
* Parse certificate from string.
* Retrieve customer specific x509 extension
* Validate certificate base on OID
Can you please suggest is Wintrust lis is good option or any other lib.
I have gone through docs did not find any sample code for it, if you can help me with sample code that will be of great help!