Skip to content

Commit

Permalink
simplify getProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
Aidan63 committed Aug 12, 2024
1 parent 29db130 commit bdb8337
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 27 deletions.
56 changes: 32 additions & 24 deletions src/hx/libs/ssl/windows/Cng.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,46 @@ namespace hx
{
namespace windows
{
template<class T>
T getProperty(BCRYPT_HANDLE handle, const wchar_t* prop)
struct CngObject
{
hx::EnterGCFreeZone();
BCRYPT_HANDLE handle;

auto result = DWORD{ 0 };
auto size = ULONG{ 0 };
if (!BCRYPT_SUCCESS(result = BCryptGetProperty(handle, prop, nullptr, 0, &size, 0)))
{
hx::ExitGCFreeZone();
hx::Throw(HX_CSTRING("Failed to get property size : ") + hx::ssl::windows::utils::NTStatusErrorToString(result));
}
virtual ~CngObject() = 0;

CngObject(BCRYPT_HANDLE inHandle) : handle(inHandle) {}

auto buffer = std::vector<UCHAR>(size);
if (!BCRYPT_SUCCESS(result = BCryptGetProperty(handle, prop, buffer.data(), buffer.size(), &size, 0)))
template<class T>
T getProperty(const wchar_t* prop) const
{
hx::EnterGCFreeZone();

auto result = DWORD{ 0 };
auto cb = ULONG{ 0 };

auto buffer = T();
if (!BCRYPT_SUCCESS(result = BCryptGetProperty(handle, prop, reinterpret_cast<PUCHAR>(&buffer), sizeof(T), &cb, 0)))
{
hx::ExitGCFreeZone();
hx::Throw(HX_CSTRING("Failed to get property : ") + hx::ssl::windows::utils::NTStatusErrorToString(result));
}

hx::ExitGCFreeZone();
hx::Throw(HX_CSTRING("Failed to get property : ") + hx::ssl::windows::utils::NTStatusErrorToString(result));

return buffer;
}

hx::ExitGCFreeZone();
void setProperty(const wchar_t* prop) const
{

return static_cast<T>(buffer[0]);
}
}
};

struct CngHash
struct CngHash : CngObject
{
BCRYPT_HASH_HANDLE handle;
std::unique_ptr<std::vector<uint8_t>> object;

CngHash(BCRYPT_HASH_HANDLE inHandle, std::unique_ptr<std::vector<uint8_t>> inObject)
: handle(inHandle)
: CngObject(inHandle)
, object(std::move(inObject))
{
//
Expand Down Expand Up @@ -97,7 +105,7 @@ namespace hx
Array<uint8_t> finish() const
{
auto result = DWORD{ 0 };
auto length = getProperty<DWORD>(handle, BCRYPT_HASH_LENGTH);
auto length = getProperty<DWORD>(BCRYPT_HASH_LENGTH);
auto output = Array<uint8_t>(length, length);

hx::EnterGCFreeZone();
Expand All @@ -114,9 +122,9 @@ namespace hx
}
};

struct CngAlgorithm
struct CngAlgorithm : CngObject
{
CngAlgorithm(BCRYPT_ALG_HANDLE inHandle) : handle(inHandle) {}
CngAlgorithm(BCRYPT_ALG_HANDLE inHandle) : CngObject(inHandle) {}

public:
BCRYPT_ALG_HANDLE handle;
Expand All @@ -126,13 +134,13 @@ namespace hx
BCryptCloseAlgorithmProvider(handle, 0);
}

std::unique_ptr<CngHash> hash()
std::unique_ptr<CngHash> hash() const
{
hx::EnterGCFreeZone();

auto result = DWORD{ 0 };
auto hHash = BCRYPT_HASH_HANDLE();
auto object = std::make_unique<std::vector<uint8_t>>(getProperty<DWORD>(handle, BCRYPT_OBJECT_LENGTH));
auto object = std::make_unique<std::vector<uint8_t>>(getProperty<DWORD>(BCRYPT_OBJECT_LENGTH));

if (!BCRYPT_SUCCESS(result = BCryptCreateHash(handle, &hHash, object->data(), object->size(), nullptr, 0, BCRYPT_HASH_REUSABLE_FLAG)))
{
Expand Down
6 changes: 3 additions & 3 deletions src/hx/libs/ssl/windows/Digest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Array<unsigned char> _hx_ssl_dgst_make(Array<unsigned char> buffer, String algId
hx::strbuf buf;

auto algorithm = hx::ssl::windows::CngAlgorithm::create(algId.wchar_str(&buf));
auto hash = algorithm->hash();
auto hash = algorithm->hash();

hash->hash(buffer);

Expand All @@ -24,7 +24,7 @@ Array<unsigned char> _hx_ssl_dgst_sign(Array<unsigned char> buffer, Dynamic hpke
auto algorithm = algId.wchar_str(&buf);
auto alg = hx::ssl::windows::CngAlgorithm::create(algorithm);
auto hash = alg->hash();
auto hashed = std::vector<uint8_t>(hx::ssl::windows::getProperty<DWORD>(hash->handle, BCRYPT_HASH_LENGTH));
auto hashed = std::vector<uint8_t>(hash->getProperty<DWORD>(BCRYPT_HASH_LENGTH));

hash->hash(buffer);
hash->finish(hashed.data(), hashed.size());
Expand Down Expand Up @@ -63,7 +63,7 @@ bool _hx_ssl_dgst_verify(Array<unsigned char> buffer, Array<unsigned char> sign,
auto algorithm = algId.wchar_str(&buf);
auto alg = hx::ssl::windows::CngAlgorithm::create(algorithm);
auto hash = alg->hash();
auto hashed = std::vector<uint8_t>(hx::ssl::windows::getProperty<DWORD>(hash->handle, BCRYPT_HASH_LENGTH));
auto hashed = std::vector<uint8_t>(hash->getProperty<DWORD>(BCRYPT_HASH_LENGTH));

hash->hash(buffer);
hash->finish(hashed.data(), hashed.size());
Expand Down

0 comments on commit bdb8337

Please sign in to comment.