blob: 371cbf95cf20216e85f9d8b1f534ee669215ff8c [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 );
152
153 snprintf( str, maxlen, "%s(Z)", text );
154 str[maxlen] = '\0';
155 debug_print_mpi( ssl, level, file, line, str, &X->Z );
156}
157#endif /* POLARSSL_ECP_C */
158
Paul Bakkered27a042013-04-18 22:46:23 +0200159#if defined(POLARSSL_BIGNUM_C)
Paul Bakkerff60ee62010-03-16 21:09:09 +0000160void debug_print_mpi( const ssl_context *ssl, int level,
161 const char *file, int line,
162 const char *text, const mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000163{
164 char str[512];
Paul Bakker23986e52011-04-24 08:57:21 +0000165 int j, k, maxlen = sizeof( str ) - 1, zeros = 1;
166 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
168 if( ssl->f_dbg == NULL || X == NULL )
169 return;
170
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000171 for( n = X->n - 1; n > 0; n-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000172 if( X->p[n] != 0 )
173 break;
174
Paul Bakkera755ca12011-04-24 09:11:17 +0000175 for( j = ( sizeof(t_uint) << 3 ) - 1; j >= 0; j-- )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000176 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
177 break;
178
Paul Bakker5c2364c2012-10-01 14:41:15 +0000179 snprintf( str, maxlen, "%s(%04d): value of '%s' (%d bits) is:\n",
Paul Bakker4ed999c2010-03-16 21:16:16 +0000180 file, line, text,
Paul Bakker5c2364c2012-10-01 14:41:15 +0000181 (int) ( ( n * ( sizeof(t_uint) << 3 ) ) + j + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000182
183 str[maxlen] = '\0';
184 ssl->f_dbg( ssl->p_dbg, level, str );
185
Paul Bakker23986e52011-04-24 08:57:21 +0000186 for( i = n + 1, j = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000187 {
Paul Bakker23986e52011-04-24 08:57:21 +0000188 if( zeros && X->p[i - 1] == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000189 continue;
Paul Bakker5121ce52009-01-03 21:22:43 +0000190
Paul Bakkera755ca12011-04-24 09:11:17 +0000191 for( k = sizeof( t_uint ) - 1; k >= 0; k-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000192 {
Paul Bakker23986e52011-04-24 08:57:21 +0000193 if( zeros && ( ( X->p[i - 1] >> (k << 3) ) & 0xFF ) == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000194 continue;
195 else
196 zeros = 0;
197
198 if( j % 16 == 0 )
199 {
200 if( j > 0 )
201 ssl->f_dbg( ssl->p_dbg, level, "\n" );
202
203 snprintf( str, maxlen, "%s(%04d): ", file, line );
204
205 str[maxlen] = '\0';
206 ssl->f_dbg( ssl->p_dbg, level, str );
207 }
208
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 snprintf( str, maxlen, " %02x", (unsigned int)
Paul Bakker23986e52011-04-24 08:57:21 +0000210 ( X->p[i - 1] >> (k << 3) ) & 0xFF );
Paul Bakker5121ce52009-01-03 21:22:43 +0000211
212 str[maxlen] = '\0';
213 ssl->f_dbg( ssl->p_dbg, level, str );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000214
215 j++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000216 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000217
218 }
219
220 if( zeros == 1 )
221 {
222 snprintf( str, maxlen, "%s(%04d): ", file, line );
223
224 str[maxlen] = '\0';
225 ssl->f_dbg( ssl->p_dbg, level, str );
226 ssl->f_dbg( ssl->p_dbg, level, " 00" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000227 }
228
229 ssl->f_dbg( ssl->p_dbg, level, "\n" );
230}
Paul Bakkered27a042013-04-18 22:46:23 +0200231#endif /* POLARSSL_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200233#if defined(POLARSSL_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200234static void debug_print_pk( const ssl_context *ssl, int level,
235 const char *file, int line,
236 const char *text, const pk_context *pk )
237{
238 size_t i;
239 pk_debug_item items[POLARSSL_PK_DEBUG_MAX_ITEMS];
240 char name[16];
241
242 memset( items, 0, sizeof( items ) );
243
244 if( pk_debug( pk, items ) != 0 )
245 {
246 debug_print_msg( ssl, level, file, line, "invalid PK context" );
247 return;
248 }
249
250 for( i = 0; i < sizeof( items ); i++ )
251 {
252 if( items[i].type == POLARSSL_PK_DEBUG_NONE )
253 return;
254
255 snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
256 name[sizeof( name ) - 1] = '\0';
257
258 if( items[i].type == POLARSSL_PK_DEBUG_MPI )
259 debug_print_mpi( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200260 else
261#if defined(POLARSSL_ECP_C)
262 if( items[i].type == POLARSSL_PK_DEBUG_ECP )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200263 debug_print_ecp( ssl, level, file, line, name, items[i].value );
264 else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200265#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200266 debug_print_msg( ssl, level, file, line, "should not happen" );
267 }
268}
269
Paul Bakkerff60ee62010-03-16 21:09:09 +0000270void debug_print_crt( const ssl_context *ssl, int level,
271 const char *file, int line,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200272 const char *text, const x509_crt *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000273{
Paul Bakkerd98030e2009-05-02 15:13:40 +0000274 char str[1024], prefix[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000275 int i = 0, maxlen = sizeof( prefix ) - 1;
276
277 if( ssl->f_dbg == NULL || crt == NULL )
278 return;
279
280 snprintf( prefix, maxlen, "%s(%04d): ", file, line );
281 prefix[maxlen] = '\0';
282 maxlen = sizeof( str ) - 1;
283
Paul Bakker29087132010-03-21 21:03:34 +0000284 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000285 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000286 char buf[1024];
Paul Bakkerddf26b42013-09-18 13:46:23 +0200287 x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt );
Paul Bakker5121ce52009-01-03 21:22:43 +0000288
289 snprintf( str, maxlen, "%s(%04d): %s #%d:\n%s",
Paul Bakkerd98030e2009-05-02 15:13:40 +0000290 file, line, text, ++i, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000291
292 str[maxlen] = '\0';
293 ssl->f_dbg( ssl->p_dbg, level, str );
294
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200295 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +0000296
297 crt = crt->next;
298 }
299}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200300#endif /* POLARSSL_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000301
302#endif