From: Nikos Mavrogiannopoulos Date: Tue, 29 Jun 2010 16:05:18 +0000 (+0200) Subject: Initialization of crypto libraries moved outside main gnutls code. X-Git-Tag: gnutls_2_11_3~134 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=223a78e601447d4d82be07da7e1506809d7d0e9a;p=thirdparty%2Fgnutls.git Initialization of crypto libraries moved outside main gnutls code. --- diff --git a/lib/gcrypt/Makefile.am b/lib/gcrypt/Makefile.am index c7efbe514d..0b3d5741ad 100644 --- a/lib/gcrypt/Makefile.am +++ b/lib/gcrypt/Makefile.am @@ -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 index 0000000000..a511ac58b2 --- /dev/null +++ b/lib/gcrypt/init.c @@ -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 +#include +#include +#include + +#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; +} + diff --git a/lib/gnutls_errors.c b/lib/gnutls_errors.c index 9c71cd1e6d..22ff77e490 100644 --- a/lib/gnutls_errors.c +++ b/lib/gnutls_errors.c @@ -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."), diff --git a/lib/gnutls_global.c b/lib/gnutls_global.c index 1f5dc9ddb7..223abdca3b 100644 --- a/lib/gnutls_global.c +++ b/lib/gnutls_global.c @@ -28,11 +28,6 @@ #include #include #include -#ifndef HAVE_LIBNETTLE -#include -#define GNUTLS_MIN_LIBGCRYPT_VERSION "1.2.4" - -#endif #include #include /* 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. diff --git a/lib/gnutls_global.h b/lib/gnutls_global.h index 081c20d464..5e6265d39b 100644 --- a/lib/gnutls_global.h +++ b/lib/gnutls_global.h @@ -27,6 +27,7 @@ # define GNUTLS_GLOBAL_H #include +#include 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 diff --git a/lib/includes/gnutls/gnutls.h.in b/lib/includes/gnutls/gnutls.h.in index bfd876a7e4..f115691f37 100644 --- a/lib/includes/gnutls/gnutls.h.in +++ b/lib/includes/gnutls/gnutls.h.in @@ -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 diff --git a/lib/locks.c b/lib/locks.c index 3b8a66d234..336fe1c1c1 100644 --- a/lib/locks.c +++ b/lib/locks.c @@ -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 diff --git a/lib/locks.h b/lib/locks.h index 4f8c35581b..0a91d2ffec 100644 --- a/lib/locks.h +++ b/lib/locks.h @@ -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 diff --git a/lib/nettle/Makefile.am b/lib/nettle/Makefile.am index c7efbe514d..0b3d5741ad 100644 --- a/lib/nettle/Makefile.am +++ b/lib/nettle/Makefile.am @@ -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 index 0000000000..799d27646f --- /dev/null +++ b/lib/nettle/init.c @@ -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 +#include +#include +#include +#include + +/* Functions that refer to the initialization of the libgcrypt library. + */ + +int gnutls_crypto_init(void) +{ + return 0; +} + diff --git a/lib/nettle/rnd.c b/lib/nettle/rnd.c index 2ae544fdf0..ac24235991 100644 --- a/lib/nettle/rnd.c +++ b/lib/nettle/rnd.c @@ -35,8 +35,7 @@ #include #include #include - -#include +#include #define SOURCES 2