FreeRDP
Loading...
Searching...
No Matches
cliprdr_common.c
1
23#include <winpr/crt.h>
24#include <winpr/stream.h>
25#include <freerdp/channels/log.h>
26
27#define TAG CHANNELS_TAG("cliprdr.common")
28
29#include "cliprdr_common.h"
30
31static const char* CB_MSG_TYPE_STR(UINT32 type)
32{
33 switch (type)
34 {
35 case CB_TYPE_NONE:
36 return "CB_TYPE_NONE";
37 case CB_MONITOR_READY:
38 return "CB_MONITOR_READY";
39 case CB_FORMAT_LIST:
40 return "CB_FORMAT_LIST";
41 case CB_FORMAT_LIST_RESPONSE:
42 return "CB_FORMAT_LIST_RESPONSE";
43 case CB_FORMAT_DATA_REQUEST:
44 return "CB_FORMAT_DATA_REQUEST";
45 case CB_FORMAT_DATA_RESPONSE:
46 return "CB_FORMAT_DATA_RESPONSE";
47 case CB_TEMP_DIRECTORY:
48 return "CB_TEMP_DIRECTORY";
49 case CB_CLIP_CAPS:
50 return "CB_CLIP_CAPS";
51 case CB_FILECONTENTS_REQUEST:
52 return "CB_FILECONTENTS_REQUEST";
53 case CB_FILECONTENTS_RESPONSE:
54 return "CB_FILECONTENTS_RESPONSE";
55 case CB_LOCK_CLIPDATA:
56 return "CB_LOCK_CLIPDATA";
57 case CB_UNLOCK_CLIPDATA:
58 return "CB_UNLOCK_CLIPDATA";
59 default:
60 return "UNKNOWN";
61 }
62}
63
64const char* CB_MSG_TYPE_STRING(UINT16 type, char* buffer, size_t size)
65{
66 (void)_snprintf(buffer, size, "%s [0x%04" PRIx16 "]", CB_MSG_TYPE_STR(type), type);
67 return buffer;
68}
69
70const char* CB_MSG_FLAGS_STRING(UINT16 msgFlags, char* buffer, size_t size)
71{
72 if ((msgFlags & CB_RESPONSE_OK) != 0)
73 winpr_str_append("CB_RESPONSE_OK", buffer, size, "|");
74 if ((msgFlags & CB_RESPONSE_FAIL) != 0)
75 winpr_str_append("CB_RESPONSE_FAIL", buffer, size, "|");
76 if ((msgFlags & CB_ASCII_NAMES) != 0)
77 winpr_str_append("CB_ASCII_NAMES", buffer, size, "|");
78
79 const size_t len = strnlen(buffer, size);
80 if (!len)
81 winpr_str_append("NONE", buffer, size, "");
82
83 char val[32] = WINPR_C_ARRAY_INIT;
84 (void)_snprintf(val, sizeof(val), "[0x%04" PRIx16 "]", msgFlags);
85 winpr_str_append(val, buffer, size, "|");
86 return buffer;
87}
88
89static BOOL cliprdr_validate_file_contents_request(const CLIPRDR_FILE_CONTENTS_REQUEST* request)
90{
91 /*
92 * [MS-RDPECLIP] 2.2.5.3 File Contents Request PDU (CLIPRDR_FILECONTENTS_REQUEST).
93 *
94 * A request for the size of the file identified by the lindex field. The size MUST be
95 * returned as a 64-bit, unsigned integer. The cbRequested field MUST be set to
96 * 0x00000008 and both the nPositionLow and nPositionHigh fields MUST be
97 * set to 0x00000000.
98 */
99
100 if (request->dwFlags & FILECONTENTS_SIZE)
101 {
102 if (request->cbRequested != sizeof(UINT64))
103 {
104 WLog_ERR(TAG, "cbRequested must be %" PRIuz ", got %" PRIu32 "", sizeof(UINT64),
105 request->cbRequested);
106 return FALSE;
107 }
108
109 if (request->nPositionHigh != 0 || request->nPositionLow != 0)
110 {
111 WLog_ERR(TAG, "nPositionHigh and nPositionLow must be set to 0");
112 return FALSE;
113 }
114 }
115
116 return TRUE;
117}
118
119wStream* cliprdr_packet_new(UINT16 msgType, UINT16 msgFlags, size_t dataLen)
120{
121 WINPR_ASSERT(dataLen < UINT32_MAX - 8ull);
122 wStream* s = Stream_New(nullptr, dataLen + 8ULL);
123
124 if (!s)
125 {
126 WLog_ERR(TAG, "Stream_New failed!");
127 return nullptr;
128 }
129
130 Stream_Write_UINT16(s, msgType);
131 Stream_Write_UINT16(s, msgFlags);
132 /* Write actual length after the entire packet has been constructed. */
133 Stream_Write_UINT32(s, 0);
134 return s;
135}
136
137static void cliprdr_write_file_contents_request(wStream* s,
138 const CLIPRDR_FILE_CONTENTS_REQUEST* request)
139{
140 Stream_Write_UINT32(s, request->streamId); /* streamId (4 bytes) */
141 Stream_Write_UINT32(s, request->listIndex); /* listIndex (4 bytes) */
142 Stream_Write_UINT32(s, request->dwFlags); /* dwFlags (4 bytes) */
143 Stream_Write_UINT32(s, request->nPositionLow); /* nPositionLow (4 bytes) */
144 Stream_Write_UINT32(s, request->nPositionHigh); /* nPositionHigh (4 bytes) */
145 Stream_Write_UINT32(s, request->cbRequested); /* cbRequested (4 bytes) */
146
147 if (request->haveClipDataId)
148 Stream_Write_UINT32(s, request->clipDataId); /* clipDataId (4 bytes) */
149}
150
151static inline void cliprdr_write_lock_unlock_clipdata(wStream* s, UINT32 clipDataId)
152{
153 Stream_Write_UINT32(s, clipDataId);
154}
155
156static void cliprdr_write_lock_clipdata(wStream* s,
157 const CLIPRDR_LOCK_CLIPBOARD_DATA* lockClipboardData)
158{
159 cliprdr_write_lock_unlock_clipdata(s, lockClipboardData->clipDataId);
160}
161
162static void cliprdr_write_unlock_clipdata(wStream* s,
163 const CLIPRDR_UNLOCK_CLIPBOARD_DATA* unlockClipboardData)
164{
165 cliprdr_write_lock_unlock_clipdata(s, unlockClipboardData->clipDataId);
166}
167
168static void cliprdr_write_file_contents_response(wStream* s,
169 const CLIPRDR_FILE_CONTENTS_RESPONSE* response)
170{
171 Stream_Write_UINT32(s, response->streamId); /* streamId (4 bytes) */
172 Stream_Write(s, response->requestedData, response->cbRequested);
173}
174
175wStream* cliprdr_packet_lock_clipdata_new(const CLIPRDR_LOCK_CLIPBOARD_DATA* lockClipboardData)
176{
177 wStream* s = nullptr;
178
179 if (!lockClipboardData)
180 return nullptr;
181
182 s = cliprdr_packet_new(CB_LOCK_CLIPDATA, 0, 4);
183
184 if (!s)
185 return nullptr;
186
187 cliprdr_write_lock_clipdata(s, lockClipboardData);
188 return s;
189}
190
191wStream*
192cliprdr_packet_unlock_clipdata_new(const CLIPRDR_UNLOCK_CLIPBOARD_DATA* unlockClipboardData)
193{
194 wStream* s = nullptr;
195
196 if (!unlockClipboardData)
197 return nullptr;
198
199 s = cliprdr_packet_new(CB_UNLOCK_CLIPDATA, 0, 4);
200
201 if (!s)
202 return nullptr;
203
204 cliprdr_write_unlock_clipdata(s, unlockClipboardData);
205 return s;
206}
207
208wStream* cliprdr_packet_file_contents_request_new(const CLIPRDR_FILE_CONTENTS_REQUEST* request)
209{
210 wStream* s = nullptr;
211
212 if (!request)
213 return nullptr;
214
215 s = cliprdr_packet_new(CB_FILECONTENTS_REQUEST, 0, 28);
216
217 if (!s)
218 return nullptr;
219
220 cliprdr_write_file_contents_request(s, request);
221 return s;
222}
223
224wStream* cliprdr_packet_file_contents_response_new(const CLIPRDR_FILE_CONTENTS_RESPONSE* response)
225{
226 if (!response || (response->cbRequested > UINT32_MAX - 4))
227 return nullptr;
228
229 wStream* s = cliprdr_packet_new(CB_FILECONTENTS_RESPONSE, response->common.msgFlags,
230 4ull + response->cbRequested);
231
232 if (!s)
233 return nullptr;
234
235 cliprdr_write_file_contents_response(s, response);
236 return s;
237}
238
239wStream* cliprdr_packet_format_list_new(const CLIPRDR_FORMAT_LIST* formatList,
240 BOOL useLongFormatNames, BOOL useAsciiNames)
241{
242 WINPR_ASSERT(formatList);
243
244 if (formatList->common.msgType != CB_FORMAT_LIST)
245 WLog_WARN(TAG, "called with invalid type %08" PRIx32, formatList->common.msgType);
246
247 if (useLongFormatNames && useAsciiNames)
248 WLog_WARN(TAG, "called with invalid arguments useLongFormatNames=true && "
249 "useAsciiNames=true. useAsciiNames requires "
250 "useLongFormatNames=false, ignoring argument.");
251
252 if (formatList->numFormats > UINT32_MAX / 36ull)
253 {
254 WLog_ERR(TAG, "called with numFormats=%" PRIu32 ", which exceeds maximum allowed %" PRIu32,
255 formatList->numFormats, UINT32_MAX);
256 return nullptr;
257 }
258
259 const UINT32 length = formatList->numFormats * 36ull;
260 const size_t formatNameCharSize =
261 (useLongFormatNames || !useAsciiNames) ? sizeof(WCHAR) : sizeof(CHAR);
262
263 wStream* s = cliprdr_packet_new(CB_FORMAT_LIST, 0, length);
264 if (!s)
265 {
266 WLog_ERR(TAG, "cliprdr_packet_new failed!");
267 return nullptr;
268 }
269
270 for (UINT32 index = 0; index < formatList->numFormats; index++)
271 {
272 const CLIPRDR_FORMAT* format = &(formatList->formats[index]);
273
274 const char* szFormatName = format->formatName;
275 size_t formatNameLength = 0;
276 size_t formatNameStrLength = 0;
277 if (szFormatName)
278 {
279 formatNameStrLength = strlen(szFormatName);
280 const SSIZE_T wlen = ConvertUtf8ToWChar(szFormatName, nullptr, 0);
281 if (wlen < 0)
282 goto fail;
283 formatNameLength = WINPR_ASSERTING_INT_CAST(size_t, wlen);
284 }
285
286 size_t formatNameMaxLength = formatNameLength + 1; /* Ensure '\0' termination in output */
287 if (!Stream_EnsureRemainingCapacity(s,
288 4 + MAX(32, formatNameMaxLength * formatNameCharSize)))
289 goto fail;
290
291 Stream_Write_UINT32(s, format->formatId); /* formatId (4 bytes) */
292
293 if (!useLongFormatNames)
294 {
295 formatNameMaxLength = useAsciiNames ? 32 : 16;
296 formatNameLength = MIN(formatNameMaxLength - 1, formatNameLength);
297 }
298
299 if (szFormatName && (formatNameLength > 0))
300 {
301 if (useAsciiNames)
302 {
303 Stream_Write(s, szFormatName, formatNameLength);
304 Stream_Zero(s, formatNameMaxLength - formatNameLength);
305 }
306 else
307 {
308 const size_t formatNameWriteLength =
309 MIN(formatNameStrLength, formatNameMaxLength - 1);
310 if (Stream_Write_UTF16_String_From_UTF8(s, formatNameMaxLength - 1, szFormatName,
311 formatNameWriteLength, TRUE) < 0)
312 goto fail;
313 Stream_Write_UINT16(s, 0);
314 }
315 }
316 else
317 Stream_Zero(s, formatNameMaxLength * formatNameCharSize);
318 }
319
320 return s;
321
322fail:
323 Stream_Free(s, TRUE);
324 return nullptr;
325}
326
327UINT cliprdr_read_unlock_clipdata(wStream* s, CLIPRDR_UNLOCK_CLIPBOARD_DATA* unlockClipboardData)
328{
329 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
330 return ERROR_INVALID_DATA;
331
332 Stream_Read_UINT32(s, unlockClipboardData->clipDataId); /* clipDataId (4 bytes) */
333 return CHANNEL_RC_OK;
334}
335
336UINT cliprdr_read_format_data_request(wStream* s, CLIPRDR_FORMAT_DATA_REQUEST* request)
337{
338 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
339 return ERROR_INVALID_DATA;
340
341 Stream_Read_UINT32(s, request->requestedFormatId); /* requestedFormatId (4 bytes) */
342 return CHANNEL_RC_OK;
343}
344
345UINT cliprdr_read_format_data_response(wStream* s, CLIPRDR_FORMAT_DATA_RESPONSE* response)
346{
347 response->requestedFormatData = nullptr;
348
349 if (!Stream_CheckAndLogRequiredLength(TAG, s, response->common.dataLen))
350 return ERROR_INVALID_DATA;
351
352 if (response->common.dataLen > 0)
353 {
354 response->requestedFormatData = Stream_ConstPointer(s);
355 if (!Stream_SafeSeek(s, response->common.dataLen))
356 return ERROR_INVALID_DATA;
357 }
358 return CHANNEL_RC_OK;
359}
360
361UINT cliprdr_read_file_contents_request(wStream* s, CLIPRDR_FILE_CONTENTS_REQUEST* request)
362{
363 if (!Stream_CheckAndLogRequiredLength(TAG, s, 24))
364 return ERROR_INVALID_DATA;
365
366 request->haveClipDataId = FALSE;
367 Stream_Read_UINT32(s, request->streamId); /* streamId (4 bytes) */
368 Stream_Read_UINT32(s, request->listIndex); /* listIndex (4 bytes) */
369 Stream_Read_UINT32(s, request->dwFlags); /* dwFlags (4 bytes) */
370 Stream_Read_UINT32(s, request->nPositionLow); /* nPositionLow (4 bytes) */
371 Stream_Read_UINT32(s, request->nPositionHigh); /* nPositionHigh (4 bytes) */
372 Stream_Read_UINT32(s, request->cbRequested); /* cbRequested (4 bytes) */
373
374 if (Stream_GetRemainingLength(s) >= 4)
375 {
376 Stream_Read_UINT32(s, request->clipDataId); /* clipDataId (4 bytes) */
377 request->haveClipDataId = TRUE;
378 }
379
380 if (!cliprdr_validate_file_contents_request(request))
381 return ERROR_BAD_ARGUMENTS;
382
383 return CHANNEL_RC_OK;
384}
385
386UINT cliprdr_read_file_contents_response(wStream* s, CLIPRDR_FILE_CONTENTS_RESPONSE* response)
387{
388 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
389 return ERROR_INVALID_DATA;
390
391 Stream_Read_UINT32(s, response->streamId); /* streamId (4 bytes) */
392 response->requestedData = Stream_ConstPointer(s); /* requestedFileContentsData */
393
394 if (response->common.dataLen < 4)
395 {
396 WLog_WARN(TAG, "dataLen=%" PRIu32 " but expected >= 4", response->common.dataLen);
397 return ERROR_INVALID_DATA;
398 }
399
400 response->cbRequested = response->common.dataLen - 4;
401 if (!Stream_CheckAndLogRequiredLength(TAG, s, response->cbRequested))
402 return ERROR_INVALID_DATA;
403 Stream_Seek(s, response->cbRequested);
404 return CHANNEL_RC_OK;
405}
406
407UINT cliprdr_read_format_list(wLog* log, wStream* s, CLIPRDR_FORMAT_LIST* formatList,
408 BOOL useLongFormatNames)
409{
410 UINT32 index = 0;
411 size_t formatNameLength = 0;
412 const char* szFormatName = nullptr;
413 const WCHAR* wszFormatName = nullptr;
414 wStream sub1buffer = WINPR_C_ARRAY_INIT;
415 CLIPRDR_FORMAT* formats = nullptr;
416 UINT error = ERROR_INTERNAL_ERROR;
417
418 const BOOL asciiNames = (formatList->common.msgFlags & CB_ASCII_NAMES) != 0;
419
420 index = 0;
421 /* empty format list */
422 formatList->formats = nullptr;
423 formatList->numFormats = 0;
424
425 wStream* sub1 =
426 Stream_StaticConstInit(&sub1buffer, Stream_ConstPointer(s), formatList->common.dataLen);
427 if (!Stream_SafeSeek(s, formatList->common.dataLen))
428 return ERROR_INVALID_DATA;
429
430 if (!formatList->common.dataLen)
431 {
432 }
433 else if (!useLongFormatNames)
434 {
435 const size_t cap = Stream_Capacity(sub1) / 36ULL;
436 if (cap > UINT32_MAX)
437 {
438 WLog_Print(log, WLOG_ERROR, "Invalid short format list length: %" PRIuz "", cap);
439 return ERROR_INTERNAL_ERROR;
440 }
441 formatList->numFormats = (UINT32)cap;
442
443 if (formatList->numFormats)
444 formats = (CLIPRDR_FORMAT*)calloc(formatList->numFormats, sizeof(CLIPRDR_FORMAT));
445
446 if (!formats)
447 {
448 WLog_Print(log, WLOG_ERROR, "calloc failed!");
449 return CHANNEL_RC_NO_MEMORY;
450 }
451
452 formatList->formats = formats;
453
454 while (Stream_GetRemainingLength(sub1) >= 4)
455 {
456 if (index >= formatList->numFormats)
457 goto error_out;
458
459 CLIPRDR_FORMAT* format = &formats[index];
460
461 Stream_Read_UINT32(sub1, format->formatId); /* formatId (4 bytes) */
462
463 /* According to MS-RDPECLIP 2.2.3.1.1.1 formatName is "a 32-byte block containing
464 * the *null-terminated* name assigned to the Clipboard Format: (32 ASCII 8 characters
465 * or 16 Unicode characters)"
466 * However, both Windows RDSH and mstsc violate this specs as seen in the following
467 * example of a transferred short format name string: [R.i.c.h. .T.e.x.t. .F.o.r.m.a.t.]
468 * These are 16 unicode characters - *without* terminating null !
469 */
470
471 szFormatName = Stream_ConstPointer(sub1);
472 wszFormatName = Stream_ConstPointer(sub1);
473 if (!Stream_SafeSeek(sub1, 32))
474 goto error_out;
475
476 free(format->formatName);
477 format->formatName = nullptr;
478
479 if (asciiNames)
480 {
481 if (szFormatName[0])
482 {
483 /* ensure null termination */
484 format->formatName = strndup(szFormatName, 31);
485 if (!format->formatName)
486 {
487 WLog_Print(log, WLOG_ERROR, "malloc failed!");
488 error = CHANNEL_RC_NO_MEMORY;
489 goto error_out;
490 }
491 }
492 }
493 else
494 {
495 if (wszFormatName[0])
496 {
497 format->formatName = ConvertWCharNToUtf8Alloc(wszFormatName, 16, nullptr);
498 if (!format->formatName)
499 goto error_out;
500 }
501 }
502
503 index++;
504 }
505 }
506 else
507 {
508 wStream sub2buffer = sub1buffer;
509 wStream* sub2 = &sub2buffer;
510
511 /* Bound must match the read pass below: some clients pad the format list with
512 * trailing bytes that do not start another entry. */
513 while (Stream_GetRemainingLength(sub1) >= 4)
514 {
515 size_t rest = 0;
516 if (!Stream_SafeSeek(sub1, 4)) /* formatId (4 bytes) */
517 goto error_out;
518
519 wszFormatName = Stream_ConstPointer(sub1);
520 rest = Stream_GetRemainingLength(sub1);
521 formatNameLength = _wcsnlen(wszFormatName, rest / sizeof(WCHAR));
522
523 if (!Stream_SafeSeek(sub1, (formatNameLength + 1) * sizeof(WCHAR)))
524 goto error_out;
525 formatList->numFormats++;
526 }
527
528 if (formatList->numFormats)
529 formats = (CLIPRDR_FORMAT*)calloc(formatList->numFormats, sizeof(CLIPRDR_FORMAT));
530
531 if (!formats)
532 {
533 WLog_Print(log, WLOG_ERROR, "calloc failed!");
534 return CHANNEL_RC_NO_MEMORY;
535 }
536
537 formatList->formats = formats;
538
539 while (Stream_GetRemainingLength(sub2) >= 4)
540 {
541 if (index >= formatList->numFormats)
542 goto error_out;
543
544 size_t rest = 0;
545 CLIPRDR_FORMAT* format = &formats[index];
546
547 Stream_Read_UINT32(sub2, format->formatId); /* formatId (4 bytes) */
548
549 free(format->formatName);
550 format->formatName = nullptr;
551
552 wszFormatName = Stream_ConstPointer(sub2);
553 rest = Stream_GetRemainingLength(sub2);
554 formatNameLength = _wcsnlen(wszFormatName, rest / sizeof(WCHAR));
555 if (!Stream_SafeSeek(sub2, (formatNameLength + 1) * sizeof(WCHAR)))
556 goto error_out;
557
558 if (formatNameLength)
559 {
560 format->formatName =
561 ConvertWCharNToUtf8Alloc(wszFormatName, formatNameLength, nullptr);
562 if (!format->formatName)
563 goto error_out;
564 }
565
566 index++;
567 }
568 }
569
570 return CHANNEL_RC_OK;
571
572error_out:
573 cliprdr_free_format_list(formatList);
574 return error;
575}
576
577void cliprdr_free_format_list(CLIPRDR_FORMAT_LIST* formatList)
578{
579 if (formatList == nullptr)
580 return;
581
582 if (formatList->formats)
583 {
584 for (UINT32 index = 0; index < formatList->numFormats; index++)
585 {
586 free(formatList->formats[index].formatName);
587 }
588
589 free(formatList->formats);
590 formatList->formats = nullptr;
591 formatList->numFormats = 0;
592 }
593}