debug.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. #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. MBEDTLS_PRINTF_ATTRIBUTE(5, 6)
  54. void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level,
  55. const char *file, int line,
  56. const char *format, ...)
  57. {
  58. va_list argp;
  59. char str[DEBUG_BUF_SIZE];
  60. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  61. if (NULL == ssl ||
  62. NULL == ssl->conf ||
  63. NULL == ssl->conf->f_dbg ||
  64. level > debug_threshold) {
  65. return;
  66. }
  67. va_start(argp, format);
  68. ret = mbedtls_vsnprintf(str, DEBUG_BUF_SIZE, format, argp);
  69. va_end(argp);
  70. if (ret >= 0 && ret < DEBUG_BUF_SIZE - 1) {
  71. str[ret] = '\n';
  72. str[ret + 1] = '\0';
  73. }
  74. debug_send_line(ssl, level, file, line, str);
  75. }
  76. void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level,
  77. const char *file, int line,
  78. const char *text, int ret)
  79. {
  80. char str[DEBUG_BUF_SIZE];
  81. if (NULL == ssl ||
  82. NULL == ssl->conf ||
  83. NULL == ssl->conf->f_dbg ||
  84. level > debug_threshold) {
  85. return;
  86. }
  87. /*
  88. * With non-blocking I/O and examples that just retry immediately,
  89. * the logs would be quickly flooded with WANT_READ, so ignore that.
  90. * Don't ignore WANT_WRITE however, since it is usually rare.
  91. */
  92. if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
  93. return;
  94. }
  95. mbedtls_snprintf(str, sizeof(str), "%s() returned %d (-0x%04x)\n",
  96. text, ret, (unsigned int) -ret);
  97. debug_send_line(ssl, level, file, line, str);
  98. }
  99. void mbedtls_debug_print_buf(const mbedtls_ssl_context *ssl, int level,
  100. const char *file, int line, const char *text,
  101. const unsigned char *buf, size_t len)
  102. {
  103. char str[DEBUG_BUF_SIZE];
  104. char txt[17];
  105. size_t i, idx = 0;
  106. if (NULL == ssl ||
  107. NULL == ssl->conf ||
  108. NULL == ssl->conf->f_dbg ||
  109. level > debug_threshold) {
  110. return;
  111. }
  112. mbedtls_snprintf(str + idx, sizeof(str) - idx, "dumping '%s' (%u bytes)\n",
  113. text, (unsigned int) len);
  114. debug_send_line(ssl, level, file, line, str);
  115. idx = 0;
  116. memset(txt, 0, sizeof(txt));
  117. for (i = 0; i < len; i++) {
  118. if (i >= 4096) {
  119. break;
  120. }
  121. if (i % 16 == 0) {
  122. if (i > 0) {
  123. mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
  124. debug_send_line(ssl, level, file, line, str);
  125. idx = 0;
  126. memset(txt, 0, sizeof(txt));
  127. }
  128. idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, "%04x: ",
  129. (unsigned int) i);
  130. }
  131. idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x",
  132. (unsigned int) buf[i]);
  133. txt[i % 16] = (buf[i] > 31 && buf[i] < 127) ? buf[i] : '.';
  134. }
  135. if (len > 0) {
  136. for (/* i = i */; i % 16 != 0; i++) {
  137. idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " ");
  138. }
  139. mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
  140. debug_send_line(ssl, level, file, line, str);
  141. }
  142. }
  143. #if defined(MBEDTLS_ECP_C)
  144. void mbedtls_debug_print_ecp(const mbedtls_ssl_context *ssl, int level,
  145. const char *file, int line,
  146. const char *text, const mbedtls_ecp_point *X)
  147. {
  148. char str[DEBUG_BUF_SIZE];
  149. if (NULL == ssl ||
  150. NULL == ssl->conf ||
  151. NULL == ssl->conf->f_dbg ||
  152. level > debug_threshold) {
  153. return;
  154. }
  155. mbedtls_snprintf(str, sizeof(str), "%s(X)", text);
  156. mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->X);
  157. mbedtls_snprintf(str, sizeof(str), "%s(Y)", text);
  158. mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->Y);
  159. }
  160. #endif /* MBEDTLS_ECP_C */
  161. #if defined(MBEDTLS_BIGNUM_C)
  162. void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl, int level,
  163. const char *file, int line,
  164. const char *text, const mbedtls_mpi *X)
  165. {
  166. char str[DEBUG_BUF_SIZE];
  167. size_t bitlen;
  168. size_t idx = 0;
  169. if (NULL == ssl ||
  170. NULL == ssl->conf ||
  171. NULL == ssl->conf->f_dbg ||
  172. NULL == X ||
  173. level > debug_threshold) {
  174. return;
  175. }
  176. bitlen = mbedtls_mpi_bitlen(X);
  177. mbedtls_snprintf(str, sizeof(str), "value of '%s' (%u bits) is:\n",
  178. text, (unsigned) bitlen);
  179. debug_send_line(ssl, level, file, line, str);
  180. if (bitlen == 0) {
  181. str[0] = ' '; str[1] = '0'; str[2] = '0';
  182. idx = 3;
  183. } else {
  184. int n;
  185. for (n = (int) ((bitlen - 1) / 8); n >= 0; n--) {
  186. size_t limb_offset = n / sizeof(mbedtls_mpi_uint);
  187. size_t offset_in_limb = n % sizeof(mbedtls_mpi_uint);
  188. unsigned char octet =
  189. (X->p[limb_offset] >> (offset_in_limb * 8)) & 0xff;
  190. mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x", octet);
  191. idx += 3;
  192. /* Wrap lines after 16 octets that each take 3 columns */
  193. if (idx >= 3 * 16) {
  194. mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
  195. debug_send_line(ssl, level, file, line, str);
  196. idx = 0;
  197. }
  198. }
  199. }
  200. if (idx != 0) {
  201. mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
  202. debug_send_line(ssl, level, file, line, str);
  203. }
  204. }
  205. #endif /* MBEDTLS_BIGNUM_C */
  206. #if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_INFO)
  207. static void debug_print_pk(const mbedtls_ssl_context *ssl, int level,
  208. const char *file, int line,
  209. const char *text, const mbedtls_pk_context *pk)
  210. {
  211. size_t i;
  212. mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
  213. char name[16];
  214. memset(items, 0, sizeof(items));
  215. if (mbedtls_pk_debug(pk, items) != 0) {
  216. debug_send_line(ssl, level, file, line,
  217. "invalid PK context\n");
  218. return;
  219. }
  220. for (i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++) {
  221. if (items[i].type == MBEDTLS_PK_DEBUG_NONE) {
  222. return;
  223. }
  224. mbedtls_snprintf(name, sizeof(name), "%s%s", text, items[i].name);
  225. name[sizeof(name) - 1] = '\0';
  226. if (items[i].type == MBEDTLS_PK_DEBUG_MPI) {
  227. mbedtls_debug_print_mpi(ssl, level, file, line, name, items[i].value);
  228. } else
  229. #if defined(MBEDTLS_ECP_C)
  230. if (items[i].type == MBEDTLS_PK_DEBUG_ECP) {
  231. mbedtls_debug_print_ecp(ssl, level, file, line, name, items[i].value);
  232. } else
  233. #endif
  234. { debug_send_line(ssl, level, file, line,
  235. "should not happen\n"); }
  236. }
  237. }
  238. static void debug_print_line_by_line(const mbedtls_ssl_context *ssl, int level,
  239. const char *file, int line, const char *text)
  240. {
  241. char str[DEBUG_BUF_SIZE];
  242. const char *start, *cur;
  243. start = text;
  244. for (cur = text; *cur != '\0'; cur++) {
  245. if (*cur == '\n') {
  246. size_t len = cur - start + 1;
  247. if (len > DEBUG_BUF_SIZE - 1) {
  248. len = DEBUG_BUF_SIZE - 1;
  249. }
  250. memcpy(str, start, len);
  251. str[len] = '\0';
  252. debug_send_line(ssl, level, file, line, str);
  253. start = cur + 1;
  254. }
  255. }
  256. }
  257. void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level,
  258. const char *file, int line,
  259. const char *text, const mbedtls_x509_crt *crt)
  260. {
  261. char str[DEBUG_BUF_SIZE];
  262. int i = 0;
  263. if (NULL == ssl ||
  264. NULL == ssl->conf ||
  265. NULL == ssl->conf->f_dbg ||
  266. NULL == crt ||
  267. level > debug_threshold) {
  268. return;
  269. }
  270. while (crt != NULL) {
  271. char buf[1024];
  272. mbedtls_snprintf(str, sizeof(str), "%s #%d:\n", text, ++i);
  273. debug_send_line(ssl, level, file, line, str);
  274. mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt);
  275. debug_print_line_by_line(ssl, level, file, line, buf);
  276. debug_print_pk(ssl, level, file, line, "crt->", &crt->pk);
  277. crt = crt->next;
  278. }
  279. }
  280. #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_X509_REMOVE_INFO */
  281. #if defined(MBEDTLS_ECDH_C)
  282. static void mbedtls_debug_printf_ecdh_internal(const mbedtls_ssl_context *ssl,
  283. int level, const char *file,
  284. int line,
  285. const mbedtls_ecdh_context *ecdh,
  286. mbedtls_debug_ecdh_attr attr)
  287. {
  288. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  289. const mbedtls_ecdh_context *ctx = ecdh;
  290. #else
  291. const mbedtls_ecdh_context_mbed *ctx = &ecdh->ctx.mbed_ecdh;
  292. #endif
  293. switch (attr) {
  294. case MBEDTLS_DEBUG_ECDH_Q:
  295. mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Q",
  296. &ctx->Q);
  297. break;
  298. case MBEDTLS_DEBUG_ECDH_QP:
  299. mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Qp",
  300. &ctx->Qp);
  301. break;
  302. case MBEDTLS_DEBUG_ECDH_Z:
  303. mbedtls_debug_print_mpi(ssl, level, file, line, "ECDH: z",
  304. &ctx->z);
  305. break;
  306. default:
  307. break;
  308. }
  309. }
  310. void mbedtls_debug_printf_ecdh(const mbedtls_ssl_context *ssl, int level,
  311. const char *file, int line,
  312. const mbedtls_ecdh_context *ecdh,
  313. mbedtls_debug_ecdh_attr attr)
  314. {
  315. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  316. mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh, attr);
  317. #else
  318. switch (ecdh->var) {
  319. default:
  320. mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh,
  321. attr);
  322. }
  323. #endif
  324. }
  325. #endif /* MBEDTLS_ECDH_C */
  326. #endif /* MBEDTLS_DEBUG_C */