err.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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_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. if (es->bottom == es->top)
  456. return 0;
  457. if (top)
  458. i = es->top; /* last error */
  459. else
  460. i = (es->bottom + 1) % ERR_NUM_ERRORS; /* first error */
  461. ret = es->err_buffer[i];
  462. if (inc) {
  463. es->bottom = i;
  464. es->err_buffer[i] = 0;
  465. }
  466. if (file != NULL && line != NULL) {
  467. if (es->err_file[i] == NULL) {
  468. *file = "NA";
  469. *line = 0;
  470. } else {
  471. *file = es->err_file[i];
  472. *line = es->err_line[i];
  473. }
  474. }
  475. if (data == NULL) {
  476. if (inc) {
  477. err_clear_data(es, i);
  478. }
  479. } else {
  480. if (es->err_data[i] == NULL) {
  481. *data = "";
  482. if (flags != NULL)
  483. *flags = 0;
  484. } else {
  485. *data = es->err_data[i];
  486. if (flags != NULL)
  487. *flags = es->err_data_flags[i];
  488. }
  489. }
  490. return ret;
  491. }
  492. void ERR_error_string_n(unsigned long e, char *buf, size_t len)
  493. {
  494. char lsbuf[64], fsbuf[64], rsbuf[64];
  495. const char *ls, *fs, *rs;
  496. unsigned long l, f, r;
  497. if (len == 0)
  498. return;
  499. l = ERR_GET_LIB(e);
  500. ls = ERR_lib_error_string(e);
  501. if (ls == NULL) {
  502. BIO_snprintf(lsbuf, sizeof(lsbuf), "lib(%lu)", l);
  503. ls = lsbuf;
  504. }
  505. fs = ERR_func_error_string(e);
  506. f = ERR_GET_FUNC(e);
  507. if (fs == NULL) {
  508. BIO_snprintf(fsbuf, sizeof(fsbuf), "func(%lu)", f);
  509. fs = fsbuf;
  510. }
  511. rs = ERR_reason_error_string(e);
  512. r = ERR_GET_REASON(e);
  513. if (rs == NULL) {
  514. BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", r);
  515. rs = rsbuf;
  516. }
  517. BIO_snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls, fs, rs);
  518. if (strlen(buf) == len - 1) {
  519. /* Didn't fit; use a minimal format. */
  520. BIO_snprintf(buf, len, "err:%lx:%lx:%lx:%lx", e, l, f, r);
  521. }
  522. }
  523. /*
  524. * ERR_error_string_n should be used instead for ret != NULL as
  525. * ERR_error_string cannot know how large the buffer is
  526. */
  527. char *ERR_error_string(unsigned long e, char *ret)
  528. {
  529. static char buf[256];
  530. if (ret == NULL)
  531. ret = buf;
  532. ERR_error_string_n(e, ret, (int)sizeof(buf));
  533. return ret;
  534. }
  535. const char *ERR_lib_error_string(unsigned long e)
  536. {
  537. ERR_STRING_DATA d, *p;
  538. unsigned long l;
  539. if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
  540. return NULL;
  541. }
  542. l = ERR_GET_LIB(e);
  543. d.error = ERR_PACK(l, 0, 0);
  544. p = int_err_get_item(&d);
  545. return ((p == NULL) ? NULL : p->string);
  546. }
  547. const char *ERR_func_error_string(unsigned long e)
  548. {
  549. ERR_STRING_DATA d, *p;
  550. unsigned long l, f;
  551. if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
  552. return NULL;
  553. }
  554. l = ERR_GET_LIB(e);
  555. f = ERR_GET_FUNC(e);
  556. d.error = ERR_PACK(l, f, 0);
  557. p = int_err_get_item(&d);
  558. return ((p == NULL) ? NULL : p->string);
  559. }
  560. const char *ERR_reason_error_string(unsigned long e)
  561. {
  562. ERR_STRING_DATA d, *p = NULL;
  563. unsigned long l, r;
  564. if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
  565. return NULL;
  566. }
  567. l = ERR_GET_LIB(e);
  568. r = ERR_GET_REASON(e);
  569. d.error = ERR_PACK(l, 0, r);
  570. p = int_err_get_item(&d);
  571. if (!p) {
  572. d.error = ERR_PACK(0, 0, r);
  573. p = int_err_get_item(&d);
  574. }
  575. return ((p == NULL) ? NULL : p->string);
  576. }
  577. void err_delete_thread_state(void)
  578. {
  579. ERR_STATE *state = CRYPTO_THREAD_get_local(&err_thread_local);
  580. if (state == NULL)
  581. return;
  582. CRYPTO_THREAD_set_local(&err_thread_local, NULL);
  583. ERR_STATE_free(state);
  584. }
  585. #if !OPENSSL_API_1_1_0
  586. void ERR_remove_thread_state(void *dummy)
  587. {
  588. }
  589. #endif
  590. #if !OPENSSL_API_1_0_0
  591. void ERR_remove_state(unsigned long pid)
  592. {
  593. }
  594. #endif
  595. DEFINE_RUN_ONCE_STATIC(err_do_init)
  596. {
  597. set_err_thread_local = 1;
  598. return CRYPTO_THREAD_init_local(&err_thread_local, NULL);
  599. }
  600. ERR_STATE *ERR_get_state(void)
  601. {
  602. ERR_STATE *state;
  603. if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
  604. return NULL;
  605. if (!RUN_ONCE(&err_init, err_do_init))
  606. return NULL;
  607. state = CRYPTO_THREAD_get_local(&err_thread_local);
  608. if (state == (ERR_STATE*)-1)
  609. return NULL;
  610. if (state == NULL) {
  611. if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1))
  612. return NULL;
  613. if ((state = OPENSSL_zalloc(sizeof(*state))) == NULL) {
  614. CRYPTO_THREAD_set_local(&err_thread_local, NULL);
  615. return NULL;
  616. }
  617. if (!ossl_init_thread_start(OPENSSL_INIT_THREAD_ERR_STATE)
  618. || !CRYPTO_THREAD_set_local(&err_thread_local, state)) {
  619. ERR_STATE_free(state);
  620. CRYPTO_THREAD_set_local(&err_thread_local, NULL);
  621. return NULL;
  622. }
  623. /* Ignore failures from these */
  624. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
  625. }
  626. return state;
  627. }
  628. /*
  629. * err_shelve_state returns the current thread local error state
  630. * and freezes the error module until err_unshelve_state is called.
  631. */
  632. int err_shelve_state(void **state)
  633. {
  634. if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
  635. return 0;
  636. if (!RUN_ONCE(&err_init, err_do_init))
  637. return 0;
  638. *state = CRYPTO_THREAD_get_local(&err_thread_local);
  639. if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1))
  640. return 0;
  641. return 1;
  642. }
  643. /*
  644. * err_unshelve_state restores the error state that was returned
  645. * by err_shelve_state previously.
  646. */
  647. void err_unshelve_state(void* state)
  648. {
  649. if (state != (void*)-1)
  650. CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)state);
  651. }
  652. int ERR_get_next_error_library(void)
  653. {
  654. int ret;
  655. if (!RUN_ONCE(&err_string_init, do_err_strings_init))
  656. return 0;
  657. CRYPTO_THREAD_write_lock(err_string_lock);
  658. ret = int_err_library_number++;
  659. CRYPTO_THREAD_unlock(err_string_lock);
  660. return ret;
  661. }
  662. void ERR_set_error_data(char *data, int flags)
  663. {
  664. ERR_STATE *es;
  665. int i;
  666. es = ERR_get_state();
  667. if (es == NULL)
  668. return;
  669. i = es->top;
  670. err_clear_data(es, i);
  671. es->err_data[i] = data;
  672. es->err_data_flags[i] = flags;
  673. }
  674. void ERR_add_error_data(int num, ...)
  675. {
  676. va_list args;
  677. va_start(args, num);
  678. ERR_add_error_vdata(num, args);
  679. va_end(args);
  680. }
  681. void ERR_add_error_vdata(int num, va_list args)
  682. {
  683. int i, n, s;
  684. char *str, *p, *a;
  685. s = 80;
  686. if ((str = OPENSSL_malloc(s + 1)) == NULL) {
  687. /* ERRerr(ERR_F_ERR_ADD_ERROR_VDATA, ERR_R_MALLOC_FAILURE); */
  688. return;
  689. }
  690. str[0] = '\0';
  691. n = 0;
  692. for (i = 0; i < num; i++) {
  693. a = va_arg(args, char *);
  694. if (a == NULL)
  695. a = "<NULL>";
  696. n += strlen(a);
  697. if (n > s) {
  698. s = n + 20;
  699. p = OPENSSL_realloc(str, s + 1);
  700. if (p == NULL) {
  701. OPENSSL_free(str);
  702. return;
  703. }
  704. str = p;
  705. }
  706. OPENSSL_strlcat(str, a, (size_t)s + 1);
  707. }
  708. ERR_set_error_data(str, ERR_TXT_MALLOCED | ERR_TXT_STRING);
  709. }
  710. int ERR_set_mark(void)
  711. {
  712. ERR_STATE *es;
  713. es = ERR_get_state();
  714. if (es == NULL)
  715. return 0;
  716. if (es->bottom == es->top)
  717. return 0;
  718. es->err_flags[es->top] |= ERR_FLAG_MARK;
  719. return 1;
  720. }
  721. int ERR_pop_to_mark(void)
  722. {
  723. ERR_STATE *es;
  724. es = ERR_get_state();
  725. if (es == NULL)
  726. return 0;
  727. while (es->bottom != es->top
  728. && (es->err_flags[es->top] & ERR_FLAG_MARK) == 0) {
  729. err_clear(es, es->top);
  730. es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1;
  731. }
  732. if (es->bottom == es->top)
  733. return 0;
  734. es->err_flags[es->top] &= ~ERR_FLAG_MARK;
  735. return 1;
  736. }
  737. int ERR_clear_last_mark(void)
  738. {
  739. ERR_STATE *es;
  740. int top;
  741. es = ERR_get_state();
  742. if (es == NULL)
  743. return 0;
  744. top = es->top;
  745. while (es->bottom != top
  746. && (es->err_flags[top] & ERR_FLAG_MARK) == 0) {
  747. top = top > 0 ? top - 1 : ERR_NUM_ERRORS - 1;
  748. }
  749. if (es->bottom == top)
  750. return 0;
  751. es->err_flags[top] &= ~ERR_FLAG_MARK;
  752. return 1;
  753. }
  754. #ifdef UINTPTR_T
  755. # undef UINTPTR_T
  756. #endif
  757. /*
  758. * uintptr_t is the answer, but unfortunately C89, current "least common
  759. * denominator" doesn't define it. Most legacy platforms typedef it anyway,
  760. * so that attempt to fill the gaps means that one would have to identify
  761. * that track these gaps, which would be undesirable. Macro it is...
  762. */
  763. #if defined(__VMS) && __INITIAL_POINTER_SIZE==64
  764. /*
  765. * But we can't use size_t on VMS, because it adheres to sizeof(size_t)==4
  766. * even in 64-bit builds, which means that it won't work as mask.
  767. */
  768. # define UINTPTR_T unsigned long long
  769. #else
  770. # define UINTPTR_T size_t
  771. #endif
  772. void err_clear_last_constant_time(int clear)
  773. {
  774. ERR_STATE *es;
  775. int top;
  776. es = ERR_get_state();
  777. if (es == NULL)
  778. return;
  779. top = es->top;
  780. es->err_flags[top] &= ~(0 - clear);
  781. es->err_buffer[top] &= ~(0UL - clear);
  782. es->err_file[top] = (const char *)((UINTPTR_T)es->err_file[top] &
  783. ~((UINTPTR_T)0 - clear));
  784. es->err_line[top] |= 0 - clear;
  785. es->top = (top + ERR_NUM_ERRORS - clear) % ERR_NUM_ERRORS;
  786. }