debug.c 11 KB

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