blob: e014c0379d245c3b257317a1c363dbe1a476e1e5 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Debugging routines
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Paul Bakker40e46942009-01-03 21:51:57 +000029#if defined(POLARSSL_DEBUG_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Paul Bakker40e46942009-01-03 21:51:57 +000031#include "polarssl/debug.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000032
33#include <stdarg.h>
34#include <stdlib.h>
35
Paul Bakkerfa6a6202013-10-28 18:48:30 +010036#if defined(EFIX64) || defined(EFI32)
37#include <stdio.h>
38#endif
39
Paul Bakker6edcd412013-10-29 15:22:54 +010040#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
41#if !defined snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +000042#define snprintf _snprintf
43#endif
44
Paul Bakker6edcd412013-10-29 15:22:54 +010045#if !defined vsnprintf
Paul Bakker5121ce52009-01-03 21:22:43 +000046#define vsnprintf _vsnprintf
47#endif
Paul Bakker6edcd412013-10-29 15:22:54 +010048#endif /* _MSC_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +000049
Paul Bakkereaebbd52014-04-25 15:04:14 +020050static int debug_log_mode = POLARSSL_DEBUG_DFL_MODE;
Paul Bakkerc73079a2014-04-25 16:34:30 +020051static int debug_threshold = 0;
Paul Bakkereaebbd52014-04-25 15:04:14 +020052
53void debug_set_log_mode( int log_mode )
54{
55 debug_log_mode = log_mode;
56}
57
Paul Bakkerc73079a2014-04-25 16:34:30 +020058void debug_set_threshold( int threshold )
59{
60 debug_threshold = threshold;
61}
62
Paul Bakker5121ce52009-01-03 21:22:43 +000063char *debug_fmt( const char *format, ... )
64{
65 va_list argp;
66 static char str[512];
67 int maxlen = sizeof( str ) - 1;
68
69 va_start( argp, format );
70 vsnprintf( str, maxlen, format, argp );
71 va_end( argp );
72
73 str[maxlen] = '\0';
74 return( str );
75}
76
Paul Bakkerff60ee62010-03-16 21:09:09 +000077void debug_print_msg( const ssl_context *ssl, int level,
78 const char *file, int line, const char *text )
Paul Bakker5121ce52009-01-03 21:22:43 +000079{
80 char str[512];
81 int maxlen = sizeof( str ) - 1;
82
Paul Bakkerc73079a2014-04-25 16:34:30 +020083 if( ssl->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +000084 return;
85
Paul Bakkereaebbd52014-04-25 15:04:14 +020086 if( debug_log_mode == POLARSSL_DEBUG_LOG_RAW )
87 {
Paul Bakker5593f7c2014-05-06 10:29:28 +020088 ssl->f_dbg( ssl->p_dbg, level, text );
Paul Bakkereaebbd52014-04-25 15:04:14 +020089 return;
90 }
91
Paul Bakker5121ce52009-01-03 21:22:43 +000092 snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text );
93 str[maxlen] = '\0';
94 ssl->f_dbg( ssl->p_dbg, level, str );
95}
96
Paul Bakkerff60ee62010-03-16 21:09:09 +000097void debug_print_ret( const ssl_context *ssl, int level,
98 const char *file, int line,
99 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +0000100{
101 char str[512];
102 int maxlen = sizeof( str ) - 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +0200103 size_t idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
Paul Bakkerc73079a2014-04-25 16:34:30 +0200105 if( ssl->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 return;
107
Paul Bakkereaebbd52014-04-25 15:04:14 +0200108 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
109 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
110
111 snprintf( str + idx, maxlen - idx, "%s() returned %d (-0x%04x)\n",
112 text, ret, -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
114 str[maxlen] = '\0';
115 ssl->f_dbg( ssl->p_dbg, level, str );
116}
117
Paul Bakkerff60ee62010-03-16 21:09:09 +0000118void debug_print_buf( const ssl_context *ssl, int level,
119 const char *file, int line, const char *text,
Paul Bakker23986e52011-04-24 08:57:21 +0000120 unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000121{
122 char str[512];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100123 char txt[17];
Paul Bakkereaebbd52014-04-25 15:04:14 +0200124 size_t i, maxlen = sizeof( str ) - 1, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000125
Paul Bakkerc73079a2014-04-25 16:34:30 +0200126 if( ssl->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 return;
128
Paul Bakkereaebbd52014-04-25 15:04:14 +0200129 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
130 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
131
Peter Vaskovic8ebfe082014-05-24 15:19:35 +0200132 snprintf( str + idx, maxlen - idx, "dumping '%s' (%u bytes)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200133 text, (unsigned int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
135 str[maxlen] = '\0';
136 ssl->f_dbg( ssl->p_dbg, level, str );
137
Paul Bakker92478c32014-04-25 15:18:34 +0200138 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100139 memset( txt, 0, sizeof( txt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 for( i = 0; i < len; i++ )
141 {
142 if( i >= 4096 )
143 break;
144
145 if( i % 16 == 0 )
146 {
147 if( i > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200148 {
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100149 snprintf( str + idx, maxlen - idx, " %s\n", txt );
Paul Bakker92478c32014-04-25 15:18:34 +0200150 ssl->f_dbg( ssl->p_dbg, level, str );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100151
Paul Bakker92478c32014-04-25 15:18:34 +0200152 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100153 memset( txt, 0, sizeof( txt ) );
Paul Bakker92478c32014-04-25 15:18:34 +0200154 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000155
Paul Bakkereaebbd52014-04-25 15:04:14 +0200156 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
157 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
158
Paul Bakker92478c32014-04-25 15:18:34 +0200159 idx += snprintf( str + idx, maxlen - idx, "%04x: ",
160 (unsigned int) i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000161
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 }
163
Paul Bakker92478c32014-04-25 15:18:34 +0200164 idx += snprintf( str + idx, maxlen - idx, " %02x",
165 (unsigned int) buf[i] );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100166 txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 }
168
169 if( len > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200170 {
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100171 for( /* i = i */; i % 16 != 0; i++ )
172 idx += snprintf( str + idx, maxlen - idx, " " );
173
174 snprintf( str + idx, maxlen - idx, " %s\n", txt );
Paul Bakker92478c32014-04-25 15:18:34 +0200175 ssl->f_dbg( ssl->p_dbg, level, str );
176 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000177}
178
Paul Bakker41c83d32013-03-20 14:39:14 +0100179#if defined(POLARSSL_ECP_C)
180void debug_print_ecp( const ssl_context *ssl, int level,
181 const char *file, int line,
182 const char *text, const ecp_point *X )
183{
184 char str[512];
185 int maxlen = sizeof( str ) - 1;
186
Paul Bakkerc73079a2014-04-25 16:34:30 +0200187 if( ssl->f_dbg == NULL || level > debug_threshold )
188 return;
189
Paul Bakker41c83d32013-03-20 14:39:14 +0100190 snprintf( str, maxlen, "%s(X)", text );
191 str[maxlen] = '\0';
192 debug_print_mpi( ssl, level, file, line, str, &X->X );
193
194 snprintf( str, maxlen, "%s(Y)", text );
195 str[maxlen] = '\0';
196 debug_print_mpi( ssl, level, file, line, str, &X->Y );
Paul Bakker41c83d32013-03-20 14:39:14 +0100197}
198#endif /* POLARSSL_ECP_C */
199
Paul Bakkered27a042013-04-18 22:46:23 +0200200#if defined(POLARSSL_BIGNUM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +0000201void debug_print_mpi( const ssl_context *ssl, int level,
202 const char *file, int line,
203 const char *text, const mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000204{
205 char str[512];
Paul Bakker23986e52011-04-24 08:57:21 +0000206 int j, k, maxlen = sizeof( str ) - 1, zeros = 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +0200207 size_t i, n, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000208
Paul Bakkerc73079a2014-04-25 16:34:30 +0200209 if( ssl->f_dbg == NULL || X == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000210 return;
211
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000212 for( n = X->n - 1; n > 0; n-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 if( X->p[n] != 0 )
214 break;
215
Paul Bakkera755ca12011-04-24 09:11:17 +0000216 for( j = ( sizeof(t_uint) << 3 ) - 1; j >= 0; j-- )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000217 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
218 break;
219
Paul Bakkereaebbd52014-04-25 15:04:14 +0200220 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
221 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
222
223 snprintf( str + idx, maxlen - idx, "value of '%s' (%d bits) is:\n",
224 text, (int) ( ( n * ( sizeof(t_uint) << 3 ) ) + j + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000225
226 str[maxlen] = '\0';
227 ssl->f_dbg( ssl->p_dbg, level, str );
228
Paul Bakker92478c32014-04-25 15:18:34 +0200229 idx = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000230 for( i = n + 1, j = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000231 {
Paul Bakker23986e52011-04-24 08:57:21 +0000232 if( zeros && X->p[i - 1] == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000233 continue;
Paul Bakker5121ce52009-01-03 21:22:43 +0000234
Paul Bakkera755ca12011-04-24 09:11:17 +0000235 for( k = sizeof( t_uint ) - 1; k >= 0; k-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000236 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200237 if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000238 continue;
239 else
240 zeros = 0;
241
242 if( j % 16 == 0 )
243 {
244 if( j > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200245 {
246 snprintf( str + idx, maxlen - idx, "\n" );
247 ssl->f_dbg( ssl->p_dbg, level, str );
248 idx = 0;
249 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000250
Paul Bakkereaebbd52014-04-25 15:04:14 +0200251 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
Paul Bakker92478c32014-04-25 15:18:34 +0200252 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000253 }
254
Paul Bakker92478c32014-04-25 15:18:34 +0200255 idx += snprintf( str + idx, maxlen - idx, " %02x", (unsigned int)
Paul Bakker66d5d072014-06-17 16:39:18 +0200256 ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000257
258 j++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000259 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000260
261 }
262
263 if( zeros == 1 )
264 {
Paul Bakkereaebbd52014-04-25 15:04:14 +0200265 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
266 {
Paul Bakker92478c32014-04-25 15:18:34 +0200267 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000268
Paul Bakkereaebbd52014-04-25 15:04:14 +0200269 }
Paul Bakker92478c32014-04-25 15:18:34 +0200270 idx += snprintf( str + idx, maxlen - idx, " 00" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000271 }
272
Paul Bakker92478c32014-04-25 15:18:34 +0200273 snprintf( str + idx, maxlen - idx, "\n" );
274 ssl->f_dbg( ssl->p_dbg, level, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000275}
Paul Bakkered27a042013-04-18 22:46:23 +0200276#endif /* POLARSSL_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000277
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200278#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200279static void debug_print_pk( const ssl_context *ssl, int level,
280 const char *file, int line,
281 const char *text, const pk_context *pk )
282{
283 size_t i;
284 pk_debug_item items[POLARSSL_PK_DEBUG_MAX_ITEMS];
285 char name[16];
286
287 memset( items, 0, sizeof( items ) );
288
289 if( pk_debug( pk, items ) != 0 )
290 {
291 debug_print_msg( ssl, level, file, line, "invalid PK context" );
292 return;
293 }
294
Paul Bakker0e4f9112014-04-17 12:39:05 +0200295 for( i = 0; i < POLARSSL_PK_DEBUG_MAX_ITEMS; i++ )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200296 {
297 if( items[i].type == POLARSSL_PK_DEBUG_NONE )
298 return;
299
300 snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
301 name[sizeof( name ) - 1] = '\0';
302
303 if( items[i].type == POLARSSL_PK_DEBUG_MPI )
304 debug_print_mpi( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200305 else
306#if defined(POLARSSL_ECP_C)
307 if( items[i].type == POLARSSL_PK_DEBUG_ECP )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200308 debug_print_ecp( ssl, level, file, line, name, items[i].value );
309 else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200310#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200311 debug_print_msg( ssl, level, file, line, "should not happen" );
312 }
313}
314
Paul Bakkerff60ee62010-03-16 21:09:09 +0000315void debug_print_crt( const ssl_context *ssl, int level,
316 const char *file, int line,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200317 const char *text, const x509_crt *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000318{
Paul Bakkerd98030e2009-05-02 15:13:40 +0000319 char str[1024], prefix[64];
Paul Bakkereaebbd52014-04-25 15:04:14 +0200320 int i = 0, maxlen = sizeof( prefix ) - 1, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000321
Paul Bakkerc73079a2014-04-25 16:34:30 +0200322 if( ssl->f_dbg == NULL || crt == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000323 return;
324
Paul Bakkereaebbd52014-04-25 15:04:14 +0200325 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
326 {
327 snprintf( prefix, maxlen, "%s(%04d): ", file, line );
328 prefix[maxlen] = '\0';
329 }
330 else
331 prefix[0] = '\0';
332
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 maxlen = sizeof( str ) - 1;
334
Paul Bakker29087132010-03-21 21:03:34 +0000335 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000336 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000337 char buf[1024];
Paul Bakkerddf26b42013-09-18 13:46:23 +0200338 x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt );
Paul Bakker5121ce52009-01-03 21:22:43 +0000339
Paul Bakkereaebbd52014-04-25 15:04:14 +0200340 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
341 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
342
343 snprintf( str + idx, maxlen - idx, "%s #%d:\n%s",
344 text, ++i, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000345
346 str[maxlen] = '\0';
347 ssl->f_dbg( ssl->p_dbg, level, str );
348
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200349 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +0000350
351 crt = crt->next;
352 }
353}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200354#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000355
Paul Bakker9af723c2014-05-01 13:03:14 +0200356#endif /* POLARSSL_DEBUG_C */