2
0

vnc-auth-vencrypt.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 "qemu/osdep.h"
  27. #include "vnc.h"
  28. #include "qapi/error.h"
  29. #include "qemu/main-loop.h"
  30. static void start_auth_vencrypt_subauth(VncState *vs)
  31. {
  32. switch (vs->subauth) {
  33. case VNC_AUTH_VENCRYPT_TLSNONE:
  34. case VNC_AUTH_VENCRYPT_X509NONE:
  35. VNC_DEBUG("Accept TLS auth none\n");
  36. vnc_write_u32(vs, 0); /* Accept auth completion */
  37. start_client_init(vs);
  38. break;
  39. case VNC_AUTH_VENCRYPT_TLSVNC:
  40. case VNC_AUTH_VENCRYPT_X509VNC:
  41. VNC_DEBUG("Start TLS auth VNC\n");
  42. start_auth_vnc(vs);
  43. break;
  44. #ifdef CONFIG_VNC_SASL
  45. case VNC_AUTH_VENCRYPT_TLSSASL:
  46. case VNC_AUTH_VENCRYPT_X509SASL:
  47. VNC_DEBUG("Start TLS auth SASL\n");
  48. start_auth_sasl(vs);
  49. break;
  50. #endif /* CONFIG_VNC_SASL */
  51. default: /* Should not be possible, but just in case */
  52. VNC_DEBUG("Reject subauth %d server bug\n", vs->auth);
  53. vnc_write_u8(vs, 1);
  54. if (vs->minor >= 8) {
  55. static const char err[] = "Unsupported authentication type";
  56. vnc_write_u32(vs, sizeof(err));
  57. vnc_write(vs, err, sizeof(err));
  58. }
  59. vnc_client_error(vs);
  60. }
  61. }
  62. static void vnc_tls_handshake_done(QIOTask *task,
  63. gpointer user_data)
  64. {
  65. VncState *vs = user_data;
  66. Error *err = NULL;
  67. if (qio_task_propagate_error(task, &err)) {
  68. VNC_DEBUG("Handshake failed %s\n",
  69. error_get_pretty(err));
  70. vnc_client_error(vs);
  71. error_free(err);
  72. } else {
  73. vs->ioc_tag = qio_channel_add_watch(
  74. vs->ioc, G_IO_IN | G_IO_OUT, vnc_client_io, vs, NULL);
  75. start_auth_vencrypt_subauth(vs);
  76. }
  77. }
  78. static int protocol_client_vencrypt_auth(VncState *vs, uint8_t *data, size_t len)
  79. {
  80. int auth = read_u32(data, 0);
  81. if (auth != vs->subauth) {
  82. VNC_DEBUG("Rejecting auth %d\n", auth);
  83. vnc_write_u8(vs, 0); /* Reject auth */
  84. vnc_flush(vs);
  85. vnc_client_error(vs);
  86. } else {
  87. Error *err = NULL;
  88. QIOChannelTLS *tls;
  89. VNC_DEBUG("Accepting auth %d, setting up TLS for handshake\n", auth);
  90. vnc_write_u8(vs, 1); /* Accept auth */
  91. vnc_flush(vs);
  92. if (vs->ioc_tag) {
  93. g_source_remove(vs->ioc_tag);
  94. vs->ioc_tag = 0;
  95. }
  96. tls = qio_channel_tls_new_server(
  97. vs->ioc,
  98. vs->vd->tlscreds,
  99. vs->vd->tlsaclname,
  100. &err);
  101. if (!tls) {
  102. VNC_DEBUG("Failed to setup TLS %s\n", error_get_pretty(err));
  103. error_free(err);
  104. vnc_client_error(vs);
  105. return 0;
  106. }
  107. qio_channel_set_name(QIO_CHANNEL(tls), "vnc-server-tls");
  108. VNC_DEBUG("Start TLS VeNCrypt handshake process\n");
  109. object_unref(OBJECT(vs->ioc));
  110. vs->ioc = QIO_CHANNEL(tls);
  111. vs->tls = qio_channel_tls_get_session(tls);
  112. qio_channel_tls_handshake(tls,
  113. vnc_tls_handshake_done,
  114. vs,
  115. NULL);
  116. }
  117. return 0;
  118. }
  119. static int protocol_client_vencrypt_init(VncState *vs, uint8_t *data, size_t len)
  120. {
  121. if (data[0] != 0 ||
  122. data[1] != 2) {
  123. VNC_DEBUG("Unsupported VeNCrypt protocol %d.%d\n", (int)data[0], (int)data[1]);
  124. vnc_write_u8(vs, 1); /* Reject version */
  125. vnc_flush(vs);
  126. vnc_client_error(vs);
  127. } else {
  128. VNC_DEBUG("Sending allowed auth %d\n", vs->subauth);
  129. vnc_write_u8(vs, 0); /* Accept version */
  130. vnc_write_u8(vs, 1); /* Number of sub-auths */
  131. vnc_write_u32(vs, vs->subauth); /* The supported auth */
  132. vnc_flush(vs);
  133. vnc_read_when(vs, protocol_client_vencrypt_auth, 4);
  134. }
  135. return 0;
  136. }
  137. void start_auth_vencrypt(VncState *vs)
  138. {
  139. /* Send VeNCrypt version 0.2 */
  140. vnc_write_u8(vs, 0);
  141. vnc_write_u8(vs, 2);
  142. vnc_read_when(vs, protocol_client_vencrypt_init, 2);
  143. }