blob: 41769eca6ce2fb1ff9597e774e68f71369e87a3e [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
Rich Evans2387c7d2015-01-30 11:10:20 +000038#endif
39
SimonBd5800b72016-04-26 07:43:27 +010040#include "mbedtls/debug.h"
41
42#include <stdarg.h>
43#include <stdio.h>
44#include <string.h>
45
Manuel Pégourié-Gonnard0223ab92015-10-05 11:40:01 +010046#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
47 !defined(inline) && !defined(__cplusplus)
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020048#define inline __inline
49#endif
50
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020051#define DEBUG_BUF_SIZE 512
52
Paul Bakkerc73079a2014-04-25 16:34:30 +020053static int debug_threshold = 0;
Paul Bakkereaebbd52014-04-25 15:04:14 +020054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055void mbedtls_debug_set_threshold( int threshold )
Paul Bakkerc73079a2014-04-25 16:34:30 +020056{
57 debug_threshold = threshold;
58}
59
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020060/*
61 * All calls to f_dbg must be made via this function
62 */
63static inline void debug_send_line( const mbedtls_ssl_context *ssl, int level,
64 const char *file, int line,
65 const char *str )
66{
67 /*
68 * If in a threaded environment, we need a thread identifier.
69 * Since there is no portable way to get one, use the address of the ssl
70 * context instead, as it shouldn't be shared between threads.
71 */
72#if defined(MBEDTLS_THREADING_C)
73 char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */
Simon Butcher097618b2016-11-10 17:28:55 +000074 mbedtls_snprintf( idstr, sizeof( idstr ), "%p: %s", (void*)ssl, str );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020075 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, idstr );
76#else
77 ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str );
78#endif
79}
80
Manuel Pégourié-Gonnarda16e7c42015-06-29 20:14:19 +020081void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020082 const char *file, int line,
83 const char *format, ... )
Paul Bakker5121ce52009-01-03 21:22:43 +000084{
85 va_list argp;
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020086 char str[DEBUG_BUF_SIZE];
87 int ret;
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020088
Hanno Becker687c5002018-06-29 09:04:46 +010089 if( NULL == ssl ||
90 NULL == ssl->conf ||
91 NULL == ssl->conf->f_dbg ||
92 level > debug_threshold )
93 {
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020094 return;
Hanno Becker687c5002018-06-29 09:04:46 +010095 }
Paul Bakker5121ce52009-01-03 21:22:43 +000096
97 va_start( argp, format );
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +020098#if defined(_WIN32)
Ron Eldorbc18eb32017-09-06 17:49:10 +030099#if defined(_TRUNCATE) && !defined(__MINGW32__)
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +0200100 ret = _vsnprintf_s( str, DEBUG_BUF_SIZE, _TRUNCATE, format, argp );
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200101#else
Manuel Pégourié-Gonnarda4f055f2015-07-08 16:46:13 +0200102 ret = _vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
103 if( ret < 0 || (size_t) ret == DEBUG_BUF_SIZE )
104 {
105 str[DEBUG_BUF_SIZE-1] = '\0';
106 ret = -1;
107 }
108#endif
109#else
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +0200110 ret = vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200111#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000112 va_end( argp );
113
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +0200114 if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 )
115 {
116 str[ret] = '\n';
117 str[ret + 1] = '\0';
118 }
119
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200120 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000121}
122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000124 const char *file, int line,
125 const char *text, int ret )
Paul Bakker5121ce52009-01-03 21:22:43 +0000126{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200127 char str[DEBUG_BUF_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
Hanno Becker687c5002018-06-29 09:04:46 +0100129 if( NULL == ssl ||
130 NULL == ssl->conf ||
131 NULL == ssl->conf->f_dbg ||
132 level > debug_threshold )
133 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100135 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000136
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200137 /*
138 * With non-blocking I/O and examples that just retry immediately,
139 * the logs would be quickly flooded with WANT_READ, so ignore that.
140 * Don't ignore WANT_WRITE however, since is is usually rare.
141 */
142 if( ret == MBEDTLS_ERR_SSL_WANT_READ )
143 return;
144
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200145 mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200146 text, ret, -ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000147
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200148 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000149}
150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000152 const char *file, int line, const char *text,
Manuel Pégourié-Gonnarda78b2182015-03-19 17:16:11 +0000153 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000154{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200155 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100156 char txt[17];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200157 size_t i, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
Hanno Becker687c5002018-06-29 09:04:46 +0100159 if( NULL == ssl ||
160 NULL == ssl->conf ||
161 NULL == ssl->conf->f_dbg ||
162 level > debug_threshold )
163 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100165 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000166
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200167 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n",
Paul Bakkereaebbd52014-04-25 15:04:14 +0200168 text, (unsigned int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +0000169
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200170 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000171
Paul Bakker92478c32014-04-25 15:18:34 +0200172 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100173 memset( txt, 0, sizeof( txt ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000174 for( i = 0; i < len; i++ )
175 {
176 if( i >= 4096 )
177 break;
178
179 if( i % 16 == 0 )
180 {
181 if( i > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200182 {
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200183 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200184 debug_send_line( ssl, level, file, line, str );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100185
Paul Bakker92478c32014-04-25 15:18:34 +0200186 idx = 0;
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100187 memset( txt, 0, sizeof( txt ) );
Paul Bakker92478c32014-04-25 15:18:34 +0200188 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000189
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200190 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ",
Paul Bakker92478c32014-04-25 15:18:34 +0200191 (unsigned int) i );
Paul Bakker5121ce52009-01-03 21:22:43 +0000192
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 }
194
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200195 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x",
Paul Bakker92478c32014-04-25 15:18:34 +0200196 (unsigned int) buf[i] );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100197 txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
Paul Bakker5121ce52009-01-03 21:22:43 +0000198 }
199
200 if( len > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200201 {
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100202 for( /* i = i */; i % 16 != 0; i++ )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200203 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " );
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100204
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200205 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200206 debug_send_line( ssl, level, file, line, str );
Paul Bakker92478c32014-04-25 15:18:34 +0200207 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000208}
209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#if defined(MBEDTLS_ECP_C)
211void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
Paul Bakker41c83d32013-03-20 14:39:14 +0100212 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 const char *text, const mbedtls_ecp_point *X )
Paul Bakker41c83d32013-03-20 14:39:14 +0100214{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200215 char str[DEBUG_BUF_SIZE];
Paul Bakker41c83d32013-03-20 14:39:14 +0100216
Hanno Becker687c5002018-06-29 09:04:46 +0100217 if( NULL == ssl ||
218 NULL == ssl->conf ||
219 NULL == ssl->conf->f_dbg ||
220 level > debug_threshold )
221 {
Paul Bakkerc73079a2014-04-25 16:34:30 +0200222 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100223 }
Paul Bakkerc73079a2014-04-25 16:34:30 +0200224
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200225 mbedtls_snprintf( str, sizeof( str ), "%s(X)", text );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
Paul Bakker41c83d32013-03-20 14:39:14 +0100227
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200228 mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
Paul Bakker41c83d32013-03-20 14:39:14 +0100230}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#endif /* MBEDTLS_ECP_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233#if defined(MBEDTLS_BIGNUM_C)
234void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000235 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 const char *text, const mbedtls_mpi *X )
Paul Bakker5121ce52009-01-03 21:22:43 +0000237{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200238 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200239 int j, k, zeros = 1;
Paul Bakkereaebbd52014-04-25 15:04:14 +0200240 size_t i, n, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000241
Hanno Becker687c5002018-06-29 09:04:46 +0100242 if( NULL == ssl ||
243 NULL == ssl->conf ||
244 NULL == ssl->conf->f_dbg ||
245 NULL == X ||
246 level > debug_threshold )
247 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100249 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000250
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000251 for( n = X->n - 1; n > 0; n-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000252 if( X->p[n] != 0 )
253 break;
254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 for( j = ( sizeof(mbedtls_mpi_uint) << 3 ) - 1; j >= 0; j-- )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000256 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
257 break;
258
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200259 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "value of '%s' (%d bits) is:\n",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000261
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200262 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000263
Paul Bakker92478c32014-04-25 15:18:34 +0200264 idx = 0;
Paul Bakker23986e52011-04-24 08:57:21 +0000265 for( i = n + 1, j = 0; i > 0; i-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 {
Paul Bakker23986e52011-04-24 08:57:21 +0000267 if( zeros && X->p[i - 1] == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000268 continue;
Paul Bakker5121ce52009-01-03 21:22:43 +0000269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 for( k = sizeof( mbedtls_mpi_uint ) - 1; k >= 0; k-- )
Paul Bakker5121ce52009-01-03 21:22:43 +0000271 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200272 if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000273 continue;
274 else
275 zeros = 0;
276
277 if( j % 16 == 0 )
278 {
279 if( j > 0 )
Paul Bakker92478c32014-04-25 15:18:34 +0200280 {
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200281 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200282 debug_send_line( ssl, level, file, line, str );
Paul Bakker92478c32014-04-25 15:18:34 +0200283 idx = 0;
284 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000285 }
286
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200287 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", (unsigned int)
Paul Bakker66d5d072014-06-17 16:39:18 +0200288 ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000289
290 j++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000291 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000292
293 }
294
295 if( zeros == 1 )
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200296 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " 00" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000297
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200298 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200299 debug_send_line( ssl, level, file, line, str );
Paul Bakker5121ce52009-01-03 21:22:43 +0000300}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000302
Hanno Becker02a21932019-06-10 15:08:43 +0100303#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200305 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 const char *text, const mbedtls_pk_context *pk )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200307{
308 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200310 char name[16];
311
312 memset( items, 0, sizeof( items ) );
313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 if( mbedtls_pk_debug( pk, items ) != 0 )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200315 {
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200316 debug_send_line( ssl, level, file, line,
Manuel Pégourié-Gonnard80d627a2015-06-29 20:12:51 +0200317 "invalid PK context\n" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200318 return;
319 }
320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200324 return;
325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200327 name[sizeof( name ) - 1] = '\0';
328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
330 mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200331 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332#if defined(MBEDTLS_ECP_C)
333 if( items[i].type == MBEDTLS_PK_DEBUG_ECP )
334 mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200335 else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200336#endif
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200337 debug_send_line( ssl, level, file, line,
Manuel Pégourié-Gonnard80d627a2015-06-29 20:12:51 +0200338 "should not happen\n" );
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200339 }
340}
341
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200342static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level,
343 const char *file, int line, const char *text )
344{
345 char str[DEBUG_BUF_SIZE];
346 const char *start, *cur;
347
348 start = text;
349 for( cur = text; *cur != '\0'; cur++ )
350 {
351 if( *cur == '\n' )
352 {
353 size_t len = cur - start + 1;
354 if( len > DEBUG_BUF_SIZE - 1 )
355 len = DEBUG_BUF_SIZE - 1;
356
357 memcpy( str, start, len );
358 str[len] = '\0';
359
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200360 debug_send_line( ssl, level, file, line, str );
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200361
362 start = cur + 1;
363 }
364 }
365}
366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000368 const char *file, int line,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 const char *text, const mbedtls_x509_crt *crt )
Paul Bakker5121ce52009-01-03 21:22:43 +0000370{
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200371 char str[DEBUG_BUF_SIZE];
372 int i = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000373
Hanno Becker687c5002018-06-29 09:04:46 +0100374 if( NULL == ssl ||
375 NULL == ssl->conf ||
376 NULL == ssl->conf->f_dbg ||
377 NULL == crt ||
378 level > debug_threshold )
379 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100381 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000382
Paul Bakker29087132010-03-21 21:03:34 +0000383 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +0000384 {
Hanno Becker6cb5f862019-02-26 16:46:04 +0000385 int ret;
386 mbedtls_pk_context *pk;
Paul Bakkerd98030e2009-05-02 15:13:40 +0000387 char buf[1024];
Paul Bakker5121ce52009-01-03 21:22:43 +0000388
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200389 mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i );
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +0200390 debug_send_line( ssl, level, file, line, str );
Paul Bakkereaebbd52014-04-25 15:04:14 +0200391
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200392 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
393 debug_print_line_by_line( ssl, level, file, line, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000394
Hanno Becker6cb5f862019-02-26 16:46:04 +0000395 ret = mbedtls_x509_crt_pk_acquire( crt, &pk );
396 if( ret != 0 )
397 {
398 mbedtls_snprintf( str, sizeof( str ),
399 "mbedtls_x509_crt_pk_acquire() failed with -%#04x\n",
400 -ret );
401 debug_send_line( ssl, level, file, line, str );
402 return;
403 }
404 debug_print_pk( ssl, level, file, line, "crt->", pk );
405 mbedtls_x509_crt_pk_release( crt, pk );
Paul Bakker5121ce52009-01-03 21:22:43 +0000406
407 crt = crt->next;
408 }
409}
Hanno Becker02a21932019-06-10 15:08:43 +0100410#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_X509_REMOVE_INFO&& MBEDTLS_X509_REMOVE_INFO !MBEDTLS_X509_REMOVE_INFO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000411
Janos Follath948f4be2018-08-22 01:37:55 +0100412#if defined(MBEDTLS_ECDH_C)
413static void mbedtls_debug_printf_ecdh_internal( const mbedtls_ssl_context *ssl,
414 int level, const char *file,
415 int line,
416 const mbedtls_ecdh_context *ecdh,
417 mbedtls_debug_ecdh_attr attr )
418{
419#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
420 const mbedtls_ecdh_context* ctx = ecdh;
421#else
422 const mbedtls_ecdh_context_mbed* ctx = &ecdh->ctx.mbed_ecdh;
423#endif
424
425 switch( attr )
426 {
427 case MBEDTLS_DEBUG_ECDH_Q:
428 mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Q",
429 &ctx->Q );
430 break;
431 case MBEDTLS_DEBUG_ECDH_QP:
432 mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Qp",
433 &ctx->Qp );
434 break;
435 case MBEDTLS_DEBUG_ECDH_Z:
436 mbedtls_debug_print_mpi( ssl, level, file, line, "ECDH: z",
437 &ctx->z );
438 break;
439 default:
440 break;
441 }
442}
443
444void mbedtls_debug_printf_ecdh( const mbedtls_ssl_context *ssl, int level,
445 const char *file, int line,
446 const mbedtls_ecdh_context *ecdh,
447 mbedtls_debug_ecdh_attr attr )
448{
449#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
450 mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh, attr );
451#else
452 switch( ecdh->var )
453 {
454 default:
455 mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh,
456 attr );
457 }
458#endif
459}
460#endif /* MBEDTLS_ECDH_C */
461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462#endif /* MBEDTLS_DEBUG_C */