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