debug.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * Debugging routines
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: GPL-2.0
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. #if !defined(MBEDTLS_CONFIG_FILE)
  24. #include "mbedtls/config.h"
  25. #else
  26. #include MBEDTLS_CONFIG_FILE
  27. #endif
  28. #if defined(MBEDTLS_DEBUG_C)
  29. #if defined(MBEDTLS_PLATFORM_C)
  30. #include "mbedtls/platform.h"
  31. #else
  32. #include <stdlib.h>
  33. #define mbedtls_calloc calloc
  34. #define mbedtls_free free
  35. #define mbedtls_time_t time_t
  36. #define mbedtls_snprintf snprintf
  37. #endif
  38. #include "mbedtls/debug.h"
  39. #include <stdarg.h>
  40. #include <stdio.h>
  41. #include <string.h>
  42. #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
  43. !defined(inline) && !defined(__cplusplus)
  44. #define inline __inline
  45. #endif
  46. #define DEBUG_BUF_SIZE 512
  47. static int debug_threshold = 0;
  48. void mbedtls_debug_set_threshold( int threshold )
  49. {
  50. debug_threshold = threshold;
  51. }
  52. /*
  53. * All calls to f_dbg must be made via this function
  54. */
  55. static inline void debug_send_line( const mbedtls_ssl_context *ssl, int level,
  56. const char *file, int line,
  57. const char *str )
  58. {
  59. /*
  60. * If in a threaded environment, we need a thread identifier.
  61. * Since there is no portable way to get one, use the address of the ssl
  62. * context instead, as it shouldn't be shared between threads.
  63. */
  64. #if defined(MBEDTLS_THREADING_C)
  65. char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */
  66. mbedtls_snprintf( idstr, sizeof( idstr ), "%p: %s", (void*)ssl, str );
  67. ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, idstr );
  68. #else
  69. ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
  70. #endif
  71. }
  72. void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
  73. const char *file, int line,
  74. const char *format, ... )
  75. {
  76. va_list argp;
  77. char str[DEBUG_BUF_SIZE];
  78. int ret;
  79. if( NULL == ssl || NULL == ssl->conf || NULL == ssl->conf->f_dbg || level > debug_threshold )
  80. return;
  81. va_start( argp, format );
  82. #if defined(_WIN32)
  83. #if defined(_TRUNCATE)
  84. ret = _vsnprintf_s( str, DEBUG_BUF_SIZE, _TRUNCATE, format, argp );
  85. #else
  86. ret = _vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
  87. if( ret < 0 || (size_t) ret == DEBUG_BUF_SIZE )
  88. {
  89. str[DEBUG_BUF_SIZE-1] = '\0';
  90. ret = -1;
  91. }
  92. #endif
  93. #else
  94. ret = vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
  95. #endif
  96. va_end( argp );
  97. if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 )
  98. {
  99. str[ret] = '\n';
  100. str[ret + 1] = '\0';
  101. }
  102. debug_send_line( ssl, level, file, line, str );
  103. }
  104. void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
  105. const char *file, int line,
  106. const char *text, int ret )
  107. {
  108. char str[DEBUG_BUF_SIZE];
  109. if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
  110. return;
  111. /*
  112. * With non-blocking I/O and examples that just retry immediately,
  113. * the logs would be quickly flooded with WANT_READ, so ignore that.
  114. * Don't ignore WANT_WRITE however, since is is usually rare.
  115. */
  116. if( ret == MBEDTLS_ERR_SSL_WANT_READ )
  117. return;
  118. mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n",
  119. text, ret, -ret );
  120. debug_send_line( ssl, level, file, line, str );
  121. }
  122. void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
  123. const char *file, int line, const char *text,
  124. const unsigned char *buf, size_t len )
  125. {
  126. char str[DEBUG_BUF_SIZE];
  127. char txt[17];
  128. size_t i, idx = 0;
  129. if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
  130. return;
  131. mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n",
  132. text, (unsigned int) len );
  133. debug_send_line( ssl, level, file, line, str );
  134. idx = 0;
  135. memset( txt, 0, sizeof( txt ) );
  136. for( i = 0; i < len; i++ )
  137. {
  138. if( i >= 4096 )
  139. break;
  140. if( i % 16 == 0 )
  141. {
  142. if( i > 0 )
  143. {
  144. mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
  145. debug_send_line( ssl, level, file, line, str );
  146. idx = 0;
  147. memset( txt, 0, sizeof( txt ) );
  148. }
  149. idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ",
  150. (unsigned int) i );
  151. }
  152. idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x",
  153. (unsigned int) buf[i] );
  154. txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
  155. }
  156. if( len > 0 )
  157. {
  158. for( /* i = i */; i % 16 != 0; i++ )
  159. idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " );
  160. mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
  161. debug_send_line( ssl, level, file, line, str );
  162. }
  163. }
  164. #if defined(MBEDTLS_ECP_C)
  165. void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
  166. const char *file, int line,
  167. const char *text, const mbedtls_ecp_point *X )
  168. {
  169. char str[DEBUG_BUF_SIZE];
  170. if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
  171. return;
  172. mbedtls_snprintf( str, sizeof( str ), "%s(X)", text );
  173. mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
  174. mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text );
  175. mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
  176. }
  177. #endif /* MBEDTLS_ECP_C */
  178. #if defined(MBEDTLS_BIGNUM_C)
  179. void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
  180. const char *file, int line,
  181. const char *text, const mbedtls_mpi *X )
  182. {
  183. char str[DEBUG_BUF_SIZE];
  184. int j, k, zeros = 1;
  185. size_t i, n, idx = 0;
  186. if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || X == NULL || level > debug_threshold )
  187. return;
  188. for( n = X->n - 1; n > 0; n-- )
  189. if( X->p[n] != 0 )
  190. break;
  191. for( j = ( sizeof(mbedtls_mpi_uint) << 3 ) - 1; j >= 0; j-- )
  192. if( ( ( X->p[n] >> j ) & 1 ) != 0 )
  193. break;
  194. mbedtls_snprintf( str + idx, sizeof( str ) - idx, "value of '%s' (%d bits) is:\n",
  195. text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) );
  196. debug_send_line( ssl, level, file, line, str );
  197. idx = 0;
  198. for( i = n + 1, j = 0; i > 0; i-- )
  199. {
  200. if( zeros && X->p[i - 1] == 0 )
  201. continue;
  202. for( k = sizeof( mbedtls_mpi_uint ) - 1; k >= 0; k-- )
  203. {
  204. if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
  205. continue;
  206. else
  207. zeros = 0;
  208. if( j % 16 == 0 )
  209. {
  210. if( j > 0 )
  211. {
  212. mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
  213. debug_send_line( ssl, level, file, line, str );
  214. idx = 0;
  215. }
  216. }
  217. idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", (unsigned int)
  218. ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
  219. j++;
  220. }
  221. }
  222. if( zeros == 1 )
  223. idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " 00" );
  224. mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
  225. debug_send_line( ssl, level, file, line, str );
  226. }
  227. #endif /* MBEDTLS_BIGNUM_C */
  228. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  229. static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
  230. const char *file, int line,
  231. const char *text, const mbedtls_pk_context *pk )
  232. {
  233. size_t i;
  234. mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
  235. char name[16];
  236. memset( items, 0, sizeof( items ) );
  237. if( mbedtls_pk_debug( pk, items ) != 0 )
  238. {
  239. debug_send_line( ssl, level, file, line,
  240. "invalid PK context\n" );
  241. return;
  242. }
  243. for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
  244. {
  245. if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
  246. return;
  247. mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
  248. name[sizeof( name ) - 1] = '\0';
  249. if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
  250. mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
  251. else
  252. #if defined(MBEDTLS_ECP_C)
  253. if( items[i].type == MBEDTLS_PK_DEBUG_ECP )
  254. mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value );
  255. else
  256. #endif
  257. debug_send_line( ssl, level, file, line,
  258. "should not happen\n" );
  259. }
  260. }
  261. static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level,
  262. const char *file, int line, const char *text )
  263. {
  264. char str[DEBUG_BUF_SIZE];
  265. const char *start, *cur;
  266. start = text;
  267. for( cur = text; *cur != '\0'; cur++ )
  268. {
  269. if( *cur == '\n' )
  270. {
  271. size_t len = cur - start + 1;
  272. if( len > DEBUG_BUF_SIZE - 1 )
  273. len = DEBUG_BUF_SIZE - 1;
  274. memcpy( str, start, len );
  275. str[len] = '\0';
  276. debug_send_line( ssl, level, file, line, str );
  277. start = cur + 1;
  278. }
  279. }
  280. }
  281. void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
  282. const char *file, int line,
  283. const char *text, const mbedtls_x509_crt *crt )
  284. {
  285. char str[DEBUG_BUF_SIZE];
  286. int i = 0;
  287. if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || crt == NULL || level > debug_threshold )
  288. return;
  289. while( crt != NULL )
  290. {
  291. char buf[1024];
  292. mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i );
  293. debug_send_line( ssl, level, file, line, str );
  294. mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
  295. debug_print_line_by_line( ssl, level, file, line, buf );
  296. debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
  297. crt = crt->next;
  298. }
  299. }
  300. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  301. #endif /* MBEDTLS_DEBUG_C */