blob: 6e1efbf94b73bfa00fc9232dd1d811a84f94fb2d [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Debugging routines
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_DEBUG_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/platform.h"
Rich Evans2387c7d2015-01-30 11:10:20 +000032#else
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020033#include <stdlib.h>
34#define mbedtls_calloc calloc
35#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010036#define mbedtls_time_t time_t
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020037#define mbedtls_snprintf snprintf
k-stachowiak723f8672018-07-16 14:27:07 +020038#define mbedtls_vsnprintf vsnprintf
Rich Evans2387c7d2015-01-30 11:10:20 +000039#endif
40
SimonBd5800b72016-04-26 07:43:27 +010041#include "mbedtls/debug.h"
42
43#include <stdarg.h>
44#include <stdio.h>
45#include <string.h>
46
Manuel Pégourié-Gonnard0223ab92015-10-05 11:40:01 +010047#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
48 !defined(inline) && !defined(__cplusplus)
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020049#define inline __inline
50#endif
51
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020052#define DEBUG_BUF_SIZE 512
53
Paul Bakkerc73079a2014-04-25 16:34:30 +020054static int debug_threshold = 0;
Paul Bakkereaebbd52014-04-25 15:04:14 +020055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056void mbedtls_debug_set_threshold( int threshold )
Paul Bakkerc73079a2014-04-25 16:34:30 +020057{
58 debug_threshold = threshold;
59}
60
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020061/*
62 * All calls to f_dbg must be made via this function
63 */
64static inline void debug_send_line( const mbedtls_ssl_context *ssl, int level,
65 const char *file, int line,
66 const char *str )
67{
68 /*
69 * If in a threaded environment, we need a thread identifier.
70 * Since there is no portable way to get one, use the address of the ssl
71 * context instead, as it shouldn't be shared between threads.
72 */
73#if defined(MBEDTLS_THREADING_C)
74 char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */
Simon Butcher097618b2016-11-10 17:28:55 +000075 mbedtls_snprintf( idstr, sizeof( idstr ), "%p: %s", (void*)ssl, str );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020076 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, idstr );
77#else
78 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
79#endif
80}
81
Manuel Pégourié-Gonnarda16e7c42015-06-29 20:14:19 +020082void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020083 const char *file, int line,
84 const char *format, ... )
Paul Bakker5121ce52009-01-03 21:22:43 +000085{
86 va_list argp;
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020087 char str[DEBUG_BUF_SIZE];
88 int ret;
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020089
Janos Follath8eb64132016-06-03 15:40:57 +010090 if( NULL == ssl || NULL == ssl->conf || NULL == ssl->conf->f_dbg || level > debug_threshold )
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020091 return;
Paul Bakker5121ce52009-01-03 21:22:43 +000092
93 va_start( argp, format );
k-stachowiak723f8672018-07-16 14:27:07 +020094 ret = mbedtls_vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
Paul Bakker5121ce52009-01-03 21:22:43 +000095 va_end( argp );
96
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020097 if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 )
98 {
99 str[ret] = '\n';
100 str[ret + 1] = '\0';
101 }
102
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200103 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000104}
105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000107 const char *file, int line,
108 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +0000109{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200110 char str[DEBUG_BUF_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200112 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 return;
114
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200115 /*
116 * With non-blocking I/O and examples that just retry immediately,
117 * the logs would be quickly flooded with WANT_READ, so ignore that.
118 * Don't ignore WANT_WRITE however, since is is usually rare.
119 */
120 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
121 return;
122
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200123 mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200124 text, ret, -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000125
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200126 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000127}
128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000130 const char *file, int line, const char *text,
Manuel Pégourié-Gonnarda78b2182015-03-19 17:16:11 +0000131 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000132{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200133 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100134 char txt[17];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200135 size_t i, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000136
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200137 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 return;
139
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200140 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200141 text, (unsigned int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200143 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000144
Paul Bakker92478c32014-04-25 15:18:34 +0200145 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100146 memset( txt, 0, sizeof( txt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 for( i = 0; i < len; i++ )
148 {
149 if( i >= 4096 )
150 break;
151
152 if( i % 16 == 0 )
153 {
154 if( i > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200155 {
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200156 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200157 debug_send_line( ssl, level, file, line, str );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100158
Paul Bakker92478c32014-04-25 15:18:34 +0200159 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100160 memset( txt, 0, sizeof( txt ) );
Paul Bakker92478c32014-04-25 15:18:34 +0200161 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200163 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ",
Paul Bakker92478c32014-04-25 15:18:34 +0200164 (unsigned int) i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000165
Paul Bakker5121ce52009-01-03 21:22:43 +0000166 }
167
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200168 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x",
Paul Bakker92478c32014-04-25 15:18:34 +0200169 (unsigned int) buf[i] );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100170 txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
Paul Bakker5121ce52009-01-03 21:22:43 +0000171 }
172
173 if( len > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200174 {
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100175 for( /* i = i */; i % 16 != 0; i++ )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200176 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100177
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200178 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200179 debug_send_line( ssl, level, file, line, str );
Paul Bakker92478c32014-04-25 15:18:34 +0200180 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000181}
182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183#if defined(MBEDTLS_ECP_C)
184void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
Paul Bakker41c83d32013-03-20 14:39:14 +0100185 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 const char *text, const mbedtls_ecp_point *X )
Paul Bakker41c83d32013-03-20 14:39:14 +0100187{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200188 char str[DEBUG_BUF_SIZE];
Paul Bakker41c83d32013-03-20 14:39:14 +0100189
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200190 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
Paul Bakkerc73079a2014-04-25 16:34:30 +0200191 return;
192
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200193 mbedtls_snprintf( str, sizeof( str ), "%s(X)", text );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
Paul Bakker41c83d32013-03-20 14:39:14 +0100195
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200196 mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
Paul Bakker41c83d32013-03-20 14:39:14 +0100198}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#endif /* MBEDTLS_ECP_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#if defined(MBEDTLS_BIGNUM_C)
202void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000203 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 const char *text, const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000205{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200206 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200207 int j, k, zeros = 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +0200208 size_t i, n, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000209
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200210 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || X == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 return;
212
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000213 for( n = X->n - 1; n > 0; n-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 if( X->p[n] != 0 )
215 break;
216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 for( j = ( sizeof(mbedtls_mpi_uint) << 3 ) - 1; j >= 0; j-- )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000218 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
219 break;
220
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200221 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "value of '%s' (%d bits) is:\n",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000223
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200224 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000225
Paul Bakker92478c32014-04-25 15:18:34 +0200226 idx = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000227 for( i = n + 1, j = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 {
Paul Bakker23986e52011-04-24 08:57:21 +0000229 if( zeros && X->p[i - 1] == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000230 continue;
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 for( k = sizeof( mbedtls_mpi_uint ) - 1; k >= 0; k-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000233 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200234 if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000235 continue;
236 else
237 zeros = 0;
238
239 if( j % 16 == 0 )
240 {
241 if( j > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200242 {
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200243 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200244 debug_send_line( ssl, level, file, line, str );
Paul Bakker92478c32014-04-25 15:18:34 +0200245 idx = 0;
246 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000247 }
248
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200249 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", (unsigned int)
Paul Bakker66d5d072014-06-17 16:39:18 +0200250 ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000251
252 j++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000253 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000254
255 }
256
257 if( zeros == 1 )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200258 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " 00" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000259
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200260 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200261 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000262}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265#if defined(MBEDTLS_X509_CRT_PARSE_C)
266static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200267 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 const char *text, const mbedtls_pk_context *pk )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200269{
270 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200272 char name[16];
273
274 memset( items, 0, sizeof( items ) );
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 if( mbedtls_pk_debug( pk, items ) != 0 )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200277 {
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200278 debug_send_line( ssl, level, file, line,
Manuel Pégourié-Gonnard80d627a2015-06-29 20:12:51 +0200279 "invalid PK context\n" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200280 return;
281 }
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200286 return;
287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200289 name[sizeof( name ) - 1] = '\0';
290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
292 mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200293 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294#if defined(MBEDTLS_ECP_C)
295 if( items[i].type == MBEDTLS_PK_DEBUG_ECP )
296 mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200297 else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200298#endif
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200299 debug_send_line( ssl, level, file, line,
Manuel Pégourié-Gonnard80d627a2015-06-29 20:12:51 +0200300 "should not happen\n" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200301 }
302}
303
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200304static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level,
305 const char *file, int line, const char *text )
306{
307 char str[DEBUG_BUF_SIZE];
308 const char *start, *cur;
309
310 start = text;
311 for( cur = text; *cur != '\0'; cur++ )
312 {
313 if( *cur == '\n' )
314 {
315 size_t len = cur - start + 1;
316 if( len > DEBUG_BUF_SIZE - 1 )
317 len = DEBUG_BUF_SIZE - 1;
318
319 memcpy( str, start, len );
320 str[len] = '\0';
321
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200322 debug_send_line( ssl, level, file, line, str );
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200323
324 start = cur + 1;
325 }
326 }
327}
328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000330 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 const char *text, const mbedtls_x509_crt *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000332{
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200333 char str[DEBUG_BUF_SIZE];
334 int i = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000335
Manuel Pégourié-Gonnard49f5eb92015-05-12 12:19:09 +0200336 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || crt == NULL || level > debug_threshold )
Paul Bakker5121ce52009-01-03 21:22:43 +0000337 return;
338
Paul Bakker29087132010-03-21 21:03:34 +0000339 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000340 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000341 char buf[1024];
Paul Bakker5121ce52009-01-03 21:22:43 +0000342
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200343 mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200344 debug_send_line( ssl, level, file, line, str );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200345
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200346 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
347 debug_print_line_by_line( ssl, level, file, line, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000348
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200349 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +0000350
351 crt = crt->next;
352 }
353}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000355
Janos Follath948f4be2018-08-22 01:37:55 +0100356#if defined(MBEDTLS_ECDH_C)
357static void mbedtls_debug_printf_ecdh_internal( const mbedtls_ssl_context *ssl,
358 int level, const char *file,
359 int line,
360 const mbedtls_ecdh_context *ecdh,
361 mbedtls_debug_ecdh_attr attr )
362{
363#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
364 const mbedtls_ecdh_context* ctx = ecdh;
365#else
366 const mbedtls_ecdh_context_mbed* ctx = &ecdh->ctx.mbed_ecdh;
367#endif
368
369 switch( attr )
370 {
371 case MBEDTLS_DEBUG_ECDH_Q:
372 mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Q",
373 &ctx->Q );
374 break;
375 case MBEDTLS_DEBUG_ECDH_QP:
376 mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Qp",
377 &ctx->Qp );
378 break;
379 case MBEDTLS_DEBUG_ECDH_Z:
380 mbedtls_debug_print_mpi( ssl, level, file, line, "ECDH: z",
381 &ctx->z );
382 break;
383 default:
384 break;
385 }
386}
387
388void mbedtls_debug_printf_ecdh( const mbedtls_ssl_context *ssl, int level,
389 const char *file, int line,
390 const mbedtls_ecdh_context *ecdh,
391 mbedtls_debug_ecdh_attr attr )
392{
393#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
394 mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh, attr );
395#else
396 switch( ecdh->var )
397 {
398 default:
399 mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh,
400 attr );
401 }
402#endif
403}
404#endif /* MBEDTLS_ECDH_C */
405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406#endif /* MBEDTLS_DEBUG_C */