-
Notifications
You must be signed in to change notification settings - Fork 1
/
CardBase.cpp
236 lines (193 loc) · 5.95 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
/*!
\file CardBase.cpp
\copyright (c) Kaido Kert ( [email protected] )
\licence BSD
\author $Author: kaidokert $
\date $Date: 2009-07-15 21:16:04 +0300 (Wed, 15 Jul 2009) $
*/
// Revision $Revision: 361 $
#include "CardBase.h"
#include <algorithm>
#include "helperMacro.h"
#include "common.h"
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(ManagerInterface &ref) :
mManager(ref),mConnection(NULL),mLogger(ref.mLogger) {
}
CardBase::CardBase(ManagerInterface &ref,unsigned int idx) :
mManager(ref),mLogger(ref.mLogger)
{
connect(idx);
}
CardBase::CardBase(ManagerInterface &ref,ConnectionBase *conn):
mManager(ref),mConnection(conn),mLogger(ref.mLogger) {}
void CardBase::connect(unsigned int idx) {
mConnection = mManager.connect(idx);
reIdentify();
}
CardBase::~CardBase(void)
{
if (mConnection) {
delete mConnection;
mConnection = NULL;
}
}
ByteVec CardBase::getTag(int identTag,int len,ByteVec &arr) {
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) {
throw std::runtime_error("unimplemented");
}
CardBase::FCI CardBase::selectMF(bool ignoreFCI)
{
byte cmdMF[]= {CLAByte(), 0xA4, 0x00, ignoreFCI ? 0x08 : 0x00, 0x00/*0x02,0x3F,0x00*/};
ByteVec code;
code = execute( MAKEVECTOR(cmdMF));
if (ignoreFCI) return FCI();
return parseFCI(code);;
}
int CardBase::selectDF(int fileID,bool ignoreFCI)
{
byte cmdSelectDF[] = {CLAByte(),0xA4,0x01,ignoreFCI ? 0x08 : 0x04,0x02};
ByteVec cmd(MAKEVECTOR(cmdSelectDF));
cmd.push_back(HIBYTE(fileID));
cmd.push_back(LOBYTE(fileID));
ByteVec fcp = execute(cmd);
if (ignoreFCI) return 0;
/* FCI blah = */parseFCI(fcp);
return 0;
}
CardBase::FCI CardBase::selectEF(int fileID,bool ignoreFCI)
{
byte cmdSelectEF[] = {CLAByte(), 0xA4, 0x02, ignoreFCI ? 0x08 : 0x04, 0x02 };
ByteVec cmd(MAKEVECTOR(cmdSelectEF));
cmd.push_back(HIBYTE(fileID));
cmd.push_back(LOBYTE(fileID));
ByteVec fci = execute(cmd);
if (ignoreFCI)
return FCI();
return parseFCI(fci);
}
#define PACKETSIZE 254
ByteVec CardBase::readEF(unsigned int fileLen)
{
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 = execute(cmd,true);
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::readRecord(int numrec)
{
byte cmdReadREC[] = {CLAByte(),0xB2,0x00,0x04,0x00};
cmdReadREC[2] = LOBYTE(numrec);
return execute(MAKEVECTOR(cmdReadREC),true);
}
void CardBase::executePinEntry(ByteVec cmd) {
mManager.execPinEntryCommand(mConnection,cmd);
}
void CardBase::executePinChange(ByteVec cmd, size_t oldPinLen,size_t newPinLen) {
mManager.execPinChangeCommand(mConnection,cmd,oldPinLen,newPinLen);
}
void CardBase::setLogging(std::ostream *logStream) {
mLogger = logStream;
}
ByteVec CardBase::execute(ByteVec cmd,bool noreply)
{
ByteVec RecvBuffer(1024);
uint realLen = (uint) RecvBuffer.size() ;
/*
if (mManager.isT1Protocol(mConnection) && !noreply) {
cmd.push_back((byte)realLen);
}
*/
if (mLogger != 0 && mLogger->good()) {
*mLogger << "-> " << std::setfill('0') << std::setw(2) << cmd.size();
if (mManager.isT1Protocol(mConnection))
*mLogger << " (T1): ";
else
*mLogger << " (T0): ";
for (ByteVec::iterator it = cmd.begin(); it < cmd.end(); it++)
*mLogger << std::hex << std::setfill('0') << std::setw(2) << (int) *it << " ";
*mLogger << std::endl << std::endl;
}
mManager.execCommand(mConnection, cmd, RecvBuffer, realLen);
if (realLen < 2)
// FIXME
throw std::runtime_error("zero-length input from cardmanager");
byte SW1 = RecvBuffer[ realLen - 2 ];
byte SW2 = RecvBuffer[ realLen - 1 ];
/*
if (SW1 == 0x67 ) { //fallback, this should never occur in production
cmd.pop_back();
realLen = (dword) RecvBuffer.size();
mManager.execCommand(mConnection,cmd,RecvBuffer,realLen);
if (realLen < 2)
// FIXME
throw std::runtime_error("zero-length input from cardmanager");
SW1 = RecvBuffer[ realLen - 2 ];
SW2 = RecvBuffer[ realLen - 1 ];
}
*/
RecvBuffer.resize(realLen - 2);
if (mLogger != 0 && mLogger->good()) {
*mLogger << "<- ";
*mLogger << std::hex << std::setfill('0') << std::setw(2) << (int) SW1;
*mLogger << std::hex << std::setfill('0') << std::setw(2) << (int) SW2;
if (RecvBuffer.size() > 0) {
*mLogger << " [" << std::hex << std::setfill('0') << std::setw(2) << RecvBuffer.size() << "]: ";
for(ByteVec::iterator it = RecvBuffer.begin(); it < RecvBuffer.end(); it++ )
*mLogger << std::hex << std::setfill('0') << std::setw(2) << (int) *it << " ";
}
*mLogger << std::endl << std::endl;
}
if (SW1 == 0x61) {
byte cmdRead[]= {CLAByte(),0xC0,0x00,0x00,0x00};
cmdRead[4] = SW2;
return execute(MAKEVECTOR(cmdRead), true);
}
if (SW1 != 0x90)
throw CardError(SW1,SW2);
return RecvBuffer;
}
void CardBase::endTransaction() {
mManager.endTransaction(mConnection,true);
}
bool CardBase::hasSecurePinEntry() {
return mConnection->isSecure();
}