1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2019 piwi
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // PCSC functions to use alternative Smartcard Readers
9 //-----------------------------------------------------------------------------
18 #if defined (__APPLE__)
19 #include <PCSC/winscard.h>
20 #include <PCSC/wintypes.h>
21 #define SCARD_ATTR_VALUE(Class, Tag) ((((ULONG)(Class)) << 16) | ((ULONG)(Tag)))
22 #define SCARD_CLASS_ICC_STATE 9
23 #define SCARD_ATTR_ATR_STRING SCARD_ATTR_VALUE(SCARD_CLASS_ICC_STATE, 0x0303)
24 #elif defined (_WIN32)
35 #define PM3_SMARTCARD_DEFAULT_NAME "PM3 RDV40 Smartcard Slot"
37 static SCARDCONTEXT SC_Context
;
38 static SCARDHANDLE SC_Card
;
39 static DWORD SC_Protocol
;
40 static char* AlternativeSmartcardReader
= NULL
;
42 #define PCSC_MAX_TRACELEN 60000
43 static uint8_t pcsc_trace_buf
[PCSC_MAX_TRACELEN
];
44 static bool tracing
= false;
45 static uint32_t traceLen
= 0;
48 uint8_t *pcsc_get_trace_addr(void)
50 return pcsc_trace_buf
;
54 uint32_t pcsc_get_traceLen(void)
60 static void pcsc_clear_trace(void)
66 static void pcsc_set_tracing(bool enable
) {
71 static bool pcsc_LogTrace(const uint8_t *btBytes
, uint16_t iLen
, uint32_t timestamp_start
, uint32_t timestamp_end
, bool readerToTag
)
73 if (!tracing
) return false;
75 uint8_t *trace
= pcsc_trace_buf
;
77 uint32_t num_paritybytes
= (iLen
-1)/8 + 1; // number of paritybytes
78 uint32_t duration
= timestamp_end
- timestamp_start
;
80 // Return when trace is full
81 if (traceLen
+ sizeof(iLen
) + sizeof(timestamp_start
) + sizeof(duration
) + num_paritybytes
+ iLen
>= PCSC_MAX_TRACELEN
) {
82 tracing
= false; // don't trace any more
86 // 32 bits timestamp (little endian)
87 // 16 bits duration (little endian)
88 // 16 bits data length (little endian, Highest Bit used as readerToTag flag)
90 // x Bytes parity (one byte per 8 bytes data)
93 trace
[traceLen
++] = ((timestamp_start
>> 0) & 0xff);
94 trace
[traceLen
++] = ((timestamp_start
>> 8) & 0xff);
95 trace
[traceLen
++] = ((timestamp_start
>> 16) & 0xff);
96 trace
[traceLen
++] = ((timestamp_start
>> 24) & 0xff);
99 trace
[traceLen
++] = ((duration
>> 0) & 0xff);
100 trace
[traceLen
++] = ((duration
>> 8) & 0xff);
103 trace
[traceLen
++] = ((iLen
>> 0) & 0xff);
104 trace
[traceLen
++] = ((iLen
>> 8) & 0xff);
108 trace
[traceLen
- 1] |= 0x80;
112 if (btBytes
!= NULL
&& iLen
!= 0) {
113 for (int i
= 0; i
< iLen
; i
++) {
114 trace
[traceLen
++] = *btBytes
++;
118 // dummy parity bytes
119 if (num_paritybytes
!= 0) {
120 for (int i
= 0; i
< num_paritybytes
; i
++) {
121 trace
[traceLen
++] = 0x00;
129 char *getAlternativeSmartcardReader(void)
131 return AlternativeSmartcardReader
? AlternativeSmartcardReader
: PM3_SMARTCARD_DEFAULT_NAME
;
135 bool pcscCheckForCardReaders(void)
137 LONG res
= SCardEstablishContext(SCARD_SCOPE_SYSTEM
, NULL
, NULL
, &SC_Context
);
138 if (res
!= SCARD_S_SUCCESS
) {
143 res
= SCardListReaders(SC_Context
, NULL
, NULL
, &pcchReaders
);
144 if (res
!= SCARD_S_SUCCESS
) {
145 SCardReleaseContext(SC_Context
);
149 if (res
== SCARD_E_NO_READERS_AVAILABLE
|| res
== SCARD_E_NO_SERVICE
) {
150 SCardReleaseContext(SC_Context
);
158 static char *pickReader(LPTSTR readerlist
)
160 PrintAndLogEx(NORMAL
, "Please select one of these:");
161 PrintAndLogEx(NORMAL
, " [0] %s %s", PM3_SMARTCARD_DEFAULT_NAME
, PM3hasSmartcardSlot() ? "(default)" : "(default, not available)");
164 for (LPTSTR p
= readerlist
; *p
!= '\0'; ) {
165 PrintAndLogEx(NORMAL
, " [%1d] %s", num
++, p
);
166 while (*p
++ != '\0') ; // advance to next entry
172 printf("Your choice (0 or 1)?");
174 printf("Your choice (0...%d)? ", num
);
176 int selection
= getch() - '0';
179 if (selection
== 0) {
180 PrintAndLogEx(INFO
, "Selected %s", PM3_SMARTCARD_DEFAULT_NAME
);
184 if (selection
>= 1 && selection
<= num
) {
185 LPTSTR p
= readerlist
;
186 for (int i
= 1; i
< selection
; i
++) {
187 while (*p
++ != '\0') ; // advance to next entry
189 PrintAndLogEx(INFO
, "Selected %s", p
);
193 PrintAndLogEx(INFO
, "Invalid selection. Using %s", PM3_SMARTCARD_DEFAULT_NAME
);
199 static bool matchString(char *string
, const char *search
)
201 if (search
[0] == '*' && search
[1] == '\0') { // the wildcard only string "*" matches everything
205 if (search
[0] == '\0' && string
[0] != '\0') { // string is longer than pattern. No match.
209 if (search
[0] == '?' || search
[0] == string
[0]) { // wildcard '?' matches any character
210 return matchString(string
+ 1, search
+ 1);
213 if (search
[0] == '*') { // wildcard '*' matches any sequence of characters
214 for (size_t i
= 0; i
< strlen(string
); i
++) {
215 if (matchString(string
+ i
, search
+ 1)) {
225 static char *matchReader(LPTSTR readerlist
, const char *readername
)
227 if (matchString(PM3_SMARTCARD_DEFAULT_NAME
, readername
)) {
228 PrintAndLogEx(INFO
, "Selected %s", PM3_SMARTCARD_DEFAULT_NAME
);
232 for (LPTSTR p
= readerlist
; *p
!= '\0'; ) {
233 if (matchString(p
, readername
)) {
234 PrintAndLogEx(INFO
, "Selected %s", p
);
237 while (*p
++ != '\0') ; // advance to next entry
240 PrintAndLogEx(INFO
, "No match. Using %s", PM3_SMARTCARD_DEFAULT_NAME
);
245 bool pcscSelectAlternativeCardReader(const char *readername
)
247 DWORD readerlist_len
;
248 LONG res
= SCardListReaders(SC_Context
, NULL
, NULL
, &readerlist_len
);
249 if (res
!= SCARD_S_SUCCESS
) {
253 LPTSTR readerlist
= calloc(readerlist_len
, sizeof(char));
254 res
= SCardListReaders(SC_Context
, NULL
, readerlist
, &readerlist_len
);
255 if (res
!= SCARD_S_SUCCESS
) {
260 char *selected_readername
= NULL
;
262 selected_readername
= matchReader(readerlist
, readername
);
264 selected_readername
= pickReader(readerlist
);
267 if (selected_readername
== NULL
) {
272 free(AlternativeSmartcardReader
);
273 AlternativeSmartcardReader
= malloc((strlen(selected_readername
) + 1) * sizeof(char));
274 strcpy(AlternativeSmartcardReader
, selected_readername
);
281 bool pcscGetATR(smart_card_atr_t
*card
)
284 pcsc_set_tracing(true);
291 memset(card
->atr
, 0, sizeof(card
->atr
));
293 LONG res
= SCardConnect(SC_Context
, AlternativeSmartcardReader
, SCARD_SHARE_SHARED
,
294 SCARD_PROTOCOL_T0
| SCARD_PROTOCOL_T1
, &SC_Card
, &SC_Protocol
);
295 if (res
!= SCARD_S_SUCCESS
) {
299 DWORD atr_len
= sizeof(card
->atr
);
300 res
= SCardGetAttrib(SC_Card
, SCARD_ATTR_ATR_STRING
, card
->atr
, &atr_len
);
301 if (res
!= SCARD_S_SUCCESS
) {
304 card
->atr_len
= atr_len
;
306 pcsc_LogTrace(card
->atr
, card
->atr_len
, 0, 0, false);
308 pcsc_set_tracing(false);
314 void pcscTransmit(uint8_t *data
, uint32_t data_len
, uint32_t flags
, uint8_t *response
, int *response_len
)
316 LPCSCARD_IO_REQUEST protocol
;
317 if (flags
& SC_RAW_T0
) {
318 protocol
= SCARD_PCI_T0
;
320 protocol
= SCARD_PCI_RAW
;
323 if ((flags
& SC_CONNECT
))
326 pcsc_set_tracing(true);
328 if ((flags
& SC_CONNECT
|| flags
& SC_SELECT
)) {
329 LONG res
= SCardConnect(SC_Context
, AlternativeSmartcardReader
, SCARD_SHARE_SHARED
,
330 SCARD_PROTOCOL_T0
| SCARD_PROTOCOL_T1
, &SC_Card
, &SC_Protocol
);
331 if (res
!= SCARD_S_SUCCESS
) {
337 if ((flags
& SC_RAW
) || (flags
& SC_RAW_T0
)) {
338 pcsc_LogTrace(data
, data_len
, 0, 0, true);
339 DWORD len
= *response_len
;
340 LONG res
= SCardTransmit(SC_Card
, protocol
, data
, data_len
, NULL
, response
, &len
);
341 if (res
!= SCARD_S_SUCCESS
) {
344 pcsc_LogTrace(response
, len
, 0, 0, false);
348 pcsc_set_tracing(false);