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