cryptlib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. # ifdef OPENSSL_SYS_WIN_CORE
  114. int OPENSSL_isservice(void)
  115. {
  116. /* OneCore API cannot interact with GUI */
  117. return 1;
  118. }
  119. # else
  120. int OPENSSL_isservice(void)
  121. {
  122. HWINSTA h;
  123. DWORD len;
  124. WCHAR *name;
  125. static union {
  126. void *p;
  127. FARPROC f;
  128. } _OPENSSL_isservice = {
  129. NULL
  130. };
  131. if (_OPENSSL_isservice.p == NULL) {
  132. HANDLE mod = GetModuleHandle(NULL);
  133. if (mod != NULL)
  134. _OPENSSL_isservice.f = GetProcAddress(mod, "_OPENSSL_isservice");
  135. if (_OPENSSL_isservice.p == NULL)
  136. _OPENSSL_isservice.p = (void *)-1;
  137. }
  138. if (_OPENSSL_isservice.p != (void *)-1)
  139. return (*_OPENSSL_isservice.f) ();
  140. h = GetProcessWindowStation();
  141. if (h == NULL)
  142. return -1;
  143. if (GetUserObjectInformationW(h, UOI_NAME, NULL, 0, &len) ||
  144. GetLastError() != ERROR_INSUFFICIENT_BUFFER)
  145. return -1;
  146. if (len > 512)
  147. return -1; /* paranoia */
  148. len++, len &= ~1; /* paranoia */
  149. name = (WCHAR *)alloca(len + sizeof(WCHAR));
  150. if (!GetUserObjectInformationW(h, UOI_NAME, name, len, &len))
  151. return -1;
  152. len++, len &= ~1; /* paranoia */
  153. name[len / sizeof(WCHAR)] = L'\0'; /* paranoia */
  154. # if 1
  155. /*
  156. * This doesn't cover "interactive" services [working with real
  157. * WinSta0's] nor programs started non-interactively by Task Scheduler
  158. * [those are working with SAWinSta].
  159. */
  160. if (wcsstr(name, L"Service-0x"))
  161. return 1;
  162. # else
  163. /* This covers all non-interactive programs such as services. */
  164. if (!wcsstr(name, L"WinSta0"))
  165. return 1;
  166. # endif
  167. else
  168. return 0;
  169. }
  170. # endif
  171. # else
  172. int OPENSSL_isservice(void)
  173. {
  174. return 0;
  175. }
  176. # endif
  177. void OPENSSL_showfatal(const char *fmta, ...)
  178. {
  179. va_list ap;
  180. TCHAR buf[256];
  181. const TCHAR *fmt;
  182. /*
  183. * First check if it's a console application, in which case the
  184. * error message would be printed to standard error.
  185. * Windows CE does not have a concept of a console application,
  186. * so we need to guard the check.
  187. */
  188. # ifdef STD_ERROR_HANDLE
  189. HANDLE h;
  190. if ((h = GetStdHandle(STD_ERROR_HANDLE)) != NULL &&
  191. GetFileType(h) != FILE_TYPE_UNKNOWN) {
  192. /* must be console application */
  193. int len;
  194. DWORD out;
  195. va_start(ap, fmta);
  196. len = _vsnprintf((char *)buf, sizeof(buf), fmta, ap);
  197. WriteFile(h, buf, len < 0 ? sizeof(buf) : (DWORD) len, &out, NULL);
  198. va_end(ap);
  199. return;
  200. }
  201. # endif
  202. if (sizeof(TCHAR) == sizeof(char))
  203. fmt = (const TCHAR *)fmta;
  204. else
  205. do {
  206. int keepgoing;
  207. size_t len_0 = strlen(fmta) + 1, i;
  208. WCHAR *fmtw;
  209. fmtw = (WCHAR *)alloca(len_0 * sizeof(WCHAR));
  210. if (fmtw == NULL) {
  211. fmt = (const TCHAR *)L"no stack?";
  212. break;
  213. }
  214. if (!MultiByteToWideChar(CP_ACP, 0, fmta, len_0, fmtw, len_0))
  215. for (i = 0; i < len_0; i++)
  216. fmtw[i] = (WCHAR)fmta[i];
  217. for (i = 0; i < len_0; i++) {
  218. if (fmtw[i] == L'%')
  219. do {
  220. keepgoing = 0;
  221. switch (fmtw[i + 1]) {
  222. case L'0':
  223. case L'1':
  224. case L'2':
  225. case L'3':
  226. case L'4':
  227. case L'5':
  228. case L'6':
  229. case L'7':
  230. case L'8':
  231. case L'9':
  232. case L'.':
  233. case L'*':
  234. case L'-':
  235. i++;
  236. keepgoing = 1;
  237. break;
  238. case L's':
  239. fmtw[i + 1] = L'S';
  240. break;
  241. case L'S':
  242. fmtw[i + 1] = L's';
  243. break;
  244. case L'c':
  245. fmtw[i + 1] = L'C';
  246. break;
  247. case L'C':
  248. fmtw[i + 1] = L'c';
  249. break;
  250. }
  251. } while (keepgoing);
  252. }
  253. fmt = (const TCHAR *)fmtw;
  254. } while (0);
  255. va_start(ap, fmta);
  256. _vsntprintf(buf, OSSL_NELEM(buf) - 1, fmt, ap);
  257. buf[OSSL_NELEM(buf) - 1] = _T('\0');
  258. va_end(ap);
  259. # if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
  260. # ifdef OPENSSL_SYS_WIN_CORE
  261. /* ONECORE is always NONGUI and NT >= 0x0601 */
  262. /*
  263. * TODO: (For non GUI and no std error cases)
  264. * Add event logging feature here.
  265. */
  266. # if !defined(NDEBUG)
  267. /*
  268. * We are in a situation where we tried to report a critical
  269. * error and this failed for some reason. As a last resort,
  270. * in debug builds, send output to the debugger or any other
  271. * tool like DebugView which can monitor the output.
  272. */
  273. OutputDebugString(buf);
  274. # endif
  275. # else
  276. /* this -------------v--- guards NT-specific calls */
  277. if (check_winnt() && OPENSSL_isservice() > 0) {
  278. HANDLE hEventLog = RegisterEventSource(NULL, _T("OpenSSL"));
  279. if (hEventLog != NULL) {
  280. const TCHAR *pmsg = buf;
  281. if (!ReportEvent(hEventLog, EVENTLOG_ERROR_TYPE, 0, 0, NULL,
  282. 1, 0, &pmsg, NULL)) {
  283. # if !defined(NDEBUG)
  284. /*
  285. * We are in a situation where we tried to report a critical
  286. * error and this failed for some reason. As a last resort,
  287. * in debug builds, send output to the debugger or any other
  288. * tool like DebugView which can monitor the output.
  289. */
  290. OutputDebugString(pmsg);
  291. # endif
  292. }
  293. (void)DeregisterEventSource(hEventLog);
  294. }
  295. } else {
  296. MessageBox(NULL, buf, _T("OpenSSL: FATAL"), MB_OK | MB_ICONERROR);
  297. }
  298. # endif
  299. # else
  300. MessageBox(NULL, buf, _T("OpenSSL: FATAL"), MB_OK | MB_ICONERROR);
  301. # endif
  302. }
  303. #else
  304. void OPENSSL_showfatal(const char *fmta, ...)
  305. {
  306. #ifndef OPENSSL_NO_STDIO
  307. va_list ap;
  308. va_start(ap, fmta);
  309. vfprintf(stderr, fmta, ap);
  310. va_end(ap);
  311. #endif
  312. }
  313. int OPENSSL_isservice(void)
  314. {
  315. return 0;
  316. }
  317. #endif
  318. void OPENSSL_die(const char *message, const char *file, int line)
  319. {
  320. OPENSSL_showfatal("%s:%d: OpenSSL internal error: %s\n",
  321. file, line, message);
  322. #if !defined(_WIN32)
  323. abort();
  324. #else
  325. /*
  326. * Win32 abort() customarily shows a dialog, but we just did that...
  327. */
  328. # if !defined(_WIN32_WCE)
  329. raise(SIGABRT);
  330. # endif
  331. _exit(3);
  332. #endif
  333. }
  334. #if !defined(OPENSSL_CPUID_OBJ)
  335. /*
  336. * The volatile is used to to ensure that the compiler generates code that reads
  337. * all values from the array and doesn't try to optimize this away. The standard
  338. * doesn't actually require this behavior if the original data pointed to is
  339. * not volatile, but compilers do this in practice anyway.
  340. *
  341. * There are also assembler versions of this function.
  342. */
  343. # undef CRYPTO_memcmp
  344. int CRYPTO_memcmp(const void * in_a, const void * in_b, size_t len)
  345. {
  346. size_t i;
  347. const volatile unsigned char *a = in_a;
  348. const volatile unsigned char *b = in_b;
  349. unsigned char x = 0;
  350. for (i = 0; i < len; i++)
  351. x |= a[i] ^ b[i];
  352. return x;
  353. }
  354. /*
  355. * For systems that don't provide an instruction counter register or equivalent.
  356. */
  357. uint32_t OPENSSL_rdtsc(void)
  358. {
  359. return 0;
  360. }
  361. #endif