Deprecate Python 2 support
[qemu/kevin.git] / crypto / tlscredsanon.c
blob3024b17cfd85d3e9d395c65ed01ec64a6814256b
1 /*
2 * QEMU crypto TLS anonymous credential support
4 * Copyright (c) 2015 Red Hat, Inc.
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.
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.
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/>.
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"
30 #ifdef CONFIG_GNUTLS
33 static int
34 qcrypto_tls_creds_anon_load(QCryptoTLSCredsAnon *creds,
35 Error **errp)
37 char *dhparams = NULL;
38 int ret;
39 int rv = -1;
41 trace_qcrypto_tls_creds_anon_load(creds,
42 creds->parent_obj.dir ? creds->parent_obj.dir : "<nodir>");
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;
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;
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;
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;
75 rv = 0;
76 cleanup:
77 g_free(dhparams);
78 return rv;
82 static void
83 qcrypto_tls_creds_anon_unload(QCryptoTLSCredsAnon *creds)
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;
90 } else {
91 if (creds->data.server) {
92 gnutls_anon_free_server_credentials(creds->data.server);
93 creds->data.server = NULL;
96 if (creds->parent_obj.dh_params) {
97 gnutls_dh_params_deinit(creds->parent_obj.dh_params);
98 creds->parent_obj.dh_params = NULL;
102 #else /* ! CONFIG_GNUTLS */
105 static void
106 qcrypto_tls_creds_anon_load(QCryptoTLSCredsAnon *creds G_GNUC_UNUSED,
107 Error **errp)
109 error_setg(errp, "TLS credentials support requires GNUTLS");
113 static void
114 qcrypto_tls_creds_anon_unload(QCryptoTLSCredsAnon *creds G_GNUC_UNUSED)
116 /* nada */
120 #endif /* ! CONFIG_GNUTLS */
123 static void
124 qcrypto_tls_creds_anon_prop_set_loaded(Object *obj,
125 bool value,
126 Error **errp)
128 QCryptoTLSCredsAnon *creds = QCRYPTO_TLS_CREDS_ANON(obj);
130 if (value) {
131 qcrypto_tls_creds_anon_load(creds, errp);
132 } else {
133 qcrypto_tls_creds_anon_unload(creds);
138 #ifdef CONFIG_GNUTLS
141 static bool
142 qcrypto_tls_creds_anon_prop_get_loaded(Object *obj,
143 Error **errp G_GNUC_UNUSED)
145 QCryptoTLSCredsAnon *creds = QCRYPTO_TLS_CREDS_ANON(obj);
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;
155 #else /* ! CONFIG_GNUTLS */
158 static bool
159 qcrypto_tls_creds_anon_prop_get_loaded(Object *obj G_GNUC_UNUSED,
160 Error **errp G_GNUC_UNUSED)
162 return false;
166 #endif /* ! CONFIG_GNUTLS */
169 static void
170 qcrypto_tls_creds_anon_complete(UserCreatable *uc, Error **errp)
172 object_property_set_bool(OBJECT(uc), true, "loaded", errp);
176 static void
177 qcrypto_tls_creds_anon_finalize(Object *obj)
179 QCryptoTLSCredsAnon *creds = QCRYPTO_TLS_CREDS_ANON(obj);
181 qcrypto_tls_creds_anon_unload(creds);
185 static void
186 qcrypto_tls_creds_anon_class_init(ObjectClass *oc, void *data)
188 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
190 ucc->complete = qcrypto_tls_creds_anon_complete;
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);
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 },
213 static void
214 qcrypto_tls_creds_anon_register_types(void)
216 type_register_static(&qcrypto_tls_creds_anon_info);
220 type_init(qcrypto_tls_creds_anon_register_types);