blob: d2f40bece49c9b47aab41f06fbf66fc831d4e17e [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
42#include <rfb/CSecurityTLS.h>
Adam Tkacf5f6a002010-07-21 09:09:19 +000043#include <rfb/CSecurityX509.h>
Adam Tkac21b61a52010-07-21 09:19:00 +000044#include <rfb/SSecurityTLS.h>
Adam Tkacdf799702010-04-28 15:45:53 +000045#endif
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000046#include <rfb/util.h>
47
Adam Tkac1d15e2d2010-04-23 14:06:38 +000048using namespace rdr;
49using namespace rfb;
50using namespace std;
51
52static LogWriter vlog("Security");
53
Adam Tkacb10489b2010-04-23 14:16:04 +000054UserPasswdGetter *CSecurity::upg = NULL;
55
Adam Tkaca6578bf2010-04-23 14:07:41 +000056StringParameter Security::secTypes
57("SecurityTypes",
58 "Specify which security scheme to use (None, VncAuth)",
Adam Tkacb10489b2010-04-23 14:16:04 +000059 "VncAuth");
Adam Tkaca6578bf2010-04-23 14:07:41 +000060
Adam Tkacb10489b2010-04-23 14:16:04 +000061Security::Security(void)
Adam Tkac1d15e2d2010-04-23 14:06:38 +000062{
Adam Tkaca6578bf2010-04-23 14:07:41 +000063 char *secTypesStr = secTypes.getData();
Adam Tkac1d15e2d2010-04-23 14:06:38 +000064
65 enabledSecTypes = parseSecTypes(secTypesStr);
66
67 delete secTypesStr;
68}
69
Adam Tkac0c77e512010-07-20 15:10:16 +000070const std::list<rdr::U8> Security::GetEnabledSecTypes(void)
Adam Tkac1d15e2d2010-04-23 14:06:38 +000071{
Adam Tkac0c77e512010-07-20 15:10:16 +000072 list<rdr::U8> result;
73 list<U32>::iterator i;
74
75 for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++)
76 if (*i < 0x100)
77 result.push_back(*i);
78
79 return result;
80}
81
82const std::list<rdr::U32> Security::GetEnabledExtSecTypes(void)
83{
84 list<rdr::U32> result;
85 list<U32>::iterator i;
86
87 for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++)
88 if (*i >= 0x100)
89 result.push_back(*i);
90
91 return result;
92}
93
94void Security::EnableSecType(U32 secType)
95{
96 list<U32>::iterator i;
Adam Tkac1d15e2d2010-04-23 14:06:38 +000097
98 for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++)
99 if (*i == secType)
100 return;
101
102 enabledSecTypes.push_back(secType);
103}
104
Adam Tkac0c77e512010-07-20 15:10:16 +0000105bool Security::IsSupported(U32 secType)
Adam Tkac1d15e2d2010-04-23 14:06:38 +0000106{
Adam Tkac0c77e512010-07-20 15:10:16 +0000107 list<U32>::iterator i;
Adam Tkac1d15e2d2010-04-23 14:06:38 +0000108
109 for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++)
110 if (*i == secType)
111 return true;
112
113 return false;
114}
115
Adam Tkac0c77e512010-07-20 15:10:16 +0000116SSecurity* Security::GetSSecurity(U32 secType)
Adam Tkac1d15e2d2010-04-23 14:06:38 +0000117{
118 if (!IsSupported(secType))
119 goto bail;
120
121 switch (secType) {
122 case secTypeNone: return new SSecurityNone();
123 case secTypeVncAuth: return new SSecurityVncAuth();
Adam Tkaca0325932010-07-20 15:14:08 +0000124 case secTypeVeNCrypt: return new SSecurityVeNCrypt(this);
Adam Tkac707d3612010-07-20 15:16:10 +0000125#ifdef HAVE_GNUTLS
126 case secTypeTLSNone:
Adam Tkac21b61a52010-07-21 09:19:00 +0000127 return new SSecurityStack(secTypeTLSNone, new SSecurityTLS(true));
Adam Tkac707d3612010-07-20 15:16:10 +0000128 case secTypeTLSVnc:
Adam Tkac21b61a52010-07-21 09:19:00 +0000129 return new SSecurityStack(secTypeTLSVnc, new SSecurityTLS(true), new SSecurityVncAuth());
Adam Tkac5bf73fb2010-07-21 09:08:24 +0000130 case secTypeX509None:
Adam Tkac21b61a52010-07-21 09:19:00 +0000131 return new SSecurityStack(secTypeX509None, new SSecurityTLS(false));
Adam Tkac5bf73fb2010-07-21 09:08:24 +0000132 case secTypeX509Vnc:
Adam Tkac21b61a52010-07-21 09:19:00 +0000133 return new SSecurityStack(secTypeX509None, new SSecurityTLS(false), new SSecurityVncAuth());
Adam Tkacdf799702010-04-28 15:45:53 +0000134#endif
Adam Tkacc210e8a2010-04-23 14:09:16 +0000135 }
136
137bail:
138 throw Exception("Security type not supported");
139}
140
Adam Tkac0c77e512010-07-20 15:10:16 +0000141CSecurity* Security::GetCSecurity(U32 secType)
Adam Tkacc210e8a2010-04-23 14:09:16 +0000142{
Adam Tkacb10489b2010-04-23 14:16:04 +0000143 assert (CSecurity::upg != NULL); /* (upg == NULL) means bug in the viewer */
Adam Tkacc210e8a2010-04-23 14:09:16 +0000144
145 if (!IsSupported(secType))
146 goto bail;
147
148 switch (secType) {
149 case secTypeNone: return new CSecurityNone();
Adam Tkacb10489b2010-04-23 14:16:04 +0000150 case secTypeVncAuth: return new CSecurityVncAuth();
Adam Tkaca0325932010-07-20 15:14:08 +0000151 case secTypeVeNCrypt: return new CSecurityVeNCrypt(this);
Adam Tkac707d3612010-07-20 15:16:10 +0000152#ifdef HAVE_GNUTLS
153 case secTypeTLSNone:
154 return new CSecurityStack(secTypeTLSNone, "TLS with no password",
155 new CSecurityTLS());
156 case secTypeTLSVnc:
157 return new CSecurityStack(secTypeTLSVnc, "TLS with VNCAuth",
158 new CSecurityTLS(), new CSecurityVncAuth());
Adam Tkacf5f6a002010-07-21 09:09:19 +0000159 case secTypeX509None:
160 return new CSecurityStack(secTypeX509None, "X509 with no password",
161 new CSecurityX509());
162 case secTypeX509Vnc:
163 return new CSecurityStack(secTypeX509None, "X509 with VNCAuth",
164 new CSecurityX509(), new CSecurityVncAuth());
Adam Tkacdf799702010-04-28 15:45:53 +0000165#endif
Adam Tkac1d15e2d2010-04-23 14:06:38 +0000166 }
167
168bail:
169 throw Exception("Security type not supported");
170}
171
Adam Tkac0c77e512010-07-20 15:10:16 +0000172rdr::U32 rfb::secTypeNum(const char* name)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000173{
174 if (strcasecmp(name, "None") == 0) return secTypeNone;
175 if (strcasecmp(name, "VncAuth") == 0) return secTypeVncAuth;
176 if (strcasecmp(name, "Tight") == 0) return secTypeTight;
177 if (strcasecmp(name, "RA2") == 0) return secTypeRA2;
178 if (strcasecmp(name, "RA2ne") == 0) return secTypeRA2ne;
179 if (strcasecmp(name, "SSPI") == 0) return secTypeSSPI;
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000180 if (strcasecmp(name, "SSPIne") == 0) return secTypeSSPIne;
181 if (strcasecmp(name, "VeNCrypt") == 0) return secTypeVeNCrypt;
Adam Tkacb3e60c62010-07-20 15:10:59 +0000182
183 /* VeNCrypt subtypes */
Adam Tkac957a5ae2010-07-20 15:12:41 +0000184 if (strcasecmp(name, "Plain") == 0) return secTypePlain;
Adam Tkacb3e60c62010-07-20 15:10:59 +0000185 if (strcasecmp(name, "TLSNone") == 0) return secTypeTLSNone;
186 if (strcasecmp(name, "TLSVnc") == 0) return secTypeTLSVnc;
Adam Tkac957a5ae2010-07-20 15:12:41 +0000187 if (strcasecmp(name, "TLSPlain") == 0) return secTypeTLSPlain;
Adam Tkacb3e60c62010-07-20 15:10:59 +0000188 if (strcasecmp(name, "X509None") == 0) return secTypeX509None;
189 if (strcasecmp(name, "X509Vnc") == 0) return secTypeX509Vnc;
Adam Tkacb3e60c62010-07-20 15:10:59 +0000190 if (strcasecmp(name, "X509Plain") == 0) return secTypeX509Plain;
Adam Tkacb3e60c62010-07-20 15:10:59 +0000191
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000192 return secTypeInvalid;
193}
194
Adam Tkac0c77e512010-07-20 15:10:16 +0000195const char* rfb::secTypeName(rdr::U32 num)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000196{
197 switch (num) {
198 case secTypeNone: return "None";
199 case secTypeVncAuth: return "VncAuth";
200 case secTypeTight: return "Tight";
201 case secTypeRA2: return "RA2";
202 case secTypeRA2ne: return "RA2ne";
203 case secTypeSSPI: return "SSPI";
204 case secTypeSSPIne: return "SSPIne";
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000205 case secTypeVeNCrypt: return "VeNCrypt";
Adam Tkacb3e60c62010-07-20 15:10:59 +0000206
207 /* VeNCrypt subtypes */
Adam Tkac957a5ae2010-07-20 15:12:41 +0000208 case secTypePlain: return "Plain";
Adam Tkacb3e60c62010-07-20 15:10:59 +0000209 case secTypeTLSNone: return "TLSNone";
210 case secTypeTLSVnc: return "TLSVnc";
Adam Tkacb3e60c62010-07-20 15:10:59 +0000211 case secTypeTLSPlain: return "TLSPlain";
212 case secTypeX509None: return "X509None";
213 case secTypeX509Vnc: return "X509Vnc";
214 case secTypeX509Plain: return "X509Plain";
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000215 default: return "[unknown secType]";
216 }
217}
218
Adam Tkac0c77e512010-07-20 15:10:16 +0000219std::list<rdr::U32> rfb::parseSecTypes(const char* types_)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000220{
Adam Tkac0c77e512010-07-20 15:10:16 +0000221 std::list<rdr::U32> result;
Adam Tkacd36b6262009-09-04 10:57:20 +0000222 CharArray types(strDup(types_)), type;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000223 while (types.buf) {
224 strSplit(types.buf, ',', &type.buf, &types.buf);
Adam Tkac0c77e512010-07-20 15:10:16 +0000225 rdr::U32 typeNum = secTypeNum(type.buf);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000226 if (typeNum != secTypeInvalid)
227 result.push_back(typeNum);
228 }
229 return result;
230}