debug.c 11 KB

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