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/tlscredsx509.h"
25 #include "qapi/error.h"
32 #include <gnutls/x509.h>
35 struct QCryptoTLSSession
{
36 QCryptoTLSCreds
*creds
;
37 gnutls_session_t handle
;
40 bool handshakeComplete
;
41 QCryptoTLSSessionWriteFunc writeFunc
;
42 QCryptoTLSSessionReadFunc readFunc
;
49 qcrypto_tls_session_free(QCryptoTLSSession
*session
)
55 gnutls_deinit(session
->handle
);
56 g_free(session
->hostname
);
57 g_free(session
->peername
);
58 g_free(session
->aclname
);
59 object_unref(OBJECT(session
->creds
));
65 qcrypto_tls_session_push(void *opaque
, const void *buf
, size_t len
)
67 QCryptoTLSSession
*session
= opaque
;
69 if (!session
->writeFunc
) {
74 return session
->writeFunc(buf
, len
, session
->opaque
);
79 qcrypto_tls_session_pull(void *opaque
, void *buf
, size_t len
)
81 QCryptoTLSSession
*session
= opaque
;
83 if (!session
->readFunc
) {
88 return session
->readFunc(buf
, len
, session
->opaque
);
93 qcrypto_tls_session_new(QCryptoTLSCreds
*creds
,
96 QCryptoTLSCredsEndpoint endpoint
,
99 QCryptoTLSSession
*session
;
102 session
= g_new0(QCryptoTLSSession
, 1);
103 trace_qcrypto_tls_session_new(
104 session
, creds
, hostname
? hostname
: "<none>",
105 aclname
? aclname
: "<none>", endpoint
);
108 session
->hostname
= g_strdup(hostname
);
111 session
->aclname
= g_strdup(aclname
);
113 session
->creds
= creds
;
114 object_ref(OBJECT(creds
));
116 if (creds
->endpoint
!= endpoint
) {
117 error_setg(errp
, "Credentials endpoint doesn't match session");
121 if (endpoint
== QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
) {
122 ret
= gnutls_init(&session
->handle
, GNUTLS_SERVER
);
124 ret
= gnutls_init(&session
->handle
, GNUTLS_CLIENT
);
127 error_setg(errp
, "Cannot initialize TLS session: %s",
128 gnutls_strerror(ret
));
132 if (object_dynamic_cast(OBJECT(creds
),
133 TYPE_QCRYPTO_TLS_CREDS_ANON
)) {
134 QCryptoTLSCredsAnon
*acreds
= QCRYPTO_TLS_CREDS_ANON(creds
);
137 if (creds
->priority
!= NULL
) {
138 prio
= g_strdup_printf("%s:+ANON-DH", creds
->priority
);
140 prio
= g_strdup(CONFIG_TLS_PRIORITY
":+ANON-DH");
143 ret
= gnutls_priority_set_direct(session
->handle
, prio
, NULL
);
145 error_setg(errp
, "Unable to set TLS session priority %s: %s",
146 prio
, gnutls_strerror(ret
));
151 if (creds
->endpoint
== QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
) {
152 ret
= gnutls_credentials_set(session
->handle
,
154 acreds
->data
.server
);
156 ret
= gnutls_credentials_set(session
->handle
,
158 acreds
->data
.client
);
161 error_setg(errp
, "Cannot set session credentials: %s",
162 gnutls_strerror(ret
));
165 } else if (object_dynamic_cast(OBJECT(creds
),
166 TYPE_QCRYPTO_TLS_CREDS_X509
)) {
167 QCryptoTLSCredsX509
*tcreds
= QCRYPTO_TLS_CREDS_X509(creds
);
168 const char *prio
= creds
->priority
;
170 prio
= CONFIG_TLS_PRIORITY
;
173 ret
= gnutls_priority_set_direct(session
->handle
, prio
, NULL
);
175 error_setg(errp
, "Cannot set default TLS session priority %s: %s",
176 prio
, gnutls_strerror(ret
));
179 ret
= gnutls_credentials_set(session
->handle
,
180 GNUTLS_CRD_CERTIFICATE
,
183 error_setg(errp
, "Cannot set session credentials: %s",
184 gnutls_strerror(ret
));
188 if (creds
->endpoint
== QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
) {
189 /* This requests, but does not enforce a client cert.
190 * The cert checking code later does enforcement */
191 gnutls_certificate_server_set_request(session
->handle
,
192 GNUTLS_CERT_REQUEST
);
195 error_setg(errp
, "Unsupported TLS credentials type %s",
196 object_get_typename(OBJECT(creds
)));
200 gnutls_transport_set_ptr(session
->handle
, session
);
201 gnutls_transport_set_push_function(session
->handle
,
202 qcrypto_tls_session_push
);
203 gnutls_transport_set_pull_function(session
->handle
,
204 qcrypto_tls_session_pull
);
209 qcrypto_tls_session_free(session
);
214 qcrypto_tls_session_check_certificate(QCryptoTLSSession
*session
,
219 const gnutls_datum_t
*certs
;
220 unsigned int nCerts
, i
;
222 gnutls_x509_crt_t cert
= NULL
;
225 if (now
== ((time_t)-1)) {
226 error_setg_errno(errp
, errno
, "Cannot get current time");
230 ret
= gnutls_certificate_verify_peers2(session
->handle
, &status
);
232 error_setg(errp
, "Verify failed: %s", gnutls_strerror(ret
));
237 const char *reason
= "Invalid certificate";
239 if (status
& GNUTLS_CERT_INVALID
) {
240 reason
= "The certificate is not trusted";
243 if (status
& GNUTLS_CERT_SIGNER_NOT_FOUND
) {
244 reason
= "The certificate hasn't got a known issuer";
247 if (status
& GNUTLS_CERT_REVOKED
) {
248 reason
= "The certificate has been revoked";
251 if (status
& GNUTLS_CERT_INSECURE_ALGORITHM
) {
252 reason
= "The certificate uses an insecure algorithm";
255 error_setg(errp
, "%s", reason
);
259 certs
= gnutls_certificate_get_peers(session
->handle
, &nCerts
);
261 error_setg(errp
, "No certificate peers");
265 for (i
= 0; i
< nCerts
; i
++) {
266 ret
= gnutls_x509_crt_init(&cert
);
268 error_setg(errp
, "Cannot initialize certificate: %s",
269 gnutls_strerror(ret
));
273 ret
= gnutls_x509_crt_import(cert
, &certs
[i
], GNUTLS_X509_FMT_DER
);
275 error_setg(errp
, "Cannot import certificate: %s",
276 gnutls_strerror(ret
));
280 if (gnutls_x509_crt_get_expiration_time(cert
) < now
) {
281 error_setg(errp
, "The certificate has expired");
285 if (gnutls_x509_crt_get_activation_time(cert
) > now
) {
286 error_setg(errp
, "The certificate is not yet activated");
290 if (gnutls_x509_crt_get_activation_time(cert
) > now
) {
291 error_setg(errp
, "The certificate is not yet activated");
296 size_t dnameSize
= 1024;
297 session
->peername
= g_malloc(dnameSize
);
299 ret
= gnutls_x509_crt_get_dn(cert
, session
->peername
, &dnameSize
);
301 if (ret
== GNUTLS_E_SHORT_MEMORY_BUFFER
) {
302 session
->peername
= g_realloc(session
->peername
,
306 error_setg(errp
, "Cannot get client distinguished name: %s",
307 gnutls_strerror(ret
));
310 if (session
->aclname
) {
311 qemu_acl
*acl
= qemu_acl_find(session
->aclname
);
314 error_setg(errp
, "Cannot find ACL %s",
319 allow
= qemu_acl_party_is_allowed(acl
, session
->peername
);
322 error_setg(errp
, "TLS x509 ACL check for %s is denied",
327 if (session
->hostname
) {
328 if (!gnutls_x509_crt_check_hostname(cert
, session
->hostname
)) {
330 "Certificate does not match the hostname %s",
337 gnutls_x509_crt_deinit(cert
);
343 gnutls_x509_crt_deinit(cert
);
349 qcrypto_tls_session_check_credentials(QCryptoTLSSession
*session
,
352 if (object_dynamic_cast(OBJECT(session
->creds
),
353 TYPE_QCRYPTO_TLS_CREDS_ANON
)) {
355 } else if (object_dynamic_cast(OBJECT(session
->creds
),
356 TYPE_QCRYPTO_TLS_CREDS_X509
)) {
357 if (session
->creds
->verifyPeer
) {
358 return qcrypto_tls_session_check_certificate(session
,
364 error_setg(errp
, "Unexpected credential type %s",
365 object_get_typename(OBJECT(session
->creds
)));
372 qcrypto_tls_session_set_callbacks(QCryptoTLSSession
*session
,
373 QCryptoTLSSessionWriteFunc writeFunc
,
374 QCryptoTLSSessionReadFunc readFunc
,
377 session
->writeFunc
= writeFunc
;
378 session
->readFunc
= readFunc
;
379 session
->opaque
= opaque
;
384 qcrypto_tls_session_write(QCryptoTLSSession
*session
,
388 ssize_t ret
= gnutls_record_send(session
->handle
, buf
, len
);
395 case GNUTLS_E_INTERRUPTED
:
410 qcrypto_tls_session_read(QCryptoTLSSession
*session
,
414 ssize_t ret
= gnutls_record_recv(session
->handle
, buf
, len
);
421 case GNUTLS_E_INTERRUPTED
:
436 qcrypto_tls_session_handshake(QCryptoTLSSession
*session
,
439 int ret
= gnutls_handshake(session
->handle
);
441 session
->handshakeComplete
= true;
443 if (ret
== GNUTLS_E_INTERRUPTED
||
444 ret
== GNUTLS_E_AGAIN
) {
447 error_setg(errp
, "TLS handshake failed: %s",
448 gnutls_strerror(ret
));
457 QCryptoTLSSessionHandshakeStatus
458 qcrypto_tls_session_get_handshake_status(QCryptoTLSSession
*session
)
460 if (session
->handshakeComplete
) {
461 return QCRYPTO_TLS_HANDSHAKE_COMPLETE
;
462 } else if (gnutls_record_get_direction(session
->handle
) == 0) {
463 return QCRYPTO_TLS_HANDSHAKE_RECVING
;
465 return QCRYPTO_TLS_HANDSHAKE_SENDING
;
471 qcrypto_tls_session_get_key_size(QCryptoTLSSession
*session
,
474 gnutls_cipher_algorithm_t cipher
;
477 cipher
= gnutls_cipher_get(session
->handle
);
478 ssf
= gnutls_cipher_get_key_size(cipher
);
480 error_setg(errp
, "Cannot get TLS cipher key size");
488 qcrypto_tls_session_get_peer_name(QCryptoTLSSession
*session
)
490 if (session
->peername
) {
491 return g_strdup(session
->peername
);
497 #else /* ! CONFIG_GNUTLS */
501 qcrypto_tls_session_new(QCryptoTLSCreds
*creds G_GNUC_UNUSED
,
502 const char *hostname G_GNUC_UNUSED
,
503 const char *aclname G_GNUC_UNUSED
,
504 QCryptoTLSCredsEndpoint endpoint G_GNUC_UNUSED
,
507 error_setg(errp
, "TLS requires GNUTLS support");
513 qcrypto_tls_session_free(QCryptoTLSSession
*sess G_GNUC_UNUSED
)
519 qcrypto_tls_session_check_credentials(QCryptoTLSSession
*sess G_GNUC_UNUSED
,
522 error_setg(errp
, "TLS requires GNUTLS support");
528 qcrypto_tls_session_set_callbacks(
529 QCryptoTLSSession
*sess G_GNUC_UNUSED
,
530 QCryptoTLSSessionWriteFunc writeFunc G_GNUC_UNUSED
,
531 QCryptoTLSSessionReadFunc readFunc G_GNUC_UNUSED
,
532 void *opaque G_GNUC_UNUSED
)
538 qcrypto_tls_session_write(QCryptoTLSSession
*sess
,
548 qcrypto_tls_session_read(QCryptoTLSSession
*sess
,
558 qcrypto_tls_session_handshake(QCryptoTLSSession
*sess
,
561 error_setg(errp
, "TLS requires GNUTLS support");
566 QCryptoTLSSessionHandshakeStatus
567 qcrypto_tls_session_get_handshake_status(QCryptoTLSSession
*sess
)
569 return QCRYPTO_TLS_HANDSHAKE_COMPLETE
;
574 qcrypto_tls_session_get_key_size(QCryptoTLSSession
*sess
,
577 error_setg(errp
, "TLS requires GNUTLS support");
583 qcrypto_tls_session_get_peer_name(QCryptoTLSSession
*sess
)