blob: fa60d13f3028b14bddfac2da486fe210c713fd09 [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000025#include "mbedtls/platform.h"
Rich Evans2387c7d2015-01-30 11:10:20 +000026#else
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020027#include <stdlib.h>
28#define mbedtls_calloc calloc
29#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010030#define mbedtls_time_t time_t
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020031#define mbedtls_snprintf snprintf
k-stachowiak723f8672018-07-16 14:27:07 +020032#define mbedtls_vsnprintf vsnprintf
Rich Evans2387c7d2015-01-30 11:10:20 +000033#endif
34
SimonBd5800b72016-04-26 07:43:27 +010035#include "mbedtls/debug.h"
Janos Follath73c616b2019-12-18 15:07:04 +000036#include "mbedtls/error.h"
SimonBd5800b72016-04-26 07:43:27 +010037
38#include <stdarg.h>
39#include <stdio.h>
40#include <string.h>
41
Manuel Pégourié-Gonnard0223ab92015-10-05 11:40:01 +010042#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
43 !defined(inline) && !defined(__cplusplus)
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020044#define inline __inline
45#endif
46
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020047#define DEBUG_BUF_SIZE 512
48
Paul Bakkerc73079a2014-04-25 16:34:30 +020049static int debug_threshold = 0;
Paul Bakkereaebbd52014-04-25 15:04:14 +020050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051void mbedtls_debug_set_threshold( int threshold )
Paul Bakkerc73079a2014-04-25 16:34:30 +020052{
53 debug_threshold = threshold;
54}
55
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020056/*
57 * All calls to f_dbg must be made via this function
58 */
59static inline void debug_send_line( const mbedtls_ssl_context *ssl, int level,
60 const char *file, int line,
61 const char *str )
62{
63 /*
64 * If in a threaded environment, we need a thread identifier.
65 * Since there is no portable way to get one, use the address of the ssl
66 * context instead, as it shouldn't be shared between threads.
67 */
68#if defined(MBEDTLS_THREADING_C)
69 char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */
Simon Butcher097618b2016-11-10 17:28:55 +000070 mbedtls_snprintf( idstr, sizeof( idstr ), "%p: %s", (void*)ssl, str );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020071 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, idstr );
72#else
73 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
74#endif
75}
76
Paul Elliott4e589702020-12-09 14:38:01 +000077MBEDTLS_PRINTF_ATTRIBUTE(5, 6)
Manuel Pégourié-Gonnarda16e7c42015-06-29 20:14:19 +020078void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020079 const char *file, int line,
80 const char *format, ... )
Paul Bakker5121ce52009-01-03 21:22:43 +000081{
82 va_list argp;
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020083 char str[DEBUG_BUF_SIZE];
Janos Follath865b3eb2019-12-16 11:46:15 +000084 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020085
Hanno Becker687c5002018-06-29 09:04:46 +010086 if( NULL == ssl ||
87 NULL == ssl->conf ||
88 NULL == ssl->conf->f_dbg ||
89 level > debug_threshold )
90 {
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020091 return;
Hanno Becker687c5002018-06-29 09:04:46 +010092 }
Paul Bakker5121ce52009-01-03 21:22:43 +000093
94 va_start( argp, format );
k-stachowiak723f8672018-07-16 14:27:07 +020095 ret = mbedtls_vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
Paul Bakker5121ce52009-01-03 21:22:43 +000096 va_end( argp );
97
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020098 if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 )
99 {
100 str[ret] = '\n';
101 str[ret + 1] = '\0';
102 }
103
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200104 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000105}
106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000108 const char *file, int line,
109 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +0000110{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200111 char str[DEBUG_BUF_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
Hanno Becker687c5002018-06-29 09:04:46 +0100113 if( NULL == ssl ||
114 NULL == ssl->conf ||
115 NULL == ssl->conf->f_dbg ||
116 level > debug_threshold )
117 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100119 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200121 /*
122 * With non-blocking I/O and examples that just retry immediately,
123 * the logs would be quickly flooded with WANT_READ, so ignore that.
124 * Don't ignore WANT_WRITE however, since is is usually rare.
125 */
126 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
127 return;
128
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200129 mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200130 text, ret, (unsigned int) -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200132 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000133}
134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000136 const char *file, int line, const char *text,
Manuel Pégourié-Gonnarda78b2182015-03-19 17:16:11 +0000137 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000138{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200139 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100140 char txt[17];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200141 size_t i, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
Hanno Becker687c5002018-06-29 09:04:46 +0100143 if( NULL == ssl ||
144 NULL == ssl->conf ||
145 NULL == ssl->conf->f_dbg ||
146 level > debug_threshold )
147 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100149 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200151 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200152 text, (unsigned int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200154 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000155
Paul Bakker92478c32014-04-25 15:18:34 +0200156 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100157 memset( txt, 0, sizeof( txt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 for( i = 0; i < len; i++ )
159 {
160 if( i >= 4096 )
161 break;
162
163 if( i % 16 == 0 )
164 {
165 if( i > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200166 {
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200167 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200168 debug_send_line( ssl, level, file, line, str );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100169
Paul Bakker92478c32014-04-25 15:18:34 +0200170 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100171 memset( txt, 0, sizeof( txt ) );
Paul Bakker92478c32014-04-25 15:18:34 +0200172 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000173
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200174 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ",
Paul Bakker92478c32014-04-25 15:18:34 +0200175 (unsigned int) i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000176
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 }
178
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200179 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x",
Paul Bakker92478c32014-04-25 15:18:34 +0200180 (unsigned int) buf[i] );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100181 txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
Paul Bakker5121ce52009-01-03 21:22:43 +0000182 }
183
184 if( len > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200185 {
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100186 for( /* i = i */; i % 16 != 0; i++ )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200187 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100188
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200189 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200190 debug_send_line( ssl, level, file, line, str );
Paul Bakker92478c32014-04-25 15:18:34 +0200191 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000192}
193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#if defined(MBEDTLS_ECP_C)
195void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
Paul Bakker41c83d32013-03-20 14:39:14 +0100196 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 const char *text, const mbedtls_ecp_point *X )
Paul Bakker41c83d32013-03-20 14:39:14 +0100198{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200199 char str[DEBUG_BUF_SIZE];
Paul Bakker41c83d32013-03-20 14:39:14 +0100200
Hanno Becker687c5002018-06-29 09:04:46 +0100201 if( NULL == ssl ||
202 NULL == ssl->conf ||
203 NULL == ssl->conf->f_dbg ||
204 level > debug_threshold )
205 {
Paul Bakkerc73079a2014-04-25 16:34:30 +0200206 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100207 }
Paul Bakkerc73079a2014-04-25 16:34:30 +0200208
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200209 mbedtls_snprintf( str, sizeof( str ), "%s(X)", text );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
Paul Bakker41c83d32013-03-20 14:39:14 +0100211
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200212 mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
Paul Bakker41c83d32013-03-20 14:39:14 +0100214}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215#endif /* MBEDTLS_ECP_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217#if defined(MBEDTLS_BIGNUM_C)
218void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000219 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 const char *text, const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000221{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200222 char str[DEBUG_BUF_SIZE];
Gilles Peskineb26696b2021-06-02 20:17:46 +0200223 size_t bitlen;
224 size_t idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000225
Hanno Becker687c5002018-06-29 09:04:46 +0100226 if( NULL == ssl ||
227 NULL == ssl->conf ||
228 NULL == ssl->conf->f_dbg ||
229 NULL == X ||
230 level > debug_threshold )
231 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100233 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000234
Gilles Peskineb26696b2021-06-02 20:17:46 +0200235 bitlen = mbedtls_mpi_bitlen( X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000236
Gilles Peskineb26696b2021-06-02 20:17:46 +0200237 mbedtls_snprintf( str, sizeof( str ), "value of '%s' (%u bits) is:\n",
238 text, (unsigned) bitlen );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200239 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000240
Gilles Peskineb26696b2021-06-02 20:17:46 +0200241 if( bitlen == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000242 {
Gilles Peskineb26696b2021-06-02 20:17:46 +0200243 str[0] = ' '; str[1] = '0'; str[2] = '0';
244 idx = 3;
245 }
246 else
247 {
248 int n;
Gilles Peskine55cb9af2021-06-07 20:56:20 +0200249 for( n = (int) ( ( bitlen - 1 ) / 8 ); n >= 0; n-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000250 {
Gilles Peskineb26696b2021-06-02 20:17:46 +0200251 size_t limb_offset = n / sizeof( mbedtls_mpi_uint );
252 size_t offset_in_limb = n % sizeof( mbedtls_mpi_uint );
253 unsigned char octet =
254 ( X->p[limb_offset] >> ( offset_in_limb * 8 ) ) & 0xff;
255 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", octet );
256 idx += 3;
257 /* Wrap lines after 16 octets that each take 3 columns */
258 if( idx >= 3 * 16 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000259 {
Gilles Peskineb26696b2021-06-02 20:17:46 +0200260 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
261 debug_send_line( ssl, level, file, line, str );
262 idx = 0;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000263 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000264 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000265 }
266
Gilles Peskineb26696b2021-06-02 20:17:46 +0200267 if( idx != 0 )
268 {
269 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
270 debug_send_line( ssl, level, file, line, str );
271 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000272}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000274
Hanno Becker612a2f12020-10-09 09:19:39 +0100275#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200277 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 const char *text, const mbedtls_pk_context *pk )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200279{
280 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200282 char name[16];
283
284 memset( items, 0, sizeof( items ) );
285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 if( mbedtls_pk_debug( pk, items ) != 0 )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200287 {
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200288 debug_send_line( ssl, level, file, line,
Manuel Pégourié-Gonnard80d627a2015-06-29 20:12:51 +0200289 "invalid PK context\n" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200290 return;
291 }
292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200296 return;
297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200299 name[sizeof( name ) - 1] = '\0';
300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
302 mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200303 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#if defined(MBEDTLS_ECP_C)
305 if( items[i].type == MBEDTLS_PK_DEBUG_ECP )
306 mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200307 else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200308#endif
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200309 debug_send_line( ssl, level, file, line,
Manuel Pégourié-Gonnard80d627a2015-06-29 20:12:51 +0200310 "should not happen\n" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200311 }
312}
313
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200314static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level,
315 const char *file, int line, const char *text )
316{
317 char str[DEBUG_BUF_SIZE];
318 const char *start, *cur;
319
320 start = text;
321 for( cur = text; *cur != '\0'; cur++ )
322 {
323 if( *cur == '\n' )
324 {
325 size_t len = cur - start + 1;
326 if( len > DEBUG_BUF_SIZE - 1 )
327 len = DEBUG_BUF_SIZE - 1;
328
329 memcpy( str, start, len );
330 str[len] = '\0';
331
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200332 debug_send_line( ssl, level, file, line, str );
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200333
334 start = cur + 1;
335 }
336 }
337}
338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000340 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 const char *text, const mbedtls_x509_crt *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000342{
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200343 char str[DEBUG_BUF_SIZE];
344 int i = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000345
Hanno Becker687c5002018-06-29 09:04:46 +0100346 if( NULL == ssl ||
347 NULL == ssl->conf ||
348 NULL == ssl->conf->f_dbg ||
349 NULL == crt ||
350 level > debug_threshold )
351 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000352 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100353 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000354
Paul Bakker29087132010-03-21 21:03:34 +0000355 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000356 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000357 char buf[1024];
Paul Bakker5121ce52009-01-03 21:22:43 +0000358
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200359 mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200360 debug_send_line( ssl, level, file, line, str );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200361
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200362 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
363 debug_print_line_by_line( ssl, level, file, line, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000364
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200365 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +0000366
367 crt = crt->next;
368 }
369}
Hanno Becker612a2f12020-10-09 09:19:39 +0100370#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_X509_REMOVE_INFO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000371
Janos Follath948f4be2018-08-22 01:37:55 +0100372#if defined(MBEDTLS_ECDH_C)
373static void mbedtls_debug_printf_ecdh_internal( const mbedtls_ssl_context *ssl,
374 int level, const char *file,
375 int line,
376 const mbedtls_ecdh_context *ecdh,
377 mbedtls_debug_ecdh_attr attr )
378{
379#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
380 const mbedtls_ecdh_context* ctx = ecdh;
381#else
382 const mbedtls_ecdh_context_mbed* ctx = &ecdh->ctx.mbed_ecdh;
383#endif
384
385 switch( attr )
386 {
387 case MBEDTLS_DEBUG_ECDH_Q:
388 mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Q",
389 &ctx->Q );
390 break;
391 case MBEDTLS_DEBUG_ECDH_QP:
392 mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Qp",
393 &ctx->Qp );
394 break;
395 case MBEDTLS_DEBUG_ECDH_Z:
396 mbedtls_debug_print_mpi( ssl, level, file, line, "ECDH: z",
397 &ctx->z );
398 break;
399 default:
400 break;
401 }
402}
403
404void mbedtls_debug_printf_ecdh( const mbedtls_ssl_context *ssl, int level,
405 const char *file, int line,
406 const mbedtls_ecdh_context *ecdh,
407 mbedtls_debug_ecdh_attr attr )
408{
409#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
410 mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh, attr );
411#else
412 switch( ecdh->var )
413 {
414 default:
415 mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh,
416 attr );
417 }
418#endif
419}
420#endif /* MBEDTLS_ECDH_C */
421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422#endif /* MBEDTLS_DEBUG_C */