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

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