1453.diff 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. diff --git a/thirdparty/mbedtls/library/entropy_poll.c b/thirdparty/mbedtls/library/entropy_poll.c
  2. index 3420616a06..57fddd4d62 100644
  3. --- a/thirdparty/mbedtls/library/entropy_poll.c
  4. +++ b/thirdparty/mbedtls/library/entropy_poll.c
  5. @@ -55,26 +55,41 @@
  6. #define _WIN32_WINNT 0x0400
  7. #endif
  8. #include <windows.h>
  9. -#include <wincrypt.h>
  10. +#include <bcrypt.h>
  11. +#if defined(_MSC_VER) && _MSC_VER <= 1600
  12. +/* Visual Studio 2010 and earlier issue a warning when both <stdint.h> and
  13. + * <intsafe.h> are included, as they redefine a number of <TYPE>_MAX constants.
  14. + * These constants are guaranteed to be the same, though, so we suppress the
  15. + * warning when including intsafe.h.
  16. + */
  17. +#pragma warning( push )
  18. +#pragma warning( disable : 4005 )
  19. +#endif
  20. +#include <intsafe.h>
  21. +#if defined(_MSC_VER) && _MSC_VER <= 1600
  22. +#pragma warning( pop )
  23. +#endif
  24. int mbedtls_platform_entropy_poll(void *data, unsigned char *output, size_t len,
  25. size_t *olen)
  26. {
  27. - HCRYPTPROV provider;
  28. + ULONG len_as_ulong = 0;
  29. ((void) data);
  30. *olen = 0;
  31. - if (CryptAcquireContext(&provider, NULL, NULL,
  32. - PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) == FALSE) {
  33. + /*
  34. + * BCryptGenRandom takes ULONG for size, which is smaller than size_t on
  35. + * 64-bit Windows platforms. Ensure len's value can be safely converted into
  36. + * a ULONG.
  37. + */
  38. + if (FAILED(SizeTToULong(len, &len_as_ulong))) {
  39. return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
  40. }
  41. - if (CryptGenRandom(provider, (DWORD) len, output) == FALSE) {
  42. - CryptReleaseContext(provider, 0);
  43. + if (!BCRYPT_SUCCESS(BCryptGenRandom(NULL, output, len_as_ulong, BCRYPT_USE_SYSTEM_PREFERRED_RNG))) {
  44. return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
  45. }
  46. - CryptReleaseContext(provider, 0);
  47. *olen = len;
  48. return 0;