]>
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 | ||
42f7a448 | 21 | #include "qemu/osdep.h" |
ddbb0d09 | 22 | #include "crypto/init.h" |
da34e65c | 23 | #include "qapi/error.h" |
62893b67 | 24 | #include "qemu/thread.h" |
ddbb0d09 DB |
25 | |
26 | #ifdef CONFIG_GNUTLS | |
27 | #include <gnutls/gnutls.h> | |
28 | #include <gnutls/crypto.h> | |
91bfcdb0 | 29 | #endif |
ddbb0d09 | 30 | |
91bfcdb0 | 31 | #ifdef CONFIG_GCRYPT |
62893b67 DB |
32 | #include <gcrypt.h> |
33 | #endif | |
34 | ||
ddbb0d09 DB |
35 | /* #define DEBUG_GNUTLS */ |
36 | ||
62893b67 DB |
37 | /* |
38 | * If GNUTLS is built against GCrypt then | |
39 | * | |
40 | * - When GNUTLS >= 2.12, we must not initialize gcrypt threading | |
41 | * because GNUTLS will do that itself | |
42 | * - When GNUTLS < 2.12 we must always initialize gcrypt threading | |
91bfcdb0 | 43 | * - When GNUTLS is disabled we must always initialize gcrypt threading |
62893b67 DB |
44 | * |
45 | * But.... | |
46 | * | |
47 | * When gcrypt >= 1.6.0 we must not initialize gcrypt threading | |
48 | * because gcrypt will do that itself. | |
49 | * | |
50 | * So we need to init gcrypt threading if | |
51 | * | |
52 | * - gcrypt < 1.6.0 | |
53 | * AND | |
91bfcdb0 DB |
54 | * - gnutls < 2.12 |
55 | * OR | |
56 | * - gnutls is disabled | |
62893b67 DB |
57 | * |
58 | */ | |
59 | ||
91bfcdb0 DB |
60 | #if (defined(CONFIG_GCRYPT) && \ |
61 | (!defined(CONFIG_GNUTLS) || \ | |
d9269b27 | 62 | (LIBGNUTLS_VERSION_NUMBER < 0x020c00)) && \ |
62893b67 DB |
63 | (!defined(GCRYPT_VERSION_NUMBER) || \ |
64 | (GCRYPT_VERSION_NUMBER < 0x010600))) | |
65 | #define QCRYPTO_INIT_GCRYPT_THREADS | |
66 | #else | |
67 | #undef QCRYPTO_INIT_GCRYPT_THREADS | |
68 | #endif | |
69 | ||
ddbb0d09 DB |
70 | #ifdef DEBUG_GNUTLS |
71 | static void qcrypto_gnutls_log(int level, const char *str) | |
72 | { | |
73 | fprintf(stderr, "%d: %s", level, str); | |
74 | } | |
75 | #endif | |
76 | ||
62893b67 DB |
77 | #ifdef QCRYPTO_INIT_GCRYPT_THREADS |
78 | static int qcrypto_gcrypt_mutex_init(void **priv) | |
79 | { \ | |
80 | QemuMutex *lock = NULL; | |
81 | lock = g_new0(QemuMutex, 1); | |
82 | qemu_mutex_init(lock); | |
83 | *priv = lock; | |
84 | return 0; | |
85 | } | |
86 | ||
87 | static int qcrypto_gcrypt_mutex_destroy(void **priv) | |
88 | { | |
89 | QemuMutex *lock = *priv; | |
90 | qemu_mutex_destroy(lock); | |
91 | g_free(lock); | |
92 | return 0; | |
93 | } | |
94 | ||
95 | static int qcrypto_gcrypt_mutex_lock(void **priv) | |
96 | { | |
97 | QemuMutex *lock = *priv; | |
98 | qemu_mutex_lock(lock); | |
99 | return 0; | |
100 | } | |
101 | ||
102 | static int qcrypto_gcrypt_mutex_unlock(void **priv) | |
103 | { | |
104 | QemuMutex *lock = *priv; | |
105 | qemu_mutex_unlock(lock); | |
106 | return 0; | |
107 | } | |
108 | ||
109 | static struct gcry_thread_cbs qcrypto_gcrypt_thread_impl = { | |
110 | (GCRY_THREAD_OPTION_PTHREAD | (GCRY_THREAD_OPTION_VERSION << 8)), | |
111 | NULL, | |
112 | qcrypto_gcrypt_mutex_init, | |
113 | qcrypto_gcrypt_mutex_destroy, | |
114 | qcrypto_gcrypt_mutex_lock, | |
115 | qcrypto_gcrypt_mutex_unlock, | |
116 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL | |
117 | }; | |
118 | #endif /* QCRYPTO_INIT_GCRYPT */ | |
119 | ||
ddbb0d09 DB |
120 | int qcrypto_init(Error **errp) |
121 | { | |
37316663 DB |
122 | #ifdef QCRYPTO_INIT_GCRYPT_THREADS |
123 | gcry_control(GCRYCTL_SET_THREAD_CBS, &qcrypto_gcrypt_thread_impl); | |
124 | #endif /* QCRYPTO_INIT_GCRYPT_THREADS */ | |
125 | ||
91bfcdb0 | 126 | #ifdef CONFIG_GNUTLS |
ddbb0d09 DB |
127 | int ret; |
128 | ret = gnutls_global_init(); | |
129 | if (ret < 0) { | |
130 | error_setg(errp, | |
131 | "Unable to initialize GNUTLS library: %s", | |
132 | gnutls_strerror(ret)); | |
133 | return -1; | |
134 | } | |
135 | #ifdef DEBUG_GNUTLS | |
136 | gnutls_global_set_log_level(10); | |
137 | gnutls_global_set_log_function(qcrypto_gnutls_log); | |
138 | #endif | |
91bfcdb0 | 139 | #endif |
62893b67 | 140 | |
91bfcdb0 | 141 | #ifdef CONFIG_GCRYPT |
62893b67 DB |
142 | if (!gcry_check_version(GCRYPT_VERSION)) { |
143 | error_setg(errp, "Unable to initialize gcrypt"); | |
144 | return -1; | |
145 | } | |
62893b67 DB |
146 | gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0); |
147 | #endif | |
148 | ||
ddbb0d09 DB |
149 | return 0; |
150 | } |