blob: 9caa361d448effced57dc3d01aefbf8221a84483 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Debugging routines
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-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útif744bd72020-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 * **********
Paul Bakker5121ce52009-01-03 21:22:43 +000045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_DEBUG_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/platform.h"
Rich Evans2387c7d2015-01-30 11:10:20 +000057#else
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020058#include <stdlib.h>
59#define mbedtls_calloc calloc
60#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010061#define mbedtls_time_t time_t
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020062#define mbedtls_snprintf snprintf
Rich Evans2387c7d2015-01-30 11:10:20 +000063#endif
64
SimonBd5800b72016-04-26 07:43:27 +010065#include "mbedtls/debug.h"
66
67#include <stdarg.h>
68#include <stdio.h>
69#include <string.h>
70
Manuel Pégourié-Gonnard0223ab92015-10-05 11:40:01 +010071#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
72 !defined(inline) && !defined(__cplusplus)
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020073#define inline __inline
74#endif
75
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020076#define DEBUG_BUF_SIZE 512
77
Paul Bakkerc73079a2014-04-25 16:34:30 +020078static int debug_threshold = 0;
Paul Bakkereaebbd52014-04-25 15:04:14 +020079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080void mbedtls_debug_set_threshold( int threshold )
Paul Bakkerc73079a2014-04-25 16:34:30 +020081{
82 debug_threshold = threshold;
83}
84
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020085/*
86 * All calls to f_dbg must be made via this function
87 */
88static inline void debug_send_line( const mbedtls_ssl_context *ssl, int level,
89 const char *file, int line,
90 const char *str )
91{
92 /*
93 * If in a threaded environment, we need a thread identifier.
94 * Since there is no portable way to get one, use the address of the ssl
95 * context instead, as it shouldn't be shared between threads.
96 */
97#if defined(MBEDTLS_THREADING_C)
98 char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */
Simon Butcher097618b2016-11-10 17:28:55 +000099 mbedtls_snprintf( idstr, sizeof( idstr ), "%p: %s", (void*)ssl, str );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200100 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, idstr );
101#else
102 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
103#endif
104}
105
Manuel Pégourié-Gonnarda16e7c42015-06-29 20:14:19 +0200106void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +0200107 const char *file, int line,
108 const char *format, ... )
Paul Bakker5121ce52009-01-03 21:22:43 +0000109{
110 va_list argp;
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +0200111 char str[DEBUG_BUF_SIZE];
112 int ret;
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200113
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 {
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +0200119 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100120 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000121
122 va_start( argp, format );
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200123#if defined(_WIN32)
Ron Eldorbc18eb32017-09-06 17:49:10 +0300124#if defined(_TRUNCATE) && !defined(__MINGW32__)
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +0200125 ret = _vsnprintf_s( str, DEBUG_BUF_SIZE, _TRUNCATE, format, argp );
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200126#else
Manuel Pégourié-Gonnarda4f055f2015-07-08 16:46:13 +0200127 ret = _vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
128 if( ret < 0 || (size_t) ret == DEBUG_BUF_SIZE )
129 {
130 str[DEBUG_BUF_SIZE-1] = '\0';
131 ret = -1;
132 }
133#endif
134#else
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +0200135 ret = vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200136#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 va_end( argp );
138
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +0200139 if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 )
140 {
141 str[ret] = '\n';
142 str[ret + 1] = '\0';
143 }
144
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}
147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000149 const char *file, int line,
150 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +0000151{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200152 char str[DEBUG_BUF_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
Hanno Becker687c5002018-06-29 09:04:46 +0100154 if( NULL == ssl ||
155 NULL == ssl->conf ||
156 NULL == ssl->conf->f_dbg ||
157 level > debug_threshold )
158 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100160 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000161
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200162 /*
163 * With non-blocking I/O and examples that just retry immediately,
164 * the logs would be quickly flooded with WANT_READ, so ignore that.
165 * Don't ignore WANT_WRITE however, since is is usually rare.
166 */
167 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
168 return;
169
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200170 mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200171 text, ret, -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000172
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200173 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000174}
175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000177 const char *file, int line, const char *text,
Manuel Pégourié-Gonnarda78b2182015-03-19 17:16:11 +0000178 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000179{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200180 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100181 char txt[17];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200182 size_t i, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
Hanno Becker687c5002018-06-29 09:04:46 +0100184 if( NULL == ssl ||
185 NULL == ssl->conf ||
186 NULL == ssl->conf->f_dbg ||
187 level > debug_threshold )
188 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000189 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100190 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000191
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200192 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200193 text, (unsigned int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000194
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200195 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000196
Paul Bakker92478c32014-04-25 15:18:34 +0200197 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100198 memset( txt, 0, sizeof( txt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 for( i = 0; i < len; i++ )
200 {
201 if( i >= 4096 )
202 break;
203
204 if( i % 16 == 0 )
205 {
206 if( i > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200207 {
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200208 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200209 debug_send_line( ssl, level, file, line, str );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100210
Paul Bakker92478c32014-04-25 15:18:34 +0200211 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100212 memset( txt, 0, sizeof( txt ) );
Paul Bakker92478c32014-04-25 15:18:34 +0200213 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000214
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200215 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ",
Paul Bakker92478c32014-04-25 15:18:34 +0200216 (unsigned int) i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000217
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 }
219
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200220 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x",
Paul Bakker92478c32014-04-25 15:18:34 +0200221 (unsigned int) buf[i] );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100222 txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
Paul Bakker5121ce52009-01-03 21:22:43 +0000223 }
224
225 if( len > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200226 {
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100227 for( /* i = i */; i % 16 != 0; i++ )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200228 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100229
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200230 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200231 debug_send_line( ssl, level, file, line, str );
Paul Bakker92478c32014-04-25 15:18:34 +0200232 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000233}
234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235#if defined(MBEDTLS_ECP_C)
236void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
Paul Bakker41c83d32013-03-20 14:39:14 +0100237 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 const char *text, const mbedtls_ecp_point *X )
Paul Bakker41c83d32013-03-20 14:39:14 +0100239{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200240 char str[DEBUG_BUF_SIZE];
Paul Bakker41c83d32013-03-20 14:39:14 +0100241
Hanno Becker687c5002018-06-29 09:04:46 +0100242 if( NULL == ssl ||
243 NULL == ssl->conf ||
244 NULL == ssl->conf->f_dbg ||
245 level > debug_threshold )
246 {
Paul Bakkerc73079a2014-04-25 16:34:30 +0200247 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100248 }
Paul Bakkerc73079a2014-04-25 16:34:30 +0200249
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200250 mbedtls_snprintf( str, sizeof( str ), "%s(X)", text );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
Paul Bakker41c83d32013-03-20 14:39:14 +0100252
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200253 mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
Paul Bakker41c83d32013-03-20 14:39:14 +0100255}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256#endif /* MBEDTLS_ECP_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258#if defined(MBEDTLS_BIGNUM_C)
259void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000260 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 const char *text, const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000262{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200263 char str[DEBUG_BUF_SIZE];
Gilles Peskinee1a31282021-06-02 20:17:46 +0200264 size_t bitlen;
265 size_t idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000266
Hanno Becker687c5002018-06-29 09:04:46 +0100267 if( NULL == ssl ||
268 NULL == ssl->conf ||
269 NULL == ssl->conf->f_dbg ||
270 NULL == X ||
271 level > debug_threshold )
272 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000273 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100274 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000275
Gilles Peskinee1a31282021-06-02 20:17:46 +0200276 bitlen = mbedtls_mpi_bitlen( X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000277
Gilles Peskinee1a31282021-06-02 20:17:46 +0200278 mbedtls_snprintf( str, sizeof( str ), "value of '%s' (%u bits) is:\n",
279 text, (unsigned) bitlen );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200280 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000281
Gilles Peskinee1a31282021-06-02 20:17:46 +0200282 if( bitlen == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000283 {
Gilles Peskinee1a31282021-06-02 20:17:46 +0200284 str[0] = ' '; str[1] = '0'; str[2] = '0';
285 idx = 3;
286 }
287 else
288 {
289 int n;
Gilles Peskine3db875e2021-06-07 20:56:20 +0200290 for( n = (int) ( ( bitlen - 1 ) / 8 ); n >= 0; n-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000291 {
Gilles Peskinee1a31282021-06-02 20:17:46 +0200292 size_t limb_offset = n / sizeof( mbedtls_mpi_uint );
293 size_t offset_in_limb = n % sizeof( mbedtls_mpi_uint );
294 unsigned char octet =
295 ( X->p[limb_offset] >> ( offset_in_limb * 8 ) ) & 0xff;
296 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", octet );
297 idx += 3;
298 /* Wrap lines after 16 octets that each take 3 columns */
299 if( idx >= 3 * 16 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000300 {
Gilles Peskinee1a31282021-06-02 20:17:46 +0200301 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
302 debug_send_line( ssl, level, file, line, str );
303 idx = 0;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000304 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000305 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000306 }
307
Gilles Peskinee1a31282021-06-02 20:17:46 +0200308 if( idx != 0 )
309 {
310 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
311 debug_send_line( ssl, level, file, line, str );
312 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000313}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316#if defined(MBEDTLS_X509_CRT_PARSE_C)
317static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200318 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 const char *text, const mbedtls_pk_context *pk )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200320{
321 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200323 char name[16];
324
325 memset( items, 0, sizeof( items ) );
326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 if( mbedtls_pk_debug( pk, items ) != 0 )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200328 {
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200329 debug_send_line( ssl, level, file, line,
Manuel Pégourié-Gonnard80d627a2015-06-29 20:12:51 +0200330 "invalid PK context\n" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200331 return;
332 }
333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200337 return;
338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200340 name[sizeof( name ) - 1] = '\0';
341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
343 mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200344 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345#if defined(MBEDTLS_ECP_C)
346 if( items[i].type == MBEDTLS_PK_DEBUG_ECP )
347 mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200348 else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200349#endif
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200350 debug_send_line( ssl, level, file, line,
Manuel Pégourié-Gonnard80d627a2015-06-29 20:12:51 +0200351 "should not happen\n" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200352 }
353}
354
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200355static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level,
356 const char *file, int line, const char *text )
357{
358 char str[DEBUG_BUF_SIZE];
359 const char *start, *cur;
360
361 start = text;
362 for( cur = text; *cur != '\0'; cur++ )
363 {
364 if( *cur == '\n' )
365 {
366 size_t len = cur - start + 1;
367 if( len > DEBUG_BUF_SIZE - 1 )
368 len = DEBUG_BUF_SIZE - 1;
369
370 memcpy( str, start, len );
371 str[len] = '\0';
372
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200373 debug_send_line( ssl, level, file, line, str );
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200374
375 start = cur + 1;
376 }
377 }
378}
379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000381 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 const char *text, const mbedtls_x509_crt *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000383{
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200384 char str[DEBUG_BUF_SIZE];
385 int i = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000386
Hanno Becker687c5002018-06-29 09:04:46 +0100387 if( NULL == ssl ||
388 NULL == ssl->conf ||
389 NULL == ssl->conf->f_dbg ||
390 NULL == crt ||
391 level > debug_threshold )
392 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000393 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100394 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000395
Paul Bakker29087132010-03-21 21:03:34 +0000396 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000397 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000398 char buf[1024];
Paul Bakker5121ce52009-01-03 21:22:43 +0000399
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200400 mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200401 debug_send_line( ssl, level, file, line, str );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200402
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200403 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
404 debug_print_line_by_line( ssl, level, file, line, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000405
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200406 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +0000407
408 crt = crt->next;
409 }
410}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000412
Janos Follath948f4be2018-08-22 01:37:55 +0100413#if defined(MBEDTLS_ECDH_C)
414static void mbedtls_debug_printf_ecdh_internal( const mbedtls_ssl_context *ssl,
415 int level, const char *file,
416 int line,
417 const mbedtls_ecdh_context *ecdh,
418 mbedtls_debug_ecdh_attr attr )
419{
420#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
421 const mbedtls_ecdh_context* ctx = ecdh;
422#else
423 const mbedtls_ecdh_context_mbed* ctx = &ecdh->ctx.mbed_ecdh;
424#endif
425
426 switch( attr )
427 {
428 case MBEDTLS_DEBUG_ECDH_Q:
429 mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Q",
430 &ctx->Q );
431 break;
432 case MBEDTLS_DEBUG_ECDH_QP:
433 mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Qp",
434 &ctx->Qp );
435 break;
436 case MBEDTLS_DEBUG_ECDH_Z:
437 mbedtls_debug_print_mpi( ssl, level, file, line, "ECDH: z",
438 &ctx->z );
439 break;
440 default:
441 break;
442 }
443}
444
445void mbedtls_debug_printf_ecdh( const mbedtls_ssl_context *ssl, int level,
446 const char *file, int line,
447 const mbedtls_ecdh_context *ecdh,
448 mbedtls_debug_ecdh_attr attr )
449{
450#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
451 mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh, attr );
452#else
453 switch( ecdh->var )
454 {
455 default:
456 mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh,
457 attr );
458 }
459#endif
460}
461#endif /* MBEDTLS_ECDH_C */
462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463#endif /* MBEDTLS_DEBUG_C */