PolarSSL v1.2.8
debug.c
Go to the documentation of this file.
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 
26 #include "polarssl/config.h"
27 
28 #if defined(POLARSSL_DEBUG_C)
29 
30 #include "polarssl/debug.h"
31 
32 #include <stdarg.h>
33 #include <stdlib.h>
34 
35 #if defined _MSC_VER && !defined snprintf
36 #define snprintf _snprintf
37 #endif
38 
39 #if defined _MSC_VER && !defined vsnprintf
40 #define vsnprintf _vsnprintf
41 #endif
42 
43 char *debug_fmt( const char *format, ... )
44 {
45  va_list argp;
46  static char str[512];
47  int maxlen = sizeof( str ) - 1;
48 
49  va_start( argp, format );
50  vsnprintf( str, maxlen, format, argp );
51  va_end( argp );
52 
53  str[maxlen] = '\0';
54  return( str );
55 }
56 
57 void debug_print_msg( const ssl_context *ssl, int level,
58  const char *file, int line, const char *text )
59 {
60  char str[512];
61  int maxlen = sizeof( str ) - 1;
62 
63  if( ssl->f_dbg == NULL )
64  return;
65 
66  snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text );
67  str[maxlen] = '\0';
68  ssl->f_dbg( ssl->p_dbg, level, str );
69 }
70 
71 void debug_print_ret( const ssl_context *ssl, int level,
72  const char *file, int line,
73  const char *text, int ret )
74 {
75  char str[512];
76  int maxlen = sizeof( str ) - 1;
77 
78  if( ssl->f_dbg == NULL )
79  return;
80 
81  snprintf( str, maxlen, "%s(%04d): %s() returned %d (0x%x)\n",
82  file, line, text, ret, ret );
83 
84  str[maxlen] = '\0';
85  ssl->f_dbg( ssl->p_dbg, level, str );
86 }
87 
88 void debug_print_buf( const ssl_context *ssl, int level,
89  const char *file, int line, const char *text,
90  unsigned char *buf, size_t len )
91 {
92  char str[512];
93  size_t i, maxlen = sizeof( str ) - 1;
94 
95  if( ssl->f_dbg == NULL )
96  return;
97 
98  snprintf( str, maxlen, "%s(%04d): dumping '%s' (%d bytes)\n",
99  file, line, text, (unsigned int) len );
100 
101  str[maxlen] = '\0';
102  ssl->f_dbg( ssl->p_dbg, level, str );
103 
104  for( i = 0; i < len; i++ )
105  {
106  if( i >= 4096 )
107  break;
108 
109  if( i % 16 == 0 )
110  {
111  if( i > 0 )
112  ssl->f_dbg( ssl->p_dbg, level, "\n" );
113 
114  snprintf( str, maxlen, "%s(%04d): %04x: ", file, line,
115  (unsigned int) i );
116 
117  str[maxlen] = '\0';
118  ssl->f_dbg( ssl->p_dbg, level, str );
119  }
120 
121  snprintf( str, maxlen, " %02x", (unsigned int) buf[i] );
122 
123  str[maxlen] = '\0';
124  ssl->f_dbg( ssl->p_dbg, level, str );
125  }
126 
127  if( len > 0 )
128  ssl->f_dbg( ssl->p_dbg, level, "\n" );
129 }
130 
131 void debug_print_mpi( const ssl_context *ssl, int level,
132  const char *file, int line,
133  const char *text, const mpi *X )
134 {
135  char str[512];
136  int j, k, maxlen = sizeof( str ) - 1, zeros = 1;
137  size_t i, n;
138 
139  if( ssl->f_dbg == NULL || X == NULL )
140  return;
141 
142  for( n = X->n - 1; n > 0; n-- )
143  if( X->p[n] != 0 )
144  break;
145 
146  for( j = ( sizeof(t_uint) << 3 ) - 1; j >= 0; j-- )
147  if( ( ( X->p[n] >> j ) & 1 ) != 0 )
148  break;
149 
150  snprintf( str, maxlen, "%s(%04d): value of '%s' (%d bits) is:\n",
151  file, line, text,
152  (int) ( ( n * ( sizeof(t_uint) << 3 ) ) + j + 1 ) );
153 
154  str[maxlen] = '\0';
155  ssl->f_dbg( ssl->p_dbg, level, str );
156 
157  for( i = n + 1, j = 0; i > 0; i-- )
158  {
159  if( zeros && X->p[i - 1] == 0 )
160  continue;
161 
162  for( k = sizeof( t_uint ) - 1; k >= 0; k-- )
163  {
164  if( zeros && ( ( X->p[i - 1] >> (k << 3) ) & 0xFF ) == 0 )
165  continue;
166  else
167  zeros = 0;
168 
169  if( j % 16 == 0 )
170  {
171  if( j > 0 )
172  ssl->f_dbg( ssl->p_dbg, level, "\n" );
173 
174  snprintf( str, maxlen, "%s(%04d): ", file, line );
175 
176  str[maxlen] = '\0';
177  ssl->f_dbg( ssl->p_dbg, level, str );
178  }
179 
180  snprintf( str, maxlen, " %02x", (unsigned int)
181  ( X->p[i - 1] >> (k << 3) ) & 0xFF );
182 
183  str[maxlen] = '\0';
184  ssl->f_dbg( ssl->p_dbg, level, str );
185 
186  j++;
187  }
188 
189  }
190 
191  if( zeros == 1 )
192  {
193  snprintf( str, maxlen, "%s(%04d): ", file, line );
194 
195  str[maxlen] = '\0';
196  ssl->f_dbg( ssl->p_dbg, level, str );
197  ssl->f_dbg( ssl->p_dbg, level, " 00" );
198  }
199 
200  ssl->f_dbg( ssl->p_dbg, level, "\n" );
201 }
202 
203 void debug_print_crt( const ssl_context *ssl, int level,
204  const char *file, int line,
205  const char *text, const x509_cert *crt )
206 {
207  char str[1024], prefix[64];
208  int i = 0, maxlen = sizeof( prefix ) - 1;
209 
210  if( ssl->f_dbg == NULL || crt == NULL )
211  return;
212 
213  snprintf( prefix, maxlen, "%s(%04d): ", file, line );
214  prefix[maxlen] = '\0';
215  maxlen = sizeof( str ) - 1;
216 
217  while( crt != NULL )
218  {
219  char buf[1024];
220  x509parse_cert_info( buf, sizeof( buf ) - 1, prefix, crt );
221 
222  snprintf( str, maxlen, "%s(%04d): %s #%d:\n%s",
223  file, line, text, ++i, buf );
224 
225  str[maxlen] = '\0';
226  ssl->f_dbg( ssl->p_dbg, level, str );
227 
228  debug_print_mpi( ssl, level, file, line,
229  "crt->rsa.N", &crt->rsa.N );
230 
231  debug_print_mpi( ssl, level, file, line,
232  "crt->rsa.E", &crt->rsa.E );
233 
234  crt = crt->next;
235  }
236 }
237 
238 #endif