debug.c 12 KB

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