2 * Copyright 1998-2017 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
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
11 #include "internal/cryptlib_int.h"
13 #include <openssl/safestack.h>
15 #if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
16 defined(__x86_64) || defined(__x86_64__) || \
17 defined(_M_AMD64) || defined(_M_X64)
19 extern unsigned int OPENSSL_ia32cap_P[4];
21 # if defined(OPENSSL_CPUID_OBJ) && !defined(OPENSSL_NO_ASM) && !defined(I386_ONLY)
23 # define OPENSSL_CPUID_SETUP
24 typedef uint64_t IA32CAP;
25 void OPENSSL_cpuid_setup(void)
27 static int trigger = 0;
28 IA32CAP OPENSSL_ia32_cpuid(unsigned int *);
36 if ((env = getenv("OPENSSL_ia32cap"))) {
37 int off = (env[0] == '~') ? 1 : 0;
39 if (!sscanf(env + off, "%I64i", &vec))
40 vec = strtoul(env + off, NULL, 0);
42 if (!sscanf(env + off, "%lli", (long long *)&vec))
43 vec = strtoul(env + off, NULL, 0);
46 vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P) & ~vec;
47 else if (env[0] == ':')
48 vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P);
50 OPENSSL_ia32cap_P[2] = 0;
51 if ((env = strchr(env, ':'))) {
54 off = (env[0] == '~') ? 1 : 0;
55 vecx = strtoul(env + off, NULL, 0);
57 OPENSSL_ia32cap_P[2] &= ~vecx;
59 OPENSSL_ia32cap_P[2] = vecx;
62 vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P);
65 * |(1<<10) sets a reserved bit to signal that variable
66 * was initialized already... This is to avoid interference
67 * with cpuid snippets in ELF .init segment.
69 OPENSSL_ia32cap_P[0] = (unsigned int)vec | (1 << 10);
70 OPENSSL_ia32cap_P[1] = (unsigned int)(vec >> 32);
73 unsigned int OPENSSL_ia32cap_P[4];
76 int OPENSSL_NONPIC_relocated = 0;
77 #if !defined(OPENSSL_CPUID_SETUP) && !defined(OPENSSL_CPUID_OBJ)
78 void OPENSSL_cpuid_setup(void)
83 #if defined(_WIN32) && !defined(__CYGWIN__)
87 # if defined(_UNICODE) || defined(__UNICODE__)
88 # define _vsntprintf _vsnwprintf
90 # define _vsntprintf _vsnprintf
94 # define alloca _alloca
97 # if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
98 int OPENSSL_isservice(void)
106 } _OPENSSL_isservice = {
110 if (_OPENSSL_isservice.p == NULL) {
111 HANDLE mod = GetModuleHandle(NULL);
113 _OPENSSL_isservice.f = GetProcAddress(mod, "_OPENSSL_isservice");
114 if (_OPENSSL_isservice.p == NULL)
115 _OPENSSL_isservice.p = (void *)-1;
118 if (_OPENSSL_isservice.p != (void *)-1)
119 return (*_OPENSSL_isservice.f) ();
121 h = GetProcessWindowStation();
125 if (GetUserObjectInformationW(h, UOI_NAME, NULL, 0, &len) ||
126 GetLastError() != ERROR_INSUFFICIENT_BUFFER)
130 return -1; /* paranoia */
131 len++, len &= ~1; /* paranoia */
132 name = (WCHAR *)alloca(len + sizeof(WCHAR));
133 if (!GetUserObjectInformationW(h, UOI_NAME, name, len, &len))
136 len++, len &= ~1; /* paranoia */
137 name[len / sizeof(WCHAR)] = L'\0'; /* paranoia */
140 * This doesn't cover "interactive" services [working with real
141 * WinSta0's] nor programs started non-interactively by Task Scheduler
142 * [those are working with SAWinSta].
144 if (wcsstr(name, L"Service-0x"))
147 /* This covers all non-interactive programs such as services. */
148 if (!wcsstr(name, L"WinSta0"))
155 int OPENSSL_isservice(void)
161 void OPENSSL_showfatal(const char *fmta, ...)
167 * First check if it's a console application, in which case the
168 * error message would be printed to standard error.
169 * Windows CE does not have a concept of a console application,
170 * so we need to guard the check.
172 # ifdef STD_ERROR_HANDLE
175 if ((h = GetStdHandle(STD_ERROR_HANDLE)) != NULL &&
176 GetFileType(h) != FILE_TYPE_UNKNOWN) {
177 /* must be console application */
182 len = _vsnprintf((char *)buf, sizeof(buf), fmta, ap);
183 WriteFile(h, buf, len < 0 ? sizeof(buf) : (DWORD) len, &out, NULL);
189 if (sizeof(TCHAR) == sizeof(char))
190 fmt = (const TCHAR *)fmta;
194 size_t len_0 = strlen(fmta) + 1, i;
197 fmtw = (WCHAR *)alloca(len_0 * sizeof(WCHAR));
199 fmt = (const TCHAR *)L"no stack?";
202 if (!MultiByteToWideChar(CP_ACP, 0, fmta, len_0, fmtw, len_0))
203 for (i = 0; i < len_0; i++)
204 fmtw[i] = (WCHAR)fmta[i];
205 for (i = 0; i < len_0; i++) {
209 switch (fmtw[i + 1]) {
241 fmt = (const TCHAR *)fmtw;
245 _vsntprintf(buf, OSSL_NELEM(buf) - 1, fmt, ap);
246 buf[OSSL_NELEM(buf) - 1] = _T('\0');
249 # if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
250 /* this -------------v--- guards NT-specific calls */
251 if (check_winnt() && OPENSSL_isservice() > 0) {
252 HANDLE hEventLog = RegisterEventSource(NULL, _T("OpenSSL"));
254 if (hEventLog != NULL) {
255 const TCHAR *pmsg = buf;
257 if (!ReportEvent(hEventLog, EVENTLOG_ERROR_TYPE, 0, 0, NULL,
258 1, 0, &pmsg, NULL)) {
261 * We are in a situation where we tried to report a critical
262 * error and this failed for some reason. As a last resort,
263 * in debug builds, send output to the debugger or any other
264 * tool like DebugView which can monitor the output.
266 OutputDebugString(pmsg);
270 (void)DeregisterEventSource(hEventLog);
274 MessageBox(NULL, buf, _T("OpenSSL: FATAL"), MB_OK | MB_ICONERROR);
277 void OPENSSL_showfatal(const char *fmta, ...)
279 #ifndef OPENSSL_NO_STDIO
283 vfprintf(stderr, fmta, ap);
288 int OPENSSL_isservice(void)
294 void OPENSSL_die(const char *message, const char *file, int line)
296 OPENSSL_showfatal("%s:%d: OpenSSL internal error: %s\n",
297 file, line, message);
298 #if !defined(_WIN32) || defined(__CYGWIN__)
302 * Win32 abort() customarily shows a dialog, but we just did that...
304 # if !defined(_WIN32_WCE)
311 #if !defined(OPENSSL_CPUID_OBJ)
313 * The volatile is used to to ensure that the compiler generates code that reads
314 * all values from the array and doesn't try to optimize this away. The standard
315 * doesn't actually require this behavior if the original data pointed to is
316 * not volatile, but compilers do this in practice anyway.
318 * There are also assembler versions of this function.
320 # undef CRYPTO_memcmp
321 int CRYPTO_memcmp(const void * in_a, const void * in_b, size_t len)
324 const volatile unsigned char *a = in_a;
325 const volatile unsigned char *b = in_b;
328 for (i = 0; i < len; i++)