err.c 29 KB

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