err.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. /*
  2. * Copyright 1995-2018 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_SERIALIZER, 0, 0), "SERIALIZER routines"},
  74. {ERR_PACK(ERR_LIB_HTTP, 0, 0), "HTTP routines"},
  75. {0, NULL},
  76. };
  77. static ERR_STRING_DATA ERR_str_reasons[] = {
  78. {ERR_R_SYS_LIB, "system lib"},
  79. {ERR_R_BN_LIB, "BN lib"},
  80. {ERR_R_RSA_LIB, "RSA lib"},
  81. {ERR_R_DH_LIB, "DH lib"},
  82. {ERR_R_EVP_LIB, "EVP lib"},
  83. {ERR_R_BUF_LIB, "BUF lib"},
  84. {ERR_R_OBJ_LIB, "OBJ lib"},
  85. {ERR_R_PEM_LIB, "PEM lib"},
  86. {ERR_R_DSA_LIB, "DSA lib"},
  87. {ERR_R_X509_LIB, "X509 lib"},
  88. {ERR_R_ASN1_LIB, "ASN1 lib"},
  89. {ERR_R_EC_LIB, "EC lib"},
  90. {ERR_R_BIO_LIB, "BIO lib"},
  91. {ERR_R_PKCS7_LIB, "PKCS7 lib"},
  92. {ERR_R_X509V3_LIB, "X509V3 lib"},
  93. {ERR_R_ENGINE_LIB, "ENGINE lib"},
  94. {ERR_R_UI_LIB, "UI lib"},
  95. {ERR_R_OSSL_STORE_LIB, "STORE lib"},
  96. {ERR_R_ECDSA_LIB, "ECDSA lib"},
  97. {ERR_R_NESTED_ASN1_ERROR, "nested asn1 error"},
  98. {ERR_R_MISSING_ASN1_EOS, "missing asn1 eos"},
  99. {ERR_R_FATAL, "fatal"},
  100. {ERR_R_MALLOC_FAILURE, "malloc failure"},
  101. {ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
  102. "called a function you should not call"},
  103. {ERR_R_PASSED_NULL_PARAMETER, "passed a null parameter"},
  104. {ERR_R_INTERNAL_ERROR, "internal error"},
  105. {ERR_R_DISABLED, "called a function that was disabled at compile-time"},
  106. {ERR_R_INIT_FAIL, "init fail"},
  107. {ERR_R_OPERATION_FAIL, "operation fail"},
  108. {ERR_R_INVALID_PROVIDER_FUNCTIONS, "invalid provider functions"},
  109. {ERR_R_INTERRUPTED_OR_CANCELLED, "interrupted or cancelled"},
  110. {0, NULL},
  111. };
  112. #endif
  113. static CRYPTO_ONCE err_init = CRYPTO_ONCE_STATIC_INIT;
  114. static int set_err_thread_local;
  115. static CRYPTO_THREAD_LOCAL err_thread_local;
  116. static CRYPTO_ONCE err_string_init = CRYPTO_ONCE_STATIC_INIT;
  117. static CRYPTO_RWLOCK *err_string_lock;
  118. static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *);
  119. /*
  120. * The internal state
  121. */
  122. static LHASH_OF(ERR_STRING_DATA) *int_error_hash = NULL;
  123. static int int_err_library_number = ERR_LIB_USER;
  124. typedef enum ERR_GET_ACTION_e {
  125. EV_POP, EV_PEEK, EV_PEEK_LAST
  126. } ERR_GET_ACTION;
  127. static unsigned long get_error_values(ERR_GET_ACTION g,
  128. const char **file, int *line,
  129. const char **func, const char **data,
  130. int *flags);
  131. static unsigned long err_string_data_hash(const ERR_STRING_DATA *a)
  132. {
  133. unsigned long ret, l;
  134. l = a->error;
  135. ret = l ^ ERR_GET_LIB(l);
  136. return (ret ^ ret % 19 * 13);
  137. }
  138. static int err_string_data_cmp(const ERR_STRING_DATA *a,
  139. const ERR_STRING_DATA *b)
  140. {
  141. if (a->error == b->error)
  142. return 0;
  143. return a->error > b->error ? 1 : -1;
  144. }
  145. static ERR_STRING_DATA *int_err_get_item(const ERR_STRING_DATA *d)
  146. {
  147. ERR_STRING_DATA *p = NULL;
  148. CRYPTO_THREAD_read_lock(err_string_lock);
  149. p = lh_ERR_STRING_DATA_retrieve(int_error_hash, d);
  150. CRYPTO_THREAD_unlock(err_string_lock);
  151. return p;
  152. }
  153. #ifndef OPENSSL_NO_ERR
  154. /* 2019-05-21: Russian and Ukrainian locales on Linux require more than 6,5 kB */
  155. # define SPACE_SYS_STR_REASONS 8 * 1024
  156. # define NUM_SYS_STR_REASONS 127
  157. static ERR_STRING_DATA SYS_str_reasons[NUM_SYS_STR_REASONS + 1];
  158. /*
  159. * SYS_str_reasons is filled with copies of strerror() results at
  160. * initialization. 'errno' values up to 127 should cover all usual errors,
  161. * others will be displayed numerically by ERR_error_string. It is crucial
  162. * that we have something for each reason code that occurs in
  163. * ERR_str_reasons, or bogus reason strings will be returned for SYSerr(),
  164. * which always gets an errno value and never one of those 'standard' reason
  165. * codes.
  166. */
  167. static void build_SYS_str_reasons(void)
  168. {
  169. /* OPENSSL_malloc cannot be used here, use static storage instead */
  170. static char strerror_pool[SPACE_SYS_STR_REASONS];
  171. char *cur = strerror_pool;
  172. size_t cnt = 0;
  173. static int init = 1;
  174. int i;
  175. int saveerrno = get_last_sys_error();
  176. CRYPTO_THREAD_write_lock(err_string_lock);
  177. if (!init) {
  178. CRYPTO_THREAD_unlock(err_string_lock);
  179. return;
  180. }
  181. for (i = 1; i <= NUM_SYS_STR_REASONS; i++) {
  182. ERR_STRING_DATA *str = &SYS_str_reasons[i - 1];
  183. str->error = ERR_PACK(ERR_LIB_SYS, 0, i);
  184. /*
  185. * If we have used up all the space in strerror_pool,
  186. * there's no point in calling openssl_strerror_r()
  187. */
  188. if (str->string == NULL && cnt < sizeof(strerror_pool)) {
  189. if (openssl_strerror_r(i, cur, sizeof(strerror_pool) - cnt)) {
  190. size_t l = strlen(cur);
  191. str->string = cur;
  192. cnt += l;
  193. cur += l;
  194. /*
  195. * VMS has an unusual quirk of adding spaces at the end of
  196. * some (most? all?) messages. Lets trim them off.
  197. */
  198. while (cur > strerror_pool && ossl_isspace(cur[-1])) {
  199. cur--;
  200. cnt--;
  201. }
  202. *cur++ = '\0';
  203. cnt++;
  204. }
  205. }
  206. if (str->string == NULL)
  207. str->string = "unknown";
  208. }
  209. /*
  210. * Now we still have SYS_str_reasons[NUM_SYS_STR_REASONS] = {0, NULL}, as
  211. * required by ERR_load_strings.
  212. */
  213. init = 0;
  214. CRYPTO_THREAD_unlock(err_string_lock);
  215. /* openssl_strerror_r could change errno, but we want to preserve it */
  216. set_sys_error(saveerrno);
  217. err_load_strings(SYS_str_reasons);
  218. }
  219. #endif
  220. static void ERR_STATE_free(ERR_STATE *s)
  221. {
  222. int i;
  223. if (s == NULL)
  224. return;
  225. for (i = 0; i < ERR_NUM_ERRORS; i++) {
  226. err_clear_data(s, i, 1);
  227. }
  228. OPENSSL_free(s);
  229. }
  230. DEFINE_RUN_ONCE_STATIC(do_err_strings_init)
  231. {
  232. if (!OPENSSL_init_crypto(0, NULL))
  233. return 0;
  234. err_string_lock = CRYPTO_THREAD_lock_new();
  235. if (err_string_lock == NULL)
  236. return 0;
  237. int_error_hash = lh_ERR_STRING_DATA_new(err_string_data_hash,
  238. err_string_data_cmp);
  239. if (int_error_hash == NULL) {
  240. CRYPTO_THREAD_lock_free(err_string_lock);
  241. err_string_lock = NULL;
  242. return 0;
  243. }
  244. return 1;
  245. }
  246. void err_cleanup(void)
  247. {
  248. if (set_err_thread_local != 0)
  249. CRYPTO_THREAD_cleanup_local(&err_thread_local);
  250. CRYPTO_THREAD_lock_free(err_string_lock);
  251. err_string_lock = NULL;
  252. lh_ERR_STRING_DATA_free(int_error_hash);
  253. int_error_hash = NULL;
  254. }
  255. /*
  256. * Legacy; pack in the library.
  257. */
  258. static void err_patch(int lib, ERR_STRING_DATA *str)
  259. {
  260. unsigned long plib = ERR_PACK(lib, 0, 0);
  261. for (; str->error != 0; str++)
  262. str->error |= plib;
  263. }
  264. /*
  265. * Hash in |str| error strings. Assumes the URN_ONCE was done.
  266. */
  267. static int err_load_strings(const ERR_STRING_DATA *str)
  268. {
  269. CRYPTO_THREAD_write_lock(err_string_lock);
  270. for (; str->error; str++)
  271. (void)lh_ERR_STRING_DATA_insert(int_error_hash,
  272. (ERR_STRING_DATA *)str);
  273. CRYPTO_THREAD_unlock(err_string_lock);
  274. return 1;
  275. }
  276. int ERR_load_ERR_strings(void)
  277. {
  278. #ifndef OPENSSL_NO_ERR
  279. if (!RUN_ONCE(&err_string_init, do_err_strings_init))
  280. return 0;
  281. err_load_strings(ERR_str_libraries);
  282. err_load_strings(ERR_str_reasons);
  283. build_SYS_str_reasons();
  284. #endif
  285. return 1;
  286. }
  287. int ERR_load_strings(int lib, ERR_STRING_DATA *str)
  288. {
  289. if (ERR_load_ERR_strings() == 0)
  290. return 0;
  291. err_patch(lib, str);
  292. err_load_strings(str);
  293. return 1;
  294. }
  295. int ERR_load_strings_const(const ERR_STRING_DATA *str)
  296. {
  297. if (ERR_load_ERR_strings() == 0)
  298. return 0;
  299. err_load_strings(str);
  300. return 1;
  301. }
  302. int ERR_unload_strings(int lib, ERR_STRING_DATA *str)
  303. {
  304. if (!RUN_ONCE(&err_string_init, do_err_strings_init))
  305. return 0;
  306. CRYPTO_THREAD_write_lock(err_string_lock);
  307. /*
  308. * We don't need to ERR_PACK the lib, since that was done (to
  309. * the table) when it was loaded.
  310. */
  311. for (; str->error; str++)
  312. (void)lh_ERR_STRING_DATA_delete(int_error_hash, str);
  313. CRYPTO_THREAD_unlock(err_string_lock);
  314. return 1;
  315. }
  316. void err_free_strings_int(void)
  317. {
  318. if (!RUN_ONCE(&err_string_init, do_err_strings_init))
  319. return;
  320. }
  321. /********************************************************/
  322. void ERR_clear_error(void)
  323. {
  324. int i;
  325. ERR_STATE *es;
  326. es = err_get_state_int();
  327. if (es == NULL)
  328. return;
  329. for (i = 0; i < ERR_NUM_ERRORS; i++) {
  330. err_clear(es, i, 0);
  331. }
  332. es->top = es->bottom = 0;
  333. }
  334. unsigned long ERR_get_error(void)
  335. {
  336. return get_error_values(EV_POP, NULL, NULL, NULL, NULL, NULL);
  337. }
  338. unsigned long ERR_get_error_line(const char **file, int *line)
  339. {
  340. return get_error_values(EV_POP, file, line, NULL, NULL, NULL);
  341. }
  342. unsigned long ERR_get_error_func(const char **func)
  343. {
  344. return get_error_values(EV_POP, NULL, NULL, func, NULL, NULL);
  345. }
  346. unsigned long ERR_get_error_data(const char **data, int *flags)
  347. {
  348. return get_error_values(EV_POP, NULL, NULL, NULL, data, flags);
  349. }
  350. unsigned long ERR_get_error_all(const char **file, int *line,
  351. const char **func,
  352. const char **data, int *flags)
  353. {
  354. return get_error_values(EV_POP, file, line, func, data, flags);
  355. }
  356. #ifndef OPENSSL_NO_DEPRECATED_3_0
  357. unsigned long ERR_get_error_line_data(const char **file, int *line,
  358. const char **data, int *flags)
  359. {
  360. return get_error_values(EV_POP, file, line, NULL, data, flags);
  361. }
  362. #endif
  363. unsigned long ERR_peek_error(void)
  364. {
  365. return get_error_values(EV_PEEK, NULL, NULL, NULL, NULL, NULL);
  366. }
  367. unsigned long ERR_peek_error_line(const char **file, int *line)
  368. {
  369. return get_error_values(EV_PEEK, file, line, NULL, NULL, NULL);
  370. }
  371. unsigned long ERR_peek_error_func(const char **func)
  372. {
  373. return get_error_values(EV_PEEK, NULL, NULL, func, NULL, NULL);
  374. }
  375. unsigned long ERR_peek_error_data(const char **data, int *flags)
  376. {
  377. return get_error_values(EV_PEEK, NULL, NULL, NULL, data, flags);
  378. }
  379. unsigned long ERR_peek_error_all(const char **file, int *line,
  380. const char **func,
  381. const char **data, int *flags)
  382. {
  383. return get_error_values(EV_PEEK, file, line, func, data, flags);
  384. }
  385. #ifndef OPENSSL_NO_DEPRECATED_3_0
  386. unsigned long ERR_peek_error_line_data(const char **file, int *line,
  387. const char **data, int *flags)
  388. {
  389. return get_error_values(EV_PEEK, file, line, NULL, data, flags);
  390. }
  391. #endif
  392. unsigned long ERR_peek_last_error(void)
  393. {
  394. return get_error_values(EV_PEEK_LAST, NULL, NULL, NULL, NULL, NULL);
  395. }
  396. unsigned long ERR_peek_last_error_line(const char **file, int *line)
  397. {
  398. return get_error_values(EV_PEEK_LAST, file, line, NULL, NULL, NULL);
  399. }
  400. unsigned long ERR_peek_last_error_func(const char **func)
  401. {
  402. return get_error_values(EV_PEEK_LAST, NULL, NULL, func, NULL, NULL);
  403. }
  404. unsigned long ERR_peek_last_error_data(const char **data, int *flags)
  405. {
  406. return get_error_values(EV_PEEK_LAST, NULL, NULL, NULL, data, flags);
  407. }
  408. unsigned long ERR_peek_last_error_all(const char **file, int *line,
  409. const char **func,
  410. const char **data, int *flags)
  411. {
  412. return get_error_values(EV_PEEK_LAST, file, line, func, data, flags);
  413. }
  414. #ifndef OPENSSL_NO_DEPRECATED_3_0
  415. unsigned long ERR_peek_last_error_line_data(const char **file, int *line,
  416. const char **data, int *flags)
  417. {
  418. return get_error_values(EV_PEEK_LAST, file, line, NULL, data, flags);
  419. }
  420. #endif
  421. static unsigned long get_error_values(ERR_GET_ACTION g,
  422. const char **file, int *line,
  423. const char **func,
  424. const char **data, int *flags)
  425. {
  426. int i = 0;
  427. ERR_STATE *es;
  428. unsigned long ret;
  429. es = err_get_state_int();
  430. if (es == NULL)
  431. return 0;
  432. /*
  433. * Clear anything that should have been cleared earlier. We do this
  434. * here because this doesn't have constant-time issues.
  435. */
  436. while (es->bottom != es->top) {
  437. if (es->err_flags[es->top] & ERR_FLAG_CLEAR) {
  438. err_clear(es, es->top, 0);
  439. es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1;
  440. continue;
  441. }
  442. i = (es->bottom + 1) % ERR_NUM_ERRORS;
  443. if (es->err_flags[i] & ERR_FLAG_CLEAR) {
  444. es->bottom = i;
  445. err_clear(es, es->bottom, 0);
  446. continue;
  447. }
  448. break;
  449. }
  450. /* If everything has been cleared, the stack is empty. */
  451. if (es->bottom == es->top)
  452. return 0;
  453. /* Which error, the top of stack (latest one) or the first one? */
  454. if (g == EV_PEEK_LAST)
  455. i = es->top;
  456. else
  457. i = (es->bottom + 1) % ERR_NUM_ERRORS;
  458. ret = es->err_buffer[i];
  459. if (g == EV_POP) {
  460. es->bottom = i;
  461. es->err_buffer[i] = 0;
  462. }
  463. if (file != NULL) {
  464. *file = es->err_file[i];
  465. if (*file == NULL)
  466. *file = "";
  467. }
  468. if (line != NULL)
  469. *line = es->err_line[i];
  470. if (func != NULL) {
  471. *func = es->err_func[i];
  472. if (*func == NULL)
  473. *func = "";
  474. }
  475. if (flags != NULL)
  476. *flags = es->err_data_flags[i];
  477. if (data == NULL) {
  478. if (g == EV_POP) {
  479. err_clear_data(es, i, 0);
  480. }
  481. } else {
  482. *data = es->err_data[i];
  483. if (*data == NULL) {
  484. *data = "";
  485. if (flags != NULL)
  486. *flags = 0;
  487. }
  488. }
  489. return ret;
  490. }
  491. void ERR_error_string_n(unsigned long e, char *buf, size_t len)
  492. {
  493. char lsbuf[64], rsbuf[64];
  494. const char *ls, *rs;
  495. unsigned long f = 0, l, r;
  496. if (len == 0)
  497. return;
  498. l = ERR_GET_LIB(e);
  499. ls = ERR_lib_error_string(e);
  500. if (ls == NULL) {
  501. BIO_snprintf(lsbuf, sizeof(lsbuf), "lib(%lu)", l);
  502. ls = lsbuf;
  503. }
  504. rs = ERR_reason_error_string(e);
  505. r = ERR_GET_REASON(e);
  506. if (rs == NULL) {
  507. BIO_snprintf(rsbuf, sizeof(rsbuf), "reason(%lu)", r);
  508. rs = rsbuf;
  509. }
  510. BIO_snprintf(buf, len, "error:%08lX:%s:%s:%s", e, ls, "", rs);
  511. if (strlen(buf) == len - 1) {
  512. /* Didn't fit; use a minimal format. */
  513. BIO_snprintf(buf, len, "err:%lx:%lx:%lx:%lx", e, l, f, r);
  514. }
  515. }
  516. /*
  517. * ERR_error_string_n should be used instead for ret != NULL as
  518. * ERR_error_string cannot know how large the buffer is
  519. */
  520. char *ERR_error_string(unsigned long e, char *ret)
  521. {
  522. static char buf[256];
  523. if (ret == NULL)
  524. ret = buf;
  525. ERR_error_string_n(e, ret, (int)sizeof(buf));
  526. return ret;
  527. }
  528. const char *ERR_lib_error_string(unsigned long e)
  529. {
  530. ERR_STRING_DATA d, *p;
  531. unsigned long l;
  532. if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
  533. return NULL;
  534. }
  535. l = ERR_GET_LIB(e);
  536. d.error = ERR_PACK(l, 0, 0);
  537. p = int_err_get_item(&d);
  538. return ((p == NULL) ? NULL : p->string);
  539. }
  540. #ifndef OPENSSL_NO_DEPRECATED_3_0
  541. const char *ERR_func_error_string(unsigned long e)
  542. {
  543. return NULL;
  544. }
  545. #endif
  546. const char *ERR_reason_error_string(unsigned long e)
  547. {
  548. ERR_STRING_DATA d, *p = NULL;
  549. unsigned long l, r;
  550. if (!RUN_ONCE(&err_string_init, do_err_strings_init)) {
  551. return NULL;
  552. }
  553. l = ERR_GET_LIB(e);
  554. r = ERR_GET_REASON(e);
  555. d.error = ERR_PACK(l, 0, r);
  556. p = int_err_get_item(&d);
  557. if (p == NULL) {
  558. d.error = ERR_PACK(0, 0, r);
  559. p = int_err_get_item(&d);
  560. }
  561. return ((p == NULL) ? NULL : p->string);
  562. }
  563. /* TODO(3.0): arg ignored for now */
  564. static void err_delete_thread_state(void *arg)
  565. {
  566. ERR_STATE *state = CRYPTO_THREAD_get_local(&err_thread_local);
  567. if (state == NULL)
  568. return;
  569. CRYPTO_THREAD_set_local(&err_thread_local, NULL);
  570. ERR_STATE_free(state);
  571. }
  572. #ifndef OPENSSL_NO_DEPRECATED_1_1_0
  573. void ERR_remove_thread_state(void *dummy)
  574. {
  575. }
  576. #endif
  577. #ifndef OPENSSL_NO_DEPRECATED_1_0_0
  578. void ERR_remove_state(unsigned long pid)
  579. {
  580. }
  581. #endif
  582. DEFINE_RUN_ONCE_STATIC(err_do_init)
  583. {
  584. set_err_thread_local = 1;
  585. return CRYPTO_THREAD_init_local(&err_thread_local, NULL);
  586. }
  587. ERR_STATE *err_get_state_int(void)
  588. {
  589. ERR_STATE *state;
  590. int saveerrno = get_last_sys_error();
  591. if (!OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL))
  592. return NULL;
  593. if (!RUN_ONCE(&err_init, err_do_init))
  594. return NULL;
  595. state = CRYPTO_THREAD_get_local(&err_thread_local);
  596. if (state == (ERR_STATE*)-1)
  597. return NULL;
  598. if (state == NULL) {
  599. if (!CRYPTO_THREAD_set_local(&err_thread_local, (ERR_STATE*)-1))
  600. return NULL;
  601. if ((state = OPENSSL_zalloc(sizeof(*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. 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 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. CRYPTO_THREAD_write_lock(err_string_lock);
  667. ret = int_err_library_number++;
  668. CRYPTO_THREAD_unlock(err_string_lock);
  669. return ret;
  670. }
  671. static int err_set_error_data_int(char *data, size_t size, int flags,
  672. int deallocate)
  673. {
  674. ERR_STATE *es;
  675. es = err_get_state_int();
  676. if (es == NULL)
  677. return 0;
  678. err_clear_data(es, es->top, deallocate);
  679. err_set_data(es, es->top, data, size, flags);
  680. return 1;
  681. }
  682. void ERR_set_error_data(char *data, int flags)
  683. {
  684. /*
  685. * This function is void so we cannot propagate the error return. Since it
  686. * is also in the public API we can't change the return type.
  687. *
  688. * We estimate the size of the data. If it's not flagged as allocated,
  689. * then this is safe, and if it is flagged as allocated, then our size
  690. * may be smaller than the actual allocation, but that doesn't matter
  691. * too much, the buffer will remain untouched or will eventually be
  692. * reallocated to a new size.
  693. *
  694. * callers should be advised that this function takes over ownership of
  695. * the allocated memory, i.e. they can't count on the pointer to remain
  696. * valid.
  697. */
  698. err_set_error_data_int(data, strlen(data) + 1, flags, 1);
  699. }
  700. void ERR_add_error_data(int num, ...)
  701. {
  702. va_list args;
  703. va_start(args, num);
  704. ERR_add_error_vdata(num, args);
  705. va_end(args);
  706. }
  707. void ERR_add_error_vdata(int num, va_list args)
  708. {
  709. int i, len, size;
  710. int flags = ERR_TXT_MALLOCED | ERR_TXT_STRING;
  711. char *str, *arg;
  712. ERR_STATE *es;
  713. /* Get the current error data; if an allocated string get it. */
  714. es = err_get_state_int();
  715. if (es == NULL)
  716. return;
  717. i = es->top;
  718. /*
  719. * If err_data is allocated already, re-use the space.
  720. * Otherwise, allocate a small new buffer.
  721. */
  722. if ((es->err_data_flags[i] & flags) == flags) {
  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. int ERR_set_mark(void)
  761. {
  762. ERR_STATE *es;
  763. es = err_get_state_int();
  764. if (es == NULL)
  765. return 0;
  766. if (es->bottom == es->top)
  767. return 0;
  768. es->err_flags[es->top] |= ERR_FLAG_MARK;
  769. return 1;
  770. }
  771. int ERR_pop_to_mark(void)
  772. {
  773. ERR_STATE *es;
  774. es = err_get_state_int();
  775. if (es == NULL)
  776. return 0;
  777. while (es->bottom != es->top
  778. && (es->err_flags[es->top] & ERR_FLAG_MARK) == 0) {
  779. err_clear(es, es->top, 0);
  780. es->top = es->top > 0 ? es->top - 1 : ERR_NUM_ERRORS - 1;
  781. }
  782. if (es->bottom == es->top)
  783. return 0;
  784. es->err_flags[es->top] &= ~ERR_FLAG_MARK;
  785. return 1;
  786. }
  787. int ERR_clear_last_mark(void)
  788. {
  789. ERR_STATE *es;
  790. int top;
  791. es = err_get_state_int();
  792. if (es == NULL)
  793. return 0;
  794. top = es->top;
  795. while (es->bottom != top
  796. && (es->err_flags[top] & ERR_FLAG_MARK) == 0) {
  797. top = top > 0 ? top - 1 : ERR_NUM_ERRORS - 1;
  798. }
  799. if (es->bottom == top)
  800. return 0;
  801. es->err_flags[top] &= ~ERR_FLAG_MARK;
  802. return 1;
  803. }
  804. void err_clear_last_constant_time(int clear)
  805. {
  806. ERR_STATE *es;
  807. int top;
  808. es = err_get_state_int();
  809. if (es == NULL)
  810. return;
  811. top = es->top;
  812. /*
  813. * Flag error as cleared but remove it elsewhere to avoid two errors
  814. * accessing the same error stack location, revealing timing information.
  815. */
  816. clear = constant_time_select_int(constant_time_eq_int(clear, 0),
  817. 0, ERR_FLAG_CLEAR);
  818. es->err_flags[top] |= clear;
  819. }