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