blob: fbab1c0b93daf565547c1b632d7b2b46428d0ce4 [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
Manuel Pégourié-Gonnard26d88cf2015-06-27 11:48:01 +020040#define DEBUG_BUF_SIZE 512
41
Paul Bakker5121ce52009-01-03 21:22:43 +000042char *debug_fmt( const char *format, ... )
43{
44 va_list argp;
Manuel Pégourié-Gonnard26d88cf2015-06-27 11:48:01 +020045 char *str = malloc( DEBUG_BUF_SIZE );
46
47 if( str == NULL )
48 return( NULL );
Paul Bakker5121ce52009-01-03 21:22:43 +000049
50 va_start( argp, format );
Manuel Pégourié-Gonnard26d88cf2015-06-27 11:48:01 +020051 vsnprintf( str, DEBUG_BUF_SIZE - 1, format, argp );
Paul Bakker5121ce52009-01-03 21:22:43 +000052 va_end( argp );
53
Manuel Pégourié-Gonnard26d88cf2015-06-27 11:48:01 +020054 str[DEBUG_BUF_SIZE - 1] = '\0';
Paul Bakker5121ce52009-01-03 21:22:43 +000055 return( str );
56}
57
Manuel Pégourié-Gonnard26d88cf2015-06-27 11:48:01 +020058void debug_print_msg_free( const ssl_context *ssl, int level,
59 const char *file, int line, char *text )
60{
61 if( text != NULL )
62 debug_print_msg( ssl, level, file, line, text );
63
64 free( text );
65}
66
Paul Bakkerff60ee62010-03-16 21:09:09 +000067void debug_print_msg( const ssl_context *ssl, int level,
68 const char *file, int line, const char *text )
Paul Bakker5121ce52009-01-03 21:22:43 +000069{
70 char str[512];
71 int maxlen = sizeof( str ) - 1;
72
73 if( ssl->f_dbg == NULL )
74 return;
75
76 snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text );
77 str[maxlen] = '\0';
78 ssl->f_dbg( ssl->p_dbg, level, str );
79}
80
Paul Bakkerff60ee62010-03-16 21:09:09 +000081void debug_print_ret( const ssl_context *ssl, int level,
82 const char *file, int line,
83 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +000084{
85 char str[512];
86 int maxlen = sizeof( str ) - 1;
87
88 if( ssl->f_dbg == NULL )
89 return;
90
Manuel Pégourié-Gonnardc2262b52014-03-25 13:22:23 +010091 snprintf( str, maxlen, "%s(%04d): %s() returned %d (-0x%04x)\n",
92 file, line, text, ret, -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000093
94 str[maxlen] = '\0';
95 ssl->f_dbg( ssl->p_dbg, level, str );
96}
97
Paul Bakkerff60ee62010-03-16 21:09:09 +000098void debug_print_buf( const ssl_context *ssl, int level,
99 const char *file, int line, const char *text,
Paul Bakker23986e52011-04-24 08:57:21 +0000100 unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000101{
102 char str[512];
Paul Bakker23986e52011-04-24 08:57:21 +0000103 size_t i, maxlen = sizeof( str ) - 1;
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
Paul Bakker23986e52011-04-24 08:57:21 +0000105 if( ssl->f_dbg == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 return;
107
Peter Vaskovic02388c92014-05-24 15:19:35 +0200108 snprintf( str, maxlen, "%s(%04d): dumping '%s' (%u bytes)\n",
Paul Bakkerf4f69682011-04-24 16:08:12 +0000109 file, line, text, (unsigned int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
111 str[maxlen] = '\0';
112 ssl->f_dbg( ssl->p_dbg, level, str );
113
114 for( i = 0; i < len; i++ )
115 {
116 if( i >= 4096 )
117 break;
118
119 if( i % 16 == 0 )
120 {
121 if( i > 0 )
122 ssl->f_dbg( ssl->p_dbg, level, "\n" );
123
Paul Bakkerf4f69682011-04-24 16:08:12 +0000124 snprintf( str, maxlen, "%s(%04d): %04x: ", file, line,
125 (unsigned int) i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
127 str[maxlen] = '\0';
128 ssl->f_dbg( ssl->p_dbg, level, str );
129 }
130
131 snprintf( str, maxlen, " %02x", (unsigned int) buf[i] );
132
133 str[maxlen] = '\0';
134 ssl->f_dbg( ssl->p_dbg, level, str );
135 }
136
137 if( len > 0 )
138 ssl->f_dbg( ssl->p_dbg, level, "\n" );
139}
140
Paul Bakkerff60ee62010-03-16 21:09:09 +0000141void debug_print_mpi( const ssl_context *ssl, int level,
142 const char *file, int line,
143 const char *text, const mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000144{
145 char str[512];
Paul Bakker23986e52011-04-24 08:57:21 +0000146 int j, k, maxlen = sizeof( str ) - 1, zeros = 1;
147 size_t i, n;
Paul Bakker5121ce52009-01-03 21:22:43 +0000148
149 if( ssl->f_dbg == NULL || X == NULL )
150 return;
151
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000152 for( n = X->n - 1; n > 0; n-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 if( X->p[n] != 0 )
154 break;
155
Paul Bakkera755ca12011-04-24 09:11:17 +0000156 for( j = ( sizeof(t_uint) << 3 ) - 1; j >= 0; j-- )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000157 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
158 break;
159
Paul Bakker5c2364c2012-10-01 14:41:15 +0000160 snprintf( str, maxlen, "%s(%04d): value of '%s' (%d bits) is:\n",
Paul Bakker4ed999c2010-03-16 21:16:16 +0000161 file, line, text,
Paul Bakker5c2364c2012-10-01 14:41:15 +0000162 (int) ( ( n * ( sizeof(t_uint) << 3 ) ) + j + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
164 str[maxlen] = '\0';
165 ssl->f_dbg( ssl->p_dbg, level, str );
166
Paul Bakker23986e52011-04-24 08:57:21 +0000167 for( i = n + 1, j = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000168 {
Paul Bakker23986e52011-04-24 08:57:21 +0000169 if( zeros && X->p[i - 1] == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000170 continue;
Paul Bakker5121ce52009-01-03 21:22:43 +0000171
Paul Bakkera755ca12011-04-24 09:11:17 +0000172 for( k = sizeof( t_uint ) - 1; k >= 0; k-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 {
Paul Bakker23986e52011-04-24 08:57:21 +0000174 if( zeros && ( ( X->p[i - 1] >> (k << 3) ) & 0xFF ) == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000175 continue;
176 else
177 zeros = 0;
178
179 if( j % 16 == 0 )
180 {
181 if( j > 0 )
182 ssl->f_dbg( ssl->p_dbg, level, "\n" );
183
184 snprintf( str, maxlen, "%s(%04d): ", file, line );
185
186 str[maxlen] = '\0';
187 ssl->f_dbg( ssl->p_dbg, level, str );
188 }
189
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 snprintf( str, maxlen, " %02x", (unsigned int)
Paul Bakker23986e52011-04-24 08:57:21 +0000191 ( X->p[i - 1] >> (k << 3) ) & 0xFF );
Paul Bakker5121ce52009-01-03 21:22:43 +0000192
193 str[maxlen] = '\0';
194 ssl->f_dbg( ssl->p_dbg, level, str );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000195
196 j++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000197 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000198
199 }
200
201 if( zeros == 1 )
202 {
203 snprintf( str, maxlen, "%s(%04d): ", file, line );
204
205 str[maxlen] = '\0';
206 ssl->f_dbg( ssl->p_dbg, level, str );
207 ssl->f_dbg( ssl->p_dbg, level, " 00" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000208 }
209
210 ssl->f_dbg( ssl->p_dbg, level, "\n" );
211}
212
Paul Bakkerff60ee62010-03-16 21:09:09 +0000213void debug_print_crt( const ssl_context *ssl, int level,
214 const char *file, int line,
215 const char *text, const x509_cert *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000216{
Paul Bakkerd98030e2009-05-02 15:13:40 +0000217 char str[1024], prefix[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 int i = 0, maxlen = sizeof( prefix ) - 1;
219
220 if( ssl->f_dbg == NULL || crt == NULL )
221 return;
222
223 snprintf( prefix, maxlen, "%s(%04d): ", file, line );
224 prefix[maxlen] = '\0';
225 maxlen = sizeof( str ) - 1;
226
Paul Bakker29087132010-03-21 21:03:34 +0000227 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000229 char buf[1024];
230 x509parse_cert_info( buf, sizeof( buf ) - 1, prefix, crt );
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
232 snprintf( str, maxlen, "%s(%04d): %s #%d:\n%s",
Paul Bakkerd98030e2009-05-02 15:13:40 +0000233 file, line, text, ++i, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000234
235 str[maxlen] = '\0';
236 ssl->f_dbg( ssl->p_dbg, level, str );
237
238 debug_print_mpi( ssl, level, file, line,
239 "crt->rsa.N", &crt->rsa.N );
240
241 debug_print_mpi( ssl, level, file, line,
242 "crt->rsa.E", &crt->rsa.E );
243
244 crt = crt->next;
245 }
246}
247
248#endif