blob: c5bfa0a9604ceb8376bdb737fc0e245dda14709e [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Debugging routines
3 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00004 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
5 *
Paul Bakker785a9ee2009-01-25 14:15:10 +00006 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
Paul Bakker5121ce52009-01-03 21:22:43 +00007 *
8 * 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
54void debug_print_msg( ssl_context *ssl, int level,
55 char *file, int line, char *text )
56{
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
68void debug_print_ret( ssl_context *ssl, int level,
69 char *file, int line, char *text, int ret )
70{
71 char str[512];
72 int maxlen = sizeof( str ) - 1;
73
74 if( ssl->f_dbg == NULL )
75 return;
76
77 snprintf( str, maxlen, "%s(%04d): %s() returned %d (0x%x)\n",
78 file, line, text, ret, ret );
79
80 str[maxlen] = '\0';
81 ssl->f_dbg( ssl->p_dbg, level, str );
82}
83
84void debug_print_buf( ssl_context *ssl, int level,
85 char *file, int line, char *text,
86 unsigned char *buf, int len )
87{
88 char str[512];
89 int i, maxlen = sizeof( str ) - 1;
90
91 if( ssl->f_dbg == NULL || len < 0 )
92 return;
93
94 snprintf( str, maxlen, "%s(%04d): dumping '%s' (%d bytes)\n",
95 file, line, text, len );
96
97 str[maxlen] = '\0';
98 ssl->f_dbg( ssl->p_dbg, level, str );
99
100 for( i = 0; i < len; i++ )
101 {
102 if( i >= 4096 )
103 break;
104
105 if( i % 16 == 0 )
106 {
107 if( i > 0 )
108 ssl->f_dbg( ssl->p_dbg, level, "\n" );
109
110 snprintf( str, maxlen, "%s(%04d): %04x: ", file, line, i );
111
112 str[maxlen] = '\0';
113 ssl->f_dbg( ssl->p_dbg, level, str );
114 }
115
116 snprintf( str, maxlen, " %02x", (unsigned int) buf[i] );
117
118 str[maxlen] = '\0';
119 ssl->f_dbg( ssl->p_dbg, level, str );
120 }
121
122 if( len > 0 )
123 ssl->f_dbg( ssl->p_dbg, level, "\n" );
124}
125
126void debug_print_mpi( ssl_context *ssl, int level,
127 char *file, int line, char *text, mpi *X )
128{
129 char str[512];
130 int i, j, k, n, maxlen = sizeof( str ) - 1;
131
132 if( ssl->f_dbg == NULL || X == NULL )
133 return;
134
135 for( n = X->n - 1; n >= 0; n-- )
136 if( X->p[n] != 0 )
137 break;
138
139 snprintf( str, maxlen, "%s(%04d): value of '%s' (%d bits) is:\n",
140 file, line, text, ((n + 1) * sizeof( t_int )) << 3 );
141
142 str[maxlen] = '\0';
143 ssl->f_dbg( ssl->p_dbg, level, str );
144
145 for( i = n, j = 0; i >= 0; i--, j++ )
146 {
147 if( j % ( 16 / sizeof( t_int ) ) == 0 )
148 {
149 if( j > 0 )
150 ssl->f_dbg( ssl->p_dbg, level, "\n" );
151
152 snprintf( str, maxlen, "%s(%04d): ", file, line );
153
154 str[maxlen] = '\0';
155 ssl->f_dbg( ssl->p_dbg, level, str );
156 }
157
158 for( k = sizeof( t_int ) - 1; k >= 0; k-- )
159 {
160 snprintf( str, maxlen, " %02x", (unsigned int)
161 ( X->p[i] >> (k << 3) ) & 0xFF );
162
163 str[maxlen] = '\0';
164 ssl->f_dbg( ssl->p_dbg, level, str );
165 }
166 }
167
168 ssl->f_dbg( ssl->p_dbg, level, "\n" );
169}
170
171void debug_print_crt( ssl_context *ssl, int level,
172 char *file, int line, char *text, x509_cert *crt )
173{
Paul Bakker3681b112009-02-07 17:14:21 +0000174 char str[1024], prefix[64], *p;
Paul Bakker5121ce52009-01-03 21:22:43 +0000175 int i = 0, maxlen = sizeof( prefix ) - 1;
176
177 if( ssl->f_dbg == NULL || crt == NULL )
178 return;
179
180 snprintf( prefix, maxlen, "%s(%04d): ", file, line );
181 prefix[maxlen] = '\0';
182 maxlen = sizeof( str ) - 1;
183
184 while( crt != NULL && crt->next != NULL )
185 {
186 p = x509parse_cert_info( prefix, crt );
187
188 snprintf( str, maxlen, "%s(%04d): %s #%d:\n%s",
189 file, line, text, ++i, p );
190
191 str[maxlen] = '\0';
192 ssl->f_dbg( ssl->p_dbg, level, str );
193
194 debug_print_mpi( ssl, level, file, line,
195 "crt->rsa.N", &crt->rsa.N );
196
197 debug_print_mpi( ssl, level, file, line,
198 "crt->rsa.E", &crt->rsa.E );
199
200 crt = crt->next;
201 }
202}
203
204#endif