debug.c 13 KB

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