vnc-auth-vencrypt.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * QEMU VNC display driver: VeNCrypt authentication setup
  3. *
  4. * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>
  5. * Copyright (C) 2006 Fabrice Bellard
  6. * Copyright (C) 2009 Red Hat, Inc
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "vnc.h"
  27. #include "qemu/main-loop.h"
  28. static void start_auth_vencrypt_subauth(VncState *vs)
  29. {
  30. switch (vs->subauth) {
  31. case VNC_AUTH_VENCRYPT_TLSNONE:
  32. case VNC_AUTH_VENCRYPT_X509NONE:
  33. VNC_DEBUG("Accept TLS auth none\n");
  34. vnc_write_u32(vs, 0); /* Accept auth completion */
  35. start_client_init(vs);
  36. break;
  37. case VNC_AUTH_VENCRYPT_TLSVNC:
  38. case VNC_AUTH_VENCRYPT_X509VNC:
  39. VNC_DEBUG("Start TLS auth VNC\n");
  40. start_auth_vnc(vs);
  41. break;
  42. #ifdef CONFIG_VNC_SASL
  43. case VNC_AUTH_VENCRYPT_TLSSASL:
  44. case VNC_AUTH_VENCRYPT_X509SASL:
  45. VNC_DEBUG("Start TLS auth SASL\n");
  46. start_auth_sasl(vs);
  47. break;
  48. #endif /* CONFIG_VNC_SASL */
  49. default: /* Should not be possible, but just in case */
  50. VNC_DEBUG("Reject subauth %d server bug\n", vs->auth);
  51. vnc_write_u8(vs, 1);
  52. if (vs->minor >= 8) {
  53. static const char err[] = "Unsupported authentication type";
  54. vnc_write_u32(vs, sizeof(err));
  55. vnc_write(vs, err, sizeof(err));
  56. }
  57. vnc_client_error(vs);
  58. }
  59. }
  60. static void vnc_tls_handshake_io(void *opaque);
  61. static int vnc_start_vencrypt_handshake(struct VncState *vs) {
  62. int ret;
  63. if ((ret = gnutls_handshake(vs->tls.session)) < 0) {
  64. if (!gnutls_error_is_fatal(ret)) {
  65. VNC_DEBUG("Handshake interrupted (blocking)\n");
  66. if (!gnutls_record_get_direction(vs->tls.session))
  67. qemu_set_fd_handler(vs->csock, vnc_tls_handshake_io, NULL, vs);
  68. else
  69. qemu_set_fd_handler(vs->csock, NULL, vnc_tls_handshake_io, vs);
  70. return 0;
  71. }
  72. VNC_DEBUG("Handshake failed %s\n", gnutls_strerror(ret));
  73. vnc_client_error(vs);
  74. return -1;
  75. }
  76. if (vs->vd->tls.x509verify) {
  77. if (vnc_tls_validate_certificate(vs) < 0) {
  78. VNC_DEBUG("Client verification failed\n");
  79. vnc_client_error(vs);
  80. return -1;
  81. } else {
  82. VNC_DEBUG("Client verification passed\n");
  83. }
  84. }
  85. VNC_DEBUG("Handshake done, switching to TLS data mode\n");
  86. vs->tls.wiremode = VNC_WIREMODE_TLS;
  87. qemu_set_fd_handler2(vs->csock, NULL, vnc_client_read, vnc_client_write, vs);
  88. start_auth_vencrypt_subauth(vs);
  89. return 0;
  90. }
  91. static void vnc_tls_handshake_io(void *opaque) {
  92. struct VncState *vs = (struct VncState *)opaque;
  93. VNC_DEBUG("Handshake IO continue\n");
  94. vnc_start_vencrypt_handshake(vs);
  95. }
  96. #define NEED_X509_AUTH(vs) \
  97. ((vs)->subauth == VNC_AUTH_VENCRYPT_X509NONE || \
  98. (vs)->subauth == VNC_AUTH_VENCRYPT_X509VNC || \
  99. (vs)->subauth == VNC_AUTH_VENCRYPT_X509PLAIN || \
  100. (vs)->subauth == VNC_AUTH_VENCRYPT_X509SASL)
  101. static int protocol_client_vencrypt_auth(VncState *vs, uint8_t *data, size_t len)
  102. {
  103. int auth = read_u32(data, 0);
  104. if (auth != vs->subauth) {
  105. VNC_DEBUG("Rejecting auth %d\n", auth);
  106. vnc_write_u8(vs, 0); /* Reject auth */
  107. vnc_flush(vs);
  108. vnc_client_error(vs);
  109. } else {
  110. VNC_DEBUG("Accepting auth %d, setting up TLS for handshake\n", auth);
  111. vnc_write_u8(vs, 1); /* Accept auth */
  112. vnc_flush(vs);
  113. if (vnc_tls_client_setup(vs, NEED_X509_AUTH(vs)) < 0) {
  114. VNC_DEBUG("Failed to setup TLS\n");
  115. return 0;
  116. }
  117. VNC_DEBUG("Start TLS VeNCrypt handshake process\n");
  118. if (vnc_start_vencrypt_handshake(vs) < 0) {
  119. VNC_DEBUG("Failed to start TLS handshake\n");
  120. return 0;
  121. }
  122. }
  123. return 0;
  124. }
  125. static int protocol_client_vencrypt_init(VncState *vs, uint8_t *data, size_t len)
  126. {
  127. if (data[0] != 0 ||
  128. data[1] != 2) {
  129. VNC_DEBUG("Unsupported VeNCrypt protocol %d.%d\n", (int)data[0], (int)data[1]);
  130. vnc_write_u8(vs, 1); /* Reject version */
  131. vnc_flush(vs);
  132. vnc_client_error(vs);
  133. } else {
  134. VNC_DEBUG("Sending allowed auth %d\n", vs->subauth);
  135. vnc_write_u8(vs, 0); /* Accept version */
  136. vnc_write_u8(vs, 1); /* Number of sub-auths */
  137. vnc_write_u32(vs, vs->subauth); /* The supported auth */
  138. vnc_flush(vs);
  139. vnc_read_when(vs, protocol_client_vencrypt_auth, 4);
  140. }
  141. return 0;
  142. }
  143. void start_auth_vencrypt(VncState *vs)
  144. {
  145. /* Send VeNCrypt version 0.2 */
  146. vnc_write_u8(vs, 0);
  147. vnc_write_u8(vs, 2);
  148. vnc_read_when(vs, protocol_client_vencrypt_init, 2);
  149. }