xref: /openbmc/qemu/crypto/tlscredspsk.c (revision ff788b70)
1 /*
2  * QEMU crypto TLS Pre-Shared Keys (PSK) support
3  *
4  * Copyright (c) 2018 Red Hat, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  *
19  */
20 
21 #include "qemu/osdep.h"
22 #include "crypto/tlscredspsk.h"
23 #include "tlscredspriv.h"
24 #include "qapi/error.h"
25 #include "qemu/module.h"
26 #include "qom/object_interfaces.h"
27 #include "trace.h"
28 
29 
30 #ifdef CONFIG_GNUTLS
31 
32 #include <gnutls/gnutls.h>
33 
34 static int
35 lookup_key(const char *pskfile, const char *username, gnutls_datum_t *key,
36            Error **errp)
37 {
38     const size_t ulen = strlen(username);
39     GError *gerr = NULL;
40     char *content = NULL;
41     char **lines = NULL;
42     size_t clen = 0, i;
43     int ret = -1;
44 
45     if (!g_file_get_contents(pskfile, &content, &clen, &gerr)) {
46         error_setg(errp, "Cannot read PSK file %s: %s",
47                    pskfile, gerr->message);
48         g_error_free(gerr);
49         return -1;
50     }
51 
52     lines = g_strsplit(content, "\n", -1);
53     for (i = 0; lines[i] != NULL; ++i) {
54         if (strncmp(lines[i], username, ulen) == 0 && lines[i][ulen] == ':') {
55             key->data = (unsigned char *) g_strdup(&lines[i][ulen + 1]);
56             key->size = strlen(lines[i]) - ulen - 1;
57             ret = 0;
58             goto out;
59         }
60     }
61     error_setg(errp, "Username %s not found in PSK file %s",
62                username, pskfile);
63 
64  out:
65     free(content);
66     g_strfreev(lines);
67     return ret;
68 }
69 
70 static int
71 qcrypto_tls_creds_psk_load(QCryptoTLSCredsPSK *creds,
72                            Error **errp)
73 {
74     g_autofree char *pskfile = NULL;
75     g_autofree char *dhparams = NULL;
76     const char *username;
77     int ret;
78     int rv = -1;
79     gnutls_datum_t key = { .data = NULL };
80 
81     trace_qcrypto_tls_creds_psk_load(creds,
82             creds->parent_obj.dir ? creds->parent_obj.dir : "<nodir>");
83 
84     if (creds->parent_obj.endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
85         if (creds->username) {
86             error_setg(errp, "username should not be set when endpoint=server");
87             goto cleanup;
88         }
89 
90         if (qcrypto_tls_creds_get_path(&creds->parent_obj,
91                                        QCRYPTO_TLS_CREDS_DH_PARAMS,
92                                        false, &dhparams, errp) < 0 ||
93             qcrypto_tls_creds_get_path(&creds->parent_obj,
94                                        QCRYPTO_TLS_CREDS_PSKFILE,
95                                        true, &pskfile, errp) < 0) {
96             goto cleanup;
97         }
98 
99         ret = gnutls_psk_allocate_server_credentials(&creds->data.server);
100         if (ret < 0) {
101             error_setg(errp, "Cannot allocate credentials: %s",
102                        gnutls_strerror(ret));
103             goto cleanup;
104         }
105 
106         if (qcrypto_tls_creds_get_dh_params_file(&creds->parent_obj, dhparams,
107                                                  &creds->parent_obj.dh_params,
108                                                  errp) < 0) {
109             goto cleanup;
110         }
111 
112         ret = gnutls_psk_set_server_credentials_file(creds->data.server, pskfile);
113         if (ret < 0) {
114             error_setg(errp, "Cannot set PSK server credentials: %s",
115                        gnutls_strerror(ret));
116             goto cleanup;
117         }
118         gnutls_psk_set_server_dh_params(creds->data.server,
119                                         creds->parent_obj.dh_params);
120     } else {
121         if (qcrypto_tls_creds_get_path(&creds->parent_obj,
122                                        QCRYPTO_TLS_CREDS_PSKFILE,
123                                        true, &pskfile, errp) < 0) {
124             goto cleanup;
125         }
126 
127         if (creds->username) {
128             username = creds->username;
129         } else {
130             username = "qemu";
131         }
132         if (lookup_key(pskfile, username, &key, errp) != 0) {
133             goto cleanup;
134         }
135 
136         ret = gnutls_psk_allocate_client_credentials(&creds->data.client);
137         if (ret < 0) {
138             error_setg(errp, "Cannot allocate credentials: %s",
139                        gnutls_strerror(ret));
140             goto cleanup;
141         }
142 
143         ret = gnutls_psk_set_client_credentials(creds->data.client,
144                                                 username, &key, GNUTLS_PSK_KEY_HEX);
145         if (ret < 0) {
146             error_setg(errp, "Cannot set PSK client credentials: %s",
147                        gnutls_strerror(ret));
148             goto cleanup;
149         }
150     }
151 
152     rv = 0;
153  cleanup:
154     g_free(key.data);
155     return rv;
156 }
157 
158 
159 static void
160 qcrypto_tls_creds_psk_unload(QCryptoTLSCredsPSK *creds)
161 {
162     if (creds->parent_obj.endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) {
163         if (creds->data.client) {
164             gnutls_psk_free_client_credentials(creds->data.client);
165             creds->data.client = NULL;
166         }
167     } else {
168         if (creds->data.server) {
169             gnutls_psk_free_server_credentials(creds->data.server);
170             creds->data.server = NULL;
171         }
172     }
173     if (creds->parent_obj.dh_params) {
174         gnutls_dh_params_deinit(creds->parent_obj.dh_params);
175         creds->parent_obj.dh_params = NULL;
176     }
177 }
178 
179 #else /* ! CONFIG_GNUTLS */
180 
181 
182 static void
183 qcrypto_tls_creds_psk_load(QCryptoTLSCredsPSK *creds G_GNUC_UNUSED,
184                            Error **errp)
185 {
186     error_setg(errp, "TLS credentials support requires GNUTLS");
187 }
188 
189 
190 static void
191 qcrypto_tls_creds_psk_unload(QCryptoTLSCredsPSK *creds G_GNUC_UNUSED)
192 {
193     /* nada */
194 }
195 
196 
197 #endif /* ! CONFIG_GNUTLS */
198 
199 
200 static void
201 qcrypto_tls_creds_psk_complete(UserCreatable *uc, Error **errp)
202 {
203     QCryptoTLSCredsPSK *creds = QCRYPTO_TLS_CREDS_PSK(uc);
204 
205     qcrypto_tls_creds_psk_load(creds, errp);
206 }
207 
208 
209 #ifdef CONFIG_GNUTLS
210 
211 
212 static bool
213 qcrypto_tls_creds_psk_prop_get_loaded(Object *obj,
214                                       Error **errp G_GNUC_UNUSED)
215 {
216     QCryptoTLSCredsPSK *creds = QCRYPTO_TLS_CREDS_PSK(obj);
217 
218     if (creds->parent_obj.endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
219         return creds->data.server != NULL;
220     } else {
221         return creds->data.client != NULL;
222     }
223 }
224 
225 
226 #else /* ! CONFIG_GNUTLS */
227 
228 
229 static bool
230 qcrypto_tls_creds_psk_prop_get_loaded(Object *obj G_GNUC_UNUSED,
231                                       Error **errp G_GNUC_UNUSED)
232 {
233     return false;
234 }
235 
236 
237 #endif /* ! CONFIG_GNUTLS */
238 
239 
240 static void
241 qcrypto_tls_creds_psk_finalize(Object *obj)
242 {
243     QCryptoTLSCredsPSK *creds = QCRYPTO_TLS_CREDS_PSK(obj);
244 
245     qcrypto_tls_creds_psk_unload(creds);
246     g_free(creds->username);
247 }
248 
249 static void
250 qcrypto_tls_creds_psk_prop_set_username(Object *obj,
251                                         const char *value,
252                                         Error **errp G_GNUC_UNUSED)
253 {
254     QCryptoTLSCredsPSK *creds = QCRYPTO_TLS_CREDS_PSK(obj);
255 
256     creds->username = g_strdup(value);
257 }
258 
259 
260 static char *
261 qcrypto_tls_creds_psk_prop_get_username(Object *obj,
262                                         Error **errp G_GNUC_UNUSED)
263 {
264     QCryptoTLSCredsPSK *creds = QCRYPTO_TLS_CREDS_PSK(obj);
265 
266     return g_strdup(creds->username);
267 }
268 
269 static void
270 qcrypto_tls_creds_psk_class_init(ObjectClass *oc, void *data)
271 {
272     UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
273 
274     ucc->complete = qcrypto_tls_creds_psk_complete;
275 
276     object_class_property_add_bool(oc, "loaded",
277                                    qcrypto_tls_creds_psk_prop_get_loaded,
278                                    NULL);
279     object_class_property_add_str(oc, "username",
280                                   qcrypto_tls_creds_psk_prop_get_username,
281                                   qcrypto_tls_creds_psk_prop_set_username);
282 }
283 
284 
285 static const TypeInfo qcrypto_tls_creds_psk_info = {
286     .parent = TYPE_QCRYPTO_TLS_CREDS,
287     .name = TYPE_QCRYPTO_TLS_CREDS_PSK,
288     .instance_size = sizeof(QCryptoTLSCredsPSK),
289     .instance_finalize = qcrypto_tls_creds_psk_finalize,
290     .class_size = sizeof(QCryptoTLSCredsPSKClass),
291     .class_init = qcrypto_tls_creds_psk_class_init,
292     .interfaces = (InterfaceInfo[]) {
293         { TYPE_USER_CREATABLE },
294         { }
295     }
296 };
297 
298 
299 static void
300 qcrypto_tls_creds_psk_register_types(void)
301 {
302     type_register_static(&qcrypto_tls_creds_psk_info);
303 }
304 
305 
306 type_init(qcrypto_tls_creds_psk_register_types);
307