2 * Copyright 1998-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* ====================================================================
11 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
12 * ECDH support in OpenSSL originally developed by
13 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
16 #include "internal/cryptlib_int.h"
17 #include <openssl/safestack.h>
19 #if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
20 defined(__x86_64) || defined(__x86_64__) || \
21 defined(_M_AMD64) || defined(_M_X64)
23 extern unsigned int OPENSSL_ia32cap_P
[4];
25 # if defined(OPENSSL_CPUID_OBJ) && !defined(OPENSSL_NO_ASM) && !defined(I386_ONLY)
27 # define OPENSSL_CPUID_SETUP
28 typedef uint64_t IA32CAP
;
29 void OPENSSL_cpuid_setup(void)
31 static int trigger
= 0;
32 IA32CAP
OPENSSL_ia32_cpuid(unsigned int *);
40 if ((env
= getenv("OPENSSL_ia32cap"))) {
41 int off
= (env
[0] == '~') ? 1 : 0;
43 if (!sscanf(env
+ off
, "%I64i", &vec
))
44 vec
= strtoul(env
+ off
, NULL
, 0);
46 if (!sscanf(env
+ off
, "%lli", (long long *)&vec
))
47 vec
= strtoul(env
+ off
, NULL
, 0);
50 vec
= OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P
) & ~vec
;
51 else if (env
[0] == ':')
52 vec
= OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P
);
54 OPENSSL_ia32cap_P
[2] = 0;
55 if ((env
= strchr(env
, ':'))) {
58 off
= (env
[0] == '~') ? 1 : 0;
59 vecx
= strtoul(env
+ off
, NULL
, 0);
61 OPENSSL_ia32cap_P
[2] &= ~vecx
;
63 OPENSSL_ia32cap_P
[2] = vecx
;
66 vec
= OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P
);
69 * |(1<<10) sets a reserved bit to signal that variable
70 * was initialized already... This is to avoid interference
71 * with cpuid snippets in ELF .init segment.
73 OPENSSL_ia32cap_P
[0] = (unsigned int)vec
| (1 << 10);
74 OPENSSL_ia32cap_P
[1] = (unsigned int)(vec
>> 32);
77 unsigned int OPENSSL_ia32cap_P
[4];
80 int OPENSSL_NONPIC_relocated
= 0;
81 #if !defined(OPENSSL_CPUID_SETUP) && !defined(OPENSSL_CPUID_OBJ)
82 void OPENSSL_cpuid_setup(void)
87 #if defined(_WIN32) && !defined(__CYGWIN__)
91 # if defined(_UNICODE) || defined(__UNICODE__)
92 # define _vsntprintf _vsnwprintf
94 # define _vsntprintf _vsnprintf
98 # define alloca _alloca
101 # if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
102 int OPENSSL_isservice(void)
110 } _OPENSSL_isservice
= {
114 if (_OPENSSL_isservice
.p
== NULL
) {
115 HANDLE mod
= GetModuleHandle(NULL
);
117 _OPENSSL_isservice
.f
= GetProcAddress(mod
, "_OPENSSL_isservice");
118 if (_OPENSSL_isservice
.p
== NULL
)
119 _OPENSSL_isservice
.p
= (void *)-1;
122 if (_OPENSSL_isservice
.p
!= (void *)-1)
123 return (*_OPENSSL_isservice
.f
) ();
125 h
= GetProcessWindowStation();
129 if (GetUserObjectInformationW(h
, UOI_NAME
, NULL
, 0, &len
) ||
130 GetLastError() != ERROR_INSUFFICIENT_BUFFER
)
134 return -1; /* paranoia */
135 len
++, len
&= ~1; /* paranoia */
136 name
= (WCHAR
*)alloca(len
+ sizeof(WCHAR
));
137 if (!GetUserObjectInformationW(h
, UOI_NAME
, name
, len
, &len
))
140 len
++, len
&= ~1; /* paranoia */
141 name
[len
/ sizeof(WCHAR
)] = L
'\0'; /* paranoia */
144 * This doesn't cover "interactive" services [working with real
145 * WinSta0's] nor programs started non-interactively by Task Scheduler
146 * [those are working with SAWinSta].
148 if (wcsstr(name
, L
"Service-0x"))
151 /* This covers all non-interactive programs such as services. */
152 if (!wcsstr(name
, L
"WinSta0"))
159 int OPENSSL_isservice(void)
165 void OPENSSL_showfatal(const char *fmta
, ...)
171 * First check if it's a console application, in which case the
172 * error message would be printed to standard error.
173 * Windows CE does not have a concept of a console application,
174 * so we need to guard the check.
176 # ifdef STD_ERROR_HANDLE
179 if ((h
= GetStdHandle(STD_ERROR_HANDLE
)) != NULL
&&
180 GetFileType(h
) != FILE_TYPE_UNKNOWN
) {
181 /* must be console application */
186 len
= _vsnprintf((char *)buf
, sizeof(buf
), fmta
, ap
);
187 WriteFile(h
, buf
, len
< 0 ? sizeof(buf
) : (DWORD
) len
, &out
, NULL
);
193 if (sizeof(TCHAR
) == sizeof(char))
194 fmt
= (const TCHAR
*)fmta
;
198 size_t len_0
= strlen(fmta
) + 1, i
;
201 fmtw
= (WCHAR
*)alloca(len_0
* sizeof(WCHAR
));
203 fmt
= (const TCHAR
*)L
"no stack?";
206 if (!MultiByteToWideChar(CP_ACP
, 0, fmta
, len_0
, fmtw
, len_0
))
207 for (i
= 0; i
< len_0
; i
++)
208 fmtw
[i
] = (WCHAR
)fmta
[i
];
209 for (i
= 0; i
< len_0
; i
++) {
213 switch (fmtw
[i
+ 1]) {
245 fmt
= (const TCHAR
*)fmtw
;
249 _vsntprintf(buf
, OSSL_NELEM(buf
) - 1, fmt
, ap
);
250 buf
[OSSL_NELEM(buf
) - 1] = _T('\0');
253 # if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
254 /* this -------------v--- guards NT-specific calls */
255 if (check_winnt() && OPENSSL_isservice() > 0) {
256 HANDLE hEventLog
= RegisterEventSource(NULL
, _T("OpenSSL"));
258 if (hEventLog
!= NULL
) {
259 const TCHAR
*pmsg
= buf
;
261 if (!ReportEvent(hEventLog
, EVENTLOG_ERROR_TYPE
, 0, 0, NULL
,
262 1, 0, &pmsg
, NULL
)) {
265 * We are in a situation where we tried to report a critical
266 * error and this failed for some reason. As a last resort,
267 * in debug builds, send output to the debugger or any other
268 * tool like DebugView which can monitor the output.
270 OutputDebugString(pmsg
);
274 (void)DeregisterEventSource(hEventLog
);
278 MessageBox(NULL
, buf
, _T("OpenSSL: FATAL"), MB_OK
| MB_ICONERROR
);
281 void OPENSSL_showfatal(const char *fmta
, ...)
283 #ifndef OPENSSL_NO_STDIO
287 vfprintf(stderr
, fmta
, ap
);
292 int OPENSSL_isservice(void)
298 void OPENSSL_die(const char *message
, const char *file
, int line
)
300 OPENSSL_showfatal("%s:%d: OpenSSL internal error: %s\n",
301 file
, line
, message
);
302 #if !defined(_WIN32) || defined(__CYGWIN__)
306 * Win32 abort() customarily shows a dialog, but we just did that...
308 # if !defined(_WIN32_WCE)
315 #if !defined(OPENSSL_CPUID_OBJ)
316 /* volatile unsigned char* pointers are there because
317 * 1. Accessing a variable declared volatile via a pointer
318 * that lacks a volatile qualifier causes undefined behavior.
319 * 2. When the variable itself is not volatile the compiler is
320 * not required to keep all those reads and can convert
321 * this into canonical memcmp() which doesn't read the whole block.
322 * Pointers to volatile resolve the first problem fully. The second
323 * problem cannot be resolved in any Standard-compliant way but this
324 * works the problem around. Compilers typically react to
325 * pointers to volatile by preserving the reads and writes through them.
326 * The latter is not required by the Standard if the memory pointed to
328 * Pointers themselves are volatile in the function signature to work
329 * around a subtle bug in gcc 4.6+ which causes writes through
330 * pointers to volatile to not be emitted in some rare,
331 * never needed in real life, pieces of code.
333 int CRYPTO_memcmp(const volatile void * volatile in_a
,
334 const volatile void * volatile in_b
,
338 const volatile unsigned char *a
= in_a
;
339 const volatile unsigned char *b
= in_b
;
342 for (i
= 0; i
< len
; i
++)