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