cryptlib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Copyright 1998-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. *
  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
  9. */
  10. #include "e_os.h"
  11. #include "internal/cryptlib_int.h"
  12. #include <openssl/safestack.h>
  13. #if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
  14. defined(__x86_64) || defined(__x86_64__) || \
  15. defined(_M_AMD64) || defined(_M_X64)
  16. extern unsigned int OPENSSL_ia32cap_P[4];
  17. # if defined(OPENSSL_CPUID_OBJ) && !defined(OPENSSL_NO_ASM) && !defined(I386_ONLY)
  18. #include <stdio.h>
  19. # define OPENSSL_CPUID_SETUP
  20. typedef uint64_t IA32CAP;
  21. void OPENSSL_cpuid_setup(void)
  22. {
  23. static int trigger = 0;
  24. IA32CAP OPENSSL_ia32_cpuid(unsigned int *);
  25. IA32CAP vec;
  26. char *env;
  27. if (trigger)
  28. return;
  29. trigger = 1;
  30. if ((env = getenv("OPENSSL_ia32cap"))) {
  31. int off = (env[0] == '~') ? 1 : 0;
  32. # if defined(_WIN32)
  33. if (!sscanf(env + off, "%I64i", &vec))
  34. vec = strtoul(env + off, NULL, 0);
  35. # else
  36. if (!sscanf(env + off, "%lli", (long long *)&vec))
  37. vec = strtoul(env + off, NULL, 0);
  38. # endif
  39. if (off) {
  40. IA32CAP mask = vec;
  41. vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P) & ~mask;
  42. if (mask & (1<<24)) {
  43. /*
  44. * User disables FXSR bit, mask even other capabilities
  45. * that operate exclusively on XMM, so we don't have to
  46. * double-check all the time. We mask PCLMULQDQ, AMD XOP,
  47. * AES-NI and AVX. Formally speaking we don't have to
  48. * do it in x86_64 case, but we can safely assume that
  49. * x86_64 users won't actually flip this flag.
  50. */
  51. vec &= ~((IA32CAP)(1<<1|1<<11|1<<25|1<<28) << 32);
  52. }
  53. } else if (env[0] == ':') {
  54. vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P);
  55. }
  56. if ((env = strchr(env, ':'))) {
  57. IA32CAP vecx;
  58. env++;
  59. off = (env[0] == '~') ? 1 : 0;
  60. # if defined(_WIN32)
  61. if (!sscanf(env + off, "%I64i", &vecx))
  62. vecx = strtoul(env + off, NULL, 0);
  63. # else
  64. if (!sscanf(env + off, "%lli", (long long *)&vecx))
  65. vecx = strtoul(env + off, NULL, 0);
  66. # endif
  67. if (off) {
  68. OPENSSL_ia32cap_P[2] &= ~(unsigned int)vecx;
  69. OPENSSL_ia32cap_P[3] &= ~(unsigned int)(vecx >> 32);
  70. } else {
  71. OPENSSL_ia32cap_P[2] = (unsigned int)vecx;
  72. OPENSSL_ia32cap_P[3] = (unsigned int)(vecx >> 32);
  73. }
  74. } else {
  75. OPENSSL_ia32cap_P[2] = 0;
  76. OPENSSL_ia32cap_P[3] = 0;
  77. }
  78. } else {
  79. vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P);
  80. }
  81. /*
  82. * |(1<<10) sets a reserved bit to signal that variable
  83. * was initialized already... This is to avoid interference
  84. * with cpuid snippets in ELF .init segment.
  85. */
  86. OPENSSL_ia32cap_P[0] = (unsigned int)vec | (1 << 10);
  87. OPENSSL_ia32cap_P[1] = (unsigned int)(vec >> 32);
  88. }
  89. # else
  90. unsigned int OPENSSL_ia32cap_P[4];
  91. # endif
  92. #endif
  93. int OPENSSL_NONPIC_relocated = 0;
  94. #if !defined(OPENSSL_CPUID_SETUP) && !defined(OPENSSL_CPUID_OBJ)
  95. void OPENSSL_cpuid_setup(void)
  96. {
  97. }
  98. #endif
  99. #if defined(_WIN32)
  100. # include <tchar.h>
  101. # include <signal.h>
  102. # ifdef __WATCOMC__
  103. # if defined(_UNICODE) || defined(__UNICODE__)
  104. # define _vsntprintf _vsnwprintf
  105. # else
  106. # define _vsntprintf _vsnprintf
  107. # endif
  108. # endif
  109. # ifdef _MSC_VER
  110. # define alloca _alloca
  111. # endif
  112. # if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
  113. int OPENSSL_isservice(void)
  114. {
  115. HWINSTA h;
  116. DWORD len;
  117. WCHAR *name;
  118. static union {
  119. void *p;
  120. FARPROC f;
  121. } _OPENSSL_isservice = {
  122. NULL
  123. };
  124. if (_OPENSSL_isservice.p == NULL) {
  125. HANDLE mod = GetModuleHandle(NULL);
  126. if (mod != NULL)
  127. _OPENSSL_isservice.f = GetProcAddress(mod, "_OPENSSL_isservice");
  128. if (_OPENSSL_isservice.p == NULL)
  129. _OPENSSL_isservice.p = (void *)-1;
  130. }
  131. if (_OPENSSL_isservice.p != (void *)-1)
  132. return (*_OPENSSL_isservice.f) ();
  133. h = GetProcessWindowStation();
  134. if (h == NULL)
  135. return -1;
  136. if (GetUserObjectInformationW(h, UOI_NAME, NULL, 0, &len) ||
  137. GetLastError() != ERROR_INSUFFICIENT_BUFFER)
  138. return -1;
  139. if (len > 512)
  140. return -1; /* paranoia */
  141. len++, len &= ~1; /* paranoia */
  142. name = (WCHAR *)alloca(len + sizeof(WCHAR));
  143. if (!GetUserObjectInformationW(h, UOI_NAME, name, len, &len))
  144. return -1;
  145. len++, len &= ~1; /* paranoia */
  146. name[len / sizeof(WCHAR)] = L'\0'; /* paranoia */
  147. # if 1
  148. /*
  149. * This doesn't cover "interactive" services [working with real
  150. * WinSta0's] nor programs started non-interactively by Task Scheduler
  151. * [those are working with SAWinSta].
  152. */
  153. if (wcsstr(name, L"Service-0x"))
  154. return 1;
  155. # else
  156. /* This covers all non-interactive programs such as services. */
  157. if (!wcsstr(name, L"WinSta0"))
  158. return 1;
  159. # endif
  160. else
  161. return 0;
  162. }
  163. # else
  164. int OPENSSL_isservice(void)
  165. {
  166. return 0;
  167. }
  168. # endif
  169. void OPENSSL_showfatal(const char *fmta, ...)
  170. {
  171. va_list ap;
  172. TCHAR buf[256];
  173. const TCHAR *fmt;
  174. /*
  175. * First check if it's a console application, in which case the
  176. * error message would be printed to standard error.
  177. * Windows CE does not have a concept of a console application,
  178. * so we need to guard the check.
  179. */
  180. # ifdef STD_ERROR_HANDLE
  181. HANDLE h;
  182. if ((h = GetStdHandle(STD_ERROR_HANDLE)) != NULL &&
  183. GetFileType(h) != FILE_TYPE_UNKNOWN) {
  184. /* must be console application */
  185. int len;
  186. DWORD out;
  187. va_start(ap, fmta);
  188. len = _vsnprintf((char *)buf, sizeof(buf), fmta, ap);
  189. WriteFile(h, buf, len < 0 ? sizeof(buf) : (DWORD) len, &out, NULL);
  190. va_end(ap);
  191. return;
  192. }
  193. # endif
  194. if (sizeof(TCHAR) == sizeof(char))
  195. fmt = (const TCHAR *)fmta;
  196. else
  197. do {
  198. int keepgoing;
  199. size_t len_0 = strlen(fmta) + 1, i;
  200. WCHAR *fmtw;
  201. fmtw = (WCHAR *)alloca(len_0 * sizeof(WCHAR));
  202. if (fmtw == NULL) {
  203. fmt = (const TCHAR *)L"no stack?";
  204. break;
  205. }
  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++) {
  210. if (fmtw[i] == L'%')
  211. do {
  212. keepgoing = 0;
  213. switch (fmtw[i + 1]) {
  214. case L'0':
  215. case L'1':
  216. case L'2':
  217. case L'3':
  218. case L'4':
  219. case L'5':
  220. case L'6':
  221. case L'7':
  222. case L'8':
  223. case L'9':
  224. case L'.':
  225. case L'*':
  226. case L'-':
  227. i++;
  228. keepgoing = 1;
  229. break;
  230. case L's':
  231. fmtw[i + 1] = L'S';
  232. break;
  233. case L'S':
  234. fmtw[i + 1] = L's';
  235. break;
  236. case L'c':
  237. fmtw[i + 1] = L'C';
  238. break;
  239. case L'C':
  240. fmtw[i + 1] = L'c';
  241. break;
  242. }
  243. } while (keepgoing);
  244. }
  245. fmt = (const TCHAR *)fmtw;
  246. } while (0);
  247. va_start(ap, fmta);
  248. _vsntprintf(buf, OSSL_NELEM(buf) - 1, fmt, ap);
  249. buf[OSSL_NELEM(buf) - 1] = _T('\0');
  250. va_end(ap);
  251. # if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
  252. /* this -------------v--- guards NT-specific calls */
  253. if (check_winnt() && OPENSSL_isservice() > 0) {
  254. HANDLE hEventLog = RegisterEventSource(NULL, _T("OpenSSL"));
  255. if (hEventLog != NULL) {
  256. const TCHAR *pmsg = buf;
  257. if (!ReportEvent(hEventLog, EVENTLOG_ERROR_TYPE, 0, 0, NULL,
  258. 1, 0, &pmsg, NULL)) {
  259. #if defined(DEBUG)
  260. /*
  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.
  265. */
  266. OutputDebugString(pmsg);
  267. #endif
  268. }
  269. (void)DeregisterEventSource(hEventLog);
  270. }
  271. } else
  272. # endif
  273. MessageBox(NULL, buf, _T("OpenSSL: FATAL"), MB_OK | MB_ICONERROR);
  274. }
  275. #else
  276. void OPENSSL_showfatal(const char *fmta, ...)
  277. {
  278. #ifndef OPENSSL_NO_STDIO
  279. va_list ap;
  280. va_start(ap, fmta);
  281. vfprintf(stderr, fmta, ap);
  282. va_end(ap);
  283. #endif
  284. }
  285. int OPENSSL_isservice(void)
  286. {
  287. return 0;
  288. }
  289. #endif
  290. void OPENSSL_die(const char *message, const char *file, int line)
  291. {
  292. OPENSSL_showfatal("%s:%d: OpenSSL internal error: %s\n",
  293. file, line, message);
  294. #if !defined(_WIN32)
  295. abort();
  296. #else
  297. /*
  298. * Win32 abort() customarily shows a dialog, but we just did that...
  299. */
  300. # if !defined(_WIN32_WCE)
  301. raise(SIGABRT);
  302. # endif
  303. _exit(3);
  304. #endif
  305. }
  306. #if !defined(OPENSSL_CPUID_OBJ)
  307. /*
  308. * The volatile is used to to ensure that the compiler generates code that reads
  309. * all values from the array and doesn't try to optimize this away. The standard
  310. * doesn't actually require this behavior if the original data pointed to is
  311. * not volatile, but compilers do this in practice anyway.
  312. *
  313. * There are also assembler versions of this function.
  314. */
  315. # undef CRYPTO_memcmp
  316. int CRYPTO_memcmp(const void * in_a, const void * in_b, size_t len)
  317. {
  318. size_t i;
  319. const volatile unsigned char *a = in_a;
  320. const volatile unsigned char *b = in_b;
  321. unsigned char x = 0;
  322. for (i = 0; i < len; i++)
  323. x |= a[i] ^ b[i];
  324. return x;
  325. }
  326. /*
  327. * For systems that don't provide an instruction counter register or equivalent.
  328. */
  329. uint32_t OPENSSL_rdtsc(void)
  330. {
  331. return 0;
  332. }
  333. #endif