err.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  1. /* crypto/err/err.c */
  2. /* Copyright (C) 1995-1998 Eric Young ([email protected])
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young ([email protected]).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson ([email protected]).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young ([email protected])"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson ([email protected])"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * [email protected].
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * ([email protected]). This product includes software written by Tim
  108. * Hudson ([email protected]).
  109. *
  110. */
  111. #include <stdio.h>
  112. #include <stdarg.h>
  113. #include <string.h>
  114. #include "cryptlib.h"
  115. #include <openssl/lhash.h>
  116. #include <openssl/crypto.h>
  117. #include <openssl/buffer.h>
  118. #include <openssl/bio.h>
  119. #include <openssl/err.h>
  120. #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. if (es->bottom == es->top)
  720. return 0;
  721. if (top)
  722. i = es->top; /* last error */
  723. else
  724. i = (es->bottom + 1) % ERR_NUM_ERRORS; /* first error */
  725. ret = es->err_buffer[i];
  726. if (inc) {
  727. es->bottom = i;
  728. es->err_buffer[i] = 0;
  729. }
  730. if ((file != NULL) && (line != NULL)) {
  731. if (es->err_file[i] == NULL) {
  732. *file = "NA";
  733. if (line != NULL)
  734. *line = 0;
  735. } else {
  736. *file = es->err_file[i];
  737. if (line != NULL)
  738. *line = es->err_line[i];
  739. }
  740. }
  741. if (data == NULL) {
  742. if (inc) {
  743. err_clear_data(es, i);
  744. }
  745. } else {
  746. if (es->err_data[i] == NULL) {
  747. *data = "";
  748. if (flags != NULL)
  749. *flags = 0;
  750. } else {
  751. *data = es->err_data[i];
  752. if (flags != NULL)
  753. *flags = es->err_data_flags[i];
  754. }
  755. }
  756. return ret;
  757. }
  758. void ERR_error_string_n(unsigned long e, char *buf, size_t len)
  759. {
  760. char lsbuf[64], fsbuf[64], rsbuf[64];
  761. const char *ls, *fs, *rs;
  762. unsigned long l, f, r;
  763. if (len == 0)
  764. return;
  765. l = ERR_GET_LIB(e);
  766. f = ERR_GET_FUNC(e);
  767. r = ERR_GET_REASON(e);
  768. ls = ERR_lib_error_string(e);
  769. fs = ERR_func_error_string(e);
  770. rs = ERR_reason_error_string(e);
  771. if (ls == NULL)
  772. BIO_snprintf(lsbuf, sizeof(lsbuf), "lib(%lu)", l);
  773. if (fs == NULL)
  774. BIO_snprintf(fsbuf, sizeof(fsbuf), "func(%lu)", f);
  775. if (rs == NULL)
  776. BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", r);
  777. BIO_snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls ? ls : lsbuf,
  778. fs ? fs : fsbuf, rs ? rs : rsbuf);
  779. if (strlen(buf) == len - 1) {
  780. /*
  781. * output may be truncated; make sure we always have 5
  782. * colon-separated fields, i.e. 4 colons ...
  783. */
  784. #define NUM_COLONS 4
  785. if (len > NUM_COLONS) { /* ... if possible */
  786. int i;
  787. char *s = buf;
  788. for (i = 0; i < NUM_COLONS; i++) {
  789. char *colon = strchr(s, ':');
  790. if (colon == NULL || colon > &buf[len - 1] - NUM_COLONS + i) {
  791. /*
  792. * set colon no. i at last possible position (buf[len-1]
  793. * is the terminating 0)
  794. */
  795. colon = &buf[len - 1] - NUM_COLONS + i;
  796. *colon = ':';
  797. }
  798. s = colon + 1;
  799. }
  800. }
  801. }
  802. }
  803. /* BAD for multi-threading: uses a local buffer if ret == NULL */
  804. /*
  805. * ERR_error_string_n should be used instead for ret != NULL as
  806. * ERR_error_string cannot know how large the buffer is
  807. */
  808. char *ERR_error_string(unsigned long e, char *ret)
  809. {
  810. static char buf[256];
  811. if (ret == NULL)
  812. ret = buf;
  813. ERR_error_string_n(e, ret, 256);
  814. return ret;
  815. }
  816. LHASH_OF(ERR_STRING_DATA) *ERR_get_string_table(void)
  817. {
  818. err_fns_check();
  819. return ERRFN(err_get) (0);
  820. }
  821. LHASH_OF(ERR_STATE) *ERR_get_err_state_table(void)
  822. {
  823. err_fns_check();
  824. return ERRFN(thread_get) (0);
  825. }
  826. void ERR_release_err_state_table(LHASH_OF(ERR_STATE) **hash)
  827. {
  828. err_fns_check();
  829. ERRFN(thread_release) (hash);
  830. }
  831. const char *ERR_lib_error_string(unsigned long e)
  832. {
  833. ERR_STRING_DATA d, *p;
  834. unsigned long l;
  835. err_fns_check();
  836. l = ERR_GET_LIB(e);
  837. d.error = ERR_PACK(l, 0, 0);
  838. p = ERRFN(err_get_item) (&d);
  839. return ((p == NULL) ? NULL : p->string);
  840. }
  841. const char *ERR_func_error_string(unsigned long e)
  842. {
  843. ERR_STRING_DATA d, *p;
  844. unsigned long l, f;
  845. err_fns_check();
  846. l = ERR_GET_LIB(e);
  847. f = ERR_GET_FUNC(e);
  848. d.error = ERR_PACK(l, f, 0);
  849. p = ERRFN(err_get_item) (&d);
  850. return ((p == NULL) ? NULL : p->string);
  851. }
  852. const char *ERR_reason_error_string(unsigned long e)
  853. {
  854. ERR_STRING_DATA d, *p = NULL;
  855. unsigned long l, r;
  856. err_fns_check();
  857. l = ERR_GET_LIB(e);
  858. r = ERR_GET_REASON(e);
  859. d.error = ERR_PACK(l, 0, r);
  860. p = ERRFN(err_get_item) (&d);
  861. if (!p) {
  862. d.error = ERR_PACK(0, 0, r);
  863. p = ERRFN(err_get_item) (&d);
  864. }
  865. return ((p == NULL) ? NULL : p->string);
  866. }
  867. void ERR_remove_thread_state(const CRYPTO_THREADID *id)
  868. {
  869. ERR_STATE tmp;
  870. if (id)
  871. CRYPTO_THREADID_cpy(&tmp.tid, id);
  872. else
  873. CRYPTO_THREADID_current(&tmp.tid);
  874. err_fns_check();
  875. /*
  876. * thread_del_item automatically destroys the LHASH if the number of
  877. * items reaches zero.
  878. */
  879. ERRFN(thread_del_item) (&tmp);
  880. }
  881. #ifndef OPENSSL_NO_DEPRECATED
  882. void ERR_remove_state(unsigned long pid)
  883. {
  884. ERR_remove_thread_state(NULL);
  885. }
  886. #endif
  887. ERR_STATE *ERR_get_state(void)
  888. {
  889. ERR_STATE *ret, tmp, *tmpp = NULL;
  890. int i;
  891. CRYPTO_THREADID tid;
  892. err_fns_check();
  893. CRYPTO_THREADID_current(&tid);
  894. CRYPTO_THREADID_cpy(&tmp.tid, &tid);
  895. ret = ERRFN(thread_get_item) (&tmp);
  896. /* ret == the error state, if NULL, make a new one */
  897. if (ret == NULL) {
  898. ret = (ERR_STATE *)OPENSSL_malloc(sizeof(ERR_STATE));
  899. if (ret == NULL)
  900. return NULL;
  901. CRYPTO_THREADID_cpy(&ret->tid, &tid);
  902. ret->top = 0;
  903. ret->bottom = 0;
  904. for (i = 0; i < ERR_NUM_ERRORS; i++) {
  905. ret->err_data[i] = NULL;
  906. ret->err_data_flags[i] = 0;
  907. }
  908. tmpp = ERRFN(thread_set_item) (ret);
  909. /* To check if insertion failed, do a get. */
  910. if (ERRFN(thread_get_item) (ret) != ret) {
  911. ERR_STATE_free(ret); /* could not insert it */
  912. return NULL;
  913. }
  914. /*
  915. * If a race occured in this function and we came second, tmpp is the
  916. * first one that we just replaced.
  917. */
  918. if (tmpp)
  919. ERR_STATE_free(tmpp);
  920. }
  921. return ret;
  922. }
  923. int ERR_get_next_error_library(void)
  924. {
  925. err_fns_check();
  926. return ERRFN(get_next_lib) ();
  927. }
  928. void ERR_set_error_data(char *data, int flags)
  929. {
  930. ERR_STATE *es;
  931. int i;
  932. es = ERR_get_state();
  933. if (es == NULL)
  934. return;
  935. i = es->top;
  936. err_clear_data(es, i);
  937. es->err_data[i] = data;
  938. es->err_data_flags[i] = flags;
  939. }
  940. void ERR_add_error_data(int num, ...)
  941. {
  942. va_list args;
  943. va_start(args, num);
  944. ERR_add_error_vdata(num, args);
  945. va_end(args);
  946. }
  947. void ERR_add_error_vdata(int num, va_list args)
  948. {
  949. int i, n, s;
  950. char *str, *p, *a;
  951. s = 80;
  952. str = OPENSSL_malloc(s + 1);
  953. if (str == NULL)
  954. return;
  955. str[0] = '\0';
  956. n = 0;
  957. for (i = 0; i < num; i++) {
  958. a = va_arg(args, char *);
  959. /* ignore NULLs, thanks to Bob Beck <[email protected]> */
  960. if (a != NULL) {
  961. n += strlen(a);
  962. if (n > s) {
  963. s = n + 20;
  964. p = OPENSSL_realloc(str, s + 1);
  965. if (p == NULL) {
  966. OPENSSL_free(str);
  967. return;
  968. } else
  969. str = p;
  970. }
  971. BUF_strlcat(str, a, (size_t)s + 1);
  972. }
  973. }
  974. ERR_set_error_data(str, ERR_TXT_MALLOCED | ERR_TXT_STRING);
  975. }
  976. int ERR_set_mark(void)
  977. {
  978. ERR_STATE *es;
  979. es = ERR_get_state();
  980. if (es == NULL)
  981. return 0;
  982. if (es->bottom == es->top)
  983. return 0;
  984. es->err_flags[es->top] |= ERR_FLAG_MARK;
  985. return 1;
  986. }
  987. int ERR_pop_to_mark(void)
  988. {
  989. ERR_STATE *es;
  990. es = ERR_get_state();
  991. if (es == NULL)
  992. return 0;
  993. while (es->bottom != es->top
  994. && (es->err_flags[es->top] & ERR_FLAG_MARK) == 0) {
  995. err_clear(es, es->top);
  996. es->top -= 1;
  997. if (es->top == -1)
  998. es->top = ERR_NUM_ERRORS - 1;
  999. }
  1000. if (es->bottom == es->top)
  1001. return 0;
  1002. es->err_flags[es->top] &= ~ERR_FLAG_MARK;
  1003. return 1;
  1004. }
  1005. #ifdef UINTPTR_T
  1006. # undef UINTPTR_T
  1007. #endif
  1008. /*
  1009. * uintptr_t is the answer, but unformtunately we can't assume that all
  1010. * compilers supported by 1.0.2 have it :-(
  1011. */
  1012. #if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE==64
  1013. /*
  1014. * But we can't use size_t on VMS, because it adheres to sizeof(size_t)==4
  1015. * even in 64-bit builds, which means that it won't work as mask.
  1016. */
  1017. # define UINTPTR_T unsigned long long
  1018. #else
  1019. # define UINTPTR_T size_t
  1020. #endif
  1021. void err_clear_last_constant_time(int clear)
  1022. {
  1023. ERR_STATE *es;
  1024. int top;
  1025. es = ERR_get_state();
  1026. if (es == NULL)
  1027. return;
  1028. top = es->top;
  1029. es->err_flags[top] &= ~(0 - clear);
  1030. es->err_buffer[top] &= ~(0UL - clear);
  1031. es->err_file[top] = (const char *)((UINTPTR_T)es->err_file[top] &
  1032. ~((UINTPTR_T)0 - clear));
  1033. es->err_line[top] |= 0 - clear;
  1034. es->top = (top + ERR_NUM_ERRORS - clear) % ERR_NUM_ERRORS;
  1035. }