2 Copyright 1991-2017 Amebis
4 This file is part of atlex.
6 atlex is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
11 atlex is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with atlex. If not, see <http://www.gnu.org/licenses/>.
28 /// \defgroup ATLCryptoAPI Cryptography API
29 /// Integrates ATL classes with Microsoft Cryptography API
34 /// Obtains the subject or issuer name from a certificate [CERT_CONTEXT](https://msdn.microsoft.com/en-us/library/windows/desktop/aa377189.aspx) structure and stores it in a ATL::CAtlStringA string.
36 /// \sa [CertGetNameString function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376086.aspx)
38 inline DWORD
CertGetNameStringA(_In_ PCCERT_CONTEXT pCertContext
, _In_ DWORD dwType
, _In_ DWORD dwFlags
, _In_
void *pvTypePara
, _Out_
ATL::CAtlStringA
&sNameString
)
40 // Query the final string length first.
41 DWORD dwSize
= ::CertGetNameStringA(pCertContext
, dwType
, dwFlags
, pvTypePara
, NULL
, 0);
43 // Allocate buffer on heap to format the string data into and read it.
44 LPSTR szBuffer
= sNameString
.GetBuffer(dwSize
);
45 if (!szBuffer
) return ERROR_OUTOFMEMORY
;
46 dwSize
= ::CertGetNameStringA(pCertContext
, dwType
, dwFlags
, pvTypePara
, szBuffer
, dwSize
);
47 sNameString
.ReleaseBuffer(dwSize
);
53 /// Obtains the subject or issuer name from a certificate [CERT_CONTEXT](https://msdn.microsoft.com/en-us/library/windows/desktop/aa377189.aspx) structure and stores it in a ATL::CAtlStringW string.
55 /// \sa [CertGetNameString function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376086.aspx)
57 inline DWORD
CertGetNameStringW(_In_ PCCERT_CONTEXT pCertContext
, _In_ DWORD dwType
, _In_ DWORD dwFlags
, _In_
void *pvTypePara
, _Out_
ATL::CAtlStringW
&sNameString
)
59 // Query the final string length first.
60 DWORD dwSize
= ::CertGetNameStringW(pCertContext
, dwType
, dwFlags
, pvTypePara
, NULL
, 0);
62 // Allocate buffer on heap to format the string data into and read it.
63 LPWSTR szBuffer
= sNameString
.GetBuffer(dwSize
);
64 if (!szBuffer
) return ERROR_OUTOFMEMORY
;
65 dwSize
= ::CertGetNameStringW(pCertContext
, dwType
, dwFlags
, pvTypePara
, szBuffer
, dwSize
);
66 sNameString
.ReleaseBuffer(dwSize
);
72 /// Retrieves data that governs the operations of a hash object. The actual hash value can be retrieved by using this function.
74 /// \sa [CryptGetHashParam function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379947.aspx)
76 inline BOOL
CryptGetHashParam(_In_ HCRYPTHASH hHash
, _In_ DWORD dwParam
, _Out_
ATL::CAtlArray
<BYTE
> &aData
, _In_ DWORD dwFlags
)
80 if (CryptGetHashParam(hHash
, dwParam
, NULL
, &dwHashSize
, dwFlags
)) {
81 if (aData
.SetCount(dwHashSize
)) {
82 if (CryptGetHashParam(hHash
, dwParam
, aData
.GetData(), &dwHashSize
, dwFlags
)) {
89 SetLastError(ERROR_OUTOFMEMORY
);
98 /// Exports a cryptographic key or a key pair from a cryptographic service provider (CSP) in a secure manner.
100 /// \sa [CryptExportKey function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379931.aspx)
102 inline BOOL
CryptExportKey(_In_ HCRYPTKEY hKey
, _In_ HCRYPTKEY hExpKey
, _In_ DWORD dwBlobType
, _In_ DWORD dwFlags
, _Out_
ATL::CAtlArray
<BYTE
> &aData
)
106 if (CryptExportKey(hKey
, hExpKey
, dwBlobType
, dwFlags
, NULL
, &dwKeyBLOBSize
)) {
107 if (aData
.SetCount(dwKeyBLOBSize
)) {
108 if (CryptExportKey(hKey
, hExpKey
, dwBlobType
, dwFlags
, aData
.GetData(), &dwKeyBLOBSize
)) {
115 SetLastError(ERROR_OUTOFMEMORY
);
129 /// \addtogroup ATLCryptoAPI
133 /// PCCERT_CONTEXT wrapper class
135 class CCertContext
: public ATL::CObjectWithHandleDuplT
<PCCERT_CONTEXT
>
139 /// Destroys the certificate context.
141 /// \sa [CertFreeCertificateContext function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376075.aspx)
143 virtual ~CCertContext()
146 CertFreeCertificateContext(m_h
);
150 /// Creates the certificate context.
153 /// - TRUE when creation succeeds;
154 /// - FALSE when creation fails. For extended error information, call `GetLastError()`.
155 /// \sa [CertCreateCertificateContext function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376033.aspx)
157 inline BOOL
Create(_In_ DWORD dwCertEncodingType
, _In_
const BYTE
*pbCertEncoded
, _In_ DWORD cbCertEncoded
)
159 HANDLE h
= CertCreateCertificateContext(dwCertEncodingType
, pbCertEncoded
, cbCertEncoded
);
169 /// Destroys the certificate context.
171 /// \sa [CertFreeCertificateContext function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376075.aspx)
173 virtual void InternalFree()
175 CertFreeCertificateContext(m_h
);
179 /// Duplicates the certificate context.
181 /// \param[in] h Object handle of existing certificate context
182 /// \return Duplicated certificate context handle
183 /// \sa [CertDuplicateCertificateContext function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376045.aspx)
185 virtual HANDLE
InternalDuplicate(_In_ HANDLE h
) const
187 return CertDuplicateCertificateContext(h
);
193 /// PCCERT_CHAIN_CONTEXT wrapper class
195 class CCertChainContext
: public ATL::CObjectWithHandleDuplT
<PCCERT_CHAIN_CONTEXT
>
199 /// Destroys the certificate chain context.
201 /// \sa [CertFreeCertificateChain function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376073.aspx)
203 virtual ~CCertChainContext()
206 CertFreeCertificateChain(m_h
);
210 /// Creates the certificate chain context.
213 /// - TRUE when creation succeeds;
214 /// - FALSE when creation fails. For extended error information, call `GetLastError()`.
215 /// \sa [CertGetCertificateChain function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376078.aspx)
217 inline BOOL
Create(_In_opt_ HCERTCHAINENGINE hChainEngine
, _In_ PCCERT_CONTEXT pCertContext
, _In_opt_ LPFILETIME pTime
, _In_opt_ HCERTSTORE hAdditionalStore
, _In_ PCERT_CHAIN_PARA pChainPara
, _In_ DWORD dwFlags
, __reserved LPVOID pvReserved
)
220 if (CertGetCertificateChain(hChainEngine
, pCertContext
, pTime
, hAdditionalStore
, pChainPara
, dwFlags
, pvReserved
, &h
)) {
229 /// Destroys the certificate chain context.
231 /// \sa [CertFreeCertificateChain function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376073.aspx)
233 virtual void InternalFree()
235 CertFreeCertificateChain(m_h
);
239 /// Duplicates the certificate chain context.
241 /// \param[in] h Object handle of existing certificate chain context
242 /// \return Duplicated certificate chain context handle
243 /// \sa [CertDuplicateCertificateContext function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376045.aspx)
245 virtual HANDLE
InternalDuplicate(_In_ HANDLE h
) const
247 return CertDuplicateCertificateChain(h
);
253 /// HCERTSTORE wrapper class
255 class CCertStore
: public ATL::CObjectWithHandleT
<HCERTSTORE
>
259 /// Closes the certificate store.
261 /// \sa [CertCloseStore function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376026.aspx)
263 virtual ~CCertStore()
266 CertCloseStore(m_h
, 0);
270 /// Opens the certificate store.
273 /// - TRUE when creation succeeds;
274 /// - FALSE when creation fails. For extended error information, call `GetLastError()`.
275 /// \sa [CertOpenStore function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376559.aspx)
277 inline BOOL
Create(_In_ LPCSTR lpszStoreProvider
, _In_ DWORD dwEncodingType
, _In_opt_ HCRYPTPROV_LEGACY hCryptProv
, _In_ DWORD dwFlags
, _In_opt_
const void *pvPara
)
279 HANDLE h
= CertOpenStore(lpszStoreProvider
, dwEncodingType
, hCryptProv
, dwFlags
, pvPara
);
289 /// Closes the certificate store.
291 /// \sa [CertCloseStore function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa376026.aspx)
293 virtual void InternalFree()
295 CertCloseStore(m_h
, 0);
301 /// HCRYPTPROV wrapper class
303 class CContext
: public ATL::CObjectWithHandleT
<HCRYPTPROV
>
307 /// Releases the cryptographi context.
309 /// \sa [CryptReleaseContext function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa380268.aspx)
314 CryptReleaseContext(m_h
, 0);
318 /// Acquires the cryptographic context.
321 /// - TRUE when creation succeeds;
322 /// - FALSE when creation fails. For extended error information, call `GetLastError()`.
323 /// \sa [CryptAcquireContext function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379886.aspx)
325 inline BOOL
Create(_In_opt_ LPCTSTR szContainer
, _In_opt_ LPCTSTR szProvider
, _In_ DWORD dwProvType
, _In_ DWORD dwFlags
)
328 if (CryptAcquireContext(&h
, szContainer
, szProvider
, dwProvType
, dwFlags
)) {
337 /// Releases the cryptographic context.
339 /// \sa [CryptReleaseContext function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa380268.aspx)
341 virtual void InternalFree()
343 CryptReleaseContext(m_h
, 0);
349 /// HCRYPTHASH wrapper class
351 class CHash
: public ATL::CObjectWithHandleDuplT
<HCRYPTHASH
>
355 /// Destroys the hash context.
357 /// \sa [CryptDestroyHash function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379917.aspx)
362 CryptDestroyHash(m_h
);
366 /// Creates the hash context.
369 /// - TRUE when creation succeeds;
370 /// - FALSE when creation fails. For extended error information, call `GetLastError()`.
371 /// \sa [CryptCreateHash function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379908.aspx)
373 inline BOOL
Create(_In_ HCRYPTPROV hProv
, _In_ ALG_ID Algid
, _In_ HCRYPTKEY hKey
, _In_ DWORD dwFlags
)
376 if (CryptCreateHash(hProv
, Algid
, hKey
, dwFlags
, &h
)) {
385 /// Destroys the hash context.
387 /// \sa [CryptDestroyHash function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379917.aspx)
389 virtual void InternalFree()
391 CryptDestroyHash(m_h
);
395 /// Duplicates the hash context.
397 /// \param[in] h Object handle of existing hash context
398 /// \return Duplicated hash context handle
399 /// \sa [CryptDuplicateHash function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379919.aspx)
401 virtual HANDLE
InternalDuplicate(_In_ HANDLE h
) const
404 return CryptDuplicateHash(h
, NULL
, 0, &hNew
) ? hNew
: NULL
;
410 /// HCRYPTKEY wrapper class
412 class CKey
: public ATL::CObjectWithHandleDuplT
<HCRYPTKEY
>
416 /// Destroys the key.
418 /// \sa [CryptDestroyKey function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379918.aspx)
423 CryptDestroyKey(m_h
);
427 /// Generates the key.
429 /// \sa [CryptGenKey function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379941.aspx)
431 inline BOOL
Generate(_In_ HCRYPTPROV hProv
, _In_ ALG_ID Algid
, _In_ DWORD dwFlags
)
434 if (CryptGenKey(hProv
, Algid
, dwFlags
, &h
)) {
444 /// \sa [CryptImportKey function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa380207.aspx)
446 inline BOOL
Import(_In_ HCRYPTPROV hProv
, __in_bcount(dwDataLen
) CONST BYTE
*pbData
, _In_ DWORD dwDataLen
, _In_ HCRYPTKEY hPubKey
, _In_ DWORD dwFlags
)
449 if (CryptImportKey(hProv
, pbData
, dwDataLen
, hPubKey
, dwFlags
, &h
)) {
457 /// Imports the public key.
459 /// \sa [CryptImportPublicKeyInfo function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa380209.aspx)
461 inline BOOL
ImportPublic(_In_ HCRYPTPROV hCryptProv
, _In_ DWORD dwCertEncodingType
, _In_ PCERT_PUBLIC_KEY_INFO pInfo
)
464 if (CryptImportPublicKeyInfo(hCryptProv
, dwCertEncodingType
, pInfo
, &h
)) {
473 /// Destroys the key.
475 /// \sa [CryptDestroyKey function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379918.aspx)
477 virtual void InternalFree()
479 CryptDestroyKey(m_h
);
483 /// Duplicates the key.
485 /// \param[in] h Object handle of existing key
486 /// \return Duplicated key handle
487 /// \sa [CryptDuplicateKey function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379920.aspx)
489 virtual HANDLE
InternalDuplicate(_In_ HANDLE h
) const
492 return CryptDuplicateKey(h
, NULL
, 0, &hNew
) ? hNew
: NULL
;