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