blob: bdbf6dd11ea5cdce34d4cb9c2ae223573a864327 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Debugging routines
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
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 Bakker5121ce52009-01-03 21:22:43 +000018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_DEBUG_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/platform.h"
Rich Evans2387c7d2015-01-30 11:10:20 +000025
SimonBd5800b72016-04-26 07:43:27 +010026#include "mbedtls/debug.h"
Janos Follath73c616b2019-12-18 15:07:04 +000027#include "mbedtls/error.h"
SimonBd5800b72016-04-26 07:43:27 +010028
29#include <stdarg.h>
30#include <stdio.h>
31#include <string.h>
32
Manuel Pégourié-Gonnard0223ab92015-10-05 11:40:01 +010033#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
34 !defined(inline) && !defined(__cplusplus)
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020035#define inline __inline
36#endif
37
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020038#define DEBUG_BUF_SIZE 512
39
Paul Bakkerc73079a2014-04-25 16:34:30 +020040static int debug_threshold = 0;
Paul Bakkereaebbd52014-04-25 15:04:14 +020041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042void mbedtls_debug_set_threshold( int threshold )
Paul Bakkerc73079a2014-04-25 16:34:30 +020043{
44 debug_threshold = threshold;
45}
46
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020047/*
48 * All calls to f_dbg must be made via this function
49 */
50static inline void debug_send_line( const mbedtls_ssl_context *ssl, int level,
51 const char *file, int line,
52 const char *str )
53{
54 /*
55 * If in a threaded environment, we need a thread identifier.
56 * Since there is no portable way to get one, use the address of the ssl
57 * context instead, as it shouldn't be shared between threads.
58 */
59#if defined(MBEDTLS_THREADING_C)
60 char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */
Simon Butcher097618b2016-11-10 17:28:55 +000061 mbedtls_snprintf( idstr, sizeof( idstr ), "%p: %s", (void*)ssl, str );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020062 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, idstr );
63#else
64 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
65#endif
66}
67
Paul Elliott4e589702020-12-09 14:38:01 +000068MBEDTLS_PRINTF_ATTRIBUTE(5, 6)
Manuel Pégourié-Gonnarda16e7c42015-06-29 20:14:19 +020069void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020070 const char *file, int line,
71 const char *format, ... )
Paul Bakker5121ce52009-01-03 21:22:43 +000072{
73 va_list argp;
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020074 char str[DEBUG_BUF_SIZE];
Janos Follath865b3eb2019-12-16 11:46:15 +000075 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020076
Hanno Becker687c5002018-06-29 09:04:46 +010077 if( NULL == ssl ||
78 NULL == ssl->conf ||
79 NULL == ssl->conf->f_dbg ||
80 level > debug_threshold )
81 {
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020082 return;
Hanno Becker687c5002018-06-29 09:04:46 +010083 }
Paul Bakker5121ce52009-01-03 21:22:43 +000084
85 va_start( argp, format );
k-stachowiak723f8672018-07-16 14:27:07 +020086 ret = mbedtls_vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
Paul Bakker5121ce52009-01-03 21:22:43 +000087 va_end( argp );
88
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020089 if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 )
90 {
91 str[ret] = '\n';
92 str[ret + 1] = '\0';
93 }
94
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020095 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +000096}
97
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +000099 const char *file, int line,
100 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +0000101{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200102 char str[DEBUG_BUF_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
Hanno Becker687c5002018-06-29 09:04:46 +0100104 if( NULL == ssl ||
105 NULL == ssl->conf ||
106 NULL == ssl->conf->f_dbg ||
107 level > debug_threshold )
108 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100110 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200112 /*
113 * With non-blocking I/O and examples that just retry immediately,
114 * the logs would be quickly flooded with WANT_READ, so ignore that.
115 * Don't ignore WANT_WRITE however, since is is usually rare.
116 */
117 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
118 return;
119
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200120 mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200121 text, ret, (unsigned int) -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000122
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200123 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000124}
125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000127 const char *file, int line, const char *text,
Manuel Pégourié-Gonnarda78b2182015-03-19 17:16:11 +0000128 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000129{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200130 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100131 char txt[17];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200132 size_t i, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000133
Hanno Becker687c5002018-06-29 09:04:46 +0100134 if( NULL == ssl ||
135 NULL == ssl->conf ||
136 NULL == ssl->conf->f_dbg ||
137 level > debug_threshold )
138 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100140 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200142 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200143 text, (unsigned int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000144
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200145 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
Paul Bakker92478c32014-04-25 15:18:34 +0200147 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100148 memset( txt, 0, sizeof( txt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 for( i = 0; i < len; i++ )
150 {
151 if( i >= 4096 )
152 break;
153
154 if( i % 16 == 0 )
155 {
156 if( i > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200157 {
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200158 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200159 debug_send_line( ssl, level, file, line, str );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100160
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 Bakker92478c32014-04-25 15:18:34 +0200163 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000164
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200165 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ",
Paul Bakker92478c32014-04-25 15:18:34 +0200166 (unsigned int) i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
Paul Bakker5121ce52009-01-03 21:22:43 +0000168 }
169
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200170 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x",
Paul Bakker92478c32014-04-25 15:18:34 +0200171 (unsigned int) buf[i] );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100172 txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 }
174
175 if( len > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200176 {
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100177 for( /* i = i */; i % 16 != 0; i++ )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200178 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100179
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200180 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200181 debug_send_line( ssl, level, file, line, str );
Paul Bakker92478c32014-04-25 15:18:34 +0200182 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000183}
184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185#if defined(MBEDTLS_ECP_C)
186void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
Paul Bakker41c83d32013-03-20 14:39:14 +0100187 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 const char *text, const mbedtls_ecp_point *X )
Paul Bakker41c83d32013-03-20 14:39:14 +0100189{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200190 char str[DEBUG_BUF_SIZE];
Paul Bakker41c83d32013-03-20 14:39:14 +0100191
Hanno Becker687c5002018-06-29 09:04:46 +0100192 if( NULL == ssl ||
193 NULL == ssl->conf ||
194 NULL == ssl->conf->f_dbg ||
195 level > debug_threshold )
196 {
Paul Bakkerc73079a2014-04-25 16:34:30 +0200197 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100198 }
Paul Bakkerc73079a2014-04-25 16:34:30 +0200199
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200200 mbedtls_snprintf( str, sizeof( str ), "%s(X)", text );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
Paul Bakker41c83d32013-03-20 14:39:14 +0100202
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200203 mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
Paul Bakker41c83d32013-03-20 14:39:14 +0100205}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#endif /* MBEDTLS_ECP_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208#if defined(MBEDTLS_BIGNUM_C)
209void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000210 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 const char *text, const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000212{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200213 char str[DEBUG_BUF_SIZE];
Gilles Peskineb26696b2021-06-02 20:17:46 +0200214 size_t bitlen;
215 size_t idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000216
Hanno Becker687c5002018-06-29 09:04:46 +0100217 if( NULL == ssl ||
218 NULL == ssl->conf ||
219 NULL == ssl->conf->f_dbg ||
220 NULL == X ||
221 level > debug_threshold )
222 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000223 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100224 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000225
Gilles Peskineb26696b2021-06-02 20:17:46 +0200226 bitlen = mbedtls_mpi_bitlen( X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000227
Gilles Peskineb26696b2021-06-02 20:17:46 +0200228 mbedtls_snprintf( str, sizeof( str ), "value of '%s' (%u bits) is:\n",
229 text, (unsigned) bitlen );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200230 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
Gilles Peskineb26696b2021-06-02 20:17:46 +0200232 if( bitlen == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000233 {
Gilles Peskineb26696b2021-06-02 20:17:46 +0200234 str[0] = ' '; str[1] = '0'; str[2] = '0';
235 idx = 3;
236 }
237 else
238 {
239 int n;
Gilles Peskine55cb9af2021-06-07 20:56:20 +0200240 for( n = (int) ( ( bitlen - 1 ) / 8 ); n >= 0; n-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000241 {
Gilles Peskineb26696b2021-06-02 20:17:46 +0200242 size_t limb_offset = n / sizeof( mbedtls_mpi_uint );
243 size_t offset_in_limb = n % sizeof( mbedtls_mpi_uint );
244 unsigned char octet =
245 ( X->p[limb_offset] >> ( offset_in_limb * 8 ) ) & 0xff;
246 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", octet );
247 idx += 3;
248 /* Wrap lines after 16 octets that each take 3 columns */
249 if( idx >= 3 * 16 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000250 {
Gilles Peskineb26696b2021-06-02 20:17:46 +0200251 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
252 debug_send_line( ssl, level, file, line, str );
253 idx = 0;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000254 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000256 }
257
Gilles Peskineb26696b2021-06-02 20:17:46 +0200258 if( idx != 0 )
259 {
260 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
261 debug_send_line( ssl, level, file, line, str );
262 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000263}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000265
Hanno Becker612a2f12020-10-09 09:19:39 +0100266#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200268 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 const char *text, const mbedtls_pk_context *pk )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200270{
271 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200273 char name[16];
274
275 memset( items, 0, sizeof( items ) );
276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 if( mbedtls_pk_debug( pk, items ) != 0 )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200278 {
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200279 debug_send_line( ssl, level, file, line,
Manuel Pégourié-Gonnard80d627a2015-06-29 20:12:51 +0200280 "invalid PK context\n" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200281 return;
282 }
283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200287 return;
288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200290 name[sizeof( name ) - 1] = '\0';
291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
293 mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200294 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295#if defined(MBEDTLS_ECP_C)
296 if( items[i].type == MBEDTLS_PK_DEBUG_ECP )
297 mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200298 else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200299#endif
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200300 debug_send_line( ssl, level, file, line,
Manuel Pégourié-Gonnard80d627a2015-06-29 20:12:51 +0200301 "should not happen\n" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200302 }
303}
304
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200305static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level,
306 const char *file, int line, const char *text )
307{
308 char str[DEBUG_BUF_SIZE];
309 const char *start, *cur;
310
311 start = text;
312 for( cur = text; *cur != '\0'; cur++ )
313 {
314 if( *cur == '\n' )
315 {
316 size_t len = cur - start + 1;
317 if( len > DEBUG_BUF_SIZE - 1 )
318 len = DEBUG_BUF_SIZE - 1;
319
320 memcpy( str, start, len );
321 str[len] = '\0';
322
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200323 debug_send_line( ssl, level, file, line, str );
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200324
325 start = cur + 1;
326 }
327 }
328}
329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000331 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 const char *text, const mbedtls_x509_crt *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000333{
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200334 char str[DEBUG_BUF_SIZE];
335 int i = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000336
Hanno Becker687c5002018-06-29 09:04:46 +0100337 if( NULL == ssl ||
338 NULL == ssl->conf ||
339 NULL == ssl->conf->f_dbg ||
340 NULL == crt ||
341 level > debug_threshold )
342 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000343 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100344 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000345
Paul Bakker29087132010-03-21 21:03:34 +0000346 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000347 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000348 char buf[1024];
Paul Bakker5121ce52009-01-03 21:22:43 +0000349
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200350 mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200351 debug_send_line( ssl, level, file, line, str );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200352
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200353 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
354 debug_print_line_by_line( ssl, level, file, line, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000355
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200356 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +0000357
358 crt = crt->next;
359 }
360}
Hanno Becker612a2f12020-10-09 09:19:39 +0100361#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_X509_REMOVE_INFO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000362
Janos Follath948f4be2018-08-22 01:37:55 +0100363#if defined(MBEDTLS_ECDH_C)
364static void mbedtls_debug_printf_ecdh_internal( const mbedtls_ssl_context *ssl,
365 int level, const char *file,
366 int line,
367 const mbedtls_ecdh_context *ecdh,
368 mbedtls_debug_ecdh_attr attr )
369{
370#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
371 const mbedtls_ecdh_context* ctx = ecdh;
372#else
373 const mbedtls_ecdh_context_mbed* ctx = &ecdh->ctx.mbed_ecdh;
374#endif
375
376 switch( attr )
377 {
378 case MBEDTLS_DEBUG_ECDH_Q:
379 mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Q",
380 &ctx->Q );
381 break;
382 case MBEDTLS_DEBUG_ECDH_QP:
383 mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Qp",
384 &ctx->Qp );
385 break;
386 case MBEDTLS_DEBUG_ECDH_Z:
387 mbedtls_debug_print_mpi( ssl, level, file, line, "ECDH: z",
388 &ctx->z );
389 break;
390 default:
391 break;
392 }
393}
394
395void mbedtls_debug_printf_ecdh( const mbedtls_ssl_context *ssl, int level,
396 const char *file, int line,
397 const mbedtls_ecdh_context *ecdh,
398 mbedtls_debug_ecdh_attr attr )
399{
400#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
401 mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh, attr );
402#else
403 switch( ecdh->var )
404 {
405 default:
406 mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh,
407 attr );
408 }
409#endif
410}
411#endif /* MBEDTLS_ECDH_C */
412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413#endif /* MBEDTLS_DEBUG_C */