2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
|
* Debugging routines
|
|
|
|
|
*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2023-11-02 19:47:20 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
|
|
|
|
|
2024-10-11 12:21:30 +01:00
|
|
|
#include "ssl_misc.h"
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_DEBUG_C)
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/platform.h"
|
2015-01-30 11:10:20 +00:00
|
|
|
|
2024-01-17 10:24:52 +01:00
|
|
|
#include "debug_internal.h"
|
2019-12-18 15:07:04 +00:00
|
|
|
#include "mbedtls/error.h"
|
2016-04-26 07:43:27 +01:00
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2023-02-15 17:41:28 +00:00
|
|
|
/* DEBUG_BUF_SIZE must be at least 2 */
|
2015-06-23 12:04:52 +02:00
|
|
|
#define DEBUG_BUF_SIZE 512
|
|
|
|
|
|
2014-04-25 16:34:30 +02:00
|
|
|
static int debug_threshold = 0;
|
2014-04-25 15:04:14 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_debug_set_threshold(int threshold)
|
2014-04-25 16:34:30 +02:00
|
|
|
{
|
|
|
|
|
debug_threshold = threshold;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-31 16:11:00 +02:00
|
|
|
/*
|
|
|
|
|
* All calls to f_dbg must be made via this function
|
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static inline void debug_send_line(const mbedtls_ssl_context *ssl, int level,
|
|
|
|
|
const char *file, int line,
|
|
|
|
|
const char *str)
|
2015-08-31 16:11:00 +02:00
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* If in a threaded environment, we need a thread identifier.
|
|
|
|
|
* Since there is no portable way to get one, use the address of the ssl
|
|
|
|
|
* context instead, as it shouldn't be shared between threads.
|
|
|
|
|
*/
|
|
|
|
|
#if defined(MBEDTLS_THREADING_C)
|
|
|
|
|
char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_snprintf(idstr, sizeof(idstr), "%p: %s", (void *) ssl, str);
|
|
|
|
|
ssl->conf->f_dbg(ssl->conf->p_dbg, level, file, line, idstr);
|
2015-08-31 16:11:00 +02:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->conf->f_dbg(ssl->conf->p_dbg, level, file, line, str);
|
2015-08-31 16:11:00 +02:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-09 14:38:01 +00:00
|
|
|
MBEDTLS_PRINTF_ATTRIBUTE(5, 6)
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level,
|
|
|
|
|
const char *file, int line,
|
|
|
|
|
const char *format, ...)
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
|
|
|
|
va_list argp;
|
2015-06-29 20:08:23 +02:00
|
|
|
char str[DEBUG_BUF_SIZE];
|
2019-12-16 11:46:15 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-02-15 19:31:39 +08:00
|
|
|
|
2023-05-16 16:43:48 +01:00
|
|
|
MBEDTLS_STATIC_ASSERT(DEBUG_BUF_SIZE >= 2, "DEBUG_BUF_SIZE too small");
|
2015-06-23 12:04:52 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (NULL == ssl ||
|
2018-06-29 09:04:46 +01:00
|
|
|
NULL == ssl->conf ||
|
|
|
|
|
NULL == ssl->conf->f_dbg ||
|
2023-01-11 14:50:10 +01:00
|
|
|
level > debug_threshold) {
|
2015-06-29 20:08:23 +02:00
|
|
|
return;
|
2018-06-29 09:04:46 +01:00
|
|
|
}
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
va_start(argp, format);
|
|
|
|
|
ret = mbedtls_vsnprintf(str, DEBUG_BUF_SIZE, format, argp);
|
|
|
|
|
va_end(argp);
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2023-02-15 19:31:39 +08:00
|
|
|
if (ret < 0) {
|
2023-02-15 21:46:47 +08:00
|
|
|
ret = 0;
|
2023-02-15 19:31:39 +08:00
|
|
|
} else {
|
|
|
|
|
if (ret >= DEBUG_BUF_SIZE - 1) {
|
2023-02-15 21:46:47 +08:00
|
|
|
ret = DEBUG_BUF_SIZE - 2;
|
2023-02-15 19:01:16 +08:00
|
|
|
}
|
2022-10-31 15:17:37 +08:00
|
|
|
}
|
2023-02-15 21:46:47 +08:00
|
|
|
str[ret] = '\n';
|
|
|
|
|
str[ret + 1] = '\0';
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2023-02-15 21:46:47 +08:00
|
|
|
debug_send_line(ssl, level, file, line, str);
|
2015-06-23 12:04:52 +02:00
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level,
|
|
|
|
|
const char *file, int line,
|
|
|
|
|
const char *text, int ret)
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2015-06-23 12:04:52 +02:00
|
|
|
char str[DEBUG_BUF_SIZE];
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (NULL == ssl ||
|
2018-06-29 09:04:46 +01:00
|
|
|
NULL == ssl->conf ||
|
|
|
|
|
NULL == ssl->conf->f_dbg ||
|
2023-01-11 14:50:10 +01:00
|
|
|
level > debug_threshold) {
|
2009-01-03 21:22:43 +00:00
|
|
|
return;
|
2018-06-29 09:04:46 +01:00
|
|
|
}
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-05-11 18:52:25 +02:00
|
|
|
/*
|
|
|
|
|
* With non-blocking I/O and examples that just retry immediately,
|
|
|
|
|
* the logs would be quickly flooded with WANT_READ, so ignore that.
|
2022-12-04 17:19:59 +00:00
|
|
|
* Don't ignore WANT_WRITE however, since it is usually rare.
|
2015-05-11 18:52:25 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
|
2015-05-11 18:52:25 +02:00
|
|
|
return;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2015-05-11 18:52:25 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_snprintf(str, sizeof(str), "%s() returned %d (-0x%04x)\n",
|
|
|
|
|
text, ret, (unsigned int) -ret);
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
debug_send_line(ssl, level, file, line, str);
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_debug_print_buf(const mbedtls_ssl_context *ssl, int level,
|
|
|
|
|
const char *file, int line, const char *text,
|
|
|
|
|
const unsigned char *buf, size_t len)
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2015-06-23 12:04:52 +02:00
|
|
|
char str[DEBUG_BUF_SIZE];
|
2014-11-19 10:17:21 +01:00
|
|
|
char txt[17];
|
2015-06-22 11:50:58 +02:00
|
|
|
size_t i, idx = 0;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (NULL == ssl ||
|
2018-06-29 09:04:46 +01:00
|
|
|
NULL == ssl->conf ||
|
|
|
|
|
NULL == ssl->conf->f_dbg ||
|
2023-01-11 14:50:10 +01:00
|
|
|
level > debug_threshold) {
|
2009-01-03 21:22:43 +00:00
|
|
|
return;
|
2018-06-29 09:04:46 +01:00
|
|
|
}
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_snprintf(str + idx, sizeof(str) - idx, "dumping '%s' (%u bytes)\n",
|
|
|
|
|
text, (unsigned int) len);
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
debug_send_line(ssl, level, file, line, str);
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(txt, 0, sizeof(txt));
|
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
|
if (i >= 4096) {
|
2009-01-03 21:22:43 +00:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (i % 16 == 0) {
|
|
|
|
|
if (i > 0) {
|
|
|
|
|
mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
|
|
|
|
|
debug_send_line(ssl, level, file, line, str);
|
2014-11-19 10:17:21 +01:00
|
|
|
|
2014-04-25 15:18:34 +02:00
|
|
|
idx = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(txt, 0, sizeof(txt));
|
2014-04-25 15:18:34 +02:00
|
|
|
}
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, "%04x: ",
|
|
|
|
|
(unsigned int) i);
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x",
|
|
|
|
|
(unsigned int) buf[i]);
|
|
|
|
|
txt[i % 16] = (buf[i] > 31 && buf[i] < 127) ? buf[i] : '.';
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (len > 0) {
|
|
|
|
|
for (/* i = i */; i % 16 != 0; i++) {
|
|
|
|
|
idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " ");
|
|
|
|
|
}
|
2014-11-19 10:17:21 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
|
|
|
|
|
debug_send_line(ssl, level, file, line, str);
|
2014-04-25 15:18:34 +02:00
|
|
|
}
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-27 09:58:02 +02:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_INFO)
|
2023-01-11 14:50:10 +01:00
|
|
|
static void debug_print_pk(const mbedtls_ssl_context *ssl, int level,
|
|
|
|
|
const char *file, int line,
|
|
|
|
|
const char *text, const mbedtls_pk_context *pk)
|
2013-08-14 18:04:18 +02:00
|
|
|
{
|
2025-11-19 15:21:18 +01:00
|
|
|
unsigned char buf[PSA_EXPORT_PUBLIC_KEY_MAX_SIZE];
|
|
|
|
|
size_t buf_len;
|
|
|
|
|
int ret;
|
2013-08-14 18:04:18 +02:00
|
|
|
|
2025-11-19 15:21:18 +01:00
|
|
|
ret = mbedtls_pk_write_pubkey_psa(pk, buf, sizeof(buf), &buf_len);
|
|
|
|
|
if (ret == 0) {
|
|
|
|
|
mbedtls_debug_print_buf(ssl, level, file, line, text, buf, buf_len);
|
|
|
|
|
} else {
|
|
|
|
|
mbedtls_debug_print_msg(ssl, level, file, line,
|
|
|
|
|
"failed to export public key from PK context");
|
2013-08-14 18:04:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static void debug_print_line_by_line(const mbedtls_ssl_context *ssl, int level,
|
|
|
|
|
const char *file, int line, const char *text)
|
2015-06-23 16:34:24 +02:00
|
|
|
{
|
|
|
|
|
char str[DEBUG_BUF_SIZE];
|
|
|
|
|
const char *start, *cur;
|
|
|
|
|
|
|
|
|
|
start = text;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (cur = text; *cur != '\0'; cur++) {
|
|
|
|
|
if (*cur == '\n') {
|
2023-11-04 12:20:09 +00:00
|
|
|
size_t len = (size_t) (cur - start) + 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (len > DEBUG_BUF_SIZE - 1) {
|
2015-06-23 16:34:24 +02:00
|
|
|
len = DEBUG_BUF_SIZE - 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2015-06-23 16:34:24 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(str, start, len);
|
2015-06-23 16:34:24 +02:00
|
|
|
str[len] = '\0';
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
debug_send_line(ssl, level, file, line, str);
|
2015-06-23 16:34:24 +02:00
|
|
|
|
|
|
|
|
start = cur + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level,
|
|
|
|
|
const char *file, int line,
|
|
|
|
|
const char *text, const mbedtls_x509_crt *crt)
|
2009-01-03 21:22:43 +00:00
|
|
|
{
|
2015-06-23 16:34:24 +02:00
|
|
|
char str[DEBUG_BUF_SIZE];
|
|
|
|
|
int i = 0;
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (NULL == ssl ||
|
2018-06-29 09:04:46 +01:00
|
|
|
NULL == ssl->conf ||
|
|
|
|
|
NULL == ssl->conf->f_dbg ||
|
|
|
|
|
NULL == crt ||
|
2023-01-11 14:50:10 +01:00
|
|
|
level > debug_threshold) {
|
2009-01-03 21:22:43 +00:00
|
|
|
return;
|
2018-06-29 09:04:46 +01:00
|
|
|
}
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (crt != NULL) {
|
2009-05-02 15:13:40 +00:00
|
|
|
char buf[1024];
|
2014-04-25 15:04:14 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_snprintf(str, sizeof(str), "%s #%d:\n", text, ++i);
|
|
|
|
|
debug_send_line(ssl, level, file, line, str);
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt);
|
|
|
|
|
debug_print_line_by_line(ssl, level, file, line, buf);
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2025-11-19 15:21:18 +01:00
|
|
|
debug_print_pk(ssl, level, file, line, "crt->PK", &crt->pk);
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
|
crt = crt->next;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-09 09:19:39 +01:00
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_X509_REMOVE_INFO */
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_DEBUG_C */
|