blob: 77cd77d14d6f61271e6e59f7127375c0b74253b8 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Debugging routines
3 *
Paul Bakker530927b2015-02-13 14:24:10 +01004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * 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
Paul Bakkerff60ee62010-03-16 21:09:09 +000054void debug_print_msg( const ssl_context *ssl, int level,
55 const char *file, int line, const char *text )
Paul Bakker5121ce52009-01-03 21:22:43 +000056{
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
Paul Bakkerff60ee62010-03-16 21:09:09 +000068void debug_print_ret( const ssl_context *ssl, int level,
69 const char *file, int line,
70 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +000071{
72 char str[512];
73 int maxlen = sizeof( str ) - 1;
74
75 if( ssl->f_dbg == NULL )
76 return;
77
Manuel Pégourié-Gonnardc2262b52014-03-25 13:22:23 +010078 snprintf( str, maxlen, "%s(%04d): %s() returned %d (-0x%04x)\n",
79 file, line, text, ret, -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000080
81 str[maxlen] = '\0';
82 ssl->f_dbg( ssl->p_dbg, level, str );
83}
84
Paul Bakkerff60ee62010-03-16 21:09:09 +000085void debug_print_buf( const ssl_context *ssl, int level,
86 const char *file, int line, const char *text,
Paul Bakker23986e52011-04-24 08:57:21 +000087 unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000088{
89 char str[512];
Paul Bakker23986e52011-04-24 08:57:21 +000090 size_t i, maxlen = sizeof( str ) - 1;
Paul Bakker5121ce52009-01-03 21:22:43 +000091
Paul Bakker23986e52011-04-24 08:57:21 +000092 if( ssl->f_dbg == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000093 return;
94
Peter Vaskovic02388c92014-05-24 15:19:35 +020095 snprintf( str, maxlen, "%s(%04d): dumping '%s' (%u bytes)\n",
Paul Bakkerf4f69682011-04-24 16:08:12 +000096 file, line, text, (unsigned int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000097
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
Paul Bakkerf4f69682011-04-24 16:08:12 +0000111 snprintf( str, maxlen, "%s(%04d): %04x: ", file, line,
112 (unsigned int) i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
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];
Paul Bakker23986e52011-04-24 08:57:21 +0000133 int j, k, maxlen = sizeof( str ) - 1, zeros = 1;
134 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000135
136 if( ssl->f_dbg == NULL || X == NULL )
137 return;
138
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000139 for( n = X->n - 1; n > 0; n-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 if( X->p[n] != 0 )
141 break;
142
Paul Bakkera755ca12011-04-24 09:11:17 +0000143 for( j = ( sizeof(t_uint) << 3 ) - 1; j >= 0; j-- )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000144 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
145 break;
146
Paul Bakker5c2364c2012-10-01 14:41:15 +0000147 snprintf( str, maxlen, "%s(%04d): value of '%s' (%d bits) is:\n",
Paul Bakker4ed999c2010-03-16 21:16:16 +0000148 file, line, text,
Paul Bakker5c2364c2012-10-01 14:41:15 +0000149 (int) ( ( n * ( sizeof(t_uint) << 3 ) ) + j + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
151 str[maxlen] = '\0';
152 ssl->f_dbg( ssl->p_dbg, level, str );
153
Paul Bakker23986e52011-04-24 08:57:21 +0000154 for( i = n + 1, j = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 {
Paul Bakker23986e52011-04-24 08:57:21 +0000156 if( zeros && X->p[i - 1] == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000157 continue;
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
Paul Bakkera755ca12011-04-24 09:11:17 +0000159 for( k = sizeof( t_uint ) - 1; k >= 0; k-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 {
Paul Bakker23986e52011-04-24 08:57:21 +0000161 if( zeros && ( ( X->p[i - 1] >> (k << 3) ) & 0xFF ) == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000162 continue;
163 else
164 zeros = 0;
165
166 if( j % 16 == 0 )
167 {
168 if( j > 0 )
169 ssl->f_dbg( ssl->p_dbg, level, "\n" );
170
171 snprintf( str, maxlen, "%s(%04d): ", file, line );
172
173 str[maxlen] = '\0';
174 ssl->f_dbg( ssl->p_dbg, level, str );
175 }
176
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 snprintf( str, maxlen, " %02x", (unsigned int)
Paul Bakker23986e52011-04-24 08:57:21 +0000178 ( X->p[i - 1] >> (k << 3) ) & 0xFF );
Paul Bakker5121ce52009-01-03 21:22:43 +0000179
180 str[maxlen] = '\0';
181 ssl->f_dbg( ssl->p_dbg, level, str );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000182
183 j++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000185
186 }
187
188 if( zeros == 1 )
189 {
190 snprintf( str, maxlen, "%s(%04d): ", file, line );
191
192 str[maxlen] = '\0';
193 ssl->f_dbg( ssl->p_dbg, level, str );
194 ssl->f_dbg( ssl->p_dbg, level, " 00" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000195 }
196
197 ssl->f_dbg( ssl->p_dbg, level, "\n" );
198}
199
Paul Bakkerff60ee62010-03-16 21:09:09 +0000200void debug_print_crt( const ssl_context *ssl, int level,
201 const char *file, int line,
202 const char *text, const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000203{
Paul Bakkerd98030e2009-05-02 15:13:40 +0000204 char str[1024], prefix[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 int i = 0, maxlen = sizeof( prefix ) - 1;
206
207 if( ssl->f_dbg == NULL || crt == NULL )
208 return;
209
210 snprintf( prefix, maxlen, "%s(%04d): ", file, line );
211 prefix[maxlen] = '\0';
212 maxlen = sizeof( str ) - 1;
213
Paul Bakker29087132010-03-21 21:03:34 +0000214 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000215 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000216 char buf[1024];
217 x509parse_cert_info( buf, sizeof( buf ) - 1, prefix, crt );
Paul Bakker5121ce52009-01-03 21:22:43 +0000218
219 snprintf( str, maxlen, "%s(%04d): %s #%d:\n%s",
Paul Bakkerd98030e2009-05-02 15:13:40 +0000220 file, line, text, ++i, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
222 str[maxlen] = '\0';
223 ssl->f_dbg( ssl->p_dbg, level, str );
224
225 debug_print_mpi( ssl, level, file, line,
226 "crt->rsa.N", &crt->rsa.N );
227
228 debug_print_mpi( ssl, level, file, line,
229 "crt->rsa.E", &crt->rsa.E );
230
231 crt = crt->next;
232 }
233}
234
235#endif