debug.c 9.93 KB
Newer Older
1 2 3
/*
 *  Debugging routines
 *
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
4
 *  Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker's avatar
Paul Bakker committed
5
 *
6
 *  This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker's avatar
Paul Bakker committed
7
 *
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

23
#if !defined(POLARSSL_CONFIG_FILE)
24
#include "mbedtls/config.h"
25 26 27
#else
#include POLARSSL_CONFIG_FILE
#endif
28

29
#if defined(POLARSSL_DEBUG_C)
30

31
#include "mbedtls/debug.h"
32 33

#include <stdarg.h>
34
#include <stdio.h>
35
#include <string.h>
36

37 38
#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
#if !defined  snprintf
39 40 41
#define  snprintf  _snprintf
#endif

42
#if !defined vsnprintf
43 44
#define vsnprintf _vsnprintf
#endif
45
#endif /* _MSC_VER */
46

47
#if defined(POLARSSL_PLATFORM_C)
48
#include "mbedtls/platform.h"
49 50 51 52
#else
#define polarssl_snprintf snprintf
#endif

53
static int debug_log_mode = POLARSSL_DEBUG_DFL_MODE;
54
static int debug_threshold = 0;
55 56 57 58 59 60

void debug_set_log_mode( int log_mode )
{
    debug_log_mode = log_mode;
}

61 62 63 64 65
void debug_set_threshold( int threshold )
{
    debug_threshold = threshold;
}

66 67 68 69 70 71 72 73 74 75 76 77 78 79
char *debug_fmt( const char *format, ... )
{
    va_list argp;
    static char str[512];
    int maxlen = sizeof( str ) - 1;

    va_start( argp, format );
    vsnprintf( str, maxlen, format, argp );
    va_end( argp );

    str[maxlen] = '\0';
    return( str );
}

80 81
void debug_print_msg( const ssl_context *ssl, int level,
                      const char *file, int line, const char *text )
82 83 84 85
{
    char str[512];
    int maxlen = sizeof( str ) - 1;

86
    if( ssl->f_dbg == NULL || level > debug_threshold )
87 88
        return;

89 90
    if( debug_log_mode == POLARSSL_DEBUG_LOG_RAW )
    {
Paul Bakker's avatar
Paul Bakker committed
91
        ssl->f_dbg( ssl->p_dbg, level, text );
92 93 94
        return;
    }

95
    polarssl_snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text );
96 97 98 99
    str[maxlen] = '\0';
    ssl->f_dbg( ssl->p_dbg, level, str );
}

100 101 102
void debug_print_ret( const ssl_context *ssl, int level,
                      const char *file, int line,
                      const char *text, int ret )
103 104 105
{
    char str[512];
    int maxlen = sizeof( str ) - 1;
106
    size_t idx = 0;
107

108
    if( ssl->f_dbg == NULL || level > debug_threshold )
109 110
        return;

111
    if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
112
        idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line );
113

114
    polarssl_snprintf( str + idx, maxlen - idx, "%s() returned %d (-0x%04x)\n",
115
              text, ret, -ret );
116 117 118 119 120

    str[maxlen] = '\0';
    ssl->f_dbg( ssl->p_dbg, level, str );
}

121 122
void debug_print_buf( const ssl_context *ssl, int level,
                      const char *file, int line, const char *text,
123
                      const unsigned char *buf, size_t len )
124 125
{
    char str[512];
126
    char txt[17];
127
    size_t i, maxlen = sizeof( str ) - 1, idx = 0;
128

129
    if( ssl->f_dbg == NULL || level > debug_threshold )
130 131
        return;

132
    if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
133
        idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line );
134

135
    polarssl_snprintf( str + idx, maxlen - idx, "dumping '%s' (%u bytes)\n",
136
              text, (unsigned int) len );
137 138 139 140

    str[maxlen] = '\0';
    ssl->f_dbg( ssl->p_dbg, level, str );

141
    idx = 0;
142
    memset( txt, 0, sizeof( txt ) );
143 144 145 146 147 148 149 150
    for( i = 0; i < len; i++ )
    {
        if( i >= 4096 )
            break;

        if( i % 16 == 0 )
        {
            if( i > 0 )
151
            {
152
                polarssl_snprintf( str + idx, maxlen - idx, "  %s\n", txt );
153
                ssl->f_dbg( ssl->p_dbg, level, str );
154

155
                idx = 0;
156
                memset( txt, 0, sizeof( txt ) );
157
            }
158

159
            if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
160
                idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line );
161

162
            idx += polarssl_snprintf( str + idx, maxlen - idx, "%04x: ",
163
                             (unsigned int) i );
164 165 166

        }

167
        idx += polarssl_snprintf( str + idx, maxlen - idx, " %02x",
168
                         (unsigned int) buf[i] );
169
        txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
170 171 172
    }

    if( len > 0 )
173
    {
174
        for( /* i = i */; i % 16 != 0; i++ )
175
            idx += polarssl_snprintf( str + idx, maxlen - idx, "   " );
176

177
        polarssl_snprintf( str + idx, maxlen - idx, "  %s\n", txt );
178 179
        ssl->f_dbg( ssl->p_dbg, level, str );
    }
180 181
}

182 183 184 185 186 187 188 189
#if defined(POLARSSL_ECP_C)
void debug_print_ecp( const ssl_context *ssl, int level,
                      const char *file, int line,
                      const char *text, const ecp_point *X )
{
    char str[512];
    int maxlen = sizeof( str ) - 1;

190 191 192
    if( ssl->f_dbg == NULL || level > debug_threshold )
        return;

193
    polarssl_snprintf( str, maxlen, "%s(X)", text );
194 195 196
    str[maxlen] = '\0';
    debug_print_mpi( ssl, level, file, line, str, &X->X );

197
    polarssl_snprintf( str, maxlen, "%s(Y)", text );
198 199 200 201 202
    str[maxlen] = '\0';
    debug_print_mpi( ssl, level, file, line, str, &X->Y );
}
#endif /* POLARSSL_ECP_C */

203
#if defined(POLARSSL_BIGNUM_C)
204 205 206
void debug_print_mpi( const ssl_context *ssl, int level,
                      const char *file, int line,
                      const char *text, const mpi *X )
207 208
{
    char str[512];
209
    int j, k, maxlen = sizeof( str ) - 1, zeros = 1;
210
    size_t i, n, idx = 0;
211

212
    if( ssl->f_dbg == NULL || X == NULL || level > debug_threshold )
213 214
        return;

215
    for( n = X->n - 1; n > 0; n-- )
216 217 218
        if( X->p[n] != 0 )
            break;

219
    for( j = ( sizeof(t_uint) << 3 ) - 1; j >= 0; j-- )
220 221 222
        if( ( ( X->p[n] >> j ) & 1 ) != 0 )
            break;

223
    if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
224
        idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line );
225

226
    polarssl_snprintf( str + idx, maxlen - idx, "value of '%s' (%d bits) is:\n",
227
              text, (int) ( ( n * ( sizeof(t_uint) << 3 ) ) + j + 1 ) );
228 229 230 231

    str[maxlen] = '\0';
    ssl->f_dbg( ssl->p_dbg, level, str );

232
    idx = 0;
233
    for( i = n + 1, j = 0; i > 0; i-- )
234
    {
235
        if( zeros && X->p[i - 1] == 0 )
236 237
            continue;

238
        for( k = sizeof( t_uint ) - 1; k >= 0; k-- )
239
        {
240
            if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
241 242 243
                continue;
            else
                zeros = 0;
244

245 246 247
            if( j % 16 == 0 )
            {
                if( j > 0 )
248
                {
249
                    polarssl_snprintf( str + idx, maxlen - idx, "\n" );
250
                    ssl->f_dbg( ssl->p_dbg, level, str );
251
                    idx = 0;
252
                }
253

254
                if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
255
                    idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line );
256
            }
257

258
            idx += polarssl_snprintf( str + idx, maxlen - idx, " %02x", (unsigned int)
259
                             ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
260 261

            j++;
262
        }
263 264 265 266 267

    }

    if( zeros == 1 )
    {
268 269
        if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
        {
270
            idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line );
271

272
        }
273
        idx += polarssl_snprintf( str + idx, maxlen - idx, " 00" );
274 275
    }

276
    polarssl_snprintf( str + idx, maxlen - idx, "\n" );
277
    ssl->f_dbg( ssl->p_dbg, level, str );
278
}
279
#endif /* POLARSSL_BIGNUM_C */
280

281
#if defined(POLARSSL_X509_CRT_PARSE_C)
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
static void debug_print_pk( const ssl_context *ssl, int level,
                            const char *file, int line,
                            const char *text, const pk_context *pk )
{
    size_t i;
    pk_debug_item items[POLARSSL_PK_DEBUG_MAX_ITEMS];
    char name[16];

    memset( items, 0, sizeof( items ) );

    if( pk_debug( pk, items ) != 0 )
    {
        debug_print_msg( ssl, level, file, line, "invalid PK context" );
        return;
    }

Paul Bakker's avatar
Paul Bakker committed
298
    for( i = 0; i < POLARSSL_PK_DEBUG_MAX_ITEMS; i++ )
299 300 301 302
    {
        if( items[i].type == POLARSSL_PK_DEBUG_NONE )
            return;

303
        polarssl_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
304 305 306 307
        name[sizeof( name ) - 1] = '\0';

        if( items[i].type == POLARSSL_PK_DEBUG_MPI )
            debug_print_mpi( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
308 309 310
        else
#if defined(POLARSSL_ECP_C)
        if( items[i].type == POLARSSL_PK_DEBUG_ECP )
311 312
            debug_print_ecp( ssl, level, file, line, name, items[i].value );
        else
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
313
#endif
314 315 316 317
            debug_print_msg( ssl, level, file, line, "should not happen" );
    }
}

318 319
void debug_print_crt( const ssl_context *ssl, int level,
                      const char *file, int line,
320
                      const char *text, const x509_crt *crt )
321
{
322
    char str[1024], prefix[64];
323
    int i = 0, maxlen = sizeof( prefix ) - 1, idx = 0;
324

325
    if( ssl->f_dbg == NULL || crt == NULL || level > debug_threshold )
326 327
        return;

328 329
    if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
    {
330
        polarssl_snprintf( prefix, maxlen, "%s(%04d): ", file, line );
331 332 333 334 335
        prefix[maxlen] = '\0';
    }
    else
        prefix[0] = '\0';

336 337
    maxlen = sizeof( str ) - 1;

Paul Bakker's avatar
Paul Bakker committed
338
    while( crt != NULL )
339
    {
340
        char buf[1024];
341
        x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt );
342

343
        if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
344
            idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line );
345

346
        polarssl_snprintf( str + idx, maxlen - idx, "%s #%d:\n%s",
347
                  text, ++i, buf );
348 349 350 351

        str[maxlen] = '\0';
        ssl->f_dbg( ssl->p_dbg, level, str );

352
        debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
353 354 355 356

        crt = crt->next;
    }
}
357
#endif /* POLARSSL_X509_CRT_PARSE_C */
358

359
#endif /* POLARSSL_DEBUG_C */