/src/opensc/src/libopensc/log.c
Line | Count | Source |
1 | | /* |
2 | | * log.c: Miscellaneous logging functions |
3 | | * |
4 | | * Copyright (C) 2001, 2002 Juha Yrjölä <juha.yrjola@iki.fi> |
5 | | * Copyright (C) 2003 Antti Tapaninen <aet@cc.hut.fi> |
6 | | * |
7 | | * This library is free software; you can redistribute it and/or |
8 | | * modify it under the terms of the GNU Lesser General Public |
9 | | * License as published by the Free Software Foundation; either |
10 | | * version 2.1 of the License, or (at your option) any later version. |
11 | | * |
12 | | * This library is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | | * Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public |
18 | | * License along with this library; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 | | */ |
21 | | |
22 | | #ifdef HAVE_CONFIG_H |
23 | | #include "config.h" |
24 | | #endif |
25 | | |
26 | | #include <stdarg.h> |
27 | | #include <stdlib.h> |
28 | | #include <ctype.h> |
29 | | #include <string.h> |
30 | | #include <time.h> |
31 | | #ifdef HAVE_UNISTD_H |
32 | | #include <unistd.h> |
33 | | #endif |
34 | | #ifdef HAVE_SYS_TIME_H |
35 | | #include <sys/time.h> |
36 | | #endif |
37 | | #ifdef HAVE_IO_H |
38 | | #include <io.h> |
39 | | #endif |
40 | | #ifdef HAVE_PTHREAD |
41 | | #include <pthread.h> |
42 | | #endif |
43 | | #ifdef _WIN32 |
44 | | #include <windows.h> |
45 | | #endif |
46 | | #ifdef ENABLE_OPENSSL |
47 | | #include <openssl/err.h> |
48 | | #endif /* ENABLE_OPENSSL */ |
49 | | #include "common/compat_strlcat.h" |
50 | | |
51 | | #include "internal.h" |
52 | | |
53 | | static void sc_do_log_va(sc_context_t *ctx, int level, const char *file, int line, const char *func, int color, const char *format, va_list args); |
54 | | static int sc_color_fprintf_va(int colors, struct sc_context *ctx, FILE * stream, const char *format, va_list args); |
55 | | |
56 | | void sc_do_log(sc_context_t *ctx, int level, const char *file, int line, const char *func, const char *format, ...) |
57 | 15.5M | { |
58 | 15.5M | va_list ap; |
59 | | |
60 | 15.5M | va_start(ap, format); |
61 | 15.5M | sc_do_log_va(ctx, level, file, line, func, 0, format, ap); |
62 | 15.5M | va_end(ap); |
63 | 15.5M | } |
64 | | |
65 | | void sc_do_log_color(sc_context_t *ctx, int level, const char *file, int line, const char *func, int color, const char *format, ...) |
66 | 2.97M | { |
67 | 2.97M | va_list ap; |
68 | | |
69 | 2.97M | va_start(ap, format); |
70 | 2.97M | sc_do_log_va(ctx, level, file, line, func, color, format, ap); |
71 | 2.97M | va_end(ap); |
72 | 2.97M | } |
73 | | |
74 | | #ifdef ENABLE_OPENSSL |
75 | | void sc_do_log_openssl(sc_context_t *ctx, int level, const char *file, int line, const char *func) |
76 | 353 | { |
77 | 353 | BIO *bio = NULL; |
78 | 353 | int length, rc; |
79 | 353 | char *buffer = NULL; |
80 | | |
81 | 353 | if ((bio = BIO_new(BIO_s_mem())) == NULL) { |
82 | 0 | sc_do_log(ctx, level, file, line, func, "Cannot log OpenSSL error"); |
83 | 0 | goto end; |
84 | 0 | } |
85 | 353 | ERR_print_errors(bio); |
86 | | |
87 | 353 | length = BIO_pending(bio); |
88 | 353 | if (length <= 0) { |
89 | | /* no error? */ |
90 | 0 | goto end; |
91 | 0 | } |
92 | | /* trailing null byte */ |
93 | 353 | buffer = malloc(length + 1); |
94 | 353 | if (buffer == NULL) { |
95 | 0 | sc_do_log(ctx, level, file, line, func, "No memory!"); |
96 | 0 | goto end; |
97 | 0 | } |
98 | 353 | rc = BIO_read(bio, buffer, length); |
99 | 353 | buffer[length] = '\0'; |
100 | 353 | if (rc <= 0) { |
101 | 0 | sc_do_log(ctx, level, file, line, func, "Cannot read OpenSSL error"); |
102 | 0 | goto end; |
103 | 0 | } |
104 | | |
105 | 353 | sc_do_log(ctx, level, file, line, func, "OpenSSL error\n%s", buffer); |
106 | 353 | end: |
107 | 353 | free(buffer); |
108 | 353 | BIO_free(bio); |
109 | 353 | } |
110 | | #else |
111 | | void sc_do_log_openssl(sc_context_t *ctx, int level, const char *file, int line, const char *func) |
112 | | { |
113 | | sc_do_log(ctx, level, file, line, func, "OpenSSL not enabled"); |
114 | | } |
115 | | #endif |
116 | | |
117 | | void sc_do_log_noframe(sc_context_t *ctx, int level, const char *format, va_list args) |
118 | 0 | { |
119 | 0 | sc_do_log_va(ctx, level, NULL, 0, NULL, 0, format, args); |
120 | 0 | } |
121 | | |
122 | | static void sc_do_log_va(sc_context_t *ctx, int level, const char *file, int line, const char *func, int color, const char *format, va_list args) |
123 | 18.4M | { |
124 | | #ifdef _WIN32 |
125 | | SYSTEMTIME st; |
126 | | #else |
127 | 18.4M | struct tm *tm; |
128 | 18.4M | struct timeval tv; |
129 | 18.4M | char time_string[40]; |
130 | 18.4M | #endif |
131 | | |
132 | 18.4M | if (!ctx || ctx->debug < level) |
133 | 18.4M | return; |
134 | | |
135 | | #ifdef _WIN32 |
136 | | /* In Windows, file handles can not be shared between DLL-s, each DLL has a |
137 | | * separate file handle table. Make sure we always have a valid file |
138 | | * descriptor. */ |
139 | | if (sc_ctx_log_to_file(ctx, ctx->debug_filename) < 0) |
140 | | return; |
141 | | #endif |
142 | 0 | if (ctx->debug_file == NULL) |
143 | 0 | return; |
144 | | |
145 | | #ifdef _WIN32 |
146 | | GetLocalTime(&st); |
147 | | sc_color_fprintf(SC_COLOR_FG_GREEN|SC_COLOR_BOLD, |
148 | | ctx, ctx->debug_file, |
149 | | "P:%lu; T:%lu", |
150 | | (unsigned long)GetCurrentProcessId(), |
151 | | (unsigned long)GetCurrentThreadId()); |
152 | | sc_color_fprintf(SC_COLOR_FG_GREEN, |
153 | | ctx, ctx->debug_file, |
154 | | " %i-%02i-%02i %02i:%02i:%02i.%03i", |
155 | | st.wYear, st.wMonth, st.wDay, |
156 | | st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); |
157 | | #else |
158 | 0 | sc_color_fprintf(SC_COLOR_FG_GREEN|SC_COLOR_BOLD, |
159 | 0 | ctx, ctx->debug_file, |
160 | 0 | "P:%lu; T:0x%lu", |
161 | 0 | (unsigned long)getpid(), |
162 | 0 | (unsigned long)pthread_self()); |
163 | 0 | gettimeofday (&tv, NULL); |
164 | 0 | tm = localtime (&tv.tv_sec); |
165 | 0 | strftime (time_string, sizeof(time_string), "%H:%M:%S", tm); |
166 | 0 | sc_color_fprintf(SC_COLOR_FG_GREEN, |
167 | 0 | ctx, ctx->debug_file, |
168 | 0 | " %s.%03ld", |
169 | 0 | time_string, |
170 | 0 | (long)tv.tv_usec / 1000); |
171 | 0 | #endif |
172 | |
|
173 | 0 | sc_color_fprintf(SC_COLOR_FG_YELLOW, |
174 | 0 | ctx, ctx->debug_file, |
175 | 0 | " ["); |
176 | 0 | sc_color_fprintf(SC_COLOR_FG_YELLOW|SC_COLOR_BOLD, |
177 | 0 | ctx, ctx->debug_file, |
178 | 0 | "%s", |
179 | 0 | ctx->app_name); |
180 | 0 | sc_color_fprintf(SC_COLOR_FG_YELLOW, |
181 | 0 | ctx, ctx->debug_file, |
182 | 0 | "] "); |
183 | |
|
184 | 0 | if (file != NULL) { |
185 | 0 | sc_color_fprintf(SC_COLOR_FG_YELLOW, |
186 | 0 | ctx, ctx->debug_file, |
187 | 0 | "%s:%d:%s: ", |
188 | 0 | file, line, func ? func : ""); |
189 | 0 | } |
190 | |
|
191 | 0 | sc_color_fprintf_va(color, ctx, ctx->debug_file, format, args); |
192 | 0 | if (strlen(format) == 0 || format[strlen(format) - 1] != '\n') |
193 | 0 | sc_color_fprintf(color, ctx, ctx->debug_file, "\n"); |
194 | 0 | fflush(ctx->debug_file); |
195 | |
|
196 | | #ifdef _WIN32 |
197 | | if (ctx->debug_file && (ctx->debug_file != stderr && ctx->debug_file != stdout)) |
198 | | fclose(ctx->debug_file); |
199 | | ctx->debug_file = NULL; |
200 | | #endif |
201 | 0 | } |
202 | | |
203 | | void _sc_debug(struct sc_context *ctx, int level, const char *format, ...) |
204 | 0 | { |
205 | 0 | va_list ap; |
206 | |
|
207 | 0 | va_start(ap, format); |
208 | 0 | sc_do_log_va(ctx, level, NULL, 0, NULL, 0, format, ap); |
209 | 0 | va_end(ap); |
210 | 0 | } |
211 | | |
212 | | void _sc_log(struct sc_context *ctx, const char *format, ...) |
213 | 0 | { |
214 | 0 | va_list ap; |
215 | |
|
216 | 0 | va_start(ap, format); |
217 | 0 | sc_do_log_va(ctx, SC_LOG_DEBUG_NORMAL, NULL, 0, NULL, 0, format, ap); |
218 | 0 | va_end(ap); |
219 | 0 | } |
220 | | |
221 | | void _sc_log_openssl(struct sc_context *ctx) |
222 | 0 | { |
223 | 0 | sc_do_log_openssl(ctx, SC_LOG_DEBUG_DEPS, NULL, 0, NULL); |
224 | 0 | } |
225 | | |
226 | | static int is_a_tty(FILE *fp) |
227 | 0 | { |
228 | 0 | if (fp != NULL) { |
229 | 0 | int fd = fileno(fp); |
230 | 0 | if (fd >= 0) { |
231 | | #ifdef _WIN32 |
232 | | HANDLE h = (HANDLE)_get_osfhandle(fd); |
233 | | if (h != INVALID_HANDLE_VALUE) { |
234 | | return GetFileType(h) == FILE_TYPE_CHAR; |
235 | | } |
236 | | #else |
237 | 0 | return isatty(fd); |
238 | 0 | #endif |
239 | 0 | } |
240 | 0 | } |
241 | 0 | return 0; |
242 | 0 | } |
243 | | |
244 | | #ifdef _WIN32 |
245 | | #define set_color(sc_color, win_color, vt100_color) \ |
246 | | do { if (colors & sc_color) { attr |= win_color; } } while (0) |
247 | | #else |
248 | | #define set_color(sc_color, win_color, vt100_color) \ |
249 | 0 | do { if (colors & sc_color) { fprintf(stream, vt100_color); } } while (0) |
250 | | #endif |
251 | | |
252 | | int sc_color_fprintf(int colors, struct sc_context *ctx, FILE * stream, const char * format, ...) |
253 | 0 | { |
254 | 0 | int r; |
255 | 0 | va_list ap; |
256 | |
|
257 | 0 | va_start(ap, format); |
258 | 0 | r = sc_color_fprintf_va(colors, ctx, stream, format, ap); |
259 | 0 | va_end(ap); |
260 | |
|
261 | 0 | return r; |
262 | 0 | } |
263 | | |
264 | | int sc_color_fprintf_va(int colors, struct sc_context *ctx, FILE * stream, const char *format, va_list args) |
265 | 0 | { |
266 | 0 | int r; |
267 | | #ifdef _WIN32 |
268 | | WORD old_attr = 0; |
269 | | int fd = stream ? fileno(stream) : -1; |
270 | | HANDLE handle = fd >= 0 ? (HANDLE) _get_osfhandle(fd) : INVALID_HANDLE_VALUE; |
271 | | #endif |
272 | |
|
273 | 0 | if (!is_a_tty(stream)) |
274 | 0 | colors = 0; |
275 | |
|
276 | 0 | if (colors && (!ctx || (!(ctx->flags & SC_CTX_FLAG_DISABLE_COLORS)))) { |
277 | | #ifdef _WIN32 |
278 | | WORD attr = 0; |
279 | | CONSOLE_SCREEN_BUFFER_INFO csbi; |
280 | | GetConsoleScreenBufferInfo(handle, &csbi); |
281 | | old_attr = csbi.wAttributes; |
282 | | #endif |
283 | 0 | set_color(SC_COLOR_FG_RED, |
284 | 0 | FOREGROUND_RED, |
285 | 0 | "\x1b[31m"); |
286 | 0 | set_color(SC_COLOR_FG_GREEN, |
287 | 0 | FOREGROUND_GREEN, |
288 | 0 | "\x1b[32m"); |
289 | 0 | set_color(SC_COLOR_FG_YELLOW, |
290 | 0 | FOREGROUND_GREEN|FOREGROUND_RED, |
291 | 0 | "\x1b[33m"); |
292 | 0 | set_color(SC_COLOR_FG_BLUE, |
293 | 0 | FOREGROUND_BLUE, |
294 | 0 | "\x1b[34m"); |
295 | 0 | set_color(SC_COLOR_FG_MAGENTA, |
296 | 0 | FOREGROUND_BLUE|FOREGROUND_RED, |
297 | 0 | "\x1b[35m"); |
298 | 0 | set_color(SC_COLOR_FG_CYAN, |
299 | 0 | FOREGROUND_BLUE|FOREGROUND_GREEN, |
300 | 0 | "\x1b[36m"); |
301 | 0 | set_color(SC_COLOR_BG_RED, |
302 | 0 | FOREGROUND_RED, |
303 | 0 | "\x1b[41m"); |
304 | 0 | set_color(SC_COLOR_BG_GREEN, |
305 | 0 | BACKGROUND_GREEN, |
306 | 0 | "\x1b[42m"); |
307 | 0 | set_color(SC_COLOR_BG_YELLOW, |
308 | 0 | BACKGROUND_GREEN|BACKGROUND_RED, |
309 | 0 | "\x1b[43m"); |
310 | 0 | set_color(SC_COLOR_BG_BLUE, |
311 | 0 | BACKGROUND_BLUE, |
312 | 0 | "\x1b[44m"); |
313 | 0 | set_color(SC_COLOR_BG_MAGENTA, |
314 | 0 | BACKGROUND_BLUE|BACKGROUND_RED, |
315 | 0 | "\x1b[45m"); |
316 | 0 | set_color(SC_COLOR_BG_CYAN, |
317 | 0 | BACKGROUND_BLUE|BACKGROUND_GREEN, |
318 | 0 | "\x1b[46m"); |
319 | 0 | set_color(SC_COLOR_BOLD, |
320 | 0 | FOREGROUND_INTENSITY, |
321 | 0 | "\x1b[1m"); |
322 | | #ifdef _WIN32 |
323 | | SetConsoleTextAttribute(handle, attr); |
324 | | #endif |
325 | 0 | } |
326 | |
|
327 | 0 | r = vfprintf(stream, format, args); |
328 | |
|
329 | 0 | if (colors && (!ctx || (!(ctx->flags & SC_CTX_FLAG_DISABLE_COLORS)))) { |
330 | | #ifdef _WIN32 |
331 | | SetConsoleTextAttribute(handle, old_attr); |
332 | | #else |
333 | 0 | fprintf(stream, "\x1b[0m"); |
334 | 0 | #endif |
335 | 0 | } |
336 | |
|
337 | 0 | return r; |
338 | 0 | } |
339 | | |
340 | | void _sc_debug_hex(sc_context_t *ctx, int type, const char *file, int line, |
341 | | const char *func, const char *label, const u8 *data, size_t len) |
342 | 409k | { |
343 | 409k | size_t blen; |
344 | 409k | char *buf; |
345 | 409k | const char *truncated = ""; |
346 | | |
347 | 409k | if (len > SC_MAX_EXT_APDU_BUFFER_SIZE) { |
348 | 0 | len = SC_MAX_EXT_APDU_BUFFER_SIZE; |
349 | 0 | truncated = "truncated to "; |
350 | 0 | } |
351 | 409k | blen = len * 5 + 128; |
352 | | |
353 | 409k | buf = malloc(blen); |
354 | 409k | if (buf == NULL) |
355 | 0 | return; |
356 | | |
357 | 409k | sc_hex_dump(data, len, buf, blen); |
358 | | |
359 | 409k | if (label) |
360 | 409k | sc_do_log(ctx, type, file, line, func, "\n%s (%s%zu byte%s):\n%s", |
361 | 409k | label, truncated, len, len == 1 ? "" : "s", buf); |
362 | 0 | else |
363 | 0 | sc_do_log(ctx, type, file, line, func, "%s%zu byte%s:\n%s", |
364 | 0 | truncated, len, len == 1 ? "" : "s", buf); |
365 | | |
366 | 409k | free(buf); |
367 | 409k | } |
368 | | |
369 | | void sc_hex_dump(const u8 * in, size_t count, char *buf, size_t len) |
370 | 410k | { |
371 | 410k | char *p = buf; |
372 | 410k | size_t p_len = len; |
373 | 410k | int lines = 0; |
374 | | |
375 | 410k | if (buf == NULL || len == 0 || (in == NULL && count != 0)) { |
376 | 0 | return; |
377 | 0 | } |
378 | 410k | buf[0] = 0; |
379 | 410k | if (count == 0) |
380 | 82.9k | return; |
381 | 327k | if (count > (SIZE_MAX - 15) / 65 || ((count + 15) / 16) * 65 >= len) |
382 | 0 | return; |
383 | 1.86M | while (count && p_len > 0) { |
384 | 1.53M | char ascbuf[17]; |
385 | 1.53M | size_t i; |
386 | 1.53M | size_t asc_len; |
387 | | |
388 | 23.6M | for (i = 0; i < count && i < 16; i++) { |
389 | 22.1M | if (p_len < 4) |
390 | 0 | return; |
391 | 22.1M | snprintf(p, p_len, "%02X ", *in); |
392 | 22.1M | if (isprint(*in)) |
393 | 11.6M | ascbuf[i] = *in; |
394 | 10.4M | else |
395 | 10.4M | ascbuf[i] = '.'; |
396 | 22.1M | p += 3; |
397 | 22.1M | p_len -= 3; |
398 | 22.1M | in++; |
399 | 22.1M | } |
400 | 1.53M | count -= i; |
401 | 1.53M | ascbuf[i] = 0; |
402 | 1.53M | asc_len = i; |
403 | 1.91M | for (; i < 16 && lines; i++) { |
404 | 380k | if (p_len < 4) |
405 | 0 | return; |
406 | 380k | strlcat(p, " ", p_len); |
407 | 380k | p += 3; |
408 | 380k | p_len -= 3; |
409 | 380k | } |
410 | 1.53M | if (p_len < asc_len + 2) |
411 | 0 | return; |
412 | 1.53M | snprintf(p, p_len, "%s\n", ascbuf); |
413 | 1.53M | p += asc_len + 1; |
414 | 1.53M | p_len -= asc_len + 1; |
415 | 1.53M | lines++; |
416 | 1.53M | } |
417 | 327k | } |
418 | | |
419 | | const char * |
420 | | sc_dump_hex(const u8 * in, size_t count) |
421 | 191k | { |
422 | 191k | static char dump_buf[0x1000]; |
423 | 191k | size_t ii, size = sizeof(dump_buf) - 0x10; |
424 | 191k | size_t offs = 0; |
425 | | |
426 | 191k | memset(dump_buf, 0, sizeof(dump_buf)); |
427 | 191k | if (in == NULL) |
428 | 0 | return dump_buf; |
429 | | |
430 | 669k | for (ii=0; ii<count; ii++) { |
431 | 478k | if (ii && !(ii%16)) { |
432 | 3.88k | if (!(ii%48)) |
433 | 1.18k | snprintf(dump_buf + offs, size - offs, "\n"); |
434 | 2.69k | else |
435 | 2.69k | snprintf(dump_buf + offs, size - offs, " "); |
436 | 3.88k | offs = strlen(dump_buf); |
437 | 3.88k | } |
438 | | |
439 | 478k | snprintf(dump_buf + offs, size - offs, "%02X", *(in + ii)); |
440 | 478k | offs += 2; |
441 | | |
442 | 478k | if (offs > size) |
443 | 0 | break; |
444 | 478k | } |
445 | | |
446 | 191k | if (ii<count) |
447 | 0 | snprintf(dump_buf + offs, sizeof(dump_buf) - offs, "....\n"); |
448 | | |
449 | 191k | return dump_buf; |
450 | 191k | } |
451 | | |
452 | | const char * |
453 | | sc_dump_oid(const struct sc_object_id *oid) |
454 | 4.57k | { |
455 | 4.57k | static char dump_buf[SC_MAX_OBJECT_ID_OCTETS * 20]; |
456 | 4.57k | size_t ii; |
457 | | |
458 | 4.57k | memset(dump_buf, 0, sizeof(dump_buf)); |
459 | 4.57k | if (oid) |
460 | 25.0k | for (ii=0; ii<SC_MAX_OBJECT_ID_OCTETS && oid->value[ii] != -1; ii++) |
461 | 20.4k | snprintf(dump_buf + strlen(dump_buf), sizeof(dump_buf) - strlen(dump_buf), "%s%i", (ii ? "." : ""), oid->value[ii]); |
462 | | |
463 | 4.57k | return dump_buf; |
464 | 4.57k | } |