blob: 0e98548575636da5a92be237d198cee18f448117 [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;
valord577536893c2023-02-15 19:31:39 +080071 int eol = -1;
72
73#if defined(static_assert)
74 static_assert(DEBUG_BUF_SIZE >= 2)
75#endif
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020076
Gilles Peskine449bd832023-01-11 14:50:10 +010077 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +010078 NULL == ssl->conf ||
79 NULL == ssl->conf->f_dbg ||
Gilles Peskine449bd832023-01-11 14:50:10 +010080 level > debug_threshold) {
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020081 return;
Hanno Becker687c5002018-06-29 09:04:46 +010082 }
Paul Bakker5121ce52009-01-03 21:22:43 +000083
Gilles Peskine449bd832023-01-11 14:50:10 +010084 va_start(argp, format);
85 ret = mbedtls_vsnprintf(str, DEBUG_BUF_SIZE, format, argp);
86 va_end(argp);
Paul Bakker5121ce52009-01-03 21:22:43 +000087
valord577536893c2023-02-15 19:31:39 +080088 if (ret < 0) {
valord577176e9272023-02-15 19:45:12 +080089 eol = 0;
valord577536893c2023-02-15 19:31:39 +080090 } else {
valord577176e9272023-02-15 19:45:12 +080091 eol = ret;
valord577536893c2023-02-15 19:31:39 +080092 if (ret >= DEBUG_BUF_SIZE - 1) {
93 eol = DEBUG_BUF_SIZE - 2;
valord57724da0cd2023-02-15 19:01:16 +080094 }
valord57725418ac2022-10-31 15:17:37 +080095 }
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020096
valord57724da0cd2023-02-15 19:01:16 +080097 /*
98 * Send if str contains '\n'.
99 */
valord577536893c2023-02-15 19:31:39 +0800100 if (eol >= 0) {
101 str[eol] = '\n';
102 str[eol + 1] = '\0';
valord57724da0cd2023-02-15 19:01:16 +0800103
104 debug_send_line(ssl, level, file, line, str);
105 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000106}
107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level,
109 const char *file, int line,
110 const char *text, int ret)
Paul Bakker5121ce52009-01-03 21:22:43 +0000111{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200112 char str[DEBUG_BUF_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100115 NULL == ssl->conf ||
116 NULL == ssl->conf->f_dbg ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100119 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200121 /*
122 * With non-blocking I/O and examples that just retry immediately,
123 * the logs would be quickly flooded with WANT_READ, so ignore that.
Tom Cosgrove1797b052022-12-04 17:19:59 +0000124 * Don't ignore WANT_WRITE however, since it is usually rare.
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200125 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200127 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 }
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 mbedtls_snprintf(str, sizeof(str), "%s() returned %d (-0x%04x)\n",
131 text, ret, (unsigned int) -ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000134}
135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136void mbedtls_debug_print_buf(const mbedtls_ssl_context *ssl, int level,
137 const char *file, int line, const char *text,
138 const unsigned char *buf, size_t len)
Paul Bakker5121ce52009-01-03 21:22:43 +0000139{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200140 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100141 char txt[17];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200142 size_t i, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100145 NULL == ssl->conf ||
146 NULL == ssl->conf->f_dbg ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100149 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 mbedtls_snprintf(str + idx, sizeof(str) - idx, "dumping '%s' (%u bytes)\n",
152 text, (unsigned int) len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000155
Paul Bakker92478c32014-04-25 15:18:34 +0200156 idx = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 memset(txt, 0, sizeof(txt));
158 for (i = 0; i < len; i++) {
159 if (i >= 4096) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 if (i % 16 == 0) {
164 if (i > 0) {
165 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
166 debug_send_line(ssl, level, file, line, str);
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100167
Paul Bakker92478c32014-04-25 15:18:34 +0200168 idx = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 memset(txt, 0, sizeof(txt));
Paul Bakker92478c32014-04-25 15:18:34 +0200170 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000171
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, "%04x: ",
173 (unsigned int) i);
Paul Bakker5121ce52009-01-03 21:22:43 +0000174
Paul Bakker5121ce52009-01-03 21:22:43 +0000175 }
176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x",
178 (unsigned int) buf[i]);
179 txt[i % 16] = (buf[i] > 31 && buf[i] < 127) ? buf[i] : '.';
Paul Bakker5121ce52009-01-03 21:22:43 +0000180 }
181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 if (len > 0) {
183 for (/* i = i */; i % 16 != 0; i++) {
184 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " ");
185 }
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
188 debug_send_line(ssl, level, file, line, str);
Paul Bakker92478c32014-04-25 15:18:34 +0200189 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000190}
191
Valerio Setti6c496a12023-04-07 15:53:51 +0200192#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100193void mbedtls_debug_print_ecp(const mbedtls_ssl_context *ssl, int level,
194 const char *file, int line,
195 const char *text, const mbedtls_ecp_point *X)
Paul Bakker41c83d32013-03-20 14:39:14 +0100196{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200197 char str[DEBUG_BUF_SIZE];
Paul Bakker41c83d32013-03-20 14:39:14 +0100198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100200 NULL == ssl->conf ||
201 NULL == ssl->conf->f_dbg ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 level > debug_threshold) {
Paul Bakkerc73079a2014-04-25 16:34:30 +0200203 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100204 }
Paul Bakkerc73079a2014-04-25 16:34:30 +0200205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 mbedtls_snprintf(str, sizeof(str), "%s(X)", text);
207 mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->X);
Paul Bakker41c83d32013-03-20 14:39:14 +0100208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 mbedtls_snprintf(str, sizeof(str), "%s(Y)", text);
210 mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->Y);
Paul Bakker41c83d32013-03-20 14:39:14 +0100211}
Valerio Setti6c496a12023-04-07 15:53:51 +0200212#endif /* MBEDTLS_ECP_LIGHT */
Paul Bakker41c83d32013-03-20 14:39:14 +0100213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100215void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl, int level,
216 const char *file, int line,
217 const char *text, const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000218{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200219 char str[DEBUG_BUF_SIZE];
Gilles Peskineb26696b2021-06-02 20:17:46 +0200220 size_t bitlen;
221 size_t idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000222
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100224 NULL == ssl->conf ||
225 NULL == ssl->conf->f_dbg ||
226 NULL == X ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100229 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 bitlen = mbedtls_mpi_bitlen(X);
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 mbedtls_snprintf(str, sizeof(str), "value of '%s' (%u bits) is:\n",
234 text, (unsigned) bitlen);
235 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 if (bitlen == 0) {
Gilles Peskineb26696b2021-06-02 20:17:46 +0200238 str[0] = ' '; str[1] = '0'; str[2] = '0';
239 idx = 3;
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 } else {
Gilles Peskineb26696b2021-06-02 20:17:46 +0200241 int n;
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 for (n = (int) ((bitlen - 1) / 8); n >= 0; n--) {
243 size_t limb_offset = n / sizeof(mbedtls_mpi_uint);
244 size_t offset_in_limb = n % sizeof(mbedtls_mpi_uint);
Gilles Peskineb26696b2021-06-02 20:17:46 +0200245 unsigned char octet =
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 (X->p[limb_offset] >> (offset_in_limb * 8)) & 0xff;
247 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x", octet);
Gilles Peskineb26696b2021-06-02 20:17:46 +0200248 idx += 3;
249 /* Wrap lines after 16 octets that each take 3 columns */
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 if (idx >= 3 * 16) {
251 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
252 debug_send_line(ssl, level, file, line, str);
Gilles Peskineb26696b2021-06-02 20:17:46 +0200253 idx = 0;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000254 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000256 }
257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 if (idx != 0) {
259 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
260 debug_send_line(ssl, level, file, line, str);
Gilles Peskineb26696b2021-06-02 20:17:46 +0200261 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000262}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000264
Hanno Becker612a2f12020-10-09 09:19:39 +0100265#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_INFO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100266static void debug_print_pk(const mbedtls_ssl_context *ssl, int level,
267 const char *file, int line,
268 const char *text, const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200269{
270 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200272 char name[16];
273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 memset(items, 0, sizeof(items));
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 if (mbedtls_pk_debug(pk, items) != 0) {
277 debug_send_line(ssl, level, file, line,
278 "invalid PK context\n");
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200279 return;
280 }
281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 for (i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++) {
283 if (items[i].type == MBEDTLS_PK_DEBUG_NONE) {
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200284 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 mbedtls_snprintf(name, sizeof(name), "%s%s", text, items[i].name);
288 name[sizeof(name) - 1] = '\0';
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 if (items[i].type == MBEDTLS_PK_DEBUG_MPI) {
291 mbedtls_debug_print_mpi(ssl, level, file, line, name, items[i].value);
292 } else
Valerio Setti6c496a12023-04-07 15:53:51 +0200293#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 if (items[i].type == MBEDTLS_PK_DEBUG_ECP) {
295 mbedtls_debug_print_ecp(ssl, level, file, line, name, items[i].value);
296 } else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200297#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 { debug_send_line(ssl, level, file, line,
299 "should not happen\n"); }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200300 }
301}
302
Gilles Peskine449bd832023-01-11 14:50:10 +0100303static void debug_print_line_by_line(const mbedtls_ssl_context *ssl, int level,
304 const char *file, int line, const char *text)
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200305{
306 char str[DEBUG_BUF_SIZE];
307 const char *start, *cur;
308
309 start = text;
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 for (cur = text; *cur != '\0'; cur++) {
311 if (*cur == '\n') {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200312 size_t len = cur - start + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 if (len > DEBUG_BUF_SIZE - 1) {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200314 len = DEBUG_BUF_SIZE - 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 }
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 memcpy(str, start, len);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200318 str[len] = '\0';
319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 debug_send_line(ssl, level, file, line, str);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200321
322 start = cur + 1;
323 }
324 }
325}
326
Gilles Peskine449bd832023-01-11 14:50:10 +0100327void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level,
328 const char *file, int line,
329 const char *text, const mbedtls_x509_crt *crt)
Paul Bakker5121ce52009-01-03 21:22:43 +0000330{
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200331 char str[DEBUG_BUF_SIZE];
332 int i = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000333
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100335 NULL == ssl->conf ||
336 NULL == ssl->conf->f_dbg ||
337 NULL == crt ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000339 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100340 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000341
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 while (crt != NULL) {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000343 char buf[1024];
Paul Bakker5121ce52009-01-03 21:22:43 +0000344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 mbedtls_snprintf(str, sizeof(str), "%s #%d:\n", text, ++i);
346 debug_send_line(ssl, level, file, line, str);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt);
349 debug_print_line_by_line(ssl, level, file, line, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 debug_print_pk(ssl, level, file, line, "crt->", &crt->pk);
Paul Bakker5121ce52009-01-03 21:22:43 +0000352
353 crt = crt->next;
354 }
355}
Hanno Becker612a2f12020-10-09 09:19:39 +0100356#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_X509_REMOVE_INFO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000357
Janos Follath948f4be2018-08-22 01:37:55 +0100358#if defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100359static void mbedtls_debug_printf_ecdh_internal(const mbedtls_ssl_context *ssl,
360 int level, const char *file,
361 int line,
362 const mbedtls_ecdh_context *ecdh,
363 mbedtls_debug_ecdh_attr attr)
Janos Follath948f4be2018-08-22 01:37:55 +0100364{
365#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 const mbedtls_ecdh_context *ctx = ecdh;
Janos Follath948f4be2018-08-22 01:37:55 +0100367#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 const mbedtls_ecdh_context_mbed *ctx = &ecdh->ctx.mbed_ecdh;
Janos Follath948f4be2018-08-22 01:37:55 +0100369#endif
370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 switch (attr) {
Janos Follath948f4be2018-08-22 01:37:55 +0100372 case MBEDTLS_DEBUG_ECDH_Q:
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Q",
374 &ctx->Q);
Janos Follath948f4be2018-08-22 01:37:55 +0100375 break;
376 case MBEDTLS_DEBUG_ECDH_QP:
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Qp",
378 &ctx->Qp);
Janos Follath948f4be2018-08-22 01:37:55 +0100379 break;
380 case MBEDTLS_DEBUG_ECDH_Z:
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 mbedtls_debug_print_mpi(ssl, level, file, line, "ECDH: z",
382 &ctx->z);
Janos Follath948f4be2018-08-22 01:37:55 +0100383 break;
384 default:
385 break;
386 }
387}
388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389void mbedtls_debug_printf_ecdh(const mbedtls_ssl_context *ssl, int level,
390 const char *file, int line,
391 const mbedtls_ecdh_context *ecdh,
392 mbedtls_debug_ecdh_attr attr)
Janos Follath948f4be2018-08-22 01:37:55 +0100393{
394#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh, attr);
Janos Follath948f4be2018-08-22 01:37:55 +0100396#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 switch (ecdh->var) {
Janos Follath948f4be2018-08-22 01:37:55 +0100398 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh,
400 attr);
Janos Follath948f4be2018-08-22 01:37:55 +0100401 }
402#endif
403}
404#endif /* MBEDTLS_ECDH_C */
405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406#endif /* MBEDTLS_DEBUG_C */