blob: 1868cb74240eb32e813f9c4193e8a5f26ccc9961 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Debugging routines
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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 Bakker5121ce52009-01-03 21:22:43 +000018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_DEBUG_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/platform.h"
Rich Evans2387c7d2015-01-30 11:10:20 +000025
SimonBd5800b72016-04-26 07:43:27 +010026#include "mbedtls/debug.h"
Janos Follath73c616b2019-12-18 15:07:04 +000027#include "mbedtls/error.h"
SimonBd5800b72016-04-26 07:43:27 +010028
29#include <stdarg.h>
30#include <stdio.h>
31#include <string.h>
32
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020033#define DEBUG_BUF_SIZE 512
34
Paul Bakkerc73079a2014-04-25 16:34:30 +020035static int debug_threshold = 0;
Paul Bakkereaebbd52014-04-25 15:04:14 +020036
Gilles Peskine449bd832023-01-11 14:50:10 +010037void mbedtls_debug_set_threshold(int threshold)
Paul Bakkerc73079a2014-04-25 16:34:30 +020038{
39 debug_threshold = threshold;
40}
41
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020042/*
43 * All calls to f_dbg must be made via this function
44 */
Gilles Peskine449bd832023-01-11 14:50:10 +010045static 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é-Gonnard7b23c512015-08-31 16:11:00 +020048{
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 Peskine449bd832023-01-11 14:50:10 +010056 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é-Gonnard7b23c512015-08-31 16:11:00 +020058#else
Gilles Peskine449bd832023-01-11 14:50:10 +010059 ssl->conf->f_dbg(ssl->conf->p_dbg, level, file, line, str);
Manuel Pégourié-Gonnard7b23c512015-08-31 16:11:00 +020060#endif
61}
62
Paul Elliott4e589702020-12-09 14:38:01 +000063MBEDTLS_PRINTF_ATTRIBUTE(5, 6)
Gilles Peskine449bd832023-01-11 14:50:10 +010064void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level,
65 const char *file, int line,
66 const char *format, ...)
Paul Bakker5121ce52009-01-03 21:22:43 +000067{
68 va_list argp;
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020069 char str[DEBUG_BUF_SIZE];
Janos Follath865b3eb2019-12-16 11:46:15 +000070 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020071
Gilles Peskine449bd832023-01-11 14:50:10 +010072 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +010073 NULL == ssl->conf ||
74 NULL == ssl->conf->f_dbg ||
Gilles Peskine449bd832023-01-11 14:50:10 +010075 level > debug_threshold) {
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020076 return;
Hanno Becker687c5002018-06-29 09:04:46 +010077 }
Paul Bakker5121ce52009-01-03 21:22:43 +000078
Gilles Peskine449bd832023-01-11 14:50:10 +010079 va_start(argp, format);
80 ret = mbedtls_vsnprintf(str, DEBUG_BUF_SIZE, format, argp);
81 va_end(argp);
Paul Bakker5121ce52009-01-03 21:22:43 +000082
Gilles Peskine449bd832023-01-11 14:50:10 +010083 if (ret >= 0 && ret < DEBUG_BUF_SIZE - 1) {
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020084 str[ret] = '\n';
85 str[ret + 1] = '\0';
86 }
87
Gilles Peskine449bd832023-01-11 14:50:10 +010088 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +000089}
90
Gilles Peskine449bd832023-01-11 14:50:10 +010091void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level,
92 const char *file, int line,
93 const char *text, int ret)
Paul Bakker5121ce52009-01-03 21:22:43 +000094{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020095 char str[DEBUG_BUF_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +000096
Gilles Peskine449bd832023-01-11 14:50:10 +010097 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +010098 NULL == ssl->conf ||
99 NULL == ssl->conf->f_dbg ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000101 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100102 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200104 /*
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 Cosgrove1797b052022-12-04 17:19:59 +0000107 * Don't ignore WANT_WRITE however, since it is usually rare.
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200108 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200110 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 }
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 mbedtls_snprintf(str, sizeof(str), "%s() returned %d (-0x%04x)\n",
114 text, ret, (unsigned int) -ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000117}
118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119void 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 Bakker5121ce52009-01-03 21:22:43 +0000122{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200123 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100124 char txt[17];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200125 size_t i, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100128 NULL == ssl->conf ||
129 NULL == ssl->conf->f_dbg ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100132 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 mbedtls_snprintf(str + idx, sizeof(str) - idx, "dumping '%s' (%u bytes)\n",
135 text, (unsigned int) len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000136
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000138
Paul Bakker92478c32014-04-25 15:18:34 +0200139 idx = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 memset(txt, 0, sizeof(txt));
141 for (i = 0; i < len; i++) {
142 if (i >= 4096) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 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é-Gonnard8c9223d2014-11-19 10:17:21 +0100150
Paul Bakker92478c32014-04-25 15:18:34 +0200151 idx = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 memset(txt, 0, sizeof(txt));
Paul Bakker92478c32014-04-25 15:18:34 +0200153 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, "%04x: ",
156 (unsigned int) i);
Paul Bakker5121ce52009-01-03 21:22:43 +0000157
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 }
159
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 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 Bakker5121ce52009-01-03 21:22:43 +0000163 }
164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 if (len > 0) {
166 for (/* i = i */; i % 16 != 0; i++) {
167 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " ");
168 }
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
171 debug_send_line(ssl, level, file, line, str);
Paul Bakker92478c32014-04-25 15:18:34 +0200172 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000173}
174
Valerio Setti6c496a12023-04-07 15:53:51 +0200175#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100176void 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 Bakker41c83d32013-03-20 14:39:14 +0100179{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200180 char str[DEBUG_BUF_SIZE];
Paul Bakker41c83d32013-03-20 14:39:14 +0100181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100183 NULL == ssl->conf ||
184 NULL == ssl->conf->f_dbg ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 level > debug_threshold) {
Paul Bakkerc73079a2014-04-25 16:34:30 +0200186 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100187 }
Paul Bakkerc73079a2014-04-25 16:34:30 +0200188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 mbedtls_snprintf(str, sizeof(str), "%s(X)", text);
190 mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->X);
Paul Bakker41c83d32013-03-20 14:39:14 +0100191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 mbedtls_snprintf(str, sizeof(str), "%s(Y)", text);
193 mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->Y);
Paul Bakker41c83d32013-03-20 14:39:14 +0100194}
Valerio Setti6c496a12023-04-07 15:53:51 +0200195#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker41c83d32013-03-20 14:39:14 +0100196
Valerio Setti7ca7b902023-05-17 15:35:46 +0200197#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
198void mbedtls_debug_print_psa_ec(const mbedtls_ssl_context *ssl, int level,
199 const char *file, int line,
200 const char *text, const mbedtls_pk_context *pk)
201{
202 char str[DEBUG_BUF_SIZE];
203 mbedtls_mpi mpi;
204 const uint8_t *mpi_start;
205 size_t mpi_len;
206 int ret;
207
208 if (NULL == ssl ||
209 NULL == ssl->conf ||
210 NULL == ssl->conf->f_dbg ||
211 level > debug_threshold) {
212 return;
213 }
214
215 /* For the description of pk->pk_raw content please refer to the description
216 * psa_export_public_key() function. */
217 mpi_len = (pk->pub_raw_len - 1)/2;
218
219 /* X coordinate */
220 mbedtls_mpi_init(&mpi);
221 mpi_start = pk->pub_raw + 1;
222 ret = mbedtls_mpi_read_binary(&mpi, mpi_start, mpi_len);
223 if (ret != 0) {
224 return;
225 }
226 mbedtls_snprintf(str, sizeof(str), "%s(X)", text);
227 mbedtls_debug_print_mpi(ssl, level, file, line, str, &mpi);
228 mbedtls_mpi_free(&mpi);
229
230 /* Y coordinate */
231 mbedtls_mpi_init(&mpi);
232 mpi_start = mpi_start + mpi_len;
233 ret = mbedtls_mpi_read_binary(&mpi, mpi_start, mpi_len);
234 if (ret != 0) {
235 return;
236 }
237 mbedtls_snprintf(str, sizeof(str), "%s(Y)", text);
238 mbedtls_debug_print_mpi(ssl, level, file, line, str, &mpi);
239 mbedtls_mpi_free(&mpi);
240}
241#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100244void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl, int level,
245 const char *file, int line,
246 const char *text, const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000247{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200248 char str[DEBUG_BUF_SIZE];
Gilles Peskineb26696b2021-06-02 20:17:46 +0200249 size_t bitlen;
250 size_t idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100253 NULL == ssl->conf ||
254 NULL == ssl->conf->f_dbg ||
255 NULL == X ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000257 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100258 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 bitlen = mbedtls_mpi_bitlen(X);
Paul Bakker5121ce52009-01-03 21:22:43 +0000261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 mbedtls_snprintf(str, sizeof(str), "value of '%s' (%u bits) is:\n",
263 text, (unsigned) bitlen);
264 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000265
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 if (bitlen == 0) {
Gilles Peskineb26696b2021-06-02 20:17:46 +0200267 str[0] = ' '; str[1] = '0'; str[2] = '0';
268 idx = 3;
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 } else {
Gilles Peskineb26696b2021-06-02 20:17:46 +0200270 int n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 for (n = (int) ((bitlen - 1) / 8); n >= 0; n--) {
272 size_t limb_offset = n / sizeof(mbedtls_mpi_uint);
273 size_t offset_in_limb = n % sizeof(mbedtls_mpi_uint);
Gilles Peskineb26696b2021-06-02 20:17:46 +0200274 unsigned char octet =
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 (X->p[limb_offset] >> (offset_in_limb * 8)) & 0xff;
276 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x", octet);
Gilles Peskineb26696b2021-06-02 20:17:46 +0200277 idx += 3;
278 /* Wrap lines after 16 octets that each take 3 columns */
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 if (idx >= 3 * 16) {
280 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
281 debug_send_line(ssl, level, file, line, str);
Gilles Peskineb26696b2021-06-02 20:17:46 +0200282 idx = 0;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000283 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000284 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000285 }
286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 if (idx != 0) {
288 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
289 debug_send_line(ssl, level, file, line, str);
Gilles Peskineb26696b2021-06-02 20:17:46 +0200290 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000291}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000293
Hanno Becker612a2f12020-10-09 09:19:39 +0100294#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100295static void debug_print_pk(const mbedtls_ssl_context *ssl, int level,
296 const char *file, int line,
297 const char *text, const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200298{
299 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200301 char name[16];
302
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 memset(items, 0, sizeof(items));
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200304
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 if (mbedtls_pk_debug(pk, items) != 0) {
306 debug_send_line(ssl, level, file, line,
307 "invalid PK context\n");
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200308 return;
309 }
310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 for (i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++) {
312 if (items[i].type == MBEDTLS_PK_DEBUG_NONE) {
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200313 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200315
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 mbedtls_snprintf(name, sizeof(name), "%s%s", text, items[i].name);
317 name[sizeof(name) - 1] = '\0';
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200318
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 if (items[i].type == MBEDTLS_PK_DEBUG_MPI) {
320 mbedtls_debug_print_mpi(ssl, level, file, line, name, items[i].value);
321 } else
Valerio Setti6c496a12023-04-07 15:53:51 +0200322#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 if (items[i].type == MBEDTLS_PK_DEBUG_ECP) {
324 mbedtls_debug_print_ecp(ssl, level, file, line, name, items[i].value);
325 } else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200326#endif
Valerio Setti7ca7b902023-05-17 15:35:46 +0200327#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
328 if (items[i].type == MBEDTLS_PK_DEBUG_PSA_EC) {
329 mbedtls_debug_print_psa_ec(ssl, level, file, line, name, items[i].value);
330 } else
331#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 { debug_send_line(ssl, level, file, line,
333 "should not happen\n"); }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200334 }
335}
336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337static void debug_print_line_by_line(const mbedtls_ssl_context *ssl, int level,
338 const char *file, int line, const char *text)
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200339{
340 char str[DEBUG_BUF_SIZE];
341 const char *start, *cur;
342
343 start = text;
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 for (cur = text; *cur != '\0'; cur++) {
345 if (*cur == '\n') {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200346 size_t len = cur - start + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 if (len > DEBUG_BUF_SIZE - 1) {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200348 len = DEBUG_BUF_SIZE - 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 }
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 memcpy(str, start, len);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200352 str[len] = '\0';
353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 debug_send_line(ssl, level, file, line, str);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200355
356 start = cur + 1;
357 }
358 }
359}
360
Gilles Peskine449bd832023-01-11 14:50:10 +0100361void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level,
362 const char *file, int line,
363 const char *text, const mbedtls_x509_crt *crt)
Paul Bakker5121ce52009-01-03 21:22:43 +0000364{
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200365 char str[DEBUG_BUF_SIZE];
366 int i = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100369 NULL == ssl->conf ||
370 NULL == ssl->conf->f_dbg ||
371 NULL == crt ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000373 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100374 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 while (crt != NULL) {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000377 char buf[1024];
Paul Bakker5121ce52009-01-03 21:22:43 +0000378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 mbedtls_snprintf(str, sizeof(str), "%s #%d:\n", text, ++i);
380 debug_send_line(ssl, level, file, line, str);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt);
383 debug_print_line_by_line(ssl, level, file, line, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000384
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 debug_print_pk(ssl, level, file, line, "crt->", &crt->pk);
Paul Bakker5121ce52009-01-03 21:22:43 +0000386
387 crt = crt->next;
388 }
389}
Hanno Becker612a2f12020-10-09 09:19:39 +0100390#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_X509_REMOVE_INFO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000391
Janos Follath948f4be2018-08-22 01:37:55 +0100392#if defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100393static void mbedtls_debug_printf_ecdh_internal(const mbedtls_ssl_context *ssl,
394 int level, const char *file,
395 int line,
396 const mbedtls_ecdh_context *ecdh,
397 mbedtls_debug_ecdh_attr attr)
Janos Follath948f4be2018-08-22 01:37:55 +0100398{
399#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 const mbedtls_ecdh_context *ctx = ecdh;
Janos Follath948f4be2018-08-22 01:37:55 +0100401#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 const mbedtls_ecdh_context_mbed *ctx = &ecdh->ctx.mbed_ecdh;
Janos Follath948f4be2018-08-22 01:37:55 +0100403#endif
404
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 switch (attr) {
Janos Follath948f4be2018-08-22 01:37:55 +0100406 case MBEDTLS_DEBUG_ECDH_Q:
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Q",
408 &ctx->Q);
Janos Follath948f4be2018-08-22 01:37:55 +0100409 break;
410 case MBEDTLS_DEBUG_ECDH_QP:
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Qp",
412 &ctx->Qp);
Janos Follath948f4be2018-08-22 01:37:55 +0100413 break;
414 case MBEDTLS_DEBUG_ECDH_Z:
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 mbedtls_debug_print_mpi(ssl, level, file, line, "ECDH: z",
416 &ctx->z);
Janos Follath948f4be2018-08-22 01:37:55 +0100417 break;
418 default:
419 break;
420 }
421}
422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423void mbedtls_debug_printf_ecdh(const mbedtls_ssl_context *ssl, int level,
424 const char *file, int line,
425 const mbedtls_ecdh_context *ecdh,
426 mbedtls_debug_ecdh_attr attr)
Janos Follath948f4be2018-08-22 01:37:55 +0100427{
428#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh, attr);
Janos Follath948f4be2018-08-22 01:37:55 +0100430#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 switch (ecdh->var) {
Janos Follath948f4be2018-08-22 01:37:55 +0100432 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh,
434 attr);
Janos Follath948f4be2018-08-22 01:37:55 +0100435 }
436#endif
437}
438#endif /* MBEDTLS_ECDH_C */
439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440#endif /* MBEDTLS_DEBUG_C */