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 |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 20 | #include "common.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 21 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if defined(MBEDTLS_DEBUG_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 23 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 24 | #include "mbedtls/platform.h" |
Rich Evans | 2387c7d | 2015-01-30 11:10:20 +0000 | [diff] [blame] | 25 | |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 26 | #include "mbedtls/debug.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 27 | #include "mbedtls/error.h" |
SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 28 | |
| 29 | #include <stdarg.h> |
| 30 | #include <stdio.h> |
| 31 | #include <string.h> |
| 32 | |
Dave Rodgman | ed59ea7 | 2023-02-15 17:41:28 +0000 | [diff] [blame] | 33 | /* DEBUG_BUF_SIZE must be at least 2 */ |
Manuel Pégourié-Gonnard | d23f593 | 2015-06-23 12:04:52 +0200 | [diff] [blame] | 34 | #define DEBUG_BUF_SIZE 512 |
| 35 | |
Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 36 | static int debug_threshold = 0; |
Paul Bakker | eaebbd5 | 2014-04-25 15:04:14 +0200 | [diff] [blame] | 37 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 38 | void mbedtls_debug_set_threshold(int threshold) |
Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 39 | { |
| 40 | debug_threshold = threshold; |
| 41 | } |
| 42 | |
Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 43 | /* |
| 44 | * All calls to f_dbg must be made via this function |
| 45 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 46 | static 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é-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 49 | { |
| 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 Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 57 | 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é-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 59 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 60 | 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] | 61 | #endif |
| 62 | } |
| 63 | |
Paul Elliott | 4e58970 | 2020-12-09 14:38:01 +0000 | [diff] [blame] | 64 | MBEDTLS_PRINTF_ATTRIBUTE(5, 6) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 65 | void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level, |
| 66 | const char *file, int line, |
| 67 | const char *format, ...) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 68 | { |
| 69 | va_list argp; |
Manuel Pégourié-Gonnard | b74c245 | 2015-06-29 20:08:23 +0200 | [diff] [blame] | 70 | char str[DEBUG_BUF_SIZE]; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 71 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
valord577 | 536893c | 2023-02-15 19:31:39 +0800 | [diff] [blame] | 72 | |
Dave Rodgman | 8508e50 | 2023-05-16 16:43:48 +0100 | [diff] [blame] | 73 | 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] | 74 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 75 | if (NULL == ssl || |
Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 76 | NULL == ssl->conf || |
| 77 | NULL == ssl->conf->f_dbg || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 78 | level > debug_threshold) { |
Manuel Pégourié-Gonnard | b74c245 | 2015-06-29 20:08:23 +0200 | [diff] [blame] | 79 | return; |
Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 80 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 81 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 82 | va_start(argp, format); |
| 83 | ret = mbedtls_vsnprintf(str, DEBUG_BUF_SIZE, format, argp); |
| 84 | va_end(argp); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 85 | |
valord577 | 536893c | 2023-02-15 19:31:39 +0800 | [diff] [blame] | 86 | if (ret < 0) { |
valord577 | 5bfcd1c | 2023-02-15 21:46:47 +0800 | [diff] [blame] | 87 | ret = 0; |
valord577 | 536893c | 2023-02-15 19:31:39 +0800 | [diff] [blame] | 88 | } else { |
valord577 | 536893c | 2023-02-15 19:31:39 +0800 | [diff] [blame] | 89 | if (ret >= DEBUG_BUF_SIZE - 1) { |
valord577 | 5bfcd1c | 2023-02-15 21:46:47 +0800 | [diff] [blame] | 90 | ret = DEBUG_BUF_SIZE - 2; |
valord577 | 24da0cd | 2023-02-15 19:01:16 +0800 | [diff] [blame] | 91 | } |
valord577 | 25418ac | 2022-10-31 15:17:37 +0800 | [diff] [blame] | 92 | } |
valord577 | 5bfcd1c | 2023-02-15 21:46:47 +0800 | [diff] [blame] | 93 | str[ret] = '\n'; |
| 94 | str[ret + 1] = '\0'; |
Manuel Pégourié-Gonnard | b74c245 | 2015-06-29 20:08:23 +0200 | [diff] [blame] | 95 | |
valord577 | 5bfcd1c | 2023-02-15 21:46:47 +0800 | [diff] [blame] | 96 | debug_send_line(ssl, level, file, line, str); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 99 | void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level, |
| 100 | const char *file, int line, |
| 101 | const char *text, int ret) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 102 | { |
Manuel Pégourié-Gonnard | d23f593 | 2015-06-23 12:04:52 +0200 | [diff] [blame] | 103 | char str[DEBUG_BUF_SIZE]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 105 | if (NULL == ssl || |
Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 106 | NULL == ssl->conf || |
| 107 | NULL == ssl->conf->f_dbg || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 108 | level > debug_threshold) { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 109 | return; |
Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 110 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 111 | |
Manuel Pégourié-Gonnard | 4cba1a7 | 2015-05-11 18:52:25 +0200 | [diff] [blame] | 112 | /* |
| 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 Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 115 | * 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] | 116 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 117 | if (ret == MBEDTLS_ERR_SSL_WANT_READ) { |
Manuel Pégourié-Gonnard | 4cba1a7 | 2015-05-11 18:52:25 +0200 | [diff] [blame] | 118 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 119 | } |
Manuel Pégourié-Gonnard | 4cba1a7 | 2015-05-11 18:52:25 +0200 | [diff] [blame] | 120 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 121 | mbedtls_snprintf(str, sizeof(str), "%s() returned %d (-0x%04x)\n", |
| 122 | text, ret, (unsigned int) -ret); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 123 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 124 | debug_send_line(ssl, level, file, line, str); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 127 | void 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 130 | { |
Manuel Pégourié-Gonnard | d23f593 | 2015-06-23 12:04:52 +0200 | [diff] [blame] | 131 | char str[DEBUG_BUF_SIZE]; |
Manuel Pégourié-Gonnard | 8c9223d | 2014-11-19 10:17:21 +0100 | [diff] [blame] | 132 | char txt[17]; |
Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 133 | size_t i, idx = 0; |
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 | if (NULL == ssl || |
Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 136 | NULL == ssl->conf || |
| 137 | NULL == ssl->conf->f_dbg || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 138 | level > debug_threshold) { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 139 | return; |
Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 140 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 142 | mbedtls_snprintf(str + idx, sizeof(str) - idx, "dumping '%s' (%u bytes)\n", |
| 143 | text, (unsigned int) len); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 144 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 145 | debug_send_line(ssl, level, file, line, str); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 146 | |
Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 147 | idx = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 148 | memset(txt, 0, sizeof(txt)); |
| 149 | for (i = 0; i < len; i++) { |
| 150 | if (i >= 4096) { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 151 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 152 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 153 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 154 | 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é-Gonnard | 8c9223d | 2014-11-19 10:17:21 +0100 | [diff] [blame] | 158 | |
Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 159 | idx = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 160 | memset(txt, 0, sizeof(txt)); |
Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 161 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 162 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 163 | idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, "%04x: ", |
| 164 | (unsigned int) i); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 165 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 168 | 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 173 | if (len > 0) { |
| 174 | for (/* i = i */; i % 16 != 0; i++) { |
| 175 | idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " "); |
| 176 | } |
Manuel Pégourié-Gonnard | 8c9223d | 2014-11-19 10:17:21 +0100 | [diff] [blame] | 177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 178 | mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt); |
| 179 | debug_send_line(ssl, level, file, line, str); |
Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 180 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Valerio Setti | 6c496a1 | 2023-04-07 15:53:51 +0200 | [diff] [blame] | 183 | #if defined(MBEDTLS_ECP_LIGHT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 184 | void 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 Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 187 | { |
Manuel Pégourié-Gonnard | d23f593 | 2015-06-23 12:04:52 +0200 | [diff] [blame] | 188 | char str[DEBUG_BUF_SIZE]; |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 189 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 190 | if (NULL == ssl || |
Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 191 | NULL == ssl->conf || |
| 192 | NULL == ssl->conf->f_dbg || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 193 | level > debug_threshold) { |
Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 194 | return; |
Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 195 | } |
Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 197 | mbedtls_snprintf(str, sizeof(str), "%s(X)", text); |
| 198 | mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->X); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 199 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 200 | mbedtls_snprintf(str, sizeof(str), "%s(Y)", text); |
| 201 | mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->Y); |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 202 | } |
Valerio Setti | 6c496a1 | 2023-04-07 15:53:51 +0200 | [diff] [blame] | 203 | #endif /* MBEDTLS_ECP_LIGHT */ |
Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 204 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 205 | #if defined(MBEDTLS_BIGNUM_C) |
Valerio Setti | 7ca7b90 | 2023-05-17 15:35:46 +0200 | [diff] [blame] | 206 | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
| 207 | void mbedtls_debug_print_psa_ec(const mbedtls_ssl_context *ssl, int level, |
| 208 | const char *file, int line, |
| 209 | const char *text, const mbedtls_pk_context *pk) |
| 210 | { |
| 211 | char str[DEBUG_BUF_SIZE]; |
| 212 | mbedtls_mpi mpi; |
| 213 | const uint8_t *mpi_start; |
| 214 | size_t mpi_len; |
| 215 | int ret; |
| 216 | |
| 217 | if (NULL == ssl || |
| 218 | NULL == ssl->conf || |
| 219 | NULL == ssl->conf->f_dbg || |
| 220 | level > debug_threshold) { |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | /* For the description of pk->pk_raw content please refer to the description |
| 225 | * psa_export_public_key() function. */ |
| 226 | mpi_len = (pk->pub_raw_len - 1)/2; |
| 227 | |
| 228 | /* X coordinate */ |
| 229 | mbedtls_mpi_init(&mpi); |
| 230 | mpi_start = pk->pub_raw + 1; |
| 231 | ret = mbedtls_mpi_read_binary(&mpi, mpi_start, mpi_len); |
| 232 | if (ret != 0) { |
| 233 | return; |
| 234 | } |
| 235 | mbedtls_snprintf(str, sizeof(str), "%s(X)", text); |
| 236 | mbedtls_debug_print_mpi(ssl, level, file, line, str, &mpi); |
| 237 | mbedtls_mpi_free(&mpi); |
| 238 | |
| 239 | /* Y coordinate */ |
| 240 | mbedtls_mpi_init(&mpi); |
| 241 | mpi_start = mpi_start + mpi_len; |
| 242 | ret = mbedtls_mpi_read_binary(&mpi, mpi_start, mpi_len); |
| 243 | if (ret != 0) { |
| 244 | return; |
| 245 | } |
| 246 | mbedtls_snprintf(str, sizeof(str), "%s(Y)", text); |
| 247 | mbedtls_debug_print_mpi(ssl, level, file, line, str, &mpi); |
| 248 | mbedtls_mpi_free(&mpi); |
| 249 | } |
| 250 | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
| 251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 252 | void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl, int level, |
| 253 | const char *file, int line, |
| 254 | const char *text, const mbedtls_mpi *X) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 255 | { |
Manuel Pégourié-Gonnard | d23f593 | 2015-06-23 12:04:52 +0200 | [diff] [blame] | 256 | char str[DEBUG_BUF_SIZE]; |
Gilles Peskine | b26696b | 2021-06-02 20:17:46 +0200 | [diff] [blame] | 257 | size_t bitlen; |
| 258 | size_t idx = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 260 | if (NULL == ssl || |
Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 261 | NULL == ssl->conf || |
| 262 | NULL == ssl->conf->f_dbg || |
| 263 | NULL == X || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 264 | level > debug_threshold) { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 265 | return; |
Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 266 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 267 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 268 | bitlen = mbedtls_mpi_bitlen(X); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 269 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 270 | mbedtls_snprintf(str, sizeof(str), "value of '%s' (%u bits) is:\n", |
| 271 | text, (unsigned) bitlen); |
| 272 | debug_send_line(ssl, level, file, line, str); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 273 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 274 | if (bitlen == 0) { |
Gilles Peskine | b26696b | 2021-06-02 20:17:46 +0200 | [diff] [blame] | 275 | str[0] = ' '; str[1] = '0'; str[2] = '0'; |
| 276 | idx = 3; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 277 | } else { |
Gilles Peskine | b26696b | 2021-06-02 20:17:46 +0200 | [diff] [blame] | 278 | int n; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 279 | for (n = (int) ((bitlen - 1) / 8); n >= 0; n--) { |
| 280 | size_t limb_offset = n / sizeof(mbedtls_mpi_uint); |
| 281 | size_t offset_in_limb = n % sizeof(mbedtls_mpi_uint); |
Gilles Peskine | b26696b | 2021-06-02 20:17:46 +0200 | [diff] [blame] | 282 | unsigned char octet = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 283 | (X->p[limb_offset] >> (offset_in_limb * 8)) & 0xff; |
| 284 | mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x", octet); |
Gilles Peskine | b26696b | 2021-06-02 20:17:46 +0200 | [diff] [blame] | 285 | idx += 3; |
| 286 | /* Wrap lines after 16 octets that each take 3 columns */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 287 | if (idx >= 3 * 16) { |
| 288 | mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n"); |
| 289 | debug_send_line(ssl, level, file, line, str); |
Gilles Peskine | b26696b | 2021-06-02 20:17:46 +0200 | [diff] [blame] | 290 | idx = 0; |
Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 291 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 292 | } |
Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 295 | if (idx != 0) { |
| 296 | mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n"); |
| 297 | debug_send_line(ssl, level, file, line, str); |
Gilles Peskine | b26696b | 2021-06-02 20:17:46 +0200 | [diff] [blame] | 298 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 299 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 300 | #endif /* MBEDTLS_BIGNUM_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 301 | |
Hanno Becker | 612a2f1 | 2020-10-09 09:19:39 +0100 | [diff] [blame] | 302 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_INFO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 303 | static void debug_print_pk(const mbedtls_ssl_context *ssl, int level, |
| 304 | const char *file, int line, |
| 305 | const char *text, const mbedtls_pk_context *pk) |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 306 | { |
| 307 | size_t i; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 308 | mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS]; |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 309 | char name[16]; |
| 310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 311 | memset(items, 0, sizeof(items)); |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 312 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 313 | if (mbedtls_pk_debug(pk, items) != 0) { |
| 314 | debug_send_line(ssl, level, file, line, |
| 315 | "invalid PK context\n"); |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 316 | return; |
| 317 | } |
| 318 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 319 | for (i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++) { |
| 320 | if (items[i].type == MBEDTLS_PK_DEBUG_NONE) { |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 321 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 322 | } |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 323 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 324 | mbedtls_snprintf(name, sizeof(name), "%s%s", text, items[i].name); |
| 325 | name[sizeof(name) - 1] = '\0'; |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 326 | |
Valerio Setti | 797e396 | 2023-07-27 16:19:00 +0200 | [diff] [blame^] | 327 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 328 | if (items[i].type == MBEDTLS_PK_DEBUG_MPI) { |
| 329 | mbedtls_debug_print_mpi(ssl, level, file, line, name, items[i].value); |
| 330 | } else |
Valerio Setti | 797e396 | 2023-07-27 16:19:00 +0200 | [diff] [blame^] | 331 | #endif /* MBEDTLS_RSA_C */ |
Valerio Setti | 6c496a1 | 2023-04-07 15:53:51 +0200 | [diff] [blame] | 332 | #if defined(MBEDTLS_ECP_LIGHT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 333 | if (items[i].type == MBEDTLS_PK_DEBUG_ECP) { |
| 334 | mbedtls_debug_print_ecp(ssl, level, file, line, name, items[i].value); |
| 335 | } else |
Valerio Setti | 797e396 | 2023-07-27 16:19:00 +0200 | [diff] [blame^] | 336 | #endif /* MBEDTLS_ECP_LIGHT */ |
| 337 | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) && defined(MBEDTLS_BIGNUM_C) |
Valerio Setti | 7ca7b90 | 2023-05-17 15:35:46 +0200 | [diff] [blame] | 338 | if (items[i].type == MBEDTLS_PK_DEBUG_PSA_EC) { |
| 339 | mbedtls_debug_print_psa_ec(ssl, level, file, line, name, items[i].value); |
| 340 | } else |
| 341 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 342 | { debug_send_line(ssl, level, file, line, |
| 343 | "should not happen\n"); } |
Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 347 | static void debug_print_line_by_line(const mbedtls_ssl_context *ssl, int level, |
| 348 | const char *file, int line, const char *text) |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 349 | { |
| 350 | char str[DEBUG_BUF_SIZE]; |
| 351 | const char *start, *cur; |
| 352 | |
| 353 | start = text; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 354 | for (cur = text; *cur != '\0'; cur++) { |
| 355 | if (*cur == '\n') { |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 356 | size_t len = cur - start + 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 357 | if (len > DEBUG_BUF_SIZE - 1) { |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 358 | len = DEBUG_BUF_SIZE - 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 359 | } |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 361 | memcpy(str, start, len); |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 362 | str[len] = '\0'; |
| 363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 364 | debug_send_line(ssl, level, file, line, str); |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 365 | |
| 366 | start = cur + 1; |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 371 | void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level, |
| 372 | const char *file, int line, |
| 373 | const char *text, const mbedtls_x509_crt *crt) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 374 | { |
Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 375 | char str[DEBUG_BUF_SIZE]; |
| 376 | int i = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 378 | if (NULL == ssl || |
Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 379 | NULL == ssl->conf || |
| 380 | NULL == ssl->conf->f_dbg || |
| 381 | NULL == crt || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 382 | level > debug_threshold) { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 383 | return; |
Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 384 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 386 | while (crt != NULL) { |
Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 387 | char buf[1024]; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 389 | mbedtls_snprintf(str, sizeof(str), "%s #%d:\n", text, ++i); |
| 390 | debug_send_line(ssl, level, file, line, str); |
Paul Bakker | eaebbd5 | 2014-04-25 15:04:14 +0200 | [diff] [blame] | 391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 392 | mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt); |
| 393 | debug_print_line_by_line(ssl, level, file, line, buf); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 394 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 395 | debug_print_pk(ssl, level, file, line, "crt->", &crt->pk); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 396 | |
| 397 | crt = crt->next; |
| 398 | } |
| 399 | } |
Hanno Becker | 612a2f1 | 2020-10-09 09:19:39 +0100 | [diff] [blame] | 400 | #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_X509_REMOVE_INFO */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 401 | |
Janos Follath | 948f4be | 2018-08-22 01:37:55 +0100 | [diff] [blame] | 402 | #if defined(MBEDTLS_ECDH_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 403 | static void mbedtls_debug_printf_ecdh_internal(const mbedtls_ssl_context *ssl, |
| 404 | int level, const char *file, |
| 405 | int line, |
| 406 | const mbedtls_ecdh_context *ecdh, |
| 407 | mbedtls_debug_ecdh_attr attr) |
Janos Follath | 948f4be | 2018-08-22 01:37:55 +0100 | [diff] [blame] | 408 | { |
| 409 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 410 | const mbedtls_ecdh_context *ctx = ecdh; |
Janos Follath | 948f4be | 2018-08-22 01:37:55 +0100 | [diff] [blame] | 411 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 412 | const mbedtls_ecdh_context_mbed *ctx = &ecdh->ctx.mbed_ecdh; |
Janos Follath | 948f4be | 2018-08-22 01:37:55 +0100 | [diff] [blame] | 413 | #endif |
| 414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 415 | switch (attr) { |
Janos Follath | 948f4be | 2018-08-22 01:37:55 +0100 | [diff] [blame] | 416 | case MBEDTLS_DEBUG_ECDH_Q: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 417 | mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Q", |
| 418 | &ctx->Q); |
Janos Follath | 948f4be | 2018-08-22 01:37:55 +0100 | [diff] [blame] | 419 | break; |
| 420 | case MBEDTLS_DEBUG_ECDH_QP: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 421 | mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Qp", |
| 422 | &ctx->Qp); |
Janos Follath | 948f4be | 2018-08-22 01:37:55 +0100 | [diff] [blame] | 423 | break; |
| 424 | case MBEDTLS_DEBUG_ECDH_Z: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 425 | mbedtls_debug_print_mpi(ssl, level, file, line, "ECDH: z", |
| 426 | &ctx->z); |
Janos Follath | 948f4be | 2018-08-22 01:37:55 +0100 | [diff] [blame] | 427 | break; |
| 428 | default: |
| 429 | break; |
| 430 | } |
| 431 | } |
| 432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 433 | void mbedtls_debug_printf_ecdh(const mbedtls_ssl_context *ssl, int level, |
| 434 | const char *file, int line, |
| 435 | const mbedtls_ecdh_context *ecdh, |
| 436 | mbedtls_debug_ecdh_attr attr) |
Janos Follath | 948f4be | 2018-08-22 01:37:55 +0100 | [diff] [blame] | 437 | { |
| 438 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 439 | mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh, attr); |
Janos Follath | 948f4be | 2018-08-22 01:37:55 +0100 | [diff] [blame] | 440 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 441 | switch (ecdh->var) { |
Janos Follath | 948f4be | 2018-08-22 01:37:55 +0100 | [diff] [blame] | 442 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 443 | mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh, |
| 444 | attr); |
Janos Follath | 948f4be | 2018-08-22 01:37:55 +0100 | [diff] [blame] | 445 | } |
| 446 | #endif |
| 447 | } |
| 448 | #endif /* MBEDTLS_ECDH_C */ |
| 449 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 450 | #endif /* MBEDTLS_DEBUG_C */ |