| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | *  Debugging routines | 
|  | 3 | * | 
| Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | *  Copyright The Mbed TLS Contributors | 
| Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | *  SPDX-License-Identifier: Apache-2.0 | 
|  | 6 | * | 
|  | 7 | *  Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 8 | *  not use this file except in compliance with the License. | 
|  | 9 | *  You may obtain a copy of the License at | 
|  | 10 | * | 
|  | 11 | *  http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 12 | * | 
|  | 13 | *  Unless required by applicable law or agreed to in writing, software | 
|  | 14 | *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 15 | *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 16 | *  See the License for the specific language governing permissions and | 
|  | 17 | *  limitations under the License. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 18 | */ | 
|  | 19 |  | 
| Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 20 | #include "common.h" | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 21 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if defined(MBEDTLS_DEBUG_C) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 23 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 24 | #if defined(MBEDTLS_PLATFORM_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 25 | #include "mbedtls/platform.h" | 
| Rich Evans | 2387c7d | 2015-01-30 11:10:20 +0000 | [diff] [blame] | 26 | #else | 
| Manuel Pégourié-Gonnard | d23f593 | 2015-06-23 12:04:52 +0200 | [diff] [blame] | 27 | #include <stdlib.h> | 
|  | 28 | #define mbedtls_calloc      calloc | 
|  | 29 | #define mbedtls_free        free | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 30 | #define mbedtls_time_t      time_t | 
| Manuel Pégourié-Gonnard | d23f593 | 2015-06-23 12:04:52 +0200 | [diff] [blame] | 31 | #define mbedtls_snprintf    snprintf | 
| k-stachowiak | 723f867 | 2018-07-16 14:27:07 +0200 | [diff] [blame] | 32 | #define mbedtls_vsnprintf   vsnprintf | 
| Rich Evans | 2387c7d | 2015-01-30 11:10:20 +0000 | [diff] [blame] | 33 | #endif | 
|  | 34 |  | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 35 | #include "mbedtls/debug.h" | 
| Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 36 | #include "mbedtls/error.h" | 
| SimonB | d5800b7 | 2016-04-26 07:43:27 +0100 | [diff] [blame] | 37 |  | 
|  | 38 | #include <stdarg.h> | 
|  | 39 | #include <stdio.h> | 
|  | 40 | #include <string.h> | 
|  | 41 |  | 
| Manuel Pégourié-Gonnard | 0223ab9 | 2015-10-05 11:40:01 +0100 | [diff] [blame] | 42 | #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \ | 
|  | 43 | !defined(inline) && !defined(__cplusplus) | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 44 | #define inline __inline | 
|  | 45 | #endif | 
|  | 46 |  | 
| Manuel Pégourié-Gonnard | d23f593 | 2015-06-23 12:04:52 +0200 | [diff] [blame] | 47 | #define DEBUG_BUF_SIZE      512 | 
|  | 48 |  | 
| Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 49 | static int debug_threshold = 0; | 
| Paul Bakker | eaebbd5 | 2014-04-25 15:04:14 +0200 | [diff] [blame] | 50 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 51 | void mbedtls_debug_set_threshold( int threshold ) | 
| Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 52 | { | 
|  | 53 | debug_threshold = threshold; | 
|  | 54 | } | 
|  | 55 |  | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 56 | /* | 
|  | 57 | * All calls to f_dbg must be made via this function | 
|  | 58 | */ | 
|  | 59 | static inline void debug_send_line( const mbedtls_ssl_context *ssl, int level, | 
|  | 60 | const char *file, int line, | 
|  | 61 | const char *str ) | 
|  | 62 | { | 
|  | 63 | /* | 
|  | 64 | * If in a threaded environment, we need a thread identifier. | 
|  | 65 | * Since there is no portable way to get one, use the address of the ssl | 
|  | 66 | * context instead, as it shouldn't be shared between threads. | 
|  | 67 | */ | 
|  | 68 | #if defined(MBEDTLS_THREADING_C) | 
|  | 69 | char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */ | 
| Simon Butcher | 097618b | 2016-11-10 17:28:55 +0000 | [diff] [blame] | 70 | mbedtls_snprintf( idstr, sizeof( idstr ), "%p: %s", (void*)ssl, str ); | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 71 | ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, idstr ); | 
|  | 72 | #else | 
|  | 73 | ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str ); | 
|  | 74 | #endif | 
|  | 75 | } | 
|  | 76 |  | 
| Manuel Pégourié-Gonnard | a16e7c4 | 2015-06-29 20:14:19 +0200 | [diff] [blame] | 77 | void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level, | 
| Manuel Pégourié-Gonnard | b74c245 | 2015-06-29 20:08:23 +0200 | [diff] [blame] | 78 | const char *file, int line, | 
|  | 79 | const char *format, ... ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 80 | { | 
|  | 81 | va_list argp; | 
| Manuel Pégourié-Gonnard | b74c245 | 2015-06-29 20:08:23 +0200 | [diff] [blame] | 82 | char str[DEBUG_BUF_SIZE]; | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 83 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | d23f593 | 2015-06-23 12:04:52 +0200 | [diff] [blame] | 84 |  | 
| Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 85 | if( NULL == ssl              || | 
|  | 86 | NULL == ssl->conf        || | 
|  | 87 | NULL == ssl->conf->f_dbg || | 
|  | 88 | level > debug_threshold ) | 
|  | 89 | { | 
| Manuel Pégourié-Gonnard | b74c245 | 2015-06-29 20:08:23 +0200 | [diff] [blame] | 90 | return; | 
| Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 91 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 92 |  | 
|  | 93 | va_start( argp, format ); | 
| k-stachowiak | 723f867 | 2018-07-16 14:27:07 +0200 | [diff] [blame] | 94 | ret = mbedtls_vsnprintf( str, DEBUG_BUF_SIZE, format, argp ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 95 | va_end( argp ); | 
|  | 96 |  | 
| Manuel Pégourié-Gonnard | b74c245 | 2015-06-29 20:08:23 +0200 | [diff] [blame] | 97 | if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 ) | 
|  | 98 | { | 
|  | 99 | str[ret]     = '\n'; | 
|  | 100 | str[ret + 1] = '\0'; | 
|  | 101 | } | 
|  | 102 |  | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 103 | debug_send_line( ssl, level, file, line, str ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 104 | } | 
|  | 105 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 106 | void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level, | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 107 | const char *file, int line, | 
|  | 108 | const char *text, int ret ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 109 | { | 
| Manuel Pégourié-Gonnard | d23f593 | 2015-06-23 12:04:52 +0200 | [diff] [blame] | 110 | char str[DEBUG_BUF_SIZE]; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 111 |  | 
| Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 112 | if( NULL == ssl              || | 
|  | 113 | NULL == ssl->conf        || | 
|  | 114 | NULL == ssl->conf->f_dbg || | 
|  | 115 | level > debug_threshold ) | 
|  | 116 | { | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 117 | return; | 
| Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 118 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 119 |  | 
| Manuel Pégourié-Gonnard | 4cba1a7 | 2015-05-11 18:52:25 +0200 | [diff] [blame] | 120 | /* | 
|  | 121 | * With non-blocking I/O and examples that just retry immediately, | 
|  | 122 | * the logs would be quickly flooded with WANT_READ, so ignore that. | 
|  | 123 | * Don't ignore WANT_WRITE however, since is is usually rare. | 
|  | 124 | */ | 
|  | 125 | if( ret == MBEDTLS_ERR_SSL_WANT_READ ) | 
|  | 126 | return; | 
|  | 127 |  | 
| Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 128 | mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n", | 
| Kenneth Soerensen | 518d435 | 2020-04-01 17:22:45 +0200 | [diff] [blame] | 129 | text, ret, (unsigned int) -ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 130 |  | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 131 | debug_send_line( ssl, level, file, line, str ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 132 | } | 
|  | 133 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 134 | void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level, | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 135 | const char *file, int line, const char *text, | 
| Manuel Pégourié-Gonnard | a78b218 | 2015-03-19 17:16:11 +0000 | [diff] [blame] | 136 | const unsigned char *buf, size_t len ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 137 | { | 
| Manuel Pégourié-Gonnard | d23f593 | 2015-06-23 12:04:52 +0200 | [diff] [blame] | 138 | char str[DEBUG_BUF_SIZE]; | 
| Manuel Pégourié-Gonnard | 8c9223d | 2014-11-19 10:17:21 +0100 | [diff] [blame] | 139 | char txt[17]; | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 140 | size_t i, idx = 0; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 141 |  | 
| Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 142 | if( NULL == ssl              || | 
|  | 143 | NULL == ssl->conf        || | 
|  | 144 | NULL == ssl->conf->f_dbg || | 
|  | 145 | level > debug_threshold ) | 
|  | 146 | { | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 147 | return; | 
| Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 148 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 149 |  | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 150 | mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n", | 
| Paul Bakker | eaebbd5 | 2014-04-25 15:04:14 +0200 | [diff] [blame] | 151 | text, (unsigned int) len ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 152 |  | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 153 | debug_send_line( ssl, level, file, line, str ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 154 |  | 
| Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 155 | idx = 0; | 
| Manuel Pégourié-Gonnard | 8c9223d | 2014-11-19 10:17:21 +0100 | [diff] [blame] | 156 | memset( txt, 0, sizeof( txt ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 157 | for( i = 0; i < len; i++ ) | 
|  | 158 | { | 
|  | 159 | if( i >= 4096 ) | 
|  | 160 | break; | 
|  | 161 |  | 
|  | 162 | if( i % 16 == 0 ) | 
|  | 163 | { | 
|  | 164 | if( i > 0 ) | 
| Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 165 | { | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 166 | mbedtls_snprintf( str + idx, sizeof( str ) - idx, "  %s\n", txt ); | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 167 | debug_send_line( ssl, level, file, line, str ); | 
| Manuel Pégourié-Gonnard | 8c9223d | 2014-11-19 10:17:21 +0100 | [diff] [blame] | 168 |  | 
| Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 169 | idx = 0; | 
| Manuel Pégourié-Gonnard | 8c9223d | 2014-11-19 10:17:21 +0100 | [diff] [blame] | 170 | memset( txt, 0, sizeof( txt ) ); | 
| Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 171 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 172 |  | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 173 | idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ", | 
| Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 174 | (unsigned int) i ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 175 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 176 | } | 
|  | 177 |  | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 178 | idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", | 
| Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 179 | (unsigned int) buf[i] ); | 
| Manuel Pégourié-Gonnard | 8c9223d | 2014-11-19 10:17:21 +0100 | [diff] [blame] | 180 | txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 181 | } | 
|  | 182 |  | 
|  | 183 | if( len > 0 ) | 
| Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 184 | { | 
| Manuel Pégourié-Gonnard | 8c9223d | 2014-11-19 10:17:21 +0100 | [diff] [blame] | 185 | for( /* i = i */; i % 16 != 0; i++ ) | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 186 | idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "   " ); | 
| Manuel Pégourié-Gonnard | 8c9223d | 2014-11-19 10:17:21 +0100 | [diff] [blame] | 187 |  | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 188 | mbedtls_snprintf( str + idx, sizeof( str ) - idx, "  %s\n", txt ); | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 189 | debug_send_line( ssl, level, file, line, str ); | 
| Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 190 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 191 | } | 
|  | 192 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 193 | #if defined(MBEDTLS_ECP_C) | 
|  | 194 | void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level, | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 195 | const char *file, int line, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 196 | const char *text, const mbedtls_ecp_point *X ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 197 | { | 
| Manuel Pégourié-Gonnard | d23f593 | 2015-06-23 12:04:52 +0200 | [diff] [blame] | 198 | char str[DEBUG_BUF_SIZE]; | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 199 |  | 
| Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 200 | if( NULL == ssl              || | 
|  | 201 | NULL == ssl->conf        || | 
|  | 202 | NULL == ssl->conf->f_dbg || | 
|  | 203 | level > debug_threshold ) | 
|  | 204 | { | 
| Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 205 | return; | 
| Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 206 | } | 
| Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 207 |  | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 208 | mbedtls_snprintf( str, sizeof( str ), "%s(X)", text ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 209 | mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 210 |  | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 211 | mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 212 | mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 213 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 214 | #endif /* MBEDTLS_ECP_C */ | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 215 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 216 | #if defined(MBEDTLS_BIGNUM_C) | 
|  | 217 | void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level, | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 218 | const char *file, int line, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 219 | const char *text, const mbedtls_mpi *X ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 220 | { | 
| Manuel Pégourié-Gonnard | d23f593 | 2015-06-23 12:04:52 +0200 | [diff] [blame] | 221 | char str[DEBUG_BUF_SIZE]; | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 222 | int j, k, zeros = 1; | 
| Paul Bakker | eaebbd5 | 2014-04-25 15:04:14 +0200 | [diff] [blame] | 223 | size_t i, n, idx = 0; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 224 |  | 
| Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 225 | if( NULL == ssl              || | 
|  | 226 | NULL == ssl->conf        || | 
|  | 227 | NULL == ssl->conf->f_dbg || | 
|  | 228 | NULL == X                || | 
|  | 229 | level > debug_threshold ) | 
|  | 230 | { | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 231 | return; | 
| Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 232 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 233 |  | 
| Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 234 | for( n = X->n - 1; n > 0; n-- ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 235 | if( X->p[n] != 0 ) | 
|  | 236 | break; | 
|  | 237 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 238 | for( j = ( sizeof(mbedtls_mpi_uint) << 3 ) - 1; j >= 0; j-- ) | 
| Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 239 | if( ( ( X->p[n] >> j ) & 1 ) != 0 ) | 
|  | 240 | break; | 
|  | 241 |  | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 242 | mbedtls_snprintf( str + idx, sizeof( str ) - idx, "value of '%s' (%d bits) is:\n", | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 243 | text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 244 |  | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 245 | debug_send_line( ssl, level, file, line, str ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 246 |  | 
| Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 247 | idx = 0; | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 248 | for( i = n + 1, j = 0; i > 0; i-- ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 249 | { | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 250 | if( zeros && X->p[i - 1] == 0 ) | 
| Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 251 | continue; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 252 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 253 | for( k = sizeof( mbedtls_mpi_uint ) - 1; k >= 0; k-- ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 254 | { | 
| Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 255 | if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 ) | 
| Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 256 | continue; | 
|  | 257 | else | 
|  | 258 | zeros = 0; | 
|  | 259 |  | 
|  | 260 | if( j % 16 == 0 ) | 
|  | 261 | { | 
|  | 262 | if( j > 0 ) | 
| Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 263 | { | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 264 | mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" ); | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 265 | debug_send_line( ssl, level, file, line, str ); | 
| Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 266 | idx = 0; | 
|  | 267 | } | 
| Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 268 | } | 
|  | 269 |  | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 270 | idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", (unsigned int) | 
| Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 271 | ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ); | 
| Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 272 |  | 
|  | 273 | j++; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 274 | } | 
| Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 275 |  | 
|  | 276 | } | 
|  | 277 |  | 
|  | 278 | if( zeros == 1 ) | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 279 | idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " 00" ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 280 |  | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 281 | mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" ); | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 282 | debug_send_line( ssl, level, file, line, str ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 283 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 284 | #endif /* MBEDTLS_BIGNUM_C */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 285 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 286 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 287 | static void debug_print_pk( const mbedtls_ssl_context *ssl, int level, | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 288 | const char *file, int line, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 289 | const char *text, const mbedtls_pk_context *pk ) | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 290 | { | 
|  | 291 | size_t i; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 292 | mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS]; | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 293 | char name[16]; | 
|  | 294 |  | 
|  | 295 | memset( items, 0, sizeof( items ) ); | 
|  | 296 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 297 | if( mbedtls_pk_debug( pk, items ) != 0 ) | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 298 | { | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 299 | debug_send_line( ssl, level, file, line, | 
| Manuel Pégourié-Gonnard | 80d627a | 2015-06-29 20:12:51 +0200 | [diff] [blame] | 300 | "invalid PK context\n" ); | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 301 | return; | 
|  | 302 | } | 
|  | 303 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 304 | for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ ) | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 305 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 306 | if( items[i].type == MBEDTLS_PK_DEBUG_NONE ) | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 307 | return; | 
|  | 308 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 309 | mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name ); | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 310 | name[sizeof( name ) - 1] = '\0'; | 
|  | 311 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 312 | if( items[i].type == MBEDTLS_PK_DEBUG_MPI ) | 
|  | 313 | mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value ); | 
| Manuel Pégourié-Gonnard | bac0e3b | 2013-10-15 11:54:47 +0200 | [diff] [blame] | 314 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 315 | #if defined(MBEDTLS_ECP_C) | 
|  | 316 | if( items[i].type == MBEDTLS_PK_DEBUG_ECP ) | 
|  | 317 | mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value ); | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 318 | else | 
| Manuel Pégourié-Gonnard | bac0e3b | 2013-10-15 11:54:47 +0200 | [diff] [blame] | 319 | #endif | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 320 | debug_send_line( ssl, level, file, line, | 
| Manuel Pégourié-Gonnard | 80d627a | 2015-06-29 20:12:51 +0200 | [diff] [blame] | 321 | "should not happen\n" ); | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 322 | } | 
|  | 323 | } | 
|  | 324 |  | 
| Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 325 | static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level, | 
|  | 326 | const char *file, int line, const char *text ) | 
|  | 327 | { | 
|  | 328 | char str[DEBUG_BUF_SIZE]; | 
|  | 329 | const char *start, *cur; | 
|  | 330 |  | 
|  | 331 | start = text; | 
|  | 332 | for( cur = text; *cur != '\0'; cur++ ) | 
|  | 333 | { | 
|  | 334 | if( *cur == '\n' ) | 
|  | 335 | { | 
|  | 336 | size_t len = cur - start + 1; | 
|  | 337 | if( len > DEBUG_BUF_SIZE - 1 ) | 
|  | 338 | len = DEBUG_BUF_SIZE - 1; | 
|  | 339 |  | 
|  | 340 | memcpy( str, start, len ); | 
|  | 341 | str[len] = '\0'; | 
|  | 342 |  | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 343 | debug_send_line( ssl, level, file, line, str ); | 
| Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 344 |  | 
|  | 345 | start = cur + 1; | 
|  | 346 | } | 
|  | 347 | } | 
|  | 348 | } | 
|  | 349 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 350 | void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level, | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 351 | const char *file, int line, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 352 | const char *text, const mbedtls_x509_crt *crt ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 353 | { | 
| Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 354 | char str[DEBUG_BUF_SIZE]; | 
|  | 355 | int i = 0; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 356 |  | 
| Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 357 | if( NULL == ssl              || | 
|  | 358 | NULL == ssl->conf        || | 
|  | 359 | NULL == ssl->conf->f_dbg || | 
|  | 360 | NULL == crt              || | 
|  | 361 | level > debug_threshold ) | 
|  | 362 | { | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 363 | return; | 
| Hanno Becker | 687c500 | 2018-06-29 09:04:46 +0100 | [diff] [blame] | 364 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 365 |  | 
| Paul Bakker | 2908713 | 2010-03-21 21:03:34 +0000 | [diff] [blame] | 366 | while( crt != NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 367 | { | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 368 | char buf[1024]; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 369 |  | 
| Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 370 | mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i ); | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 371 | debug_send_line( ssl, level, file, line, str ); | 
| Paul Bakker | eaebbd5 | 2014-04-25 15:04:14 +0200 | [diff] [blame] | 372 |  | 
| Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 373 | mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt ); | 
|  | 374 | debug_print_line_by_line( ssl, level, file, line, buf ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 375 |  | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 376 | debug_print_pk( ssl, level, file, line, "crt->", &crt->pk ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 377 |  | 
|  | 378 | crt = crt->next; | 
|  | 379 | } | 
|  | 380 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 381 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 382 |  | 
| Janos Follath | 948f4be | 2018-08-22 01:37:55 +0100 | [diff] [blame] | 383 | #if defined(MBEDTLS_ECDH_C) | 
|  | 384 | static void mbedtls_debug_printf_ecdh_internal( const mbedtls_ssl_context *ssl, | 
|  | 385 | int level, const char *file, | 
|  | 386 | int line, | 
|  | 387 | const mbedtls_ecdh_context *ecdh, | 
|  | 388 | mbedtls_debug_ecdh_attr attr ) | 
|  | 389 | { | 
|  | 390 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) | 
|  | 391 | const mbedtls_ecdh_context* ctx = ecdh; | 
|  | 392 | #else | 
|  | 393 | const mbedtls_ecdh_context_mbed* ctx = &ecdh->ctx.mbed_ecdh; | 
|  | 394 | #endif | 
|  | 395 |  | 
|  | 396 | switch( attr ) | 
|  | 397 | { | 
|  | 398 | case MBEDTLS_DEBUG_ECDH_Q: | 
|  | 399 | mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Q", | 
|  | 400 | &ctx->Q ); | 
|  | 401 | break; | 
|  | 402 | case MBEDTLS_DEBUG_ECDH_QP: | 
|  | 403 | mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Qp", | 
|  | 404 | &ctx->Qp ); | 
|  | 405 | break; | 
|  | 406 | case MBEDTLS_DEBUG_ECDH_Z: | 
|  | 407 | mbedtls_debug_print_mpi( ssl, level, file, line, "ECDH: z", | 
|  | 408 | &ctx->z ); | 
|  | 409 | break; | 
|  | 410 | default: | 
|  | 411 | break; | 
|  | 412 | } | 
|  | 413 | } | 
|  | 414 |  | 
|  | 415 | void mbedtls_debug_printf_ecdh( const mbedtls_ssl_context *ssl, int level, | 
|  | 416 | const char *file, int line, | 
|  | 417 | const mbedtls_ecdh_context *ecdh, | 
|  | 418 | mbedtls_debug_ecdh_attr attr ) | 
|  | 419 | { | 
|  | 420 | #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) | 
|  | 421 | mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh, attr ); | 
|  | 422 | #else | 
|  | 423 | switch( ecdh->var ) | 
|  | 424 | { | 
|  | 425 | default: | 
|  | 426 | mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh, | 
|  | 427 | attr ); | 
|  | 428 | } | 
|  | 429 | #endif | 
|  | 430 | } | 
|  | 431 | #endif /* MBEDTLS_ECDH_C */ | 
|  | 432 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 433 | #endif /* MBEDTLS_DEBUG_C */ |