Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Simon Cooksey 0:fb7af294d5d9 1 /*
Simon Cooksey 0:fb7af294d5d9 2 * Debugging routines
Simon Cooksey 0:fb7af294d5d9 3 *
Simon Cooksey 0:fb7af294d5d9 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Simon Cooksey 0:fb7af294d5d9 5 * SPDX-License-Identifier: Apache-2.0
Simon Cooksey 0:fb7af294d5d9 6 *
Simon Cooksey 0:fb7af294d5d9 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
Simon Cooksey 0:fb7af294d5d9 8 * not use this file except in compliance with the License.
Simon Cooksey 0:fb7af294d5d9 9 * You may obtain a copy of the License at
Simon Cooksey 0:fb7af294d5d9 10 *
Simon Cooksey 0:fb7af294d5d9 11 * http://www.apache.org/licenses/LICENSE-2.0
Simon Cooksey 0:fb7af294d5d9 12 *
Simon Cooksey 0:fb7af294d5d9 13 * Unless required by applicable law or agreed to in writing, software
Simon Cooksey 0:fb7af294d5d9 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
Simon Cooksey 0:fb7af294d5d9 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Simon Cooksey 0:fb7af294d5d9 16 * See the License for the specific language governing permissions and
Simon Cooksey 0:fb7af294d5d9 17 * limitations under the License.
Simon Cooksey 0:fb7af294d5d9 18 *
Simon Cooksey 0:fb7af294d5d9 19 * This file is part of mbed TLS (https://tls.mbed.org)
Simon Cooksey 0:fb7af294d5d9 20 */
Simon Cooksey 0:fb7af294d5d9 21
Simon Cooksey 0:fb7af294d5d9 22 #if !defined(MBEDTLS_CONFIG_FILE)
Simon Cooksey 0:fb7af294d5d9 23 #include "mbedtls/config.h"
Simon Cooksey 0:fb7af294d5d9 24 #else
Simon Cooksey 0:fb7af294d5d9 25 #include MBEDTLS_CONFIG_FILE
Simon Cooksey 0:fb7af294d5d9 26 #endif
Simon Cooksey 0:fb7af294d5d9 27
Simon Cooksey 0:fb7af294d5d9 28 #if defined(MBEDTLS_DEBUG_C)
Simon Cooksey 0:fb7af294d5d9 29
Simon Cooksey 0:fb7af294d5d9 30 #if defined(MBEDTLS_PLATFORM_C)
Simon Cooksey 0:fb7af294d5d9 31 #include "mbedtls/platform.h"
Simon Cooksey 0:fb7af294d5d9 32 #else
Simon Cooksey 0:fb7af294d5d9 33 #include <stdlib.h>
Simon Cooksey 0:fb7af294d5d9 34 #define mbedtls_calloc calloc
Simon Cooksey 0:fb7af294d5d9 35 #define mbedtls_free free
Simon Cooksey 0:fb7af294d5d9 36 #define mbedtls_time_t time_t
Simon Cooksey 0:fb7af294d5d9 37 #define mbedtls_snprintf snprintf
Simon Cooksey 0:fb7af294d5d9 38 #endif
Simon Cooksey 0:fb7af294d5d9 39
Simon Cooksey 0:fb7af294d5d9 40 #include "mbedtls/debug.h"
Simon Cooksey 0:fb7af294d5d9 41
Simon Cooksey 0:fb7af294d5d9 42 #include <stdarg.h>
Simon Cooksey 0:fb7af294d5d9 43 #include <stdio.h>
Simon Cooksey 0:fb7af294d5d9 44 #include <string.h>
Simon Cooksey 0:fb7af294d5d9 45
Simon Cooksey 0:fb7af294d5d9 46 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
Simon Cooksey 0:fb7af294d5d9 47 !defined(inline) && !defined(__cplusplus)
Simon Cooksey 0:fb7af294d5d9 48 #define inline __inline
Simon Cooksey 0:fb7af294d5d9 49 #endif
Simon Cooksey 0:fb7af294d5d9 50
Simon Cooksey 0:fb7af294d5d9 51 #define DEBUG_BUF_SIZE 512
Simon Cooksey 0:fb7af294d5d9 52
Simon Cooksey 0:fb7af294d5d9 53 static int debug_threshold = 0;
Simon Cooksey 0:fb7af294d5d9 54
Simon Cooksey 0:fb7af294d5d9 55 void mbedtls_debug_set_threshold( int threshold )
Simon Cooksey 0:fb7af294d5d9 56 {
Simon Cooksey 0:fb7af294d5d9 57 debug_threshold = threshold;
Simon Cooksey 0:fb7af294d5d9 58 }
Simon Cooksey 0:fb7af294d5d9 59
Simon Cooksey 0:fb7af294d5d9 60 /*
Simon Cooksey 0:fb7af294d5d9 61 * All calls to f_dbg must be made via this function
Simon Cooksey 0:fb7af294d5d9 62 */
Simon Cooksey 0:fb7af294d5d9 63 static inline void debug_send_line( const mbedtls_ssl_context *ssl, int level,
Simon Cooksey 0:fb7af294d5d9 64 const char *file, int line,
Simon Cooksey 0:fb7af294d5d9 65 const char *str )
Simon Cooksey 0:fb7af294d5d9 66 {
Simon Cooksey 0:fb7af294d5d9 67 /*
Simon Cooksey 0:fb7af294d5d9 68 * If in a threaded environment, we need a thread identifier.
Simon Cooksey 0:fb7af294d5d9 69 * Since there is no portable way to get one, use the address of the ssl
Simon Cooksey 0:fb7af294d5d9 70 * context instead, as it shouldn't be shared between threads.
Simon Cooksey 0:fb7af294d5d9 71 */
Simon Cooksey 0:fb7af294d5d9 72 #if defined(MBEDTLS_THREADING_C)
Simon Cooksey 0:fb7af294d5d9 73 char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */
Simon Cooksey 0:fb7af294d5d9 74 mbedtls_snprintf( idstr, sizeof( idstr ), "%p: %s", ssl, str );
Simon Cooksey 0:fb7af294d5d9 75 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, idstr );
Simon Cooksey 0:fb7af294d5d9 76 #else
Simon Cooksey 0:fb7af294d5d9 77 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
Simon Cooksey 0:fb7af294d5d9 78 #endif
Simon Cooksey 0:fb7af294d5d9 79 }
Simon Cooksey 0:fb7af294d5d9 80
Simon Cooksey 0:fb7af294d5d9 81 void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
Simon Cooksey 0:fb7af294d5d9 82 const char *file, int line,
Simon Cooksey 0:fb7af294d5d9 83 const char *format, ... )
Simon Cooksey 0:fb7af294d5d9 84 {
Simon Cooksey 0:fb7af294d5d9 85 va_list argp;
Simon Cooksey 0:fb7af294d5d9 86 char str[DEBUG_BUF_SIZE];
Simon Cooksey 0:fb7af294d5d9 87 int ret;
Simon Cooksey 0:fb7af294d5d9 88
Simon Cooksey 0:fb7af294d5d9 89 if( NULL == ssl || NULL == ssl->conf || NULL == ssl->conf->f_dbg || level > debug_threshold )
Simon Cooksey 0:fb7af294d5d9 90 return;
Simon Cooksey 0:fb7af294d5d9 91
Simon Cooksey 0:fb7af294d5d9 92 va_start( argp, format );
Simon Cooksey 0:fb7af294d5d9 93 #if defined(_WIN32)
Simon Cooksey 0:fb7af294d5d9 94 #if defined(_TRUNCATE)
Simon Cooksey 0:fb7af294d5d9 95 ret = _vsnprintf_s( str, DEBUG_BUF_SIZE, _TRUNCATE, format, argp );
Simon Cooksey 0:fb7af294d5d9 96 #else
Simon Cooksey 0:fb7af294d5d9 97 ret = _vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
Simon Cooksey 0:fb7af294d5d9 98 if( ret < 0 || (size_t) ret == DEBUG_BUF_SIZE )
Simon Cooksey 0:fb7af294d5d9 99 {
Simon Cooksey 0:fb7af294d5d9 100 str[DEBUG_BUF_SIZE-1] = '\0';
Simon Cooksey 0:fb7af294d5d9 101 ret = -1;
Simon Cooksey 0:fb7af294d5d9 102 }
Simon Cooksey 0:fb7af294d5d9 103 #endif
Simon Cooksey 0:fb7af294d5d9 104 #else
Simon Cooksey 0:fb7af294d5d9 105 ret = vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
Simon Cooksey 0:fb7af294d5d9 106 #endif
Simon Cooksey 0:fb7af294d5d9 107 va_end( argp );
Simon Cooksey 0:fb7af294d5d9 108
Simon Cooksey 0:fb7af294d5d9 109 if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 )
Simon Cooksey 0:fb7af294d5d9 110 {
Simon Cooksey 0:fb7af294d5d9 111 str[ret] = '\n';
Simon Cooksey 0:fb7af294d5d9 112 str[ret + 1] = '\0';
Simon Cooksey 0:fb7af294d5d9 113 }
Simon Cooksey 0:fb7af294d5d9 114
Simon Cooksey 0:fb7af294d5d9 115 debug_send_line( ssl, level, file, line, str );
Simon Cooksey 0:fb7af294d5d9 116 }
Simon Cooksey 0:fb7af294d5d9 117
Simon Cooksey 0:fb7af294d5d9 118 void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
Simon Cooksey 0:fb7af294d5d9 119 const char *file, int line,
Simon Cooksey 0:fb7af294d5d9 120 const char *text, int ret )
Simon Cooksey 0:fb7af294d5d9 121 {
Simon Cooksey 0:fb7af294d5d9 122 char str[DEBUG_BUF_SIZE];
Simon Cooksey 0:fb7af294d5d9 123
Simon Cooksey 0:fb7af294d5d9 124 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
Simon Cooksey 0:fb7af294d5d9 125 return;
Simon Cooksey 0:fb7af294d5d9 126
Simon Cooksey 0:fb7af294d5d9 127 /*
Simon Cooksey 0:fb7af294d5d9 128 * With non-blocking I/O and examples that just retry immediately,
Simon Cooksey 0:fb7af294d5d9 129 * the logs would be quickly flooded with WANT_READ, so ignore that.
Simon Cooksey 0:fb7af294d5d9 130 * Don't ignore WANT_WRITE however, since is is usually rare.
Simon Cooksey 0:fb7af294d5d9 131 */
Simon Cooksey 0:fb7af294d5d9 132 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
Simon Cooksey 0:fb7af294d5d9 133 return;
Simon Cooksey 0:fb7af294d5d9 134
Simon Cooksey 0:fb7af294d5d9 135 mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n",
Simon Cooksey 0:fb7af294d5d9 136 text, ret, -ret );
Simon Cooksey 0:fb7af294d5d9 137
Simon Cooksey 0:fb7af294d5d9 138 debug_send_line( ssl, level, file, line, str );
Simon Cooksey 0:fb7af294d5d9 139 }
Simon Cooksey 0:fb7af294d5d9 140
Simon Cooksey 0:fb7af294d5d9 141 void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
Simon Cooksey 0:fb7af294d5d9 142 const char *file, int line, const char *text,
Simon Cooksey 0:fb7af294d5d9 143 const unsigned char *buf, size_t len )
Simon Cooksey 0:fb7af294d5d9 144 {
Simon Cooksey 0:fb7af294d5d9 145 char str[DEBUG_BUF_SIZE];
Simon Cooksey 0:fb7af294d5d9 146 char txt[17];
Simon Cooksey 0:fb7af294d5d9 147 size_t i, idx = 0;
Simon Cooksey 0:fb7af294d5d9 148
Simon Cooksey 0:fb7af294d5d9 149 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
Simon Cooksey 0:fb7af294d5d9 150 return;
Simon Cooksey 0:fb7af294d5d9 151
Simon Cooksey 0:fb7af294d5d9 152 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n",
Simon Cooksey 0:fb7af294d5d9 153 text, (unsigned int) len );
Simon Cooksey 0:fb7af294d5d9 154
Simon Cooksey 0:fb7af294d5d9 155 debug_send_line( ssl, level, file, line, str );
Simon Cooksey 0:fb7af294d5d9 156
Simon Cooksey 0:fb7af294d5d9 157 idx = 0;
Simon Cooksey 0:fb7af294d5d9 158 memset( txt, 0, sizeof( txt ) );
Simon Cooksey 0:fb7af294d5d9 159 for( i = 0; i < len; i++ )
Simon Cooksey 0:fb7af294d5d9 160 {
Simon Cooksey 0:fb7af294d5d9 161 if( i >= 4096 )
Simon Cooksey 0:fb7af294d5d9 162 break;
Simon Cooksey 0:fb7af294d5d9 163
Simon Cooksey 0:fb7af294d5d9 164 if( i % 16 == 0 )
Simon Cooksey 0:fb7af294d5d9 165 {
Simon Cooksey 0:fb7af294d5d9 166 if( i > 0 )
Simon Cooksey 0:fb7af294d5d9 167 {
Simon Cooksey 0:fb7af294d5d9 168 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Simon Cooksey 0:fb7af294d5d9 169 debug_send_line( ssl, level, file, line, str );
Simon Cooksey 0:fb7af294d5d9 170
Simon Cooksey 0:fb7af294d5d9 171 idx = 0;
Simon Cooksey 0:fb7af294d5d9 172 memset( txt, 0, sizeof( txt ) );
Simon Cooksey 0:fb7af294d5d9 173 }
Simon Cooksey 0:fb7af294d5d9 174
Simon Cooksey 0:fb7af294d5d9 175 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ",
Simon Cooksey 0:fb7af294d5d9 176 (unsigned int) i );
Simon Cooksey 0:fb7af294d5d9 177
Simon Cooksey 0:fb7af294d5d9 178 }
Simon Cooksey 0:fb7af294d5d9 179
Simon Cooksey 0:fb7af294d5d9 180 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x",
Simon Cooksey 0:fb7af294d5d9 181 (unsigned int) buf[i] );
Simon Cooksey 0:fb7af294d5d9 182 txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
Simon Cooksey 0:fb7af294d5d9 183 }
Simon Cooksey 0:fb7af294d5d9 184
Simon Cooksey 0:fb7af294d5d9 185 if( len > 0 )
Simon Cooksey 0:fb7af294d5d9 186 {
Simon Cooksey 0:fb7af294d5d9 187 for( /* i = i */; i % 16 != 0; i++ )
Simon Cooksey 0:fb7af294d5d9 188 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " );
Simon Cooksey 0:fb7af294d5d9 189
Simon Cooksey 0:fb7af294d5d9 190 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Simon Cooksey 0:fb7af294d5d9 191 debug_send_line( ssl, level, file, line, str );
Simon Cooksey 0:fb7af294d5d9 192 }
Simon Cooksey 0:fb7af294d5d9 193 }
Simon Cooksey 0:fb7af294d5d9 194
Simon Cooksey 0:fb7af294d5d9 195 #if defined(MBEDTLS_ECP_C)
Simon Cooksey 0:fb7af294d5d9 196 void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
Simon Cooksey 0:fb7af294d5d9 197 const char *file, int line,
Simon Cooksey 0:fb7af294d5d9 198 const char *text, const mbedtls_ecp_point *X )
Simon Cooksey 0:fb7af294d5d9 199 {
Simon Cooksey 0:fb7af294d5d9 200 char str[DEBUG_BUF_SIZE];
Simon Cooksey 0:fb7af294d5d9 201
Simon Cooksey 0:fb7af294d5d9 202 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
Simon Cooksey 0:fb7af294d5d9 203 return;
Simon Cooksey 0:fb7af294d5d9 204
Simon Cooksey 0:fb7af294d5d9 205 mbedtls_snprintf( str, sizeof( str ), "%s(X)", text );
Simon Cooksey 0:fb7af294d5d9 206 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
Simon Cooksey 0:fb7af294d5d9 207
Simon Cooksey 0:fb7af294d5d9 208 mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text );
Simon Cooksey 0:fb7af294d5d9 209 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
Simon Cooksey 0:fb7af294d5d9 210 }
Simon Cooksey 0:fb7af294d5d9 211 #endif /* MBEDTLS_ECP_C */
Simon Cooksey 0:fb7af294d5d9 212
Simon Cooksey 0:fb7af294d5d9 213 #if defined(MBEDTLS_BIGNUM_C)
Simon Cooksey 0:fb7af294d5d9 214 void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
Simon Cooksey 0:fb7af294d5d9 215 const char *file, int line,
Simon Cooksey 0:fb7af294d5d9 216 const char *text, const mbedtls_mpi *X )
Simon Cooksey 0:fb7af294d5d9 217 {
Simon Cooksey 0:fb7af294d5d9 218 char str[DEBUG_BUF_SIZE];
Simon Cooksey 0:fb7af294d5d9 219 int j, k, zeros = 1;
Simon Cooksey 0:fb7af294d5d9 220 size_t i, n, idx = 0;
Simon Cooksey 0:fb7af294d5d9 221
Simon Cooksey 0:fb7af294d5d9 222 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || X == NULL || level > debug_threshold )
Simon Cooksey 0:fb7af294d5d9 223 return;
Simon Cooksey 0:fb7af294d5d9 224
Simon Cooksey 0:fb7af294d5d9 225 for( n = X->n - 1; n > 0; n-- )
Simon Cooksey 0:fb7af294d5d9 226 if( X->p[n] != 0 )
Simon Cooksey 0:fb7af294d5d9 227 break;
Simon Cooksey 0:fb7af294d5d9 228
Simon Cooksey 0:fb7af294d5d9 229 for( j = ( sizeof(mbedtls_mpi_uint) << 3 ) - 1; j >= 0; j-- )
Simon Cooksey 0:fb7af294d5d9 230 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
Simon Cooksey 0:fb7af294d5d9 231 break;
Simon Cooksey 0:fb7af294d5d9 232
Simon Cooksey 0:fb7af294d5d9 233 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "value of '%s' (%d bits) is:\n",
Simon Cooksey 0:fb7af294d5d9 234 text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) );
Simon Cooksey 0:fb7af294d5d9 235
Simon Cooksey 0:fb7af294d5d9 236 debug_send_line( ssl, level, file, line, str );
Simon Cooksey 0:fb7af294d5d9 237
Simon Cooksey 0:fb7af294d5d9 238 idx = 0;
Simon Cooksey 0:fb7af294d5d9 239 for( i = n + 1, j = 0; i > 0; i-- )
Simon Cooksey 0:fb7af294d5d9 240 {
Simon Cooksey 0:fb7af294d5d9 241 if( zeros && X->p[i - 1] == 0 )
Simon Cooksey 0:fb7af294d5d9 242 continue;
Simon Cooksey 0:fb7af294d5d9 243
Simon Cooksey 0:fb7af294d5d9 244 for( k = sizeof( mbedtls_mpi_uint ) - 1; k >= 0; k-- )
Simon Cooksey 0:fb7af294d5d9 245 {
Simon Cooksey 0:fb7af294d5d9 246 if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
Simon Cooksey 0:fb7af294d5d9 247 continue;
Simon Cooksey 0:fb7af294d5d9 248 else
Simon Cooksey 0:fb7af294d5d9 249 zeros = 0;
Simon Cooksey 0:fb7af294d5d9 250
Simon Cooksey 0:fb7af294d5d9 251 if( j % 16 == 0 )
Simon Cooksey 0:fb7af294d5d9 252 {
Simon Cooksey 0:fb7af294d5d9 253 if( j > 0 )
Simon Cooksey 0:fb7af294d5d9 254 {
Simon Cooksey 0:fb7af294d5d9 255 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
Simon Cooksey 0:fb7af294d5d9 256 debug_send_line( ssl, level, file, line, str );
Simon Cooksey 0:fb7af294d5d9 257 idx = 0;
Simon Cooksey 0:fb7af294d5d9 258 }
Simon Cooksey 0:fb7af294d5d9 259 }
Simon Cooksey 0:fb7af294d5d9 260
Simon Cooksey 0:fb7af294d5d9 261 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", (unsigned int)
Simon Cooksey 0:fb7af294d5d9 262 ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
Simon Cooksey 0:fb7af294d5d9 263
Simon Cooksey 0:fb7af294d5d9 264 j++;
Simon Cooksey 0:fb7af294d5d9 265 }
Simon Cooksey 0:fb7af294d5d9 266
Simon Cooksey 0:fb7af294d5d9 267 }
Simon Cooksey 0:fb7af294d5d9 268
Simon Cooksey 0:fb7af294d5d9 269 if( zeros == 1 )
Simon Cooksey 0:fb7af294d5d9 270 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " 00" );
Simon Cooksey 0:fb7af294d5d9 271
Simon Cooksey 0:fb7af294d5d9 272 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
Simon Cooksey 0:fb7af294d5d9 273 debug_send_line( ssl, level, file, line, str );
Simon Cooksey 0:fb7af294d5d9 274 }
Simon Cooksey 0:fb7af294d5d9 275 #endif /* MBEDTLS_BIGNUM_C */
Simon Cooksey 0:fb7af294d5d9 276
Simon Cooksey 0:fb7af294d5d9 277 #if defined(MBEDTLS_X509_CRT_PARSE_C)
Simon Cooksey 0:fb7af294d5d9 278 static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
Simon Cooksey 0:fb7af294d5d9 279 const char *file, int line,
Simon Cooksey 0:fb7af294d5d9 280 const char *text, const mbedtls_pk_context *pk )
Simon Cooksey 0:fb7af294d5d9 281 {
Simon Cooksey 0:fb7af294d5d9 282 size_t i;
Simon Cooksey 0:fb7af294d5d9 283 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
Simon Cooksey 0:fb7af294d5d9 284 char name[16];
Simon Cooksey 0:fb7af294d5d9 285
Simon Cooksey 0:fb7af294d5d9 286 memset( items, 0, sizeof( items ) );
Simon Cooksey 0:fb7af294d5d9 287
Simon Cooksey 0:fb7af294d5d9 288 if( mbedtls_pk_debug( pk, items ) != 0 )
Simon Cooksey 0:fb7af294d5d9 289 {
Simon Cooksey 0:fb7af294d5d9 290 debug_send_line( ssl, level, file, line,
Simon Cooksey 0:fb7af294d5d9 291 "invalid PK context\n" );
Simon Cooksey 0:fb7af294d5d9 292 return;
Simon Cooksey 0:fb7af294d5d9 293 }
Simon Cooksey 0:fb7af294d5d9 294
Simon Cooksey 0:fb7af294d5d9 295 for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
Simon Cooksey 0:fb7af294d5d9 296 {
Simon Cooksey 0:fb7af294d5d9 297 if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
Simon Cooksey 0:fb7af294d5d9 298 return;
Simon Cooksey 0:fb7af294d5d9 299
Simon Cooksey 0:fb7af294d5d9 300 mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
Simon Cooksey 0:fb7af294d5d9 301 name[sizeof( name ) - 1] = '\0';
Simon Cooksey 0:fb7af294d5d9 302
Simon Cooksey 0:fb7af294d5d9 303 if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
Simon Cooksey 0:fb7af294d5d9 304 mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
Simon Cooksey 0:fb7af294d5d9 305 else
Simon Cooksey 0:fb7af294d5d9 306 #if defined(MBEDTLS_ECP_C)
Simon Cooksey 0:fb7af294d5d9 307 if( items[i].type == MBEDTLS_PK_DEBUG_ECP )
Simon Cooksey 0:fb7af294d5d9 308 mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value );
Simon Cooksey 0:fb7af294d5d9 309 else
Simon Cooksey 0:fb7af294d5d9 310 #endif
Simon Cooksey 0:fb7af294d5d9 311 debug_send_line( ssl, level, file, line,
Simon Cooksey 0:fb7af294d5d9 312 "should not happen\n" );
Simon Cooksey 0:fb7af294d5d9 313 }
Simon Cooksey 0:fb7af294d5d9 314 }
Simon Cooksey 0:fb7af294d5d9 315
Simon Cooksey 0:fb7af294d5d9 316 static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level,
Simon Cooksey 0:fb7af294d5d9 317 const char *file, int line, const char *text )
Simon Cooksey 0:fb7af294d5d9 318 {
Simon Cooksey 0:fb7af294d5d9 319 char str[DEBUG_BUF_SIZE];
Simon Cooksey 0:fb7af294d5d9 320 const char *start, *cur;
Simon Cooksey 0:fb7af294d5d9 321
Simon Cooksey 0:fb7af294d5d9 322 start = text;
Simon Cooksey 0:fb7af294d5d9 323 for( cur = text; *cur != '\0'; cur++ )
Simon Cooksey 0:fb7af294d5d9 324 {
Simon Cooksey 0:fb7af294d5d9 325 if( *cur == '\n' )
Simon Cooksey 0:fb7af294d5d9 326 {
Simon Cooksey 0:fb7af294d5d9 327 size_t len = cur - start + 1;
Simon Cooksey 0:fb7af294d5d9 328 if( len > DEBUG_BUF_SIZE - 1 )
Simon Cooksey 0:fb7af294d5d9 329 len = DEBUG_BUF_SIZE - 1;
Simon Cooksey 0:fb7af294d5d9 330
Simon Cooksey 0:fb7af294d5d9 331 memcpy( str, start, len );
Simon Cooksey 0:fb7af294d5d9 332 str[len] = '\0';
Simon Cooksey 0:fb7af294d5d9 333
Simon Cooksey 0:fb7af294d5d9 334 debug_send_line( ssl, level, file, line, str );
Simon Cooksey 0:fb7af294d5d9 335
Simon Cooksey 0:fb7af294d5d9 336 start = cur + 1;
Simon Cooksey 0:fb7af294d5d9 337 }
Simon Cooksey 0:fb7af294d5d9 338 }
Simon Cooksey 0:fb7af294d5d9 339 }
Simon Cooksey 0:fb7af294d5d9 340
Simon Cooksey 0:fb7af294d5d9 341 void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
Simon Cooksey 0:fb7af294d5d9 342 const char *file, int line,
Simon Cooksey 0:fb7af294d5d9 343 const char *text, const mbedtls_x509_crt *crt )
Simon Cooksey 0:fb7af294d5d9 344 {
Simon Cooksey 0:fb7af294d5d9 345 char str[DEBUG_BUF_SIZE];
Simon Cooksey 0:fb7af294d5d9 346 int i = 0;
Simon Cooksey 0:fb7af294d5d9 347
Simon Cooksey 0:fb7af294d5d9 348 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || crt == NULL || level > debug_threshold )
Simon Cooksey 0:fb7af294d5d9 349 return;
Simon Cooksey 0:fb7af294d5d9 350
Simon Cooksey 0:fb7af294d5d9 351 while( crt != NULL )
Simon Cooksey 0:fb7af294d5d9 352 {
Simon Cooksey 0:fb7af294d5d9 353 char buf[1024];
Simon Cooksey 0:fb7af294d5d9 354
Simon Cooksey 0:fb7af294d5d9 355 mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i );
Simon Cooksey 0:fb7af294d5d9 356 debug_send_line( ssl, level, file, line, str );
Simon Cooksey 0:fb7af294d5d9 357
Simon Cooksey 0:fb7af294d5d9 358 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
Simon Cooksey 0:fb7af294d5d9 359 debug_print_line_by_line( ssl, level, file, line, buf );
Simon Cooksey 0:fb7af294d5d9 360
Simon Cooksey 0:fb7af294d5d9 361 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
Simon Cooksey 0:fb7af294d5d9 362
Simon Cooksey 0:fb7af294d5d9 363 crt = crt->next;
Simon Cooksey 0:fb7af294d5d9 364 }
Simon Cooksey 0:fb7af294d5d9 365 }
Simon Cooksey 0:fb7af294d5d9 366 #endif /* MBEDTLS_X509_CRT_PARSE_C */
Simon Cooksey 0:fb7af294d5d9 367
Simon Cooksey 0:fb7af294d5d9 368 #endif /* MBEDTLS_DEBUG_C */