Hannes Tschofenig
/
aes-gcm-test-program
Example program to test AES-GCM functionality. Used for a workshop
SSL/library/debug.c@0:796d0f61a05b, 2018-09-27 (annotated)
- Committer:
- HannesTschofenig
- Date:
- Thu Sep 27 06:34:22 2018 +0000
- Revision:
- 0:796d0f61a05b
Example AES-GCM test program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
HannesTschofenig | 0:796d0f61a05b | 1 | /* |
HannesTschofenig | 0:796d0f61a05b | 2 | * Debugging routines |
HannesTschofenig | 0:796d0f61a05b | 3 | * |
HannesTschofenig | 0:796d0f61a05b | 4 | * Copyright (C) 2006-2010, Brainspark B.V. |
HannesTschofenig | 0:796d0f61a05b | 5 | * |
HannesTschofenig | 0:796d0f61a05b | 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
HannesTschofenig | 0:796d0f61a05b | 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
HannesTschofenig | 0:796d0f61a05b | 8 | * |
HannesTschofenig | 0:796d0f61a05b | 9 | * All rights reserved. |
HannesTschofenig | 0:796d0f61a05b | 10 | * |
HannesTschofenig | 0:796d0f61a05b | 11 | * This program is free software; you can redistribute it and/or modify |
HannesTschofenig | 0:796d0f61a05b | 12 | * it under the terms of the GNU General Public License as published by |
HannesTschofenig | 0:796d0f61a05b | 13 | * the Free Software Foundation; either version 2 of the License, or |
HannesTschofenig | 0:796d0f61a05b | 14 | * (at your option) any later version. |
HannesTschofenig | 0:796d0f61a05b | 15 | * |
HannesTschofenig | 0:796d0f61a05b | 16 | * This program is distributed in the hope that it will be useful, |
HannesTschofenig | 0:796d0f61a05b | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
HannesTschofenig | 0:796d0f61a05b | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
HannesTschofenig | 0:796d0f61a05b | 19 | * GNU General Public License for more details. |
HannesTschofenig | 0:796d0f61a05b | 20 | * |
HannesTschofenig | 0:796d0f61a05b | 21 | * You should have received a copy of the GNU General Public License along |
HannesTschofenig | 0:796d0f61a05b | 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
HannesTschofenig | 0:796d0f61a05b | 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
HannesTschofenig | 0:796d0f61a05b | 24 | */ |
HannesTschofenig | 0:796d0f61a05b | 25 | |
HannesTschofenig | 0:796d0f61a05b | 26 | #if !defined(POLARSSL_CONFIG_FILE) |
HannesTschofenig | 0:796d0f61a05b | 27 | #include "polarssl/config.h" |
HannesTschofenig | 0:796d0f61a05b | 28 | #else |
HannesTschofenig | 0:796d0f61a05b | 29 | #include POLARSSL_CONFIG_FILE |
HannesTschofenig | 0:796d0f61a05b | 30 | #endif |
HannesTschofenig | 0:796d0f61a05b | 31 | |
HannesTschofenig | 0:796d0f61a05b | 32 | #if defined(POLARSSL_DEBUG_C) |
HannesTschofenig | 0:796d0f61a05b | 33 | |
HannesTschofenig | 0:796d0f61a05b | 34 | #include "polarssl/debug.h" |
HannesTschofenig | 0:796d0f61a05b | 35 | |
HannesTschofenig | 0:796d0f61a05b | 36 | #include <stdarg.h> |
HannesTschofenig | 0:796d0f61a05b | 37 | #include <stdlib.h> |
HannesTschofenig | 0:796d0f61a05b | 38 | |
HannesTschofenig | 0:796d0f61a05b | 39 | #if defined(EFIX64) || defined(EFI32) |
HannesTschofenig | 0:796d0f61a05b | 40 | #include <stdio.h> |
HannesTschofenig | 0:796d0f61a05b | 41 | #endif |
HannesTschofenig | 0:796d0f61a05b | 42 | |
HannesTschofenig | 0:796d0f61a05b | 43 | #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) |
HannesTschofenig | 0:796d0f61a05b | 44 | #if !defined snprintf |
HannesTschofenig | 0:796d0f61a05b | 45 | #define snprintf _snprintf |
HannesTschofenig | 0:796d0f61a05b | 46 | #endif |
HannesTschofenig | 0:796d0f61a05b | 47 | |
HannesTschofenig | 0:796d0f61a05b | 48 | #if !defined vsnprintf |
HannesTschofenig | 0:796d0f61a05b | 49 | #define vsnprintf _vsnprintf |
HannesTschofenig | 0:796d0f61a05b | 50 | #endif |
HannesTschofenig | 0:796d0f61a05b | 51 | #endif /* _MSC_VER */ |
HannesTschofenig | 0:796d0f61a05b | 52 | |
HannesTschofenig | 0:796d0f61a05b | 53 | static int debug_log_mode = POLARSSL_DEBUG_DFL_MODE; |
HannesTschofenig | 0:796d0f61a05b | 54 | static int debug_threshold = 0; |
HannesTschofenig | 0:796d0f61a05b | 55 | |
HannesTschofenig | 0:796d0f61a05b | 56 | void debug_set_log_mode( int log_mode ) |
HannesTschofenig | 0:796d0f61a05b | 57 | { |
HannesTschofenig | 0:796d0f61a05b | 58 | debug_log_mode = log_mode; |
HannesTschofenig | 0:796d0f61a05b | 59 | } |
HannesTschofenig | 0:796d0f61a05b | 60 | |
HannesTschofenig | 0:796d0f61a05b | 61 | void debug_set_threshold( int threshold ) |
HannesTschofenig | 0:796d0f61a05b | 62 | { |
HannesTschofenig | 0:796d0f61a05b | 63 | debug_threshold = threshold; |
HannesTschofenig | 0:796d0f61a05b | 64 | } |
HannesTschofenig | 0:796d0f61a05b | 65 | |
HannesTschofenig | 0:796d0f61a05b | 66 | char *debug_fmt( const char *format, ... ) |
HannesTschofenig | 0:796d0f61a05b | 67 | { |
HannesTschofenig | 0:796d0f61a05b | 68 | va_list argp; |
HannesTschofenig | 0:796d0f61a05b | 69 | static char str[512]; |
HannesTschofenig | 0:796d0f61a05b | 70 | int maxlen = sizeof( str ) - 1; |
HannesTschofenig | 0:796d0f61a05b | 71 | |
HannesTschofenig | 0:796d0f61a05b | 72 | va_start( argp, format ); |
HannesTschofenig | 0:796d0f61a05b | 73 | vsnprintf( str, maxlen, format, argp ); |
HannesTschofenig | 0:796d0f61a05b | 74 | va_end( argp ); |
HannesTschofenig | 0:796d0f61a05b | 75 | |
HannesTschofenig | 0:796d0f61a05b | 76 | str[maxlen] = '\0'; |
HannesTschofenig | 0:796d0f61a05b | 77 | return( str ); |
HannesTschofenig | 0:796d0f61a05b | 78 | } |
HannesTschofenig | 0:796d0f61a05b | 79 | |
HannesTschofenig | 0:796d0f61a05b | 80 | void debug_print_msg( const ssl_context *ssl, int level, |
HannesTschofenig | 0:796d0f61a05b | 81 | const char *file, int line, const char *text ) |
HannesTschofenig | 0:796d0f61a05b | 82 | { |
HannesTschofenig | 0:796d0f61a05b | 83 | char str[512]; |
HannesTschofenig | 0:796d0f61a05b | 84 | int maxlen = sizeof( str ) - 1; |
HannesTschofenig | 0:796d0f61a05b | 85 | |
HannesTschofenig | 0:796d0f61a05b | 86 | if( ssl->f_dbg == NULL || level > debug_threshold ) |
HannesTschofenig | 0:796d0f61a05b | 87 | return; |
HannesTschofenig | 0:796d0f61a05b | 88 | |
HannesTschofenig | 0:796d0f61a05b | 89 | if( debug_log_mode == POLARSSL_DEBUG_LOG_RAW ) |
HannesTschofenig | 0:796d0f61a05b | 90 | { |
HannesTschofenig | 0:796d0f61a05b | 91 | ssl->f_dbg( ssl->p_dbg, level, str ); |
HannesTschofenig | 0:796d0f61a05b | 92 | return; |
HannesTschofenig | 0:796d0f61a05b | 93 | } |
HannesTschofenig | 0:796d0f61a05b | 94 | |
HannesTschofenig | 0:796d0f61a05b | 95 | snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text ); |
HannesTschofenig | 0:796d0f61a05b | 96 | str[maxlen] = '\0'; |
HannesTschofenig | 0:796d0f61a05b | 97 | ssl->f_dbg( ssl->p_dbg, level, str ); |
HannesTschofenig | 0:796d0f61a05b | 98 | } |
HannesTschofenig | 0:796d0f61a05b | 99 | |
HannesTschofenig | 0:796d0f61a05b | 100 | void debug_print_ret( const ssl_context *ssl, int level, |
HannesTschofenig | 0:796d0f61a05b | 101 | const char *file, int line, |
HannesTschofenig | 0:796d0f61a05b | 102 | const char *text, int ret ) |
HannesTschofenig | 0:796d0f61a05b | 103 | { |
HannesTschofenig | 0:796d0f61a05b | 104 | char str[512]; |
HannesTschofenig | 0:796d0f61a05b | 105 | int maxlen = sizeof( str ) - 1; |
HannesTschofenig | 0:796d0f61a05b | 106 | size_t idx = 0; |
HannesTschofenig | 0:796d0f61a05b | 107 | |
HannesTschofenig | 0:796d0f61a05b | 108 | if( ssl->f_dbg == NULL || level > debug_threshold ) |
HannesTschofenig | 0:796d0f61a05b | 109 | return; |
HannesTschofenig | 0:796d0f61a05b | 110 | |
HannesTschofenig | 0:796d0f61a05b | 111 | if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) |
HannesTschofenig | 0:796d0f61a05b | 112 | idx = snprintf( str, maxlen, "%s(%04d): ", file, line ); |
HannesTschofenig | 0:796d0f61a05b | 113 | |
HannesTschofenig | 0:796d0f61a05b | 114 | snprintf( str + idx, maxlen - idx, "%s() returned %d (-0x%04x)\n", |
HannesTschofenig | 0:796d0f61a05b | 115 | text, ret, -ret ); |
HannesTschofenig | 0:796d0f61a05b | 116 | |
HannesTschofenig | 0:796d0f61a05b | 117 | str[maxlen] = '\0'; |
HannesTschofenig | 0:796d0f61a05b | 118 | ssl->f_dbg( ssl->p_dbg, level, str ); |
HannesTschofenig | 0:796d0f61a05b | 119 | } |
HannesTschofenig | 0:796d0f61a05b | 120 | |
HannesTschofenig | 0:796d0f61a05b | 121 | void debug_print_buf( const ssl_context *ssl, int level, |
HannesTschofenig | 0:796d0f61a05b | 122 | const char *file, int line, const char *text, |
HannesTschofenig | 0:796d0f61a05b | 123 | unsigned char *buf, size_t len ) |
HannesTschofenig | 0:796d0f61a05b | 124 | { |
HannesTschofenig | 0:796d0f61a05b | 125 | char str[512]; |
HannesTschofenig | 0:796d0f61a05b | 126 | size_t i, maxlen = sizeof( str ) - 1, idx = 0; |
HannesTschofenig | 0:796d0f61a05b | 127 | |
HannesTschofenig | 0:796d0f61a05b | 128 | if( ssl->f_dbg == NULL || level > debug_threshold ) |
HannesTschofenig | 0:796d0f61a05b | 129 | return; |
HannesTschofenig | 0:796d0f61a05b | 130 | |
HannesTschofenig | 0:796d0f61a05b | 131 | if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) |
HannesTschofenig | 0:796d0f61a05b | 132 | idx = snprintf( str, maxlen, "%s(%04d): ", file, line ); |
HannesTschofenig | 0:796d0f61a05b | 133 | |
HannesTschofenig | 0:796d0f61a05b | 134 | snprintf( str + idx, maxlen - idx, "dumping '%s' (%d bytes)\n", |
HannesTschofenig | 0:796d0f61a05b | 135 | text, (unsigned int) len ); |
HannesTschofenig | 0:796d0f61a05b | 136 | |
HannesTschofenig | 0:796d0f61a05b | 137 | str[maxlen] = '\0'; |
HannesTschofenig | 0:796d0f61a05b | 138 | ssl->f_dbg( ssl->p_dbg, level, str ); |
HannesTschofenig | 0:796d0f61a05b | 139 | |
HannesTschofenig | 0:796d0f61a05b | 140 | idx = 0; |
HannesTschofenig | 0:796d0f61a05b | 141 | for( i = 0; i < len; i++ ) |
HannesTschofenig | 0:796d0f61a05b | 142 | { |
HannesTschofenig | 0:796d0f61a05b | 143 | if( i >= 4096 ) |
HannesTschofenig | 0:796d0f61a05b | 144 | break; |
HannesTschofenig | 0:796d0f61a05b | 145 | |
HannesTschofenig | 0:796d0f61a05b | 146 | if( i % 16 == 0 ) |
HannesTschofenig | 0:796d0f61a05b | 147 | { |
HannesTschofenig | 0:796d0f61a05b | 148 | if( i > 0 ) |
HannesTschofenig | 0:796d0f61a05b | 149 | { |
HannesTschofenig | 0:796d0f61a05b | 150 | snprintf( str + idx, maxlen - idx, "\n" ); |
HannesTschofenig | 0:796d0f61a05b | 151 | ssl->f_dbg( ssl->p_dbg, level, str ); |
HannesTschofenig | 0:796d0f61a05b | 152 | idx = 0; |
HannesTschofenig | 0:796d0f61a05b | 153 | } |
HannesTschofenig | 0:796d0f61a05b | 154 | |
HannesTschofenig | 0:796d0f61a05b | 155 | if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) |
HannesTschofenig | 0:796d0f61a05b | 156 | idx = snprintf( str, maxlen, "%s(%04d): ", file, line ); |
HannesTschofenig | 0:796d0f61a05b | 157 | |
HannesTschofenig | 0:796d0f61a05b | 158 | idx += snprintf( str + idx, maxlen - idx, "%04x: ", |
HannesTschofenig | 0:796d0f61a05b | 159 | (unsigned int) i ); |
HannesTschofenig | 0:796d0f61a05b | 160 | |
HannesTschofenig | 0:796d0f61a05b | 161 | } |
HannesTschofenig | 0:796d0f61a05b | 162 | |
HannesTschofenig | 0:796d0f61a05b | 163 | idx += snprintf( str + idx, maxlen - idx, " %02x", |
HannesTschofenig | 0:796d0f61a05b | 164 | (unsigned int) buf[i] ); |
HannesTschofenig | 0:796d0f61a05b | 165 | } |
HannesTschofenig | 0:796d0f61a05b | 166 | |
HannesTschofenig | 0:796d0f61a05b | 167 | if( len > 0 ) |
HannesTschofenig | 0:796d0f61a05b | 168 | { |
HannesTschofenig | 0:796d0f61a05b | 169 | snprintf( str + idx, maxlen - idx, "\n" ); |
HannesTschofenig | 0:796d0f61a05b | 170 | ssl->f_dbg( ssl->p_dbg, level, str ); |
HannesTschofenig | 0:796d0f61a05b | 171 | } |
HannesTschofenig | 0:796d0f61a05b | 172 | } |
HannesTschofenig | 0:796d0f61a05b | 173 | |
HannesTschofenig | 0:796d0f61a05b | 174 | #if defined(POLARSSL_ECP_C) |
HannesTschofenig | 0:796d0f61a05b | 175 | void debug_print_ecp( const ssl_context *ssl, int level, |
HannesTschofenig | 0:796d0f61a05b | 176 | const char *file, int line, |
HannesTschofenig | 0:796d0f61a05b | 177 | const char *text, const ecp_point *X ) |
HannesTschofenig | 0:796d0f61a05b | 178 | { |
HannesTschofenig | 0:796d0f61a05b | 179 | char str[512]; |
HannesTschofenig | 0:796d0f61a05b | 180 | int maxlen = sizeof( str ) - 1; |
HannesTschofenig | 0:796d0f61a05b | 181 | |
HannesTschofenig | 0:796d0f61a05b | 182 | if( ssl->f_dbg == NULL || level > debug_threshold ) |
HannesTschofenig | 0:796d0f61a05b | 183 | return; |
HannesTschofenig | 0:796d0f61a05b | 184 | |
HannesTschofenig | 0:796d0f61a05b | 185 | snprintf( str, maxlen, "%s(X)", text ); |
HannesTschofenig | 0:796d0f61a05b | 186 | str[maxlen] = '\0'; |
HannesTschofenig | 0:796d0f61a05b | 187 | debug_print_mpi( ssl, level, file, line, str, &X->X ); |
HannesTschofenig | 0:796d0f61a05b | 188 | |
HannesTschofenig | 0:796d0f61a05b | 189 | snprintf( str, maxlen, "%s(Y)", text ); |
HannesTschofenig | 0:796d0f61a05b | 190 | str[maxlen] = '\0'; |
HannesTschofenig | 0:796d0f61a05b | 191 | debug_print_mpi( ssl, level, file, line, str, &X->Y ); |
HannesTschofenig | 0:796d0f61a05b | 192 | } |
HannesTschofenig | 0:796d0f61a05b | 193 | #endif /* POLARSSL_ECP_C */ |
HannesTschofenig | 0:796d0f61a05b | 194 | |
HannesTschofenig | 0:796d0f61a05b | 195 | #if defined(POLARSSL_BIGNUM_C) |
HannesTschofenig | 0:796d0f61a05b | 196 | void debug_print_mpi( const ssl_context *ssl, int level, |
HannesTschofenig | 0:796d0f61a05b | 197 | const char *file, int line, |
HannesTschofenig | 0:796d0f61a05b | 198 | const char *text, const mpi *X ) |
HannesTschofenig | 0:796d0f61a05b | 199 | { |
HannesTschofenig | 0:796d0f61a05b | 200 | char str[512]; |
HannesTschofenig | 0:796d0f61a05b | 201 | int j, k, maxlen = sizeof( str ) - 1, zeros = 1; |
HannesTschofenig | 0:796d0f61a05b | 202 | size_t i, n, idx = 0; |
HannesTschofenig | 0:796d0f61a05b | 203 | |
HannesTschofenig | 0:796d0f61a05b | 204 | if( ssl->f_dbg == NULL || X == NULL || level > debug_threshold ) |
HannesTschofenig | 0:796d0f61a05b | 205 | return; |
HannesTschofenig | 0:796d0f61a05b | 206 | |
HannesTschofenig | 0:796d0f61a05b | 207 | for( n = X->n - 1; n > 0; n-- ) |
HannesTschofenig | 0:796d0f61a05b | 208 | if( X->p[n] != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 209 | break; |
HannesTschofenig | 0:796d0f61a05b | 210 | |
HannesTschofenig | 0:796d0f61a05b | 211 | for( j = ( sizeof(t_uint) << 3 ) - 1; j >= 0; j-- ) |
HannesTschofenig | 0:796d0f61a05b | 212 | if( ( ( X->p[n] >> j ) & 1 ) != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 213 | break; |
HannesTschofenig | 0:796d0f61a05b | 214 | |
HannesTschofenig | 0:796d0f61a05b | 215 | if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) |
HannesTschofenig | 0:796d0f61a05b | 216 | idx = snprintf( str, maxlen, "%s(%04d): ", file, line ); |
HannesTschofenig | 0:796d0f61a05b | 217 | |
HannesTschofenig | 0:796d0f61a05b | 218 | snprintf( str + idx, maxlen - idx, "value of '%s' (%d bits) is:\n", |
HannesTschofenig | 0:796d0f61a05b | 219 | text, (int) ( ( n * ( sizeof(t_uint) << 3 ) ) + j + 1 ) ); |
HannesTschofenig | 0:796d0f61a05b | 220 | |
HannesTschofenig | 0:796d0f61a05b | 221 | str[maxlen] = '\0'; |
HannesTschofenig | 0:796d0f61a05b | 222 | ssl->f_dbg( ssl->p_dbg, level, str ); |
HannesTschofenig | 0:796d0f61a05b | 223 | |
HannesTschofenig | 0:796d0f61a05b | 224 | idx = 0; |
HannesTschofenig | 0:796d0f61a05b | 225 | for( i = n + 1, j = 0; i > 0; i-- ) |
HannesTschofenig | 0:796d0f61a05b | 226 | { |
HannesTschofenig | 0:796d0f61a05b | 227 | if( zeros && X->p[i - 1] == 0 ) |
HannesTschofenig | 0:796d0f61a05b | 228 | continue; |
HannesTschofenig | 0:796d0f61a05b | 229 | |
HannesTschofenig | 0:796d0f61a05b | 230 | for( k = sizeof( t_uint ) - 1; k >= 0; k-- ) |
HannesTschofenig | 0:796d0f61a05b | 231 | { |
HannesTschofenig | 0:796d0f61a05b | 232 | if( zeros && ( ( X->p[i - 1] >> (k << 3) ) & 0xFF ) == 0 ) |
HannesTschofenig | 0:796d0f61a05b | 233 | continue; |
HannesTschofenig | 0:796d0f61a05b | 234 | else |
HannesTschofenig | 0:796d0f61a05b | 235 | zeros = 0; |
HannesTschofenig | 0:796d0f61a05b | 236 | |
HannesTschofenig | 0:796d0f61a05b | 237 | if( j % 16 == 0 ) |
HannesTschofenig | 0:796d0f61a05b | 238 | { |
HannesTschofenig | 0:796d0f61a05b | 239 | if( j > 0 ) |
HannesTschofenig | 0:796d0f61a05b | 240 | { |
HannesTschofenig | 0:796d0f61a05b | 241 | snprintf( str + idx, maxlen - idx, "\n" ); |
HannesTschofenig | 0:796d0f61a05b | 242 | ssl->f_dbg( ssl->p_dbg, level, str ); |
HannesTschofenig | 0:796d0f61a05b | 243 | idx = 0; |
HannesTschofenig | 0:796d0f61a05b | 244 | } |
HannesTschofenig | 0:796d0f61a05b | 245 | |
HannesTschofenig | 0:796d0f61a05b | 246 | if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) |
HannesTschofenig | 0:796d0f61a05b | 247 | idx = snprintf( str, maxlen, "%s(%04d): ", file, line ); |
HannesTschofenig | 0:796d0f61a05b | 248 | } |
HannesTschofenig | 0:796d0f61a05b | 249 | |
HannesTschofenig | 0:796d0f61a05b | 250 | idx += snprintf( str + idx, maxlen - idx, " %02x", (unsigned int) |
HannesTschofenig | 0:796d0f61a05b | 251 | ( X->p[i - 1] >> (k << 3) ) & 0xFF ); |
HannesTschofenig | 0:796d0f61a05b | 252 | |
HannesTschofenig | 0:796d0f61a05b | 253 | j++; |
HannesTschofenig | 0:796d0f61a05b | 254 | } |
HannesTschofenig | 0:796d0f61a05b | 255 | |
HannesTschofenig | 0:796d0f61a05b | 256 | } |
HannesTschofenig | 0:796d0f61a05b | 257 | |
HannesTschofenig | 0:796d0f61a05b | 258 | if( zeros == 1 ) |
HannesTschofenig | 0:796d0f61a05b | 259 | { |
HannesTschofenig | 0:796d0f61a05b | 260 | if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) |
HannesTschofenig | 0:796d0f61a05b | 261 | { |
HannesTschofenig | 0:796d0f61a05b | 262 | idx = snprintf( str, maxlen, "%s(%04d): ", file, line ); |
HannesTschofenig | 0:796d0f61a05b | 263 | |
HannesTschofenig | 0:796d0f61a05b | 264 | } |
HannesTschofenig | 0:796d0f61a05b | 265 | idx += snprintf( str + idx, maxlen - idx, " 00" ); |
HannesTschofenig | 0:796d0f61a05b | 266 | } |
HannesTschofenig | 0:796d0f61a05b | 267 | |
HannesTschofenig | 0:796d0f61a05b | 268 | snprintf( str + idx, maxlen - idx, "\n" ); |
HannesTschofenig | 0:796d0f61a05b | 269 | ssl->f_dbg( ssl->p_dbg, level, str ); |
HannesTschofenig | 0:796d0f61a05b | 270 | } |
HannesTschofenig | 0:796d0f61a05b | 271 | #endif /* POLARSSL_BIGNUM_C */ |
HannesTschofenig | 0:796d0f61a05b | 272 | |
HannesTschofenig | 0:796d0f61a05b | 273 | #if defined(POLARSSL_X509_CRT_PARSE_C) |
HannesTschofenig | 0:796d0f61a05b | 274 | static void debug_print_pk( const ssl_context *ssl, int level, |
HannesTschofenig | 0:796d0f61a05b | 275 | const char *file, int line, |
HannesTschofenig | 0:796d0f61a05b | 276 | const char *text, const pk_context *pk ) |
HannesTschofenig | 0:796d0f61a05b | 277 | { |
HannesTschofenig | 0:796d0f61a05b | 278 | size_t i; |
HannesTschofenig | 0:796d0f61a05b | 279 | pk_debug_item items[POLARSSL_PK_DEBUG_MAX_ITEMS]; |
HannesTschofenig | 0:796d0f61a05b | 280 | char name[16]; |
HannesTschofenig | 0:796d0f61a05b | 281 | |
HannesTschofenig | 0:796d0f61a05b | 282 | memset( items, 0, sizeof( items ) ); |
HannesTschofenig | 0:796d0f61a05b | 283 | |
HannesTschofenig | 0:796d0f61a05b | 284 | if( pk_debug( pk, items ) != 0 ) |
HannesTschofenig | 0:796d0f61a05b | 285 | { |
HannesTschofenig | 0:796d0f61a05b | 286 | debug_print_msg( ssl, level, file, line, "invalid PK context" ); |
HannesTschofenig | 0:796d0f61a05b | 287 | return; |
HannesTschofenig | 0:796d0f61a05b | 288 | } |
HannesTschofenig | 0:796d0f61a05b | 289 | |
HannesTschofenig | 0:796d0f61a05b | 290 | for( i = 0; i < POLARSSL_PK_DEBUG_MAX_ITEMS; i++ ) |
HannesTschofenig | 0:796d0f61a05b | 291 | { |
HannesTschofenig | 0:796d0f61a05b | 292 | if( items[i].type == POLARSSL_PK_DEBUG_NONE ) |
HannesTschofenig | 0:796d0f61a05b | 293 | return; |
HannesTschofenig | 0:796d0f61a05b | 294 | |
HannesTschofenig | 0:796d0f61a05b | 295 | snprintf( name, sizeof( name ), "%s%s", text, items[i].name ); |
HannesTschofenig | 0:796d0f61a05b | 296 | name[sizeof( name ) - 1] = '\0'; |
HannesTschofenig | 0:796d0f61a05b | 297 | |
HannesTschofenig | 0:796d0f61a05b | 298 | if( items[i].type == POLARSSL_PK_DEBUG_MPI ) |
HannesTschofenig | 0:796d0f61a05b | 299 | debug_print_mpi( ssl, level, file, line, name, items[i].value ); |
HannesTschofenig | 0:796d0f61a05b | 300 | else |
HannesTschofenig | 0:796d0f61a05b | 301 | #if defined(POLARSSL_ECP_C) |
HannesTschofenig | 0:796d0f61a05b | 302 | if( items[i].type == POLARSSL_PK_DEBUG_ECP ) |
HannesTschofenig | 0:796d0f61a05b | 303 | debug_print_ecp( ssl, level, file, line, name, items[i].value ); |
HannesTschofenig | 0:796d0f61a05b | 304 | else |
HannesTschofenig | 0:796d0f61a05b | 305 | #endif |
HannesTschofenig | 0:796d0f61a05b | 306 | debug_print_msg( ssl, level, file, line, "should not happen" ); |
HannesTschofenig | 0:796d0f61a05b | 307 | } |
HannesTschofenig | 0:796d0f61a05b | 308 | } |
HannesTschofenig | 0:796d0f61a05b | 309 | |
HannesTschofenig | 0:796d0f61a05b | 310 | void debug_print_crt( const ssl_context *ssl, int level, |
HannesTschofenig | 0:796d0f61a05b | 311 | const char *file, int line, |
HannesTschofenig | 0:796d0f61a05b | 312 | const char *text, const x509_crt *crt ) |
HannesTschofenig | 0:796d0f61a05b | 313 | { |
HannesTschofenig | 0:796d0f61a05b | 314 | char str[1024], prefix[64]; |
HannesTschofenig | 0:796d0f61a05b | 315 | int i = 0, maxlen = sizeof( prefix ) - 1, idx = 0; |
HannesTschofenig | 0:796d0f61a05b | 316 | |
HannesTschofenig | 0:796d0f61a05b | 317 | if( ssl->f_dbg == NULL || crt == NULL || level > debug_threshold ) |
HannesTschofenig | 0:796d0f61a05b | 318 | return; |
HannesTschofenig | 0:796d0f61a05b | 319 | |
HannesTschofenig | 0:796d0f61a05b | 320 | if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) |
HannesTschofenig | 0:796d0f61a05b | 321 | { |
HannesTschofenig | 0:796d0f61a05b | 322 | snprintf( prefix, maxlen, "%s(%04d): ", file, line ); |
HannesTschofenig | 0:796d0f61a05b | 323 | prefix[maxlen] = '\0'; |
HannesTschofenig | 0:796d0f61a05b | 324 | } |
HannesTschofenig | 0:796d0f61a05b | 325 | else |
HannesTschofenig | 0:796d0f61a05b | 326 | prefix[0] = '\0'; |
HannesTschofenig | 0:796d0f61a05b | 327 | |
HannesTschofenig | 0:796d0f61a05b | 328 | maxlen = sizeof( str ) - 1; |
HannesTschofenig | 0:796d0f61a05b | 329 | |
HannesTschofenig | 0:796d0f61a05b | 330 | while( crt != NULL ) |
HannesTschofenig | 0:796d0f61a05b | 331 | { |
HannesTschofenig | 0:796d0f61a05b | 332 | char buf[1024]; |
HannesTschofenig | 0:796d0f61a05b | 333 | x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt ); |
HannesTschofenig | 0:796d0f61a05b | 334 | |
HannesTschofenig | 0:796d0f61a05b | 335 | if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL ) |
HannesTschofenig | 0:796d0f61a05b | 336 | idx = snprintf( str, maxlen, "%s(%04d): ", file, line ); |
HannesTschofenig | 0:796d0f61a05b | 337 | |
HannesTschofenig | 0:796d0f61a05b | 338 | snprintf( str + idx, maxlen - idx, "%s #%d:\n%s", |
HannesTschofenig | 0:796d0f61a05b | 339 | text, ++i, buf ); |
HannesTschofenig | 0:796d0f61a05b | 340 | |
HannesTschofenig | 0:796d0f61a05b | 341 | str[maxlen] = '\0'; |
HannesTschofenig | 0:796d0f61a05b | 342 | ssl->f_dbg( ssl->p_dbg, level, str ); |
HannesTschofenig | 0:796d0f61a05b | 343 | |
HannesTschofenig | 0:796d0f61a05b | 344 | debug_print_pk( ssl, level, file, line, "crt->", &crt->pk ); |
HannesTschofenig | 0:796d0f61a05b | 345 | |
HannesTschofenig | 0:796d0f61a05b | 346 | crt = crt->next; |
HannesTschofenig | 0:796d0f61a05b | 347 | } |
HannesTschofenig | 0:796d0f61a05b | 348 | } |
HannesTschofenig | 0:796d0f61a05b | 349 | #endif /* POLARSSL_X509_CRT_PARSE_C */ |
HannesTschofenig | 0:796d0f61a05b | 350 | |
HannesTschofenig | 0:796d0f61a05b | 351 | #endif /* POLARSSL_DEBUG_C */ |
HannesTschofenig | 0:796d0f61a05b | 352 | |
HannesTschofenig | 0:796d0f61a05b | 353 |