2 * QEMU crypto TLS Pre-Shared Keys (PSK) support
4 * Copyright (c) 2018 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 "crypto/tlscredspsk.h"
23 #include "tlscredspriv.h"
24 #include "qapi/error.h"
25 #include "qemu/module.h"
26 #include "qom/object_interfaces.h"
32 #include <gnutls/gnutls.h>
35 lookup_key(const char *pskfile
, const char *username
, gnutls_datum_t
*key
,
38 const size_t ulen
= strlen(username
);
45 if (!g_file_get_contents(pskfile
, &content
, &clen
, &gerr
)) {
46 error_setg(errp
, "Cannot read PSK file %s: %s",
47 pskfile
, gerr
->message
);
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;
61 error_setg(errp
, "Username %s not found in PSK file %s",
71 qcrypto_tls_creds_psk_load(QCryptoTLSCredsPSK
*creds
,
74 g_autofree
char *pskfile
= NULL
;
75 g_autofree
char *dhparams
= NULL
;
79 gnutls_datum_t key
= { .data
= NULL
};
81 trace_qcrypto_tls_creds_psk_load(creds
,
82 creds
->parent_obj
.dir
? creds
->parent_obj
.dir
: "<nodir>");
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");
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) {
99 ret
= gnutls_psk_allocate_server_credentials(&creds
->data
.server
);
101 error_setg(errp
, "Cannot allocate credentials: %s",
102 gnutls_strerror(ret
));
106 if (qcrypto_tls_creds_get_dh_params_file(&creds
->parent_obj
, dhparams
,
107 &creds
->parent_obj
.dh_params
,
112 gnutls_psk_set_server_credentials_file(creds
->data
.server
, pskfile
);
113 gnutls_psk_set_server_dh_params(creds
->data
.server
,
114 creds
->parent_obj
.dh_params
);
116 if (qcrypto_tls_creds_get_path(&creds
->parent_obj
,
117 QCRYPTO_TLS_CREDS_PSKFILE
,
118 true, &pskfile
, errp
) < 0) {
122 if (creds
->username
) {
123 username
= creds
->username
;
127 if (lookup_key(pskfile
, username
, &key
, errp
) != 0) {
131 ret
= gnutls_psk_allocate_client_credentials(&creds
->data
.client
);
133 error_setg(errp
, "Cannot allocate credentials: %s",
134 gnutls_strerror(ret
));
138 gnutls_psk_set_client_credentials(creds
->data
.client
,
139 username
, &key
, GNUTLS_PSK_KEY_HEX
);
150 qcrypto_tls_creds_psk_unload(QCryptoTLSCredsPSK
*creds
)
152 if (creds
->parent_obj
.endpoint
== QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
) {
153 if (creds
->data
.client
) {
154 gnutls_psk_free_client_credentials(creds
->data
.client
);
155 creds
->data
.client
= NULL
;
158 if (creds
->data
.server
) {
159 gnutls_psk_free_server_credentials(creds
->data
.server
);
160 creds
->data
.server
= NULL
;
163 if (creds
->parent_obj
.dh_params
) {
164 gnutls_dh_params_deinit(creds
->parent_obj
.dh_params
);
165 creds
->parent_obj
.dh_params
= NULL
;
169 #else /* ! CONFIG_GNUTLS */
173 qcrypto_tls_creds_psk_load(QCryptoTLSCredsPSK
*creds G_GNUC_UNUSED
,
176 error_setg(errp
, "TLS credentials support requires GNUTLS");
181 qcrypto_tls_creds_psk_unload(QCryptoTLSCredsPSK
*creds G_GNUC_UNUSED
)
187 #endif /* ! CONFIG_GNUTLS */
191 qcrypto_tls_creds_psk_complete(UserCreatable
*uc
, Error
**errp
)
193 QCryptoTLSCredsPSK
*creds
= QCRYPTO_TLS_CREDS_PSK(uc
);
195 qcrypto_tls_creds_psk_load(creds
, errp
);
203 qcrypto_tls_creds_psk_prop_get_loaded(Object
*obj
,
204 Error
**errp G_GNUC_UNUSED
)
206 QCryptoTLSCredsPSK
*creds
= QCRYPTO_TLS_CREDS_PSK(obj
);
208 if (creds
->parent_obj
.endpoint
== QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
) {
209 return creds
->data
.server
!= NULL
;
211 return creds
->data
.client
!= NULL
;
216 #else /* ! CONFIG_GNUTLS */
220 qcrypto_tls_creds_psk_prop_get_loaded(Object
*obj G_GNUC_UNUSED
,
221 Error
**errp G_GNUC_UNUSED
)
227 #endif /* ! CONFIG_GNUTLS */
231 qcrypto_tls_creds_psk_finalize(Object
*obj
)
233 QCryptoTLSCredsPSK
*creds
= QCRYPTO_TLS_CREDS_PSK(obj
);
235 qcrypto_tls_creds_psk_unload(creds
);
239 qcrypto_tls_creds_psk_prop_set_username(Object
*obj
,
241 Error
**errp G_GNUC_UNUSED
)
243 QCryptoTLSCredsPSK
*creds
= QCRYPTO_TLS_CREDS_PSK(obj
);
245 creds
->username
= g_strdup(value
);
250 qcrypto_tls_creds_psk_prop_get_username(Object
*obj
,
251 Error
**errp G_GNUC_UNUSED
)
253 QCryptoTLSCredsPSK
*creds
= QCRYPTO_TLS_CREDS_PSK(obj
);
255 return g_strdup(creds
->username
);
259 qcrypto_tls_creds_psk_class_init(ObjectClass
*oc
, void *data
)
261 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
263 ucc
->complete
= qcrypto_tls_creds_psk_complete
;
265 object_class_property_add_bool(oc
, "loaded",
266 qcrypto_tls_creds_psk_prop_get_loaded
,
268 object_class_property_add_str(oc
, "username",
269 qcrypto_tls_creds_psk_prop_get_username
,
270 qcrypto_tls_creds_psk_prop_set_username
);
274 static const TypeInfo qcrypto_tls_creds_psk_info
= {
275 .parent
= TYPE_QCRYPTO_TLS_CREDS
,
276 .name
= TYPE_QCRYPTO_TLS_CREDS_PSK
,
277 .instance_size
= sizeof(QCryptoTLSCredsPSK
),
278 .instance_finalize
= qcrypto_tls_creds_psk_finalize
,
279 .class_size
= sizeof(QCryptoTLSCredsPSKClass
),
280 .class_init
= qcrypto_tls_creds_psk_class_init
,
281 .interfaces
= (InterfaceInfo
[]) {
282 { TYPE_USER_CREATABLE
},
289 qcrypto_tls_creds_psk_register_types(void)
291 type_register_static(&qcrypto_tls_creds_psk_info
);
295 type_init(qcrypto_tls_creds_psk_register_types
);