Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Debugging routines |
| 3 | * |
Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2010, Brainspark B.V. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * |
| 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 8 | * |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 9 | * All rights reserved. |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 10 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
| 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | */ |
| 25 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 26 | #include "polarssl/config.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 27 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 28 | #if defined(POLARSSL_DEBUG_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 29 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 30 | #include "polarssl/debug.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 31 | |
| 32 | #include <stdarg.h> |
| 33 | #include <stdlib.h> |
| 34 | |
| 35 | #if defined _MSC_VER && !defined snprintf |
| 36 | #define snprintf _snprintf |
| 37 | #endif |
| 38 | |
| 39 | #if defined _MSC_VER && !defined vsnprintf |
| 40 | #define vsnprintf _vsnprintf |
| 41 | #endif |
| 42 | |
| 43 | char *debug_fmt( const char *format, ... ) |
| 44 | { |
| 45 | va_list argp; |
| 46 | static char str[512]; |
| 47 | int maxlen = sizeof( str ) - 1; |
| 48 | |
| 49 | va_start( argp, format ); |
| 50 | vsnprintf( str, maxlen, format, argp ); |
| 51 | va_end( argp ); |
| 52 | |
| 53 | str[maxlen] = '\0'; |
| 54 | return( str ); |
| 55 | } |
| 56 | |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 57 | void debug_print_msg( const ssl_context *ssl, int level, |
| 58 | const char *file, int line, const char *text ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 59 | { |
| 60 | char str[512]; |
| 61 | int maxlen = sizeof( str ) - 1; |
| 62 | |
| 63 | if( ssl->f_dbg == NULL ) |
| 64 | return; |
| 65 | |
| 66 | snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text ); |
| 67 | str[maxlen] = '\0'; |
| 68 | ssl->f_dbg( ssl->p_dbg, level, str ); |
| 69 | } |
| 70 | |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 71 | void debug_print_ret( const ssl_context *ssl, int level, |
| 72 | const char *file, int line, |
| 73 | const char *text, int ret ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 74 | { |
| 75 | char str[512]; |
| 76 | int maxlen = sizeof( str ) - 1; |
| 77 | |
| 78 | if( ssl->f_dbg == NULL ) |
| 79 | return; |
| 80 | |
| 81 | snprintf( str, maxlen, "%s(%04d): %s() returned %d (0x%x)\n", |
| 82 | file, line, text, ret, ret ); |
| 83 | |
| 84 | str[maxlen] = '\0'; |
| 85 | ssl->f_dbg( ssl->p_dbg, level, str ); |
| 86 | } |
| 87 | |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 88 | void debug_print_buf( const ssl_context *ssl, int level, |
| 89 | const char *file, int line, const char *text, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 90 | unsigned char *buf, size_t len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 91 | { |
| 92 | char str[512]; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 93 | size_t i, maxlen = sizeof( str ) - 1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 94 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 95 | if( ssl->f_dbg == NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 96 | return; |
| 97 | |
| 98 | snprintf( str, maxlen, "%s(%04d): dumping '%s' (%d bytes)\n", |
Paul Bakker | f4f6968 | 2011-04-24 16:08:12 +0000 | [diff] [blame] | 99 | file, line, text, (unsigned int) len ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 100 | |
| 101 | str[maxlen] = '\0'; |
| 102 | ssl->f_dbg( ssl->p_dbg, level, str ); |
| 103 | |
| 104 | for( i = 0; i < len; i++ ) |
| 105 | { |
| 106 | if( i >= 4096 ) |
| 107 | break; |
| 108 | |
| 109 | if( i % 16 == 0 ) |
| 110 | { |
| 111 | if( i > 0 ) |
| 112 | ssl->f_dbg( ssl->p_dbg, level, "\n" ); |
| 113 | |
Paul Bakker | f4f6968 | 2011-04-24 16:08:12 +0000 | [diff] [blame] | 114 | snprintf( str, maxlen, "%s(%04d): %04x: ", file, line, |
| 115 | (unsigned int) i ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 116 | |
| 117 | str[maxlen] = '\0'; |
| 118 | ssl->f_dbg( ssl->p_dbg, level, str ); |
| 119 | } |
| 120 | |
| 121 | snprintf( str, maxlen, " %02x", (unsigned int) buf[i] ); |
| 122 | |
| 123 | str[maxlen] = '\0'; |
| 124 | ssl->f_dbg( ssl->p_dbg, level, str ); |
| 125 | } |
| 126 | |
| 127 | if( len > 0 ) |
| 128 | ssl->f_dbg( ssl->p_dbg, level, "\n" ); |
| 129 | } |
| 130 | |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 131 | #if defined(POLARSSL_ECP_C) |
| 132 | void debug_print_ecp( const ssl_context *ssl, int level, |
| 133 | const char *file, int line, |
| 134 | const char *text, const ecp_point *X ) |
| 135 | { |
| 136 | char str[512]; |
| 137 | int maxlen = sizeof( str ) - 1; |
| 138 | |
| 139 | snprintf( str, maxlen, "%s(X)", text ); |
| 140 | str[maxlen] = '\0'; |
| 141 | debug_print_mpi( ssl, level, file, line, str, &X->X ); |
| 142 | |
| 143 | snprintf( str, maxlen, "%s(Y)", text ); |
| 144 | str[maxlen] = '\0'; |
| 145 | debug_print_mpi( ssl, level, file, line, str, &X->Y ); |
| 146 | |
| 147 | snprintf( str, maxlen, "%s(Z)", text ); |
| 148 | str[maxlen] = '\0'; |
| 149 | debug_print_mpi( ssl, level, file, line, str, &X->Z ); |
| 150 | } |
| 151 | #endif /* POLARSSL_ECP_C */ |
| 152 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 153 | #if defined(POLARSSL_BIGNUM_C) |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 154 | void debug_print_mpi( const ssl_context *ssl, int level, |
| 155 | const char *file, int line, |
| 156 | const char *text, const mpi *X ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 157 | { |
| 158 | char str[512]; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 159 | int j, k, maxlen = sizeof( str ) - 1, zeros = 1; |
| 160 | size_t i, n; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 161 | |
| 162 | if( ssl->f_dbg == NULL || X == NULL ) |
| 163 | return; |
| 164 | |
Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 165 | for( n = X->n - 1; n > 0; n-- ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 166 | if( X->p[n] != 0 ) |
| 167 | break; |
| 168 | |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 169 | for( j = ( sizeof(t_uint) << 3 ) - 1; j >= 0; j-- ) |
Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 170 | if( ( ( X->p[n] >> j ) & 1 ) != 0 ) |
| 171 | break; |
| 172 | |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 173 | snprintf( str, maxlen, "%s(%04d): value of '%s' (%d bits) is:\n", |
Paul Bakker | 4ed999c | 2010-03-16 21:16:16 +0000 | [diff] [blame] | 174 | file, line, text, |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 175 | (int) ( ( n * ( sizeof(t_uint) << 3 ) ) + j + 1 ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 176 | |
| 177 | str[maxlen] = '\0'; |
| 178 | ssl->f_dbg( ssl->p_dbg, level, str ); |
| 179 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 180 | for( i = n + 1, j = 0; i > 0; i-- ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 181 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 182 | if( zeros && X->p[i - 1] == 0 ) |
Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 183 | continue; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 184 | |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 185 | for( k = sizeof( t_uint ) - 1; k >= 0; k-- ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 186 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 187 | if( zeros && ( ( X->p[i - 1] >> (k << 3) ) & 0xFF ) == 0 ) |
Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 188 | continue; |
| 189 | else |
| 190 | zeros = 0; |
| 191 | |
| 192 | if( j % 16 == 0 ) |
| 193 | { |
| 194 | if( j > 0 ) |
| 195 | ssl->f_dbg( ssl->p_dbg, level, "\n" ); |
| 196 | |
| 197 | snprintf( str, maxlen, "%s(%04d): ", file, line ); |
| 198 | |
| 199 | str[maxlen] = '\0'; |
| 200 | ssl->f_dbg( ssl->p_dbg, level, str ); |
| 201 | } |
| 202 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 203 | snprintf( str, maxlen, " %02x", (unsigned int) |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 204 | ( X->p[i - 1] >> (k << 3) ) & 0xFF ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 205 | |
| 206 | str[maxlen] = '\0'; |
| 207 | ssl->f_dbg( ssl->p_dbg, level, str ); |
Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 208 | |
| 209 | j++; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 210 | } |
Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 211 | |
| 212 | } |
| 213 | |
| 214 | if( zeros == 1 ) |
| 215 | { |
| 216 | snprintf( str, maxlen, "%s(%04d): ", file, line ); |
| 217 | |
| 218 | str[maxlen] = '\0'; |
| 219 | ssl->f_dbg( ssl->p_dbg, level, str ); |
| 220 | ssl->f_dbg( ssl->p_dbg, level, " 00" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | ssl->f_dbg( ssl->p_dbg, level, "\n" ); |
| 224 | } |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 225 | #endif /* POLARSSL_BIGNUM_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 226 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 227 | #if defined(POLARSSL_X509_PARSE_C) |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 228 | void debug_print_crt( const ssl_context *ssl, int level, |
| 229 | const char *file, int line, |
| 230 | const char *text, const x509_cert *crt ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 231 | { |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 232 | char str[1024], prefix[64]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 233 | int i = 0, maxlen = sizeof( prefix ) - 1; |
| 234 | |
| 235 | if( ssl->f_dbg == NULL || crt == NULL ) |
| 236 | return; |
| 237 | |
| 238 | snprintf( prefix, maxlen, "%s(%04d): ", file, line ); |
| 239 | prefix[maxlen] = '\0'; |
| 240 | maxlen = sizeof( str ) - 1; |
| 241 | |
Paul Bakker | 2908713 | 2010-03-21 21:03:34 +0000 | [diff] [blame] | 242 | while( crt != NULL ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 243 | { |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 244 | char buf[1024]; |
| 245 | x509parse_cert_info( buf, sizeof( buf ) - 1, prefix, crt ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 246 | |
| 247 | snprintf( str, maxlen, "%s(%04d): %s #%d:\n%s", |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 248 | file, line, text, ++i, buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 249 | |
| 250 | str[maxlen] = '\0'; |
| 251 | ssl->f_dbg( ssl->p_dbg, level, str ); |
| 252 | |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 253 | #if defined(POLARSSL_RSA_C) |
| 254 | if( crt->pk.type == POLARSSL_PK_RSA ) |
Manuel Pégourié-Gonnard | 893879a | 2013-07-11 10:31:57 +0200 | [diff] [blame] | 255 | { |
Manuel Pégourié-Gonnard | fd5164e | 2013-07-11 16:39:05 +0200 | [diff] [blame] | 256 | debug_print_mpi( ssl, level, file, line, |
| 257 | "crt->rsa.N", &pk_rsa( crt->pk )->N ); |
| 258 | debug_print_mpi( ssl, level, file, line, |
| 259 | "crt->rsa.E", &pk_rsa( crt->pk )->E ); |
| 260 | } else |
| 261 | #endif /* POLARSSL_RSA_C */ |
| 262 | #if defined(POLARSSL_ECP_C) |
| 263 | if( crt->pk.type == POLARSSL_PK_ECKEY || |
| 264 | crt->pk.type == POLARSSL_PK_ECKEY_DH ) |
| 265 | { |
| 266 | debug_print_ecp( ssl, level, file, line, |
| 267 | "crt->eckey.Q", &pk_ec( crt->pk )->Q ); |
| 268 | } else |
| 269 | #endif /* POLARSSL_ECP_C */ |
| 270 | debug_print_msg( ssl, level, file, line, |
| 271 | "crt->pk.type is not valid" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 272 | |
| 273 | crt = crt->next; |
| 274 | } |
| 275 | } |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 276 | #endif /* POLARSSL_X509_PARSE_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 277 | |
| 278 | #endif |