]>
Commit | Line | Data |
---|---|---|
d2e9e320 | 1 | /* |
72a7a702 | 2 | * Copyright 1998-2019 The OpenSSL Project Authors. All Rights Reserved. |
aa8f3d76 | 3 | * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved |
19b8d06a | 4 | * |
d2e9e320 RS |
5 | * Licensed under the OpenSSL license (the "License"). You may not use |
6 | * this file except in compliance with the License. You can obtain a copy | |
7 | * in the file LICENSE in the source distribution or at | |
8 | * https://www.openssl.org/source/license.html | |
19b8d06a | 9 | */ |
d2e9e320 | 10 | |
677963e5 | 11 | #include "e_os.h" |
0c994d54 | 12 | #include "crypto/cryptlib.h" |
c7922304 | 13 | #include <openssl/safestack.h> |
d02b48c6 | 14 | |
0f113f3e | 15 | #if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \ |
0f113f3e MC |
16 | defined(__x86_64) || defined(__x86_64__) || \ |
17 | defined(_M_AMD64) || defined(_M_X64) | |
14e21f86 | 18 | |
0f113f3e | 19 | extern unsigned int OPENSSL_ia32cap_P[4]; |
14e21f86 | 20 | |
0f113f3e | 21 | # if defined(OPENSSL_CPUID_OBJ) && !defined(OPENSSL_NO_ASM) && !defined(I386_ONLY) |
b86d57bb AP |
22 | |
23 | /* | |
24 | * Purpose of these minimalistic and character-type-agnostic subroutines | |
25 | * is to break dependency on MSVCRT (on Windows) and locale. This makes | |
26 | * OPENSSL_cpuid_setup safe to use as "constructor". "Character-type- | |
27 | * agnostic" means that they work with either wide or 8-bit characters, | |
28 | * exploiting the fact that first 127 characters can be simply casted | |
29 | * between the sets, while the rest would be simply rejected by ossl_is* | |
30 | * subroutines. | |
31 | */ | |
32 | # ifdef _WIN32 | |
33 | typedef WCHAR variant_char; | |
34 | ||
35 | static variant_char *ossl_getenv(const char *name) | |
36 | { | |
37 | /* | |
38 | * Since we pull only one environment variable, it's simpler to | |
39 | * to just ignore |name| and use equivalent wide-char L-literal. | |
40 | * As well as to ignore excessively long values... | |
41 | */ | |
42 | static WCHAR value[48]; | |
43 | DWORD len = GetEnvironmentVariableW(L"OPENSSL_ia32cap", value, 48); | |
44 | ||
45 | return (len > 0 && len < 48) ? value : NULL; | |
46 | } | |
47 | # else | |
48 | typedef char variant_char; | |
49 | # define ossl_getenv getenv | |
50 | # endif | |
51 | ||
0c994d54 | 52 | # include "crypto/ctype.h" |
b86d57bb AP |
53 | |
54 | static int todigit(variant_char c) | |
55 | { | |
56 | if (ossl_isdigit(c)) | |
57 | return c - '0'; | |
58 | else if (ossl_isxdigit(c)) | |
59 | return ossl_tolower(c) - 'a' + 10; | |
60 | ||
61 | /* return largest base value to make caller terminate the loop */ | |
62 | return 16; | |
63 | } | |
64 | ||
65 | static uint64_t ossl_strtouint64(const variant_char *str) | |
66 | { | |
67 | uint64_t ret = 0; | |
68 | unsigned int digit, base = 10; | |
69 | ||
70 | if (*str == '0') { | |
71 | base = 8, str++; | |
72 | if (ossl_tolower(*str) == 'x') | |
73 | base = 16, str++; | |
74 | } | |
75 | ||
76 | while((digit = todigit(*str++)) < base) | |
77 | ret = ret * base + digit; | |
78 | ||
79 | return ret; | |
80 | } | |
81 | ||
82 | static variant_char *ossl_strchr(const variant_char *str, char srch) | |
83 | { variant_char c; | |
84 | ||
85 | while((c = *str)) { | |
86 | if (c == srch) | |
87 | return (variant_char *)str; | |
88 | str++; | |
89 | } | |
90 | ||
91 | return NULL; | |
92 | } | |
93 | ||
0f113f3e | 94 | # define OPENSSL_CPUID_SETUP |
1c7b2c0e | 95 | typedef uint64_t IA32CAP; |
b86d57bb | 96 | |
2b247cf8 | 97 | void OPENSSL_cpuid_setup(void) |
0f113f3e MC |
98 | { |
99 | static int trigger = 0; | |
100 | IA32CAP OPENSSL_ia32_cpuid(unsigned int *); | |
101 | IA32CAP vec; | |
b86d57bb | 102 | const variant_char *env; |
0f113f3e MC |
103 | |
104 | if (trigger) | |
105 | return; | |
106 | ||
107 | trigger = 1; | |
b86d57bb | 108 | if ((env = ossl_getenv("OPENSSL_ia32cap")) != NULL) { |
0f113f3e | 109 | int off = (env[0] == '~') ? 1 : 0; |
b86d57bb AP |
110 | |
111 | vec = ossl_strtouint64(env + off); | |
112 | ||
6e5a853b AP |
113 | if (off) { |
114 | IA32CAP mask = vec; | |
115 | vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P) & ~mask; | |
116 | if (mask & (1<<24)) { | |
117 | /* | |
118 | * User disables FXSR bit, mask even other capabilities | |
119 | * that operate exclusively on XMM, so we don't have to | |
120 | * double-check all the time. We mask PCLMULQDQ, AMD XOP, | |
121 | * AES-NI and AVX. Formally speaking we don't have to | |
122 | * do it in x86_64 case, but we can safely assume that | |
123 | * x86_64 users won't actually flip this flag. | |
124 | */ | |
125 | vec &= ~((IA32CAP)(1<<1|1<<11|1<<25|1<<28) << 32); | |
126 | } | |
127 | } else if (env[0] == ':') { | |
0f113f3e | 128 | vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P); |
6e5a853b | 129 | } |
0f113f3e | 130 | |
b86d57bb | 131 | if ((env = ossl_strchr(env, ':')) != NULL) { |
d6ee8f3d | 132 | IA32CAP vecx; |
b86d57bb | 133 | |
0f113f3e MC |
134 | env++; |
135 | off = (env[0] == '~') ? 1 : 0; | |
b86d57bb | 136 | vecx = ossl_strtouint64(env + off); |
d6ee8f3d AP |
137 | if (off) { |
138 | OPENSSL_ia32cap_P[2] &= ~(unsigned int)vecx; | |
139 | OPENSSL_ia32cap_P[3] &= ~(unsigned int)(vecx >> 32); | |
140 | } else { | |
141 | OPENSSL_ia32cap_P[2] = (unsigned int)vecx; | |
142 | OPENSSL_ia32cap_P[3] = (unsigned int)(vecx >> 32); | |
143 | } | |
6e5a853b AP |
144 | } else { |
145 | OPENSSL_ia32cap_P[2] = 0; | |
d6ee8f3d | 146 | OPENSSL_ia32cap_P[3] = 0; |
0f113f3e | 147 | } |
6e5a853b | 148 | } else { |
0f113f3e | 149 | vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P); |
6e5a853b | 150 | } |
e303f55f | 151 | |
14e21f86 AP |
152 | /* |
153 | * |(1<<10) sets a reserved bit to signal that variable | |
154 | * was initialized already... This is to avoid interference | |
155 | * with cpuid snippets in ELF .init segment. | |
156 | */ | |
0f113f3e MC |
157 | OPENSSL_ia32cap_P[0] = (unsigned int)vec | (1 << 10); |
158 | OPENSSL_ia32cap_P[1] = (unsigned int)(vec >> 32); | |
14e21f86 | 159 | } |
0f113f3e | 160 | # else |
c5cd28bd | 161 | unsigned int OPENSSL_ia32cap_P[4]; |
0f113f3e | 162 | # endif |
14e21f86 | 163 | #endif |
a00e414f | 164 | #if !defined(OPENSSL_CPUID_SETUP) && !defined(OPENSSL_CPUID_OBJ) |
0f113f3e MC |
165 | void OPENSSL_cpuid_setup(void) |
166 | { | |
167 | } | |
14e21f86 AP |
168 | #endif |
169 | ||
4d3c278c | 170 | #if defined(_WIN32) |
0f113f3e MC |
171 | # include <tchar.h> |
172 | # include <signal.h> | |
173 | # ifdef __WATCOMC__ | |
174 | # if defined(_UNICODE) || defined(__UNICODE__) | |
175 | # define _vsntprintf _vsnwprintf | |
176 | # else | |
177 | # define _vsntprintf _vsnprintf | |
178 | # endif | |
179 | # endif | |
180 | # ifdef _MSC_VER | |
181 | # define alloca _alloca | |
182 | # endif | |
183 | ||
184 | # if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333 | |
b2b4dfcc P |
185 | # ifdef OPENSSL_SYS_WIN_CORE |
186 | ||
187 | int OPENSSL_isservice(void) | |
188 | { | |
189 | /* OneCore API cannot interact with GUI */ | |
190 | return 1; | |
191 | } | |
192 | # else | |
e476f942 | 193 | int OPENSSL_isservice(void) |
0f113f3e MC |
194 | { |
195 | HWINSTA h; | |
196 | DWORD len; | |
197 | WCHAR *name; | |
198 | static union { | |
199 | void *p; | |
156561b0 | 200 | FARPROC f; |
0f113f3e MC |
201 | } _OPENSSL_isservice = { |
202 | NULL | |
203 | }; | |
471d0eb3 AP |
204 | |
205 | if (_OPENSSL_isservice.p == NULL) { | |
156561b0 | 206 | HANDLE mod = GetModuleHandle(NULL); |
f8e1c190 | 207 | FARPROC f = NULL; |
9e4a1c3f | 208 | |
156561b0 | 209 | if (mod != NULL) |
9e4a1c3f AP |
210 | f = GetProcAddress(mod, "_OPENSSL_isservice"); |
211 | if (f == NULL) | |
0f113f3e | 212 | _OPENSSL_isservice.p = (void *)-1; |
9e4a1c3f AP |
213 | else |
214 | _OPENSSL_isservice.f = f; | |
471d0eb3 AP |
215 | } |
216 | ||
217 | if (_OPENSSL_isservice.p != (void *)-1) | |
0f113f3e | 218 | return (*_OPENSSL_isservice.f) (); |
9f2027e5 | 219 | |
9f2027e5 | 220 | h = GetProcessWindowStation(); |
0f113f3e MC |
221 | if (h == NULL) |
222 | return -1; | |
223 | ||
224 | if (GetUserObjectInformationW(h, UOI_NAME, NULL, 0, &len) || | |
225 | GetLastError() != ERROR_INSUFFICIENT_BUFFER) | |
226 | return -1; | |
227 | ||
228 | if (len > 512) | |
229 | return -1; /* paranoia */ | |
230 | len++, len &= ~1; /* paranoia */ | |
231 | name = (WCHAR *)alloca(len + sizeof(WCHAR)); | |
232 | if (!GetUserObjectInformationW(h, UOI_NAME, name, len, &len)) | |
233 | return -1; | |
234 | ||
235 | len++, len &= ~1; /* paranoia */ | |
236 | name[len / sizeof(WCHAR)] = L'\0'; /* paranoia */ | |
b2b4dfcc | 237 | # if 1 |
0f113f3e MC |
238 | /* |
239 | * This doesn't cover "interactive" services [working with real | |
240 | * WinSta0's] nor programs started non-interactively by Task Scheduler | |
241 | * [those are working with SAWinSta]. | |
242 | */ | |
243 | if (wcsstr(name, L"Service-0x")) | |
244 | return 1; | |
b2b4dfcc | 245 | # else |
9f2027e5 | 246 | /* This covers all non-interactive programs such as services. */ |
0f113f3e MC |
247 | if (!wcsstr(name, L"WinSta0")) |
248 | return 1; | |
b2b4dfcc | 249 | # endif |
0f113f3e MC |
250 | else |
251 | return 0; | |
9f2027e5 | 252 | } |
b2b4dfcc | 253 | # endif |
0f113f3e MC |
254 | # else |
255 | int OPENSSL_isservice(void) | |
256 | { | |
257 | return 0; | |
258 | } | |
259 | # endif | |
260 | ||
261 | void OPENSSL_showfatal(const char *fmta, ...) | |
262 | { | |
263 | va_list ap; | |
264 | TCHAR buf[256]; | |
265 | const TCHAR *fmt; | |
2f0ca54c EK |
266 | /* |
267 | * First check if it's a console application, in which case the | |
268 | * error message would be printed to standard error. | |
269 | * Windows CE does not have a concept of a console application, | |
270 | * so we need to guard the check. | |
271 | */ | |
272 | # ifdef STD_ERROR_HANDLE | |
0f113f3e MC |
273 | HANDLE h; |
274 | ||
275 | if ((h = GetStdHandle(STD_ERROR_HANDLE)) != NULL && | |
276 | GetFileType(h) != FILE_TYPE_UNKNOWN) { | |
277 | /* must be console application */ | |
278 | int len; | |
279 | DWORD out; | |
280 | ||
281 | va_start(ap, fmta); | |
282 | len = _vsnprintf((char *)buf, sizeof(buf), fmta, ap); | |
283 | WriteFile(h, buf, len < 0 ? sizeof(buf) : (DWORD) len, &out, NULL); | |
284 | va_end(ap); | |
285 | return; | |
9f2027e5 | 286 | } |
0f113f3e | 287 | # endif |
9f2027e5 | 288 | |
0f113f3e MC |
289 | if (sizeof(TCHAR) == sizeof(char)) |
290 | fmt = (const TCHAR *)fmta; | |
9f2027e5 | 291 | else |
0f113f3e MC |
292 | do { |
293 | int keepgoing; | |
294 | size_t len_0 = strlen(fmta) + 1, i; | |
295 | WCHAR *fmtw; | |
296 | ||
297 | fmtw = (WCHAR *)alloca(len_0 * sizeof(WCHAR)); | |
298 | if (fmtw == NULL) { | |
299 | fmt = (const TCHAR *)L"no stack?"; | |
300 | break; | |
301 | } | |
0f113f3e | 302 | if (!MultiByteToWideChar(CP_ACP, 0, fmta, len_0, fmtw, len_0)) |
0f113f3e MC |
303 | for (i = 0; i < len_0; i++) |
304 | fmtw[i] = (WCHAR)fmta[i]; | |
0f113f3e MC |
305 | for (i = 0; i < len_0; i++) { |
306 | if (fmtw[i] == L'%') | |
307 | do { | |
308 | keepgoing = 0; | |
309 | switch (fmtw[i + 1]) { | |
310 | case L'0': | |
311 | case L'1': | |
312 | case L'2': | |
313 | case L'3': | |
314 | case L'4': | |
315 | case L'5': | |
316 | case L'6': | |
317 | case L'7': | |
318 | case L'8': | |
319 | case L'9': | |
320 | case L'.': | |
321 | case L'*': | |
322 | case L'-': | |
323 | i++; | |
324 | keepgoing = 1; | |
325 | break; | |
326 | case L's': | |
327 | fmtw[i + 1] = L'S'; | |
328 | break; | |
329 | case L'S': | |
330 | fmtw[i + 1] = L's'; | |
331 | break; | |
332 | case L'c': | |
333 | fmtw[i + 1] = L'C'; | |
334 | break; | |
335 | case L'C': | |
336 | fmtw[i + 1] = L'c'; | |
337 | break; | |
338 | } | |
339 | } while (keepgoing); | |
340 | } | |
341 | fmt = (const TCHAR *)fmtw; | |
342 | } while (0); | |
343 | ||
344 | va_start(ap, fmta); | |
bdcb1a2c DSH |
345 | _vsntprintf(buf, OSSL_NELEM(buf) - 1, fmt, ap); |
346 | buf[OSSL_NELEM(buf) - 1] = _T('\0'); | |
0f113f3e MC |
347 | va_end(ap); |
348 | ||
349 | # if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333 | |
b2b4dfcc P |
350 | # ifdef OPENSSL_SYS_WIN_CORE |
351 | /* ONECORE is always NONGUI and NT >= 0x0601 */ | |
352 | ||
353 | /* | |
354 | * TODO: (For non GUI and no std error cases) | |
3b09585b | 355 | * Add event logging feature here. |
b2b4dfcc | 356 | */ |
3b09585b | 357 | |
b2b4dfcc P |
358 | # if !defined(NDEBUG) |
359 | /* | |
360 | * We are in a situation where we tried to report a critical | |
361 | * error and this failed for some reason. As a last resort, | |
362 | * in debug builds, send output to the debugger or any other | |
363 | * tool like DebugView which can monitor the output. | |
364 | */ | |
365 | OutputDebugString(buf); | |
366 | # endif | |
367 | # else | |
0f113f3e MC |
368 | /* this -------------v--- guards NT-specific calls */ |
369 | if (check_winnt() && OPENSSL_isservice() > 0) { | |
4cd94416 GK |
370 | HANDLE hEventLog = RegisterEventSource(NULL, _T("OpenSSL")); |
371 | ||
372 | if (hEventLog != NULL) { | |
373 | const TCHAR *pmsg = buf; | |
374 | ||
375 | if (!ReportEvent(hEventLog, EVENTLOG_ERROR_TYPE, 0, 0, NULL, | |
376 | 1, 0, &pmsg, NULL)) { | |
b2b4dfcc | 377 | # if !defined(NDEBUG) |
4cd94416 GK |
378 | /* |
379 | * We are in a situation where we tried to report a critical | |
380 | * error and this failed for some reason. As a last resort, | |
381 | * in debug builds, send output to the debugger or any other | |
382 | * tool like DebugView which can monitor the output. | |
383 | */ | |
384 | OutputDebugString(pmsg); | |
b2b4dfcc | 385 | # endif |
4cd94416 GK |
386 | } |
387 | ||
388 | (void)DeregisterEventSource(hEventLog); | |
389 | } | |
b2b4dfcc | 390 | } else { |
4cd94416 | 391 | MessageBox(NULL, buf, _T("OpenSSL: FATAL"), MB_OK | MB_ICONERROR); |
b2b4dfcc P |
392 | } |
393 | # endif | |
394 | # else | |
395 | MessageBox(NULL, buf, _T("OpenSSL: FATAL"), MB_OK | MB_ICONERROR); | |
3b09585b | 396 | # endif |
9f2027e5 AP |
397 | } |
398 | #else | |
0f113f3e MC |
399 | void OPENSSL_showfatal(const char *fmta, ...) |
400 | { | |
984d6c60 | 401 | #ifndef OPENSSL_NO_STDIO |
0f113f3e MC |
402 | va_list ap; |
403 | ||
404 | va_start(ap, fmta); | |
405 | vfprintf(stderr, fmta, ap); | |
406 | va_end(ap); | |
984d6c60 | 407 | #endif |
0f113f3e | 408 | } |
9f2027e5 | 409 | |
0f113f3e MC |
410 | int OPENSSL_isservice(void) |
411 | { | |
412 | return 0; | |
9f2027e5 AP |
413 | } |
414 | #endif | |
415 | ||
040d43b3 | 416 | void OPENSSL_die(const char *message, const char *file, int line) |
0f113f3e | 417 | { |
040d43b3 RS |
418 | OPENSSL_showfatal("%s:%d: OpenSSL internal error: %s\n", |
419 | file, line, message); | |
4d3c278c | 420 | #if !defined(_WIN32) |
0f113f3e | 421 | abort(); |
3b58c74c | 422 | #else |
0f113f3e MC |
423 | /* |
424 | * Win32 abort() customarily shows a dialog, but we just did that... | |
425 | */ | |
426 | # if !defined(_WIN32_WCE) | |
427 | raise(SIGABRT); | |
428 | # endif | |
429 | _exit(3); | |
3b58c74c | 430 | #endif |
0f113f3e | 431 | } |
9e88c827 | 432 | |
e33826f0 | 433 | #if !defined(OPENSSL_CPUID_OBJ) |
49791083 KR |
434 | /* |
435 | * The volatile is used to to ensure that the compiler generates code that reads | |
436 | * all values from the array and doesn't try to optimize this away. The standard | |
437 | * doesn't actually require this behavior if the original data pointed to is | |
438 | * not volatile, but compilers do this in practice anyway. | |
439 | * | |
440 | * There are also assembler versions of this function. | |
eb507efb | 441 | */ |
43c56417 | 442 | # undef CRYPTO_memcmp |
49791083 | 443 | int CRYPTO_memcmp(const void * in_a, const void * in_b, size_t len) |
0f113f3e MC |
444 | { |
445 | size_t i; | |
98ab5764 RS |
446 | const volatile unsigned char *a = in_a; |
447 | const volatile unsigned char *b = in_b; | |
0f113f3e | 448 | unsigned char x = 0; |
7c770d57 | 449 | |
0f113f3e MC |
450 | for (i = 0; i < len; i++) |
451 | x |= a[i] ^ b[i]; | |
7c770d57 | 452 | |
0f113f3e MC |
453 | return x; |
454 | } | |
360fc9f4 P |
455 | |
456 | /* | |
457 | * For systems that don't provide an instruction counter register or equivalent. | |
458 | */ | |
459 | uint32_t OPENSSL_rdtsc(void) | |
460 | { | |
461 | return 0; | |
462 | } | |
b6769a38 BE |
463 | |
464 | size_t OPENSSL_instrument_bus(unsigned int *out, size_t cnt) | |
465 | { | |
466 | return 0; | |
467 | } | |
468 | ||
469 | size_t OPENSSL_instrument_bus2(unsigned int *out, size_t cnt, size_t max) | |
470 | { | |
471 | return 0; | |
472 | } | |
e33826f0 | 473 | #endif |