blob: eecde10c03b499ce3f38a61f9b81ea13c4644f64 [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;
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020071
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010072 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +010073 NULL == ssl->conf ||
74 NULL == ssl->conf->f_dbg ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010075 level > debug_threshold) {
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020076 return;
Hanno Becker687c5002018-06-29 09:04:46 +010077 }
Paul Bakker5121ce52009-01-03 21:22:43 +000078
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010079 va_start(argp, format);
80 ret = mbedtls_vsnprintf(str, DEBUG_BUF_SIZE, format, argp);
81 va_end(argp);
Paul Bakker5121ce52009-01-03 21:22:43 +000082
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010083 if (ret >= 0 && ret < DEBUG_BUF_SIZE - 1) {
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020084 str[ret] = '\n';
85 str[ret + 1] = '\0';
86 }
Dave Rodgman40fe1cd2022-10-31 11:11:27 +000087 else
valord577afbaac22022-10-31 15:17:37 +080088 {
89 str[DEBUG_BUF_SIZE - 2] = '\n';
90 }
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020091
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +000093}
94
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010095void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level,
96 const char *file, int line,
97 const char *text, int ret)
Paul Bakker5121ce52009-01-03 21:22:43 +000098{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020099 char str[DEBUG_BUF_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100101 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100102 NULL == ssl->conf ||
103 NULL == ssl->conf->f_dbg ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100106 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200108 /*
109 * With non-blocking I/O and examples that just retry immediately,
110 * the logs would be quickly flooded with WANT_READ, so ignore that.
111 * Don't ignore WANT_WRITE however, since is is usually rare.
112 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113 if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200114 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100115 }
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200116
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100117 mbedtls_snprintf(str, sizeof(str), "%s() returned %d (-0x%04x)\n",
118 text, ret, (unsigned int) -ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000119
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100120 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000121}
122
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100123void mbedtls_debug_print_buf(const mbedtls_ssl_context *ssl, int level,
124 const char *file, int line, const char *text,
125 const unsigned char *buf, size_t len)
Paul Bakker5121ce52009-01-03 21:22:43 +0000126{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200127 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100128 char txt[17];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200129 size_t i, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000130
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100132 NULL == ssl->conf ||
133 NULL == ssl->conf->f_dbg ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100134 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100136 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100138 mbedtls_snprintf(str + idx, sizeof(str) - idx, "dumping '%s' (%u bytes)\n",
139 text, (unsigned int) len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
Paul Bakker92478c32014-04-25 15:18:34 +0200143 idx = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100144 memset(txt, 0, sizeof(txt));
145 for (i = 0; i < len; i++) {
146 if (i >= 4096) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100148 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000149
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100150 if (i % 16 == 0) {
151 if (i > 0) {
152 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
153 debug_send_line(ssl, level, file, line, str);
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100154
Paul Bakker92478c32014-04-25 15:18:34 +0200155 idx = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 memset(txt, 0, sizeof(txt));
Paul Bakker92478c32014-04-25 15:18:34 +0200157 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100159 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, "%04x: ",
160 (unsigned int) i);
Paul Bakker5121ce52009-01-03 21:22:43 +0000161
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 }
163
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100164 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x",
165 (unsigned int) buf[i]);
166 txt[i % 16] = (buf[i] > 31 && buf[i] < 127) ? buf[i] : '.';
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 }
168
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100169 if (len > 0) {
170 for (/* i = i */; i % 16 != 0; i++) {
171 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " ");
172 }
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100173
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100174 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
175 debug_send_line(ssl, level, file, line, str);
Paul Bakker92478c32014-04-25 15:18:34 +0200176 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000177}
178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100180void mbedtls_debug_print_ecp(const mbedtls_ssl_context *ssl, int level,
181 const char *file, int line,
182 const char *text, const mbedtls_ecp_point *X)
Paul Bakker41c83d32013-03-20 14:39:14 +0100183{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200184 char str[DEBUG_BUF_SIZE];
Paul Bakker41c83d32013-03-20 14:39:14 +0100185
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100187 NULL == ssl->conf ||
188 NULL == ssl->conf->f_dbg ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100189 level > debug_threshold) {
Paul Bakkerc73079a2014-04-25 16:34:30 +0200190 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100191 }
Paul Bakkerc73079a2014-04-25 16:34:30 +0200192
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100193 mbedtls_snprintf(str, sizeof(str), "%s(X)", text);
194 mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->X);
Paul Bakker41c83d32013-03-20 14:39:14 +0100195
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100196 mbedtls_snprintf(str, sizeof(str), "%s(Y)", text);
197 mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->Y);
Paul Bakker41c83d32013-03-20 14:39:14 +0100198}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#endif /* MBEDTLS_ECP_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100202void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl, int level,
203 const char *file, int line,
204 const char *text, const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000205{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200206 char str[DEBUG_BUF_SIZE];
Gilles Peskine2ee0bb32021-06-02 20:17:46 +0200207 size_t bitlen;
208 size_t idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000209
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100210 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100211 NULL == ssl->conf ||
212 NULL == ssl->conf->f_dbg ||
213 NULL == X ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100214 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000215 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100216 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000217
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100218 bitlen = mbedtls_mpi_bitlen(X);
Paul Bakker5121ce52009-01-03 21:22:43 +0000219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220 mbedtls_snprintf(str, sizeof(str), "value of '%s' (%u bits) is:\n",
221 text, (unsigned) bitlen);
222 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000223
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100224 if (bitlen == 0) {
Gilles Peskine2ee0bb32021-06-02 20:17:46 +0200225 str[0] = ' '; str[1] = '0'; str[2] = '0';
226 idx = 3;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100227 } else {
Gilles Peskine2ee0bb32021-06-02 20:17:46 +0200228 int n;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100229 for (n = (int) ((bitlen - 1) / 8); n >= 0; n--) {
230 size_t limb_offset = n / sizeof(mbedtls_mpi_uint);
231 size_t offset_in_limb = n % sizeof(mbedtls_mpi_uint);
Gilles Peskine2ee0bb32021-06-02 20:17:46 +0200232 unsigned char octet =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 (X->p[limb_offset] >> (offset_in_limb * 8)) & 0xff;
234 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x", octet);
Gilles Peskine2ee0bb32021-06-02 20:17:46 +0200235 idx += 3;
236 /* Wrap lines after 16 octets that each take 3 columns */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100237 if (idx >= 3 * 16) {
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 idx = 0;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000241 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000242 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000243 }
244
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100245 if (idx != 0) {
246 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
247 debug_send_line(ssl, level, file, line, str);
Gilles Peskine2ee0bb32021-06-02 20:17:46 +0200248 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000249}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100253static void debug_print_pk(const mbedtls_ssl_context *ssl, int level,
254 const char *file, int line,
255 const char *text, const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200256{
257 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200259 char name[16];
260
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100261 memset(items, 0, sizeof(items));
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200262
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100263 if (mbedtls_pk_debug(pk, items) != 0) {
264 debug_send_line(ssl, level, file, line,
265 "invalid PK context\n");
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200266 return;
267 }
268
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269 for (i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++) {
270 if (items[i].type == MBEDTLS_PK_DEBUG_NONE) {
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200271 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272 }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200273
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100274 mbedtls_snprintf(name, sizeof(name), "%s%s", text, items[i].name);
275 name[sizeof(name) - 1] = '\0';
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200276
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277 if (items[i].type == MBEDTLS_PK_DEBUG_MPI) {
278 mbedtls_debug_print_mpi(ssl, level, file, line, name, items[i].value);
279 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100281 if (items[i].type == MBEDTLS_PK_DEBUG_ECP) {
282 mbedtls_debug_print_ecp(ssl, level, file, line, name, items[i].value);
283 } else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200284#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100285 { debug_send_line(ssl, level, file, line,
286 "should not happen\n"); }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200287 }
288}
289
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100290static void debug_print_line_by_line(const mbedtls_ssl_context *ssl, int level,
291 const char *file, int line, const char *text)
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200292{
293 char str[DEBUG_BUF_SIZE];
294 const char *start, *cur;
295
296 start = text;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100297 for (cur = text; *cur != '\0'; cur++) {
298 if (*cur == '\n') {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200299 size_t len = cur - start + 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100300 if (len > DEBUG_BUF_SIZE - 1) {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200301 len = DEBUG_BUF_SIZE - 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100302 }
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200303
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100304 memcpy(str, start, len);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200305 str[len] = '\0';
306
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100307 debug_send_line(ssl, level, file, line, str);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200308
309 start = cur + 1;
310 }
311 }
312}
313
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100314void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level,
315 const char *file, int line,
316 const char *text, const mbedtls_x509_crt *crt)
Paul Bakker5121ce52009-01-03 21:22:43 +0000317{
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200318 char str[DEBUG_BUF_SIZE];
319 int i = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000320
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100321 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100322 NULL == ssl->conf ||
323 NULL == ssl->conf->f_dbg ||
324 NULL == crt ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100325 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000326 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100327 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000328
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100329 while (crt != NULL) {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000330 char buf[1024];
Paul Bakker5121ce52009-01-03 21:22:43 +0000331
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100332 mbedtls_snprintf(str, sizeof(str), "%s #%d:\n", text, ++i);
333 debug_send_line(ssl, level, file, line, str);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200334
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100335 mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt);
336 debug_print_line_by_line(ssl, level, file, line, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000337
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100338 debug_print_pk(ssl, level, file, line, "crt->", &crt->pk);
Paul Bakker5121ce52009-01-03 21:22:43 +0000339
340 crt = crt->next;
341 }
342}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000344
Janos Follath948f4be2018-08-22 01:37:55 +0100345#if defined(MBEDTLS_ECDH_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100346static void mbedtls_debug_printf_ecdh_internal(const mbedtls_ssl_context *ssl,
347 int level, const char *file,
348 int line,
349 const mbedtls_ecdh_context *ecdh,
350 mbedtls_debug_ecdh_attr attr)
Janos Follath948f4be2018-08-22 01:37:55 +0100351{
352#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100353 const mbedtls_ecdh_context *ctx = ecdh;
Janos Follath948f4be2018-08-22 01:37:55 +0100354#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100355 const mbedtls_ecdh_context_mbed *ctx = &ecdh->ctx.mbed_ecdh;
Janos Follath948f4be2018-08-22 01:37:55 +0100356#endif
357
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100358 switch (attr) {
Janos Follath948f4be2018-08-22 01:37:55 +0100359 case MBEDTLS_DEBUG_ECDH_Q:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100360 mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Q",
361 &ctx->Q);
Janos Follath948f4be2018-08-22 01:37:55 +0100362 break;
363 case MBEDTLS_DEBUG_ECDH_QP:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100364 mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Qp",
365 &ctx->Qp);
Janos Follath948f4be2018-08-22 01:37:55 +0100366 break;
367 case MBEDTLS_DEBUG_ECDH_Z:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100368 mbedtls_debug_print_mpi(ssl, level, file, line, "ECDH: z",
369 &ctx->z);
Janos Follath948f4be2018-08-22 01:37:55 +0100370 break;
371 default:
372 break;
373 }
374}
375
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100376void mbedtls_debug_printf_ecdh(const mbedtls_ssl_context *ssl, int level,
377 const char *file, int line,
378 const mbedtls_ecdh_context *ecdh,
379 mbedtls_debug_ecdh_attr attr)
Janos Follath948f4be2018-08-22 01:37:55 +0100380{
381#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100382 mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh, attr);
Janos Follath948f4be2018-08-22 01:37:55 +0100383#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100384 switch (ecdh->var) {
Janos Follath948f4be2018-08-22 01:37:55 +0100385 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100386 mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh,
387 attr);
Janos Follath948f4be2018-08-22 01:37:55 +0100388 }
389#endif
390}
391#endif /* MBEDTLS_ECDH_C */
392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393#endif /* MBEDTLS_DEBUG_C */