00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00026 #ifndef OW32_CertStore_h
00027 #define OW32_CertStore_h
00028
00029 #include <OW32/OW32Libs.h>
00030 #include <OW32/XHCERTSTORE.h>
00031 #ifndef OW32_NO_STL
00032 #include <OW32/tstl.h>
00033 #endif
00034
00035 namespace OW32
00036 {
00037
00038 #ifndef MY_ENCODING_TYPE
00039 #define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
00040 #endif
00041
00043 class OW32_LIB_EXPORT CCertStore
00044 {
00045 private:
00046 CCertStore(const CCertStore& );
00047 CCertStore& operator=(const CCertStore& );
00048
00049 XHCERTSTORE m_hCertStore;
00050
00051 public:
00053 CCertStore() {}
00054
00058 CCertStore(HCERTSTORE hCertStore) :
00059 m_hCertStore(hCertStore)
00060 {
00061 }
00062
00064 HCERTSTORE Detach()
00065 {
00066 return m_hCertStore.Detach();
00067 }
00068
00072 void OpenFromHandle(HCERTSTORE hCertStore)
00073 {
00074 m_hCertStore = hCertStore;
00075 }
00076
00078 BOOL Open(LPCSTR lpszStoreProvider = CERT_STORE_PROV_SYSTEM,
00079 DWORD dwMsgAndCertEncodingType = PKCS_7_ASN_ENCODING | X509_ASN_ENCODING,
00080 HCRYPTPROV hCryptProv = NULL,
00081 DWORD dwFlags = CERT_SYSTEM_STORE_CURRENT_USER,
00082 const void* pvPara = L"MY")
00083 {
00084 m_hCertStore = CertOpenStore(lpszStoreProvider, dwMsgAndCertEncodingType,
00085 hCryptProv, dwFlags, pvPara);
00086 return (m_hCertStore != NULL);
00087 }
00088
00090 BOOL OpenCurrentUser(LPCWSTR lpStore = L"MY")
00091 {
00092 return Open(CERT_STORE_PROV_SYSTEM, PKCS_7_ASN_ENCODING|X509_ASN_ENCODING,
00093 NULL, CERT_SYSTEM_STORE_CURRENT_USER, lpStore);
00094 }
00095
00097 BOOL OpenLocalMachine(LPCWSTR lpStore = L"MY")
00098 {
00099 return Open(CERT_STORE_PROV_SYSTEM, MY_ENCODING_TYPE,
00100 NULL, CERT_SYSTEM_STORE_LOCAL_MACHINE, lpStore);
00101 }
00102
00104 BOOL OpenMemoryStore()
00105 {
00106 return Open(CERT_STORE_PROV_MEMORY, MY_ENCODING_TYPE,
00107 NULL, 0, NULL);
00108 }
00109
00111 void Close(DWORD dwFlags=0)
00112 {
00113 m_hCertStore.Close(dwFlags);
00114 }
00115
00119 BOOL FindCertificateBySubject(LPCWSTR subject, PCCERT_CONTEXT* ppCertContext)
00120 {
00121 *ppCertContext = CertFindCertificateInStore(
00122 m_hCertStore, MY_ENCODING_TYPE, 0, CERT_FIND_SUBJECT_STR, subject, NULL);
00123 if (!*ppCertContext)
00124 return FALSE;
00125 return TRUE;
00126 }
00127
00128
00129 BOOL FindCertificateByCertID(CERT_ID* pCertID, PCCERT_CONTEXT* ppCertContext)
00130 {
00131 *ppCertContext = CertFindCertificateInStore(
00132 m_hCertStore, MY_ENCODING_TYPE, 0, CERT_FIND_CERT_ID, pCertID, NULL);
00133 if (!*ppCertContext)
00134 return FALSE;
00135 return TRUE;
00136 }
00137
00139 BOOL FindCertificateByHash(CRYPT_HASH_BLOB* hash, PCCERT_CONTEXT* ppCertContext)
00140 {
00141 *ppCertContext = CertFindCertificateInStore(
00142 m_hCertStore, MY_ENCODING_TYPE, 0, CERT_FIND_HASH, hash, NULL);
00143 if (!*ppCertContext)
00144 return FALSE;
00145 return TRUE;
00146 }
00147
00149 BOOL FindCertificateByHash(BYTE* hash, DWORD hashLen, PCCERT_CONTEXT* ppCertContext)
00150 {
00151 CRYPT_HASH_BLOB hashBlob;
00152 hashBlob.cbData = hashLen;
00153 hashBlob.pbData = hash;
00154 return FindCertificateByHash(&hashBlob, ppCertContext);
00155 }
00156
00158 BOOL FindCertificateByCertID(LPCSTR issuer,
00159 LPCSTR serial, PCCERT_CONTEXT* ppCertContext);
00160
00161
00162 static BOOL NameToStr(LPTSTR buf, DWORD cch, CERT_NAME_BLOB* pName,
00163 DWORD dwStrType = CERT_X500_NAME_STR);
00164
00165 #ifndef OW32_NO_STL
00166
00167 static BOOL NameToStr(std::_tstring& str, CERT_NAME_BLOB* pName,
00168 DWORD dwStrType = CERT_X500_NAME_STR);
00169 #endif
00170
00171 BOOL AddCertificateContext(PCCERT_CONTEXT pCertContext,
00172 DWORD dwAddDisposition = CERT_STORE_ADD_NEW,
00173 PCCERT_CONTEXT* ppStoreContext = NULL)
00174 {
00175 return CertAddCertificateContextToStore(m_hCertStore,
00176 pCertContext, dwAddDisposition, ppStoreContext);
00177 }
00178
00180 PCCERT_CONTEXT EnumCertificates(PCCERT_CONTEXT pPrevCertContext)
00181 {
00182 return CertEnumCertificatesInStore(m_hCertStore, pPrevCertContext);
00183 }
00184
00186 operator HCERTSTORE() { return m_hCertStore; }
00187 HCERTSTORE* operator&() { return &m_hCertStore; }
00188 };
00189
00190 }
00191
00192 #endif // OW32_CertStore_h