1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| template <typename T> T decodeObject(DWORD dwEncodingType, const PCERT_EXTENSION pExt) { T pInfo = nullptr; DWORD cbInfo = 0; if (!CryptDecodeObjectEx(dwEncodingType, pExt->pszObjId, pExt->Value.pbData, pExt->Value.cbData, CRYPT_DECODE_ALLOC_FLAG | CRYPT_DECODE_NOCOPY_FLAG, nullptr, &pInfo, &cbInfo)) return nullptr; return pInfo; }
HCERTSTORE hCertStore = CertOpenStore(CERT_STORE_PROV_FILENAME_A, dwEncodingType, NULL, CERT_STORE_OPEN_EXISTING_FLAG | CERT_STORE_READONLY_FLAG, "ca.cer"); if (hCertStore) { PCCERT_CONTEXT pCertContext = CertEnumCertificatesInStore(hCertStore, nullptr); if (pCertContext) { for (DWORD i = 0; i < pCertContext->pCertInfo->cExtension; ++i) { const PCERT_EXTENSION pExt = pCertContext->pCertInfo->rgExtension + i; if (strcmp(pExt->pszObjId, szOID_KEY_USAGE) == 0) { const auto pInfo = decodeObject<PCRYPT_BIT_BLOB>(dwEncodingType, pExt); LocalFree(pInfo); } else if(strcmp(pExt->pszObjId, szOID_BASIC_CONSTRAINTS2) == 0) { const auto pInfo = decodeObject<PCERT_BASIC_CONSTRAINTS2_INFO>(dwEncodingType, pExt); LocalFree(pInfo); } else if (strcmp(pExt->pszObjId, szOID_ENHANCED_KEY_USAGE) == 0) { auto pInfo = decodeObject<PCTL_USAGE>(dwEncodingType, pExt); LocalFree(pInfo); } else if (strcmp(pExt->pszObjId, szOID_SUBJECT_ALT_NAME2) == 0) { const auto pInfo = decodeObject<PCERT_ALT_NAME_INFO>(dwEncodingType, pExt); LocalFree(pInfo); } } } CertCloseStore(hCertStore, 0); }
|