FreeRDP
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 
31 static 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 
64 const 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 
70 const 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] = { 0 };
84  (void)_snprintf(val, sizeof(val), "[0x%04" PRIx16 "]", msgFlags);
85  winpr_str_append(val, buffer, size, "|");
86  return buffer;
87 }
88 
89 static 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 %" PRIu32 ", 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 
119 wStream* cliprdr_packet_new(UINT16 msgType, UINT16 msgFlags, UINT32 dataLen)
120 {
121  wStream* s = NULL;
122  s = Stream_New(NULL, dataLen + 8);
123 
124  if (!s)
125  {
126  WLog_ERR(TAG, "Stream_New failed!");
127  return NULL;
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 
137 static 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 
151 static INLINE void cliprdr_write_lock_unlock_clipdata(wStream* s, UINT32 clipDataId)
152 {
153  Stream_Write_UINT32(s, clipDataId);
154 }
155 
156 static 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 
162 static 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 
168 static 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 
175 wStream* cliprdr_packet_lock_clipdata_new(const CLIPRDR_LOCK_CLIPBOARD_DATA* lockClipboardData)
176 {
177  wStream* s = NULL;
178 
179  if (!lockClipboardData)
180  return NULL;
181 
182  s = cliprdr_packet_new(CB_LOCK_CLIPDATA, 0, 4);
183 
184  if (!s)
185  return NULL;
186 
187  cliprdr_write_lock_clipdata(s, lockClipboardData);
188  return s;
189 }
190 
191 wStream*
192 cliprdr_packet_unlock_clipdata_new(const CLIPRDR_UNLOCK_CLIPBOARD_DATA* unlockClipboardData)
193 {
194  wStream* s = NULL;
195 
196  if (!unlockClipboardData)
197  return NULL;
198 
199  s = cliprdr_packet_new(CB_UNLOCK_CLIPDATA, 0, 4);
200 
201  if (!s)
202  return NULL;
203 
204  cliprdr_write_unlock_clipdata(s, unlockClipboardData);
205  return s;
206 }
207 
208 wStream* cliprdr_packet_file_contents_request_new(const CLIPRDR_FILE_CONTENTS_REQUEST* request)
209 {
210  wStream* s = NULL;
211 
212  if (!request)
213  return NULL;
214 
215  s = cliprdr_packet_new(CB_FILECONTENTS_REQUEST, 0, 28);
216 
217  if (!s)
218  return NULL;
219 
220  cliprdr_write_file_contents_request(s, request);
221  return s;
222 }
223 
224 wStream* cliprdr_packet_file_contents_response_new(const CLIPRDR_FILE_CONTENTS_RESPONSE* response)
225 {
226  wStream* s = NULL;
227 
228  if (!response)
229  return NULL;
230 
231  s = cliprdr_packet_new(CB_FILECONTENTS_RESPONSE, response->common.msgFlags,
232  4 + response->cbRequested);
233 
234  if (!s)
235  return NULL;
236 
237  cliprdr_write_file_contents_response(s, response);
238  return s;
239 }
240 
241 wStream* cliprdr_packet_format_list_new(const CLIPRDR_FORMAT_LIST* formatList,
242  BOOL useLongFormatNames, BOOL useAsciiNames)
243 {
244  WINPR_ASSERT(formatList);
245 
246  if (formatList->common.msgType != CB_FORMAT_LIST)
247  WLog_WARN(TAG, "called with invalid type %08" PRIx32, formatList->common.msgType);
248 
249  if (useLongFormatNames && useAsciiNames)
250  WLog_WARN(TAG, "called with invalid arguments useLongFormatNames=true && "
251  "useAsciiNames=true. useAsciiNames requires "
252  "useLongFormatNames=false, ignoring argument.");
253 
254  const UINT32 length = formatList->numFormats * 36;
255  const size_t formatNameCharSize =
256  (useLongFormatNames || !useAsciiNames) ? sizeof(WCHAR) : sizeof(CHAR);
257 
258  wStream* s = cliprdr_packet_new(CB_FORMAT_LIST, 0, length);
259  if (!s)
260  {
261  WLog_ERR(TAG, "cliprdr_packet_new failed!");
262  return NULL;
263  }
264 
265  for (UINT32 index = 0; index < formatList->numFormats; index++)
266  {
267  const CLIPRDR_FORMAT* format = &(formatList->formats[index]);
268 
269  const char* szFormatName = format->formatName;
270  size_t formatNameLength = 0;
271  if (szFormatName)
272  formatNameLength = strlen(szFormatName);
273 
274  size_t formatNameMaxLength = formatNameLength + 1; /* Ensure '\0' termination in output */
275  if (!Stream_EnsureRemainingCapacity(s,
276  4 + MAX(32, formatNameMaxLength * formatNameCharSize)))
277  goto fail;
278 
279  Stream_Write_UINT32(s, format->formatId); /* formatId (4 bytes) */
280 
281  if (!useLongFormatNames)
282  {
283  formatNameMaxLength = useAsciiNames ? 32 : 16;
284  formatNameLength = MIN(formatNameMaxLength - 1, formatNameLength);
285  }
286 
287  if (szFormatName && (formatNameLength > 0))
288  {
289  if (useAsciiNames)
290  {
291  Stream_Write(s, szFormatName, formatNameLength);
292  Stream_Zero(s, formatNameMaxLength - formatNameLength);
293  }
294  else
295  {
296  if (Stream_Write_UTF16_String_From_UTF8(s, formatNameMaxLength, szFormatName,
297  formatNameLength, TRUE) < 0)
298  goto fail;
299  }
300  }
301  else
302  Stream_Zero(s, formatNameMaxLength * formatNameCharSize);
303  }
304 
305  return s;
306 
307 fail:
308  Stream_Free(s, TRUE);
309  return NULL;
310 }
311 
312 UINT cliprdr_read_unlock_clipdata(wStream* s, CLIPRDR_UNLOCK_CLIPBOARD_DATA* unlockClipboardData)
313 {
314  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
315  return ERROR_INVALID_DATA;
316 
317  Stream_Read_UINT32(s, unlockClipboardData->clipDataId); /* clipDataId (4 bytes) */
318  return CHANNEL_RC_OK;
319 }
320 
321 UINT cliprdr_read_format_data_request(wStream* s, CLIPRDR_FORMAT_DATA_REQUEST* request)
322 {
323  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
324  return ERROR_INVALID_DATA;
325 
326  Stream_Read_UINT32(s, request->requestedFormatId); /* requestedFormatId (4 bytes) */
327  return CHANNEL_RC_OK;
328 }
329 
330 UINT cliprdr_read_format_data_response(wStream* s, CLIPRDR_FORMAT_DATA_RESPONSE* response)
331 {
332  response->requestedFormatData = NULL;
333 
334  if (!Stream_CheckAndLogRequiredLength(TAG, s, response->common.dataLen))
335  return ERROR_INVALID_DATA;
336 
337  if (response->common.dataLen)
338  response->requestedFormatData = Stream_ConstPointer(s);
339 
340  return CHANNEL_RC_OK;
341 }
342 
343 UINT cliprdr_read_file_contents_request(wStream* s, CLIPRDR_FILE_CONTENTS_REQUEST* request)
344 {
345  if (!Stream_CheckAndLogRequiredLength(TAG, s, 24))
346  return ERROR_INVALID_DATA;
347 
348  request->haveClipDataId = FALSE;
349  Stream_Read_UINT32(s, request->streamId); /* streamId (4 bytes) */
350  Stream_Read_UINT32(s, request->listIndex); /* listIndex (4 bytes) */
351  Stream_Read_UINT32(s, request->dwFlags); /* dwFlags (4 bytes) */
352  Stream_Read_UINT32(s, request->nPositionLow); /* nPositionLow (4 bytes) */
353  Stream_Read_UINT32(s, request->nPositionHigh); /* nPositionHigh (4 bytes) */
354  Stream_Read_UINT32(s, request->cbRequested); /* cbRequested (4 bytes) */
355 
356  if (Stream_GetRemainingLength(s) >= 4)
357  {
358  Stream_Read_UINT32(s, request->clipDataId); /* clipDataId (4 bytes) */
359  request->haveClipDataId = TRUE;
360  }
361 
362  if (!cliprdr_validate_file_contents_request(request))
363  return ERROR_BAD_ARGUMENTS;
364 
365  return CHANNEL_RC_OK;
366 }
367 
368 UINT cliprdr_read_file_contents_response(wStream* s, CLIPRDR_FILE_CONTENTS_RESPONSE* response)
369 {
370  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
371  return ERROR_INVALID_DATA;
372 
373  Stream_Read_UINT32(s, response->streamId); /* streamId (4 bytes) */
374  response->requestedData = Stream_ConstPointer(s); /* requestedFileContentsData */
375 
376  WINPR_ASSERT(response->common.dataLen >= 4);
377  response->cbRequested = response->common.dataLen - 4;
378  return CHANNEL_RC_OK;
379 }
380 
381 UINT cliprdr_read_format_list(wStream* s, CLIPRDR_FORMAT_LIST* formatList, BOOL useLongFormatNames)
382 {
383  UINT32 index = 0;
384  size_t formatNameLength = 0;
385  const char* szFormatName = NULL;
386  const WCHAR* wszFormatName = NULL;
387  wStream sub1buffer = { 0 };
388  CLIPRDR_FORMAT* formats = NULL;
389  UINT error = ERROR_INTERNAL_ERROR;
390 
391  const BOOL asciiNames = (formatList->common.msgFlags & CB_ASCII_NAMES) ? TRUE : FALSE;
392 
393  index = 0;
394  /* empty format list */
395  formatList->formats = NULL;
396  formatList->numFormats = 0;
397 
398  wStream* sub1 =
399  Stream_StaticConstInit(&sub1buffer, Stream_ConstPointer(s), formatList->common.dataLen);
400  if (!Stream_SafeSeek(s, formatList->common.dataLen))
401  return ERROR_INVALID_DATA;
402 
403  if (!formatList->common.dataLen)
404  {
405  }
406  else if (!useLongFormatNames)
407  {
408  const size_t cap = Stream_Capacity(sub1) / 36ULL;
409  if (cap > UINT32_MAX)
410  {
411  WLog_ERR(TAG, "Invalid short format list length: %" PRIuz "", cap);
412  return ERROR_INTERNAL_ERROR;
413  }
414  formatList->numFormats = (UINT32)cap;
415 
416  if (formatList->numFormats)
417  formats = (CLIPRDR_FORMAT*)calloc(formatList->numFormats, sizeof(CLIPRDR_FORMAT));
418 
419  if (!formats)
420  {
421  WLog_ERR(TAG, "calloc failed!");
422  return CHANNEL_RC_NO_MEMORY;
423  }
424 
425  formatList->formats = formats;
426 
427  while (Stream_GetRemainingLength(sub1) >= 4)
428  {
429  CLIPRDR_FORMAT* format = &formats[index];
430 
431  Stream_Read_UINT32(sub1, format->formatId); /* formatId (4 bytes) */
432 
433  /* According to MS-RDPECLIP 2.2.3.1.1.1 formatName is "a 32-byte block containing
434  * the *null-terminated* name assigned to the Clipboard Format: (32 ASCII 8 characters
435  * or 16 Unicode characters)"
436  * However, both Windows RDSH and mstsc violate this specs as seen in the following
437  * example of a transferred short format name string: [R.i.c.h. .T.e.x.t. .F.o.r.m.a.t.]
438  * These are 16 unicode charaters - *without* terminating null !
439  */
440 
441  szFormatName = Stream_ConstPointer(sub1);
442  wszFormatName = Stream_ConstPointer(sub1);
443  if (!Stream_SafeSeek(sub1, 32))
444  goto error_out;
445 
446  free(format->formatName);
447  format->formatName = NULL;
448 
449  if (asciiNames)
450  {
451  if (szFormatName[0])
452  {
453  /* ensure null termination */
454  format->formatName = strndup(szFormatName, 31);
455  if (!format->formatName)
456  {
457  WLog_ERR(TAG, "malloc failed!");
458  error = CHANNEL_RC_NO_MEMORY;
459  goto error_out;
460  }
461  }
462  }
463  else
464  {
465  if (wszFormatName[0])
466  {
467  format->formatName = ConvertWCharNToUtf8Alloc(wszFormatName, 16, NULL);
468  if (!format->formatName)
469  goto error_out;
470  }
471  }
472 
473  index++;
474  }
475  }
476  else
477  {
478  wStream sub2buffer = sub1buffer;
479  wStream* sub2 = &sub2buffer;
480 
481  while (Stream_GetRemainingLength(sub1) > 0)
482  {
483  size_t rest = 0;
484  if (!Stream_SafeSeek(sub1, 4)) /* formatId (4 bytes) */
485  goto error_out;
486 
487  wszFormatName = Stream_ConstPointer(sub1);
488  rest = Stream_GetRemainingLength(sub1);
489  formatNameLength = _wcsnlen(wszFormatName, rest / sizeof(WCHAR));
490 
491  if (!Stream_SafeSeek(sub1, (formatNameLength + 1) * sizeof(WCHAR)))
492  goto error_out;
493  formatList->numFormats++;
494  }
495 
496  if (formatList->numFormats)
497  formats = (CLIPRDR_FORMAT*)calloc(formatList->numFormats, sizeof(CLIPRDR_FORMAT));
498 
499  if (!formats)
500  {
501  WLog_ERR(TAG, "calloc failed!");
502  return CHANNEL_RC_NO_MEMORY;
503  }
504 
505  formatList->formats = formats;
506 
507  while (Stream_GetRemainingLength(sub2) >= 4)
508  {
509  size_t rest = 0;
510  CLIPRDR_FORMAT* format = &formats[index];
511 
512  Stream_Read_UINT32(sub2, format->formatId); /* formatId (4 bytes) */
513 
514  free(format->formatName);
515  format->formatName = NULL;
516 
517  wszFormatName = Stream_ConstPointer(sub2);
518  rest = Stream_GetRemainingLength(sub2);
519  formatNameLength = _wcsnlen(wszFormatName, rest / sizeof(WCHAR));
520  if (!Stream_SafeSeek(sub2, (formatNameLength + 1) * sizeof(WCHAR)))
521  goto error_out;
522 
523  if (formatNameLength)
524  {
525  format->formatName =
526  ConvertWCharNToUtf8Alloc(wszFormatName, formatNameLength, NULL);
527  if (!format->formatName)
528  goto error_out;
529  }
530 
531  index++;
532  }
533  }
534 
535  return CHANNEL_RC_OK;
536 
537 error_out:
538  cliprdr_free_format_list(formatList);
539  return error;
540 }
541 
542 void cliprdr_free_format_list(CLIPRDR_FORMAT_LIST* formatList)
543 {
544  if (formatList == NULL)
545  return;
546 
547  if (formatList->formats)
548  {
549  for (UINT32 index = 0; index < formatList->numFormats; index++)
550  {
551  free(formatList->formats[index].formatName);
552  }
553 
554  free(formatList->formats);
555  formatList->formats = NULL;
556  formatList->numFormats = 0;
557  }
558 }