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