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.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (C) 2005-2006 Martin Koegler
  3. * Copyright (C) 2006 OCCAM Financial Technology
  4. * Copyright (C) 2010 TigerVNC Team
  5. * Copyright (C) 2011 Brian P. Hinz
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this software; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  20. * USA.
  21. */
  22. package com.tigervnc.rfb;
  23. import java.util.*;
  24. import com.tigervnc.rdr.*;
  25. public class CSecurityVeNCrypt extends CSecurity {
  26. public CSecurityVeNCrypt(SecurityClient sec)
  27. {
  28. haveRecvdMajorVersion = false;
  29. haveRecvdMinorVersion = false;
  30. haveSentVersion = false;
  31. haveAgreedVersion = false;
  32. haveListOfTypes = false;
  33. haveNumberOfTypes = false;
  34. haveChosenType = false;
  35. majorVersion = 0;
  36. minorVersion = 0;
  37. chosenType = Security.secTypeVeNCrypt;
  38. nAvailableTypes = 0;
  39. availableTypes = null;
  40. iAvailableType = 0;
  41. security = sec;
  42. }
  43. public boolean processMsg(CConnection cc) {
  44. InStream is = cc.getInStream();
  45. OutStream os = cc.getOutStream();
  46. /* get major, minor versions, send what we can support (or 0.0 for can't support it) */
  47. if (!haveRecvdMajorVersion) {
  48. majorVersion = is.readU8();
  49. haveRecvdMajorVersion = true;
  50. return false;
  51. }
  52. if (!haveRecvdMinorVersion) {
  53. minorVersion = is.readU8();
  54. haveRecvdMinorVersion = true;
  55. }
  56. /* major version in upper 8 bits and minor version in lower 8 bits */
  57. int Version = (majorVersion << 8) | minorVersion;
  58. if (!haveSentVersion) {
  59. /* Currently we don't support former VeNCrypt 0.1 */
  60. if (Version >= 0x0002) {
  61. majorVersion = 0;
  62. minorVersion = 2;
  63. os.writeU8(majorVersion);
  64. os.writeU8(minorVersion);
  65. os.flush();
  66. } else {
  67. /* Send 0.0 to indicate no support */
  68. majorVersion = 0;
  69. minorVersion = 0;
  70. os.writeU8(majorVersion);
  71. os.writeU8(minorVersion);
  72. os.flush();
  73. throw new Exception("Server reported an unsupported VeNCrypt version");
  74. }
  75. haveSentVersion = true;
  76. return false;
  77. }
  78. /* Check that the server is OK */
  79. if (!haveAgreedVersion) {
  80. if (is.readU8() != 0)
  81. throw new Exception("Server reported it could not support the VeNCrypt version");
  82. haveAgreedVersion = true;
  83. return false;
  84. }
  85. /* get a number of types */
  86. if (!haveNumberOfTypes) {
  87. nAvailableTypes = is.readU8();
  88. iAvailableType = 0;
  89. if (nAvailableTypes <= 0)
  90. throw new Exception("The server reported no VeNCrypt sub-types");
  91. availableTypes = new int[nAvailableTypes];
  92. haveNumberOfTypes = true;
  93. return false;
  94. }
  95. if (nAvailableTypes > 0) {
  96. /* read in the types possible */
  97. if (!haveListOfTypes) {
  98. if (is.checkNoWait(4)) {
  99. availableTypes[iAvailableType++] = is.readU32();
  100. haveListOfTypes = (iAvailableType >= nAvailableTypes);
  101. vlog.debug("Server offers security type "+
  102. Security.secTypeName(availableTypes[iAvailableType - 1])+" ("+
  103. availableTypes[iAvailableType - 1]+")");
  104. if (!haveListOfTypes)
  105. return false;
  106. } else
  107. return false;
  108. }
  109. /* make a choice and send it to the server, meanwhile set up the stack */
  110. if (!haveChosenType) {
  111. chosenType = Security.secTypeInvalid;
  112. int i;
  113. Iterator<Integer> j;
  114. List<Integer> secTypes = new ArrayList<Integer>();
  115. secTypes = security.GetEnabledExtSecTypes();
  116. /* Honor server's security type order */
  117. for (i = 0; i < nAvailableTypes; i++) {
  118. for (j = secTypes.iterator(); j.hasNext(); ) {
  119. int refType = (Integer)j.next();
  120. if (refType == availableTypes[i]) {
  121. chosenType = refType;
  122. break;
  123. }
  124. }
  125. if (chosenType != Security.secTypeInvalid)
  126. break;
  127. }
  128. vlog.debug("Choosing security type "+Security.secTypeName(chosenType)+
  129. " ("+chosenType+")");
  130. /* Set up the stack according to the chosen type: */
  131. if (chosenType == Security.secTypeInvalid || chosenType == Security.secTypeVeNCrypt)
  132. throw new AuthFailureException("No valid VeNCrypt sub-type");
  133. csecurity = security.GetCSecurity(chosenType);
  134. /* send chosen type to server */
  135. os.writeU32(chosenType);
  136. os.flush();
  137. haveChosenType = true;
  138. }
  139. } else {
  140. /*
  141. * Server told us that there are 0 types it can support - this should not
  142. * happen, since if the server supports 0 sub-types, it doesn't support
  143. * this security type
  144. */
  145. throw new AuthFailureException("The server reported 0 VeNCrypt sub-types");
  146. }
  147. return csecurity.processMsg(cc);
  148. }
  149. public final int getType() { return chosenType; }
  150. public final String description()
  151. {
  152. if (csecurity != null)
  153. return csecurity.description();
  154. return "VeNCrypt";
  155. }
  156. public final boolean isSecure()
  157. {
  158. if (csecurity != null && csecurity.isSecure())
  159. return true;
  160. return false;
  161. }
  162. public static StringParameter secTypesStr;
  163. private CSecurity csecurity;
  164. SecurityClient security;
  165. private boolean haveRecvdMajorVersion;
  166. private boolean haveRecvdMinorVersion;
  167. private boolean haveSentVersion;
  168. private boolean haveAgreedVersion;
  169. private boolean haveListOfTypes;
  170. private boolean haveNumberOfTypes;
  171. private boolean haveChosenType;
  172. private int majorVersion, minorVersion;
  173. private int chosenType;
  174. private int nAvailableTypes;
  175. private int[] availableTypes;
  176. private int iAvailableType;
  177. //private final String desc;
  178. static LogWriter vlog = new LogWriter("CSecurityVeNCrypt");
  179. }