PolarSSL v1.3.9
debug.c
Go to the documentation of this file.
1/*
2 * Debugging routines
3 *
4 * Copyright (C) 2006-2014, 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#if !defined(POLARSSL_CONFIG_FILE)
27#include "polarssl/config.h"
28#else
29#include POLARSSL_CONFIG_FILE
30#endif
31
32#if defined(POLARSSL_DEBUG_C)
33
34#include "polarssl/debug.h"
35
36#include <stdarg.h>
37#include <stdlib.h>
38
39#if defined(EFIX64) || defined(EFI32)
40#include <stdio.h>
41#endif
42
43#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
44#if !defined snprintf
45#define snprintf _snprintf
46#endif
47
48#if !defined vsnprintf
49#define vsnprintf _vsnprintf
50#endif
51#endif /* _MSC_VER */
52
53static int debug_log_mode = POLARSSL_DEBUG_DFL_MODE;
54static int debug_threshold = 0;
55
56void debug_set_log_mode( int log_mode )
57{
58 debug_log_mode = log_mode;
59}
60
61void debug_set_threshold( int threshold )
62{
63 debug_threshold = threshold;
64}
65
66char *debug_fmt( const char *format, ... )
67{
68 va_list argp;
69 static char str[512];
70 int maxlen = sizeof( str ) - 1;
71
72 va_start( argp, format );
73 vsnprintf( str, maxlen, format, argp );
74 va_end( argp );
75
76 str[maxlen] = '\0';
77 return( str );
78}
79
80void debug_print_msg( const ssl_context *ssl, int level,
81 const char *file, int line, const char *text )
82{
83 char str[512];
84 int maxlen = sizeof( str ) - 1;
85
86 if( ssl->f_dbg == NULL || level > debug_threshold )
87 return;
88
89 if( debug_log_mode == POLARSSL_DEBUG_LOG_RAW )
90 {
91 ssl->f_dbg( ssl->p_dbg, level, text );
92 return;
93 }
94
95 snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text );
96 str[maxlen] = '\0';
97 ssl->f_dbg( ssl->p_dbg, level, str );
98}
99
100void debug_print_ret( const ssl_context *ssl, int level,
101 const char *file, int line,
102 const char *text, int ret )
103{
104 char str[512];
105 int maxlen = sizeof( str ) - 1;
106 size_t idx = 0;
107
108 if( ssl->f_dbg == NULL || level > debug_threshold )
109 return;
110
111 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
112 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
113
114 snprintf( str + idx, maxlen - idx, "%s() returned %d (-0x%04x)\n",
115 text, ret, -ret );
116
117 str[maxlen] = '\0';
118 ssl->f_dbg( ssl->p_dbg, level, str );
119}
120
121void debug_print_buf( const ssl_context *ssl, int level,
122 const char *file, int line, const char *text,
123 unsigned char *buf, size_t len )
124{
125 char str[512];
126 size_t i, maxlen = sizeof( str ) - 1, idx = 0;
127
128 if( ssl->f_dbg == NULL || level > debug_threshold )
129 return;
130
131 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
132 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
133
134 snprintf( str + idx, maxlen - idx, "dumping '%s' (%u bytes)\n",
135 text, (unsigned int) len );
136
137 str[maxlen] = '\0';
138 ssl->f_dbg( ssl->p_dbg, level, str );
139
140 idx = 0;
141 for( i = 0; i < len; i++ )
142 {
143 if( i >= 4096 )
144 break;
145
146 if( i % 16 == 0 )
147 {
148 if( i > 0 )
149 {
150 snprintf( str + idx, maxlen - idx, "\n" );
151 ssl->f_dbg( ssl->p_dbg, level, str );
152 idx = 0;
153 }
154
155 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
156 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
157
158 idx += snprintf( str + idx, maxlen - idx, "%04x: ",
159 (unsigned int) i );
160
161 }
162
163 idx += snprintf( str + idx, maxlen - idx, " %02x",
164 (unsigned int) buf[i] );
165 }
166
167 if( len > 0 )
168 {
169 snprintf( str + idx, maxlen - idx, "\n" );
170 ssl->f_dbg( ssl->p_dbg, level, str );
171 }
172}
173
174#if defined(POLARSSL_ECP_C)
175void debug_print_ecp( const ssl_context *ssl, int level,
176 const char *file, int line,
177 const char *text, const ecp_point *X )
178{
179 char str[512];
180 int maxlen = sizeof( str ) - 1;
181
182 if( ssl->f_dbg == NULL || level > debug_threshold )
183 return;
184
185 snprintf( str, maxlen, "%s(X)", text );
186 str[maxlen] = '\0';
187 debug_print_mpi( ssl, level, file, line, str, &X->X );
188
189 snprintf( str, maxlen, "%s(Y)", text );
190 str[maxlen] = '\0';
191 debug_print_mpi( ssl, level, file, line, str, &X->Y );
192}
193#endif /* POLARSSL_ECP_C */
194
195#if defined(POLARSSL_BIGNUM_C)
196void debug_print_mpi( const ssl_context *ssl, int level,
197 const char *file, int line,
198 const char *text, const mpi *X )
199{
200 char str[512];
201 int j, k, maxlen = sizeof( str ) - 1, zeros = 1;
202 size_t i, n, idx = 0;
203
204 if( ssl->f_dbg == NULL || X == NULL || level > debug_threshold )
205 return;
206
207 for( n = X->n - 1; n > 0; n-- )
208 if( X->p[n] != 0 )
209 break;
210
211 for( j = ( sizeof(t_uint) << 3 ) - 1; j >= 0; j-- )
212 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
213 break;
214
215 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
216 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
217
218 snprintf( str + idx, maxlen - idx, "value of '%s' (%d bits) is:\n",
219 text, (int) ( ( n * ( sizeof(t_uint) << 3 ) ) + j + 1 ) );
220
221 str[maxlen] = '\0';
222 ssl->f_dbg( ssl->p_dbg, level, str );
223
224 idx = 0;
225 for( i = n + 1, j = 0; i > 0; i-- )
226 {
227 if( zeros && X->p[i - 1] == 0 )
228 continue;
229
230 for( k = sizeof( t_uint ) - 1; k >= 0; k-- )
231 {
232 if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
233 continue;
234 else
235 zeros = 0;
236
237 if( j % 16 == 0 )
238 {
239 if( j > 0 )
240 {
241 snprintf( str + idx, maxlen - idx, "\n" );
242 ssl->f_dbg( ssl->p_dbg, level, str );
243 idx = 0;
244 }
245
246 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
247 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
248 }
249
250 idx += snprintf( str + idx, maxlen - idx, " %02x", (unsigned int)
251 ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
252
253 j++;
254 }
255
256 }
257
258 if( zeros == 1 )
259 {
260 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
261 {
262 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
263
264 }
265 idx += snprintf( str + idx, maxlen - idx, " 00" );
266 }
267
268 snprintf( str + idx, maxlen - idx, "\n" );
269 ssl->f_dbg( ssl->p_dbg, level, str );
270}
271#endif /* POLARSSL_BIGNUM_C */
272
273#if defined(POLARSSL_X509_CRT_PARSE_C)
274static void debug_print_pk( const ssl_context *ssl, int level,
275 const char *file, int line,
276 const char *text, const pk_context *pk )
277{
278 size_t i;
280 char name[16];
281
282 memset( items, 0, sizeof( items ) );
283
284 if( pk_debug( pk, items ) != 0 )
285 {
286 debug_print_msg( ssl, level, file, line, "invalid PK context" );
287 return;
288 }
289
290 for( i = 0; i < POLARSSL_PK_DEBUG_MAX_ITEMS; i++ )
291 {
292 if( items[i].type == POLARSSL_PK_DEBUG_NONE )
293 return;
294
295 snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
296 name[sizeof( name ) - 1] = '\0';
297
298 if( items[i].type == POLARSSL_PK_DEBUG_MPI )
299 debug_print_mpi( ssl, level, file, line, name, items[i].value );
300 else
301#if defined(POLARSSL_ECP_C)
302 if( items[i].type == POLARSSL_PK_DEBUG_ECP )
303 debug_print_ecp( ssl, level, file, line, name, items[i].value );
304 else
305#endif
306 debug_print_msg( ssl, level, file, line, "should not happen" );
307 }
308}
309
310void debug_print_crt( const ssl_context *ssl, int level,
311 const char *file, int line,
312 const char *text, const x509_crt *crt )
313{
314 char str[1024], prefix[64];
315 int i = 0, maxlen = sizeof( prefix ) - 1, idx = 0;
316
317 if( ssl->f_dbg == NULL || crt == NULL || level > debug_threshold )
318 return;
319
320 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
321 {
322 snprintf( prefix, maxlen, "%s(%04d): ", file, line );
323 prefix[maxlen] = '\0';
324 }
325 else
326 prefix[0] = '\0';
327
328 maxlen = sizeof( str ) - 1;
329
330 while( crt != NULL )
331 {
332 char buf[1024];
333 x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt );
334
335 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
336 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
337
338 snprintf( str + idx, maxlen - idx, "%s #%d:\n%s",
339 text, ++i, buf );
340
341 str[maxlen] = '\0';
342 ssl->f_dbg( ssl->p_dbg, level, str );
343
344 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
345
346 crt = crt->next;
347 }
348}
349#endif /* POLARSSL_X509_CRT_PARSE_C */
350
351#endif /* POLARSSL_DEBUG_C */
uint32_t t_uint
Definition: bignum.h:160
Configuration options (set of defines)
Debug functions.
void debug_set_threshold(int threshold)
Set the level threshold to handle globally.
void debug_set_log_mode(int log_mode)
Set the log mode for the debug functions globally (Default value: POLARSSL_DEBUG_DFL_MODE)
#define POLARSSL_DEBUG_LOG_FULL
Include file:line in log lines.
Definition: debug.h:42
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_msg(const ssl_context *ssl, int level, const char *file, int line, const char *text)
void debug_print_ecp(const ssl_context *ssl, int level, const char *file, int line, const char *text, const ecp_point *X)
char * debug_fmt(const char *format,...)
#define POLARSSL_DEBUG_DFL_MODE
Default log: Full or Raw.
Definition: debug.h:54
void debug_print_crt(const ssl_context *ssl, int level, const char *file, int line, const char *text, const x509_crt *crt)
void debug_print_mpi(const ssl_context *ssl, int level, const char *file, int line, const char *text, const mpi *X)
#define POLARSSL_DEBUG_LOG_RAW
Only log raw debug lines.
Definition: debug.h:43
void debug_print_ret(const ssl_context *ssl, int level, const char *file, int line, const char *text, int ret)
pk_context pk
Container for the public key context.
Definition: x509_crt.h:75
int x509_crt_info(char *buf, size_t size, const char *prefix, const x509_crt *crt)
Returns an informational string about the certificate.
struct _x509_crt * next
Next certificate in the CA-chain.
Definition: x509_crt.h:98
#define POLARSSL_PK_DEBUG_MAX_ITEMS
Maximum number of item send for debugging, plus 1.
Definition: pk.h:137
@ POLARSSL_PK_DEBUG_NONE
Definition: pk.h:121
@ POLARSSL_PK_DEBUG_MPI
Definition: pk.h:122
@ POLARSSL_PK_DEBUG_ECP
Definition: pk.h:123
int pk_debug(const pk_context *ctx, pk_debug_item *items)
Export debug information.
void * p_dbg
Definition: ssl.h:711
void(* f_dbg)(void *, int, const char *)
Definition: ssl.h:704
Container for an X.509 certificate.
Definition: x509_crt.h:58
ECP point structure (jacobian coordinates)
Definition: ecp.h:105
mpi Y
Definition: ecp.h:107
mpi X
Definition: ecp.h:106
MPI structure.
Definition: bignum.h:183
t_uint * p
Definition: bignum.h:186
size_t n
Definition: bignum.h:185
Public key container.
Definition: pk.h:195
Item to send to the debug module.
Definition: pk.h:130