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.1 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"
32 #include <gnutls/gnutls.h>
36 qcrypto_tls_creds_anon_load(QCryptoTLSCredsAnon
*creds
,
39 g_autofree
char *dhparams
= NULL
;
42 trace_qcrypto_tls_creds_anon_load(creds
,
43 creds
->parent_obj
.dir
? creds
->parent_obj
.dir
: "<nodir>");
45 if (creds
->parent_obj
.endpoint
== QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
) {
46 if (qcrypto_tls_creds_get_path(&creds
->parent_obj
,
47 QCRYPTO_TLS_CREDS_DH_PARAMS
,
48 false, &dhparams
, errp
) < 0) {
52 ret
= gnutls_anon_allocate_server_credentials(&creds
->data
.server
);
54 error_setg(errp
, "Cannot allocate credentials: %s",
55 gnutls_strerror(ret
));
59 if (qcrypto_tls_creds_get_dh_params_file(&creds
->parent_obj
, dhparams
,
60 &creds
->parent_obj
.dh_params
,
65 gnutls_anon_set_server_dh_params(creds
->data
.server
,
66 creds
->parent_obj
.dh_params
);
68 ret
= gnutls_anon_allocate_client_credentials(&creds
->data
.client
);
70 error_setg(errp
, "Cannot allocate credentials: %s",
71 gnutls_strerror(ret
));
81 qcrypto_tls_creds_anon_unload(QCryptoTLSCredsAnon
*creds
)
83 if (creds
->parent_obj
.endpoint
== QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
) {
84 if (creds
->data
.client
) {
85 gnutls_anon_free_client_credentials(creds
->data
.client
);
86 creds
->data
.client
= NULL
;
89 if (creds
->data
.server
) {
90 gnutls_anon_free_server_credentials(creds
->data
.server
);
91 creds
->data
.server
= NULL
;
94 if (creds
->parent_obj
.dh_params
) {
95 gnutls_dh_params_deinit(creds
->parent_obj
.dh_params
);
96 creds
->parent_obj
.dh_params
= NULL
;
100 #else /* ! CONFIG_GNUTLS */
104 qcrypto_tls_creds_anon_load(QCryptoTLSCredsAnon
*creds G_GNUC_UNUSED
,
107 error_setg(errp
, "TLS credentials support requires GNUTLS");
112 qcrypto_tls_creds_anon_unload(QCryptoTLSCredsAnon
*creds G_GNUC_UNUSED
)
118 #endif /* ! CONFIG_GNUTLS */
122 qcrypto_tls_creds_anon_prop_set_loaded(Object
*obj
,
126 QCryptoTLSCredsAnon
*creds
= QCRYPTO_TLS_CREDS_ANON(obj
);
128 qcrypto_tls_creds_anon_unload(creds
);
130 qcrypto_tls_creds_anon_load(creds
, errp
);
139 qcrypto_tls_creds_anon_prop_get_loaded(Object
*obj
,
140 Error
**errp G_GNUC_UNUSED
)
142 QCryptoTLSCredsAnon
*creds
= QCRYPTO_TLS_CREDS_ANON(obj
);
144 if (creds
->parent_obj
.endpoint
== QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
) {
145 return creds
->data
.server
!= NULL
;
147 return creds
->data
.client
!= NULL
;
152 #else /* ! CONFIG_GNUTLS */
156 qcrypto_tls_creds_anon_prop_get_loaded(Object
*obj G_GNUC_UNUSED
,
157 Error
**errp G_GNUC_UNUSED
)
163 #endif /* ! CONFIG_GNUTLS */
167 qcrypto_tls_creds_anon_complete(UserCreatable
*uc
, Error
**errp
)
169 object_property_set_bool(OBJECT(uc
), "loaded", true, errp
);
174 qcrypto_tls_creds_anon_finalize(Object
*obj
)
176 QCryptoTLSCredsAnon
*creds
= QCRYPTO_TLS_CREDS_ANON(obj
);
178 qcrypto_tls_creds_anon_unload(creds
);
183 qcrypto_tls_creds_anon_class_init(ObjectClass
*oc
, void *data
)
185 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
187 ucc
->complete
= qcrypto_tls_creds_anon_complete
;
189 object_class_property_add_bool(oc
, "loaded",
190 qcrypto_tls_creds_anon_prop_get_loaded
,
191 qcrypto_tls_creds_anon_prop_set_loaded
);
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
),
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
},
210 qcrypto_tls_creds_anon_register_types(void)
212 type_register_static(&qcrypto_tls_creds_anon_info
);
216 type_init(qcrypto_tls_creds_anon_register_types
);