mbedtls ported to mbed-classic

Fork of mbedtls by Christopher Haster

Committer:
Christopher Haster
Date:
Fri Jan 22 16:44:49 2016 -0600
Revision:
1:24750b9ad5ef
Initial move of mbedtls to mercurial

Who changed what in which revision?

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