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 9 * version 2.1 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 "qemu/osdep.h" 22 #include "crypto/tlscredsanon.h" 23 #include "tlscredspriv.h" 24 #include "qapi/error.h" 25 #include "qemu/module.h" 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 { 37 char *dhparams = NULL; 38 int ret; 39 int rv = -1; 40 41 trace_qcrypto_tls_creds_anon_load(creds, 42 creds->parent_obj.dir ? creds->parent_obj.dir : "<nodir>"); 43 44 if (creds->parent_obj.endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) { 45 if (qcrypto_tls_creds_get_path(&creds->parent_obj, 46 QCRYPTO_TLS_CREDS_DH_PARAMS, 47 false, &dhparams, errp) < 0) { 48 goto cleanup; 49 } 50 51 ret = gnutls_anon_allocate_server_credentials(&creds->data.server); 52 if (ret < 0) { 53 error_setg(errp, "Cannot allocate credentials: %s", 54 gnutls_strerror(ret)); 55 goto cleanup; 56 } 57 58 if (qcrypto_tls_creds_get_dh_params_file(&creds->parent_obj, dhparams, 59 &creds->parent_obj.dh_params, 60 errp) < 0) { 61 goto cleanup; 62 } 63 64 gnutls_anon_set_server_dh_params(creds->data.server, 65 creds->parent_obj.dh_params); 66 } else { 67 ret = gnutls_anon_allocate_client_credentials(&creds->data.client); 68 if (ret < 0) { 69 error_setg(errp, "Cannot allocate credentials: %s", 70 gnutls_strerror(ret)); 71 goto cleanup; 72 } 73 } 74 75 rv = 0; 76 cleanup: 77 g_free(dhparams); 78 return rv; 79 } 80 81 82 static void 83 qcrypto_tls_creds_anon_unload(QCryptoTLSCredsAnon *creds) 84 { 85 if (creds->parent_obj.endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) { 86 if (creds->data.client) { 87 gnutls_anon_free_client_credentials(creds->data.client); 88 creds->data.client = NULL; 89 } 90 } else { 91 if (creds->data.server) { 92 gnutls_anon_free_server_credentials(creds->data.server); 93 creds->data.server = NULL; 94 } 95 } 96 if (creds->parent_obj.dh_params) { 97 gnutls_dh_params_deinit(creds->parent_obj.dh_params); 98 creds->parent_obj.dh_params = NULL; 99 } 100 } 101 102 #else /* ! CONFIG_GNUTLS */ 103 104 105 static void 106 qcrypto_tls_creds_anon_load(QCryptoTLSCredsAnon *creds G_GNUC_UNUSED, 107 Error **errp) 108 { 109 error_setg(errp, "TLS credentials support requires GNUTLS"); 110 } 111 112 113 static void 114 qcrypto_tls_creds_anon_unload(QCryptoTLSCredsAnon *creds G_GNUC_UNUSED) 115 { 116 /* nada */ 117 } 118 119 120 #endif /* ! CONFIG_GNUTLS */ 121 122 123 static void 124 qcrypto_tls_creds_anon_prop_set_loaded(Object *obj, 125 bool value, 126 Error **errp) 127 { 128 QCryptoTLSCredsAnon *creds = QCRYPTO_TLS_CREDS_ANON(obj); 129 130 if (value) { 131 qcrypto_tls_creds_anon_load(creds, errp); 132 } else { 133 qcrypto_tls_creds_anon_unload(creds); 134 } 135 } 136 137 138 #ifdef CONFIG_GNUTLS 139 140 141 static bool 142 qcrypto_tls_creds_anon_prop_get_loaded(Object *obj, 143 Error **errp G_GNUC_UNUSED) 144 { 145 QCryptoTLSCredsAnon *creds = QCRYPTO_TLS_CREDS_ANON(obj); 146 147 if (creds->parent_obj.endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) { 148 return creds->data.server != NULL; 149 } else { 150 return creds->data.client != NULL; 151 } 152 } 153 154 155 #else /* ! CONFIG_GNUTLS */ 156 157 158 static bool 159 qcrypto_tls_creds_anon_prop_get_loaded(Object *obj G_GNUC_UNUSED, 160 Error **errp G_GNUC_UNUSED) 161 { 162 return false; 163 } 164 165 166 #endif /* ! CONFIG_GNUTLS */ 167 168 169 static void 170 qcrypto_tls_creds_anon_complete(UserCreatable *uc, Error **errp) 171 { 172 object_property_set_bool(OBJECT(uc), true, "loaded", errp); 173 } 174 175 176 static void 177 qcrypto_tls_creds_anon_finalize(Object *obj) 178 { 179 QCryptoTLSCredsAnon *creds = QCRYPTO_TLS_CREDS_ANON(obj); 180 181 qcrypto_tls_creds_anon_unload(creds); 182 } 183 184 185 static void 186 qcrypto_tls_creds_anon_class_init(ObjectClass *oc, void *data) 187 { 188 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc); 189 190 ucc->complete = qcrypto_tls_creds_anon_complete; 191 192 object_class_property_add_bool(oc, "loaded", 193 qcrypto_tls_creds_anon_prop_get_loaded, 194 qcrypto_tls_creds_anon_prop_set_loaded, 195 NULL); 196 } 197 198 199 static const TypeInfo qcrypto_tls_creds_anon_info = { 200 .parent = TYPE_QCRYPTO_TLS_CREDS, 201 .name = TYPE_QCRYPTO_TLS_CREDS_ANON, 202 .instance_size = sizeof(QCryptoTLSCredsAnon), 203 .instance_finalize = qcrypto_tls_creds_anon_finalize, 204 .class_size = sizeof(QCryptoTLSCredsAnonClass), 205 .class_init = qcrypto_tls_creds_anon_class_init, 206 .interfaces = (InterfaceInfo[]) { 207 { TYPE_USER_CREATABLE }, 208 { } 209 } 210 }; 211 212 213 static void 214 qcrypto_tls_creds_anon_register_types(void) 215 { 216 type_register_static(&qcrypto_tls_creds_anon_info); 217 } 218 219 220 type_init(qcrypto_tls_creds_anon_register_types); 221