source: EcnlProtoTool/trunk/openssl-1.1.0e/crypto/cryptlib.c@ 331

Last change on this file since 331 was 331, checked in by coas-nagasima, 6 years ago

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 10.0 KB
Line 
1/*
2 * Copyright 1998-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
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.
14 */
15
16#include "internal/cryptlib_int.h"
17#include <openssl/safestack.h>
18
19#if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
20 defined(__x86_64) || defined(__x86_64__) || \
21 defined(_M_AMD64) || defined(_M_X64)
22
23extern unsigned int OPENSSL_ia32cap_P[4];
24
25# if defined(OPENSSL_CPUID_OBJ) && !defined(OPENSSL_NO_ASM) && !defined(I386_ONLY)
26#include <stdio.h>
27# define OPENSSL_CPUID_SETUP
28typedef uint64_t IA32CAP;
29void OPENSSL_cpuid_setup(void)
30{
31 static int trigger = 0;
32 IA32CAP OPENSSL_ia32_cpuid(unsigned int *);
33 IA32CAP vec;
34 char *env;
35
36 if (trigger)
37 return;
38
39 trigger = 1;
40 if ((env = getenv("OPENSSL_ia32cap"))) {
41 int off = (env[0] == '~') ? 1 : 0;
42# if defined(_WIN32)
43 if (!sscanf(env + off, "%I64i", &vec))
44 vec = strtoul(env + off, NULL, 0);
45# else
46 if (!sscanf(env + off, "%lli", (long long *)&vec))
47 vec = strtoul(env + off, NULL, 0);
48# endif
49 if (off)
50 vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P) & ~vec;
51 else if (env[0] == ':')
52 vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P);
53
54 OPENSSL_ia32cap_P[2] = 0;
55 if ((env = strchr(env, ':'))) {
56 unsigned int vecx;
57 env++;
58 off = (env[0] == '~') ? 1 : 0;
59 vecx = strtoul(env + off, NULL, 0);
60 if (off)
61 OPENSSL_ia32cap_P[2] &= ~vecx;
62 else
63 OPENSSL_ia32cap_P[2] = vecx;
64 }
65 } else
66 vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P);
67
68 /*
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.
72 */
73 OPENSSL_ia32cap_P[0] = (unsigned int)vec | (1 << 10);
74 OPENSSL_ia32cap_P[1] = (unsigned int)(vec >> 32);
75}
76# else
77unsigned int OPENSSL_ia32cap_P[4];
78# endif
79#endif
80int OPENSSL_NONPIC_relocated = 0;
81#if !defined(OPENSSL_CPUID_SETUP) && !defined(OPENSSL_CPUID_OBJ)
82void OPENSSL_cpuid_setup(void)
83{
84}
85#endif
86
87#if defined(_WIN32) && !defined(__CYGWIN__)
88# include <tchar.h>
89# include <signal.h>
90# ifdef __WATCOMC__
91# if defined(_UNICODE) || defined(__UNICODE__)
92# define _vsntprintf _vsnwprintf
93# else
94# define _vsntprintf _vsnprintf
95# endif
96# endif
97# ifdef _MSC_VER
98# define alloca _alloca
99# endif
100
101# if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
102int OPENSSL_isservice(void)
103{
104 HWINSTA h;
105 DWORD len;
106 WCHAR *name;
107 static union {
108 void *p;
109 FARPROC f;
110 } _OPENSSL_isservice = {
111 NULL
112 };
113
114 if (_OPENSSL_isservice.p == NULL) {
115 HANDLE mod = GetModuleHandle(NULL);
116 if (mod != NULL)
117 _OPENSSL_isservice.f = GetProcAddress(mod, "_OPENSSL_isservice");
118 if (_OPENSSL_isservice.p == NULL)
119 _OPENSSL_isservice.p = (void *)-1;
120 }
121
122 if (_OPENSSL_isservice.p != (void *)-1)
123 return (*_OPENSSL_isservice.f) ();
124
125 h = GetProcessWindowStation();
126 if (h == NULL)
127 return -1;
128
129 if (GetUserObjectInformationW(h, UOI_NAME, NULL, 0, &len) ||
130 GetLastError() != ERROR_INSUFFICIENT_BUFFER)
131 return -1;
132
133 if (len > 512)
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))
138 return -1;
139
140 len++, len &= ~1; /* paranoia */
141 name[len / sizeof(WCHAR)] = L'\0'; /* paranoia */
142# if 1
143 /*
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].
147 */
148 if (wcsstr(name, L"Service-0x"))
149 return 1;
150# else
151 /* This covers all non-interactive programs such as services. */
152 if (!wcsstr(name, L"WinSta0"))
153 return 1;
154# endif
155 else
156 return 0;
157}
158# else
159int OPENSSL_isservice(void)
160{
161 return 0;
162}
163# endif
164
165void OPENSSL_showfatal(const char *fmta, ...)
166{
167 va_list ap;
168 TCHAR buf[256];
169 const TCHAR *fmt;
170# ifdef STD_ERROR_HANDLE /* what a dirty trick! */
171 HANDLE h;
172
173 if ((h = GetStdHandle(STD_ERROR_HANDLE)) != NULL &&
174 GetFileType(h) != FILE_TYPE_UNKNOWN) {
175 /* must be console application */
176 int len;
177 DWORD out;
178
179 va_start(ap, fmta);
180 len = _vsnprintf((char *)buf, sizeof(buf), fmta, ap);
181 WriteFile(h, buf, len < 0 ? sizeof(buf) : (DWORD) len, &out, NULL);
182 va_end(ap);
183 return;
184 }
185# endif
186
187 if (sizeof(TCHAR) == sizeof(char))
188 fmt = (const TCHAR *)fmta;
189 else
190 do {
191 int keepgoing;
192 size_t len_0 = strlen(fmta) + 1, i;
193 WCHAR *fmtw;
194
195 fmtw = (WCHAR *)alloca(len_0 * sizeof(WCHAR));
196 if (fmtw == NULL) {
197 fmt = (const TCHAR *)L"no stack?";
198 break;
199 }
200 if (!MultiByteToWideChar(CP_ACP, 0, fmta, len_0, fmtw, len_0))
201 for (i = 0; i < len_0; i++)
202 fmtw[i] = (WCHAR)fmta[i];
203 for (i = 0; i < len_0; i++) {
204 if (fmtw[i] == L'%')
205 do {
206 keepgoing = 0;
207 switch (fmtw[i + 1]) {
208 case L'0':
209 case L'1':
210 case L'2':
211 case L'3':
212 case L'4':
213 case L'5':
214 case L'6':
215 case L'7':
216 case L'8':
217 case L'9':
218 case L'.':
219 case L'*':
220 case L'-':
221 i++;
222 keepgoing = 1;
223 break;
224 case L's':
225 fmtw[i + 1] = L'S';
226 break;
227 case L'S':
228 fmtw[i + 1] = L's';
229 break;
230 case L'c':
231 fmtw[i + 1] = L'C';
232 break;
233 case L'C':
234 fmtw[i + 1] = L'c';
235 break;
236 }
237 } while (keepgoing);
238 }
239 fmt = (const TCHAR *)fmtw;
240 } while (0);
241
242 va_start(ap, fmta);
243 _vsntprintf(buf, OSSL_NELEM(buf) - 1, fmt, ap);
244 buf[OSSL_NELEM(buf) - 1] = _T('\0');
245 va_end(ap);
246
247# if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
248 /* this -------------v--- guards NT-specific calls */
249 if (check_winnt() && OPENSSL_isservice() > 0) {
250 HANDLE hEventLog = RegisterEventSource(NULL, _T("OpenSSL"));
251
252 if (hEventLog != NULL) {
253 const TCHAR *pmsg = buf;
254
255 if (!ReportEvent(hEventLog, EVENTLOG_ERROR_TYPE, 0, 0, NULL,
256 1, 0, &pmsg, NULL)) {
257#if defined(DEBUG)
258 /*
259 * We are in a situation where we tried to report a critical
260 * error and this failed for some reason. As a last resort,
261 * in debug builds, send output to the debugger or any other
262 * tool like DebugView which can monitor the output.
263 */
264 OutputDebugString(pmsg);
265#endif
266 }
267
268 (void)DeregisterEventSource(hEventLog);
269 }
270 } else
271# endif
272 MessageBox(NULL, buf, _T("OpenSSL: FATAL"), MB_OK | MB_ICONERROR);
273}
274#else
275void OPENSSL_showfatal(const char *fmta, ...)
276{
277#ifndef OPENSSL_NO_STDIO
278 va_list ap;
279
280 va_start(ap, fmta);
281 vfprintf(stderr, fmta, ap);
282 va_end(ap);
283#endif
284}
285
286int OPENSSL_isservice(void)
287{
288 return 0;
289}
290#endif
291
292void OPENSSL_die(const char *message, const char *file, int line)
293{
294 OPENSSL_showfatal("%s:%d: OpenSSL internal error: %s\n",
295 file, line, message);
296#if !defined(_WIN32) || defined(__CYGWIN__)
297 abort();
298#else
299 /*
300 * Win32 abort() customarily shows a dialog, but we just did that...
301 */
302# if !defined(_WIN32_WCE)
303 raise(SIGABRT);
304# endif
305 _exit(3);
306#endif
307}
308
309#if !defined(OPENSSL_CPUID_OBJ)
310/* volatile unsigned char* pointers are there because
311 * 1. Accessing a variable declared volatile via a pointer
312 * that lacks a volatile qualifier causes undefined behavior.
313 * 2. When the variable itself is not volatile the compiler is
314 * not required to keep all those reads and can convert
315 * this into canonical memcmp() which doesn't read the whole block.
316 * Pointers to volatile resolve the first problem fully. The second
317 * problem cannot be resolved in any Standard-compliant way but this
318 * works the problem around. Compilers typically react to
319 * pointers to volatile by preserving the reads and writes through them.
320 * The latter is not required by the Standard if the memory pointed to
321 * is not volatile.
322 * Pointers themselves are volatile in the function signature to work
323 * around a subtle bug in gcc 4.6+ which causes writes through
324 * pointers to volatile to not be emitted in some rare,
325 * never needed in real life, pieces of code.
326 */
327int CRYPTO_memcmp(const volatile void * volatile in_a,
328 const volatile void * volatile in_b,
329 size_t len)
330{
331 size_t i;
332 const volatile unsigned char *a = in_a;
333 const volatile unsigned char *b = in_b;
334 unsigned char x = 0;
335
336 for (i = 0; i < len; i++)
337 x |= a[i] ^ b[i];
338
339 return x;
340}
341#endif
Note: See TracBrowser for help on using the repository browser.