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