err.c 33 KB

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