debug.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. int j, k, zeros = 1;
  228. size_t i, n, 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. for( n = X->n - 1; n > 0; n-- )
  238. if( X->p[n] != 0 )
  239. break;
  240. for( j = ( sizeof(mbedtls_mpi_uint) << 3 ) - 1; j >= 0; j-- )
  241. if( ( ( X->p[n] >> j ) & 1 ) != 0 )
  242. break;
  243. mbedtls_snprintf( str + idx, sizeof( str ) - idx, "value of '%s' (%d bits) is:\n",
  244. text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) );
  245. debug_send_line( ssl, level, file, line, str );
  246. idx = 0;
  247. for( i = n + 1, j = 0; i > 0; i-- )
  248. {
  249. if( zeros && X->p[i - 1] == 0 )
  250. continue;
  251. for( k = sizeof( mbedtls_mpi_uint ) - 1; k >= 0; k-- )
  252. {
  253. if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
  254. continue;
  255. else
  256. zeros = 0;
  257. if( j % 16 == 0 )
  258. {
  259. if( j > 0 )
  260. {
  261. mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
  262. debug_send_line( ssl, level, file, line, str );
  263. idx = 0;
  264. }
  265. }
  266. idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", (unsigned int)
  267. ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
  268. j++;
  269. }
  270. }
  271. if( zeros == 1 )
  272. idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " 00" );
  273. mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
  274. debug_send_line( ssl, level, file, line, str );
  275. }
  276. #endif /* MBEDTLS_BIGNUM_C */
  277. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  278. static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
  279. const char *file, int line,
  280. const char *text, const mbedtls_pk_context *pk )
  281. {
  282. size_t i;
  283. mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
  284. char name[16];
  285. memset( items, 0, sizeof( items ) );
  286. if( mbedtls_pk_debug( pk, items ) != 0 )
  287. {
  288. debug_send_line( ssl, level, file, line,
  289. "invalid PK context\n" );
  290. return;
  291. }
  292. for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
  293. {
  294. if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
  295. return;
  296. mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
  297. name[sizeof( name ) - 1] = '\0';
  298. if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
  299. mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
  300. else
  301. #if defined(MBEDTLS_ECP_C)
  302. if( items[i].type == MBEDTLS_PK_DEBUG_ECP )
  303. mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value );
  304. else
  305. #endif
  306. debug_send_line( ssl, level, file, line,
  307. "should not happen\n" );
  308. }
  309. }
  310. static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level,
  311. const char *file, int line, const char *text )
  312. {
  313. char str[DEBUG_BUF_SIZE];
  314. const char *start, *cur;
  315. start = text;
  316. for( cur = text; *cur != '\0'; cur++ )
  317. {
  318. if( *cur == '\n' )
  319. {
  320. size_t len = cur - start + 1;
  321. if( len > DEBUG_BUF_SIZE - 1 )
  322. len = DEBUG_BUF_SIZE - 1;
  323. memcpy( str, start, len );
  324. str[len] = '\0';
  325. debug_send_line( ssl, level, file, line, str );
  326. start = cur + 1;
  327. }
  328. }
  329. }
  330. void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
  331. const char *file, int line,
  332. const char *text, const mbedtls_x509_crt *crt )
  333. {
  334. char str[DEBUG_BUF_SIZE];
  335. int i = 0;
  336. if( NULL == ssl ||
  337. NULL == ssl->conf ||
  338. NULL == ssl->conf->f_dbg ||
  339. NULL == crt ||
  340. level > debug_threshold )
  341. {
  342. return;
  343. }
  344. while( crt != NULL )
  345. {
  346. char buf[1024];
  347. mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i );
  348. debug_send_line( ssl, level, file, line, str );
  349. mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
  350. debug_print_line_by_line( ssl, level, file, line, buf );
  351. debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
  352. crt = crt->next;
  353. }
  354. }
  355. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  356. #if defined(MBEDTLS_ECDH_C)
  357. static void mbedtls_debug_printf_ecdh_internal( const mbedtls_ssl_context *ssl,
  358. int level, const char *file,
  359. int line,
  360. const mbedtls_ecdh_context *ecdh,
  361. mbedtls_debug_ecdh_attr attr )
  362. {
  363. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  364. const mbedtls_ecdh_context* ctx = ecdh;
  365. #else
  366. const mbedtls_ecdh_context_mbed* ctx = &ecdh->ctx.mbed_ecdh;
  367. #endif
  368. switch( attr )
  369. {
  370. case MBEDTLS_DEBUG_ECDH_Q:
  371. mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Q",
  372. &ctx->Q );
  373. break;
  374. case MBEDTLS_DEBUG_ECDH_QP:
  375. mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Qp",
  376. &ctx->Qp );
  377. break;
  378. case MBEDTLS_DEBUG_ECDH_Z:
  379. mbedtls_debug_print_mpi( ssl, level, file, line, "ECDH: z",
  380. &ctx->z );
  381. break;
  382. default:
  383. break;
  384. }
  385. }
  386. void mbedtls_debug_printf_ecdh( const mbedtls_ssl_context *ssl, int level,
  387. const char *file, int line,
  388. const mbedtls_ecdh_context *ecdh,
  389. mbedtls_debug_ecdh_attr attr )
  390. {
  391. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  392. mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh, attr );
  393. #else
  394. switch( ecdh->var )
  395. {
  396. default:
  397. mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh,
  398. attr );
  399. }
  400. #endif
  401. }
  402. #endif /* MBEDTLS_ECDH_C */
  403. #endif /* MBEDTLS_DEBUG_C */