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.

CSecurityVeNCrypt.cxx 5.4KB

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