mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 31 06:02:27 2019 +0000
Revision:
1:9db0e321a9f4
Parent:
0:5b88d5760320
updated based on mbed-os5.15.0

Who changed what in which revision?

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