From: Gianluigi Tiesi Date: Mon, 1 Jun 2020 18:49:08 +0000 (+0200) Subject: get_cryptgenrandom_seed: compat with old windows + fallback X-Git-Tag: json-c-0.15-20200726~36^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F628%2Fhead;p=thirdparty%2Fjson-c.git get_cryptgenrandom_seed: compat with old windows + fallback --- diff --git a/random_seed.c b/random_seed.c index b5f8a079..833b1a53 100644 --- a/random_seed.c +++ b/random_seed.c @@ -226,28 +226,36 @@ static int get_dev_random_seed(void) #pragma comment(lib, "advapi32.lib") #endif +static int get_time_seed(void); + static int get_cryptgenrandom_seed(void) { HCRYPTPROV hProvider = 0; + DWORD dwFlags = CRYPT_VERIFYCONTEXT; int r; DEBUG_SEED("get_cryptgenrandom_seed"); - if (!CryptAcquireContextW(&hProvider, 0, 0, PROV_RSA_FULL, - CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) + /* WinNT 4 and Win98 do no support CRYPT_SILENT */ + if (LOBYTE(LOWORD(GetVersion())) > 4) + dwFlags |= CRYPT_SILENT; + + if (!CryptAcquireContextA(&hProvider, 0, 0, PROV_RSA_FULL, dwFlags)) { - fprintf(stderr, "error CryptAcquireContextW"); - exit(1); + fprintf(stderr, "error CryptAcquireContextA 0x%08lx", GetLastError()); + r = get_time_seed(); } - - if (!CryptGenRandom(hProvider, sizeof(r), (BYTE *)&r)) + else { - fprintf(stderr, "error CryptGenRandom"); - exit(1); + BOOL ret = CryptGenRandom(hProvider, sizeof(r), (BYTE*)&r); + CryptReleaseContext(hProvider, 0); + if (!ret) + { + fprintf(stderr, "error CryptGenRandom 0x%08lx", GetLastError()); + r = get_time_seed(); + } } - CryptReleaseContext(hProvider, 0); - return r; }