blob: 843b9f5ce72281624172f8ed3c52f540360634b8 [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
55void debug_print_msg( ssl_context *ssl, int level,
56 char *file, int line, char *text )
57{
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
69void debug_print_ret( ssl_context *ssl, int level,
70 char *file, int line, char *text, int ret )
71{
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
85void debug_print_buf( ssl_context *ssl, int level,
86 char *file, int line, char *text,
87 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
127void debug_print_mpi( ssl_context *ssl, int level,
128 char *file, int line, char *text, mpi *X )
129{
130 char str[512];
131 int i, j, k, n, maxlen = sizeof( str ) - 1;
132
133 if( ssl->f_dbg == NULL || X == NULL )
134 return;
135
136 for( n = X->n - 1; n >= 0; n-- )
137 if( X->p[n] != 0 )
138 break;
139
140 snprintf( str, maxlen, "%s(%04d): value of '%s' (%d bits) is:\n",
141 file, line, text, ((n + 1) * sizeof( t_int )) << 3 );
142
143 str[maxlen] = '\0';
144 ssl->f_dbg( ssl->p_dbg, level, str );
145
146 for( i = n, j = 0; i >= 0; i--, j++ )
147 {
148 if( j % ( 16 / sizeof( t_int ) ) == 0 )
149 {
150 if( j > 0 )
151 ssl->f_dbg( ssl->p_dbg, level, "\n" );
152
153 snprintf( str, maxlen, "%s(%04d): ", file, line );
154
155 str[maxlen] = '\0';
156 ssl->f_dbg( ssl->p_dbg, level, str );
157 }
158
159 for( k = sizeof( t_int ) - 1; k >= 0; k-- )
160 {
161 snprintf( str, maxlen, " %02x", (unsigned int)
162 ( X->p[i] >> (k << 3) ) & 0xFF );
163
164 str[maxlen] = '\0';
165 ssl->f_dbg( ssl->p_dbg, level, str );
166 }
167 }
168
169 ssl->f_dbg( ssl->p_dbg, level, "\n" );
170}
171
172void debug_print_crt( ssl_context *ssl, int level,
173 char *file, int line, char *text, x509_cert *crt )
174{
Paul Bakkerd98030e2009-05-02 15:13:40 +0000175 char str[1024], prefix[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000176 int i = 0, maxlen = sizeof( prefix ) - 1;
177
178 if( ssl->f_dbg == NULL || crt == NULL )
179 return;
180
181 snprintf( prefix, maxlen, "%s(%04d): ", file, line );
182 prefix[maxlen] = '\0';
183 maxlen = sizeof( str ) - 1;
184
Paul Bakker1f761152010-02-18 18:16:31 +0000185 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000186 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000187 char buf[1024];
188 x509parse_cert_info( buf, sizeof( buf ) - 1, prefix, crt );
Paul Bakker5121ce52009-01-03 21:22:43 +0000189
190 snprintf( str, maxlen, "%s(%04d): %s #%d:\n%s",
Paul Bakkerd98030e2009-05-02 15:13:40 +0000191 file, line, text, ++i, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000192
193 str[maxlen] = '\0';
194 ssl->f_dbg( ssl->p_dbg, level, str );
195
196 debug_print_mpi( ssl, level, file, line,
197 "crt->rsa.N", &crt->rsa.N );
198
199 debug_print_mpi( ssl, level, file, line,
200 "crt->rsa.E", &crt->rsa.E );
201
202 crt = crt->next;
203 }
204}
205
206#endif