forked from open-eid/smartcardpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCardBase.cpp
479 lines (422 loc) · 14.4 KB
/
CardBase.cpp
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
* SMARTCARDPP
*
* This software is released under either the GNU Library General Public
* License (see LICENSE.LGPL) or the BSD License (see LICENSE.BSD).
*
* Note that the only valid version of the LGPL license as far as this
* project is concerned is the original GNU Library General Public License
* Version 2.1, February 1999
*
*/
#include "CardBase.h"
#define log() (SCardLog::writeLog("[%i:%i][%s:%d]", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__))
CardError::CardError(byte a,byte b):runtime_error("invalid condition on card"),SW1(a),SW2(b)
{
std::ostringstream buf;
buf << "CardError:'" << runtime_error::what() << "'" <<
" SW1:'0x" <<
std::hex << std::setfill('0') <<
std::setw(2) << ushort(a) << "'"
" SW2:'0x" <<
std::hex << std::setfill('0') <<
std::setw(2) << ushort(b) << "'"
;
desc = buf.str();
}
CardBase::CardBase(PCSCManager *mgr)
{
this->mManager = mgr;
log();
}
CardBase::CardBase(PCSCManager *mManager, unsigned int idx)
{
this->mManager = mManager;
log();
connect(idx);
}
void CardBase::connect(unsigned int idx)
{
log();
mManager->connect(idx);
reIdentify();
}
CardBase::~CardBase(void)
{
log();
}
ByteVec CardBase::getTag(int identTag,int len,ByteVec &arr)
{
log();
std::ostringstream oss;
ByteVec::iterator iTag;
iTag = find(arr.begin(),arr.end(),identTag);
if (iTag == arr.end() )
{
oss << "fci tag not found, tag " << identTag;
throw CardDataError( oss.str() );
}
if (len && *(iTag+1) != len)
{
oss << "fci tag " << identTag << " invalid length, expecting " <<
len << " got " << int(*(iTag+1));
throw CardDataError(oss.str());
}
return ByteVec(iTag + 2,iTag + 2 + *(iTag + 1));
}
CardBase::FCI CardBase::parseFCI(ByteVec fci)
{
log();
ByteVec tag;
CardBase::FCI tmp;
tmp.fileID = 0;
if (fci.size() < 0x0B || (fci[0] != tagFCP && fci[0] != tagFCI) || fci.size()-2 != fci[1] )
throw CardDataError("invalid fci record");
fci = ByteVec(fci.begin()+ 2, fci.end());
tag = this->getTag(0x83, 2, fci);
if (tag.size() != 2)
throw CardDataError("file name record invalid length, not two bytes");
tmp.fileID = MAKEWORD(tag[1],tag[0]);
tag = this->getTag(0x82, 0, fci);
switch (tag[0] & 0x3F)
{
case 0x38: //DF
case 0x01: //binary
if (tag.size() != 1)
throw CardDataError("linear variable file descriptor not 1 bytes long");
tag = this->getTag(0x85,2,fci);
tmp.fileLength = MAKEWORD(tag[1],tag[0]);
break;
case 0x02:
//linear variable
case 0x04:
if (tag.size() != 5)
throw CardDataError("linear variable file descriptor not 5 bytes long");
tmp.recMaxLen = MAKEWORD( tag[0x03], tag[0x02] );
tmp.recCount = tag[0x04];
tmp.fileLength = 0;
break;
default:
throw CardDataError("invalid filelen record, unrecognized tag");
}
return tmp;
}
CardBase::FCI CardBase::selectMF(bool ignoreFCI)
{
log();
byte cmdMF[]= {CLAByte(), 0xA4, 0x00, byte(ignoreFCI ? 0x08 : 0x00), 0x00/*0x02,0x3F,0x00*/};
ByteVec code;
try
{
code = execute( MAKEVECTOR(cmdMF), ignoreFCI ? 2 : 4);
}
catch(...)
{
mManager->endTransaction(false);
SCardLog::writeLog("[%i:%i][%s:%d] Communication error. Forsing transaction end", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__);
throw;
}
if (ignoreFCI) return FCI();
return parseFCI(code);;
}
int CardBase::selectDF(int fileID,bool ignoreFCI)
{
log();
byte cmdSelectDF[] = {CLAByte(),0xA4,0x01,byte(ignoreFCI ? 0x08 : 0x04),0x02};
ByteVec cmd(MAKEVECTOR(cmdSelectDF));
cmd.push_back(HIBYTE(fileID));
cmd.push_back(LOBYTE(fileID));
ByteVec fcp;
try
{
fcp = execute(cmd, 4);
}
catch(...)
{
mManager->endTransaction(false);
SCardLog::writeLog("[%i:%i][%s:%d] Communication error. Forsing transaction end", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__);
throw;
}
if (ignoreFCI) return 0;
/* FCI blah = */parseFCI(fcp);
return 0;
}
CardBase::FCI CardBase::selectEF(int fileID,bool ignoreFCI)
{
log();
byte cmdSelectEF[] = {CLAByte(), 0xA4, 0x02, byte(ignoreFCI ? 0x08 : 0x04), 0x02 };
ByteVec cmd(MAKEVECTOR(cmdSelectEF));
cmd.push_back(HIBYTE(fileID));
cmd.push_back(LOBYTE(fileID));
ByteVec fci;
try
{
fci = execute(cmd, 4);
}
catch(...)
{
mManager->endTransaction(false);
SCardLog::writeLog("[%i:%i][%s:%d] Communication error. Forsing transaction end", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__);
throw;
}
if (ignoreFCI)
return FCI();
return parseFCI(fci);
}
#define PACKETSIZE 254
ByteVec CardBase::readEF(unsigned int fileLen)
{
log();
byte cmdReadEF[] = {CLAByte(),0xB0,0x00,0x00,0x00};
ByteVec cmd(MAKEVECTOR(cmdReadEF));
ByteVec read(0);
dword i=0;
do {
byte bytes = LOBYTE( i + PACKETSIZE > fileLen ? fileLen - i : PACKETSIZE );
cmd[2] = HIBYTE(i); //offsethi
cmd[3] = LOBYTE(i); //offsetlo
cmd[4] = bytes; //count
ByteVec ret;
try
{
ret = execute(cmd);
}
catch(CardResetError &e)
{
throw e;
}
catch(...)
{
mManager->endTransaction(false);
SCardLog::writeLog("[%i:%i][%s:%d] Communication error. Forsing transaction end", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__);
throw;
}
if ( bytes != ret.size() )
throw CardDataError("less bytes read from binary file than specified");
read.insert(read.end(), ret.begin(),ret.end());
i += PACKETSIZE ;
} while (i < (fileLen - 1));
return read;
}
ByteVec CardBase::readEFEx(unsigned int fileLen)
{
log();
byte cmdReadEF[] = {CLAByte(), 0xB0, 0x00, 0x00, 0x00, 0x00, 0x00};
ByteVec cmd(MAKEVECTOR(cmdReadEF));
ByteVec read(0);
size_t i=0;
do
{
cmd[5] = HIBYTE(fileLen); //offsethi
cmd[6] = LOBYTE(fileLen); //offsetlo
ByteVec ret;
try
{
ret = execute(cmd);
}
catch(CardResetError &e)
{
throw e;
}
catch(std::runtime_error &e)
{
throw e;
}
catch(...)
{
mManager->endTransaction(false);
SCardLog::writeLog("[%i:%i][%s:%d] Communication error. Forsing transaction end", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__);
throw;
}
SCardLog::writeLog("[%i:%i][%s:%d] Expected %i bytes, recieved %i bytes.", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__, fileLen, ret.size());
if ( fileLen != ret.size() )
{
throw CardDataError("Less bytes read from binary file than specified");
}
read.insert(read.end(), ret.begin(),ret.end());
i += ret.size();
}
while (i < (fileLen - 1));
return read;
}
ByteVec CardBase::readRecord(int numrec)
{
log();
byte cmdReadREC[] = {CLAByte(),0xB2,0x00,0x04,0x00};
cmdReadREC[2] = LOBYTE(numrec);
ByteVec ret;
try
{
ret = execute(MAKEVECTOR(cmdReadREC));
}
catch(...)
{
mManager->endTransaction(false);
SCardLog::writeLog("[%i:%i][%s:%d] Communication error. Forsing transaction end", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__);
throw;
}
return ret;
}
void CardBase::executePinEntry(ByteVec cmd)
{
log();
try
{
mManager->execPinEntryCommand(cmd);
}
catch(...)
{
mManager->endTransaction(false);
SCardLog::writeLog("[%i:%i][%s:%d] Communication error. Forsing transaction end", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__);
throw;
}
}
void CardBase::executePinChange(ByteVec cmd, size_t oldPinLen,size_t newPinLen)
{
log();
try
{
mManager->execPinChangeCommand(cmd, oldPinLen, newPinLen);
}
catch(...)
{
mManager->endTransaction(false);
SCardLog::writeLog("[%i:%i][%s:%d] Communication error. Forsing transaction end", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__);
throw;
}
}
void CardBase::setLogging(std::ostream * /*logStream*/)
{
log();
}
ByteVec CardBase::execute(ByteVec cmd, int apducase)
{
log();
ByteVec RecvBuffer(65537); // 65535 + SW
byte SW1 = 0x00;
byte SW2 = 0x00;
std::string readerState = "";
int retriesLeft = 10;
uint realLen = (uint) RecvBuffer.size() ;
std::stringstream APDU;
try
{
// T=1 needs Le byte for case 4 APDU-s
if(mManager->getProtocol() == SCARD_PROTOCOL_T1 && apducase == 4)
{
SCardLog::writeLog("[%i:%i][%s:%d] Protocol is T1. Pushing back one byte", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__);
cmd.push_back((byte) 0x00); // 0x00 == "All data available, up to 256 bytes". Sufficient for our uses.
}
SCardLog::writeLog("[%i:%i][%s:%d] Executing APDU", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__);
while((SW1 == 0x00 && SW2 == 0x00) && retriesLeft > 0)
{
SCardLog::writeAPDULog(__FUNC__, __LINE__, cmd, mManager->getProtocol(), true, mManager->getConnectionID(), mManager->getTransactionId());
if(retriesLeft < 10)
{
SCardLog::writeLog("[%i:%i][%s:%d] Command send failed with status SW1 = 0x00 SW 2 == 0x00. Pausing and then will try again. Retry: %i ", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__, 10 - retriesLeft);
#ifdef WIN32
Sleep(500);
#else
sleep(1);
#endif
SCardLog::writeLog("[%i:%i][%s:%d] Reconnecting", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__);
mManager->reconnect();
}
SCardLog::writeLog("[%i:%i][%s:%d] Executing", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__);
mManager->execCommand(cmd, RecvBuffer, realLen);
SCardLog::writeLog("[%i:%i][%s:%d] Recieved buffer size is: %i bytes", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__, realLen);
if(realLen < 2)
{
SCardLog::writeLog("[%i:%i][%s:%d] Recieved buffer size is less then two bytes: %i", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__, realLen);
throw CardDataError("Zero-length input from cardmanager");
}
RecvBuffer.resize(realLen);
SCardLog::writeLog("[%i:%i][%s:%d] Executed", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__);
SW1 = RecvBuffer[realLen - 2];
SW2 = RecvBuffer[realLen - 1];
if(SW1 == 0x00 && SW2 == 0x00)
SCardLog::writeLog("[%i:%i][%s:%d] 0x%02X 0x%02X", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__, SW1, SW2);
else
SCardLog::writeAPDULog(__FUNC__, __LINE__, RecvBuffer, mManager->getProtocol(), false, mManager->getConnectionID(), mManager->getTransactionId());
SCardLog::writeLog("[%i:%i][%s:%d] SW1: %02x SW2: %02x", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__, SW1, SW2);
// chop off SW
if(realLen > 2)
RecvBuffer.resize(realLen - 2);
SCardLog::writeLog("[%i:%i][%s:%d] Buffer resized", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__);
int retriesCount = 0;
while(SW1 == 0x61)
{
SCardLog::writeLog("[%i:%i][%s:%d] Executing retry command: %i", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__, retriesCount);
RecvBuffer.resize(258);
realLen = (uint) RecvBuffer.size();
byte cmdRead[]= {CLAByte(),0xC0,0x00,0x00,0x00};
cmdRead[4] = SW2;
ByteVec cmdReadVec = MAKEVECTOR(cmdRead);
SCardLog::writeAPDULog(__FUNC__, __LINE__, cmdReadVec, mManager->getProtocol(), true, mManager->getConnectionID(), mManager->getTransactionId());
SCardLog::writeLog("[%i:%i][%s:%d] Executing...", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__);
mManager->execCommand(cmdReadVec, RecvBuffer, realLen);
SCardLog::writeLog("[%i:%i][%s:%d] Recieved buffer size is: %i bytes", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__, realLen);
if(realLen < 2)
{
SCardLog::writeLog("[%i:%i][%s:%d] Recieved buffer size is less then two bytes: %i", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__, realLen);
throw CardDataError("Zero-length input from cardmanager");
}
RecvBuffer.resize(realLen);
SW1 = RecvBuffer[realLen - 2];
SW2 = RecvBuffer[realLen - 1];
if(SW1 == 0x00 && SW2 == 0x00)
SCardLog::writeLog("[%i:%i][%s:%d] 0x%02X 0x%02X", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__, SW1, SW2);
else
SCardLog::writeAPDULog(__FUNC__, __LINE__, RecvBuffer, mManager->getProtocol(), false, mManager->getConnectionID(), mManager->getTransactionId());
if(realLen > 2)
RecvBuffer.resize(realLen - 2);
SCardLog::writeLog("[%i:%i][%s:%d] Retry buffer resized", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__);
retriesCount++;
}
SCardLog::writeLog("[%i:%i][%s:%d] Going to next iteraction", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__);
retriesLeft--;
}
if(retriesLeft <= 0)
{
SCardLog::writeLog("[%i:%i][%s:%d] Card was reset more than 10 times. Command execution failed.", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__);
throw CardDataError("Card is in use by some other process");
}
if (!(SW1 == 0x90 && SW2 == 0x00))
{
SCardLog::writeLog("[%i:%i][%s:%d] Error: SW1 %02x SW2 %02x", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__, SW1, SW2);
throw CardError(SW1,SW2);
}
SCardLog::writeLog("[%i:%i][%s:%d] Rerturning OK", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__);
}
catch(CardError &e)
{
mManager->endTransaction(false);
SCardLog::writeLog("[%i:%i][%s:%d] CardError: SW1 %02x SW2 %02x", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__, e.SW1, e.SW2);
throw e;
}
catch(CardDataError &e)
{
mManager->endTransaction(false);
SCardLog::writeLog("[%i:%i][%s:%d] CardError: %s", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__, e.what());
throw e;
}
catch(CardResetError &e)
{
mManager->endTransaction(false);
SCardLog::writeLog("[%i:%i][%s:%d] %s", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__, e.what());
throw e;
}
catch(std::runtime_error &e)
{
mManager->endTransaction(false);
SCardLog::writeLog("[%i:%i][%s:%d] Runtime error: %s", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__, e.what());
throw e;
}
catch(...)
{
mManager->endTransaction(false);
SCardLog::writeLog("[%i:%i][%s:%d] Communication error. Forsing transaction end", mManager->getConnectionID(), mManager->getTransactionId(), __FUNC__, __LINE__);
throw;
}
return RecvBuffer;
}