From 35bc2c5c518da3133cc2810efbfcb10542b44d27 Mon Sep 17 00:00:00 2001 From: Nicholas Tay Date: Sun, 16 Jan 2022 00:33:39 +1100 Subject: Handle errors properly for wincrypt --- passgen.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/passgen.c b/passgen.c index b45e7b6..f4112af 100644 --- a/passgen.c +++ b/passgen.c @@ -55,19 +55,18 @@ int const classes_n = sizeof(classes) / sizeof(classes[0]); #ifdef USE_WINCRYPT -HCRYPTPROV win_rng = NULL; +HCRYPTPROV win_rng; #endif bool init_rng(void) { #ifdef USE_WINCRYPT - CryptAcquireContext( + if (!CryptAcquireContext( &win_rng, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT - ); - if (!win_rng) + )) return false; #elif ! defined (USE_GETENTROPY) && ! defined (USE_WINCRYPT) /* @@ -88,8 +87,16 @@ unsigned int get_rng(void) #ifdef USE_GETENTROPY getentropy(&r, sizeof(r)); #elif defined (USE_WINCRYPT) - /* TODO: This could fail. Figure out how to handle */ - CryptGenRandom(win_rng, sizeof(r), (BYTE *) &r); + /* + * TODO: I'd prefer if this went through the standard cleanup. + * Need to figure out how to do that, given the return value + * here. Maybe check out some other projects to see how this is + * handled, lol + */ + if (!CryptGenRandom(win_rng, sizeof(r), (BYTE *) &r)) { + perror("CryptGenRandom"); + exit(EXIT_FAILURE); + } #else r = rand(); #endif -- cgit