vnc-ws.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. #include "vnc.h"
  21. #include "qemu/main-loop.h"
  22. #ifdef CONFIG_VNC_TLS
  23. #include "qemu/sockets.h"
  24. static void vncws_tls_handshake_io(void *opaque);
  25. static int vncws_start_tls_handshake(struct VncState *vs)
  26. {
  27. int ret = gnutls_handshake(vs->ws_tls.session);
  28. if (ret < 0) {
  29. if (!gnutls_error_is_fatal(ret)) {
  30. VNC_DEBUG("Handshake interrupted (blocking)\n");
  31. if (!gnutls_record_get_direction(vs->ws_tls.session)) {
  32. qemu_set_fd_handler(vs->csock, vncws_tls_handshake_io,
  33. NULL, vs);
  34. } else {
  35. qemu_set_fd_handler(vs->csock, NULL, vncws_tls_handshake_io,
  36. vs);
  37. }
  38. return 0;
  39. }
  40. VNC_DEBUG("Handshake failed %s\n", gnutls_strerror(ret));
  41. vnc_client_error(vs);
  42. return -1;
  43. }
  44. VNC_DEBUG("Handshake done, switching to TLS data mode\n");
  45. vs->ws_tls.wiremode = VNC_WIREMODE_TLS;
  46. qemu_set_fd_handler2(vs->csock, NULL, vncws_handshake_read, NULL, vs);
  47. return 0;
  48. }
  49. static void vncws_tls_handshake_io(void *opaque)
  50. {
  51. struct VncState *vs = (struct VncState *)opaque;
  52. VNC_DEBUG("Handshake IO continue\n");
  53. vncws_start_tls_handshake(vs);
  54. }
  55. void vncws_tls_handshake_peek(void *opaque)
  56. {
  57. VncState *vs = opaque;
  58. long ret;
  59. if (!vs->ws_tls.session) {
  60. char peek[4];
  61. ret = qemu_recv(vs->csock, peek, sizeof(peek), MSG_PEEK);
  62. if (ret && (strncmp(peek, "\x16", 1) == 0
  63. || strncmp(peek, "\x80", 1) == 0)) {
  64. VNC_DEBUG("TLS Websocket connection recognized");
  65. vnc_tls_client_setup(vs, 1);
  66. vncws_start_tls_handshake(vs);
  67. } else {
  68. vncws_handshake_read(vs);
  69. }
  70. } else {
  71. qemu_set_fd_handler2(vs->csock, NULL, vncws_handshake_read, NULL, vs);
  72. }
  73. }
  74. #endif /* CONFIG_VNC_TLS */
  75. void vncws_handshake_read(void *opaque)
  76. {
  77. VncState *vs = opaque;
  78. uint8_t *handshake_end;
  79. long ret;
  80. buffer_reserve(&vs->ws_input, 4096);
  81. ret = vnc_client_read_buf(vs, buffer_end(&vs->ws_input), 4096);
  82. if (!ret) {
  83. if (vs->csock == -1) {
  84. vnc_disconnect_finish(vs);
  85. }
  86. return;
  87. }
  88. vs->ws_input.offset += ret;
  89. handshake_end = (uint8_t *)g_strstr_len((char *)vs->ws_input.buffer,
  90. vs->ws_input.offset, WS_HANDSHAKE_END);
  91. if (handshake_end) {
  92. qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
  93. vncws_process_handshake(vs, vs->ws_input.buffer, vs->ws_input.offset);
  94. buffer_advance(&vs->ws_input, handshake_end - vs->ws_input.buffer +
  95. strlen(WS_HANDSHAKE_END));
  96. }
  97. }
  98. long vnc_client_read_ws(VncState *vs)
  99. {
  100. int ret, err;
  101. uint8_t *payload;
  102. size_t payload_size, frame_size;
  103. VNC_DEBUG("Read websocket %p size %zd offset %zd\n", vs->ws_input.buffer,
  104. vs->ws_input.capacity, vs->ws_input.offset);
  105. buffer_reserve(&vs->ws_input, 4096);
  106. ret = vnc_client_read_buf(vs, buffer_end(&vs->ws_input), 4096);
  107. if (!ret) {
  108. return 0;
  109. }
  110. vs->ws_input.offset += ret;
  111. /* make sure that nothing is left in the ws_input buffer */
  112. do {
  113. err = vncws_decode_frame(&vs->ws_input, &payload,
  114. &payload_size, &frame_size);
  115. if (err <= 0) {
  116. return err;
  117. }
  118. buffer_reserve(&vs->input, payload_size);
  119. buffer_append(&vs->input, payload, payload_size);
  120. buffer_advance(&vs->ws_input, frame_size);
  121. } while (vs->ws_input.offset > 0);
  122. return ret;
  123. }
  124. long vnc_client_write_ws(VncState *vs)
  125. {
  126. long ret;
  127. VNC_DEBUG("Write WS: Pending output %p size %zd offset %zd\n",
  128. vs->output.buffer, vs->output.capacity, vs->output.offset);
  129. vncws_encode_frame(&vs->ws_output, vs->output.buffer, vs->output.offset);
  130. buffer_reset(&vs->output);
  131. ret = vnc_client_write_buf(vs, vs->ws_output.buffer, vs->ws_output.offset);
  132. if (!ret) {
  133. return 0;
  134. }
  135. buffer_advance(&vs->ws_output, ret);
  136. if (vs->ws_output.offset == 0) {
  137. qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, NULL, vs);
  138. }
  139. return ret;
  140. }
  141. static char *vncws_extract_handshake_entry(const char *handshake,
  142. size_t handshake_len, const char *name)
  143. {
  144. char *begin, *end, *ret = NULL;
  145. char *line = g_strdup_printf("%s%s: ", WS_HANDSHAKE_DELIM, name);
  146. begin = g_strstr_len(handshake, handshake_len, line);
  147. if (begin != NULL) {
  148. begin += strlen(line);
  149. end = g_strstr_len(begin, handshake_len - (begin - handshake),
  150. WS_HANDSHAKE_DELIM);
  151. if (end != NULL) {
  152. ret = g_strndup(begin, end - begin);
  153. }
  154. }
  155. g_free(line);
  156. return ret;
  157. }
  158. static void vncws_send_handshake_response(VncState *vs, const char* key)
  159. {
  160. char combined_key[WS_CLIENT_KEY_LEN + WS_GUID_LEN + 1];
  161. unsigned char hash[SHA1_DIGEST_LEN];
  162. size_t hash_size = sizeof(hash);
  163. char *accept = NULL, *response = NULL;
  164. gnutls_datum_t in;
  165. int ret;
  166. g_strlcpy(combined_key, key, WS_CLIENT_KEY_LEN + 1);
  167. g_strlcat(combined_key, WS_GUID, WS_CLIENT_KEY_LEN + WS_GUID_LEN + 1);
  168. /* hash and encode it */
  169. in.data = (void *)combined_key;
  170. in.size = WS_CLIENT_KEY_LEN + WS_GUID_LEN;
  171. ret = gnutls_fingerprint(GNUTLS_DIG_SHA1, &in, hash, &hash_size);
  172. if (ret == GNUTLS_E_SUCCESS && hash_size <= SHA1_DIGEST_LEN) {
  173. accept = g_base64_encode(hash, hash_size);
  174. }
  175. if (accept == NULL) {
  176. VNC_DEBUG("Hashing Websocket combined key failed\n");
  177. vnc_client_error(vs);
  178. return;
  179. }
  180. response = g_strdup_printf(WS_HANDSHAKE, accept);
  181. vnc_write(vs, response, strlen(response));
  182. vnc_flush(vs);
  183. g_free(accept);
  184. g_free(response);
  185. vs->encode_ws = 1;
  186. vnc_init_state(vs);
  187. }
  188. void vncws_process_handshake(VncState *vs, uint8_t *line, size_t size)
  189. {
  190. char *protocols = vncws_extract_handshake_entry((const char *)line, size,
  191. "Sec-WebSocket-Protocol");
  192. char *version = vncws_extract_handshake_entry((const char *)line, size,
  193. "Sec-WebSocket-Version");
  194. char *key = vncws_extract_handshake_entry((const char *)line, size,
  195. "Sec-WebSocket-Key");
  196. if (protocols && version && key
  197. && g_strrstr(protocols, "binary")
  198. && !strcmp(version, WS_SUPPORTED_VERSION)
  199. && strlen(key) == WS_CLIENT_KEY_LEN) {
  200. vncws_send_handshake_response(vs, key);
  201. } else {
  202. VNC_DEBUG("Defective Websockets header or unsupported protocol\n");
  203. vnc_client_error(vs);
  204. }
  205. g_free(protocols);
  206. g_free(version);
  207. g_free(key);
  208. }
  209. void vncws_encode_frame(Buffer *output, const void *payload,
  210. const size_t payload_size)
  211. {
  212. size_t header_size = 0;
  213. unsigned char opcode = WS_OPCODE_BINARY_FRAME;
  214. union {
  215. char buf[WS_HEAD_MAX_LEN];
  216. WsHeader ws;
  217. } header;
  218. if (!payload_size) {
  219. return;
  220. }
  221. header.ws.b0 = 0x80 | (opcode & 0x0f);
  222. if (payload_size <= 125) {
  223. header.ws.b1 = (uint8_t)payload_size;
  224. header_size = 2;
  225. } else if (payload_size < 65536) {
  226. header.ws.b1 = 0x7e;
  227. header.ws.u.s16.l16 = cpu_to_be16((uint16_t)payload_size);
  228. header_size = 4;
  229. } else {
  230. header.ws.b1 = 0x7f;
  231. header.ws.u.s64.l64 = cpu_to_be64(payload_size);
  232. header_size = 10;
  233. }
  234. buffer_reserve(output, header_size + payload_size);
  235. buffer_append(output, header.buf, header_size);
  236. buffer_append(output, payload, payload_size);
  237. }
  238. int vncws_decode_frame(Buffer *input, uint8_t **payload,
  239. size_t *payload_size, size_t *frame_size)
  240. {
  241. unsigned char opcode = 0, fin = 0, has_mask = 0;
  242. size_t header_size = 0;
  243. uint32_t *payload32;
  244. WsHeader *header = (WsHeader *)input->buffer;
  245. WsMask mask;
  246. int i;
  247. if (input->offset < WS_HEAD_MIN_LEN + 4) {
  248. /* header not complete */
  249. return 0;
  250. }
  251. fin = (header->b0 & 0x80) >> 7;
  252. opcode = header->b0 & 0x0f;
  253. has_mask = (header->b1 & 0x80) >> 7;
  254. *payload_size = header->b1 & 0x7f;
  255. if (opcode == WS_OPCODE_CLOSE) {
  256. /* disconnect */
  257. return -1;
  258. }
  259. /* Websocket frame sanity check:
  260. * * Websocket fragmentation is not supported.
  261. * * All websockets frames sent by a client have to be masked.
  262. * * Only binary encoding is supported.
  263. */
  264. if (!fin || !has_mask || opcode != WS_OPCODE_BINARY_FRAME) {
  265. VNC_DEBUG("Received faulty/unsupported Websocket frame\n");
  266. return -2;
  267. }
  268. if (*payload_size < 126) {
  269. header_size = 6;
  270. mask = header->u.m;
  271. } else if (*payload_size == 126 && input->offset >= 8) {
  272. *payload_size = be16_to_cpu(header->u.s16.l16);
  273. header_size = 8;
  274. mask = header->u.s16.m16;
  275. } else if (*payload_size == 127 && input->offset >= 14) {
  276. *payload_size = be64_to_cpu(header->u.s64.l64);
  277. header_size = 14;
  278. mask = header->u.s64.m64;
  279. } else {
  280. /* header not complete */
  281. return 0;
  282. }
  283. *frame_size = header_size + *payload_size;
  284. if (input->offset < *frame_size) {
  285. /* frame not complete */
  286. return 0;
  287. }
  288. *payload = input->buffer + header_size;
  289. /* unmask frame */
  290. /* process 1 frame (32 bit op) */
  291. payload32 = (uint32_t *)(*payload);
  292. for (i = 0; i < *payload_size / 4; i++) {
  293. payload32[i] ^= mask.u;
  294. }
  295. /* process the remaining bytes (if any) */
  296. for (i *= 4; i < *payload_size; i++) {
  297. (*payload)[i] ^= mask.c[i % 4];
  298. }
  299. return 1;
  300. }