blob: 0983cb0fb56066a858a64440c325fbca3eeeb945 [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
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 memset(txt, 0, sizeof(txt));
148 for (i = 0; i < len; i++) {
149 if (i >= 4096) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000150 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 if (i % 16 == 0) {
154 if (i > 0) {
155 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
156 debug_send_line(ssl, level, file, line, str);
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100157
Paul Bakker92478c32014-04-25 15:18:34 +0200158 idx = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 memset(txt, 0, sizeof(txt));
Paul Bakker92478c32014-04-25 15:18:34 +0200160 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, "%04x: ",
163 (unsigned int) i);
Paul Bakker5121ce52009-01-03 21:22:43 +0000164
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 }
166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x",
168 (unsigned int) buf[i]);
169 txt[i % 16] = (buf[i] > 31 && buf[i] < 127) ? buf[i] : '.';
Paul Bakker5121ce52009-01-03 21:22:43 +0000170 }
171
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 if (len > 0) {
173 for (/* i = i */; i % 16 != 0; i++) {
174 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " ");
175 }
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
178 debug_send_line(ssl, level, file, line, str);
Paul Bakker92478c32014-04-25 15:18:34 +0200179 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000180}
181
Valerio Setti6c496a12023-04-07 15:53:51 +0200182#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100183void mbedtls_debug_print_ecp(const mbedtls_ssl_context *ssl, int level,
184 const char *file, int line,
185 const char *text, const mbedtls_ecp_point *X)
Paul Bakker41c83d32013-03-20 14:39:14 +0100186{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200187 char str[DEBUG_BUF_SIZE];
Paul Bakker41c83d32013-03-20 14:39:14 +0100188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100190 NULL == ssl->conf ||
191 NULL == ssl->conf->f_dbg ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 level > debug_threshold) {
Paul Bakkerc73079a2014-04-25 16:34:30 +0200193 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100194 }
Paul Bakkerc73079a2014-04-25 16:34:30 +0200195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 mbedtls_snprintf(str, sizeof(str), "%s(X)", text);
197 mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->X);
Paul Bakker41c83d32013-03-20 14:39:14 +0100198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 mbedtls_snprintf(str, sizeof(str), "%s(Y)", text);
200 mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->Y);
Paul Bakker41c83d32013-03-20 14:39:14 +0100201}
Valerio Setti6c496a12023-04-07 15:53:51 +0200202#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker41c83d32013-03-20 14:39:14 +0100203
Valerio Setti7ca7b902023-05-17 15:35:46 +0200204#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Settic1319f42023-07-27 16:20:07 +0200205static void mbedtls_debug_print_ec_coord(const mbedtls_ssl_context *ssl, int level,
206 const char *file, int line, const char *text,
207 const unsigned char *buf, size_t len)
208{
209 char str[DEBUG_BUF_SIZE];
210 size_t i, idx = 0;
211
212 mbedtls_snprintf(str + idx, sizeof(str) - idx, "value of '%s' (%u bits) is:\n",
213 text, (unsigned int) len * 8);
214
215 debug_send_line(ssl, level, file, line, str);
216
Valerio Settic1319f42023-07-27 16:20:07 +0200217 for (i = 0; i < len; i++) {
218 if (i >= 4096) {
219 break;
220 }
221
222 if (i % 16 == 0) {
223 if (i > 0) {
224 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
225 debug_send_line(ssl, level, file, line, str);
226
227 idx = 0;
228 }
229 }
230
231 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x",
232 (unsigned int) buf[i]);
233 }
234
235 if (len > 0) {
236 for (/* i = i */; i % 16 != 0; i++) {
237 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " ");
238 }
239
240 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
241 debug_send_line(ssl, level, file, line, str);
242 }
243}
244
Valerio Setti7ca7b902023-05-17 15:35:46 +0200245void mbedtls_debug_print_psa_ec(const mbedtls_ssl_context *ssl, int level,
246 const char *file, int line,
247 const char *text, const mbedtls_pk_context *pk)
248{
249 char str[DEBUG_BUF_SIZE];
Valerio Settic1319f42023-07-27 16:20:07 +0200250 const uint8_t *coord_start;
251 size_t coord_len;
Valerio Setti7ca7b902023-05-17 15:35:46 +0200252
253 if (NULL == ssl ||
254 NULL == ssl->conf ||
255 NULL == ssl->conf->f_dbg ||
256 level > debug_threshold) {
257 return;
258 }
259
260 /* For the description of pk->pk_raw content please refer to the description
261 * psa_export_public_key() function. */
Valerio Settic1319f42023-07-27 16:20:07 +0200262 coord_len = (pk->pub_raw_len - 1)/2;
Valerio Setti7ca7b902023-05-17 15:35:46 +0200263
264 /* X coordinate */
Valerio Settic1319f42023-07-27 16:20:07 +0200265 coord_start = pk->pub_raw + 1;
Valerio Setti7ca7b902023-05-17 15:35:46 +0200266 mbedtls_snprintf(str, sizeof(str), "%s(X)", text);
Valerio Settic1319f42023-07-27 16:20:07 +0200267 mbedtls_debug_print_ec_coord(ssl, level, file, line, str, coord_start, coord_len);
Valerio Setti7ca7b902023-05-17 15:35:46 +0200268
269 /* Y coordinate */
Valerio Settic1319f42023-07-27 16:20:07 +0200270 coord_start = coord_start + coord_len;
Valerio Setti7ca7b902023-05-17 15:35:46 +0200271 mbedtls_snprintf(str, sizeof(str), "%s(Y)", text);
Valerio Settic1319f42023-07-27 16:20:07 +0200272 mbedtls_debug_print_ec_coord(ssl, level, file, line, str, coord_start, coord_len);
Valerio Setti7ca7b902023-05-17 15:35:46 +0200273}
274#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
275
Valerio Settic1319f42023-07-27 16:20:07 +0200276#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100277void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl, int level,
278 const char *file, int line,
279 const char *text, const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000280{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200281 char str[DEBUG_BUF_SIZE];
Gilles Peskineb26696b2021-06-02 20:17:46 +0200282 size_t bitlen;
283 size_t idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100286 NULL == ssl->conf ||
287 NULL == ssl->conf->f_dbg ||
288 NULL == X ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100291 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 bitlen = mbedtls_mpi_bitlen(X);
Paul Bakker5121ce52009-01-03 21:22:43 +0000294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 mbedtls_snprintf(str, sizeof(str), "value of '%s' (%u bits) is:\n",
296 text, (unsigned) bitlen);
297 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 if (bitlen == 0) {
Gilles Peskineb26696b2021-06-02 20:17:46 +0200300 str[0] = ' '; str[1] = '0'; str[2] = '0';
301 idx = 3;
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 } else {
Gilles Peskineb26696b2021-06-02 20:17:46 +0200303 int n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 for (n = (int) ((bitlen - 1) / 8); n >= 0; n--) {
305 size_t limb_offset = n / sizeof(mbedtls_mpi_uint);
306 size_t offset_in_limb = n % sizeof(mbedtls_mpi_uint);
Gilles Peskineb26696b2021-06-02 20:17:46 +0200307 unsigned char octet =
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 (X->p[limb_offset] >> (offset_in_limb * 8)) & 0xff;
309 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x", octet);
Gilles Peskineb26696b2021-06-02 20:17:46 +0200310 idx += 3;
311 /* Wrap lines after 16 octets that each take 3 columns */
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 if (idx >= 3 * 16) {
313 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
314 debug_send_line(ssl, level, file, line, str);
Gilles Peskineb26696b2021-06-02 20:17:46 +0200315 idx = 0;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000316 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000317 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000318 }
319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 if (idx != 0) {
321 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
322 debug_send_line(ssl, level, file, line, str);
Gilles Peskineb26696b2021-06-02 20:17:46 +0200323 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000324}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000326
Hanno Becker612a2f12020-10-09 09:19:39 +0100327#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100328static void debug_print_pk(const mbedtls_ssl_context *ssl, int level,
329 const char *file, int line,
330 const char *text, const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200331{
332 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200334 char name[16];
335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 memset(items, 0, sizeof(items));
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 if (mbedtls_pk_debug(pk, items) != 0) {
339 debug_send_line(ssl, level, file, line,
340 "invalid PK context\n");
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200341 return;
342 }
343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 for (i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++) {
345 if (items[i].type == MBEDTLS_PK_DEBUG_NONE) {
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200346 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200348
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 mbedtls_snprintf(name, sizeof(name), "%s%s", text, items[i].name);
350 name[sizeof(name) - 1] = '\0';
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200351
Valerio Setti797e3962023-07-27 16:19:00 +0200352#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 if (items[i].type == MBEDTLS_PK_DEBUG_MPI) {
354 mbedtls_debug_print_mpi(ssl, level, file, line, name, items[i].value);
355 } else
Valerio Setti797e3962023-07-27 16:19:00 +0200356#endif /* MBEDTLS_RSA_C */
Valerio Setti6c496a12023-04-07 15:53:51 +0200357#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if (items[i].type == MBEDTLS_PK_DEBUG_ECP) {
359 mbedtls_debug_print_ecp(ssl, level, file, line, name, items[i].value);
360 } else
Valerio Setti797e3962023-07-27 16:19:00 +0200361#endif /* MBEDTLS_ECP_LIGHT */
Valerio Setti7ca7b902023-05-17 15:35:46 +0200362#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
363 if (items[i].type == MBEDTLS_PK_DEBUG_PSA_EC) {
364 mbedtls_debug_print_psa_ec(ssl, level, file, line, name, items[i].value);
365 } else
Valerio Settic1319f42023-07-27 16:20:07 +0200366#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 { debug_send_line(ssl, level, file, line,
368 "should not happen\n"); }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200369 }
370}
371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372static void debug_print_line_by_line(const mbedtls_ssl_context *ssl, int level,
373 const char *file, int line, const char *text)
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200374{
375 char str[DEBUG_BUF_SIZE];
376 const char *start, *cur;
377
378 start = text;
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 for (cur = text; *cur != '\0'; cur++) {
380 if (*cur == '\n') {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200381 size_t len = cur - start + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 if (len > DEBUG_BUF_SIZE - 1) {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200383 len = DEBUG_BUF_SIZE - 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 }
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 memcpy(str, start, len);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200387 str[len] = '\0';
388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 debug_send_line(ssl, level, file, line, str);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200390
391 start = cur + 1;
392 }
393 }
394}
395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level,
397 const char *file, int line,
398 const char *text, const mbedtls_x509_crt *crt)
Paul Bakker5121ce52009-01-03 21:22:43 +0000399{
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200400 char str[DEBUG_BUF_SIZE];
401 int i = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000402
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100404 NULL == ssl->conf ||
405 NULL == ssl->conf->f_dbg ||
406 NULL == crt ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100409 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000410
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 while (crt != NULL) {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000412 char buf[1024];
Paul Bakker5121ce52009-01-03 21:22:43 +0000413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 mbedtls_snprintf(str, sizeof(str), "%s #%d:\n", text, ++i);
415 debug_send_line(ssl, level, file, line, str);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt);
418 debug_print_line_by_line(ssl, level, file, line, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 debug_print_pk(ssl, level, file, line, "crt->", &crt->pk);
Paul Bakker5121ce52009-01-03 21:22:43 +0000421
422 crt = crt->next;
423 }
424}
Hanno Becker612a2f12020-10-09 09:19:39 +0100425#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_X509_REMOVE_INFO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000426
Valerio Settid0371b02023-07-25 10:57:01 +0200427#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED) && \
428 defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100429static void mbedtls_debug_printf_ecdh_internal(const mbedtls_ssl_context *ssl,
430 int level, const char *file,
431 int line,
432 const mbedtls_ecdh_context *ecdh,
433 mbedtls_debug_ecdh_attr attr)
Janos Follath948f4be2018-08-22 01:37:55 +0100434{
435#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 const mbedtls_ecdh_context *ctx = ecdh;
Janos Follath948f4be2018-08-22 01:37:55 +0100437#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 const mbedtls_ecdh_context_mbed *ctx = &ecdh->ctx.mbed_ecdh;
Janos Follath948f4be2018-08-22 01:37:55 +0100439#endif
440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 switch (attr) {
Janos Follath948f4be2018-08-22 01:37:55 +0100442 case MBEDTLS_DEBUG_ECDH_Q:
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Q",
444 &ctx->Q);
Janos Follath948f4be2018-08-22 01:37:55 +0100445 break;
446 case MBEDTLS_DEBUG_ECDH_QP:
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Qp",
448 &ctx->Qp);
Janos Follath948f4be2018-08-22 01:37:55 +0100449 break;
450 case MBEDTLS_DEBUG_ECDH_Z:
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 mbedtls_debug_print_mpi(ssl, level, file, line, "ECDH: z",
452 &ctx->z);
Janos Follath948f4be2018-08-22 01:37:55 +0100453 break;
454 default:
455 break;
456 }
457}
458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459void mbedtls_debug_printf_ecdh(const mbedtls_ssl_context *ssl, int level,
460 const char *file, int line,
461 const mbedtls_ecdh_context *ecdh,
462 mbedtls_debug_ecdh_attr attr)
Janos Follath948f4be2018-08-22 01:37:55 +0100463{
464#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh, attr);
Janos Follath948f4be2018-08-22 01:37:55 +0100466#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 switch (ecdh->var) {
Janos Follath948f4be2018-08-22 01:37:55 +0100468 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh,
470 attr);
Janos Follath948f4be2018-08-22 01:37:55 +0100471 }
472#endif
473}
Valerio Settid0371b02023-07-25 10:57:01 +0200474#endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED &&
475 MBEDTLS_ECDH_C */
Janos Follath948f4be2018-08-22 01:37:55 +0100476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477#endif /* MBEDTLS_DEBUG_C */