debug.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Debugging routines
  3. *
  4. * Copyright (C) 2006-2010, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. #include "config.h"
  26. #if defined(POLARSSL_DEBUG_C)
  27. #include "polarssl/debug.h"
  28. #include <stdarg.h>
  29. #include <stdlib.h>
  30. #if defined _MSC_VER && !defined snprintf
  31. #define snprintf _snprintf
  32. #endif
  33. #if defined _MSC_VER && !defined vsnprintf
  34. #define vsnprintf _vsnprintf
  35. #endif
  36. char *debug_fmt( const char *format, ... )
  37. {
  38. va_list argp;
  39. static char str[512];
  40. int maxlen = sizeof( str ) - 1;
  41. va_start( argp, format );
  42. vsnprintf( str, maxlen, format, argp );
  43. va_end( argp );
  44. str[maxlen] = '\0';
  45. return( str );
  46. }
  47. void debug_print_msg( const ssl_context *ssl, int level,
  48. const char *file, int line, const char *text )
  49. {
  50. char str[512];
  51. int maxlen = sizeof( str ) - 1;
  52. if( ssl->f_dbg == NULL )
  53. return;
  54. snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text );
  55. str[maxlen] = '\0';
  56. ssl->f_dbg( ssl->p_dbg, level, str );
  57. }
  58. void debug_print_ret( const ssl_context *ssl, int level,
  59. const char *file, int line,
  60. const char *text, int ret )
  61. {
  62. char str[512];
  63. int maxlen = sizeof( str ) - 1;
  64. if( ssl->f_dbg == NULL )
  65. return;
  66. snprintf( str, maxlen, "%s(%04d): %s() returned %d (0x%x)\n",
  67. file, line, text, ret, ret );
  68. str[maxlen] = '\0';
  69. ssl->f_dbg( ssl->p_dbg, level, str );
  70. }
  71. void debug_print_buf( const ssl_context *ssl, int level,
  72. const char *file, int line, const char *text,
  73. unsigned char *buf, size_t len )
  74. {
  75. char str[512];
  76. size_t i, maxlen = sizeof( str ) - 1;
  77. if( ssl->f_dbg == NULL )
  78. return;
  79. snprintf( str, maxlen, "%s(%04d): dumping '%s' (%d bytes)\n",
  80. file, line, text, (unsigned int) len );
  81. str[maxlen] = '\0';
  82. ssl->f_dbg( ssl->p_dbg, level, str );
  83. for( i = 0; i < len; i++ )
  84. {
  85. if( i >= 4096 )
  86. break;
  87. if( i % 16 == 0 )
  88. {
  89. if( i > 0 )
  90. ssl->f_dbg( ssl->p_dbg, level, "\n" );
  91. snprintf( str, maxlen, "%s(%04d): %04x: ", file, line,
  92. (unsigned int) i );
  93. str[maxlen] = '\0';
  94. ssl->f_dbg( ssl->p_dbg, level, str );
  95. }
  96. snprintf( str, maxlen, " %02x", (unsigned int) buf[i] );
  97. str[maxlen] = '\0';
  98. ssl->f_dbg( ssl->p_dbg, level, str );
  99. }
  100. if( len > 0 )
  101. ssl->f_dbg( ssl->p_dbg, level, "\n" );
  102. }
  103. void debug_print_mpi( const ssl_context *ssl, int level,
  104. const char *file, int line,
  105. const char *text, const mpi *X )
  106. {
  107. char str[512];
  108. int j, k, maxlen = sizeof( str ) - 1, zeros = 1;
  109. size_t i, n;
  110. if( ssl->f_dbg == NULL || X == NULL )
  111. return;
  112. for( n = X->n - 1; n > 0; n-- )
  113. if( X->p[n] != 0 )
  114. break;
  115. for( j = ( sizeof(t_uint) << 3 ) - 1; j >= 0; j-- )
  116. if( ( ( X->p[n] >> j ) & 1 ) != 0 )
  117. break;
  118. snprintf( str, maxlen, "%s(%04d): value of '%s' (%lu bits) is:\n",
  119. file, line, text,
  120. (unsigned long) ( ( n * ( sizeof(t_uint) << 3 ) ) + j + 1 ) );
  121. str[maxlen] = '\0';
  122. ssl->f_dbg( ssl->p_dbg, level, str );
  123. for( i = n + 1, j = 0; i > 0; i-- )
  124. {
  125. if( zeros && X->p[i - 1] == 0 )
  126. continue;
  127. for( k = sizeof( t_uint ) - 1; k >= 0; k-- )
  128. {
  129. if( zeros && ( ( X->p[i - 1] >> (k << 3) ) & 0xFF ) == 0 )
  130. continue;
  131. else
  132. zeros = 0;
  133. if( j % 16 == 0 )
  134. {
  135. if( j > 0 )
  136. ssl->f_dbg( ssl->p_dbg, level, "\n" );
  137. snprintf( str, maxlen, "%s(%04d): ", file, line );
  138. str[maxlen] = '\0';
  139. ssl->f_dbg( ssl->p_dbg, level, str );
  140. }
  141. snprintf( str, maxlen, " %02x", (unsigned int)
  142. ( X->p[i - 1] >> (k << 3) ) & 0xFF );
  143. str[maxlen] = '\0';
  144. ssl->f_dbg( ssl->p_dbg, level, str );
  145. j++;
  146. }
  147. }
  148. if( zeros == 1 )
  149. {
  150. snprintf( str, maxlen, "%s(%04d): ", file, line );
  151. str[maxlen] = '\0';
  152. ssl->f_dbg( ssl->p_dbg, level, str );
  153. ssl->f_dbg( ssl->p_dbg, level, " 00" );
  154. }
  155. ssl->f_dbg( ssl->p_dbg, level, "\n" );
  156. }
  157. void debug_print_crt( const ssl_context *ssl, int level,
  158. const char *file, int line,
  159. const char *text, const x509_cert *crt )
  160. {
  161. char str[1024], prefix[64];
  162. int i = 0, maxlen = sizeof( prefix ) - 1;
  163. if( ssl->f_dbg == NULL || crt == NULL )
  164. return;
  165. snprintf( prefix, maxlen, "%s(%04d): ", file, line );
  166. prefix[maxlen] = '\0';
  167. maxlen = sizeof( str ) - 1;
  168. while( crt != NULL )
  169. {
  170. char buf[1024];
  171. x509parse_cert_info( buf, sizeof( buf ) - 1, prefix, crt );
  172. snprintf( str, maxlen, "%s(%04d): %s #%d:\n%s",
  173. file, line, text, ++i, buf );
  174. str[maxlen] = '\0';
  175. ssl->f_dbg( ssl->p_dbg, level, str );
  176. debug_print_mpi( ssl, level, file, line,
  177. "crt->rsa.N", &crt->rsa.N );
  178. debug_print_mpi( ssl, level, file, line,
  179. "crt->rsa.E", &crt->rsa.E );
  180. crt = crt->next;
  181. }
  182. }
  183. #endif