4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
6 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * * Neither the names of PolarSSL or XySSL nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include "tropicssl/config.h"
38 #if defined(TROPICSSL_DEBUG_C)
40 #include "tropicssl/debug.h"
45 #if defined _MSC_VER && !defined snprintf
46 #define snprintf _snprintf
49 #if defined _MSC_VER && !defined vsnprintf
50 #define vsnprintf _vsnprintf
53 char *debug_fmt(const char *format
, ...)
57 int maxlen
= sizeof(str
) - 1;
59 va_start(argp
, format
);
60 vsnprintf(str
, maxlen
, format
, argp
);
67 void debug_print_msg(const ssl_context
* ssl
, int level
,
68 const char *file
, int line
, const char *text
)
71 int maxlen
= sizeof(str
) - 1;
73 if (ssl
->f_dbg
== NULL
)
76 snprintf(str
, maxlen
, "%s(%04d): %s\n", file
, line
, text
);
78 ssl
->f_dbg(ssl
->p_dbg
, level
, str
);
81 void debug_print_ret(const ssl_context
* ssl
, int level
,
82 const char *file
, int line
,
83 const char *text
, int ret
)
86 int maxlen
= sizeof(str
) - 1;
88 if (ssl
->f_dbg
== NULL
)
91 snprintf(str
, maxlen
, "%s(%04d): %s() returned %d (0x%x)\n",
92 file
, line
, text
, ret
, ret
);
95 ssl
->f_dbg(ssl
->p_dbg
, level
, str
);
98 void debug_print_buf(const ssl_context
* ssl
, int level
,
99 const char *file
, int line
, const char *text
,
100 unsigned char *buf
, int len
)
103 int i
, maxlen
= sizeof(str
) - 1;
105 if (ssl
->f_dbg
== NULL
|| len
< 0)
108 snprintf(str
, maxlen
, "%s(%04d): dumping '%s' (%d bytes)\n",
109 file
, line
, text
, len
);
112 ssl
->f_dbg(ssl
->p_dbg
, level
, str
);
114 for (i
= 0; i
< len
; i
++) {
120 ssl
->f_dbg(ssl
->p_dbg
, level
, "\n");
122 snprintf(str
, maxlen
, "%s(%04d): %04x: ", file
, line
,
126 ssl
->f_dbg(ssl
->p_dbg
, level
, str
);
129 snprintf(str
, maxlen
, " %02x", (unsigned int)buf
[i
]);
132 ssl
->f_dbg(ssl
->p_dbg
, level
, str
);
136 ssl
->f_dbg(ssl
->p_dbg
, level
, "\n");
139 void debug_print_mpi(const ssl_context
* ssl
, int level
,
140 const char *file
, int line
,
141 const char *text
, const mpi
* X
)
144 int i
, j
, k
, n
, maxlen
= sizeof(str
) - 1;
146 if (ssl
->f_dbg
== NULL
|| X
== NULL
)
149 for (n
= X
->n
- 1; n
>= 0; n
--)
153 snprintf(str
, maxlen
, "%s(%04d): value of '%s' (%lu bits) is:\n",
155 (unsigned long)((n
+ 1) * sizeof(t_int
)) << 3);
158 ssl
->f_dbg(ssl
->p_dbg
, level
, str
);
160 for (i
= n
, j
= 0; i
>= 0; i
--, j
++) {
161 if (j
% (16 / sizeof(t_int
)) == 0) {
163 ssl
->f_dbg(ssl
->p_dbg
, level
, "\n");
165 snprintf(str
, maxlen
, "%s(%04d): ", file
, line
);
168 ssl
->f_dbg(ssl
->p_dbg
, level
, str
);
171 for (k
= sizeof(t_int
) - 1; k
>= 0; k
--) {
172 snprintf(str
, maxlen
, " %02x", (unsigned int)
173 (X
->p
[i
] >> (k
<< 3)) & 0xFF);
176 ssl
->f_dbg(ssl
->p_dbg
, level
, str
);
180 ssl
->f_dbg(ssl
->p_dbg
, level
, "\n");
183 void debug_print_crt(const ssl_context
* ssl
, int level
,
184 const char *file
, int line
,
185 const char *text
, const x509_cert
* crt
)
187 char str
[512], prefix
[64], *p
;
188 int i
= 0, maxlen
= sizeof(prefix
) - 1;
190 if (ssl
->f_dbg
== NULL
|| crt
== NULL
)
193 snprintf(prefix
, maxlen
, "%s(%04d): ", file
, line
);
194 prefix
[maxlen
] = '\0';
195 maxlen
= sizeof(str
) - 1;
197 while (crt
!= NULL
&& crt
->next
!= NULL
) {
199 p
= x509parse_cert_info(buf
, sizeof(buf
)-1, prefix
, crt
);
201 snprintf(str
, maxlen
, "%s(%04d): %s #%d:\n%s",
202 file
, line
, text
, ++i
, p
);
205 ssl
->f_dbg(ssl
->p_dbg
, level
, str
);
207 debug_print_mpi(ssl
, level
, file
, line
,
208 "crt->rsa.N", &crt
->rsa
.N
);
210 debug_print_mpi(ssl
, level
, file
, line
,
211 "crt->rsa.E", &crt
->rsa
.E
);