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