blob: f327baab9019aa4be5d1ae6110e35b8f66ba03c3 [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é-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://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>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010034#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000035#include <string.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010036
Paul Bakker6edcd412013-10-29 15:22:54 +010037#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
38#if !defined snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +000039#define snprintf _snprintf
40#endif
41
Paul Bakker6edcd412013-10-29 15:22:54 +010042#if !defined vsnprintf
Paul Bakker5121ce52009-01-03 21:22:43 +000043#define vsnprintf _vsnprintf
44#endif
Paul Bakker6edcd412013-10-29 15:22:54 +010045#endif /* _MSC_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +000046
Paul Bakkereaebbd52014-04-25 15:04:14 +020047static int debug_log_mode = POLARSSL_DEBUG_DFL_MODE;
Paul Bakkerc73079a2014-04-25 16:34:30 +020048static int debug_threshold = 0;
Paul Bakkereaebbd52014-04-25 15:04:14 +020049
50void debug_set_log_mode( int log_mode )
51{
52 debug_log_mode = log_mode;
53}
54
Paul Bakkerc73079a2014-04-25 16:34:30 +020055void debug_set_threshold( int threshold )
56{
57 debug_threshold = threshold;
58}
59
Paul Bakker5121ce52009-01-03 21:22:43 +000060char *debug_fmt( const char *format, ... )
61{
62 va_list argp;
63 static char str[512];
64 int maxlen = sizeof( str ) - 1;
65
66 va_start( argp, format );
67 vsnprintf( str, maxlen, format, argp );
68 va_end( argp );
69
70 str[maxlen] = '\0';
71 return( str );
72}
73
Paul Bakkerff60ee62010-03-16 21:09:09 +000074void debug_print_msg( const ssl_context *ssl, int level,
75 const char *file, int line, const char *text )
Paul Bakker5121ce52009-01-03 21:22:43 +000076{
77 char str[512];
78 int maxlen = sizeof( str ) - 1;
79
Paul Bakkerc73079a2014-04-25 16:34:30 +020080 if( ssl->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +000081 return;
82
Paul Bakkereaebbd52014-04-25 15:04:14 +020083 if( debug_log_mode == POLARSSL_DEBUG_LOG_RAW )
84 {
Paul Bakker5593f7c2014-05-06 10:29:28 +020085 ssl->f_dbg( ssl->p_dbg, level, text );
Paul Bakkereaebbd52014-04-25 15:04:14 +020086 return;
87 }
88
Paul Bakker5121ce52009-01-03 21:22:43 +000089 snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text );
90 str[maxlen] = '\0';
91 ssl->f_dbg( ssl->p_dbg, level, str );
92}
93
Paul Bakkerff60ee62010-03-16 21:09:09 +000094void debug_print_ret( const ssl_context *ssl, int level,
95 const char *file, int line,
96 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +000097{
98 char str[512];
99 int maxlen = sizeof( str ) - 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +0200100 size_t idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
Paul Bakkerc73079a2014-04-25 16:34:30 +0200102 if( ssl->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 return;
104
Paul Bakkereaebbd52014-04-25 15:04:14 +0200105 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
106 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
107
108 snprintf( str + idx, maxlen - idx, "%s() returned %d (-0x%04x)\n",
109 text, ret, -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
111 str[maxlen] = '\0';
112 ssl->f_dbg( ssl->p_dbg, level, str );
113}
114
Paul Bakkerff60ee62010-03-16 21:09:09 +0000115void debug_print_buf( const ssl_context *ssl, int level,
116 const char *file, int line, const char *text,
Paul Bakker23986e52011-04-24 08:57:21 +0000117 unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000118{
119 char str[512];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100120 char txt[17];
Paul Bakkereaebbd52014-04-25 15:04:14 +0200121 size_t i, maxlen = sizeof( str ) - 1, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000122
Paul Bakkerc73079a2014-04-25 16:34:30 +0200123 if( ssl->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 return;
125
Paul Bakkereaebbd52014-04-25 15:04:14 +0200126 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
127 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
128
Peter Vaskovic8ebfe082014-05-24 15:19:35 +0200129 snprintf( str + idx, maxlen - idx, "dumping '%s' (%u bytes)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200130 text, (unsigned int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
132 str[maxlen] = '\0';
133 ssl->f_dbg( ssl->p_dbg, level, str );
134
Paul Bakker92478c32014-04-25 15:18:34 +0200135 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100136 memset( txt, 0, sizeof( txt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 for( i = 0; i < len; i++ )
138 {
139 if( i >= 4096 )
140 break;
141
142 if( i % 16 == 0 )
143 {
144 if( i > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200145 {
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100146 snprintf( str + idx, maxlen - idx, " %s\n", txt );
Paul Bakker92478c32014-04-25 15:18:34 +0200147 ssl->f_dbg( ssl->p_dbg, level, str );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100148
Paul Bakker92478c32014-04-25 15:18:34 +0200149 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100150 memset( txt, 0, sizeof( txt ) );
Paul Bakker92478c32014-04-25 15:18:34 +0200151 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
Paul Bakkereaebbd52014-04-25 15:04:14 +0200153 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
154 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
155
Paul Bakker92478c32014-04-25 15:18:34 +0200156 idx += snprintf( str + idx, maxlen - idx, "%04x: ",
157 (unsigned int) i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 }
160
Paul Bakker92478c32014-04-25 15:18:34 +0200161 idx += snprintf( str + idx, maxlen - idx, " %02x",
162 (unsigned int) buf[i] );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100163 txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 }
165
166 if( len > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200167 {
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100168 for( /* i = i */; i % 16 != 0; i++ )
169 idx += snprintf( str + idx, maxlen - idx, " " );
170
171 snprintf( str + idx, maxlen - idx, " %s\n", txt );
Paul Bakker92478c32014-04-25 15:18:34 +0200172 ssl->f_dbg( ssl->p_dbg, level, str );
173 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000174}
175
Paul Bakker41c83d32013-03-20 14:39:14 +0100176#if defined(POLARSSL_ECP_C)
177void debug_print_ecp( const ssl_context *ssl, int level,
178 const char *file, int line,
179 const char *text, const ecp_point *X )
180{
181 char str[512];
182 int maxlen = sizeof( str ) - 1;
183
Paul Bakkerc73079a2014-04-25 16:34:30 +0200184 if( ssl->f_dbg == NULL || level > debug_threshold )
185 return;
186
Paul Bakker41c83d32013-03-20 14:39:14 +0100187 snprintf( str, maxlen, "%s(X)", text );
188 str[maxlen] = '\0';
189 debug_print_mpi( ssl, level, file, line, str, &X->X );
190
191 snprintf( str, maxlen, "%s(Y)", text );
192 str[maxlen] = '\0';
193 debug_print_mpi( ssl, level, file, line, str, &X->Y );
Paul Bakker41c83d32013-03-20 14:39:14 +0100194}
195#endif /* POLARSSL_ECP_C */
196
Paul Bakkered27a042013-04-18 22:46:23 +0200197#if defined(POLARSSL_BIGNUM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +0000198void debug_print_mpi( const ssl_context *ssl, int level,
199 const char *file, int line,
200 const char *text, const mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000201{
202 char str[512];
Paul Bakker23986e52011-04-24 08:57:21 +0000203 int j, k, maxlen = sizeof( str ) - 1, zeros = 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +0200204 size_t i, n, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000205
Paul Bakkerc73079a2014-04-25 16:34:30 +0200206 if( ssl->f_dbg == NULL || X == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 return;
208
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000209 for( n = X->n - 1; n > 0; n-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000210 if( X->p[n] != 0 )
211 break;
212
Paul Bakkera755ca12011-04-24 09:11:17 +0000213 for( j = ( sizeof(t_uint) << 3 ) - 1; j >= 0; j-- )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000214 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
215 break;
216
Paul Bakkereaebbd52014-04-25 15:04:14 +0200217 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
218 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
219
220 snprintf( str + idx, maxlen - idx, "value of '%s' (%d bits) is:\n",
221 text, (int) ( ( n * ( sizeof(t_uint) << 3 ) ) + j + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000222
223 str[maxlen] = '\0';
224 ssl->f_dbg( ssl->p_dbg, level, str );
225
Paul Bakker92478c32014-04-25 15:18:34 +0200226 idx = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000227 for( i = n + 1, j = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 {
Paul Bakker23986e52011-04-24 08:57:21 +0000229 if( zeros && X->p[i - 1] == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000230 continue;
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
Paul Bakkera755ca12011-04-24 09:11:17 +0000232 for( k = sizeof( t_uint ) - 1; k >= 0; k-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000233 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200234 if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000235 continue;
236 else
237 zeros = 0;
238
239 if( j % 16 == 0 )
240 {
241 if( j > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200242 {
243 snprintf( str + idx, maxlen - idx, "\n" );
244 ssl->f_dbg( ssl->p_dbg, level, str );
245 idx = 0;
246 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000247
Paul Bakkereaebbd52014-04-25 15:04:14 +0200248 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
Paul Bakker92478c32014-04-25 15:18:34 +0200249 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000250 }
251
Paul Bakker92478c32014-04-25 15:18:34 +0200252 idx += snprintf( str + idx, maxlen - idx, " %02x", (unsigned int)
Paul Bakker66d5d072014-06-17 16:39:18 +0200253 ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000254
255 j++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000256 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000257
258 }
259
260 if( zeros == 1 )
261 {
Paul Bakkereaebbd52014-04-25 15:04:14 +0200262 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
263 {
Paul Bakker92478c32014-04-25 15:18:34 +0200264 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000265
Paul Bakkereaebbd52014-04-25 15:04:14 +0200266 }
Paul Bakker92478c32014-04-25 15:18:34 +0200267 idx += snprintf( str + idx, maxlen - idx, " 00" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000268 }
269
Paul Bakker92478c32014-04-25 15:18:34 +0200270 snprintf( str + idx, maxlen - idx, "\n" );
271 ssl->f_dbg( ssl->p_dbg, level, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000272}
Paul Bakkered27a042013-04-18 22:46:23 +0200273#endif /* POLARSSL_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000274
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200275#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200276static void debug_print_pk( const ssl_context *ssl, int level,
277 const char *file, int line,
278 const char *text, const pk_context *pk )
279{
280 size_t i;
281 pk_debug_item items[POLARSSL_PK_DEBUG_MAX_ITEMS];
282 char name[16];
283
284 memset( items, 0, sizeof( items ) );
285
286 if( pk_debug( pk, items ) != 0 )
287 {
288 debug_print_msg( ssl, level, file, line, "invalid PK context" );
289 return;
290 }
291
Paul Bakker0e4f9112014-04-17 12:39:05 +0200292 for( i = 0; i < POLARSSL_PK_DEBUG_MAX_ITEMS; i++ )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200293 {
294 if( items[i].type == POLARSSL_PK_DEBUG_NONE )
295 return;
296
297 snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
298 name[sizeof( name ) - 1] = '\0';
299
300 if( items[i].type == POLARSSL_PK_DEBUG_MPI )
301 debug_print_mpi( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200302 else
303#if defined(POLARSSL_ECP_C)
304 if( items[i].type == POLARSSL_PK_DEBUG_ECP )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200305 debug_print_ecp( ssl, level, file, line, name, items[i].value );
306 else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200307#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200308 debug_print_msg( ssl, level, file, line, "should not happen" );
309 }
310}
311
Paul Bakkerff60ee62010-03-16 21:09:09 +0000312void debug_print_crt( const ssl_context *ssl, int level,
313 const char *file, int line,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200314 const char *text, const x509_crt *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000315{
Paul Bakkerd98030e2009-05-02 15:13:40 +0000316 char str[1024], prefix[64];
Paul Bakkereaebbd52014-04-25 15:04:14 +0200317 int i = 0, maxlen = sizeof( prefix ) - 1, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000318
Paul Bakkerc73079a2014-04-25 16:34:30 +0200319 if( ssl->f_dbg == NULL || crt == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000320 return;
321
Paul Bakkereaebbd52014-04-25 15:04:14 +0200322 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
323 {
324 snprintf( prefix, maxlen, "%s(%04d): ", file, line );
325 prefix[maxlen] = '\0';
326 }
327 else
328 prefix[0] = '\0';
329
Paul Bakker5121ce52009-01-03 21:22:43 +0000330 maxlen = sizeof( str ) - 1;
331
Paul Bakker29087132010-03-21 21:03:34 +0000332 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000334 char buf[1024];
Paul Bakkerddf26b42013-09-18 13:46:23 +0200335 x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt );
Paul Bakker5121ce52009-01-03 21:22:43 +0000336
Paul Bakkereaebbd52014-04-25 15:04:14 +0200337 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
338 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
339
340 snprintf( str + idx, maxlen - idx, "%s #%d:\n%s",
341 text, ++i, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000342
343 str[maxlen] = '\0';
344 ssl->f_dbg( ssl->p_dbg, level, str );
345
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200346 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +0000347
348 crt = crt->next;
349 }
350}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200351#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000352
Paul Bakker9af723c2014-05-01 13:03:14 +0200353#endif /* POLARSSL_DEBUG_C */