debug.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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. #include "mbedtls/platform.h"
  22. #include "mbedtls/debug.h"
  23. #include "mbedtls/error.h"
  24. #include <stdarg.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #define DEBUG_BUF_SIZE 512
  28. static int debug_threshold = 0;
  29. void mbedtls_debug_set_threshold( int threshold )
  30. {
  31. debug_threshold = threshold;
  32. }
  33. /*
  34. * All calls to f_dbg must be made via this function
  35. */
  36. static inline void debug_send_line( const mbedtls_ssl_context *ssl, int level,
  37. const char *file, int line,
  38. const char *str )
  39. {
  40. /*
  41. * If in a threaded environment, we need a thread identifier.
  42. * Since there is no portable way to get one, use the address of the ssl
  43. * context instead, as it shouldn't be shared between threads.
  44. */
  45. #if defined(MBEDTLS_THREADING_C)
  46. char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */
  47. mbedtls_snprintf( idstr, sizeof( idstr ), "%p: %s", (void*)ssl, str );
  48. ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, idstr );
  49. #else
  50. ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
  51. #endif
  52. }
  53. #ifdef __SMALL_RAM__
  54. MBEDTLS_PRINTF_ATTRIBUTE(5, 6)
  55. void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
  56. const char *file, int line,
  57. const char *format, ... )
  58. {
  59. va_list argp;
  60. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  61. if( NULL == ssl ||
  62. NULL == ssl->conf ||
  63. NULL == ssl->conf->f_dbg ||
  64. level > debug_threshold )
  65. {
  66. return;
  67. }
  68. char *str = mbedtls_calloc(1, DEBUG_BUF_SIZE);
  69. va_start( argp, format );
  70. ret = mbedtls_vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
  71. va_end( argp );
  72. if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 )
  73. {
  74. #ifdef LUAT_LOG_NO_NEWLINE
  75. #else
  76. str[ret] = '\n';
  77. str[ret + 1] = '\0';
  78. #endif
  79. }
  80. debug_send_line( ssl, level, file, line, str );
  81. mbedtls_free(str);
  82. }
  83. void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
  84. const char *file, int line,
  85. const char *text, int ret )
  86. {
  87. if( NULL == ssl ||
  88. NULL == ssl->conf ||
  89. NULL == ssl->conf->f_dbg ||
  90. MBEDTLS_ERR_SSL_WANT_READ == ret ||
  91. level > debug_threshold )
  92. {
  93. return;
  94. }
  95. char *str = mbedtls_calloc(1, DEBUG_BUF_SIZE);
  96. /*
  97. * With non-blocking I/O and examples that just retry immediately,
  98. * the logs would be quickly flooded with WANT_READ, so ignore that.
  99. * Don't ignore WANT_WRITE however, since is is usually rare.
  100. */
  101. #ifdef LUAT_LOG_NO_NEWLINE
  102. mbedtls_snprintf( str, DEBUG_BUF_SIZE, "%s() returned %d (-0x%04x)",
  103. text, ret, (unsigned int) -ret );
  104. #else
  105. mbedtls_snprintf( str, DEBUG_BUF_SIZE, "%s() returned %d (-0x%04x)\n",
  106. text, ret, (unsigned int) -ret );
  107. #endif
  108. debug_send_line( ssl, level, file, line, str );
  109. mbedtls_free(str);
  110. }
  111. void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
  112. const char *file, int line, const char *text,
  113. const unsigned char *buf, size_t len )
  114. {
  115. char txt[17];
  116. size_t i, idx = 0;
  117. if( NULL == ssl ||
  118. NULL == ssl->conf ||
  119. NULL == ssl->conf->f_dbg ||
  120. level > debug_threshold )
  121. {
  122. return;
  123. }
  124. char *str = mbedtls_calloc(1, DEBUG_BUF_SIZE + 8);
  125. mbedtls_snprintf( str + idx, DEBUG_BUF_SIZE - idx, "dumping '%s' (%u bytes)\n",
  126. text, (unsigned int) len );
  127. debug_send_line( ssl, level, file, line, str );
  128. idx = 0;
  129. memset( txt, 0, sizeof( txt ) );
  130. for( i = 0; i < len; i++ )
  131. {
  132. if( i >= 256 )
  133. break;
  134. if( i % 16 == 0 )
  135. {
  136. if( i > 0 )
  137. {
  138. mbedtls_snprintf( str + idx, DEBUG_BUF_SIZE - idx, " %s\n", txt );
  139. debug_send_line( ssl, level, file, line, str );
  140. idx = 0;
  141. memset( txt, 0, sizeof( txt ) );
  142. }
  143. idx += mbedtls_snprintf( str + idx, DEBUG_BUF_SIZE - idx, "%04x: ",
  144. (unsigned int) i );
  145. }
  146. idx += mbedtls_snprintf( str + idx, DEBUG_BUF_SIZE - idx, " %02x",
  147. (unsigned int) buf[i] );
  148. txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
  149. }
  150. if( len > 0 )
  151. {
  152. for( /* i = i */; i % 16 != 0; i++ )
  153. idx += mbedtls_snprintf( str + idx, DEBUG_BUF_SIZE - idx, " " );
  154. mbedtls_snprintf( str + idx, DEBUG_BUF_SIZE - idx, " %s\n", txt );
  155. debug_send_line( ssl, level, file, line, str );
  156. }
  157. mbedtls_free(str);
  158. }
  159. #if defined(MBEDTLS_ECP_C)
  160. void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
  161. const char *file, int line,
  162. const char *text, const mbedtls_ecp_point *X )
  163. {
  164. if( NULL == ssl ||
  165. NULL == ssl->conf ||
  166. NULL == ssl->conf->f_dbg ||
  167. level > debug_threshold )
  168. {
  169. return;
  170. }
  171. char *str = mbedtls_calloc(1, DEBUG_BUF_SIZE + 8);
  172. mbedtls_snprintf( str, DEBUG_BUF_SIZE, "%s(X)", text );
  173. mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
  174. mbedtls_snprintf( str, DEBUG_BUF_SIZE, "%s(Y)", text );
  175. mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
  176. mbedtls_free(str);
  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. size_t bitlen;
  185. size_t idx = 0;
  186. if( NULL == ssl ||
  187. NULL == ssl->conf ||
  188. NULL == ssl->conf->f_dbg ||
  189. NULL == X ||
  190. level > debug_threshold )
  191. {
  192. return;
  193. }
  194. char *str = mbedtls_calloc(1, DEBUG_BUF_SIZE + 8);
  195. bitlen = mbedtls_mpi_bitlen( X );
  196. mbedtls_snprintf( str, DEBUG_BUF_SIZE, "value of '%s' (%u bits) is:\n",
  197. text, (unsigned) bitlen );
  198. debug_send_line( ssl, level, file, line, str );
  199. if( bitlen == 0 )
  200. {
  201. str[0] = ' '; str[1] = '0'; str[2] = '0';
  202. idx = 3;
  203. }
  204. else
  205. {
  206. int n;
  207. for( n = (int) ( ( bitlen - 1 ) / 8 ); n >= 0; n-- )
  208. {
  209. size_t limb_offset = n / sizeof( mbedtls_mpi_uint );
  210. size_t offset_in_limb = n % sizeof( mbedtls_mpi_uint );
  211. unsigned char octet =
  212. ( X->p[limb_offset] >> ( offset_in_limb * 8 ) ) & 0xff;
  213. mbedtls_snprintf( str + idx, DEBUG_BUF_SIZE - idx, " %02x", octet );
  214. idx += 3;
  215. /* Wrap lines after 16 octets that each take 3 columns */
  216. if( idx >= 3 * 16 )
  217. {
  218. mbedtls_snprintf( str + idx, DEBUG_BUF_SIZE - idx, "\n" );
  219. debug_send_line( ssl, level, file, line, str );
  220. idx = 0;
  221. }
  222. }
  223. }
  224. if( idx != 0 )
  225. {
  226. mbedtls_snprintf( str + idx, DEBUG_BUF_SIZE - idx, "\n" );
  227. debug_send_line( ssl, level, file, line, str );
  228. }
  229. mbedtls_free(str);
  230. }
  231. #endif /* MBEDTLS_BIGNUM_C */
  232. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  233. static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
  234. const char *file, int line,
  235. const char *text, const mbedtls_pk_context *pk )
  236. {
  237. size_t i;
  238. mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
  239. char name[16];
  240. memset( items, 0, sizeof( items ) );
  241. if( mbedtls_pk_debug( pk, items ) != 0 )
  242. {
  243. debug_send_line( ssl, level, file, line,
  244. "invalid PK context\n" );
  245. return;
  246. }
  247. for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
  248. {
  249. if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
  250. return;
  251. mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
  252. name[sizeof( name ) - 1] = '\0';
  253. if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
  254. mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
  255. else
  256. #if defined(MBEDTLS_ECP_C)
  257. if( items[i].type == MBEDTLS_PK_DEBUG_ECP )
  258. mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value );
  259. else
  260. #endif
  261. debug_send_line( ssl, level, file, line,
  262. "should not happen\n" );
  263. }
  264. }
  265. static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level,
  266. const char *file, int line, const char *text )
  267. {
  268. char *str = mbedtls_calloc(1, DEBUG_BUF_SIZE);
  269. const char *start, *cur;
  270. start = text;
  271. for( cur = text; *cur != '\0'; cur++ )
  272. {
  273. if( *cur == '\n' )
  274. {
  275. size_t len = cur - start + 1;
  276. if( len > DEBUG_BUF_SIZE - 1 )
  277. len = DEBUG_BUF_SIZE - 1;
  278. memcpy( str, start, len );
  279. #ifdef LUAT_LOG_NO_NEWLINE
  280. str[len - 1] = '\0';
  281. #else
  282. str[len] = '\0';
  283. #endif
  284. debug_send_line( ssl, level, file, line, str );
  285. start = cur + 1;
  286. }
  287. }
  288. mbedtls_free(str);
  289. }
  290. void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
  291. const char *file, int line,
  292. const char *text, const mbedtls_x509_crt *crt )
  293. {
  294. int i = 0;
  295. if( NULL == ssl ||
  296. NULL == ssl->conf ||
  297. NULL == ssl->conf->f_dbg ||
  298. NULL == crt ||
  299. level > debug_threshold )
  300. {
  301. return;
  302. }
  303. char *str = mbedtls_calloc(1, DEBUG_BUF_SIZE);
  304. char *buf = mbedtls_calloc(1, 1024);
  305. while( crt != NULL )
  306. {
  307. mbedtls_snprintf( str, DEBUG_BUF_SIZE, "%s #%d:\n", text, ++i );
  308. debug_send_line( ssl, level, file, line, str );
  309. mbedtls_x509_crt_info( buf, 1024 - 1, "", crt );
  310. debug_print_line_by_line( ssl, level, file, line, buf );
  311. debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
  312. crt = crt->next;
  313. }
  314. mbedtls_free(str);
  315. mbedtls_free(buf);
  316. }
  317. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  318. #if defined(MBEDTLS_ECDH_C)
  319. static void mbedtls_debug_printf_ecdh_internal( const mbedtls_ssl_context *ssl,
  320. int level, const char *file,
  321. int line,
  322. const mbedtls_ecdh_context *ecdh,
  323. mbedtls_debug_ecdh_attr attr )
  324. {
  325. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  326. const mbedtls_ecdh_context* ctx = ecdh;
  327. #else
  328. const mbedtls_ecdh_context_mbed* ctx = &ecdh->ctx.mbed_ecdh;
  329. #endif
  330. switch( attr )
  331. {
  332. case MBEDTLS_DEBUG_ECDH_Q:
  333. mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Q",
  334. &ctx->Q );
  335. break;
  336. case MBEDTLS_DEBUG_ECDH_QP:
  337. mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Qp",
  338. &ctx->Qp );
  339. break;
  340. case MBEDTLS_DEBUG_ECDH_Z:
  341. mbedtls_debug_print_mpi( ssl, level, file, line, "ECDH: z",
  342. &ctx->z );
  343. break;
  344. default:
  345. break;
  346. }
  347. }
  348. void mbedtls_debug_printf_ecdh( const mbedtls_ssl_context *ssl, int level,
  349. const char *file, int line,
  350. const mbedtls_ecdh_context *ecdh,
  351. mbedtls_debug_ecdh_attr attr )
  352. {
  353. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  354. mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh, attr );
  355. #else
  356. switch( ecdh->var )
  357. {
  358. default:
  359. mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh,
  360. attr );
  361. }
  362. #endif
  363. }
  364. #endif /* MBEDTLS_ECDH_C */
  365. #else
  366. MBEDTLS_PRINTF_ATTRIBUTE(5, 6)
  367. void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
  368. const char *file, int line,
  369. const char *format, ... )
  370. {
  371. va_list argp;
  372. char str[DEBUG_BUF_SIZE];
  373. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  374. if( NULL == ssl ||
  375. NULL == ssl->conf ||
  376. NULL == ssl->conf->f_dbg ||
  377. level > debug_threshold )
  378. {
  379. return;
  380. }
  381. va_start( argp, format );
  382. ret = mbedtls_vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
  383. va_end( argp );
  384. if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 )
  385. {
  386. #ifdef LUAT_LOG_NO_NEWLINE
  387. #else
  388. str[ret] = '\n';
  389. str[ret + 1] = '\0';
  390. #endif
  391. }
  392. debug_send_line( ssl, level, file, line, str );
  393. }
  394. void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
  395. const char *file, int line,
  396. const char *text, int ret )
  397. {
  398. char str[DEBUG_BUF_SIZE];
  399. if( NULL == ssl ||
  400. NULL == ssl->conf ||
  401. NULL == ssl->conf->f_dbg ||
  402. level > debug_threshold )
  403. {
  404. return;
  405. }
  406. /*
  407. * With non-blocking I/O and examples that just retry immediately,
  408. * the logs would be quickly flooded with WANT_READ, so ignore that.
  409. * Don't ignore WANT_WRITE however, since is is usually rare.
  410. */
  411. if( ret == MBEDTLS_ERR_SSL_WANT_READ )
  412. return;
  413. #ifdef LUAT_LOG_NO_NEWLINE
  414. mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)",
  415. text, ret, (unsigned int) -ret );
  416. #else
  417. mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n",
  418. text, ret, (unsigned int) -ret );
  419. #endif
  420. debug_send_line( ssl, level, file, line, str );
  421. }
  422. void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
  423. const char *file, int line, const char *text,
  424. const unsigned char *buf, size_t len )
  425. {
  426. char str[DEBUG_BUF_SIZE];
  427. char txt[17];
  428. size_t i, idx = 0;
  429. if( NULL == ssl ||
  430. NULL == ssl->conf ||
  431. NULL == ssl->conf->f_dbg ||
  432. level > debug_threshold )
  433. {
  434. return;
  435. }
  436. mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n",
  437. text, (unsigned int) len );
  438. debug_send_line( ssl, level, file, line, str );
  439. idx = 0;
  440. memset( txt, 0, sizeof( txt ) );
  441. for( i = 0; i < len; i++ )
  442. {
  443. if( i >= 4096 )
  444. break;
  445. if( i % 16 == 0 )
  446. {
  447. if( i > 0 )
  448. {
  449. mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
  450. debug_send_line( ssl, level, file, line, str );
  451. idx = 0;
  452. memset( txt, 0, sizeof( txt ) );
  453. }
  454. idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ",
  455. (unsigned int) i );
  456. }
  457. idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x",
  458. (unsigned int) buf[i] );
  459. txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
  460. }
  461. if( len > 0 )
  462. {
  463. for( /* i = i */; i % 16 != 0; i++ )
  464. idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " );
  465. mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
  466. debug_send_line( ssl, level, file, line, str );
  467. }
  468. }
  469. #if defined(MBEDTLS_ECP_C)
  470. void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
  471. const char *file, int line,
  472. const char *text, const mbedtls_ecp_point *X )
  473. {
  474. char str[DEBUG_BUF_SIZE];
  475. if( NULL == ssl ||
  476. NULL == ssl->conf ||
  477. NULL == ssl->conf->f_dbg ||
  478. level > debug_threshold )
  479. {
  480. return;
  481. }
  482. mbedtls_snprintf( str, sizeof( str ), "%s(X)", text );
  483. mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
  484. mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text );
  485. mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
  486. }
  487. #endif /* MBEDTLS_ECP_C */
  488. #if defined(MBEDTLS_BIGNUM_C)
  489. void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
  490. const char *file, int line,
  491. const char *text, const mbedtls_mpi *X )
  492. {
  493. char str[DEBUG_BUF_SIZE];
  494. size_t bitlen;
  495. size_t idx = 0;
  496. if( NULL == ssl ||
  497. NULL == ssl->conf ||
  498. NULL == ssl->conf->f_dbg ||
  499. NULL == X ||
  500. level > debug_threshold )
  501. {
  502. return;
  503. }
  504. bitlen = mbedtls_mpi_bitlen( X );
  505. mbedtls_snprintf( str, sizeof( str ), "value of '%s' (%u bits) is:\n",
  506. text, (unsigned) bitlen );
  507. debug_send_line( ssl, level, file, line, str );
  508. if( bitlen == 0 )
  509. {
  510. str[0] = ' '; str[1] = '0'; str[2] = '0';
  511. idx = 3;
  512. }
  513. else
  514. {
  515. int n;
  516. for( n = (int) ( ( bitlen - 1 ) / 8 ); n >= 0; n-- )
  517. {
  518. size_t limb_offset = n / sizeof( mbedtls_mpi_uint );
  519. size_t offset_in_limb = n % sizeof( mbedtls_mpi_uint );
  520. unsigned char octet =
  521. ( X->p[limb_offset] >> ( offset_in_limb * 8 ) ) & 0xff;
  522. mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", octet );
  523. idx += 3;
  524. /* Wrap lines after 16 octets that each take 3 columns */
  525. if( idx >= 3 * 16 )
  526. {
  527. mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
  528. debug_send_line( ssl, level, file, line, str );
  529. idx = 0;
  530. }
  531. }
  532. }
  533. if( idx != 0 )
  534. {
  535. mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
  536. debug_send_line( ssl, level, file, line, str );
  537. }
  538. }
  539. #endif /* MBEDTLS_BIGNUM_C */
  540. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  541. static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
  542. const char *file, int line,
  543. const char *text, const mbedtls_pk_context *pk )
  544. {
  545. size_t i;
  546. mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
  547. char name[16];
  548. memset( items, 0, sizeof( items ) );
  549. if( mbedtls_pk_debug( pk, items ) != 0 )
  550. {
  551. debug_send_line( ssl, level, file, line,
  552. "invalid PK context\n" );
  553. return;
  554. }
  555. for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
  556. {
  557. if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
  558. return;
  559. mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
  560. name[sizeof( name ) - 1] = '\0';
  561. if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
  562. mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
  563. else
  564. #if defined(MBEDTLS_ECP_C)
  565. if( items[i].type == MBEDTLS_PK_DEBUG_ECP )
  566. mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value );
  567. else
  568. #endif
  569. debug_send_line( ssl, level, file, line,
  570. "should not happen\n" );
  571. }
  572. }
  573. static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level,
  574. const char *file, int line, const char *text )
  575. {
  576. char str[DEBUG_BUF_SIZE];
  577. const char *start, *cur;
  578. start = text;
  579. for( cur = text; *cur != '\0'; cur++ )
  580. {
  581. if( *cur == '\n' )
  582. {
  583. size_t len = cur - start + 1;
  584. if( len > DEBUG_BUF_SIZE - 1 )
  585. len = DEBUG_BUF_SIZE - 1;
  586. memcpy( str, start, len );
  587. #ifdef LUAT_LOG_NO_NEWLINE
  588. str[len - 1] = '\0';
  589. #else
  590. str[len] = '\0';
  591. #endif
  592. debug_send_line( ssl, level, file, line, str );
  593. start = cur + 1;
  594. }
  595. }
  596. }
  597. void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
  598. const char *file, int line,
  599. const char *text, const mbedtls_x509_crt *crt )
  600. {
  601. char str[DEBUG_BUF_SIZE];
  602. int i = 0;
  603. if( NULL == ssl ||
  604. NULL == ssl->conf ||
  605. NULL == ssl->conf->f_dbg ||
  606. NULL == crt ||
  607. level > debug_threshold )
  608. {
  609. return;
  610. }
  611. while( crt != NULL )
  612. {
  613. char buf[1024];
  614. mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i );
  615. debug_send_line( ssl, level, file, line, str );
  616. mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
  617. debug_print_line_by_line( ssl, level, file, line, buf );
  618. debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
  619. crt = crt->next;
  620. }
  621. }
  622. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  623. #if defined(MBEDTLS_ECDH_C)
  624. static void mbedtls_debug_printf_ecdh_internal( const mbedtls_ssl_context *ssl,
  625. int level, const char *file,
  626. int line,
  627. const mbedtls_ecdh_context *ecdh,
  628. mbedtls_debug_ecdh_attr attr )
  629. {
  630. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  631. const mbedtls_ecdh_context* ctx = ecdh;
  632. #else
  633. const mbedtls_ecdh_context_mbed* ctx = &ecdh->ctx.mbed_ecdh;
  634. #endif
  635. switch( attr )
  636. {
  637. case MBEDTLS_DEBUG_ECDH_Q:
  638. mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Q",
  639. &ctx->Q );
  640. break;
  641. case MBEDTLS_DEBUG_ECDH_QP:
  642. mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Qp",
  643. &ctx->Qp );
  644. break;
  645. case MBEDTLS_DEBUG_ECDH_Z:
  646. mbedtls_debug_print_mpi( ssl, level, file, line, "ECDH: z",
  647. &ctx->z );
  648. break;
  649. default:
  650. break;
  651. }
  652. }
  653. void mbedtls_debug_printf_ecdh( const mbedtls_ssl_context *ssl, int level,
  654. const char *file, int line,
  655. const mbedtls_ecdh_context *ecdh,
  656. mbedtls_debug_ecdh_attr attr )
  657. {
  658. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  659. mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh, attr );
  660. #else
  661. switch( ecdh->var )
  662. {
  663. default:
  664. mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh,
  665. attr );
  666. }
  667. #endif
  668. }
  669. #endif /* MBEDTLS_ECDH_C */
  670. #endif
  671. #endif /* MBEDTLS_DEBUG_C */