debug.c 9.42 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
#if !defined(POLARSSL_CONFIG_FILE)
27
#include "polarssl/config.h"
28 29 30
#else
#include POLARSSL_CONFIG_FILE
#endif
31

32
#if defined(POLARSSL_DEBUG_C)
33

34
#include "polarssl/debug.h"
35 36 37 38

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

39 40 41 42
#if defined(EFIX64) || defined(EFI32)
#include <stdio.h>
#endif

43 44
#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
#if !defined  snprintf
45 46 47
#define  snprintf  _snprintf
#endif

48
#if !defined vsnprintf
49 50
#define vsnprintf _vsnprintf
#endif
51
#endif /* _MSC_VER */
52

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 91 92 93 94
    if( debug_log_mode == POLARSSL_DEBUG_LOG_RAW )
    {
        ssl->f_dbg( ssl->p_dbg, level, str );
        return;
    }

95 96 97 98 99
    snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text );
    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 112 113 114 115
    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 );
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
                      unsigned char *buf, size_t len )
124 125
{
    char str[512];
126
    size_t i, maxlen = sizeof( str ) - 1, idx = 0;
127

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

131 132 133 134 135
    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 );
136 137 138 139

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

140
    idx = 0;
141 142 143 144 145 146 147 148
    for( i = 0; i < len; i++ )
    {
        if( i >= 4096 )
            break;

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

155 156 157
            if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
                idx = snprintf( str, maxlen, "%s(%04d): ", file, line );

158 159
            idx += snprintf( str + idx, maxlen - idx, "%04x: ",
                             (unsigned int) i );
160 161 162

        }

163 164
        idx += snprintf( str + idx, maxlen - idx, " %02x",
                         (unsigned int) buf[i] );
165 166 167
    }

    if( len > 0 )
168 169 170 171
    {
        snprintf( str + idx, maxlen - idx, "\n" );
        ssl->f_dbg( ssl->p_dbg, level, str );
    }
172 173
}

174 175 176 177 178 179 180 181
#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;

182 183 184
    if( ssl->f_dbg == NULL || level > debug_threshold )
        return;

185 186 187 188 189 190 191 192 193 194
    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 */

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

204
    if( ssl->f_dbg == NULL || X == NULL || level > debug_threshold )
205 206
        return;

207
    for( n = X->n - 1; n > 0; n-- )
208 209 210
        if( X->p[n] != 0 )
            break;

211
    for( j = ( sizeof(t_uint) << 3 ) - 1; j >= 0; j-- )
212 213 214
        if( ( ( X->p[n] >> j ) & 1 ) != 0 )
            break;

215 216 217 218 219
    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 ) );
220 221 222 223

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

224
    idx = 0;
225
    for( i = n + 1, j = 0; i > 0; i-- )
226
    {
227
        if( zeros && X->p[i - 1] == 0 )
228 229
            continue;

230
        for( k = sizeof( t_uint ) - 1; k >= 0; k-- )
231
        {
232
            if( zeros && ( ( X->p[i - 1] >> (k << 3) ) & 0xFF ) == 0 )
233 234 235
                continue;
            else
                zeros = 0;
236

237 238 239
            if( j % 16 == 0 )
            {
                if( j > 0 )
240
                {
241
                    snprintf( str + idx, maxlen - idx, "\n" );
242
                    ssl->f_dbg( ssl->p_dbg, level, str );
243
                    idx = 0;
244
                }
245

246 247 248
                if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
                    idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
            }
249

250 251
            idx += snprintf( str + idx, maxlen - idx, " %02x", (unsigned int)
                             ( X->p[i - 1] >> (k << 3) ) & 0xFF );
252 253

            j++;
254
        }
255 256 257 258 259

    }

    if( zeros == 1 )
    {
260 261
        if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
        {
262
            idx = snprintf( str, maxlen, "%s(%04d): ", file, line );
263

264
        }
265
        idx += snprintf( str + idx, maxlen - idx, " 00" );
266 267
    }

268 269
    snprintf( str + idx, maxlen - idx, "\n" );
    ssl->f_dbg( ssl->p_dbg, level, str );
270
}
271
#endif /* POLARSSL_BIGNUM_C */
272

273
#if defined(POLARSSL_X509_CRT_PARSE_C)
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
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
290
    for( i = 0; i < POLARSSL_PK_DEBUG_MAX_ITEMS; i++ )
291 292 293 294 295 296 297 298 299
    {
        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
300 301 302
        else
#if defined(POLARSSL_ECP_C)
        if( items[i].type == POLARSSL_PK_DEBUG_ECP )
303 304
            debug_print_ecp( ssl, level, file, line, name, items[i].value );
        else
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
305
#endif
306 307 308 309
            debug_print_msg( ssl, level, file, line, "should not happen" );
    }
}

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

317
    if( ssl->f_dbg == NULL || crt == NULL || level > debug_threshold )
318 319
        return;

320 321 322 323 324 325 326 327
    if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
    {
        snprintf( prefix, maxlen, "%s(%04d): ", file, line );
        prefix[maxlen] = '\0';
    }
    else
        prefix[0] = '\0';

328 329
    maxlen = sizeof( str ) - 1;

Paul Bakker's avatar
Paul Bakker committed
330
    while( crt != NULL )
331
    {
332
        char buf[1024];
333
        x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt );
334

335 336 337 338 339
        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 );
340 341 342 343

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

344
        debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
345 346 347 348

        crt = crt->next;
    }
}
349
#endif /* POLARSSL_X509_CRT_PARSE_C */
350

351
#endif /* POLARSSL_DEBUG_C */