Tim Hardeck | 7536ee4 | 2013-01-21 11:04:44 +0100 | [diff] [blame] | 1 | /* |
| 2 | * QEMU VNC display driver: Websockets support |
| 3 | * |
| 4 | * Copyright (C) 2010 Joel Martin |
| 5 | * Copyright (C) 2012 Tim Hardeck |
| 6 | * |
| 7 | * This is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This software is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this software; if not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
Peter Maydell | e16f4c8 | 2016-01-29 17:49:51 +0000 | [diff] [blame] | 21 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 22 | #include "qapi/error.h" |
Tim Hardeck | 7536ee4 | 2013-01-21 11:04:44 +0100 | [diff] [blame] | 23 | #include "vnc.h" |
Daniel P. Berrange | d5f0422 | 2015-03-11 15:53:49 +0000 | [diff] [blame] | 24 | #include "io/channel-websock.h" |
Paolo Bonzini | 58369e2 | 2016-03-15 17:22:36 +0100 | [diff] [blame] | 25 | #include "qemu/bswap.h" |
Daniel P. Berrange | ad6374c | 2017-09-21 13:15:27 +0100 | [diff] [blame] | 26 | #include "trace.h" |
Daniel P. Berrange | 2cc4522 | 2015-03-02 19:01:05 +0000 | [diff] [blame] | 27 | |
Daniel P. Berrange | 60e705c | 2016-08-11 15:20:58 +0100 | [diff] [blame] | 28 | static void vncws_tls_handshake_done(QIOTask *task, |
Daniel P. Berrange | 2cc4522 | 2015-03-02 19:01:05 +0000 | [diff] [blame] | 29 | gpointer user_data) |
Tim Hardeck | 0057a0d | 2013-04-23 16:33:01 +0200 | [diff] [blame] | 30 | { |
Daniel P. Berrange | 2cc4522 | 2015-03-02 19:01:05 +0000 | [diff] [blame] | 31 | VncState *vs = user_data; |
Daniel P. Berrange | 60e705c | 2016-08-11 15:20:58 +0100 | [diff] [blame] | 32 | Error *err = NULL; |
Tim Hardeck | 0057a0d | 2013-04-23 16:33:01 +0200 | [diff] [blame] | 33 | |
Daniel P. Berrange | 60e705c | 2016-08-11 15:20:58 +0100 | [diff] [blame] | 34 | if (qio_task_propagate_error(task, &err)) { |
Daniel P. Berrange | 2cc4522 | 2015-03-02 19:01:05 +0000 | [diff] [blame] | 35 | VNC_DEBUG("Handshake failed %s\n", error_get_pretty(err)); |
| 36 | vnc_client_error(vs); |
Daniel P. Berrange | 60e705c | 2016-08-11 15:20:58 +0100 | [diff] [blame] | 37 | error_free(err); |
Daniel P. Berrange | 2cc4522 | 2015-03-02 19:01:05 +0000 | [diff] [blame] | 38 | } else { |
Daniel P. Berrange | d5f0422 | 2015-03-11 15:53:49 +0000 | [diff] [blame] | 39 | VNC_DEBUG("TLS handshake complete, starting websocket handshake\n"); |
Brandon Carpenter | a75d6f0 | 2017-09-12 08:21:47 -0700 | [diff] [blame] | 40 | if (vs->ioc_tag) { |
| 41 | g_source_remove(vs->ioc_tag); |
| 42 | } |
Philippe Mathieu-Daudé | 7d5b0d6 | 2023-06-01 11:34:52 +0200 | [diff] [blame] | 43 | vs->ioc_tag = qio_channel_add_watch(vs->ioc, |
| 44 | G_IO_IN | G_IO_HUP | G_IO_ERR, |
| 45 | vncws_handshake_io, vs, NULL); |
Tim Hardeck | 0057a0d | 2013-04-23 16:33:01 +0200 | [diff] [blame] | 46 | } |
Tim Hardeck | 0057a0d | 2013-04-23 16:33:01 +0200 | [diff] [blame] | 47 | } |
| 48 | |
Daniel P. Berrange | 2cc4522 | 2015-03-02 19:01:05 +0000 | [diff] [blame] | 49 | |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 50 | gboolean vncws_tls_handshake_io(QIOChannel *ioc G_GNUC_UNUSED, |
Ding Hui | 2ddafce | 2020-10-29 11:22:41 +0800 | [diff] [blame] | 51 | GIOCondition condition, |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 52 | void *opaque) |
Tim Hardeck | 0057a0d | 2013-04-23 16:33:01 +0200 | [diff] [blame] | 53 | { |
Daniel P. Berrange | 2cc4522 | 2015-03-02 19:01:05 +0000 | [diff] [blame] | 54 | VncState *vs = opaque; |
| 55 | QIOChannelTLS *tls; |
Daniel P. Berrange | 3e305e4 | 2015-08-06 14:39:32 +0100 | [diff] [blame] | 56 | Error *err = NULL; |
Tim Hardeck | 0057a0d | 2013-04-23 16:33:01 +0200 | [diff] [blame] | 57 | |
Daniel P. Berrange | 2cc4522 | 2015-03-02 19:01:05 +0000 | [diff] [blame] | 58 | if (vs->ioc_tag) { |
| 59 | g_source_remove(vs->ioc_tag); |
| 60 | vs->ioc_tag = 0; |
| 61 | } |
| 62 | |
Ding Hui | 2ddafce | 2020-10-29 11:22:41 +0800 | [diff] [blame] | 63 | if (condition & (G_IO_HUP | G_IO_ERR)) { |
| 64 | vnc_client_error(vs); |
| 65 | return TRUE; |
| 66 | } |
| 67 | |
Daniel P. Berrange | 2cc4522 | 2015-03-02 19:01:05 +0000 | [diff] [blame] | 68 | tls = qio_channel_tls_new_server( |
| 69 | vs->ioc, |
| 70 | vs->vd->tlscreds, |
Daniel P. Berrange | b76806d | 2016-02-18 18:40:24 +0000 | [diff] [blame] | 71 | vs->vd->tlsauthzid, |
Daniel P. Berrange | 2cc4522 | 2015-03-02 19:01:05 +0000 | [diff] [blame] | 72 | &err); |
| 73 | if (!tls) { |
| 74 | VNC_DEBUG("Failed to setup TLS %s\n", error_get_pretty(err)); |
Daniel P. Berrange | 3e305e4 | 2015-08-06 14:39:32 +0100 | [diff] [blame] | 75 | error_free(err); |
| 76 | vnc_client_error(vs); |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 77 | return TRUE; |
Daniel P. Berrange | 51941e4 | 2015-03-17 13:42:58 +0000 | [diff] [blame] | 78 | } |
Daniel P. Berrange | 3e305e4 | 2015-08-06 14:39:32 +0100 | [diff] [blame] | 79 | |
Daniel P. Berrange | 10bcfe5 | 2016-09-30 11:57:14 +0100 | [diff] [blame] | 80 | qio_channel_set_name(QIO_CHANNEL(tls), "vnc-ws-server-tls"); |
| 81 | |
Daniel P. Berrange | 2cc4522 | 2015-03-02 19:01:05 +0000 | [diff] [blame] | 82 | object_unref(OBJECT(vs->ioc)); |
| 83 | vs->ioc = QIO_CHANNEL(tls); |
Daniel P. Berrange | ad6374c | 2017-09-21 13:15:27 +0100 | [diff] [blame] | 84 | trace_vnc_client_io_wrap(vs, vs->ioc, "tls"); |
Daniel P. Berrange | 2cc4522 | 2015-03-02 19:01:05 +0000 | [diff] [blame] | 85 | vs->tls = qio_channel_tls_get_session(tls); |
| 86 | |
| 87 | qio_channel_tls_handshake(tls, |
| 88 | vncws_tls_handshake_done, |
| 89 | vs, |
Peter Xu | 1939ccd | 2018-03-05 14:43:24 +0800 | [diff] [blame] | 90 | NULL, |
Daniel P. Berrange | 2cc4522 | 2015-03-02 19:01:05 +0000 | [diff] [blame] | 91 | NULL); |
| 92 | |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 93 | return TRUE; |
Tim Hardeck | 0057a0d | 2013-04-23 16:33:01 +0200 | [diff] [blame] | 94 | } |
Tim Hardeck | 0057a0d | 2013-04-23 16:33:01 +0200 | [diff] [blame] | 95 | |
Daniel P. Berrange | d5f0422 | 2015-03-11 15:53:49 +0000 | [diff] [blame] | 96 | |
Daniel P. Berrange | 60e705c | 2016-08-11 15:20:58 +0100 | [diff] [blame] | 97 | static void vncws_handshake_done(QIOTask *task, |
Daniel P. Berrange | d5f0422 | 2015-03-11 15:53:49 +0000 | [diff] [blame] | 98 | gpointer user_data) |
Tim Hardeck | 7536ee4 | 2013-01-21 11:04:44 +0100 | [diff] [blame] | 99 | { |
Daniel P. Berrange | d5f0422 | 2015-03-11 15:53:49 +0000 | [diff] [blame] | 100 | VncState *vs = user_data; |
Daniel P. Berrange | 60e705c | 2016-08-11 15:20:58 +0100 | [diff] [blame] | 101 | Error *err = NULL; |
Tim Hardeck | 7536ee4 | 2013-01-21 11:04:44 +0100 | [diff] [blame] | 102 | |
Daniel P. Berrange | 60e705c | 2016-08-11 15:20:58 +0100 | [diff] [blame] | 103 | if (qio_task_propagate_error(task, &err)) { |
Daniel P. Berrange | d5f0422 | 2015-03-11 15:53:49 +0000 | [diff] [blame] | 104 | VNC_DEBUG("Websock handshake failed %s\n", error_get_pretty(err)); |
| 105 | vnc_client_error(vs); |
Daniel P. Berrange | 60e705c | 2016-08-11 15:20:58 +0100 | [diff] [blame] | 106 | error_free(err); |
Daniel P. Berrange | d5f0422 | 2015-03-11 15:53:49 +0000 | [diff] [blame] | 107 | } else { |
| 108 | VNC_DEBUG("Websock handshake complete, starting VNC protocol\n"); |
Daniel P. Berrange | dbee989 | 2016-09-29 16:45:40 +0100 | [diff] [blame] | 109 | vnc_start_protocol(vs); |
Brandon Carpenter | a75d6f0 | 2017-09-12 08:21:47 -0700 | [diff] [blame] | 110 | if (vs->ioc_tag) { |
| 111 | g_source_remove(vs->ioc_tag); |
| 112 | } |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 113 | vs->ioc_tag = qio_channel_add_watch( |
Ding Hui | 2ddafce | 2020-10-29 11:22:41 +0800 | [diff] [blame] | 114 | vs->ioc, G_IO_IN | G_IO_HUP | G_IO_ERR, |
| 115 | vnc_client_io, vs, NULL); |
Tim Hardeck | 7536ee4 | 2013-01-21 11:04:44 +0100 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
| 119 | |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 120 | gboolean vncws_handshake_io(QIOChannel *ioc G_GNUC_UNUSED, |
Ding Hui | 2ddafce | 2020-10-29 11:22:41 +0800 | [diff] [blame] | 121 | GIOCondition condition, |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 122 | void *opaque) |
| 123 | { |
| 124 | VncState *vs = opaque; |
Daniel P. Berrange | d5f0422 | 2015-03-11 15:53:49 +0000 | [diff] [blame] | 125 | QIOChannelWebsock *wioc; |
| 126 | |
Daniel P. Berrange | d5f0422 | 2015-03-11 15:53:49 +0000 | [diff] [blame] | 127 | if (vs->ioc_tag) { |
| 128 | g_source_remove(vs->ioc_tag); |
| 129 | vs->ioc_tag = 0; |
| 130 | } |
| 131 | |
Ding Hui | 2ddafce | 2020-10-29 11:22:41 +0800 | [diff] [blame] | 132 | if (condition & (G_IO_HUP | G_IO_ERR)) { |
| 133 | vnc_client_error(vs); |
| 134 | return TRUE; |
| 135 | } |
| 136 | |
Daniel P. Berrange | d5f0422 | 2015-03-11 15:53:49 +0000 | [diff] [blame] | 137 | wioc = qio_channel_websock_new_server(vs->ioc); |
Daniel P. Berrange | 10bcfe5 | 2016-09-30 11:57:14 +0100 | [diff] [blame] | 138 | qio_channel_set_name(QIO_CHANNEL(wioc), "vnc-ws-server-websock"); |
Daniel P. Berrange | d5f0422 | 2015-03-11 15:53:49 +0000 | [diff] [blame] | 139 | |
| 140 | object_unref(OBJECT(vs->ioc)); |
| 141 | vs->ioc = QIO_CHANNEL(wioc); |
Daniel P. Berrange | ad6374c | 2017-09-21 13:15:27 +0100 | [diff] [blame] | 142 | trace_vnc_client_io_wrap(vs, vs->ioc, "websock"); |
Daniel P. Berrange | d5f0422 | 2015-03-11 15:53:49 +0000 | [diff] [blame] | 143 | |
| 144 | qio_channel_websock_handshake(wioc, |
| 145 | vncws_handshake_done, |
| 146 | vs, |
| 147 | NULL); |
| 148 | |
Daniel P. Berrange | 04d2529 | 2015-02-27 16:20:57 +0000 | [diff] [blame] | 149 | return TRUE; |
| 150 | } |