err.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  1. /* crypto/err/err.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-2006 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. DECLARE_LHASH_OF(ERR_STRING_DATA);
  121. DECLARE_LHASH_OF(ERR_STATE);
  122. static void err_load_strings(int lib, ERR_STRING_DATA *str);
  123. static void ERR_STATE_free(ERR_STATE *s);
  124. #ifndef OPENSSL_NO_ERR
  125. static ERR_STRING_DATA ERR_str_libraries[] = {
  126. {ERR_PACK(ERR_LIB_NONE, 0, 0), "unknown library"},
  127. {ERR_PACK(ERR_LIB_SYS, 0, 0), "system library"},
  128. {ERR_PACK(ERR_LIB_BN, 0, 0), "bignum routines"},
  129. {ERR_PACK(ERR_LIB_RSA, 0, 0), "rsa routines"},
  130. {ERR_PACK(ERR_LIB_DH, 0, 0), "Diffie-Hellman routines"},
  131. {ERR_PACK(ERR_LIB_EVP, 0, 0), "digital envelope routines"},
  132. {ERR_PACK(ERR_LIB_BUF, 0, 0), "memory buffer routines"},
  133. {ERR_PACK(ERR_LIB_OBJ, 0, 0), "object identifier routines"},
  134. {ERR_PACK(ERR_LIB_PEM, 0, 0), "PEM routines"},
  135. {ERR_PACK(ERR_LIB_DSA, 0, 0), "dsa routines"},
  136. {ERR_PACK(ERR_LIB_X509, 0, 0), "x509 certificate routines"},
  137. {ERR_PACK(ERR_LIB_ASN1, 0, 0), "asn1 encoding routines"},
  138. {ERR_PACK(ERR_LIB_CONF, 0, 0), "configuration file routines"},
  139. {ERR_PACK(ERR_LIB_CRYPTO, 0, 0), "common libcrypto routines"},
  140. {ERR_PACK(ERR_LIB_EC, 0, 0), "elliptic curve routines"},
  141. {ERR_PACK(ERR_LIB_SSL, 0, 0), "SSL routines"},
  142. {ERR_PACK(ERR_LIB_BIO, 0, 0), "BIO routines"},
  143. {ERR_PACK(ERR_LIB_PKCS7, 0, 0), "PKCS7 routines"},
  144. {ERR_PACK(ERR_LIB_X509V3, 0, 0), "X509 V3 routines"},
  145. {ERR_PACK(ERR_LIB_PKCS12, 0, 0), "PKCS12 routines"},
  146. {ERR_PACK(ERR_LIB_RAND, 0, 0), "random number generator"},
  147. {ERR_PACK(ERR_LIB_DSO, 0, 0), "DSO support routines"},
  148. {ERR_PACK(ERR_LIB_TS, 0, 0), "time stamp routines"},
  149. {ERR_PACK(ERR_LIB_ENGINE, 0, 0), "engine routines"},
  150. {ERR_PACK(ERR_LIB_OCSP, 0, 0), "OCSP routines"},
  151. {ERR_PACK(ERR_LIB_FIPS, 0, 0), "FIPS routines"},
  152. {ERR_PACK(ERR_LIB_CMS, 0, 0), "CMS routines"},
  153. {ERR_PACK(ERR_LIB_HMAC, 0, 0), "HMAC routines"},
  154. {0, NULL},
  155. };
  156. static ERR_STRING_DATA ERR_str_functs[] = {
  157. {ERR_PACK(0, SYS_F_FOPEN, 0), "fopen"},
  158. {ERR_PACK(0, SYS_F_CONNECT, 0), "connect"},
  159. {ERR_PACK(0, SYS_F_GETSERVBYNAME, 0), "getservbyname"},
  160. {ERR_PACK(0, SYS_F_SOCKET, 0), "socket"},
  161. {ERR_PACK(0, SYS_F_IOCTLSOCKET, 0), "ioctlsocket"},
  162. {ERR_PACK(0, SYS_F_BIND, 0), "bind"},
  163. {ERR_PACK(0, SYS_F_LISTEN, 0), "listen"},
  164. {ERR_PACK(0, SYS_F_ACCEPT, 0), "accept"},
  165. # ifdef OPENSSL_SYS_WINDOWS
  166. {ERR_PACK(0, SYS_F_WSASTARTUP, 0), "WSAstartup"},
  167. # endif
  168. {ERR_PACK(0, SYS_F_OPENDIR, 0), "opendir"},
  169. {ERR_PACK(0, SYS_F_FREAD, 0), "fread"},
  170. {ERR_PACK(0, SYS_F_FFLUSH, 0), "fflush"},
  171. {0, NULL},
  172. };
  173. static ERR_STRING_DATA ERR_str_reasons[] = {
  174. {ERR_R_SYS_LIB, "system lib"},
  175. {ERR_R_BN_LIB, "BN lib"},
  176. {ERR_R_RSA_LIB, "RSA lib"},
  177. {ERR_R_DH_LIB, "DH lib"},
  178. {ERR_R_EVP_LIB, "EVP lib"},
  179. {ERR_R_BUF_LIB, "BUF lib"},
  180. {ERR_R_OBJ_LIB, "OBJ lib"},
  181. {ERR_R_PEM_LIB, "PEM lib"},
  182. {ERR_R_DSA_LIB, "DSA lib"},
  183. {ERR_R_X509_LIB, "X509 lib"},
  184. {ERR_R_ASN1_LIB, "ASN1 lib"},
  185. {ERR_R_CONF_LIB, "CONF lib"},
  186. {ERR_R_CRYPTO_LIB, "CRYPTO lib"},
  187. {ERR_R_EC_LIB, "EC lib"},
  188. {ERR_R_SSL_LIB, "SSL lib"},
  189. {ERR_R_BIO_LIB, "BIO lib"},
  190. {ERR_R_PKCS7_LIB, "PKCS7 lib"},
  191. {ERR_R_X509V3_LIB, "X509V3 lib"},
  192. {ERR_R_PKCS12_LIB, "PKCS12 lib"},
  193. {ERR_R_RAND_LIB, "RAND lib"},
  194. {ERR_R_DSO_LIB, "DSO lib"},
  195. {ERR_R_ENGINE_LIB, "ENGINE lib"},
  196. {ERR_R_OCSP_LIB, "OCSP lib"},
  197. {ERR_R_TS_LIB, "TS lib"},
  198. {ERR_R_NESTED_ASN1_ERROR, "nested asn1 error"},
  199. {ERR_R_BAD_ASN1_OBJECT_HEADER, "bad asn1 object header"},
  200. {ERR_R_BAD_GET_ASN1_OBJECT_CALL, "bad get asn1 object call"},
  201. {ERR_R_EXPECTING_AN_ASN1_SEQUENCE, "expecting an asn1 sequence"},
  202. {ERR_R_ASN1_LENGTH_MISMATCH, "asn1 length mismatch"},
  203. {ERR_R_MISSING_ASN1_EOS, "missing asn1 eos"},
  204. {ERR_R_FATAL, "fatal"},
  205. {ERR_R_MALLOC_FAILURE, "malloc failure"},
  206. {ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
  207. "called a function you should not call"},
  208. {ERR_R_PASSED_NULL_PARAMETER, "passed a null parameter"},
  209. {ERR_R_INTERNAL_ERROR, "internal error"},
  210. {ERR_R_DISABLED, "called a function that was disabled at compile-time"},
  211. {0, NULL},
  212. };
  213. #endif
  214. /* Define the predeclared (but externally opaque) "ERR_FNS" type */
  215. struct st_ERR_FNS {
  216. /* Works on the "error_hash" string table */
  217. LHASH_OF(ERR_STRING_DATA) *(*cb_err_get) (int create);
  218. void (*cb_err_del) (void);
  219. ERR_STRING_DATA *(*cb_err_get_item) (const ERR_STRING_DATA *);
  220. ERR_STRING_DATA *(*cb_err_set_item) (ERR_STRING_DATA *);
  221. ERR_STRING_DATA *(*cb_err_del_item) (ERR_STRING_DATA *);
  222. /* Works on the "thread_hash" error-state table */
  223. LHASH_OF(ERR_STATE) *(*cb_thread_get) (int create);
  224. void (*cb_thread_release) (LHASH_OF(ERR_STATE) **hash);
  225. ERR_STATE *(*cb_thread_get_item) (const ERR_STATE *);
  226. ERR_STATE *(*cb_thread_set_item) (ERR_STATE *);
  227. void (*cb_thread_del_item) (const ERR_STATE *);
  228. /* Returns the next available error "library" numbers */
  229. int (*cb_get_next_lib) (void);
  230. };
  231. /* Predeclarations of the "err_defaults" functions */
  232. static LHASH_OF(ERR_STRING_DATA) *int_err_get(int create);
  233. static void int_err_del(void);
  234. static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *);
  235. static ERR_STRING_DATA *int_err_set_item(ERR_STRING_DATA *);
  236. static ERR_STRING_DATA *int_err_del_item(ERR_STRING_DATA *);
  237. static LHASH_OF(ERR_STATE) *int_thread_get(int create);
  238. static void int_thread_release(LHASH_OF(ERR_STATE) **hash);
  239. static ERR_STATE *int_thread_get_item(const ERR_STATE *);
  240. static ERR_STATE *int_thread_set_item(ERR_STATE *);
  241. static void int_thread_del_item(const ERR_STATE *);
  242. static int int_err_get_next_lib(void);
  243. /* The static ERR_FNS table using these defaults functions */
  244. static const ERR_FNS err_defaults = {
  245. int_err_get,
  246. int_err_del,
  247. int_err_get_item,
  248. int_err_set_item,
  249. int_err_del_item,
  250. int_thread_get,
  251. int_thread_release,
  252. int_thread_get_item,
  253. int_thread_set_item,
  254. int_thread_del_item,
  255. int_err_get_next_lib
  256. };
  257. /* The replacable table of ERR_FNS functions we use at run-time */
  258. static const ERR_FNS *err_fns = NULL;
  259. /* Eg. rather than using "err_get()", use "ERRFN(err_get)()". */
  260. #define ERRFN(a) err_fns->cb_##a
  261. /*
  262. * The internal state used by "err_defaults" - as such, the setting, reading,
  263. * creating, and deleting of this data should only be permitted via the
  264. * "err_defaults" functions. This way, a linked module can completely defer
  265. * all ERR state operation (together with requisite locking) to the
  266. * implementations and state in the loading application.
  267. */
  268. static LHASH_OF(ERR_STRING_DATA) *int_error_hash = NULL;
  269. static LHASH_OF(ERR_STATE) *int_thread_hash = NULL;
  270. static int int_thread_hash_references = 0;
  271. static int int_err_library_number = ERR_LIB_USER;
  272. /*
  273. * Internal function that checks whether "err_fns" is set and if not, sets it
  274. * to the defaults.
  275. */
  276. static void err_fns_check(void)
  277. {
  278. if (err_fns)
  279. return;
  280. CRYPTO_w_lock(CRYPTO_LOCK_ERR);
  281. if (!err_fns)
  282. err_fns = &err_defaults;
  283. CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
  284. }
  285. /* API functions to get or set the underlying ERR functions. */
  286. const ERR_FNS *ERR_get_implementation(void)
  287. {
  288. err_fns_check();
  289. return err_fns;
  290. }
  291. int ERR_set_implementation(const ERR_FNS *fns)
  292. {
  293. int ret = 0;
  294. CRYPTO_w_lock(CRYPTO_LOCK_ERR);
  295. /*
  296. * It's too late if 'err_fns' is non-NULL. BTW: not much point setting an
  297. * error is there?!
  298. */
  299. if (!err_fns) {
  300. err_fns = fns;
  301. ret = 1;
  302. }
  303. CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
  304. return ret;
  305. }
  306. /*
  307. * These are the callbacks provided to "lh_new()" when creating the LHASH
  308. * tables internal to the "err_defaults" implementation.
  309. */
  310. static unsigned long get_error_values(int inc, int top, const char **file,
  311. int *line, const char **data,
  312. int *flags);
  313. /* The internal functions used in the "err_defaults" implementation */
  314. static unsigned long err_string_data_hash(const ERR_STRING_DATA *a)
  315. {
  316. unsigned long ret, l;
  317. l = a->error;
  318. ret = l ^ ERR_GET_LIB(l) ^ ERR_GET_FUNC(l);
  319. return (ret ^ ret % 19 * 13);
  320. }
  321. static IMPLEMENT_LHASH_HASH_FN(err_string_data, ERR_STRING_DATA)
  322. static int err_string_data_cmp(const ERR_STRING_DATA *a,
  323. const ERR_STRING_DATA *b)
  324. {
  325. return (int)(a->error - b->error);
  326. }
  327. static IMPLEMENT_LHASH_COMP_FN(err_string_data, ERR_STRING_DATA)
  328. static LHASH_OF(ERR_STRING_DATA) *int_err_get(int create)
  329. {
  330. LHASH_OF(ERR_STRING_DATA) *ret = NULL;
  331. CRYPTO_w_lock(CRYPTO_LOCK_ERR);
  332. if (!int_error_hash && create) {
  333. CRYPTO_push_info("int_err_get (err.c)");
  334. int_error_hash = lh_ERR_STRING_DATA_new();
  335. CRYPTO_pop_info();
  336. }
  337. if (int_error_hash)
  338. ret = int_error_hash;
  339. CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
  340. return ret;
  341. }
  342. static void int_err_del(void)
  343. {
  344. CRYPTO_w_lock(CRYPTO_LOCK_ERR);
  345. if (int_error_hash) {
  346. lh_ERR_STRING_DATA_free(int_error_hash);
  347. int_error_hash = NULL;
  348. }
  349. CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
  350. }
  351. static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *d)
  352. {
  353. ERR_STRING_DATA *p;
  354. LHASH_OF(ERR_STRING_DATA) *hash;
  355. err_fns_check();
  356. hash = ERRFN(err_get) (0);
  357. if (!hash)
  358. return NULL;
  359. CRYPTO_r_lock(CRYPTO_LOCK_ERR);
  360. p = lh_ERR_STRING_DATA_retrieve(hash, d);
  361. CRYPTO_r_unlock(CRYPTO_LOCK_ERR);
  362. return p;
  363. }
  364. static ERR_STRING_DATA *int_err_set_item(ERR_STRING_DATA *d)
  365. {
  366. ERR_STRING_DATA *p;
  367. LHASH_OF(ERR_STRING_DATA) *hash;
  368. err_fns_check();
  369. hash = ERRFN(err_get) (1);
  370. if (!hash)
  371. return NULL;
  372. CRYPTO_w_lock(CRYPTO_LOCK_ERR);
  373. p = lh_ERR_STRING_DATA_insert(hash, d);
  374. CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
  375. return p;
  376. }
  377. static ERR_STRING_DATA *int_err_del_item(ERR_STRING_DATA *d)
  378. {
  379. ERR_STRING_DATA *p;
  380. LHASH_OF(ERR_STRING_DATA) *hash;
  381. err_fns_check();
  382. hash = ERRFN(err_get) (0);
  383. if (!hash)
  384. return NULL;
  385. CRYPTO_w_lock(CRYPTO_LOCK_ERR);
  386. p = lh_ERR_STRING_DATA_delete(hash, d);
  387. CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
  388. return p;
  389. }
  390. static unsigned long err_state_hash(const ERR_STATE *a)
  391. {
  392. return CRYPTO_THREADID_hash(&a->tid) * 13;
  393. }
  394. static IMPLEMENT_LHASH_HASH_FN(err_state, ERR_STATE)
  395. static int err_state_cmp(const ERR_STATE *a, const ERR_STATE *b)
  396. {
  397. return CRYPTO_THREADID_cmp(&a->tid, &b->tid);
  398. }
  399. static IMPLEMENT_LHASH_COMP_FN(err_state, ERR_STATE)
  400. static LHASH_OF(ERR_STATE) *int_thread_get(int create)
  401. {
  402. LHASH_OF(ERR_STATE) *ret = NULL;
  403. CRYPTO_w_lock(CRYPTO_LOCK_ERR);
  404. if (!int_thread_hash && create) {
  405. CRYPTO_push_info("int_thread_get (err.c)");
  406. int_thread_hash = lh_ERR_STATE_new();
  407. CRYPTO_pop_info();
  408. }
  409. if (int_thread_hash) {
  410. int_thread_hash_references++;
  411. ret = int_thread_hash;
  412. }
  413. CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
  414. return ret;
  415. }
  416. static void int_thread_release(LHASH_OF(ERR_STATE) **hash)
  417. {
  418. int i;
  419. if (hash == NULL || *hash == NULL)
  420. return;
  421. i = CRYPTO_add(&int_thread_hash_references, -1, CRYPTO_LOCK_ERR);
  422. #ifdef REF_PRINT
  423. fprintf(stderr, "%4d:%s\n", int_thread_hash_references, "ERR");
  424. #endif
  425. if (i > 0)
  426. return;
  427. #ifdef REF_CHECK
  428. if (i < 0) {
  429. fprintf(stderr, "int_thread_release, bad reference count\n");
  430. abort(); /* ok */
  431. }
  432. #endif
  433. *hash = NULL;
  434. }
  435. static ERR_STATE *int_thread_get_item(const ERR_STATE *d)
  436. {
  437. ERR_STATE *p;
  438. LHASH_OF(ERR_STATE) *hash;
  439. err_fns_check();
  440. hash = ERRFN(thread_get) (0);
  441. if (!hash)
  442. return NULL;
  443. CRYPTO_r_lock(CRYPTO_LOCK_ERR);
  444. p = lh_ERR_STATE_retrieve(hash, d);
  445. CRYPTO_r_unlock(CRYPTO_LOCK_ERR);
  446. ERRFN(thread_release) (&hash);
  447. return p;
  448. }
  449. static ERR_STATE *int_thread_set_item(ERR_STATE *d)
  450. {
  451. ERR_STATE *p;
  452. LHASH_OF(ERR_STATE) *hash;
  453. err_fns_check();
  454. hash = ERRFN(thread_get) (1);
  455. if (!hash)
  456. return NULL;
  457. CRYPTO_w_lock(CRYPTO_LOCK_ERR);
  458. p = lh_ERR_STATE_insert(hash, d);
  459. CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
  460. ERRFN(thread_release) (&hash);
  461. return p;
  462. }
  463. static void int_thread_del_item(const ERR_STATE *d)
  464. {
  465. ERR_STATE *p;
  466. LHASH_OF(ERR_STATE) *hash;
  467. err_fns_check();
  468. hash = ERRFN(thread_get) (0);
  469. if (!hash)
  470. return;
  471. CRYPTO_w_lock(CRYPTO_LOCK_ERR);
  472. p = lh_ERR_STATE_delete(hash, d);
  473. /* make sure we don't leak memory */
  474. if (int_thread_hash_references == 1
  475. && int_thread_hash && lh_ERR_STATE_num_items(int_thread_hash) == 0) {
  476. lh_ERR_STATE_free(int_thread_hash);
  477. int_thread_hash = NULL;
  478. }
  479. CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
  480. ERRFN(thread_release) (&hash);
  481. if (p)
  482. ERR_STATE_free(p);
  483. }
  484. static int int_err_get_next_lib(void)
  485. {
  486. int ret;
  487. CRYPTO_w_lock(CRYPTO_LOCK_ERR);
  488. ret = int_err_library_number++;
  489. CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
  490. return ret;
  491. }
  492. #ifndef OPENSSL_NO_ERR
  493. # define NUM_SYS_STR_REASONS 127
  494. # define LEN_SYS_STR_REASON 32
  495. static ERR_STRING_DATA SYS_str_reasons[NUM_SYS_STR_REASONS + 1];
  496. /*
  497. * SYS_str_reasons is filled with copies of strerror() results at
  498. * initialization. 'errno' values up to 127 should cover all usual errors,
  499. * others will be displayed numerically by ERR_error_string. It is crucial
  500. * that we have something for each reason code that occurs in
  501. * ERR_str_reasons, or bogus reason strings will be returned for SYSerr(),
  502. * which always gets an errno value and never one of those 'standard' reason
  503. * codes.
  504. */
  505. static void build_SYS_str_reasons(void)
  506. {
  507. /* OPENSSL_malloc cannot be used here, use static storage instead */
  508. static char strerror_tab[NUM_SYS_STR_REASONS][LEN_SYS_STR_REASON];
  509. int i;
  510. static int init = 1;
  511. CRYPTO_r_lock(CRYPTO_LOCK_ERR);
  512. if (!init) {
  513. CRYPTO_r_unlock(CRYPTO_LOCK_ERR);
  514. return;
  515. }
  516. CRYPTO_r_unlock(CRYPTO_LOCK_ERR);
  517. CRYPTO_w_lock(CRYPTO_LOCK_ERR);
  518. if (!init) {
  519. CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
  520. return;
  521. }
  522. for (i = 1; i <= NUM_SYS_STR_REASONS; i++) {
  523. ERR_STRING_DATA *str = &SYS_str_reasons[i - 1];
  524. str->error = (unsigned long)i;
  525. if (str->string == NULL) {
  526. char (*dest)[LEN_SYS_STR_REASON] = &(strerror_tab[i - 1]);
  527. char *src = strerror(i);
  528. if (src != NULL) {
  529. strncpy(*dest, src, sizeof *dest);
  530. (*dest)[sizeof *dest - 1] = '\0';
  531. str->string = *dest;
  532. }
  533. }
  534. if (str->string == NULL)
  535. str->string = "unknown";
  536. }
  537. /*
  538. * Now we still have SYS_str_reasons[NUM_SYS_STR_REASONS] = {0, NULL}, as
  539. * required by ERR_load_strings.
  540. */
  541. init = 0;
  542. CRYPTO_w_unlock(CRYPTO_LOCK_ERR);
  543. }
  544. #endif
  545. #define err_clear_data(p,i) \
  546. do { \
  547. if (((p)->err_data[i] != NULL) && \
  548. (p)->err_data_flags[i] & ERR_TXT_MALLOCED) \
  549. { \
  550. OPENSSL_free((p)->err_data[i]); \
  551. (p)->err_data[i]=NULL; \
  552. } \
  553. (p)->err_data_flags[i]=0; \
  554. } while(0)
  555. #define err_clear(p,i) \
  556. do { \
  557. (p)->err_flags[i]=0; \
  558. (p)->err_buffer[i]=0; \
  559. err_clear_data(p,i); \
  560. (p)->err_file[i]=NULL; \
  561. (p)->err_line[i]= -1; \
  562. } while(0)
  563. static void ERR_STATE_free(ERR_STATE *s)
  564. {
  565. int i;
  566. if (s == NULL)
  567. return;
  568. for (i = 0; i < ERR_NUM_ERRORS; i++) {
  569. err_clear_data(s, i);
  570. }
  571. OPENSSL_free(s);
  572. }
  573. void ERR_load_ERR_strings(void)
  574. {
  575. err_fns_check();
  576. #ifndef OPENSSL_NO_ERR
  577. err_load_strings(0, ERR_str_libraries);
  578. err_load_strings(0, ERR_str_reasons);
  579. err_load_strings(ERR_LIB_SYS, ERR_str_functs);
  580. build_SYS_str_reasons();
  581. err_load_strings(ERR_LIB_SYS, SYS_str_reasons);
  582. #endif
  583. }
  584. static void err_load_strings(int lib, ERR_STRING_DATA *str)
  585. {
  586. while (str->error) {
  587. if (lib)
  588. str->error |= ERR_PACK(lib, 0, 0);
  589. ERRFN(err_set_item) (str);
  590. str++;
  591. }
  592. }
  593. void ERR_load_strings(int lib, ERR_STRING_DATA *str)
  594. {
  595. ERR_load_ERR_strings();
  596. err_load_strings(lib, str);
  597. }
  598. void ERR_unload_strings(int lib, ERR_STRING_DATA *str)
  599. {
  600. while (str->error) {
  601. if (lib)
  602. str->error |= ERR_PACK(lib, 0, 0);
  603. ERRFN(err_del_item) (str);
  604. str++;
  605. }
  606. }
  607. void ERR_free_strings(void)
  608. {
  609. err_fns_check();
  610. ERRFN(err_del) ();
  611. }
  612. /********************************************************/
  613. void ERR_put_error(int lib, int func, int reason, const char *file, int line)
  614. {
  615. ERR_STATE *es;
  616. #ifdef _OSD_POSIX
  617. /*
  618. * In the BS2000-OSD POSIX subsystem, the compiler generates path names
  619. * in the form "*POSIX(/etc/passwd)". This dirty hack strips them to
  620. * something sensible. @@@ We shouldn't modify a const string, though.
  621. */
  622. if (strncmp(file, "*POSIX(", sizeof("*POSIX(") - 1) == 0) {
  623. char *end;
  624. /* Skip the "*POSIX(" prefix */
  625. file += sizeof("*POSIX(") - 1;
  626. end = &file[strlen(file) - 1];
  627. if (*end == ')')
  628. *end = '\0';
  629. /* Optional: use the basename of the path only. */
  630. if ((end = strrchr(file, '/')) != NULL)
  631. file = &end[1];
  632. }
  633. #endif
  634. es = ERR_get_state();
  635. es->top = (es->top + 1) % ERR_NUM_ERRORS;
  636. if (es->top == es->bottom)
  637. es->bottom = (es->bottom + 1) % ERR_NUM_ERRORS;
  638. es->err_flags[es->top] = 0;
  639. es->err_buffer[es->top] = ERR_PACK(lib, func, reason);
  640. es->err_file[es->top] = file;
  641. es->err_line[es->top] = line;
  642. err_clear_data(es, es->top);
  643. }
  644. void ERR_clear_error(void)
  645. {
  646. int i;
  647. ERR_STATE *es;
  648. es = ERR_get_state();
  649. for (i = 0; i < ERR_NUM_ERRORS; i++) {
  650. err_clear(es, i);
  651. }
  652. es->top = es->bottom = 0;
  653. }
  654. unsigned long ERR_get_error(void)
  655. {
  656. return (get_error_values(1, 0, NULL, NULL, NULL, NULL));
  657. }
  658. unsigned long ERR_get_error_line(const char **file, int *line)
  659. {
  660. return (get_error_values(1, 0, file, line, NULL, NULL));
  661. }
  662. unsigned long ERR_get_error_line_data(const char **file, int *line,
  663. const char **data, int *flags)
  664. {
  665. return (get_error_values(1, 0, file, line, data, flags));
  666. }
  667. unsigned long ERR_peek_error(void)
  668. {
  669. return (get_error_values(0, 0, NULL, NULL, NULL, NULL));
  670. }
  671. unsigned long ERR_peek_error_line(const char **file, int *line)
  672. {
  673. return (get_error_values(0, 0, file, line, NULL, NULL));
  674. }
  675. unsigned long ERR_peek_error_line_data(const char **file, int *line,
  676. const char **data, int *flags)
  677. {
  678. return (get_error_values(0, 0, file, line, data, flags));
  679. }
  680. unsigned long ERR_peek_last_error(void)
  681. {
  682. return (get_error_values(0, 1, NULL, NULL, NULL, NULL));
  683. }
  684. unsigned long ERR_peek_last_error_line(const char **file, int *line)
  685. {
  686. return (get_error_values(0, 1, file, line, NULL, NULL));
  687. }
  688. unsigned long ERR_peek_last_error_line_data(const char **file, int *line,
  689. const char **data, int *flags)
  690. {
  691. return (get_error_values(0, 1, file, line, data, flags));
  692. }
  693. static unsigned long get_error_values(int inc, int top, const char **file,
  694. int *line, const char **data,
  695. int *flags)
  696. {
  697. int i = 0;
  698. ERR_STATE *es;
  699. unsigned long ret;
  700. es = ERR_get_state();
  701. if (inc && top) {
  702. if (file)
  703. *file = "";
  704. if (line)
  705. *line = 0;
  706. if (data)
  707. *data = "";
  708. if (flags)
  709. *flags = 0;
  710. return ERR_R_INTERNAL_ERROR;
  711. }
  712. if (es->bottom == es->top)
  713. return 0;
  714. if (top)
  715. i = es->top; /* last error */
  716. else
  717. i = (es->bottom + 1) % ERR_NUM_ERRORS; /* first error */
  718. ret = es->err_buffer[i];
  719. if (inc) {
  720. es->bottom = i;
  721. es->err_buffer[i] = 0;
  722. }
  723. if ((file != NULL) && (line != NULL)) {
  724. if (es->err_file[i] == NULL) {
  725. *file = "NA";
  726. if (line != NULL)
  727. *line = 0;
  728. } else {
  729. *file = es->err_file[i];
  730. if (line != NULL)
  731. *line = es->err_line[i];
  732. }
  733. }
  734. if (data == NULL) {
  735. if (inc) {
  736. err_clear_data(es, i);
  737. }
  738. } else {
  739. if (es->err_data[i] == NULL) {
  740. *data = "";
  741. if (flags != NULL)
  742. *flags = 0;
  743. } else {
  744. *data = es->err_data[i];
  745. if (flags != NULL)
  746. *flags = es->err_data_flags[i];
  747. }
  748. }
  749. return ret;
  750. }
  751. void ERR_error_string_n(unsigned long e, char *buf, size_t len)
  752. {
  753. char lsbuf[64], fsbuf[64], rsbuf[64];
  754. const char *ls, *fs, *rs;
  755. unsigned long l, f, r;
  756. if (len == 0)
  757. return;
  758. l = ERR_GET_LIB(e);
  759. f = ERR_GET_FUNC(e);
  760. r = ERR_GET_REASON(e);
  761. ls = ERR_lib_error_string(e);
  762. fs = ERR_func_error_string(e);
  763. rs = ERR_reason_error_string(e);
  764. if (ls == NULL)
  765. BIO_snprintf(lsbuf, sizeof(lsbuf), "lib(%lu)", l);
  766. if (fs == NULL)
  767. BIO_snprintf(fsbuf, sizeof(fsbuf), "func(%lu)", f);
  768. if (rs == NULL)
  769. BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", r);
  770. BIO_snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls ? ls : lsbuf,
  771. fs ? fs : fsbuf, rs ? rs : rsbuf);
  772. if (strlen(buf) == len - 1) {
  773. /*
  774. * output may be truncated; make sure we always have 5
  775. * colon-separated fields, i.e. 4 colons ...
  776. */
  777. #define NUM_COLONS 4
  778. if (len > NUM_COLONS) { /* ... if possible */
  779. int i;
  780. char *s = buf;
  781. for (i = 0; i < NUM_COLONS; i++) {
  782. char *colon = strchr(s, ':');
  783. if (colon == NULL || colon > &buf[len - 1] - NUM_COLONS + i) {
  784. /*
  785. * set colon no. i at last possible position (buf[len-1]
  786. * is the terminating 0)
  787. */
  788. colon = &buf[len - 1] - NUM_COLONS + i;
  789. *colon = ':';
  790. }
  791. s = colon + 1;
  792. }
  793. }
  794. }
  795. }
  796. /* BAD for multi-threading: uses a local buffer if ret == NULL */
  797. /*
  798. * ERR_error_string_n should be used instead for ret != NULL as
  799. * ERR_error_string cannot know how large the buffer is
  800. */
  801. char *ERR_error_string(unsigned long e, char *ret)
  802. {
  803. static char buf[256];
  804. if (ret == NULL)
  805. ret = buf;
  806. ERR_error_string_n(e, ret, 256);
  807. return ret;
  808. }
  809. LHASH_OF(ERR_STRING_DATA) *ERR_get_string_table(void)
  810. {
  811. err_fns_check();
  812. return ERRFN(err_get) (0);
  813. }
  814. LHASH_OF(ERR_STATE) *ERR_get_err_state_table(void)
  815. {
  816. err_fns_check();
  817. return ERRFN(thread_get) (0);
  818. }
  819. void ERR_release_err_state_table(LHASH_OF(ERR_STATE) **hash)
  820. {
  821. err_fns_check();
  822. ERRFN(thread_release) (hash);
  823. }
  824. const char *ERR_lib_error_string(unsigned long e)
  825. {
  826. ERR_STRING_DATA d, *p;
  827. unsigned long l;
  828. err_fns_check();
  829. l = ERR_GET_LIB(e);
  830. d.error = ERR_PACK(l, 0, 0);
  831. p = ERRFN(err_get_item) (&d);
  832. return ((p == NULL) ? NULL : p->string);
  833. }
  834. const char *ERR_func_error_string(unsigned long e)
  835. {
  836. ERR_STRING_DATA d, *p;
  837. unsigned long l, f;
  838. err_fns_check();
  839. l = ERR_GET_LIB(e);
  840. f = ERR_GET_FUNC(e);
  841. d.error = ERR_PACK(l, f, 0);
  842. p = ERRFN(err_get_item) (&d);
  843. return ((p == NULL) ? NULL : p->string);
  844. }
  845. const char *ERR_reason_error_string(unsigned long e)
  846. {
  847. ERR_STRING_DATA d, *p = NULL;
  848. unsigned long l, r;
  849. err_fns_check();
  850. l = ERR_GET_LIB(e);
  851. r = ERR_GET_REASON(e);
  852. d.error = ERR_PACK(l, 0, r);
  853. p = ERRFN(err_get_item) (&d);
  854. if (!p) {
  855. d.error = ERR_PACK(0, 0, r);
  856. p = ERRFN(err_get_item) (&d);
  857. }
  858. return ((p == NULL) ? NULL : p->string);
  859. }
  860. void ERR_remove_thread_state(const CRYPTO_THREADID *id)
  861. {
  862. ERR_STATE tmp;
  863. if (id)
  864. CRYPTO_THREADID_cpy(&tmp.tid, id);
  865. else
  866. CRYPTO_THREADID_current(&tmp.tid);
  867. err_fns_check();
  868. /*
  869. * thread_del_item automatically destroys the LHASH if the number of
  870. * items reaches zero.
  871. */
  872. ERRFN(thread_del_item) (&tmp);
  873. }
  874. #ifndef OPENSSL_NO_DEPRECATED
  875. void ERR_remove_state(unsigned long pid)
  876. {
  877. ERR_remove_thread_state(NULL);
  878. }
  879. #endif
  880. ERR_STATE *ERR_get_state(void)
  881. {
  882. static ERR_STATE fallback;
  883. ERR_STATE *ret, tmp, *tmpp = NULL;
  884. int i;
  885. CRYPTO_THREADID tid;
  886. err_fns_check();
  887. CRYPTO_THREADID_current(&tid);
  888. CRYPTO_THREADID_cpy(&tmp.tid, &tid);
  889. ret = ERRFN(thread_get_item) (&tmp);
  890. /* ret == the error state, if NULL, make a new one */
  891. if (ret == NULL) {
  892. ret = (ERR_STATE *)OPENSSL_malloc(sizeof(ERR_STATE));
  893. if (ret == NULL)
  894. return (&fallback);
  895. CRYPTO_THREADID_cpy(&ret->tid, &tid);
  896. ret->top = 0;
  897. ret->bottom = 0;
  898. for (i = 0; i < ERR_NUM_ERRORS; i++) {
  899. ret->err_data[i] = NULL;
  900. ret->err_data_flags[i] = 0;
  901. }
  902. tmpp = ERRFN(thread_set_item) (ret);
  903. /* To check if insertion failed, do a get. */
  904. if (ERRFN(thread_get_item) (ret) != ret) {
  905. ERR_STATE_free(ret); /* could not insert it */
  906. return (&fallback);
  907. }
  908. /*
  909. * If a race occured in this function and we came second, tmpp is the
  910. * first one that we just replaced.
  911. */
  912. if (tmpp)
  913. ERR_STATE_free(tmpp);
  914. }
  915. return ret;
  916. }
  917. int ERR_get_next_error_library(void)
  918. {
  919. err_fns_check();
  920. return ERRFN(get_next_lib) ();
  921. }
  922. void ERR_set_error_data(char *data, int flags)
  923. {
  924. ERR_STATE *es;
  925. int i;
  926. es = ERR_get_state();
  927. i = es->top;
  928. if (i == 0)
  929. i = ERR_NUM_ERRORS - 1;
  930. err_clear_data(es, i);
  931. es->err_data[i] = data;
  932. es->err_data_flags[i] = flags;
  933. }
  934. void ERR_add_error_data(int num, ...)
  935. {
  936. va_list args;
  937. va_start(args, num);
  938. ERR_add_error_vdata(num, args);
  939. va_end(args);
  940. }
  941. void ERR_add_error_vdata(int num, va_list args)
  942. {
  943. int i, n, s;
  944. char *str, *p, *a;
  945. s = 80;
  946. str = OPENSSL_malloc(s + 1);
  947. if (str == NULL)
  948. return;
  949. str[0] = '\0';
  950. n = 0;
  951. for (i = 0; i < num; i++) {
  952. a = va_arg(args, char *);
  953. /* ignore NULLs, thanks to Bob Beck <beck@obtuse.com> */
  954. if (a != NULL) {
  955. n += strlen(a);
  956. if (n > s) {
  957. s = n + 20;
  958. p = OPENSSL_realloc(str, s + 1);
  959. if (p == NULL) {
  960. OPENSSL_free(str);
  961. return;
  962. } else
  963. str = p;
  964. }
  965. BUF_strlcat(str, a, (size_t)s + 1);
  966. }
  967. }
  968. ERR_set_error_data(str, ERR_TXT_MALLOCED | ERR_TXT_STRING);
  969. }
  970. int ERR_set_mark(void)
  971. {
  972. ERR_STATE *es;
  973. es = ERR_get_state();
  974. if (es->bottom == es->top)
  975. return 0;
  976. es->err_flags[es->top] |= ERR_FLAG_MARK;
  977. return 1;
  978. }
  979. int ERR_pop_to_mark(void)
  980. {
  981. ERR_STATE *es;
  982. es = ERR_get_state();
  983. while (es->bottom != es->top
  984. && (es->err_flags[es->top] & ERR_FLAG_MARK) == 0) {
  985. err_clear(es, es->top);
  986. es->top -= 1;
  987. if (es->top == -1)
  988. es->top = ERR_NUM_ERRORS - 1;
  989. }
  990. if (es->bottom == es->top)
  991. return 0;
  992. es->err_flags[es->top] &= ~ERR_FLAG_MARK;
  993. return 1;
  994. }