/src/FreeRDP/libfreerdp/core/connection.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * Connection Sequence |
4 | | * |
5 | | * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
6 | | * Copyright 2015 Thincast Technologies GmbH |
7 | | * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com> |
8 | | * Copyright 2023 Armin Novak <anovak@thincast.com> |
9 | | * Copyright 2023 Thincast Technologies GmbH |
10 | | * |
11 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
12 | | * you may not use this file except in compliance with the License. |
13 | | * You may obtain a copy of the License at |
14 | | * |
15 | | * http://www.apache.org/licenses/LICENSE-2.0 |
16 | | * |
17 | | * Unless required by applicable law or agreed to in writing, software |
18 | | * distributed under the License is distributed on an "AS IS" BASIS, |
19 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
20 | | * See the License for the specific language governing permissions and |
21 | | * limitations under the License. |
22 | | */ |
23 | | |
24 | | #include <freerdp/config.h> |
25 | | |
26 | | #include "settings.h" |
27 | | |
28 | | #include "info.h" |
29 | | #include "input.h" |
30 | | #include "rdp.h" |
31 | | #include "peer.h" |
32 | | |
33 | | #include "connection.h" |
34 | | #include "transport.h" |
35 | | |
36 | | #include <winpr/crt.h> |
37 | | #include <winpr/crypto.h> |
38 | | #include <winpr/ssl.h> |
39 | | |
40 | | #include <freerdp/log.h> |
41 | | #include <freerdp/error.h> |
42 | | #include <freerdp/listener.h> |
43 | | |
44 | | #include "../cache/pointer.h" |
45 | | #include "../crypto/crypto.h" |
46 | | #include "../crypto/privatekey.h" |
47 | | #include "../crypto/certificate.h" |
48 | | #include "gateway/arm.h" |
49 | | |
50 | | #include "utils.h" |
51 | | |
52 | 0 | #define TAG FREERDP_TAG("core.connection") |
53 | | |
54 | | /** |
55 | | * Connection Sequence |
56 | | * client server |
57 | | * | | |
58 | | * |-----------------------X.224 Connection Request PDU--------------------->| |
59 | | * |<----------------------X.224 Connection Confirm PDU----------------------| |
60 | | * |-------MCS Connect-Initial PDU with GCC Conference Create Request------->| |
61 | | * |<-----MCS Connect-Response PDU with GCC Conference Create Response-------| |
62 | | * |------------------------MCS Erect Domain Request PDU-------------------->| |
63 | | * |------------------------MCS Attach User Request PDU--------------------->| |
64 | | * |<-----------------------MCS Attach User Confirm PDU----------------------| |
65 | | * |------------------------MCS Channel Join Request PDU-------------------->| |
66 | | * |<-----------------------MCS Channel Join Confirm PDU---------------------| |
67 | | * |----------------------------Security Exchange PDU----------------------->| |
68 | | * |-------------------------------Client Info PDU-------------------------->| |
69 | | * |<---------------------License Error PDU - Valid Client-------------------| |
70 | | * |<-----------------------------Demand Active PDU--------------------------| |
71 | | * |------------------------------Confirm Active PDU------------------------>| |
72 | | * |-------------------------------Synchronize PDU-------------------------->| |
73 | | * |---------------------------Control PDU - Cooperate---------------------->| |
74 | | * |------------------------Control PDU - Request Control------------------->| |
75 | | * |--------------------------Persistent Key List PDU(s)-------------------->| |
76 | | * |--------------------------------Font List PDU--------------------------->| |
77 | | * |<------------------------------Synchronize PDU---------------------------| |
78 | | * |<--------------------------Control PDU - Cooperate-----------------------| |
79 | | * |<-----------------------Control PDU - Granted Control--------------------| |
80 | | * |<-------------------------------Font Map PDU-----------------------------| |
81 | | * |
82 | | */ |
83 | | |
84 | | /** |
85 | | * |
86 | | * Connection Sequence |
87 | | * |
88 | | * 1. Connection Initiation: The client initiates the connection by sending the server a |
89 | | * Class 0 X.224 Connection Request PDU (section 2.2.1.1). The server responds with a |
90 | | * Class 0 X.224 Connection Confirm PDU (section 2.2.1.2). From this point, all subsequent |
91 | | * data sent between client and server is wrapped in an X.224 Data Protocol Data Unit (PDU). |
92 | | * |
93 | | * 2. Basic Settings Exchange: Basic settings are exchanged between the client and server by |
94 | | * using the MCS Connect Initial PDU (section 2.2.1.3) and MCS Connect Response PDU |
95 | | *(section 2.2.1.4). The Connect Initial PDU contains a Generic Conference Control (GCC) Conference |
96 | | *Create Request, while the Connect Response PDU contains a GCC Conference Create Response. These |
97 | | *two GCC packets contain concatenated blocks of settings data (such as core data, security data, |
98 | | *and network data) which are read by client and server. |
99 | | * |
100 | | * 3. Channel Connection: The client sends an MCS Erect Domain Request PDU (section 2.2.1.5), |
101 | | * followed by an MCS Attach User Request PDU (section 2.2.1.6) to attach the primary user identity |
102 | | * to the MCS domain. The server responds with an MCS Attach User Confirm PDU (section 2.2.1.7) |
103 | | * containing the User Channel ID. The client then proceeds to join the user channel, the |
104 | | * input/output (I/O) channel, and all of the static virtual channels (the I/O and static virtual |
105 | | * channel IDs are obtained from the data embedded in the GCC packets) by using multiple MCS |
106 | | *Channel Join Request PDUs (section 2.2.1.8). The server confirms each channel with an MCS Channel |
107 | | *Join Confirm PDU (section 2.2.1.9). (The client only sends a Channel Join Request after it has |
108 | | *received the Channel Join Confirm for the previously sent request.) |
109 | | * |
110 | | * From this point, all subsequent data sent from the client to the server is wrapped in an MCS |
111 | | *Send Data Request PDU, while data sent from the server to the client is wrapped in an MCS Send |
112 | | *Data Indication PDU. This is in addition to the data being wrapped by an X.224 Data PDU. |
113 | | * |
114 | | * 4. RDP Security Commencement: If Standard RDP Security mechanisms (section 5.3) are being |
115 | | *employed and encryption is in force (this is determined by examining the data embedded in the GCC |
116 | | *Conference Create Response packet) then the client sends a Security Exchange PDU |
117 | | *(section 2.2.1.10) containing an encrypted 32-byte random number to the server. This random number |
118 | | *is encrypted with the public key of the server as described in section 5.3.4.1 (the server's |
119 | | *public key, as well as a 32-byte server-generated random number, are both obtained from the data |
120 | | *embedded in the GCC Conference Create Response packet). The client and server then utilize the two |
121 | | *32-byte random numbers to generate session keys which are used to encrypt and validate the |
122 | | *integrity of subsequent RDP traffic. |
123 | | * |
124 | | * From this point, all subsequent RDP traffic can be encrypted and a security header is included |
125 | | *with the data if encryption is in force. (The Client Info PDU (section 2.2.1.11) and licensing |
126 | | *PDUs ([MS-RDPELE] section 2.2.2) are an exception in that they always have a security header). The |
127 | | *Security Header follows the X.224 and MCS Headers and indicates whether the attached data is |
128 | | *encrypted. Even if encryption is in force, server-to-client traffic may not always be encrypted, |
129 | | *while client-to-server traffic must always be encrypted (encryption of licensing PDUs is optional, |
130 | | *however). |
131 | | * |
132 | | * 5. Secure Settings Exchange: Secure client data (such as the username, password, and |
133 | | *auto-reconnect cookie) is sent to the server by using the Client Info PDU (section 2.2.1.11). |
134 | | * |
135 | | * 6. Optional Connect-Time Auto-Detection: During the optional connect-time auto-detect phase the |
136 | | *goal is to determine characteristics of the network, such as the round-trip latency time and the |
137 | | *bandwidth of the link between the server and client. This is accomplished by exchanging a |
138 | | *collection of PDUs (specified in section 2.2.1.4) over a predetermined period of time with enough |
139 | | *data to ensure that the results are statistically relevant. |
140 | | * |
141 | | * 7. Licensing: The goal of the licensing exchange is to transfer a license from the server to |
142 | | *the client. The client stores this license and on subsequent connections sends the license to the |
143 | | *server for validation. However, in some situations the client may not be issued a license to |
144 | | *store. In effect, the packets exchanged during this phase of the protocol depend on the licensing |
145 | | *mechanisms employed by the server. Within the context of this document, it is assumed that the |
146 | | *client will not be issued a license to store. For details regarding more advanced licensing |
147 | | *scenarios that take place during the Licensing Phase, see [MS-RDPELE] section 1.3. |
148 | | * |
149 | | * 8. Optional Multitransport Bootstrapping: After the connection has been secured and the |
150 | | *Licensing Phase has run to completion, the server can choose to initiate multitransport |
151 | | *connections ([MS-RDPEMT] section 1.3). The Initiate Multitransport Request PDU (section 2.2.15.1) |
152 | | *is sent by the server to the client and results in the out-of-band creation of a multitransport |
153 | | *connection using messages from the RDP-UDP, TLS, DTLS, and multitransport protocols ([MS-RDPEMT] |
154 | | *section 1.3.1). |
155 | | * |
156 | | * 9. Capabilities Exchange: The server sends the set of capabilities it supports to the client in |
157 | | *a Demand Active PDU (section 2.2.1.13.1). The client responds with its capabilities by sending a |
158 | | *Confirm Active PDU (section 2.2.1.13.2). |
159 | | * |
160 | | * 10. Connection Finalization: The client and server exchange PDUs to finalize the connection |
161 | | *details. The client-to-server PDUs sent during this phase have no dependencies on any of the |
162 | | *server-to-client PDUs; they may be sent as a single batch, provided that sequencing is maintained. |
163 | | * |
164 | | * - The Client Synchronize PDU (section 2.2.1.14) is sent after transmitting the Confirm Active |
165 | | *PDU. |
166 | | * - The Client Control (Cooperate) PDU (section 2.2.1.15) is sent after transmitting the Client |
167 | | *Synchronize PDU. |
168 | | * - The Client Control (Request Control) PDU (section 2.2.1.16) is sent after transmitting the |
169 | | *Client Control (Cooperate) PDU. |
170 | | * - The optional Persistent Key List PDUs (section 2.2.1.17) are sent after transmitting the |
171 | | *Client Control (Request Control) PDU. |
172 | | * - The Font List PDU (section 2.2.1.18) is sent after transmitting the Persistent Key List PDUs |
173 | | *or, if the Persistent Key List PDUs were not sent, it is sent after transmitting the Client |
174 | | *Control (Request Control) PDU (section 2.2.1.16). |
175 | | * |
176 | | * The server-to-client PDUs sent during the Connection Finalization Phase have dependencies on the |
177 | | *client-to-server PDUs. |
178 | | * |
179 | | * - The optional Monitor Layout PDU (section 2.2.12.1) has no dependency on any client-to-server |
180 | | *PDUs and is sent after the Demand Active PDU. |
181 | | * - The Server Synchronize PDU (section 2.2.1.19) is sent in response to the Confirm Active PDU. |
182 | | * - The Server Control (Cooperate) PDU (section 2.2.1.20) is sent after transmitting the Server |
183 | | *Synchronize PDU. |
184 | | * - The Server Control (Granted Control) PDU (section 2.2.1.21) is sent in response to the Client |
185 | | *Control (Request Control) PDU. |
186 | | * - The Font Map PDU (section 2.2.1.22) is sent in response to the Font List PDU. |
187 | | * |
188 | | * Once the client has sent the Confirm Active PDU, it can start sending mouse and keyboard input |
189 | | *to the server, and upon receipt of the Font List PDU the server can start sending graphics output |
190 | | *to the client. |
191 | | * |
192 | | * Besides input and graphics data, other data that can be exchanged between client and server |
193 | | *after the connection has been finalized includes connection management information and virtual |
194 | | *channel messages (exchanged between client-side plug-ins and server-side applications). |
195 | | */ |
196 | | |
197 | | static BOOL rdp_set_state(rdpRdp* rdp, CONNECTION_STATE state); |
198 | | |
199 | | static BOOL rdp_client_reset_codecs(rdpContext* context) |
200 | 0 | { |
201 | 0 | rdpSettings* settings = NULL; |
202 | |
|
203 | 0 | if (!context || !context->settings) |
204 | 0 | return FALSE; |
205 | | |
206 | 0 | settings = context->settings; |
207 | |
|
208 | 0 | if (!freerdp_settings_get_bool(settings, FreeRDP_DeactivateClientDecoding)) |
209 | 0 | { |
210 | 0 | const UINT32 flags = freerdp_settings_get_uint32(settings, FreeRDP_ThreadingFlags); |
211 | 0 | freerdp_client_codecs_free(context->codecs); |
212 | 0 | context->codecs = freerdp_client_codecs_new(flags); |
213 | |
|
214 | 0 | if (!context->codecs) |
215 | 0 | return FALSE; |
216 | | |
217 | 0 | if (!freerdp_client_codecs_prepare(context->codecs, |
218 | 0 | freerdp_settings_get_codecs_flags(settings), |
219 | 0 | settings->DesktopWidth, settings->DesktopHeight)) |
220 | 0 | return FALSE; |
221 | | |
222 | | /* Runtime H264 detection. (only available if dynamic backend loading is defined) |
223 | | * If no backend is available disable it before the channel is loaded. |
224 | | */ |
225 | | #if defined(WITH_GFX_H264) && defined(WITH_OPENH264_LOADING) |
226 | | if (!context->codecs->h264) |
227 | | { |
228 | | settings->GfxH264 = FALSE; |
229 | | settings->GfxAVC444 = FALSE; |
230 | | settings->GfxAVC444v2 = FALSE; |
231 | | } |
232 | | #endif |
233 | 0 | } |
234 | | |
235 | 0 | return TRUE; |
236 | 0 | } |
237 | | |
238 | | static BOOL rdp_client_wait_for_activation(rdpRdp* rdp) |
239 | 0 | { |
240 | 0 | BOOL timedout = FALSE; |
241 | 0 | WINPR_ASSERT(rdp); |
242 | | |
243 | 0 | const rdpSettings* settings = rdp->settings; |
244 | 0 | WINPR_ASSERT(settings); |
245 | | |
246 | 0 | UINT64 now = GetTickCount64(); |
247 | 0 | UINT64 dueDate = now + freerdp_settings_get_uint32(settings, FreeRDP_TcpAckTimeout); |
248 | |
|
249 | 0 | for (; (now < dueDate) && !timedout; now = GetTickCount64()) |
250 | 0 | { |
251 | 0 | HANDLE events[MAXIMUM_WAIT_OBJECTS] = { 0 }; |
252 | 0 | DWORD wstatus = 0; |
253 | 0 | DWORD nevents = freerdp_get_event_handles(rdp->context, events, ARRAYSIZE(events)); |
254 | 0 | if (!nevents) |
255 | 0 | { |
256 | 0 | WLog_ERR(TAG, "error retrieving connection events"); |
257 | 0 | return FALSE; |
258 | 0 | } |
259 | | |
260 | 0 | const UINT64 timeout = (dueDate - now); |
261 | 0 | WINPR_ASSERT(timeout <= UINT32_MAX); |
262 | 0 | wstatus = WaitForMultipleObjectsEx(nevents, events, FALSE, (UINT32)timeout, TRUE); |
263 | 0 | switch (wstatus) |
264 | 0 | { |
265 | 0 | case WAIT_TIMEOUT: |
266 | | /* will make us quit with a timeout */ |
267 | 0 | timedout = TRUE; |
268 | 0 | break; |
269 | 0 | case WAIT_ABANDONED: |
270 | 0 | case WAIT_FAILED: |
271 | 0 | return FALSE; |
272 | 0 | case WAIT_IO_COMPLETION: |
273 | 0 | break; |
274 | 0 | case WAIT_OBJECT_0: |
275 | 0 | default: |
276 | | /* handles all WAIT_OBJECT_0 + [0 .. MAXIMUM_WAIT_OBJECTS-1] cases */ |
277 | 0 | if (rdp_check_fds(rdp) < 0) |
278 | 0 | { |
279 | 0 | freerdp_set_last_error_if_not(rdp->context, |
280 | 0 | FREERDP_ERROR_CONNECT_TRANSPORT_FAILED); |
281 | 0 | return FALSE; |
282 | 0 | } |
283 | 0 | break; |
284 | 0 | } |
285 | | |
286 | 0 | if (rdp_is_active_state(rdp)) |
287 | 0 | return TRUE; |
288 | 0 | } |
289 | | |
290 | 0 | WLog_ERR(TAG, "Timeout waiting for activation"); |
291 | 0 | freerdp_set_last_error_if_not(rdp->context, FREERDP_ERROR_CONNECT_ACTIVATION_TIMEOUT); |
292 | 0 | return FALSE; |
293 | 0 | } |
294 | | /** |
295 | | * Establish RDP Connection based on the settings given in the 'rdp' parameter. |
296 | | * msdn{cc240452} |
297 | | * @param rdp RDP module |
298 | | * @return true if the connection succeeded. FALSE otherwise. |
299 | | */ |
300 | | |
301 | | BOOL rdp_client_connect(rdpRdp* rdp) |
302 | 0 | { |
303 | 0 | UINT32 SelectedProtocol = 0; |
304 | 0 | BOOL status = 0; |
305 | 0 | rdpSettings* settings = NULL; |
306 | | /* make sure SSL is initialize for earlier enough for crypto, by taking advantage of winpr SSL |
307 | | * FIPS flag for openssl initialization */ |
308 | 0 | DWORD flags = WINPR_SSL_INIT_DEFAULT; |
309 | |
|
310 | 0 | WINPR_ASSERT(rdp); |
311 | | |
312 | 0 | settings = rdp->settings; |
313 | 0 | WINPR_ASSERT(settings); |
314 | | |
315 | 0 | if (!rdp_client_reset_codecs(rdp->context)) |
316 | 0 | return FALSE; |
317 | | |
318 | 0 | if (settings->FIPSMode) |
319 | 0 | flags |= WINPR_SSL_INIT_ENABLE_FIPS; |
320 | |
|
321 | 0 | winpr_InitializeSSL(flags); |
322 | 0 | rdp_log_build_warnings(rdp); |
323 | | |
324 | | /* FIPS Mode forces the following and overrides the following(by happening later */ |
325 | | /* in the command line processing): */ |
326 | | /* 1. Disables NLA Security since NLA in freerdp uses NTLM(no Kerberos support yet) which uses |
327 | | * algorithms */ |
328 | | /* not allowed in FIPS for sensitive data. So, we disallow NLA when FIPS is required. */ |
329 | | /* 2. Forces the only supported RDP encryption method to be FIPS. */ |
330 | 0 | if (settings->FIPSMode || winpr_FIPSMode()) |
331 | 0 | { |
332 | 0 | settings->NlaSecurity = FALSE; |
333 | 0 | settings->EncryptionMethods = ENCRYPTION_METHOD_FIPS; |
334 | 0 | } |
335 | |
|
336 | 0 | UINT32 TcpConnectTimeout = freerdp_settings_get_uint32(settings, FreeRDP_TcpConnectTimeout); |
337 | 0 | if (settings->GatewayArmTransport) |
338 | 0 | { |
339 | 0 | if (!arm_resolve_endpoint(rdp->log, rdp->context, TcpConnectTimeout)) |
340 | 0 | { |
341 | 0 | WLog_ERR(TAG, "error retrieving ARM configuration"); |
342 | 0 | return FALSE; |
343 | 0 | } |
344 | 0 | } |
345 | | |
346 | 0 | const char* hostname = settings->ServerHostname; |
347 | 0 | if (!hostname) |
348 | 0 | { |
349 | 0 | WLog_ERR(TAG, "Missing hostname, can not connect to NULL target"); |
350 | 0 | return FALSE; |
351 | 0 | } |
352 | | |
353 | 0 | const UINT32 port = settings->ServerPort; |
354 | 0 | WINPR_ASSERT(port <= UINT32_MAX); |
355 | | |
356 | 0 | nego_init(rdp->nego); |
357 | 0 | nego_set_target(rdp->nego, hostname, (UINT16)port); |
358 | |
|
359 | 0 | if (settings->GatewayEnabled) |
360 | 0 | { |
361 | 0 | char* user = NULL; |
362 | 0 | char* domain = NULL; |
363 | 0 | size_t user_length = 0; |
364 | |
|
365 | 0 | if (settings->Username) |
366 | 0 | { |
367 | 0 | user = settings->Username; |
368 | 0 | user_length = strlen(settings->Username); |
369 | 0 | } |
370 | |
|
371 | 0 | if (settings->Domain) |
372 | 0 | domain = settings->Domain; |
373 | 0 | else |
374 | 0 | domain = settings->ComputerName; |
375 | |
|
376 | 0 | const size_t domain_length = strlen(domain); |
377 | 0 | const size_t cookie_length = domain_length + 1 + user_length; |
378 | 0 | char* cookie = malloc(cookie_length + 1); |
379 | |
|
380 | 0 | if (!cookie) |
381 | 0 | return FALSE; |
382 | | |
383 | 0 | CopyMemory(cookie, domain, domain_length); |
384 | 0 | WINPR_ASSERT(domain_length <= UINT32_MAX); |
385 | 0 | CharUpperBuffA(cookie, (UINT32)domain_length); |
386 | 0 | cookie[domain_length] = '\\'; |
387 | |
|
388 | 0 | if (settings->Username) |
389 | 0 | CopyMemory(&cookie[domain_length + 1], user, user_length); |
390 | |
|
391 | 0 | cookie[cookie_length] = '\0'; |
392 | 0 | status = nego_set_cookie(rdp->nego, cookie); |
393 | 0 | free(cookie); |
394 | 0 | } |
395 | 0 | else |
396 | 0 | { |
397 | 0 | status = nego_set_cookie(rdp->nego, settings->Username); |
398 | 0 | } |
399 | | |
400 | 0 | if (!status) |
401 | 0 | return FALSE; |
402 | | |
403 | 0 | nego_set_childsession_enabled(rdp->nego, settings->ConnectChildSession); |
404 | 0 | nego_set_send_preconnection_pdu(rdp->nego, settings->SendPreconnectionPdu); |
405 | 0 | nego_set_preconnection_id(rdp->nego, settings->PreconnectionId); |
406 | 0 | nego_set_preconnection_blob(rdp->nego, settings->PreconnectionBlob); |
407 | 0 | nego_set_negotiation_enabled(rdp->nego, settings->NegotiateSecurityLayer); |
408 | 0 | nego_set_restricted_admin_mode_required(rdp->nego, settings->RestrictedAdminModeRequired); |
409 | 0 | nego_set_RCG_required(rdp->nego, settings->RemoteCredentialGuard); |
410 | 0 | nego_set_gateway_enabled(rdp->nego, settings->GatewayEnabled); |
411 | 0 | nego_set_gateway_bypass_local(rdp->nego, settings->GatewayBypassLocal); |
412 | 0 | nego_enable_rdp(rdp->nego, settings->RdpSecurity); |
413 | 0 | nego_enable_tls(rdp->nego, settings->TlsSecurity); |
414 | 0 | nego_enable_nla(rdp->nego, settings->NlaSecurity); |
415 | 0 | nego_enable_ext(rdp->nego, settings->ExtSecurity); |
416 | 0 | nego_enable_rdstls(rdp->nego, settings->RdstlsSecurity); |
417 | 0 | nego_enable_aad(rdp->nego, settings->AadSecurity); |
418 | |
|
419 | 0 | if (settings->MstscCookieMode) |
420 | 0 | settings->CookieMaxLength = MSTSC_COOKIE_MAX_LENGTH; |
421 | |
|
422 | 0 | nego_set_cookie_max_length(rdp->nego, settings->CookieMaxLength); |
423 | |
|
424 | 0 | if (settings->LoadBalanceInfo && (settings->LoadBalanceInfoLength > 0)) |
425 | 0 | { |
426 | 0 | if (!nego_set_routing_token(rdp->nego, settings->LoadBalanceInfo, |
427 | 0 | settings->LoadBalanceInfoLength)) |
428 | 0 | return FALSE; |
429 | 0 | } |
430 | | |
431 | 0 | if (!freerdp_settings_get_bool(settings, FreeRDP_TransportDumpReplay)) |
432 | 0 | { |
433 | 0 | if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_NEGO)) |
434 | 0 | return FALSE; |
435 | | |
436 | 0 | if (!nego_connect(rdp->nego)) |
437 | 0 | { |
438 | 0 | if (!freerdp_get_last_error(rdp->context)) |
439 | 0 | { |
440 | 0 | freerdp_set_last_error_log(rdp->context, |
441 | 0 | FREERDP_ERROR_SECURITY_NEGO_CONNECT_FAILED); |
442 | 0 | WLog_ERR(TAG, "Error: protocol security negotiation or connection failure"); |
443 | 0 | } |
444 | |
|
445 | 0 | return FALSE; |
446 | 0 | } |
447 | | |
448 | 0 | SelectedProtocol = nego_get_selected_protocol(rdp->nego); |
449 | |
|
450 | 0 | if ((SelectedProtocol & PROTOCOL_SSL) || (SelectedProtocol == PROTOCOL_RDP) || |
451 | 0 | (SelectedProtocol == PROTOCOL_RDSTLS)) |
452 | 0 | { |
453 | 0 | wStream s = { 0 }; |
454 | |
|
455 | 0 | if ((settings->Username != NULL) && |
456 | 0 | ((freerdp_settings_get_string(settings, FreeRDP_Password) != NULL) || |
457 | 0 | (settings->RedirectionPassword != NULL && |
458 | 0 | settings->RedirectionPasswordLength > 0))) |
459 | 0 | settings->AutoLogonEnabled = TRUE; |
460 | |
|
461 | 0 | if (rdp_recv_callback(rdp->transport, &s, rdp) < 0) |
462 | 0 | return FALSE; |
463 | 0 | } |
464 | | |
465 | 0 | transport_set_blocking_mode(rdp->transport, FALSE); |
466 | 0 | } |
467 | 0 | else |
468 | 0 | { |
469 | 0 | if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_MCS_CREATE_REQUEST)) |
470 | 0 | return FALSE; |
471 | 0 | } |
472 | | |
473 | | /* everything beyond this point is event-driven and non blocking */ |
474 | 0 | if (!transport_set_recv_callbacks(rdp->transport, rdp_recv_callback, rdp)) |
475 | 0 | return FALSE; |
476 | | |
477 | 0 | return rdp_client_wait_for_activation(rdp); |
478 | 0 | } |
479 | | |
480 | | BOOL rdp_client_disconnect(rdpRdp* rdp) |
481 | 0 | { |
482 | 0 | rdpContext* context = NULL; |
483 | |
|
484 | 0 | if (!rdp || !rdp->settings || !rdp->context) |
485 | 0 | return FALSE; |
486 | | |
487 | 0 | context = rdp->context; |
488 | |
|
489 | 0 | if (rdp->nego) |
490 | 0 | { |
491 | 0 | if (!nego_disconnect(rdp->nego)) |
492 | 0 | return FALSE; |
493 | 0 | } |
494 | | |
495 | 0 | if (!transport_disconnect(rdp->transport)) |
496 | 0 | return FALSE; |
497 | | |
498 | 0 | if (!rdp_reset(rdp)) |
499 | 0 | return FALSE; |
500 | | |
501 | 0 | if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_INITIAL)) |
502 | 0 | return FALSE; |
503 | | |
504 | 0 | if (freerdp_channels_disconnect(context->channels, context->instance) != CHANNEL_RC_OK) |
505 | 0 | return FALSE; |
506 | | |
507 | 0 | freerdp_client_codecs_free(context->codecs); |
508 | 0 | context->codecs = NULL; |
509 | 0 | return TRUE; |
510 | 0 | } |
511 | | |
512 | | BOOL rdp_client_disconnect_and_clear(rdpRdp* rdp) |
513 | 0 | { |
514 | 0 | rdpContext* context = NULL; |
515 | |
|
516 | 0 | if (!rdp_client_disconnect(rdp)) |
517 | 0 | return FALSE; |
518 | | |
519 | 0 | WINPR_ASSERT(rdp); |
520 | | |
521 | 0 | context = rdp->context; |
522 | 0 | WINPR_ASSERT(context); |
523 | | |
524 | 0 | if (freerdp_get_last_error(context) == FREERDP_ERROR_CONNECT_CANCELLED) |
525 | 0 | return FALSE; |
526 | | |
527 | 0 | context->LastError = FREERDP_ERROR_SUCCESS; |
528 | 0 | clearChannelError(context); |
529 | 0 | return utils_reset_abort(rdp); |
530 | 0 | } |
531 | | |
532 | | static BOOL rdp_client_reconnect_channels(rdpRdp* rdp, BOOL redirect) |
533 | 0 | { |
534 | 0 | BOOL status = FALSE; |
535 | 0 | rdpContext* context = NULL; |
536 | |
|
537 | 0 | if (!rdp || !rdp->context || !rdp->context->channels) |
538 | 0 | return FALSE; |
539 | | |
540 | 0 | context = rdp->context; |
541 | |
|
542 | 0 | if (context->instance->ConnectionCallbackState == CLIENT_STATE_INITIAL) |
543 | 0 | return FALSE; |
544 | | |
545 | 0 | if (context->instance->ConnectionCallbackState == CLIENT_STATE_PRECONNECT_PASSED) |
546 | 0 | { |
547 | 0 | if (redirect) |
548 | 0 | return TRUE; |
549 | | |
550 | 0 | pointer_cache_register_callbacks(context->update); |
551 | |
|
552 | 0 | if (!IFCALLRESULT(FALSE, context->instance->PostConnect, context->instance)) |
553 | 0 | return FALSE; |
554 | | |
555 | 0 | context->instance->ConnectionCallbackState = CLIENT_STATE_POSTCONNECT_PASSED; |
556 | 0 | } |
557 | | |
558 | 0 | if (context->instance->ConnectionCallbackState == CLIENT_STATE_POSTCONNECT_PASSED) |
559 | 0 | status = |
560 | 0 | (freerdp_channels_post_connect(context->channels, context->instance) == CHANNEL_RC_OK); |
561 | |
|
562 | 0 | return status; |
563 | 0 | } |
564 | | |
565 | | static BOOL rdp_client_redirect_resolvable(const char* host) |
566 | 0 | { |
567 | 0 | struct addrinfo* result = freerdp_tcp_resolve_host(host, -1, 0); |
568 | |
|
569 | 0 | if (!result) |
570 | 0 | return FALSE; |
571 | | |
572 | 0 | freeaddrinfo(result); |
573 | 0 | return TRUE; |
574 | 0 | } |
575 | | |
576 | | static BOOL rdp_client_redirect_try_fqdn(rdpSettings* settings) |
577 | 0 | { |
578 | 0 | if (settings->RedirectionFlags & LB_TARGET_FQDN) |
579 | 0 | { |
580 | 0 | if (settings->GatewayEnabled || |
581 | 0 | rdp_client_redirect_resolvable(settings->RedirectionTargetFQDN)) |
582 | 0 | { |
583 | 0 | if (!freerdp_settings_set_string(settings, FreeRDP_ServerHostname, |
584 | 0 | settings->RedirectionTargetFQDN)) |
585 | 0 | return FALSE; |
586 | | |
587 | 0 | return TRUE; |
588 | 0 | } |
589 | 0 | } |
590 | | |
591 | 0 | return FALSE; |
592 | 0 | } |
593 | | |
594 | | static BOOL rdp_client_redirect_try_ip(rdpSettings* settings) |
595 | 0 | { |
596 | 0 | if (settings->RedirectionFlags & LB_TARGET_NET_ADDRESS) |
597 | 0 | { |
598 | 0 | if (!freerdp_settings_set_string(settings, FreeRDP_ServerHostname, |
599 | 0 | settings->TargetNetAddress)) |
600 | 0 | return FALSE; |
601 | | |
602 | 0 | return TRUE; |
603 | 0 | } |
604 | | |
605 | 0 | return FALSE; |
606 | 0 | } |
607 | | |
608 | | static BOOL rdp_client_redirect_try_netbios(rdpSettings* settings) |
609 | 0 | { |
610 | 0 | if (settings->RedirectionFlags & LB_TARGET_NETBIOS_NAME) |
611 | 0 | { |
612 | 0 | if (settings->GatewayEnabled || |
613 | 0 | rdp_client_redirect_resolvable(settings->RedirectionTargetNetBiosName)) |
614 | 0 | { |
615 | 0 | if (!freerdp_settings_set_string(settings, FreeRDP_ServerHostname, |
616 | 0 | settings->RedirectionTargetNetBiosName)) |
617 | 0 | return FALSE; |
618 | | |
619 | 0 | return TRUE; |
620 | 0 | } |
621 | 0 | } |
622 | | |
623 | 0 | return FALSE; |
624 | 0 | } |
625 | | |
626 | | BOOL rdp_client_redirect(rdpRdp* rdp) |
627 | 0 | { |
628 | 0 | BOOL status = 0; |
629 | 0 | rdpSettings* settings = NULL; |
630 | |
|
631 | 0 | if (!rdp_client_disconnect_and_clear(rdp)) |
632 | 0 | return FALSE; |
633 | | |
634 | | /* Only disconnect & close the channels here. |
635 | | * they will be discarded and recreated after the new settings have been applied. */ |
636 | 0 | freerdp_channels_disconnect(rdp->context->channels, rdp->context->instance); |
637 | 0 | freerdp_channels_close(rdp->context->channels, rdp->context->instance); |
638 | |
|
639 | 0 | if (rdp_redirection_apply_settings(rdp) != 0) |
640 | 0 | return FALSE; |
641 | | |
642 | 0 | WINPR_ASSERT(rdp); |
643 | | |
644 | 0 | settings = rdp->settings; |
645 | 0 | WINPR_ASSERT(settings); |
646 | | |
647 | 0 | if ((settings->RedirectionFlags & LB_LOAD_BALANCE_INFO) == 0) |
648 | 0 | { |
649 | 0 | BOOL haveRedirectAddress = FALSE; |
650 | 0 | UINT32 redirectionMask = settings->RedirectionPreferType; |
651 | |
|
652 | 0 | do |
653 | 0 | { |
654 | 0 | const BOOL tryFQDN = (redirectionMask & 0x01) == 0; |
655 | 0 | const BOOL tryNetAddress = (redirectionMask & 0x02) == 0; |
656 | 0 | const BOOL tryNetbios = (redirectionMask & 0x04) == 0; |
657 | |
|
658 | 0 | if (tryFQDN && !haveRedirectAddress) |
659 | 0 | haveRedirectAddress = rdp_client_redirect_try_fqdn(settings); |
660 | |
|
661 | 0 | if (tryNetAddress && !haveRedirectAddress) |
662 | 0 | haveRedirectAddress = rdp_client_redirect_try_ip(settings); |
663 | |
|
664 | 0 | if (tryNetbios && !haveRedirectAddress) |
665 | 0 | haveRedirectAddress = rdp_client_redirect_try_netbios(settings); |
666 | |
|
667 | 0 | redirectionMask >>= 3; |
668 | 0 | } while (!haveRedirectAddress && (redirectionMask != 0)); |
669 | 0 | } |
670 | |
|
671 | 0 | if (settings->RedirectionFlags & LB_USERNAME) |
672 | 0 | { |
673 | 0 | if (!freerdp_settings_set_string( |
674 | 0 | settings, FreeRDP_Username, |
675 | 0 | freerdp_settings_get_string(settings, FreeRDP_RedirectionUsername))) |
676 | 0 | return FALSE; |
677 | 0 | } |
678 | | |
679 | 0 | if (settings->RedirectionFlags & LB_DOMAIN) |
680 | 0 | { |
681 | 0 | if (!freerdp_settings_set_string( |
682 | 0 | settings, FreeRDP_Domain, |
683 | 0 | freerdp_settings_get_string(settings, FreeRDP_RedirectionDomain))) |
684 | 0 | return FALSE; |
685 | 0 | } |
686 | | |
687 | 0 | settings->RdstlsSecurity = |
688 | 0 | (settings->RedirectionFlags & LB_PASSWORD_IS_PK_ENCRYPTED) != 0 ? TRUE : FALSE; |
689 | |
|
690 | 0 | WINPR_ASSERT(rdp->context); |
691 | 0 | WINPR_ASSERT(rdp->context->instance); |
692 | 0 | if (!IFCALLRESULT(TRUE, rdp->context->instance->Redirect, rdp->context->instance)) |
693 | 0 | return FALSE; |
694 | | |
695 | 0 | BOOL ok = utils_reload_channels(rdp->context); |
696 | 0 | if (!ok) |
697 | 0 | return FALSE; |
698 | | |
699 | 0 | status = rdp_client_connect(rdp); |
700 | |
|
701 | 0 | if (status) |
702 | 0 | status = rdp_client_reconnect_channels(rdp, TRUE); |
703 | |
|
704 | 0 | return status; |
705 | 0 | } |
706 | | |
707 | | BOOL rdp_client_reconnect(rdpRdp* rdp) |
708 | 0 | { |
709 | 0 | BOOL status = 0; |
710 | |
|
711 | 0 | if (!rdp_client_disconnect_and_clear(rdp)) |
712 | 0 | return FALSE; |
713 | | |
714 | 0 | status = rdp_client_connect(rdp); |
715 | |
|
716 | 0 | if (status) |
717 | 0 | status = rdp_client_reconnect_channels(rdp, FALSE); |
718 | |
|
719 | 0 | return status; |
720 | 0 | } |
721 | | |
722 | | static const BYTE fips_ivec[8] = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; |
723 | | |
724 | | static BOOL rdp_client_establish_keys(rdpRdp* rdp) |
725 | 0 | { |
726 | 0 | wStream* s = NULL; |
727 | 0 | int status = 0; |
728 | 0 | BOOL ret = FALSE; |
729 | |
|
730 | 0 | WINPR_ASSERT(rdp); |
731 | 0 | rdpSettings* settings = rdp->settings; |
732 | 0 | BYTE* crypt_client_random = NULL; |
733 | |
|
734 | 0 | WINPR_ASSERT(settings); |
735 | 0 | if (!settings->UseRdpSecurityLayer) |
736 | 0 | { |
737 | | /* no RDP encryption */ |
738 | 0 | return TRUE; |
739 | 0 | } |
740 | | |
741 | 0 | if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_RDP_SECURITY_COMMENCEMENT)) |
742 | 0 | return FALSE; |
743 | | |
744 | | /* encrypt client random */ |
745 | 0 | if (!freerdp_settings_set_pointer_len(settings, FreeRDP_ClientRandom, NULL, |
746 | 0 | CLIENT_RANDOM_LENGTH)) |
747 | 0 | return FALSE; |
748 | 0 | winpr_RAND(settings->ClientRandom, settings->ClientRandomLength); |
749 | |
|
750 | 0 | const rdpCertInfo* info = freerdp_certificate_get_info(settings->RdpServerCertificate); |
751 | 0 | if (!info) |
752 | 0 | { |
753 | 0 | WLog_ERR(TAG, "Failed to get rdpCertInfo from RdpServerCertificate"); |
754 | 0 | return FALSE; |
755 | 0 | } |
756 | | |
757 | | /* |
758 | | * client random must be (bitlen / 8) + 8 - see [MS-RDPBCGR] 5.3.4.1 |
759 | | * for details |
760 | | */ |
761 | 0 | crypt_client_random = calloc(info->ModulusLength, 1); |
762 | |
|
763 | 0 | if (!crypt_client_random) |
764 | 0 | return FALSE; |
765 | | |
766 | 0 | crypto_rsa_public_encrypt(settings->ClientRandom, settings->ClientRandomLength, info, |
767 | 0 | crypt_client_random, info->ModulusLength); |
768 | | /* send crypt client random to server */ |
769 | 0 | const size_t length = RDP_PACKET_HEADER_MAX_LENGTH + RDP_SECURITY_HEADER_LENGTH + 4ULL + |
770 | 0 | info->ModulusLength + 8ULL; |
771 | 0 | if (length > UINT16_MAX) |
772 | 0 | return FALSE; |
773 | | |
774 | 0 | s = Stream_New(NULL, length); |
775 | |
|
776 | 0 | if (!s) |
777 | 0 | { |
778 | 0 | WLog_ERR(TAG, "Stream_New failed!"); |
779 | 0 | goto end; |
780 | 0 | } |
781 | | |
782 | 0 | UINT16 sec_flags = SEC_EXCHANGE_PKT | SEC_LICENSE_ENCRYPT_SC; |
783 | 0 | if (!rdp_write_header(rdp, s, length, MCS_GLOBAL_CHANNEL_ID, sec_flags)) |
784 | 0 | goto end; |
785 | 0 | if (!rdp_write_security_header(rdp, s, sec_flags)) |
786 | 0 | goto end; |
787 | | |
788 | 0 | Stream_Write_UINT32(s, info->ModulusLength + 8); |
789 | 0 | Stream_Write(s, crypt_client_random, info->ModulusLength); |
790 | 0 | Stream_Zero(s, 8); |
791 | 0 | Stream_SealLength(s); |
792 | 0 | status = transport_write(rdp->mcs->transport, s); |
793 | |
|
794 | 0 | if (status < 0) |
795 | 0 | goto end; |
796 | | |
797 | 0 | rdp->do_crypt_license = TRUE; |
798 | | |
799 | | /* now calculate encrypt / decrypt and update keys */ |
800 | 0 | if (!security_establish_keys(rdp)) |
801 | 0 | goto end; |
802 | | |
803 | 0 | rdp->do_crypt = TRUE; |
804 | |
|
805 | 0 | if (settings->SaltedChecksum) |
806 | 0 | rdp->do_secure_checksum = TRUE; |
807 | |
|
808 | 0 | if (settings->EncryptionMethods == ENCRYPTION_METHOD_FIPS) |
809 | 0 | { |
810 | 0 | rdp->fips_encrypt = |
811 | 0 | winpr_Cipher_NewEx(WINPR_CIPHER_DES_EDE3_CBC, WINPR_ENCRYPT, rdp->fips_encrypt_key, |
812 | 0 | sizeof(rdp->fips_encrypt_key), fips_ivec, sizeof(fips_ivec)); |
813 | |
|
814 | 0 | if (!rdp->fips_encrypt) |
815 | 0 | { |
816 | 0 | WLog_ERR(TAG, "unable to allocate des3 encrypt key"); |
817 | 0 | goto end; |
818 | 0 | } |
819 | | |
820 | 0 | rdp->fips_decrypt = |
821 | 0 | winpr_Cipher_NewEx(WINPR_CIPHER_DES_EDE3_CBC, WINPR_DECRYPT, rdp->fips_decrypt_key, |
822 | 0 | sizeof(rdp->fips_decrypt_key), fips_ivec, sizeof(fips_ivec)); |
823 | |
|
824 | 0 | if (!rdp->fips_decrypt) |
825 | 0 | { |
826 | 0 | WLog_ERR(TAG, "unable to allocate des3 decrypt key"); |
827 | 0 | goto end; |
828 | 0 | } |
829 | | |
830 | 0 | ret = TRUE; |
831 | 0 | goto end; |
832 | 0 | } |
833 | | |
834 | 0 | if (!rdp_reset_rc4_encrypt_keys(rdp)) |
835 | 0 | goto end; |
836 | 0 | if (!rdp_reset_rc4_decrypt_keys(rdp)) |
837 | 0 | goto end; |
838 | | |
839 | 0 | ret = TRUE; |
840 | 0 | end: |
841 | 0 | Stream_Free(s, TRUE); |
842 | 0 | free(crypt_client_random); |
843 | |
|
844 | 0 | if (!ret) |
845 | 0 | { |
846 | 0 | winpr_Cipher_Free(rdp->fips_decrypt); |
847 | 0 | winpr_Cipher_Free(rdp->fips_encrypt); |
848 | 0 | rdp->fips_decrypt = NULL; |
849 | 0 | rdp->fips_encrypt = NULL; |
850 | |
|
851 | 0 | rdp_free_rc4_decrypt_keys(rdp); |
852 | 0 | rdp_free_rc4_encrypt_keys(rdp); |
853 | 0 | } |
854 | |
|
855 | 0 | return ret; |
856 | 0 | } |
857 | | |
858 | | static BOOL rdp_update_client_random(rdpSettings* settings, const BYTE* crypt_random, |
859 | | size_t crypt_random_len) |
860 | 0 | { |
861 | 0 | const size_t length = 32; |
862 | 0 | WINPR_ASSERT(settings); |
863 | | |
864 | 0 | const rdpPrivateKey* rsa = freerdp_settings_get_pointer(settings, FreeRDP_RdpServerRsaKey); |
865 | 0 | WINPR_ASSERT(rsa); |
866 | | |
867 | 0 | const rdpCertInfo* cinfo = freerdp_key_get_info(rsa); |
868 | 0 | WINPR_ASSERT(cinfo); |
869 | | |
870 | 0 | if (crypt_random_len != cinfo->ModulusLength + 8) |
871 | 0 | { |
872 | 0 | WLog_ERR(TAG, "invalid encrypted client random length"); |
873 | 0 | return FALSE; |
874 | 0 | } |
875 | 0 | if (!freerdp_settings_set_pointer_len(settings, FreeRDP_ClientRandom, NULL, length)) |
876 | 0 | return FALSE; |
877 | | |
878 | 0 | BYTE* client_random = freerdp_settings_get_pointer_writable(settings, FreeRDP_ClientRandom); |
879 | 0 | WINPR_ASSERT(client_random); |
880 | 0 | return crypto_rsa_private_decrypt(crypt_random, crypt_random_len - 8, rsa, client_random, |
881 | 0 | length) > 0; |
882 | 0 | } |
883 | | |
884 | | BOOL rdp_server_establish_keys(rdpRdp* rdp, wStream* s) |
885 | 0 | { |
886 | 0 | UINT32 rand_len = 0; |
887 | 0 | UINT16 channel_id = 0; |
888 | 0 | UINT16 length = 0; |
889 | 0 | UINT16 sec_flags = 0; |
890 | 0 | BOOL ret = FALSE; |
891 | |
|
892 | 0 | WINPR_ASSERT(rdp); |
893 | | |
894 | 0 | if (!rdp->settings->UseRdpSecurityLayer) |
895 | 0 | { |
896 | | /* No RDP Security. */ |
897 | 0 | return TRUE; |
898 | 0 | } |
899 | | |
900 | 0 | if (!rdp_read_header(rdp, s, &length, &channel_id)) |
901 | 0 | return FALSE; |
902 | | |
903 | 0 | if (!rdp_read_security_header(rdp, s, &sec_flags, NULL)) |
904 | 0 | { |
905 | 0 | WLog_Print(rdp->log, WLOG_ERROR, "invalid security header"); |
906 | 0 | return FALSE; |
907 | 0 | } |
908 | | |
909 | 0 | if ((sec_flags & SEC_EXCHANGE_PKT) == 0) |
910 | 0 | { |
911 | 0 | WLog_Print(rdp->log, WLOG_ERROR, "missing SEC_EXCHANGE_PKT in security header"); |
912 | 0 | return FALSE; |
913 | 0 | } |
914 | | |
915 | 0 | rdp->do_crypt_license = (sec_flags & SEC_LICENSE_ENCRYPT_SC) != 0 ? TRUE : FALSE; |
916 | |
|
917 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 4)) |
918 | 0 | return FALSE; |
919 | | |
920 | 0 | Stream_Read_UINT32(s, rand_len); |
921 | | |
922 | | /* rand_len already includes 8 bytes of padding */ |
923 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, rand_len)) |
924 | 0 | return FALSE; |
925 | | |
926 | 0 | const BYTE* crypt_random = Stream_ConstPointer(s); |
927 | 0 | if (!Stream_SafeSeek(s, rand_len)) |
928 | 0 | goto end; |
929 | 0 | if (!rdp_update_client_random(rdp->settings, crypt_random, rand_len)) |
930 | 0 | goto end; |
931 | | |
932 | | /* now calculate encrypt / decrypt and update keys */ |
933 | 0 | if (!security_establish_keys(rdp)) |
934 | 0 | goto end; |
935 | | |
936 | 0 | rdp->do_crypt = TRUE; |
937 | |
|
938 | 0 | if (rdp->settings->EncryptionMethods == ENCRYPTION_METHOD_FIPS) |
939 | 0 | { |
940 | 0 | rdp->fips_encrypt = |
941 | 0 | winpr_Cipher_NewEx(WINPR_CIPHER_DES_EDE3_CBC, WINPR_ENCRYPT, rdp->fips_encrypt_key, |
942 | 0 | sizeof(rdp->fips_encrypt_key), fips_ivec, sizeof(fips_ivec)); |
943 | |
|
944 | 0 | if (!rdp->fips_encrypt) |
945 | 0 | { |
946 | 0 | WLog_Print(rdp->log, WLOG_ERROR, "unable to allocate des3 encrypt key"); |
947 | 0 | goto end; |
948 | 0 | } |
949 | | |
950 | 0 | rdp->fips_decrypt = |
951 | 0 | winpr_Cipher_NewEx(WINPR_CIPHER_DES_EDE3_CBC, WINPR_DECRYPT, rdp->fips_decrypt_key, |
952 | 0 | sizeof(rdp->fips_decrypt_key), fips_ivec, sizeof(fips_ivec)); |
953 | |
|
954 | 0 | if (!rdp->fips_decrypt) |
955 | 0 | { |
956 | 0 | WLog_Print(rdp->log, WLOG_ERROR, "unable to allocate des3 decrypt key"); |
957 | 0 | goto end; |
958 | 0 | } |
959 | | |
960 | 0 | ret = TRUE; |
961 | 0 | goto end; |
962 | 0 | } |
963 | | |
964 | 0 | if (!rdp_reset_rc4_encrypt_keys(rdp)) |
965 | 0 | goto end; |
966 | | |
967 | 0 | if (!rdp_reset_rc4_decrypt_keys(rdp)) |
968 | 0 | goto end; |
969 | | |
970 | 0 | ret = tpkt_ensure_stream_consumed(rdp->log, s, length); |
971 | 0 | end: |
972 | |
|
973 | 0 | if (!ret) |
974 | 0 | { |
975 | 0 | winpr_Cipher_Free(rdp->fips_encrypt); |
976 | 0 | winpr_Cipher_Free(rdp->fips_decrypt); |
977 | 0 | rdp->fips_encrypt = NULL; |
978 | 0 | rdp->fips_decrypt = NULL; |
979 | |
|
980 | 0 | rdp_free_rc4_encrypt_keys(rdp); |
981 | 0 | rdp_free_rc4_decrypt_keys(rdp); |
982 | 0 | } |
983 | |
|
984 | 0 | return ret; |
985 | 0 | } |
986 | | |
987 | | static BOOL rdp_client_send_client_info_and_change_state(rdpRdp* rdp) |
988 | 0 | { |
989 | 0 | WINPR_ASSERT(rdp); |
990 | 0 | if (!rdp_client_establish_keys(rdp)) |
991 | 0 | return FALSE; |
992 | 0 | if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_SECURE_SETTINGS_EXCHANGE)) |
993 | 0 | return FALSE; |
994 | 0 | if (!rdp_send_client_info(rdp)) |
995 | 0 | return FALSE; |
996 | 0 | if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_CONNECT_TIME_AUTO_DETECT_REQUEST)) |
997 | 0 | return FALSE; |
998 | 0 | return TRUE; |
999 | 0 | } |
1000 | | |
1001 | | BOOL rdp_client_skip_mcs_channel_join(rdpRdp* rdp) |
1002 | 0 | { |
1003 | 0 | WINPR_ASSERT(rdp); |
1004 | | |
1005 | 0 | rdpMcs* mcs = rdp->mcs; |
1006 | 0 | WINPR_ASSERT(mcs); |
1007 | | |
1008 | 0 | mcs->userChannelJoined = TRUE; |
1009 | 0 | mcs->globalChannelJoined = TRUE; |
1010 | 0 | mcs->messageChannelJoined = TRUE; |
1011 | |
|
1012 | 0 | for (UINT32 i = 0; i < mcs->channelCount; i++) |
1013 | 0 | { |
1014 | 0 | rdpMcsChannel* cur = &mcs->channels[i]; |
1015 | 0 | WLog_DBG(TAG, " %s [%" PRIu16 "]", cur->Name, cur->ChannelId); |
1016 | 0 | cur->joined = TRUE; |
1017 | 0 | } |
1018 | |
|
1019 | 0 | return rdp_client_send_client_info_and_change_state(rdp); |
1020 | 0 | } |
1021 | | |
1022 | | static BOOL rdp_client_join_channel(rdpRdp* rdp, UINT16 ChannelId) |
1023 | 0 | { |
1024 | 0 | WINPR_ASSERT(rdp); |
1025 | | |
1026 | 0 | rdpMcs* mcs = rdp->mcs; |
1027 | 0 | if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_MCS_CHANNEL_JOIN_REQUEST)) |
1028 | 0 | return FALSE; |
1029 | 0 | if (!mcs_send_channel_join_request(mcs, ChannelId)) |
1030 | 0 | return FALSE; |
1031 | 0 | if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_MCS_CHANNEL_JOIN_RESPONSE)) |
1032 | 0 | return FALSE; |
1033 | 0 | return TRUE; |
1034 | 0 | } |
1035 | | |
1036 | | BOOL rdp_client_connect_mcs_channel_join_confirm(rdpRdp* rdp, wStream* s) |
1037 | 0 | { |
1038 | 0 | UINT16 channelId = 0; |
1039 | 0 | BOOL allJoined = TRUE; |
1040 | |
|
1041 | 0 | WINPR_ASSERT(rdp); |
1042 | 0 | rdpMcs* mcs = rdp->mcs; |
1043 | |
|
1044 | 0 | if (!mcs_recv_channel_join_confirm(mcs, s, &channelId)) |
1045 | 0 | return FALSE; |
1046 | | |
1047 | 0 | if (!mcs->userChannelJoined) |
1048 | 0 | { |
1049 | 0 | if (channelId != mcs->userId) |
1050 | 0 | { |
1051 | 0 | WLog_ERR(TAG, "expected user channel id %" PRIu16 ", but received %" PRIu16, |
1052 | 0 | mcs->userId, channelId); |
1053 | 0 | return FALSE; |
1054 | 0 | } |
1055 | | |
1056 | 0 | mcs->userChannelJoined = TRUE; |
1057 | 0 | if (!rdp_client_join_channel(rdp, MCS_GLOBAL_CHANNEL_ID)) |
1058 | 0 | return FALSE; |
1059 | 0 | } |
1060 | 0 | else if (!mcs->globalChannelJoined) |
1061 | 0 | { |
1062 | 0 | if (channelId != MCS_GLOBAL_CHANNEL_ID) |
1063 | 0 | { |
1064 | 0 | WLog_ERR(TAG, "expected uglobalser channel id %" PRIu16 ", but received %" PRIu16, |
1065 | 0 | MCS_GLOBAL_CHANNEL_ID, channelId); |
1066 | 0 | return FALSE; |
1067 | 0 | } |
1068 | 0 | mcs->globalChannelJoined = TRUE; |
1069 | |
|
1070 | 0 | if (mcs->messageChannelId != 0) |
1071 | 0 | { |
1072 | 0 | if (!rdp_client_join_channel(rdp, mcs->messageChannelId)) |
1073 | 0 | return FALSE; |
1074 | 0 | allJoined = FALSE; |
1075 | 0 | } |
1076 | 0 | else |
1077 | 0 | { |
1078 | 0 | if (mcs->channelCount > 0) |
1079 | 0 | { |
1080 | 0 | const rdpMcsChannel* cur = &mcs->channels[0]; |
1081 | 0 | if (!rdp_client_join_channel(rdp, cur->ChannelId)) |
1082 | 0 | return FALSE; |
1083 | 0 | allJoined = FALSE; |
1084 | 0 | } |
1085 | 0 | } |
1086 | 0 | } |
1087 | 0 | else if ((mcs->messageChannelId != 0) && !mcs->messageChannelJoined) |
1088 | 0 | { |
1089 | 0 | if (channelId != mcs->messageChannelId) |
1090 | 0 | { |
1091 | 0 | WLog_ERR(TAG, "expected messageChannelId=%" PRIu16 ", got %" PRIu16, |
1092 | 0 | mcs->messageChannelId, channelId); |
1093 | 0 | return FALSE; |
1094 | 0 | } |
1095 | | |
1096 | 0 | mcs->messageChannelJoined = TRUE; |
1097 | |
|
1098 | 0 | if (mcs->channelCount > 0) |
1099 | 0 | { |
1100 | 0 | const rdpMcsChannel* cur = &mcs->channels[0]; |
1101 | 0 | if (!rdp_client_join_channel(rdp, cur->ChannelId)) |
1102 | 0 | return FALSE; |
1103 | 0 | allJoined = FALSE; |
1104 | 0 | } |
1105 | 0 | } |
1106 | 0 | else |
1107 | 0 | { |
1108 | 0 | UINT32 i = 0; |
1109 | 0 | for (; i < mcs->channelCount; i++) |
1110 | 0 | { |
1111 | 0 | rdpMcsChannel* cur = &mcs->channels[i]; |
1112 | 0 | if (cur->joined) |
1113 | 0 | continue; |
1114 | | |
1115 | 0 | if (cur->ChannelId != channelId) |
1116 | 0 | { |
1117 | 0 | WLog_ERR(TAG, "expected channel id %" PRIu16 ", but received %" PRIu16, |
1118 | 0 | MCS_GLOBAL_CHANNEL_ID, channelId); |
1119 | 0 | return FALSE; |
1120 | 0 | } |
1121 | 0 | cur->joined = TRUE; |
1122 | 0 | break; |
1123 | 0 | } |
1124 | | |
1125 | 0 | if (i + 1 < mcs->channelCount) |
1126 | 0 | { |
1127 | 0 | const rdpMcsChannel* cur = &mcs->channels[i + 1]; |
1128 | 0 | if (!rdp_client_join_channel(rdp, cur->ChannelId)) |
1129 | 0 | return FALSE; |
1130 | 0 | allJoined = FALSE; |
1131 | 0 | } |
1132 | 0 | } |
1133 | | |
1134 | 0 | if (mcs->userChannelJoined && mcs->globalChannelJoined && allJoined) |
1135 | 0 | { |
1136 | 0 | if (!rdp_client_send_client_info_and_change_state(rdp)) |
1137 | 0 | return FALSE; |
1138 | 0 | } |
1139 | | |
1140 | 0 | return TRUE; |
1141 | 0 | } |
1142 | | |
1143 | | state_run_t rdp_handle_message_channel(rdpRdp* rdp, wStream* s, UINT16 channelId, UINT16 length) |
1144 | 0 | { |
1145 | 0 | WINPR_ASSERT(rdp); |
1146 | 0 | WINPR_ASSERT(rdp->mcs); |
1147 | | |
1148 | 0 | if (!rdp->mcs->messageChannelJoined) |
1149 | 0 | { |
1150 | 0 | WLog_Print(rdp->log, WLOG_WARN, "MCS message channel not joined!"); |
1151 | 0 | return STATE_RUN_FAILED; |
1152 | 0 | } |
1153 | 0 | const UINT16 messageChannelId = rdp->mcs->messageChannelId; |
1154 | 0 | if (messageChannelId == 0) |
1155 | 0 | { |
1156 | 0 | WLog_Print(rdp->log, WLOG_WARN, "MCS message channel id == 0"); |
1157 | 0 | return STATE_RUN_FAILED; |
1158 | 0 | } |
1159 | | |
1160 | 0 | if ((channelId != messageChannelId) && (channelId != MCS_GLOBAL_CHANNEL_ID)) |
1161 | 0 | { |
1162 | 0 | WLog_Print(rdp->log, WLOG_WARN, |
1163 | 0 | "MCS message channel expected id=[%" PRIu16 "|%d], got %" PRIu16, |
1164 | 0 | messageChannelId, MCS_GLOBAL_CHANNEL_ID, channelId); |
1165 | 0 | return STATE_RUN_FAILED; |
1166 | 0 | } |
1167 | | |
1168 | 0 | UINT16 securityFlags = 0; |
1169 | 0 | if (!rdp_read_security_header(rdp, s, &securityFlags, &length)) |
1170 | 0 | return STATE_RUN_FAILED; |
1171 | | |
1172 | 0 | if (securityFlags & SEC_ENCRYPT) |
1173 | 0 | { |
1174 | 0 | if (!rdp_decrypt(rdp, s, &length, securityFlags)) |
1175 | 0 | return STATE_RUN_FAILED; |
1176 | 0 | } |
1177 | | |
1178 | 0 | const state_run_t rc = rdp_recv_message_channel_pdu(rdp, s, securityFlags); |
1179 | 0 | if (state_run_success(rc)) |
1180 | 0 | { |
1181 | 0 | if (!tpkt_ensure_stream_consumed(rdp->log, s, length)) |
1182 | 0 | return STATE_RUN_FAILED; |
1183 | 0 | } |
1184 | 0 | return rc; |
1185 | 0 | } |
1186 | | |
1187 | | BOOL rdp_client_connect_auto_detect(rdpRdp* rdp, wStream* s, DWORD logLevel) |
1188 | 0 | { |
1189 | 0 | WINPR_ASSERT(rdp); |
1190 | 0 | WINPR_ASSERT(rdp->mcs); |
1191 | | |
1192 | 0 | const size_t pos = Stream_GetPosition(s); |
1193 | 0 | UINT16 length = 0; |
1194 | 0 | UINT16 channelId = 0; |
1195 | |
|
1196 | 0 | if (rdp_read_header(rdp, s, &length, &channelId)) |
1197 | 0 | { |
1198 | 0 | const UINT16 messageChannelId = rdp->mcs->messageChannelId; |
1199 | | /* If the MCS message channel has been joined... */ |
1200 | | |
1201 | | /* Process any MCS message channel PDUs. */ |
1202 | 0 | if (rdp->mcs->messageChannelJoined && (channelId == messageChannelId)) |
1203 | 0 | { |
1204 | 0 | const state_run_t rc = rdp_handle_message_channel(rdp, s, channelId, length); |
1205 | 0 | return state_run_success(rc); |
1206 | 0 | } |
1207 | 0 | else |
1208 | 0 | { |
1209 | 0 | wLog* log = WLog_Get(TAG); |
1210 | 0 | WLog_Print(log, logLevel, "expected messageChannelId=%" PRIu16 ", got %" PRIu16, |
1211 | 0 | messageChannelId, channelId); |
1212 | 0 | } |
1213 | 0 | } |
1214 | | |
1215 | 0 | Stream_SetPosition(s, pos); |
1216 | 0 | return FALSE; |
1217 | 0 | } |
1218 | | |
1219 | | state_run_t rdp_client_connect_license(rdpRdp* rdp, wStream* s) |
1220 | 0 | { |
1221 | 0 | state_run_t status = STATE_RUN_FAILED; |
1222 | 0 | LICENSE_STATE state = LICENSE_STATE_ABORTED; |
1223 | 0 | UINT16 length = 0; |
1224 | 0 | UINT16 channelId = 0; |
1225 | 0 | UINT16 securityFlags = 0; |
1226 | |
|
1227 | 0 | WINPR_ASSERT(rdp); |
1228 | 0 | if (!rdp_read_header(rdp, s, &length, &channelId)) |
1229 | 0 | return STATE_RUN_FAILED; |
1230 | | |
1231 | | /* there might be autodetect messages mixed in between licensing messages. |
1232 | | * that has been observed with 2k12 R2 and 2k19 |
1233 | | */ |
1234 | 0 | const UINT16 messageChannelId = rdp->mcs->messageChannelId; |
1235 | 0 | if (rdp->mcs->messageChannelJoined && (channelId == messageChannelId)) |
1236 | 0 | { |
1237 | 0 | return rdp_handle_message_channel(rdp, s, channelId, length); |
1238 | 0 | } |
1239 | | |
1240 | 0 | if (!rdp_read_security_header(rdp, s, &securityFlags, &length)) |
1241 | 0 | return STATE_RUN_FAILED; |
1242 | | |
1243 | 0 | if (securityFlags & SEC_ENCRYPT) |
1244 | 0 | { |
1245 | 0 | if (!rdp_decrypt(rdp, s, &length, securityFlags)) |
1246 | 0 | return STATE_RUN_FAILED; |
1247 | 0 | } |
1248 | | |
1249 | 0 | if (channelId != MCS_GLOBAL_CHANNEL_ID) |
1250 | 0 | WLog_WARN(TAG, "unexpected message for channel %u, expected %u", channelId, |
1251 | 0 | MCS_GLOBAL_CHANNEL_ID); |
1252 | |
|
1253 | 0 | if ((securityFlags & SEC_LICENSE_PKT) == 0) |
1254 | 0 | { |
1255 | 0 | char buffer[512] = { 0 }; |
1256 | 0 | char lbuffer[32] = { 0 }; |
1257 | 0 | WLog_ERR(TAG, "securityFlags=%s, missing required flag %s", |
1258 | 0 | rdp_security_flag_string(securityFlags, buffer, sizeof(buffer)), |
1259 | 0 | rdp_security_flag_string(SEC_LICENSE_PKT, lbuffer, sizeof(lbuffer))); |
1260 | 0 | return STATE_RUN_FAILED; |
1261 | 0 | } |
1262 | | |
1263 | 0 | status = license_recv(rdp->license, s); |
1264 | |
|
1265 | 0 | if (state_run_failed(status)) |
1266 | 0 | return status; |
1267 | | |
1268 | 0 | state = license_get_state(rdp->license); |
1269 | 0 | switch (state) |
1270 | 0 | { |
1271 | 0 | case LICENSE_STATE_ABORTED: |
1272 | 0 | WLog_ERR(TAG, "license connection sequence aborted."); |
1273 | 0 | return STATE_RUN_FAILED; |
1274 | 0 | case LICENSE_STATE_COMPLETED: |
1275 | 0 | if (rdp->settings->MultitransportFlags) |
1276 | 0 | { |
1277 | 0 | if (!rdp_client_transition_to_state( |
1278 | 0 | rdp, CONNECTION_STATE_MULTITRANSPORT_BOOTSTRAPPING_REQUEST)) |
1279 | 0 | return STATE_RUN_FAILED; |
1280 | 0 | } |
1281 | 0 | else |
1282 | 0 | { |
1283 | 0 | if (!rdp_client_transition_to_state( |
1284 | 0 | rdp, CONNECTION_STATE_CAPABILITIES_EXCHANGE_DEMAND_ACTIVE)) |
1285 | 0 | return STATE_RUN_FAILED; |
1286 | 0 | } |
1287 | 0 | return STATE_RUN_SUCCESS; |
1288 | 0 | default: |
1289 | 0 | return STATE_RUN_SUCCESS; |
1290 | 0 | } |
1291 | 0 | } |
1292 | | |
1293 | | state_run_t rdp_client_connect_demand_active(rdpRdp* rdp, wStream* s) |
1294 | 0 | { |
1295 | 0 | UINT16 length = 0; |
1296 | 0 | UINT16 channelId = 0; |
1297 | 0 | UINT16 pduType = 0; |
1298 | 0 | UINT16 pduSource = 0; |
1299 | |
|
1300 | 0 | WINPR_ASSERT(rdp); |
1301 | 0 | WINPR_ASSERT(s); |
1302 | 0 | WINPR_ASSERT(rdp->settings); |
1303 | | |
1304 | 0 | if (!rdp_recv_get_active_header(rdp, s, &channelId, &length)) |
1305 | 0 | return STATE_RUN_FAILED; |
1306 | | |
1307 | 0 | if (freerdp_shall_disconnect_context(rdp->context)) |
1308 | 0 | return STATE_RUN_QUIT_SESSION; |
1309 | | |
1310 | 0 | if (rdp->mcs->messageChannelId && (channelId == rdp->mcs->messageChannelId)) |
1311 | 0 | { |
1312 | 0 | rdp->inPackets++; |
1313 | 0 | return rdp_handle_message_channel(rdp, s, channelId, length); |
1314 | 0 | } |
1315 | | |
1316 | 0 | if (!rdp_handle_optional_rdp_decryption(rdp, s, &length, NULL)) |
1317 | 0 | return STATE_RUN_FAILED; |
1318 | | |
1319 | 0 | if (!rdp_read_share_control_header(rdp, s, NULL, NULL, &pduType, &pduSource)) |
1320 | 0 | return STATE_RUN_FAILED; |
1321 | | |
1322 | 0 | switch (pduType) |
1323 | 0 | { |
1324 | 0 | case PDU_TYPE_DEMAND_ACTIVE: |
1325 | 0 | if (!rdp_recv_demand_active(rdp, s, pduSource, length)) |
1326 | 0 | return STATE_RUN_FAILED; |
1327 | 0 | return STATE_RUN_ACTIVE; |
1328 | 0 | default: |
1329 | 0 | return rdp_recv_out_of_sequence_pdu(rdp, s, pduType, length); |
1330 | 0 | } |
1331 | 0 | } |
1332 | | |
1333 | | state_run_t rdp_client_connect_finalize(rdpRdp* rdp) |
1334 | 0 | { |
1335 | 0 | WINPR_ASSERT(rdp); |
1336 | | /** |
1337 | | * [MS-RDPBCGR] 1.3.1.1 - 8. |
1338 | | * The client-to-server PDUs sent during this phase have no dependencies on any of the |
1339 | | * server-to- client PDUs; they may be sent as a single batch, provided that sequencing is |
1340 | | * maintained. |
1341 | | */ |
1342 | 0 | if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_SYNC)) |
1343 | 0 | return STATE_RUN_FAILED; |
1344 | | |
1345 | 0 | if (!rdp_send_client_synchronize_pdu(rdp)) |
1346 | 0 | return STATE_RUN_FAILED; |
1347 | | |
1348 | 0 | if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_COOPERATE)) |
1349 | 0 | return STATE_RUN_FAILED; |
1350 | 0 | if (!rdp_send_client_control_pdu(rdp, CTRLACTION_COOPERATE)) |
1351 | 0 | return STATE_RUN_FAILED; |
1352 | | |
1353 | 0 | if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_REQUEST_CONTROL)) |
1354 | 0 | return STATE_RUN_FAILED; |
1355 | 0 | if (!rdp_send_client_control_pdu(rdp, CTRLACTION_REQUEST_CONTROL)) |
1356 | 0 | return STATE_RUN_FAILED; |
1357 | | |
1358 | | /** |
1359 | | * [MS-RDPBCGR] 2.2.1.17 |
1360 | | * Client persistent key list must be sent if a bitmap is |
1361 | | * stored in persistent bitmap cache or the server has advertised support for bitmap |
1362 | | * host cache and a deactivation reactivation sequence is *not* in progress. |
1363 | | */ |
1364 | | |
1365 | 0 | if (!rdp_finalize_is_flag_set(rdp, FINALIZE_DEACTIVATE_REACTIVATE) && |
1366 | 0 | freerdp_settings_get_bool(rdp->settings, FreeRDP_BitmapCachePersistEnabled)) |
1367 | 0 | { |
1368 | 0 | if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_PERSISTENT_KEY_LIST)) |
1369 | 0 | return STATE_RUN_FAILED; |
1370 | 0 | if (!rdp_send_client_persistent_key_list_pdu(rdp)) |
1371 | 0 | return STATE_RUN_FAILED; |
1372 | 0 | } |
1373 | | |
1374 | 0 | if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_FONT_LIST)) |
1375 | 0 | return STATE_RUN_FAILED; |
1376 | 0 | if (!rdp_send_client_font_list_pdu(rdp, FONTLIST_FIRST | FONTLIST_LAST)) |
1377 | 0 | return STATE_RUN_FAILED; |
1378 | | |
1379 | 0 | if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_CLIENT_SYNC)) |
1380 | 0 | return STATE_RUN_FAILED; |
1381 | 0 | return STATE_RUN_SUCCESS; |
1382 | 0 | } |
1383 | | |
1384 | | BOOL rdp_client_transition_to_state(rdpRdp* rdp, CONNECTION_STATE state) |
1385 | 0 | { |
1386 | 0 | const char* name = rdp_state_string(state); |
1387 | |
|
1388 | 0 | WINPR_ASSERT(rdp); |
1389 | 0 | WLog_Print(rdp->log, WLOG_DEBUG, "%s --> %s", rdp_get_state_string(rdp), name); |
1390 | |
|
1391 | 0 | if (!rdp_set_state(rdp, state)) |
1392 | 0 | return FALSE; |
1393 | | |
1394 | 0 | switch (state) |
1395 | 0 | { |
1396 | 0 | case CONNECTION_STATE_FINALIZATION_SYNC: |
1397 | 0 | case CONNECTION_STATE_FINALIZATION_COOPERATE: |
1398 | 0 | case CONNECTION_STATE_FINALIZATION_REQUEST_CONTROL: |
1399 | 0 | case CONNECTION_STATE_FINALIZATION_PERSISTENT_KEY_LIST: |
1400 | 0 | case CONNECTION_STATE_FINALIZATION_FONT_LIST: |
1401 | 0 | update_reset_state(rdp->update); |
1402 | 0 | break; |
1403 | | |
1404 | 0 | case CONNECTION_STATE_CAPABILITIES_EXCHANGE_CONFIRM_ACTIVE: |
1405 | 0 | { |
1406 | 0 | ActivatedEventArgs activatedEvent = { 0 }; |
1407 | 0 | rdpContext* context = rdp->context; |
1408 | 0 | EventArgsInit(&activatedEvent, "libfreerdp"); |
1409 | 0 | activatedEvent.firstActivation = |
1410 | 0 | !rdp_finalize_is_flag_set(rdp, FINALIZE_DEACTIVATE_REACTIVATE); |
1411 | 0 | PubSub_OnActivated(rdp->pubSub, context, &activatedEvent); |
1412 | 0 | } |
1413 | |
|
1414 | 0 | break; |
1415 | | |
1416 | 0 | default: |
1417 | 0 | break; |
1418 | 0 | } |
1419 | | |
1420 | 0 | { |
1421 | 0 | ConnectionStateChangeEventArgs stateEvent = { 0 }; |
1422 | 0 | rdpContext* context = rdp->context; |
1423 | 0 | EventArgsInit(&stateEvent, "libfreerdp"); |
1424 | 0 | stateEvent.state = WINPR_ASSERTING_INT_CAST(int32_t, rdp_get_state(rdp)); |
1425 | 0 | stateEvent.active = rdp_is_active_state(rdp); |
1426 | 0 | PubSub_OnConnectionStateChange(rdp->pubSub, context, &stateEvent); |
1427 | 0 | } |
1428 | | |
1429 | 0 | return TRUE; |
1430 | 0 | } |
1431 | | |
1432 | | BOOL rdp_server_accept_nego(rdpRdp* rdp, wStream* s) |
1433 | 0 | { |
1434 | 0 | UINT32 SelectedProtocol = 0; |
1435 | 0 | UINT32 RequestedProtocols = 0; |
1436 | 0 | BOOL status = 0; |
1437 | 0 | rdpSettings* settings = NULL; |
1438 | 0 | rdpNego* nego = NULL; |
1439 | |
|
1440 | 0 | WINPR_ASSERT(rdp); |
1441 | 0 | WINPR_ASSERT(s); |
1442 | | |
1443 | 0 | settings = rdp->settings; |
1444 | 0 | WINPR_ASSERT(settings); |
1445 | | |
1446 | 0 | nego = rdp->nego; |
1447 | 0 | WINPR_ASSERT(nego); |
1448 | | |
1449 | 0 | transport_set_blocking_mode(rdp->transport, TRUE); |
1450 | |
|
1451 | 0 | if (!nego_read_request(nego, s)) |
1452 | 0 | return FALSE; |
1453 | | |
1454 | 0 | RequestedProtocols = nego_get_requested_protocols(nego); |
1455 | 0 | WLog_DBG(TAG, "Client Security: RDSTLS:%d NLA:%d TLS:%d RDP:%d", |
1456 | 0 | (RequestedProtocols & PROTOCOL_RDSTLS) ? 1 : 0, |
1457 | 0 | (RequestedProtocols & PROTOCOL_HYBRID) ? 1 : 0, |
1458 | 0 | (RequestedProtocols & PROTOCOL_SSL) ? 1 : 0, |
1459 | 0 | (RequestedProtocols == PROTOCOL_RDP) ? 1 : 0); |
1460 | 0 | WLog_DBG(TAG, |
1461 | 0 | "Server Security: RDSTLS:%" PRId32 " NLA:%" PRId32 " TLS:%" PRId32 " RDP:%" PRId32 "", |
1462 | 0 | settings->RdstlsSecurity, settings->NlaSecurity, settings->TlsSecurity, |
1463 | 0 | settings->RdpSecurity); |
1464 | |
|
1465 | 0 | if ((settings->RdstlsSecurity) && (RequestedProtocols & PROTOCOL_RDSTLS)) |
1466 | 0 | { |
1467 | 0 | SelectedProtocol = PROTOCOL_RDSTLS; |
1468 | 0 | } |
1469 | 0 | else if ((settings->NlaSecurity) && (RequestedProtocols & PROTOCOL_HYBRID)) |
1470 | 0 | { |
1471 | 0 | SelectedProtocol = PROTOCOL_HYBRID; |
1472 | 0 | } |
1473 | 0 | else if ((settings->TlsSecurity) && (RequestedProtocols & PROTOCOL_SSL)) |
1474 | 0 | { |
1475 | 0 | SelectedProtocol = PROTOCOL_SSL; |
1476 | 0 | } |
1477 | 0 | else if ((settings->RdpSecurity) && (RequestedProtocols == PROTOCOL_RDP)) |
1478 | 0 | { |
1479 | 0 | SelectedProtocol = PROTOCOL_RDP; |
1480 | 0 | } |
1481 | 0 | else |
1482 | 0 | { |
1483 | | /* |
1484 | | * when here client and server aren't compatible, we select the right |
1485 | | * error message to return to the client in the nego failure packet |
1486 | | */ |
1487 | 0 | SelectedProtocol = PROTOCOL_FAILED_NEGO; |
1488 | |
|
1489 | 0 | if (settings->RdpSecurity) |
1490 | 0 | { |
1491 | 0 | WLog_ERR(TAG, "server supports only Standard RDP Security"); |
1492 | 0 | SelectedProtocol |= SSL_NOT_ALLOWED_BY_SERVER; |
1493 | 0 | } |
1494 | 0 | else |
1495 | 0 | { |
1496 | 0 | if (settings->NlaSecurity && !settings->TlsSecurity) |
1497 | 0 | { |
1498 | 0 | WLog_WARN(TAG, "server supports only NLA Security"); |
1499 | 0 | SelectedProtocol |= HYBRID_REQUIRED_BY_SERVER; |
1500 | 0 | } |
1501 | 0 | else |
1502 | 0 | { |
1503 | 0 | WLog_WARN(TAG, "server supports only a SSL based Security (TLS or NLA)"); |
1504 | 0 | SelectedProtocol |= SSL_REQUIRED_BY_SERVER; |
1505 | 0 | } |
1506 | 0 | } |
1507 | |
|
1508 | 0 | WLog_ERR(TAG, "Protocol security negotiation failure"); |
1509 | 0 | } |
1510 | |
|
1511 | 0 | if (!(SelectedProtocol & PROTOCOL_FAILED_NEGO)) |
1512 | 0 | { |
1513 | 0 | WLog_DBG(TAG, "Negotiated Security: RDSTLS:%d NLA:%d TLS:%d RDP:%d", |
1514 | 0 | (SelectedProtocol & PROTOCOL_RDSTLS) ? 1 : 0, |
1515 | 0 | (SelectedProtocol & PROTOCOL_HYBRID) ? 1 : 0, |
1516 | 0 | (SelectedProtocol & PROTOCOL_SSL) ? 1 : 0, |
1517 | 0 | (SelectedProtocol == PROTOCOL_RDP) ? 1 : 0); |
1518 | 0 | } |
1519 | |
|
1520 | 0 | if (!nego_set_selected_protocol(nego, SelectedProtocol)) |
1521 | 0 | return FALSE; |
1522 | | |
1523 | 0 | if (!nego_send_negotiation_response(nego)) |
1524 | 0 | return FALSE; |
1525 | | |
1526 | 0 | SelectedProtocol = nego_get_selected_protocol(nego); |
1527 | 0 | status = FALSE; |
1528 | |
|
1529 | 0 | if (freerdp_settings_get_bool(rdp->settings, FreeRDP_VmConnectMode) && |
1530 | 0 | SelectedProtocol != PROTOCOL_RDP) |
1531 | | /* When behind a Hyper-V proxy, security != RDP is handled by the host. */ |
1532 | 0 | status = TRUE; |
1533 | 0 | else if (SelectedProtocol & PROTOCOL_RDSTLS) |
1534 | 0 | status = transport_accept_rdstls(rdp->transport); |
1535 | 0 | else if (SelectedProtocol & PROTOCOL_HYBRID) |
1536 | 0 | status = transport_accept_nla(rdp->transport); |
1537 | 0 | else if (SelectedProtocol & PROTOCOL_SSL) |
1538 | 0 | status = transport_accept_tls(rdp->transport); |
1539 | 0 | else if (SelectedProtocol == PROTOCOL_RDP) /* 0 */ |
1540 | 0 | status = transport_accept_rdp(rdp->transport); |
1541 | |
|
1542 | 0 | if (!status) |
1543 | 0 | return FALSE; |
1544 | | |
1545 | 0 | transport_set_blocking_mode(rdp->transport, FALSE); |
1546 | |
|
1547 | 0 | return TRUE; |
1548 | 0 | } |
1549 | | |
1550 | | static BOOL rdp_update_encryption_level(rdpSettings* settings) |
1551 | 0 | { |
1552 | 0 | WINPR_ASSERT(settings); |
1553 | | |
1554 | 0 | UINT32 EncryptionLevel = freerdp_settings_get_uint32(settings, FreeRDP_EncryptionLevel); |
1555 | 0 | UINT32 EncryptionMethods = freerdp_settings_get_uint32(settings, FreeRDP_EncryptionMethods); |
1556 | | |
1557 | | /** |
1558 | | * Re: settings->EncryptionLevel: |
1559 | | * This is configured/set by the server implementation and serves the same |
1560 | | * purpose as the "Encryption Level" setting in the RDP-Tcp configuration |
1561 | | * dialog of Microsoft's Remote Desktop Session Host Configuration. |
1562 | | * Re: settings->EncryptionMethods: |
1563 | | * at this point this setting contains the client's supported encryption |
1564 | | * methods we've received in gcc_read_client_security_data() |
1565 | | */ |
1566 | |
|
1567 | 0 | if (!settings->UseRdpSecurityLayer) |
1568 | 0 | { |
1569 | | /* TLS/NLA is used: disable rdp style encryption */ |
1570 | 0 | EncryptionLevel = ENCRYPTION_LEVEL_NONE; |
1571 | 0 | } |
1572 | 0 | else |
1573 | 0 | { |
1574 | | /* verify server encryption level value */ |
1575 | 0 | switch (EncryptionLevel) |
1576 | 0 | { |
1577 | 0 | case ENCRYPTION_LEVEL_NONE: |
1578 | 0 | WLog_INFO(TAG, "Active rdp encryption level: NONE"); |
1579 | 0 | break; |
1580 | | |
1581 | 0 | case ENCRYPTION_LEVEL_FIPS: |
1582 | 0 | WLog_INFO(TAG, "Active rdp encryption level: FIPS Compliant"); |
1583 | 0 | break; |
1584 | | |
1585 | 0 | case ENCRYPTION_LEVEL_HIGH: |
1586 | 0 | WLog_INFO(TAG, "Active rdp encryption level: HIGH"); |
1587 | 0 | break; |
1588 | | |
1589 | 0 | case ENCRYPTION_LEVEL_LOW: |
1590 | 0 | WLog_INFO(TAG, "Active rdp encryption level: LOW"); |
1591 | 0 | break; |
1592 | | |
1593 | 0 | case ENCRYPTION_LEVEL_CLIENT_COMPATIBLE: |
1594 | 0 | WLog_INFO(TAG, "Active rdp encryption level: CLIENT-COMPATIBLE"); |
1595 | 0 | break; |
1596 | | |
1597 | 0 | default: |
1598 | 0 | WLog_ERR(TAG, "Invalid server encryption level 0x%08" PRIX32 "", EncryptionLevel); |
1599 | 0 | WLog_ERR(TAG, "Switching to encryption level CLIENT-COMPATIBLE"); |
1600 | 0 | EncryptionLevel = ENCRYPTION_LEVEL_CLIENT_COMPATIBLE; |
1601 | 0 | } |
1602 | 0 | } |
1603 | | |
1604 | | /* choose rdp encryption method based on server level and client methods */ |
1605 | 0 | switch (EncryptionLevel) |
1606 | 0 | { |
1607 | 0 | case ENCRYPTION_LEVEL_NONE: |
1608 | | /* The only valid method is NONE in this case */ |
1609 | 0 | EncryptionMethods = ENCRYPTION_METHOD_NONE; |
1610 | 0 | break; |
1611 | | |
1612 | 0 | case ENCRYPTION_LEVEL_FIPS: |
1613 | | |
1614 | | /* The only valid method is FIPS in this case */ |
1615 | 0 | if (!(EncryptionMethods & ENCRYPTION_METHOD_FIPS)) |
1616 | 0 | { |
1617 | 0 | WLog_WARN(TAG, "client does not support FIPS as required by server configuration"); |
1618 | 0 | } |
1619 | |
|
1620 | 0 | EncryptionMethods = ENCRYPTION_METHOD_FIPS; |
1621 | 0 | break; |
1622 | | |
1623 | 0 | case ENCRYPTION_LEVEL_HIGH: |
1624 | | |
1625 | | /* Maximum key strength supported by the server must be used (128 bit)*/ |
1626 | 0 | if (!(EncryptionMethods & ENCRYPTION_METHOD_128BIT)) |
1627 | 0 | { |
1628 | 0 | WLog_WARN(TAG, "client does not support 128 bit encryption method as required by " |
1629 | 0 | "server configuration"); |
1630 | 0 | } |
1631 | |
|
1632 | 0 | EncryptionMethods = ENCRYPTION_METHOD_128BIT; |
1633 | 0 | break; |
1634 | | |
1635 | 0 | case ENCRYPTION_LEVEL_LOW: |
1636 | 0 | case ENCRYPTION_LEVEL_CLIENT_COMPATIBLE: |
1637 | | |
1638 | | /* Maximum key strength supported by the client must be used */ |
1639 | 0 | if (EncryptionMethods & ENCRYPTION_METHOD_128BIT) |
1640 | 0 | EncryptionMethods = ENCRYPTION_METHOD_128BIT; |
1641 | 0 | else if (EncryptionMethods & ENCRYPTION_METHOD_56BIT) |
1642 | 0 | EncryptionMethods = ENCRYPTION_METHOD_56BIT; |
1643 | 0 | else if (EncryptionMethods & ENCRYPTION_METHOD_40BIT) |
1644 | 0 | EncryptionMethods = ENCRYPTION_METHOD_40BIT; |
1645 | 0 | else if (EncryptionMethods & ENCRYPTION_METHOD_FIPS) |
1646 | 0 | EncryptionMethods = ENCRYPTION_METHOD_FIPS; |
1647 | 0 | else |
1648 | 0 | { |
1649 | 0 | WLog_WARN(TAG, "client has not announced any supported encryption methods"); |
1650 | 0 | EncryptionMethods = ENCRYPTION_METHOD_128BIT; |
1651 | 0 | } |
1652 | |
|
1653 | 0 | break; |
1654 | | |
1655 | 0 | default: |
1656 | 0 | WLog_ERR(TAG, "internal error: unknown encryption level"); |
1657 | 0 | return FALSE; |
1658 | 0 | } |
1659 | | |
1660 | | /* log selected encryption method */ |
1661 | 0 | if (settings->UseRdpSecurityLayer) |
1662 | 0 | { |
1663 | 0 | switch (EncryptionMethods) |
1664 | 0 | { |
1665 | 0 | case ENCRYPTION_METHOD_NONE: |
1666 | 0 | WLog_INFO(TAG, "Selected rdp encryption method: NONE"); |
1667 | 0 | break; |
1668 | | |
1669 | 0 | case ENCRYPTION_METHOD_40BIT: |
1670 | 0 | WLog_INFO(TAG, "Selected rdp encryption method: 40BIT"); |
1671 | 0 | break; |
1672 | | |
1673 | 0 | case ENCRYPTION_METHOD_56BIT: |
1674 | 0 | WLog_INFO(TAG, "Selected rdp encryption method: 56BIT"); |
1675 | 0 | break; |
1676 | | |
1677 | 0 | case ENCRYPTION_METHOD_128BIT: |
1678 | 0 | WLog_INFO(TAG, "Selected rdp encryption method: 128BIT"); |
1679 | 0 | break; |
1680 | | |
1681 | 0 | case ENCRYPTION_METHOD_FIPS: |
1682 | 0 | WLog_INFO(TAG, "Selected rdp encryption method: FIPS"); |
1683 | 0 | break; |
1684 | | |
1685 | 0 | default: |
1686 | 0 | WLog_ERR(TAG, "internal error: unknown encryption method"); |
1687 | 0 | return FALSE; |
1688 | 0 | } |
1689 | 0 | } |
1690 | | |
1691 | 0 | if (!freerdp_settings_set_uint32(settings, FreeRDP_EncryptionLevel, EncryptionLevel)) |
1692 | 0 | return FALSE; |
1693 | 0 | if (!freerdp_settings_set_uint32(settings, FreeRDP_EncryptionMethods, EncryptionMethods)) |
1694 | 0 | return FALSE; |
1695 | 0 | return TRUE; |
1696 | 0 | } |
1697 | | |
1698 | | BOOL rdp_server_accept_mcs_connect_initial(rdpRdp* rdp, wStream* s) |
1699 | 0 | { |
1700 | 0 | WINPR_ASSERT(rdp); |
1701 | 0 | WINPR_ASSERT(s); |
1702 | | |
1703 | 0 | rdpMcs* mcs = rdp->mcs; |
1704 | 0 | WINPR_ASSERT(mcs); |
1705 | | |
1706 | 0 | WINPR_ASSERT(rdp_get_state(rdp) == CONNECTION_STATE_MCS_CREATE_REQUEST); |
1707 | 0 | if (!mcs_recv_connect_initial(mcs, s)) |
1708 | 0 | return FALSE; |
1709 | 0 | WINPR_ASSERT(rdp->settings); |
1710 | | |
1711 | 0 | if (!mcs_server_apply_to_settings(mcs, rdp->settings)) |
1712 | 0 | return FALSE; |
1713 | | |
1714 | 0 | WLog_DBG(TAG, "Accepted client: %s", rdp->settings->ClientHostname); |
1715 | 0 | WLog_DBG(TAG, "Accepted channels:"); |
1716 | |
|
1717 | 0 | WINPR_ASSERT(mcs->channels || (mcs->channelCount == 0)); |
1718 | 0 | for (UINT32 i = 0; i < mcs->channelCount; i++) |
1719 | 0 | { |
1720 | 0 | ADDIN_ARGV* arg = NULL; |
1721 | 0 | rdpMcsChannel* cur = &mcs->channels[i]; |
1722 | 0 | const char* params[1] = { cur->Name }; |
1723 | 0 | WLog_DBG(TAG, " %s [%" PRIu16 "]", cur->Name, cur->ChannelId); |
1724 | 0 | arg = freerdp_addin_argv_new(ARRAYSIZE(params), params); |
1725 | 0 | if (!arg) |
1726 | 0 | return FALSE; |
1727 | | |
1728 | 0 | if (!freerdp_static_channel_collection_add(rdp->settings, arg)) |
1729 | 0 | { |
1730 | 0 | freerdp_addin_argv_free(arg); |
1731 | 0 | return FALSE; |
1732 | 0 | } |
1733 | 0 | } |
1734 | | |
1735 | 0 | if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_MCS_CREATE_RESPONSE)) |
1736 | 0 | return FALSE; |
1737 | 0 | if (!rdp_update_encryption_level(rdp->settings)) |
1738 | 0 | return FALSE; |
1739 | 0 | if (!mcs_send_connect_response(mcs)) |
1740 | 0 | return FALSE; |
1741 | 0 | if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_MCS_ERECT_DOMAIN)) |
1742 | 0 | return FALSE; |
1743 | | |
1744 | 0 | return TRUE; |
1745 | 0 | } |
1746 | | |
1747 | | BOOL rdp_server_accept_mcs_erect_domain_request(rdpRdp* rdp, wStream* s) |
1748 | 0 | { |
1749 | 0 | WINPR_ASSERT(rdp); |
1750 | 0 | WINPR_ASSERT(s); |
1751 | 0 | WINPR_ASSERT(rdp_get_state(rdp) == CONNECTION_STATE_MCS_ERECT_DOMAIN); |
1752 | | |
1753 | 0 | if (!mcs_recv_erect_domain_request(rdp->mcs, s)) |
1754 | 0 | return FALSE; |
1755 | | |
1756 | 0 | return rdp_server_transition_to_state(rdp, CONNECTION_STATE_MCS_ATTACH_USER); |
1757 | 0 | } |
1758 | | |
1759 | | static BOOL rdp_server_skip_mcs_channel_join(rdpRdp* rdp) |
1760 | 0 | { |
1761 | 0 | WINPR_ASSERT(rdp); |
1762 | | |
1763 | 0 | rdpMcs* mcs = rdp->mcs; |
1764 | 0 | WINPR_ASSERT(mcs); |
1765 | | |
1766 | 0 | mcs->userChannelJoined = TRUE; |
1767 | 0 | mcs->globalChannelJoined = TRUE; |
1768 | 0 | mcs->messageChannelJoined = TRUE; |
1769 | |
|
1770 | 0 | for (UINT32 i = 0; i < mcs->channelCount; i++) |
1771 | 0 | { |
1772 | 0 | rdpMcsChannel* cur = &mcs->channels[i]; |
1773 | 0 | WLog_DBG(TAG, " %s [%" PRIu16 "]", cur->Name, cur->ChannelId); |
1774 | 0 | cur->joined = TRUE; |
1775 | 0 | } |
1776 | 0 | return rdp_server_transition_to_state(rdp, CONNECTION_STATE_RDP_SECURITY_COMMENCEMENT); |
1777 | 0 | } |
1778 | | |
1779 | | BOOL rdp_server_accept_mcs_attach_user_request(rdpRdp* rdp, wStream* s) |
1780 | 0 | { |
1781 | 0 | if (!mcs_recv_attach_user_request(rdp->mcs, s)) |
1782 | 0 | return FALSE; |
1783 | | |
1784 | 0 | if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_MCS_ATTACH_USER_CONFIRM)) |
1785 | 0 | return FALSE; |
1786 | | |
1787 | 0 | if (!mcs_send_attach_user_confirm(rdp->mcs)) |
1788 | 0 | return FALSE; |
1789 | | |
1790 | 0 | if (freerdp_settings_get_bool(rdp->settings, FreeRDP_SupportSkipChannelJoin)) |
1791 | 0 | return rdp_server_skip_mcs_channel_join(rdp); |
1792 | 0 | return rdp_server_transition_to_state(rdp, CONNECTION_STATE_MCS_CHANNEL_JOIN_REQUEST); |
1793 | 0 | } |
1794 | | |
1795 | | BOOL rdp_server_accept_mcs_channel_join_request(rdpRdp* rdp, wStream* s) |
1796 | 0 | { |
1797 | 0 | UINT16 channelId = 0; |
1798 | 0 | BOOL allJoined = TRUE; |
1799 | 0 | rdpMcs* mcs = NULL; |
1800 | |
|
1801 | 0 | WINPR_ASSERT(rdp); |
1802 | 0 | WINPR_ASSERT(rdp->context); |
1803 | | |
1804 | 0 | mcs = rdp->mcs; |
1805 | 0 | WINPR_ASSERT(mcs); |
1806 | | |
1807 | 0 | WINPR_ASSERT(rdp_get_state(rdp) == CONNECTION_STATE_MCS_CHANNEL_JOIN_REQUEST); |
1808 | | |
1809 | 0 | if (!mcs_recv_channel_join_request(mcs, rdp->settings, s, &channelId)) |
1810 | 0 | return FALSE; |
1811 | | |
1812 | 0 | if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_MCS_CHANNEL_JOIN_RESPONSE)) |
1813 | 0 | return FALSE; |
1814 | | |
1815 | 0 | if (!mcs_send_channel_join_confirm(mcs, channelId)) |
1816 | 0 | return FALSE; |
1817 | | |
1818 | 0 | if (channelId == mcs->userId) |
1819 | 0 | mcs->userChannelJoined = TRUE; |
1820 | 0 | if (channelId == MCS_GLOBAL_CHANNEL_ID) |
1821 | 0 | mcs->globalChannelJoined = TRUE; |
1822 | 0 | if (channelId == mcs->messageChannelId) |
1823 | 0 | mcs->messageChannelJoined = TRUE; |
1824 | |
|
1825 | 0 | for (UINT32 i = 0; i < mcs->channelCount; i++) |
1826 | 0 | { |
1827 | 0 | rdpMcsChannel* cur = &mcs->channels[i]; |
1828 | 0 | WLog_DBG(TAG, " %s [%" PRIu16 "]", cur->Name, cur->ChannelId); |
1829 | 0 | if (cur->ChannelId == channelId) |
1830 | 0 | cur->joined = TRUE; |
1831 | |
|
1832 | 0 | if (!cur->joined) |
1833 | 0 | allJoined = FALSE; |
1834 | 0 | } |
1835 | |
|
1836 | 0 | CONNECTION_STATE rc = CONNECTION_STATE_INITIAL; |
1837 | 0 | if ((mcs->userChannelJoined) && (mcs->globalChannelJoined) && |
1838 | 0 | (mcs->messageChannelId == 0 || mcs->messageChannelJoined) && allJoined) |
1839 | 0 | rc = CONNECTION_STATE_RDP_SECURITY_COMMENCEMENT; |
1840 | 0 | else |
1841 | 0 | rc = CONNECTION_STATE_MCS_CHANNEL_JOIN_REQUEST; |
1842 | |
|
1843 | 0 | return rdp_server_transition_to_state(rdp, rc); |
1844 | 0 | } |
1845 | | |
1846 | | static BOOL rdp_server_send_sync(rdpRdp* rdp) |
1847 | 0 | { |
1848 | 0 | WINPR_ASSERT(rdp); |
1849 | | |
1850 | 0 | if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_CLIENT_SYNC)) |
1851 | 0 | return FALSE; |
1852 | 0 | if (!rdp_send_server_synchronize_pdu(rdp)) |
1853 | 0 | return FALSE; |
1854 | 0 | if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_CLIENT_COOPERATE)) |
1855 | 0 | return FALSE; |
1856 | 0 | if (!rdp_send_server_control_cooperate_pdu(rdp)) |
1857 | 0 | return FALSE; |
1858 | 0 | if (!rdp_finalize_reset_flags(rdp, FALSE)) |
1859 | 0 | return FALSE; |
1860 | 0 | return rdp_server_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_SYNC); |
1861 | 0 | } |
1862 | | |
1863 | | BOOL rdp_server_accept_confirm_active(rdpRdp* rdp, wStream* s, UINT16 pduLength) |
1864 | 0 | { |
1865 | 0 | WINPR_ASSERT(rdp); |
1866 | 0 | WINPR_ASSERT(rdp->context); |
1867 | 0 | WINPR_ASSERT(rdp->settings); |
1868 | 0 | WINPR_ASSERT(s); |
1869 | | |
1870 | 0 | freerdp_peer* peer = rdp->context->peer; |
1871 | 0 | WINPR_ASSERT(peer); |
1872 | | |
1873 | 0 | if (rdp_get_state(rdp) != CONNECTION_STATE_CAPABILITIES_EXCHANGE_CONFIRM_ACTIVE) |
1874 | 0 | { |
1875 | 0 | if (freerdp_settings_get_bool(rdp->settings, FreeRDP_TransportDumpReplay)) |
1876 | 0 | rdp_finalize_set_flag(rdp, FINALIZE_DEACTIVATE_REACTIVATE); |
1877 | 0 | else |
1878 | 0 | { |
1879 | 0 | WLog_WARN(TAG, "Invalid state, got %s, expected %s", rdp_get_state_string(rdp), |
1880 | 0 | rdp_state_string(CONNECTION_STATE_CAPABILITIES_EXCHANGE_CONFIRM_ACTIVE)); |
1881 | 0 | return FALSE; |
1882 | 0 | } |
1883 | 0 | } |
1884 | | |
1885 | 0 | if (!rdp_recv_confirm_active(rdp, s, pduLength)) |
1886 | 0 | return FALSE; |
1887 | | |
1888 | 0 | if (peer->ClientCapabilities && !peer->ClientCapabilities(peer)) |
1889 | 0 | { |
1890 | 0 | WLog_WARN(TAG, "peer->ClientCapabilities failed"); |
1891 | 0 | return FALSE; |
1892 | 0 | } |
1893 | | |
1894 | 0 | if (rdp->settings->SaltedChecksum) |
1895 | 0 | rdp->do_secure_checksum = TRUE; |
1896 | |
|
1897 | 0 | return rdp_server_send_sync(rdp); |
1898 | 0 | } |
1899 | | |
1900 | | BOOL rdp_server_reactivate(rdpRdp* rdp) |
1901 | 0 | { |
1902 | 0 | freerdp_peer* client = NULL; |
1903 | |
|
1904 | 0 | if (rdp->context && rdp->context->peer) |
1905 | 0 | client = rdp->context->peer; |
1906 | |
|
1907 | 0 | if (client) |
1908 | 0 | client->activated = FALSE; |
1909 | |
|
1910 | 0 | if (!rdp_send_deactivate_all(rdp)) |
1911 | 0 | return FALSE; |
1912 | | |
1913 | 0 | rdp_finalize_set_flag(rdp, FINALIZE_DEACTIVATE_REACTIVATE); |
1914 | 0 | if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_CAPABILITIES_EXCHANGE_DEMAND_ACTIVE)) |
1915 | 0 | return FALSE; |
1916 | | |
1917 | 0 | state_run_t rc = rdp_peer_handle_state_demand_active(client); |
1918 | 0 | return state_run_success(rc); |
1919 | 0 | } |
1920 | | |
1921 | | static BOOL rdp_is_active_peer_state(CONNECTION_STATE state) |
1922 | 0 | { |
1923 | | /* [MS-RDPBCGR] 1.3.1.1 Connection Sequence states: |
1924 | | * 'upon receipt of the Font List PDU the server can start sending graphics |
1925 | | * output to the client' |
1926 | | */ |
1927 | 0 | switch (state) |
1928 | 0 | { |
1929 | 0 | case CONNECTION_STATE_FINALIZATION_CLIENT_SYNC: |
1930 | 0 | case CONNECTION_STATE_FINALIZATION_CLIENT_COOPERATE: |
1931 | 0 | case CONNECTION_STATE_FINALIZATION_CLIENT_GRANTED_CONTROL: |
1932 | 0 | case CONNECTION_STATE_FINALIZATION_CLIENT_FONT_MAP: |
1933 | 0 | case CONNECTION_STATE_ACTIVE: |
1934 | 0 | return TRUE; |
1935 | 0 | default: |
1936 | 0 | return FALSE; |
1937 | 0 | } |
1938 | 0 | } |
1939 | | |
1940 | | static BOOL rdp_is_active_client_state(CONNECTION_STATE state) |
1941 | 0 | { |
1942 | | /* [MS-RDPBCGR] 1.3.1.1 Connection Sequence states: |
1943 | | * 'Once the client has sent the Confirm Active PDU, it can start sending |
1944 | | * mouse and keyboard input to the server' |
1945 | | */ |
1946 | 0 | switch (state) |
1947 | 0 | { |
1948 | 0 | case CONNECTION_STATE_FINALIZATION_SYNC: |
1949 | 0 | case CONNECTION_STATE_FINALIZATION_COOPERATE: |
1950 | 0 | case CONNECTION_STATE_FINALIZATION_REQUEST_CONTROL: |
1951 | 0 | case CONNECTION_STATE_FINALIZATION_PERSISTENT_KEY_LIST: |
1952 | 0 | case CONNECTION_STATE_FINALIZATION_FONT_LIST: |
1953 | 0 | case CONNECTION_STATE_FINALIZATION_CLIENT_SYNC: |
1954 | 0 | case CONNECTION_STATE_FINALIZATION_CLIENT_COOPERATE: |
1955 | 0 | case CONNECTION_STATE_FINALIZATION_CLIENT_GRANTED_CONTROL: |
1956 | 0 | case CONNECTION_STATE_FINALIZATION_CLIENT_FONT_MAP: |
1957 | 0 | case CONNECTION_STATE_ACTIVE: |
1958 | 0 | return TRUE; |
1959 | 0 | default: |
1960 | 0 | return FALSE; |
1961 | 0 | } |
1962 | 0 | } |
1963 | | |
1964 | | BOOL rdp_is_active_state(const rdpRdp* rdp) |
1965 | 0 | { |
1966 | 0 | WINPR_ASSERT(rdp); |
1967 | 0 | WINPR_ASSERT(rdp->context); |
1968 | | |
1969 | 0 | const CONNECTION_STATE state = rdp_get_state(rdp); |
1970 | 0 | if (freerdp_settings_get_bool(rdp->context->settings, FreeRDP_ServerMode)) |
1971 | 0 | return rdp_is_active_peer_state(state); |
1972 | 0 | else |
1973 | 0 | return rdp_is_active_client_state(state); |
1974 | 0 | } |
1975 | | |
1976 | | BOOL rdp_server_transition_to_state(rdpRdp* rdp, CONNECTION_STATE state) |
1977 | 0 | { |
1978 | 0 | BOOL status = FALSE; |
1979 | 0 | freerdp_peer* client = NULL; |
1980 | 0 | const CONNECTION_STATE cstate = rdp_get_state(rdp); |
1981 | |
|
1982 | 0 | if (cstate >= CONNECTION_STATE_RDP_SECURITY_COMMENCEMENT) |
1983 | 0 | { |
1984 | 0 | WINPR_ASSERT(rdp->context); |
1985 | 0 | client = rdp->context->peer; |
1986 | 0 | } |
1987 | | |
1988 | 0 | if (!rdp_is_active_peer_state(cstate)) |
1989 | 0 | { |
1990 | 0 | if (client) |
1991 | 0 | client->activated = FALSE; |
1992 | 0 | } |
1993 | |
|
1994 | 0 | WLog_Print(rdp->log, WLOG_DEBUG, "%s --> %s", rdp_get_state_string(rdp), |
1995 | 0 | rdp_state_string(state)); |
1996 | 0 | if (!rdp_set_state(rdp, state)) |
1997 | 0 | goto fail; |
1998 | | |
1999 | 0 | status = TRUE; |
2000 | 0 | fail: |
2001 | 0 | return status; |
2002 | 0 | } |
2003 | | |
2004 | | const char* rdp_client_connection_state_string(UINT state) |
2005 | 0 | { |
2006 | 0 | switch (state) |
2007 | 0 | { |
2008 | 0 | case CLIENT_STATE_INITIAL: |
2009 | 0 | return "CLIENT_STATE_INITIAL"; |
2010 | 0 | case CLIENT_STATE_PRECONNECT_PASSED: |
2011 | 0 | return "CLIENT_STATE_PRECONNECT_PASSED"; |
2012 | 0 | case CLIENT_STATE_POSTCONNECT_PASSED: |
2013 | 0 | return "CLIENT_STATE_POSTCONNECT_PASSED"; |
2014 | 0 | default: |
2015 | 0 | return "UNKNOWN"; |
2016 | 0 | } |
2017 | 0 | } |
2018 | | |
2019 | | const char* rdp_state_string(CONNECTION_STATE state) |
2020 | 0 | { |
2021 | 0 | switch (state) |
2022 | 0 | { |
2023 | 0 | case CONNECTION_STATE_INITIAL: |
2024 | 0 | return "CONNECTION_STATE_INITIAL"; |
2025 | 0 | case CONNECTION_STATE_NEGO: |
2026 | 0 | return "CONNECTION_STATE_NEGO"; |
2027 | 0 | case CONNECTION_STATE_NLA: |
2028 | 0 | return "CONNECTION_STATE_NLA"; |
2029 | 0 | case CONNECTION_STATE_AAD: |
2030 | 0 | return "CONNECTION_STATE_AAD"; |
2031 | 0 | case CONNECTION_STATE_MCS_CREATE_REQUEST: |
2032 | 0 | return "CONNECTION_STATE_MCS_CREATE_REQUEST"; |
2033 | 0 | case CONNECTION_STATE_MCS_CREATE_RESPONSE: |
2034 | 0 | return "CONNECTION_STATE_MCS_CREATE_RESPONSE"; |
2035 | 0 | case CONNECTION_STATE_MCS_ERECT_DOMAIN: |
2036 | 0 | return "CONNECTION_STATE_MCS_ERECT_DOMAIN"; |
2037 | 0 | case CONNECTION_STATE_MCS_ATTACH_USER: |
2038 | 0 | return "CONNECTION_STATE_MCS_ATTACH_USER"; |
2039 | 0 | case CONNECTION_STATE_MCS_ATTACH_USER_CONFIRM: |
2040 | 0 | return "CONNECTION_STATE_MCS_ATTACH_USER_CONFIRM"; |
2041 | 0 | case CONNECTION_STATE_MCS_CHANNEL_JOIN_REQUEST: |
2042 | 0 | return "CONNECTION_STATE_MCS_CHANNEL_JOIN_REQUEST"; |
2043 | 0 | case CONNECTION_STATE_MCS_CHANNEL_JOIN_RESPONSE: |
2044 | 0 | return "CONNECTION_STATE_MCS_CHANNEL_JOIN_RESPONSE"; |
2045 | 0 | case CONNECTION_STATE_RDP_SECURITY_COMMENCEMENT: |
2046 | 0 | return "CONNECTION_STATE_RDP_SECURITY_COMMENCEMENT"; |
2047 | 0 | case CONNECTION_STATE_SECURE_SETTINGS_EXCHANGE: |
2048 | 0 | return "CONNECTION_STATE_SECURE_SETTINGS_EXCHANGE"; |
2049 | 0 | case CONNECTION_STATE_CONNECT_TIME_AUTO_DETECT_REQUEST: |
2050 | 0 | return "CONNECTION_STATE_CONNECT_TIME_AUTO_DETECT_REQUEST"; |
2051 | 0 | case CONNECTION_STATE_CONNECT_TIME_AUTO_DETECT_RESPONSE: |
2052 | 0 | return "CONNECTION_STATE_CONNECT_TIME_AUTO_DETECT_RESPONSE"; |
2053 | 0 | case CONNECTION_STATE_LICENSING: |
2054 | 0 | return "CONNECTION_STATE_LICENSING"; |
2055 | 0 | case CONNECTION_STATE_MULTITRANSPORT_BOOTSTRAPPING_REQUEST: |
2056 | 0 | return "CONNECTION_STATE_MULTITRANSPORT_BOOTSTRAPPING_REQUEST"; |
2057 | 0 | case CONNECTION_STATE_MULTITRANSPORT_BOOTSTRAPPING_RESPONSE: |
2058 | 0 | return "CONNECTION_STATE_MULTITRANSPORT_BOOTSTRAPPING_RESPONSE"; |
2059 | 0 | case CONNECTION_STATE_CAPABILITIES_EXCHANGE_DEMAND_ACTIVE: |
2060 | 0 | return "CONNECTION_STATE_CAPABILITIES_EXCHANGE_DEMAND_ACTIVE"; |
2061 | 0 | case CONNECTION_STATE_CAPABILITIES_EXCHANGE_MONITOR_LAYOUT: |
2062 | 0 | return "CONNECTION_STATE_CAPABILITIES_EXCHANGE_MONITOR_LAYOUT"; |
2063 | 0 | case CONNECTION_STATE_CAPABILITIES_EXCHANGE_CONFIRM_ACTIVE: |
2064 | 0 | return "CONNECTION_STATE_CAPABILITIES_EXCHANGE_CONFIRM_ACTIVE"; |
2065 | 0 | case CONNECTION_STATE_FINALIZATION_SYNC: |
2066 | 0 | return "CONNECTION_STATE_FINALIZATION_SYNC"; |
2067 | 0 | case CONNECTION_STATE_FINALIZATION_COOPERATE: |
2068 | 0 | return "CONNECTION_STATE_FINALIZATION_COOPERATE"; |
2069 | 0 | case CONNECTION_STATE_FINALIZATION_REQUEST_CONTROL: |
2070 | 0 | return "CONNECTION_STATE_FINALIZATION_REQUEST_CONTROL"; |
2071 | 0 | case CONNECTION_STATE_FINALIZATION_PERSISTENT_KEY_LIST: |
2072 | 0 | return "CONNECTION_STATE_FINALIZATION_PERSISTENT_KEY_LIST"; |
2073 | 0 | case CONNECTION_STATE_FINALIZATION_FONT_LIST: |
2074 | 0 | return "CONNECTION_STATE_FINALIZATION_FONT_LIST"; |
2075 | 0 | case CONNECTION_STATE_FINALIZATION_CLIENT_SYNC: |
2076 | 0 | return "CONNECTION_STATE_FINALIZATION_CLIENT_SYNC"; |
2077 | 0 | case CONNECTION_STATE_FINALIZATION_CLIENT_COOPERATE: |
2078 | 0 | return "CONNECTION_STATE_FINALIZATION_CLIENT_COOPERATE"; |
2079 | 0 | case CONNECTION_STATE_FINALIZATION_CLIENT_GRANTED_CONTROL: |
2080 | 0 | return "CONNECTION_STATE_FINALIZATION_CLIENT_GRANTED_CONTROL"; |
2081 | 0 | case CONNECTION_STATE_FINALIZATION_CLIENT_FONT_MAP: |
2082 | 0 | return "CONNECTION_STATE_FINALIZATION_CLIENT_FONT_MAP"; |
2083 | 0 | case CONNECTION_STATE_ACTIVE: |
2084 | 0 | return "CONNECTION_STATE_ACTIVE"; |
2085 | 0 | default: |
2086 | 0 | return "UNKNOWN"; |
2087 | 0 | } |
2088 | 0 | } |
2089 | | |
2090 | | CONNECTION_STATE rdp_get_state(const rdpRdp* rdp) |
2091 | 0 | { |
2092 | 0 | WINPR_ASSERT(rdp); |
2093 | 0 | return rdp->state; |
2094 | 0 | } |
2095 | | |
2096 | | BOOL rdp_set_state(rdpRdp* rdp, CONNECTION_STATE state) |
2097 | 0 | { |
2098 | 0 | WINPR_ASSERT(rdp); |
2099 | 0 | rdp->state = state; |
2100 | 0 | return TRUE; |
2101 | 0 | } |
2102 | | |
2103 | | const char* rdp_get_state_string(const rdpRdp* rdp) |
2104 | 0 | { |
2105 | 0 | CONNECTION_STATE state = rdp_get_state(rdp); |
2106 | 0 | return rdp_state_string(state); |
2107 | 0 | } |
2108 | | |
2109 | | BOOL rdp_channels_from_mcs(rdpSettings* settings, const rdpRdp* rdp) |
2110 | 0 | { |
2111 | 0 | const rdpMcs* mcs = NULL; |
2112 | |
|
2113 | 0 | WINPR_ASSERT(rdp); |
2114 | | |
2115 | 0 | mcs = rdp->mcs; |
2116 | 0 | WINPR_ASSERT(mcs); |
2117 | | |
2118 | 0 | if (!freerdp_settings_set_pointer_len(settings, FreeRDP_ChannelDefArray, NULL, |
2119 | 0 | CHANNEL_MAX_COUNT)) |
2120 | 0 | return FALSE; |
2121 | | |
2122 | 0 | for (UINT32 x = 0; x < mcs->channelCount; x++) |
2123 | 0 | { |
2124 | 0 | const rdpMcsChannel* mchannel = &mcs->channels[x]; |
2125 | 0 | CHANNEL_DEF cur = { 0 }; |
2126 | |
|
2127 | 0 | memcpy(cur.name, mchannel->Name, sizeof(cur.name)); |
2128 | 0 | cur.options = mchannel->options; |
2129 | 0 | if (!freerdp_settings_set_pointer_array(settings, FreeRDP_ChannelDefArray, x, &cur)) |
2130 | 0 | return FALSE; |
2131 | 0 | } |
2132 | | |
2133 | 0 | return freerdp_settings_set_uint32(settings, FreeRDP_ChannelCount, mcs->channelCount); |
2134 | 0 | } |
2135 | | |
2136 | | /* Here we are in client state CONFIRM_ACTIVE. |
2137 | | * |
2138 | | * This means: |
2139 | | * 1. send the CONFIRM_ACTIVE PDU to the server |
2140 | | * 2. register callbacks, the server can now start sending stuff |
2141 | | */ |
2142 | | state_run_t rdp_client_connect_confirm_active(rdpRdp* rdp, WINPR_ATTR_UNUSED wStream* s) |
2143 | 0 | { |
2144 | 0 | WINPR_ASSERT(rdp); |
2145 | 0 | WINPR_ASSERT(rdp->settings); |
2146 | 0 | WINPR_ASSERT(s); |
2147 | | |
2148 | 0 | const UINT32 width = rdp->settings->DesktopWidth; |
2149 | 0 | const UINT32 height = rdp->settings->DesktopHeight; |
2150 | |
|
2151 | 0 | if (!rdp_send_confirm_active(rdp)) |
2152 | 0 | return STATE_RUN_FAILED; |
2153 | | |
2154 | 0 | if (!input_register_client_callbacks(rdp->input)) |
2155 | 0 | { |
2156 | 0 | WLog_ERR(TAG, "error registering client callbacks"); |
2157 | 0 | return STATE_RUN_FAILED; |
2158 | 0 | } |
2159 | | |
2160 | | /** |
2161 | | * The server may request a different desktop size during Deactivation-Reactivation sequence. |
2162 | | * In this case, the UI should be informed and do actual window resizing at this point. |
2163 | | */ |
2164 | 0 | const BOOL deactivate_reactivate = |
2165 | 0 | rdp->was_deactivated && ((rdp->deactivated_width != rdp->settings->DesktopWidth) || |
2166 | 0 | (rdp->deactivated_height != rdp->settings->DesktopHeight)); |
2167 | 0 | const BOOL resolution_change = |
2168 | 0 | ((width != rdp->settings->DesktopWidth) || (height != rdp->settings->DesktopHeight)); |
2169 | 0 | if (deactivate_reactivate || resolution_change) |
2170 | 0 | { |
2171 | 0 | BOOL status = TRUE; |
2172 | 0 | IFCALLRET(rdp->update->DesktopResize, status, rdp->update->context); |
2173 | |
|
2174 | 0 | if (!status) |
2175 | 0 | { |
2176 | 0 | WLog_ERR(TAG, "client desktop resize callback failed"); |
2177 | 0 | return STATE_RUN_FAILED; |
2178 | 0 | } |
2179 | 0 | } |
2180 | | |
2181 | 0 | WINPR_ASSERT(rdp->context); |
2182 | 0 | if (freerdp_shall_disconnect_context(rdp->context)) |
2183 | 0 | return STATE_RUN_SUCCESS; |
2184 | | |
2185 | 0 | state_run_t status = STATE_RUN_SUCCESS; |
2186 | 0 | if (!rdp->settings->SupportMonitorLayoutPdu) |
2187 | 0 | status = rdp_client_connect_finalize(rdp); |
2188 | 0 | else |
2189 | 0 | { |
2190 | 0 | if (!rdp_client_transition_to_state(rdp, |
2191 | 0 | CONNECTION_STATE_CAPABILITIES_EXCHANGE_MONITOR_LAYOUT)) |
2192 | 0 | status = STATE_RUN_FAILED; |
2193 | 0 | } |
2194 | 0 | if (!rdp_finalize_reset_flags(rdp, FALSE)) |
2195 | 0 | status = STATE_RUN_FAILED; |
2196 | 0 | return status; |
2197 | 0 | } |
2198 | | |
2199 | | BOOL rdp_handle_optional_rdp_decryption(rdpRdp* rdp, wStream* s, UINT16* length, |
2200 | | UINT16* pSecurityFlags) |
2201 | 0 | { |
2202 | 0 | BOOL rc = FALSE; |
2203 | 0 | WINPR_ASSERT(rdp); |
2204 | 0 | WINPR_ASSERT(rdp->settings); |
2205 | | |
2206 | 0 | UINT16 securityFlags = 0; |
2207 | 0 | if (rdp->settings->UseRdpSecurityLayer) |
2208 | 0 | { |
2209 | 0 | if (!rdp_read_security_header(rdp, s, &securityFlags, length)) |
2210 | 0 | goto fail; |
2211 | | |
2212 | 0 | if (securityFlags & SEC_ENCRYPT) |
2213 | 0 | { |
2214 | 0 | if (!rdp_decrypt(rdp, s, length, securityFlags)) |
2215 | 0 | goto fail; |
2216 | 0 | } |
2217 | 0 | } |
2218 | | |
2219 | 0 | rc = TRUE; |
2220 | |
|
2221 | 0 | fail: |
2222 | 0 | if (pSecurityFlags) |
2223 | 0 | *pSecurityFlags = securityFlags; |
2224 | 0 | return rc; |
2225 | 0 | } |