debug.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * Debugging routines
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  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. * This file is part of mbed TLS (https://tls.mbed.org)
  47. */
  48. #if !defined(MBEDTLS_CONFIG_FILE)
  49. #include "mbedtls/config.h"
  50. #else
  51. #include MBEDTLS_CONFIG_FILE
  52. #endif
  53. #if defined(MBEDTLS_DEBUG_C)
  54. #if defined(MBEDTLS_PLATFORM_C)
  55. #include "mbedtls/platform.h"
  56. #else
  57. #include <stdlib.h>
  58. #define mbedtls_calloc calloc
  59. #define mbedtls_free free
  60. #define mbedtls_time_t time_t
  61. #define mbedtls_snprintf snprintf
  62. #endif
  63. #include "mbedtls/debug.h"
  64. #include <stdarg.h>
  65. #include <stdio.h>
  66. #include <string.h>
  67. #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
  68. !defined(inline) && !defined(__cplusplus)
  69. #define inline __inline
  70. #endif
  71. #define DEBUG_BUF_SIZE 512
  72. static int debug_threshold = 0;
  73. void mbedtls_debug_set_threshold( int threshold )
  74. {
  75. debug_threshold = threshold;
  76. }
  77. /*
  78. * All calls to f_dbg must be made via this function
  79. */
  80. static inline void debug_send_line( const mbedtls_ssl_context *ssl, int level,
  81. const char *file, int line,
  82. const char *str )
  83. {
  84. /*
  85. * If in a threaded environment, we need a thread identifier.
  86. * Since there is no portable way to get one, use the address of the ssl
  87. * context instead, as it shouldn't be shared between threads.
  88. */
  89. #if defined(MBEDTLS_THREADING_C)
  90. char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */
  91. mbedtls_snprintf( idstr, sizeof( idstr ), "%p: %s", (void*)ssl, str );
  92. ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, idstr );
  93. #else
  94. ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
  95. #endif
  96. }
  97. void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
  98. const char *file, int line,
  99. const char *format, ... )
  100. {
  101. va_list argp;
  102. char str[DEBUG_BUF_SIZE];
  103. int ret;
  104. if( NULL == ssl ||
  105. NULL == ssl->conf ||
  106. NULL == ssl->conf->f_dbg ||
  107. level > debug_threshold )
  108. {
  109. return;
  110. }
  111. va_start( argp, format );
  112. #if defined(_WIN32)
  113. #if defined(_TRUNCATE) && !defined(__MINGW32__)
  114. ret = _vsnprintf_s( str, DEBUG_BUF_SIZE, _TRUNCATE, format, argp );
  115. #else
  116. ret = _vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
  117. if( ret < 0 || (size_t) ret == DEBUG_BUF_SIZE )
  118. {
  119. str[DEBUG_BUF_SIZE-1] = '\0';
  120. ret = -1;
  121. }
  122. #endif
  123. #else
  124. ret = vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
  125. #endif
  126. va_end( argp );
  127. if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 )
  128. {
  129. str[ret] = '\n';
  130. str[ret + 1] = '\0';
  131. }
  132. debug_send_line( ssl, level, file, line, str );
  133. }
  134. void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
  135. const char *file, int line,
  136. const char *text, int ret )
  137. {
  138. char str[DEBUG_BUF_SIZE];
  139. if( NULL == ssl ||
  140. NULL == ssl->conf ||
  141. NULL == ssl->conf->f_dbg ||
  142. level > debug_threshold )
  143. {
  144. return;
  145. }
  146. /*
  147. * With non-blocking I/O and examples that just retry immediately,
  148. * the logs would be quickly flooded with WANT_READ, so ignore that.
  149. * Don't ignore WANT_WRITE however, since is is usually rare.
  150. */
  151. if( ret == MBEDTLS_ERR_SSL_WANT_READ )
  152. return;
  153. mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n",
  154. text, ret, -ret );
  155. debug_send_line( ssl, level, file, line, str );
  156. }
  157. void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
  158. const char *file, int line, const char *text,
  159. const unsigned char *buf, size_t len )
  160. {
  161. char str[DEBUG_BUF_SIZE];
  162. char txt[17];
  163. size_t i, idx = 0;
  164. if( NULL == ssl ||
  165. NULL == ssl->conf ||
  166. NULL == ssl->conf->f_dbg ||
  167. level > debug_threshold )
  168. {
  169. return;
  170. }
  171. mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n",
  172. text, (unsigned int) len );
  173. debug_send_line( ssl, level, file, line, str );
  174. idx = 0;
  175. memset( txt, 0, sizeof( txt ) );
  176. for( i = 0; i < len; i++ )
  177. {
  178. if( i >= 4096 )
  179. break;
  180. if( i % 16 == 0 )
  181. {
  182. if( i > 0 )
  183. {
  184. mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
  185. debug_send_line( ssl, level, file, line, str );
  186. idx = 0;
  187. memset( txt, 0, sizeof( txt ) );
  188. }
  189. idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ",
  190. (unsigned int) i );
  191. }
  192. idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x",
  193. (unsigned int) buf[i] );
  194. txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
  195. }
  196. if( len > 0 )
  197. {
  198. for( /* i = i */; i % 16 != 0; i++ )
  199. idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " );
  200. mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
  201. debug_send_line( ssl, level, file, line, str );
  202. }
  203. }
  204. #if defined(MBEDTLS_ECP_C)
  205. void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
  206. const char *file, int line,
  207. const char *text, const mbedtls_ecp_point *X )
  208. {
  209. char str[DEBUG_BUF_SIZE];
  210. if( NULL == ssl ||
  211. NULL == ssl->conf ||
  212. NULL == ssl->conf->f_dbg ||
  213. level > debug_threshold )
  214. {
  215. return;
  216. }
  217. mbedtls_snprintf( str, sizeof( str ), "%s(X)", text );
  218. mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
  219. mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text );
  220. mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
  221. }
  222. #endif /* MBEDTLS_ECP_C */
  223. #if defined(MBEDTLS_BIGNUM_C)
  224. void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
  225. const char *file, int line,
  226. const char *text, const mbedtls_mpi *X )
  227. {
  228. char str[DEBUG_BUF_SIZE];
  229. int j, k, zeros = 1;
  230. size_t i, n, idx = 0;
  231. if( NULL == ssl ||
  232. NULL == ssl->conf ||
  233. NULL == ssl->conf->f_dbg ||
  234. NULL == X ||
  235. level > debug_threshold )
  236. {
  237. return;
  238. }
  239. for( n = X->n - 1; n > 0; n-- )
  240. if( X->p[n] != 0 )
  241. break;
  242. for( j = ( sizeof(mbedtls_mpi_uint) << 3 ) - 1; j >= 0; j-- )
  243. if( ( ( X->p[n] >> j ) & 1 ) != 0 )
  244. break;
  245. mbedtls_snprintf( str + idx, sizeof( str ) - idx, "value of '%s' (%d bits) is:\n",
  246. text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) );
  247. debug_send_line( ssl, level, file, line, str );
  248. idx = 0;
  249. for( i = n + 1, j = 0; i > 0; i-- )
  250. {
  251. if( zeros && X->p[i - 1] == 0 )
  252. continue;
  253. for( k = sizeof( mbedtls_mpi_uint ) - 1; k >= 0; k-- )
  254. {
  255. if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
  256. continue;
  257. else
  258. zeros = 0;
  259. if( j % 16 == 0 )
  260. {
  261. if( j > 0 )
  262. {
  263. mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
  264. debug_send_line( ssl, level, file, line, str );
  265. idx = 0;
  266. }
  267. }
  268. idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", (unsigned int)
  269. ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
  270. j++;
  271. }
  272. }
  273. if( zeros == 1 )
  274. idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " 00" );
  275. mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
  276. debug_send_line( ssl, level, file, line, str );
  277. }
  278. #endif /* MBEDTLS_BIGNUM_C */
  279. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  280. static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
  281. const char *file, int line,
  282. const char *text, const mbedtls_pk_context *pk )
  283. {
  284. size_t i;
  285. mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
  286. char name[16];
  287. memset( items, 0, sizeof( items ) );
  288. if( mbedtls_pk_debug( pk, items ) != 0 )
  289. {
  290. debug_send_line( ssl, level, file, line,
  291. "invalid PK context\n" );
  292. return;
  293. }
  294. for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
  295. {
  296. if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
  297. return;
  298. mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
  299. name[sizeof( name ) - 1] = '\0';
  300. if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
  301. mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
  302. else
  303. #if defined(MBEDTLS_ECP_C)
  304. if( items[i].type == MBEDTLS_PK_DEBUG_ECP )
  305. mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value );
  306. else
  307. #endif
  308. debug_send_line( ssl, level, file, line,
  309. "should not happen\n" );
  310. }
  311. }
  312. static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level,
  313. const char *file, int line, const char *text )
  314. {
  315. char str[DEBUG_BUF_SIZE];
  316. const char *start, *cur;
  317. start = text;
  318. for( cur = text; *cur != '\0'; cur++ )
  319. {
  320. if( *cur == '\n' )
  321. {
  322. size_t len = cur - start + 1;
  323. if( len > DEBUG_BUF_SIZE - 1 )
  324. len = DEBUG_BUF_SIZE - 1;
  325. memcpy( str, start, len );
  326. str[len] = '\0';
  327. debug_send_line( ssl, level, file, line, str );
  328. start = cur + 1;
  329. }
  330. }
  331. }
  332. void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
  333. const char *file, int line,
  334. const char *text, const mbedtls_x509_crt *crt )
  335. {
  336. char str[DEBUG_BUF_SIZE];
  337. int i = 0;
  338. if( NULL == ssl ||
  339. NULL == ssl->conf ||
  340. NULL == ssl->conf->f_dbg ||
  341. NULL == crt ||
  342. level > debug_threshold )
  343. {
  344. return;
  345. }
  346. while( crt != NULL )
  347. {
  348. char buf[1024];
  349. mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i );
  350. debug_send_line( ssl, level, file, line, str );
  351. mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
  352. debug_print_line_by_line( ssl, level, file, line, buf );
  353. debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
  354. crt = crt->next;
  355. }
  356. }
  357. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  358. #if defined(MBEDTLS_ECDH_C)
  359. static void mbedtls_debug_printf_ecdh_internal( const mbedtls_ssl_context *ssl,
  360. int level, const char *file,
  361. int line,
  362. const mbedtls_ecdh_context *ecdh,
  363. mbedtls_debug_ecdh_attr attr )
  364. {
  365. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  366. const mbedtls_ecdh_context* ctx = ecdh;
  367. #else
  368. const mbedtls_ecdh_context_mbed* ctx = &ecdh->ctx.mbed_ecdh;
  369. #endif
  370. switch( attr )
  371. {
  372. case MBEDTLS_DEBUG_ECDH_Q:
  373. mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Q",
  374. &ctx->Q );
  375. break;
  376. case MBEDTLS_DEBUG_ECDH_QP:
  377. mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Qp",
  378. &ctx->Qp );
  379. break;
  380. case MBEDTLS_DEBUG_ECDH_Z:
  381. mbedtls_debug_print_mpi( ssl, level, file, line, "ECDH: z",
  382. &ctx->z );
  383. break;
  384. default:
  385. break;
  386. }
  387. }
  388. void mbedtls_debug_printf_ecdh( const mbedtls_ssl_context *ssl, int level,
  389. const char *file, int line,
  390. const mbedtls_ecdh_context *ecdh,
  391. mbedtls_debug_ecdh_attr attr )
  392. {
  393. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  394. mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh, attr );
  395. #else
  396. switch( ecdh->var )
  397. {
  398. default:
  399. mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh,
  400. attr );
  401. }
  402. #endif
  403. }
  404. #endif /* MBEDTLS_ECDH_C */
  405. #endif /* MBEDTLS_DEBUG_C */