blob: 6fb766b5ff78c4869442532576bed83ea2f403a4 [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
Gilles Peskinedb09ef62020-06-03 01:43:33 +020022#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if defined(MBEDTLS_DEBUG_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/platform.h"
Rich Evans2387c7d2015-01-30 11:10:20 +000028#else
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020029#include <stdlib.h>
30#define mbedtls_calloc calloc
31#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010032#define mbedtls_time_t time_t
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020033#define mbedtls_snprintf snprintf
k-stachowiak723f8672018-07-16 14:27:07 +020034#define mbedtls_vsnprintf vsnprintf
Rich Evans2387c7d2015-01-30 11:10:20 +000035#endif
36
SimonBd5800b72016-04-26 07:43:27 +010037#include "mbedtls/debug.h"
Janos Follath73c616b2019-12-18 15:07:04 +000038#include "mbedtls/error.h"
SimonBd5800b72016-04-26 07:43:27 +010039
40#include <stdarg.h>
41#include <stdio.h>
42#include <string.h>
43
Manuel Pégourié-Gonnard0223ab92015-10-05 11:40:01 +010044#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
45 !defined(inline) && !defined(__cplusplus)
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020046#define inline __inline
47#endif
48
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020049#define DEBUG_BUF_SIZE 512
50
Paul Bakkerc73079a2014-04-25 16:34:30 +020051static int debug_threshold = 0;
Paul Bakkereaebbd52014-04-25 15:04:14 +020052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053void mbedtls_debug_set_threshold( int threshold )
Paul Bakkerc73079a2014-04-25 16:34:30 +020054{
55 debug_threshold = threshold;
56}
57
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020058/*
59 * All calls to f_dbg must be made via this function
60 */
61static inline void debug_send_line( const mbedtls_ssl_context *ssl, int level,
62 const char *file, int line,
63 const char *str )
64{
65 /*
66 * If in a threaded environment, we need a thread identifier.
67 * Since there is no portable way to get one, use the address of the ssl
68 * context instead, as it shouldn't be shared between threads.
69 */
70#if defined(MBEDTLS_THREADING_C)
71 char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */
Simon Butcher097618b2016-11-10 17:28:55 +000072 mbedtls_snprintf( idstr, sizeof( idstr ), "%p: %s", (void*)ssl, str );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020073 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, idstr );
74#else
75 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
76#endif
77}
78
Manuel Pégourié-Gonnarda16e7c42015-06-29 20:14:19 +020079void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020080 const char *file, int line,
81 const char *format, ... )
Paul Bakker5121ce52009-01-03 21:22:43 +000082{
83 va_list argp;
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020084 char str[DEBUG_BUF_SIZE];
Janos Follath865b3eb2019-12-16 11:46:15 +000085 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020086
Hanno Becker687c5002018-06-29 09:04:46 +010087 if( NULL == ssl ||
88 NULL == ssl->conf ||
89 NULL == ssl->conf->f_dbg ||
90 level > debug_threshold )
91 {
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020092 return;
Hanno Becker687c5002018-06-29 09:04:46 +010093 }
Paul Bakker5121ce52009-01-03 21:22:43 +000094
95 va_start( argp, format );
k-stachowiak723f8672018-07-16 14:27:07 +020096 ret = mbedtls_vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
Paul Bakker5121ce52009-01-03 21:22:43 +000097 va_end( argp );
98
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020099 if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 )
100 {
101 str[ret] = '\n';
102 str[ret + 1] = '\0';
103 }
104
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200105 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000106}
107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000109 const char *file, int line,
110 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +0000111{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200112 char str[DEBUG_BUF_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
Hanno Becker687c5002018-06-29 09:04:46 +0100114 if( NULL == ssl ||
115 NULL == ssl->conf ||
116 NULL == ssl->conf->f_dbg ||
117 level > debug_threshold )
118 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100120 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000121
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200122 /*
123 * With non-blocking I/O and examples that just retry immediately,
124 * the logs would be quickly flooded with WANT_READ, so ignore that.
125 * Don't ignore WANT_WRITE however, since is is usually rare.
126 */
127 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
128 return;
129
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200130 mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n",
Kenneth Soerensen518d4352020-04-01 17:22:45 +0200131 text, ret, (unsigned int) -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000132
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200133 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000134}
135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000137 const char *file, int line, const char *text,
Manuel Pégourié-Gonnarda78b2182015-03-19 17:16:11 +0000138 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000139{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200140 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100141 char txt[17];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200142 size_t i, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000143
Hanno Becker687c5002018-06-29 09:04:46 +0100144 if( NULL == ssl ||
145 NULL == ssl->conf ||
146 NULL == ssl->conf->f_dbg ||
147 level > debug_threshold )
148 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100150 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200152 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200153 text, (unsigned int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200155 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
Paul Bakker92478c32014-04-25 15:18:34 +0200157 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100158 memset( txt, 0, sizeof( txt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 for( i = 0; i < len; i++ )
160 {
161 if( i >= 4096 )
162 break;
163
164 if( i % 16 == 0 )
165 {
166 if( i > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200167 {
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200168 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200169 debug_send_line( ssl, level, file, line, str );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100170
Paul Bakker92478c32014-04-25 15:18:34 +0200171 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100172 memset( txt, 0, sizeof( txt ) );
Paul Bakker92478c32014-04-25 15:18:34 +0200173 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000174
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200175 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ",
Paul Bakker92478c32014-04-25 15:18:34 +0200176 (unsigned int) i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000177
Paul Bakker5121ce52009-01-03 21:22:43 +0000178 }
179
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200180 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x",
Paul Bakker92478c32014-04-25 15:18:34 +0200181 (unsigned int) buf[i] );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100182 txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
Paul Bakker5121ce52009-01-03 21:22:43 +0000183 }
184
185 if( len > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200186 {
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100187 for( /* i = i */; i % 16 != 0; i++ )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200188 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100189
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200190 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200191 debug_send_line( ssl, level, file, line, str );
Paul Bakker92478c32014-04-25 15:18:34 +0200192 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000193}
194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#if defined(MBEDTLS_ECP_C)
196void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
Paul Bakker41c83d32013-03-20 14:39:14 +0100197 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 const char *text, const mbedtls_ecp_point *X )
Paul Bakker41c83d32013-03-20 14:39:14 +0100199{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200200 char str[DEBUG_BUF_SIZE];
Paul Bakker41c83d32013-03-20 14:39:14 +0100201
Hanno Becker687c5002018-06-29 09:04:46 +0100202 if( NULL == ssl ||
203 NULL == ssl->conf ||
204 NULL == ssl->conf->f_dbg ||
205 level > debug_threshold )
206 {
Paul Bakkerc73079a2014-04-25 16:34:30 +0200207 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100208 }
Paul Bakkerc73079a2014-04-25 16:34:30 +0200209
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200210 mbedtls_snprintf( str, sizeof( str ), "%s(X)", text );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
Paul Bakker41c83d32013-03-20 14:39:14 +0100212
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200213 mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
Paul Bakker41c83d32013-03-20 14:39:14 +0100215}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216#endif /* MBEDTLS_ECP_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218#if defined(MBEDTLS_BIGNUM_C)
219void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000220 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 const char *text, const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000222{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200223 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200224 int j, k, zeros = 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +0200225 size_t i, n, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000226
Hanno Becker687c5002018-06-29 09:04:46 +0100227 if( NULL == ssl ||
228 NULL == ssl->conf ||
229 NULL == ssl->conf->f_dbg ||
230 NULL == X ||
231 level > debug_threshold )
232 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000233 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100234 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000235
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000236 for( n = X->n - 1; n > 0; n-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000237 if( X->p[n] != 0 )
238 break;
239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 for( j = ( sizeof(mbedtls_mpi_uint) << 3 ) - 1; j >= 0; j-- )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000241 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
242 break;
243
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200244 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "value of '%s' (%d bits) is:\n",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000246
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200247 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000248
Paul Bakker92478c32014-04-25 15:18:34 +0200249 idx = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000250 for( i = n + 1, j = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000251 {
Paul Bakker23986e52011-04-24 08:57:21 +0000252 if( zeros && X->p[i - 1] == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000253 continue;
Paul Bakker5121ce52009-01-03 21:22:43 +0000254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 for( k = sizeof( mbedtls_mpi_uint ) - 1; k >= 0; k-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000256 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200257 if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000258 continue;
259 else
260 zeros = 0;
261
262 if( j % 16 == 0 )
263 {
264 if( j > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200265 {
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200266 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200267 debug_send_line( ssl, level, file, line, str );
Paul Bakker92478c32014-04-25 15:18:34 +0200268 idx = 0;
269 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000270 }
271
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200272 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", (unsigned int)
Paul Bakker66d5d072014-06-17 16:39:18 +0200273 ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000274
275 j++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000276 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000277
278 }
279
280 if( zeros == 1 )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200281 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " 00" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000282
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200283 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200284 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000285}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288#if defined(MBEDTLS_X509_CRT_PARSE_C)
289static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200290 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 const char *text, const mbedtls_pk_context *pk )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200292{
293 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200295 char name[16];
296
297 memset( items, 0, sizeof( items ) );
298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 if( mbedtls_pk_debug( pk, items ) != 0 )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200300 {
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200301 debug_send_line( ssl, level, file, line,
Manuel Pégourié-Gonnard80d627a2015-06-29 20:12:51 +0200302 "invalid PK context\n" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200303 return;
304 }
305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200309 return;
310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200312 name[sizeof( name ) - 1] = '\0';
313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
315 mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200316 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317#if defined(MBEDTLS_ECP_C)
318 if( items[i].type == MBEDTLS_PK_DEBUG_ECP )
319 mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200320 else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200321#endif
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200322 debug_send_line( ssl, level, file, line,
Manuel Pégourié-Gonnard80d627a2015-06-29 20:12:51 +0200323 "should not happen\n" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200324 }
325}
326
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200327static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level,
328 const char *file, int line, const char *text )
329{
330 char str[DEBUG_BUF_SIZE];
331 const char *start, *cur;
332
333 start = text;
334 for( cur = text; *cur != '\0'; cur++ )
335 {
336 if( *cur == '\n' )
337 {
338 size_t len = cur - start + 1;
339 if( len > DEBUG_BUF_SIZE - 1 )
340 len = DEBUG_BUF_SIZE - 1;
341
342 memcpy( str, start, len );
343 str[len] = '\0';
344
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200345 debug_send_line( ssl, level, file, line, str );
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200346
347 start = cur + 1;
348 }
349 }
350}
351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000353 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 const char *text, const mbedtls_x509_crt *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000355{
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200356 char str[DEBUG_BUF_SIZE];
357 int i = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000358
Hanno Becker687c5002018-06-29 09:04:46 +0100359 if( NULL == ssl ||
360 NULL == ssl->conf ||
361 NULL == ssl->conf->f_dbg ||
362 NULL == crt ||
363 level > debug_threshold )
364 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000365 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100366 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000367
Paul Bakker29087132010-03-21 21:03:34 +0000368 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000369 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000370 char buf[1024];
Paul Bakker5121ce52009-01-03 21:22:43 +0000371
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200372 mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200373 debug_send_line( ssl, level, file, line, str );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200374
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200375 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
376 debug_print_line_by_line( ssl, level, file, line, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000377
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200378 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +0000379
380 crt = crt->next;
381 }
382}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000384
Janos Follath948f4be2018-08-22 01:37:55 +0100385#if defined(MBEDTLS_ECDH_C)
386static void mbedtls_debug_printf_ecdh_internal( const mbedtls_ssl_context *ssl,
387 int level, const char *file,
388 int line,
389 const mbedtls_ecdh_context *ecdh,
390 mbedtls_debug_ecdh_attr attr )
391{
392#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
393 const mbedtls_ecdh_context* ctx = ecdh;
394#else
395 const mbedtls_ecdh_context_mbed* ctx = &ecdh->ctx.mbed_ecdh;
396#endif
397
398 switch( attr )
399 {
400 case MBEDTLS_DEBUG_ECDH_Q:
401 mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Q",
402 &ctx->Q );
403 break;
404 case MBEDTLS_DEBUG_ECDH_QP:
405 mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Qp",
406 &ctx->Qp );
407 break;
408 case MBEDTLS_DEBUG_ECDH_Z:
409 mbedtls_debug_print_mpi( ssl, level, file, line, "ECDH: z",
410 &ctx->z );
411 break;
412 default:
413 break;
414 }
415}
416
417void mbedtls_debug_printf_ecdh( const mbedtls_ssl_context *ssl, int level,
418 const char *file, int line,
419 const mbedtls_ecdh_context *ecdh,
420 mbedtls_debug_ecdh_attr attr )
421{
422#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
423 mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh, attr );
424#else
425 switch( ecdh->var )
426 {
427 default:
428 mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh,
429 attr );
430 }
431#endif
432}
433#endif /* MBEDTLS_ECDH_C */
434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435#endif /* MBEDTLS_DEBUG_C */