vnc-auth-vencrypt.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #include "trace.h"
  31. static void start_auth_vencrypt_subauth(VncState *vs)
  32. {
  33. switch (vs->subauth) {
  34. case VNC_AUTH_VENCRYPT_TLSNONE:
  35. case VNC_AUTH_VENCRYPT_X509NONE:
  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. start_auth_vnc(vs);
  42. break;
  43. #ifdef CONFIG_VNC_SASL
  44. case VNC_AUTH_VENCRYPT_TLSSASL:
  45. case VNC_AUTH_VENCRYPT_X509SASL:
  46. start_auth_sasl(vs);
  47. break;
  48. #endif /* CONFIG_VNC_SASL */
  49. default: /* Should not be possible, but just in case */
  50. trace_vnc_auth_fail(vs, vs->auth, "Unhandled VeNCrypt subauth", "");
  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_done(QIOTask *task,
  61. gpointer user_data)
  62. {
  63. VncState *vs = user_data;
  64. Error *err = NULL;
  65. if (qio_task_propagate_error(task, &err)) {
  66. trace_vnc_auth_fail(vs, vs->auth, "TLS handshake failed",
  67. error_get_pretty(err));
  68. vnc_client_error(vs);
  69. error_free(err);
  70. } else {
  71. if (vs->ioc_tag) {
  72. g_source_remove(vs->ioc_tag);
  73. }
  74. vs->ioc_tag = qio_channel_add_watch(
  75. vs->ioc, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_OUT,
  76. vnc_client_io, vs, NULL);
  77. start_auth_vencrypt_subauth(vs);
  78. }
  79. }
  80. static int protocol_client_vencrypt_auth(VncState *vs, uint8_t *data, size_t len)
  81. {
  82. int auth = read_u32(data, 0);
  83. trace_vnc_auth_vencrypt_subauth(vs, auth);
  84. if (auth != vs->subauth) {
  85. trace_vnc_auth_fail(vs, vs->auth, "Unsupported sub-auth version", "");
  86. vnc_write_u8(vs, 0); /* Reject auth */
  87. vnc_flush(vs);
  88. vnc_client_error(vs);
  89. } else {
  90. Error *err = NULL;
  91. QIOChannelTLS *tls;
  92. vnc_write_u8(vs, 1); /* Accept auth */
  93. vnc_flush(vs);
  94. if (vs->ioc_tag) {
  95. g_source_remove(vs->ioc_tag);
  96. vs->ioc_tag = 0;
  97. }
  98. tls = qio_channel_tls_new_server(
  99. vs->ioc,
  100. vs->vd->tlscreds,
  101. vs->vd->tlsauthzid,
  102. &err);
  103. if (!tls) {
  104. trace_vnc_auth_fail(vs, vs->auth, "TLS setup failed",
  105. error_get_pretty(err));
  106. error_free(err);
  107. vnc_client_error(vs);
  108. return 0;
  109. }
  110. qio_channel_set_name(QIO_CHANNEL(tls), "vnc-server-tls");
  111. object_unref(OBJECT(vs->ioc));
  112. vs->ioc = QIO_CHANNEL(tls);
  113. trace_vnc_client_io_wrap(vs, vs->ioc, "tls");
  114. vs->tls = qio_channel_tls_get_session(tls);
  115. qio_channel_tls_handshake(tls,
  116. vnc_tls_handshake_done,
  117. vs,
  118. NULL,
  119. NULL);
  120. }
  121. return 0;
  122. }
  123. static int protocol_client_vencrypt_init(VncState *vs, uint8_t *data, size_t len)
  124. {
  125. trace_vnc_auth_vencrypt_version(vs, (int)data[0], (int)data[1]);
  126. if (data[0] != 0 ||
  127. data[1] != 2) {
  128. trace_vnc_auth_fail(vs, vs->auth, "Unsupported version", "");
  129. vnc_write_u8(vs, 1); /* Reject version */
  130. vnc_flush(vs);
  131. vnc_client_error(vs);
  132. } else {
  133. vnc_write_u8(vs, 0); /* Accept version */
  134. vnc_write_u8(vs, 1); /* Number of sub-auths */
  135. vnc_write_u32(vs, vs->subauth); /* The supported auth */
  136. vnc_flush(vs);
  137. vnc_read_when(vs, protocol_client_vencrypt_auth, 4);
  138. }
  139. return 0;
  140. }
  141. void start_auth_vencrypt(VncState *vs)
  142. {
  143. /* Send VeNCrypt version 0.2 */
  144. vnc_write_u8(vs, 0);
  145. vnc_write_u8(vs, 2);
  146. vnc_read_when(vs, protocol_client_vencrypt_init, 2);
  147. }