1453.diff 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. diff --git a/library/entropy_poll.c b/library/entropy_poll.c
  2. index 67900c46c8..cefe882d2a 100644
  3. --- a/library/entropy_poll.c
  4. +++ b/library/entropy_poll.c
  5. @@ -54,28 +54,43 @@
  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. {
  40. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  41. }
  42. - if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
  43. + if ( !BCRYPT_SUCCESS( BCryptGenRandom( NULL, output, len_as_ulong, BCRYPT_USE_SYSTEM_PREFERRED_RNG ) ) )
  44. {
  45. - CryptReleaseContext( provider, 0 );
  46. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  47. }
  48. - CryptReleaseContext( provider, 0 );
  49. *olen = len;
  50. return( 0 );
  51. diff --git a/library/x509_crt.c b/library/x509_crt.c
  52. index 290c1eb3d1..3cf1743821 100644
  53. --- a/library/x509_crt.c
  54. +++ b/library/x509_crt.c
  55. @@ -65,6 +65,19 @@
  56. #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
  57. #include <windows.h>
  58. +#if defined(_MSC_VER) && _MSC_VER <= 1600
  59. +/* Visual Studio 2010 and earlier issue a warning when both <stdint.h> and
  60. + * <intsafe.h> are included, as they redefine a number of <TYPE>_MAX constants.
  61. + * These constants are guaranteed to be the same, though, so we suppress the
  62. + * warning when including intsafe.h.
  63. + */
  64. +#pragma warning( push )
  65. +#pragma warning( disable : 4005 )
  66. +#endif
  67. +#include <intsafe.h>
  68. +#if defined(_MSC_VER) && _MSC_VER <= 1600
  69. +#pragma warning( pop )
  70. +#endif
  71. #else
  72. #include <time.h>
  73. #endif
  74. @@ -1126,6 +1139,7 @@ int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
  75. char filename[MAX_PATH];
  76. char *p;
  77. size_t len = strlen( path );
  78. + int length_as_int = 0;
  79. WIN32_FIND_DATAW file_data;
  80. HANDLE hFind;
  81. @@ -1140,7 +1154,18 @@ int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
  82. p = filename + len;
  83. filename[len++] = '*';
  84. - w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
  85. + if ( FAILED ( SizeTToInt( len, &length_as_int ) ) )
  86. + return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
  87. +
  88. + /*
  89. + * Note this function uses the code page CP_ACP, and assumes the incoming
  90. + * string is encoded in ANSI, before translating it into Unicode. If the
  91. + * incoming string were changed to be UTF-8, then the length check needs to
  92. + * change to check the number of characters, not the number of bytes, in the
  93. + * incoming string are less than MAX_PATH to avoid a buffer overrun with
  94. + * MultiByteToWideChar().
  95. + */
  96. + w_ret = MultiByteToWideChar( CP_ACP, 0, filename, length_as_int, szDir,
  97. MAX_PATH - 3 );
  98. if( w_ret == 0 )
  99. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  100. @@ -1157,8 +1182,11 @@ int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
  101. if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
  102. continue;
  103. + if ( FAILED( SizeTToInt( wcslen( file_data.cFileName ), &length_as_int ) ) )
  104. + return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
  105. +
  106. w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
  107. - lstrlenW( file_data.cFileName ),
  108. + length_as_int,
  109. p, (int) len - 1,
  110. NULL, NULL );
  111. if( w_ret == 0 )