00001
00009 #include "CAPITrustRootCRLRepository2.h"
00010 #include "CAPIUtils.h"
00011 #include "ToolkitUtils.h"
00012 #include "PKIFCacheErrors.h"
00013 #include "PKIFCacheException.h"
00014
00015 #include "Buffer.h"
00016 #include "Certificate.h"
00017 #include "Name.h"
00018 #include "CRL.h"
00019 #include "GottaMatch.h"
00020
00021 #include <atlbase.h>
00022 #include <sstream>
00023
00025 struct CPKIFCAPITrustRootCRLRepository2Impl
00026 {
00027 HCERTSTORE m_hSto;
00028 int m_nSysStoRegLoc;
00029 char* m_szStore;
00030 };
00032
00050 CPKIFCAPITrustRootCRLRepository2::CPKIFCAPITrustRootCRLRepository2(
00052 int sysStoRegLoc,
00054 const char* store)
00055 :m_impl (new CPKIFCAPITrustRootCRLRepository2Impl), IPKIFCAPISource(sysStoRegLoc, store)
00056 {
00057 LOG_STRING_DEBUG("CPKIFCAPITrustRootCRLRepository2::CPKIFCAPITrustRootCRLRepository2(void)", TOOLKIT_SR_CAPITRUSTROOTCRLSTORE, 0, this);
00058
00059 m_impl->m_hSto = NULL;
00060
00061 m_impl->m_nSysStoRegLoc = sysStoRegLoc;
00062
00063 m_impl->m_szStore = NULL;
00064 size_t len = 0;
00065 if(store)
00066 {
00067 len = strlen(store);
00068 m_impl->m_szStore = new char[len + 1];
00069
00070 strcpy(m_impl->m_szStore, store);
00071 }
00072 }
00080 CPKIFCAPITrustRootCRLRepository2::~CPKIFCAPITrustRootCRLRepository2(void)
00081 {
00082 LOG_STRING_DEBUG("CPKIFCAPITrustRootCRLRepository2::~CPKIFCAPITrustRootCRLRepository2(void)", TOOLKIT_SR_CAPITRUSTROOTCRLSTORE, 0, this);
00083
00084 if(m_impl->m_szStore)
00085 delete[]m_impl-> m_szStore;
00086
00087 if(NULL != m_impl->m_hSto)
00088 {
00089 CertCloseStore(m_impl->m_hSto, 0); m_impl->m_hSto = NULL;
00090 }
00091
00092 delete m_impl;
00093 m_impl = NULL;
00094 }
00104 void CPKIFCAPITrustRootCRLRepository2::Initialize(void)
00105 {
00106 LOG_STRING_DEBUG("CPKIFCAPITrustRootCRLRepository2::Initialize(void)", TOOLKIT_SR_CAPITRUSTROOTCRLSTORE, 0, this);
00107
00108 if(NULL != m_impl->m_hSto)
00109 {
00110 LOG_STRING_WARN("Skipping initialization - CPKIFCAPITrustRootCRLRepository2 instance already initialized", TOOLKIT_SR_CAPIREPOSITORY, COMMON_ALREADY_INITIALIZED, this);
00111 return;
00112 }
00113
00114 USES_CONVERSION;
00115
00116 m_impl->m_hSto = CertOpenStore(CERT_STORE_PROV_SYSTEM, X509_ASN_ENCODING, NULL,
00117 CERT_STORE_OPEN_EXISTING_FLAG | CERT_STORE_READONLY_FLAG | m_impl->m_nSysStoRegLoc , T2OLE(m_impl->m_szStore));
00118 if(NULL == m_impl->m_hSto)
00119 {
00120 std::ostringstream os;
00121 os << "CertOpenStore failed: " << GetLastError();
00122 RAISE_CACHE_EXCEPTION(os.str().c_str(), thisComponent, CACHE_CERT_STORE_OPEN_FAILED, this)
00123 }
00124 }
00140 void CPKIFCAPITrustRootCRLRepository2::GetCRLs(
00142 const CPKIFCertificatePtr& cert,
00144 CPKIFCRLList& crlList,
00146 PKIInfoSource source)
00147 {
00148 LOG_STRING_DEBUG("CPKIFCAPITrustRootCRLRepository2::GetCRLs(const CPKIFCertificatePtr& cert, CPKIFCRLList& crlList, PKIInfoSource source)", TOOLKIT_SR_CAPITRUSTROOTCRLSTORE, 0, this);
00149
00150
00151 if(REMOTE == source)
00152 {
00153 LOG_STRING_DEBUG("Skipping CPKIFCAPITrustRootCRLRepository2 - searching REMOTE sources only", thisComponent, 0, this);
00154 return;
00155 }
00156
00157 if(NULL == m_impl->m_hSto)
00158 {
00159 RAISE_CACHE_EXCEPTION("CPKIFCAPITrustRootCRLRepository2 instance not initialized.", thisComponent, COMMON_NOT_INITIALIZED, this)
00160 }
00161
00162 const size_t origSize = crlList.size();
00163
00164 PCCRL_CONTEXT crl = NULL;
00165 PCCERT_CONTEXT certCtx = NULL;
00166 PCCERT_CONTEXT issuerCtx = NULL;
00167
00168 if(cert == (CPKIFCertificate*)NULL)
00169 {
00170 RAISE_CACHE_EXCEPTION("NULL certificate passed to GetCRLs.", thisComponent, COMMON_INVALID_INPUT, this)
00171 }
00172
00173 certCtx = CertCreateCertificateContext(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, cert->Encoded()->GetBuffer(), cert->Encoded()->GetLength());
00174 if(NULL == certCtx)
00175 {
00176 std::ostringstream os;
00177 os << "Failed to find a CRL issued by: " << cert->Issuer()->ToString();
00178 LOG_STRING_ERROR(os.str().c_str(), thisComponent, CACHE_PARSE_ERROR, this)
00179 return;
00180 }
00181
00182 issuerCtx = CertFindCertificateInStore(m_impl->m_hSto, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
00183 0, CERT_FIND_SUBJECT_NAME, &certCtx->pCertInfo->Issuer, NULL);
00184 CertFreeCertificateContext(certCtx); certCtx = NULL;
00185 if(NULL == issuerCtx)
00186 {
00187 std::ostringstream os;
00188 os << "Failed to create certificate context: " << GetLastError();
00189 LOG_STRING_INFO(os.str().c_str(), thisComponent, 0, this)
00190 return;
00191 }
00192
00193 CPKIFCRL* cacCRL = NULL;
00194 do
00195 {
00196
00197 crl = CertFindCRLInStore(m_impl->m_hSto, 0, 0, CRL_FIND_ISSUED_BY, issuerCtx, crl);
00198 if(NULL == crl)
00199 break;
00200
00201
00202 cacCRL = new CPKIFCRL();
00203 CPKIFCRLPtr tmpCRL(cacCRL);
00204 try
00205 {
00206 tmpCRL->Decode(crl->pbCrlEncoded, crl->cbCrlEncoded);
00207 }
00208 catch(CPKIFException& )
00209 {
00210
00211
00212
00213
00214
00215 std::ostringstream os;
00216 os << "Failed to parse CRL from CAPI store searching for certificates CRLs issued by: " << cert->Issuer()->ToString();
00217 LOG_STRING_ERROR(os.str().c_str(), thisComponent, CACHE_PARSE_ERROR, this)
00218 }
00219
00220 GottaMatch<CPKIFCRLPtr> gm;
00221 gm.SetRHS(tmpCRL);
00222 if(crlList.end() == find_if(crlList.begin(), crlList.end(), gm))
00223 crlList.push_back(tmpCRL);
00224
00225 }while(NULL != crl);
00226
00227 CertFreeCertificateContext(issuerCtx);
00228
00229
00230 if(origSize != crlList.size())
00231 {
00232 std::ostringstream os;
00233 os << "Found one or more CRLs issued by: " << cert->Issuer()->ToString();
00234 LOG_STRING_DEBUG(os.str().c_str(), thisComponent, 0, this);
00235 }
00236 else
00237 {
00238 std::ostringstream os;
00239 os << "Failed to find a CRL issued by: " << cert->Issuer()->ToString();
00240 LOG_STRING_INFO(os.str().c_str(), thisComponent, 0, this);
00241 }
00242 }
00243
00244