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. "err_hash",
  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. "debug_malloc",
  90. "BIO",
  91. "gethostbyname",
  92. "getservbyname",
  93. "readdir",
  94. "RSA_blinding",
  95. "dh",
  96. "debug_malloc2",
  97. "dso",
  98. "dynlock",
  99. "engine",
  100. #if CRYPTO_NUM_LOCKS != 29
  101. # error "Inconsistency between crypto.h and cryptlib.c"
  102. #endif
  103. };
  104. /* This is for applications to allocate new type names in the non-dynamic
  105. array of lock names. These are numbered with positive numbers. */
  106. static STACK *app_locks=NULL;
  107. /* For applications that want a more dynamic way of handling threads, the
  108. following stack is used. These are externally numbered with negative
  109. numbers. */
  110. static STACK_OF(CRYPTO_dynlock) *dyn_locks=NULL;
  111. static void (MS_FAR *locking_callback)(int mode,int type,
  112. const char *file,int line)=NULL;
  113. static int (MS_FAR *add_lock_callback)(int *pointer,int amount,
  114. int type,const char *file,int line)=NULL;
  115. static unsigned long (MS_FAR *id_callback)(void)=NULL;
  116. static struct CRYPTO_dynlock_value *(MS_FAR *dynlock_create_callback)
  117. (const char *file,int line)=NULL;
  118. static void (MS_FAR *dynlock_lock_callback)(int mode,
  119. struct CRYPTO_dynlock_value *l, const char *file,int line)=NULL;
  120. static void (MS_FAR *dynlock_destroy_callback)(struct CRYPTO_dynlock_value *l,
  121. const char *file,int line)=NULL;
  122. int CRYPTO_get_new_lockid(char *name)
  123. {
  124. char *str;
  125. int i;
  126. #if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16)
  127. /* A hack to make Visual C++ 5.0 work correctly when linking as
  128. * a DLL using /MT. Without this, the application cannot use
  129. * and floating point printf's.
  130. * It also seems to be needed for Visual C 1.5 (win16) */
  131. SSLeay_MSVC5_hack=(double)name[0]*(double)name[1];
  132. #endif
  133. if ((app_locks == NULL) && ((app_locks=sk_new_null()) == NULL))
  134. {
  135. CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_LOCKID,ERR_R_MALLOC_FAILURE);
  136. return(0);
  137. }
  138. if ((str=BUF_strdup(name)) == NULL)
  139. {
  140. CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_LOCKID,ERR_R_MALLOC_FAILURE);
  141. return(0);
  142. }
  143. i=sk_push(app_locks,str);
  144. if (!i)
  145. OPENSSL_free(str);
  146. else
  147. i+=CRYPTO_NUM_LOCKS; /* gap of one :-) */
  148. return(i);
  149. }
  150. int CRYPTO_num_locks(void)
  151. {
  152. return CRYPTO_NUM_LOCKS;
  153. }
  154. int CRYPTO_get_new_dynlockid(void)
  155. {
  156. int i = 0;
  157. CRYPTO_dynlock *pointer = NULL;
  158. if (dynlock_create_callback == NULL)
  159. {
  160. CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID,CRYPTO_R_NO_DYNLOCK_CREATE_CALLBACK);
  161. return(0);
  162. }
  163. CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
  164. if ((dyn_locks == NULL)
  165. && ((dyn_locks=sk_CRYPTO_dynlock_new_null()) == NULL))
  166. {
  167. CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
  168. CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID,ERR_R_MALLOC_FAILURE);
  169. return(0);
  170. }
  171. CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
  172. pointer = (CRYPTO_dynlock *)OPENSSL_malloc(sizeof(CRYPTO_dynlock));
  173. if (pointer == NULL)
  174. {
  175. CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID,ERR_R_MALLOC_FAILURE);
  176. return(0);
  177. }
  178. pointer->references = 1;
  179. pointer->data = dynlock_create_callback(__FILE__,__LINE__);
  180. if (pointer->data == NULL)
  181. {
  182. OPENSSL_free(pointer);
  183. CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID,ERR_R_MALLOC_FAILURE);
  184. return(0);
  185. }
  186. CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
  187. /* First, try to find an existing empty slot */
  188. i=sk_CRYPTO_dynlock_find(dyn_locks,NULL);
  189. /* If there was none, push, thereby creating a new one */
  190. if (i == -1)
  191. i=sk_CRYPTO_dynlock_push(dyn_locks,pointer);
  192. CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
  193. if (!i)
  194. {
  195. dynlock_destroy_callback(pointer->data,__FILE__,__LINE__);
  196. OPENSSL_free(pointer);
  197. }
  198. else
  199. i += 1; /* to avoid 0 */
  200. return -i;
  201. }
  202. void CRYPTO_destroy_dynlockid(int i)
  203. {
  204. CRYPTO_dynlock *pointer = NULL;
  205. if (i)
  206. i = -i-1;
  207. if (dynlock_destroy_callback == NULL)
  208. return;
  209. CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
  210. if (dyn_locks == NULL || i >= sk_CRYPTO_dynlock_num(dyn_locks))
  211. return;
  212. pointer = sk_CRYPTO_dynlock_value(dyn_locks, i);
  213. if (pointer != NULL)
  214. {
  215. --pointer->references;
  216. #ifdef REF_CHECK
  217. if (pointer->references < 0)
  218. {
  219. fprintf(stderr,"CRYPTO_destroy_dynlockid, bad reference count\n");
  220. abort();
  221. }
  222. else
  223. #endif
  224. if (--(pointer->references) <= 0)
  225. {
  226. sk_CRYPTO_dynlock_set(dyn_locks, i, NULL);
  227. }
  228. else
  229. pointer = NULL;
  230. }
  231. CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
  232. if (pointer)
  233. {
  234. dynlock_destroy_callback(pointer->data,__FILE__,__LINE__);
  235. OPENSSL_free(pointer);
  236. }
  237. }
  238. struct CRYPTO_dynlock_value *CRYPTO_get_dynlock_value(int i)
  239. {
  240. CRYPTO_dynlock *pointer = NULL;
  241. if (i)
  242. i = -i-1;
  243. CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
  244. if (dyn_locks != NULL && i < sk_CRYPTO_dynlock_num(dyn_locks))
  245. pointer = sk_CRYPTO_dynlock_value(dyn_locks, i);
  246. if (pointer)
  247. pointer->references++;
  248. CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
  249. if (pointer)
  250. return pointer->data;
  251. return NULL;
  252. }
  253. struct CRYPTO_dynlock_value *(*CRYPTO_get_dynlock_create_callback(void))
  254. (const char *file,int line)
  255. {
  256. return(dynlock_create_callback);
  257. }
  258. void (*CRYPTO_get_dynlock_lock_callback(void))(int mode,
  259. struct CRYPTO_dynlock_value *l, const char *file,int line)
  260. {
  261. return(dynlock_lock_callback);
  262. }
  263. void (*CRYPTO_get_dynlock_destroy_callback(void))
  264. (struct CRYPTO_dynlock_value *l, const char *file,int line)
  265. {
  266. return(dynlock_destroy_callback);
  267. }
  268. void CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value *(*func)
  269. (const char *file, int line))
  270. {
  271. dynlock_create_callback=func;
  272. }
  273. void CRYPTO_set_dynlock_lock_callback(void (*func)(int mode,
  274. struct CRYPTO_dynlock_value *l, const char *file, int line))
  275. {
  276. dynlock_lock_callback=func;
  277. }
  278. void CRYPTO_set_dynlock_destroy_callback(void (*func)
  279. (struct CRYPTO_dynlock_value *l, const char *file, int line))
  280. {
  281. dynlock_destroy_callback=func;
  282. }
  283. void (*CRYPTO_get_locking_callback(void))(int mode,int type,const char *file,
  284. int line)
  285. {
  286. return(locking_callback);
  287. }
  288. int (*CRYPTO_get_add_lock_callback(void))(int *num,int mount,int type,
  289. const char *file,int line)
  290. {
  291. return(add_lock_callback);
  292. }
  293. void CRYPTO_set_locking_callback(void (*func)(int mode,int type,
  294. const char *file,int line))
  295. {
  296. locking_callback=func;
  297. }
  298. void CRYPTO_set_add_lock_callback(int (*func)(int *num,int mount,int type,
  299. const char *file,int line))
  300. {
  301. add_lock_callback=func;
  302. }
  303. unsigned long (*CRYPTO_get_id_callback(void))(void)
  304. {
  305. return(id_callback);
  306. }
  307. void CRYPTO_set_id_callback(unsigned long (*func)(void))
  308. {
  309. id_callback=func;
  310. }
  311. unsigned long CRYPTO_thread_id(void)
  312. {
  313. unsigned long ret=0;
  314. if (id_callback == NULL)
  315. {
  316. #ifdef OPENSSL_SYS_WIN16
  317. ret=(unsigned long)GetCurrentTask();
  318. #elif defined(OPENSSL_SYS_WIN32)
  319. ret=(unsigned long)GetCurrentThreadId();
  320. #elif defined(GETPID_IS_MEANINGLESS)
  321. ret=1L;
  322. #else
  323. ret=(unsigned long)getpid();
  324. #endif
  325. }
  326. else
  327. ret=id_callback();
  328. return(ret);
  329. }
  330. void CRYPTO_lock(int mode, int type, const char *file, int line)
  331. {
  332. #ifdef LOCK_DEBUG
  333. {
  334. char *rw_text,*operation_text;
  335. if (mode & CRYPTO_LOCK)
  336. operation_text="lock ";
  337. else if (mode & CRYPTO_UNLOCK)
  338. operation_text="unlock";
  339. else
  340. operation_text="ERROR ";
  341. if (mode & CRYPTO_READ)
  342. rw_text="r";
  343. else if (mode & CRYPTO_WRITE)
  344. rw_text="w";
  345. else
  346. rw_text="ERROR";
  347. fprintf(stderr,"lock:%08lx:(%s)%s %-18s %s:%d\n",
  348. CRYPTO_thread_id(), rw_text, operation_text,
  349. CRYPTO_get_lock_name(type), file, line);
  350. }
  351. #endif
  352. if (type < 0)
  353. {
  354. int i = -type - 1;
  355. struct CRYPTO_dynlock_value *pointer
  356. = CRYPTO_get_dynlock_value(i);
  357. if (pointer)
  358. {
  359. dynlock_lock_callback(mode, pointer, file, line);
  360. }
  361. CRYPTO_destroy_dynlockid(i);
  362. }
  363. else
  364. if (locking_callback != NULL)
  365. locking_callback(mode,type,file,line);
  366. }
  367. int CRYPTO_add_lock(int *pointer, int amount, int type, const char *file,
  368. int line)
  369. {
  370. int ret = 0;
  371. if (add_lock_callback != NULL)
  372. {
  373. #ifdef LOCK_DEBUG
  374. int before= *pointer;
  375. #endif
  376. ret=add_lock_callback(pointer,amount,type,file,line);
  377. #ifdef LOCK_DEBUG
  378. fprintf(stderr,"ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\n",
  379. CRYPTO_thread_id(),
  380. before,amount,ret,
  381. CRYPTO_get_lock_name(type),
  382. file,line);
  383. #endif
  384. *pointer=ret;
  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