Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

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