rand.c 7.5 KB

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