Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

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