diff --git a/atl.vcxproj b/atl.vcxproj index 912d52c..be2a208 100644 --- a/atl.vcxproj +++ b/atl.vcxproj @@ -104,6 +104,7 @@ + diff --git a/atl.vcxproj.filters b/atl.vcxproj.filters index 037c86c..37be935 100644 --- a/atl.vcxproj.filters +++ b/atl.vcxproj.filters @@ -34,5 +34,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/atlcrypt.h b/atlcrypt.h index c3b28b6..57e7b5b 100644 --- a/atlcrypt.h +++ b/atlcrypt.h @@ -19,6 +19,8 @@ #pragma once +#include "atlex.h" +#include #include #include @@ -51,113 +53,148 @@ inline DWORD CertGetNameStringW(PCCERT_CONTEXT pCertContext, DWORD dwType, DWORD } +inline BOOL CryptGetHashParam(__in HCRYPTHASH hHash, __in DWORD dwParam, __out ATL::CAtlArray &aData, __in DWORD dwFlags) +{ + DWORD dwHashSize; + + if (CryptGetHashParam(hHash, dwParam, NULL, &dwHashSize, dwFlags)) { + if (aData.SetCount(dwHashSize)) { + if (CryptGetHashParam(hHash, dwParam, aData.GetData(), &dwHashSize, dwFlags)) { + return TRUE; + } else { + aData.SetCount(0); + return FALSE; + } + } else { + SetLastError(ERROR_OUTOFMEMORY); + return FALSE; + } + } else + return FALSE; +} + + namespace ATL { namespace Crypt { - // // CCertContext // - class CCertContext + class CCertContext : public ATL::CHandleT { public: - inline CCertContext() throw() : m_pCertContext(NULL) + virtual ~CCertContext() throw() { - } - - inline CCertContext(PCCERT_CONTEXT p) throw() : m_pCertContext(p) - { - } - - inline ~CCertContext() throw() - { - if (m_pCertContext) - CertFreeCertificateContext(m_pCertContext); - } - - inline operator PCCERT_CONTEXT() const throw() - { - return m_pCertContext; - } - - inline const CERT_CONTEXT& operator*() const - { - ATLENSURE(m_pCertContext != NULL); - return *m_pCertContext; - } - - inline PCCERT_CONTEXT* operator&() throw() - { - ATLASSERT(m_pCertContext == NULL); - return &m_pCertContext; - } - - inline PCCERT_CONTEXT operator->() const throw() - { - ATLASSERT(m_pCertContext != NULL); - return m_pCertContext; - } - - inline bool operator!() const throw() - { - return m_pCertContext == NULL; - } - - inline bool operator<(_In_opt_ PCCERT_CONTEXT p) const throw() - { - return m_pCertContext < p; - } - - inline bool operator!=(_In_opt_ PCCERT_CONTEXT p) const - { - return !operator==(p); - } - - inline bool operator==(_In_opt_ PCCERT_CONTEXT p) const throw() - { - return m_pCertContext == p; - } - - inline void Attach(_In_opt_ PCCERT_CONTEXT p) throw() - { - if (m_pCertContext) - CertFreeCertificateContext(m_pCertContext); - m_pCertContext = p; - } - - inline PCCERT_CONTEXT Detach() throw() - { - PCCERT_CONTEXT p = m_pCertContext; - m_pCertContext = NULL; - return p; + if (m_h) + CertFreeCertificateContext(m_h); } inline BOOL Create(_In_ DWORD dwCertEncodingType, _In_ const BYTE *pbCertEncoded, _In_ DWORD cbCertEncoded) throw() { - PCCERT_CONTEXT p; - - p = CertCreateCertificateContext(dwCertEncodingType, pbCertEncoded, cbCertEncoded); - if (!p) return FALSE; - - if (m_pCertContext) - CertFreeCertificateContext(m_pCertContext); - m_pCertContext = p; - return TRUE; - } - - inline BOOL Free() throw() - { - if (m_pCertContext) { - BOOL bResult = CertFreeCertificateContext(m_pCertContext); - m_pCertContext = NULL; - return bResult; - } else + HANDLE h = CertCreateCertificateContext(dwCertEncodingType, pbCertEncoded, cbCertEncoded); + if (h) { + Attach(h); return TRUE; + } else + return FALSE; } protected: - PCCERT_CONTEXT m_pCertContext; + virtual void InternalFree() + { + CertFreeCertificateContext(m_h); + } + }; + + + // + // CCertStore + // + class CCertStore : public ATL::CHandleT + { + public: + virtual ~CCertStore() throw() + { + if (m_h) + CertCloseStore(m_h, 0); + } + + inline BOOL Create(__in LPCSTR lpszStoreProvider, __in DWORD dwEncodingType, __in_opt HCRYPTPROV_LEGACY hCryptProv, __in DWORD dwFlags, __in_opt const void *pvPara) throw() + { + HANDLE h = CertOpenStore(lpszStoreProvider, dwEncodingType, hCryptProv, dwFlags, pvPara); + if (h) { + Attach(h); + return TRUE; + } else + return FALSE; + } + + protected: + virtual void InternalFree() + { + CertCloseStore(m_h, 0); + } + }; + + + // + // CContext + // + class CContext : public ATL::CHandleT + { + public: + virtual ~CContext() throw() + { + if (m_h) + CryptReleaseContext(m_h, 0); + } + + inline BOOL Create(__in_opt LPCTSTR szContainer, __in_opt LPCTSTR szProvider, __in DWORD dwProvType, __in DWORD dwFlags) throw() + { + HANDLE h; + if (CryptAcquireContext(&h, szContainer, szProvider, dwProvType, dwFlags)) { + Attach(h); + return TRUE; + } else + return FALSE; + } + + protected: + virtual void InternalFree() + { + CryptReleaseContext(m_h, 0); + } + }; + + + // + // CHash + // + class CHash : public ATL::CHandleT + { + public: + virtual ~CHash() throw() + { + if (m_h) + CryptDestroyHash(m_h); + } + + inline BOOL Create(__in HCRYPTPROV hProv, __in ALG_ID Algid, __in HCRYPTKEY hKey, __in DWORD dwFlags) throw() + { + HANDLE h; + if (CryptCreateHash(hProv, Algid, hKey, dwFlags, &h)) { + Attach(h); + return TRUE; + } else + return FALSE; + } + + protected: + virtual void InternalFree() + { + CryptDestroyHash(m_h); + } }; } } diff --git a/atlex.h b/atlex.h new file mode 100644 index 0000000..e0ae75d --- /dev/null +++ b/atlex.h @@ -0,0 +1,115 @@ +/* + Copyright 1991-2015 Amebis + + This file is part of libatl. + + Setup is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Setup is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Setup. If not, see . +*/ + +#pragma once + +#include + + +namespace ATL +{ + // + // CHandleT + // + template + class CHandleT + { + public: + typedef T HANDLE; + + inline CHandleT() throw() : m_h(NULL) + { + } + + inline CHandleT(T h) throw() : m_h(h) + { + } + + inline operator T() const throw() + { + return m_h; + } + + inline T*& operator*() const + { + ATLENSURE(m_h != NULL); + return *m_h; + } + + inline T* operator&() throw() + { + ATLASSERT(m_h == NULL); + return &m_h; + } + + inline T operator->() const throw() + { + ATLASSERT(m_h != NULL); + return m_h; + } + + inline bool operator!() const throw() + { + return m_h == NULL; + } + + inline bool operator<(_In_opt_ T h) const throw() + { + return m_h < h; + } + + inline bool operator!=(_In_opt_ T h) const + { + return !operator==(h); + } + + inline bool operator==(_In_opt_ T h) const throw() + { + return m_h == h; + } + + inline void Attach(_In_opt_ T h) throw() + { + if (m_h) + InternalFree(); + m_h = h; + } + + inline T Detach() throw() + { + T h = m_h; + m_h = NULL; + return h; + } + + inline void Free() throw() + { + if (m_h) { + InternalFree(); + m_h = NULL; + } + } + + protected: + virtual void InternalFree() = 0; + + protected: + T m_h; + }; +}