blob: 1f7c2a09035605ca6915fa96bcd573cfa4466b9c [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_DEBUG_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/platform.h"
Rich Evans2387c7d2015-01-30 11:10:20 +000032#else
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020033#include <stdlib.h>
34#define mbedtls_calloc calloc
35#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010036#define mbedtls_time_t time_t
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020037#define mbedtls_snprintf snprintf
k-stachowiak723f8672018-07-16 14:27:07 +020038#define mbedtls_vsnprintf vsnprintf
Rich Evans2387c7d2015-01-30 11:10:20 +000039#endif
40
SimonBd5800b72016-04-26 07:43:27 +010041#include "mbedtls/debug.h"
42
43#include <stdarg.h>
44#include <stdio.h>
45#include <string.h>
46
Janos Follath865b3eb2019-12-16 11:46:15 +000047#include "mbedtls/error.h"
48
Manuel Pégourié-Gonnard0223ab92015-10-05 11:40:01 +010049#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
50 !defined(inline) && !defined(__cplusplus)
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020051#define inline __inline
52#endif
53
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020054#define DEBUG_BUF_SIZE 512
55
Paul Bakkerc73079a2014-04-25 16:34:30 +020056static int debug_threshold = 0;
Paul Bakkereaebbd52014-04-25 15:04:14 +020057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058void mbedtls_debug_set_threshold( int threshold )
Paul Bakkerc73079a2014-04-25 16:34:30 +020059{
60 debug_threshold = threshold;
61}
62
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020063/*
64 * All calls to f_dbg must be made via this function
65 */
66static inline void debug_send_line( const mbedtls_ssl_context *ssl, int level,
67 const char *file, int line,
68 const char *str )
69{
70 /*
71 * If in a threaded environment, we need a thread identifier.
72 * Since there is no portable way to get one, use the address of the ssl
73 * context instead, as it shouldn't be shared between threads.
74 */
75#if defined(MBEDTLS_THREADING_C)
76 char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */
Simon Butcher097618b2016-11-10 17:28:55 +000077 mbedtls_snprintf( idstr, sizeof( idstr ), "%p: %s", (void*)ssl, str );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020078 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, idstr );
79#else
80 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
81#endif
82}
83
Manuel Pégourié-Gonnarda16e7c42015-06-29 20:14:19 +020084void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020085 const char *file, int line,
86 const char *format, ... )
Paul Bakker5121ce52009-01-03 21:22:43 +000087{
88 va_list argp;
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020089 char str[DEBUG_BUF_SIZE];
Janos Follath865b3eb2019-12-16 11:46:15 +000090 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020091
Hanno Becker687c5002018-06-29 09:04:46 +010092 if( NULL == ssl ||
93 NULL == ssl->conf ||
94 NULL == ssl->conf->f_dbg ||
95 level > debug_threshold )
96 {
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020097 return;
Hanno Becker687c5002018-06-29 09:04:46 +010098 }
Paul Bakker5121ce52009-01-03 21:22:43 +000099
100 va_start( argp, format );
k-stachowiak723f8672018-07-16 14:27:07 +0200101 ret = mbedtls_vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000102 va_end( argp );
103
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +0200104 if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 )
105 {
106 str[ret] = '\n';
107 str[ret + 1] = '\0';
108 }
109
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200110 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000111}
112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000114 const char *file, int line,
115 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +0000116{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200117 char str[DEBUG_BUF_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +0000118
Hanno Becker687c5002018-06-29 09:04:46 +0100119 if( NULL == ssl ||
120 NULL == ssl->conf ||
121 NULL == ssl->conf->f_dbg ||
122 level > debug_threshold )
123 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100125 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200127 /*
128 * With non-blocking I/O and examples that just retry immediately,
129 * the logs would be quickly flooded with WANT_READ, so ignore that.
130 * Don't ignore WANT_WRITE however, since is is usually rare.
131 */
132 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
133 return;
134
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200135 mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200136 text, ret, -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000137
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200138 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000139}
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000142 const char *file, int line, const char *text,
Manuel Pégourié-Gonnarda78b2182015-03-19 17:16:11 +0000143 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000144{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200145 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100146 char txt[17];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200147 size_t i, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000148
Hanno Becker687c5002018-06-29 09:04:46 +0100149 if( NULL == ssl ||
150 NULL == ssl->conf ||
151 NULL == ssl->conf->f_dbg ||
152 level > debug_threshold )
153 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100155 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200157 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200158 text, (unsigned int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000159
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200160 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000161
Paul Bakker92478c32014-04-25 15:18:34 +0200162 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100163 memset( txt, 0, sizeof( txt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 for( i = 0; i < len; i++ )
165 {
166 if( i >= 4096 )
167 break;
168
169 if( i % 16 == 0 )
170 {
171 if( i > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200172 {
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200173 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200174 debug_send_line( ssl, level, file, line, str );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100175
Paul Bakker92478c32014-04-25 15:18:34 +0200176 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100177 memset( txt, 0, sizeof( txt ) );
Paul Bakker92478c32014-04-25 15:18:34 +0200178 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000179
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200180 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ",
Paul Bakker92478c32014-04-25 15:18:34 +0200181 (unsigned int) i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000182
Paul Bakker5121ce52009-01-03 21:22:43 +0000183 }
184
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200185 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x",
Paul Bakker92478c32014-04-25 15:18:34 +0200186 (unsigned int) buf[i] );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100187 txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 }
189
190 if( len > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200191 {
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100192 for( /* i = i */; i % 16 != 0; i++ )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200193 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100194
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200195 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200196 debug_send_line( ssl, level, file, line, str );
Paul Bakker92478c32014-04-25 15:18:34 +0200197 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000198}
199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200#if defined(MBEDTLS_ECP_C)
201void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
Paul Bakker41c83d32013-03-20 14:39:14 +0100202 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 const char *text, const mbedtls_ecp_point *X )
Paul Bakker41c83d32013-03-20 14:39:14 +0100204{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200205 char str[DEBUG_BUF_SIZE];
Paul Bakker41c83d32013-03-20 14:39:14 +0100206
Hanno Becker687c5002018-06-29 09:04:46 +0100207 if( NULL == ssl ||
208 NULL == ssl->conf ||
209 NULL == ssl->conf->f_dbg ||
210 level > debug_threshold )
211 {
Paul Bakkerc73079a2014-04-25 16:34:30 +0200212 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100213 }
Paul Bakkerc73079a2014-04-25 16:34:30 +0200214
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200215 mbedtls_snprintf( str, sizeof( str ), "%s(X)", text );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
Paul Bakker41c83d32013-03-20 14:39:14 +0100217
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200218 mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
Paul Bakker41c83d32013-03-20 14:39:14 +0100220}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221#endif /* MBEDTLS_ECP_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223#if defined(MBEDTLS_BIGNUM_C)
224void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000225 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 const char *text, const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000227{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200228 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200229 int j, k, zeros = 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +0200230 size_t i, n, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
Hanno Becker687c5002018-06-29 09:04:46 +0100232 if( NULL == ssl ||
233 NULL == ssl->conf ||
234 NULL == ssl->conf->f_dbg ||
235 NULL == X ||
236 level > debug_threshold )
237 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100239 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000240
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000241 for( n = X->n - 1; n > 0; n-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000242 if( X->p[n] != 0 )
243 break;
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 for( j = ( sizeof(mbedtls_mpi_uint) << 3 ) - 1; j >= 0; j-- )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000246 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
247 break;
248
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200249 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "value of '%s' (%d bits) is:\n",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000251
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200252 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000253
Paul Bakker92478c32014-04-25 15:18:34 +0200254 idx = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000255 for( i = n + 1, j = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000256 {
Paul Bakker23986e52011-04-24 08:57:21 +0000257 if( zeros && X->p[i - 1] == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000258 continue;
Paul Bakker5121ce52009-01-03 21:22:43 +0000259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 for( k = sizeof( mbedtls_mpi_uint ) - 1; k >= 0; k-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000261 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200262 if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000263 continue;
264 else
265 zeros = 0;
266
267 if( j % 16 == 0 )
268 {
269 if( j > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200270 {
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200271 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200272 debug_send_line( ssl, level, file, line, str );
Paul Bakker92478c32014-04-25 15:18:34 +0200273 idx = 0;
274 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000275 }
276
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200277 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", (unsigned int)
Paul Bakker66d5d072014-06-17 16:39:18 +0200278 ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000279
280 j++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000281 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000282
283 }
284
285 if( zeros == 1 )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200286 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " 00" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000287
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200288 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
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}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293#if defined(MBEDTLS_X509_CRT_PARSE_C)
294static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200295 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 const char *text, const mbedtls_pk_context *pk )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200297{
298 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200300 char name[16];
301
302 memset( items, 0, sizeof( items ) );
303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 if( mbedtls_pk_debug( pk, items ) != 0 )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200305 {
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200306 debug_send_line( ssl, level, file, line,
Manuel Pégourié-Gonnard80d627a2015-06-29 20:12:51 +0200307 "invalid PK context\n" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200308 return;
309 }
310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200314 return;
315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200317 name[sizeof( name ) - 1] = '\0';
318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
320 mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200321 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322#if defined(MBEDTLS_ECP_C)
323 if( items[i].type == MBEDTLS_PK_DEBUG_ECP )
324 mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200325 else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200326#endif
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200327 debug_send_line( ssl, level, file, line,
Manuel Pégourié-Gonnard80d627a2015-06-29 20:12:51 +0200328 "should not happen\n" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200329 }
330}
331
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200332static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level,
333 const char *file, int line, const char *text )
334{
335 char str[DEBUG_BUF_SIZE];
336 const char *start, *cur;
337
338 start = text;
339 for( cur = text; *cur != '\0'; cur++ )
340 {
341 if( *cur == '\n' )
342 {
343 size_t len = cur - start + 1;
344 if( len > DEBUG_BUF_SIZE - 1 )
345 len = DEBUG_BUF_SIZE - 1;
346
347 memcpy( str, start, len );
348 str[len] = '\0';
349
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200350 debug_send_line( ssl, level, file, line, str );
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200351
352 start = cur + 1;
353 }
354 }
355}
356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000358 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 const char *text, const mbedtls_x509_crt *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000360{
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200361 char str[DEBUG_BUF_SIZE];
362 int i = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000363
Hanno Becker687c5002018-06-29 09:04:46 +0100364 if( NULL == ssl ||
365 NULL == ssl->conf ||
366 NULL == ssl->conf->f_dbg ||
367 NULL == crt ||
368 level > debug_threshold )
369 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000370 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100371 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000372
Paul Bakker29087132010-03-21 21:03:34 +0000373 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000374 {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000375 char buf[1024];
Paul Bakker5121ce52009-01-03 21:22:43 +0000376
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200377 mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200378 debug_send_line( ssl, level, file, line, str );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200379
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200380 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
381 debug_print_line_by_line( ssl, level, file, line, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000382
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200383 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
Paul Bakker5121ce52009-01-03 21:22:43 +0000384
385 crt = crt->next;
386 }
387}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000389
Janos Follath948f4be2018-08-22 01:37:55 +0100390#if defined(MBEDTLS_ECDH_C)
391static void mbedtls_debug_printf_ecdh_internal( const mbedtls_ssl_context *ssl,
392 int level, const char *file,
393 int line,
394 const mbedtls_ecdh_context *ecdh,
395 mbedtls_debug_ecdh_attr attr )
396{
397#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
398 const mbedtls_ecdh_context* ctx = ecdh;
399#else
400 const mbedtls_ecdh_context_mbed* ctx = &ecdh->ctx.mbed_ecdh;
401#endif
402
403 switch( attr )
404 {
405 case MBEDTLS_DEBUG_ECDH_Q:
406 mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Q",
407 &ctx->Q );
408 break;
409 case MBEDTLS_DEBUG_ECDH_QP:
410 mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Qp",
411 &ctx->Qp );
412 break;
413 case MBEDTLS_DEBUG_ECDH_Z:
414 mbedtls_debug_print_mpi( ssl, level, file, line, "ECDH: z",
415 &ctx->z );
416 break;
417 default:
418 break;
419 }
420}
421
422void mbedtls_debug_printf_ecdh( const mbedtls_ssl_context *ssl, int level,
423 const char *file, int line,
424 const mbedtls_ecdh_context *ecdh,
425 mbedtls_debug_ecdh_attr attr )
426{
427#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
428 mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh, attr );
429#else
430 switch( ecdh->var )
431 {
432 default:
433 mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh,
434 attr );
435 }
436#endif
437}
438#endif /* MBEDTLS_ECDH_C */
439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440#endif /* MBEDTLS_DEBUG_C */