err.c.grpc_back 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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_grpc/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_grpc/mem.h>
  119. #include <openssl_grpc/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. // err_string_cmp is a compare function for searching error values with
  324. // |bsearch| in |err_string_lookup|.
  325. static int err_string_cmp(const void *a, const void *b) {
  326. const uint32_t a_key = *((const uint32_t*) a) >> 15;
  327. const uint32_t b_key = *((const uint32_t*) b) >> 15;
  328. if (a_key < b_key) {
  329. return -1;
  330. } else if (a_key > b_key) {
  331. return 1;
  332. } else {
  333. return 0;
  334. }
  335. }
  336. // err_string_lookup looks up the string associated with |lib| and |key| in
  337. // |values| and |string_data|. It returns the string or NULL if not found.
  338. static const char *err_string_lookup(uint32_t lib, uint32_t key,
  339. const uint32_t *values,
  340. size_t num_values,
  341. const char *string_data) {
  342. // |values| points to data in err_data.h, which is generated by
  343. // err_data_generate.go. It's an array of uint32_t values. Each value has the
  344. // following structure:
  345. // | lib | key | offset |
  346. // |6 bits| 11 bits | 15 bits |
  347. //
  348. // The |lib| value is a library identifier: one of the |ERR_LIB_*| values.
  349. // The |key| is a reason code, depending on the context.
  350. // The |offset| is the number of bytes from the start of |string_data| where
  351. // the (NUL terminated) string for this value can be found.
  352. //
  353. // Values are sorted based on treating the |lib| and |key| part as an
  354. // unsigned integer.
  355. if (lib >= (1 << 6) || key >= (1 << 11)) {
  356. return NULL;
  357. }
  358. uint32_t search_key = lib << 26 | key << 15;
  359. const uint32_t *result = bsearch(&search_key, values, num_values,
  360. sizeof(uint32_t), err_string_cmp);
  361. if (result == NULL) {
  362. return NULL;
  363. }
  364. return &string_data[(*result) & 0x7fff];
  365. }
  366. static const char *const kLibraryNames[ERR_NUM_LIBS] = {
  367. "invalid library (0)",
  368. "unknown library", // ERR_LIB_NONE
  369. "system library", // ERR_LIB_SYS
  370. "bignum routines", // ERR_LIB_BN
  371. "RSA routines", // ERR_LIB_RSA
  372. "Diffie-Hellman routines", // ERR_LIB_DH
  373. "public key routines", // ERR_LIB_EVP
  374. "memory buffer routines", // ERR_LIB_BUF
  375. "object identifier routines", // ERR_LIB_OBJ
  376. "PEM routines", // ERR_LIB_PEM
  377. "DSA routines", // ERR_LIB_DSA
  378. "X.509 certificate routines", // ERR_LIB_X509
  379. "ASN.1 encoding routines", // ERR_LIB_ASN1
  380. "configuration file routines", // ERR_LIB_CONF
  381. "common libcrypto routines", // ERR_LIB_CRYPTO
  382. "elliptic curve routines", // ERR_LIB_EC
  383. "SSL routines", // ERR_LIB_SSL
  384. "BIO routines", // ERR_LIB_BIO
  385. "PKCS7 routines", // ERR_LIB_PKCS7
  386. "PKCS8 routines", // ERR_LIB_PKCS8
  387. "X509 V3 routines", // ERR_LIB_X509V3
  388. "random number generator", // ERR_LIB_RAND
  389. "ENGINE routines", // ERR_LIB_ENGINE
  390. "OCSP routines", // ERR_LIB_OCSP
  391. "UI routines", // ERR_LIB_UI
  392. "COMP routines", // ERR_LIB_COMP
  393. "ECDSA routines", // ERR_LIB_ECDSA
  394. "ECDH routines", // ERR_LIB_ECDH
  395. "HMAC routines", // ERR_LIB_HMAC
  396. "Digest functions", // ERR_LIB_DIGEST
  397. "Cipher functions", // ERR_LIB_CIPHER
  398. "HKDF functions", // ERR_LIB_HKDF
  399. "Trust Token functions", // ERR_LIB_TRUST_TOKEN
  400. "User defined functions", // ERR_LIB_USER
  401. };
  402. static const char *err_lib_error_string(uint32_t packed_error) {
  403. const uint32_t lib = ERR_GET_LIB(packed_error);
  404. if (lib >= ERR_NUM_LIBS) {
  405. return NULL;
  406. }
  407. return kLibraryNames[lib];
  408. }
  409. const char *ERR_lib_error_string(uint32_t packed_error) {
  410. const char *ret = err_lib_error_string(packed_error);
  411. return ret == NULL ? "unknown library" : ret;
  412. }
  413. const char *ERR_func_error_string(uint32_t packed_error) {
  414. return "OPENSSL_internal";
  415. }
  416. static const char *err_reason_error_string(uint32_t packed_error) {
  417. const uint32_t lib = ERR_GET_LIB(packed_error);
  418. const uint32_t reason = ERR_GET_REASON(packed_error);
  419. if (lib == ERR_LIB_SYS) {
  420. if (reason < 127) {
  421. return strerror(reason);
  422. }
  423. return NULL;
  424. }
  425. if (reason < ERR_NUM_LIBS) {
  426. return kLibraryNames[reason];
  427. }
  428. if (reason < 100) {
  429. switch (reason) {
  430. case ERR_R_MALLOC_FAILURE:
  431. return "malloc failure";
  432. case ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED:
  433. return "function should not have been called";
  434. case ERR_R_PASSED_NULL_PARAMETER:
  435. return "passed a null parameter";
  436. case ERR_R_INTERNAL_ERROR:
  437. return "internal error";
  438. case ERR_R_OVERFLOW:
  439. return "overflow";
  440. default:
  441. return NULL;
  442. }
  443. }
  444. return err_string_lookup(lib, reason, kOpenSSLReasonValues,
  445. kOpenSSLReasonValuesLen, kOpenSSLReasonStringData);
  446. }
  447. const char *ERR_reason_error_string(uint32_t packed_error) {
  448. const char *ret = err_reason_error_string(packed_error);
  449. return ret == NULL ? "unknown error" : ret;
  450. }
  451. char *ERR_error_string(uint32_t packed_error, char *ret) {
  452. static char buf[ERR_ERROR_STRING_BUF_LEN];
  453. if (ret == NULL) {
  454. // TODO(fork): remove this.
  455. ret = buf;
  456. }
  457. #if !defined(NDEBUG)
  458. // This is aimed to help catch callers who don't provide
  459. // |ERR_ERROR_STRING_BUF_LEN| bytes of space.
  460. OPENSSL_memset(ret, 0, ERR_ERROR_STRING_BUF_LEN);
  461. #endif
  462. return ERR_error_string_n(packed_error, ret, ERR_ERROR_STRING_BUF_LEN);
  463. }
  464. char *ERR_error_string_n(uint32_t packed_error, char *buf, size_t len) {
  465. if (len == 0) {
  466. return NULL;
  467. }
  468. unsigned lib = ERR_GET_LIB(packed_error);
  469. unsigned reason = ERR_GET_REASON(packed_error);
  470. const char *lib_str = err_lib_error_string(packed_error);
  471. const char *reason_str = err_reason_error_string(packed_error);
  472. char lib_buf[64], reason_buf[64];
  473. if (lib_str == NULL) {
  474. BIO_snprintf(lib_buf, sizeof(lib_buf), "lib(%u)", lib);
  475. lib_str = lib_buf;
  476. }
  477. if (reason_str == NULL) {
  478. BIO_snprintf(reason_buf, sizeof(reason_buf), "reason(%u)", reason);
  479. reason_str = reason_buf;
  480. }
  481. BIO_snprintf(buf, len, "error:%08" PRIx32 ":%s:OPENSSL_internal:%s",
  482. packed_error, lib_str, reason_str);
  483. if (strlen(buf) == len - 1) {
  484. // output may be truncated; make sure we always have 5 colon-separated
  485. // fields, i.e. 4 colons.
  486. static const unsigned num_colons = 4;
  487. unsigned i;
  488. char *s = buf;
  489. if (len <= num_colons) {
  490. // In this situation it's not possible to ensure that the correct number
  491. // of colons are included in the output.
  492. return buf;
  493. }
  494. for (i = 0; i < num_colons; i++) {
  495. char *colon = strchr(s, ':');
  496. char *last_pos = &buf[len - 1] - num_colons + i;
  497. if (colon == NULL || colon > last_pos) {
  498. // set colon |i| at last possible position (buf[len-1] is the
  499. // terminating 0). If we're setting this colon, then all whole of the
  500. // rest of the string must be colons in order to have the correct
  501. // number.
  502. OPENSSL_memset(last_pos, ':', num_colons - i);
  503. break;
  504. }
  505. s = colon + 1;
  506. }
  507. }
  508. return buf;
  509. }
  510. void ERR_print_errors_cb(ERR_print_errors_callback_t callback, void *ctx) {
  511. char buf[ERR_ERROR_STRING_BUF_LEN];
  512. char buf2[1024];
  513. const char *file, *data;
  514. int line, flags;
  515. uint32_t packed_error;
  516. // thread_hash is the least-significant bits of the |ERR_STATE| pointer value
  517. // for this thread.
  518. const unsigned long thread_hash = (uintptr_t) err_get_state();
  519. for (;;) {
  520. packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
  521. if (packed_error == 0) {
  522. break;
  523. }
  524. ERR_error_string_n(packed_error, buf, sizeof(buf));
  525. BIO_snprintf(buf2, sizeof(buf2), "%lu:%s:%s:%d:%s\n", thread_hash, buf,
  526. file, line, (flags & ERR_FLAG_STRING) ? data : "");
  527. if (callback(buf2, strlen(buf2), ctx) <= 0) {
  528. break;
  529. }
  530. }
  531. }
  532. static int print_errors_to_file(const char* msg, size_t msg_len, void* ctx) {
  533. assert(msg[msg_len] == '\0');
  534. FILE* fp = ctx;
  535. int res = fputs(msg, fp);
  536. return res < 0 ? 0 : 1;
  537. }
  538. void ERR_print_errors_fp(FILE *file) {
  539. ERR_print_errors_cb(print_errors_to_file, file);
  540. }
  541. // err_set_error_data sets the data on the most recent error.
  542. static void err_set_error_data(char *data) {
  543. ERR_STATE *const state = err_get_state();
  544. struct err_error_st *error;
  545. if (state == NULL || state->top == state->bottom) {
  546. OPENSSL_free(data);
  547. return;
  548. }
  549. error = &state->errors[state->top];
  550. OPENSSL_free(error->data);
  551. error->data = data;
  552. }
  553. void ERR_put_error(int library, int unused, 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(OPENSSL_WINDOWS)
  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, 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. OPENSSL_memcpy(buf + len, substr, substr_len);
  613. len = new_len;
  614. }
  615. buf[len] = 0;
  616. err_set_error_data(buf);
  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);
  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].mark = 1;
  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->mark) {
  657. error->mark = 0;
  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. void ERR_load_BIO_strings(void) {}
  672. void ERR_load_ERR_strings(void) {}
  673. void ERR_load_RAND_strings(void) {}
  674. struct err_save_state_st {
  675. struct err_error_st *errors;
  676. size_t num_errors;
  677. };
  678. void ERR_SAVE_STATE_free(ERR_SAVE_STATE *state) {
  679. if (state == NULL) {
  680. return;
  681. }
  682. for (size_t i = 0; i < state->num_errors; i++) {
  683. err_clear(&state->errors[i]);
  684. }
  685. OPENSSL_free(state->errors);
  686. OPENSSL_free(state);
  687. }
  688. ERR_SAVE_STATE *ERR_save_state(void) {
  689. ERR_STATE *const state = err_get_state();
  690. if (state == NULL || state->top == state->bottom) {
  691. return NULL;
  692. }
  693. ERR_SAVE_STATE *ret = OPENSSL_malloc(sizeof(ERR_SAVE_STATE));
  694. if (ret == NULL) {
  695. return NULL;
  696. }
  697. // Errors are stored in the range (bottom, top].
  698. size_t num_errors = state->top >= state->bottom
  699. ? state->top - state->bottom
  700. : ERR_NUM_ERRORS + state->top - state->bottom;
  701. assert(num_errors < ERR_NUM_ERRORS);
  702. ret->errors = OPENSSL_malloc(num_errors * sizeof(struct err_error_st));
  703. if (ret->errors == NULL) {
  704. OPENSSL_free(ret);
  705. return NULL;
  706. }
  707. OPENSSL_memset(ret->errors, 0, num_errors * sizeof(struct err_error_st));
  708. ret->num_errors = num_errors;
  709. for (size_t i = 0; i < num_errors; i++) {
  710. size_t j = (state->bottom + i + 1) % ERR_NUM_ERRORS;
  711. err_copy(&ret->errors[i], &state->errors[j]);
  712. }
  713. return ret;
  714. }
  715. void ERR_restore_state(const ERR_SAVE_STATE *state) {
  716. if (state == NULL || state->num_errors == 0) {
  717. ERR_clear_error();
  718. return;
  719. }
  720. ERR_STATE *const dst = err_get_state();
  721. if (dst == NULL) {
  722. return;
  723. }
  724. for (size_t i = 0; i < state->num_errors; i++) {
  725. err_copy(&dst->errors[i], &state->errors[i]);
  726. }
  727. dst->top = state->num_errors - 1;
  728. dst->bottom = ERR_NUM_ERRORS - 1;
  729. }