23template<
class _Traits,
class _Ax>
24static DWORD CertGetNameStringA(_In_ PCCERT_CONTEXT pCertContext, _In_ DWORD dwType, _In_ DWORD dwFlags, _In_opt_
void *pvTypePara, _Out_ std::basic_string<char, _Traits, _Ax> &sNameString)
27 DWORD dwSize = ::CertGetNameStringA(pCertContext, dwType, dwFlags, pvTypePara, NULL, 0);
30 std::unique_ptr<char[]> szBuffer(
new char[dwSize]);
31 dwSize = ::CertGetNameStringA(pCertContext, dwType, dwFlags, pvTypePara, szBuffer.get(), dwSize);
32 sNameString.assign(szBuffer.get(), dwSize - 1);
41template<
class _Traits,
class _Ax>
42static DWORD CertGetNameStringW(_In_ PCCERT_CONTEXT pCertContext, _In_ DWORD dwType, _In_ DWORD dwFlags, _In_opt_
void *pvTypePara, _Out_ std::basic_string<wchar_t, _Traits, _Ax> &sNameString)
45 DWORD dwSize = ::CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara, NULL, 0);
48 std::unique_ptr<wchar_t[]> szBuffer(
new wchar_t[dwSize]);
49 dwSize = ::CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara, szBuffer.get(), dwSize);
50 sNameString.assign(szBuffer.get(), dwSize - 1);
59template<
class _Ty,
class _Ax>
60static _Success_(
return != 0) BOOL WINAPI CertGetCertificateContextProperty(_In_ PCCERT_CONTEXT pCertContext, _In_ DWORD dwPropId, _Out_ std::vector<_Ty, _Ax> &aData)
66 if (CertGetCertificateContextProperty(pCertContext, dwPropId, buf, &dwSize)) {
68 aData.assign((
const _Ty*)buf, (
const _Ty*)buf + (dwSize +
sizeof(_Ty) - 1) /
sizeof(_Ty));
70 }
else if (GetLastError() == ERROR_MORE_DATA) {
71 aData.resize((dwSize +
sizeof(_Ty) - 1) /
sizeof(_Ty));
72 if (CertGetCertificateContextProperty(pCertContext, dwPropId, (BYTE*)aData.data(), &dwSize))
84template<
class _Ty,
class _Ax>
85static _Success_(
return != 0) BOOL CryptGetHashParam(_In_ HCRYPTHASH hHash, _In_ DWORD dwParam, _Out_ std::vector<_Ty, _Ax> &aData, _In_ DWORD dwFlags)
91 if (CryptGetHashParam(hHash, dwParam, buf, &dwSize, dwFlags)) {
93 aData.assign((
const _Ty*)buf, (
const _Ty*)buf + (dwSize +
sizeof(_Ty) - 1) /
sizeof(_Ty));
95 }
else if (GetLastError() == ERROR_MORE_DATA) {
96 aData.resize((dwSize +
sizeof(_Ty) - 1) /
sizeof(_Ty));
97 if (CryptGetHashParam(hHash, dwParam, (BYTE*)aData.data(), &dwSize, dwFlags))
110static _Success_(
return != 0) BOOL CryptGetHashParam(_In_ HCRYPTHASH hHash, _In_ DWORD dwParam, _Out_ T &data, _In_ DWORD dwFlags)
112 DWORD dwSize =
sizeof(T);
113 return CryptGetHashParam(hHash, dwParam, (BYTE*)&data, &dwSize, dwFlags);
121template<
class _Ty,
class _Ax>
122static _Success_(
return != 0) BOOL CryptGetKeyParam(_In_ HCRYPTKEY hKey, _In_ DWORD dwParam, _Out_ std::vector<_Ty, _Ax> &aData, _In_ DWORD dwFlags)
128 if (CryptGetKeyParam(hKey, dwParam, buf, &dwSize, dwFlags)) {
130 aData.assign((
const _Ty*)buf, (
const _Ty*)buf + (dwSize +
sizeof(_Ty) - 1) /
sizeof(_Ty));
132 }
else if (GetLastError() == ERROR_MORE_DATA) {
133 aData.resize((dwSize +
sizeof(_Ty) - 1) /
sizeof(_Ty));
134 if (CryptGetKeyParam(hKey, dwParam, (BYTE*)aData.data(), &dwSize, dwFlags))
147static BOOL CryptGetKeyParam(_In_ HCRYPTKEY hKey, _In_ DWORD dwParam, _Out_ T &data, _In_ DWORD dwFlags)
149 DWORD dwSize =
sizeof(T);
150 return CryptGetKeyParam(hKey, dwParam, (BYTE*)&data, &dwSize, dwFlags);
158template<
class _Ty,
class _Ax>
159static _Success_(
return != 0) BOOL CryptExportKey(_In_ HCRYPTKEY hKey, _In_ HCRYPTKEY hExpKey, _In_ DWORD dwBlobType, _In_ DWORD dwFlags, _Out_ std::vector<_Ty, _Ax> &aData)
161 DWORD dwKeyBLOBSize = 0;
163 if (CryptExportKey(hKey, hExpKey, dwBlobType, dwFlags, NULL, &dwKeyBLOBSize)) {
164 aData.resize((dwKeyBLOBSize +
sizeof(_Ty) - 1) /
sizeof(_Ty));
165 if (CryptExportKey(hKey, hExpKey, dwBlobType, dwFlags, aData.data(), &dwKeyBLOBSize))
177template<
class _Ty,
class _Ax>
178static _Success_(
return != 0) BOOL CryptEncrypt(_In_ HCRYPTKEY hKey, _In_opt_ HCRYPTHASH hHash, _In_ BOOL Final, _In_ DWORD dwFlags, _Inout_ std::vector<_Ty, _Ax> &aData)
181 dwDataLen = (DWORD)(aData.size() *
sizeof(_Ty)),
182 dwBufLen = (DWORD)(aData.capacity() *
sizeof(_Ty)),
183 dwEncLen = dwDataLen,
187 aData.resize(dwBufLen);
188 if (CryptEncrypt(hKey, hHash, Final, dwFlags, (BYTE*)aData.data(), &dwEncLen, dwBufLen)) {
190 assert(dwEncLen <= dwBufLen);
191 if (dwEncLen < dwBufLen)
192 aData.resize((dwEncLen +
sizeof(_Ty) - 1) /
sizeof(_Ty));
195 dwResult = GetLastError();
196 }
else if (CryptEncrypt(hKey, NULL, Final, dwFlags, NULL, &dwEncLen, 0)) {
199 dwResult = ERROR_MORE_DATA;
201 dwResult = GetLastError();
203 if (dwResult == ERROR_MORE_DATA) {
205 aData.resize(((dwBufLen = dwEncLen) +
sizeof(_Ty) - 1) /
sizeof(_Ty));
206 dwEncLen = dwDataLen;
207 if (CryptEncrypt(hKey, hHash, Final, dwFlags, (BYTE*)aData.data(), &dwEncLen, dwBufLen)) {
209 assert(dwEncLen <= dwBufLen);
210 if (dwEncLen < dwBufLen)
211 aData.resize((dwEncLen +
sizeof(_Ty) - 1) /
sizeof(_Ty));
216 aData.resize((dwDataLen +
sizeof(_Ty) - 1) /
sizeof(_Ty));
227template<
class _Ty,
class _Ax>
228static _Success_(
return != 0) BOOL CryptDecrypt(_In_ HCRYPTKEY hKey, _In_opt_ HCRYPTHASH hHash, _In_ BOOL Final, _In_ DWORD dwFlags, _Inout_ std::vector<_Ty, _Ax> &aData)
230 DWORD dwDataLen = (DWORD)(aData.size() *
sizeof(_Ty));
232 if (CryptDecrypt(hKey, hHash, Final, dwFlags, (BYTE*)aData.data(), &dwDataLen)) {
234 aData.resize((dwDataLen +
sizeof(_Ty) - 1) /
sizeof(_Ty));
276 bool create(_In_ DWORD dwCertEncodingType, _In_ LPCBYTE pbCertEncoded, _In_ DWORD cbCertEncoded)
noexcept
278 handle_type h = CertCreateCertificateContext(dwCertEncodingType, pbCertEncoded, cbCertEncoded);
299 m_h->cbCertEncoded == other->cbCertEncoded && memcmp(
m_h->pbCertEncoded, other->pbCertEncoded,
m_h->cbCertEncoded) == 0;
326 const int r = memcmp(
m_h->pbCertEncoded, other->pbCertEncoded, std::min<DWORD>(
m_h->cbCertEncoded, other->cbCertEncoded));
327 return r < 0 || r == 0 &&
m_h->cbCertEncoded < other->cbCertEncoded;
341 const int r = memcmp(
m_h->pbCertEncoded, other->pbCertEncoded, std::min<DWORD>(
m_h->cbCertEncoded, other->cbCertEncoded));
342 return r > 0 || r == 0 &&
m_h->cbCertEncoded > other->cbCertEncoded;
379 CertFreeCertificateContext(
m_h);
393 return CertDuplicateCertificateContext(h);
425 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 = NULL)
noexcept
428 if (CertGetCertificateChain(hChainEngine, pCertContext, pTime, hAdditionalStore, pChainPara, dwFlags, pvReserved, &h)) {
443 CertFreeCertificateChain(
m_h);
457 return CertDuplicateCertificateChain(h);
489 bool create(_In_ LPCSTR lpszStoreProvider, _In_ DWORD dwEncodingType, _In_opt_ HCRYPTPROV_LEGACY hCryptProv, _In_ DWORD dwFlags, _In_opt_
const void *pvPara)
noexcept
491 handle_type h = CertOpenStore(lpszStoreProvider, dwEncodingType, hCryptProv, dwFlags, pvPara);
508 bool create(_In_opt_ HCRYPTPROV_LEGACY hCryptProv, _In_z_ LPCTSTR szSubsystemProtocol)
noexcept
510 handle_type h = CertOpenSystemStore(hCryptProv, szSubsystemProtocol);
526 CertCloseStore(
m_h, 0);
558 bool create(_In_opt_z_ LPCTSTR szContainer, _In_opt_z_ LPCTSTR szProvider, _In_ DWORD dwProvType, _In_ DWORD dwFlags = 0) noexcept
561 if (CryptAcquireContext(&h, szContainer, szProvider, dwProvType, dwFlags)) {
576 CryptReleaseContext(
m_h, 0);
608 bool create(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_opt_ HCRYPTKEY hKey = NULL, _In_opt_ DWORD dwFlags = 0) noexcept
611 if (CryptCreateHash(hProv, Algid, hKey, dwFlags, &h)) {
626 CryptDestroyHash(
m_h);
641 return CryptDuplicateHash(h, NULL, 0, &hNew) ? hNew :
invalid;
669 bool generate(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ DWORD dwFlags)
noexcept
672 if (CryptGenKey(hProv, Algid, dwFlags, &h)) {
684 bool import(_In_ HCRYPTPROV hProv, __in_bcount(dwDataLen) LPCBYTE pbData, _In_ DWORD dwDataLen, _In_ HCRYPTKEY hPubKey, _In_ DWORD dwFlags)
noexcept
687 if (CryptImportKey(hProv, pbData, dwDataLen, hPubKey, dwFlags, &h)) {
699 bool import_public(_In_ HCRYPTPROV hCryptProv, _In_ DWORD dwCertEncodingType, _In_ PCERT_PUBLIC_KEY_INFO pInfo)
noexcept
702 if (CryptImportPublicKeyInfo(hCryptProv, dwCertEncodingType, pInfo, &h)) {
714 bool derive(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ HCRYPTHASH hBaseData, _In_ DWORD dwFlags)
noexcept
717 if (CryptDeriveKey(hProv, Algid, hBaseData, dwFlags, &h)) {
734 if (dwKeySpec != AT_KEYEXCHANGE && dwKeySpec != AT_SIGNATURE) {
735 SetLastError(ERROR_INVALID_PARAMETER);
741 if (CryptGenKey(hProv, dwKeySpec, CRYPT_EXPORTABLE, &h)) {
743 std::vector<BYTE, sanitizing_allocator<BYTE>> key_blob;
744 if (CryptExportKey(h, 0, PRIVATEKEYBLOB, 0, key_blob)) {
749 size_key = *
reinterpret_cast<DWORD*
>(&key_blob[12])/8,
750 size_prime = size_key/2;
756 LPBYTE ptr = &key_blob[16];
757 *
reinterpret_cast<DWORD*
>(ptr) = 1;
758 ptr +=
sizeof(DWORD);
767 memset(ptr + 1, 0, size_prime - 1);
772 memset(ptr + 1, 0, size_prime - 1);
780 memset(ptr + 1, 0, size_key - 1);
783 if (CryptImportKey(hProv, key_blob.data(),
static_cast<DWORD
>(key_blob.size()), 0, 0, &h)) {
802 CryptDestroyKey(
m_h);
817 return CryptDuplicateKey(h, NULL, 0, &hNew) ? hNew :
invalid;
824 #pragma warning(push)
825 #pragma warning(disable: 26432)
852 cbData = other.cbData;
854 pbData =
static_cast<BYTE*
>(LocalAlloc(LMEM_FIXED, other.cbData));
856 memcpy(pbData, other.pbData, other.cbData);
866 cbData = other.cbData;
867 pbData = other.pbData;
886 if (
this != &other) {
887 cbData = other.cbData;
891 pbData =
static_cast<BYTE*
>(LocalAlloc(LMEM_FIXED, other.cbData));
893 memcpy(pbData, other.pbData, other.cbData);
906 if (
this != &other) {
907 cbData = other.cbData;
910 pbData = other.pbData;
929 const BYTE*
data() const noexcept
PCCERT_CHAIN_CONTEXT wrapper class.
Definition: Crypt.h:401
handle_type duplicate_internal(_In_ handle_type h) const noexcept override
Duplicates the certificate chain context.
Definition: Crypt.h:455
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=NULL) noexcept
Creates the certificate chain context.
Definition: Crypt.h:425
virtual ~cert_chain_context()
Destroys the certificate chain context.
Definition: Crypt.h:410
void free_internal() noexcept override
Destroys the certificate chain context.
Definition: Crypt.h:441
PCCERT_CONTEXT wrapper class.
Definition: Crypt.h:252
bool operator==(_In_ const handle_type &other) const noexcept
Is certificate equal to?
Definition: Crypt.h:294
void free_internal() noexcept override
Destroys the certificate context.
Definition: Crypt.h:377
bool operator<=(_In_ const handle_type &other) const noexcept
Is certificate less than or equal?
Definition: Crypt.h:353
bool create(_In_ DWORD dwCertEncodingType, _In_ LPCBYTE pbCertEncoded, _In_ DWORD cbCertEncoded) noexcept
Creates the certificate context.
Definition: Crypt.h:276
bool operator!=(_In_ const handle_type &other) const noexcept
Is certificate not equal to?
Definition: Crypt.h:310
bool operator>=(_In_ const handle_type &other) const noexcept
Is certificate greater than or equal?
Definition: Crypt.h:366
bool operator<(_In_ const handle_type &other) const noexcept
Is certificate less than?
Definition: Crypt.h:323
handle_type duplicate_internal(_In_ handle_type h) const noexcept override
Duplicates the certificate context.
Definition: Crypt.h:391
bool operator>(_In_ const handle_type &other) const noexcept
Is certificate greater than?
Definition: Crypt.h:338
virtual ~cert_context()
Destroys the certificate context.
Definition: Crypt.h:261
HCERTSTORE wrapper class.
Definition: Crypt.h:465
bool create(_In_opt_ HCRYPTPROV_LEGACY hCryptProv, _In_z_ LPCTSTR szSubsystemProtocol) noexcept
Opens the most common system certificate store. To open certificate stores with more complex requirem...
Definition: Crypt.h:508
virtual ~cert_store()
Closes the certificate store.
Definition: Crypt.h:474
void free_internal() noexcept override
Closes the certificate store.
Definition: Crypt.h:524
bool create(_In_ LPCSTR lpszStoreProvider, _In_ DWORD dwEncodingType, _In_opt_ HCRYPTPROV_LEGACY hCryptProv, _In_ DWORD dwFlags, _In_opt_ const void *pvPara) noexcept
Opens the certificate store.
Definition: Crypt.h:489
HCRYPTHASH wrapper class.
Definition: Crypt.h:584
void free_internal() noexcept override
Destroys the hash context.
Definition: Crypt.h:624
virtual ~crypt_hash()
Destroys the hash context.
Definition: Crypt.h:593
handle_type duplicate_internal(_In_ handle_type h) const noexcept override
Duplicates the hash context.
Definition: Crypt.h:638
bool create(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_opt_ HCRYPTKEY hKey=NULL, _In_opt_ DWORD dwFlags=0) noexcept
Creates the hash context.
Definition: Crypt.h:608
HCRYPTKEY wrapper class.
Definition: Crypt.h:649
bool generate(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ DWORD dwFlags) noexcept
Generates the key.
Definition: Crypt.h:669
bool derive(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ HCRYPTHASH hBaseData, _In_ DWORD dwFlags) noexcept
Generates cryptographic session keys derived from a base data value.
Definition: Crypt.h:714
bool create_exp1(_In_ HCRYPTPROV hProv, _In_ DWORD dwKeySpec)
Creates Exponent-of-one key.
Definition: Crypt.h:732
virtual ~crypt_key()
Destroys the key.
Definition: Crypt.h:658
bool import_public(_In_ HCRYPTPROV hCryptProv, _In_ DWORD dwCertEncodingType, _In_ PCERT_PUBLIC_KEY_INFO pInfo) noexcept
Imports the public key.
Definition: Crypt.h:699
handle_type duplicate_internal(_In_ handle_type h) const noexcept override
Duplicates the key.
Definition: Crypt.h:814
void free_internal() noexcept override
Destroys the key.
Definition: Crypt.h:800
HCRYPTPROV wrapper class.
Definition: Crypt.h:534
virtual ~crypt_prov()
Releases the cryptographic context.
Definition: Crypt.h:543
void free_internal() noexcept override
Releases the cryptographic context.
Definition: Crypt.h:574
bool create(_In_opt_z_ LPCTSTR szContainer, _In_opt_z_ LPCTSTR szProvider, _In_ DWORD dwProvType, _In_ DWORD dwFlags=0) noexcept
Acquires the cryptographic context.
Definition: Crypt.h:558
DATA_BLOB wrapper class.
Definition: Crypt.h:827
data_blob(_Inout_ data_blob &&other) noexcept
Move an existing BLOB.
Definition: Crypt.h:864
virtual ~data_blob()
Destroys the BLOB.
Definition: Crypt.h:875
data_blob & operator=(_In_ const DATA_BLOB &other)
Copy an existing BLOB.
Definition: Crypt.h:884
data_blob & operator=(_Inout_ data_blob &&other) noexcept
Move an existing BLOB.
Definition: Crypt.h:904
BYTE * data() noexcept
Get BLOB buffer.
Definition: Crypt.h:937
const BYTE * data() const noexcept
Get BLOB buffer.
Definition: Crypt.h:929
data_blob(_In_ const DATA_BLOB &other)
Duplicate an existing BLOB.
Definition: Crypt.h:850
data_blob() noexcept
Initializes an empty BLOB.
Definition: Crypt.h:832
data_blob(_In_count_(size) BYTE *data, _In_ DWORD size) noexcept
Initializes a BLOB from existing data.
Definition: Crypt.h:841
DWORD size() const noexcept
Get BLOB size.
Definition: Crypt.h:921
Base abstract template class to support object handle keeping for objects that support handle duplica...
Definition: Common.h:844
Base abstract template class to support generic object handle keeping.
Definition: Common.h:580
PCCERT_CONTEXT handle_type
Datatype of the object handle this template class handles.
Definition: Common.h:585
void attach(_In_opt_ handle_type h) noexcept
Sets a new object handle for the class.
Definition: Common.h:794
handle_type m_h
Object handle.
Definition: Common.h:833
Windows runtime error.
Definition: Common.h:1026
#define WINSTD_STACK_BUFFER_BYTES
Size of the stack buffer in bytes used for initial system function call.
Definition: Common.h:80
#define WINSTD_DPLHANDLE_IMPL(C, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition: Common.h:150
#define WINSTD_HANDLE_IMPL(C, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition: Common.h:138
static const PCCERT_CONTEXT invalid
Invalid handle value.
Definition: Common.h:590