ssl_dbg.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright 2015-2016 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. #ifndef _SSL_DEBUG_H_
  14. #define _SSL_DEBUG_H_
  15. #include "platform/ssl_opt.h"
  16. #include "platform/ssl_port.h"
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. #ifdef CONFIG_OPENSSL_DEBUG_LEVEL
  21. #define SSL_DEBUG_LEVEL CONFIG_OPENSSL_DEBUG_LEVEL
  22. #else
  23. #define SSL_DEBUG_LEVEL 0
  24. #endif
  25. #define SSL_DEBUG_ON (SSL_DEBUG_LEVEL + 1)
  26. #define SSL_DEBUG_OFF (SSL_DEBUG_LEVEL - 1)
  27. #ifdef CONFIG_OPENSSL_DEBUG
  28. #ifndef SSL_DEBUG_LOG
  29. #error "SSL_DEBUG_LOG is not defined"
  30. #endif
  31. #ifndef SSL_DEBUG_FL
  32. #define SSL_DEBUG_FL "\n"
  33. #endif
  34. #define SSL_SHOW_LOCATION() \
  35. SSL_DEBUG_LOG("SSL assert : %s %d\n", \
  36. __FILE__, __LINE__)
  37. #define SSL_DEBUG(level, fmt, ...) \
  38. { \
  39. if (level > SSL_DEBUG_LEVEL) { \
  40. SSL_DEBUG_LOG(fmt SSL_DEBUG_FL, ##__VA_ARGS__); \
  41. } \
  42. }
  43. #else /* CONFIG_OPENSSL_DEBUG */
  44. #define SSL_SHOW_LOCATION()
  45. #define SSL_DEBUG(level, fmt, ...)
  46. #endif /* CONFIG_OPENSSL_DEBUG */
  47. /**
  48. * OpenSSL assert function
  49. *
  50. * if select "CONFIG_OPENSSL_ASSERT_DEBUG", SSL_ASSERT* will show error file name and line
  51. * if select "CONFIG_OPENSSL_ASSERT_EXIT", SSL_ASSERT* will just return error code.
  52. * if select "CONFIG_OPENSSL_ASSERT_DEBUG_EXIT" SSL_ASSERT* will show error file name and line,
  53. * then return error code.
  54. * if select "CONFIG_OPENSSL_ASSERT_DEBUG_BLOCK", SSL_ASSERT* will show error file name and line,
  55. * then block here with "while (1)"
  56. *
  57. * SSL_ASSERT1 may will return "-1", so function's return argument is integer.
  58. * SSL_ASSERT2 may will return "NULL", so function's return argument is a point.
  59. * SSL_ASSERT2 may will return nothing, so function's return argument is "void".
  60. */
  61. #if defined(CONFIG_OPENSSL_ASSERT_DEBUG)
  62. #define SSL_ASSERT1(s) \
  63. { \
  64. if (!(s)) { \
  65. SSL_SHOW_LOCATION(); \
  66. } \
  67. }
  68. #define SSL_ASSERT2(s) \
  69. { \
  70. if (!(s)) { \
  71. SSL_SHOW_LOCATION(); \
  72. } \
  73. }
  74. #define SSL_ASSERT3(s) \
  75. { \
  76. if (!(s)) { \
  77. SSL_SHOW_LOCATION(); \
  78. } \
  79. }
  80. #elif defined(CONFIG_OPENSSL_ASSERT_EXIT)
  81. #define SSL_ASSERT1(s) \
  82. { \
  83. if (!(s)) { \
  84. return -1; \
  85. } \
  86. }
  87. #define SSL_ASSERT2(s) \
  88. { \
  89. if (!(s)) { \
  90. return NULL; \
  91. } \
  92. }
  93. #define SSL_ASSERT3(s) \
  94. { \
  95. if (!(s)) { \
  96. return ; \
  97. } \
  98. }
  99. #elif defined(CONFIG_OPENSSL_ASSERT_DEBUG_EXIT)
  100. #define SSL_ASSERT1(s) \
  101. { \
  102. if (!(s)) { \
  103. SSL_SHOW_LOCATION(); \
  104. return -1; \
  105. } \
  106. }
  107. #define SSL_ASSERT2(s) \
  108. { \
  109. if (!(s)) { \
  110. SSL_SHOW_LOCATION(); \
  111. return NULL; \
  112. } \
  113. }
  114. #define SSL_ASSERT3(s) \
  115. { \
  116. if (!(s)) { \
  117. SSL_SHOW_LOCATION(); \
  118. return ; \
  119. } \
  120. }
  121. #elif defined(CONFIG_OPENSSL_ASSERT_DEBUG_BLOCK)
  122. #define SSL_ASSERT1(s) \
  123. { \
  124. if (!(s)) { \
  125. SSL_SHOW_LOCATION(); \
  126. while (1); \
  127. } \
  128. }
  129. #define SSL_ASSERT2(s) \
  130. { \
  131. if (!(s)) { \
  132. SSL_SHOW_LOCATION(); \
  133. while (1); \
  134. } \
  135. }
  136. #define SSL_ASSERT3(s) \
  137. { \
  138. if (!(s)) { \
  139. SSL_SHOW_LOCATION(); \
  140. while (1); \
  141. } \
  142. }
  143. #else
  144. #define SSL_ASSERT1(s)
  145. #define SSL_ASSERT2(s)
  146. #define SSL_ASSERT3(s)
  147. #endif
  148. #define SSL_PLATFORM_DEBUG_LEVEL SSL_DEBUG_OFF
  149. #define SSL_PLATFORM_ERROR_LEVEL SSL_DEBUG_ON
  150. #define SSL_CERT_DEBUG_LEVEL SSL_DEBUG_OFF
  151. #define SSL_CERT_ERROR_LEVEL SSL_DEBUG_ON
  152. #define SSL_PKEY_DEBUG_LEVEL SSL_DEBUG_OFF
  153. #define SSL_PKEY_ERROR_LEVEL SSL_DEBUG_ON
  154. #define SSL_X509_DEBUG_LEVEL SSL_DEBUG_OFF
  155. #define SSL_X509_ERROR_LEVEL SSL_DEBUG_ON
  156. #define SSL_LIB_DEBUG_LEVEL SSL_DEBUG_OFF
  157. #define SSL_LIB_ERROR_LEVEL SSL_DEBUG_ON
  158. #define SSL_STACK_DEBUG_LEVEL SSL_DEBUG_OFF
  159. #define SSL_STACK_ERROR_LEVEL SSL_DEBUG_ON
  160. #ifdef __cplusplus
  161. }
  162. #endif
  163. #endif