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