debug.c 9.32 KB
Newer Older
1 2 3
/*
 *  Debugging routines
 *
4
 *  Copyright (C) 2006-2010, Brainspark B.V.
Paul Bakker's avatar
Paul Bakker committed
5 6
 *
 *  This file is part of PolarSSL (http://www.polarssl.org)
7
 *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakker's avatar
Paul Bakker committed
8
 *
9
 *  All rights reserved.
Paul Bakker's avatar
Paul Bakker committed
10
 *
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *  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.
 */

26
#include "polarssl/config.h"
27

28
#if defined(POLARSSL_DEBUG_C)
29

30
#include "polarssl/debug.h"
31 32 33 34

#include <stdarg.h>
#include <stdlib.h>

35 36 37 38
#if defined(EFIX64) || defined(EFI32)
#include <stdio.h>
#endif

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

44
#if !defined vsnprintf
45 46
#define vsnprintf _vsnprintf
#endif
47
#endif /* _MSC_VER */
48

49
static int debug_log_mode = POLARSSL_DEBUG_DFL_MODE;
50
static int debug_threshold = 0;
51 52 53 54 55 56

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

57 58 59 60 61
void debug_set_threshold( int threshold )
{
    debug_threshold = threshold;
}

62 63 64 65 66 67 68 69 70 71 72 73 74 75
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 );
}

76 77
void debug_print_msg( const ssl_context *ssl, int level,
                      const char *file, int line, const char *text )
78 79 80 81
{
    char str[512];
    int maxlen = sizeof( str ) - 1;

82
    if( ssl->f_dbg == NULL || level > debug_threshold )
83 84
        return;

85 86 87 88 89 90
    if( debug_log_mode == POLARSSL_DEBUG_LOG_RAW )
    {
        ssl->f_dbg( ssl->p_dbg, level, str );
        return;
    }

91 92 93 94 95
    snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text );
    str[maxlen] = '\0';
    ssl->f_dbg( ssl->p_dbg, level, str );
}

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

104
    if( ssl->f_dbg == NULL || level > debug_threshold )
105 106
        return;

107 108 109 110 111
    if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
        idx = snprintf( str, maxlen, "%s(%04d): ", file, line );

    snprintf( str + idx, maxlen - idx, "%s() returned %d (-0x%04x)\n",
              text, ret, -ret );
112 113 114 115 116

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

117 118
void debug_print_buf( const ssl_context *ssl, int level,
                      const char *file, int line, const char *text,
119
                      unsigned char *buf, size_t len )
120 121
{
    char str[512];
122
    size_t i, maxlen = sizeof( str ) - 1, idx = 0;
123

124
    if( ssl->f_dbg == NULL || level > debug_threshold )
125 126
        return;

127 128 129 130 131
    if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
        idx = snprintf( str, maxlen, "%s(%04d): ", file, line );

    snprintf( str + idx, maxlen - idx, "dumping '%s' (%d bytes)\n",
              text, (unsigned int) len );
132 133 134 135

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

136
    idx = 0;
137 138 139 140 141 142 143 144
    for( i = 0; i < len; i++ )
    {
        if( i >= 4096 )
            break;

        if( i % 16 == 0 )
        {
            if( i > 0 )
145 146 147 148 149
            {
                snprintf( str + idx, maxlen - idx, "\n" );
                ssl->f_dbg( ssl->p_dbg, level, str );
                idx = 0;
            }
150

151 152 153
            if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
                idx = snprintf( str, maxlen, "%s(%04d): ", file, line );

154 155
            idx += snprintf( str + idx, maxlen - idx, "%04x: ",
                             (unsigned int) i );
156 157 158

        }

159 160
        idx += snprintf( str + idx, maxlen - idx, " %02x",
                         (unsigned int) buf[i] );
161 162 163
    }

    if( len > 0 )
164 165 166 167
    {
        snprintf( str + idx, maxlen - idx, "\n" );
        ssl->f_dbg( ssl->p_dbg, level, str );
    }
168 169
}

170 171 172 173 174 175 176 177
#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;

178 179 180
    if( ssl->f_dbg == NULL || level > debug_threshold )
        return;

181 182 183 184 185 186 187 188 189 190
    snprintf( str, maxlen, "%s(X)", text );
    str[maxlen] = '\0';
    debug_print_mpi( ssl, level, file, line, str, &X->X );

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

191
#if defined(POLARSSL_BIGNUM_C)
192 193 194
void debug_print_mpi( const ssl_context *ssl, int level,
                      const char *file, int line,
                      const char *text, const mpi *X )
195 196
{
    char str[512];
197
    int j, k, maxlen = sizeof( str ) - 1, zeros = 1;
198
    size_t i, n, idx = 0;
199

200
    if( ssl->f_dbg == NULL || X == NULL || level > debug_threshold )
201 202
        return;

203
    for( n = X->n - 1; n > 0; n-- )
204 205 206
        if( X->p[n] != 0 )
            break;

207
    for( j = ( sizeof(t_uint) << 3 ) - 1; j >= 0; j-- )
208 209 210
        if( ( ( X->p[n] >> j ) & 1 ) != 0 )
            break;

211 212 213 214 215
    if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
        idx = snprintf( str, maxlen, "%s(%04d): ", file, line );

    snprintf( str + idx, maxlen - idx, "value of '%s' (%d bits) is:\n",
              text, (int) ( ( n * ( sizeof(t_uint) << 3 ) ) + j + 1 ) );
216 217 218 219

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

220
    idx = 0;
221
    for( i = n + 1, j = 0; i > 0; i-- )
222
    {
223
        if( zeros && X->p[i - 1] == 0 )
224 225
            continue;

226
        for( k = sizeof( t_uint ) - 1; k >= 0; k-- )
227
        {
228
            if( zeros && ( ( X->p[i - 1] >> (k << 3) ) & 0xFF ) == 0 )
229 230 231
                continue;
            else
                zeros = 0;
232

233 234 235
            if( j % 16 == 0 )
            {
                if( j > 0 )
236
                {
237
                    snprintf( str + idx, maxlen - idx, "\n" );
238
                    ssl->f_dbg( ssl->p_dbg, level, str );
239
                    idx = 0;
240
                }
241

242 243 244
                if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
                    idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
            }
245

246 247
            idx += snprintf( str + idx, maxlen - idx, " %02x", (unsigned int)
                             ( X->p[i - 1] >> (k << 3) ) & 0xFF );
248 249

            j++;
250
        }
251 252 253 254 255

    }

    if( zeros == 1 )
    {
256 257
        if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
        {
258
            idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
259

260
        }
261
        idx += snprintf( str + idx, maxlen - idx, " 00" );
262 263
    }

264 265
    snprintf( str + idx, maxlen - idx, "\n" );
    ssl->f_dbg( ssl->p_dbg, level, str );
266
}
267
#endif /* POLARSSL_BIGNUM_C */
268

269
#if defined(POLARSSL_X509_CRT_PARSE_C)
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
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
286
    for( i = 0; i < POLARSSL_PK_DEBUG_MAX_ITEMS; i++ )
287 288 289 290 291 292 293 294 295
    {
        if( items[i].type == POLARSSL_PK_DEBUG_NONE )
            return;

        snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
        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
296 297 298
        else
#if defined(POLARSSL_ECP_C)
        if( items[i].type == POLARSSL_PK_DEBUG_ECP )
299 300
            debug_print_ecp( ssl, level, file, line, name, items[i].value );
        else
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
301
#endif
302 303 304 305
            debug_print_msg( ssl, level, file, line, "should not happen" );
    }
}

306 307
void debug_print_crt( const ssl_context *ssl, int level,
                      const char *file, int line,
308
                      const char *text, const x509_crt *crt )
309
{
310
    char str[1024], prefix[64];
311
    int i = 0, maxlen = sizeof( prefix ) - 1, idx = 0;
312

313
    if( ssl->f_dbg == NULL || crt == NULL || level > debug_threshold )
314 315
        return;

316 317 318 319 320 321 322 323
    if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
    {
        snprintf( prefix, maxlen, "%s(%04d): ", file, line );
        prefix[maxlen] = '\0';
    }
    else
        prefix[0] = '\0';

324 325
    maxlen = sizeof( str ) - 1;

Paul Bakker's avatar
Paul Bakker committed
326
    while( crt != NULL )
327
    {
328
        char buf[1024];
329
        x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt );
330

331 332 333 334 335
        if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
            idx = snprintf( str, maxlen, "%s(%04d): ", file, line );

        snprintf( str + idx, maxlen - idx, "%s #%d:\n%s",
                  text, ++i, buf );
336 337 338 339

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

340
        debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
341 342 343 344

        crt = crt->next;
    }
}
345
#endif /* POLARSSL_X509_CRT_PARSE_C */
346 347

#endif