err.c 26 KB

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