@@ -112,61 +112,72 @@ void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level,
112112 debug_send_line (ssl , level , file , line , str );
113113}
114114
115- void mbedtls_debug_print_buf (const mbedtls_ssl_context * ssl , int level ,
116- const char * file , int line , const char * text ,
117- const unsigned char * buf , size_t len )
115+ #define MBEDTLS_DEBUG_PRINT_BUF_NO_TEXT 0
116+ #define MBEDTLS_DEBUG_PRINT_BUF_ADD_TEXT 1
117+
118+ static void mbedtls_debug_print_buf_one_line (char * out_buf , size_t out_size ,
119+ const unsigned char * in_buf , size_t in_size ,
120+ int add_text )
118121{
119- char str [DEBUG_BUF_SIZE ];
120- char txt [17 ];
122+ char txt [17 ] = { 0 };
121123 size_t i , idx = 0 ;
122124
125+ for (i = 0 ; i < 16 ; i ++ ) {
126+ if (i < in_size ) {
127+ idx += mbedtls_snprintf (out_buf + idx , out_size - idx , " %02x" ,
128+ (unsigned int ) in_buf [i ]);
129+ txt [i ] = (in_buf [i ] > 31 && in_buf [i ] < 127 ) ? in_buf [i ] : '.' ;
130+ } else {
131+ /* Just add spaces until the end of the line */
132+ idx += mbedtls_snprintf (out_buf + idx , out_size - idx , " " );
133+ }
134+ }
135+
136+ if (add_text ) {
137+ idx += mbedtls_snprintf (out_buf + idx , out_size - idx , " %s" , txt );
138+ }
139+ mbedtls_snprintf (out_buf + idx , out_size - idx , "\n" , txt );
140+ }
141+
142+ static void mbedtls_debug_print_buf_ext (const mbedtls_ssl_context * ssl , int level ,
143+ const char * file , int line , const char * text ,
144+ const unsigned char * buf , size_t len ,
145+ int add_text )
146+ {
147+ char str [DEBUG_BUF_SIZE ] = { 0 };
148+ size_t curr_offset = 0 , idx = 0 , chunk_len ;
149+
123150 if (NULL == ssl ||
124151 NULL == ssl -> conf ||
125152 NULL == ssl -> conf -> f_dbg ||
126153 level > debug_threshold ) {
127154 return ;
128155 }
129156
130- mbedtls_snprintf (str + idx , sizeof (str ) - idx , "dumping '%s' (%u bytes)\n" ,
131- text , (unsigned int ) len );
132-
157+ mbedtls_snprintf (str , sizeof (str ), "dumping '%s' (%u bytes)\n" , text , len );
133158 debug_send_line (ssl , level , file , line , str );
134159
135- memset (txt , 0 , sizeof (txt ));
136- for (i = 0 ; i < len ; i ++ ) {
137- if (i >= 4096 ) {
138- break ;
139- }
140-
141- if (i % 16 == 0 ) {
142- if (i > 0 ) {
143- mbedtls_snprintf (str + idx , sizeof (str ) - idx , " %s\n" , txt );
144- debug_send_line (ssl , level , file , line , str );
145-
146- idx = 0 ;
147- memset (txt , 0 , sizeof (txt ));
148- }
149-
150- idx += mbedtls_snprintf (str + idx , sizeof (str ) - idx , "%04x: " ,
151- (unsigned int ) i );
152-
153- }
154-
155- idx += mbedtls_snprintf (str + idx , sizeof (str ) - idx , " %02x" ,
156- (unsigned int ) buf [i ]);
157- txt [i % 16 ] = (buf [i ] > 31 && buf [i ] < 127 ) ? buf [i ] : '.' ;
158- }
159-
160- if (len > 0 ) {
161- for (/* i = i */ ; i % 16 != 0 ; i ++ ) {
162- idx += mbedtls_snprintf (str + idx , sizeof (str ) - idx , " " );
163- }
164-
165- mbedtls_snprintf (str + idx , sizeof (str ) - idx , " %s\n" , txt );
160+ while (len > 0 ) {
161+ memset (str , 0 , sizeof (str ));
162+ idx = mbedtls_snprintf (str , sizeof (str ), "%04x: " , curr_offset );
163+ chunk_len = (len >= 16 ) ? 16 : len ;
164+ mbedtls_debug_print_buf_one_line (str + idx , sizeof (str ) - idx ,
165+ & buf [curr_offset ], chunk_len ,
166+ add_text );
166167 debug_send_line (ssl , level , file , line , str );
168+ curr_offset += 16 ;
169+ len -= chunk_len ;
167170 }
168171}
169172
173+ void mbedtls_debug_print_buf (const mbedtls_ssl_context * ssl , int level ,
174+ const char * file , int line , const char * text ,
175+ const unsigned char * buf , size_t len )
176+ {
177+ mbedtls_debug_print_buf_ext (ssl , level , file , line , text , buf , len ,
178+ MBEDTLS_DEBUG_PRINT_BUF_ADD_TEXT );
179+ }
180+
170181#if defined(MBEDTLS_X509_CRT_PARSE_C ) && !defined(MBEDTLS_X509_REMOVE_INFO )
171182static void debug_print_pk (const mbedtls_ssl_context * ssl , int level ,
172183 const char * file , int line ,
@@ -178,7 +189,8 @@ static void debug_print_pk(const mbedtls_ssl_context *ssl, int level,
178189
179190 ret = mbedtls_pk_write_pubkey_psa (pk , buf , sizeof (buf ), & buf_len );
180191 if (ret == 0 ) {
181- mbedtls_debug_print_buf (ssl , level , file , line , text , buf , buf_len );
192+ mbedtls_debug_print_buf_ext (ssl , level , file , line , text , buf , buf_len ,
193+ MBEDTLS_DEBUG_PRINT_BUF_NO_TEXT );
182194 } else {
183195 mbedtls_debug_print_msg (ssl , level , file , line ,
184196 "failed to export public key from PK context" );
0 commit comments