vnc-ws.c 8.4 KB

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