2 * Copyright (c) 2000-2001,2011,2014 Apple Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
19 /* crypto/cryptlib.c */
20 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
21 * All rights reserved.
23 * This package is an SSL implementation written
24 * by Eric Young (eay@cryptsoft.com).
25 * The implementation was written so as to conform with Netscapes SSL.
27 * This library is free for commercial and non-commercial use as long as
28 * the following conditions are aheared to. The following conditions
29 * apply to all code found in this distribution, be it the RC4, RSA,
30 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
31 * included with this distribution is covered by the same copyright terms
32 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
34 * Copyright remains Eric Young's, and as such any Copyright notices in
35 * the code are not to be removed.
36 * If this package is used in a product, Eric Young should be given attribution
37 * as the author of the parts of the library used.
38 * This can be in the form of a textual message at program startup or
39 * in documentation (online or textual) provided with the package.
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
44 * 1. Redistributions of source code must retain the copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. All advertising materials mentioning features or use of this software
50 * must display the following acknowledgement:
51 * "This product includes cryptographic software written by
52 * Eric Young (eay@cryptsoft.com)"
53 * The word 'cryptographic' can be left out if the rouines from the library
54 * being used are not cryptographic related :-).
55 * 4. If you include any Windows specific code (or a derivative thereof) from
56 * the apps directory (application code) you must include an acknowledgement:
57 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
59 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * The licence and distribution terms for any publically available version or
72 * derivative of this code cannot be changed. i.e. this code cannot simply be
73 * copied and put under another distribution licence
74 * [including the GNU Public Licence.]
80 #include <openssl/crypto_legacy.h>
82 #if defined(WIN32) || defined(WIN16)
83 static double SSLeay_MSVC5_hack
=0.0; /* and for VC1.5 */
86 /* real #defines in crypto_legacy.h, keep these upto date */
87 #if defined(LOCK_DEBUG)
88 static const char* const lock_names
[CRYPTO_NUM_LOCKS
] =
116 #if CRYPTO_NUM_LOCKS != 26
117 # error "Inconsistency between crypto_legacy.h and cryptlib.c"
120 #endif /*LOCK_DEBUG*/
122 static STACK
*app_locks
=NULL
;
124 #if CRYPTO_CALLBACK_ENABLE
125 static void (MS_FAR
*locking_callback
)(int mode
,int type
,
126 const char *file
,int line
)=NULL
;
127 static int (MS_FAR
*add_lock_callback
)(int *pointer
,int amount
,
128 int type
,const char *file
,int line
)=NULL
;
129 static unsigned long (MS_FAR
*id_callback
)(void)=NULL
;
130 #endif /* CRYPTO_CALLBACK_ENABLE */
132 int CRYPTO_get_new_lockid(char *name
)
137 /* A hack to make Visual C++ 5.0 work correctly when linking as
138 * a DLL using /MT. Without this, the application cannot use
139 * and floating point printf's.
140 * It also seems to be needed for Visual C 1.5 (win16) */
141 #if defined(WIN32) || defined(WIN16)
142 SSLeay_MSVC5_hack
=(double)name
[0]*(double)name
[1];
145 if ((app_locks
== NULL
) && ((app_locks
=sk_new_null()) == NULL
))
147 CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_LOCKID
,ERR_R_MALLOC_FAILURE
);
150 if ((str
=BUF_strdup(name
)) == NULL
)
152 i
=sk_push(app_locks
,str
);
156 i
+=CRYPTO_NUM_LOCKS
; /* gap of one :-) */
160 int CRYPTO_num_locks(void)
162 return CRYPTO_NUM_LOCKS
;
165 void (*CRYPTO_get_locking_callback(void))(int mode
,int type
,const char *file
,
168 #if CRYPTO_CALLBACK_ENABLE
169 return(locking_callback
);
175 int (*CRYPTO_get_add_lock_callback(void))(int *num
,int mount
,int type
,
176 const char *file
,int line
)
178 #if CRYPTO_CALLBACK_ENABLE
179 return(add_lock_callback
);
185 void CRYPTO_set_locking_callback(void (*func
)(int mode
,int type
,
186 const char *file
,int line
))
188 #if CRYPTO_CALLBACK_ENABLE
189 locking_callback
=func
;
193 void CRYPTO_set_add_lock_callback(int (*func
)(int *num
,int mount
,int type
,
194 const char *file
,int line
))
196 #if CRYPTO_CALLBACK_ENABLE
197 add_lock_callback
=func
;
201 unsigned long (*CRYPTO_get_id_callback(void))(void)
203 #if CRYPTO_CALLBACK_ENABLE
210 void CRYPTO_set_id_callback(unsigned long (*func
)(void))
212 #if CRYPTO_CALLBACK_ENABLE
217 unsigned long CRYPTO_thread_id(void)
221 #if CRYPTO_CALLBACK_ENABLE
222 if (id_callback
== NULL
)
225 ret
=(unsigned long)GetCurrentTask();
227 ret
=(unsigned long)GetCurrentThreadId();
228 #elif defined(GETPID_IS_MEANINGLESS)
231 ret
=(unsigned long)getpid();
236 #endif /* CRYPTO_CALLBACK_ENABLE */
240 void CRYPTO_lock(int mode
, int type
, const char *file
, int line
)
244 char *rw_text
,*operation_text
;
246 if (mode
& CRYPTO_LOCK
)
247 operation_text
="lock ";
248 else if (mode
& CRYPTO_UNLOCK
)
249 operation_text
="unlock";
251 operation_text
="ERROR ";
253 if (mode
& CRYPTO_READ
)
255 else if (mode
& CRYPTO_WRITE
)
260 fprintf(stderr
,"lock:%08lx:(%s)%s %-18s %s:%d\n",
261 CRYPTO_thread_id(), rw_text
, operation_text
,
262 CRYPTO_get_lock_name(type
), file
, line
);
265 #if CRYPTO_CALLBACK_ENABLE
266 if (locking_callback
!= NULL
)
267 locking_callback(mode
,type
,file
,line
);
271 int CRYPTO_add_lock(int *pointer
, int amount
, int type
, const char *file
,
275 #if CRYPTO_CALLBACK_ENABLE
276 if (add_lock_callback
!= NULL
)
279 int before
= *pointer
;
282 ret
=add_lock_callback(pointer
,amount
,type
,file
,line
);
284 fprintf(stderr
,"ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\n",
287 CRYPTO_get_lock_name(type
),
293 #endif /* CRYPTO_CALLBACK_ENABLE */
295 CRYPTO_lock(CRYPTO_LOCK
|CRYPTO_WRITE
,type
,file
,line
);
297 ret
= *pointer
+amount
;
299 fprintf(stderr
,"ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\n",
302 CRYPTO_get_lock_name(type
),
306 CRYPTO_lock(CRYPTO_UNLOCK
|CRYPTO_WRITE
,type
,file
,line
);
312 const char *CRYPTO_get_lock_name(int type
)
316 else if (type
< CRYPTO_NUM_LOCKS
)
317 return(lock_names
[type
]);
318 else if (type
-CRYPTO_NUM_LOCKS
>= sk_num(app_locks
))
321 return(sk_value(app_locks
,type
-CRYPTO_NUM_LOCKS
));
323 #endif /*LOCK_DEBUG*/
328 /* All we really need to do is remove the 'error' state when a thread
331 BOOL WINAPI
DLLEntryPoint(HINSTANCE hinstDLL
, DWORD fdwReason
,
336 case DLL_PROCESS_ATTACH
:
338 case DLL_THREAD_ATTACH
:
340 case DLL_THREAD_DETACH
:
343 case DLL_PROCESS_DETACH
: