2 * Copyright (C) 1999, 2001, 2002, 2003, 2007,
3 * 2008 Free Software Foundation, Inc.
5 * This file is part of Libgcrypt.
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
12 * Libgcrypt 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
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
33 static int verbosity_level = 0;
35 /* Prevent compiler from optimizing away the call to memset by accessing
36 memset through volatile pointer. */
37 static void *(*volatile memset_ptr)(void *, int, size_t) = (void *)memset;
39 static void (*fatal_error_handler)(void*,int, const char*) = NULL;
40 static void *fatal_error_handler_value = 0;
41 static void (*log_handler)(void*,int, const char*, va_list) = NULL;
42 static void *log_handler_value = 0;
44 static const char *(*user_gettext_handler)( const char * ) = NULL;
47 _gcry_set_gettext_handler (const char *(*f)(const char*))
49 user_gettext_handler = f;
54 _gcry_gettext( const char *key )
56 if( user_gettext_handler )
57 return user_gettext_handler( key );
58 /* FIXME: switch the domain to gnupg and restore later */
63 _gcry_set_fatalerror_handler( void (*fnc)(void*,int, const char*), void *value)
65 fatal_error_handler_value = value;
66 fatal_error_handler = fnc;
70 write2stderr( const char *s )
72 /* Dummy variable to silence gcc warning. */
73 int res = write( 2, s, strlen(s) );
78 * This function is called for fatal errors. A caller might want to
79 * set his own handler because this function simply calls abort().
82 _gcry_fatal_error (int rc, const char *text)
84 if ( !text ) /* get a default text */
85 text = gpg_strerror (rc);
87 if (fatal_error_handler && !fips_mode () )
88 fatal_error_handler (fatal_error_handler_value, rc, text);
90 fips_signal_fatal_error (text);
91 write2stderr("\nFatal error: ");
99 _gcry_set_log_handler (void (*f)(void*,int, const char*, va_list), void *opaque)
102 log_handler_value = opaque;
106 _gcry_set_log_verbosity( int level )
108 verbosity_level = level;
112 _gcry_log_verbosity( int level )
114 return verbosity_level >= level;
118 * This is our log function which prints all log messages to stderr or
119 * using the function defined with gcry_set_log_handler().
122 _gcry_logv( int level, const char *fmt, va_list arg_ptr )
125 log_handler (log_handler_value, level, fmt, arg_ptr);
130 case GCRY_LOG_CONT: break;
131 case GCRY_LOG_INFO: break;
132 case GCRY_LOG_WARN: break;
133 case GCRY_LOG_ERROR: break;
134 case GCRY_LOG_FATAL: fputs("Fatal: ",stderr ); break;
135 case GCRY_LOG_BUG: fputs("Ohhhh jeeee: ", stderr); break;
136 case GCRY_LOG_DEBUG: fputs("DBG: ", stderr ); break;
137 default: fprintf(stderr,"[Unknown log level %d]: ", level ); break;
139 vfprintf(stderr,fmt,arg_ptr) ;
142 if ( level == GCRY_LOG_FATAL || level == GCRY_LOG_BUG )
144 fips_signal_fatal_error ("internal error (fatal or bug)");
145 _gcry_secmem_term ();
152 _gcry_log( int level, const char *fmt, ... )
156 va_start( arg_ptr, fmt ) ;
157 _gcry_logv( level, fmt, arg_ptr );
162 #if defined(JNLIB_GCC_M_FUNCTION) || __STDC_VERSION__ >= 199901L
164 _gcry_bug( const char *file, int line, const char *func )
166 _gcry_log( GCRY_LOG_BUG,
167 ("... this is a bug (%s:%d:%s)\n"), file, line, func );
168 abort(); /* never called, but it makes the compiler happy */
171 _gcry_assert_failed (const char *expr, const char *file, int line,
174 _gcry_log (GCRY_LOG_BUG,
175 ("Assertion `%s' failed (%s:%d:%s)\n"), expr, file, line, func );
176 abort(); /* Never called, but it makes the compiler happy. */
180 _gcry_bug( const char *file, int line )
182 _gcry_log( GCRY_LOG_BUG,
183 _("you found a bug ... (%s:%d)\n"), file, line);
184 abort(); /* never called, but it makes the compiler happy */
187 _gcry_assert_failed (const char *expr, const char *file, int line)
189 _gcry_log (GCRY_LOG_BUG,
190 ("Assertion `%s' failed (%s:%d)\n"), expr, file, line);
191 abort(); /* Never called, but it makes the compiler happy. */
196 _gcry_log_info( const char *fmt, ... )
200 va_start( arg_ptr, fmt ) ;
201 _gcry_logv( GCRY_LOG_INFO, fmt, arg_ptr );
206 _gcry_log_error( const char *fmt, ... )
210 va_start( arg_ptr, fmt ) ;
211 _gcry_logv( GCRY_LOG_ERROR, fmt, arg_ptr );
217 _gcry_log_fatal( const char *fmt, ... )
221 va_start( arg_ptr, fmt ) ;
222 _gcry_logv( GCRY_LOG_FATAL, fmt, arg_ptr );
224 abort(); /* never called, but it makes the compiler happy */
228 _gcry_log_bug( const char *fmt, ... )
232 va_start( arg_ptr, fmt ) ;
233 _gcry_logv( GCRY_LOG_BUG, fmt, arg_ptr );
235 abort(); /* never called, but it makes the compiler happy */
239 _gcry_log_debug( const char *fmt, ... )
243 va_start( arg_ptr, fmt ) ;
244 _gcry_logv( GCRY_LOG_DEBUG, fmt, arg_ptr );
250 _gcry_log_printf (const char *fmt, ...)
256 va_start( arg_ptr, fmt ) ;
257 _gcry_logv (GCRY_LOG_CONT, fmt, arg_ptr);
263 /* Helper for _gcry_log_printhex and _gcry_log_printmpi. */
265 do_printhex (const char *text, const char *text2,
266 const void *buffer, size_t length)
274 log_debug ("%s:%s", text, text2);
275 if (text2[1] == '[' && length && buffer)
277 /* Start with a new line so that we get nice output for
283 log_debug ("%*s ", (int)strlen(text), "");
286 if (length && buffer)
288 const unsigned char *p = buffer;
289 for (; length--; p++)
291 log_printf ("%02x", *p);
292 if (wrap && ++cnt == 32 && length)
295 log_printf (" \\\n");
296 log_debug ("%*s %*s",
297 (int)strlen(text), "", (int)strlen(text2), "");
306 /* Print a hexdump of BUFFER. With TEXT of NULL print just the raw
307 dump without any wrappping, with TEXT an empty string, print a
308 trailing linefeed, otherwise print an entire debug line. */
310 _gcry_log_printhex (const char *text, const void *buffer, size_t length)
312 do_printhex (text, " ", buffer, length);
316 /* Print MPI in hex notation. To make clear that the output is an MPI
317 a sign is always printed. With TEXT of NULL print just the raw dump
318 without any wrapping, with TEXT an empty string, print a trailing
319 linefeed, otherwise print an entire debug line. */
321 _gcry_log_printmpi (const char *text, gcry_mpi_t mpi)
323 unsigned char *rawmpi;
324 unsigned int rawmpilen;
328 do_printhex (text? text:" ", " (null)", NULL, 0);
329 else if (mpi_is_opaque (mpi))
332 const unsigned char *p;
335 p = mpi_get_opaque (mpi, &nbits);
336 snprintf (prefix, sizeof prefix, " [%u bit]", nbits);
337 do_printhex (text? text:" ", prefix, p, (nbits+7)/8);
341 rawmpi = _gcry_mpi_get_buffer (mpi, 0, &rawmpilen, &sign);
343 do_printhex (text? text:" ", " [out of core]", NULL, 0);
347 do_printhex (text, sign? "-":"+", "", 1);
349 do_printhex (text, sign? "-":"+", rawmpi, rawmpilen);
357 count_closing_parens (const char *p)
364 else if (!strchr ("\n \t", *p))
371 /* Print SEXP in human readabale format. With TEXT of NULL print just the raw
372 dump without any wrappping, with TEXT an empty string, print a
373 trailing linefeed, otherwise print the full debug output. */
375 _gcry_log_printsxp (const char *text, gcry_sexp_t sexp)
381 if ((with_lf = !!strchr (text, '\n')))
382 log_debug ("%s", text);
384 log_debug ("%s: ", text);
394 size = sexp_sprint (sexp, GCRYSEXP_FMT_ADVANCED, NULL, 0);
395 p = buf = xmalloc (size);
396 sexp_sprint (sexp, GCRYSEXP_FMT_ADVANCED, buf, size);
401 log_debug ("%*s ", text?(int)strlen(text):0, "");
404 pend = strchr (p, '\n');
405 size = pend? (pend - p) : strlen (p);
407 log_debug ("%.*s", (int)size, p);
409 log_printf ("%.*s", (int)size, p);
414 n_closing = count_closing_parens (p);
432 * Tokenize STRING using the set of delimiters in DELIM. Leading
433 * white spaces are removed from all tokens. The caller must xfree
436 * Returns: A malloced and NULL delimited array with the tokens. On
437 * memory error NULL is returned and ERRNO is set.
440 _gcry_strtokenize (const char *string, const char *delim)
448 char const ws[] = " \t\v\f\r\n";
453 /* Count the number of fields. */
454 for (fields = 1, s = strpbrk (string, delim); s; s = strpbrk (s + 1, delim))
456 fields++; /* Add one for the terminating NULL. */
458 /* Allocate an array for all fields, a terminating NULL, and space
459 for a copy of the string. */
460 bytes = fields * sizeof *result;
461 if (bytes / sizeof *result != fields)
463 gpg_err_set_errno (ENOMEM);
466 n = strlen (string) + 1;
470 gpg_err_set_errno (ENOMEM);
473 result = xtrymalloc (bytes);
476 buffer = (char*)(result + fields);
478 /* Copy and parse the string. */
479 strcpy (buffer, string);
480 for (n = 0, p = buffer; (pend = strpbrk (p, delim)); p = pend + 1)
483 while (strchr (ws, *(byte*)p))
485 for (px = pend - 1; px >= p && strchr (ws, *(byte*)px); px--)
489 while (*p && strchr (ws, *(byte*)p))
491 for (px = p + strlen (p) - 1; px >= p && strchr (ws, *(byte*)px); px--)
493 /* Traling spaces may result in an empty field. We do not want to
495 result[n++] = *p? p : NULL;
498 gcry_assert ((char*)(result + n + 1) == buffer);
505 _gcry_fast_wipememory (void *ptr, size_t len)
507 /* Note: This function is called from wipememory/wipememory2 only if LEN
508 is large or unknown at compile time. New wipe function alternatives
509 need to be checked before adding to this function. New implementations
510 need to be faster than wipememory/wipememory2 macros in 'misc.h'.
512 Following implementations were found to have suboptimal performance:
514 - [_WIN32/mingw32] SecureZeroMemory; Inline function, equivalent to
515 volatile byte buffer set: while(buflen--) (volatile char *)(buf++)=set;
517 #ifdef HAVE_EXPLICIT_BZERO
518 explicit_bzero (ptr, len);
520 memset_ptr (ptr, 0, len);
526 _gcry_fast_wipememory2 (void *ptr, int set, size_t len)
528 #ifdef HAVE_EXPLICIT_BZERO
531 explicit_bzero (ptr, len);
536 memset_ptr (ptr, set, len);
541 __gcry_burn_stack (unsigned int bytes)
544 /* (bytes == 0 ? 1 : bytes) == (!bytes + bytes) */
545 unsigned int buflen = ((!bytes + bytes) + 63) & ~63;
548 _gcry_fast_wipememory (buf, buflen);
550 volatile char buf[64];
552 _gcry_fast_wipememory (buf, sizeof buf);
554 if (bytes > sizeof buf)
555 _gcry_burn_stack (bytes - sizeof buf);
559 #ifndef HAVE_GCC_ASM_VOLATILE_MEMORY
561 __gcry_burn_stack_dummy (void)
567 _gcry_divide_by_zero (void)
569 gpg_err_set_errno (EDOM);
570 _gcry_fatal_error (gpg_err_code_from_errno (errno), "divide by zero");