12cf0733ec9a15a51e9b2e1505a612a175993bf7
[openssl.git] / crypto / cryptlib.c
1 /* crypto/cryptlib.c */
2 /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  * 
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  * 
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from 
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  * 
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * 
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 #include <stdio.h>
60 #include <string.h>
61 #include "cryptlib.h"
62 #include "crypto.h"
63 #include "date.h"
64
65 /* real #defines in crypto.h, keep these upto date */
66 static char* lock_names[CRYPTO_NUM_LOCKS] =
67         {
68         "<<ERROR>>",
69         "err",
70         "err_hash",
71         "x509",
72         "x509_info",
73         "x509_pkey",
74         "x509_crl",
75         "x509_req",
76         "dsa",
77         "rsa",
78         "evp_pkey",
79         "x509_store",
80         "ssl_ctx",
81         "ssl_cert",
82         "ssl_session",
83         "ssl",
84         "rand",
85         "debug_malloc",
86         "bio_gethostbyname",
87         "BIO",
88         };
89
90 #ifndef NOPROTO
91 static void (MS_FAR *locking_callback)(int mode,int type,
92         char *file,int line)=NULL;
93 static int (MS_FAR *add_lock_callback)(int *pointer,int amount,
94         int type,char *file,int line)=NULL;
95 static unsigned long (MS_FAR *id_callback)(void)=NULL;
96 #else
97 static void (MS_FAR *locking_callback)()=NULL;
98 static int (MS_FAR *add_lock_callback)()=NULL;
99 static unsigned long (MS_FAR *id_callback)()=NULL;
100 #endif
101
102 void (*CRYPTO_get_locking_callback(P_V))(P_I_I_P_I)
103         {
104         return(locking_callback);
105         }
106
107 int (*CRYPTO_get_add_lock_callback(P_V))(P_IP_I_I_P_I)
108         {
109         return(add_lock_callback);
110         }
111
112 void CRYPTO_set_locking_callback(func)
113 void (*func)(P_I_I_P_I);
114         {
115         locking_callback=func;
116         }
117
118 void CRYPTO_set_add_lock_callback(func)
119 int (*func)(P_IP_I_I_P_I);
120         {
121         add_lock_callback=func;
122         }
123
124 unsigned long (*CRYPTO_get_id_callback(P_V))(P_V)
125         {
126         return(id_callback);
127         }
128
129 void CRYPTO_set_id_callback(func)
130 unsigned long (*func)(P_V);
131         {
132         id_callback=func;
133         }
134
135 unsigned long CRYPTO_thread_id()
136         {
137         unsigned long ret=0;
138
139         if (id_callback == NULL)
140                 {
141 #ifdef WIN16
142                 ret=(unsigned long)GetCurrentTask();
143 #elif defined(WIN32)
144                 ret=(unsigned long)GetCurrentThreadId();
145 #elif defined(MSDOS)
146                 ret=1L;
147 #else
148                 ret=(unsigned long)getpid();
149 #endif
150                 }
151         else
152                 ret=id_callback();
153         return(ret);
154         }
155
156 void CRYPTO_lock(mode,type,file,line)
157 int mode;
158 int type;
159 char *file;
160 int line;
161         {
162 #ifdef LOCK_DEBUG
163                 {
164                 char *rw_text,*operation_text;
165
166                 if (mode & CRYPTO_LOCK)
167                         operation_text="lock  ";
168                 else if (mode & CRYPTO_UNLOCK)
169                         operation_text="unlock";
170                 else
171                         operation_text="ERROR ";
172
173                 if (mode & CRYPTO_READ)
174                         rw_text="r";
175                 else if (mode & CRYPTO_WRITE)
176                         rw_text="w";
177                 else
178                         rw_text="ERROR";
179
180                 fprintf(stderr,"lock:%08lx:(%s)%s %-18s %s:%d\n",
181                         CRYPTO_thread_id(), rw_text, operation_text,
182                         CRYPTO_get_lock_name(type), file, line);
183                 }
184 #endif
185         if (locking_callback != NULL)
186                 locking_callback(mode,type,file,line);
187         }
188
189 int CRYPTO_add_lock(pointer,amount,type,file,line)
190 int *pointer;
191 int amount;
192 int type;
193 char *file;
194 int line;
195         {
196         int ret;
197
198         if (add_lock_callback != NULL)
199                 {
200 #ifdef LOCK_DEBUG
201                 int before= *pointer;
202 #endif
203
204                 ret=add_lock_callback(pointer,amount,type,file,line);
205 #ifdef LOCK_DEBUG
206                 fprintf(stderr,"ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\n",
207                         CRYPTO_thread_id(),
208                         before,amount,ret,
209                         CRYPTO_get_lock_name(type),
210                         file,line);
211 #endif
212                 *pointer=ret;
213                 }
214         else
215                 {
216                 CRYPTO_lock(CRYPTO_LOCK|CRYPTO_WRITE,type,file,line);
217
218                 ret= *pointer+amount;
219 #ifdef LOCK_DEBUG
220                 fprintf(stderr,"ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\n",
221                         CRYPTO_thread_id(),
222                         *pointer,amount,ret,
223                         CRYPTO_get_lock_name(type),
224                         file,line);
225 #endif
226                 *pointer=ret;
227                 CRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_WRITE,type,file,line);
228                 }
229         return(ret);
230         }
231
232 char *CRYPTO_get_lock_name(type)
233 int type;
234         {
235         if ((type < 0) || (type >= CRYPTO_NUM_LOCKS))
236                 return("ERROR");
237         return(lock_names[type]);
238         }
239
240 #ifdef _DLL
241 #ifdef WIN32
242
243 /* All we really need to do is remove the 'error' state when a thread
244  * detaches */
245
246 BOOL WINAPI DLLEntryPoint(hinstDLL,fdwReason,lpvReserved)
247 HINSTANCE hinstDLL;
248 DWORD fdwReason;
249 LPVOID lpvReserved;
250         {
251         switch(fdwReason)
252                 {
253         case DLL_PROCESS_ATTACH:
254                 break;
255         case DLL_THREAD_ATTACH:
256                 break;
257         case DLL_THREAD_DETACH:
258                 ERR_remove_state(0);
259                 break;
260         case DLL_PROCESS_DETACH:
261                 break;
262                 }
263         return(TRUE);
264         }
265 #endif
266
267 #endif