blob: 64717069b3206c9843230c044407cfa286b434da [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Debugging routines
3 *
Paul Bakker83ded912010-03-21 17:46:26 +00004 * Copyright (C) 2006-2010, Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakker77b385e2009-07-28 17:23:11 +00005 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00006 *
Paul Bakker5121ce52009-01-03 21:22:43 +00007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
Paul Bakker40e46942009-01-03 21:51:57 +000022#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000023
Paul Bakker40e46942009-01-03 21:51:57 +000024#if defined(POLARSSL_DEBUG_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Paul Bakker40e46942009-01-03 21:51:57 +000026#include "polarssl/debug.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
28#include <stdarg.h>
29#include <stdlib.h>
30
31#if defined _MSC_VER && !defined snprintf
32#define snprintf _snprintf
33#endif
34
35#if defined _MSC_VER && !defined vsnprintf
36#define vsnprintf _vsnprintf
37#endif
38
39char *debug_fmt( const char *format, ... )
40{
41 va_list argp;
42 static char str[512];
43 int maxlen = sizeof( str ) - 1;
44
45 va_start( argp, format );
46 vsnprintf( str, maxlen, format, argp );
47 va_end( argp );
48
49 str[maxlen] = '\0';
50 return( str );
51}
52
Paul Bakkerff60ee62010-03-16 21:09:09 +000053void debug_print_msg( const ssl_context *ssl, int level,
54 const char *file, int line, const char *text )
Paul Bakker5121ce52009-01-03 21:22:43 +000055{
56 char str[512];
57 int maxlen = sizeof( str ) - 1;
58
59 if( ssl->f_dbg == NULL )
60 return;
61
62 snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text );
63 str[maxlen] = '\0';
64 ssl->f_dbg( ssl->p_dbg, level, str );
65}
66
Paul Bakkerff60ee62010-03-16 21:09:09 +000067void debug_print_ret( const ssl_context *ssl, int level,
68 const char *file, int line,
69 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +000070{
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
Paul Bakkerff60ee62010-03-16 21:09:09 +000084void debug_print_buf( const ssl_context *ssl, int level,
85 const char *file, int line, const char *text,
Paul Bakker5121ce52009-01-03 21:22:43 +000086 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
Paul Bakkerff60ee62010-03-16 21:09:09 +0000126void debug_print_mpi( const ssl_context *ssl, int level,
127 const char *file, int line,
128 const char *text, const mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000129{
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
Paul Bakker4ed999c2010-03-16 21:16:16 +0000140 snprintf( str, maxlen, "%s(%04d): value of '%s' (%lu bits) is:\n",
141 file, line, text,
142 (unsigned long) ((n + 1) * sizeof( t_int )) << 3 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000143
144 str[maxlen] = '\0';
145 ssl->f_dbg( ssl->p_dbg, level, str );
146
147 for( i = n, j = 0; i >= 0; i--, j++ )
148 {
149 if( j % ( 16 / sizeof( t_int ) ) == 0 )
150 {
151 if( j > 0 )
152 ssl->f_dbg( ssl->p_dbg, level, "\n" );
153
154 snprintf( str, maxlen, "%s(%04d): ", file, line );
155
156 str[maxlen] = '\0';
157 ssl->f_dbg( ssl->p_dbg, level, str );
158 }
159
160 for( k = sizeof( t_int ) - 1; k >= 0; k-- )
161 {
162 snprintf( str, maxlen, " %02x", (unsigned int)
163 ( X->p[i] >> (k << 3) ) & 0xFF );
164
165 str[maxlen] = '\0';
166 ssl->f_dbg( ssl->p_dbg, level, str );
167 }
168 }
169
170 ssl->f_dbg( ssl->p_dbg, level, "\n" );
171}
172
Paul Bakkerff60ee62010-03-16 21:09:09 +0000173void debug_print_crt( const ssl_context *ssl, int level,
174 const char *file, int line,
175 const char *text, const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000176{
Paul Bakkerd98030e2009-05-02 15:13:40 +0000177 char str[1024], prefix[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000178 int i = 0, maxlen = sizeof( prefix ) - 1;
179
180 if( ssl->f_dbg == NULL || crt == NULL )
181 return;
182
183 snprintf( prefix, maxlen, "%s(%04d): ", file, line );
184 prefix[maxlen] = '\0';
185 maxlen = sizeof( str ) - 1;
186
Paul Bakker1f761152010-02-18 18:16:31 +0000187 while( crt != NULL && crt->version != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000189 char buf[1024];
190 x509parse_cert_info( buf, sizeof( buf ) - 1, prefix, crt );
Paul Bakker5121ce52009-01-03 21:22:43 +0000191
192 snprintf( str, maxlen, "%s(%04d): %s #%d:\n%s",
Paul Bakkerd98030e2009-05-02 15:13:40 +0000193 file, line, text, ++i, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000194
195 str[maxlen] = '\0';
196 ssl->f_dbg( ssl->p_dbg, level, str );
197
198 debug_print_mpi( ssl, level, file, line,
199 "crt->rsa.N", &crt->rsa.N );
200
201 debug_print_mpi( ssl, level, file, line,
202 "crt->rsa.E", &crt->rsa.E );
203
204 crt = crt->next;
205 }
206}
207
208#endif