blob: 40a4399f9b6636f5964360f816fe29aa12429375 [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;
50
51void debug_set_log_mode( int log_mode )
52{
53 debug_log_mode = log_mode;
54}
55
Paul Bakker5121ce52009-01-03 21:22:43 +000056char *debug_fmt( const char *format, ... )
57{
58 va_list argp;
59 static char str[512];
60 int maxlen = sizeof( str ) - 1;
61
62 va_start( argp, format );
63 vsnprintf( str, maxlen, format, argp );
64 va_end( argp );
65
66 str[maxlen] = '\0';
67 return( str );
68}
69
Paul Bakkerff60ee62010-03-16 21:09:09 +000070void debug_print_msg( const ssl_context *ssl, int level,
71 const char *file, int line, const char *text )
Paul Bakker5121ce52009-01-03 21:22:43 +000072{
73 char str[512];
74 int maxlen = sizeof( str ) - 1;
75
76 if( ssl->f_dbg == NULL )
77 return;
78
Paul Bakkereaebbd52014-04-25 15:04:14 +020079 if( debug_log_mode == POLARSSL_DEBUG_LOG_RAW )
80 {
81 ssl->f_dbg( ssl->p_dbg, level, str );
82 return;
83 }
84
Paul Bakker5121ce52009-01-03 21:22:43 +000085 snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text );
86 str[maxlen] = '\0';
87 ssl->f_dbg( ssl->p_dbg, level, str );
88}
89
Paul Bakkerff60ee62010-03-16 21:09:09 +000090void debug_print_ret( const ssl_context *ssl, int level,
91 const char *file, int line,
92 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +000093{
94 char str[512];
95 int maxlen = sizeof( str ) - 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +020096 size_t idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +000097
98 if( ssl->f_dbg == NULL )
99 return;
100
Paul Bakkereaebbd52014-04-25 15:04:14 +0200101 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
102 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
103
104 snprintf( str + idx, maxlen - idx, "%s() returned %d (-0x%04x)\n",
105 text, ret, -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
107 str[maxlen] = '\0';
108 ssl->f_dbg( ssl->p_dbg, level, str );
109}
110
Paul Bakkerff60ee62010-03-16 21:09:09 +0000111void debug_print_buf( const ssl_context *ssl, int level,
112 const char *file, int line, const char *text,
Paul Bakker23986e52011-04-24 08:57:21 +0000113 unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000114{
115 char str[512];
Paul Bakkereaebbd52014-04-25 15:04:14 +0200116 size_t i, maxlen = sizeof( str ) - 1, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000117
Paul Bakker23986e52011-04-24 08:57:21 +0000118 if( ssl->f_dbg == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 return;
120
Paul Bakkereaebbd52014-04-25 15:04:14 +0200121 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
122 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
123
124 snprintf( str + idx, maxlen - idx, "dumping '%s' (%d bytes)\n",
125 text, (unsigned int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
127 str[maxlen] = '\0';
128 ssl->f_dbg( ssl->p_dbg, level, str );
129
Paul Bakker92478c32014-04-25 15:18:34 +0200130 idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 for( i = 0; i < len; i++ )
132 {
133 if( i >= 4096 )
134 break;
135
136 if( i % 16 == 0 )
137 {
138 if( i > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200139 {
140 snprintf( str + idx, maxlen - idx, "\n" );
141 ssl->f_dbg( ssl->p_dbg, level, str );
142 idx = 0;
143 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000144
Paul Bakkereaebbd52014-04-25 15:04:14 +0200145 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
146 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
147
Paul Bakker92478c32014-04-25 15:18:34 +0200148 idx += snprintf( str + idx, maxlen - idx, "%04x: ",
149 (unsigned int) i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
Paul Bakker5121ce52009-01-03 21:22:43 +0000151 }
152
Paul Bakker92478c32014-04-25 15:18:34 +0200153 idx += snprintf( str + idx, maxlen - idx, " %02x",
154 (unsigned int) buf[i] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 }
156
157 if( len > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200158 {
159 snprintf( str + idx, maxlen - idx, "\n" );
160 ssl->f_dbg( ssl->p_dbg, level, str );
161 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000162}
163
Paul Bakker41c83d32013-03-20 14:39:14 +0100164#if defined(POLARSSL_ECP_C)
165void debug_print_ecp( const ssl_context *ssl, int level,
166 const char *file, int line,
167 const char *text, const ecp_point *X )
168{
169 char str[512];
170 int maxlen = sizeof( str ) - 1;
171
172 snprintf( str, maxlen, "%s(X)", text );
173 str[maxlen] = '\0';
174 debug_print_mpi( ssl, level, file, line, str, &X->X );
175
176 snprintf( str, maxlen, "%s(Y)", text );
177 str[maxlen] = '\0';
178 debug_print_mpi( ssl, level, file, line, str, &X->Y );
Paul Bakker41c83d32013-03-20 14:39:14 +0100179}
180#endif /* POLARSSL_ECP_C */
181
Paul Bakkered27a042013-04-18 22:46:23 +0200182#if defined(POLARSSL_BIGNUM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +0000183void debug_print_mpi( const ssl_context *ssl, int level,
184 const char *file, int line,
185 const char *text, const mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000186{
187 char str[512];
Paul Bakker23986e52011-04-24 08:57:21 +0000188 int j, k, maxlen = sizeof( str ) - 1, zeros = 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +0200189 size_t i, n, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000190
191 if( ssl->f_dbg == NULL || X == NULL )
192 return;
193
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000194 for( n = X->n - 1; n > 0; n-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000195 if( X->p[n] != 0 )
196 break;
197
Paul Bakkera755ca12011-04-24 09:11:17 +0000198 for( j = ( sizeof(t_uint) << 3 ) - 1; j >= 0; j-- )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000199 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
200 break;
201
Paul Bakkereaebbd52014-04-25 15:04:14 +0200202 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
203 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
204
205 snprintf( str + idx, maxlen - idx, "value of '%s' (%d bits) is:\n",
206 text, (int) ( ( n * ( sizeof(t_uint) << 3 ) ) + j + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
208 str[maxlen] = '\0';
209 ssl->f_dbg( ssl->p_dbg, level, str );
210
Paul Bakker92478c32014-04-25 15:18:34 +0200211 idx = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000212 for( i = n + 1, j = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 {
Paul Bakker23986e52011-04-24 08:57:21 +0000214 if( zeros && X->p[i - 1] == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000215 continue;
Paul Bakker5121ce52009-01-03 21:22:43 +0000216
Paul Bakkera755ca12011-04-24 09:11:17 +0000217 for( k = sizeof( t_uint ) - 1; k >= 0; k-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 {
Paul Bakker23986e52011-04-24 08:57:21 +0000219 if( zeros && ( ( X->p[i - 1] >> (k << 3) ) & 0xFF ) == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000220 continue;
221 else
222 zeros = 0;
223
224 if( j % 16 == 0 )
225 {
226 if( j > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200227 {
228 snprintf( str + idx, maxlen - idx, "\n" );
229 ssl->f_dbg( ssl->p_dbg, level, str );
230 idx = 0;
231 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000232
Paul Bakkereaebbd52014-04-25 15:04:14 +0200233 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
Paul Bakker92478c32014-04-25 15:18:34 +0200234 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000235 }
236
Paul Bakker92478c32014-04-25 15:18:34 +0200237 idx += snprintf( str + idx, maxlen - idx, " %02x", (unsigned int)
238 ( X->p[i - 1] >> (k << 3) ) & 0xFF );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000239
240 j++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000241 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000242
243 }
244
245 if( zeros == 1 )
246 {
Paul Bakkereaebbd52014-04-25 15:04:14 +0200247 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
248 {
Paul Bakker92478c32014-04-25 15:18:34 +0200249 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000250
Paul Bakkereaebbd52014-04-25 15:04:14 +0200251 }
Paul Bakker92478c32014-04-25 15:18:34 +0200252 idx += snprintf( str + idx, maxlen - idx, " 00" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000253 }
254
Paul Bakker92478c32014-04-25 15:18:34 +0200255 snprintf( str + idx, maxlen - idx, "\n" );
256 ssl->f_dbg( ssl->p_dbg, level, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000257}
Paul Bakkered27a042013-04-18 22:46:23 +0200258#endif /* POLARSSL_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000259
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200260#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200261static void debug_print_pk( const ssl_context *ssl, int level,
262 const char *file, int line,
263 const char *text, const pk_context *pk )
264{
265 size_t i;
266 pk_debug_item items[POLARSSL_PK_DEBUG_MAX_ITEMS];
267 char name[16];
268
269 memset( items, 0, sizeof( items ) );
270
271 if( pk_debug( pk, items ) != 0 )
272 {
273 debug_print_msg( ssl, level, file, line, "invalid PK context" );
274 return;
275 }
276
Paul Bakker0e4f9112014-04-17 12:39:05 +0200277 for( i = 0; i < POLARSSL_PK_DEBUG_MAX_ITEMS; i++ )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200278 {
279 if( items[i].type == POLARSSL_PK_DEBUG_NONE )
280 return;
281
282 snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
283 name[sizeof( name ) - 1] = '\0';
284
285 if( items[i].type == POLARSSL_PK_DEBUG_MPI )
286 debug_print_mpi( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200287 else
288#if defined(POLARSSL_ECP_C)
289 if( items[i].type == POLARSSL_PK_DEBUG_ECP )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200290 debug_print_ecp( ssl, level, file, line, name, items[i].value );
291 else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200292#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200293 debug_print_msg( ssl, level, file, line, "should not happen" );
294 }
295}
296
Paul Bakkerff60ee62010-03-16 21:09:09 +0000297void debug_print_crt( const ssl_context *ssl, int level,
298 const char *file, int line,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200299 const char *text, const x509_crt *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000300{
Paul Bakkerd98030e2009-05-02 15:13:40 +0000301 char str[1024], prefix[64];
Paul Bakkereaebbd52014-04-25 15:04:14 +0200302 int i = 0, maxlen = sizeof( prefix ) - 1, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000303
304 if( ssl->f_dbg == NULL || crt == NULL )
305 return;
306
Paul Bakkereaebbd52014-04-25 15:04:14 +0200307 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
308 {
309 snprintf( prefix, maxlen, "%s(%04d): ", file, line );
310 prefix[maxlen] = '\0';
311 }
312 else
313 prefix[0] = '\0';
314
Paul Bakker5121ce52009-01-03 21:22:43 +0000315 maxlen = sizeof( str ) - 1;
316
Paul Bakker29087132010-03-21 21:03:34 +0000317 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000318 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000319 char buf[1024];
Paul Bakkerddf26b42013-09-18 13:46:23 +0200320 x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt );
Paul Bakker5121ce52009-01-03 21:22:43 +0000321
Paul Bakkereaebbd52014-04-25 15:04:14 +0200322 if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
323 idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
324
325 snprintf( str + idx, maxlen - idx, "%s #%d:\n%s",
326 text, ++i, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000327
328 str[maxlen] = '\0';
329 ssl->f_dbg( ssl->p_dbg, level, str );
330
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200331 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +0000332
333 crt = crt->next;
334 }
335}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200336#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000337
338#endif