blob: 462db0588935dcc45fa3091913893a850a723e2c [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Debugging routines
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
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 Bakker5121ce52009-01-03 21:22:43 +000018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_DEBUG_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000023
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020024# if defined(MBEDTLS_PLATFORM_C)
25# include "mbedtls/platform.h"
26# else
27# include <stdlib.h>
28# define mbedtls_calloc calloc
29# define mbedtls_free free
30# define mbedtls_time_t time_t
31# define mbedtls_snprintf snprintf
32# define mbedtls_vsnprintf vsnprintf
33# endif
Rich Evans2387c7d2015-01-30 11:10:20 +000034
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020035# include "mbedtls/debug.h"
36# include "mbedtls/error.h"
SimonBd5800b72016-04-26 07:43:27 +010037
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020038# include <stdarg.h>
39# include <stdio.h>
40# include <string.h>
SimonBd5800b72016-04-26 07:43:27 +010041
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020042# if (defined(__ARMCC_VERSION) || defined(_MSC_VER)) && !defined(inline) && \
43 !defined(__cplusplus)
44# define inline __inline
45# endif
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020046
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020047# define DEBUG_BUF_SIZE 512
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020048
Paul Bakkerc73079a2014-04-25 16:34:30 +020049static int debug_threshold = 0;
Paul Bakkereaebbd52014-04-25 15:04:14 +020050
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020051void mbedtls_debug_set_threshold(int threshold)
Paul Bakkerc73079a2014-04-25 16:34:30 +020052{
53 debug_threshold = threshold;
54}
55
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020056/*
57 * All calls to f_dbg must be made via this function
58 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020059static inline void debug_send_line(const mbedtls_ssl_context *ssl,
60 int level,
61 const char *file,
62 int line,
63 const char *str)
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020064{
65 /*
66 * If in a threaded environment, we need a thread identifier.
67 * Since there is no portable way to get one, use the address of the ssl
68 * context instead, as it shouldn't be shared between threads.
69 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020070# if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020071 char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020072 mbedtls_snprintf(idstr, sizeof(idstr), "%p: %s", (void *)ssl, str);
73 ssl->conf->f_dbg(ssl->conf->p_dbg, level, file, line, idstr);
74# else
75 ssl->conf->f_dbg(ssl->conf->p_dbg, level, file, line, str);
76# endif
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020077}
78
Paul Elliott4e589702020-12-09 14:38:01 +000079MBEDTLS_PRINTF_ATTRIBUTE(5, 6)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020080void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl,
81 int level,
82 const char *file,
83 int line,
84 const char *format,
85 ...)
Paul Bakker5121ce52009-01-03 21:22:43 +000086{
87 va_list argp;
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020088 char str[DEBUG_BUF_SIZE];
Janos Follath865b3eb2019-12-16 11:46:15 +000089 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020090
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020091 if (NULL == ssl || NULL == ssl->conf || NULL == ssl->conf->f_dbg ||
92 level > debug_threshold) {
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020093 return;
Hanno Becker687c5002018-06-29 09:04:46 +010094 }
Paul Bakker5121ce52009-01-03 21:22:43 +000095
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020096 va_start(argp, format);
97 ret = mbedtls_vsnprintf(str, DEBUG_BUF_SIZE, format, argp);
98 va_end(argp);
Paul Bakker5121ce52009-01-03 21:22:43 +000099
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200100 if (ret >= 0 && ret < DEBUG_BUF_SIZE - 1) {
101 str[ret] = '\n';
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +0200102 str[ret + 1] = '\0';
103 }
104
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200105 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000106}
107
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200108void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl,
109 int level,
110 const char *file,
111 int line,
112 const char *text,
113 int ret)
Paul Bakker5121ce52009-01-03 21:22:43 +0000114{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200115 char str[DEBUG_BUF_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +0000116
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200117 if (NULL == ssl || NULL == ssl->conf || NULL == ssl->conf->f_dbg ||
118 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100120 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000121
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200122 /*
123 * With non-blocking I/O and examples that just retry immediately,
124 * the logs would be quickly flooded with WANT_READ, so ignore that.
125 * Don't ignore WANT_WRITE however, since is is usually rare.
126 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200127 if (ret == MBEDTLS_ERR_SSL_WANT_READ)
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200128 return;
129
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200130 mbedtls_snprintf(str, sizeof(str), "%s() returned %d (-0x%04x)\n", text,
131 ret, (unsigned int)-ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000132
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200133 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000134}
135
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200136void mbedtls_debug_print_buf(const mbedtls_ssl_context *ssl,
137 int level,
138 const char *file,
139 int line,
140 const char *text,
141 const unsigned char *buf,
142 size_t len)
Paul Bakker5121ce52009-01-03 21:22:43 +0000143{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200144 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100145 char txt[17];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200146 size_t i, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000147
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200148 if (NULL == ssl || NULL == ssl->conf || NULL == ssl->conf->f_dbg ||
149 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000150 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100151 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200153 mbedtls_snprintf(str + idx, sizeof(str) - idx, "dumping '%s' (%u bytes)\n",
154 text, (unsigned int)len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000155
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200156 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000157
Paul Bakker92478c32014-04-25 15:18:34 +0200158 idx = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200159 memset(txt, 0, sizeof(txt));
160 for (i = 0; i < len; i++) {
161 if (i >= 4096)
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 break;
163
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200164 if (i % 16 == 0) {
165 if (i > 0) {
166 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
167 debug_send_line(ssl, level, file, line, str);
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100168
Paul Bakker92478c32014-04-25 15:18:34 +0200169 idx = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200170 memset(txt, 0, sizeof(txt));
Paul Bakker92478c32014-04-25 15:18:34 +0200171 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000172
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200173 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx,
174 "%04x: ", (unsigned int)i);
Paul Bakker5121ce52009-01-03 21:22:43 +0000175 }
176
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200177 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x",
178 (unsigned int)buf[i]);
179 txt[i % 16] = (buf[i] > 31 && buf[i] < 127) ? buf[i] : '.';
Paul Bakker5121ce52009-01-03 21:22:43 +0000180 }
181
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200182 if (len > 0) {
183 for (/* i = i */; i % 16 != 0; i++)
184 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " ");
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100185
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200186 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
187 debug_send_line(ssl, level, file, line, str);
Paul Bakker92478c32014-04-25 15:18:34 +0200188 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000189}
190
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200191# if defined(MBEDTLS_ECP_C)
192void mbedtls_debug_print_ecp(const mbedtls_ssl_context *ssl,
193 int level,
194 const char *file,
195 int line,
196 const char *text,
197 const mbedtls_ecp_point *X)
Paul Bakker41c83d32013-03-20 14:39:14 +0100198{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200199 char str[DEBUG_BUF_SIZE];
Paul Bakker41c83d32013-03-20 14:39:14 +0100200
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200201 if (NULL == ssl || NULL == ssl->conf || NULL == ssl->conf->f_dbg ||
202 level > debug_threshold) {
Paul Bakkerc73079a2014-04-25 16:34:30 +0200203 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100204 }
Paul Bakkerc73079a2014-04-25 16:34:30 +0200205
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200206 mbedtls_snprintf(str, sizeof(str), "%s(X)", text);
207 mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->X);
Paul Bakker41c83d32013-03-20 14:39:14 +0100208
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200209 mbedtls_snprintf(str, sizeof(str), "%s(Y)", text);
210 mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->Y);
Paul Bakker41c83d32013-03-20 14:39:14 +0100211}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200212# endif /* MBEDTLS_ECP_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100213
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200214# if defined(MBEDTLS_BIGNUM_C)
215void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl,
216 int level,
217 const char *file,
218 int line,
219 const char *text,
220 const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000221{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200222 char str[DEBUG_BUF_SIZE];
Gilles Peskineb26696b2021-06-02 20:17:46 +0200223 size_t bitlen;
224 size_t idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000225
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200226 if (NULL == ssl || NULL == ssl->conf || NULL == ssl->conf->f_dbg ||
227 NULL == X || level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100229 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000230
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200231 bitlen = mbedtls_mpi_bitlen(X);
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200233 mbedtls_snprintf(str, sizeof(str), "value of '%s' (%u bits) is:\n", text,
234 (unsigned)bitlen);
235 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000236
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200237 if (bitlen == 0) {
238 str[0] = ' ';
239 str[1] = '0';
240 str[2] = '0';
Gilles Peskineb26696b2021-06-02 20:17:46 +0200241 idx = 3;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200242 } else {
Gilles Peskineb26696b2021-06-02 20:17:46 +0200243 int n;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200244 for (n = (int)((bitlen - 1) / 8); n >= 0; n--) {
245 size_t limb_offset = n / sizeof(mbedtls_mpi_uint);
246 size_t offset_in_limb = n % sizeof(mbedtls_mpi_uint);
247 unsigned char octet = (X->p[limb_offset] >> (offset_in_limb * 8)) &
248 0xff;
249 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x", octet);
Gilles Peskineb26696b2021-06-02 20:17:46 +0200250 idx += 3;
251 /* Wrap lines after 16 octets that each take 3 columns */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200252 if (idx >= 3 * 16) {
253 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
254 debug_send_line(ssl, level, file, line, str);
Gilles Peskineb26696b2021-06-02 20:17:46 +0200255 idx = 0;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000256 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000257 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000258 }
259
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200260 if (idx != 0) {
261 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
262 debug_send_line(ssl, level, file, line, str);
Gilles Peskineb26696b2021-06-02 20:17:46 +0200263 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000264}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200265# endif /* MBEDTLS_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000266
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200267# if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_INFO)
268static void debug_print_pk(const mbedtls_ssl_context *ssl,
269 int level,
270 const char *file,
271 int line,
272 const char *text,
273 const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200274{
275 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200277 char name[16];
278
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200279 memset(items, 0, sizeof(items));
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200280
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200281 if (mbedtls_pk_debug(pk, items) != 0) {
282 debug_send_line(ssl, level, file, line, "invalid PK context\n");
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200283 return;
284 }
285
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200286 for (i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++) {
287 if (items[i].type == MBEDTLS_PK_DEBUG_NONE)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200288 return;
289
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200290 mbedtls_snprintf(name, sizeof(name), "%s%s", text, items[i].name);
291 name[sizeof(name) - 1] = '\0';
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200292
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200293 if (items[i].type == MBEDTLS_PK_DEBUG_MPI)
294 mbedtls_debug_print_mpi(ssl, level, file, line, name,
295 items[i].value);
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200296 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200297# if defined(MBEDTLS_ECP_C)
298 if (items[i].type == MBEDTLS_PK_DEBUG_ECP)
299 mbedtls_debug_print_ecp(ssl, level, file, line, name,
300 items[i].value);
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200301 else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200302# endif
303 debug_send_line(ssl, level, file, line, "should not happen\n");
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200304 }
305}
306
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200307static void debug_print_line_by_line(const mbedtls_ssl_context *ssl,
308 int level,
309 const char *file,
310 int line,
311 const char *text)
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200312{
313 char str[DEBUG_BUF_SIZE];
314 const char *start, *cur;
315
316 start = text;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200317 for (cur = text; *cur != '\0'; cur++) {
318 if (*cur == '\n') {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200319 size_t len = cur - start + 1;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200320 if (len > DEBUG_BUF_SIZE - 1)
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200321 len = DEBUG_BUF_SIZE - 1;
322
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200323 memcpy(str, start, len);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200324 str[len] = '\0';
325
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200326 debug_send_line(ssl, level, file, line, str);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200327
328 start = cur + 1;
329 }
330 }
331}
332
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200333void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl,
334 int level,
335 const char *file,
336 int line,
337 const char *text,
338 const mbedtls_x509_crt *crt)
Paul Bakker5121ce52009-01-03 21:22:43 +0000339{
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200340 char str[DEBUG_BUF_SIZE];
341 int i = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000342
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200343 if (NULL == ssl || NULL == ssl->conf || NULL == ssl->conf->f_dbg ||
344 NULL == crt || level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000345 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100346 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000347
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200348 while (crt != NULL) {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000349 char buf[1024];
Paul Bakker5121ce52009-01-03 21:22:43 +0000350
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200351 mbedtls_snprintf(str, sizeof(str), "%s #%d:\n", text, ++i);
352 debug_send_line(ssl, level, file, line, str);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200353
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200354 mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt);
355 debug_print_line_by_line(ssl, level, file, line, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000356
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200357 debug_print_pk(ssl, level, file, line, "crt->", &crt->pk);
Paul Bakker5121ce52009-01-03 21:22:43 +0000358
359 crt = crt->next;
360 }
361}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200362# endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_X509_REMOVE_INFO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000363
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200364# if defined(MBEDTLS_ECDH_C)
365static void mbedtls_debug_printf_ecdh_internal(const mbedtls_ssl_context *ssl,
366 int level,
367 const char *file,
368 int line,
369 const mbedtls_ecdh_context *ecdh,
370 mbedtls_debug_ecdh_attr attr)
Janos Follath948f4be2018-08-22 01:37:55 +0100371{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200372# if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
373 const mbedtls_ecdh_context *ctx = ecdh;
374# else
375 const mbedtls_ecdh_context_mbed *ctx = &ecdh->ctx.mbed_ecdh;
376# endif
Janos Follath948f4be2018-08-22 01:37:55 +0100377
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200378 switch (attr) {
Janos Follath948f4be2018-08-22 01:37:55 +0100379 case MBEDTLS_DEBUG_ECDH_Q:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200380 mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Q", &ctx->Q);
Janos Follath948f4be2018-08-22 01:37:55 +0100381 break;
382 case MBEDTLS_DEBUG_ECDH_QP:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200383 mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Qp",
384 &ctx->Qp);
Janos Follath948f4be2018-08-22 01:37:55 +0100385 break;
386 case MBEDTLS_DEBUG_ECDH_Z:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200387 mbedtls_debug_print_mpi(ssl, level, file, line, "ECDH: z", &ctx->z);
Janos Follath948f4be2018-08-22 01:37:55 +0100388 break;
389 default:
390 break;
391 }
392}
393
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200394void mbedtls_debug_printf_ecdh(const mbedtls_ssl_context *ssl,
395 int level,
396 const char *file,
397 int line,
398 const mbedtls_ecdh_context *ecdh,
399 mbedtls_debug_ecdh_attr attr)
Janos Follath948f4be2018-08-22 01:37:55 +0100400{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200401# if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
402 mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh, attr);
403# else
404 switch (ecdh->var) {
Janos Follath948f4be2018-08-22 01:37:55 +0100405 default:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200406 mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh,
407 attr);
Janos Follath948f4be2018-08-22 01:37:55 +0100408 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200409# endif
Janos Follath948f4be2018-08-22 01:37:55 +0100410}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200411# endif /* MBEDTLS_ECDH_C */
Janos Follath948f4be2018-08-22 01:37:55 +0100412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413#endif /* MBEDTLS_DEBUG_C */