4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * This file is part of mbed TLS (https://tls.mbed.org)
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
25 #include MBEDTLS_CONFIG_FILE
28 #if defined(MBEDTLS_DEBUG_C)
30 #if defined(MBEDTLS_PLATFORM_C)
31 #include "mbedtls/platform.h"
34 #define mbedtls_calloc calloc
35 #define mbedtls_free free
36 #define mbedtls_time_t time_t
37 #define mbedtls_snprintf snprintf
40 #include "mbedtls/debug.h"
46 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
47 !defined(inline) && !defined(__cplusplus)
48 #define inline __inline
51 #define DEBUG_BUF_SIZE 512
53 static int debug_threshold
= 0;
55 void mbedtls_debug_set_threshold( int threshold
)
57 debug_threshold
= threshold
;
61 * All calls to f_dbg must be made via this function
63 static inline void debug_send_line( const mbedtls_ssl_context
*ssl
, int level
,
64 const char *file
, int line
,
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.
72 #if defined(MBEDTLS_THREADING_C)
73 char idstr
[20 + DEBUG_BUF_SIZE
]; /* 0x + 16 nibbles + ': ' */
74 mbedtls_snprintf( idstr
, sizeof( idstr
), "%p: %s", ssl
, str
);
75 ssl
->conf
->f_dbg( ssl
->conf
->p_dbg
, level
, file
, line
, idstr
);
77 ssl
->conf
->f_dbg( ssl
->conf
->p_dbg
, level
, file
, line
, str
);
81 void mbedtls_debug_print_msg( const mbedtls_ssl_context
*ssl
, int level
,
82 const char *file
, int line
,
83 const char *format
, ... )
86 char str
[DEBUG_BUF_SIZE
];
89 if( NULL
== ssl
|| NULL
== ssl
->conf
|| NULL
== ssl
->conf
->f_dbg
|| level
> debug_threshold
)
92 va_start( argp
, format
);
94 #if defined(_TRUNCATE)
95 ret
= _vsnprintf_s( str
, DEBUG_BUF_SIZE
, _TRUNCATE
, format
, argp
);
97 ret
= _vsnprintf( str
, DEBUG_BUF_SIZE
, format
, argp
);
98 if( ret
< 0 || (size_t) ret
== DEBUG_BUF_SIZE
)
100 str
[DEBUG_BUF_SIZE
-1] = '\0';
105 ret
= vsnprintf( str
, DEBUG_BUF_SIZE
, format
, argp
);
109 if( ret
>= 0 && ret
< DEBUG_BUF_SIZE
- 1 )
115 debug_send_line( ssl
, level
, file
, line
, str
);
118 void mbedtls_debug_print_ret( const mbedtls_ssl_context
*ssl
, int level
,
119 const char *file
, int line
,
120 const char *text
, int ret
)
122 char str
[DEBUG_BUF_SIZE
];
124 if( ssl
->conf
== NULL
|| ssl
->conf
->f_dbg
== NULL
|| level
> debug_threshold
)
128 * With non-blocking I/O and examples that just retry immediately,
129 * the logs would be quickly flooded with WANT_READ, so ignore that.
130 * Don't ignore WANT_WRITE however, since is is usually rare.
132 if( ret
== MBEDTLS_ERR_SSL_WANT_READ
)
135 mbedtls_snprintf( str
, sizeof( str
), "%s() returned %d (-0x%04x)\n",
138 debug_send_line( ssl
, level
, file
, line
, str
);
141 void mbedtls_debug_print_buf( const mbedtls_ssl_context
*ssl
, int level
,
142 const char *file
, int line
, const char *text
,
143 const unsigned char *buf
, size_t len
)
145 char str
[DEBUG_BUF_SIZE
];
149 if( ssl
->conf
== NULL
|| ssl
->conf
->f_dbg
== NULL
|| level
> debug_threshold
)
152 mbedtls_snprintf( str
+ idx
, sizeof( str
) - idx
, "dumping '%s' (%u bytes)\n",
153 text
, (unsigned int) len
);
155 debug_send_line( ssl
, level
, file
, line
, str
);
158 memset( txt
, 0, sizeof( txt
) );
159 for( i
= 0; i
< len
; i
++ )
168 mbedtls_snprintf( str
+ idx
, sizeof( str
) - idx
, " %s\n", txt
);
169 debug_send_line( ssl
, level
, file
, line
, str
);
172 memset( txt
, 0, sizeof( txt
) );
175 idx
+= mbedtls_snprintf( str
+ idx
, sizeof( str
) - idx
, "%04x: ",
180 idx
+= mbedtls_snprintf( str
+ idx
, sizeof( str
) - idx
, " %02x",
181 (unsigned int) buf
[i
] );
182 txt
[i
% 16] = ( buf
[i
] > 31 && buf
[i
] < 127 ) ? buf
[i
] : '.' ;
187 for( /* i = i */; i
% 16 != 0; i
++ )
188 idx
+= mbedtls_snprintf( str
+ idx
, sizeof( str
) - idx
, " " );
190 mbedtls_snprintf( str
+ idx
, sizeof( str
) - idx
, " %s\n", txt
);
191 debug_send_line( ssl
, level
, file
, line
, str
);
195 #if defined(MBEDTLS_ECP_C)
196 void mbedtls_debug_print_ecp( const mbedtls_ssl_context
*ssl
, int level
,
197 const char *file
, int line
,
198 const char *text
, const mbedtls_ecp_point
*X
)
200 char str
[DEBUG_BUF_SIZE
];
202 if( ssl
->conf
== NULL
|| ssl
->conf
->f_dbg
== NULL
|| level
> debug_threshold
)
205 mbedtls_snprintf( str
, sizeof( str
), "%s(X)", text
);
206 mbedtls_debug_print_mpi( ssl
, level
, file
, line
, str
, &X
->X
);
208 mbedtls_snprintf( str
, sizeof( str
), "%s(Y)", text
);
209 mbedtls_debug_print_mpi( ssl
, level
, file
, line
, str
, &X
->Y
);
211 #endif /* MBEDTLS_ECP_C */
213 #if defined(MBEDTLS_BIGNUM_C)
214 void mbedtls_debug_print_mpi( const mbedtls_ssl_context
*ssl
, int level
,
215 const char *file
, int line
,
216 const char *text
, const mbedtls_mpi
*X
)
218 char str
[DEBUG_BUF_SIZE
];
220 size_t i
, n
, idx
= 0;
222 if( ssl
->conf
== NULL
|| ssl
->conf
->f_dbg
== NULL
|| X
== NULL
|| level
> debug_threshold
)
225 for( n
= X
->n
- 1; n
> 0; n
-- )
229 for( j
= ( sizeof(mbedtls_mpi_uint
) << 3 ) - 1; j
>= 0; j
-- )
230 if( ( ( X
->p
[n
] >> j
) & 1 ) != 0 )
233 mbedtls_snprintf( str
+ idx
, sizeof( str
) - idx
, "value of '%s' (%d bits) is:\n",
234 text
, (int) ( ( n
* ( sizeof(mbedtls_mpi_uint
) << 3 ) ) + j
+ 1 ) );
236 debug_send_line( ssl
, level
, file
, line
, str
);
239 for( i
= n
+ 1, j
= 0; i
> 0; i
-- )
241 if( zeros
&& X
->p
[i
- 1] == 0 )
244 for( k
= sizeof( mbedtls_mpi_uint
) - 1; k
>= 0; k
-- )
246 if( zeros
&& ( ( X
->p
[i
- 1] >> ( k
<< 3 ) ) & 0xFF ) == 0 )
255 mbedtls_snprintf( str
+ idx
, sizeof( str
) - idx
, "\n" );
256 debug_send_line( ssl
, level
, file
, line
, str
);
261 idx
+= mbedtls_snprintf( str
+ idx
, sizeof( str
) - idx
, " %02x", (unsigned int)
262 ( X
->p
[i
- 1] >> ( k
<< 3 ) ) & 0xFF );
270 idx
+= mbedtls_snprintf( str
+ idx
, sizeof( str
) - idx
, " 00" );
272 mbedtls_snprintf( str
+ idx
, sizeof( str
) - idx
, "\n" );
273 debug_send_line( ssl
, level
, file
, line
, str
);
275 #endif /* MBEDTLS_BIGNUM_C */
277 #if defined(MBEDTLS_X509_CRT_PARSE_C)
278 static void debug_print_pk( const mbedtls_ssl_context
*ssl
, int level
,
279 const char *file
, int line
,
280 const char *text
, const mbedtls_pk_context
*pk
)
283 mbedtls_pk_debug_item items
[MBEDTLS_PK_DEBUG_MAX_ITEMS
];
286 memset( items
, 0, sizeof( items
) );
288 if( mbedtls_pk_debug( pk
, items
) != 0 )
290 debug_send_line( ssl
, level
, file
, line
,
291 "invalid PK context\n" );
295 for( i
= 0; i
< MBEDTLS_PK_DEBUG_MAX_ITEMS
; i
++ )
297 if( items
[i
].type
== MBEDTLS_PK_DEBUG_NONE
)
300 mbedtls_snprintf( name
, sizeof( name
), "%s%s", text
, items
[i
].name
);
301 name
[sizeof( name
) - 1] = '\0';
303 if( items
[i
].type
== MBEDTLS_PK_DEBUG_MPI
)
304 mbedtls_debug_print_mpi( ssl
, level
, file
, line
, name
, items
[i
].value
);
306 #if defined(MBEDTLS_ECP_C)
307 if( items
[i
].type
== MBEDTLS_PK_DEBUG_ECP
)
308 mbedtls_debug_print_ecp( ssl
, level
, file
, line
, name
, items
[i
].value
);
311 debug_send_line( ssl
, level
, file
, line
,
312 "should not happen\n" );
316 static void debug_print_line_by_line( const mbedtls_ssl_context
*ssl
, int level
,
317 const char *file
, int line
, const char *text
)
319 char str
[DEBUG_BUF_SIZE
];
320 const char *start
, *cur
;
323 for( cur
= text
; *cur
!= '\0'; cur
++ )
327 size_t len
= cur
- start
+ 1;
328 if( len
> DEBUG_BUF_SIZE
- 1 )
329 len
= DEBUG_BUF_SIZE
- 1;
331 memcpy( str
, start
, len
);
334 debug_send_line( ssl
, level
, file
, line
, str
);
341 void mbedtls_debug_print_crt( const mbedtls_ssl_context
*ssl
, int level
,
342 const char *file
, int line
,
343 const char *text
, const mbedtls_x509_crt
*crt
)
345 char str
[DEBUG_BUF_SIZE
];
348 if( ssl
->conf
== NULL
|| ssl
->conf
->f_dbg
== NULL
|| crt
== NULL
|| level
> debug_threshold
)
355 mbedtls_snprintf( str
, sizeof( str
), "%s #%d:\n", text
, ++i
);
356 debug_send_line( ssl
, level
, file
, line
, str
);
358 mbedtls_x509_crt_info( buf
, sizeof( buf
) - 1, "", crt
);
359 debug_print_line_by_line( ssl
, level
, file
, line
, buf
);
361 debug_print_pk( ssl
, level
, file
, line
, "crt->", &crt
->pk
);
366 #endif /* MBEDTLS_X509_CRT_PARSE_C */
368 #endif /* MBEDTLS_DEBUG_C */