blob: 589eaa669db0692449891d28aa077f79c45cda15 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
Adam Tkac1d15e2d2010-04-23 14:06:38 +000018#include <assert.h>
19#include <stdlib.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000020#include <string.h>
21#ifdef _WIN32
22#define strcasecmp _stricmp
23#endif
Adam Tkacc210e8a2010-04-23 14:09:16 +000024#include <rfb/CSecurityNone.h>
25#include <rfb/CSecurityVncAuth.h>
Adam Tkac1d15e2d2010-04-23 14:06:38 +000026#include <rdr/Exception.h>
27#include <rfb/LogWriter.h>
Adam Tkacb6eb3992010-04-23 14:05:00 +000028#include <rfb/Security.h>
Adam Tkac1d15e2d2010-04-23 14:06:38 +000029#include <rfb/SSecurityNone.h>
Adam Tkac1d15e2d2010-04-23 14:06:38 +000030#include <rfb/SSecurityVncAuth.h>
Adam Tkacdfe19cf2010-04-23 14:14:11 +000031#include <rfb/SSecurityVeNCrypt.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000032#include <rfb/util.h>
33
Adam Tkac1d15e2d2010-04-23 14:06:38 +000034using namespace rdr;
35using namespace rfb;
36using namespace std;
37
38static LogWriter vlog("Security");
39
Adam Tkaca6578bf2010-04-23 14:07:41 +000040StringParameter Security::secTypes
41("SecurityTypes",
42 "Specify which security scheme to use (None, VncAuth)",
Adam Tkacf324dc42010-04-23 14:10:17 +000043 "VncAuth", ConfServer);
Adam Tkaca6578bf2010-04-23 14:07:41 +000044
Adam Tkacc210e8a2010-04-23 14:09:16 +000045Security::Security(void) : upg(NULL)
Adam Tkac1d15e2d2010-04-23 14:06:38 +000046{
Adam Tkaca6578bf2010-04-23 14:07:41 +000047 char *secTypesStr = secTypes.getData();
Adam Tkac1d15e2d2010-04-23 14:06:38 +000048
49 enabledSecTypes = parseSecTypes(secTypesStr);
50
51 delete secTypesStr;
52}
53
54void Security::EnableSecType(U8 secType)
55{
56 list<U8>::iterator i;
57
58 for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++)
59 if (*i == secType)
60 return;
61
62 enabledSecTypes.push_back(secType);
63}
64
65bool Security::IsSupported(U8 secType)
66{
67 list<U8>::iterator i;
68
69 for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++)
70 if (*i == secType)
71 return true;
72
73 return false;
74}
75
76SSecurity* Security::GetSSecurity(U8 secType)
77{
78 if (!IsSupported(secType))
79 goto bail;
80
81 switch (secType) {
82 case secTypeNone: return new SSecurityNone();
83 case secTypeVncAuth: return new SSecurityVncAuth();
Adam Tkacdfe19cf2010-04-23 14:14:11 +000084 case secTypeVeNCrypt: return new SSecurityVeNCrypt();
Adam Tkacc210e8a2010-04-23 14:09:16 +000085 }
86
87bail:
88 throw Exception("Security type not supported");
89}
90
91CSecurity* Security::GetCSecurity(rdr::U8 secType)
92{
93 assert (upg != NULL); /* (upg == NULL) means bug in the viewer */
94
95 if (!IsSupported(secType))
96 goto bail;
97
98 switch (secType) {
99 case secTypeNone: return new CSecurityNone();
100 case secTypeVncAuth: return new CSecurityVncAuth(upg);
Adam Tkac1d15e2d2010-04-23 14:06:38 +0000101 }
102
103bail:
104 throw Exception("Security type not supported");
105}
106
Adam Tkac94d88c12010-04-23 13:59:52 +0000107rdr::U8 rfb::secTypeNum(const char* name)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000108{
109 if (strcasecmp(name, "None") == 0) return secTypeNone;
110 if (strcasecmp(name, "VncAuth") == 0) return secTypeVncAuth;
111 if (strcasecmp(name, "Tight") == 0) return secTypeTight;
112 if (strcasecmp(name, "RA2") == 0) return secTypeRA2;
113 if (strcasecmp(name, "RA2ne") == 0) return secTypeRA2ne;
114 if (strcasecmp(name, "SSPI") == 0) return secTypeSSPI;
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000115 if (strcasecmp(name, "SSPIne") == 0) return secTypeSSPIne;
116 if (strcasecmp(name, "VeNCrypt") == 0) return secTypeVeNCrypt;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000117 return secTypeInvalid;
118}
119
Adam Tkac94d88c12010-04-23 13:59:52 +0000120const char* rfb::secTypeName(rdr::U8 num)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000121{
122 switch (num) {
123 case secTypeNone: return "None";
124 case secTypeVncAuth: return "VncAuth";
125 case secTypeTight: return "Tight";
126 case secTypeRA2: return "RA2";
127 case secTypeRA2ne: return "RA2ne";
128 case secTypeSSPI: return "SSPI";
129 case secTypeSSPIne: return "SSPIne";
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000130 case secTypeVeNCrypt: return "VeNCrypt";
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000131 default: return "[unknown secType]";
132 }
133}
134
Adam Tkac94d88c12010-04-23 13:59:52 +0000135std::list<rdr::U8> rfb::parseSecTypes(const char* types_)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000136{
Adam Tkac94d88c12010-04-23 13:59:52 +0000137 std::list<rdr::U8> result;
Adam Tkacd36b6262009-09-04 10:57:20 +0000138 CharArray types(strDup(types_)), type;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000139 while (types.buf) {
140 strSplit(types.buf, ',', &type.buf, &types.buf);
Adam Tkac94d88c12010-04-23 13:59:52 +0000141 rdr::U8 typeNum = secTypeNum(type.buf);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000142 if (typeNum != secTypeInvalid)
143 result.push_back(typeNum);
144 }
145 return result;
146}