21 #include <winpr/wlog.h>
22 #include <winpr/print.h>
23 #include <winpr/smartcard.h>
25 #include <freerdp/utils/rdpdr_utils.h>
26 #include <freerdp/channels/scard.h>
27 #include <freerdp/channels/rdpdr.h>
29 #include <freerdp/log.h>
31 LONG scard_log_status_error(
const char* tag,
const char* what, LONG status)
33 if (status != SCARD_S_SUCCESS)
35 DWORD level = WLOG_ERROR;
41 case SCARD_E_NO_READERS_AVAILABLE:
47 WLog_Print(WLog_Get(tag), level,
"%s failed with error %s [%" PRId32
"]", what,
48 SCardGetErrorString(status), status);
53 const char* scard_get_ioctl_string(UINT32 ioControlCode, BOOL funcName)
55 switch (ioControlCode)
57 case SCARD_IOCTL_ESTABLISHCONTEXT:
58 return funcName ?
"SCardEstablishContext" :
"SCARD_IOCTL_ESTABLISHCONTEXT";
60 case SCARD_IOCTL_RELEASECONTEXT:
61 return funcName ?
"SCardReleaseContext" :
"SCARD_IOCTL_RELEASECONTEXT";
63 case SCARD_IOCTL_ISVALIDCONTEXT:
64 return funcName ?
"SCardIsValidContext" :
"SCARD_IOCTL_ISVALIDCONTEXT";
66 case SCARD_IOCTL_LISTREADERGROUPSA:
67 return funcName ?
"SCardListReaderGroupsA" :
"SCARD_IOCTL_LISTREADERGROUPSA";
69 case SCARD_IOCTL_LISTREADERGROUPSW:
70 return funcName ?
"SCardListReaderGroupsW" :
"SCARD_IOCTL_LISTREADERGROUPSW";
72 case SCARD_IOCTL_LISTREADERSA:
73 return funcName ?
"SCardListReadersA" :
"SCARD_IOCTL_LISTREADERSA";
75 case SCARD_IOCTL_LISTREADERSW:
76 return funcName ?
"SCardListReadersW" :
"SCARD_IOCTL_LISTREADERSW";
78 case SCARD_IOCTL_INTRODUCEREADERGROUPA:
79 return funcName ?
"SCardIntroduceReaderGroupA" :
"SCARD_IOCTL_INTRODUCEREADERGROUPA";
81 case SCARD_IOCTL_INTRODUCEREADERGROUPW:
82 return funcName ?
"SCardIntroduceReaderGroupW" :
"SCARD_IOCTL_INTRODUCEREADERGROUPW";
84 case SCARD_IOCTL_FORGETREADERGROUPA:
85 return funcName ?
"SCardForgetReaderGroupA" :
"SCARD_IOCTL_FORGETREADERGROUPA";
87 case SCARD_IOCTL_FORGETREADERGROUPW:
88 return funcName ?
"SCardForgetReaderGroupW" :
"SCARD_IOCTL_FORGETREADERGROUPW";
90 case SCARD_IOCTL_INTRODUCEREADERA:
91 return funcName ?
"SCardIntroduceReaderA" :
"SCARD_IOCTL_INTRODUCEREADERA";
93 case SCARD_IOCTL_INTRODUCEREADERW:
94 return funcName ?
"SCardIntroduceReaderW" :
"SCARD_IOCTL_INTRODUCEREADERW";
96 case SCARD_IOCTL_FORGETREADERA:
97 return funcName ?
"SCardForgetReaderA" :
"SCARD_IOCTL_FORGETREADERA";
99 case SCARD_IOCTL_FORGETREADERW:
100 return funcName ?
"SCardForgetReaderW" :
"SCARD_IOCTL_FORGETREADERW";
102 case SCARD_IOCTL_ADDREADERTOGROUPA:
103 return funcName ?
"SCardAddReaderToGroupA" :
"SCARD_IOCTL_ADDREADERTOGROUPA";
105 case SCARD_IOCTL_ADDREADERTOGROUPW:
106 return funcName ?
"SCardAddReaderToGroupW" :
"SCARD_IOCTL_ADDREADERTOGROUPW";
108 case SCARD_IOCTL_REMOVEREADERFROMGROUPA:
109 return funcName ?
"SCardRemoveReaderFromGroupA" :
"SCARD_IOCTL_REMOVEREADERFROMGROUPA";
111 case SCARD_IOCTL_REMOVEREADERFROMGROUPW:
112 return funcName ?
"SCardRemoveReaderFromGroupW" :
"SCARD_IOCTL_REMOVEREADERFROMGROUPW";
114 case SCARD_IOCTL_LOCATECARDSA:
115 return funcName ?
"SCardLocateCardsA" :
"SCARD_IOCTL_LOCATECARDSA";
117 case SCARD_IOCTL_LOCATECARDSW:
118 return funcName ?
"SCardLocateCardsW" :
"SCARD_IOCTL_LOCATECARDSW";
120 case SCARD_IOCTL_GETSTATUSCHANGEA:
121 return funcName ?
"SCardGetStatusChangeA" :
"SCARD_IOCTL_GETSTATUSCHANGEA";
123 case SCARD_IOCTL_GETSTATUSCHANGEW:
124 return funcName ?
"SCardGetStatusChangeW" :
"SCARD_IOCTL_GETSTATUSCHANGEW";
126 case SCARD_IOCTL_CANCEL:
127 return funcName ?
"SCardCancel" :
"SCARD_IOCTL_CANCEL";
129 case SCARD_IOCTL_CONNECTA:
130 return funcName ?
"SCardConnectA" :
"SCARD_IOCTL_CONNECTA";
132 case SCARD_IOCTL_CONNECTW:
133 return funcName ?
"SCardConnectW" :
"SCARD_IOCTL_CONNECTW";
135 case SCARD_IOCTL_RECONNECT:
136 return funcName ?
"SCardReconnect" :
"SCARD_IOCTL_RECONNECT";
138 case SCARD_IOCTL_DISCONNECT:
139 return funcName ?
"SCardDisconnect" :
"SCARD_IOCTL_DISCONNECT";
141 case SCARD_IOCTL_BEGINTRANSACTION:
142 return funcName ?
"SCardBeginTransaction" :
"SCARD_IOCTL_BEGINTRANSACTION";
144 case SCARD_IOCTL_ENDTRANSACTION:
145 return funcName ?
"SCardEndTransaction" :
"SCARD_IOCTL_ENDTRANSACTION";
147 case SCARD_IOCTL_STATE:
148 return funcName ?
"SCardState" :
"SCARD_IOCTL_STATE";
150 case SCARD_IOCTL_STATUSA:
151 return funcName ?
"SCardStatusA" :
"SCARD_IOCTL_STATUSA";
153 case SCARD_IOCTL_STATUSW:
154 return funcName ?
"SCardStatusW" :
"SCARD_IOCTL_STATUSW";
156 case SCARD_IOCTL_TRANSMIT:
157 return funcName ?
"SCardTransmit" :
"SCARD_IOCTL_TRANSMIT";
159 case SCARD_IOCTL_CONTROL:
160 return funcName ?
"SCardControl" :
"SCARD_IOCTL_CONTROL";
162 case SCARD_IOCTL_GETATTRIB:
163 return funcName ?
"SCardGetAttrib" :
"SCARD_IOCTL_GETATTRIB";
165 case SCARD_IOCTL_SETATTRIB:
166 return funcName ?
"SCardSetAttrib" :
"SCARD_IOCTL_SETATTRIB";
168 case SCARD_IOCTL_ACCESSSTARTEDEVENT:
169 return funcName ?
"SCardAccessStartedEvent" :
"SCARD_IOCTL_ACCESSSTARTEDEVENT";
171 case SCARD_IOCTL_LOCATECARDSBYATRA:
172 return funcName ?
"SCardLocateCardsByATRA" :
"SCARD_IOCTL_LOCATECARDSBYATRA";
174 case SCARD_IOCTL_LOCATECARDSBYATRW:
175 return funcName ?
"SCardLocateCardsByATRB" :
"SCARD_IOCTL_LOCATECARDSBYATRW";
177 case SCARD_IOCTL_READCACHEA:
178 return funcName ?
"SCardReadCacheA" :
"SCARD_IOCTL_READCACHEA";
180 case SCARD_IOCTL_READCACHEW:
181 return funcName ?
"SCardReadCacheW" :
"SCARD_IOCTL_READCACHEW";
183 case SCARD_IOCTL_WRITECACHEA:
184 return funcName ?
"SCardWriteCacheA" :
"SCARD_IOCTL_WRITECACHEA";
186 case SCARD_IOCTL_WRITECACHEW:
187 return funcName ?
"SCardWriteCacheW" :
"SCARD_IOCTL_WRITECACHEW";
189 case SCARD_IOCTL_GETTRANSMITCOUNT:
190 return funcName ?
"SCardGetTransmitCount" :
"SCARD_IOCTL_GETTRANSMITCOUNT";
192 case SCARD_IOCTL_RELEASETARTEDEVENT:
193 return funcName ?
"SCardReleaseStartedEvent" :
"SCARD_IOCTL_RELEASETARTEDEVENT";
195 case SCARD_IOCTL_GETREADERICON:
196 return funcName ?
"SCardGetReaderIcon" :
"SCARD_IOCTL_GETREADERICON";
198 case SCARD_IOCTL_GETDEVICETYPEID:
199 return funcName ?
"SCardGetDeviceTypeId" :
"SCARD_IOCTL_GETDEVICETYPEID";
202 return funcName ?
"SCardUnknown" :
"SCARD_IOCTL_UNKNOWN";
206 const char* rdpdr_component_string(UINT16 component)
211 return "RDPDR_CTYP_PRN";
212 case RDPDR_CTYP_CORE:
213 return "RDPDR_CTYP_CORE";
219 const char* rdpdr_packetid_string(UINT16 packetid)
223 case PAKID_CORE_SERVER_ANNOUNCE:
224 return "PAKID_CORE_SERVER_ANNOUNCE";
225 case PAKID_CORE_CLIENTID_CONFIRM:
226 return "PAKID_CORE_CLIENTID_CONFIRM";
227 case PAKID_CORE_CLIENT_NAME:
228 return "PAKID_CORE_CLIENT_NAME";
229 case PAKID_CORE_DEVICELIST_ANNOUNCE:
230 return "PAKID_CORE_DEVICELIST_ANNOUNCE";
231 case PAKID_CORE_DEVICE_REPLY:
232 return "PAKID_CORE_DEVICE_REPLY";
233 case PAKID_CORE_DEVICE_IOREQUEST:
234 return "PAKID_CORE_DEVICE_IOREQUEST";
235 case PAKID_CORE_DEVICE_IOCOMPLETION:
236 return "PAKID_CORE_DEVICE_IOCOMPLETION";
237 case PAKID_CORE_SERVER_CAPABILITY:
238 return "PAKID_CORE_SERVER_CAPABILITY";
239 case PAKID_CORE_CLIENT_CAPABILITY:
240 return "PAKID_CORE_CLIENT_CAPABILITY";
241 case PAKID_CORE_DEVICELIST_REMOVE:
242 return "PAKID_CORE_DEVICELIST_REMOVE";
243 case PAKID_CORE_USER_LOGGEDON:
244 return "PAKID_CORE_USER_LOGGEDON";
245 case PAKID_PRN_CACHE_DATA:
246 return "PAKID_PRN_CACHE_DATA";
247 case PAKID_PRN_USING_XPS:
248 return "PAKID_PRN_USING_XPS";
254 BOOL rdpdr_write_iocompletion_header(
wStream* out, UINT32 DeviceId, UINT32 CompletionId,
258 Stream_SetPosition(out, 0);
259 if (!Stream_EnsureRemainingCapacity(out, 16))
261 Stream_Write_UINT16(out, RDPDR_CTYP_CORE);
262 Stream_Write_UINT16(out, PAKID_CORE_DEVICE_IOCOMPLETION);
263 Stream_Write_UINT32(out, DeviceId);
264 Stream_Write_UINT32(out, CompletionId);
265 Stream_Write_UINT32(out, ioStatus);
270 static void rdpdr_dump_packet(wLog* log, DWORD lvl,
wStream* s,
const char* custom, BOOL send)
272 if (!WLog_IsLevelActive(log, lvl))
275 const size_t gpos = Stream_GetPosition(s);
276 const size_t pos = send ? Stream_GetPosition(s) : Stream_Length(s);
278 UINT16 component = 0;
281 Stream_SetPosition(s, 0);
284 Stream_Read_UINT16(s, component);
286 Stream_Read_UINT16(s, packetid);
290 case PAKID_CORE_SERVER_ANNOUNCE:
291 case PAKID_CORE_CLIENTID_CONFIRM:
293 UINT16 versionMajor = 0;
294 UINT16 versionMinor = 0;
298 Stream_Read_UINT16(s, versionMajor);
300 Stream_Read_UINT16(s, versionMinor);
302 Stream_Read_UINT32(s, clientID);
304 "%s [%s | %s] [version:%" PRIu16
".%" PRIu16
"][id:0x%08" PRIx32
306 custom, rdpdr_component_string(component), rdpdr_packetid_string(packetid),
307 versionMajor, versionMinor, clientID, pos);
310 case PAKID_CORE_CLIENT_NAME:
312 char name[256] = { 0 };
313 UINT32 unicodeFlag = 0;
315 UINT32 computerNameLen = 0;
317 Stream_Read_UINT32(s, unicodeFlag);
319 Stream_Read_UINT32(s, codePage);
321 Stream_Read_UINT32(s, computerNameLen);
322 if (pos >= 16 + computerNameLen)
324 if (unicodeFlag == 0)
325 Stream_Read(s, name, MIN(
sizeof(name), computerNameLen));
327 (
void)ConvertWCharNToUtf8(Stream_ConstPointer(s),
328 computerNameLen /
sizeof(WCHAR), name,
sizeof(name));
331 "%s [%s | %s] [ucs:%" PRIu32
"|cp:%" PRIu32
"][len:0x%08" PRIx32
333 custom, rdpdr_component_string(component), rdpdr_packetid_string(packetid),
334 unicodeFlag, codePage, computerNameLen, name, pos);
338 case PAKID_CORE_DEVICE_IOREQUEST:
340 UINT32 CompletionId = 0;
343 UINT32 MajorFunction = 0;
344 UINT32 MinorFunction = 0;
347 Stream_Read_UINT32(s, deviceID);
349 Stream_Read_UINT32(s, FileId);
351 Stream_Read_UINT32(s, CompletionId);
353 Stream_Read_UINT32(s, MajorFunction);
355 Stream_Read_UINT32(s, MinorFunction);
357 "%s [%s | %s] [0x%08" PRIx32
"] FileId=0x%08" PRIx32
358 ", CompletionId=0x%08" PRIx32
", MajorFunction=0x%08" PRIx32
359 ", MinorFunction=0x%08" PRIx32
" -> %" PRIuz,
360 custom, rdpdr_component_string(component), rdpdr_packetid_string(packetid),
361 deviceID, FileId, CompletionId, MajorFunction, MinorFunction, pos);
364 case PAKID_CORE_DEVICE_IOCOMPLETION:
366 UINT32 completionID = 0;
370 Stream_Read_UINT32(s, deviceID);
372 Stream_Read_UINT32(s, completionID);
374 Stream_Read_UINT32(s, ioStatus);
377 "%s [%s | %s] [0x%08" PRIx32
"] completionID=0x%08" PRIx32
378 ", ioStatus=0x%08" PRIx32
" -> %" PRIuz,
379 custom, rdpdr_component_string(component), rdpdr_packetid_string(packetid),
380 deviceID, completionID, ioStatus, pos);
383 case PAKID_CORE_DEVICE_REPLY:
389 Stream_Read_UINT32(s, deviceID);
391 Stream_Read_UINT32(s, status);
393 "%s [%s | %s] [id:0x%08" PRIx32
",status=0x%08" PRIx32
"] -> %" PRIuz,
394 custom, rdpdr_component_string(component), rdpdr_packetid_string(packetid),
395 deviceID, status, pos);
398 case PAKID_CORE_CLIENT_CAPABILITY:
399 case PAKID_CORE_SERVER_CAPABILITY:
401 UINT16 numCapabilities = 0;
403 Stream_Read_UINT16(s, numCapabilities);
405 Stream_Seek_UINT16(s);
406 WLog_Print(log, lvl,
"%s [%s | %s] [caps:%" PRIu16
"] -> %" PRIuz, custom,
407 rdpdr_component_string(component), rdpdr_packetid_string(packetid),
408 numCapabilities, pos);
409 for (UINT16 x = 0; x < numCapabilities; x++)
412 const UINT error = rdpdr_read_capset_header(log, s, &header);
413 if (error == CHANNEL_RC_OK)
414 Stream_Seek(s, header.CapabilityLength);
418 case PAKID_CORE_DEVICELIST_ANNOUNCE:
424 Stream_Read_UINT32(s, count);
426 WLog_Print(log, lvl,
"%s [%s | %s] [%" PRIu32
"] -> %" PRIuz, custom,
427 rdpdr_component_string(component), rdpdr_packetid_string(packetid), count,
430 for (UINT32 x = 0; x < count; x++)
437 Stream_Read_UINT32(s, device.DeviceType);
438 Stream_Read_UINT32(s, device.DeviceId);
439 Stream_Read(s, device.PreferredDosName, 8);
440 Stream_Read_UINT32(s, device.DeviceDataLength);
441 device.DeviceData = Stream_Pointer(s);
443 offset += device.DeviceDataLength;
446 "%s [announce][%" PRIu32
"] %s [0x%08" PRIx32
447 "] '%s' [DeviceDataLength=%" PRIu32
"]",
449 device.PreferredDosName, device.DeviceDataLength);
453 case PAKID_CORE_DEVICELIST_REMOVE:
459 Stream_Read_UINT32(s, count);
461 WLog_Print(log, lvl,
"%s [%s | %s] [%" PRIu32
"] -> %" PRIuz, custom,
462 rdpdr_component_string(component), rdpdr_packetid_string(packetid), count,
465 for (UINT32 x = 0; x < count; x++)
471 Stream_Read_UINT32(s,
id);
473 WLog_Print(log, lvl,
"%s [remove][%" PRIu32
"] id=%" PRIu32, custom, x,
id);
477 case PAKID_CORE_USER_LOGGEDON:
478 WLog_Print(log, lvl,
"%s [%s | %s] -> %" PRIuz, custom,
479 rdpdr_component_string(component), rdpdr_packetid_string(packetid), pos);
483 WLog_Print(log, lvl,
"%s [%s | %s] -> %" PRIuz, custom,
484 rdpdr_component_string(component), rdpdr_packetid_string(packetid), pos);
490 Stream_SetPosition(s, gpos);
493 void rdpdr_dump_received_packet(wLog* log, DWORD lvl,
wStream* s,
const char* custom)
495 rdpdr_dump_packet(log, lvl, s, custom, FALSE);
498 void rdpdr_dump_send_packet(wLog* log, DWORD lvl,
wStream* s,
const char* custom)
500 rdpdr_dump_packet(log, lvl, s, custom, TRUE);
503 const char* rdpdr_irp_string(UINT32 major)
508 return "IRP_MJ_CREATE";
510 return "IRP_MJ_CLOSE";
512 return "IRP_MJ_READ";
514 return "IRP_MJ_WRITE";
515 case IRP_MJ_DEVICE_CONTROL:
516 return "IRP_MJ_DEVICE_CONTROL";
517 case IRP_MJ_QUERY_VOLUME_INFORMATION:
518 return "IRP_MJ_QUERY_VOLUME_INFORMATION";
519 case IRP_MJ_SET_VOLUME_INFORMATION:
520 return "IRP_MJ_SET_VOLUME_INFORMATION";
521 case IRP_MJ_QUERY_INFORMATION:
522 return "IRP_MJ_QUERY_INFORMATION";
523 case IRP_MJ_SET_INFORMATION:
524 return "IRP_MJ_SET_INFORMATION";
525 case IRP_MJ_DIRECTORY_CONTROL:
526 return "IRP_MJ_DIRECTORY_CONTROL";
527 case IRP_MJ_LOCK_CONTROL:
528 return "IRP_MJ_LOCK_CONTROL";
530 return "IRP_UNKNOWN";
534 const char* rdpdr_cap_type_string(UINT16 capability)
538 case CAP_GENERAL_TYPE:
539 return "CAP_GENERAL_TYPE";
540 case CAP_PRINTER_TYPE:
541 return "CAP_PRINTER_TYPE";
543 return "CAP_PORT_TYPE";
545 return "CAP_DRIVE_TYPE";
546 case CAP_SMARTCARD_TYPE:
547 return "CAP_SMARTCARD_TYPE";
549 return "CAP_UNKNOWN";
555 WINPR_ASSERT(header);
556 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
557 return ERROR_INVALID_DATA;
559 Stream_Read_UINT16(s, header->CapabilityType);
560 Stream_Read_UINT16(s, header->CapabilityLength);
561 Stream_Read_UINT32(s, header->Version);
563 WLog_Print(log, WLOG_TRACE,
564 "capability %s [0x%04" PRIx16
"] got version %" PRIu32
", length %" PRIu16,
565 rdpdr_cap_type_string(header->CapabilityType), header->CapabilityType,
566 header->Version, header->CapabilityLength);
567 if (header->CapabilityLength < 8)
569 WLog_Print(log, WLOG_ERROR,
"capability %s got short length %" PRIu32,
570 rdpdr_cap_type_string(header->CapabilityType), header->CapabilityLength);
571 return ERROR_INVALID_DATA;
573 header->CapabilityLength -= 8;
574 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, header->CapabilityLength))
575 return ERROR_INVALID_DATA;
576 return CHANNEL_RC_OK;
581 WINPR_ASSERT(header);
582 WINPR_ASSERT(header->CapabilityLength >= 8);
584 if (!Stream_EnsureRemainingCapacity(s, header->CapabilityLength))
586 WLog_Print(log, WLOG_ERROR,
"not enough data in stream!");
587 return ERROR_INVALID_DATA;
590 WLog_Print(log, WLOG_TRACE,
"writing capability %s version %" PRIu32
", length %" PRIu16,
591 rdpdr_cap_type_string(header->CapabilityType), header->Version,
592 header->CapabilityLength);
593 Stream_Write_UINT16(s, header->CapabilityType);
594 Stream_Write_UINT16(s, header->CapabilityLength);
595 Stream_Write_UINT32(s, header->Version);
596 return CHANNEL_RC_OK;
FREERDP_API const char * freerdp_rdpdr_dtyp_string(UINT32 type)
Returns a string representation of RDPDR_DTYP_*.