2
0

err.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. /*
  2. * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <stdarg.h>
  11. #include <string.h>
  12. #include "crypto/cryptlib.h"
  13. #include "internal/err.h"
  14. #include "crypto/err.h"
  15. #include <openssl/err.h>
  16. #include <openssl/crypto.h>
  17. #include <openssl/buffer.h>
  18. #include <openssl/bio.h>
  19. #include <openssl/opensslconf.h>
  20. #include "internal/thread_once.h"
  21. #include "crypto/ctype.h"
  22. #include "internal/constant_time.h"
  23. #include "e_os.h"
  24. static int err_load_strings(const ERR_STRING_DATA *str);
  25. static void ERR_STATE_free(ERR_STATE *s);
  26. #ifndef OPENSSL_NO_ERR
  27. static ERR_STRING_DATA ERR_str_libraries[] = {
  28. {ERR_PACK(ERR_LIB_NONE, 0, 0), "unknown library"},
  29. {ERR_PACK(ERR_LIB_SYS, 0, 0), "system library"},
  30. {ERR_PACK(ERR_LIB_BN, 0, 0), "bignum routines"},
  31. {ERR_PACK(ERR_LIB_RSA, 0, 0), "rsa routines"},
  32. {ERR_PACK(ERR_LIB_DH, 0, 0), "Diffie-Hellman routines"},
  33. {ERR_PACK(ERR_LIB_EVP, 0, 0), "digital envelope routines"},
  34. {ERR_PACK(ERR_LIB_BUF, 0, 0), "memory buffer routines"},
  35. {ERR_PACK(ERR_LIB_OBJ, 0, 0), "object identifier routines"},
  36. {ERR_PACK(ERR_LIB_PEM, 0, 0), "PEM routines"},
  37. {ERR_PACK(ERR_LIB_DSA, 0, 0), "dsa routines"},
  38. {ERR_PACK(ERR_LIB_X509, 0, 0), "x509 certificate routines"},
  39. {ERR_PACK(ERR_LIB_ASN1, 0, 0), "asn1 encoding routines"},
  40. {ERR_PACK(ERR_LIB_CONF, 0, 0), "configuration file routines"},
  41. {ERR_PACK(ERR_LIB_CRYPTO, 0, 0), "common libcrypto routines"},
  42. {ERR_PACK(ERR_LIB_EC, 0, 0), "elliptic curve routines"},
  43. {ERR_PACK(ERR_LIB_ECDSA, 0, 0), "ECDSA routines"},
  44. {ERR_PACK(ERR_LIB_ECDH, 0, 0), "ECDH routines"},
  45. {ERR_PACK(ERR_LIB_SSL, 0, 0), "SSL routines"},
  46. {ERR_PACK(ERR_LIB_BIO, 0, 0), "BIO routines"},
  47. {ERR_PACK(ERR_LIB_PKCS7, 0, 0), "PKCS7 routines"},
  48. {ERR_PACK(ERR_LIB_X509V3, 0, 0), "X509 V3 routines"},
  49. {ERR_PACK(ERR_LIB_PKCS12, 0, 0), "PKCS12 routines"},
  50. {ERR_PACK(ERR_LIB_RAND, 0, 0), "random number generator"},
  51. {ERR_PACK(ERR_LIB_DSO, 0, 0), "DSO support routines"},
  52. {ERR_PACK(ERR_LIB_TS, 0, 0), "time stamp routines"},
  53. {ERR_PACK(ERR_LIB_ENGINE, 0, 0), "engine routines"},
  54. {ERR_PACK(ERR_LIB_OCSP, 0, 0), "OCSP routines"},
  55. {ERR_PACK(ERR_LIB_UI, 0, 0), "UI routines"},
  56. {ERR_PACK(ERR_LIB_FIPS, 0, 0), "FIPS routines"},
  57. {ERR_PACK(ERR_LIB_CMS, 0, 0), "CMS routines"},
  58. {ERR_PACK(ERR_LIB_HMAC, 0, 0), "HMAC routines"},
  59. {ERR_PACK(ERR_LIB_CT, 0, 0), "CT routines"},
  60. {ERR_PACK(ERR_LIB_ASYNC, 0, 0), "ASYNC routines"},
  61. {ERR_PACK(ERR_LIB_KDF, 0, 0), "KDF routines"},
  62. {ERR_PACK(ERR_LIB_OSSL_STORE, 0, 0), "STORE routines"},
  63. {ERR_PACK(ERR_LIB_SM2, 0, 0), "SM2 routines"},
  64. {0, NULL},
  65. };
  66. static ERR_STRING_DATA ERR_str_functs[] = {
  67. {ERR_PACK(0, SYS_F_FOPEN, 0), "fopen"},
  68. {ERR_PACK(0, SYS_F_CONNECT, 0), "connect"},
  69. {ERR_PACK(0, SYS_F_GETSERVBYNAME, 0), "getservbyname"},
  70. {ERR_PACK(0, SYS_F_SOCKET, 0), "socket"},
  71. {ERR_PACK(0, SYS_F_IOCTLSOCKET, 0), "ioctlsocket"},
  72. {ERR_PACK(0, SYS_F_BIND, 0), "bind"},
  73. {ERR_PACK(0, SYS_F_LISTEN, 0), "listen"},
  74. {ERR_PACK(0, SYS_F_ACCEPT, 0), "accept"},
  75. # ifdef OPENSSL_SYS_WINDOWS
  76. {ERR_PACK(0, SYS_F_WSASTARTUP, 0), "WSAstartup"},
  77. # endif
  78. {ERR_PACK(0, SYS_F_OPENDIR, 0), "opendir"},
  79. {ERR_PACK(0, SYS_F_FREAD, 0), "fread"},
  80. {ERR_PACK(0, SYS_F_GETADDRINFO, 0), "getaddrinfo"},
  81. {ERR_PACK(0, SYS_F_GETNAMEINFO, 0), "getnameinfo"},
  82. {ERR_PACK(0, SYS_F_SETSOCKOPT, 0), "setsockopt"},
  83. {ERR_PACK(0, SYS_F_GETSOCKOPT, 0), "getsockopt"},
  84. {ERR_PACK(0, SYS_F_GETSOCKNAME, 0), "getsockname"},
  85. {ERR_PACK(0, SYS_F_GETHOSTBYNAME, 0), "gethostbyname"},
  86. {ERR_PACK(0, SYS_F_FFLUSH, 0), "fflush"},
  87. {ERR_PACK(0, SYS_F_OPEN, 0), "open"},
  88. {ERR_PACK(0, SYS_F_CLOSE, 0), "close"},
  89. {ERR_PACK(0, SYS_F_IOCTL, 0), "ioctl"},
  90. {ERR_PACK(0, SYS_F_STAT, 0), "stat"},
  91. {ERR_PACK(0, SYS_F_FCNTL, 0), "fcntl"},
  92. {ERR_PACK(0, SYS_F_FSTAT, 0), "fstat"},
  93. {0, NULL},
  94. };
  95. static ERR_STRING_DATA ERR_str_reasons[] = {
  96. {ERR_R_SYS_LIB, "system lib"},
  97. {ERR_R_BN_LIB, "BN lib"},
  98. {ERR_R_RSA_LIB, "RSA lib"},
  99. {ERR_R_DH_LIB, "DH lib"},
  100. {ERR_R_EVP_LIB, "EVP lib"},
  101. {ERR_R_BUF_LIB, "BUF lib"},
  102. {ERR_R_OBJ_LIB, "OBJ lib"},
  103. {ERR_R_PEM_LIB, "PEM lib"},
  104. {ERR_R_DSA_LIB, "DSA lib"},
  105. {ERR_R_X509_LIB, "X509 lib"},
  106. {ERR_R_ASN1_LIB, "ASN1 lib"},
  107. {ERR_R_EC_LIB, "EC lib"},
  108. {ERR_R_BIO_LIB, "BIO lib"},
  109. {ERR_R_PKCS7_LIB, "PKCS7 lib"},
  110. {ERR_R_X509V3_LIB, "X509V3 lib"},
  111. {ERR_R_ENGINE_LIB, "ENGINE lib"},
  112. {ERR_R_UI_LIB, "UI lib"},
  113. {ERR_R_OSSL_STORE_LIB, "STORE lib"},
  114. {ERR_R_ECDSA_LIB, "ECDSA lib"},
  115. {ERR_R_NESTED_ASN1_ERROR, "nested asn1 error"},
  116. {ERR_R_MISSING_ASN1_EOS, "missing asn1 eos"},
  117. {ERR_R_FATAL, "fatal"},
  118. {ERR_R_MALLOC_FAILURE, "malloc failure"},
  119. {ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
  120. "called a function you should not call"},
  121. {ERR_R_PASSED_NULL_PARAMETER, "passed a null parameter"},
  122. {ERR_R_INTERNAL_ERROR, "internal error"},
  123. {ERR_R_DISABLED, "called a function that was disabled at compile-time"},
  124. {ERR_R_INIT_FAIL, "init fail"},
  125. {ERR_R_OPERATION_FAIL, "operation fail"},
  126. {0, NULL},
  127. };
  128. #endif
  129. static CRYPTO_ONCE err_init = CRYPTO_ONCE_STATIC_INIT;
  130. static int set_err_thread_local;
  131. static CRYPTO_THREAD_LOCAL err_thread_local;
  132. static CRYPTO_ONCE err_string_init = CRYPTO_ONCE_STATIC_INIT;
  133. static CRYPTO_RWLOCK *err_string_lock;
  134. static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *);
  135. /*
  136. * The internal state
  137. */
  138. static LHASH_OF(ERR_STRING_DATA) *int_error_hash = NULL;
  139. static int int_err_library_number = ERR_LIB_USER;
  140. static unsigned long get_error_values(int inc, int top, const char **file,
  141. int *line, const char **data,
  142. int *flags);
  143. static unsigned long err_string_data_hash(const ERR_STRING_DATA *a)
  144. {
  145. unsigned long ret, l;
  146. l = a->error;
  147. ret = l ^ ERR_GET_LIB(l) ^ ERR_GET_FUNC(l);
  148. return (ret ^ ret % 19 * 13);
  149. }
  150. static int err_string_data_cmp(const ERR_STRING_DATA *a,
  151. const ERR_STRING_DATA *b)
  152. {
  153. if (a->error == b->error)
  154. return 0;
  155. return a->error > b->error ? 1 : -1;
  156. }
  157. static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *d)
  158. {
  159. ERR_STRING_DATA *p = NULL;
  160. CRYPTO_THREAD_read_lock(err_string_lock);
  161. p = lh_ERR_STRING_DATA_retrieve(int_error_hash, d);
  162. CRYPTO_THREAD_unlock(err_string_lock);
  163. return p;
  164. }
  165. #ifndef OPENSSL_NO_ERR
  166. /* 2019-05-21: Russian and Ukrainian locales on Linux require more than 6,5 kB */
  167. # define SPACE_SYS_STR_REASONS 8 * 1024
  168. # define NUM_SYS_STR_REASONS 127
  169. static ERR_STRING_DATA SYS_str_reasons[NUM_SYS_STR_REASONS + 1];
  170. /*
  171. * SYS_str_reasons is filled with copies of strerror() results at
  172. * initialization. 'errno' values up to 127 should cover all usual errors,
  173. * others will be displayed numerically by ERR_error_string. It is crucial
  174. * that we have something for each reason code that occurs in
  175. * ERR_str_reasons, or bogus reason strings will be returned for SYSerr(),
  176. * which always gets an errno value and never one of those 'standard' reason
  177. * codes.
  178. */
  179. static void build_SYS_str_reasons(void)
  180. {
  181. /* OPENSSL_malloc cannot be used here, use static storage instead */
  182. static char strerror_pool[SPACE_SYS_STR_REASONS];
  183. char *cur = strerror_pool;
  184. size_t cnt = 0;
  185. static int init = 1;
  186. int i;
  187. int saveerrno = get_last_sys_error();
  188. CRYPTO_THREAD_write_lock(err_string_lock);
  189. if (!init) {
  190. CRYPTO_THREAD_unlock(err_string_lock);
  191. return;
  192. }
  193. for (i = 1; i <= NUM_SYS_STR_REASONS; i++) {
  194. ERR_STRING_DATA *str = &SYS_str_reasons[i - 1];
  195. str->error = ERR_PACK(ERR_LIB_SYS, 0, i);
  196. /*
  197. * If we have used up all the space in strerror_pool,
  198. * there's no point in calling openssl_strerror_r()
  199. */
  200. if (str->string == NULL && cnt < sizeof(strerror_pool)) {
  201. if (openssl_strerror_r(i, cur, sizeof(strerror_pool) - cnt)) {
  202. size_t l = strlen(cur);
  203. str->string = cur;
  204. cnt += l;
  205. cur += l;
  206. /*
  207. * VMS has an unusual quirk of adding spaces at the end of
  208. * some (most? all?) messages. Lets trim them off.
  209. */
  210. while (cur > strerror_pool && ossl_isspace(cur[-1])) {
  211. cur--;
  212. cnt--;
  213. }
  214. *cur++ = '\0';
  215. cnt++;
  216. }
  217. }
  218. if (str->string == NULL)
  219. str->string = "unknown";
  220. }
  221. /*
  222. * Now we still have SYS_str_reasons[NUM_SYS_STR_REASONS] = {0, NULL}, as
  223. * required by ERR_load_strings.
  224. */
  225. init = 0;
  226. CRYPTO_THREAD_unlock(err_string_lock);
  227. /* openssl_strerror_r could change errno, but we want to preserve it */
  228. set_sys_error(saveerrno);
  229. err_load_strings(SYS_str_reasons);
  230. }
  231. #endif
  232. #define err_clear_data(p, i) \
  233. do { \
  234. if ((p)->err_data_flags[i] & ERR_TXT_MALLOCED) {\
  235. OPENSSL_free((p)->err_data[i]); \
  236. (p)->err_data[i] = NULL; \
  237. } \
  238. (p)->err_data_flags[i] = 0; \
  239. } while (0)
  240. #define err_clear(p, i) \
  241. do { \
  242. err_clear_data(p, i); \
  243. (p)->err_flags[i] = 0; \
  244. (p)->err_buffer[i] = 0; \
  245. (p)->err_file[i] = NULL; \
  246. (p)->err_line[i] = -1; \
  247. } while (0)
  248. static void ERR_STATE_free(ERR_STATE *s)
  249. {
  250. int i;
  251. if (s == NULL)
  252. return;
  253. for (i = 0; i < ERR_NUM_ERRORS; i++) {
  254. err_clear_data(s, i);
  255. }
  256. OPENSSL_free(s);
  257. }
  258. DEFINE_RUN_ONCE_STATIC(do_err_strings_init)
  259. {
  260. if (!OPENSSL_init_crypto(0, NULL))
  261. return 0;
  262. err_string_lock = CRYPTO_THREAD_lock_new();
  263. if (err_string_lock == NULL)
  264. return 0;
  265. int_error_hash = lh_ERR_STRING_DATA_new(err_string_data_hash,
  266. err_string_data_cmp);
  267. if (int_error_hash == NULL) {
  268. CRYPTO_THREAD_lock_free(err_string_lock);
  269. err_string_lock = NULL;
  270. return 0;
  271. }
  272. return 1;
  273. }
  274. void err_cleanup(void)
  275. {
  276. if (set_err_thread_local != 0)
  277. CRYPTO_THREAD_cleanup_local(&err_thread_local);
  278. CRYPTO_THREAD_lock_free(err_string_lock);
  279. err_string_lock = NULL;
  280. lh_ERR_STRING_DATA_free(int_error_hash);
  281. int_error_hash = NULL;
  282. }
  283. /*
  284. * Legacy; pack in the library.
  285. */
  286. static void err_patch(int lib, ERR_STRING_DATA *str)
  287. {
  288. unsigned long plib = ERR_PACK(lib, 0, 0);
  289. for (; str->error != 0; str++)
  290. str->error |= plib;
  291. }
  292. /*
  293. * Hash in |str| error strings. Assumes the URN_ONCE was done.
  294. */
  295. static int err_load_strings(const ERR_STRING_DATA *str)
  296. {
  297. CRYPTO_THREAD_write_lock(err_string_lock);
  298. for (; str->error; str++)
  299. (void)lh_ERR_STRING_DATA_insert(int_error_hash,
  300. (ERR_STRING_DATA *)str);
  301. CRYPTO_THREAD_unlock(err_string_lock);
  302. return 1;
  303. }
  304. int ERR_load_ERR_strings(void)
  305. {
  306. #ifndef OPENSSL_NO_ERR
  307. if (!RUN_ONCE(&err_string_init, do_err_strings_init))
  308. return 0;
  309. err_load_strings(ERR_str_libraries);
  310. err_load_strings(ERR_str_reasons);
  311. err_patch(ERR_LIB_SYS, ERR_str_functs);
  312. err_load_strings(ERR_str_functs);
  313. build_SYS_str_reasons();
  314. #endif
  315. return 1;
  316. }
  317. int ERR_load_strings(int lib, ERR_STRING_DATA *str)
  318. {
  319. if (ERR_load_ERR_strings() == 0)
  320. return 0;
  321. err_patch(lib, str);
  322. err_load_strings(str);
  323. return 1;
  324. }
  325. int ERR_load_strings_const(const ERR_STRING_DATA *str)
  326. {
  327. if (ERR_load_ERR_strings() == 0)
  328. return 0;
  329. err_load_strings(str);
  330. return 1;
  331. }
  332. int ERR_unload_strings(int lib, ERR_STRING_DATA *str)
  333. {
  334. if (!RUN_ONCE(&err_string_init, do_err_strings_init))
  335. return 0;
  336. CRYPTO_THREAD_write_lock(err_string_lock);
  337. /*
  338. * We don't need to ERR_PACK the lib, since that was done (to
  339. * the table) when it was loaded.
  340. */
  341. for (; str->error; str++)
  342. (void)lh_ERR_STRING_DATA_delete(int_error_hash, str);
  343. CRYPTO_THREAD_unlock(err_string_lock);
  344. return 1;
  345. }
  346. void err_free_strings_int(void)
  347. {
  348. if (!RUN_ONCE(&err_string_init, do_err_strings_init))
  349. return;
  350. }
  351. /********************************************************/
  352. void ERR_put_error(int lib, int func, int reason, const char *file, int line)
  353. {
  354. ERR_STATE *es;
  355. #ifdef _OSD_POSIX
  356. /*
  357. * In the BS2000-OSD POSIX subsystem, the compiler generates path names
  358. * in the form "*POSIX(/etc/passwd)". This dirty hack strips them to
  359. * something sensible. @@@ We shouldn't modify a const string, though.
  360. */
  361. if (strncmp(file, "*POSIX(", sizeof("*POSIX(") - 1) == 0) {
  362. char *end;
  363. /* Skip the "*POSIX(" prefix */
  364. file += sizeof("*POSIX(") - 1;
  365. end = &file[strlen(file) - 1];
  366. if (*end == ')')
  367. *end = '\0';
  368. /* Optional: use the basename of the path only. */
  369. if ((end = strrchr(file, '/')) != NULL)
  370. file = &end[1];
  371. }
  372. #endif
  373. es = ERR_get_state();
  374. if (es == NULL)
  375. return;
  376. es->top = (es->top + 1) % ERR_NUM_ERRORS;
  377. if (es->top == es->bottom)
  378. es->bottom = (es->bottom + 1) % ERR_NUM_ERRORS;
  379. es->err_flags[es->top] = 0;
  380. es->err_buffer[es->top] = ERR_PACK(lib, func, reason);
  381. es->err_file[es->top] = file;
  382. es->err_line[es->top] = line;
  383. err_clear_data(es, es->top);
  384. }
  385. void ERR_clear_error(void)
  386. {
  387. int i;
  388. ERR_STATE *es;
  389. es = ERR_get_state();
  390. if (es == NULL)
  391. return;
  392. for (i = 0; i < ERR_NUM_ERRORS; i++) {
  393. err_clear(es, i);
  394. }
  395. es->top = es->bottom = 0;
  396. }
  397. unsigned long ERR_get_error(void)
  398. {
  399. return get_error_values(1, 0, NULL, NULL, NULL, NULL);
  400. }
  401. unsigned long ERR_get_error_line(const char **file, int *line)
  402. {
  403. return get_error_values(1, 0, file, line, NULL, NULL);
  404. }
  405. unsigned long ERR_get_error_line_data(const char **file, int *line,
  406. const char **data, int *flags)
  407. {
  408. return get_error_values(1, 0, file, line, data, flags);
  409. }
  410. unsigned long ERR_peek_error(void)
  411. {
  412. return get_error_values(0, 0, NULL, NULL, NULL, NULL);
  413. }
  414. unsigned long ERR_peek_error_line(const char **file, int *line)
  415. {
  416. return get_error_values(0, 0, file, line, NULL, NULL);
  417. }
  418. unsigned long ERR_peek_error_line_data(const char **file, int *line,
  419. const char **data, int *flags)
  420. {
  421. return get_error_values(0, 0, file, line, data, flags);
  422. }
  423. unsigned long ERR_peek_last_error(void)
  424. {
  425. return get_error_values(0, 1, NULL, NULL, NULL, NULL);
  426. }
  427. unsigned long ERR_peek_last_error_line(const char **file, int *line)
  428. {
  429. return get_error_values(0, 1, file, line, NULL, NULL);
  430. }
  431. unsigned long ERR_peek_last_error_line_data(const char **file, int *line,
  432. const char **data, int *flags)
  433. {
  434. return get_error_values(0, 1, file, line, data, flags);
  435. }
  436. static unsigned long get_error_values(int inc, int top, const char **file,
  437. int *line, const char **data,
  438. int *flags)
  439. {
  440. int i = 0;
  441. ERR_STATE *es;
  442. unsigned long ret;
  443. es = ERR_get_state();
  444. if (es == NULL)
  445. return 0;
  446. if (inc && top) {
  447. if (file)
  448. *file = "";
  449. if (line)
  450. *line = 0;
  451. if (data)
  452. *data = "";
  453. if (flags)
  454. *flags = 0;
  455. return ERR_R_INTERNAL_ERROR;
  456. }
  457. while (es->bottom != es->top) {
  458. if (es->err_flags[es->top] & ERR_FLAG_CLEAR) {
  459. err_clear(es, es->top);
  460. es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1;
  461. continue;
  462. }
  463. i = (es->bottom + 1) % ERR_NUM_ERRORS;
  464. if (es->err_flags[i] & ERR_FLAG_CLEAR) {
  465. es->bottom = i;
  466. err_clear(es, es->bottom);
  467. continue;
  468. }
  469. break;
  470. }
  471. if (es->bottom == es->top)
  472. return 0;
  473. if (top)
  474. i = es->top; /* last error */
  475. else
  476. i = (es->bottom + 1) % ERR_NUM_ERRORS; /* first error */
  477. ret = es->err_buffer[i];
  478. if (inc) {
  479. es->bottom = i;
  480. es->err_buffer[i] = 0;
  481. }
  482. if (file != NULL && line != NULL) {
  483. if (es->err_file[i] == NULL) {
  484. *file = "NA";
  485. *line = 0;
  486. } else {
  487. *file = es->err_file[i];
  488. *line = es->err_line[i];
  489. }
  490. }
  491. if (data == NULL) {
  492. if (inc) {
  493. err_clear_data(es, i);
  494. }
  495. } else {
  496. if (es->err_data[i] == NULL) {
  497. *data = "";
  498. if (flags != NULL)
  499. *flags = 0;
  500. } else {
  501. *data = es->err_data[i];
  502. if (flags != NULL)
  503. *flags = es->err_data_flags[i];
  504. }
  505. }
  506. return ret;
  507. }
  508. void ERR_error_string_n(unsigned long e, char *buf, size_t len)
  509. {
  510. char lsbuf[64], fsbuf[64], rsbuf[64];
  511. const char *ls, *fs, *rs;
  512. unsigned long l, f, r;
  513. if (len == 0)
  514. return;
  515. l = ERR_GET_LIB(e);
  516. ls = ERR_lib_error_string(e);
  517. if (ls == NULL) {
  518. BIO_snprintf(lsbuf, sizeof(lsbuf), "lib(%lu)", l);
  519. ls = lsbuf;
  520. }
  521. fs = ERR_func_error_string(e);
  522. f = ERR_GET_FUNC(e);
  523. if (fs == NULL) {
  524. BIO_snprintf(fsbuf, sizeof(fsbuf), "func(%lu)", f);
  525. fs = fsbuf;
  526. }
  527. rs = ERR_reason_error_string(e);
  528. r = ERR_GET_REASON(e);
  529. if (rs == NULL) {
  530. BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", r);
  531. rs = rsbuf;
  532. }
  533. BIO_snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls, fs, rs);
  534. if (strlen(buf) == len - 1) {
  535. /* Didn't fit; use a minimal format. */
  536. BIO_snprintf(buf, len, "err:%lx:%lx:%lx:%lx", e, l, f, r);
  537. }
  538. }
  539. /*
  540. * ERR_error_string_n should be used instead for ret != NULL as
  541. * ERR_error_string cannot know how large the buffer is
  542. */
  543. char *ERR_error_string(unsigned long e, char *ret)
  544. {
  545. static char buf[256];
  546. if (ret == NULL)
  547. ret = buf;
  548. ERR_error_string_n(e, ret, (int)sizeof(buf));
  549. return ret;
  550. }
  551. const char *ERR_lib_error_string(unsigned long e)
  552. {
  553. ERR_STRING_DATA d, *p;
  554. unsigned long l;
  555. if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
  556. return NULL;
  557. }
  558. l = ERR_GET_LIB(e);
  559. d.error = ERR_PACK(l, 0, 0);
  560. p = int_err_get_item(&d);
  561. return ((p == NULL) ? NULL : p->string);
  562. }
  563. const char *ERR_func_error_string(unsigned long e)
  564. {
  565. ERR_STRING_DATA d, *p;
  566. unsigned long l, f;
  567. if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
  568. return NULL;
  569. }
  570. l = ERR_GET_LIB(e);
  571. f = ERR_GET_FUNC(e);
  572. d.error = ERR_PACK(l, f, 0);
  573. p = int_err_get_item(&d);
  574. return ((p == NULL) ? NULL : p->string);
  575. }
  576. const char *ERR_reason_error_string(unsigned long e)
  577. {
  578. ERR_STRING_DATA d, *p = NULL;
  579. unsigned long l, r;
  580. if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
  581. return NULL;
  582. }
  583. l = ERR_GET_LIB(e);
  584. r = ERR_GET_REASON(e);
  585. d.error = ERR_PACK(l, 0, r);
  586. p = int_err_get_item(&d);
  587. if (!p) {
  588. d.error = ERR_PACK(0, 0, r);
  589. p = int_err_get_item(&d);
  590. }
  591. return ((p == NULL) ? NULL : p->string);
  592. }
  593. void err_delete_thread_state(void)
  594. {
  595. ERR_STATE *state = CRYPTO_THREAD_get_local(&err_thread_local);
  596. if (state == NULL)
  597. return;
  598. CRYPTO_THREAD_set_local(&err_thread_local, NULL);
  599. ERR_STATE_free(state);
  600. }
  601. #if OPENSSL_API_COMPAT < 0x10100000L
  602. void ERR_remove_thread_state(void *dummy)
  603. {
  604. }
  605. #endif
  606. #if OPENSSL_API_COMPAT < 0x10000000L
  607. void ERR_remove_state(unsigned long pid)
  608. {
  609. }
  610. #endif
  611. DEFINE_RUN_ONCE_STATIC(err_do_init)
  612. {
  613. set_err_thread_local = 1;
  614. return CRYPTO_THREAD_init_local(&err_thread_local, NULL);
  615. }
  616. ERR_STATE *ERR_get_state(void)
  617. {
  618. ERR_STATE *state;
  619. int saveerrno = get_last_sys_error();
  620. if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
  621. return NULL;
  622. if (!RUN_ONCE(&err_init, err_do_init))
  623. return NULL;
  624. state = CRYPTO_THREAD_get_local(&err_thread_local);
  625. if (state == (ERR_STATE*)-1)
  626. return NULL;
  627. if (state == NULL) {
  628. if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1))
  629. return NULL;
  630. if ((state = OPENSSL_zalloc(sizeof(*state))) == NULL) {
  631. CRYPTO_THREAD_set_local(&err_thread_local, NULL);
  632. return NULL;
  633. }
  634. if (!ossl_init_thread_start(OPENSSL_INIT_THREAD_ERR_STATE)
  635. || !CRYPTO_THREAD_set_local(&err_thread_local, state)) {
  636. ERR_STATE_free(state);
  637. CRYPTO_THREAD_set_local(&err_thread_local, NULL);
  638. return NULL;
  639. }
  640. /* Ignore failures from these */
  641. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
  642. }
  643. set_sys_error(saveerrno);
  644. return state;
  645. }
  646. /*
  647. * err_shelve_state returns the current thread local error state
  648. * and freezes the error module until err_unshelve_state is called.
  649. */
  650. int err_shelve_state(void **state)
  651. {
  652. int saveerrno = get_last_sys_error();
  653. /*
  654. * Note, at present our only caller is OPENSSL_init_crypto(), indirectly
  655. * via ossl_init_load_crypto_nodelete(), by which point the requested
  656. * "base" initialization has already been performed, so the below call is a
  657. * NOOP, that re-enters OPENSSL_init_crypto() only to quickly return.
  658. *
  659. * If are no other valid callers of this function, the call below can be
  660. * removed, avoiding the re-entry into OPENSSL_init_crypto(). If there are
  661. * potential uses that are not from inside OPENSSL_init_crypto(), then this
  662. * call is needed, but some care is required to make sure that the re-entry
  663. * remains a NOOP.
  664. */
  665. if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
  666. return 0;
  667. if (!RUN_ONCE(&err_init, err_do_init))
  668. return 0;
  669. *state = CRYPTO_THREAD_get_local(&err_thread_local);
  670. if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1))
  671. return 0;
  672. set_sys_error(saveerrno);
  673. return 1;
  674. }
  675. /*
  676. * err_unshelve_state restores the error state that was returned
  677. * by err_shelve_state previously.
  678. */
  679. void err_unshelve_state(void* state)
  680. {
  681. if (state != (void*)-1)
  682. CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)state);
  683. }
  684. int ERR_get_next_error_library(void)
  685. {
  686. int ret;
  687. if (!RUN_ONCE(&err_string_init, do_err_strings_init))
  688. return 0;
  689. CRYPTO_THREAD_write_lock(err_string_lock);
  690. ret = int_err_library_number++;
  691. CRYPTO_THREAD_unlock(err_string_lock);
  692. return ret;
  693. }
  694. static int err_set_error_data_int(char *data, int flags)
  695. {
  696. ERR_STATE *es;
  697. int i;
  698. es = ERR_get_state();
  699. if (es == NULL)
  700. return 0;
  701. i = es->top;
  702. err_clear_data(es, i);
  703. es->err_data[i] = data;
  704. es->err_data_flags[i] = flags;
  705. return 1;
  706. }
  707. void ERR_set_error_data(char *data, int flags)
  708. {
  709. /*
  710. * This function is void so we cannot propagate the error return. Since it
  711. * is also in the public API we can't change the return type.
  712. */
  713. err_set_error_data_int(data, flags);
  714. }
  715. void ERR_add_error_data(int num, ...)
  716. {
  717. va_list args;
  718. va_start(args, num);
  719. ERR_add_error_vdata(num, args);
  720. va_end(args);
  721. }
  722. void ERR_add_error_vdata(int num, va_list args)
  723. {
  724. int i, n, s;
  725. char *str, *p, *a;
  726. s = 80;
  727. if ((str = OPENSSL_malloc(s + 1)) == NULL) {
  728. /* ERRerr(ERR_F_ERR_ADD_ERROR_VDATA, ERR_R_MALLOC_FAILURE); */
  729. return;
  730. }
  731. str[0] = '\0';
  732. n = 0;
  733. for (i = 0; i < num; i++) {
  734. a = va_arg(args, char *);
  735. if (a == NULL)
  736. a = "<NULL>";
  737. n += strlen(a);
  738. if (n > s) {
  739. s = n + 20;
  740. p = OPENSSL_realloc(str, s + 1);
  741. if (p == NULL) {
  742. OPENSSL_free(str);
  743. return;
  744. }
  745. str = p;
  746. }
  747. OPENSSL_strlcat(str, a, (size_t)s + 1);
  748. }
  749. if (!err_set_error_data_int(str, ERR_TXT_MALLOCED | ERR_TXT_STRING))
  750. OPENSSL_free(str);
  751. }
  752. int ERR_set_mark(void)
  753. {
  754. ERR_STATE *es;
  755. es = ERR_get_state();
  756. if (es == NULL)
  757. return 0;
  758. if (es->bottom == es->top)
  759. return 0;
  760. es->err_flags[es->top] |= ERR_FLAG_MARK;
  761. return 1;
  762. }
  763. int ERR_pop_to_mark(void)
  764. {
  765. ERR_STATE *es;
  766. es = ERR_get_state();
  767. if (es == NULL)
  768. return 0;
  769. while (es->bottom != es->top
  770. && (es->err_flags[es->top] & ERR_FLAG_MARK) == 0) {
  771. err_clear(es, es->top);
  772. es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1;
  773. }
  774. if (es->bottom == es->top)
  775. return 0;
  776. es->err_flags[es->top] &= ~ERR_FLAG_MARK;
  777. return 1;
  778. }
  779. int ERR_clear_last_mark(void)
  780. {
  781. ERR_STATE *es;
  782. int top;
  783. es = ERR_get_state();
  784. if (es == NULL)
  785. return 0;
  786. top = es->top;
  787. while (es->bottom != top
  788. && (es->err_flags[top] & ERR_FLAG_MARK) == 0) {
  789. top = top > 0 ? top - 1 : ERR_NUM_ERRORS - 1;
  790. }
  791. if (es->bottom == top)
  792. return 0;
  793. es->err_flags[top] &= ~ERR_FLAG_MARK;
  794. return 1;
  795. }
  796. void err_clear_last_constant_time(int clear)
  797. {
  798. ERR_STATE *es;
  799. int top;
  800. es = ERR_get_state();
  801. if (es == NULL)
  802. return;
  803. top = es->top;
  804. /*
  805. * Flag error as cleared but remove it elsewhere to avoid two errors
  806. * accessing the same error stack location, revealing timing information.
  807. */
  808. clear = constant_time_select_int(constant_time_eq_int(clear, 0),
  809. 0, ERR_FLAG_CLEAR);
  810. es->err_flags[top] |= clear;
  811. }