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