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