err.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. /*
  2. * Copyright 1995-2021 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. /* TODO: When ERR_STATE becomes opaque, this musts be removed */
  10. #define OSSL_FORCE_ERR_STATE
  11. #include <stdio.h>
  12. #include <stdarg.h>
  13. #include <string.h>
  14. #include "crypto/cryptlib.h"
  15. #include "internal/err.h"
  16. #include "crypto/err.h"
  17. #include <openssl/err.h>
  18. #include <openssl/crypto.h>
  19. #include <openssl/buffer.h>
  20. #include <openssl/bio.h>
  21. #include <openssl/opensslconf.h>
  22. #include "internal/thread_once.h"
  23. #include "crypto/ctype.h"
  24. #include "internal/constant_time.h"
  25. #include "e_os.h"
  26. #include "err_local.h"
  27. /* Forward declaration in case it's not published because of configuration */
  28. ERR_STATE *ERR_get_state(void);
  29. static int err_load_strings(const ERR_STRING_DATA *str);
  30. static void ERR_STATE_free(ERR_STATE *s);
  31. #ifndef OPENSSL_NO_ERR
  32. static ERR_STRING_DATA ERR_str_libraries[] = {
  33. {ERR_PACK(ERR_LIB_NONE, 0, 0), "unknown library"},
  34. {ERR_PACK(ERR_LIB_SYS, 0, 0), "system library"},
  35. {ERR_PACK(ERR_LIB_BN, 0, 0), "bignum routines"},
  36. {ERR_PACK(ERR_LIB_RSA, 0, 0), "rsa routines"},
  37. {ERR_PACK(ERR_LIB_DH, 0, 0), "Diffie-Hellman routines"},
  38. {ERR_PACK(ERR_LIB_EVP, 0, 0), "digital envelope routines"},
  39. {ERR_PACK(ERR_LIB_BUF, 0, 0), "memory buffer routines"},
  40. {ERR_PACK(ERR_LIB_OBJ, 0, 0), "object identifier routines"},
  41. {ERR_PACK(ERR_LIB_PEM, 0, 0), "PEM routines"},
  42. {ERR_PACK(ERR_LIB_DSA, 0, 0), "dsa routines"},
  43. {ERR_PACK(ERR_LIB_X509, 0, 0), "x509 certificate routines"},
  44. {ERR_PACK(ERR_LIB_ASN1, 0, 0), "asn1 encoding routines"},
  45. {ERR_PACK(ERR_LIB_CONF, 0, 0), "configuration file routines"},
  46. {ERR_PACK(ERR_LIB_CRYPTO, 0, 0), "common libcrypto routines"},
  47. {ERR_PACK(ERR_LIB_EC, 0, 0), "elliptic curve routines"},
  48. {ERR_PACK(ERR_LIB_ECDSA, 0, 0), "ECDSA routines"},
  49. {ERR_PACK(ERR_LIB_ECDH, 0, 0), "ECDH routines"},
  50. {ERR_PACK(ERR_LIB_SSL, 0, 0), "SSL routines"},
  51. {ERR_PACK(ERR_LIB_BIO, 0, 0), "BIO routines"},
  52. {ERR_PACK(ERR_LIB_PKCS7, 0, 0), "PKCS7 routines"},
  53. {ERR_PACK(ERR_LIB_X509V3, 0, 0), "X509 V3 routines"},
  54. {ERR_PACK(ERR_LIB_PKCS12, 0, 0), "PKCS12 routines"},
  55. {ERR_PACK(ERR_LIB_RAND, 0, 0), "random number generator"},
  56. {ERR_PACK(ERR_LIB_DSO, 0, 0), "DSO support routines"},
  57. {ERR_PACK(ERR_LIB_TS, 0, 0), "time stamp routines"},
  58. {ERR_PACK(ERR_LIB_ENGINE, 0, 0), "engine routines"},
  59. {ERR_PACK(ERR_LIB_OCSP, 0, 0), "OCSP routines"},
  60. {ERR_PACK(ERR_LIB_UI, 0, 0), "UI routines"},
  61. {ERR_PACK(ERR_LIB_FIPS, 0, 0), "FIPS routines"},
  62. {ERR_PACK(ERR_LIB_CMS, 0, 0), "CMS routines"},
  63. {ERR_PACK(ERR_LIB_CRMF, 0, 0), "CRMF routines"},
  64. {ERR_PACK(ERR_LIB_CMP, 0, 0), "CMP routines"},
  65. {ERR_PACK(ERR_LIB_HMAC, 0, 0), "HMAC routines"},
  66. {ERR_PACK(ERR_LIB_CT, 0, 0), "CT routines"},
  67. {ERR_PACK(ERR_LIB_ASYNC, 0, 0), "ASYNC routines"},
  68. {ERR_PACK(ERR_LIB_KDF, 0, 0), "KDF routines"},
  69. {ERR_PACK(ERR_LIB_OSSL_STORE, 0, 0), "STORE routines"},
  70. {ERR_PACK(ERR_LIB_SM2, 0, 0), "SM2 routines"},
  71. {ERR_PACK(ERR_LIB_ESS, 0, 0), "ESS routines"},
  72. {ERR_PACK(ERR_LIB_PROV, 0, 0), "Provider routines"},
  73. {ERR_PACK(ERR_LIB_OSSL_ENCODER, 0, 0), "ENCODER routines"},
  74. {ERR_PACK(ERR_LIB_OSSL_DECODER, 0, 0), "DECODER routines"},
  75. {ERR_PACK(ERR_LIB_HTTP, 0, 0), "HTTP routines"},
  76. {0, NULL},
  77. };
  78. static ERR_STRING_DATA ERR_str_reasons[] = {
  79. {ERR_R_SYS_LIB, "system lib"},
  80. {ERR_R_BN_LIB, "BN lib"},
  81. {ERR_R_RSA_LIB, "RSA lib"},
  82. {ERR_R_DH_LIB, "DH lib"},
  83. {ERR_R_EVP_LIB, "EVP lib"},
  84. {ERR_R_BUF_LIB, "BUF lib"},
  85. {ERR_R_OBJ_LIB, "OBJ lib"},
  86. {ERR_R_PEM_LIB, "PEM lib"},
  87. {ERR_R_DSA_LIB, "DSA lib"},
  88. {ERR_R_X509_LIB, "X509 lib"},
  89. {ERR_R_ASN1_LIB, "ASN1 lib"},
  90. {ERR_R_EC_LIB, "EC lib"},
  91. {ERR_R_BIO_LIB, "BIO lib"},
  92. {ERR_R_PKCS7_LIB, "PKCS7 lib"},
  93. {ERR_R_X509V3_LIB, "X509V3 lib"},
  94. {ERR_R_ENGINE_LIB, "ENGINE lib"},
  95. {ERR_R_UI_LIB, "UI lib"},
  96. {ERR_R_OSSL_STORE_LIB, "STORE lib"},
  97. {ERR_R_ECDSA_LIB, "ECDSA lib"},
  98. {ERR_R_NESTED_ASN1_ERROR, "nested asn1 error"},
  99. {ERR_R_MISSING_ASN1_EOS, "missing asn1 eos"},
  100. {ERR_R_FATAL, "fatal"},
  101. {ERR_R_MALLOC_FAILURE, "malloc failure"},
  102. {ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
  103. "called a function you should not call"},
  104. {ERR_R_PASSED_NULL_PARAMETER, "passed a null parameter"},
  105. {ERR_R_INTERNAL_ERROR, "internal error"},
  106. {ERR_R_DISABLED, "called a function that was disabled at compile-time"},
  107. {ERR_R_INIT_FAIL, "init fail"},
  108. {ERR_R_OPERATION_FAIL, "operation fail"},
  109. {ERR_R_INVALID_PROVIDER_FUNCTIONS, "invalid provider functions"},
  110. {ERR_R_INTERRUPTED_OR_CANCELLED, "interrupted or cancelled"},
  111. /*
  112. * Something is unsupported, exactly what is expressed with additional data
  113. */
  114. {ERR_R_UNSUPPORTED, "unsupported"},
  115. /*
  116. * A fetch failed for other reasons than the name to be fetched being
  117. * unsupported.
  118. */
  119. {ERR_R_FETCH_FAILED, "fetch failed"},
  120. {0, NULL},
  121. };
  122. #endif
  123. static CRYPTO_ONCE err_init = CRYPTO_ONCE_STATIC_INIT;
  124. static int set_err_thread_local;
  125. static CRYPTO_THREAD_LOCAL err_thread_local;
  126. static CRYPTO_ONCE err_string_init = CRYPTO_ONCE_STATIC_INIT;
  127. static CRYPTO_RWLOCK *err_string_lock;
  128. static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *);
  129. /*
  130. * The internal state
  131. */
  132. static LHASH_OF(ERR_STRING_DATA) *int_error_hash = NULL;
  133. static int int_err_library_number = ERR_LIB_USER;
  134. typedef enum ERR_GET_ACTION_e {
  135. EV_POP, EV_PEEK, EV_PEEK_LAST
  136. } ERR_GET_ACTION;
  137. static unsigned long get_error_values(ERR_GET_ACTION g,
  138. const char **file, int *line,
  139. const char **func, const char **data,
  140. int *flags);
  141. static unsigned long err_string_data_hash(const ERR_STRING_DATA *a)
  142. {
  143. unsigned long ret, l;
  144. l = a->error;
  145. ret = l ^ ERR_GET_LIB(l);
  146. return (ret ^ ret % 19 * 13);
  147. }
  148. static int err_string_data_cmp(const ERR_STRING_DATA *a,
  149. const ERR_STRING_DATA *b)
  150. {
  151. if (a->error == b->error)
  152. return 0;
  153. return a->error > b->error ? 1 : -1;
  154. }
  155. static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *d)
  156. {
  157. ERR_STRING_DATA *p = NULL;
  158. if (!CRYPTO_THREAD_read_lock(err_string_lock))
  159. return NULL;
  160. p = lh_ERR_STRING_DATA_retrieve(int_error_hash, d);
  161. CRYPTO_THREAD_unlock(err_string_lock);
  162. return p;
  163. }
  164. static void ERR_STATE_free(ERR_STATE *s)
  165. {
  166. int i;
  167. if (s == NULL)
  168. return;
  169. for (i = 0; i < ERR_NUM_ERRORS; i++) {
  170. err_clear(s, i, 1);
  171. }
  172. OPENSSL_free(s);
  173. }
  174. DEFINE_RUN_ONCE_STATIC(do_err_strings_init)
  175. {
  176. if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
  177. return 0;
  178. err_string_lock = CRYPTO_THREAD_lock_new();
  179. if (err_string_lock == NULL)
  180. return 0;
  181. int_error_hash = lh_ERR_STRING_DATA_new(err_string_data_hash,
  182. err_string_data_cmp);
  183. if (int_error_hash == NULL) {
  184. CRYPTO_THREAD_lock_free(err_string_lock);
  185. err_string_lock = NULL;
  186. return 0;
  187. }
  188. return 1;
  189. }
  190. void err_cleanup(void)
  191. {
  192. if (set_err_thread_local != 0)
  193. CRYPTO_THREAD_cleanup_local(&err_thread_local);
  194. CRYPTO_THREAD_lock_free(err_string_lock);
  195. err_string_lock = NULL;
  196. lh_ERR_STRING_DATA_free(int_error_hash);
  197. int_error_hash = NULL;
  198. }
  199. /*
  200. * Legacy; pack in the library.
  201. */
  202. static void err_patch(int lib, ERR_STRING_DATA *str)
  203. {
  204. unsigned long plib = ERR_PACK(lib, 0, 0);
  205. for (; str->error != 0; str++)
  206. str->error |= plib;
  207. }
  208. /*
  209. * Hash in |str| error strings. Assumes the RUN_ONCE was done.
  210. */
  211. static int err_load_strings(const ERR_STRING_DATA *str)
  212. {
  213. if (!CRYPTO_THREAD_write_lock(err_string_lock))
  214. return 0;
  215. for (; str->error; str++)
  216. (void)lh_ERR_STRING_DATA_insert(int_error_hash,
  217. (ERR_STRING_DATA *)str);
  218. CRYPTO_THREAD_unlock(err_string_lock);
  219. return 1;
  220. }
  221. int err_load_ERR_strings_int(void)
  222. {
  223. #ifndef OPENSSL_NO_ERR
  224. if (!RUN_ONCE(&err_string_init, do_err_strings_init))
  225. return 0;
  226. err_load_strings(ERR_str_libraries);
  227. err_load_strings(ERR_str_reasons);
  228. #endif
  229. return 1;
  230. }
  231. int ERR_load_strings(int lib, ERR_STRING_DATA *str)
  232. {
  233. if (err_load_ERR_strings_int() == 0)
  234. return 0;
  235. err_patch(lib, str);
  236. err_load_strings(str);
  237. return 1;
  238. }
  239. int ERR_load_strings_const(const ERR_STRING_DATA *str)
  240. {
  241. if (err_load_ERR_strings_int() == 0)
  242. return 0;
  243. err_load_strings(str);
  244. return 1;
  245. }
  246. int ERR_unload_strings(int lib, ERR_STRING_DATA *str)
  247. {
  248. if (!RUN_ONCE(&err_string_init, do_err_strings_init))
  249. return 0;
  250. if (!CRYPTO_THREAD_write_lock(err_string_lock))
  251. return 0;
  252. /*
  253. * We don't need to ERR_PACK the lib, since that was done (to
  254. * the table) when it was loaded.
  255. */
  256. for (; str->error; str++)
  257. (void)lh_ERR_STRING_DATA_delete(int_error_hash, str);
  258. CRYPTO_THREAD_unlock(err_string_lock);
  259. return 1;
  260. }
  261. void err_free_strings_int(void)
  262. {
  263. if (!RUN_ONCE(&err_string_init, do_err_strings_init))
  264. return;
  265. }
  266. /********************************************************/
  267. void ERR_clear_error(void)
  268. {
  269. int i;
  270. ERR_STATE *es;
  271. es = ossl_err_get_state_int();
  272. if (es == NULL)
  273. return;
  274. for (i = 0; i < ERR_NUM_ERRORS; i++) {
  275. err_clear(es, i, 0);
  276. }
  277. es->top = es->bottom = 0;
  278. }
  279. unsigned long ERR_get_error(void)
  280. {
  281. return get_error_values(EV_POP, NULL, NULL, NULL, NULL, NULL);
  282. }
  283. unsigned long ERR_get_error_all(const char **file, int *line,
  284. const char **func,
  285. const char **data, int *flags)
  286. {
  287. return get_error_values(EV_POP, file, line, func, data, flags);
  288. }
  289. #ifndef OPENSSL_NO_DEPRECATED_3_0
  290. unsigned long ERR_get_error_line(const char **file, int *line)
  291. {
  292. return get_error_values(EV_POP, file, line, NULL, NULL, NULL);
  293. }
  294. unsigned long ERR_get_error_line_data(const char **file, int *line,
  295. const char **data, int *flags)
  296. {
  297. return get_error_values(EV_POP, file, line, NULL, data, flags);
  298. }
  299. #endif
  300. unsigned long ERR_peek_error(void)
  301. {
  302. return get_error_values(EV_PEEK, NULL, NULL, NULL, NULL, NULL);
  303. }
  304. unsigned long ERR_peek_error_line(const char **file, int *line)
  305. {
  306. return get_error_values(EV_PEEK, file, line, NULL, NULL, NULL);
  307. }
  308. unsigned long ERR_peek_error_func(const char **func)
  309. {
  310. return get_error_values(EV_PEEK, NULL, NULL, func, NULL, NULL);
  311. }
  312. unsigned long ERR_peek_error_data(const char **data, int *flags)
  313. {
  314. return get_error_values(EV_PEEK, NULL, NULL, NULL, data, flags);
  315. }
  316. unsigned long ERR_peek_error_all(const char **file, int *line,
  317. const char **func,
  318. const char **data, int *flags)
  319. {
  320. return get_error_values(EV_PEEK, file, line, func, data, flags);
  321. }
  322. #ifndef OPENSSL_NO_DEPRECATED_3_0
  323. unsigned long ERR_peek_error_line_data(const char **file, int *line,
  324. const char **data, int *flags)
  325. {
  326. return get_error_values(EV_PEEK, file, line, NULL, data, flags);
  327. }
  328. #endif
  329. unsigned long ERR_peek_last_error(void)
  330. {
  331. return get_error_values(EV_PEEK_LAST, NULL, NULL, NULL, NULL, NULL);
  332. }
  333. unsigned long ERR_peek_last_error_line(const char **file, int *line)
  334. {
  335. return get_error_values(EV_PEEK_LAST, file, line, NULL, NULL, NULL);
  336. }
  337. unsigned long ERR_peek_last_error_func(const char **func)
  338. {
  339. return get_error_values(EV_PEEK_LAST, NULL, NULL, func, NULL, NULL);
  340. }
  341. unsigned long ERR_peek_last_error_data(const char **data, int *flags)
  342. {
  343. return get_error_values(EV_PEEK_LAST, NULL, NULL, NULL, data, flags);
  344. }
  345. unsigned long ERR_peek_last_error_all(const char **file, int *line,
  346. const char **func,
  347. const char **data, int *flags)
  348. {
  349. return get_error_values(EV_PEEK_LAST, file, line, func, data, flags);
  350. }
  351. #ifndef OPENSSL_NO_DEPRECATED_3_0
  352. unsigned long ERR_peek_last_error_line_data(const char **file, int *line,
  353. const char **data, int *flags)
  354. {
  355. return get_error_values(EV_PEEK_LAST, file, line, NULL, data, flags);
  356. }
  357. #endif
  358. static unsigned long get_error_values(ERR_GET_ACTION g,
  359. const char **file, int *line,
  360. const char **func,
  361. const char **data, int *flags)
  362. {
  363. int i = 0;
  364. ERR_STATE *es;
  365. unsigned long ret;
  366. es = ossl_err_get_state_int();
  367. if (es == NULL)
  368. return 0;
  369. /*
  370. * Clear anything that should have been cleared earlier. We do this
  371. * here because this doesn't have constant-time issues.
  372. */
  373. while (es->bottom != es->top) {
  374. if (es->err_flags[es->top] & ERR_FLAG_CLEAR) {
  375. err_clear(es, es->top, 0);
  376. es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1;
  377. continue;
  378. }
  379. i = (es->bottom + 1) % ERR_NUM_ERRORS;
  380. if (es->err_flags[i] & ERR_FLAG_CLEAR) {
  381. es->bottom = i;
  382. err_clear(es, es->bottom, 0);
  383. continue;
  384. }
  385. break;
  386. }
  387. /* If everything has been cleared, the stack is empty. */
  388. if (es->bottom == es->top)
  389. return 0;
  390. /* Which error, the top of stack (latest one) or the first one? */
  391. if (g == EV_PEEK_LAST)
  392. i = es->top;
  393. else
  394. i = (es->bottom + 1) % ERR_NUM_ERRORS;
  395. ret = es->err_buffer[i];
  396. if (g == EV_POP) {
  397. es->bottom = i;
  398. es->err_buffer[i] = 0;
  399. }
  400. if (file != NULL) {
  401. *file = es->err_file[i];
  402. if (*file == NULL)
  403. *file = "";
  404. }
  405. if (line != NULL)
  406. *line = es->err_line[i];
  407. if (func != NULL) {
  408. *func = es->err_func[i];
  409. if (*func == NULL)
  410. *func = "";
  411. }
  412. if (flags != NULL)
  413. *flags = es->err_data_flags[i];
  414. if (data == NULL) {
  415. if (g == EV_POP) {
  416. err_clear_data(es, i, 0);
  417. }
  418. } else {
  419. *data = es->err_data[i];
  420. if (*data == NULL) {
  421. *data = "";
  422. if (flags != NULL)
  423. *flags = 0;
  424. }
  425. }
  426. return ret;
  427. }
  428. void ossl_err_string_int(unsigned long e, const char *func,
  429. char *buf, size_t len)
  430. {
  431. char lsbuf[64], rsbuf[256];
  432. const char *ls, *rs = NULL;
  433. unsigned long l, r;
  434. if (len == 0)
  435. return;
  436. l = ERR_GET_LIB(e);
  437. ls = ERR_lib_error_string(e);
  438. if (ls == NULL) {
  439. BIO_snprintf(lsbuf, sizeof(lsbuf), "lib(%lu)", l);
  440. ls = lsbuf;
  441. }
  442. /*
  443. * ERR_reason_error_string() can't safely return system error strings,
  444. * since it would call openssl_strerror_r(), which needs a buffer for
  445. * thread safety. So for system errors, we call openssl_strerror_r()
  446. * directly instead.
  447. */
  448. r = ERR_GET_REASON(e);
  449. #ifndef OPENSSL_NO_ERR
  450. if (ERR_SYSTEM_ERROR(e)) {
  451. if (openssl_strerror_r(r, rsbuf, sizeof(rsbuf)))
  452. rs = rsbuf;
  453. } else {
  454. rs = ERR_reason_error_string(e);
  455. }
  456. #endif
  457. if (rs == NULL) {
  458. BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", r);
  459. rs = rsbuf;
  460. }
  461. BIO_snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls, func, rs);
  462. if (strlen(buf) == len - 1) {
  463. /* Didn't fit; use a minimal format. */
  464. BIO_snprintf(buf, len, "err:%lx:%lx:%lx:%lx", e, l, 0L, r);
  465. }
  466. }
  467. void ERR_error_string_n(unsigned long e, char *buf, size_t len)
  468. {
  469. ossl_err_string_int(e, "", buf, len);
  470. }
  471. /*
  472. * ERR_error_string_n should be used instead for ret != NULL as
  473. * ERR_error_string cannot know how large the buffer is
  474. */
  475. char *ERR_error_string(unsigned long e, char *ret)
  476. {
  477. static char buf[256];
  478. if (ret == NULL)
  479. ret = buf;
  480. ERR_error_string_n(e, ret, (int)sizeof(buf));
  481. return ret;
  482. }
  483. const char *ERR_lib_error_string(unsigned long e)
  484. {
  485. ERR_STRING_DATA d, *p;
  486. unsigned long l;
  487. if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
  488. return NULL;
  489. }
  490. l = ERR_GET_LIB(e);
  491. d.error = ERR_PACK(l, 0, 0);
  492. p = int_err_get_item(&d);
  493. return ((p == NULL) ? NULL : p->string);
  494. }
  495. #ifndef OPENSSL_NO_DEPRECATED_3_0
  496. const char *ERR_func_error_string(unsigned long e)
  497. {
  498. return NULL;
  499. }
  500. #endif
  501. const char *ERR_reason_error_string(unsigned long e)
  502. {
  503. ERR_STRING_DATA d, *p = NULL;
  504. unsigned long l, r;
  505. if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
  506. return NULL;
  507. }
  508. /*
  509. * ERR_reason_error_string() can't safely return system error strings,
  510. * since openssl_strerror_r() needs a buffer for thread safety, and we
  511. * haven't got one that would serve any sensible purpose.
  512. */
  513. if (ERR_SYSTEM_ERROR(e))
  514. return NULL;
  515. l = ERR_GET_LIB(e);
  516. r = ERR_GET_REASON(e);
  517. d.error = ERR_PACK(l, 0, r);
  518. p = int_err_get_item(&d);
  519. if (p == NULL) {
  520. d.error = ERR_PACK(0, 0, r);
  521. p = int_err_get_item(&d);
  522. }
  523. return ((p == NULL) ? NULL : p->string);
  524. }
  525. static void err_delete_thread_state(void *unused)
  526. {
  527. ERR_STATE *state = CRYPTO_THREAD_get_local(&err_thread_local);
  528. if (state == NULL)
  529. return;
  530. CRYPTO_THREAD_set_local(&err_thread_local, NULL);
  531. ERR_STATE_free(state);
  532. }
  533. #ifndef OPENSSL_NO_DEPRECATED_1_1_0
  534. void ERR_remove_thread_state(void *dummy)
  535. {
  536. }
  537. #endif
  538. #ifndef OPENSSL_NO_DEPRECATED_1_0_0
  539. void ERR_remove_state(unsigned long pid)
  540. {
  541. }
  542. #endif
  543. DEFINE_RUN_ONCE_STATIC(err_do_init)
  544. {
  545. set_err_thread_local = 1;
  546. return CRYPTO_THREAD_init_local(&err_thread_local, NULL);
  547. }
  548. ERR_STATE *ossl_err_get_state_int(void)
  549. {
  550. ERR_STATE *state;
  551. int saveerrno = get_last_sys_error();
  552. if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
  553. return NULL;
  554. if (!RUN_ONCE(&err_init, err_do_init))
  555. return NULL;
  556. state = CRYPTO_THREAD_get_local(&err_thread_local);
  557. if (state == (ERR_STATE*)-1)
  558. return NULL;
  559. if (state == NULL) {
  560. if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1))
  561. return NULL;
  562. if ((state = OPENSSL_zalloc(sizeof(*state))) == NULL) {
  563. CRYPTO_THREAD_set_local(&err_thread_local, NULL);
  564. return NULL;
  565. }
  566. if (!ossl_init_thread_start(NULL, NULL, err_delete_thread_state)
  567. || !CRYPTO_THREAD_set_local(&err_thread_local, state)) {
  568. ERR_STATE_free(state);
  569. CRYPTO_THREAD_set_local(&err_thread_local, NULL);
  570. return NULL;
  571. }
  572. /* Ignore failures from these */
  573. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
  574. }
  575. set_sys_error(saveerrno);
  576. return state;
  577. }
  578. #ifndef OPENSSL_NO_DEPRECATED_3_0
  579. ERR_STATE *ERR_get_state(void)
  580. {
  581. return ossl_err_get_state_int();
  582. }
  583. #endif
  584. /*
  585. * err_shelve_state returns the current thread local error state
  586. * and freezes the error module until err_unshelve_state is called.
  587. */
  588. int err_shelve_state(void **state)
  589. {
  590. int saveerrno = get_last_sys_error();
  591. /*
  592. * Note, at present our only caller is OPENSSL_init_crypto(), indirectly
  593. * via ossl_init_load_crypto_nodelete(), by which point the requested
  594. * "base" initialization has already been performed, so the below call is a
  595. * NOOP, that re-enters OPENSSL_init_crypto() only to quickly return.
  596. *
  597. * If are no other valid callers of this function, the call below can be
  598. * removed, avoiding the re-entry into OPENSSL_init_crypto(). If there are
  599. * potential uses that are not from inside OPENSSL_init_crypto(), then this
  600. * call is needed, but some care is required to make sure that the re-entry
  601. * remains a NOOP.
  602. */
  603. if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
  604. return 0;
  605. if (!RUN_ONCE(&err_init, err_do_init))
  606. return 0;
  607. *state = CRYPTO_THREAD_get_local(&err_thread_local);
  608. if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1))
  609. return 0;
  610. set_sys_error(saveerrno);
  611. return 1;
  612. }
  613. /*
  614. * err_unshelve_state restores the error state that was returned
  615. * by err_shelve_state previously.
  616. */
  617. void err_unshelve_state(void* state)
  618. {
  619. if (state != (void*)-1)
  620. CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)state);
  621. }
  622. int ERR_get_next_error_library(void)
  623. {
  624. int ret;
  625. if (!RUN_ONCE(&err_string_init, do_err_strings_init))
  626. return 0;
  627. if (!CRYPTO_THREAD_write_lock(err_string_lock))
  628. return 0;
  629. ret = int_err_library_number++;
  630. CRYPTO_THREAD_unlock(err_string_lock);
  631. return ret;
  632. }
  633. static int err_set_error_data_int(char *data, size_t size, int flags,
  634. int deallocate)
  635. {
  636. ERR_STATE *es;
  637. es = ossl_err_get_state_int();
  638. if (es == NULL)
  639. return 0;
  640. err_clear_data(es, es->top, deallocate);
  641. err_set_data(es, es->top, data, size, flags);
  642. return 1;
  643. }
  644. void ERR_set_error_data(char *data, int flags)
  645. {
  646. /*
  647. * This function is void so we cannot propagate the error return. Since it
  648. * is also in the public API we can't change the return type.
  649. *
  650. * We estimate the size of the data. If it's not flagged as allocated,
  651. * then this is safe, and if it is flagged as allocated, then our size
  652. * may be smaller than the actual allocation, but that doesn't matter
  653. * too much, the buffer will remain untouched or will eventually be
  654. * reallocated to a new size.
  655. *
  656. * callers should be advised that this function takes over ownership of
  657. * the allocated memory, i.e. they can't count on the pointer to remain
  658. * valid.
  659. */
  660. err_set_error_data_int(data, strlen(data) + 1, flags, 1);
  661. }
  662. void ERR_add_error_data(int num, ...)
  663. {
  664. va_list args;
  665. va_start(args, num);
  666. ERR_add_error_vdata(num, args);
  667. va_end(args);
  668. }
  669. void ERR_add_error_vdata(int num, va_list args)
  670. {
  671. int i, len, size;
  672. int flags = ERR_TXT_MALLOCED | ERR_TXT_STRING;
  673. char *str, *arg;
  674. ERR_STATE *es;
  675. /* Get the current error data; if an allocated string get it. */
  676. es = ossl_err_get_state_int();
  677. if (es == NULL)
  678. return;
  679. i = es->top;
  680. /*
  681. * If err_data is allocated already, re-use the space.
  682. * Otherwise, allocate a small new buffer.
  683. */
  684. if ((es->err_data_flags[i] & flags) == flags) {
  685. str = es->err_data[i];
  686. size = es->err_data_size[i];
  687. /*
  688. * To protect the string we just grabbed from tampering by other
  689. * functions we may call, or to protect them from freeing a pointer
  690. * that may no longer be valid at that point, we clear away the
  691. * data pointer and the flags. We will set them again at the end
  692. * of this function.
  693. */
  694. es->err_data[i] = NULL;
  695. es->err_data_flags[i] = 0;
  696. } else if ((str = OPENSSL_malloc(size = 81)) == NULL) {
  697. return;
  698. } else {
  699. str[0] = '\0';
  700. }
  701. len = strlen(str);
  702. while (--num >= 0) {
  703. arg = va_arg(args, char *);
  704. if (arg == NULL)
  705. arg = "<NULL>";
  706. len += strlen(arg);
  707. if (len >= size) {
  708. char *p;
  709. size = len + 20;
  710. p = OPENSSL_realloc(str, size);
  711. if (p == NULL) {
  712. OPENSSL_free(str);
  713. return;
  714. }
  715. str = p;
  716. }
  717. OPENSSL_strlcat(str, arg, (size_t)size);
  718. }
  719. if (!err_set_error_data_int(str, size, flags, 0))
  720. OPENSSL_free(str);
  721. }
  722. int ERR_set_mark(void)
  723. {
  724. ERR_STATE *es;
  725. es = ossl_err_get_state_int();
  726. if (es == NULL)
  727. return 0;
  728. if (es->bottom == es->top)
  729. return 0;
  730. es->err_marks[es->top]++;
  731. return 1;
  732. }
  733. int ERR_pop_to_mark(void)
  734. {
  735. ERR_STATE *es;
  736. es = ossl_err_get_state_int();
  737. if (es == NULL)
  738. return 0;
  739. while (es->bottom != es->top
  740. && es->err_marks[es->top] == 0) {
  741. err_clear(es, es->top, 0);
  742. es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1;
  743. }
  744. if (es->bottom == es->top)
  745. return 0;
  746. es->err_marks[es->top]--;
  747. return 1;
  748. }
  749. int ERR_clear_last_mark(void)
  750. {
  751. ERR_STATE *es;
  752. int top;
  753. es = ossl_err_get_state_int();
  754. if (es == NULL)
  755. return 0;
  756. top = es->top;
  757. while (es->bottom != top
  758. && es->err_marks[top] == 0) {
  759. top = top > 0 ? top - 1 : ERR_NUM_ERRORS - 1;
  760. }
  761. if (es->bottom == top)
  762. return 0;
  763. es->err_marks[top]--;
  764. return 1;
  765. }
  766. void err_clear_last_constant_time(int clear)
  767. {
  768. ERR_STATE *es;
  769. int top;
  770. es = ossl_err_get_state_int();
  771. if (es == NULL)
  772. return;
  773. top = es->top;
  774. /*
  775. * Flag error as cleared but remove it elsewhere to avoid two errors
  776. * accessing the same error stack location, revealing timing information.
  777. */
  778. clear = constant_time_select_int(constant_time_eq_int(clear, 0),
  779. 0, ERR_FLAG_CLEAR);
  780. es->err_flags[top] |= clear;
  781. }