blob: 60e39ed37a005e411e26afdb88ab917f2c2eeb61 [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;
Paul Bakkerc73079a2014-04-25 16:34:30 +020050static int debug_threshold = 0;
Paul Bakkereaebbd52014-04-25 15:04:14 +020051
52void debug_set_log_mode( int log_mode )
53{
54 debug_log_mode = log_mode;
55}
56
Paul Bakkerc73079a2014-04-25 16:34:30 +020057void debug_set_threshold( int threshold )
58{
59 debug_threshold = threshold;
60}
61
Paul Bakker5121ce52009-01-03 21:22:43 +000062char *debug_fmt( const char *format, ... )
63{
64 va_list argp;
65 static char str[512];
66 int maxlen = sizeof( str ) - 1;
67
68 va_start( argp, format );
69 vsnprintf( str, maxlen, format, argp );
70 va_end( argp );
71
72 str[maxlen] = '\0';
73 return( str );
74}
75
Paul Bakkerff60ee62010-03-16 21:09:09 +000076void debug_print_msg( const ssl_context *ssl, int level,
77 const char *file, int line, const char *text )
Paul Bakker5121ce52009-01-03 21:22:43 +000078{
79 char str[512];
80 int maxlen = sizeof( str ) - 1;
81
Paul Bakkerc73079a2014-04-25 16:34:30 +020082 if( ssl->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +000083 return;
84
Paul Bakkereaebbd52014-04-25 15:04:14 +020085 if( debug_log_mode == POLARSSL_DEBUG_LOG_RAW )
86 {
87 ssl->f_dbg( ssl->p_dbg, level, str );
88 return;
89 }
90
Paul Bakker5121ce52009-01-03 21:22:43 +000091 snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text );
92 str[maxlen] = '\0';
93 ssl->f_dbg( ssl->p_dbg, level, str );
94}
95
Paul Bakkerff60ee62010-03-16 21:09:09 +000096void debug_print_ret( const ssl_context *ssl, int level,
97 const char *file, int line,
98 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +000099{
100 char str[512];
101 int maxlen = sizeof( str ) - 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +0200102 size_t idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
Paul Bakkerc73079a2014-04-25 16:34:30 +0200104 if( ssl->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 return;
106
Paul Bakkereaebbd52014-04-25 15:04:14 +0200107 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
108 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
109
110 snprintf( str + idx, maxlen - idx, "%s() returned %d (-0x%04x)\n",
111 text, ret, -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
113 str[maxlen] = '\0';
114 ssl->f_dbg( ssl->p_dbg, level, str );
115}
116
Paul Bakkerff60ee62010-03-16 21:09:09 +0000117void debug_print_buf( const ssl_context *ssl, int level,
118 const char *file, int line, const char *text,
Paul Bakker23986e52011-04-24 08:57:21 +0000119 unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000120{
121 char str[512];
Paul Bakkereaebbd52014-04-25 15:04:14 +0200122 size_t i, maxlen = sizeof( str ) - 1, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000123
Paul Bakkerc73079a2014-04-25 16:34:30 +0200124 if( ssl->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 return;
126
Paul Bakkereaebbd52014-04-25 15:04:14 +0200127 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
128 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
129
130 snprintf( str + idx, maxlen - idx, "dumping '%s' (%d bytes)\n",
131 text, (unsigned int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000132
133 str[maxlen] = '\0';
134 ssl->f_dbg( ssl->p_dbg, level, str );
135
Paul Bakker92478c32014-04-25 15:18:34 +0200136 idx = 0;
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 {
146 snprintf( str + idx, maxlen - idx, "\n" );
147 ssl->f_dbg( ssl->p_dbg, level, str );
148 idx = 0;
149 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
Paul Bakkereaebbd52014-04-25 15:04:14 +0200151 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
152 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
153
Paul Bakker92478c32014-04-25 15:18:34 +0200154 idx += snprintf( str + idx, maxlen - idx, "%04x: ",
155 (unsigned int) i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 }
158
Paul Bakker92478c32014-04-25 15:18:34 +0200159 idx += snprintf( str + idx, maxlen - idx, " %02x",
160 (unsigned int) buf[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 }
162
163 if( len > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200164 {
165 snprintf( str + idx, maxlen - idx, "\n" );
166 ssl->f_dbg( ssl->p_dbg, level, str );
167 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000168}
169
Paul Bakker41c83d32013-03-20 14:39:14 +0100170#if defined(POLARSSL_ECP_C)
171void debug_print_ecp( const ssl_context *ssl, int level,
172 const char *file, int line,
173 const char *text, const ecp_point *X )
174{
175 char str[512];
176 int maxlen = sizeof( str ) - 1;
177
Paul Bakkerc73079a2014-04-25 16:34:30 +0200178 if( ssl->f_dbg == NULL || level > debug_threshold )
179 return;
180
Paul Bakker41c83d32013-03-20 14:39:14 +0100181 snprintf( str, maxlen, "%s(X)", text );
182 str[maxlen] = '\0';
183 debug_print_mpi( ssl, level, file, line, str, &X->X );
184
185 snprintf( str, maxlen, "%s(Y)", text );
186 str[maxlen] = '\0';
187 debug_print_mpi( ssl, level, file, line, str, &X->Y );
Paul Bakker41c83d32013-03-20 14:39:14 +0100188}
189#endif /* POLARSSL_ECP_C */
190
Paul Bakkered27a042013-04-18 22:46:23 +0200191#if defined(POLARSSL_BIGNUM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +0000192void debug_print_mpi( const ssl_context *ssl, int level,
193 const char *file, int line,
194 const char *text, const mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000195{
196 char str[512];
Paul Bakker23986e52011-04-24 08:57:21 +0000197 int j, k, maxlen = sizeof( str ) - 1, zeros = 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +0200198 size_t i, n, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000199
Paul Bakkerc73079a2014-04-25 16:34:30 +0200200 if( ssl->f_dbg == NULL || X == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000201 return;
202
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000203 for( n = X->n - 1; n > 0; n-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000204 if( X->p[n] != 0 )
205 break;
206
Paul Bakkera755ca12011-04-24 09:11:17 +0000207 for( j = ( sizeof(t_uint) << 3 ) - 1; j >= 0; j-- )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000208 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
209 break;
210
Paul Bakkereaebbd52014-04-25 15:04:14 +0200211 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
212 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
213
214 snprintf( str + idx, maxlen - idx, "value of '%s' (%d bits) is:\n",
215 text, (int) ( ( n * ( sizeof(t_uint) << 3 ) ) + j + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000216
217 str[maxlen] = '\0';
218 ssl->f_dbg( ssl->p_dbg, level, str );
219
Paul Bakker92478c32014-04-25 15:18:34 +0200220 idx = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000221 for( i = n + 1, j = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000222 {
Paul Bakker23986e52011-04-24 08:57:21 +0000223 if( zeros && X->p[i - 1] == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000224 continue;
Paul Bakker5121ce52009-01-03 21:22:43 +0000225
Paul Bakkera755ca12011-04-24 09:11:17 +0000226 for( k = sizeof( t_uint ) - 1; k >= 0; k-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000227 {
Paul Bakker23986e52011-04-24 08:57:21 +0000228 if( zeros && ( ( X->p[i - 1] >> (k << 3) ) & 0xFF ) == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000229 continue;
230 else
231 zeros = 0;
232
233 if( j % 16 == 0 )
234 {
235 if( j > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200236 {
237 snprintf( str + idx, maxlen - idx, "\n" );
238 ssl->f_dbg( ssl->p_dbg, level, str );
239 idx = 0;
240 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000241
Paul Bakkereaebbd52014-04-25 15:04:14 +0200242 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
Paul Bakker92478c32014-04-25 15:18:34 +0200243 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000244 }
245
Paul Bakker92478c32014-04-25 15:18:34 +0200246 idx += snprintf( str + idx, maxlen - idx, " %02x", (unsigned int)
247 ( X->p[i - 1] >> (k << 3) ) & 0xFF );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000248
249 j++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000250 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000251
252 }
253
254 if( zeros == 1 )
255 {
Paul Bakkereaebbd52014-04-25 15:04:14 +0200256 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
257 {
Paul Bakker92478c32014-04-25 15:18:34 +0200258 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000259
Paul Bakkereaebbd52014-04-25 15:04:14 +0200260 }
Paul Bakker92478c32014-04-25 15:18:34 +0200261 idx += snprintf( str + idx, maxlen - idx, " 00" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000262 }
263
Paul Bakker92478c32014-04-25 15:18:34 +0200264 snprintf( str + idx, maxlen - idx, "\n" );
265 ssl->f_dbg( ssl->p_dbg, level, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000266}
Paul Bakkered27a042013-04-18 22:46:23 +0200267#endif /* POLARSSL_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000268
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200269#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200270static void debug_print_pk( const ssl_context *ssl, int level,
271 const char *file, int line,
272 const char *text, const pk_context *pk )
273{
274 size_t i;
275 pk_debug_item items[POLARSSL_PK_DEBUG_MAX_ITEMS];
276 char name[16];
277
278 memset( items, 0, sizeof( items ) );
279
280 if( pk_debug( pk, items ) != 0 )
281 {
282 debug_print_msg( ssl, level, file, line, "invalid PK context" );
283 return;
284 }
285
Paul Bakker0e4f9112014-04-17 12:39:05 +0200286 for( i = 0; i < POLARSSL_PK_DEBUG_MAX_ITEMS; i++ )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200287 {
288 if( items[i].type == POLARSSL_PK_DEBUG_NONE )
289 return;
290
291 snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
292 name[sizeof( name ) - 1] = '\0';
293
294 if( items[i].type == POLARSSL_PK_DEBUG_MPI )
295 debug_print_mpi( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200296 else
297#if defined(POLARSSL_ECP_C)
298 if( items[i].type == POLARSSL_PK_DEBUG_ECP )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200299 debug_print_ecp( ssl, level, file, line, name, items[i].value );
300 else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200301#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200302 debug_print_msg( ssl, level, file, line, "should not happen" );
303 }
304}
305
Paul Bakkerff60ee62010-03-16 21:09:09 +0000306void debug_print_crt( const ssl_context *ssl, int level,
307 const char *file, int line,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200308 const char *text, const x509_crt *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000309{
Paul Bakkerd98030e2009-05-02 15:13:40 +0000310 char str[1024], prefix[64];
Paul Bakkereaebbd52014-04-25 15:04:14 +0200311 int i = 0, maxlen = sizeof( prefix ) - 1, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000312
Paul Bakkerc73079a2014-04-25 16:34:30 +0200313 if( ssl->f_dbg == NULL || crt == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 return;
315
Paul Bakkereaebbd52014-04-25 15:04:14 +0200316 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
317 {
318 snprintf( prefix, maxlen, "%s(%04d): ", file, line );
319 prefix[maxlen] = '\0';
320 }
321 else
322 prefix[0] = '\0';
323
Paul Bakker5121ce52009-01-03 21:22:43 +0000324 maxlen = sizeof( str ) - 1;
325
Paul Bakker29087132010-03-21 21:03:34 +0000326 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000327 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000328 char buf[1024];
Paul Bakkerddf26b42013-09-18 13:46:23 +0200329 x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt );
Paul Bakker5121ce52009-01-03 21:22:43 +0000330
Paul Bakkereaebbd52014-04-25 15:04:14 +0200331 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
332 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
333
334 snprintf( str + idx, maxlen - idx, "%s #%d:\n%s",
335 text, ++i, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000336
337 str[maxlen] = '\0';
338 ssl->f_dbg( ssl->p_dbg, level, str );
339
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200340 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +0000341
342 crt = crt->next;
343 }
344}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200345#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000346
347#endif