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