blob: 7049439da96441194fa344685c12d8bb7858b051 [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 Tkacb10489b2010-04-23 14:16:04 +000031#include <rfb/CSecurityVeNCrypt.h>
Adam Tkacc210e8a2010-04-23 14:09:16 +000032#include <rfb/CSecurityVncAuth.h>
Adam Tkac1d15e2d2010-04-23 14:06:38 +000033#include <rdr/Exception.h>
34#include <rfb/LogWriter.h>
Adam Tkacb6eb3992010-04-23 14:05:00 +000035#include <rfb/Security.h>
Adam Tkac1d15e2d2010-04-23 14:06:38 +000036#include <rfb/SSecurityNone.h>
Adam Tkac1d15e2d2010-04-23 14:06:38 +000037#include <rfb/SSecurityVncAuth.h>
Adam Tkacdf799702010-04-28 15:45:53 +000038#ifdef HAVE_GNUTLS
Adam Tkacdfe19cf2010-04-23 14:14:11 +000039#include <rfb/SSecurityVeNCrypt.h>
Adam Tkacdf799702010-04-28 15:45:53 +000040#endif
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000041#include <rfb/util.h>
42
Adam Tkac1d15e2d2010-04-23 14:06:38 +000043using namespace rdr;
44using namespace rfb;
45using namespace std;
46
47static LogWriter vlog("Security");
48
Adam Tkacb10489b2010-04-23 14:16:04 +000049UserPasswdGetter *CSecurity::upg = NULL;
50
Adam Tkaca6578bf2010-04-23 14:07:41 +000051StringParameter Security::secTypes
52("SecurityTypes",
53 "Specify which security scheme to use (None, VncAuth)",
Adam Tkacb10489b2010-04-23 14:16:04 +000054 "VncAuth");
Adam Tkaca6578bf2010-04-23 14:07:41 +000055
Adam Tkacb10489b2010-04-23 14:16:04 +000056Security::Security(void)
Adam Tkac1d15e2d2010-04-23 14:06:38 +000057{
Adam Tkaca6578bf2010-04-23 14:07:41 +000058 char *secTypesStr = secTypes.getData();
Adam Tkac1d15e2d2010-04-23 14:06:38 +000059
60 enabledSecTypes = parseSecTypes(secTypesStr);
61
62 delete secTypesStr;
63}
64
65void Security::EnableSecType(U8 secType)
66{
67 list<U8>::iterator i;
68
69 for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++)
70 if (*i == secType)
71 return;
72
73 enabledSecTypes.push_back(secType);
74}
75
76bool Security::IsSupported(U8 secType)
77{
78 list<U8>::iterator i;
79
80 for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++)
81 if (*i == secType)
82 return true;
83
84 return false;
85}
86
87SSecurity* Security::GetSSecurity(U8 secType)
88{
89 if (!IsSupported(secType))
90 goto bail;
91
92 switch (secType) {
93 case secTypeNone: return new SSecurityNone();
94 case secTypeVncAuth: return new SSecurityVncAuth();
Adam Tkacdf799702010-04-28 15:45:53 +000095#ifdef HAVE_GNUTLS
Adam Tkacdfe19cf2010-04-23 14:14:11 +000096 case secTypeVeNCrypt: return new SSecurityVeNCrypt();
Adam Tkacdf799702010-04-28 15:45:53 +000097#endif
Adam Tkacc210e8a2010-04-23 14:09:16 +000098 }
99
100bail:
101 throw Exception("Security type not supported");
102}
103
Adam Tkacb10489b2010-04-23 14:16:04 +0000104CSecurity* Security::GetCSecurity(U8 secType)
Adam Tkacc210e8a2010-04-23 14:09:16 +0000105{
Adam Tkacb10489b2010-04-23 14:16:04 +0000106 assert (CSecurity::upg != NULL); /* (upg == NULL) means bug in the viewer */
Adam Tkacc210e8a2010-04-23 14:09:16 +0000107
108 if (!IsSupported(secType))
109 goto bail;
110
111 switch (secType) {
112 case secTypeNone: return new CSecurityNone();
Adam Tkacb10489b2010-04-23 14:16:04 +0000113 case secTypeVncAuth: return new CSecurityVncAuth();
Adam Tkacdf799702010-04-28 15:45:53 +0000114#ifdef HAVE_GNUTLS
Adam Tkacb10489b2010-04-23 14:16:04 +0000115 case secTypeVeNCrypt: return new CSecurityVeNCrypt();
Adam Tkacdf799702010-04-28 15:45:53 +0000116#endif
Adam Tkac1d15e2d2010-04-23 14:06:38 +0000117 }
118
119bail:
120 throw Exception("Security type not supported");
121}
122
Adam Tkac94d88c12010-04-23 13:59:52 +0000123rdr::U8 rfb::secTypeNum(const char* name)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000124{
125 if (strcasecmp(name, "None") == 0) return secTypeNone;
126 if (strcasecmp(name, "VncAuth") == 0) return secTypeVncAuth;
127 if (strcasecmp(name, "Tight") == 0) return secTypeTight;
128 if (strcasecmp(name, "RA2") == 0) return secTypeRA2;
129 if (strcasecmp(name, "RA2ne") == 0) return secTypeRA2ne;
130 if (strcasecmp(name, "SSPI") == 0) return secTypeSSPI;
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000131 if (strcasecmp(name, "SSPIne") == 0) return secTypeSSPIne;
132 if (strcasecmp(name, "VeNCrypt") == 0) return secTypeVeNCrypt;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000133 return secTypeInvalid;
134}
135
Adam Tkac94d88c12010-04-23 13:59:52 +0000136const char* rfb::secTypeName(rdr::U8 num)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000137{
138 switch (num) {
139 case secTypeNone: return "None";
140 case secTypeVncAuth: return "VncAuth";
141 case secTypeTight: return "Tight";
142 case secTypeRA2: return "RA2";
143 case secTypeRA2ne: return "RA2ne";
144 case secTypeSSPI: return "SSPI";
145 case secTypeSSPIne: return "SSPIne";
Adam Tkacdfe19cf2010-04-23 14:14:11 +0000146 case secTypeVeNCrypt: return "VeNCrypt";
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000147 default: return "[unknown secType]";
148 }
149}
150
Adam Tkac94d88c12010-04-23 13:59:52 +0000151std::list<rdr::U8> rfb::parseSecTypes(const char* types_)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000152{
Adam Tkac94d88c12010-04-23 13:59:52 +0000153 std::list<rdr::U8> result;
Adam Tkacd36b6262009-09-04 10:57:20 +0000154 CharArray types(strDup(types_)), type;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000155 while (types.buf) {
156 strSplit(types.buf, ',', &type.buf, &types.buf);
Adam Tkac94d88c12010-04-23 13:59:52 +0000157 rdr::U8 typeNum = secTypeNum(type.buf);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000158 if (typeNum != secTypeInvalid)
159 result.push_back(typeNum);
160 }
161 return result;
162}