err.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  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. if (es == NULL)
  636. return;
  637. es->top = (es->top + 1) % ERR_NUM_ERRORS;
  638. if (es->top == es->bottom)
  639. es->bottom = (es->bottom + 1) % ERR_NUM_ERRORS;
  640. es->err_flags[es->top] = 0;
  641. es->err_buffer[es->top] = ERR_PACK(lib, func, reason);
  642. es->err_file[es->top] = file;
  643. es->err_line[es->top] = line;
  644. err_clear_data(es, es->top);
  645. }
  646. void ERR_clear_error(void)
  647. {
  648. int i;
  649. ERR_STATE *es;
  650. es = ERR_get_state();
  651. if (es == NULL)
  652. return;
  653. for (i = 0; i < ERR_NUM_ERRORS; i++) {
  654. err_clear(es, i);
  655. }
  656. es->top = es->bottom = 0;
  657. }
  658. unsigned long ERR_get_error(void)
  659. {
  660. return (get_error_values(1, 0, NULL, NULL, NULL, NULL));
  661. }
  662. unsigned long ERR_get_error_line(const char **file, int *line)
  663. {
  664. return (get_error_values(1, 0, file, line, NULL, NULL));
  665. }
  666. unsigned long ERR_get_error_line_data(const char **file, int *line,
  667. const char **data, int *flags)
  668. {
  669. return (get_error_values(1, 0, file, line, data, flags));
  670. }
  671. unsigned long ERR_peek_error(void)
  672. {
  673. return (get_error_values(0, 0, NULL, NULL, NULL, NULL));
  674. }
  675. unsigned long ERR_peek_error_line(const char **file, int *line)
  676. {
  677. return (get_error_values(0, 0, file, line, NULL, NULL));
  678. }
  679. unsigned long ERR_peek_error_line_data(const char **file, int *line,
  680. const char **data, int *flags)
  681. {
  682. return (get_error_values(0, 0, file, line, data, flags));
  683. }
  684. unsigned long ERR_peek_last_error(void)
  685. {
  686. return (get_error_values(0, 1, NULL, NULL, NULL, NULL));
  687. }
  688. unsigned long ERR_peek_last_error_line(const char **file, int *line)
  689. {
  690. return (get_error_values(0, 1, file, line, NULL, NULL));
  691. }
  692. unsigned long ERR_peek_last_error_line_data(const char **file, int *line,
  693. const char **data, int *flags)
  694. {
  695. return (get_error_values(0, 1, file, line, data, flags));
  696. }
  697. static unsigned long get_error_values(int inc, int top, const char **file,
  698. int *line, const char **data,
  699. int *flags)
  700. {
  701. int i = 0;
  702. ERR_STATE *es;
  703. unsigned long ret;
  704. es = ERR_get_state();
  705. if (es == NULL)
  706. return 0;
  707. if (inc && top) {
  708. if (file)
  709. *file = "";
  710. if (line)
  711. *line = 0;
  712. if (data)
  713. *data = "";
  714. if (flags)
  715. *flags = 0;
  716. return ERR_R_INTERNAL_ERROR;
  717. }
  718. if (es->bottom == es->top)
  719. return 0;
  720. if (top)
  721. i = es->top; /* last error */
  722. else
  723. i = (es->bottom + 1) % ERR_NUM_ERRORS; /* first error */
  724. ret = es->err_buffer[i];
  725. if (inc) {
  726. es->bottom = i;
  727. es->err_buffer[i] = 0;
  728. }
  729. if ((file != NULL) && (line != NULL)) {
  730. if (es->err_file[i] == NULL) {
  731. *file = "NA";
  732. if (line != NULL)
  733. *line = 0;
  734. } else {
  735. *file = es->err_file[i];
  736. if (line != NULL)
  737. *line = es->err_line[i];
  738. }
  739. }
  740. if (data == NULL) {
  741. if (inc) {
  742. err_clear_data(es, i);
  743. }
  744. } else {
  745. if (es->err_data[i] == NULL) {
  746. *data = "";
  747. if (flags != NULL)
  748. *flags = 0;
  749. } else {
  750. *data = es->err_data[i];
  751. if (flags != NULL)
  752. *flags = es->err_data_flags[i];
  753. }
  754. }
  755. return ret;
  756. }
  757. void ERR_error_string_n(unsigned long e, char *buf, size_t len)
  758. {
  759. char lsbuf[64], fsbuf[64], rsbuf[64];
  760. const char *ls, *fs, *rs;
  761. unsigned long l, f, r;
  762. if (len == 0)
  763. return;
  764. l = ERR_GET_LIB(e);
  765. f = ERR_GET_FUNC(e);
  766. r = ERR_GET_REASON(e);
  767. ls = ERR_lib_error_string(e);
  768. fs = ERR_func_error_string(e);
  769. rs = ERR_reason_error_string(e);
  770. if (ls == NULL)
  771. BIO_snprintf(lsbuf, sizeof(lsbuf), "lib(%lu)", l);
  772. if (fs == NULL)
  773. BIO_snprintf(fsbuf, sizeof(fsbuf), "func(%lu)", f);
  774. if (rs == NULL)
  775. BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", r);
  776. BIO_snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls ? ls : lsbuf,
  777. fs ? fs : fsbuf, rs ? rs : rsbuf);
  778. if (strlen(buf) == len - 1) {
  779. /*
  780. * output may be truncated; make sure we always have 5
  781. * colon-separated fields, i.e. 4 colons ...
  782. */
  783. #define NUM_COLONS 4
  784. if (len > NUM_COLONS) { /* ... if possible */
  785. int i;
  786. char *s = buf;
  787. for (i = 0; i < NUM_COLONS; i++) {
  788. char *colon = strchr(s, ':');
  789. if (colon == NULL || colon > &buf[len - 1] - NUM_COLONS + i) {
  790. /*
  791. * set colon no. i at last possible position (buf[len-1]
  792. * is the terminating 0)
  793. */
  794. colon = &buf[len - 1] - NUM_COLONS + i;
  795. *colon = ':';
  796. }
  797. s = colon + 1;
  798. }
  799. }
  800. }
  801. }
  802. /* BAD for multi-threading: uses a local buffer if ret == NULL */
  803. /*
  804. * ERR_error_string_n should be used instead for ret != NULL as
  805. * ERR_error_string cannot know how large the buffer is
  806. */
  807. char *ERR_error_string(unsigned long e, char *ret)
  808. {
  809. static char buf[256];
  810. if (ret == NULL)
  811. ret = buf;
  812. ERR_error_string_n(e, ret, 256);
  813. return ret;
  814. }
  815. LHASH_OF(ERR_STRING_DATA) *ERR_get_string_table(void)
  816. {
  817. err_fns_check();
  818. return ERRFN(err_get) (0);
  819. }
  820. LHASH_OF(ERR_STATE) *ERR_get_err_state_table(void)
  821. {
  822. err_fns_check();
  823. return ERRFN(thread_get) (0);
  824. }
  825. void ERR_release_err_state_table(LHASH_OF(ERR_STATE) **hash)
  826. {
  827. err_fns_check();
  828. ERRFN(thread_release) (hash);
  829. }
  830. const char *ERR_lib_error_string(unsigned long e)
  831. {
  832. ERR_STRING_DATA d, *p;
  833. unsigned long l;
  834. err_fns_check();
  835. l = ERR_GET_LIB(e);
  836. d.error = ERR_PACK(l, 0, 0);
  837. p = ERRFN(err_get_item) (&d);
  838. return ((p == NULL) ? NULL : p->string);
  839. }
  840. const char *ERR_func_error_string(unsigned long e)
  841. {
  842. ERR_STRING_DATA d, *p;
  843. unsigned long l, f;
  844. err_fns_check();
  845. l = ERR_GET_LIB(e);
  846. f = ERR_GET_FUNC(e);
  847. d.error = ERR_PACK(l, f, 0);
  848. p = ERRFN(err_get_item) (&d);
  849. return ((p == NULL) ? NULL : p->string);
  850. }
  851. const char *ERR_reason_error_string(unsigned long e)
  852. {
  853. ERR_STRING_DATA d, *p = NULL;
  854. unsigned long l, r;
  855. err_fns_check();
  856. l = ERR_GET_LIB(e);
  857. r = ERR_GET_REASON(e);
  858. d.error = ERR_PACK(l, 0, r);
  859. p = ERRFN(err_get_item) (&d);
  860. if (!p) {
  861. d.error = ERR_PACK(0, 0, r);
  862. p = ERRFN(err_get_item) (&d);
  863. }
  864. return ((p == NULL) ? NULL : p->string);
  865. }
  866. void ERR_remove_thread_state(const CRYPTO_THREADID *id)
  867. {
  868. ERR_STATE tmp;
  869. if (id)
  870. CRYPTO_THREADID_cpy(&tmp.tid, id);
  871. else
  872. CRYPTO_THREADID_current(&tmp.tid);
  873. err_fns_check();
  874. /*
  875. * thread_del_item automatically destroys the LHASH if the number of
  876. * items reaches zero.
  877. */
  878. ERRFN(thread_del_item) (&tmp);
  879. }
  880. #ifndef OPENSSL_NO_DEPRECATED
  881. void ERR_remove_state(unsigned long pid)
  882. {
  883. ERR_remove_thread_state(NULL);
  884. }
  885. #endif
  886. ERR_STATE *ERR_get_state(void)
  887. {
  888. ERR_STATE *ret, tmp, *tmpp = NULL;
  889. int i;
  890. CRYPTO_THREADID tid;
  891. err_fns_check();
  892. CRYPTO_THREADID_current(&tid);
  893. CRYPTO_THREADID_cpy(&tmp.tid, &tid);
  894. ret = ERRFN(thread_get_item) (&tmp);
  895. /* ret == the error state, if NULL, make a new one */
  896. if (ret == NULL) {
  897. ret = (ERR_STATE *)OPENSSL_malloc(sizeof(ERR_STATE));
  898. if (ret == NULL)
  899. return NULL;
  900. CRYPTO_THREADID_cpy(&ret->tid, &tid);
  901. ret->top = 0;
  902. ret->bottom = 0;
  903. for (i = 0; i < ERR_NUM_ERRORS; i++) {
  904. ret->err_data[i] = NULL;
  905. ret->err_data_flags[i] = 0;
  906. }
  907. tmpp = ERRFN(thread_set_item) (ret);
  908. /* To check if insertion failed, do a get. */
  909. if (ERRFN(thread_get_item) (ret) != ret) {
  910. ERR_STATE_free(ret); /* could not insert it */
  911. return NULL;
  912. }
  913. /*
  914. * If a race occured in this function and we came second, tmpp is the
  915. * first one that we just replaced.
  916. */
  917. if (tmpp)
  918. ERR_STATE_free(tmpp);
  919. }
  920. return ret;
  921. }
  922. int ERR_get_next_error_library(void)
  923. {
  924. err_fns_check();
  925. return ERRFN(get_next_lib) ();
  926. }
  927. void ERR_set_error_data(char *data, int flags)
  928. {
  929. ERR_STATE *es;
  930. int i;
  931. es = ERR_get_state();
  932. if (es == NULL)
  933. return;
  934. i = es->top;
  935. if (i == 0)
  936. i = ERR_NUM_ERRORS - 1;
  937. err_clear_data(es, i);
  938. es->err_data[i] = data;
  939. es->err_data_flags[i] = flags;
  940. }
  941. void ERR_add_error_data(int num, ...)
  942. {
  943. va_list args;
  944. va_start(args, num);
  945. ERR_add_error_vdata(num, args);
  946. va_end(args);
  947. }
  948. void ERR_add_error_vdata(int num, va_list args)
  949. {
  950. int i, n, s;
  951. char *str, *p, *a;
  952. s = 80;
  953. str = OPENSSL_malloc(s + 1);
  954. if (str == NULL)
  955. return;
  956. str[0] = '\0';
  957. n = 0;
  958. for (i = 0; i < num; i++) {
  959. a = va_arg(args, char *);
  960. /* ignore NULLs, thanks to Bob Beck <beck@obtuse.com> */
  961. if (a != NULL) {
  962. n += strlen(a);
  963. if (n > s) {
  964. s = n + 20;
  965. p = OPENSSL_realloc(str, s + 1);
  966. if (p == NULL) {
  967. OPENSSL_free(str);
  968. return;
  969. } else
  970. str = p;
  971. }
  972. BUF_strlcat(str, a, (size_t)s + 1);
  973. }
  974. }
  975. ERR_set_error_data(str, ERR_TXT_MALLOCED | ERR_TXT_STRING);
  976. }
  977. int ERR_set_mark(void)
  978. {
  979. ERR_STATE *es;
  980. es = ERR_get_state();
  981. if (es == NULL)
  982. return 0;
  983. if (es->bottom == es->top)
  984. return 0;
  985. es->err_flags[es->top] |= ERR_FLAG_MARK;
  986. return 1;
  987. }
  988. int ERR_pop_to_mark(void)
  989. {
  990. ERR_STATE *es;
  991. es = ERR_get_state();
  992. if (es == NULL)
  993. return 0;
  994. while (es->bottom != es->top
  995. && (es->err_flags[es->top] & ERR_FLAG_MARK) == 0) {
  996. err_clear(es, es->top);
  997. es->top -= 1;
  998. if (es->top == -1)
  999. es->top = ERR_NUM_ERRORS - 1;
  1000. }
  1001. if (es->bottom == es->top)
  1002. return 0;
  1003. es->err_flags[es->top] &= ~ERR_FLAG_MARK;
  1004. return 1;
  1005. }