No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com). */
  108. #include <openssl/err.h>
  109. #include <assert.h>
  110. #include <errno.h>
  111. #include <inttypes.h>
  112. #include <string.h>
  113. #if defined(OPENSSL_WINDOWS)
  114. #pragma warning(push, 3)
  115. #include <windows.h>
  116. #pragma warning(pop)
  117. #endif
  118. #include <openssl/mem.h>
  119. #include <openssl/thread.h>
  120. #include "../internal.h"
  121. extern const uint32_t kOpenSSLReasonValues[];
  122. extern const size_t kOpenSSLReasonValuesLen;
  123. extern const char kOpenSSLReasonStringData[];
  124. /* err_clear_data frees the optional |data| member of the given error. */
  125. static void err_clear_data(struct err_error_st *error) {
  126. if ((error->flags & ERR_FLAG_MALLOCED) != 0) {
  127. OPENSSL_free(error->data);
  128. }
  129. error->data = NULL;
  130. error->flags &= ~ERR_FLAG_MALLOCED;
  131. }
  132. /* err_clear clears the given queued error. */
  133. static void err_clear(struct err_error_st *error) {
  134. err_clear_data(error);
  135. memset(error, 0, sizeof(struct err_error_st));
  136. }
  137. /* global_next_library contains the next custom library value to return. */
  138. static int global_next_library = ERR_NUM_LIBS;
  139. /* global_next_library_mutex protects |global_next_library| from concurrent
  140. * updates. */
  141. static struct CRYPTO_STATIC_MUTEX global_next_library_mutex =
  142. CRYPTO_STATIC_MUTEX_INIT;
  143. static void err_state_free(void *statep) {
  144. ERR_STATE *state = statep;
  145. if (state == NULL) {
  146. return;
  147. }
  148. unsigned i;
  149. for (i = 0; i < ERR_NUM_ERRORS; i++) {
  150. err_clear(&state->errors[i]);
  151. }
  152. OPENSSL_free(state->to_free);
  153. OPENSSL_free(state);
  154. }
  155. /* err_get_state gets the ERR_STATE object for the current thread. */
  156. static ERR_STATE *err_get_state(void) {
  157. ERR_STATE *state = CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_ERR);
  158. if (state == NULL) {
  159. state = OPENSSL_malloc(sizeof(ERR_STATE));
  160. if (state == NULL) {
  161. return NULL;
  162. }
  163. memset(state, 0, sizeof(ERR_STATE));
  164. if (!CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_ERR, state,
  165. err_state_free)) {
  166. return NULL;
  167. }
  168. }
  169. return state;
  170. }
  171. static uint32_t get_error_values(int inc, int top, const char **file, int *line,
  172. const char **data, int *flags) {
  173. unsigned i = 0;
  174. ERR_STATE *state;
  175. struct err_error_st *error;
  176. uint32_t ret;
  177. state = err_get_state();
  178. if (state == NULL || state->bottom == state->top) {
  179. return 0;
  180. }
  181. if (top) {
  182. assert(!inc);
  183. /* last error */
  184. i = state->top;
  185. } else {
  186. i = (state->bottom + 1) % ERR_NUM_ERRORS;
  187. }
  188. error = &state->errors[i];
  189. ret = error->packed;
  190. if (file != NULL && line != NULL) {
  191. if (error->file == NULL) {
  192. *file = "NA";
  193. *line = 0;
  194. } else {
  195. *file = error->file;
  196. *line = error->line;
  197. }
  198. }
  199. if (data != NULL) {
  200. if (error->data == NULL) {
  201. *data = "";
  202. if (flags != NULL) {
  203. *flags = 0;
  204. }
  205. } else {
  206. *data = error->data;
  207. if (flags != NULL) {
  208. *flags = error->flags & ERR_FLAG_PUBLIC_MASK;
  209. }
  210. /* If this error is being removed, take ownership of data from
  211. * the error. The semantics are such that the caller doesn't
  212. * take ownership either. Instead the error system takes
  213. * ownership and retains it until the next call that affects the
  214. * error queue. */
  215. if (inc) {
  216. if (error->flags & ERR_FLAG_MALLOCED) {
  217. OPENSSL_free(state->to_free);
  218. state->to_free = error->data;
  219. }
  220. error->data = NULL;
  221. error->flags = 0;
  222. }
  223. }
  224. }
  225. if (inc) {
  226. assert(!top);
  227. err_clear(error);
  228. state->bottom = i;
  229. }
  230. return ret;
  231. }
  232. uint32_t ERR_get_error(void) {
  233. return get_error_values(1 /* inc */, 0 /* bottom */, NULL, NULL, NULL, NULL);
  234. }
  235. uint32_t ERR_get_error_line(const char **file, int *line) {
  236. return get_error_values(1 /* inc */, 0 /* bottom */, file, line, NULL, NULL);
  237. }
  238. uint32_t ERR_get_error_line_data(const char **file, int *line,
  239. const char **data, int *flags) {
  240. return get_error_values(1 /* inc */, 0 /* bottom */, file, line, data, flags);
  241. }
  242. uint32_t ERR_peek_error(void) {
  243. return get_error_values(0 /* peek */, 0 /* bottom */, NULL, NULL, NULL, NULL);
  244. }
  245. uint32_t ERR_peek_error_line(const char **file, int *line) {
  246. return get_error_values(0 /* peek */, 0 /* bottom */, file, line, NULL, NULL);
  247. }
  248. uint32_t ERR_peek_error_line_data(const char **file, int *line,
  249. const char **data, int *flags) {
  250. return get_error_values(0 /* peek */, 0 /* bottom */, file, line, data,
  251. flags);
  252. }
  253. const char *ERR_peek_function(void) {
  254. ERR_STATE *state = err_get_state();
  255. if (state == NULL || state->bottom == state->top) {
  256. return NULL;
  257. }
  258. return state->errors[(state->bottom + 1) % ERR_NUM_ERRORS].function;
  259. }
  260. uint32_t ERR_peek_last_error(void) {
  261. return get_error_values(0 /* peek */, 1 /* top */, NULL, NULL, NULL, NULL);
  262. }
  263. uint32_t ERR_peek_last_error_line(const char **file, int *line) {
  264. return get_error_values(0 /* peek */, 1 /* top */, file, line, NULL, NULL);
  265. }
  266. uint32_t ERR_peek_last_error_line_data(const char **file, int *line,
  267. const char **data, int *flags) {
  268. return get_error_values(0 /* peek */, 1 /* top */, file, line, data, flags);
  269. }
  270. void ERR_clear_error(void) {
  271. ERR_STATE *const state = err_get_state();
  272. unsigned i;
  273. if (state == NULL) {
  274. return;
  275. }
  276. for (i = 0; i < ERR_NUM_ERRORS; i++) {
  277. err_clear(&state->errors[i]);
  278. }
  279. OPENSSL_free(state->to_free);
  280. state->to_free = NULL;
  281. state->top = state->bottom = 0;
  282. }
  283. void ERR_remove_thread_state(const CRYPTO_THREADID *tid) {
  284. if (tid != NULL) {
  285. assert(0);
  286. return;
  287. }
  288. ERR_clear_error();
  289. }
  290. int ERR_get_next_error_library(void) {
  291. int ret;
  292. CRYPTO_STATIC_MUTEX_lock_write(&global_next_library_mutex);
  293. ret = global_next_library++;
  294. CRYPTO_STATIC_MUTEX_unlock(&global_next_library_mutex);
  295. return ret;
  296. }
  297. void ERR_remove_state(unsigned long pid) {
  298. ERR_clear_error();
  299. }
  300. void ERR_clear_system_error(void) {
  301. errno = 0;
  302. }
  303. static void err_error_string(uint32_t packed_error, const char *func_str,
  304. char *buf, size_t len) {
  305. char lib_buf[64], reason_buf[64];
  306. const char *lib_str, *reason_str;
  307. unsigned lib, reason;
  308. if (len == 0) {
  309. return;
  310. }
  311. lib = ERR_GET_LIB(packed_error);
  312. reason = ERR_GET_REASON(packed_error);
  313. lib_str = ERR_lib_error_string(packed_error);
  314. reason_str = ERR_reason_error_string(packed_error);
  315. if (lib_str == NULL) {
  316. BIO_snprintf(lib_buf, sizeof(lib_buf), "lib(%u)", lib);
  317. lib_str = lib_buf;
  318. }
  319. if (func_str == NULL) {
  320. func_str = "OPENSSL_internal";
  321. }
  322. if (reason_str == NULL) {
  323. BIO_snprintf(reason_buf, sizeof(reason_buf), "reason(%u)", reason);
  324. reason_str = reason_buf;
  325. }
  326. BIO_snprintf(buf, len, "error:%08" PRIx32 ":%s:%s:%s",
  327. packed_error, lib_str, func_str, reason_str);
  328. if (strlen(buf) == len - 1) {
  329. /* output may be truncated; make sure we always have 5 colon-separated
  330. * fields, i.e. 4 colons. */
  331. static const unsigned num_colons = 4;
  332. unsigned i;
  333. char *s = buf;
  334. if (len <= num_colons) {
  335. /* In this situation it's not possible to ensure that the correct number
  336. * of colons are included in the output. */
  337. return;
  338. }
  339. for (i = 0; i < num_colons; i++) {
  340. char *colon = strchr(s, ':');
  341. char *last_pos = &buf[len - 1] - num_colons + i;
  342. if (colon == NULL || colon > last_pos) {
  343. /* set colon |i| at last possible position (buf[len-1] is the
  344. * terminating 0). If we're setting this colon, then all whole of the
  345. * rest of the string must be colons in order to have the correct
  346. * number. */
  347. memset(last_pos, ':', num_colons - i);
  348. break;
  349. }
  350. s = colon + 1;
  351. }
  352. }
  353. }
  354. char *ERR_error_string(uint32_t packed_error, char *ret) {
  355. static char buf[ERR_ERROR_STRING_BUF_LEN];
  356. if (ret == NULL) {
  357. /* TODO(fork): remove this. */
  358. ret = buf;
  359. }
  360. #if !defined(NDEBUG)
  361. /* This is aimed to help catch callers who don't provide
  362. * |ERR_ERROR_STRING_BUF_LEN| bytes of space. */
  363. memset(ret, 0, ERR_ERROR_STRING_BUF_LEN);
  364. #endif
  365. ERR_error_string_n(packed_error, ret, ERR_ERROR_STRING_BUF_LEN);
  366. return ret;
  367. }
  368. void ERR_error_string_n(uint32_t packed_error, char *buf, size_t len) {
  369. err_error_string(packed_error, NULL, buf, len);
  370. }
  371. // err_string_cmp is a compare function for searching error values with
  372. // |bsearch| in |err_string_lookup|.
  373. static int err_string_cmp(const void *a, const void *b) {
  374. const uint32_t a_key = *((const uint32_t*) a) >> 15;
  375. const uint32_t b_key = *((const uint32_t*) b) >> 15;
  376. if (a_key < b_key) {
  377. return -1;
  378. } else if (a_key > b_key) {
  379. return 1;
  380. } else {
  381. return 0;
  382. }
  383. }
  384. /* err_string_lookup looks up the string associated with |lib| and |key| in
  385. * |values| and |string_data|. It returns the string or NULL if not found. */
  386. static const char *err_string_lookup(uint32_t lib, uint32_t key,
  387. const uint32_t *values,
  388. size_t num_values,
  389. const char *string_data) {
  390. /* |values| points to data in err_data.h, which is generated by
  391. * err_data_generate.go. It's an array of uint32_t values. Each value has the
  392. * following structure:
  393. * | lib | key | offset |
  394. * |6 bits| 11 bits | 15 bits |
  395. *
  396. * The |lib| value is a library identifier: one of the |ERR_LIB_*| values.
  397. * The |key| is either a function or a reason code, depending on the context.
  398. * The |offset| is the number of bytes from the start of |string_data| where
  399. * the (NUL terminated) string for this value can be found.
  400. *
  401. * Values are sorted based on treating the |lib| and |key| part as an
  402. * unsigned integer. */
  403. if (lib >= (1 << 6) || key >= (1 << 11)) {
  404. return NULL;
  405. }
  406. uint32_t search_key = lib << 26 | key << 15;
  407. const uint32_t *result = bsearch(&search_key, values, num_values,
  408. sizeof(uint32_t), err_string_cmp);
  409. if (result == NULL) {
  410. return NULL;
  411. }
  412. return &string_data[(*result) & 0x7fff];
  413. }
  414. static const char *const kLibraryNames[ERR_NUM_LIBS] = {
  415. "invalid library (0)",
  416. "unknown library", /* ERR_LIB_NONE */
  417. "system library", /* ERR_LIB_SYS */
  418. "bignum routines", /* ERR_LIB_BN */
  419. "RSA routines", /* ERR_LIB_RSA */
  420. "Diffie-Hellman routines", /* ERR_LIB_DH */
  421. "public key routines", /* ERR_LIB_EVP */
  422. "memory buffer routines", /* ERR_LIB_BUF */
  423. "object identifier routines", /* ERR_LIB_OBJ */
  424. "PEM routines", /* ERR_LIB_PEM */
  425. "DSA routines", /* ERR_LIB_DSA */
  426. "X.509 certificate routines", /* ERR_LIB_X509 */
  427. "ASN.1 encoding routines", /* ERR_LIB_ASN1 */
  428. "configuration file routines", /* ERR_LIB_CONF */
  429. "common libcrypto routines", /* ERR_LIB_CRYPTO */
  430. "elliptic curve routines", /* ERR_LIB_EC */
  431. "SSL routines", /* ERR_LIB_SSL */
  432. "BIO routines", /* ERR_LIB_BIO */
  433. "PKCS7 routines", /* ERR_LIB_PKCS7 */
  434. "PKCS8 routines", /* ERR_LIB_PKCS8 */
  435. "X509 V3 routines", /* ERR_LIB_X509V3 */
  436. "random number generator", /* ERR_LIB_RAND */
  437. "ENGINE routines", /* ERR_LIB_ENGINE */
  438. "OCSP routines", /* ERR_LIB_OCSP */
  439. "UI routines", /* ERR_LIB_UI */
  440. "COMP routines", /* ERR_LIB_COMP */
  441. "ECDSA routines", /* ERR_LIB_ECDSA */
  442. "ECDH routines", /* ERR_LIB_ECDH */
  443. "HMAC routines", /* ERR_LIB_HMAC */
  444. "Digest functions", /* ERR_LIB_DIGEST */
  445. "Cipher functions", /* ERR_LIB_CIPHER */
  446. "HKDF functions", /* ERR_LIB_HKDF */
  447. "User defined functions", /* ERR_LIB_USER */
  448. };
  449. const char *ERR_lib_error_string(uint32_t packed_error) {
  450. const uint32_t lib = ERR_GET_LIB(packed_error);
  451. if (lib >= ERR_NUM_LIBS) {
  452. return NULL;
  453. }
  454. return kLibraryNames[lib];
  455. }
  456. const char *ERR_func_error_string(uint32_t packed_error) {
  457. return "OPENSSL_internal";
  458. }
  459. const char *ERR_reason_error_string(uint32_t packed_error) {
  460. const uint32_t lib = ERR_GET_LIB(packed_error);
  461. const uint32_t reason = ERR_GET_REASON(packed_error);
  462. if (lib == ERR_LIB_SYS) {
  463. if (reason < 127) {
  464. return strerror(reason);
  465. }
  466. return NULL;
  467. }
  468. if (reason < ERR_NUM_LIBS) {
  469. return kLibraryNames[reason];
  470. }
  471. if (reason < 100) {
  472. switch (reason) {
  473. case ERR_R_MALLOC_FAILURE:
  474. return "malloc failure";
  475. case ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED:
  476. return "function should not have been called";
  477. case ERR_R_PASSED_NULL_PARAMETER:
  478. return "passed a null parameter";
  479. case ERR_R_INTERNAL_ERROR:
  480. return "internal error";
  481. case ERR_R_OVERFLOW:
  482. return "overflow";
  483. default:
  484. return NULL;
  485. }
  486. }
  487. return err_string_lookup(lib, reason, kOpenSSLReasonValues,
  488. kOpenSSLReasonValuesLen, kOpenSSLReasonStringData);
  489. }
  490. void ERR_print_errors_cb(ERR_print_errors_callback_t callback, void *ctx) {
  491. char buf[ERR_ERROR_STRING_BUF_LEN];
  492. char buf2[1024];
  493. const char *file, *data;
  494. int line, flags;
  495. uint32_t packed_error;
  496. /* thread_hash is the least-significant bits of the |ERR_STATE| pointer value
  497. * for this thread. */
  498. const unsigned long thread_hash = (uintptr_t) err_get_state();
  499. for (;;) {
  500. const char *function = ERR_peek_function();
  501. packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
  502. if (packed_error == 0) {
  503. break;
  504. }
  505. err_error_string(packed_error, function, buf, sizeof(buf));
  506. BIO_snprintf(buf2, sizeof(buf2), "%lu:%s:%s:%d:%s\n", thread_hash, buf,
  507. file, line, (flags & ERR_FLAG_STRING) ? data : "");
  508. if (callback(buf2, strlen(buf2), ctx) <= 0) {
  509. break;
  510. }
  511. }
  512. }
  513. static int print_errors_to_file(const char* msg, size_t msg_len, void* ctx) {
  514. assert(msg[msg_len] == '\0');
  515. FILE* fp = ctx;
  516. int res = fputs(msg, fp);
  517. return res < 0 ? 0 : 1;
  518. }
  519. void ERR_print_errors_fp(FILE *file) {
  520. ERR_print_errors_cb(print_errors_to_file, file);
  521. }
  522. /* err_set_error_data sets the data on the most recent error. The |flags|
  523. * argument is a combination of the |ERR_FLAG_*| values. */
  524. static void err_set_error_data(char *data, int flags) {
  525. ERR_STATE *const state = err_get_state();
  526. struct err_error_st *error;
  527. if (state == NULL || state->top == state->bottom) {
  528. if (flags & ERR_FLAG_MALLOCED) {
  529. OPENSSL_free(data);
  530. }
  531. return;
  532. }
  533. error = &state->errors[state->top];
  534. err_clear_data(error);
  535. error->data = data;
  536. error->flags = flags;
  537. }
  538. void ERR_put_error(int library, int reason, const char *function,
  539. const char *file, unsigned line) {
  540. ERR_STATE *const state = err_get_state();
  541. struct err_error_st *error;
  542. if (state == NULL) {
  543. return;
  544. }
  545. if (library == ERR_LIB_SYS && reason == 0) {
  546. #if defined(WIN32)
  547. reason = GetLastError();
  548. #else
  549. reason = errno;
  550. #endif
  551. }
  552. state->top = (state->top + 1) % ERR_NUM_ERRORS;
  553. if (state->top == state->bottom) {
  554. state->bottom = (state->bottom + 1) % ERR_NUM_ERRORS;
  555. }
  556. error = &state->errors[state->top];
  557. err_clear(error);
  558. error->function = function;
  559. error->file = file;
  560. error->line = line;
  561. error->packed = ERR_PACK(library, reason);
  562. }
  563. /* ERR_add_error_data_vdata takes a variable number of const char* pointers,
  564. * concatenates them and sets the result as the data on the most recent
  565. * error. */
  566. static void err_add_error_vdata(unsigned num, va_list args) {
  567. size_t alloced, new_len, len = 0, substr_len;
  568. char *buf;
  569. const char *substr;
  570. unsigned i;
  571. alloced = 80;
  572. buf = OPENSSL_malloc(alloced + 1);
  573. if (buf == NULL) {
  574. return;
  575. }
  576. for (i = 0; i < num; i++) {
  577. substr = va_arg(args, const char *);
  578. if (substr == NULL) {
  579. continue;
  580. }
  581. substr_len = strlen(substr);
  582. new_len = len + substr_len;
  583. if (new_len > alloced) {
  584. char *new_buf;
  585. if (alloced + 20 + 1 < alloced) {
  586. /* overflow. */
  587. OPENSSL_free(buf);
  588. return;
  589. }
  590. alloced = new_len + 20;
  591. new_buf = OPENSSL_realloc(buf, alloced + 1);
  592. if (new_buf == NULL) {
  593. OPENSSL_free(buf);
  594. return;
  595. }
  596. buf = new_buf;
  597. }
  598. memcpy(buf + len, substr, substr_len);
  599. len = new_len;
  600. }
  601. buf[len] = 0;
  602. err_set_error_data(buf, ERR_FLAG_MALLOCED | ERR_FLAG_STRING);
  603. }
  604. void ERR_add_error_data(unsigned count, ...) {
  605. va_list args;
  606. va_start(args, count);
  607. err_add_error_vdata(count, args);
  608. va_end(args);
  609. }
  610. void ERR_add_error_dataf(const char *format, ...) {
  611. va_list ap;
  612. char *buf;
  613. static const unsigned buf_len = 256;
  614. /* A fixed-size buffer is used because va_copy (which would be needed in
  615. * order to call vsnprintf twice and measure the buffer) wasn't defined until
  616. * C99. */
  617. buf = OPENSSL_malloc(buf_len + 1);
  618. if (buf == NULL) {
  619. return;
  620. }
  621. va_start(ap, format);
  622. BIO_vsnprintf(buf, buf_len, format, ap);
  623. buf[buf_len] = 0;
  624. va_end(ap);
  625. err_set_error_data(buf, ERR_FLAG_MALLOCED | ERR_FLAG_STRING);
  626. }
  627. int ERR_set_mark(void) {
  628. ERR_STATE *const state = err_get_state();
  629. if (state == NULL || state->bottom == state->top) {
  630. return 0;
  631. }
  632. state->errors[state->top].flags |= ERR_FLAG_MARK;
  633. return 1;
  634. }
  635. int ERR_pop_to_mark(void) {
  636. ERR_STATE *const state = err_get_state();
  637. if (state == NULL) {
  638. return 0;
  639. }
  640. while (state->bottom != state->top) {
  641. struct err_error_st *error = &state->errors[state->top];
  642. if ((error->flags & ERR_FLAG_MARK) != 0) {
  643. error->flags &= ~ERR_FLAG_MARK;
  644. return 1;
  645. }
  646. err_clear(error);
  647. if (state->top == 0) {
  648. state->top = ERR_NUM_ERRORS - 1;
  649. } else {
  650. state->top--;
  651. }
  652. }
  653. return 0;
  654. }
  655. void ERR_load_crypto_strings(void) {}
  656. void ERR_free_strings(void) {}
  657. void ERR_load_BIO_strings(void) {}
  658. void ERR_load_ERR_strings(void) {}