rand.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifdef HAVE_FCNTL_H
  26. #include <fcntl.h>
  27. #endif
  28. #include <curl/curl.h>
  29. #include "vtls/vtls.h"
  30. #include "sendf.h"
  31. #include "rand.h"
  32. /* The last 3 #include files should be in this order */
  33. #include "curl_printf.h"
  34. #include "curl_memory.h"
  35. #include "memdebug.h"
  36. #ifdef WIN32
  37. #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
  38. # define HAVE_MINGW_ORIGINAL
  39. #endif
  40. #if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x600 && \
  41. !defined(HAVE_MINGW_ORIGINAL)
  42. # define HAVE_WIN_BCRYPTGENRANDOM
  43. # include <bcrypt.h>
  44. # ifdef _MSC_VER
  45. # pragma comment(lib, "bcrypt.lib")
  46. # endif
  47. # ifndef BCRYPT_USE_SYSTEM_PREFERRED_RNG
  48. # define BCRYPT_USE_SYSTEM_PREFERRED_RNG 0x00000002
  49. # endif
  50. # ifndef STATUS_SUCCESS
  51. # define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
  52. # endif
  53. #elif defined(USE_WIN32_CRYPTO)
  54. # include <wincrypt.h>
  55. # ifdef _MSC_VER
  56. # pragma comment(lib, "advapi32.lib")
  57. # endif
  58. #endif
  59. CURLcode Curl_win32_random(unsigned char *entropy, size_t length)
  60. {
  61. memset(entropy, 0, length);
  62. #if defined(HAVE_WIN_BCRYPTGENRANDOM)
  63. if(BCryptGenRandom(NULL, entropy, (ULONG)length,
  64. BCRYPT_USE_SYSTEM_PREFERRED_RNG) != STATUS_SUCCESS)
  65. return CURLE_FAILED_INIT;
  66. return CURLE_OK;
  67. #elif defined(USE_WIN32_CRYPTO)
  68. {
  69. HCRYPTPROV hCryptProv = 0;
  70. if(!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL,
  71. CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
  72. return CURLE_FAILED_INIT;
  73. if(!CryptGenRandom(hCryptProv, (DWORD)length, entropy)) {
  74. CryptReleaseContext(hCryptProv, 0UL);
  75. return CURLE_FAILED_INIT;
  76. }
  77. CryptReleaseContext(hCryptProv, 0UL);
  78. }
  79. return CURLE_OK;
  80. #else
  81. return CURLE_NOT_BUILT_IN;
  82. #endif
  83. }
  84. #endif
  85. static CURLcode randit(struct Curl_easy *data, unsigned int *rnd)
  86. {
  87. unsigned int r;
  88. CURLcode result = CURLE_OK;
  89. static unsigned int randseed;
  90. static bool seeded = FALSE;
  91. #ifdef CURLDEBUG
  92. char *force_entropy = getenv("CURL_ENTROPY");
  93. if(force_entropy) {
  94. if(!seeded) {
  95. unsigned int seed = 0;
  96. size_t elen = strlen(force_entropy);
  97. size_t clen = sizeof(seed);
  98. size_t min = elen < clen ? elen : clen;
  99. memcpy((char *)&seed, force_entropy, min);
  100. randseed = ntohl(seed);
  101. seeded = TRUE;
  102. }
  103. else
  104. randseed++;
  105. *rnd = randseed;
  106. return CURLE_OK;
  107. }
  108. #endif
  109. /* data may be NULL! */
  110. result = Curl_ssl_random(data, (unsigned char *)rnd, sizeof(*rnd));
  111. if(result != CURLE_NOT_BUILT_IN)
  112. /* only if there is no random function in the TLS backend do the non crypto
  113. version, otherwise return result */
  114. return result;
  115. /* ---- non-cryptographic version following ---- */
  116. #ifdef WIN32
  117. if(!seeded) {
  118. result = Curl_win32_random((unsigned char *)rnd, sizeof(*rnd));
  119. if(result != CURLE_NOT_BUILT_IN)
  120. return result;
  121. }
  122. #endif
  123. #if defined(RANDOM_FILE) && !defined(WIN32)
  124. if(!seeded) {
  125. /* if there's a random file to read a seed from, use it */
  126. int fd = open(RANDOM_FILE, O_RDONLY);
  127. if(fd > -1) {
  128. /* read random data into the randseed variable */
  129. ssize_t nread = read(fd, &randseed, sizeof(randseed));
  130. if(nread == sizeof(randseed))
  131. seeded = TRUE;
  132. close(fd);
  133. }
  134. }
  135. #endif
  136. if(!seeded) {
  137. struct curltime now = Curl_now();
  138. infof(data, "WARNING: using weak random seed");
  139. randseed += (unsigned int)now.tv_usec + (unsigned int)now.tv_sec;
  140. randseed = randseed * 1103515245 + 12345;
  141. randseed = randseed * 1103515245 + 12345;
  142. randseed = randseed * 1103515245 + 12345;
  143. seeded = TRUE;
  144. }
  145. /* Return an unsigned 32-bit pseudo-random number. */
  146. r = randseed = randseed * 1103515245 + 12345;
  147. *rnd = (r << 16) | ((r >> 16) & 0xFFFF);
  148. return CURLE_OK;
  149. }
  150. /*
  151. * Curl_rand() stores 'num' number of random unsigned integers in the buffer
  152. * 'rndptr' points to.
  153. *
  154. * If libcurl is built without TLS support or with a TLS backend that lacks a
  155. * proper random API (rustls, Gskit or mbedTLS), this function will use "weak"
  156. * random.
  157. *
  158. * When built *with* TLS support and a backend that offers strong random, it
  159. * will return error if it cannot provide strong random values.
  160. *
  161. * NOTE: 'data' may be passed in as NULL when coming from external API without
  162. * easy handle!
  163. *
  164. */
  165. CURLcode Curl_rand(struct Curl_easy *data, unsigned char *rnd, size_t num)
  166. {
  167. CURLcode result = CURLE_BAD_FUNCTION_ARGUMENT;
  168. DEBUGASSERT(num > 0);
  169. while(num) {
  170. unsigned int r;
  171. size_t left = num < sizeof(unsigned int) ? num : sizeof(unsigned int);
  172. result = randit(data, &r);
  173. if(result)
  174. return result;
  175. while(left) {
  176. *rnd++ = (unsigned char)(r & 0xFF);
  177. r >>= 8;
  178. --num;
  179. --left;
  180. }
  181. }
  182. return result;
  183. }
  184. /*
  185. * Curl_rand_hex() fills the 'rnd' buffer with a given 'num' size with random
  186. * hexadecimal digits PLUS a null-terminating byte. It must be an odd number
  187. * size.
  188. */
  189. CURLcode Curl_rand_hex(struct Curl_easy *data, unsigned char *rnd,
  190. size_t num)
  191. {
  192. CURLcode result = CURLE_BAD_FUNCTION_ARGUMENT;
  193. const char *hex = "0123456789abcdef";
  194. unsigned char buffer[128];
  195. unsigned char *bufp = buffer;
  196. DEBUGASSERT(num > 1);
  197. #ifdef __clang_analyzer__
  198. /* This silences a scan-build warning about accessing this buffer with
  199. uninitialized memory. */
  200. memset(buffer, 0, sizeof(buffer));
  201. #endif
  202. if((num/2 >= sizeof(buffer)) || !(num&1))
  203. /* make sure it fits in the local buffer and that it is an odd number! */
  204. return CURLE_BAD_FUNCTION_ARGUMENT;
  205. num--; /* save one for null-termination */
  206. result = Curl_rand(data, buffer, num/2);
  207. if(result)
  208. return result;
  209. while(num) {
  210. /* clang-tidy warns on this line without this comment: */
  211. /* NOLINTNEXTLINE(clang-analyzer-core.UndefinedBinaryOperatorResult) */
  212. *rnd++ = hex[(*bufp & 0xF0)>>4];
  213. *rnd++ = hex[*bufp & 0x0F];
  214. bufp++;
  215. num -= 2;
  216. }
  217. *rnd = 0;
  218. return result;
  219. }