rand.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. #ifdef HAVE_ARC4RANDOM
  33. /* Some platforms might have the prototype missing (ubuntu + libressl) */
  34. uint32_t arc4random(void);
  35. #endif
  36. #include <curl/curl.h>
  37. #include "urldata.h"
  38. #include "vtls/vtls.h"
  39. #include "sendf.h"
  40. #include "timeval.h"
  41. #include "rand.h"
  42. #include "escape.h"
  43. /* The last 3 #include files should be in this order */
  44. #include "curl_printf.h"
  45. #include "curl_memory.h"
  46. #include "memdebug.h"
  47. #ifdef WIN32
  48. #if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x600
  49. # define HAVE_WIN_BCRYPTGENRANDOM
  50. # include <bcrypt.h>
  51. # ifdef _MSC_VER
  52. # pragma comment(lib, "bcrypt.lib")
  53. # endif
  54. # ifndef BCRYPT_USE_SYSTEM_PREFERRED_RNG
  55. # define BCRYPT_USE_SYSTEM_PREFERRED_RNG 0x00000002
  56. # endif
  57. # ifndef STATUS_SUCCESS
  58. # define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
  59. # endif
  60. #elif defined(USE_WIN32_CRYPTO)
  61. # include <wincrypt.h>
  62. # ifdef _MSC_VER
  63. # pragma comment(lib, "advapi32.lib")
  64. # endif
  65. #endif
  66. CURLcode Curl_win32_random(unsigned char *entropy, size_t length)
  67. {
  68. memset(entropy, 0, length);
  69. #if defined(HAVE_WIN_BCRYPTGENRANDOM)
  70. if(BCryptGenRandom(NULL, entropy, (ULONG)length,
  71. BCRYPT_USE_SYSTEM_PREFERRED_RNG) != STATUS_SUCCESS)
  72. return CURLE_FAILED_INIT;
  73. return CURLE_OK;
  74. #elif defined(USE_WIN32_CRYPTO)
  75. {
  76. HCRYPTPROV hCryptProv = 0;
  77. if(!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL,
  78. CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
  79. return CURLE_FAILED_INIT;
  80. if(!CryptGenRandom(hCryptProv, (DWORD)length, entropy)) {
  81. CryptReleaseContext(hCryptProv, 0UL);
  82. return CURLE_FAILED_INIT;
  83. }
  84. CryptReleaseContext(hCryptProv, 0UL);
  85. }
  86. return CURLE_OK;
  87. #else
  88. return CURLE_NOT_BUILT_IN;
  89. #endif
  90. }
  91. #endif
  92. static CURLcode randit(struct Curl_easy *data, unsigned int *rnd)
  93. {
  94. unsigned int r;
  95. CURLcode result = CURLE_OK;
  96. static unsigned int randseed;
  97. static bool seeded = FALSE;
  98. #ifdef CURLDEBUG
  99. char *force_entropy = getenv("CURL_ENTROPY");
  100. if(force_entropy) {
  101. if(!seeded) {
  102. unsigned int seed = 0;
  103. size_t elen = strlen(force_entropy);
  104. size_t clen = sizeof(seed);
  105. size_t min = elen < clen ? elen : clen;
  106. memcpy((char *)&seed, force_entropy, min);
  107. randseed = ntohl(seed);
  108. seeded = TRUE;
  109. }
  110. else
  111. randseed++;
  112. *rnd = randseed;
  113. return CURLE_OK;
  114. }
  115. #endif
  116. /* data may be NULL! */
  117. result = Curl_ssl_random(data, (unsigned char *)rnd, sizeof(*rnd));
  118. if(result != CURLE_NOT_BUILT_IN)
  119. /* only if there is no random function in the TLS backend do the non crypto
  120. version, otherwise return result */
  121. return result;
  122. /* ---- non-cryptographic version following ---- */
  123. #ifdef WIN32
  124. if(!seeded) {
  125. result = Curl_win32_random((unsigned char *)rnd, sizeof(*rnd));
  126. if(result != CURLE_NOT_BUILT_IN)
  127. return result;
  128. }
  129. #endif
  130. #ifdef HAVE_ARC4RANDOM
  131. *rnd = (unsigned int)arc4random();
  132. return CURLE_OK;
  133. #endif
  134. #if defined(RANDOM_FILE) && !defined(WIN32)
  135. if(!seeded) {
  136. /* if there's a random file to read a seed from, use it */
  137. int fd = open(RANDOM_FILE, O_RDONLY);
  138. if(fd > -1) {
  139. /* read random data into the randseed variable */
  140. ssize_t nread = read(fd, &randseed, sizeof(randseed));
  141. if(nread == sizeof(randseed))
  142. seeded = TRUE;
  143. close(fd);
  144. }
  145. }
  146. #endif
  147. if(!seeded) {
  148. struct curltime now = Curl_now();
  149. infof(data, "WARNING: using weak random seed");
  150. randseed += (unsigned int)now.tv_usec + (unsigned int)now.tv_sec;
  151. randseed = randseed * 1103515245 + 12345;
  152. randseed = randseed * 1103515245 + 12345;
  153. randseed = randseed * 1103515245 + 12345;
  154. seeded = TRUE;
  155. }
  156. /* Return an unsigned 32-bit pseudo-random number. */
  157. r = randseed = randseed * 1103515245 + 12345;
  158. *rnd = (r << 16) | ((r >> 16) & 0xFFFF);
  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 > 0);
  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. return CURLE_BAD_FUNCTION_ARGUMENT;
  214. num--; /* save one for null-termination */
  215. result = Curl_rand(data, buffer, num/2);
  216. if(result)
  217. return result;
  218. Curl_hexencode(buffer, num/2, rnd, num + 1);
  219. return result;
  220. }
  221. /*
  222. * Curl_rand_alnum() fills the 'rnd' buffer with a given 'num' size with random
  223. * alphanumerical chars PLUS a null-terminating byte.
  224. */
  225. static const char alnum[] =
  226. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  227. CURLcode Curl_rand_alnum(struct Curl_easy *data, unsigned char *rnd,
  228. size_t num)
  229. {
  230. CURLcode result = CURLE_OK;
  231. const int alnumspace = sizeof(alnum) - 1;
  232. unsigned int r;
  233. DEBUGASSERT(num > 1);
  234. num--; /* save one for null-termination */
  235. while(num) {
  236. do {
  237. result = randit(data, &r);
  238. if(result)
  239. return result;
  240. } while(r >= (UINT_MAX - UINT_MAX % alnumspace));
  241. *rnd++ = alnum[r % alnumspace];
  242. num--;
  243. }
  244. *rnd = 0;
  245. return result;
  246. }