blob: c36ed3c5c21c4523a96fa75511d637026a68dbc4 [file] [log] [blame]
Jens Wiklander817466c2018-05-22 13:49:31 +02001/*
2 * Debugging routines
3 *
Jerome Forissier79013242021-07-28 10:24:04 +02004 * Copyright The Mbed TLS Contributors
Tom Van Eyckb0563632024-06-13 16:20:14 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Jens Wiklander817466c2018-05-22 13:49:31 +02006 */
7
Jerome Forissier79013242021-07-28 10:24:04 +02008#include "common.h"
Jens Wiklander817466c2018-05-22 13:49:31 +02009
10#if defined(MBEDTLS_DEBUG_C)
11
Jens Wiklander817466c2018-05-22 13:49:31 +020012#include "mbedtls/platform.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020013
Tom Van Eyckb0563632024-06-13 16:20:14 +020014#include "debug_internal.h"
Jerome Forissier11fa71b2020-04-20 17:17:56 +020015#include "mbedtls/error.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020016
17#include <stdarg.h>
18#include <stdio.h>
19#include <string.h>
20
Tom Van Eyckb0563632024-06-13 16:20:14 +020021/* DEBUG_BUF_SIZE must be at least 2 */
Jens Wiklander817466c2018-05-22 13:49:31 +020022#define DEBUG_BUF_SIZE 512
23
24static int debug_threshold = 0;
25
Jens Wiklander32b31802023-10-06 16:59:46 +020026void mbedtls_debug_set_threshold(int threshold)
Jens Wiklander817466c2018-05-22 13:49:31 +020027{
28 debug_threshold = threshold;
29}
30
31/*
32 * All calls to f_dbg must be made via this function
33 */
Jens Wiklander32b31802023-10-06 16:59:46 +020034static inline void debug_send_line(const mbedtls_ssl_context *ssl, int level,
35 const char *file, int line,
36 const char *str)
Jens Wiklander817466c2018-05-22 13:49:31 +020037{
38 /*
39 * If in a threaded environment, we need a thread identifier.
40 * Since there is no portable way to get one, use the address of the ssl
41 * context instead, as it shouldn't be shared between threads.
42 */
43#if defined(MBEDTLS_THREADING_C)
44 char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */
Jens Wiklander32b31802023-10-06 16:59:46 +020045 mbedtls_snprintf(idstr, sizeof(idstr), "%p: %s", (void *) ssl, str);
46 ssl->conf->f_dbg(ssl->conf->p_dbg, level, file, line, idstr);
Jens Wiklander817466c2018-05-22 13:49:31 +020047#else
Jens Wiklander32b31802023-10-06 16:59:46 +020048 ssl->conf->f_dbg(ssl->conf->p_dbg, level, file, line, str);
Jens Wiklander817466c2018-05-22 13:49:31 +020049#endif
50}
51
Jerome Forissier79013242021-07-28 10:24:04 +020052MBEDTLS_PRINTF_ATTRIBUTE(5, 6)
Jens Wiklander32b31802023-10-06 16:59:46 +020053void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level,
54 const char *file, int line,
55 const char *format, ...)
Jens Wiklander817466c2018-05-22 13:49:31 +020056{
57 va_list argp;
58 char str[DEBUG_BUF_SIZE];
Jerome Forissier11fa71b2020-04-20 17:17:56 +020059 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +020060
Tom Van Eyckb0563632024-06-13 16:20:14 +020061 MBEDTLS_STATIC_ASSERT(DEBUG_BUF_SIZE >= 2, "DEBUG_BUF_SIZE too small");
62
Jens Wiklander32b31802023-10-06 16:59:46 +020063 if (NULL == ssl ||
Jerome Forissier5b25c762020-04-07 11:18:49 +020064 NULL == ssl->conf ||
65 NULL == ssl->conf->f_dbg ||
Jens Wiklander32b31802023-10-06 16:59:46 +020066 level > debug_threshold) {
Jens Wiklander817466c2018-05-22 13:49:31 +020067 return;
Jerome Forissier5b25c762020-04-07 11:18:49 +020068 }
Jens Wiklander817466c2018-05-22 13:49:31 +020069
Jens Wiklander32b31802023-10-06 16:59:46 +020070 va_start(argp, format);
71 ret = mbedtls_vsnprintf(str, DEBUG_BUF_SIZE, format, argp);
72 va_end(argp);
Jens Wiklander817466c2018-05-22 13:49:31 +020073
Tom Van Eyckb0563632024-06-13 16:20:14 +020074 if (ret < 0) {
75 ret = 0;
76 } else {
77 if (ret >= DEBUG_BUF_SIZE - 1) {
78 ret = DEBUG_BUF_SIZE - 2;
79 }
Jens Wiklander817466c2018-05-22 13:49:31 +020080 }
Tom Van Eyckb0563632024-06-13 16:20:14 +020081 str[ret] = '\n';
82 str[ret + 1] = '\0';
Jens Wiklander817466c2018-05-22 13:49:31 +020083
Jens Wiklander32b31802023-10-06 16:59:46 +020084 debug_send_line(ssl, level, file, line, str);
Jens Wiklander817466c2018-05-22 13:49:31 +020085}
86
Jens Wiklander32b31802023-10-06 16:59:46 +020087void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level,
88 const char *file, int line,
89 const char *text, int ret)
Jens Wiklander817466c2018-05-22 13:49:31 +020090{
91 char str[DEBUG_BUF_SIZE];
92
Jens Wiklander32b31802023-10-06 16:59:46 +020093 if (NULL == ssl ||
Jerome Forissier5b25c762020-04-07 11:18:49 +020094 NULL == ssl->conf ||
95 NULL == ssl->conf->f_dbg ||
Jens Wiklander32b31802023-10-06 16:59:46 +020096 level > debug_threshold) {
Jens Wiklander817466c2018-05-22 13:49:31 +020097 return;
Jerome Forissier5b25c762020-04-07 11:18:49 +020098 }
Jens Wiklander817466c2018-05-22 13:49:31 +020099
100 /*
101 * With non-blocking I/O and examples that just retry immediately,
102 * the logs would be quickly flooded with WANT_READ, so ignore that.
Jens Wiklander32b31802023-10-06 16:59:46 +0200103 * Don't ignore WANT_WRITE however, since it is usually rare.
Jens Wiklander817466c2018-05-22 13:49:31 +0200104 */
Jens Wiklander32b31802023-10-06 16:59:46 +0200105 if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200106 return;
Jens Wiklander32b31802023-10-06 16:59:46 +0200107 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200108
Jens Wiklander32b31802023-10-06 16:59:46 +0200109 mbedtls_snprintf(str, sizeof(str), "%s() returned %d (-0x%04x)\n",
110 text, ret, (unsigned int) -ret);
Jens Wiklander817466c2018-05-22 13:49:31 +0200111
Jens Wiklander32b31802023-10-06 16:59:46 +0200112 debug_send_line(ssl, level, file, line, str);
Jens Wiklander817466c2018-05-22 13:49:31 +0200113}
114
Jens Wiklander32b31802023-10-06 16:59:46 +0200115void mbedtls_debug_print_buf(const mbedtls_ssl_context *ssl, int level,
116 const char *file, int line, const char *text,
117 const unsigned char *buf, size_t len)
Jens Wiklander817466c2018-05-22 13:49:31 +0200118{
119 char str[DEBUG_BUF_SIZE];
120 char txt[17];
121 size_t i, idx = 0;
122
Jens Wiklander32b31802023-10-06 16:59:46 +0200123 if (NULL == ssl ||
Jerome Forissier5b25c762020-04-07 11:18:49 +0200124 NULL == ssl->conf ||
125 NULL == ssl->conf->f_dbg ||
Jens Wiklander32b31802023-10-06 16:59:46 +0200126 level > debug_threshold) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200127 return;
Jerome Forissier5b25c762020-04-07 11:18:49 +0200128 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200129
Jens Wiklander32b31802023-10-06 16:59:46 +0200130 mbedtls_snprintf(str + idx, sizeof(str) - idx, "dumping '%s' (%u bytes)\n",
131 text, (unsigned int) len);
Jens Wiklander817466c2018-05-22 13:49:31 +0200132
Jens Wiklander32b31802023-10-06 16:59:46 +0200133 debug_send_line(ssl, level, file, line, str);
Jens Wiklander817466c2018-05-22 13:49:31 +0200134
Jens Wiklander32b31802023-10-06 16:59:46 +0200135 memset(txt, 0, sizeof(txt));
136 for (i = 0; i < len; i++) {
137 if (i >= 4096) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200138 break;
Jens Wiklander32b31802023-10-06 16:59:46 +0200139 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200140
Jens Wiklander32b31802023-10-06 16:59:46 +0200141 if (i % 16 == 0) {
142 if (i > 0) {
143 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
144 debug_send_line(ssl, level, file, line, str);
Jens Wiklander817466c2018-05-22 13:49:31 +0200145
146 idx = 0;
Jens Wiklander32b31802023-10-06 16:59:46 +0200147 memset(txt, 0, sizeof(txt));
Jens Wiklander817466c2018-05-22 13:49:31 +0200148 }
149
Jens Wiklander32b31802023-10-06 16:59:46 +0200150 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, "%04x: ",
151 (unsigned int) i);
Jens Wiklander817466c2018-05-22 13:49:31 +0200152
153 }
154
Jens Wiklander32b31802023-10-06 16:59:46 +0200155 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x",
156 (unsigned int) buf[i]);
157 txt[i % 16] = (buf[i] > 31 && buf[i] < 127) ? buf[i] : '.';
Jens Wiklander817466c2018-05-22 13:49:31 +0200158 }
159
Jens Wiklander32b31802023-10-06 16:59:46 +0200160 if (len > 0) {
161 for (/* i = i */; i % 16 != 0; i++) {
162 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " ");
163 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200164
Jens Wiklander32b31802023-10-06 16:59:46 +0200165 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
166 debug_send_line(ssl, level, file, line, str);
Jens Wiklander817466c2018-05-22 13:49:31 +0200167 }
168}
169
Tom Van Eyckb0563632024-06-13 16:20:14 +0200170#if defined(MBEDTLS_ECP_LIGHT)
Jens Wiklander32b31802023-10-06 16:59:46 +0200171void mbedtls_debug_print_ecp(const mbedtls_ssl_context *ssl, int level,
172 const char *file, int line,
173 const char *text, const mbedtls_ecp_point *X)
Jens Wiklander817466c2018-05-22 13:49:31 +0200174{
175 char str[DEBUG_BUF_SIZE];
176
Jens Wiklander32b31802023-10-06 16:59:46 +0200177 if (NULL == ssl ||
Jerome Forissier5b25c762020-04-07 11:18:49 +0200178 NULL == ssl->conf ||
179 NULL == ssl->conf->f_dbg ||
Jens Wiklander32b31802023-10-06 16:59:46 +0200180 level > debug_threshold) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200181 return;
Jerome Forissier5b25c762020-04-07 11:18:49 +0200182 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200183
Jens Wiklander32b31802023-10-06 16:59:46 +0200184 mbedtls_snprintf(str, sizeof(str), "%s(X)", text);
185 mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->X);
Jens Wiklander817466c2018-05-22 13:49:31 +0200186
Jens Wiklander32b31802023-10-06 16:59:46 +0200187 mbedtls_snprintf(str, sizeof(str), "%s(Y)", text);
188 mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->Y);
Jens Wiklander817466c2018-05-22 13:49:31 +0200189}
Tom Van Eyckb0563632024-06-13 16:20:14 +0200190#endif /* MBEDTLS_ECP_LIGHT */
191
192#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
193static void mbedtls_debug_print_ec_coord(const mbedtls_ssl_context *ssl, int level,
194 const char *file, int line, const char *text,
195 const unsigned char *buf, size_t len)
196{
197 char str[DEBUG_BUF_SIZE];
198 size_t i, idx = 0;
199
200 mbedtls_snprintf(str + idx, sizeof(str) - idx, "value of '%s' (%u bits) is:\n",
201 text, (unsigned int) len * 8);
202
203 debug_send_line(ssl, level, file, line, str);
204
205 for (i = 0; i < len; i++) {
206 if (i >= 4096) {
207 break;
208 }
209
210 if (i % 16 == 0) {
211 if (i > 0) {
212 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
213 debug_send_line(ssl, level, file, line, str);
214
215 idx = 0;
216 }
217 }
218
219 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x",
220 (unsigned int) buf[i]);
221 }
222
223 if (len > 0) {
224 for (/* i = i */; i % 16 != 0; i++) {
225 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " ");
226 }
227
228 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
229 debug_send_line(ssl, level, file, line, str);
230 }
231}
232
233void mbedtls_debug_print_psa_ec(const mbedtls_ssl_context *ssl, int level,
234 const char *file, int line,
235 const char *text, const mbedtls_pk_context *pk)
236{
237 char str[DEBUG_BUF_SIZE];
238 const uint8_t *coord_start;
239 size_t coord_len;
240
241 if (NULL == ssl ||
242 NULL == ssl->conf ||
243 NULL == ssl->conf->f_dbg ||
244 level > debug_threshold) {
245 return;
246 }
247
248 /* For the description of pk->pk_raw content please refer to the description
249 * psa_export_public_key() function. */
250 coord_len = (pk->pub_raw_len - 1)/2;
251
252 /* X coordinate */
253 coord_start = pk->pub_raw + 1;
254 mbedtls_snprintf(str, sizeof(str), "%s(X)", text);
255 mbedtls_debug_print_ec_coord(ssl, level, file, line, str, coord_start, coord_len);
256
257 /* Y coordinate */
258 coord_start = coord_start + coord_len;
259 mbedtls_snprintf(str, sizeof(str), "%s(Y)", text);
260 mbedtls_debug_print_ec_coord(ssl, level, file, line, str, coord_start, coord_len);
261}
262#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jens Wiklander817466c2018-05-22 13:49:31 +0200263
264#if defined(MBEDTLS_BIGNUM_C)
Jens Wiklander32b31802023-10-06 16:59:46 +0200265void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl, int level,
266 const char *file, int line,
267 const char *text, const mbedtls_mpi *X)
Jens Wiklander817466c2018-05-22 13:49:31 +0200268{
269 char str[DEBUG_BUF_SIZE];
Jerome Forissier79013242021-07-28 10:24:04 +0200270 size_t bitlen;
271 size_t idx = 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200272
Jens Wiklander32b31802023-10-06 16:59:46 +0200273 if (NULL == ssl ||
Jerome Forissier5b25c762020-04-07 11:18:49 +0200274 NULL == ssl->conf ||
275 NULL == ssl->conf->f_dbg ||
276 NULL == X ||
Jens Wiklander32b31802023-10-06 16:59:46 +0200277 level > debug_threshold) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200278 return;
Jerome Forissier5b25c762020-04-07 11:18:49 +0200279 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200280
Jens Wiklander32b31802023-10-06 16:59:46 +0200281 bitlen = mbedtls_mpi_bitlen(X);
Jens Wiklander817466c2018-05-22 13:49:31 +0200282
Jens Wiklander32b31802023-10-06 16:59:46 +0200283 mbedtls_snprintf(str, sizeof(str), "value of '%s' (%u bits) is:\n",
284 text, (unsigned) bitlen);
285 debug_send_line(ssl, level, file, line, str);
Jens Wiklander817466c2018-05-22 13:49:31 +0200286
Jens Wiklander32b31802023-10-06 16:59:46 +0200287 if (bitlen == 0) {
Jerome Forissier79013242021-07-28 10:24:04 +0200288 str[0] = ' '; str[1] = '0'; str[2] = '0';
289 idx = 3;
Jens Wiklander32b31802023-10-06 16:59:46 +0200290 } else {
Jerome Forissier79013242021-07-28 10:24:04 +0200291 int n;
Jens Wiklander32b31802023-10-06 16:59:46 +0200292 for (n = (int) ((bitlen - 1) / 8); n >= 0; n--) {
293 size_t limb_offset = n / sizeof(mbedtls_mpi_uint);
294 size_t offset_in_limb = n % sizeof(mbedtls_mpi_uint);
Jerome Forissier79013242021-07-28 10:24:04 +0200295 unsigned char octet =
Jens Wiklander32b31802023-10-06 16:59:46 +0200296 (X->p[limb_offset] >> (offset_in_limb * 8)) & 0xff;
297 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x", octet);
Jerome Forissier79013242021-07-28 10:24:04 +0200298 idx += 3;
299 /* Wrap lines after 16 octets that each take 3 columns */
Jens Wiklander32b31802023-10-06 16:59:46 +0200300 if (idx >= 3 * 16) {
301 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
302 debug_send_line(ssl, level, file, line, str);
Jerome Forissier79013242021-07-28 10:24:04 +0200303 idx = 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200304 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200305 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200306 }
307
Jens Wiklander32b31802023-10-06 16:59:46 +0200308 if (idx != 0) {
309 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
310 debug_send_line(ssl, level, file, line, str);
Jerome Forissier79013242021-07-28 10:24:04 +0200311 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200312}
313#endif /* MBEDTLS_BIGNUM_C */
314
Jens Wiklander32b31802023-10-06 16:59:46 +0200315#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_INFO)
316static void debug_print_pk(const mbedtls_ssl_context *ssl, int level,
317 const char *file, int line,
318 const char *text, const mbedtls_pk_context *pk)
Jens Wiklander817466c2018-05-22 13:49:31 +0200319{
320 size_t i;
321 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
322 char name[16];
323
Jens Wiklander32b31802023-10-06 16:59:46 +0200324 memset(items, 0, sizeof(items));
Jens Wiklander817466c2018-05-22 13:49:31 +0200325
Jens Wiklander32b31802023-10-06 16:59:46 +0200326 if (mbedtls_pk_debug(pk, items) != 0) {
327 debug_send_line(ssl, level, file, line,
328 "invalid PK context\n");
Jens Wiklander817466c2018-05-22 13:49:31 +0200329 return;
330 }
331
Jens Wiklander32b31802023-10-06 16:59:46 +0200332 for (i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++) {
333 if (items[i].type == MBEDTLS_PK_DEBUG_NONE) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200334 return;
Jens Wiklander32b31802023-10-06 16:59:46 +0200335 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200336
Jens Wiklander32b31802023-10-06 16:59:46 +0200337 mbedtls_snprintf(name, sizeof(name), "%s%s", text, items[i].name);
338 name[sizeof(name) - 1] = '\0';
Jens Wiklander817466c2018-05-22 13:49:31 +0200339
Tom Van Eyckb0563632024-06-13 16:20:14 +0200340#if defined(MBEDTLS_RSA_C)
Jens Wiklander32b31802023-10-06 16:59:46 +0200341 if (items[i].type == MBEDTLS_PK_DEBUG_MPI) {
342 mbedtls_debug_print_mpi(ssl, level, file, line, name, items[i].value);
343 } else
Tom Van Eyckb0563632024-06-13 16:20:14 +0200344#endif /* MBEDTLS_RSA_C */
345#if defined(MBEDTLS_ECP_LIGHT)
Jens Wiklander32b31802023-10-06 16:59:46 +0200346 if (items[i].type == MBEDTLS_PK_DEBUG_ECP) {
347 mbedtls_debug_print_ecp(ssl, level, file, line, name, items[i].value);
348 } else
Tom Van Eyckb0563632024-06-13 16:20:14 +0200349#endif /* MBEDTLS_ECP_LIGHT */
350#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
351 if (items[i].type == MBEDTLS_PK_DEBUG_PSA_EC) {
352 mbedtls_debug_print_psa_ec(ssl, level, file, line, name, items[i].value);
353 } else
354#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Jens Wiklander32b31802023-10-06 16:59:46 +0200355 { debug_send_line(ssl, level, file, line,
356 "should not happen\n"); }
Jens Wiklander817466c2018-05-22 13:49:31 +0200357 }
358}
359
Jens Wiklander32b31802023-10-06 16:59:46 +0200360static void debug_print_line_by_line(const mbedtls_ssl_context *ssl, int level,
361 const char *file, int line, const char *text)
Jens Wiklander817466c2018-05-22 13:49:31 +0200362{
363 char str[DEBUG_BUF_SIZE];
364 const char *start, *cur;
365
366 start = text;
Jens Wiklander32b31802023-10-06 16:59:46 +0200367 for (cur = text; *cur != '\0'; cur++) {
368 if (*cur == '\n') {
Tom Van Eyckb0563632024-06-13 16:20:14 +0200369 size_t len = (size_t) (cur - start) + 1;
Jens Wiklander32b31802023-10-06 16:59:46 +0200370 if (len > DEBUG_BUF_SIZE - 1) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200371 len = DEBUG_BUF_SIZE - 1;
Jens Wiklander32b31802023-10-06 16:59:46 +0200372 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200373
Jens Wiklander32b31802023-10-06 16:59:46 +0200374 memcpy(str, start, len);
Jens Wiklander817466c2018-05-22 13:49:31 +0200375 str[len] = '\0';
376
Jens Wiklander32b31802023-10-06 16:59:46 +0200377 debug_send_line(ssl, level, file, line, str);
Jens Wiklander817466c2018-05-22 13:49:31 +0200378
379 start = cur + 1;
380 }
381 }
382}
383
Jens Wiklander32b31802023-10-06 16:59:46 +0200384void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level,
385 const char *file, int line,
386 const char *text, const mbedtls_x509_crt *crt)
Jens Wiklander817466c2018-05-22 13:49:31 +0200387{
388 char str[DEBUG_BUF_SIZE];
389 int i = 0;
390
Jens Wiklander32b31802023-10-06 16:59:46 +0200391 if (NULL == ssl ||
Jerome Forissier5b25c762020-04-07 11:18:49 +0200392 NULL == ssl->conf ||
393 NULL == ssl->conf->f_dbg ||
394 NULL == crt ||
Jens Wiklander32b31802023-10-06 16:59:46 +0200395 level > debug_threshold) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200396 return;
Jerome Forissier5b25c762020-04-07 11:18:49 +0200397 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200398
Jens Wiklander32b31802023-10-06 16:59:46 +0200399 while (crt != NULL) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200400 char buf[1024];
401
Jens Wiklander32b31802023-10-06 16:59:46 +0200402 mbedtls_snprintf(str, sizeof(str), "%s #%d:\n", text, ++i);
403 debug_send_line(ssl, level, file, line, str);
Jens Wiklander817466c2018-05-22 13:49:31 +0200404
Jens Wiklander32b31802023-10-06 16:59:46 +0200405 mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt);
406 debug_print_line_by_line(ssl, level, file, line, buf);
Jens Wiklander817466c2018-05-22 13:49:31 +0200407
Jens Wiklander32b31802023-10-06 16:59:46 +0200408 debug_print_pk(ssl, level, file, line, "crt->", &crt->pk);
Jens Wiklander817466c2018-05-22 13:49:31 +0200409
410 crt = crt->next;
411 }
412}
Jens Wiklander32b31802023-10-06 16:59:46 +0200413#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_X509_REMOVE_INFO */
Jens Wiklander817466c2018-05-22 13:49:31 +0200414
Tom Van Eyckb0563632024-06-13 16:20:14 +0200415#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED) && \
416 defined(MBEDTLS_ECDH_C)
Jens Wiklander32b31802023-10-06 16:59:46 +0200417static void mbedtls_debug_printf_ecdh_internal(const mbedtls_ssl_context *ssl,
418 int level, const char *file,
419 int line,
420 const mbedtls_ecdh_context *ecdh,
421 mbedtls_debug_ecdh_attr attr)
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100422{
423#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Jens Wiklander32b31802023-10-06 16:59:46 +0200424 const mbedtls_ecdh_context *ctx = ecdh;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100425#else
Jens Wiklander32b31802023-10-06 16:59:46 +0200426 const mbedtls_ecdh_context_mbed *ctx = &ecdh->ctx.mbed_ecdh;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100427#endif
428
Jens Wiklander32b31802023-10-06 16:59:46 +0200429 switch (attr) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100430 case MBEDTLS_DEBUG_ECDH_Q:
Jens Wiklander32b31802023-10-06 16:59:46 +0200431 mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Q",
432 &ctx->Q);
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100433 break;
434 case MBEDTLS_DEBUG_ECDH_QP:
Jens Wiklander32b31802023-10-06 16:59:46 +0200435 mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Qp",
436 &ctx->Qp);
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100437 break;
438 case MBEDTLS_DEBUG_ECDH_Z:
Jens Wiklander32b31802023-10-06 16:59:46 +0200439 mbedtls_debug_print_mpi(ssl, level, file, line, "ECDH: z",
440 &ctx->z);
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100441 break;
442 default:
443 break;
444 }
445}
446
Jens Wiklander32b31802023-10-06 16:59:46 +0200447void mbedtls_debug_printf_ecdh(const mbedtls_ssl_context *ssl, int level,
448 const char *file, int line,
449 const mbedtls_ecdh_context *ecdh,
450 mbedtls_debug_ecdh_attr attr)
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100451{
452#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Jens Wiklander32b31802023-10-06 16:59:46 +0200453 mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh, attr);
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100454#else
Jens Wiklander32b31802023-10-06 16:59:46 +0200455 switch (ecdh->var) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100456 default:
Jens Wiklander32b31802023-10-06 16:59:46 +0200457 mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh,
458 attr);
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100459 }
460#endif
461}
Tom Van Eyckb0563632024-06-13 16:20:14 +0200462#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED &&
463 MBEDTLS_ECDH_C */
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100464
Jens Wiklander817466c2018-05-22 13:49:31 +0200465#endif /* MBEDTLS_DEBUG_C */