cryptlib.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. /* crypto/cryptlib.c */
  2. /* ====================================================================
  3. * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * 3. All advertising materials mentioning features or use of this
  18. * software must display the following acknowledgment:
  19. * "This product includes software developed by the OpenSSL Project
  20. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  21. *
  22. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  23. * endorse or promote products derived from this software without
  24. * prior written permission. For written permission, please contact
  25. * [email protected].
  26. *
  27. * 5. Products derived from this software may not be called "OpenSSL"
  28. * nor may "OpenSSL" appear in their names without prior written
  29. * permission of the OpenSSL Project.
  30. *
  31. * 6. Redistributions of any form whatsoever must retain the following
  32. * acknowledgment:
  33. * "This product includes software developed by the OpenSSL Project
  34. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  37. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  39. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  45. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  46. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  47. * OF THE POSSIBILITY OF SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This product includes cryptographic software written by Eric Young
  51. * ([email protected]). This product includes software written by Tim
  52. * Hudson ([email protected]).
  53. *
  54. */
  55. /* Copyright (C) 1995-1998 Eric Young ([email protected])
  56. * All rights reserved.
  57. *
  58. * This package is an SSL implementation written
  59. * by Eric Young ([email protected]).
  60. * The implementation was written so as to conform with Netscapes SSL.
  61. *
  62. * This library is free for commercial and non-commercial use as long as
  63. * the following conditions are aheared to. The following conditions
  64. * apply to all code found in this distribution, be it the RC4, RSA,
  65. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  66. * included with this distribution is covered by the same copyright terms
  67. * except that the holder is Tim Hudson ([email protected]).
  68. *
  69. * Copyright remains Eric Young's, and as such any Copyright notices in
  70. * the code are not to be removed.
  71. * If this package is used in a product, Eric Young should be given attribution
  72. * as the author of the parts of the library used.
  73. * This can be in the form of a textual message at program startup or
  74. * in documentation (online or textual) provided with the package.
  75. *
  76. * Redistribution and use in source and binary forms, with or without
  77. * modification, are permitted provided that the following conditions
  78. * are met:
  79. * 1. Redistributions of source code must retain the copyright
  80. * notice, this list of conditions and the following disclaimer.
  81. * 2. Redistributions in binary form must reproduce the above copyright
  82. * notice, this list of conditions and the following disclaimer in the
  83. * documentation and/or other materials provided with the distribution.
  84. * 3. All advertising materials mentioning features or use of this software
  85. * must display the following acknowledgement:
  86. * "This product includes cryptographic software written by
  87. * Eric Young ([email protected])"
  88. * The word 'cryptographic' can be left out if the rouines from the library
  89. * being used are not cryptographic related :-).
  90. * 4. If you include any Windows specific code (or a derivative thereof) from
  91. * the apps directory (application code) you must include an acknowledgement:
  92. * "This product includes software written by Tim Hudson ([email protected])"
  93. *
  94. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  95. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  96. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  97. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  98. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  99. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  100. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  101. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  102. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  103. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  104. * SUCH DAMAGE.
  105. *
  106. * The licence and distribution terms for any publically available version or
  107. * derivative of this code cannot be changed. i.e. this code cannot simply be
  108. * copied and put under another distribution licence
  109. * [including the GNU Public Licence.]
  110. */
  111. /* ====================================================================
  112. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  113. * ECDH support in OpenSSL originally developed by
  114. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  115. */
  116. #include "cryptlib.h"
  117. #include <openssl/safestack.h>
  118. #if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16)
  119. static double SSLeay_MSVC5_hack = 0.0; /* and for VC1.5 */
  120. #endif
  121. DECLARE_STACK_OF(CRYPTO_dynlock)
  122. /* real #defines in crypto.h, keep these upto date */
  123. static const char *const lock_names[CRYPTO_NUM_LOCKS] = {
  124. "<<ERROR>>",
  125. "err",
  126. "ex_data",
  127. "x509",
  128. "x509_info",
  129. "x509_pkey",
  130. "x509_crl",
  131. "x509_req",
  132. "dsa",
  133. "rsa",
  134. "evp_pkey",
  135. "x509_store",
  136. "ssl_ctx",
  137. "ssl_cert",
  138. "ssl_session",
  139. "ssl_sess_cert",
  140. "ssl",
  141. "ssl_method",
  142. "rand",
  143. "rand2",
  144. "debug_malloc",
  145. "BIO",
  146. "gethostbyname",
  147. "getservbyname",
  148. "readdir",
  149. "RSA_blinding",
  150. "dh",
  151. "debug_malloc2",
  152. "dso",
  153. "dynlock",
  154. "engine",
  155. "ui",
  156. "ecdsa",
  157. "ec",
  158. "ecdh",
  159. "bn",
  160. "ec_pre_comp",
  161. "store",
  162. "comp",
  163. "fips",
  164. "fips2",
  165. #if CRYPTO_NUM_LOCKS != 41
  166. # error "Inconsistency between crypto.h and cryptlib.c"
  167. #endif
  168. };
  169. /*
  170. * This is for applications to allocate new type names in the non-dynamic
  171. * array of lock names. These are numbered with positive numbers.
  172. */
  173. static STACK_OF(OPENSSL_STRING) *app_locks = NULL;
  174. /*
  175. * For applications that want a more dynamic way of handling threads, the
  176. * following stack is used. These are externally numbered with negative
  177. * numbers.
  178. */
  179. static STACK_OF(CRYPTO_dynlock) *dyn_locks = NULL;
  180. static void (MS_FAR *locking_callback) (int mode, int type,
  181. const char *file, int line) = 0;
  182. static int (MS_FAR *add_lock_callback) (int *pointer, int amount,
  183. int type, const char *file,
  184. int line) = 0;
  185. #ifndef OPENSSL_NO_DEPRECATED
  186. static unsigned long (MS_FAR *id_callback) (void) = 0;
  187. #endif
  188. static void (MS_FAR *threadid_callback) (CRYPTO_THREADID *) = 0;
  189. static struct CRYPTO_dynlock_value *(MS_FAR *dynlock_create_callback)
  190. (const char *file, int line) = 0;
  191. static void (MS_FAR *dynlock_lock_callback) (int mode,
  192. struct CRYPTO_dynlock_value *l,
  193. const char *file, int line) = 0;
  194. static void (MS_FAR *dynlock_destroy_callback) (struct CRYPTO_dynlock_value
  195. *l, const char *file,
  196. int line) = 0;
  197. int CRYPTO_get_new_lockid(char *name)
  198. {
  199. char *str;
  200. int i;
  201. #if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16)
  202. /*
  203. * A hack to make Visual C++ 5.0 work correctly when linking as a DLL
  204. * using /MT. Without this, the application cannot use any floating point
  205. * printf's. It also seems to be needed for Visual C 1.5 (win16)
  206. */
  207. SSLeay_MSVC5_hack = (double)name[0] * (double)name[1];
  208. #endif
  209. if ((app_locks == NULL)
  210. && ((app_locks = sk_OPENSSL_STRING_new_null()) == NULL)) {
  211. CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_LOCKID, ERR_R_MALLOC_FAILURE);
  212. return (0);
  213. }
  214. if ((str = BUF_strdup(name)) == NULL) {
  215. CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_LOCKID, ERR_R_MALLOC_FAILURE);
  216. return (0);
  217. }
  218. i = sk_OPENSSL_STRING_push(app_locks, str);
  219. if (!i)
  220. OPENSSL_free(str);
  221. else
  222. i += CRYPTO_NUM_LOCKS; /* gap of one :-) */
  223. return (i);
  224. }
  225. int CRYPTO_num_locks(void)
  226. {
  227. return CRYPTO_NUM_LOCKS;
  228. }
  229. int CRYPTO_get_new_dynlockid(void)
  230. {
  231. int i = 0;
  232. CRYPTO_dynlock *pointer = NULL;
  233. if (dynlock_create_callback == NULL) {
  234. CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID,
  235. CRYPTO_R_NO_DYNLOCK_CREATE_CALLBACK);
  236. return (0);
  237. }
  238. CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
  239. if ((dyn_locks == NULL)
  240. && ((dyn_locks = sk_CRYPTO_dynlock_new_null()) == NULL)) {
  241. CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
  242. CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID, ERR_R_MALLOC_FAILURE);
  243. return (0);
  244. }
  245. CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
  246. pointer = (CRYPTO_dynlock *) OPENSSL_malloc(sizeof(CRYPTO_dynlock));
  247. if (pointer == NULL) {
  248. CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID, ERR_R_MALLOC_FAILURE);
  249. return (0);
  250. }
  251. pointer->references = 1;
  252. pointer->data = dynlock_create_callback(__FILE__, __LINE__);
  253. if (pointer->data == NULL) {
  254. OPENSSL_free(pointer);
  255. CRYPTOerr(CRYPTO_F_CRYPTO_GET_NEW_DYNLOCKID, ERR_R_MALLOC_FAILURE);
  256. return (0);
  257. }
  258. CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
  259. /* First, try to find an existing empty slot */
  260. i = sk_CRYPTO_dynlock_find(dyn_locks, NULL);
  261. /* If there was none, push, thereby creating a new one */
  262. if (i == -1)
  263. /*
  264. * Since sk_push() returns the number of items on the stack, not the
  265. * location of the pushed item, we need to transform the returned
  266. * number into a position, by decreasing it.
  267. */
  268. i = sk_CRYPTO_dynlock_push(dyn_locks, pointer) - 1;
  269. else
  270. /*
  271. * If we found a place with a NULL pointer, put our pointer in it.
  272. */
  273. (void)sk_CRYPTO_dynlock_set(dyn_locks, i, pointer);
  274. CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
  275. if (i == -1) {
  276. dynlock_destroy_callback(pointer->data, __FILE__, __LINE__);
  277. OPENSSL_free(pointer);
  278. } else
  279. i += 1; /* to avoid 0 */
  280. return -i;
  281. }
  282. void CRYPTO_destroy_dynlockid(int i)
  283. {
  284. CRYPTO_dynlock *pointer = NULL;
  285. if (i)
  286. i = -i - 1;
  287. if (dynlock_destroy_callback == NULL)
  288. return;
  289. CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
  290. if (dyn_locks == NULL || i >= sk_CRYPTO_dynlock_num(dyn_locks)) {
  291. CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
  292. return;
  293. }
  294. pointer = sk_CRYPTO_dynlock_value(dyn_locks, i);
  295. if (pointer != NULL) {
  296. --pointer->references;
  297. #ifdef REF_CHECK
  298. if (pointer->references < 0) {
  299. fprintf(stderr,
  300. "CRYPTO_destroy_dynlockid, bad reference count\n");
  301. abort();
  302. } else
  303. #endif
  304. if (pointer->references <= 0) {
  305. (void)sk_CRYPTO_dynlock_set(dyn_locks, i, NULL);
  306. } else
  307. pointer = NULL;
  308. }
  309. CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
  310. if (pointer) {
  311. dynlock_destroy_callback(pointer->data, __FILE__, __LINE__);
  312. OPENSSL_free(pointer);
  313. }
  314. }
  315. struct CRYPTO_dynlock_value *CRYPTO_get_dynlock_value(int i)
  316. {
  317. CRYPTO_dynlock *pointer = NULL;
  318. if (i)
  319. i = -i - 1;
  320. CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
  321. if (dyn_locks != NULL && i < sk_CRYPTO_dynlock_num(dyn_locks))
  322. pointer = sk_CRYPTO_dynlock_value(dyn_locks, i);
  323. if (pointer)
  324. pointer->references++;
  325. CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
  326. if (pointer)
  327. return pointer->data;
  328. return NULL;
  329. }
  330. struct CRYPTO_dynlock_value *(*CRYPTO_get_dynlock_create_callback(void))
  331. (const char *file, int line) {
  332. return (dynlock_create_callback);
  333. }
  334. void (*CRYPTO_get_dynlock_lock_callback(void)) (int mode,
  335. struct CRYPTO_dynlock_value
  336. *l, const char *file,
  337. int line) {
  338. return (dynlock_lock_callback);
  339. }
  340. void (*CRYPTO_get_dynlock_destroy_callback(void))
  341. (struct CRYPTO_dynlock_value *l, const char *file, int line) {
  342. return (dynlock_destroy_callback);
  343. }
  344. void CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value *(*func)
  345. (const char *file, int line))
  346. {
  347. dynlock_create_callback = func;
  348. }
  349. void CRYPTO_set_dynlock_lock_callback(void (*func) (int mode,
  350. struct
  351. CRYPTO_dynlock_value *l,
  352. const char *file,
  353. int line))
  354. {
  355. dynlock_lock_callback = func;
  356. }
  357. void CRYPTO_set_dynlock_destroy_callback(void (*func)
  358. (struct CRYPTO_dynlock_value *l,
  359. const char *file, int line))
  360. {
  361. dynlock_destroy_callback = func;
  362. }
  363. void (*CRYPTO_get_locking_callback(void)) (int mode, int type,
  364. const char *file, int line) {
  365. return (locking_callback);
  366. }
  367. int (*CRYPTO_get_add_lock_callback(void)) (int *num, int mount, int type,
  368. const char *file, int line) {
  369. return (add_lock_callback);
  370. }
  371. void CRYPTO_set_locking_callback(void (*func) (int mode, int type,
  372. const char *file, int line))
  373. {
  374. /*
  375. * Calling this here ensures initialisation before any threads are
  376. * started.
  377. */
  378. OPENSSL_init();
  379. locking_callback = func;
  380. }
  381. void CRYPTO_set_add_lock_callback(int (*func) (int *num, int mount, int type,
  382. const char *file, int line))
  383. {
  384. add_lock_callback = func;
  385. }
  386. /*
  387. * the memset() here and in set_pointer() seem overkill, but for the sake of
  388. * CRYPTO_THREADID_cmp() this avoids any platform silliness that might cause
  389. * two "equal" THREADID structs to not be memcmp()-identical.
  390. */
  391. void CRYPTO_THREADID_set_numeric(CRYPTO_THREADID *id, unsigned long val)
  392. {
  393. memset(id, 0, sizeof(*id));
  394. id->val = val;
  395. }
  396. static const unsigned char hash_coeffs[] = { 3, 5, 7, 11, 13, 17, 19, 23 };
  397. void CRYPTO_THREADID_set_pointer(CRYPTO_THREADID *id, void *ptr)
  398. {
  399. unsigned char *dest = (void *)&id->val;
  400. unsigned int accum = 0;
  401. unsigned char dnum = sizeof(id->val);
  402. memset(id, 0, sizeof(*id));
  403. id->ptr = ptr;
  404. if (sizeof(id->val) >= sizeof(id->ptr)) {
  405. /*
  406. * 'ptr' can be embedded in 'val' without loss of uniqueness
  407. */
  408. id->val = (unsigned long)id->ptr;
  409. return;
  410. }
  411. /*
  412. * hash ptr ==> val. Each byte of 'val' gets the mod-256 total of a
  413. * linear function over the bytes in 'ptr', the co-efficients of which
  414. * are a sequence of low-primes (hash_coeffs is an 8-element cycle) - the
  415. * starting prime for the sequence varies for each byte of 'val' (unique
  416. * polynomials unless pointers are >64-bit). For added spice, the totals
  417. * accumulate rather than restarting from zero, and the index of the
  418. * 'val' byte is added each time (position dependence). If I was a
  419. * black-belt, I'd scan big-endian pointers in reverse to give low-order
  420. * bits more play, but this isn't crypto and I'd prefer nobody mistake it
  421. * as such. Plus I'm lazy.
  422. */
  423. while (dnum--) {
  424. const unsigned char *src = (void *)&id->ptr;
  425. unsigned char snum = sizeof(id->ptr);
  426. while (snum--)
  427. accum += *(src++) * hash_coeffs[(snum + dnum) & 7];
  428. accum += dnum;
  429. *(dest++) = accum & 255;
  430. }
  431. }
  432. int CRYPTO_THREADID_set_callback(void (*func) (CRYPTO_THREADID *))
  433. {
  434. if (threadid_callback)
  435. return 0;
  436. threadid_callback = func;
  437. return 1;
  438. }
  439. void (*CRYPTO_THREADID_get_callback(void)) (CRYPTO_THREADID *) {
  440. return threadid_callback;
  441. }
  442. void CRYPTO_THREADID_current(CRYPTO_THREADID *id)
  443. {
  444. if (threadid_callback) {
  445. threadid_callback(id);
  446. return;
  447. }
  448. #ifndef OPENSSL_NO_DEPRECATED
  449. /* If the deprecated callback was set, fall back to that */
  450. if (id_callback) {
  451. CRYPTO_THREADID_set_numeric(id, id_callback());
  452. return;
  453. }
  454. #endif
  455. /* Else pick a backup */
  456. #ifdef OPENSSL_SYS_WIN16
  457. CRYPTO_THREADID_set_numeric(id, (unsigned long)GetCurrentTask());
  458. #elif defined(OPENSSL_SYS_WIN32)
  459. CRYPTO_THREADID_set_numeric(id, (unsigned long)GetCurrentThreadId());
  460. #elif defined(OPENSSL_SYS_BEOS)
  461. CRYPTO_THREADID_set_numeric(id, (unsigned long)find_thread(NULL));
  462. #else
  463. /* For everything else, default to using the address of 'errno' */
  464. CRYPTO_THREADID_set_pointer(id, (void *)&errno);
  465. #endif
  466. }
  467. int CRYPTO_THREADID_cmp(const CRYPTO_THREADID *a, const CRYPTO_THREADID *b)
  468. {
  469. return memcmp(a, b, sizeof(*a));
  470. }
  471. void CRYPTO_THREADID_cpy(CRYPTO_THREADID *dest, const CRYPTO_THREADID *src)
  472. {
  473. memcpy(dest, src, sizeof(*src));
  474. }
  475. unsigned long CRYPTO_THREADID_hash(const CRYPTO_THREADID *id)
  476. {
  477. return id->val;
  478. }
  479. #ifndef OPENSSL_NO_DEPRECATED
  480. unsigned long (*CRYPTO_get_id_callback(void)) (void) {
  481. return (id_callback);
  482. }
  483. void CRYPTO_set_id_callback(unsigned long (*func) (void))
  484. {
  485. id_callback = func;
  486. }
  487. unsigned long CRYPTO_thread_id(void)
  488. {
  489. unsigned long ret = 0;
  490. if (id_callback == NULL) {
  491. # ifdef OPENSSL_SYS_WIN16
  492. ret = (unsigned long)GetCurrentTask();
  493. # elif defined(OPENSSL_SYS_WIN32)
  494. ret = (unsigned long)GetCurrentThreadId();
  495. # elif defined(GETPID_IS_MEANINGLESS)
  496. ret = 1L;
  497. # elif defined(OPENSSL_SYS_BEOS)
  498. ret = (unsigned long)find_thread(NULL);
  499. # else
  500. ret = (unsigned long)getpid();
  501. # endif
  502. } else
  503. ret = id_callback();
  504. return (ret);
  505. }
  506. #endif
  507. void CRYPTO_lock(int mode, int type, const char *file, int line)
  508. {
  509. #ifdef LOCK_DEBUG
  510. {
  511. CRYPTO_THREADID id;
  512. char *rw_text, *operation_text;
  513. if (mode & CRYPTO_LOCK)
  514. operation_text = "lock ";
  515. else if (mode & CRYPTO_UNLOCK)
  516. operation_text = "unlock";
  517. else
  518. operation_text = "ERROR ";
  519. if (mode & CRYPTO_READ)
  520. rw_text = "r";
  521. else if (mode & CRYPTO_WRITE)
  522. rw_text = "w";
  523. else
  524. rw_text = "ERROR";
  525. CRYPTO_THREADID_current(&id);
  526. fprintf(stderr, "lock:%08lx:(%s)%s %-18s %s:%d\n",
  527. CRYPTO_THREADID_hash(&id), rw_text, operation_text,
  528. CRYPTO_get_lock_name(type), file, line);
  529. }
  530. #endif
  531. if (type < 0) {
  532. if (dynlock_lock_callback != NULL) {
  533. struct CRYPTO_dynlock_value *pointer
  534. = CRYPTO_get_dynlock_value(type);
  535. OPENSSL_assert(pointer != NULL);
  536. dynlock_lock_callback(mode, pointer, file, line);
  537. CRYPTO_destroy_dynlockid(type);
  538. }
  539. } else if (locking_callback != NULL)
  540. locking_callback(mode, type, file, line);
  541. }
  542. int CRYPTO_add_lock(int *pointer, int amount, int type, const char *file,
  543. int line)
  544. {
  545. int ret = 0;
  546. if (add_lock_callback != NULL) {
  547. #ifdef LOCK_DEBUG
  548. int before = *pointer;
  549. #endif
  550. ret = add_lock_callback(pointer, amount, type, file, line);
  551. #ifdef LOCK_DEBUG
  552. {
  553. CRYPTO_THREADID id;
  554. CRYPTO_THREADID_current(&id);
  555. fprintf(stderr, "ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\n",
  556. CRYPTO_THREADID_hash(&id), before, amount, ret,
  557. CRYPTO_get_lock_name(type), file, line);
  558. }
  559. #endif
  560. } else {
  561. CRYPTO_lock(CRYPTO_LOCK | CRYPTO_WRITE, type, file, line);
  562. ret = *pointer + amount;
  563. #ifdef LOCK_DEBUG
  564. {
  565. CRYPTO_THREADID id;
  566. CRYPTO_THREADID_current(&id);
  567. fprintf(stderr, "ladd:%08lx:%2d+%2d->%2d %-18s %s:%d\n",
  568. CRYPTO_THREADID_hash(&id),
  569. *pointer, amount, ret,
  570. CRYPTO_get_lock_name(type), file, line);
  571. }
  572. #endif
  573. *pointer = ret;
  574. CRYPTO_lock(CRYPTO_UNLOCK | CRYPTO_WRITE, type, file, line);
  575. }
  576. return (ret);
  577. }
  578. const char *CRYPTO_get_lock_name(int type)
  579. {
  580. if (type < 0)
  581. return ("dynamic");
  582. else if (type < CRYPTO_NUM_LOCKS)
  583. return (lock_names[type]);
  584. else if (type - CRYPTO_NUM_LOCKS > sk_OPENSSL_STRING_num(app_locks))
  585. return ("ERROR");
  586. else
  587. return (sk_OPENSSL_STRING_value(app_locks, type - CRYPTO_NUM_LOCKS));
  588. }
  589. #if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
  590. defined(__INTEL__) || \
  591. defined(__x86_64) || defined(__x86_64__) || \
  592. defined(_M_AMD64) || defined(_M_X64)
  593. unsigned int OPENSSL_ia32cap_P[2];
  594. unsigned long *OPENSSL_ia32cap_loc(void)
  595. {
  596. if (sizeof(long) == 4)
  597. /*
  598. * If 32-bit application pulls address of OPENSSL_ia32cap_P[0]
  599. * clear second element to maintain the illusion that vector
  600. * is 32-bit.
  601. */
  602. OPENSSL_ia32cap_P[1] = 0;
  603. return (unsigned long *)OPENSSL_ia32cap_P;
  604. }
  605. # if defined(OPENSSL_CPUID_OBJ) && !defined(OPENSSL_NO_ASM) && !defined(I386_ONLY)
  606. # define OPENSSL_CPUID_SETUP
  607. # if defined(_WIN32)
  608. typedef unsigned __int64 IA32CAP;
  609. # else
  610. typedef unsigned long long IA32CAP;
  611. # endif
  612. void OPENSSL_cpuid_setup(void)
  613. {
  614. static int trigger = 0;
  615. IA32CAP OPENSSL_ia32_cpuid(void);
  616. IA32CAP vec;
  617. char *env;
  618. if (trigger)
  619. return;
  620. trigger = 1;
  621. if ((env = getenv("OPENSSL_ia32cap"))) {
  622. int off = (env[0] == '~') ? 1 : 0;
  623. # if defined(_WIN32)
  624. if (!sscanf(env + off, "%I64i", &vec))
  625. vec = strtoul(env + off, NULL, 0);
  626. # else
  627. if (!sscanf(env + off, "%lli", (long long *)&vec))
  628. vec = strtoul(env + off, NULL, 0);
  629. # endif
  630. if (off)
  631. vec = OPENSSL_ia32_cpuid() & ~vec;
  632. } else
  633. vec = OPENSSL_ia32_cpuid();
  634. /*
  635. * |(1<<10) sets a reserved bit to signal that variable
  636. * was initialized already... This is to avoid interference
  637. * with cpuid snippets in ELF .init segment.
  638. */
  639. OPENSSL_ia32cap_P[0] = (unsigned int)vec | (1 << 10);
  640. OPENSSL_ia32cap_P[1] = (unsigned int)(vec >> 32);
  641. }
  642. # endif
  643. #else
  644. unsigned long *OPENSSL_ia32cap_loc(void)
  645. {
  646. return NULL;
  647. }
  648. #endif
  649. int OPENSSL_NONPIC_relocated = 0;
  650. #if !defined(OPENSSL_CPUID_SETUP) && !defined(OPENSSL_CPUID_OBJ)
  651. void OPENSSL_cpuid_setup(void)
  652. {
  653. }
  654. #endif
  655. #if (defined(_WIN32) || defined(__CYGWIN__)) && defined(_WINDLL)
  656. # ifdef __CYGWIN__
  657. /* pick DLL_[PROCESS|THREAD]_[ATTACH|DETACH] definitions */
  658. # include <windows.h>
  659. /*
  660. * this has side-effect of _WIN32 getting defined, which otherwise is
  661. * mutually exclusive with __CYGWIN__...
  662. */
  663. # endif
  664. /*
  665. * All we really need to do is remove the 'error' state when a thread
  666. * detaches
  667. */
  668. BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
  669. {
  670. switch (fdwReason) {
  671. case DLL_PROCESS_ATTACH:
  672. OPENSSL_cpuid_setup();
  673. # if defined(_WIN32_WINNT)
  674. {
  675. IMAGE_DOS_HEADER *dos_header = (IMAGE_DOS_HEADER *) hinstDLL;
  676. IMAGE_NT_HEADERS *nt_headers;
  677. if (dos_header->e_magic == IMAGE_DOS_SIGNATURE) {
  678. nt_headers = (IMAGE_NT_HEADERS *) ((char *)dos_header
  679. + dos_header->e_lfanew);
  680. if (nt_headers->Signature == IMAGE_NT_SIGNATURE &&
  681. hinstDLL !=
  682. (HINSTANCE) (nt_headers->OptionalHeader.ImageBase))
  683. OPENSSL_NONPIC_relocated = 1;
  684. }
  685. }
  686. # endif
  687. break;
  688. case DLL_THREAD_ATTACH:
  689. break;
  690. case DLL_THREAD_DETACH:
  691. break;
  692. case DLL_PROCESS_DETACH:
  693. break;
  694. }
  695. return (TRUE);
  696. }
  697. #endif
  698. #if defined(_WIN32) && !defined(__CYGWIN__)
  699. # include <tchar.h>
  700. # include <signal.h>
  701. # ifdef __WATCOMC__
  702. # if defined(_UNICODE) || defined(__UNICODE__)
  703. # define _vsntprintf _vsnwprintf
  704. # else
  705. # define _vsntprintf _vsnprintf
  706. # endif
  707. # endif
  708. # ifdef _MSC_VER
  709. # define alloca _alloca
  710. # endif
  711. # if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
  712. int OPENSSL_isservice(void)
  713. {
  714. HWINSTA h;
  715. DWORD len;
  716. WCHAR *name;
  717. static union {
  718. void *p;
  719. int (*f) (void);
  720. } _OPENSSL_isservice = {
  721. NULL
  722. };
  723. if (_OPENSSL_isservice.p == NULL) {
  724. HANDLE h = GetModuleHandle(NULL);
  725. if (h != NULL)
  726. _OPENSSL_isservice.p = GetProcAddress(h, "_OPENSSL_isservice");
  727. if (_OPENSSL_isservice.p == NULL)
  728. _OPENSSL_isservice.p = (void *)-1;
  729. }
  730. if (_OPENSSL_isservice.p != (void *)-1)
  731. return (*_OPENSSL_isservice.f) ();
  732. h = GetProcessWindowStation();
  733. if (h == NULL)
  734. return -1;
  735. if (GetUserObjectInformationW(h, UOI_NAME, NULL, 0, &len) ||
  736. GetLastError() != ERROR_INSUFFICIENT_BUFFER)
  737. return -1;
  738. if (len > 512)
  739. return -1; /* paranoia */
  740. len++, len &= ~1; /* paranoia */
  741. name = (WCHAR *)alloca(len + sizeof(WCHAR));
  742. if (!GetUserObjectInformationW(h, UOI_NAME, name, len, &len))
  743. return -1;
  744. len++, len &= ~1; /* paranoia */
  745. name[len / sizeof(WCHAR)] = L'\0'; /* paranoia */
  746. # if 1
  747. /*
  748. * This doesn't cover "interactive" services [working with real
  749. * WinSta0's] nor programs started non-interactively by Task Scheduler
  750. * [those are working with SAWinSta].
  751. */
  752. if (wcsstr(name, L"Service-0x"))
  753. return 1;
  754. # else
  755. /* This covers all non-interactive programs such as services. */
  756. if (!wcsstr(name, L"WinSta0"))
  757. return 1;
  758. # endif
  759. else
  760. return 0;
  761. }
  762. # else
  763. int OPENSSL_isservice(void)
  764. {
  765. return 0;
  766. }
  767. # endif
  768. void OPENSSL_showfatal(const char *fmta, ...)
  769. {
  770. va_list ap;
  771. TCHAR buf[256];
  772. const TCHAR *fmt;
  773. # ifdef STD_ERROR_HANDLE /* what a dirty trick! */
  774. HANDLE h;
  775. if ((h = GetStdHandle(STD_ERROR_HANDLE)) != NULL &&
  776. GetFileType(h) != FILE_TYPE_UNKNOWN) {
  777. /* must be console application */
  778. va_start(ap, fmta);
  779. vfprintf(stderr, fmta, ap);
  780. va_end(ap);
  781. return;
  782. }
  783. # endif
  784. if (sizeof(TCHAR) == sizeof(char))
  785. fmt = (const TCHAR *)fmta;
  786. else
  787. do {
  788. int keepgoing;
  789. size_t len_0 = strlen(fmta) + 1, i;
  790. WCHAR *fmtw;
  791. fmtw = (WCHAR *)alloca(len_0 * sizeof(WCHAR));
  792. if (fmtw == NULL) {
  793. fmt = (const TCHAR *)L"no stack?";
  794. break;
  795. }
  796. # ifndef OPENSSL_NO_MULTIBYTE
  797. if (!MultiByteToWideChar(CP_ACP, 0, fmta, len_0, fmtw, len_0))
  798. # endif
  799. for (i = 0; i < len_0; i++)
  800. fmtw[i] = (WCHAR)fmta[i];
  801. for (i = 0; i < len_0; i++) {
  802. if (fmtw[i] == L'%')
  803. do {
  804. keepgoing = 0;
  805. switch (fmtw[i + 1]) {
  806. case L'0':
  807. case L'1':
  808. case L'2':
  809. case L'3':
  810. case L'4':
  811. case L'5':
  812. case L'6':
  813. case L'7':
  814. case L'8':
  815. case L'9':
  816. case L'.':
  817. case L'*':
  818. case L'-':
  819. i++;
  820. keepgoing = 1;
  821. break;
  822. case L's':
  823. fmtw[i + 1] = L'S';
  824. break;
  825. case L'S':
  826. fmtw[i + 1] = L's';
  827. break;
  828. case L'c':
  829. fmtw[i + 1] = L'C';
  830. break;
  831. case L'C':
  832. fmtw[i + 1] = L'c';
  833. break;
  834. }
  835. } while (keepgoing);
  836. }
  837. fmt = (const TCHAR *)fmtw;
  838. } while (0);
  839. va_start(ap, fmta);
  840. _vsntprintf(buf, sizeof(buf) / sizeof(TCHAR) - 1, fmt, ap);
  841. buf[sizeof(buf) / sizeof(TCHAR) - 1] = _T('\0');
  842. va_end(ap);
  843. # if defined(_WIN32_WINNT) && _WIN32_WINNT>=0x0333
  844. /* this -------------v--- guards NT-specific calls */
  845. if (check_winnt() && OPENSSL_isservice() > 0) {
  846. HANDLE hEventLog = RegisterEventSource(NULL, _T("OpenSSL"));
  847. if (hEventLog != NULL) {
  848. const TCHAR *pmsg = buf;
  849. if (!ReportEvent(hEventLog, EVENTLOG_ERROR_TYPE, 0, 0, NULL,
  850. 1, 0, &pmsg, NULL)) {
  851. #if defined(DEBUG)
  852. /*
  853. * We are in a situation where we tried to report a critical
  854. * error and this failed for some reason. As a last resort,
  855. * in debug builds, send output to the debugger or any other
  856. * tool like DebugView which can monitor the output.
  857. */
  858. OutputDebugString(pmsg);
  859. #endif
  860. }
  861. (void)DeregisterEventSource(hEventLog);
  862. }
  863. } else
  864. # endif
  865. MessageBox(NULL, buf, _T("OpenSSL: FATAL"), MB_OK | MB_ICONERROR);
  866. }
  867. #else
  868. void OPENSSL_showfatal(const char *fmta, ...)
  869. {
  870. va_list ap;
  871. va_start(ap, fmta);
  872. vfprintf(stderr, fmta, ap);
  873. va_end(ap);
  874. }
  875. int OPENSSL_isservice(void)
  876. {
  877. return 0;
  878. }
  879. #endif
  880. void OpenSSLDie(const char *file, int line, const char *assertion)
  881. {
  882. OPENSSL_showfatal
  883. ("%s(%d): OpenSSL internal error, assertion failed: %s\n", file, line,
  884. assertion);
  885. #if !defined(_WIN32) || defined(__CYGWIN__)
  886. abort();
  887. #else
  888. /*
  889. * Win32 abort() customarily shows a dialog, but we just did that...
  890. */
  891. raise(SIGABRT);
  892. _exit(3);
  893. #endif
  894. }
  895. void *OPENSSL_stderr(void)
  896. {
  897. return stderr;
  898. }
  899. int CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len)
  900. {
  901. size_t i;
  902. const unsigned char *a = in_a;
  903. const unsigned char *b = in_b;
  904. unsigned char x = 0;
  905. for (i = 0; i < len; i++)
  906. x |= a[i] ^ b[i];
  907. return x;
  908. }