blob: f2d8dced5f4b2a02bfee59c526426eb291ea4f6c [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
Dave Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
7
Gilles Peskinedb09ef62020-06-03 01:43:33 +02008#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#if defined(MBEDTLS_DEBUG_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/platform.h"
Rich Evans2387c7d2015-01-30 11:10:20 +000013
SimonBd5800b72016-04-26 07:43:27 +010014#include "mbedtls/debug.h"
Janos Follath73c616b2019-12-18 15:07:04 +000015#include "mbedtls/error.h"
SimonBd5800b72016-04-26 07:43:27 +010016
17#include <stdarg.h>
18#include <stdio.h>
19#include <string.h>
20
Dave Rodgman00191782023-02-15 17:41:28 +000021/* DEBUG_BUF_SIZE must be at least 2 */
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020022#define DEBUG_BUF_SIZE 512
23
Paul Bakkerc73079a2014-04-25 16:34:30 +020024static int debug_threshold = 0;
Paul Bakkereaebbd52014-04-25 15:04:14 +020025
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010026void mbedtls_debug_set_threshold(int threshold)
Paul Bakkerc73079a2014-04-25 16:34:30 +020027{
28 debug_threshold = threshold;
29}
30
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020031/*
32 * All calls to f_dbg must be made via this function
33 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010034static inline void debug_send_line(const mbedtls_ssl_context *ssl, int level,
35 const char *file, int line,
36 const char *str)
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +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 + ': ' */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010045 mbedtls_snprintf(idstr, sizeof(idstr), "%p: %s", (void *) ssl, str);
46 ssl->conf->f_dbg(ssl->conf->p_dbg, level, file, line, idstr);
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020047#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010048 ssl->conf->f_dbg(ssl->conf->p_dbg, level, file, line, str);
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020049#endif
50}
51
Paul Elliott4e589702020-12-09 14:38:01 +000052MBEDTLS_PRINTF_ATTRIBUTE(5, 6)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010053void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level,
54 const char *file, int line,
55 const char *format, ...)
Paul Bakker5121ce52009-01-03 21:22:43 +000056{
57 va_list argp;
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020058 char str[DEBUG_BUF_SIZE];
Janos Follath865b3eb2019-12-16 11:46:15 +000059 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
valord57706b0bb52023-02-15 19:31:39 +080060
Dave Rodgmanbd771822023-05-16 16:43:48 +010061 MBEDTLS_STATIC_ASSERT(DEBUG_BUF_SIZE >= 2, "DEBUG_BUF_SIZE too small");
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020062
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010063 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +010064 NULL == ssl->conf ||
65 NULL == ssl->conf->f_dbg ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010066 level > debug_threshold) {
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020067 return;
Hanno Becker687c5002018-06-29 09:04:46 +010068 }
Paul Bakker5121ce52009-01-03 21:22:43 +000069
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010070 va_start(argp, format);
71 ret = mbedtls_vsnprintf(str, DEBUG_BUF_SIZE, format, argp);
72 va_end(argp);
Paul Bakker5121ce52009-01-03 21:22:43 +000073
valord57706b0bb52023-02-15 19:31:39 +080074 if (ret < 0) {
valord5770d87d902023-02-15 21:46:47 +080075 ret = 0;
valord57706b0bb52023-02-15 19:31:39 +080076 } else {
valord57706b0bb52023-02-15 19:31:39 +080077 if (ret >= DEBUG_BUF_SIZE - 1) {
valord5770d87d902023-02-15 21:46:47 +080078 ret = DEBUG_BUF_SIZE - 2;
valord577e3623922023-02-15 19:01:16 +080079 }
valord577afbaac22022-10-31 15:17:37 +080080 }
valord5770d87d902023-02-15 21:46:47 +080081 str[ret] = '\n';
82 str[ret + 1] = '\0';
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020083
valord5770d87d902023-02-15 21:46:47 +080084 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +000085}
86
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010087void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level,
88 const char *file, int line,
89 const char *text, int ret)
Paul Bakker5121ce52009-01-03 21:22:43 +000090{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020091 char str[DEBUG_BUF_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +000092
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010093 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +010094 NULL == ssl->conf ||
95 NULL == ssl->conf->f_dbg ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010096 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +000097 return;
Hanno Becker687c5002018-06-29 09:04:46 +010098 }
Paul Bakker5121ce52009-01-03 21:22:43 +000099
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200100 /*
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.
103 * Don't ignore WANT_WRITE however, since is is usually rare.
104 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100105 if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200106 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100107 }
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200108
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100109 mbedtls_snprintf(str, sizeof(str), "%s() returned %d (-0x%04x)\n",
110 text, ret, (unsigned int) -ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100112 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000113}
114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100115void 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)
Paul Bakker5121ce52009-01-03 21:22:43 +0000118{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200119 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100120 char txt[17];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200121 size_t i, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000122
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100123 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100124 NULL == ssl->conf ||
125 NULL == ssl->conf->f_dbg ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100128 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100130 mbedtls_snprintf(str + idx, sizeof(str) - idx, "dumping '%s' (%u bytes)\n",
131 text, (unsigned int) len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000132
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100133 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
Paul Bakker92478c32014-04-25 15:18:34 +0200135 idx = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136 memset(txt, 0, sizeof(txt));
137 for (i = 0; i < len; i++) {
138 if (i >= 4096) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100140 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100142 if (i % 16 == 0) {
143 if (i > 0) {
144 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
145 debug_send_line(ssl, level, file, line, str);
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100146
Paul Bakker92478c32014-04-25 15:18:34 +0200147 idx = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100148 memset(txt, 0, sizeof(txt));
Paul Bakker92478c32014-04-25 15:18:34 +0200149 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100151 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, "%04x: ",
152 (unsigned int) i);
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 }
155
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x",
157 (unsigned int) buf[i]);
158 txt[i % 16] = (buf[i] > 31 && buf[i] < 127) ? buf[i] : '.';
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 }
160
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100161 if (len > 0) {
162 for (/* i = i */; i % 16 != 0; i++) {
163 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " ");
164 }
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100165
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100166 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
167 debug_send_line(ssl, level, file, line, str);
Paul Bakker92478c32014-04-25 15:18:34 +0200168 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000169}
170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100172void mbedtls_debug_print_ecp(const mbedtls_ssl_context *ssl, int level,
173 const char *file, int line,
174 const char *text, const mbedtls_ecp_point *X)
Paul Bakker41c83d32013-03-20 14:39:14 +0100175{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200176 char str[DEBUG_BUF_SIZE];
Paul Bakker41c83d32013-03-20 14:39:14 +0100177
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100178 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100179 NULL == ssl->conf ||
180 NULL == ssl->conf->f_dbg ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100181 level > debug_threshold) {
Paul Bakkerc73079a2014-04-25 16:34:30 +0200182 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100183 }
Paul Bakkerc73079a2014-04-25 16:34:30 +0200184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100185 mbedtls_snprintf(str, sizeof(str), "%s(X)", text);
186 mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->X);
Paul Bakker41c83d32013-03-20 14:39:14 +0100187
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100188 mbedtls_snprintf(str, sizeof(str), "%s(Y)", text);
189 mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->Y);
Paul Bakker41c83d32013-03-20 14:39:14 +0100190}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191#endif /* MBEDTLS_ECP_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl, int level,
195 const char *file, int line,
196 const char *text, const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000197{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200198 char str[DEBUG_BUF_SIZE];
Gilles Peskine2ee0bb32021-06-02 20:17:46 +0200199 size_t bitlen;
200 size_t idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000201
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100202 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100203 NULL == ssl->conf ||
204 NULL == ssl->conf->f_dbg ||
205 NULL == X ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100208 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000209
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100210 bitlen = mbedtls_mpi_bitlen(X);
Paul Bakker5121ce52009-01-03 21:22:43 +0000211
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100212 mbedtls_snprintf(str, sizeof(str), "value of '%s' (%u bits) is:\n",
213 text, (unsigned) bitlen);
214 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000215
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216 if (bitlen == 0) {
Gilles Peskine2ee0bb32021-06-02 20:17:46 +0200217 str[0] = ' '; str[1] = '0'; str[2] = '0';
218 idx = 3;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100219 } else {
Gilles Peskine2ee0bb32021-06-02 20:17:46 +0200220 int n;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100221 for (n = (int) ((bitlen - 1) / 8); n >= 0; n--) {
222 size_t limb_offset = n / sizeof(mbedtls_mpi_uint);
223 size_t offset_in_limb = n % sizeof(mbedtls_mpi_uint);
Gilles Peskine2ee0bb32021-06-02 20:17:46 +0200224 unsigned char octet =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100225 (X->p[limb_offset] >> (offset_in_limb * 8)) & 0xff;
226 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x", octet);
Gilles Peskine2ee0bb32021-06-02 20:17:46 +0200227 idx += 3;
228 /* Wrap lines after 16 octets that each take 3 columns */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100229 if (idx >= 3 * 16) {
230 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
231 debug_send_line(ssl, level, file, line, str);
Gilles Peskine2ee0bb32021-06-02 20:17:46 +0200232 idx = 0;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000233 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000234 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000235 }
236
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100237 if (idx != 0) {
238 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
239 debug_send_line(ssl, level, file, line, str);
Gilles Peskine2ee0bb32021-06-02 20:17:46 +0200240 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000241}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100245static void debug_print_pk(const mbedtls_ssl_context *ssl, int level,
246 const char *file, int line,
247 const char *text, const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200248{
249 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200251 char name[16];
252
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100253 memset(items, 0, sizeof(items));
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200254
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100255 if (mbedtls_pk_debug(pk, items) != 0) {
256 debug_send_line(ssl, level, file, line,
257 "invalid PK context\n");
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200258 return;
259 }
260
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100261 for (i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++) {
262 if (items[i].type == MBEDTLS_PK_DEBUG_NONE) {
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200263 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200265
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266 mbedtls_snprintf(name, sizeof(name), "%s%s", text, items[i].name);
267 name[sizeof(name) - 1] = '\0';
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200268
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269 if (items[i].type == MBEDTLS_PK_DEBUG_MPI) {
270 mbedtls_debug_print_mpi(ssl, level, file, line, name, items[i].value);
271 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100273 if (items[i].type == MBEDTLS_PK_DEBUG_ECP) {
274 mbedtls_debug_print_ecp(ssl, level, file, line, name, items[i].value);
275 } else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200276#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277 { debug_send_line(ssl, level, file, line,
278 "should not happen\n"); }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200279 }
280}
281
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100282static void debug_print_line_by_line(const mbedtls_ssl_context *ssl, int level,
283 const char *file, int line, const char *text)
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200284{
285 char str[DEBUG_BUF_SIZE];
286 const char *start, *cur;
287
288 start = text;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289 for (cur = text; *cur != '\0'; cur++) {
290 if (*cur == '\n') {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200291 size_t len = cur - start + 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100292 if (len > DEBUG_BUF_SIZE - 1) {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200293 len = DEBUG_BUF_SIZE - 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100294 }
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200295
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100296 memcpy(str, start, len);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200297 str[len] = '\0';
298
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100299 debug_send_line(ssl, level, file, line, str);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200300
301 start = cur + 1;
302 }
303 }
304}
305
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level,
307 const char *file, int line,
308 const char *text, const mbedtls_x509_crt *crt)
Paul Bakker5121ce52009-01-03 21:22:43 +0000309{
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200310 char str[DEBUG_BUF_SIZE];
311 int i = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100313 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100314 NULL == ssl->conf ||
315 NULL == ssl->conf->f_dbg ||
316 NULL == crt ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100317 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000318 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100319 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000320
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100321 while (crt != NULL) {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000322 char buf[1024];
Paul Bakker5121ce52009-01-03 21:22:43 +0000323
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100324 mbedtls_snprintf(str, sizeof(str), "%s #%d:\n", text, ++i);
325 debug_send_line(ssl, level, file, line, str);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200326
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100327 mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt);
328 debug_print_line_by_line(ssl, level, file, line, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000329
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100330 debug_print_pk(ssl, level, file, line, "crt->", &crt->pk);
Paul Bakker5121ce52009-01-03 21:22:43 +0000331
332 crt = crt->next;
333 }
334}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000336
Janos Follath948f4be2018-08-22 01:37:55 +0100337#if defined(MBEDTLS_ECDH_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100338static void mbedtls_debug_printf_ecdh_internal(const mbedtls_ssl_context *ssl,
339 int level, const char *file,
340 int line,
341 const mbedtls_ecdh_context *ecdh,
342 mbedtls_debug_ecdh_attr attr)
Janos Follath948f4be2018-08-22 01:37:55 +0100343{
344#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100345 const mbedtls_ecdh_context *ctx = ecdh;
Janos Follath948f4be2018-08-22 01:37:55 +0100346#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100347 const mbedtls_ecdh_context_mbed *ctx = &ecdh->ctx.mbed_ecdh;
Janos Follath948f4be2018-08-22 01:37:55 +0100348#endif
349
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350 switch (attr) {
Janos Follath948f4be2018-08-22 01:37:55 +0100351 case MBEDTLS_DEBUG_ECDH_Q:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100352 mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Q",
353 &ctx->Q);
Janos Follath948f4be2018-08-22 01:37:55 +0100354 break;
355 case MBEDTLS_DEBUG_ECDH_QP:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100356 mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Qp",
357 &ctx->Qp);
Janos Follath948f4be2018-08-22 01:37:55 +0100358 break;
359 case MBEDTLS_DEBUG_ECDH_Z:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100360 mbedtls_debug_print_mpi(ssl, level, file, line, "ECDH: z",
361 &ctx->z);
Janos Follath948f4be2018-08-22 01:37:55 +0100362 break;
363 default:
364 break;
365 }
366}
367
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100368void mbedtls_debug_printf_ecdh(const mbedtls_ssl_context *ssl, int level,
369 const char *file, int line,
370 const mbedtls_ecdh_context *ecdh,
371 mbedtls_debug_ecdh_attr attr)
Janos Follath948f4be2018-08-22 01:37:55 +0100372{
373#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100374 mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh, attr);
Janos Follath948f4be2018-08-22 01:37:55 +0100375#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100376 switch (ecdh->var) {
Janos Follath948f4be2018-08-22 01:37:55 +0100377 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100378 mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh,
379 attr);
Janos Follath948f4be2018-08-22 01:37:55 +0100380 }
381#endif
382}
383#endif /* MBEDTLS_ECDH_C */
384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385#endif /* MBEDTLS_DEBUG_C */