You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SSecurityVeNCrypt.cxx 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (C) 2005-2006 Martin Koegler
  3. * Copyright (C) 2006 OCCAM Financial Technology
  4. * Copyright (C) 2010 TigerVNC Team
  5. *
  6. * This is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This software is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this software; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  19. * USA.
  20. */
  21. /*
  22. * SSecurityVeNCrypt
  23. */
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #include <rfb/SSecurityVeNCrypt.h>
  28. #include <rfb/Exception.h>
  29. #include <rfb/LogWriter.h>
  30. #include <rdr/InStream.h>
  31. #include <rdr/OutStream.h>
  32. using namespace rfb;
  33. using namespace rdr;
  34. using namespace std;
  35. static LogWriter vlog("SVeNCrypt");
  36. SSecurityVeNCrypt::SSecurityVeNCrypt(SConnection* sc, SecurityServer *sec)
  37. : SSecurity(sc), security(sec)
  38. {
  39. ssecurity = NULL;
  40. haveSentVersion = false;
  41. haveRecvdMajorVersion = false;
  42. haveRecvdMinorVersion = false;
  43. majorVersion = 0;
  44. minorVersion = 0;
  45. haveSentTypes = false;
  46. haveChosenType = false;
  47. chosenType = secTypeVeNCrypt;
  48. numTypes = 0;
  49. subTypes = NULL;
  50. }
  51. SSecurityVeNCrypt::~SSecurityVeNCrypt()
  52. {
  53. delete ssecurity;
  54. if (subTypes) {
  55. delete [] subTypes;
  56. subTypes = NULL;
  57. }
  58. }
  59. bool SSecurityVeNCrypt::processMsg()
  60. {
  61. rdr::InStream* is = sc->getInStream();
  62. rdr::OutStream* os = sc->getOutStream();
  63. rdr::U8 i;
  64. /* VeNCrypt initialization */
  65. /* Send the highest version we can support */
  66. if (!haveSentVersion) {
  67. os->writeU8(0);
  68. os->writeU8(2);
  69. haveSentVersion = true;
  70. os->flush();
  71. }
  72. /* Receive back highest version that client can support (up to and including ours) */
  73. if (!haveRecvdMajorVersion) {
  74. if (!is->hasData(1))
  75. return false;
  76. majorVersion = is->readU8();
  77. haveRecvdMajorVersion = true;
  78. }
  79. if (!haveRecvdMinorVersion) {
  80. if (!is->hasData(1))
  81. return false;
  82. minorVersion = is->readU8();
  83. haveRecvdMinorVersion = true;
  84. /* WORD value with major version in upper 8 bits and minor version in lower 8 bits */
  85. U16 Version = (((U16)majorVersion) << 8) | ((U16)minorVersion);
  86. switch (Version) {
  87. case 0x0000: /* 0.0 - The client cannot support us! */
  88. case 0x0001: /* 0.1 Legacy VeNCrypt, not supported */
  89. os->writeU8(0xFF); /* This is not OK */
  90. os->flush();
  91. throw AuthFailureException("The client cannot support the server's "
  92. "VeNCrypt version");
  93. case 0x0002: /* 0.2 */
  94. os->writeU8(0); /* OK */
  95. break;
  96. default:
  97. os->writeU8(0xFF); /* Not OK */
  98. os->flush();
  99. throw AuthFailureException("The client returned an unsupported VeNCrypt version");
  100. }
  101. }
  102. /*
  103. * send number of supported VeNCrypt authentication types (U8) followed
  104. * by authentication types (U32s)
  105. */
  106. if (!haveSentTypes) {
  107. list<U32> listSubTypes;
  108. listSubTypes = security->GetEnabledExtSecTypes();
  109. numTypes = listSubTypes.size();
  110. subTypes = new U32[numTypes];
  111. for (i = 0; i < numTypes; i++) {
  112. subTypes[i] = listSubTypes.front();
  113. listSubTypes.pop_front();
  114. }
  115. if (numTypes) {
  116. os->writeU8(numTypes);
  117. for (i = 0; i < numTypes; i++)
  118. os->writeU32(subTypes[i]);
  119. os->flush();
  120. haveSentTypes = true;
  121. } else
  122. throw AuthFailureException("There are no VeNCrypt sub-types to send to the client");
  123. }
  124. /* get type back from client (must be one of the ones we sent) */
  125. if (!haveChosenType) {
  126. if (!is->hasData(4))
  127. return false;
  128. chosenType = is->readU32();
  129. for (i = 0; i < numTypes; i++) {
  130. if (chosenType == subTypes[i]) {
  131. haveChosenType = true;
  132. break;
  133. }
  134. }
  135. if (!haveChosenType)
  136. chosenType = secTypeInvalid;
  137. vlog.info("Client requests security type %s (%d)", secTypeName(chosenType),
  138. chosenType);
  139. /* Set up the stack according to the chosen type */
  140. if (chosenType == secTypeInvalid || chosenType == secTypeVeNCrypt)
  141. throw AuthFailureException("No valid VeNCrypt sub-type");
  142. ssecurity = security->GetSSecurity(sc, chosenType);
  143. }
  144. /* continue processing the messages */
  145. return ssecurity->processMsg();
  146. }
  147. const char* SSecurityVeNCrypt::getUserName() const
  148. {
  149. if (ssecurity == NULL)
  150. return NULL;
  151. return ssecurity->getUserName();
  152. }
  153. SConnection::AccessRights SSecurityVeNCrypt::getAccessRights() const
  154. {
  155. if (ssecurity == NULL)
  156. return SSecurity::getAccessRights();
  157. return ssecurity->getAccessRights();
  158. }