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 "io/channel-tls.h"
26 static ssize_t qio_channel_tls_write_handler(const char *buf,
30 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(opaque);
33 ret = qio_channel_write(tioc->master, buf, len, NULL);
34 if (ret == QIO_CHANNEL_ERR_BLOCK) {
44 static ssize_t qio_channel_tls_read_handler(char *buf,
48 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(opaque);
51 ret = qio_channel_read(tioc->master, buf, len, NULL);
52 if (ret == QIO_CHANNEL_ERR_BLOCK) {
64 qio_channel_tls_new_server(QIOChannel *master,
65 QCryptoTLSCreds *creds,
71 ioc = QIO_CHANNEL_TLS(object_new(TYPE_QIO_CHANNEL_TLS));
74 object_ref(OBJECT(master));
76 ioc->session = qcrypto_tls_session_new(
80 QCRYPTO_TLS_CREDS_ENDPOINT_SERVER,
86 qcrypto_tls_session_set_callbacks(
88 qio_channel_tls_write_handler,
89 qio_channel_tls_read_handler,
92 trace_qio_channel_tls_new_server(ioc, master, creds, aclname);
96 object_unref(OBJECT(ioc));
101 qio_channel_tls_new_client(QIOChannel *master,
102 QCryptoTLSCreds *creds,
103 const char *hostname,
109 tioc = QIO_CHANNEL_TLS(object_new(TYPE_QIO_CHANNEL_TLS));
110 ioc = QIO_CHANNEL(tioc);
112 tioc->master = master;
113 if (master->features & (1 << QIO_CHANNEL_FEATURE_SHUTDOWN)) {
114 ioc->features |= (1 << QIO_CHANNEL_FEATURE_SHUTDOWN);
116 object_ref(OBJECT(master));
118 tioc->session = qcrypto_tls_session_new(
122 QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT,
124 if (!tioc->session) {
128 qcrypto_tls_session_set_callbacks(
130 qio_channel_tls_write_handler,
131 qio_channel_tls_read_handler,
134 trace_qio_channel_tls_new_client(tioc, master, creds, hostname);
138 object_unref(OBJECT(tioc));
143 static gboolean qio_channel_tls_handshake_io(QIOChannel *ioc,
144 GIOCondition condition,
147 static void qio_channel_tls_handshake_task(QIOChannelTLS *ioc,
151 QCryptoTLSSessionHandshakeStatus status;
153 if (qcrypto_tls_session_handshake(ioc->session, &err) < 0) {
154 trace_qio_channel_tls_handshake_fail(ioc);
155 qio_task_abort(task, err);
159 status = qcrypto_tls_session_get_handshake_status(ioc->session);
160 if (status == QCRYPTO_TLS_HANDSHAKE_COMPLETE) {
161 trace_qio_channel_tls_handshake_complete(ioc);
162 if (qcrypto_tls_session_check_credentials(ioc->session,
164 trace_qio_channel_tls_credentials_deny(ioc);
165 qio_task_abort(task, err);
168 trace_qio_channel_tls_credentials_allow(ioc);
169 qio_task_complete(task);
171 GIOCondition condition;
172 if (status == QCRYPTO_TLS_HANDSHAKE_SENDING) {
173 condition = G_IO_OUT;
178 trace_qio_channel_tls_handshake_pending(ioc, status);
179 qio_channel_add_watch(ioc->master,
181 qio_channel_tls_handshake_io,
191 static gboolean qio_channel_tls_handshake_io(QIOChannel *ioc,
192 GIOCondition condition,
195 QIOTask *task = user_data;
196 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(
197 qio_task_get_source(task));
199 qio_channel_tls_handshake_task(
202 object_unref(OBJECT(tioc));
207 void qio_channel_tls_handshake(QIOChannelTLS *ioc,
210 GDestroyNotify destroy)
214 task = qio_task_new(OBJECT(ioc),
215 func, opaque, destroy);
217 trace_qio_channel_tls_handshake_start(ioc);
218 qio_channel_tls_handshake_task(ioc, task);
222 static void qio_channel_tls_init(Object *obj G_GNUC_UNUSED)
227 static void qio_channel_tls_finalize(Object *obj)
229 QIOChannelTLS *ioc = QIO_CHANNEL_TLS(obj);
231 object_unref(OBJECT(ioc->master));
232 qcrypto_tls_session_free(ioc->session);
236 static ssize_t qio_channel_tls_readv(QIOChannel *ioc,
237 const struct iovec *iov,
243 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
247 for (i = 0 ; i < niov ; i++) {
248 ssize_t ret = qcrypto_tls_session_read(tioc->session,
252 if (errno == EAGAIN) {
256 return QIO_CHANNEL_ERR_BLOCK;
260 error_setg_errno(errp, errno,
261 "Cannot read from TLS channel");
265 if (ret < iov[i].iov_len) {
273 static ssize_t qio_channel_tls_writev(QIOChannel *ioc,
274 const struct iovec *iov,
280 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
284 for (i = 0 ; i < niov ; i++) {
285 ssize_t ret = qcrypto_tls_session_write(tioc->session,
289 if (errno == EAGAIN) {
293 return QIO_CHANNEL_ERR_BLOCK;
297 error_setg_errno(errp, errno,
298 "Cannot write to TLS channel");
302 if (ret < iov[i].iov_len) {
309 static int qio_channel_tls_set_blocking(QIOChannel *ioc,
313 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
315 return qio_channel_set_blocking(tioc->master, enabled, errp);
318 static void qio_channel_tls_set_delay(QIOChannel *ioc,
321 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
323 qio_channel_set_delay(tioc->master, enabled);
326 static void qio_channel_tls_set_cork(QIOChannel *ioc,
329 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
331 qio_channel_set_cork(tioc->master, enabled);
334 static int qio_channel_tls_shutdown(QIOChannel *ioc,
335 QIOChannelShutdown how,
338 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
340 return qio_channel_shutdown(tioc->master, how, errp);
343 static int qio_channel_tls_close(QIOChannel *ioc,
346 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
348 return qio_channel_close(tioc->master, errp);
351 static GSource *qio_channel_tls_create_watch(QIOChannel *ioc,
352 GIOCondition condition)
354 QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc);
356 return qio_channel_create_watch(tioc->master, condition);
360 qio_channel_tls_get_session(QIOChannelTLS *ioc)
365 static void qio_channel_tls_class_init(ObjectClass *klass,
366 void *class_data G_GNUC_UNUSED)
368 QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
370 ioc_klass->io_writev = qio_channel_tls_writev;
371 ioc_klass->io_readv = qio_channel_tls_readv;
372 ioc_klass->io_set_blocking = qio_channel_tls_set_blocking;
373 ioc_klass->io_set_delay = qio_channel_tls_set_delay;
374 ioc_klass->io_set_cork = qio_channel_tls_set_cork;
375 ioc_klass->io_close = qio_channel_tls_close;
376 ioc_klass->io_shutdown = qio_channel_tls_shutdown;
377 ioc_klass->io_create_watch = qio_channel_tls_create_watch;
380 static const TypeInfo qio_channel_tls_info = {
381 .parent = TYPE_QIO_CHANNEL,
382 .name = TYPE_QIO_CHANNEL_TLS,
383 .instance_size = sizeof(QIOChannelTLS),
384 .instance_init = qio_channel_tls_init,
385 .instance_finalize = qio_channel_tls_finalize,
386 .class_init = qio_channel_tls_class_init,
389 static void qio_channel_tls_register_types(void)
391 type_register_static(&qio_channel_tls_info);
394 type_init(qio_channel_tls_register_types);