CAPICertUpdate2.cpp
Go to the documentation of this file.00001
00010 #include "CAPICertUpdate2.h"
00011 #include "CAPIUtils.h"
00012 #include "ToolkitUtils.h"
00013 #include "components.h"
00014 #include "PKIFCacheErrors.h"
00015 #include "Buffer.h"
00016 #include "Certificate.h"
00017 #include "PKIFCacheException.h"
00018
00019 #include <atlbase.h>
00020 #include <sstream>
00021
00022
00024 struct CPKIFCAPICertUpdate2Impl
00025 {
00026 HCERTSTORE m_hSto;
00027 int m_nSysStoRegLoc;
00028 char* m_szStore;
00029 };
00031
00049 CPKIFCAPICertUpdate2::CPKIFCAPICertUpdate2(
00051 int sysStoRegLoc,
00053 const char* store)
00054 :m_impl (new CPKIFCAPICertUpdate2Impl), IPKIFCAPISource(sysStoRegLoc, store)
00055 {
00056 LOG_STRING_DEBUG("CPKIFCAPICertUpdate2::CPKIFCAPICertUpdate2(void)", TOOLKIT_SR_CAPICERTUPDATE, 0, this);
00057
00058 m_impl->m_hSto = NULL;
00059
00060 m_impl->m_nSysStoRegLoc = sysStoRegLoc;
00061
00062 m_impl->m_szStore = NULL;
00063 size_t len = 0;
00064 if(store)
00065 {
00066 len = strlen(store);
00067 m_impl->m_szStore = new char[len + 1];
00068
00069
00070 strcpy(m_impl->m_szStore, store);
00071 }
00072 }
00080 CPKIFCAPICertUpdate2::~CPKIFCAPICertUpdate2(void)
00081 {
00082 LOG_STRING_DEBUG("CPKIFCAPICertUpdate2::~CPKIFCAPICertUpdate2(void)", TOOLKIT_SR_CAPICERTUPDATE, 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 }
00106 void CPKIFCAPICertUpdate2::Initialize(void)
00107 {
00108 LOG_STRING_DEBUG("CPKIFCAPICertUpdate2::Initialize(void)", TOOLKIT_SR_CAPICERTUPDATE, 0, this);
00109
00110 if(NULL != m_impl->m_hSto)
00111 {
00112 LOG_STRING_WARN("Skipping initialization - CPKIFCAPICertUpdate2 instance already initialized", TOOLKIT_SR_CAPIREPOSITORY, COMMON_ALREADY_INITIALIZED, this);
00113 return;
00114 }
00115
00116 USES_CONVERSION;
00117 m_impl->m_hSto = CertOpenStore(CERT_STORE_PROV_SYSTEM, X509_ASN_ENCODING, NULL,
00118 CERT_STORE_OPEN_EXISTING_FLAG | m_impl->m_nSysStoRegLoc , T2OLE(m_impl->m_szStore));
00119 if(NULL == m_impl->m_hSto)
00120 {
00121 std::ostringstream os;
00122 os << "CertOpenStore failed: " << GetLastError();
00123 RAISE_CACHE_EXCEPTION(os.str().c_str(), thisComponent, CACHE_CERT_STORE_OPEN_FAILED, this)
00124 }
00125 }
00126
00141 void CPKIFCAPICertUpdate2::AddCertificate(CertType certType, const CPKIFCertificatePtr& cert)
00142 {
00143 LOG_STRING_DEBUG("CPKIFCAPICertUpdate2::AddCertificate(CertType certType, const CPKIFCertificatePtr& cert)", TOOLKIT_SR_CAPICERTUPDATE, 0, this);
00144
00145 if(NULL == m_impl->m_hSto)
00146 {
00147 RAISE_CACHE_EXCEPTION("CPKIFCAPICertUpdate2 instance not initialized.", thisComponent, COMMON_NOT_INITIALIZED, this)
00148 }
00149
00150 CPKIFBufferPtr certBuf = cert->Encoded();
00151 if(!CertAddEncodedCertificateToStore(m_impl->m_hSto, X509_ASN_ENCODING, certBuf->GetBuffer(), certBuf->GetLength(),CERT_STORE_ADD_NEWER_INHERIT_PROPERTIES, NULL))
00152 {
00153
00154 std::ostringstream os;
00155 os << "CertAddEncodedCertificateToStore failed: " << GetLastError();
00156 LOG_STRING_ERROR(os.str().c_str(), thisComponent, CACHE_UPDATE_FAILED, this);
00157 }
00158 else
00159 {
00160 LOG_STRING_DEBUG("Successfully added certificate to CAPI store", thisComponent, 0, this);
00161 }
00162 }
00163