err.c 24 KB

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