Commit cf7b6169 authored by Christopher Dykes's avatar Christopher Dykes Committed by Facebook Github Bot 7

Handle creating the default crypto context if it doesn't already exist

Summary: It's perfectly possible that the default crypto context simply hasn't been created yet, so try to create it if the initial acquisition fails.

Reviewed By: yfeldblum

Differential Revision: D3673138

fbshipit-source-id: 122955df04055ff4f99513b182375d4388dd0305
parent 86b83461
...@@ -42,8 +42,16 @@ void readRandomDevice(void* data, size_t size) { ...@@ -42,8 +42,16 @@ void readRandomDevice(void* data, size_t size) {
static folly::once_flag flag; static folly::once_flag flag;
static HCRYPTPROV cryptoProv; static HCRYPTPROV cryptoProv;
folly::call_once(flag, [&] { folly::call_once(flag, [&] {
PCHECK(CryptAcquireContext(&cryptoProv, nullptr, nullptr, if (!CryptAcquireContext(&cryptoProv, nullptr, nullptr, PROV_RSA_FULL, 0)) {
PROV_RSA_FULL, 0)); if (GetLastError() == NTE_BAD_KEYSET) {
// Mostly likely cause of this is that no key container
// exists yet, so try to create one.
PCHECK(CryptAcquireContext(
&cryptoProv, nullptr, nullptr, PROV_RSA_FULL, CRYPT_NEWKEYSET));
} else {
LOG(FATAL) << "Failed to acquire the default crypto context.";
}
}
}); });
CHECK(size <= std::numeric_limits<DWORD>::max()); CHECK(size <= std::numeric_limits<DWORD>::max());
PCHECK(CryptGenRandom(cryptoProv, (DWORD)size, (BYTE*)data)); PCHECK(CryptGenRandom(cryptoProv, (DWORD)size, (BYTE*)data));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment