blob: fb0ec719c82db088a059e5797cde2785f48f7721 [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
39#if defined(_MSC_VER) && !defined snprintf
Paul Bakker5121ce52009-01-03 21:22:43 +000040#define snprintf _snprintf
41#endif
42
Paul Bakkerfa6a6202013-10-28 18:48:30 +010043#if defined(_MSC_VER) && !defined vsnprintf
Paul Bakker5121ce52009-01-03 21:22:43 +000044#define vsnprintf _vsnprintf
45#endif
46
47char *debug_fmt( const char *format, ... )
48{
49 va_list argp;
50 static char str[512];
51 int maxlen = sizeof( str ) - 1;
52
53 va_start( argp, format );
54 vsnprintf( str, maxlen, format, argp );
55 va_end( argp );
56
57 str[maxlen] = '\0';
58 return( str );
59}
60
Paul Bakkerff60ee62010-03-16 21:09:09 +000061void debug_print_msg( const ssl_context *ssl, int level,
62 const char *file, int line, const char *text )
Paul Bakker5121ce52009-01-03 21:22:43 +000063{
64 char str[512];
65 int maxlen = sizeof( str ) - 1;
66
67 if( ssl->f_dbg == NULL )
68 return;
69
70 snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text );
71 str[maxlen] = '\0';
72 ssl->f_dbg( ssl->p_dbg, level, str );
73}
74
Paul Bakkerff60ee62010-03-16 21:09:09 +000075void debug_print_ret( const ssl_context *ssl, int level,
76 const char *file, int line,
77 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +000078{
79 char str[512];
80 int maxlen = sizeof( str ) - 1;
81
82 if( ssl->f_dbg == NULL )
83 return;
84
85 snprintf( str, maxlen, "%s(%04d): %s() returned %d (0x%x)\n",
86 file, line, text, ret, ret );
87
88 str[maxlen] = '\0';
89 ssl->f_dbg( ssl->p_dbg, level, str );
90}
91
Paul Bakkerff60ee62010-03-16 21:09:09 +000092void debug_print_buf( const ssl_context *ssl, int level,
93 const char *file, int line, const char *text,
Paul Bakker23986e52011-04-24 08:57:21 +000094 unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000095{
96 char str[512];
Paul Bakker23986e52011-04-24 08:57:21 +000097 size_t i, maxlen = sizeof( str ) - 1;
Paul Bakker5121ce52009-01-03 21:22:43 +000098
Paul Bakker23986e52011-04-24 08:57:21 +000099 if( ssl->f_dbg == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000100 return;
101
102 snprintf( str, maxlen, "%s(%04d): dumping '%s' (%d bytes)\n",
Paul Bakkerf4f69682011-04-24 16:08:12 +0000103 file, line, text, (unsigned int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
105 str[maxlen] = '\0';
106 ssl->f_dbg( ssl->p_dbg, level, str );
107
108 for( i = 0; i < len; i++ )
109 {
110 if( i >= 4096 )
111 break;
112
113 if( i % 16 == 0 )
114 {
115 if( i > 0 )
116 ssl->f_dbg( ssl->p_dbg, level, "\n" );
117
Paul Bakkerf4f69682011-04-24 16:08:12 +0000118 snprintf( str, maxlen, "%s(%04d): %04x: ", file, line,
119 (unsigned int) i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
121 str[maxlen] = '\0';
122 ssl->f_dbg( ssl->p_dbg, level, str );
123 }
124
125 snprintf( str, maxlen, " %02x", (unsigned int) buf[i] );
126
127 str[maxlen] = '\0';
128 ssl->f_dbg( ssl->p_dbg, level, str );
129 }
130
131 if( len > 0 )
132 ssl->f_dbg( ssl->p_dbg, level, "\n" );
133}
134
Paul Bakker41c83d32013-03-20 14:39:14 +0100135#if defined(POLARSSL_ECP_C)
136void debug_print_ecp( const ssl_context *ssl, int level,
137 const char *file, int line,
138 const char *text, const ecp_point *X )
139{
140 char str[512];
141 int maxlen = sizeof( str ) - 1;
142
143 snprintf( str, maxlen, "%s(X)", text );
144 str[maxlen] = '\0';
145 debug_print_mpi( ssl, level, file, line, str, &X->X );
146
147 snprintf( str, maxlen, "%s(Y)", text );
148 str[maxlen] = '\0';
149 debug_print_mpi( ssl, level, file, line, str, &X->Y );
150
151 snprintf( str, maxlen, "%s(Z)", text );
152 str[maxlen] = '\0';
153 debug_print_mpi( ssl, level, file, line, str, &X->Z );
154}
155#endif /* POLARSSL_ECP_C */
156
Paul Bakkered27a042013-04-18 22:46:23 +0200157#if defined(POLARSSL_BIGNUM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +0000158void debug_print_mpi( const ssl_context *ssl, int level,
159 const char *file, int line,
160 const char *text, const mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000161{
162 char str[512];
Paul Bakker23986e52011-04-24 08:57:21 +0000163 int j, k, maxlen = sizeof( str ) - 1, zeros = 1;
164 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000165
166 if( ssl->f_dbg == NULL || X == NULL )
167 return;
168
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000169 for( n = X->n - 1; n > 0; n-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000170 if( X->p[n] != 0 )
171 break;
172
Paul Bakkera755ca12011-04-24 09:11:17 +0000173 for( j = ( sizeof(t_uint) << 3 ) - 1; j >= 0; j-- )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000174 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
175 break;
176
Paul Bakker5c2364c2012-10-01 14:41:15 +0000177 snprintf( str, maxlen, "%s(%04d): value of '%s' (%d bits) is:\n",
Paul Bakker4ed999c2010-03-16 21:16:16 +0000178 file, line, text,
Paul Bakker5c2364c2012-10-01 14:41:15 +0000179 (int) ( ( n * ( sizeof(t_uint) << 3 ) ) + j + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000180
181 str[maxlen] = '\0';
182 ssl->f_dbg( ssl->p_dbg, level, str );
183
Paul Bakker23986e52011-04-24 08:57:21 +0000184 for( i = n + 1, j = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000185 {
Paul Bakker23986e52011-04-24 08:57:21 +0000186 if( zeros && X->p[i - 1] == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000187 continue;
Paul Bakker5121ce52009-01-03 21:22:43 +0000188
Paul Bakkera755ca12011-04-24 09:11:17 +0000189 for( k = sizeof( t_uint ) - 1; k >= 0; k-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 {
Paul Bakker23986e52011-04-24 08:57:21 +0000191 if( zeros && ( ( X->p[i - 1] >> (k << 3) ) & 0xFF ) == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000192 continue;
193 else
194 zeros = 0;
195
196 if( j % 16 == 0 )
197 {
198 if( j > 0 )
199 ssl->f_dbg( ssl->p_dbg, level, "\n" );
200
201 snprintf( str, maxlen, "%s(%04d): ", file, line );
202
203 str[maxlen] = '\0';
204 ssl->f_dbg( ssl->p_dbg, level, str );
205 }
206
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 snprintf( str, maxlen, " %02x", (unsigned int)
Paul Bakker23986e52011-04-24 08:57:21 +0000208 ( X->p[i - 1] >> (k << 3) ) & 0xFF );
Paul Bakker5121ce52009-01-03 21:22:43 +0000209
210 str[maxlen] = '\0';
211 ssl->f_dbg( ssl->p_dbg, level, str );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000212
213 j++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000215
216 }
217
218 if( zeros == 1 )
219 {
220 snprintf( str, maxlen, "%s(%04d): ", file, line );
221
222 str[maxlen] = '\0';
223 ssl->f_dbg( ssl->p_dbg, level, str );
224 ssl->f_dbg( ssl->p_dbg, level, " 00" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000225 }
226
227 ssl->f_dbg( ssl->p_dbg, level, "\n" );
228}
Paul Bakkered27a042013-04-18 22:46:23 +0200229#endif /* POLARSSL_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000230
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200231#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200232static void debug_print_pk( const ssl_context *ssl, int level,
233 const char *file, int line,
234 const char *text, const pk_context *pk )
235{
236 size_t i;
237 pk_debug_item items[POLARSSL_PK_DEBUG_MAX_ITEMS];
238 char name[16];
239
240 memset( items, 0, sizeof( items ) );
241
242 if( pk_debug( pk, items ) != 0 )
243 {
244 debug_print_msg( ssl, level, file, line, "invalid PK context" );
245 return;
246 }
247
248 for( i = 0; i < sizeof( items ); i++ )
249 {
250 if( items[i].type == POLARSSL_PK_DEBUG_NONE )
251 return;
252
253 snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
254 name[sizeof( name ) - 1] = '\0';
255
256 if( items[i].type == POLARSSL_PK_DEBUG_MPI )
257 debug_print_mpi( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200258 else
259#if defined(POLARSSL_ECP_C)
260 if( items[i].type == POLARSSL_PK_DEBUG_ECP )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200261 debug_print_ecp( ssl, level, file, line, name, items[i].value );
262 else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200263#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200264 debug_print_msg( ssl, level, file, line, "should not happen" );
265 }
266}
267
Paul Bakkerff60ee62010-03-16 21:09:09 +0000268void debug_print_crt( const ssl_context *ssl, int level,
269 const char *file, int line,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200270 const char *text, const x509_crt *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000271{
Paul Bakkerd98030e2009-05-02 15:13:40 +0000272 char str[1024], prefix[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000273 int i = 0, maxlen = sizeof( prefix ) - 1;
274
275 if( ssl->f_dbg == NULL || crt == NULL )
276 return;
277
278 snprintf( prefix, maxlen, "%s(%04d): ", file, line );
279 prefix[maxlen] = '\0';
280 maxlen = sizeof( str ) - 1;
281
Paul Bakker29087132010-03-21 21:03:34 +0000282 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000283 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000284 char buf[1024];
Paul Bakkerddf26b42013-09-18 13:46:23 +0200285 x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt );
Paul Bakker5121ce52009-01-03 21:22:43 +0000286
287 snprintf( str, maxlen, "%s(%04d): %s #%d:\n%s",
Paul Bakkerd98030e2009-05-02 15:13:40 +0000288 file, line, text, ++i, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000289
290 str[maxlen] = '\0';
291 ssl->f_dbg( ssl->p_dbg, level, str );
292
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200293 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +0000294
295 crt = crt->next;
296 }
297}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200298#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000299
300#endif