/src/FreeRDP/libfreerdp/core/fastpath.c
Line | Count | Source |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * Fast Path |
4 | | * |
5 | | * Copyright 2011 Vic Lee |
6 | | * Copyright 2014 Norbert Federa <norbert.federa@thincast.com> |
7 | | * Copyright 2017 Armin Novak <armin.novak@thincast.com> |
8 | | * Copyright 2017 Thincast Technologies GmbH |
9 | | * |
10 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
11 | | * you may not use this file except in compliance with the License. |
12 | | * You may obtain a copy of the License at |
13 | | * |
14 | | * http://www.apache.org/licenses/LICENSE-2.0 |
15 | | * |
16 | | * Unless required by applicable law or agreed to in writing, software |
17 | | * distributed under the License is distributed on an "AS IS" BASIS, |
18 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
19 | | * See the License for the specific language governing permissions and |
20 | | * limitations under the License. |
21 | | */ |
22 | | |
23 | | #include <freerdp/config.h> |
24 | | |
25 | | #include "settings.h" |
26 | | |
27 | | #include <stdio.h> |
28 | | #include <stdlib.h> |
29 | | #include <string.h> |
30 | | |
31 | | #include <winpr/crt.h> |
32 | | #include <winpr/assert.h> |
33 | | #include <winpr/stream.h> |
34 | | |
35 | | #include <freerdp/api.h> |
36 | | #include <freerdp/log.h> |
37 | | #include <freerdp/crypto/per.h> |
38 | | |
39 | | #include "orders.h" |
40 | | #include "update.h" |
41 | | #include "surface.h" |
42 | | #include "fastpath.h" |
43 | | #include "rdp.h" |
44 | | |
45 | | #include "../cache/pointer.h" |
46 | | #include "../cache/palette.h" |
47 | | #include "../cache/bitmap.h" |
48 | | |
49 | 24.7k | #define FTAG FREERDP_TAG("core.fastpath") |
50 | | |
51 | | enum FASTPATH_INPUT_ENCRYPTION_FLAGS |
52 | | { |
53 | | FASTPATH_INPUT_SECURE_CHECKSUM = 0x1, |
54 | | FASTPATH_INPUT_ENCRYPTED = 0x2 |
55 | | }; |
56 | | |
57 | | enum FASTPATH_OUTPUT_ENCRYPTION_FLAGS |
58 | | { |
59 | | FASTPATH_OUTPUT_SECURE_CHECKSUM = 0x1, |
60 | | FASTPATH_OUTPUT_ENCRYPTED = 0x2 |
61 | | }; |
62 | | |
63 | | struct rdp_fastpath |
64 | | { |
65 | | rdpRdp* rdp; |
66 | | wStream* fs; |
67 | | BYTE encryptionFlags; |
68 | | BYTE numberEvents; |
69 | | wStream* updateData; |
70 | | int fragmentation; |
71 | | wLog* log; |
72 | | }; |
73 | | |
74 | | /** |
75 | | * Fast-Path packet format is defined in [MS-RDPBCGR] 2.2.9.1.2, which revises |
76 | | * server output packets from the first byte with the goal of improving |
77 | | * bandwidth. |
78 | | * |
79 | | * Slow-Path packet always starts with TPKT header, which has the first |
80 | | * byte 0x03, while Fast-Path packet starts with 2 zero bits in the first |
81 | | * two less significant bits of the first byte. |
82 | | */ |
83 | | |
84 | | static const char* const FASTPATH_UPDATETYPE_STRINGS[] = { |
85 | | "Orders", /* 0x0 */ |
86 | | "Bitmap", /* 0x1 */ |
87 | | "Palette", /* 0x2 */ |
88 | | "Synchronize", /* 0x3 */ |
89 | | "Surface Commands", /* 0x4 */ |
90 | | "System Pointer Hidden", /* 0x5 */ |
91 | | "System Pointer Default", /* 0x6 */ |
92 | | "???", /* 0x7 */ |
93 | | "Pointer Position", /* 0x8 */ |
94 | | "Color Pointer", /* 0x9 */ |
95 | | "Cached Pointer", /* 0xA */ |
96 | | "New Pointer", /* 0xB */ |
97 | | }; |
98 | | |
99 | | WINPR_ATTR_NODISCARD |
100 | | static const char* fastpath_update_to_string(UINT8 update) |
101 | 5.88k | { |
102 | 5.88k | if (update >= ARRAYSIZE(FASTPATH_UPDATETYPE_STRINGS)) |
103 | 606 | return "UNKNOWN"; |
104 | | |
105 | 5.28k | return FASTPATH_UPDATETYPE_STRINGS[update]; |
106 | 5.88k | } |
107 | | |
108 | | WINPR_ATTR_NODISCARD |
109 | | static BOOL fastpath_read_update_header(wLog* log, wStream* s, BYTE* updateCode, |
110 | | BYTE* fragmentation, BYTE* compression) |
111 | 50.1k | { |
112 | 50.1k | BYTE updateHeader = 0; |
113 | | |
114 | 50.1k | if (!s || !updateCode || !fragmentation || !compression) |
115 | 0 | return FALSE; |
116 | | |
117 | 50.1k | if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 1)) |
118 | 0 | return FALSE; |
119 | | |
120 | 50.1k | Stream_Read_UINT8(s, updateHeader); |
121 | 50.1k | *updateCode = updateHeader & 0x0F; |
122 | 50.1k | *fragmentation = (updateHeader >> 4) & 0x03; |
123 | 50.1k | *compression = (updateHeader >> 6) & 0x03; |
124 | 50.1k | return TRUE; |
125 | 50.1k | } |
126 | | |
127 | | WINPR_ATTR_NODISCARD |
128 | | static BOOL fastpath_write_update_header(wLog* log, wStream* s, |
129 | | const FASTPATH_UPDATE_HEADER* fpUpdateHeader) |
130 | 45 | { |
131 | 45 | BYTE updateHeader = 0; |
132 | 45 | WINPR_ASSERT(fpUpdateHeader); |
133 | | |
134 | 45 | updateHeader |= fpUpdateHeader->updateCode & 0x0F; |
135 | 45 | updateHeader |= (fpUpdateHeader->fragmentation & 0x03) << 4; |
136 | 45 | updateHeader |= (fpUpdateHeader->compression & 0x03) << 6; |
137 | | |
138 | 45 | if (!Stream_CheckAndLogRequiredCapacityWLog(log, s, 1)) |
139 | 0 | return FALSE; |
140 | 45 | Stream_Write_UINT8(s, updateHeader); |
141 | | |
142 | 45 | if (fpUpdateHeader->compression) |
143 | 0 | { |
144 | 0 | if (!Stream_CheckAndLogRequiredCapacityWLog(log, s, 1)) |
145 | 0 | return FALSE; |
146 | | |
147 | 0 | Stream_Write_UINT8(s, fpUpdateHeader->compressionFlags); |
148 | 0 | } |
149 | | |
150 | 45 | if (!Stream_CheckAndLogRequiredCapacityWLog(log, s, 2)) |
151 | 0 | return FALSE; |
152 | | |
153 | 45 | Stream_Write_UINT16(s, fpUpdateHeader->size); |
154 | 45 | return TRUE; |
155 | 45 | } |
156 | | |
157 | | WINPR_ATTR_NODISCARD |
158 | | static UINT32 fastpath_get_update_header_size(FASTPATH_UPDATE_HEADER* fpUpdateHeader) |
159 | 45 | { |
160 | 45 | WINPR_ASSERT(fpUpdateHeader); |
161 | 45 | return (fpUpdateHeader->compression) ? 4 : 3; |
162 | 45 | } |
163 | | |
164 | | WINPR_ATTR_NODISCARD |
165 | | static BOOL fastpath_write_update_pdu_header(wLog* log, wStream* s, |
166 | | const FASTPATH_UPDATE_PDU_HEADER* fpUpdatePduHeader, |
167 | | rdpRdp* rdp) |
168 | 45 | { |
169 | 45 | BYTE fpOutputHeader = 0; |
170 | 45 | WINPR_ASSERT(fpUpdatePduHeader); |
171 | 45 | WINPR_ASSERT(rdp); |
172 | | |
173 | 45 | if (!Stream_CheckAndLogRequiredCapacityWLog(log, s, 3)) |
174 | 0 | return FALSE; |
175 | | |
176 | 45 | fpOutputHeader |= (fpUpdatePduHeader->action & 0x03); |
177 | 45 | fpOutputHeader |= (fpUpdatePduHeader->secFlags & 0x03) << 6; |
178 | 45 | Stream_Write_UINT8(s, fpOutputHeader); /* fpOutputHeader (1 byte) */ |
179 | 45 | Stream_Write_UINT8(s, 0x80 | (fpUpdatePduHeader->length >> 8)); /* length1 */ |
180 | 45 | Stream_Write_UINT8(s, fpUpdatePduHeader->length & 0xFF); /* length2 */ |
181 | | |
182 | 45 | if (fpUpdatePduHeader->secFlags) |
183 | 0 | { |
184 | 0 | WINPR_ASSERT(rdp->settings); |
185 | 0 | if (freerdp_settings_get_uint32(rdp->settings, FreeRDP_EncryptionMethods) == |
186 | 0 | ENCRYPTION_METHOD_FIPS) |
187 | 0 | { |
188 | 0 | if (!Stream_CheckAndLogRequiredCapacityWLog(log, s, 4)) |
189 | 0 | return FALSE; |
190 | | |
191 | 0 | Stream_Write(s, fpUpdatePduHeader->fipsInformation, 4); |
192 | 0 | } |
193 | | |
194 | 0 | if (!Stream_CheckAndLogRequiredCapacityWLog(log, s, 8)) |
195 | 0 | return FALSE; |
196 | | |
197 | 0 | Stream_Write(s, fpUpdatePduHeader->dataSignature, 8); |
198 | 0 | } |
199 | | |
200 | 45 | return TRUE; |
201 | 45 | } |
202 | | |
203 | | WINPR_ATTR_NODISCARD |
204 | | static UINT32 fastpath_get_update_pdu_header_size(FASTPATH_UPDATE_PDU_HEADER* fpUpdatePduHeader, |
205 | | rdpRdp* rdp) |
206 | 45 | { |
207 | 45 | UINT32 size = 3; /* fpUpdatePduHeader + length1 + length2 */ |
208 | | |
209 | 45 | if (!fpUpdatePduHeader || !rdp) |
210 | 0 | return 0; |
211 | | |
212 | 45 | if (fpUpdatePduHeader->secFlags) |
213 | 0 | { |
214 | 0 | size += 8; /* dataSignature */ |
215 | |
|
216 | 0 | WINPR_ASSERT(rdp->settings); |
217 | 0 | if (freerdp_settings_get_uint32(rdp->settings, FreeRDP_EncryptionMethods) == |
218 | 0 | ENCRYPTION_METHOD_FIPS) |
219 | 0 | size += 4; /* fipsInformation */ |
220 | 0 | } |
221 | | |
222 | 45 | return size; |
223 | 45 | } |
224 | | |
225 | | BOOL fastpath_read_header_rdp(rdpFastPath* fastpath, wStream* s, UINT16* length) |
226 | 15.8k | { |
227 | 15.8k | WINPR_ASSERT(fastpath); |
228 | 15.8k | WINPR_ASSERT(s); |
229 | 15.8k | WINPR_ASSERT(length); |
230 | | |
231 | 15.8k | if (!Stream_CheckAndLogRequiredLengthWLog(fastpath->log, s, 1)) |
232 | 3.90k | return FALSE; |
233 | | |
234 | 11.9k | const BYTE header = Stream_Get_UINT8(s); |
235 | 11.9k | fastpath->encryptionFlags = (header & 0xC0) >> 6; |
236 | 11.9k | fastpath->numberEvents = (header & 0x3C) >> 2; |
237 | | |
238 | 11.9k | if (!per_read_length(s, length)) |
239 | 308 | return FALSE; |
240 | | |
241 | 11.6k | const size_t pos = Stream_GetPosition(s); |
242 | 11.6k | if (pos > *length) |
243 | 7.09k | return FALSE; |
244 | | |
245 | 4.52k | *length = *length - (UINT16)pos; |
246 | 4.52k | return TRUE; |
247 | 11.6k | } |
248 | | |
249 | | WINPR_ATTR_NODISCARD |
250 | | static BOOL fastpath_recv_orders(rdpUpdate* update, wStream* s) |
251 | 5.61k | { |
252 | 5.61k | UINT16 numberOrders = 0; |
253 | | |
254 | 5.61k | WINPR_ASSERT(update); |
255 | 5.61k | WINPR_ASSERT(update->context); |
256 | 5.61k | WINPR_ASSERT(update->context->rdp); |
257 | 5.61k | WINPR_ASSERT(update->context->rdp->fastpath); |
258 | | |
259 | 5.61k | wLog* log = update->context->rdp->fastpath->log; |
260 | 5.61k | if (!s) |
261 | 0 | { |
262 | 0 | WLog_Print(log, WLOG_ERROR, "Invalid arguments"); |
263 | 0 | return FALSE; |
264 | 0 | } |
265 | | |
266 | 5.61k | if (!update) |
267 | 0 | { |
268 | 0 | WLog_Print(log, WLOG_ERROR, "Invalid configuration"); |
269 | 0 | return FALSE; |
270 | 0 | } |
271 | | |
272 | 5.61k | if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 2)) |
273 | 1.13k | return FALSE; |
274 | | |
275 | 4.47k | Stream_Read_UINT16(s, numberOrders); /* numberOrders (2 bytes) */ |
276 | | |
277 | 4.47k | while (numberOrders > 0) |
278 | 298 | { |
279 | 298 | if (!update_recv_order(update, s)) |
280 | 298 | return FALSE; |
281 | | |
282 | 0 | numberOrders--; |
283 | 0 | } |
284 | | |
285 | 4.18k | return TRUE; |
286 | 4.47k | } |
287 | | |
288 | | WINPR_ATTR_NODISCARD |
289 | | static BOOL fastpath_recv_update_common(rdpUpdate* update, wStream* s) |
290 | 863 | { |
291 | 863 | BOOL rc = FALSE; |
292 | 863 | UINT16 updateType = 0; |
293 | 863 | BOOL defaultReturn = 0; |
294 | | |
295 | 863 | rdp_update_internal* up = update_cast(update); |
296 | 863 | if (!s) |
297 | 0 | return FALSE; |
298 | | |
299 | 863 | if (!update || !update->context) |
300 | 0 | return FALSE; |
301 | | |
302 | 863 | rdpContext* context = update->context; |
303 | 863 | WINPR_ASSERT(context->rdp); |
304 | 863 | WINPR_ASSERT(context->rdp->fastpath); |
305 | | |
306 | 863 | defaultReturn = freerdp_settings_get_bool(context->settings, FreeRDP_DeactivateClientDecoding); |
307 | | |
308 | 863 | if (!Stream_CheckAndLogRequiredLengthWLog(context->rdp->fastpath->log, s, 2)) |
309 | 185 | return FALSE; |
310 | | |
311 | 678 | Stream_Read_UINT16(s, updateType); /* updateType (2 bytes) */ |
312 | 678 | switch (updateType) |
313 | 678 | { |
314 | 385 | case UPDATE_TYPE_BITMAP: |
315 | 385 | { |
316 | 385 | BITMAP_UPDATE* bitmap_update = update_read_bitmap_update(update, s); |
317 | | |
318 | 385 | if (!bitmap_update) |
319 | 279 | return FALSE; |
320 | | |
321 | 106 | up->stats.base[RDP_STATS_BITMAP_UPDATE]++; |
322 | 106 | rc = IFCALLRESULT(defaultReturn, update->BitmapUpdate, context, bitmap_update); |
323 | 106 | free_bitmap_update(context, bitmap_update); |
324 | 106 | } |
325 | 0 | break; |
326 | | |
327 | 147 | case UPDATE_TYPE_PALETTE: |
328 | 147 | { |
329 | 147 | PALETTE_UPDATE* palette_update = update_read_palette(update, s); |
330 | | |
331 | 147 | if (!palette_update) |
332 | 78 | return FALSE; |
333 | | |
334 | 69 | up->stats.base[RDP_STATS_PALETTE]++; |
335 | 69 | rc = IFCALLRESULT(defaultReturn, update->Palette, context, palette_update); |
336 | 69 | free_palette_update(context, palette_update); |
337 | 69 | } |
338 | 0 | break; |
339 | | |
340 | 146 | default: |
341 | 146 | break; |
342 | 678 | } |
343 | | |
344 | 321 | return rc; |
345 | 678 | } |
346 | | |
347 | | WINPR_ATTR_NODISCARD |
348 | | static BOOL fastpath_recv_update_synchronize(WINPR_ATTR_UNUSED rdpFastPath* fastpath, wStream* s) |
349 | 4.47k | { |
350 | | /* server 2008 can send invalid synchronize packet with missing padding, |
351 | | so don't return FALSE even if the packet is invalid */ |
352 | 4.47k | WINPR_ASSERT(fastpath); |
353 | 4.47k | WINPR_ASSERT(s); |
354 | | |
355 | 4.47k | const size_t len = Stream_GetRemainingLength(s); |
356 | 4.47k | const size_t skip = MIN(2, len); |
357 | 4.47k | return Stream_SafeSeek(s, skip); /* size (2 bytes), MUST be set to zero */ |
358 | 4.47k | } |
359 | | |
360 | | WINPR_ATTR_NODISCARD |
361 | | static BOOL fastpath_recv_update_paint_block(rdpUpdate* update, wStream* s, |
362 | | BOOL (*fkt)(rdpUpdate*, wStream*)) |
363 | 30.4k | { |
364 | 30.4k | WINPR_ASSERT(fkt); |
365 | 30.4k | if (!update_begin_paint(update)) |
366 | 0 | return FALSE; |
367 | | |
368 | 30.4k | BOOL res = fkt(update, s); |
369 | 30.4k | if (!update_end_paint(update)) |
370 | 0 | return FALSE; |
371 | 30.4k | return res; |
372 | 30.4k | } |
373 | | |
374 | | WINPR_ATTR_NODISCARD |
375 | | static int fastpath_recv_update(rdpFastPath* fastpath, BYTE updateCode, wStream* s) |
376 | 36.6k | { |
377 | 36.6k | BOOL rc = FALSE; |
378 | 36.6k | int status = 0; |
379 | | |
380 | 36.6k | if (!fastpath || !fastpath->rdp || !s) |
381 | 0 | return -1; |
382 | | |
383 | 36.6k | Stream_SealLength(s); |
384 | 36.6k | Stream_ResetPosition(s); |
385 | | |
386 | 36.6k | rdpUpdate* update = fastpath->rdp->update; |
387 | | |
388 | 36.6k | if (!update || !update->pointer || !update->context) |
389 | 0 | return -1; |
390 | | |
391 | 36.6k | rdp_update_internal* up = update_cast(update); |
392 | | |
393 | 36.6k | rdpContext* context = update->context; |
394 | 36.6k | WINPR_ASSERT(context); |
395 | | |
396 | 36.6k | rdpPointerUpdate* pointer = update->pointer; |
397 | 36.6k | WINPR_ASSERT(pointer); |
398 | | |
399 | | #ifdef WITH_DEBUG_RDP |
400 | | DEBUG_RDP(fastpath->rdp, "recv Fast-Path %s Update (0x%02" PRIX8 "), length:%" PRIuz "", |
401 | | fastpath_update_to_string(updateCode), updateCode, Stream_GetRemainingLength(s)); |
402 | | #endif |
403 | | |
404 | 36.6k | const BOOL defaultReturn = |
405 | 36.6k | freerdp_settings_get_bool(context->settings, FreeRDP_DeactivateClientDecoding); |
406 | 36.6k | switch (updateCode) |
407 | 36.6k | { |
408 | 5.61k | case FASTPATH_UPDATETYPE_ORDERS: |
409 | 5.61k | rc = fastpath_recv_update_paint_block(update, s, fastpath_recv_orders); |
410 | 5.61k | break; |
411 | | |
412 | 568 | case FASTPATH_UPDATETYPE_BITMAP: |
413 | 863 | case FASTPATH_UPDATETYPE_PALETTE: |
414 | 863 | rc = fastpath_recv_update_paint_block(update, s, fastpath_recv_update_common); |
415 | 863 | break; |
416 | | |
417 | 4.47k | case FASTPATH_UPDATETYPE_SYNCHRONIZE: |
418 | 4.47k | if (!fastpath_recv_update_synchronize(fastpath, s)) |
419 | 0 | WLog_Print(fastpath->log, WLOG_ERROR, |
420 | 4.47k | "fastpath_recv_update_synchronize failure but we continue"); |
421 | 4.47k | else |
422 | 4.47k | { |
423 | 4.47k | up->stats.base[RDP_STATS_SYNC]++; |
424 | 4.47k | rc = IFCALLRESULT(TRUE, update->Synchronize, context); |
425 | 4.47k | } |
426 | | |
427 | 4.47k | break; |
428 | | |
429 | 23.9k | case FASTPATH_UPDATETYPE_SURFCMDS: |
430 | 23.9k | status = fastpath_recv_update_paint_block(update, s, update_recv_surfcmds); |
431 | 23.9k | rc = (status >= 0); |
432 | 23.9k | break; |
433 | | |
434 | 81 | case FASTPATH_UPDATETYPE_PTR_NULL: |
435 | 81 | { |
436 | 81 | POINTER_SYSTEM_UPDATE pointer_system = WINPR_C_ARRAY_INIT; |
437 | 81 | pointer_system.type = SYSPTR_NULL; |
438 | 81 | up->stats.base[RDP_STATS_POINTER_SYSTEM]++; |
439 | 81 | rc = IFCALLRESULT(defaultReturn, pointer->PointerSystem, context, &pointer_system); |
440 | 81 | } |
441 | 81 | break; |
442 | | |
443 | 111 | case FASTPATH_UPDATETYPE_PTR_DEFAULT: |
444 | 111 | { |
445 | 111 | POINTER_SYSTEM_UPDATE pointer_system = WINPR_C_ARRAY_INIT; |
446 | 111 | pointer_system.type = SYSPTR_DEFAULT; |
447 | 111 | up->stats.base[RDP_STATS_POINTER_DEFAULT]++; |
448 | 111 | rc = IFCALLRESULT(defaultReturn, pointer->PointerSystem, context, &pointer_system); |
449 | 111 | } |
450 | 111 | break; |
451 | | |
452 | 176 | case FASTPATH_UPDATETYPE_PTR_POSITION: |
453 | 176 | { |
454 | 176 | POINTER_POSITION_UPDATE* pointer_position = update_read_pointer_position(update, s); |
455 | | |
456 | 176 | if (pointer_position) |
457 | 115 | { |
458 | 115 | up->stats.base[RDP_STATS_POINTER_POSITION]++; |
459 | 115 | rc = IFCALLRESULT(defaultReturn, pointer->PointerPosition, context, |
460 | 115 | pointer_position); |
461 | 115 | free_pointer_position_update(context, pointer_position); |
462 | 115 | } |
463 | 176 | } |
464 | 176 | break; |
465 | | |
466 | 457 | case FASTPATH_UPDATETYPE_COLOR: |
467 | 457 | { |
468 | 457 | POINTER_COLOR_UPDATE* pointer_color = update_read_pointer_color(update, s, 24); |
469 | | |
470 | 457 | if (pointer_color) |
471 | 114 | { |
472 | 114 | up->stats.base[RDP_STATS_POINTER_COLOR]++; |
473 | 114 | rc = IFCALLRESULT(defaultReturn, pointer->PointerColor, context, pointer_color); |
474 | 114 | free_pointer_color_update(context, pointer_color); |
475 | 114 | } |
476 | 457 | } |
477 | 457 | break; |
478 | | |
479 | 124 | case FASTPATH_UPDATETYPE_CACHED: |
480 | 124 | { |
481 | 124 | POINTER_CACHED_UPDATE* pointer_cached = update_read_pointer_cached(update, s); |
482 | | |
483 | 124 | if (pointer_cached) |
484 | 88 | { |
485 | 88 | up->stats.base[RDP_STATS_POINTER_CACHED]++; |
486 | 88 | rc = IFCALLRESULT(defaultReturn, pointer->PointerCached, context, pointer_cached); |
487 | 88 | free_pointer_cached_update(context, pointer_cached); |
488 | 88 | } |
489 | 124 | } |
490 | 124 | break; |
491 | | |
492 | 159 | case FASTPATH_UPDATETYPE_POINTER: |
493 | 159 | { |
494 | 159 | POINTER_NEW_UPDATE* pointer_new = update_read_pointer_new(update, s); |
495 | | |
496 | 159 | if (pointer_new) |
497 | 16 | { |
498 | 16 | up->stats.base[RDP_STATS_POINTER_NEW]++; |
499 | 16 | rc = IFCALLRESULT(defaultReturn, pointer->PointerNew, context, pointer_new); |
500 | 16 | free_pointer_new_update(context, pointer_new); |
501 | 16 | } |
502 | 159 | } |
503 | 159 | break; |
504 | | |
505 | 569 | case FASTPATH_UPDATETYPE_LARGE_POINTER: |
506 | 569 | { |
507 | 569 | POINTER_LARGE_UPDATE* pointer_large = update_read_pointer_large(update, s); |
508 | | |
509 | 569 | if (pointer_large) |
510 | 125 | { |
511 | 125 | up->stats.base[RDP_STATS_POINTER_LARGE]++; |
512 | 125 | rc = IFCALLRESULT(defaultReturn, pointer->PointerLarge, context, pointer_large); |
513 | 125 | free_pointer_large_update(context, pointer_large); |
514 | 125 | } |
515 | 569 | } |
516 | 569 | break; |
517 | 56 | default: |
518 | 56 | break; |
519 | 36.6k | } |
520 | | |
521 | 36.6k | Stream_ResetPosition(s); |
522 | 36.6k | if (!rc) |
523 | 5.88k | { |
524 | 5.88k | WLog_Print(fastpath->log, WLOG_ERROR, "Fastpath update %s [%" PRIx8 "] failed, status %d", |
525 | 5.88k | fastpath_update_to_string(updateCode), updateCode, status); |
526 | 5.88k | const BOOL ignore = |
527 | 5.88k | freerdp_settings_get_bool(context->settings, FreeRDP_AllowUnanouncedOrdersFromServer); |
528 | 5.88k | if (ignore) |
529 | 0 | return 0; |
530 | 5.88k | return -1; |
531 | 5.88k | } |
532 | | |
533 | 30.7k | return status; |
534 | 36.6k | } |
535 | | |
536 | | WINPR_ATTR_NODISCARD |
537 | | static int fastpath_recv_update_data(rdpFastPath* fastpath, wStream* s) |
538 | 50.1k | { |
539 | 50.1k | int status = 0; |
540 | 50.1k | UINT16 size = 0; |
541 | 50.1k | BYTE updateCode = 0; |
542 | 50.1k | BYTE fragmentation = 0; |
543 | 50.1k | BYTE compression = 0; |
544 | 50.1k | BYTE compressionFlags = 0; |
545 | 50.1k | UINT32 DstSize = 0; |
546 | 50.1k | const BYTE* pDstData = nullptr; |
547 | | |
548 | 50.1k | if (!fastpath || !s) |
549 | 0 | return -1; |
550 | | |
551 | 50.1k | rdpRdp* rdp = fastpath->rdp; |
552 | | |
553 | 50.1k | if (!rdp) |
554 | 0 | return -1; |
555 | | |
556 | 50.1k | rdpTransport* transport = rdp->transport; |
557 | | |
558 | 50.1k | if (!transport) |
559 | 0 | return -1; |
560 | | |
561 | 50.1k | if (!fastpath_read_update_header(fastpath->log, s, &updateCode, &fragmentation, &compression)) |
562 | 0 | return -1; |
563 | | |
564 | 50.1k | if (compression == FASTPATH_OUTPUT_COMPRESSION_USED) |
565 | 15.4k | { |
566 | 15.4k | if (!Stream_CheckAndLogRequiredLengthWLog(fastpath->log, s, 1)) |
567 | 0 | return -1; |
568 | | |
569 | 15.4k | Stream_Read_UINT8(s, compressionFlags); |
570 | 15.4k | } |
571 | 34.6k | else |
572 | 34.6k | compressionFlags = 0; |
573 | | |
574 | 50.1k | if (!Stream_CheckAndLogRequiredLengthWLog(fastpath->log, s, 2)) |
575 | 48 | return -1; |
576 | | |
577 | 50.0k | Stream_Read_UINT16(s, size); |
578 | | |
579 | 50.0k | if (!Stream_CheckAndLogRequiredLengthWLog(fastpath->log, s, size)) |
580 | 6.21k | return -1; |
581 | | |
582 | 43.8k | const int bulkStatus = |
583 | 43.8k | bulk_decompress(rdp->bulk, Stream_Pointer(s), size, &pDstData, &DstSize, compressionFlags); |
584 | 43.8k | Stream_Seek(s, size); |
585 | | |
586 | 43.8k | if (bulkStatus < 0) |
587 | 1.65k | { |
588 | 1.65k | WLog_Print(fastpath->log, WLOG_ERROR, "bulk_decompress() failed"); |
589 | 1.65k | return -1; |
590 | 1.65k | } |
591 | | |
592 | 42.2k | if (!Stream_EnsureRemainingCapacity(fastpath->updateData, DstSize)) |
593 | 0 | return -1; |
594 | | |
595 | 42.2k | Stream_Write(fastpath->updateData, pDstData, DstSize); |
596 | | |
597 | 42.2k | if (fragmentation == FASTPATH_FRAGMENT_SINGLE) |
598 | 34.9k | { |
599 | 34.9k | if (fastpath->fragmentation != -1) |
600 | 70 | { |
601 | 70 | WLog_Print(fastpath->log, WLOG_ERROR, "Unexpected FASTPATH_FRAGMENT_SINGLE"); |
602 | 70 | goto out_fail; |
603 | 70 | } |
604 | | |
605 | 34.8k | status = fastpath_recv_update(fastpath, updateCode, fastpath->updateData); |
606 | | |
607 | 34.8k | if (status < 0) |
608 | 5.87k | { |
609 | 5.87k | WLog_Print(fastpath->log, WLOG_ERROR, "fastpath_recv_update() - %i", status); |
610 | 5.87k | goto out_fail; |
611 | 5.87k | } |
612 | 34.8k | } |
613 | 7.25k | else |
614 | 7.25k | { |
615 | 7.25k | rdpContext* context = nullptr; |
616 | 7.25k | const size_t totalSize = Stream_GetPosition(fastpath->updateData); |
617 | | |
618 | 7.25k | context = transport_get_context(transport); |
619 | 7.25k | WINPR_ASSERT(context); |
620 | 7.25k | WINPR_ASSERT(context->settings); |
621 | | |
622 | 7.25k | if (totalSize > |
623 | 7.25k | freerdp_settings_get_uint32(context->settings, FreeRDP_MultifragMaxRequestSize)) |
624 | 117 | { |
625 | 117 | WLog_Print( |
626 | 117 | fastpath->log, WLOG_ERROR, |
627 | 117 | "Total size (%" PRIuz ") exceeds MultifragMaxRequestSize (%" PRIu32 ")", totalSize, |
628 | 117 | freerdp_settings_get_uint32(context->settings, FreeRDP_MultifragMaxRequestSize)); |
629 | 117 | goto out_fail; |
630 | 117 | } |
631 | | |
632 | 7.13k | if (fragmentation == FASTPATH_FRAGMENT_FIRST) |
633 | 2.09k | { |
634 | 2.09k | if (fastpath->fragmentation != -1) |
635 | 34 | { |
636 | 34 | WLog_Print(fastpath->log, WLOG_ERROR, "Unexpected FASTPATH_FRAGMENT_FIRST"); |
637 | 34 | goto out_fail; |
638 | 34 | } |
639 | | |
640 | 2.06k | fastpath->fragmentation = FASTPATH_FRAGMENT_FIRST; |
641 | 2.06k | } |
642 | 5.04k | else if (fragmentation == FASTPATH_FRAGMENT_NEXT) |
643 | 3.07k | { |
644 | 3.07k | if ((fastpath->fragmentation != FASTPATH_FRAGMENT_FIRST) && |
645 | 2.41k | (fastpath->fragmentation != FASTPATH_FRAGMENT_NEXT)) |
646 | 391 | { |
647 | 391 | WLog_Print(fastpath->log, WLOG_ERROR, "Unexpected FASTPATH_FRAGMENT_NEXT"); |
648 | 391 | goto out_fail; |
649 | 391 | } |
650 | | |
651 | 2.67k | fastpath->fragmentation = FASTPATH_FRAGMENT_NEXT; |
652 | 2.67k | } |
653 | 1.97k | else if (fragmentation == FASTPATH_FRAGMENT_LAST) |
654 | 1.97k | { |
655 | 1.97k | if ((fastpath->fragmentation != FASTPATH_FRAGMENT_FIRST) && |
656 | 778 | (fastpath->fragmentation != FASTPATH_FRAGMENT_NEXT)) |
657 | 239 | { |
658 | 239 | WLog_Print(fastpath->log, WLOG_ERROR, "Unexpected FASTPATH_FRAGMENT_LAST"); |
659 | 239 | goto out_fail; |
660 | 239 | } |
661 | | |
662 | 1.73k | fastpath->fragmentation = -1; |
663 | 1.73k | status = fastpath_recv_update(fastpath, updateCode, fastpath->updateData); |
664 | | |
665 | 1.73k | if (status < 0) |
666 | 16 | { |
667 | 16 | WLog_Print(fastpath->log, WLOG_ERROR, "fastpath_recv_update() - %i", status); |
668 | 16 | goto out_fail; |
669 | 16 | } |
670 | 1.73k | } |
671 | 7.13k | } |
672 | | |
673 | 35.4k | return status; |
674 | 6.73k | out_fail: |
675 | 6.73k | return -1; |
676 | 42.2k | } |
677 | | |
678 | | state_run_t fastpath_recv_updates(rdpFastPath* fastpath, wStream* s) |
679 | 15.8k | { |
680 | 15.8k | state_run_t rc = STATE_RUN_FAILED; |
681 | | |
682 | 15.8k | WINPR_ASSERT(s); |
683 | 15.8k | WINPR_ASSERT(fastpath); |
684 | 15.8k | WINPR_ASSERT(fastpath->rdp); |
685 | | |
686 | 51.3k | while (Stream_GetRemainingLength(s) >= 3) |
687 | 50.1k | { |
688 | 50.1k | if (fastpath_recv_update_data(fastpath, s) < 0) |
689 | 14.6k | { |
690 | 14.6k | WLog_Print(fastpath->log, WLOG_ERROR, "fastpath_recv_update_data() fail"); |
691 | 14.6k | rc = STATE_RUN_FAILED; |
692 | 14.6k | goto fail; |
693 | 14.6k | } |
694 | 50.1k | } |
695 | | |
696 | 1.17k | rc = STATE_RUN_SUCCESS; |
697 | 15.8k | fail: |
698 | | |
699 | 15.8k | return rc; |
700 | 1.17k | } |
701 | | |
702 | | WINPR_ATTR_NODISCARD |
703 | | static BOOL fastpath_read_input_event_header(wLog* log, wStream* s, BYTE* eventFlags, |
704 | | BYTE* eventCode) |
705 | 0 | { |
706 | 0 | BYTE eventHeader = 0; |
707 | |
|
708 | 0 | WINPR_ASSERT(s); |
709 | 0 | WINPR_ASSERT(eventFlags); |
710 | 0 | WINPR_ASSERT(eventCode); |
711 | |
|
712 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 1)) |
713 | 0 | return FALSE; |
714 | | |
715 | 0 | Stream_Read_UINT8(s, eventHeader); /* eventHeader (1 byte) */ |
716 | 0 | *eventFlags = (eventHeader & 0x1F); |
717 | 0 | *eventCode = (eventHeader >> 5); |
718 | 0 | return TRUE; |
719 | 0 | } |
720 | | |
721 | | WINPR_ATTR_NODISCARD |
722 | | static BOOL fastpath_recv_input_event_scancode(rdpFastPath* fastpath, wStream* s, BYTE eventFlags) |
723 | 0 | { |
724 | 0 | WINPR_ASSERT(fastpath); |
725 | 0 | WINPR_ASSERT(fastpath->rdp); |
726 | 0 | WINPR_ASSERT(fastpath->rdp->input); |
727 | 0 | WINPR_ASSERT(s); |
728 | |
|
729 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(fastpath->log, s, 1)) |
730 | 0 | return FALSE; |
731 | | |
732 | 0 | rdpInput* input = fastpath->rdp->input; |
733 | |
|
734 | 0 | const UINT8 code = Stream_Get_UINT8(s); /* keyCode (1 byte) */ |
735 | |
|
736 | 0 | UINT16 flags = 0; |
737 | 0 | if ((eventFlags & FASTPATH_INPUT_KBDFLAGS_RELEASE)) |
738 | 0 | flags |= KBD_FLAGS_RELEASE; |
739 | |
|
740 | 0 | if ((eventFlags & FASTPATH_INPUT_KBDFLAGS_EXTENDED)) |
741 | 0 | flags |= KBD_FLAGS_EXTENDED; |
742 | |
|
743 | 0 | if ((eventFlags & FASTPATH_INPUT_KBDFLAGS_PREFIX_E1)) |
744 | 0 | flags |= KBD_FLAGS_EXTENDED1; |
745 | | |
746 | | /* only pass input if we are actually connected */ |
747 | 0 | if (!rdp_has_reached_state(input->context->rdp, CONNECTION_STATE_ACTIVE)) |
748 | 0 | return TRUE; |
749 | 0 | return IFCALLRESULT(TRUE, input->KeyboardEvent, input, flags, code); |
750 | 0 | } |
751 | | |
752 | | WINPR_ATTR_NODISCARD |
753 | | static BOOL fastpath_recv_input_event_mouse(rdpFastPath* fastpath, wStream* s, |
754 | | WINPR_ATTR_UNUSED BYTE eventFlags) |
755 | 0 | { |
756 | 0 | UINT16 pointerFlags = 0; |
757 | 0 | UINT16 xPos = 0; |
758 | 0 | UINT16 yPos = 0; |
759 | 0 | WINPR_ASSERT(fastpath); |
760 | 0 | WINPR_ASSERT(fastpath->rdp); |
761 | 0 | WINPR_ASSERT(fastpath->rdp->input); |
762 | 0 | WINPR_ASSERT(s); |
763 | |
|
764 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(fastpath->log, s, 6)) |
765 | 0 | return FALSE; |
766 | | |
767 | 0 | rdpInput* input = fastpath->rdp->input; |
768 | | |
769 | | /* only pass input if we are actually connected */ |
770 | 0 | if (!rdp_has_reached_state(input->context->rdp, CONNECTION_STATE_ACTIVE)) |
771 | 0 | return TRUE; |
772 | | |
773 | 0 | Stream_Read_UINT16(s, pointerFlags); /* pointerFlags (2 bytes) */ |
774 | 0 | Stream_Read_UINT16(s, xPos); /* xPos (2 bytes) */ |
775 | 0 | Stream_Read_UINT16(s, yPos); /* yPos (2 bytes) */ |
776 | 0 | return IFCALLRESULT(TRUE, input->MouseEvent, input, pointerFlags, xPos, yPos); |
777 | 0 | } |
778 | | |
779 | | WINPR_ATTR_NODISCARD |
780 | | static BOOL fastpath_recv_input_event_relmouse(rdpFastPath* fastpath, wStream* s, |
781 | | WINPR_ATTR_UNUSED BYTE eventFlags) |
782 | 0 | { |
783 | 0 | rdpInput* input = nullptr; |
784 | 0 | UINT16 pointerFlags = 0; |
785 | 0 | INT16 xDelta = 0; |
786 | 0 | INT16 yDelta = 0; |
787 | 0 | WINPR_ASSERT(fastpath); |
788 | 0 | WINPR_ASSERT(fastpath->rdp); |
789 | 0 | WINPR_ASSERT(fastpath->rdp->context); |
790 | 0 | WINPR_ASSERT(fastpath->rdp->input); |
791 | 0 | WINPR_ASSERT(s); |
792 | |
|
793 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(fastpath->log, s, 6)) |
794 | 0 | return FALSE; |
795 | | |
796 | 0 | input = fastpath->rdp->input; |
797 | |
|
798 | 0 | Stream_Read_UINT16(s, pointerFlags); /* pointerFlags (2 bytes) */ |
799 | 0 | Stream_Read_INT16(s, xDelta); /* xDelta (2 bytes) */ |
800 | 0 | Stream_Read_INT16(s, yDelta); /* yDelta (2 bytes) */ |
801 | |
|
802 | 0 | if (!freerdp_settings_get_bool(input->context->settings, FreeRDP_HasRelativeMouseEvent)) |
803 | 0 | { |
804 | 0 | WLog_Print(fastpath->log, WLOG_ERROR, |
805 | 0 | "Received relative mouse event(flags=0x%04" PRIx16 ", xPos=%" PRId16 |
806 | 0 | ", yPos=%" PRId16 "), but we did not announce support for that", |
807 | 0 | pointerFlags, xDelta, yDelta); |
808 | 0 | return FALSE; |
809 | 0 | } |
810 | | |
811 | | /* only pass input if we are actually connected */ |
812 | 0 | if (!rdp_has_reached_state(input->context->rdp, CONNECTION_STATE_ACTIVE)) |
813 | 0 | return TRUE; |
814 | 0 | return IFCALLRESULT(TRUE, input->RelMouseEvent, input, pointerFlags, xDelta, yDelta); |
815 | 0 | } |
816 | | |
817 | | WINPR_ATTR_NODISCARD |
818 | | static BOOL fastpath_recv_input_event_qoe(rdpFastPath* fastpath, wStream* s, |
819 | | WINPR_ATTR_UNUSED BYTE eventFlags) |
820 | 0 | { |
821 | 0 | WINPR_ASSERT(fastpath); |
822 | 0 | WINPR_ASSERT(fastpath->rdp); |
823 | 0 | WINPR_ASSERT(fastpath->rdp->context); |
824 | 0 | WINPR_ASSERT(fastpath->rdp->input); |
825 | 0 | WINPR_ASSERT(s); |
826 | |
|
827 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(fastpath->log, s, 4)) |
828 | 0 | return FALSE; |
829 | | |
830 | 0 | rdpInput* input = fastpath->rdp->input; |
831 | |
|
832 | 0 | UINT32 timestampMS = 0; |
833 | 0 | Stream_Read_UINT32(s, timestampMS); /* timestamp (4 bytes) */ |
834 | |
|
835 | 0 | if (!freerdp_settings_get_bool(input->context->settings, FreeRDP_HasQoeEvent)) |
836 | 0 | { |
837 | 0 | WLog_Print(fastpath->log, WLOG_ERROR, |
838 | 0 | "Received qoe event(timestamp=%" PRIu32 |
839 | 0 | "ms), but we did not announce support for that", |
840 | 0 | timestampMS); |
841 | 0 | return FALSE; |
842 | 0 | } |
843 | | |
844 | | /* only pass input if we are actually connected */ |
845 | 0 | if (!rdp_has_reached_state(input->context->rdp, CONNECTION_STATE_ACTIVE)) |
846 | 0 | return TRUE; |
847 | 0 | return IFCALLRESULT(TRUE, input->QoEEvent, input, timestampMS); |
848 | 0 | } |
849 | | |
850 | | WINPR_ATTR_NODISCARD |
851 | | static BOOL fastpath_recv_input_event_mousex(rdpFastPath* fastpath, wStream* s, |
852 | | WINPR_ATTR_UNUSED BYTE eventFlags) |
853 | 0 | { |
854 | 0 | rdpInput* input = nullptr; |
855 | 0 | UINT16 pointerFlags = 0; |
856 | 0 | UINT16 xPos = 0; |
857 | 0 | UINT16 yPos = 0; |
858 | |
|
859 | 0 | WINPR_ASSERT(fastpath); |
860 | 0 | WINPR_ASSERT(fastpath->rdp); |
861 | 0 | WINPR_ASSERT(fastpath->rdp->context); |
862 | 0 | WINPR_ASSERT(fastpath->rdp->input); |
863 | 0 | WINPR_ASSERT(s); |
864 | |
|
865 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(fastpath->log, s, 6)) |
866 | 0 | return FALSE; |
867 | | |
868 | 0 | input = fastpath->rdp->input; |
869 | |
|
870 | 0 | Stream_Read_UINT16(s, pointerFlags); /* pointerFlags (2 bytes) */ |
871 | 0 | Stream_Read_UINT16(s, xPos); /* xPos (2 bytes) */ |
872 | 0 | Stream_Read_UINT16(s, yPos); /* yPos (2 bytes) */ |
873 | |
|
874 | 0 | if (!freerdp_settings_get_bool(input->context->settings, FreeRDP_HasExtendedMouseEvent)) |
875 | 0 | { |
876 | 0 | WLog_Print(fastpath->log, WLOG_ERROR, |
877 | 0 | "Received extended mouse event(flags=0x%04" PRIx16 ", xPos=%" PRIu16 |
878 | 0 | ", yPos=%" PRIu16 "), but we did not announce support for that", |
879 | 0 | pointerFlags, xPos, yPos); |
880 | 0 | return FALSE; |
881 | 0 | } |
882 | | |
883 | | /* only pass input if we are actually connected */ |
884 | 0 | if (!rdp_has_reached_state(input->context->rdp, CONNECTION_STATE_ACTIVE)) |
885 | 0 | return TRUE; |
886 | | |
887 | 0 | return IFCALLRESULT(TRUE, input->ExtendedMouseEvent, input, pointerFlags, xPos, yPos); |
888 | 0 | } |
889 | | |
890 | | WINPR_ATTR_NODISCARD |
891 | | static BOOL fastpath_recv_input_event_sync(rdpFastPath* fastpath, WINPR_ATTR_UNUSED wStream* s, |
892 | | BYTE eventFlags) |
893 | 0 | { |
894 | 0 | WINPR_ASSERT(fastpath); |
895 | 0 | WINPR_ASSERT(fastpath->rdp); |
896 | 0 | WINPR_ASSERT(fastpath->rdp->input); |
897 | 0 | WINPR_ASSERT(s); |
898 | |
|
899 | 0 | rdpInput* input = fastpath->rdp->input; |
900 | | |
901 | | /* only pass input if we are actually connected */ |
902 | 0 | if (!rdp_has_reached_state(input->context->rdp, CONNECTION_STATE_ACTIVE)) |
903 | 0 | return TRUE; |
904 | 0 | return IFCALLRESULT(TRUE, input->SynchronizeEvent, input, eventFlags); |
905 | 0 | } |
906 | | |
907 | | WINPR_ATTR_NODISCARD |
908 | | static BOOL fastpath_recv_input_event_unicode(rdpFastPath* fastpath, wStream* s, BYTE eventFlags) |
909 | 0 | { |
910 | 0 | UINT16 unicodeCode = 0; |
911 | 0 | UINT16 flags = 0; |
912 | |
|
913 | 0 | WINPR_ASSERT(fastpath); |
914 | 0 | WINPR_ASSERT(s); |
915 | |
|
916 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(fastpath->log, s, 2)) |
917 | 0 | return FALSE; |
918 | | |
919 | 0 | Stream_Read_UINT16(s, unicodeCode); /* unicodeCode (2 bytes) */ |
920 | 0 | flags = 0; |
921 | |
|
922 | 0 | if ((eventFlags & FASTPATH_INPUT_KBDFLAGS_RELEASE)) |
923 | 0 | flags |= KBD_FLAGS_RELEASE; |
924 | | |
925 | | /* only pass input if we are actually connected */ |
926 | 0 | if (!rdp_has_reached_state(fastpath->rdp->input->context->rdp, CONNECTION_STATE_ACTIVE)) |
927 | 0 | return TRUE; |
928 | | |
929 | 0 | WINPR_ASSERT(fastpath->rdp); |
930 | 0 | WINPR_ASSERT(fastpath->rdp); |
931 | 0 | WINPR_ASSERT(fastpath->rdp->input); |
932 | 0 | return IFCALLRESULT(FALSE, fastpath->rdp->input->UnicodeKeyboardEvent, fastpath->rdp->input, |
933 | 0 | flags, unicodeCode); |
934 | 0 | } |
935 | | |
936 | | WINPR_ATTR_NODISCARD |
937 | | static BOOL fastpath_recv_input_event(rdpFastPath* fastpath, wStream* s) |
938 | 512k | { |
939 | 512k | BYTE eventFlags = 0; |
940 | 512k | BYTE eventCode = 0; |
941 | | |
942 | 512k | WINPR_ASSERT(fastpath); |
943 | 512k | WINPR_ASSERT(s); |
944 | | |
945 | 512k | if (!rdp_is_active_state(fastpath->rdp)) |
946 | 512k | { |
947 | 512k | WLog_Print(fastpath->log, WLOG_WARN, "Input event before activation, ignoring"); |
948 | 512k | return TRUE; |
949 | 512k | } |
950 | | |
951 | 0 | if (!fastpath_read_input_event_header(fastpath->log, s, &eventFlags, &eventCode)) |
952 | 0 | return FALSE; |
953 | | |
954 | 0 | switch (eventCode) |
955 | 0 | { |
956 | 0 | case FASTPATH_INPUT_EVENT_SCANCODE: |
957 | 0 | if (!fastpath_recv_input_event_scancode(fastpath, s, eventFlags)) |
958 | 0 | return FALSE; |
959 | | |
960 | 0 | break; |
961 | | |
962 | 0 | case FASTPATH_INPUT_EVENT_MOUSE: |
963 | 0 | if (!fastpath_recv_input_event_mouse(fastpath, s, eventFlags)) |
964 | 0 | return FALSE; |
965 | | |
966 | 0 | break; |
967 | | |
968 | 0 | case FASTPATH_INPUT_EVENT_MOUSEX: |
969 | 0 | if (!fastpath_recv_input_event_mousex(fastpath, s, eventFlags)) |
970 | 0 | return FALSE; |
971 | | |
972 | 0 | break; |
973 | | |
974 | 0 | case FASTPATH_INPUT_EVENT_SYNC: |
975 | 0 | if (!fastpath_recv_input_event_sync(fastpath, s, eventFlags)) |
976 | 0 | return FALSE; |
977 | | |
978 | 0 | break; |
979 | | |
980 | 0 | case FASTPATH_INPUT_EVENT_UNICODE: |
981 | 0 | if (!fastpath_recv_input_event_unicode(fastpath, s, eventFlags)) |
982 | 0 | return FALSE; |
983 | | |
984 | 0 | break; |
985 | | |
986 | 0 | case TS_FP_RELPOINTER_EVENT: |
987 | 0 | if (!fastpath_recv_input_event_relmouse(fastpath, s, eventFlags)) |
988 | 0 | return FALSE; |
989 | | |
990 | 0 | break; |
991 | | |
992 | 0 | case TS_FP_QOETIMESTAMP_EVENT: |
993 | 0 | if (!fastpath_recv_input_event_qoe(fastpath, s, eventFlags)) |
994 | 0 | return FALSE; |
995 | 0 | break; |
996 | | |
997 | 0 | default: |
998 | 0 | WLog_Print(fastpath->log, WLOG_ERROR, "Unknown eventCode %" PRIu8 "", eventCode); |
999 | 0 | break; |
1000 | 0 | } |
1001 | | |
1002 | 0 | return TRUE; |
1003 | 0 | } |
1004 | | |
1005 | | state_run_t fastpath_recv_inputs(rdpFastPath* fastpath, wStream* s) |
1006 | 15.8k | { |
1007 | 15.8k | WINPR_ASSERT(fastpath); |
1008 | 15.8k | WINPR_ASSERT(s); |
1009 | | |
1010 | 15.8k | if (fastpath->numberEvents == 0) |
1011 | 15.8k | { |
1012 | | /** |
1013 | | * If numberEvents is not provided in fpInputHeader, it will be provided |
1014 | | * as one additional byte here. |
1015 | | */ |
1016 | 15.8k | if (!Stream_CheckAndLogRequiredLengthWLog(fastpath->log, s, 1)) |
1017 | 3.40k | return STATE_RUN_FAILED; |
1018 | | |
1019 | 12.4k | Stream_Read_UINT8(s, fastpath->numberEvents); /* eventHeader (1 byte) */ |
1020 | 12.4k | } |
1021 | | |
1022 | 525k | for (BYTE i = 0; i < fastpath->numberEvents; i++) |
1023 | 512k | { |
1024 | 512k | if (!fastpath_recv_input_event(fastpath, s)) |
1025 | 0 | return STATE_RUN_FAILED; |
1026 | 512k | } |
1027 | | |
1028 | 12.4k | return STATE_RUN_SUCCESS; |
1029 | 12.4k | } |
1030 | | |
1031 | | WINPR_ATTR_NODISCARD |
1032 | | static UINT32 fastpath_get_sec_bytes(rdpRdp* rdp) |
1033 | 0 | { |
1034 | 0 | UINT32 sec_bytes = 0; |
1035 | |
|
1036 | 0 | if (!rdp) |
1037 | 0 | return 0; |
1038 | | |
1039 | 0 | if (rdp->do_crypt) |
1040 | 0 | { |
1041 | 0 | sec_bytes = 8; |
1042 | |
|
1043 | 0 | if (freerdp_settings_get_uint32(rdp->settings, FreeRDP_EncryptionMethods) == |
1044 | 0 | ENCRYPTION_METHOD_FIPS) |
1045 | 0 | sec_bytes += 4; |
1046 | 0 | } |
1047 | |
|
1048 | 0 | return sec_bytes; |
1049 | 0 | } |
1050 | | |
1051 | | wStream* fastpath_input_pdu_init_header(rdpFastPath* fastpath, UINT16* sec_flags) |
1052 | 0 | { |
1053 | 0 | if (!fastpath || !fastpath->rdp) |
1054 | 0 | return nullptr; |
1055 | | |
1056 | 0 | rdpRdp* rdp = fastpath->rdp; |
1057 | 0 | wStream* s = transport_send_stream_init(rdp->transport, 256); |
1058 | |
|
1059 | 0 | if (!s) |
1060 | 0 | return nullptr; |
1061 | | |
1062 | 0 | Stream_Seek(s, 3); /* fpInputHeader, length1 and length2 */ |
1063 | |
|
1064 | 0 | if (rdp->do_crypt) |
1065 | 0 | { |
1066 | 0 | *sec_flags |= SEC_ENCRYPT; |
1067 | |
|
1068 | 0 | if (rdp->do_secure_checksum) |
1069 | 0 | *sec_flags |= SEC_SECURE_CHECKSUM; |
1070 | 0 | } |
1071 | |
|
1072 | 0 | Stream_Seek(s, fastpath_get_sec_bytes(rdp)); |
1073 | 0 | return s; |
1074 | 0 | } |
1075 | | |
1076 | | wStream* fastpath_input_pdu_init(rdpFastPath* fastpath, BYTE eventFlags, BYTE eventCode, |
1077 | | UINT16* sec_flags) |
1078 | 0 | { |
1079 | 0 | wStream* s = nullptr; |
1080 | 0 | s = fastpath_input_pdu_init_header(fastpath, sec_flags); |
1081 | |
|
1082 | 0 | if (!s) |
1083 | 0 | return nullptr; |
1084 | | |
1085 | 0 | WINPR_ASSERT(eventCode < 8); |
1086 | 0 | WINPR_ASSERT(eventFlags < 0x20); |
1087 | 0 | Stream_Write_UINT8(s, (UINT8)(eventFlags | (eventCode << 5))); /* eventHeader (1 byte) */ |
1088 | 0 | return s; |
1089 | 0 | } |
1090 | | |
1091 | | BOOL fastpath_send_multiple_input_pdu(rdpFastPath* fastpath, wStream* s, size_t iNumEvents, |
1092 | | UINT16 sec_flags) |
1093 | 0 | { |
1094 | 0 | BOOL rc = FALSE; |
1095 | 0 | BYTE eventHeader = 0; |
1096 | 0 | BOOL should_unlock = FALSE; |
1097 | |
|
1098 | 0 | if (!s) |
1099 | 0 | return FALSE; |
1100 | | |
1101 | 0 | if (!fastpath) |
1102 | 0 | { |
1103 | 0 | Stream_Release(s); |
1104 | 0 | return FALSE; |
1105 | 0 | } |
1106 | | |
1107 | 0 | rdpRdp* rdp = fastpath->rdp; |
1108 | 0 | WINPR_ASSERT(rdp); |
1109 | |
|
1110 | 0 | WINPR_ASSERT(iNumEvents > 0); |
1111 | 0 | { |
1112 | 0 | const CONNECTION_STATE state = rdp_get_state(rdp); |
1113 | 0 | if (!rdp_is_active_state(rdp)) |
1114 | 0 | { |
1115 | 0 | if (!rdp->was_deactivated) |
1116 | 0 | { |
1117 | 0 | WLog_Print(fastpath->log, WLOG_WARN, "called before activation [%s]", |
1118 | 0 | rdp_state_string(state)); |
1119 | 0 | goto fail; |
1120 | 0 | } |
1121 | | |
1122 | 0 | WLog_Print(fastpath->log, WLOG_DEBUG, |
1123 | 0 | "called during deactivate-reactivate [%s], ignoring", |
1124 | 0 | rdp_state_string(state)); |
1125 | 0 | rc = TRUE; |
1126 | 0 | goto fail; |
1127 | 0 | } |
1128 | 0 | } |
1129 | | |
1130 | | /* |
1131 | | * A maximum of 15 events are allowed per request |
1132 | | * if the optional numEvents field isn't used |
1133 | | * see MS-RDPBCGR 2.2.8.1.2 for details |
1134 | | */ |
1135 | 0 | if (iNumEvents > 15) |
1136 | 0 | goto fail; |
1137 | | |
1138 | 0 | { |
1139 | 0 | size_t length = Stream_GetPosition(s); |
1140 | |
|
1141 | 0 | if (length >= (2u << 14)) |
1142 | 0 | { |
1143 | 0 | WLog_Print(fastpath->log, WLOG_ERROR, "Maximum FastPath PDU length is 32767"); |
1144 | 0 | goto fail; |
1145 | 0 | } |
1146 | | |
1147 | 0 | eventHeader = FASTPATH_INPUT_ACTION_FASTPATH; |
1148 | 0 | eventHeader |= (iNumEvents << 2); /* numberEvents */ |
1149 | |
|
1150 | 0 | if (sec_flags & SEC_ENCRYPT) |
1151 | 0 | eventHeader |= (FASTPATH_INPUT_ENCRYPTED << 6); |
1152 | |
|
1153 | 0 | if (sec_flags & SEC_SECURE_CHECKSUM) |
1154 | 0 | eventHeader |= (FASTPATH_INPUT_SECURE_CHECKSUM << 6); |
1155 | |
|
1156 | 0 | Stream_ResetPosition(s); |
1157 | 0 | Stream_Write_UINT8(s, eventHeader); |
1158 | | /* Write length later, RDP encryption might add a padding */ |
1159 | 0 | Stream_Seek(s, 2); |
1160 | |
|
1161 | 0 | if (sec_flags & SEC_ENCRYPT) |
1162 | 0 | { |
1163 | 0 | security_lock(rdp); |
1164 | 0 | should_unlock = TRUE; |
1165 | |
|
1166 | 0 | const size_t sec_bytes = fastpath_get_sec_bytes(fastpath->rdp); |
1167 | 0 | if (sec_bytes + 3ULL > length) |
1168 | 0 | goto fail; |
1169 | | |
1170 | 0 | BYTE* fpInputEvents = Stream_PointerAs(s, BYTE) + sec_bytes; |
1171 | 0 | const UINT16 fpInputEvents_length = (UINT16)(length - 3 - sec_bytes); |
1172 | |
|
1173 | 0 | WINPR_ASSERT(rdp->settings); |
1174 | 0 | if (freerdp_settings_get_uint32(rdp->settings, FreeRDP_EncryptionMethods) == |
1175 | 0 | ENCRYPTION_METHOD_FIPS) |
1176 | 0 | { |
1177 | 0 | BYTE pad = 0; |
1178 | |
|
1179 | 0 | if ((pad = 8 - (fpInputEvents_length % 8)) == 8) |
1180 | 0 | pad = 0; |
1181 | |
|
1182 | 0 | if (!Stream_CheckAndLogRequiredCapacityWLog(fastpath->log, s, 4)) |
1183 | 0 | goto fail; |
1184 | | |
1185 | 0 | Stream_Write_UINT16(s, 0x10); /* length */ |
1186 | 0 | Stream_Write_UINT8(s, 0x1); /* TSFIPS_VERSION 1*/ |
1187 | 0 | Stream_Write_UINT8(s, pad); /* padding */ |
1188 | |
|
1189 | 0 | if (!Stream_CheckAndLogRequiredCapacityWLog( |
1190 | 0 | fastpath->log, s, 1ull * fpInputEvents_length + sec_bytes + pad + 8ull)) |
1191 | 0 | goto fail; |
1192 | | |
1193 | 0 | if (!security_hmac_signature(fpInputEvents, fpInputEvents_length, Stream_Pointer(s), |
1194 | 0 | 8, rdp)) |
1195 | 0 | goto fail; |
1196 | | |
1197 | 0 | if (pad) |
1198 | 0 | memset(fpInputEvents + fpInputEvents_length, 0, pad); |
1199 | |
|
1200 | 0 | if (!security_fips_encrypt(fpInputEvents, fpInputEvents_length + pad, rdp)) |
1201 | 0 | goto fail; |
1202 | | |
1203 | 0 | length += pad; |
1204 | 0 | } |
1205 | 0 | else |
1206 | 0 | { |
1207 | 0 | BOOL res = 0; |
1208 | 0 | if (!Stream_CheckAndLogRequiredCapacityWLog(fastpath->log, s, 8)) |
1209 | 0 | goto fail; |
1210 | 0 | if (sec_flags & SEC_SECURE_CHECKSUM) |
1211 | 0 | res = security_salted_mac_signature(rdp, fpInputEvents, fpInputEvents_length, |
1212 | 0 | TRUE, Stream_Pointer(s), 8); |
1213 | 0 | else |
1214 | 0 | res = security_mac_signature(rdp, fpInputEvents, fpInputEvents_length, |
1215 | 0 | Stream_Pointer(s), 8); |
1216 | |
|
1217 | 0 | if (!res || !security_encrypt(fpInputEvents, fpInputEvents_length, rdp)) |
1218 | 0 | goto fail; |
1219 | 0 | } |
1220 | 0 | } |
1221 | | |
1222 | | /* |
1223 | | * We always encode length in two bytes, even though we could use |
1224 | | * only one byte if length <= 0x7F. It is just easier that way, |
1225 | | * because we can leave room for fixed-length header, store all |
1226 | | * the data first and then store the header. |
1227 | | */ |
1228 | 0 | WINPR_ASSERT(length < UINT16_MAX); |
1229 | 0 | if (!Stream_SetPosition(s, 1)) |
1230 | 0 | goto fail; |
1231 | 0 | Stream_Write_UINT16_BE(s, 0x8000 | (UINT16)length); |
1232 | 0 | if (!Stream_SetPosition(s, length)) |
1233 | 0 | goto fail; |
1234 | 0 | Stream_SealLength(s); |
1235 | 0 | } |
1236 | | |
1237 | 0 | if (transport_write(rdp->transport, s) < 0) |
1238 | 0 | goto fail; |
1239 | | |
1240 | 0 | rc = TRUE; |
1241 | 0 | fail: |
1242 | 0 | if (should_unlock) |
1243 | 0 | security_unlock(rdp); |
1244 | 0 | Stream_Release(s); |
1245 | 0 | return rc; |
1246 | 0 | } |
1247 | | |
1248 | | BOOL fastpath_send_input_pdu(rdpFastPath* fastpath, wStream* s, UINT16 sec_flags) |
1249 | 0 | { |
1250 | 0 | return fastpath_send_multiple_input_pdu(fastpath, s, 1, sec_flags); |
1251 | 0 | } |
1252 | | |
1253 | | wStream* fastpath_update_pdu_init(rdpFastPath* fastpath) |
1254 | 5.93k | { |
1255 | 5.93k | return transport_send_stream_init(fastpath->rdp->transport, FASTPATH_MAX_PACKET_SIZE); |
1256 | 5.93k | } |
1257 | | |
1258 | | wStream* fastpath_update_pdu_init_new(WINPR_ATTR_UNUSED rdpFastPath* fastpath) |
1259 | 17.5k | { |
1260 | 17.5k | wStream* s = nullptr; |
1261 | 17.5k | s = Stream_New(nullptr, FASTPATH_MAX_PACKET_SIZE); |
1262 | 17.5k | return s; |
1263 | 17.5k | } |
1264 | | |
1265 | | BOOL fastpath_send_update_pdu(rdpFastPath* fastpath, BYTE updateCode, wStream* s, |
1266 | | BOOL skipCompression) |
1267 | 5.93k | { |
1268 | 5.93k | BOOL status = TRUE; |
1269 | 5.93k | wStream* fs = nullptr; |
1270 | 5.93k | rdpSettings* settings = nullptr; |
1271 | 5.93k | rdpRdp* rdp = nullptr; |
1272 | 5.93k | UINT32 fpHeaderSize = 6; |
1273 | 5.93k | UINT32 fpUpdatePduHeaderSize = 0; |
1274 | 5.93k | UINT32 fpUpdateHeaderSize = 0; |
1275 | 5.93k | FASTPATH_UPDATE_PDU_HEADER fpUpdatePduHeader = WINPR_C_ARRAY_INIT; |
1276 | 5.93k | FASTPATH_UPDATE_HEADER fpUpdateHeader = WINPR_C_ARRAY_INIT; |
1277 | 5.93k | UINT16 sec_flags = 0; |
1278 | | |
1279 | 5.93k | if (!fastpath || !fastpath->rdp || !fastpath->fs || !s) |
1280 | 0 | return FALSE; |
1281 | | |
1282 | 5.93k | rdp = fastpath->rdp; |
1283 | 5.93k | fs = fastpath->fs; |
1284 | 5.93k | settings = rdp->settings; |
1285 | | |
1286 | 5.93k | if (!settings) |
1287 | 0 | return FALSE; |
1288 | | |
1289 | 5.93k | UINT16 maxLength = FASTPATH_MAX_PACKET_SIZE - 20; |
1290 | | |
1291 | 5.93k | if (freerdp_settings_get_bool(rdp->settings, FreeRDP_CompressionEnabled) && !skipCompression) |
1292 | 5.93k | { |
1293 | 5.93k | const UINT16 CompressionMaxSize = bulk_compression_max_size(rdp->bulk); |
1294 | 5.93k | maxLength = (maxLength < CompressionMaxSize) ? maxLength : CompressionMaxSize; |
1295 | 5.93k | maxLength -= 20; |
1296 | 5.93k | } |
1297 | | |
1298 | 5.93k | size_t totalLength = Stream_GetPosition(s); |
1299 | 5.93k | Stream_ResetPosition(s); |
1300 | | |
1301 | | /* check if fast path output is possible */ |
1302 | 5.93k | if (!freerdp_settings_get_bool(rdp->settings, FreeRDP_FastPathOutput)) |
1303 | 0 | { |
1304 | 0 | WLog_Print(fastpath->log, WLOG_ERROR, "client does not support fast path output"); |
1305 | 0 | return FALSE; |
1306 | 0 | } |
1307 | | |
1308 | | /* check if the client's fast path pdu buffer is large enough */ |
1309 | 5.93k | if (totalLength > freerdp_settings_get_uint32(settings, FreeRDP_MultifragMaxRequestSize)) |
1310 | 5.89k | { |
1311 | 5.89k | WLog_Print(fastpath->log, WLOG_ERROR, |
1312 | 5.89k | "fast path update size (%" PRIuz |
1313 | 5.89k | ") exceeds the client's maximum request size (%" PRIu32 ")", |
1314 | 5.89k | totalLength, |
1315 | 5.89k | freerdp_settings_get_uint32(settings, FreeRDP_MultifragMaxRequestSize)); |
1316 | 5.89k | return FALSE; |
1317 | 5.89k | } |
1318 | | |
1319 | 45 | if (rdp->do_crypt) |
1320 | 0 | { |
1321 | 0 | sec_flags |= SEC_ENCRYPT; |
1322 | |
|
1323 | 0 | if (rdp->do_secure_checksum) |
1324 | 0 | sec_flags |= SEC_SECURE_CHECKSUM; |
1325 | 0 | } |
1326 | | |
1327 | 45 | for (int fragment = 0; (totalLength > 0) || (fragment == 0); fragment++) |
1328 | 45 | { |
1329 | 45 | UINT32 DstSize = 0; |
1330 | 45 | const BYTE* pDstData = nullptr; |
1331 | 45 | UINT32 compressionFlags = 0; |
1332 | 45 | BYTE pad = 0; |
1333 | 45 | BYTE* pSignature = nullptr; |
1334 | 45 | fpUpdatePduHeader.action = 0; |
1335 | 45 | fpUpdatePduHeader.secFlags = 0; |
1336 | 45 | fpUpdateHeader.compression = 0; |
1337 | 45 | fpUpdateHeader.compressionFlags = 0; |
1338 | 45 | fpUpdateHeader.updateCode = updateCode; |
1339 | 45 | fpUpdateHeader.size = (UINT16)(totalLength > maxLength) ? maxLength : (UINT16)totalLength; |
1340 | 45 | const BYTE* pSrcData = Stream_Pointer(s); |
1341 | 45 | UINT32 SrcSize = DstSize = fpUpdateHeader.size; |
1342 | 45 | BOOL should_unlock = FALSE; |
1343 | | |
1344 | 45 | if (sec_flags & SEC_ENCRYPT) |
1345 | 0 | fpUpdatePduHeader.secFlags |= FASTPATH_OUTPUT_ENCRYPTED; |
1346 | | |
1347 | 45 | if (sec_flags & SEC_SECURE_CHECKSUM) |
1348 | 0 | fpUpdatePduHeader.secFlags |= FASTPATH_OUTPUT_SECURE_CHECKSUM; |
1349 | | |
1350 | 45 | if (freerdp_settings_get_bool(settings, FreeRDP_CompressionEnabled) && !skipCompression) |
1351 | 45 | { |
1352 | 45 | if (bulk_compress(rdp->bulk, pSrcData, SrcSize, &pDstData, &DstSize, |
1353 | 45 | &compressionFlags) >= 0) |
1354 | 45 | { |
1355 | 45 | if (compressionFlags) |
1356 | 0 | { |
1357 | 0 | WINPR_ASSERT(compressionFlags <= UINT8_MAX); |
1358 | 0 | fpUpdateHeader.compressionFlags = (UINT8)compressionFlags; |
1359 | 0 | fpUpdateHeader.compression = FASTPATH_OUTPUT_COMPRESSION_USED; |
1360 | 0 | } |
1361 | 45 | } |
1362 | 45 | } |
1363 | | |
1364 | 45 | if (!fpUpdateHeader.compression) |
1365 | 45 | { |
1366 | 45 | pDstData = Stream_Pointer(s); |
1367 | 45 | DstSize = fpUpdateHeader.size; |
1368 | 45 | } |
1369 | | |
1370 | 45 | if (DstSize > UINT16_MAX) |
1371 | 0 | return FALSE; |
1372 | 45 | fpUpdateHeader.size = (UINT16)DstSize; |
1373 | 45 | totalLength -= SrcSize; |
1374 | | |
1375 | 45 | if (totalLength == 0) |
1376 | 45 | fpUpdateHeader.fragmentation = |
1377 | 45 | (fragment == 0) ? FASTPATH_FRAGMENT_SINGLE : FASTPATH_FRAGMENT_LAST; |
1378 | 0 | else |
1379 | 0 | fpUpdateHeader.fragmentation = |
1380 | 0 | (fragment == 0) ? FASTPATH_FRAGMENT_FIRST : FASTPATH_FRAGMENT_NEXT; |
1381 | | |
1382 | 45 | fpUpdateHeaderSize = fastpath_get_update_header_size(&fpUpdateHeader); |
1383 | 45 | fpUpdatePduHeaderSize = fastpath_get_update_pdu_header_size(&fpUpdatePduHeader, rdp); |
1384 | 45 | fpHeaderSize = fpUpdateHeaderSize + fpUpdatePduHeaderSize; |
1385 | | |
1386 | 45 | if (sec_flags & SEC_ENCRYPT) |
1387 | 0 | { |
1388 | 0 | pSignature = Stream_Buffer(fs) + 3; |
1389 | |
|
1390 | 0 | if (freerdp_settings_get_uint32(rdp->settings, FreeRDP_EncryptionMethods) == |
1391 | 0 | ENCRYPTION_METHOD_FIPS) |
1392 | 0 | { |
1393 | 0 | pSignature += 4; |
1394 | |
|
1395 | 0 | if ((pad = 8 - ((DstSize + fpUpdateHeaderSize) % 8)) == 8) |
1396 | 0 | pad = 0; |
1397 | |
|
1398 | 0 | fpUpdatePduHeader.fipsInformation[0] = 0x10; |
1399 | 0 | fpUpdatePduHeader.fipsInformation[1] = 0x00; |
1400 | 0 | fpUpdatePduHeader.fipsInformation[2] = 0x01; |
1401 | 0 | fpUpdatePduHeader.fipsInformation[3] = pad; |
1402 | 0 | } |
1403 | 0 | } |
1404 | | |
1405 | 45 | const size_t len = fpUpdateHeader.size + fpHeaderSize + pad; |
1406 | 45 | if (len > UINT16_MAX) |
1407 | 0 | return FALSE; |
1408 | | |
1409 | 45 | fpUpdatePduHeader.length = (UINT16)len; |
1410 | 45 | Stream_ResetPosition(fs); |
1411 | 45 | if (!fastpath_write_update_pdu_header(fastpath->log, fs, &fpUpdatePduHeader, rdp)) |
1412 | 0 | return FALSE; |
1413 | 45 | if (!fastpath_write_update_header(fastpath->log, fs, &fpUpdateHeader)) |
1414 | 0 | return FALSE; |
1415 | | |
1416 | 45 | if (!Stream_CheckAndLogRequiredCapacityWLog(fastpath->log, (fs), (size_t)DstSize + pad)) |
1417 | 0 | return FALSE; |
1418 | 45 | Stream_Write(fs, pDstData, DstSize); |
1419 | | |
1420 | 45 | if (pad) |
1421 | 0 | Stream_Zero(fs, pad); |
1422 | | |
1423 | 45 | BOOL res = FALSE; |
1424 | 45 | if (sec_flags & SEC_ENCRYPT) |
1425 | 0 | { |
1426 | 0 | security_lock(rdp); |
1427 | |
|
1428 | 0 | should_unlock = TRUE; |
1429 | 0 | UINT32 dataSize = fpUpdateHeaderSize + DstSize + pad; |
1430 | 0 | BYTE* data = Stream_PointerAs(fs, BYTE) - dataSize; |
1431 | |
|
1432 | 0 | if (freerdp_settings_get_uint32(rdp->settings, FreeRDP_EncryptionMethods) == |
1433 | 0 | ENCRYPTION_METHOD_FIPS) |
1434 | 0 | { |
1435 | | // TODO: Ensure stream capacity |
1436 | 0 | if (!security_hmac_signature(data, dataSize - pad, pSignature, 8, rdp)) |
1437 | 0 | goto unlock; |
1438 | | |
1439 | 0 | if (!security_fips_encrypt(data, dataSize, rdp)) |
1440 | 0 | goto unlock; |
1441 | 0 | } |
1442 | 0 | else |
1443 | 0 | { |
1444 | | // TODO: Ensure stream capacity |
1445 | 0 | if (sec_flags & SEC_SECURE_CHECKSUM) |
1446 | 0 | status = |
1447 | 0 | security_salted_mac_signature(rdp, data, dataSize, TRUE, pSignature, 8); |
1448 | 0 | else |
1449 | 0 | status = security_mac_signature(rdp, data, dataSize, pSignature, 8); |
1450 | |
|
1451 | 0 | if (!status || !security_encrypt(data, dataSize, rdp)) |
1452 | 0 | goto unlock; |
1453 | 0 | } |
1454 | 0 | } |
1455 | 45 | res = TRUE; |
1456 | | |
1457 | 45 | Stream_SealLength(fs); |
1458 | | |
1459 | 45 | if (transport_write(rdp->transport, fs) < 0) |
1460 | 45 | { |
1461 | 45 | status = FALSE; |
1462 | 45 | } |
1463 | | |
1464 | 45 | unlock: |
1465 | 45 | if (should_unlock) |
1466 | 0 | security_unlock(rdp); |
1467 | | |
1468 | 45 | if (!res || !status) |
1469 | 45 | return FALSE; |
1470 | | |
1471 | 0 | Stream_Seek(s, SrcSize); |
1472 | 0 | } |
1473 | | |
1474 | 0 | return status; |
1475 | 45 | } |
1476 | | |
1477 | | rdpFastPath* fastpath_new(rdpRdp* rdp) |
1478 | 24.7k | { |
1479 | 24.7k | WINPR_ASSERT(rdp); |
1480 | | |
1481 | 24.7k | rdpFastPath* fastpath = (rdpFastPath*)calloc(1, sizeof(rdpFastPath)); |
1482 | | |
1483 | 24.7k | if (!fastpath) |
1484 | 0 | return nullptr; |
1485 | 24.7k | fastpath->log = WLog_Get(FTAG); |
1486 | 24.7k | fastpath->rdp = rdp; |
1487 | 24.7k | fastpath->fragmentation = -1; |
1488 | 24.7k | fastpath->fs = Stream_New(nullptr, FASTPATH_MAX_PACKET_SIZE); |
1489 | 24.7k | fastpath->updateData = Stream_New(nullptr, FASTPATH_MAX_PACKET_SIZE); |
1490 | | |
1491 | 24.7k | if (!fastpath->fs || !fastpath->updateData) |
1492 | 0 | goto out_free; |
1493 | | |
1494 | 24.7k | return fastpath; |
1495 | 0 | out_free: |
1496 | 0 | fastpath_free(fastpath); |
1497 | 0 | return nullptr; |
1498 | 24.7k | } |
1499 | | |
1500 | | void fastpath_free(rdpFastPath* fastpath) |
1501 | 24.7k | { |
1502 | 24.7k | if (fastpath) |
1503 | 24.7k | { |
1504 | 24.7k | Stream_Free(fastpath->updateData, TRUE); |
1505 | 24.7k | Stream_Free(fastpath->fs, TRUE); |
1506 | 24.7k | free(fastpath); |
1507 | 24.7k | } |
1508 | 24.7k | } |
1509 | | |
1510 | | BYTE fastpath_get_encryption_flags(rdpFastPath* fastpath) |
1511 | 17.7k | { |
1512 | 17.7k | WINPR_ASSERT(fastpath); |
1513 | 17.7k | return fastpath->encryptionFlags; |
1514 | 17.7k | } |
1515 | | |
1516 | | BOOL fastpath_decrypt(rdpFastPath* fastpath, wStream* s, UINT16* length) |
1517 | 15.8k | { |
1518 | 15.8k | WINPR_ASSERT(fastpath); |
1519 | 15.8k | if (fastpath_get_encryption_flags(fastpath) & FASTPATH_OUTPUT_ENCRYPTED) |
1520 | 1.92k | { |
1521 | 1.92k | const UINT16 flags = |
1522 | 1.92k | (fastpath_get_encryption_flags(fastpath) & FASTPATH_OUTPUT_SECURE_CHECKSUM) |
1523 | 1.92k | ? SEC_SECURE_CHECKSUM |
1524 | 1.92k | : 0; |
1525 | | |
1526 | 1.92k | if (!rdp_decrypt(fastpath->rdp, s, length, flags)) |
1527 | 0 | return FALSE; |
1528 | 1.92k | } |
1529 | | |
1530 | 15.8k | return TRUE; |
1531 | 15.8k | } |