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