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
)) {
354 trace_qcrypto_tls_session_check_creds(session
, "nop");
356 } else if (object_dynamic_cast(OBJECT(session
->creds
),
357 TYPE_QCRYPTO_TLS_CREDS_X509
)) {
358 if (session
->creds
->verifyPeer
) {
359 int ret
= qcrypto_tls_session_check_certificate(session
,
361 trace_qcrypto_tls_session_check_creds(session
,
362 ret
== 0 ? "pass" : "fail");
365 trace_qcrypto_tls_session_check_creds(session
, "skip");
369 trace_qcrypto_tls_session_check_creds(session
, "error");
370 error_setg(errp
, "Unexpected credential type %s",
371 object_get_typename(OBJECT(session
->creds
)));
378 qcrypto_tls_session_set_callbacks(QCryptoTLSSession
*session
,
379 QCryptoTLSSessionWriteFunc writeFunc
,
380 QCryptoTLSSessionReadFunc readFunc
,
383 session
->writeFunc
= writeFunc
;
384 session
->readFunc
= readFunc
;
385 session
->opaque
= opaque
;
390 qcrypto_tls_session_write(QCryptoTLSSession
*session
,
394 ssize_t ret
= gnutls_record_send(session
->handle
, buf
, len
);
401 case GNUTLS_E_INTERRUPTED
:
416 qcrypto_tls_session_read(QCryptoTLSSession
*session
,
420 ssize_t ret
= gnutls_record_recv(session
->handle
, buf
, len
);
427 case GNUTLS_E_INTERRUPTED
:
442 qcrypto_tls_session_handshake(QCryptoTLSSession
*session
,
445 int ret
= gnutls_handshake(session
->handle
);
447 session
->handshakeComplete
= true;
449 if (ret
== GNUTLS_E_INTERRUPTED
||
450 ret
== GNUTLS_E_AGAIN
) {
453 error_setg(errp
, "TLS handshake failed: %s",
454 gnutls_strerror(ret
));
463 QCryptoTLSSessionHandshakeStatus
464 qcrypto_tls_session_get_handshake_status(QCryptoTLSSession
*session
)
466 if (session
->handshakeComplete
) {
467 return QCRYPTO_TLS_HANDSHAKE_COMPLETE
;
468 } else if (gnutls_record_get_direction(session
->handle
) == 0) {
469 return QCRYPTO_TLS_HANDSHAKE_RECVING
;
471 return QCRYPTO_TLS_HANDSHAKE_SENDING
;
477 qcrypto_tls_session_get_key_size(QCryptoTLSSession
*session
,
480 gnutls_cipher_algorithm_t cipher
;
483 cipher
= gnutls_cipher_get(session
->handle
);
484 ssf
= gnutls_cipher_get_key_size(cipher
);
486 error_setg(errp
, "Cannot get TLS cipher key size");
494 qcrypto_tls_session_get_peer_name(QCryptoTLSSession
*session
)
496 if (session
->peername
) {
497 return g_strdup(session
->peername
);
503 #else /* ! CONFIG_GNUTLS */
507 qcrypto_tls_session_new(QCryptoTLSCreds
*creds G_GNUC_UNUSED
,
508 const char *hostname G_GNUC_UNUSED
,
509 const char *aclname G_GNUC_UNUSED
,
510 QCryptoTLSCredsEndpoint endpoint G_GNUC_UNUSED
,
513 error_setg(errp
, "TLS requires GNUTLS support");
519 qcrypto_tls_session_free(QCryptoTLSSession
*sess G_GNUC_UNUSED
)
525 qcrypto_tls_session_check_credentials(QCryptoTLSSession
*sess G_GNUC_UNUSED
,
528 error_setg(errp
, "TLS requires GNUTLS support");
534 qcrypto_tls_session_set_callbacks(
535 QCryptoTLSSession
*sess G_GNUC_UNUSED
,
536 QCryptoTLSSessionWriteFunc writeFunc G_GNUC_UNUSED
,
537 QCryptoTLSSessionReadFunc readFunc G_GNUC_UNUSED
,
538 void *opaque G_GNUC_UNUSED
)
544 qcrypto_tls_session_write(QCryptoTLSSession
*sess
,
554 qcrypto_tls_session_read(QCryptoTLSSession
*sess
,
564 qcrypto_tls_session_handshake(QCryptoTLSSession
*sess
,
567 error_setg(errp
, "TLS requires GNUTLS support");
572 QCryptoTLSSessionHandshakeStatus
573 qcrypto_tls_session_get_handshake_status(QCryptoTLSSession
*sess
)
575 return QCRYPTO_TLS_HANDSHAKE_COMPLETE
;
580 qcrypto_tls_session_get_key_size(QCryptoTLSSession
*sess
,
583 error_setg(errp
, "TLS requires GNUTLS support");
589 qcrypto_tls_session_get_peer_name(QCryptoTLSSession
*sess
)