1 /* crypto/cryptlib.c */
2 /* ====================================================================
3 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
55 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
56 * All rights reserved.
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.
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).
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.
76 * Redistribution and use in source and binary forms, with or without
77 * modification, are permitted provided that the following conditions
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 :-).
90 * 4. If you include any Windows specific code (or a derivative thereof) from
91 * the apps directory (application code) you must include an acknowledgement:
92 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
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
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.]
111 /* ====================================================================
112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
113 * ECDH support in OpenSSL originally developed by
114 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
117 #include "cryptlib.h"
118 #include <openssl/safestack.h>
120 #if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16)
121 static double SSLeay_MSVC5_hack = 0.0; /* and for VC1.5 */
124 DECLARE_STACK_OF(CRYPTO_dynlock)
126 /* real #defines in crypto.h, keep these upto date */
127 static const char *const lock_names[CRYPTO_NUM_LOCKS] = {
169 #if CRYPTO_NUM_LOCKS != 41
170 # error "Inconsistency between crypto.h and cryptlib.c"
175 * This is for applications to allocate new type names in the non-dynamic
176 * array of lock names. These are numbered with positive numbers.
178 static STACK_OF(OPENSSL_STRING) *app_locks = NULL;
181 * For applications that want a more dynamic way of handling threads, the
182 * following stack is used. These are externally numbered with negative
185 static STACK_OF(CRYPTO_dynlock) *dyn_locks = NULL;
187 static void (MS_FAR *locking_callback) (int mode, int type,
188 const char *file, int line) = 0;
189 static int (MS_FAR *add_lock_callback) (int *pointer, int amount,
190 int type, const char *file,
192 #ifndef OPENSSL_NO_DEPRECATED
193 static unsigned long (MS_FAR *id_callback) (void) = 0;
195 static void (MS_FAR *threadid_callback) (CRYPTO_THREADID *) = 0;
196 static struct CRYPTO_dynlock_value *(MS_FAR *dynlock_create_callback)
197 (const char *file, int line) = 0;
198 static void (MS_FAR *dynlock_lock_callback) (int mode,
199 struct CRYPTO_dynlock_value *l,
200 const char *file, int line) = 0;
201 static void (MS_FAR *dynlock_destroy_callback) (struct CRYPTO_dynlock_value
202 *l, const char *file,
205 int CRYPTO_get_new_lockid(char *name)
210 #if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16)
212 * A hack to make Visual C++ 5.0 work correctly when linking as a DLL
213 * using /MT. Without this, the application cannot use any floating point
214 * printf's. It also seems to be needed for Visual C 1.5 (win16)
216 SSLeay_MSVC5_hack = (double)name[0] * (double)name[1];
219 if ((app_locks == NULL)
220 && ((app_locks = sk_OPENSSL_STRING_new_null()) == NULL)) {
221 CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_LOCKID, ERR_R_MALLOC_FAILURE);
224 if ((str = BUF_strdup(name)) == NULL) {
225 CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_LOCKID, ERR_R_MALLOC_FAILURE);
228 i = sk_OPENSSL_STRING_push(app_locks, str);
232 i += CRYPTO_NUM_LOCKS; /* gap of one :-) */
236 int CRYPTO_num_locks(void)
238 return CRYPTO_NUM_LOCKS;
241 int CRYPTO_get_new_dynlockid(void)
244 CRYPTO_dynlock *pointer = NULL;
246 if (dynlock_create_callback == NULL) {
247 CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID,
248 CRYPTO_R_NO_DYNLOCK_CREATE_CALLBACK);
251 CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
252 if ((dyn_locks == NULL)
253 && ((dyn_locks = sk_CRYPTO_dynlock_new_null()) == NULL)) {
254 CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
255 CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID, ERR_R_MALLOC_FAILURE);
258 CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
260 pointer = (CRYPTO_dynlock *) OPENSSL_malloc(sizeof(CRYPTO_dynlock));
261 if (pointer == NULL) {
262 CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID, ERR_R_MALLOC_FAILURE);
265 pointer->references = 1;
266 pointer->data = dynlock_create_callback(__FILE__, __LINE__);
267 if (pointer->data == NULL) {
268 OPENSSL_free(pointer);
269 CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID, ERR_R_MALLOC_FAILURE);
273 CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
274 /* First, try to find an existing empty slot */
275 i = sk_CRYPTO_dynlock_find(dyn_locks, NULL);
276 /* If there was none, push, thereby creating a new one */
279 * Since sk_push() returns the number of items on the stack, not the
280 * location of the pushed item, we need to transform the returned
281 * number into a position, by decreasing it.
283 i = sk_CRYPTO_dynlock_push(dyn_locks, pointer) - 1;
286 * If we found a place with a NULL pointer, put our pointer in it.
288 (void)sk_CRYPTO_dynlock_set(dyn_locks, i, pointer);
289 CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
292 dynlock_destroy_callback(pointer->data, __FILE__, __LINE__);
293 OPENSSL_free(pointer);
295 i += 1; /* to avoid 0 */
299 void CRYPTO_destroy_dynlockid(int i)
301 CRYPTO_dynlock *pointer = NULL;
304 if (dynlock_destroy_callback == NULL)
307 CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
309 if (dyn_locks == NULL || i >= sk_CRYPTO_dynlock_num(dyn_locks)) {
310 CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
313 pointer = sk_CRYPTO_dynlock_value(dyn_locks, i);
314 if (pointer != NULL) {
315 --pointer->references;
317 if (pointer->references < 0) {
319 "CRYPTO_destroy_dynlockid, bad reference count\n");
323 if (pointer->references <= 0) {
324 (void)sk_CRYPTO_dynlock_set(dyn_locks, i, NULL);
328 CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
331 dynlock_destroy_callback(pointer->data, __FILE__, __LINE__);
332 OPENSSL_free(pointer);
336 struct CRYPTO_dynlock_value *CRYPTO_get_dynlock_value(int i)
338 CRYPTO_dynlock *pointer = NULL;
342 CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
344 if (dyn_locks != NULL && i < sk_CRYPTO_dynlock_num(dyn_locks))
345 pointer = sk_CRYPTO_dynlock_value(dyn_locks, i);
347 pointer->references++;
349 CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
352 return pointer->data;
356 struct CRYPTO_dynlock_value *(*CRYPTO_get_dynlock_create_callback(void))
357 (const char *file, int line) {
358 return (dynlock_create_callback);
361 void (*CRYPTO_get_dynlock_lock_callback(void)) (int mode,
362 struct CRYPTO_dynlock_value
363 *l, const char *file,
365 return (dynlock_lock_callback);
368 void (*CRYPTO_get_dynlock_destroy_callback(void))
369 (struct CRYPTO_dynlock_value *l, const char *file, int line) {
370 return (dynlock_destroy_callback);
373 void CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value *(*func)
374 (const char *file, int line))
376 dynlock_create_callback = func;
379 void CRYPTO_set_dynlock_lock_callback(void (*func) (int mode,
381 CRYPTO_dynlock_value *l,
385 dynlock_lock_callback = func;
388 void CRYPTO_set_dynlock_destroy_callback(void (*func)
389 (struct CRYPTO_dynlock_value *l,
390 const char *file, int line))
392 dynlock_destroy_callback = func;
395 void (*CRYPTO_get_locking_callback(void)) (int mode, int type,
396 const char *file, int line) {
397 return (locking_callback);
400 int (*CRYPTO_get_add_lock_callback(void)) (int *num, int mount, int type,
401 const char *file, int line) {
402 return (add_lock_callback);
405 void CRYPTO_set_locking_callback(void (*func) (int mode, int type,
406 const char *file, int line))
408 locking_callback = func;
411 void CRYPTO_set_add_lock_callback(int (*func) (int *num, int mount, int type,
412 const char *file, int line))
414 add_lock_callback = func;
418 * the memset() here and in set_pointer() seem overkill, but for the sake of
419 * CRYPTO_THREADID_cmp() this avoids any platform silliness that might cause
420 * two "equal" THREADID structs to not be memcmp()-identical.
422 void CRYPTO_THREADID_set_numeric(CRYPTO_THREADID *id, unsigned long val)
424 memset(id, 0, sizeof(*id));
428 static const unsigned char hash_coeffs[] = { 3, 5, 7, 11, 13, 17, 19, 23 };
430 void CRYPTO_THREADID_set_pointer(CRYPTO_THREADID *id, void *ptr)
432 unsigned char *dest = (void *)&id->val;
433 unsigned int accum = 0;
434 unsigned char dnum = sizeof(id->val);
436 memset(id, 0, sizeof(*id));
438 if (sizeof(id->val) >= sizeof(id->ptr)) {
440 * 'ptr' can be embedded in 'val' without loss of uniqueness
442 id->val = (unsigned long)id->ptr;
446 * hash ptr ==> val. Each byte of 'val' gets the mod-256 total of a
447 * linear function over the bytes in 'ptr', the co-efficients of which
448 * are a sequence of low-primes (hash_coeffs is an 8-element cycle) - the
449 * starting prime for the sequence varies for each byte of 'val' (unique
450 * polynomials unless pointers are >64-bit). For added spice, the totals
451 * accumulate rather than restarting from zero, and the index of the
452 * 'val' byte is added each time (position dependence). If I was a
453 * black-belt, I'd scan big-endian pointers in reverse to give low-order
454 * bits more play, but this isn't crypto and I'd prefer nobody mistake it
455 * as such. Plus I'm lazy.
458 const unsigned char *src = (void *)&id->ptr;
459 unsigned char snum = sizeof(id->ptr);
461 accum += *(src++) * hash_coeffs[(snum + dnum) & 7];
463 *(dest++) = accum & 255;
467 int CRYPTO_THREADID_set_callback(void (*func) (CRYPTO_THREADID *))
469 if (threadid_callback)
471 threadid_callback = func;
475 void (*CRYPTO_THREADID_get_callback(void)) (CRYPTO_THREADID *) {
476 return threadid_callback;
479 void CRYPTO_THREADID_current(CRYPTO_THREADID *id)
481 if (threadid_callback) {
482 threadid_callback(id);
485 #ifndef OPENSSL_NO_DEPRECATED
486 /* If the deprecated callback was set, fall back to that */
488 CRYPTO_THREADID_set_numeric(id, id_callback());
492 /* Else pick a backup */
493 #ifdef OPENSSL_SYS_WIN16
494 CRYPTO_THREADID_set_numeric(id, (unsigned long)GetCurrentTask());
495 #elif defined(OPENSSL_SYS_WIN32)
496 CRYPTO_THREADID_set_numeric(id, (unsigned long)GetCurrentThreadId());
497 #elif defined(OPENSSL_SYS_BEOS)
498 CRYPTO_THREADID_set_numeric(id, (unsigned long)find_thread(NULL));
500 /* For everything else, default to using the address of 'errno' */
501 CRYPTO_THREADID_set_pointer(id, (void *)&errno);
505 int CRYPTO_THREADID_cmp(const CRYPTO_THREADID *a, const CRYPTO_THREADID *b)
507 return memcmp(a, b, sizeof(*a));
510 void CRYPTO_THREADID_cpy(CRYPTO_THREADID *dest, const CRYPTO_THREADID *src)
512 memcpy(dest, src, sizeof(*src));
515 unsigned long CRYPTO_THREADID_hash(const CRYPTO_THREADID *id)
520 #ifndef OPENSSL_NO_DEPRECATED
521 unsigned long (*CRYPTO_get_id_callback(void)) (void) {
522 return (id_callback);
525 void CRYPTO_set_id_callback(unsigned long (*func) (void))
530 unsigned long CRYPTO_thread_id(void)
532 unsigned long ret = 0;
534 if (id_callback == NULL) {
535 # ifdef OPENSSL_SYS_WIN16
536 ret = (unsigned long)GetCurrentTask();
537 # elif defined(OPENSSL_SYS_WIN32)
538 ret = (unsigned long)GetCurrentThreadId();
539 # elif defined(GETPID_IS_MEANINGLESS)
541 # elif defined(OPENSSL_SYS_BEOS)
542 ret = (unsigned long)find_thread(NULL);
544 ret = (unsigned long)getpid();
552 void CRYPTO_lock(int mode, int type, const char *file, int line)
557 char *rw_text, *operation_text;
559 if (mode & CRYPTO_LOCK)
560 operation_text = "lock ";
561 else if (mode & CRYPTO_UNLOCK)
562 operation_text = "unlock";
564 operation_text = "ERROR ";
566 if (mode & CRYPTO_READ)
568 else if (mode & CRYPTO_WRITE)
573 CRYPTO_THREADID_current(&id);
574 fprintf(stderr, "lock:%08lx:(%s)%s %-18s %s:%d\n",
575 CRYPTO_THREADID_hash(&id), rw_text, operation_text,
576 CRYPTO_get_lock_name(type), file, line);
580 if (dynlock_lock_callback != NULL) {
581 struct CRYPTO_dynlock_value *pointer
582 = CRYPTO_get_dynlock_value(type);
584 OPENSSL_assert(pointer != NULL);
586 dynlock_lock_callback(mode, pointer, file, line);
588 CRYPTO_destroy_dynlockid(type);
590 } else if (locking_callback != NULL)
591 locking_callback(mode, type, file, line);
594 int CRYPTO_add_lock(int *pointer, int amount, int type, const char *file,
599 if (add_lock_callback != NULL) {
601 int before = *pointer;
604 ret = add_lock_callback(pointer, amount, type, file, line);
608 CRYPTO_THREADID_current(&id);
609 fprintf(stderr, "ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\n",
610 CRYPTO_THREADID_hash(&id), before, amount, ret,
611 CRYPTO_get_lock_name(type), file, line);
615 CRYPTO_lock(CRYPTO_LOCK | CRYPTO_WRITE, type, file, line);
617 ret = *pointer + amount;
621 CRYPTO_THREADID_current(&id);
622 fprintf(stderr, "ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\n",
623 CRYPTO_THREADID_hash(&id),
624 *pointer, amount, ret,
625 CRYPTO_get_lock_name(type), file, line);
629 CRYPTO_lock(CRYPTO_UNLOCK | CRYPTO_WRITE, type, file, line);
634 const char *CRYPTO_get_lock_name(int type)
638 else if (type < CRYPTO_NUM_LOCKS)
639 return (lock_names[type]);
640 else if (type - CRYPTO_NUM_LOCKS > sk_OPENSSL_STRING_num(app_locks))
643 return (sk_OPENSSL_STRING_value(app_locks, type - CRYPTO_NUM_LOCKS));
646 #if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
647 defined(__INTEL__) || \
648 defined(__x86_64) || defined(__x86_64__) || \
649 defined(_M_AMD64) || defined(_M_X64)
651 unsigned long OPENSSL_ia32cap_P = 0;
652 unsigned long *OPENSSL_ia32cap_loc(void)
654 return &OPENSSL_ia32cap_P;
657 # if defined(OPENSSL_CPUID_OBJ) && !defined(OPENSSL_NO_ASM) && !defined(I386_ONLY)
658 # define OPENSSL_CPUID_SETUP
659 void OPENSSL_cpuid_setup(void)
661 static int trigger = 0;
662 unsigned long OPENSSL_ia32_cpuid(void);
669 if ((env = getenv("OPENSSL_ia32cap")))
670 OPENSSL_ia32cap_P = strtoul(env, NULL, 0) | (1 << 10);
672 OPENSSL_ia32cap_P = OPENSSL_ia32_cpuid() | (1 << 10);
674 * |(1<<10) sets a reserved bit to signal that variable
675 * was initialized already... This is to avoid interference
676 * with cpuid snippets in ELF .init segment.
682 unsigned long *OPENSSL_ia32cap_loc(void)
687 int OPENSSL_NONPIC_relocated = 0;
688 #if !defined(OPENSSL_CPUID_SETUP) && !defined(OPENSSL_CPUID_OBJ)
689 void OPENSSL_cpuid_setup(void)
694 #if (defined(_WIN32) || defined(__CYGWIN__)) && defined(_WINDLL)
696 /* pick DLL_[PROCESS|THREAD]_[ATTACH|DETACH] definitions */
697 # include <windows.h>
699 * this has side-effect of _WIN32 getting defined, which otherwise is
700 * mutually exclusive with __CYGWIN__...
705 * All we really need to do is remove the 'error' state when a thread
709 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
712 case DLL_PROCESS_ATTACH:
713 OPENSSL_cpuid_setup();
714 # if defined(_WIN32_WINNT)
716 IMAGE_DOS_HEADER *dos_header = (IMAGE_DOS_HEADER *) hinstDLL;
717 IMAGE_NT_HEADERS *nt_headers;
719 if (dos_header->e_magic == IMAGE_DOS_SIGNATURE) {
720 nt_headers = (IMAGE_NT_HEADERS *) ((char *)dos_header
721 + dos_header->e_lfanew);
722 if (nt_headers->Signature == IMAGE_NT_SIGNATURE &&
724 (HINSTANCE) (nt_headers->OptionalHeader.ImageBase))
725 OPENSSL_NONPIC_relocated = 1;
730 case DLL_THREAD_ATTACH:
732 case DLL_THREAD_DETACH:
734 case DLL_PROCESS_DETACH:
741 #if defined(_WIN32) && !defined(__CYGWIN__)
745 # if defined(_UNICODE) || defined(__UNICODE__)
746 # define _vsntprintf _vsnwprintf
748 # define _vsntprintf _vsnprintf
752 # define alloca _alloca
755 # if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
756 int OPENSSL_isservice(void)
764 } _OPENSSL_isservice = {
768 if (_OPENSSL_isservice.p == NULL) {
769 HANDLE h = GetModuleHandle(NULL);
771 _OPENSSL_isservice.p = GetProcAddress(h, "_OPENSSL_isservice");
772 if (_OPENSSL_isservice.p == NULL)
773 _OPENSSL_isservice.p = (void *)-1;
776 if (_OPENSSL_isservice.p != (void *)-1)
777 return (*_OPENSSL_isservice.f) ();
779 h = GetProcessWindowStation();
783 if (GetUserObjectInformationW(h, UOI_NAME, NULL, 0, &len) ||
784 GetLastError() != ERROR_INSUFFICIENT_BUFFER)
788 return -1; /* paranoia */
789 len++, len &= ~1; /* paranoia */
790 name = (WCHAR *)alloca(len + sizeof(WCHAR));
791 if (!GetUserObjectInformationW(h, UOI_NAME, name, len, &len))
794 len++, len &= ~1; /* paranoia */
795 name[len / sizeof(WCHAR)] = L'\0'; /* paranoia */
798 * This doesn't cover "interactive" services [working with real
799 * WinSta0's] nor programs started non-interactively by Task Scheduler
800 * [those are working with SAWinSta].
802 if (wcsstr(name, L"Service-0x"))
805 /* This covers all non-interactive programs such as services. */
806 if (!wcsstr(name, L"WinSta0"))
813 int OPENSSL_isservice(void)
819 void OPENSSL_showfatal(const char *fmta, ...)
824 # ifdef STD_ERROR_HANDLE /* what a dirty trick! */
827 if ((h = GetStdHandle(STD_ERROR_HANDLE)) != NULL &&
828 GetFileType(h) != FILE_TYPE_UNKNOWN) {
829 /* must be console application */
831 vfprintf(stderr, fmta, ap);
837 if (sizeof(TCHAR) == sizeof(char))
838 fmt = (const TCHAR *)fmta;
842 size_t len_0 = strlen(fmta) + 1, i;
845 fmtw = (WCHAR *)alloca(len_0 * sizeof(WCHAR));
847 fmt = (const TCHAR *)L"no stack?";
850 # ifndef OPENSSL_NO_MULTIBYTE
851 if (!MultiByteToWideChar(CP_ACP, 0, fmta, len_0, fmtw, len_0))
853 for (i = 0; i < len_0; i++)
854 fmtw[i] = (WCHAR)fmta[i];
856 for (i = 0; i < len_0; i++) {
860 switch (fmtw[i + 1]) {
892 fmt = (const TCHAR *)fmtw;
896 _vsntprintf(buf, sizeof(buf) / sizeof(TCHAR) - 1, fmt, ap);
897 buf[sizeof(buf) / sizeof(TCHAR) - 1] = _T('\0');
900 # if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
901 /* this -------------v--- guards NT-specific calls */
902 if (check_winnt() && OPENSSL_isservice() > 0) {
903 HANDLE h = RegisterEventSource(0, _T("OPENSSL"));
904 const TCHAR *pmsg = buf;
905 ReportEvent(h, EVENTLOG_ERROR_TYPE, 0, 0, 0, 1, 0, &pmsg, 0);
906 DeregisterEventSource(h);
909 MessageBox(NULL, buf, _T("OpenSSL: FATAL"), MB_OK | MB_ICONSTOP);
912 void OPENSSL_showfatal(const char *fmta, ...)
917 vfprintf(stderr, fmta, ap);
921 int OPENSSL_isservice(void)
927 void OpenSSLDie(const char *file, int line, const char *assertion)
930 ("%s(%d): OpenSSL internal error, assertion failed: %s\n", file, line,
932 #if !defined(_WIN32) || defined(__CYGWIN__)
936 * Win32 abort() customarily shows a dialog, but we just did that...
943 void *OPENSSL_stderr(void)
948 int CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len)
951 const unsigned char *a = in_a;
952 const unsigned char *b = in_b;
955 for (i = 0; i < len; i++)