-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathXvncConnection.cpp
360 lines (287 loc) · 10.3 KB
/
XvncConnection.cpp
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* Copyright (c) 2016 Michal Srb <michalsrb@gmail.com>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <assert.h>
#include <iostream>
#include <vector>
#include <gnutls/gnutls.h>
#include <gnutls/crypto.h>
#include "helper.h"
#include "Log.h"
#include "XvncConnection.h"
XvncConnection::XvncConnection(std::shared_ptr<Xvnc> xvnc)
: m_xvnc(xvnc)
, m_stream(xvnc->connect())
, m_streamFormatter(&m_stream)
{
Log::debug() << "Opening connection to Xvnc #" << m_xvnc->id() << std::endl;
}
XvncConnection::~XvncConnection()
{
Log::debug() << "Closing connection to Xvnc #" << m_xvnc->id() << std::endl;
m_xvnc->disconnect();
}
void XvncConnection::initialize()
{
SecurityType selectedSecurityType = startInitialization( { SecurityType::None });
if (selectedSecurityType != SecurityType::None) {
throw ConnectionException(this, std::string("Connection to Xvnc was expecting security None, but got ") + std::to_string((int)selectedSecurityType));
}
handleNoneSecurity();
}
void XvncConnection::initialize(ConnectionInitializedHandler connectionInitializedHandler, PasswordRequestHandler passwordRequestHandler, CredentialsRequestHandler credentialsRequestHandler)
{
m_connectionInitializedHandler = connectionInitializedHandler;
m_passwordRequestHandler = passwordRequestHandler;
m_credentialsRequestHandler = credentialsRequestHandler;
SecurityType selectedSecurityType = startInitialization( {
SecurityType::None, SecurityType::VncAuth, SecurityType::VeNCrypt
});
switch (selectedSecurityType) {
case SecurityType::None:
handleNoneSecurity();
break;
case SecurityType::VncAuth:
handleVncAuthSecurity();
break;
case SecurityType::VeNCrypt:
handleVeNCryptSecurity();
break;
default:
assert(!"Not reached");
break;
}
}
SecurityType XvncConnection::startInitialization(std::set<SecurityType> supportedTypes)
{
// Read the server's version
char versionString[VersionStringLength];
fmt().recv_raw(versionString, sizeof(versionString));
if (strncmp(versionString, HighestVersionString, VersionStringLength) != 0) {
throw ConnectionException(this, "Unsupported version of RFB protocol");
}
// Respond with the same version.
fmt().send_raw(versionString, sizeof(versionString));
// Negotiate security
uint8_t numberOfSecurityTypes;
fmt().recv(numberOfSecurityTypes);
// Zero means the server wants to report failure
if (numberOfSecurityTypes == 0) {
throw ConnectionException(this, std::string("Connection failed, reason: ") + receiveFailureReason());
}
// Read the list of security types
std::vector<SecurityType> securityTypes(numberOfSecurityTypes);
fmt().recv(securityTypes);
// Find any supported security type
SecurityType selectedSecurityType = SecurityType::Invalid;
for (SecurityType securityType : securityTypes) {
if (supportedTypes.find((SecurityType)securityType) != supportedTypes.end()) {
selectedSecurityType = securityType;
break;
}
}
if (selectedSecurityType == SecurityType::Invalid) {
throw ConnectionException(this, "No supported security type offered");
}
// Respond with chosen security type
fmt().send(selectedSecurityType);
return selectedSecurityType;
}
void XvncConnection::handleNoneSecurity()
{
receiveSecurityResult();
completeInitialization();
}
void XvncConnection::handleVncAuthSecurity()
{
m_passwordRequestHandler(std::bind(&XvncConnection::handleVncAuthSecurityWithPassword, this, std::placeholders::_1));
}
void XvncConnection::handleVncAuthSecurityWithPassword(std::string password)
{
// Receive challenge
VNCAuthMessage challenge_response;
fmt().recv(challenge_response);
// Convert the password into key
unsigned char key[8];
for (std::string::size_type i = 0; i < sizeof(key); i++) {
if (i >= password.size()) {
key[i] = '\0';
} else {
key[i] = ((password[i] * 0x0202020202ULL & 0x010884422010ULL) % 1023) & 0xfe; // Reverse the order of bits in the byte. That's how VncAuth does it...
}
}
// Encrypt the challenge with DES-ECB (gnutls offers only CBC, to get ECB we reinit the cipher for each block)
const gnutls_datum_t datum = { key, sizeof(key) };
gnutls_cipher_hd_t handle;
for (int i = 0; i < 2; i++) {
if (gnutls_cipher_init(&handle, GNUTLS_CIPHER_DES_CBC, &datum, nullptr) < 0) {
throw std::runtime_error("gnutls_cipher_init failed");
}
if (gnutls_cipher_encrypt(handle, &challenge_response.data[i * 8], 8) < 0) {
throw std::runtime_error("gnutls_cipher_encrypt failed");
}
gnutls_cipher_deinit(handle);
}
// Send response back
fmt().send(challenge_response);
receiveSecurityResult();
completeInitialization();
}
void XvncConnection::handleVeNCryptSecurity()
{
// Receive server's VeNCrypt version
VeNCryptVersion version;
fmt().recv(version);
if (version.major != 0 || version.minor != 2) {
throw ConnectionException(this, "Unsupported VeNCrypt version.");
}
// Send our version (which is the same as server's)
fmt().send(version);
// Receive status
uint8_t status;
fmt().recv(status);
if (status != 0) {
throw ConnectionException(this, "VeNCrypt version selection failed.");
}
// Receive list of VeNCrypt subtypes
uint8_t length;
fmt().recv(length);
std::vector<VeNCryptSubtype> subtypes(length);
fmt().recv(subtypes);
VeNCryptSubtype selectedSubtype = VeNCryptSubtype::Invalid;
for (VeNCryptSubtype subtype : subtypes) {
switch (subtype) {
case VeNCryptSubtype::Plain:
case VeNCryptSubtype::None:
case VeNCryptSubtype::VncAuth:
selectedSubtype = subtype;
break;
default:
// Other VeNCrypt subtypes are not supported.
// Empty default case to silence Wswitch.
break;
}
if (selectedSubtype != VeNCryptSubtype::Invalid) {
break;
}
}
if (selectedSubtype == VeNCryptSubtype::Invalid) {
throw ConnectionException(this, "No supported VeNCrypt subtype available.");
}
// Send the subtype we want
fmt().send(selectedSubtype);
switch (selectedSubtype) {
case VeNCryptSubtype::None:
handleNoneSecurity();
break;
case VeNCryptSubtype::VncAuth:
handleVncAuthSecurity();
break;
case VeNCryptSubtype::Plain:
// Request username and password
m_credentialsRequestHandler(std::bind(&XvncConnection::handleVeNCryptSecurityWithCredentials, this, std::placeholders::_1, std::placeholders::_2));
break;
default:
assert(!"Not reached"); // This would mean that we selected subtype that we don't support -> our bug.
break;
}
}
void XvncConnection::handleVeNCryptSecurityWithCredentials(std::string username, std::string password)
{
VeNCryptPlainMessage message;
message.usernameLength = username.length();
message.passwordLength = password.length();
fmt().send(message);
fmt().send(username);
fmt().send(password);
receiveSecurityResult();
completeInitialization();
}
void XvncConnection::receiveSecurityResult()
{
// Expect the security result to succeed
uint32_t status;
fmt().recv(status);
if (status != 0) {
throw ConnectionException(this, std::string("Connection failed, reason: ") + receiveFailureReason());
}
}
void XvncConnection::completeInitialization()
{
// Send shared flag
ClientInitMessage clientInit;
clientInit.shared = true;
fmt().send(clientInit);
// Receive server init
ServerInitMessage serverInit;
fmt().recv(serverInit);
m_framebufferWidth = serverInit.framebufferWidth;
m_framebufferHeight = serverInit.framebufferHeight;
m_pixelFormat = serverInit.serverPixelFormat;
m_xvnc->setDesktopName(fmt().recv_string(serverInit.nameLength));
if (m_connectionInitializedHandler) {
m_connectionInitializedHandler();
m_connectionInitializedHandler = ConnectionInitializedHandler();
}
}
std::string XvncConnection::receiveFailureReason()
{
uint32_t reasonLength;
fmt().recv(reasonLength);
return fmt().recv_string(reasonLength);
}
void XvncConnection::sendSetPixelFormat(const PixelFormat &pixelFormat)
{
m_pixelFormat = pixelFormat;
SetPixelFormatMessage message;
message.pixelFormat = pixelFormat;
fmt().send(message);
}
void XvncConnection::sendSetEncodings(const std::vector<EncodingType> &encodings)
{
SetEncodingsMessage message;
message.numberOfEncodings = encodings.size();
fmt().send(message);
fmt().send(encodings);
}
void XvncConnection::sendNonIncrementalFramebufferUpdateRequest()
{
FramebufferUpdateRequestMessage message;
message.xPosition = 0;
message.yPosition = 0;
message.width = framebufferWidth();
message.height = framebufferHeight();
message.incremental = false;
fmt().send(message);
}
void XvncConnection::setFramebufferSize(uint16_t width, uint16_t height)
{
m_framebufferWidth = width;
m_framebufferHeight = height;
}
void XvncConnection::setDesktopName(const std::string &desktopName)
{
m_xvnc->setDesktopName(desktopName);
}