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