| 1 | /* |
| 2 | * QEMU I/O channels TLS driver |
| 3 | * |
| 4 | * Copyright (c) 2015 Red Hat, Inc. |
| 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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/>. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include "qemu/osdep.h" |
| 22 | #include "qapi/error.h" |
| 23 | #include "qemu/module.h" |
| 24 | #include "io/channel-tls.h" |
| 25 | #include "trace.h" |
| 26 | #include "qemu/atomic.h" |
| 27 | |
| 28 | |
| 29 | static ssize_t qio_channel_tls_write_handler(const void *buf, |
| 30 | size_t len, |
| 31 | void *opaque, |
| 32 | Error **errp) |
| 33 | { |
| 34 | QIOChannelTLS *tioc = QIO_CHANNEL_TLS(opaque); |
| 35 | ssize_t ret; |
| 36 | |
| 37 | ret = qio_channel_write(tioc->master, buf, len, errp); |
| 38 | if (ret == QIO_CHANNEL_ERR_BLOCK) { |
| 39 | return QCRYPTO_TLS_SESSION_ERR_BLOCK; |
| 40 | } else if (ret < 0) { |
| 41 | return -1; |
| 42 | } |
| 43 | return ret; |
| 44 | } |
| 45 | |
| 46 | static ssize_t qio_channel_tls_read_handler(void *buf, |
| 47 | size_t len, |
| 48 | void *opaque, |
| 49 | Error **errp) |
| 50 | { |
| 51 | QIOChannelTLS *tioc = QIO_CHANNEL_TLS(opaque); |
| 52 | ssize_t ret; |
| 53 | |
| 54 | ret = qio_channel_read(tioc->master, buf, len, errp); |
| 55 | if (ret == QIO_CHANNEL_ERR_BLOCK) { |
| 56 | return QCRYPTO_TLS_SESSION_ERR_BLOCK; |
| 57 | } else if (ret < 0) { |
| 58 | return -1; |
| 59 | } |
| 60 | return ret; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | QIOChannelTLS * |
| 65 | qio_channel_tls_new_server(QIOChannel *master, |
| 66 | QCryptoTLSCreds *creds, |
| 67 | const char *aclname, |
| 68 | Error **errp) |
| 69 | { |
| 70 | QIOChannelTLS *tioc; |
| 71 | QIOChannel *ioc; |
| 72 | |
| 73 | tioc = QIO_CHANNEL_TLS(object_new(TYPE_QIO_CHANNEL_TLS)); |
| 74 | ioc = QIO_CHANNEL(tioc); |
| 75 | |
| 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); |
| 80 | } |
| 81 | object_ref(OBJECT(master)); |
| 82 | |
| 83 | tioc->session = qcrypto_tls_session_new( |
| 84 | creds, |
| 85 | NULL, |
| 86 | aclname, |
| 87 | QCRYPTO_TLS_CREDS_ENDPOINT_SERVER, |
| 88 | errp); |
| 89 | if (!tioc->session) { |
| 90 | goto error; |
| 91 | } |
| 92 | |
| 93 | qcrypto_tls_session_set_callbacks( |
| 94 | tioc->session, |
| 95 | qio_channel_tls_write_handler, |
| 96 | qio_channel_tls_read_handler, |
| 97 | tioc); |
| 98 | |
| 99 | trace_qio_channel_tls_new_server(tioc, master, creds, aclname); |
| 100 | return tioc; |
| 101 | |
| 102 | error: |
| 103 | object_unref(OBJECT(tioc)); |
| 104 | return NULL; |
| 105 | } |
| 106 | |
| 107 | QIOChannelTLS * |
| 108 | qio_channel_tls_new_client(QIOChannel *master, |
| 109 | QCryptoTLSCreds *creds, |
| 110 | const char *hostname, |
| 111 | Error **errp) |
| 112 | { |
| 113 | QIOChannelTLS *tioc; |
| 114 | QIOChannel *ioc; |
| 115 | |
| 116 | tioc = QIO_CHANNEL_TLS(object_new(TYPE_QIO_CHANNEL_TLS)); |
| 117 | ioc = QIO_CHANNEL(tioc); |
| 118 | |
| 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); |
| 123 | } |
| 124 | object_ref(OBJECT(master)); |
| 125 | |
| 126 | tioc->session = qcrypto_tls_session_new( |
| 127 | creds, |
| 128 | hostname, |
| 129 | NULL, |
| 130 | QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT, |
| 131 | errp); |
| 132 | if (!tioc->session) { |
| 133 | goto error; |
| 134 | } |
| 135 | |
| 136 | qcrypto_tls_session_set_callbacks( |
| 137 | tioc->session, |
| 138 | qio_channel_tls_write_handler, |
| 139 | qio_channel_tls_read_handler, |
| 140 | tioc); |
| 141 | |
| 142 | trace_qio_channel_tls_new_client(tioc, master, creds, hostname); |
| 143 | return tioc; |
| 144 | |
| 145 | error: |
| 146 | object_unref(OBJECT(tioc)); |
| 147 | return NULL; |
| 148 | } |
| 149 | |
| 150 | struct QIOChannelTLSData { |
| 151 | QIOTask *task; |
| 152 | GMainContext *context; |
| 153 | }; |
| 154 | typedef struct QIOChannelTLSData QIOChannelTLSData; |
| 155 | |
| 156 | static void qio_channel_tls_io_data_free(gpointer user_data) |
| 157 | { |
| 158 | QIOChannelTLSData *data = user_data; |
| 159 | /* |
| 160 | * Usually 'task' will be NULL since the GSource |
| 161 | * callback will either complete the task or pass |
| 162 | * it on to a new GSource. We'll see a non-NULL |
| 163 | * task here only if the GSource was released before |
| 164 | * its callback triggers |
| 165 | */ |
| 166 | if (data->task) { |
| 167 | qio_task_free(data->task); |
| 168 | } |
| 169 | if (data->context) { |
| 170 | g_main_context_unref(data->context); |
| 171 | } |
| 172 | g_free(data); |
| 173 | } |
| 174 | |
| 175 | static gboolean qio_channel_tls_handshake_io(QIOChannel *ioc, |
| 176 | GIOCondition condition, |
| 177 | gpointer user_data); |
| 178 | |
| 179 | static gboolean qio_channel_tls_handshake_task(QIOChannelTLS *ioc, |
| 180 | QIOTask *task, |
| 181 | GMainContext *context) |
| 182 | { |
| 183 | Error *err = NULL; |
| 184 | int status; |
| 185 | |
| 186 | status = qcrypto_tls_session_handshake(ioc->session, &err); |
| 187 | |
| 188 | if (status < 0) { |
| 189 | trace_qio_channel_tls_handshake_fail(ioc); |
| 190 | qio_task_set_error(task, err); |
| 191 | qio_task_complete(task); |
| 192 | return TRUE; |
| 193 | } |
| 194 | |
| 195 | if (status == QCRYPTO_TLS_HANDSHAKE_COMPLETE) { |
| 196 | trace_qio_channel_tls_handshake_complete(ioc); |
| 197 | if (qcrypto_tls_session_check_credentials(ioc->session, |
| 198 | &err) < 0) { |
| 199 | trace_qio_channel_tls_credentials_deny(ioc); |
| 200 | qio_task_set_error(task, err); |
| 201 | } else { |
| 202 | trace_qio_channel_tls_credentials_allow(ioc); |
| 203 | } |
| 204 | qio_task_complete(task); |
| 205 | return TRUE; |
| 206 | } else { |
| 207 | GIOCondition condition; |
| 208 | QIOChannelTLSData *data = g_new0(typeof(*data), 1); |
| 209 | |
| 210 | data->task = task; |
| 211 | data->context = context; |
| 212 | |
| 213 | if (context) { |
| 214 | g_main_context_ref(context); |
| 215 | } |
| 216 | |
| 217 | if (status == QCRYPTO_TLS_HANDSHAKE_SENDING) { |
| 218 | condition = G_IO_OUT; |
| 219 | } else { |
| 220 | condition = G_IO_IN; |
| 221 | } |
| 222 | |
| 223 | trace_qio_channel_tls_handshake_pending(ioc, status); |
| 224 | ioc->hs_ioc_tag = |
| 225 | qio_channel_add_watch_full(ioc->master, |
| 226 | condition, |
| 227 | qio_channel_tls_handshake_io, |
| 228 | data, |
| 229 | qio_channel_tls_io_data_free, |
| 230 | context); |
| 231 | return FALSE; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | |
| 236 | static gboolean qio_channel_tls_handshake_io(QIOChannel *ioc, |
| 237 | GIOCondition condition, |
| 238 | gpointer user_data) |
| 239 | { |
| 240 | QIOChannelTLSData *data = user_data; |
| 241 | QIOTask *task = data->task; |
| 242 | GMainContext *context = data->context; |
| 243 | QIOChannelTLS *tioc = QIO_CHANNEL_TLS( |
| 244 | qio_task_get_source(task)); |
| 245 | |
| 246 | tioc->hs_ioc_tag = 0; |
| 247 | if (!qio_channel_tls_handshake_task(tioc, task, context)) { |
| 248 | /* task is kept by new GSource so must not be released yet */ |
| 249 | data->task = NULL; |
| 250 | } |
| 251 | |
| 252 | return FALSE; |
| 253 | } |
| 254 | |
| 255 | void qio_channel_tls_handshake(QIOChannelTLS *ioc, |
| 256 | QIOTaskFunc func, |
| 257 | gpointer opaque, |
| 258 | GDestroyNotify destroy, |
| 259 | GMainContext *context) |
| 260 | { |
| 261 | QIOTask *task; |
| 262 | |
| 263 | if (qio_channel_has_feature(QIO_CHANNEL(ioc), |
| 264 | QIO_CHANNEL_FEATURE_CONCURRENT_IO)) { |
| 265 | qcrypto_tls_session_require_thread_safety(ioc->session); |
| 266 | } |
| 267 | |
| 268 | task = qio_task_new(OBJECT(ioc), |
| 269 | func, opaque, destroy); |
| 270 | |
| 271 | trace_qio_channel_tls_handshake_start(ioc); |
| 272 | if (qio_channel_tls_handshake_task(ioc, task, context)) { |
| 273 | qio_task_free(task); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | static gboolean qio_channel_tls_bye_io(QIOChannel *ioc, GIOCondition condition, |
| 278 | gpointer user_data); |
| 279 | |
| 280 | static gboolean qio_channel_tls_bye_task(QIOChannelTLS *ioc, QIOTask *task, |
| 281 | GMainContext *context) |
| 282 | { |
| 283 | GIOCondition condition; |
| 284 | QIOChannelTLSData *data; |
| 285 | int status; |
| 286 | Error *err = NULL; |
| 287 | |
| 288 | status = qcrypto_tls_session_bye(ioc->session, &err); |
| 289 | |
| 290 | if (status < 0) { |
| 291 | trace_qio_channel_tls_bye_fail(ioc); |
| 292 | qio_task_set_error(task, err); |
| 293 | qio_task_complete(task); |
| 294 | return TRUE; |
| 295 | } |
| 296 | |
| 297 | if (status == QCRYPTO_TLS_BYE_COMPLETE) { |
| 298 | qio_task_complete(task); |
| 299 | return TRUE; |
| 300 | } |
| 301 | |
| 302 | data = g_new0(typeof(*data), 1); |
| 303 | data->task = task; |
| 304 | data->context = context; |
| 305 | |
| 306 | if (context) { |
| 307 | g_main_context_ref(context); |
| 308 | } |
| 309 | |
| 310 | if (status == QCRYPTO_TLS_BYE_SENDING) { |
| 311 | condition = G_IO_OUT; |
| 312 | } else { |
| 313 | condition = G_IO_IN; |
| 314 | } |
| 315 | |
| 316 | trace_qio_channel_tls_bye_pending(ioc, status); |
| 317 | ioc->bye_ioc_tag = qio_channel_add_watch_full(ioc->master, condition, |
| 318 | qio_channel_tls_bye_io, |
| 319 | data, |
| 320 | qio_channel_tls_io_data_free, |
| 321 | context); |
| 322 | return FALSE; |
| 323 | } |
| 324 | |
| 325 | |
| 326 | static gboolean qio_channel_tls_bye_io(QIOChannel *ioc, GIOCondition condition, |
| 327 | gpointer user_data) |
| 328 | { |
| 329 | QIOChannelTLSData *data = user_data; |
| 330 | QIOTask *task = data->task; |
| 331 | GMainContext *context = data->context; |
| 332 | QIOChannelTLS *tioc = QIO_CHANNEL_TLS(qio_task_get_source(task)); |
| 333 | |
| 334 | tioc->bye_ioc_tag = 0; |
| 335 | if (!qio_channel_tls_bye_task(tioc, task, context)) { |
| 336 | /* task is kept by new GSource so must not be released yet */ |
| 337 | data->task = NULL; |
| 338 | } |
| 339 | |
| 340 | return FALSE; |
| 341 | } |
| 342 | |
| 343 | static void propagate_error(QIOTask *task, gpointer opaque) |
| 344 | { |
| 345 | qio_task_propagate_error(task, opaque); |
| 346 | } |
| 347 | |
| 348 | void qio_channel_tls_bye(QIOChannelTLS *ioc, Error **errp) |
| 349 | { |
| 350 | QIOTask *task; |
| 351 | |
| 352 | task = qio_task_new(OBJECT(ioc), propagate_error, errp, NULL); |
| 353 | |
| 354 | trace_qio_channel_tls_bye_start(ioc); |
| 355 | if (qio_channel_tls_bye_task(ioc, task, NULL)) { |
| 356 | qio_task_free(task); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | static void qio_channel_tls_init(Object *obj G_GNUC_UNUSED) |
| 361 | { |
| 362 | } |
| 363 | |
| 364 | |
| 365 | static void qio_channel_tls_finalize(Object *obj) |
| 366 | { |
| 367 | QIOChannelTLS *ioc = QIO_CHANNEL_TLS(obj); |
| 368 | |
| 369 | if (ioc->hs_ioc_tag) { |
| 370 | trace_qio_channel_tls_handshake_cancel(ioc); |
| 371 | g_clear_handle_id(&ioc->hs_ioc_tag, g_source_remove); |
| 372 | } |
| 373 | |
| 374 | if (ioc->bye_ioc_tag) { |
| 375 | trace_qio_channel_tls_bye_cancel(ioc); |
| 376 | g_clear_handle_id(&ioc->bye_ioc_tag, g_source_remove); |
| 377 | } |
| 378 | |
| 379 | object_unref(OBJECT(ioc->master)); |
| 380 | qcrypto_tls_session_free(ioc->session); |
| 381 | } |
| 382 | |
| 383 | static bool |
| 384 | qio_channel_tls_allow_premature_termination(QIOChannelTLS *tioc, int flags) |
| 385 | { |
| 386 | if (flags & QIO_CHANNEL_READ_FLAG_RELAXED_EOF) { |
| 387 | return true; |
| 388 | } |
| 389 | |
| 390 | if (qatomic_read(&tioc->shutdown) & QIO_CHANNEL_SHUTDOWN_READ) { |
| 391 | return true; |
| 392 | } |
| 393 | |
| 394 | return false; |
| 395 | } |
| 396 | |
| 397 | static ssize_t qio_channel_tls_readv(QIOChannel *ioc, |
| 398 | const struct iovec *iov, |
| 399 | size_t niov, |
| 400 | int **fds, |
| 401 | size_t *nfds, |
| 402 | int flags, |
| 403 | Error **errp) |
| 404 | { |
| 405 | ERRP_GUARD(); |
| 406 | QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc); |
| 407 | size_t i; |
| 408 | ssize_t got = 0; |
| 409 | |
| 410 | for (i = 0 ; i < niov ; i++) { |
| 411 | ssize_t ret = qcrypto_tls_session_read( |
| 412 | tioc->session, |
| 413 | iov[i].iov_base, |
| 414 | iov[i].iov_len, |
| 415 | errp); |
| 416 | if (ret == QCRYPTO_TLS_SESSION_ERR_BLOCK) { |
| 417 | if (got) { |
| 418 | return got; |
| 419 | } else { |
| 420 | return QIO_CHANNEL_ERR_BLOCK; |
| 421 | } |
| 422 | } else if (ret < 0) { |
| 423 | if (ret == QCRYPTO_TLS_SESSION_PREMATURE_TERMINATION && |
| 424 | qio_channel_tls_allow_premature_termination(tioc, flags)) { |
| 425 | error_free(*errp); |
| 426 | *errp = NULL; |
| 427 | return got; |
| 428 | } |
| 429 | return -1; |
| 430 | } |
| 431 | got += ret; |
| 432 | if (ret < iov[i].iov_len) { |
| 433 | break; |
| 434 | } |
| 435 | } |
| 436 | return got; |
| 437 | } |
| 438 | |
| 439 | |
| 440 | static ssize_t qio_channel_tls_writev(QIOChannel *ioc, |
| 441 | const struct iovec *iov, |
| 442 | size_t niov, |
| 443 | int *fds, |
| 444 | size_t nfds, |
| 445 | int flags, |
| 446 | Error **errp) |
| 447 | { |
| 448 | QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc); |
| 449 | size_t i; |
| 450 | ssize_t done = 0; |
| 451 | |
| 452 | for (i = 0 ; i < niov ; i++) { |
| 453 | ssize_t ret = qcrypto_tls_session_write(tioc->session, |
| 454 | iov[i].iov_base, |
| 455 | iov[i].iov_len, |
| 456 | errp); |
| 457 | if (ret == QCRYPTO_TLS_SESSION_ERR_BLOCK) { |
| 458 | if (done) { |
| 459 | return done; |
| 460 | } else { |
| 461 | return QIO_CHANNEL_ERR_BLOCK; |
| 462 | } |
| 463 | } else if (ret < 0) { |
| 464 | return -1; |
| 465 | } |
| 466 | done += ret; |
| 467 | if (ret < iov[i].iov_len) { |
| 468 | break; |
| 469 | } |
| 470 | } |
| 471 | return done; |
| 472 | } |
| 473 | |
| 474 | static int qio_channel_tls_set_blocking(QIOChannel *ioc, |
| 475 | bool enabled, |
| 476 | Error **errp) |
| 477 | { |
| 478 | QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc); |
| 479 | |
| 480 | return qio_channel_set_blocking(tioc->master, enabled, errp) ? 0 : -1; |
| 481 | } |
| 482 | |
| 483 | static void qio_channel_tls_set_delay(QIOChannel *ioc, |
| 484 | bool enabled) |
| 485 | { |
| 486 | QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc); |
| 487 | |
| 488 | qio_channel_set_delay(tioc->master, enabled); |
| 489 | } |
| 490 | |
| 491 | static void qio_channel_tls_set_cork(QIOChannel *ioc, |
| 492 | bool enabled) |
| 493 | { |
| 494 | QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc); |
| 495 | |
| 496 | qio_channel_set_cork(tioc->master, enabled); |
| 497 | } |
| 498 | |
| 499 | static int qio_channel_tls_shutdown(QIOChannel *ioc, |
| 500 | QIOChannelShutdown how, |
| 501 | Error **errp) |
| 502 | { |
| 503 | QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc); |
| 504 | |
| 505 | qatomic_or(&tioc->shutdown, how); |
| 506 | |
| 507 | return qio_channel_shutdown(tioc->master, how, errp); |
| 508 | } |
| 509 | |
| 510 | static int qio_channel_tls_close(QIOChannel *ioc, |
| 511 | Error **errp) |
| 512 | { |
| 513 | QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc); |
| 514 | |
| 515 | if (tioc->hs_ioc_tag) { |
| 516 | trace_qio_channel_tls_handshake_cancel(ioc); |
| 517 | g_clear_handle_id(&tioc->hs_ioc_tag, g_source_remove); |
| 518 | } |
| 519 | |
| 520 | if (tioc->bye_ioc_tag) { |
| 521 | trace_qio_channel_tls_bye_cancel(ioc); |
| 522 | g_clear_handle_id(&tioc->bye_ioc_tag, g_source_remove); |
| 523 | } |
| 524 | |
| 525 | return qio_channel_close(tioc->master, errp); |
| 526 | } |
| 527 | |
| 528 | static void qio_channel_tls_set_aio_fd_handler(QIOChannel *ioc, |
| 529 | AioContext *read_ctx, |
| 530 | IOHandler *io_read, |
| 531 | AioContext *write_ctx, |
| 532 | IOHandler *io_write, |
| 533 | void *opaque) |
| 534 | { |
| 535 | QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc); |
| 536 | |
| 537 | qio_channel_set_aio_fd_handler(tioc->master, read_ctx, io_read, |
| 538 | write_ctx, io_write, opaque); |
| 539 | } |
| 540 | |
| 541 | typedef struct QIOChannelTLSSource QIOChannelTLSSource; |
| 542 | struct QIOChannelTLSSource { |
| 543 | GSource parent; |
| 544 | QIOChannelTLS *tioc; |
| 545 | }; |
| 546 | |
| 547 | static gboolean |
| 548 | qio_channel_tls_source_check(GSource *source) |
| 549 | { |
| 550 | QIOChannelTLSSource *tsource = (QIOChannelTLSSource *)source; |
| 551 | |
| 552 | return qcrypto_tls_session_check_pending(tsource->tioc->session) > 0; |
| 553 | } |
| 554 | |
| 555 | static gboolean |
| 556 | qio_channel_tls_source_prepare(GSource *source, gint *timeout) |
| 557 | { |
| 558 | *timeout = -1; |
| 559 | return qio_channel_tls_source_check(source); |
| 560 | } |
| 561 | |
| 562 | static gboolean |
| 563 | qio_channel_tls_source_dispatch(GSource *source, GSourceFunc callback, |
| 564 | gpointer user_data) |
| 565 | { |
| 566 | return G_SOURCE_CONTINUE; |
| 567 | } |
| 568 | |
| 569 | static void |
| 570 | qio_channel_tls_source_finalize(GSource *source) |
| 571 | { |
| 572 | QIOChannelTLSSource *tsource = (QIOChannelTLSSource *)source; |
| 573 | |
| 574 | object_unref(OBJECT(tsource->tioc)); |
| 575 | } |
| 576 | |
| 577 | static GSourceFuncs qio_channel_tls_source_funcs = { |
| 578 | qio_channel_tls_source_prepare, |
| 579 | qio_channel_tls_source_check, |
| 580 | qio_channel_tls_source_dispatch, |
| 581 | qio_channel_tls_source_finalize |
| 582 | }; |
| 583 | |
| 584 | static void |
| 585 | qio_channel_tls_read_watch(QIOChannelTLS *tioc, GSource *source) |
| 586 | { |
| 587 | GSource *child; |
| 588 | QIOChannelTLSSource *tlssource; |
| 589 | |
| 590 | child = g_source_new(&qio_channel_tls_source_funcs, |
| 591 | sizeof(QIOChannelTLSSource)); |
| 592 | tlssource = (QIOChannelTLSSource *)child; |
| 593 | |
| 594 | tlssource->tioc = tioc; |
| 595 | object_ref(OBJECT(tioc)); |
| 596 | |
| 597 | g_source_add_child_source(source, child); |
| 598 | g_source_unref(child); |
| 599 | } |
| 600 | |
| 601 | static GSource *qio_channel_tls_create_watch(QIOChannel *ioc, |
| 602 | GIOCondition condition) |
| 603 | { |
| 604 | QIOChannelTLS *tioc = QIO_CHANNEL_TLS(ioc); |
| 605 | GSource *source = qio_channel_create_watch(tioc->master, condition); |
| 606 | |
| 607 | if (condition & G_IO_IN) { |
| 608 | qio_channel_tls_read_watch(tioc, source); |
| 609 | } |
| 610 | |
| 611 | return source; |
| 612 | } |
| 613 | |
| 614 | QCryptoTLSSession * |
| 615 | qio_channel_tls_get_session(QIOChannelTLS *ioc) |
| 616 | { |
| 617 | return ioc->session; |
| 618 | } |
| 619 | |
| 620 | static void qio_channel_tls_class_init(ObjectClass *klass, |
| 621 | const void *class_data G_GNUC_UNUSED) |
| 622 | { |
| 623 | QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass); |
| 624 | |
| 625 | ioc_klass->io_writev = qio_channel_tls_writev; |
| 626 | ioc_klass->io_readv = qio_channel_tls_readv; |
| 627 | ioc_klass->io_set_blocking = qio_channel_tls_set_blocking; |
| 628 | ioc_klass->io_set_delay = qio_channel_tls_set_delay; |
| 629 | ioc_klass->io_set_cork = qio_channel_tls_set_cork; |
| 630 | ioc_klass->io_close = qio_channel_tls_close; |
| 631 | ioc_klass->io_shutdown = qio_channel_tls_shutdown; |
| 632 | ioc_klass->io_create_watch = qio_channel_tls_create_watch; |
| 633 | ioc_klass->io_set_aio_fd_handler = qio_channel_tls_set_aio_fd_handler; |
| 634 | } |
| 635 | |
| 636 | static const TypeInfo qio_channel_tls_info = { |
| 637 | .parent = TYPE_QIO_CHANNEL, |
| 638 | .name = TYPE_QIO_CHANNEL_TLS, |
| 639 | .instance_size = sizeof(QIOChannelTLS), |
| 640 | .instance_init = qio_channel_tls_init, |
| 641 | .instance_finalize = qio_channel_tls_finalize, |
| 642 | .class_init = qio_channel_tls_class_init, |
| 643 | }; |
| 644 | |
| 645 | static void qio_channel_tls_register_types(void) |
| 646 | { |
| 647 | type_register_static(&qio_channel_tls_info); |
| 648 | } |
| 649 | |
| 650 | type_init(qio_channel_tls_register_types); |