void
egg_libgcrypt_initialize (void)
{
	static volatile gsize gcrypt_initialized = 0;
	unsigned seed;

	if (g_once_init_enter (&gcrypt_initialized)) {
		
		/* Only initialize libgcrypt if it hasn't already been initialized */
		if (!gcry_control (GCRYCTL_INITIALIZATION_FINISHED_P)) {
#if GCRYPT_VERSION_NUMBER < 0x010600
			gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
#endif
			gcry_check_version (LIBGCRYPT_VERSION);
			gcry_set_log_handler (log_handler, NULL);
			gcry_set_outofcore_handler (no_mem_handler, NULL);
			gcry_set_fatalerror_handler (fatal_handler, NULL);
			gcry_set_allocation_handler ((gcry_handler_alloc_t)g_malloc, 
			                             (gcry_handler_alloc_t)egg_secure_alloc, 
			                             egg_secure_check, 
			                             (gcry_handler_realloc_t)egg_secure_realloc, 
			                             egg_secure_free);
			gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
		}
		
		gcry_create_nonce (&seed, sizeof (seed));
		srand (seed);

		g_once_init_leave (&gcrypt_initialized, 1);
	}
}
Beispiel #2
0
void
gc_set_allocators (gc_malloc_t func_malloc,
                   gc_malloc_t secure_malloc,
                   gc_secure_check_t secure_check,
                   gc_realloc_t func_realloc, gc_free_t func_free)
{
    gcry_set_allocation_handler (func_malloc, secure_malloc, secure_check,
                                 func_realloc, func_free);
}
Beispiel #3
0
void otrl_mem_init(void)
{
    header_size = 8;
#ifdef OTRL_MEM_MAGIC
    if (header_size < 2*sizeof(size_t)) {
	header_size = 2*sizeof(size_t);
    }
#else
    if (header_size < sizeof(size_t)) {
	header_size = sizeof(size_t);
    }
#endif

    gcry_set_allocation_handler(
	    otrl_mem_malloc,
	    otrl_mem_malloc,
	    otrl_mem_is_secure,
	    otrl_mem_realloc,
	    otrl_mem_free
	);
}
Beispiel #4
0
/**
  * gnutls_global_init - This function initializes the global data to defaults.
  *
  * This function initializes the global data to defaults.
  * Every gnutls application has a global data which holds common parameters
  * shared by gnutls session structures.
  * You must call gnutls_global_deinit() when gnutls usage is no longer needed
  * Returns zero on success.
  *
  * Note that this function will also initialize libgcrypt, if it has not
  * been initialized before. Thus if you want to manually initialize libgcrypt
  * you must do it before calling this function. This is useful in cases you 
  * want to disable libgcrypt's internal lockings etc.
  *
  * This function increment a global counter, so that
  * gnutls_global_deinit() only releases resources when it has been
  * called as many times as gnutls_global_init().  This is useful when
  * GnuTLS is used by more than one library in an application.  This
  * function can be called many times, but will only do something the
  * first time.
  *
  * Note!  This function is not thread safe.  If two threads call this
  * function simultaneously, they can cause a race between checking
  * the global counter and incrementing it, causing both threads to
  * execute the library initialization code.  That would lead to a
  * memory leak.  To handle this, your application could invoke this
  * function after aquiring a thread mutex.  To ignore the potential
  * memory leak is also an option.
  *
  **/
int
gnutls_global_init (void)
{
  int result = 0;
  int res;
  char c;

  if (_gnutls_init++)
    goto out;

#if HAVE_WINSOCK
  {
    WORD requested;
    WSADATA data;
    int err;

    requested = MAKEWORD (1, 1);
    err = WSAStartup (requested, &data);
    if (err != 0)
      {
	_gnutls_debug_log ("WSAStartup failed: %d.\n", err);
	return GNUTLS_E_LIBRARY_VERSION_MISMATCH;
      }

    if (data.wVersion < requested)
      {
	_gnutls_debug_log ("WSAStartup version check failed (%d < %d).\n",
			   data.wVersion, requested);
	WSACleanup ();
	return GNUTLS_E_LIBRARY_VERSION_MISMATCH;
      }
  }
#endif

  bindtextdomain (PACKAGE, LOCALEDIR);

  if (gcry_control (GCRYCTL_ANY_INITIALIZATION_P) == 0)
    {
      const char *p;
      p = strchr (GNUTLS_GCRYPT_VERSION, ':');
      if (p == NULL)
	p = GNUTLS_GCRYPT_VERSION;
      else
	p++;

      if (gcry_check_version (p) == NULL)
	{
	  gnutls_assert ();
	  _gnutls_debug_log ("Checking for libgcrypt failed '%s'\n", p);
	  return GNUTLS_E_INCOMPATIBLE_GCRYPT_LIBRARY;
	}

      /* for gcrypt in order to be able to allocate memory */
      gcry_set_allocation_handler (gnutls_malloc, gnutls_secure_malloc,
				   _gnutls_is_secure_memory, gnutls_realloc,
				   gnutls_free);

      /* gcry_control (GCRYCTL_DISABLE_INTERNAL_LOCKING, NULL, 0); */

      gcry_control (GCRYCTL_INITIALIZATION_FINISHED, NULL, 0);

#ifdef DEBUG
      /* applications may want to override that, so we only use
       * it in debugging mode.
       */
      gcry_set_log_handler (_gnutls_gcry_log_handler, NULL);
#endif
    }

  if (gc_init () != GC_OK)
    {
      gnutls_assert ();
      _gnutls_debug_log ("Initializing crypto backend failed\n");
      return GNUTLS_E_INCOMPATIBLE_CRYPTO_LIBRARY;
    }

  /* for gcrypt in order to be able to allocate memory */
  gc_set_allocators (gnutls_malloc, gnutls_secure_malloc,
		     _gnutls_is_secure_memory, gnutls_realloc, gnutls_free);

#ifdef DEBUG
  gnutls_global_set_log_function (dlog);
#endif

  /* initialize parser 
   * This should not deal with files in the final
   * version.
   */

  if (asn1_check_version (GNUTLS_LIBTASN1_VERSION) == NULL)
    {
      gnutls_assert ();
      return GNUTLS_E_INCOMPATIBLE_LIBTASN1_LIBRARY;
    }

  res = asn1_array2tree (pkix_asn1_tab, &_gnutls_pkix1_asn, NULL);
  if (res != ASN1_SUCCESS)
    {
      result = _gnutls_asn2err (res);
      goto out;
    }

  res = asn1_array2tree (gnutls_asn1_tab, &_gnutls_gnutls_asn, NULL);
  if (res != ASN1_SUCCESS)
    {
      asn1_delete_structure (&_gnutls_pkix1_asn);
      result = _gnutls_asn2err (res);
      goto out;
    }
    
  /* Initialize the gcrypt (if used random generator) */
  gc_pseudo_random (&c, 1);

out:
  return result;
}