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