blob: e3dfaefb0864bcc67f5b1348cd4e0c49ef4b2286 [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
Dave Rodgman8508e502023-05-16 16:43:48 +010073 MBEDTLS_STATIC_ASSERT(DEBUG_BUF_SIZE >= 2, "DEBUG_BUF_SIZE too small");
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020074
Gilles Peskine449bd832023-01-11 14:50:10 +010075 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +010076 NULL == ssl->conf ||
77 NULL == ssl->conf->f_dbg ||
Gilles Peskine449bd832023-01-11 14:50:10 +010078 level > debug_threshold) {
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020079 return;
Hanno Becker687c5002018-06-29 09:04:46 +010080 }
Paul Bakker5121ce52009-01-03 21:22:43 +000081
Gilles Peskine449bd832023-01-11 14:50:10 +010082 va_start(argp, format);
83 ret = mbedtls_vsnprintf(str, DEBUG_BUF_SIZE, format, argp);
84 va_end(argp);
Paul Bakker5121ce52009-01-03 21:22:43 +000085
valord577536893c2023-02-15 19:31:39 +080086 if (ret < 0) {
valord5775bfcd1c2023-02-15 21:46:47 +080087 ret = 0;
valord577536893c2023-02-15 19:31:39 +080088 } else {
valord577536893c2023-02-15 19:31:39 +080089 if (ret >= DEBUG_BUF_SIZE - 1) {
valord5775bfcd1c2023-02-15 21:46:47 +080090 ret = DEBUG_BUF_SIZE - 2;
valord57724da0cd2023-02-15 19:01:16 +080091 }
valord57725418ac2022-10-31 15:17:37 +080092 }
valord5775bfcd1c2023-02-15 21:46:47 +080093 str[ret] = '\n';
94 str[ret + 1] = '\0';
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020095
valord5775bfcd1c2023-02-15 21:46:47 +080096 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +000097}
98
Gilles Peskine449bd832023-01-11 14:50:10 +010099void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level,
100 const char *file, int line,
101 const char *text, int ret)
Paul Bakker5121ce52009-01-03 21:22:43 +0000102{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200103 char str[DEBUG_BUF_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100106 NULL == ssl->conf ||
107 NULL == ssl->conf->f_dbg ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100110 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200112 /*
113 * With non-blocking I/O and examples that just retry immediately,
114 * the logs would be quickly flooded with WANT_READ, so ignore that.
Tom Cosgrove1797b052022-12-04 17:19:59 +0000115 * Don't ignore WANT_WRITE however, since it is usually rare.
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200116 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200118 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 }
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 mbedtls_snprintf(str, sizeof(str), "%s() returned %d (-0x%04x)\n",
122 text, ret, (unsigned int) -ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000125}
126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127void mbedtls_debug_print_buf(const mbedtls_ssl_context *ssl, int level,
128 const char *file, int line, const char *text,
129 const unsigned char *buf, size_t len)
Paul Bakker5121ce52009-01-03 21:22:43 +0000130{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200131 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100132 char txt[17];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200133 size_t i, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100136 NULL == ssl->conf ||
137 NULL == ssl->conf->f_dbg ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100140 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 mbedtls_snprintf(str + idx, sizeof(str) - idx, "dumping '%s' (%u bytes)\n",
143 text, (unsigned int) len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000144
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
Paul Bakker92478c32014-04-25 15:18:34 +0200147 idx = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 memset(txt, 0, sizeof(txt));
149 for (i = 0; i < len; i++) {
150 if (i >= 4096) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000151 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 if (i % 16 == 0) {
155 if (i > 0) {
156 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
157 debug_send_line(ssl, level, file, line, str);
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100158
Paul Bakker92478c32014-04-25 15:18:34 +0200159 idx = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 memset(txt, 0, sizeof(txt));
Paul Bakker92478c32014-04-25 15:18:34 +0200161 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, "%04x: ",
164 (unsigned int) i);
Paul Bakker5121ce52009-01-03 21:22:43 +0000165
Paul Bakker5121ce52009-01-03 21:22:43 +0000166 }
167
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x",
169 (unsigned int) buf[i]);
170 txt[i % 16] = (buf[i] > 31 && buf[i] < 127) ? buf[i] : '.';
Paul Bakker5121ce52009-01-03 21:22:43 +0000171 }
172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 if (len > 0) {
174 for (/* i = i */; i % 16 != 0; i++) {
175 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " ");
176 }
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
179 debug_send_line(ssl, level, file, line, str);
Paul Bakker92478c32014-04-25 15:18:34 +0200180 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000181}
182
Valerio Setti6c496a12023-04-07 15:53:51 +0200183#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100184void mbedtls_debug_print_ecp(const mbedtls_ssl_context *ssl, int level,
185 const char *file, int line,
186 const char *text, const mbedtls_ecp_point *X)
Paul Bakker41c83d32013-03-20 14:39:14 +0100187{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200188 char str[DEBUG_BUF_SIZE];
Paul Bakker41c83d32013-03-20 14:39:14 +0100189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100191 NULL == ssl->conf ||
192 NULL == ssl->conf->f_dbg ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 level > debug_threshold) {
Paul Bakkerc73079a2014-04-25 16:34:30 +0200194 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100195 }
Paul Bakkerc73079a2014-04-25 16:34:30 +0200196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 mbedtls_snprintf(str, sizeof(str), "%s(X)", text);
198 mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->X);
Paul Bakker41c83d32013-03-20 14:39:14 +0100199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 mbedtls_snprintf(str, sizeof(str), "%s(Y)", text);
201 mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->Y);
Paul Bakker41c83d32013-03-20 14:39:14 +0100202}
Valerio Setti6c496a12023-04-07 15:53:51 +0200203#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker41c83d32013-03-20 14:39:14 +0100204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100206void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl, int level,
207 const char *file, int line,
208 const char *text, const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000209{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200210 char str[DEBUG_BUF_SIZE];
Gilles Peskineb26696b2021-06-02 20:17:46 +0200211 size_t bitlen;
212 size_t idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100215 NULL == ssl->conf ||
216 NULL == ssl->conf->f_dbg ||
217 NULL == X ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000219 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100220 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 bitlen = mbedtls_mpi_bitlen(X);
Paul Bakker5121ce52009-01-03 21:22:43 +0000223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 mbedtls_snprintf(str, sizeof(str), "value of '%s' (%u bits) is:\n",
225 text, (unsigned) bitlen);
226 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 if (bitlen == 0) {
Gilles Peskineb26696b2021-06-02 20:17:46 +0200229 str[0] = ' '; str[1] = '0'; str[2] = '0';
230 idx = 3;
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 } else {
Gilles Peskineb26696b2021-06-02 20:17:46 +0200232 int n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 for (n = (int) ((bitlen - 1) / 8); n >= 0; n--) {
234 size_t limb_offset = n / sizeof(mbedtls_mpi_uint);
235 size_t offset_in_limb = n % sizeof(mbedtls_mpi_uint);
Gilles Peskineb26696b2021-06-02 20:17:46 +0200236 unsigned char octet =
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 (X->p[limb_offset] >> (offset_in_limb * 8)) & 0xff;
238 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x", octet);
Gilles Peskineb26696b2021-06-02 20:17:46 +0200239 idx += 3;
240 /* Wrap lines after 16 octets that each take 3 columns */
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 if (idx >= 3 * 16) {
242 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
243 debug_send_line(ssl, level, file, line, str);
Gilles Peskineb26696b2021-06-02 20:17:46 +0200244 idx = 0;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000245 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000246 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000247 }
248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 if (idx != 0) {
250 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
251 debug_send_line(ssl, level, file, line, str);
Gilles Peskineb26696b2021-06-02 20:17:46 +0200252 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000253}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000255
Hanno Becker612a2f12020-10-09 09:19:39 +0100256#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100257static void debug_print_pk(const mbedtls_ssl_context *ssl, int level,
258 const char *file, int line,
259 const char *text, const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200260{
261 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200263 char name[16];
264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 memset(items, 0, sizeof(items));
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 if (mbedtls_pk_debug(pk, items) != 0) {
268 debug_send_line(ssl, level, file, line,
269 "invalid PK context\n");
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200270 return;
271 }
272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 for (i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++) {
274 if (items[i].type == MBEDTLS_PK_DEBUG_NONE) {
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200275 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 mbedtls_snprintf(name, sizeof(name), "%s%s", text, items[i].name);
279 name[sizeof(name) - 1] = '\0';
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 if (items[i].type == MBEDTLS_PK_DEBUG_MPI) {
282 mbedtls_debug_print_mpi(ssl, level, file, line, name, items[i].value);
283 } else
Valerio Setti6c496a12023-04-07 15:53:51 +0200284#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 if (items[i].type == MBEDTLS_PK_DEBUG_ECP) {
286 mbedtls_debug_print_ecp(ssl, level, file, line, name, items[i].value);
287 } else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200288#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 { debug_send_line(ssl, level, file, line,
290 "should not happen\n"); }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200291 }
292}
293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294static void debug_print_line_by_line(const mbedtls_ssl_context *ssl, int level,
295 const char *file, int line, const char *text)
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200296{
297 char str[DEBUG_BUF_SIZE];
298 const char *start, *cur;
299
300 start = text;
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 for (cur = text; *cur != '\0'; cur++) {
302 if (*cur == '\n') {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200303 size_t len = cur - start + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 if (len > DEBUG_BUF_SIZE - 1) {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200305 len = DEBUG_BUF_SIZE - 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 }
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 memcpy(str, start, len);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200309 str[len] = '\0';
310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 debug_send_line(ssl, level, file, line, str);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200312
313 start = cur + 1;
314 }
315 }
316}
317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level,
319 const char *file, int line,
320 const char *text, const mbedtls_x509_crt *crt)
Paul Bakker5121ce52009-01-03 21:22:43 +0000321{
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200322 char str[DEBUG_BUF_SIZE];
323 int i = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100326 NULL == ssl->conf ||
327 NULL == ssl->conf->f_dbg ||
328 NULL == crt ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000330 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100331 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 while (crt != NULL) {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000334 char buf[1024];
Paul Bakker5121ce52009-01-03 21:22:43 +0000335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 mbedtls_snprintf(str, sizeof(str), "%s #%d:\n", text, ++i);
337 debug_send_line(ssl, level, file, line, str);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt);
340 debug_print_line_by_line(ssl, level, file, line, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000341
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 debug_print_pk(ssl, level, file, line, "crt->", &crt->pk);
Paul Bakker5121ce52009-01-03 21:22:43 +0000343
344 crt = crt->next;
345 }
346}
Hanno Becker612a2f12020-10-09 09:19:39 +0100347#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_X509_REMOVE_INFO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000348
Janos Follath948f4be2018-08-22 01:37:55 +0100349#if defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100350static void mbedtls_debug_printf_ecdh_internal(const mbedtls_ssl_context *ssl,
351 int level, const char *file,
352 int line,
353 const mbedtls_ecdh_context *ecdh,
354 mbedtls_debug_ecdh_attr attr)
Janos Follath948f4be2018-08-22 01:37:55 +0100355{
356#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 const mbedtls_ecdh_context *ctx = ecdh;
Janos Follath948f4be2018-08-22 01:37:55 +0100358#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 const mbedtls_ecdh_context_mbed *ctx = &ecdh->ctx.mbed_ecdh;
Janos Follath948f4be2018-08-22 01:37:55 +0100360#endif
361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 switch (attr) {
Janos Follath948f4be2018-08-22 01:37:55 +0100363 case MBEDTLS_DEBUG_ECDH_Q:
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Q",
365 &ctx->Q);
Janos Follath948f4be2018-08-22 01:37:55 +0100366 break;
367 case MBEDTLS_DEBUG_ECDH_QP:
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Qp",
369 &ctx->Qp);
Janos Follath948f4be2018-08-22 01:37:55 +0100370 break;
371 case MBEDTLS_DEBUG_ECDH_Z:
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 mbedtls_debug_print_mpi(ssl, level, file, line, "ECDH: z",
373 &ctx->z);
Janos Follath948f4be2018-08-22 01:37:55 +0100374 break;
375 default:
376 break;
377 }
378}
379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380void mbedtls_debug_printf_ecdh(const mbedtls_ssl_context *ssl, int level,
381 const char *file, int line,
382 const mbedtls_ecdh_context *ecdh,
383 mbedtls_debug_ecdh_attr attr)
Janos Follath948f4be2018-08-22 01:37:55 +0100384{
385#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh, attr);
Janos Follath948f4be2018-08-22 01:37:55 +0100387#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 switch (ecdh->var) {
Janos Follath948f4be2018-08-22 01:37:55 +0100389 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh,
391 attr);
Janos Follath948f4be2018-08-22 01:37:55 +0100392 }
393#endif
394}
395#endif /* MBEDTLS_ECDH_C */
396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397#endif /* MBEDTLS_DEBUG_C */