rand.c 6.9 KB

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