blob: 5fdb3a08fcc5549cff2bd2cd39afcc47d90514d1 [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
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_DEBUG_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/platform.h"
Rich Evans2387c7d2015-01-30 11:10:20 +000059#else
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020060#include <stdlib.h>
61#define mbedtls_calloc calloc
62#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010063#define mbedtls_time_t time_t
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020064#define mbedtls_snprintf snprintf
Rich Evans2387c7d2015-01-30 11:10:20 +000065#endif
66
SimonBd5800b72016-04-26 07:43:27 +010067#include "mbedtls/debug.h"
68
69#include <stdarg.h>
70#include <stdio.h>
71#include <string.h>
72
Manuel Pégourié-Gonnard0223ab92015-10-05 11:40:01 +010073#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
74 !defined(inline) && !defined(__cplusplus)
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020075#define inline __inline
76#endif
77
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020078#define DEBUG_BUF_SIZE 512
79
Paul Bakkerc73079a2014-04-25 16:34:30 +020080static int debug_threshold = 0;
Paul Bakkereaebbd52014-04-25 15:04:14 +020081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082void mbedtls_debug_set_threshold( int threshold )
Paul Bakkerc73079a2014-04-25 16:34:30 +020083{
84 debug_threshold = threshold;
85}
86
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020087/*
88 * All calls to f_dbg must be made via this function
89 */
90static inline void debug_send_line( const mbedtls_ssl_context *ssl, int level,
91 const char *file, int line,
92 const char *str )
93{
94 /*
95 * If in a threaded environment, we need a thread identifier.
96 * Since there is no portable way to get one, use the address of the ssl
97 * context instead, as it shouldn't be shared between threads.
98 */
99#if defined(MBEDTLS_THREADING_C)
100 char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */
Simon Butcher097618b2016-11-10 17:28:55 +0000101 mbedtls_snprintf( idstr, sizeof( idstr ), "%p: %s", (void*)ssl, str );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200102 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, idstr );
103#else
104 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
105#endif
106}
107
Manuel Pégourié-Gonnarda16e7c42015-06-29 20:14:19 +0200108void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +0200109 const char *file, int line,
110 const char *format, ... )
Paul Bakker5121ce52009-01-03 21:22:43 +0000111{
112 va_list argp;
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +0200113 char str[DEBUG_BUF_SIZE];
114 int ret;
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200115
Hanno Becker99f39162018-06-29 09:04:46 +0100116 if( NULL == ssl ||
117 NULL == ssl->conf ||
118 NULL == ssl->conf->f_dbg ||
119 level > debug_threshold )
120 {
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +0200121 return;
Hanno Becker99f39162018-06-29 09:04:46 +0100122 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000123
124 va_start( argp, format );
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200125#if defined(_WIN32)
Ron Eldorbc18eb32017-09-06 17:49:10 +0300126#if defined(_TRUNCATE) && !defined(__MINGW32__)
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +0200127 ret = _vsnprintf_s( str, DEBUG_BUF_SIZE, _TRUNCATE, format, argp );
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200128#else
Manuel Pégourié-Gonnarda4f055f2015-07-08 16:46:13 +0200129 ret = _vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
130 if( ret < 0 || (size_t) ret == DEBUG_BUF_SIZE )
131 {
132 str[DEBUG_BUF_SIZE-1] = '\0';
133 ret = -1;
134 }
135#endif
136#else
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +0200137 ret = vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200138#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 va_end( argp );
140
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +0200141 if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 )
142 {
143 str[ret] = '\n';
144 str[ret + 1] = '\0';
145 }
146
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200147 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000148}
149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000151 const char *file, int line,
152 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +0000153{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200154 char str[DEBUG_BUF_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +0000155
Hanno Becker99f39162018-06-29 09:04:46 +0100156 if( NULL == ssl ||
157 NULL == ssl->conf ||
158 NULL == ssl->conf->f_dbg ||
159 level > debug_threshold )
160 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 return;
Hanno Becker99f39162018-06-29 09:04:46 +0100162 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200164 /*
165 * With non-blocking I/O and examples that just retry immediately,
166 * the logs would be quickly flooded with WANT_READ, so ignore that.
167 * Don't ignore WANT_WRITE however, since is is usually rare.
168 */
169 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
170 return;
171
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200172 mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200173 text, ret, -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000174
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200175 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000176}
177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000179 const char *file, int line, const char *text,
Manuel Pégourié-Gonnarda78b2182015-03-19 17:16:11 +0000180 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000181{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200182 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100183 char txt[17];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200184 size_t i, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000185
Hanno Becker99f39162018-06-29 09:04:46 +0100186 if( NULL == ssl ||
187 NULL == ssl->conf ||
188 NULL == ssl->conf->f_dbg ||
189 level > debug_threshold )
190 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000191 return;
Hanno Becker99f39162018-06-29 09:04:46 +0100192 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000193
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200194 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200195 text, (unsigned int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000196
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200197 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000198
Paul Bakker92478c32014-04-25 15:18:34 +0200199 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100200 memset( txt, 0, sizeof( txt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000201 for( i = 0; i < len; i++ )
202 {
203 if( i >= 4096 )
204 break;
205
206 if( i % 16 == 0 )
207 {
208 if( i > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200209 {
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200210 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200211 debug_send_line( ssl, level, file, line, str );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100212
Paul Bakker92478c32014-04-25 15:18:34 +0200213 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100214 memset( txt, 0, sizeof( txt ) );
Paul Bakker92478c32014-04-25 15:18:34 +0200215 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000216
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200217 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ",
Paul Bakker92478c32014-04-25 15:18:34 +0200218 (unsigned int) i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000219
Paul Bakker5121ce52009-01-03 21:22:43 +0000220 }
221
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200222 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x",
Paul Bakker92478c32014-04-25 15:18:34 +0200223 (unsigned int) buf[i] );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100224 txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
Paul Bakker5121ce52009-01-03 21:22:43 +0000225 }
226
227 if( len > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200228 {
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100229 for( /* i = i */; i % 16 != 0; i++ )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200230 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100231
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200232 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200233 debug_send_line( ssl, level, file, line, str );
Paul Bakker92478c32014-04-25 15:18:34 +0200234 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000235}
236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#if defined(MBEDTLS_ECP_C)
238void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
Paul Bakker41c83d32013-03-20 14:39:14 +0100239 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 const char *text, const mbedtls_ecp_point *X )
Paul Bakker41c83d32013-03-20 14:39:14 +0100241{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200242 char str[DEBUG_BUF_SIZE];
Paul Bakker41c83d32013-03-20 14:39:14 +0100243
Hanno Becker99f39162018-06-29 09:04:46 +0100244 if( NULL == ssl ||
245 NULL == ssl->conf ||
246 NULL == ssl->conf->f_dbg ||
247 level > debug_threshold )
248 {
Paul Bakkerc73079a2014-04-25 16:34:30 +0200249 return;
Hanno Becker99f39162018-06-29 09:04:46 +0100250 }
Paul Bakkerc73079a2014-04-25 16:34:30 +0200251
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200252 mbedtls_snprintf( str, sizeof( str ), "%s(X)", text );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
Paul Bakker41c83d32013-03-20 14:39:14 +0100254
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200255 mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
Paul Bakker41c83d32013-03-20 14:39:14 +0100257}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258#endif /* MBEDTLS_ECP_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260#if defined(MBEDTLS_BIGNUM_C)
261void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000262 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 const char *text, const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000264{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200265 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200266 int j, k, zeros = 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +0200267 size_t i, n, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000268
Hanno Becker99f39162018-06-29 09:04:46 +0100269 if( NULL == ssl ||
270 NULL == ssl->conf ||
271 NULL == ssl->conf->f_dbg ||
272 NULL == X ||
273 level > debug_threshold )
274 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000275 return;
Hanno Becker99f39162018-06-29 09:04:46 +0100276 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000277
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000278 for( n = X->n - 1; n > 0; n-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 if( X->p[n] != 0 )
280 break;
281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 for( j = ( sizeof(mbedtls_mpi_uint) << 3 ) - 1; j >= 0; j-- )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000283 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
284 break;
285
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200286 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "value of '%s' (%d bits) is:\n",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000288
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200289 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000290
Paul Bakker92478c32014-04-25 15:18:34 +0200291 idx = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000292 for( i = n + 1, j = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000293 {
Paul Bakker23986e52011-04-24 08:57:21 +0000294 if( zeros && X->p[i - 1] == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000295 continue;
Paul Bakker5121ce52009-01-03 21:22:43 +0000296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 for( k = sizeof( mbedtls_mpi_uint ) - 1; k >= 0; k-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200299 if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000300 continue;
301 else
302 zeros = 0;
303
304 if( j % 16 == 0 )
305 {
306 if( j > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200307 {
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200308 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200309 debug_send_line( ssl, level, file, line, str );
Paul Bakker92478c32014-04-25 15:18:34 +0200310 idx = 0;
311 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000312 }
313
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200314 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", (unsigned int)
Paul Bakker66d5d072014-06-17 16:39:18 +0200315 ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000316
317 j++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000318 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000319
320 }
321
322 if( zeros == 1 )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200323 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " 00" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000324
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200325 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200326 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000327}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330#if defined(MBEDTLS_X509_CRT_PARSE_C)
331static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200332 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 const char *text, const mbedtls_pk_context *pk )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200334{
335 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200337 char name[16];
338
339 memset( items, 0, sizeof( items ) );
340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 if( mbedtls_pk_debug( pk, items ) != 0 )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200342 {
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200343 debug_send_line( ssl, level, file, line,
Manuel Pégourié-Gonnard80d627a2015-06-29 20:12:51 +0200344 "invalid PK context\n" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200345 return;
346 }
347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200351 return;
352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200354 name[sizeof( name ) - 1] = '\0';
355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
357 mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200358 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359#if defined(MBEDTLS_ECP_C)
360 if( items[i].type == MBEDTLS_PK_DEBUG_ECP )
361 mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200362 else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200363#endif
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200364 debug_send_line( ssl, level, file, line,
Manuel Pégourié-Gonnard80d627a2015-06-29 20:12:51 +0200365 "should not happen\n" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200366 }
367}
368
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200369static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level,
370 const char *file, int line, const char *text )
371{
372 char str[DEBUG_BUF_SIZE];
373 const char *start, *cur;
374
375 start = text;
376 for( cur = text; *cur != '\0'; cur++ )
377 {
378 if( *cur == '\n' )
379 {
380 size_t len = cur - start + 1;
381 if( len > DEBUG_BUF_SIZE - 1 )
382 len = DEBUG_BUF_SIZE - 1;
383
384 memcpy( str, start, len );
385 str[len] = '\0';
386
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200387 debug_send_line( ssl, level, file, line, str );
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200388
389 start = cur + 1;
390 }
391 }
392}
393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000395 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 const char *text, const mbedtls_x509_crt *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000397{
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200398 char str[DEBUG_BUF_SIZE];
399 int i = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000400
Hanno Becker99f39162018-06-29 09:04:46 +0100401 if( NULL == ssl ||
402 NULL == ssl->conf ||
403 NULL == ssl->conf->f_dbg ||
404 NULL == crt ||
405 level > debug_threshold )
406 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000407 return;
Hanno Becker99f39162018-06-29 09:04:46 +0100408 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000409
Paul Bakker29087132010-03-21 21:03:34 +0000410 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000411 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000412 char buf[1024];
Paul Bakker5121ce52009-01-03 21:22:43 +0000413
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200414 mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200415 debug_send_line( ssl, level, file, line, str );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200416
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200417 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
418 debug_print_line_by_line( ssl, level, file, line, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000419
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200420 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +0000421
422 crt = crt->next;
423 }
424}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427#endif /* MBEDTLS_DEBUG_C */