blob: 88c289b384560431b02944a21a4e26d123690729 [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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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;
valord57706b0bb52023-02-15 19:31:39 +080071
72#if defined(static_assert)
73 static_assert(DEBUG_BUF_SIZE >= 2)
74#endif
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +020075
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +010077 NULL == ssl->conf ||
78 NULL == ssl->conf->f_dbg ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010079 level > debug_threshold) {
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020080 return;
Hanno Becker687c5002018-06-29 09:04:46 +010081 }
Paul Bakker5121ce52009-01-03 21:22:43 +000082
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010083 va_start(argp, format);
84 ret = mbedtls_vsnprintf(str, DEBUG_BUF_SIZE, format, argp);
85 va_end(argp);
Paul Bakker5121ce52009-01-03 21:22:43 +000086
valord57706b0bb52023-02-15 19:31:39 +080087 if (ret < 0) {
valord5770d87d902023-02-15 21:46:47 +080088 ret = 0;
valord57706b0bb52023-02-15 19:31:39 +080089 } else {
valord57706b0bb52023-02-15 19:31:39 +080090 if (ret >= DEBUG_BUF_SIZE - 1) {
valord5770d87d902023-02-15 21:46:47 +080091 ret = DEBUG_BUF_SIZE - 2;
valord577e3623922023-02-15 19:01:16 +080092 }
valord577afbaac22022-10-31 15:17:37 +080093 }
valord5770d87d902023-02-15 21:46:47 +080094 str[ret] = '\n';
95 str[ret + 1] = '\0';
Manuel Pégourié-Gonnardb74c2452015-06-29 20:08:23 +020096
valord5770d87d902023-02-15 21:46:47 +080097 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +000098}
99
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100100void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level,
101 const char *file, int line,
102 const char *text, int ret)
Paul Bakker5121ce52009-01-03 21:22:43 +0000103{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200104 char str[DEBUG_BUF_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100107 NULL == ssl->conf ||
108 NULL == ssl->conf->f_dbg ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100109 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000110 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100111 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200113 /*
114 * With non-blocking I/O and examples that just retry immediately,
115 * the logs would be quickly flooded with WANT_READ, so ignore that.
116 * Don't ignore WANT_WRITE however, since is is usually rare.
117 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118 if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200119 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100120 }
Manuel Pégourié-Gonnard4cba1a72015-05-11 18:52:25 +0200121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122 mbedtls_snprintf(str, sizeof(str), "%s() returned %d (-0x%04x)\n",
123 text, ret, (unsigned int) -ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000124
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100125 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000126}
127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100128void mbedtls_debug_print_buf(const mbedtls_ssl_context *ssl, int level,
129 const char *file, int line, const char *text,
130 const unsigned char *buf, size_t len)
Paul Bakker5121ce52009-01-03 21:22:43 +0000131{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200132 char str[DEBUG_BUF_SIZE];
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100133 char txt[17];
Manuel Pégourié-Gonnard9dbaf402015-06-22 11:50:58 +0200134 size_t i, idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000135
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100137 NULL == ssl->conf ||
138 NULL == ssl->conf->f_dbg ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100139 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100141 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100143 mbedtls_snprintf(str + idx, sizeof(str) - idx, "dumping '%s' (%u bytes)\n",
144 text, (unsigned int) len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100146 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000147
Paul Bakker92478c32014-04-25 15:18:34 +0200148 idx = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100149 memset(txt, 0, sizeof(txt));
150 for (i = 0; i < len; i++) {
151 if (i >= 4096) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100153 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100155 if (i % 16 == 0) {
156 if (i > 0) {
157 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
158 debug_send_line(ssl, level, file, line, str);
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100159
Paul Bakker92478c32014-04-25 15:18:34 +0200160 idx = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100161 memset(txt, 0, sizeof(txt));
Paul Bakker92478c32014-04-25 15:18:34 +0200162 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100164 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, "%04x: ",
165 (unsigned int) i);
Paul Bakker5121ce52009-01-03 21:22:43 +0000166
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 }
168
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100169 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x",
170 (unsigned int) buf[i]);
171 txt[i % 16] = (buf[i] > 31 && buf[i] < 127) ? buf[i] : '.';
Paul Bakker5121ce52009-01-03 21:22:43 +0000172 }
173
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100174 if (len > 0) {
175 for (/* i = i */; i % 16 != 0; i++) {
176 idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " ");
177 }
Manuel Pégourié-Gonnard8c9223d2014-11-19 10:17:21 +0100178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100179 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %s\n", txt);
180 debug_send_line(ssl, level, file, line, str);
Paul Bakker92478c32014-04-25 15:18:34 +0200181 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000182}
183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100185void mbedtls_debug_print_ecp(const mbedtls_ssl_context *ssl, int level,
186 const char *file, int line,
187 const char *text, const mbedtls_ecp_point *X)
Paul Bakker41c83d32013-03-20 14:39:14 +0100188{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200189 char str[DEBUG_BUF_SIZE];
Paul Bakker41c83d32013-03-20 14:39:14 +0100190
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100191 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100192 NULL == ssl->conf ||
193 NULL == ssl->conf->f_dbg ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194 level > debug_threshold) {
Paul Bakkerc73079a2014-04-25 16:34:30 +0200195 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100196 }
Paul Bakkerc73079a2014-04-25 16:34:30 +0200197
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100198 mbedtls_snprintf(str, sizeof(str), "%s(X)", text);
199 mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->X);
Paul Bakker41c83d32013-03-20 14:39:14 +0100200
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100201 mbedtls_snprintf(str, sizeof(str), "%s(Y)", text);
202 mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->Y);
Paul Bakker41c83d32013-03-20 14:39:14 +0100203}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#endif /* MBEDTLS_ECP_C */
Paul Bakker41c83d32013-03-20 14:39:14 +0100205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl, int level,
208 const char *file, int line,
209 const char *text, const mbedtls_mpi *X)
Paul Bakker5121ce52009-01-03 21:22:43 +0000210{
Manuel Pégourié-Gonnardd23f5932015-06-23 12:04:52 +0200211 char str[DEBUG_BUF_SIZE];
Gilles Peskine2ee0bb32021-06-02 20:17:46 +0200212 size_t bitlen;
213 size_t idx = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000214
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100215 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100216 NULL == ssl->conf ||
217 NULL == ssl->conf->f_dbg ||
218 NULL == X ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100219 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000220 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100221 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000222
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100223 bitlen = mbedtls_mpi_bitlen(X);
Paul Bakker5121ce52009-01-03 21:22:43 +0000224
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100225 mbedtls_snprintf(str, sizeof(str), "value of '%s' (%u bits) is:\n",
226 text, (unsigned) bitlen);
227 debug_send_line(ssl, level, file, line, str);
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100229 if (bitlen == 0) {
Gilles Peskine2ee0bb32021-06-02 20:17:46 +0200230 str[0] = ' '; str[1] = '0'; str[2] = '0';
231 idx = 3;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100232 } else {
Gilles Peskine2ee0bb32021-06-02 20:17:46 +0200233 int n;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100234 for (n = (int) ((bitlen - 1) / 8); n >= 0; n--) {
235 size_t limb_offset = n / sizeof(mbedtls_mpi_uint);
236 size_t offset_in_limb = n % sizeof(mbedtls_mpi_uint);
Gilles Peskine2ee0bb32021-06-02 20:17:46 +0200237 unsigned char octet =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100238 (X->p[limb_offset] >> (offset_in_limb * 8)) & 0xff;
239 mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x", octet);
Gilles Peskine2ee0bb32021-06-02 20:17:46 +0200240 idx += 3;
241 /* Wrap lines after 16 octets that each take 3 columns */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100242 if (idx >= 3 * 16) {
243 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
244 debug_send_line(ssl, level, file, line, str);
Gilles Peskine2ee0bb32021-06-02 20:17:46 +0200245 idx = 0;
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000246 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000247 }
Paul Bakkerbe4e7dc2011-03-14 20:41:31 +0000248 }
249
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100250 if (idx != 0) {
251 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n");
252 debug_send_line(ssl, level, file, line, str);
Gilles Peskine2ee0bb32021-06-02 20:17:46 +0200253 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000254}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255#endif /* MBEDTLS_BIGNUM_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258static void debug_print_pk(const mbedtls_ssl_context *ssl, int level,
259 const char *file, int line,
260 const char *text, const mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200261{
262 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200264 char name[16];
265
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266 memset(items, 0, sizeof(items));
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200267
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100268 if (mbedtls_pk_debug(pk, items) != 0) {
269 debug_send_line(ssl, level, file, line,
270 "invalid PK context\n");
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200271 return;
272 }
273
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100274 for (i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++) {
275 if (items[i].type == MBEDTLS_PK_DEBUG_NONE) {
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200276 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277 }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200278
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279 mbedtls_snprintf(name, sizeof(name), "%s%s", text, items[i].name);
280 name[sizeof(name) - 1] = '\0';
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200281
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100282 if (items[i].type == MBEDTLS_PK_DEBUG_MPI) {
283 mbedtls_debug_print_mpi(ssl, level, file, line, name, items[i].value);
284 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100286 if (items[i].type == MBEDTLS_PK_DEBUG_ECP) {
287 mbedtls_debug_print_ecp(ssl, level, file, line, name, items[i].value);
288 } else
Manuel Pégourié-Gonnardbac0e3b2013-10-15 11:54:47 +0200289#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100290 { debug_send_line(ssl, level, file, line,
291 "should not happen\n"); }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200292 }
293}
294
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295static void debug_print_line_by_line(const mbedtls_ssl_context *ssl, int level,
296 const char *file, int line, const char *text)
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200297{
298 char str[DEBUG_BUF_SIZE];
299 const char *start, *cur;
300
301 start = text;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100302 for (cur = text; *cur != '\0'; cur++) {
303 if (*cur == '\n') {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200304 size_t len = cur - start + 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100305 if (len > DEBUG_BUF_SIZE - 1) {
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200306 len = DEBUG_BUF_SIZE - 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100307 }
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200308
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100309 memcpy(str, start, len);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200310 str[len] = '\0';
311
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100312 debug_send_line(ssl, level, file, line, str);
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200313
314 start = cur + 1;
315 }
316 }
317}
318
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100319void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level,
320 const char *file, int line,
321 const char *text, const mbedtls_x509_crt *crt)
Paul Bakker5121ce52009-01-03 21:22:43 +0000322{
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +0200323 char str[DEBUG_BUF_SIZE];
324 int i = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000325
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100326 if (NULL == ssl ||
Hanno Becker687c5002018-06-29 09:04:46 +0100327 NULL == ssl->conf ||
328 NULL == ssl->conf->f_dbg ||
329 NULL == crt ||
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100330 level > debug_threshold) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000331 return;
Hanno Becker687c5002018-06-29 09:04:46 +0100332 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000333
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100334 while (crt != NULL) {
Paul Bakkerd98030e2009-05-02 15:13:40 +0000335 char buf[1024];
Paul Bakker5121ce52009-01-03 21:22:43 +0000336
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100337 mbedtls_snprintf(str, sizeof(str), "%s #%d:\n", text, ++i);
338 debug_send_line(ssl, level, file, line, str);
Paul Bakkereaebbd52014-04-25 15:04:14 +0200339
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100340 mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt);
341 debug_print_line_by_line(ssl, level, file, line, buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000342
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100343 debug_print_pk(ssl, level, file, line, "crt->", &crt->pk);
Paul Bakker5121ce52009-01-03 21:22:43 +0000344
345 crt = crt->next;
346 }
347}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +0000349
Janos Follath948f4be2018-08-22 01:37:55 +0100350#if defined(MBEDTLS_ECDH_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100351static void mbedtls_debug_printf_ecdh_internal(const mbedtls_ssl_context *ssl,
352 int level, const char *file,
353 int line,
354 const mbedtls_ecdh_context *ecdh,
355 mbedtls_debug_ecdh_attr attr)
Janos Follath948f4be2018-08-22 01:37:55 +0100356{
357#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100358 const mbedtls_ecdh_context *ctx = ecdh;
Janos Follath948f4be2018-08-22 01:37:55 +0100359#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100360 const mbedtls_ecdh_context_mbed *ctx = &ecdh->ctx.mbed_ecdh;
Janos Follath948f4be2018-08-22 01:37:55 +0100361#endif
362
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100363 switch (attr) {
Janos Follath948f4be2018-08-22 01:37:55 +0100364 case MBEDTLS_DEBUG_ECDH_Q:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100365 mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Q",
366 &ctx->Q);
Janos Follath948f4be2018-08-22 01:37:55 +0100367 break;
368 case MBEDTLS_DEBUG_ECDH_QP:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100369 mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Qp",
370 &ctx->Qp);
Janos Follath948f4be2018-08-22 01:37:55 +0100371 break;
372 case MBEDTLS_DEBUG_ECDH_Z:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100373 mbedtls_debug_print_mpi(ssl, level, file, line, "ECDH: z",
374 &ctx->z);
Janos Follath948f4be2018-08-22 01:37:55 +0100375 break;
376 default:
377 break;
378 }
379}
380
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100381void mbedtls_debug_printf_ecdh(const mbedtls_ssl_context *ssl, int level,
382 const char *file, int line,
383 const mbedtls_ecdh_context *ecdh,
384 mbedtls_debug_ecdh_attr attr)
Janos Follath948f4be2018-08-22 01:37:55 +0100385{
386#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100387 mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh, attr);
Janos Follath948f4be2018-08-22 01:37:55 +0100388#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100389 switch (ecdh->var) {
Janos Follath948f4be2018-08-22 01:37:55 +0100390 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391 mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh,
392 attr);
Janos Follath948f4be2018-08-22 01:37:55 +0100393 }
394#endif
395}
396#endif /* MBEDTLS_ECDH_C */
397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398#endif /* MBEDTLS_DEBUG_C */