Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Debugging routines |
| 3 | * |
| 4 | * Copyright (C) 2006-2007 Christophe Devine |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 21 | #include "polarssl/config.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 22 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 23 | #if defined(POLARSSL_DEBUG_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 24 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 25 | #include "polarssl/debug.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 26 | |
| 27 | #include <stdarg.h> |
| 28 | #include <stdlib.h> |
| 29 | |
| 30 | #if defined _MSC_VER && !defined snprintf |
| 31 | #define snprintf _snprintf |
| 32 | #endif |
| 33 | |
| 34 | #if defined _MSC_VER && !defined vsnprintf |
| 35 | #define vsnprintf _vsnprintf |
| 36 | #endif |
| 37 | |
| 38 | char *debug_fmt( const char *format, ... ) |
| 39 | { |
| 40 | va_list argp; |
| 41 | static char str[512]; |
| 42 | int maxlen = sizeof( str ) - 1; |
| 43 | |
| 44 | va_start( argp, format ); |
| 45 | vsnprintf( str, maxlen, format, argp ); |
| 46 | va_end( argp ); |
| 47 | |
| 48 | str[maxlen] = '\0'; |
| 49 | return( str ); |
| 50 | } |
| 51 | |
| 52 | void debug_print_msg( ssl_context *ssl, int level, |
| 53 | char *file, int line, char *text ) |
| 54 | { |
| 55 | char str[512]; |
| 56 | int maxlen = sizeof( str ) - 1; |
| 57 | |
| 58 | if( ssl->f_dbg == NULL ) |
| 59 | return; |
| 60 | |
| 61 | snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text ); |
| 62 | str[maxlen] = '\0'; |
| 63 | ssl->f_dbg( ssl->p_dbg, level, str ); |
| 64 | } |
| 65 | |
| 66 | void debug_print_ret( ssl_context *ssl, int level, |
| 67 | char *file, int line, char *text, int ret ) |
| 68 | { |
| 69 | char str[512]; |
| 70 | int maxlen = sizeof( str ) - 1; |
| 71 | |
| 72 | if( ssl->f_dbg == NULL ) |
| 73 | return; |
| 74 | |
| 75 | snprintf( str, maxlen, "%s(%04d): %s() returned %d (0x%x)\n", |
| 76 | file, line, text, ret, ret ); |
| 77 | |
| 78 | str[maxlen] = '\0'; |
| 79 | ssl->f_dbg( ssl->p_dbg, level, str ); |
| 80 | } |
| 81 | |
| 82 | void debug_print_buf( ssl_context *ssl, int level, |
| 83 | char *file, int line, char *text, |
| 84 | unsigned char *buf, int len ) |
| 85 | { |
| 86 | char str[512]; |
| 87 | int i, maxlen = sizeof( str ) - 1; |
| 88 | |
| 89 | if( ssl->f_dbg == NULL || len < 0 ) |
| 90 | return; |
| 91 | |
| 92 | snprintf( str, maxlen, "%s(%04d): dumping '%s' (%d bytes)\n", |
| 93 | file, line, text, len ); |
| 94 | |
| 95 | str[maxlen] = '\0'; |
| 96 | ssl->f_dbg( ssl->p_dbg, level, str ); |
| 97 | |
| 98 | for( i = 0; i < len; i++ ) |
| 99 | { |
| 100 | if( i >= 4096 ) |
| 101 | break; |
| 102 | |
| 103 | if( i % 16 == 0 ) |
| 104 | { |
| 105 | if( i > 0 ) |
| 106 | ssl->f_dbg( ssl->p_dbg, level, "\n" ); |
| 107 | |
| 108 | snprintf( str, maxlen, "%s(%04d): %04x: ", file, line, i ); |
| 109 | |
| 110 | str[maxlen] = '\0'; |
| 111 | ssl->f_dbg( ssl->p_dbg, level, str ); |
| 112 | } |
| 113 | |
| 114 | snprintf( str, maxlen, " %02x", (unsigned int) buf[i] ); |
| 115 | |
| 116 | str[maxlen] = '\0'; |
| 117 | ssl->f_dbg( ssl->p_dbg, level, str ); |
| 118 | } |
| 119 | |
| 120 | if( len > 0 ) |
| 121 | ssl->f_dbg( ssl->p_dbg, level, "\n" ); |
| 122 | } |
| 123 | |
| 124 | void debug_print_mpi( ssl_context *ssl, int level, |
| 125 | char *file, int line, char *text, mpi *X ) |
| 126 | { |
| 127 | char str[512]; |
| 128 | int i, j, k, n, maxlen = sizeof( str ) - 1; |
| 129 | |
| 130 | if( ssl->f_dbg == NULL || X == NULL ) |
| 131 | return; |
| 132 | |
| 133 | for( n = X->n - 1; n >= 0; n-- ) |
| 134 | if( X->p[n] != 0 ) |
| 135 | break; |
| 136 | |
| 137 | snprintf( str, maxlen, "%s(%04d): value of '%s' (%d bits) is:\n", |
| 138 | file, line, text, ((n + 1) * sizeof( t_int )) << 3 ); |
| 139 | |
| 140 | str[maxlen] = '\0'; |
| 141 | ssl->f_dbg( ssl->p_dbg, level, str ); |
| 142 | |
| 143 | for( i = n, j = 0; i >= 0; i--, j++ ) |
| 144 | { |
| 145 | if( j % ( 16 / sizeof( t_int ) ) == 0 ) |
| 146 | { |
| 147 | if( j > 0 ) |
| 148 | ssl->f_dbg( ssl->p_dbg, level, "\n" ); |
| 149 | |
| 150 | snprintf( str, maxlen, "%s(%04d): ", file, line ); |
| 151 | |
| 152 | str[maxlen] = '\0'; |
| 153 | ssl->f_dbg( ssl->p_dbg, level, str ); |
| 154 | } |
| 155 | |
| 156 | for( k = sizeof( t_int ) - 1; k >= 0; k-- ) |
| 157 | { |
| 158 | snprintf( str, maxlen, " %02x", (unsigned int) |
| 159 | ( X->p[i] >> (k << 3) ) & 0xFF ); |
| 160 | |
| 161 | str[maxlen] = '\0'; |
| 162 | ssl->f_dbg( ssl->p_dbg, level, str ); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | ssl->f_dbg( ssl->p_dbg, level, "\n" ); |
| 167 | } |
| 168 | |
| 169 | void debug_print_crt( ssl_context *ssl, int level, |
| 170 | char *file, int line, char *text, x509_cert *crt ) |
| 171 | { |
| 172 | char str[512], prefix[64], *p; |
| 173 | int i = 0, maxlen = sizeof( prefix ) - 1; |
| 174 | |
| 175 | if( ssl->f_dbg == NULL || crt == NULL ) |
| 176 | return; |
| 177 | |
| 178 | snprintf( prefix, maxlen, "%s(%04d): ", file, line ); |
| 179 | prefix[maxlen] = '\0'; |
| 180 | maxlen = sizeof( str ) - 1; |
| 181 | |
| 182 | while( crt != NULL && crt->next != NULL ) |
| 183 | { |
| 184 | p = x509parse_cert_info( prefix, crt ); |
| 185 | |
| 186 | snprintf( str, maxlen, "%s(%04d): %s #%d:\n%s", |
| 187 | file, line, text, ++i, p ); |
| 188 | |
| 189 | str[maxlen] = '\0'; |
| 190 | ssl->f_dbg( ssl->p_dbg, level, str ); |
| 191 | |
| 192 | debug_print_mpi( ssl, level, file, line, |
| 193 | "crt->rsa.N", &crt->rsa.N ); |
| 194 | |
| 195 | debug_print_mpi( ssl, level, file, line, |
| 196 | "crt->rsa.E", &crt->rsa.E ); |
| 197 | |
| 198 | crt = crt->next; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | #endif |