2 * QEMU crypto TLS 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 "qapi/error.h"
23 #include "qemu/module.h"
24 #include "tlscredspriv.h"
31 qcrypto_tls_creds_get_dh_params_file(QCryptoTLSCreds
*creds
,
33 gnutls_dh_params_t
*dh_params
,
38 trace_qcrypto_tls_creds_load_dh(creds
, filename
? filename
: "<generated>");
40 if (filename
== NULL
) {
41 ret
= gnutls_dh_params_init(dh_params
);
43 error_setg(errp
, "Unable to initialize DH parameters: %s",
44 gnutls_strerror(ret
));
47 ret
= gnutls_dh_params_generate2(*dh_params
, DH_BITS
);
49 gnutls_dh_params_deinit(*dh_params
);
51 error_setg(errp
, "Unable to generate DH parameters: %s",
52 gnutls_strerror(ret
));
60 if (!g_file_get_contents(filename
,
65 error_setg(errp
, "%s", gerr
->message
);
69 data
.data
= (unsigned char *)contents
;
71 ret
= gnutls_dh_params_init(dh_params
);
74 error_setg(errp
, "Unable to initialize DH parameters: %s",
75 gnutls_strerror(ret
));
78 ret
= gnutls_dh_params_import_pkcs3(*dh_params
,
83 gnutls_dh_params_deinit(*dh_params
);
85 error_setg(errp
, "Unable to load DH parameters from %s: %s",
86 filename
, gnutls_strerror(ret
));
96 qcrypto_tls_creds_get_path(QCryptoTLSCreds
*creds
,
107 error_setg(errp
, "Missing 'dir' property value");
114 *cred
= g_strdup_printf("%s/%s", creds
->dir
, filename
);
116 if (stat(*cred
, &sb
) < 0) {
117 if (errno
== ENOENT
&& !required
) {
120 error_setg_errno(errp
, errno
,
121 "Unable to access credentials %s",
131 trace_qcrypto_tls_creds_get_path(creds
, filename
,
132 *cred
? *cred
: "<none>");
137 #endif /* ! CONFIG_GNUTLS */
141 qcrypto_tls_creds_prop_set_verify(Object
*obj
,
143 Error
**errp G_GNUC_UNUSED
)
145 QCryptoTLSCreds
*creds
= QCRYPTO_TLS_CREDS(obj
);
147 creds
->verifyPeer
= value
;
152 qcrypto_tls_creds_prop_get_verify(Object
*obj
,
153 Error
**errp G_GNUC_UNUSED
)
155 QCryptoTLSCreds
*creds
= QCRYPTO_TLS_CREDS(obj
);
157 return creds
->verifyPeer
;
162 qcrypto_tls_creds_prop_set_dir(Object
*obj
,
164 Error
**errp G_GNUC_UNUSED
)
166 QCryptoTLSCreds
*creds
= QCRYPTO_TLS_CREDS(obj
);
168 creds
->dir
= g_strdup(value
);
173 qcrypto_tls_creds_prop_get_dir(Object
*obj
,
174 Error
**errp G_GNUC_UNUSED
)
176 QCryptoTLSCreds
*creds
= QCRYPTO_TLS_CREDS(obj
);
178 return g_strdup(creds
->dir
);
183 qcrypto_tls_creds_prop_set_priority(Object
*obj
,
185 Error
**errp G_GNUC_UNUSED
)
187 QCryptoTLSCreds
*creds
= QCRYPTO_TLS_CREDS(obj
);
189 creds
->priority
= g_strdup(value
);
194 qcrypto_tls_creds_prop_get_priority(Object
*obj
,
195 Error
**errp G_GNUC_UNUSED
)
197 QCryptoTLSCreds
*creds
= QCRYPTO_TLS_CREDS(obj
);
199 return g_strdup(creds
->priority
);
204 qcrypto_tls_creds_prop_set_endpoint(Object
*obj
,
206 Error
**errp G_GNUC_UNUSED
)
208 QCryptoTLSCreds
*creds
= QCRYPTO_TLS_CREDS(obj
);
210 creds
->endpoint
= value
;
215 qcrypto_tls_creds_prop_get_endpoint(Object
*obj
,
216 Error
**errp G_GNUC_UNUSED
)
218 QCryptoTLSCreds
*creds
= QCRYPTO_TLS_CREDS(obj
);
220 return creds
->endpoint
;
225 qcrypto_tls_creds_class_init(ObjectClass
*oc
, void *data
)
227 object_class_property_add_bool(oc
, "verify-peer",
228 qcrypto_tls_creds_prop_get_verify
,
229 qcrypto_tls_creds_prop_set_verify
,
231 object_class_property_add_str(oc
, "dir",
232 qcrypto_tls_creds_prop_get_dir
,
233 qcrypto_tls_creds_prop_set_dir
,
235 object_class_property_add_enum(oc
, "endpoint",
236 "QCryptoTLSCredsEndpoint",
237 &QCryptoTLSCredsEndpoint_lookup
,
238 qcrypto_tls_creds_prop_get_endpoint
,
239 qcrypto_tls_creds_prop_set_endpoint
,
241 object_class_property_add_str(oc
, "priority",
242 qcrypto_tls_creds_prop_get_priority
,
243 qcrypto_tls_creds_prop_set_priority
,
249 qcrypto_tls_creds_init(Object
*obj
)
251 QCryptoTLSCreds
*creds
= QCRYPTO_TLS_CREDS(obj
);
253 creds
->verifyPeer
= true;
258 qcrypto_tls_creds_finalize(Object
*obj
)
260 QCryptoTLSCreds
*creds
= QCRYPTO_TLS_CREDS(obj
);
263 g_free(creds
->priority
);
267 static const TypeInfo qcrypto_tls_creds_info
= {
268 .parent
= TYPE_OBJECT
,
269 .name
= TYPE_QCRYPTO_TLS_CREDS
,
270 .instance_size
= sizeof(QCryptoTLSCreds
),
271 .instance_init
= qcrypto_tls_creds_init
,
272 .instance_finalize
= qcrypto_tls_creds_finalize
,
273 .class_init
= qcrypto_tls_creds_class_init
,
274 .class_size
= sizeof(QCryptoTLSCredsClass
),
280 qcrypto_tls_creds_register_types(void)
282 type_register_static(&qcrypto_tls_creds_info
);
286 type_init(qcrypto_tls_creds_register_types
);