diff options
Diffstat (limited to 'src/lib/libcrypto/rand/rand_win.c')
-rw-r--r-- | src/lib/libcrypto/rand/rand_win.c | 99 |
1 files changed, 55 insertions, 44 deletions
diff --git a/src/lib/libcrypto/rand/rand_win.c b/src/lib/libcrypto/rand/rand_win.c index 30c69161ef..00dbe4232c 100644 --- a/src/lib/libcrypto/rand/rand_win.c +++ b/src/lib/libcrypto/rand/rand_win.c | |||
@@ -121,6 +121,10 @@ | |||
121 | #include <wincrypt.h> | 121 | #include <wincrypt.h> |
122 | #include <tlhelp32.h> | 122 | #include <tlhelp32.h> |
123 | 123 | ||
124 | /* Limit the time spent walking through the heap, processes, threads and modules to | ||
125 | a maximum of 1000 miliseconds each, unless CryptoGenRandom failed */ | ||
126 | #define MAXDELAY 1000 | ||
127 | |||
124 | /* Intel hardware RNG CSP -- available from | 128 | /* Intel hardware RNG CSP -- available from |
125 | * http://developer.intel.com/design/security/rng/redist_license.htm | 129 | * http://developer.intel.com/design/security/rng/redist_license.htm |
126 | */ | 130 | */ |
@@ -152,6 +156,7 @@ typedef struct tagCURSORINFO | |||
152 | #define CURSOR_SHOWING 0x00000001 | 156 | #define CURSOR_SHOWING 0x00000001 |
153 | #endif /* CURSOR_SHOWING */ | 157 | #endif /* CURSOR_SHOWING */ |
154 | 158 | ||
159 | #if !defined(OPENSSL_SYS_WINCE) | ||
155 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTW)(HCRYPTPROV *, LPCWSTR, LPCWSTR, | 160 | typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTW)(HCRYPTPROV *, LPCWSTR, LPCWSTR, |
156 | DWORD, DWORD); | 161 | DWORD, DWORD); |
157 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV, DWORD, BYTE *); | 162 | typedef BOOL (WINAPI *CRYPTGENRANDOM)(HCRYPTPROV, DWORD, BYTE *); |
@@ -163,7 +168,7 @@ typedef DWORD (WINAPI *GETQUEUESTATUS)(UINT); | |||
163 | 168 | ||
164 | typedef HANDLE (WINAPI *CREATETOOLHELP32SNAPSHOT)(DWORD, DWORD); | 169 | typedef HANDLE (WINAPI *CREATETOOLHELP32SNAPSHOT)(DWORD, DWORD); |
165 | typedef BOOL (WINAPI *CLOSETOOLHELP32SNAPSHOT)(HANDLE); | 170 | typedef BOOL (WINAPI *CLOSETOOLHELP32SNAPSHOT)(HANDLE); |
166 | typedef BOOL (WINAPI *HEAP32FIRST)(LPHEAPENTRY32, DWORD, DWORD); | 171 | typedef BOOL (WINAPI *HEAP32FIRST)(LPHEAPENTRY32, DWORD, size_t); |
167 | typedef BOOL (WINAPI *HEAP32NEXT)(LPHEAPENTRY32); | 172 | typedef BOOL (WINAPI *HEAP32NEXT)(LPHEAPENTRY32); |
168 | typedef BOOL (WINAPI *HEAP32LIST)(HANDLE, LPHEAPLIST32); | 173 | typedef BOOL (WINAPI *HEAP32LIST)(HANDLE, LPHEAPLIST32); |
169 | typedef BOOL (WINAPI *PROCESS32)(HANDLE, LPPROCESSENTRY32); | 174 | typedef BOOL (WINAPI *PROCESS32)(HANDLE, LPPROCESSENTRY32); |
@@ -171,9 +176,7 @@ typedef BOOL (WINAPI *THREAD32)(HANDLE, LPTHREADENTRY32); | |||
171 | typedef BOOL (WINAPI *MODULE32)(HANDLE, LPMODULEENTRY32); | 176 | typedef BOOL (WINAPI *MODULE32)(HANDLE, LPMODULEENTRY32); |
172 | 177 | ||
173 | #include <lmcons.h> | 178 | #include <lmcons.h> |
174 | #ifndef OPENSSL_SYS_WINCE | ||
175 | #include <lmstats.h> | 179 | #include <lmstats.h> |
176 | #endif | ||
177 | #if 1 /* The NET API is Unicode only. It requires the use of the UNICODE | 180 | #if 1 /* The NET API is Unicode only. It requires the use of the UNICODE |
178 | * macro. When UNICODE is defined LPTSTR becomes LPWSTR. LMSTR was | 181 | * macro. When UNICODE is defined LPTSTR becomes LPWSTR. LMSTR was |
179 | * was added to the Platform SDK to allow the NET API to be used in | 182 | * was added to the Platform SDK to allow the NET API to be used in |
@@ -184,26 +187,14 @@ typedef NET_API_STATUS (NET_API_FUNCTION * NETSTATGET) | |||
184 | (LPWSTR, LPWSTR, DWORD, DWORD, LPBYTE*); | 187 | (LPWSTR, LPWSTR, DWORD, DWORD, LPBYTE*); |
185 | typedef NET_API_STATUS (NET_API_FUNCTION * NETFREE)(LPBYTE); | 188 | typedef NET_API_STATUS (NET_API_FUNCTION * NETFREE)(LPBYTE); |
186 | #endif /* 1 */ | 189 | #endif /* 1 */ |
190 | #endif /* !OPENSSL_SYS_WINCE */ | ||
187 | 191 | ||
188 | int RAND_poll(void) | 192 | int RAND_poll(void) |
189 | { | 193 | { |
190 | MEMORYSTATUS m; | 194 | MEMORYSTATUS m; |
191 | HCRYPTPROV hProvider = 0; | 195 | HCRYPTPROV hProvider = 0; |
192 | BYTE buf[64]; | ||
193 | DWORD w; | 196 | DWORD w; |
194 | HWND h; | 197 | int good = 0; |
195 | |||
196 | HMODULE advapi, kernel, user, netapi; | ||
197 | CRYPTACQUIRECONTEXTW acquire = 0; | ||
198 | CRYPTGENRANDOM gen = 0; | ||
199 | CRYPTRELEASECONTEXT release = 0; | ||
200 | #if 1 /* There was previously a problem with NETSTATGET. Currently, this | ||
201 | * section is still experimental, but if all goes well, this conditional | ||
202 | * will be removed | ||
203 | */ | ||
204 | NETSTATGET netstatget = 0; | ||
205 | NETFREE netfree = 0; | ||
206 | #endif /* 1 */ | ||
207 | 198 | ||
208 | /* Determine the OS version we are on so we can turn off things | 199 | /* Determine the OS version we are on so we can turn off things |
209 | * that do not work properly. | 200 | * that do not work properly. |
@@ -212,21 +203,24 @@ int RAND_poll(void) | |||
212 | osverinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO) ; | 203 | osverinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO) ; |
213 | GetVersionEx( &osverinfo ) ; | 204 | GetVersionEx( &osverinfo ) ; |
214 | 205 | ||
215 | #if defined(OPENSSL_SYS_WINCE) && WCEPLATFORM!=MS_HPC_PRO | 206 | #if defined(OPENSSL_SYS_WINCE) |
216 | #ifndef CryptAcquireContext | 207 | # if defined(_WIN32_WCE) && _WIN32_WCE>=300 |
217 | #define CryptAcquireContext CryptAcquireContextW | 208 | /* Even though MSDN says _WIN32_WCE>=210, it doesn't seem to be available |
218 | #endif | 209 | * in commonly available implementations prior 300... */ |
210 | { | ||
211 | BYTE buf[64]; | ||
219 | /* poll the CryptoAPI PRNG */ | 212 | /* poll the CryptoAPI PRNG */ |
220 | /* The CryptoAPI returns sizeof(buf) bytes of randomness */ | 213 | /* The CryptoAPI returns sizeof(buf) bytes of randomness */ |
221 | if (CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) | 214 | if (CryptAcquireContextW(&hProvider, NULL, NULL, PROV_RSA_FULL, |
215 | CRYPT_VERIFYCONTEXT)) | ||
222 | { | 216 | { |
223 | if (CryptGenRandom(hProvider, sizeof(buf), buf)) | 217 | if (CryptGenRandom(hProvider, sizeof(buf), buf)) |
224 | RAND_add(buf, sizeof(buf), sizeof(buf)); | 218 | RAND_add(buf, sizeof(buf), sizeof(buf)); |
225 | CryptReleaseContext(hProvider, 0); | 219 | CryptReleaseContext(hProvider, 0); |
226 | } | 220 | } |
227 | #endif | 221 | } |
228 | 222 | # endif | |
229 | #ifndef OPENSSL_SYS_WINCE | 223 | #else /* OPENSSL_SYS_WINCE */ |
230 | /* | 224 | /* |
231 | * None of below libraries are present on Windows CE, which is | 225 | * None of below libraries are present on Windows CE, which is |
232 | * why we #ifndef the whole section. This also excuses us from | 226 | * why we #ifndef the whole section. This also excuses us from |
@@ -240,17 +234,19 @@ int RAND_poll(void) | |||
240 | * implement own shim routine, which would accept ANSI argument | 234 | * implement own shim routine, which would accept ANSI argument |
241 | * and expand it to Unicode. | 235 | * and expand it to Unicode. |
242 | */ | 236 | */ |
243 | 237 | { | |
244 | /* load functions dynamically - not available on all systems */ | 238 | /* load functions dynamically - not available on all systems */ |
245 | advapi = LoadLibrary(TEXT("ADVAPI32.DLL")); | 239 | HMODULE advapi = LoadLibrary(TEXT("ADVAPI32.DLL")); |
246 | kernel = LoadLibrary(TEXT("KERNEL32.DLL")); | 240 | HMODULE kernel = LoadLibrary(TEXT("KERNEL32.DLL")); |
247 | user = LoadLibrary(TEXT("USER32.DLL")); | 241 | HMODULE user = NULL; |
248 | netapi = LoadLibrary(TEXT("NETAPI32.DLL")); | 242 | HMODULE netapi = LoadLibrary(TEXT("NETAPI32.DLL")); |
249 | 243 | CRYPTACQUIRECONTEXTW acquire = NULL; | |
250 | #if 1 /* There was previously a problem with NETSTATGET. Currently, this | 244 | CRYPTGENRANDOM gen = NULL; |
251 | * section is still experimental, but if all goes well, this conditional | 245 | CRYPTRELEASECONTEXT release = NULL; |
252 | * will be removed | 246 | NETSTATGET netstatget = NULL; |
253 | */ | 247 | NETFREE netfree = NULL; |
248 | BYTE buf[64]; | ||
249 | |||
254 | if (netapi) | 250 | if (netapi) |
255 | { | 251 | { |
256 | netstatget = (NETSTATGET) GetProcAddress(netapi,"NetStatisticsGet"); | 252 | netstatget = (NETSTATGET) GetProcAddress(netapi,"NetStatisticsGet"); |
@@ -280,7 +276,6 @@ int RAND_poll(void) | |||
280 | 276 | ||
281 | if (netapi) | 277 | if (netapi) |
282 | FreeLibrary(netapi); | 278 | FreeLibrary(netapi); |
283 | #endif /* 1 */ | ||
284 | 279 | ||
285 | /* It appears like this can cause an exception deep within ADVAPI32.DLL | 280 | /* It appears like this can cause an exception deep within ADVAPI32.DLL |
286 | * at random times on Windows 2000. Reported by Jeffrey Altman. | 281 | * at random times on Windows 2000. Reported by Jeffrey Altman. |
@@ -356,12 +351,13 @@ int RAND_poll(void) | |||
356 | { | 351 | { |
357 | /* poll the CryptoAPI PRNG */ | 352 | /* poll the CryptoAPI PRNG */ |
358 | /* The CryptoAPI returns sizeof(buf) bytes of randomness */ | 353 | /* The CryptoAPI returns sizeof(buf) bytes of randomness */ |
359 | if (acquire(&hProvider, 0, 0, PROV_RSA_FULL, | 354 | if (acquire(&hProvider, NULL, NULL, PROV_RSA_FULL, |
360 | CRYPT_VERIFYCONTEXT)) | 355 | CRYPT_VERIFYCONTEXT)) |
361 | { | 356 | { |
362 | if (gen(hProvider, sizeof(buf), buf) != 0) | 357 | if (gen(hProvider, sizeof(buf), buf) != 0) |
363 | { | 358 | { |
364 | RAND_add(buf, sizeof(buf), 0); | 359 | RAND_add(buf, sizeof(buf), 0); |
360 | good = 1; | ||
365 | #if 0 | 361 | #if 0 |
366 | printf("randomness from PROV_RSA_FULL\n"); | 362 | printf("randomness from PROV_RSA_FULL\n"); |
367 | #endif | 363 | #endif |
@@ -375,6 +371,7 @@ int RAND_poll(void) | |||
375 | if (gen(hProvider, sizeof(buf), buf) != 0) | 371 | if (gen(hProvider, sizeof(buf), buf) != 0) |
376 | { | 372 | { |
377 | RAND_add(buf, sizeof(buf), sizeof(buf)); | 373 | RAND_add(buf, sizeof(buf), sizeof(buf)); |
374 | good = 1; | ||
378 | #if 0 | 375 | #if 0 |
379 | printf("randomness from PROV_INTEL_SEC\n"); | 376 | printf("randomness from PROV_INTEL_SEC\n"); |
380 | #endif | 377 | #endif |
@@ -386,7 +383,9 @@ int RAND_poll(void) | |||
386 | if (advapi) | 383 | if (advapi) |
387 | FreeLibrary(advapi); | 384 | FreeLibrary(advapi); |
388 | 385 | ||
389 | if (user) | 386 | if ((osverinfo.dwPlatformId != VER_PLATFORM_WIN32_NT || |
387 | !OPENSSL_isservice()) && | ||
388 | (user = LoadLibrary(TEXT("USER32.DLL")))) | ||
390 | { | 389 | { |
391 | GETCURSORINFO cursor; | 390 | GETCURSORINFO cursor; |
392 | GETFOREGROUNDWINDOW win; | 391 | GETFOREGROUNDWINDOW win; |
@@ -399,7 +398,7 @@ int RAND_poll(void) | |||
399 | if (win) | 398 | if (win) |
400 | { | 399 | { |
401 | /* window handle */ | 400 | /* window handle */ |
402 | h = win(); | 401 | HWND h = win(); |
403 | RAND_add(&h, sizeof(h), 0); | 402 | RAND_add(&h, sizeof(h), 0); |
404 | } | 403 | } |
405 | if (cursor) | 404 | if (cursor) |
@@ -464,6 +463,7 @@ int RAND_poll(void) | |||
464 | PROCESSENTRY32 p; | 463 | PROCESSENTRY32 p; |
465 | THREADENTRY32 t; | 464 | THREADENTRY32 t; |
466 | MODULEENTRY32 m; | 465 | MODULEENTRY32 m; |
466 | DWORD stoptime = 0; | ||
467 | 467 | ||
468 | snap = (CREATETOOLHELP32SNAPSHOT) | 468 | snap = (CREATETOOLHELP32SNAPSHOT) |
469 | GetProcAddress(kernel, "CreateToolhelp32Snapshot"); | 469 | GetProcAddress(kernel, "CreateToolhelp32Snapshot"); |
@@ -495,6 +495,7 @@ int RAND_poll(void) | |||
495 | * of entropy. | 495 | * of entropy. |
496 | */ | 496 | */ |
497 | hlist.dwSize = sizeof(HEAPLIST32); | 497 | hlist.dwSize = sizeof(HEAPLIST32); |
498 | if (good) stoptime = GetTickCount() + MAXDELAY; | ||
498 | if (heaplist_first(handle, &hlist)) | 499 | if (heaplist_first(handle, &hlist)) |
499 | do | 500 | do |
500 | { | 501 | { |
@@ -512,18 +513,20 @@ int RAND_poll(void) | |||
512 | && --entrycnt > 0); | 513 | && --entrycnt > 0); |
513 | } | 514 | } |
514 | } while (heaplist_next(handle, | 515 | } while (heaplist_next(handle, |
515 | &hlist)); | 516 | &hlist) && GetTickCount() < stoptime); |
516 | 517 | ||
517 | /* process walking */ | 518 | /* process walking */ |
518 | /* PROCESSENTRY32 contains 9 fields that will change | 519 | /* PROCESSENTRY32 contains 9 fields that will change |
519 | * with each entry. Consider each field a source of | 520 | * with each entry. Consider each field a source of |
520 | * 1 byte of entropy. | 521 | * 1 byte of entropy. |
521 | */ | 522 | */ |
522 | p.dwSize = sizeof(PROCESSENTRY32); | 523 | p.dwSize = sizeof(PROCESSENTRY32); |
524 | |||
525 | if (good) stoptime = GetTickCount() + MAXDELAY; | ||
523 | if (process_first(handle, &p)) | 526 | if (process_first(handle, &p)) |
524 | do | 527 | do |
525 | RAND_add(&p, p.dwSize, 9); | 528 | RAND_add(&p, p.dwSize, 9); |
526 | while (process_next(handle, &p)); | 529 | while (process_next(handle, &p) && GetTickCount() < stoptime); |
527 | 530 | ||
528 | /* thread walking */ | 531 | /* thread walking */ |
529 | /* THREADENTRY32 contains 6 fields that will change | 532 | /* THREADENTRY32 contains 6 fields that will change |
@@ -531,10 +534,11 @@ int RAND_poll(void) | |||
531 | * 1 byte of entropy. | 534 | * 1 byte of entropy. |
532 | */ | 535 | */ |
533 | t.dwSize = sizeof(THREADENTRY32); | 536 | t.dwSize = sizeof(THREADENTRY32); |
537 | if (good) stoptime = GetTickCount() + MAXDELAY; | ||
534 | if (thread_first(handle, &t)) | 538 | if (thread_first(handle, &t)) |
535 | do | 539 | do |
536 | RAND_add(&t, t.dwSize, 6); | 540 | RAND_add(&t, t.dwSize, 6); |
537 | while (thread_next(handle, &t)); | 541 | while (thread_next(handle, &t) && GetTickCount() < stoptime); |
538 | 542 | ||
539 | /* module walking */ | 543 | /* module walking */ |
540 | /* MODULEENTRY32 contains 9 fields that will change | 544 | /* MODULEENTRY32 contains 9 fields that will change |
@@ -542,18 +546,22 @@ int RAND_poll(void) | |||
542 | * 1 byte of entropy. | 546 | * 1 byte of entropy. |
543 | */ | 547 | */ |
544 | m.dwSize = sizeof(MODULEENTRY32); | 548 | m.dwSize = sizeof(MODULEENTRY32); |
549 | if (good) stoptime = GetTickCount() + MAXDELAY; | ||
545 | if (module_first(handle, &m)) | 550 | if (module_first(handle, &m)) |
546 | do | 551 | do |
547 | RAND_add(&m, m.dwSize, 9); | 552 | RAND_add(&m, m.dwSize, 9); |
548 | while (module_next(handle, &m)); | 553 | while (module_next(handle, &m) |
554 | && (GetTickCount() < stoptime)); | ||
549 | if (close_snap) | 555 | if (close_snap) |
550 | close_snap(handle); | 556 | close_snap(handle); |
551 | else | 557 | else |
552 | CloseHandle(handle); | 558 | CloseHandle(handle); |
559 | |||
553 | } | 560 | } |
554 | 561 | ||
555 | FreeLibrary(kernel); | 562 | FreeLibrary(kernel); |
556 | } | 563 | } |
564 | } | ||
557 | #endif /* !OPENSSL_SYS_WINCE */ | 565 | #endif /* !OPENSSL_SYS_WINCE */ |
558 | 566 | ||
559 | /* timer data */ | 567 | /* timer data */ |
@@ -693,6 +701,9 @@ static void readscreen(void) | |||
693 | int y; /* y-coordinate of screen lines to grab */ | 701 | int y; /* y-coordinate of screen lines to grab */ |
694 | int n = 16; /* number of screen lines to grab at a time */ | 702 | int n = 16; /* number of screen lines to grab at a time */ |
695 | 703 | ||
704 | if (GetVersion() >= 0x80000000 || !OPENSSL_isservice()) | ||
705 | return; | ||
706 | |||
696 | /* Create a screen DC and a memory DC compatible to screen DC */ | 707 | /* Create a screen DC and a memory DC compatible to screen DC */ |
697 | hScrDC = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL); | 708 | hScrDC = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL); |
698 | hMemDC = CreateCompatibleDC(hScrDC); | 709 | hMemDC = CreateCompatibleDC(hScrDC); |