This Trac instance is not used for development anymore!

We migrated our development workflow to git and Gitea.
To test the future redirection, replace trac by ariadne in the page URL.

source: ps/trunk/build/premake/premake5/contrib/mbedtls/library/debug.c

Last change on this file was 20366, checked in by Itms, 7 years ago

Alpha 12 version of Premake 5, including prebuilt binary for Windows.
Directly taken from https://premake.github.io/.

Refs #3729.

File size: 10.7 KB
Line 
1/*
2 * Debugging routines
3 *
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_DEBUG_C)
29
30#include "mbedtls/debug.h"
31
32#include <stdarg.h>
33#include <stdio.h>
34#include <string.h>
35
36#if defined(MBEDTLS_PLATFORM_C)
37#include "mbedtls/platform.h"
38#else
39#include <stdlib.h>
40#define mbedtls_calloc calloc
41#define mbedtls_free free
42#define mbedtls_snprintf snprintf
43#endif
44
45#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
46 !defined(inline) && !defined(__cplusplus)
47#define inline __inline
48#endif
49
50#define DEBUG_BUF_SIZE 512
51
52static int debug_threshold = 0;
53
54void mbedtls_debug_set_threshold( int threshold )
55{
56 debug_threshold = threshold;
57}
58
59/*
60 * All calls to f_dbg must be made via this function
61 */
62static 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
80void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
81 const char *file, int line,
82 const char *format, ... )
83{
84 va_list argp;
85 char str[DEBUG_BUF_SIZE];
86 int ret;
87
88 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
89 return;
90
91 va_start( argp, format );
92#if defined(_WIN32)
93#if defined(_TRUNCATE)
94 ret = _vsnprintf_s( str, DEBUG_BUF_SIZE, _TRUNCATE, format, argp );
95#else
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
104 ret = vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
105#endif
106 va_end( argp );
107
108 if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 )
109 {
110 str[ret] = '\n';
111 str[ret + 1] = '\0';
112 }
113
114 debug_send_line( ssl, level, file, line, str );
115}
116
117void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
118 const char *file, int line,
119 const char *text, int ret )
120{
121 char str[DEBUG_BUF_SIZE];
122
123 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
124 return;
125
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
134 mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n",
135 text, ret, -ret );
136
137 debug_send_line( ssl, level, file, line, str );
138}
139
140void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
141 const char *file, int line, const char *text,
142 const unsigned char *buf, size_t len )
143{
144 char str[DEBUG_BUF_SIZE];
145 char txt[17];
146 size_t i, idx = 0;
147
148 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
149 return;
150
151 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "dumping '%s' (%u bytes)\n",
152 text, (unsigned int) len );
153
154 debug_send_line( ssl, level, file, line, str );
155
156 idx = 0;
157 memset( txt, 0, sizeof( txt ) );
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 )
166 {
167 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
168 debug_send_line( ssl, level, file, line, str );
169
170 idx = 0;
171 memset( txt, 0, sizeof( txt ) );
172 }
173
174 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, "%04x: ",
175 (unsigned int) i );
176
177 }
178
179 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x",
180 (unsigned int) buf[i] );
181 txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
182 }
183
184 if( len > 0 )
185 {
186 for( /* i = i */; i % 16 != 0; i++ )
187 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " " );
188
189 mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %s\n", txt );
190 debug_send_line( ssl, level, file, line, str );
191 }
192}
193
194#if defined(MBEDTLS_ECP_C)
195void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
196 const char *file, int line,
197 const char *text, const mbedtls_ecp_point *X )
198{
199 char str[DEBUG_BUF_SIZE];
200
201 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || level > debug_threshold )
202 return;
203
204 mbedtls_snprintf( str, sizeof( str ), "%s(X)", text );
205 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
206
207 mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text );
208 mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
209}
210#endif /* MBEDTLS_ECP_C */
211
212#if defined(MBEDTLS_BIGNUM_C)
213void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
214 const char *file, int line,
215 const char *text, const mbedtls_mpi *X )
216{
217 char str[DEBUG_BUF_SIZE];
218 int j, k, zeros = 1;
219 size_t i, n, idx = 0;
220
221 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || X == NULL || level > debug_threshold )
222 return;
223
224 for( n = X->n - 1; n > 0; n-- )
225 if( X->p[n] != 0 )
226 break;
227
228 for( j = ( sizeof(mbedtls_mpi_uint) << 3 ) - 1; j >= 0; j-- )
229 if( ( ( X->p[n] >> j ) & 1 ) != 0 )
230 break;
231
232 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "value of '%s' (%d bits) is:\n",
233 text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) );
234
235 debug_send_line( ssl, level, file, line, str );
236
237 idx = 0;
238 for( i = n + 1, j = 0; i > 0; i-- )
239 {
240 if( zeros && X->p[i - 1] == 0 )
241 continue;
242
243 for( k = sizeof( mbedtls_mpi_uint ) - 1; k >= 0; k-- )
244 {
245 if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
246 continue;
247 else
248 zeros = 0;
249
250 if( j % 16 == 0 )
251 {
252 if( j > 0 )
253 {
254 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
255 debug_send_line( ssl, level, file, line, str );
256 idx = 0;
257 }
258 }
259
260 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", (unsigned int)
261 ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
262
263 j++;
264 }
265
266 }
267
268 if( zeros == 1 )
269 idx += mbedtls_snprintf( str + idx, sizeof( str ) - idx, " 00" );
270
271 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
272 debug_send_line( ssl, level, file, line, str );
273}
274#endif /* MBEDTLS_BIGNUM_C */
275
276#if defined(MBEDTLS_X509_CRT_PARSE_C)
277static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
278 const char *file, int line,
279 const char *text, const mbedtls_pk_context *pk )
280{
281 size_t i;
282 mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
283 char name[16];
284
285 memset( items, 0, sizeof( items ) );
286
287 if( mbedtls_pk_debug( pk, items ) != 0 )
288 {
289 debug_send_line( ssl, level, file, line,
290 "invalid PK context\n" );
291 return;
292 }
293
294 for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
295 {
296 if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
297 return;
298
299 mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
300 name[sizeof( name ) - 1] = '\0';
301
302 if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
303 mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
304 else
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 );
308 else
309#endif
310 debug_send_line( ssl, level, file, line,
311 "should not happen\n" );
312 }
313}
314
315static 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
333 debug_send_line( ssl, level, file, line, str );
334
335 start = cur + 1;
336 }
337 }
338}
339
340void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
341 const char *file, int line,
342 const char *text, const mbedtls_x509_crt *crt )
343{
344 char str[DEBUG_BUF_SIZE];
345 int i = 0;
346
347 if( ssl->conf == NULL || ssl->conf->f_dbg == NULL || crt == NULL || level > debug_threshold )
348 return;
349
350 while( crt != NULL )
351 {
352 char buf[1024];
353
354 mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i );
355 debug_send_line( ssl, level, file, line, str );
356
357 mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
358 debug_print_line_by_line( ssl, level, file, line, buf );
359
360 debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
361
362 crt = crt->next;
363 }
364}
365#endif /* MBEDTLS_X509_CRT_PARSE_C */
366
367#endif /* MBEDTLS_DEBUG_C */
Note: See TracBrowser for help on using the repository browser.