| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  *  Debugging routines | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2020-09-05 12:53:20 +02:00
										 |  |  |  *  Copyright The Mbed TLS Contributors | 
					
						
							| 
									
										
										
										
											2024-01-30 14:09:13 +01:00
										 |  |  |  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-21 12:54:05 +01:00
										 |  |  | #include "common.h"
 | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if defined(MBEDTLS_DEBUG_C)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "mbedtls/platform.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  | #include "debug_internal.h"
 | 
					
						
							| 
									
										
										
										
											2021-12-21 12:54:05 +01:00
										 |  |  | #include "mbedtls/error.h"
 | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <stdarg.h>
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-04 18:09:03 +02:00
										 |  |  | /* DEBUG_BUF_SIZE must be at least 2 */ | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | #define DEBUG_BUF_SIZE      512
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int debug_threshold = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | void mbedtls_debug_set_threshold(int threshold) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     debug_threshold = threshold; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * All calls to f_dbg must be made via this function | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | static inline void debug_send_line(const mbedtls_ssl_context *ssl, int level, | 
					
						
							|  |  |  |                                    const char *file, int line, | 
					
						
							|  |  |  |                                    const char *str) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * If in a threaded environment, we need a thread identifier. | 
					
						
							|  |  |  |      * Since there is no portable way to get one, use the address of the ssl | 
					
						
							|  |  |  |      * context instead, as it shouldn't be shared between threads. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | #if defined(MBEDTLS_THREADING_C)
 | 
					
						
							|  |  |  |     char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */ | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     mbedtls_snprintf(idstr, sizeof(idstr), "%p: %s", (void *) ssl, str); | 
					
						
							|  |  |  |     ssl->conf->f_dbg(ssl->conf->p_dbg, level, file, line, idstr); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     ssl->conf->f_dbg(ssl->conf->p_dbg, level, file, line, str); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-21 12:54:05 +01:00
										 |  |  | MBEDTLS_PRINTF_ATTRIBUTE(5, 6) | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level, | 
					
						
							|  |  |  |                              const char *file, int line, | 
					
						
							|  |  |  |                              const char *format, ...) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     va_list argp; | 
					
						
							|  |  |  |     char str[DEBUG_BUF_SIZE]; | 
					
						
							| 
									
										
										
										
											2021-12-21 12:54:05 +01:00
										 |  |  |     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-04 18:09:03 +02:00
										 |  |  |     MBEDTLS_STATIC_ASSERT(DEBUG_BUF_SIZE >= 2, "DEBUG_BUF_SIZE too small"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (NULL == ssl              || | 
					
						
							| 
									
										
										
										
											2019-07-10 16:15:01 +02:00
										 |  |  |         NULL == ssl->conf        || | 
					
						
							|  |  |  |         NULL == ssl->conf->f_dbg || | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         level > debug_threshold) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-07-10 16:15:01 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     va_start(argp, format); | 
					
						
							|  |  |  |     ret = mbedtls_vsnprintf(str, DEBUG_BUF_SIZE, format, argp); | 
					
						
							|  |  |  |     va_end(argp); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-04 18:09:03 +02:00
										 |  |  |     if (ret < 0) { | 
					
						
							|  |  |  |         ret = 0; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         if (ret >= DEBUG_BUF_SIZE - 1) { | 
					
						
							|  |  |  |             ret = DEBUG_BUF_SIZE - 2; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-08-04 18:09:03 +02:00
										 |  |  |     str[ret]     = '\n'; | 
					
						
							|  |  |  |     str[ret + 1] = '\0'; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     debug_send_line(ssl, level, file, line, str); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level, | 
					
						
							|  |  |  |                              const char *file, int line, | 
					
						
							|  |  |  |                              const char *text, int ret) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     char str[DEBUG_BUF_SIZE]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (NULL == ssl              || | 
					
						
							| 
									
										
										
										
											2019-07-10 16:15:01 +02:00
										 |  |  |         NULL == ssl->conf        || | 
					
						
							|  |  |  |         NULL == ssl->conf->f_dbg || | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         level > debug_threshold) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-07-10 16:15:01 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * With non-blocking I/O and examples that just retry immediately, | 
					
						
							|  |  |  |      * the logs would be quickly flooded with WANT_READ, so ignore that. | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  |      * Don't ignore WANT_WRITE however, since it is usually rare. | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (ret == MBEDTLS_ERR_SSL_WANT_READ) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     mbedtls_snprintf(str, sizeof(str), "%s() returned %d (-0x%04x)\n", | 
					
						
							|  |  |  |                      text, ret, (unsigned int) -ret); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     debug_send_line(ssl, level, file, line, str); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | void mbedtls_debug_print_buf(const mbedtls_ssl_context *ssl, int level, | 
					
						
							|  |  |  |                              const char *file, int line, const char *text, | 
					
						
							|  |  |  |                              const unsigned char *buf, size_t len) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     char str[DEBUG_BUF_SIZE]; | 
					
						
							|  |  |  |     char txt[17]; | 
					
						
							|  |  |  |     size_t i, idx = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (NULL == ssl              || | 
					
						
							| 
									
										
										
										
											2019-07-10 16:15:01 +02:00
										 |  |  |         NULL == ssl->conf        || | 
					
						
							|  |  |  |         NULL == ssl->conf->f_dbg || | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         level > debug_threshold) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-07-10 16:15:01 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     mbedtls_snprintf(str + idx, sizeof(str) - idx, "dumping '%s' (%u bytes)\n", | 
					
						
							|  |  |  |                      text, (unsigned int) len); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     debug_send_line(ssl, level, file, line, str); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     memset(txt, 0, sizeof(txt)); | 
					
						
							|  |  |  |     for (i = 0; i < len; i++) { | 
					
						
							|  |  |  |         if (i >= 4096) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |             break; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         if (i % 16 == 0) { | 
					
						
							|  |  |  |             if (i > 0) { | 
					
						
							|  |  |  |                 mbedtls_snprintf(str + idx, sizeof(str) - idx, "  %s\n", txt); | 
					
						
							|  |  |  |                 debug_send_line(ssl, level, file, line, str); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 idx = 0; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |                 memset(txt, 0, sizeof(txt)); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |             idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, "%04x: ", | 
					
						
							|  |  |  |                                     (unsigned int) i); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x", | 
					
						
							|  |  |  |                                 (unsigned int) buf[i]); | 
					
						
							|  |  |  |         txt[i % 16] = (buf[i] > 31 && buf[i] < 127) ? buf[i] : '.'; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (len > 0) { | 
					
						
							|  |  |  |         for (/* i = i */; i % 16 != 0; i++) { | 
					
						
							|  |  |  |             idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, "   "); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         mbedtls_snprintf(str + idx, sizeof(str) - idx, "  %s\n", txt); | 
					
						
							|  |  |  |         debug_send_line(ssl, level, file, line, str); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  | #if defined(MBEDTLS_ECP_LIGHT)
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | void mbedtls_debug_print_ecp(const mbedtls_ssl_context *ssl, int level, | 
					
						
							|  |  |  |                              const char *file, int line, | 
					
						
							|  |  |  |                              const char *text, const mbedtls_ecp_point *X) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     char str[DEBUG_BUF_SIZE]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (NULL == ssl              || | 
					
						
							| 
									
										
										
										
											2019-07-10 16:15:01 +02:00
										 |  |  |         NULL == ssl->conf        || | 
					
						
							|  |  |  |         NULL == ssl->conf->f_dbg || | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         level > debug_threshold) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-07-10 16:15:01 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     mbedtls_snprintf(str, sizeof(str), "%s(X)", text); | 
					
						
							|  |  |  |     mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->X); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     mbedtls_snprintf(str, sizeof(str), "%s(Y)", text); | 
					
						
							|  |  |  |     mbedtls_debug_print_mpi(ssl, level, file, line, str, &X->Y); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  | #endif /* MBEDTLS_ECP_LIGHT */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
 | 
					
						
							|  |  |  | static void mbedtls_debug_print_ec_coord(const mbedtls_ssl_context *ssl, int level, | 
					
						
							|  |  |  |                                          const char *file, int line, const char *text, | 
					
						
							|  |  |  |                                          const unsigned char *buf, size_t len) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     char str[DEBUG_BUF_SIZE]; | 
					
						
							|  |  |  |     size_t i, idx = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     mbedtls_snprintf(str + idx, sizeof(str) - idx, "value of '%s' (%u bits) is:\n", | 
					
						
							|  |  |  |                      text, (unsigned int) len * 8); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     debug_send_line(ssl, level, file, line, str); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (i = 0; i < len; i++) { | 
					
						
							|  |  |  |         if (i >= 4096) { | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (i % 16 == 0) { | 
					
						
							|  |  |  |             if (i > 0) { | 
					
						
							|  |  |  |                 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n"); | 
					
						
							|  |  |  |                 debug_send_line(ssl, level, file, line, str); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 idx = 0; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x", | 
					
						
							|  |  |  |                                 (unsigned int) buf[i]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (len > 0) { | 
					
						
							|  |  |  |         for (/* i = i */; i % 16 != 0; i++) { | 
					
						
							|  |  |  |             idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, "   "); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n"); | 
					
						
							|  |  |  |         debug_send_line(ssl, level, file, line, str); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void mbedtls_debug_print_psa_ec(const mbedtls_ssl_context *ssl, int level, | 
					
						
							|  |  |  |                                 const char *file, int line, | 
					
						
							|  |  |  |                                 const char *text, const mbedtls_pk_context *pk) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     char str[DEBUG_BUF_SIZE]; | 
					
						
							|  |  |  |     const uint8_t *coord_start; | 
					
						
							|  |  |  |     size_t coord_len; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (NULL == ssl              || | 
					
						
							|  |  |  |         NULL == ssl->conf        || | 
					
						
							|  |  |  |         NULL == ssl->conf->f_dbg || | 
					
						
							|  |  |  |         level > debug_threshold) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* For the description of pk->pk_raw content please refer to the description
 | 
					
						
							|  |  |  |      * psa_export_public_key() function. */ | 
					
						
							|  |  |  |     coord_len = (pk->pub_raw_len - 1)/2; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* X coordinate */ | 
					
						
							|  |  |  |     coord_start = pk->pub_raw + 1; | 
					
						
							|  |  |  |     mbedtls_snprintf(str, sizeof(str), "%s(X)", text); | 
					
						
							|  |  |  |     mbedtls_debug_print_ec_coord(ssl, level, file, line, str, coord_start, coord_len); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Y coordinate */ | 
					
						
							|  |  |  |     coord_start = coord_start + coord_len; | 
					
						
							|  |  |  |     mbedtls_snprintf(str, sizeof(str), "%s(Y)", text); | 
					
						
							|  |  |  |     mbedtls_debug_print_ec_coord(ssl, level, file, line, str, coord_start, coord_len); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
 | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if defined(MBEDTLS_BIGNUM_C)
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl, int level, | 
					
						
							|  |  |  |                              const char *file, int line, | 
					
						
							|  |  |  |                              const char *text, const mbedtls_mpi *X) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     char str[DEBUG_BUF_SIZE]; | 
					
						
							| 
									
										
										
										
											2021-07-20 12:32:46 +02:00
										 |  |  |     size_t bitlen; | 
					
						
							|  |  |  |     size_t idx = 0; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (NULL == ssl              || | 
					
						
							| 
									
										
										
										
											2019-07-10 16:15:01 +02:00
										 |  |  |         NULL == ssl->conf        || | 
					
						
							|  |  |  |         NULL == ssl->conf->f_dbg || | 
					
						
							|  |  |  |         NULL == X                || | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         level > debug_threshold) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-07-10 16:15:01 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     bitlen = mbedtls_mpi_bitlen(X); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     mbedtls_snprintf(str, sizeof(str), "value of '%s' (%u bits) is:\n", | 
					
						
							|  |  |  |                      text, (unsigned) bitlen); | 
					
						
							|  |  |  |     debug_send_line(ssl, level, file, line, str); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (bitlen == 0) { | 
					
						
							| 
									
										
										
										
											2021-07-20 12:32:46 +02:00
										 |  |  |         str[0] = ' '; str[1] = '0'; str[2] = '0'; | 
					
						
							|  |  |  |         idx = 3; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2021-07-20 12:32:46 +02:00
										 |  |  |         int n; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         for (n = (int) ((bitlen - 1) / 8); n >= 0; n--) { | 
					
						
							|  |  |  |             size_t limb_offset = n / sizeof(mbedtls_mpi_uint); | 
					
						
							|  |  |  |             size_t offset_in_limb = n % sizeof(mbedtls_mpi_uint); | 
					
						
							| 
									
										
										
										
											2021-07-20 12:32:46 +02:00
										 |  |  |             unsigned char octet = | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |                 (X->p[limb_offset] >> (offset_in_limb * 8)) & 0xff; | 
					
						
							|  |  |  |             mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x", octet); | 
					
						
							| 
									
										
										
										
											2021-07-20 12:32:46 +02:00
										 |  |  |             idx += 3; | 
					
						
							|  |  |  |             /* Wrap lines after 16 octets that each take 3 columns */ | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |             if (idx >= 3 * 16) { | 
					
						
							|  |  |  |                 mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n"); | 
					
						
							|  |  |  |                 debug_send_line(ssl, level, file, line, str); | 
					
						
							| 
									
										
										
										
											2021-07-20 12:32:46 +02:00
										 |  |  |                 idx = 0; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (idx != 0) { | 
					
						
							|  |  |  |         mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n"); | 
					
						
							|  |  |  |         debug_send_line(ssl, level, file, line, str); | 
					
						
							| 
									
										
										
										
											2021-07-20 12:32:46 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | #endif /* MBEDTLS_BIGNUM_C */
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  | #if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_INFO)
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | static void debug_print_pk(const mbedtls_ssl_context *ssl, int level, | 
					
						
							|  |  |  |                            const char *file, int line, | 
					
						
							|  |  |  |                            const char *text, const mbedtls_pk_context *pk) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     size_t i; | 
					
						
							|  |  |  |     mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS]; | 
					
						
							|  |  |  |     char name[16]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     memset(items, 0, sizeof(items)); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (mbedtls_pk_debug(pk, items) != 0) { | 
					
						
							|  |  |  |         debug_send_line(ssl, level, file, line, | 
					
						
							|  |  |  |                         "invalid PK context\n"); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     for (i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++) { | 
					
						
							|  |  |  |         if (items[i].type == MBEDTLS_PK_DEBUG_NONE) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |             return; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         mbedtls_snprintf(name, sizeof(name), "%s%s", text, items[i].name); | 
					
						
							|  |  |  |         name[sizeof(name) - 1] = '\0'; | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  | #if defined(MBEDTLS_RSA_C)
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         if (items[i].type == MBEDTLS_PK_DEBUG_MPI) { | 
					
						
							|  |  |  |             mbedtls_debug_print_mpi(ssl, level, file, line, name, items[i].value); | 
					
						
							|  |  |  |         } else | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  | #endif /* MBEDTLS_RSA_C */
 | 
					
						
							|  |  |  | #if defined(MBEDTLS_ECP_LIGHT)
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         if (items[i].type == MBEDTLS_PK_DEBUG_ECP) { | 
					
						
							|  |  |  |             mbedtls_debug_print_ecp(ssl, level, file, line, name, items[i].value); | 
					
						
							|  |  |  |         } else | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  | #endif /* MBEDTLS_ECP_LIGHT */
 | 
					
						
							|  |  |  | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
 | 
					
						
							|  |  |  |         if (items[i].type == MBEDTLS_PK_DEBUG_PSA_EC) { | 
					
						
							|  |  |  |             mbedtls_debug_print_psa_ec(ssl, level, file, line, name, items[i].value); | 
					
						
							|  |  |  |         } else | 
					
						
							|  |  |  | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         { debug_send_line(ssl, level, file, line, | 
					
						
							|  |  |  |                           "should not happen\n"); } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | static void debug_print_line_by_line(const mbedtls_ssl_context *ssl, int level, | 
					
						
							|  |  |  |                                      const char *file, int line, const char *text) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     char str[DEBUG_BUF_SIZE]; | 
					
						
							|  |  |  |     const char *start, *cur; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     start = text; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     for (cur = text; *cur != '\0'; cur++) { | 
					
						
							|  |  |  |         if (*cur == '\n') { | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  |             size_t len = (size_t) (cur - start) + 1; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |             if (len > DEBUG_BUF_SIZE - 1) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |                 len = DEBUG_BUF_SIZE - 1; | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |             memcpy(str, start, len); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |             str[len] = '\0'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |             debug_send_line(ssl, level, file, line, str); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             start = cur + 1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level, | 
					
						
							|  |  |  |                              const char *file, int line, | 
					
						
							|  |  |  |                              const char *text, const mbedtls_x509_crt *crt) | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     char str[DEBUG_BUF_SIZE]; | 
					
						
							|  |  |  |     int i = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     if (NULL == ssl              || | 
					
						
							| 
									
										
										
										
											2019-07-10 16:15:01 +02:00
										 |  |  |         NULL == ssl->conf        || | 
					
						
							|  |  |  |         NULL == ssl->conf->f_dbg || | 
					
						
							|  |  |  |         NULL == crt              || | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         level > debug_threshold) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-07-10 16:15:01 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     while (crt != NULL) { | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  |         char buf[1024]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         mbedtls_snprintf(str, sizeof(str), "%s #%d:\n", text, ++i); | 
					
						
							|  |  |  |         debug_send_line(ssl, level, file, line, str); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         mbedtls_x509_crt_info(buf, sizeof(buf) - 1, "", crt); | 
					
						
							|  |  |  |         debug_print_line_by_line(ssl, level, file, line, buf); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |         debug_print_pk(ssl, level, file, line, "crt->", &crt->pk); | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         crt = crt->next; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  | #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_X509_REMOVE_INFO */
 | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED) && \
 | 
					
						
							|  |  |  |     defined(MBEDTLS_ECDH_C) | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | static void mbedtls_debug_printf_ecdh_internal(const mbedtls_ssl_context *ssl, | 
					
						
							|  |  |  |                                                int level, const char *file, | 
					
						
							|  |  |  |                                                int line, | 
					
						
							|  |  |  |                                                const mbedtls_ecdh_context *ecdh, | 
					
						
							|  |  |  |                                                mbedtls_debug_ecdh_attr attr) | 
					
						
							| 
									
										
										
										
											2019-02-16 17:19:46 +01:00
										 |  |  | { | 
					
						
							|  |  |  | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     const mbedtls_ecdh_context *ctx = ecdh; | 
					
						
							| 
									
										
										
										
											2019-02-16 17:19:46 +01:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     const mbedtls_ecdh_context_mbed *ctx = &ecdh->ctx.mbed_ecdh; | 
					
						
							| 
									
										
										
										
											2019-02-16 17:19:46 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     switch (attr) { | 
					
						
							| 
									
										
										
										
											2019-02-16 17:19:46 +01:00
										 |  |  |         case MBEDTLS_DEBUG_ECDH_Q: | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |             mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Q", | 
					
						
							|  |  |  |                                     &ctx->Q); | 
					
						
							| 
									
										
										
										
											2019-02-16 17:19:46 +01:00
										 |  |  |             break; | 
					
						
							|  |  |  |         case MBEDTLS_DEBUG_ECDH_QP: | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |             mbedtls_debug_print_ecp(ssl, level, file, line, "ECDH: Qp", | 
					
						
							|  |  |  |                                     &ctx->Qp); | 
					
						
							| 
									
										
										
										
											2019-02-16 17:19:46 +01:00
										 |  |  |             break; | 
					
						
							|  |  |  |         case MBEDTLS_DEBUG_ECDH_Z: | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |             mbedtls_debug_print_mpi(ssl, level, file, line, "ECDH: z", | 
					
						
							|  |  |  |                                     &ctx->z); | 
					
						
							| 
									
										
										
										
											2019-02-16 17:19:46 +01:00
										 |  |  |             break; | 
					
						
							|  |  |  |         default: | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  | void mbedtls_debug_printf_ecdh(const mbedtls_ssl_context *ssl, int level, | 
					
						
							|  |  |  |                                const char *file, int line, | 
					
						
							|  |  |  |                                const mbedtls_ecdh_context *ecdh, | 
					
						
							|  |  |  |                                mbedtls_debug_ecdh_attr attr) | 
					
						
							| 
									
										
										
										
											2019-02-16 17:19:46 +01:00
										 |  |  | { | 
					
						
							|  |  |  | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh, attr); | 
					
						
							| 
									
										
										
										
											2019-02-16 17:19:46 +01:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |     switch (ecdh->var) { | 
					
						
							| 
									
										
										
										
											2019-02-16 17:19:46 +01:00
										 |  |  |         default: | 
					
						
							| 
									
										
										
										
											2023-04-18 10:38:24 +02:00
										 |  |  |             mbedtls_debug_printf_ecdh_internal(ssl, level, file, line, ecdh, | 
					
						
							|  |  |  |                                                attr); | 
					
						
							| 
									
										
										
										
											2019-02-16 17:19:46 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-09-24 20:04:06 -07:00
										 |  |  | #endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED &&
 | 
					
						
							|  |  |  |           MBEDTLS_ECDH_C */ | 
					
						
							| 
									
										
										
										
											2019-02-16 17:19:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-08 19:04:43 +01:00
										 |  |  | #endif /* MBEDTLS_DEBUG_C */
 |