void CEstEIDCertificate::readFromCertContext() { LOG_LOCATION; PCCERT_CONTEXT pCertContext = NULL; HCERTSTORE hCertStore = NULL; CRYPTUI_SELECTCERTIFICATE_STRUCT sel = {sizeof(sel)}; int counter = 0; hCertStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, NULL, CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_READONLY_FLAG, L"MY"); if(!hCertStore){ throw CryptoException(); } sel.pvCallbackData = &counter; sel.pFilterCallback = filter_proc; sel.rghDisplayStores = &hCertStore; sel.cDisplayStores = 1; #ifdef _SEB_BUILD EstEID_log("SEB build"); PCCERT_CONTEXT pCertContextForEnumeration = NULL; int certificatesCount = 0; while(pCertContextForEnumeration = CertEnumCertificatesInStore(hCertStore, pCertContextForEnumeration)) { if(isValidForSigning(pCertContextForEnumeration)) { certificatesCount++; pCertContext = pCertContextForEnumeration; } } EstEID_log("Certificates count %i", certificatesCount); if(certificatesCount != 1) { pCertContext = CryptUIDlgSelectCertificate(&sel); } #else pCertContext = CryptUIDlgSelectCertificate(&sel); #endif if(!pCertContext) { EstEID_log("User didn't select sertificate"); throw CryptoException(ESTEID_USER_CANCEL); } loadCertContexts(pCertContext); if(pCertContext){ CertFreeCertificateContext(pCertContext); } if(hCertStore) { CertCloseStore(hCertStore, CERT_CLOSE_STORE_FORCE_FLAG); } }
void CEstEIDCertificate::readFromCertContext() { PCCERT_CONTEXT certContext = NULL; HCERTSTORE cert_store = NULL; cert_store = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, NULL, CERT_SYSTEM_STORE_CURRENT_USER | CERT_STORE_READONLY_FLAG, L"MY"); if(!cert_store){ throw CryptoException(); } if(!CertFindCertificateInStore(cert_store, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, 0, CERT_FIND_ANY, NULL, NULL)) { CertCloseStore(cert_store, CERT_CLOSE_STORE_FORCE_FLAG); throw CryptoException(); } while(certContext = CertFindCertificateInStore(cert_store, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, 0, CERT_FIND_ANY, NULL, certContext)) { BYTE keyUsage; CertGetIntendedKeyUsage(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, certContext->pCertInfo, &keyUsage, 1); if (keyUsage & CERT_NON_REPUDIATION_KEY_USAGE) { this->certificates.push_back(CertDuplicateCertificateContext(certContext)); } } //PCCERT_CONTEXT ct = CryptUIDlgSelectCertificateFromStore(cert_store, NULL, L"TIITEL", L"Vali cert:", NULL, 0, 0); //loadCertContexts(ct); CCertificateSelectionDlg *dlg = new CCertificateSelectionDlg(); dlg->setCertificate(this->certificates); INT_PTR selectedItem = dlg->DoModal(); EstEID_log("selected item index = %i", selectedItem); if(selectedItem == -1) { throw CryptoException(ESTEID_USER_CANCEL); } loadCertContexts(this->certificates[selectedItem]); if(certContext){ CertFreeCertificateContext(certContext); } if(cert_store) { CertCloseStore(cert_store, CERT_CLOSE_STORE_FORCE_FLAG); } }