Gradual code migration to ATL and object-oriented approach
This commit is contained in:
79
atlcrypt.h
79
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<BYTE> &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 ATL
|
||||||
{
|
{
|
||||||
namespace Crypt
|
namespace Crypt
|
||||||
@@ -81,7 +102,7 @@ namespace ATL
|
|||||||
//
|
//
|
||||||
// CCertContext
|
// CCertContext
|
||||||
//
|
//
|
||||||
class CCertContext : public ATL::CHandleT<PCCERT_CONTEXT>
|
class CCertContext : public ATL::CObjectWithHandleT<PCCERT_CONTEXT>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~CCertContext() throw()
|
virtual ~CCertContext() throw()
|
||||||
@@ -111,7 +132,7 @@ namespace ATL
|
|||||||
//
|
//
|
||||||
// CCertStore
|
// CCertStore
|
||||||
//
|
//
|
||||||
class CCertStore : public ATL::CHandleT<HCERTSTORE>
|
class CCertStore : public ATL::CObjectWithHandleT<HCERTSTORE>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~CCertStore() throw()
|
virtual ~CCertStore() throw()
|
||||||
@@ -141,7 +162,7 @@ namespace ATL
|
|||||||
//
|
//
|
||||||
// CContext
|
// CContext
|
||||||
//
|
//
|
||||||
class CContext : public ATL::CHandleT<HCRYPTPROV>
|
class CContext : public ATL::CObjectWithHandleT<HCRYPTPROV>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~CContext() throw()
|
virtual ~CContext() throw()
|
||||||
@@ -171,7 +192,7 @@ namespace ATL
|
|||||||
//
|
//
|
||||||
// CHash
|
// CHash
|
||||||
//
|
//
|
||||||
class CHash : public ATL::CHandleT<HCRYPTHASH>
|
class CHash : public ATL::CObjectWithHandleT<HCRYPTHASH>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~CHash() throw()
|
virtual ~CHash() throw()
|
||||||
@@ -196,5 +217,55 @@ namespace ATL
|
|||||||
CryptDestroyHash(m_h);
|
CryptDestroyHash(m_h);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
//
|
||||||
|
// CKey
|
||||||
|
//
|
||||||
|
class CKey : public ATL::CObjectWithHandleT<HCRYPTKEY>
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
8
atlex.h
8
atlex.h
@@ -25,19 +25,19 @@
|
|||||||
namespace ATL
|
namespace ATL
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
// CHandleT
|
// CObjectWithHandleT
|
||||||
//
|
//
|
||||||
template <class T>
|
template <class T>
|
||||||
class CHandleT
|
class CObjectWithHandleT
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
typedef T HANDLE;
|
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)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user