err_def.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /* crypto/err/err_def.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. /* ====================================================================
  59. * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * openssl-core@openssl.org.
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * (eay@cryptsoft.com). This product includes software written by Tim
  108. * Hudson (tjh@cryptsoft.com).
  109. *
  110. */
  111. #include <stdio.h>
  112. #include <stdarg.h>
  113. #include <string.h>
  114. #include "cryptlib.h"
  115. #include <openssl/lhash.h>
  116. #include <openssl/crypto.h>
  117. #include <openssl/buffer.h>
  118. #include <openssl/bio.h>
  119. #include <openssl/err.h>
  120. #define err_clear_data(p,i) \
  121. do { \
  122. if (((p)->err_data[i] != NULL) && \
  123. (p)->err_data_flags[i] & ERR_TXT_MALLOCED) \
  124. { \
  125. OPENSSL_free((p)->err_data[i]); \
  126. (p)->err_data[i]=NULL; \
  127. } \
  128. (p)->err_data_flags[i]=0; \
  129. } while(0)
  130. #define err_clear(p,i) \
  131. do { \
  132. (p)->err_flags[i]=0; \
  133. (p)->err_buffer[i]=0; \
  134. err_clear_data(p,i); \
  135. (p)->err_file[i]=NULL; \
  136. (p)->err_line[i]= -1; \
  137. } while(0)
  138. static void err_load_strings(int lib, ERR_STRING_DATA *str);
  139. static void ERR_STATE_free(ERR_STATE *s);
  140. /* Define the predeclared (but externally opaque) "ERR_FNS" type */
  141. struct st_ERR_FNS {
  142. /* Works on the "error_hash" string table */
  143. LHASH *(*cb_err_get) (int create);
  144. void (*cb_err_del) (void);
  145. ERR_STRING_DATA *(*cb_err_get_item) (const ERR_STRING_DATA *);
  146. ERR_STRING_DATA *(*cb_err_set_item) (ERR_STRING_DATA *);
  147. ERR_STRING_DATA *(*cb_err_del_item) (ERR_STRING_DATA *);
  148. /* Works on the "thread_hash" error-state table */
  149. LHASH *(*cb_thread_get) (int create);
  150. void (*cb_thread_release) (LHASH **hash);
  151. ERR_STATE *(*cb_thread_get_item) (const ERR_STATE *);
  152. ERR_STATE *(*cb_thread_set_item) (ERR_STATE *);
  153. void (*cb_thread_del_item) (const ERR_STATE *);
  154. /* Returns the next available error "library" numbers */
  155. int (*cb_get_next_lib) (void);
  156. };
  157. /* Predeclarations of the "err_defaults" functions */
  158. static LHASH *int_err_get(int create);
  159. static void int_err_del(void);
  160. static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *);
  161. static ERR_STRING_DATA *int_err_set_item(ERR_STRING_DATA *);
  162. static ERR_STRING_DATA *int_err_del_item(ERR_STRING_DATA *);
  163. static LHASH *int_thread_get(int create);
  164. static void int_thread_release(LHASH **hash);
  165. static ERR_STATE *int_thread_get_item(const ERR_STATE *);
  166. static ERR_STATE *int_thread_set_item(ERR_STATE *);
  167. static void int_thread_del_item(const ERR_STATE *);
  168. static int int_err_get_next_lib(void);
  169. /* The static ERR_FNS table using these defaults functions */
  170. static const ERR_FNS err_defaults = {
  171. int_err_get,
  172. int_err_del,
  173. int_err_get_item,
  174. int_err_set_item,
  175. int_err_del_item,
  176. int_thread_get,
  177. int_thread_release,
  178. int_thread_get_item,
  179. int_thread_set_item,
  180. int_thread_del_item,
  181. int_err_get_next_lib
  182. };
  183. /* The replacable table of ERR_FNS functions we use at run-time */
  184. static const ERR_FNS *err_fns = NULL;
  185. /* Eg. rather than using "err_get()", use "ERRFN(err_get)()". */
  186. #define ERRFN(a) err_fns->cb_##a
  187. /*
  188. * The internal state used by "err_defaults" - as such, the setting, reading,
  189. * creating, and deleting of this data should only be permitted via the
  190. * "err_defaults" functions. This way, a linked module can completely defer
  191. * all ERR state operation (together with requisite locking) to the
  192. * implementations and state in the loading application.
  193. */
  194. static LHASH *int_error_hash = NULL;
  195. static LHASH *int_thread_hash = NULL;
  196. static int int_thread_hash_references = 0;
  197. static int int_err_library_number = ERR_LIB_USER;
  198. /*
  199. * Internal function that checks whether "err_fns" is set and if not, sets it
  200. * to the defaults.
  201. */
  202. static void err_fns_check(void)
  203. {
  204. if (err_fns)
  205. return;
  206. CRYPTO_w_lock(CRYPTO_LOCK_ERR);
  207. if (!err_fns)
  208. err_fns = &err_defaults;
  209. CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
  210. }
  211. /* API functions to get or set the underlying ERR functions. */
  212. const ERR_FNS *ERR_get_implementation(void)
  213. {
  214. err_fns_check();
  215. return err_fns;
  216. }
  217. int ERR_set_implementation(const ERR_FNS *fns)
  218. {
  219. int ret = 0;
  220. CRYPTO_w_lock(CRYPTO_LOCK_ERR);
  221. /*
  222. * It's too late if 'err_fns' is non-NULL. BTW: not much point setting an
  223. * error is there?!
  224. */
  225. if (!err_fns) {
  226. err_fns = fns;
  227. ret = 1;
  228. }
  229. CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
  230. return ret;
  231. }
  232. /*
  233. * These are the callbacks provided to "lh_new()" when creating the LHASH
  234. * tables internal to the "err_defaults" implementation.
  235. */
  236. /* static unsigned long err_hash(ERR_STRING_DATA *a); */
  237. static unsigned long err_hash(const void *a_void);
  238. /* static int err_cmp(ERR_STRING_DATA *a, ERR_STRING_DATA *b); */
  239. static int err_cmp(const void *a_void, const void *b_void);
  240. /* static unsigned long pid_hash(ERR_STATE *pid); */
  241. static unsigned long pid_hash(const void *pid_void);
  242. /* static int pid_cmp(ERR_STATE *a,ERR_STATE *pid); */
  243. static int pid_cmp(const void *a_void, const void *pid_void);
  244. /* The internal functions used in the "err_defaults" implementation */
  245. static LHASH *int_err_get(int create)
  246. {
  247. LHASH *ret = NULL;
  248. CRYPTO_w_lock(CRYPTO_LOCK_ERR);
  249. if (!int_error_hash && create) {
  250. CRYPTO_push_info("int_err_get (err.c)");
  251. int_error_hash = lh_new(err_hash, err_cmp);
  252. CRYPTO_pop_info();
  253. }
  254. if (int_error_hash)
  255. ret = int_error_hash;
  256. CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
  257. return ret;
  258. }
  259. static void int_err_del(void)
  260. {
  261. CRYPTO_w_lock(CRYPTO_LOCK_ERR);
  262. if (int_error_hash) {
  263. lh_free(int_error_hash);
  264. int_error_hash = NULL;
  265. }
  266. CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
  267. }
  268. static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *d)
  269. {
  270. ERR_STRING_DATA *p;
  271. LHASH *hash;
  272. err_fns_check();
  273. hash = ERRFN(err_get) (0);
  274. if (!hash)
  275. return NULL;
  276. CRYPTO_r_lock(CRYPTO_LOCK_ERR);
  277. p = (ERR_STRING_DATA *)lh_retrieve(hash, d);
  278. CRYPTO_r_unlock(CRYPTO_LOCK_ERR);
  279. return p;
  280. }
  281. static ERR_STRING_DATA *int_err_set_item(ERR_STRING_DATA *d)
  282. {
  283. ERR_STRING_DATA *p;
  284. LHASH *hash;
  285. err_fns_check();
  286. hash = ERRFN(err_get) (1);
  287. if (!hash)
  288. return NULL;
  289. CRYPTO_w_lock(CRYPTO_LOCK_ERR);
  290. p = (ERR_STRING_DATA *)lh_insert(hash, d);
  291. CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
  292. return p;
  293. }
  294. static ERR_STRING_DATA *int_err_del_item(ERR_STRING_DATA *d)
  295. {
  296. ERR_STRING_DATA *p;
  297. LHASH *hash;
  298. err_fns_check();
  299. hash = ERRFN(err_get) (0);
  300. if (!hash)
  301. return NULL;
  302. CRYPTO_w_lock(CRYPTO_LOCK_ERR);
  303. p = (ERR_STRING_DATA *)lh_delete(hash, d);
  304. CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
  305. return p;
  306. }
  307. static LHASH *int_thread_get(int create)
  308. {
  309. LHASH *ret = NULL;
  310. CRYPTO_w_lock(CRYPTO_LOCK_ERR);
  311. if (!int_thread_hash && create) {
  312. CRYPTO_push_info("int_thread_get (err.c)");
  313. int_thread_hash = lh_new(pid_hash, pid_cmp);
  314. CRYPTO_pop_info();
  315. }
  316. if (int_thread_hash) {
  317. int_thread_hash_references++;
  318. ret = int_thread_hash;
  319. }
  320. CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
  321. return ret;
  322. }
  323. static void int_thread_release(LHASH **hash)
  324. {
  325. int i;
  326. if (hash == NULL || *hash == NULL)
  327. return;
  328. i = CRYPTO_add(&int_thread_hash_references, -1, CRYPTO_LOCK_ERR);
  329. #ifdef REF_PRINT
  330. fprintf(stderr, "%4d:%s\n", int_thread_hash_references, "ERR");
  331. #endif
  332. if (i > 0)
  333. return;
  334. #ifdef REF_CHECK
  335. if (i < 0) {
  336. fprintf(stderr, "int_thread_release, bad reference count\n");
  337. abort(); /* ok */
  338. }
  339. #endif
  340. *hash = NULL;
  341. }
  342. static ERR_STATE *int_thread_get_item(const ERR_STATE *d)
  343. {
  344. ERR_STATE *p;
  345. LHASH *hash;
  346. err_fns_check();
  347. hash = ERRFN(thread_get) (0);
  348. if (!hash)
  349. return NULL;
  350. CRYPTO_r_lock(CRYPTO_LOCK_ERR);
  351. p = (ERR_STATE *)lh_retrieve(hash, d);
  352. CRYPTO_r_unlock(CRYPTO_LOCK_ERR);
  353. ERRFN(thread_release) (&hash);
  354. return p;
  355. }
  356. static ERR_STATE *int_thread_set_item(ERR_STATE *d)
  357. {
  358. ERR_STATE *p;
  359. LHASH *hash;
  360. err_fns_check();
  361. hash = ERRFN(thread_get) (1);
  362. if (!hash)
  363. return NULL;
  364. CRYPTO_w_lock(CRYPTO_LOCK_ERR);
  365. p = (ERR_STATE *)lh_insert(hash, d);
  366. CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
  367. ERRFN(thread_release) (&hash);
  368. return p;
  369. }
  370. static void int_thread_del_item(const ERR_STATE *d)
  371. {
  372. ERR_STATE *p;
  373. LHASH *hash;
  374. err_fns_check();
  375. hash = ERRFN(thread_get) (0);
  376. if (!hash)
  377. return;
  378. CRYPTO_w_lock(CRYPTO_LOCK_ERR);
  379. p = (ERR_STATE *)lh_delete(hash, d);
  380. /* make sure we don't leak memory */
  381. if (int_thread_hash_references == 1
  382. && int_thread_hash && (lh_num_items(int_thread_hash) == 0)) {
  383. lh_free(int_thread_hash);
  384. int_thread_hash = NULL;
  385. }
  386. CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
  387. ERRFN(thread_release) (&hash);
  388. if (p)
  389. ERR_STATE_free(p);
  390. }
  391. static int int_err_get_next_lib(void)
  392. {
  393. int ret;
  394. CRYPTO_w_lock(CRYPTO_LOCK_ERR);
  395. ret = int_err_library_number++;
  396. CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
  397. return ret;
  398. }
  399. static void ERR_STATE_free(ERR_STATE *s)
  400. {
  401. int i;
  402. if (s == NULL)
  403. return;
  404. for (i = 0; i < ERR_NUM_ERRORS; i++) {
  405. err_clear_data(s, i);
  406. }
  407. OPENSSL_free(s);
  408. }
  409. static void err_load_strings(int lib, ERR_STRING_DATA *str)
  410. {
  411. while (str->error) {
  412. if (lib)
  413. str->error |= ERR_PACK(lib, 0, 0);
  414. ERRFN(err_set_item) (str);
  415. str++;
  416. }
  417. }
  418. void ERR_load_strings(int lib, ERR_STRING_DATA *str)
  419. {
  420. err_fns_check();
  421. err_load_strings(lib, str);
  422. }
  423. void ERR_unload_strings(int lib, ERR_STRING_DATA *str)
  424. {
  425. while (str->error) {
  426. if (lib)
  427. str->error |= ERR_PACK(lib, 0, 0);
  428. ERRFN(err_del_item) (str);
  429. str++;
  430. }
  431. }
  432. void ERR_free_strings(void)
  433. {
  434. err_fns_check();
  435. ERRFN(err_del) ();
  436. }
  437. LHASH *ERR_get_string_table(void)
  438. {
  439. err_fns_check();
  440. return ERRFN(err_get) (0);
  441. }
  442. LHASH *ERR_get_err_state_table(void)
  443. {
  444. err_fns_check();
  445. return ERRFN(thread_get) (0);
  446. }
  447. void ERR_release_err_state_table(LHASH **hash)
  448. {
  449. err_fns_check();
  450. ERRFN(thread_release) (hash);
  451. }
  452. const char *ERR_lib_error_string(unsigned long e)
  453. {
  454. ERR_STRING_DATA d, *p;
  455. unsigned long l;
  456. err_fns_check();
  457. l = ERR_GET_LIB(e);
  458. d.error = ERR_PACK(l, 0, 0);
  459. p = ERRFN(err_get_item) (&d);
  460. return ((p == NULL) ? NULL : p->string);
  461. }
  462. const char *ERR_func_error_string(unsigned long e)
  463. {
  464. ERR_STRING_DATA d, *p;
  465. unsigned long l, f;
  466. err_fns_check();
  467. l = ERR_GET_LIB(e);
  468. f = ERR_GET_FUNC(e);
  469. d.error = ERR_PACK(l, f, 0);
  470. p = ERRFN(err_get_item) (&d);
  471. return ((p == NULL) ? NULL : p->string);
  472. }
  473. const char *ERR_reason_error_string(unsigned long e)
  474. {
  475. ERR_STRING_DATA d, *p = NULL;
  476. unsigned long l, r;
  477. err_fns_check();
  478. l = ERR_GET_LIB(e);
  479. r = ERR_GET_REASON(e);
  480. d.error = ERR_PACK(l, 0, r);
  481. p = ERRFN(err_get_item) (&d);
  482. if (!p) {
  483. d.error = ERR_PACK(0, 0, r);
  484. p = ERRFN(err_get_item) (&d);
  485. }
  486. return ((p == NULL) ? NULL : p->string);
  487. }
  488. /* static unsigned long err_hash(ERR_STRING_DATA *a) */
  489. static unsigned long err_hash(const void *a_void)
  490. {
  491. unsigned long ret, l;
  492. l = ((const ERR_STRING_DATA *)a_void)->error;
  493. ret = l ^ ERR_GET_LIB(l) ^ ERR_GET_FUNC(l);
  494. return (ret ^ ret % 19 * 13);
  495. }
  496. /* static int err_cmp(ERR_STRING_DATA *a, ERR_STRING_DATA *b) */
  497. static int err_cmp(const void *a_void, const void *b_void)
  498. {
  499. return ((int)(((const ERR_STRING_DATA *)a_void)->error -
  500. ((const ERR_STRING_DATA *)b_void)->error));
  501. }
  502. /* static unsigned long pid_hash(ERR_STATE *a) */
  503. static unsigned long pid_hash(const void *a_void)
  504. {
  505. return (((const ERR_STATE *)a_void)->pid * 13);
  506. }
  507. /* static int pid_cmp(ERR_STATE *a, ERR_STATE *b) */
  508. static int pid_cmp(const void *a_void, const void *b_void)
  509. {
  510. return ((int)((long)((const ERR_STATE *)a_void)->pid -
  511. (long)((const ERR_STATE *)b_void)->pid));
  512. }
  513. #ifdef OPENSSL_FIPS
  514. static void int_err_remove_state(unsigned long pid)
  515. #else
  516. void ERR_remove_state(unsigned long pid)
  517. #endif
  518. {
  519. ERR_STATE tmp;
  520. err_fns_check();
  521. if (pid == 0)
  522. pid = (unsigned long)CRYPTO_thread_id();
  523. tmp.pid = pid;
  524. /*
  525. * thread_del_item automatically destroys the LHASH if the number of
  526. * items reaches zero.
  527. */
  528. ERRFN(thread_del_item) (&tmp);
  529. }
  530. #ifdef OPENSSL_FIPS
  531. static ERR_STATE *int_err_get_state(void)
  532. #else
  533. ERR_STATE *ERR_get_state(void)
  534. #endif
  535. {
  536. static ERR_STATE fallback;
  537. ERR_STATE *ret, tmp, *tmpp = NULL;
  538. int i;
  539. unsigned long pid;
  540. err_fns_check();
  541. pid = (unsigned long)CRYPTO_thread_id();
  542. tmp.pid = pid;
  543. ret = ERRFN(thread_get_item) (&tmp);
  544. /* ret == the error state, if NULL, make a new one */
  545. if (ret == NULL) {
  546. ret = (ERR_STATE *)OPENSSL_malloc(sizeof(ERR_STATE));
  547. if (ret == NULL)
  548. return (&fallback);
  549. ret->pid = pid;
  550. ret->top = 0;
  551. ret->bottom = 0;
  552. for (i = 0; i < ERR_NUM_ERRORS; i++) {
  553. ret->err_data[i] = NULL;
  554. ret->err_data_flags[i] = 0;
  555. }
  556. tmpp = ERRFN(thread_set_item) (ret);
  557. /* To check if insertion failed, do a get. */
  558. if (ERRFN(thread_get_item) (ret) != ret) {
  559. ERR_STATE_free(ret); /* could not insert it */
  560. return (&fallback);
  561. }
  562. /*
  563. * If a race occured in this function and we came second, tmpp is the
  564. * first one that we just replaced.
  565. */
  566. if (tmpp)
  567. ERR_STATE_free(tmpp);
  568. }
  569. return ret;
  570. }
  571. #ifdef OPENSSL_FIPS
  572. void int_ERR_lib_init(void)
  573. {
  574. int_ERR_set_state_func(int_err_get_state, int_err_remove_state);
  575. }
  576. #endif
  577. int ERR_get_next_error_library(void)
  578. {
  579. err_fns_check();
  580. return ERRFN(get_next_lib) ();
  581. }