rand: Use crypto libs instead of CryptGenRandom()
authorNirbheek Chauhan <nirbheek@centricular.com>
Mon, 15 Jun 2020 22:19:14 +0000 (03:49 +0530)
committerNirbheek Chauhan <nirbheek@centricular.com>
Mon, 15 Jun 2020 22:26:53 +0000 (03:56 +0530)
We will always have either openssl or gnutls, which are better sources
for random nonces. Also, CryptGenRandom is deprecated API that's not
available on WinRT/UWP.

stun/rand.c

index cc0927f..1361245 100644 (file)
 
 #include "rand.h"
 
-
-#ifdef _WIN32
-
-#include <windows.h>
-#include <wincrypt.h>
-
-void nice_RAND_nonce (uint8_t *dst, int len)
-{
-  HCRYPTPROV hCryptProv;
-  LPCSTR container = "Libnice key container";
-
-  if(!CryptAcquireContext(&hCryptProv, container, NULL, PROV_RSA_FULL, 0)) {
-    /* non existing container. try to create a new one */
-    // I hope this cast here doesn't cause issues
-    // gcc was complaining about comparing signed and unsigned values
-    if (GetLastError() == (DWORD) NTE_BAD_KEYSET) {
-      if(!CryptAcquireContext(&hCryptProv, container, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET)) {
-        return;
-      }
-    }
-    return;
-  }
-
-  CryptGenRandom (hCryptProv, len, dst);
-
-  CryptReleaseContext(hCryptProv,0);
-}
-#else
-
-#ifdef HAVE_OPENSSL
+#if defined(HAVE_OPENSSL)
 
 #include <openssl/rand.h>
 
@@ -77,7 +48,7 @@ void nice_RAND_nonce (uint8_t *dst, int len)
   RAND_bytes (dst, len);
 }
 
-#else
+#elif defined(HAVE_GNUTLS)
 
 #include <sys/types.h>
 #include <gnutls/gnutls.h>
@@ -88,6 +59,4 @@ void nice_RAND_nonce (uint8_t *dst, int len)
   gnutls_rnd (GNUTLS_RND_NONCE, dst, len);
 }
 
-#endif /* HAVE_OPENSSL */
-
-#endif /* _WIN32 */
+#endif