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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. delete [] subTypes;
  55. }
  56. bool SSecurityVeNCrypt::processMsg()
  57. {
  58. rdr::InStream* is = sc->getInStream();
  59. rdr::OutStream* os = sc->getOutStream();
  60. rdr::U8 i;
  61. /* VeNCrypt initialization */
  62. /* Send the highest version we can support */
  63. if (!haveSentVersion) {
  64. os->writeU8(0);
  65. os->writeU8(2);
  66. haveSentVersion = true;
  67. os->flush();
  68. }
  69. /* Receive back highest version that client can support (up to and including ours) */
  70. if (!haveRecvdMajorVersion) {
  71. if (!is->hasData(1))
  72. return false;
  73. majorVersion = is->readU8();
  74. haveRecvdMajorVersion = true;
  75. }
  76. if (!haveRecvdMinorVersion) {
  77. if (!is->hasData(1))
  78. return false;
  79. minorVersion = is->readU8();
  80. haveRecvdMinorVersion = true;
  81. /* WORD value with major version in upper 8 bits and minor version in lower 8 bits */
  82. U16 Version = (((U16)majorVersion) << 8) | ((U16)minorVersion);
  83. switch (Version) {
  84. case 0x0000: /* 0.0 - The client cannot support us! */
  85. case 0x0001: /* 0.1 Legacy VeNCrypt, not supported */
  86. os->writeU8(0xFF); /* This is not OK */
  87. os->flush();
  88. throw AuthFailureException("The client cannot support the server's "
  89. "VeNCrypt version");
  90. case 0x0002: /* 0.2 */
  91. os->writeU8(0); /* OK */
  92. break;
  93. default:
  94. os->writeU8(0xFF); /* Not OK */
  95. os->flush();
  96. throw AuthFailureException("The client returned an unsupported VeNCrypt version");
  97. }
  98. }
  99. /*
  100. * send number of supported VeNCrypt authentication types (U8) followed
  101. * by authentication types (U32s)
  102. */
  103. if (!haveSentTypes) {
  104. list<U32> listSubTypes;
  105. listSubTypes = security->GetEnabledExtSecTypes();
  106. numTypes = listSubTypes.size();
  107. subTypes = new U32[numTypes];
  108. for (i = 0; i < numTypes; i++) {
  109. subTypes[i] = listSubTypes.front();
  110. listSubTypes.pop_front();
  111. }
  112. if (numTypes) {
  113. os->writeU8(numTypes);
  114. for (i = 0; i < numTypes; i++)
  115. os->writeU32(subTypes[i]);
  116. os->flush();
  117. haveSentTypes = true;
  118. } else
  119. throw AuthFailureException("There are no VeNCrypt sub-types to send to the client");
  120. }
  121. /* get type back from client (must be one of the ones we sent) */
  122. if (!haveChosenType) {
  123. if (!is->hasData(4))
  124. return false;
  125. chosenType = is->readU32();
  126. for (i = 0; i < numTypes; i++) {
  127. if (chosenType == subTypes[i]) {
  128. haveChosenType = true;
  129. break;
  130. }
  131. }
  132. if (!haveChosenType)
  133. chosenType = secTypeInvalid;
  134. vlog.info("Client requests security type %s (%d)", secTypeName(chosenType),
  135. chosenType);
  136. /* Set up the stack according to the chosen type */
  137. if (chosenType == secTypeInvalid || chosenType == secTypeVeNCrypt)
  138. throw AuthFailureException("No valid VeNCrypt sub-type");
  139. ssecurity = security->GetSSecurity(sc, chosenType);
  140. }
  141. /* continue processing the messages */
  142. return ssecurity->processMsg();
  143. }
  144. const char* SSecurityVeNCrypt::getUserName() const
  145. {
  146. if (ssecurity == NULL)
  147. return NULL;
  148. return ssecurity->getUserName();
  149. }
  150. SConnection::AccessRights SSecurityVeNCrypt::getAccessRights() const
  151. {
  152. if (ssecurity == NULL)
  153. return SSecurity::getAccessRights();
  154. return ssecurity->getAccessRights();
  155. }