2 * Copyright 1998-2016 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"
12 #include <openssl/safestack.h>
14 #if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
15 defined(__x86_64) || defined(__x86_64__) || \
16 defined(_M_AMD64) || defined(_M_X64)
18 extern unsigned int OPENSSL_ia32cap_P
[4];
20 # if defined(OPENSSL_CPUID_OBJ) && !defined(OPENSSL_NO_ASM) && !defined(I386_ONLY)
22 # define OPENSSL_CPUID_SETUP
23 typedef uint64_t IA32CAP
;
24 void OPENSSL_cpuid_setup(void)
26 static int trigger
= 0;
27 IA32CAP
OPENSSL_ia32_cpuid(unsigned int *);
35 if ((env
= getenv("OPENSSL_ia32cap"))) {
36 int off
= (env
[0] == '~') ? 1 : 0;
38 if (!sscanf(env
+ off
, "%I64i", &vec
))
39 vec
= strtoul(env
+ off
, NULL
, 0);
41 if (!sscanf(env
+ off
, "%lli", (long long *)&vec
))
42 vec
= strtoul(env
+ off
, NULL
, 0);
45 vec
= OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P
) & ~vec
;
46 else if (env
[0] == ':')
47 vec
= OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P
);
49 OPENSSL_ia32cap_P
[2] = 0;
50 if ((env
= strchr(env
, ':'))) {
53 off
= (env
[0] == '~') ? 1 : 0;
54 vecx
= strtoul(env
+ off
, NULL
, 0);
56 OPENSSL_ia32cap_P
[2] &= ~vecx
;
58 OPENSSL_ia32cap_P
[2] = vecx
;
61 vec
= OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P
);
64 * |(1<<10) sets a reserved bit to signal that variable
65 * was initialized already... This is to avoid interference
66 * with cpuid snippets in ELF .init segment.
68 OPENSSL_ia32cap_P
[0] = (unsigned int)vec
| (1 << 10);
69 OPENSSL_ia32cap_P
[1] = (unsigned int)(vec
>> 32);
72 unsigned int OPENSSL_ia32cap_P
[4];
75 int OPENSSL_NONPIC_relocated
= 0;
76 #if !defined(OPENSSL_CPUID_SETUP) && !defined(OPENSSL_CPUID_OBJ)
77 void OPENSSL_cpuid_setup(void)
82 #if defined(_WIN32) && !defined(__CYGWIN__)
86 # if defined(_UNICODE) || defined(__UNICODE__)
87 # define _vsntprintf _vsnwprintf
89 # define _vsntprintf _vsnprintf
93 # define alloca _alloca
96 # if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
97 int OPENSSL_isservice(void)
105 } _OPENSSL_isservice
= {
109 if (_OPENSSL_isservice
.p
== NULL
) {
110 HANDLE mod
= GetModuleHandle(NULL
);
112 _OPENSSL_isservice
.f
= GetProcAddress(mod
, "_OPENSSL_isservice");
113 if (_OPENSSL_isservice
.p
== NULL
)
114 _OPENSSL_isservice
.p
= (void *)-1;
117 if (_OPENSSL_isservice
.p
!= (void *)-1)
118 return (*_OPENSSL_isservice
.f
) ();
120 h
= GetProcessWindowStation();
124 if (GetUserObjectInformationW(h
, UOI_NAME
, NULL
, 0, &len
) ||
125 GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
129 return -1; /* paranoia */
130 len
++, len
&= ~1; /* paranoia */
131 name
= (WCHAR
*)alloca(len
+ sizeof(WCHAR
));
132 if (!GetUserObjectInformationW(h
, UOI_NAME
, name
, len
, &len
))
135 len
++, len
&= ~1; /* paranoia */
136 name
[len
/ sizeof(WCHAR
)] = L
'\0'; /* paranoia */
139 * This doesn't cover "interactive" services [working with real
140 * WinSta0's] nor programs started non-interactively by Task Scheduler
141 * [those are working with SAWinSta].
143 if (wcsstr(name
, L
"Service-0x"))
146 /* This covers all non-interactive programs such as services. */
147 if (!wcsstr(name
, L
"WinSta0"))
154 int OPENSSL_isservice(void)
160 void OPENSSL_showfatal(const char *fmta
, ...)
166 * First check if it's a console application, in which case the
167 * error message would be printed to standard error.
168 * Windows CE does not have a concept of a console application,
169 * so we need to guard the check.
171 # ifdef STD_ERROR_HANDLE
174 if ((h
= GetStdHandle(STD_ERROR_HANDLE
)) != NULL
&&
175 GetFileType(h
) != FILE_TYPE_UNKNOWN
) {
176 /* must be console application */
181 len
= _vsnprintf((char *)buf
, sizeof(buf
), fmta
, ap
);
182 WriteFile(h
, buf
, len
< 0 ? sizeof(buf
) : (DWORD
) len
, &out
, NULL
);
188 if (sizeof(TCHAR
) == sizeof(char))
189 fmt
= (const TCHAR
*)fmta
;
193 size_t len_0
= strlen(fmta
) + 1, i
;
196 fmtw
= (WCHAR
*)alloca(len_0
* sizeof(WCHAR
));
198 fmt
= (const TCHAR
*)L
"no stack?";
201 if (!MultiByteToWideChar(CP_ACP
, 0, fmta
, len_0
, fmtw
, len_0
))
202 for (i
= 0; i
< len_0
; i
++)
203 fmtw
[i
] = (WCHAR
)fmta
[i
];
204 for (i
= 0; i
< len_0
; i
++) {
208 switch (fmtw
[i
+ 1]) {
240 fmt
= (const TCHAR
*)fmtw
;
244 _vsntprintf(buf
, OSSL_NELEM(buf
) - 1, fmt
, ap
);
245 buf
[OSSL_NELEM(buf
) - 1] = _T('\0');
248 # if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
249 /* this -------------v--- guards NT-specific calls */
250 if (check_winnt() && OPENSSL_isservice() > 0) {
251 HANDLE hEventLog
= RegisterEventSource(NULL
, _T("OpenSSL"));
253 if (hEventLog
!= NULL
) {
254 const TCHAR
*pmsg
= buf
;
256 if (!ReportEvent(hEventLog
, EVENTLOG_ERROR_TYPE
, 0, 0, NULL
,
257 1, 0, &pmsg
, NULL
)) {
260 * We are in a situation where we tried to report a critical
261 * error and this failed for some reason. As a last resort,
262 * in debug builds, send output to the debugger or any other
263 * tool like DebugView which can monitor the output.
265 OutputDebugString(pmsg
);
269 (void)DeregisterEventSource(hEventLog
);
273 MessageBox(NULL
, buf
, _T("OpenSSL: FATAL"), MB_OK
| MB_ICONERROR
);
276 void OPENSSL_showfatal(const char *fmta
, ...)
278 #ifndef OPENSSL_NO_STDIO
282 vfprintf(stderr
, fmta
, ap
);
287 int OPENSSL_isservice(void)
293 void OPENSSL_die(const char *message
, const char *file
, int line
)
295 OPENSSL_showfatal("%s:%d: OpenSSL internal error: %s\n",
296 file
, line
, message
);
297 #if !defined(_WIN32) || defined(__CYGWIN__)
301 * Win32 abort() customarily shows a dialog, but we just did that...
303 # if !defined(_WIN32_WCE)
310 #if !defined(OPENSSL_CPUID_OBJ)
312 * The volatile is used to to ensure that the compiler generates code that reads
313 * all values from the array and doesn't try to optimize this away. The standard
314 * doesn't actually require this behavior if the original data pointed to is
315 * not volatile, but compilers do this in practice anyway.
317 * There are also assembler versions of this function.
319 # undef CRYPTO_memcmp
320 int CRYPTO_memcmp(const void * in_a
, const void * in_b
, size_t len
)
323 const volatile unsigned char *a
= in_a
;
324 const volatile unsigned char *b
= in_b
;
327 for (i
= 0; i
< len
; i
++)