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

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