Crypt.h
/*
SPDX-License-Identifier: MIT
Copyright © 1991-2022 Amebis
Copyright © 2016 GÉANT
*/
#pragma once
#include "Common.h"
#include <assert.h>
#include <WinCrypt.h>
#include <algorithm>
#include <string>
#include <vector>
template<class _Traits, class _Ax>
static DWORD CertGetNameStringA(_In_ PCCERT_CONTEXT pCertContext, _In_ DWORD dwType, _In_ DWORD dwFlags, _In_opt_ void *pvTypePara, _Out_ std::basic_string<char, _Traits, _Ax> &sNameString)
{
// Query the final string length first.
DWORD dwSize = ::CertGetNameStringA(pCertContext, dwType, dwFlags, pvTypePara, NULL, 0);
// Allocate buffer on heap to format the string data into and read it.
std::unique_ptr<char[]> szBuffer(new char[dwSize]);
dwSize = ::CertGetNameStringA(pCertContext, dwType, dwFlags, pvTypePara, szBuffer.get(), dwSize);
sNameString.assign(szBuffer.get(), dwSize - 1);
return dwSize;
}
template<class _Traits, class _Ax>
static 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)
{
// Query the final string length first.
DWORD dwSize = ::CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara, NULL, 0);
// Allocate buffer on heap to format the string data into and read it.
std::unique_ptr<wchar_t[]> szBuffer(new wchar_t[dwSize]);
dwSize = ::CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara, szBuffer.get(), dwSize);
sNameString.assign(szBuffer.get(), dwSize - 1);
return dwSize;
}
template<class _Ty, class _Ax>
static _Success_(return != 0) BOOL WINAPI CertGetCertificateContextProperty(_In_ PCCERT_CONTEXT pCertContext, _In_ DWORD dwPropId, _Out_ std::vector<_Ty, _Ax> &aData)
{
BYTE buf[WINSTD_STACK_BUFFER_BYTES];
DWORD dwSize = WINSTD_STACK_BUFFER_BYTES;
// Try with the stack buffer first.
if (CertGetCertificateContextProperty(pCertContext, dwPropId, buf, &dwSize)) {
// Copy from stack.
aData.assign((const _Ty*)buf, (const _Ty*)buf + (dwSize + sizeof(_Ty) - 1) / sizeof(_Ty));
return TRUE;
} else if (GetLastError() == ERROR_MORE_DATA) {
aData.resize((dwSize + sizeof(_Ty) - 1) / sizeof(_Ty));
if (CertGetCertificateContextProperty(pCertContext, dwPropId, (BYTE*)aData.data(), &dwSize))
return TRUE;
}
return FALSE;
}
template<class _Ty, class _Ax>
static _Success_(return != 0) BOOL CryptGetHashParam(_In_ HCRYPTHASH hHash, _In_ DWORD dwParam, _Out_ std::vector<_Ty, _Ax> &aData, _In_ DWORD dwFlags)
{
BYTE buf[WINSTD_STACK_BUFFER_BYTES];
DWORD dwSize = WINSTD_STACK_BUFFER_BYTES;
// Try with the stack buffer first.
if (CryptGetHashParam(hHash, dwParam, buf, &dwSize, dwFlags)) {
// Copy from stack.
aData.assign((const _Ty*)buf, (const _Ty*)buf + (dwSize + sizeof(_Ty) - 1) / sizeof(_Ty));
return TRUE;
} else if (GetLastError() == ERROR_MORE_DATA) {
aData.resize((dwSize + sizeof(_Ty) - 1) / sizeof(_Ty));
if (CryptGetHashParam(hHash, dwParam, (BYTE*)aData.data(), &dwSize, dwFlags))
return TRUE;
}
return FALSE;
}
template<class T>
static _Success_(return != 0) BOOL CryptGetHashParam(_In_ HCRYPTHASH hHash, _In_ DWORD dwParam, _Out_ T &data, _In_ DWORD dwFlags)
{
DWORD dwSize = sizeof(T);
return CryptGetHashParam(hHash, dwParam, (BYTE*)&data, &dwSize, dwFlags);
}
template<class _Ty, class _Ax>
static _Success_(return != 0) BOOL CryptGetKeyParam(_In_ HCRYPTKEY hKey, _In_ DWORD dwParam, _Out_ std::vector<_Ty, _Ax> &aData, _In_ DWORD dwFlags)
{
BYTE buf[WINSTD_STACK_BUFFER_BYTES];
DWORD dwSize = WINSTD_STACK_BUFFER_BYTES;
// Try with the stack buffer first.
if (CryptGetKeyParam(hKey, dwParam, buf, &dwSize, dwFlags)) {
// Copy from stack.
aData.assign((const _Ty*)buf, (const _Ty*)buf + (dwSize + sizeof(_Ty) - 1) / sizeof(_Ty));
return TRUE;
} else if (GetLastError() == ERROR_MORE_DATA) {
aData.resize((dwSize + sizeof(_Ty) - 1) / sizeof(_Ty));
if (CryptGetKeyParam(hKey, dwParam, (BYTE*)aData.data(), &dwSize, dwFlags))
return TRUE;
}
return FALSE;
}
template<class T>
static BOOL CryptGetKeyParam(_In_ HCRYPTKEY hKey, _In_ DWORD dwParam, _Out_ T &data, _In_ DWORD dwFlags)
{
DWORD dwSize = sizeof(T);
return CryptGetKeyParam(hKey, dwParam, (BYTE*)&data, &dwSize, dwFlags);
}
template<class _Ty, class _Ax>
static _Success_(return != 0) BOOL CryptExportKey(_In_ HCRYPTKEY hKey, _In_ HCRYPTKEY hExpKey, _In_ DWORD dwBlobType, _In_ DWORD dwFlags, _Out_ std::vector<_Ty, _Ax> &aData)
{
DWORD dwKeyBLOBSize = 0;
if (CryptExportKey(hKey, hExpKey, dwBlobType, dwFlags, NULL, &dwKeyBLOBSize)) {
aData.resize((dwKeyBLOBSize + sizeof(_Ty) - 1) / sizeof(_Ty));
if (CryptExportKey(hKey, hExpKey, dwBlobType, dwFlags, aData.data(), &dwKeyBLOBSize))
return TRUE;
}
return FALSE;
}
template<class _Ty, class _Ax>
static _Success_(return != 0) BOOL CryptEncrypt(_In_ HCRYPTKEY hKey, _In_opt_ HCRYPTHASH hHash, _In_ BOOL Final, _In_ DWORD dwFlags, _Inout_ std::vector<_Ty, _Ax> &aData)
{
DWORD
dwDataLen = (DWORD)(aData.size() * sizeof(_Ty)),
dwBufLen = (DWORD)(aData.capacity() * sizeof(_Ty)),
dwEncLen = dwDataLen,
dwResult;
if (dwBufLen) {
aData.resize(dwBufLen);
if (CryptEncrypt(hKey, hHash, Final, dwFlags, (BYTE*)aData.data(), &dwEncLen, dwBufLen)) {
// Encryption succeeded.
assert(dwEncLen <= dwBufLen);
if (dwEncLen < dwBufLen)
aData.resize((dwEncLen + sizeof(_Ty) - 1) / sizeof(_Ty));
return TRUE;
} else
dwResult = GetLastError();
} else if (CryptEncrypt(hKey, NULL, Final, dwFlags, NULL, &dwEncLen, 0)) {
// CryptEncrypt() always succeeds for output data size queries.
// dwEncLen contains required output data size. Continue as if the buffer was to small. Actually, the output buffer _was_ too small!
dwResult = ERROR_MORE_DATA;
} else
dwResult = GetLastError();
if (dwResult == ERROR_MORE_DATA) {
// Encrypted data will be longer. Reserve more space and retry.
aData.resize(((dwBufLen = dwEncLen) + sizeof(_Ty) - 1) / sizeof(_Ty));
dwEncLen = dwDataLen;
if (CryptEncrypt(hKey, hHash, Final, dwFlags, (BYTE*)aData.data(), &dwEncLen, dwBufLen)) {
// Encryption succeeded.
assert(dwEncLen <= dwBufLen);
if (dwEncLen < dwBufLen)
aData.resize((dwEncLen + sizeof(_Ty) - 1) / sizeof(_Ty));
return TRUE;
}
} else {
// Resize back to data length.
aData.resize((dwDataLen + sizeof(_Ty) - 1) / sizeof(_Ty));
}
return FALSE;
}
template<class _Ty, class _Ax>
static _Success_(return != 0) BOOL CryptDecrypt(_In_ HCRYPTKEY hKey, _In_opt_ HCRYPTHASH hHash, _In_ BOOL Final, _In_ DWORD dwFlags, _Inout_ std::vector<_Ty, _Ax> &aData)
{
DWORD dwDataLen = (DWORD)(aData.size() * sizeof(_Ty));
if (CryptDecrypt(hKey, hHash, Final, dwFlags, (BYTE*)aData.data(), &dwDataLen)) {
// Decryption succeeded.
aData.resize((dwDataLen + sizeof(_Ty) - 1) / sizeof(_Ty));
return TRUE;
}
return FALSE;
}
namespace winstd
{
class cert_context : public dplhandle<PCCERT_CONTEXT, NULL>
{
WINSTD_DPLHANDLE_IMPL(cert_context, NULL)
public:
virtual ~cert_context()
{
if (m_h != invalid)
free_internal();
}
bool create(_In_ DWORD dwCertEncodingType, _In_ LPCBYTE pbCertEncoded, _In_ DWORD cbCertEncoded) noexcept
{
handle_type h = CertCreateCertificateContext(dwCertEncodingType, pbCertEncoded, cbCertEncoded);
if (h != invalid) {
attach(h);
return true;
} else
return false;
}
bool operator==(_In_ const handle_type &other) const noexcept
{
// TODO: [Crypto] Make constant time.
return
m_h == other ||
m_h->cbCertEncoded == other->cbCertEncoded && memcmp(m_h->pbCertEncoded, other->pbCertEncoded, m_h->cbCertEncoded) == 0;
}
bool operator!=(_In_ const handle_type &other) const noexcept
{
return !operator==(other);
}
bool operator<(_In_ const handle_type &other) const noexcept
{
// TODO: [Crypto] Make constant time.
const int r = memcmp(m_h->pbCertEncoded, other->pbCertEncoded, std::min<DWORD>(m_h->cbCertEncoded, other->cbCertEncoded));
return r < 0 || r == 0 && m_h->cbCertEncoded < other->cbCertEncoded;
}
bool operator>(_In_ const handle_type &other) const noexcept
{
// TODO: [Crypto] Make constant time.
const int r = memcmp(m_h->pbCertEncoded, other->pbCertEncoded, std::min<DWORD>(m_h->cbCertEncoded, other->cbCertEncoded));
return r > 0 || r == 0 && m_h->cbCertEncoded > other->cbCertEncoded;
}
bool operator<=(_In_ const handle_type &other) const noexcept
{
return !operator>(other);
}
bool operator>=(_In_ const handle_type &other) const noexcept
{
return !operator<(other);
}
protected:
void free_internal() noexcept override
{
CertFreeCertificateContext(m_h);
}
handle_type duplicate_internal(_In_ handle_type h) const noexcept override
{
return CertDuplicateCertificateContext(h);
}
};
class cert_chain_context : public dplhandle<PCCERT_CHAIN_CONTEXT, NULL>
{
WINSTD_DPLHANDLE_IMPL(cert_chain_context, NULL)
public:
virtual ~cert_chain_context()
{
if (m_h != invalid)
free_internal();
}
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
{
handle_type h;
if (CertGetCertificateChain(hChainEngine, pCertContext, pTime, hAdditionalStore, pChainPara, dwFlags, pvReserved, &h)) {
attach(h);
return true;
} else
return false;
}
protected:
void free_internal() noexcept override
{
CertFreeCertificateChain(m_h);
}
handle_type duplicate_internal(_In_ handle_type h) const noexcept override
{
return CertDuplicateCertificateChain(h);
}
};
class cert_store : public handle<HCERTSTORE, NULL>
{
WINSTD_HANDLE_IMPL(cert_store, NULL)
public:
virtual ~cert_store()
{
if (m_h != invalid)
free_internal();
}
bool create(_In_ LPCSTR lpszStoreProvider, _In_ DWORD dwEncodingType, _In_opt_ HCRYPTPROV_LEGACY hCryptProv, _In_ DWORD dwFlags, _In_opt_ const void *pvPara) noexcept
{
handle_type h = CertOpenStore(lpszStoreProvider, dwEncodingType, hCryptProv, dwFlags, pvPara);
if (h != invalid) {
attach(h);
return true;
} else
return false;
}
bool create(_In_opt_ HCRYPTPROV_LEGACY hCryptProv, _In_z_ LPCTSTR szSubsystemProtocol) noexcept
{
handle_type h = CertOpenSystemStore(hCryptProv, szSubsystemProtocol);
if (h != invalid) {
attach(h);
return true;
} else
return false;
}
protected:
void free_internal() noexcept override
{
CertCloseStore(m_h, 0);
}
};
class crypt_prov : public handle<HCRYPTPROV, NULL>
{
WINSTD_HANDLE_IMPL(crypt_prov, NULL)
public:
virtual ~crypt_prov()
{
if (m_h != invalid)
free_internal();
}
bool create(_In_opt_z_ LPCTSTR szContainer, _In_opt_z_ LPCTSTR szProvider, _In_ DWORD dwProvType, _In_ DWORD dwFlags = 0) noexcept
{
handle_type h;
if (CryptAcquireContext(&h, szContainer, szProvider, dwProvType, dwFlags)) {
attach(h);
return true;
} else
return false;
}
protected:
void free_internal() noexcept override
{
CryptReleaseContext(m_h, 0);
}
};
class crypt_hash : public dplhandle<HCRYPTHASH, NULL>
{
WINSTD_DPLHANDLE_IMPL(crypt_hash, NULL)
public:
virtual ~crypt_hash()
{
if (m_h != invalid)
free_internal();
}
bool create(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_opt_ HCRYPTKEY hKey = NULL, _In_opt_ DWORD dwFlags = 0) noexcept
{
handle_type h;
if (CryptCreateHash(hProv, Algid, hKey, dwFlags, &h)) {
attach(h);
return true;
} else
return false;
}
protected:
void free_internal() noexcept override
{
CryptDestroyHash(m_h);
}
handle_type duplicate_internal(_In_ handle_type h) const noexcept override
{
handle_type hNew = invalid;
return CryptDuplicateHash(h, NULL, 0, &hNew) ? hNew : invalid;
}
};
class crypt_key : public dplhandle<HCRYPTKEY, NULL>
{
WINSTD_DPLHANDLE_IMPL(crypt_key, NULL)
public:
virtual ~crypt_key()
{
if (m_h != invalid)
free_internal();
}
bool generate(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ DWORD dwFlags) noexcept
{
handle_type h;
if (CryptGenKey(hProv, Algid, dwFlags, &h)) {
attach(h);
return true;
} else
return false;
}
bool import(_In_ HCRYPTPROV hProv, __in_bcount(dwDataLen) LPCBYTE pbData, _In_ DWORD dwDataLen, _In_ HCRYPTKEY hPubKey, _In_ DWORD dwFlags) noexcept
{
handle_type h;
if (CryptImportKey(hProv, pbData, dwDataLen, hPubKey, dwFlags, &h)) {
attach(h);
return true;
} else
return false;
}
bool import_public(_In_ HCRYPTPROV hCryptProv, _In_ DWORD dwCertEncodingType, _In_ PCERT_PUBLIC_KEY_INFO pInfo) noexcept
{
handle_type h;
if (CryptImportPublicKeyInfo(hCryptProv, dwCertEncodingType, pInfo, &h)) {
attach(h);
return true;
} else
return false;
}
bool derive(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ HCRYPTHASH hBaseData, _In_ DWORD dwFlags) noexcept
{
handle_type h;
if (CryptDeriveKey(hProv, Algid, hBaseData, dwFlags, &h)) {
attach(h);
return true;
} else
return false;
}
bool create_exp1(_In_ HCRYPTPROV hProv, _In_ DWORD dwKeySpec)
{
if (dwKeySpec != AT_KEYEXCHANGE && dwKeySpec != AT_SIGNATURE) {
SetLastError(ERROR_INVALID_PARAMETER);
return false;
}
// Generate the private key.
handle_type h;
if (CryptGenKey(hProv, dwKeySpec, CRYPT_EXPORTABLE, &h)) {
// Export the private key, we'll convert it to a private exponent of one key.
std::vector<BYTE, sanitizing_allocator<BYTE>> key_blob;
if (CryptExportKey(h, 0, PRIVATEKEYBLOB, 0, key_blob)) {
CryptDestroyKey(h);
// Get the byte length of the key.
size_t
size_key = *reinterpret_cast<DWORD*>(&key_blob[12])/8,
size_prime = size_key/2;
// Modify the Exponent in Key BLOB format
// Key BLOB format is documented in SDK
// Convert pubexp in rsapubkey to 1
LPBYTE ptr = &key_blob[16];
*reinterpret_cast<DWORD*>(ptr) = 1;
ptr += sizeof(DWORD);
// Skip modulus, prime1, prime2
ptr += size_key;
ptr += size_prime;
ptr += size_prime;
// Convert exponent1 to 1
ptr[0] = 1;
memset(ptr + 1, 0, size_prime - 1);
ptr += size_prime;
// Convert exponent2 to 1
ptr[0] = 1;
memset(ptr + 1, 0, size_prime - 1);
ptr += size_prime;
// Skip coefficient
ptr += size_prime;
// Convert privateExponent to 1
ptr[0] = 1;
memset(ptr + 1, 0, size_key - 1);
// Import the exponent-of-one private key.
if (CryptImportKey(hProv, key_blob.data(), static_cast<DWORD>(key_blob.size()), 0, 0, &h)) {
attach(h);
return true;
}
} else
CryptDestroyKey(h);
}
return false;
}
protected:
void free_internal() noexcept override
{
CryptDestroyKey(m_h);
}
handle_type duplicate_internal(_In_ handle_type h) const noexcept override
{
handle_type hNew = invalid;
return CryptDuplicateKey(h, NULL, 0, &hNew) ? hNew : invalid;
}
};
#pragma warning(push)
#pragma warning(disable: 26432) // Copy constructor and assignment operator are also present, but not detected by code analysis as they are using base type source object reference.
class data_blob : public DATA_BLOB
{
public:
data_blob() noexcept
{
cbData = 0;
pbData = NULL;
}
data_blob(_In_count_(size) BYTE *data, _In_ DWORD size) noexcept
{
cbData = size;
pbData = data;
}
data_blob(_In_ const DATA_BLOB &other)
{
cbData = other.cbData;
if (cbData) {
pbData = static_cast<BYTE*>(LocalAlloc(LMEM_FIXED, other.cbData));
if (!pbData) throw win_runtime_error("LocalAlloc failed.");
memcpy(pbData, other.pbData, other.cbData);
} else
pbData = NULL;
}
data_blob(_Inout_ data_blob &&other) noexcept
{
cbData = other.cbData;
pbData = other.pbData;
other.cbData = 0;
other.pbData = NULL;
}
virtual ~data_blob()
{
if (pbData != NULL)
LocalFree(pbData);
}
data_blob& operator=(_In_ const DATA_BLOB &other)
{
if (this != &other) {
cbData = other.cbData;
if (pbData)
LocalFree(pbData);
if (cbData) {
pbData = static_cast<BYTE*>(LocalAlloc(LMEM_FIXED, other.cbData));
if (!pbData) throw win_runtime_error("LocalAlloc failed.");
memcpy(pbData, other.pbData, other.cbData);
} else
pbData = NULL;
}
return *this;
}
data_blob& operator=(_Inout_ data_blob &&other) noexcept
{
if (this != &other) {
cbData = other.cbData;
if (pbData)
LocalFree(pbData);
pbData = other.pbData;
other.cbData = 0;
other.pbData = NULL;
}
return *this;
}
DWORD size() const noexcept
{
return cbData;
}
const BYTE* data() const noexcept
{
return pbData;
}
BYTE* data() noexcept
{
return pbData;
}
};
#pragma warning(pop)
}
winstd::cert_chain_context
PCCERT_CHAIN_CONTEXT wrapper class.
Definition: Crypt.h:401
winstd::cert_chain_context::duplicate_internal
handle_type duplicate_internal(handle_type h) const noexcept override
Duplicates the certificate chain context.
Definition: Crypt.h:455
winstd::cert_chain_context::~cert_chain_context
virtual ~cert_chain_context()
Destroys the certificate chain context.
Definition: Crypt.h:410
winstd::cert_chain_context::create
bool create(HCERTCHAINENGINE hChainEngine, PCCERT_CONTEXT pCertContext, LPFILETIME pTime, HCERTSTORE hAdditionalStore, PCERT_CHAIN_PARA pChainPara, DWORD dwFlags, __reserved LPVOID pvReserved=NULL) noexcept
Creates the certificate chain context.
Definition: Crypt.h:425
winstd::cert_chain_context::free_internal
void free_internal() noexcept override
Destroys the certificate chain context.
Definition: Crypt.h:441
winstd::cert_context
PCCERT_CONTEXT wrapper class.
Definition: Crypt.h:252
winstd::cert_context::operator<=
bool operator<=(const handle_type &other) const noexcept
Is certificate less than or equal?
Definition: Crypt.h:353
winstd::cert_context::free_internal
void free_internal() noexcept override
Destroys the certificate context.
Definition: Crypt.h:377
winstd::cert_context::operator==
bool operator==(const handle_type &other) const noexcept
Is certificate equal to?
Definition: Crypt.h:294
winstd::cert_context::duplicate_internal
handle_type duplicate_internal(handle_type h) const noexcept override
Duplicates the certificate context.
Definition: Crypt.h:391
winstd::cert_context::create
bool create(DWORD dwCertEncodingType, LPCBYTE pbCertEncoded, DWORD cbCertEncoded) noexcept
Creates the certificate context.
Definition: Crypt.h:276
winstd::cert_context::operator>=
bool operator>=(const handle_type &other) const noexcept
Is certificate greater than or equal?
Definition: Crypt.h:366
winstd::cert_context::operator>
bool operator>(const handle_type &other) const noexcept
Is certificate greater than?
Definition: Crypt.h:338
winstd::cert_context::operator<
bool operator<(const handle_type &other) const noexcept
Is certificate less than?
Definition: Crypt.h:323
winstd::cert_context::operator!=
bool operator!=(const handle_type &other) const noexcept
Is certificate not equal to?
Definition: Crypt.h:310
winstd::cert_context::~cert_context
virtual ~cert_context()
Destroys the certificate context.
Definition: Crypt.h:261
winstd::cert_store
HCERTSTORE wrapper class.
Definition: Crypt.h:465
winstd::cert_store::create
bool create(LPCSTR lpszStoreProvider, DWORD dwEncodingType, HCRYPTPROV_LEGACY hCryptProv, DWORD dwFlags, const void *pvPara) noexcept
Opens the certificate store.
Definition: Crypt.h:489
winstd::cert_store::~cert_store
virtual ~cert_store()
Closes the certificate store.
Definition: Crypt.h:474
winstd::cert_store::free_internal
void free_internal() noexcept override
Closes the certificate store.
Definition: Crypt.h:524
winstd::cert_store::create
bool create(HCRYPTPROV_LEGACY hCryptProv, LPCTSTR szSubsystemProtocol) noexcept
Opens the most common system certificate store. To open certificate stores with more complex requirem...
Definition: Crypt.h:508
winstd::crypt_hash
HCRYPTHASH wrapper class.
Definition: Crypt.h:584
winstd::crypt_hash::free_internal
void free_internal() noexcept override
Destroys the hash context.
Definition: Crypt.h:624
winstd::crypt_hash::create
bool create(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey=NULL, DWORD dwFlags=0) noexcept
Creates the hash context.
Definition: Crypt.h:608
winstd::crypt_hash::~crypt_hash
virtual ~crypt_hash()
Destroys the hash context.
Definition: Crypt.h:593
winstd::crypt_hash::duplicate_internal
handle_type duplicate_internal(handle_type h) const noexcept override
Duplicates the hash context.
Definition: Crypt.h:638
winstd::crypt_key
HCRYPTKEY wrapper class.
Definition: Crypt.h:649
winstd::crypt_key::generate
bool generate(HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags) noexcept
Generates the key.
Definition: Crypt.h:669
winstd::crypt_key::derive
bool derive(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData, DWORD dwFlags) noexcept
Generates cryptographic session keys derived from a base data value.
Definition: Crypt.h:714
winstd::crypt_key::~crypt_key
virtual ~crypt_key()
Destroys the key.
Definition: Crypt.h:658
winstd::crypt_key::create_exp1
bool create_exp1(HCRYPTPROV hProv, DWORD dwKeySpec)
Creates Exponent-of-one key.
Definition: Crypt.h:732
winstd::crypt_key::duplicate_internal
handle_type duplicate_internal(handle_type h) const noexcept override
Duplicates the key.
Definition: Crypt.h:814
winstd::crypt_key::import_public
bool import_public(HCRYPTPROV hCryptProv, DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo) noexcept
Imports the public key.
Definition: Crypt.h:699
winstd::crypt_key::free_internal
void free_internal() noexcept override
Destroys the key.
Definition: Crypt.h:800
winstd::crypt_prov
HCRYPTPROV wrapper class.
Definition: Crypt.h:534
winstd::crypt_prov::create
bool create(LPCTSTR szContainer, LPCTSTR szProvider, DWORD dwProvType, DWORD dwFlags=0) noexcept
Acquires the cryptographic context.
Definition: Crypt.h:558
winstd::crypt_prov::~crypt_prov
virtual ~crypt_prov()
Releases the cryptographic context.
Definition: Crypt.h:543
winstd::crypt_prov::free_internal
void free_internal() noexcept override
Releases the cryptographic context.
Definition: Crypt.h:574
winstd::data_blob
DATA_BLOB wrapper class.
Definition: Crypt.h:827
winstd::data_blob::data_blob
data_blob(const DATA_BLOB &other)
Duplicate an existing BLOB.
Definition: Crypt.h:850
winstd::data_blob::~data_blob
virtual ~data_blob()
Destroys the BLOB.
Definition: Crypt.h:875
winstd::data_blob::data
BYTE * data() noexcept
Get BLOB buffer.
Definition: Crypt.h:937
winstd::data_blob::data
const BYTE * data() const noexcept
Get BLOB buffer.
Definition: Crypt.h:929
winstd::data_blob::data_blob
data_blob() noexcept
Initializes an empty BLOB.
Definition: Crypt.h:832
winstd::data_blob::data_blob
data_blob(data_blob &&other) noexcept
Move an existing BLOB.
Definition: Crypt.h:864
winstd::data_blob::operator=
data_blob & operator=(data_blob &&other) noexcept
Move an existing BLOB.
Definition: Crypt.h:904
winstd::data_blob::data_blob
data_blob(BYTE *data, DWORD size) noexcept
Initializes a BLOB from existing data.
Definition: Crypt.h:841
winstd::data_blob::size
DWORD size() const noexcept
Get BLOB size.
Definition: Crypt.h:921
winstd::data_blob::operator=
data_blob & operator=(const DATA_BLOB &other)
Copy an existing BLOB.
Definition: Crypt.h:884
winstd::dplhandle
Base abstract template class to support object handle keeping for objects that support handle duplica...
Definition: Common.h:866
winstd::handle
Base abstract template class to support generic object handle keeping.
Definition: Common.h:604
winstd::handle< PCCERT_CONTEXT, INVAL >::handle_type
PCCERT_CONTEXT handle_type
Datatype of the object handle this template class handles.
Definition: Common.h:609
winstd::handle< PCCERT_CONTEXT, INVAL >::m_h
handle_type m_h
Object handle.
Definition: Common.h:855
winstd::handle< PCCERT_CONTEXT, INVAL >::attach
void attach(handle_type h) noexcept
Sets a new object handle for the class.
Definition: Common.h:818
winstd::win_runtime_error
Windows runtime error.
Definition: Common.h:1048
WINSTD_STACK_BUFFER_BYTES
#define WINSTD_STACK_BUFFER_BYTES
Size of the stack buffer in bytes used for initial system function call.
Definition: Common.h:80
WINSTD_DPLHANDLE_IMPL
#define WINSTD_DPLHANDLE_IMPL(C, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition: Common.h:174
WINSTD_HANDLE_IMPL
#define WINSTD_HANDLE_IMPL(C, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition: Common.h:162
winstd::handle< PCCERT_CONTEXT, INVAL >::invalid
static const PCCERT_CONTEXT invalid
Invalid handle value.
Definition: Common.h:614