err.c 34 KB

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