summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCedric BAIL <cedric.bail@samsung.com>2015-03-17 08:50:09 +0100
committerCedric BAIL <cedric@osg.samsung.com>2015-03-17 09:58:18 +0100
commite649992bffb7c76ef0290ecba1dac15d45a8169f (patch)
treeeef26f0dfe080afbe76cf34f3198f5d9aa6a0707 /src
parent31a3a5afff5f042d3fd96359ba84e1c0c4c4c33e (diff)
emile: make the initialization part of backend cipher file to.
Diffstat (limited to 'src')
-rw-r--r--src/lib/emile/emile_cipher.c5
-rw-r--r--src/lib/emile/emile_cipher_gnutls.c85
-rw-r--r--src/lib/emile/emile_cipher_openssl.c13
-rw-r--r--src/lib/emile/emile_main.c92
-rw-r--r--src/lib/emile/emile_private.h2
5 files changed, 109 insertions, 88 deletions
diff --git a/src/lib/emile/emile_cipher.c b/src/lib/emile/emile_cipher.c
index e1a3ac08d8..3af06886b9 100644
--- a/src/lib/emile/emile_cipher.c
+++ b/src/lib/emile/emile_cipher.c
@@ -8,6 +8,11 @@
8 8
9#include "emile_private.h" 9#include "emile_private.h"
10 10
11Eina_Bool _emile_cipher_init(void)
12{
13 return EINA_FALSE;
14}
15
11EAPI Eina_Binbuf * 16EAPI Eina_Binbuf *
12emile_binbuf_cipher(const Eina_Binbuf *data EINA_UNUSED, 17emile_binbuf_cipher(const Eina_Binbuf *data EINA_UNUSED,
13 const char *key EINA_UNUSED, 18 const char *key EINA_UNUSED,
diff --git a/src/lib/emile/emile_cipher_gnutls.c b/src/lib/emile/emile_cipher_gnutls.c
index 8cfcc6366f..92823fa75f 100644
--- a/src/lib/emile/emile_cipher_gnutls.c
+++ b/src/lib/emile/emile_cipher_gnutls.c
@@ -21,6 +21,91 @@
21#define MAX_KEY_LEN 32 21#define MAX_KEY_LEN 32
22#define MAX_IV_LEN 16 22#define MAX_IV_LEN 16
23 23
24#ifdef HAVE_GNUTLS
25static int
26_emile_thread_mutex_init(void **priv)
27{
28 Eina_Lock *lock;
29
30 lock = malloc(sizeof (Eina_Lock));
31 if (!lock) return ENOMEM;
32
33 if (!eina_lock_new(lock))
34 {
35 free(lock);
36 return ENOMEM;
37 }
38
39 *priv = lock;
40 return 0;
41}
42
43static int
44_emile_thread_mutex_destroy(void **priv)
45{
46 eina_lock_free(*priv);
47 free(*priv);
48 return 0;
49}
50
51static int
52_emile_thread_mutex_lock(void **priv)
53{
54 if (eina_lock_take(*priv) == EINA_LOCK_FAIL)
55 return EINVAL;
56 return 0;
57}
58
59static int
60_emile_thread_mutex_unlock(void **priv)
61{
62 if (eina_lock_release(*priv) == EINA_LOCK_FAIL)
63 return EINVAL;
64 return 0;
65}
66
67static struct gcry_thread_cbs _emile_threads = {
68 (GCRY_THREAD_OPTION_PTHREAD | (GCRY_THREAD_OPTION_VERSION << 8)),
69 NULL, _emile_thread_mutex_init, _emile_thread_mutex_destroy,
70 _emile_thread_mutex_lock, _emile_thread_mutex_unlock,
71 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
72};
73#endif /* ifdef HAVE_GNUTLS */
74
75Eina_Bool
76_emile_cipher_init(void)
77{
78#ifdef HAVE_GNUTLS
79 if (gcry_control(GCRYCTL_SET_THREAD_CBS, &_emile_threads))
80 WRN(
81 "YOU ARE USING PTHREADS, BUT I CANNOT INITIALIZE THREADSAFE GCRYPT OPERATIONS!");
82
83 /* Before the library can be used, it must initialize itself if needed. */
84 if (gcry_control(GCRYCTL_ANY_INITIALIZATION_P) == 0)
85 {
86 gcry_check_version(NULL);
87 /* Disable warning messages about problems with the secure memory subsystem.
88 This command should be run right after gcry_check_version. */
89 if (gcry_control(GCRYCTL_DISABLE_SECMEM_WARN))
90 return EINA_FALSE; /* This command is used to allocate a pool of secure memory and thus
91 enabling the use of secure memory. It also drops all extra privileges the
92 process has (i.e. if it is run as setuid (root)). If the argument nbytes
93 is 0, secure memory will be disabled. The minimum amount of secure memory
94 allocated is currently 16384 bytes; you may thus use a value of 1 to
95 request that default size. */
96
97 if (gcry_control(GCRYCTL_INIT_SECMEM, 16384, 0))
98 WRN(
99 "BIG FAT WARNING: I AM UNABLE TO REQUEST SECMEM, Cryptographic operation are at risk !");
100 }
101
102 if (gnutls_global_init())
103 return EINA_FALSE;
104#endif /* ifdef HAVE_GNUTLS */
105
106 return EINA_TRUE;
107}
108
24# ifdef HAVE_GNUTLS 109# ifdef HAVE_GNUTLS
25static inline Eina_Bool 110static inline Eina_Bool
26emile_hmac_sha1(const void *key, 111emile_hmac_sha1(const void *key,
diff --git a/src/lib/emile/emile_cipher_openssl.c b/src/lib/emile/emile_cipher_openssl.c
index 630cf127d1..9229e776f1 100644
--- a/src/lib/emile/emile_cipher_openssl.c
+++ b/src/lib/emile/emile_cipher_openssl.c
@@ -22,6 +22,19 @@
22#define MAX_KEY_LEN EVP_MAX_KEY_LENGTH 22#define MAX_KEY_LEN EVP_MAX_KEY_LENGTH
23#define MAX_IV_LEN EVP_MAX_IV_LENGTH 23#define MAX_IV_LEN EVP_MAX_IV_LENGTH
24 24
25Eina_Bool
26_emile_cipher_init(void)
27{
28#ifdef HAVE_OPENSSL
29 ERR_load_crypto_strings();
30 SSL_library_init();
31 SSL_load_error_strings();
32 OpenSSL_add_all_algorithms();
33#endif /* ifdef HAVE_OPENSSL */
34
35 return EINA_TRUE;
36}
37
25static Eina_Bool 38static Eina_Bool
26emile_pbkdf2_sha1(const char *key, 39emile_pbkdf2_sha1(const char *key,
27 int key_len, 40 int key_len,
diff --git a/src/lib/emile/emile_main.c b/src/lib/emile/emile_main.c
index 2e137d2009..d5939e8ad8 100644
--- a/src/lib/emile/emile_main.c
+++ b/src/lib/emile/emile_main.c
@@ -19,102 +19,18 @@
19#include "Emile.h" 19#include "Emile.h"
20#include "emile_private.h" 20#include "emile_private.h"
21 21
22static Eina_Bool _emile_cipher_init = EINA_FALSE; 22static Eina_Bool _emile_cipher_inited = EINA_FALSE;
23static unsigned int _emile_init_count = 0; 23static unsigned int _emile_init_count = 0;
24int _emile_log_dom_global = -1; 24int _emile_log_dom_global = -1;
25 25
26#ifdef HAVE_GNUTLS
27static int
28_emile_thread_mutex_init(void **priv)
29{
30 Eina_Lock *lock;
31
32 lock = malloc(sizeof (Eina_Lock));
33 if (!lock) return ENOMEM;
34
35 if (!eina_lock_new(lock))
36 {
37 free(lock);
38 return ENOMEM;
39 }
40
41 *priv = lock;
42 return 0;
43}
44
45static int
46_emile_thread_mutex_destroy(void **priv)
47{
48 eina_lock_free(*priv);
49 free(*priv);
50 return 0;
51}
52
53static int
54_emile_thread_mutex_lock(void **priv)
55{
56 if (eina_lock_take(*priv) == EINA_LOCK_FAIL)
57 return EINVAL;
58 return 0;
59}
60
61static int
62_emile_thread_mutex_unlock(void **priv)
63{
64 if (eina_lock_release(*priv) == EINA_LOCK_FAIL)
65 return EINVAL;
66 return 0;
67}
68
69static struct gcry_thread_cbs _emile_threads = {
70 (GCRY_THREAD_OPTION_PTHREAD | (GCRY_THREAD_OPTION_VERSION << 8)),
71 NULL, _emile_thread_mutex_init, _emile_thread_mutex_destroy,
72 _emile_thread_mutex_lock, _emile_thread_mutex_unlock,
73 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
74};
75#endif /* ifdef HAVE_GNUTLS */
76
77EAPI Eina_Bool 26EAPI Eina_Bool
78emile_cipher_init(void) 27emile_cipher_init(void)
79{ 28{
80 if (_emile_cipher_init) return EINA_TRUE; 29 if (_emile_cipher_inited) return EINA_TRUE;
81
82#ifdef HAVE_GNUTLS
83 if (gcry_control(GCRYCTL_SET_THREAD_CBS, &_emile_threads))
84 WRN(
85 "YOU ARE USING PTHREADS, BUT I CANNOT INITIALIZE THREADSAFE GCRYPT OPERATIONS!");
86 30
87 /* Before the library can be used, it must initialize itself if needed. */ 31 if (!_emile_cipher_init()) return EINA_FALSE;
88 if (gcry_control(GCRYCTL_ANY_INITIALIZATION_P) == 0)
89 {
90 gcry_check_version(NULL);
91 /* Disable warning messages about problems with the secure memory subsystem.
92 This command should be run right after gcry_check_version. */
93 if (gcry_control(GCRYCTL_DISABLE_SECMEM_WARN))
94 return EINA_FALSE; /* This command is used to allocate a pool of secure memory and thus
95 enabling the use of secure memory. It also drops all extra privileges the
96 process has (i.e. if it is run as setuid (root)). If the argument nbytes
97 is 0, secure memory will be disabled. The minimum amount of secure memory
98 allocated is currently 16384 bytes; you may thus use a value of 1 to
99 request that default size. */
100
101 if (gcry_control(GCRYCTL_INIT_SECMEM, 16384, 0))
102 WRN(
103 "BIG FAT WARNING: I AM UNABLE TO REQUEST SECMEM, Cryptographic operation are at risk !");
104 }
105
106 if (gnutls_global_init())
107 return EINA_FALSE;
108
109#endif /* ifdef HAVE_GNUTLS */
110#ifdef HAVE_OPENSSL
111 ERR_load_crypto_strings();
112 SSL_library_init();
113 SSL_load_error_strings();
114 OpenSSL_add_all_algorithms();
115#endif /* ifdef HAVE_OPENSSL */
116 32
117 _emile_cipher_init = EINA_TRUE; 33 _emile_cipher_inited = EINA_TRUE;
118 34
119 return EINA_TRUE; 35 return EINA_TRUE;
120} 36}
diff --git a/src/lib/emile/emile_private.h b/src/lib/emile/emile_private.h
index 0210379091..e6b4763f40 100644
--- a/src/lib/emile/emile_private.h
+++ b/src/lib/emile/emile_private.h
@@ -24,4 +24,6 @@ extern int _emile_log_dom_global;
24#endif /* ifdef CRI */ 24#endif /* ifdef CRI */
25#define CRI(...) EINA_LOG_DOM_CRIT(_emile_log_dom_global, __VA_ARGS__) 25#define CRI(...) EINA_LOG_DOM_CRIT(_emile_log_dom_global, __VA_ARGS__)
26 26
27Eina_Bool _emile_cipher_init(void);
28
27#endif /* EMILE_PRIVATE_H_ */ 29#endif /* EMILE_PRIVATE_H_ */