| 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 | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 30 | #include "mbedtls/debug.h" | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 31 |  | 
|  | 32 | #include <stdarg.h> | 
| Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 33 | #include <stdio.h> | 
| Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 34 | #include <string.h> | 
| Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 35 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 36 | #if defined(MBEDTLS_PLATFORM_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 37 | #include "mbedtls/platform.h" | 
| Rich Evans | 2387c7d | 2015-01-30 11:10:20 +0000 | [diff] [blame] | 38 | #else | 
| Manuel Pégourié-Gonnard | d23f593 | 2015-06-23 12:04:52 +0200 | [diff] [blame] | 39 | #include <stdlib.h> | 
|  | 40 | #define mbedtls_calloc      calloc | 
|  | 41 | #define mbedtls_free        free | 
|  | 42 | #define mbedtls_snprintf    snprintf | 
| Rich Evans | 2387c7d | 2015-01-30 11:10:20 +0000 | [diff] [blame] | 43 | #endif | 
|  | 44 |  | 
| Manuel Pégourié-Gonnard | 0223ab9 | 2015-10-05 11:40:01 +0100 | [diff] [blame] | 45 | #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \ | 
|  | 46 | !defined(inline) && !defined(__cplusplus) | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 47 | #define inline __inline | 
|  | 48 | #endif | 
|  | 49 |  | 
| Manuel Pégourié-Gonnard | d23f593 | 2015-06-23 12:04:52 +0200 | [diff] [blame] | 50 | #define DEBUG_BUF_SIZE      512 | 
|  | 51 |  | 
| Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 52 | static int debug_threshold = 0; | 
| Paul Bakker | eaebbd5 | 2014-04-25 15:04:14 +0200 | [diff] [blame] | 53 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 54 | void mbedtls_debug_set_threshold( int threshold ) | 
| Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 55 | { | 
|  | 56 | debug_threshold = threshold; | 
|  | 57 | } | 
|  | 58 |  | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 59 | /* | 
|  | 60 | * All calls to f_dbg must be made via this function | 
|  | 61 | */ | 
|  | 62 | static inline void debug_send_line( const mbedtls_ssl_context *ssl, int level, | 
|  | 63 | const char *file, int line, | 
|  | 64 | const char *str ) | 
|  | 65 | { | 
|  | 66 | /* | 
|  | 67 | * If in a threaded environment, we need a thread identifier. | 
|  | 68 | * Since there is no portable way to get one, use the address of the ssl | 
|  | 69 | * context instead, as it shouldn't be shared between threads. | 
|  | 70 | */ | 
|  | 71 | #if defined(MBEDTLS_THREADING_C) | 
|  | 72 | char idstr[20 + DEBUG_BUF_SIZE]; /* 0x + 16 nibbles + ': ' */ | 
|  | 73 | mbedtls_snprintf( idstr, sizeof( idstr ), "%p: %s", ssl, str ); | 
|  | 74 | ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, idstr ); | 
|  | 75 | #else | 
|  | 76 | ssl->conf->f_dbg( ssl->conf->p_dbg, level, file, line, str ); | 
|  | 77 | #endif | 
|  | 78 | } | 
|  | 79 |  | 
| Manuel Pégourié-Gonnard | a16e7c4 | 2015-06-29 20:14:19 +0200 | [diff] [blame] | 80 | 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] | 81 | const char *file, int line, | 
|  | 82 | const char *format, ... ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 83 | { | 
|  | 84 | va_list argp; | 
| Manuel Pégourié-Gonnard | b74c245 | 2015-06-29 20:08:23 +0200 | [diff] [blame] | 85 | char str[DEBUG_BUF_SIZE]; | 
|  | 86 | int ret; | 
| Manuel Pégourié-Gonnard | d23f593 | 2015-06-23 12:04:52 +0200 | [diff] [blame] | 87 |  | 
| Manuel Pégourié-Gonnard | b74c245 | 2015-06-29 20:08:23 +0200 | [diff] [blame] | 88 | if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold ) | 
|  | 89 | return; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 90 |  | 
|  | 91 | va_start( argp, format ); | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 92 | #if defined(_WIN32) | 
| Manuel Pégourié-Gonnard | a4f055f | 2015-07-08 16:46:13 +0200 | [diff] [blame] | 93 | #if defined(_TRUNCATE) | 
| Manuel Pégourié-Gonnard | b74c245 | 2015-06-29 20:08:23 +0200 | [diff] [blame] | 94 | ret = _vsnprintf_s( str, DEBUG_BUF_SIZE, _TRUNCATE, format, argp ); | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 95 | #else | 
| Manuel Pégourié-Gonnard | a4f055f | 2015-07-08 16:46:13 +0200 | [diff] [blame] | 96 | ret = _vsnprintf( str, DEBUG_BUF_SIZE, format, argp ); | 
|  | 97 | if( ret < 0 || (size_t) ret == DEBUG_BUF_SIZE ) | 
|  | 98 | { | 
|  | 99 | str[DEBUG_BUF_SIZE-1] = '\0'; | 
|  | 100 | ret = -1; | 
|  | 101 | } | 
|  | 102 | #endif | 
|  | 103 | #else | 
| Manuel Pégourié-Gonnard | b74c245 | 2015-06-29 20:08:23 +0200 | [diff] [blame] | 104 | ret = vsnprintf( str, DEBUG_BUF_SIZE, format, argp ); | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 105 | #endif | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 106 | va_end( argp ); | 
|  | 107 |  | 
| Manuel Pégourié-Gonnard | b74c245 | 2015-06-29 20:08:23 +0200 | [diff] [blame] | 108 | if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 ) | 
|  | 109 | { | 
|  | 110 | str[ret]     = '\n'; | 
|  | 111 | str[ret + 1] = '\0'; | 
|  | 112 | } | 
|  | 113 |  | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 114 | debug_send_line( ssl, level, file, line, str ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 115 | } | 
|  | 116 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 117 | void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level, | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 118 | const char *file, int line, | 
|  | 119 | const char *text, int ret ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 120 | { | 
| Manuel Pégourié-Gonnard | d23f593 | 2015-06-23 12:04:52 +0200 | [diff] [blame] | 121 | char str[DEBUG_BUF_SIZE]; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 122 |  | 
| Manuel Pégourié-Gonnard | 49f5eb9 | 2015-05-12 12:19:09 +0200 | [diff] [blame] | 123 | if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 124 | return; | 
|  | 125 |  | 
| Manuel Pégourié-Gonnard | 4cba1a7 | 2015-05-11 18:52:25 +0200 | [diff] [blame] | 126 | /* | 
|  | 127 | * With non-blocking I/O and examples that just retry immediately, | 
|  | 128 | * the logs would be quickly flooded with WANT_READ, so ignore that. | 
|  | 129 | * Don't ignore WANT_WRITE however, since is is usually rare. | 
|  | 130 | */ | 
|  | 131 | if( ret == MBEDTLS_ERR_SSL_WANT_READ ) | 
|  | 132 | return; | 
|  | 133 |  | 
| Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 134 | mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n", | 
| Paul Bakker | eaebbd5 | 2014-04-25 15:04:14 +0200 | [diff] [blame] | 135 | text, ret, -ret ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 136 |  | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 137 | debug_send_line( ssl, level, file, line, str ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 138 | } | 
|  | 139 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 140 | void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level, | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 141 | const char *file, int line, const char *text, | 
| Manuel Pégourié-Gonnard | a78b218 | 2015-03-19 17:16:11 +0000 | [diff] [blame] | 142 | const unsigned char *buf, size_t len ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 143 | { | 
| Manuel Pégourié-Gonnard | d23f593 | 2015-06-23 12:04:52 +0200 | [diff] [blame] | 144 | char str[DEBUG_BUF_SIZE]; | 
| Manuel Pégourié-Gonnard | 8c9223d | 2014-11-19 10:17:21 +0100 | [diff] [blame] | 145 | char txt[17]; | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 146 | size_t i, idx = 0; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 147 |  | 
| Manuel Pégourié-Gonnard | 49f5eb9 | 2015-05-12 12:19:09 +0200 | [diff] [blame] | 148 | if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 149 | return; | 
|  | 150 |  | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 151 | mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n", | 
| Paul Bakker | eaebbd5 | 2014-04-25 15:04:14 +0200 | [diff] [blame] | 152 | text, (unsigned int) len ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 153 |  | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 154 | debug_send_line( ssl, level, file, line, str ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 155 |  | 
| Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 156 | idx = 0; | 
| Manuel Pégourié-Gonnard | 8c9223d | 2014-11-19 10:17:21 +0100 | [diff] [blame] | 157 | memset( txt, 0, sizeof( txt ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 158 | for( i = 0; i < len; i++ ) | 
|  | 159 | { | 
|  | 160 | if( i >= 4096 ) | 
|  | 161 | break; | 
|  | 162 |  | 
|  | 163 | if( i % 16 == 0 ) | 
|  | 164 | { | 
|  | 165 | if( i > 0 ) | 
| Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [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, "  %s\n", txt ); | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 168 | debug_send_line( ssl, level, file, line, str ); | 
| Manuel Pégourié-Gonnard | 8c9223d | 2014-11-19 10:17:21 +0100 | [diff] [blame] | 169 |  | 
| Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 170 | idx = 0; | 
| Manuel Pégourié-Gonnard | 8c9223d | 2014-11-19 10:17:21 +0100 | [diff] [blame] | 171 | memset( txt, 0, sizeof( txt ) ); | 
| Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 172 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 173 |  | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 174 | idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ", | 
| Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 175 | (unsigned int) i ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 176 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 177 | } | 
|  | 178 |  | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 179 | idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", | 
| Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 180 | (unsigned int) buf[i] ); | 
| Manuel Pégourié-Gonnard | 8c9223d | 2014-11-19 10:17:21 +0100 | [diff] [blame] | 181 | txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 182 | } | 
|  | 183 |  | 
|  | 184 | if( len > 0 ) | 
| Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 185 | { | 
| Manuel Pégourié-Gonnard | 8c9223d | 2014-11-19 10:17:21 +0100 | [diff] [blame] | 186 | for( /* i = i */; i % 16 != 0; i++ ) | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 187 | idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "   " ); | 
| Manuel Pégourié-Gonnard | 8c9223d | 2014-11-19 10:17:21 +0100 | [diff] [blame] | 188 |  | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 189 | mbedtls_snprintf( str + idx, sizeof( str ) - idx, "  %s\n", txt ); | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 190 | debug_send_line( ssl, level, file, line, str ); | 
| Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 191 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 192 | } | 
|  | 193 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 194 | #if defined(MBEDTLS_ECP_C) | 
|  | 195 | void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level, | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 196 | const char *file, int line, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 197 | const char *text, const mbedtls_ecp_point *X ) | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 198 | { | 
| Manuel Pégourié-Gonnard | d23f593 | 2015-06-23 12:04:52 +0200 | [diff] [blame] | 199 | char str[DEBUG_BUF_SIZE]; | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 200 |  | 
| Manuel Pégourié-Gonnard | 49f5eb9 | 2015-05-12 12:19:09 +0200 | [diff] [blame] | 201 | if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold ) | 
| Paul Bakker | c73079a | 2014-04-25 16:34:30 +0200 | [diff] [blame] | 202 | return; | 
|  | 203 |  | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 204 | mbedtls_snprintf( str, sizeof( str ), "%s(X)", text ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 205 | mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 206 |  | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 207 | mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 208 | mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y ); | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 209 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 210 | #endif /* MBEDTLS_ECP_C */ | 
| Paul Bakker | 41c83d3 | 2013-03-20 14:39:14 +0100 | [diff] [blame] | 211 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 212 | #if defined(MBEDTLS_BIGNUM_C) | 
|  | 213 | void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level, | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 214 | const char *file, int line, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 215 | const char *text, const mbedtls_mpi *X ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 216 | { | 
| Manuel Pégourié-Gonnard | d23f593 | 2015-06-23 12:04:52 +0200 | [diff] [blame] | 217 | char str[DEBUG_BUF_SIZE]; | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 218 | int j, k, zeros = 1; | 
| Paul Bakker | eaebbd5 | 2014-04-25 15:04:14 +0200 | [diff] [blame] | 219 | size_t i, n, idx = 0; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 220 |  | 
| Manuel Pégourié-Gonnard | 49f5eb9 | 2015-05-12 12:19:09 +0200 | [diff] [blame] | 221 | 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] | 222 | return; | 
|  | 223 |  | 
| Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 224 | for( n = X->n - 1; n > 0; n-- ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 225 | if( X->p[n] != 0 ) | 
|  | 226 | break; | 
|  | 227 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 228 | for( j = ( sizeof(mbedtls_mpi_uint) << 3 ) - 1; j >= 0; j-- ) | 
| Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 229 | if( ( ( X->p[n] >> j ) & 1 ) != 0 ) | 
|  | 230 | break; | 
|  | 231 |  | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 232 | 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] | 233 | text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 234 |  | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 235 | debug_send_line( ssl, level, file, line, str ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 236 |  | 
| Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 237 | idx = 0; | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 238 | for( i = n + 1, j = 0; i > 0; i-- ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 239 | { | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 240 | if( zeros && X->p[i - 1] == 0 ) | 
| Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 241 | continue; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 242 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 243 | for( k = sizeof( mbedtls_mpi_uint ) - 1; k >= 0; k-- ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 244 | { | 
| Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 245 | if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 ) | 
| Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 246 | continue; | 
|  | 247 | else | 
|  | 248 | zeros = 0; | 
|  | 249 |  | 
|  | 250 | if( j % 16 == 0 ) | 
|  | 251 | { | 
|  | 252 | if( j > 0 ) | 
| Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 253 | { | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 254 | mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" ); | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 255 | debug_send_line( ssl, level, file, line, str ); | 
| Paul Bakker | 92478c3 | 2014-04-25 15:18:34 +0200 | [diff] [blame] | 256 | idx = 0; | 
|  | 257 | } | 
| Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 258 | } | 
|  | 259 |  | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 260 | idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", (unsigned int) | 
| Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 261 | ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ); | 
| Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 262 |  | 
|  | 263 | j++; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 264 | } | 
| Paul Bakker | be4e7dc | 2011-03-14 20:41:31 +0000 | [diff] [blame] | 265 |  | 
|  | 266 | } | 
|  | 267 |  | 
|  | 268 | if( zeros == 1 ) | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 269 | idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " 00" ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 270 |  | 
| Manuel Pégourié-Gonnard | 9dbaf40 | 2015-06-22 11:50:58 +0200 | [diff] [blame] | 271 | mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" ); | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 272 | debug_send_line( ssl, level, file, line, str ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 273 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 274 | #endif /* MBEDTLS_BIGNUM_C */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 275 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 276 | #if defined(MBEDTLS_X509_CRT_PARSE_C) | 
|  | 277 | 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] | 278 | const char *file, int line, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 279 | const char *text, const mbedtls_pk_context *pk ) | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 280 | { | 
|  | 281 | size_t i; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 282 | mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS]; | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 283 | char name[16]; | 
|  | 284 |  | 
|  | 285 | memset( items, 0, sizeof( items ) ); | 
|  | 286 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 287 | if( mbedtls_pk_debug( pk, items ) != 0 ) | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 288 | { | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 289 | debug_send_line( ssl, level, file, line, | 
| Manuel Pégourié-Gonnard | 80d627a | 2015-06-29 20:12:51 +0200 | [diff] [blame] | 290 | "invalid PK context\n" ); | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 291 | return; | 
|  | 292 | } | 
|  | 293 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 294 | for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ ) | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 295 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 296 | if( items[i].type == MBEDTLS_PK_DEBUG_NONE ) | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 297 | return; | 
|  | 298 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 299 | 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] | 300 | name[sizeof( name ) - 1] = '\0'; | 
|  | 301 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 302 | if( items[i].type == MBEDTLS_PK_DEBUG_MPI ) | 
|  | 303 | 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] | 304 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 305 | #if defined(MBEDTLS_ECP_C) | 
|  | 306 | if( items[i].type == MBEDTLS_PK_DEBUG_ECP ) | 
|  | 307 | 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] | 308 | else | 
| Manuel Pégourié-Gonnard | bac0e3b | 2013-10-15 11:54:47 +0200 | [diff] [blame] | 309 | #endif | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 310 | debug_send_line( ssl, level, file, line, | 
| Manuel Pégourié-Gonnard | 80d627a | 2015-06-29 20:12:51 +0200 | [diff] [blame] | 311 | "should not happen\n" ); | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 312 | } | 
|  | 313 | } | 
|  | 314 |  | 
| Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 315 | static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level, | 
|  | 316 | const char *file, int line, const char *text ) | 
|  | 317 | { | 
|  | 318 | char str[DEBUG_BUF_SIZE]; | 
|  | 319 | const char *start, *cur; | 
|  | 320 |  | 
|  | 321 | start = text; | 
|  | 322 | for( cur = text; *cur != '\0'; cur++ ) | 
|  | 323 | { | 
|  | 324 | if( *cur == '\n' ) | 
|  | 325 | { | 
|  | 326 | size_t len = cur - start + 1; | 
|  | 327 | if( len > DEBUG_BUF_SIZE - 1 ) | 
|  | 328 | len = DEBUG_BUF_SIZE - 1; | 
|  | 329 |  | 
|  | 330 | memcpy( str, start, len ); | 
|  | 331 | str[len] = '\0'; | 
|  | 332 |  | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 333 | debug_send_line( ssl, level, file, line, str ); | 
| Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 334 |  | 
|  | 335 | start = cur + 1; | 
|  | 336 | } | 
|  | 337 | } | 
|  | 338 | } | 
|  | 339 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 340 | void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level, | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 341 | const char *file, int line, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 342 | const char *text, const mbedtls_x509_crt *crt ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 343 | { | 
| Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 344 | char str[DEBUG_BUF_SIZE]; | 
|  | 345 | int i = 0; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 346 |  | 
| Manuel Pégourié-Gonnard | 49f5eb9 | 2015-05-12 12:19:09 +0200 | [diff] [blame] | 347 | 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] | 348 | return; | 
|  | 349 |  | 
| Paul Bakker | 2908713 | 2010-03-21 21:03:34 +0000 | [diff] [blame] | 350 | while( crt != NULL ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 351 | { | 
| Paul Bakker | d98030e | 2009-05-02 15:13:40 +0000 | [diff] [blame] | 352 | char buf[1024]; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 353 |  | 
| Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 354 | mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i ); | 
| Manuel Pégourié-Gonnard | 7b23c51 | 2015-08-31 16:11:00 +0200 | [diff] [blame] | 355 | debug_send_line( ssl, level, file, line, str ); | 
| Paul Bakker | eaebbd5 | 2014-04-25 15:04:14 +0200 | [diff] [blame] | 356 |  | 
| Manuel Pégourié-Gonnard | fd47423 | 2015-06-23 16:34:24 +0200 | [diff] [blame] | 357 | mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt ); | 
|  | 358 | debug_print_line_by_line( ssl, level, file, line, buf ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 359 |  | 
| Manuel Pégourié-Gonnard | c6ac887 | 2013-08-14 18:04:18 +0200 | [diff] [blame] | 360 | debug_print_pk( ssl, level, file, line, "crt->", &crt->pk ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 361 |  | 
|  | 362 | crt = crt->next; | 
|  | 363 | } | 
|  | 364 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 365 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 366 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 367 | #endif /* MBEDTLS_DEBUG_C */ |