Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 1 | /* crypto/cryptlib.c */ |
Bodo Möller | 19b8d06 | 2002-10-28 14:02:19 +0000 | [diff] [blame] | 2 | /* ==================================================================== |
Bodo Möller | 48fc582 | 2006-06-23 15:21:36 +0000 | [diff] [blame] | 3 | * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. |
Bodo Möller | 19b8d06 | 2002-10-28 14:02:19 +0000 | [diff] [blame] | 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * |
| 9 | * 1. Redistributions of source code must retain the above copyright |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 10 | * notice, this list of conditions and the following disclaimer. |
Bodo Möller | 19b8d06 | 2002-10-28 14:02:19 +0000 | [diff] [blame] | 11 | * |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in |
| 14 | * the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * |
| 17 | * 3. All advertising materials mentioning features or use of this |
| 18 | * software must display the following acknowledgment: |
| 19 | * "This product includes software developed by the OpenSSL Project |
| 20 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 21 | * |
| 22 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 23 | * endorse or promote products derived from this software without |
| 24 | * prior written permission. For written permission, please contact |
| 25 | * openssl-core@openssl.org. |
| 26 | * |
| 27 | * 5. Products derived from this software may not be called "OpenSSL" |
| 28 | * nor may "OpenSSL" appear in their names without prior written |
| 29 | * permission of the OpenSSL Project. |
| 30 | * |
| 31 | * 6. Redistributions of any form whatsoever must retain the following |
| 32 | * acknowledgment: |
| 33 | * "This product includes software developed by the OpenSSL Project |
| 34 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 35 | * |
| 36 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 37 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 38 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 39 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 40 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 41 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 42 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 43 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 44 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 45 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 46 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 47 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 48 | * ==================================================================== |
| 49 | * |
| 50 | * This product includes cryptographic software written by Eric Young |
| 51 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 52 | * Hudson (tjh@cryptsoft.com). |
| 53 | * |
| 54 | */ |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 55 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 56 | * All rights reserved. |
| 57 | * |
| 58 | * This package is an SSL implementation written |
| 59 | * by Eric Young (eay@cryptsoft.com). |
| 60 | * The implementation was written so as to conform with Netscapes SSL. |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 61 | * |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 62 | * This library is free for commercial and non-commercial use as long as |
| 63 | * the following conditions are aheared to. The following conditions |
| 64 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 65 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 66 | * included with this distribution is covered by the same copyright terms |
| 67 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 68 | * |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 69 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 70 | * the code are not to be removed. |
| 71 | * If this package is used in a product, Eric Young should be given attribution |
| 72 | * as the author of the parts of the library used. |
| 73 | * This can be in the form of a textual message at program startup or |
| 74 | * in documentation (online or textual) provided with the package. |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 75 | * |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 76 | * Redistribution and use in source and binary forms, with or without |
| 77 | * modification, are permitted provided that the following conditions |
| 78 | * are met: |
| 79 | * 1. Redistributions of source code must retain the copyright |
| 80 | * notice, this list of conditions and the following disclaimer. |
| 81 | * 2. Redistributions in binary form must reproduce the above copyright |
| 82 | * notice, this list of conditions and the following disclaimer in the |
| 83 | * documentation and/or other materials provided with the distribution. |
| 84 | * 3. All advertising materials mentioning features or use of this software |
| 85 | * must display the following acknowledgement: |
| 86 | * "This product includes cryptographic software written by |
| 87 | * Eric Young (eay@cryptsoft.com)" |
| 88 | * The word 'cryptographic' can be left out if the rouines from the library |
| 89 | * being used are not cryptographic related :-). |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 90 | * 4. If you include any Windows specific code (or a derivative thereof) from |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 91 | * the apps directory (application code) you must include an acknowledgement: |
| 92 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 93 | * |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 94 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 95 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 96 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 97 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 98 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 99 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 100 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 101 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 102 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 103 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 104 | * SUCH DAMAGE. |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 105 | * |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 106 | * The licence and distribution terms for any publically available version or |
| 107 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 108 | * copied and put under another distribution licence |
| 109 | * [including the GNU Public Licence.] |
| 110 | */ |
Bodo Möller | e172d60 | 2002-08-09 08:43:04 +0000 | [diff] [blame] | 111 | /* ==================================================================== |
| 112 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 113 | * ECDH support in OpenSSL originally developed by |
Bodo Möller | e172d60 | 2002-08-09 08:43:04 +0000 | [diff] [blame] | 114 | * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. |
| 115 | */ |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 116 | |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 117 | #include "cryptlib.h" |
Richard Levitte | c792230 | 2000-06-18 15:59:04 +0000 | [diff] [blame] | 118 | #include <openssl/safestack.h> |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 119 | |
Rich Salz | 6d23cf9 | 2015-01-12 17:29:26 -0500 | [diff] [blame] | 120 | #if defined(OPENSSL_SYS_WIN32) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 121 | static double SSLeay_MSVC5_hack = 0.0; /* and for VC1.5 */ |
Ralf S. Engelschall | 58964a4 | 1998-12-21 10:56:39 +0000 | [diff] [blame] | 122 | #endif |
| 123 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 124 | #if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \ |
| 125 | defined(__INTEL__) || \ |
| 126 | defined(__x86_64) || defined(__x86_64__) || \ |
| 127 | defined(_M_AMD64) || defined(_M_X64) |
Andy Polyakov | 14e21f8 | 2004-07-26 20:18:55 +0000 | [diff] [blame] | 128 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 129 | extern unsigned int OPENSSL_ia32cap_P[4]; |
| 130 | unsigned int *OPENSSL_ia32cap_loc(void) |
| 131 | { |
| 132 | return OPENSSL_ia32cap_P; |
| 133 | } |
Andy Polyakov | 14e21f8 | 2004-07-26 20:18:55 +0000 | [diff] [blame] | 134 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 135 | # if defined(OPENSSL_CPUID_OBJ) && !defined(OPENSSL_NO_ASM) && !defined(I386_ONLY) |
| 136 | # define OPENSSL_CPUID_SETUP |
| 137 | # if defined(_WIN32) |
Andy Polyakov | e303f55 | 2009-04-26 17:49:41 +0000 | [diff] [blame] | 138 | typedef unsigned __int64 IA32CAP; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 139 | # else |
Andy Polyakov | e303f55 | 2009-04-26 17:49:41 +0000 | [diff] [blame] | 140 | typedef unsigned long long IA32CAP; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 141 | # endif |
Andy Polyakov | 2b247cf | 2004-08-29 16:36:05 +0000 | [diff] [blame] | 142 | void OPENSSL_cpuid_setup(void) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 143 | { |
| 144 | static int trigger = 0; |
| 145 | IA32CAP OPENSSL_ia32_cpuid(unsigned int *); |
| 146 | IA32CAP vec; |
| 147 | char *env; |
Andy Polyakov | 14e21f8 | 2004-07-26 20:18:55 +0000 | [diff] [blame] | 148 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 149 | if (trigger) |
| 150 | return; |
Andy Polyakov | 14e21f8 | 2004-07-26 20:18:55 +0000 | [diff] [blame] | 151 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 152 | trigger = 1; |
| 153 | if ((env = getenv("OPENSSL_ia32cap"))) { |
| 154 | int off = (env[0] == '~') ? 1 : 0; |
| 155 | # if defined(_WIN32) |
| 156 | if (!sscanf(env + off, "%I64i", &vec)) |
| 157 | vec = strtoul(env + off, NULL, 0); |
| 158 | # else |
| 159 | if (!sscanf(env + off, "%lli", (long long *)&vec)) |
| 160 | vec = strtoul(env + off, NULL, 0); |
| 161 | # endif |
| 162 | if (off) |
| 163 | vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P) & ~vec; |
| 164 | else if (env[0] == ':') |
| 165 | vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P); |
Andy Polyakov | c5cd28b | 2012-11-17 19:04:15 +0000 | [diff] [blame] | 166 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 167 | OPENSSL_ia32cap_P[2] = 0; |
| 168 | if ((env = strchr(env, ':'))) { |
| 169 | unsigned int vecx; |
| 170 | env++; |
| 171 | off = (env[0] == '~') ? 1 : 0; |
| 172 | vecx = strtoul(env + off, NULL, 0); |
| 173 | if (off) |
| 174 | OPENSSL_ia32cap_P[2] &= ~vecx; |
| 175 | else |
| 176 | OPENSSL_ia32cap_P[2] = vecx; |
| 177 | } |
| 178 | } else |
| 179 | vec = OPENSSL_ia32_cpuid(OPENSSL_ia32cap_P); |
Andy Polyakov | e303f55 | 2009-04-26 17:49:41 +0000 | [diff] [blame] | 180 | |
Andy Polyakov | 14e21f8 | 2004-07-26 20:18:55 +0000 | [diff] [blame] | 181 | /* |
| 182 | * |(1<<10) sets a reserved bit to signal that variable |
| 183 | * was initialized already... This is to avoid interference |
| 184 | * with cpuid snippets in ELF .init segment. |
| 185 | */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 186 | OPENSSL_ia32cap_P[0] = (unsigned int)vec | (1 << 10); |
| 187 | OPENSSL_ia32cap_P[1] = (unsigned int)(vec >> 32); |
Andy Polyakov | 14e21f8 | 2004-07-26 20:18:55 +0000 | [diff] [blame] | 188 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 189 | # else |
Andy Polyakov | c5cd28b | 2012-11-17 19:04:15 +0000 | [diff] [blame] | 190 | unsigned int OPENSSL_ia32cap_P[4]; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 191 | # endif |
Andy Polyakov | 14e21f8 | 2004-07-26 20:18:55 +0000 | [diff] [blame] | 192 | |
Richard Levitte | 630e4a6 | 2005-04-21 09:10:19 +0000 | [diff] [blame] | 193 | #else |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 194 | unsigned int *OPENSSL_ia32cap_loc(void) |
| 195 | { |
| 196 | return NULL; |
| 197 | } |
Andy Polyakov | 14e21f8 | 2004-07-26 20:18:55 +0000 | [diff] [blame] | 198 | #endif |
Andy Polyakov | 1875e6d | 2005-07-05 11:44:45 +0000 | [diff] [blame] | 199 | int OPENSSL_NONPIC_relocated = 0; |
Andy Polyakov | a00e414 | 2005-12-16 17:39:57 +0000 | [diff] [blame] | 200 | #if !defined(OPENSSL_CPUID_SETUP) && !defined(OPENSSL_CPUID_OBJ) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 201 | void OPENSSL_cpuid_setup(void) |
| 202 | { |
| 203 | } |
Andy Polyakov | 14e21f8 | 2004-07-26 20:18:55 +0000 | [diff] [blame] | 204 | #endif |
| 205 | |
Andy Polyakov | e1d51de | 2005-04-07 15:51:55 +0000 | [diff] [blame] | 206 | #if (defined(_WIN32) || defined(__CYGWIN__)) && defined(_WINDLL) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 207 | # ifdef __CYGWIN__ |
Andy Polyakov | c770944 | 2004-08-01 21:24:34 +0000 | [diff] [blame] | 208 | /* pick DLL_[PROCESS|THREAD]_[ATTACH|DETACH] definitions */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 209 | # include <windows.h> |
| 210 | /* |
| 211 | * this has side-effect of _WIN32 getting defined, which otherwise is |
| 212 | * mutually exclusive with __CYGWIN__... |
| 213 | */ |
| 214 | # endif |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 215 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 216 | /* |
| 217 | * All we really need to do is remove the 'error' state when a thread |
| 218 | * detaches |
| 219 | */ |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 220 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 221 | BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) |
| 222 | { |
| 223 | switch (fdwReason) { |
| 224 | case DLL_PROCESS_ATTACH: |
| 225 | OPENSSL_cpuid_setup(); |
| 226 | # if defined(_WIN32_WINNT) |
| 227 | { |
| 228 | IMAGE_DOS_HEADER *dos_header = (IMAGE_DOS_HEADER *) hinstDLL; |
| 229 | IMAGE_NT_HEADERS *nt_headers; |
Andy Polyakov | 51d2801 | 2005-04-13 08:46:35 +0000 | [diff] [blame] | 230 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 231 | if (dos_header->e_magic == IMAGE_DOS_SIGNATURE) { |
| 232 | nt_headers = (IMAGE_NT_HEADERS *) ((char *)dos_header |
| 233 | + dos_header->e_lfanew); |
| 234 | if (nt_headers->Signature == IMAGE_NT_SIGNATURE && |
| 235 | hinstDLL != |
| 236 | (HINSTANCE) (nt_headers->OptionalHeader.ImageBase)) |
| 237 | OPENSSL_NONPIC_relocated = 1; |
| 238 | } |
| 239 | } |
| 240 | # endif |
| 241 | break; |
| 242 | case DLL_THREAD_ATTACH: |
| 243 | break; |
| 244 | case DLL_THREAD_DETACH: |
| 245 | break; |
| 246 | case DLL_PROCESS_DETACH: |
| 247 | break; |
| 248 | } |
| 249 | return (TRUE); |
| 250 | } |
Ralf S. Engelschall | d02b48c | 1998-12-21 10:52:47 +0000 | [diff] [blame] | 251 | #endif |
| 252 | |
Andy Polyakov | 34c7ff6 | 2005-05-03 21:20:17 +0000 | [diff] [blame] | 253 | #if defined(_WIN32) && !defined(__CYGWIN__) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 254 | # include <tchar.h> |
| 255 | # include <signal.h> |
| 256 | # ifdef __WATCOMC__ |
| 257 | # if defined(_UNICODE) || defined(__UNICODE__) |
| 258 | # define _vsntprintf _vsnwprintf |
| 259 | # else |
| 260 | # define _vsntprintf _vsnprintf |
| 261 | # endif |
| 262 | # endif |
| 263 | # ifdef _MSC_VER |
| 264 | # define alloca _alloca |
| 265 | # endif |
Andy Polyakov | 9f2027e | 2005-04-07 18:39:45 +0000 | [diff] [blame] | 266 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 267 | # if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333 |
Andy Polyakov | e476f94 | 2005-05-21 13:19:27 +0000 | [diff] [blame] | 268 | int OPENSSL_isservice(void) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 269 | { |
| 270 | HWINSTA h; |
| 271 | DWORD len; |
| 272 | WCHAR *name; |
| 273 | static union { |
| 274 | void *p; |
| 275 | int (*f) (void); |
| 276 | } _OPENSSL_isservice = { |
| 277 | NULL |
| 278 | }; |
Andy Polyakov | 471d0eb | 2010-03-29 10:06:01 +0000 | [diff] [blame] | 279 | |
| 280 | if (_OPENSSL_isservice.p == NULL) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 281 | HANDLE h = GetModuleHandle(NULL); |
| 282 | if (h != NULL) |
| 283 | _OPENSSL_isservice.p = GetProcAddress(h, "_OPENSSL_isservice"); |
| 284 | if (_OPENSSL_isservice.p == NULL) |
| 285 | _OPENSSL_isservice.p = (void *)-1; |
Andy Polyakov | 471d0eb | 2010-03-29 10:06:01 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | if (_OPENSSL_isservice.p != (void *)-1) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 289 | return (*_OPENSSL_isservice.f) (); |
Andy Polyakov | 9f2027e | 2005-04-07 18:39:45 +0000 | [diff] [blame] | 290 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 291 | (void)GetDesktopWindow(); /* return value is ignored */ |
Andy Polyakov | 9f2027e | 2005-04-07 18:39:45 +0000 | [diff] [blame] | 292 | |
| 293 | h = GetProcessWindowStation(); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 294 | if (h == NULL) |
| 295 | return -1; |
Andy Polyakov | 9f2027e | 2005-04-07 18:39:45 +0000 | [diff] [blame] | 296 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 297 | if (GetUserObjectInformationW(h, UOI_NAME, NULL, 0, &len) || |
| 298 | GetLastError() != ERROR_INSUFFICIENT_BUFFER) |
| 299 | return -1; |
Andy Polyakov | 9f2027e | 2005-04-07 18:39:45 +0000 | [diff] [blame] | 300 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 301 | if (len > 512) |
| 302 | return -1; /* paranoia */ |
| 303 | len++, len &= ~1; /* paranoia */ |
| 304 | name = (WCHAR *)alloca(len + sizeof(WCHAR)); |
| 305 | if (!GetUserObjectInformationW(h, UOI_NAME, name, len, &len)) |
| 306 | return -1; |
Andy Polyakov | 9f2027e | 2005-04-07 18:39:45 +0000 | [diff] [blame] | 307 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 308 | len++, len &= ~1; /* paranoia */ |
| 309 | name[len / sizeof(WCHAR)] = L'\0'; /* paranoia */ |
| 310 | # if 1 |
| 311 | /* |
| 312 | * This doesn't cover "interactive" services [working with real |
| 313 | * WinSta0's] nor programs started non-interactively by Task Scheduler |
| 314 | * [those are working with SAWinSta]. |
| 315 | */ |
| 316 | if (wcsstr(name, L"Service-0x")) |
| 317 | return 1; |
| 318 | # else |
Andy Polyakov | 9f2027e | 2005-04-07 18:39:45 +0000 | [diff] [blame] | 319 | /* This covers all non-interactive programs such as services. */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 320 | if (!wcsstr(name, L"WinSta0")) |
| 321 | return 1; |
| 322 | # endif |
Andy Polyakov | 9f2027e | 2005-04-07 18:39:45 +0000 | [diff] [blame] | 323 | else |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 324 | return 0; |
| 325 | } |
| 326 | # else |
| 327 | int OPENSSL_isservice(void) |
| 328 | { |
| 329 | return 0; |
| 330 | } |
| 331 | # endif |
| 332 | |
| 333 | void OPENSSL_showfatal(const char *fmta, ...) |
| 334 | { |
| 335 | va_list ap; |
| 336 | TCHAR buf[256]; |
| 337 | const TCHAR *fmt; |
| 338 | # ifdef STD_ERROR_HANDLE /* what a dirty trick! */ |
| 339 | HANDLE h; |
| 340 | |
| 341 | if ((h = GetStdHandle(STD_ERROR_HANDLE)) != NULL && |
| 342 | GetFileType(h) != FILE_TYPE_UNKNOWN) { |
| 343 | /* must be console application */ |
| 344 | int len; |
| 345 | DWORD out; |
| 346 | |
| 347 | va_start(ap, fmta); |
| 348 | len = _vsnprintf((char *)buf, sizeof(buf), fmta, ap); |
| 349 | WriteFile(h, buf, len < 0 ? sizeof(buf) : (DWORD) len, &out, NULL); |
| 350 | va_end(ap); |
| 351 | return; |
| 352 | } |
| 353 | # endif |
| 354 | |
| 355 | if (sizeof(TCHAR) == sizeof(char)) |
| 356 | fmt = (const TCHAR *)fmta; |
| 357 | else |
| 358 | do { |
| 359 | int keepgoing; |
| 360 | size_t len_0 = strlen(fmta) + 1, i; |
| 361 | WCHAR *fmtw; |
| 362 | |
| 363 | fmtw = (WCHAR *)alloca(len_0 * sizeof(WCHAR)); |
| 364 | if (fmtw == NULL) { |
| 365 | fmt = (const TCHAR *)L"no stack?"; |
| 366 | break; |
| 367 | } |
| 368 | # ifndef OPENSSL_NO_MULTIBYTE |
| 369 | if (!MultiByteToWideChar(CP_ACP, 0, fmta, len_0, fmtw, len_0)) |
| 370 | # endif |
| 371 | for (i = 0; i < len_0; i++) |
| 372 | fmtw[i] = (WCHAR)fmta[i]; |
| 373 | |
| 374 | for (i = 0; i < len_0; i++) { |
| 375 | if (fmtw[i] == L'%') |
| 376 | do { |
| 377 | keepgoing = 0; |
| 378 | switch (fmtw[i + 1]) { |
| 379 | case L'0': |
| 380 | case L'1': |
| 381 | case L'2': |
| 382 | case L'3': |
| 383 | case L'4': |
| 384 | case L'5': |
| 385 | case L'6': |
| 386 | case L'7': |
| 387 | case L'8': |
| 388 | case L'9': |
| 389 | case L'.': |
| 390 | case L'*': |
| 391 | case L'-': |
| 392 | i++; |
| 393 | keepgoing = 1; |
| 394 | break; |
| 395 | case L's': |
| 396 | fmtw[i + 1] = L'S'; |
| 397 | break; |
| 398 | case L'S': |
| 399 | fmtw[i + 1] = L's'; |
| 400 | break; |
| 401 | case L'c': |
| 402 | fmtw[i + 1] = L'C'; |
| 403 | break; |
| 404 | case L'C': |
| 405 | fmtw[i + 1] = L'c'; |
| 406 | break; |
| 407 | } |
| 408 | } while (keepgoing); |
| 409 | } |
| 410 | fmt = (const TCHAR *)fmtw; |
| 411 | } while (0); |
| 412 | |
| 413 | va_start(ap, fmta); |
| 414 | _vsntprintf(buf, sizeof(buf) / sizeof(TCHAR) - 1, fmt, ap); |
| 415 | buf[sizeof(buf) / sizeof(TCHAR) - 1] = _T('\0'); |
| 416 | va_end(ap); |
| 417 | |
| 418 | # if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333 |
| 419 | /* this -------------v--- guards NT-specific calls */ |
| 420 | if (check_winnt() && OPENSSL_isservice() > 0) { |
| 421 | HANDLE h = RegisterEventSource(0, _T("OPENSSL")); |
| 422 | const TCHAR *pmsg = buf; |
| 423 | ReportEvent(h, EVENTLOG_ERROR_TYPE, 0, 0, 0, 1, 0, &pmsg, 0); |
| 424 | DeregisterEventSource(h); |
| 425 | } else |
| 426 | # endif |
| 427 | MessageBox(NULL, buf, _T("OpenSSL: FATAL"), MB_OK | MB_ICONSTOP); |
Andy Polyakov | 9f2027e | 2005-04-07 18:39:45 +0000 | [diff] [blame] | 428 | } |
| 429 | #else |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 430 | void OPENSSL_showfatal(const char *fmta, ...) |
| 431 | { |
| 432 | va_list ap; |
Andy Polyakov | 9f2027e | 2005-04-07 18:39:45 +0000 | [diff] [blame] | 433 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 434 | va_start(ap, fmta); |
| 435 | vfprintf(stderr, fmta, ap); |
| 436 | va_end(ap); |
Andy Polyakov | 9f2027e | 2005-04-07 18:39:45 +0000 | [diff] [blame] | 437 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 438 | |
| 439 | int OPENSSL_isservice(void) |
| 440 | { |
| 441 | return 0; |
| 442 | } |
Andy Polyakov | 9f2027e | 2005-04-07 18:39:45 +0000 | [diff] [blame] | 443 | #endif |
| 444 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 445 | void OpenSSLDie(const char *file, int line, const char *assertion) |
| 446 | { |
| 447 | OPENSSL_showfatal |
| 448 | ("%s(%d): OpenSSL internal error, assertion failed: %s\n", file, line, |
| 449 | assertion); |
Andy Polyakov | 6022fe8 | 2009-05-04 06:23:05 +0000 | [diff] [blame] | 450 | #if !defined(_WIN32) || defined(__CYGWIN__) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 451 | abort(); |
Andy Polyakov | 3b58c74 | 2009-05-03 14:16:40 +0000 | [diff] [blame] | 452 | #else |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 453 | /* |
| 454 | * Win32 abort() customarily shows a dialog, but we just did that... |
| 455 | */ |
| 456 | # if !defined(_WIN32_WCE) |
| 457 | raise(SIGABRT); |
| 458 | # endif |
| 459 | _exit(3); |
Andy Polyakov | a006fef | 2013-01-19 21:23:13 +0100 | [diff] [blame] | 460 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 461 | } |
Andy Polyakov | 9e88c82 | 2005-04-13 06:55:42 +0000 | [diff] [blame] | 462 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 463 | void *OPENSSL_stderr(void) |
| 464 | { |
| 465 | return stderr; |
| 466 | } |
Ben Laurie | 7c770d5 | 2013-01-28 17:30:38 +0000 | [diff] [blame] | 467 | |
| 468 | int CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 469 | { |
| 470 | size_t i; |
| 471 | const unsigned char *a = in_a; |
| 472 | const unsigned char *b = in_b; |
| 473 | unsigned char x = 0; |
Ben Laurie | 7c770d5 | 2013-01-28 17:30:38 +0000 | [diff] [blame] | 474 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 475 | for (i = 0; i < len; i++) |
| 476 | x |= a[i] ^ b[i]; |
Ben Laurie | 7c770d5 | 2013-01-28 17:30:38 +0000 | [diff] [blame] | 477 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 478 | return x; |
| 479 | } |