blob: 8e24a84e18d46ca4fc9d73a422f14593a1635aa7 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Debugging routines
3 *
Paul Bakker84f12b72010-07-18 10:13:04 +00004 * Copyright (C) 2006-2010, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * 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
Paul Bakker40e46942009-01-03 21:51:57 +000026#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Paul Bakker40e46942009-01-03 21:51:57 +000028#if defined(POLARSSL_DEBUG_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakker40e46942009-01-03 21:51:57 +000030#include "polarssl/debug.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000031
32#include <stdarg.h>
33#include <stdlib.h>
34
Paul Bakkerfa6a6202013-10-28 18:48:30 +010035#if defined(EFIX64) || defined(EFI32)
36#include <stdio.h>
37#endif
38
Paul Bakker6edcd412013-10-29 15:22:54 +010039#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
40#if !defined snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +000041#define snprintf _snprintf
42#endif
43
Paul Bakker6edcd412013-10-29 15:22:54 +010044#if !defined vsnprintf
Paul Bakker5121ce52009-01-03 21:22:43 +000045#define vsnprintf _vsnprintf
46#endif
Paul Bakker6edcd412013-10-29 15:22:54 +010047#endif /* _MSC_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +000048
Paul Bakkereaebbd52014-04-25 15:04:14 +020049static int debug_log_mode = POLARSSL_DEBUG_DFL_MODE;
50
51void debug_set_log_mode( int log_mode )
52{
53 debug_log_mode = log_mode;
54}
55
Paul Bakker5121ce52009-01-03 21:22:43 +000056char *debug_fmt( const char *format, ... )
57{
58 va_list argp;
59 static char str[512];
60 int maxlen = sizeof( str ) - 1;
61
62 va_start( argp, format );
63 vsnprintf( str, maxlen, format, argp );
64 va_end( argp );
65
66 str[maxlen] = '\0';
67 return( str );
68}
69
Paul Bakkerff60ee62010-03-16 21:09:09 +000070void debug_print_msg( const ssl_context *ssl, int level,
71 const char *file, int line, const char *text )
Paul Bakker5121ce52009-01-03 21:22:43 +000072{
73 char str[512];
74 int maxlen = sizeof( str ) - 1;
75
76 if( ssl->f_dbg == NULL )
77 return;
78
Paul Bakkereaebbd52014-04-25 15:04:14 +020079 if( debug_log_mode == POLARSSL_DEBUG_LOG_RAW )
80 {
81 ssl->f_dbg( ssl->p_dbg, level, str );
82 return;
83 }
84
Paul Bakker5121ce52009-01-03 21:22:43 +000085 snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text );
86 str[maxlen] = '\0';
87 ssl->f_dbg( ssl->p_dbg, level, str );
88}
89
Paul Bakkerff60ee62010-03-16 21:09:09 +000090void debug_print_ret( const ssl_context *ssl, int level,
91 const char *file, int line,
92 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +000093{
94 char str[512];
95 int maxlen = sizeof( str ) - 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +020096 size_t idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +000097
98 if( ssl->f_dbg == NULL )
99 return;
100
Paul Bakkereaebbd52014-04-25 15:04:14 +0200101 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
102 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
103
104 snprintf( str + idx, maxlen - idx, "%s() returned %d (-0x%04x)\n",
105 text, ret, -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
107 str[maxlen] = '\0';
108 ssl->f_dbg( ssl->p_dbg, level, str );
109}
110
Paul Bakkerff60ee62010-03-16 21:09:09 +0000111void debug_print_buf( const ssl_context *ssl, int level,
112 const char *file, int line, const char *text,
Paul Bakker23986e52011-04-24 08:57:21 +0000113 unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000114{
115 char str[512];
Paul Bakkereaebbd52014-04-25 15:04:14 +0200116 size_t i, maxlen = sizeof( str ) - 1, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000117
Paul Bakker23986e52011-04-24 08:57:21 +0000118 if( ssl->f_dbg == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 return;
120
Paul Bakkereaebbd52014-04-25 15:04:14 +0200121 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
122 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
123
124 snprintf( str + idx, maxlen - idx, "dumping '%s' (%d bytes)\n",
125 text, (unsigned int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
127 str[maxlen] = '\0';
128 ssl->f_dbg( ssl->p_dbg, level, str );
129
130 for( i = 0; i < len; i++ )
131 {
132 if( i >= 4096 )
133 break;
134
135 if( i % 16 == 0 )
136 {
137 if( i > 0 )
138 ssl->f_dbg( ssl->p_dbg, level, "\n" );
139
Paul Bakkereaebbd52014-04-25 15:04:14 +0200140 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
141 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
142
143 snprintf( str + idx, maxlen - idx, "%04x: ", (unsigned int) i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000144
145 str[maxlen] = '\0';
146 ssl->f_dbg( ssl->p_dbg, level, str );
147 }
148
149 snprintf( str, maxlen, " %02x", (unsigned int) buf[i] );
150
151 str[maxlen] = '\0';
152 ssl->f_dbg( ssl->p_dbg, level, str );
153 }
154
155 if( len > 0 )
156 ssl->f_dbg( ssl->p_dbg, level, "\n" );
157}
158
Paul Bakker41c83d32013-03-20 14:39:14 +0100159#if defined(POLARSSL_ECP_C)
160void debug_print_ecp( const ssl_context *ssl, int level,
161 const char *file, int line,
162 const char *text, const ecp_point *X )
163{
164 char str[512];
165 int maxlen = sizeof( str ) - 1;
166
167 snprintf( str, maxlen, "%s(X)", text );
168 str[maxlen] = '\0';
169 debug_print_mpi( ssl, level, file, line, str, &X->X );
170
171 snprintf( str, maxlen, "%s(Y)", text );
172 str[maxlen] = '\0';
173 debug_print_mpi( ssl, level, file, line, str, &X->Y );
Paul Bakker41c83d32013-03-20 14:39:14 +0100174}
175#endif /* POLARSSL_ECP_C */
176
Paul Bakkered27a042013-04-18 22:46:23 +0200177#if defined(POLARSSL_BIGNUM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +0000178void debug_print_mpi( const ssl_context *ssl, int level,
179 const char *file, int line,
180 const char *text, const mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000181{
182 char str[512];
Paul Bakker23986e52011-04-24 08:57:21 +0000183 int j, k, maxlen = sizeof( str ) - 1, zeros = 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +0200184 size_t i, n, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000185
186 if( ssl->f_dbg == NULL || X == NULL )
187 return;
188
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000189 for( n = X->n - 1; n > 0; n-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 if( X->p[n] != 0 )
191 break;
192
Paul Bakkera755ca12011-04-24 09:11:17 +0000193 for( j = ( sizeof(t_uint) << 3 ) - 1; j >= 0; j-- )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000194 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
195 break;
196
Paul Bakkereaebbd52014-04-25 15:04:14 +0200197 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
198 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
199
200 snprintf( str + idx, maxlen - idx, "value of '%s' (%d bits) is:\n",
201 text, (int) ( ( n * ( sizeof(t_uint) << 3 ) ) + j + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
203 str[maxlen] = '\0';
204 ssl->f_dbg( ssl->p_dbg, level, str );
205
Paul Bakker23986e52011-04-24 08:57:21 +0000206 for( i = n + 1, j = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 {
Paul Bakker23986e52011-04-24 08:57:21 +0000208 if( zeros && X->p[i - 1] == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000209 continue;
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
Paul Bakkera755ca12011-04-24 09:11:17 +0000211 for( k = sizeof( t_uint ) - 1; k >= 0; k-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000212 {
Paul Bakker23986e52011-04-24 08:57:21 +0000213 if( zeros && ( ( X->p[i - 1] >> (k << 3) ) & 0xFF ) == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000214 continue;
215 else
216 zeros = 0;
217
218 if( j % 16 == 0 )
219 {
220 if( j > 0 )
221 ssl->f_dbg( ssl->p_dbg, level, "\n" );
222
Paul Bakkereaebbd52014-04-25 15:04:14 +0200223 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
224 {
225 snprintf( str, maxlen, "%s(%04d): ", file, line );
226 str[maxlen] = '\0';
227 ssl->f_dbg( ssl->p_dbg, level, str );
228 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000229 }
230
Paul Bakker5121ce52009-01-03 21:22:43 +0000231 snprintf( str, maxlen, " %02x", (unsigned int)
Paul Bakker23986e52011-04-24 08:57:21 +0000232 ( X->p[i - 1] >> (k << 3) ) & 0xFF );
Paul Bakker5121ce52009-01-03 21:22:43 +0000233
234 str[maxlen] = '\0';
235 ssl->f_dbg( ssl->p_dbg, level, str );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000236
237 j++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000239
240 }
241
242 if( zeros == 1 )
243 {
Paul Bakkereaebbd52014-04-25 15:04:14 +0200244 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
245 {
246 snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000247
Paul Bakkereaebbd52014-04-25 15:04:14 +0200248 str[maxlen] = '\0';
249 ssl->f_dbg( ssl->p_dbg, level, str );
250 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000251 ssl->f_dbg( ssl->p_dbg, level, " 00" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000252 }
253
254 ssl->f_dbg( ssl->p_dbg, level, "\n" );
255}
Paul Bakkered27a042013-04-18 22:46:23 +0200256#endif /* POLARSSL_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000257
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200258#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200259static void debug_print_pk( const ssl_context *ssl, int level,
260 const char *file, int line,
261 const char *text, const pk_context *pk )
262{
263 size_t i;
264 pk_debug_item items[POLARSSL_PK_DEBUG_MAX_ITEMS];
265 char name[16];
266
267 memset( items, 0, sizeof( items ) );
268
269 if( pk_debug( pk, items ) != 0 )
270 {
271 debug_print_msg( ssl, level, file, line, "invalid PK context" );
272 return;
273 }
274
Paul Bakker0e4f9112014-04-17 12:39:05 +0200275 for( i = 0; i < POLARSSL_PK_DEBUG_MAX_ITEMS; i++ )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200276 {
277 if( items[i].type == POLARSSL_PK_DEBUG_NONE )
278 return;
279
280 snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
281 name[sizeof( name ) - 1] = '\0';
282
283 if( items[i].type == POLARSSL_PK_DEBUG_MPI )
284 debug_print_mpi( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200285 else
286#if defined(POLARSSL_ECP_C)
287 if( items[i].type == POLARSSL_PK_DEBUG_ECP )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200288 debug_print_ecp( ssl, level, file, line, name, items[i].value );
289 else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200290#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200291 debug_print_msg( ssl, level, file, line, "should not happen" );
292 }
293}
294
Paul Bakkerff60ee62010-03-16 21:09:09 +0000295void debug_print_crt( const ssl_context *ssl, int level,
296 const char *file, int line,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200297 const char *text, const x509_crt *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000298{
Paul Bakkerd98030e2009-05-02 15:13:40 +0000299 char str[1024], prefix[64];
Paul Bakkereaebbd52014-04-25 15:04:14 +0200300 int i = 0, maxlen = sizeof( prefix ) - 1, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000301
302 if( ssl->f_dbg == NULL || crt == NULL )
303 return;
304
Paul Bakkereaebbd52014-04-25 15:04:14 +0200305 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
306 {
307 snprintf( prefix, maxlen, "%s(%04d): ", file, line );
308 prefix[maxlen] = '\0';
309 }
310 else
311 prefix[0] = '\0';
312
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 maxlen = sizeof( str ) - 1;
314
Paul Bakker29087132010-03-21 21:03:34 +0000315 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000317 char buf[1024];
Paul Bakkerddf26b42013-09-18 13:46:23 +0200318 x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt );
Paul Bakker5121ce52009-01-03 21:22:43 +0000319
Paul Bakkereaebbd52014-04-25 15:04:14 +0200320 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
321 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
322
323 snprintf( str + idx, maxlen - idx, "%s #%d:\n%s",
324 text, ++i, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000325
326 str[maxlen] = '\0';
327 ssl->f_dbg( ssl->p_dbg, level, str );
328
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200329 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +0000330
331 crt = crt->next;
332 }
333}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200334#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000335
336#endif