214 HCERTSTORE storeHandle = NULL;
218 if(NULL != storeHandle)
220 PCCERT_CONTEXT pCertContext = NULL;
221 while((pCertContext = ::CertEnumCertificatesInStore(storeHandle, pCertContext)) != NULL)
223 Data certDER(
Data::Borrow, (
const char*)pCertContext->pbCertEncoded, pCertContext->cbCertEncoded);
226 char pszNameString[256] = {0};
227 CertGetNameString(pCertContext, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL, pszNameString,
sizeof(pszNameString));
228 char pszIssuerString[256] = {0};
229 CertGetNameString(pCertContext, CERT_NAME_SIMPLE_DISPLAY_TYPE, CERT_NAME_ISSUER_FLAG, NULL, pszIssuerString,
sizeof(pszIssuerString));
230 char pszFriendlyNameString[256] = {0};
231 CertGetNameString(pCertContext, CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0, NULL, pszFriendlyNameString,
sizeof(pszFriendlyNameString));
232 char pszRDNString[256] = {0};
233 DWORD dwStrType = CERT_X500_NAME_STR;
235 CertGetNameString(pCertContext, CERT_NAME_RDN_TYPE, 0, &dwStrType, pszRDNString,
sizeof(pszRDNString));
237 DebugLog(<<
"[" << i+1 <<
"] WinSecurity::getCerts: CertificateInfo: Version=" << pCertContext->pCertInfo->dwVersion);
238 DebugLog(<<
" Name=" << pszNameString);
239 DebugLog(<<
" Issuer=" << pszIssuerString);
240 DebugLog(<<
" FriendlyName=" << pszFriendlyNameString);
241 DebugLog(<<
" Subject=" << pszRDNString);
245 HCRYPTPROV hCryptProv;
246 BOOL bCallerFreeProvOrNCryptKey;
248 BOOL bRet = CryptAcquireCertificatePrivateKey(
254 &bCallerFreeProvOrNCryptKey);
257 DWORD dwRet = GetLastError();
258 if(dwRet == CRYPT_E_NO_KEY_PROPERTY)
260 ErrLog( <<
" Cannot retrieve private key - non present!");
264 ErrLog( <<
" Cannot retrieve private key, dwRet=" << dwRet);
272 HCERTSTORE hMemoryStore;
273 if(hMemoryStore = CertOpenStore(
274 CERT_STORE_PROV_MEMORY,
281 printf(
"Opened a memory store for private key export. \n");
285 if(CertAddCertificateContextToStore(
288 CERT_STORE_ADD_USE_EXISTING,
291 printf(
"Certificate added to the memory store. \n");
293 CRYPT_DATA_BLOB dataBlob = {0};
294 LPCWSTR password = L
"";
296 if(PFXExportCertStoreEx(hMemoryStore, &dataBlob, password, NULL,
297 EXPORT_PRIVATE_KEYS | REPORT_NOT_ABLE_TO_EXPORT_PRIVATE_KEY | REPORT_NO_PRIVATE_KEY))
299 if (dataBlob.cbData > 0)
301 dataBlob.pbData = (BYTE*)malloc(dataBlob.cbData);
302 if (PFXExportCertStoreEx(hMemoryStore, &dataBlob, password, NULL,
303 EXPORT_PRIVATE_KEYS | REPORT_NOT_ABLE_TO_EXPORT_PRIVATE_KEY | REPORT_NO_PRIVATE_KEY))
307 STACK_OF(
X509) *ca = NULL;
310 BIO* input = BIO_new_mem_buf((
void*)dataBlob.pbData, dataBlob.cbData);
311 p12 = d2i_PKCS12_bio(input, NULL);
313 PKCS12_parse(p12,
"" , &pkey, &cert, &ca);
318 BIO *bo = BIO_new( BIO_s_mem() );
319 PEM_write_bio_PrivateKey(bo, pkey, NULL, (
unsigned char *)
"" , 0, NULL, (
char*)
"" );
322 long len = BIO_get_mem_data(bo, &p);
329 free(dataBlob.pbData);
335 DWORD dwRet = GetLastError();
336 ErrLog( <<
" Could not PFXExportCertStoreEx, dwRet=" << dwRet);
341 DWORD dwRet = GetLastError();
342 ErrLog( <<
" Could not add the certificate to the memory store, dwRet=" << dwRet);
345 CertCloseStore(hMemoryStore, CERT_CLOSE_STORE_CHECK_FLAG);
349 DWORD dwRet = GetLastError();
350 ErrLog( <<
" Cannot open cert store, dwRet=" << dwRet);
353 if(bCallerFreeProvOrNCryptKey)
355#if(_WIN32_WINNT >= 0x0600)
356 if(dwKeySpec == CERT_NCRYPT_KEY_SPEC)
358 NCryptFreeObject(hCryptProv);
362 CryptReleaseContext(hCryptProv, 0);
365 CryptReleaseContext(hCryptProv, 0);
371 InfoLog( << i <<
" certs loaded of type " << eType <<
" (" << ip <<
" private keys loaded)");