debug.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * Debugging routines
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. */
  7. #include "common.h"
  8. #if defined(MBEDTLS_DEBUG_C)
  9. #include "mbedtls/platform.h"
  10. #include "mbedtls/debug.h"
  11. #include "mbedtls/error.h"
  12. #include <stdarg.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. /* DEBUG_BUF_SIZE must be at least 2 */
  16. #define DEBUG_BUF_SIZE 512
  17. static int debug_threshold = 0;
  18. void mbedtls_debug_set_threshold(int threshold)
  19. {
  20. debug_threshold = threshold;
  21. }
  22. /*
  23. * All calls to f_dbg must be made via this function
  24. */
  25. static inline void debug_send_line(const mbedtls_ssl_context *ssl, int level,
  26. const char *file, int line,
  27. const char *str)
  28. {
  29. /*
  30. * If in a threaded environment, we need a thread identifier.
  31. * Since there is no portable way to get one, use the address of the ssl
  32. * context instead, as it shouldn't be shared between threads.
  33. */
  34. #if defined(MBEDTLS_THREADING_C)
  35. char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */
  36. mbedtls_snprintf(idstr, sizeof(idstr), "%p: %s", (void *) ssl, str);
  37. ssl->conf->f_dbg(ssl->conf->p_dbg, level, file, line, idstr);
  38. #else
  39. ssl->conf->f_dbg(ssl->conf->p_dbg, level, file, line, str);
  40. #endif
  41. }
  42. MBEDTLS_PRINTF_ATTRIBUTE(5, 6)
  43. void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level,
  44. const char *file, int line,
  45. const char *format, ...)
  46. {
  47. va_list argp;
  48. char str[DEBUG_BUF_SIZE];
  49. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  50. MBEDTLS_STATIC_ASSERT(DEBUG_BUF_SIZE >= 2, "DEBUG_BUF_SIZE too small");
  51. if (NULL == ssl ||
  52. NULL == ssl->conf ||
  53. NULL == ssl->conf->f_dbg ||
  54. level > debug_threshold) {
  55. return;
  56. }
  57. va_start(argp, format);
  58. ret = mbedtls_vsnprintf(str, DEBUG_BUF_SIZE, format, argp);
  59. va_end(argp);
  60. if (ret < 0) {
  61. ret = 0;
  62. } else {
  63. if (ret >= DEBUG_BUF_SIZE - 1) {
  64. ret = DEBUG_BUF_SIZE - 2;
  65. }
  66. }
  67. str[ret] = '\n';
  68. str[ret + 1] = '\0';
  69. debug_send_line(ssl, level, file, line, str);
  70. }
  71. void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level,
  72. const char *file, int line,
  73. const char *text, int ret)
  74. {
  75. char str[DEBUG_BUF_SIZE];
  76. if (NULL == ssl ||
  77. NULL == ssl->conf ||
  78. NULL == ssl->conf->f_dbg ||
  79. level > debug_threshold) {
  80. return;
  81. }
  82. /*
  83. * With non-blocking I/O and examples that just retry immediately,
  84. * the logs would be quickly flooded with WANT_READ, so ignore that.
  85. * Don't ignore WANT_WRITE however, since is is usually rare.
  86. */
  87. if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
  88. return;
  89. }
  90. mbedtls_snprintf(str, sizeof(str), "%s() returned %d (-0x%04x)\n",
  91. text, ret, (unsigned int) -ret);
  92. debug_send_line(ssl, level, file, line, str);
  93. }
  94. void mbedtls_debug_print_buf(const mbedtls_ssl_context *ssl, int level,
  95. const char *file, int line, const char *text,
  96. const unsigned char *buf, size_t len)
  97. {
  98. char str[DEBUG_BUF_SIZE];
  99. char txt[17];
  100. size_t i, idx = 0;
  101. if (NULL == ssl ||
  102. NULL == ssl->conf ||
  103. NULL == ssl->conf->f_dbg ||
  104. level > debug_threshold) {
  105. return;
  106. }
  107. mbedtls_snprintf(str + idx, sizeof(str) - idx, "dumping '%s' (%u bytes)\n",
  108. text, (unsigned int) len);
  109. debug_send_line(ssl, level, file, line, str);
  110. idx = 0;
  111. memset(txt, 0, sizeof(txt));
  112. for (i = 0; i < len; i++) {
  113. if (i >= 4096) {
  114. break;
  115. }
  116. if (i % 16 == 0) {
  117. if (i > 0) {
  118. mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
  119. debug_send_line(ssl, level, file, line, str);
  120. idx = 0;
  121. memset(txt, 0, sizeof(txt));
  122. }
  123. idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, "%04x: ",
  124. (unsigned int) i);
  125. }
  126. idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x",
  127. (unsigned int) buf[i]);
  128. txt[i % 16] = (buf[i] > 31 && buf[i] < 127) ? buf[i] : '.';
  129. }
  130. if (len > 0) {
  131. for (/* i = i */; i % 16 != 0; i++) {
  132. idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " ");
  133. }
  134. mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
  135. debug_send_line(ssl, level, file, line, str);
  136. }
  137. }
  138. #if defined(MBEDTLS_ECP_C)
  139. void mbedtls_debug_print_ecp(const mbedtls_ssl_context *ssl, int level,
  140. const char *file, int line,
  141. const char *text, const mbedtls_ecp_point *X)
  142. {
  143. char str[DEBUG_BUF_SIZE];
  144. if (NULL == ssl ||
  145. NULL == ssl->conf ||
  146. NULL == ssl->conf->f_dbg ||
  147. level > debug_threshold) {
  148. return;
  149. }
  150. mbedtls_snprintf(str, sizeof(str), "%s(X)", text);
  151. mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->X);
  152. mbedtls_snprintf(str, sizeof(str), "%s(Y)", text);
  153. mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->Y);
  154. }
  155. #endif /* MBEDTLS_ECP_C */
  156. #if defined(MBEDTLS_BIGNUM_C)
  157. void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl, int level,
  158. const char *file, int line,
  159. const char *text, const mbedtls_mpi *X)
  160. {
  161. char str[DEBUG_BUF_SIZE];
  162. size_t bitlen;
  163. size_t idx = 0;
  164. if (NULL == ssl ||
  165. NULL == ssl->conf ||
  166. NULL == ssl->conf->f_dbg ||
  167. NULL == X ||
  168. level > debug_threshold) {
  169. return;
  170. }
  171. bitlen = mbedtls_mpi_bitlen(X);
  172. mbedtls_snprintf(str, sizeof(str), "value of '%s' (%u bits) is:\n",
  173. text, (unsigned) bitlen);
  174. debug_send_line(ssl, level, file, line, str);
  175. if (bitlen == 0) {
  176. str[0] = ' '; str[1] = '0'; str[2] = '0';
  177. idx = 3;
  178. } else {
  179. int n;
  180. for (n = (int) ((bitlen - 1) / 8); n >= 0; n--) {
  181. size_t limb_offset = n / sizeof(mbedtls_mpi_uint);
  182. size_t offset_in_limb = n % sizeof(mbedtls_mpi_uint);
  183. unsigned char octet =
  184. (X->p[limb_offset] >> (offset_in_limb * 8)) & 0xff;
  185. mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x", octet);
  186. idx += 3;
  187. /* Wrap lines after 16 octets that each take 3 columns */
  188. if (idx >= 3 * 16) {
  189. mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
  190. debug_send_line(ssl, level, file, line, str);
  191. idx = 0;
  192. }
  193. }
  194. }
  195. if (idx != 0) {
  196. mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
  197. debug_send_line(ssl, level, file, line, str);
  198. }
  199. }
  200. #endif /* MBEDTLS_BIGNUM_C */
  201. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  202. static void debug_print_pk(const mbedtls_ssl_context *ssl, int level,
  203. const char *file, int line,
  204. const char *text, const mbedtls_pk_context *pk)
  205. {
  206. size_t i;
  207. mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
  208. char name[16];
  209. memset(items, 0, sizeof(items));
  210. if (mbedtls_pk_debug(pk, items) != 0) {
  211. debug_send_line(ssl, level, file, line,
  212. "invalid PK context\n");
  213. return;
  214. }
  215. for (i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++) {
  216. if (items[i].type == MBEDTLS_PK_DEBUG_NONE) {
  217. return;
  218. }
  219. mbedtls_snprintf(name, sizeof(name), "%s%s", text, items[i].name);
  220. name[sizeof(name) - 1] = '\0';
  221. if (items[i].type == MBEDTLS_PK_DEBUG_MPI) {
  222. mbedtls_debug_print_mpi(ssl, level, file, line, name, items[i].value);
  223. } else
  224. #if defined(MBEDTLS_ECP_C)
  225. if (items[i].type == MBEDTLS_PK_DEBUG_ECP) {
  226. mbedtls_debug_print_ecp(ssl, level, file, line, name, items[i].value);
  227. } else
  228. #endif
  229. { debug_send_line(ssl, level, file, line,
  230. "should not happen\n"); }
  231. }
  232. }
  233. static void debug_print_line_by_line(const mbedtls_ssl_context *ssl, int level,
  234. const char *file, int line, const char *text)
  235. {
  236. char str[DEBUG_BUF_SIZE];
  237. const char *start, *cur;
  238. start = text;
  239. for (cur = text; *cur != '\0'; cur++) {
  240. if (*cur == '\n') {
  241. size_t len = cur - start + 1;
  242. if (len > DEBUG_BUF_SIZE - 1) {
  243. len = DEBUG_BUF_SIZE - 1;
  244. }
  245. memcpy(str, start, len);
  246. str[len] = '\0';
  247. debug_send_line(ssl, level, file, line, str);
  248. start = cur + 1;
  249. }
  250. }
  251. }
  252. void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level,
  253. const char *file, int line,
  254. const char *text, const mbedtls_x509_crt *crt)
  255. {
  256. char str[DEBUG_BUF_SIZE];
  257. int i = 0;
  258. if (NULL == ssl ||
  259. NULL == ssl->conf ||
  260. NULL == ssl->conf->f_dbg ||
  261. NULL == crt ||
  262. level > debug_threshold) {
  263. return;
  264. }
  265. while (crt != NULL) {
  266. char buf[1024];
  267. mbedtls_snprintf(str, sizeof(str), "%s #%d:\n", text, ++i);
  268. debug_send_line(ssl, level, file, line, str);
  269. mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt);
  270. debug_print_line_by_line(ssl, level, file, line, buf);
  271. debug_print_pk(ssl, level, file, line, "crt->", &crt->pk);
  272. crt = crt->next;
  273. }
  274. }
  275. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  276. #if defined(MBEDTLS_ECDH_C)
  277. static void mbedtls_debug_printf_ecdh_internal(const mbedtls_ssl_context *ssl,
  278. int level, const char *file,
  279. int line,
  280. const mbedtls_ecdh_context *ecdh,
  281. mbedtls_debug_ecdh_attr attr)
  282. {
  283. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  284. const mbedtls_ecdh_context *ctx = ecdh;
  285. #else
  286. const mbedtls_ecdh_context_mbed *ctx = &ecdh->ctx.mbed_ecdh;
  287. #endif
  288. switch (attr) {
  289. case MBEDTLS_DEBUG_ECDH_Q:
  290. mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Q",
  291. &ctx->Q);
  292. break;
  293. case MBEDTLS_DEBUG_ECDH_QP:
  294. mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Qp",
  295. &ctx->Qp);
  296. break;
  297. case MBEDTLS_DEBUG_ECDH_Z:
  298. mbedtls_debug_print_mpi(ssl, level, file, line, "ECDH: z",
  299. &ctx->z);
  300. break;
  301. default:
  302. break;
  303. }
  304. }
  305. void mbedtls_debug_printf_ecdh(const mbedtls_ssl_context *ssl, int level,
  306. const char *file, int line,
  307. const mbedtls_ecdh_context *ecdh,
  308. mbedtls_debug_ecdh_attr attr)
  309. {
  310. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  311. mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh, attr);
  312. #else
  313. switch (ecdh->var) {
  314. default:
  315. mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh,
  316. attr);
  317. }
  318. #endif
  319. }
  320. #endif /* MBEDTLS_ECDH_C */
  321. #endif /* MBEDTLS_DEBUG_C */