blob: 71e45de831bbabc91ec3843545ba9fb9c6732b71 [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 Tkac1d15e2d2010-04-23 14:06:38 +000034#include <rdr/Exception.h>
35#include <rfb/LogWriter.h>
Adam Tkacb6eb3992010-04-23 14:05:00 +000036#include <rfb/Security.h>
Adam Tkac1d15e2d2010-04-23 14:06:38 +000037#include <rfb/SSecurityNone.h>
Adam Tkac707d3612010-07-20 15:16:10 +000038#include <rfb/SSecurityStack.h>
Adam Tkac1d15e2d2010-04-23 14:06:38 +000039#include <rfb/SSecurityVncAuth.h>
Adam Tkacdfe19cf2010-04-23 14:14:11 +000040#include <rfb/SSecurityVeNCrypt.h>
Adam Tkac707d3612010-07-20 15:16:10 +000041#ifdef HAVE_GNUTLS
Adam Tkac3c5be392010-07-21 09:27:34 +000042#include <rfb/CSecurityTLS.h>
Adam Tkac21b61a52010-07-21 09:19:00 +000043#include <rfb/SSecurityTLS.h>
Adam Tkacdf799702010-04-28 15:45:53 +000044#endif
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000045#include <rfb/util.h>
46
Adam Tkac1d15e2d2010-04-23 14:06:38 +000047using namespace rdr;
48using namespace rfb;
49using namespace std;
50
51static LogWriter vlog("Security");
52
Adam Tkacb10489b2010-04-23 14:16:04 +000053UserPasswdGetter *CSecurity::upg = NULL;
54
Adam Tkacfb993152010-08-12 14:17:28 +000055StringParameter Security::secTypesViewer
Adam Tkaca6578bf2010-04-23 14:07:41 +000056("SecurityTypes",
57 "Specify which security scheme to use (None, VncAuth)",
Adam Tkac80fb7ef2010-08-12 14:24:43 +000058#ifdef HAVE_GNUTLS
59 "VeNCrypt,X509Vnc,TLSVnc,X509None,TLSNone,VncAuth,None",
60#else
61 "VncAuth,None",
62#endif
63ConfViewer);
Adam Tkaca6578bf2010-04-23 14:07:41 +000064
Adam Tkacfb993152010-08-12 14:17:28 +000065StringParameter Security::secTypesServer
66("SecurityTypes",
67 "Specify which security scheme to use (None, VncAuth)",
Adam Tkac80fb7ef2010-08-12 14:24:43 +000068#ifdef HAVE_GNUTLS
69 "VeNCrypt,TLSVnc,VncAuth",
70#else
71 "VncAuth",
72#endif
73ConfServer);
Adam Tkacfb993152010-08-12 14:17:28 +000074
75Security::Security(SecurityClassType secClassType)
Adam Tkac1d15e2d2010-04-23 14:06:38 +000076{
Adam Tkacfb993152010-08-12 14:17:28 +000077 char *secTypesStr;
78
79 switch (secClassType) {
80 case SecurityViewer:
81 secTypesStr = secTypesViewer.getData();
82 break;
83 case SecurityServer:
84 secTypesStr = secTypesServer.getData();
85 break;
86 };
Adam Tkac1d15e2d2010-04-23 14:06:38 +000087
88 enabledSecTypes = parseSecTypes(secTypesStr);
89
90 delete secTypesStr;
91}
92
Adam Tkac0c77e512010-07-20 15:10:16 +000093const std::list<rdr::U8> Security::GetEnabledSecTypes(void)
Adam Tkac1d15e2d2010-04-23 14:06:38 +000094{
Adam Tkac0c77e512010-07-20 15:10:16 +000095 list<rdr::U8> result;
96 list<U32>::iterator i;
97
98 for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++)
99 if (*i < 0x100)
100 result.push_back(*i);
101
102 return result;
103}
104
105const std::list<rdr::U32> Security::GetEnabledExtSecTypes(void)
106{
107 list<rdr::U32> result;
108 list<U32>::iterator i;
109
110 for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++)
Adam Tkac95e2fe82010-08-12 13:54:59 +0000111 if (*i != secTypeVeNCrypt) /* Do not include VeNCrypt type to avoid loops */
Adam Tkac0c77e512010-07-20 15:10:16 +0000112 result.push_back(*i);
113
114 return result;
115}
116
117void Security::EnableSecType(U32 secType)
118{
119 list<U32>::iterator i;
Adam Tkac1d15e2d2010-04-23 14:06:38 +0000120
121 for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++)
122 if (*i == secType)
123 return;
124
125 enabledSecTypes.push_back(secType);
126}
127
Adam Tkac0c77e512010-07-20 15:10:16 +0000128bool Security::IsSupported(U32 secType)
Adam Tkac1d15e2d2010-04-23 14:06:38 +0000129{
Adam Tkac0c77e512010-07-20 15:10:16 +0000130 list<U32>::iterator i;
Adam Tkac1d15e2d2010-04-23 14:06:38 +0000131
132 for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++)
133 if (*i == secType)
134 return true;
135
136 return false;
137}
138
Adam Tkac0c77e512010-07-20 15:10:16 +0000139SSecurity* Security::GetSSecurity(U32 secType)
Adam Tkac1d15e2d2010-04-23 14:06:38 +0000140{
141 if (!IsSupported(secType))
142 goto bail;
143
144 switch (secType) {
145 case secTypeNone: return new SSecurityNone();
146 case secTypeVncAuth: return new SSecurityVncAuth();
Adam Tkaca0325932010-07-20 15:14:08 +0000147 case secTypeVeNCrypt: return new SSecurityVeNCrypt(this);
Adam Tkac707d3612010-07-20 15:16:10 +0000148#ifdef HAVE_GNUTLS
149 case secTypeTLSNone:
Adam Tkac21b61a52010-07-21 09:19:00 +0000150 return new SSecurityStack(secTypeTLSNone, new SSecurityTLS(true));
Adam Tkac707d3612010-07-20 15:16:10 +0000151 case secTypeTLSVnc:
Adam Tkac21b61a52010-07-21 09:19:00 +0000152 return new SSecurityStack(secTypeTLSVnc, new SSecurityTLS(true), new SSecurityVncAuth());
Adam Tkac5bf73fb2010-07-21 09:08:24 +0000153 case secTypeX509None:
Adam Tkac21b61a52010-07-21 09:19:00 +0000154 return new SSecurityStack(secTypeX509None, new SSecurityTLS(false));
Adam Tkac5bf73fb2010-07-21 09:08:24 +0000155 case secTypeX509Vnc:
Adam Tkac21b61a52010-07-21 09:19:00 +0000156 return new SSecurityStack(secTypeX509None, new SSecurityTLS(false), new SSecurityVncAuth());
Adam Tkacdf799702010-04-28 15:45:53 +0000157#endif
Adam Tkacc210e8a2010-04-23 14:09:16 +0000158 }
159
160bail:
161 throw Exception("Security type not supported");
162}
163
Adam Tkac0c77e512010-07-20 15:10:16 +0000164CSecurity* Security::GetCSecurity(U32 secType)
Adam Tkacc210e8a2010-04-23 14:09:16 +0000165{
Adam Tkacb10489b2010-04-23 14:16:04 +0000166 assert (CSecurity::upg != NULL); /* (upg == NULL) means bug in the viewer */
Adam Tkacc210e8a2010-04-23 14:09:16 +0000167
168 if (!IsSupported(secType))
169 goto bail;
170
171 switch (secType) {
172 case secTypeNone: return new CSecurityNone();
Adam Tkacb10489b2010-04-23 14:16:04 +0000173 case secTypeVncAuth: return new CSecurityVncAuth();
Adam Tkaca0325932010-07-20 15:14:08 +0000174 case secTypeVeNCrypt: return new CSecurityVeNCrypt(this);
Adam Tkac707d3612010-07-20 15:16:10 +0000175#ifdef HAVE_GNUTLS
176 case secTypeTLSNone:
177 return new CSecurityStack(secTypeTLSNone, "TLS with no password",
Adam Tkac3c5be392010-07-21 09:27:34 +0000178 new CSecurityTLS(true));
Adam Tkac707d3612010-07-20 15:16:10 +0000179 case secTypeTLSVnc:
180 return new CSecurityStack(secTypeTLSVnc, "TLS with VNCAuth",
Adam Tkac3c5be392010-07-21 09:27:34 +0000181 new CSecurityTLS(true), new CSecurityVncAuth());
Adam Tkacf5f6a002010-07-21 09:09:19 +0000182 case secTypeX509None:
183 return new CSecurityStack(secTypeX509None, "X509 with no password",
Adam Tkac3c5be392010-07-21 09:27:34 +0000184 new CSecurityTLS(false));
Adam Tkacf5f6a002010-07-21 09:09:19 +0000185 case secTypeX509Vnc:
186 return new CSecurityStack(secTypeX509None, "X509 with VNCAuth",
Adam Tkac3c5be392010-07-21 09:27:34 +0000187 new CSecurityTLS(false), new CSecurityVncAuth());
Adam Tkacdf799702010-04-28 15:45:53 +0000188#endif
Adam Tkac1d15e2d2010-04-23 14:06:38 +0000189 }
190
191bail:
192 throw Exception("Security type not supported");
193}
194
Adam Tkac0c77e512010-07-20 15:10:16 +0000195rdr::U32 rfb::secTypeNum(const char* name)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000196{
197 if (strcasecmp(name, "None") == 0) return secTypeNone;
198 if (strcasecmp(name, "VncAuth") == 0) return secTypeVncAuth;
199 if (strcasecmp(name, "Tight") == 0) return secTypeTight;
200 if (strcasecmp(name, "RA2") == 0) return secTypeRA2;
201 if (strcasecmp(name, "RA2ne") == 0) return secTypeRA2ne;
202 if (strcasecmp(name, "SSPI") == 0) return secTypeSSPI;
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000203 if (strcasecmp(name, "SSPIne") == 0) return secTypeSSPIne;
204 if (strcasecmp(name, "VeNCrypt") == 0) return secTypeVeNCrypt;
Adam Tkacb3e60c62010-07-20 15:10:59 +0000205
206 /* VeNCrypt subtypes */
Adam Tkac957a5ae2010-07-20 15:12:41 +0000207 if (strcasecmp(name, "Plain") == 0) return secTypePlain;
Adam Tkacb3e60c62010-07-20 15:10:59 +0000208 if (strcasecmp(name, "TLSNone") == 0) return secTypeTLSNone;
209 if (strcasecmp(name, "TLSVnc") == 0) return secTypeTLSVnc;
Adam Tkac957a5ae2010-07-20 15:12:41 +0000210 if (strcasecmp(name, "TLSPlain") == 0) return secTypeTLSPlain;
Adam Tkacb3e60c62010-07-20 15:10:59 +0000211 if (strcasecmp(name, "X509None") == 0) return secTypeX509None;
212 if (strcasecmp(name, "X509Vnc") == 0) return secTypeX509Vnc;
Adam Tkacb3e60c62010-07-20 15:10:59 +0000213 if (strcasecmp(name, "X509Plain") == 0) return secTypeX509Plain;
Adam Tkacb3e60c62010-07-20 15:10:59 +0000214
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000215 return secTypeInvalid;
216}
217
Adam Tkac0c77e512010-07-20 15:10:16 +0000218const char* rfb::secTypeName(rdr::U32 num)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000219{
220 switch (num) {
221 case secTypeNone: return "None";
222 case secTypeVncAuth: return "VncAuth";
223 case secTypeTight: return "Tight";
224 case secTypeRA2: return "RA2";
225 case secTypeRA2ne: return "RA2ne";
226 case secTypeSSPI: return "SSPI";
227 case secTypeSSPIne: return "SSPIne";
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000228 case secTypeVeNCrypt: return "VeNCrypt";
Adam Tkacb3e60c62010-07-20 15:10:59 +0000229
230 /* VeNCrypt subtypes */
Adam Tkac957a5ae2010-07-20 15:12:41 +0000231 case secTypePlain: return "Plain";
Adam Tkacb3e60c62010-07-20 15:10:59 +0000232 case secTypeTLSNone: return "TLSNone";
233 case secTypeTLSVnc: return "TLSVnc";
Adam Tkacb3e60c62010-07-20 15:10:59 +0000234 case secTypeTLSPlain: return "TLSPlain";
235 case secTypeX509None: return "X509None";
236 case secTypeX509Vnc: return "X509Vnc";
237 case secTypeX509Plain: return "X509Plain";
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000238 default: return "[unknown secType]";
239 }
240}
241
Adam Tkac0c77e512010-07-20 15:10:16 +0000242std::list<rdr::U32> rfb::parseSecTypes(const char* types_)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000243{
Adam Tkac0c77e512010-07-20 15:10:16 +0000244 std::list<rdr::U32> result;
Adam Tkacd36b6262009-09-04 10:57:20 +0000245 CharArray types(strDup(types_)), type;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000246 while (types.buf) {
247 strSplit(types.buf, ',', &type.buf, &types.buf);
Adam Tkac0c77e512010-07-20 15:10:16 +0000248 rdr::U32 typeNum = secTypeNum(type.buf);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000249 if (typeNum != secTypeInvalid)
250 result.push_back(typeNum);
251 }
252 return result;
253}