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