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
|
package cz.crcs.ectester.reader;
import com.licel.jcardsim.io.JavaxSmartCardInterface;
import cz.crcs.ectester.common.util.ByteUtil;
import javacard.framework.AID;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
import javax.smartcardio.*;
import java.util.*;
/**
* @author Petr Svenda petr@svenda.com
* @author Jan Jancar johny@neuromancer.sk
*/
public class CardMngr {
private CardTerminal terminal = null;
private CardChannel channel = null;
private Card card = null;
// Simulator related attributes
private JavaxSmartCardInterface simulator = null;
private boolean simulate = false;
private boolean verbose = true;
private boolean chunking = false;
private final byte[] selectCM = {
(byte) 0x00, (byte) 0xa4, (byte) 0x04, (byte) 0x00, (byte) 0x07, (byte) 0xa0, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x18, (byte) 0x43, (byte) 0x4d};
public static final byte OFFSET_CLA = 0x00;
public static final byte OFFSET_INS = 0x01;
public static final byte OFFSET_P1 = 0x02;
public static final byte OFFSET_P2 = 0x03;
public static final byte OFFSET_LC = 0x04;
public static final byte OFFSET_DATA = 0x05;
public static final byte HEADER_LENGTH = 0x05;
public static final short DATA_RECORD_LENGTH = (short) 0x80; // 128B per record
public static final short NUMBER_OF_RECORDS = (short) 0x0a; // 10 records
public CardMngr() {
}
public CardMngr(boolean verbose) {
this.verbose = verbose;
}
public CardMngr(boolean verbose, boolean simulate) {
this(verbose);
this.simulate = simulate;
}
private void connectWithHighest() throws CardException {
try {
card = terminal.connect("T=1");
} catch (CardException ex) {
if (verbose)
System.out.println("T=1 failed, trying protocol '*'");
card = terminal.connect("*");
if (card.getProtocol().equals("T=0")) {
chunking = true;
}
}
}
public boolean connectToCard() throws CardException {
if (simulate)
return true;
// TRY ALL READERS, FIND FIRST SELECTABLE
List<CardTerminal> terminalList = getReaderList();
if (terminalList == null || terminalList.isEmpty()) {
System.err.println("No terminals found");
return false;
}
//List numbers of Card readers
boolean cardFound = false;
for (int i = 0; i < terminalList.size(); i++) {
if (verbose)
System.out.println(i + " : " + terminalList.get(i));
terminal = terminalList.get(i);
if (terminal.isCardPresent()) {
connectWithHighest();
if (verbose)
System.out.println("card: " + card);
channel = card.getBasicChannel();
//reset the card
if (verbose)
System.out.println(ByteUtil.bytesToHex(card.getATR().getBytes()));
cardFound = true;
}
}
return cardFound;
}
public boolean connectToCardSelect() throws CardException {
if (simulate)
return true;
// Test available card - if more present, let user to select one
List<CardTerminal> terminalList = CardMngr.getReaderList();
if (terminalList == null || terminalList.isEmpty()) {
System.err.println("ERROR: No suitable reader with card detected. Please check your reader connection");
return false;
} else {
if (terminalList.size() == 1) {
terminal = terminalList.get(0); // return first and only reader
} else {
int terminalIndex = 1;
// Let user select target terminal
for (CardTerminal terminal : terminalList) {
Card card;
try {
card = terminal.connect("*");
ATR atr = card.getATR();
System.out.println(terminalIndex + " : " + terminal.getName() + " - " + ByteUtil.bytesToHex(atr.getBytes()));
terminalIndex++;
} catch (CardException ex) {
ex.printStackTrace(System.out);
}
}
System.out.print("Select index of target reader you like to use 1.." + (terminalIndex - 1) + ": ");
Scanner sc = new Scanner(System.in);
int answ = sc.nextInt();
System.out.println(String.format("%d", answ));
answ--; // is starting with 0
// BUGBUG; verify allowed index range
terminal = terminalList.get(answ);
}
}
if (terminal != null) {
connectWithHighest();
if (verbose)
System.out.println("card: " + card);
channel = card.getBasicChannel();
}
return true;
}
public boolean reconnectToCard(byte[] selectAPDU) throws CardException {
if (simulate)
return true;
if (connected()) {
disconnectFromCard();
}
boolean result = connectToCard();
if (result) {
// Select our application on card
send(selectAPDU);
}
return result;
}
public boolean connected() {
return simulate || card != null;
}
public void disconnectFromCard() throws CardException {
if (simulate)
return;
if (card != null) {
card.disconnect(false);
card = null;
}
}
public void setChunking(boolean state) {
chunking = state;
}
public String getProtocol() {
if (simulate) {
return simulator.getProtocol();
} else {
if (card != null) {
return card.getProtocol();
} else {
return null;
}
}
}
// Functions for CPLC taken and modified from https://github.com/martinpaljak/GlobalPlatformPro
private static final byte CLA_GP = (byte) 0x80;
private static final byte ISO7816_INS_GET_DATA = (byte) 0xCA;
private static final byte[] FETCH_GP_CPLC_APDU = {CLA_GP, ISO7816_INS_GET_DATA, (byte) 0x9F, (byte) 0x7F, (byte) 0x00};
private static final byte[] FETCH_ISO_CPLC_APDU = {ISO7816.CLA_ISO7816, ISO7816_INS_GET_DATA, (byte) 0x9F, (byte) 0x7F, (byte) 0x00};
private static final byte[] FETCH_GP_CARDDATA_APDU = {CLA_GP, ISO7816_INS_GET_DATA, (byte) 0x00, (byte) 0x66, (byte) 0x00};
public byte[] fetchCPLC() throws CardException {
// Try CPLC via GP
ResponseAPDU resp = send(FETCH_GP_CPLC_APDU);
// If GP CLA fails, try with ISO
if (resp.getSW() == (ISO7816.SW_CLA_NOT_SUPPORTED & 0xffff)) {
resp = send(FETCH_ISO_CPLC_APDU);
}
if (resp.getSW() == (ISO7816.SW_NO_ERROR & 0xffff)) {
return resp.getData();
}
return null;
}
public static final class CPLC {
public enum Field {
ICFabricator,
ICType,
OperatingSystemID,
OperatingSystemReleaseDate,
OperatingSystemReleaseLevel,
ICFabricationDate,
ICSerialNumber,
ICBatchIdentifier,
ICModuleFabricator,
ICModulePackagingDate,
ICCManufacturer,
ICEmbeddingDate,
ICPrePersonalizer,
ICPrePersonalizationEquipmentDate,
ICPrePersonalizationEquipmentID,
ICPersonalizer,
ICPersonalizationDate,
ICPersonalizationEquipmentID
}
private Map<Field, byte[]> values = new TreeMap<>();
public CPLC(byte[] data) {
if (data == null) {
return;
}
if (data.length < 3 || data[2] != 0x2A) {
throw new IllegalArgumentException("CPLC must be 0x2A bytes long");
}
//offset = TLVUtils.skipTag(data, offset, (short)0x9F7F);
short offset = 3;
values.put(Field.ICFabricator, Arrays.copyOfRange(data, offset, offset + 2));
offset += 2;
values.put(Field.ICType, Arrays.copyOfRange(data, offset, offset + 2));
offset += 2;
values.put(Field.OperatingSystemID, Arrays.copyOfRange(data, offset, offset + 2));
offset += 2;
values.put(Field.OperatingSystemReleaseDate, Arrays.copyOfRange(data, offset, offset + 2));
offset += 2;
values.put(Field.OperatingSystemReleaseLevel, Arrays.copyOfRange(data, offset, offset + 2));
offset += 2;
values.put(Field.ICFabricationDate, Arrays.copyOfRange(data, offset, offset + 2));
offset += 2;
values.put(Field.ICSerialNumber, Arrays.copyOfRange(data, offset, offset + 4));
offset += 4;
values.put(Field.ICBatchIdentifier, Arrays.copyOfRange(data, offset, offset + 2));
offset += 2;
values.put(Field.ICModuleFabricator, Arrays.copyOfRange(data, offset, offset + 2));
offset += 2;
values.put(Field.ICModulePackagingDate, Arrays.copyOfRange(data, offset, offset + 2));
offset += 2;
values.put(Field.ICCManufacturer, Arrays.copyOfRange(data, offset, offset + 2));
offset += 2;
values.put(Field.ICEmbeddingDate, Arrays.copyOfRange(data, offset, offset + 2));
offset += 2;
values.put(Field.ICPrePersonalizer, Arrays.copyOfRange(data, offset, offset + 2));
offset += 2;
values.put(Field.ICPrePersonalizationEquipmentDate, Arrays.copyOfRange(data, offset, offset + 2));
offset += 2;
values.put(Field.ICPrePersonalizationEquipmentID, Arrays.copyOfRange(data, offset, offset + 4));
offset += 4;
values.put(Field.ICPersonalizer, Arrays.copyOfRange(data, offset, offset + 2));
offset += 2;
values.put(Field.ICPersonalizationDate, Arrays.copyOfRange(data, offset, offset + 2));
offset += 2;
values.put(Field.ICPersonalizationEquipmentID, Arrays.copyOfRange(data, offset, offset + 4));
offset += 4;
}
public Map<Field, byte[]> values() {
return values;
}
}
public ATR getATR() {
if (simulate) {
return new ATR(simulator.getATR());
} else {
if (card != null) {
return card.getATR();
} else {
return null;
}
}
}
public CPLC getCPLC() throws CardException {
byte[] data = fetchCPLC();
return new CPLC(data);
}
public static String mapCPLCField(CPLC.Field field, byte[] value) {
switch (field) {
case ICFabricator:
String id = ByteUtil.bytesToHex(value, false);
String fabricatorName = "unknown";
if (id.equals("3060")) {
fabricatorName = "Renesas";
}
if (id.equals("4090")) {
fabricatorName = "Infineon";
}
if (id.equals("4180")) {
fabricatorName = "Atmel";
}
if (id.equals("4250")) {
fabricatorName = "Samsung";
}
if (id.equals("4790")) {
fabricatorName = "NXP";
}
return id + " (" + fabricatorName + ")";
default:
return ByteUtil.bytesToHex(value, false);
}
}
public static List<CardTerminal> getReaderList() {
try {
TerminalFactory factory = TerminalFactory.getDefault();
return factory.terminals().list();
} catch (CardException ex) {
System.err.println("Exception : " + ex);
return null;
}
}
private CommandAPDU chunk(CommandAPDU apdu) throws CardException {
if (verbose) {
System.out.print("Chunking:");
}
byte[] data = apdu.getBytes();
int numChunks = (data.length + 254) / 255;
for (int i = 0; i < numChunks; ++i) {
int chunkStart = i * 255;
int chunkLength = 255;
if (chunkStart + chunkLength > data.length) {
chunkLength = data.length - chunkStart;
}
if (verbose) {
System.out.print(" " + chunkLength);
}
byte[] chunk = new byte[chunkLength];
System.arraycopy(data, chunkStart, chunk, 0, chunkLength);
CommandAPDU cmd = new CommandAPDU(apdu.getCLA(), 0x7a, 0, 0, chunk);
ResponseAPDU resp;
if (simulate) {
resp = simulator.transmitCommand(cmd);
} else {
resp = channel.transmit(cmd);
}
if ((short) resp.getSW() != ISO7816.SW_NO_ERROR) {
throw new CardException("Chunking failed!");
}
}
if (verbose)
System.out.println();
return new CommandAPDU(apdu.getCLA(), 0x7b, 0, 0, 0xff);
}
public ResponseAPDU sendAPDU(CommandAPDU apdu) throws CardException {
if (verbose) {
System.out.println(">>>>");
System.out.println(apdu);
System.out.println(ByteUtil.bytesToHex(apdu.getBytes()));
}
long elapsed;
if (chunking && apdu.getNc() >= 0xff) {
apdu = chunk(apdu);
}
elapsed = -System.nanoTime();
ResponseAPDU responseAPDU = channel.transmit(apdu);
elapsed += System.nanoTime();
if (verbose) {
System.out.println(responseAPDU);
System.out.println(ByteUtil.bytesToHex(responseAPDU.getBytes()));
}
if (responseAPDU.getSW1() == (byte) 0x61) {
CommandAPDU apduToSend = new CommandAPDU((byte) 0x00,
(byte) 0xC0, (byte) 0x00, (byte) 0x00,
responseAPDU.getSW2());
responseAPDU = channel.transmit(apduToSend);
if (verbose)
System.out.println(ByteUtil.bytesToHex(responseAPDU.getBytes()));
}
if (verbose) {
System.out.println("<<<<");
System.out.println("Elapsed time (ms): " + elapsed / 1000000);
System.out.println("---------------------------------------------------------");
}
return responseAPDU;
}
public ResponseAPDU sendAPDU(byte[] apdu) throws CardException {
CommandAPDU commandAPDU = new CommandAPDU(apdu);
return sendAPDU(commandAPDU);
}
public boolean prepareLocalSimulatorApplet(byte[] appletAIDArray, byte[] installData, Class<? extends Applet> appletClass) {
simulator = new JavaxSmartCardInterface();
AID appletAID = new AID(appletAIDArray, (short) 0, (byte) appletAIDArray.length);
simulator.installApplet(appletAID, appletClass, installData, (short) 0, (byte) installData.length);
return simulator.selectApplet(appletAID);
}
public ResponseAPDU sendAPDUSimulator(CommandAPDU apdu) throws CardException {
if (verbose) {
System.out.println(">>>>");
System.out.println(apdu);
System.out.println(ByteUtil.bytesToHex(apdu.getBytes()));
}
if (chunking && apdu.getNc() >= 0xff) {
apdu = chunk(apdu);
}
ResponseAPDU response = simulator.transmitCommand(apdu);
byte[] responseBytes = response.getBytes();
if (verbose) {
System.out.println(response);
System.out.println(ByteUtil.bytesToHex(responseBytes));
System.out.println("<<<<");
}
return response;
}
public ResponseAPDU sendAPDUSimulator(byte[] apdu) throws CardException {
CommandAPDU commandAPDU = new CommandAPDU(apdu);
return sendAPDUSimulator(commandAPDU);
}
public ResponseAPDU send(CommandAPDU apdu) throws CardException {
ResponseAPDU response;
if (simulate) {
response = sendAPDUSimulator(apdu);
} else {
response = sendAPDU(apdu);
}
return response;
}
public ResponseAPDU send(byte[] apdu) throws CardException {
CommandAPDU commandAPDU = new CommandAPDU(apdu);
return send(commandAPDU);
}
}
|