debug.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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 "debug_internal.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 it 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. memset(txt, 0, sizeof(txt));
  111. for (i = 0; i < len; i++) {
  112. if (i >= 4096) {
  113. break;
  114. }
  115. if (i % 16 == 0) {
  116. if (i > 0) {
  117. mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
  118. debug_send_line(ssl, level, file, line, str);
  119. idx = 0;
  120. memset(txt, 0, sizeof(txt));
  121. }
  122. idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, "%04x: ",
  123. (unsigned int) i);
  124. }
  125. idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x",
  126. (unsigned int) buf[i]);
  127. txt[i % 16] = (buf[i] > 31 && buf[i] < 127) ? buf[i] : '.';
  128. }
  129. if (len > 0) {
  130. for (/* i = i */; i % 16 != 0; i++) {
  131. idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " ");
  132. }
  133. mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
  134. debug_send_line(ssl, level, file, line, str);
  135. }
  136. }
  137. #if defined(MBEDTLS_ECP_LIGHT)
  138. void mbedtls_debug_print_ecp(const mbedtls_ssl_context *ssl, int level,
  139. const char *file, int line,
  140. const char *text, const mbedtls_ecp_point *X)
  141. {
  142. char str[DEBUG_BUF_SIZE];
  143. if (NULL == ssl ||
  144. NULL == ssl->conf ||
  145. NULL == ssl->conf->f_dbg ||
  146. level > debug_threshold) {
  147. return;
  148. }
  149. mbedtls_snprintf(str, sizeof(str), "%s(X)", text);
  150. mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->X);
  151. mbedtls_snprintf(str, sizeof(str), "%s(Y)", text);
  152. mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->Y);
  153. }
  154. #endif /* MBEDTLS_ECP_LIGHT */
  155. #if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
  156. static void mbedtls_debug_print_ec_coord(const mbedtls_ssl_context *ssl, int level,
  157. const char *file, int line, const char *text,
  158. const unsigned char *buf, size_t len)
  159. {
  160. char str[DEBUG_BUF_SIZE];
  161. size_t i, idx = 0;
  162. mbedtls_snprintf(str + idx, sizeof(str) - idx, "value of '%s' (%u bits) is:\n",
  163. text, (unsigned int) len * 8);
  164. debug_send_line(ssl, level, file, line, str);
  165. for (i = 0; i < len; i++) {
  166. if (i >= 4096) {
  167. break;
  168. }
  169. if (i % 16 == 0) {
  170. if (i > 0) {
  171. mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
  172. debug_send_line(ssl, level, file, line, str);
  173. idx = 0;
  174. }
  175. }
  176. idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x",
  177. (unsigned int) buf[i]);
  178. }
  179. if (len > 0) {
  180. for (/* i = i */; i % 16 != 0; i++) {
  181. idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " ");
  182. }
  183. mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
  184. debug_send_line(ssl, level, file, line, str);
  185. }
  186. }
  187. void mbedtls_debug_print_psa_ec(const mbedtls_ssl_context *ssl, int level,
  188. const char *file, int line,
  189. const char *text, const mbedtls_pk_context *pk)
  190. {
  191. char str[DEBUG_BUF_SIZE];
  192. const uint8_t *coord_start;
  193. size_t coord_len;
  194. if (NULL == ssl ||
  195. NULL == ssl->conf ||
  196. NULL == ssl->conf->f_dbg ||
  197. level > debug_threshold) {
  198. return;
  199. }
  200. /* For the description of pk->pk_raw content please refer to the description
  201. * psa_export_public_key() function. */
  202. coord_len = (pk->pub_raw_len - 1)/2;
  203. /* X coordinate */
  204. coord_start = pk->pub_raw + 1;
  205. mbedtls_snprintf(str, sizeof(str), "%s(X)", text);
  206. mbedtls_debug_print_ec_coord(ssl, level, file, line, str, coord_start, coord_len);
  207. /* Y coordinate */
  208. coord_start = coord_start + coord_len;
  209. mbedtls_snprintf(str, sizeof(str), "%s(Y)", text);
  210. mbedtls_debug_print_ec_coord(ssl, level, file, line, str, coord_start, coord_len);
  211. }
  212. #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
  213. #if defined(MBEDTLS_BIGNUM_C)
  214. void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl, int level,
  215. const char *file, int line,
  216. const char *text, const mbedtls_mpi *X)
  217. {
  218. char str[DEBUG_BUF_SIZE];
  219. size_t bitlen;
  220. size_t idx = 0;
  221. if (NULL == ssl ||
  222. NULL == ssl->conf ||
  223. NULL == ssl->conf->f_dbg ||
  224. NULL == X ||
  225. level > debug_threshold) {
  226. return;
  227. }
  228. bitlen = mbedtls_mpi_bitlen(X);
  229. mbedtls_snprintf(str, sizeof(str), "value of '%s' (%u bits) is:\n",
  230. text, (unsigned) bitlen);
  231. debug_send_line(ssl, level, file, line, str);
  232. if (bitlen == 0) {
  233. str[0] = ' '; str[1] = '0'; str[2] = '0';
  234. idx = 3;
  235. } else {
  236. int n;
  237. for (n = (int) ((bitlen - 1) / 8); n >= 0; n--) {
  238. size_t limb_offset = n / sizeof(mbedtls_mpi_uint);
  239. size_t offset_in_limb = n % sizeof(mbedtls_mpi_uint);
  240. unsigned char octet =
  241. (X->p[limb_offset] >> (offset_in_limb * 8)) & 0xff;
  242. mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x", octet);
  243. idx += 3;
  244. /* Wrap lines after 16 octets that each take 3 columns */
  245. if (idx >= 3 * 16) {
  246. mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
  247. debug_send_line(ssl, level, file, line, str);
  248. idx = 0;
  249. }
  250. }
  251. }
  252. if (idx != 0) {
  253. mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
  254. debug_send_line(ssl, level, file, line, str);
  255. }
  256. }
  257. #endif /* MBEDTLS_BIGNUM_C */
  258. #if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_INFO)
  259. static void debug_print_pk(const mbedtls_ssl_context *ssl, int level,
  260. const char *file, int line,
  261. const char *text, const mbedtls_pk_context *pk)
  262. {
  263. size_t i;
  264. mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
  265. char name[16];
  266. memset(items, 0, sizeof(items));
  267. if (mbedtls_pk_debug(pk, items) != 0) {
  268. debug_send_line(ssl, level, file, line,
  269. "invalid PK context\n");
  270. return;
  271. }
  272. for (i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++) {
  273. if (items[i].type == MBEDTLS_PK_DEBUG_NONE) {
  274. return;
  275. }
  276. mbedtls_snprintf(name, sizeof(name), "%s%s", text, items[i].name);
  277. name[sizeof(name) - 1] = '\0';
  278. #if defined(MBEDTLS_RSA_C)
  279. if (items[i].type == MBEDTLS_PK_DEBUG_MPI) {
  280. mbedtls_debug_print_mpi(ssl, level, file, line, name, items[i].value);
  281. } else
  282. #endif /* MBEDTLS_RSA_C */
  283. #if defined(MBEDTLS_ECP_LIGHT)
  284. if (items[i].type == MBEDTLS_PK_DEBUG_ECP) {
  285. mbedtls_debug_print_ecp(ssl, level, file, line, name, items[i].value);
  286. } else
  287. #endif /* MBEDTLS_ECP_LIGHT */
  288. #if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
  289. if (items[i].type == MBEDTLS_PK_DEBUG_PSA_EC) {
  290. mbedtls_debug_print_psa_ec(ssl, level, file, line, name, items[i].value);
  291. } else
  292. #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
  293. { debug_send_line(ssl, level, file, line,
  294. "should not happen\n"); }
  295. }
  296. }
  297. static void debug_print_line_by_line(const mbedtls_ssl_context *ssl, int level,
  298. const char *file, int line, const char *text)
  299. {
  300. char str[DEBUG_BUF_SIZE];
  301. const char *start, *cur;
  302. start = text;
  303. for (cur = text; *cur != '\0'; cur++) {
  304. if (*cur == '\n') {
  305. size_t len = (size_t) (cur - start) + 1;
  306. if (len > DEBUG_BUF_SIZE - 1) {
  307. len = DEBUG_BUF_SIZE - 1;
  308. }
  309. memcpy(str, start, len);
  310. str[len] = '\0';
  311. debug_send_line(ssl, level, file, line, str);
  312. start = cur + 1;
  313. }
  314. }
  315. }
  316. void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level,
  317. const char *file, int line,
  318. const char *text, const mbedtls_x509_crt *crt)
  319. {
  320. char str[DEBUG_BUF_SIZE];
  321. int i = 0;
  322. if (NULL == ssl ||
  323. NULL == ssl->conf ||
  324. NULL == ssl->conf->f_dbg ||
  325. NULL == crt ||
  326. level > debug_threshold) {
  327. return;
  328. }
  329. while (crt != NULL) {
  330. char buf[1024];
  331. mbedtls_snprintf(str, sizeof(str), "%s #%d:\n", text, ++i);
  332. debug_send_line(ssl, level, file, line, str);
  333. mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt);
  334. debug_print_line_by_line(ssl, level, file, line, buf);
  335. debug_print_pk(ssl, level, file, line, "crt->", &crt->pk);
  336. crt = crt->next;
  337. }
  338. }
  339. #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_X509_REMOVE_INFO */
  340. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED) && \
  341. defined(MBEDTLS_ECDH_C)
  342. static void mbedtls_debug_printf_ecdh_internal(const mbedtls_ssl_context *ssl,
  343. int level, const char *file,
  344. int line,
  345. const mbedtls_ecdh_context *ecdh,
  346. mbedtls_debug_ecdh_attr attr)
  347. {
  348. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  349. const mbedtls_ecdh_context *ctx = ecdh;
  350. #else
  351. const mbedtls_ecdh_context_mbed *ctx = &ecdh->ctx.mbed_ecdh;
  352. #endif
  353. switch (attr) {
  354. case MBEDTLS_DEBUG_ECDH_Q:
  355. mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Q",
  356. &ctx->Q);
  357. break;
  358. case MBEDTLS_DEBUG_ECDH_QP:
  359. mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Qp",
  360. &ctx->Qp);
  361. break;
  362. case MBEDTLS_DEBUG_ECDH_Z:
  363. mbedtls_debug_print_mpi(ssl, level, file, line, "ECDH: z",
  364. &ctx->z);
  365. break;
  366. default:
  367. break;
  368. }
  369. }
  370. void mbedtls_debug_printf_ecdh(const mbedtls_ssl_context *ssl, int level,
  371. const char *file, int line,
  372. const mbedtls_ecdh_context *ecdh,
  373. mbedtls_debug_ecdh_attr attr)
  374. {
  375. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  376. mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh, attr);
  377. #else
  378. switch (ecdh->var) {
  379. default:
  380. mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh,
  381. attr);
  382. }
  383. #endif
  384. }
  385. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED &&
  386. MBEDTLS_ECDH_C */
  387. #endif /* MBEDTLS_DEBUG_C */