diff --git a/atlcrypt.h b/atlcrypt.h index 57e7b5b..19c3d60 100644 --- a/atlcrypt.h +++ b/atlcrypt.h @@ -74,6 +74,27 @@ inline BOOL CryptGetHashParam(__in HCRYPTHASH hHash, __in DWORD dwParam, __out } +inline BOOL CryptExportKey(__in HCRYPTKEY hKey, __in HCRYPTKEY hExpKey, __in DWORD dwBlobType, __in DWORD dwFlags, __out ATL::CAtlArray &aData) +{ + DWORD dwKeyBLOBSize; + + if (CryptExportKey(hKey, hExpKey, dwBlobType, dwFlags, NULL, &dwKeyBLOBSize)) { + if (aData.SetCount(dwKeyBLOBSize)) { + if (CryptExportKey(hKey, hExpKey, dwBlobType, dwFlags, aData.GetData(), &dwKeyBLOBSize)) { + return TRUE; + } else { + aData.SetCount(0); + return FALSE; + } + } else { + SetLastError(ERROR_OUTOFMEMORY); + return FALSE; + } + } else + return FALSE; +} + + namespace ATL { namespace Crypt @@ -81,7 +102,7 @@ namespace ATL // // CCertContext // - class CCertContext : public ATL::CHandleT + class CCertContext : public ATL::CObjectWithHandleT { public: virtual ~CCertContext() throw() @@ -111,7 +132,7 @@ namespace ATL // // CCertStore // - class CCertStore : public ATL::CHandleT + class CCertStore : public ATL::CObjectWithHandleT { public: virtual ~CCertStore() throw() @@ -141,7 +162,7 @@ namespace ATL // // CContext // - class CContext : public ATL::CHandleT + class CContext : public ATL::CObjectWithHandleT { public: virtual ~CContext() throw() @@ -171,7 +192,7 @@ namespace ATL // // CHash // - class CHash : public ATL::CHandleT + class CHash : public ATL::CObjectWithHandleT { public: virtual ~CHash() throw() @@ -196,5 +217,55 @@ namespace ATL CryptDestroyHash(m_h); } }; + + + // + // CKey + // + class CKey : public ATL::CObjectWithHandleT + { + public: + virtual ~CKey() throw() + { + if (m_h) + CryptDestroyKey(m_h); + } + + inline BOOL Generate(__in HCRYPTPROV hProv, __in ALG_ID Algid, __in DWORD dwFlags) throw() + { + HANDLE h; + if (CryptGenKey(hProv, Algid, dwFlags, &h)) { + Attach(h); + return TRUE; + } else + return FALSE; + } + + inline BOOL Import(__in HCRYPTPROV hProv, __in_bcount(dwDataLen) CONST BYTE *pbData, __in DWORD dwDataLen, __in HCRYPTKEY hPubKey, __in DWORD dwFlags) throw() + { + HANDLE h; + if (CryptImportKey(hProv, pbData, dwDataLen, hPubKey, dwFlags, &h)) { + Attach(h); + return TRUE; + } else + return FALSE; + } + + inline BOOL ImportPublic(__in HCRYPTPROV hCryptProv, __in DWORD dwCertEncodingType, __in PCERT_PUBLIC_KEY_INFO pInfo) throw() + { + HANDLE h; + if (CryptImportPublicKeyInfo(hCryptProv, dwCertEncodingType, pInfo, &h)) { + Attach(h); + return TRUE; + } else + return FALSE; + } + + protected: + virtual void InternalFree() + { + CryptDestroyKey(m_h); + } + }; } } diff --git a/atlex.h b/atlex.h index e0ae75d..dc29262 100644 --- a/atlex.h +++ b/atlex.h @@ -25,19 +25,19 @@ namespace ATL { // - // CHandleT + // CObjectWithHandleT // template - class CHandleT + class CObjectWithHandleT { public: typedef T HANDLE; - inline CHandleT() throw() : m_h(NULL) + inline CObjectWithHandleT() throw() : m_h(NULL) { } - inline CHandleT(T h) throw() : m_h(h) + inline CObjectWithHandleT(T h) throw() : m_h(h) { }