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 "crypto/tlscredspriv.h"
30 qcrypto_tls_creds_get_dh_params_file(QCryptoTLSCreds
*creds
,
32 gnutls_dh_params_t
*dh_params
,
37 trace_qcrypto_tls_creds_load_dh(creds
, filename
? filename
: "<generated>");
39 if (filename
== NULL
) {
40 ret
= gnutls_dh_params_init(dh_params
);
42 error_setg(errp
, "Unable to initialize DH parameters: %s",
43 gnutls_strerror(ret
));
46 ret
= gnutls_dh_params_generate2(*dh_params
, DH_BITS
);
48 gnutls_dh_params_deinit(*dh_params
);
50 error_setg(errp
, "Unable to generate DH parameters: %s",
51 gnutls_strerror(ret
));
59 if (!g_file_get_contents(filename
,
64 error_setg(errp
, "%s", gerr
->message
);
68 data
.data
= (unsigned char *)contents
;
70 ret
= gnutls_dh_params_init(dh_params
);
73 error_setg(errp
, "Unable to initialize DH parameters: %s",
74 gnutls_strerror(ret
));
77 ret
= gnutls_dh_params_import_pkcs3(*dh_params
,
82 gnutls_dh_params_deinit(*dh_params
);
84 error_setg(errp
, "Unable to load DH parameters from %s: %s",
85 filename
, gnutls_strerror(ret
));
95 qcrypto_tls_creds_get_path(QCryptoTLSCreds
*creds
,
106 error_setg(errp
, "Missing 'dir' property value");
113 *cred
= g_strdup_printf("%s/%s", creds
->dir
, filename
);
115 if (stat(*cred
, &sb
) < 0) {
116 if (errno
== ENOENT
&& !required
) {
119 error_setg_errno(errp
, errno
,
120 "Unable to access credentials %s",
130 trace_qcrypto_tls_creds_get_path(creds
, filename
,
131 *cred
? *cred
: "<none>");
136 #endif /* ! CONFIG_GNUTLS */
140 qcrypto_tls_creds_prop_set_verify(Object
*obj
,
142 Error
**errp G_GNUC_UNUSED
)
144 QCryptoTLSCreds
*creds
= QCRYPTO_TLS_CREDS(obj
);
146 creds
->verifyPeer
= value
;
151 qcrypto_tls_creds_prop_get_verify(Object
*obj
,
152 Error
**errp G_GNUC_UNUSED
)
154 QCryptoTLSCreds
*creds
= QCRYPTO_TLS_CREDS(obj
);
156 return creds
->verifyPeer
;
161 qcrypto_tls_creds_prop_set_dir(Object
*obj
,
163 Error
**errp G_GNUC_UNUSED
)
165 QCryptoTLSCreds
*creds
= QCRYPTO_TLS_CREDS(obj
);
167 creds
->dir
= g_strdup(value
);
172 qcrypto_tls_creds_prop_get_dir(Object
*obj
,
173 Error
**errp G_GNUC_UNUSED
)
175 QCryptoTLSCreds
*creds
= QCRYPTO_TLS_CREDS(obj
);
177 return g_strdup(creds
->dir
);
182 qcrypto_tls_creds_prop_set_priority(Object
*obj
,
184 Error
**errp G_GNUC_UNUSED
)
186 QCryptoTLSCreds
*creds
= QCRYPTO_TLS_CREDS(obj
);
188 creds
->priority
= g_strdup(value
);
193 qcrypto_tls_creds_prop_get_priority(Object
*obj
,
194 Error
**errp G_GNUC_UNUSED
)
196 QCryptoTLSCreds
*creds
= QCRYPTO_TLS_CREDS(obj
);
198 return g_strdup(creds
->priority
);
203 qcrypto_tls_creds_prop_set_endpoint(Object
*obj
,
205 Error
**errp G_GNUC_UNUSED
)
207 QCryptoTLSCreds
*creds
= QCRYPTO_TLS_CREDS(obj
);
209 creds
->endpoint
= value
;
214 qcrypto_tls_creds_prop_get_endpoint(Object
*obj
,
215 Error
**errp G_GNUC_UNUSED
)
217 QCryptoTLSCreds
*creds
= QCRYPTO_TLS_CREDS(obj
);
219 return creds
->endpoint
;
224 qcrypto_tls_creds_class_init(ObjectClass
*oc
, void *data
)
226 object_class_property_add_bool(oc
, "verify-peer",
227 qcrypto_tls_creds_prop_get_verify
,
228 qcrypto_tls_creds_prop_set_verify
,
230 object_class_property_add_str(oc
, "dir",
231 qcrypto_tls_creds_prop_get_dir
,
232 qcrypto_tls_creds_prop_set_dir
,
234 object_class_property_add_enum(oc
, "endpoint",
235 "QCryptoTLSCredsEndpoint",
236 &QCryptoTLSCredsEndpoint_lookup
,
237 qcrypto_tls_creds_prop_get_endpoint
,
238 qcrypto_tls_creds_prop_set_endpoint
,
240 object_class_property_add_str(oc
, "priority",
241 qcrypto_tls_creds_prop_get_priority
,
242 qcrypto_tls_creds_prop_set_priority
,
248 qcrypto_tls_creds_init(Object
*obj
)
250 QCryptoTLSCreds
*creds
= QCRYPTO_TLS_CREDS(obj
);
252 creds
->verifyPeer
= true;
257 qcrypto_tls_creds_finalize(Object
*obj
)
259 QCryptoTLSCreds
*creds
= QCRYPTO_TLS_CREDS(obj
);
262 g_free(creds
->priority
);
266 static const TypeInfo qcrypto_tls_creds_info
= {
267 .parent
= TYPE_OBJECT
,
268 .name
= TYPE_QCRYPTO_TLS_CREDS
,
269 .instance_size
= sizeof(QCryptoTLSCreds
),
270 .instance_init
= qcrypto_tls_creds_init
,
271 .instance_finalize
= qcrypto_tls_creds_finalize
,
272 .class_init
= qcrypto_tls_creds_class_init
,
273 .class_size
= sizeof(QCryptoTLSCredsClass
),
279 qcrypto_tls_creds_register_types(void)
281 type_register_static(&qcrypto_tls_creds_info
);
285 type_init(qcrypto_tls_creds_register_types
);