00001
00009 #include "PKIFCAPICredential2.h"
00010 #include "ToolkitUtils.h"
00011
00012 #include "PKIFCAPIErrors.h"
00013 #include "PKIFCryptoException.h"
00014
00015 #include "Certificate.h"
00016
00017 #include <atlbase.h>
00018 #include <sstream>
00019 using namespace std;
00020
00028 CPKIFCAPICredential2::CPKIFCAPICredential2(
00030 const char* provider,
00032 int provType,
00034 int sysStoRegLoc)
00035 {
00036 LOG_STRING_DEBUG("CPKIFCAPICredential2::CPKIFCAPICredential2(void)", TOOLKIT_CRYPTO_CAPICRED, 0, this);
00037
00038 m_provType = provType;
00039 m_sysStoRegLoc = sysStoRegLoc;
00040 m_provider = NULL;
00041
00042 m_provider = NULL;
00043 size_t len = 0;
00044 if(provider)
00045 {
00046 len = strlen(provider);
00047 m_provider = new char[len + 1];
00048
00049
00050 strcpy(m_provider, provider);
00051 }
00052
00053 m_certContext = NULL;
00054 m_keyProviderInfo = NULL;
00055 m_password = NULL;
00056 m_nPasswordLen = 0;
00057 }
00058
00067 CPKIFCAPICredential2::~CPKIFCAPICredential2(void)
00068 {
00069 LOG_STRING_DEBUG("CPKIFCAPICredential2::~CPKIFCAPICredential2(void)", TOOLKIT_CRYPTO_CAPICRED, 0, this);
00070
00071 if(m_provider)
00072 delete[] m_provider;
00073
00074 if(NULL != m_certContext)
00075 {
00076 CertFreeCertificateContext(m_certContext); m_certContext = NULL;
00077 }
00078
00079 if(NULL != m_keyProviderInfo)
00080 {
00081 PKIFDelete(m_keyProviderInfo); m_keyProviderInfo = NULL;
00082 }
00083
00084 if(NULL != m_password)
00085 {
00086 memset(m_password, 0, m_nPasswordLen);
00087 PKIFDelete(m_password); m_password = NULL;
00088 }
00089 }
00090
00102 CPKIFCertificatePtr CPKIFCAPICredential2::GetCertificate() const
00103 {
00104 LOG_STRING_DEBUG("CPKIFCAPICredential2::GetCertificate()", TOOLKIT_CRYPTO_CAPICRED, 0, this);
00105
00106
00107 if(m_cacCert != (CPKIFCertificate*)NULL)
00108 return m_cacCert;
00109
00110
00111 if(NULL != m_certContext && NULL != m_certContext->pbCertEncoded && 0 != m_certContext->cbCertEncoded)
00112 {
00113
00114 CPKIFCertificatePtr tmp(new CPKIFCertificate);
00115 tmp->Decode(m_certContext->pbCertEncoded, m_certContext->cbCertEncoded);
00116
00117 CPKIFCAPICredential2* nonConst = const_cast<CPKIFCAPICredential2*>(this);
00118 nonConst->m_cacCert = tmp;
00119 }
00120
00121 return m_cacCert;
00122 }
00123
00135 void CPKIFCAPICredential2::SetPassword(
00137 unsigned char* password,
00139 int len)
00140 {
00141 LOG_STRING_DEBUG("CPKIFCAPICredential2::SetPassword(unsigned char* password, int len)", TOOLKIT_CRYPTO_CAPICRED, 0, this);
00142
00143
00144 if(NULL == password || 0 >= len)
00145 {
00146
00147 if(NULL != m_password)
00148 {
00149 memset(m_password, 0, m_nPasswordLen);
00150 PKIFDelete(m_password); m_password = NULL; m_nPasswordLen = 0;
00151 }
00152
00153 return;
00154 }
00155
00156
00157 unsigned char* tmpPassword = (unsigned char*)PKIFNew(len + 1);
00158 memcpy(tmpPassword, password, len);
00159 tmpPassword[len] = '\0';
00160
00161
00162 if(NULL != m_password)
00163 {
00164 memset(m_password, 0, m_nPasswordLen);
00165 PKIFDelete(m_password); m_password = NULL; m_nPasswordLen = 0;
00166 }
00167
00168
00169 m_password = tmpPassword;
00170 m_nPasswordLen = len;
00171 }
00172
00183 void CPKIFCAPICredential2::SetKeyProviderInfo() const
00184 {
00185 LOG_STRING_DEBUG("CPKIFCAPICredential2::SetKeyProviderInfo()", TOOLKIT_CRYPTO_CAPICRED, 0, this);
00186
00187 if(NULL == m_certContext)
00188 throw CPKIFCryptoException(thisComponent, PKIFCAPI_NO_KEY_ASSOCIATED, "No key has been associated with this credential object.");
00189
00190
00191 if(NULL != m_keyProviderInfo)
00192 return;
00193
00194 CRYPT_KEY_PROV_INFO* tmpKeyProviderInfo = NULL;
00195 DWORD providerInfoLen = 0;
00196
00197
00198 BOOL succ = CertGetCertificateContextProperty(m_certContext, CERT_KEY_PROV_INFO_PROP_ID,
00199 NULL, &providerInfoLen);
00200 if(succ)
00201 {
00202
00203 tmpKeyProviderInfo = (CRYPT_KEY_PROV_INFO *)PKIFNew(providerInfoLen);
00204 if(NULL == tmpKeyProviderInfo)
00205 {
00206 throw CPKIFCryptoException(thisComponent, COMMON_MEMORY_ALLOC_FAILURE);
00207 }
00208
00209
00210 succ = CertGetCertificateContextProperty(m_certContext,
00211 CERT_KEY_PROV_INFO_PROP_ID, (void *)tmpKeyProviderInfo, &providerInfoLen);
00212 if(!succ)
00213 {
00214 PKIFDelete(tmpKeyProviderInfo); tmpKeyProviderInfo = NULL;
00215
00216 std::ostringstream os;
00217 os << "CertGetCertificateContextProperty failed: " << GetLastError();
00218 RAISE_CRYPTO_EXCEPTION(os.str().c_str(), thisComponent, PKIFCAPI_KEY_PROV_INFO_FAILED, this);
00219 }
00220
00221
00222 const_cast<CPKIFCAPICredential2*>(this)->m_keyProviderInfo = tmpKeyProviderInfo;
00223 return;
00224 }
00225 else
00226 {
00227 std::ostringstream os;
00228 os << "CertGetCertificateContextProperty failed: " << GetLastError();
00229 RAISE_CRYPTO_EXCEPTION(os.str().c_str(), thisComponent, PKIFCAPI_KEY_PROV_INFO_FAILED, this);
00230 }
00231 }
00232
00241 bool CPKIFCAPICredential2::ProviderInfoMatches(
00243 const char* provider,
00245 int provType) const
00246 {
00247 LOG_STRING_DEBUG("CPKIFCAPICredential2::ProviderInfoMatches(const char* m_provider, int m_provType) const", TOOLKIT_CRYPTO_CAPICRED, 0, this);
00248
00249
00250 if(NULL == m_provider)
00251 return false;
00252
00253
00254 if(NULL == m_keyProviderInfo)
00255 SetKeyProviderInfo();
00256
00257 USES_CONVERSION;
00258
00259 if(0 == strcmp(provider, OLE2T(m_keyProviderInfo->pwszProvName)) &&
00260 provType == m_keyProviderInfo->dwProvType)
00261 return true;
00262 else
00263 return false;
00264 }