debug.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  3. */
  4. #if !defined(MBEDTLS_CONFIG_FILE)
  5. #include "mbedtls/config.h"
  6. #else
  7. #include MBEDTLS_CONFIG_FILE
  8. #endif
  9. #if defined(MBEDTLS_DEBUG_C)
  10. #if defined(MBEDTLS_PLATFORM_C)
  11. #include "mbedtls/platform.h"
  12. #else
  13. #include <stdlib.h>
  14. #define mbedtls_calloc calloc
  15. #define mbedtls_free free
  16. #define mbedtls_time_t time_t
  17. #define mbedtls_snprintf snprintf
  18. #endif
  19. #include "mbedtls/debug.h"
  20. #include <stdarg.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
  24. !defined(inline) && !defined(__cplusplus)
  25. #define inline __inline
  26. #endif
  27. #define DEBUG_BUF_SIZE 512
  28. static int debug_threshold = 0;
  29. void mbedtls_debug_set_threshold(int threshold)
  30. {
  31. debug_threshold = threshold;
  32. }
  33. /*
  34. * All calls to f_dbg must be made via this function
  35. */
  36. static inline void debug_send_line(const mbedtls_ssl_context *ssl, int level,
  37. const char *file, int line,
  38. const char *str)
  39. {
  40. /*
  41. * If in a threaded environment, we need a thread identifier.
  42. * Since there is no portable way to get one, use the address of the ssl
  43. * context instead, as it shouldn't be shared between threads.
  44. */
  45. #if defined(MBEDTLS_THREADING_C)
  46. char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */
  47. mbedtls_snprintf(idstr, sizeof(idstr), "%p: %s", (void *)ssl, str);
  48. ssl->conf->f_dbg(ssl->conf->p_dbg, level, file, line, idstr);
  49. #else
  50. ssl->conf->f_dbg(ssl->conf->p_dbg, level, file, line, str);
  51. #endif
  52. }
  53. void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level,
  54. const char *file, int line,
  55. const char *format, ...)
  56. {
  57. va_list argp;
  58. char str[DEBUG_BUF_SIZE];
  59. int ret;
  60. if (NULL == ssl || NULL == ssl->conf || NULL == ssl->conf->f_dbg || level > debug_threshold) {
  61. return;
  62. }
  63. va_start(argp, format);
  64. #if defined(_WIN32)
  65. #if defined(_TRUNCATE)
  66. ret = _vsnprintf_s(str, DEBUG_BUF_SIZE, _TRUNCATE, format, argp);
  67. #else
  68. ret = _vsnprintf(str, DEBUG_BUF_SIZE, format, argp);
  69. if (ret < 0 || (size_t) ret == DEBUG_BUF_SIZE) {
  70. str[DEBUG_BUF_SIZE - 1] = '\0';
  71. ret = -1;
  72. }
  73. #endif
  74. #else
  75. ret = vsnprintf(str, DEBUG_BUF_SIZE, format, argp);
  76. #endif
  77. va_end(argp);
  78. if (ret >= 0 && ret < DEBUG_BUF_SIZE - 1) {
  79. //str[ret] = '\n';
  80. str[ret] = '\0';
  81. }
  82. debug_send_line(ssl, level, file, line, str);
  83. }
  84. void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level,
  85. const char *file, int line,
  86. const char *text, int ret)
  87. {
  88. char str[DEBUG_BUF_SIZE];
  89. if (ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold) {
  90. return;
  91. }
  92. /*
  93. * With non-blocking I/O and examples that just retry immediately,
  94. * the logs would be quickly flooded with WANT_READ, so ignore that.
  95. * Don't ignore WANT_WRITE however, since is is usually rare.
  96. */
  97. if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
  98. return;
  99. }
  100. mbedtls_snprintf(str, sizeof(str), "%s() returned %d (-0x%04x)",
  101. text, ret, -ret);
  102. debug_send_line(ssl, level, file, line, str);
  103. }
  104. void mbedtls_debug_print_buf(const mbedtls_ssl_context *ssl, int level,
  105. const char *file, int line, const char *text,
  106. const unsigned char *buf, size_t len)
  107. {
  108. char str[DEBUG_BUF_SIZE];
  109. char txt[17];
  110. size_t i, idx = 0;
  111. if (ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold) {
  112. return;
  113. }
  114. mbedtls_snprintf(str + idx, sizeof(str) - idx, "dumping '%s' (%u bytes)",
  115. text, (unsigned int) len);
  116. debug_send_line(ssl, level, file, line, str);
  117. idx = 0;
  118. memset(txt, 0, sizeof(txt));
  119. for (i = 0; i < len; i++) {
  120. if (i >= 4096) {
  121. break;
  122. }
  123. if (i % 16 == 0) {
  124. if (i > 0) {
  125. mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s", txt);
  126. debug_send_line(ssl, level, file, line, str);
  127. idx = 0;
  128. memset(txt, 0, sizeof(txt));
  129. }
  130. idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, "%04x: ",
  131. (unsigned int) i);
  132. }
  133. idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x",
  134. (unsigned int) buf[i]);
  135. txt[i % 16] = (buf[i] > 31 && buf[i] < 127) ? buf[i] : '.' ;
  136. }
  137. if (len > 0) {
  138. for (/* i = i */; i % 16 != 0; i++) {
  139. idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " ");
  140. }
  141. mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s", txt);
  142. debug_send_line(ssl, level, file, line, str);
  143. }
  144. }
  145. #if defined(MBEDTLS_ECP_C)
  146. void mbedtls_debug_print_ecp(const mbedtls_ssl_context *ssl, int level,
  147. const char *file, int line,
  148. const char *text, const mbedtls_ecp_point *X)
  149. {
  150. char str[DEBUG_BUF_SIZE];
  151. if (ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold) {
  152. return;
  153. }
  154. mbedtls_snprintf(str, sizeof(str), "%s(X)", text);
  155. mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->X);
  156. mbedtls_snprintf(str, sizeof(str), "%s(Y)", text);
  157. mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->Y);
  158. }
  159. #endif /* MBEDTLS_ECP_C */
  160. #if defined(MBEDTLS_BIGNUM_C)
  161. void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl, int level,
  162. const char *file, int line,
  163. const char *text, const mbedtls_mpi *X)
  164. {
  165. char str[DEBUG_BUF_SIZE];
  166. int j, k, zeros = 1;
  167. size_t i, n, idx = 0;
  168. if (ssl->conf == NULL || ssl->conf->f_dbg == NULL || X == NULL || level > debug_threshold) {
  169. return;
  170. }
  171. for (n = X->n - 1; n > 0; n--)
  172. if (X->p[n] != 0) {
  173. break;
  174. }
  175. for (j = (sizeof(mbedtls_mpi_uint) << 3) - 1; j >= 0; j--)
  176. if (((X->p[n] >> j) & 1) != 0) {
  177. break;
  178. }
  179. mbedtls_snprintf(str + idx, sizeof(str) - idx, "value of '%s' (%d bits) is:\n",
  180. text, (int)((n * (sizeof(mbedtls_mpi_uint) << 3)) + j + 1));
  181. debug_send_line(ssl, level, file, line, str);
  182. idx = 0;
  183. for (i = n + 1, j = 0; i > 0; i--) {
  184. if (zeros && X->p[i - 1] == 0) {
  185. continue;
  186. }
  187. for (k = sizeof(mbedtls_mpi_uint) - 1; k >= 0; k--) {
  188. if (zeros && ((X->p[i - 1] >> (k << 3)) & 0xFF) == 0) {
  189. continue;
  190. } else {
  191. zeros = 0;
  192. }
  193. if (j % 16 == 0) {
  194. if (j > 0) {
  195. mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
  196. debug_send_line(ssl, level, file, line, str);
  197. idx = 0;
  198. }
  199. }
  200. idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x", (unsigned int)
  201. (X->p[i - 1] >> (k << 3)) & 0xFF);
  202. j++;
  203. }
  204. }
  205. if (zeros == 1) {
  206. idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " 00");
  207. }
  208. mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
  209. debug_send_line(ssl, level, file, line, str);
  210. }
  211. #endif /* MBEDTLS_BIGNUM_C */
  212. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  213. static void debug_print_pk(const mbedtls_ssl_context *ssl, int level,
  214. const char *file, int line,
  215. const char *text, const mbedtls_pk_context *pk)
  216. {
  217. size_t i;
  218. mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
  219. char name[16];
  220. memset(items, 0, sizeof(items));
  221. if (mbedtls_pk_debug(pk, items) != 0) {
  222. debug_send_line(ssl, level, file, line,
  223. "invalid PK context\n");
  224. return;
  225. }
  226. for (i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++) {
  227. if (items[i].type == MBEDTLS_PK_DEBUG_NONE) {
  228. return;
  229. }
  230. mbedtls_snprintf(name, sizeof(name), "%s%s", text, items[i].name);
  231. name[sizeof(name) - 1] = '\0';
  232. if (items[i].type == MBEDTLS_PK_DEBUG_MPI) {
  233. mbedtls_debug_print_mpi(ssl, level, file, line, name, items[i].value);
  234. } else
  235. #if defined(MBEDTLS_ECP_C)
  236. if (items[i].type == MBEDTLS_PK_DEBUG_ECP) {
  237. mbedtls_debug_print_ecp(ssl, level, file, line, name, items[i].value);
  238. } else
  239. #endif
  240. debug_send_line(ssl, level, file, line,
  241. "should not happen\n");
  242. }
  243. }
  244. static void debug_print_line_by_line(const mbedtls_ssl_context *ssl, int level,
  245. const char *file, int line, const char *text)
  246. {
  247. char str[DEBUG_BUF_SIZE];
  248. const char *start, *cur;
  249. start = text;
  250. for (cur = text; *cur != '\0'; cur++) {
  251. if (*cur == '\n') {
  252. size_t len = cur - start + 1;
  253. if (len > DEBUG_BUF_SIZE - 1) {
  254. len = DEBUG_BUF_SIZE - 1;
  255. }
  256. memcpy(str, start, len);
  257. str[len] = '\0';
  258. debug_send_line(ssl, level, file, line, str);
  259. start = cur + 1;
  260. }
  261. }
  262. }
  263. void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level,
  264. const char *file, int line,
  265. const char *text, const mbedtls_x509_crt *crt)
  266. {
  267. char str[DEBUG_BUF_SIZE];
  268. int i = 0;
  269. if (ssl->conf == NULL || ssl->conf->f_dbg == NULL || crt == NULL || level > debug_threshold) {
  270. return;
  271. }
  272. while (crt != NULL) {
  273. char buf[1024];
  274. mbedtls_snprintf(str, sizeof(str), "%s #%d:\n", text, ++i);
  275. debug_send_line(ssl, level, file, line, str);
  276. mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt);
  277. debug_print_line_by_line(ssl, level, file, line, buf);
  278. debug_print_pk(ssl, level, file, line, "crt->", &crt->pk);
  279. crt = crt->next;
  280. }
  281. }
  282. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  283. #endif /* MBEDTLS_DEBUG_C */