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 "io/channel-tls.h"
27 static ssize_t qio_channel_tls_write_handler(const char *buf,
31 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(opaque);
34 ret = qio_channel_write(tioc->master, buf, len, NULL);
35 if (ret == QIO_CHANNEL_ERR_BLOCK) {
45 static ssize_t qio_channel_tls_read_handler(char *buf,
49 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(opaque);
52 ret = qio_channel_read(tioc->master, buf, len, NULL);
53 if (ret == QIO_CHANNEL_ERR_BLOCK) {
65 qio_channel_tls_new_server(QIOChannel *master,
66 QCryptoTLSCreds *creds,
72 ioc = QIO_CHANNEL_TLS(object_new(TYPE_QIO_CHANNEL_TLS));
75 object_ref(OBJECT(master));
77 ioc->session = qcrypto_tls_session_new(
81 QCRYPTO_TLS_CREDS_ENDPOINT_SERVER,
87 qcrypto_tls_session_set_callbacks(
89 qio_channel_tls_write_handler,
90 qio_channel_tls_read_handler,
93 trace_qio_channel_tls_new_server(ioc, master, creds, aclname);
97 object_unref(OBJECT(ioc));
102 qio_channel_tls_new_client(QIOChannel *master,
103 QCryptoTLSCreds *creds,
104 const char *hostname,
110 tioc = QIO_CHANNEL_TLS(object_new(TYPE_QIO_CHANNEL_TLS));
111 ioc = QIO_CHANNEL(tioc);
113 tioc->master = master;
114 if (master->features & (1 << QIO_CHANNEL_FEATURE_SHUTDOWN)) {
115 ioc->features |= (1 << QIO_CHANNEL_FEATURE_SHUTDOWN);
117 object_ref(OBJECT(master));
119 tioc->session = qcrypto_tls_session_new(
123 QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT,
125 if (!tioc->session) {
129 qcrypto_tls_session_set_callbacks(
131 qio_channel_tls_write_handler,
132 qio_channel_tls_read_handler,
135 trace_qio_channel_tls_new_client(tioc, master, creds, hostname);
139 object_unref(OBJECT(tioc));
144 static gboolean qio_channel_tls_handshake_io(QIOChannel *ioc,
145 GIOCondition condition,
148 static void qio_channel_tls_handshake_task(QIOChannelTLS *ioc,
152 QCryptoTLSSessionHandshakeStatus status;
154 if (qcrypto_tls_session_handshake(ioc->session, &err) < 0) {
155 trace_qio_channel_tls_handshake_fail(ioc);
156 qio_task_abort(task, err);
160 status = qcrypto_tls_session_get_handshake_status(ioc->session);
161 if (status == QCRYPTO_TLS_HANDSHAKE_COMPLETE) {
162 trace_qio_channel_tls_handshake_complete(ioc);
163 if (qcrypto_tls_session_check_credentials(ioc->session,
165 trace_qio_channel_tls_credentials_deny(ioc);
166 qio_task_abort(task, err);
169 trace_qio_channel_tls_credentials_allow(ioc);
170 qio_task_complete(task);
172 GIOCondition condition;
173 if (status == QCRYPTO_TLS_HANDSHAKE_SENDING) {
174 condition = G_IO_OUT;
179 trace_qio_channel_tls_handshake_pending(ioc, status);
180 qio_channel_add_watch(ioc->master,
182 qio_channel_tls_handshake_io,
192 static gboolean qio_channel_tls_handshake_io(QIOChannel *ioc,
193 GIOCondition condition,
196 QIOTask *task = user_data;
197 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(
198 qio_task_get_source(task));
200 qio_channel_tls_handshake_task(
203 object_unref(OBJECT(tioc));
208 void qio_channel_tls_handshake(QIOChannelTLS *ioc,
211 GDestroyNotify destroy)
215 task = qio_task_new(OBJECT(ioc),
216 func, opaque, destroy);
218 trace_qio_channel_tls_handshake_start(ioc);
219 qio_channel_tls_handshake_task(ioc, task);
223 static void qio_channel_tls_init(Object *obj G_GNUC_UNUSED)
228 static void qio_channel_tls_finalize(Object *obj)
230 QIOChannelTLS *ioc = QIO_CHANNEL_TLS(obj);
232 object_unref(OBJECT(ioc->master));
233 qcrypto_tls_session_free(ioc->session);
237 static ssize_t qio_channel_tls_readv(QIOChannel *ioc,
238 const struct iovec *iov,
244 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
248 for (i = 0 ; i < niov ; i++) {
249 ssize_t ret = qcrypto_tls_session_read(tioc->session,
253 if (errno == EAGAIN) {
257 return QIO_CHANNEL_ERR_BLOCK;
261 error_setg_errno(errp, errno,
262 "Cannot read from TLS channel");
266 if (ret < iov[i].iov_len) {
274 static ssize_t qio_channel_tls_writev(QIOChannel *ioc,
275 const struct iovec *iov,
281 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
285 for (i = 0 ; i < niov ; i++) {
286 ssize_t ret = qcrypto_tls_session_write(tioc->session,
290 if (errno == EAGAIN) {
294 return QIO_CHANNEL_ERR_BLOCK;
298 error_setg_errno(errp, errno,
299 "Cannot write to TLS channel");
303 if (ret < iov[i].iov_len) {
310 static int qio_channel_tls_set_blocking(QIOChannel *ioc,
314 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
316 return qio_channel_set_blocking(tioc->master, enabled, errp);
319 static void qio_channel_tls_set_delay(QIOChannel *ioc,
322 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
324 qio_channel_set_delay(tioc->master, enabled);
327 static void qio_channel_tls_set_cork(QIOChannel *ioc,
330 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
332 qio_channel_set_cork(tioc->master, enabled);
335 static int qio_channel_tls_shutdown(QIOChannel *ioc,
336 QIOChannelShutdown how,
339 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
341 return qio_channel_shutdown(tioc->master, how, errp);
344 static int qio_channel_tls_close(QIOChannel *ioc,
347 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
349 return qio_channel_close(tioc->master, errp);
352 static GSource *qio_channel_tls_create_watch(QIOChannel *ioc,
353 GIOCondition condition)
355 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
357 return qio_channel_create_watch(tioc->master, condition);
361 qio_channel_tls_get_session(QIOChannelTLS *ioc)
366 static void qio_channel_tls_class_init(ObjectClass *klass,
367 void *class_data G_GNUC_UNUSED)
369 QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
371 ioc_klass->io_writev = qio_channel_tls_writev;
372 ioc_klass->io_readv = qio_channel_tls_readv;
373 ioc_klass->io_set_blocking = qio_channel_tls_set_blocking;
374 ioc_klass->io_set_delay = qio_channel_tls_set_delay;
375 ioc_klass->io_set_cork = qio_channel_tls_set_cork;
376 ioc_klass->io_close = qio_channel_tls_close;
377 ioc_klass->io_shutdown = qio_channel_tls_shutdown;
378 ioc_klass->io_create_watch = qio_channel_tls_create_watch;
381 static const TypeInfo qio_channel_tls_info = {
382 .parent = TYPE_QIO_CHANNEL,
383 .name = TYPE_QIO_CHANNEL_TLS,
384 .instance_size = sizeof(QIOChannelTLS),
385 .instance_init = qio_channel_tls_init,
386 .instance_finalize = qio_channel_tls_finalize,
387 .class_init = qio_channel_tls_class_init,
390 static void qio_channel_tls_register_types(void)
392 type_register_static(&qio_channel_tls_info);
395 type_init(qio_channel_tls_register_types);