Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

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