Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

CSecurityVeNCrypt.cxx 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (C) 2006 OCCAM Financial Technology
  3. * Copyright (C) 2005-2006 Martin Koegler
  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. // CSecurityVeNCrypt
  23. //
  24. #include <rfb/Exception.h>
  25. #include <rdr/InStream.h>
  26. #include <rdr/OutStream.h>
  27. #include <rfb/CConnection.h>
  28. #include <rfb/CSecurityVeNCrypt.h>
  29. #include <rfb/LogWriter.h>
  30. #include <list>
  31. using namespace rfb;
  32. using namespace rdr;
  33. using namespace std;
  34. static LogWriter vlog("CVeNCrypt");
  35. CSecurityVeNCrypt::CSecurityVeNCrypt(CConnection* cc, SecurityClient* sec)
  36. : CSecurity(cc), csecurity(NULL), security(sec)
  37. {
  38. haveRecvdMajorVersion = false;
  39. haveRecvdMinorVersion = false;
  40. haveSentVersion = false;
  41. haveAgreedVersion = false;
  42. haveListOfTypes = false;
  43. haveNumberOfTypes = false;
  44. haveChosenType = false;
  45. majorVersion = 0;
  46. minorVersion = 0;
  47. chosenType = secTypeVeNCrypt;
  48. nAvailableTypes = 0;
  49. availableTypes = NULL;
  50. iAvailableType = 0;
  51. }
  52. CSecurityVeNCrypt::~CSecurityVeNCrypt()
  53. {
  54. if (availableTypes)
  55. delete[] availableTypes;
  56. }
  57. bool CSecurityVeNCrypt::processMsg()
  58. {
  59. InStream* is = cc->getInStream();
  60. OutStream* os = cc->getOutStream();
  61. /* get major, minor versions, send what we can support (or 0.0 for can't support it) */
  62. if (!haveRecvdMajorVersion) {
  63. majorVersion = is->readU8();
  64. haveRecvdMajorVersion = true;
  65. return false;
  66. }
  67. if (!haveRecvdMinorVersion) {
  68. minorVersion = is->readU8();
  69. haveRecvdMinorVersion = true;
  70. }
  71. /* major version in upper 8 bits and minor version in lower 8 bits */
  72. U16 Version = (((U16) majorVersion) << 8) | ((U16) minorVersion);
  73. if (!haveSentVersion) {
  74. /* Currently we don't support former VeNCrypt 0.1 */
  75. if (Version >= 0x0002) {
  76. majorVersion = 0;
  77. minorVersion = 2;
  78. os->writeU8(majorVersion);
  79. os->writeU8(minorVersion);
  80. os->flush();
  81. } else {
  82. /* Send 0.0 to indicate no support */
  83. majorVersion = 0;
  84. minorVersion = 0;
  85. os->writeU8(0);
  86. os->writeU8(0);
  87. os->flush();
  88. throw AuthFailureException("The server reported an unsupported VeNCrypt version");
  89. }
  90. haveSentVersion = true;
  91. return false;
  92. }
  93. /* Check that the server is OK */
  94. if (!haveAgreedVersion) {
  95. if (is->readU8())
  96. throw AuthFailureException("The server reported it could not support the "
  97. "VeNCrypt version");
  98. haveAgreedVersion = true;
  99. return false;
  100. }
  101. /* get a number of types */
  102. if (!haveNumberOfTypes) {
  103. nAvailableTypes = is->readU8();
  104. iAvailableType = 0;
  105. if (!nAvailableTypes)
  106. throw AuthFailureException("The server reported no VeNCrypt sub-types");
  107. availableTypes = new rdr::U32[nAvailableTypes];
  108. haveNumberOfTypes = true;
  109. return false;
  110. }
  111. if (nAvailableTypes) {
  112. /* read in the types possible */
  113. if (!haveListOfTypes) {
  114. if (is->checkNoWait(4)) {
  115. availableTypes[iAvailableType++] = is->readU32();
  116. haveListOfTypes = (iAvailableType >= nAvailableTypes);
  117. vlog.debug("Server offers security type %s (%d)",
  118. secTypeName(availableTypes[iAvailableType - 1]),
  119. availableTypes[iAvailableType - 1]);
  120. if (!haveListOfTypes)
  121. return false;
  122. } else
  123. return false;
  124. }
  125. /* make a choice and send it to the server, meanwhile set up the stack */
  126. if (!haveChosenType) {
  127. chosenType = secTypeInvalid;
  128. U8 i;
  129. list<U32>::iterator j;
  130. list<U32> secTypes;
  131. secTypes = security->GetEnabledExtSecTypes();
  132. /* Honor server's security type order */
  133. for (i = 0; i < nAvailableTypes; i++) {
  134. for (j = secTypes.begin(); j != secTypes.end(); j++) {
  135. if (*j == availableTypes[i]) {
  136. chosenType = *j;
  137. break;
  138. }
  139. }
  140. if (chosenType != secTypeInvalid)
  141. break;
  142. }
  143. vlog.info("Choosing security type %s (%d)", secTypeName(chosenType),
  144. chosenType);
  145. /* Set up the stack according to the chosen type: */
  146. if (chosenType == secTypeInvalid || chosenType == secTypeVeNCrypt)
  147. throw AuthFailureException("No valid VeNCrypt sub-type");
  148. csecurity = security->GetCSecurity(cc, chosenType);
  149. /* send chosen type to server */
  150. os->writeU32(chosenType);
  151. os->flush();
  152. haveChosenType = true;
  153. }
  154. } else {
  155. /*
  156. * Server told us that there are 0 types it can support - this should not
  157. * happen, since if the server supports 0 sub-types, it doesn't support
  158. * this security type
  159. */
  160. throw AuthFailureException("The server reported 0 VeNCrypt sub-types");
  161. }
  162. return csecurity->processMsg();
  163. }
  164. const char* CSecurityVeNCrypt::description() const
  165. {
  166. if (csecurity)
  167. return csecurity->description();
  168. return "VeNCrypt";
  169. }
  170. bool CSecurityVeNCrypt::isSecure() const
  171. {
  172. if (csecurity && csecurity->isSecure())
  173. return true;
  174. return false;
  175. }