2
0

err.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /* crypto/err/err.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * openssl-core@openssl.org.
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * (eay@cryptsoft.com). This product includes software written by Tim
  108. * Hudson (tjh@cryptsoft.com).
  109. *
  110. */
  111. #include <stdio.h>
  112. #include <stdarg.h>
  113. #include <string.h>
  114. #include "cryptlib.h"
  115. #include <openssl/lhash.h>
  116. #include <openssl/crypto.h>
  117. #include <openssl/buffer.h>
  118. #include <openssl/bio.h>
  119. #include <openssl/err.h>
  120. static unsigned long get_error_values(int inc, int top,
  121. const char **file, int *line,
  122. const char **data, int *flags);
  123. #define err_clear_data(p,i) \
  124. do { \
  125. if (((p)->err_data[i] != NULL) && \
  126. (p)->err_data_flags[i] & ERR_TXT_MALLOCED) \
  127. { \
  128. OPENSSL_free((p)->err_data[i]); \
  129. (p)->err_data[i]=NULL; \
  130. } \
  131. (p)->err_data_flags[i]=0; \
  132. } while(0)
  133. #define err_clear(p,i) \
  134. do { \
  135. (p)->err_flags[i]=0; \
  136. (p)->err_buffer[i]=0; \
  137. err_clear_data(p,i); \
  138. (p)->err_file[i]=NULL; \
  139. (p)->err_line[i]= -1; \
  140. } while(0)
  141. void ERR_put_error(int lib, int func, int reason, const char *file, int line)
  142. {
  143. ERR_STATE *es;
  144. #ifdef _OSD_POSIX
  145. /*
  146. * In the BS2000-OSD POSIX subsystem, the compiler generates path names
  147. * in the form "*POSIX(/etc/passwd)". This dirty hack strips them to
  148. * something sensible. @@@ We shouldn't modify a const string, though.
  149. */
  150. if (strncmp(file, "*POSIX(", sizeof("*POSIX(") - 1) == 0) {
  151. char *end;
  152. /* Skip the "*POSIX(" prefix */
  153. file += sizeof("*POSIX(") - 1;
  154. end = &file[strlen(file) - 1];
  155. if (*end == ')')
  156. *end = '\0';
  157. /* Optional: use the basename of the path only. */
  158. if ((end = strrchr(file, '/')) != NULL)
  159. file = &end[1];
  160. }
  161. #endif
  162. es = ERR_get_state();
  163. es->top = (es->top + 1) % ERR_NUM_ERRORS;
  164. if (es->top == es->bottom)
  165. es->bottom = (es->bottom + 1) % ERR_NUM_ERRORS;
  166. es->err_flags[es->top] = 0;
  167. es->err_buffer[es->top] = ERR_PACK(lib, func, reason);
  168. es->err_file[es->top] = file;
  169. es->err_line[es->top] = line;
  170. err_clear_data(es, es->top);
  171. }
  172. void ERR_clear_error(void)
  173. {
  174. int i;
  175. ERR_STATE *es;
  176. es = ERR_get_state();
  177. for (i = 0; i < ERR_NUM_ERRORS; i++) {
  178. err_clear(es, i);
  179. }
  180. es->top = es->bottom = 0;
  181. }
  182. unsigned long ERR_get_error(void)
  183. {
  184. return (get_error_values(1, 0, NULL, NULL, NULL, NULL));
  185. }
  186. unsigned long ERR_get_error_line(const char **file, int *line)
  187. {
  188. return (get_error_values(1, 0, file, line, NULL, NULL));
  189. }
  190. unsigned long ERR_get_error_line_data(const char **file, int *line,
  191. const char **data, int *flags)
  192. {
  193. return (get_error_values(1, 0, file, line, data, flags));
  194. }
  195. unsigned long ERR_peek_error(void)
  196. {
  197. return (get_error_values(0, 0, NULL, NULL, NULL, NULL));
  198. }
  199. unsigned long ERR_peek_error_line(const char **file, int *line)
  200. {
  201. return (get_error_values(0, 0, file, line, NULL, NULL));
  202. }
  203. unsigned long ERR_peek_error_line_data(const char **file, int *line,
  204. const char **data, int *flags)
  205. {
  206. return (get_error_values(0, 0, file, line, data, flags));
  207. }
  208. unsigned long ERR_peek_last_error(void)
  209. {
  210. return (get_error_values(0, 1, NULL, NULL, NULL, NULL));
  211. }
  212. unsigned long ERR_peek_last_error_line(const char **file, int *line)
  213. {
  214. return (get_error_values(0, 1, file, line, NULL, NULL));
  215. }
  216. unsigned long ERR_peek_last_error_line_data(const char **file, int *line,
  217. const char **data, int *flags)
  218. {
  219. return (get_error_values(0, 1, file, line, data, flags));
  220. }
  221. static unsigned long get_error_values(int inc, int top, const char **file,
  222. int *line, const char **data,
  223. int *flags)
  224. {
  225. int i = 0;
  226. ERR_STATE *es;
  227. unsigned long ret;
  228. es = ERR_get_state();
  229. if (inc && top) {
  230. if (file)
  231. *file = "";
  232. if (line)
  233. *line = 0;
  234. if (data)
  235. *data = "";
  236. if (flags)
  237. *flags = 0;
  238. return ERR_R_INTERNAL_ERROR;
  239. }
  240. if (es->bottom == es->top)
  241. return 0;
  242. if (top)
  243. i = es->top; /* last error */
  244. else
  245. i = (es->bottom + 1) % ERR_NUM_ERRORS; /* first error */
  246. ret = es->err_buffer[i];
  247. if (inc) {
  248. es->bottom = i;
  249. es->err_buffer[i] = 0;
  250. }
  251. if ((file != NULL) && (line != NULL)) {
  252. if (es->err_file[i] == NULL) {
  253. *file = "NA";
  254. if (line != NULL)
  255. *line = 0;
  256. } else {
  257. *file = es->err_file[i];
  258. if (line != NULL)
  259. *line = es->err_line[i];
  260. }
  261. }
  262. if (data == NULL) {
  263. if (inc) {
  264. err_clear_data(es, i);
  265. }
  266. } else {
  267. if (es->err_data[i] == NULL) {
  268. *data = "";
  269. if (flags != NULL)
  270. *flags = 0;
  271. } else {
  272. *data = es->err_data[i];
  273. if (flags != NULL)
  274. *flags = es->err_data_flags[i];
  275. }
  276. }
  277. return ret;
  278. }
  279. void ERR_set_error_data(char *data, int flags)
  280. {
  281. ERR_STATE *es;
  282. int i;
  283. es = ERR_get_state();
  284. i = es->top;
  285. if (i == 0)
  286. i = ERR_NUM_ERRORS - 1;
  287. err_clear_data(es, i);
  288. es->err_data[i] = data;
  289. es->err_data_flags[i] = flags;
  290. }
  291. void ERR_add_error_data(int num, ...)
  292. {
  293. va_list args;
  294. int i, n, s;
  295. char *str, *p, *a;
  296. s = 80;
  297. str = OPENSSL_malloc(s + 1);
  298. if (str == NULL)
  299. return;
  300. str[0] = '\0';
  301. va_start(args, num);
  302. n = 0;
  303. for (i = 0; i < num; i++) {
  304. a = va_arg(args, char *);
  305. /* ignore NULLs, thanks to Bob Beck <beck@obtuse.com> */
  306. if (a != NULL) {
  307. n += strlen(a);
  308. if (n > s) {
  309. s = n + 20;
  310. p = OPENSSL_realloc(str, s + 1);
  311. if (p == NULL) {
  312. OPENSSL_free(str);
  313. goto err;
  314. } else
  315. str = p;
  316. }
  317. BUF_strlcat(str, a, (size_t)s + 1);
  318. }
  319. }
  320. ERR_set_error_data(str, ERR_TXT_MALLOCED | ERR_TXT_STRING);
  321. err:
  322. va_end(args);
  323. }
  324. int ERR_set_mark(void)
  325. {
  326. ERR_STATE *es;
  327. es = ERR_get_state();
  328. if (es->bottom == es->top)
  329. return 0;
  330. es->err_flags[es->top] |= ERR_FLAG_MARK;
  331. return 1;
  332. }
  333. int ERR_pop_to_mark(void)
  334. {
  335. ERR_STATE *es;
  336. es = ERR_get_state();
  337. while (es->bottom != es->top
  338. && (es->err_flags[es->top] & ERR_FLAG_MARK) == 0) {
  339. err_clear(es, es->top);
  340. es->top -= 1;
  341. if (es->top == -1)
  342. es->top = ERR_NUM_ERRORS - 1;
  343. }
  344. if (es->bottom == es->top)
  345. return 0;
  346. es->err_flags[es->top] &= ~ERR_FLAG_MARK;
  347. return 1;
  348. }
  349. #ifdef OPENSSL_FIPS
  350. static ERR_STATE *fget_state(void)
  351. {
  352. static ERR_STATE fstate;
  353. return &fstate;
  354. }
  355. ERR_STATE *(*get_state_func) (void) = fget_state;
  356. void (*remove_state_func) (unsigned long pid);
  357. ERR_STATE *ERR_get_state(void)
  358. {
  359. return get_state_func();
  360. }
  361. void int_ERR_set_state_func(ERR_STATE *(*get_func) (void),
  362. void (*remove_func) (unsigned long pid))
  363. {
  364. get_state_func = get_func;
  365. remove_state_func = remove_func;
  366. }
  367. void ERR_remove_state(unsigned long pid)
  368. {
  369. if (remove_state_func)
  370. remove_state_func(pid);
  371. }
  372. #endif