-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWinscardDriver.cpp
More file actions
363 lines (295 loc) · 10.7 KB
/
WinscardDriver.cpp
File metadata and controls
363 lines (295 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include "WinscardDriver.hpp"
//#define __DEBUG__
LONG WinscardDriver::SCard_EstablishContext(DWORD dwScope) {
LONG ret = SCardEstablishContext(dwScope, NULL, NULL, &(this->hContext));
return ret;
}
LONG WinscardDriver::SCard_RealeaseContext() {
return SCardReleaseContext(hContext);
}
LONG WinscardDriver::SCard_ListReaders() {
wchar_t readers[256];
DWORD readersLen = sizeof(readers);
LPTSTR pReader; //Reader List String Ptr
LONG ret = SCardListReaders(hContext, NULL, readers, &readersLen);
if (ret != SCARD_S_SUCCESS) {
/*printf("Failed to list readers: %lX\n", ret);*/
SCard_RealeaseContext();
return ret;
}
readerList.clear();
pReader = readers;
while ('\0' != *pReader)
{
//Reader 리스트 wstring으로 변환하여 vector에 삽입
std::wstring reader(pReader);
readerList.push_back(reader);
// Advance to the next value.
pReader = pReader + wcslen(pReader) + 1;
}
return SCARD_S_SUCCESS;
}
std::vector<std::wstring> WinscardDriver::getWReaderList() {
return this->readerList;
}
std::vector<std::string> WinscardDriver::getReaderList() {
return WSVtoSV(this->readerList);
}
int WinscardDriver::getReaderNum() {
return this->readerList.size();
}
LONG WinscardDriver::SCard_Connect(int readerNum) {
if (readerNum > readerList.size()) return -1;
LONG result = SCardConnect(
hContext,
readerList[readerNum].c_str(),
SCARD_SHARE_SHARED,
SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1,
&hCard,
&dwActiveProtocol
);
if (result != SCARD_S_SUCCESS) {
#ifdef __DEBUG__
std::cerr << "Failed to connect to the card: ";
printf("%X\n", result);
#endif
SCard_RealeaseContext();
return result;
}
connectStatus = ConnectStatus::Connected;
return result;
}
LONG WinscardDriver::SCard_Disconnect() {
return SCardDisconnect(hCard, SCARD_LEAVE_CARD);
}
LONG WinscardDriver::SCard_getATR(uint8_t readerNum, BYTE * pATR, DWORD * atrLen) {
if (readerNum >= readerList.size()) return -1;
DWORD dwReaderLen = 256;
wchar_t szReaderName[256];
memset(szReaderName, '\0', 256);
wcscpy_s(szReaderName, dwReaderLen, readerList[readerNum].c_str());
DWORD dwState, dwProtocol;
LONG ret = SCardStatus(
hCard,
szReaderName, &dwReaderLen,
&dwState, &dwProtocol,
pATR, atrLen
);
return ret;
}
//Applications
//Get Smart Card UID(CSN)
LONG WinscardDriver::getSCardUID(uint8_t * UID) {
if (this->connectStatus == Disconnected) {
std::cerr << "Card is Disconneted now" << std::endl;
return -1;
}
const SCARD_IO_REQUEST* pciProtocol = (dwActiveProtocol == SCARD_PROTOCOL_T0) ? SCARD_PCI_T0 : SCARD_PCI_T1;
unsigned char apduCommand[] = { 0xFF, 0xCA, 0x00, 0x00, 0x00 };
unsigned char response[256];
DWORD resLen = sizeof(response);
LONG result = SCardTransmit(
hCard,
pciProtocol,
apduCommand,
sizeof(apduCommand),
nullptr,
response,
&resLen
);
if (result == SCARD_S_SUCCESS && (resLen) != 0) {
/*
std::cout << "UID: ";
for (DWORD i = 0; i < dwResponseLength; ++i) {
printf("%02X ", response[i]);
}
printf("\n");
*/
if (response[resLen - 2] == 0x90 && response[resLen - 1] == 0x00) {
memcpy_s(UID, 4, response, 4);
return result;
}
else if (response[resLen - 2] == 0x63 && response[resLen - 1] == 0x00) {
return -1;
}
}
//std::cerr << "Failed to transmit APDU: " << result << std::endl;
return result;
}
LONG WinscardDriver::SCard_Transmit(uint8_t* resBuf, DWORD* bufLen, uint8_t * sendData, DWORD sendLen) {
const SCARD_IO_REQUEST* pciProtocol = (dwActiveProtocol == SCARD_PROTOCOL_T0) ? SCARD_PCI_T0 : SCARD_PCI_T1;
LONG result = SCardTransmit(
hCard,
pciProtocol,
sendData,
sendLen,
nullptr,
resBuf,
bufLen
);
if (result == SCARD_S_SUCCESS && (*bufLen) != 0) {
#ifdef __DEBUG__
#endif
}
return result;
}
LONG WinscardDriver::loadKey(uint8_t * key, uint8_t keyNum, uint8_t* resBuf, DWORD* bufLen) {
unsigned char loadAuthenticationKeysCmd[] = { 0xff,0x82, 0x00,keyNum,0x06,0xff,0xff,0xff,0xff,0xff,0xff };
memcpy(&(loadAuthenticationKeysCmd[5]), key, 6);
#ifdef __DEBUG__
std::cout << "Scard Transmit Command : ";
for (int i = 0; i < 11; i++) {
printf("%02x", loadAuthenticationKeysCmd[i]);
}
printf("\n");
#endif
const SCARD_IO_REQUEST* pciProtocol = (dwActiveProtocol == SCARD_PROTOCOL_T0) ? SCARD_PCI_T0 : SCARD_PCI_T1;
LONG result = SCardTransmit(
hCard,
pciProtocol,
loadAuthenticationKeysCmd,
sizeof(loadAuthenticationKeysCmd),
nullptr,
resBuf,
bufLen
);
if (result == SCARD_S_SUCCESS && (*bufLen) != 0 ) {
#ifdef __DEBUG__
std::cout << "Load Key Success" << std::endl;
printf(" └ Response Data : ");
for (int cnt = 0; cnt < dwResponseLength; cnt++) {
printf("%02x ", resBuf[cnt]);
}
printf("\n");
#endif
if (resBuf[(*bufLen) - 2] == 0x90 && resBuf[(*bufLen) - 1] == 0x00) {
return result;
}
#ifdef __DEBUG__
std::cerr << "Failed to Load Key : " << result << std::endl;
#endif
else if (resBuf[(*bufLen) - 2] == 0x63 && resBuf[(*bufLen) - 1] == 0x00) {
return -1;
}
}
return result;
}
//TODO : keyType Typedef
LONG WinscardDriver::authentication(int blkNum, char keyType, uint8_t keyNum, uint8_t* resBuf, DWORD* bufLen) {
if (keyType != 'A' && keyType != 'B') {
#ifdef __DEBUG__
printf("Auth Key Type Error %c\n", keyType);
#endif
return -1;
}
//printf("[Read %02d Block Start]\n", blkNum);
const SCARD_IO_REQUEST* pciProtocol = (dwActiveProtocol == SCARD_PROTOCOL_T0) ? SCARD_PCI_T0 : SCARD_PCI_T1;
unsigned char authenticationCmd[] = { 0xff,0x88, 0x00, blkNum,0x60,0x00 };
authenticationCmd[4] = keyType == 'A' ? 0x60 : 0x61;
LONG result = SCardTransmit(
hCard,
pciProtocol,
authenticationCmd,
sizeof(authenticationCmd),
nullptr,
resBuf,
bufLen
);
if (result == SCARD_S_SUCCESS && (*bufLen) != 0) {
if (resBuf[(*bufLen) - 2] == 0x63 && resBuf[(*bufLen) - 1] == 0x00)
return -1;
else if (resBuf[(*bufLen) - 2] == 0x90 && resBuf[(*bufLen)-1] == 0x00)
return result;
}
return result;
}
LONG WinscardDriver::readBinaryBlock(int blkNum, uint8_t * resBuf, DWORD *bufLen) {
const SCARD_IO_REQUEST* pciProtocol = (dwActiveProtocol == SCARD_PROTOCOL_T0) ? SCARD_PCI_T0 : SCARD_PCI_T1;
#ifdef __DEBUG__
printf("[Read Block %d]\n", blkNum);
#endif
unsigned char readBinaryBlocks[] = { 0xff, 0xb0, 0x00, blkNum, 0x10 };
LONG result = SCardTransmit(
hCard,
pciProtocol,
readBinaryBlocks,
sizeof(readBinaryBlocks),
nullptr,
resBuf,
bufLen
);
if (result == SCARD_S_SUCCESS && (*bufLen) != 0) {
if (resBuf[0] == 0x63 && resBuf[1] == 0x00) {
return -1;
}
else if (resBuf[(*bufLen) - 2] == 0x90 && resBuf[(*bufLen) - 1] == 0x00)
return result;
}
return result;
}
std::string WinscardDriver::errToString(LONG errCode) {
switch (errCode) {
case 0x00000000: return "SCARD_S_SUCCESS";
case 0x80100001: return "SCARD_F_INTERNAL_ERROR";
case 0x80100002: return "SCARD_E_CANCELLED";
case 0x80100003: return "SCARD_E_INVALID_HANDLE";
case 0x80100004: return "SCARD_E_INVALID_PARAMETER";
case 0x80100005: return "SCARD_E_INVALID_TARGET";
case 0x80100006: return "SCARD_E_NO_MEMORY";
case 0x80100007: return "SCARD_F_WAITED_TOO_LONG";
case 0x80100008: return "SCARD_E_INSUFFICIENT_BUFFER";
case 0x80100009: return "SCARD_E_UNKNOWN_READER";
case 0x8010000A: return "SCARD_E_TIMEOUT";
case 0x8010000B: return "SCARD_E_SHARING_VIOLATION";
case 0x8010000C: return "SCARD_E_NO_SMARTCARD";
case 0x8010000D: return "SCARD_E_UNKNOWN_CARD";
case 0x8010000E: return "SCARD_E_CANT_DISPOSE";
case 0x8010000F: return "SCARD_E_PROTO_MISMATCH";
case 0x80100010: return "SCARD_E_NOT_READY";
case 0x80100011: return "SCARD_E_INVALID_VALUE";
case 0x80100012: return "SCARD_E_SYSTEM_CANCELLED";
case 0x80100013: return "SCARD_F_COMM_ERROR";
case 0x80100014: return "SCARD_F_UNKNOWN_ERROR";
case 0x80100015: return "SCARD_E_INVALID_ATR";
case 0x80100016: return "SCARD_E_NOT_TRANSACTED";
case 0x80100017: return "SCARD_E_READER_UNAVAILABLE";
case 0x80100018: return "SCARD_P_SHUTDOWN";
case 0x80100019: return "SCARD_E_PCI_TOO_SMALL";
case 0x8010001A: return "SCARD_E_READER_UNSUPPORTED";
case 0x8010001B: return "SCARD_E_DUPLICATE_READER";
case 0x8010001C: return "SCARD_E_CARD_UNSUPPORTED";
case 0x8010001D: return "SCARD_E_NO_SERVICE";
case 0x8010001E: return "SCARD_E_SERVICE_STOPPED";
case 0x8010001F: return "SCARD_E_UNSUPPORTED_FEATURE";
case 0x80100020: return "SCARD_E_ICC_INSTALLATION";
case 0x80100021: return "SCARD_E_ICC_CREATEORDER";
case 0x80100023: return "SCARD_E_DIR_NOT_FOUND";
case 0x80100024: return "SCARD_E_FILE_NOT_FOUND";
case 0x80100025: return "SCARD_E_NO_DIR";
case 0x80100026: return "SCARD_E_NO_FILE";
case 0x80100027: return "SCARD_E_NO_ACCESS";
case 0x80100028: return "SCARD_E_WRITE_TOO_MANY";
case 0x80100029: return "SCARD_E_BAD_SEEK";
case 0x8010002A: return "SCARD_E_INVALID_CHV";
case 0x8010002B: return "SCARD_E_UNKNOWN_RES_MSG";
case 0x8010002C: return "SCARD_E_NO_SUCH_CERTIFICATE";
case 0x8010002D: return "SCARD_E_CERTIFICATE_UNAVAILABLE";
case 0x8010002E: return "SCARD_E_NO_READERS_AVAILABLE";
case 0x8010002F: return "SCARD_E_COMM_DATA_LOST";
case 0x80100030: return "SCARD_E_NO_KEY_CONTAINER";
case 0x80100031: return "SCARD_E_SERVER_TOO_BUSY";
case 0x80100065: return "SCARD_W_UNSUPPORTED_CARD";
case 0x80100066: return "SCARD_W_UNRESPONSIVE_CARD";
case 0x80100067: return "SCARD_W_UNPOWERED_CARD";
case 0x80100068: return "SCARD_W_RESET_CARD";
case 0x80100069: return "SCARD_W_REMOVED_CARD";
case 0x8010006A: return "SCARD_W_SECURITY_VIOLATION";
case 0x8010006B: return "SCARD_W_WRONG_CHV";
case 0x8010006C: return "SCARD_W_CHV_BLOCKED";
case 0x8010006D: return "SCARD_W_EOF";
case 0x8010006E: return "SCARD_W_CANCELLED_BY_USER";
case 0x8010006F: return "SCARD_W_CARD_NOT_AUTHENTICATED";
case -1 : return "General Application Error (6300)";
default : return "UNKNOWN_ERROR";
}
}