2 * QEMU crypto TLS session 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 "crypto/tlssession.h"
23 #include "crypto/tlscredsanon.h"
24 #include "crypto/tlscredspsk.h"
25 #include "crypto/tlscredsx509.h"
26 #include "qapi/error.h"
33 #include <gnutls/x509.h>
36 struct QCryptoTLSSession {
37 QCryptoTLSCreds *creds;
38 gnutls_session_t handle;
41 bool handshakeComplete;
42 QCryptoTLSSessionWriteFunc writeFunc;
43 QCryptoTLSSessionReadFunc readFunc;
50 qcrypto_tls_session_free(QCryptoTLSSession *session)
56 gnutls_deinit(session->handle);
57 g_free(session->hostname);
58 g_free(session->peername);
59 g_free(session->aclname);
60 object_unref(OBJECT(session->creds));
66 qcrypto_tls_session_push(void *opaque, const void *buf, size_t len)
68 QCryptoTLSSession *session = opaque;
70 if (!session->writeFunc) {
75 return session->writeFunc(buf, len, session->opaque);
80 qcrypto_tls_session_pull(void *opaque, void *buf, size_t len)
82 QCryptoTLSSession *session = opaque;
84 if (!session->readFunc) {
89 return session->readFunc(buf, len, session->opaque);
92 #define TLS_PRIORITY_ADDITIONAL_ANON "+ANON-DH"
94 #if GNUTLS_VERSION_MAJOR >= 3
95 #define TLS_ECDHE_PSK "+ECDHE-PSK:"
97 #define TLS_ECDHE_PSK ""
99 #define TLS_PRIORITY_ADDITIONAL_PSK TLS_ECDHE_PSK "+DHE-PSK:+PSK"
102 qcrypto_tls_session_new(QCryptoTLSCreds *creds,
103 const char *hostname,
105 QCryptoTLSCredsEndpoint endpoint,
108 QCryptoTLSSession *session;
111 session = g_new0(QCryptoTLSSession, 1);
112 trace_qcrypto_tls_session_new(
113 session, creds, hostname ? hostname : "<none>",
114 aclname ? aclname : "<none>", endpoint);
117 session->hostname = g_strdup(hostname);
120 session->aclname = g_strdup(aclname);
122 session->creds = creds;
123 object_ref(OBJECT(creds));
125 if (creds->endpoint != endpoint) {
126 error_setg(errp, "Credentials endpoint doesn't match session");
130 if (endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
131 ret = gnutls_init(&session->handle, GNUTLS_SERVER);
133 ret = gnutls_init(&session->handle, GNUTLS_CLIENT);
136 error_setg(errp, "Cannot initialize TLS session: %s",
137 gnutls_strerror(ret));
141 if (object_dynamic_cast(OBJECT(creds),
142 TYPE_QCRYPTO_TLS_CREDS_ANON)) {
143 QCryptoTLSCredsAnon *acreds = QCRYPTO_TLS_CREDS_ANON(creds);
146 if (creds->priority != NULL) {
147 prio = g_strdup_printf("%s:%s",
149 TLS_PRIORITY_ADDITIONAL_ANON);
151 prio = g_strdup(CONFIG_TLS_PRIORITY ":"
152 TLS_PRIORITY_ADDITIONAL_ANON);
155 ret = gnutls_priority_set_direct(session->handle, prio, NULL);
157 error_setg(errp, "Unable to set TLS session priority %s: %s",
158 prio, gnutls_strerror(ret));
163 if (creds->endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
164 ret = gnutls_credentials_set(session->handle,
166 acreds->data.server);
168 ret = gnutls_credentials_set(session->handle,
170 acreds->data.client);
173 error_setg(errp, "Cannot set session credentials: %s",
174 gnutls_strerror(ret));
177 } else if (object_dynamic_cast(OBJECT(creds),
178 TYPE_QCRYPTO_TLS_CREDS_PSK)) {
179 QCryptoTLSCredsPSK *pcreds = QCRYPTO_TLS_CREDS_PSK(creds);
182 if (creds->priority != NULL) {
183 prio = g_strdup_printf("%s:%s",
185 TLS_PRIORITY_ADDITIONAL_PSK);
187 prio = g_strdup(CONFIG_TLS_PRIORITY ":"
188 TLS_PRIORITY_ADDITIONAL_PSK);
191 ret = gnutls_priority_set_direct(session->handle, prio, NULL);
193 error_setg(errp, "Unable to set TLS session priority %s: %s",
194 prio, gnutls_strerror(ret));
199 if (creds->endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
200 ret = gnutls_credentials_set(session->handle,
202 pcreds->data.server);
204 ret = gnutls_credentials_set(session->handle,
206 pcreds->data.client);
209 error_setg(errp, "Cannot set session credentials: %s",
210 gnutls_strerror(ret));
213 } else if (object_dynamic_cast(OBJECT(creds),
214 TYPE_QCRYPTO_TLS_CREDS_X509)) {
215 QCryptoTLSCredsX509 *tcreds = QCRYPTO_TLS_CREDS_X509(creds);
216 const char *prio = creds->priority;
218 prio = CONFIG_TLS_PRIORITY;
221 ret = gnutls_priority_set_direct(session->handle, prio, NULL);
223 error_setg(errp, "Cannot set default TLS session priority %s: %s",
224 prio, gnutls_strerror(ret));
227 ret = gnutls_credentials_set(session->handle,
228 GNUTLS_CRD_CERTIFICATE,
231 error_setg(errp, "Cannot set session credentials: %s",
232 gnutls_strerror(ret));
236 if (creds->endpoint == QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
237 /* This requests, but does not enforce a client cert.
238 * The cert checking code later does enforcement */
239 gnutls_certificate_server_set_request(session->handle,
240 GNUTLS_CERT_REQUEST);
243 error_setg(errp, "Unsupported TLS credentials type %s",
244 object_get_typename(OBJECT(creds)));
248 gnutls_transport_set_ptr(session->handle, session);
249 gnutls_transport_set_push_function(session->handle,
250 qcrypto_tls_session_push);
251 gnutls_transport_set_pull_function(session->handle,
252 qcrypto_tls_session_pull);
257 qcrypto_tls_session_free(session);
262 qcrypto_tls_session_check_certificate(QCryptoTLSSession *session,
267 const gnutls_datum_t *certs;
268 unsigned int nCerts, i;
270 gnutls_x509_crt_t cert = NULL;
273 if (now == ((time_t)-1)) {
274 error_setg_errno(errp, errno, "Cannot get current time");
278 ret = gnutls_certificate_verify_peers2(session->handle, &status);
280 error_setg(errp, "Verify failed: %s", gnutls_strerror(ret));
285 const char *reason = "Invalid certificate";
287 if (status & GNUTLS_CERT_INVALID) {
288 reason = "The certificate is not trusted";
291 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND) {
292 reason = "The certificate hasn't got a known issuer";
295 if (status & GNUTLS_CERT_REVOKED) {
296 reason = "The certificate has been revoked";
299 if (status & GNUTLS_CERT_INSECURE_ALGORITHM) {
300 reason = "The certificate uses an insecure algorithm";
303 error_setg(errp, "%s", reason);
307 certs = gnutls_certificate_get_peers(session->handle, &nCerts);
309 error_setg(errp, "No certificate peers");
313 for (i = 0; i < nCerts; i++) {
314 ret = gnutls_x509_crt_init(&cert);
316 error_setg(errp, "Cannot initialize certificate: %s",
317 gnutls_strerror(ret));
321 ret = gnutls_x509_crt_import(cert, &certs[i], GNUTLS_X509_FMT_DER);
323 error_setg(errp, "Cannot import certificate: %s",
324 gnutls_strerror(ret));
328 if (gnutls_x509_crt_get_expiration_time(cert) < now) {
329 error_setg(errp, "The certificate has expired");
333 if (gnutls_x509_crt_get_activation_time(cert) > now) {
334 error_setg(errp, "The certificate is not yet activated");
338 if (gnutls_x509_crt_get_activation_time(cert) > now) {
339 error_setg(errp, "The certificate is not yet activated");
344 size_t dnameSize = 1024;
345 session->peername = g_malloc(dnameSize);
347 ret = gnutls_x509_crt_get_dn(cert, session->peername, &dnameSize);
349 if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) {
350 session->peername = g_realloc(session->peername,
354 error_setg(errp, "Cannot get client distinguished name: %s",
355 gnutls_strerror(ret));
358 if (session->aclname) {
359 qemu_acl *acl = qemu_acl_find(session->aclname);
362 error_setg(errp, "Cannot find ACL %s",
367 allow = qemu_acl_party_is_allowed(acl, session->peername);
370 error_setg(errp, "TLS x509 ACL check for %s is denied",
375 if (session->hostname) {
376 if (!gnutls_x509_crt_check_hostname(cert, session->hostname)) {
378 "Certificate does not match the hostname %s",
385 gnutls_x509_crt_deinit(cert);
391 gnutls_x509_crt_deinit(cert);
397 qcrypto_tls_session_check_credentials(QCryptoTLSSession *session,
400 if (object_dynamic_cast(OBJECT(session->creds),
401 TYPE_QCRYPTO_TLS_CREDS_ANON)) {
402 trace_qcrypto_tls_session_check_creds(session, "nop");
404 } else if (object_dynamic_cast(OBJECT(session->creds),
405 TYPE_QCRYPTO_TLS_CREDS_PSK)) {
406 trace_qcrypto_tls_session_check_creds(session, "nop");
408 } else if (object_dynamic_cast(OBJECT(session->creds),
409 TYPE_QCRYPTO_TLS_CREDS_X509)) {
410 if (session->creds->verifyPeer) {
411 int ret = qcrypto_tls_session_check_certificate(session,
413 trace_qcrypto_tls_session_check_creds(session,
414 ret == 0 ? "pass" : "fail");
417 trace_qcrypto_tls_session_check_creds(session, "skip");
421 trace_qcrypto_tls_session_check_creds(session, "error");
422 error_setg(errp, "Unexpected credential type %s",
423 object_get_typename(OBJECT(session->creds)));
430 qcrypto_tls_session_set_callbacks(QCryptoTLSSession *session,
431 QCryptoTLSSessionWriteFunc writeFunc,
432 QCryptoTLSSessionReadFunc readFunc,
435 session->writeFunc = writeFunc;
436 session->readFunc = readFunc;
437 session->opaque = opaque;
442 qcrypto_tls_session_write(QCryptoTLSSession *session,
446 ssize_t ret = gnutls_record_send(session->handle, buf, len);
453 case GNUTLS_E_INTERRUPTED:
468 qcrypto_tls_session_read(QCryptoTLSSession *session,
472 ssize_t ret = gnutls_record_recv(session->handle, buf, len);
479 case GNUTLS_E_INTERRUPTED:
494 qcrypto_tls_session_handshake(QCryptoTLSSession *session,
497 int ret = gnutls_handshake(session->handle);
499 session->handshakeComplete = true;
501 if (ret == GNUTLS_E_INTERRUPTED ||
502 ret == GNUTLS_E_AGAIN) {
505 error_setg(errp, "TLS handshake failed: %s",
506 gnutls_strerror(ret));
515 QCryptoTLSSessionHandshakeStatus
516 qcrypto_tls_session_get_handshake_status(QCryptoTLSSession *session)
518 if (session->handshakeComplete) {
519 return QCRYPTO_TLS_HANDSHAKE_COMPLETE;
520 } else if (gnutls_record_get_direction(session->handle) == 0) {
521 return QCRYPTO_TLS_HANDSHAKE_RECVING;
523 return QCRYPTO_TLS_HANDSHAKE_SENDING;
529 qcrypto_tls_session_get_key_size(QCryptoTLSSession *session,
532 gnutls_cipher_algorithm_t cipher;
535 cipher = gnutls_cipher_get(session->handle);
536 ssf = gnutls_cipher_get_key_size(cipher);
538 error_setg(errp, "Cannot get TLS cipher key size");
546 qcrypto_tls_session_get_peer_name(QCryptoTLSSession *session)
548 if (session->peername) {
549 return g_strdup(session->peername);
555 #else /* ! CONFIG_GNUTLS */
559 qcrypto_tls_session_new(QCryptoTLSCreds *creds G_GNUC_UNUSED,
560 const char *hostname G_GNUC_UNUSED,
561 const char *aclname G_GNUC_UNUSED,
562 QCryptoTLSCredsEndpoint endpoint G_GNUC_UNUSED,
565 error_setg(errp, "TLS requires GNUTLS support");
571 qcrypto_tls_session_free(QCryptoTLSSession *sess G_GNUC_UNUSED)
577 qcrypto_tls_session_check_credentials(QCryptoTLSSession *sess G_GNUC_UNUSED,
580 error_setg(errp, "TLS requires GNUTLS support");
586 qcrypto_tls_session_set_callbacks(
587 QCryptoTLSSession *sess G_GNUC_UNUSED,
588 QCryptoTLSSessionWriteFunc writeFunc G_GNUC_UNUSED,
589 QCryptoTLSSessionReadFunc readFunc G_GNUC_UNUSED,
590 void *opaque G_GNUC_UNUSED)
596 qcrypto_tls_session_write(QCryptoTLSSession *sess,
606 qcrypto_tls_session_read(QCryptoTLSSession *sess,
616 qcrypto_tls_session_handshake(QCryptoTLSSession *sess,
619 error_setg(errp, "TLS requires GNUTLS support");
624 QCryptoTLSSessionHandshakeStatus
625 qcrypto_tls_session_get_handshake_status(QCryptoTLSSession *sess)
627 return QCRYPTO_TLS_HANDSHAKE_COMPLETE;
632 qcrypto_tls_session_get_key_size(QCryptoTLSSession *sess,
635 error_setg(errp, "TLS requires GNUTLS support");
641 qcrypto_tls_session_get_peer_name(QCryptoTLSSession *sess)