Daniel P. Berrange | e00adf6 | 2015-03-13 17:39:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU crypto TLS anonymous credential support |
| 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 |
Thomas Huth | b7cbb87 | 2019-02-13 16:54:59 +0100 | [diff] [blame] | 9 | * version 2.1 of the License, or (at your option) any later version. |
Daniel P. Berrange | e00adf6 | 2015-03-13 17:39:26 +0000 | [diff] [blame] | 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 | e00adf6 | 2015-03-13 17:39:26 +0000 | [diff] [blame] | 22 | #include "crypto/tlscredsanon.h" |
Michael S. Tsirkin | 986bc8d | 2018-05-03 22:50:23 +0300 | [diff] [blame] | 23 | #include "tlscredspriv.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 24 | #include "qapi/error.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 25 | #include "qemu/module.h" |
Daniel P. Berrange | e00adf6 | 2015-03-13 17:39:26 +0000 | [diff] [blame] | 26 | #include "qom/object_interfaces.h" |
| 27 | #include "trace.h" |
| 28 | |
| 29 | |
| 30 | #ifdef CONFIG_GNUTLS |
| 31 | |
| 32 | |
| 33 | static int |
| 34 | qcrypto_tls_creds_anon_load(QCryptoTLSCredsAnon *creds, |
| 35 | Error **errp) |
| 36 | { |
Daniel P. Berrangé | 57b9f11 | 2019-07-23 16:22:36 +0100 | [diff] [blame] | 37 | g_autofree char *dhparams = NULL; |
Daniel P. Berrange | e00adf6 | 2015-03-13 17:39:26 +0000 | [diff] [blame] | 38 | int ret; |
Daniel P. Berrange | e00adf6 | 2015-03-13 17:39:26 +0000 | [diff] [blame] | 39 | |
| 40 | trace_qcrypto_tls_creds_anon_load(creds, |
| 41 | creds->parent_obj.dir ? creds->parent_obj.dir : "<nodir>"); |
| 42 | |
| 43 | if (creds->parent_obj.endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) { |
| 44 | if (qcrypto_tls_creds_get_path(&creds->parent_obj, |
| 45 | QCRYPTO_TLS_CREDS_DH_PARAMS, |
| 46 | false, &dhparams, errp) < 0) { |
Daniel P. Berrangé | 57b9f11 | 2019-07-23 16:22:36 +0100 | [diff] [blame] | 47 | return -1; |
Daniel P. Berrange | e00adf6 | 2015-03-13 17:39:26 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | ret = gnutls_anon_allocate_server_credentials(&creds->data.server); |
| 51 | if (ret < 0) { |
| 52 | error_setg(errp, "Cannot allocate credentials: %s", |
| 53 | gnutls_strerror(ret)); |
Daniel P. Berrangé | 57b9f11 | 2019-07-23 16:22:36 +0100 | [diff] [blame] | 54 | return -1; |
Daniel P. Berrange | e00adf6 | 2015-03-13 17:39:26 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | if (qcrypto_tls_creds_get_dh_params_file(&creds->parent_obj, dhparams, |
| 58 | &creds->parent_obj.dh_params, |
| 59 | errp) < 0) { |
Daniel P. Berrangé | 57b9f11 | 2019-07-23 16:22:36 +0100 | [diff] [blame] | 60 | return -1; |
Daniel P. Berrange | e00adf6 | 2015-03-13 17:39:26 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | gnutls_anon_set_server_dh_params(creds->data.server, |
| 64 | creds->parent_obj.dh_params); |
| 65 | } else { |
| 66 | ret = gnutls_anon_allocate_client_credentials(&creds->data.client); |
| 67 | if (ret < 0) { |
| 68 | error_setg(errp, "Cannot allocate credentials: %s", |
| 69 | gnutls_strerror(ret)); |
Daniel P. Berrangé | 57b9f11 | 2019-07-23 16:22:36 +0100 | [diff] [blame] | 70 | return -1; |
Daniel P. Berrange | e00adf6 | 2015-03-13 17:39:26 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
Daniel P. Berrangé | 57b9f11 | 2019-07-23 16:22:36 +0100 | [diff] [blame] | 74 | return 0; |
Daniel P. Berrange | e00adf6 | 2015-03-13 17:39:26 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | |
| 78 | static void |
| 79 | qcrypto_tls_creds_anon_unload(QCryptoTLSCredsAnon *creds) |
| 80 | { |
| 81 | if (creds->parent_obj.endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) { |
| 82 | if (creds->data.client) { |
| 83 | gnutls_anon_free_client_credentials(creds->data.client); |
| 84 | creds->data.client = NULL; |
| 85 | } |
| 86 | } else { |
| 87 | if (creds->data.server) { |
| 88 | gnutls_anon_free_server_credentials(creds->data.server); |
| 89 | creds->data.server = NULL; |
| 90 | } |
| 91 | } |
| 92 | if (creds->parent_obj.dh_params) { |
| 93 | gnutls_dh_params_deinit(creds->parent_obj.dh_params); |
| 94 | creds->parent_obj.dh_params = NULL; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | #else /* ! CONFIG_GNUTLS */ |
| 99 | |
| 100 | |
| 101 | static void |
| 102 | qcrypto_tls_creds_anon_load(QCryptoTLSCredsAnon *creds G_GNUC_UNUSED, |
| 103 | Error **errp) |
| 104 | { |
| 105 | error_setg(errp, "TLS credentials support requires GNUTLS"); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | static void |
| 110 | qcrypto_tls_creds_anon_unload(QCryptoTLSCredsAnon *creds G_GNUC_UNUSED) |
| 111 | { |
| 112 | /* nada */ |
| 113 | } |
| 114 | |
| 115 | |
| 116 | #endif /* ! CONFIG_GNUTLS */ |
| 117 | |
| 118 | |
| 119 | static void |
| 120 | qcrypto_tls_creds_anon_prop_set_loaded(Object *obj, |
| 121 | bool value, |
| 122 | Error **errp) |
| 123 | { |
| 124 | QCryptoTLSCredsAnon *creds = QCRYPTO_TLS_CREDS_ANON(obj); |
| 125 | |
| 126 | if (value) { |
| 127 | qcrypto_tls_creds_anon_load(creds, errp); |
| 128 | } else { |
| 129 | qcrypto_tls_creds_anon_unload(creds); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | |
| 134 | #ifdef CONFIG_GNUTLS |
| 135 | |
| 136 | |
| 137 | static bool |
| 138 | qcrypto_tls_creds_anon_prop_get_loaded(Object *obj, |
| 139 | Error **errp G_GNUC_UNUSED) |
| 140 | { |
| 141 | QCryptoTLSCredsAnon *creds = QCRYPTO_TLS_CREDS_ANON(obj); |
| 142 | |
| 143 | if (creds->parent_obj.endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) { |
| 144 | return creds->data.server != NULL; |
| 145 | } else { |
| 146 | return creds->data.client != NULL; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | |
| 151 | #else /* ! CONFIG_GNUTLS */ |
| 152 | |
| 153 | |
| 154 | static bool |
| 155 | qcrypto_tls_creds_anon_prop_get_loaded(Object *obj G_GNUC_UNUSED, |
| 156 | Error **errp G_GNUC_UNUSED) |
| 157 | { |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | #endif /* ! CONFIG_GNUTLS */ |
| 163 | |
| 164 | |
| 165 | static void |
| 166 | qcrypto_tls_creds_anon_complete(UserCreatable *uc, Error **errp) |
| 167 | { |
| 168 | object_property_set_bool(OBJECT(uc), true, "loaded", errp); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | static void |
Daniel P. Berrange | e00adf6 | 2015-03-13 17:39:26 +0000 | [diff] [blame] | 173 | qcrypto_tls_creds_anon_finalize(Object *obj) |
| 174 | { |
| 175 | QCryptoTLSCredsAnon *creds = QCRYPTO_TLS_CREDS_ANON(obj); |
| 176 | |
| 177 | qcrypto_tls_creds_anon_unload(creds); |
| 178 | } |
| 179 | |
| 180 | |
| 181 | static void |
| 182 | qcrypto_tls_creds_anon_class_init(ObjectClass *oc, void *data) |
| 183 | { |
| 184 | UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc); |
| 185 | |
| 186 | ucc->complete = qcrypto_tls_creds_anon_complete; |
Daniel P. Berrange | 9884abe | 2015-08-24 18:46:57 +0100 | [diff] [blame] | 187 | |
| 188 | object_class_property_add_bool(oc, "loaded", |
| 189 | qcrypto_tls_creds_anon_prop_get_loaded, |
| 190 | qcrypto_tls_creds_anon_prop_set_loaded, |
| 191 | NULL); |
Daniel P. Berrange | e00adf6 | 2015-03-13 17:39:26 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | |
| 195 | static const TypeInfo qcrypto_tls_creds_anon_info = { |
| 196 | .parent = TYPE_QCRYPTO_TLS_CREDS, |
| 197 | .name = TYPE_QCRYPTO_TLS_CREDS_ANON, |
| 198 | .instance_size = sizeof(QCryptoTLSCredsAnon), |
Daniel P. Berrange | e00adf6 | 2015-03-13 17:39:26 +0000 | [diff] [blame] | 199 | .instance_finalize = qcrypto_tls_creds_anon_finalize, |
| 200 | .class_size = sizeof(QCryptoTLSCredsAnonClass), |
| 201 | .class_init = qcrypto_tls_creds_anon_class_init, |
| 202 | .interfaces = (InterfaceInfo[]) { |
| 203 | { TYPE_USER_CREATABLE }, |
| 204 | { } |
| 205 | } |
| 206 | }; |
| 207 | |
| 208 | |
| 209 | static void |
| 210 | qcrypto_tls_creds_anon_register_types(void) |
| 211 | { |
| 212 | type_register_static(&qcrypto_tls_creds_anon_info); |
| 213 | } |
| 214 | |
| 215 | |
| 216 | type_init(qcrypto_tls_creds_anon_register_types); |