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 
77 MBEDTLS_PRINTF_ATTRIBUTE(5, 6)
mbedtls_debug_print_msg(const mbedtls_ssl_context * ssl,int level,const char * file,int line,const char * format,...)78 void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
79                               const char *file, int line,
80                               const char *format, ... )
81 {
82     va_list argp;
83     char str[DEBUG_BUF_SIZE];
84     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
85 
86     if( NULL == ssl              ||
87         NULL == ssl->conf        ||
88         NULL == ssl->conf->f_dbg ||
89         level > debug_threshold )
90     {
91         return;
92     }
93 
94     va_start( argp, format );
95     ret = mbedtls_vsnprintf( str, DEBUG_BUF_SIZE, format, argp );
96     va_end( argp );
97 
98     if( ret >= 0 && ret < DEBUG_BUF_SIZE - 1 )
99     {
100         str[ret]     = '\n';
101         str[ret + 1] = '\0';
102     }
103 
104     debug_send_line( ssl, level, file, line, str );
105 }
106 
mbedtls_debug_print_ret(const mbedtls_ssl_context * ssl,int level,const char * file,int line,const char * text,int ret)107 void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
108                       const char *file, int line,
109                       const char *text, int ret )
110 {
111     char str[DEBUG_BUF_SIZE];
112 
113     if( NULL == ssl              ||
114         NULL == ssl->conf        ||
115         NULL == ssl->conf->f_dbg ||
116         level > debug_threshold )
117     {
118         return;
119     }
120 
121     /*
122      * With non-blocking I/O and examples that just retry immediately,
123      * the logs would be quickly flooded with WANT_READ, so ignore that.
124      * Don't ignore WANT_WRITE however, since is is usually rare.
125      */
126     if( ret == MBEDTLS_ERR_SSL_WANT_READ )
127         return;
128 
129     mbedtls_snprintf( str, sizeof( str ), "%s() returned %d (-0x%04x)\n",
130               text, ret, (unsigned int) -ret );
131 
132     debug_send_line( ssl, level, file, line, str );
133 }
134 
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)135 void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
136                       const char *file, int line, const char *text,
137                       const unsigned char *buf, size_t len )
138 {
139     char str[DEBUG_BUF_SIZE];
140     char txt[17];
141     size_t i, idx = 0;
142 
143     if( NULL == ssl              ||
144         NULL == ssl->conf        ||
145         NULL == ssl->conf->f_dbg ||
146         level > debug_threshold )
147     {
148         return;
149     }
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)
mbedtls_debug_print_ecp(const mbedtls_ssl_context * ssl,int level,const char * file,int line,const char * text,const mbedtls_ecp_point * X)195 void 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( NULL == ssl              ||
202         NULL == ssl->conf        ||
203         NULL == ssl->conf->f_dbg ||
204         level > debug_threshold )
205     {
206         return;
207     }
208 
209     mbedtls_snprintf( str, sizeof( str ), "%s(X)", text );
210     mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
211 
212     mbedtls_snprintf( str, sizeof( str ), "%s(Y)", text );
213     mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
214 }
215 #endif /* MBEDTLS_ECP_C */
216 
217 #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)218 void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
219                       const char *file, int line,
220                       const char *text, const mbedtls_mpi *X )
221 {
222     char str[DEBUG_BUF_SIZE];
223     size_t bitlen;
224     size_t idx = 0;
225 
226     if( NULL == ssl              ||
227         NULL == ssl->conf        ||
228         NULL == ssl->conf->f_dbg ||
229         NULL == X                ||
230         level > debug_threshold )
231     {
232         return;
233     }
234 
235     bitlen = mbedtls_mpi_bitlen( X );
236 
237     mbedtls_snprintf( str, sizeof( str ), "value of '%s' (%u bits) is:\n",
238                       text, (unsigned) bitlen );
239     debug_send_line( ssl, level, file, line, str );
240 
241     if( bitlen == 0 )
242     {
243         str[0] = ' '; str[1] = '0'; str[2] = '0';
244         idx = 3;
245     }
246     else
247     {
248         int n;
249         for( n = (int) ( ( bitlen - 1 ) / 8 ); n >= 0; n-- )
250         {
251             size_t limb_offset = n / sizeof( mbedtls_mpi_uint );
252             size_t offset_in_limb = n % sizeof( mbedtls_mpi_uint );
253             unsigned char octet =
254                 ( X->p[limb_offset] >> ( offset_in_limb * 8 ) ) & 0xff;
255             mbedtls_snprintf( str + idx, sizeof( str ) - idx, " %02x", octet );
256             idx += 3;
257             /* Wrap lines after 16 octets that each take 3 columns */
258             if( idx >= 3 * 16 )
259             {
260                 mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
261                 debug_send_line( ssl, level, file, line, str );
262                 idx = 0;
263             }
264         }
265     }
266 
267     if( idx != 0 )
268     {
269         mbedtls_snprintf( str + idx, sizeof( str ) - idx, "\n" );
270         debug_send_line( ssl, level, file, line, str );
271     }
272 }
273 #endif /* MBEDTLS_BIGNUM_C */
274 
275 #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)276 static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
277                             const char *file, int line,
278                             const char *text, const mbedtls_pk_context *pk )
279 {
280     size_t i;
281     mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
282     char name[16];
283 
284     memset( items, 0, sizeof( items ) );
285 
286     if( mbedtls_pk_debug( pk, items ) != 0 )
287     {
288         debug_send_line( ssl, level, file, line,
289                           "invalid PK context\n" );
290         return;
291     }
292 
293     for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
294     {
295         if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
296             return;
297 
298         mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
299         name[sizeof( name ) - 1] = '\0';
300 
301         if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
302             mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
303         else
304 #if defined(MBEDTLS_ECP_C)
305         if( items[i].type == MBEDTLS_PK_DEBUG_ECP )
306             mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value );
307         else
308 #endif
309             debug_send_line( ssl, level, file, line,
310                               "should not happen\n" );
311     }
312 }
313 
debug_print_line_by_line(const mbedtls_ssl_context * ssl,int level,const char * file,int line,const char * text)314 static void debug_print_line_by_line( const mbedtls_ssl_context *ssl, int level,
315                                       const char *file, int line, const char *text )
316 {
317     char str[DEBUG_BUF_SIZE];
318     const char *start, *cur;
319 
320     start = text;
321     for( cur = text; *cur != '\0'; cur++ )
322     {
323         if( *cur == '\n' )
324         {
325             size_t len = cur - start + 1;
326             if( len > DEBUG_BUF_SIZE - 1 )
327                 len = DEBUG_BUF_SIZE - 1;
328 
329             memcpy( str, start, len );
330             str[len] = '\0';
331 
332             debug_send_line( ssl, level, file, line, str );
333 
334             start = cur + 1;
335         }
336     }
337 }
338 
mbedtls_debug_print_crt(const mbedtls_ssl_context * ssl,int level,const char * file,int line,const char * text,const mbedtls_x509_crt * crt)339 void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
340                       const char *file, int line,
341                       const char *text, const mbedtls_x509_crt *crt )
342 {
343     char str[DEBUG_BUF_SIZE];
344     int i = 0;
345 
346     if( NULL == ssl              ||
347         NULL == ssl->conf        ||
348         NULL == ssl->conf->f_dbg ||
349         NULL == crt              ||
350         level > debug_threshold )
351     {
352         return;
353     }
354 
355     while( crt != NULL )
356     {
357         char buf[1024];
358 
359         mbedtls_snprintf( str, sizeof( str ), "%s #%d:\n", text, ++i );
360         debug_send_line( ssl, level, file, line, str );
361 
362         mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
363         debug_print_line_by_line( ssl, level, file, line, buf );
364 
365         debug_print_pk( ssl, level, file, line, "crt->", &crt->pk );
366 
367         crt = crt->next;
368     }
369 }
370 #endif /* MBEDTLS_X509_CRT_PARSE_C */
371 
372 #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)373 static void mbedtls_debug_printf_ecdh_internal( const mbedtls_ssl_context *ssl,
374                                                 int level, const char *file,
375                                                 int line,
376                                                 const mbedtls_ecdh_context *ecdh,
377                                                 mbedtls_debug_ecdh_attr attr )
378 {
379 #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
380     const mbedtls_ecdh_context* ctx = ecdh;
381 #else
382     const mbedtls_ecdh_context_mbed* ctx = &ecdh->ctx.mbed_ecdh;
383 #endif
384 
385     switch( attr )
386     {
387         case MBEDTLS_DEBUG_ECDH_Q:
388             mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Q",
389                                      &ctx->Q );
390             break;
391         case MBEDTLS_DEBUG_ECDH_QP:
392             mbedtls_debug_print_ecp( ssl, level, file, line, "ECDH: Qp",
393                                      &ctx->Qp );
394             break;
395         case MBEDTLS_DEBUG_ECDH_Z:
396             mbedtls_debug_print_mpi( ssl, level, file, line, "ECDH: z",
397                                      &ctx->z );
398             break;
399         default:
400             break;
401     }
402 }
403 
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)404 void mbedtls_debug_printf_ecdh( const mbedtls_ssl_context *ssl, int level,
405                                 const char *file, int line,
406                                 const mbedtls_ecdh_context *ecdh,
407                                 mbedtls_debug_ecdh_attr attr )
408 {
409 #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
410     mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh, attr );
411 #else
412     switch( ecdh->var )
413     {
414         default:
415             mbedtls_debug_printf_ecdh_internal( ssl, level, file, line, ecdh,
416                                                 attr );
417     }
418 #endif
419 }
420 #endif /* MBEDTLS_ECDH_C */
421 
422 #endif /* MBEDTLS_DEBUG_C */
423