]>
Commit | Line | Data |
---|---|---|
ddbb0d09 DB |
1 | /* |
2 | * QEMU Crypto initialization | |
3 | * | |
4 | * Copyright (c) 2015 Red Hat, Inc. | |
5 | * | |
6 | * This library is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License as published by the Free Software Foundation; either | |
9 | * version 2 of the License, or (at your option) any later version. | |
10 | * | |
11 | * This library is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU Lesser General Public | |
17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. | |
18 | * | |
19 | */ | |
20 | ||
21 | #include "crypto/init.h" | |
62893b67 | 22 | #include "qemu/thread.h" |
ddbb0d09 DB |
23 | |
24 | #ifdef CONFIG_GNUTLS | |
25 | #include <gnutls/gnutls.h> | |
26 | #include <gnutls/crypto.h> | |
91bfcdb0 | 27 | #endif |
ddbb0d09 | 28 | |
91bfcdb0 | 29 | #ifdef CONFIG_GCRYPT |
62893b67 DB |
30 | #include <gcrypt.h> |
31 | #endif | |
32 | ||
ddbb0d09 DB |
33 | /* #define DEBUG_GNUTLS */ |
34 | ||
62893b67 DB |
35 | /* |
36 | * If GNUTLS is built against GCrypt then | |
37 | * | |
38 | * - When GNUTLS >= 2.12, we must not initialize gcrypt threading | |
39 | * because GNUTLS will do that itself | |
40 | * - When GNUTLS < 2.12 we must always initialize gcrypt threading | |
91bfcdb0 | 41 | * - When GNUTLS is disabled we must always initialize gcrypt threading |
62893b67 DB |
42 | * |
43 | * But.... | |
44 | * | |
45 | * When gcrypt >= 1.6.0 we must not initialize gcrypt threading | |
46 | * because gcrypt will do that itself. | |
47 | * | |
48 | * So we need to init gcrypt threading if | |
49 | * | |
50 | * - gcrypt < 1.6.0 | |
51 | * AND | |
91bfcdb0 DB |
52 | * - gnutls < 2.12 |
53 | * OR | |
54 | * - gnutls is disabled | |
62893b67 DB |
55 | * |
56 | */ | |
57 | ||
91bfcdb0 DB |
58 | #if (defined(CONFIG_GCRYPT) && \ |
59 | (!defined(CONFIG_GNUTLS) || \ | |
60 | !defined(GNUTLS_VERSION_NUMBER) || \ | |
62893b67 DB |
61 | (GNUTLS_VERSION_NUMBER < 0x020c00)) && \ |
62 | (!defined(GCRYPT_VERSION_NUMBER) || \ | |
63 | (GCRYPT_VERSION_NUMBER < 0x010600))) | |
64 | #define QCRYPTO_INIT_GCRYPT_THREADS | |
65 | #else | |
66 | #undef QCRYPTO_INIT_GCRYPT_THREADS | |
67 | #endif | |
68 | ||
ddbb0d09 DB |
69 | #ifdef DEBUG_GNUTLS |
70 | static void qcrypto_gnutls_log(int level, const char *str) | |
71 | { | |
72 | fprintf(stderr, "%d: %s", level, str); | |
73 | } | |
74 | #endif | |
75 | ||
62893b67 DB |
76 | #ifdef QCRYPTO_INIT_GCRYPT_THREADS |
77 | static int qcrypto_gcrypt_mutex_init(void **priv) | |
78 | { \ | |
79 | QemuMutex *lock = NULL; | |
80 | lock = g_new0(QemuMutex, 1); | |
81 | qemu_mutex_init(lock); | |
82 | *priv = lock; | |
83 | return 0; | |
84 | } | |
85 | ||
86 | static int qcrypto_gcrypt_mutex_destroy(void **priv) | |
87 | { | |
88 | QemuMutex *lock = *priv; | |
89 | qemu_mutex_destroy(lock); | |
90 | g_free(lock); | |
91 | return 0; | |
92 | } | |
93 | ||
94 | static int qcrypto_gcrypt_mutex_lock(void **priv) | |
95 | { | |
96 | QemuMutex *lock = *priv; | |
97 | qemu_mutex_lock(lock); | |
98 | return 0; | |
99 | } | |
100 | ||
101 | static int qcrypto_gcrypt_mutex_unlock(void **priv) | |
102 | { | |
103 | QemuMutex *lock = *priv; | |
104 | qemu_mutex_unlock(lock); | |
105 | return 0; | |
106 | } | |
107 | ||
108 | static struct gcry_thread_cbs qcrypto_gcrypt_thread_impl = { | |
109 | (GCRY_THREAD_OPTION_PTHREAD | (GCRY_THREAD_OPTION_VERSION << 8)), | |
110 | NULL, | |
111 | qcrypto_gcrypt_mutex_init, | |
112 | qcrypto_gcrypt_mutex_destroy, | |
113 | qcrypto_gcrypt_mutex_lock, | |
114 | qcrypto_gcrypt_mutex_unlock, | |
115 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL | |
116 | }; | |
117 | #endif /* QCRYPTO_INIT_GCRYPT */ | |
118 | ||
ddbb0d09 DB |
119 | int qcrypto_init(Error **errp) |
120 | { | |
91bfcdb0 | 121 | #ifdef CONFIG_GNUTLS |
ddbb0d09 DB |
122 | int ret; |
123 | ret = gnutls_global_init(); | |
124 | if (ret < 0) { | |
125 | error_setg(errp, | |
126 | "Unable to initialize GNUTLS library: %s", | |
127 | gnutls_strerror(ret)); | |
128 | return -1; | |
129 | } | |
130 | #ifdef DEBUG_GNUTLS | |
131 | gnutls_global_set_log_level(10); | |
132 | gnutls_global_set_log_function(qcrypto_gnutls_log); | |
133 | #endif | |
91bfcdb0 | 134 | #endif |
62893b67 | 135 | |
91bfcdb0 | 136 | #ifdef CONFIG_GCRYPT |
62893b67 DB |
137 | if (!gcry_check_version(GCRYPT_VERSION)) { |
138 | error_setg(errp, "Unable to initialize gcrypt"); | |
139 | return -1; | |
140 | } | |
141 | #ifdef QCRYPTO_INIT_GCRYPT_THREADS | |
142 | gcry_control(GCRYCTL_SET_THREAD_CBS, &qcrypto_gcrypt_thread_impl); | |
143 | #endif /* QCRYPTO_INIT_GCRYPT_THREADS */ | |
144 | gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0); | |
145 | #endif | |
146 | ||
ddbb0d09 DB |
147 | return 0; |
148 | } |