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
);
136 ret
= gnutls_priority_set_direct(session
->handle
,
137 "NORMAL:+ANON-DH", NULL
);
139 error_setg(errp
, "Unable to set TLS session priority: %s",
140 gnutls_strerror(ret
));
143 if (creds
->endpoint
== QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
) {
144 ret
= gnutls_credentials_set(session
->handle
,
146 acreds
->data
.server
);
148 ret
= gnutls_credentials_set(session
->handle
,
150 acreds
->data
.client
);
153 error_setg(errp
, "Cannot set session credentials: %s",
154 gnutls_strerror(ret
));
157 } else if (object_dynamic_cast(OBJECT(creds
),
158 TYPE_QCRYPTO_TLS_CREDS_X509
)) {
159 QCryptoTLSCredsX509
*tcreds
= QCRYPTO_TLS_CREDS_X509(creds
);
161 ret
= gnutls_set_default_priority(session
->handle
);
163 error_setg(errp
, "Cannot set default TLS session priority: %s",
164 gnutls_strerror(ret
));
167 ret
= gnutls_credentials_set(session
->handle
,
168 GNUTLS_CRD_CERTIFICATE
,
171 error_setg(errp
, "Cannot set session credentials: %s",
172 gnutls_strerror(ret
));
176 if (creds
->endpoint
== QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
) {
177 /* This requests, but does not enforce a client cert.
178 * The cert checking code later does enforcement */
179 gnutls_certificate_server_set_request(session
->handle
,
180 GNUTLS_CERT_REQUEST
);
183 error_setg(errp
, "Unsupported TLS credentials type %s",
184 object_get_typename(OBJECT(creds
)));
188 gnutls_transport_set_ptr(session
->handle
, session
);
189 gnutls_transport_set_push_function(session
->handle
,
190 qcrypto_tls_session_push
);
191 gnutls_transport_set_pull_function(session
->handle
,
192 qcrypto_tls_session_pull
);
197 qcrypto_tls_session_free(session
);
202 qcrypto_tls_session_check_certificate(QCryptoTLSSession
*session
,
207 const gnutls_datum_t
*certs
;
208 unsigned int nCerts
, i
;
210 gnutls_x509_crt_t cert
= NULL
;
213 if (now
== ((time_t)-1)) {
214 error_setg_errno(errp
, errno
, "Cannot get current time");
218 ret
= gnutls_certificate_verify_peers2(session
->handle
, &status
);
220 error_setg(errp
, "Verify failed: %s", gnutls_strerror(ret
));
225 const char *reason
= "Invalid certificate";
227 if (status
& GNUTLS_CERT_INVALID
) {
228 reason
= "The certificate is not trusted";
231 if (status
& GNUTLS_CERT_SIGNER_NOT_FOUND
) {
232 reason
= "The certificate hasn't got a known issuer";
235 if (status
& GNUTLS_CERT_REVOKED
) {
236 reason
= "The certificate has been revoked";
239 if (status
& GNUTLS_CERT_INSECURE_ALGORITHM
) {
240 reason
= "The certificate uses an insecure algorithm";
243 error_setg(errp
, "%s", reason
);
247 certs
= gnutls_certificate_get_peers(session
->handle
, &nCerts
);
249 error_setg(errp
, "No certificate peers");
253 for (i
= 0; i
< nCerts
; i
++) {
254 ret
= gnutls_x509_crt_init(&cert
);
256 error_setg(errp
, "Cannot initialize certificate: %s",
257 gnutls_strerror(ret
));
261 ret
= gnutls_x509_crt_import(cert
, &certs
[i
], GNUTLS_X509_FMT_DER
);
263 error_setg(errp
, "Cannot import certificate: %s",
264 gnutls_strerror(ret
));
268 if (gnutls_x509_crt_get_expiration_time(cert
) < now
) {
269 error_setg(errp
, "The certificate has expired");
273 if (gnutls_x509_crt_get_activation_time(cert
) > now
) {
274 error_setg(errp
, "The certificate is not yet activated");
278 if (gnutls_x509_crt_get_activation_time(cert
) > now
) {
279 error_setg(errp
, "The certificate is not yet activated");
284 size_t dnameSize
= 1024;
285 session
->peername
= g_malloc(dnameSize
);
287 ret
= gnutls_x509_crt_get_dn(cert
, session
->peername
, &dnameSize
);
289 if (ret
== GNUTLS_E_SHORT_MEMORY_BUFFER
) {
290 session
->peername
= g_realloc(session
->peername
,
294 error_setg(errp
, "Cannot get client distinguished name: %s",
295 gnutls_strerror(ret
));
298 if (session
->aclname
) {
299 qemu_acl
*acl
= qemu_acl_find(session
->aclname
);
302 error_setg(errp
, "Cannot find ACL %s",
307 allow
= qemu_acl_party_is_allowed(acl
, session
->peername
);
310 error_setg(errp
, "TLS x509 ACL check for %s is denied",
315 if (session
->hostname
) {
316 if (!gnutls_x509_crt_check_hostname(cert
, session
->hostname
)) {
318 "Certificate does not match the hostname %s",
325 gnutls_x509_crt_deinit(cert
);
331 gnutls_x509_crt_deinit(cert
);
337 qcrypto_tls_session_check_credentials(QCryptoTLSSession
*session
,
340 if (object_dynamic_cast(OBJECT(session
->creds
),
341 TYPE_QCRYPTO_TLS_CREDS_ANON
)) {
343 } else if (object_dynamic_cast(OBJECT(session
->creds
),
344 TYPE_QCRYPTO_TLS_CREDS_X509
)) {
345 if (session
->creds
->verifyPeer
) {
346 return qcrypto_tls_session_check_certificate(session
,
352 error_setg(errp
, "Unexpected credential type %s",
353 object_get_typename(OBJECT(session
->creds
)));
360 qcrypto_tls_session_set_callbacks(QCryptoTLSSession
*session
,
361 QCryptoTLSSessionWriteFunc writeFunc
,
362 QCryptoTLSSessionReadFunc readFunc
,
365 session
->writeFunc
= writeFunc
;
366 session
->readFunc
= readFunc
;
367 session
->opaque
= opaque
;
372 qcrypto_tls_session_write(QCryptoTLSSession
*session
,
376 ssize_t ret
= gnutls_record_send(session
->handle
, buf
, len
);
383 case GNUTLS_E_INTERRUPTED
:
398 qcrypto_tls_session_read(QCryptoTLSSession
*session
,
402 ssize_t ret
= gnutls_record_recv(session
->handle
, buf
, len
);
409 case GNUTLS_E_INTERRUPTED
:
424 qcrypto_tls_session_handshake(QCryptoTLSSession
*session
,
427 int ret
= gnutls_handshake(session
->handle
);
429 session
->handshakeComplete
= true;
431 if (ret
== GNUTLS_E_INTERRUPTED
||
432 ret
== GNUTLS_E_AGAIN
) {
435 error_setg(errp
, "TLS handshake failed: %s",
436 gnutls_strerror(ret
));
445 QCryptoTLSSessionHandshakeStatus
446 qcrypto_tls_session_get_handshake_status(QCryptoTLSSession
*session
)
448 if (session
->handshakeComplete
) {
449 return QCRYPTO_TLS_HANDSHAKE_COMPLETE
;
450 } else if (gnutls_record_get_direction(session
->handle
) == 0) {
451 return QCRYPTO_TLS_HANDSHAKE_RECVING
;
453 return QCRYPTO_TLS_HANDSHAKE_SENDING
;
459 qcrypto_tls_session_get_key_size(QCryptoTLSSession
*session
,
462 gnutls_cipher_algorithm_t cipher
;
465 cipher
= gnutls_cipher_get(session
->handle
);
466 ssf
= gnutls_cipher_get_key_size(cipher
);
468 error_setg(errp
, "Cannot get TLS cipher key size");
476 qcrypto_tls_session_get_peer_name(QCryptoTLSSession
*session
)
478 if (session
->peername
) {
479 return g_strdup(session
->peername
);
485 #else /* ! CONFIG_GNUTLS */
489 qcrypto_tls_session_new(QCryptoTLSCreds
*creds G_GNUC_UNUSED
,
490 const char *hostname G_GNUC_UNUSED
,
491 const char *aclname G_GNUC_UNUSED
,
492 QCryptoTLSCredsEndpoint endpoint G_GNUC_UNUSED
,
495 error_setg(errp
, "TLS requires GNUTLS support");
501 qcrypto_tls_session_free(QCryptoTLSSession
*sess G_GNUC_UNUSED
)
507 qcrypto_tls_session_check_credentials(QCryptoTLSSession
*sess G_GNUC_UNUSED
,
510 error_setg(errp
, "TLS requires GNUTLS support");
516 qcrypto_tls_session_set_callbacks(
517 QCryptoTLSSession
*sess G_GNUC_UNUSED
,
518 QCryptoTLSSessionWriteFunc writeFunc G_GNUC_UNUSED
,
519 QCryptoTLSSessionReadFunc readFunc G_GNUC_UNUSED
,
520 void *opaque G_GNUC_UNUSED
)
526 qcrypto_tls_session_write(QCryptoTLSSession
*sess
,
536 qcrypto_tls_session_read(QCryptoTLSSession
*sess
,
546 qcrypto_tls_session_handshake(QCryptoTLSSession
*sess
,
549 error_setg(errp
, "TLS requires GNUTLS support");
554 QCryptoTLSSessionHandshakeStatus
555 qcrypto_tls_session_get_handshake_status(QCryptoTLSSession
*sess
)
557 return QCRYPTO_TLS_HANDSHAKE_COMPLETE
;
562 qcrypto_tls_session_get_key_size(QCryptoTLSSession
*sess
,
565 error_setg(errp
, "TLS requires GNUTLS support");
571 qcrypto_tls_session_get_peer_name(QCryptoTLSSession
*sess
)