diff options
author | Jay Berkenbilt <ejb@ql.org> | 2019-06-22 21:32:18 +0200 |
---|---|---|
committer | Jay Berkenbilt <ejb@ql.org> | 2019-06-22 23:12:01 +0200 |
commit | 7bd38a3eb34b8248dd48a541feb4ff245acf0bb0 (patch) | |
tree | dc88adb63256320e5669490055aaa5cafdaf2d76 /libqpdf | |
parent | 6c39aa87638f7a6f96a97627ac112fb2022bd3a7 (diff) | |
download | qpdf-7bd38a3eb34b8248dd48a541feb4ff245acf0bb0.tar.zst |
Provide error message in Windows crypto code (fixes #286)
Thanks to github user zdenop for supplying some additional
error-handling code.
Diffstat (limited to 'libqpdf')
-rw-r--r-- | libqpdf/SecureRandomDataProvider.cc | 33 |
1 files changed, 27 insertions, 6 deletions
diff --git a/libqpdf/SecureRandomDataProvider.cc b/libqpdf/SecureRandomDataProvider.cc index 6d40852a..86fb3752 100644 --- a/libqpdf/SecureRandomDataProvider.cc +++ b/libqpdf/SecureRandomDataProvider.cc @@ -62,18 +62,21 @@ class WindowsCryptProvider #endif { if (! CryptAcquireContext(&crypt_prov, - "Container", - NULL, - PROV_RSA_FULL, - CRYPT_NEWKEYSET)) + "Container", + NULL, + PROV_RSA_FULL, + CRYPT_NEWKEYSET)) { throw std::runtime_error( - "unable to acquire crypt context with new keyset"); + "unable to acquire crypt context with new keyset: " + + getErrorMessage()); } } else { - throw std::runtime_error("unable to acquire crypt context"); + throw std::runtime_error( + "unable to acquire crypt context: " + + getErrorMessage()); } } } @@ -84,6 +87,24 @@ class WindowsCryptProvider } HCRYPTPROV crypt_prov; + + private: + std::string getErrorMessage() + { + DWORD errorMessageID = ::GetLastError(); + LPSTR messageBuffer = nullptr; + size_t size = FormatMessageA( + FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorMessageID, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + reinterpret_cast<LPSTR>(&messageBuffer), 0, NULL); + std::string message(messageBuffer, size); + LocalFree(messageBuffer); + return ("error number " + + QUtil::int_to_string_base(errorMessageID, 16) + + ": " + message); + } }; #endif |