blob: dba14fdb46a6fbd10ee2838cbd50e097fd6140ea [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
Adam Tkacdf799702010-04-28 15:45:53 +00002 * Copyright (C) 2010 TigerVNC Team
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00003 *
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 */
Adam Tkacdf799702010-04-28 15:45:53 +000019
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
Adam Tkac1d15e2d2010-04-23 14:06:38 +000024#include <assert.h>
25#include <stdlib.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000026#include <string.h>
27#ifdef _WIN32
28#define strcasecmp _stricmp
29#endif
Adam Tkacc210e8a2010-04-23 14:09:16 +000030#include <rfb/CSecurityNone.h>
Adam Tkac707d3612010-07-20 15:16:10 +000031#include <rfb/CSecurityStack.h>
Adam Tkacb10489b2010-04-23 14:16:04 +000032#include <rfb/CSecurityVeNCrypt.h>
Adam Tkacc210e8a2010-04-23 14:09:16 +000033#include <rfb/CSecurityVncAuth.h>
Adam Tkac8c048382010-09-02 12:37:00 +000034#include <rfb/CSecurityPlain.h>
Adam Tkac1d15e2d2010-04-23 14:06:38 +000035#include <rdr/Exception.h>
36#include <rfb/LogWriter.h>
Adam Tkacb6eb3992010-04-23 14:05:00 +000037#include <rfb/Security.h>
Adam Tkac1d15e2d2010-04-23 14:06:38 +000038#include <rfb/SSecurityNone.h>
Adam Tkac707d3612010-07-20 15:16:10 +000039#include <rfb/SSecurityStack.h>
Adam Tkac520fc412010-09-02 14:13:24 +000040#include <rfb/SSecurityPlain.h>
Adam Tkac1d15e2d2010-04-23 14:06:38 +000041#include <rfb/SSecurityVncAuth.h>
Adam Tkacdfe19cf2010-04-23 14:14:11 +000042#include <rfb/SSecurityVeNCrypt.h>
Adam Tkac707d3612010-07-20 15:16:10 +000043#ifdef HAVE_GNUTLS
Adam Tkac3c5be392010-07-21 09:27:34 +000044#include <rfb/CSecurityTLS.h>
Adam Tkac21b61a52010-07-21 09:19:00 +000045#include <rfb/SSecurityTLS.h>
Adam Tkacdf799702010-04-28 15:45:53 +000046#endif
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000047#include <rfb/util.h>
48
Adam Tkac1d15e2d2010-04-23 14:06:38 +000049using namespace rdr;
50using namespace rfb;
51using namespace std;
52
53static LogWriter vlog("Security");
54
Adam Tkacbfd66c12010-10-01 08:33:29 +000055Security::Security(StringParameter &secTypes)
Adam Tkac1d15e2d2010-04-23 14:06:38 +000056{
Adam Tkacfb993152010-08-12 14:17:28 +000057 char *secTypesStr;
58
Adam Tkacbfd66c12010-10-01 08:33:29 +000059 secTypesStr = secTypes.getData();
Adam Tkac1d15e2d2010-04-23 14:06:38 +000060 enabledSecTypes = parseSecTypes(secTypesStr);
61
62 delete secTypesStr;
63}
64
Adam Tkac0c77e512010-07-20 15:10:16 +000065const std::list<rdr::U8> Security::GetEnabledSecTypes(void)
Adam Tkac1d15e2d2010-04-23 14:06:38 +000066{
Adam Tkac0c77e512010-07-20 15:10:16 +000067 list<rdr::U8> result;
68 list<U32>::iterator i;
69
70 for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++)
71 if (*i < 0x100)
72 result.push_back(*i);
73
74 return result;
75}
76
77const std::list<rdr::U32> Security::GetEnabledExtSecTypes(void)
78{
79 list<rdr::U32> result;
80 list<U32>::iterator i;
81
82 for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++)
Adam Tkac95e2fe82010-08-12 13:54:59 +000083 if (*i != secTypeVeNCrypt) /* Do not include VeNCrypt type to avoid loops */
Adam Tkac0c77e512010-07-20 15:10:16 +000084 result.push_back(*i);
85
86 return result;
87}
88
89void Security::EnableSecType(U32 secType)
90{
91 list<U32>::iterator i;
Adam Tkac1d15e2d2010-04-23 14:06:38 +000092
93 for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++)
94 if (*i == secType)
95 return;
96
97 enabledSecTypes.push_back(secType);
98}
99
Adam Tkac0c77e512010-07-20 15:10:16 +0000100bool Security::IsSupported(U32 secType)
Adam Tkac1d15e2d2010-04-23 14:06:38 +0000101{
Adam Tkac0c77e512010-07-20 15:10:16 +0000102 list<U32>::iterator i;
Adam Tkac1d15e2d2010-04-23 14:06:38 +0000103
104 for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++)
105 if (*i == secType)
106 return true;
107
108 return false;
109}
110
Adam Tkaca9a7b4b2011-02-01 14:34:55 +0000111char *Security::ToString(void)
112{
113 list<U32>::iterator i;
114 static char out[128]; /* Should be enough */
115 bool firstpass = true;
116 const char *name;
117
118 memset(out, 0, sizeof(out));
119
120 for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++) {
121 name = secTypeName(*i);
122 if (name[0] == '[') /* Unknown security type */
123 continue;
124
125 if (!firstpass)
126 strncat(out, ",", sizeof(out) - 1);
127 else
128 firstpass = false;
129 strncat(out, name, sizeof(out) - 1);
130 }
131
132 return out;
133}
134
Adam Tkac0c77e512010-07-20 15:10:16 +0000135rdr::U32 rfb::secTypeNum(const char* name)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000136{
137 if (strcasecmp(name, "None") == 0) return secTypeNone;
138 if (strcasecmp(name, "VncAuth") == 0) return secTypeVncAuth;
139 if (strcasecmp(name, "Tight") == 0) return secTypeTight;
140 if (strcasecmp(name, "RA2") == 0) return secTypeRA2;
141 if (strcasecmp(name, "RA2ne") == 0) return secTypeRA2ne;
142 if (strcasecmp(name, "SSPI") == 0) return secTypeSSPI;
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000143 if (strcasecmp(name, "SSPIne") == 0) return secTypeSSPIne;
144 if (strcasecmp(name, "VeNCrypt") == 0) return secTypeVeNCrypt;
Adam Tkacb3e60c62010-07-20 15:10:59 +0000145
146 /* VeNCrypt subtypes */
Adam Tkac957a5ae2010-07-20 15:12:41 +0000147 if (strcasecmp(name, "Plain") == 0) return secTypePlain;
Adam Tkacb3e60c62010-07-20 15:10:59 +0000148 if (strcasecmp(name, "TLSNone") == 0) return secTypeTLSNone;
149 if (strcasecmp(name, "TLSVnc") == 0) return secTypeTLSVnc;
Adam Tkac957a5ae2010-07-20 15:12:41 +0000150 if (strcasecmp(name, "TLSPlain") == 0) return secTypeTLSPlain;
Adam Tkacb3e60c62010-07-20 15:10:59 +0000151 if (strcasecmp(name, "X509None") == 0) return secTypeX509None;
152 if (strcasecmp(name, "X509Vnc") == 0) return secTypeX509Vnc;
Adam Tkacb3e60c62010-07-20 15:10:59 +0000153 if (strcasecmp(name, "X509Plain") == 0) return secTypeX509Plain;
Adam Tkacb3e60c62010-07-20 15:10:59 +0000154
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000155 return secTypeInvalid;
156}
157
Adam Tkac0c77e512010-07-20 15:10:16 +0000158const char* rfb::secTypeName(rdr::U32 num)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000159{
160 switch (num) {
161 case secTypeNone: return "None";
162 case secTypeVncAuth: return "VncAuth";
163 case secTypeTight: return "Tight";
164 case secTypeRA2: return "RA2";
165 case secTypeRA2ne: return "RA2ne";
166 case secTypeSSPI: return "SSPI";
167 case secTypeSSPIne: return "SSPIne";
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000168 case secTypeVeNCrypt: return "VeNCrypt";
Adam Tkacb3e60c62010-07-20 15:10:59 +0000169
170 /* VeNCrypt subtypes */
Adam Tkac957a5ae2010-07-20 15:12:41 +0000171 case secTypePlain: return "Plain";
Adam Tkacb3e60c62010-07-20 15:10:59 +0000172 case secTypeTLSNone: return "TLSNone";
173 case secTypeTLSVnc: return "TLSVnc";
Adam Tkacb3e60c62010-07-20 15:10:59 +0000174 case secTypeTLSPlain: return "TLSPlain";
175 case secTypeX509None: return "X509None";
176 case secTypeX509Vnc: return "X509Vnc";
177 case secTypeX509Plain: return "X509Plain";
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000178 default: return "[unknown secType]";
179 }
180}
181
Adam Tkac0c77e512010-07-20 15:10:16 +0000182std::list<rdr::U32> rfb::parseSecTypes(const char* types_)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000183{
Adam Tkac0c77e512010-07-20 15:10:16 +0000184 std::list<rdr::U32> result;
Adam Tkacd36b6262009-09-04 10:57:20 +0000185 CharArray types(strDup(types_)), type;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000186 while (types.buf) {
187 strSplit(types.buf, ',', &type.buf, &types.buf);
Adam Tkac0c77e512010-07-20 15:10:16 +0000188 rdr::U32 typeNum = secTypeNum(type.buf);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000189 if (typeNum != secTypeInvalid)
190 result.push_back(typeNum);
191 }
192 return result;
193}