2 * QEMU VNC display driver
4 * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
5 * Copyright (C) 2006 Fabrice Bellard
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 #include "qemu_socket.h"
29 #define VNC_REFRESH_INTERVAL (1000 / 30)
31 #include "vnc_keysym.h"
36 #include <gnutls/gnutls.h>
37 #include <gnutls/x509.h>
38 #endif /* CONFIG_VNC_TLS */
40 // #define _VNC_DEBUG 1
43 #define VNC_DEBUG(fmt, ...) do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
45 #if CONFIG_VNC_TLS && _VNC_DEBUG >= 2
46 /* Very verbose, so only enabled for _VNC_DEBUG >= 2 */
47 static void vnc_debug_gnutls_log(int level
, const char* str
) {
48 VNC_DEBUG("%d %s", level
, str
);
50 #endif /* CONFIG_VNC_TLS && _VNC_DEBUG */
52 #define VNC_DEBUG(fmt, ...) do { } while (0)
63 typedef struct VncState VncState
;
65 typedef int VncReadEvent(VncState
*vs
, char *data
, size_t len
);
67 typedef void VncWritePixels(VncState
*vs
, void *data
, int size
);
69 typedef void VncSendHextileTile(VncState
*vs
,
70 int x
, int y
, int w
, int h
,
73 int *has_bg
, int *has_fg
);
75 #define VNC_MAX_WIDTH 2048
76 #define VNC_MAX_HEIGHT 2048
77 #define VNC_DIRTY_WORDS (VNC_MAX_WIDTH / (16 * 32))
79 #define VNC_AUTH_CHALLENGE_SIZE 16
90 VNC_AUTH_VENCRYPT
= 19
100 VNC_AUTH_VENCRYPT_PLAIN
= 256,
101 VNC_AUTH_VENCRYPT_TLSNONE
= 257,
102 VNC_AUTH_VENCRYPT_TLSVNC
= 258,
103 VNC_AUTH_VENCRYPT_TLSPLAIN
= 259,
104 VNC_AUTH_VENCRYPT_X509NONE
= 260,
105 VNC_AUTH_VENCRYPT_X509VNC
= 261,
106 VNC_AUTH_VENCRYPT_X509PLAIN
= 262,
110 #define X509_CA_CERT_FILE "ca-cert.pem"
111 #define X509_CA_CRL_FILE "ca-crl.pem"
112 #define X509_SERVER_KEY_FILE "server-key.pem"
113 #define X509_SERVER_CERT_FILE "server-cert.pem"
116 #endif /* CONFIG_VNC_TLS */
127 uint32_t dirty_row
[VNC_MAX_HEIGHT
][VNC_DIRTY_WORDS
];
129 int depth
; /* internal VNC frame buffer byte per pixel */
132 int has_pointer_type_change
;
152 char challenge
[VNC_AUTH_CHALLENGE_SIZE
];
156 gnutls_session_t tls_session
;
161 kbd_layout_t
*kbd_layout
;
162 /* current output mode information */
163 VncWritePixels
*write_pixels
;
164 VncSendHextileTile
*send_hextile_tile
;
165 int pix_bpp
, pix_big_endian
;
166 int red_shift
, red_max
, red_shift1
;
167 int green_shift
, green_max
, green_shift1
;
168 int blue_shift
, blue_max
, blue_shift1
;
170 VncReadEvent
*read_handler
;
171 size_t read_handler_expect
;
173 uint8_t modifiers_state
[256];
176 static VncState
*vnc_state
; /* needed for info vnc */
178 void do_info_vnc(void)
180 if (vnc_state
== NULL
)
181 term_printf("VNC server disabled\n");
183 term_printf("VNC server active on: ");
184 term_print_filename(vnc_state
->display
);
187 if (vnc_state
->csock
== -1)
188 term_printf("No client connected\n");
190 term_printf("Client connected\n");
195 1) Get the queue working for IO.
196 2) there is some weirdness when using the -S option (the screen is grey
197 and not totally invalidated
198 3) resolutions > 1024
201 static void vnc_write(VncState
*vs
, const void *data
, size_t len
);
202 static void vnc_write_u32(VncState
*vs
, uint32_t value
);
203 static void vnc_write_s32(VncState
*vs
, int32_t value
);
204 static void vnc_write_u16(VncState
*vs
, uint16_t value
);
205 static void vnc_write_u8(VncState
*vs
, uint8_t value
);
206 static void vnc_flush(VncState
*vs
);
207 static void vnc_update_client(void *opaque
);
208 static void vnc_client_read(void *opaque
);
210 static inline void vnc_set_bit(uint32_t *d
, int k
)
212 d
[k
>> 5] |= 1 << (k
& 0x1f);
215 static inline void vnc_clear_bit(uint32_t *d
, int k
)
217 d
[k
>> 5] &= ~(1 << (k
& 0x1f));
220 static inline void vnc_set_bits(uint32_t *d
, int n
, int nb_words
)
230 d
[j
++] = (1 << n
) - 1;
235 static inline int vnc_get_bit(const uint32_t *d
, int k
)
237 return (d
[k
>> 5] >> (k
& 0x1f)) & 1;
240 static inline int vnc_and_bits(const uint32_t *d1
, const uint32_t *d2
,
244 for(i
= 0; i
< nb_words
; i
++) {
245 if ((d1
[i
] & d2
[i
]) != 0)
251 static void vnc_dpy_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
253 VncState
*vs
= ds
->opaque
;
259 for (i
= 0; i
< w
; i
+= 16)
260 vnc_set_bit(vs
->dirty_row
[y
], (x
+ i
) / 16);
263 static void vnc_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
,
266 vnc_write_u16(vs
, x
);
267 vnc_write_u16(vs
, y
);
268 vnc_write_u16(vs
, w
);
269 vnc_write_u16(vs
, h
);
271 vnc_write_s32(vs
, encoding
);
274 static void vnc_dpy_resize(DisplayState
*ds
, int w
, int h
)
277 VncState
*vs
= ds
->opaque
;
279 ds
->data
= realloc(ds
->data
, w
* h
* vs
->depth
);
280 vs
->old_data
= realloc(vs
->old_data
, w
* h
* vs
->depth
);
282 if (ds
->data
== NULL
|| vs
->old_data
== NULL
) {
283 fprintf(stderr
, "vnc: memory allocation failed\n");
287 ds
->depth
= vs
->depth
* 8;
288 size_changed
= ds
->width
!= w
|| ds
->height
!= h
;
291 ds
->linesize
= w
* vs
->depth
;
292 if (vs
->csock
!= -1 && vs
->has_resize
&& size_changed
) {
293 vnc_write_u8(vs
, 0); /* msg id */
295 vnc_write_u16(vs
, 1); /* number of rects */
296 vnc_framebuffer_update(vs
, 0, 0, ds
->width
, ds
->height
, -223);
298 vs
->width
= ds
->width
;
299 vs
->height
= ds
->height
;
304 static void vnc_write_pixels_copy(VncState
*vs
, void *pixels
, int size
)
306 vnc_write(vs
, pixels
, size
);
309 /* slowest but generic code. */
310 static void vnc_convert_pixel(VncState
*vs
, uint8_t *buf
, uint32_t v
)
312 unsigned int r
, g
, b
;
314 r
= (v
>> vs
->red_shift1
) & vs
->red_max
;
315 g
= (v
>> vs
->green_shift1
) & vs
->green_max
;
316 b
= (v
>> vs
->blue_shift1
) & vs
->blue_max
;
317 v
= (r
<< vs
->red_shift
) |
318 (g
<< vs
->green_shift
) |
319 (b
<< vs
->blue_shift
);
320 switch(vs
->pix_bpp
) {
325 if (vs
->pix_big_endian
) {
335 if (vs
->pix_big_endian
) {
350 static void vnc_write_pixels_generic(VncState
*vs
, void *pixels1
, int size
)
352 uint32_t *pixels
= pixels1
;
357 for(i
= 0; i
< n
; i
++) {
358 vnc_convert_pixel(vs
, buf
, pixels
[i
]);
359 vnc_write(vs
, buf
, vs
->pix_bpp
);
363 static void send_framebuffer_update_raw(VncState
*vs
, int x
, int y
, int w
, int h
)
368 vnc_framebuffer_update(vs
, x
, y
, w
, h
, 0);
370 row
= vs
->ds
->data
+ y
* vs
->ds
->linesize
+ x
* vs
->depth
;
371 for (i
= 0; i
< h
; i
++) {
372 vs
->write_pixels(vs
, row
, w
* vs
->depth
);
373 row
+= vs
->ds
->linesize
;
377 static void hextile_enc_cord(uint8_t *ptr
, int x
, int y
, int w
, int h
)
379 ptr
[0] = ((x
& 0x0F) << 4) | (y
& 0x0F);
380 ptr
[1] = (((w
- 1) & 0x0F) << 4) | ((h
- 1) & 0x0F);
384 #include "vnchextile.h"
388 #include "vnchextile.h"
392 #include "vnchextile.h"
397 #include "vnchextile.h"
401 static void send_framebuffer_update_hextile(VncState
*vs
, int x
, int y
, int w
, int h
)
405 uint32_t last_fg32
, last_bg32
;
407 vnc_framebuffer_update(vs
, x
, y
, w
, h
, 5);
410 for (j
= y
; j
< (y
+ h
); j
+= 16) {
411 for (i
= x
; i
< (x
+ w
); i
+= 16) {
412 vs
->send_hextile_tile(vs
, i
, j
,
413 MIN(16, x
+ w
- i
), MIN(16, y
+ h
- j
),
414 &last_bg32
, &last_fg32
, &has_bg
, &has_fg
);
419 static void send_framebuffer_update(VncState
*vs
, int x
, int y
, int w
, int h
)
422 send_framebuffer_update_hextile(vs
, x
, y
, w
, h
);
424 send_framebuffer_update_raw(vs
, x
, y
, w
, h
);
427 static void vnc_copy(DisplayState
*ds
, int src_x
, int src_y
, int dst_x
, int dst_y
, int w
, int h
)
434 int pitch
= ds
->linesize
;
435 VncState
*vs
= ds
->opaque
;
437 vnc_update_client(vs
);
444 src
= (ds
->linesize
* (src_y
+ y
) + vs
->depth
* src_x
);
445 dst
= (ds
->linesize
* (dst_y
+ y
) + vs
->depth
* dst_x
);
447 src_row
= ds
->data
+ src
;
448 dst_row
= ds
->data
+ dst
;
449 old_row
= vs
->old_data
+ dst
;
451 for (y
= 0; y
< h
; y
++) {
452 memmove(old_row
, src_row
, w
* vs
->depth
);
453 memmove(dst_row
, src_row
, w
* vs
->depth
);
459 vnc_write_u8(vs
, 0); /* msg id */
461 vnc_write_u16(vs
, 1); /* number of rects */
462 vnc_framebuffer_update(vs
, dst_x
, dst_y
, w
, h
, 1);
463 vnc_write_u16(vs
, src_x
);
464 vnc_write_u16(vs
, src_y
);
468 static int find_dirty_height(VncState
*vs
, int y
, int last_x
, int x
)
472 for (h
= 1; h
< (vs
->height
- y
); h
++) {
474 if (!vnc_get_bit(vs
->dirty_row
[y
+ h
], last_x
))
476 for (tmp_x
= last_x
; tmp_x
< x
; tmp_x
++)
477 vnc_clear_bit(vs
->dirty_row
[y
+ h
], tmp_x
);
483 static void vnc_update_client(void *opaque
)
485 VncState
*vs
= opaque
;
487 if (vs
->need_update
&& vs
->csock
!= -1) {
491 uint32_t width_mask
[VNC_DIRTY_WORDS
];
496 vnc_set_bits(width_mask
, (vs
->width
/ 16), VNC_DIRTY_WORDS
);
498 /* Walk through the dirty map and eliminate tiles that
499 really aren't dirty */
501 old_row
= vs
->old_data
;
503 for (y
= 0; y
< vs
->height
; y
++) {
504 if (vnc_and_bits(vs
->dirty_row
[y
], width_mask
, VNC_DIRTY_WORDS
)) {
511 for (x
= 0; x
< vs
->ds
->width
; x
+= 16) {
512 if (memcmp(old_ptr
, ptr
, 16 * vs
->depth
) == 0) {
513 vnc_clear_bit(vs
->dirty_row
[y
], (x
/ 16));
516 memcpy(old_ptr
, ptr
, 16 * vs
->depth
);
519 ptr
+= 16 * vs
->depth
;
520 old_ptr
+= 16 * vs
->depth
;
524 row
+= vs
->ds
->linesize
;
525 old_row
+= vs
->ds
->linesize
;
529 qemu_mod_timer(vs
->timer
, qemu_get_clock(rt_clock
) + VNC_REFRESH_INTERVAL
);
533 /* Count rectangles */
535 vnc_write_u8(vs
, 0); /* msg id */
537 saved_offset
= vs
->output
.offset
;
538 vnc_write_u16(vs
, 0);
540 for (y
= 0; y
< vs
->height
; y
++) {
543 for (x
= 0; x
< vs
->width
/ 16; x
++) {
544 if (vnc_get_bit(vs
->dirty_row
[y
], x
)) {
548 vnc_clear_bit(vs
->dirty_row
[y
], x
);
551 int h
= find_dirty_height(vs
, y
, last_x
, x
);
552 send_framebuffer_update(vs
, last_x
* 16, y
, (x
- last_x
) * 16, h
);
559 int h
= find_dirty_height(vs
, y
, last_x
, x
);
560 send_framebuffer_update(vs
, last_x
* 16, y
, (x
- last_x
) * 16, h
);
564 vs
->output
.buffer
[saved_offset
] = (n_rectangles
>> 8) & 0xFF;
565 vs
->output
.buffer
[saved_offset
+ 1] = n_rectangles
& 0xFF;
569 qemu_mod_timer(vs
->timer
, qemu_get_clock(rt_clock
) + VNC_REFRESH_INTERVAL
);
572 static void vnc_timer_init(VncState
*vs
)
574 if (vs
->timer
== NULL
) {
575 vs
->timer
= qemu_new_timer(rt_clock
, vnc_update_client
, vs
);
576 qemu_mod_timer(vs
->timer
, qemu_get_clock(rt_clock
));
580 static void vnc_dpy_refresh(DisplayState
*ds
)
582 VncState
*vs
= ds
->opaque
;
587 static int vnc_listen_poll(void *opaque
)
589 VncState
*vs
= opaque
;
595 static void buffer_reserve(Buffer
*buffer
, size_t len
)
597 if ((buffer
->capacity
- buffer
->offset
) < len
) {
598 buffer
->capacity
+= (len
+ 1024);
599 buffer
->buffer
= realloc(buffer
->buffer
, buffer
->capacity
);
600 if (buffer
->buffer
== NULL
) {
601 fprintf(stderr
, "vnc: out of memory\n");
607 static int buffer_empty(Buffer
*buffer
)
609 return buffer
->offset
== 0;
612 static char *buffer_end(Buffer
*buffer
)
614 return buffer
->buffer
+ buffer
->offset
;
617 static void buffer_reset(Buffer
*buffer
)
622 static void buffer_append(Buffer
*buffer
, const void *data
, size_t len
)
624 memcpy(buffer
->buffer
+ buffer
->offset
, data
, len
);
625 buffer
->offset
+= len
;
628 static int vnc_client_io_error(VncState
*vs
, int ret
, int last_errno
)
630 if (ret
== 0 || ret
== -1) {
631 if (ret
== -1 && (last_errno
== EINTR
|| last_errno
== EAGAIN
))
634 VNC_DEBUG("Closing down client sock %d %d\n", ret
, ret
< 0 ? last_errno
: 0);
635 qemu_set_fd_handler2(vs
->csock
, NULL
, NULL
, NULL
, NULL
);
636 closesocket(vs
->csock
);
638 buffer_reset(&vs
->input
);
639 buffer_reset(&vs
->output
);
642 if (vs
->tls_session
) {
643 gnutls_deinit(vs
->tls_session
);
644 vs
->tls_session
= NULL
;
646 vs
->wiremode
= VNC_WIREMODE_CLEAR
;
647 #endif /* CONFIG_VNC_TLS */
653 static void vnc_client_error(VncState
*vs
)
655 vnc_client_io_error(vs
, -1, EINVAL
);
658 static void vnc_client_write(void *opaque
)
661 VncState
*vs
= opaque
;
664 if (vs
->tls_session
) {
665 ret
= gnutls_write(vs
->tls_session
, vs
->output
.buffer
, vs
->output
.offset
);
667 if (ret
== GNUTLS_E_AGAIN
)
674 #endif /* CONFIG_VNC_TLS */
675 ret
= send(vs
->csock
, vs
->output
.buffer
, vs
->output
.offset
, 0);
676 ret
= vnc_client_io_error(vs
, ret
, socket_error());
680 memmove(vs
->output
.buffer
, vs
->output
.buffer
+ ret
, (vs
->output
.offset
- ret
));
681 vs
->output
.offset
-= ret
;
683 if (vs
->output
.offset
== 0) {
684 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, NULL
, vs
);
688 static void vnc_read_when(VncState
*vs
, VncReadEvent
*func
, size_t expecting
)
690 vs
->read_handler
= func
;
691 vs
->read_handler_expect
= expecting
;
694 static void vnc_client_read(void *opaque
)
696 VncState
*vs
= opaque
;
699 buffer_reserve(&vs
->input
, 4096);
702 if (vs
->tls_session
) {
703 ret
= gnutls_read(vs
->tls_session
, buffer_end(&vs
->input
), 4096);
705 if (ret
== GNUTLS_E_AGAIN
)
712 #endif /* CONFIG_VNC_TLS */
713 ret
= recv(vs
->csock
, buffer_end(&vs
->input
), 4096, 0);
714 ret
= vnc_client_io_error(vs
, ret
, socket_error());
718 vs
->input
.offset
+= ret
;
720 while (vs
->read_handler
&& vs
->input
.offset
>= vs
->read_handler_expect
) {
721 size_t len
= vs
->read_handler_expect
;
724 ret
= vs
->read_handler(vs
, vs
->input
.buffer
, len
);
729 memmove(vs
->input
.buffer
, vs
->input
.buffer
+ len
, (vs
->input
.offset
- len
));
730 vs
->input
.offset
-= len
;
732 vs
->read_handler_expect
= ret
;
737 static void vnc_write(VncState
*vs
, const void *data
, size_t len
)
739 buffer_reserve(&vs
->output
, len
);
741 if (buffer_empty(&vs
->output
)) {
742 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, vnc_client_write
, vs
);
745 buffer_append(&vs
->output
, data
, len
);
748 static void vnc_write_s32(VncState
*vs
, int32_t value
)
750 vnc_write_u32(vs
, *(uint32_t *)&value
);
753 static void vnc_write_u32(VncState
*vs
, uint32_t value
)
757 buf
[0] = (value
>> 24) & 0xFF;
758 buf
[1] = (value
>> 16) & 0xFF;
759 buf
[2] = (value
>> 8) & 0xFF;
760 buf
[3] = value
& 0xFF;
762 vnc_write(vs
, buf
, 4);
765 static void vnc_write_u16(VncState
*vs
, uint16_t value
)
769 buf
[0] = (value
>> 8) & 0xFF;
770 buf
[1] = value
& 0xFF;
772 vnc_write(vs
, buf
, 2);
775 static void vnc_write_u8(VncState
*vs
, uint8_t value
)
777 vnc_write(vs
, (char *)&value
, 1);
780 static void vnc_flush(VncState
*vs
)
782 if (vs
->output
.offset
)
783 vnc_client_write(vs
);
786 static uint8_t read_u8(uint8_t *data
, size_t offset
)
791 static uint16_t read_u16(uint8_t *data
, size_t offset
)
793 return ((data
[offset
] & 0xFF) << 8) | (data
[offset
+ 1] & 0xFF);
796 static int32_t read_s32(uint8_t *data
, size_t offset
)
798 return (int32_t)((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
799 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
802 static uint32_t read_u32(uint8_t *data
, size_t offset
)
804 return ((data
[offset
] << 24) | (data
[offset
+ 1] << 16) |
805 (data
[offset
+ 2] << 8) | data
[offset
+ 3]);
809 ssize_t
vnc_tls_push(gnutls_transport_ptr_t transport
,
812 struct VncState
*vs
= (struct VncState
*)transport
;
816 ret
= send(vs
->csock
, data
, len
, 0);
826 ssize_t
vnc_tls_pull(gnutls_transport_ptr_t transport
,
829 struct VncState
*vs
= (struct VncState
*)transport
;
833 ret
= recv(vs
->csock
, data
, len
, 0);
841 #endif /* CONFIG_VNC_TLS */
843 static void client_cut_text(VncState
*vs
, size_t len
, char *text
)
847 static void check_pointer_type_change(VncState
*vs
, int absolute
)
849 if (vs
->has_pointer_type_change
&& vs
->absolute
!= absolute
) {
852 vnc_write_u16(vs
, 1);
853 vnc_framebuffer_update(vs
, absolute
, 0,
854 vs
->ds
->width
, vs
->ds
->height
, -257);
857 vs
->absolute
= absolute
;
860 static void pointer_event(VncState
*vs
, int button_mask
, int x
, int y
)
865 if (button_mask
& 0x01)
866 buttons
|= MOUSE_EVENT_LBUTTON
;
867 if (button_mask
& 0x02)
868 buttons
|= MOUSE_EVENT_MBUTTON
;
869 if (button_mask
& 0x04)
870 buttons
|= MOUSE_EVENT_RBUTTON
;
871 if (button_mask
& 0x08)
873 if (button_mask
& 0x10)
877 kbd_mouse_event(x
* 0x7FFF / vs
->ds
->width
,
878 y
* 0x7FFF / vs
->ds
->height
,
880 } else if (vs
->has_pointer_type_change
) {
884 kbd_mouse_event(x
, y
, dz
, buttons
);
886 if (vs
->last_x
!= -1)
887 kbd_mouse_event(x
- vs
->last_x
,
894 check_pointer_type_change(vs
, kbd_mouse_is_absolute());
897 static void reset_keys(VncState
*vs
)
900 for(i
= 0; i
< 256; i
++) {
901 if (vs
->modifiers_state
[i
]) {
903 kbd_put_keycode(0xe0);
904 kbd_put_keycode(i
| 0x80);
905 vs
->modifiers_state
[i
] = 0;
910 static void do_key_event(VncState
*vs
, int down
, uint32_t sym
)
914 keycode
= keysym2scancode(vs
->kbd_layout
, sym
& 0xFFFF);
916 /* QEMU console switch */
918 case 0x2a: /* Left Shift */
919 case 0x36: /* Right Shift */
920 case 0x1d: /* Left CTRL */
921 case 0x9d: /* Right CTRL */
922 case 0x38: /* Left ALT */
923 case 0xb8: /* Right ALT */
925 vs
->modifiers_state
[keycode
] = 1;
927 vs
->modifiers_state
[keycode
] = 0;
929 case 0x02 ... 0x0a: /* '1' to '9' keys */
930 if (down
&& vs
->modifiers_state
[0x1d] && vs
->modifiers_state
[0x38]) {
931 /* Reset the modifiers sent to the current console */
933 console_select(keycode
- 0x02);
939 if (is_graphic_console()) {
941 kbd_put_keycode(0xe0);
943 kbd_put_keycode(keycode
& 0x7f);
945 kbd_put_keycode(keycode
| 0x80);
947 /* QEMU console emulation */
950 case 0x2a: /* Left Shift */
951 case 0x36: /* Right Shift */
952 case 0x1d: /* Left CTRL */
953 case 0x9d: /* Right CTRL */
954 case 0x38: /* Left ALT */
955 case 0xb8: /* Right ALT */
958 kbd_put_keysym(QEMU_KEY_UP
);
961 kbd_put_keysym(QEMU_KEY_DOWN
);
964 kbd_put_keysym(QEMU_KEY_LEFT
);
967 kbd_put_keysym(QEMU_KEY_RIGHT
);
970 kbd_put_keysym(QEMU_KEY_DELETE
);
973 kbd_put_keysym(QEMU_KEY_HOME
);
976 kbd_put_keysym(QEMU_KEY_END
);
979 kbd_put_keysym(QEMU_KEY_PAGEUP
);
982 kbd_put_keysym(QEMU_KEY_PAGEDOWN
);
992 static void key_event(VncState
*vs
, int down
, uint32_t sym
)
994 if (sym
>= 'A' && sym
<= 'Z')
995 sym
= sym
- 'A' + 'a';
996 do_key_event(vs
, down
, sym
);
999 static void framebuffer_update_request(VncState
*vs
, int incremental
,
1000 int x_position
, int y_position
,
1003 if (x_position
> vs
->ds
->width
)
1004 x_position
= vs
->ds
->width
;
1005 if (y_position
> vs
->ds
->height
)
1006 y_position
= vs
->ds
->height
;
1007 if (x_position
+ w
>= vs
->ds
->width
)
1008 w
= vs
->ds
->width
- x_position
;
1009 if (y_position
+ h
>= vs
->ds
->height
)
1010 h
= vs
->ds
->height
- y_position
;
1013 vs
->need_update
= 1;
1015 char *old_row
= vs
->old_data
+ y_position
* vs
->ds
->linesize
;
1017 for (i
= 0; i
< h
; i
++) {
1018 vnc_set_bits(vs
->dirty_row
[y_position
+ i
],
1019 (vs
->ds
->width
/ 16), VNC_DIRTY_WORDS
);
1020 memset(old_row
, 42, vs
->ds
->width
* vs
->depth
);
1021 old_row
+= vs
->ds
->linesize
;
1026 static void set_encodings(VncState
*vs
, int32_t *encodings
, size_t n_encodings
)
1030 vs
->has_hextile
= 0;
1032 vs
->has_pointer_type_change
= 0;
1034 vs
->ds
->dpy_copy
= NULL
;
1036 for (i
= n_encodings
- 1; i
>= 0; i
--) {
1037 switch (encodings
[i
]) {
1039 vs
->has_hextile
= 0;
1041 case 1: /* CopyRect */
1042 vs
->ds
->dpy_copy
= vnc_copy
;
1044 case 5: /* Hextile */
1045 vs
->has_hextile
= 1;
1047 case -223: /* DesktopResize */
1051 vs
->has_pointer_type_change
= 1;
1058 check_pointer_type_change(vs
, kbd_mouse_is_absolute());
1061 static int compute_nbits(unsigned int val
)
1072 static void set_pixel_format(VncState
*vs
,
1073 int bits_per_pixel
, int depth
,
1074 int big_endian_flag
, int true_color_flag
,
1075 int red_max
, int green_max
, int blue_max
,
1076 int red_shift
, int green_shift
, int blue_shift
)
1078 int host_big_endian_flag
;
1080 #ifdef WORDS_BIGENDIAN
1081 host_big_endian_flag
= 1;
1083 host_big_endian_flag
= 0;
1085 if (!true_color_flag
) {
1087 vnc_client_error(vs
);
1090 if (bits_per_pixel
== 32 &&
1091 host_big_endian_flag
== big_endian_flag
&&
1092 red_max
== 0xff && green_max
== 0xff && blue_max
== 0xff &&
1093 red_shift
== 16 && green_shift
== 8 && blue_shift
== 0) {
1095 vs
->write_pixels
= vnc_write_pixels_copy
;
1096 vs
->send_hextile_tile
= send_hextile_tile_32
;
1098 if (bits_per_pixel
== 16 &&
1099 host_big_endian_flag
== big_endian_flag
&&
1100 red_max
== 31 && green_max
== 63 && blue_max
== 31 &&
1101 red_shift
== 11 && green_shift
== 5 && blue_shift
== 0) {
1103 vs
->write_pixels
= vnc_write_pixels_copy
;
1104 vs
->send_hextile_tile
= send_hextile_tile_16
;
1106 if (bits_per_pixel
== 8 &&
1107 red_max
== 7 && green_max
== 7 && blue_max
== 3 &&
1108 red_shift
== 5 && green_shift
== 2 && blue_shift
== 0) {
1110 vs
->write_pixels
= vnc_write_pixels_copy
;
1111 vs
->send_hextile_tile
= send_hextile_tile_8
;
1114 /* generic and slower case */
1115 if (bits_per_pixel
!= 8 &&
1116 bits_per_pixel
!= 16 &&
1117 bits_per_pixel
!= 32)
1120 vs
->red_shift
= red_shift
;
1121 vs
->red_max
= red_max
;
1122 vs
->red_shift1
= 24 - compute_nbits(red_max
);
1123 vs
->green_shift
= green_shift
;
1124 vs
->green_max
= green_max
;
1125 vs
->green_shift1
= 16 - compute_nbits(green_max
);
1126 vs
->blue_shift
= blue_shift
;
1127 vs
->blue_max
= blue_max
;
1128 vs
->blue_shift1
= 8 - compute_nbits(blue_max
);
1129 vs
->pix_bpp
= bits_per_pixel
/ 8;
1130 vs
->pix_big_endian
= big_endian_flag
;
1131 vs
->write_pixels
= vnc_write_pixels_generic
;
1132 vs
->send_hextile_tile
= send_hextile_tile_generic
;
1135 vnc_dpy_resize(vs
->ds
, vs
->ds
->width
, vs
->ds
->height
);
1136 memset(vs
->dirty_row
, 0xFF, sizeof(vs
->dirty_row
));
1137 memset(vs
->old_data
, 42, vs
->ds
->linesize
* vs
->ds
->height
);
1139 vga_hw_invalidate();
1143 static int protocol_client_msg(VncState
*vs
, char *data
, size_t len
)
1153 set_pixel_format(vs
, read_u8(data
, 4), read_u8(data
, 5),
1154 read_u8(data
, 6), read_u8(data
, 7),
1155 read_u16(data
, 8), read_u16(data
, 10),
1156 read_u16(data
, 12), read_u8(data
, 14),
1157 read_u8(data
, 15), read_u8(data
, 16));
1164 return 4 + (read_u16(data
, 2) * 4);
1166 limit
= read_u16(data
, 2);
1167 for (i
= 0; i
< limit
; i
++) {
1168 int32_t val
= read_s32(data
, 4 + (i
* 4));
1169 memcpy(data
+ 4 + (i
* 4), &val
, sizeof(val
));
1172 set_encodings(vs
, (int32_t *)(data
+ 4), limit
);
1178 framebuffer_update_request(vs
,
1179 read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4),
1180 read_u16(data
, 6), read_u16(data
, 8));
1186 key_event(vs
, read_u8(data
, 1), read_u32(data
, 4));
1192 pointer_event(vs
, read_u8(data
, 1), read_u16(data
, 2), read_u16(data
, 4));
1199 uint32_t dlen
= read_u32(data
, 4);
1204 client_cut_text(vs
, read_u32(data
, 4), data
+ 8);
1207 printf("Msg: %d\n", data
[0]);
1208 vnc_client_error(vs
);
1212 vnc_read_when(vs
, protocol_client_msg
, 1);
1216 static int protocol_client_init(VncState
*vs
, char *data
, size_t len
)
1218 char pad
[3] = { 0, 0, 0 };
1221 unsigned char *prog
= "QEMU";
1223 vs
->width
= vs
->ds
->width
;
1224 vs
->height
= vs
->ds
->height
;
1225 vnc_write_u16(vs
, vs
->ds
->width
);
1226 vnc_write_u16(vs
, vs
->ds
->height
);
1228 vnc_write_u8(vs
, vs
->depth
* 8); /* bits-per-pixel */
1229 vnc_write_u8(vs
, vs
->depth
* 8); /* depth */
1230 #ifdef WORDS_BIGENDIAN
1231 vnc_write_u8(vs
, 1); /* big-endian-flag */
1233 vnc_write_u8(vs
, 0); /* big-endian-flag */
1235 vnc_write_u8(vs
, 1); /* true-color-flag */
1236 if (vs
->depth
== 4) {
1237 vnc_write_u16(vs
, 0xFF); /* red-max */
1238 vnc_write_u16(vs
, 0xFF); /* green-max */
1239 vnc_write_u16(vs
, 0xFF); /* blue-max */
1240 vnc_write_u8(vs
, 16); /* red-shift */
1241 vnc_write_u8(vs
, 8); /* green-shift */
1242 vnc_write_u8(vs
, 0); /* blue-shift */
1243 vs
->send_hextile_tile
= send_hextile_tile_32
;
1244 } else if (vs
->depth
== 2) {
1245 vnc_write_u16(vs
, 31); /* red-max */
1246 vnc_write_u16(vs
, 63); /* green-max */
1247 vnc_write_u16(vs
, 31); /* blue-max */
1248 vnc_write_u8(vs
, 11); /* red-shift */
1249 vnc_write_u8(vs
, 5); /* green-shift */
1250 vnc_write_u8(vs
, 0); /* blue-shift */
1251 vs
->send_hextile_tile
= send_hextile_tile_16
;
1252 } else if (vs
->depth
== 1) {
1253 /* XXX: change QEMU pixel 8 bit pixel format to match the VNC one ? */
1254 vnc_write_u16(vs
, 7); /* red-max */
1255 vnc_write_u16(vs
, 7); /* green-max */
1256 vnc_write_u16(vs
, 3); /* blue-max */
1257 vnc_write_u8(vs
, 5); /* red-shift */
1258 vnc_write_u8(vs
, 2); /* green-shift */
1259 vnc_write_u8(vs
, 0); /* blue-shift */
1260 vs
->send_hextile_tile
= send_hextile_tile_8
;
1262 vs
->write_pixels
= vnc_write_pixels_copy
;
1264 vnc_write(vs
, pad
, 3); /* padding */
1272 size
= snprintf(buf
, sizeof(buf
), "%s (%s)", prog
, qemu_name
);
1274 size
= snprintf(buf
, sizeof(buf
), "%s", prog
);
1276 vnc_write_u32(vs
, size
);
1277 vnc_write(vs
, buf
, size
);
1280 vnc_read_when(vs
, protocol_client_msg
, 1);
1285 static void make_challenge(VncState
*vs
)
1289 srand(time(NULL
)+getpid()+getpid()*987654+rand());
1291 for (i
= 0 ; i
< sizeof(vs
->challenge
) ; i
++)
1292 vs
->challenge
[i
] = (int) (256.0*rand()/(RAND_MAX
+1.0));
1295 static int protocol_client_auth_vnc(VncState
*vs
, char *data
, size_t len
)
1297 char response
[VNC_AUTH_CHALLENGE_SIZE
];
1301 if (!vs
->password
|| !vs
->password
[0]) {
1302 VNC_DEBUG("No password configured on server");
1303 vnc_write_u32(vs
, 1); /* Reject auth */
1304 if (vs
->minor
>= 8) {
1305 static const char err
[] = "Authentication failed";
1306 vnc_write_u32(vs
, sizeof(err
));
1307 vnc_write(vs
, err
, sizeof(err
));
1310 vnc_client_error(vs
);
1314 memcpy(response
, vs
->challenge
, VNC_AUTH_CHALLENGE_SIZE
);
1316 /* Calculate the expected challenge response */
1317 pwlen
= strlen(vs
->password
);
1318 for (i
=0; i
<sizeof(key
); i
++)
1319 key
[i
] = i
<pwlen
? vs
->password
[i
] : 0;
1321 for (j
= 0; j
< VNC_AUTH_CHALLENGE_SIZE
; j
+= 8)
1322 des(response
+j
, response
+j
);
1324 /* Compare expected vs actual challenge response */
1325 if (memcmp(response
, data
, VNC_AUTH_CHALLENGE_SIZE
) != 0) {
1326 VNC_DEBUG("Client challenge reponse did not match\n");
1327 vnc_write_u32(vs
, 1); /* Reject auth */
1328 if (vs
->minor
>= 8) {
1329 static const char err
[] = "Authentication failed";
1330 vnc_write_u32(vs
, sizeof(err
));
1331 vnc_write(vs
, err
, sizeof(err
));
1334 vnc_client_error(vs
);
1336 VNC_DEBUG("Accepting VNC challenge response\n");
1337 vnc_write_u32(vs
, 0); /* Accept auth */
1340 vnc_read_when(vs
, protocol_client_init
, 1);
1345 static int start_auth_vnc(VncState
*vs
)
1348 /* Send client a 'random' challenge */
1349 vnc_write(vs
, vs
->challenge
, sizeof(vs
->challenge
));
1352 vnc_read_when(vs
, protocol_client_auth_vnc
, sizeof(vs
->challenge
));
1358 #define DH_BITS 1024
1359 static gnutls_dh_params_t dh_params
;
1361 static int vnc_tls_initialize(void)
1363 static int tlsinitialized
= 0;
1368 if (gnutls_global_init () < 0)
1371 /* XXX ought to re-generate diffie-hellmen params periodically */
1372 if (gnutls_dh_params_init (&dh_params
) < 0)
1374 if (gnutls_dh_params_generate2 (dh_params
, DH_BITS
) < 0)
1378 gnutls_global_set_log_level(10);
1379 gnutls_global_set_log_function(vnc_debug_gnutls_log
);
1387 static gnutls_anon_server_credentials
vnc_tls_initialize_anon_cred(void)
1389 gnutls_anon_server_credentials anon_cred
;
1392 if ((ret
= gnutls_anon_allocate_server_credentials(&anon_cred
)) < 0) {
1393 VNC_DEBUG("Cannot allocate credentials %s\n", gnutls_strerror(ret
));
1397 gnutls_anon_set_server_dh_params(anon_cred
, dh_params
);
1403 static gnutls_certificate_credentials_t
vnc_tls_initialize_x509_cred(VncState
*vs
)
1405 gnutls_certificate_credentials_t x509_cred
;
1408 if (!vs
->x509cacert
) {
1409 VNC_DEBUG("No CA x509 certificate specified\n");
1412 if (!vs
->x509cert
) {
1413 VNC_DEBUG("No server x509 certificate specified\n");
1417 VNC_DEBUG("No server private key specified\n");
1421 if ((ret
= gnutls_certificate_allocate_credentials(&x509_cred
)) < 0) {
1422 VNC_DEBUG("Cannot allocate credentials %s\n", gnutls_strerror(ret
));
1425 if ((ret
= gnutls_certificate_set_x509_trust_file(x509_cred
,
1427 GNUTLS_X509_FMT_PEM
)) < 0) {
1428 VNC_DEBUG("Cannot load CA certificate %s\n", gnutls_strerror(ret
));
1429 gnutls_certificate_free_credentials(x509_cred
);
1433 if ((ret
= gnutls_certificate_set_x509_key_file (x509_cred
,
1436 GNUTLS_X509_FMT_PEM
)) < 0) {
1437 VNC_DEBUG("Cannot load certificate & key %s\n", gnutls_strerror(ret
));
1438 gnutls_certificate_free_credentials(x509_cred
);
1442 if (vs
->x509cacrl
) {
1443 if ((ret
= gnutls_certificate_set_x509_crl_file(x509_cred
,
1445 GNUTLS_X509_FMT_PEM
)) < 0) {
1446 VNC_DEBUG("Cannot load CRL %s\n", gnutls_strerror(ret
));
1447 gnutls_certificate_free_credentials(x509_cred
);
1452 gnutls_certificate_set_dh_params (x509_cred
, dh_params
);
1457 static int vnc_validate_certificate(struct VncState
*vs
)
1460 unsigned int status
;
1461 const gnutls_datum_t
*certs
;
1462 unsigned int nCerts
, i
;
1465 VNC_DEBUG("Validating client certificate\n");
1466 if ((ret
= gnutls_certificate_verify_peers2 (vs
->tls_session
, &status
)) < 0) {
1467 VNC_DEBUG("Verify failed %s\n", gnutls_strerror(ret
));
1471 if ((now
= time(NULL
)) == ((time_t)-1)) {
1476 if (status
& GNUTLS_CERT_INVALID
)
1477 VNC_DEBUG("The certificate is not trusted.\n");
1479 if (status
& GNUTLS_CERT_SIGNER_NOT_FOUND
)
1480 VNC_DEBUG("The certificate hasn't got a known issuer.\n");
1482 if (status
& GNUTLS_CERT_REVOKED
)
1483 VNC_DEBUG("The certificate has been revoked.\n");
1485 if (status
& GNUTLS_CERT_INSECURE_ALGORITHM
)
1486 VNC_DEBUG("The certificate uses an insecure algorithm\n");
1490 VNC_DEBUG("Certificate is valid!\n");
1493 /* Only support x509 for now */
1494 if (gnutls_certificate_type_get(vs
->tls_session
) != GNUTLS_CRT_X509
)
1497 if (!(certs
= gnutls_certificate_get_peers(vs
->tls_session
, &nCerts
)))
1500 for (i
= 0 ; i
< nCerts
; i
++) {
1501 gnutls_x509_crt_t cert
;
1502 VNC_DEBUG ("Checking certificate chain %d\n", i
);
1503 if (gnutls_x509_crt_init (&cert
) < 0)
1506 if (gnutls_x509_crt_import(cert
, &certs
[i
], GNUTLS_X509_FMT_DER
) < 0) {
1507 gnutls_x509_crt_deinit (cert
);
1511 if (gnutls_x509_crt_get_expiration_time (cert
) < now
) {
1512 VNC_DEBUG("The certificate has expired\n");
1513 gnutls_x509_crt_deinit (cert
);
1517 if (gnutls_x509_crt_get_activation_time (cert
) > now
) {
1518 VNC_DEBUG("The certificate is not yet activated\n");
1519 gnutls_x509_crt_deinit (cert
);
1523 if (gnutls_x509_crt_get_activation_time (cert
) > now
) {
1524 VNC_DEBUG("The certificate is not yet activated\n");
1525 gnutls_x509_crt_deinit (cert
);
1529 gnutls_x509_crt_deinit (cert
);
1536 static int start_auth_vencrypt_subauth(VncState
*vs
)
1538 switch (vs
->subauth
) {
1539 case VNC_AUTH_VENCRYPT_TLSNONE
:
1540 case VNC_AUTH_VENCRYPT_X509NONE
:
1541 VNC_DEBUG("Accept TLS auth none\n");
1542 vnc_write_u32(vs
, 0); /* Accept auth completion */
1543 vnc_read_when(vs
, protocol_client_init
, 1);
1546 case VNC_AUTH_VENCRYPT_TLSVNC
:
1547 case VNC_AUTH_VENCRYPT_X509VNC
:
1548 VNC_DEBUG("Start TLS auth VNC\n");
1549 return start_auth_vnc(vs
);
1551 default: /* Should not be possible, but just in case */
1552 VNC_DEBUG("Reject auth %d\n", vs
->auth
);
1553 vnc_write_u8(vs
, 1);
1554 if (vs
->minor
>= 8) {
1555 static const char err
[] = "Unsupported authentication type";
1556 vnc_write_u32(vs
, sizeof(err
));
1557 vnc_write(vs
, err
, sizeof(err
));
1559 vnc_client_error(vs
);
1565 static void vnc_handshake_io(void *opaque
);
1567 static int vnc_continue_handshake(struct VncState
*vs
) {
1570 if ((ret
= gnutls_handshake(vs
->tls_session
)) < 0) {
1571 if (!gnutls_error_is_fatal(ret
)) {
1572 VNC_DEBUG("Handshake interrupted (blocking)\n");
1573 if (!gnutls_record_get_direction(vs
->tls_session
))
1574 qemu_set_fd_handler(vs
->csock
, vnc_handshake_io
, NULL
, vs
);
1576 qemu_set_fd_handler(vs
->csock
, NULL
, vnc_handshake_io
, vs
);
1579 VNC_DEBUG("Handshake failed %s\n", gnutls_strerror(ret
));
1580 vnc_client_error(vs
);
1584 if (vs
->x509verify
) {
1585 if (vnc_validate_certificate(vs
) < 0) {
1586 VNC_DEBUG("Client verification failed\n");
1587 vnc_client_error(vs
);
1590 VNC_DEBUG("Client verification passed\n");
1594 VNC_DEBUG("Handshake done, switching to TLS data mode\n");
1595 vs
->wiremode
= VNC_WIREMODE_TLS
;
1596 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, vnc_client_write
, vs
);
1598 return start_auth_vencrypt_subauth(vs
);
1601 static void vnc_handshake_io(void *opaque
) {
1602 struct VncState
*vs
= (struct VncState
*)opaque
;
1604 VNC_DEBUG("Handshake IO continue\n");
1605 vnc_continue_handshake(vs
);
1608 #define NEED_X509_AUTH(vs) \
1609 ((vs)->subauth == VNC_AUTH_VENCRYPT_X509NONE || \
1610 (vs)->subauth == VNC_AUTH_VENCRYPT_X509VNC || \
1611 (vs)->subauth == VNC_AUTH_VENCRYPT_X509PLAIN)
1614 static int vnc_start_tls(struct VncState
*vs
) {
1615 static const int cert_type_priority
[] = { GNUTLS_CRT_X509
, 0 };
1616 static const int protocol_priority
[]= { GNUTLS_TLS1_1
, GNUTLS_TLS1_0
, GNUTLS_SSL3
, 0 };
1617 static const int kx_anon
[] = {GNUTLS_KX_ANON_DH
, 0};
1618 static const int kx_x509
[] = {GNUTLS_KX_DHE_DSS
, GNUTLS_KX_RSA
, GNUTLS_KX_DHE_RSA
, GNUTLS_KX_SRP
, 0};
1620 VNC_DEBUG("Do TLS setup\n");
1621 if (vnc_tls_initialize() < 0) {
1622 VNC_DEBUG("Failed to init TLS\n");
1623 vnc_client_error(vs
);
1626 if (vs
->tls_session
== NULL
) {
1627 if (gnutls_init(&vs
->tls_session
, GNUTLS_SERVER
) < 0) {
1628 vnc_client_error(vs
);
1632 if (gnutls_set_default_priority(vs
->tls_session
) < 0) {
1633 gnutls_deinit(vs
->tls_session
);
1634 vs
->tls_session
= NULL
;
1635 vnc_client_error(vs
);
1639 if (gnutls_kx_set_priority(vs
->tls_session
, NEED_X509_AUTH(vs
) ? kx_x509
: kx_anon
) < 0) {
1640 gnutls_deinit(vs
->tls_session
);
1641 vs
->tls_session
= NULL
;
1642 vnc_client_error(vs
);
1646 if (gnutls_certificate_type_set_priority(vs
->tls_session
, cert_type_priority
) < 0) {
1647 gnutls_deinit(vs
->tls_session
);
1648 vs
->tls_session
= NULL
;
1649 vnc_client_error(vs
);
1653 if (gnutls_protocol_set_priority(vs
->tls_session
, protocol_priority
) < 0) {
1654 gnutls_deinit(vs
->tls_session
);
1655 vs
->tls_session
= NULL
;
1656 vnc_client_error(vs
);
1660 if (NEED_X509_AUTH(vs
)) {
1661 gnutls_certificate_server_credentials x509_cred
= vnc_tls_initialize_x509_cred(vs
);
1663 gnutls_deinit(vs
->tls_session
);
1664 vs
->tls_session
= NULL
;
1665 vnc_client_error(vs
);
1668 if (gnutls_credentials_set(vs
->tls_session
, GNUTLS_CRD_CERTIFICATE
, x509_cred
) < 0) {
1669 gnutls_deinit(vs
->tls_session
);
1670 vs
->tls_session
= NULL
;
1671 gnutls_certificate_free_credentials(x509_cred
);
1672 vnc_client_error(vs
);
1675 if (vs
->x509verify
) {
1676 VNC_DEBUG("Requesting a client certificate\n");
1677 gnutls_certificate_server_set_request (vs
->tls_session
, GNUTLS_CERT_REQUEST
);
1681 gnutls_anon_server_credentials anon_cred
= vnc_tls_initialize_anon_cred();
1683 gnutls_deinit(vs
->tls_session
);
1684 vs
->tls_session
= NULL
;
1685 vnc_client_error(vs
);
1688 if (gnutls_credentials_set(vs
->tls_session
, GNUTLS_CRD_ANON
, anon_cred
) < 0) {
1689 gnutls_deinit(vs
->tls_session
);
1690 vs
->tls_session
= NULL
;
1691 gnutls_anon_free_server_credentials(anon_cred
);
1692 vnc_client_error(vs
);
1697 gnutls_transport_set_ptr(vs
->tls_session
, (gnutls_transport_ptr_t
)vs
);
1698 gnutls_transport_set_push_function(vs
->tls_session
, vnc_tls_push
);
1699 gnutls_transport_set_pull_function(vs
->tls_session
, vnc_tls_pull
);
1702 VNC_DEBUG("Start TLS handshake process\n");
1703 return vnc_continue_handshake(vs
);
1706 static int protocol_client_vencrypt_auth(VncState
*vs
, char *data
, size_t len
)
1708 int auth
= read_u32(data
, 0);
1710 if (auth
!= vs
->subauth
) {
1711 VNC_DEBUG("Rejecting auth %d\n", auth
);
1712 vnc_write_u8(vs
, 0); /* Reject auth */
1714 vnc_client_error(vs
);
1716 VNC_DEBUG("Accepting auth %d, starting handshake\n", auth
);
1717 vnc_write_u8(vs
, 1); /* Accept auth */
1720 if (vnc_start_tls(vs
) < 0) {
1721 VNC_DEBUG("Failed to complete TLS\n");
1725 if (vs
->wiremode
== VNC_WIREMODE_TLS
) {
1726 VNC_DEBUG("Starting VeNCrypt subauth\n");
1727 return start_auth_vencrypt_subauth(vs
);
1729 VNC_DEBUG("TLS handshake blocked\n");
1736 static int protocol_client_vencrypt_init(VncState
*vs
, char *data
, size_t len
)
1740 VNC_DEBUG("Unsupported VeNCrypt protocol %d.%d\n", (int)data
[0], (int)data
[1]);
1741 vnc_write_u8(vs
, 1); /* Reject version */
1743 vnc_client_error(vs
);
1745 VNC_DEBUG("Sending allowed auth %d\n", vs
->subauth
);
1746 vnc_write_u8(vs
, 0); /* Accept version */
1747 vnc_write_u8(vs
, 1); /* Number of sub-auths */
1748 vnc_write_u32(vs
, vs
->subauth
); /* The supported auth */
1750 vnc_read_when(vs
, protocol_client_vencrypt_auth
, 4);
1755 static int start_auth_vencrypt(VncState
*vs
)
1757 /* Send VeNCrypt version 0.2 */
1758 vnc_write_u8(vs
, 0);
1759 vnc_write_u8(vs
, 2);
1761 vnc_read_when(vs
, protocol_client_vencrypt_init
, 2);
1764 #endif /* CONFIG_VNC_TLS */
1766 static int protocol_client_auth(VncState
*vs
, char *data
, size_t len
)
1768 /* We only advertise 1 auth scheme at a time, so client
1769 * must pick the one we sent. Verify this */
1770 if (data
[0] != vs
->auth
) { /* Reject auth */
1771 VNC_DEBUG("Reject auth %d\n", (int)data
[0]);
1772 vnc_write_u32(vs
, 1);
1773 if (vs
->minor
>= 8) {
1774 static const char err
[] = "Authentication failed";
1775 vnc_write_u32(vs
, sizeof(err
));
1776 vnc_write(vs
, err
, sizeof(err
));
1778 vnc_client_error(vs
);
1779 } else { /* Accept requested auth */
1780 VNC_DEBUG("Client requested auth %d\n", (int)data
[0]);
1783 VNC_DEBUG("Accept auth none\n");
1784 if (vs
->minor
>= 8) {
1785 vnc_write_u32(vs
, 0); /* Accept auth completion */
1788 vnc_read_when(vs
, protocol_client_init
, 1);
1792 VNC_DEBUG("Start VNC auth\n");
1793 return start_auth_vnc(vs
);
1796 case VNC_AUTH_VENCRYPT
:
1797 VNC_DEBUG("Accept VeNCrypt auth\n");;
1798 return start_auth_vencrypt(vs
);
1799 #endif /* CONFIG_VNC_TLS */
1801 default: /* Should not be possible, but just in case */
1802 VNC_DEBUG("Reject auth %d\n", vs
->auth
);
1803 vnc_write_u8(vs
, 1);
1804 if (vs
->minor
>= 8) {
1805 static const char err
[] = "Authentication failed";
1806 vnc_write_u32(vs
, sizeof(err
));
1807 vnc_write(vs
, err
, sizeof(err
));
1809 vnc_client_error(vs
);
1815 static int protocol_version(VncState
*vs
, char *version
, size_t len
)
1819 memcpy(local
, version
, 12);
1822 if (sscanf(local
, "RFB %03d.%03d\n", &vs
->major
, &vs
->minor
) != 2) {
1823 VNC_DEBUG("Malformed protocol version %s\n", local
);
1824 vnc_client_error(vs
);
1827 VNC_DEBUG("Client request protocol version %d.%d\n", vs
->major
, vs
->minor
);
1828 if (vs
->major
!= 3 ||
1834 VNC_DEBUG("Unsupported client version\n");
1835 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
1837 vnc_client_error(vs
);
1840 /* Some broken clients report v3.4 or v3.5, which spec requires to be treated
1841 * as equivalent to v3.3 by servers
1843 if (vs
->minor
== 4 || vs
->minor
== 5)
1846 if (vs
->minor
== 3) {
1847 if (vs
->auth
== VNC_AUTH_NONE
) {
1848 VNC_DEBUG("Tell client auth none\n");
1849 vnc_write_u32(vs
, vs
->auth
);
1851 vnc_read_when(vs
, protocol_client_init
, 1);
1852 } else if (vs
->auth
== VNC_AUTH_VNC
) {
1853 VNC_DEBUG("Tell client VNC auth\n");
1854 vnc_write_u32(vs
, vs
->auth
);
1858 VNC_DEBUG("Unsupported auth %d for protocol 3.3\n", vs
->auth
);
1859 vnc_write_u32(vs
, VNC_AUTH_INVALID
);
1861 vnc_client_error(vs
);
1864 VNC_DEBUG("Telling client we support auth %d\n", vs
->auth
);
1865 vnc_write_u8(vs
, 1); /* num auth */
1866 vnc_write_u8(vs
, vs
->auth
);
1867 vnc_read_when(vs
, protocol_client_auth
, 1);
1874 static void vnc_listen_read(void *opaque
)
1876 VncState
*vs
= opaque
;
1877 struct sockaddr_in addr
;
1878 socklen_t addrlen
= sizeof(addr
);
1880 vs
->csock
= accept(vs
->lsock
, (struct sockaddr
*)&addr
, &addrlen
);
1881 if (vs
->csock
!= -1) {
1882 VNC_DEBUG("New client on socket %d\n", vs
->csock
);
1883 socket_set_nonblock(vs
->csock
);
1884 qemu_set_fd_handler2(vs
->csock
, NULL
, vnc_client_read
, NULL
, opaque
);
1885 vnc_write(vs
, "RFB 003.008\n", 12);
1887 vnc_read_when(vs
, protocol_version
, 12);
1888 memset(vs
->old_data
, 0, vs
->ds
->linesize
* vs
->ds
->height
);
1889 memset(vs
->dirty_row
, 0xFF, sizeof(vs
->dirty_row
));
1891 vs
->has_hextile
= 0;
1892 vs
->ds
->dpy_copy
= NULL
;
1896 extern int parse_host_port(struct sockaddr_in
*saddr
, const char *str
);
1898 void vnc_display_init(DisplayState
*ds
)
1902 vs
= qemu_mallocz(sizeof(VncState
));
1909 vs
->password
= NULL
;
1919 if (!keyboard_layout
)
1920 keyboard_layout
= "en-us";
1922 vs
->kbd_layout
= init_keyboard_layout(keyboard_layout
);
1923 if (!vs
->kbd_layout
)
1926 vs
->ds
->data
= NULL
;
1927 vs
->ds
->dpy_update
= vnc_dpy_update
;
1928 vs
->ds
->dpy_resize
= vnc_dpy_resize
;
1929 vs
->ds
->dpy_refresh
= vnc_dpy_refresh
;
1931 memset(vs
->dirty_row
, 0xFF, sizeof(vs
->dirty_row
));
1933 vnc_dpy_resize(vs
->ds
, 640, 400);
1937 static int vnc_set_x509_credential(VncState
*vs
,
1938 const char *certdir
,
1939 const char *filename
,
1950 if (!(*cred
= qemu_malloc(strlen(certdir
) + strlen(filename
) + 2)))
1953 strcpy(*cred
, certdir
);
1955 strcat(*cred
, filename
);
1957 VNC_DEBUG("Check %s\n", *cred
);
1958 if (stat(*cred
, &sb
) < 0) {
1961 if (ignoreMissing
&& errno
== ENOENT
)
1969 static int vnc_set_x509_credential_dir(VncState
*vs
,
1970 const char *certdir
)
1972 if (vnc_set_x509_credential(vs
, certdir
, X509_CA_CERT_FILE
, &vs
->x509cacert
, 0) < 0)
1974 if (vnc_set_x509_credential(vs
, certdir
, X509_CA_CRL_FILE
, &vs
->x509cacrl
, 1) < 0)
1976 if (vnc_set_x509_credential(vs
, certdir
, X509_SERVER_CERT_FILE
, &vs
->x509cert
, 0) < 0)
1978 if (vnc_set_x509_credential(vs
, certdir
, X509_SERVER_KEY_FILE
, &vs
->x509key
, 0) < 0)
1984 qemu_free(vs
->x509cacert
);
1985 qemu_free(vs
->x509cacrl
);
1986 qemu_free(vs
->x509cert
);
1987 qemu_free(vs
->x509key
);
1988 vs
->x509cacert
= vs
->x509cacrl
= vs
->x509cert
= vs
->x509key
= NULL
;
1991 #endif /* CONFIG_VNC_TLS */
1993 void vnc_display_close(DisplayState
*ds
)
1995 VncState
*vs
= ds
? (VncState
*)ds
->opaque
: vnc_state
;
1998 qemu_free(vs
->display
);
2001 if (vs
->lsock
!= -1) {
2002 qemu_set_fd_handler2(vs
->lsock
, NULL
, NULL
, NULL
, NULL
);
2006 if (vs
->csock
!= -1) {
2007 qemu_set_fd_handler2(vs
->csock
, NULL
, NULL
, NULL
, NULL
);
2008 closesocket(vs
->csock
);
2010 buffer_reset(&vs
->input
);
2011 buffer_reset(&vs
->output
);
2012 vs
->need_update
= 0;
2014 if (vs
->tls_session
) {
2015 gnutls_deinit(vs
->tls_session
);
2016 vs
->tls_session
= NULL
;
2018 vs
->wiremode
= VNC_WIREMODE_CLEAR
;
2019 #endif /* CONFIG_VNC_TLS */
2021 vs
->auth
= VNC_AUTH_INVALID
;
2023 vs
->subauth
= VNC_AUTH_INVALID
;
2028 int vnc_display_password(DisplayState
*ds
, const char *password
)
2030 VncState
*vs
= ds
? (VncState
*)ds
->opaque
: vnc_state
;
2033 qemu_free(vs
->password
);
2034 vs
->password
= NULL
;
2036 if (password
&& password
[0]) {
2037 if (!(vs
->password
= qemu_strdup(password
)))
2044 int vnc_display_open(DisplayState
*ds
, const char *display
)
2046 struct sockaddr
*addr
;
2047 struct sockaddr_in iaddr
;
2049 struct sockaddr_un uaddr
;
2051 int reuse_addr
, ret
;
2054 VncState
*vs
= ds
? (VncState
*)ds
->opaque
: vnc_state
;
2055 const char *options
;
2058 int tls
= 0, x509
= 0;
2061 vnc_display_close(ds
);
2062 if (strcmp(display
, "none") == 0)
2065 if (!(vs
->display
= strdup(display
)))
2069 while ((options
= strchr(options
, ','))) {
2071 if (strncmp(options
, "password", 8) == 0) {
2072 password
= 1; /* Require password auth */
2074 } else if (strncmp(options
, "tls", 3) == 0) {
2075 tls
= 1; /* Require TLS */
2076 } else if (strncmp(options
, "x509", 4) == 0) {
2078 x509
= 1; /* Require x509 certificates */
2079 if (strncmp(options
, "x509verify", 10) == 0)
2080 vs
->x509verify
= 1; /* ...and verify client certs */
2082 /* Now check for 'x509=/some/path' postfix
2083 * and use that to setup x509 certificate/key paths */
2084 start
= strchr(options
, '=');
2085 end
= strchr(options
, ',');
2086 if (start
&& (!end
|| (start
< end
))) {
2087 int len
= end
? end
-(start
+1) : strlen(start
+1);
2088 char *path
= qemu_malloc(len
+1);
2089 strncpy(path
, start
+1, len
);
2091 VNC_DEBUG("Trying certificate path '%s'\n", path
);
2092 if (vnc_set_x509_credential_dir(vs
, path
) < 0) {
2093 fprintf(stderr
, "Failed to find x509 certificates/keys in %s\n", path
);
2095 qemu_free(vs
->display
);
2101 fprintf(stderr
, "No certificate path provided\n");
2102 qemu_free(vs
->display
);
2113 vs
->auth
= VNC_AUTH_VENCRYPT
;
2115 VNC_DEBUG("Initializing VNC server with x509 password auth\n");
2116 vs
->subauth
= VNC_AUTH_VENCRYPT_X509VNC
;
2118 VNC_DEBUG("Initializing VNC server with TLS password auth\n");
2119 vs
->subauth
= VNC_AUTH_VENCRYPT_TLSVNC
;
2123 VNC_DEBUG("Initializing VNC server with password auth\n");
2124 vs
->auth
= VNC_AUTH_VNC
;
2126 vs
->subauth
= VNC_AUTH_INVALID
;
2132 vs
->auth
= VNC_AUTH_VENCRYPT
;
2134 VNC_DEBUG("Initializing VNC server with x509 no auth\n");
2135 vs
->subauth
= VNC_AUTH_VENCRYPT_X509NONE
;
2137 VNC_DEBUG("Initializing VNC server with TLS no auth\n");
2138 vs
->subauth
= VNC_AUTH_VENCRYPT_TLSNONE
;
2142 VNC_DEBUG("Initializing VNC server with no auth\n");
2143 vs
->auth
= VNC_AUTH_NONE
;
2145 vs
->subauth
= VNC_AUTH_INVALID
;
2150 if (strstart(display
, "unix:", &p
)) {
2151 addr
= (struct sockaddr
*)&uaddr
;
2152 addrlen
= sizeof(uaddr
);
2154 vs
->lsock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
2155 if (vs
->lsock
== -1) {
2156 fprintf(stderr
, "Could not create socket\n");
2162 uaddr
.sun_family
= AF_UNIX
;
2163 memset(uaddr
.sun_path
, 0, 108);
2164 snprintf(uaddr
.sun_path
, 108, "%s", p
);
2166 unlink(uaddr
.sun_path
);
2170 addr
= (struct sockaddr
*)&iaddr
;
2171 addrlen
= sizeof(iaddr
);
2173 if (parse_host_port(&iaddr
, display
) < 0) {
2174 fprintf(stderr
, "Could not parse VNC address\n");
2180 iaddr
.sin_port
= htons(ntohs(iaddr
.sin_port
) + 5900);
2182 vs
->lsock
= socket(PF_INET
, SOCK_STREAM
, 0);
2183 if (vs
->lsock
== -1) {
2184 fprintf(stderr
, "Could not create socket\n");
2191 ret
= setsockopt(vs
->lsock
, SOL_SOCKET
, SO_REUSEADDR
,
2192 (const char *)&reuse_addr
, sizeof(reuse_addr
));
2194 fprintf(stderr
, "setsockopt() failed\n");
2203 if (bind(vs
->lsock
, addr
, addrlen
) == -1) {
2204 fprintf(stderr
, "bind() failed\n");
2212 if (listen(vs
->lsock
, 1) == -1) {
2213 fprintf(stderr
, "listen() failed\n");
2221 return qemu_set_fd_handler2(vs
->lsock
, vnc_listen_poll
, vnc_listen_read
, NULL
, vs
);