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.

Security.cxx 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright (C) 2010 TigerVNC Team
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This software is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this software; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. * USA.
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22. #include <assert.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <rfb/CSecurityNone.h>
  26. #include <rfb/CSecurityStack.h>
  27. #include <rfb/CSecurityVeNCrypt.h>
  28. #include <rfb/CSecurityVncAuth.h>
  29. #include <rfb/CSecurityPlain.h>
  30. #include <rdr/Exception.h>
  31. #include <rfb/LogWriter.h>
  32. #include <rfb/Security.h>
  33. #include <rfb/SSecurityNone.h>
  34. #include <rfb/SSecurityStack.h>
  35. #include <rfb/SSecurityPlain.h>
  36. #include <rfb/SSecurityVncAuth.h>
  37. #include <rfb/SSecurityVeNCrypt.h>
  38. #ifdef HAVE_GNUTLS
  39. #include <rfb/CSecurityTLS.h>
  40. #include <rfb/SSecurityTLS.h>
  41. #endif
  42. #include <rfb/util.h>
  43. using namespace rdr;
  44. using namespace rfb;
  45. using namespace std;
  46. static LogWriter vlog("Security");
  47. #ifdef HAVE_GNUTLS
  48. StringParameter Security::GnuTLSPriority("GnuTLSPriority",
  49. "GnuTLS priority string that controls the TLS session’s handshake algorithms",
  50. "");
  51. #endif
  52. Security::Security()
  53. {
  54. }
  55. Security::Security(StringParameter &secTypes)
  56. {
  57. char *secTypesStr;
  58. secTypesStr = secTypes.getData();
  59. enabledSecTypes = parseSecTypes(secTypesStr);
  60. delete [] secTypesStr;
  61. }
  62. const std::list<rdr::U8> Security::GetEnabledSecTypes(void)
  63. {
  64. list<rdr::U8> result;
  65. list<U32>::iterator i;
  66. /* Partial workaround for Vino's stupid behaviour. It doesn't allow
  67. * the basic authentication types as part of the VeNCrypt handshake,
  68. * making it impossible for a client to do opportunistic encryption.
  69. * At least make it possible to connect when encryption is explicitly
  70. * disabled. */
  71. for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++) {
  72. if (*i >= 0x100) {
  73. result.push_back(secTypeVeNCrypt);
  74. break;
  75. }
  76. }
  77. for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++)
  78. if (*i < 0x100)
  79. result.push_back(*i);
  80. return result;
  81. }
  82. const std::list<rdr::U32> Security::GetEnabledExtSecTypes(void)
  83. {
  84. list<rdr::U32> result;
  85. list<U32>::iterator i;
  86. for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++)
  87. if (*i != secTypeVeNCrypt) /* Do not include VeNCrypt type to avoid loops */
  88. result.push_back(*i);
  89. return result;
  90. }
  91. void Security::EnableSecType(U32 secType)
  92. {
  93. list<U32>::iterator i;
  94. for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++)
  95. if (*i == secType)
  96. return;
  97. enabledSecTypes.push_back(secType);
  98. }
  99. bool Security::IsSupported(U32 secType)
  100. {
  101. list<U32>::iterator i;
  102. for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++)
  103. if (*i == secType)
  104. return true;
  105. if (secType == secTypeVeNCrypt)
  106. return true;
  107. return false;
  108. }
  109. char *Security::ToString(void)
  110. {
  111. list<U32>::iterator i;
  112. static char out[128]; /* Should be enough */
  113. bool firstpass = true;
  114. const char *name;
  115. memset(out, 0, sizeof(out));
  116. for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++) {
  117. name = secTypeName(*i);
  118. if (name[0] == '[') /* Unknown security type */
  119. continue;
  120. if (!firstpass)
  121. strncat(out, ",", sizeof(out) - 1);
  122. else
  123. firstpass = false;
  124. strncat(out, name, sizeof(out) - 1);
  125. }
  126. return out;
  127. }
  128. rdr::U32 rfb::secTypeNum(const char* name)
  129. {
  130. if (strcasecmp(name, "None") == 0) return secTypeNone;
  131. if (strcasecmp(name, "VncAuth") == 0) return secTypeVncAuth;
  132. if (strcasecmp(name, "Tight") == 0) return secTypeTight;
  133. if (strcasecmp(name, "RA2") == 0) return secTypeRA2;
  134. if (strcasecmp(name, "RA2ne") == 0) return secTypeRA2ne;
  135. if (strcasecmp(name, "SSPI") == 0) return secTypeSSPI;
  136. if (strcasecmp(name, "SSPIne") == 0) return secTypeSSPIne;
  137. if (strcasecmp(name, "VeNCrypt") == 0) return secTypeVeNCrypt;
  138. /* VeNCrypt subtypes */
  139. if (strcasecmp(name, "Plain") == 0) return secTypePlain;
  140. if (strcasecmp(name, "TLSNone") == 0) return secTypeTLSNone;
  141. if (strcasecmp(name, "TLSVnc") == 0) return secTypeTLSVnc;
  142. if (strcasecmp(name, "TLSPlain") == 0) return secTypeTLSPlain;
  143. if (strcasecmp(name, "X509None") == 0) return secTypeX509None;
  144. if (strcasecmp(name, "X509Vnc") == 0) return secTypeX509Vnc;
  145. if (strcasecmp(name, "X509Plain") == 0) return secTypeX509Plain;
  146. return secTypeInvalid;
  147. }
  148. const char* rfb::secTypeName(rdr::U32 num)
  149. {
  150. switch (num) {
  151. case secTypeNone: return "None";
  152. case secTypeVncAuth: return "VncAuth";
  153. case secTypeTight: return "Tight";
  154. case secTypeRA2: return "RA2";
  155. case secTypeRA2ne: return "RA2ne";
  156. case secTypeSSPI: return "SSPI";
  157. case secTypeSSPIne: return "SSPIne";
  158. case secTypeVeNCrypt: return "VeNCrypt";
  159. /* VeNCrypt subtypes */
  160. case secTypePlain: return "Plain";
  161. case secTypeTLSNone: return "TLSNone";
  162. case secTypeTLSVnc: return "TLSVnc";
  163. case secTypeTLSPlain: return "TLSPlain";
  164. case secTypeX509None: return "X509None";
  165. case secTypeX509Vnc: return "X509Vnc";
  166. case secTypeX509Plain: return "X509Plain";
  167. default: return "[unknown secType]";
  168. }
  169. }
  170. std::list<rdr::U32> rfb::parseSecTypes(const char* types_)
  171. {
  172. std::list<rdr::U32> result;
  173. CharArray types(strDup(types_)), type;
  174. while (types.buf) {
  175. strSplit(types.buf, ',', &type.buf, &types.buf);
  176. rdr::U32 typeNum = secTypeNum(type.buf);
  177. if (typeNum != secTypeInvalid)
  178. result.push_back(typeNum);
  179. }
  180. return result;
  181. }