6161#undef WIN32 /* Redefined in MingW/MSVC headers */
6262#endif
6363
64- /* Define RtlGenRandom = SystemFunction036. This is in advapi32.dll. There is
65- * no need to dynamically load this, other software used widely does not.
66- * http://blogs.msdn.com/michael_howard/archive/2005/01/14/353379.aspx
67- * https://docs.microsoft.com/en-us/windows/win32/api/ntsecapi/nf-ntsecapi-rtlgenrandom
68- */
69- #ifdef _WIN32
70- BOOLEAN WINAPI SystemFunction036 (PVOID RandomBuffer , ULONG RandomBufferLength );
71- # ifndef RtlGenRandom
72- # define RtlGenRandom (a ,b ) SystemFunction036(a,b)
73- # endif
74- #endif
7564
7665static int init_by_options (ares_channel channel ,
7766 const struct ares_options * options ,
@@ -87,7 +76,6 @@ static int config_nameserver(struct server_state **servers, int *nservers,
8776static int set_search (ares_channel channel , const char * str );
8877static int set_options (ares_channel channel , const char * str );
8978static const char * try_option (const char * p , const char * q , const char * opt );
90- static int init_id_key (rc4_key * key ,int key_data_len );
9179
9280static int config_sortlist (struct apattern * * sortlist , int * nsort ,
9381 const char * str );
@@ -165,6 +153,7 @@ int ares_init_options(ares_channel *channelptr, struct ares_options *options,
165153 channel -> sock_func_cb_data = NULL ;
166154 channel -> resolvconf_path = NULL ;
167155 channel -> hosts_path = NULL ;
156+ channel -> rand_state = NULL ;
168157
169158 channel -> last_server = 0 ;
170159 channel -> last_timeout_processed = (time_t )now .tv_sec ;
@@ -218,9 +207,13 @@ int ares_init_options(ares_channel *channelptr, struct ares_options *options,
218207 /* Generate random key */
219208
220209 if (status == ARES_SUCCESS ) {
221- status = init_id_key (& channel -> id_key , ARES_ID_KEY_LEN );
210+ channel -> rand_state = ares__init_rand_state ();
211+ if (channel -> rand_state == NULL ) {
212+ status = ARES_ENOMEM ;
213+ }
214+
222215 if (status == ARES_SUCCESS )
223- channel -> next_id = ares__generate_new_id (& channel -> id_key );
216+ channel -> next_id = ares__generate_new_id (channel -> rand_state );
224217 else
225218 DEBUGF (fprintf (stderr , "Error: init_id_key failed: %s\n" ,
226219 ares_strerror (status )));
@@ -242,6 +235,8 @@ int ares_init_options(ares_channel *channelptr, struct ares_options *options,
242235 ares_free (channel -> resolvconf_path );
243236 if (channel -> hosts_path )
244237 ares_free (channel -> hosts_path );
238+ if (channel -> rand_state )
239+ ares__destroy_rand_state (channel -> rand_state );
245240 ares_free (channel );
246241 return status ;
247242 }
@@ -2182,72 +2177,6 @@ static int sortlist_alloc(struct apattern **sortlist, int *nsort,
21822177}
21832178
21842179
2185- /* initialize an rc4 key. If possible a cryptographically secure random key
2186- is generated using a suitable function otherwise the code defaults to
2187- cross-platform albeit less secure mechanism using rand
2188- */
2189- static void randomize_key (unsigned char * key ,int key_data_len )
2190- {
2191- int randomized = 0 ;
2192- int counter = 0 ;
2193- #ifdef WIN32
2194- BOOLEAN res ;
2195-
2196- res = RtlGenRandom (key , key_data_len );
2197- if (res )
2198- randomized = 1 ;
2199-
2200- #else /* !WIN32 */
2201- # ifdef CARES_RANDOM_FILE
2202- FILE * f = fopen (CARES_RANDOM_FILE , "rb" );
2203- if (f ) {
2204- setvbuf (f , NULL , _IONBF , 0 );
2205- counter = aresx_uztosi (fread (key , 1 , key_data_len , f ));
2206- fclose (f );
2207- }
2208- # endif
2209- #endif /* WIN32 */
2210-
2211- if (!randomized ) {
2212- for (;counter < key_data_len ;counter ++ )
2213- key [counter ]= (unsigned char )(rand () % 256 ); /* LCOV_EXCL_LINE */
2214- }
2215- }
2216-
2217- static int init_id_key (rc4_key * key ,int key_data_len )
2218- {
2219- unsigned char index1 ;
2220- unsigned char index2 ;
2221- unsigned char * state ;
2222- short counter ;
2223- unsigned char * key_data_ptr = 0 ;
2224-
2225- key_data_ptr = ares_malloc (key_data_len );
2226- if (!key_data_ptr )
2227- return ARES_ENOMEM ;
2228- memset (key_data_ptr , 0 , key_data_len );
2229-
2230- state = & key -> state [0 ];
2231- for (counter = 0 ; counter < 256 ; counter ++ )
2232- /* unnecessary AND but it keeps some compilers happier */
2233- state [counter ] = (unsigned char )(counter & 0xff );
2234- randomize_key (key -> state ,key_data_len );
2235- key -> x = 0 ;
2236- key -> y = 0 ;
2237- index1 = 0 ;
2238- index2 = 0 ;
2239- for (counter = 0 ; counter < 256 ; counter ++ )
2240- {
2241- index2 = (unsigned char )((key_data_ptr [index1 ] + state [counter ] +
2242- index2 ) % 256 );
2243- ARES_SWAP_BYTE (& state [counter ], & state [index2 ]);
2244-
2245- index1 = (unsigned char )((index1 + 1 ) % key_data_len );
2246- }
2247- ares_free (key_data_ptr );
2248- return ARES_SUCCESS ;
2249- }
2250-
22512180void ares_set_local_ip4 (ares_channel channel , unsigned int local_ip )
22522181{
22532182 channel -> local_ip4 = local_ip ;
0 commit comments