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"
34 qcrypto_tls_creds_anon_load(QCryptoTLSCredsAnon
*creds
,
37 char *dhparams
= NULL
;
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) {
51 ret
= gnutls_anon_allocate_server_credentials(&creds
->data
.server
);
53 error_setg(errp
, "Cannot allocate credentials: %s",
54 gnutls_strerror(ret
));
58 if (qcrypto_tls_creds_get_dh_params_file(&creds
->parent_obj
, dhparams
,
59 &creds
->parent_obj
.dh_params
,
64 gnutls_anon_set_server_dh_params(creds
->data
.server
,
65 creds
->parent_obj
.dh_params
);
67 ret
= gnutls_anon_allocate_client_credentials(&creds
->data
.client
);
69 error_setg(errp
, "Cannot allocate credentials: %s",
70 gnutls_strerror(ret
));
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
;
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 */
106 qcrypto_tls_creds_anon_load(QCryptoTLSCredsAnon
*creds G_GNUC_UNUSED
,
109 error_setg(errp
, "TLS credentials support requires GNUTLS");
114 qcrypto_tls_creds_anon_unload(QCryptoTLSCredsAnon
*creds G_GNUC_UNUSED
)
120 #endif /* ! CONFIG_GNUTLS */
124 qcrypto_tls_creds_anon_prop_set_loaded(Object
*obj
,
128 QCryptoTLSCredsAnon
*creds
= QCRYPTO_TLS_CREDS_ANON(obj
);
131 qcrypto_tls_creds_anon_load(creds
, errp
);
133 qcrypto_tls_creds_anon_unload(creds
);
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
;
150 return creds
->data
.client
!= NULL
;
155 #else /* ! CONFIG_GNUTLS */
159 qcrypto_tls_creds_anon_prop_get_loaded(Object
*obj G_GNUC_UNUSED
,
160 Error
**errp G_GNUC_UNUSED
)
166 #endif /* ! CONFIG_GNUTLS */
170 qcrypto_tls_creds_anon_complete(UserCreatable
*uc
, Error
**errp
)
172 object_property_set_bool(OBJECT(uc
), true, "loaded", errp
);
177 qcrypto_tls_creds_anon_finalize(Object
*obj
)
179 QCryptoTLSCredsAnon
*creds
= QCRYPTO_TLS_CREDS_ANON(obj
);
181 qcrypto_tls_creds_anon_unload(creds
);
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
,
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
},
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
);