blob: 20ef3fd879e70e34b67159fd7cb8bf37e1ad2e66 [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
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
7
Harry Ramseye8e23fb2024-10-11 12:21:30 +01008#include "ssl_misc.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#if defined(MBEDTLS_DEBUG_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/platform.h"
Rich Evans2387c7d2015-01-30 11:10:20 +000013
Valerio Settib4f50762024-01-17 10:24:52 +010014#include "debug_internal.h"
Janos Follath73c616b2019-12-18 15:07:04 +000015#include "mbedtls/error.h"
SimonBd5800b72016-04-26 07:43:27 +010016
17#include <stdarg.h>
18#include <stdio.h>
19#include <string.h>
20
Dave Rodgmaned59ea72023-02-15 17:41:28 +000021/* DEBUG_BUF_SIZE must be at least 2 */
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020022#define DEBUG_BUF_SIZE 512
23
Paul Bakkerc73079a2014-04-25 16:34:30 +020024static int debug_threshold = 0;
Paul Bakkereaebbd52014-04-25 15:04:14 +020025
Gilles Peskine449bd832023-01-11 14:50:10 +010026void mbedtls_debug_set_threshold(int threshold)
Paul Bakkerc73079a2014-04-25 16:34:30 +020027{
28 debug_threshold = threshold;
29}
30
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020031/*
32 * All calls to f_dbg must be made via this function
33 */
Gilles Peskine449bd832023-01-11 14:50:10 +010034static inline void debug_send_line(const mbedtls_ssl_context *ssl, int level,
35 const char *file, int line,
36 const char *str)
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020037{
38 /*
39 * If in a threaded environment, we need a thread identifier.
40 * Since there is no portable way to get one, use the address of the ssl
41 * context instead, as it shouldn't be shared between threads.
42 */
43#if defined(MBEDTLS_THREADING_C)
44 char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */
Gilles Peskine449bd832023-01-11 14:50:10 +010045 mbedtls_snprintf(idstr, sizeof(idstr), "%p: %s", (void *) ssl, str);
46 ssl->conf->f_dbg(ssl->conf->p_dbg, level, file, line, idstr);
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020047#else
Gilles Peskine449bd832023-01-11 14:50:10 +010048 ssl->conf->f_dbg(ssl->conf->p_dbg, level, file, line, str);
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020049#endif
50}
51
Paul Elliott4e589702020-12-09 14:38:01 +000052MBEDTLS_PRINTF_ATTRIBUTE(5, 6)
Gilles Peskine449bd832023-01-11 14:50:10 +010053void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level,
54 const char *file, int line,
55 const char *format, ...)
Paul Bakker5121ce52009-01-03 21:22:43 +000056{
57 va_list argp;
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020058 char str[DEBUG_BUF_SIZE];
Janos Follath865b3eb2019-12-16 11:46:15 +000059 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
valord577536893c2023-02-15 19:31:39 +080060
Dave Rodgman8508e502023-05-16 16:43:48 +010061 MBEDTLS_STATIC_ASSERT(DEBUG_BUF_SIZE >= 2, "DEBUG_BUF_SIZE too small");
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020062
Gilles Peskine449bd832023-01-11 14:50:10 +010063 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +010064 NULL == ssl->conf ||
65 NULL == ssl->conf->f_dbg ||
Gilles Peskine449bd832023-01-11 14:50:10 +010066 level > debug_threshold) {
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020067 return;
Hanno Becker687c5002018-06-29 09:04:46 +010068 }
Paul Bakker5121ce52009-01-03 21:22:43 +000069
Gilles Peskine449bd832023-01-11 14:50:10 +010070 va_start(argp, format);
71 ret = mbedtls_vsnprintf(str, DEBUG_BUF_SIZE, format, argp);
72 va_end(argp);
Paul Bakker5121ce52009-01-03 21:22:43 +000073
valord577536893c2023-02-15 19:31:39 +080074 if (ret < 0) {
valord5775bfcd1c2023-02-15 21:46:47 +080075 ret = 0;
valord577536893c2023-02-15 19:31:39 +080076 } else {
valord577536893c2023-02-15 19:31:39 +080077 if (ret >= DEBUG_BUF_SIZE - 1) {
valord5775bfcd1c2023-02-15 21:46:47 +080078 ret = DEBUG_BUF_SIZE - 2;
valord57724da0cd2023-02-15 19:01:16 +080079 }
valord57725418ac2022-10-31 15:17:37 +080080 }
valord5775bfcd1c2023-02-15 21:46:47 +080081 str[ret] = '\n';
82 str[ret + 1] = '\0';
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020083
valord5775bfcd1c2023-02-15 21:46:47 +080084 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +000085}
86
Gilles Peskine449bd832023-01-11 14:50:10 +010087void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level,
88 const char *file, int line,
89 const char *text, int ret)
Paul Bakker5121ce52009-01-03 21:22:43 +000090{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020091 char str[DEBUG_BUF_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +000092
Gilles Peskine449bd832023-01-11 14:50:10 +010093 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +010094 NULL == ssl->conf ||
95 NULL == ssl->conf->f_dbg ||
Gilles Peskine449bd832023-01-11 14:50:10 +010096 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +000097 return;
Hanno Becker687c5002018-06-29 09:04:46 +010098 }
Paul Bakker5121ce52009-01-03 21:22:43 +000099
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200100 /*
101 * With non-blocking I/O and examples that just retry immediately,
102 * the logs would be quickly flooded with WANT_READ, so ignore that.
Tom Cosgrove1797b052022-12-04 17:19:59 +0000103 * Don't ignore WANT_WRITE however, since it is usually rare.
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200104 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200106 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 }
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 mbedtls_snprintf(str, sizeof(str), "%s() returned %d (-0x%04x)\n",
110 text, ret, (unsigned int) -ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000113}
114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115void mbedtls_debug_print_buf(const mbedtls_ssl_context *ssl, int level,
116 const char *file, int line, const char *text,
117 const unsigned char *buf, size_t len)
Paul Bakker5121ce52009-01-03 21:22:43 +0000118{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200119 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100120 char txt[17];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200121 size_t i, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100124 NULL == ssl->conf ||
125 NULL == ssl->conf->f_dbg ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100128 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 mbedtls_snprintf(str + idx, sizeof(str) - idx, "dumping '%s' (%u bytes)\n",
131 text, (unsigned int) len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 memset(txt, 0, sizeof(txt));
136 for (i = 0; i < len; i++) {
137 if (i >= 4096) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 if (i % 16 == 0) {
142 if (i > 0) {
143 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
144 debug_send_line(ssl, level, file, line, str);
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100145
Paul Bakker92478c32014-04-25 15:18:34 +0200146 idx = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 memset(txt, 0, sizeof(txt));
Paul Bakker92478c32014-04-25 15:18:34 +0200148 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, "%04x: ",
151 (unsigned int) i);
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 }
154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x",
156 (unsigned int) buf[i]);
157 txt[i % 16] = (buf[i] > 31 && buf[i] < 127) ? buf[i] : '.';
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 }
159
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 if (len > 0) {
161 for (/* i = i */; i % 16 != 0; i++) {
162 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " ");
163 }
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
166 debug_send_line(ssl, level, file, line, str);
Paul Bakker92478c32014-04-25 15:18:34 +0200167 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000168}
169
Valerio Settiffac3112025-05-27 09:58:02 +0200170#if defined(MBEDTLS_BIGNUM_C)
171void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl, int level,
172 const char *file, int line,
173 const char *text, const mbedtls_mpi *X)
174{
175 char str[DEBUG_BUF_SIZE];
176 size_t bitlen;
177 size_t idx = 0;
178
179 if (NULL == ssl ||
180 NULL == ssl->conf ||
181 NULL == ssl->conf->f_dbg ||
182 NULL == X ||
183 level > debug_threshold) {
184 return;
185 }
186
187 bitlen = mbedtls_mpi_bitlen(X);
188
189 mbedtls_snprintf(str, sizeof(str), "value of '%s' (%u bits) is:\n",
190 text, (unsigned) bitlen);
191 debug_send_line(ssl, level, file, line, str);
192
193 if (bitlen == 0) {
194 str[0] = ' '; str[1] = '0'; str[2] = '0';
195 idx = 3;
196 } else {
197 int n;
198 for (n = (int) ((bitlen - 1) / 8); n >= 0; n--) {
199 size_t limb_offset = n / sizeof(mbedtls_mpi_uint);
200 size_t offset_in_limb = n % sizeof(mbedtls_mpi_uint);
201 unsigned char octet =
202 (X->p[limb_offset] >> (offset_in_limb * 8)) & 0xff;
203 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x", octet);
204 idx += 3;
205 /* Wrap lines after 16 octets that each take 3 columns */
206 if (idx >= 3 * 16) {
207 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
208 debug_send_line(ssl, level, file, line, str);
209 idx = 0;
210 }
211 }
212 }
213
214 if (idx != 0) {
215 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
216 debug_send_line(ssl, level, file, line, str);
217 }
218}
219#endif /* MBEDTLS_BIGNUM_C */
220
221#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_INFO)
Paul Bakker41c83d32013-03-20 14:39:14 +0100222
Valerio Settia18627a2025-06-19 23:50:05 +0200223/* no-check-names will be removed in mbedtls#10229. */
Valerio Setti3388c4a2025-06-06 15:56:59 +0200224#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || defined(MBEDTLS_PK_USE_PSA_RSA_DATA) //no-check-names
Valerio Setti6676f722025-06-19 23:05:34 +0200225static void mbedtls_debug_print_integer(const mbedtls_ssl_context *ssl, int level,
226 const char *file, int line, const char *text,
Valerio Setti0c924662025-06-19 23:53:55 +0200227 const unsigned char *buf, size_t bitlen)
Valerio Settic1319f42023-07-27 16:20:07 +0200228{
229 char str[DEBUG_BUF_SIZE];
Valerio Setti0c924662025-06-19 23:53:55 +0200230 size_t i, len_bytes = PSA_BITS_TO_BYTES(bitlen), idx = 0;
Valerio Settic1319f42023-07-27 16:20:07 +0200231
232 mbedtls_snprintf(str + idx, sizeof(str) - idx, "value of '%s' (%u bits) is:\n",
Valerio Setti0c924662025-06-19 23:53:55 +0200233 text, (unsigned int) bitlen);
Valerio Settic1319f42023-07-27 16:20:07 +0200234
235 debug_send_line(ssl, level, file, line, str);
236
Valerio Setti3388c4a2025-06-06 15:56:59 +0200237 for (i = 0; i < len_bytes; i++) {
Valerio Settic1319f42023-07-27 16:20:07 +0200238 if (i >= 4096) {
239 break;
240 }
241
242 if (i % 16 == 0) {
243 if (i > 0) {
244 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
245 debug_send_line(ssl, level, file, line, str);
246
247 idx = 0;
248 }
249 }
250
251 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x",
252 (unsigned int) buf[i]);
253 }
254
Valerio Setti3388c4a2025-06-06 15:56:59 +0200255 if (len_bytes > 0) {
Valerio Settic1319f42023-07-27 16:20:07 +0200256 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
257 debug_send_line(ssl, level, file, line, str);
258 }
259}
Valerio Settia18627a2025-06-19 23:50:05 +0200260/* no-check-names will be removed in mbedtls#10229. */
Valerio Setti3388c4a2025-06-06 15:56:59 +0200261#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY || MBEDTLS_PK_USE_PSA_RSA_DATA */ //no-check-names
Valerio Settic1319f42023-07-27 16:20:07 +0200262
Valerio Setti3388c4a2025-06-06 15:56:59 +0200263#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Valerio Setti28ef01a2025-05-23 15:03:26 +0200264static void mbedtls_debug_print_psa_ec(const mbedtls_ssl_context *ssl, int level,
265 const char *file, int line,
266 const char *text, const mbedtls_pk_context *pk)
Valerio Setti7ca7b902023-05-17 15:35:46 +0200267{
268 char str[DEBUG_BUF_SIZE];
Valerio Settic1319f42023-07-27 16:20:07 +0200269 const uint8_t *coord_start;
270 size_t coord_len;
Valerio Setti7ca7b902023-05-17 15:35:46 +0200271
272 if (NULL == ssl ||
273 NULL == ssl->conf ||
274 NULL == ssl->conf->f_dbg ||
275 level > debug_threshold) {
276 return;
277 }
278
279 /* For the description of pk->pk_raw content please refer to the description
280 * psa_export_public_key() function. */
Valerio Settic1319f42023-07-27 16:20:07 +0200281 coord_len = (pk->pub_raw_len - 1)/2;
Valerio Setti7ca7b902023-05-17 15:35:46 +0200282
283 /* X coordinate */
Valerio Settic1319f42023-07-27 16:20:07 +0200284 coord_start = pk->pub_raw + 1;
Valerio Setti7ca7b902023-05-17 15:35:46 +0200285 mbedtls_snprintf(str, sizeof(str), "%s(X)", text);
Valerio Setti6676f722025-06-19 23:05:34 +0200286 mbedtls_debug_print_integer(ssl, level, file, line, str, coord_start, coord_len * 8);
Valerio Setti7ca7b902023-05-17 15:35:46 +0200287
288 /* Y coordinate */
Valerio Settic1319f42023-07-27 16:20:07 +0200289 coord_start = coord_start + coord_len;
Valerio Setti7ca7b902023-05-17 15:35:46 +0200290 mbedtls_snprintf(str, sizeof(str), "%s(Y)", text);
Valerio Setti6676f722025-06-19 23:05:34 +0200291 mbedtls_debug_print_integer(ssl, level, file, line, str, coord_start, coord_len * 8);
Valerio Setti7ca7b902023-05-17 15:35:46 +0200292}
Valerio Settic394fd02025-05-05 15:42:56 +0200293#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Valerio Setti7ca7b902023-05-17 15:35:46 +0200294
Valerio Settia18627a2025-06-19 23:50:05 +0200295/* no-check-names will be removed in mbedtls#10229. */
Valerio Setti3388c4a2025-06-06 15:56:59 +0200296#if defined(MBEDTLS_PK_USE_PSA_RSA_DATA) //no-check-names
297static size_t debug_count_valid_bits(unsigned char **buf, size_t len)
298{
299 size_t i, bits;
300
301 /* Ignore initial null bytes (if any). */
302 while ((len > 0) && (**buf == 0x00)) {
303 (*buf)++;
304 len--;
305 }
306
307 if (len == 0) {
308 return 0;
309 }
310
311 bits = len * 8;
312
313 /* Ignore initial null bits (if any). */
314 for (i = 7; i > 0; i--) {
315 if ((**buf & (0x1 << i)) != 0) {
316 break;
317 }
318 bits--;
319 }
320
321 return bits;
322}
323
324static void mbedtls_debug_print_psa_rsa(const mbedtls_ssl_context *ssl, int level,
325 const char *file, int line,
326 const char *text, const mbedtls_pk_context *pk)
327{
328 char str[DEBUG_BUF_SIZE];
Valerio Settia18627a2025-06-19 23:50:05 +0200329 /* no-check-names will be removed in mbedtls#10229. */
Valerio Setti3388c4a2025-06-06 15:56:59 +0200330 unsigned char key_der[MBEDTLS_PK_MAX_RSA_PUBKEY_RAW_LEN]; //no-check-names
331 unsigned char *start_cur;
332 unsigned char *end_cur;
333 size_t len, bits;
334 int ret;
335
Valerio Setti069617f2025-06-19 23:56:09 +0200336 if (NULL == ssl ||
337 NULL == ssl->conf ||
338 NULL == ssl->conf->f_dbg ||
339 level > debug_threshold) {
340 return;
341 }
342
Valerio Setti3388c4a2025-06-06 15:56:59 +0200343 if (pk->pub_raw_len > sizeof(key_der)) {
344 return;
345 }
346
347 memcpy(key_der, pk->pub_raw, pk->pub_raw_len);
348 start_cur = key_der;
349 end_cur = key_der + pk->pub_raw_len;
350
Valerio Setti1e4423bc2025-06-19 23:16:09 +0200351 /* This integer parsing solution should be replaced with mbedtls_asn1_get_integer().
352 * See #10238. */
Valerio Setti3388c4a2025-06-06 15:56:59 +0200353 ret = mbedtls_asn1_get_tag(&start_cur, end_cur, &len,
354 MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED);
355 if (ret != 0) {
356 return;
357 }
358
359 ret = mbedtls_asn1_get_tag(&start_cur, end_cur, &len, MBEDTLS_ASN1_INTEGER);
360 if (ret != 0) {
361 return;
362 }
363
364 bits = debug_count_valid_bits(&start_cur, len);
365 if (bits == 0) {
366 return;
367 }
368 len = PSA_BITS_TO_BYTES(bits);
369
370 mbedtls_snprintf(str, sizeof(str), "%s.N", text);
Valerio Setti6676f722025-06-19 23:05:34 +0200371 mbedtls_debug_print_integer(ssl, level, file, line, str, start_cur, bits);
Valerio Setti3388c4a2025-06-06 15:56:59 +0200372
373 start_cur += len;
374
375 ret = mbedtls_asn1_get_tag(&start_cur, end_cur, &len, MBEDTLS_ASN1_INTEGER);
376 if (ret != 0) {
377 return;
378 }
379
380 bits = debug_count_valid_bits(&start_cur, len);
381 if (bits == 0) {
382 return;
383 }
384
385 mbedtls_snprintf(str, sizeof(str), "%s.E", text);
Valerio Setti6676f722025-06-19 23:05:34 +0200386 mbedtls_debug_print_integer(ssl, level, file, line, str, start_cur, bits);
Valerio Setti3388c4a2025-06-06 15:56:59 +0200387}
Valerio Settia18627a2025-06-19 23:50:05 +0200388/* no-check-names will be removed in mbedtls#10229. */
Valerio Setti3388c4a2025-06-06 15:56:59 +0200389#endif /* MBEDTLS_PK_USE_PSA_RSA_DATA */ //no-check-names
390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391static void debug_print_pk(const mbedtls_ssl_context *ssl, int level,
392 const char *file, int line,
393 const char *text, const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200394{
395 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200397 char name[16];
398
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 memset(items, 0, sizeof(items));
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200400
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if (mbedtls_pk_debug(pk, items) != 0) {
402 debug_send_line(ssl, level, file, line,
403 "invalid PK context\n");
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200404 return;
405 }
406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 for (i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++) {
408 if (items[i].type == MBEDTLS_PK_DEBUG_NONE) {
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200409 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 mbedtls_snprintf(name, sizeof(name), "%s%s", text, items[i].name);
413 name[sizeof(name) - 1] = '\0';
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200414
Valerio Setti797e3962023-07-27 16:19:00 +0200415#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 if (items[i].type == MBEDTLS_PK_DEBUG_MPI) {
417 mbedtls_debug_print_mpi(ssl, level, file, line, name, items[i].value);
418 } else
Valerio Setti797e3962023-07-27 16:19:00 +0200419#endif /* MBEDTLS_RSA_C */
Valerio Settia18627a2025-06-19 23:50:05 +0200420/* no-check-names will be removed in mbedtls#10229. */
Valerio Setti3388c4a2025-06-06 15:56:59 +0200421#if defined(MBEDTLS_PK_USE_PSA_RSA_DATA) //no-check-names
422 if (items[i].type == MBEDTLS_PK_DEBUG_PSA_RSA) { //no-check-names
423 mbedtls_debug_print_psa_rsa(ssl, level, file, line, name, items[i].value);
424 } else
425#endif /* MBEDTLS_PK_USE_PSA_RSA_DATA */ //no-check-names
Valerio Settic394fd02025-05-05 15:42:56 +0200426#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Valerio Setti7ca7b902023-05-17 15:35:46 +0200427 if (items[i].type == MBEDTLS_PK_DEBUG_PSA_EC) {
428 mbedtls_debug_print_psa_ec(ssl, level, file, line, name, items[i].value);
429 } else
Valerio Settic394fd02025-05-05 15:42:56 +0200430#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 { debug_send_line(ssl, level, file, line,
432 "should not happen\n"); }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200433 }
434}
435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436static void debug_print_line_by_line(const mbedtls_ssl_context *ssl, int level,
437 const char *file, int line, const char *text)
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200438{
439 char str[DEBUG_BUF_SIZE];
440 const char *start, *cur;
441
442 start = text;
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 for (cur = text; *cur != '\0'; cur++) {
444 if (*cur == '\n') {
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000445 size_t len = (size_t) (cur - start) + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 if (len > DEBUG_BUF_SIZE - 1) {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200447 len = DEBUG_BUF_SIZE - 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 }
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 memcpy(str, start, len);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200451 str[len] = '\0';
452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 debug_send_line(ssl, level, file, line, str);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200454
455 start = cur + 1;
456 }
457 }
458}
459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level,
461 const char *file, int line,
462 const char *text, const mbedtls_x509_crt *crt)
Paul Bakker5121ce52009-01-03 21:22:43 +0000463{
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200464 char str[DEBUG_BUF_SIZE];
465 int i = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000466
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100468 NULL == ssl->conf ||
469 NULL == ssl->conf->f_dbg ||
470 NULL == crt ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000472 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100473 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 while (crt != NULL) {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000476 char buf[1024];
Paul Bakker5121ce52009-01-03 21:22:43 +0000477
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 mbedtls_snprintf(str, sizeof(str), "%s #%d:\n", text, ++i);
479 debug_send_line(ssl, level, file, line, str);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200480
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt);
482 debug_print_line_by_line(ssl, level, file, line, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000483
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 debug_print_pk(ssl, level, file, line, "crt->", &crt->pk);
Paul Bakker5121ce52009-01-03 21:22:43 +0000485
486 crt = crt->next;
487 }
488}
Hanno Becker612a2f12020-10-09 09:19:39 +0100489#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_X509_REMOVE_INFO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491#endif /* MBEDTLS_DEBUG_C */