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
,
34 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(opaque
);
37 ret
= qio_channel_write(tioc
->master
, buf
, len
, errp
);
38 if (ret
== QIO_CHANNEL_ERR_BLOCK
) {
39 return QCRYPTO_TLS_SESSION_ERR_BLOCK
;
46 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
, errp
);
55 if (ret
== QIO_CHANNEL_ERR_BLOCK
) {
56 return QCRYPTO_TLS_SESSION_ERR_BLOCK
;
65 qio_channel_tls_new_server(QIOChannel
*master
,
66 QCryptoTLSCreds
*creds
,
73 tioc
= QIO_CHANNEL_TLS(object_new(TYPE_QIO_CHANNEL_TLS
));
74 ioc
= QIO_CHANNEL(tioc
);
76 tioc
->master
= master
;
77 ioc
->follow_coroutine_ctx
= master
->follow_coroutine_ctx
;
78 if (qio_channel_has_feature(master
, QIO_CHANNEL_FEATURE_SHUTDOWN
)) {
79 qio_channel_set_feature(ioc
, QIO_CHANNEL_FEATURE_SHUTDOWN
);
81 object_ref(OBJECT(master
));
83 tioc
->session
= qcrypto_tls_session_new(
87 QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
,
93 qcrypto_tls_session_set_callbacks(
95 qio_channel_tls_write_handler
,
96 qio_channel_tls_read_handler
,
99 trace_qio_channel_tls_new_server(tioc
, master
, creds
, aclname
);
103 object_unref(OBJECT(tioc
));
108 qio_channel_tls_new_client(QIOChannel
*master
,
109 QCryptoTLSCreds
*creds
,
110 const char *hostname
,
116 tioc
= QIO_CHANNEL_TLS(object_new(TYPE_QIO_CHANNEL_TLS
));
117 ioc
= QIO_CHANNEL(tioc
);
119 tioc
->master
= master
;
120 ioc
->follow_coroutine_ctx
= master
->follow_coroutine_ctx
;
121 if (qio_channel_has_feature(master
, QIO_CHANNEL_FEATURE_SHUTDOWN
)) {
122 qio_channel_set_feature(ioc
, QIO_CHANNEL_FEATURE_SHUTDOWN
);
124 object_ref(OBJECT(master
));
126 tioc
->session
= qcrypto_tls_session_new(
130 QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
,
132 if (!tioc
->session
) {
136 qcrypto_tls_session_set_callbacks(
138 qio_channel_tls_write_handler
,
139 qio_channel_tls_read_handler
,
142 trace_qio_channel_tls_new_client(tioc
, master
, creds
, hostname
);
146 object_unref(OBJECT(tioc
));
150 struct QIOChannelTLSData
{
152 GMainContext
*context
;
154 typedef struct QIOChannelTLSData QIOChannelTLSData
;
156 static gboolean
qio_channel_tls_handshake_io(QIOChannel
*ioc
,
157 GIOCondition condition
,
160 static void qio_channel_tls_handshake_task(QIOChannelTLS
*ioc
,
162 GMainContext
*context
)
165 QCryptoTLSSessionHandshakeStatus status
;
167 if (qcrypto_tls_session_handshake(ioc
->session
, &err
) < 0) {
168 trace_qio_channel_tls_handshake_fail(ioc
);
169 qio_task_set_error(task
, err
);
170 qio_task_complete(task
);
174 status
= qcrypto_tls_session_get_handshake_status(ioc
->session
);
175 if (status
== QCRYPTO_TLS_HANDSHAKE_COMPLETE
) {
176 trace_qio_channel_tls_handshake_complete(ioc
);
177 if (qcrypto_tls_session_check_credentials(ioc
->session
,
179 trace_qio_channel_tls_credentials_deny(ioc
);
180 qio_task_set_error(task
, err
);
182 trace_qio_channel_tls_credentials_allow(ioc
);
184 qio_task_complete(task
);
186 GIOCondition condition
;
187 QIOChannelTLSData
*data
= g_new0(typeof(*data
), 1);
190 data
->context
= context
;
193 g_main_context_ref(context
);
196 if (status
== QCRYPTO_TLS_HANDSHAKE_SENDING
) {
197 condition
= G_IO_OUT
;
202 trace_qio_channel_tls_handshake_pending(ioc
, status
);
204 qio_channel_add_watch_full(ioc
->master
,
206 qio_channel_tls_handshake_io
,
214 static gboolean
qio_channel_tls_handshake_io(QIOChannel
*ioc
,
215 GIOCondition condition
,
218 QIOChannelTLSData
*data
= user_data
;
219 QIOTask
*task
= data
->task
;
220 GMainContext
*context
= data
->context
;
221 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(
222 qio_task_get_source(task
));
224 tioc
->hs_ioc_tag
= 0;
226 qio_channel_tls_handshake_task(tioc
, task
, context
);
229 g_main_context_unref(context
);
235 void qio_channel_tls_handshake(QIOChannelTLS
*ioc
,
238 GDestroyNotify destroy
,
239 GMainContext
*context
)
243 task
= qio_task_new(OBJECT(ioc
),
244 func
, opaque
, destroy
);
246 trace_qio_channel_tls_handshake_start(ioc
);
247 qio_channel_tls_handshake_task(ioc
, task
, context
);
251 static void qio_channel_tls_init(Object
*obj G_GNUC_UNUSED
)
256 static void qio_channel_tls_finalize(Object
*obj
)
258 QIOChannelTLS
*ioc
= QIO_CHANNEL_TLS(obj
);
260 object_unref(OBJECT(ioc
->master
));
261 qcrypto_tls_session_free(ioc
->session
);
265 static ssize_t
qio_channel_tls_readv(QIOChannel
*ioc
,
266 const struct iovec
*iov
,
273 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(ioc
);
277 for (i
= 0 ; i
< niov
; i
++) {
278 ssize_t ret
= qcrypto_tls_session_read(
282 qatomic_load_acquire(&tioc
->shutdown
) & QIO_CHANNEL_SHUTDOWN_READ
,
284 if (ret
== QCRYPTO_TLS_SESSION_ERR_BLOCK
) {
288 return QIO_CHANNEL_ERR_BLOCK
;
290 } else if (ret
< 0) {
294 if (ret
< iov
[i
].iov_len
) {
302 static ssize_t
qio_channel_tls_writev(QIOChannel
*ioc
,
303 const struct iovec
*iov
,
310 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(ioc
);
314 for (i
= 0 ; i
< niov
; i
++) {
315 ssize_t ret
= qcrypto_tls_session_write(tioc
->session
,
319 if (ret
== QCRYPTO_TLS_SESSION_ERR_BLOCK
) {
323 return QIO_CHANNEL_ERR_BLOCK
;
325 } else if (ret
< 0) {
329 if (ret
< iov
[i
].iov_len
) {
336 static int qio_channel_tls_set_blocking(QIOChannel
*ioc
,
340 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(ioc
);
342 return qio_channel_set_blocking(tioc
->master
, enabled
, errp
);
345 static void qio_channel_tls_set_delay(QIOChannel
*ioc
,
348 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(ioc
);
350 qio_channel_set_delay(tioc
->master
, enabled
);
353 static void qio_channel_tls_set_cork(QIOChannel
*ioc
,
356 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(ioc
);
358 qio_channel_set_cork(tioc
->master
, enabled
);
361 static int qio_channel_tls_shutdown(QIOChannel
*ioc
,
362 QIOChannelShutdown how
,
365 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(ioc
);
367 qatomic_or(&tioc
->shutdown
, how
);
369 return qio_channel_shutdown(tioc
->master
, how
, errp
);
372 static int qio_channel_tls_close(QIOChannel
*ioc
,
375 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(ioc
);
377 if (tioc
->hs_ioc_tag
) {
378 trace_qio_channel_tls_handshake_cancel(ioc
);
379 g_clear_handle_id(&tioc
->hs_ioc_tag
, g_source_remove
);
382 return qio_channel_close(tioc
->master
, errp
);
385 static void qio_channel_tls_set_aio_fd_handler(QIOChannel
*ioc
,
386 AioContext
*read_ctx
,
388 AioContext
*write_ctx
,
392 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(ioc
);
394 qio_channel_set_aio_fd_handler(tioc
->master
, read_ctx
, io_read
,
395 write_ctx
, io_write
, opaque
);
398 typedef struct QIOChannelTLSSource QIOChannelTLSSource
;
399 struct QIOChannelTLSSource
{
405 qio_channel_tls_source_check(GSource
*source
)
407 QIOChannelTLSSource
*tsource
= (QIOChannelTLSSource
*)source
;
409 return qcrypto_tls_session_check_pending(tsource
->tioc
->session
) > 0;
413 qio_channel_tls_source_prepare(GSource
*source
, gint
*timeout
)
416 return qio_channel_tls_source_check(source
);
420 qio_channel_tls_source_dispatch(GSource
*source
, GSourceFunc callback
,
423 return G_SOURCE_CONTINUE
;
427 qio_channel_tls_source_finalize(GSource
*source
)
429 QIOChannelTLSSource
*tsource
= (QIOChannelTLSSource
*)source
;
431 object_unref(OBJECT(tsource
->tioc
));
434 static GSourceFuncs qio_channel_tls_source_funcs
= {
435 qio_channel_tls_source_prepare
,
436 qio_channel_tls_source_check
,
437 qio_channel_tls_source_dispatch
,
438 qio_channel_tls_source_finalize
442 qio_channel_tls_read_watch(QIOChannelTLS
*tioc
, GSource
*source
)
445 QIOChannelTLSSource
*tlssource
;
447 child
= g_source_new(&qio_channel_tls_source_funcs
,
448 sizeof(QIOChannelTLSSource
));
449 tlssource
= (QIOChannelTLSSource
*)child
;
451 tlssource
->tioc
= tioc
;
452 object_ref(OBJECT(tioc
));
454 g_source_add_child_source(source
, child
);
455 g_source_unref(child
);
458 static GSource
*qio_channel_tls_create_watch(QIOChannel
*ioc
,
459 GIOCondition condition
)
461 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(ioc
);
462 GSource
*source
= qio_channel_create_watch(tioc
->master
, condition
);
464 if (condition
& G_IO_IN
) {
465 qio_channel_tls_read_watch(tioc
, source
);
472 qio_channel_tls_get_session(QIOChannelTLS
*ioc
)
477 static void qio_channel_tls_class_init(ObjectClass
*klass
,
478 void *class_data G_GNUC_UNUSED
)
480 QIOChannelClass
*ioc_klass
= QIO_CHANNEL_CLASS(klass
);
482 ioc_klass
->io_writev
= qio_channel_tls_writev
;
483 ioc_klass
->io_readv
= qio_channel_tls_readv
;
484 ioc_klass
->io_set_blocking
= qio_channel_tls_set_blocking
;
485 ioc_klass
->io_set_delay
= qio_channel_tls_set_delay
;
486 ioc_klass
->io_set_cork
= qio_channel_tls_set_cork
;
487 ioc_klass
->io_close
= qio_channel_tls_close
;
488 ioc_klass
->io_shutdown
= qio_channel_tls_shutdown
;
489 ioc_klass
->io_create_watch
= qio_channel_tls_create_watch
;
490 ioc_klass
->io_set_aio_fd_handler
= qio_channel_tls_set_aio_fd_handler
;
493 static const TypeInfo qio_channel_tls_info
= {
494 .parent
= TYPE_QIO_CHANNEL
,
495 .name
= TYPE_QIO_CHANNEL_TLS
,
496 .instance_size
= sizeof(QIOChannelTLS
),
497 .instance_init
= qio_channel_tls_init
,
498 .instance_finalize
= qio_channel_tls_finalize
,
499 .class_init
= qio_channel_tls_class_init
,
502 static void qio_channel_tls_register_types(void)
504 type_register_static(&qio_channel_tls_info
);
507 type_init(qio_channel_tls_register_types
);