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