]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
Initialization of crypto libraries moved outside main gnutls code.
authorNikos Mavrogiannopoulos <nmav@gnutls.org>
Tue, 29 Jun 2010 16:05:18 +0000 (18:05 +0200)
committerNikos Mavrogiannopoulos <nmav@gnutls.org>
Tue, 29 Jun 2010 16:16:36 +0000 (18:16 +0200)
lib/gcrypt/Makefile.am
lib/gcrypt/init.c [new file with mode: 0644]
lib/gnutls_errors.c
lib/gnutls_global.c
lib/gnutls_global.h
lib/includes/gnutls/gnutls.h.in
lib/locks.c
lib/locks.h
lib/nettle/Makefile.am
lib/nettle/init.c [new file with mode: 0644]
lib/nettle/rnd.c

index c7efbe514d065b5d52b6e30e32820ef8a5a665ce..0b3d5741ad1fa2bd711aca508afaf783f1553492 100644 (file)
@@ -31,4 +31,4 @@ AM_CPPFLAGS = \
 
 noinst_LTLIBRARIES = libcrypto.la
 
-libcrypto_la_SOURCES = pk.c mpi.c mac.c cipher.c rnd.c
+libcrypto_la_SOURCES = pk.c mpi.c mac.c cipher.c rnd.c init.c
diff --git a/lib/gcrypt/init.c b/lib/gcrypt/init.c
new file mode 100644 (file)
index 0000000..a511ac5
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * Copyright (C) 2010 Free Software Foundation, Inc.
+ *
+ * Author: Nikos Mavrogiannopoulos
+ *
+ * This file is part of GnuTLS.
+ *
+ * The GnuTLS is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+ * USA
+ *
+ */
+
+#include <gnutls_int.h>
+#include <gnutls_errors.h>
+#include <gcrypt.h>
+#include <locks.h>
+
+#define GNUTLS_MIN_LIBGCRYPT_VERSION "1.2.4"
+
+/* Functions that refer to the initialization of the libgcrypt library.
+ */
+
+static struct gcry_thread_cbs gct = {
+  .option = (GCRY_THREAD_OPTION_PTHREAD | (GCRY_THREAD_OPTION_VERSION << 8)),
+  .init = NULL,
+  .select = NULL,
+  .waitpid = NULL,
+  .accept = NULL,
+  .connect = NULL,
+  .sendmsg = NULL,
+  .recvmsg = NULL,
+};
+
+static int wrap_gcry_mutex_lock(void** m)
+{
+  return gnutls_mutex_lock(*m);
+}
+
+static int wrap_gcry_mutex_unlock(void** m)
+{
+  return gnutls_mutex_unlock(*m);
+}
+
+static int wrap_gcry_mutex_deinit(void** m)
+{
+  gnutls_mutex_deinit(*m);
+  return 0;
+}
+
+void _gnutls_gcry_register_mutexes(void)
+{
+
+}
+
+int gnutls_crypto_init(void)
+{
+  /* Initialize libgcrypt if it hasn't already been initialized. */
+  if (gcry_control (GCRYCTL_ANY_INITIALIZATION_P) == 0)
+    {
+      const char *p;
+
+      if (gnutls_mutex_init != NULL) 
+        {
+         gct.mutex_init = gnutls_mutex_init;
+         gct.mutex_destroy = wrap_gcry_mutex_deinit;
+         gct.mutex_lock = wrap_gcry_mutex_lock;
+         gct.mutex_unlock = wrap_gcry_mutex_unlock;
+
+         gcry_control (GCRYCTL_SET_THREAD_CBS, &gct);
+        }
+
+      p = gcry_check_version (GNUTLS_MIN_LIBGCRYPT_VERSION);
+
+      if (p == NULL)
+       {
+         gnutls_assert ();
+         _gnutls_debug_log ("Checking for libgcrypt failed: %s < %s\n",
+                            gcry_check_version (NULL),
+                            GNUTLS_MIN_LIBGCRYPT_VERSION);
+         return GNUTLS_E_INCOMPATIBLE_GCRYPT_LIBRARY;
+       }
+
+      /* for gcrypt in order to be able to allocate memory */
+      gcry_control (GCRYCTL_DISABLE_SECMEM, NULL, 0);
+
+      gcry_control (GCRYCTL_INITIALIZATION_FINISHED, NULL, 0);
+      
+      gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
+    }
+
+   return 0;
+}
+
index 9c71cd1e6d4e27c0242dd25c4ec5eb98ba1fc296..22ff77e490165037b8272c42ff0a2a809fde973c 100644 (file)
@@ -211,6 +211,8 @@ static const gnutls_error_entry error_algorithms[] = {
               GNUTLS_E_OPENPGP_UID_REVOKED, 1),
   ERROR_ENTRY (N_("Error loading the keyring."),
               GNUTLS_E_OPENPGP_KEYRING_ERROR, 1),
+  ERROR_ENTRY (N_("The initialization of crypto backend has failed."),
+              GNUTLS_E_CRYPTO_INIT_FAILED, 1),
   ERROR_ENTRY (N_("The initialization of LZO has failed."),
               GNUTLS_E_LZO_INIT_FAILED, 1),
   ERROR_ENTRY (N_("No supported compression algorithms have been found."),
index 1f5dc9ddb7260c82f986368366ac538d8e0fe150..223abdca3b70be0e0922a14ddc68207086ce7f3f 100644 (file)
 #include <libtasn1.h>
 #include <gnutls_dh.h>
 #include <random.h>
-#ifndef HAVE_LIBNETTLE
-#include <gcrypt.h>
-#define GNUTLS_MIN_LIBGCRYPT_VERSION "1.2.4"
-
-#endif
 #include <gnutls/pkcs11.h>
 
 #include <gnutls_extensions.h> /* for _gnutls_ext_init */
@@ -103,7 +98,6 @@ gnutls_global_set_log_level (int level)
  * (malloc(), free()), are used by gnutls, to allocate both sensitive
  * and not sensitive data.  This function is provided to set the
  * memory allocation functions to something other than the defaults
- * (ie the gcrypt allocation functions).
  *
  * This function must be called before gnutls_global_init() is called.
  * This function is not thread safe.
@@ -151,8 +145,8 @@ static int _gnutls_init = 0;
  * shared by gnutls session structures.  You should call
  * gnutls_global_deinit() when gnutls usage is no longer needed
  *
- * Note that this function will also initialize libgcrypt, if it has
- * not been initialized before.  
+ * Note that this function will also initialize the underlying crypto
+ * backend, if it has not been initialized before.  
  *
  * This function increment a global counter, so that
  * gnutls_global_deinit() only releases resources when it has been
@@ -186,33 +180,13 @@ gnutls_global_init (void)
 
   bindtextdomain (PACKAGE, LOCALEDIR);
 
-#ifndef HAVE_LIBNETTLE
-  /* Initialize libgcrypt if it hasn't already been initialized. */
-  if (gcry_control (GCRYCTL_ANY_INITIALIZATION_P) == 0)
+  res = gnutls_crypto_init();
+  if (res != 0) 
     {
-      const char *p;
-
-      _gnutls_gcry_register_mutexes();
-
-      p = gcry_check_version (GNUTLS_MIN_LIBGCRYPT_VERSION);
-
-      if (p == NULL)
-       {
-         gnutls_assert ();
-         _gnutls_debug_log ("Checking for libgcrypt failed: %s < %s\n",
-                            gcry_check_version (NULL),
-                            GNUTLS_MIN_LIBGCRYPT_VERSION);
-         return GNUTLS_E_INCOMPATIBLE_GCRYPT_LIBRARY;
-       }
-
-      /* for gcrypt in order to be able to allocate memory */
-      gcry_control (GCRYCTL_DISABLE_SECMEM, NULL, 0);
-
-      gcry_control (GCRYCTL_INITIALIZATION_FINISHED, NULL, 0);
-      
-      gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
+      gnutls_assert();
+      return GNUTLS_E_CRYPTO_INIT_FAILED;
     }
-#endif
+
   /* initialize ASN.1 parser
    * This should not deal with files in the final
    * version.
index 081c20d464a8d74df1fae0121af54fcb44559cec..5e6265d39b2be0b409bef6d5f264819ea8bdfaea 100644 (file)
@@ -27,6 +27,7 @@
 # define GNUTLS_GLOBAL_H
 
 #include <libtasn1.h>
+#include <gnutls/gnutls.h>
 
 int gnutls_is_secure_memory (const void *mem);
 
@@ -42,5 +43,6 @@ extern ASN1_TYPE _gnutls_gnutls_asn;
 
 extern gnutls_log_func _gnutls_log_func;
 extern int _gnutls_log_level;
+extern int gnutls_crypto_init(void);
 
 #endif
index bfd876a7e4916b22236d5d34891ca2c700a1ef5e..f115691f37c139ae83f675d94bf5472ee822551d 100644 (file)
@@ -1760,6 +1760,8 @@ extern "C"
 #define GNUTLS_E_PKCS11_TOKEN_ERROR -316
 #define GNUTLS_E_PKCS11_USER_ERROR -317
 
+#define GNUTLS_E_CRYPTO_INIT_FAILED -318
+
 #define GNUTLS_E_UNIMPLEMENTED_FEATURE -1250
 
 
index 3b8a66d23407fe03c791c25c6b275d62b92240e7..336fe1c1c1ece72d8b5dc0bf51c8cc2887b0a6d8 100644 (file)
@@ -166,44 +166,3 @@ void gnutls_global_set_mutex(mutex_init_func init, mutex_deinit_func deinit,
   gnutls_mutex_unlock = unlock;
 }
 
-#ifndef HAVE_LIBNETTLE
-static struct gcry_thread_cbs gct = {
-  .option = (GCRY_THREAD_OPTION_PTHREAD | (GCRY_THREAD_OPTION_VERSION << 8)),
-  .init = NULL,
-  .select = NULL,
-  .waitpid = NULL,
-  .accept = NULL,
-  .connect = NULL,
-  .sendmsg = NULL,
-  .recvmsg = NULL,
-};
-
-static int wrap_gcry_mutex_lock(void** m)
-{
-  return gnutls_mutex_lock(*m);
-}
-
-static int wrap_gcry_mutex_unlock(void** m)
-{
-  return gnutls_mutex_unlock(*m);
-}
-
-static int wrap_gcry_mutex_deinit(void** m)
-{
-  gnutls_mutex_deinit(*m);
-  return 0;
-}
-
-void _gnutls_gcry_register_mutexes(void)
-{
-       if (gnutls_mutex_init != NULL) {
-               gct.mutex_init = gnutls_mutex_init;
-               gct.mutex_destroy = wrap_gcry_mutex_deinit;
-               gct.mutex_lock = wrap_gcry_mutex_lock;
-               gct.mutex_unlock = wrap_gcry_mutex_unlock;
-
-               gcry_control (GCRYCTL_SET_THREAD_CBS, &gct);
-       }
-}
-
-#endif
index 4f8c35581befeeea5a29d8bac91134ed63f77c74..0a91d2ffece9e449243559082688bac1cca4f89a 100644 (file)
@@ -9,8 +9,4 @@ extern mutex_deinit_func gnutls_mutex_deinit;
 extern mutex_lock_func gnutls_mutex_lock;
 extern mutex_unlock_func gnutls_mutex_unlock;
 
-# ifndef HAVE_LIBNETTLE
-void _gnutls_gcry_register_mutexes(void);
-# endif
-
 #endif
index c7efbe514d065b5d52b6e30e32820ef8a5a665ce..0b3d5741ad1fa2bd711aca508afaf783f1553492 100644 (file)
@@ -31,4 +31,4 @@ AM_CPPFLAGS = \
 
 noinst_LTLIBRARIES = libcrypto.la
 
-libcrypto_la_SOURCES = pk.c mpi.c mac.c cipher.c rnd.c
+libcrypto_la_SOURCES = pk.c mpi.c mac.c cipher.c rnd.c init.c
diff --git a/lib/nettle/init.c b/lib/nettle/init.c
new file mode 100644 (file)
index 0000000..799d276
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2010 Free Software Foundation, Inc.
+ *
+ * Author: Nikos Mavrogiannopoulos
+ *
+ * This file is part of GnuTLS.
+ *
+ * The GnuTLS is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+ * USA
+ *
+ */
+
+#include <gnutls_int.h>
+#include <gnutls_errors.h>
+#include <gnutls_num.h>
+#include <gnutls_mpi.h>
+#include <gcrypt.h>
+
+/* Functions that refer to the initialization of the libgcrypt library.
+ */
+
+int gnutls_crypto_init(void)
+{
+   return 0;
+}
+
index 2ae544fdf090a0eb9a098888e1102afea6f6af88..ac24235991b0e9255ffdb20724ced5752feca9d2 100644 (file)
@@ -35,8 +35,7 @@
 #include <sys/stat.h>
 #include <sys/time.h>
 #include <fcntl.h>
-
-#include <pthread.h>
+#include <locks.h>
 
 #define SOURCES 2