Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Debugging routines |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Harry Ramsey | e8e23fb | 2024-10-11 12:21:30 +0100 | [diff] [blame] | 8 | #include "ssl_misc.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 9 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 10 | #if defined(MBEDTLS_DEBUG_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 12 | #include "mbedtls/platform.h" |
Rich Evans | 2387c7d | 2015-01-30 11:10:20 +0000 | [diff] [blame] | 13 | |
Valerio Setti | b4f5076 | 2024-01-17 10:24:52 +0100 | [diff] [blame] | 14 | #include "debug_internal.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 15 | #include "mbedtls/error.h" |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 16 | |
| 17 | #include <stdarg.h> |
| 18 | #include <stdio.h> |
| 19 | #include <string.h> |
| 20 | |
Dave Rodgman | ed59ea7 | 2023-02-15 17:41:28 +0000 | [diff] [blame] | 21 | /* DEBUG_BUF_SIZE must be at least 2 */ |
Manuel Pégourié-Gonnard | d23f593 | 2015-06-23 12:04:52 +0200 | [diff] [blame] | 22 | #define DEBUG_BUF_SIZE 512 |
| 23 | |
Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 24 | static int debug_threshold = 0; |
Paul Bakker | eaebbd5 | 2014-04-25 15:04:14 +0200 | [diff] [blame] | 25 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 26 | void mbedtls_debug_set_threshold(int threshold) |
Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 27 | { |
| 28 | debug_threshold = threshold; |
| 29 | } |
| 30 | |
Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 31 | /* |
| 32 | * All calls to f_dbg must be made via this function |
| 33 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 34 | static 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é-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 37 | { |
| 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 Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 45 | 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é-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 47 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 48 | ssl->conf->f_dbg(ssl->conf->p_dbg, level, file, line, str); |
Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 49 | #endif |
| 50 | } |
| 51 | |
Paul Elliott | 4e58970 | 2020-12-09 14:38:01 +0000 | [diff] [blame] | 52 | MBEDTLS_PRINTF_ATTRIBUTE(5, 6) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 53 | void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level, |
| 54 | const char *file, int line, |
| 55 | const char *format, ...) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 56 | { |
| 57 | va_list argp; |
Manuel Pégourié-Gonnard | b74c245 | 2015-06-29 20:08:23 +0200 | [diff] [blame] | 58 | char str[DEBUG_BUF_SIZE]; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 59 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
valord577 | 536893c | 2023-02-15 19:31:39 +0800 | [diff] [blame] | 60 | |
Dave Rodgman | 8508e50 | 2023-05-16 16:43:48 +0100 | [diff] [blame] | 61 | MBEDTLS_STATIC_ASSERT(DEBUG_BUF_SIZE >= 2, "DEBUG_BUF_SIZE too small"); |
Manuel Pégourié-Gonnard | d23f593 | 2015-06-23 12:04:52 +0200 | [diff] [blame] | 62 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 63 | if (NULL == ssl || |
Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 64 | NULL == ssl->conf || |
| 65 | NULL == ssl->conf->f_dbg || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 66 | level > debug_threshold) { |
Manuel Pégourié-Gonnard | b74c245 | 2015-06-29 20:08:23 +0200 | [diff] [blame] | 67 | return; |
Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 68 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 69 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 70 | va_start(argp, format); |
| 71 | ret = mbedtls_vsnprintf(str, DEBUG_BUF_SIZE, format, argp); |
| 72 | va_end(argp); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 73 | |
valord577 | 536893c | 2023-02-15 19:31:39 +0800 | [diff] [blame] | 74 | if (ret < 0) { |
valord577 | 5bfcd1c | 2023-02-15 21:46:47 +0800 | [diff] [blame] | 75 | ret = 0; |
valord577 | 536893c | 2023-02-15 19:31:39 +0800 | [diff] [blame] | 76 | } else { |
valord577 | 536893c | 2023-02-15 19:31:39 +0800 | [diff] [blame] | 77 | if (ret >= DEBUG_BUF_SIZE - 1) { |
valord577 | 5bfcd1c | 2023-02-15 21:46:47 +0800 | [diff] [blame] | 78 | ret = DEBUG_BUF_SIZE - 2; |
valord577 | 24da0cd | 2023-02-15 19:01:16 +0800 | [diff] [blame] | 79 | } |
valord577 | 25418ac | 2022-10-31 15:17:37 +0800 | [diff] [blame] | 80 | } |
valord577 | 5bfcd1c | 2023-02-15 21:46:47 +0800 | [diff] [blame] | 81 | str[ret] = '\n'; |
| 82 | str[ret + 1] = '\0'; |
Manuel Pégourié-Gonnard | b74c245 | 2015-06-29 20:08:23 +0200 | [diff] [blame] | 83 | |
valord577 | 5bfcd1c | 2023-02-15 21:46:47 +0800 | [diff] [blame] | 84 | debug_send_line(ssl, level, file, line, str); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 87 | void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level, |
| 88 | const char *file, int line, |
| 89 | const char *text, int ret) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 90 | { |
Manuel Pégourié-Gonnard | d23f593 | 2015-06-23 12:04:52 +0200 | [diff] [blame] | 91 | char str[DEBUG_BUF_SIZE]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 92 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 93 | if (NULL == ssl || |
Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 94 | NULL == ssl->conf || |
| 95 | NULL == ssl->conf->f_dbg || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 96 | level > debug_threshold) { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 97 | return; |
Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 98 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 99 | |
Manuel Pégourié-Gonnard | 4cba1a7 | 2015-05-11 18:52:25 +0200 | [diff] [blame] | 100 | /* |
| 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 Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 103 | * Don't ignore WANT_WRITE however, since it is usually rare. |
Manuel Pégourié-Gonnard | 4cba1a7 | 2015-05-11 18:52:25 +0200 | [diff] [blame] | 104 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 105 | if (ret == MBEDTLS_ERR_SSL_WANT_READ) { |
Manuel Pégourié-Gonnard | 4cba1a7 | 2015-05-11 18:52:25 +0200 | [diff] [blame] | 106 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 107 | } |
Manuel Pégourié-Gonnard | 4cba1a7 | 2015-05-11 18:52:25 +0200 | [diff] [blame] | 108 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 109 | mbedtls_snprintf(str, sizeof(str), "%s() returned %d (-0x%04x)\n", |
| 110 | text, ret, (unsigned int) -ret); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 112 | debug_send_line(ssl, level, file, line, str); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 115 | void 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 118 | { |
Manuel Pégourié-Gonnard | d23f593 | 2015-06-23 12:04:52 +0200 | [diff] [blame] | 119 | char str[DEBUG_BUF_SIZE]; |
Manuel Pégourié-Gonnard | 8c9223d | 2014-11-19 10:17:21 +0100 | [diff] [blame] | 120 | char txt[17]; |
Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 121 | size_t i, idx = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 123 | if (NULL == ssl || |
Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 124 | NULL == ssl->conf || |
| 125 | NULL == ssl->conf->f_dbg || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 126 | level > debug_threshold) { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 127 | return; |
Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 128 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 129 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 130 | mbedtls_snprintf(str + idx, sizeof(str) - idx, "dumping '%s' (%u bytes)\n", |
| 131 | text, (unsigned int) len); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 132 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 133 | debug_send_line(ssl, level, file, line, str); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 135 | memset(txt, 0, sizeof(txt)); |
| 136 | for (i = 0; i < len; i++) { |
| 137 | if (i >= 4096) { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 138 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 139 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 141 | 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é-Gonnard | 8c9223d | 2014-11-19 10:17:21 +0100 | [diff] [blame] | 145 | |
Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 146 | idx = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 147 | memset(txt, 0, sizeof(txt)); |
Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 148 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 150 | idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, "%04x: ", |
| 151 | (unsigned int) i); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 152 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 155 | 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 160 | if (len > 0) { |
| 161 | for (/* i = i */; i % 16 != 0; i++) { |
| 162 | idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " "); |
| 163 | } |
Manuel Pégourié-Gonnard | 8c9223d | 2014-11-19 10:17:21 +0100 | [diff] [blame] | 164 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 165 | mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt); |
| 166 | debug_send_line(ssl, level, file, line, str); |
Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 167 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Valerio Setti | ffac311 | 2025-05-27 09:58:02 +0200 | [diff] [blame] | 170 | #if defined(MBEDTLS_BIGNUM_C) |
| 171 | void 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 Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 222 | |
Valerio Setti | 3388c4a | 2025-06-06 15:56:59 +0200 | [diff] [blame^] | 223 | #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) || defined(MBEDTLS_PK_USE_PSA_RSA_DATA) //no-check-names |
Valerio Setti | c1319f4 | 2023-07-27 16:20:07 +0200 | [diff] [blame] | 224 | static void mbedtls_debug_print_ec_coord(const mbedtls_ssl_context *ssl, int level, |
| 225 | const char *file, int line, const char *text, |
| 226 | const unsigned char *buf, size_t len) |
| 227 | { |
| 228 | char str[DEBUG_BUF_SIZE]; |
Valerio Setti | 3388c4a | 2025-06-06 15:56:59 +0200 | [diff] [blame^] | 229 | size_t i, len_bytes = PSA_BITS_TO_BYTES(len), idx = 0; |
Valerio Setti | c1319f4 | 2023-07-27 16:20:07 +0200 | [diff] [blame] | 230 | |
| 231 | mbedtls_snprintf(str + idx, sizeof(str) - idx, "value of '%s' (%u bits) is:\n", |
Valerio Setti | 3388c4a | 2025-06-06 15:56:59 +0200 | [diff] [blame^] | 232 | text, (unsigned int) len); |
Valerio Setti | c1319f4 | 2023-07-27 16:20:07 +0200 | [diff] [blame] | 233 | |
| 234 | debug_send_line(ssl, level, file, line, str); |
| 235 | |
Valerio Setti | 3388c4a | 2025-06-06 15:56:59 +0200 | [diff] [blame^] | 236 | for (i = 0; i < len_bytes; i++) { |
Valerio Setti | c1319f4 | 2023-07-27 16:20:07 +0200 | [diff] [blame] | 237 | if (i >= 4096) { |
| 238 | break; |
| 239 | } |
| 240 | |
| 241 | if (i % 16 == 0) { |
| 242 | if (i > 0) { |
| 243 | mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n"); |
| 244 | debug_send_line(ssl, level, file, line, str); |
| 245 | |
| 246 | idx = 0; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x", |
| 251 | (unsigned int) buf[i]); |
| 252 | } |
| 253 | |
Valerio Setti | 3388c4a | 2025-06-06 15:56:59 +0200 | [diff] [blame^] | 254 | if (len_bytes > 0) { |
Valerio Setti | c1319f4 | 2023-07-27 16:20:07 +0200 | [diff] [blame] | 255 | mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n"); |
| 256 | debug_send_line(ssl, level, file, line, str); |
| 257 | } |
| 258 | } |
Valerio Setti | 3388c4a | 2025-06-06 15:56:59 +0200 | [diff] [blame^] | 259 | #endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY || MBEDTLS_PK_USE_PSA_RSA_DATA */ //no-check-names |
Valerio Setti | c1319f4 | 2023-07-27 16:20:07 +0200 | [diff] [blame] | 260 | |
Valerio Setti | 3388c4a | 2025-06-06 15:56:59 +0200 | [diff] [blame^] | 261 | #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) |
Valerio Setti | 28ef01a | 2025-05-23 15:03:26 +0200 | [diff] [blame] | 262 | static void mbedtls_debug_print_psa_ec(const mbedtls_ssl_context *ssl, int level, |
| 263 | const char *file, int line, |
| 264 | const char *text, const mbedtls_pk_context *pk) |
Valerio Setti | 7ca7b90 | 2023-05-17 15:35:46 +0200 | [diff] [blame] | 265 | { |
| 266 | char str[DEBUG_BUF_SIZE]; |
Valerio Setti | c1319f4 | 2023-07-27 16:20:07 +0200 | [diff] [blame] | 267 | const uint8_t *coord_start; |
| 268 | size_t coord_len; |
Valerio Setti | 7ca7b90 | 2023-05-17 15:35:46 +0200 | [diff] [blame] | 269 | |
| 270 | if (NULL == ssl || |
| 271 | NULL == ssl->conf || |
| 272 | NULL == ssl->conf->f_dbg || |
| 273 | level > debug_threshold) { |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | /* For the description of pk->pk_raw content please refer to the description |
| 278 | * psa_export_public_key() function. */ |
Valerio Setti | c1319f4 | 2023-07-27 16:20:07 +0200 | [diff] [blame] | 279 | coord_len = (pk->pub_raw_len - 1)/2; |
Valerio Setti | 7ca7b90 | 2023-05-17 15:35:46 +0200 | [diff] [blame] | 280 | |
| 281 | /* X coordinate */ |
Valerio Setti | c1319f4 | 2023-07-27 16:20:07 +0200 | [diff] [blame] | 282 | coord_start = pk->pub_raw + 1; |
Valerio Setti | 7ca7b90 | 2023-05-17 15:35:46 +0200 | [diff] [blame] | 283 | mbedtls_snprintf(str, sizeof(str), "%s(X)", text); |
Valerio Setti | 3388c4a | 2025-06-06 15:56:59 +0200 | [diff] [blame^] | 284 | mbedtls_debug_print_ec_coord(ssl, level, file, line, str, coord_start, coord_len * 8); |
Valerio Setti | 7ca7b90 | 2023-05-17 15:35:46 +0200 | [diff] [blame] | 285 | |
| 286 | /* Y coordinate */ |
Valerio Setti | c1319f4 | 2023-07-27 16:20:07 +0200 | [diff] [blame] | 287 | coord_start = coord_start + coord_len; |
Valerio Setti | 7ca7b90 | 2023-05-17 15:35:46 +0200 | [diff] [blame] | 288 | mbedtls_snprintf(str, sizeof(str), "%s(Y)", text); |
Valerio Setti | 3388c4a | 2025-06-06 15:56:59 +0200 | [diff] [blame^] | 289 | mbedtls_debug_print_ec_coord(ssl, level, file, line, str, coord_start, coord_len * 8); |
Valerio Setti | 7ca7b90 | 2023-05-17 15:35:46 +0200 | [diff] [blame] | 290 | } |
Valerio Setti | c394fd0 | 2025-05-05 15:42:56 +0200 | [diff] [blame] | 291 | #endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
Valerio Setti | 7ca7b90 | 2023-05-17 15:35:46 +0200 | [diff] [blame] | 292 | |
Valerio Setti | 3388c4a | 2025-06-06 15:56:59 +0200 | [diff] [blame^] | 293 | #if defined(MBEDTLS_PK_USE_PSA_RSA_DATA) //no-check-names |
| 294 | static size_t debug_count_valid_bits(unsigned char **buf, size_t len) |
| 295 | { |
| 296 | size_t i, bits; |
| 297 | |
| 298 | /* Ignore initial null bytes (if any). */ |
| 299 | while ((len > 0) && (**buf == 0x00)) { |
| 300 | (*buf)++; |
| 301 | len--; |
| 302 | } |
| 303 | |
| 304 | if (len == 0) { |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | bits = len * 8; |
| 309 | |
| 310 | /* Ignore initial null bits (if any). */ |
| 311 | for (i = 7; i > 0; i--) { |
| 312 | if ((**buf & (0x1 << i)) != 0) { |
| 313 | break; |
| 314 | } |
| 315 | bits--; |
| 316 | } |
| 317 | |
| 318 | return bits; |
| 319 | } |
| 320 | |
| 321 | static void mbedtls_debug_print_psa_rsa(const mbedtls_ssl_context *ssl, int level, |
| 322 | const char *file, int line, |
| 323 | const char *text, const mbedtls_pk_context *pk) |
| 324 | { |
| 325 | char str[DEBUG_BUF_SIZE]; |
| 326 | unsigned char key_der[MBEDTLS_PK_MAX_RSA_PUBKEY_RAW_LEN]; //no-check-names |
| 327 | unsigned char *start_cur; |
| 328 | unsigned char *end_cur; |
| 329 | size_t len, bits; |
| 330 | int ret; |
| 331 | |
| 332 | if (pk->pub_raw_len > sizeof(key_der)) { |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | memcpy(key_der, pk->pub_raw, pk->pub_raw_len); |
| 337 | start_cur = key_der; |
| 338 | end_cur = key_der + pk->pub_raw_len; |
| 339 | |
| 340 | ret = mbedtls_asn1_get_tag(&start_cur, end_cur, &len, |
| 341 | MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED); |
| 342 | if (ret != 0) { |
| 343 | return; |
| 344 | } |
| 345 | |
| 346 | ret = mbedtls_asn1_get_tag(&start_cur, end_cur, &len, MBEDTLS_ASN1_INTEGER); |
| 347 | if (ret != 0) { |
| 348 | return; |
| 349 | } |
| 350 | |
| 351 | bits = debug_count_valid_bits(&start_cur, len); |
| 352 | if (bits == 0) { |
| 353 | return; |
| 354 | } |
| 355 | len = PSA_BITS_TO_BYTES(bits); |
| 356 | |
| 357 | mbedtls_snprintf(str, sizeof(str), "%s.N", text); |
| 358 | mbedtls_debug_print_ec_coord(ssl, level, file, line, str, start_cur, bits); |
| 359 | |
| 360 | start_cur += len; |
| 361 | |
| 362 | ret = mbedtls_asn1_get_tag(&start_cur, end_cur, &len, MBEDTLS_ASN1_INTEGER); |
| 363 | if (ret != 0) { |
| 364 | return; |
| 365 | } |
| 366 | |
| 367 | bits = debug_count_valid_bits(&start_cur, len); |
| 368 | if (bits == 0) { |
| 369 | return; |
| 370 | } |
| 371 | |
| 372 | mbedtls_snprintf(str, sizeof(str), "%s.E", text); |
| 373 | mbedtls_debug_print_ec_coord(ssl, level, file, line, str, start_cur, bits); |
| 374 | } |
| 375 | #endif /* MBEDTLS_PK_USE_PSA_RSA_DATA */ //no-check-names |
| 376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 377 | static void debug_print_pk(const mbedtls_ssl_context *ssl, int level, |
| 378 | const char *file, int line, |
| 379 | const char *text, const mbedtls_pk_context *pk) |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 380 | { |
| 381 | size_t i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 382 | mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS]; |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 383 | char name[16]; |
| 384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 385 | memset(items, 0, sizeof(items)); |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 386 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 387 | if (mbedtls_pk_debug(pk, items) != 0) { |
| 388 | debug_send_line(ssl, level, file, line, |
| 389 | "invalid PK context\n"); |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 390 | return; |
| 391 | } |
| 392 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 393 | for (i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++) { |
| 394 | if (items[i].type == MBEDTLS_PK_DEBUG_NONE) { |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 395 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 396 | } |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 397 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 398 | mbedtls_snprintf(name, sizeof(name), "%s%s", text, items[i].name); |
| 399 | name[sizeof(name) - 1] = '\0'; |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 400 | |
Valerio Setti | 797e396 | 2023-07-27 16:19:00 +0200 | [diff] [blame] | 401 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 402 | if (items[i].type == MBEDTLS_PK_DEBUG_MPI) { |
| 403 | mbedtls_debug_print_mpi(ssl, level, file, line, name, items[i].value); |
| 404 | } else |
Valerio Setti | 797e396 | 2023-07-27 16:19:00 +0200 | [diff] [blame] | 405 | #endif /* MBEDTLS_RSA_C */ |
Valerio Setti | 3388c4a | 2025-06-06 15:56:59 +0200 | [diff] [blame^] | 406 | #if defined(MBEDTLS_PK_USE_PSA_RSA_DATA) //no-check-names |
| 407 | if (items[i].type == MBEDTLS_PK_DEBUG_PSA_RSA) { //no-check-names |
| 408 | mbedtls_debug_print_psa_rsa(ssl, level, file, line, name, items[i].value); |
| 409 | } else |
| 410 | #endif /* MBEDTLS_PK_USE_PSA_RSA_DATA */ //no-check-names |
Valerio Setti | c394fd0 | 2025-05-05 15:42:56 +0200 | [diff] [blame] | 411 | #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY) |
Valerio Setti | 7ca7b90 | 2023-05-17 15:35:46 +0200 | [diff] [blame] | 412 | if (items[i].type == MBEDTLS_PK_DEBUG_PSA_EC) { |
| 413 | mbedtls_debug_print_psa_ec(ssl, level, file, line, name, items[i].value); |
| 414 | } else |
Valerio Setti | c394fd0 | 2025-05-05 15:42:56 +0200 | [diff] [blame] | 415 | #endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 416 | { debug_send_line(ssl, level, file, line, |
| 417 | "should not happen\n"); } |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 421 | static void debug_print_line_by_line(const mbedtls_ssl_context *ssl, int level, |
| 422 | const char *file, int line, const char *text) |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 423 | { |
| 424 | char str[DEBUG_BUF_SIZE]; |
| 425 | const char *start, *cur; |
| 426 | |
| 427 | start = text; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 428 | for (cur = text; *cur != '\0'; cur++) { |
| 429 | if (*cur == '\n') { |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 430 | size_t len = (size_t) (cur - start) + 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 431 | if (len > DEBUG_BUF_SIZE - 1) { |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 432 | len = DEBUG_BUF_SIZE - 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 433 | } |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 434 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 435 | memcpy(str, start, len); |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 436 | str[len] = '\0'; |
| 437 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 438 | debug_send_line(ssl, level, file, line, str); |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 439 | |
| 440 | start = cur + 1; |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 445 | void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level, |
| 446 | const char *file, int line, |
| 447 | const char *text, const mbedtls_x509_crt *crt) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 448 | { |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 449 | char str[DEBUG_BUF_SIZE]; |
| 450 | int i = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 451 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 452 | if (NULL == ssl || |
Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 453 | NULL == ssl->conf || |
| 454 | NULL == ssl->conf->f_dbg || |
| 455 | NULL == crt || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 456 | level > debug_threshold) { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 457 | return; |
Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 458 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 459 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 460 | while (crt != NULL) { |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 461 | char buf[1024]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 462 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 463 | mbedtls_snprintf(str, sizeof(str), "%s #%d:\n", text, ++i); |
| 464 | debug_send_line(ssl, level, file, line, str); |
Paul Bakker | eaebbd5 | 2014-04-25 15:04:14 +0200 | [diff] [blame] | 465 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 466 | mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt); |
| 467 | debug_print_line_by_line(ssl, level, file, line, buf); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 468 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 469 | debug_print_pk(ssl, level, file, line, "crt->", &crt->pk); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 470 | |
| 471 | crt = crt->next; |
| 472 | } |
| 473 | } |
Hanno Becker | 612a2f1 | 2020-10-09 09:19:39 +0100 | [diff] [blame] | 474 | #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_X509_REMOVE_INFO */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 475 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 476 | #endif /* MBEDTLS_DEBUG_C */ |