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