blob: 921249bd4168d6432b225ee894654c446257c858 [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é-Gonnard967a2a52015-01-22 14:28:16 +00006 * This file is part of mbed TLS (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 Bakker5121ce52009-01-03 21:22:43 +00009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker40e46942009-01-03 21:51:57 +000025#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#else
27#include POLARSSL_CONFIG_FILE
28#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakker40e46942009-01-03 21:51:57 +000030#if defined(POLARSSL_DEBUG_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Paul Bakker40e46942009-01-03 21:51:57 +000032#include "polarssl/debug.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000033
34#include <stdarg.h>
35#include <stdlib.h>
36
Paul Bakkerfa6a6202013-10-28 18:48:30 +010037#if defined(EFIX64) || defined(EFI32)
38#include <stdio.h>
39#endif
40
Paul Bakker6edcd412013-10-29 15:22:54 +010041#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
42#if !defined snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +000043#define snprintf _snprintf
44#endif
45
Paul Bakker6edcd412013-10-29 15:22:54 +010046#if !defined vsnprintf
Paul Bakker5121ce52009-01-03 21:22:43 +000047#define vsnprintf _vsnprintf
48#endif
Paul Bakker6edcd412013-10-29 15:22:54 +010049#endif /* _MSC_VER */
Paul Bakker5121ce52009-01-03 21:22:43 +000050
Paul Bakkereaebbd52014-04-25 15:04:14 +020051static int debug_log_mode = POLARSSL_DEBUG_DFL_MODE;
Paul Bakkerc73079a2014-04-25 16:34:30 +020052static int debug_threshold = 0;
Paul Bakkereaebbd52014-04-25 15:04:14 +020053
54void debug_set_log_mode( int log_mode )
55{
56 debug_log_mode = log_mode;
57}
58
Paul Bakkerc73079a2014-04-25 16:34:30 +020059void debug_set_threshold( int threshold )
60{
61 debug_threshold = threshold;
62}
63
Paul Bakker5121ce52009-01-03 21:22:43 +000064char *debug_fmt( const char *format, ... )
65{
66 va_list argp;
67 static char str[512];
68 int maxlen = sizeof( str ) - 1;
69
70 va_start( argp, format );
71 vsnprintf( str, maxlen, format, argp );
72 va_end( argp );
73
74 str[maxlen] = '\0';
75 return( str );
76}
77
Paul Bakkerff60ee62010-03-16 21:09:09 +000078void debug_print_msg( const ssl_context *ssl, int level,
79 const char *file, int line, const char *text )
Paul Bakker5121ce52009-01-03 21:22:43 +000080{
81 char str[512];
82 int maxlen = sizeof( str ) - 1;
83
Paul Bakkerc73079a2014-04-25 16:34:30 +020084 if( ssl->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +000085 return;
86
Paul Bakkereaebbd52014-04-25 15:04:14 +020087 if( debug_log_mode == POLARSSL_DEBUG_LOG_RAW )
88 {
Paul Bakker5593f7c2014-05-06 10:29:28 +020089 ssl->f_dbg( ssl->p_dbg, level, text );
Paul Bakkereaebbd52014-04-25 15:04:14 +020090 return;
91 }
92
Paul Bakker5121ce52009-01-03 21:22:43 +000093 snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text );
94 str[maxlen] = '\0';
95 ssl->f_dbg( ssl->p_dbg, level, str );
96}
97
Paul Bakkerff60ee62010-03-16 21:09:09 +000098void debug_print_ret( const ssl_context *ssl, int level,
99 const char *file, int line,
100 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +0000101{
102 char str[512];
103 int maxlen = sizeof( str ) - 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +0200104 size_t idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
Paul Bakkerc73079a2014-04-25 16:34:30 +0200106 if( ssl->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 return;
108
Paul Bakkereaebbd52014-04-25 15:04:14 +0200109 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
110 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
111
112 snprintf( str + idx, maxlen - idx, "%s() returned %d (-0x%04x)\n",
113 text, ret, -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
115 str[maxlen] = '\0';
116 ssl->f_dbg( ssl->p_dbg, level, str );
117}
118
Paul Bakkerff60ee62010-03-16 21:09:09 +0000119void debug_print_buf( const ssl_context *ssl, int level,
120 const char *file, int line, const char *text,
Paul Bakker23986e52011-04-24 08:57:21 +0000121 unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000122{
123 char str[512];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100124 char txt[17];
Paul Bakkereaebbd52014-04-25 15:04:14 +0200125 size_t i, maxlen = sizeof( str ) - 1, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
Paul Bakkerc73079a2014-04-25 16:34:30 +0200127 if( ssl->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 return;
129
Paul Bakkereaebbd52014-04-25 15:04:14 +0200130 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
131 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
132
Peter Vaskovic8ebfe082014-05-24 15:19:35 +0200133 snprintf( str + idx, maxlen - idx, "dumping '%s' (%u bytes)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200134 text, (unsigned int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000135
136 str[maxlen] = '\0';
137 ssl->f_dbg( ssl->p_dbg, level, str );
138
Paul Bakker92478c32014-04-25 15:18:34 +0200139 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100140 memset( txt, 0, sizeof( txt ) );
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 {
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100150 snprintf( str + idx, maxlen - idx, " %s\n", txt );
Paul Bakker92478c32014-04-25 15:18:34 +0200151 ssl->f_dbg( ssl->p_dbg, level, str );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100152
Paul Bakker92478c32014-04-25 15:18:34 +0200153 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100154 memset( txt, 0, sizeof( txt ) );
Paul Bakker92478c32014-04-25 15:18:34 +0200155 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
Paul Bakkereaebbd52014-04-25 15:04:14 +0200157 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
158 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
159
Paul Bakker92478c32014-04-25 15:18:34 +0200160 idx += snprintf( str + idx, maxlen - idx, "%04x: ",
161 (unsigned int) i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 }
164
Paul Bakker92478c32014-04-25 15:18:34 +0200165 idx += snprintf( str + idx, maxlen - idx, " %02x",
166 (unsigned int) buf[i] );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100167 txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
Paul Bakker5121ce52009-01-03 21:22:43 +0000168 }
169
170 if( len > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200171 {
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100172 for( /* i = i */; i % 16 != 0; i++ )
173 idx += snprintf( str + idx, maxlen - idx, " " );
174
175 snprintf( str + idx, maxlen - idx, " %s\n", txt );
Paul Bakker92478c32014-04-25 15:18:34 +0200176 ssl->f_dbg( ssl->p_dbg, level, str );
177 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000178}
179
Paul Bakker41c83d32013-03-20 14:39:14 +0100180#if defined(POLARSSL_ECP_C)
181void debug_print_ecp( const ssl_context *ssl, int level,
182 const char *file, int line,
183 const char *text, const ecp_point *X )
184{
185 char str[512];
186 int maxlen = sizeof( str ) - 1;
187
Paul Bakkerc73079a2014-04-25 16:34:30 +0200188 if( ssl->f_dbg == NULL || level > debug_threshold )
189 return;
190
Paul Bakker41c83d32013-03-20 14:39:14 +0100191 snprintf( str, maxlen, "%s(X)", text );
192 str[maxlen] = '\0';
193 debug_print_mpi( ssl, level, file, line, str, &X->X );
194
195 snprintf( str, maxlen, "%s(Y)", text );
196 str[maxlen] = '\0';
197 debug_print_mpi( ssl, level, file, line, str, &X->Y );
Paul Bakker41c83d32013-03-20 14:39:14 +0100198}
199#endif /* POLARSSL_ECP_C */
200
Paul Bakkered27a042013-04-18 22:46:23 +0200201#if defined(POLARSSL_BIGNUM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +0000202void debug_print_mpi( const ssl_context *ssl, int level,
203 const char *file, int line,
204 const char *text, const mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000205{
206 char str[512];
Paul Bakker23986e52011-04-24 08:57:21 +0000207 int j, k, maxlen = sizeof( str ) - 1, zeros = 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +0200208 size_t i, n, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000209
Paul Bakkerc73079a2014-04-25 16:34:30 +0200210 if( ssl->f_dbg == NULL || X == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 return;
212
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000213 for( n = X->n - 1; n > 0; n-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 if( X->p[n] != 0 )
215 break;
216
Paul Bakkera755ca12011-04-24 09:11:17 +0000217 for( j = ( sizeof(t_uint) << 3 ) - 1; j >= 0; j-- )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000218 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
219 break;
220
Paul Bakkereaebbd52014-04-25 15:04:14 +0200221 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
222 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
223
224 snprintf( str + idx, maxlen - idx, "value of '%s' (%d bits) is:\n",
225 text, (int) ( ( n * ( sizeof(t_uint) << 3 ) ) + j + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000226
227 str[maxlen] = '\0';
228 ssl->f_dbg( ssl->p_dbg, level, str );
229
Paul Bakker92478c32014-04-25 15:18:34 +0200230 idx = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000231 for( i = n + 1, j = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 {
Paul Bakker23986e52011-04-24 08:57:21 +0000233 if( zeros && X->p[i - 1] == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000234 continue;
Paul Bakker5121ce52009-01-03 21:22:43 +0000235
Paul Bakkera755ca12011-04-24 09:11:17 +0000236 for( k = sizeof( t_uint ) - 1; k >= 0; k-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000237 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200238 if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000239 continue;
240 else
241 zeros = 0;
242
243 if( j % 16 == 0 )
244 {
245 if( j > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200246 {
247 snprintf( str + idx, maxlen - idx, "\n" );
248 ssl->f_dbg( ssl->p_dbg, level, str );
249 idx = 0;
250 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000251
Paul Bakkereaebbd52014-04-25 15:04:14 +0200252 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
Paul Bakker92478c32014-04-25 15:18:34 +0200253 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000254 }
255
Paul Bakker92478c32014-04-25 15:18:34 +0200256 idx += snprintf( str + idx, maxlen - idx, " %02x", (unsigned int)
Paul Bakker66d5d072014-06-17 16:39:18 +0200257 ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000258
259 j++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000260 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000261
262 }
263
264 if( zeros == 1 )
265 {
Paul Bakkereaebbd52014-04-25 15:04:14 +0200266 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
267 {
Paul Bakker92478c32014-04-25 15:18:34 +0200268 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000269
Paul Bakkereaebbd52014-04-25 15:04:14 +0200270 }
Paul Bakker92478c32014-04-25 15:18:34 +0200271 idx += snprintf( str + idx, maxlen - idx, " 00" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000272 }
273
Paul Bakker92478c32014-04-25 15:18:34 +0200274 snprintf( str + idx, maxlen - idx, "\n" );
275 ssl->f_dbg( ssl->p_dbg, level, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000276}
Paul Bakkered27a042013-04-18 22:46:23 +0200277#endif /* POLARSSL_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000278
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200279#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200280static void debug_print_pk( const ssl_context *ssl, int level,
281 const char *file, int line,
282 const char *text, const pk_context *pk )
283{
284 size_t i;
285 pk_debug_item items[POLARSSL_PK_DEBUG_MAX_ITEMS];
286 char name[16];
287
288 memset( items, 0, sizeof( items ) );
289
290 if( pk_debug( pk, items ) != 0 )
291 {
292 debug_print_msg( ssl, level, file, line, "invalid PK context" );
293 return;
294 }
295
Paul Bakker0e4f9112014-04-17 12:39:05 +0200296 for( i = 0; i < POLARSSL_PK_DEBUG_MAX_ITEMS; i++ )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200297 {
298 if( items[i].type == POLARSSL_PK_DEBUG_NONE )
299 return;
300
301 snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
302 name[sizeof( name ) - 1] = '\0';
303
304 if( items[i].type == POLARSSL_PK_DEBUG_MPI )
305 debug_print_mpi( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200306 else
307#if defined(POLARSSL_ECP_C)
308 if( items[i].type == POLARSSL_PK_DEBUG_ECP )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200309 debug_print_ecp( ssl, level, file, line, name, items[i].value );
310 else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200311#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200312 debug_print_msg( ssl, level, file, line, "should not happen" );
313 }
314}
315
Paul Bakkerff60ee62010-03-16 21:09:09 +0000316void debug_print_crt( const ssl_context *ssl, int level,
317 const char *file, int line,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200318 const char *text, const x509_crt *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000319{
Paul Bakkerd98030e2009-05-02 15:13:40 +0000320 char str[1024], prefix[64];
Paul Bakkereaebbd52014-04-25 15:04:14 +0200321 int i = 0, maxlen = sizeof( prefix ) - 1, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000322
Paul Bakkerc73079a2014-04-25 16:34:30 +0200323 if( ssl->f_dbg == NULL || crt == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000324 return;
325
Paul Bakkereaebbd52014-04-25 15:04:14 +0200326 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
327 {
328 snprintf( prefix, maxlen, "%s(%04d): ", file, line );
329 prefix[maxlen] = '\0';
330 }
331 else
332 prefix[0] = '\0';
333
Paul Bakker5121ce52009-01-03 21:22:43 +0000334 maxlen = sizeof( str ) - 1;
335
Paul Bakker29087132010-03-21 21:03:34 +0000336 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000337 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000338 char buf[1024];
Paul Bakkerddf26b42013-09-18 13:46:23 +0200339 x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt );
Paul Bakker5121ce52009-01-03 21:22:43 +0000340
Paul Bakkereaebbd52014-04-25 15:04:14 +0200341 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
342 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
343
344 snprintf( str + idx, maxlen - idx, "%s #%d:\n%s",
345 text, ++i, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000346
347 str[maxlen] = '\0';
348 ssl->f_dbg( ssl->p_dbg, level, str );
349
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200350 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +0000351
352 crt = crt->next;
353 }
354}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200355#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000356
Paul Bakker9af723c2014-05-01 13:03:14 +0200357#endif /* POLARSSL_DEBUG_C */