debug.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. * Debugging routines
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. *
  7. * This file is provided under the Apache License 2.0, or the
  8. * GNU General Public License v2.0 or later.
  9. *
  10. * **********
  11. * Apache License 2.0:
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * **********
  26. *
  27. * **********
  28. * GNU General Public License v2.0 or later:
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License along
  41. * with this program; if not, write to the Free Software Foundation, Inc.,
  42. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. *
  44. * **********
  45. */
  46. #if !defined(MBEDTLS_CONFIG_FILE)
  47. #include "mbedtls/config.h"
  48. #else
  49. #include MBEDTLS_CONFIG_FILE
  50. #endif
  51. #if defined(MBEDTLS_DEBUG_C)
  52. #if defined(MBEDTLS_PLATFORM_C)
  53. #include "mbedtls/platform.h"
  54. #else
  55. #include <stdlib.h>
  56. #define mbedtls_calloc calloc
  57. #define mbedtls_free free
  58. #define mbedtls_time_t time_t
  59. #define mbedtls_snprintf snprintf
  60. #endif
  61. #include "mbedtls/debug.h"
  62. #include <stdarg.h>
  63. #include <stdio.h>
  64. #include <string.h>
  65. #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
  66. !defined(inline) && !defined(__cplusplus)
  67. #define inline __inline
  68. #endif
  69. #define DEBUG_BUF_SIZE 512
  70. static int debug_threshold = 0;
  71. void mbedtls_debug_set_threshold( int threshold )
  72. {
  73. debug_threshold = threshold;
  74. }
  75. /*
  76. * All calls to f_dbg must be made via this function
  77. */
  78. static inline void debug_send_line( const mbedtls_ssl_context *ssl, int level,
  79. const char *file, int line,
  80. const char *str )
  81. {
  82. /*
  83. * If in a threaded environment, we need a thread identifier.
  84. * Since there is no portable way to get one, use the address of the ssl
  85. * context instead, as it shouldn't be shared between threads.
  86. */
  87. #if defined(MBEDTLS_THREADING_C)
  88. char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */
  89. mbedtls_snprintf( idstr, sizeof( idstr ), "%p: %s", (void*)ssl, str );
  90. ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, idstr );
  91. #else
  92. ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
  93. #endif
  94. }
  95. void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
  96. const char *file, int line,
  97. const char *format, ... )
  98. {
  99. va_list argp;
  100. char str[DEBUG_BUF_SIZE];
  101. int ret;
  102. if( NULL == ssl ||
  103. NULL == ssl->conf ||
  104. NULL == ssl->conf->f_dbg ||
  105. level > debug_threshold )
  106. {
  107. return;
  108. }
  109. va_start( argp, format );
  110. #if defined(_WIN32)
  111. #if defined(_TRUNCATE) && !defined(__MINGW32__)
  112. ret = _vsnprintf_s( str, DEBUG_BUF_SIZE, _TRUNCATE, format, argp );
  113. #else
  114. ret = _vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
  115. if( ret < 0 || (size_t) ret == DEBUG_BUF_SIZE )
  116. {
  117. str[DEBUG_BUF_SIZE-1] = '\0';
  118. ret = -1;
  119. }
  120. #endif
  121. #else
  122. ret = vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
  123. #endif
  124. va_end( argp );
  125. if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 )
  126. {
  127. str[ret] = '\n';
  128. str[ret + 1] = '\0';
  129. }
  130. debug_send_line( ssl, level, file, line, str );
  131. }
  132. void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
  133. const char *file, int line,
  134. const char *text, int ret )
  135. {
  136. char str[DEBUG_BUF_SIZE];
  137. if( NULL == ssl ||
  138. NULL == ssl->conf ||
  139. NULL == ssl->conf->f_dbg ||
  140. level > debug_threshold )
  141. {
  142. return;
  143. }
  144. /*
  145. * With non-blocking I/O and examples that just retry immediately,
  146. * the logs would be quickly flooded with WANT_READ, so ignore that.
  147. * Don't ignore WANT_WRITE however, since is is usually rare.
  148. */
  149. if( ret == MBEDTLS_ERR_SSL_WANT_READ )
  150. return;
  151. mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n",
  152. text, ret, -ret );
  153. debug_send_line( ssl, level, file, line, str );
  154. }
  155. void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
  156. const char *file, int line, const char *text,
  157. const unsigned char *buf, size_t len )
  158. {
  159. char str[DEBUG_BUF_SIZE];
  160. char txt[17];
  161. size_t i, idx = 0;
  162. if( NULL == ssl ||
  163. NULL == ssl->conf ||
  164. NULL == ssl->conf->f_dbg ||
  165. level > debug_threshold )
  166. {
  167. return;
  168. }
  169. mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n",
  170. text, (unsigned int) len );
  171. debug_send_line( ssl, level, file, line, str );
  172. idx = 0;
  173. memset( txt, 0, sizeof( txt ) );
  174. for( i = 0; i < len; i++ )
  175. {
  176. if( i >= 4096 )
  177. break;
  178. if( i % 16 == 0 )
  179. {
  180. if( i > 0 )
  181. {
  182. mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
  183. debug_send_line( ssl, level, file, line, str );
  184. idx = 0;
  185. memset( txt, 0, sizeof( txt ) );
  186. }
  187. idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ",
  188. (unsigned int) i );
  189. }
  190. idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x",
  191. (unsigned int) buf[i] );
  192. txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
  193. }
  194. if( len > 0 )
  195. {
  196. for( /* i = i */; i % 16 != 0; i++ )
  197. idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " );
  198. mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
  199. debug_send_line( ssl, level, file, line, str );
  200. }
  201. }
  202. #if defined(MBEDTLS_ECP_C)
  203. void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
  204. const char *file, int line,
  205. const char *text, const mbedtls_ecp_point *X )
  206. {
  207. char str[DEBUG_BUF_SIZE];
  208. if( NULL == ssl ||
  209. NULL == ssl->conf ||
  210. NULL == ssl->conf->f_dbg ||
  211. level > debug_threshold )
  212. {
  213. return;
  214. }
  215. mbedtls_snprintf( str, sizeof( str ), "%s(X)", text );
  216. mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
  217. mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text );
  218. mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
  219. }
  220. #endif /* MBEDTLS_ECP_C */
  221. #if defined(MBEDTLS_BIGNUM_C)
  222. void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
  223. const char *file, int line,
  224. const char *text, const mbedtls_mpi *X )
  225. {
  226. char str[DEBUG_BUF_SIZE];
  227. size_t bitlen;
  228. size_t idx = 0;
  229. if( NULL == ssl ||
  230. NULL == ssl->conf ||
  231. NULL == ssl->conf->f_dbg ||
  232. NULL == X ||
  233. level > debug_threshold )
  234. {
  235. return;
  236. }
  237. bitlen = mbedtls_mpi_bitlen( X );
  238. mbedtls_snprintf( str, sizeof( str ), "value of '%s' (%u bits) is:\n",
  239. text, (unsigned) bitlen );
  240. debug_send_line( ssl, level, file, line, str );
  241. if( bitlen == 0 )
  242. {
  243. str[0] = ' '; str[1] = '0'; str[2] = '0';
  244. idx = 3;
  245. }
  246. else
  247. {
  248. int n;
  249. for( n = (int) ( ( bitlen - 1 ) / 8 ); n >= 0; n-- )
  250. {
  251. size_t limb_offset = n / sizeof( mbedtls_mpi_uint );
  252. size_t offset_in_limb = n % sizeof( mbedtls_mpi_uint );
  253. unsigned char octet =
  254. ( X->p[limb_offset] >> ( offset_in_limb * 8 ) ) & 0xff;
  255. mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", octet );
  256. idx += 3;
  257. /* Wrap lines after 16 octets that each take 3 columns */
  258. if( idx >= 3 * 16 )
  259. {
  260. mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
  261. debug_send_line( ssl, level, file, line, str );
  262. idx = 0;
  263. }
  264. }
  265. }
  266. if( idx != 0 )
  267. {
  268. mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
  269. debug_send_line( ssl, level, file, line, str );
  270. }
  271. }
  272. #endif /* MBEDTLS_BIGNUM_C */
  273. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  274. static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
  275. const char *file, int line,
  276. const char *text, const mbedtls_pk_context *pk )
  277. {
  278. size_t i;
  279. mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
  280. char name[16];
  281. memset( items, 0, sizeof( items ) );
  282. if( mbedtls_pk_debug( pk, items ) != 0 )
  283. {
  284. debug_send_line( ssl, level, file, line,
  285. "invalid PK context\n" );
  286. return;
  287. }
  288. for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
  289. {
  290. if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
  291. return;
  292. mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
  293. name[sizeof( name ) - 1] = '\0';
  294. if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
  295. mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
  296. else
  297. #if defined(MBEDTLS_ECP_C)
  298. if( items[i].type == MBEDTLS_PK_DEBUG_ECP )
  299. mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value );
  300. else
  301. #endif
  302. debug_send_line( ssl, level, file, line,
  303. "should not happen\n" );
  304. }
  305. }
  306. static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level,
  307. const char *file, int line, const char *text )
  308. {
  309. char str[DEBUG_BUF_SIZE];
  310. const char *start, *cur;
  311. start = text;
  312. for( cur = text; *cur != '\0'; cur++ )
  313. {
  314. if( *cur == '\n' )
  315. {
  316. size_t len = cur - start + 1;
  317. if( len > DEBUG_BUF_SIZE - 1 )
  318. len = DEBUG_BUF_SIZE - 1;
  319. memcpy( str, start, len );
  320. str[len] = '\0';
  321. debug_send_line( ssl, level, file, line, str );
  322. start = cur + 1;
  323. }
  324. }
  325. }
  326. void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
  327. const char *file, int line,
  328. const char *text, const mbedtls_x509_crt *crt )
  329. {
  330. char str[DEBUG_BUF_SIZE];
  331. int i = 0;
  332. if( NULL == ssl ||
  333. NULL == ssl->conf ||
  334. NULL == ssl->conf->f_dbg ||
  335. NULL == crt ||
  336. level > debug_threshold )
  337. {
  338. return;
  339. }
  340. while( crt != NULL )
  341. {
  342. char buf[1024];
  343. mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i );
  344. debug_send_line( ssl, level, file, line, str );
  345. mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
  346. debug_print_line_by_line( ssl, level, file, line, buf );
  347. debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
  348. crt = crt->next;
  349. }
  350. }
  351. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  352. #if defined(MBEDTLS_ECDH_C)
  353. static void mbedtls_debug_printf_ecdh_internal( const mbedtls_ssl_context *ssl,
  354. int level, const char *file,
  355. int line,
  356. const mbedtls_ecdh_context *ecdh,
  357. mbedtls_debug_ecdh_attr attr )
  358. {
  359. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  360. const mbedtls_ecdh_context* ctx = ecdh;
  361. #else
  362. const mbedtls_ecdh_context_mbed* ctx = &ecdh->ctx.mbed_ecdh;
  363. #endif
  364. switch( attr )
  365. {
  366. case MBEDTLS_DEBUG_ECDH_Q:
  367. mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Q",
  368. &ctx->Q );
  369. break;
  370. case MBEDTLS_DEBUG_ECDH_QP:
  371. mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Qp",
  372. &ctx->Qp );
  373. break;
  374. case MBEDTLS_DEBUG_ECDH_Z:
  375. mbedtls_debug_print_mpi( ssl, level, file, line, "ECDH: z",
  376. &ctx->z );
  377. break;
  378. default:
  379. break;
  380. }
  381. }
  382. void mbedtls_debug_printf_ecdh( const mbedtls_ssl_context *ssl, int level,
  383. const char *file, int line,
  384. const mbedtls_ecdh_context *ecdh,
  385. mbedtls_debug_ecdh_attr attr )
  386. {
  387. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  388. mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh, attr );
  389. #else
  390. switch( ecdh->var )
  391. {
  392. default:
  393. mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh,
  394. attr );
  395. }
  396. #endif
  397. }
  398. #endif /* MBEDTLS_ECDH_C */
  399. #endif /* MBEDTLS_DEBUG_C */