PolarSSL v1.3.4
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(EFIX64) || defined(EFI32)
36 #include <stdio.h>
37 #endif
38 
39 #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
40 #if !defined snprintf
41 #define snprintf _snprintf
42 #endif
43 
44 #if !defined vsnprintf
45 #define vsnprintf _vsnprintf
46 #endif
47 #endif /* _MSC_VER */
48 
49 char *debug_fmt( const char *format, ... )
50 {
51  va_list argp;
52  static char str[512];
53  int maxlen = sizeof( str ) - 1;
54 
55  va_start( argp, format );
56  vsnprintf( str, maxlen, format, argp );
57  va_end( argp );
58 
59  str[maxlen] = '\0';
60  return( str );
61 }
62 
63 void debug_print_msg( const ssl_context *ssl, int level,
64  const char *file, int line, const char *text )
65 {
66  char str[512];
67  int maxlen = sizeof( str ) - 1;
68 
69  if( ssl->f_dbg == NULL )
70  return;
71 
72  snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text );
73  str[maxlen] = '\0';
74  ssl->f_dbg( ssl->p_dbg, level, str );
75 }
76 
77 void debug_print_ret( const ssl_context *ssl, int level,
78  const char *file, int line,
79  const char *text, int ret )
80 {
81  char str[512];
82  int maxlen = sizeof( str ) - 1;
83 
84  if( ssl->f_dbg == NULL )
85  return;
86 
87  snprintf( str, maxlen, "%s(%04d): %s() returned %d (0x%x)\n",
88  file, line, text, ret, ret );
89 
90  str[maxlen] = '\0';
91  ssl->f_dbg( ssl->p_dbg, level, str );
92 }
93 
94 void debug_print_buf( const ssl_context *ssl, int level,
95  const char *file, int line, const char *text,
96  unsigned char *buf, size_t len )
97 {
98  char str[512];
99  size_t i, maxlen = sizeof( str ) - 1;
100 
101  if( ssl->f_dbg == NULL )
102  return;
103 
104  snprintf( str, maxlen, "%s(%04d): dumping '%s' (%d bytes)\n",
105  file, line, text, (unsigned int) len );
106 
107  str[maxlen] = '\0';
108  ssl->f_dbg( ssl->p_dbg, level, str );
109 
110  for( i = 0; i < len; i++ )
111  {
112  if( i >= 4096 )
113  break;
114 
115  if( i % 16 == 0 )
116  {
117  if( i > 0 )
118  ssl->f_dbg( ssl->p_dbg, level, "\n" );
119 
120  snprintf( str, maxlen, "%s(%04d): %04x: ", file, line,
121  (unsigned int) i );
122 
123  str[maxlen] = '\0';
124  ssl->f_dbg( ssl->p_dbg, level, str );
125  }
126 
127  snprintf( str, maxlen, " %02x", (unsigned int) buf[i] );
128 
129  str[maxlen] = '\0';
130  ssl->f_dbg( ssl->p_dbg, level, str );
131  }
132 
133  if( len > 0 )
134  ssl->f_dbg( ssl->p_dbg, level, "\n" );
135 }
136 
137 #if defined(POLARSSL_ECP_C)
138 void debug_print_ecp( const ssl_context *ssl, int level,
139  const char *file, int line,
140  const char *text, const ecp_point *X )
141 {
142  char str[512];
143  int maxlen = sizeof( str ) - 1;
144 
145  snprintf( str, maxlen, "%s(X)", text );
146  str[maxlen] = '\0';
147  debug_print_mpi( ssl, level, file, line, str, &X->X );
148 
149  snprintf( str, maxlen, "%s(Y)", text );
150  str[maxlen] = '\0';
151  debug_print_mpi( ssl, level, file, line, str, &X->Y );
152 
153  snprintf( str, maxlen, "%s(Z)", text );
154  str[maxlen] = '\0';
155  debug_print_mpi( ssl, level, file, line, str, &X->Z );
156 }
157 #endif /* POLARSSL_ECP_C */
158 
159 #if defined(POLARSSL_BIGNUM_C)
160 void debug_print_mpi( const ssl_context *ssl, int level,
161  const char *file, int line,
162  const char *text, const mpi *X )
163 {
164  char str[512];
165  int j, k, maxlen = sizeof( str ) - 1, zeros = 1;
166  size_t i, n;
167 
168  if( ssl->f_dbg == NULL || X == NULL )
169  return;
170 
171  for( n = X->n - 1; n > 0; n-- )
172  if( X->p[n] != 0 )
173  break;
174 
175  for( j = ( sizeof(t_uint) << 3 ) - 1; j >= 0; j-- )
176  if( ( ( X->p[n] >> j ) & 1 ) != 0 )
177  break;
178 
179  snprintf( str, maxlen, "%s(%04d): value of '%s' (%d bits) is:\n",
180  file, line, text,
181  (int) ( ( n * ( sizeof(t_uint) << 3 ) ) + j + 1 ) );
182 
183  str[maxlen] = '\0';
184  ssl->f_dbg( ssl->p_dbg, level, str );
185 
186  for( i = n + 1, j = 0; i > 0; i-- )
187  {
188  if( zeros && X->p[i - 1] == 0 )
189  continue;
190 
191  for( k = sizeof( t_uint ) - 1; k >= 0; k-- )
192  {
193  if( zeros && ( ( X->p[i - 1] >> (k << 3) ) & 0xFF ) == 0 )
194  continue;
195  else
196  zeros = 0;
197 
198  if( j % 16 == 0 )
199  {
200  if( j > 0 )
201  ssl->f_dbg( ssl->p_dbg, level, "\n" );
202 
203  snprintf( str, maxlen, "%s(%04d): ", file, line );
204 
205  str[maxlen] = '\0';
206  ssl->f_dbg( ssl->p_dbg, level, str );
207  }
208 
209  snprintf( str, maxlen, " %02x", (unsigned int)
210  ( X->p[i - 1] >> (k << 3) ) & 0xFF );
211 
212  str[maxlen] = '\0';
213  ssl->f_dbg( ssl->p_dbg, level, str );
214 
215  j++;
216  }
217 
218  }
219 
220  if( zeros == 1 )
221  {
222  snprintf( str, maxlen, "%s(%04d): ", file, line );
223 
224  str[maxlen] = '\0';
225  ssl->f_dbg( ssl->p_dbg, level, str );
226  ssl->f_dbg( ssl->p_dbg, level, " 00" );
227  }
228 
229  ssl->f_dbg( ssl->p_dbg, level, "\n" );
230 }
231 #endif /* POLARSSL_BIGNUM_C */
232 
233 #if defined(POLARSSL_X509_CRT_PARSE_C)
234 static void debug_print_pk( const ssl_context *ssl, int level,
235  const char *file, int line,
236  const char *text, const pk_context *pk )
237 {
238  size_t i;
240  char name[16];
241 
242  memset( items, 0, sizeof( items ) );
243 
244  if( pk_debug( pk, items ) != 0 )
245  {
246  debug_print_msg( ssl, level, file, line, "invalid PK context" );
247  return;
248  }
249 
250  for( i = 0; i < sizeof( items ); i++ )
251  {
252  if( items[i].type == POLARSSL_PK_DEBUG_NONE )
253  return;
254 
255  snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
256  name[sizeof( name ) - 1] = '\0';
257 
258  if( items[i].type == POLARSSL_PK_DEBUG_MPI )
259  debug_print_mpi( ssl, level, file, line, name, items[i].value );
260  else
261 #if defined(POLARSSL_ECP_C)
262  if( items[i].type == POLARSSL_PK_DEBUG_ECP )
263  debug_print_ecp( ssl, level, file, line, name, items[i].value );
264  else
265 #endif
266  debug_print_msg( ssl, level, file, line, "should not happen" );
267  }
268 }
269 
270 void debug_print_crt( const ssl_context *ssl, int level,
271  const char *file, int line,
272  const char *text, const x509_crt *crt )
273 {
274  char str[1024], prefix[64];
275  int i = 0, maxlen = sizeof( prefix ) - 1;
276 
277  if( ssl->f_dbg == NULL || crt == NULL )
278  return;
279 
280  snprintf( prefix, maxlen, "%s(%04d): ", file, line );
281  prefix[maxlen] = '\0';
282  maxlen = sizeof( str ) - 1;
283 
284  while( crt != NULL )
285  {
286  char buf[1024];
287  x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt );
288 
289  snprintf( str, maxlen, "%s(%04d): %s #%d:\n%s",
290  file, line, text, ++i, buf );
291 
292  str[maxlen] = '\0';
293  ssl->f_dbg( ssl->p_dbg, level, str );
294 
295  debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
296 
297  crt = crt->next;
298  }
299 }
300 #endif /* POLARSSL_X509_CRT_PARSE_C */
301 
302 #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:612
uint32_t t_uint
Definition: bignum.h:155
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:177
mpi X
Definition: ecp.h:105
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:103
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:619
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:181
mpi Y
Definition: ecp.h:106
size_t n
Definition: bignum.h:180
pk_context pk
Container for the public key context.
Definition: x509_crt.h:71
mpi Z
Definition: ecp.h:107
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