2 * QEMU I/O channels TLS driver
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.1 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 "qapi/error.h"
23 #include "qemu/module.h"
24 #include "io/channel-tls.h"
26 #include "qemu/atomic.h"
29 static ssize_t
qio_channel_tls_write_handler(const char *buf
,
33 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(opaque
);
36 ret
= qio_channel_write(tioc
->master
, buf
, len
, NULL
);
37 if (ret
== QIO_CHANNEL_ERR_BLOCK
) {
47 static ssize_t
qio_channel_tls_read_handler(char *buf
,
51 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(opaque
);
54 ret
= qio_channel_read(tioc
->master
, buf
, len
, NULL
);
55 if (ret
== QIO_CHANNEL_ERR_BLOCK
) {
67 qio_channel_tls_new_server(QIOChannel
*master
,
68 QCryptoTLSCreds
*creds
,
75 tioc
= QIO_CHANNEL_TLS(object_new(TYPE_QIO_CHANNEL_TLS
));
76 ioc
= QIO_CHANNEL(tioc
);
78 tioc
->master
= master
;
79 ioc
->follow_coroutine_ctx
= master
->follow_coroutine_ctx
;
80 if (qio_channel_has_feature(master
, QIO_CHANNEL_FEATURE_SHUTDOWN
)) {
81 qio_channel_set_feature(ioc
, QIO_CHANNEL_FEATURE_SHUTDOWN
);
83 object_ref(OBJECT(master
));
85 tioc
->session
= qcrypto_tls_session_new(
89 QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
,
95 qcrypto_tls_session_set_callbacks(
97 qio_channel_tls_write_handler
,
98 qio_channel_tls_read_handler
,
101 trace_qio_channel_tls_new_server(tioc
, master
, creds
, aclname
);
105 object_unref(OBJECT(tioc
));
110 qio_channel_tls_new_client(QIOChannel
*master
,
111 QCryptoTLSCreds
*creds
,
112 const char *hostname
,
118 tioc
= QIO_CHANNEL_TLS(object_new(TYPE_QIO_CHANNEL_TLS
));
119 ioc
= QIO_CHANNEL(tioc
);
121 tioc
->master
= master
;
122 ioc
->follow_coroutine_ctx
= master
->follow_coroutine_ctx
;
123 if (qio_channel_has_feature(master
, QIO_CHANNEL_FEATURE_SHUTDOWN
)) {
124 qio_channel_set_feature(ioc
, QIO_CHANNEL_FEATURE_SHUTDOWN
);
126 object_ref(OBJECT(master
));
128 tioc
->session
= qcrypto_tls_session_new(
132 QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
,
134 if (!tioc
->session
) {
138 qcrypto_tls_session_set_callbacks(
140 qio_channel_tls_write_handler
,
141 qio_channel_tls_read_handler
,
144 trace_qio_channel_tls_new_client(tioc
, master
, creds
, hostname
);
148 object_unref(OBJECT(tioc
));
152 struct QIOChannelTLSData
{
154 GMainContext
*context
;
156 typedef struct QIOChannelTLSData QIOChannelTLSData
;
158 static gboolean
qio_channel_tls_handshake_io(QIOChannel
*ioc
,
159 GIOCondition condition
,
162 static void qio_channel_tls_handshake_task(QIOChannelTLS
*ioc
,
164 GMainContext
*context
)
167 QCryptoTLSSessionHandshakeStatus status
;
169 if (qcrypto_tls_session_handshake(ioc
->session
, &err
) < 0) {
170 trace_qio_channel_tls_handshake_fail(ioc
);
171 qio_task_set_error(task
, err
);
172 qio_task_complete(task
);
176 status
= qcrypto_tls_session_get_handshake_status(ioc
->session
);
177 if (status
== QCRYPTO_TLS_HANDSHAKE_COMPLETE
) {
178 trace_qio_channel_tls_handshake_complete(ioc
);
179 if (qcrypto_tls_session_check_credentials(ioc
->session
,
181 trace_qio_channel_tls_credentials_deny(ioc
);
182 qio_task_set_error(task
, err
);
184 trace_qio_channel_tls_credentials_allow(ioc
);
186 qio_task_complete(task
);
188 GIOCondition condition
;
189 QIOChannelTLSData
*data
= g_new0(typeof(*data
), 1);
192 data
->context
= context
;
195 g_main_context_ref(context
);
198 if (status
== QCRYPTO_TLS_HANDSHAKE_SENDING
) {
199 condition
= G_IO_OUT
;
204 trace_qio_channel_tls_handshake_pending(ioc
, status
);
206 qio_channel_add_watch_full(ioc
->master
,
208 qio_channel_tls_handshake_io
,
216 static gboolean
qio_channel_tls_handshake_io(QIOChannel
*ioc
,
217 GIOCondition condition
,
220 QIOChannelTLSData
*data
= user_data
;
221 QIOTask
*task
= data
->task
;
222 GMainContext
*context
= data
->context
;
223 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(
224 qio_task_get_source(task
));
226 tioc
->hs_ioc_tag
= 0;
228 qio_channel_tls_handshake_task(tioc
, task
, context
);
231 g_main_context_unref(context
);
237 void qio_channel_tls_handshake(QIOChannelTLS
*ioc
,
240 GDestroyNotify destroy
,
241 GMainContext
*context
)
245 task
= qio_task_new(OBJECT(ioc
),
246 func
, opaque
, destroy
);
248 trace_qio_channel_tls_handshake_start(ioc
);
249 qio_channel_tls_handshake_task(ioc
, task
, context
);
253 static void qio_channel_tls_init(Object
*obj G_GNUC_UNUSED
)
258 static void qio_channel_tls_finalize(Object
*obj
)
260 QIOChannelTLS
*ioc
= QIO_CHANNEL_TLS(obj
);
262 object_unref(OBJECT(ioc
->master
));
263 qcrypto_tls_session_free(ioc
->session
);
267 static ssize_t
qio_channel_tls_readv(QIOChannel
*ioc
,
268 const struct iovec
*iov
,
275 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(ioc
);
279 for (i
= 0 ; i
< niov
; i
++) {
280 ssize_t ret
= qcrypto_tls_session_read(tioc
->session
,
284 if (errno
== EAGAIN
) {
288 return QIO_CHANNEL_ERR_BLOCK
;
290 } else if (errno
== ECONNABORTED
&&
291 (qatomic_load_acquire(&tioc
->shutdown
) &
292 QIO_CHANNEL_SHUTDOWN_READ
)) {
296 error_setg_errno(errp
, errno
,
297 "Cannot read from TLS channel");
301 if (ret
< iov
[i
].iov_len
) {
309 static ssize_t
qio_channel_tls_writev(QIOChannel
*ioc
,
310 const struct iovec
*iov
,
317 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(ioc
);
321 for (i
= 0 ; i
< niov
; i
++) {
322 ssize_t ret
= qcrypto_tls_session_write(tioc
->session
,
326 if (errno
== EAGAIN
) {
330 return QIO_CHANNEL_ERR_BLOCK
;
334 error_setg_errno(errp
, errno
,
335 "Cannot write to TLS channel");
339 if (ret
< iov
[i
].iov_len
) {
346 static int qio_channel_tls_set_blocking(QIOChannel
*ioc
,
350 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(ioc
);
352 return qio_channel_set_blocking(tioc
->master
, enabled
, errp
);
355 static void qio_channel_tls_set_delay(QIOChannel
*ioc
,
358 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(ioc
);
360 qio_channel_set_delay(tioc
->master
, enabled
);
363 static void qio_channel_tls_set_cork(QIOChannel
*ioc
,
366 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(ioc
);
368 qio_channel_set_cork(tioc
->master
, enabled
);
371 static int qio_channel_tls_shutdown(QIOChannel
*ioc
,
372 QIOChannelShutdown how
,
375 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(ioc
);
377 qatomic_or(&tioc
->shutdown
, how
);
379 return qio_channel_shutdown(tioc
->master
, how
, errp
);
382 static int qio_channel_tls_close(QIOChannel
*ioc
,
385 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(ioc
);
387 if (tioc
->hs_ioc_tag
) {
388 trace_qio_channel_tls_handshake_cancel(ioc
);
389 g_clear_handle_id(&tioc
->hs_ioc_tag
, g_source_remove
);
392 return qio_channel_close(tioc
->master
, errp
);
395 static void qio_channel_tls_set_aio_fd_handler(QIOChannel
*ioc
,
396 AioContext
*read_ctx
,
398 AioContext
*write_ctx
,
402 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(ioc
);
404 qio_channel_set_aio_fd_handler(tioc
->master
, read_ctx
, io_read
,
405 write_ctx
, io_write
, opaque
);
408 typedef struct QIOChannelTLSSource QIOChannelTLSSource
;
409 struct QIOChannelTLSSource
{
415 qio_channel_tls_source_check(GSource
*source
)
417 QIOChannelTLSSource
*tsource
= (QIOChannelTLSSource
*)source
;
419 return qcrypto_tls_session_check_pending(tsource
->tioc
->session
) > 0;
423 qio_channel_tls_source_prepare(GSource
*source
, gint
*timeout
)
426 return qio_channel_tls_source_check(source
);
430 qio_channel_tls_source_dispatch(GSource
*source
, GSourceFunc callback
,
433 return G_SOURCE_CONTINUE
;
437 qio_channel_tls_source_finalize(GSource
*source
)
439 QIOChannelTLSSource
*tsource
= (QIOChannelTLSSource
*)source
;
441 object_unref(OBJECT(tsource
->tioc
));
444 static GSourceFuncs qio_channel_tls_source_funcs
= {
445 qio_channel_tls_source_prepare
,
446 qio_channel_tls_source_check
,
447 qio_channel_tls_source_dispatch
,
448 qio_channel_tls_source_finalize
452 qio_channel_tls_read_watch(QIOChannelTLS
*tioc
, GSource
*source
)
455 QIOChannelTLSSource
*tlssource
;
457 child
= g_source_new(&qio_channel_tls_source_funcs
,
458 sizeof(QIOChannelTLSSource
));
459 tlssource
= (QIOChannelTLSSource
*)child
;
461 tlssource
->tioc
= tioc
;
462 object_ref(OBJECT(tioc
));
464 g_source_add_child_source(source
, child
);
465 g_source_unref(child
);
468 static GSource
*qio_channel_tls_create_watch(QIOChannel
*ioc
,
469 GIOCondition condition
)
471 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(ioc
);
472 GSource
*source
= qio_channel_create_watch(tioc
->master
, condition
);
474 if (condition
& G_IO_IN
) {
475 qio_channel_tls_read_watch(tioc
, source
);
482 qio_channel_tls_get_session(QIOChannelTLS
*ioc
)
487 static void qio_channel_tls_class_init(ObjectClass
*klass
,
488 void *class_data G_GNUC_UNUSED
)
490 QIOChannelClass
*ioc_klass
= QIO_CHANNEL_CLASS(klass
);
492 ioc_klass
->io_writev
= qio_channel_tls_writev
;
493 ioc_klass
->io_readv
= qio_channel_tls_readv
;
494 ioc_klass
->io_set_blocking
= qio_channel_tls_set_blocking
;
495 ioc_klass
->io_set_delay
= qio_channel_tls_set_delay
;
496 ioc_klass
->io_set_cork
= qio_channel_tls_set_cork
;
497 ioc_klass
->io_close
= qio_channel_tls_close
;
498 ioc_klass
->io_shutdown
= qio_channel_tls_shutdown
;
499 ioc_klass
->io_create_watch
= qio_channel_tls_create_watch
;
500 ioc_klass
->io_set_aio_fd_handler
= qio_channel_tls_set_aio_fd_handler
;
503 static const TypeInfo qio_channel_tls_info
= {
504 .parent
= TYPE_QIO_CHANNEL
,
505 .name
= TYPE_QIO_CHANNEL_TLS
,
506 .instance_size
= sizeof(QIOChannelTLS
),
507 .instance_init
= qio_channel_tls_init
,
508 .instance_finalize
= qio_channel_tls_finalize
,
509 .class_init
= qio_channel_tls_class_init
,
512 static void qio_channel_tls_register_types(void)
514 type_register_static(&qio_channel_tls_info
);
517 type_init(qio_channel_tls_register_types
);