blob: a81f502bfb763e2151c1d8a0b877257cac4a935e [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Debugging routines
3 *
Paul Bakker66d5d072014-06-17 16:39:18 +02004 * Copyright (C) 2006-2014, 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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakker40e46942009-01-03 21:51:57 +000032#if defined(POLARSSL_DEBUG_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/debug.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000035
36#include <stdarg.h>
37#include <stdlib.h>
38
Paul Bakkerfa6a6202013-10-28 18:48:30 +010039#if defined(EFIX64) || defined(EFI32)
40#include <stdio.h>
41#endif
42
Paul Bakker6edcd412013-10-29 15:22:54 +010043#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
44#if !defined snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +000045#define snprintf _snprintf
46#endif
47
Paul Bakker6edcd412013-10-29 15:22:54 +010048#if !defined vsnprintf
Paul Bakker5121ce52009-01-03 21:22:43 +000049#define vsnprintf _vsnprintf
50#endif
Paul Bakker6edcd412013-10-29 15:22:54 +010051#endif /* _MSC_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +000052
Paul Bakkereaebbd52014-04-25 15:04:14 +020053static int debug_log_mode = POLARSSL_DEBUG_DFL_MODE;
Paul Bakkerc73079a2014-04-25 16:34:30 +020054static int debug_threshold = 0;
Paul Bakkereaebbd52014-04-25 15:04:14 +020055
56void debug_set_log_mode( int log_mode )
57{
58 debug_log_mode = log_mode;
59}
60
Paul Bakkerc73079a2014-04-25 16:34:30 +020061void debug_set_threshold( int threshold )
62{
63 debug_threshold = threshold;
64}
65
Paul Bakker5121ce52009-01-03 21:22:43 +000066char *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
Paul Bakkerff60ee62010-03-16 21:09:09 +000080void debug_print_msg( const ssl_context *ssl, int level,
81 const char *file, int line, const char *text )
Paul Bakker5121ce52009-01-03 21:22:43 +000082{
83 char str[512];
84 int maxlen = sizeof( str ) - 1;
85
Paul Bakkerc73079a2014-04-25 16:34:30 +020086 if( ssl->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +000087 return;
88
Paul Bakkereaebbd52014-04-25 15:04:14 +020089 if( debug_log_mode == POLARSSL_DEBUG_LOG_RAW )
90 {
Paul Bakker5593f7c2014-05-06 10:29:28 +020091 ssl->f_dbg( ssl->p_dbg, level, text );
Paul Bakkereaebbd52014-04-25 15:04:14 +020092 return;
93 }
94
Paul Bakker5121ce52009-01-03 21:22:43 +000095 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
Paul Bakkerff60ee62010-03-16 21:09:09 +0000100void debug_print_ret( const ssl_context *ssl, int level,
101 const char *file, int line,
102 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +0000103{
104 char str[512];
105 int maxlen = sizeof( str ) - 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +0200106 size_t idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
Paul Bakkerc73079a2014-04-25 16:34:30 +0200108 if( ssl->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 return;
110
Paul Bakkereaebbd52014-04-25 15:04:14 +0200111 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 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000116
117 str[maxlen] = '\0';
118 ssl->f_dbg( ssl->p_dbg, level, str );
119}
120
Paul Bakkerff60ee62010-03-16 21:09:09 +0000121void debug_print_buf( const ssl_context *ssl, int level,
122 const char *file, int line, const char *text,
Paul Bakker23986e52011-04-24 08:57:21 +0000123 unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000124{
125 char str[512];
Paul Bakkereaebbd52014-04-25 15:04:14 +0200126 size_t i, maxlen = sizeof( str ) - 1, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000127
Paul Bakkerc73079a2014-04-25 16:34:30 +0200128 if( ssl->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 return;
130
Paul Bakkereaebbd52014-04-25 15:04:14 +0200131 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
132 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
133
Peter Vaskovic8ebfe082014-05-24 15:19:35 +0200134 snprintf( str + idx, maxlen - idx, "dumping '%s' (%u bytes)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200135 text, (unsigned int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000136
137 str[maxlen] = '\0';
138 ssl->f_dbg( ssl->p_dbg, level, str );
139
Paul Bakker92478c32014-04-25 15:18:34 +0200140 idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 for( i = 0; i < len; i++ )
142 {
143 if( i >= 4096 )
144 break;
145
146 if( i % 16 == 0 )
147 {
148 if( i > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200149 {
150 snprintf( str + idx, maxlen - idx, "\n" );
151 ssl->f_dbg( ssl->p_dbg, level, str );
152 idx = 0;
153 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
Paul Bakkereaebbd52014-04-25 15:04:14 +0200155 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
156 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
157
Paul Bakker92478c32014-04-25 15:18:34 +0200158 idx += snprintf( str + idx, maxlen - idx, "%04x: ",
159 (unsigned int) i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000160
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 }
162
Paul Bakker92478c32014-04-25 15:18:34 +0200163 idx += snprintf( str + idx, maxlen - idx, " %02x",
164 (unsigned int) buf[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 }
166
167 if( len > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200168 {
169 snprintf( str + idx, maxlen - idx, "\n" );
170 ssl->f_dbg( ssl->p_dbg, level, str );
171 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000172}
173
Paul Bakker41c83d32013-03-20 14:39:14 +0100174#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
Paul Bakkerc73079a2014-04-25 16:34:30 +0200182 if( ssl->f_dbg == NULL || level > debug_threshold )
183 return;
184
Paul Bakker41c83d32013-03-20 14:39:14 +0100185 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 );
Paul Bakker41c83d32013-03-20 14:39:14 +0100192}
193#endif /* POLARSSL_ECP_C */
194
Paul Bakkered27a042013-04-18 22:46:23 +0200195#if defined(POLARSSL_BIGNUM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +0000196void debug_print_mpi( const ssl_context *ssl, int level,
197 const char *file, int line,
198 const char *text, const mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000199{
200 char str[512];
Paul Bakker23986e52011-04-24 08:57:21 +0000201 int j, k, maxlen = sizeof( str ) - 1, zeros = 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +0200202 size_t i, n, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000203
Paul Bakkerc73079a2014-04-25 16:34:30 +0200204 if( ssl->f_dbg == NULL || X == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 return;
206
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000207 for( n = X->n - 1; n > 0; n-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000208 if( X->p[n] != 0 )
209 break;
210
Paul Bakkera755ca12011-04-24 09:11:17 +0000211 for( j = ( sizeof(t_uint) << 3 ) - 1; j >= 0; j-- )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000212 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
213 break;
214
Paul Bakkereaebbd52014-04-25 15:04:14 +0200215 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 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000220
221 str[maxlen] = '\0';
222 ssl->f_dbg( ssl->p_dbg, level, str );
223
Paul Bakker92478c32014-04-25 15:18:34 +0200224 idx = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000225 for( i = n + 1, j = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 {
Paul Bakker23986e52011-04-24 08:57:21 +0000227 if( zeros && X->p[i - 1] == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000228 continue;
Paul Bakker5121ce52009-01-03 21:22:43 +0000229
Paul Bakkera755ca12011-04-24 09:11:17 +0000230 for( k = sizeof( t_uint ) - 1; k >= 0; k-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000231 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200232 if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000233 continue;
234 else
235 zeros = 0;
236
237 if( j % 16 == 0 )
238 {
239 if( j > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200240 {
241 snprintf( str + idx, maxlen - idx, "\n" );
242 ssl->f_dbg( ssl->p_dbg, level, str );
243 idx = 0;
244 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000245
Paul Bakkereaebbd52014-04-25 15:04:14 +0200246 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
Paul Bakker92478c32014-04-25 15:18:34 +0200247 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000248 }
249
Paul Bakker92478c32014-04-25 15:18:34 +0200250 idx += snprintf( str + idx, maxlen - idx, " %02x", (unsigned int)
Paul Bakker66d5d072014-06-17 16:39:18 +0200251 ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000252
253 j++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000254 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000255
256 }
257
258 if( zeros == 1 )
259 {
Paul Bakkereaebbd52014-04-25 15:04:14 +0200260 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
261 {
Paul Bakker92478c32014-04-25 15:18:34 +0200262 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000263
Paul Bakkereaebbd52014-04-25 15:04:14 +0200264 }
Paul Bakker92478c32014-04-25 15:18:34 +0200265 idx += snprintf( str + idx, maxlen - idx, " 00" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 }
267
Paul Bakker92478c32014-04-25 15:18:34 +0200268 snprintf( str + idx, maxlen - idx, "\n" );
269 ssl->f_dbg( ssl->p_dbg, level, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000270}
Paul Bakkered27a042013-04-18 22:46:23 +0200271#endif /* POLARSSL_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000272
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200273#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200274static 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;
279 pk_debug_item items[POLARSSL_PK_DEBUG_MAX_ITEMS];
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
Paul Bakker0e4f9112014-04-17 12:39:05 +0200290 for( i = 0; i < POLARSSL_PK_DEBUG_MAX_ITEMS; i++ )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200291 {
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 );
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200300 else
301#if defined(POLARSSL_ECP_C)
302 if( items[i].type == POLARSSL_PK_DEBUG_ECP )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200303 debug_print_ecp( ssl, level, file, line, name, items[i].value );
304 else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200305#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200306 debug_print_msg( ssl, level, file, line, "should not happen" );
307 }
308}
309
Paul Bakkerff60ee62010-03-16 21:09:09 +0000310void debug_print_crt( const ssl_context *ssl, int level,
311 const char *file, int line,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200312 const char *text, const x509_crt *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000313{
Paul Bakkerd98030e2009-05-02 15:13:40 +0000314 char str[1024], prefix[64];
Paul Bakkereaebbd52014-04-25 15:04:14 +0200315 int i = 0, maxlen = sizeof( prefix ) - 1, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000316
Paul Bakkerc73079a2014-04-25 16:34:30 +0200317 if( ssl->f_dbg == NULL || crt == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000318 return;
319
Paul Bakkereaebbd52014-04-25 15:04:14 +0200320 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
Paul Bakker5121ce52009-01-03 21:22:43 +0000328 maxlen = sizeof( str ) - 1;
329
Paul Bakker29087132010-03-21 21:03:34 +0000330 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000331 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000332 char buf[1024];
Paul Bakkerddf26b42013-09-18 13:46:23 +0200333 x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt );
Paul Bakker5121ce52009-01-03 21:22:43 +0000334
Paul Bakkereaebbd52014-04-25 15:04:14 +0200335 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 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000340
341 str[maxlen] = '\0';
342 ssl->f_dbg( ssl->p_dbg, level, str );
343
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200344 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +0000345
346 crt = crt->next;
347 }
348}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200349#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000350
Paul Bakker9af723c2014-05-01 13:03:14 +0200351#endif /* POLARSSL_DEBUG_C */