-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseTest.java
181 lines (147 loc) · 5.24 KB
/
BaseTest.java
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
package tests;
import jcmint.JCMint;
import cz.muni.fi.crocs.rcard.client.CardManager;
import cz.muni.fi.crocs.rcard.client.CardType;
import cz.muni.fi.crocs.rcard.client.RunConfig;
import cz.muni.fi.crocs.rcard.client.Util;
import javax.smartcardio.CardException;
import javax.smartcardio.CommandAPDU;
import javax.smartcardio.ResponseAPDU;
import java.util.ArrayList;
/**
* Base Test class.
* Note: If simulator cannot be started try adding "-noverify" JVM parameter
*
* @author Petr Svenda, Dusan Klinec (ph4r05)
*/
public class BaseTest {
private static String APPLET_AID = "6a636d696e74617070";
private static byte APPLET_AID_BYTE[] = Util.hexStringToByteArray(APPLET_AID);
protected CardType cardType = CardType.JCARDSIMLOCAL;
protected boolean simulateStateful = false;
protected CardManager statefulCard = null;
public BaseTest() {
}
/**
* Creates card manager and connects to the card.
*
* @return
* @throws Exception
*/
public CardManager connect() throws Exception {
return connect(null);
}
public CardManager connect(byte[] installData) throws Exception {
if (simulateStateful && statefulCard != null){
return statefulCard;
} else if (simulateStateful){
statefulCard = connectRaw(installData);
return statefulCard;
}
return connectRaw(installData);
}
public CardManager connectRaw(byte[] installData) throws Exception {
final CardManager cardMngr = new CardManager(true, APPLET_AID_BYTE);
final RunConfig runCfg = RunConfig.getDefaultConfig();
System.setProperty("com.licel.jcardsim.object_deletion_supported", "1");
System.setProperty("com.licel.jcardsim.sign.dsasigner.computedhash", "1");
// Otherwise simulator requires modulus to be composite
System.setProperty("com.licel.jcardsim.bouncycastle.rsa.allow_unsafe_mod", "true");
// Set to statically seed RandomData in the applet by "02", hexcoded
// System.setProperty("com.licel.jcardsim.randomdata.seed", "02");
// Set to seed RandomData from the SecureRandom
// System.setProperty("com.licel.jcardsim.randomdata.secure", "1");
runCfg.setTestCardType(cardType);
runCfg.setTargetReaderIndex(2);
if (cardType == CardType.REMOTE){
runCfg.setRemoteAddress("http://127.0.0.1:9901");
runCfg.setRemoteCardType(CardType.PHYSICAL);
// runCfg.setRemoteCardType(CardType.JCARDSIMLOCAL);
runCfg.setAid(APPLET_AID_BYTE); // performs select after connect
} else if (cardType != CardType.PHYSICAL && cardType != CardType.PHYSICAL_JAVAX) {
// Running in the simulator
runCfg.setAppletToSimulate(JCMint.class)
.setTestCardType(CardType.JCARDSIMLOCAL)
.setbReuploadApplet(true)
.setInstallData(installData);
}
if (!cardMngr.connect(runCfg)) {
throw new RuntimeException("Connection failed");
}
return cardMngr;
}
/**
* Convenience method for connecting and sending
* @param cmd
* @return
*/
public ResponseAPDU connectAndSend(CommandAPDU cmd) throws Exception {
return connect().transmit(cmd);
}
/**
* Convenience method for building APDU command
* @param data
* @return
*/
public static CommandAPDU buildApdu(String data){
return new CommandAPDU(Util.hexStringToByteArray(data));
}
/**
* Convenience method for building APDU command
* @param data
* @return
*/
public static CommandAPDU buildApdu(byte[] data){
return new CommandAPDU(data);
}
/**
* Convenience method for building APDU command
* @param data
* @return
*/
public static CommandAPDU buildApdu(CommandAPDU data){
return data;
}
/**
* Sending command to the card.
* Enables to send init commands before the main one.
*
* @param cardMngr
* @param command
* @param initCommands
* @return
* @throws CardException
*/
public ResponseAPDU sendCommandWithInitSequence(CardManager cardMngr, String command, ArrayList<String> initCommands) throws CardException {
if (initCommands != null) {
for (String cmd : initCommands) {
cardMngr.getChannel().transmit(buildApdu(cmd));
}
}
final ResponseAPDU resp = cardMngr.getChannel().transmit(buildApdu(command));
return resp;
}
public CardType getCardType() {
return cardType;
}
public BaseTest setCardType(CardType cardType) {
this.cardType = cardType;
return this;
}
public boolean isSimulateStateful() {
return simulateStateful;
}
public BaseTest setSimulateStateful(boolean simulateStateful) {
this.simulateStateful = simulateStateful;
return this;
}
public boolean isPhysical() {
return cardType == CardType.PHYSICAL || cardType == CardType.PHYSICAL_JAVAX;
}
public boolean isStateful(){
return isPhysical() || simulateStateful;
}
public boolean canReinstall(){
return !isPhysical() && !simulateStateful;
}
}