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
;
43 char *getAlternativeSmartcardReader(void)
45 return AlternativeSmartcardReader
? AlternativeSmartcardReader
: PM3_SMARTCARD_DEFAULT_NAME
;
49 bool pcscCheckForCardReaders(void)
51 LONG res
= SCardEstablishContext(SCARD_SCOPE_SYSTEM
, NULL
, NULL
, &SC_Context
);
52 if (res
!= SCARD_S_SUCCESS
) {
57 res
= SCardListReaders(SC_Context
, NULL
, NULL
, &pcchReaders
);
58 if (res
!= SCARD_S_SUCCESS
) {
59 SCardReleaseContext(SC_Context
);
63 if (res
== SCARD_E_NO_READERS_AVAILABLE
|| res
== SCARD_E_NO_SERVICE
) {
64 SCardReleaseContext(SC_Context
);
72 static char *pickReader(LPTSTR readerlist
)
74 PrintAndLogEx(NORMAL
, "Please select one of these:");
75 PrintAndLogEx(NORMAL
, " [0] %s %s", PM3_SMARTCARD_DEFAULT_NAME
, PM3hasSmartcardSlot() ? "(default)" : "(default, not available)");
78 for (LPTSTR p
= readerlist
; *p
!= '\0'; ) {
79 PrintAndLogEx(NORMAL
, " [%1d] %s", num
++, p
);
80 while (*p
++ != '\0') ; // advance to next entry
86 printf("Your choice (0 or 1)?");
88 printf("Your choice (0...%d)? ", num
);
90 int selection
= getch() - '0';
94 PrintAndLogEx(INFO
, "Selected %s", PM3_SMARTCARD_DEFAULT_NAME
);
98 if (selection
>= 1 && selection
<= num
) {
99 LPTSTR p
= readerlist
;
100 for (int i
= 1; i
< selection
; i
++) {
101 while (*p
++ != '\0') ; // advance to next entry
103 PrintAndLogEx(INFO
, "Selected %s", p
);
107 PrintAndLogEx(INFO
, "Invalid selection. Using %s", PM3_SMARTCARD_DEFAULT_NAME
);
113 static bool matchString(char *string
, const char *search
)
115 if (search
[0] == '*' && search
[1] == '\0') { // the wildcard only string "*" matches everything
119 if (search
[0] == '\0' && string
[0] != '\0') { // string is longer than pattern. No match.
123 if (search
[0] == '?' || search
[0] == string
[0]) { // wildcard '?' matches any character
124 return matchString(string
+ 1, search
+ 1);
127 if (search
[0] == '*') { // wildcard '*' matches any sequence of characters
128 for (size_t i
= 0; i
< strlen(string
); i
++) {
129 if (matchString(string
+ i
, search
+ 1)) {
139 static char *matchReader(LPTSTR readerlist
, const char *readername
)
141 if (matchString(PM3_SMARTCARD_DEFAULT_NAME
, readername
)) {
142 PrintAndLogEx(INFO
, "Selected %s", PM3_SMARTCARD_DEFAULT_NAME
);
146 for (LPTSTR p
= readerlist
; *p
!= '\0'; ) {
147 if (matchString(p
, readername
)) {
148 PrintAndLogEx(INFO
, "Selected %s", p
);
151 while (*p
++ != '\0') ; // advance to next entry
154 PrintAndLogEx(INFO
, "No match. Using %s", PM3_SMARTCARD_DEFAULT_NAME
);
159 bool pcscSelectAlternativeCardReader(const char *readername
)
161 DWORD readerlist_len
;
162 LONG res
= SCardListReaders(SC_Context
, NULL
, NULL
, &readerlist_len
);
163 if (res
!= SCARD_S_SUCCESS
) {
167 LPTSTR readerlist
= calloc(readerlist_len
, sizeof(char));
168 res
= SCardListReaders(SC_Context
, NULL
, readerlist
, &readerlist_len
);
169 if (res
!= SCARD_S_SUCCESS
) {
174 char *selected_readername
= NULL
;
176 selected_readername
= matchReader(readerlist
, readername
);
178 selected_readername
= pickReader(readerlist
);
181 if (selected_readername
== NULL
) {
186 free(AlternativeSmartcardReader
);
187 AlternativeSmartcardReader
= malloc((strlen(selected_readername
) + 1) * sizeof(char));
188 strcpy(AlternativeSmartcardReader
, selected_readername
);
195 bool pcscGetATR(smart_card_atr_t
*card
)
202 memset(card
->atr
, 0, sizeof(card
->atr
));
204 LONG res
= SCardConnect(SC_Context
, AlternativeSmartcardReader
, SCARD_SHARE_SHARED
,
205 SCARD_PROTOCOL_T0
| SCARD_PROTOCOL_T1
, &SC_Card
, &SC_Protocol
);
206 if (res
!= SCARD_S_SUCCESS
) {
210 DWORD atr_len
= sizeof(card
->atr
);
211 res
= SCardGetAttrib(SC_Card
, SCARD_ATTR_ATR_STRING
, card
->atr
, &atr_len
);
212 if (res
!= SCARD_S_SUCCESS
) {
215 card
->atr_len
= atr_len
;
217 // TODO: LogTrace without device
223 void pcscTransmit(uint8_t *data
, uint32_t data_len
, uint32_t flags
, uint8_t *response
, int *response_len
)
225 LPCSCARD_IO_REQUEST protocol
;
226 if (flags
& SC_RAW_T0
) {
227 protocol
= SCARD_PCI_T0
;
229 protocol
= SCARD_PCI_RAW
;
233 // if ((flags & SC_CONNECT))
236 // set_tracing(true);
238 if ((flags
& SC_CONNECT
|| flags
& SC_SELECT
)) {
239 LONG res
= SCardConnect(SC_Context
, AlternativeSmartcardReader
, SCARD_SHARE_SHARED
,
240 SCARD_PROTOCOL_T0
| SCARD_PROTOCOL_T1
, &SC_Card
, &SC_Protocol
);
241 if (res
!= SCARD_S_SUCCESS
) {
247 if ((flags
& SC_RAW
) || (flags
& SC_RAW_T0
)) {
249 // LogTrace(data, arg1, 0, 0, NULL, true);
250 DWORD len
= *response_len
;
251 LONG res
= SCardTransmit(SC_Card
, protocol
, data
, data_len
, NULL
, response
, &len
);
252 if (res
!= SCARD_S_SUCCESS
) {