blob: 8a5b28758cce4836276d00b31aab7beef76b9b8a [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
Dave Rodgmaned59ea72023-02-15 17:41:28 +000033/* DEBUG_BUF_SIZE must be at least 2 */
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020034#define DEBUG_BUF_SIZE 512
35
Paul Bakkerc73079a2014-04-25 16:34:30 +020036static int debug_threshold = 0;
Paul Bakkereaebbd52014-04-25 15:04:14 +020037
Gilles Peskine449bd832023-01-11 14:50:10 +010038void mbedtls_debug_set_threshold(int threshold)
Paul Bakkerc73079a2014-04-25 16:34:30 +020039{
40 debug_threshold = threshold;
41}
42
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020043/*
44 * All calls to f_dbg must be made via this function
45 */
Gilles Peskine449bd832023-01-11 14:50:10 +010046static inline void debug_send_line(const mbedtls_ssl_context *ssl, int level,
47 const char *file, int line,
48 const char *str)
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020049{
50 /*
51 * If in a threaded environment, we need a thread identifier.
52 * Since there is no portable way to get one, use the address of the ssl
53 * context instead, as it shouldn't be shared between threads.
54 */
55#if defined(MBEDTLS_THREADING_C)
56 char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */
Gilles Peskine449bd832023-01-11 14:50:10 +010057 mbedtls_snprintf(idstr, sizeof(idstr), "%p: %s", (void *) ssl, str);
58 ssl->conf->f_dbg(ssl->conf->p_dbg, level, file, line, idstr);
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020059#else
Gilles Peskine449bd832023-01-11 14:50:10 +010060 ssl->conf->f_dbg(ssl->conf->p_dbg, level, file, line, str);
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020061#endif
62}
63
Paul Elliott4e589702020-12-09 14:38:01 +000064MBEDTLS_PRINTF_ATTRIBUTE(5, 6)
Gilles Peskine449bd832023-01-11 14:50:10 +010065void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level,
66 const char *file, int line,
67 const char *format, ...)
Paul Bakker5121ce52009-01-03 21:22:43 +000068{
69 va_list argp;
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020070 char str[DEBUG_BUF_SIZE];
Janos Follath865b3eb2019-12-16 11:46:15 +000071 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
valord577536893c2023-02-15 19:31:39 +080072
73#if defined(static_assert)
74 static_assert(DEBUG_BUF_SIZE >= 2)
75#endif
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020076
Gilles Peskine449bd832023-01-11 14:50:10 +010077 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +010078 NULL == ssl->conf ||
79 NULL == ssl->conf->f_dbg ||
Gilles Peskine449bd832023-01-11 14:50:10 +010080 level > debug_threshold) {
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020081 return;
Hanno Becker687c5002018-06-29 09:04:46 +010082 }
Paul Bakker5121ce52009-01-03 21:22:43 +000083
Gilles Peskine449bd832023-01-11 14:50:10 +010084 va_start(argp, format);
85 ret = mbedtls_vsnprintf(str, DEBUG_BUF_SIZE, format, argp);
86 va_end(argp);
Paul Bakker5121ce52009-01-03 21:22:43 +000087
valord577536893c2023-02-15 19:31:39 +080088 if (ret < 0) {
valord5775bfcd1c2023-02-15 21:46:47 +080089 ret = 0;
valord577536893c2023-02-15 19:31:39 +080090 } else {
valord577536893c2023-02-15 19:31:39 +080091 if (ret >= DEBUG_BUF_SIZE - 1) {
valord5775bfcd1c2023-02-15 21:46:47 +080092 ret = DEBUG_BUF_SIZE - 2;
valord57724da0cd2023-02-15 19:01:16 +080093 }
valord57725418ac2022-10-31 15:17:37 +080094 }
valord5775bfcd1c2023-02-15 21:46:47 +080095 str[ret] = '\n';
96 str[ret + 1] = '\0';
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020097
valord5775bfcd1c2023-02-15 21:46:47 +080098 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +000099}
100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level,
102 const char *file, int line,
103 const char *text, int ret)
Paul Bakker5121ce52009-01-03 21:22:43 +0000104{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200105 char str[DEBUG_BUF_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100108 NULL == ssl->conf ||
109 NULL == ssl->conf->f_dbg ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100112 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200114 /*
115 * With non-blocking I/O and examples that just retry immediately,
116 * the logs would be quickly flooded with WANT_READ, so ignore that.
Tom Cosgrove1797b052022-12-04 17:19:59 +0000117 * Don't ignore WANT_WRITE however, since it is usually rare.
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200118 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200120 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 }
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 mbedtls_snprintf(str, sizeof(str), "%s() returned %d (-0x%04x)\n",
124 text, ret, (unsigned int) -ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000127}
128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129void mbedtls_debug_print_buf(const mbedtls_ssl_context *ssl, int level,
130 const char *file, int line, const char *text,
131 const unsigned char *buf, size_t len)
Paul Bakker5121ce52009-01-03 21:22:43 +0000132{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200133 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100134 char txt[17];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200135 size_t i, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000136
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100138 NULL == ssl->conf ||
139 NULL == ssl->conf->f_dbg ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100142 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 mbedtls_snprintf(str + idx, sizeof(str) - idx, "dumping '%s' (%u bytes)\n",
145 text, (unsigned int) len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000148
Paul Bakker92478c32014-04-25 15:18:34 +0200149 idx = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 memset(txt, 0, sizeof(txt));
151 for (i = 0; i < len; i++) {
152 if (i >= 4096) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 if (i % 16 == 0) {
157 if (i > 0) {
158 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
159 debug_send_line(ssl, level, file, line, str);
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100160
Paul Bakker92478c32014-04-25 15:18:34 +0200161 idx = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 memset(txt, 0, sizeof(txt));
Paul Bakker92478c32014-04-25 15:18:34 +0200163 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, "%04x: ",
166 (unsigned int) i);
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
Paul Bakker5121ce52009-01-03 21:22:43 +0000168 }
169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x",
171 (unsigned int) buf[i]);
172 txt[i % 16] = (buf[i] > 31 && buf[i] < 127) ? buf[i] : '.';
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 }
174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 if (len > 0) {
176 for (/* i = i */; i % 16 != 0; i++) {
177 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " ");
178 }
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
181 debug_send_line(ssl, level, file, line, str);
Paul Bakker92478c32014-04-25 15:18:34 +0200182 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000183}
184
Valerio Setti6c496a12023-04-07 15:53:51 +0200185#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100186void mbedtls_debug_print_ecp(const mbedtls_ssl_context *ssl, int level,
187 const char *file, int line,
188 const char *text, const mbedtls_ecp_point *X)
Paul Bakker41c83d32013-03-20 14:39:14 +0100189{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200190 char str[DEBUG_BUF_SIZE];
Paul Bakker41c83d32013-03-20 14:39:14 +0100191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100193 NULL == ssl->conf ||
194 NULL == ssl->conf->f_dbg ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 level > debug_threshold) {
Paul Bakkerc73079a2014-04-25 16:34:30 +0200196 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100197 }
Paul Bakkerc73079a2014-04-25 16:34:30 +0200198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 mbedtls_snprintf(str, sizeof(str), "%s(X)", text);
200 mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->X);
Paul Bakker41c83d32013-03-20 14:39:14 +0100201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 mbedtls_snprintf(str, sizeof(str), "%s(Y)", text);
203 mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->Y);
Paul Bakker41c83d32013-03-20 14:39:14 +0100204}
Valerio Setti6c496a12023-04-07 15:53:51 +0200205#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker41c83d32013-03-20 14:39:14 +0100206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100208void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl, int level,
209 const char *file, int line,
210 const char *text, const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000211{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200212 char str[DEBUG_BUF_SIZE];
Gilles Peskineb26696b2021-06-02 20:17:46 +0200213 size_t bitlen;
214 size_t idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100217 NULL == ssl->conf ||
218 NULL == ssl->conf->f_dbg ||
219 NULL == X ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000221 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100222 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 bitlen = mbedtls_mpi_bitlen(X);
Paul Bakker5121ce52009-01-03 21:22:43 +0000225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 mbedtls_snprintf(str, sizeof(str), "value of '%s' (%u bits) is:\n",
227 text, (unsigned) bitlen);
228 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 if (bitlen == 0) {
Gilles Peskineb26696b2021-06-02 20:17:46 +0200231 str[0] = ' '; str[1] = '0'; str[2] = '0';
232 idx = 3;
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 } else {
Gilles Peskineb26696b2021-06-02 20:17:46 +0200234 int n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 for (n = (int) ((bitlen - 1) / 8); n >= 0; n--) {
236 size_t limb_offset = n / sizeof(mbedtls_mpi_uint);
237 size_t offset_in_limb = n % sizeof(mbedtls_mpi_uint);
Gilles Peskineb26696b2021-06-02 20:17:46 +0200238 unsigned char octet =
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 (X->p[limb_offset] >> (offset_in_limb * 8)) & 0xff;
240 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x", octet);
Gilles Peskineb26696b2021-06-02 20:17:46 +0200241 idx += 3;
242 /* Wrap lines after 16 octets that each take 3 columns */
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 if (idx >= 3 * 16) {
244 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
245 debug_send_line(ssl, level, file, line, str);
Gilles Peskineb26696b2021-06-02 20:17:46 +0200246 idx = 0;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000247 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000249 }
250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 if (idx != 0) {
252 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
253 debug_send_line(ssl, level, file, line, str);
Gilles Peskineb26696b2021-06-02 20:17:46 +0200254 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000255}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000257
Hanno Becker612a2f12020-10-09 09:19:39 +0100258#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100259static void debug_print_pk(const mbedtls_ssl_context *ssl, int level,
260 const char *file, int line,
261 const char *text, const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200262{
263 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200265 char name[16];
266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 memset(items, 0, sizeof(items));
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 if (mbedtls_pk_debug(pk, items) != 0) {
270 debug_send_line(ssl, level, file, line,
271 "invalid PK context\n");
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200272 return;
273 }
274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 for (i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++) {
276 if (items[i].type == MBEDTLS_PK_DEBUG_NONE) {
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200277 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 mbedtls_snprintf(name, sizeof(name), "%s%s", text, items[i].name);
281 name[sizeof(name) - 1] = '\0';
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 if (items[i].type == MBEDTLS_PK_DEBUG_MPI) {
284 mbedtls_debug_print_mpi(ssl, level, file, line, name, items[i].value);
285 } else
Valerio Setti6c496a12023-04-07 15:53:51 +0200286#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 if (items[i].type == MBEDTLS_PK_DEBUG_ECP) {
288 mbedtls_debug_print_ecp(ssl, level, file, line, name, items[i].value);
289 } else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200290#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 { debug_send_line(ssl, level, file, line,
292 "should not happen\n"); }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200293 }
294}
295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296static void debug_print_line_by_line(const mbedtls_ssl_context *ssl, int level,
297 const char *file, int line, const char *text)
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200298{
299 char str[DEBUG_BUF_SIZE];
300 const char *start, *cur;
301
302 start = text;
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 for (cur = text; *cur != '\0'; cur++) {
304 if (*cur == '\n') {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200305 size_t len = cur - start + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 if (len > DEBUG_BUF_SIZE - 1) {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200307 len = DEBUG_BUF_SIZE - 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 }
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 memcpy(str, start, len);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200311 str[len] = '\0';
312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 debug_send_line(ssl, level, file, line, str);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200314
315 start = cur + 1;
316 }
317 }
318}
319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level,
321 const char *file, int line,
322 const char *text, const mbedtls_x509_crt *crt)
Paul Bakker5121ce52009-01-03 21:22:43 +0000323{
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200324 char str[DEBUG_BUF_SIZE];
325 int i = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000326
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100328 NULL == ssl->conf ||
329 NULL == ssl->conf->f_dbg ||
330 NULL == crt ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000332 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100333 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000334
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 while (crt != NULL) {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000336 char buf[1024];
Paul Bakker5121ce52009-01-03 21:22:43 +0000337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 mbedtls_snprintf(str, sizeof(str), "%s #%d:\n", text, ++i);
339 debug_send_line(ssl, level, file, line, str);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt);
342 debug_print_line_by_line(ssl, level, file, line, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 debug_print_pk(ssl, level, file, line, "crt->", &crt->pk);
Paul Bakker5121ce52009-01-03 21:22:43 +0000345
346 crt = crt->next;
347 }
348}
Hanno Becker612a2f12020-10-09 09:19:39 +0100349#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_X509_REMOVE_INFO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000350
Janos Follath948f4be2018-08-22 01:37:55 +0100351#if defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100352static void mbedtls_debug_printf_ecdh_internal(const mbedtls_ssl_context *ssl,
353 int level, const char *file,
354 int line,
355 const mbedtls_ecdh_context *ecdh,
356 mbedtls_debug_ecdh_attr attr)
Janos Follath948f4be2018-08-22 01:37:55 +0100357{
358#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 const mbedtls_ecdh_context *ctx = ecdh;
Janos Follath948f4be2018-08-22 01:37:55 +0100360#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 const mbedtls_ecdh_context_mbed *ctx = &ecdh->ctx.mbed_ecdh;
Janos Follath948f4be2018-08-22 01:37:55 +0100362#endif
363
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 switch (attr) {
Janos Follath948f4be2018-08-22 01:37:55 +0100365 case MBEDTLS_DEBUG_ECDH_Q:
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Q",
367 &ctx->Q);
Janos Follath948f4be2018-08-22 01:37:55 +0100368 break;
369 case MBEDTLS_DEBUG_ECDH_QP:
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Qp",
371 &ctx->Qp);
Janos Follath948f4be2018-08-22 01:37:55 +0100372 break;
373 case MBEDTLS_DEBUG_ECDH_Z:
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 mbedtls_debug_print_mpi(ssl, level, file, line, "ECDH: z",
375 &ctx->z);
Janos Follath948f4be2018-08-22 01:37:55 +0100376 break;
377 default:
378 break;
379 }
380}
381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382void mbedtls_debug_printf_ecdh(const mbedtls_ssl_context *ssl, int level,
383 const char *file, int line,
384 const mbedtls_ecdh_context *ecdh,
385 mbedtls_debug_ecdh_attr attr)
Janos Follath948f4be2018-08-22 01:37:55 +0100386{
387#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh, attr);
Janos Follath948f4be2018-08-22 01:37:55 +0100389#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 switch (ecdh->var) {
Janos Follath948f4be2018-08-22 01:37:55 +0100391 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh,
393 attr);
Janos Follath948f4be2018-08-22 01:37:55 +0100394 }
395#endif
396}
397#endif /* MBEDTLS_ECDH_C */
398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399#endif /* MBEDTLS_DEBUG_C */