debug.c 12 KB

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