err.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. /*
  2. * Copyright 1995-2020 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. {0, NULL},
  112. };
  113. #endif
  114. static CRYPTO_ONCE err_init = CRYPTO_ONCE_STATIC_INIT;
  115. static int set_err_thread_local;
  116. static CRYPTO_THREAD_LOCAL err_thread_local;
  117. static CRYPTO_ONCE err_string_init = CRYPTO_ONCE_STATIC_INIT;
  118. static CRYPTO_RWLOCK *err_string_lock;
  119. static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *);
  120. /*
  121. * The internal state
  122. */
  123. static LHASH_OF(ERR_STRING_DATA) *int_error_hash = NULL;
  124. static int int_err_library_number = ERR_LIB_USER;
  125. typedef enum ERR_GET_ACTION_e {
  126. EV_POP, EV_PEEK, EV_PEEK_LAST
  127. } ERR_GET_ACTION;
  128. static unsigned long get_error_values(ERR_GET_ACTION g,
  129. const char **file, int *line,
  130. const char **func, const char **data,
  131. int *flags);
  132. static unsigned long err_string_data_hash(const ERR_STRING_DATA *a)
  133. {
  134. unsigned long ret, l;
  135. l = a->error;
  136. ret = l ^ ERR_GET_LIB(l);
  137. return (ret ^ ret % 19 * 13);
  138. }
  139. static int err_string_data_cmp(const ERR_STRING_DATA *a,
  140. const ERR_STRING_DATA *b)
  141. {
  142. if (a->error == b->error)
  143. return 0;
  144. return a->error > b->error ? 1 : -1;
  145. }
  146. static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *d)
  147. {
  148. ERR_STRING_DATA *p = NULL;
  149. CRYPTO_THREAD_read_lock(err_string_lock);
  150. p = lh_ERR_STRING_DATA_retrieve(int_error_hash, d);
  151. CRYPTO_THREAD_unlock(err_string_lock);
  152. return p;
  153. }
  154. static void ERR_STATE_free(ERR_STATE *s)
  155. {
  156. int i;
  157. if (s == NULL)
  158. return;
  159. for (i = 0; i < ERR_NUM_ERRORS; i++) {
  160. err_clear_data(s, i, 1);
  161. }
  162. OPENSSL_free(s);
  163. }
  164. DEFINE_RUN_ONCE_STATIC(do_err_strings_init)
  165. {
  166. if (!OPENSSL_init_crypto(0, NULL))
  167. return 0;
  168. err_string_lock = CRYPTO_THREAD_lock_new();
  169. if (err_string_lock == NULL)
  170. return 0;
  171. int_error_hash = lh_ERR_STRING_DATA_new(err_string_data_hash,
  172. err_string_data_cmp);
  173. if (int_error_hash == NULL) {
  174. CRYPTO_THREAD_lock_free(err_string_lock);
  175. err_string_lock = NULL;
  176. return 0;
  177. }
  178. return 1;
  179. }
  180. void err_cleanup(void)
  181. {
  182. if (set_err_thread_local != 0)
  183. CRYPTO_THREAD_cleanup_local(&err_thread_local);
  184. CRYPTO_THREAD_lock_free(err_string_lock);
  185. err_string_lock = NULL;
  186. lh_ERR_STRING_DATA_free(int_error_hash);
  187. int_error_hash = NULL;
  188. }
  189. /*
  190. * Legacy; pack in the library.
  191. */
  192. static void err_patch(int lib, ERR_STRING_DATA *str)
  193. {
  194. unsigned long plib = ERR_PACK(lib, 0, 0);
  195. for (; str->error != 0; str++)
  196. str->error |= plib;
  197. }
  198. /*
  199. * Hash in |str| error strings. Assumes the URN_ONCE was done.
  200. */
  201. static int err_load_strings(const ERR_STRING_DATA *str)
  202. {
  203. CRYPTO_THREAD_write_lock(err_string_lock);
  204. for (; str->error; str++)
  205. (void)lh_ERR_STRING_DATA_insert(int_error_hash,
  206. (ERR_STRING_DATA *)str);
  207. CRYPTO_THREAD_unlock(err_string_lock);
  208. return 1;
  209. }
  210. int ERR_load_ERR_strings(void)
  211. {
  212. #ifndef OPENSSL_NO_ERR
  213. if (!RUN_ONCE(&err_string_init, do_err_strings_init))
  214. return 0;
  215. err_load_strings(ERR_str_libraries);
  216. err_load_strings(ERR_str_reasons);
  217. #endif
  218. return 1;
  219. }
  220. int ERR_load_strings(int lib, ERR_STRING_DATA *str)
  221. {
  222. if (ERR_load_ERR_strings() == 0)
  223. return 0;
  224. err_patch(lib, str);
  225. err_load_strings(str);
  226. return 1;
  227. }
  228. int ERR_load_strings_const(const ERR_STRING_DATA *str)
  229. {
  230. if (ERR_load_ERR_strings() == 0)
  231. return 0;
  232. err_load_strings(str);
  233. return 1;
  234. }
  235. int ERR_unload_strings(int lib, ERR_STRING_DATA *str)
  236. {
  237. if (!RUN_ONCE(&err_string_init, do_err_strings_init))
  238. return 0;
  239. CRYPTO_THREAD_write_lock(err_string_lock);
  240. /*
  241. * We don't need to ERR_PACK the lib, since that was done (to
  242. * the table) when it was loaded.
  243. */
  244. for (; str->error; str++)
  245. (void)lh_ERR_STRING_DATA_delete(int_error_hash, str);
  246. CRYPTO_THREAD_unlock(err_string_lock);
  247. return 1;
  248. }
  249. void err_free_strings_int(void)
  250. {
  251. if (!RUN_ONCE(&err_string_init, do_err_strings_init))
  252. return;
  253. }
  254. /********************************************************/
  255. void ERR_clear_error(void)
  256. {
  257. int i;
  258. ERR_STATE *es;
  259. es = err_get_state_int();
  260. if (es == NULL)
  261. return;
  262. for (i = 0; i < ERR_NUM_ERRORS; i++) {
  263. err_clear(es, i, 0);
  264. }
  265. es->top = es->bottom = 0;
  266. }
  267. unsigned long ERR_get_error(void)
  268. {
  269. return get_error_values(EV_POP, NULL, NULL, NULL, NULL, NULL);
  270. }
  271. unsigned long ERR_get_error_line(const char **file, int *line)
  272. {
  273. return get_error_values(EV_POP, file, line, NULL, NULL, NULL);
  274. }
  275. unsigned long ERR_get_error_func(const char **func)
  276. {
  277. return get_error_values(EV_POP, NULL, NULL, func, NULL, NULL);
  278. }
  279. unsigned long ERR_get_error_data(const char **data, int *flags)
  280. {
  281. return get_error_values(EV_POP, NULL, NULL, NULL, data, flags);
  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_data(const char **file, int *line,
  291. const char **data, int *flags)
  292. {
  293. return get_error_values(EV_POP, file, line, NULL, data, flags);
  294. }
  295. #endif
  296. unsigned long ERR_peek_error(void)
  297. {
  298. return get_error_values(EV_PEEK, NULL, NULL, NULL, NULL, NULL);
  299. }
  300. unsigned long ERR_peek_error_line(const char **file, int *line)
  301. {
  302. return get_error_values(EV_PEEK, file, line, NULL, NULL, NULL);
  303. }
  304. unsigned long ERR_peek_error_func(const char **func)
  305. {
  306. return get_error_values(EV_PEEK, NULL, NULL, func, NULL, NULL);
  307. }
  308. unsigned long ERR_peek_error_data(const char **data, int *flags)
  309. {
  310. return get_error_values(EV_PEEK, NULL, NULL, NULL, data, flags);
  311. }
  312. unsigned long ERR_peek_error_all(const char **file, int *line,
  313. const char **func,
  314. const char **data, int *flags)
  315. {
  316. return get_error_values(EV_PEEK, file, line, func, data, flags);
  317. }
  318. #ifndef OPENSSL_NO_DEPRECATED_3_0
  319. unsigned long ERR_peek_error_line_data(const char **file, int *line,
  320. const char **data, int *flags)
  321. {
  322. return get_error_values(EV_PEEK, file, line, NULL, data, flags);
  323. }
  324. #endif
  325. unsigned long ERR_peek_last_error(void)
  326. {
  327. return get_error_values(EV_PEEK_LAST, NULL, NULL, NULL, NULL, NULL);
  328. }
  329. unsigned long ERR_peek_last_error_line(const char **file, int *line)
  330. {
  331. return get_error_values(EV_PEEK_LAST, file, line, NULL, NULL, NULL);
  332. }
  333. unsigned long ERR_peek_last_error_func(const char **func)
  334. {
  335. return get_error_values(EV_PEEK_LAST, NULL, NULL, func, NULL, NULL);
  336. }
  337. unsigned long ERR_peek_last_error_data(const char **data, int *flags)
  338. {
  339. return get_error_values(EV_PEEK_LAST, NULL, NULL, NULL, data, flags);
  340. }
  341. unsigned long ERR_peek_last_error_all(const char **file, int *line,
  342. const char **func,
  343. const char **data, int *flags)
  344. {
  345. return get_error_values(EV_PEEK_LAST, file, line, func, data, flags);
  346. }
  347. #ifndef OPENSSL_NO_DEPRECATED_3_0
  348. unsigned long ERR_peek_last_error_line_data(const char **file, int *line,
  349. const char **data, int *flags)
  350. {
  351. return get_error_values(EV_PEEK_LAST, file, line, NULL, data, flags);
  352. }
  353. #endif
  354. static unsigned long get_error_values(ERR_GET_ACTION g,
  355. const char **file, int *line,
  356. const char **func,
  357. const char **data, int *flags)
  358. {
  359. int i = 0;
  360. ERR_STATE *es;
  361. unsigned long ret;
  362. es = err_get_state_int();
  363. if (es == NULL)
  364. return 0;
  365. /*
  366. * Clear anything that should have been cleared earlier. We do this
  367. * here because this doesn't have constant-time issues.
  368. */
  369. while (es->bottom != es->top) {
  370. if (es->err_flags[es->top] & ERR_FLAG_CLEAR) {
  371. err_clear(es, es->top, 0);
  372. es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1;
  373. continue;
  374. }
  375. i = (es->bottom + 1) % ERR_NUM_ERRORS;
  376. if (es->err_flags[i] & ERR_FLAG_CLEAR) {
  377. es->bottom = i;
  378. err_clear(es, es->bottom, 0);
  379. continue;
  380. }
  381. break;
  382. }
  383. /* If everything has been cleared, the stack is empty. */
  384. if (es->bottom == es->top)
  385. return 0;
  386. /* Which error, the top of stack (latest one) or the first one? */
  387. if (g == EV_PEEK_LAST)
  388. i = es->top;
  389. else
  390. i = (es->bottom + 1) % ERR_NUM_ERRORS;
  391. ret = es->err_buffer[i];
  392. if (g == EV_POP) {
  393. es->bottom = i;
  394. es->err_buffer[i] = 0;
  395. }
  396. if (file != NULL) {
  397. *file = es->err_file[i];
  398. if (*file == NULL)
  399. *file = "";
  400. }
  401. if (line != NULL)
  402. *line = es->err_line[i];
  403. if (func != NULL) {
  404. *func = es->err_func[i];
  405. if (*func == NULL)
  406. *func = "";
  407. }
  408. if (flags != NULL)
  409. *flags = es->err_data_flags[i];
  410. if (data == NULL) {
  411. if (g == EV_POP) {
  412. err_clear_data(es, i, 0);
  413. }
  414. } else {
  415. *data = es->err_data[i];
  416. if (*data == NULL) {
  417. *data = "";
  418. if (flags != NULL)
  419. *flags = 0;
  420. }
  421. }
  422. return ret;
  423. }
  424. void ERR_error_string_n(unsigned long e, char *buf, size_t len)
  425. {
  426. char lsbuf[64], rsbuf[256];
  427. const char *ls, *rs = NULL;
  428. unsigned long f = 0, l, r;
  429. if (len == 0)
  430. return;
  431. l = ERR_GET_LIB(e);
  432. ls = ERR_lib_error_string(e);
  433. if (ls == NULL) {
  434. BIO_snprintf(lsbuf, sizeof(lsbuf), "lib(%lu)", l);
  435. ls = lsbuf;
  436. }
  437. /*
  438. * ERR_reason_error_string() can't safely return system error strings,
  439. * since it would call openssl_strerror_r(), which needs a buffer for
  440. * thread safety. So for system errors, we call openssl_strerror_r()
  441. * directly instead.
  442. */
  443. r = ERR_GET_REASON(e);
  444. if (ERR_SYSTEM_ERROR(e)) {
  445. if (openssl_strerror_r(r, rsbuf, sizeof(rsbuf)))
  446. rs = rsbuf;
  447. } else {
  448. rs = ERR_reason_error_string(e);
  449. }
  450. if (rs == NULL) {
  451. BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", r);
  452. rs = rsbuf;
  453. }
  454. BIO_snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls, "", rs);
  455. if (strlen(buf) == len - 1) {
  456. /* Didn't fit; use a minimal format. */
  457. BIO_snprintf(buf, len, "err:%lx:%lx:%lx:%lx", e, l, f, r);
  458. }
  459. }
  460. /*
  461. * ERR_error_string_n should be used instead for ret != NULL as
  462. * ERR_error_string cannot know how large the buffer is
  463. */
  464. char *ERR_error_string(unsigned long e, char *ret)
  465. {
  466. static char buf[256];
  467. if (ret == NULL)
  468. ret = buf;
  469. ERR_error_string_n(e, ret, (int)sizeof(buf));
  470. return ret;
  471. }
  472. const char *ERR_lib_error_string(unsigned long e)
  473. {
  474. ERR_STRING_DATA d, *p;
  475. unsigned long l;
  476. if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
  477. return NULL;
  478. }
  479. l = ERR_GET_LIB(e);
  480. d.error = ERR_PACK(l, 0, 0);
  481. p = int_err_get_item(&d);
  482. return ((p == NULL) ? NULL : p->string);
  483. }
  484. #ifndef OPENSSL_NO_DEPRECATED_3_0
  485. const char *ERR_func_error_string(unsigned long e)
  486. {
  487. return NULL;
  488. }
  489. #endif
  490. const char *ERR_reason_error_string(unsigned long e)
  491. {
  492. ERR_STRING_DATA d, *p = NULL;
  493. unsigned long l, r;
  494. if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
  495. return NULL;
  496. }
  497. /*
  498. * ERR_reason_error_string() can't safely return system error strings,
  499. * since openssl_strerror_r() needs a buffer for thread safety, and we
  500. * haven't got one that would serve any sensible purpose.
  501. */
  502. if (ERR_SYSTEM_ERROR(e))
  503. return NULL;
  504. l = ERR_GET_LIB(e);
  505. r = ERR_GET_REASON(e);
  506. d.error = ERR_PACK(l, 0, r);
  507. p = int_err_get_item(&d);
  508. if (p == NULL) {
  509. d.error = ERR_PACK(0, 0, r);
  510. p = int_err_get_item(&d);
  511. }
  512. return ((p == NULL) ? NULL : p->string);
  513. }
  514. /* TODO(3.0): arg ignored for now */
  515. static void err_delete_thread_state(void *arg)
  516. {
  517. ERR_STATE *state = CRYPTO_THREAD_get_local(&err_thread_local);
  518. if (state == NULL)
  519. return;
  520. CRYPTO_THREAD_set_local(&err_thread_local, NULL);
  521. ERR_STATE_free(state);
  522. }
  523. #ifndef OPENSSL_NO_DEPRECATED_1_1_0
  524. void ERR_remove_thread_state(void *dummy)
  525. {
  526. }
  527. #endif
  528. #ifndef OPENSSL_NO_DEPRECATED_1_0_0
  529. void ERR_remove_state(unsigned long pid)
  530. {
  531. }
  532. #endif
  533. DEFINE_RUN_ONCE_STATIC(err_do_init)
  534. {
  535. set_err_thread_local = 1;
  536. return CRYPTO_THREAD_init_local(&err_thread_local, NULL);
  537. }
  538. ERR_STATE *err_get_state_int(void)
  539. {
  540. ERR_STATE *state;
  541. int saveerrno = get_last_sys_error();
  542. if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
  543. return NULL;
  544. if (!RUN_ONCE(&err_init, err_do_init))
  545. return NULL;
  546. state = CRYPTO_THREAD_get_local(&err_thread_local);
  547. if (state == (ERR_STATE*)-1)
  548. return NULL;
  549. if (state == NULL) {
  550. if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1))
  551. return NULL;
  552. if ((state = OPENSSL_zalloc(sizeof(*state))) == NULL) {
  553. CRYPTO_THREAD_set_local(&err_thread_local, NULL);
  554. return NULL;
  555. }
  556. if (!ossl_init_thread_start(NULL, NULL, err_delete_thread_state)
  557. || !CRYPTO_THREAD_set_local(&err_thread_local, state)) {
  558. ERR_STATE_free(state);
  559. CRYPTO_THREAD_set_local(&err_thread_local, NULL);
  560. return NULL;
  561. }
  562. /* Ignore failures from these */
  563. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
  564. }
  565. set_sys_error(saveerrno);
  566. return state;
  567. }
  568. #ifndef OPENSSL_NO_DEPRECATED_3_0
  569. ERR_STATE *ERR_get_state(void)
  570. {
  571. return err_get_state_int();
  572. }
  573. #endif
  574. /*
  575. * err_shelve_state returns the current thread local error state
  576. * and freezes the error module until err_unshelve_state is called.
  577. */
  578. int err_shelve_state(void **state)
  579. {
  580. int saveerrno = get_last_sys_error();
  581. /*
  582. * Note, at present our only caller is OPENSSL_init_crypto(), indirectly
  583. * via ossl_init_load_crypto_nodelete(), by which point the requested
  584. * "base" initialization has already been performed, so the below call is a
  585. * NOOP, that re-enters OPENSSL_init_crypto() only to quickly return.
  586. *
  587. * If are no other valid callers of this function, the call below can be
  588. * removed, avoiding the re-entry into OPENSSL_init_crypto(). If there are
  589. * potential uses that are not from inside OPENSSL_init_crypto(), then this
  590. * call is needed, but some care is required to make sure that the re-entry
  591. * remains a NOOP.
  592. */
  593. if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
  594. return 0;
  595. if (!RUN_ONCE(&err_init, err_do_init))
  596. return 0;
  597. *state = CRYPTO_THREAD_get_local(&err_thread_local);
  598. if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1))
  599. return 0;
  600. set_sys_error(saveerrno);
  601. return 1;
  602. }
  603. /*
  604. * err_unshelve_state restores the error state that was returned
  605. * by err_shelve_state previously.
  606. */
  607. void err_unshelve_state(void* state)
  608. {
  609. if (state != (void*)-1)
  610. CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)state);
  611. }
  612. int ERR_get_next_error_library(void)
  613. {
  614. int ret;
  615. if (!RUN_ONCE(&err_string_init, do_err_strings_init))
  616. return 0;
  617. CRYPTO_THREAD_write_lock(err_string_lock);
  618. ret = int_err_library_number++;
  619. CRYPTO_THREAD_unlock(err_string_lock);
  620. return ret;
  621. }
  622. static int err_set_error_data_int(char *data, size_t size, int flags,
  623. int deallocate)
  624. {
  625. ERR_STATE *es;
  626. es = err_get_state_int();
  627. if (es == NULL)
  628. return 0;
  629. err_clear_data(es, es->top, deallocate);
  630. err_set_data(es, es->top, data, size, flags);
  631. return 1;
  632. }
  633. void ERR_set_error_data(char *data, int flags)
  634. {
  635. /*
  636. * This function is void so we cannot propagate the error return. Since it
  637. * is also in the public API we can't change the return type.
  638. *
  639. * We estimate the size of the data. If it's not flagged as allocated,
  640. * then this is safe, and if it is flagged as allocated, then our size
  641. * may be smaller than the actual allocation, but that doesn't matter
  642. * too much, the buffer will remain untouched or will eventually be
  643. * reallocated to a new size.
  644. *
  645. * callers should be advised that this function takes over ownership of
  646. * the allocated memory, i.e. they can't count on the pointer to remain
  647. * valid.
  648. */
  649. err_set_error_data_int(data, strlen(data) + 1, flags, 1);
  650. }
  651. void ERR_add_error_data(int num, ...)
  652. {
  653. va_list args;
  654. va_start(args, num);
  655. ERR_add_error_vdata(num, args);
  656. va_end(args);
  657. }
  658. void ERR_add_error_vdata(int num, va_list args)
  659. {
  660. int i, len, size;
  661. int flags = ERR_TXT_MALLOCED | ERR_TXT_STRING;
  662. char *str, *arg;
  663. ERR_STATE *es;
  664. /* Get the current error data; if an allocated string get it. */
  665. es = err_get_state_int();
  666. if (es == NULL)
  667. return;
  668. i = es->top;
  669. /*
  670. * If err_data is allocated already, re-use the space.
  671. * Otherwise, allocate a small new buffer.
  672. */
  673. if ((es->err_data_flags[i] & flags) == flags) {
  674. str = es->err_data[i];
  675. size = es->err_data_size[i];
  676. /*
  677. * To protect the string we just grabbed from tampering by other
  678. * functions we may call, or to protect them from freeing a pointer
  679. * that may no longer be valid at that point, we clear away the
  680. * data pointer and the flags. We will set them again at the end
  681. * of this function.
  682. */
  683. es->err_data[i] = NULL;
  684. es->err_data_flags[i] = 0;
  685. } else if ((str = OPENSSL_malloc(size = 81)) == NULL) {
  686. return;
  687. } else {
  688. str[0] = '\0';
  689. }
  690. len = strlen(str);
  691. while (--num >= 0) {
  692. arg = va_arg(args, char *);
  693. if (arg == NULL)
  694. arg = "<NULL>";
  695. len += strlen(arg);
  696. if (len >= size) {
  697. char *p;
  698. size = len + 20;
  699. p = OPENSSL_realloc(str, size);
  700. if (p == NULL) {
  701. OPENSSL_free(str);
  702. return;
  703. }
  704. str = p;
  705. }
  706. OPENSSL_strlcat(str, arg, (size_t)size);
  707. }
  708. if (!err_set_error_data_int(str, size, flags, 0))
  709. OPENSSL_free(str);
  710. }
  711. int ERR_set_mark(void)
  712. {
  713. ERR_STATE *es;
  714. es = err_get_state_int();
  715. if (es == NULL)
  716. return 0;
  717. if (es->bottom == es->top)
  718. return 0;
  719. es->err_flags[es->top] |= ERR_FLAG_MARK;
  720. return 1;
  721. }
  722. int ERR_pop_to_mark(void)
  723. {
  724. ERR_STATE *es;
  725. es = err_get_state_int();
  726. if (es == NULL)
  727. return 0;
  728. while (es->bottom != es->top
  729. && (es->err_flags[es->top] & ERR_FLAG_MARK) == 0) {
  730. err_clear(es, es->top, 0);
  731. es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1;
  732. }
  733. if (es->bottom == es->top)
  734. return 0;
  735. es->err_flags[es->top] &= ~ERR_FLAG_MARK;
  736. return 1;
  737. }
  738. int ERR_clear_last_mark(void)
  739. {
  740. ERR_STATE *es;
  741. int top;
  742. es = err_get_state_int();
  743. if (es == NULL)
  744. return 0;
  745. top = es->top;
  746. while (es->bottom != top
  747. && (es->err_flags[top] & ERR_FLAG_MARK) == 0) {
  748. top = top > 0 ? top - 1 : ERR_NUM_ERRORS - 1;
  749. }
  750. if (es->bottom == top)
  751. return 0;
  752. es->err_flags[top] &= ~ERR_FLAG_MARK;
  753. return 1;
  754. }
  755. void err_clear_last_constant_time(int clear)
  756. {
  757. ERR_STATE *es;
  758. int top;
  759. es = err_get_state_int();
  760. if (es == NULL)
  761. return;
  762. top = es->top;
  763. /*
  764. * Flag error as cleared but remove it elsewhere to avoid two errors
  765. * accessing the same error stack location, revealing timing information.
  766. */
  767. clear = constant_time_select_int(constant_time_eq_int(clear, 0),
  768. 0, ERR_FLAG_CLEAR);
  769. es->err_flags[top] |= clear;
  770. }