vnc-auth-vencrypt.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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_OUT, vnc_client_io, vs, NULL);
  76. start_auth_vencrypt_subauth(vs);
  77. }
  78. }
  79. static int protocol_client_vencrypt_auth(VncState *vs, uint8_t *data, size_t len)
  80. {
  81. int auth = read_u32(data, 0);
  82. trace_vnc_auth_vencrypt_subauth(vs, auth);
  83. if (auth != vs->subauth) {
  84. trace_vnc_auth_fail(vs, vs->auth, "Unsupported sub-auth version", "");
  85. vnc_write_u8(vs, 0); /* Reject auth */
  86. vnc_flush(vs);
  87. vnc_client_error(vs);
  88. } else {
  89. Error *err = NULL;
  90. QIOChannelTLS *tls;
  91. vnc_write_u8(vs, 1); /* Accept auth */
  92. vnc_flush(vs);
  93. if (vs->ioc_tag) {
  94. g_source_remove(vs->ioc_tag);
  95. vs->ioc_tag = 0;
  96. }
  97. tls = qio_channel_tls_new_server(
  98. vs->ioc,
  99. vs->vd->tlscreds,
  100. vs->vd->tlsauthzid,
  101. &err);
  102. if (!tls) {
  103. trace_vnc_auth_fail(vs, vs->auth, "TLS setup failed",
  104. error_get_pretty(err));
  105. error_free(err);
  106. vnc_client_error(vs);
  107. return 0;
  108. }
  109. qio_channel_set_name(QIO_CHANNEL(tls), "vnc-server-tls");
  110. object_unref(OBJECT(vs->ioc));
  111. vs->ioc = QIO_CHANNEL(tls);
  112. trace_vnc_client_io_wrap(vs, vs->ioc, "tls");
  113. vs->tls = qio_channel_tls_get_session(tls);
  114. qio_channel_tls_handshake(tls,
  115. vnc_tls_handshake_done,
  116. vs,
  117. NULL,
  118. NULL);
  119. }
  120. return 0;
  121. }
  122. static int protocol_client_vencrypt_init(VncState *vs, uint8_t *data, size_t len)
  123. {
  124. trace_vnc_auth_vencrypt_version(vs, (int)data[0], (int)data[1]);
  125. if (data[0] != 0 ||
  126. data[1] != 2) {
  127. trace_vnc_auth_fail(vs, vs->auth, "Unsupported version", "");
  128. vnc_write_u8(vs, 1); /* Reject version */
  129. vnc_flush(vs);
  130. vnc_client_error(vs);
  131. } else {
  132. vnc_write_u8(vs, 0); /* Accept version */
  133. vnc_write_u8(vs, 1); /* Number of sub-auths */
  134. vnc_write_u32(vs, vs->subauth); /* The supported auth */
  135. vnc_flush(vs);
  136. vnc_read_when(vs, protocol_client_vencrypt_auth, 4);
  137. }
  138. return 0;
  139. }
  140. void start_auth_vencrypt(VncState *vs)
  141. {
  142. /* Send VeNCrypt version 0.2 */
  143. vnc_write_u8(vs, 0);
  144. vnc_write_u8(vs, 2);
  145. vnc_read_when(vs, protocol_client_vencrypt_init, 2);
  146. }