err.c 25 KB

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