]>
Commit | Line | Data |
---|---|---|
a9104f7e | 1 | //----------------------------------------------------------------------------- |
2 | // Copyright (C) 2019 piwi | |
3 | // | |
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 | |
6 | // the license. | |
7 | //----------------------------------------------------------------------------- | |
8 | // PCSC functions to use alternative Smartcard Readers | |
9 | //----------------------------------------------------------------------------- | |
10 | ||
11 | #include "pcsc.h" | |
12 | ||
13 | #include <stdlib.h> | |
14 | #include <stdbool.h> | |
15 | #include <stdio.h> | |
16 | #include <string.h> | |
17 | ||
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))) | |
41bdfce3 | 22 | #define SCARD_CLASS_ICC_STATE 9 |
a9104f7e | 23 | #define SCARD_ATTR_ATR_STRING SCARD_ATTR_VALUE(SCARD_CLASS_ICC_STATE, 0x0303) |
24 | #elif defined (_WIN32) | |
25 | #include <winscard.h> | |
26 | #else | |
27 | #include <winscard.h> | |
28 | #include <reader.h> | |
29 | #endif | |
30 | ||
31 | #include "ui.h" | |
32 | #include "util.h" | |
33 | #include "cmdhw.h" | |
34 | ||
41bdfce3 | 35 | #define PM3_SMARTCARD_DEFAULT_NAME "PM3 RDV40 Smartcard Slot" |
36 | ||
a9104f7e | 37 | static SCARDCONTEXT SC_Context; |
38 | static SCARDHANDLE SC_Card; | |
39 | static DWORD SC_Protocol; | |
40 | static char* AlternativeSmartcardReader = NULL; | |
41 | ||
42 | ||
43 | char *getAlternativeSmartcardReader(void) | |
44 | { | |
41bdfce3 | 45 | return AlternativeSmartcardReader ? AlternativeSmartcardReader : PM3_SMARTCARD_DEFAULT_NAME; |
a9104f7e | 46 | } |
47 | ||
48 | ||
49 | bool pcscCheckForCardReaders(void) | |
50 | { | |
51 | LONG res = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &SC_Context); | |
52 | if (res != SCARD_S_SUCCESS) { | |
53 | return false; | |
54 | } | |
55 | ||
56 | DWORD pcchReaders; | |
57 | res = SCardListReaders(SC_Context, NULL, NULL, &pcchReaders); | |
58 | if (res != SCARD_S_SUCCESS) { | |
59 | SCardReleaseContext(SC_Context); | |
60 | return false; | |
61 | } | |
62 | ||
63 | if (res == SCARD_E_NO_READERS_AVAILABLE || res == SCARD_E_NO_SERVICE) { | |
64 | SCardReleaseContext(SC_Context); | |
65 | return false; | |
66 | } | |
41bdfce3 | 67 | |
a9104f7e | 68 | return true; |
69 | } | |
70 | ||
71 | ||
72 | static char *pickReader(LPTSTR readerlist) | |
73 | { | |
74 | PrintAndLogEx(NORMAL, "Please select one of these:"); | |
41bdfce3 | 75 | PrintAndLogEx(NORMAL, " [0] %s %s", PM3_SMARTCARD_DEFAULT_NAME, PM3hasSmartcardSlot() ? "(default)" : "(default, not available)"); |
a9104f7e | 76 | |
77 | int num = 1; | |
78 | for (LPTSTR p = readerlist; *p != '\0'; ) { | |
79 | PrintAndLogEx(NORMAL, " [%1d] %s", num++, p); | |
80 | while (*p++ != '\0') ; // advance to next entry | |
81 | } | |
82 | ||
83 | num--; | |
41bdfce3 | 84 | |
a9104f7e | 85 | if (num == 1) { |
86 | printf("Your choice (0 or 1)?"); | |
87 | } else { | |
88 | printf("Your choice (0...%d)? ", num); | |
89 | } | |
90 | int selection = getch() - '0'; | |
41bdfce3 | 91 | printf("\n"); |
a9104f7e | 92 | |
93 | if (selection == 0) { | |
41bdfce3 | 94 | PrintAndLogEx(INFO, "Selected %s", PM3_SMARTCARD_DEFAULT_NAME); |
a9104f7e | 95 | return NULL; |
96 | } | |
97 | ||
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 | |
102 | } | |
103 | PrintAndLogEx(INFO, "Selected %s", p); | |
104 | return p; | |
105 | } | |
106 | ||
41bdfce3 | 107 | PrintAndLogEx(INFO, "Invalid selection. Using %s", PM3_SMARTCARD_DEFAULT_NAME); |
a9104f7e | 108 | return NULL; |
41bdfce3 | 109 | |
110 | } | |
111 | ||
112 | ||
113 | static bool matchString(char *string, const char *search) | |
114 | { | |
115 | if (search[0] == '*' && search[1] == '\0') { // the wildcard only string "*" matches everything | |
116 | return true; | |
117 | } | |
118 | ||
119 | if (search[0] == '\0' && string[0] != '\0') { // string is longer than pattern. No match. | |
120 | return false; | |
121 | } | |
122 | ||
123 | if (search[0] == '?' || search[0] == string[0]) { // wildcard '?' matches any character | |
124 | return matchString(string + 1, search + 1); | |
125 | } | |
126 | ||
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)) { | |
130 | return true; | |
131 | } | |
132 | } | |
133 | } | |
134 | ||
135 | return false; | |
a9104f7e | 136 | } |
137 | ||
138 | ||
41bdfce3 | 139 | static char *matchReader(LPTSTR readerlist, const char *readername) |
a9104f7e | 140 | { |
41bdfce3 | 141 | if (matchString(PM3_SMARTCARD_DEFAULT_NAME, readername)) { |
142 | PrintAndLogEx(INFO, "Selected %s", PM3_SMARTCARD_DEFAULT_NAME); | |
143 | return NULL; | |
144 | } | |
145 | ||
146 | for (LPTSTR p = readerlist; *p != '\0'; ) { | |
147 | if (matchString(p, readername)) { | |
148 | PrintAndLogEx(INFO, "Selected %s", p); | |
149 | return p; | |
150 | } | |
151 | while (*p++ != '\0') ; // advance to next entry | |
152 | } | |
153 | ||
154 | PrintAndLogEx(INFO, "No match. Using %s", PM3_SMARTCARD_DEFAULT_NAME); | |
155 | return NULL; | |
a9104f7e | 156 | } |
157 | ||
41bdfce3 | 158 | |
a9104f7e | 159 | bool pcscSelectAlternativeCardReader(const char *readername) |
160 | { | |
161 | DWORD readerlist_len; | |
162 | LONG res = SCardListReaders(SC_Context, NULL, NULL, &readerlist_len); | |
163 | if (res != SCARD_S_SUCCESS) { | |
164 | return false; | |
165 | } | |
166 | ||
167 | LPTSTR readerlist = calloc(readerlist_len, sizeof(char)); | |
168 | res = SCardListReaders(SC_Context, NULL, readerlist, &readerlist_len); | |
169 | if (res != SCARD_S_SUCCESS) { | |
170 | free(readerlist); | |
171 | return false; | |
172 | } | |
173 | ||
174 | char *selected_readername = NULL; | |
175 | if (readername) { | |
41bdfce3 | 176 | selected_readername = matchReader(readerlist, readername); |
a9104f7e | 177 | } else { |
178 | selected_readername = pickReader(readerlist); | |
179 | } | |
180 | ||
181 | if (selected_readername == NULL) { | |
182 | free(readerlist); | |
183 | return false; | |
184 | } | |
185 | ||
186 | free(AlternativeSmartcardReader); | |
187 | AlternativeSmartcardReader = malloc((strlen(selected_readername) + 1) * sizeof(char)); | |
188 | strcpy(AlternativeSmartcardReader, selected_readername); | |
41bdfce3 | 189 | |
190 | free(readerlist); | |
a9104f7e | 191 | return true; |
192 | } | |
193 | ||
194 | ||
195 | bool pcscGetATR(smart_card_atr_t *card) | |
196 | { | |
197 | if (!card) { | |
198 | return false; | |
199 | } | |
41bdfce3 | 200 | |
a9104f7e | 201 | card->atr_len = 0; |
202 | memset(card->atr, 0, sizeof(card->atr)); | |
203 | ||
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) { | |
207 | return false; | |
208 | } | |
209 | ||
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) { | |
213 | return false; | |
214 | } | |
215 | card->atr_len = atr_len; | |
41bdfce3 | 216 | |
a9104f7e | 217 | // TODO: LogTrace without device |
41bdfce3 | 218 | |
219 | return true; | |
a9104f7e | 220 | } |
6b5105be | 221 | |
222 | ||
223 | void pcscTransmit(uint8_t *data, uint32_t data_len, uint32_t flags, uint8_t *response, int *response_len) | |
224 | { | |
225 | LPCSCARD_IO_REQUEST protocol; | |
226 | if (flags & SC_RAW_T0) { | |
227 | protocol = SCARD_PCI_T0; | |
228 | } else { | |
229 | protocol = SCARD_PCI_RAW; | |
230 | } | |
231 | ||
232 | // TODO: tracing | |
233 | // if ((flags & SC_CONNECT)) | |
234 | // clear_trace(); | |
235 | ||
236 | // set_tracing(true); | |
237 | ||
41bdfce3 | 238 | if ((flags & SC_CONNECT || flags & SC_SELECT)) { |
6b5105be | 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) { | |
242 | *response_len = -1; | |
243 | return; | |
244 | } | |
245 | } | |
41bdfce3 | 246 | |
6b5105be | 247 | if ((flags & SC_RAW) || (flags & SC_RAW_T0)) { |
248 | // TODO: tracing | |
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) { | |
253 | *response_len = -1; | |
254 | } else { | |
255 | *response_len = len; | |
256 | } | |
257 | } | |
258 | } |