debug.c 13 KB

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