2
0

err.c 22 KB

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