This is a fork due to permission issues

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of 6_songs-from-the-cloud by MakingMusicWorkshop

Committer:
maclobdell
Date:
Wed May 18 19:06:32 2016 +0000
Revision:
0:f7c60d3e7b8a
clean version

Who changed what in which revision?

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