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