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