22template<
class _Traits,
class _Ax>
23static DWORD
CertGetNameStringA(_In_ PCCERT_CONTEXT pCertContext, _In_ DWORD dwType, _In_ DWORD dwFlags, _In_opt_
void *pvTypePara, _Out_ std::basic_string<char, _Traits, _Ax> &sNameString)
29 std::unique_ptr<char[]> szBuffer(
new char[dwSize]);
30 dwSize =
::CertGetNameStringA(pCertContext, dwType, dwFlags, pvTypePara, szBuffer.get(), dwSize);
31 sNameString.assign(szBuffer.get(), dwSize - 1);
40template<
class _Traits,
class _Ax>
41static 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)
47 std::unique_ptr<wchar_t[]> szBuffer(
new wchar_t[dwSize]);
48 dwSize =
::CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara, szBuffer.get(), dwSize);
49 sNameString.assign(szBuffer.get(), dwSize - 1);
58template<
class _Ty,
class _Ax>
67 aData.assign((
const _Ty*)buf, (
const _Ty*)buf + (dwSize +
sizeof(_Ty) - 1) /
sizeof(_Ty));
69 }
else if (GetLastError() == ERROR_MORE_DATA) {
70 aData.resize((dwSize +
sizeof(_Ty) - 1) /
sizeof(_Ty));
83template<
class _Ty,
class _Ax>
84static _Success_(
return != 0) BOOL
CryptGetHashParam(_In_ HCRYPTHASH hHash, _In_ DWORD dwParam, _Out_ std::vector<_Ty, _Ax> &aData, _In_ DWORD dwFlags)
92 aData.assign((
const _Ty*)buf, (
const _Ty*)buf + (dwSize +
sizeof(_Ty) - 1) /
sizeof(_Ty));
94 }
else if (GetLastError() == ERROR_MORE_DATA) {
95 aData.resize((dwSize +
sizeof(_Ty) - 1) /
sizeof(_Ty));
96 if (
CryptGetHashParam(hHash, dwParam,
reinterpret_cast<BYTE*
>(aData.data()), &dwSize, dwFlags))
109static _Success_(
return != 0) BOOL
CryptGetHashParam(_In_ HCRYPTHASH hHash, _In_ DWORD dwParam, _Out_ T &data, _In_ DWORD dwFlags)
111 DWORD dwSize =
sizeof(T);
120template<
class _Ty,
class _Ax>
121static _Success_(
return != 0) BOOL
CryptGetKeyParam(_In_ HCRYPTKEY hKey, _In_ DWORD dwParam, _Out_ std::vector<_Ty, _Ax> &aData, _In_ DWORD dwFlags)
129 aData.assign((
const _Ty*)buf, (
const _Ty*)buf + (dwSize +
sizeof(_Ty) - 1) /
sizeof(_Ty));
131 }
else if (GetLastError() == ERROR_MORE_DATA) {
132 aData.resize((dwSize +
sizeof(_Ty) - 1) /
sizeof(_Ty));
133 if (
CryptGetKeyParam(hKey, dwParam,
reinterpret_cast<BYTE*
>(aData.data()), &dwSize, dwFlags))
146static BOOL
CryptGetKeyParam(_In_ HCRYPTKEY hKey, _In_ DWORD dwParam, _Out_ T &data, _In_ DWORD dwFlags)
148 DWORD dwSize =
sizeof(T);
157template<
class _Ty,
class _Ax>
158static _Success_(
return != 0) BOOL
CryptExportKey(_In_ HCRYPTKEY hKey, _In_ HCRYPTKEY hExpKey, _In_ DWORD dwBlobType, _In_ DWORD dwFlags, _Out_ std::vector<_Ty, _Ax> &aData)
160 DWORD dwKeyBLOBSize = 0;
162 if (
CryptExportKey(hKey, hExpKey, dwBlobType, dwFlags, NULL, &dwKeyBLOBSize)) {
163 aData.resize((dwKeyBLOBSize +
sizeof(_Ty) - 1) /
sizeof(_Ty));
164 if (
CryptExportKey(hKey, hExpKey, dwBlobType, dwFlags,
reinterpret_cast<BYTE*
>(aData.data()), &dwKeyBLOBSize))
176template<
class _Ty,
class _Ax>
177static _Success_(
return != 0) BOOL
CryptEncrypt(_In_ HCRYPTKEY hKey, _In_opt_ HCRYPTHASH hHash, _In_ BOOL Final, _In_ DWORD dwFlags, _Inout_ std::vector<_Ty, _Ax> &aData)
180 sDataLen = SIZETMult(aData.size(),
sizeof(_Ty)),
181 sBufLen = SIZETMult(aData.capacity(),
sizeof(_Ty));
182 if (sDataLen > DWORD_MAX || sBufLen > DWORD_MAX)
183 throw std::invalid_argument(
"Data too big");
185 dwDataLen =
static_cast<DWORD
>(sDataLen),
186 dwBufLen =
static_cast<DWORD
>(sBufLen),
187 dwEncLen = dwDataLen,
191 aData.resize(dwBufLen);
192 if (
CryptEncrypt(hKey, hHash, Final, dwFlags,
reinterpret_cast<BYTE*
>(aData.data()), &dwEncLen, dwBufLen)) {
194 assert(dwEncLen <= dwBufLen);
195 if (dwEncLen < dwBufLen)
196 aData.resize((dwEncLen +
sizeof(_Ty) - 1) /
sizeof(_Ty));
199 dwResult = GetLastError();
200 }
else if (
CryptEncrypt(hKey, NULL, Final, dwFlags, NULL, &dwEncLen, 0)) {
203 dwResult = ERROR_MORE_DATA;
205 dwResult = GetLastError();
207 if (dwResult == ERROR_MORE_DATA) {
209 aData.resize(((dwBufLen = dwEncLen) +
sizeof(_Ty) - 1) /
sizeof(_Ty));
210 dwEncLen = dwDataLen;
211 if (
CryptEncrypt(hKey, hHash, Final, dwFlags,
reinterpret_cast<BYTE*
>(aData.data()), &dwEncLen, dwBufLen)) {
213 assert(dwEncLen <= dwBufLen);
214 if (dwEncLen < dwBufLen)
215 aData.resize((dwEncLen +
sizeof(_Ty) - 1) /
sizeof(_Ty));
220 aData.resize((dwDataLen +
sizeof(_Ty) - 1) /
sizeof(_Ty));
231template<
class _Ty,
class _Ax>
232static _Success_(
return != 0) BOOL
CryptDecrypt(_In_ HCRYPTKEY hKey, _In_opt_ HCRYPTHASH hHash, _In_ BOOL Final, _In_ DWORD dwFlags, _Inout_ std::vector<_Ty, _Ax> &aData)
234 SIZE_T sDataLen = SIZETMult(aData.size(),
sizeof(_Ty));
235 if (sDataLen > DWORD_MAX)
236 throw std::invalid_argument(
"Data too big");
237 DWORD dwDataLen =
static_cast<DWORD
>(sDataLen);
239 if (
CryptDecrypt(hKey, hHash, Final, dwFlags,
reinterpret_cast<BYTE*
>(aData.data()), &dwDataLen)) {
241 aData.resize((dwDataLen +
sizeof(_Ty) - 1) /
sizeof(_Ty));
289 m_h->cbCertEncoded == other->cbCertEncoded && memcmp(
m_h->pbCertEncoded, other->pbCertEncoded,
m_h->cbCertEncoded) == 0;
316 const int r = memcmp(
m_h->pbCertEncoded, other->pbCertEncoded, std::min<DWORD>(
m_h->cbCertEncoded, other->cbCertEncoded));
317 return r < 0 || r == 0 &&
m_h->cbCertEncoded < other->cbCertEncoded;
331 const int r = memcmp(
m_h->pbCertEncoded, other->pbCertEncoded, std::min<DWORD>(
m_h->cbCertEncoded, other->cbCertEncoded));
332 return r > 0 || r == 0 &&
m_h->cbCertEncoded > other->cbCertEncoded;
369 CertFreeCertificateContext(
m_h);
384 return CertDuplicateCertificateContext(h);
417 CertFreeCertificateChain(
m_h);
432 return CertDuplicateCertificateChain(h);
466 CertCloseStore(
m_h, 0);
499 CryptReleaseContext(
m_h, 0);
532 CryptDestroyHash(
m_h);
547 if (CryptDuplicateHash(h, NULL, 0, &hNew))
587 if (dwKeySpec != AT_KEYEXCHANGE && dwKeySpec != AT_SIGNATURE) {
588 SetLastError(ERROR_INVALID_PARAMETER);
594 if (
CryptGenKey(hProv, dwKeySpec, CRYPT_EXPORTABLE, &h)) {
596 std::vector<BYTE, sanitizing_allocator<BYTE>> key_blob;
602 size_key = *
reinterpret_cast<DWORD*
>(&key_blob[12])/8,
603 size_prime = size_key/2;
609 LPBYTE ptr = &key_blob[16];
610 *
reinterpret_cast<DWORD*
>(ptr) = 1;
611 ptr +=
sizeof(DWORD);
620 memset(ptr + 1, 0, size_prime - 1);
625 memset(ptr + 1, 0, size_prime - 1);
633 memset(ptr + 1, 0, size_key - 1);
636 if (
CryptImportKey(hProv, key_blob.data(),
static_cast<DWORD
>(key_blob.size()), 0, 0, &h)) {
655 CryptDestroyKey(
m_h);
670 if (CryptDuplicateKey(h, NULL, 0, &hNew))
679 #pragma warning(push)
680 #pragma warning(disable: 26432)
707 cbData = other.cbData;
709 pbData =
static_cast<BYTE*
>(LocalAlloc(LMEM_FIXED, other.cbData));
711 memcpy(pbData, other.pbData, other.cbData);
721 cbData = other.cbData;
722 pbData = other.pbData;
741 if (
this != &other) {
742 cbData = other.cbData;
746 pbData =
static_cast<BYTE*
>(LocalAlloc(LMEM_FIXED, other.cbData));
748 memcpy(pbData, other.pbData, other.cbData);
761 if (
this != &other) {
762 cbData = other.cbData;
765 pbData = other.pbData;
784 const BYTE*
data() const noexcept
806#pragma warning(disable: 4505)
813static BOOL
CertGetCertificateChain(_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, _Inout_
winstd::cert_chain_context &ctx)
815 PCCERT_CHAIN_CONTEXT pChainContext;
816 BOOL bResult =
CertGetCertificateChain(hChainEngine, pCertContext, pTime, hAdditionalStore, pChainPara, dwFlags, pvReserved, &pChainContext);
818 ctx.attach(pChainContext);
868 BOOL bResult =
CryptGenKey(hProv, Algid, dwFlags, &h);
879static bool CryptImportKey(_In_ HCRYPTPROV hProv, __in_bcount(dwDataLen) LPCBYTE pbData, _In_ DWORD dwDataLen, _In_ HCRYPTKEY hPubKey, _In_ DWORD dwFlags, _Inout_
winstd::crypt_key &key)
882 BOOL bResult =
CryptImportKey(hProv, pbData, dwDataLen, hPubKey, dwFlags, &h);
910 BOOL bResult =
CryptDeriveKey(hProv, Algid, hBaseData, dwFlags, &h);
PCCERT_CHAIN_CONTEXT wrapper class.
Definition Crypt.h:394
virtual ~cert_chain_context()
Destroys the certificate chain context.
Definition Crypt.h:403
void free_internal() noexcept override
Destroys the certificate chain context.
Definition Crypt.h:415
handle_type duplicate_internal(handle_type h) const override
Duplicates the certificate chain context.
Definition Crypt.h:429
PCCERT_CONTEXT wrapper class.
Definition Crypt.h:261
bool operator<=(const handle_type &other) const noexcept
Is certificate less than or equal?
Definition Crypt.h:343
void free_internal() noexcept override
Destroys the certificate context.
Definition Crypt.h:367
bool operator==(const handle_type &other) const noexcept
Is certificate equal to?
Definition Crypt.h:284
bool operator>=(const handle_type &other) const noexcept
Is certificate greater than or equal?
Definition Crypt.h:356
bool operator>(const handle_type &other) const noexcept
Is certificate greater than?
Definition Crypt.h:328
bool operator<(const handle_type &other) const noexcept
Is certificate less than?
Definition Crypt.h:313
bool operator!=(const handle_type &other) const noexcept
Is certificate not equal to?
Definition Crypt.h:300
handle_type duplicate_internal(handle_type h) const override
Duplicates the certificate context.
Definition Crypt.h:381
virtual ~cert_context()
Destroys the certificate context.
Definition Crypt.h:270
HCERTSTORE wrapper class.
Definition Crypt.h:443
virtual ~cert_store()
Closes the certificate store.
Definition Crypt.h:452
void free_internal() noexcept override
Closes the certificate store.
Definition Crypt.h:464
HCRYPTHASH wrapper class.
Definition Crypt.h:509
void free_internal() noexcept override
Destroys the hash context.
Definition Crypt.h:530
virtual ~crypt_hash()
Destroys the hash context.
Definition Crypt.h:518
handle_type duplicate_internal(handle_type h) const override
Duplicates the hash context.
Definition Crypt.h:544
HCRYPTKEY wrapper class.
Definition Crypt.h:562
handle_type duplicate_internal(handle_type h) const override
Duplicates the key.
Definition Crypt.h:667
virtual ~crypt_key()
Destroys the key.
Definition Crypt.h:571
bool create_exp1(HCRYPTPROV hProv, DWORD dwKeySpec)
Creates Exponent-of-one key.
Definition Crypt.h:585
void free_internal() noexcept override
Destroys the key.
Definition Crypt.h:653
HCRYPTPROV wrapper class.
Definition Crypt.h:476
virtual ~crypt_prov()
Releases the cryptographic context.
Definition Crypt.h:485
void free_internal() noexcept override
Releases the cryptographic context.
Definition Crypt.h:497
DATA_BLOB wrapper class.
Definition Crypt.h:682
data_blob(const DATA_BLOB &other)
Duplicate an existing BLOB.
Definition Crypt.h:705
virtual ~data_blob()
Destroys the BLOB.
Definition Crypt.h:730
BYTE * data() noexcept
Get BLOB buffer.
Definition Crypt.h:792
const BYTE * data() const noexcept
Get BLOB buffer.
Definition Crypt.h:784
data_blob() noexcept
Initializes an empty BLOB.
Definition Crypt.h:687
data_blob(data_blob &&other) noexcept
Move an existing BLOB.
Definition Crypt.h:719
data_blob & operator=(data_blob &&other) noexcept
Move an existing BLOB.
Definition Crypt.h:759
data_blob(BYTE *data, DWORD size) noexcept
Initializes a BLOB from existing data.
Definition Crypt.h:696
DWORD size() const noexcept
Get BLOB size.
Definition Crypt.h:776
data_blob & operator=(const DATA_BLOB &other)
Copy an existing BLOB.
Definition Crypt.h:739
Base abstract template class to support object handle keeping for objects that support trivial handle...
Definition Common.h:1285
Base abstract template class to support generic object handle keeping.
Definition Common.h:1020
T handle_type
Datatype of the object handle this template class handles.
Definition Common.h:1025
handle_type m_h
Object handle.
Definition Common.h:1274
void attach(handle_type h) noexcept
Sets a new object handle for the class.
Definition Common.h:1237
Windows runtime error.
Definition Common.h:1528
static bool CryptImportPublicKeyInfo(HCRYPTPROV hCryptProv, DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, winstd::crypt_key &key)
Imports the public key.
Definition Crypt.h:893
static BOOL WINAPI CertGetCertificateContextProperty(PCCERT_CONTEXT pCertContext, DWORD dwPropId, std::vector< _Ty, _Ax > &aData)
Retrieves the information contained in an extended property of a certificate context.
Definition Crypt.h:59
static BOOL CertGetCertificateChain(HCERTCHAINENGINE hChainEngine, PCCERT_CONTEXT pCertContext, LPFILETIME pTime, HCERTSTORE hAdditionalStore, PCERT_CHAIN_PARA pChainPara, DWORD dwFlags, LPVOID pvReserved, winstd::cert_chain_context &ctx)
The CertGetCertificateChain function builds a certificate chain context starting from an end certific...
Definition Crypt.h:813
static BOOL CryptGetHashParam(HCRYPTHASH hHash, DWORD dwParam, std::vector< _Ty, _Ax > &aData, DWORD dwFlags)
Retrieves data that governs the operations of a hash object. The actual hash value can be retrieved b...
Definition Crypt.h:84
static DWORD CertGetNameStringW(PCCERT_CONTEXT pCertContext, DWORD dwType, DWORD dwFlags, void *pvTypePara, std::basic_string< wchar_t, _Traits, _Ax > &sNameString)
Obtains the subject or issuer name from a certificate CERT_CONTEXT structure and stores it in a std::...
Definition Crypt.h:41
static BOOL CryptAcquireContextA(winstd::crypt_prov &prov, LPCSTR szContainer, LPCSTR szProvider, DWORD dwProvType, DWORD dwFlags)
Acquires the cryptographic context.
Definition Crypt.h:823
static DWORD CertGetNameStringA(PCCERT_CONTEXT pCertContext, DWORD dwType, DWORD dwFlags, void *pvTypePara, std::basic_string< char, _Traits, _Ax > &sNameString)
Obtains the subject or issuer name from a certificate CERT_CONTEXT structure and stores it in a std::...
Definition Crypt.h:23
static BOOL CryptGenKey(HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, winstd::crypt_key &key)
Generates the key.
Definition Crypt.h:865
static BOOL CryptExportKey(HCRYPTKEY hKey, HCRYPTKEY hExpKey, DWORD dwBlobType, DWORD dwFlags, std::vector< _Ty, _Ax > &aData)
Exports a cryptographic key or a key pair from a cryptographic service provider (CSP) in a secure man...
Definition Crypt.h:158
static BOOL CryptGetKeyParam(HCRYPTKEY hKey, DWORD dwParam, std::vector< _Ty, _Ax > &aData, DWORD dwFlags)
Retrieves data that governs the operations of a key.
Definition Crypt.h:121
static BOOL CryptCreateHash(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey, DWORD dwFlags, winstd::crypt_hash &hash)
Creates the hash context.
Definition Crypt.h:851
static BOOL CryptAcquireContextW(winstd::crypt_prov &prov, LPCWSTR szContainer, LPCWSTR szProvider, DWORD dwProvType, DWORD dwFlags)
Acquires the cryptographic context.
Definition Crypt.h:837
static BOOL CryptEncrypt(HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final, DWORD dwFlags, std::vector< _Ty, _Ax > &aData)
Encrypts data.
Definition Crypt.h:177
static bool CryptDeriveKey(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData, DWORD dwFlags, winstd::crypt_key &key)
Generates cryptographic session keys derived from a base data value.
Definition Crypt.h:907
static BOOL CryptDecrypt(HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final, DWORD dwFlags, std::vector< _Ty, _Ax > &aData)
Decrypts data previously encrypted by using the CryptEncrypt function.
Definition Crypt.h:232
static bool CryptImportKey(HCRYPTPROV hProv, __in_bcount(dwDataLen) LPCBYTE pbData, DWORD dwDataLen, HCRYPTKEY hPubKey, DWORD dwFlags, winstd::crypt_key &key)
Imports the key.
Definition Crypt.h:879
#define WINSTD_STACK_BUFFER_BYTES
Size of the stack buffer in bytes used for initial system function call.
Definition Common.h:94
#define WINSTD_DPLHANDLE_IMPL(C, T, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition Common.h:176
#define WINSTD_HANDLE_IMPL(C, T, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition Common.h:164
static const T invalid
Invalid handle value.
Definition Common.h:1030