1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
/*
* Copyright (C) 2005-2006 Martin Koegler
* Copyright (C) 2006 OCCAM Financial Technology
* Copyright (C) 2010 TigerVNC Team
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
/*
* SSecurityVeNCrypt
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <rfb/SSecurityVeNCrypt.h>
#include <rfb/Exception.h>
#include <rfb/LogWriter.h>
#include <rdr/InStream.h>
#include <rdr/OutStream.h>
#include <rfb/SSecurityVncAuth.h>
#include <rfb/SSecurityTLS.h>
using namespace rfb;
using namespace rdr;
using namespace std;
static LogWriter vlog("SVeNCrypt");
StringParameter SSecurityVeNCrypt::X509_CertFile
("x509cert",
"specifies path to the x509 certificate in PEM format",
"", ConfServer);
StringParameter SSecurityVeNCrypt::X509_KeyFile
("x509key",
"specifies path to the key of the x509 certificate in PEM format",
"", ConfServer);
StringParameter SSecurityVeNCrypt::secTypesStr
("VeNCryptTypes",
"Specify which security scheme to use for VeNCrypt connections (TLSNone, "
"TLSVnc, TLSPlain, X509None, X509Vnc, X509Plain)",
"TLSVnc,TLSPlain,X509Vnc,X509Plain", ConfServer);
SSecurityVeNCrypt::SSecurityVeNCrypt(void)
{
ssecurityStack = NULL;
haveSentVersion = false;
haveRecvdMajorVersion = false;
haveRecvdMinorVersion = false;
majorVersion = 0;
minorVersion = 0;
haveSentTypes = false;
haveChosenType = false;
chosenType = secTypeVeNCrypt;
numTypes = 0;
subTypes = NULL;
}
SSecurityVeNCrypt::~SSecurityVeNCrypt()
{
if (subTypes) {
delete [] subTypes;
subTypes = NULL;
}
}
bool SSecurityVeNCrypt::processMsg(SConnection* sc)
{
rdr::InStream* is = sc->getInStream();
rdr::OutStream* os = sc->getOutStream();
rdr::U8 i;
/* VeNCrypt initialization */
/* Send the highest version we can support */
if (!haveSentVersion) {
os->writeU8(0);
os->writeU8(2);
haveSentVersion = true;
os->flush();
return false;
}
/* Receive back highest version that client can support (up to and including ours) */
if (!haveRecvdMajorVersion) {
majorVersion = is->readU8();
haveRecvdMajorVersion = true;
return false;
}
if (!haveRecvdMinorVersion) {
minorVersion = is->readU8();
haveRecvdMinorVersion = true;
/* WORD value with major version in upper 8 bits and minor version in lower 8 bits */
U16 Version = (((U16)majorVersion) << 8) | ((U16)minorVersion);
switch (Version) {
case 0x0000: /* 0.0 - The client cannot support us! */
case 0x0001: /* 0.1 Legacy VeNCrypt, not supported */
os->writeU8(0xFF); /* This is not OK */
os->flush();
throw AuthFailureException("The client cannot support the server's "
"VeNCrypt version");
case 0x0002: /* 0.2 */
os->writeU8(0); /* OK */
break;
default:
os->writeU8(0xFF); /* Not OK */
os->flush();
throw AuthFailureException("The client returned an unsupported VeNCrypt version");
}
}
/*
* send number of supported VeNCrypt authentication types (U8) followed
* by authentication types (U32s)
*/
if (!haveSentTypes) {
list<U32> listSubTypes;
SSecurityVeNCrypt::getSecTypes(&listSubTypes);
numTypes = listSubTypes.size();
subTypes = new U32[numTypes];
for (i = 0; i < numTypes; i++) {
subTypes[i] = listSubTypes.front();
listSubTypes.pop_front();
}
if (numTypes) {
os->writeU8(numTypes);
for (i = 0; i < numTypes; i++)
os->writeU32(subTypes[i]);
os->flush();
haveSentTypes = true;
return false;
} else
throw AuthFailureException("There are no VeNCrypt sub-types to send to the client");
}
/* get type back from client (must be one of the ones we sent) */
if (!haveChosenType) {
is->check(4);
chosenType = is->readU32();
for (i = 0; i < numTypes; i++) {
if (chosenType == subTypes[i]) {
haveChosenType = true;
break;
}
}
if (!haveChosenType)
chosenType = secTypeInvalid;
/* Set up the stack according to the chosen type */
switch(chosenType) {
case secTypeTLSNone:
case secTypeTLSVnc:
case secTypeTLSPlain:
case secTypeX509None:
case secTypeX509Vnc:
case secTypeX509Plain:
ssecurityStack = SSecurityVeNCrypt::getSSecurityStack(chosenType);
break;
case secTypeInvalid:
case secTypeVeNCrypt: /* This would cause looping */
default:
throw AuthFailureException("No valid VeNCrypt sub-type");
}
}
/* continue processing the messages */
return ssecurityStack->processMsg(sc);
}
SSecurityStack* SSecurityVeNCrypt::getSSecurityStack(int secType)
{
switch (secType) {
case secTypeTLSNone:
return new SSecurityStack(secTypeTLSNone, new SSecurityTLS());
case secTypeTLSVnc:
return new SSecurityStack(secTypeTLSVnc, new SSecurityTLS(), new SSecurityVncAuth());
#if 0
/* Following types are not implemented, yet */
case secTypeTLSPlain:
case secTypeX509None:
case secTypeX509Vnc:
case secTypeX509Plain:
#endif
default:
throw Exception("Bug in the SSecurityVeNCrypt::getSSecurityStack");
}
}
void SSecurityVeNCrypt::getSecTypes(list<U32>* secTypes)
{
CharArray types;
types.buf = SSecurityVeNCrypt::secTypesStr.getData();
list<U32> configured = SSecurityVeNCrypt::parseSecTypes(types.buf);
list<U32>::iterator i;
for (i = configured.begin(); i != configured.end(); i++)
secTypes->push_back(*i);
}
U32 SSecurityVeNCrypt::secTypeNum(const char *name)
{
if (strcasecmp(name, "TLSNone") == 0)
return secTypeTLSNone;
if (strcasecmp(name, "TLSVnc") == 0)
return secTypeTLSVnc;
if (strcasecmp(name, "TLSPlain") == 0)
return secTypeTLSPlain;
if (strcasecmp(name, "X509None") == 0)
return secTypeX509None;
if (strcasecmp(name, "X509Vnc") == 0)
return secTypeX509Vnc;
if (strcasecmp(name, "X509Plain") == 0)
return secTypeX509Plain;
return secTypeInvalid;
}
char* SSecurityVeNCrypt::secTypeName(U32 num)
{
switch (num) {
case secTypePlain:
return "Plain";
case secTypeTLSNone:
return "TLSNone";
case secTypeTLSVnc:
return "TLSVnc";
case secTypeTLSPlain:
return "TLSPlain";
case secTypeX509None:
return "X509None";
case secTypeX509Vnc:
return "X509Vnc";
case secTypeX509Plain:
return "X509Plain";
default:
return "[unknown secType]";
}
}
list<U32> SSecurityVeNCrypt::parseSecTypes(const char *secTypes)
{
list<U32> result;
CharArray types(strDup(secTypes)), type;
while (types.buf) {
strSplit(types.buf, ',', &type.buf, &types.buf);
int typeNum = SSecurityVeNCrypt::secTypeNum(type.buf);
if (typeNum != secTypeInvalid)
result.push_back(typeNum);
}
return result;
}
|