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