err.c 29 KB

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