blob: 2b25e997c55a1fec6a2231e966d41a773f51ba18 [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"
Janos Follath73c616b2019-12-18 15:07:04 +000042#include "mbedtls/error.h"
SimonBd5800b72016-04-26 07:43:27 +010043
44#include <stdarg.h>
45#include <stdio.h>
46#include <string.h>
47
Manuel Pégourié-Gonnard0223ab92015-10-05 11:40:01 +010048#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
49 !defined(inline) && !defined(__cplusplus)
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020050#define inline __inline
51#endif
52
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020053#define DEBUG_BUF_SIZE 512
54
Paul Bakkerc73079a2014-04-25 16:34:30 +020055static int debug_threshold = 0;
Paul Bakkereaebbd52014-04-25 15:04:14 +020056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057void mbedtls_debug_set_threshold( int threshold )
Paul Bakkerc73079a2014-04-25 16:34:30 +020058{
59 debug_threshold = threshold;
60}
61
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020062/*
63 * All calls to f_dbg must be made via this function
64 */
65static inline void debug_send_line( const mbedtls_ssl_context *ssl, int level,
66 const char *file, int line,
67 const char *str )
68{
69 /*
70 * If in a threaded environment, we need a thread identifier.
71 * Since there is no portable way to get one, use the address of the ssl
72 * context instead, as it shouldn't be shared between threads.
73 */
74#if defined(MBEDTLS_THREADING_C)
75 char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */
Simon Butcher097618b2016-11-10 17:28:55 +000076 mbedtls_snprintf( idstr, sizeof( idstr ), "%p: %s", (void*)ssl, str );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020077 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, idstr );
78#else
79 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
80#endif
81}
82
Manuel Pégourié-Gonnarda16e7c42015-06-29 20:14:19 +020083void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020084 const char *file, int line,
85 const char *format, ... )
Paul Bakker5121ce52009-01-03 21:22:43 +000086{
87 va_list argp;
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020088 char str[DEBUG_BUF_SIZE];
Janos Follath865b3eb2019-12-16 11:46:15 +000089 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020090
Hanno Becker687c5002018-06-29 09:04:46 +010091 if( NULL == ssl ||
92 NULL == ssl->conf ||
93 NULL == ssl->conf->f_dbg ||
94 level > debug_threshold )
95 {
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020096 return;
Hanno Becker687c5002018-06-29 09:04:46 +010097 }
Paul Bakker5121ce52009-01-03 21:22:43 +000098
99 va_start( argp, format );
k-stachowiak723f8672018-07-16 14:27:07 +0200100 ret = mbedtls_vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000101 va_end( argp );
102
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +0200103 if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 )
104 {
105 str[ret] = '\n';
106 str[ret + 1] = '\0';
107 }
108
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200109 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000110}
111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000113 const char *file, int line,
114 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +0000115{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200116 char str[DEBUG_BUF_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +0000117
Hanno Becker687c5002018-06-29 09:04:46 +0100118 if( NULL == ssl ||
119 NULL == ssl->conf ||
120 NULL == ssl->conf->f_dbg ||
121 level > debug_threshold )
122 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100124 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000125
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200126 /*
127 * With non-blocking I/O and examples that just retry immediately,
128 * the logs would be quickly flooded with WANT_READ, so ignore that.
129 * Don't ignore WANT_WRITE however, since is is usually rare.
130 */
131 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
132 return;
133
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200134 mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200135 text, ret, (unsigned int) -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000136
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200137 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000138}
139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000141 const char *file, int line, const char *text,
Manuel Pégourié-Gonnarda78b2182015-03-19 17:16:11 +0000142 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000143{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200144 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100145 char txt[17];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200146 size_t i, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000147
Hanno Becker687c5002018-06-29 09:04:46 +0100148 if( NULL == ssl ||
149 NULL == ssl->conf ||
150 NULL == ssl->conf->f_dbg ||
151 level > debug_threshold )
152 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100154 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000155
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200156 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200157 text, (unsigned int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200159 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000160
Paul Bakker92478c32014-04-25 15:18:34 +0200161 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100162 memset( txt, 0, sizeof( txt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 for( i = 0; i < len; i++ )
164 {
165 if( i >= 4096 )
166 break;
167
168 if( i % 16 == 0 )
169 {
170 if( i > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200171 {
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200172 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200173 debug_send_line( ssl, level, file, line, str );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100174
Paul Bakker92478c32014-04-25 15:18:34 +0200175 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100176 memset( txt, 0, sizeof( txt ) );
Paul Bakker92478c32014-04-25 15:18:34 +0200177 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000178
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200179 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ",
Paul Bakker92478c32014-04-25 15:18:34 +0200180 (unsigned int) i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000181
Paul Bakker5121ce52009-01-03 21:22:43 +0000182 }
183
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200184 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x",
Paul Bakker92478c32014-04-25 15:18:34 +0200185 (unsigned int) buf[i] );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100186 txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
Paul Bakker5121ce52009-01-03 21:22:43 +0000187 }
188
189 if( len > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200190 {
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100191 for( /* i = i */; i % 16 != 0; i++ )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200192 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100193
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200194 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200195 debug_send_line( ssl, level, file, line, str );
Paul Bakker92478c32014-04-25 15:18:34 +0200196 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000197}
198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#if defined(MBEDTLS_ECP_C)
200void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
Paul Bakker41c83d32013-03-20 14:39:14 +0100201 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 const char *text, const mbedtls_ecp_point *X )
Paul Bakker41c83d32013-03-20 14:39:14 +0100203{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200204 char str[DEBUG_BUF_SIZE];
Paul Bakker41c83d32013-03-20 14:39:14 +0100205
Hanno Becker687c5002018-06-29 09:04:46 +0100206 if( NULL == ssl ||
207 NULL == ssl->conf ||
208 NULL == ssl->conf->f_dbg ||
209 level > debug_threshold )
210 {
Paul Bakkerc73079a2014-04-25 16:34:30 +0200211 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100212 }
Paul Bakkerc73079a2014-04-25 16:34:30 +0200213
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200214 mbedtls_snprintf( str, sizeof( str ), "%s(X)", text );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
Paul Bakker41c83d32013-03-20 14:39:14 +0100216
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200217 mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
Paul Bakker41c83d32013-03-20 14:39:14 +0100219}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220#endif /* MBEDTLS_ECP_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#if defined(MBEDTLS_BIGNUM_C)
223void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000224 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 const char *text, const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000226{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200227 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200228 int j, k, zeros = 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +0200229 size_t i, n, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000230
Hanno Becker687c5002018-06-29 09:04:46 +0100231 if( NULL == ssl ||
232 NULL == ssl->conf ||
233 NULL == ssl->conf->f_dbg ||
234 NULL == X ||
235 level > debug_threshold )
236 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000237 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100238 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000239
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000240 for( n = X->n - 1; n > 0; n-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000241 if( X->p[n] != 0 )
242 break;
243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 for( j = ( sizeof(mbedtls_mpi_uint) << 3 ) - 1; j >= 0; j-- )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000245 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
246 break;
247
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200248 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "value of '%s' (%d bits) is:\n",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000250
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200251 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000252
Paul Bakker92478c32014-04-25 15:18:34 +0200253 idx = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000254 for( i = n + 1, j = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 {
Paul Bakker23986e52011-04-24 08:57:21 +0000256 if( zeros && X->p[i - 1] == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000257 continue;
Paul Bakker5121ce52009-01-03 21:22:43 +0000258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 for( k = sizeof( mbedtls_mpi_uint ) - 1; k >= 0; k-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000260 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200261 if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000262 continue;
263 else
264 zeros = 0;
265
266 if( j % 16 == 0 )
267 {
268 if( j > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200269 {
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200270 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200271 debug_send_line( ssl, level, file, line, str );
Paul Bakker92478c32014-04-25 15:18:34 +0200272 idx = 0;
273 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000274 }
275
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200276 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", (unsigned int)
Paul Bakker66d5d072014-06-17 16:39:18 +0200277 ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000278
279 j++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000280 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000281
282 }
283
284 if( zeros == 1 )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200285 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " 00" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000286
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200287 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200288 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000289}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292#if defined(MBEDTLS_X509_CRT_PARSE_C)
293static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200294 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 const char *text, const mbedtls_pk_context *pk )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200296{
297 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200299 char name[16];
300
301 memset( items, 0, sizeof( items ) );
302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 if( mbedtls_pk_debug( pk, items ) != 0 )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200304 {
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200305 debug_send_line( ssl, level, file, line,
Manuel Pégourié-Gonnard80d627a2015-06-29 20:12:51 +0200306 "invalid PK context\n" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200307 return;
308 }
309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200313 return;
314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200316 name[sizeof( name ) - 1] = '\0';
317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
319 mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200320 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321#if defined(MBEDTLS_ECP_C)
322 if( items[i].type == MBEDTLS_PK_DEBUG_ECP )
323 mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200324 else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200325#endif
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200326 debug_send_line( ssl, level, file, line,
Manuel Pégourié-Gonnard80d627a2015-06-29 20:12:51 +0200327 "should not happen\n" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200328 }
329}
330
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200331static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level,
332 const char *file, int line, const char *text )
333{
334 char str[DEBUG_BUF_SIZE];
335 const char *start, *cur;
336
337 start = text;
338 for( cur = text; *cur != '\0'; cur++ )
339 {
340 if( *cur == '\n' )
341 {
342 size_t len = cur - start + 1;
343 if( len > DEBUG_BUF_SIZE - 1 )
344 len = DEBUG_BUF_SIZE - 1;
345
346 memcpy( str, start, len );
347 str[len] = '\0';
348
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200349 debug_send_line( ssl, level, file, line, str );
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200350
351 start = cur + 1;
352 }
353 }
354}
355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000357 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 const char *text, const mbedtls_x509_crt *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000359{
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200360 char str[DEBUG_BUF_SIZE];
361 int i = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000362
Hanno Becker687c5002018-06-29 09:04:46 +0100363 if( NULL == ssl ||
364 NULL == ssl->conf ||
365 NULL == ssl->conf->f_dbg ||
366 NULL == crt ||
367 level > debug_threshold )
368 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000369 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100370 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000371
Paul Bakker29087132010-03-21 21:03:34 +0000372 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000373 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000374 char buf[1024];
Paul Bakker5121ce52009-01-03 21:22:43 +0000375
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200376 mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200377 debug_send_line( ssl, level, file, line, str );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200378
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200379 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
380 debug_print_line_by_line( ssl, level, file, line, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000381
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200382 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +0000383
384 crt = crt->next;
385 }
386}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000388
Janos Follath948f4be2018-08-22 01:37:55 +0100389#if defined(MBEDTLS_ECDH_C)
390static void mbedtls_debug_printf_ecdh_internal( const mbedtls_ssl_context *ssl,
391 int level, const char *file,
392 int line,
393 const mbedtls_ecdh_context *ecdh,
394 mbedtls_debug_ecdh_attr attr )
395{
396#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
397 const mbedtls_ecdh_context* ctx = ecdh;
398#else
399 const mbedtls_ecdh_context_mbed* ctx = &ecdh->ctx.mbed_ecdh;
400#endif
401
402 switch( attr )
403 {
404 case MBEDTLS_DEBUG_ECDH_Q:
405 mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Q",
406 &ctx->Q );
407 break;
408 case MBEDTLS_DEBUG_ECDH_QP:
409 mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Qp",
410 &ctx->Qp );
411 break;
412 case MBEDTLS_DEBUG_ECDH_Z:
413 mbedtls_debug_print_mpi( ssl, level, file, line, "ECDH: z",
414 &ctx->z );
415 break;
416 default:
417 break;
418 }
419}
420
421void mbedtls_debug_printf_ecdh( const mbedtls_ssl_context *ssl, int level,
422 const char *file, int line,
423 const mbedtls_ecdh_context *ecdh,
424 mbedtls_debug_ecdh_attr attr )
425{
426#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
427 mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh, attr );
428#else
429 switch( ecdh->var )
430 {
431 default:
432 mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh,
433 attr );
434 }
435#endif
436}
437#endif /* MBEDTLS_ECDH_C */
438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439#endif /* MBEDTLS_DEBUG_C */