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
,
74 ioc
= QIO_CHANNEL_TLS(object_new(TYPE_QIO_CHANNEL_TLS
));
77 object_ref(OBJECT(master
));
79 ioc
->session
= qcrypto_tls_session_new(
83 QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
,
89 qcrypto_tls_session_set_callbacks(
91 qio_channel_tls_write_handler
,
92 qio_channel_tls_read_handler
,
95 trace_qio_channel_tls_new_server(ioc
, master
, creds
, aclname
);
99 object_unref(OBJECT(ioc
));
104 qio_channel_tls_new_client(QIOChannel
*master
,
105 QCryptoTLSCreds
*creds
,
106 const char *hostname
,
112 tioc
= QIO_CHANNEL_TLS(object_new(TYPE_QIO_CHANNEL_TLS
));
113 ioc
= QIO_CHANNEL(tioc
);
115 tioc
->master
= master
;
116 if (qio_channel_has_feature(master
, QIO_CHANNEL_FEATURE_SHUTDOWN
)) {
117 qio_channel_set_feature(ioc
, QIO_CHANNEL_FEATURE_SHUTDOWN
);
119 object_ref(OBJECT(master
));
121 tioc
->session
= qcrypto_tls_session_new(
125 QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
,
127 if (!tioc
->session
) {
131 qcrypto_tls_session_set_callbacks(
133 qio_channel_tls_write_handler
,
134 qio_channel_tls_read_handler
,
137 trace_qio_channel_tls_new_client(tioc
, master
, creds
, hostname
);
141 object_unref(OBJECT(tioc
));
145 struct QIOChannelTLSData
{
147 GMainContext
*context
;
149 typedef struct QIOChannelTLSData QIOChannelTLSData
;
151 static gboolean
qio_channel_tls_handshake_io(QIOChannel
*ioc
,
152 GIOCondition condition
,
155 static void qio_channel_tls_handshake_task(QIOChannelTLS
*ioc
,
157 GMainContext
*context
)
160 QCryptoTLSSessionHandshakeStatus status
;
162 if (qcrypto_tls_session_handshake(ioc
->session
, &err
) < 0) {
163 trace_qio_channel_tls_handshake_fail(ioc
);
164 qio_task_set_error(task
, err
);
165 qio_task_complete(task
);
169 status
= qcrypto_tls_session_get_handshake_status(ioc
->session
);
170 if (status
== QCRYPTO_TLS_HANDSHAKE_COMPLETE
) {
171 trace_qio_channel_tls_handshake_complete(ioc
);
172 if (qcrypto_tls_session_check_credentials(ioc
->session
,
174 trace_qio_channel_tls_credentials_deny(ioc
);
175 qio_task_set_error(task
, err
);
177 trace_qio_channel_tls_credentials_allow(ioc
);
179 qio_task_complete(task
);
181 GIOCondition condition
;
182 QIOChannelTLSData
*data
= g_new0(typeof(*data
), 1);
185 data
->context
= context
;
188 g_main_context_ref(context
);
191 if (status
== QCRYPTO_TLS_HANDSHAKE_SENDING
) {
192 condition
= G_IO_OUT
;
197 trace_qio_channel_tls_handshake_pending(ioc
, status
);
198 qio_channel_add_watch_full(ioc
->master
,
200 qio_channel_tls_handshake_io
,
208 static gboolean
qio_channel_tls_handshake_io(QIOChannel
*ioc
,
209 GIOCondition condition
,
212 QIOChannelTLSData
*data
= user_data
;
213 QIOTask
*task
= data
->task
;
214 GMainContext
*context
= data
->context
;
215 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(
216 qio_task_get_source(task
));
219 qio_channel_tls_handshake_task(tioc
, task
, context
);
222 g_main_context_unref(context
);
228 void qio_channel_tls_handshake(QIOChannelTLS
*ioc
,
231 GDestroyNotify destroy
,
232 GMainContext
*context
)
236 task
= qio_task_new(OBJECT(ioc
),
237 func
, opaque
, destroy
);
239 trace_qio_channel_tls_handshake_start(ioc
);
240 qio_channel_tls_handshake_task(ioc
, task
, context
);
244 static void qio_channel_tls_init(Object
*obj G_GNUC_UNUSED
)
249 static void qio_channel_tls_finalize(Object
*obj
)
251 QIOChannelTLS
*ioc
= QIO_CHANNEL_TLS(obj
);
253 object_unref(OBJECT(ioc
->master
));
254 qcrypto_tls_session_free(ioc
->session
);
258 static ssize_t
qio_channel_tls_readv(QIOChannel
*ioc
,
259 const struct iovec
*iov
,
265 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(ioc
);
269 for (i
= 0 ; i
< niov
; i
++) {
270 ssize_t ret
= qcrypto_tls_session_read(tioc
->session
,
274 if (errno
== EAGAIN
) {
278 return QIO_CHANNEL_ERR_BLOCK
;
280 } else if (errno
== ECONNABORTED
&&
281 (qatomic_load_acquire(&tioc
->shutdown
) &
282 QIO_CHANNEL_SHUTDOWN_READ
)) {
286 error_setg_errno(errp
, errno
,
287 "Cannot read from TLS channel");
291 if (ret
< iov
[i
].iov_len
) {
299 static ssize_t
qio_channel_tls_writev(QIOChannel
*ioc
,
300 const struct iovec
*iov
,
307 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(ioc
);
311 for (i
= 0 ; i
< niov
; i
++) {
312 ssize_t ret
= qcrypto_tls_session_write(tioc
->session
,
316 if (errno
== EAGAIN
) {
320 return QIO_CHANNEL_ERR_BLOCK
;
324 error_setg_errno(errp
, errno
,
325 "Cannot write to TLS channel");
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 return qio_channel_close(tioc
->master
, errp
);
380 static void qio_channel_tls_set_aio_fd_handler(QIOChannel
*ioc
,
386 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(ioc
);
388 qio_channel_set_aio_fd_handler(tioc
->master
, ctx
, io_read
, io_write
, opaque
);
391 static GSource
*qio_channel_tls_create_watch(QIOChannel
*ioc
,
392 GIOCondition condition
)
394 QIOChannelTLS
*tioc
= QIO_CHANNEL_TLS(ioc
);
396 return qio_channel_create_watch(tioc
->master
, condition
);
400 qio_channel_tls_get_session(QIOChannelTLS
*ioc
)
405 static void qio_channel_tls_class_init(ObjectClass
*klass
,
406 void *class_data G_GNUC_UNUSED
)
408 QIOChannelClass
*ioc_klass
= QIO_CHANNEL_CLASS(klass
);
410 ioc_klass
->io_writev
= qio_channel_tls_writev
;
411 ioc_klass
->io_readv
= qio_channel_tls_readv
;
412 ioc_klass
->io_set_blocking
= qio_channel_tls_set_blocking
;
413 ioc_klass
->io_set_delay
= qio_channel_tls_set_delay
;
414 ioc_klass
->io_set_cork
= qio_channel_tls_set_cork
;
415 ioc_klass
->io_close
= qio_channel_tls_close
;
416 ioc_klass
->io_shutdown
= qio_channel_tls_shutdown
;
417 ioc_klass
->io_create_watch
= qio_channel_tls_create_watch
;
418 ioc_klass
->io_set_aio_fd_handler
= qio_channel_tls_set_aio_fd_handler
;
421 static const TypeInfo qio_channel_tls_info
= {
422 .parent
= TYPE_QIO_CHANNEL
,
423 .name
= TYPE_QIO_CHANNEL_TLS
,
424 .instance_size
= sizeof(QIOChannelTLS
),
425 .instance_init
= qio_channel_tls_init
,
426 .instance_finalize
= qio_channel_tls_finalize
,
427 .class_init
= qio_channel_tls_class_init
,
430 static void qio_channel_tls_register_types(void)
432 type_register_static(&qio_channel_tls_info
);
435 type_init(qio_channel_tls_register_types
);