bignum: make mpi_init() and mpi_free() accept a single argument
[tropicssl.git] / library / debug.c
blobb842b424f0991a76f7f1ad2f56a33eef4a43ffba
1 /*
2 * Debugging routines
4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
6 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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"
42 #include <stdarg.h>
43 #include <stdlib.h>
45 #if defined _MSC_VER && !defined snprintf
46 #define snprintf _snprintf
47 #endif
49 #if defined _MSC_VER && !defined vsnprintf
50 #define vsnprintf _vsnprintf
51 #endif
53 char *debug_fmt(const char *format, ...)
55 va_list argp;
56 static char str[512];
57 int maxlen = sizeof(str) - 1;
59 va_start(argp, format);
60 vsnprintf(str, maxlen, format, argp);
61 va_end(argp);
63 str[maxlen] = '\0';
64 return (str);
67 void debug_print_msg(const ssl_context * ssl, int level,
68 const char *file, int line, const char *text)
70 char str[512];
71 int maxlen = sizeof(str) - 1;
73 if (ssl->f_dbg == NULL)
74 return;
76 snprintf(str, maxlen, "%s(%04d): %s\n", file, line, text);
77 str[maxlen] = '\0';
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)
85 char str[512];
86 int maxlen = sizeof(str) - 1;
88 if (ssl->f_dbg == NULL)
89 return;
91 snprintf(str, maxlen, "%s(%04d): %s() returned %d (0x%x)\n",
92 file, line, text, ret, ret);
94 str[maxlen] = '\0';
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)
102 char str[512];
103 int i, maxlen = sizeof(str) - 1;
105 if (ssl->f_dbg == NULL || len < 0)
106 return;
108 snprintf(str, maxlen, "%s(%04d): dumping '%s' (%d bytes)\n",
109 file, line, text, len);
111 str[maxlen] = '\0';
112 ssl->f_dbg(ssl->p_dbg, level, str);
114 for (i = 0; i < len; i++) {
115 if (i >= 4096)
116 break;
118 if (i % 16 == 0) {
119 if (i > 0)
120 ssl->f_dbg(ssl->p_dbg, level, "\n");
122 snprintf(str, maxlen, "%s(%04d): %04x: ", file, line,
125 str[maxlen] = '\0';
126 ssl->f_dbg(ssl->p_dbg, level, str);
129 snprintf(str, maxlen, " %02x", (unsigned int)buf[i]);
131 str[maxlen] = '\0';
132 ssl->f_dbg(ssl->p_dbg, level, str);
135 if (len > 0)
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)
143 char str[512];
144 int i, j, k, n, maxlen = sizeof(str) - 1;
146 if (ssl->f_dbg == NULL || X == NULL)
147 return;
149 for (n = X->n - 1; n >= 0; n--)
150 if (X->p[n] != 0)
151 break;
153 snprintf(str, maxlen, "%s(%04d): value of '%s' (%lu bits) is:\n",
154 file, line, text,
155 (unsigned long)((n + 1) * sizeof(t_int)) << 3);
157 str[maxlen] = '\0';
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) {
162 if (j > 0)
163 ssl->f_dbg(ssl->p_dbg, level, "\n");
165 snprintf(str, maxlen, "%s(%04d): ", file, line);
167 str[maxlen] = '\0';
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);
175 str[maxlen] = '\0';
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)
191 return;
193 snprintf(prefix, maxlen, "%s(%04d): ", file, line);
194 prefix[maxlen] = '\0';
195 maxlen = sizeof(str) - 1;
197 while (crt != NULL && crt->next != NULL) {
198 char buf[1024];
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);
204 str[maxlen] = '\0';
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);
213 crt = crt->next;
217 #endif