blob: 92fe903de266b3d5290ef606fdd9471f6bb2e348 [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
Valerio Setti7ca7b902023-05-17 15:35:46 +0200205#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Settic1319f42023-07-27 16:20:07 +0200206static void mbedtls_debug_print_ec_coord(const mbedtls_ssl_context *ssl, int level,
207 const char *file, int line, const char *text,
208 const unsigned char *buf, size_t len)
209{
210 char str[DEBUG_BUF_SIZE];
211 size_t i, idx = 0;
212
213 mbedtls_snprintf(str + idx, sizeof(str) - idx, "value of '%s' (%u bits) is:\n",
214 text, (unsigned int) len * 8);
215
216 debug_send_line(ssl, level, file, line, str);
217
218 idx = 0;
219 for (i = 0; i < len; i++) {
220 if (i >= 4096) {
221 break;
222 }
223
224 if (i % 16 == 0) {
225 if (i > 0) {
226 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
227 debug_send_line(ssl, level, file, line, str);
228
229 idx = 0;
230 }
231 }
232
233 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x",
234 (unsigned int) buf[i]);
235 }
236
237 if (len > 0) {
238 for (/* i = i */; i % 16 != 0; i++) {
239 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " ");
240 }
241
242 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
243 debug_send_line(ssl, level, file, line, str);
244 }
245}
246
Valerio Setti7ca7b902023-05-17 15:35:46 +0200247void mbedtls_debug_print_psa_ec(const mbedtls_ssl_context *ssl, int level,
248 const char *file, int line,
249 const char *text, const mbedtls_pk_context *pk)
250{
251 char str[DEBUG_BUF_SIZE];
Valerio Settic1319f42023-07-27 16:20:07 +0200252 const uint8_t *coord_start;
253 size_t coord_len;
Valerio Setti7ca7b902023-05-17 15:35:46 +0200254
255 if (NULL == ssl ||
256 NULL == ssl->conf ||
257 NULL == ssl->conf->f_dbg ||
258 level > debug_threshold) {
259 return;
260 }
261
262 /* For the description of pk->pk_raw content please refer to the description
263 * psa_export_public_key() function. */
Valerio Settic1319f42023-07-27 16:20:07 +0200264 coord_len = (pk->pub_raw_len - 1)/2;
Valerio Setti7ca7b902023-05-17 15:35:46 +0200265
266 /* X coordinate */
Valerio Settic1319f42023-07-27 16:20:07 +0200267 coord_start = pk->pub_raw + 1;
Valerio Setti7ca7b902023-05-17 15:35:46 +0200268 mbedtls_snprintf(str, sizeof(str), "%s(X)", text);
Valerio Settic1319f42023-07-27 16:20:07 +0200269 mbedtls_debug_print_ec_coord(ssl, level, file, line, str, coord_start, coord_len);
Valerio Setti7ca7b902023-05-17 15:35:46 +0200270
271 /* Y coordinate */
Valerio Settic1319f42023-07-27 16:20:07 +0200272 coord_start = coord_start + coord_len;
Valerio Setti7ca7b902023-05-17 15:35:46 +0200273 mbedtls_snprintf(str, sizeof(str), "%s(Y)", text);
Valerio Settic1319f42023-07-27 16:20:07 +0200274 mbedtls_debug_print_ec_coord(ssl, level, file, line, str, coord_start, coord_len);
Valerio Setti7ca7b902023-05-17 15:35:46 +0200275}
276#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
277
Valerio Settic1319f42023-07-27 16:20:07 +0200278#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100279void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl, int level,
280 const char *file, int line,
281 const char *text, const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000282{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200283 char str[DEBUG_BUF_SIZE];
Gilles Peskineb26696b2021-06-02 20:17:46 +0200284 size_t bitlen;
285 size_t idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100288 NULL == ssl->conf ||
289 NULL == ssl->conf->f_dbg ||
290 NULL == X ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100293 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 bitlen = mbedtls_mpi_bitlen(X);
Paul Bakker5121ce52009-01-03 21:22:43 +0000296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 mbedtls_snprintf(str, sizeof(str), "value of '%s' (%u bits) is:\n",
298 text, (unsigned) bitlen);
299 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000300
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 if (bitlen == 0) {
Gilles Peskineb26696b2021-06-02 20:17:46 +0200302 str[0] = ' '; str[1] = '0'; str[2] = '0';
303 idx = 3;
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 } else {
Gilles Peskineb26696b2021-06-02 20:17:46 +0200305 int n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 for (n = (int) ((bitlen - 1) / 8); n >= 0; n--) {
307 size_t limb_offset = n / sizeof(mbedtls_mpi_uint);
308 size_t offset_in_limb = n % sizeof(mbedtls_mpi_uint);
Gilles Peskineb26696b2021-06-02 20:17:46 +0200309 unsigned char octet =
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 (X->p[limb_offset] >> (offset_in_limb * 8)) & 0xff;
311 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x", octet);
Gilles Peskineb26696b2021-06-02 20:17:46 +0200312 idx += 3;
313 /* Wrap lines after 16 octets that each take 3 columns */
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 if (idx >= 3 * 16) {
315 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
316 debug_send_line(ssl, level, file, line, str);
Gilles Peskineb26696b2021-06-02 20:17:46 +0200317 idx = 0;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000318 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000320 }
321
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 if (idx != 0) {
323 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
324 debug_send_line(ssl, level, file, line, str);
Gilles Peskineb26696b2021-06-02 20:17:46 +0200325 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000326}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000328
Hanno Becker612a2f12020-10-09 09:19:39 +0100329#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100330static void debug_print_pk(const mbedtls_ssl_context *ssl, int level,
331 const char *file, int line,
332 const char *text, const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200333{
334 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200336 char name[16];
337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 memset(items, 0, sizeof(items));
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (mbedtls_pk_debug(pk, items) != 0) {
341 debug_send_line(ssl, level, file, line,
342 "invalid PK context\n");
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200343 return;
344 }
345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 for (i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++) {
347 if (items[i].type == MBEDTLS_PK_DEBUG_NONE) {
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200348 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 mbedtls_snprintf(name, sizeof(name), "%s%s", text, items[i].name);
352 name[sizeof(name) - 1] = '\0';
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200353
Valerio Setti797e3962023-07-27 16:19:00 +0200354#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 if (items[i].type == MBEDTLS_PK_DEBUG_MPI) {
356 mbedtls_debug_print_mpi(ssl, level, file, line, name, items[i].value);
357 } else
Valerio Setti797e3962023-07-27 16:19:00 +0200358#endif /* MBEDTLS_RSA_C */
Valerio Setti6c496a12023-04-07 15:53:51 +0200359#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 if (items[i].type == MBEDTLS_PK_DEBUG_ECP) {
361 mbedtls_debug_print_ecp(ssl, level, file, line, name, items[i].value);
362 } else
Valerio Setti797e3962023-07-27 16:19:00 +0200363#endif /* MBEDTLS_ECP_LIGHT */
Valerio Settic1319f42023-07-27 16:20:07 +0200364#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti7ca7b902023-05-17 15:35:46 +0200365 if (items[i].type == MBEDTLS_PK_DEBUG_PSA_EC) {
366 mbedtls_debug_print_psa_ec(ssl, level, file, line, name, items[i].value);
367 } else
Valerio Settic1319f42023-07-27 16:20:07 +0200368#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 { debug_send_line(ssl, level, file, line,
370 "should not happen\n"); }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200371 }
372}
373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374static void debug_print_line_by_line(const mbedtls_ssl_context *ssl, int level,
375 const char *file, int line, const char *text)
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200376{
377 char str[DEBUG_BUF_SIZE];
378 const char *start, *cur;
379
380 start = text;
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 for (cur = text; *cur != '\0'; cur++) {
382 if (*cur == '\n') {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200383 size_t len = cur - start + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 if (len > DEBUG_BUF_SIZE - 1) {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200385 len = DEBUG_BUF_SIZE - 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 }
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 memcpy(str, start, len);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200389 str[len] = '\0';
390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 debug_send_line(ssl, level, file, line, str);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200392
393 start = cur + 1;
394 }
395 }
396}
397
Gilles Peskine449bd832023-01-11 14:50:10 +0100398void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level,
399 const char *file, int line,
400 const char *text, const mbedtls_x509_crt *crt)
Paul Bakker5121ce52009-01-03 21:22:43 +0000401{
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200402 char str[DEBUG_BUF_SIZE];
403 int i = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000404
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100406 NULL == ssl->conf ||
407 NULL == ssl->conf->f_dbg ||
408 NULL == crt ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000410 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100411 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000412
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 while (crt != NULL) {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000414 char buf[1024];
Paul Bakker5121ce52009-01-03 21:22:43 +0000415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 mbedtls_snprintf(str, sizeof(str), "%s #%d:\n", text, ++i);
417 debug_send_line(ssl, level, file, line, str);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt);
420 debug_print_line_by_line(ssl, level, file, line, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000421
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 debug_print_pk(ssl, level, file, line, "crt->", &crt->pk);
Paul Bakker5121ce52009-01-03 21:22:43 +0000423
424 crt = crt->next;
425 }
426}
Hanno Becker612a2f12020-10-09 09:19:39 +0100427#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_X509_REMOVE_INFO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000428
Janos Follath948f4be2018-08-22 01:37:55 +0100429#if defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100430static void mbedtls_debug_printf_ecdh_internal(const mbedtls_ssl_context *ssl,
431 int level, const char *file,
432 int line,
433 const mbedtls_ecdh_context *ecdh,
434 mbedtls_debug_ecdh_attr attr)
Janos Follath948f4be2018-08-22 01:37:55 +0100435{
436#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 const mbedtls_ecdh_context *ctx = ecdh;
Janos Follath948f4be2018-08-22 01:37:55 +0100438#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 const mbedtls_ecdh_context_mbed *ctx = &ecdh->ctx.mbed_ecdh;
Janos Follath948f4be2018-08-22 01:37:55 +0100440#endif
441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 switch (attr) {
Janos Follath948f4be2018-08-22 01:37:55 +0100443 case MBEDTLS_DEBUG_ECDH_Q:
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Q",
445 &ctx->Q);
Janos Follath948f4be2018-08-22 01:37:55 +0100446 break;
447 case MBEDTLS_DEBUG_ECDH_QP:
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Qp",
449 &ctx->Qp);
Janos Follath948f4be2018-08-22 01:37:55 +0100450 break;
451 case MBEDTLS_DEBUG_ECDH_Z:
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 mbedtls_debug_print_mpi(ssl, level, file, line, "ECDH: z",
453 &ctx->z);
Janos Follath948f4be2018-08-22 01:37:55 +0100454 break;
455 default:
456 break;
457 }
458}
459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460void mbedtls_debug_printf_ecdh(const mbedtls_ssl_context *ssl, int level,
461 const char *file, int line,
462 const mbedtls_ecdh_context *ecdh,
463 mbedtls_debug_ecdh_attr attr)
Janos Follath948f4be2018-08-22 01:37:55 +0100464{
465#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh, attr);
Janos Follath948f4be2018-08-22 01:37:55 +0100467#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 switch (ecdh->var) {
Janos Follath948f4be2018-08-22 01:37:55 +0100469 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh,
471 attr);
Janos Follath948f4be2018-08-22 01:37:55 +0100472 }
473#endif
474}
475#endif /* MBEDTLS_ECDH_C */
476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477#endif /* MBEDTLS_DEBUG_C */