err.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. /*
  2. * Copyright 1995-2021 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_PASSED_INVALID_ARGUMENT, "passed invalid argument"},
  126. {ERR_R_OPERATION_FAIL, "operation fail"},
  127. {0, NULL},
  128. };
  129. #endif
  130. static CRYPTO_ONCE err_init = CRYPTO_ONCE_STATIC_INIT;
  131. static int set_err_thread_local;
  132. static CRYPTO_THREAD_LOCAL err_thread_local;
  133. static CRYPTO_ONCE err_string_init = CRYPTO_ONCE_STATIC_INIT;
  134. static CRYPTO_RWLOCK *err_string_lock;
  135. static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *);
  136. /*
  137. * The internal state
  138. */
  139. static LHASH_OF(ERR_STRING_DATA) *int_error_hash = NULL;
  140. static int int_err_library_number = ERR_LIB_USER;
  141. static unsigned long get_error_values(int inc, int top, const char **file,
  142. int *line, const char **data,
  143. int *flags);
  144. static unsigned long err_string_data_hash(const ERR_STRING_DATA *a)
  145. {
  146. unsigned long ret, l;
  147. l = a->error;
  148. ret = l ^ ERR_GET_LIB(l) ^ ERR_GET_FUNC(l);
  149. return (ret ^ ret % 19 * 13);
  150. }
  151. static int err_string_data_cmp(const ERR_STRING_DATA *a,
  152. const ERR_STRING_DATA *b)
  153. {
  154. if (a->error == b->error)
  155. return 0;
  156. return a->error > b->error ? 1 : -1;
  157. }
  158. static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *d)
  159. {
  160. ERR_STRING_DATA *p = NULL;
  161. CRYPTO_THREAD_read_lock(err_string_lock);
  162. p = lh_ERR_STRING_DATA_retrieve(int_error_hash, d);
  163. CRYPTO_THREAD_unlock(err_string_lock);
  164. return p;
  165. }
  166. #ifndef OPENSSL_NO_ERR
  167. /* 2019-05-21: Russian and Ukrainian locales on Linux require more than 6,5 kB */
  168. # define SPACE_SYS_STR_REASONS 8 * 1024
  169. # define NUM_SYS_STR_REASONS 127
  170. static ERR_STRING_DATA SYS_str_reasons[NUM_SYS_STR_REASONS + 1];
  171. /*
  172. * SYS_str_reasons is filled with copies of strerror() results at
  173. * initialization. 'errno' values up to 127 should cover all usual errors,
  174. * others will be displayed numerically by ERR_error_string. It is crucial
  175. * that we have something for each reason code that occurs in
  176. * ERR_str_reasons, or bogus reason strings will be returned for SYSerr(),
  177. * which always gets an errno value and never one of those 'standard' reason
  178. * codes.
  179. */
  180. static void build_SYS_str_reasons(void)
  181. {
  182. /* OPENSSL_malloc cannot be used here, use static storage instead */
  183. static char strerror_pool[SPACE_SYS_STR_REASONS];
  184. char *cur = strerror_pool;
  185. size_t cnt = 0;
  186. static int init = 1;
  187. int i;
  188. int saveerrno = get_last_sys_error();
  189. CRYPTO_THREAD_write_lock(err_string_lock);
  190. if (!init) {
  191. CRYPTO_THREAD_unlock(err_string_lock);
  192. return;
  193. }
  194. for (i = 1; i <= NUM_SYS_STR_REASONS; i++) {
  195. ERR_STRING_DATA *str = &SYS_str_reasons[i - 1];
  196. str->error = ERR_PACK(ERR_LIB_SYS, 0, i);
  197. /*
  198. * If we have used up all the space in strerror_pool,
  199. * there's no point in calling openssl_strerror_r()
  200. */
  201. if (str->string == NULL && cnt < sizeof(strerror_pool)) {
  202. if (openssl_strerror_r(i, cur, sizeof(strerror_pool) - cnt)) {
  203. size_t l = strlen(cur);
  204. str->string = cur;
  205. cnt += l;
  206. cur += l;
  207. /*
  208. * VMS has an unusual quirk of adding spaces at the end of
  209. * some (most? all?) messages. Lets trim them off.
  210. */
  211. while (cur > strerror_pool && ossl_isspace(cur[-1])) {
  212. cur--;
  213. cnt--;
  214. }
  215. *cur++ = '\0';
  216. cnt++;
  217. }
  218. }
  219. if (str->string == NULL)
  220. str->string = "unknown";
  221. }
  222. /*
  223. * Now we still have SYS_str_reasons[NUM_SYS_STR_REASONS] = {0, NULL}, as
  224. * required by ERR_load_strings.
  225. */
  226. init = 0;
  227. CRYPTO_THREAD_unlock(err_string_lock);
  228. /* openssl_strerror_r could change errno, but we want to preserve it */
  229. set_sys_error(saveerrno);
  230. err_load_strings(SYS_str_reasons);
  231. }
  232. #endif
  233. #define err_clear_data(p, i) \
  234. do { \
  235. if ((p)->err_data_flags[i] & ERR_TXT_MALLOCED) {\
  236. OPENSSL_free((p)->err_data[i]); \
  237. (p)->err_data[i] = NULL; \
  238. } \
  239. (p)->err_data_flags[i] = 0; \
  240. } while (0)
  241. #define err_clear(p, i) \
  242. do { \
  243. err_clear_data(p, i); \
  244. (p)->err_flags[i] = 0; \
  245. (p)->err_buffer[i] = 0; \
  246. (p)->err_file[i] = NULL; \
  247. (p)->err_line[i] = -1; \
  248. } while (0)
  249. static void ERR_STATE_free(ERR_STATE *s)
  250. {
  251. int i;
  252. if (s == NULL)
  253. return;
  254. for (i = 0; i < ERR_NUM_ERRORS; i++) {
  255. err_clear_data(s, i);
  256. }
  257. OPENSSL_free(s);
  258. }
  259. DEFINE_RUN_ONCE_STATIC(do_err_strings_init)
  260. {
  261. if (!OPENSSL_init_crypto(0, NULL))
  262. return 0;
  263. err_string_lock = CRYPTO_THREAD_lock_new();
  264. if (err_string_lock == NULL)
  265. return 0;
  266. int_error_hash = lh_ERR_STRING_DATA_new(err_string_data_hash,
  267. err_string_data_cmp);
  268. if (int_error_hash == NULL) {
  269. CRYPTO_THREAD_lock_free(err_string_lock);
  270. err_string_lock = NULL;
  271. return 0;
  272. }
  273. return 1;
  274. }
  275. void err_cleanup(void)
  276. {
  277. if (set_err_thread_local != 0)
  278. CRYPTO_THREAD_cleanup_local(&err_thread_local);
  279. CRYPTO_THREAD_lock_free(err_string_lock);
  280. err_string_lock = NULL;
  281. lh_ERR_STRING_DATA_free(int_error_hash);
  282. int_error_hash = NULL;
  283. }
  284. /*
  285. * Legacy; pack in the library.
  286. */
  287. static void err_patch(int lib, ERR_STRING_DATA *str)
  288. {
  289. unsigned long plib = ERR_PACK(lib, 0, 0);
  290. for (; str->error != 0; str++)
  291. str->error |= plib;
  292. }
  293. /*
  294. * Hash in |str| error strings. Assumes the URN_ONCE was done.
  295. */
  296. static int err_load_strings(const ERR_STRING_DATA *str)
  297. {
  298. CRYPTO_THREAD_write_lock(err_string_lock);
  299. for (; str->error; str++)
  300. (void)lh_ERR_STRING_DATA_insert(int_error_hash,
  301. (ERR_STRING_DATA *)str);
  302. CRYPTO_THREAD_unlock(err_string_lock);
  303. return 1;
  304. }
  305. int ERR_load_ERR_strings(void)
  306. {
  307. #ifndef OPENSSL_NO_ERR
  308. if (!RUN_ONCE(&err_string_init, do_err_strings_init))
  309. return 0;
  310. err_load_strings(ERR_str_libraries);
  311. err_load_strings(ERR_str_reasons);
  312. err_patch(ERR_LIB_SYS, ERR_str_functs);
  313. err_load_strings(ERR_str_functs);
  314. build_SYS_str_reasons();
  315. #endif
  316. return 1;
  317. }
  318. int ERR_load_strings(int lib, ERR_STRING_DATA *str)
  319. {
  320. if (ERR_load_ERR_strings() == 0)
  321. return 0;
  322. err_patch(lib, str);
  323. err_load_strings(str);
  324. return 1;
  325. }
  326. int ERR_load_strings_const(const ERR_STRING_DATA *str)
  327. {
  328. if (ERR_load_ERR_strings() == 0)
  329. return 0;
  330. err_load_strings(str);
  331. return 1;
  332. }
  333. int ERR_unload_strings(int lib, ERR_STRING_DATA *str)
  334. {
  335. if (!RUN_ONCE(&err_string_init, do_err_strings_init))
  336. return 0;
  337. CRYPTO_THREAD_write_lock(err_string_lock);
  338. /*
  339. * We don't need to ERR_PACK the lib, since that was done (to
  340. * the table) when it was loaded.
  341. */
  342. for (; str->error; str++)
  343. (void)lh_ERR_STRING_DATA_delete(int_error_hash, str);
  344. CRYPTO_THREAD_unlock(err_string_lock);
  345. return 1;
  346. }
  347. void err_free_strings_int(void)
  348. {
  349. if (!RUN_ONCE(&err_string_init, do_err_strings_init))
  350. return;
  351. }
  352. /********************************************************/
  353. void ERR_put_error(int lib, int func, int reason, const char *file, int line)
  354. {
  355. ERR_STATE *es;
  356. #ifdef _OSD_POSIX
  357. /*
  358. * In the BS2000-OSD POSIX subsystem, the compiler generates path names
  359. * in the form "*POSIX(/etc/passwd)". This dirty hack strips them to
  360. * something sensible. @@@ We shouldn't modify a const string, though.
  361. */
  362. if (strncmp(file, "*POSIX(", sizeof("*POSIX(") - 1) == 0) {
  363. char *end;
  364. /* Skip the "*POSIX(" prefix */
  365. file += sizeof("*POSIX(") - 1;
  366. end = &file[strlen(file) - 1];
  367. if (*end == ')')
  368. *end = '\0';
  369. /* Optional: use the basename of the path only. */
  370. if ((end = strrchr(file, '/')) != NULL)
  371. file = &end[1];
  372. }
  373. #endif
  374. es = ERR_get_state();
  375. if (es == NULL)
  376. return;
  377. es->top = (es->top + 1) % ERR_NUM_ERRORS;
  378. if (es->top == es->bottom)
  379. es->bottom = (es->bottom + 1) % ERR_NUM_ERRORS;
  380. es->err_flags[es->top] = 0;
  381. es->err_buffer[es->top] = ERR_PACK(lib, func, reason);
  382. es->err_file[es->top] = file;
  383. es->err_line[es->top] = line;
  384. err_clear_data(es, es->top);
  385. }
  386. void ERR_clear_error(void)
  387. {
  388. int i;
  389. ERR_STATE *es;
  390. es = ERR_get_state();
  391. if (es == NULL)
  392. return;
  393. for (i = 0; i < ERR_NUM_ERRORS; i++) {
  394. err_clear(es, i);
  395. }
  396. es->top = es->bottom = 0;
  397. }
  398. unsigned long ERR_get_error(void)
  399. {
  400. return get_error_values(1, 0, NULL, NULL, NULL, NULL);
  401. }
  402. unsigned long ERR_get_error_line(const char **file, int *line)
  403. {
  404. return get_error_values(1, 0, file, line, NULL, NULL);
  405. }
  406. unsigned long ERR_get_error_line_data(const char **file, int *line,
  407. const char **data, int *flags)
  408. {
  409. return get_error_values(1, 0, file, line, data, flags);
  410. }
  411. unsigned long ERR_peek_error(void)
  412. {
  413. return get_error_values(0, 0, NULL, NULL, NULL, NULL);
  414. }
  415. unsigned long ERR_peek_error_line(const char **file, int *line)
  416. {
  417. return get_error_values(0, 0, file, line, NULL, NULL);
  418. }
  419. unsigned long ERR_peek_error_line_data(const char **file, int *line,
  420. const char **data, int *flags)
  421. {
  422. return get_error_values(0, 0, file, line, data, flags);
  423. }
  424. unsigned long ERR_peek_last_error(void)
  425. {
  426. return get_error_values(0, 1, NULL, NULL, NULL, NULL);
  427. }
  428. unsigned long ERR_peek_last_error_line(const char **file, int *line)
  429. {
  430. return get_error_values(0, 1, file, line, NULL, NULL);
  431. }
  432. unsigned long ERR_peek_last_error_line_data(const char **file, int *line,
  433. const char **data, int *flags)
  434. {
  435. return get_error_values(0, 1, file, line, data, flags);
  436. }
  437. static unsigned long get_error_values(int inc, int top, const char **file,
  438. int *line, const char **data,
  439. int *flags)
  440. {
  441. int i = 0;
  442. ERR_STATE *es;
  443. unsigned long ret;
  444. es = ERR_get_state();
  445. if (es == NULL)
  446. return 0;
  447. if (inc && top) {
  448. if (file)
  449. *file = "";
  450. if (line)
  451. *line = 0;
  452. if (data)
  453. *data = "";
  454. if (flags)
  455. *flags = 0;
  456. return ERR_R_INTERNAL_ERROR;
  457. }
  458. while (es->bottom != es->top) {
  459. if (es->err_flags[es->top] & ERR_FLAG_CLEAR) {
  460. err_clear(es, es->top);
  461. es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1;
  462. continue;
  463. }
  464. i = (es->bottom + 1) % ERR_NUM_ERRORS;
  465. if (es->err_flags[i] & ERR_FLAG_CLEAR) {
  466. es->bottom = i;
  467. err_clear(es, es->bottom);
  468. continue;
  469. }
  470. break;
  471. }
  472. if (es->bottom == es->top)
  473. return 0;
  474. if (top)
  475. i = es->top; /* last error */
  476. else
  477. i = (es->bottom + 1) % ERR_NUM_ERRORS; /* first error */
  478. ret = es->err_buffer[i];
  479. if (inc) {
  480. es->bottom = i;
  481. es->err_buffer[i] = 0;
  482. }
  483. if (file != NULL && line != NULL) {
  484. if (es->err_file[i] == NULL) {
  485. *file = "NA";
  486. *line = 0;
  487. } else {
  488. *file = es->err_file[i];
  489. *line = es->err_line[i];
  490. }
  491. }
  492. if (data == NULL) {
  493. if (inc) {
  494. err_clear_data(es, i);
  495. }
  496. } else {
  497. if (es->err_data[i] == NULL) {
  498. *data = "";
  499. if (flags != NULL)
  500. *flags = 0;
  501. } else {
  502. *data = es->err_data[i];
  503. if (flags != NULL)
  504. *flags = es->err_data_flags[i];
  505. }
  506. }
  507. return ret;
  508. }
  509. void ERR_error_string_n(unsigned long e, char *buf, size_t len)
  510. {
  511. char lsbuf[64], fsbuf[64], rsbuf[64];
  512. const char *ls, *fs, *rs;
  513. unsigned long l, f, r;
  514. if (len == 0)
  515. return;
  516. l = ERR_GET_LIB(e);
  517. ls = ERR_lib_error_string(e);
  518. if (ls == NULL) {
  519. BIO_snprintf(lsbuf, sizeof(lsbuf), "lib(%lu)", l);
  520. ls = lsbuf;
  521. }
  522. fs = ERR_func_error_string(e);
  523. f = ERR_GET_FUNC(e);
  524. if (fs == NULL) {
  525. BIO_snprintf(fsbuf, sizeof(fsbuf), "func(%lu)", f);
  526. fs = fsbuf;
  527. }
  528. rs = ERR_reason_error_string(e);
  529. r = ERR_GET_REASON(e);
  530. if (rs == NULL) {
  531. BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", r);
  532. rs = rsbuf;
  533. }
  534. BIO_snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls, fs, rs);
  535. if (strlen(buf) == len - 1) {
  536. /* Didn't fit; use a minimal format. */
  537. BIO_snprintf(buf, len, "err:%lx:%lx:%lx:%lx", e, l, f, r);
  538. }
  539. }
  540. /*
  541. * ERR_error_string_n should be used instead for ret != NULL as
  542. * ERR_error_string cannot know how large the buffer is
  543. */
  544. char *ERR_error_string(unsigned long e, char *ret)
  545. {
  546. static char buf[256];
  547. if (ret == NULL)
  548. ret = buf;
  549. ERR_error_string_n(e, ret, (int)sizeof(buf));
  550. return ret;
  551. }
  552. const char *ERR_lib_error_string(unsigned long e)
  553. {
  554. ERR_STRING_DATA d, *p;
  555. unsigned long l;
  556. if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
  557. return NULL;
  558. }
  559. l = ERR_GET_LIB(e);
  560. d.error = ERR_PACK(l, 0, 0);
  561. p = int_err_get_item(&d);
  562. return ((p == NULL) ? NULL : p->string);
  563. }
  564. const char *ERR_func_error_string(unsigned long e)
  565. {
  566. ERR_STRING_DATA d, *p;
  567. unsigned long l, f;
  568. if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
  569. return NULL;
  570. }
  571. l = ERR_GET_LIB(e);
  572. f = ERR_GET_FUNC(e);
  573. d.error = ERR_PACK(l, f, 0);
  574. p = int_err_get_item(&d);
  575. return ((p == NULL) ? NULL : p->string);
  576. }
  577. const char *ERR_reason_error_string(unsigned long e)
  578. {
  579. ERR_STRING_DATA d, *p = NULL;
  580. unsigned long l, r;
  581. if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
  582. return NULL;
  583. }
  584. l = ERR_GET_LIB(e);
  585. r = ERR_GET_REASON(e);
  586. d.error = ERR_PACK(l, 0, r);
  587. p = int_err_get_item(&d);
  588. if (!p) {
  589. d.error = ERR_PACK(0, 0, r);
  590. p = int_err_get_item(&d);
  591. }
  592. return ((p == NULL) ? NULL : p->string);
  593. }
  594. void err_delete_thread_state(void)
  595. {
  596. ERR_STATE *state = CRYPTO_THREAD_get_local(&err_thread_local);
  597. if (state == NULL)
  598. return;
  599. CRYPTO_THREAD_set_local(&err_thread_local, NULL);
  600. ERR_STATE_free(state);
  601. }
  602. #if OPENSSL_API_COMPAT < 0x10100000L
  603. void ERR_remove_thread_state(void *dummy)
  604. {
  605. }
  606. #endif
  607. #if OPENSSL_API_COMPAT < 0x10000000L
  608. void ERR_remove_state(unsigned long pid)
  609. {
  610. }
  611. #endif
  612. DEFINE_RUN_ONCE_STATIC(err_do_init)
  613. {
  614. set_err_thread_local = 1;
  615. return CRYPTO_THREAD_init_local(&err_thread_local, NULL);
  616. }
  617. ERR_STATE *ERR_get_state(void)
  618. {
  619. ERR_STATE *state;
  620. int saveerrno = get_last_sys_error();
  621. if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
  622. return NULL;
  623. if (!RUN_ONCE(&err_init, err_do_init))
  624. return NULL;
  625. state = CRYPTO_THREAD_get_local(&err_thread_local);
  626. if (state == (ERR_STATE*)-1)
  627. return NULL;
  628. if (state == NULL) {
  629. if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1))
  630. return NULL;
  631. if ((state = OPENSSL_zalloc(sizeof(*state))) == NULL) {
  632. CRYPTO_THREAD_set_local(&err_thread_local, NULL);
  633. return NULL;
  634. }
  635. if (!ossl_init_thread_start(OPENSSL_INIT_THREAD_ERR_STATE)
  636. || !CRYPTO_THREAD_set_local(&err_thread_local, state)) {
  637. ERR_STATE_free(state);
  638. CRYPTO_THREAD_set_local(&err_thread_local, NULL);
  639. return NULL;
  640. }
  641. /* Ignore failures from these */
  642. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
  643. }
  644. set_sys_error(saveerrno);
  645. return state;
  646. }
  647. /*
  648. * err_shelve_state returns the current thread local error state
  649. * and freezes the error module until err_unshelve_state is called.
  650. */
  651. int err_shelve_state(void **state)
  652. {
  653. int saveerrno = get_last_sys_error();
  654. /*
  655. * Note, at present our only caller is OPENSSL_init_crypto(), indirectly
  656. * via ossl_init_load_crypto_nodelete(), by which point the requested
  657. * "base" initialization has already been performed, so the below call is a
  658. * NOOP, that re-enters OPENSSL_init_crypto() only to quickly return.
  659. *
  660. * If are no other valid callers of this function, the call below can be
  661. * removed, avoiding the re-entry into OPENSSL_init_crypto(). If there are
  662. * potential uses that are not from inside OPENSSL_init_crypto(), then this
  663. * call is needed, but some care is required to make sure that the re-entry
  664. * remains a NOOP.
  665. */
  666. if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
  667. return 0;
  668. if (!RUN_ONCE(&err_init, err_do_init))
  669. return 0;
  670. *state = CRYPTO_THREAD_get_local(&err_thread_local);
  671. if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1))
  672. return 0;
  673. set_sys_error(saveerrno);
  674. return 1;
  675. }
  676. /*
  677. * err_unshelve_state restores the error state that was returned
  678. * by err_shelve_state previously.
  679. */
  680. void err_unshelve_state(void* state)
  681. {
  682. if (state != (void*)-1)
  683. CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)state);
  684. }
  685. int ERR_get_next_error_library(void)
  686. {
  687. int ret;
  688. if (!RUN_ONCE(&err_string_init, do_err_strings_init))
  689. return 0;
  690. CRYPTO_THREAD_write_lock(err_string_lock);
  691. ret = int_err_library_number++;
  692. CRYPTO_THREAD_unlock(err_string_lock);
  693. return ret;
  694. }
  695. static int err_set_error_data_int(char *data, int flags)
  696. {
  697. ERR_STATE *es;
  698. int i;
  699. es = ERR_get_state();
  700. if (es == NULL)
  701. return 0;
  702. i = es->top;
  703. err_clear_data(es, i);
  704. es->err_data[i] = data;
  705. es->err_data_flags[i] = flags;
  706. return 1;
  707. }
  708. void ERR_set_error_data(char *data, int flags)
  709. {
  710. /*
  711. * This function is void so we cannot propagate the error return. Since it
  712. * is also in the public API we can't change the return type.
  713. */
  714. err_set_error_data_int(data, flags);
  715. }
  716. void ERR_add_error_data(int num, ...)
  717. {
  718. va_list args;
  719. va_start(args, num);
  720. ERR_add_error_vdata(num, args);
  721. va_end(args);
  722. }
  723. void ERR_add_error_vdata(int num, va_list args)
  724. {
  725. int i, n, s;
  726. char *str, *p, *a;
  727. s = 80;
  728. if ((str = OPENSSL_malloc(s + 1)) == NULL) {
  729. /* ERRerr(ERR_F_ERR_ADD_ERROR_VDATA, ERR_R_MALLOC_FAILURE); */
  730. return;
  731. }
  732. str[0] = '\0';
  733. n = 0;
  734. for (i = 0; i < num; i++) {
  735. a = va_arg(args, char *);
  736. if (a == NULL)
  737. a = "<NULL>";
  738. n += strlen(a);
  739. if (n > s) {
  740. s = n + 20;
  741. p = OPENSSL_realloc(str, s + 1);
  742. if (p == NULL) {
  743. OPENSSL_free(str);
  744. return;
  745. }
  746. str = p;
  747. }
  748. OPENSSL_strlcat(str, a, (size_t)s + 1);
  749. }
  750. if (!err_set_error_data_int(str, ERR_TXT_MALLOCED | ERR_TXT_STRING))
  751. OPENSSL_free(str);
  752. }
  753. int ERR_set_mark(void)
  754. {
  755. ERR_STATE *es;
  756. es = ERR_get_state();
  757. if (es == NULL)
  758. return 0;
  759. if (es->bottom == es->top)
  760. return 0;
  761. es->err_flags[es->top] |= ERR_FLAG_MARK;
  762. return 1;
  763. }
  764. int ERR_pop_to_mark(void)
  765. {
  766. ERR_STATE *es;
  767. es = ERR_get_state();
  768. if (es == NULL)
  769. return 0;
  770. while (es->bottom != es->top
  771. && (es->err_flags[es->top] & ERR_FLAG_MARK) == 0) {
  772. err_clear(es, es->top);
  773. es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1;
  774. }
  775. if (es->bottom == es->top)
  776. return 0;
  777. es->err_flags[es->top] &= ~ERR_FLAG_MARK;
  778. return 1;
  779. }
  780. int ERR_clear_last_mark(void)
  781. {
  782. ERR_STATE *es;
  783. int top;
  784. es = ERR_get_state();
  785. if (es == NULL)
  786. return 0;
  787. top = es->top;
  788. while (es->bottom != top
  789. && (es->err_flags[top] & ERR_FLAG_MARK) == 0) {
  790. top = top > 0 ? top - 1 : ERR_NUM_ERRORS - 1;
  791. }
  792. if (es->bottom == top)
  793. return 0;
  794. es->err_flags[top] &= ~ERR_FLAG_MARK;
  795. return 1;
  796. }
  797. void err_clear_last_constant_time(int clear)
  798. {
  799. ERR_STATE *es;
  800. int top;
  801. es = ERR_get_state();
  802. if (es == NULL)
  803. return;
  804. top = es->top;
  805. /*
  806. * Flag error as cleared but remove it elsewhere to avoid two errors
  807. * accessing the same error stack location, revealing timing information.
  808. */
  809. clear = constant_time_select_int(constant_time_eq_int(clear, 0),
  810. 0, ERR_FLAG_CLEAR);
  811. es->err_flags[top] |= clear;
  812. }