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"
31 #include <gnutls/x509.h>
34 struct QCryptoTLSSession
{
35 QCryptoTLSCreds
*creds
;
36 gnutls_session_t handle
;
39 bool handshakeComplete
;
40 QCryptoTLSSessionWriteFunc writeFunc
;
41 QCryptoTLSSessionReadFunc readFunc
;
48 qcrypto_tls_session_free(QCryptoTLSSession
*session
)
54 gnutls_deinit(session
->handle
);
55 g_free(session
->hostname
);
56 g_free(session
->peername
);
57 g_free(session
->aclname
);
58 object_unref(OBJECT(session
->creds
));
64 qcrypto_tls_session_push(void *opaque
, const void *buf
, size_t len
)
66 QCryptoTLSSession
*session
= opaque
;
68 if (!session
->writeFunc
) {
73 return session
->writeFunc(buf
, len
, session
->opaque
);
78 qcrypto_tls_session_pull(void *opaque
, void *buf
, size_t len
)
80 QCryptoTLSSession
*session
= opaque
;
82 if (!session
->readFunc
) {
87 return session
->readFunc(buf
, len
, session
->opaque
);
92 qcrypto_tls_session_new(QCryptoTLSCreds
*creds
,
95 QCryptoTLSCredsEndpoint endpoint
,
98 QCryptoTLSSession
*session
;
101 session
= g_new0(QCryptoTLSSession
, 1);
102 trace_qcrypto_tls_session_new(
103 session
, creds
, hostname
? hostname
: "<none>",
104 aclname
? aclname
: "<none>", endpoint
);
107 session
->hostname
= g_strdup(hostname
);
110 session
->aclname
= g_strdup(aclname
);
112 session
->creds
= creds
;
113 object_ref(OBJECT(creds
));
115 if (creds
->endpoint
!= endpoint
) {
116 error_setg(errp
, "Credentials endpoint doesn't match session");
120 if (endpoint
== QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
) {
121 ret
= gnutls_init(&session
->handle
, GNUTLS_SERVER
);
123 ret
= gnutls_init(&session
->handle
, GNUTLS_CLIENT
);
126 error_setg(errp
, "Cannot initialize TLS session: %s",
127 gnutls_strerror(ret
));
131 if (object_dynamic_cast(OBJECT(creds
),
132 TYPE_QCRYPTO_TLS_CREDS_ANON
)) {
133 QCryptoTLSCredsAnon
*acreds
= QCRYPTO_TLS_CREDS_ANON(creds
);
135 ret
= gnutls_priority_set_direct(session
->handle
,
136 "NORMAL:+ANON-DH", NULL
);
138 error_setg(errp
, "Unable to set TLS session priority: %s",
139 gnutls_strerror(ret
));
142 if (creds
->endpoint
== QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
) {
143 ret
= gnutls_credentials_set(session
->handle
,
145 acreds
->data
.server
);
147 ret
= gnutls_credentials_set(session
->handle
,
149 acreds
->data
.client
);
152 error_setg(errp
, "Cannot set session credentials: %s",
153 gnutls_strerror(ret
));
156 } else if (object_dynamic_cast(OBJECT(creds
),
157 TYPE_QCRYPTO_TLS_CREDS_X509
)) {
158 QCryptoTLSCredsX509
*tcreds
= QCRYPTO_TLS_CREDS_X509(creds
);
160 ret
= gnutls_set_default_priority(session
->handle
);
162 error_setg(errp
, "Cannot set default TLS session priority: %s",
163 gnutls_strerror(ret
));
166 ret
= gnutls_credentials_set(session
->handle
,
167 GNUTLS_CRD_CERTIFICATE
,
170 error_setg(errp
, "Cannot set session credentials: %s",
171 gnutls_strerror(ret
));
175 if (creds
->endpoint
== QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
) {
176 /* This requests, but does not enforce a client cert.
177 * The cert checking code later does enforcement */
178 gnutls_certificate_server_set_request(session
->handle
,
179 GNUTLS_CERT_REQUEST
);
182 error_setg(errp
, "Unsupported TLS credentials type %s",
183 object_get_typename(OBJECT(creds
)));
187 gnutls_transport_set_ptr(session
->handle
, session
);
188 gnutls_transport_set_push_function(session
->handle
,
189 qcrypto_tls_session_push
);
190 gnutls_transport_set_pull_function(session
->handle
,
191 qcrypto_tls_session_pull
);
196 qcrypto_tls_session_free(session
);
201 qcrypto_tls_session_check_certificate(QCryptoTLSSession
*session
,
206 const gnutls_datum_t
*certs
;
207 unsigned int nCerts
, i
;
209 gnutls_x509_crt_t cert
= NULL
;
212 if (now
== ((time_t)-1)) {
213 error_setg_errno(errp
, errno
, "Cannot get current time");
217 ret
= gnutls_certificate_verify_peers2(session
->handle
, &status
);
219 error_setg(errp
, "Verify failed: %s", gnutls_strerror(ret
));
224 const char *reason
= "Invalid certificate";
226 if (status
& GNUTLS_CERT_INVALID
) {
227 reason
= "The certificate is not trusted";
230 if (status
& GNUTLS_CERT_SIGNER_NOT_FOUND
) {
231 reason
= "The certificate hasn't got a known issuer";
234 if (status
& GNUTLS_CERT_REVOKED
) {
235 reason
= "The certificate has been revoked";
238 if (status
& GNUTLS_CERT_INSECURE_ALGORITHM
) {
239 reason
= "The certificate uses an insecure algorithm";
242 error_setg(errp
, "%s", reason
);
246 certs
= gnutls_certificate_get_peers(session
->handle
, &nCerts
);
248 error_setg(errp
, "No certificate peers");
252 for (i
= 0; i
< nCerts
; i
++) {
253 ret
= gnutls_x509_crt_init(&cert
);
255 error_setg(errp
, "Cannot initialize certificate: %s",
256 gnutls_strerror(ret
));
260 ret
= gnutls_x509_crt_import(cert
, &certs
[i
], GNUTLS_X509_FMT_DER
);
262 error_setg(errp
, "Cannot import certificate: %s",
263 gnutls_strerror(ret
));
267 if (gnutls_x509_crt_get_expiration_time(cert
) < now
) {
268 error_setg(errp
, "The certificate has expired");
272 if (gnutls_x509_crt_get_activation_time(cert
) > now
) {
273 error_setg(errp
, "The certificate is not yet activated");
277 if (gnutls_x509_crt_get_activation_time(cert
) > now
) {
278 error_setg(errp
, "The certificate is not yet activated");
283 size_t dnameSize
= 1024;
284 session
->peername
= g_malloc(dnameSize
);
286 ret
= gnutls_x509_crt_get_dn(cert
, session
->peername
, &dnameSize
);
288 if (ret
== GNUTLS_E_SHORT_MEMORY_BUFFER
) {
289 session
->peername
= g_realloc(session
->peername
,
293 error_setg(errp
, "Cannot get client distinguished name: %s",
294 gnutls_strerror(ret
));
297 if (session
->aclname
) {
298 qemu_acl
*acl
= qemu_acl_find(session
->aclname
);
301 error_setg(errp
, "Cannot find ACL %s",
306 allow
= qemu_acl_party_is_allowed(acl
, session
->peername
);
309 error_setg(errp
, "TLS x509 ACL check for %s is denied",
314 if (session
->hostname
) {
315 if (!gnutls_x509_crt_check_hostname(cert
, session
->hostname
)) {
317 "Certificate does not match the hostname %s",
324 gnutls_x509_crt_deinit(cert
);
330 gnutls_x509_crt_deinit(cert
);
336 qcrypto_tls_session_check_credentials(QCryptoTLSSession
*session
,
339 if (object_dynamic_cast(OBJECT(session
->creds
),
340 TYPE_QCRYPTO_TLS_CREDS_ANON
)) {
342 } else if (object_dynamic_cast(OBJECT(session
->creds
),
343 TYPE_QCRYPTO_TLS_CREDS_X509
)) {
344 if (session
->creds
->verifyPeer
) {
345 return qcrypto_tls_session_check_certificate(session
,
351 error_setg(errp
, "Unexpected credential type %s",
352 object_get_typename(OBJECT(session
->creds
)));
359 qcrypto_tls_session_set_callbacks(QCryptoTLSSession
*session
,
360 QCryptoTLSSessionWriteFunc writeFunc
,
361 QCryptoTLSSessionReadFunc readFunc
,
364 session
->writeFunc
= writeFunc
;
365 session
->readFunc
= readFunc
;
366 session
->opaque
= opaque
;
371 qcrypto_tls_session_write(QCryptoTLSSession
*session
,
375 ssize_t ret
= gnutls_record_send(session
->handle
, buf
, len
);
382 case GNUTLS_E_INTERRUPTED
:
397 qcrypto_tls_session_read(QCryptoTLSSession
*session
,
401 ssize_t ret
= gnutls_record_recv(session
->handle
, buf
, len
);
408 case GNUTLS_E_INTERRUPTED
:
423 qcrypto_tls_session_handshake(QCryptoTLSSession
*session
,
426 int ret
= gnutls_handshake(session
->handle
);
428 session
->handshakeComplete
= true;
430 if (ret
== GNUTLS_E_INTERRUPTED
||
431 ret
== GNUTLS_E_AGAIN
) {
434 error_setg(errp
, "TLS handshake failed: %s",
435 gnutls_strerror(ret
));
444 QCryptoTLSSessionHandshakeStatus
445 qcrypto_tls_session_get_handshake_status(QCryptoTLSSession
*session
)
447 if (session
->handshakeComplete
) {
448 return QCRYPTO_TLS_HANDSHAKE_COMPLETE
;
449 } else if (gnutls_record_get_direction(session
->handle
) == 0) {
450 return QCRYPTO_TLS_HANDSHAKE_RECVING
;
452 return QCRYPTO_TLS_HANDSHAKE_SENDING
;
458 qcrypto_tls_session_get_key_size(QCryptoTLSSession
*session
,
461 gnutls_cipher_algorithm_t cipher
;
464 cipher
= gnutls_cipher_get(session
->handle
);
465 ssf
= gnutls_cipher_get_key_size(cipher
);
467 error_setg(errp
, "Cannot get TLS cipher key size");
475 qcrypto_tls_session_get_peer_name(QCryptoTLSSession
*session
)
477 if (session
->peername
) {
478 return g_strdup(session
->peername
);
484 #else /* ! CONFIG_GNUTLS */
488 qcrypto_tls_session_new(QCryptoTLSCreds
*creds G_GNUC_UNUSED
,
489 const char *hostname G_GNUC_UNUSED
,
490 const char *aclname G_GNUC_UNUSED
,
491 QCryptoTLSCredsEndpoint endpoint G_GNUC_UNUSED
,
494 error_setg(errp
, "TLS requires GNUTLS support");
500 qcrypto_tls_session_free(QCryptoTLSSession
*sess G_GNUC_UNUSED
)
506 qcrypto_tls_session_check_credentials(QCryptoTLSSession
*sess G_GNUC_UNUSED
,
509 error_setg(errp
, "TLS requires GNUTLS support");
515 qcrypto_tls_session_set_callbacks(
516 QCryptoTLSSession
*sess G_GNUC_UNUSED
,
517 QCryptoTLSSessionWriteFunc writeFunc G_GNUC_UNUSED
,
518 QCryptoTLSSessionReadFunc readFunc G_GNUC_UNUSED
,
519 void *opaque G_GNUC_UNUSED
)
525 qcrypto_tls_session_write(QCryptoTLSSession
*sess
,
535 qcrypto_tls_session_read(QCryptoTLSSession
*sess
,
545 qcrypto_tls_session_handshake(QCryptoTLSSession
*sess
,
548 error_setg(errp
, "TLS requires GNUTLS support");
553 QCryptoTLSSessionHandshakeStatus
554 qcrypto_tls_session_get_handshake_status(QCryptoTLSSession
*sess
)
556 return QCRYPTO_TLS_HANDSHAKE_COMPLETE
;
561 qcrypto_tls_session_get_key_size(QCryptoTLSSession
*sess
,
564 error_setg(errp
, "TLS requires GNUTLS support");
570 qcrypto_tls_session_get_peer_name(QCryptoTLSSession
*sess
)