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

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