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 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"
28 static ssize_t qio_channel_tls_write_handler(const char *buf,
32 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(opaque);
35 ret = qio_channel_write(tioc->master, buf, len, NULL);
36 if (ret == QIO_CHANNEL_ERR_BLOCK) {
46 static ssize_t qio_channel_tls_read_handler(char *buf,
50 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(opaque);
53 ret = qio_channel_read(tioc->master, buf, len, NULL);
54 if (ret == QIO_CHANNEL_ERR_BLOCK) {
66 qio_channel_tls_new_server(QIOChannel *master,
67 QCryptoTLSCreds *creds,
73 ioc = QIO_CHANNEL_TLS(object_new(TYPE_QIO_CHANNEL_TLS));
76 object_ref(OBJECT(master));
78 ioc->session = qcrypto_tls_session_new(
82 QCRYPTO_TLS_CREDS_ENDPOINT_SERVER,
88 qcrypto_tls_session_set_callbacks(
90 qio_channel_tls_write_handler,
91 qio_channel_tls_read_handler,
94 trace_qio_channel_tls_new_server(ioc, master, creds, aclname);
98 object_unref(OBJECT(ioc));
103 qio_channel_tls_new_client(QIOChannel *master,
104 QCryptoTLSCreds *creds,
105 const char *hostname,
111 tioc = QIO_CHANNEL_TLS(object_new(TYPE_QIO_CHANNEL_TLS));
112 ioc = QIO_CHANNEL(tioc);
114 tioc->master = master;
115 if (qio_channel_has_feature(master, QIO_CHANNEL_FEATURE_SHUTDOWN)) {
116 qio_channel_set_feature(ioc, QIO_CHANNEL_FEATURE_SHUTDOWN);
118 object_ref(OBJECT(master));
120 tioc->session = qcrypto_tls_session_new(
124 QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT,
126 if (!tioc->session) {
130 qcrypto_tls_session_set_callbacks(
132 qio_channel_tls_write_handler,
133 qio_channel_tls_read_handler,
136 trace_qio_channel_tls_new_client(tioc, master, creds, hostname);
140 object_unref(OBJECT(tioc));
144 struct QIOChannelTLSData {
146 GMainContext *context;
148 typedef struct QIOChannelTLSData QIOChannelTLSData;
150 static gboolean qio_channel_tls_handshake_io(QIOChannel *ioc,
151 GIOCondition condition,
154 static void qio_channel_tls_handshake_task(QIOChannelTLS *ioc,
156 GMainContext *context)
159 QCryptoTLSSessionHandshakeStatus status;
161 if (qcrypto_tls_session_handshake(ioc->session, &err) < 0) {
162 trace_qio_channel_tls_handshake_fail(ioc);
163 qio_task_set_error(task, err);
164 qio_task_complete(task);
168 status = qcrypto_tls_session_get_handshake_status(ioc->session);
169 if (status == QCRYPTO_TLS_HANDSHAKE_COMPLETE) {
170 trace_qio_channel_tls_handshake_complete(ioc);
171 if (qcrypto_tls_session_check_credentials(ioc->session,
173 trace_qio_channel_tls_credentials_deny(ioc);
174 qio_task_set_error(task, err);
176 trace_qio_channel_tls_credentials_allow(ioc);
178 qio_task_complete(task);
180 GIOCondition condition;
181 QIOChannelTLSData *data = g_new0(typeof(*data), 1);
184 data->context = context;
187 g_main_context_ref(context);
190 if (status == QCRYPTO_TLS_HANDSHAKE_SENDING) {
191 condition = G_IO_OUT;
196 trace_qio_channel_tls_handshake_pending(ioc, status);
197 qio_channel_add_watch_full(ioc->master,
199 qio_channel_tls_handshake_io,
207 static gboolean qio_channel_tls_handshake_io(QIOChannel *ioc,
208 GIOCondition condition,
211 QIOChannelTLSData *data = user_data;
212 QIOTask *task = data->task;
213 GMainContext *context = data->context;
214 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(
215 qio_task_get_source(task));
218 qio_channel_tls_handshake_task(tioc, task, context);
221 g_main_context_unref(context);
227 void qio_channel_tls_handshake(QIOChannelTLS *ioc,
230 GDestroyNotify destroy,
231 GMainContext *context)
235 task = qio_task_new(OBJECT(ioc),
236 func, opaque, destroy);
238 trace_qio_channel_tls_handshake_start(ioc);
239 qio_channel_tls_handshake_task(ioc, task, context);
243 static void qio_channel_tls_init(Object *obj G_GNUC_UNUSED)
248 static void qio_channel_tls_finalize(Object *obj)
250 QIOChannelTLS *ioc = QIO_CHANNEL_TLS(obj);
252 object_unref(OBJECT(ioc->master));
253 qcrypto_tls_session_free(ioc->session);
257 static ssize_t qio_channel_tls_readv(QIOChannel *ioc,
258 const struct iovec *iov,
264 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
268 for (i = 0 ; i < niov ; i++) {
269 ssize_t ret = qcrypto_tls_session_read(tioc->session,
273 if (errno == EAGAIN) {
277 return QIO_CHANNEL_ERR_BLOCK;
279 } else if (errno == ECONNABORTED &&
280 (tioc->shutdown & QIO_CHANNEL_SHUTDOWN_READ)) {
284 error_setg_errno(errp, errno,
285 "Cannot read from TLS channel");
289 if (ret < iov[i].iov_len) {
297 static ssize_t qio_channel_tls_writev(QIOChannel *ioc,
298 const struct iovec *iov,
304 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
308 for (i = 0 ; i < niov ; i++) {
309 ssize_t ret = qcrypto_tls_session_write(tioc->session,
313 if (errno == EAGAIN) {
317 return QIO_CHANNEL_ERR_BLOCK;
321 error_setg_errno(errp, errno,
322 "Cannot write to TLS channel");
326 if (ret < iov[i].iov_len) {
333 static int qio_channel_tls_set_blocking(QIOChannel *ioc,
337 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
339 return qio_channel_set_blocking(tioc->master, enabled, errp);
342 static void qio_channel_tls_set_delay(QIOChannel *ioc,
345 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
347 qio_channel_set_delay(tioc->master, enabled);
350 static void qio_channel_tls_set_cork(QIOChannel *ioc,
353 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
355 qio_channel_set_cork(tioc->master, enabled);
358 static int qio_channel_tls_shutdown(QIOChannel *ioc,
359 QIOChannelShutdown how,
362 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
364 tioc->shutdown |= how;
366 return qio_channel_shutdown(tioc->master, how, errp);
369 static int qio_channel_tls_close(QIOChannel *ioc,
372 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
374 return qio_channel_close(tioc->master, errp);
377 static void qio_channel_tls_set_aio_fd_handler(QIOChannel *ioc,
383 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
385 qio_channel_set_aio_fd_handler(tioc->master, ctx, io_read, io_write, opaque);
388 static GSource *qio_channel_tls_create_watch(QIOChannel *ioc,
389 GIOCondition condition)
391 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
393 return qio_channel_create_watch(tioc->master, condition);
397 qio_channel_tls_get_session(QIOChannelTLS *ioc)
402 static void qio_channel_tls_class_init(ObjectClass *klass,
403 void *class_data G_GNUC_UNUSED)
405 QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
407 ioc_klass->io_writev = qio_channel_tls_writev;
408 ioc_klass->io_readv = qio_channel_tls_readv;
409 ioc_klass->io_set_blocking = qio_channel_tls_set_blocking;
410 ioc_klass->io_set_delay = qio_channel_tls_set_delay;
411 ioc_klass->io_set_cork = qio_channel_tls_set_cork;
412 ioc_klass->io_close = qio_channel_tls_close;
413 ioc_klass->io_shutdown = qio_channel_tls_shutdown;
414 ioc_klass->io_create_watch = qio_channel_tls_create_watch;
415 ioc_klass->io_set_aio_fd_handler = qio_channel_tls_set_aio_fd_handler;
418 static const TypeInfo qio_channel_tls_info = {
419 .parent = TYPE_QIO_CHANNEL,
420 .name = TYPE_QIO_CHANNEL_TLS,
421 .instance_size = sizeof(QIOChannelTLS),
422 .instance_init = qio_channel_tls_init,
423 .instance_finalize = qio_channel_tls_finalize,
424 .class_init = qio_channel_tls_class_init,
427 static void qio_channel_tls_register_types(void)
429 type_register_static(&qio_channel_tls_info);
432 type_init(qio_channel_tls_register_types);