qapi: Tidy up a slightly awkward TODO comment
[qemu/armbru.git] / crypto / tlscreds.c
blob084ce0d51ae75449f7c37d628457c2a21f907064
1 /*
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.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 "qapi/error.h"
23 #include "qapi-types-crypto.h"
24 #include "qemu/module.h"
25 #include "tlscredspriv.h"
26 #include "trace.h"
28 #define DH_BITS 2048
30 #ifdef CONFIG_GNUTLS
31 int
32 qcrypto_tls_creds_get_dh_params_file(QCryptoTLSCreds *creds,
33 const char *filename,
34 gnutls_dh_params_t *dh_params,
35 Error **errp)
37 int ret;
39 trace_qcrypto_tls_creds_load_dh(creds, filename ? filename : "<generated>");
41 if (filename == NULL) {
42 ret = gnutls_dh_params_init(dh_params);
43 if (ret < 0) {
44 error_setg(errp, "Unable to initialize DH parameters: %s",
45 gnutls_strerror(ret));
46 return -1;
48 ret = gnutls_dh_params_generate2(*dh_params, DH_BITS);
49 if (ret < 0) {
50 gnutls_dh_params_deinit(*dh_params);
51 *dh_params = NULL;
52 error_setg(errp, "Unable to generate DH parameters: %s",
53 gnutls_strerror(ret));
54 return -1;
56 } else {
57 GError *gerr = NULL;
58 gchar *contents;
59 gsize len;
60 gnutls_datum_t data;
61 if (!g_file_get_contents(filename,
62 &contents,
63 &len,
64 &gerr)) {
66 error_setg(errp, "%s", gerr->message);
67 g_error_free(gerr);
68 return -1;
70 data.data = (unsigned char *)contents;
71 data.size = len;
72 ret = gnutls_dh_params_init(dh_params);
73 if (ret < 0) {
74 g_free(contents);
75 error_setg(errp, "Unable to initialize DH parameters: %s",
76 gnutls_strerror(ret));
77 return -1;
79 ret = gnutls_dh_params_import_pkcs3(*dh_params,
80 &data,
81 GNUTLS_X509_FMT_PEM);
82 g_free(contents);
83 if (ret < 0) {
84 gnutls_dh_params_deinit(*dh_params);
85 *dh_params = NULL;
86 error_setg(errp, "Unable to load DH parameters from %s: %s",
87 filename, gnutls_strerror(ret));
88 return -1;
92 return 0;
96 int
97 qcrypto_tls_creds_get_path(QCryptoTLSCreds *creds,
98 const char *filename,
99 bool required,
100 char **cred,
101 Error **errp)
103 struct stat sb;
104 int ret = -1;
106 if (!creds->dir) {
107 if (required) {
108 error_setg(errp, "Missing 'dir' property value");
109 return -1;
110 } else {
111 return 0;
115 *cred = g_strdup_printf("%s/%s", creds->dir, filename);
117 if (stat(*cred, &sb) < 0) {
118 if (errno == ENOENT && !required) {
119 ret = 0;
120 } else {
121 error_setg_errno(errp, errno,
122 "Unable to access credentials %s",
123 *cred);
125 g_free(*cred);
126 *cred = NULL;
127 goto cleanup;
130 ret = 0;
131 cleanup:
132 trace_qcrypto_tls_creds_get_path(creds, filename,
133 *cred ? *cred : "<none>");
134 return ret;
138 #endif /* ! CONFIG_GNUTLS */
141 static void
142 qcrypto_tls_creds_prop_set_verify(Object *obj,
143 bool value,
144 Error **errp G_GNUC_UNUSED)
146 QCryptoTLSCreds *creds = QCRYPTO_TLS_CREDS(obj);
148 creds->verifyPeer = value;
152 static bool
153 qcrypto_tls_creds_prop_get_verify(Object *obj,
154 Error **errp G_GNUC_UNUSED)
156 QCryptoTLSCreds *creds = QCRYPTO_TLS_CREDS(obj);
158 return creds->verifyPeer;
162 static void
163 qcrypto_tls_creds_prop_set_dir(Object *obj,
164 const char *value,
165 Error **errp G_GNUC_UNUSED)
167 QCryptoTLSCreds *creds = QCRYPTO_TLS_CREDS(obj);
169 creds->dir = g_strdup(value);
173 static char *
174 qcrypto_tls_creds_prop_get_dir(Object *obj,
175 Error **errp G_GNUC_UNUSED)
177 QCryptoTLSCreds *creds = QCRYPTO_TLS_CREDS(obj);
179 return g_strdup(creds->dir);
183 static void
184 qcrypto_tls_creds_prop_set_priority(Object *obj,
185 const char *value,
186 Error **errp G_GNUC_UNUSED)
188 QCryptoTLSCreds *creds = QCRYPTO_TLS_CREDS(obj);
190 creds->priority = g_strdup(value);
194 static char *
195 qcrypto_tls_creds_prop_get_priority(Object *obj,
196 Error **errp G_GNUC_UNUSED)
198 QCryptoTLSCreds *creds = QCRYPTO_TLS_CREDS(obj);
200 return g_strdup(creds->priority);
204 static void
205 qcrypto_tls_creds_prop_set_endpoint(Object *obj,
206 int value,
207 Error **errp G_GNUC_UNUSED)
209 QCryptoTLSCreds *creds = QCRYPTO_TLS_CREDS(obj);
211 creds->endpoint = value;
215 static int
216 qcrypto_tls_creds_prop_get_endpoint(Object *obj,
217 Error **errp G_GNUC_UNUSED)
219 QCryptoTLSCreds *creds = QCRYPTO_TLS_CREDS(obj);
221 return creds->endpoint;
225 static void
226 qcrypto_tls_creds_class_init(ObjectClass *oc, void *data)
228 object_class_property_add_bool(oc, "verify-peer",
229 qcrypto_tls_creds_prop_get_verify,
230 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);
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);
239 object_class_property_add_str(oc, "priority",
240 qcrypto_tls_creds_prop_get_priority,
241 qcrypto_tls_creds_prop_set_priority);
245 static void
246 qcrypto_tls_creds_init(Object *obj)
248 QCryptoTLSCreds *creds = QCRYPTO_TLS_CREDS(obj);
250 creds->verifyPeer = true;
254 static void
255 qcrypto_tls_creds_finalize(Object *obj)
257 QCryptoTLSCreds *creds = QCRYPTO_TLS_CREDS(obj);
259 g_free(creds->dir);
260 g_free(creds->priority);
263 bool qcrypto_tls_creds_check_endpoint(QCryptoTLSCreds *creds,
264 QCryptoTLSCredsEndpoint endpoint,
265 Error **errp)
267 if (creds->endpoint != endpoint) {
268 error_setg(errp, "Expected TLS credentials for a %s endpoint",
269 QCryptoTLSCredsEndpoint_str(endpoint));
270 return false;
272 return true;
275 static const TypeInfo qcrypto_tls_creds_info = {
276 .parent = TYPE_OBJECT,
277 .name = TYPE_QCRYPTO_TLS_CREDS,
278 .instance_size = sizeof(QCryptoTLSCreds),
279 .instance_init = qcrypto_tls_creds_init,
280 .instance_finalize = qcrypto_tls_creds_finalize,
281 .class_init = qcrypto_tls_creds_class_init,
282 .class_size = sizeof(QCryptoTLSCredsClass),
283 .abstract = true,
287 static void
288 qcrypto_tls_creds_register_types(void)
290 type_register_static(&qcrypto_tls_creds_info);
294 type_init(qcrypto_tls_creds_register_types);