blob: f73722210eada7dedd0daf4e5cc5e9bc01f37425 [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.
5 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakker77b385e2009-07-28 17:23:11 +00006 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Paul Bakker40e46942009-01-03 21:51:57 +000023#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000024
Paul Bakker40e46942009-01-03 21:51:57 +000025#if defined(POLARSSL_DEBUG_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Paul Bakker40e46942009-01-03 21:51:57 +000027#include "polarssl/debug.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000028
29#include <stdarg.h>
30#include <stdlib.h>
31
32#if defined _MSC_VER && !defined snprintf
33#define snprintf _snprintf
34#endif
35
36#if defined _MSC_VER && !defined vsnprintf
37#define vsnprintf _vsnprintf
38#endif
39
40char *debug_fmt( const char *format, ... )
41{
42 va_list argp;
43 static char str[512];
44 int maxlen = sizeof( str ) - 1;
45
46 va_start( argp, format );
47 vsnprintf( str, maxlen, format, argp );
48 va_end( argp );
49
50 str[maxlen] = '\0';
51 return( str );
52}
53
Paul Bakkerff60ee62010-03-16 21:09:09 +000054void debug_print_msg( const ssl_context *ssl, int level,
55 const char *file, int line, const char *text )
Paul Bakker5121ce52009-01-03 21:22:43 +000056{
57 char str[512];
58 int maxlen = sizeof( str ) - 1;
59
60 if( ssl->f_dbg == NULL )
61 return;
62
63 snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text );
64 str[maxlen] = '\0';
65 ssl->f_dbg( ssl->p_dbg, level, str );
66}
67
Paul Bakkerff60ee62010-03-16 21:09:09 +000068void debug_print_ret( const ssl_context *ssl, int level,
69 const char *file, int line,
70 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +000071{
72 char str[512];
73 int maxlen = sizeof( str ) - 1;
74
75 if( ssl->f_dbg == NULL )
76 return;
77
78 snprintf( str, maxlen, "%s(%04d): %s() returned %d (0x%x)\n",
79 file, line, text, ret, ret );
80
81 str[maxlen] = '\0';
82 ssl->f_dbg( ssl->p_dbg, level, str );
83}
84
Paul Bakkerff60ee62010-03-16 21:09:09 +000085void debug_print_buf( const ssl_context *ssl, int level,
86 const char *file, int line, const char *text,
Paul Bakker5121ce52009-01-03 21:22:43 +000087 unsigned char *buf, int len )
88{
89 char str[512];
90 int i, maxlen = sizeof( str ) - 1;
91
92 if( ssl->f_dbg == NULL || len < 0 )
93 return;
94
95 snprintf( str, maxlen, "%s(%04d): dumping '%s' (%d bytes)\n",
96 file, line, text, len );
97
98 str[maxlen] = '\0';
99 ssl->f_dbg( ssl->p_dbg, level, str );
100
101 for( i = 0; i < len; i++ )
102 {
103 if( i >= 4096 )
104 break;
105
106 if( i % 16 == 0 )
107 {
108 if( i > 0 )
109 ssl->f_dbg( ssl->p_dbg, level, "\n" );
110
111 snprintf( str, maxlen, "%s(%04d): %04x: ", file, line, i );
112
113 str[maxlen] = '\0';
114 ssl->f_dbg( ssl->p_dbg, level, str );
115 }
116
117 snprintf( str, maxlen, " %02x", (unsigned int) buf[i] );
118
119 str[maxlen] = '\0';
120 ssl->f_dbg( ssl->p_dbg, level, str );
121 }
122
123 if( len > 0 )
124 ssl->f_dbg( ssl->p_dbg, level, "\n" );
125}
126
Paul Bakkerff60ee62010-03-16 21:09:09 +0000127void debug_print_mpi( const ssl_context *ssl, int level,
128 const char *file, int line,
129 const char *text, const mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000130{
131 char str[512];
132 int i, j, k, n, maxlen = sizeof( str ) - 1;
133
134 if( ssl->f_dbg == NULL || X == NULL )
135 return;
136
137 for( n = X->n - 1; n >= 0; n-- )
138 if( X->p[n] != 0 )
139 break;
140
Paul Bakker4ed999c2010-03-16 21:16:16 +0000141 snprintf( str, maxlen, "%s(%04d): value of '%s' (%lu bits) is:\n",
142 file, line, text,
143 (unsigned long) ((n + 1) * sizeof( t_int )) << 3 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000144
145 str[maxlen] = '\0';
146 ssl->f_dbg( ssl->p_dbg, level, str );
147
148 for( i = n, j = 0; i >= 0; i--, j++ )
149 {
150 if( j % ( 16 / sizeof( t_int ) ) == 0 )
151 {
152 if( j > 0 )
153 ssl->f_dbg( ssl->p_dbg, level, "\n" );
154
155 snprintf( str, maxlen, "%s(%04d): ", file, line );
156
157 str[maxlen] = '\0';
158 ssl->f_dbg( ssl->p_dbg, level, str );
159 }
160
161 for( k = sizeof( t_int ) - 1; k >= 0; k-- )
162 {
163 snprintf( str, maxlen, " %02x", (unsigned int)
164 ( X->p[i] >> (k << 3) ) & 0xFF );
165
166 str[maxlen] = '\0';
167 ssl->f_dbg( ssl->p_dbg, level, str );
168 }
169 }
170
171 ssl->f_dbg( ssl->p_dbg, level, "\n" );
172}
173
Paul Bakkerff60ee62010-03-16 21:09:09 +0000174void debug_print_crt( const ssl_context *ssl, int level,
175 const char *file, int line,
176 const char *text, const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000177{
Paul Bakkerd98030e2009-05-02 15:13:40 +0000178 char str[1024], prefix[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000179 int i = 0, maxlen = sizeof( prefix ) - 1;
180
181 if( ssl->f_dbg == NULL || crt == NULL )
182 return;
183
184 snprintf( prefix, maxlen, "%s(%04d): ", file, line );
185 prefix[maxlen] = '\0';
186 maxlen = sizeof( str ) - 1;
187
Paul Bakker29087132010-03-21 21:03:34 +0000188 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000189 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000190 char buf[1024];
191 x509parse_cert_info( buf, sizeof( buf ) - 1, prefix, crt );
Paul Bakker5121ce52009-01-03 21:22:43 +0000192
193 snprintf( str, maxlen, "%s(%04d): %s #%d:\n%s",
Paul Bakkerd98030e2009-05-02 15:13:40 +0000194 file, line, text, ++i, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000195
196 str[maxlen] = '\0';
197 ssl->f_dbg( ssl->p_dbg, level, str );
198
199 debug_print_mpi( ssl, level, file, line,
200 "crt->rsa.N", &crt->rsa.N );
201
202 debug_print_mpi( ssl, level, file, line,
203 "crt->rsa.E", &crt->rsa.E );
204
205 crt = crt->next;
206 }
207}
208
209#endif