You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

796 line
24 KiB

  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 kOpenSSLFunctionValues[];
  122. extern const size_t kOpenSSLFunctionValuesLen;
  123. extern const char kOpenSSLFunctionStringData[];
  124. extern const uint32_t kOpenSSLReasonValues[];
  125. extern const size_t kOpenSSLReasonValuesLen;
  126. extern const char kOpenSSLReasonStringData[];
  127. /* err_clear_data frees the optional |data| member of the given error. */
  128. static void err_clear_data(struct err_error_st *error) {
  129. if ((error->flags & ERR_FLAG_MALLOCED) != 0) {
  130. OPENSSL_free(error->data);
  131. }
  132. error->data = NULL;
  133. error->flags &= ~ERR_FLAG_MALLOCED;
  134. }
  135. /* err_clear clears the given queued error. */
  136. static void err_clear(struct err_error_st *error) {
  137. err_clear_data(error);
  138. memset(error, 0, sizeof(struct err_error_st));
  139. }
  140. /* global_next_library contains the next custom library value to return. */
  141. static int global_next_library = ERR_NUM_LIBS;
  142. /* global_next_library_mutex protects |global_next_library| from concurrent
  143. * updates. */
  144. static struct CRYPTO_STATIC_MUTEX global_next_library_mutex =
  145. CRYPTO_STATIC_MUTEX_INIT;
  146. static void err_state_free(void *statep) {
  147. ERR_STATE *state = statep;
  148. if (state == NULL) {
  149. return;
  150. }
  151. unsigned i;
  152. for (i = 0; i < ERR_NUM_ERRORS; i++) {
  153. err_clear(&state->errors[i]);
  154. }
  155. OPENSSL_free(state->to_free);
  156. OPENSSL_free(state);
  157. }
  158. /* err_get_state gets the ERR_STATE object for the current thread. */
  159. static ERR_STATE *err_get_state(void) {
  160. ERR_STATE *state = CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_ERR);
  161. if (state == NULL) {
  162. state = OPENSSL_malloc(sizeof(ERR_STATE));
  163. if (state == NULL) {
  164. return NULL;
  165. }
  166. memset(state, 0, sizeof(ERR_STATE));
  167. if (!CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_ERR, state,
  168. err_state_free)) {
  169. return NULL;
  170. }
  171. }
  172. return state;
  173. }
  174. static uint32_t get_error_values(int inc, int top, const char **file, int *line,
  175. const char **data, int *flags) {
  176. unsigned i = 0;
  177. ERR_STATE *state;
  178. struct err_error_st *error;
  179. uint32_t ret;
  180. state = err_get_state();
  181. if (state == NULL || state->bottom == state->top) {
  182. return 0;
  183. }
  184. if (top) {
  185. assert(!inc);
  186. /* last error */
  187. i = state->top;
  188. } else {
  189. i = (state->bottom + 1) % ERR_NUM_ERRORS;
  190. }
  191. error = &state->errors[i];
  192. ret = error->packed;
  193. if (file != NULL && line != NULL) {
  194. if (error->file == NULL) {
  195. *file = "NA";
  196. *line = 0;
  197. } else {
  198. *file = error->file;
  199. *line = error->line;
  200. }
  201. }
  202. if (data != NULL) {
  203. if (error->data == NULL) {
  204. *data = "";
  205. if (flags != NULL) {
  206. *flags = 0;
  207. }
  208. } else {
  209. *data = error->data;
  210. if (flags != NULL) {
  211. *flags = error->flags & ERR_FLAG_PUBLIC_MASK;
  212. }
  213. /* If this error is being removed, take ownership of data from
  214. * the error. The semantics are such that the caller doesn't
  215. * take ownership either. Instead the error system takes
  216. * ownership and retains it until the next call that affects the
  217. * error queue. */
  218. if (inc) {
  219. if (error->flags & ERR_FLAG_MALLOCED) {
  220. OPENSSL_free(state->to_free);
  221. state->to_free = error->data;
  222. }
  223. error->data = NULL;
  224. error->flags = 0;
  225. }
  226. }
  227. }
  228. if (inc) {
  229. assert(!top);
  230. err_clear(error);
  231. state->bottom = i;
  232. }
  233. return ret;
  234. }
  235. uint32_t ERR_get_error(void) {
  236. return get_error_values(1, 0, NULL, NULL, NULL, NULL);
  237. }
  238. uint32_t ERR_get_error_line(const char **file, int *line) {
  239. return get_error_values(1, 0, file, line, NULL, NULL);
  240. }
  241. uint32_t ERR_get_error_line_data(const char **file, int *line,
  242. const char **data, int *flags) {
  243. return get_error_values(1, 0, file, line, data, flags);
  244. }
  245. uint32_t ERR_peek_error(void) {
  246. return get_error_values(0, 0, NULL, NULL, NULL, NULL);
  247. }
  248. uint32_t ERR_peek_error_line(const char **file, int *line) {
  249. return get_error_values(0, 0, file, line, NULL, NULL);
  250. }
  251. uint32_t ERR_peek_error_line_data(const char **file, int *line,
  252. const char **data, int *flags) {
  253. return get_error_values(0, 0, file, line, data, flags);
  254. }
  255. uint32_t ERR_peek_last_error(void) {
  256. return get_error_values(0, 1, NULL, NULL, NULL, NULL);
  257. }
  258. uint32_t ERR_peek_last_error_line(const char **file, int *line) {
  259. return get_error_values(0, 1, file, line, NULL, NULL);
  260. }
  261. uint32_t ERR_peek_last_error_line_data(const char **file, int *line,
  262. const char **data, int *flags) {
  263. return get_error_values(0, 1, file, line, data, flags);
  264. }
  265. void ERR_clear_error(void) {
  266. ERR_STATE *const state = err_get_state();
  267. unsigned i;
  268. if (state == NULL) {
  269. return;
  270. }
  271. for (i = 0; i < ERR_NUM_ERRORS; i++) {
  272. err_clear(&state->errors[i]);
  273. }
  274. OPENSSL_free(state->to_free);
  275. state->to_free = NULL;
  276. state->top = state->bottom = 0;
  277. }
  278. void ERR_remove_thread_state(const CRYPTO_THREADID *tid) {
  279. if (tid != NULL) {
  280. assert(0);
  281. return;
  282. }
  283. ERR_clear_error();
  284. }
  285. int ERR_get_next_error_library(void) {
  286. int ret;
  287. CRYPTO_STATIC_MUTEX_lock_write(&global_next_library_mutex);
  288. ret = global_next_library++;
  289. CRYPTO_STATIC_MUTEX_unlock(&global_next_library_mutex);
  290. return ret;
  291. }
  292. void ERR_remove_state(unsigned long pid) {
  293. ERR_clear_error();
  294. }
  295. void ERR_clear_system_error(void) {
  296. errno = 0;
  297. }
  298. char *ERR_error_string(uint32_t packed_error, char *ret) {
  299. static char buf[ERR_ERROR_STRING_BUF_LEN];
  300. if (ret == NULL) {
  301. /* TODO(fork): remove this. */
  302. ret = buf;
  303. }
  304. #if !defined(NDEBUG)
  305. /* This is aimed to help catch callers who don't provide
  306. * |ERR_ERROR_STRING_BUF_LEN| bytes of space. */
  307. memset(ret, 0, ERR_ERROR_STRING_BUF_LEN);
  308. #endif
  309. ERR_error_string_n(packed_error, ret, ERR_ERROR_STRING_BUF_LEN);
  310. return ret;
  311. }
  312. void ERR_error_string_n(uint32_t packed_error, char *buf, size_t len) {
  313. char lib_buf[64], func_buf[64], reason_buf[64];
  314. const char *lib_str, *func_str, *reason_str;
  315. unsigned lib, func, reason;
  316. if (len == 0) {
  317. return;
  318. }
  319. lib = ERR_GET_LIB(packed_error);
  320. func = ERR_GET_FUNC(packed_error);
  321. reason = ERR_GET_REASON(packed_error);
  322. lib_str = ERR_lib_error_string(packed_error);
  323. func_str = ERR_func_error_string(packed_error);
  324. reason_str = ERR_reason_error_string(packed_error);
  325. if (lib_str == NULL) {
  326. BIO_snprintf(lib_buf, sizeof(lib_buf), "lib(%u)", lib);
  327. lib_str = lib_buf;
  328. }
  329. if (func_str == NULL) {
  330. BIO_snprintf(func_buf, sizeof(func_buf), "func(%u)", func);
  331. func_str = func_buf;
  332. }
  333. if (reason_str == NULL) {
  334. BIO_snprintf(reason_buf, sizeof(reason_buf), "reason(%u)", reason);
  335. reason_str = reason_buf;
  336. }
  337. BIO_snprintf(buf, len, "error:%08" PRIx32 ":%s:%s:%s",
  338. packed_error, lib_str, func_str, reason_str);
  339. if (strlen(buf) == len - 1) {
  340. /* output may be truncated; make sure we always have 5 colon-separated
  341. * fields, i.e. 4 colons. */
  342. static const unsigned num_colons = 4;
  343. unsigned i;
  344. char *s = buf;
  345. if (len <= num_colons) {
  346. /* In this situation it's not possible to ensure that the correct number
  347. * of colons are included in the output. */
  348. return;
  349. }
  350. for (i = 0; i < num_colons; i++) {
  351. char *colon = strchr(s, ':');
  352. char *last_pos = &buf[len - 1] - num_colons + i;
  353. if (colon == NULL || colon > last_pos) {
  354. /* set colon |i| at last possible position (buf[len-1] is the
  355. * terminating 0). If we're setting this colon, then all whole of the
  356. * rest of the string must be colons in order to have the correct
  357. * number. */
  358. memset(last_pos, ':', num_colons - i);
  359. break;
  360. }
  361. s = colon + 1;
  362. }
  363. }
  364. }
  365. // err_string_cmp is a compare function for searching error values with
  366. // |bsearch| in |err_string_lookup|.
  367. static int err_string_cmp(const void *a, const void *b) {
  368. const uint32_t a_key = *((const uint32_t*) a) >> 15;
  369. const uint32_t b_key = *((const uint32_t*) b) >> 15;
  370. if (a_key < b_key) {
  371. return -1;
  372. } else if (a_key > b_key) {
  373. return 1;
  374. } else {
  375. return 0;
  376. }
  377. }
  378. /* err_string_lookup looks up the string associated with |lib| and |key| in
  379. * |values| and |string_data|. It returns the string or NULL if not found. */
  380. static const char *err_string_lookup(uint32_t lib, uint32_t key,
  381. const uint32_t *values,
  382. size_t num_values,
  383. const char *string_data) {
  384. /* |values| points to data in err_data.h, which is generated by
  385. * err_data_generate.go. It's an array of uint32_t values. Each value has the
  386. * following structure:
  387. * | lib | key | offset |
  388. * |6 bits| 11 bits | 15 bits |
  389. *
  390. * The |lib| value is a library identifier: one of the |ERR_LIB_*| values.
  391. * The |key| is either a function or a reason code, depending on the context.
  392. * The |offset| is the number of bytes from the start of |string_data| where
  393. * the (NUL terminated) string for this value can be found.
  394. *
  395. * Values are sorted based on treating the |lib| and |key| part as an
  396. * unsigned integer. */
  397. if (lib >= (1 << 6) || key >= (1 << 11)) {
  398. return NULL;
  399. }
  400. uint32_t search_key = lib << 26 | key << 15;
  401. const uint32_t *result = bsearch(&search_key, values, num_values,
  402. sizeof(uint32_t), err_string_cmp);
  403. if (result == NULL) {
  404. return NULL;
  405. }
  406. return &string_data[(*result) & 0x7fff];
  407. }
  408. static const char *const kLibraryNames[ERR_NUM_LIBS] = {
  409. "invalid library (0)",
  410. "unknown library", /* ERR_LIB_NONE */
  411. "system library", /* ERR_LIB_SYS */
  412. "bignum routines", /* ERR_LIB_BN */
  413. "RSA routines", /* ERR_LIB_RSA */
  414. "Diffie-Hellman routines", /* ERR_LIB_DH */
  415. "public key routines", /* ERR_LIB_EVP */
  416. "memory buffer routines", /* ERR_LIB_BUF */
  417. "object identifier routines", /* ERR_LIB_OBJ */
  418. "PEM routines", /* ERR_LIB_PEM */
  419. "DSA routines", /* ERR_LIB_DSA */
  420. "X.509 certificate routines", /* ERR_LIB_X509 */
  421. "ASN.1 encoding routines", /* ERR_LIB_ASN1 */
  422. "configuration file routines", /* ERR_LIB_CONF */
  423. "common libcrypto routines", /* ERR_LIB_CRYPTO */
  424. "elliptic curve routines", /* ERR_LIB_EC */
  425. "SSL routines", /* ERR_LIB_SSL */
  426. "BIO routines", /* ERR_LIB_BIO */
  427. "PKCS7 routines", /* ERR_LIB_PKCS7 */
  428. "PKCS8 routines", /* ERR_LIB_PKCS8 */
  429. "X509 V3 routines", /* ERR_LIB_X509V3 */
  430. "random number generator", /* ERR_LIB_RAND */
  431. "ENGINE routines", /* ERR_LIB_ENGINE */
  432. "OCSP routines", /* ERR_LIB_OCSP */
  433. "UI routines", /* ERR_LIB_UI */
  434. "COMP routines", /* ERR_LIB_COMP */
  435. "ECDSA routines", /* ERR_LIB_ECDSA */
  436. "ECDH routines", /* ERR_LIB_ECDH */
  437. "HMAC routines", /* ERR_LIB_HMAC */
  438. "Digest functions", /* ERR_LIB_DIGEST */
  439. "Cipher functions", /* ERR_LIB_CIPHER */
  440. "User defined functions", /* ERR_LIB_USER */
  441. "HKDF functions", /* ERR_LIB_HKDF */
  442. };
  443. const char *ERR_lib_error_string(uint32_t packed_error) {
  444. const uint32_t lib = ERR_GET_LIB(packed_error);
  445. if (lib >= ERR_NUM_LIBS) {
  446. return NULL;
  447. }
  448. return kLibraryNames[lib];
  449. }
  450. const char *ERR_func_error_string(uint32_t packed_error) {
  451. const uint32_t lib = ERR_GET_LIB(packed_error);
  452. const uint32_t func = ERR_GET_FUNC(packed_error);
  453. if (lib == ERR_LIB_SYS) {
  454. switch (func) {
  455. case SYS_F_fopen:
  456. return "fopen";
  457. case SYS_F_fclose:
  458. return "fclose";
  459. case SYS_F_fread:
  460. return "fread";
  461. case SYS_F_fwrite:
  462. return "fwrite";
  463. case SYS_F_socket:
  464. return "socket";
  465. case SYS_F_setsockopt:
  466. return "setsockopt";
  467. case SYS_F_connect:
  468. return "connect";
  469. case SYS_F_getaddrinfo:
  470. return "getaddrinfo";
  471. default:
  472. return NULL;
  473. }
  474. }
  475. return err_string_lookup(ERR_GET_LIB(packed_error),
  476. ERR_GET_FUNC(packed_error), kOpenSSLFunctionValues,
  477. kOpenSSLFunctionValuesLen,
  478. kOpenSSLFunctionStringData);
  479. }
  480. const char *ERR_reason_error_string(uint32_t packed_error) {
  481. const uint32_t lib = ERR_GET_LIB(packed_error);
  482. const uint32_t reason = ERR_GET_REASON(packed_error);
  483. if (lib == ERR_LIB_SYS) {
  484. if (reason < 127) {
  485. return strerror(reason);
  486. }
  487. return NULL;
  488. }
  489. if (reason < ERR_NUM_LIBS) {
  490. return kLibraryNames[reason];
  491. }
  492. if (reason < 100) {
  493. switch (reason) {
  494. case ERR_R_MALLOC_FAILURE:
  495. return "malloc failure";
  496. case ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED:
  497. return "function should not have been called";
  498. case ERR_R_PASSED_NULL_PARAMETER:
  499. return "passed a null parameter";
  500. case ERR_R_INTERNAL_ERROR:
  501. return "internal error";
  502. case ERR_R_OVERFLOW:
  503. return "overflow";
  504. default:
  505. return NULL;
  506. }
  507. }
  508. return err_string_lookup(lib, reason, kOpenSSLReasonValues,
  509. kOpenSSLReasonValuesLen, kOpenSSLReasonStringData);
  510. }
  511. void ERR_print_errors_cb(ERR_print_errors_callback_t callback, void *ctx) {
  512. char buf[ERR_ERROR_STRING_BUF_LEN];
  513. char buf2[1024];
  514. const char *file, *data;
  515. int line, flags;
  516. uint32_t packed_error;
  517. /* thread_hash is the least-significant bits of the |ERR_STATE| pointer value
  518. * for this thread. */
  519. const unsigned long thread_hash = (uintptr_t) err_get_state();
  520. for (;;) {
  521. packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
  522. if (packed_error == 0) {
  523. break;
  524. }
  525. ERR_error_string_n(packed_error, buf, sizeof(buf));
  526. BIO_snprintf(buf2, sizeof(buf2), "%lu:%s:%s:%d:%s\n", thread_hash, buf,
  527. file, line, (flags & ERR_FLAG_STRING) ? data : "");
  528. if (callback(buf2, strlen(buf2), ctx) <= 0) {
  529. break;
  530. }
  531. }
  532. }
  533. static int print_errors_to_file(const char* msg, size_t msg_len, void* ctx) {
  534. assert(msg[msg_len] == '\0');
  535. FILE* fp = ctx;
  536. int res = fputs(msg, fp);
  537. return res < 0 ? 0 : 1;
  538. }
  539. void ERR_print_errors_fp(FILE *file) {
  540. ERR_print_errors_cb(print_errors_to_file, file);
  541. }
  542. /* err_set_error_data sets the data on the most recent error. The |flags|
  543. * argument is a combination of the |ERR_FLAG_*| values. */
  544. static void err_set_error_data(char *data, int flags) {
  545. ERR_STATE *const state = err_get_state();
  546. struct err_error_st *error;
  547. if (state == NULL || state->top == state->bottom) {
  548. if (flags & ERR_FLAG_MALLOCED) {
  549. OPENSSL_free(data);
  550. }
  551. return;
  552. }
  553. error = &state->errors[state->top];
  554. err_clear_data(error);
  555. error->data = data;
  556. error->flags = flags;
  557. }
  558. void ERR_put_error(int library, int func, int reason, const char *file,
  559. unsigned line) {
  560. ERR_STATE *const state = err_get_state();
  561. struct err_error_st *error;
  562. if (state == NULL) {
  563. return;
  564. }
  565. if (library == ERR_LIB_SYS && reason == 0) {
  566. #if defined(WIN32)
  567. reason = GetLastError();
  568. #else
  569. reason = errno;
  570. #endif
  571. }
  572. state->top = (state->top + 1) % ERR_NUM_ERRORS;
  573. if (state->top == state->bottom) {
  574. state->bottom = (state->bottom + 1) % ERR_NUM_ERRORS;
  575. }
  576. error = &state->errors[state->top];
  577. err_clear(error);
  578. error->file = file;
  579. error->line = line;
  580. error->packed = ERR_PACK(library, func, reason);
  581. }
  582. /* ERR_add_error_data_vdata takes a variable number of const char* pointers,
  583. * concatenates them and sets the result as the data on the most recent
  584. * error. */
  585. static void err_add_error_vdata(unsigned num, va_list args) {
  586. size_t alloced, new_len, len = 0, substr_len;
  587. char *buf;
  588. const char *substr;
  589. unsigned i;
  590. alloced = 80;
  591. buf = OPENSSL_malloc(alloced + 1);
  592. if (buf == NULL) {
  593. return;
  594. }
  595. for (i = 0; i < num; i++) {
  596. substr = va_arg(args, const char *);
  597. if (substr == NULL) {
  598. continue;
  599. }
  600. substr_len = strlen(substr);
  601. new_len = len + substr_len;
  602. if (new_len > alloced) {
  603. char *new_buf;
  604. if (alloced + 20 + 1 < alloced) {
  605. /* overflow. */
  606. OPENSSL_free(buf);
  607. return;
  608. }
  609. alloced = new_len + 20;
  610. new_buf = OPENSSL_realloc(buf, alloced + 1);
  611. if (new_buf == NULL) {
  612. OPENSSL_free(buf);
  613. return;
  614. }
  615. buf = new_buf;
  616. }
  617. memcpy(buf + len, substr, substr_len);
  618. len = new_len;
  619. }
  620. buf[len] = 0;
  621. err_set_error_data(buf, ERR_FLAG_MALLOCED | ERR_FLAG_STRING);
  622. }
  623. void ERR_add_error_data(unsigned count, ...) {
  624. va_list args;
  625. va_start(args, count);
  626. err_add_error_vdata(count, args);
  627. va_end(args);
  628. }
  629. void ERR_add_error_dataf(const char *format, ...) {
  630. va_list ap;
  631. char *buf;
  632. static const unsigned buf_len = 256;
  633. /* A fixed-size buffer is used because va_copy (which would be needed in
  634. * order to call vsnprintf twice and measure the buffer) wasn't defined until
  635. * C99. */
  636. buf = OPENSSL_malloc(buf_len + 1);
  637. if (buf == NULL) {
  638. return;
  639. }
  640. va_start(ap, format);
  641. BIO_vsnprintf(buf, buf_len, format, ap);
  642. buf[buf_len] = 0;
  643. va_end(ap);
  644. err_set_error_data(buf, ERR_FLAG_MALLOCED | ERR_FLAG_STRING);
  645. }
  646. int ERR_set_mark(void) {
  647. ERR_STATE *const state = err_get_state();
  648. if (state == NULL || state->bottom == state->top) {
  649. return 0;
  650. }
  651. state->errors[state->top].flags |= ERR_FLAG_MARK;
  652. return 1;
  653. }
  654. int ERR_pop_to_mark(void) {
  655. ERR_STATE *const state = err_get_state();
  656. if (state == NULL) {
  657. return 0;
  658. }
  659. while (state->bottom != state->top) {
  660. struct err_error_st *error = &state->errors[state->top];
  661. if ((error->flags & ERR_FLAG_MARK) != 0) {
  662. error->flags &= ~ERR_FLAG_MARK;
  663. return 1;
  664. }
  665. err_clear(error);
  666. if (state->top == 0) {
  667. state->top = ERR_NUM_ERRORS - 1;
  668. } else {
  669. state->top--;
  670. }
  671. }
  672. return 0;
  673. }
  674. void ERR_load_crypto_strings(void) {}
  675. void ERR_free_strings(void) {}
  676. void ERR_load_BIO_strings(void) {}
  677. void ERR_load_ERR_strings(void) {}