blob: 839ceba07ce0b3a7a488fe03d998de7ec0422c2e [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
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/platform.h"
Rich Evans2387c7d2015-01-30 11:10:20 +000025
SimonBd5800b72016-04-26 07:43:27 +010026#include "mbedtls/debug.h"
Janos Follath73c616b2019-12-18 15:07:04 +000027#include "mbedtls/error.h"
SimonBd5800b72016-04-26 07:43:27 +010028
29#include <stdarg.h>
30#include <stdio.h>
31#include <string.h>
32
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020033#define DEBUG_BUF_SIZE 512
34
Paul Bakkerc73079a2014-04-25 16:34:30 +020035static int debug_threshold = 0;
Paul Bakkereaebbd52014-04-25 15:04:14 +020036
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010037void mbedtls_debug_set_threshold(int threshold)
Paul Bakkerc73079a2014-04-25 16:34:30 +020038{
39 debug_threshold = threshold;
40}
41
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020042/*
43 * All calls to f_dbg must be made via this function
44 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010045static inline void debug_send_line(const mbedtls_ssl_context *ssl, int level,
46 const char *file, int line,
47 const char *str)
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020048{
49 /*
50 * If in a threaded environment, we need a thread identifier.
51 * Since there is no portable way to get one, use the address of the ssl
52 * context instead, as it shouldn't be shared between threads.
53 */
54#if defined(MBEDTLS_THREADING_C)
55 char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010056 mbedtls_snprintf(idstr, sizeof(idstr), "%p: %s", (void *) ssl, str);
57 ssl->conf->f_dbg(ssl->conf->p_dbg, level, file, line, idstr);
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020058#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010059 ssl->conf->f_dbg(ssl->conf->p_dbg, level, file, line, str);
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020060#endif
61}
62
Paul Elliott4e589702020-12-09 14:38:01 +000063MBEDTLS_PRINTF_ATTRIBUTE(5, 6)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010064void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level,
65 const char *file, int line,
66 const char *format, ...)
Paul Bakker5121ce52009-01-03 21:22:43 +000067{
68 va_list argp;
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020069 char str[DEBUG_BUF_SIZE];
Janos Follath865b3eb2019-12-16 11:46:15 +000070 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
valord577e3623922023-02-15 19:01:16 +080071 int newline = -1;
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020072
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010073 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +010074 NULL == ssl->conf ||
75 NULL == ssl->conf->f_dbg ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076 level > debug_threshold) {
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020077 return;
Hanno Becker687c5002018-06-29 09:04:46 +010078 }
Paul Bakker5121ce52009-01-03 21:22:43 +000079
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010080 va_start(argp, format);
81 ret = mbedtls_vsnprintf(str, DEBUG_BUF_SIZE, format, argp);
82 va_end(argp);
Paul Bakker5121ce52009-01-03 21:22:43 +000083
valord577e3623922023-02-15 19:01:16 +080084 if (DEBUG_BUF_SIZE >= 2) {
85 if (ret < 0) {
86 newline = 0;
87 } else {
88 newline = ret;
89 if (ret >= DEBUG_BUF_SIZE - 1) {
90 newline = DEBUG_BUF_SIZE - 2;
91 }
92 }
valord577afbaac22022-10-31 15:17:37 +080093 }
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020094
valord577e3623922023-02-15 19:01:16 +080095 /*
96 * Send if str contains '\n'.
97 */
98 if (newline >= 0) {
99 str[newline] = '\n';
100 str[newline + 1] = '\0';
101
102 debug_send_line(ssl, level, file, line, str);
103 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000104}
105
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level,
107 const char *file, int line,
108 const char *text, int ret)
Paul Bakker5121ce52009-01-03 21:22:43 +0000109{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200110 char str[DEBUG_BUF_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100112 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100113 NULL == ssl->conf ||
114 NULL == ssl->conf->f_dbg ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100115 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100117 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000118
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200119 /*
120 * With non-blocking I/O and examples that just retry immediately,
121 * the logs would be quickly flooded with WANT_READ, so ignore that.
122 * Don't ignore WANT_WRITE however, since is is usually rare.
123 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100124 if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200125 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126 }
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100128 mbedtls_snprintf(str, sizeof(str), "%s() returned %d (-0x%04x)\n",
129 text, ret, (unsigned int) -ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000130
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000132}
133
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100134void mbedtls_debug_print_buf(const mbedtls_ssl_context *ssl, int level,
135 const char *file, int line, const char *text,
136 const unsigned char *buf, size_t len)
Paul Bakker5121ce52009-01-03 21:22:43 +0000137{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200138 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100139 char txt[17];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200140 size_t i, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100142 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100143 NULL == ssl->conf ||
144 NULL == ssl->conf->f_dbg ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100145 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100147 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000148
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100149 mbedtls_snprintf(str + idx, sizeof(str) - idx, "dumping '%s' (%u bytes)\n",
150 text, (unsigned int) len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100152 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
Paul Bakker92478c32014-04-25 15:18:34 +0200154 idx = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100155 memset(txt, 0, sizeof(txt));
156 for (i = 0; i < len; i++) {
157 if (i >= 4096) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100159 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000160
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100161 if (i % 16 == 0) {
162 if (i > 0) {
163 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
164 debug_send_line(ssl, level, file, line, str);
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100165
Paul Bakker92478c32014-04-25 15:18:34 +0200166 idx = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100167 memset(txt, 0, sizeof(txt));
Paul Bakker92478c32014-04-25 15:18:34 +0200168 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000169
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100170 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, "%04x: ",
171 (unsigned int) i);
Paul Bakker5121ce52009-01-03 21:22:43 +0000172
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 }
174
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100175 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x",
176 (unsigned int) buf[i]);
177 txt[i % 16] = (buf[i] > 31 && buf[i] < 127) ? buf[i] : '.';
Paul Bakker5121ce52009-01-03 21:22:43 +0000178 }
179
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100180 if (len > 0) {
181 for (/* i = i */; i % 16 != 0; i++) {
182 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " ");
183 }
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100185 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
186 debug_send_line(ssl, level, file, line, str);
Paul Bakker92478c32014-04-25 15:18:34 +0200187 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000188}
189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100191void mbedtls_debug_print_ecp(const mbedtls_ssl_context *ssl, int level,
192 const char *file, int line,
193 const char *text, const mbedtls_ecp_point *X)
Paul Bakker41c83d32013-03-20 14:39:14 +0100194{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200195 char str[DEBUG_BUF_SIZE];
Paul Bakker41c83d32013-03-20 14:39:14 +0100196
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100197 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100198 NULL == ssl->conf ||
199 NULL == ssl->conf->f_dbg ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100200 level > debug_threshold) {
Paul Bakkerc73079a2014-04-25 16:34:30 +0200201 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100202 }
Paul Bakkerc73079a2014-04-25 16:34:30 +0200203
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100204 mbedtls_snprintf(str, sizeof(str), "%s(X)", text);
205 mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->X);
Paul Bakker41c83d32013-03-20 14:39:14 +0100206
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207 mbedtls_snprintf(str, sizeof(str), "%s(Y)", text);
208 mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->Y);
Paul Bakker41c83d32013-03-20 14:39:14 +0100209}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#endif /* MBEDTLS_ECP_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100213void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl, int level,
214 const char *file, int line,
215 const char *text, const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000216{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200217 char str[DEBUG_BUF_SIZE];
Gilles Peskine2ee0bb32021-06-02 20:17:46 +0200218 size_t bitlen;
219 size_t idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000220
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100221 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100222 NULL == ssl->conf ||
223 NULL == ssl->conf->f_dbg ||
224 NULL == X ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100225 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100227 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100229 bitlen = mbedtls_mpi_bitlen(X);
Paul Bakker5121ce52009-01-03 21:22:43 +0000230
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100231 mbedtls_snprintf(str, sizeof(str), "value of '%s' (%u bits) is:\n",
232 text, (unsigned) bitlen);
233 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000234
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100235 if (bitlen == 0) {
Gilles Peskine2ee0bb32021-06-02 20:17:46 +0200236 str[0] = ' '; str[1] = '0'; str[2] = '0';
237 idx = 3;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100238 } else {
Gilles Peskine2ee0bb32021-06-02 20:17:46 +0200239 int n;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100240 for (n = (int) ((bitlen - 1) / 8); n >= 0; n--) {
241 size_t limb_offset = n / sizeof(mbedtls_mpi_uint);
242 size_t offset_in_limb = n % sizeof(mbedtls_mpi_uint);
Gilles Peskine2ee0bb32021-06-02 20:17:46 +0200243 unsigned char octet =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100244 (X->p[limb_offset] >> (offset_in_limb * 8)) & 0xff;
245 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x", octet);
Gilles Peskine2ee0bb32021-06-02 20:17:46 +0200246 idx += 3;
247 /* Wrap lines after 16 octets that each take 3 columns */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100248 if (idx >= 3 * 16) {
249 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
250 debug_send_line(ssl, level, file, line, str);
Gilles Peskine2ee0bb32021-06-02 20:17:46 +0200251 idx = 0;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000252 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000253 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000254 }
255
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100256 if (idx != 0) {
257 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
258 debug_send_line(ssl, level, file, line, str);
Gilles Peskine2ee0bb32021-06-02 20:17:46 +0200259 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000260}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264static void debug_print_pk(const mbedtls_ssl_context *ssl, int level,
265 const char *file, int line,
266 const char *text, const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200267{
268 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200270 char name[16];
271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272 memset(items, 0, sizeof(items));
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200273
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100274 if (mbedtls_pk_debug(pk, items) != 0) {
275 debug_send_line(ssl, level, file, line,
276 "invalid PK context\n");
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200277 return;
278 }
279
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100280 for (i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++) {
281 if (items[i].type == MBEDTLS_PK_DEBUG_NONE) {
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200282 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100283 }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200284
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100285 mbedtls_snprintf(name, sizeof(name), "%s%s", text, items[i].name);
286 name[sizeof(name) - 1] = '\0';
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200287
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100288 if (items[i].type == MBEDTLS_PK_DEBUG_MPI) {
289 mbedtls_debug_print_mpi(ssl, level, file, line, name, items[i].value);
290 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100292 if (items[i].type == MBEDTLS_PK_DEBUG_ECP) {
293 mbedtls_debug_print_ecp(ssl, level, file, line, name, items[i].value);
294 } else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200295#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100296 { debug_send_line(ssl, level, file, line,
297 "should not happen\n"); }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200298 }
299}
300
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100301static void debug_print_line_by_line(const mbedtls_ssl_context *ssl, int level,
302 const char *file, int line, const char *text)
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200303{
304 char str[DEBUG_BUF_SIZE];
305 const char *start, *cur;
306
307 start = text;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100308 for (cur = text; *cur != '\0'; cur++) {
309 if (*cur == '\n') {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200310 size_t len = cur - start + 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311 if (len > DEBUG_BUF_SIZE - 1) {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200312 len = DEBUG_BUF_SIZE - 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100313 }
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200314
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100315 memcpy(str, start, len);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200316 str[len] = '\0';
317
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100318 debug_send_line(ssl, level, file, line, str);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200319
320 start = cur + 1;
321 }
322 }
323}
324
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100325void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level,
326 const char *file, int line,
327 const char *text, const mbedtls_x509_crt *crt)
Paul Bakker5121ce52009-01-03 21:22:43 +0000328{
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200329 char str[DEBUG_BUF_SIZE];
330 int i = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000331
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100332 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100333 NULL == ssl->conf ||
334 NULL == ssl->conf->f_dbg ||
335 NULL == crt ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100336 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000337 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100338 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000339
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100340 while (crt != NULL) {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000341 char buf[1024];
Paul Bakker5121ce52009-01-03 21:22:43 +0000342
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100343 mbedtls_snprintf(str, sizeof(str), "%s #%d:\n", text, ++i);
344 debug_send_line(ssl, level, file, line, str);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200345
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100346 mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt);
347 debug_print_line_by_line(ssl, level, file, line, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000348
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100349 debug_print_pk(ssl, level, file, line, "crt->", &crt->pk);
Paul Bakker5121ce52009-01-03 21:22:43 +0000350
351 crt = crt->next;
352 }
353}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000355
Janos Follath948f4be2018-08-22 01:37:55 +0100356#if defined(MBEDTLS_ECDH_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100357static void mbedtls_debug_printf_ecdh_internal(const mbedtls_ssl_context *ssl,
358 int level, const char *file,
359 int line,
360 const mbedtls_ecdh_context *ecdh,
361 mbedtls_debug_ecdh_attr attr)
Janos Follath948f4be2018-08-22 01:37:55 +0100362{
363#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100364 const mbedtls_ecdh_context *ctx = ecdh;
Janos Follath948f4be2018-08-22 01:37:55 +0100365#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100366 const mbedtls_ecdh_context_mbed *ctx = &ecdh->ctx.mbed_ecdh;
Janos Follath948f4be2018-08-22 01:37:55 +0100367#endif
368
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100369 switch (attr) {
Janos Follath948f4be2018-08-22 01:37:55 +0100370 case MBEDTLS_DEBUG_ECDH_Q:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100371 mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Q",
372 &ctx->Q);
Janos Follath948f4be2018-08-22 01:37:55 +0100373 break;
374 case MBEDTLS_DEBUG_ECDH_QP:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100375 mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Qp",
376 &ctx->Qp);
Janos Follath948f4be2018-08-22 01:37:55 +0100377 break;
378 case MBEDTLS_DEBUG_ECDH_Z:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100379 mbedtls_debug_print_mpi(ssl, level, file, line, "ECDH: z",
380 &ctx->z);
Janos Follath948f4be2018-08-22 01:37:55 +0100381 break;
382 default:
383 break;
384 }
385}
386
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100387void mbedtls_debug_printf_ecdh(const mbedtls_ssl_context *ssl, int level,
388 const char *file, int line,
389 const mbedtls_ecdh_context *ecdh,
390 mbedtls_debug_ecdh_attr attr)
Janos Follath948f4be2018-08-22 01:37:55 +0100391{
392#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393 mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh, attr);
Janos Follath948f4be2018-08-22 01:37:55 +0100394#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100395 switch (ecdh->var) {
Janos Follath948f4be2018-08-22 01:37:55 +0100396 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100397 mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh,
398 attr);
Janos Follath948f4be2018-08-22 01:37:55 +0100399 }
400#endif
401}
402#endif /* MBEDTLS_ECDH_C */
403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404#endif /* MBEDTLS_DEBUG_C */