Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NCryptEnumKeys returns ERROR_NO_MORE_ITEMS and is positive error code #1314

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions client/QCNG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ using namespace Qt::Literals::StringLiterals;

Q_LOGGING_CATEGORY(CNG, "qdigidoc4.QCNG")

template<typename T, typename D = decltype(NCryptFreeObject)>
template<typename T, auto D = NCryptFreeObject>
struct SCOPE
{
T d {};
~SCOPE() { if(d) D(d); }
constexpr operator T() const { return d; }
constexpr T* operator&() { return &d; }
~SCOPE() noexcept { if(d) D(d); }
constexpr operator T() const noexcept { return d; }
constexpr T* operator&() noexcept { return &d; }
};

class QCNG::Private
Expand Down Expand Up @@ -194,21 +194,16 @@ QList<TokenData> QCNG::tokens() const
return data;
};
auto enumKeys = [&result, &prop](const QString &provider, QString reader = {}) {
if(provider == u"SafeCert Key Storage Provider"_s)
{
qCWarning(CNG) << "Skip: Crashes on NCryptEnumKeys";
return;
}
QString scope = uR"(\\.\%1\)"_s.arg(reader);
SCOPE<NCRYPT_PROV_HANDLE> h;
SECURITY_STATUS err = NCryptOpenStorageProvider(&h, LPCWSTR(provider.utf16()), 0);
NCryptKeyName *keyname{};
PVOID pos {};
BCRYPT_PSS_PADDING_INFO rsaPSS { NCRYPT_SHA256_ALGORITHM, 32 };
DWORD size {};
while(SUCCEEDED(NCryptEnumKeys(h, reader.isEmpty() ? nullptr : LPCWSTR(scope.utf16()), &keyname, &pos, NCRYPT_SILENT_FLAG)))
while(NCryptEnumKeys(h, reader.isEmpty() ? nullptr : LPCWSTR(scope.utf16()), &keyname, &pos, NCRYPT_SILENT_FLAG) == ERROR_SUCCESS)
{
SCOPE<NCryptKeyName*,decltype(NCryptFreeBuffer)> keyname_scope{keyname};
SCOPE<NCryptKeyName*,NCryptFreeBuffer> keyname_scope{keyname};
SCOPE<NCRYPT_KEY_HANDLE> key;
err = NCryptOpenKey(h, &key, keyname->pszName, keyname->dwLegacyKeySpec, NCRYPT_SILENT_FLAG);
SslCertificate cert(prop(key, NCRYPT_CERTIFICATE_PROPERTY), QSsl::Der);
Expand Down Expand Up @@ -248,7 +243,7 @@ QList<TokenData> QCNG::tokens() const

qCWarning(CNG) << "Start enumerationg providers";
DWORD count {};
SCOPE<NCryptProviderName*,decltype(NCryptFreeBuffer)> providers {};
SCOPE<NCryptProviderName*,NCryptFreeBuffer> providers {};
NCryptEnumStorageProviders(&count, &providers, NCRYPT_SILENT_FLAG);
for(DWORD i {}; i < count; ++i)
{
Expand Down
Loading