blob: 4e674e1e2cf4b18238f138229ded5ad6091e0007 [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
49char *debug_fmt( const char *format, ... )
50{
51 va_list argp;
52 static char str[512];
53 int maxlen = sizeof( str ) - 1;
54
55 va_start( argp, format );
56 vsnprintf( str, maxlen, format, argp );
57 va_end( argp );
58
59 str[maxlen] = '\0';
60 return( str );
61}
62
Paul Bakkerff60ee62010-03-16 21:09:09 +000063void debug_print_msg( const ssl_context *ssl, int level,
64 const char *file, int line, const char *text )
Paul Bakker5121ce52009-01-03 21:22:43 +000065{
66 char str[512];
67 int maxlen = sizeof( str ) - 1;
68
69 if( ssl->f_dbg == NULL )
70 return;
71
72 snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text );
73 str[maxlen] = '\0';
74 ssl->f_dbg( ssl->p_dbg, level, str );
75}
76
Paul Bakkerff60ee62010-03-16 21:09:09 +000077void debug_print_ret( const ssl_context *ssl, int level,
78 const char *file, int line,
79 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +000080{
81 char str[512];
82 int maxlen = sizeof( str ) - 1;
83
84 if( ssl->f_dbg == NULL )
85 return;
86
87 snprintf( str, maxlen, "%s(%04d): %s() returned %d (0x%x)\n",
88 file, line, text, ret, ret );
89
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_buf( const ssl_context *ssl, int level,
95 const char *file, int line, const char *text,
Paul Bakker23986e52011-04-24 08:57:21 +000096 unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000097{
98 char str[512];
Paul Bakker23986e52011-04-24 08:57:21 +000099 size_t i, maxlen = sizeof( str ) - 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
Paul Bakker23986e52011-04-24 08:57:21 +0000101 if( ssl->f_dbg == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000102 return;
103
104 snprintf( str, maxlen, "%s(%04d): dumping '%s' (%d bytes)\n",
Paul Bakkerf4f69682011-04-24 16:08:12 +0000105 file, line, text, (unsigned int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
107 str[maxlen] = '\0';
108 ssl->f_dbg( ssl->p_dbg, level, str );
109
110 for( i = 0; i < len; i++ )
111 {
112 if( i >= 4096 )
113 break;
114
115 if( i % 16 == 0 )
116 {
117 if( i > 0 )
118 ssl->f_dbg( ssl->p_dbg, level, "\n" );
119
Paul Bakkerf4f69682011-04-24 16:08:12 +0000120 snprintf( str, maxlen, "%s(%04d): %04x: ", file, line,
121 (unsigned int) i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000122
123 str[maxlen] = '\0';
124 ssl->f_dbg( ssl->p_dbg, level, str );
125 }
126
127 snprintf( str, maxlen, " %02x", (unsigned int) buf[i] );
128
129 str[maxlen] = '\0';
130 ssl->f_dbg( ssl->p_dbg, level, str );
131 }
132
133 if( len > 0 )
134 ssl->f_dbg( ssl->p_dbg, level, "\n" );
135}
136
Paul Bakker41c83d32013-03-20 14:39:14 +0100137#if defined(POLARSSL_ECP_C)
138void debug_print_ecp( const ssl_context *ssl, int level,
139 const char *file, int line,
140 const char *text, const ecp_point *X )
141{
142 char str[512];
143 int maxlen = sizeof( str ) - 1;
144
145 snprintf( str, maxlen, "%s(X)", text );
146 str[maxlen] = '\0';
147 debug_print_mpi( ssl, level, file, line, str, &X->X );
148
149 snprintf( str, maxlen, "%s(Y)", text );
150 str[maxlen] = '\0';
151 debug_print_mpi( ssl, level, file, line, str, &X->Y );
Paul Bakker41c83d32013-03-20 14:39:14 +0100152}
153#endif /* POLARSSL_ECP_C */
154
Paul Bakkered27a042013-04-18 22:46:23 +0200155#if defined(POLARSSL_BIGNUM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +0000156void debug_print_mpi( const ssl_context *ssl, int level,
157 const char *file, int line,
158 const char *text, const mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000159{
160 char str[512];
Paul Bakker23986e52011-04-24 08:57:21 +0000161 int j, k, maxlen = sizeof( str ) - 1, zeros = 1;
162 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
164 if( ssl->f_dbg == NULL || X == NULL )
165 return;
166
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000167 for( n = X->n - 1; n > 0; n-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000168 if( X->p[n] != 0 )
169 break;
170
Paul Bakkera755ca12011-04-24 09:11:17 +0000171 for( j = ( sizeof(t_uint) << 3 ) - 1; j >= 0; j-- )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000172 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
173 break;
174
Paul Bakker5c2364c2012-10-01 14:41:15 +0000175 snprintf( str, maxlen, "%s(%04d): value of '%s' (%d bits) is:\n",
Paul Bakker4ed999c2010-03-16 21:16:16 +0000176 file, line, text,
Paul Bakker5c2364c2012-10-01 14:41:15 +0000177 (int) ( ( n * ( sizeof(t_uint) << 3 ) ) + j + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000178
179 str[maxlen] = '\0';
180 ssl->f_dbg( ssl->p_dbg, level, str );
181
Paul Bakker23986e52011-04-24 08:57:21 +0000182 for( i = n + 1, j = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000183 {
Paul Bakker23986e52011-04-24 08:57:21 +0000184 if( zeros && X->p[i - 1] == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000185 continue;
Paul Bakker5121ce52009-01-03 21:22:43 +0000186
Paul Bakkera755ca12011-04-24 09:11:17 +0000187 for( k = sizeof( t_uint ) - 1; k >= 0; k-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 {
Paul Bakker23986e52011-04-24 08:57:21 +0000189 if( zeros && ( ( X->p[i - 1] >> (k << 3) ) & 0xFF ) == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000190 continue;
191 else
192 zeros = 0;
193
194 if( j % 16 == 0 )
195 {
196 if( j > 0 )
197 ssl->f_dbg( ssl->p_dbg, level, "\n" );
198
199 snprintf( str, maxlen, "%s(%04d): ", file, line );
200
201 str[maxlen] = '\0';
202 ssl->f_dbg( ssl->p_dbg, level, str );
203 }
204
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 snprintf( str, maxlen, " %02x", (unsigned int)
Paul Bakker23986e52011-04-24 08:57:21 +0000206 ( X->p[i - 1] >> (k << 3) ) & 0xFF );
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
208 str[maxlen] = '\0';
209 ssl->f_dbg( ssl->p_dbg, level, str );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000210
211 j++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000212 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000213
214 }
215
216 if( zeros == 1 )
217 {
218 snprintf( str, maxlen, "%s(%04d): ", file, line );
219
220 str[maxlen] = '\0';
221 ssl->f_dbg( ssl->p_dbg, level, str );
222 ssl->f_dbg( ssl->p_dbg, level, " 00" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000223 }
224
225 ssl->f_dbg( ssl->p_dbg, level, "\n" );
226}
Paul Bakkered27a042013-04-18 22:46:23 +0200227#endif /* POLARSSL_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200229#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200230static void debug_print_pk( const ssl_context *ssl, int level,
231 const char *file, int line,
232 const char *text, const pk_context *pk )
233{
234 size_t i;
235 pk_debug_item items[POLARSSL_PK_DEBUG_MAX_ITEMS];
236 char name[16];
237
238 memset( items, 0, sizeof( items ) );
239
240 if( pk_debug( pk, items ) != 0 )
241 {
242 debug_print_msg( ssl, level, file, line, "invalid PK context" );
243 return;
244 }
245
246 for( i = 0; i < sizeof( items ); i++ )
247 {
248 if( items[i].type == POLARSSL_PK_DEBUG_NONE )
249 return;
250
251 snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
252 name[sizeof( name ) - 1] = '\0';
253
254 if( items[i].type == POLARSSL_PK_DEBUG_MPI )
255 debug_print_mpi( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200256 else
257#if defined(POLARSSL_ECP_C)
258 if( items[i].type == POLARSSL_PK_DEBUG_ECP )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200259 debug_print_ecp( ssl, level, file, line, name, items[i].value );
260 else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200261#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200262 debug_print_msg( ssl, level, file, line, "should not happen" );
263 }
264}
265
Paul Bakkerff60ee62010-03-16 21:09:09 +0000266void debug_print_crt( const ssl_context *ssl, int level,
267 const char *file, int line,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200268 const char *text, const x509_crt *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000269{
Paul Bakkerd98030e2009-05-02 15:13:40 +0000270 char str[1024], prefix[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000271 int i = 0, maxlen = sizeof( prefix ) - 1;
272
273 if( ssl->f_dbg == NULL || crt == NULL )
274 return;
275
276 snprintf( prefix, maxlen, "%s(%04d): ", file, line );
277 prefix[maxlen] = '\0';
278 maxlen = sizeof( str ) - 1;
279
Paul Bakker29087132010-03-21 21:03:34 +0000280 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000281 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000282 char buf[1024];
Paul Bakkerddf26b42013-09-18 13:46:23 +0200283 x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt );
Paul Bakker5121ce52009-01-03 21:22:43 +0000284
285 snprintf( str, maxlen, "%s(%04d): %s #%d:\n%s",
Paul Bakkerd98030e2009-05-02 15:13:40 +0000286 file, line, text, ++i, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000287
288 str[maxlen] = '\0';
289 ssl->f_dbg( ssl->p_dbg, level, str );
290
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200291 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +0000292
293 crt = crt->next;
294 }
295}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200296#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000297
298#endif