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