PolarSSL v1.3.1
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 #if defined(POLARSSL_ECP_C)
132 void debug_print_ecp( const ssl_context *ssl, int level,
133  const char *file, int line,
134  const char *text, const ecp_point *X )
135 {
136  char str[512];
137  int maxlen = sizeof( str ) - 1;
138 
139  snprintf( str, maxlen, "%s(X)", text );
140  str[maxlen] = '\0';
141  debug_print_mpi( ssl, level, file, line, str, &X->X );
142 
143  snprintf( str, maxlen, "%s(Y)", text );
144  str[maxlen] = '\0';
145  debug_print_mpi( ssl, level, file, line, str, &X->Y );
146 
147  snprintf( str, maxlen, "%s(Z)", text );
148  str[maxlen] = '\0';
149  debug_print_mpi( ssl, level, file, line, str, &X->Z );
150 }
151 #endif /* POLARSSL_ECP_C */
152 
153 #if defined(POLARSSL_BIGNUM_C)
154 void debug_print_mpi( const ssl_context *ssl, int level,
155  const char *file, int line,
156  const char *text, const mpi *X )
157 {
158  char str[512];
159  int j, k, maxlen = sizeof( str ) - 1, zeros = 1;
160  size_t i, n;
161 
162  if( ssl->f_dbg == NULL || X == NULL )
163  return;
164 
165  for( n = X->n - 1; n > 0; n-- )
166  if( X->p[n] != 0 )
167  break;
168 
169  for( j = ( sizeof(t_uint) << 3 ) - 1; j >= 0; j-- )
170  if( ( ( X->p[n] >> j ) & 1 ) != 0 )
171  break;
172 
173  snprintf( str, maxlen, "%s(%04d): value of '%s' (%d bits) is:\n",
174  file, line, text,
175  (int) ( ( n * ( sizeof(t_uint) << 3 ) ) + j + 1 ) );
176 
177  str[maxlen] = '\0';
178  ssl->f_dbg( ssl->p_dbg, level, str );
179 
180  for( i = n + 1, j = 0; i > 0; i-- )
181  {
182  if( zeros && X->p[i - 1] == 0 )
183  continue;
184 
185  for( k = sizeof( t_uint ) - 1; k >= 0; k-- )
186  {
187  if( zeros && ( ( X->p[i - 1] >> (k << 3) ) & 0xFF ) == 0 )
188  continue;
189  else
190  zeros = 0;
191 
192  if( j % 16 == 0 )
193  {
194  if( j > 0 )
195  ssl->f_dbg( ssl->p_dbg, level, "\n" );
196 
197  snprintf( str, maxlen, "%s(%04d): ", file, line );
198 
199  str[maxlen] = '\0';
200  ssl->f_dbg( ssl->p_dbg, level, str );
201  }
202 
203  snprintf( str, maxlen, " %02x", (unsigned int)
204  ( X->p[i - 1] >> (k << 3) ) & 0xFF );
205 
206  str[maxlen] = '\0';
207  ssl->f_dbg( ssl->p_dbg, level, str );
208 
209  j++;
210  }
211 
212  }
213 
214  if( zeros == 1 )
215  {
216  snprintf( str, maxlen, "%s(%04d): ", file, line );
217 
218  str[maxlen] = '\0';
219  ssl->f_dbg( ssl->p_dbg, level, str );
220  ssl->f_dbg( ssl->p_dbg, level, " 00" );
221  }
222 
223  ssl->f_dbg( ssl->p_dbg, level, "\n" );
224 }
225 #endif /* POLARSSL_BIGNUM_C */
226 
227 #if defined(POLARSSL_X509_CRT_PARSE_C)
228 static void debug_print_pk( const ssl_context *ssl, int level,
229  const char *file, int line,
230  const char *text, const pk_context *pk )
231 {
232  size_t i;
234  char name[16];
235 
236  memset( items, 0, sizeof( items ) );
237 
238  if( pk_debug( pk, items ) != 0 )
239  {
240  debug_print_msg( ssl, level, file, line, "invalid PK context" );
241  return;
242  }
243 
244  for( i = 0; i < sizeof( items ); i++ )
245  {
246  if( items[i].type == POLARSSL_PK_DEBUG_NONE )
247  return;
248 
249  snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
250  name[sizeof( name ) - 1] = '\0';
251 
252  if( items[i].type == POLARSSL_PK_DEBUG_MPI )
253  debug_print_mpi( ssl, level, file, line, name, items[i].value );
254  else
255 #if defined(POLARSSL_ECP_C)
256  if( items[i].type == POLARSSL_PK_DEBUG_ECP )
257  debug_print_ecp( ssl, level, file, line, name, items[i].value );
258  else
259 #endif
260  debug_print_msg( ssl, level, file, line, "should not happen" );
261  }
262 }
263 
264 void debug_print_crt( const ssl_context *ssl, int level,
265  const char *file, int line,
266  const char *text, const x509_crt *crt )
267 {
268  char str[1024], prefix[64];
269  int i = 0, maxlen = sizeof( prefix ) - 1;
270 
271  if( ssl->f_dbg == NULL || crt == NULL )
272  return;
273 
274  snprintf( prefix, maxlen, "%s(%04d): ", file, line );
275  prefix[maxlen] = '\0';
276  maxlen = sizeof( str ) - 1;
277 
278  while( crt != NULL )
279  {
280  char buf[1024];
281  x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt );
282 
283  snprintf( str, maxlen, "%s(%04d): %s #%d:\n%s",
284  file, line, text, ++i, buf );
285 
286  str[maxlen] = '\0';
287  ssl->f_dbg( ssl->p_dbg, level, str );
288 
289  debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
290 
291  crt = crt->next;
292  }
293 }
294 #endif /* POLARSSL_X509_CRT_PARSE_C */
295 
296 #endif
void debug_print_crt(const ssl_context *ssl, int level, const char *file, int line, const char *text, const x509_crt *crt)
void(* f_dbg)(void *, int, const char *)
Definition: ssl.h:601
uint32_t t_uint
Definition: bignum.h:146
Debug functions.
void debug_print_msg(const ssl_context *ssl, int level, const char *file, int line, const char *text)
int pk_debug(const pk_context *ctx, pk_debug_item *items)
Export debug information.
Configuration options (set of defines)
MPI structure.
Definition: bignum.h:168
mpi X
Definition: ecp.h:96
void debug_print_ecp(const ssl_context *ssl, int level, const char *file, int line, const char *text, const ecp_point *X)
struct _x509_crt * next
Next certificate in the CA-chain.
Definition: x509_crt.h:93
Container for an X.509 certificate.
Definition: x509_crt.h:53
Item to send to the debug module.
Definition: pk.h:112
ECP point structure (jacobian coordinates)
Definition: ecp.h:94
void debug_print_mpi(const ssl_context *ssl, int level, const char *file, int line, const char *text, const mpi *X)
void * p_dbg
Definition: ssl.h:608
void debug_print_buf(const ssl_context *ssl, int level, const char *file, int line, const char *text, unsigned char *buf, size_t len)
void debug_print_ret(const ssl_context *ssl, int level, const char *file, int line, const char *text, int ret)
t_uint * p
Definition: bignum.h:172
mpi Y
Definition: ecp.h:97
size_t n
Definition: bignum.h:171
pk_context pk
Container for the public key context.
Definition: x509_crt.h:71
mpi Z
Definition: ecp.h:98
char * debug_fmt(const char *format,...)
#define POLARSSL_PK_DEBUG_MAX_ITEMS
Maximum number of item send for debugging, plus 1.
Definition: pk.h:120
int x509_crt_info(char *buf, size_t size, const char *prefix, const x509_crt *crt)
Returns an informational string about the certificate.
Public key container.
Definition: pk.h:177