err.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. /*
  2. * Copyright 1995-2022 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. static void ERR_STATE_free(ERR_STATE *s);
  32. #ifndef OPENSSL_NO_ERR
  33. static ERR_STRING_DATA ERR_str_libraries[] = {
  34. {ERR_PACK(ERR_LIB_NONE, 0, 0), "unknown library"},
  35. {ERR_PACK(ERR_LIB_SYS, 0, 0), "system library"},
  36. {ERR_PACK(ERR_LIB_BN, 0, 0), "bignum routines"},
  37. {ERR_PACK(ERR_LIB_RSA, 0, 0), "rsa routines"},
  38. {ERR_PACK(ERR_LIB_DH, 0, 0), "Diffie-Hellman routines"},
  39. {ERR_PACK(ERR_LIB_EVP, 0, 0), "digital envelope routines"},
  40. {ERR_PACK(ERR_LIB_BUF, 0, 0), "memory buffer routines"},
  41. {ERR_PACK(ERR_LIB_OBJ, 0, 0), "object identifier routines"},
  42. {ERR_PACK(ERR_LIB_PEM, 0, 0), "PEM routines"},
  43. {ERR_PACK(ERR_LIB_DSA, 0, 0), "dsa routines"},
  44. {ERR_PACK(ERR_LIB_X509, 0, 0), "x509 certificate routines"},
  45. {ERR_PACK(ERR_LIB_ASN1, 0, 0), "asn1 encoding routines"},
  46. {ERR_PACK(ERR_LIB_CONF, 0, 0), "configuration file routines"},
  47. {ERR_PACK(ERR_LIB_CRYPTO, 0, 0), "common libcrypto routines"},
  48. {ERR_PACK(ERR_LIB_EC, 0, 0), "elliptic curve routines"},
  49. {ERR_PACK(ERR_LIB_ECDSA, 0, 0), "ECDSA routines"},
  50. {ERR_PACK(ERR_LIB_ECDH, 0, 0), "ECDH routines"},
  51. {ERR_PACK(ERR_LIB_SSL, 0, 0), "SSL routines"},
  52. {ERR_PACK(ERR_LIB_BIO, 0, 0), "BIO routines"},
  53. {ERR_PACK(ERR_LIB_PKCS7, 0, 0), "PKCS7 routines"},
  54. {ERR_PACK(ERR_LIB_X509V3, 0, 0), "X509 V3 routines"},
  55. {ERR_PACK(ERR_LIB_PKCS12, 0, 0), "PKCS12 routines"},
  56. {ERR_PACK(ERR_LIB_RAND, 0, 0), "random number generator"},
  57. {ERR_PACK(ERR_LIB_DSO, 0, 0), "DSO support routines"},
  58. {ERR_PACK(ERR_LIB_TS, 0, 0), "time stamp routines"},
  59. {ERR_PACK(ERR_LIB_ENGINE, 0, 0), "engine routines"},
  60. {ERR_PACK(ERR_LIB_OCSP, 0, 0), "OCSP routines"},
  61. {ERR_PACK(ERR_LIB_UI, 0, 0), "UI routines"},
  62. {ERR_PACK(ERR_LIB_FIPS, 0, 0), "FIPS routines"},
  63. {ERR_PACK(ERR_LIB_CMS, 0, 0), "CMS routines"},
  64. {ERR_PACK(ERR_LIB_CRMF, 0, 0), "CRMF routines"},
  65. {ERR_PACK(ERR_LIB_CMP, 0, 0), "CMP routines"},
  66. {ERR_PACK(ERR_LIB_HMAC, 0, 0), "HMAC routines"},
  67. {ERR_PACK(ERR_LIB_CT, 0, 0), "CT routines"},
  68. {ERR_PACK(ERR_LIB_ASYNC, 0, 0), "ASYNC routines"},
  69. {ERR_PACK(ERR_LIB_KDF, 0, 0), "KDF routines"},
  70. {ERR_PACK(ERR_LIB_OSSL_STORE, 0, 0), "STORE routines"},
  71. {ERR_PACK(ERR_LIB_SM2, 0, 0), "SM2 routines"},
  72. {ERR_PACK(ERR_LIB_ESS, 0, 0), "ESS routines"},
  73. {ERR_PACK(ERR_LIB_PROV, 0, 0), "Provider routines"},
  74. {ERR_PACK(ERR_LIB_OSSL_ENCODER, 0, 0), "ENCODER routines"},
  75. {ERR_PACK(ERR_LIB_OSSL_DECODER, 0, 0), "DECODER routines"},
  76. {ERR_PACK(ERR_LIB_HTTP, 0, 0), "HTTP routines"},
  77. {0, NULL},
  78. };
  79. /*
  80. * Should make sure that all ERR_R_ reasons defined in include/openssl/err.h.in
  81. * are listed. For maintainability, please keep all reasons in the same order.
  82. */
  83. static ERR_STRING_DATA ERR_str_reasons[] = {
  84. {ERR_R_SYS_LIB, "system lib"},
  85. {ERR_R_BN_LIB, "BN lib"},
  86. {ERR_R_RSA_LIB, "RSA lib"},
  87. {ERR_R_DH_LIB, "DH lib"},
  88. {ERR_R_EVP_LIB, "EVP lib"},
  89. {ERR_R_BUF_LIB, "BUF lib"},
  90. {ERR_R_OBJ_LIB, "OBJ lib"},
  91. {ERR_R_PEM_LIB, "PEM lib"},
  92. {ERR_R_DSA_LIB, "DSA lib"},
  93. {ERR_R_X509_LIB, "X509 lib"},
  94. {ERR_R_ASN1_LIB, "ASN1 lib"},
  95. {ERR_R_CRYPTO_LIB, "CRYPTO lib"},
  96. {ERR_R_EC_LIB, "EC lib"},
  97. {ERR_R_BIO_LIB, "BIO lib"},
  98. {ERR_R_PKCS7_LIB, "PKCS7 lib"},
  99. {ERR_R_X509V3_LIB, "X509V3 lib"},
  100. {ERR_R_ENGINE_LIB, "ENGINE lib"},
  101. {ERR_R_UI_LIB, "UI lib"},
  102. {ERR_R_ECDSA_LIB, "ECDSA lib"},
  103. {ERR_R_OSSL_STORE_LIB, "OSSL_STORE lib"},
  104. {ERR_R_OSSL_DECODER_LIB, "OSSL_DECODER lib"},
  105. {ERR_R_FATAL, "fatal"},
  106. {ERR_R_MALLOC_FAILURE, "malloc failure"},
  107. {ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
  108. "called a function you should not call"},
  109. {ERR_R_PASSED_NULL_PARAMETER, "passed a null parameter"},
  110. {ERR_R_INTERNAL_ERROR, "internal error"},
  111. {ERR_R_DISABLED, "called a function that was disabled at compile-time"},
  112. {ERR_R_INIT_FAIL, "init fail"},
  113. {ERR_R_PASSED_INVALID_ARGUMENT, "passed invalid argument"},
  114. {ERR_R_OPERATION_FAIL, "operation fail"},
  115. {ERR_R_INVALID_PROVIDER_FUNCTIONS, "invalid provider functions"},
  116. {ERR_R_INTERRUPTED_OR_CANCELLED, "interrupted or cancelled"},
  117. {ERR_R_NESTED_ASN1_ERROR, "nested asn1 error"},
  118. {ERR_R_MISSING_ASN1_EOS, "missing asn1 eos"},
  119. /*
  120. * Something is unsupported, exactly what is expressed with additional data
  121. */
  122. {ERR_R_UNSUPPORTED, "unsupported"},
  123. /*
  124. * A fetch failed for other reasons than the name to be fetched being
  125. * unsupported.
  126. */
  127. {ERR_R_FETCH_FAILED, "fetch failed"},
  128. {ERR_R_INVALID_PROPERTY_DEFINITION, "invalid property definition"},
  129. {ERR_R_UNABLE_TO_GET_READ_LOCK, "unable to get read lock"},
  130. {ERR_R_UNABLE_TO_GET_WRITE_LOCK, "unable to get write lock"},
  131. {0, NULL},
  132. };
  133. #endif
  134. static CRYPTO_ONCE err_init = CRYPTO_ONCE_STATIC_INIT;
  135. static int set_err_thread_local;
  136. static CRYPTO_THREAD_LOCAL err_thread_local;
  137. static CRYPTO_ONCE err_string_init = CRYPTO_ONCE_STATIC_INIT;
  138. static CRYPTO_RWLOCK *err_string_lock = NULL;
  139. #ifndef OPENSSL_NO_ERR
  140. static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *);
  141. #endif
  142. /*
  143. * The internal state
  144. */
  145. #ifndef OPENSSL_NO_ERR
  146. static LHASH_OF(ERR_STRING_DATA) *int_error_hash = NULL;
  147. #endif
  148. static int int_err_library_number = ERR_LIB_USER;
  149. typedef enum ERR_GET_ACTION_e {
  150. EV_POP, EV_PEEK, EV_PEEK_LAST
  151. } ERR_GET_ACTION;
  152. static unsigned long get_error_values(ERR_GET_ACTION g,
  153. const char **file, int *line,
  154. const char **func, const char **data,
  155. int *flags);
  156. #ifndef OPENSSL_NO_ERR
  157. static unsigned long err_string_data_hash(const ERR_STRING_DATA *a)
  158. {
  159. unsigned long ret, l;
  160. l = a->error;
  161. ret = l ^ ERR_GET_LIB(l);
  162. return (ret ^ ret % 19 * 13);
  163. }
  164. static int err_string_data_cmp(const ERR_STRING_DATA *a,
  165. const ERR_STRING_DATA *b)
  166. {
  167. if (a->error == b->error)
  168. return 0;
  169. return a->error > b->error ? 1 : -1;
  170. }
  171. static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *d)
  172. {
  173. ERR_STRING_DATA *p = NULL;
  174. if (!CRYPTO_THREAD_read_lock(err_string_lock))
  175. return NULL;
  176. p = lh_ERR_STRING_DATA_retrieve(int_error_hash, d);
  177. CRYPTO_THREAD_unlock(err_string_lock);
  178. return p;
  179. }
  180. #endif
  181. static void ERR_STATE_free(ERR_STATE *s)
  182. {
  183. int i;
  184. if (s == NULL)
  185. return;
  186. for (i = 0; i < ERR_NUM_ERRORS; i++) {
  187. err_clear(s, i, 1);
  188. }
  189. OPENSSL_free(s);
  190. }
  191. DEFINE_RUN_ONCE_STATIC(do_err_strings_init)
  192. {
  193. if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
  194. return 0;
  195. err_string_lock = CRYPTO_THREAD_lock_new();
  196. if (err_string_lock == NULL)
  197. return 0;
  198. #ifndef OPENSSL_NO_ERR
  199. int_error_hash = lh_ERR_STRING_DATA_new(err_string_data_hash,
  200. err_string_data_cmp);
  201. if (int_error_hash == NULL) {
  202. CRYPTO_THREAD_lock_free(err_string_lock);
  203. err_string_lock = NULL;
  204. return 0;
  205. }
  206. #endif
  207. return 1;
  208. }
  209. void err_cleanup(void)
  210. {
  211. if (set_err_thread_local != 0)
  212. CRYPTO_THREAD_cleanup_local(&err_thread_local);
  213. CRYPTO_THREAD_lock_free(err_string_lock);
  214. err_string_lock = NULL;
  215. #ifndef OPENSSL_NO_ERR
  216. lh_ERR_STRING_DATA_free(int_error_hash);
  217. int_error_hash = NULL;
  218. #endif
  219. }
  220. #ifndef OPENSSL_NO_ERR
  221. /*
  222. * Legacy; pack in the library.
  223. */
  224. static void err_patch(int lib, ERR_STRING_DATA *str)
  225. {
  226. unsigned long plib = ERR_PACK(lib, 0, 0);
  227. for (; str->error != 0; str++)
  228. str->error |= plib;
  229. }
  230. /*
  231. * Hash in |str| error strings. Assumes the RUN_ONCE was done.
  232. */
  233. static int err_load_strings(const ERR_STRING_DATA *str)
  234. {
  235. if (!CRYPTO_THREAD_write_lock(err_string_lock))
  236. return 0;
  237. for (; str->error; str++)
  238. (void)lh_ERR_STRING_DATA_insert(int_error_hash,
  239. (ERR_STRING_DATA *)str);
  240. CRYPTO_THREAD_unlock(err_string_lock);
  241. return 1;
  242. }
  243. #endif
  244. int ossl_err_load_ERR_strings(void)
  245. {
  246. #ifndef OPENSSL_NO_ERR
  247. if (!RUN_ONCE(&err_string_init, do_err_strings_init))
  248. return 0;
  249. err_load_strings(ERR_str_libraries);
  250. err_load_strings(ERR_str_reasons);
  251. #endif
  252. return 1;
  253. }
  254. int ERR_load_strings(int lib, ERR_STRING_DATA *str)
  255. {
  256. #ifndef OPENSSL_NO_ERR
  257. if (ossl_err_load_ERR_strings() == 0)
  258. return 0;
  259. err_patch(lib, str);
  260. err_load_strings(str);
  261. #endif
  262. return 1;
  263. }
  264. int ERR_load_strings_const(const ERR_STRING_DATA *str)
  265. {
  266. #ifndef OPENSSL_NO_ERR
  267. if (ossl_err_load_ERR_strings() == 0)
  268. return 0;
  269. err_load_strings(str);
  270. #endif
  271. return 1;
  272. }
  273. int ERR_unload_strings(int lib, ERR_STRING_DATA *str)
  274. {
  275. #ifndef OPENSSL_NO_ERR
  276. if (!RUN_ONCE(&err_string_init, do_err_strings_init))
  277. return 0;
  278. if (!CRYPTO_THREAD_write_lock(err_string_lock))
  279. return 0;
  280. /*
  281. * We don't need to ERR_PACK the lib, since that was done (to
  282. * the table) when it was loaded.
  283. */
  284. for (; str->error; str++)
  285. (void)lh_ERR_STRING_DATA_delete(int_error_hash, str);
  286. CRYPTO_THREAD_unlock(err_string_lock);
  287. #endif
  288. return 1;
  289. }
  290. void err_free_strings_int(void)
  291. {
  292. /* obsolete */
  293. }
  294. /********************************************************/
  295. void ERR_clear_error(void)
  296. {
  297. int i;
  298. ERR_STATE *es;
  299. es = ossl_err_get_state_int();
  300. if (es == NULL)
  301. return;
  302. for (i = 0; i < ERR_NUM_ERRORS; i++) {
  303. err_clear(es, i, 0);
  304. }
  305. es->top = es->bottom = 0;
  306. }
  307. unsigned long ERR_get_error(void)
  308. {
  309. return get_error_values(EV_POP, NULL, NULL, NULL, NULL, NULL);
  310. }
  311. unsigned long ERR_get_error_all(const char **file, int *line,
  312. const char **func,
  313. const char **data, int *flags)
  314. {
  315. return get_error_values(EV_POP, file, line, func, data, flags);
  316. }
  317. #ifndef OPENSSL_NO_DEPRECATED_3_0
  318. unsigned long ERR_get_error_line(const char **file, int *line)
  319. {
  320. return get_error_values(EV_POP, file, line, NULL, NULL, NULL);
  321. }
  322. unsigned long ERR_get_error_line_data(const char **file, int *line,
  323. const char **data, int *flags)
  324. {
  325. return get_error_values(EV_POP, file, line, NULL, data, flags);
  326. }
  327. #endif
  328. unsigned long ERR_peek_error(void)
  329. {
  330. return get_error_values(EV_PEEK, NULL, NULL, NULL, NULL, NULL);
  331. }
  332. unsigned long ERR_peek_error_line(const char **file, int *line)
  333. {
  334. return get_error_values(EV_PEEK, file, line, NULL, NULL, NULL);
  335. }
  336. unsigned long ERR_peek_error_func(const char **func)
  337. {
  338. return get_error_values(EV_PEEK, NULL, NULL, func, NULL, NULL);
  339. }
  340. unsigned long ERR_peek_error_data(const char **data, int *flags)
  341. {
  342. return get_error_values(EV_PEEK, NULL, NULL, NULL, data, flags);
  343. }
  344. unsigned long ERR_peek_error_all(const char **file, int *line,
  345. const char **func,
  346. const char **data, int *flags)
  347. {
  348. return get_error_values(EV_PEEK, file, line, func, data, flags);
  349. }
  350. #ifndef OPENSSL_NO_DEPRECATED_3_0
  351. unsigned long ERR_peek_error_line_data(const char **file, int *line,
  352. const char **data, int *flags)
  353. {
  354. return get_error_values(EV_PEEK, file, line, NULL, data, flags);
  355. }
  356. #endif
  357. unsigned long ERR_peek_last_error(void)
  358. {
  359. return get_error_values(EV_PEEK_LAST, NULL, NULL, NULL, NULL, NULL);
  360. }
  361. unsigned long ERR_peek_last_error_line(const char **file, int *line)
  362. {
  363. return get_error_values(EV_PEEK_LAST, file, line, NULL, NULL, NULL);
  364. }
  365. unsigned long ERR_peek_last_error_func(const char **func)
  366. {
  367. return get_error_values(EV_PEEK_LAST, NULL, NULL, func, NULL, NULL);
  368. }
  369. unsigned long ERR_peek_last_error_data(const char **data, int *flags)
  370. {
  371. return get_error_values(EV_PEEK_LAST, NULL, NULL, NULL, data, flags);
  372. }
  373. unsigned long ERR_peek_last_error_all(const char **file, int *line,
  374. const char **func,
  375. const char **data, int *flags)
  376. {
  377. return get_error_values(EV_PEEK_LAST, file, line, func, data, flags);
  378. }
  379. #ifndef OPENSSL_NO_DEPRECATED_3_0
  380. unsigned long ERR_peek_last_error_line_data(const char **file, int *line,
  381. const char **data, int *flags)
  382. {
  383. return get_error_values(EV_PEEK_LAST, file, line, NULL, data, flags);
  384. }
  385. #endif
  386. static unsigned long get_error_values(ERR_GET_ACTION g,
  387. const char **file, int *line,
  388. const char **func,
  389. const char **data, int *flags)
  390. {
  391. int i = 0;
  392. ERR_STATE *es;
  393. unsigned long ret;
  394. es = ossl_err_get_state_int();
  395. if (es == NULL)
  396. return 0;
  397. /*
  398. * Clear anything that should have been cleared earlier. We do this
  399. * here because this doesn't have constant-time issues.
  400. */
  401. while (es->bottom != es->top) {
  402. if (es->err_flags[es->top] & ERR_FLAG_CLEAR) {
  403. err_clear(es, es->top, 0);
  404. es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1;
  405. continue;
  406. }
  407. i = (es->bottom + 1) % ERR_NUM_ERRORS;
  408. if (es->err_flags[i] & ERR_FLAG_CLEAR) {
  409. es->bottom = i;
  410. err_clear(es, es->bottom, 0);
  411. continue;
  412. }
  413. break;
  414. }
  415. /* If everything has been cleared, the stack is empty. */
  416. if (es->bottom == es->top)
  417. return 0;
  418. /* Which error, the top of stack (latest one) or the first one? */
  419. if (g == EV_PEEK_LAST)
  420. i = es->top;
  421. else
  422. i = (es->bottom + 1) % ERR_NUM_ERRORS;
  423. ret = es->err_buffer[i];
  424. if (g == EV_POP) {
  425. es->bottom = i;
  426. es->err_buffer[i] = 0;
  427. }
  428. if (file != NULL) {
  429. *file = es->err_file[i];
  430. if (*file == NULL)
  431. *file = "";
  432. }
  433. if (line != NULL)
  434. *line = es->err_line[i];
  435. if (func != NULL) {
  436. *func = es->err_func[i];
  437. if (*func == NULL)
  438. *func = "";
  439. }
  440. if (flags != NULL)
  441. *flags = es->err_data_flags[i];
  442. if (data == NULL) {
  443. if (g == EV_POP) {
  444. err_clear_data(es, i, 0);
  445. }
  446. } else {
  447. *data = es->err_data[i];
  448. if (*data == NULL) {
  449. *data = "";
  450. if (flags != NULL)
  451. *flags = 0;
  452. }
  453. }
  454. return ret;
  455. }
  456. void ossl_err_string_int(unsigned long e, const char *func,
  457. char *buf, size_t len)
  458. {
  459. char lsbuf[64], rsbuf[256];
  460. const char *ls, *rs = NULL;
  461. unsigned long l, r;
  462. if (len == 0)
  463. return;
  464. l = ERR_GET_LIB(e);
  465. ls = ERR_lib_error_string(e);
  466. if (ls == NULL) {
  467. BIO_snprintf(lsbuf, sizeof(lsbuf), "lib(%lu)", l);
  468. ls = lsbuf;
  469. }
  470. /*
  471. * ERR_reason_error_string() can't safely return system error strings,
  472. * since it would call openssl_strerror_r(), which needs a buffer for
  473. * thread safety. So for system errors, we call openssl_strerror_r()
  474. * directly instead.
  475. */
  476. r = ERR_GET_REASON(e);
  477. #ifndef OPENSSL_NO_ERR
  478. if (ERR_SYSTEM_ERROR(e)) {
  479. if (openssl_strerror_r(r, rsbuf, sizeof(rsbuf)))
  480. rs = rsbuf;
  481. } else {
  482. rs = ERR_reason_error_string(e);
  483. }
  484. #endif
  485. if (rs == NULL) {
  486. BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)",
  487. r & ~(ERR_RFLAGS_MASK << ERR_RFLAGS_OFFSET));
  488. rs = rsbuf;
  489. }
  490. BIO_snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls, func, rs);
  491. if (strlen(buf) == len - 1) {
  492. /* Didn't fit; use a minimal format. */
  493. BIO_snprintf(buf, len, "err:%lx:%lx:%lx:%lx", e, l, 0L, r);
  494. }
  495. }
  496. void ERR_error_string_n(unsigned long e, char *buf, size_t len)
  497. {
  498. ossl_err_string_int(e, "", buf, len);
  499. }
  500. /*
  501. * ERR_error_string_n should be used instead for ret != NULL as
  502. * ERR_error_string cannot know how large the buffer is
  503. */
  504. char *ERR_error_string(unsigned long e, char *ret)
  505. {
  506. static char buf[256];
  507. if (ret == NULL)
  508. ret = buf;
  509. ERR_error_string_n(e, ret, (int)sizeof(buf));
  510. return ret;
  511. }
  512. const char *ERR_lib_error_string(unsigned long e)
  513. {
  514. #ifndef OPENSSL_NO_ERR
  515. ERR_STRING_DATA d, *p;
  516. unsigned long l;
  517. if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
  518. return NULL;
  519. }
  520. l = ERR_GET_LIB(e);
  521. d.error = ERR_PACK(l, 0, 0);
  522. p = int_err_get_item(&d);
  523. return ((p == NULL) ? NULL : p->string);
  524. #else
  525. return NULL;
  526. #endif
  527. }
  528. #ifndef OPENSSL_NO_DEPRECATED_3_0
  529. const char *ERR_func_error_string(unsigned long e)
  530. {
  531. return NULL;
  532. }
  533. #endif
  534. const char *ERR_reason_error_string(unsigned long e)
  535. {
  536. #ifndef OPENSSL_NO_ERR
  537. ERR_STRING_DATA d, *p = NULL;
  538. unsigned long l, r;
  539. if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
  540. return NULL;
  541. }
  542. /*
  543. * ERR_reason_error_string() can't safely return system error strings,
  544. * since openssl_strerror_r() needs a buffer for thread safety, and we
  545. * haven't got one that would serve any sensible purpose.
  546. */
  547. if (ERR_SYSTEM_ERROR(e))
  548. return NULL;
  549. l = ERR_GET_LIB(e);
  550. r = ERR_GET_REASON(e);
  551. d.error = ERR_PACK(l, 0, r);
  552. p = int_err_get_item(&d);
  553. if (p == NULL) {
  554. d.error = ERR_PACK(0, 0, r);
  555. p = int_err_get_item(&d);
  556. }
  557. return ((p == NULL) ? NULL : p->string);
  558. #else
  559. return NULL;
  560. #endif
  561. }
  562. static void err_delete_thread_state(void *unused)
  563. {
  564. ERR_STATE *state = CRYPTO_THREAD_get_local(&err_thread_local);
  565. if (state == NULL)
  566. return;
  567. CRYPTO_THREAD_set_local(&err_thread_local, NULL);
  568. ERR_STATE_free(state);
  569. }
  570. #ifndef OPENSSL_NO_DEPRECATED_1_1_0
  571. void ERR_remove_thread_state(void *dummy)
  572. {
  573. }
  574. #endif
  575. #ifndef OPENSSL_NO_DEPRECATED_1_0_0
  576. void ERR_remove_state(unsigned long pid)
  577. {
  578. }
  579. #endif
  580. DEFINE_RUN_ONCE_STATIC(err_do_init)
  581. {
  582. set_err_thread_local = 1;
  583. return CRYPTO_THREAD_init_local(&err_thread_local, NULL);
  584. }
  585. ERR_STATE *ossl_err_get_state_int(void)
  586. {
  587. ERR_STATE *state;
  588. int saveerrno = get_last_sys_error();
  589. if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
  590. return NULL;
  591. if (!RUN_ONCE(&err_init, err_do_init))
  592. return NULL;
  593. state = CRYPTO_THREAD_get_local(&err_thread_local);
  594. if (state == (ERR_STATE*)-1)
  595. return NULL;
  596. if (state == NULL) {
  597. if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1))
  598. return NULL;
  599. if ((state = OPENSSL_zalloc(sizeof(*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. 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, re-use the space.
  719. * Otherwise, allocate a small new buffer.
  720. */
  721. if ((es->err_data_flags[i] & flags) == flags) {
  722. str = es->err_data[i];
  723. size = es->err_data_size[i];
  724. /*
  725. * To protect the string we just grabbed from tampering by other
  726. * functions we may call, or to protect them from freeing a pointer
  727. * that may no longer be valid at that point, we clear away the
  728. * data pointer and the flags. We will set them again at the end
  729. * of this function.
  730. */
  731. es->err_data[i] = NULL;
  732. es->err_data_flags[i] = 0;
  733. } else if ((str = OPENSSL_malloc(size = 81)) == NULL) {
  734. return;
  735. } else {
  736. str[0] = '\0';
  737. }
  738. len = strlen(str);
  739. while (--num >= 0) {
  740. arg = va_arg(args, char *);
  741. if (arg == NULL)
  742. arg = "<NULL>";
  743. len += strlen(arg);
  744. if (len >= size) {
  745. char *p;
  746. size = len + 20;
  747. p = OPENSSL_realloc(str, size);
  748. if (p == NULL) {
  749. OPENSSL_free(str);
  750. return;
  751. }
  752. str = p;
  753. }
  754. OPENSSL_strlcat(str, arg, (size_t)size);
  755. }
  756. if (!err_set_error_data_int(str, size, flags, 0))
  757. OPENSSL_free(str);
  758. }
  759. void err_clear_last_constant_time(int clear)
  760. {
  761. ERR_STATE *es;
  762. int top;
  763. es = ossl_err_get_state_int();
  764. if (es == NULL)
  765. return;
  766. top = es->top;
  767. /*
  768. * Flag error as cleared but remove it elsewhere to avoid two errors
  769. * accessing the same error stack location, revealing timing information.
  770. */
  771. clear = constant_time_select_int(constant_time_eq_int(clear, 0),
  772. 0, ERR_FLAG_CLEAR);
  773. es->err_flags[top] |= clear;
  774. }