-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheapi_dummy_session.cpp
245 lines (226 loc) · 7.24 KB
/
eapi_dummy_session.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
extern "C" {
#include "nexoid-ed/include/gtd.h"
#include "nexoid-ed/include/eapi.h"
}
#include "eapi_dummy_session.hpp"
#include "utils.hpp"
#include "ttd_keeper.hpp"
#include <cstring>
using namespace std;
EapiDummy::EapiDummy() = default;
EapiDummy::~EapiDummy() noexcept = default;
enum EapiResult
EapiDummy::Activate_Contacts_And_Reset_Chip() {
const int test_variant = 0;
switch (test_variant) {
case 0:
TtdKeeper::instance().update(TECH_EMV_CHIP);
return EAPI_OK;
case 1:
TtdKeeper::instance().update(TECH_NON_EMV_CHIP);
return EAPI_OK;
case 2:
TtdKeeper::instance().update(N_CARD_MISSING);
return EAPI_NOK;
case 3:
TtdKeeper::instance().update(N_CHIP_ERROR);
return EAPI_NOK;
}
return EAPI_NOK;
}
enum EapiResult
EapiDummy::Reset_Chip() {
return EAPI_NOK;
}
enum EapiResult
EapiDummy::Select_Pse() {
const uint8_t response[] = {
0x6F, 0x15, // FCI Template
0x84, 0x0E, // DF Name
'1','P','A','Y','.','S','Y','S','.','D','D','F','0','1',
0xA5, 0x03, // FCI Proprietary Template
0x88, 0x01, // SFI
0x01
};
cr = acp((struct CardResponse){
.responseData = acp((struct ResponseData){
.s = sizeof(response),
.c = {}
}),
.parsed = acp((struct ParsedResponseData){
.fci = acp((struct FileControlInformation){
.DfName = {
.l_raw = 14,
.raw = {
'1','P','A','Y','.','S','Y','S','.','D','D','F','0','1'
}
},
.a5 = {
.applicationLabel = nullptr,
.applicationPriorityIndicator = nullptr,
.pdol = nullptr,
.languagePreference = nullptr,
.issuerCodeTableIndex = nullptr,
.applicationPreferredName = nullptr,
.issuerDiscretionaryData = nullptr,
.sfi = acp((unsigned char)0x01),
},
}),
.responseMessageTemplate = nullptr,
.readRecordResponeMessageTemplate = nullptr,
.unpredictableNumber = nullptr,
}),
.sw1Sw2 = { .e = I_COMMAND_OK }
});
memcpy(cr->responseData->c, response, sizeof(response));
return EAPI_OK;
}
enum EapiResult
EapiDummy::Select_Application(const struct Aid& aid) {
cr = acp((struct CardResponse){
.responseData = nullptr,
.parsed = nullptr,
.sw1Sw2 = { .e = I_COMMAND_OK }
});
return EAPI_OK;
}
enum EapiResult
EapiDummy::Read_Record(const byte p1, const byte p2) {
cr = acp((struct CardResponse){
.responseData = nullptr,
.parsed = nullptr,
.sw1Sw2 = { .e = I_COMMAND_OK }
});
return EAPI_OK;
}
enum EapiResult
EapiDummy::Final_Application_Selection() {
struct FileControlInformation fci = {
.DfName = {
.l_raw = 6,
.raw = { 0xA0, 0x00, 0x00, 0x00, 0x00, 0x01 }
},
.a5 = {
.applicationLabel = acp((struct string16){ "TestApp", {} }),
.applicationPriorityIndicator = nullptr,
.pdol = acp((struct Dol){
.s = 9,
.a = {
0x9F, 0x1A, 0x02, // Terminal Country Code
0x9F, 0x33, 0x03, // Terminal Capabilities
0x9F, 0x40, 0x02 // Additional Terminal Capabilities (partial)
}
}),
.languagePreference = acp((struct TerminalSupportedLanguageList){
.l = 1,
.a = { { .e = LANG_ENGLISH } }
}),
.issuerCodeTableIndex = nullptr,
.applicationPreferredName = nullptr,
.issuerDiscretionaryData = nullptr,
.sfi = nullptr,
}
};
cr = acp((struct CardResponse){
.responseData = nullptr,
.parsed = acp((struct ParsedResponseData){
.fci = &fci,
.responseMessageTemplate = nullptr,
.readRecordResponeMessageTemplate = nullptr,
.unpredictableNumber = nullptr,
}),
.sw1Sw2 = { .e = I_COMMAND_OK }
});
return EAPI_OK;
}
enum EapiResult
EapiDummy::Select_Next_Application(const struct Aid& aid) {
return EAPI_OK;
}
enum EapiResult
EapiDummy::Get_Processing_Options(const byte commandTemplate, const struct DolData& cdaTransactionData) {
struct ResponseMessageTemplate gpoResponse = [](){
struct ResponseMessageTemplate ret{};
ret.aip.cardholderVerificationIsSupported = 1;
ret.aip.sdaSupported = 1;
ret.afl.raw_size = 4;
ret.afl.a[0].sfi = 5;
ret.afl.a[0].firstRecordNumber = 1;
ret.afl.a[0].lastRecordNumber = 1;
ret.afl.a[0].numberOfOdaAuthenticatedRecords = 0;
return ret;
}();
cr = acp((struct CardResponse){
.responseData = nullptr,
.parsed = acp((struct ParsedResponseData){
.fci = nullptr,
.responseMessageTemplate = &gpoResponse,
.readRecordResponeMessageTemplate = nullptr,
.unpredictableNumber = nullptr,
}),
.sw1Sw2 = { .e = I_COMMAND_OK }
});
return EAPI_OK;
}
enum EapiResult
EapiDummy::External_Authenticate(void) {
return EAPI_OK;
}
enum EapiResult
EapiDummy::Verify_Pin_Block(const byte p2) {
static const uint8_t s_expected_pin_block[8] = {
0x25, 0x11, 0x22, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF
};
static uint8_t s_pin_try_counter = 3;
if (!e1kd->cd.pinData.offlinePinBlock) {
if (!e1kd->cd.pinData.encipheredPinData) {
return EAPI_NOK;
}
cr = acp((struct CardResponse){
.responseData = nullptr,
.parsed = nullptr,
.sw1Sw2 = { .e = I_COMMAND_OK }
});
return EAPI_OK;
}
enum EapiResult res = EAPI_NOK;
if (0 == s_pin_try_counter) {
cr->sw1Sw2.e = E_AUTHENTICATION_METHOD_BLOCKED;
res = EAPI_OK;
} else {
cr = acp((struct CardResponse){});
if (memcmp(e1kd->cd.pinData.offlinePinBlock->raw, s_expected_pin_block, 8) == 0) {
s_pin_try_counter = 3;
cr->sw1Sw2.e = I_COMMAND_OK;
res = EAPI_OK;
} else {
s_pin_try_counter--;
const union EmvStatus tmp = { .raw = { 0x63, integer_cast<uint8_t>(0xC0 + s_pin_try_counter) } };
cr->sw1Sw2 = tmp;
res = EAPI_OK;
}
}
return res;
}
enum EapiResult
EapiDummy::Get_Challenge() {
cr = acp((struct CardResponse){
.responseData = nullptr,
.parsed = acp((struct ParsedResponseData){
.fci = nullptr,
.responseMessageTemplate = nullptr,
.readRecordResponeMessageTemplate = nullptr,
.unpredictableNumber = acp((struct UnpredictableNumber){})
}),
.sw1Sw2 = { .e = I_COMMAND_OK }
});
static const size_t s_unSize = sizeof(cr->parsed->unpredictableNumber->c);
for (size_t i = 0; i < s_unSize; i++) {
cr->parsed->unpredictableNumber->c[i] = integer_cast<uint8_t>(rand() % numeric_limits<uint8_t>::max());
}
return EAPI_OK;
}
enum EapiResult
EapiDummy::Generate_Ac() {
return EAPI_OK;
}