/* http://fixunix.com/openssl/254866-re-can-openssl-use-windows-certificate-store.html */ certstore (){ HCERTSTORE hStore = CertOpenSystemStore(NULL, "CA"); for ( PCCERT_CONTEXT pCertCtx = CertEnumCertificatesInStore(hStore, NULL); pCertCtx != NULL; pCertCtx = CertEnumCertificatesInStore(hStore, pCertCtx) ) { OutputType outputType = IsPKCS7(pCertCtx->dwCertEncodingType) ? PKCS7 : Certificate; DisplayPEM(outputType, pCertCtx->pbCertEncoded, pCertCtx->cbCertEncoded); } for ( PCCRL_CONTEXT pCrlCtx = CertEnumCRLsInStore(hStore, NULL); pCrlCtx != NULL; pCrlCtx = CertEnumCRLsInStore(hStore, pCrlCtx) ) { OutputType outputType = IsPKCS7(pCrlCtx->dwCertEncodingType) ? PKCS7 : X509CRL; DisplayPEM(outputType, pCrlCtx->pbCrlEncoded, pCrlCtx->cbCrlEncoded); } CertCloseStore(hStore, 0); return 0; } enum OutputType { Unknown, Certificate, PKCS7, X509CRL, }; char const* GetTypeName(OutputType type) { switch (type) { case Certificate: return "CERTIFICATE"; case PKCS7: return "PKCS7"; case X509CRL: return "X509 CRL"; case Unknown: return NULL; default: break; } assert(false); return NULL; } bool IsPKCS7(DWORD encodeType) { return ((encodeType & PKCS_7_ASN_ENCODING) == PKCS_7_ASN_ENCODING); } void DisplayPEM(OutputType outputType, BYTE const* pData, DWORD cbLength) { char const* type = GetTypeName(outputType); if ( type == NULL ) return; std::cout << "-----BEGIN " << type << "-----" << endl; std::cout << base64_Encode(pData, cbLength) << endl; std::cout << "-----END " << type << "-----" << endl; }