Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

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