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.
 
 
 
 
 
 

757 lines
23 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. 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. uint32_t ERR_peek_last_error(void) {
  254. return get_error_values(0 /* peek */, 1 /* top */, NULL, NULL, NULL, NULL);
  255. }
  256. uint32_t ERR_peek_last_error_line(const char **file, int *line) {
  257. return get_error_values(0 /* peek */, 1 /* top */, file, line, NULL, NULL);
  258. }
  259. uint32_t ERR_peek_last_error_line_data(const char **file, int *line,
  260. const char **data, int *flags) {
  261. return get_error_values(0 /* peek */, 1 /* top */, file, line, data, flags);
  262. }
  263. void ERR_clear_error(void) {
  264. ERR_STATE *const state = err_get_state();
  265. unsigned i;
  266. if (state == NULL) {
  267. return;
  268. }
  269. for (i = 0; i < ERR_NUM_ERRORS; i++) {
  270. err_clear(&state->errors[i]);
  271. }
  272. OPENSSL_free(state->to_free);
  273. state->to_free = NULL;
  274. state->top = state->bottom = 0;
  275. }
  276. void ERR_remove_thread_state(const CRYPTO_THREADID *tid) {
  277. if (tid != NULL) {
  278. assert(0);
  279. return;
  280. }
  281. ERR_clear_error();
  282. }
  283. int ERR_get_next_error_library(void) {
  284. int ret;
  285. CRYPTO_STATIC_MUTEX_lock_write(&global_next_library_mutex);
  286. ret = global_next_library++;
  287. CRYPTO_STATIC_MUTEX_unlock_write(&global_next_library_mutex);
  288. return ret;
  289. }
  290. void ERR_remove_state(unsigned long pid) {
  291. ERR_clear_error();
  292. }
  293. void ERR_clear_system_error(void) {
  294. errno = 0;
  295. }
  296. char *ERR_error_string(uint32_t packed_error, char *ret) {
  297. static char buf[ERR_ERROR_STRING_BUF_LEN];
  298. if (ret == NULL) {
  299. /* TODO(fork): remove this. */
  300. ret = buf;
  301. }
  302. #if !defined(NDEBUG)
  303. /* This is aimed to help catch callers who don't provide
  304. * |ERR_ERROR_STRING_BUF_LEN| bytes of space. */
  305. memset(ret, 0, ERR_ERROR_STRING_BUF_LEN);
  306. #endif
  307. ERR_error_string_n(packed_error, ret, ERR_ERROR_STRING_BUF_LEN);
  308. return ret;
  309. }
  310. void ERR_error_string_n(uint32_t packed_error, char *buf, size_t len) {
  311. char lib_buf[64], reason_buf[64];
  312. const char *lib_str, *reason_str;
  313. unsigned lib, reason;
  314. if (len == 0) {
  315. return;
  316. }
  317. lib = ERR_GET_LIB(packed_error);
  318. reason = ERR_GET_REASON(packed_error);
  319. lib_str = ERR_lib_error_string(packed_error);
  320. reason_str = ERR_reason_error_string(packed_error);
  321. if (lib_str == NULL) {
  322. BIO_snprintf(lib_buf, sizeof(lib_buf), "lib(%u)", lib);
  323. lib_str = lib_buf;
  324. }
  325. if (reason_str == NULL) {
  326. BIO_snprintf(reason_buf, sizeof(reason_buf), "reason(%u)", reason);
  327. reason_str = reason_buf;
  328. }
  329. BIO_snprintf(buf, len, "error:%08" PRIx32 ":%s:OPENSSL_internal:%s",
  330. packed_error, lib_str, reason_str);
  331. if (strlen(buf) == len - 1) {
  332. /* output may be truncated; make sure we always have 5 colon-separated
  333. * fields, i.e. 4 colons. */
  334. static const unsigned num_colons = 4;
  335. unsigned i;
  336. char *s = buf;
  337. if (len <= num_colons) {
  338. /* In this situation it's not possible to ensure that the correct number
  339. * of colons are included in the output. */
  340. return;
  341. }
  342. for (i = 0; i < num_colons; i++) {
  343. char *colon = strchr(s, ':');
  344. char *last_pos = &buf[len - 1] - num_colons + i;
  345. if (colon == NULL || colon > last_pos) {
  346. /* set colon |i| at last possible position (buf[len-1] is the
  347. * terminating 0). If we're setting this colon, then all whole of the
  348. * rest of the string must be colons in order to have the correct
  349. * number. */
  350. memset(last_pos, ':', num_colons - i);
  351. break;
  352. }
  353. s = colon + 1;
  354. }
  355. }
  356. }
  357. // err_string_cmp is a compare function for searching error values with
  358. // |bsearch| in |err_string_lookup|.
  359. static int err_string_cmp(const void *a, const void *b) {
  360. const uint32_t a_key = *((const uint32_t*) a) >> 15;
  361. const uint32_t b_key = *((const uint32_t*) b) >> 15;
  362. if (a_key < b_key) {
  363. return -1;
  364. } else if (a_key > b_key) {
  365. return 1;
  366. } else {
  367. return 0;
  368. }
  369. }
  370. /* err_string_lookup looks up the string associated with |lib| and |key| in
  371. * |values| and |string_data|. It returns the string or NULL if not found. */
  372. static const char *err_string_lookup(uint32_t lib, uint32_t key,
  373. const uint32_t *values,
  374. size_t num_values,
  375. const char *string_data) {
  376. /* |values| points to data in err_data.h, which is generated by
  377. * err_data_generate.go. It's an array of uint32_t values. Each value has the
  378. * following structure:
  379. * | lib | key | offset |
  380. * |6 bits| 11 bits | 15 bits |
  381. *
  382. * The |lib| value is a library identifier: one of the |ERR_LIB_*| values.
  383. * The |key| is a reason code, depending on the context.
  384. * The |offset| is the number of bytes from the start of |string_data| where
  385. * the (NUL terminated) string for this value can be found.
  386. *
  387. * Values are sorted based on treating the |lib| and |key| part as an
  388. * unsigned integer. */
  389. if (lib >= (1 << 6) || key >= (1 << 11)) {
  390. return NULL;
  391. }
  392. uint32_t search_key = lib << 26 | key << 15;
  393. const uint32_t *result = bsearch(&search_key, values, num_values,
  394. sizeof(uint32_t), err_string_cmp);
  395. if (result == NULL) {
  396. return NULL;
  397. }
  398. return &string_data[(*result) & 0x7fff];
  399. }
  400. static const char *const kLibraryNames[ERR_NUM_LIBS] = {
  401. "invalid library (0)",
  402. "unknown library", /* ERR_LIB_NONE */
  403. "system library", /* ERR_LIB_SYS */
  404. "bignum routines", /* ERR_LIB_BN */
  405. "RSA routines", /* ERR_LIB_RSA */
  406. "Diffie-Hellman routines", /* ERR_LIB_DH */
  407. "public key routines", /* ERR_LIB_EVP */
  408. "memory buffer routines", /* ERR_LIB_BUF */
  409. "object identifier routines", /* ERR_LIB_OBJ */
  410. "PEM routines", /* ERR_LIB_PEM */
  411. "DSA routines", /* ERR_LIB_DSA */
  412. "X.509 certificate routines", /* ERR_LIB_X509 */
  413. "ASN.1 encoding routines", /* ERR_LIB_ASN1 */
  414. "configuration file routines", /* ERR_LIB_CONF */
  415. "common libcrypto routines", /* ERR_LIB_CRYPTO */
  416. "elliptic curve routines", /* ERR_LIB_EC */
  417. "SSL routines", /* ERR_LIB_SSL */
  418. "BIO routines", /* ERR_LIB_BIO */
  419. "PKCS7 routines", /* ERR_LIB_PKCS7 */
  420. "PKCS8 routines", /* ERR_LIB_PKCS8 */
  421. "X509 V3 routines", /* ERR_LIB_X509V3 */
  422. "random number generator", /* ERR_LIB_RAND */
  423. "ENGINE routines", /* ERR_LIB_ENGINE */
  424. "OCSP routines", /* ERR_LIB_OCSP */
  425. "UI routines", /* ERR_LIB_UI */
  426. "COMP routines", /* ERR_LIB_COMP */
  427. "ECDSA routines", /* ERR_LIB_ECDSA */
  428. "ECDH routines", /* ERR_LIB_ECDH */
  429. "HMAC routines", /* ERR_LIB_HMAC */
  430. "Digest functions", /* ERR_LIB_DIGEST */
  431. "Cipher functions", /* ERR_LIB_CIPHER */
  432. "HKDF functions", /* ERR_LIB_HKDF */
  433. "User defined functions", /* ERR_LIB_USER */
  434. };
  435. const char *ERR_lib_error_string(uint32_t packed_error) {
  436. const uint32_t lib = ERR_GET_LIB(packed_error);
  437. if (lib >= ERR_NUM_LIBS) {
  438. return NULL;
  439. }
  440. return kLibraryNames[lib];
  441. }
  442. const char *ERR_func_error_string(uint32_t packed_error) {
  443. return "OPENSSL_internal";
  444. }
  445. const char *ERR_reason_error_string(uint32_t packed_error) {
  446. const uint32_t lib = ERR_GET_LIB(packed_error);
  447. const uint32_t reason = ERR_GET_REASON(packed_error);
  448. if (lib == ERR_LIB_SYS) {
  449. if (reason < 127) {
  450. return strerror(reason);
  451. }
  452. return NULL;
  453. }
  454. if (reason < ERR_NUM_LIBS) {
  455. return kLibraryNames[reason];
  456. }
  457. if (reason < 100) {
  458. switch (reason) {
  459. case ERR_R_MALLOC_FAILURE:
  460. return "malloc failure";
  461. case ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED:
  462. return "function should not have been called";
  463. case ERR_R_PASSED_NULL_PARAMETER:
  464. return "passed a null parameter";
  465. case ERR_R_INTERNAL_ERROR:
  466. return "internal error";
  467. case ERR_R_OVERFLOW:
  468. return "overflow";
  469. default:
  470. return NULL;
  471. }
  472. }
  473. return err_string_lookup(lib, reason, kOpenSSLReasonValues,
  474. kOpenSSLReasonValuesLen, kOpenSSLReasonStringData);
  475. }
  476. void ERR_print_errors_cb(ERR_print_errors_callback_t callback, void *ctx) {
  477. char buf[ERR_ERROR_STRING_BUF_LEN];
  478. char buf2[1024];
  479. const char *file, *data;
  480. int line, flags;
  481. uint32_t packed_error;
  482. /* thread_hash is the least-significant bits of the |ERR_STATE| pointer value
  483. * for this thread. */
  484. const unsigned long thread_hash = (uintptr_t) err_get_state();
  485. for (;;) {
  486. packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
  487. if (packed_error == 0) {
  488. break;
  489. }
  490. ERR_error_string_n(packed_error, buf, sizeof(buf));
  491. BIO_snprintf(buf2, sizeof(buf2), "%lu:%s:%s:%d:%s\n", thread_hash, buf,
  492. file, line, (flags & ERR_FLAG_STRING) ? data : "");
  493. if (callback(buf2, strlen(buf2), ctx) <= 0) {
  494. break;
  495. }
  496. }
  497. }
  498. static int print_errors_to_file(const char* msg, size_t msg_len, void* ctx) {
  499. assert(msg[msg_len] == '\0');
  500. FILE* fp = ctx;
  501. int res = fputs(msg, fp);
  502. return res < 0 ? 0 : 1;
  503. }
  504. void ERR_print_errors_fp(FILE *file) {
  505. ERR_print_errors_cb(print_errors_to_file, file);
  506. }
  507. /* err_set_error_data sets the data on the most recent error. The |flags|
  508. * argument is a combination of the |ERR_FLAG_*| values. */
  509. static void err_set_error_data(char *data, int flags) {
  510. ERR_STATE *const state = err_get_state();
  511. struct err_error_st *error;
  512. if (state == NULL || state->top == state->bottom) {
  513. if (flags & ERR_FLAG_MALLOCED) {
  514. OPENSSL_free(data);
  515. }
  516. return;
  517. }
  518. error = &state->errors[state->top];
  519. err_clear_data(error);
  520. error->data = data;
  521. error->flags = flags;
  522. }
  523. void ERR_put_error(int library, int unused, int reason, const char *file,
  524. unsigned line) {
  525. ERR_STATE *const state = err_get_state();
  526. struct err_error_st *error;
  527. if (state == NULL) {
  528. return;
  529. }
  530. if (library == ERR_LIB_SYS && reason == 0) {
  531. #if defined(OPENSSL_WINDOWS)
  532. reason = GetLastError();
  533. #else
  534. reason = errno;
  535. #endif
  536. }
  537. state->top = (state->top + 1) % ERR_NUM_ERRORS;
  538. if (state->top == state->bottom) {
  539. state->bottom = (state->bottom + 1) % ERR_NUM_ERRORS;
  540. }
  541. error = &state->errors[state->top];
  542. err_clear(error);
  543. error->file = file;
  544. error->line = line;
  545. error->packed = ERR_PACK(library, reason);
  546. }
  547. /* ERR_add_error_data_vdata takes a variable number of const char* pointers,
  548. * concatenates them and sets the result as the data on the most recent
  549. * error. */
  550. static void err_add_error_vdata(unsigned num, va_list args) {
  551. size_t alloced, new_len, len = 0, substr_len;
  552. char *buf;
  553. const char *substr;
  554. unsigned i;
  555. alloced = 80;
  556. buf = OPENSSL_malloc(alloced + 1);
  557. if (buf == NULL) {
  558. return;
  559. }
  560. for (i = 0; i < num; i++) {
  561. substr = va_arg(args, const char *);
  562. if (substr == NULL) {
  563. continue;
  564. }
  565. substr_len = strlen(substr);
  566. new_len = len + substr_len;
  567. if (new_len > alloced) {
  568. char *new_buf;
  569. if (alloced + 20 + 1 < alloced) {
  570. /* overflow. */
  571. OPENSSL_free(buf);
  572. return;
  573. }
  574. alloced = new_len + 20;
  575. new_buf = OPENSSL_realloc(buf, alloced + 1);
  576. if (new_buf == NULL) {
  577. OPENSSL_free(buf);
  578. return;
  579. }
  580. buf = new_buf;
  581. }
  582. memcpy(buf + len, substr, substr_len);
  583. len = new_len;
  584. }
  585. buf[len] = 0;
  586. err_set_error_data(buf, ERR_FLAG_MALLOCED | ERR_FLAG_STRING);
  587. }
  588. void ERR_add_error_data(unsigned count, ...) {
  589. va_list args;
  590. va_start(args, count);
  591. err_add_error_vdata(count, args);
  592. va_end(args);
  593. }
  594. void ERR_add_error_dataf(const char *format, ...) {
  595. va_list ap;
  596. char *buf;
  597. static const unsigned buf_len = 256;
  598. /* A fixed-size buffer is used because va_copy (which would be needed in
  599. * order to call vsnprintf twice and measure the buffer) wasn't defined until
  600. * C99. */
  601. buf = OPENSSL_malloc(buf_len + 1);
  602. if (buf == NULL) {
  603. return;
  604. }
  605. va_start(ap, format);
  606. BIO_vsnprintf(buf, buf_len, format, ap);
  607. buf[buf_len] = 0;
  608. va_end(ap);
  609. err_set_error_data(buf, ERR_FLAG_MALLOCED | ERR_FLAG_STRING);
  610. }
  611. int ERR_set_mark(void) {
  612. ERR_STATE *const state = err_get_state();
  613. if (state == NULL || state->bottom == state->top) {
  614. return 0;
  615. }
  616. state->errors[state->top].flags |= ERR_FLAG_MARK;
  617. return 1;
  618. }
  619. int ERR_pop_to_mark(void) {
  620. ERR_STATE *const state = err_get_state();
  621. if (state == NULL) {
  622. return 0;
  623. }
  624. while (state->bottom != state->top) {
  625. struct err_error_st *error = &state->errors[state->top];
  626. if ((error->flags & ERR_FLAG_MARK) != 0) {
  627. error->flags &= ~ERR_FLAG_MARK;
  628. return 1;
  629. }
  630. err_clear(error);
  631. if (state->top == 0) {
  632. state->top = ERR_NUM_ERRORS - 1;
  633. } else {
  634. state->top--;
  635. }
  636. }
  637. return 0;
  638. }
  639. void ERR_load_crypto_strings(void) {}
  640. void ERR_free_strings(void) {}
  641. void ERR_load_BIO_strings(void) {}
  642. void ERR_load_ERR_strings(void) {}