rand.c 7.1 KB

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