xref: /onnv-gate/usr/src/common/openssl/crypto/cryptlib.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/cryptlib.c */
2*2139Sjp161948 /* ====================================================================
3*2139Sjp161948  * Copyright (c) 1998-2003 The OpenSSL Project.  All rights reserved.
4*2139Sjp161948  *
5*2139Sjp161948  * Redistribution and use in source and binary forms, with or without
6*2139Sjp161948  * modification, are permitted provided that the following conditions
7*2139Sjp161948  * are met:
8*2139Sjp161948  *
9*2139Sjp161948  * 1. Redistributions of source code must retain the above copyright
10*2139Sjp161948  *    notice, this list of conditions and the following disclaimer.
11*2139Sjp161948  *
12*2139Sjp161948  * 2. Redistributions in binary form must reproduce the above copyright
13*2139Sjp161948  *    notice, this list of conditions and the following disclaimer in
14*2139Sjp161948  *    the documentation and/or other materials provided with the
15*2139Sjp161948  *    distribution.
16*2139Sjp161948  *
17*2139Sjp161948  * 3. All advertising materials mentioning features or use of this
18*2139Sjp161948  *    software must display the following acknowledgment:
19*2139Sjp161948  *    "This product includes software developed by the OpenSSL Project
20*2139Sjp161948  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21*2139Sjp161948  *
22*2139Sjp161948  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23*2139Sjp161948  *    endorse or promote products derived from this software without
24*2139Sjp161948  *    prior written permission. For written permission, please contact
25*2139Sjp161948  *    openssl-core@openssl.org.
26*2139Sjp161948  *
27*2139Sjp161948  * 5. Products derived from this software may not be called "OpenSSL"
28*2139Sjp161948  *    nor may "OpenSSL" appear in their names without prior written
29*2139Sjp161948  *    permission of the OpenSSL Project.
30*2139Sjp161948  *
31*2139Sjp161948  * 6. Redistributions of any form whatsoever must retain the following
32*2139Sjp161948  *    acknowledgment:
33*2139Sjp161948  *    "This product includes software developed by the OpenSSL Project
34*2139Sjp161948  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35*2139Sjp161948  *
36*2139Sjp161948  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37*2139Sjp161948  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38*2139Sjp161948  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39*2139Sjp161948  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40*2139Sjp161948  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41*2139Sjp161948  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42*2139Sjp161948  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43*2139Sjp161948  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44*2139Sjp161948  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45*2139Sjp161948  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46*2139Sjp161948  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47*2139Sjp161948  * OF THE POSSIBILITY OF SUCH DAMAGE.
48*2139Sjp161948  * ====================================================================
49*2139Sjp161948  *
50*2139Sjp161948  * This product includes cryptographic software written by Eric Young
51*2139Sjp161948  * (eay@cryptsoft.com).  This product includes software written by Tim
52*2139Sjp161948  * Hudson (tjh@cryptsoft.com).
53*2139Sjp161948  *
54*2139Sjp161948  */
550Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
560Sstevel@tonic-gate  * All rights reserved.
570Sstevel@tonic-gate  *
580Sstevel@tonic-gate  * This package is an SSL implementation written
590Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
600Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
610Sstevel@tonic-gate  *
620Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
630Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
640Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
650Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
660Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
670Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
680Sstevel@tonic-gate  *
690Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
700Sstevel@tonic-gate  * the code are not to be removed.
710Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
720Sstevel@tonic-gate  * as the author of the parts of the library used.
730Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
740Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
750Sstevel@tonic-gate  *
760Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
770Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
780Sstevel@tonic-gate  * are met:
790Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
800Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
810Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
820Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
830Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
840Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
850Sstevel@tonic-gate  *    must display the following acknowledgement:
860Sstevel@tonic-gate  *    "This product includes cryptographic software written by
870Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
880Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
890Sstevel@tonic-gate  *    being used are not cryptographic related :-).
900Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
910Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
920Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
930Sstevel@tonic-gate  *
940Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
950Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
960Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
970Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
980Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
990Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1000Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1010Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1020Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1030Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1040Sstevel@tonic-gate  * SUCH DAMAGE.
1050Sstevel@tonic-gate  *
1060Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
1070Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
1080Sstevel@tonic-gate  * copied and put under another distribution licence
1090Sstevel@tonic-gate  * [including the GNU Public Licence.]
1100Sstevel@tonic-gate  */
111*2139Sjp161948 /* ====================================================================
112*2139Sjp161948  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
113*2139Sjp161948  * ECDH support in OpenSSL originally developed by
114*2139Sjp161948  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
115*2139Sjp161948  */
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate #include "cryptlib.h"
1180Sstevel@tonic-gate #include <openssl/safestack.h>
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate #if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16)
1210Sstevel@tonic-gate static double SSLeay_MSVC5_hack=0.0; /* and for VC1.5 */
1220Sstevel@tonic-gate #endif
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate DECLARE_STACK_OF(CRYPTO_dynlock)
1250Sstevel@tonic-gate IMPLEMENT_STACK_OF(CRYPTO_dynlock)
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate /* real #defines in crypto.h, keep these upto date */
1280Sstevel@tonic-gate static const char* lock_names[CRYPTO_NUM_LOCKS] =
1290Sstevel@tonic-gate 	{
1300Sstevel@tonic-gate 	"<<ERROR>>",
1310Sstevel@tonic-gate 	"err",
1320Sstevel@tonic-gate 	"ex_data",
1330Sstevel@tonic-gate 	"x509",
1340Sstevel@tonic-gate 	"x509_info",
1350Sstevel@tonic-gate 	"x509_pkey",
1360Sstevel@tonic-gate 	"x509_crl",
1370Sstevel@tonic-gate 	"x509_req",
1380Sstevel@tonic-gate 	"dsa",
1390Sstevel@tonic-gate 	"rsa",
1400Sstevel@tonic-gate 	"evp_pkey",
1410Sstevel@tonic-gate 	"x509_store",
1420Sstevel@tonic-gate 	"ssl_ctx",
1430Sstevel@tonic-gate 	"ssl_cert",
1440Sstevel@tonic-gate 	"ssl_session",
1450Sstevel@tonic-gate 	"ssl_sess_cert",
1460Sstevel@tonic-gate 	"ssl",
1470Sstevel@tonic-gate 	"ssl_method",
1480Sstevel@tonic-gate 	"rand",
1490Sstevel@tonic-gate 	"rand2",
1500Sstevel@tonic-gate 	"debug_malloc",
1510Sstevel@tonic-gate 	"BIO",
1520Sstevel@tonic-gate 	"gethostbyname",
1530Sstevel@tonic-gate 	"getservbyname",
1540Sstevel@tonic-gate 	"readdir",
1550Sstevel@tonic-gate 	"RSA_blinding",
1560Sstevel@tonic-gate 	"dh",
1570Sstevel@tonic-gate 	"debug_malloc2",
1580Sstevel@tonic-gate 	"dso",
1590Sstevel@tonic-gate 	"dynlock",
1600Sstevel@tonic-gate 	"engine",
1610Sstevel@tonic-gate 	"ui",
162*2139Sjp161948 	"ecdsa",
163*2139Sjp161948 	"ec",
164*2139Sjp161948 	"ecdh",
165*2139Sjp161948 	"bn",
166*2139Sjp161948 	"ec_pre_comp",
167*2139Sjp161948 	"store",
168*2139Sjp161948 	"comp",
169*2139Sjp161948 #if CRYPTO_NUM_LOCKS != 39
1700Sstevel@tonic-gate # error "Inconsistency between crypto.h and cryptlib.c"
1710Sstevel@tonic-gate #endif
1720Sstevel@tonic-gate 	};
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate /* This is for applications to allocate new type names in the non-dynamic
1750Sstevel@tonic-gate    array of lock names.  These are numbered with positive numbers.  */
1760Sstevel@tonic-gate static STACK *app_locks=NULL;
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate /* For applications that want a more dynamic way of handling threads, the
1790Sstevel@tonic-gate    following stack is used.  These are externally numbered with negative
1800Sstevel@tonic-gate    numbers.  */
1810Sstevel@tonic-gate static STACK_OF(CRYPTO_dynlock) *dyn_locks=NULL;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate static void (MS_FAR *locking_callback)(int mode,int type,
1850Sstevel@tonic-gate 	const char *file,int line)=NULL;
1860Sstevel@tonic-gate static int (MS_FAR *add_lock_callback)(int *pointer,int amount,
1870Sstevel@tonic-gate 	int type,const char *file,int line)=NULL;
1880Sstevel@tonic-gate static unsigned long (MS_FAR *id_callback)(void)=NULL;
1890Sstevel@tonic-gate static struct CRYPTO_dynlock_value *(MS_FAR *dynlock_create_callback)
1900Sstevel@tonic-gate 	(const char *file,int line)=NULL;
1910Sstevel@tonic-gate static void (MS_FAR *dynlock_lock_callback)(int mode,
1920Sstevel@tonic-gate 	struct CRYPTO_dynlock_value *l, const char *file,int line)=NULL;
1930Sstevel@tonic-gate static void (MS_FAR *dynlock_destroy_callback)(struct CRYPTO_dynlock_value *l,
1940Sstevel@tonic-gate 	const char *file,int line)=NULL;
1950Sstevel@tonic-gate 
CRYPTO_get_new_lockid(char * name)1960Sstevel@tonic-gate int CRYPTO_get_new_lockid(char *name)
1970Sstevel@tonic-gate 	{
1980Sstevel@tonic-gate 	char *str;
1990Sstevel@tonic-gate 	int i;
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate #if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16)
2020Sstevel@tonic-gate 	/* A hack to make Visual C++ 5.0 work correctly when linking as
2030Sstevel@tonic-gate 	 * a DLL using /MT. Without this, the application cannot use
2040Sstevel@tonic-gate 	 * and floating point printf's.
2050Sstevel@tonic-gate 	 * It also seems to be needed for Visual C 1.5 (win16) */
2060Sstevel@tonic-gate 	SSLeay_MSVC5_hack=(double)name[0]*(double)name[1];
2070Sstevel@tonic-gate #endif
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	if ((app_locks == NULL) && ((app_locks=sk_new_null()) == NULL))
2100Sstevel@tonic-gate 		{
2110Sstevel@tonic-gate 		CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_LOCKID,ERR_R_MALLOC_FAILURE);
2120Sstevel@tonic-gate 		return(0);
2130Sstevel@tonic-gate 		}
2140Sstevel@tonic-gate 	if ((str=BUF_strdup(name)) == NULL)
2150Sstevel@tonic-gate 		{
2160Sstevel@tonic-gate 		CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_LOCKID,ERR_R_MALLOC_FAILURE);
2170Sstevel@tonic-gate 		return(0);
2180Sstevel@tonic-gate 		}
2190Sstevel@tonic-gate 	i=sk_push(app_locks,str);
2200Sstevel@tonic-gate 	if (!i)
2210Sstevel@tonic-gate 		OPENSSL_free(str);
2220Sstevel@tonic-gate 	else
2230Sstevel@tonic-gate 		i+=CRYPTO_NUM_LOCKS; /* gap of one :-) */
2240Sstevel@tonic-gate 	return(i);
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate 
CRYPTO_num_locks(void)2270Sstevel@tonic-gate int CRYPTO_num_locks(void)
2280Sstevel@tonic-gate 	{
2290Sstevel@tonic-gate 	return CRYPTO_NUM_LOCKS;
2300Sstevel@tonic-gate 	}
2310Sstevel@tonic-gate 
CRYPTO_get_new_dynlockid(void)2320Sstevel@tonic-gate int CRYPTO_get_new_dynlockid(void)
2330Sstevel@tonic-gate 	{
2340Sstevel@tonic-gate 	int i = 0;
2350Sstevel@tonic-gate 	CRYPTO_dynlock *pointer = NULL;
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	if (dynlock_create_callback == NULL)
2380Sstevel@tonic-gate 		{
2390Sstevel@tonic-gate 		CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID,CRYPTO_R_NO_DYNLOCK_CREATE_CALLBACK);
2400Sstevel@tonic-gate 		return(0);
2410Sstevel@tonic-gate 		}
2420Sstevel@tonic-gate 	CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
2430Sstevel@tonic-gate 	if ((dyn_locks == NULL)
2440Sstevel@tonic-gate 		&& ((dyn_locks=sk_CRYPTO_dynlock_new_null()) == NULL))
2450Sstevel@tonic-gate 		{
2460Sstevel@tonic-gate 		CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
2470Sstevel@tonic-gate 		CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID,ERR_R_MALLOC_FAILURE);
2480Sstevel@tonic-gate 		return(0);
2490Sstevel@tonic-gate 		}
2500Sstevel@tonic-gate 	CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	pointer = (CRYPTO_dynlock *)OPENSSL_malloc(sizeof(CRYPTO_dynlock));
2530Sstevel@tonic-gate 	if (pointer == NULL)
2540Sstevel@tonic-gate 		{
2550Sstevel@tonic-gate 		CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID,ERR_R_MALLOC_FAILURE);
2560Sstevel@tonic-gate 		return(0);
2570Sstevel@tonic-gate 		}
2580Sstevel@tonic-gate 	pointer->references = 1;
2590Sstevel@tonic-gate 	pointer->data = dynlock_create_callback(__FILE__,__LINE__);
2600Sstevel@tonic-gate 	if (pointer->data == NULL)
2610Sstevel@tonic-gate 		{
2620Sstevel@tonic-gate 		OPENSSL_free(pointer);
2630Sstevel@tonic-gate 		CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID,ERR_R_MALLOC_FAILURE);
2640Sstevel@tonic-gate 		return(0);
2650Sstevel@tonic-gate 		}
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
2680Sstevel@tonic-gate 	/* First, try to find an existing empty slot */
2690Sstevel@tonic-gate 	i=sk_CRYPTO_dynlock_find(dyn_locks,NULL);
2700Sstevel@tonic-gate 	/* If there was none, push, thereby creating a new one */
2710Sstevel@tonic-gate 	if (i == -1)
2720Sstevel@tonic-gate 		/* Since sk_push() returns the number of items on the
2730Sstevel@tonic-gate 		   stack, not the location of the pushed item, we need
2740Sstevel@tonic-gate 		   to transform the returned number into a position,
2750Sstevel@tonic-gate 		   by decreasing it.  */
2760Sstevel@tonic-gate 		i=sk_CRYPTO_dynlock_push(dyn_locks,pointer) - 1;
2770Sstevel@tonic-gate 	else
2780Sstevel@tonic-gate 		/* If we found a place with a NULL pointer, put our pointer
2790Sstevel@tonic-gate 		   in it.  */
2800Sstevel@tonic-gate 		sk_CRYPTO_dynlock_set(dyn_locks,i,pointer);
2810Sstevel@tonic-gate 	CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	if (i == -1)
2840Sstevel@tonic-gate 		{
2850Sstevel@tonic-gate 		dynlock_destroy_callback(pointer->data,__FILE__,__LINE__);
2860Sstevel@tonic-gate 		OPENSSL_free(pointer);
2870Sstevel@tonic-gate 		}
2880Sstevel@tonic-gate 	else
2890Sstevel@tonic-gate 		i += 1; /* to avoid 0 */
2900Sstevel@tonic-gate 	return -i;
2910Sstevel@tonic-gate 	}
2920Sstevel@tonic-gate 
CRYPTO_destroy_dynlockid(int i)2930Sstevel@tonic-gate void CRYPTO_destroy_dynlockid(int i)
2940Sstevel@tonic-gate 	{
2950Sstevel@tonic-gate 	CRYPTO_dynlock *pointer = NULL;
2960Sstevel@tonic-gate 	if (i)
2970Sstevel@tonic-gate 		i = -i-1;
2980Sstevel@tonic-gate 	if (dynlock_destroy_callback == NULL)
2990Sstevel@tonic-gate 		return;
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	if (dyn_locks == NULL || i >= sk_CRYPTO_dynlock_num(dyn_locks))
3040Sstevel@tonic-gate 		{
3050Sstevel@tonic-gate 		CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
3060Sstevel@tonic-gate 		return;
3070Sstevel@tonic-gate 		}
3080Sstevel@tonic-gate 	pointer = sk_CRYPTO_dynlock_value(dyn_locks, i);
3090Sstevel@tonic-gate 	if (pointer != NULL)
3100Sstevel@tonic-gate 		{
3110Sstevel@tonic-gate 		--pointer->references;
3120Sstevel@tonic-gate #ifdef REF_CHECK
3130Sstevel@tonic-gate 		if (pointer->references < 0)
3140Sstevel@tonic-gate 			{
3150Sstevel@tonic-gate 			fprintf(stderr,"CRYPTO_destroy_dynlockid, bad reference count\n");
3160Sstevel@tonic-gate 			abort();
3170Sstevel@tonic-gate 			}
3180Sstevel@tonic-gate 		else
3190Sstevel@tonic-gate #endif
3200Sstevel@tonic-gate 			if (pointer->references <= 0)
3210Sstevel@tonic-gate 				{
3220Sstevel@tonic-gate 				sk_CRYPTO_dynlock_set(dyn_locks, i, NULL);
3230Sstevel@tonic-gate 				}
3240Sstevel@tonic-gate 			else
3250Sstevel@tonic-gate 				pointer = NULL;
3260Sstevel@tonic-gate 		}
3270Sstevel@tonic-gate 	CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	if (pointer)
3300Sstevel@tonic-gate 		{
3310Sstevel@tonic-gate 		dynlock_destroy_callback(pointer->data,__FILE__,__LINE__);
3320Sstevel@tonic-gate 		OPENSSL_free(pointer);
3330Sstevel@tonic-gate 		}
3340Sstevel@tonic-gate 	}
3350Sstevel@tonic-gate 
CRYPTO_get_dynlock_value(int i)3360Sstevel@tonic-gate struct CRYPTO_dynlock_value *CRYPTO_get_dynlock_value(int i)
3370Sstevel@tonic-gate 	{
3380Sstevel@tonic-gate 	CRYPTO_dynlock *pointer = NULL;
3390Sstevel@tonic-gate 	if (i)
3400Sstevel@tonic-gate 		i = -i-1;
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	if (dyn_locks != NULL && i < sk_CRYPTO_dynlock_num(dyn_locks))
3450Sstevel@tonic-gate 		pointer = sk_CRYPTO_dynlock_value(dyn_locks, i);
3460Sstevel@tonic-gate 	if (pointer)
3470Sstevel@tonic-gate 		pointer->references++;
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	if (pointer)
3520Sstevel@tonic-gate 		return pointer->data;
3530Sstevel@tonic-gate 	return NULL;
3540Sstevel@tonic-gate 	}
3550Sstevel@tonic-gate 
CRYPTO_get_dynlock_create_callback(void)3560Sstevel@tonic-gate struct CRYPTO_dynlock_value *(*CRYPTO_get_dynlock_create_callback(void))
3570Sstevel@tonic-gate 	(const char *file,int line)
3580Sstevel@tonic-gate 	{
3590Sstevel@tonic-gate 	return(dynlock_create_callback);
3600Sstevel@tonic-gate 	}
3610Sstevel@tonic-gate 
CRYPTO_get_dynlock_lock_callback(void)3620Sstevel@tonic-gate void (*CRYPTO_get_dynlock_lock_callback(void))(int mode,
3630Sstevel@tonic-gate 	struct CRYPTO_dynlock_value *l, const char *file,int line)
3640Sstevel@tonic-gate 	{
3650Sstevel@tonic-gate 	return(dynlock_lock_callback);
3660Sstevel@tonic-gate 	}
3670Sstevel@tonic-gate 
CRYPTO_get_dynlock_destroy_callback(void)3680Sstevel@tonic-gate void (*CRYPTO_get_dynlock_destroy_callback(void))
3690Sstevel@tonic-gate 	(struct CRYPTO_dynlock_value *l, const char *file,int line)
3700Sstevel@tonic-gate 	{
3710Sstevel@tonic-gate 	return(dynlock_destroy_callback);
3720Sstevel@tonic-gate 	}
3730Sstevel@tonic-gate 
CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value * (* func)(const char * file,int line))3740Sstevel@tonic-gate void CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value *(*func)
3750Sstevel@tonic-gate 	(const char *file, int line))
3760Sstevel@tonic-gate 	{
3770Sstevel@tonic-gate 	dynlock_create_callback=func;
3780Sstevel@tonic-gate 	}
3790Sstevel@tonic-gate 
CRYPTO_set_dynlock_lock_callback(void (* func)(int mode,struct CRYPTO_dynlock_value * l,const char * file,int line))3800Sstevel@tonic-gate void CRYPTO_set_dynlock_lock_callback(void (*func)(int mode,
3810Sstevel@tonic-gate 	struct CRYPTO_dynlock_value *l, const char *file, int line))
3820Sstevel@tonic-gate 	{
3830Sstevel@tonic-gate 	dynlock_lock_callback=func;
3840Sstevel@tonic-gate 	}
3850Sstevel@tonic-gate 
CRYPTO_set_dynlock_destroy_callback(void (* func)(struct CRYPTO_dynlock_value * l,const char * file,int line))3860Sstevel@tonic-gate void CRYPTO_set_dynlock_destroy_callback(void (*func)
3870Sstevel@tonic-gate 	(struct CRYPTO_dynlock_value *l, const char *file, int line))
3880Sstevel@tonic-gate 	{
3890Sstevel@tonic-gate 	dynlock_destroy_callback=func;
3900Sstevel@tonic-gate 	}
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 
CRYPTO_get_locking_callback(void)3930Sstevel@tonic-gate void (*CRYPTO_get_locking_callback(void))(int mode,int type,const char *file,
3940Sstevel@tonic-gate 		int line)
3950Sstevel@tonic-gate 	{
3960Sstevel@tonic-gate 	return(locking_callback);
3970Sstevel@tonic-gate 	}
3980Sstevel@tonic-gate 
CRYPTO_get_add_lock_callback(void)3990Sstevel@tonic-gate int (*CRYPTO_get_add_lock_callback(void))(int *num,int mount,int type,
4000Sstevel@tonic-gate 					  const char *file,int line)
4010Sstevel@tonic-gate 	{
4020Sstevel@tonic-gate 	return(add_lock_callback);
4030Sstevel@tonic-gate 	}
4040Sstevel@tonic-gate 
CRYPTO_set_locking_callback(void (* func)(int mode,int type,const char * file,int line))4050Sstevel@tonic-gate void CRYPTO_set_locking_callback(void (*func)(int mode,int type,
4060Sstevel@tonic-gate 					      const char *file,int line))
4070Sstevel@tonic-gate 	{
4080Sstevel@tonic-gate 	locking_callback=func;
4090Sstevel@tonic-gate 	}
4100Sstevel@tonic-gate 
CRYPTO_set_add_lock_callback(int (* func)(int * num,int mount,int type,const char * file,int line))4110Sstevel@tonic-gate void CRYPTO_set_add_lock_callback(int (*func)(int *num,int mount,int type,
4120Sstevel@tonic-gate 					      const char *file,int line))
4130Sstevel@tonic-gate 	{
4140Sstevel@tonic-gate 	add_lock_callback=func;
4150Sstevel@tonic-gate 	}
4160Sstevel@tonic-gate 
CRYPTO_get_id_callback(void)4170Sstevel@tonic-gate unsigned long (*CRYPTO_get_id_callback(void))(void)
4180Sstevel@tonic-gate 	{
4190Sstevel@tonic-gate 	return(id_callback);
4200Sstevel@tonic-gate 	}
4210Sstevel@tonic-gate 
CRYPTO_set_id_callback(unsigned long (* func)(void))4220Sstevel@tonic-gate void CRYPTO_set_id_callback(unsigned long (*func)(void))
4230Sstevel@tonic-gate 	{
4240Sstevel@tonic-gate 	id_callback=func;
4250Sstevel@tonic-gate 	}
4260Sstevel@tonic-gate 
CRYPTO_thread_id(void)4270Sstevel@tonic-gate unsigned long CRYPTO_thread_id(void)
4280Sstevel@tonic-gate 	{
4290Sstevel@tonic-gate 	unsigned long ret=0;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	if (id_callback == NULL)
4320Sstevel@tonic-gate 		{
4330Sstevel@tonic-gate #ifdef OPENSSL_SYS_WIN16
4340Sstevel@tonic-gate 		ret=(unsigned long)GetCurrentTask();
4350Sstevel@tonic-gate #elif defined(OPENSSL_SYS_WIN32)
4360Sstevel@tonic-gate 		ret=(unsigned long)GetCurrentThreadId();
4370Sstevel@tonic-gate #elif defined(GETPID_IS_MEANINGLESS)
4380Sstevel@tonic-gate 		ret=1L;
4390Sstevel@tonic-gate #else
4400Sstevel@tonic-gate 		ret=(unsigned long)getpid();
4410Sstevel@tonic-gate #endif
4420Sstevel@tonic-gate 		}
4430Sstevel@tonic-gate 	else
4440Sstevel@tonic-gate 		ret=id_callback();
4450Sstevel@tonic-gate 	return(ret);
4460Sstevel@tonic-gate 	}
4470Sstevel@tonic-gate 
CRYPTO_lock(int mode,int type,const char * file,int line)4480Sstevel@tonic-gate void CRYPTO_lock(int mode, int type, const char *file, int line)
4490Sstevel@tonic-gate 	{
4500Sstevel@tonic-gate #ifdef LOCK_DEBUG
4510Sstevel@tonic-gate 		{
4520Sstevel@tonic-gate 		char *rw_text,*operation_text;
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 		if (mode & CRYPTO_LOCK)
4550Sstevel@tonic-gate 			operation_text="lock  ";
4560Sstevel@tonic-gate 		else if (mode & CRYPTO_UNLOCK)
4570Sstevel@tonic-gate 			operation_text="unlock";
4580Sstevel@tonic-gate 		else
4590Sstevel@tonic-gate 			operation_text="ERROR ";
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 		if (mode & CRYPTO_READ)
4620Sstevel@tonic-gate 			rw_text="r";
4630Sstevel@tonic-gate 		else if (mode & CRYPTO_WRITE)
4640Sstevel@tonic-gate 			rw_text="w";
4650Sstevel@tonic-gate 		else
4660Sstevel@tonic-gate 			rw_text="ERROR";
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 		fprintf(stderr,"lock:%08lx:(%s)%s %-18s %s:%d\n",
4690Sstevel@tonic-gate 			CRYPTO_thread_id(), rw_text, operation_text,
4700Sstevel@tonic-gate 			CRYPTO_get_lock_name(type), file, line);
4710Sstevel@tonic-gate 		}
4720Sstevel@tonic-gate #endif
4730Sstevel@tonic-gate 	if (type < 0)
4740Sstevel@tonic-gate 		{
4750Sstevel@tonic-gate 		if (dynlock_lock_callback != NULL)
4760Sstevel@tonic-gate 			{
4770Sstevel@tonic-gate 			struct CRYPTO_dynlock_value *pointer
4780Sstevel@tonic-gate 				= CRYPTO_get_dynlock_value(type);
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 			OPENSSL_assert(pointer != NULL);
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 			dynlock_lock_callback(mode, pointer, file, line);
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 			CRYPTO_destroy_dynlockid(type);
4850Sstevel@tonic-gate 			}
4860Sstevel@tonic-gate 		}
4870Sstevel@tonic-gate 	else
4880Sstevel@tonic-gate 		if (locking_callback != NULL)
4890Sstevel@tonic-gate 			locking_callback(mode,type,file,line);
4900Sstevel@tonic-gate 	}
4910Sstevel@tonic-gate 
CRYPTO_add_lock(int * pointer,int amount,int type,const char * file,int line)4920Sstevel@tonic-gate int CRYPTO_add_lock(int *pointer, int amount, int type, const char *file,
4930Sstevel@tonic-gate 	     int line)
4940Sstevel@tonic-gate 	{
4950Sstevel@tonic-gate 	int ret = 0;
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	if (add_lock_callback != NULL)
4980Sstevel@tonic-gate 		{
4990Sstevel@tonic-gate #ifdef LOCK_DEBUG
5000Sstevel@tonic-gate 		int before= *pointer;
5010Sstevel@tonic-gate #endif
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 		ret=add_lock_callback(pointer,amount,type,file,line);
5040Sstevel@tonic-gate #ifdef LOCK_DEBUG
5050Sstevel@tonic-gate 		fprintf(stderr,"ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\n",
5060Sstevel@tonic-gate 			CRYPTO_thread_id(),
5070Sstevel@tonic-gate 			before,amount,ret,
5080Sstevel@tonic-gate 			CRYPTO_get_lock_name(type),
5090Sstevel@tonic-gate 			file,line);
5100Sstevel@tonic-gate #endif
5110Sstevel@tonic-gate 		}
5120Sstevel@tonic-gate 	else
5130Sstevel@tonic-gate 		{
5140Sstevel@tonic-gate 		CRYPTO_lock(CRYPTO_LOCK|CRYPTO_WRITE,type,file,line);
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 		ret= *pointer+amount;
5170Sstevel@tonic-gate #ifdef LOCK_DEBUG
5180Sstevel@tonic-gate 		fprintf(stderr,"ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\n",
5190Sstevel@tonic-gate 			CRYPTO_thread_id(),
5200Sstevel@tonic-gate 			*pointer,amount,ret,
5210Sstevel@tonic-gate 			CRYPTO_get_lock_name(type),
5220Sstevel@tonic-gate 			file,line);
5230Sstevel@tonic-gate #endif
5240Sstevel@tonic-gate 		*pointer=ret;
5250Sstevel@tonic-gate 		CRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_WRITE,type,file,line);
5260Sstevel@tonic-gate 		}
5270Sstevel@tonic-gate 	return(ret);
5280Sstevel@tonic-gate 	}
5290Sstevel@tonic-gate 
CRYPTO_get_lock_name(int type)5300Sstevel@tonic-gate const char *CRYPTO_get_lock_name(int type)
5310Sstevel@tonic-gate 	{
5320Sstevel@tonic-gate 	if (type < 0)
5330Sstevel@tonic-gate 		return("dynamic");
5340Sstevel@tonic-gate 	else if (type < CRYPTO_NUM_LOCKS)
5350Sstevel@tonic-gate 		return(lock_names[type]);
5360Sstevel@tonic-gate 	else if (type-CRYPTO_NUM_LOCKS > sk_num(app_locks))
5370Sstevel@tonic-gate 		return("ERROR");
5380Sstevel@tonic-gate 	else
5390Sstevel@tonic-gate 		return(sk_value(app_locks,type-CRYPTO_NUM_LOCKS));
5400Sstevel@tonic-gate 	}
5410Sstevel@tonic-gate 
542*2139Sjp161948 #if	defined(__i386)   || defined(__i386__)   || defined(_M_IX86) || \
543*2139Sjp161948 	defined(__INTEL__) || \
544*2139Sjp161948 	defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64)
545*2139Sjp161948 
546*2139Sjp161948 unsigned long  OPENSSL_ia32cap_P=0;
OPENSSL_ia32cap_loc(void)547*2139Sjp161948 unsigned long *OPENSSL_ia32cap_loc(void) { return &OPENSSL_ia32cap_P; }
548*2139Sjp161948 
549*2139Sjp161948 #if defined(OPENSSL_CPUID_OBJ) && !defined(OPENSSL_NO_ASM) && !defined(I386_ONLY)
550*2139Sjp161948 #define OPENSSL_CPUID_SETUP
OPENSSL_cpuid_setup(void)551*2139Sjp161948 void OPENSSL_cpuid_setup(void)
552*2139Sjp161948 { static int trigger=0;
553*2139Sjp161948   unsigned long OPENSSL_ia32_cpuid(void);
554*2139Sjp161948   char *env;
555*2139Sjp161948 
556*2139Sjp161948     if (trigger)	return;
557*2139Sjp161948 
558*2139Sjp161948     trigger=1;
559*2139Sjp161948     if ((env=getenv("OPENSSL_ia32cap")))
560*2139Sjp161948 	OPENSSL_ia32cap_P = strtoul(env,NULL,0)|(1<<10);
561*2139Sjp161948     else
562*2139Sjp161948 	OPENSSL_ia32cap_P = OPENSSL_ia32_cpuid()|(1<<10);
563*2139Sjp161948     /*
564*2139Sjp161948      * |(1<<10) sets a reserved bit to signal that variable
565*2139Sjp161948      * was initialized already... This is to avoid interference
566*2139Sjp161948      * with cpuid snippets in ELF .init segment.
567*2139Sjp161948      */
568*2139Sjp161948 }
569*2139Sjp161948 #endif
570*2139Sjp161948 
571*2139Sjp161948 #else
OPENSSL_ia32cap_loc(void)572*2139Sjp161948 unsigned long *OPENSSL_ia32cap_loc(void) { return NULL; }
573*2139Sjp161948 #endif
574*2139Sjp161948 int OPENSSL_NONPIC_relocated = 0;
575*2139Sjp161948 #if !defined(OPENSSL_CPUID_SETUP)
OPENSSL_cpuid_setup(void)576*2139Sjp161948 void OPENSSL_cpuid_setup(void) {}
577*2139Sjp161948 #endif
578*2139Sjp161948 
579*2139Sjp161948 #if (defined(_WIN32) || defined(__CYGWIN__)) && defined(_WINDLL)
580*2139Sjp161948 #ifdef __CYGWIN__
581*2139Sjp161948 /* pick DLL_[PROCESS|THREAD]_[ATTACH|DETACH] definitions */
582*2139Sjp161948 #include <windows.h>
583*2139Sjp161948 #endif
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate /* All we really need to do is remove the 'error' state when a thread
5860Sstevel@tonic-gate  * detaches */
5870Sstevel@tonic-gate 
DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)588*2139Sjp161948 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
5890Sstevel@tonic-gate 	     LPVOID lpvReserved)
5900Sstevel@tonic-gate 	{
5910Sstevel@tonic-gate 	switch(fdwReason)
5920Sstevel@tonic-gate 		{
5930Sstevel@tonic-gate 	case DLL_PROCESS_ATTACH:
594*2139Sjp161948 		OPENSSL_cpuid_setup();
595*2139Sjp161948 #if defined(_WIN32_WINNT)
596*2139Sjp161948 		{
597*2139Sjp161948 		IMAGE_DOS_HEADER *dos_header = (IMAGE_DOS_HEADER *)hinstDLL;
598*2139Sjp161948 		IMAGE_NT_HEADERS *nt_headers;
599*2139Sjp161948 
600*2139Sjp161948 		if (dos_header->e_magic==IMAGE_DOS_SIGNATURE)
601*2139Sjp161948 			{
602*2139Sjp161948 			nt_headers = (IMAGE_NT_HEADERS *)((char *)dos_header
603*2139Sjp161948 						+ dos_header->e_lfanew);
604*2139Sjp161948 			if (nt_headers->Signature==IMAGE_NT_SIGNATURE &&
605*2139Sjp161948 			    hinstDLL!=(HINSTANCE)(nt_headers->OptionalHeader.ImageBase))
606*2139Sjp161948 				OPENSSL_NONPIC_relocated=1;
607*2139Sjp161948 			}
608*2139Sjp161948 		}
609*2139Sjp161948 #endif
6100Sstevel@tonic-gate 		break;
6110Sstevel@tonic-gate 	case DLL_THREAD_ATTACH:
6120Sstevel@tonic-gate 		break;
6130Sstevel@tonic-gate 	case DLL_THREAD_DETACH:
6140Sstevel@tonic-gate 		ERR_remove_state(0);
6150Sstevel@tonic-gate 		break;
6160Sstevel@tonic-gate 	case DLL_PROCESS_DETACH:
6170Sstevel@tonic-gate 		break;
6180Sstevel@tonic-gate 		}
6190Sstevel@tonic-gate 	return(TRUE);
6200Sstevel@tonic-gate 	}
6210Sstevel@tonic-gate #endif
6220Sstevel@tonic-gate 
623*2139Sjp161948 #if defined(_WIN32) && !defined(__CYGWIN__)
624*2139Sjp161948 #include <tchar.h>
625*2139Sjp161948 
626*2139Sjp161948 #if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
OPENSSL_isservice(void)627*2139Sjp161948 int OPENSSL_isservice(void)
628*2139Sjp161948 { HWINSTA h;
629*2139Sjp161948   DWORD len;
630*2139Sjp161948   WCHAR *name;
631*2139Sjp161948 
632*2139Sjp161948     (void)GetDesktopWindow(); /* return value is ignored */
633*2139Sjp161948 
634*2139Sjp161948     h = GetProcessWindowStation();
635*2139Sjp161948     if (h==NULL) return -1;
636*2139Sjp161948 
637*2139Sjp161948     if (GetUserObjectInformationW (h,UOI_NAME,NULL,0,&len) ||
638*2139Sjp161948 	GetLastError() != ERROR_INSUFFICIENT_BUFFER)
639*2139Sjp161948 	return -1;
640*2139Sjp161948 
641*2139Sjp161948     if (len>512) return -1;		/* paranoia */
642*2139Sjp161948     len++,len&=~1;			/* paranoia */
643*2139Sjp161948 #ifdef _MSC_VER
644*2139Sjp161948     name=(WCHAR *)_alloca(len+sizeof(WCHAR));
645*2139Sjp161948 #else
646*2139Sjp161948     name=(WCHAR *)alloca(len+sizeof(WCHAR));
647*2139Sjp161948 #endif
648*2139Sjp161948     if (!GetUserObjectInformationW (h,UOI_NAME,name,len,&len))
649*2139Sjp161948 	return -1;
650*2139Sjp161948 
651*2139Sjp161948     len++,len&=~1;			/* paranoia */
652*2139Sjp161948     name[len/sizeof(WCHAR)]=L'\0';	/* paranoia */
653*2139Sjp161948 #if 1
654*2139Sjp161948     /* This doesn't cover "interactive" services [working with real
655*2139Sjp161948      * WinSta0's] nor programs started non-interactively by Task
656*2139Sjp161948      * Scheduler [those are working with SAWinSta]. */
657*2139Sjp161948     if (wcsstr(name,L"Service-0x"))	return 1;
658*2139Sjp161948 #else
659*2139Sjp161948     /* This covers all non-interactive programs such as services. */
660*2139Sjp161948     if (!wcsstr(name,L"WinSta0"))	return 1;
661*2139Sjp161948 #endif
662*2139Sjp161948     else				return 0;
663*2139Sjp161948 }
664*2139Sjp161948 #else
OPENSSL_isservice(void)665*2139Sjp161948 int OPENSSL_isservice(void) { return 0; }
666*2139Sjp161948 #endif
667*2139Sjp161948 
OPENSSL_showfatal(const char * fmta,...)668*2139Sjp161948 void OPENSSL_showfatal (const char *fmta,...)
669*2139Sjp161948 { va_list ap;
670*2139Sjp161948   TCHAR buf[256];
671*2139Sjp161948   const TCHAR *fmt;
672*2139Sjp161948 #ifdef STD_ERROR_HANDLE	/* what a dirty trick! */
673*2139Sjp161948   HANDLE h;
674*2139Sjp161948 
675*2139Sjp161948     if ((h=GetStdHandle(STD_ERROR_HANDLE)) != NULL &&
676*2139Sjp161948 	GetFileType(h)!=FILE_TYPE_UNKNOWN)
677*2139Sjp161948     {	/* must be console application */
678*2139Sjp161948 	va_start (ap,fmta);
679*2139Sjp161948 	vfprintf (stderr,fmta,ap);
680*2139Sjp161948 	va_end (ap);
681*2139Sjp161948 	return;
682*2139Sjp161948     }
683*2139Sjp161948 #endif
684*2139Sjp161948 
685*2139Sjp161948     if (sizeof(TCHAR)==sizeof(char))
686*2139Sjp161948 	fmt=(const TCHAR *)fmta;
687*2139Sjp161948     else do
688*2139Sjp161948     { int    keepgoing;
689*2139Sjp161948       size_t len_0=strlen(fmta)+1,i;
690*2139Sjp161948       WCHAR *fmtw;
691*2139Sjp161948 
692*2139Sjp161948 #ifdef _MSC_VER
693*2139Sjp161948 	fmtw = (WCHAR *)_alloca (len_0*sizeof(WCHAR));
694*2139Sjp161948 #else
695*2139Sjp161948 	fmtw = (WCHAR *)alloca (len_0*sizeof(WCHAR));
696*2139Sjp161948 #endif
697*2139Sjp161948 	if (fmtw == NULL) { fmt=(const TCHAR *)L"no stack?"; break; }
698*2139Sjp161948 
699*2139Sjp161948 #ifndef OPENSSL_NO_MULTIBYTE
700*2139Sjp161948 	if (!MultiByteToWideChar(CP_ACP,0,fmta,len_0,fmtw,len_0))
701*2139Sjp161948 #endif
702*2139Sjp161948 	    for (i=0;i<len_0;i++) fmtw[i]=(WCHAR)fmta[i];
703*2139Sjp161948 
704*2139Sjp161948 	for (i=0;i<len_0;i++)
705*2139Sjp161948 	{   if (fmtw[i]==L'%') do
706*2139Sjp161948 	    {	keepgoing=0;
707*2139Sjp161948 		switch (fmtw[i+1])
708*2139Sjp161948 		{   case L'0': case L'1': case L'2': case L'3': case L'4':
709*2139Sjp161948 		    case L'5': case L'6': case L'7': case L'8': case L'9':
710*2139Sjp161948 		    case L'.': case L'*':
711*2139Sjp161948 		    case L'-':	i++; keepgoing=1; break;
712*2139Sjp161948 		    case L's':	fmtw[i+1]=L'S';   break;
713*2139Sjp161948 		    case L'S':	fmtw[i+1]=L's';   break;
714*2139Sjp161948 		    case L'c':	fmtw[i+1]=L'C';   break;
715*2139Sjp161948 		    case L'C':	fmtw[i+1]=L'c';   break;
716*2139Sjp161948 		}
717*2139Sjp161948 	    } while (keepgoing);
718*2139Sjp161948 	}
719*2139Sjp161948 	fmt = (const TCHAR *)fmtw;
720*2139Sjp161948     } while (0);
721*2139Sjp161948 
722*2139Sjp161948     va_start (ap,fmta);
723*2139Sjp161948     _vsntprintf (buf,sizeof(buf)/sizeof(TCHAR)-1,fmt,ap);
724*2139Sjp161948     buf [sizeof(buf)/sizeof(TCHAR)-1] = _T('\0');
725*2139Sjp161948     va_end (ap);
726*2139Sjp161948 
727*2139Sjp161948 #if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
728*2139Sjp161948     /* this -------------v--- guards NT-specific calls */
729*2139Sjp161948     if (GetVersion() < 0x80000000 && OPENSSL_isservice())
730*2139Sjp161948     {	HANDLE h = RegisterEventSource(0,_T("OPENSSL"));
731*2139Sjp161948 	const TCHAR *pmsg=buf;
732*2139Sjp161948 	ReportEvent(h,EVENTLOG_ERROR_TYPE,0,0,0,1,0,&pmsg,0);
733*2139Sjp161948 	DeregisterEventSource(h);
734*2139Sjp161948     }
735*2139Sjp161948     else
736*2139Sjp161948 #endif
737*2139Sjp161948 	MessageBox (NULL,buf,_T("OpenSSL: FATAL"),MB_OK|MB_ICONSTOP);
738*2139Sjp161948 }
739*2139Sjp161948 #else
740*2139Sjp161948 
741*2139Sjp161948 /* Solaris libsa.a used for WAN boot doesn't provide for vfprintf(). Since
742*2139Sjp161948  * OPENSSL_showfatal() is not used anywhere else then here we can safely use
743*2139Sjp161948  * the code from 0.9.7d version. */
744*2139Sjp161948 #ifndef	_BOOT
OPENSSL_showfatal(const char * fmta,...)745*2139Sjp161948 void OPENSSL_showfatal (const char *fmta,...)
746*2139Sjp161948 { va_list ap;
747*2139Sjp161948 
748*2139Sjp161948     va_start (ap,fmta);
749*2139Sjp161948     vfprintf (stderr,fmta,ap);
750*2139Sjp161948     va_end (ap);
751*2139Sjp161948 }
752*2139Sjp161948 #endif
OPENSSL_isservice(void)753*2139Sjp161948 int OPENSSL_isservice (void) { return 0; }
7540Sstevel@tonic-gate #endif
7550Sstevel@tonic-gate 
OpenSSLDie(const char * file,int line,const char * assertion)7560Sstevel@tonic-gate void OpenSSLDie(const char *file,int line,const char *assertion)
7570Sstevel@tonic-gate 	{
758*2139Sjp161948 #ifndef	_BOOT
759*2139Sjp161948 	OPENSSL_showfatal(
760*2139Sjp161948 		"%s(%d): OpenSSL internal error, assertion failed: %s\n",
761*2139Sjp161948 		file,line,assertion);
762*2139Sjp161948 #else
7630Sstevel@tonic-gate 	fprintf(stderr,
7640Sstevel@tonic-gate 		"%s(%d): OpenSSL internal error, assertion failed: %s\n",
7650Sstevel@tonic-gate 		file,line,assertion);
766*2139Sjp161948 #endif
7670Sstevel@tonic-gate 	abort();
768*2139Sjp161948 
7690Sstevel@tonic-gate 	}
770*2139Sjp161948 
OPENSSL_stderr(void)771*2139Sjp161948 void *OPENSSL_stderr(void)	{ return stderr; }
772