blob: daf3ac5654d87dc51808e8986e9d8a1f3adfc2c7 [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>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000031#include <rfb/util.h>
32
Adam Tkac1d15e2d2010-04-23 14:06:38 +000033using namespace rdr;
34using namespace rfb;
35using namespace std;
36
37static LogWriter vlog("Security");
38
Adam Tkaca6578bf2010-04-23 14:07:41 +000039StringParameter Security::secTypes
40("SecurityTypes",
41 "Specify which security scheme to use (None, VncAuth)",
42 "VncAuth");
43
Adam Tkacc210e8a2010-04-23 14:09:16 +000044Security::Security(void) : upg(NULL)
Adam Tkac1d15e2d2010-04-23 14:06:38 +000045{
Adam Tkaca6578bf2010-04-23 14:07:41 +000046 char *secTypesStr = secTypes.getData();
Adam Tkac1d15e2d2010-04-23 14:06:38 +000047
48 enabledSecTypes = parseSecTypes(secTypesStr);
49
50 delete secTypesStr;
51}
52
53void Security::EnableSecType(U8 secType)
54{
55 list<U8>::iterator i;
56
57 for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++)
58 if (*i == secType)
59 return;
60
61 enabledSecTypes.push_back(secType);
62}
63
64bool Security::IsSupported(U8 secType)
65{
66 list<U8>::iterator i;
67
68 for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++)
69 if (*i == secType)
70 return true;
71
72 return false;
73}
74
75SSecurity* Security::GetSSecurity(U8 secType)
76{
77 if (!IsSupported(secType))
78 goto bail;
79
80 switch (secType) {
81 case secTypeNone: return new SSecurityNone();
82 case secTypeVncAuth: return new SSecurityVncAuth();
Adam Tkacc210e8a2010-04-23 14:09:16 +000083 }
84
85bail:
86 throw Exception("Security type not supported");
87}
88
89CSecurity* Security::GetCSecurity(rdr::U8 secType)
90{
91 assert (upg != NULL); /* (upg == NULL) means bug in the viewer */
92
93 if (!IsSupported(secType))
94 goto bail;
95
96 switch (secType) {
97 case secTypeNone: return new CSecurityNone();
98 case secTypeVncAuth: return new CSecurityVncAuth(upg);
Adam Tkac1d15e2d2010-04-23 14:06:38 +000099 }
100
101bail:
102 throw Exception("Security type not supported");
103}
104
Adam Tkac94d88c12010-04-23 13:59:52 +0000105rdr::U8 rfb::secTypeNum(const char* name)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000106{
107 if (strcasecmp(name, "None") == 0) return secTypeNone;
108 if (strcasecmp(name, "VncAuth") == 0) return secTypeVncAuth;
109 if (strcasecmp(name, "Tight") == 0) return secTypeTight;
110 if (strcasecmp(name, "RA2") == 0) return secTypeRA2;
111 if (strcasecmp(name, "RA2ne") == 0) return secTypeRA2ne;
112 if (strcasecmp(name, "SSPI") == 0) return secTypeSSPI;
113 if (strcasecmp(name, "SSPIne") == 0) return secTypeSSPIne;
114 return secTypeInvalid;
115}
116
Adam Tkac94d88c12010-04-23 13:59:52 +0000117const char* rfb::secTypeName(rdr::U8 num)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000118{
119 switch (num) {
120 case secTypeNone: return "None";
121 case secTypeVncAuth: return "VncAuth";
122 case secTypeTight: return "Tight";
123 case secTypeRA2: return "RA2";
124 case secTypeRA2ne: return "RA2ne";
125 case secTypeSSPI: return "SSPI";
126 case secTypeSSPIne: return "SSPIne";
127 default: return "[unknown secType]";
128 }
129}
130
Adam Tkac94d88c12010-04-23 13:59:52 +0000131std::list<rdr::U8> rfb::parseSecTypes(const char* types_)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000132{
Adam Tkac94d88c12010-04-23 13:59:52 +0000133 std::list<rdr::U8> result;
Adam Tkacd36b6262009-09-04 10:57:20 +0000134 CharArray types(strDup(types_)), type;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000135 while (types.buf) {
136 strSplit(types.buf, ',', &type.buf, &types.buf);
Adam Tkac94d88c12010-04-23 13:59:52 +0000137 rdr::U8 typeNum = secTypeNum(type.buf);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000138 if (typeNum != secTypeInvalid)
139 result.push_back(typeNum);
140 }
141 return result;
142}