| 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 |  | 
| Manuel Pégourié-Gonnard | d23f593 | 2015-06-23 12:04:52 +0200 | [diff] [blame] | 33 | #define DEBUG_BUF_SIZE      512 | 
|  | 34 |  | 
| Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 35 | static int debug_threshold = 0; | 
| Paul Bakker | eaebbd5 | 2014-04-25 15:04:14 +0200 | [diff] [blame] | 36 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 37 | void mbedtls_debug_set_threshold(int threshold) | 
| Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 38 | { | 
|  | 39 | debug_threshold = threshold; | 
|  | 40 | } | 
|  | 41 |  | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 42 | /* | 
|  | 43 | * All calls to f_dbg must be made via this function | 
|  | 44 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 45 | static inline void debug_send_line(const mbedtls_ssl_context *ssl, int level, | 
|  | 46 | const char *file, int line, | 
|  | 47 | const char *str) | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 48 | { | 
|  | 49 | /* | 
|  | 50 | * If in a threaded environment, we need a thread identifier. | 
|  | 51 | * Since there is no portable way to get one, use the address of the ssl | 
|  | 52 | * context instead, as it shouldn't be shared between threads. | 
|  | 53 | */ | 
|  | 54 | #if defined(MBEDTLS_THREADING_C) | 
|  | 55 | char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 56 | mbedtls_snprintf(idstr, sizeof(idstr), "%p: %s", (void *) ssl, str); | 
|  | 57 | 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] | 58 | #else | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 59 | 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] | 60 | #endif | 
|  | 61 | } | 
|  | 62 |  | 
| Paul Elliott | 4e58970 | 2020-12-09 14:38:01 +0000 | [diff] [blame] | 63 | MBEDTLS_PRINTF_ATTRIBUTE(5, 6) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 64 | void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level, | 
|  | 65 | const char *file, int line, | 
|  | 66 | const char *format, ...) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 67 | { | 
|  | 68 | va_list argp; | 
| Manuel Pégourié-Gonnard | b74c245 | 2015-06-29 20:08:23 +0200 | [diff] [blame] | 69 | char str[DEBUG_BUF_SIZE]; | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 70 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | d23f593 | 2015-06-23 12:04:52 +0200 | [diff] [blame] | 71 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 72 | if (NULL == ssl              || | 
| Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 73 | NULL == ssl->conf        || | 
|  | 74 | NULL == ssl->conf->f_dbg || | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 75 | level > debug_threshold) { | 
| Manuel Pégourié-Gonnard | b74c245 | 2015-06-29 20:08:23 +0200 | [diff] [blame] | 76 | return; | 
| Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 77 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 78 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 79 | va_start(argp, format); | 
|  | 80 | ret = mbedtls_vsnprintf(str, DEBUG_BUF_SIZE, format, argp); | 
|  | 81 | va_end(argp); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 82 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 83 | if (ret >= 0 && ret < DEBUG_BUF_SIZE - 1) { | 
| Manuel Pégourié-Gonnard | b74c245 | 2015-06-29 20:08:23 +0200 | [diff] [blame] | 84 | str[ret]     = '\n'; | 
|  | 85 | str[ret + 1] = '\0'; | 
|  | 86 | } | 
|  | 87 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 88 | debug_send_line(ssl, level, file, line, str); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 89 | } | 
|  | 90 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 91 | void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level, | 
|  | 92 | const char *file, int line, | 
|  | 93 | const char *text, int ret) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 94 | { | 
| Manuel Pégourié-Gonnard | d23f593 | 2015-06-23 12:04:52 +0200 | [diff] [blame] | 95 | char str[DEBUG_BUF_SIZE]; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 96 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 97 | if (NULL == ssl              || | 
| Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 98 | NULL == ssl->conf        || | 
|  | 99 | NULL == ssl->conf->f_dbg || | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 100 | level > debug_threshold) { | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 101 | return; | 
| Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 102 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 103 |  | 
| Manuel Pégourié-Gonnard | 4cba1a7 | 2015-05-11 18:52:25 +0200 | [diff] [blame] | 104 | /* | 
|  | 105 | * With non-blocking I/O and examples that just retry immediately, | 
|  | 106 | * the logs would be quickly flooded with WANT_READ, so ignore that. | 
| Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 107 | * 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] | 108 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 109 | if (ret == MBEDTLS_ERR_SSL_WANT_READ) { | 
| Manuel Pégourié-Gonnard | 4cba1a7 | 2015-05-11 18:52:25 +0200 | [diff] [blame] | 110 | return; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 111 | } | 
| Manuel Pégourié-Gonnard | 4cba1a7 | 2015-05-11 18:52:25 +0200 | [diff] [blame] | 112 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 113 | mbedtls_snprintf(str, sizeof(str), "%s() returned %d (-0x%04x)\n", | 
|  | 114 | text, ret, (unsigned int) -ret); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 115 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 116 | debug_send_line(ssl, level, file, line, str); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 117 | } | 
|  | 118 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 119 | void mbedtls_debug_print_buf(const mbedtls_ssl_context *ssl, int level, | 
|  | 120 | const char *file, int line, const char *text, | 
|  | 121 | const unsigned char *buf, size_t len) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 122 | { | 
| Manuel Pégourié-Gonnard | d23f593 | 2015-06-23 12:04:52 +0200 | [diff] [blame] | 123 | char str[DEBUG_BUF_SIZE]; | 
| Manuel Pégourié-Gonnard | 8c9223d | 2014-11-19 10:17:21 +0100 | [diff] [blame] | 124 | char txt[17]; | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 125 | size_t i, idx = 0; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 126 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 127 | if (NULL == ssl              || | 
| Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 128 | NULL == ssl->conf        || | 
|  | 129 | NULL == ssl->conf->f_dbg || | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 130 | level > debug_threshold) { | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 131 | return; | 
| Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 132 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 133 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 134 | mbedtls_snprintf(str + idx, sizeof(str) - idx, "dumping '%s' (%u bytes)\n", | 
|  | 135 | text, (unsigned int) len); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 136 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 137 | debug_send_line(ssl, level, file, line, str); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 138 |  | 
| Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 139 | idx = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 140 | memset(txt, 0, sizeof(txt)); | 
|  | 141 | for (i = 0; i < len; i++) { | 
|  | 142 | if (i >= 4096) { | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 143 | break; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 144 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 145 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 146 | if (i % 16 == 0) { | 
|  | 147 | if (i > 0) { | 
|  | 148 | mbedtls_snprintf(str + idx, sizeof(str) - idx, "  %s\n", txt); | 
|  | 149 | debug_send_line(ssl, level, file, line, str); | 
| Manuel Pégourié-Gonnard | 8c9223d | 2014-11-19 10:17:21 +0100 | [diff] [blame] | 150 |  | 
| Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 151 | idx = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 152 | memset(txt, 0, sizeof(txt)); | 
| Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 153 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 154 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 155 | idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, "%04x: ", | 
|  | 156 | (unsigned int) i); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 157 |  | 
| 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 | idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x", | 
|  | 161 | (unsigned int) buf[i]); | 
|  | 162 | txt[i % 16] = (buf[i] > 31 && buf[i] < 127) ? buf[i] : '.'; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 163 | } | 
|  | 164 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 165 | if (len > 0) { | 
|  | 166 | for (/* i = i */; i % 16 != 0; i++) { | 
|  | 167 | idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, "   "); | 
|  | 168 | } | 
| Manuel Pégourié-Gonnard | 8c9223d | 2014-11-19 10:17:21 +0100 | [diff] [blame] | 169 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 170 | mbedtls_snprintf(str + idx, sizeof(str) - idx, "  %s\n", txt); | 
|  | 171 | debug_send_line(ssl, level, file, line, str); | 
| Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 172 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 173 | } | 
|  | 174 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 175 | #if defined(MBEDTLS_ECP_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 176 | void mbedtls_debug_print_ecp(const mbedtls_ssl_context *ssl, int level, | 
|  | 177 | const char *file, int line, | 
|  | 178 | const char *text, const mbedtls_ecp_point *X) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 179 | { | 
| Manuel Pégourié-Gonnard | d23f593 | 2015-06-23 12:04:52 +0200 | [diff] [blame] | 180 | char str[DEBUG_BUF_SIZE]; | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 181 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 182 | if (NULL == ssl              || | 
| Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 183 | NULL == ssl->conf        || | 
|  | 184 | NULL == ssl->conf->f_dbg || | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 185 | level > debug_threshold) { | 
| Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 186 | return; | 
| Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 187 | } | 
| Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 188 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 189 | mbedtls_snprintf(str, sizeof(str), "%s(X)", text); | 
|  | 190 | mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->X); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 191 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 192 | mbedtls_snprintf(str, sizeof(str), "%s(Y)", text); | 
|  | 193 | mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->Y); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 194 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 195 | #endif /* MBEDTLS_ECP_C */ | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 196 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 197 | #if defined(MBEDTLS_BIGNUM_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 198 | void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl, int level, | 
|  | 199 | const char *file, int line, | 
|  | 200 | const char *text, const mbedtls_mpi *X) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 201 | { | 
| Manuel Pégourié-Gonnard | d23f593 | 2015-06-23 12:04:52 +0200 | [diff] [blame] | 202 | char str[DEBUG_BUF_SIZE]; | 
| Gilles Peskine | b26696b | 2021-06-02 20:17:46 +0200 | [diff] [blame] | 203 | size_t bitlen; | 
|  | 204 | size_t idx = 0; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 205 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 206 | if (NULL == ssl              || | 
| Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 207 | NULL == ssl->conf        || | 
|  | 208 | NULL == ssl->conf->f_dbg || | 
|  | 209 | NULL == X                || | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 210 | level > debug_threshold) { | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 211 | return; | 
| Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 212 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 213 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 214 | bitlen = mbedtls_mpi_bitlen(X); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 215 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 216 | mbedtls_snprintf(str, sizeof(str), "value of '%s' (%u bits) is:\n", | 
|  | 217 | text, (unsigned) bitlen); | 
|  | 218 | debug_send_line(ssl, level, file, line, str); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 219 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 220 | if (bitlen == 0) { | 
| Gilles Peskine | b26696b | 2021-06-02 20:17:46 +0200 | [diff] [blame] | 221 | str[0] = ' '; str[1] = '0'; str[2] = '0'; | 
|  | 222 | idx = 3; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 223 | } else { | 
| Gilles Peskine | b26696b | 2021-06-02 20:17:46 +0200 | [diff] [blame] | 224 | int n; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 225 | for (n = (int) ((bitlen - 1) / 8); n >= 0; n--) { | 
|  | 226 | size_t limb_offset = n / sizeof(mbedtls_mpi_uint); | 
|  | 227 | size_t offset_in_limb = n % sizeof(mbedtls_mpi_uint); | 
| Gilles Peskine | b26696b | 2021-06-02 20:17:46 +0200 | [diff] [blame] | 228 | unsigned char octet = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 229 | (X->p[limb_offset] >> (offset_in_limb * 8)) & 0xff; | 
|  | 230 | mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x", octet); | 
| Gilles Peskine | b26696b | 2021-06-02 20:17:46 +0200 | [diff] [blame] | 231 | idx += 3; | 
|  | 232 | /* Wrap lines after 16 octets that each take 3 columns */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 233 | if (idx >= 3 * 16) { | 
|  | 234 | mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n"); | 
|  | 235 | debug_send_line(ssl, level, file, line, str); | 
| Gilles Peskine | b26696b | 2021-06-02 20:17:46 +0200 | [diff] [blame] | 236 | idx = 0; | 
| Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 237 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 238 | } | 
| Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 239 | } | 
|  | 240 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 241 | if (idx != 0) { | 
|  | 242 | mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n"); | 
|  | 243 | debug_send_line(ssl, level, file, line, str); | 
| Gilles Peskine | b26696b | 2021-06-02 20:17:46 +0200 | [diff] [blame] | 244 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 245 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 246 | #endif /* MBEDTLS_BIGNUM_C */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 247 |  | 
| Hanno Becker | 612a2f1 | 2020-10-09 09:19:39 +0100 | [diff] [blame] | 248 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_INFO) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 249 | static void debug_print_pk(const mbedtls_ssl_context *ssl, int level, | 
|  | 250 | const char *file, int line, | 
|  | 251 | const char *text, const mbedtls_pk_context *pk) | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 252 | { | 
|  | 253 | size_t i; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 254 | mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS]; | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 255 | char name[16]; | 
|  | 256 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 257 | memset(items, 0, sizeof(items)); | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 258 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 259 | if (mbedtls_pk_debug(pk, items) != 0) { | 
|  | 260 | debug_send_line(ssl, level, file, line, | 
|  | 261 | "invalid PK context\n"); | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 262 | return; | 
|  | 263 | } | 
|  | 264 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 265 | for (i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++) { | 
|  | 266 | if (items[i].type == MBEDTLS_PK_DEBUG_NONE) { | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 267 | return; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 268 | } | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 269 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 270 | mbedtls_snprintf(name, sizeof(name), "%s%s", text, items[i].name); | 
|  | 271 | name[sizeof(name) - 1] = '\0'; | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 272 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 273 | if (items[i].type == MBEDTLS_PK_DEBUG_MPI) { | 
|  | 274 | mbedtls_debug_print_mpi(ssl, level, file, line, name, items[i].value); | 
|  | 275 | } else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 276 | #if defined(MBEDTLS_ECP_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 277 | if (items[i].type == MBEDTLS_PK_DEBUG_ECP) { | 
|  | 278 | mbedtls_debug_print_ecp(ssl, level, file, line, name, items[i].value); | 
|  | 279 | } else | 
| Manuel Pégourié-Gonnard | bac0e3b | 2013-10-15 11:54:47 +0200 | [diff] [blame] | 280 | #endif | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 281 | { debug_send_line(ssl, level, file, line, | 
|  | 282 | "should not happen\n"); } | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 283 | } | 
|  | 284 | } | 
|  | 285 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 286 | static void debug_print_line_by_line(const mbedtls_ssl_context *ssl, int level, | 
|  | 287 | const char *file, int line, const char *text) | 
| Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 288 | { | 
|  | 289 | char str[DEBUG_BUF_SIZE]; | 
|  | 290 | const char *start, *cur; | 
|  | 291 |  | 
|  | 292 | start = text; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 293 | for (cur = text; *cur != '\0'; cur++) { | 
|  | 294 | if (*cur == '\n') { | 
| Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 295 | size_t len = cur - start + 1; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 296 | if (len > DEBUG_BUF_SIZE - 1) { | 
| Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 297 | len = DEBUG_BUF_SIZE - 1; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 298 | } | 
| Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 299 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 300 | memcpy(str, start, len); | 
| Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 301 | str[len] = '\0'; | 
|  | 302 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 303 | debug_send_line(ssl, level, file, line, str); | 
| Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 304 |  | 
|  | 305 | start = cur + 1; | 
|  | 306 | } | 
|  | 307 | } | 
|  | 308 | } | 
|  | 309 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 310 | void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level, | 
|  | 311 | const char *file, int line, | 
|  | 312 | const char *text, const mbedtls_x509_crt *crt) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 313 | { | 
| Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 314 | char str[DEBUG_BUF_SIZE]; | 
|  | 315 | int i = 0; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 316 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 317 | if (NULL == ssl              || | 
| Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 318 | NULL == ssl->conf        || | 
|  | 319 | NULL == ssl->conf->f_dbg || | 
|  | 320 | NULL == crt              || | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 321 | level > debug_threshold) { | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 322 | return; | 
| Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 323 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 324 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 325 | while (crt != NULL) { | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 326 | char buf[1024]; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 327 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 328 | mbedtls_snprintf(str, sizeof(str), "%s #%d:\n", text, ++i); | 
|  | 329 | debug_send_line(ssl, level, file, line, str); | 
| Paul Bakker | eaebbd5 | 2014-04-25 15:04:14 +0200 | [diff] [blame] | 330 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 331 | mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt); | 
|  | 332 | debug_print_line_by_line(ssl, level, file, line, buf); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 333 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 334 | debug_print_pk(ssl, level, file, line, "crt->", &crt->pk); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 335 |  | 
|  | 336 | crt = crt->next; | 
|  | 337 | } | 
|  | 338 | } | 
| Hanno Becker | 612a2f1 | 2020-10-09 09:19:39 +0100 | [diff] [blame] | 339 | #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_X509_REMOVE_INFO */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 340 |  | 
| Janos Follath | 948f4be | 2018-08-22 01:37:55 +0100 | [diff] [blame] | 341 | #if defined(MBEDTLS_ECDH_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 342 | static void mbedtls_debug_printf_ecdh_internal(const mbedtls_ssl_context *ssl, | 
|  | 343 | int level, const char *file, | 
|  | 344 | int line, | 
|  | 345 | const mbedtls_ecdh_context *ecdh, | 
|  | 346 | mbedtls_debug_ecdh_attr attr) | 
| Janos Follath | 948f4be | 2018-08-22 01:37:55 +0100 | [diff] [blame] | 347 | { | 
|  | 348 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 349 | const mbedtls_ecdh_context *ctx = ecdh; | 
| Janos Follath | 948f4be | 2018-08-22 01:37:55 +0100 | [diff] [blame] | 350 | #else | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 351 | const mbedtls_ecdh_context_mbed *ctx = &ecdh->ctx.mbed_ecdh; | 
| Janos Follath | 948f4be | 2018-08-22 01:37:55 +0100 | [diff] [blame] | 352 | #endif | 
|  | 353 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 354 | switch (attr) { | 
| Janos Follath | 948f4be | 2018-08-22 01:37:55 +0100 | [diff] [blame] | 355 | case MBEDTLS_DEBUG_ECDH_Q: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 356 | mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Q", | 
|  | 357 | &ctx->Q); | 
| Janos Follath | 948f4be | 2018-08-22 01:37:55 +0100 | [diff] [blame] | 358 | break; | 
|  | 359 | case MBEDTLS_DEBUG_ECDH_QP: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 360 | mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Qp", | 
|  | 361 | &ctx->Qp); | 
| Janos Follath | 948f4be | 2018-08-22 01:37:55 +0100 | [diff] [blame] | 362 | break; | 
|  | 363 | case MBEDTLS_DEBUG_ECDH_Z: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 364 | mbedtls_debug_print_mpi(ssl, level, file, line, "ECDH: z", | 
|  | 365 | &ctx->z); | 
| Janos Follath | 948f4be | 2018-08-22 01:37:55 +0100 | [diff] [blame] | 366 | break; | 
|  | 367 | default: | 
|  | 368 | break; | 
|  | 369 | } | 
|  | 370 | } | 
|  | 371 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 372 | void mbedtls_debug_printf_ecdh(const mbedtls_ssl_context *ssl, int level, | 
|  | 373 | const char *file, int line, | 
|  | 374 | const mbedtls_ecdh_context *ecdh, | 
|  | 375 | mbedtls_debug_ecdh_attr attr) | 
| Janos Follath | 948f4be | 2018-08-22 01:37:55 +0100 | [diff] [blame] | 376 | { | 
|  | 377 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 378 | mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh, attr); | 
| Janos Follath | 948f4be | 2018-08-22 01:37:55 +0100 | [diff] [blame] | 379 | #else | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 380 | switch (ecdh->var) { | 
| Janos Follath | 948f4be | 2018-08-22 01:37:55 +0100 | [diff] [blame] | 381 | default: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 382 | mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh, | 
|  | 383 | attr); | 
| Janos Follath | 948f4be | 2018-08-22 01:37:55 +0100 | [diff] [blame] | 384 | } | 
|  | 385 | #endif | 
|  | 386 | } | 
|  | 387 | #endif /* MBEDTLS_ECDH_C */ | 
|  | 388 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 389 | #endif /* MBEDTLS_DEBUG_C */ |