Merge remote-tracking branch 'remotes/pmaydell/tags/pull-target-arm-20210330' into...
[qemu/ar7.git] / crypto / tlscredsanon.c
blobbea5f76c55dae44152d06a2c4c25b014dc8b5626
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  */
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     g_autofree char *dhparams = NULL;
38     int ret;
40     trace_qcrypto_tls_creds_anon_load(creds,
41             creds->parent_obj.dir ? creds->parent_obj.dir : "<nodir>");
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) {
47             return -1;
48         }
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));
54             return -1;
55         }
57         if (qcrypto_tls_creds_get_dh_params_file(&creds->parent_obj, dhparams,
58                                                  &creds->parent_obj.dh_params,
59                                                  errp) < 0) {
60             return -1;
61         }
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));
70             return -1;
71         }
72     }
74     return 0;
78 static void
79 qcrypto_tls_creds_anon_unload(QCryptoTLSCredsAnon *creds)
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     }
98 #else /* ! CONFIG_GNUTLS */
101 static void
102 qcrypto_tls_creds_anon_load(QCryptoTLSCredsAnon *creds G_GNUC_UNUSED,
103                             Error **errp)
105     error_setg(errp, "TLS credentials support requires GNUTLS");
109 static void
110 qcrypto_tls_creds_anon_unload(QCryptoTLSCredsAnon *creds G_GNUC_UNUSED)
112     /* nada */
116 #endif /* ! CONFIG_GNUTLS */
119 static void
120 qcrypto_tls_creds_anon_prop_set_loaded(Object *obj,
121                                        bool value,
122                                        Error **errp)
124     QCryptoTLSCredsAnon *creds = QCRYPTO_TLS_CREDS_ANON(obj);
126     qcrypto_tls_creds_anon_unload(creds);
127     if (value) {
128         qcrypto_tls_creds_anon_load(creds, errp);
129     }
133 #ifdef CONFIG_GNUTLS
136 static bool
137 qcrypto_tls_creds_anon_prop_get_loaded(Object *obj,
138                                        Error **errp G_GNUC_UNUSED)
140     QCryptoTLSCredsAnon *creds = QCRYPTO_TLS_CREDS_ANON(obj);
142     if (creds->parent_obj.endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
143         return creds->data.server != NULL;
144     } else {
145         return creds->data.client != NULL;
146     }
150 #else /* ! CONFIG_GNUTLS */
153 static bool
154 qcrypto_tls_creds_anon_prop_get_loaded(Object *obj G_GNUC_UNUSED,
155                                        Error **errp G_GNUC_UNUSED)
157     return false;
161 #endif /* ! CONFIG_GNUTLS */
164 static void
165 qcrypto_tls_creds_anon_complete(UserCreatable *uc, Error **errp)
167     object_property_set_bool(OBJECT(uc), "loaded", true, errp);
171 static void
172 qcrypto_tls_creds_anon_finalize(Object *obj)
174     QCryptoTLSCredsAnon *creds = QCRYPTO_TLS_CREDS_ANON(obj);
176     qcrypto_tls_creds_anon_unload(creds);
180 static void
181 qcrypto_tls_creds_anon_class_init(ObjectClass *oc, void *data)
183     UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
185     ucc->complete = qcrypto_tls_creds_anon_complete;
187     object_class_property_add_bool(oc, "loaded",
188                                    qcrypto_tls_creds_anon_prop_get_loaded,
189                                    qcrypto_tls_creds_anon_prop_set_loaded);
193 static const TypeInfo qcrypto_tls_creds_anon_info = {
194     .parent = TYPE_QCRYPTO_TLS_CREDS,
195     .name = TYPE_QCRYPTO_TLS_CREDS_ANON,
196     .instance_size = sizeof(QCryptoTLSCredsAnon),
197     .instance_finalize = qcrypto_tls_creds_anon_finalize,
198     .class_size = sizeof(QCryptoTLSCredsAnonClass),
199     .class_init = qcrypto_tls_creds_anon_class_init,
200     .interfaces = (InterfaceInfo[]) {
201         { TYPE_USER_CREATABLE },
202         { }
203     }
207 static void
208 qcrypto_tls_creds_anon_register_types(void)
210     type_register_static(&qcrypto_tls_creds_anon_info);
214 type_init(qcrypto_tls_creds_anon_register_types);