2
0

cryptlib.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /* crypto/cryptlib.c */
  2. /* Copyright (C) 1995-1998 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. #include <stdio.h>
  59. #include <string.h>
  60. #include "cryptlib.h"
  61. #include <openssl/crypto.h>
  62. #include <openssl/safestack.h>
  63. #if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16)
  64. static double SSLeay_MSVC5_hack=0.0; /* and for VC1.5 */
  65. #endif
  66. DECLARE_STACK_OF(CRYPTO_dynlock)
  67. IMPLEMENT_STACK_OF(CRYPTO_dynlock)
  68. /* real #defines in crypto.h, keep these upto date */
  69. static const char* lock_names[CRYPTO_NUM_LOCKS] =
  70. {
  71. "<<ERROR>>",
  72. "err",
  73. "ex_data",
  74. "x509",
  75. "x509_info",
  76. "x509_pkey",
  77. "x509_crl",
  78. "x509_req",
  79. "dsa",
  80. "rsa",
  81. "evp_pkey",
  82. "x509_store",
  83. "ssl_ctx",
  84. "ssl_cert",
  85. "ssl_session",
  86. "ssl_sess_cert",
  87. "ssl",
  88. "rand",
  89. "rand2",
  90. "debug_malloc",
  91. "BIO",
  92. "gethostbyname",
  93. "getservbyname",
  94. "readdir",
  95. "RSA_blinding",
  96. "dh",
  97. "debug_malloc2",
  98. "dso",
  99. "dynlock",
  100. "engine",
  101. "ui",
  102. #if CRYPTO_NUM_LOCKS != 31
  103. # error "Inconsistency between crypto.h and cryptlib.c"
  104. #endif
  105. };
  106. /* This is for applications to allocate new type names in the non-dynamic
  107. array of lock names. These are numbered with positive numbers. */
  108. static STACK *app_locks=NULL;
  109. /* For applications that want a more dynamic way of handling threads, the
  110. following stack is used. These are externally numbered with negative
  111. numbers. */
  112. static STACK_OF(CRYPTO_dynlock) *dyn_locks=NULL;
  113. static void (MS_FAR *locking_callback)(int mode,int type,
  114. const char *file,int line)=NULL;
  115. static int (MS_FAR *add_lock_callback)(int *pointer,int amount,
  116. int type,const char *file,int line)=NULL;
  117. static unsigned long (MS_FAR *id_callback)(void)=NULL;
  118. static struct CRYPTO_dynlock_value *(MS_FAR *dynlock_create_callback)
  119. (const char *file,int line)=NULL;
  120. static void (MS_FAR *dynlock_lock_callback)(int mode,
  121. struct CRYPTO_dynlock_value *l, const char *file,int line)=NULL;
  122. static void (MS_FAR *dynlock_destroy_callback)(struct CRYPTO_dynlock_value *l,
  123. const char *file,int line)=NULL;
  124. int CRYPTO_get_new_lockid(char *name)
  125. {
  126. char *str;
  127. int i;
  128. #if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16)
  129. /* A hack to make Visual C++ 5.0 work correctly when linking as
  130. * a DLL using /MT. Without this, the application cannot use
  131. * and floating point printf's.
  132. * It also seems to be needed for Visual C 1.5 (win16) */
  133. SSLeay_MSVC5_hack=(double)name[0]*(double)name[1];
  134. #endif
  135. if ((app_locks == NULL) && ((app_locks=sk_new_null()) == NULL))
  136. {
  137. CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_LOCKID,ERR_R_MALLOC_FAILURE);
  138. return(0);
  139. }
  140. if ((str=BUF_strdup(name)) == NULL)
  141. {
  142. CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_LOCKID,ERR_R_MALLOC_FAILURE);
  143. return(0);
  144. }
  145. i=sk_push(app_locks,str);
  146. if (!i)
  147. OPENSSL_free(str);
  148. else
  149. i+=CRYPTO_NUM_LOCKS; /* gap of one :-) */
  150. return(i);
  151. }
  152. int CRYPTO_num_locks(void)
  153. {
  154. return CRYPTO_NUM_LOCKS;
  155. }
  156. int CRYPTO_get_new_dynlockid(void)
  157. {
  158. int i = 0;
  159. CRYPTO_dynlock *pointer = NULL;
  160. if (dynlock_create_callback == NULL)
  161. {
  162. CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID,CRYPTO_R_NO_DYNLOCK_CREATE_CALLBACK);
  163. return(0);
  164. }
  165. CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
  166. if ((dyn_locks == NULL)
  167. && ((dyn_locks=sk_CRYPTO_dynlock_new_null()) == NULL))
  168. {
  169. CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
  170. CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID,ERR_R_MALLOC_FAILURE);
  171. return(0);
  172. }
  173. CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
  174. pointer = (CRYPTO_dynlock *)OPENSSL_malloc(sizeof(CRYPTO_dynlock));
  175. if (pointer == NULL)
  176. {
  177. CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID,ERR_R_MALLOC_FAILURE);
  178. return(0);
  179. }
  180. pointer->references = 1;
  181. pointer->data = dynlock_create_callback(__FILE__,__LINE__);
  182. if (pointer->data == NULL)
  183. {
  184. OPENSSL_free(pointer);
  185. CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID,ERR_R_MALLOC_FAILURE);
  186. return(0);
  187. }
  188. CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
  189. /* First, try to find an existing empty slot */
  190. i=sk_CRYPTO_dynlock_find(dyn_locks,NULL);
  191. /* If there was none, push, thereby creating a new one */
  192. if (i == -1)
  193. i=sk_CRYPTO_dynlock_push(dyn_locks,pointer);
  194. CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
  195. if (!i)
  196. {
  197. dynlock_destroy_callback(pointer->data,__FILE__,__LINE__);
  198. OPENSSL_free(pointer);
  199. }
  200. else
  201. i += 1; /* to avoid 0 */
  202. return -i;
  203. }
  204. void CRYPTO_destroy_dynlockid(int i)
  205. {
  206. CRYPTO_dynlock *pointer = NULL;
  207. if (i)
  208. i = -i-1;
  209. if (dynlock_destroy_callback == NULL)
  210. return;
  211. CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
  212. if (dyn_locks == NULL || i >= sk_CRYPTO_dynlock_num(dyn_locks))
  213. return;
  214. pointer = sk_CRYPTO_dynlock_value(dyn_locks, i);
  215. if (pointer != NULL)
  216. {
  217. --pointer->references;
  218. #ifdef REF_CHECK
  219. if (pointer->references < 0)
  220. {
  221. fprintf(stderr,"CRYPTO_destroy_dynlockid, bad reference count\n");
  222. abort();
  223. }
  224. else
  225. #endif
  226. if (pointer->references <= 0)
  227. {
  228. sk_CRYPTO_dynlock_set(dyn_locks, i, NULL);
  229. }
  230. else
  231. pointer = NULL;
  232. }
  233. CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
  234. if (pointer)
  235. {
  236. dynlock_destroy_callback(pointer->data,__FILE__,__LINE__);
  237. OPENSSL_free(pointer);
  238. }
  239. }
  240. struct CRYPTO_dynlock_value *CRYPTO_get_dynlock_value(int i)
  241. {
  242. CRYPTO_dynlock *pointer = NULL;
  243. if (i)
  244. i = -i-1;
  245. CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
  246. if (dyn_locks != NULL && i < sk_CRYPTO_dynlock_num(dyn_locks))
  247. pointer = sk_CRYPTO_dynlock_value(dyn_locks, i);
  248. if (pointer)
  249. pointer->references++;
  250. CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
  251. if (pointer)
  252. return pointer->data;
  253. return NULL;
  254. }
  255. struct CRYPTO_dynlock_value *(*CRYPTO_get_dynlock_create_callback(void))
  256. (const char *file,int line)
  257. {
  258. return(dynlock_create_callback);
  259. }
  260. void (*CRYPTO_get_dynlock_lock_callback(void))(int mode,
  261. struct CRYPTO_dynlock_value *l, const char *file,int line)
  262. {
  263. return(dynlock_lock_callback);
  264. }
  265. void (*CRYPTO_get_dynlock_destroy_callback(void))
  266. (struct CRYPTO_dynlock_value *l, const char *file,int line)
  267. {
  268. return(dynlock_destroy_callback);
  269. }
  270. void CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value *(*func)
  271. (const char *file, int line))
  272. {
  273. dynlock_create_callback=func;
  274. }
  275. void CRYPTO_set_dynlock_lock_callback(void (*func)(int mode,
  276. struct CRYPTO_dynlock_value *l, const char *file, int line))
  277. {
  278. dynlock_lock_callback=func;
  279. }
  280. void CRYPTO_set_dynlock_destroy_callback(void (*func)
  281. (struct CRYPTO_dynlock_value *l, const char *file, int line))
  282. {
  283. dynlock_destroy_callback=func;
  284. }
  285. void (*CRYPTO_get_locking_callback(void))(int mode,int type,const char *file,
  286. int line)
  287. {
  288. return(locking_callback);
  289. }
  290. int (*CRYPTO_get_add_lock_callback(void))(int *num,int mount,int type,
  291. const char *file,int line)
  292. {
  293. return(add_lock_callback);
  294. }
  295. void CRYPTO_set_locking_callback(void (*func)(int mode,int type,
  296. const char *file,int line))
  297. {
  298. locking_callback=func;
  299. }
  300. void CRYPTO_set_add_lock_callback(int (*func)(int *num,int mount,int type,
  301. const char *file,int line))
  302. {
  303. add_lock_callback=func;
  304. }
  305. unsigned long (*CRYPTO_get_id_callback(void))(void)
  306. {
  307. return(id_callback);
  308. }
  309. void CRYPTO_set_id_callback(unsigned long (*func)(void))
  310. {
  311. id_callback=func;
  312. }
  313. unsigned long CRYPTO_thread_id(void)
  314. {
  315. unsigned long ret=0;
  316. if (id_callback == NULL)
  317. {
  318. #ifdef OPENSSL_SYS_WIN16
  319. ret=(unsigned long)GetCurrentTask();
  320. #elif defined(OPENSSL_SYS_WIN32)
  321. ret=(unsigned long)GetCurrentThreadId();
  322. #elif defined(GETPID_IS_MEANINGLESS)
  323. ret=1L;
  324. #else
  325. ret=(unsigned long)getpid();
  326. #endif
  327. }
  328. else
  329. ret=id_callback();
  330. return(ret);
  331. }
  332. void CRYPTO_lock(int mode, int type, const char *file, int line)
  333. {
  334. #ifdef LOCK_DEBUG
  335. {
  336. char *rw_text,*operation_text;
  337. if (mode & CRYPTO_LOCK)
  338. operation_text="lock ";
  339. else if (mode & CRYPTO_UNLOCK)
  340. operation_text="unlock";
  341. else
  342. operation_text="ERROR ";
  343. if (mode & CRYPTO_READ)
  344. rw_text="r";
  345. else if (mode & CRYPTO_WRITE)
  346. rw_text="w";
  347. else
  348. rw_text="ERROR";
  349. fprintf(stderr,"lock:%08lx:(%s)%s %-18s %s:%d\n",
  350. CRYPTO_thread_id(), rw_text, operation_text,
  351. CRYPTO_get_lock_name(type), file, line);
  352. }
  353. #endif
  354. if (type < 0)
  355. {
  356. struct CRYPTO_dynlock_value *pointer
  357. = CRYPTO_get_dynlock_value(type);
  358. if (pointer && dynlock_lock_callback)
  359. {
  360. dynlock_lock_callback(mode, pointer, file, line);
  361. }
  362. CRYPTO_destroy_dynlockid(type);
  363. }
  364. else
  365. if (locking_callback != NULL)
  366. locking_callback(mode,type,file,line);
  367. }
  368. int CRYPTO_add_lock(int *pointer, int amount, int type, const char *file,
  369. int line)
  370. {
  371. int ret = 0;
  372. if (add_lock_callback != NULL)
  373. {
  374. #ifdef LOCK_DEBUG
  375. int before= *pointer;
  376. #endif
  377. ret=add_lock_callback(pointer,amount,type,file,line);
  378. #ifdef LOCK_DEBUG
  379. fprintf(stderr,"ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\n",
  380. CRYPTO_thread_id(),
  381. before,amount,ret,
  382. CRYPTO_get_lock_name(type),
  383. file,line);
  384. #endif
  385. }
  386. else
  387. {
  388. CRYPTO_lock(CRYPTO_LOCK|CRYPTO_WRITE,type,file,line);
  389. ret= *pointer+amount;
  390. #ifdef LOCK_DEBUG
  391. fprintf(stderr,"ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\n",
  392. CRYPTO_thread_id(),
  393. *pointer,amount,ret,
  394. CRYPTO_get_lock_name(type),
  395. file,line);
  396. #endif
  397. *pointer=ret;
  398. CRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_WRITE,type,file,line);
  399. }
  400. return(ret);
  401. }
  402. const char *CRYPTO_get_lock_name(int type)
  403. {
  404. if (type < 0)
  405. return("dynamic");
  406. else if (type < CRYPTO_NUM_LOCKS)
  407. return(lock_names[type]);
  408. else if (type-CRYPTO_NUM_LOCKS >= sk_num(app_locks))
  409. return("ERROR");
  410. else
  411. return(sk_value(app_locks,type-CRYPTO_NUM_LOCKS));
  412. }
  413. #ifdef _DLL
  414. #ifdef OPENSSL_SYS_WIN32
  415. /* All we really need to do is remove the 'error' state when a thread
  416. * detaches */
  417. BOOL WINAPI DLLEntryPoint(HINSTANCE hinstDLL, DWORD fdwReason,
  418. LPVOID lpvReserved)
  419. {
  420. switch(fdwReason)
  421. {
  422. case DLL_PROCESS_ATTACH:
  423. break;
  424. case DLL_THREAD_ATTACH:
  425. break;
  426. case DLL_THREAD_DETACH:
  427. ERR_remove_state(0);
  428. break;
  429. case DLL_PROCESS_DETACH:
  430. break;
  431. }
  432. return(TRUE);
  433. }
  434. #endif
  435. #endif