blob: c6ab41076eb7266bc3fb6f783078df7b9a353fe6 [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>
43#include <rfb/SSecurityTLS.h>
Adam Tkac5bf73fb2010-07-21 09:08:24 +000044#include <rfb/SSecurityX509.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:
127 return new SSecurityStack(secTypeTLSNone, new SSecurityTLS());
128 case secTypeTLSVnc:
129 return new SSecurityStack(secTypeTLSVnc, new SSecurityTLS(), new SSecurityVncAuth());
Adam Tkac5bf73fb2010-07-21 09:08:24 +0000130 case secTypeX509None:
131 return new SSecurityStack(secTypeX509None, new SSecurityX509());
132 case secTypeX509Vnc:
133 return new SSecurityStack(secTypeX509None, new SSecurityX509(), 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 Tkacdf799702010-04-28 15:45:53 +0000159#endif
Adam Tkac1d15e2d2010-04-23 14:06:38 +0000160 }
161
162bail:
163 throw Exception("Security type not supported");
164}
165
Adam Tkac0c77e512010-07-20 15:10:16 +0000166rdr::U32 rfb::secTypeNum(const char* name)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000167{
168 if (strcasecmp(name, "None") == 0) return secTypeNone;
169 if (strcasecmp(name, "VncAuth") == 0) return secTypeVncAuth;
170 if (strcasecmp(name, "Tight") == 0) return secTypeTight;
171 if (strcasecmp(name, "RA2") == 0) return secTypeRA2;
172 if (strcasecmp(name, "RA2ne") == 0) return secTypeRA2ne;
173 if (strcasecmp(name, "SSPI") == 0) return secTypeSSPI;
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000174 if (strcasecmp(name, "SSPIne") == 0) return secTypeSSPIne;
175 if (strcasecmp(name, "VeNCrypt") == 0) return secTypeVeNCrypt;
Adam Tkacb3e60c62010-07-20 15:10:59 +0000176
177 /* VeNCrypt subtypes */
Adam Tkac957a5ae2010-07-20 15:12:41 +0000178 if (strcasecmp(name, "Plain") == 0) return secTypePlain;
Adam Tkacb3e60c62010-07-20 15:10:59 +0000179 if (strcasecmp(name, "TLSNone") == 0) return secTypeTLSNone;
180 if (strcasecmp(name, "TLSVnc") == 0) return secTypeTLSVnc;
Adam Tkac957a5ae2010-07-20 15:12:41 +0000181 if (strcasecmp(name, "TLSPlain") == 0) return secTypeTLSPlain;
Adam Tkacb3e60c62010-07-20 15:10:59 +0000182 if (strcasecmp(name, "X509None") == 0) return secTypeX509None;
183 if (strcasecmp(name, "X509Vnc") == 0) return secTypeX509Vnc;
Adam Tkacb3e60c62010-07-20 15:10:59 +0000184 if (strcasecmp(name, "X509Plain") == 0) return secTypeX509Plain;
Adam Tkacb3e60c62010-07-20 15:10:59 +0000185
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000186 return secTypeInvalid;
187}
188
Adam Tkac0c77e512010-07-20 15:10:16 +0000189const char* rfb::secTypeName(rdr::U32 num)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000190{
191 switch (num) {
192 case secTypeNone: return "None";
193 case secTypeVncAuth: return "VncAuth";
194 case secTypeTight: return "Tight";
195 case secTypeRA2: return "RA2";
196 case secTypeRA2ne: return "RA2ne";
197 case secTypeSSPI: return "SSPI";
198 case secTypeSSPIne: return "SSPIne";
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000199 case secTypeVeNCrypt: return "VeNCrypt";
Adam Tkacb3e60c62010-07-20 15:10:59 +0000200
201 /* VeNCrypt subtypes */
Adam Tkac957a5ae2010-07-20 15:12:41 +0000202 case secTypePlain: return "Plain";
Adam Tkacb3e60c62010-07-20 15:10:59 +0000203 case secTypeTLSNone: return "TLSNone";
204 case secTypeTLSVnc: return "TLSVnc";
Adam Tkacb3e60c62010-07-20 15:10:59 +0000205 case secTypeTLSPlain: return "TLSPlain";
206 case secTypeX509None: return "X509None";
207 case secTypeX509Vnc: return "X509Vnc";
208 case secTypeX509Plain: return "X509Plain";
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000209 default: return "[unknown secType]";
210 }
211}
212
Adam Tkac0c77e512010-07-20 15:10:16 +0000213std::list<rdr::U32> rfb::parseSecTypes(const char* types_)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000214{
Adam Tkac0c77e512010-07-20 15:10:16 +0000215 std::list<rdr::U32> result;
Adam Tkacd36b6262009-09-04 10:57:20 +0000216 CharArray types(strDup(types_)), type;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000217 while (types.buf) {
218 strSplit(types.buf, ',', &type.buf, &types.buf);
Adam Tkac0c77e512010-07-20 15:10:16 +0000219 rdr::U32 typeNum = secTypeNum(type.buf);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000220 if (typeNum != secTypeInvalid)
221 result.push_back(typeNum);
222 }
223 return result;
224}