ssl_err.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2020 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include "ssl_dbg.h"
  14. struct err_error_st {
  15. /* file contains the filename where the error occurred. */
  16. const char *file;
  17. /* packed contains the error library and reason, as packed by ERR_PACK. */
  18. uint32_t packed;
  19. /* line contains the line number where the error occurred. */
  20. uint32_t line;
  21. };
  22. #define ERR_NUM_ERRORS 4
  23. typedef struct err_state_st {
  24. /* errors contains the ERR_NUM_ERRORS most recent errors, organised as a ring
  25. * buffer. */
  26. struct err_error_st errors[ERR_NUM_ERRORS];
  27. /* top contains the index one past the most recent error. If |top| equals
  28. * |bottom| then the queue is empty. */
  29. unsigned top;
  30. /* bottom contains the index of the last error in the queue. */
  31. unsigned bottom;
  32. } ERR_STATE;
  33. #if CONFIG_OPENSSL_ERROR_STACK
  34. static ERR_STATE s_err_state = { 0 };
  35. #endif
  36. void ERR_clear_error(void)
  37. {
  38. #if CONFIG_OPENSSL_ERROR_STACK
  39. memset(&s_err_state.errors[0], 0, sizeof(struct err_state_st));
  40. s_err_state.top = s_err_state.bottom = 0;
  41. #endif
  42. }
  43. static uint32_t ERR_get_peek_error_internal(const char **file, int *line, bool peak)
  44. {
  45. #if CONFIG_OPENSSL_ERROR_STACK
  46. if (s_err_state.top == s_err_state.bottom) {
  47. return 0;
  48. }
  49. unsigned new_bottom = (s_err_state.bottom + 1) % ERR_NUM_ERRORS;
  50. int err = s_err_state.errors[new_bottom].packed;
  51. if (file) {
  52. *file = s_err_state.errors[new_bottom].file;
  53. }
  54. if (line) {
  55. *line = s_err_state.errors[new_bottom].line;
  56. }
  57. if (peak == false) {
  58. memset(&s_err_state.errors[new_bottom], 0, sizeof(struct err_error_st));
  59. s_err_state.bottom = new_bottom;
  60. }
  61. return err;
  62. #else
  63. return 0;
  64. #endif
  65. }
  66. uint32_t ERR_get_error(void)
  67. {
  68. return ERR_get_peek_error_internal(NULL, NULL, false);
  69. }
  70. uint32_t ERR_peek_error(void)
  71. {
  72. return ERR_get_peek_error_internal(NULL, NULL, true);
  73. }
  74. uint32_t ERR_peek_last_error(void)
  75. {
  76. return ERR_get_peek_error_internal(NULL, NULL, true);
  77. }
  78. uint32_t ERR_peek_error_line_data(const char **file, int *line, const char **data, int *flags)
  79. {
  80. return ERR_get_peek_error_internal(file, line, true);
  81. }
  82. uint32_t ERR_get_error_line_data(const char **file, int *line, const char **data, int *flags)
  83. {
  84. return ERR_get_peek_error_internal(file, line, false);
  85. }
  86. const char *ERR_reason_error_string(uint32_t packed_error)
  87. {
  88. return NULL;
  89. }
  90. void ERR_put_error(int library, int unused, int reason, const char *file, unsigned line)
  91. {
  92. #if CONFIG_OPENSSL_ERROR_STACK
  93. s_err_state.top = (s_err_state.top + 1) % ERR_NUM_ERRORS;
  94. if (s_err_state.top == s_err_state.bottom) {
  95. s_err_state.bottom = (s_err_state.bottom + 1) % ERR_NUM_ERRORS;
  96. }
  97. s_err_state.errors[s_err_state.top].packed = (uint32_t)library<<24 | abs(reason);
  98. s_err_state.errors[s_err_state.top].file = file;
  99. s_err_state.errors[s_err_state.top].line = line;
  100. #endif
  101. }