err.c 26 KB

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